2 * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
3 * Copyright (c) 2014- QLogic Corporation.
7 * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License (GPL) Version 2 as
11 * published by the Free Software Foundation
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
19 #include <linux/debugfs.h>
20 #include <linux/export.h>
26 * BFA debufs interface
28 * To access the interface, debugfs file system should be mounted
29 * if not already mounted using:
30 * mount -t debugfs none /sys/kernel/debug
33 * - bfa/pci_dev:<pci_name>
34 * where the pci_name corresponds to the one under /sys/bus/pci/drivers/bfa
36 * Debugging service available per pci_dev:
37 * fwtrc: To collect current firmware trace.
38 * drvtrc: To collect current driver trace
39 * fwsave: To collect last saved fw trace as a result of firmware crash.
40 * regwr: To write one word to chip register
41 * regrd: To read one or more words from chip register.
44 struct bfad_debug_info {
51 bfad_debugfs_open_drvtrc(struct inode *inode, struct file *file)
53 struct bfad_port_s *port = inode->i_private;
54 struct bfad_s *bfad = port->bfad;
55 struct bfad_debug_info *debug;
57 debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
61 debug->debug_buffer = (void *) bfad->trcmod;
62 debug->buffer_len = sizeof(struct bfa_trc_mod_s);
64 file->private_data = debug;
70 bfad_debugfs_open_fwtrc(struct inode *inode, struct file *file)
72 struct bfad_port_s *port = inode->i_private;
73 struct bfad_s *bfad = port->bfad;
74 struct bfad_debug_info *fw_debug;
78 fw_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
82 fw_debug->buffer_len = sizeof(struct bfa_trc_mod_s);
84 fw_debug->debug_buffer = vmalloc(fw_debug->buffer_len);
85 if (!fw_debug->debug_buffer) {
87 printk(KERN_INFO "bfad[%d]: Failed to allocate fwtrc buffer\n",
92 memset(fw_debug->debug_buffer, 0, fw_debug->buffer_len);
94 spin_lock_irqsave(&bfad->bfad_lock, flags);
95 rc = bfa_ioc_debug_fwtrc(&bfad->bfa.ioc,
96 fw_debug->debug_buffer,
97 &fw_debug->buffer_len);
98 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
99 if (rc != BFA_STATUS_OK) {
100 vfree(fw_debug->debug_buffer);
101 fw_debug->debug_buffer = NULL;
103 printk(KERN_INFO "bfad[%d]: Failed to collect fwtrc\n",
108 file->private_data = fw_debug;
114 bfad_debugfs_open_fwsave(struct inode *inode, struct file *file)
116 struct bfad_port_s *port = inode->i_private;
117 struct bfad_s *bfad = port->bfad;
118 struct bfad_debug_info *fw_debug;
122 fw_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
126 fw_debug->buffer_len = sizeof(struct bfa_trc_mod_s);
128 fw_debug->debug_buffer = vmalloc(fw_debug->buffer_len);
129 if (!fw_debug->debug_buffer) {
131 printk(KERN_INFO "bfad[%d]: Failed to allocate fwsave buffer\n",
136 memset(fw_debug->debug_buffer, 0, fw_debug->buffer_len);
138 spin_lock_irqsave(&bfad->bfad_lock, flags);
139 rc = bfa_ioc_debug_fwsave(&bfad->bfa.ioc,
140 fw_debug->debug_buffer,
141 &fw_debug->buffer_len);
142 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
143 if (rc != BFA_STATUS_OK) {
144 vfree(fw_debug->debug_buffer);
145 fw_debug->debug_buffer = NULL;
147 printk(KERN_INFO "bfad[%d]: Failed to collect fwsave\n",
152 file->private_data = fw_debug;
158 bfad_debugfs_open_reg(struct inode *inode, struct file *file)
160 struct bfad_debug_info *reg_debug;
162 reg_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
166 reg_debug->i_private = inode->i_private;
168 file->private_data = reg_debug;
173 /* Changes the current file position */
175 bfad_debugfs_lseek(struct file *file, loff_t offset, int orig)
177 struct bfad_debug_info *debug = file->private_data;
178 return fixed_size_llseek(file, offset, orig,
183 bfad_debugfs_read(struct file *file, char __user *buf,
184 size_t nbytes, loff_t *pos)
186 struct bfad_debug_info *debug = file->private_data;
188 if (!debug || !debug->debug_buffer)
191 return simple_read_from_buffer(buf, nbytes, pos,
192 debug->debug_buffer, debug->buffer_len);
195 #define BFA_REG_CT_ADDRSZ (0x40000)
196 #define BFA_REG_CB_ADDRSZ (0x20000)
197 #define BFA_REG_ADDRSZ(__ioc) \
198 ((u32)(bfa_asic_id_ctc(bfa_ioc_devid(__ioc)) ? \
199 BFA_REG_CT_ADDRSZ : BFA_REG_CB_ADDRSZ))
200 #define BFA_REG_ADDRMSK(__ioc) (BFA_REG_ADDRSZ(__ioc) - 1)
203 bfad_reg_offset_check(struct bfa_s *bfa, u32 offset, u32 len)
208 area = (offset >> 15) & 0x7;
210 /* PCIe core register */
211 if ((offset + (len<<2)) > 0x8000) /* 8k dwords or 32KB */
212 return BFA_STATUS_EINVAL;
213 } else if (area == 0x1) {
214 /* CB 32 KB memory page */
215 if ((offset + (len<<2)) > 0x10000) /* 8k dwords or 32KB */
216 return BFA_STATUS_EINVAL;
218 /* CB register space 64KB */
219 if ((offset + (len<<2)) > BFA_REG_ADDRMSK(&bfa->ioc))
220 return BFA_STATUS_EINVAL;
222 return BFA_STATUS_OK;
226 bfad_debugfs_read_regrd(struct file *file, char __user *buf,
227 size_t nbytes, loff_t *pos)
229 struct bfad_debug_info *regrd_debug = file->private_data;
230 struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private;
231 struct bfad_s *bfad = port->bfad;
237 rc = simple_read_from_buffer(buf, nbytes, pos,
238 bfad->regdata, bfad->reglen);
240 if ((*pos + nbytes) >= bfad->reglen) {
241 kfree(bfad->regdata);
242 bfad->regdata = NULL;
250 bfad_debugfs_write_regrd(struct file *file, const char __user *buf,
251 size_t nbytes, loff_t *ppos)
253 struct bfad_debug_info *regrd_debug = file->private_data;
254 struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private;
255 struct bfad_s *bfad = port->bfad;
256 struct bfa_s *bfa = &bfad->bfa;
257 struct bfa_ioc_s *ioc = &bfa->ioc;
261 void __iomem *rb, *reg_addr;
265 kern_buf = memdup_user(buf, nbytes);
266 if (IS_ERR(kern_buf))
267 return PTR_ERR(kern_buf);
269 rc = sscanf(kern_buf, "%x:%x", &addr, &len);
270 if (rc < 2 || len > (UINT_MAX >> 2)) {
272 "bfad[%d]: %s failed to read user buf\n",
273 bfad->inst_no, __func__);
279 kfree(bfad->regdata);
280 bfad->regdata = NULL;
283 bfad->regdata = kzalloc(len << 2, GFP_KERNEL);
284 if (!bfad->regdata) {
285 printk(KERN_INFO "bfad[%d]: Failed to allocate regrd buffer\n",
290 bfad->reglen = len << 2;
291 rb = bfa_ioc_bar0(ioc);
292 addr &= BFA_REG_ADDRMSK(ioc);
294 /* offset and len sanity check */
295 rc = bfad_reg_offset_check(bfa, addr, len);
297 printk(KERN_INFO "bfad[%d]: Failed reg offset check\n",
299 kfree(bfad->regdata);
300 bfad->regdata = NULL;
305 reg_addr = rb + addr;
306 regbuf = (u32 *)bfad->regdata;
307 spin_lock_irqsave(&bfad->bfad_lock, flags);
308 for (i = 0; i < len; i++) {
309 *regbuf = readl(reg_addr);
311 reg_addr += sizeof(u32);
313 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
319 bfad_debugfs_write_regwr(struct file *file, const char __user *buf,
320 size_t nbytes, loff_t *ppos)
322 struct bfad_debug_info *debug = file->private_data;
323 struct bfad_port_s *port = (struct bfad_port_s *)debug->i_private;
324 struct bfad_s *bfad = port->bfad;
325 struct bfa_s *bfa = &bfad->bfa;
326 struct bfa_ioc_s *ioc = &bfa->ioc;
328 void __iomem *reg_addr;
332 kern_buf = memdup_user(buf, nbytes);
333 if (IS_ERR(kern_buf))
334 return PTR_ERR(kern_buf);
336 rc = sscanf(kern_buf, "%x:%x", &addr, &val);
339 "bfad[%d]: %s failed to read user buf\n",
340 bfad->inst_no, __func__);
346 addr &= BFA_REG_ADDRMSK(ioc); /* offset only 17 bit and word align */
348 /* offset and len sanity check */
349 rc = bfad_reg_offset_check(bfa, addr, 1);
352 "bfad[%d]: Failed reg offset check\n",
357 reg_addr = (bfa_ioc_bar0(ioc)) + addr;
358 spin_lock_irqsave(&bfad->bfad_lock, flags);
359 writel(val, reg_addr);
360 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
366 bfad_debugfs_release(struct inode *inode, struct file *file)
368 struct bfad_debug_info *debug = file->private_data;
373 file->private_data = NULL;
379 bfad_debugfs_release_fwtrc(struct inode *inode, struct file *file)
381 struct bfad_debug_info *fw_debug = file->private_data;
386 if (fw_debug->debug_buffer)
387 vfree(fw_debug->debug_buffer);
389 file->private_data = NULL;
394 static const struct file_operations bfad_debugfs_op_drvtrc = {
395 .owner = THIS_MODULE,
396 .open = bfad_debugfs_open_drvtrc,
397 .llseek = bfad_debugfs_lseek,
398 .read = bfad_debugfs_read,
399 .release = bfad_debugfs_release,
402 static const struct file_operations bfad_debugfs_op_fwtrc = {
403 .owner = THIS_MODULE,
404 .open = bfad_debugfs_open_fwtrc,
405 .llseek = bfad_debugfs_lseek,
406 .read = bfad_debugfs_read,
407 .release = bfad_debugfs_release_fwtrc,
410 static const struct file_operations bfad_debugfs_op_fwsave = {
411 .owner = THIS_MODULE,
412 .open = bfad_debugfs_open_fwsave,
413 .llseek = bfad_debugfs_lseek,
414 .read = bfad_debugfs_read,
415 .release = bfad_debugfs_release_fwtrc,
418 static const struct file_operations bfad_debugfs_op_regrd = {
419 .owner = THIS_MODULE,
420 .open = bfad_debugfs_open_reg,
421 .llseek = bfad_debugfs_lseek,
422 .read = bfad_debugfs_read_regrd,
423 .write = bfad_debugfs_write_regrd,
424 .release = bfad_debugfs_release,
427 static const struct file_operations bfad_debugfs_op_regwr = {
428 .owner = THIS_MODULE,
429 .open = bfad_debugfs_open_reg,
430 .llseek = bfad_debugfs_lseek,
431 .write = bfad_debugfs_write_regwr,
432 .release = bfad_debugfs_release,
435 struct bfad_debugfs_entry {
438 const struct file_operations *fops;
441 static const struct bfad_debugfs_entry bfad_debugfs_files[] = {
442 { "drvtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_drvtrc, },
443 { "fwtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwtrc, },
444 { "fwsave", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwsave, },
445 { "regrd", S_IFREG|S_IRUGO|S_IWUSR, &bfad_debugfs_op_regrd, },
446 { "regwr", S_IFREG|S_IWUSR, &bfad_debugfs_op_regwr, },
449 static struct dentry *bfa_debugfs_root;
450 static atomic_t bfa_debugfs_port_count;
453 bfad_debugfs_init(struct bfad_port_s *port)
455 struct bfad_s *bfad = port->bfad;
456 const struct bfad_debugfs_entry *file;
460 if (!bfa_debugfs_enable)
463 /* Setup the BFA debugfs root directory*/
464 if (!bfa_debugfs_root) {
465 bfa_debugfs_root = debugfs_create_dir("bfa", NULL);
466 atomic_set(&bfa_debugfs_port_count, 0);
467 if (!bfa_debugfs_root) {
469 "BFA debugfs root dir creation failed\n");
474 /* Setup the pci_dev debugfs directory for the port */
475 snprintf(name, sizeof(name), "pci_dev:%s", bfad->pci_name);
476 if (!port->port_debugfs_root) {
477 port->port_debugfs_root =
478 debugfs_create_dir(name, bfa_debugfs_root);
479 if (!port->port_debugfs_root) {
481 "bfa %s: debugfs root creation failed\n",
486 atomic_inc(&bfa_debugfs_port_count);
488 for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
489 file = &bfad_debugfs_files[i];
490 bfad->bfad_dentry_files[i] =
491 debugfs_create_file(file->name,
493 port->port_debugfs_root,
496 if (!bfad->bfad_dentry_files[i]) {
498 "bfa %s: debugfs %s creation failed\n",
499 bfad->pci_name, file->name);
510 bfad_debugfs_exit(struct bfad_port_s *port)
512 struct bfad_s *bfad = port->bfad;
515 for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
516 if (bfad->bfad_dentry_files[i]) {
517 debugfs_remove(bfad->bfad_dentry_files[i]);
518 bfad->bfad_dentry_files[i] = NULL;
522 /* Remove the pci_dev debugfs directory for the port */
523 if (port->port_debugfs_root) {
524 debugfs_remove(port->port_debugfs_root);
525 port->port_debugfs_root = NULL;
526 atomic_dec(&bfa_debugfs_port_count);
529 /* Remove the BFA debugfs root directory */
530 if (atomic_read(&bfa_debugfs_port_count) == 0) {
531 debugfs_remove(bfa_debugfs_root);
532 bfa_debugfs_root = NULL;