ipa_modem.c 12 KB

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