xref: /linux/drivers/net/ipa/ipa_modem.c (revision a751449f8b477e0e1d97f778ed97ae9f6576b690)
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 #include "ipa_uc.h"
23 #include "ipa_clock.h"
24 
25 #define IPA_NETDEV_NAME		"rmnet_ipa%d"
26 #define IPA_NETDEV_TAILROOM	0	/* for padding by mux layer */
27 #define IPA_NETDEV_TIMEOUT	10	/* seconds */
28 
29 enum ipa_modem_state {
30 	IPA_MODEM_STATE_STOPPED	= 0,
31 	IPA_MODEM_STATE_STARTING,
32 	IPA_MODEM_STATE_RUNNING,
33 	IPA_MODEM_STATE_STOPPING,
34 };
35 
36 /** struct ipa_priv - IPA network device private data */
37 struct ipa_priv {
38 	struct ipa *ipa;
39 };
40 
41 /** ipa_open() - Opens the modem network interface */
42 static int ipa_open(struct net_device *netdev)
43 {
44 	struct ipa_priv *priv = netdev_priv(netdev);
45 	struct ipa *ipa = priv->ipa;
46 	int ret;
47 
48 	ipa_clock_get(ipa);
49 
50 	ret = ipa_endpoint_enable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
51 	if (ret)
52 		goto err_clock_put;
53 
54 	ret = ipa_endpoint_enable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
55 	if (ret)
56 		goto err_disable_tx;
57 
58 	netif_start_queue(netdev);
59 
60 	return 0;
61 
62 err_disable_tx:
63 	ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
64 err_clock_put:
65 	ipa_clock_put(ipa);
66 
67 	return ret;
68 }
69 
70 /** ipa_stop() - Stops the modem network interface. */
71 static int ipa_stop(struct net_device *netdev)
72 {
73 	struct ipa_priv *priv = netdev_priv(netdev);
74 	struct ipa *ipa = priv->ipa;
75 
76 	netif_stop_queue(netdev);
77 
78 	ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
79 	ipa_endpoint_disable_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
80 
81 	ipa_clock_put(ipa);
82 
83 	return 0;
84 }
85 
86 /** ipa_start_xmit() - Transmits an skb.
87  * @skb: skb to be transmitted
88  * @dev: network device
89  *
90  * Return codes:
91  * NETDEV_TX_OK: Success
92  * NETDEV_TX_BUSY: Error while transmitting the skb. Try again later
93  */
94 static int ipa_start_xmit(struct sk_buff *skb, struct net_device *netdev)
95 {
96 	struct net_device_stats *stats = &netdev->stats;
97 	struct ipa_priv *priv = netdev_priv(netdev);
98 	struct ipa_endpoint *endpoint;
99 	struct ipa *ipa = priv->ipa;
100 	u32 skb_len = skb->len;
101 	int ret;
102 
103 	if (!skb_len)
104 		goto err_drop_skb;
105 
106 	endpoint = ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX];
107 	if (endpoint->data->qmap && skb->protocol != htons(ETH_P_MAP))
108 		goto err_drop_skb;
109 
110 	ret = ipa_endpoint_skb_tx(endpoint, skb);
111 	if (ret) {
112 		if (ret != -E2BIG)
113 			return NETDEV_TX_BUSY;
114 		goto err_drop_skb;
115 	}
116 
117 	stats->tx_packets++;
118 	stats->tx_bytes += skb_len;
119 
120 	return NETDEV_TX_OK;
121 
122 err_drop_skb:
123 	dev_kfree_skb_any(skb);
124 	stats->tx_dropped++;
125 
126 	return NETDEV_TX_OK;
127 }
128 
129 void ipa_modem_skb_rx(struct net_device *netdev, struct sk_buff *skb)
130 {
131 	struct net_device_stats *stats = &netdev->stats;
132 
133 	if (skb) {
134 		skb->dev = netdev;
135 		skb->protocol = htons(ETH_P_MAP);
136 		stats->rx_packets++;
137 		stats->rx_bytes += skb->len;
138 
139 		(void)netif_receive_skb(skb);
140 	} else {
141 		stats->rx_dropped++;
142 	}
143 }
144 
145 static const struct net_device_ops ipa_modem_ops = {
146 	.ndo_open	= ipa_open,
147 	.ndo_stop	= ipa_stop,
148 	.ndo_start_xmit	= ipa_start_xmit,
149 };
150 
151 /** ipa_modem_netdev_setup() - netdev setup function for the modem */
152 static void ipa_modem_netdev_setup(struct net_device *netdev)
153 {
154 	netdev->netdev_ops = &ipa_modem_ops;
155 	ether_setup(netdev);
156 	/* No header ops (override value set by ether_setup()) */
157 	netdev->header_ops = NULL;
158 	netdev->type = ARPHRD_RAWIP;
159 	netdev->hard_header_len = 0;
160 	netdev->max_mtu = IPA_MTU;
161 	netdev->mtu = netdev->max_mtu;
162 	netdev->addr_len = 0;
163 	netdev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
164 	/* The endpoint is configured for QMAP */
165 	netdev->needed_headroom = sizeof(struct rmnet_map_header);
166 	netdev->needed_tailroom = IPA_NETDEV_TAILROOM;
167 	netdev->watchdog_timeo = IPA_NETDEV_TIMEOUT * HZ;
168 	netdev->hw_features = NETIF_F_SG;
169 }
170 
171 /** ipa_modem_suspend() - suspend callback
172  * @netdev:	Network device
173  *
174  * Suspend the modem's endpoints.
175  */
176 void ipa_modem_suspend(struct net_device *netdev)
177 {
178 	struct ipa_priv *priv = netdev_priv(netdev);
179 	struct ipa *ipa = priv->ipa;
180 
181 	netif_stop_queue(netdev);
182 
183 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
184 	ipa_endpoint_suspend_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
185 }
186 
187 /** ipa_modem_resume() - resume callback for runtime_pm
188  * @dev: pointer to device
189  *
190  * Resume the modem's endpoints.
191  */
192 void ipa_modem_resume(struct net_device *netdev)
193 {
194 	struct ipa_priv *priv = netdev_priv(netdev);
195 	struct ipa *ipa = priv->ipa;
196 
197 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]);
198 	ipa_endpoint_resume_one(ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]);
199 
200 	netif_wake_queue(netdev);
201 }
202 
203 int ipa_modem_start(struct ipa *ipa)
204 {
205 	enum ipa_modem_state state;
206 	struct net_device *netdev;
207 	struct ipa_priv *priv;
208 	int ret;
209 
210 	/* Only attempt to start the modem if it's stopped */
211 	state = atomic_cmpxchg(&ipa->modem_state, IPA_MODEM_STATE_STOPPED,
212 			       IPA_MODEM_STATE_STARTING);
213 
214 	/* Silently ignore attempts when running, or when changing state */
215 	if (state != IPA_MODEM_STATE_STOPPED)
216 		return 0;
217 
218 	netdev = alloc_netdev(sizeof(struct ipa_priv), IPA_NETDEV_NAME,
219 			      NET_NAME_UNKNOWN, ipa_modem_netdev_setup);
220 	if (!netdev) {
221 		ret = -ENOMEM;
222 		goto out_set_state;
223 	}
224 
225 	SET_NETDEV_DEV(netdev, &ipa->pdev->dev);
226 	priv = netdev_priv(netdev);
227 	priv->ipa = ipa;
228 
229 	ret = register_netdev(netdev);
230 	if (!ret) {
231 		ipa->modem_netdev = netdev;
232 		ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = netdev;
233 		ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = netdev;
234 	} else {
235 		free_netdev(netdev);
236 	}
237 
238 out_set_state:
239 	if (ret)
240 		atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED);
241 	else
242 		atomic_set(&ipa->modem_state, IPA_MODEM_STATE_RUNNING);
243 	smp_mb__after_atomic();
244 
245 	return ret;
246 }
247 
248 int ipa_modem_stop(struct ipa *ipa)
249 {
250 	struct net_device *netdev = ipa->modem_netdev;
251 	enum ipa_modem_state state;
252 
253 	/* Only attempt to stop the modem if it's running */
254 	state = atomic_cmpxchg(&ipa->modem_state, IPA_MODEM_STATE_RUNNING,
255 			       IPA_MODEM_STATE_STOPPING);
256 
257 	/* Silently ignore attempts when already stopped */
258 	if (state == IPA_MODEM_STATE_STOPPED)
259 		return 0;
260 
261 	/* If we're somewhere between stopped and starting, we're busy */
262 	if (state != IPA_MODEM_STATE_RUNNING)
263 		return -EBUSY;
264 
265 	/* Prevent the modem from triggering a call to ipa_setup() */
266 	ipa_smp2p_disable(ipa);
267 
268 	/* Stop the queue and disable the endpoints if it's open */
269 	if (netdev) {
270 		(void)ipa_stop(netdev);
271 		ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->netdev = NULL;
272 		ipa->name_map[IPA_ENDPOINT_AP_MODEM_TX]->netdev = NULL;
273 		ipa->modem_netdev = NULL;
274 		unregister_netdev(netdev);
275 		free_netdev(netdev);
276 	}
277 
278 	atomic_set(&ipa->modem_state, IPA_MODEM_STATE_STOPPED);
279 	smp_mb__after_atomic();
280 
281 	return 0;
282 }
283 
284 /* Treat a "clean" modem stop the same as a crash */
285 static void ipa_modem_crashed(struct ipa *ipa)
286 {
287 	struct device *dev = &ipa->pdev->dev;
288 	int ret;
289 
290 	ipa_clock_get(ipa);
291 
292 	ipa_endpoint_modem_pause_all(ipa, true);
293 
294 	ipa_endpoint_modem_hol_block_clear_all(ipa);
295 
296 	ipa_table_reset(ipa, true);
297 
298 	ret = ipa_table_hash_flush(ipa);
299 	if (ret)
300 		dev_err(dev, "error %d flushing hash caches\n", ret);
301 
302 	ret = ipa_endpoint_modem_exception_reset_all(ipa);
303 	if (ret)
304 		dev_err(dev, "error %d resetting exception endpoint\n", ret);
305 
306 	ipa_endpoint_modem_pause_all(ipa, false);
307 
308 	ret = ipa_modem_stop(ipa);
309 	if (ret)
310 		dev_err(dev, "error %d stopping modem\n", ret);
311 
312 	/* Now prepare for the next modem boot */
313 	ret = ipa_mem_zero_modem(ipa);
314 	if (ret)
315 		dev_err(dev, "error %d zeroing modem memory regions\n", ret);
316 
317 	ipa_clock_put(ipa);
318 }
319 
320 static int ipa_modem_notify(struct notifier_block *nb, unsigned long action,
321 			    void *data)
322 {
323 	struct ipa *ipa = container_of(nb, struct ipa, nb);
324 	struct qcom_ssr_notify_data *notify_data = data;
325 	struct device *dev = &ipa->pdev->dev;
326 
327 	switch (action) {
328 	case QCOM_SSR_BEFORE_POWERUP:
329 		dev_info(dev, "received modem starting event\n");
330 		ipa_uc_clock(ipa);
331 		ipa_smp2p_notify_reset(ipa);
332 		break;
333 
334 	case QCOM_SSR_AFTER_POWERUP:
335 		dev_info(dev, "received modem running event\n");
336 		break;
337 
338 	case QCOM_SSR_BEFORE_SHUTDOWN:
339 		dev_info(dev, "received modem %s event\n",
340 			 notify_data->crashed ? "crashed" : "stopping");
341 		if (ipa->setup_complete)
342 			ipa_modem_crashed(ipa);
343 		break;
344 
345 	case QCOM_SSR_AFTER_SHUTDOWN:
346 		dev_info(dev, "received modem offline event\n");
347 		break;
348 
349 	default:
350 		dev_err(dev, "received unrecognized event %lu\n", action);
351 		break;
352 	}
353 
354 	return NOTIFY_OK;
355 }
356 
357 int ipa_modem_init(struct ipa *ipa, bool modem_init)
358 {
359 	return ipa_smp2p_init(ipa, modem_init);
360 }
361 
362 void ipa_modem_exit(struct ipa *ipa)
363 {
364 	ipa_smp2p_exit(ipa);
365 }
366 
367 int ipa_modem_config(struct ipa *ipa)
368 {
369 	void *notifier;
370 
371 	ipa->nb.notifier_call = ipa_modem_notify;
372 
373 	notifier = qcom_register_ssr_notifier("mpss", &ipa->nb);
374 	if (IS_ERR(notifier))
375 		return PTR_ERR(notifier);
376 
377 	ipa->notifier = notifier;
378 
379 	return 0;
380 }
381 
382 void ipa_modem_deconfig(struct ipa *ipa)
383 {
384 	struct device *dev = &ipa->pdev->dev;
385 	int ret;
386 
387 	ret = qcom_unregister_ssr_notifier(ipa->notifier, &ipa->nb);
388 	if (ret)
389 		dev_err(dev, "error %d unregistering notifier", ret);
390 
391 	ipa->notifier = NULL;
392 	memset(&ipa->nb, 0, sizeof(ipa->nb));
393 }
394