1 /* 2 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc. 3 * All rights reserved 4 * www.brocade.com 5 * 6 * Linux driver for Brocade Fibre Channel Host Bus Adapter. 7 * 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 11 * 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. 16 */ 17 18 #include <linux/debugfs.h> 19 #include <linux/export.h> 20 21 #include "bfad_drv.h" 22 #include "bfad_im.h" 23 24 /* 25 * BFA debufs interface 26 * 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 30 * 31 * BFA Hierarchy: 32 * - bfa/pci_dev:<pci_name> 33 * where the pci_name corresponds to the one under /sys/bus/pci/drivers/bfa 34 * 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. 41 */ 42 43 struct bfad_debug_info { 44 char *debug_buffer; 45 void *i_private; 46 int buffer_len; 47 }; 48 49 static int 50 bfad_debugfs_open_drvtrc(struct inode *inode, struct file *file) 51 { 52 struct bfad_port_s *port = inode->i_private; 53 struct bfad_s *bfad = port->bfad; 54 struct bfad_debug_info *debug; 55 56 debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL); 57 if (!debug) 58 return -ENOMEM; 59 60 debug->debug_buffer = (void *) bfad->trcmod; 61 debug->buffer_len = sizeof(struct bfa_trc_mod_s); 62 63 file->private_data = debug; 64 65 return 0; 66 } 67 68 static int 69 bfad_debugfs_open_fwtrc(struct inode *inode, struct file *file) 70 { 71 struct bfad_port_s *port = inode->i_private; 72 struct bfad_s *bfad = port->bfad; 73 struct bfad_debug_info *fw_debug; 74 unsigned long flags; 75 int rc; 76 77 fw_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL); 78 if (!fw_debug) 79 return -ENOMEM; 80 81 fw_debug->buffer_len = sizeof(struct bfa_trc_mod_s); 82 83 fw_debug->debug_buffer = vmalloc(fw_debug->buffer_len); 84 if (!fw_debug->debug_buffer) { 85 kfree(fw_debug); 86 printk(KERN_INFO "bfad[%d]: Failed to allocate fwtrc buffer\n", 87 bfad->inst_no); 88 return -ENOMEM; 89 } 90 91 memset(fw_debug->debug_buffer, 0, fw_debug->buffer_len); 92 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; 101 kfree(fw_debug); 102 printk(KERN_INFO "bfad[%d]: Failed to collect fwtrc\n", 103 bfad->inst_no); 104 return -ENOMEM; 105 } 106 107 file->private_data = fw_debug; 108 109 return 0; 110 } 111 112 static int 113 bfad_debugfs_open_fwsave(struct inode *inode, struct file *file) 114 { 115 struct bfad_port_s *port = inode->i_private; 116 struct bfad_s *bfad = port->bfad; 117 struct bfad_debug_info *fw_debug; 118 unsigned long flags; 119 int rc; 120 121 fw_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL); 122 if (!fw_debug) 123 return -ENOMEM; 124 125 fw_debug->buffer_len = sizeof(struct bfa_trc_mod_s); 126 127 fw_debug->debug_buffer = vmalloc(fw_debug->buffer_len); 128 if (!fw_debug->debug_buffer) { 129 kfree(fw_debug); 130 printk(KERN_INFO "bfad[%d]: Failed to allocate fwsave buffer\n", 131 bfad->inst_no); 132 return -ENOMEM; 133 } 134 135 memset(fw_debug->debug_buffer, 0, fw_debug->buffer_len); 136 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; 145 kfree(fw_debug); 146 printk(KERN_INFO "bfad[%d]: Failed to collect fwsave\n", 147 bfad->inst_no); 148 return -ENOMEM; 149 } 150 151 file->private_data = fw_debug; 152 153 return 0; 154 } 155 156 static int 157 bfad_debugfs_open_reg(struct inode *inode, struct file *file) 158 { 159 struct bfad_debug_info *reg_debug; 160 161 reg_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL); 162 if (!reg_debug) 163 return -ENOMEM; 164 165 reg_debug->i_private = inode->i_private; 166 167 file->private_data = reg_debug; 168 169 return 0; 170 } 171 172 /* Changes the current file position */ 173 static loff_t 174 bfad_debugfs_lseek(struct file *file, loff_t offset, int orig) 175 { 176 struct bfad_debug_info *debug = file->private_data; 177 return fixed_size_llseek(file, offset, orig, 178 debug->buffer_len); 179 } 180 181 static ssize_t 182 bfad_debugfs_read(struct file *file, char __user *buf, 183 size_t nbytes, loff_t *pos) 184 { 185 struct bfad_debug_info *debug = file->private_data; 186 187 if (!debug || !debug->debug_buffer) 188 return 0; 189 190 return simple_read_from_buffer(buf, nbytes, pos, 191 debug->debug_buffer, debug->buffer_len); 192 } 193 194 #define BFA_REG_CT_ADDRSZ (0x40000) 195 #define BFA_REG_CB_ADDRSZ (0x20000) 196 #define BFA_REG_ADDRSZ(__ioc) \ 197 ((u32)(bfa_asic_id_ctc(bfa_ioc_devid(__ioc)) ? \ 198 BFA_REG_CT_ADDRSZ : BFA_REG_CB_ADDRSZ)) 199 #define BFA_REG_ADDRMSK(__ioc) (BFA_REG_ADDRSZ(__ioc) - 1) 200 201 static bfa_status_t 202 bfad_reg_offset_check(struct bfa_s *bfa, u32 offset, u32 len) 203 { 204 u8 area; 205 206 /* check [16:15] */ 207 area = (offset >> 15) & 0x7; 208 if (area == 0) { 209 /* PCIe core register */ 210 if ((offset + (len<<2)) > 0x8000) /* 8k dwords or 32KB */ 211 return BFA_STATUS_EINVAL; 212 } else if (area == 0x1) { 213 /* CB 32 KB memory page */ 214 if ((offset + (len<<2)) > 0x10000) /* 8k dwords or 32KB */ 215 return BFA_STATUS_EINVAL; 216 } else { 217 /* CB register space 64KB */ 218 if ((offset + (len<<2)) > BFA_REG_ADDRMSK(&bfa->ioc)) 219 return BFA_STATUS_EINVAL; 220 } 221 return BFA_STATUS_OK; 222 } 223 224 static ssize_t 225 bfad_debugfs_read_regrd(struct file *file, char __user *buf, 226 size_t nbytes, loff_t *pos) 227 { 228 struct bfad_debug_info *regrd_debug = file->private_data; 229 struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private; 230 struct bfad_s *bfad = port->bfad; 231 ssize_t rc; 232 233 if (!bfad->regdata) 234 return 0; 235 236 rc = simple_read_from_buffer(buf, nbytes, pos, 237 bfad->regdata, bfad->reglen); 238 239 if ((*pos + nbytes) >= bfad->reglen) { 240 kfree(bfad->regdata); 241 bfad->regdata = NULL; 242 bfad->reglen = 0; 243 } 244 245 return rc; 246 } 247 248 static ssize_t 249 bfad_debugfs_write_regrd(struct file *file, const char __user *buf, 250 size_t nbytes, loff_t *ppos) 251 { 252 struct bfad_debug_info *regrd_debug = file->private_data; 253 struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private; 254 struct bfad_s *bfad = port->bfad; 255 struct bfa_s *bfa = &bfad->bfa; 256 struct bfa_ioc_s *ioc = &bfa->ioc; 257 int addr, len, rc, i; 258 u32 *regbuf; 259 void __iomem *rb, *reg_addr; 260 unsigned long flags; 261 void *kern_buf; 262 263 kern_buf = kzalloc(nbytes, GFP_KERNEL); 264 265 if (!kern_buf) { 266 printk(KERN_INFO "bfad[%d]: Failed to allocate buffer\n", 267 bfad->inst_no); 268 return -ENOMEM; 269 } 270 271 if (copy_from_user(kern_buf, (void __user *)buf, nbytes)) { 272 kfree(kern_buf); 273 return -ENOMEM; 274 } 275 276 rc = sscanf(kern_buf, "%x:%x", &addr, &len); 277 if (rc < 2) { 278 printk(KERN_INFO 279 "bfad[%d]: %s failed to read user buf\n", 280 bfad->inst_no, __func__); 281 kfree(kern_buf); 282 return -EINVAL; 283 } 284 285 kfree(kern_buf); 286 kfree(bfad->regdata); 287 bfad->regdata = NULL; 288 bfad->reglen = 0; 289 290 bfad->regdata = kzalloc(len << 2, GFP_KERNEL); 291 if (!bfad->regdata) { 292 printk(KERN_INFO "bfad[%d]: Failed to allocate regrd buffer\n", 293 bfad->inst_no); 294 return -ENOMEM; 295 } 296 297 bfad->reglen = len << 2; 298 rb = bfa_ioc_bar0(ioc); 299 addr &= BFA_REG_ADDRMSK(ioc); 300 301 /* offset and len sanity check */ 302 rc = bfad_reg_offset_check(bfa, addr, len); 303 if (rc) { 304 printk(KERN_INFO "bfad[%d]: Failed reg offset check\n", 305 bfad->inst_no); 306 kfree(bfad->regdata); 307 bfad->regdata = NULL; 308 bfad->reglen = 0; 309 return -EINVAL; 310 } 311 312 reg_addr = rb + addr; 313 regbuf = (u32 *)bfad->regdata; 314 spin_lock_irqsave(&bfad->bfad_lock, flags); 315 for (i = 0; i < len; i++) { 316 *regbuf = readl(reg_addr); 317 regbuf++; 318 reg_addr += sizeof(u32); 319 } 320 spin_unlock_irqrestore(&bfad->bfad_lock, flags); 321 322 return nbytes; 323 } 324 325 static ssize_t 326 bfad_debugfs_write_regwr(struct file *file, const char __user *buf, 327 size_t nbytes, loff_t *ppos) 328 { 329 struct bfad_debug_info *debug = file->private_data; 330 struct bfad_port_s *port = (struct bfad_port_s *)debug->i_private; 331 struct bfad_s *bfad = port->bfad; 332 struct bfa_s *bfa = &bfad->bfa; 333 struct bfa_ioc_s *ioc = &bfa->ioc; 334 int addr, val, rc; 335 void __iomem *reg_addr; 336 unsigned long flags; 337 void *kern_buf; 338 339 kern_buf = kzalloc(nbytes, GFP_KERNEL); 340 341 if (!kern_buf) { 342 printk(KERN_INFO "bfad[%d]: Failed to allocate buffer\n", 343 bfad->inst_no); 344 return -ENOMEM; 345 } 346 347 if (copy_from_user(kern_buf, (void __user *)buf, nbytes)) { 348 kfree(kern_buf); 349 return -ENOMEM; 350 } 351 352 rc = sscanf(kern_buf, "%x:%x", &addr, &val); 353 if (rc < 2) { 354 printk(KERN_INFO 355 "bfad[%d]: %s failed to read user buf\n", 356 bfad->inst_no, __func__); 357 kfree(kern_buf); 358 return -EINVAL; 359 } 360 kfree(kern_buf); 361 362 addr &= BFA_REG_ADDRMSK(ioc); /* offset only 17 bit and word align */ 363 364 /* offset and len sanity check */ 365 rc = bfad_reg_offset_check(bfa, addr, 1); 366 if (rc) { 367 printk(KERN_INFO 368 "bfad[%d]: Failed reg offset check\n", 369 bfad->inst_no); 370 return -EINVAL; 371 } 372 373 reg_addr = (bfa_ioc_bar0(ioc)) + addr; 374 spin_lock_irqsave(&bfad->bfad_lock, flags); 375 writel(val, reg_addr); 376 spin_unlock_irqrestore(&bfad->bfad_lock, flags); 377 378 return nbytes; 379 } 380 381 static int 382 bfad_debugfs_release(struct inode *inode, struct file *file) 383 { 384 struct bfad_debug_info *debug = file->private_data; 385 386 if (!debug) 387 return 0; 388 389 file->private_data = NULL; 390 kfree(debug); 391 return 0; 392 } 393 394 static int 395 bfad_debugfs_release_fwtrc(struct inode *inode, struct file *file) 396 { 397 struct bfad_debug_info *fw_debug = file->private_data; 398 399 if (!fw_debug) 400 return 0; 401 402 if (fw_debug->debug_buffer) 403 vfree(fw_debug->debug_buffer); 404 405 file->private_data = NULL; 406 kfree(fw_debug); 407 return 0; 408 } 409 410 static const struct file_operations bfad_debugfs_op_drvtrc = { 411 .owner = THIS_MODULE, 412 .open = bfad_debugfs_open_drvtrc, 413 .llseek = bfad_debugfs_lseek, 414 .read = bfad_debugfs_read, 415 .release = bfad_debugfs_release, 416 }; 417 418 static const struct file_operations bfad_debugfs_op_fwtrc = { 419 .owner = THIS_MODULE, 420 .open = bfad_debugfs_open_fwtrc, 421 .llseek = bfad_debugfs_lseek, 422 .read = bfad_debugfs_read, 423 .release = bfad_debugfs_release_fwtrc, 424 }; 425 426 static const struct file_operations bfad_debugfs_op_fwsave = { 427 .owner = THIS_MODULE, 428 .open = bfad_debugfs_open_fwsave, 429 .llseek = bfad_debugfs_lseek, 430 .read = bfad_debugfs_read, 431 .release = bfad_debugfs_release_fwtrc, 432 }; 433 434 static const struct file_operations bfad_debugfs_op_regrd = { 435 .owner = THIS_MODULE, 436 .open = bfad_debugfs_open_reg, 437 .llseek = bfad_debugfs_lseek, 438 .read = bfad_debugfs_read_regrd, 439 .write = bfad_debugfs_write_regrd, 440 .release = bfad_debugfs_release, 441 }; 442 443 static const struct file_operations bfad_debugfs_op_regwr = { 444 .owner = THIS_MODULE, 445 .open = bfad_debugfs_open_reg, 446 .llseek = bfad_debugfs_lseek, 447 .write = bfad_debugfs_write_regwr, 448 .release = bfad_debugfs_release, 449 }; 450 451 struct bfad_debugfs_entry { 452 const char *name; 453 umode_t mode; 454 const struct file_operations *fops; 455 }; 456 457 static const struct bfad_debugfs_entry bfad_debugfs_files[] = { 458 { "drvtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_drvtrc, }, 459 { "fwtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwtrc, }, 460 { "fwsave", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwsave, }, 461 { "regrd", S_IFREG|S_IRUGO|S_IWUSR, &bfad_debugfs_op_regrd, }, 462 { "regwr", S_IFREG|S_IWUSR, &bfad_debugfs_op_regwr, }, 463 }; 464 465 static struct dentry *bfa_debugfs_root; 466 static atomic_t bfa_debugfs_port_count; 467 468 inline void 469 bfad_debugfs_init(struct bfad_port_s *port) 470 { 471 struct bfad_s *bfad = port->bfad; 472 const struct bfad_debugfs_entry *file; 473 char name[64]; 474 int i; 475 476 if (!bfa_debugfs_enable) 477 return; 478 479 /* Setup the BFA debugfs root directory*/ 480 if (!bfa_debugfs_root) { 481 bfa_debugfs_root = debugfs_create_dir("bfa", NULL); 482 atomic_set(&bfa_debugfs_port_count, 0); 483 if (!bfa_debugfs_root) { 484 printk(KERN_WARNING 485 "BFA debugfs root dir creation failed\n"); 486 goto err; 487 } 488 } 489 490 /* Setup the pci_dev debugfs directory for the port */ 491 snprintf(name, sizeof(name), "pci_dev:%s", bfad->pci_name); 492 if (!port->port_debugfs_root) { 493 port->port_debugfs_root = 494 debugfs_create_dir(name, bfa_debugfs_root); 495 if (!port->port_debugfs_root) { 496 printk(KERN_WARNING 497 "bfa %s: debugfs root creation failed\n", 498 bfad->pci_name); 499 goto err; 500 } 501 502 atomic_inc(&bfa_debugfs_port_count); 503 504 for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) { 505 file = &bfad_debugfs_files[i]; 506 bfad->bfad_dentry_files[i] = 507 debugfs_create_file(file->name, 508 file->mode, 509 port->port_debugfs_root, 510 port, 511 file->fops); 512 if (!bfad->bfad_dentry_files[i]) { 513 printk(KERN_WARNING 514 "bfa %s: debugfs %s creation failed\n", 515 bfad->pci_name, file->name); 516 goto err; 517 } 518 } 519 } 520 521 err: 522 return; 523 } 524 525 inline void 526 bfad_debugfs_exit(struct bfad_port_s *port) 527 { 528 struct bfad_s *bfad = port->bfad; 529 int i; 530 531 for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) { 532 if (bfad->bfad_dentry_files[i]) { 533 debugfs_remove(bfad->bfad_dentry_files[i]); 534 bfad->bfad_dentry_files[i] = NULL; 535 } 536 } 537 538 /* Remove the pci_dev debugfs directory for the port */ 539 if (port->port_debugfs_root) { 540 debugfs_remove(port->port_debugfs_root); 541 port->port_debugfs_root = NULL; 542 atomic_dec(&bfa_debugfs_port_count); 543 } 544 545 /* Remove the BFA debugfs root directory */ 546 if (atomic_read(&bfa_debugfs_port_count) == 0) { 547 debugfs_remove(bfa_debugfs_root); 548 bfa_debugfs_root = NULL; 549 } 550 } 551