mhi_net.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* MHI Network driver - Network over MHI bus
  3. *
  4. * Copyright (C) 2020 Linaro Ltd <[email protected]>
  5. */
  6. #include <linux/if_arp.h>
  7. #include <linux/mhi.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/module.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/u64_stats_sync.h>
  13. #define MHI_NET_MIN_MTU ETH_MIN_MTU
  14. #define MHI_NET_MAX_MTU 0xffff
  15. #define MHI_NET_DEFAULT_MTU 0x4000
  16. struct mhi_net_stats {
  17. u64_stats_t rx_packets;
  18. u64_stats_t rx_bytes;
  19. u64_stats_t rx_errors;
  20. u64_stats_t tx_packets;
  21. u64_stats_t tx_bytes;
  22. u64_stats_t tx_errors;
  23. u64_stats_t tx_dropped;
  24. struct u64_stats_sync tx_syncp;
  25. struct u64_stats_sync rx_syncp;
  26. };
  27. struct mhi_net_dev {
  28. struct mhi_device *mdev;
  29. struct net_device *ndev;
  30. struct sk_buff *skbagg_head;
  31. struct sk_buff *skbagg_tail;
  32. struct delayed_work rx_refill;
  33. struct mhi_net_stats stats;
  34. u32 rx_queue_sz;
  35. int msg_enable;
  36. unsigned int mru;
  37. };
  38. struct mhi_device_info {
  39. const char *netname;
  40. };
  41. static int mhi_ndo_open(struct net_device *ndev)
  42. {
  43. struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
  44. /* Feed the rx buffer pool */
  45. schedule_delayed_work(&mhi_netdev->rx_refill, 0);
  46. /* Carrier is established via out-of-band channel (e.g. qmi) */
  47. netif_carrier_on(ndev);
  48. netif_start_queue(ndev);
  49. return 0;
  50. }
  51. static int mhi_ndo_stop(struct net_device *ndev)
  52. {
  53. struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
  54. netif_stop_queue(ndev);
  55. netif_carrier_off(ndev);
  56. cancel_delayed_work_sync(&mhi_netdev->rx_refill);
  57. return 0;
  58. }
  59. static netdev_tx_t mhi_ndo_xmit(struct sk_buff *skb, struct net_device *ndev)
  60. {
  61. struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
  62. struct mhi_device *mdev = mhi_netdev->mdev;
  63. int err;
  64. err = mhi_queue_skb(mdev, DMA_TO_DEVICE, skb, skb->len, MHI_EOT);
  65. if (unlikely(err)) {
  66. net_err_ratelimited("%s: Failed to queue TX buf (%d)\n",
  67. ndev->name, err);
  68. dev_kfree_skb_any(skb);
  69. goto exit_drop;
  70. }
  71. if (mhi_queue_is_full(mdev, DMA_TO_DEVICE))
  72. netif_stop_queue(ndev);
  73. return NETDEV_TX_OK;
  74. exit_drop:
  75. u64_stats_update_begin(&mhi_netdev->stats.tx_syncp);
  76. u64_stats_inc(&mhi_netdev->stats.tx_dropped);
  77. u64_stats_update_end(&mhi_netdev->stats.tx_syncp);
  78. return NETDEV_TX_OK;
  79. }
  80. static void mhi_ndo_get_stats64(struct net_device *ndev,
  81. struct rtnl_link_stats64 *stats)
  82. {
  83. struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
  84. unsigned int start;
  85. do {
  86. start = u64_stats_fetch_begin_irq(&mhi_netdev->stats.rx_syncp);
  87. stats->rx_packets = u64_stats_read(&mhi_netdev->stats.rx_packets);
  88. stats->rx_bytes = u64_stats_read(&mhi_netdev->stats.rx_bytes);
  89. stats->rx_errors = u64_stats_read(&mhi_netdev->stats.rx_errors);
  90. } while (u64_stats_fetch_retry_irq(&mhi_netdev->stats.rx_syncp, start));
  91. do {
  92. start = u64_stats_fetch_begin_irq(&mhi_netdev->stats.tx_syncp);
  93. stats->tx_packets = u64_stats_read(&mhi_netdev->stats.tx_packets);
  94. stats->tx_bytes = u64_stats_read(&mhi_netdev->stats.tx_bytes);
  95. stats->tx_errors = u64_stats_read(&mhi_netdev->stats.tx_errors);
  96. stats->tx_dropped = u64_stats_read(&mhi_netdev->stats.tx_dropped);
  97. } while (u64_stats_fetch_retry_irq(&mhi_netdev->stats.tx_syncp, start));
  98. }
  99. static const struct net_device_ops mhi_netdev_ops = {
  100. .ndo_open = mhi_ndo_open,
  101. .ndo_stop = mhi_ndo_stop,
  102. .ndo_start_xmit = mhi_ndo_xmit,
  103. .ndo_get_stats64 = mhi_ndo_get_stats64,
  104. };
  105. static void mhi_net_setup(struct net_device *ndev)
  106. {
  107. ndev->header_ops = NULL; /* No header */
  108. ndev->type = ARPHRD_RAWIP;
  109. ndev->hard_header_len = 0;
  110. ndev->addr_len = 0;
  111. ndev->flags = IFF_POINTOPOINT | IFF_NOARP;
  112. ndev->netdev_ops = &mhi_netdev_ops;
  113. ndev->mtu = MHI_NET_DEFAULT_MTU;
  114. ndev->min_mtu = MHI_NET_MIN_MTU;
  115. ndev->max_mtu = MHI_NET_MAX_MTU;
  116. ndev->tx_queue_len = 1000;
  117. }
  118. static struct sk_buff *mhi_net_skb_agg(struct mhi_net_dev *mhi_netdev,
  119. struct sk_buff *skb)
  120. {
  121. struct sk_buff *head = mhi_netdev->skbagg_head;
  122. struct sk_buff *tail = mhi_netdev->skbagg_tail;
  123. /* This is non-paged skb chaining using frag_list */
  124. if (!head) {
  125. mhi_netdev->skbagg_head = skb;
  126. return skb;
  127. }
  128. if (!skb_shinfo(head)->frag_list)
  129. skb_shinfo(head)->frag_list = skb;
  130. else
  131. tail->next = skb;
  132. head->len += skb->len;
  133. head->data_len += skb->len;
  134. head->truesize += skb->truesize;
  135. mhi_netdev->skbagg_tail = skb;
  136. return mhi_netdev->skbagg_head;
  137. }
  138. static void mhi_net_dl_callback(struct mhi_device *mhi_dev,
  139. struct mhi_result *mhi_res)
  140. {
  141. struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev);
  142. struct sk_buff *skb = mhi_res->buf_addr;
  143. int free_desc_count;
  144. free_desc_count = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE);
  145. if (unlikely(mhi_res->transaction_status)) {
  146. switch (mhi_res->transaction_status) {
  147. case -EOVERFLOW:
  148. /* Packet can not fit in one MHI buffer and has been
  149. * split over multiple MHI transfers, do re-aggregation.
  150. * That usually means the device side MTU is larger than
  151. * the host side MTU/MRU. Since this is not optimal,
  152. * print a warning (once).
  153. */
  154. netdev_warn_once(mhi_netdev->ndev,
  155. "Fragmented packets received, fix MTU?\n");
  156. skb_put(skb, mhi_res->bytes_xferd);
  157. mhi_net_skb_agg(mhi_netdev, skb);
  158. break;
  159. case -ENOTCONN:
  160. /* MHI layer stopping/resetting the DL channel */
  161. dev_kfree_skb_any(skb);
  162. return;
  163. default:
  164. /* Unknown error, simply drop */
  165. dev_kfree_skb_any(skb);
  166. u64_stats_update_begin(&mhi_netdev->stats.rx_syncp);
  167. u64_stats_inc(&mhi_netdev->stats.rx_errors);
  168. u64_stats_update_end(&mhi_netdev->stats.rx_syncp);
  169. }
  170. } else {
  171. skb_put(skb, mhi_res->bytes_xferd);
  172. if (mhi_netdev->skbagg_head) {
  173. /* Aggregate the final fragment */
  174. skb = mhi_net_skb_agg(mhi_netdev, skb);
  175. mhi_netdev->skbagg_head = NULL;
  176. }
  177. switch (skb->data[0] & 0xf0) {
  178. case 0x40:
  179. skb->protocol = htons(ETH_P_IP);
  180. break;
  181. case 0x60:
  182. skb->protocol = htons(ETH_P_IPV6);
  183. break;
  184. default:
  185. skb->protocol = htons(ETH_P_MAP);
  186. break;
  187. }
  188. u64_stats_update_begin(&mhi_netdev->stats.rx_syncp);
  189. u64_stats_inc(&mhi_netdev->stats.rx_packets);
  190. u64_stats_add(&mhi_netdev->stats.rx_bytes, skb->len);
  191. u64_stats_update_end(&mhi_netdev->stats.rx_syncp);
  192. __netif_rx(skb);
  193. }
  194. /* Refill if RX buffers queue becomes low */
  195. if (free_desc_count >= mhi_netdev->rx_queue_sz / 2)
  196. schedule_delayed_work(&mhi_netdev->rx_refill, 0);
  197. }
  198. static void mhi_net_ul_callback(struct mhi_device *mhi_dev,
  199. struct mhi_result *mhi_res)
  200. {
  201. struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev);
  202. struct net_device *ndev = mhi_netdev->ndev;
  203. struct mhi_device *mdev = mhi_netdev->mdev;
  204. struct sk_buff *skb = mhi_res->buf_addr;
  205. /* Hardware has consumed the buffer, so free the skb (which is not
  206. * freed by the MHI stack) and perform accounting.
  207. */
  208. dev_consume_skb_any(skb);
  209. u64_stats_update_begin(&mhi_netdev->stats.tx_syncp);
  210. if (unlikely(mhi_res->transaction_status)) {
  211. /* MHI layer stopping/resetting the UL channel */
  212. if (mhi_res->transaction_status == -ENOTCONN) {
  213. u64_stats_update_end(&mhi_netdev->stats.tx_syncp);
  214. return;
  215. }
  216. u64_stats_inc(&mhi_netdev->stats.tx_errors);
  217. } else {
  218. u64_stats_inc(&mhi_netdev->stats.tx_packets);
  219. u64_stats_add(&mhi_netdev->stats.tx_bytes, mhi_res->bytes_xferd);
  220. }
  221. u64_stats_update_end(&mhi_netdev->stats.tx_syncp);
  222. if (netif_queue_stopped(ndev) && !mhi_queue_is_full(mdev, DMA_TO_DEVICE))
  223. netif_wake_queue(ndev);
  224. }
  225. static void mhi_net_rx_refill_work(struct work_struct *work)
  226. {
  227. struct mhi_net_dev *mhi_netdev = container_of(work, struct mhi_net_dev,
  228. rx_refill.work);
  229. struct net_device *ndev = mhi_netdev->ndev;
  230. struct mhi_device *mdev = mhi_netdev->mdev;
  231. struct sk_buff *skb;
  232. unsigned int size;
  233. int err;
  234. size = mhi_netdev->mru ? mhi_netdev->mru : READ_ONCE(ndev->mtu);
  235. while (!mhi_queue_is_full(mdev, DMA_FROM_DEVICE)) {
  236. skb = netdev_alloc_skb(ndev, size);
  237. if (unlikely(!skb))
  238. break;
  239. err = mhi_queue_skb(mdev, DMA_FROM_DEVICE, skb, size, MHI_EOT);
  240. if (unlikely(err)) {
  241. net_err_ratelimited("%s: Failed to queue RX buf (%d)\n",
  242. ndev->name, err);
  243. kfree_skb(skb);
  244. break;
  245. }
  246. /* Do not hog the CPU if rx buffers are consumed faster than
  247. * queued (unlikely).
  248. */
  249. cond_resched();
  250. }
  251. /* If we're still starved of rx buffers, reschedule later */
  252. if (mhi_get_free_desc_count(mdev, DMA_FROM_DEVICE) == mhi_netdev->rx_queue_sz)
  253. schedule_delayed_work(&mhi_netdev->rx_refill, HZ / 2);
  254. }
  255. static int mhi_net_newlink(struct mhi_device *mhi_dev, struct net_device *ndev)
  256. {
  257. struct mhi_net_dev *mhi_netdev;
  258. int err;
  259. mhi_netdev = netdev_priv(ndev);
  260. dev_set_drvdata(&mhi_dev->dev, mhi_netdev);
  261. mhi_netdev->ndev = ndev;
  262. mhi_netdev->mdev = mhi_dev;
  263. mhi_netdev->skbagg_head = NULL;
  264. mhi_netdev->mru = mhi_dev->mhi_cntrl->mru;
  265. INIT_DELAYED_WORK(&mhi_netdev->rx_refill, mhi_net_rx_refill_work);
  266. u64_stats_init(&mhi_netdev->stats.rx_syncp);
  267. u64_stats_init(&mhi_netdev->stats.tx_syncp);
  268. /* Start MHI channels */
  269. err = mhi_prepare_for_transfer(mhi_dev);
  270. if (err)
  271. return err;
  272. /* Number of transfer descriptors determines size of the queue */
  273. mhi_netdev->rx_queue_sz = mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE);
  274. err = register_netdev(ndev);
  275. if (err)
  276. return err;
  277. return 0;
  278. }
  279. static void mhi_net_dellink(struct mhi_device *mhi_dev, struct net_device *ndev)
  280. {
  281. struct mhi_net_dev *mhi_netdev = netdev_priv(ndev);
  282. unregister_netdev(ndev);
  283. mhi_unprepare_from_transfer(mhi_dev);
  284. kfree_skb(mhi_netdev->skbagg_head);
  285. free_netdev(ndev);
  286. dev_set_drvdata(&mhi_dev->dev, NULL);
  287. }
  288. static int mhi_net_probe(struct mhi_device *mhi_dev,
  289. const struct mhi_device_id *id)
  290. {
  291. const struct mhi_device_info *info = (struct mhi_device_info *)id->driver_data;
  292. struct net_device *ndev;
  293. int err;
  294. ndev = alloc_netdev(sizeof(struct mhi_net_dev), info->netname,
  295. NET_NAME_PREDICTABLE, mhi_net_setup);
  296. if (!ndev)
  297. return -ENOMEM;
  298. SET_NETDEV_DEV(ndev, &mhi_dev->dev);
  299. err = mhi_net_newlink(mhi_dev, ndev);
  300. if (err) {
  301. free_netdev(ndev);
  302. return err;
  303. }
  304. return 0;
  305. }
  306. static void mhi_net_remove(struct mhi_device *mhi_dev)
  307. {
  308. struct mhi_net_dev *mhi_netdev = dev_get_drvdata(&mhi_dev->dev);
  309. mhi_net_dellink(mhi_dev, mhi_netdev->ndev);
  310. }
  311. static const struct mhi_device_info mhi_hwip0 = {
  312. .netname = "mhi_hwip%d",
  313. };
  314. static const struct mhi_device_info mhi_swip0 = {
  315. .netname = "mhi_swip%d",
  316. };
  317. static const struct mhi_device_id mhi_net_id_table[] = {
  318. /* Hardware accelerated data PATH (to modem IPA), protocol agnostic */
  319. { .chan = "IP_HW0", .driver_data = (kernel_ulong_t)&mhi_hwip0 },
  320. /* Software data PATH (to modem CPU) */
  321. { .chan = "IP_SW0", .driver_data = (kernel_ulong_t)&mhi_swip0 },
  322. {}
  323. };
  324. MODULE_DEVICE_TABLE(mhi, mhi_net_id_table);
  325. static struct mhi_driver mhi_net_driver = {
  326. .probe = mhi_net_probe,
  327. .remove = mhi_net_remove,
  328. .dl_xfer_cb = mhi_net_dl_callback,
  329. .ul_xfer_cb = mhi_net_ul_callback,
  330. .id_table = mhi_net_id_table,
  331. .driver = {
  332. .name = "mhi_net",
  333. .owner = THIS_MODULE,
  334. },
  335. };
  336. module_mhi_driver(mhi_net_driver);
  337. MODULE_AUTHOR("Loic Poulain <[email protected]>");
  338. MODULE_DESCRIPTION("Network over MHI");
  339. MODULE_LICENSE("GPL v2");