2 * Linux network driver for QLogic BR-series Converged Network Adapter.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License (GPL) Version 2 as
6 * published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
14 * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
15 * Copyright (c) 2014-2015 QLogic Corporation
20 #include <linux/debugfs.h>
21 #include <linux/module.h>
25 * BNA debufs interface
27 * To access the interface, debugfs file system should be mounted
28 * if not already mounted using:
29 * mount -t debugfs none /sys/kernel/debug
32 * - bna/pci_dev:<pci_name>
33 * where the pci_name corresponds to the one under /sys/bus/pci/drivers/bna
35 * Debugging service available per pci_dev:
36 * fwtrc: To collect current firmware trace.
37 * fwsave: To collect last saved fw trace as a result of firmware crash.
38 * regwr: To write one word to chip register
39 * regrd: To read one or more words from chip register.
42 struct bnad_debug_info {
49 bnad_debugfs_open_fwtrc(struct inode *inode, struct file *file)
51 struct bnad *bnad = inode->i_private;
52 struct bnad_debug_info *fw_debug;
56 fw_debug = kzalloc(sizeof(struct bnad_debug_info), GFP_KERNEL);
60 fw_debug->buffer_len = BNA_DBG_FWTRC_LEN;
62 fw_debug->debug_buffer = kzalloc(fw_debug->buffer_len, GFP_KERNEL);
63 if (!fw_debug->debug_buffer) {
69 spin_lock_irqsave(&bnad->bna_lock, flags);
70 rc = bfa_nw_ioc_debug_fwtrc(&bnad->bna.ioceth.ioc,
71 fw_debug->debug_buffer,
72 &fw_debug->buffer_len);
73 spin_unlock_irqrestore(&bnad->bna_lock, flags);
74 if (rc != BFA_STATUS_OK) {
75 kfree(fw_debug->debug_buffer);
76 fw_debug->debug_buffer = NULL;
79 pr_warn("bnad %s: Failed to collect fwtrc\n",
80 pci_name(bnad->pcidev));
84 file->private_data = fw_debug;
90 bnad_debugfs_open_fwsave(struct inode *inode, struct file *file)
92 struct bnad *bnad = inode->i_private;
93 struct bnad_debug_info *fw_debug;
97 fw_debug = kzalloc(sizeof(struct bnad_debug_info), GFP_KERNEL);
101 fw_debug->buffer_len = BNA_DBG_FWTRC_LEN;
103 fw_debug->debug_buffer = kzalloc(fw_debug->buffer_len, GFP_KERNEL);
104 if (!fw_debug->debug_buffer) {
110 spin_lock_irqsave(&bnad->bna_lock, flags);
111 rc = bfa_nw_ioc_debug_fwsave(&bnad->bna.ioceth.ioc,
112 fw_debug->debug_buffer,
113 &fw_debug->buffer_len);
114 spin_unlock_irqrestore(&bnad->bna_lock, flags);
115 if (rc != BFA_STATUS_OK && rc != BFA_STATUS_ENOFSAVE) {
116 kfree(fw_debug->debug_buffer);
117 fw_debug->debug_buffer = NULL;
120 pr_warn("bna %s: Failed to collect fwsave\n",
121 pci_name(bnad->pcidev));
125 file->private_data = fw_debug;
131 bnad_debugfs_open_reg(struct inode *inode, struct file *file)
133 struct bnad_debug_info *reg_debug;
135 reg_debug = kzalloc(sizeof(struct bnad_debug_info), GFP_KERNEL);
139 reg_debug->i_private = inode->i_private;
141 file->private_data = reg_debug;
147 bnad_get_debug_drvinfo(struct bnad *bnad, void *buffer, u32 len)
149 struct bnad_drvinfo *drvinfo = (struct bnad_drvinfo *) buffer;
150 struct bnad_iocmd_comp fcomp;
151 unsigned long flags = 0;
152 int ret = BFA_STATUS_FAILED;
155 spin_lock_irqsave(&bnad->bna_lock, flags);
156 bfa_nw_ioc_get_attr(&bnad->bna.ioceth.ioc, &drvinfo->ioc_attr);
157 spin_unlock_irqrestore(&bnad->bna_lock, flags);
159 /* Retrieve CEE related info */
161 fcomp.comp_status = 0;
162 init_completion(&fcomp.comp);
163 spin_lock_irqsave(&bnad->bna_lock, flags);
164 ret = bfa_nw_cee_get_attr(&bnad->bna.cee, &drvinfo->cee_attr,
165 bnad_cb_completion, &fcomp);
166 if (ret != BFA_STATUS_OK) {
167 spin_unlock_irqrestore(&bnad->bna_lock, flags);
170 spin_unlock_irqrestore(&bnad->bna_lock, flags);
171 wait_for_completion(&fcomp.comp);
172 drvinfo->cee_status = fcomp.comp_status;
174 /* Retrieve flash partition info */
175 fcomp.comp_status = 0;
176 reinit_completion(&fcomp.comp);
177 spin_lock_irqsave(&bnad->bna_lock, flags);
178 ret = bfa_nw_flash_get_attr(&bnad->bna.flash, &drvinfo->flash_attr,
179 bnad_cb_completion, &fcomp);
180 if (ret != BFA_STATUS_OK) {
181 spin_unlock_irqrestore(&bnad->bna_lock, flags);
184 spin_unlock_irqrestore(&bnad->bna_lock, flags);
185 wait_for_completion(&fcomp.comp);
186 drvinfo->flash_status = fcomp.comp_status;
192 bnad_debugfs_open_drvinfo(struct inode *inode, struct file *file)
194 struct bnad *bnad = inode->i_private;
195 struct bnad_debug_info *drv_info;
198 drv_info = kzalloc(sizeof(struct bnad_debug_info), GFP_KERNEL);
202 drv_info->buffer_len = sizeof(struct bnad_drvinfo);
204 drv_info->debug_buffer = kzalloc(drv_info->buffer_len, GFP_KERNEL);
205 if (!drv_info->debug_buffer) {
211 mutex_lock(&bnad->conf_mutex);
212 rc = bnad_get_debug_drvinfo(bnad, drv_info->debug_buffer,
213 drv_info->buffer_len);
214 mutex_unlock(&bnad->conf_mutex);
215 if (rc != BFA_STATUS_OK) {
216 kfree(drv_info->debug_buffer);
217 drv_info->debug_buffer = NULL;
220 pr_warn("bna %s: Failed to collect drvinfo\n",
221 pci_name(bnad->pcidev));
225 file->private_data = drv_info;
230 /* Changes the current file position */
232 bnad_debugfs_lseek(struct file *file, loff_t offset, int orig)
234 struct bnad_debug_info *debug = file->private_data;
239 return fixed_size_llseek(file, offset, orig, debug->buffer_len);
243 bnad_debugfs_read(struct file *file, char __user *buf,
244 size_t nbytes, loff_t *pos)
246 struct bnad_debug_info *debug = file->private_data;
248 if (!debug || !debug->debug_buffer)
251 return simple_read_from_buffer(buf, nbytes, pos,
252 debug->debug_buffer, debug->buffer_len);
255 #define BFA_REG_CT_ADDRSZ (0x40000)
256 #define BFA_REG_CB_ADDRSZ (0x20000)
257 #define BFA_REG_ADDRSZ(__ioc) \
258 ((u32)(bfa_asic_id_ctc(bfa_ioc_devid(__ioc)) ? \
259 BFA_REG_CT_ADDRSZ : BFA_REG_CB_ADDRSZ))
260 #define BFA_REG_ADDRMSK(__ioc) (BFA_REG_ADDRSZ(__ioc) - 1)
263 * Function to check if the register offset passed is valid.
266 bna_reg_offset_check(struct bfa_ioc *ioc, u32 offset, u32 len)
271 area = (offset >> 15) & 0x7;
273 /* PCIe core register */
274 if ((offset + (len<<2)) > 0x8000) /* 8k dwords or 32KB */
275 return BFA_STATUS_EINVAL;
276 } else if (area == 0x1) {
277 /* CB 32 KB memory page */
278 if ((offset + (len<<2)) > 0x10000) /* 8k dwords or 32KB */
279 return BFA_STATUS_EINVAL;
281 /* CB register space 64KB */
282 if ((offset + (len<<2)) > BFA_REG_ADDRMSK(ioc))
283 return BFA_STATUS_EINVAL;
285 return BFA_STATUS_OK;
289 bnad_debugfs_read_regrd(struct file *file, char __user *buf,
290 size_t nbytes, loff_t *pos)
292 struct bnad_debug_info *regrd_debug = file->private_data;
293 struct bnad *bnad = (struct bnad *)regrd_debug->i_private;
299 rc = simple_read_from_buffer(buf, nbytes, pos,
300 bnad->regdata, bnad->reglen);
302 if ((*pos + nbytes) >= bnad->reglen) {
303 kfree(bnad->regdata);
304 bnad->regdata = NULL;
312 bnad_debugfs_write_regrd(struct file *file, const char __user *buf,
313 size_t nbytes, loff_t *ppos)
315 struct bnad_debug_info *regrd_debug = file->private_data;
316 struct bnad *bnad = (struct bnad *)regrd_debug->i_private;
317 struct bfa_ioc *ioc = &bnad->bna.ioceth.ioc;
318 int addr, len, rc, i;
320 void __iomem *rb, *reg_addr;
324 /* Allocate memory to store the user space buf */
325 kern_buf = kzalloc(nbytes, GFP_KERNEL);
329 if (copy_from_user(kern_buf, (void __user *)buf, nbytes)) {
334 rc = sscanf(kern_buf, "%x:%x", &addr, &len);
336 pr_warn("bna %s: Failed to read user buffer\n",
337 pci_name(bnad->pcidev));
343 kfree(bnad->regdata);
344 bnad->regdata = NULL;
347 bnad->regdata = kzalloc(len << 2, GFP_KERNEL);
351 bnad->reglen = len << 2;
352 rb = bfa_ioc_bar0(ioc);
353 addr &= BFA_REG_ADDRMSK(ioc);
355 /* offset and len sanity check */
356 rc = bna_reg_offset_check(ioc, addr, len);
358 pr_warn("bna %s: Failed reg offset check\n",
359 pci_name(bnad->pcidev));
360 kfree(bnad->regdata);
361 bnad->regdata = NULL;
366 reg_addr = rb + addr;
367 regbuf = (u32 *)bnad->regdata;
368 spin_lock_irqsave(&bnad->bna_lock, flags);
369 for (i = 0; i < len; i++) {
370 *regbuf = readl(reg_addr);
372 reg_addr += sizeof(u32);
374 spin_unlock_irqrestore(&bnad->bna_lock, flags);
380 bnad_debugfs_write_regwr(struct file *file, const char __user *buf,
381 size_t nbytes, loff_t *ppos)
383 struct bnad_debug_info *debug = file->private_data;
384 struct bnad *bnad = (struct bnad *)debug->i_private;
385 struct bfa_ioc *ioc = &bnad->bna.ioceth.ioc;
387 void __iomem *reg_addr;
391 /* Allocate memory to store the user space buf */
392 kern_buf = kzalloc(nbytes, GFP_KERNEL);
396 if (copy_from_user(kern_buf, (void __user *)buf, nbytes)) {
401 rc = sscanf(kern_buf, "%x:%x", &addr, &val);
403 pr_warn("bna %s: Failed to read user buffer\n",
404 pci_name(bnad->pcidev));
410 addr &= BFA_REG_ADDRMSK(ioc); /* offset only 17 bit and word align */
412 /* offset and len sanity check */
413 rc = bna_reg_offset_check(ioc, addr, 1);
415 pr_warn("bna %s: Failed reg offset check\n",
416 pci_name(bnad->pcidev));
420 reg_addr = (bfa_ioc_bar0(ioc)) + addr;
421 spin_lock_irqsave(&bnad->bna_lock, flags);
422 writel(val, reg_addr);
423 spin_unlock_irqrestore(&bnad->bna_lock, flags);
429 bnad_debugfs_release(struct inode *inode, struct file *file)
431 struct bnad_debug_info *debug = file->private_data;
436 file->private_data = NULL;
442 bnad_debugfs_buffer_release(struct inode *inode, struct file *file)
444 struct bnad_debug_info *debug = file->private_data;
449 kfree(debug->debug_buffer);
451 file->private_data = NULL;
457 static const struct file_operations bnad_debugfs_op_fwtrc = {
458 .owner = THIS_MODULE,
459 .open = bnad_debugfs_open_fwtrc,
460 .llseek = bnad_debugfs_lseek,
461 .read = bnad_debugfs_read,
462 .release = bnad_debugfs_buffer_release,
465 static const struct file_operations bnad_debugfs_op_fwsave = {
466 .owner = THIS_MODULE,
467 .open = bnad_debugfs_open_fwsave,
468 .llseek = bnad_debugfs_lseek,
469 .read = bnad_debugfs_read,
470 .release = bnad_debugfs_buffer_release,
473 static const struct file_operations bnad_debugfs_op_regrd = {
474 .owner = THIS_MODULE,
475 .open = bnad_debugfs_open_reg,
476 .llseek = bnad_debugfs_lseek,
477 .read = bnad_debugfs_read_regrd,
478 .write = bnad_debugfs_write_regrd,
479 .release = bnad_debugfs_release,
482 static const struct file_operations bnad_debugfs_op_regwr = {
483 .owner = THIS_MODULE,
484 .open = bnad_debugfs_open_reg,
485 .llseek = bnad_debugfs_lseek,
486 .write = bnad_debugfs_write_regwr,
487 .release = bnad_debugfs_release,
490 static const struct file_operations bnad_debugfs_op_drvinfo = {
491 .owner = THIS_MODULE,
492 .open = bnad_debugfs_open_drvinfo,
493 .llseek = bnad_debugfs_lseek,
494 .read = bnad_debugfs_read,
495 .release = bnad_debugfs_buffer_release,
498 struct bnad_debugfs_entry {
501 const struct file_operations *fops;
504 static const struct bnad_debugfs_entry bnad_debugfs_files[] = {
505 { "fwtrc", S_IFREG|S_IRUGO, &bnad_debugfs_op_fwtrc, },
506 { "fwsave", S_IFREG|S_IRUGO, &bnad_debugfs_op_fwsave, },
507 { "regrd", S_IFREG|S_IRUGO|S_IWUSR, &bnad_debugfs_op_regrd, },
508 { "regwr", S_IFREG|S_IWUSR, &bnad_debugfs_op_regwr, },
509 { "drvinfo", S_IFREG|S_IRUGO, &bnad_debugfs_op_drvinfo, },
512 static struct dentry *bna_debugfs_root;
513 static atomic_t bna_debugfs_port_count;
515 /* Initialize debugfs interface for BNA */
517 bnad_debugfs_init(struct bnad *bnad)
519 const struct bnad_debugfs_entry *file;
523 /* Setup the BNA debugfs root directory*/
524 if (!bna_debugfs_root) {
525 bna_debugfs_root = debugfs_create_dir("bna", NULL);
526 atomic_set(&bna_debugfs_port_count, 0);
527 if (!bna_debugfs_root) {
528 pr_warn("BNA: debugfs root dir creation failed\n");
533 /* Setup the pci_dev debugfs directory for the port */
534 snprintf(name, sizeof(name), "pci_dev:%s", pci_name(bnad->pcidev));
535 if (!bnad->port_debugfs_root) {
536 bnad->port_debugfs_root =
537 debugfs_create_dir(name, bna_debugfs_root);
538 if (!bnad->port_debugfs_root) {
539 pr_warn("bna pci_dev %s: root dir creation failed\n",
540 pci_name(bnad->pcidev));
544 atomic_inc(&bna_debugfs_port_count);
546 for (i = 0; i < ARRAY_SIZE(bnad_debugfs_files); i++) {
547 file = &bnad_debugfs_files[i];
548 bnad->bnad_dentry_files[i] =
549 debugfs_create_file(file->name,
551 bnad->port_debugfs_root,
554 if (!bnad->bnad_dentry_files[i]) {
556 "BNA pci_dev:%s: create %s entry failed\n",
557 pci_name(bnad->pcidev), file->name);
564 /* Uninitialize debugfs interface for BNA */
566 bnad_debugfs_uninit(struct bnad *bnad)
570 for (i = 0; i < ARRAY_SIZE(bnad_debugfs_files); i++) {
571 if (bnad->bnad_dentry_files[i]) {
572 debugfs_remove(bnad->bnad_dentry_files[i]);
573 bnad->bnad_dentry_files[i] = NULL;
577 /* Remove the pci_dev debugfs directory for the port */
578 if (bnad->port_debugfs_root) {
579 debugfs_remove(bnad->port_debugfs_root);
580 bnad->port_debugfs_root = NULL;
581 atomic_dec(&bna_debugfs_port_count);
584 /* Remove the BNA debugfs root directory */
585 if (atomic_read(&bna_debugfs_port_count) == 0) {
586 debugfs_remove(bna_debugfs_root);
587 bna_debugfs_root = NULL;