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