1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022, Intel Corporation. */
5 #include <linux/debugfs.h>
6 #include <linux/random.h>
7 #include <linux/vmalloc.h>
10 static struct dentry *ice_debugfs_root;
12 /* create a define that has an extra module that doesn't really exist. this
13 * is so we can add a module 'all' to easily enable/disable all the modules
15 #define ICE_NR_FW_LOG_MODULES (ICE_AQC_FW_LOG_ID_MAX + 1)
17 /* the ordering in this array is important. it matches the ordering of the
18 * values in the FW so the index is the same value as in ice_aqc_fw_logging_mod
20 static const char * const ice_fwlog_module_string[] = {
56 /* the ordering in this array is important. it matches the ordering of the
57 * values in the FW so the index is the same value as in ice_fwlog_level
59 static const char * const ice_fwlog_level_string[] = {
67 static const char * const ice_fwlog_log_size[] = {
76 * ice_fwlog_print_module_cfg - print current FW logging module configuration
77 * @hw: pointer to the HW structure
78 * @module: module to print
79 * @s: the seq file to put data into
82 ice_fwlog_print_module_cfg(struct ice_hw *hw, int module, struct seq_file *s)
84 struct ice_fwlog_cfg *cfg = &hw->fwlog_cfg;
85 struct ice_fwlog_module_entry *entry;
87 if (module != ICE_AQC_FW_LOG_ID_MAX) {
88 entry = &cfg->module_entries[module];
90 seq_printf(s, "\tModule: %s, Log Level: %s\n",
91 ice_fwlog_module_string[entry->module_id],
92 ice_fwlog_level_string[entry->log_level]);
96 for (i = 0; i < ICE_AQC_FW_LOG_ID_MAX; i++) {
97 entry = &cfg->module_entries[i];
99 seq_printf(s, "\tModule: %s, Log Level: %s\n",
100 ice_fwlog_module_string[entry->module_id],
101 ice_fwlog_level_string[entry->log_level]);
106 static int ice_find_module_by_dentry(struct ice_pf *pf, struct dentry *d)
111 /* find the module based on the dentry */
112 for (i = 0; i < ICE_NR_FW_LOG_MODULES; i++) {
113 if (d == pf->ice_debugfs_pf_fwlog_modules[i]) {
123 * ice_debugfs_module_show - read from 'module' file
124 * @s: the opened file
125 * @v: pointer to the offset
127 static int ice_debugfs_module_show(struct seq_file *s, void *v)
129 const struct file *filp = s->file;
130 struct dentry *dentry;
134 dentry = file_dentry(filp);
137 module = ice_find_module_by_dentry(pf, dentry);
139 dev_info(ice_pf_to_dev(pf), "unknown module\n");
143 ice_fwlog_print_module_cfg(&pf->hw, module, s);
148 static int ice_debugfs_module_open(struct inode *inode, struct file *filp)
150 return single_open(filp, ice_debugfs_module_show, inode->i_private);
154 * ice_debugfs_module_write - write into 'module' file
155 * @filp: the opened file
156 * @buf: where to find the user's data
157 * @count: the length of the user's data
158 * @ppos: file position offset
161 ice_debugfs_module_write(struct file *filp, const char __user *buf,
162 size_t count, loff_t *ppos)
164 struct ice_pf *pf = file_inode(filp)->i_private;
165 struct dentry *dentry = file_dentry(filp);
166 struct device *dev = ice_pf_to_dev(pf);
167 char user_val[16], *cmd_buf;
168 int module, log_level, cnt;
170 /* don't allow partial writes or invalid input */
171 if (*ppos != 0 || count > 8)
174 cmd_buf = memdup_user_nul(buf, count);
176 return PTR_ERR(cmd_buf);
178 module = ice_find_module_by_dentry(pf, dentry);
180 dev_info(dev, "unknown module\n");
184 cnt = sscanf(cmd_buf, "%s", user_val);
188 log_level = sysfs_match_string(ice_fwlog_level_string, user_val);
190 dev_info(dev, "unknown log level '%s'\n", user_val);
194 if (module != ICE_AQC_FW_LOG_ID_MAX) {
195 ice_pf_fwlog_update_module(pf, log_level, module);
197 /* the module 'all' is a shortcut so that we can set
198 * all of the modules to the same level quickly
202 for (i = 0; i < ICE_AQC_FW_LOG_ID_MAX; i++)
203 ice_pf_fwlog_update_module(pf, log_level, i);
209 static const struct file_operations ice_debugfs_module_fops = {
210 .owner = THIS_MODULE,
211 .open = ice_debugfs_module_open,
213 .release = single_release,
214 .write = ice_debugfs_module_write,
218 * ice_debugfs_nr_messages_read - read from 'nr_messages' file
219 * @filp: the opened file
220 * @buffer: where to write the data for the user to read
221 * @count: the size of the user's buffer
222 * @ppos: file position offset
224 static ssize_t ice_debugfs_nr_messages_read(struct file *filp,
225 char __user *buffer, size_t count,
228 struct ice_pf *pf = filp->private_data;
229 struct ice_hw *hw = &pf->hw;
232 snprintf(buff, sizeof(buff), "%d\n",
233 hw->fwlog_cfg.log_resolution);
235 return simple_read_from_buffer(buffer, count, ppos, buff, strlen(buff));
239 * ice_debugfs_nr_messages_write - write into 'nr_messages' file
240 * @filp: the opened file
241 * @buf: where to find the user's data
242 * @count: the length of the user's data
243 * @ppos: file position offset
246 ice_debugfs_nr_messages_write(struct file *filp, const char __user *buf,
247 size_t count, loff_t *ppos)
249 struct ice_pf *pf = filp->private_data;
250 struct device *dev = ice_pf_to_dev(pf);
251 struct ice_hw *hw = &pf->hw;
252 char user_val[8], *cmd_buf;
256 /* don't allow partial writes or invalid input */
257 if (*ppos != 0 || count > 4)
260 cmd_buf = memdup_user_nul(buf, count);
262 return PTR_ERR(cmd_buf);
264 ret = sscanf(cmd_buf, "%s", user_val);
268 ret = kstrtos16(user_val, 0, &nr_messages);
272 if (nr_messages < ICE_AQC_FW_LOG_MIN_RESOLUTION ||
273 nr_messages > ICE_AQC_FW_LOG_MAX_RESOLUTION) {
274 dev_err(dev, "Invalid FW log number of messages %d, value must be between %d - %d\n",
275 nr_messages, ICE_AQC_FW_LOG_MIN_RESOLUTION,
276 ICE_AQC_FW_LOG_MAX_RESOLUTION);
280 hw->fwlog_cfg.log_resolution = nr_messages;
285 static const struct file_operations ice_debugfs_nr_messages_fops = {
286 .owner = THIS_MODULE,
288 .read = ice_debugfs_nr_messages_read,
289 .write = ice_debugfs_nr_messages_write,
293 * ice_debugfs_enable_read - read from 'enable' file
294 * @filp: the opened file
295 * @buffer: where to write the data for the user to read
296 * @count: the size of the user's buffer
297 * @ppos: file position offset
299 static ssize_t ice_debugfs_enable_read(struct file *filp,
300 char __user *buffer, size_t count,
303 struct ice_pf *pf = filp->private_data;
304 struct ice_hw *hw = &pf->hw;
307 snprintf(buff, sizeof(buff), "%u\n",
308 (u16)(hw->fwlog_cfg.options &
309 ICE_FWLOG_OPTION_IS_REGISTERED) >> 3);
311 return simple_read_from_buffer(buffer, count, ppos, buff, strlen(buff));
315 * ice_debugfs_enable_write - write into 'enable' file
316 * @filp: the opened file
317 * @buf: where to find the user's data
318 * @count: the length of the user's data
319 * @ppos: file position offset
322 ice_debugfs_enable_write(struct file *filp, const char __user *buf,
323 size_t count, loff_t *ppos)
325 struct ice_pf *pf = filp->private_data;
326 struct ice_hw *hw = &pf->hw;
327 char user_val[8], *cmd_buf;
331 /* don't allow partial writes or invalid input */
332 if (*ppos != 0 || count > 2)
335 cmd_buf = memdup_user_nul(buf, count);
337 return PTR_ERR(cmd_buf);
339 ret = sscanf(cmd_buf, "%s", user_val);
343 ret = kstrtobool(user_val, &enable);
345 goto enable_write_error;
348 hw->fwlog_cfg.options |= ICE_FWLOG_OPTION_ARQ_ENA;
350 hw->fwlog_cfg.options &= ~ICE_FWLOG_OPTION_ARQ_ENA;
352 ret = ice_fwlog_set(hw, &hw->fwlog_cfg);
354 goto enable_write_error;
357 ret = ice_fwlog_register(hw);
359 ret = ice_fwlog_unregister(hw);
362 goto enable_write_error;
364 /* if we get here, nothing went wrong; return count since we didn't
365 * really write anything
367 ret = (ssize_t)count;
370 /* This function always consumes all of the written input, or produces
371 * an error. Check and enforce this. Otherwise, the write operation
372 * won't complete properly.
374 if (WARN_ON(ret != (ssize_t)count && ret >= 0))
380 static const struct file_operations ice_debugfs_enable_fops = {
381 .owner = THIS_MODULE,
383 .read = ice_debugfs_enable_read,
384 .write = ice_debugfs_enable_write,
388 * ice_debugfs_log_size_read - read from 'log_size' file
389 * @filp: the opened file
390 * @buffer: where to write the data for the user to read
391 * @count: the size of the user's buffer
392 * @ppos: file position offset
394 static ssize_t ice_debugfs_log_size_read(struct file *filp,
395 char __user *buffer, size_t count,
398 struct ice_pf *pf = filp->private_data;
399 struct ice_hw *hw = &pf->hw;
403 index = hw->fwlog_ring.index;
404 snprintf(buff, sizeof(buff), "%s\n", ice_fwlog_log_size[index]);
406 return simple_read_from_buffer(buffer, count, ppos, buff, strlen(buff));
410 * ice_debugfs_log_size_write - write into 'log_size' file
411 * @filp: the opened file
412 * @buf: where to find the user's data
413 * @count: the length of the user's data
414 * @ppos: file position offset
417 ice_debugfs_log_size_write(struct file *filp, const char __user *buf,
418 size_t count, loff_t *ppos)
420 struct ice_pf *pf = filp->private_data;
421 struct device *dev = ice_pf_to_dev(pf);
422 struct ice_hw *hw = &pf->hw;
423 char user_val[8], *cmd_buf;
427 /* don't allow partial writes or invalid input */
428 if (*ppos != 0 || count > 5)
431 cmd_buf = memdup_user_nul(buf, count);
433 return PTR_ERR(cmd_buf);
435 ret = sscanf(cmd_buf, "%s", user_val);
439 index = sysfs_match_string(ice_fwlog_log_size, user_val);
441 dev_info(dev, "Invalid log size '%s'. The value must be one of 128K, 256K, 512K, 1M, 2M\n",
444 goto log_size_write_error;
445 } else if (hw->fwlog_cfg.options & ICE_FWLOG_OPTION_IS_REGISTERED) {
446 dev_info(dev, "FW logging is currently running. Please disable FW logging to change log_size\n");
448 goto log_size_write_error;
451 /* free all the buffers and the tracking info and resize */
452 ice_fwlog_realloc_rings(hw, index);
454 /* if we get here, nothing went wrong; return count since we didn't
455 * really write anything
457 ret = (ssize_t)count;
459 log_size_write_error:
460 /* This function always consumes all of the written input, or produces
461 * an error. Check and enforce this. Otherwise, the write operation
462 * won't complete properly.
464 if (WARN_ON(ret != (ssize_t)count && ret >= 0))
470 static const struct file_operations ice_debugfs_log_size_fops = {
471 .owner = THIS_MODULE,
473 .read = ice_debugfs_log_size_read,
474 .write = ice_debugfs_log_size_write,
478 * ice_debugfs_data_read - read from 'data' file
479 * @filp: the opened file
480 * @buffer: where to write the data for the user to read
481 * @count: the size of the user's buffer
482 * @ppos: file position offset
484 static ssize_t ice_debugfs_data_read(struct file *filp, char __user *buffer,
485 size_t count, loff_t *ppos)
487 struct ice_pf *pf = filp->private_data;
488 struct ice_hw *hw = &pf->hw;
492 if (ice_fwlog_ring_empty(&hw->fwlog_ring))
495 while (!ice_fwlog_ring_empty(&hw->fwlog_ring) && !done) {
496 struct ice_fwlog_data *log;
499 log = &hw->fwlog_ring.rings[hw->fwlog_ring.head];
500 cur_buf_len = log->data_size;
501 if (cur_buf_len >= count) {
506 if (copy_to_user(buffer, log->data, cur_buf_len)) {
507 /* if there is an error then bail and return whatever
508 * the driver has copied so far
514 data_copied += cur_buf_len;
515 buffer += cur_buf_len;
516 count -= cur_buf_len;
517 *ppos += cur_buf_len;
518 ice_fwlog_ring_increment(&hw->fwlog_ring.head,
519 hw->fwlog_ring.size);
526 * ice_debugfs_data_write - write into 'data' file
527 * @filp: the opened file
528 * @buf: where to find the user's data
529 * @count: the length of the user's data
530 * @ppos: file position offset
533 ice_debugfs_data_write(struct file *filp, const char __user *buf, size_t count,
536 struct ice_pf *pf = filp->private_data;
537 struct device *dev = ice_pf_to_dev(pf);
538 struct ice_hw *hw = &pf->hw;
541 /* don't allow partial writes */
545 /* any value is allowed to clear the buffer so no need to even look at
548 if (!(hw->fwlog_cfg.options & ICE_FWLOG_OPTION_IS_REGISTERED)) {
549 hw->fwlog_ring.head = 0;
550 hw->fwlog_ring.tail = 0;
552 dev_info(dev, "Can't clear FW log data while FW log running\n");
554 goto nr_buffs_write_error;
557 /* if we get here, nothing went wrong; return count since we didn't
558 * really write anything
560 ret = (ssize_t)count;
562 nr_buffs_write_error:
563 /* This function always consumes all of the written input, or produces
564 * an error. Check and enforce this. Otherwise, the write operation
565 * won't complete properly.
567 if (WARN_ON(ret != (ssize_t)count && ret >= 0))
573 static const struct file_operations ice_debugfs_data_fops = {
574 .owner = THIS_MODULE,
576 .read = ice_debugfs_data_read,
577 .write = ice_debugfs_data_write,
581 * ice_debugfs_fwlog_init - setup the debugfs directory
582 * @pf: the ice that is starting up
584 void ice_debugfs_fwlog_init(struct ice_pf *pf)
586 const char *name = pci_name(pf->pdev);
587 struct dentry *fw_modules_dir;
588 struct dentry **fw_modules;
591 /* only support fw log commands on PF 0 */
595 /* allocate space for this first because if it fails then we don't
598 fw_modules = kcalloc(ICE_NR_FW_LOG_MODULES, sizeof(*fw_modules),
603 pf->ice_debugfs_pf = debugfs_create_dir(name, ice_debugfs_root);
604 if (IS_ERR(pf->ice_debugfs_pf))
605 goto err_create_module_files;
607 pf->ice_debugfs_pf_fwlog = debugfs_create_dir("fwlog",
609 if (IS_ERR(pf->ice_debugfs_pf))
610 goto err_create_module_files;
612 fw_modules_dir = debugfs_create_dir("modules",
613 pf->ice_debugfs_pf_fwlog);
614 if (IS_ERR(fw_modules_dir))
615 goto err_create_module_files;
617 for (i = 0; i < ICE_NR_FW_LOG_MODULES; i++) {
618 fw_modules[i] = debugfs_create_file(ice_fwlog_module_string[i],
619 0600, fw_modules_dir, pf,
620 &ice_debugfs_module_fops);
621 if (IS_ERR(fw_modules[i]))
622 goto err_create_module_files;
625 debugfs_create_file("nr_messages", 0600,
626 pf->ice_debugfs_pf_fwlog, pf,
627 &ice_debugfs_nr_messages_fops);
629 pf->ice_debugfs_pf_fwlog_modules = fw_modules;
631 debugfs_create_file("enable", 0600, pf->ice_debugfs_pf_fwlog,
632 pf, &ice_debugfs_enable_fops);
634 debugfs_create_file("log_size", 0600, pf->ice_debugfs_pf_fwlog,
635 pf, &ice_debugfs_log_size_fops);
637 debugfs_create_file("data", 0600, pf->ice_debugfs_pf_fwlog,
638 pf, &ice_debugfs_data_fops);
642 err_create_module_files:
643 debugfs_remove_recursive(pf->ice_debugfs_pf_fwlog);
648 * ice_debugfs_pf_deinit - cleanup PF's debugfs
649 * @pf: pointer to the PF struct
651 void ice_debugfs_pf_deinit(struct ice_pf *pf)
653 debugfs_remove_recursive(pf->ice_debugfs_pf);
654 pf->ice_debugfs_pf = NULL;
658 * ice_debugfs_init - create root directory for debugfs entries
660 void ice_debugfs_init(void)
662 ice_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
663 if (IS_ERR(ice_debugfs_root))
664 pr_info("init of debugfs failed\n");
668 * ice_debugfs_exit - remove debugfs entries
670 void ice_debugfs_exit(void)
672 debugfs_remove_recursive(ice_debugfs_root);
673 ice_debugfs_root = NULL;