1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2021 Linaro Ltd.
7 #include <linux/errno.h>
8 #include <linux/if_arp.h>
9 #include <linux/netdevice.h>
10 #include <linux/skbuff.h>
11 #include <linux/if_rmnet.h>
12 #include <linux/remoteproc/qcom_rproc.h>
16 #include "ipa_endpoint.h"
17 #include "ipa_table.h"
19 #include "ipa_modem.h"
20 #include "ipa_smp2p.h"
23 #define IPA_NETDEV_NAME "rmnet_ipa%d"
24 #define IPA_NETDEV_TAILROOM 0 /* for padding by mux layer */
25 #define IPA_NETDEV_TIMEOUT 10 /* seconds */
27 enum ipa_modem_state {
28 IPA_MODEM_STATE_STOPPED = 0,
29 IPA_MODEM_STATE_STARTING,
30 IPA_MODEM_STATE_RUNNING,
31 IPA_MODEM_STATE_STOPPING,
34 /** struct ipa_priv - IPA network device private data */
39 /** ipa_open() - Opens the modem network interface */
40 static int ipa_open(struct net_device *netdev)
42 struct ipa_priv *priv = netdev_priv(netdev);
43 struct ipa *ipa = priv->ipa;
46 ret = ipa_endpoint_enable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
49 ret = ipa_endpoint_enable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
53 netif_start_queue(netdev);
58 ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
63 /** ipa_stop() - Stops the modem network interface. */
64 static int ipa_stop(struct net_device *netdev)
66 struct ipa_priv *priv = netdev_priv(netdev);
67 struct ipa *ipa = priv->ipa;
69 netif_stop_queue(netdev);
71 ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
72 ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
77 /** ipa_start_xmit() - Transmits an skb.
78 * @skb: skb to be transmitted
79 * @dev: network device
82 * NETDEV_TX_OK: Success
83 * NETDEV_TX_BUSY: Error while transmitting the skb. Try again later
85 static int ipa_start_xmit(struct sk_buff *skb, struct net_device *netdev)
87 struct net_device_stats *stats = &netdev->stats;
88 struct ipa_priv *priv = netdev_priv(netdev);
89 struct ipa_endpoint *endpoint;
90 struct ipa *ipa = priv->ipa;
91 u32 skb_len = skb->len;
97 endpoint = ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX];
98 if (endpoint->data->qmap && skb->protocol != htons(ETH_P_MAP))
101 ret = ipa_endpoint_skb_tx(endpoint, skb);
104 return NETDEV_TX_BUSY;
109 stats->tx_bytes += skb_len;
114 dev_kfree_skb_any(skb);
120 void ipa_modem_skb_rx(struct net_device *netdev, struct sk_buff *skb)
122 struct net_device_stats *stats = &netdev->stats;
126 skb->protocol = htons(ETH_P_MAP);
128 stats->rx_bytes += skb->len;
130 (void)netif_receive_skb(skb);
136 static const struct net_device_ops ipa_modem_ops = {
137 .ndo_open = ipa_open,
138 .ndo_stop = ipa_stop,
139 .ndo_start_xmit = ipa_start_xmit,
142 /** ipa_modem_netdev_setup() - netdev setup function for the modem */
143 static void ipa_modem_netdev_setup(struct net_device *netdev)
145 netdev->netdev_ops = &ipa_modem_ops;
147 /* No header ops (override value set by ether_setup()) */
148 netdev->header_ops = NULL;
149 netdev->type = ARPHRD_RAWIP;
150 netdev->hard_header_len = 0;
151 netdev->max_mtu = IPA_MTU;
152 netdev->mtu = netdev->max_mtu;
153 netdev->addr_len = 0;
154 netdev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
155 /* The endpoint is configured for QMAP */
156 netdev->needed_headroom = sizeof(struct rmnet_map_header);
157 netdev->needed_tailroom = IPA_NETDEV_TAILROOM;
158 netdev->watchdog_timeo = IPA_NETDEV_TIMEOUT * HZ;
159 netdev->hw_features = NETIF_F_SG;
162 /** ipa_modem_suspend() - suspend callback
163 * @netdev: Network device
165 * Suspend the modem's endpoints.
167 void ipa_modem_suspend(struct net_device *netdev)
169 struct ipa_priv *priv = netdev_priv(netdev);
170 struct ipa *ipa = priv->ipa;
172 netif_stop_queue(netdev);
174 ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
175 ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
178 /** ipa_modem_resume() - resume callback for runtime_pm
179 * @dev: pointer to device
181 * Resume the modem's endpoints.
183 void ipa_modem_resume(struct net_device *netdev)
185 struct ipa_priv *priv = netdev_priv(netdev);
186 struct ipa *ipa = priv->ipa;
188 ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
189 ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
191 netif_wake_queue(netdev);
194 int ipa_modem_start(struct ipa *ipa)
196 enum ipa_modem_state state;
197 struct net_device *netdev;
198 struct ipa_priv *priv;
201 /* Only attempt to start the modem if it's stopped */
202 state = atomic_cmpxchg(&ipa->modem_state, IPA_MODEM_STATE_STOPPED,
203 IPA_MODEM_STATE_STARTING);
205 /* Silently ignore attempts when running, or when changing state */
206 if (state != IPA_MODEM_STATE_STOPPED)
209 netdev = alloc_netdev(sizeof(struct ipa_priv), IPA_NETDEV_NAME,
210 NET_NAME_UNKNOWN, ipa_modem_netdev_setup);
216 SET_NETDEV_DEV(netdev, &ipa->pdev->dev);
217 priv = netdev_priv(netdev);
220 ret = register_netdev(netdev);
222 ipa->modem_netdev = netdev;
223 ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = netdev;
224 ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = netdev;
231 atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED);
233 atomic_set(&ipa->modem_state, IPA_MODEM_STATE_RUNNING);
234 smp_mb__after_atomic();
239 int ipa_modem_stop(struct ipa *ipa)
241 struct net_device *netdev = ipa->modem_netdev;
242 enum ipa_modem_state state;
244 /* Only attempt to stop the modem if it's running */
245 state = atomic_cmpxchg(&ipa->modem_state, IPA_MODEM_STATE_RUNNING,
246 IPA_MODEM_STATE_STOPPING);
248 /* Silently ignore attempts when already stopped */
249 if (state == IPA_MODEM_STATE_STOPPED)
252 /* If we're somewhere between stopped and starting, we're busy */
253 if (state != IPA_MODEM_STATE_RUNNING)
256 /* Prevent the modem from triggering a call to ipa_setup() */
257 ipa_smp2p_disable(ipa);
259 /* Stop the queue and disable the endpoints if it's open */
261 (void)ipa_stop(netdev);
262 ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = NULL;
263 ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = NULL;
264 ipa->modem_netdev = NULL;
265 unregister_netdev(netdev);
269 atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED);
270 smp_mb__after_atomic();
275 /* Treat a "clean" modem stop the same as a crash */
276 static void ipa_modem_crashed(struct ipa *ipa)
278 struct device *dev = &ipa->pdev->dev;
281 ipa_endpoint_modem_pause_all(ipa, true);
283 ipa_endpoint_modem_hol_block_clear_all(ipa);
285 ipa_table_reset(ipa, true);
287 ret = ipa_table_hash_flush(ipa);
289 dev_err(dev, "error %d flushing hash caches\n", ret);
291 ret = ipa_endpoint_modem_exception_reset_all(ipa);
293 dev_err(dev, "error %d resetting exception endpoint\n", ret);
295 ipa_endpoint_modem_pause_all(ipa, false);
297 ret = ipa_modem_stop(ipa);
299 dev_err(dev, "error %d stopping modem\n", ret);
301 /* Now prepare for the next modem boot */
302 ret = ipa_mem_zero_modem(ipa);
304 dev_err(dev, "error %d zeroing modem memory regions\n", ret);
307 static int ipa_modem_notify(struct notifier_block *nb, unsigned long action,
310 struct ipa *ipa = container_of(nb, struct ipa, nb);
311 struct qcom_ssr_notify_data *notify_data = data;
312 struct device *dev = &ipa->pdev->dev;
315 case QCOM_SSR_BEFORE_POWERUP:
316 dev_info(dev, "received modem starting event\n");
317 ipa_smp2p_notify_reset(ipa);
320 case QCOM_SSR_AFTER_POWERUP:
321 dev_info(dev, "received modem running event\n");
324 case QCOM_SSR_BEFORE_SHUTDOWN:
325 dev_info(dev, "received modem %s event\n",
326 notify_data->crashed ? "crashed" : "stopping");
327 if (ipa->setup_complete)
328 ipa_modem_crashed(ipa);
331 case QCOM_SSR_AFTER_SHUTDOWN:
332 dev_info(dev, "received modem offline event\n");
336 dev_err(dev, "received unrecognized event %lu\n", action);
343 int ipa_modem_init(struct ipa *ipa, bool modem_init)
345 return ipa_smp2p_init(ipa, modem_init);
348 void ipa_modem_exit(struct ipa *ipa)
353 int ipa_modem_config(struct ipa *ipa)
357 ipa->nb.notifier_call = ipa_modem_notify;
359 notifier = qcom_register_ssr_notifier("mpss", &ipa->nb);
360 if (IS_ERR(notifier))
361 return PTR_ERR(notifier);
363 ipa->notifier = notifier;
368 void ipa_modem_deconfig(struct ipa *ipa)
370 struct device *dev = &ipa->pdev->dev;
373 ret = qcom_unregister_ssr_notifier(ipa->notifier, &ipa->nb);
375 dev_err(dev, "error %d unregistering notifier", ret);
377 ipa->notifier = NULL;
378 memset(&ipa->nb, 0, sizeof(ipa->nb));
381 int ipa_modem_setup(struct ipa *ipa)
383 return ipa_qmi_setup(ipa);
386 void ipa_modem_teardown(struct ipa *ipa)
388 ipa_qmi_teardown(ipa);