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 /* the order in this array is important. it matches the ordering of the
68 * values in the FW so the index is the same value as in ice_fwlog_level
70 static const char * const ice_fwlog_log_size[] = {
79 * ice_fwlog_print_module_cfg - print current FW logging module configuration
80 * @hw: pointer to the HW structure
81 * @module: module to print
82 * @s: the seq file to put data into
85 ice_fwlog_print_module_cfg(struct ice_hw *hw, int module, struct seq_file *s)
87 struct ice_fwlog_cfg *cfg = &hw->fwlog_cfg;
88 struct ice_fwlog_module_entry *entry;
90 if (module != ICE_AQC_FW_LOG_ID_MAX) {
91 entry = &cfg->module_entries[module];
93 seq_printf(s, "\tModule: %s, Log Level: %s\n",
94 ice_fwlog_module_string[entry->module_id],
95 ice_fwlog_level_string[entry->log_level]);
99 for (i = 0; i < ICE_AQC_FW_LOG_ID_MAX; i++) {
100 entry = &cfg->module_entries[i];
102 seq_printf(s, "\tModule: %s, Log Level: %s\n",
103 ice_fwlog_module_string[entry->module_id],
104 ice_fwlog_level_string[entry->log_level]);
109 static int ice_find_module_by_dentry(struct ice_pf *pf, struct dentry *d)
114 /* find the module based on the dentry */
115 for (i = 0; i < ICE_NR_FW_LOG_MODULES; i++) {
116 if (d == pf->ice_debugfs_pf_fwlog_modules[i]) {
126 * ice_debugfs_module_show - read from 'module' file
127 * @s: the opened file
128 * @v: pointer to the offset
130 static int ice_debugfs_module_show(struct seq_file *s, void *v)
132 const struct file *filp = s->file;
133 struct dentry *dentry;
137 dentry = file_dentry(filp);
140 module = ice_find_module_by_dentry(pf, dentry);
142 dev_info(ice_pf_to_dev(pf), "unknown module\n");
146 ice_fwlog_print_module_cfg(&pf->hw, module, s);
151 static int ice_debugfs_module_open(struct inode *inode, struct file *filp)
153 return single_open(filp, ice_debugfs_module_show, inode->i_private);
157 * ice_debugfs_module_write - write into 'module' file
158 * @filp: the opened file
159 * @buf: where to find the user's data
160 * @count: the length of the user's data
161 * @ppos: file position offset
164 ice_debugfs_module_write(struct file *filp, const char __user *buf,
165 size_t count, loff_t *ppos)
167 struct ice_pf *pf = file_inode(filp)->i_private;
168 struct dentry *dentry = file_dentry(filp);
169 struct device *dev = ice_pf_to_dev(pf);
170 char user_val[16], *cmd_buf;
171 int module, log_level, cnt;
173 /* don't allow partial writes or invalid input */
174 if (*ppos != 0 || count > 8)
177 cmd_buf = memdup_user(buf, count);
179 return PTR_ERR(cmd_buf);
181 module = ice_find_module_by_dentry(pf, dentry);
183 dev_info(dev, "unknown module\n");
187 cnt = sscanf(cmd_buf, "%s", user_val);
191 log_level = sysfs_match_string(ice_fwlog_level_string, user_val);
193 dev_info(dev, "unknown log level '%s'\n", user_val);
197 if (module != ICE_AQC_FW_LOG_ID_MAX) {
198 ice_pf_fwlog_update_module(pf, log_level, module);
200 /* the module 'all' is a shortcut so that we can set
201 * all of the modules to the same level quickly
205 for (i = 0; i < ICE_AQC_FW_LOG_ID_MAX; i++)
206 ice_pf_fwlog_update_module(pf, log_level, i);
212 static const struct file_operations ice_debugfs_module_fops = {
213 .owner = THIS_MODULE,
214 .open = ice_debugfs_module_open,
216 .release = single_release,
217 .write = ice_debugfs_module_write,
221 * ice_debugfs_nr_messages_read - read from 'nr_messages' file
222 * @filp: the opened file
223 * @buffer: where to write the data for the user to read
224 * @count: the size of the user's buffer
225 * @ppos: file position offset
227 static ssize_t ice_debugfs_nr_messages_read(struct file *filp,
228 char __user *buffer, size_t count,
231 struct ice_pf *pf = filp->private_data;
232 struct ice_hw *hw = &pf->hw;
235 snprintf(buff, sizeof(buff), "%d\n",
236 hw->fwlog_cfg.log_resolution);
238 return simple_read_from_buffer(buffer, count, ppos, buff, strlen(buff));
242 * ice_debugfs_nr_messages_write - write into 'nr_messages' file
243 * @filp: the opened file
244 * @buf: where to find the user's data
245 * @count: the length of the user's data
246 * @ppos: file position offset
249 ice_debugfs_nr_messages_write(struct file *filp, const char __user *buf,
250 size_t count, loff_t *ppos)
252 struct ice_pf *pf = filp->private_data;
253 struct device *dev = ice_pf_to_dev(pf);
254 struct ice_hw *hw = &pf->hw;
255 char user_val[8], *cmd_buf;
259 /* don't allow partial writes or invalid input */
260 if (*ppos != 0 || count > 4)
263 cmd_buf = memdup_user(buf, count);
265 return PTR_ERR(cmd_buf);
267 ret = sscanf(cmd_buf, "%s", user_val);
271 ret = kstrtos16(user_val, 0, &nr_messages);
275 if (nr_messages < ICE_AQC_FW_LOG_MIN_RESOLUTION ||
276 nr_messages > ICE_AQC_FW_LOG_MAX_RESOLUTION) {
277 dev_err(dev, "Invalid FW log number of messages %d, value must be between %d - %d\n",
278 nr_messages, ICE_AQC_FW_LOG_MIN_RESOLUTION,
279 ICE_AQC_FW_LOG_MAX_RESOLUTION);
283 hw->fwlog_cfg.log_resolution = nr_messages;
288 static const struct file_operations ice_debugfs_nr_messages_fops = {
289 .owner = THIS_MODULE,
291 .read = ice_debugfs_nr_messages_read,
292 .write = ice_debugfs_nr_messages_write,
296 * ice_debugfs_enable_read - read from 'enable' file
297 * @filp: the opened file
298 * @buffer: where to write the data for the user to read
299 * @count: the size of the user's buffer
300 * @ppos: file position offset
302 static ssize_t ice_debugfs_enable_read(struct file *filp,
303 char __user *buffer, size_t count,
306 struct ice_pf *pf = filp->private_data;
307 struct ice_hw *hw = &pf->hw;
310 snprintf(buff, sizeof(buff), "%u\n",
311 (u16)(hw->fwlog_cfg.options &
312 ICE_FWLOG_OPTION_IS_REGISTERED) >> 3);
314 return simple_read_from_buffer(buffer, count, ppos, buff, strlen(buff));
318 * ice_debugfs_enable_write - write into 'enable' file
319 * @filp: the opened file
320 * @buf: where to find the user's data
321 * @count: the length of the user's data
322 * @ppos: file position offset
325 ice_debugfs_enable_write(struct file *filp, const char __user *buf,
326 size_t count, loff_t *ppos)
328 struct ice_pf *pf = filp->private_data;
329 struct ice_hw *hw = &pf->hw;
330 char user_val[8], *cmd_buf;
334 /* don't allow partial writes or invalid input */
335 if (*ppos != 0 || count > 2)
338 cmd_buf = memdup_user(buf, count);
340 return PTR_ERR(cmd_buf);
342 ret = sscanf(cmd_buf, "%s", user_val);
346 ret = kstrtobool(user_val, &enable);
348 goto enable_write_error;
351 hw->fwlog_cfg.options |= ICE_FWLOG_OPTION_ARQ_ENA;
353 hw->fwlog_cfg.options &= ~ICE_FWLOG_OPTION_ARQ_ENA;
355 ret = ice_fwlog_set(hw, &hw->fwlog_cfg);
357 goto enable_write_error;
360 ret = ice_fwlog_register(hw);
362 ret = ice_fwlog_unregister(hw);
365 goto enable_write_error;
367 /* if we get here, nothing went wrong; return count since we didn't
368 * really write anything
370 ret = (ssize_t)count;
373 /* This function always consumes all of the written input, or produces
374 * an error. Check and enforce this. Otherwise, the write operation
375 * won't complete properly.
377 if (WARN_ON(ret != (ssize_t)count && ret >= 0))
383 static const struct file_operations ice_debugfs_enable_fops = {
384 .owner = THIS_MODULE,
386 .read = ice_debugfs_enable_read,
387 .write = ice_debugfs_enable_write,
391 * ice_debugfs_log_size_read - read from 'log_size' file
392 * @filp: the opened file
393 * @buffer: where to write the data for the user to read
394 * @count: the size of the user's buffer
395 * @ppos: file position offset
397 static ssize_t ice_debugfs_log_size_read(struct file *filp,
398 char __user *buffer, size_t count,
401 struct ice_pf *pf = filp->private_data;
402 struct ice_hw *hw = &pf->hw;
406 index = hw->fwlog_ring.index;
407 snprintf(buff, sizeof(buff), "%s\n", ice_fwlog_log_size[index]);
409 return simple_read_from_buffer(buffer, count, ppos, buff, strlen(buff));
413 * ice_debugfs_log_size_write - write into 'log_size' file
414 * @filp: the opened file
415 * @buf: where to find the user's data
416 * @count: the length of the user's data
417 * @ppos: file position offset
420 ice_debugfs_log_size_write(struct file *filp, const char __user *buf,
421 size_t count, loff_t *ppos)
423 struct ice_pf *pf = filp->private_data;
424 struct device *dev = ice_pf_to_dev(pf);
425 struct ice_hw *hw = &pf->hw;
426 char user_val[8], *cmd_buf;
430 /* don't allow partial writes or invalid input */
431 if (*ppos != 0 || count > 5)
434 cmd_buf = memdup_user(buf, count);
436 return PTR_ERR(cmd_buf);
438 ret = sscanf(cmd_buf, "%s", user_val);
442 index = sysfs_match_string(ice_fwlog_log_size, user_val);
444 dev_info(dev, "Invalid log size '%s'. The value must be one of 128K, 256K, 512K, 1M, 2M\n",
447 goto log_size_write_error;
448 } else if (hw->fwlog_cfg.options & ICE_FWLOG_OPTION_IS_REGISTERED) {
449 dev_info(dev, "FW logging is currently running. Please disable FW logging to change log_size\n");
451 goto log_size_write_error;
454 /* free all the buffers and the tracking info and resize */
455 ice_fwlog_realloc_rings(hw, index);
457 /* if we get here, nothing went wrong; return count since we didn't
458 * really write anything
460 ret = (ssize_t)count;
462 log_size_write_error:
463 /* This function always consumes all of the written input, or produces
464 * an error. Check and enforce this. Otherwise, the write operation
465 * won't complete properly.
467 if (WARN_ON(ret != (ssize_t)count && ret >= 0))
473 static const struct file_operations ice_debugfs_log_size_fops = {
474 .owner = THIS_MODULE,
476 .read = ice_debugfs_log_size_read,
477 .write = ice_debugfs_log_size_write,
481 * ice_debugfs_data_read - read from 'data' file
482 * @filp: the opened file
483 * @buffer: where to write the data for the user to read
484 * @count: the size of the user's buffer
485 * @ppos: file position offset
487 static ssize_t ice_debugfs_data_read(struct file *filp, char __user *buffer,
488 size_t count, loff_t *ppos)
490 struct ice_pf *pf = filp->private_data;
491 struct ice_hw *hw = &pf->hw;
495 if (ice_fwlog_ring_empty(&hw->fwlog_ring))
498 while (!ice_fwlog_ring_empty(&hw->fwlog_ring) && !done) {
499 struct ice_fwlog_data *log;
502 log = &hw->fwlog_ring.rings[hw->fwlog_ring.head];
503 cur_buf_len = log->data_size;
504 if (cur_buf_len >= count) {
509 if (copy_to_user(buffer, log->data, cur_buf_len)) {
510 /* if there is an error then bail and return whatever
511 * the driver has copied so far
517 data_copied += cur_buf_len;
518 buffer += cur_buf_len;
519 count -= cur_buf_len;
520 *ppos += cur_buf_len;
521 ice_fwlog_ring_increment(&hw->fwlog_ring.head,
522 hw->fwlog_ring.size);
529 * ice_debugfs_data_write - write into 'data' file
530 * @filp: the opened file
531 * @buf: where to find the user's data
532 * @count: the length of the user's data
533 * @ppos: file position offset
536 ice_debugfs_data_write(struct file *filp, const char __user *buf, size_t count,
539 struct ice_pf *pf = filp->private_data;
540 struct device *dev = ice_pf_to_dev(pf);
541 struct ice_hw *hw = &pf->hw;
544 /* don't allow partial writes */
548 /* any value is allowed to clear the buffer so no need to even look at
551 if (!(hw->fwlog_cfg.options & ICE_FWLOG_OPTION_IS_REGISTERED)) {
552 hw->fwlog_ring.head = 0;
553 hw->fwlog_ring.tail = 0;
555 dev_info(dev, "Can't clear FW log data while FW log running\n");
557 goto nr_buffs_write_error;
560 /* if we get here, nothing went wrong; return count since we didn't
561 * really write anything
563 ret = (ssize_t)count;
565 nr_buffs_write_error:
566 /* This function always consumes all of the written input, or produces
567 * an error. Check and enforce this. Otherwise, the write operation
568 * won't complete properly.
570 if (WARN_ON(ret != (ssize_t)count && ret >= 0))
576 static const struct file_operations ice_debugfs_data_fops = {
577 .owner = THIS_MODULE,
579 .read = ice_debugfs_data_read,
580 .write = ice_debugfs_data_write,
584 * ice_debugfs_fwlog_init - setup the debugfs directory
585 * @pf: the ice that is starting up
587 void ice_debugfs_fwlog_init(struct ice_pf *pf)
589 const char *name = pci_name(pf->pdev);
590 struct dentry *fw_modules_dir;
591 struct dentry **fw_modules;
594 /* only support fw log commands on PF 0 */
598 /* allocate space for this first because if it fails then we don't
601 fw_modules = kcalloc(ICE_NR_FW_LOG_MODULES, sizeof(*fw_modules),
606 pf->ice_debugfs_pf = debugfs_create_dir(name, ice_debugfs_root);
607 if (IS_ERR(pf->ice_debugfs_pf))
608 goto err_create_module_files;
610 pf->ice_debugfs_pf_fwlog = debugfs_create_dir("fwlog",
612 if (IS_ERR(pf->ice_debugfs_pf))
613 goto err_create_module_files;
615 fw_modules_dir = debugfs_create_dir("modules",
616 pf->ice_debugfs_pf_fwlog);
617 if (IS_ERR(fw_modules_dir))
618 goto err_create_module_files;
620 for (i = 0; i < ICE_NR_FW_LOG_MODULES; i++) {
621 fw_modules[i] = debugfs_create_file(ice_fwlog_module_string[i],
622 0600, fw_modules_dir, pf,
623 &ice_debugfs_module_fops);
624 if (IS_ERR(fw_modules[i]))
625 goto err_create_module_files;
628 debugfs_create_file("nr_messages", 0600,
629 pf->ice_debugfs_pf_fwlog, pf,
630 &ice_debugfs_nr_messages_fops);
632 pf->ice_debugfs_pf_fwlog_modules = fw_modules;
634 debugfs_create_file("enable", 0600, pf->ice_debugfs_pf_fwlog,
635 pf, &ice_debugfs_enable_fops);
637 debugfs_create_file("log_size", 0600, pf->ice_debugfs_pf_fwlog,
638 pf, &ice_debugfs_log_size_fops);
640 debugfs_create_file("data", 0600, pf->ice_debugfs_pf_fwlog,
641 pf, &ice_debugfs_data_fops);
645 err_create_module_files:
646 debugfs_remove_recursive(pf->ice_debugfs_pf_fwlog);
651 * ice_debugfs_init - create root directory for debugfs entries
653 void ice_debugfs_init(void)
655 ice_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
656 if (IS_ERR(ice_debugfs_root))
657 pr_info("init of debugfs failed\n");
661 * ice_debugfs_exit - remove debugfs entries
663 void ice_debugfs_exit(void)
665 debugfs_remove_recursive(ice_debugfs_root);
666 ice_debugfs_root = NULL;