rmnet_ll_ipa.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright (c) 2021 The Linux Foundation. All rights reserved.
  2. * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * RmNet IPA Low Latency channel handlers
  14. */
  15. #include <linux/netdevice.h>
  16. #include <linux/skbuff.h>
  17. #if !defined(__arch_um__)
  18. #include <linux/ipa.h>
  19. #endif /* !defined(__arch_um__) */
  20. #include <linux/if_ether.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/version.h>
  23. #include "rmnet_ll.h"
  24. #include "rmnet_ll_core.h"
  25. #define IPA_RMNET_LL_RECEIVE 1
  26. #define IPA_RMNET_LL_FLOW_EVT 2
  27. #define MAX_Q_LEN 1000
  28. #if !defined(__arch_um__)
  29. static struct rmnet_ll_endpoint *rmnet_ll_ipa_ep;
  30. static struct sk_buff_head tx_pending_list;
  31. extern spinlock_t rmnet_ll_tx_lock;
  32. static void rmnet_ll_ipa_tx_pending(struct tasklet_struct *t);
  33. DECLARE_TASKLET(tx_pending_task, rmnet_ll_ipa_tx_pending);
  34. static void rmnet_ll_ipa_tx_pending(struct tasklet_struct *t)
  35. {
  36. struct rmnet_ll_stats *stats = rmnet_ll_get_stats();
  37. struct sk_buff *skb;
  38. int rc;
  39. spin_lock_bh(&rmnet_ll_tx_lock);
  40. while ((skb = __skb_dequeue(&tx_pending_list))) {
  41. rc = ipa_rmnet_ll_xmit(skb);
  42. if (rc == -EAGAIN) {
  43. stats->tx_disabled++;
  44. __skb_queue_head(&tx_pending_list, skb);
  45. break;
  46. }
  47. if (rc >= 0)
  48. stats->tx_fc_sent++;
  49. else
  50. stats->tx_fc_err++;
  51. }
  52. spin_unlock_bh(&rmnet_ll_tx_lock);
  53. }
  54. static void rmnet_ll_ipa_rx(void *arg, void *rx_data)
  55. {
  56. struct rmnet_ll_endpoint *ll_ep = rmnet_ll_ipa_ep;
  57. struct rmnet_ll_stats *stats = rmnet_ll_get_stats();
  58. struct sk_buff *skb, *tmp;
  59. if (arg == (void *)(uintptr_t)(IPA_RMNET_LL_FLOW_EVT)) {
  60. stats->tx_enabled++;
  61. tasklet_schedule(&tx_pending_task);
  62. return;
  63. }
  64. if (unlikely(arg != (void *)(uintptr_t)(IPA_RMNET_LL_RECEIVE))) {
  65. pr_err("%s: invalid arg %u\n", __func__, (uintptr_t)arg);
  66. return;
  67. }
  68. skb = rx_data;
  69. /* Odds are IPA does this, but just to be safe */
  70. skb->dev = ll_ep->phys_dev;
  71. skb->protocol = htons(ETH_P_MAP);
  72. skb_record_rx_queue(skb, 1);
  73. tmp = skb;
  74. while (tmp) {
  75. /* Mark the SKB as low latency */
  76. tmp->priority = 0xda1a;
  77. tmp = skb_shinfo(tmp)->frag_list;
  78. }
  79. stats->rx_pkts++;
  80. netif_rx(skb);
  81. }
  82. static void rmnet_ll_ipa_probe(void *arg)
  83. {
  84. struct rmnet_ll_endpoint *ll_ep;
  85. ll_ep = kzalloc(sizeof(*ll_ep), GFP_KERNEL);
  86. if (!ll_ep) {
  87. pr_err("%s(): allocating LL CTX failed\n", __func__);
  88. return;
  89. }
  90. ll_ep->phys_dev = dev_get_by_name(&init_net, "rmnet_ipa0");
  91. if (!ll_ep->phys_dev) {
  92. pr_err("%s(): Invalid physical device\n", __func__);
  93. kfree(ll_ep);
  94. return;
  95. }
  96. *((struct rmnet_ll_endpoint **)arg) = ll_ep;
  97. }
  98. static void rmnet_ll_ipa_remove(void *arg)
  99. {
  100. struct rmnet_ll_endpoint **ll_ep = arg;
  101. struct sk_buff *skb;
  102. dev_put((*ll_ep)->phys_dev);
  103. kfree(*ll_ep);
  104. *ll_ep = NULL;
  105. spin_lock_bh(&rmnet_ll_tx_lock);
  106. while ((skb = __skb_dequeue(&tx_pending_list)))
  107. kfree_skb(skb);
  108. spin_unlock_bh(&rmnet_ll_tx_lock);
  109. }
  110. static void rmnet_ll_ipa_ready(void * __unused)
  111. {
  112. int rc;
  113. rc = ipa_register_rmnet_ll_cb(rmnet_ll_ipa_probe,
  114. (void *)&rmnet_ll_ipa_ep,
  115. rmnet_ll_ipa_remove,
  116. (void *)&rmnet_ll_ipa_ep,
  117. rmnet_ll_ipa_rx,
  118. (void *)&rmnet_ll_ipa_ep);
  119. if (rc)
  120. pr_err("%s(): Registering IPA LL callback failed with rc %d\n",
  121. __func__, rc);
  122. }
  123. static int rmnet_ll_ipa_tx(struct sk_buff *skb)
  124. {
  125. struct rmnet_ll_stats *stats = rmnet_ll_get_stats();
  126. int rc;
  127. if (!rmnet_ll_ipa_ep)
  128. return -ENODEV;
  129. if (!skb_queue_empty(&tx_pending_list))
  130. goto queue_skb;
  131. rc = ipa_rmnet_ll_xmit(skb);
  132. /* rc >=0: success, return number of free descriptors left */
  133. if (rc >= 0)
  134. return 0;
  135. /* IPA handles freeing the SKB on failure */
  136. if (rc != -EAGAIN)
  137. return rc;
  138. stats->tx_disabled++;
  139. queue_skb:
  140. /* Flow controlled */
  141. if (skb_queue_len(&tx_pending_list) >= MAX_Q_LEN) {
  142. kfree_skb(skb);
  143. return -ENOMEM;
  144. }
  145. __skb_queue_tail(&tx_pending_list, skb);
  146. stats->tx_fc_queued++;
  147. return 0;
  148. }
  149. static int rmnet_ll_ipa_init(void)
  150. {
  151. int rc;
  152. __skb_queue_head_init(&tx_pending_list);
  153. rc = ipa_register_ipa_ready_cb(rmnet_ll_ipa_ready, NULL);
  154. if (rc == -EEXIST) {
  155. /* IPA is already up. Call it ourselves, since they don't */
  156. rmnet_ll_ipa_ready(NULL);
  157. rc = 0;
  158. }
  159. return rc;
  160. }
  161. static int rmnet_ll_ipa_exit(void)
  162. {
  163. if (rmnet_ll_ipa_ep) {
  164. ipa_unregister_rmnet_ll_cb();
  165. /* Teardown? */
  166. rmnet_ll_ipa_ep = NULL;
  167. }
  168. return 0;
  169. }
  170. #else
  171. static int rmnet_ll_ipa_tx(struct sk_buff *skb){return 0;};
  172. static int rmnet_ll_ipa_init(void){return 0;}
  173. static int rmnet_ll_ipa_exit(void){return 0;};
  174. #endif /* !defined(__arch_um__) */
  175. /* Export operations struct to the main framework */
  176. struct rmnet_ll_client_ops rmnet_ll_client = {
  177. .tx = rmnet_ll_ipa_tx,
  178. .init = rmnet_ll_ipa_init,
  179. .exit = rmnet_ll_ipa_exit,
  180. };