rmnet_ll_ipa.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Copyright (c) 2021 The Linux Foundation. All rights reserved.
  2. * Copyright (c) 2021-2023 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. if (tmp == skb)
  78. tmp = skb_shinfo(tmp)->frag_list;
  79. else
  80. tmp = tmp->next;
  81. }
  82. stats->rx_pkts++;
  83. netif_rx(skb);
  84. }
  85. static void rmnet_ll_ipa_probe(void *arg)
  86. {
  87. struct rmnet_ll_endpoint *ll_ep;
  88. ll_ep = kzalloc(sizeof(*ll_ep), GFP_KERNEL);
  89. if (!ll_ep) {
  90. pr_err("%s(): allocating LL CTX failed\n", __func__);
  91. return;
  92. }
  93. ll_ep->phys_dev = dev_get_by_name(&init_net, "rmnet_ipa0");
  94. if (!ll_ep->phys_dev) {
  95. pr_err("%s(): Invalid physical device\n", __func__);
  96. kfree(ll_ep);
  97. return;
  98. }
  99. *((struct rmnet_ll_endpoint **)arg) = ll_ep;
  100. }
  101. static void rmnet_ll_ipa_remove(void *arg)
  102. {
  103. struct rmnet_ll_endpoint **ll_ep = arg;
  104. struct sk_buff *skb;
  105. dev_put((*ll_ep)->phys_dev);
  106. kfree(*ll_ep);
  107. *ll_ep = NULL;
  108. spin_lock_bh(&rmnet_ll_tx_lock);
  109. while ((skb = __skb_dequeue(&tx_pending_list)))
  110. kfree_skb(skb);
  111. spin_unlock_bh(&rmnet_ll_tx_lock);
  112. }
  113. static void rmnet_ll_ipa_ready(void * __unused)
  114. {
  115. int rc;
  116. rc = ipa_register_rmnet_ll_cb(rmnet_ll_ipa_probe,
  117. (void *)&rmnet_ll_ipa_ep,
  118. rmnet_ll_ipa_remove,
  119. (void *)&rmnet_ll_ipa_ep,
  120. rmnet_ll_ipa_rx,
  121. (void *)&rmnet_ll_ipa_ep);
  122. if (rc)
  123. pr_err("%s(): Registering IPA LL callback failed with rc %d\n",
  124. __func__, rc);
  125. }
  126. static int rmnet_ll_ipa_tx(struct sk_buff *skb)
  127. {
  128. struct rmnet_ll_stats *stats = rmnet_ll_get_stats();
  129. int rc;
  130. if (!rmnet_ll_ipa_ep)
  131. return -ENODEV;
  132. if (!skb_queue_empty(&tx_pending_list))
  133. goto queue_skb;
  134. rc = ipa_rmnet_ll_xmit(skb);
  135. /* rc >=0: success, return number of free descriptors left */
  136. if (rc >= 0)
  137. return 0;
  138. /* IPA handles freeing the SKB on failure */
  139. if (rc != -EAGAIN)
  140. return rc;
  141. stats->tx_disabled++;
  142. queue_skb:
  143. /* Flow controlled */
  144. if (skb_queue_len(&tx_pending_list) >= MAX_Q_LEN) {
  145. kfree_skb(skb);
  146. return -ENOMEM;
  147. }
  148. __skb_queue_tail(&tx_pending_list, skb);
  149. stats->tx_fc_queued++;
  150. return 0;
  151. }
  152. static int rmnet_ll_ipa_init(void)
  153. {
  154. int rc;
  155. __skb_queue_head_init(&tx_pending_list);
  156. rc = ipa_register_ipa_ready_cb(rmnet_ll_ipa_ready, NULL);
  157. if (rc == -EEXIST) {
  158. /* IPA is already up. Call it ourselves, since they don't */
  159. rmnet_ll_ipa_ready(NULL);
  160. rc = 0;
  161. }
  162. return rc;
  163. }
  164. static int rmnet_ll_ipa_exit(void)
  165. {
  166. if (rmnet_ll_ipa_ep) {
  167. ipa_unregister_rmnet_ll_cb();
  168. /* Teardown? */
  169. rmnet_ll_ipa_ep = NULL;
  170. }
  171. return 0;
  172. }
  173. #else
  174. static int rmnet_ll_ipa_tx(struct sk_buff *skb){return 0;};
  175. static int rmnet_ll_ipa_init(void){return 0;}
  176. static int rmnet_ll_ipa_exit(void){return 0;};
  177. #endif /* !defined(__arch_um__) */
  178. /* Export operations struct to the main framework */
  179. struct rmnet_ll_client_ops rmnet_ll_client = {
  180. .tx = rmnet_ll_ipa_tx,
  181. .init = rmnet_ll_ipa_init,
  182. .exit = rmnet_ll_ipa_exit,
  183. };