rmnet_ll_mhi.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* Copyright (c) 2020-2021 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * RmNet MHI Low Latency channel handlers
  13. */
  14. #include <linux/device.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/of.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/mhi.h>
  19. #include <linux/if_ether.h>
  20. #include <linux/mm.h>
  21. #include "rmnet_ll.h"
  22. #include "rmnet_ll_core.h"
  23. static struct rmnet_ll_endpoint *rmnet_ll_mhi_ep;
  24. static void rmnet_ll_mhi_rx(struct mhi_device *mhi_dev, struct mhi_result *res)
  25. {
  26. struct rmnet_ll_endpoint *ll_ep = dev_get_drvdata(&mhi_dev->dev);
  27. struct rmnet_ll_stats *stats = rmnet_ll_get_stats();
  28. struct rmnet_ll_buffer *ll_buf;
  29. struct sk_buff *skb;
  30. /* Get the buffer struct back for our page information */
  31. ll_buf = res->buf_addr + ll_ep->buf_len;
  32. ll_buf->submitted = false;
  33. if (res->transaction_status) {
  34. stats->rx_status_err++;
  35. goto err;
  36. } else if (!res->bytes_xferd) {
  37. stats->rx_null++;
  38. goto err;
  39. }
  40. /* Store this away so we don't have to look it up every time */
  41. if (!ll_ep->phys_dev) {
  42. ll_ep->phys_dev = dev_get_by_name(&init_net, "rmnet_mhi0");
  43. if (!ll_ep->phys_dev)
  44. goto err;
  45. }
  46. skb = alloc_skb(0, GFP_ATOMIC);
  47. if (!skb) {
  48. stats->rx_oom++;
  49. goto err;
  50. }
  51. /* Build the SKB and pass it off to the stack */
  52. skb_add_rx_frag(skb, 0, ll_buf->page, 0, res->bytes_xferd,
  53. ll_ep->buf_len);
  54. if (!ll_buf->temp_alloc)
  55. get_page(ll_buf->page);
  56. skb->dev = ll_ep->phys_dev;
  57. skb->protocol = htons(ETH_P_MAP);
  58. /* Mark this as arriving on the LL channel. Allows rmnet to skip
  59. * module handling as needed.
  60. */
  61. skb->priority = 0xda1a;
  62. stats->rx_pkts++;
  63. netif_rx(skb);
  64. rmnet_ll_buffers_recycle(ll_ep);
  65. return;
  66. err:
  67. /* Go, and never darken my towels again! */
  68. if (ll_buf->temp_alloc)
  69. put_page(ll_buf->page);
  70. }
  71. static void rmnet_ll_mhi_tx_complete(struct mhi_device *mhi_dev,
  72. struct mhi_result *res)
  73. {
  74. struct rmnet_ll_stats *stats = rmnet_ll_get_stats();
  75. struct sk_buff *skb = res->buf_addr;
  76. /* Check the result and free the SKB */
  77. if (res->transaction_status)
  78. stats->tx_complete_err++;
  79. else
  80. stats->tx_complete++;
  81. dev_kfree_skb_any(skb);
  82. }
  83. static int rmnet_ll_mhi_probe(struct mhi_device *mhi_dev,
  84. const struct mhi_device_id *id)
  85. {
  86. struct rmnet_ll_endpoint *ll_ep;
  87. int rc;
  88. /* Allocate space for our state from the managed pool tied to the life
  89. * of the mhi device.
  90. */
  91. ll_ep = devm_kzalloc(&mhi_dev->dev, sizeof(*ll_ep), GFP_KERNEL);
  92. if (!ll_ep)
  93. return -ENOMEM;
  94. /* Hold on to the mhi_dev so we can send data to it later */
  95. ll_ep->priv = (void *)mhi_dev;
  96. /* Grab the MRU of the device so we know the size of the pages we need
  97. * to allocate for the pool.
  98. */
  99. rc = of_property_read_u32(mhi_dev->dev.of_node, "mhi,mru",
  100. &ll_ep->dev_mru);
  101. if (rc || !ll_ep->dev_mru)
  102. /* Use our default mru */
  103. ll_ep->dev_mru = RMNET_LL_DEFAULT_MRU;
  104. ll_ep->page_order = get_order(ll_ep->dev_mru);
  105. /* We store some stuff at the end of the page, so don't let the HW
  106. * use that part of it.
  107. */
  108. ll_ep->buf_len = ll_ep->dev_mru - sizeof(struct rmnet_ll_buffer);
  109. /* Tell MHI to initialize the UL/DL channels for transfer */
  110. rc = mhi_prepare_for_transfer(mhi_dev);
  111. if (rc) {
  112. pr_err("%s(): Failed to prepare device for transfer: 0x%x\n",
  113. __func__, rc);
  114. return rc;
  115. }
  116. rc = rmnet_ll_buffer_pool_alloc(ll_ep);
  117. if (rc) {
  118. pr_err("%s(): Failed to allocate buffer pool: %d\n", __func__,
  119. rc);
  120. mhi_unprepare_from_transfer(mhi_dev);
  121. return rc;
  122. }
  123. rmnet_ll_buffers_recycle(ll_ep);
  124. /* Not a fan of storing this pointer in two locations, but I've yet to
  125. * come up with any other good way of accessing it on the TX path from
  126. * rmnet otherwise, since we won't have any references to the mhi_dev.
  127. */
  128. dev_set_drvdata(&mhi_dev->dev, ll_ep);
  129. rmnet_ll_mhi_ep = ll_ep;
  130. return 0;
  131. }
  132. static void rmnet_ll_mhi_remove(struct mhi_device *mhi_dev)
  133. {
  134. struct rmnet_ll_endpoint *ll_ep;
  135. ll_ep = dev_get_drvdata(&mhi_dev->dev);
  136. /* Remove our private data form the device. No need to free it though.
  137. * It will be freed once the mhi_dev is released since it was alloced
  138. * from a managed pool.
  139. */
  140. dev_set_drvdata(&mhi_dev->dev, NULL);
  141. rmnet_ll_mhi_ep = NULL;
  142. rmnet_ll_buffer_pool_free(ll_ep);
  143. }
  144. static const struct mhi_device_id rmnet_ll_mhi_channel_table[] = {
  145. {
  146. .chan = "RMNET_DATA_LL",
  147. },
  148. {},
  149. };
  150. static struct mhi_driver rmnet_ll_driver = {
  151. .probe = rmnet_ll_mhi_probe,
  152. .remove = rmnet_ll_mhi_remove,
  153. .dl_xfer_cb = rmnet_ll_mhi_rx,
  154. .ul_xfer_cb = rmnet_ll_mhi_tx_complete,
  155. .id_table = rmnet_ll_mhi_channel_table,
  156. .driver = {
  157. .name = "rmnet_ll",
  158. .owner = THIS_MODULE,
  159. },
  160. };
  161. static int rmnet_ll_mhi_queue(struct rmnet_ll_endpoint *ll_ep,
  162. struct rmnet_ll_buffer *ll_buf)
  163. {
  164. struct mhi_device *mhi_dev = ll_ep->priv;
  165. return mhi_queue_buf(mhi_dev, DMA_FROM_DEVICE,
  166. page_address(ll_buf->page),
  167. ll_ep->buf_len, MHI_EOT);
  168. }
  169. static int rmnet_ll_mhi_query_free_descriptors(struct rmnet_ll_endpoint *ll_ep)
  170. {
  171. struct mhi_device *mhi_dev = ll_ep->priv;
  172. return mhi_get_free_desc_count(mhi_dev, DMA_FROM_DEVICE);
  173. }
  174. static int rmnet_ll_mhi_tx(struct sk_buff *skb)
  175. {
  176. struct mhi_device *mhi_dev;
  177. int rc;
  178. if (!rmnet_ll_mhi_ep)
  179. return -ENODEV;
  180. mhi_dev = rmnet_ll_mhi_ep->priv;
  181. rc = mhi_queue_skb(mhi_dev, DMA_TO_DEVICE, skb, skb->len, MHI_EOT);
  182. if (rc)
  183. kfree_skb(skb);
  184. return rc;
  185. }
  186. static int rmnet_ll_mhi_init(void)
  187. {
  188. return mhi_driver_register(&rmnet_ll_driver);
  189. }
  190. static int rmnet_ll_mhi_exit(void)
  191. {
  192. mhi_driver_unregister(&rmnet_ll_driver);
  193. return 0;
  194. }
  195. /* Export operations struct to the main framework */
  196. struct rmnet_ll_client_ops rmnet_ll_client = {
  197. .buffer_queue = rmnet_ll_mhi_queue,
  198. .query_free_descriptors = rmnet_ll_mhi_query_free_descriptors,
  199. .tx = rmnet_ll_mhi_tx,
  200. .init = rmnet_ll_mhi_init,
  201. .exit = rmnet_ll_mhi_exit,
  202. };