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