cros_ec_debugfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * cros_ec_debugfs - debug logs for Chrome OS EC
  3. *
  4. * Copyright 2015 Google, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/circ_buf.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/delay.h>
  22. #include <linux/fs.h>
  23. #include <linux/mfd/cros_ec.h>
  24. #include <linux/mfd/cros_ec_commands.h>
  25. #include <linux/mutex.h>
  26. #include <linux/poll.h>
  27. #include <linux/sched.h>
  28. #include <linux/slab.h>
  29. #include <linux/wait.h>
  30. #include "cros_ec_dev.h"
  31. #include "cros_ec_debugfs.h"
  32. #define LOG_SHIFT 14
  33. #define LOG_SIZE (1 << LOG_SHIFT)
  34. #define LOG_POLL_SEC 10
  35. #define CIRC_ADD(idx, size, value) (((idx) + (value)) & ((size) - 1))
  36. /* struct cros_ec_debugfs - ChromeOS EC debugging information
  37. *
  38. * @ec: EC device this debugfs information belongs to
  39. * @dir: dentry for debugfs files
  40. * @log_buffer: circular buffer for console log information
  41. * @read_msg: preallocated EC command and buffer to read console log
  42. * @log_mutex: mutex to protect circular buffer
  43. * @log_wq: waitqueue for log readers
  44. * @log_poll_work: recurring task to poll EC for new console log data
  45. * @panicinfo_blob: panicinfo debugfs blob
  46. */
  47. struct cros_ec_debugfs {
  48. struct cros_ec_dev *ec;
  49. struct dentry *dir;
  50. /* EC log */
  51. struct circ_buf log_buffer;
  52. struct cros_ec_command *read_msg;
  53. struct mutex log_mutex;
  54. wait_queue_head_t log_wq;
  55. struct delayed_work log_poll_work;
  56. /* EC panicinfo */
  57. struct debugfs_blob_wrapper panicinfo_blob;
  58. };
  59. /*
  60. * We need to make sure that the EC log buffer on the UART is large enough,
  61. * so that it is unlikely enough to overlow within LOG_POLL_SEC.
  62. */
  63. static void cros_ec_console_log_work(struct work_struct *__work)
  64. {
  65. struct cros_ec_debugfs *debug_info =
  66. container_of(to_delayed_work(__work),
  67. struct cros_ec_debugfs,
  68. log_poll_work);
  69. struct cros_ec_dev *ec = debug_info->ec;
  70. struct circ_buf *cb = &debug_info->log_buffer;
  71. struct cros_ec_command snapshot_msg = {
  72. .command = EC_CMD_CONSOLE_SNAPSHOT + ec->cmd_offset,
  73. };
  74. struct ec_params_console_read_v1 *read_params =
  75. (struct ec_params_console_read_v1 *)debug_info->read_msg->data;
  76. uint8_t *ec_buffer = (uint8_t *)debug_info->read_msg->data;
  77. int idx;
  78. int buf_space;
  79. int ret;
  80. ret = cros_ec_cmd_xfer(ec->ec_dev, &snapshot_msg);
  81. if (ret < 0) {
  82. dev_err(ec->dev, "EC communication failed\n");
  83. goto resched;
  84. }
  85. if (snapshot_msg.result != EC_RES_SUCCESS) {
  86. dev_err(ec->dev, "EC failed to snapshot the console log\n");
  87. goto resched;
  88. }
  89. /* Loop until we have read everything, or there's an error. */
  90. mutex_lock(&debug_info->log_mutex);
  91. buf_space = CIRC_SPACE(cb->head, cb->tail, LOG_SIZE);
  92. while (1) {
  93. if (!buf_space) {
  94. dev_info_once(ec->dev,
  95. "Some logs may have been dropped...\n");
  96. break;
  97. }
  98. memset(read_params, '\0', sizeof(*read_params));
  99. read_params->subcmd = CONSOLE_READ_RECENT;
  100. ret = cros_ec_cmd_xfer(ec->ec_dev, debug_info->read_msg);
  101. if (ret < 0) {
  102. dev_err(ec->dev, "EC communication failed\n");
  103. break;
  104. }
  105. if (debug_info->read_msg->result != EC_RES_SUCCESS) {
  106. dev_err(ec->dev,
  107. "EC failed to read the console log\n");
  108. break;
  109. }
  110. /* If the buffer is empty, we're done here. */
  111. if (ret == 0 || ec_buffer[0] == '\0')
  112. break;
  113. idx = 0;
  114. while (idx < ret && ec_buffer[idx] != '\0' && buf_space > 0) {
  115. cb->buf[cb->head] = ec_buffer[idx];
  116. cb->head = CIRC_ADD(cb->head, LOG_SIZE, 1);
  117. idx++;
  118. buf_space--;
  119. }
  120. wake_up(&debug_info->log_wq);
  121. }
  122. mutex_unlock(&debug_info->log_mutex);
  123. resched:
  124. schedule_delayed_work(&debug_info->log_poll_work,
  125. msecs_to_jiffies(LOG_POLL_SEC * 1000));
  126. }
  127. static int cros_ec_console_log_open(struct inode *inode, struct file *file)
  128. {
  129. file->private_data = inode->i_private;
  130. return nonseekable_open(inode, file);
  131. }
  132. static ssize_t cros_ec_console_log_read(struct file *file, char __user *buf,
  133. size_t count, loff_t *ppos)
  134. {
  135. struct cros_ec_debugfs *debug_info = file->private_data;
  136. struct circ_buf *cb = &debug_info->log_buffer;
  137. ssize_t ret;
  138. mutex_lock(&debug_info->log_mutex);
  139. while (!CIRC_CNT(cb->head, cb->tail, LOG_SIZE)) {
  140. if (file->f_flags & O_NONBLOCK) {
  141. ret = -EAGAIN;
  142. goto error;
  143. }
  144. mutex_unlock(&debug_info->log_mutex);
  145. ret = wait_event_interruptible(debug_info->log_wq,
  146. CIRC_CNT(cb->head, cb->tail, LOG_SIZE));
  147. if (ret < 0)
  148. return ret;
  149. mutex_lock(&debug_info->log_mutex);
  150. }
  151. /* Only copy until the end of the circular buffer, and let userspace
  152. * retry to get the rest of the data.
  153. */
  154. ret = min_t(size_t, CIRC_CNT_TO_END(cb->head, cb->tail, LOG_SIZE),
  155. count);
  156. if (copy_to_user(buf, cb->buf + cb->tail, ret)) {
  157. ret = -EFAULT;
  158. goto error;
  159. }
  160. cb->tail = CIRC_ADD(cb->tail, LOG_SIZE, ret);
  161. error:
  162. mutex_unlock(&debug_info->log_mutex);
  163. return ret;
  164. }
  165. static unsigned int cros_ec_console_log_poll(struct file *file,
  166. poll_table *wait)
  167. {
  168. struct cros_ec_debugfs *debug_info = file->private_data;
  169. unsigned int mask = 0;
  170. poll_wait(file, &debug_info->log_wq, wait);
  171. mutex_lock(&debug_info->log_mutex);
  172. if (CIRC_CNT(debug_info->log_buffer.head,
  173. debug_info->log_buffer.tail,
  174. LOG_SIZE))
  175. mask |= POLLIN | POLLRDNORM;
  176. mutex_unlock(&debug_info->log_mutex);
  177. return mask;
  178. }
  179. static int cros_ec_console_log_release(struct inode *inode, struct file *file)
  180. {
  181. return 0;
  182. }
  183. const struct file_operations cros_ec_console_log_fops = {
  184. .owner = THIS_MODULE,
  185. .open = cros_ec_console_log_open,
  186. .read = cros_ec_console_log_read,
  187. .llseek = no_llseek,
  188. .poll = cros_ec_console_log_poll,
  189. .release = cros_ec_console_log_release,
  190. };
  191. static int ec_read_version_supported(struct cros_ec_dev *ec)
  192. {
  193. struct ec_params_get_cmd_versions_v1 *params;
  194. struct ec_response_get_cmd_versions *response;
  195. int ret;
  196. struct cros_ec_command *msg;
  197. msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*response)),
  198. GFP_KERNEL);
  199. if (!msg)
  200. return 0;
  201. msg->command = EC_CMD_GET_CMD_VERSIONS + ec->cmd_offset;
  202. msg->outsize = sizeof(*params);
  203. msg->insize = sizeof(*response);
  204. params = (struct ec_params_get_cmd_versions_v1 *)msg->data;
  205. params->cmd = EC_CMD_CONSOLE_READ;
  206. response = (struct ec_response_get_cmd_versions *)msg->data;
  207. ret = cros_ec_cmd_xfer(ec->ec_dev, msg) >= 0 &&
  208. msg->result == EC_RES_SUCCESS &&
  209. (response->version_mask & EC_VER_MASK(1));
  210. kfree(msg);
  211. return ret;
  212. }
  213. static int cros_ec_create_console_log(struct cros_ec_debugfs *debug_info)
  214. {
  215. struct cros_ec_dev *ec = debug_info->ec;
  216. char *buf;
  217. int read_params_size;
  218. int read_response_size;
  219. if (!ec_read_version_supported(ec)) {
  220. dev_warn(ec->dev,
  221. "device does not support reading the console log\n");
  222. return 0;
  223. }
  224. buf = devm_kzalloc(ec->dev, LOG_SIZE, GFP_KERNEL);
  225. if (!buf)
  226. return -ENOMEM;
  227. read_params_size = sizeof(struct ec_params_console_read_v1);
  228. read_response_size = ec->ec_dev->max_response;
  229. debug_info->read_msg = devm_kzalloc(ec->dev,
  230. sizeof(*debug_info->read_msg) +
  231. max(read_params_size, read_response_size), GFP_KERNEL);
  232. if (!debug_info->read_msg)
  233. return -ENOMEM;
  234. debug_info->read_msg->version = 1;
  235. debug_info->read_msg->command = EC_CMD_CONSOLE_READ + ec->cmd_offset;
  236. debug_info->read_msg->outsize = read_params_size;
  237. debug_info->read_msg->insize = read_response_size;
  238. debug_info->log_buffer.buf = buf;
  239. debug_info->log_buffer.head = 0;
  240. debug_info->log_buffer.tail = 0;
  241. mutex_init(&debug_info->log_mutex);
  242. init_waitqueue_head(&debug_info->log_wq);
  243. if (!debugfs_create_file("console_log",
  244. S_IFREG | S_IRUGO,
  245. debug_info->dir,
  246. debug_info,
  247. &cros_ec_console_log_fops))
  248. return -ENOMEM;
  249. INIT_DELAYED_WORK(&debug_info->log_poll_work,
  250. cros_ec_console_log_work);
  251. schedule_delayed_work(&debug_info->log_poll_work, 0);
  252. return 0;
  253. }
  254. static void cros_ec_cleanup_console_log(struct cros_ec_debugfs *debug_info)
  255. {
  256. if (debug_info->log_buffer.buf) {
  257. cancel_delayed_work_sync(&debug_info->log_poll_work);
  258. mutex_destroy(&debug_info->log_mutex);
  259. }
  260. }
  261. static int cros_ec_create_panicinfo(struct cros_ec_debugfs *debug_info)
  262. {
  263. struct cros_ec_device *ec_dev = debug_info->ec->ec_dev;
  264. int ret;
  265. struct cros_ec_command *msg;
  266. int insize;
  267. insize = ec_dev->max_response;
  268. msg = devm_kzalloc(debug_info->ec->dev,
  269. sizeof(*msg) + insize, GFP_KERNEL);
  270. if (!msg)
  271. return -ENOMEM;
  272. msg->command = EC_CMD_GET_PANIC_INFO;
  273. msg->insize = insize;
  274. ret = cros_ec_cmd_xfer(ec_dev, msg);
  275. if (ret < 0) {
  276. dev_warn(debug_info->ec->dev, "Cannot read panicinfo.\n");
  277. ret = 0;
  278. goto free;
  279. }
  280. /* No panic data */
  281. if (ret == 0)
  282. goto free;
  283. debug_info->panicinfo_blob.data = msg->data;
  284. debug_info->panicinfo_blob.size = ret;
  285. if (!debugfs_create_blob("panicinfo",
  286. S_IFREG | S_IRUGO,
  287. debug_info->dir,
  288. &debug_info->panicinfo_blob)) {
  289. ret = -ENOMEM;
  290. goto free;
  291. }
  292. return 0;
  293. free:
  294. devm_kfree(debug_info->ec->dev, msg);
  295. return ret;
  296. }
  297. int cros_ec_debugfs_init(struct cros_ec_dev *ec)
  298. {
  299. struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev);
  300. const char *name = ec_platform->ec_name;
  301. struct cros_ec_debugfs *debug_info;
  302. int ret;
  303. debug_info = devm_kzalloc(ec->dev, sizeof(*debug_info), GFP_KERNEL);
  304. if (!debug_info)
  305. return -ENOMEM;
  306. debug_info->ec = ec;
  307. debug_info->dir = debugfs_create_dir(name, NULL);
  308. if (!debug_info->dir)
  309. return -ENOMEM;
  310. ret = cros_ec_create_panicinfo(debug_info);
  311. if (ret)
  312. goto remove_debugfs;
  313. ret = cros_ec_create_console_log(debug_info);
  314. if (ret)
  315. goto remove_debugfs;
  316. ec->debug_info = debug_info;
  317. return 0;
  318. remove_debugfs:
  319. debugfs_remove_recursive(debug_info->dir);
  320. return ret;
  321. }
  322. void cros_ec_debugfs_remove(struct cros_ec_dev *ec)
  323. {
  324. if (!ec->debug_info)
  325. return;
  326. debugfs_remove_recursive(ec->debug_info->dir);
  327. cros_ec_cleanup_console_log(ec->debug_info);
  328. }