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 /* Copy the user space buf */
325 kern_buf = memdup_user(buf, nbytes);
326 if (IS_ERR(kern_buf))
327 return PTR_ERR(kern_buf);
329 rc = sscanf(kern_buf, "%x:%x", &addr, &len);
331 pr_warn("bna %s: Failed to read user buffer\n",
332 pci_name(bnad->pcidev));
338 kfree(bnad->regdata);
341 bnad->regdata = kzalloc(len << 2, GFP_KERNEL);
345 bnad->reglen = len << 2;
346 rb = bfa_ioc_bar0(ioc);
347 addr &= BFA_REG_ADDRMSK(ioc);
349 /* offset and len sanity check */
350 rc = bna_reg_offset_check(ioc, addr, len);
352 pr_warn("bna %s: Failed reg offset check\n",
353 pci_name(bnad->pcidev));
354 kfree(bnad->regdata);
355 bnad->regdata = NULL;
360 reg_addr = rb + addr;
361 regbuf = (u32 *)bnad->regdata;
362 spin_lock_irqsave(&bnad->bna_lock, flags);
363 for (i = 0; i < len; i++) {
364 *regbuf = readl(reg_addr);
366 reg_addr += sizeof(u32);
368 spin_unlock_irqrestore(&bnad->bna_lock, flags);
374 bnad_debugfs_write_regwr(struct file *file, const char __user *buf,
375 size_t nbytes, loff_t *ppos)
377 struct bnad_debug_info *debug = file->private_data;
378 struct bnad *bnad = (struct bnad *)debug->i_private;
379 struct bfa_ioc *ioc = &bnad->bna.ioceth.ioc;
381 void __iomem *reg_addr;
385 /* Copy the user space buf */
386 kern_buf = memdup_user(buf, nbytes);
387 if (IS_ERR(kern_buf))
388 return PTR_ERR(kern_buf);
390 rc = sscanf(kern_buf, "%x:%x", &addr, &val);
392 pr_warn("bna %s: Failed to read user buffer\n",
393 pci_name(bnad->pcidev));
399 addr &= BFA_REG_ADDRMSK(ioc); /* offset only 17 bit and word align */
401 /* offset and len sanity check */
402 rc = bna_reg_offset_check(ioc, addr, 1);
404 pr_warn("bna %s: Failed reg offset check\n",
405 pci_name(bnad->pcidev));
409 reg_addr = (bfa_ioc_bar0(ioc)) + addr;
410 spin_lock_irqsave(&bnad->bna_lock, flags);
411 writel(val, reg_addr);
412 spin_unlock_irqrestore(&bnad->bna_lock, flags);
418 bnad_debugfs_release(struct inode *inode, struct file *file)
420 struct bnad_debug_info *debug = file->private_data;
425 file->private_data = NULL;
431 bnad_debugfs_buffer_release(struct inode *inode, struct file *file)
433 struct bnad_debug_info *debug = file->private_data;
438 kfree(debug->debug_buffer);
440 file->private_data = NULL;
446 static const struct file_operations bnad_debugfs_op_fwtrc = {
447 .owner = THIS_MODULE,
448 .open = bnad_debugfs_open_fwtrc,
449 .llseek = bnad_debugfs_lseek,
450 .read = bnad_debugfs_read,
451 .release = bnad_debugfs_buffer_release,
454 static const struct file_operations bnad_debugfs_op_fwsave = {
455 .owner = THIS_MODULE,
456 .open = bnad_debugfs_open_fwsave,
457 .llseek = bnad_debugfs_lseek,
458 .read = bnad_debugfs_read,
459 .release = bnad_debugfs_buffer_release,
462 static const struct file_operations bnad_debugfs_op_regrd = {
463 .owner = THIS_MODULE,
464 .open = bnad_debugfs_open_reg,
465 .llseek = bnad_debugfs_lseek,
466 .read = bnad_debugfs_read_regrd,
467 .write = bnad_debugfs_write_regrd,
468 .release = bnad_debugfs_release,
471 static const struct file_operations bnad_debugfs_op_regwr = {
472 .owner = THIS_MODULE,
473 .open = bnad_debugfs_open_reg,
474 .llseek = bnad_debugfs_lseek,
475 .write = bnad_debugfs_write_regwr,
476 .release = bnad_debugfs_release,
479 static const struct file_operations bnad_debugfs_op_drvinfo = {
480 .owner = THIS_MODULE,
481 .open = bnad_debugfs_open_drvinfo,
482 .llseek = bnad_debugfs_lseek,
483 .read = bnad_debugfs_read,
484 .release = bnad_debugfs_buffer_release,
487 struct bnad_debugfs_entry {
490 const struct file_operations *fops;
493 static const struct bnad_debugfs_entry bnad_debugfs_files[] = {
494 { "fwtrc", S_IFREG|S_IRUGO, &bnad_debugfs_op_fwtrc, },
495 { "fwsave", S_IFREG|S_IRUGO, &bnad_debugfs_op_fwsave, },
496 { "regrd", S_IFREG|S_IRUGO|S_IWUSR, &bnad_debugfs_op_regrd, },
497 { "regwr", S_IFREG|S_IWUSR, &bnad_debugfs_op_regwr, },
498 { "drvinfo", S_IFREG|S_IRUGO, &bnad_debugfs_op_drvinfo, },
501 static struct dentry *bna_debugfs_root;
502 static atomic_t bna_debugfs_port_count;
504 /* Initialize debugfs interface for BNA */
506 bnad_debugfs_init(struct bnad *bnad)
508 const struct bnad_debugfs_entry *file;
512 /* Setup the BNA debugfs root directory*/
513 if (!bna_debugfs_root) {
514 bna_debugfs_root = debugfs_create_dir("bna", NULL);
515 atomic_set(&bna_debugfs_port_count, 0);
516 if (!bna_debugfs_root) {
517 pr_warn("BNA: debugfs root dir creation failed\n");
522 /* Setup the pci_dev debugfs directory for the port */
523 snprintf(name, sizeof(name), "pci_dev:%s", pci_name(bnad->pcidev));
524 if (!bnad->port_debugfs_root) {
525 bnad->port_debugfs_root =
526 debugfs_create_dir(name, bna_debugfs_root);
527 if (!bnad->port_debugfs_root) {
528 pr_warn("bna pci_dev %s: root dir creation failed\n",
529 pci_name(bnad->pcidev));
533 atomic_inc(&bna_debugfs_port_count);
535 for (i = 0; i < ARRAY_SIZE(bnad_debugfs_files); i++) {
536 file = &bnad_debugfs_files[i];
537 bnad->bnad_dentry_files[i] =
538 debugfs_create_file(file->name,
540 bnad->port_debugfs_root,
543 if (!bnad->bnad_dentry_files[i]) {
545 "BNA pci_dev:%s: create %s entry failed\n",
546 pci_name(bnad->pcidev), file->name);
553 /* Uninitialize debugfs interface for BNA */
555 bnad_debugfs_uninit(struct bnad *bnad)
559 for (i = 0; i < ARRAY_SIZE(bnad_debugfs_files); i++) {
560 if (bnad->bnad_dentry_files[i]) {
561 debugfs_remove(bnad->bnad_dentry_files[i]);
562 bnad->bnad_dentry_files[i] = NULL;
566 /* Remove the pci_dev debugfs directory for the port */
567 if (bnad->port_debugfs_root) {
568 debugfs_remove(bnad->port_debugfs_root);
569 bnad->port_debugfs_root = NULL;
570 atomic_dec(&bna_debugfs_port_count);
573 /* Remove the BNA debugfs root directory */
574 if (atomic_read(&bna_debugfs_port_count) == 0) {
575 debugfs_remove(bna_debugfs_root);
576 bna_debugfs_root = NULL;