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 netdev_warn(bnad
->netdev
, "failed to collect fwtrc\n");
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 netdev_warn(bnad
->netdev
, "failed to collect fwsave\n");
123 file
->private_data
= fw_debug
;
129 bnad_debugfs_open_reg(struct inode
*inode
, struct file
*file
)
131 struct bnad_debug_info
*reg_debug
;
133 reg_debug
= kzalloc(sizeof(struct bnad_debug_info
), GFP_KERNEL
);
137 reg_debug
->i_private
= inode
->i_private
;
139 file
->private_data
= reg_debug
;
145 bnad_get_debug_drvinfo(struct bnad
*bnad
, void *buffer
, u32 len
)
147 struct bnad_drvinfo
*drvinfo
= (struct bnad_drvinfo
*) buffer
;
148 struct bnad_iocmd_comp fcomp
;
149 unsigned long flags
= 0;
150 int ret
= BFA_STATUS_FAILED
;
153 spin_lock_irqsave(&bnad
->bna_lock
, flags
);
154 bfa_nw_ioc_get_attr(&bnad
->bna
.ioceth
.ioc
, &drvinfo
->ioc_attr
);
155 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
157 /* Retrieve CEE related info */
159 fcomp
.comp_status
= 0;
160 init_completion(&fcomp
.comp
);
161 spin_lock_irqsave(&bnad
->bna_lock
, flags
);
162 ret
= bfa_nw_cee_get_attr(&bnad
->bna
.cee
, &drvinfo
->cee_attr
,
163 bnad_cb_completion
, &fcomp
);
164 if (ret
!= BFA_STATUS_OK
) {
165 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
168 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
169 wait_for_completion(&fcomp
.comp
);
170 drvinfo
->cee_status
= fcomp
.comp_status
;
172 /* Retrieve flash partition info */
173 fcomp
.comp_status
= 0;
174 reinit_completion(&fcomp
.comp
);
175 spin_lock_irqsave(&bnad
->bna_lock
, flags
);
176 ret
= bfa_nw_flash_get_attr(&bnad
->bna
.flash
, &drvinfo
->flash_attr
,
177 bnad_cb_completion
, &fcomp
);
178 if (ret
!= BFA_STATUS_OK
) {
179 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
182 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
183 wait_for_completion(&fcomp
.comp
);
184 drvinfo
->flash_status
= fcomp
.comp_status
;
190 bnad_debugfs_open_drvinfo(struct inode
*inode
, struct file
*file
)
192 struct bnad
*bnad
= inode
->i_private
;
193 struct bnad_debug_info
*drv_info
;
196 drv_info
= kzalloc(sizeof(struct bnad_debug_info
), GFP_KERNEL
);
200 drv_info
->buffer_len
= sizeof(struct bnad_drvinfo
);
202 drv_info
->debug_buffer
= kzalloc(drv_info
->buffer_len
, GFP_KERNEL
);
203 if (!drv_info
->debug_buffer
) {
209 mutex_lock(&bnad
->conf_mutex
);
210 rc
= bnad_get_debug_drvinfo(bnad
, drv_info
->debug_buffer
,
211 drv_info
->buffer_len
);
212 mutex_unlock(&bnad
->conf_mutex
);
213 if (rc
!= BFA_STATUS_OK
) {
214 kfree(drv_info
->debug_buffer
);
215 drv_info
->debug_buffer
= NULL
;
218 netdev_warn(bnad
->netdev
, "failed to collect drvinfo\n");
222 file
->private_data
= drv_info
;
227 /* Changes the current file position */
229 bnad_debugfs_lseek(struct file
*file
, loff_t offset
, int orig
)
231 struct bnad_debug_info
*debug
= file
->private_data
;
236 return fixed_size_llseek(file
, offset
, orig
, debug
->buffer_len
);
240 bnad_debugfs_read(struct file
*file
, char __user
*buf
,
241 size_t nbytes
, loff_t
*pos
)
243 struct bnad_debug_info
*debug
= file
->private_data
;
245 if (!debug
|| !debug
->debug_buffer
)
248 return simple_read_from_buffer(buf
, nbytes
, pos
,
249 debug
->debug_buffer
, debug
->buffer_len
);
252 #define BFA_REG_CT_ADDRSZ (0x40000)
253 #define BFA_REG_CB_ADDRSZ (0x20000)
254 #define BFA_REG_ADDRSZ(__ioc) \
255 ((u32)(bfa_asic_id_ctc(bfa_ioc_devid(__ioc)) ? \
256 BFA_REG_CT_ADDRSZ : BFA_REG_CB_ADDRSZ))
257 #define BFA_REG_ADDRMSK(__ioc) (BFA_REG_ADDRSZ(__ioc) - 1)
260 * Function to check if the register offset passed is valid.
263 bna_reg_offset_check(struct bfa_ioc
*ioc
, u32 offset
, u32 len
)
268 area
= (offset
>> 15) & 0x7;
270 /* PCIe core register */
271 if (offset
+ (len
<< 2) > 0x8000) /* 8k dwords or 32KB */
272 return BFA_STATUS_EINVAL
;
273 } else if (area
== 0x1) {
274 /* CB 32 KB memory page */
275 if (offset
+ (len
<< 2) > 0x10000) /* 8k dwords or 32KB */
276 return BFA_STATUS_EINVAL
;
278 /* CB register space 64KB */
279 if (offset
+ (len
<< 2) > BFA_REG_ADDRMSK(ioc
))
280 return BFA_STATUS_EINVAL
;
282 return BFA_STATUS_OK
;
286 bnad_debugfs_read_regrd(struct file
*file
, char __user
*buf
,
287 size_t nbytes
, loff_t
*pos
)
289 struct bnad_debug_info
*regrd_debug
= file
->private_data
;
290 struct bnad
*bnad
= (struct bnad
*)regrd_debug
->i_private
;
296 rc
= simple_read_from_buffer(buf
, nbytes
, pos
,
297 bnad
->regdata
, bnad
->reglen
);
299 if ((*pos
+ nbytes
) >= bnad
->reglen
) {
300 kfree(bnad
->regdata
);
301 bnad
->regdata
= NULL
;
309 bnad_debugfs_write_regrd(struct file
*file
, const char __user
*buf
,
310 size_t nbytes
, loff_t
*ppos
)
312 struct bnad_debug_info
*regrd_debug
= file
->private_data
;
313 struct bnad
*bnad
= (struct bnad
*)regrd_debug
->i_private
;
314 struct bfa_ioc
*ioc
= &bnad
->bna
.ioceth
.ioc
;
315 int addr
, len
, rc
, i
;
317 void __iomem
*rb
, *reg_addr
;
321 /* Copy the user space buf */
322 kern_buf
= memdup_user(buf
, nbytes
);
323 if (IS_ERR(kern_buf
))
324 return PTR_ERR(kern_buf
);
326 rc
= sscanf(kern_buf
, "%x:%x", &addr
, &len
);
328 netdev_warn(bnad
->netdev
, "failed to read user buffer\n");
334 kfree(bnad
->regdata
);
337 bnad
->regdata
= kzalloc(len
<< 2, GFP_KERNEL
);
341 bnad
->reglen
= len
<< 2;
342 rb
= bfa_ioc_bar0(ioc
);
343 addr
&= BFA_REG_ADDRMSK(ioc
);
345 /* offset and len sanity check */
346 rc
= bna_reg_offset_check(ioc
, addr
, len
);
348 netdev_warn(bnad
->netdev
, "failed reg offset check\n");
349 kfree(bnad
->regdata
);
350 bnad
->regdata
= NULL
;
355 reg_addr
= rb
+ addr
;
356 regbuf
= (u32
*)bnad
->regdata
;
357 spin_lock_irqsave(&bnad
->bna_lock
, flags
);
358 for (i
= 0; i
< len
; i
++) {
359 *regbuf
= readl(reg_addr
);
361 reg_addr
+= sizeof(u32
);
363 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
369 bnad_debugfs_write_regwr(struct file
*file
, const char __user
*buf
,
370 size_t nbytes
, loff_t
*ppos
)
372 struct bnad_debug_info
*debug
= file
->private_data
;
373 struct bnad
*bnad
= (struct bnad
*)debug
->i_private
;
374 struct bfa_ioc
*ioc
= &bnad
->bna
.ioceth
.ioc
;
376 void __iomem
*reg_addr
;
380 /* Copy the user space buf */
381 kern_buf
= memdup_user(buf
, nbytes
);
382 if (IS_ERR(kern_buf
))
383 return PTR_ERR(kern_buf
);
385 rc
= sscanf(kern_buf
, "%x:%x", &addr
, &val
);
387 netdev_warn(bnad
->netdev
, "failed to read user buffer\n");
393 addr
&= BFA_REG_ADDRMSK(ioc
); /* offset only 17 bit and word align */
395 /* offset and len sanity check */
396 rc
= bna_reg_offset_check(ioc
, addr
, 1);
398 netdev_warn(bnad
->netdev
, "failed reg offset check\n");
402 reg_addr
= (bfa_ioc_bar0(ioc
)) + addr
;
403 spin_lock_irqsave(&bnad
->bna_lock
, flags
);
404 writel(val
, reg_addr
);
405 spin_unlock_irqrestore(&bnad
->bna_lock
, flags
);
411 bnad_debugfs_release(struct inode
*inode
, struct file
*file
)
413 struct bnad_debug_info
*debug
= file
->private_data
;
418 file
->private_data
= NULL
;
424 bnad_debugfs_buffer_release(struct inode
*inode
, struct file
*file
)
426 struct bnad_debug_info
*debug
= file
->private_data
;
431 kfree(debug
->debug_buffer
);
433 file
->private_data
= NULL
;
439 static const struct file_operations bnad_debugfs_op_fwtrc
= {
440 .owner
= THIS_MODULE
,
441 .open
= bnad_debugfs_open_fwtrc
,
442 .llseek
= bnad_debugfs_lseek
,
443 .read
= bnad_debugfs_read
,
444 .release
= bnad_debugfs_buffer_release
,
447 static const struct file_operations bnad_debugfs_op_fwsave
= {
448 .owner
= THIS_MODULE
,
449 .open
= bnad_debugfs_open_fwsave
,
450 .llseek
= bnad_debugfs_lseek
,
451 .read
= bnad_debugfs_read
,
452 .release
= bnad_debugfs_buffer_release
,
455 static const struct file_operations bnad_debugfs_op_regrd
= {
456 .owner
= THIS_MODULE
,
457 .open
= bnad_debugfs_open_reg
,
458 .llseek
= bnad_debugfs_lseek
,
459 .read
= bnad_debugfs_read_regrd
,
460 .write
= bnad_debugfs_write_regrd
,
461 .release
= bnad_debugfs_release
,
464 static const struct file_operations bnad_debugfs_op_regwr
= {
465 .owner
= THIS_MODULE
,
466 .open
= bnad_debugfs_open_reg
,
467 .llseek
= bnad_debugfs_lseek
,
468 .write
= bnad_debugfs_write_regwr
,
469 .release
= bnad_debugfs_release
,
472 static const struct file_operations bnad_debugfs_op_drvinfo
= {
473 .owner
= THIS_MODULE
,
474 .open
= bnad_debugfs_open_drvinfo
,
475 .llseek
= bnad_debugfs_lseek
,
476 .read
= bnad_debugfs_read
,
477 .release
= bnad_debugfs_buffer_release
,
480 struct bnad_debugfs_entry
{
483 const struct file_operations
*fops
;
486 static const struct bnad_debugfs_entry bnad_debugfs_files
[] = {
487 { "fwtrc", S_IFREG
|S_IRUGO
, &bnad_debugfs_op_fwtrc
, },
488 { "fwsave", S_IFREG
|S_IRUGO
, &bnad_debugfs_op_fwsave
, },
489 { "regrd", S_IFREG
|S_IRUGO
|S_IWUSR
, &bnad_debugfs_op_regrd
, },
490 { "regwr", S_IFREG
|S_IWUSR
, &bnad_debugfs_op_regwr
, },
491 { "drvinfo", S_IFREG
|S_IRUGO
, &bnad_debugfs_op_drvinfo
, },
494 static struct dentry
*bna_debugfs_root
;
495 static atomic_t bna_debugfs_port_count
;
497 /* Initialize debugfs interface for BNA */
499 bnad_debugfs_init(struct bnad
*bnad
)
501 const struct bnad_debugfs_entry
*file
;
505 /* Setup the BNA debugfs root directory*/
506 if (!bna_debugfs_root
) {
507 bna_debugfs_root
= debugfs_create_dir("bna", NULL
);
508 atomic_set(&bna_debugfs_port_count
, 0);
509 if (!bna_debugfs_root
) {
510 netdev_warn(bnad
->netdev
,
511 "debugfs root dir creation failed\n");
516 /* Setup the pci_dev debugfs directory for the port */
517 snprintf(name
, sizeof(name
), "pci_dev:%s", pci_name(bnad
->pcidev
));
518 if (!bnad
->port_debugfs_root
) {
519 bnad
->port_debugfs_root
=
520 debugfs_create_dir(name
, bna_debugfs_root
);
521 if (!bnad
->port_debugfs_root
) {
522 netdev_warn(bnad
->netdev
,
523 "debugfs root dir creation failed\n");
527 atomic_inc(&bna_debugfs_port_count
);
529 for (i
= 0; i
< ARRAY_SIZE(bnad_debugfs_files
); i
++) {
530 file
= &bnad_debugfs_files
[i
];
531 bnad
->bnad_dentry_files
[i
] =
532 debugfs_create_file(file
->name
,
534 bnad
->port_debugfs_root
,
537 if (!bnad
->bnad_dentry_files
[i
]) {
538 netdev_warn(bnad
->netdev
,
539 "create %s entry failed\n",
547 /* Uninitialize debugfs interface for BNA */
549 bnad_debugfs_uninit(struct bnad
*bnad
)
553 for (i
= 0; i
< ARRAY_SIZE(bnad_debugfs_files
); i
++) {
554 if (bnad
->bnad_dentry_files
[i
]) {
555 debugfs_remove(bnad
->bnad_dentry_files
[i
]);
556 bnad
->bnad_dentry_files
[i
] = NULL
;
560 /* Remove the pci_dev debugfs directory for the port */
561 if (bnad
->port_debugfs_root
) {
562 debugfs_remove(bnad
->port_debugfs_root
);
563 bnad
->port_debugfs_root
= NULL
;
564 atomic_dec(&bna_debugfs_port_count
);
567 /* Remove the BNA debugfs root directory */
568 if (atomic_read(&bna_debugfs_port_count
) == 0) {
569 debugfs_remove(bna_debugfs_root
);
570 bna_debugfs_root
= NULL
;