rmnet_ctl_client.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
  3. *
  4. * RMNET_CTL client handlers
  5. *
  6. */
  7. #include <linux/debugfs.h>
  8. #include <linux/ipc_logging.h>
  9. #include "rmnet_ctl.h"
  10. #include "rmnet_ctl_client.h"
  11. #define RMNET_CTL_LOG_PAGE 10
  12. #define RMNET_CTL_LOG_NAME "rmnet_ctl"
  13. #define RMNET_CTL_LOG_LVL "ipc_log_lvl"
  14. struct rmnet_ctl_client {
  15. struct rmnet_ctl_client_hooks hooks;
  16. };
  17. struct rmnet_ctl_endpoint {
  18. struct rmnet_ctl_dev __rcu *dev;
  19. struct rmnet_ctl_client __rcu *client;
  20. struct dentry *dbgfs_dir;
  21. struct dentry *dbgfs_loglvl;
  22. void *ipc_log;
  23. };
  24. #ifdef CONFIG_RMNET_CTL_DEBUG
  25. static u8 ipc_log_lvl = RMNET_CTL_LOG_DEBUG;
  26. #else
  27. static u8 ipc_log_lvl = RMNET_CTL_LOG_ERR;
  28. #endif
  29. static DEFINE_SPINLOCK(client_lock);
  30. static struct rmnet_ctl_endpoint ctl_ep;
  31. void rmnet_ctl_endpoint_setdev(const struct rmnet_ctl_dev *dev)
  32. {
  33. rcu_assign_pointer(ctl_ep.dev, dev);
  34. if (dev) {
  35. ctl_ep.dbgfs_dir = debugfs_create_dir(
  36. RMNET_CTL_LOG_NAME, NULL);
  37. if (!IS_ERR_OR_NULL(ctl_ep.dbgfs_dir))
  38. ctl_ep.dbgfs_loglvl = debugfs_create_u8(
  39. RMNET_CTL_LOG_LVL, 0644, ctl_ep.dbgfs_dir,
  40. &ipc_log_lvl);
  41. if (!ctl_ep.ipc_log)
  42. ctl_ep.ipc_log = ipc_log_context_create(
  43. RMNET_CTL_LOG_PAGE, RMNET_CTL_LOG_NAME, 0);
  44. } else {
  45. debugfs_remove_recursive(ctl_ep.dbgfs_dir);
  46. }
  47. }
  48. void rmnet_ctl_endpoint_post(const void *data, size_t len)
  49. {
  50. struct rmnet_ctl_client *client;
  51. struct sk_buff *skb;
  52. if (unlikely(!data || !len))
  53. return;
  54. rmnet_ctl_log_info("RX", data, len);
  55. rcu_read_lock();
  56. client = rcu_dereference(ctl_ep.client);
  57. if (client && client->hooks.ctl_dl_client_hook) {
  58. skb = alloc_skb(len, GFP_ATOMIC);
  59. if (skb) {
  60. skb_put_data(skb, data, len);
  61. skb->protocol = htons(ETH_P_MAP);
  62. client->hooks.ctl_dl_client_hook(skb);
  63. }
  64. }
  65. rcu_read_unlock();
  66. }
  67. void *rmnet_ctl_register_client(struct rmnet_ctl_client_hooks *hook)
  68. {
  69. struct rmnet_ctl_client *client;
  70. if (!hook)
  71. return NULL;
  72. client = kzalloc(sizeof(*client), GFP_KERNEL);
  73. if (!client)
  74. return NULL;
  75. client->hooks = *hook;
  76. spin_lock(&client_lock);
  77. /* Only support one client for now */
  78. if (rcu_dereference(ctl_ep.client)) {
  79. spin_unlock(&client_lock);
  80. kfree(client);
  81. return NULL;
  82. }
  83. rcu_assign_pointer(ctl_ep.client, client);
  84. spin_unlock(&client_lock);
  85. return client;
  86. }
  87. EXPORT_SYMBOL(rmnet_ctl_register_client);
  88. int rmnet_ctl_unregister_client(void *handle)
  89. {
  90. struct rmnet_ctl_client *client = (struct rmnet_ctl_client *)handle;
  91. spin_lock(&client_lock);
  92. if (rcu_dereference(ctl_ep.client) != client) {
  93. spin_unlock(&client_lock);
  94. return -EINVAL;
  95. }
  96. RCU_INIT_POINTER(ctl_ep.client, NULL);
  97. spin_unlock(&client_lock);
  98. synchronize_rcu();
  99. kfree(client);
  100. return 0;
  101. }
  102. EXPORT_SYMBOL(rmnet_ctl_unregister_client);
  103. int rmnet_ctl_send_client(void *handle, struct sk_buff *skb)
  104. {
  105. struct rmnet_ctl_client *client = (struct rmnet_ctl_client *)handle;
  106. struct rmnet_ctl_dev *dev;
  107. int rc = -EINVAL;
  108. if (client != rcu_dereference(ctl_ep.client))
  109. return rc;
  110. rmnet_ctl_log_info("TX", skb->data, skb->len);
  111. rcu_read_lock();
  112. dev = rcu_dereference(ctl_ep.dev);
  113. if (dev && dev->xmit)
  114. rc = dev->xmit(dev, skb);
  115. rcu_read_unlock();
  116. if (rc)
  117. rmnet_ctl_log_err("TXE", rc, skb->data, skb->len);
  118. return rc;
  119. }
  120. EXPORT_SYMBOL(rmnet_ctl_send_client);
  121. void rmnet_ctl_log(enum rmnet_ctl_log_lvl lvl, const char *msg,
  122. int rc, const void *data, unsigned int len)
  123. {
  124. if (lvl <= ipc_log_lvl && ctl_ep.ipc_log) {
  125. if (data == NULL || len == 0)
  126. ipc_log_string(ctl_ep.ipc_log, "%3s(%d): (null)\n",
  127. msg, rc);
  128. else
  129. ipc_log_string(ctl_ep.ipc_log, "%3s(%d): %*ph\n",
  130. msg, rc, len > 32 ? 32 : len, data);
  131. }
  132. }
  133. EXPORT_SYMBOL(rmnet_ctl_log);