linux/drivers/infiniband/hw/hfi1/debugfs.c
<<
>>
Prefs
   1/*
   2 * Copyright(c) 2015-2018 Intel Corporation.
   3 *
   4 * This file is provided under a dual BSD/GPLv2 license.  When using or
   5 * redistributing this file, you may do so under either license.
   6 *
   7 * GPL LICENSE SUMMARY
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of version 2 of the GNU General Public License 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 * BSD LICENSE
  19 *
  20 * Redistribution and use in source and binary forms, with or without
  21 * modification, are permitted provided that the following conditions
  22 * are met:
  23 *
  24 *  - Redistributions of source code must retain the above copyright
  25 *    notice, this list of conditions and the following disclaimer.
  26 *  - Redistributions in binary form must reproduce the above copyright
  27 *    notice, this list of conditions and the following disclaimer in
  28 *    the documentation and/or other materials provided with the
  29 *    distribution.
  30 *  - Neither the name of Intel Corporation nor the names of its
  31 *    contributors may be used to endorse or promote products derived
  32 *    from this software without specific prior written permission.
  33 *
  34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45 *
  46 */
  47#include <linux/debugfs.h>
  48#include <linux/seq_file.h>
  49#include <linux/kernel.h>
  50#include <linux/export.h>
  51#include <linux/module.h>
  52#include <linux/string.h>
  53#include <linux/types.h>
  54#include <linux/ratelimit.h>
  55#include <linux/fault-inject.h>
  56
  57#include "hfi.h"
  58#include "trace.h"
  59#include "debugfs.h"
  60#include "device.h"
  61#include "qp.h"
  62#include "sdma.h"
  63#include "fault.h"
  64
  65static struct dentry *hfi1_dbg_root;
  66
  67/* wrappers to enforce srcu in seq file */
  68ssize_t hfi1_seq_read(struct file *file, char __user *buf, size_t size,
  69                      loff_t *ppos)
  70{
  71        struct dentry *d = file->f_path.dentry;
  72        ssize_t r;
  73
  74        r = debugfs_file_get(d);
  75        if (unlikely(r))
  76                return r;
  77        r = seq_read(file, buf, size, ppos);
  78        debugfs_file_put(d);
  79        return r;
  80}
  81
  82loff_t hfi1_seq_lseek(struct file *file, loff_t offset, int whence)
  83{
  84        struct dentry *d = file->f_path.dentry;
  85        loff_t r;
  86
  87        r = debugfs_file_get(d);
  88        if (unlikely(r))
  89                return r;
  90        r = seq_lseek(file, offset, whence);
  91        debugfs_file_put(d);
  92        return r;
  93}
  94
  95#define private2dd(file) (file_inode(file)->i_private)
  96#define private2ppd(file) (file_inode(file)->i_private)
  97
  98static void *_opcode_stats_seq_start(struct seq_file *s, loff_t *pos)
  99{
 100        struct hfi1_opcode_stats_perctx *opstats;
 101
 102        if (*pos >= ARRAY_SIZE(opstats->stats))
 103                return NULL;
 104        return pos;
 105}
 106
 107static void *_opcode_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
 108{
 109        struct hfi1_opcode_stats_perctx *opstats;
 110
 111        ++*pos;
 112        if (*pos >= ARRAY_SIZE(opstats->stats))
 113                return NULL;
 114        return pos;
 115}
 116
 117static void _opcode_stats_seq_stop(struct seq_file *s, void *v)
 118{
 119}
 120
 121static int opcode_stats_show(struct seq_file *s, u8 i, u64 packets, u64 bytes)
 122{
 123        if (!packets && !bytes)
 124                return SEQ_SKIP;
 125        seq_printf(s, "%02x %llu/%llu\n", i,
 126                   (unsigned long long)packets,
 127                   (unsigned long long)bytes);
 128
 129        return 0;
 130}
 131
 132static int _opcode_stats_seq_show(struct seq_file *s, void *v)
 133{
 134        loff_t *spos = v;
 135        loff_t i = *spos, j;
 136        u64 n_packets = 0, n_bytes = 0;
 137        struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
 138        struct hfi1_devdata *dd = dd_from_dev(ibd);
 139        struct hfi1_ctxtdata *rcd;
 140
 141        for (j = 0; j < dd->first_dyn_alloc_ctxt; j++) {
 142                rcd = hfi1_rcd_get_by_index(dd, j);
 143                if (rcd) {
 144                        n_packets += rcd->opstats->stats[i].n_packets;
 145                        n_bytes += rcd->opstats->stats[i].n_bytes;
 146                }
 147                hfi1_rcd_put(rcd);
 148        }
 149        return opcode_stats_show(s, i, n_packets, n_bytes);
 150}
 151
 152DEBUGFS_SEQ_FILE_OPS(opcode_stats);
 153DEBUGFS_SEQ_FILE_OPEN(opcode_stats)
 154DEBUGFS_FILE_OPS(opcode_stats);
 155
 156static void *_tx_opcode_stats_seq_start(struct seq_file *s, loff_t *pos)
 157{
 158        return _opcode_stats_seq_start(s, pos);
 159}
 160
 161static void *_tx_opcode_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
 162{
 163        return _opcode_stats_seq_next(s, v, pos);
 164}
 165
 166static void _tx_opcode_stats_seq_stop(struct seq_file *s, void *v)
 167{
 168}
 169
 170static int _tx_opcode_stats_seq_show(struct seq_file *s, void *v)
 171{
 172        loff_t *spos = v;
 173        loff_t i = *spos;
 174        int j;
 175        u64 n_packets = 0, n_bytes = 0;
 176        struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
 177        struct hfi1_devdata *dd = dd_from_dev(ibd);
 178
 179        for_each_possible_cpu(j) {
 180                struct hfi1_opcode_stats_perctx *s =
 181                        per_cpu_ptr(dd->tx_opstats, j);
 182                n_packets += s->stats[i].n_packets;
 183                n_bytes += s->stats[i].n_bytes;
 184        }
 185        return opcode_stats_show(s, i, n_packets, n_bytes);
 186}
 187
 188DEBUGFS_SEQ_FILE_OPS(tx_opcode_stats);
 189DEBUGFS_SEQ_FILE_OPEN(tx_opcode_stats)
 190DEBUGFS_FILE_OPS(tx_opcode_stats);
 191
 192static void *_ctx_stats_seq_start(struct seq_file *s, loff_t *pos)
 193{
 194        struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
 195        struct hfi1_devdata *dd = dd_from_dev(ibd);
 196
 197        if (!*pos)
 198                return SEQ_START_TOKEN;
 199        if (*pos >= dd->first_dyn_alloc_ctxt)
 200                return NULL;
 201        return pos;
 202}
 203
 204static void *_ctx_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
 205{
 206        struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
 207        struct hfi1_devdata *dd = dd_from_dev(ibd);
 208
 209        if (v == SEQ_START_TOKEN)
 210                return pos;
 211
 212        ++*pos;
 213        if (*pos >= dd->first_dyn_alloc_ctxt)
 214                return NULL;
 215        return pos;
 216}
 217
 218static void _ctx_stats_seq_stop(struct seq_file *s, void *v)
 219{
 220        /* nothing allocated */
 221}
 222
 223static int _ctx_stats_seq_show(struct seq_file *s, void *v)
 224{
 225        loff_t *spos;
 226        loff_t i, j;
 227        u64 n_packets = 0;
 228        struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
 229        struct hfi1_devdata *dd = dd_from_dev(ibd);
 230        struct hfi1_ctxtdata *rcd;
 231
 232        if (v == SEQ_START_TOKEN) {
 233                seq_puts(s, "Ctx:npkts\n");
 234                return 0;
 235        }
 236
 237        spos = v;
 238        i = *spos;
 239
 240        rcd = hfi1_rcd_get_by_index_safe(dd, i);
 241        if (!rcd)
 242                return SEQ_SKIP;
 243
 244        for (j = 0; j < ARRAY_SIZE(rcd->opstats->stats); j++)
 245                n_packets += rcd->opstats->stats[j].n_packets;
 246
 247        hfi1_rcd_put(rcd);
 248
 249        if (!n_packets)
 250                return SEQ_SKIP;
 251
 252        seq_printf(s, "  %llu:%llu\n", i, n_packets);
 253        return 0;
 254}
 255
 256DEBUGFS_SEQ_FILE_OPS(ctx_stats);
 257DEBUGFS_SEQ_FILE_OPEN(ctx_stats)
 258DEBUGFS_FILE_OPS(ctx_stats);
 259
 260static void *_qp_stats_seq_start(struct seq_file *s, loff_t *pos)
 261        __acquires(RCU)
 262{
 263        struct rvt_qp_iter *iter;
 264        loff_t n = *pos;
 265
 266        iter = rvt_qp_iter_init(s->private, 0, NULL);
 267
 268        /* stop calls rcu_read_unlock */
 269        rcu_read_lock();
 270
 271        if (!iter)
 272                return NULL;
 273
 274        do {
 275                if (rvt_qp_iter_next(iter)) {
 276                        kfree(iter);
 277                        return NULL;
 278                }
 279        } while (n--);
 280
 281        return iter;
 282}
 283
 284static void *_qp_stats_seq_next(struct seq_file *s, void *iter_ptr,
 285                                loff_t *pos)
 286        __must_hold(RCU)
 287{
 288        struct rvt_qp_iter *iter = iter_ptr;
 289
 290        (*pos)++;
 291
 292        if (rvt_qp_iter_next(iter)) {
 293                kfree(iter);
 294                return NULL;
 295        }
 296
 297        return iter;
 298}
 299
 300static void _qp_stats_seq_stop(struct seq_file *s, void *iter_ptr)
 301        __releases(RCU)
 302{
 303        rcu_read_unlock();
 304}
 305
 306static int _qp_stats_seq_show(struct seq_file *s, void *iter_ptr)
 307{
 308        struct rvt_qp_iter *iter = iter_ptr;
 309
 310        if (!iter)
 311                return 0;
 312
 313        qp_iter_print(s, iter);
 314
 315        return 0;
 316}
 317
 318DEBUGFS_SEQ_FILE_OPS(qp_stats);
 319DEBUGFS_SEQ_FILE_OPEN(qp_stats)
 320DEBUGFS_FILE_OPS(qp_stats);
 321
 322static void *_sdes_seq_start(struct seq_file *s, loff_t *pos)
 323{
 324        struct hfi1_ibdev *ibd;
 325        struct hfi1_devdata *dd;
 326
 327        ibd = (struct hfi1_ibdev *)s->private;
 328        dd = dd_from_dev(ibd);
 329        if (!dd->per_sdma || *pos >= dd->num_sdma)
 330                return NULL;
 331        return pos;
 332}
 333
 334static void *_sdes_seq_next(struct seq_file *s, void *v, loff_t *pos)
 335{
 336        struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
 337        struct hfi1_devdata *dd = dd_from_dev(ibd);
 338
 339        ++*pos;
 340        if (!dd->per_sdma || *pos >= dd->num_sdma)
 341                return NULL;
 342        return pos;
 343}
 344
 345static void _sdes_seq_stop(struct seq_file *s, void *v)
 346{
 347}
 348
 349static int _sdes_seq_show(struct seq_file *s, void *v)
 350{
 351        struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
 352        struct hfi1_devdata *dd = dd_from_dev(ibd);
 353        loff_t *spos = v;
 354        loff_t i = *spos;
 355
 356        sdma_seqfile_dump_sde(s, &dd->per_sdma[i]);
 357        return 0;
 358}
 359
 360DEBUGFS_SEQ_FILE_OPS(sdes);
 361DEBUGFS_SEQ_FILE_OPEN(sdes)
 362DEBUGFS_FILE_OPS(sdes);
 363
 364static void *_rcds_seq_start(struct seq_file *s, loff_t *pos)
 365{
 366        struct hfi1_ibdev *ibd;
 367        struct hfi1_devdata *dd;
 368
 369        ibd = (struct hfi1_ibdev *)s->private;
 370        dd = dd_from_dev(ibd);
 371        if (!dd->rcd || *pos >= dd->n_krcv_queues)
 372                return NULL;
 373        return pos;
 374}
 375
 376static void *_rcds_seq_next(struct seq_file *s, void *v, loff_t *pos)
 377{
 378        struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
 379        struct hfi1_devdata *dd = dd_from_dev(ibd);
 380
 381        ++*pos;
 382        if (!dd->rcd || *pos >= dd->n_krcv_queues)
 383                return NULL;
 384        return pos;
 385}
 386
 387static void _rcds_seq_stop(struct seq_file *s, void *v)
 388{
 389}
 390
 391static int _rcds_seq_show(struct seq_file *s, void *v)
 392{
 393        struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
 394        struct hfi1_devdata *dd = dd_from_dev(ibd);
 395        struct hfi1_ctxtdata *rcd;
 396        loff_t *spos = v;
 397        loff_t i = *spos;
 398
 399        rcd = hfi1_rcd_get_by_index_safe(dd, i);
 400        if (rcd)
 401                seqfile_dump_rcd(s, rcd);
 402        hfi1_rcd_put(rcd);
 403        return 0;
 404}
 405
 406DEBUGFS_SEQ_FILE_OPS(rcds);
 407DEBUGFS_SEQ_FILE_OPEN(rcds)
 408DEBUGFS_FILE_OPS(rcds);
 409
 410/* read the per-device counters */
 411static ssize_t dev_counters_read(struct file *file, char __user *buf,
 412                                 size_t count, loff_t *ppos)
 413{
 414        u64 *counters;
 415        size_t avail;
 416        struct hfi1_devdata *dd;
 417        ssize_t rval;
 418
 419        dd = private2dd(file);
 420        avail = hfi1_read_cntrs(dd, NULL, &counters);
 421        rval =  simple_read_from_buffer(buf, count, ppos, counters, avail);
 422        return rval;
 423}
 424
 425/* read the per-device counters */
 426static ssize_t dev_names_read(struct file *file, char __user *buf,
 427                              size_t count, loff_t *ppos)
 428{
 429        char *names;
 430        size_t avail;
 431        struct hfi1_devdata *dd;
 432        ssize_t rval;
 433
 434        dd = private2dd(file);
 435        avail = hfi1_read_cntrs(dd, &names, NULL);
 436        rval =  simple_read_from_buffer(buf, count, ppos, names, avail);
 437        return rval;
 438}
 439
 440struct counter_info {
 441        char *name;
 442        const struct file_operations ops;
 443};
 444
 445/*
 446 * Could use file_inode(file)->i_ino to figure out which file,
 447 * instead of separate routine for each, but for now, this works...
 448 */
 449
 450/* read the per-port names (same for each port) */
 451static ssize_t portnames_read(struct file *file, char __user *buf,
 452                              size_t count, loff_t *ppos)
 453{
 454        char *names;
 455        size_t avail;
 456        struct hfi1_devdata *dd;
 457        ssize_t rval;
 458
 459        dd = private2dd(file);
 460        avail = hfi1_read_portcntrs(dd->pport, &names, NULL);
 461        rval = simple_read_from_buffer(buf, count, ppos, names, avail);
 462        return rval;
 463}
 464
 465/* read the per-port counters */
 466static ssize_t portcntrs_debugfs_read(struct file *file, char __user *buf,
 467                                      size_t count, loff_t *ppos)
 468{
 469        u64 *counters;
 470        size_t avail;
 471        struct hfi1_pportdata *ppd;
 472        ssize_t rval;
 473
 474        ppd = private2ppd(file);
 475        avail = hfi1_read_portcntrs(ppd, NULL, &counters);
 476        rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
 477        return rval;
 478}
 479
 480static void check_dyn_flag(u64 scratch0, char *p, int size, int *used,
 481                           int this_hfi, int hfi, u32 flag, const char *what)
 482{
 483        u32 mask;
 484
 485        mask = flag << (hfi ? CR_DYN_SHIFT : 0);
 486        if (scratch0 & mask) {
 487                *used += scnprintf(p + *used, size - *used,
 488                                   "  0x%08x - HFI%d %s in use, %s device\n",
 489                                   mask, hfi, what,
 490                                   this_hfi == hfi ? "this" : "other");
 491        }
 492}
 493
 494static ssize_t asic_flags_read(struct file *file, char __user *buf,
 495                               size_t count, loff_t *ppos)
 496{
 497        struct hfi1_pportdata *ppd;
 498        struct hfi1_devdata *dd;
 499        u64 scratch0;
 500        char *tmp;
 501        int ret = 0;
 502        int size;
 503        int used;
 504        int i;
 505
 506        ppd = private2ppd(file);
 507        dd = ppd->dd;
 508        size = PAGE_SIZE;
 509        used = 0;
 510        tmp = kmalloc(size, GFP_KERNEL);
 511        if (!tmp)
 512                return -ENOMEM;
 513
 514        scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
 515        used += scnprintf(tmp + used, size - used,
 516                          "Resource flags: 0x%016llx\n", scratch0);
 517
 518        /* check permanent flag */
 519        if (scratch0 & CR_THERM_INIT) {
 520                used += scnprintf(tmp + used, size - used,
 521                                  "  0x%08x - thermal monitoring initialized\n",
 522                                  (u32)CR_THERM_INIT);
 523        }
 524
 525        /* check each dynamic flag on each HFI */
 526        for (i = 0; i < 2; i++) {
 527                check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
 528                               CR_SBUS, "SBus");
 529                check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
 530                               CR_EPROM, "EPROM");
 531                check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
 532                               CR_I2C1, "i2c chain 1");
 533                check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
 534                               CR_I2C2, "i2c chain 2");
 535        }
 536        used += scnprintf(tmp + used, size - used, "Write bits to clear\n");
 537
 538        ret = simple_read_from_buffer(buf, count, ppos, tmp, used);
 539        kfree(tmp);
 540        return ret;
 541}
 542
 543static ssize_t asic_flags_write(struct file *file, const char __user *buf,
 544                                size_t count, loff_t *ppos)
 545{
 546        struct hfi1_pportdata *ppd;
 547        struct hfi1_devdata *dd;
 548        char *buff;
 549        int ret;
 550        unsigned long long value;
 551        u64 scratch0;
 552        u64 clear;
 553
 554        ppd = private2ppd(file);
 555        dd = ppd->dd;
 556
 557        /* zero terminate and read the expected integer */
 558        buff = memdup_user_nul(buf, count);
 559        if (IS_ERR(buff))
 560                return PTR_ERR(buff);
 561
 562        ret = kstrtoull(buff, 0, &value);
 563        if (ret)
 564                goto do_free;
 565        clear = value;
 566
 567        /* obtain exclusive access */
 568        mutex_lock(&dd->asic_data->asic_resource_mutex);
 569        acquire_hw_mutex(dd);
 570
 571        scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
 572        scratch0 &= ~clear;
 573        write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
 574        /* force write to be visible to other HFI on another OS */
 575        (void)read_csr(dd, ASIC_CFG_SCRATCH);
 576
 577        release_hw_mutex(dd);
 578        mutex_unlock(&dd->asic_data->asic_resource_mutex);
 579
 580        /* return the number of bytes written */
 581        ret = count;
 582
 583 do_free:
 584        kfree(buff);
 585        return ret;
 586}
 587
 588/* read the dc8051 memory */
 589static ssize_t dc8051_memory_read(struct file *file, char __user *buf,
 590                                  size_t count, loff_t *ppos)
 591{
 592        struct hfi1_pportdata *ppd = private2ppd(file);
 593        ssize_t rval;
 594        void *tmp;
 595        loff_t start, end;
 596
 597        /* the checks below expect the position to be positive */
 598        if (*ppos < 0)
 599                return -EINVAL;
 600
 601        tmp = kzalloc(DC8051_DATA_MEM_SIZE, GFP_KERNEL);
 602        if (!tmp)
 603                return -ENOMEM;
 604
 605        /*
 606         * Fill in the requested portion of the temporary buffer from the
 607         * 8051 memory.  The 8051 memory read is done in terms of 8 bytes.
 608         * Adjust start and end to fit.  Skip reading anything if out of
 609         * range.
 610         */
 611        start = *ppos & ~0x7;   /* round down */
 612        if (start < DC8051_DATA_MEM_SIZE) {
 613                end = (*ppos + count + 7) & ~0x7; /* round up */
 614                if (end > DC8051_DATA_MEM_SIZE)
 615                        end = DC8051_DATA_MEM_SIZE;
 616                rval = read_8051_data(ppd->dd, start, end - start,
 617                                      (u64 *)(tmp + start));
 618                if (rval)
 619                        goto done;
 620        }
 621
 622        rval = simple_read_from_buffer(buf, count, ppos, tmp,
 623                                       DC8051_DATA_MEM_SIZE);
 624done:
 625        kfree(tmp);
 626        return rval;
 627}
 628
 629static ssize_t debugfs_lcb_read(struct file *file, char __user *buf,
 630                                size_t count, loff_t *ppos)
 631{
 632        struct hfi1_pportdata *ppd = private2ppd(file);
 633        struct hfi1_devdata *dd = ppd->dd;
 634        unsigned long total, csr_off;
 635        u64 data;
 636
 637        if (*ppos < 0)
 638                return -EINVAL;
 639        /* only read 8 byte quantities */
 640        if ((count % 8) != 0)
 641                return -EINVAL;
 642        /* offset must be 8-byte aligned */
 643        if ((*ppos % 8) != 0)
 644                return -EINVAL;
 645        /* do nothing if out of range or zero count */
 646        if (*ppos >= (LCB_END - LCB_START) || !count)
 647                return 0;
 648        /* reduce count if needed */
 649        if (*ppos + count > LCB_END - LCB_START)
 650                count = (LCB_END - LCB_START) - *ppos;
 651
 652        csr_off = LCB_START + *ppos;
 653        for (total = 0; total < count; total += 8, csr_off += 8) {
 654                if (read_lcb_csr(dd, csr_off, (u64 *)&data))
 655                        break; /* failed */
 656                if (put_user(data, (unsigned long __user *)(buf + total)))
 657                        break;
 658        }
 659        *ppos += total;
 660        return total;
 661}
 662
 663static ssize_t debugfs_lcb_write(struct file *file, const char __user *buf,
 664                                 size_t count, loff_t *ppos)
 665{
 666        struct hfi1_pportdata *ppd = private2ppd(file);
 667        struct hfi1_devdata *dd = ppd->dd;
 668        unsigned long total, csr_off, data;
 669
 670        if (*ppos < 0)
 671                return -EINVAL;
 672        /* only write 8 byte quantities */
 673        if ((count % 8) != 0)
 674                return -EINVAL;
 675        /* offset must be 8-byte aligned */
 676        if ((*ppos % 8) != 0)
 677                return -EINVAL;
 678        /* do nothing if out of range or zero count */
 679        if (*ppos >= (LCB_END - LCB_START) || !count)
 680                return 0;
 681        /* reduce count if needed */
 682        if (*ppos + count > LCB_END - LCB_START)
 683                count = (LCB_END - LCB_START) - *ppos;
 684
 685        csr_off = LCB_START + *ppos;
 686        for (total = 0; total < count; total += 8, csr_off += 8) {
 687                if (get_user(data, (unsigned long __user *)(buf + total)))
 688                        break;
 689                if (write_lcb_csr(dd, csr_off, data))
 690                        break; /* failed */
 691        }
 692        *ppos += total;
 693        return total;
 694}
 695
 696/*
 697 * read the per-port QSFP data for ppd
 698 */
 699static ssize_t qsfp_debugfs_dump(struct file *file, char __user *buf,
 700                                 size_t count, loff_t *ppos)
 701{
 702        struct hfi1_pportdata *ppd;
 703        char *tmp;
 704        int ret;
 705
 706        ppd = private2ppd(file);
 707        tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
 708        if (!tmp)
 709                return -ENOMEM;
 710
 711        ret = qsfp_dump(ppd, tmp, PAGE_SIZE);
 712        if (ret > 0)
 713                ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
 714        kfree(tmp);
 715        return ret;
 716}
 717
 718/* Do an i2c write operation on the chain for the given HFI. */
 719static ssize_t __i2c_debugfs_write(struct file *file, const char __user *buf,
 720                                   size_t count, loff_t *ppos, u32 target)
 721{
 722        struct hfi1_pportdata *ppd;
 723        char *buff;
 724        int ret;
 725        int i2c_addr;
 726        int offset;
 727        int total_written;
 728
 729        ppd = private2ppd(file);
 730
 731        /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
 732        i2c_addr = (*ppos >> 16) & 0xffff;
 733        offset = *ppos & 0xffff;
 734
 735        /* explicitly reject invalid address 0 to catch cp and cat */
 736        if (i2c_addr == 0)
 737                return -EINVAL;
 738
 739        buff = memdup_user(buf, count);
 740        if (IS_ERR(buff))
 741                return PTR_ERR(buff);
 742
 743        total_written = i2c_write(ppd, target, i2c_addr, offset, buff, count);
 744        if (total_written < 0) {
 745                ret = total_written;
 746                goto _free;
 747        }
 748
 749        *ppos += total_written;
 750
 751        ret = total_written;
 752
 753 _free:
 754        kfree(buff);
 755        return ret;
 756}
 757
 758/* Do an i2c write operation on chain for HFI 0. */
 759static ssize_t i2c1_debugfs_write(struct file *file, const char __user *buf,
 760                                  size_t count, loff_t *ppos)
 761{
 762        return __i2c_debugfs_write(file, buf, count, ppos, 0);
 763}
 764
 765/* Do an i2c write operation on chain for HFI 1. */
 766static ssize_t i2c2_debugfs_write(struct file *file, const char __user *buf,
 767                                  size_t count, loff_t *ppos)
 768{
 769        return __i2c_debugfs_write(file, buf, count, ppos, 1);
 770}
 771
 772/* Do an i2c read operation on the chain for the given HFI. */
 773static ssize_t __i2c_debugfs_read(struct file *file, char __user *buf,
 774                                  size_t count, loff_t *ppos, u32 target)
 775{
 776        struct hfi1_pportdata *ppd;
 777        char *buff;
 778        int ret;
 779        int i2c_addr;
 780        int offset;
 781        int total_read;
 782
 783        ppd = private2ppd(file);
 784
 785        /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
 786        i2c_addr = (*ppos >> 16) & 0xffff;
 787        offset = *ppos & 0xffff;
 788
 789        /* explicitly reject invalid address 0 to catch cp and cat */
 790        if (i2c_addr == 0)
 791                return -EINVAL;
 792
 793        buff = kmalloc(count, GFP_KERNEL);
 794        if (!buff)
 795                return -ENOMEM;
 796
 797        total_read = i2c_read(ppd, target, i2c_addr, offset, buff, count);
 798        if (total_read < 0) {
 799                ret = total_read;
 800                goto _free;
 801        }
 802
 803        *ppos += total_read;
 804
 805        ret = copy_to_user(buf, buff, total_read);
 806        if (ret > 0) {
 807                ret = -EFAULT;
 808                goto _free;
 809        }
 810
 811        ret = total_read;
 812
 813 _free:
 814        kfree(buff);
 815        return ret;
 816}
 817
 818/* Do an i2c read operation on chain for HFI 0. */
 819static ssize_t i2c1_debugfs_read(struct file *file, char __user *buf,
 820                                 size_t count, loff_t *ppos)
 821{
 822        return __i2c_debugfs_read(file, buf, count, ppos, 0);
 823}
 824
 825/* Do an i2c read operation on chain for HFI 1. */
 826static ssize_t i2c2_debugfs_read(struct file *file, char __user *buf,
 827                                 size_t count, loff_t *ppos)
 828{
 829        return __i2c_debugfs_read(file, buf, count, ppos, 1);
 830}
 831
 832/* Do a QSFP write operation on the i2c chain for the given HFI. */
 833static ssize_t __qsfp_debugfs_write(struct file *file, const char __user *buf,
 834                                    size_t count, loff_t *ppos, u32 target)
 835{
 836        struct hfi1_pportdata *ppd;
 837        char *buff;
 838        int ret;
 839        int total_written;
 840
 841        if (*ppos + count > QSFP_PAGESIZE * 4) /* base page + page00-page03 */
 842                return -EINVAL;
 843
 844        ppd = private2ppd(file);
 845
 846        buff = memdup_user(buf, count);
 847        if (IS_ERR(buff))
 848                return PTR_ERR(buff);
 849
 850        total_written = qsfp_write(ppd, target, *ppos, buff, count);
 851        if (total_written < 0) {
 852                ret = total_written;
 853                goto _free;
 854        }
 855
 856        *ppos += total_written;
 857
 858        ret = total_written;
 859
 860 _free:
 861        kfree(buff);
 862        return ret;
 863}
 864
 865/* Do a QSFP write operation on i2c chain for HFI 0. */
 866static ssize_t qsfp1_debugfs_write(struct file *file, const char __user *buf,
 867                                   size_t count, loff_t *ppos)
 868{
 869        return __qsfp_debugfs_write(file, buf, count, ppos, 0);
 870}
 871
 872/* Do a QSFP write operation on i2c chain for HFI 1. */
 873static ssize_t qsfp2_debugfs_write(struct file *file, const char __user *buf,
 874                                   size_t count, loff_t *ppos)
 875{
 876        return __qsfp_debugfs_write(file, buf, count, ppos, 1);
 877}
 878
 879/* Do a QSFP read operation on the i2c chain for the given HFI. */
 880static ssize_t __qsfp_debugfs_read(struct file *file, char __user *buf,
 881                                   size_t count, loff_t *ppos, u32 target)
 882{
 883        struct hfi1_pportdata *ppd;
 884        char *buff;
 885        int ret;
 886        int total_read;
 887
 888        if (*ppos + count > QSFP_PAGESIZE * 4) { /* base page + page00-page03 */
 889                ret = -EINVAL;
 890                goto _return;
 891        }
 892
 893        ppd = private2ppd(file);
 894
 895        buff = kmalloc(count, GFP_KERNEL);
 896        if (!buff) {
 897                ret = -ENOMEM;
 898                goto _return;
 899        }
 900
 901        total_read = qsfp_read(ppd, target, *ppos, buff, count);
 902        if (total_read < 0) {
 903                ret = total_read;
 904                goto _free;
 905        }
 906
 907        *ppos += total_read;
 908
 909        ret = copy_to_user(buf, buff, total_read);
 910        if (ret > 0) {
 911                ret = -EFAULT;
 912                goto _free;
 913        }
 914
 915        ret = total_read;
 916
 917 _free:
 918        kfree(buff);
 919 _return:
 920        return ret;
 921}
 922
 923/* Do a QSFP read operation on i2c chain for HFI 0. */
 924static ssize_t qsfp1_debugfs_read(struct file *file, char __user *buf,
 925                                  size_t count, loff_t *ppos)
 926{
 927        return __qsfp_debugfs_read(file, buf, count, ppos, 0);
 928}
 929
 930/* Do a QSFP read operation on i2c chain for HFI 1. */
 931static ssize_t qsfp2_debugfs_read(struct file *file, char __user *buf,
 932                                  size_t count, loff_t *ppos)
 933{
 934        return __qsfp_debugfs_read(file, buf, count, ppos, 1);
 935}
 936
 937static int __i2c_debugfs_open(struct inode *in, struct file *fp, u32 target)
 938{
 939        struct hfi1_pportdata *ppd;
 940        int ret;
 941
 942        if (!try_module_get(THIS_MODULE))
 943                return -ENODEV;
 944
 945        ppd = private2ppd(fp);
 946
 947        ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
 948        if (ret) /* failed - release the module */
 949                module_put(THIS_MODULE);
 950
 951        return ret;
 952}
 953
 954static int i2c1_debugfs_open(struct inode *in, struct file *fp)
 955{
 956        return __i2c_debugfs_open(in, fp, 0);
 957}
 958
 959static int i2c2_debugfs_open(struct inode *in, struct file *fp)
 960{
 961        return __i2c_debugfs_open(in, fp, 1);
 962}
 963
 964static int __i2c_debugfs_release(struct inode *in, struct file *fp, u32 target)
 965{
 966        struct hfi1_pportdata *ppd;
 967
 968        ppd = private2ppd(fp);
 969
 970        release_chip_resource(ppd->dd, i2c_target(target));
 971        module_put(THIS_MODULE);
 972
 973        return 0;
 974}
 975
 976static int i2c1_debugfs_release(struct inode *in, struct file *fp)
 977{
 978        return __i2c_debugfs_release(in, fp, 0);
 979}
 980
 981static int i2c2_debugfs_release(struct inode *in, struct file *fp)
 982{
 983        return __i2c_debugfs_release(in, fp, 1);
 984}
 985
 986static int __qsfp_debugfs_open(struct inode *in, struct file *fp, u32 target)
 987{
 988        struct hfi1_pportdata *ppd;
 989        int ret;
 990
 991        if (!try_module_get(THIS_MODULE))
 992                return -ENODEV;
 993
 994        ppd = private2ppd(fp);
 995
 996        ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
 997        if (ret) /* failed - release the module */
 998                module_put(THIS_MODULE);
 999
1000        return ret;
1001}
1002
1003static int qsfp1_debugfs_open(struct inode *in, struct file *fp)
1004{
1005        return __qsfp_debugfs_open(in, fp, 0);
1006}
1007
1008static int qsfp2_debugfs_open(struct inode *in, struct file *fp)
1009{
1010        return __qsfp_debugfs_open(in, fp, 1);
1011}
1012
1013static int __qsfp_debugfs_release(struct inode *in, struct file *fp, u32 target)
1014{
1015        struct hfi1_pportdata *ppd;
1016
1017        ppd = private2ppd(fp);
1018
1019        release_chip_resource(ppd->dd, i2c_target(target));
1020        module_put(THIS_MODULE);
1021
1022        return 0;
1023}
1024
1025static int qsfp1_debugfs_release(struct inode *in, struct file *fp)
1026{
1027        return __qsfp_debugfs_release(in, fp, 0);
1028}
1029
1030static int qsfp2_debugfs_release(struct inode *in, struct file *fp)
1031{
1032        return __qsfp_debugfs_release(in, fp, 1);
1033}
1034
1035#define DEBUGFS_OPS(nm, readroutine, writeroutine)      \
1036{ \
1037        .name = nm, \
1038        .ops = { \
1039                .read = readroutine, \
1040                .write = writeroutine, \
1041                .llseek = generic_file_llseek, \
1042        }, \
1043}
1044
1045#define DEBUGFS_XOPS(nm, readf, writef, openf, releasef) \
1046{ \
1047        .name = nm, \
1048        .ops = { \
1049                .read = readf, \
1050                .write = writef, \
1051                .llseek = generic_file_llseek, \
1052                .open = openf, \
1053                .release = releasef \
1054        }, \
1055}
1056
1057static const struct counter_info cntr_ops[] = {
1058        DEBUGFS_OPS("counter_names", dev_names_read, NULL),
1059        DEBUGFS_OPS("counters", dev_counters_read, NULL),
1060        DEBUGFS_OPS("portcounter_names", portnames_read, NULL),
1061};
1062
1063static const struct counter_info port_cntr_ops[] = {
1064        DEBUGFS_OPS("port%dcounters", portcntrs_debugfs_read, NULL),
1065        DEBUGFS_XOPS("i2c1", i2c1_debugfs_read, i2c1_debugfs_write,
1066                     i2c1_debugfs_open, i2c1_debugfs_release),
1067        DEBUGFS_XOPS("i2c2", i2c2_debugfs_read, i2c2_debugfs_write,
1068                     i2c2_debugfs_open, i2c2_debugfs_release),
1069        DEBUGFS_OPS("qsfp_dump%d", qsfp_debugfs_dump, NULL),
1070        DEBUGFS_XOPS("qsfp1", qsfp1_debugfs_read, qsfp1_debugfs_write,
1071                     qsfp1_debugfs_open, qsfp1_debugfs_release),
1072        DEBUGFS_XOPS("qsfp2", qsfp2_debugfs_read, qsfp2_debugfs_write,
1073                     qsfp2_debugfs_open, qsfp2_debugfs_release),
1074        DEBUGFS_OPS("asic_flags", asic_flags_read, asic_flags_write),
1075        DEBUGFS_OPS("dc8051_memory", dc8051_memory_read, NULL),
1076        DEBUGFS_OPS("lcb", debugfs_lcb_read, debugfs_lcb_write),
1077};
1078
1079static void *_sdma_cpu_list_seq_start(struct seq_file *s, loff_t *pos)
1080{
1081        if (*pos >= num_online_cpus())
1082                return NULL;
1083
1084        return pos;
1085}
1086
1087static void *_sdma_cpu_list_seq_next(struct seq_file *s, void *v, loff_t *pos)
1088{
1089        ++*pos;
1090        if (*pos >= num_online_cpus())
1091                return NULL;
1092
1093        return pos;
1094}
1095
1096static void _sdma_cpu_list_seq_stop(struct seq_file *s, void *v)
1097{
1098        /* nothing allocated */
1099}
1100
1101static int _sdma_cpu_list_seq_show(struct seq_file *s, void *v)
1102{
1103        struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
1104        struct hfi1_devdata *dd = dd_from_dev(ibd);
1105        loff_t *spos = v;
1106        loff_t i = *spos;
1107
1108        sdma_seqfile_dump_cpu_list(s, dd, (unsigned long)i);
1109        return 0;
1110}
1111
1112DEBUGFS_SEQ_FILE_OPS(sdma_cpu_list);
1113DEBUGFS_SEQ_FILE_OPEN(sdma_cpu_list)
1114DEBUGFS_FILE_OPS(sdma_cpu_list);
1115
1116void hfi1_dbg_ibdev_init(struct hfi1_ibdev *ibd)
1117{
1118        char name[sizeof("port0counters") + 1];
1119        char link[10];
1120        struct hfi1_devdata *dd = dd_from_dev(ibd);
1121        struct hfi1_pportdata *ppd;
1122        int unit = dd->unit;
1123        int i, j;
1124
1125        if (!hfi1_dbg_root)
1126                return;
1127        snprintf(name, sizeof(name), "%s_%d", class_name(), unit);
1128        snprintf(link, sizeof(link), "%d", unit);
1129        ibd->hfi1_ibdev_dbg = debugfs_create_dir(name, hfi1_dbg_root);
1130        if (!ibd->hfi1_ibdev_dbg) {
1131                pr_warn("create of %s failed\n", name);
1132                return;
1133        }
1134        ibd->hfi1_ibdev_link =
1135                debugfs_create_symlink(link, hfi1_dbg_root, name);
1136        if (!ibd->hfi1_ibdev_link) {
1137                pr_warn("create of %s symlink failed\n", name);
1138                return;
1139        }
1140        DEBUGFS_SEQ_FILE_CREATE(opcode_stats, ibd->hfi1_ibdev_dbg, ibd);
1141        DEBUGFS_SEQ_FILE_CREATE(tx_opcode_stats, ibd->hfi1_ibdev_dbg, ibd);
1142        DEBUGFS_SEQ_FILE_CREATE(ctx_stats, ibd->hfi1_ibdev_dbg, ibd);
1143        DEBUGFS_SEQ_FILE_CREATE(qp_stats, ibd->hfi1_ibdev_dbg, ibd);
1144        DEBUGFS_SEQ_FILE_CREATE(sdes, ibd->hfi1_ibdev_dbg, ibd);
1145        DEBUGFS_SEQ_FILE_CREATE(rcds, ibd->hfi1_ibdev_dbg, ibd);
1146        DEBUGFS_SEQ_FILE_CREATE(sdma_cpu_list, ibd->hfi1_ibdev_dbg, ibd);
1147        /* dev counter files */
1148        for (i = 0; i < ARRAY_SIZE(cntr_ops); i++)
1149                DEBUGFS_FILE_CREATE(cntr_ops[i].name,
1150                                    ibd->hfi1_ibdev_dbg,
1151                                    dd,
1152                                    &cntr_ops[i].ops, S_IRUGO);
1153        /* per port files */
1154        for (ppd = dd->pport, j = 0; j < dd->num_pports; j++, ppd++)
1155                for (i = 0; i < ARRAY_SIZE(port_cntr_ops); i++) {
1156                        snprintf(name,
1157                                 sizeof(name),
1158                                 port_cntr_ops[i].name,
1159                                 j + 1);
1160                        DEBUGFS_FILE_CREATE(name,
1161                                            ibd->hfi1_ibdev_dbg,
1162                                            ppd,
1163                                            &port_cntr_ops[i].ops,
1164                                            !port_cntr_ops[i].ops.write ?
1165                                            S_IRUGO : S_IRUGO | S_IWUSR);
1166                }
1167
1168        hfi1_fault_init_debugfs(ibd);
1169}
1170
1171void hfi1_dbg_ibdev_exit(struct hfi1_ibdev *ibd)
1172{
1173        if (!hfi1_dbg_root)
1174                goto out;
1175        hfi1_fault_exit_debugfs(ibd);
1176        debugfs_remove(ibd->hfi1_ibdev_link);
1177        debugfs_remove_recursive(ibd->hfi1_ibdev_dbg);
1178out:
1179        ibd->hfi1_ibdev_dbg = NULL;
1180}
1181
1182/*
1183 * driver stats field names, one line per stat, single string.  Used by
1184 * programs like hfistats to print the stats in a way which works for
1185 * different versions of drivers, without changing program source.
1186 * if hfi1_ib_stats changes, this needs to change.  Names need to be
1187 * 12 chars or less (w/o newline), for proper display by hfistats utility.
1188 */
1189static const char * const hfi1_statnames[] = {
1190        /* must be element 0*/
1191        "KernIntr",
1192        "ErrorIntr",
1193        "Tx_Errs",
1194        "Rcv_Errs",
1195        "H/W_Errs",
1196        "NoPIOBufs",
1197        "CtxtsOpen",
1198        "RcvLen_Errs",
1199        "EgrBufFull",
1200        "EgrHdrFull"
1201};
1202
1203static void *_driver_stats_names_seq_start(struct seq_file *s, loff_t *pos)
1204{
1205        if (*pos >= ARRAY_SIZE(hfi1_statnames))
1206                return NULL;
1207        return pos;
1208}
1209
1210static void *_driver_stats_names_seq_next(
1211        struct seq_file *s,
1212        void *v,
1213        loff_t *pos)
1214{
1215        ++*pos;
1216        if (*pos >= ARRAY_SIZE(hfi1_statnames))
1217                return NULL;
1218        return pos;
1219}
1220
1221static void _driver_stats_names_seq_stop(struct seq_file *s, void *v)
1222{
1223}
1224
1225static int _driver_stats_names_seq_show(struct seq_file *s, void *v)
1226{
1227        loff_t *spos = v;
1228
1229        seq_printf(s, "%s\n", hfi1_statnames[*spos]);
1230        return 0;
1231}
1232
1233DEBUGFS_SEQ_FILE_OPS(driver_stats_names);
1234DEBUGFS_SEQ_FILE_OPEN(driver_stats_names)
1235DEBUGFS_FILE_OPS(driver_stats_names);
1236
1237static void *_driver_stats_seq_start(struct seq_file *s, loff_t *pos)
1238{
1239        if (*pos >= ARRAY_SIZE(hfi1_statnames))
1240                return NULL;
1241        return pos;
1242}
1243
1244static void *_driver_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
1245{
1246        ++*pos;
1247        if (*pos >= ARRAY_SIZE(hfi1_statnames))
1248                return NULL;
1249        return pos;
1250}
1251
1252static void _driver_stats_seq_stop(struct seq_file *s, void *v)
1253{
1254}
1255
1256static u64 hfi1_sps_ints(void)
1257{
1258        unsigned long flags;
1259        struct hfi1_devdata *dd;
1260        u64 sps_ints = 0;
1261
1262        spin_lock_irqsave(&hfi1_devs_lock, flags);
1263        list_for_each_entry(dd, &hfi1_dev_list, list) {
1264                sps_ints += get_all_cpu_total(dd->int_counter);
1265        }
1266        spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1267        return sps_ints;
1268}
1269
1270static int _driver_stats_seq_show(struct seq_file *s, void *v)
1271{
1272        loff_t *spos = v;
1273        char *buffer;
1274        u64 *stats = (u64 *)&hfi1_stats;
1275        size_t sz = seq_get_buf(s, &buffer);
1276
1277        if (sz < sizeof(u64))
1278                return SEQ_SKIP;
1279        /* special case for interrupts */
1280        if (*spos == 0)
1281                *(u64 *)buffer = hfi1_sps_ints();
1282        else
1283                *(u64 *)buffer = stats[*spos];
1284        seq_commit(s,  sizeof(u64));
1285        return 0;
1286}
1287
1288DEBUGFS_SEQ_FILE_OPS(driver_stats);
1289DEBUGFS_SEQ_FILE_OPEN(driver_stats)
1290DEBUGFS_FILE_OPS(driver_stats);
1291
1292void hfi1_dbg_init(void)
1293{
1294        hfi1_dbg_root  = debugfs_create_dir(DRIVER_NAME, NULL);
1295        if (!hfi1_dbg_root)
1296                pr_warn("init of debugfs failed\n");
1297        DEBUGFS_SEQ_FILE_CREATE(driver_stats_names, hfi1_dbg_root, NULL);
1298        DEBUGFS_SEQ_FILE_CREATE(driver_stats, hfi1_dbg_root, NULL);
1299}
1300
1301void hfi1_dbg_exit(void)
1302{
1303        debugfs_remove_recursive(hfi1_dbg_root);
1304        hfi1_dbg_root = NULL;
1305}
1306