Loading...
1// SPDX-License-Identifier: GPL-2.0
2
3/* Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2024 Linaro Ltd.
5 */
6
7#include <linux/errno.h>
8#include <linux/etherdevice.h>
9#include <linux/if_arp.h>
10#include <linux/if_rmnet.h>
11#include <linux/netdevice.h>
12#include <linux/pm_runtime.h>
13#include <linux/skbuff.h>
14#include <net/pkt_sched.h>
15
16#include <linux/remoteproc/qcom_rproc.h>
17
18#include "ipa.h"
19#include "ipa_endpoint.h"
20#include "ipa_mem.h"
21#include "ipa_modem.h"
22#include "ipa_smp2p.h"
23#include "ipa_table.h"
24#include "ipa_uc.h"
25
26#define IPA_NETDEV_NAME "rmnet_ipa%d"
27#define IPA_NETDEV_TAILROOM 0 /* for padding by mux layer */
28#define IPA_NETDEV_TIMEOUT 10 /* seconds */
29
30enum ipa_modem_state {
31 IPA_MODEM_STATE_STOPPED = 0,
32 IPA_MODEM_STATE_STARTING,
33 IPA_MODEM_STATE_RUNNING,
34 IPA_MODEM_STATE_STOPPING,
35};
36
37/**
38 * struct ipa_priv - IPA network device private data
39 * @ipa: IPA pointer
40 * @tx: Transmit endpoint pointer
41 * @rx: Receive endpoint pointer
42 * @work: Work structure used to wake the modem netdev TX queue
43 */
44struct ipa_priv {
45 struct ipa *ipa;
46 struct ipa_endpoint *tx;
47 struct ipa_endpoint *rx;
48 struct work_struct work;
49};
50
51/** ipa_open() - Opens the modem network interface */
52static int ipa_open(struct net_device *netdev)
53{
54 struct ipa_priv *priv = netdev_priv(netdev);
55 struct ipa *ipa = priv->ipa;
56 struct device *dev;
57 int ret;
58
59 dev = ipa->dev;
60 ret = pm_runtime_get_sync(dev);
61 if (ret < 0)
62 goto err_power_put;
63
64 ret = ipa_endpoint_enable_one(priv->tx);
65 if (ret)
66 goto err_power_put;
67
68 ret = ipa_endpoint_enable_one(priv->rx);
69 if (ret)
70 goto err_disable_tx;
71
72 netif_start_queue(netdev);
73
74 pm_runtime_mark_last_busy(dev);
75 (void)pm_runtime_put_autosuspend(dev);
76
77 return 0;
78
79err_disable_tx:
80 ipa_endpoint_disable_one(priv->tx);
81err_power_put:
82 pm_runtime_put_noidle(dev);
83
84 return ret;
85}
86
87/** ipa_stop() - Stops the modem network interface. */
88static int ipa_stop(struct net_device *netdev)
89{
90 struct ipa_priv *priv = netdev_priv(netdev);
91 struct ipa *ipa = priv->ipa;
92 struct device *dev;
93 int ret;
94
95 dev = ipa->dev;
96 ret = pm_runtime_get_sync(dev);
97 if (ret < 0)
98 goto out_power_put;
99
100 netif_stop_queue(netdev);
101
102 ipa_endpoint_disable_one(priv->rx);
103 ipa_endpoint_disable_one(priv->tx);
104out_power_put:
105 pm_runtime_mark_last_busy(dev);
106 (void)pm_runtime_put_autosuspend(dev);
107
108 return 0;
109}
110
111/** ipa_start_xmit() - Transmit an skb
112 * @skb: Socket buffer to be transmitted
113 * @netdev: Network device
114 *
115 * Return: NETDEV_TX_OK if successful (or dropped), NETDEV_TX_BUSY otherwise
116
117 * Normally NETDEV_TX_OK indicates the buffer was successfully transmitted.
118 * If the buffer has an unexpected protocol or its size is out of range it
119 * is quietly dropped, returning NETDEV_TX_OK. NETDEV_TX_BUSY indicates
120 * the buffer cannot be sent at this time and should retried later.
121 */
122static netdev_tx_t
123ipa_start_xmit(struct sk_buff *skb, struct net_device *netdev)
124{
125 struct net_device_stats *stats = &netdev->stats;
126 struct ipa_priv *priv = netdev_priv(netdev);
127 struct ipa_endpoint *endpoint;
128 struct ipa *ipa = priv->ipa;
129 u32 skb_len = skb->len;
130 struct device *dev;
131 int ret;
132
133 if (!skb_len)
134 goto err_drop_skb;
135
136 endpoint = ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX];
137 if (endpoint->config.qmap && skb->protocol != htons(ETH_P_MAP))
138 goto err_drop_skb;
139
140 /* The hardware must be powered for us to transmit, so if we're not
141 * ready we want the network stack to stop queueing until power is
142 * ACTIVE. Once runtime resume has completed, we inform the network
143 * stack it's OK to try transmitting again.
144 *
145 * We learn from pm_runtime_get() whether the hardware is powered.
146 * If it was not, powering up is either started or already underway.
147 * And in that case we want to disable queueing, expecting it to be
148 * re-enabled once power is ACTIVE. But runtime PM and network
149 * transmit run concurrently, and if we're not careful the requests
150 * to stop and start queueing could occur in the wrong order.
151 *
152 * For that reason we *always* stop queueing here, *before* the call
153 * to pm_runtime_get(). If we determine here that power is ACTIVE,
154 * we restart queueing before transmitting the SKB. Otherwise
155 * queueing will eventually be enabled after resume completes.
156 */
157 netif_stop_queue(netdev);
158
159 dev = ipa->dev;
160 ret = pm_runtime_get(dev);
161 if (ret < 1) {
162 /* If a resume won't happen, just drop the packet */
163 if (ret < 0 && ret != -EINPROGRESS) {
164 netif_wake_queue(netdev);
165 pm_runtime_put_noidle(dev);
166 goto err_drop_skb;
167 }
168
169 pm_runtime_put_noidle(dev);
170
171 return NETDEV_TX_BUSY;
172 }
173
174 netif_wake_queue(netdev);
175
176 ret = ipa_endpoint_skb_tx(endpoint, skb);
177
178 pm_runtime_mark_last_busy(dev);
179 (void)pm_runtime_put_autosuspend(dev);
180
181 if (ret) {
182 if (ret != -E2BIG)
183 return NETDEV_TX_BUSY;
184 goto err_drop_skb;
185 }
186
187 stats->tx_packets++;
188 stats->tx_bytes += skb_len;
189
190 return NETDEV_TX_OK;
191
192err_drop_skb:
193 dev_kfree_skb_any(skb);
194 stats->tx_dropped++;
195
196 return NETDEV_TX_OK;
197}
198
199void ipa_modem_skb_rx(struct net_device *netdev, struct sk_buff *skb)
200{
201 struct net_device_stats *stats = &netdev->stats;
202
203 if (skb) {
204 skb->dev = netdev;
205 skb->protocol = htons(ETH_P_MAP);
206 stats->rx_packets++;
207 stats->rx_bytes += skb->len;
208
209 (void)netif_receive_skb(skb);
210 } else {
211 stats->rx_dropped++;
212 }
213}
214
215static const struct net_device_ops ipa_modem_ops = {
216 .ndo_open = ipa_open,
217 .ndo_stop = ipa_stop,
218 .ndo_start_xmit = ipa_start_xmit,
219};
220
221/** ipa_modem_netdev_setup() - netdev setup function for the modem */
222static void ipa_modem_netdev_setup(struct net_device *netdev)
223{
224 netdev->netdev_ops = &ipa_modem_ops;
225
226 netdev->header_ops = NULL;
227 netdev->type = ARPHRD_RAWIP;
228 netdev->hard_header_len = 0;
229 netdev->min_header_len = ETH_HLEN;
230 netdev->min_mtu = ETH_MIN_MTU;
231 netdev->max_mtu = IPA_MTU;
232 netdev->mtu = netdev->max_mtu;
233 netdev->addr_len = 0;
234 netdev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
235 netdev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
236 netdev->priv_flags |= IFF_TX_SKB_SHARING;
237 eth_broadcast_addr(netdev->broadcast);
238
239 /* The endpoint is configured for QMAP */
240 netdev->needed_headroom = sizeof(struct rmnet_map_header);
241 netdev->needed_tailroom = IPA_NETDEV_TAILROOM;
242 netdev->watchdog_timeo = IPA_NETDEV_TIMEOUT * HZ;
243 netdev->hw_features = NETIF_F_SG;
244}
245
246/** ipa_modem_suspend() - suspend callback
247 * @netdev: Network device
248 *
249 * Suspend the modem's endpoints.
250 */
251void ipa_modem_suspend(struct net_device *netdev)
252{
253 struct ipa_priv *priv;
254
255 if (!(netdev->flags & IFF_UP))
256 return;
257
258 priv = netdev_priv(netdev);
259 ipa_endpoint_suspend_one(priv->rx);
260 ipa_endpoint_suspend_one(priv->tx);
261}
262
263/**
264 * ipa_modem_wake_queue_work() - enable modem netdev queue
265 * @work: Work structure
266 *
267 * Re-enable transmit on the modem network device. This is called
268 * in (power management) work queue context, scheduled when resuming
269 * the modem. We can't enable the queue directly in ipa_modem_resume()
270 * because transmits restart the instant the queue is awakened; but the
271 * device power state won't be ACTIVE until *after* ipa_modem_resume()
272 * returns.
273 */
274static void ipa_modem_wake_queue_work(struct work_struct *work)
275{
276 struct ipa_priv *priv = container_of(work, struct ipa_priv, work);
277
278 netif_wake_queue(priv->tx->netdev);
279}
280
281/** ipa_modem_resume() - resume callback for runtime_pm
282 * @dev: pointer to device
283 *
284 * Resume the modem's endpoints.
285 */
286void ipa_modem_resume(struct net_device *netdev)
287{
288 struct ipa_priv *priv;
289
290 if (!(netdev->flags & IFF_UP))
291 return;
292
293 priv = netdev_priv(netdev);
294 ipa_endpoint_resume_one(priv->tx);
295 ipa_endpoint_resume_one(priv->rx);
296
297 /* Arrange for the TX queue to be restarted */
298 (void)queue_pm_work(&priv->work);
299}
300
301int ipa_modem_start(struct ipa *ipa)
302{
303 enum ipa_modem_state state;
304 struct net_device *netdev;
305 struct ipa_priv *priv;
306 int ret;
307
308 /* Only attempt to start the modem if it's stopped */
309 state = atomic_cmpxchg(&ipa->modem_state, IPA_MODEM_STATE_STOPPED,
310 IPA_MODEM_STATE_STARTING);
311
312 /* Silently ignore attempts when running, or when changing state */
313 if (state != IPA_MODEM_STATE_STOPPED)
314 return 0;
315
316 netdev = alloc_netdev(sizeof(struct ipa_priv), IPA_NETDEV_NAME,
317 NET_NAME_UNKNOWN, ipa_modem_netdev_setup);
318 if (!netdev) {
319 ret = -ENOMEM;
320 goto out_set_state;
321 }
322
323 SET_NETDEV_DEV(netdev, ipa->dev);
324 priv = netdev_priv(netdev);
325 priv->ipa = ipa;
326 priv->tx = ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX];
327 priv->rx = ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX];
328 INIT_WORK(&priv->work, ipa_modem_wake_queue_work);
329
330 priv->tx->netdev = netdev;
331 priv->rx->netdev = netdev;
332
333 ipa->modem_netdev = netdev;
334
335 ret = register_netdev(netdev);
336 if (ret) {
337 ipa->modem_netdev = NULL;
338 priv->rx->netdev = NULL;
339 priv->tx->netdev = NULL;
340
341 free_netdev(netdev);
342 }
343
344out_set_state:
345 if (ret)
346 atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED);
347 else
348 atomic_set(&ipa->modem_state, IPA_MODEM_STATE_RUNNING);
349 smp_mb__after_atomic();
350
351 return ret;
352}
353
354int ipa_modem_stop(struct ipa *ipa)
355{
356 struct net_device *netdev = ipa->modem_netdev;
357 enum ipa_modem_state state;
358
359 /* Only attempt to stop the modem if it's running */
360 state = atomic_cmpxchg(&ipa->modem_state, IPA_MODEM_STATE_RUNNING,
361 IPA_MODEM_STATE_STOPPING);
362
363 /* Silently ignore attempts when already stopped */
364 if (state == IPA_MODEM_STATE_STOPPED)
365 return 0;
366
367 /* If we're somewhere between stopped and starting, we're busy */
368 if (state != IPA_MODEM_STATE_RUNNING)
369 return -EBUSY;
370
371 /* Clean up the netdev and endpoints if it was started */
372 if (netdev) {
373 struct ipa_priv *priv = netdev_priv(netdev);
374
375 cancel_work_sync(&priv->work);
376 /* If it was opened, stop it first */
377 if (netdev->flags & IFF_UP)
378 (void)ipa_stop(netdev);
379 unregister_netdev(netdev);
380
381 ipa->modem_netdev = NULL;
382 priv->rx->netdev = NULL;
383 priv->tx->netdev = NULL;
384
385 free_netdev(netdev);
386 }
387
388 atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED);
389 smp_mb__after_atomic();
390
391 return 0;
392}
393
394/* Treat a "clean" modem stop the same as a crash */
395static void ipa_modem_crashed(struct ipa *ipa)
396{
397 struct device *dev = ipa->dev;
398 int ret;
399
400 /* Prevent the modem from triggering a call to ipa_setup() */
401 ipa_smp2p_irq_disable_setup(ipa);
402
403 ret = pm_runtime_get_sync(dev);
404 if (ret < 0) {
405 dev_err(dev, "error %d getting power to handle crash\n", ret);
406 goto out_power_put;
407 }
408
409 ipa_endpoint_modem_pause_all(ipa, true);
410
411 ipa_endpoint_modem_hol_block_clear_all(ipa);
412
413 ipa_table_reset(ipa, true);
414
415 ret = ipa_table_hash_flush(ipa);
416 if (ret)
417 dev_err(dev, "error %d flushing hash caches\n", ret);
418
419 ret = ipa_endpoint_modem_exception_reset_all(ipa);
420 if (ret)
421 dev_err(dev, "error %d resetting exception endpoint\n", ret);
422
423 ipa_endpoint_modem_pause_all(ipa, false);
424
425 ret = ipa_modem_stop(ipa);
426 if (ret)
427 dev_err(dev, "error %d stopping modem\n", ret);
428
429 /* Now prepare for the next modem boot */
430 ret = ipa_mem_zero_modem(ipa);
431 if (ret)
432 dev_err(dev, "error %d zeroing modem memory regions\n", ret);
433
434out_power_put:
435 pm_runtime_mark_last_busy(dev);
436 (void)pm_runtime_put_autosuspend(dev);
437}
438
439static int ipa_modem_notify(struct notifier_block *nb, unsigned long action,
440 void *data)
441{
442 struct ipa *ipa = container_of(nb, struct ipa, nb);
443 struct qcom_ssr_notify_data *notify_data = data;
444 struct device *dev = ipa->dev;
445
446 switch (action) {
447 case QCOM_SSR_BEFORE_POWERUP:
448 dev_info(dev, "received modem starting event\n");
449 ipa_uc_power(ipa);
450 ipa_smp2p_notify_reset(ipa);
451 break;
452
453 case QCOM_SSR_AFTER_POWERUP:
454 dev_info(dev, "received modem running event\n");
455 break;
456
457 case QCOM_SSR_BEFORE_SHUTDOWN:
458 dev_info(dev, "received modem %s event\n",
459 notify_data->crashed ? "crashed" : "stopping");
460 if (ipa->setup_complete)
461 ipa_modem_crashed(ipa);
462 break;
463
464 case QCOM_SSR_AFTER_SHUTDOWN:
465 dev_info(dev, "received modem offline event\n");
466 break;
467
468 default:
469 dev_err(dev, "received unrecognized event %lu\n", action);
470 break;
471 }
472
473 return NOTIFY_OK;
474}
475
476int ipa_modem_config(struct ipa *ipa)
477{
478 void *notifier;
479
480 ipa->nb.notifier_call = ipa_modem_notify;
481
482 notifier = qcom_register_ssr_notifier("mpss", &ipa->nb);
483 if (IS_ERR(notifier))
484 return PTR_ERR(notifier);
485
486 ipa->notifier = notifier;
487
488 return 0;
489}
490
491void ipa_modem_deconfig(struct ipa *ipa)
492{
493 struct device *dev = ipa->dev;
494 int ret;
495
496 ret = qcom_unregister_ssr_notifier(ipa->notifier, &ipa->nb);
497 if (ret)
498 dev_err(dev, "error %d unregistering notifier", ret);
499
500 ipa->notifier = NULL;
501 memset(&ipa->nb, 0, sizeof(ipa->nb));
502}
1// SPDX-License-Identifier: GPL-2.0
2
3/* Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2021 Linaro Ltd.
5 */
6
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>
13
14#include "ipa.h"
15#include "ipa_data.h"
16#include "ipa_endpoint.h"
17#include "ipa_table.h"
18#include "ipa_mem.h"
19#include "ipa_modem.h"
20#include "ipa_smp2p.h"
21#include "ipa_qmi.h"
22
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 */
26
27enum ipa_modem_state {
28 IPA_MODEM_STATE_STOPPED = 0,
29 IPA_MODEM_STATE_STARTING,
30 IPA_MODEM_STATE_RUNNING,
31 IPA_MODEM_STATE_STOPPING,
32};
33
34/** struct ipa_priv - IPA network device private data */
35struct ipa_priv {
36 struct ipa *ipa;
37};
38
39/** ipa_open() - Opens the modem network interface */
40static int ipa_open(struct net_device *netdev)
41{
42 struct ipa_priv *priv = netdev_priv(netdev);
43 struct ipa *ipa = priv->ipa;
44 int ret;
45
46 ret = ipa_endpoint_enable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
47 if (ret)
48 return ret;
49 ret = ipa_endpoint_enable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
50 if (ret)
51 goto err_disable_tx;
52
53 netif_start_queue(netdev);
54
55 return 0;
56
57err_disable_tx:
58 ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
59
60 return ret;
61}
62
63/** ipa_stop() - Stops the modem network interface. */
64static int ipa_stop(struct net_device *netdev)
65{
66 struct ipa_priv *priv = netdev_priv(netdev);
67 struct ipa *ipa = priv->ipa;
68
69 netif_stop_queue(netdev);
70
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]);
73
74 return 0;
75}
76
77/** ipa_start_xmit() - Transmits an skb.
78 * @skb: skb to be transmitted
79 * @dev: network device
80 *
81 * Return codes:
82 * NETDEV_TX_OK: Success
83 * NETDEV_TX_BUSY: Error while transmitting the skb. Try again later
84 */
85static int ipa_start_xmit(struct sk_buff *skb, struct net_device *netdev)
86{
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;
92 int ret;
93
94 if (!skb_len)
95 goto err_drop_skb;
96
97 endpoint = ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX];
98 if (endpoint->data->qmap && skb->protocol != htons(ETH_P_MAP))
99 goto err_drop_skb;
100
101 ret = ipa_endpoint_skb_tx(endpoint, skb);
102 if (ret) {
103 if (ret != -E2BIG)
104 return NETDEV_TX_BUSY;
105 goto err_drop_skb;
106 }
107
108 stats->tx_packets++;
109 stats->tx_bytes += skb_len;
110
111 return NETDEV_TX_OK;
112
113err_drop_skb:
114 dev_kfree_skb_any(skb);
115 stats->tx_dropped++;
116
117 return NETDEV_TX_OK;
118}
119
120void ipa_modem_skb_rx(struct net_device *netdev, struct sk_buff *skb)
121{
122 struct net_device_stats *stats = &netdev->stats;
123
124 if (skb) {
125 skb->dev = netdev;
126 skb->protocol = htons(ETH_P_MAP);
127 stats->rx_packets++;
128 stats->rx_bytes += skb->len;
129
130 (void)netif_receive_skb(skb);
131 } else {
132 stats->rx_dropped++;
133 }
134}
135
136static 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,
140};
141
142/** ipa_modem_netdev_setup() - netdev setup function for the modem */
143static void ipa_modem_netdev_setup(struct net_device *netdev)
144{
145 netdev->netdev_ops = &ipa_modem_ops;
146 ether_setup(netdev);
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;
160}
161
162/** ipa_modem_suspend() - suspend callback
163 * @netdev: Network device
164 *
165 * Suspend the modem's endpoints.
166 */
167void ipa_modem_suspend(struct net_device *netdev)
168{
169 struct ipa_priv *priv = netdev_priv(netdev);
170 struct ipa *ipa = priv->ipa;
171
172 netif_stop_queue(netdev);
173
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]);
176}
177
178/** ipa_modem_resume() - resume callback for runtime_pm
179 * @dev: pointer to device
180 *
181 * Resume the modem's endpoints.
182 */
183void ipa_modem_resume(struct net_device *netdev)
184{
185 struct ipa_priv *priv = netdev_priv(netdev);
186 struct ipa *ipa = priv->ipa;
187
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]);
190
191 netif_wake_queue(netdev);
192}
193
194int ipa_modem_start(struct ipa *ipa)
195{
196 enum ipa_modem_state state;
197 struct net_device *netdev;
198 struct ipa_priv *priv;
199 int ret;
200
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);
204
205 /* Silently ignore attempts when running, or when changing state */
206 if (state != IPA_MODEM_STATE_STOPPED)
207 return 0;
208
209 netdev = alloc_netdev(sizeof(struct ipa_priv), IPA_NETDEV_NAME,
210 NET_NAME_UNKNOWN, ipa_modem_netdev_setup);
211 if (!netdev) {
212 ret = -ENOMEM;
213 goto out_set_state;
214 }
215
216 SET_NETDEV_DEV(netdev, &ipa->pdev->dev);
217 priv = netdev_priv(netdev);
218 priv->ipa = ipa;
219
220 ret = register_netdev(netdev);
221 if (!ret) {
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;
225 } else {
226 free_netdev(netdev);
227 }
228
229out_set_state:
230 if (ret)
231 atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED);
232 else
233 atomic_set(&ipa->modem_state, IPA_MODEM_STATE_RUNNING);
234 smp_mb__after_atomic();
235
236 return ret;
237}
238
239int ipa_modem_stop(struct ipa *ipa)
240{
241 struct net_device *netdev = ipa->modem_netdev;
242 enum ipa_modem_state state;
243
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);
247
248 /* Silently ignore attempts when already stopped */
249 if (state == IPA_MODEM_STATE_STOPPED)
250 return 0;
251
252 /* If we're somewhere between stopped and starting, we're busy */
253 if (state != IPA_MODEM_STATE_RUNNING)
254 return -EBUSY;
255
256 /* Prevent the modem from triggering a call to ipa_setup() */
257 ipa_smp2p_disable(ipa);
258
259 /* Stop the queue and disable the endpoints if it's open */
260 if (netdev) {
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);
266 free_netdev(netdev);
267 }
268
269 atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED);
270 smp_mb__after_atomic();
271
272 return 0;
273}
274
275/* Treat a "clean" modem stop the same as a crash */
276static void ipa_modem_crashed(struct ipa *ipa)
277{
278 struct device *dev = &ipa->pdev->dev;
279 int ret;
280
281 ipa_endpoint_modem_pause_all(ipa, true);
282
283 ipa_endpoint_modem_hol_block_clear_all(ipa);
284
285 ipa_table_reset(ipa, true);
286
287 ret = ipa_table_hash_flush(ipa);
288 if (ret)
289 dev_err(dev, "error %d flushing hash caches\n", ret);
290
291 ret = ipa_endpoint_modem_exception_reset_all(ipa);
292 if (ret)
293 dev_err(dev, "error %d resetting exception endpoint\n", ret);
294
295 ipa_endpoint_modem_pause_all(ipa, false);
296
297 ret = ipa_modem_stop(ipa);
298 if (ret)
299 dev_err(dev, "error %d stopping modem\n", ret);
300
301 /* Now prepare for the next modem boot */
302 ret = ipa_mem_zero_modem(ipa);
303 if (ret)
304 dev_err(dev, "error %d zeroing modem memory regions\n", ret);
305}
306
307static int ipa_modem_notify(struct notifier_block *nb, unsigned long action,
308 void *data)
309{
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;
313
314 switch (action) {
315 case QCOM_SSR_BEFORE_POWERUP:
316 dev_info(dev, "received modem starting event\n");
317 ipa_smp2p_notify_reset(ipa);
318 break;
319
320 case QCOM_SSR_AFTER_POWERUP:
321 dev_info(dev, "received modem running event\n");
322 break;
323
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);
329 break;
330
331 case QCOM_SSR_AFTER_SHUTDOWN:
332 dev_info(dev, "received modem offline event\n");
333 break;
334
335 default:
336 dev_err(dev, "received unrecognized event %lu\n", action);
337 break;
338 }
339
340 return NOTIFY_OK;
341}
342
343int ipa_modem_init(struct ipa *ipa, bool modem_init)
344{
345 return ipa_smp2p_init(ipa, modem_init);
346}
347
348void ipa_modem_exit(struct ipa *ipa)
349{
350 ipa_smp2p_exit(ipa);
351}
352
353int ipa_modem_config(struct ipa *ipa)
354{
355 void *notifier;
356
357 ipa->nb.notifier_call = ipa_modem_notify;
358
359 notifier = qcom_register_ssr_notifier("mpss", &ipa->nb);
360 if (IS_ERR(notifier))
361 return PTR_ERR(notifier);
362
363 ipa->notifier = notifier;
364
365 return 0;
366}
367
368void ipa_modem_deconfig(struct ipa *ipa)
369{
370 struct device *dev = &ipa->pdev->dev;
371 int ret;
372
373 ret = qcom_unregister_ssr_notifier(ipa->notifier, &ipa->nb);
374 if (ret)
375 dev_err(dev, "error %d unregistering notifier", ret);
376
377 ipa->notifier = NULL;
378 memset(&ipa->nb, 0, sizeof(ipa->nb));
379}
380
381int ipa_modem_setup(struct ipa *ipa)
382{
383 return ipa_qmi_setup(ipa);
384}
385
386void ipa_modem_teardown(struct ipa *ipa)
387{
388 ipa_qmi_teardown(ipa);
389}