debugfs.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2012-2013, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/mei.h>
  21. #include "mei_dev.h"
  22. #include "hw.h"
  23. static ssize_t mei_dbgfs_read_meclients(struct file *fp, char __user *ubuf,
  24. size_t cnt, loff_t *ppos)
  25. {
  26. struct mei_device *dev = fp->private_data;
  27. struct mei_me_client *me_cl;
  28. size_t bufsz = 1;
  29. char *buf;
  30. int i = 0;
  31. int pos = 0;
  32. int ret;
  33. #define HDR " |id|addr| UUID |con|msg len|sb|\n"
  34. mutex_lock(&dev->device_lock);
  35. list_for_each_entry(me_cl, &dev->me_clients, list)
  36. bufsz++;
  37. bufsz *= sizeof(HDR) + 1;
  38. buf = kzalloc(bufsz, GFP_KERNEL);
  39. if (!buf) {
  40. mutex_unlock(&dev->device_lock);
  41. return -ENOMEM;
  42. }
  43. pos += scnprintf(buf + pos, bufsz - pos, HDR);
  44. /* if the driver is not enabled the list won't be consistent */
  45. if (dev->dev_state != MEI_DEV_ENABLED)
  46. goto out;
  47. list_for_each_entry(me_cl, &dev->me_clients, list) {
  48. /* skip me clients that cannot be connected */
  49. if (me_cl->props.max_number_of_connections == 0)
  50. continue;
  51. pos += scnprintf(buf + pos, bufsz - pos,
  52. "%2d|%2d|%4d|%pUl|%3d|%7d|%2d|\n",
  53. i++, me_cl->client_id,
  54. me_cl->props.fixed_address,
  55. &me_cl->props.protocol_name,
  56. me_cl->props.max_number_of_connections,
  57. me_cl->props.max_msg_length,
  58. me_cl->props.single_recv_buf);
  59. }
  60. out:
  61. mutex_unlock(&dev->device_lock);
  62. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
  63. kfree(buf);
  64. return ret;
  65. }
  66. static const struct file_operations mei_dbgfs_fops_meclients = {
  67. .open = simple_open,
  68. .read = mei_dbgfs_read_meclients,
  69. .llseek = generic_file_llseek,
  70. };
  71. static ssize_t mei_dbgfs_read_active(struct file *fp, char __user *ubuf,
  72. size_t cnt, loff_t *ppos)
  73. {
  74. struct mei_device *dev = fp->private_data;
  75. struct mei_cl *cl;
  76. const size_t bufsz = 1024;
  77. char *buf;
  78. int i = 0;
  79. int pos = 0;
  80. int ret;
  81. if (!dev)
  82. return -ENODEV;
  83. buf = kzalloc(bufsz, GFP_KERNEL);
  84. if (!buf)
  85. return -ENOMEM;
  86. pos += scnprintf(buf + pos, bufsz - pos,
  87. " |me|host|state|rd|wr|\n");
  88. mutex_lock(&dev->device_lock);
  89. /* if the driver is not enabled the list won't be consistent */
  90. if (dev->dev_state != MEI_DEV_ENABLED)
  91. goto out;
  92. list_for_each_entry(cl, &dev->file_list, link) {
  93. pos += scnprintf(buf + pos, bufsz - pos,
  94. "%2d|%2d|%4d|%5d|%2d|%2d|\n",
  95. i, cl->me_client_id, cl->host_client_id, cl->state,
  96. cl->reading_state, cl->writing_state);
  97. i++;
  98. }
  99. out:
  100. mutex_unlock(&dev->device_lock);
  101. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
  102. kfree(buf);
  103. return ret;
  104. }
  105. static const struct file_operations mei_dbgfs_fops_active = {
  106. .open = simple_open,
  107. .read = mei_dbgfs_read_active,
  108. .llseek = generic_file_llseek,
  109. };
  110. static ssize_t mei_dbgfs_read_devstate(struct file *fp, char __user *ubuf,
  111. size_t cnt, loff_t *ppos)
  112. {
  113. struct mei_device *dev = fp->private_data;
  114. const size_t bufsz = 1024;
  115. char *buf = kzalloc(bufsz, GFP_KERNEL);
  116. int pos = 0;
  117. int ret;
  118. if (!buf)
  119. return -ENOMEM;
  120. pos += scnprintf(buf + pos, bufsz - pos, "dev: %s\n",
  121. mei_dev_state_str(dev->dev_state));
  122. pos += scnprintf(buf + pos, bufsz - pos, "hbm: %s\n",
  123. mei_hbm_state_str(dev->hbm_state));
  124. pos += scnprintf(buf + pos, bufsz - pos, "pg: %s, %s\n",
  125. mei_pg_is_enabled(dev) ? "ENABLED" : "DISABLED",
  126. mei_pg_state_str(mei_pg_state(dev)));
  127. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
  128. kfree(buf);
  129. return ret;
  130. }
  131. static const struct file_operations mei_dbgfs_fops_devstate = {
  132. .open = simple_open,
  133. .read = mei_dbgfs_read_devstate,
  134. .llseek = generic_file_llseek,
  135. };
  136. /**
  137. * mei_dbgfs_deregister - Remove the debugfs files and directories
  138. *
  139. * @dev: the mei device structure
  140. */
  141. void mei_dbgfs_deregister(struct mei_device *dev)
  142. {
  143. if (!dev->dbgfs_dir)
  144. return;
  145. debugfs_remove_recursive(dev->dbgfs_dir);
  146. dev->dbgfs_dir = NULL;
  147. }
  148. /**
  149. * mei_dbgfs_register - Add the debugfs files
  150. *
  151. * @dev: the mei device structure
  152. * @name: the mei device name
  153. *
  154. * Return: 0 on success, <0 on failure.
  155. */
  156. int mei_dbgfs_register(struct mei_device *dev, const char *name)
  157. {
  158. struct dentry *dir, *f;
  159. dir = debugfs_create_dir(name, NULL);
  160. if (!dir)
  161. return -ENOMEM;
  162. f = debugfs_create_file("meclients", S_IRUSR, dir,
  163. dev, &mei_dbgfs_fops_meclients);
  164. if (!f) {
  165. dev_err(dev->dev, "meclients: registration failed\n");
  166. goto err;
  167. }
  168. f = debugfs_create_file("active", S_IRUSR, dir,
  169. dev, &mei_dbgfs_fops_active);
  170. if (!f) {
  171. dev_err(dev->dev, "meclients: registration failed\n");
  172. goto err;
  173. }
  174. f = debugfs_create_file("devstate", S_IRUSR, dir,
  175. dev, &mei_dbgfs_fops_devstate);
  176. if (!f) {
  177. dev_err(dev->dev, "devstate: registration failed\n");
  178. goto err;
  179. }
  180. dev->dbgfs_dir = dir;
  181. return 0;
  182. err:
  183. mei_dbgfs_deregister(dev);
  184. return -ENODEV;
  185. }