2 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
6 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License (GPL) Version 2 as
10 * published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
18 #include <linux/debugfs.h>
19 #include <linux/export.h>
25 * BFA 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 * - bfa/pci_dev:<pci_name>
33 * where the pci_name corresponds to the one under /sys/bus/pci/drivers/bfa
35 * Debugging service available per pci_dev:
36 * fwtrc: To collect current firmware trace.
37 * drvtrc: To collect current driver trace
38 * fwsave: To collect last saved fw trace as a result of firmware crash.
39 * regwr: To write one word to chip register
40 * regrd: To read one or more words from chip register.
43 struct bfad_debug_info {
50 bfad_debugfs_open_drvtrc(struct inode *inode, struct file *file)
52 struct bfad_port_s *port = inode->i_private;
53 struct bfad_s *bfad = port->bfad;
54 struct bfad_debug_info *debug;
56 debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
60 debug->debug_buffer = (void *) bfad->trcmod;
61 debug->buffer_len = sizeof(struct bfa_trc_mod_s);
63 file->private_data = debug;
69 bfad_debugfs_open_fwtrc(struct inode *inode, struct file *file)
71 struct bfad_port_s *port = inode->i_private;
72 struct bfad_s *bfad = port->bfad;
73 struct bfad_debug_info *fw_debug;
77 fw_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
81 fw_debug->buffer_len = sizeof(struct bfa_trc_mod_s);
83 fw_debug->debug_buffer = vmalloc(fw_debug->buffer_len);
84 if (!fw_debug->debug_buffer) {
86 printk(KERN_INFO "bfad[%d]: Failed to allocate fwtrc buffer\n",
91 memset(fw_debug->debug_buffer, 0, fw_debug->buffer_len);
93 spin_lock_irqsave(&bfad->bfad_lock, flags);
94 rc = bfa_ioc_debug_fwtrc(&bfad->bfa.ioc,
95 fw_debug->debug_buffer,
96 &fw_debug->buffer_len);
97 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
98 if (rc != BFA_STATUS_OK) {
99 vfree(fw_debug->debug_buffer);
100 fw_debug->debug_buffer = NULL;
102 printk(KERN_INFO "bfad[%d]: Failed to collect fwtrc\n",
107 file->private_data = fw_debug;
113 bfad_debugfs_open_fwsave(struct inode *inode, struct file *file)
115 struct bfad_port_s *port = inode->i_private;
116 struct bfad_s *bfad = port->bfad;
117 struct bfad_debug_info *fw_debug;
121 fw_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
125 fw_debug->buffer_len = sizeof(struct bfa_trc_mod_s);
127 fw_debug->debug_buffer = vmalloc(fw_debug->buffer_len);
128 if (!fw_debug->debug_buffer) {
130 printk(KERN_INFO "bfad[%d]: Failed to allocate fwsave buffer\n",
135 memset(fw_debug->debug_buffer, 0, fw_debug->buffer_len);
137 spin_lock_irqsave(&bfad->bfad_lock, flags);
138 rc = bfa_ioc_debug_fwsave(&bfad->bfa.ioc,
139 fw_debug->debug_buffer,
140 &fw_debug->buffer_len);
141 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
142 if (rc != BFA_STATUS_OK) {
143 vfree(fw_debug->debug_buffer);
144 fw_debug->debug_buffer = NULL;
146 printk(KERN_INFO "bfad[%d]: Failed to collect fwsave\n",
151 file->private_data = fw_debug;
157 bfad_debugfs_open_reg(struct inode *inode, struct file *file)
159 struct bfad_debug_info *reg_debug;
161 reg_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
165 reg_debug->i_private = inode->i_private;
167 file->private_data = reg_debug;
172 /* Changes the current file position */
174 bfad_debugfs_lseek(struct file *file, loff_t offset, int orig)
176 struct bfad_debug_info *debug;
177 loff_t pos = file->f_pos;
179 debug = file->private_data;
183 file->f_pos = offset;
186 file->f_pos += offset;
189 file->f_pos = debug->buffer_len - offset;
195 if (file->f_pos < 0 || file->f_pos > debug->buffer_len) {
204 bfad_debugfs_read(struct file *file, char __user *buf,
205 size_t nbytes, loff_t *pos)
207 struct bfad_debug_info *debug = file->private_data;
209 if (!debug || !debug->debug_buffer)
212 return simple_read_from_buffer(buf, nbytes, pos,
213 debug->debug_buffer, debug->buffer_len);
216 #define BFA_REG_CT_ADDRSZ (0x40000)
217 #define BFA_REG_CB_ADDRSZ (0x20000)
218 #define BFA_REG_ADDRSZ(__ioc) \
219 ((u32)(bfa_asic_id_ctc(bfa_ioc_devid(__ioc)) ? \
220 BFA_REG_CT_ADDRSZ : BFA_REG_CB_ADDRSZ))
221 #define BFA_REG_ADDRMSK(__ioc) (BFA_REG_ADDRSZ(__ioc) - 1)
224 bfad_reg_offset_check(struct bfa_s *bfa, u32 offset, u32 len)
229 area = (offset >> 15) & 0x7;
231 /* PCIe core register */
232 if ((offset + (len<<2)) > 0x8000) /* 8k dwords or 32KB */
233 return BFA_STATUS_EINVAL;
234 } else if (area == 0x1) {
235 /* CB 32 KB memory page */
236 if ((offset + (len<<2)) > 0x10000) /* 8k dwords or 32KB */
237 return BFA_STATUS_EINVAL;
239 /* CB register space 64KB */
240 if ((offset + (len<<2)) > BFA_REG_ADDRMSK(&bfa->ioc))
241 return BFA_STATUS_EINVAL;
243 return BFA_STATUS_OK;
247 bfad_debugfs_read_regrd(struct file *file, char __user *buf,
248 size_t nbytes, loff_t *pos)
250 struct bfad_debug_info *regrd_debug = file->private_data;
251 struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private;
252 struct bfad_s *bfad = port->bfad;
258 rc = simple_read_from_buffer(buf, nbytes, pos,
259 bfad->regdata, bfad->reglen);
261 if ((*pos + nbytes) >= bfad->reglen) {
262 kfree(bfad->regdata);
263 bfad->regdata = NULL;
271 bfad_debugfs_write_regrd(struct file *file, const char __user *buf,
272 size_t nbytes, loff_t *ppos)
274 struct bfad_debug_info *regrd_debug = file->private_data;
275 struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private;
276 struct bfad_s *bfad = port->bfad;
277 struct bfa_s *bfa = &bfad->bfa;
278 struct bfa_ioc_s *ioc = &bfa->ioc;
282 void __iomem *rb, *reg_addr;
286 kern_buf = kzalloc(nbytes, GFP_KERNEL);
289 printk(KERN_INFO "bfad[%d]: Failed to allocate buffer\n",
294 if (copy_from_user(kern_buf, (void __user *)buf, nbytes)) {
299 rc = sscanf(kern_buf, "%x:%x", &addr, &len);
300 if (rc < 2 || len > (UINT_MAX >> 2)) {
302 "bfad[%d]: %s failed to read user buf\n",
303 bfad->inst_no, __func__);
309 kfree(bfad->regdata);
310 bfad->regdata = NULL;
313 bfad->regdata = kzalloc(len << 2, GFP_KERNEL);
314 if (!bfad->regdata) {
315 printk(KERN_INFO "bfad[%d]: Failed to allocate regrd buffer\n",
320 bfad->reglen = len << 2;
321 rb = bfa_ioc_bar0(ioc);
322 addr &= BFA_REG_ADDRMSK(ioc);
324 /* offset and len sanity check */
325 rc = bfad_reg_offset_check(bfa, addr, len);
327 printk(KERN_INFO "bfad[%d]: Failed reg offset check\n",
329 kfree(bfad->regdata);
330 bfad->regdata = NULL;
335 reg_addr = rb + addr;
336 regbuf = (u32 *)bfad->regdata;
337 spin_lock_irqsave(&bfad->bfad_lock, flags);
338 for (i = 0; i < len; i++) {
339 *regbuf = readl(reg_addr);
341 reg_addr += sizeof(u32);
343 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
349 bfad_debugfs_write_regwr(struct file *file, const char __user *buf,
350 size_t nbytes, loff_t *ppos)
352 struct bfad_debug_info *debug = file->private_data;
353 struct bfad_port_s *port = (struct bfad_port_s *)debug->i_private;
354 struct bfad_s *bfad = port->bfad;
355 struct bfa_s *bfa = &bfad->bfa;
356 struct bfa_ioc_s *ioc = &bfa->ioc;
358 void __iomem *reg_addr;
362 kern_buf = kzalloc(nbytes, GFP_KERNEL);
365 printk(KERN_INFO "bfad[%d]: Failed to allocate buffer\n",
370 if (copy_from_user(kern_buf, (void __user *)buf, nbytes)) {
375 rc = sscanf(kern_buf, "%x:%x", &addr, &val);
378 "bfad[%d]: %s failed to read user buf\n",
379 bfad->inst_no, __func__);
385 addr &= BFA_REG_ADDRMSK(ioc); /* offset only 17 bit and word align */
387 /* offset and len sanity check */
388 rc = bfad_reg_offset_check(bfa, addr, 1);
391 "bfad[%d]: Failed reg offset check\n",
396 reg_addr = (bfa_ioc_bar0(ioc)) + addr;
397 spin_lock_irqsave(&bfad->bfad_lock, flags);
398 writel(val, reg_addr);
399 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
405 bfad_debugfs_release(struct inode *inode, struct file *file)
407 struct bfad_debug_info *debug = file->private_data;
412 file->private_data = NULL;
418 bfad_debugfs_release_fwtrc(struct inode *inode, struct file *file)
420 struct bfad_debug_info *fw_debug = file->private_data;
425 if (fw_debug->debug_buffer)
426 vfree(fw_debug->debug_buffer);
428 file->private_data = NULL;
433 static const struct file_operations bfad_debugfs_op_drvtrc = {
434 .owner = THIS_MODULE,
435 .open = bfad_debugfs_open_drvtrc,
436 .llseek = bfad_debugfs_lseek,
437 .read = bfad_debugfs_read,
438 .release = bfad_debugfs_release,
441 static const struct file_operations bfad_debugfs_op_fwtrc = {
442 .owner = THIS_MODULE,
443 .open = bfad_debugfs_open_fwtrc,
444 .llseek = bfad_debugfs_lseek,
445 .read = bfad_debugfs_read,
446 .release = bfad_debugfs_release_fwtrc,
449 static const struct file_operations bfad_debugfs_op_fwsave = {
450 .owner = THIS_MODULE,
451 .open = bfad_debugfs_open_fwsave,
452 .llseek = bfad_debugfs_lseek,
453 .read = bfad_debugfs_read,
454 .release = bfad_debugfs_release_fwtrc,
457 static const struct file_operations bfad_debugfs_op_regrd = {
458 .owner = THIS_MODULE,
459 .open = bfad_debugfs_open_reg,
460 .llseek = bfad_debugfs_lseek,
461 .read = bfad_debugfs_read_regrd,
462 .write = bfad_debugfs_write_regrd,
463 .release = bfad_debugfs_release,
466 static const struct file_operations bfad_debugfs_op_regwr = {
467 .owner = THIS_MODULE,
468 .open = bfad_debugfs_open_reg,
469 .llseek = bfad_debugfs_lseek,
470 .write = bfad_debugfs_write_regwr,
471 .release = bfad_debugfs_release,
474 struct bfad_debugfs_entry {
477 const struct file_operations *fops;
480 static const struct bfad_debugfs_entry bfad_debugfs_files[] = {
481 { "drvtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_drvtrc, },
482 { "fwtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwtrc, },
483 { "fwsave", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwsave, },
484 { "regrd", S_IFREG|S_IRUGO|S_IWUSR, &bfad_debugfs_op_regrd, },
485 { "regwr", S_IFREG|S_IWUSR, &bfad_debugfs_op_regwr, },
488 static struct dentry *bfa_debugfs_root;
489 static atomic_t bfa_debugfs_port_count;
492 bfad_debugfs_init(struct bfad_port_s *port)
494 struct bfad_s *bfad = port->bfad;
495 const struct bfad_debugfs_entry *file;
499 if (!bfa_debugfs_enable)
502 /* Setup the BFA debugfs root directory*/
503 if (!bfa_debugfs_root) {
504 bfa_debugfs_root = debugfs_create_dir("bfa", NULL);
505 atomic_set(&bfa_debugfs_port_count, 0);
506 if (!bfa_debugfs_root) {
508 "BFA debugfs root dir creation failed\n");
513 /* Setup the pci_dev debugfs directory for the port */
514 snprintf(name, sizeof(name), "pci_dev:%s", bfad->pci_name);
515 if (!port->port_debugfs_root) {
516 port->port_debugfs_root =
517 debugfs_create_dir(name, bfa_debugfs_root);
518 if (!port->port_debugfs_root) {
520 "bfa %s: debugfs root creation failed\n",
525 atomic_inc(&bfa_debugfs_port_count);
527 for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
528 file = &bfad_debugfs_files[i];
529 bfad->bfad_dentry_files[i] =
530 debugfs_create_file(file->name,
532 port->port_debugfs_root,
535 if (!bfad->bfad_dentry_files[i]) {
537 "bfa %s: debugfs %s creation failed\n",
538 bfad->pci_name, file->name);
549 bfad_debugfs_exit(struct bfad_port_s *port)
551 struct bfad_s *bfad = port->bfad;
554 for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
555 if (bfad->bfad_dentry_files[i]) {
556 debugfs_remove(bfad->bfad_dentry_files[i]);
557 bfad->bfad_dentry_files[i] = NULL;
562 * Remove the pci_dev debugfs directory for the port */
563 if (port->port_debugfs_root) {
564 debugfs_remove(port->port_debugfs_root);
565 port->port_debugfs_root = NULL;
566 atomic_dec(&bfa_debugfs_port_count);
569 /* Remove the BFA debugfs root directory */
570 if (atomic_read(&bfa_debugfs_port_count) == 0) {
571 debugfs_remove(bfa_debugfs_root);
572 bfa_debugfs_root = NULL;