2 * Linux network driver for Brocade 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-2011 Brocade Communications Systems, Inc.
19 #include <linux/debugfs.h>
20 #include <linux/module.h>
24 * BNA debufs interface
26 * To access the interface, debugfs file system should be mounted
27 * if not already mounted using:
28 * mount -t debugfs none /sys/kernel/debug
31 * - bna/pci_dev:<pci_name>
32 * where the pci_name corresponds to the one under /sys/bus/pci/drivers/bna
34 * Debugging service available per pci_dev:
35 * fwtrc: To collect current firmware trace.
36 * fwsave: To collect last saved fw trace as a result of firmware crash.
37 * regwr: To write one word to chip register
38 * regrd: To read one or more words from chip register.
41 struct bnad_debug_info
{
48 bnad_debugfs_open_fwtrc(struct inode
*inode
, struct file
*file
)
50 struct bnad
*bnad
= inode
->i_private
;
51 struct bnad_debug_info
*fw_debug
;
55 fw_debug
= kzalloc(sizeof(struct bnad_debug_info
), GFP_KERNEL
);
59 fw_debug
->buffer_len
= BNA_DBG_FWTRC_LEN
;
61 fw_debug
->debug_buffer
= kzalloc(fw_debug
->buffer_len
, GFP_KERNEL
);
62 if (!fw_debug
->debug_buffer
) {
68 spin_lock_irqsave(&bnad
->bna_lock
, flags
);
69 rc
= bfa_nw_ioc_debug_fwtrc(&bnad
->bna
.ioceth
.ioc
,
70 fw_debug
->debug_buffer
,
71 &fw_debug
->buffer_len
);
72 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
73 if (rc
!= BFA_STATUS_OK
) {
74 kfree(fw_debug
->debug_buffer
);
75 fw_debug
->debug_buffer
= NULL
;
78 pr_warn("bnad %s: Failed to collect fwtrc\n",
79 pci_name(bnad
->pcidev
));
83 file
->private_data
= fw_debug
;
89 bnad_debugfs_open_fwsave(struct inode
*inode
, struct file
*file
)
91 struct bnad
*bnad
= inode
->i_private
;
92 struct bnad_debug_info
*fw_debug
;
96 fw_debug
= kzalloc(sizeof(struct bnad_debug_info
), GFP_KERNEL
);
100 fw_debug
->buffer_len
= BNA_DBG_FWTRC_LEN
;
102 fw_debug
->debug_buffer
= kzalloc(fw_debug
->buffer_len
, GFP_KERNEL
);
103 if (!fw_debug
->debug_buffer
) {
109 spin_lock_irqsave(&bnad
->bna_lock
, flags
);
110 rc
= bfa_nw_ioc_debug_fwsave(&bnad
->bna
.ioceth
.ioc
,
111 fw_debug
->debug_buffer
,
112 &fw_debug
->buffer_len
);
113 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
114 if (rc
!= BFA_STATUS_OK
&& rc
!= BFA_STATUS_ENOFSAVE
) {
115 kfree(fw_debug
->debug_buffer
);
116 fw_debug
->debug_buffer
= NULL
;
119 pr_warn("bna %s: Failed to collect fwsave\n",
120 pci_name(bnad
->pcidev
));
124 file
->private_data
= fw_debug
;
130 bnad_debugfs_open_reg(struct inode
*inode
, struct file
*file
)
132 struct bnad_debug_info
*reg_debug
;
134 reg_debug
= kzalloc(sizeof(struct bnad_debug_info
), GFP_KERNEL
);
138 reg_debug
->i_private
= inode
->i_private
;
140 file
->private_data
= reg_debug
;
146 bnad_get_debug_drvinfo(struct bnad
*bnad
, void *buffer
, u32 len
)
148 struct bnad_drvinfo
*drvinfo
= (struct bnad_drvinfo
*) buffer
;
149 struct bnad_iocmd_comp fcomp
;
150 unsigned long flags
= 0;
151 int ret
= BFA_STATUS_FAILED
;
154 spin_lock_irqsave(&bnad
->bna_lock
, flags
);
155 bfa_nw_ioc_get_attr(&bnad
->bna
.ioceth
.ioc
, &drvinfo
->ioc_attr
);
156 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
158 /* Retrieve CEE related info */
160 fcomp
.comp_status
= 0;
161 init_completion(&fcomp
.comp
);
162 spin_lock_irqsave(&bnad
->bna_lock
, flags
);
163 ret
= bfa_nw_cee_get_attr(&bnad
->bna
.cee
, &drvinfo
->cee_attr
,
164 bnad_cb_completion
, &fcomp
);
165 if (ret
!= BFA_STATUS_OK
) {
166 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
169 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
170 wait_for_completion(&fcomp
.comp
);
171 drvinfo
->cee_status
= fcomp
.comp_status
;
173 /* Retrieve flash partition info */
174 fcomp
.comp_status
= 0;
175 init_completion(&fcomp
.comp
);
176 spin_lock_irqsave(&bnad
->bna_lock
, flags
);
177 ret
= bfa_nw_flash_get_attr(&bnad
->bna
.flash
, &drvinfo
->flash_attr
,
178 bnad_cb_completion
, &fcomp
);
179 if (ret
!= BFA_STATUS_OK
) {
180 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
183 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
184 wait_for_completion(&fcomp
.comp
);
185 drvinfo
->flash_status
= fcomp
.comp_status
;
191 bnad_debugfs_open_drvinfo(struct inode
*inode
, struct file
*file
)
193 struct bnad
*bnad
= inode
->i_private
;
194 struct bnad_debug_info
*drv_info
;
197 drv_info
= kzalloc(sizeof(struct bnad_debug_info
), GFP_KERNEL
);
201 drv_info
->buffer_len
= sizeof(struct bnad_drvinfo
);
203 drv_info
->debug_buffer
= kzalloc(drv_info
->buffer_len
, GFP_KERNEL
);
204 if (!drv_info
->debug_buffer
) {
210 mutex_lock(&bnad
->conf_mutex
);
211 rc
= bnad_get_debug_drvinfo(bnad
, drv_info
->debug_buffer
,
212 drv_info
->buffer_len
);
213 mutex_unlock(&bnad
->conf_mutex
);
214 if (rc
!= BFA_STATUS_OK
) {
215 kfree(drv_info
->debug_buffer
);
216 drv_info
->debug_buffer
= NULL
;
219 pr_warn("bna %s: Failed to collect drvinfo\n",
220 pci_name(bnad
->pcidev
));
224 file
->private_data
= drv_info
;
229 /* Changes the current file position */
231 bnad_debugfs_lseek(struct file
*file
, loff_t offset
, int orig
)
233 loff_t pos
= file
->f_pos
;
234 struct bnad_debug_info
*debug
= file
->private_data
;
241 file
->f_pos
= offset
;
244 file
->f_pos
+= offset
;
247 file
->f_pos
= debug
->buffer_len
- offset
;
253 if (file
->f_pos
< 0 || file
->f_pos
> debug
->buffer_len
) {
262 bnad_debugfs_read(struct file
*file
, char __user
*buf
,
263 size_t nbytes
, loff_t
*pos
)
265 struct bnad_debug_info
*debug
= file
->private_data
;
267 if (!debug
|| !debug
->debug_buffer
)
270 return simple_read_from_buffer(buf
, nbytes
, pos
,
271 debug
->debug_buffer
, debug
->buffer_len
);
274 #define BFA_REG_CT_ADDRSZ (0x40000)
275 #define BFA_REG_CB_ADDRSZ (0x20000)
276 #define BFA_REG_ADDRSZ(__ioc) \
277 ((u32)(bfa_asic_id_ctc(bfa_ioc_devid(__ioc)) ? \
278 BFA_REG_CT_ADDRSZ : BFA_REG_CB_ADDRSZ))
279 #define BFA_REG_ADDRMSK(__ioc) (BFA_REG_ADDRSZ(__ioc) - 1)
282 * Function to check if the register offset passed is valid.
285 bna_reg_offset_check(struct bfa_ioc
*ioc
, u32 offset
, u32 len
)
290 area
= (offset
>> 15) & 0x7;
292 /* PCIe core register */
293 if ((offset
+ (len
<<2)) > 0x8000) /* 8k dwords or 32KB */
294 return BFA_STATUS_EINVAL
;
295 } else if (area
== 0x1) {
296 /* CB 32 KB memory page */
297 if ((offset
+ (len
<<2)) > 0x10000) /* 8k dwords or 32KB */
298 return BFA_STATUS_EINVAL
;
300 /* CB register space 64KB */
301 if ((offset
+ (len
<<2)) > BFA_REG_ADDRMSK(ioc
))
302 return BFA_STATUS_EINVAL
;
304 return BFA_STATUS_OK
;
308 bnad_debugfs_read_regrd(struct file
*file
, char __user
*buf
,
309 size_t nbytes
, loff_t
*pos
)
311 struct bnad_debug_info
*regrd_debug
= file
->private_data
;
312 struct bnad
*bnad
= (struct bnad
*)regrd_debug
->i_private
;
318 rc
= simple_read_from_buffer(buf
, nbytes
, pos
,
319 bnad
->regdata
, bnad
->reglen
);
321 if ((*pos
+ nbytes
) >= bnad
->reglen
) {
322 kfree(bnad
->regdata
);
323 bnad
->regdata
= NULL
;
331 bnad_debugfs_write_regrd(struct file
*file
, const char __user
*buf
,
332 size_t nbytes
, loff_t
*ppos
)
334 struct bnad_debug_info
*regrd_debug
= file
->private_data
;
335 struct bnad
*bnad
= (struct bnad
*)regrd_debug
->i_private
;
336 struct bfa_ioc
*ioc
= &bnad
->bna
.ioceth
.ioc
;
337 int addr
, len
, rc
, i
;
339 void __iomem
*rb
, *reg_addr
;
343 /* Allocate memory to store the user space buf */
344 kern_buf
= kzalloc(nbytes
, GFP_KERNEL
);
348 if (copy_from_user(kern_buf
, (void __user
*)buf
, nbytes
)) {
353 rc
= sscanf(kern_buf
, "%x:%x", &addr
, &len
);
355 pr_warn("bna %s: Failed to read user buffer\n",
356 pci_name(bnad
->pcidev
));
362 kfree(bnad
->regdata
);
363 bnad
->regdata
= NULL
;
366 bnad
->regdata
= kzalloc(len
<< 2, GFP_KERNEL
);
370 bnad
->reglen
= len
<< 2;
371 rb
= bfa_ioc_bar0(ioc
);
372 addr
&= BFA_REG_ADDRMSK(ioc
);
374 /* offset and len sanity check */
375 rc
= bna_reg_offset_check(ioc
, addr
, len
);
377 pr_warn("bna %s: Failed reg offset check\n",
378 pci_name(bnad
->pcidev
));
379 kfree(bnad
->regdata
);
380 bnad
->regdata
= NULL
;
385 reg_addr
= rb
+ addr
;
386 regbuf
= (u32
*)bnad
->regdata
;
387 spin_lock_irqsave(&bnad
->bna_lock
, flags
);
388 for (i
= 0; i
< len
; i
++) {
389 *regbuf
= readl(reg_addr
);
391 reg_addr
+= sizeof(u32
);
393 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
399 bnad_debugfs_write_regwr(struct file
*file
, const char __user
*buf
,
400 size_t nbytes
, loff_t
*ppos
)
402 struct bnad_debug_info
*debug
= file
->private_data
;
403 struct bnad
*bnad
= (struct bnad
*)debug
->i_private
;
404 struct bfa_ioc
*ioc
= &bnad
->bna
.ioceth
.ioc
;
406 void __iomem
*reg_addr
;
410 /* Allocate memory to store the user space buf */
411 kern_buf
= kzalloc(nbytes
, GFP_KERNEL
);
415 if (copy_from_user(kern_buf
, (void __user
*)buf
, nbytes
)) {
420 rc
= sscanf(kern_buf
, "%x:%x", &addr
, &val
);
422 pr_warn("bna %s: Failed to read user buffer\n",
423 pci_name(bnad
->pcidev
));
429 addr
&= BFA_REG_ADDRMSK(ioc
); /* offset only 17 bit and word align */
431 /* offset and len sanity check */
432 rc
= bna_reg_offset_check(ioc
, addr
, 1);
434 pr_warn("bna %s: Failed reg offset check\n",
435 pci_name(bnad
->pcidev
));
439 reg_addr
= (bfa_ioc_bar0(ioc
)) + addr
;
440 spin_lock_irqsave(&bnad
->bna_lock
, flags
);
441 writel(val
, reg_addr
);
442 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
448 bnad_debugfs_release(struct inode
*inode
, struct file
*file
)
450 struct bnad_debug_info
*debug
= file
->private_data
;
455 file
->private_data
= NULL
;
461 bnad_debugfs_buffer_release(struct inode
*inode
, struct file
*file
)
463 struct bnad_debug_info
*debug
= file
->private_data
;
468 kfree(debug
->debug_buffer
);
470 file
->private_data
= NULL
;
476 static const struct file_operations bnad_debugfs_op_fwtrc
= {
477 .owner
= THIS_MODULE
,
478 .open
= bnad_debugfs_open_fwtrc
,
479 .llseek
= bnad_debugfs_lseek
,
480 .read
= bnad_debugfs_read
,
481 .release
= bnad_debugfs_buffer_release
,
484 static const struct file_operations bnad_debugfs_op_fwsave
= {
485 .owner
= THIS_MODULE
,
486 .open
= bnad_debugfs_open_fwsave
,
487 .llseek
= bnad_debugfs_lseek
,
488 .read
= bnad_debugfs_read
,
489 .release
= bnad_debugfs_buffer_release
,
492 static const struct file_operations bnad_debugfs_op_regrd
= {
493 .owner
= THIS_MODULE
,
494 .open
= bnad_debugfs_open_reg
,
495 .llseek
= bnad_debugfs_lseek
,
496 .read
= bnad_debugfs_read_regrd
,
497 .write
= bnad_debugfs_write_regrd
,
498 .release
= bnad_debugfs_release
,
501 static const struct file_operations bnad_debugfs_op_regwr
= {
502 .owner
= THIS_MODULE
,
503 .open
= bnad_debugfs_open_reg
,
504 .llseek
= bnad_debugfs_lseek
,
505 .write
= bnad_debugfs_write_regwr
,
506 .release
= bnad_debugfs_release
,
509 static const struct file_operations bnad_debugfs_op_drvinfo
= {
510 .owner
= THIS_MODULE
,
511 .open
= bnad_debugfs_open_drvinfo
,
512 .llseek
= bnad_debugfs_lseek
,
513 .read
= bnad_debugfs_read
,
514 .release
= bnad_debugfs_buffer_release
,
517 struct bnad_debugfs_entry
{
520 const struct file_operations
*fops
;
523 static const struct bnad_debugfs_entry bnad_debugfs_files
[] = {
524 { "fwtrc", S_IFREG
|S_IRUGO
, &bnad_debugfs_op_fwtrc
, },
525 { "fwsave", S_IFREG
|S_IRUGO
, &bnad_debugfs_op_fwsave
, },
526 { "regrd", S_IFREG
|S_IRUGO
|S_IWUSR
, &bnad_debugfs_op_regrd
, },
527 { "regwr", S_IFREG
|S_IWUSR
, &bnad_debugfs_op_regwr
, },
528 { "drvinfo", S_IFREG
|S_IRUGO
, &bnad_debugfs_op_drvinfo
, },
531 static struct dentry
*bna_debugfs_root
;
532 static atomic_t bna_debugfs_port_count
;
534 /* Initialize debugfs interface for BNA */
536 bnad_debugfs_init(struct bnad
*bnad
)
538 const struct bnad_debugfs_entry
*file
;
542 /* Setup the BNA debugfs root directory*/
543 if (!bna_debugfs_root
) {
544 bna_debugfs_root
= debugfs_create_dir("bna", NULL
);
545 atomic_set(&bna_debugfs_port_count
, 0);
546 if (!bna_debugfs_root
) {
547 pr_warn("BNA: debugfs root dir creation failed\n");
552 /* Setup the pci_dev debugfs directory for the port */
553 snprintf(name
, sizeof(name
), "pci_dev:%s", pci_name(bnad
->pcidev
));
554 if (!bnad
->port_debugfs_root
) {
555 bnad
->port_debugfs_root
=
556 debugfs_create_dir(name
, bna_debugfs_root
);
557 if (!bnad
->port_debugfs_root
) {
558 pr_warn("bna pci_dev %s: root dir creation failed\n",
559 pci_name(bnad
->pcidev
));
563 atomic_inc(&bna_debugfs_port_count
);
565 for (i
= 0; i
< ARRAY_SIZE(bnad_debugfs_files
); i
++) {
566 file
= &bnad_debugfs_files
[i
];
567 bnad
->bnad_dentry_files
[i
] =
568 debugfs_create_file(file
->name
,
570 bnad
->port_debugfs_root
,
573 if (!bnad
->bnad_dentry_files
[i
]) {
575 "BNA pci_dev:%s: create %s entry failed\n",
576 pci_name(bnad
->pcidev
), file
->name
);
583 /* Uninitialize debugfs interface for BNA */
585 bnad_debugfs_uninit(struct bnad
*bnad
)
589 for (i
= 0; i
< ARRAY_SIZE(bnad_debugfs_files
); i
++) {
590 if (bnad
->bnad_dentry_files
[i
]) {
591 debugfs_remove(bnad
->bnad_dentry_files
[i
]);
592 bnad
->bnad_dentry_files
[i
] = NULL
;
596 /* Remove the pci_dev debugfs directory for the port */
597 if (bnad
->port_debugfs_root
) {
598 debugfs_remove(bnad
->port_debugfs_root
);
599 bnad
->port_debugfs_root
= NULL
;
600 atomic_dec(&bna_debugfs_port_count
);
603 /* Remove the BNA debugfs root directory */
604 if (atomic_read(&bna_debugfs_port_count
) == 0) {
605 debugfs_remove(bna_debugfs_root
);
606 bna_debugfs_root
= NULL
;