debugfs.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. pos += scnprintf(buf + pos, bufsz - pos, "\tDR: %01d\n",
  158. dev->hbm_f_dr_supported);
  159. }
  160. pos += scnprintf(buf + pos, bufsz - pos, "pg: %s, %s\n",
  161. mei_pg_is_enabled(dev) ? "ENABLED" : "DISABLED",
  162. mei_pg_state_str(mei_pg_state(dev)));
  163. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
  164. kfree(buf);
  165. return ret;
  166. }
  167. static const struct file_operations mei_dbgfs_fops_devstate = {
  168. .open = simple_open,
  169. .read = mei_dbgfs_read_devstate,
  170. .llseek = generic_file_llseek,
  171. };
  172. static ssize_t mei_dbgfs_write_allow_fa(struct file *file,
  173. const char __user *user_buf,
  174. size_t count, loff_t *ppos)
  175. {
  176. struct mei_device *dev;
  177. int ret;
  178. dev = container_of(file->private_data,
  179. struct mei_device, allow_fixed_address);
  180. ret = debugfs_write_file_bool(file, user_buf, count, ppos);
  181. if (ret < 0)
  182. return ret;
  183. dev->override_fixed_address = true;
  184. return ret;
  185. }
  186. static const struct file_operations mei_dbgfs_fops_allow_fa = {
  187. .open = simple_open,
  188. .read = debugfs_read_file_bool,
  189. .write = mei_dbgfs_write_allow_fa,
  190. .llseek = generic_file_llseek,
  191. };
  192. /**
  193. * mei_dbgfs_deregister - Remove the debugfs files and directories
  194. *
  195. * @dev: the mei device structure
  196. */
  197. void mei_dbgfs_deregister(struct mei_device *dev)
  198. {
  199. if (!dev->dbgfs_dir)
  200. return;
  201. debugfs_remove_recursive(dev->dbgfs_dir);
  202. dev->dbgfs_dir = NULL;
  203. }
  204. /**
  205. * mei_dbgfs_register - Add the debugfs files
  206. *
  207. * @dev: the mei device structure
  208. * @name: the mei device name
  209. *
  210. * Return: 0 on success, <0 on failure.
  211. */
  212. int mei_dbgfs_register(struct mei_device *dev, const char *name)
  213. {
  214. struct dentry *dir, *f;
  215. dir = debugfs_create_dir(name, NULL);
  216. if (!dir)
  217. return -ENOMEM;
  218. dev->dbgfs_dir = dir;
  219. f = debugfs_create_file("meclients", S_IRUSR, dir,
  220. dev, &mei_dbgfs_fops_meclients);
  221. if (!f) {
  222. dev_err(dev->dev, "meclients: registration failed\n");
  223. goto err;
  224. }
  225. f = debugfs_create_file("active", S_IRUSR, dir,
  226. dev, &mei_dbgfs_fops_active);
  227. if (!f) {
  228. dev_err(dev->dev, "active: registration failed\n");
  229. goto err;
  230. }
  231. f = debugfs_create_file("devstate", S_IRUSR, dir,
  232. dev, &mei_dbgfs_fops_devstate);
  233. if (!f) {
  234. dev_err(dev->dev, "devstate: registration failed\n");
  235. goto err;
  236. }
  237. f = debugfs_create_file("allow_fixed_address", S_IRUSR | S_IWUSR, dir,
  238. &dev->allow_fixed_address,
  239. &mei_dbgfs_fops_allow_fa);
  240. if (!f) {
  241. dev_err(dev->dev, "allow_fixed_address: registration failed\n");
  242. goto err;
  243. }
  244. return 0;
  245. err:
  246. mei_dbgfs_deregister(dev);
  247. return -ENODEV;
  248. }