ipa_modem.c 12 KB

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