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