debugfs.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 "client.h"
  23. #include "hw.h"
  24. static ssize_t mei_dbgfs_read_meclients(struct file *fp, char __user *ubuf,
  25. size_t cnt, loff_t *ppos)
  26. {
  27. struct mei_device *dev = fp->private_data;
  28. struct mei_me_client *me_cl;
  29. size_t bufsz = 1;
  30. char *buf;
  31. int i = 0;
  32. int pos = 0;
  33. int ret;
  34. #define HDR \
  35. " |id|fix| UUID |con|msg len|sb|refc|\n"
  36. down_read(&dev->me_clients_rwsem);
  37. list_for_each_entry(me_cl, &dev->me_clients, list)
  38. bufsz++;
  39. bufsz *= sizeof(HDR) + 1;
  40. buf = kzalloc(bufsz, GFP_KERNEL);
  41. if (!buf) {
  42. up_read(&dev->me_clients_rwsem);
  43. return -ENOMEM;
  44. }
  45. pos += scnprintf(buf + pos, bufsz - pos, HDR);
  46. #undef HDR
  47. /* if the driver is not enabled the list won't be consistent */
  48. if (dev->dev_state != MEI_DEV_ENABLED)
  49. goto out;
  50. list_for_each_entry(me_cl, &dev->me_clients, list) {
  51. if (mei_me_cl_get(me_cl)) {
  52. pos += scnprintf(buf + pos, bufsz - pos,
  53. "%2d|%2d|%3d|%pUl|%3d|%7d|%2d|%4d|\n",
  54. i++, me_cl->client_id,
  55. me_cl->props.fixed_address,
  56. &me_cl->props.protocol_name,
  57. me_cl->props.max_number_of_connections,
  58. me_cl->props.max_msg_length,
  59. me_cl->props.single_recv_buf,
  60. kref_read(&me_cl->refcnt));
  61. mei_me_cl_put(me_cl);
  62. }
  63. }
  64. out:
  65. up_read(&dev->me_clients_rwsem);
  66. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
  67. kfree(buf);
  68. return ret;
  69. }
  70. static const struct file_operations mei_dbgfs_fops_meclients = {
  71. .open = simple_open,
  72. .read = mei_dbgfs_read_meclients,
  73. .llseek = generic_file_llseek,
  74. };
  75. static ssize_t mei_dbgfs_read_active(struct file *fp, char __user *ubuf,
  76. size_t cnt, loff_t *ppos)
  77. {
  78. struct mei_device *dev = fp->private_data;
  79. struct mei_cl *cl;
  80. size_t bufsz = 1;
  81. char *buf;
  82. int i = 0;
  83. int pos = 0;
  84. int ret;
  85. #define HDR " |me|host|state|rd|wr|\n"
  86. if (!dev)
  87. return -ENODEV;
  88. mutex_lock(&dev->device_lock);
  89. /*
  90. * if the driver is not enabled the list won't be consistent,
  91. * we output empty table
  92. */
  93. if (dev->dev_state == MEI_DEV_ENABLED)
  94. list_for_each_entry(cl, &dev->file_list, link)
  95. bufsz++;
  96. bufsz *= sizeof(HDR) + 1;
  97. buf = kzalloc(bufsz, GFP_KERNEL);
  98. if (!buf) {
  99. mutex_unlock(&dev->device_lock);
  100. return -ENOMEM;
  101. }
  102. pos += scnprintf(buf + pos, bufsz - pos, HDR);
  103. #undef HDR
  104. /* if the driver is not enabled the list won't be consistent */
  105. if (dev->dev_state != MEI_DEV_ENABLED)
  106. goto out;
  107. list_for_each_entry(cl, &dev->file_list, link) {
  108. pos += scnprintf(buf + pos, bufsz - pos,
  109. "%3d|%2d|%4d|%5d|%2d|%2d|\n",
  110. i, mei_cl_me_id(cl), cl->host_client_id, cl->state,
  111. !list_empty(&cl->rd_completed), cl->writing_state);
  112. i++;
  113. }
  114. out:
  115. mutex_unlock(&dev->device_lock);
  116. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
  117. kfree(buf);
  118. return ret;
  119. }
  120. static const struct file_operations mei_dbgfs_fops_active = {
  121. .open = simple_open,
  122. .read = mei_dbgfs_read_active,
  123. .llseek = generic_file_llseek,
  124. };
  125. static ssize_t mei_dbgfs_read_devstate(struct file *fp, char __user *ubuf,
  126. size_t cnt, loff_t *ppos)
  127. {
  128. struct mei_device *dev = fp->private_data;
  129. const size_t bufsz = 1024;
  130. char *buf = kzalloc(bufsz, GFP_KERNEL);
  131. int pos = 0;
  132. int ret;
  133. if (!buf)
  134. return -ENOMEM;
  135. pos += scnprintf(buf + pos, bufsz - pos, "dev: %s\n",
  136. mei_dev_state_str(dev->dev_state));
  137. pos += scnprintf(buf + pos, bufsz - pos, "hbm: %s\n",
  138. mei_hbm_state_str(dev->hbm_state));
  139. if (dev->hbm_state >= MEI_HBM_ENUM_CLIENTS &&
  140. dev->hbm_state <= MEI_HBM_STARTED) {
  141. pos += scnprintf(buf + pos, bufsz - pos, "hbm features:\n");
  142. pos += scnprintf(buf + pos, bufsz - pos, "\tPG: %01d\n",
  143. dev->hbm_f_pg_supported);
  144. pos += scnprintf(buf + pos, bufsz - pos, "\tDC: %01d\n",
  145. dev->hbm_f_dc_supported);
  146. pos += scnprintf(buf + pos, bufsz - pos, "\tIE: %01d\n",
  147. dev->hbm_f_ie_supported);
  148. pos += scnprintf(buf + pos, bufsz - pos, "\tDOT: %01d\n",
  149. dev->hbm_f_dot_supported);
  150. pos += scnprintf(buf + pos, bufsz - pos, "\tEV: %01d\n",
  151. dev->hbm_f_ev_supported);
  152. pos += scnprintf(buf + pos, bufsz - pos, "\tFA: %01d\n",
  153. dev->hbm_f_fa_supported);
  154. pos += scnprintf(buf + pos, bufsz - pos, "\tOS: %01d\n",
  155. dev->hbm_f_os_supported);
  156. }
  157. pos += scnprintf(buf + pos, bufsz - pos, "pg: %s, %s\n",
  158. mei_pg_is_enabled(dev) ? "ENABLED" : "DISABLED",
  159. mei_pg_state_str(mei_pg_state(dev)));
  160. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
  161. kfree(buf);
  162. return ret;
  163. }
  164. static const struct file_operations mei_dbgfs_fops_devstate = {
  165. .open = simple_open,
  166. .read = mei_dbgfs_read_devstate,
  167. .llseek = generic_file_llseek,
  168. };
  169. static ssize_t mei_dbgfs_write_allow_fa(struct file *file,
  170. const char __user *user_buf,
  171. size_t count, loff_t *ppos)
  172. {
  173. struct mei_device *dev;
  174. int ret;
  175. dev = container_of(file->private_data,
  176. struct mei_device, allow_fixed_address);
  177. ret = debugfs_write_file_bool(file, user_buf, count, ppos);
  178. if (ret < 0)
  179. return ret;
  180. dev->override_fixed_address = true;
  181. return ret;
  182. }
  183. static const struct file_operations mei_dbgfs_fops_allow_fa = {
  184. .open = simple_open,
  185. .read = debugfs_read_file_bool,
  186. .write = mei_dbgfs_write_allow_fa,
  187. .llseek = generic_file_llseek,
  188. };
  189. /**
  190. * mei_dbgfs_deregister - Remove the debugfs files and directories
  191. *
  192. * @dev: the mei device structure
  193. */
  194. void mei_dbgfs_deregister(struct mei_device *dev)
  195. {
  196. if (!dev->dbgfs_dir)
  197. return;
  198. debugfs_remove_recursive(dev->dbgfs_dir);
  199. dev->dbgfs_dir = NULL;
  200. }
  201. /**
  202. * mei_dbgfs_register - Add the debugfs files
  203. *
  204. * @dev: the mei device structure
  205. * @name: the mei device name
  206. *
  207. * Return: 0 on success, <0 on failure.
  208. */
  209. int mei_dbgfs_register(struct mei_device *dev, const char *name)
  210. {
  211. struct dentry *dir, *f;
  212. dir = debugfs_create_dir(name, NULL);
  213. if (!dir)
  214. return -ENOMEM;
  215. dev->dbgfs_dir = dir;
  216. f = debugfs_create_file("meclients", S_IRUSR, dir,
  217. dev, &mei_dbgfs_fops_meclients);
  218. if (!f) {
  219. dev_err(dev->dev, "meclients: registration failed\n");
  220. goto err;
  221. }
  222. f = debugfs_create_file("active", S_IRUSR, dir,
  223. dev, &mei_dbgfs_fops_active);
  224. if (!f) {
  225. dev_err(dev->dev, "active: registration failed\n");
  226. goto err;
  227. }
  228. f = debugfs_create_file("devstate", S_IRUSR, dir,
  229. dev, &mei_dbgfs_fops_devstate);
  230. if (!f) {
  231. dev_err(dev->dev, "devstate: registration failed\n");
  232. goto err;
  233. }
  234. f = debugfs_create_file("allow_fixed_address", S_IRUSR | S_IWUSR, dir,
  235. &dev->allow_fixed_address,
  236. &mei_dbgfs_fops_allow_fa);
  237. if (!f) {
  238. dev_err(dev->dev, "allow_fixed_address: registration failed\n");
  239. goto err;
  240. }
  241. return 0;
  242. err:
  243. mei_dbgfs_deregister(dev);
  244. return -ENODEV;
  245. }