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|wrq\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|%3u\n",
  110. i, mei_cl_me_id(cl), cl->host_client_id, cl->state,
  111. !list_empty(&cl->rd_completed), cl->writing_state,
  112. cl->tx_cb_queued);
  113. i++;
  114. }
  115. out:
  116. mutex_unlock(&dev->device_lock);
  117. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
  118. kfree(buf);
  119. return ret;
  120. }
  121. static const struct file_operations mei_dbgfs_fops_active = {
  122. .open = simple_open,
  123. .read = mei_dbgfs_read_active,
  124. .llseek = generic_file_llseek,
  125. };
  126. static ssize_t mei_dbgfs_read_devstate(struct file *fp, char __user *ubuf,
  127. size_t cnt, loff_t *ppos)
  128. {
  129. struct mei_device *dev = fp->private_data;
  130. const size_t bufsz = 1024;
  131. char *buf = kzalloc(bufsz, GFP_KERNEL);
  132. int pos = 0;
  133. int ret;
  134. if (!buf)
  135. return -ENOMEM;
  136. pos += scnprintf(buf + pos, bufsz - pos, "dev: %s\n",
  137. mei_dev_state_str(dev->dev_state));
  138. pos += scnprintf(buf + pos, bufsz - pos, "hbm: %s\n",
  139. mei_hbm_state_str(dev->hbm_state));
  140. if (dev->hbm_state >= MEI_HBM_ENUM_CLIENTS &&
  141. dev->hbm_state <= MEI_HBM_STARTED) {
  142. pos += scnprintf(buf + pos, bufsz - pos, "hbm features:\n");
  143. pos += scnprintf(buf + pos, bufsz - pos, "\tPG: %01d\n",
  144. dev->hbm_f_pg_supported);
  145. pos += scnprintf(buf + pos, bufsz - pos, "\tDC: %01d\n",
  146. dev->hbm_f_dc_supported);
  147. pos += scnprintf(buf + pos, bufsz - pos, "\tIE: %01d\n",
  148. dev->hbm_f_ie_supported);
  149. pos += scnprintf(buf + pos, bufsz - pos, "\tDOT: %01d\n",
  150. dev->hbm_f_dot_supported);
  151. pos += scnprintf(buf + pos, bufsz - pos, "\tEV: %01d\n",
  152. dev->hbm_f_ev_supported);
  153. pos += scnprintf(buf + pos, bufsz - pos, "\tFA: %01d\n",
  154. dev->hbm_f_fa_supported);
  155. pos += scnprintf(buf + pos, bufsz - pos, "\tOS: %01d\n",
  156. dev->hbm_f_os_supported);
  157. }
  158. pos += scnprintf(buf + pos, bufsz - pos, "pg: %s, %s\n",
  159. mei_pg_is_enabled(dev) ? "ENABLED" : "DISABLED",
  160. mei_pg_state_str(mei_pg_state(dev)));
  161. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
  162. kfree(buf);
  163. return ret;
  164. }
  165. static const struct file_operations mei_dbgfs_fops_devstate = {
  166. .open = simple_open,
  167. .read = mei_dbgfs_read_devstate,
  168. .llseek = generic_file_llseek,
  169. };
  170. static ssize_t mei_dbgfs_write_allow_fa(struct file *file,
  171. const char __user *user_buf,
  172. size_t count, loff_t *ppos)
  173. {
  174. struct mei_device *dev;
  175. int ret;
  176. dev = container_of(file->private_data,
  177. struct mei_device, allow_fixed_address);
  178. ret = debugfs_write_file_bool(file, user_buf, count, ppos);
  179. if (ret < 0)
  180. return ret;
  181. dev->override_fixed_address = true;
  182. return ret;
  183. }
  184. static const struct file_operations mei_dbgfs_fops_allow_fa = {
  185. .open = simple_open,
  186. .read = debugfs_read_file_bool,
  187. .write = mei_dbgfs_write_allow_fa,
  188. .llseek = generic_file_llseek,
  189. };
  190. /**
  191. * mei_dbgfs_deregister - Remove the debugfs files and directories
  192. *
  193. * @dev: the mei device structure
  194. */
  195. void mei_dbgfs_deregister(struct mei_device *dev)
  196. {
  197. if (!dev->dbgfs_dir)
  198. return;
  199. debugfs_remove_recursive(dev->dbgfs_dir);
  200. dev->dbgfs_dir = NULL;
  201. }
  202. /**
  203. * mei_dbgfs_register - Add the debugfs files
  204. *
  205. * @dev: the mei device structure
  206. * @name: the mei device name
  207. *
  208. * Return: 0 on success, <0 on failure.
  209. */
  210. int mei_dbgfs_register(struct mei_device *dev, const char *name)
  211. {
  212. struct dentry *dir, *f;
  213. dir = debugfs_create_dir(name, NULL);
  214. if (!dir)
  215. return -ENOMEM;
  216. dev->dbgfs_dir = dir;
  217. f = debugfs_create_file("meclients", S_IRUSR, dir,
  218. dev, &mei_dbgfs_fops_meclients);
  219. if (!f) {
  220. dev_err(dev->dev, "meclients: registration failed\n");
  221. goto err;
  222. }
  223. f = debugfs_create_file("active", S_IRUSR, dir,
  224. dev, &mei_dbgfs_fops_active);
  225. if (!f) {
  226. dev_err(dev->dev, "active: registration failed\n");
  227. goto err;
  228. }
  229. f = debugfs_create_file("devstate", S_IRUSR, dir,
  230. dev, &mei_dbgfs_fops_devstate);
  231. if (!f) {
  232. dev_err(dev->dev, "devstate: registration failed\n");
  233. goto err;
  234. }
  235. f = debugfs_create_file("allow_fixed_address", S_IRUSR | S_IWUSR, dir,
  236. &dev->allow_fixed_address,
  237. &mei_dbgfs_fops_allow_fa);
  238. if (!f) {
  239. dev_err(dev->dev, "allow_fixed_address: registration failed\n");
  240. goto err;
  241. }
  242. return 0;
  243. err:
  244. mei_dbgfs_deregister(dev);
  245. return -ENODEV;
  246. }