xref: /linux/drivers/net/ipa/ipa_modem.c (revision 7caeabd726f22e6a6c44c434574fb489986e5baa)
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/pm_runtime.h>
13 #include <linux/remoteproc/qcom_rproc.h>
14 
15 #include "ipa.h"
16 #include "ipa_data.h"
17 #include "ipa_endpoint.h"
18 #include "ipa_table.h"
19 #include "ipa_mem.h"
20 #include "ipa_modem.h"
21 #include "ipa_smp2p.h"
22 #include "ipa_qmi.h"
23 #include "ipa_uc.h"
24 #include "ipa_clock.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 
30 enum 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  * @work:	Work structure used to wake the modem netdev TX queue
41  */
42 struct ipa_priv {
43 	struct ipa *ipa;
44 	struct work_struct work;
45 };
46 
47 /** ipa_open() - Opens the modem network interface */
48 static int ipa_open(struct net_device *netdev)
49 {
50 	struct ipa_priv *priv = netdev_priv(netdev);
51 	struct ipa *ipa = priv->ipa;
52 	int ret;
53 
54 	ret = ipa_clock_get(ipa);
55 	if (WARN_ON(ret < 0))
56 		goto err_clock_put;
57 
58 	ret = ipa_endpoint_enable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
59 	if (ret)
60 		goto err_clock_put;
61 
62 	ret = ipa_endpoint_enable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
63 	if (ret)
64 		goto err_disable_tx;
65 
66 	netif_start_queue(netdev);
67 
68 	(void)ipa_clock_put(ipa);
69 
70 	return 0;
71 
72 err_disable_tx:
73 	ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
74 err_clock_put:
75 	(void)ipa_clock_put(ipa);
76 
77 	return ret;
78 }
79 
80 /** ipa_stop() - Stops the modem network interface. */
81 static int ipa_stop(struct net_device *netdev)
82 {
83 	struct ipa_priv *priv = netdev_priv(netdev);
84 	struct ipa *ipa = priv->ipa;
85 	int ret;
86 
87 	ret = ipa_clock_get(ipa);
88 	if (WARN_ON(ret < 0))
89 		goto out_clock_put;
90 
91 	netif_stop_queue(netdev);
92 
93 	ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
94 	ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
95 out_clock_put:
96 	(void)ipa_clock_put(ipa);
97 
98 	return 0;
99 }
100 
101 /** ipa_start_xmit() - Transmits an skb.
102  * @skb: skb to be transmitted
103  * @dev: network device
104  *
105  * Return codes:
106  * NETDEV_TX_OK: Success
107  * NETDEV_TX_BUSY: Error while transmitting the skb. Try again later
108  */
109 static netdev_tx_t
110 ipa_start_xmit(struct sk_buff *skb, struct net_device *netdev)
111 {
112 	struct net_device_stats *stats = &netdev->stats;
113 	struct ipa_priv *priv = netdev_priv(netdev);
114 	struct ipa_endpoint *endpoint;
115 	struct ipa *ipa = priv->ipa;
116 	u32 skb_len = skb->len;
117 	struct device *dev;
118 	int ret;
119 
120 	if (!skb_len)
121 		goto err_drop_skb;
122 
123 	endpoint = ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX];
124 	if (endpoint->data->qmap && skb->protocol != htons(ETH_P_MAP))
125 		goto err_drop_skb;
126 
127 	/* The hardware must be powered for us to transmit */
128 	dev = &ipa->pdev->dev;
129 	ret = pm_runtime_get(dev);
130 	if (ret < 1) {
131 		/* If a resume won't happen, just drop the packet */
132 		if (ret < 0 && ret != -EINPROGRESS) {
133 			pm_runtime_put_noidle(dev);
134 			goto err_drop_skb;
135 		}
136 
137 		/* No power (yet).  Stop the network stack from transmitting
138 		 * until we're resumed; ipa_modem_resume() arranges for the
139 		 * TX queue to be started again.
140 		 */
141 		netif_stop_queue(netdev);
142 
143 		(void)pm_runtime_put(dev);
144 
145 		return NETDEV_TX_BUSY;
146 	}
147 
148 	ret = ipa_endpoint_skb_tx(endpoint, skb);
149 
150 	(void)pm_runtime_put(dev);
151 
152 	if (ret) {
153 		if (ret != -E2BIG)
154 			return NETDEV_TX_BUSY;
155 		goto err_drop_skb;
156 	}
157 
158 	stats->tx_packets++;
159 	stats->tx_bytes += skb_len;
160 
161 	return NETDEV_TX_OK;
162 
163 err_drop_skb:
164 	dev_kfree_skb_any(skb);
165 	stats->tx_dropped++;
166 
167 	return NETDEV_TX_OK;
168 }
169 
170 void ipa_modem_skb_rx(struct net_device *netdev, struct sk_buff *skb)
171 {
172 	struct net_device_stats *stats = &netdev->stats;
173 
174 	if (skb) {
175 		skb->dev = netdev;
176 		skb->protocol = htons(ETH_P_MAP);
177 		stats->rx_packets++;
178 		stats->rx_bytes += skb->len;
179 
180 		(void)netif_receive_skb(skb);
181 	} else {
182 		stats->rx_dropped++;
183 	}
184 }
185 
186 static const struct net_device_ops ipa_modem_ops = {
187 	.ndo_open	= ipa_open,
188 	.ndo_stop	= ipa_stop,
189 	.ndo_start_xmit	= ipa_start_xmit,
190 };
191 
192 /** ipa_modem_netdev_setup() - netdev setup function for the modem */
193 static void ipa_modem_netdev_setup(struct net_device *netdev)
194 {
195 	netdev->netdev_ops = &ipa_modem_ops;
196 	ether_setup(netdev);
197 	/* No header ops (override value set by ether_setup()) */
198 	netdev->header_ops = NULL;
199 	netdev->type = ARPHRD_RAWIP;
200 	netdev->hard_header_len = 0;
201 	netdev->max_mtu = IPA_MTU;
202 	netdev->mtu = netdev->max_mtu;
203 	netdev->addr_len = 0;
204 	netdev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
205 	/* The endpoint is configured for QMAP */
206 	netdev->needed_headroom = sizeof(struct rmnet_map_header);
207 	netdev->needed_tailroom = IPA_NETDEV_TAILROOM;
208 	netdev->watchdog_timeo = IPA_NETDEV_TIMEOUT * HZ;
209 	netdev->hw_features = NETIF_F_SG;
210 }
211 
212 /** ipa_modem_suspend() - suspend callback
213  * @netdev:	Network device
214  *
215  * Suspend the modem's endpoints.
216  */
217 void ipa_modem_suspend(struct net_device *netdev)
218 {
219 	struct ipa_priv *priv = netdev_priv(netdev);
220 	struct ipa *ipa = priv->ipa;
221 
222 	if (!(netdev->flags & IFF_UP))
223 		return;
224 
225 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
226 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
227 }
228 
229 /**
230  * ipa_modem_wake_queue_work() - enable modem netdev queue
231  * @work:	Work structure
232  *
233  * Re-enable transmit on the modem network device.  This is called
234  * in (power management) work queue context, scheduled when resuming
235  * the modem.  We can't enable the queue directly in ipa_modem_resume()
236  * because transmits restart the instant the queue is awakened; but the
237  * device power state won't be ACTIVE until *after* ipa_modem_resume()
238  * returns.
239  */
240 static void ipa_modem_wake_queue_work(struct work_struct *work)
241 {
242 	struct ipa_priv *priv = container_of(work, struct ipa_priv, work);
243 
244 	netif_wake_queue(priv->ipa->modem_netdev);
245 }
246 
247 /** ipa_modem_resume() - resume callback for runtime_pm
248  * @dev: pointer to device
249  *
250  * Resume the modem's endpoints.
251  */
252 void ipa_modem_resume(struct net_device *netdev)
253 {
254 	struct ipa_priv *priv = netdev_priv(netdev);
255 	struct ipa *ipa = priv->ipa;
256 
257 	if (!(netdev->flags & IFF_UP))
258 		return;
259 
260 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
261 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
262 
263 	/* Arrange for the TX queue to be restarted */
264 	(void)queue_pm_work(&priv->work);
265 }
266 
267 int ipa_modem_start(struct ipa *ipa)
268 {
269 	enum ipa_modem_state state;
270 	struct net_device *netdev;
271 	struct ipa_priv *priv;
272 	int ret;
273 
274 	/* Only attempt to start the modem if it's stopped */
275 	state = atomic_cmpxchg(&ipa->modem_state, IPA_MODEM_STATE_STOPPED,
276 			       IPA_MODEM_STATE_STARTING);
277 
278 	/* Silently ignore attempts when running, or when changing state */
279 	if (state != IPA_MODEM_STATE_STOPPED)
280 		return 0;
281 
282 	netdev = alloc_netdev(sizeof(struct ipa_priv), IPA_NETDEV_NAME,
283 			      NET_NAME_UNKNOWN, ipa_modem_netdev_setup);
284 	if (!netdev) {
285 		ret = -ENOMEM;
286 		goto out_set_state;
287 	}
288 
289 	SET_NETDEV_DEV(netdev, &ipa->pdev->dev);
290 	priv = netdev_priv(netdev);
291 	priv->ipa = ipa;
292 	INIT_WORK(&priv->work, ipa_modem_wake_queue_work);
293 	ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = netdev;
294 	ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = netdev;
295 	ipa->modem_netdev = netdev;
296 
297 	ret = register_netdev(netdev);
298 	if (ret) {
299 		ipa->modem_netdev = NULL;
300 		ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = NULL;
301 		ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = NULL;
302 		free_netdev(netdev);
303 	}
304 
305 out_set_state:
306 	if (ret)
307 		atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED);
308 	else
309 		atomic_set(&ipa->modem_state, IPA_MODEM_STATE_RUNNING);
310 	smp_mb__after_atomic();
311 
312 	return ret;
313 }
314 
315 int ipa_modem_stop(struct ipa *ipa)
316 {
317 	struct net_device *netdev = ipa->modem_netdev;
318 	enum ipa_modem_state state;
319 
320 	/* Only attempt to stop the modem if it's running */
321 	state = atomic_cmpxchg(&ipa->modem_state, IPA_MODEM_STATE_RUNNING,
322 			       IPA_MODEM_STATE_STOPPING);
323 
324 	/* Silently ignore attempts when already stopped */
325 	if (state == IPA_MODEM_STATE_STOPPED)
326 		return 0;
327 
328 	/* If we're somewhere between stopped and starting, we're busy */
329 	if (state != IPA_MODEM_STATE_RUNNING)
330 		return -EBUSY;
331 
332 	/* Prevent the modem from triggering a call to ipa_setup() */
333 	ipa_smp2p_disable(ipa);
334 
335 	/* Clean up the netdev and endpoints if it was started */
336 	if (netdev) {
337 		struct ipa_priv *priv = netdev_priv(netdev);
338 
339 		cancel_work_sync(&priv->work);
340 		/* If it was opened, stop it first */
341 		if (netdev->flags & IFF_UP)
342 			(void)ipa_stop(netdev);
343 		unregister_netdev(netdev);
344 		ipa->modem_netdev = NULL;
345 		ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = NULL;
346 		ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = NULL;
347 		free_netdev(netdev);
348 	}
349 
350 	atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED);
351 	smp_mb__after_atomic();
352 
353 	return 0;
354 }
355 
356 /* Treat a "clean" modem stop the same as a crash */
357 static void ipa_modem_crashed(struct ipa *ipa)
358 {
359 	struct device *dev = &ipa->pdev->dev;
360 	int ret;
361 
362 	ret = ipa_clock_get(ipa);
363 	if (WARN_ON(ret < 0))
364 		goto out_clock_put;
365 
366 	ipa_endpoint_modem_pause_all(ipa, true);
367 
368 	ipa_endpoint_modem_hol_block_clear_all(ipa);
369 
370 	ipa_table_reset(ipa, true);
371 
372 	ret = ipa_table_hash_flush(ipa);
373 	if (ret)
374 		dev_err(dev, "error %d flushing hash caches\n", ret);
375 
376 	ret = ipa_endpoint_modem_exception_reset_all(ipa);
377 	if (ret)
378 		dev_err(dev, "error %d resetting exception endpoint\n", ret);
379 
380 	ipa_endpoint_modem_pause_all(ipa, false);
381 
382 	ret = ipa_modem_stop(ipa);
383 	if (ret)
384 		dev_err(dev, "error %d stopping modem\n", ret);
385 
386 	/* Now prepare for the next modem boot */
387 	ret = ipa_mem_zero_modem(ipa);
388 	if (ret)
389 		dev_err(dev, "error %d zeroing modem memory regions\n", ret);
390 
391 out_clock_put:
392 	(void)ipa_clock_put(ipa);
393 }
394 
395 static int ipa_modem_notify(struct notifier_block *nb, unsigned long action,
396 			    void *data)
397 {
398 	struct ipa *ipa = container_of(nb, struct ipa, nb);
399 	struct qcom_ssr_notify_data *notify_data = data;
400 	struct device *dev = &ipa->pdev->dev;
401 
402 	switch (action) {
403 	case QCOM_SSR_BEFORE_POWERUP:
404 		dev_info(dev, "received modem starting event\n");
405 		ipa_uc_clock(ipa);
406 		ipa_smp2p_notify_reset(ipa);
407 		break;
408 
409 	case QCOM_SSR_AFTER_POWERUP:
410 		dev_info(dev, "received modem running event\n");
411 		break;
412 
413 	case QCOM_SSR_BEFORE_SHUTDOWN:
414 		dev_info(dev, "received modem %s event\n",
415 			 notify_data->crashed ? "crashed" : "stopping");
416 		if (ipa->setup_complete)
417 			ipa_modem_crashed(ipa);
418 		break;
419 
420 	case QCOM_SSR_AFTER_SHUTDOWN:
421 		dev_info(dev, "received modem offline event\n");
422 		break;
423 
424 	default:
425 		dev_err(dev, "received unrecognized event %lu\n", action);
426 		break;
427 	}
428 
429 	return NOTIFY_OK;
430 }
431 
432 int ipa_modem_init(struct ipa *ipa, bool modem_init)
433 {
434 	return ipa_smp2p_init(ipa, modem_init);
435 }
436 
437 void ipa_modem_exit(struct ipa *ipa)
438 {
439 	ipa_smp2p_exit(ipa);
440 }
441 
442 int ipa_modem_config(struct ipa *ipa)
443 {
444 	void *notifier;
445 
446 	ipa->nb.notifier_call = ipa_modem_notify;
447 
448 	notifier = qcom_register_ssr_notifier("mpss", &ipa->nb);
449 	if (IS_ERR(notifier))
450 		return PTR_ERR(notifier);
451 
452 	ipa->notifier = notifier;
453 
454 	return 0;
455 }
456 
457 void ipa_modem_deconfig(struct ipa *ipa)
458 {
459 	struct device *dev = &ipa->pdev->dev;
460 	int ret;
461 
462 	ret = qcom_unregister_ssr_notifier(ipa->notifier, &ipa->nb);
463 	if (ret)
464 		dev_err(dev, "error %d unregistering notifier", ret);
465 
466 	ipa->notifier = NULL;
467 	memset(&ipa->nb, 0, sizeof(ipa->nb));
468 }
469