rmnet_ctl_client.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  4. *
  5. * RMNET_CTL client handlers
  6. *
  7. */
  8. #include <linux/debugfs.h>
  9. #include <linux/ipc_logging.h>
  10. #include <linux/version.h>
  11. #include "rmnet_ctl.h"
  12. #include "rmnet_ctl_client.h"
  13. #define RMNET_CTL_LOG_PAGE 10
  14. #define RMNET_CTL_LOG_NAME "rmnet_ctl"
  15. #define RMNET_CTL_LOG_LVL "ipc_log_lvl"
  16. struct rmnet_ctl_client {
  17. struct rmnet_ctl_client_hooks hooks;
  18. };
  19. struct rmnet_ctl_endpoint {
  20. struct rmnet_ctl_dev __rcu *dev;
  21. struct rmnet_ctl_client __rcu *client;
  22. struct dentry *dbgfs_dir;
  23. struct dentry *dbgfs_loglvl;
  24. void *ipc_log;
  25. };
  26. #if defined(CONFIG_IPA_DEBUG) || defined(CONFIG_MHI_DEBUG)
  27. #define CONFIG_RMNET_CTL_DEBUG 1
  28. #endif
  29. #ifdef CONFIG_RMNET_CTL_DEBUG
  30. static u8 ipc_log_lvl = RMNET_CTL_LOG_DEBUG;
  31. #else
  32. static u8 ipc_log_lvl = RMNET_CTL_LOG_ERR;
  33. #endif
  34. static DEFINE_SPINLOCK(client_lock);
  35. static struct rmnet_ctl_endpoint ctl_ep;
  36. void rmnet_ctl_set_dbgfs(bool enable)
  37. {
  38. if (enable) {
  39. if (IS_ERR_OR_NULL(ctl_ep.dbgfs_dir))
  40. ctl_ep.dbgfs_dir = debugfs_create_dir(
  41. RMNET_CTL_LOG_NAME, NULL);
  42. if (!IS_ERR_OR_NULL(ctl_ep.dbgfs_dir))
  43. debugfs_create_u8((const char *) RMNET_CTL_LOG_LVL,
  44. (umode_t) 0644,
  45. (struct dentry *) ctl_ep.dbgfs_dir,
  46. (u8 *) &ipc_log_lvl);
  47. if (!ctl_ep.ipc_log)
  48. ctl_ep.ipc_log = ipc_log_context_create(
  49. RMNET_CTL_LOG_PAGE, RMNET_CTL_LOG_NAME, 0);
  50. } else {
  51. debugfs_remove_recursive(ctl_ep.dbgfs_dir);
  52. ipc_log_context_destroy(ctl_ep.ipc_log);
  53. ctl_ep.dbgfs_dir = NULL;
  54. ctl_ep.dbgfs_loglvl = NULL;
  55. ctl_ep.ipc_log = NULL;
  56. }
  57. }
  58. void rmnet_ctl_endpoint_setdev(const struct rmnet_ctl_dev *dev)
  59. {
  60. rcu_assign_pointer(ctl_ep.dev, dev);
  61. }
  62. void rmnet_ctl_endpoint_post(const void *data, size_t len)
  63. {
  64. struct rmnet_ctl_client *client;
  65. struct sk_buff *skb;
  66. if (unlikely(!data || !len))
  67. return;
  68. if (len == 0xFFFFFFFF) {
  69. skb = (struct sk_buff *)data;
  70. rmnet_ctl_log_info("RX", skb->data, skb->len);
  71. rcu_read_lock();
  72. client = rcu_dereference(ctl_ep.client);
  73. if (client && client->hooks.ctl_dl_client_hook) {
  74. skb->protocol = htons(ETH_P_MAP);
  75. client->hooks.ctl_dl_client_hook(skb);
  76. } else {
  77. kfree(skb);
  78. }
  79. rcu_read_unlock();
  80. } else {
  81. rmnet_ctl_log_info("RX", data, len);
  82. rcu_read_lock();
  83. client = rcu_dereference(ctl_ep.client);
  84. if (client && client->hooks.ctl_dl_client_hook) {
  85. skb = alloc_skb(len, GFP_ATOMIC);
  86. if (skb) {
  87. skb_put_data(skb, data, len);
  88. skb->protocol = htons(ETH_P_MAP);
  89. client->hooks.ctl_dl_client_hook(skb);
  90. }
  91. }
  92. rcu_read_unlock();
  93. }
  94. }
  95. void *rmnet_ctl_register_client(struct rmnet_ctl_client_hooks *hook)
  96. {
  97. struct rmnet_ctl_client *client;
  98. if (!hook)
  99. return NULL;
  100. client = kzalloc(sizeof(*client), GFP_KERNEL);
  101. if (!client)
  102. return NULL;
  103. client->hooks = *hook;
  104. spin_lock(&client_lock);
  105. /* Only support one client for now */
  106. if (rcu_dereference(ctl_ep.client)) {
  107. spin_unlock(&client_lock);
  108. kfree(client);
  109. return NULL;
  110. }
  111. rcu_assign_pointer(ctl_ep.client, client);
  112. spin_unlock(&client_lock);
  113. rmnet_ctl_set_dbgfs(true);
  114. return client;
  115. }
  116. EXPORT_SYMBOL(rmnet_ctl_register_client);
  117. int rmnet_ctl_unregister_client(void *handle)
  118. {
  119. struct rmnet_ctl_client *client = (struct rmnet_ctl_client *)handle;
  120. spin_lock(&client_lock);
  121. if (rcu_dereference(ctl_ep.client) != client) {
  122. spin_unlock(&client_lock);
  123. return -EINVAL;
  124. }
  125. RCU_INIT_POINTER(ctl_ep.client, NULL);
  126. spin_unlock(&client_lock);
  127. synchronize_rcu();
  128. kfree(client);
  129. rmnet_ctl_set_dbgfs(false);
  130. return 0;
  131. }
  132. EXPORT_SYMBOL(rmnet_ctl_unregister_client);
  133. int rmnet_ctl_send_client(void *handle, struct sk_buff *skb)
  134. {
  135. struct rmnet_ctl_client *client = (struct rmnet_ctl_client *)handle;
  136. struct rmnet_ctl_dev *dev;
  137. int rc = -EINVAL;
  138. if (client != rcu_dereference(ctl_ep.client)) {
  139. kfree_skb(skb);
  140. return rc;
  141. }
  142. rmnet_ctl_log_info("TX", skb->data, skb->len);
  143. rcu_read_lock();
  144. dev = rcu_dereference(ctl_ep.dev);
  145. if (dev && dev->xmit)
  146. rc = dev->xmit(dev, skb);
  147. else
  148. kfree_skb(skb);
  149. rcu_read_unlock();
  150. if (rc)
  151. rmnet_ctl_log_err("TXE", rc, NULL, 0);
  152. return rc;
  153. }
  154. EXPORT_SYMBOL(rmnet_ctl_send_client);
  155. void rmnet_ctl_log(enum rmnet_ctl_log_lvl lvl, const char *msg,
  156. int rc, const void *data, unsigned int len)
  157. {
  158. if (lvl <= ipc_log_lvl && ctl_ep.ipc_log) {
  159. if (data == NULL || len == 0)
  160. ipc_log_string(ctl_ep.ipc_log, "%3s(%d): (null)\n",
  161. msg, rc);
  162. else
  163. ipc_log_string(ctl_ep.ipc_log, "%3s(%d): %*ph\n",
  164. msg, rc, len > 32 ? 32 : len, data);
  165. }
  166. }
  167. EXPORT_SYMBOL(rmnet_ctl_log);
  168. static struct rmnet_ctl_client_if client_if = {
  169. .reg = rmnet_ctl_register_client,
  170. .dereg = rmnet_ctl_unregister_client,
  171. .send = rmnet_ctl_send_client,
  172. .log = rmnet_ctl_log,
  173. };
  174. struct rmnet_ctl_client_if *rmnet_ctl_if(void)
  175. {
  176. return &client_if;
  177. }
  178. EXPORT_SYMBOL(rmnet_ctl_if);
  179. int rmnet_ctl_get_stats(u64 *s, int n)
  180. {
  181. struct rmnet_ctl_dev *dev;
  182. rcu_read_lock();
  183. dev = rcu_dereference(ctl_ep.dev);
  184. if (dev && n > 0) {
  185. n = min(n, (int)(sizeof(dev->stats) / sizeof(u64)));
  186. memcpy(s, &dev->stats, n * sizeof(u64));
  187. }
  188. rcu_read_unlock();
  189. return n;
  190. }
  191. EXPORT_SYMBOL(rmnet_ctl_get_stats);