treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / infiniband / hw / hfi1 / debugfs.c
blob4633a0ce1a8c0fdeadf10af7bd42f32b0819709e
1 /*
2 * Copyright(c) 2015-2018 Intel Corporation.
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.
7 * GPL LICENSE SUMMARY
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.
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.
18 * BSD LICENSE
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
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.
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.
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>
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"
65 static struct dentry *hfi1_dbg_root;
67 /* wrappers to enforce srcu in seq file */
68 ssize_t hfi1_seq_read(struct file *file, char __user *buf, size_t size,
69 loff_t *ppos)
71 struct dentry *d = file->f_path.dentry;
72 ssize_t r;
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;
82 loff_t hfi1_seq_lseek(struct file *file, loff_t offset, int whence)
84 struct dentry *d = file->f_path.dentry;
85 loff_t r;
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;
95 #define private2dd(file) (file_inode(file)->i_private)
96 #define private2ppd(file) (file_inode(file)->i_private)
98 static void *_opcode_stats_seq_start(struct seq_file *s, loff_t *pos)
100 struct hfi1_opcode_stats_perctx *opstats;
102 if (*pos >= ARRAY_SIZE(opstats->stats))
103 return NULL;
104 return pos;
107 static void *_opcode_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
109 struct hfi1_opcode_stats_perctx *opstats;
111 ++*pos;
112 if (*pos >= ARRAY_SIZE(opstats->stats))
113 return NULL;
114 return pos;
117 static void _opcode_stats_seq_stop(struct seq_file *s, void *v)
121 static int opcode_stats_show(struct seq_file *s, u8 i, u64 packets, u64 bytes)
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);
129 return 0;
132 static int _opcode_stats_seq_show(struct seq_file *s, void *v)
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;
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;
147 hfi1_rcd_put(rcd);
149 return opcode_stats_show(s, i, n_packets, n_bytes);
152 DEBUGFS_SEQ_FILE_OPS(opcode_stats);
153 DEBUGFS_SEQ_FILE_OPEN(opcode_stats)
154 DEBUGFS_FILE_OPS(opcode_stats);
156 static void *_tx_opcode_stats_seq_start(struct seq_file *s, loff_t *pos)
158 return _opcode_stats_seq_start(s, pos);
161 static void *_tx_opcode_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
163 return _opcode_stats_seq_next(s, v, pos);
166 static void _tx_opcode_stats_seq_stop(struct seq_file *s, void *v)
170 static int _tx_opcode_stats_seq_show(struct seq_file *s, void *v)
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);
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;
185 return opcode_stats_show(s, i, n_packets, n_bytes);
188 DEBUGFS_SEQ_FILE_OPS(tx_opcode_stats);
189 DEBUGFS_SEQ_FILE_OPEN(tx_opcode_stats)
190 DEBUGFS_FILE_OPS(tx_opcode_stats);
192 static void *_ctx_stats_seq_start(struct seq_file *s, loff_t *pos)
194 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
195 struct hfi1_devdata *dd = dd_from_dev(ibd);
197 if (!*pos)
198 return SEQ_START_TOKEN;
199 if (*pos >= dd->first_dyn_alloc_ctxt)
200 return NULL;
201 return pos;
204 static void *_ctx_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
206 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
207 struct hfi1_devdata *dd = dd_from_dev(ibd);
209 if (v == SEQ_START_TOKEN)
210 return pos;
212 ++*pos;
213 if (*pos >= dd->first_dyn_alloc_ctxt)
214 return NULL;
215 return pos;
218 static void _ctx_stats_seq_stop(struct seq_file *s, void *v)
220 /* nothing allocated */
223 static int _ctx_stats_seq_show(struct seq_file *s, void *v)
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;
232 if (v == SEQ_START_TOKEN) {
233 seq_puts(s, "Ctx:npkts\n");
234 return 0;
237 spos = v;
238 i = *spos;
240 rcd = hfi1_rcd_get_by_index_safe(dd, i);
241 if (!rcd)
242 return SEQ_SKIP;
244 for (j = 0; j < ARRAY_SIZE(rcd->opstats->stats); j++)
245 n_packets += rcd->opstats->stats[j].n_packets;
247 hfi1_rcd_put(rcd);
249 if (!n_packets)
250 return SEQ_SKIP;
252 seq_printf(s, " %llu:%llu\n", i, n_packets);
253 return 0;
256 DEBUGFS_SEQ_FILE_OPS(ctx_stats);
257 DEBUGFS_SEQ_FILE_OPEN(ctx_stats)
258 DEBUGFS_FILE_OPS(ctx_stats);
260 static void *_qp_stats_seq_start(struct seq_file *s, loff_t *pos)
261 __acquires(RCU)
263 struct rvt_qp_iter *iter;
264 loff_t n = *pos;
266 iter = rvt_qp_iter_init(s->private, 0, NULL);
268 /* stop calls rcu_read_unlock */
269 rcu_read_lock();
271 if (!iter)
272 return NULL;
274 do {
275 if (rvt_qp_iter_next(iter)) {
276 kfree(iter);
277 return NULL;
279 } while (n--);
281 return iter;
284 static void *_qp_stats_seq_next(struct seq_file *s, void *iter_ptr,
285 loff_t *pos)
286 __must_hold(RCU)
288 struct rvt_qp_iter *iter = iter_ptr;
290 (*pos)++;
292 if (rvt_qp_iter_next(iter)) {
293 kfree(iter);
294 return NULL;
297 return iter;
300 static void _qp_stats_seq_stop(struct seq_file *s, void *iter_ptr)
301 __releases(RCU)
303 rcu_read_unlock();
306 static int _qp_stats_seq_show(struct seq_file *s, void *iter_ptr)
308 struct rvt_qp_iter *iter = iter_ptr;
310 if (!iter)
311 return 0;
313 qp_iter_print(s, iter);
315 return 0;
318 DEBUGFS_SEQ_FILE_OPS(qp_stats);
319 DEBUGFS_SEQ_FILE_OPEN(qp_stats)
320 DEBUGFS_FILE_OPS(qp_stats);
322 static void *_sdes_seq_start(struct seq_file *s, loff_t *pos)
324 struct hfi1_ibdev *ibd;
325 struct hfi1_devdata *dd;
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;
334 static void *_sdes_seq_next(struct seq_file *s, void *v, loff_t *pos)
336 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
337 struct hfi1_devdata *dd = dd_from_dev(ibd);
339 ++*pos;
340 if (!dd->per_sdma || *pos >= dd->num_sdma)
341 return NULL;
342 return pos;
345 static void _sdes_seq_stop(struct seq_file *s, void *v)
349 static int _sdes_seq_show(struct seq_file *s, void *v)
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;
356 sdma_seqfile_dump_sde(s, &dd->per_sdma[i]);
357 return 0;
360 DEBUGFS_SEQ_FILE_OPS(sdes);
361 DEBUGFS_SEQ_FILE_OPEN(sdes)
362 DEBUGFS_FILE_OPS(sdes);
364 static void *_rcds_seq_start(struct seq_file *s, loff_t *pos)
366 struct hfi1_ibdev *ibd;
367 struct hfi1_devdata *dd;
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;
376 static void *_rcds_seq_next(struct seq_file *s, void *v, loff_t *pos)
378 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
379 struct hfi1_devdata *dd = dd_from_dev(ibd);
381 ++*pos;
382 if (!dd->rcd || *pos >= dd->num_rcv_contexts)
383 return NULL;
384 return pos;
387 static void _rcds_seq_stop(struct seq_file *s, void *v)
391 static int _rcds_seq_show(struct seq_file *s, void *v)
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;
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;
406 DEBUGFS_SEQ_FILE_OPS(rcds);
407 DEBUGFS_SEQ_FILE_OPEN(rcds)
408 DEBUGFS_FILE_OPS(rcds);
410 static void *_pios_seq_start(struct seq_file *s, loff_t *pos)
412 struct hfi1_ibdev *ibd;
413 struct hfi1_devdata *dd;
415 ibd = (struct hfi1_ibdev *)s->private;
416 dd = dd_from_dev(ibd);
417 if (!dd->send_contexts || *pos >= dd->num_send_contexts)
418 return NULL;
419 return pos;
422 static void *_pios_seq_next(struct seq_file *s, void *v, loff_t *pos)
424 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
425 struct hfi1_devdata *dd = dd_from_dev(ibd);
427 ++*pos;
428 if (!dd->send_contexts || *pos >= dd->num_send_contexts)
429 return NULL;
430 return pos;
433 static void _pios_seq_stop(struct seq_file *s, void *v)
437 static int _pios_seq_show(struct seq_file *s, void *v)
439 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
440 struct hfi1_devdata *dd = dd_from_dev(ibd);
441 struct send_context_info *sci;
442 loff_t *spos = v;
443 loff_t i = *spos;
444 unsigned long flags;
446 spin_lock_irqsave(&dd->sc_lock, flags);
447 sci = &dd->send_contexts[i];
448 if (sci && sci->type != SC_USER && sci->allocated && sci->sc)
449 seqfile_dump_sci(s, i, sci);
450 spin_unlock_irqrestore(&dd->sc_lock, flags);
451 return 0;
454 DEBUGFS_SEQ_FILE_OPS(pios);
455 DEBUGFS_SEQ_FILE_OPEN(pios)
456 DEBUGFS_FILE_OPS(pios);
458 /* read the per-device counters */
459 static ssize_t dev_counters_read(struct file *file, char __user *buf,
460 size_t count, loff_t *ppos)
462 u64 *counters;
463 size_t avail;
464 struct hfi1_devdata *dd;
465 ssize_t rval;
467 dd = private2dd(file);
468 avail = hfi1_read_cntrs(dd, NULL, &counters);
469 rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
470 return rval;
473 /* read the per-device counters */
474 static ssize_t dev_names_read(struct file *file, char __user *buf,
475 size_t count, loff_t *ppos)
477 char *names;
478 size_t avail;
479 struct hfi1_devdata *dd;
480 ssize_t rval;
482 dd = private2dd(file);
483 avail = hfi1_read_cntrs(dd, &names, NULL);
484 rval = simple_read_from_buffer(buf, count, ppos, names, avail);
485 return rval;
488 struct counter_info {
489 char *name;
490 const struct file_operations ops;
494 * Could use file_inode(file)->i_ino to figure out which file,
495 * instead of separate routine for each, but for now, this works...
498 /* read the per-port names (same for each port) */
499 static ssize_t portnames_read(struct file *file, char __user *buf,
500 size_t count, loff_t *ppos)
502 char *names;
503 size_t avail;
504 struct hfi1_devdata *dd;
505 ssize_t rval;
507 dd = private2dd(file);
508 avail = hfi1_read_portcntrs(dd->pport, &names, NULL);
509 rval = simple_read_from_buffer(buf, count, ppos, names, avail);
510 return rval;
513 /* read the per-port counters */
514 static ssize_t portcntrs_debugfs_read(struct file *file, char __user *buf,
515 size_t count, loff_t *ppos)
517 u64 *counters;
518 size_t avail;
519 struct hfi1_pportdata *ppd;
520 ssize_t rval;
522 ppd = private2ppd(file);
523 avail = hfi1_read_portcntrs(ppd, NULL, &counters);
524 rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
525 return rval;
528 static void check_dyn_flag(u64 scratch0, char *p, int size, int *used,
529 int this_hfi, int hfi, u32 flag, const char *what)
531 u32 mask;
533 mask = flag << (hfi ? CR_DYN_SHIFT : 0);
534 if (scratch0 & mask) {
535 *used += scnprintf(p + *used, size - *used,
536 " 0x%08x - HFI%d %s in use, %s device\n",
537 mask, hfi, what,
538 this_hfi == hfi ? "this" : "other");
542 static ssize_t asic_flags_read(struct file *file, char __user *buf,
543 size_t count, loff_t *ppos)
545 struct hfi1_pportdata *ppd;
546 struct hfi1_devdata *dd;
547 u64 scratch0;
548 char *tmp;
549 int ret = 0;
550 int size;
551 int used;
552 int i;
554 ppd = private2ppd(file);
555 dd = ppd->dd;
556 size = PAGE_SIZE;
557 used = 0;
558 tmp = kmalloc(size, GFP_KERNEL);
559 if (!tmp)
560 return -ENOMEM;
562 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
563 used += scnprintf(tmp + used, size - used,
564 "Resource flags: 0x%016llx\n", scratch0);
566 /* check permanent flag */
567 if (scratch0 & CR_THERM_INIT) {
568 used += scnprintf(tmp + used, size - used,
569 " 0x%08x - thermal monitoring initialized\n",
570 (u32)CR_THERM_INIT);
573 /* check each dynamic flag on each HFI */
574 for (i = 0; i < 2; i++) {
575 check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
576 CR_SBUS, "SBus");
577 check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
578 CR_EPROM, "EPROM");
579 check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
580 CR_I2C1, "i2c chain 1");
581 check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
582 CR_I2C2, "i2c chain 2");
584 used += scnprintf(tmp + used, size - used, "Write bits to clear\n");
586 ret = simple_read_from_buffer(buf, count, ppos, tmp, used);
587 kfree(tmp);
588 return ret;
591 static ssize_t asic_flags_write(struct file *file, const char __user *buf,
592 size_t count, loff_t *ppos)
594 struct hfi1_pportdata *ppd;
595 struct hfi1_devdata *dd;
596 char *buff;
597 int ret;
598 unsigned long long value;
599 u64 scratch0;
600 u64 clear;
602 ppd = private2ppd(file);
603 dd = ppd->dd;
605 /* zero terminate and read the expected integer */
606 buff = memdup_user_nul(buf, count);
607 if (IS_ERR(buff))
608 return PTR_ERR(buff);
610 ret = kstrtoull(buff, 0, &value);
611 if (ret)
612 goto do_free;
613 clear = value;
615 /* obtain exclusive access */
616 mutex_lock(&dd->asic_data->asic_resource_mutex);
617 acquire_hw_mutex(dd);
619 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
620 scratch0 &= ~clear;
621 write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
622 /* force write to be visible to other HFI on another OS */
623 (void)read_csr(dd, ASIC_CFG_SCRATCH);
625 release_hw_mutex(dd);
626 mutex_unlock(&dd->asic_data->asic_resource_mutex);
628 /* return the number of bytes written */
629 ret = count;
631 do_free:
632 kfree(buff);
633 return ret;
636 /* read the dc8051 memory */
637 static ssize_t dc8051_memory_read(struct file *file, char __user *buf,
638 size_t count, loff_t *ppos)
640 struct hfi1_pportdata *ppd = private2ppd(file);
641 ssize_t rval;
642 void *tmp;
643 loff_t start, end;
645 /* the checks below expect the position to be positive */
646 if (*ppos < 0)
647 return -EINVAL;
649 tmp = kzalloc(DC8051_DATA_MEM_SIZE, GFP_KERNEL);
650 if (!tmp)
651 return -ENOMEM;
654 * Fill in the requested portion of the temporary buffer from the
655 * 8051 memory. The 8051 memory read is done in terms of 8 bytes.
656 * Adjust start and end to fit. Skip reading anything if out of
657 * range.
659 start = *ppos & ~0x7; /* round down */
660 if (start < DC8051_DATA_MEM_SIZE) {
661 end = (*ppos + count + 7) & ~0x7; /* round up */
662 if (end > DC8051_DATA_MEM_SIZE)
663 end = DC8051_DATA_MEM_SIZE;
664 rval = read_8051_data(ppd->dd, start, end - start,
665 (u64 *)(tmp + start));
666 if (rval)
667 goto done;
670 rval = simple_read_from_buffer(buf, count, ppos, tmp,
671 DC8051_DATA_MEM_SIZE);
672 done:
673 kfree(tmp);
674 return rval;
677 static ssize_t debugfs_lcb_read(struct file *file, char __user *buf,
678 size_t count, loff_t *ppos)
680 struct hfi1_pportdata *ppd = private2ppd(file);
681 struct hfi1_devdata *dd = ppd->dd;
682 unsigned long total, csr_off;
683 u64 data;
685 if (*ppos < 0)
686 return -EINVAL;
687 /* only read 8 byte quantities */
688 if ((count % 8) != 0)
689 return -EINVAL;
690 /* offset must be 8-byte aligned */
691 if ((*ppos % 8) != 0)
692 return -EINVAL;
693 /* do nothing if out of range or zero count */
694 if (*ppos >= (LCB_END - LCB_START) || !count)
695 return 0;
696 /* reduce count if needed */
697 if (*ppos + count > LCB_END - LCB_START)
698 count = (LCB_END - LCB_START) - *ppos;
700 csr_off = LCB_START + *ppos;
701 for (total = 0; total < count; total += 8, csr_off += 8) {
702 if (read_lcb_csr(dd, csr_off, (u64 *)&data))
703 break; /* failed */
704 if (put_user(data, (unsigned long __user *)(buf + total)))
705 break;
707 *ppos += total;
708 return total;
711 static ssize_t debugfs_lcb_write(struct file *file, const char __user *buf,
712 size_t count, loff_t *ppos)
714 struct hfi1_pportdata *ppd = private2ppd(file);
715 struct hfi1_devdata *dd = ppd->dd;
716 unsigned long total, csr_off, data;
718 if (*ppos < 0)
719 return -EINVAL;
720 /* only write 8 byte quantities */
721 if ((count % 8) != 0)
722 return -EINVAL;
723 /* offset must be 8-byte aligned */
724 if ((*ppos % 8) != 0)
725 return -EINVAL;
726 /* do nothing if out of range or zero count */
727 if (*ppos >= (LCB_END - LCB_START) || !count)
728 return 0;
729 /* reduce count if needed */
730 if (*ppos + count > LCB_END - LCB_START)
731 count = (LCB_END - LCB_START) - *ppos;
733 csr_off = LCB_START + *ppos;
734 for (total = 0; total < count; total += 8, csr_off += 8) {
735 if (get_user(data, (unsigned long __user *)(buf + total)))
736 break;
737 if (write_lcb_csr(dd, csr_off, data))
738 break; /* failed */
740 *ppos += total;
741 return total;
745 * read the per-port QSFP data for ppd
747 static ssize_t qsfp_debugfs_dump(struct file *file, char __user *buf,
748 size_t count, loff_t *ppos)
750 struct hfi1_pportdata *ppd;
751 char *tmp;
752 int ret;
754 ppd = private2ppd(file);
755 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
756 if (!tmp)
757 return -ENOMEM;
759 ret = qsfp_dump(ppd, tmp, PAGE_SIZE);
760 if (ret > 0)
761 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
762 kfree(tmp);
763 return ret;
766 /* Do an i2c write operation on the chain for the given HFI. */
767 static ssize_t __i2c_debugfs_write(struct file *file, const char __user *buf,
768 size_t count, loff_t *ppos, u32 target)
770 struct hfi1_pportdata *ppd;
771 char *buff;
772 int ret;
773 int i2c_addr;
774 int offset;
775 int total_written;
777 ppd = private2ppd(file);
779 /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
780 i2c_addr = (*ppos >> 16) & 0xffff;
781 offset = *ppos & 0xffff;
783 /* explicitly reject invalid address 0 to catch cp and cat */
784 if (i2c_addr == 0)
785 return -EINVAL;
787 buff = memdup_user(buf, count);
788 if (IS_ERR(buff))
789 return PTR_ERR(buff);
791 total_written = i2c_write(ppd, target, i2c_addr, offset, buff, count);
792 if (total_written < 0) {
793 ret = total_written;
794 goto _free;
797 *ppos += total_written;
799 ret = total_written;
801 _free:
802 kfree(buff);
803 return ret;
806 /* Do an i2c write operation on chain for HFI 0. */
807 static ssize_t i2c1_debugfs_write(struct file *file, const char __user *buf,
808 size_t count, loff_t *ppos)
810 return __i2c_debugfs_write(file, buf, count, ppos, 0);
813 /* Do an i2c write operation on chain for HFI 1. */
814 static ssize_t i2c2_debugfs_write(struct file *file, const char __user *buf,
815 size_t count, loff_t *ppos)
817 return __i2c_debugfs_write(file, buf, count, ppos, 1);
820 /* Do an i2c read operation on the chain for the given HFI. */
821 static ssize_t __i2c_debugfs_read(struct file *file, char __user *buf,
822 size_t count, loff_t *ppos, u32 target)
824 struct hfi1_pportdata *ppd;
825 char *buff;
826 int ret;
827 int i2c_addr;
828 int offset;
829 int total_read;
831 ppd = private2ppd(file);
833 /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
834 i2c_addr = (*ppos >> 16) & 0xffff;
835 offset = *ppos & 0xffff;
837 /* explicitly reject invalid address 0 to catch cp and cat */
838 if (i2c_addr == 0)
839 return -EINVAL;
841 buff = kmalloc(count, GFP_KERNEL);
842 if (!buff)
843 return -ENOMEM;
845 total_read = i2c_read(ppd, target, i2c_addr, offset, buff, count);
846 if (total_read < 0) {
847 ret = total_read;
848 goto _free;
851 *ppos += total_read;
853 ret = copy_to_user(buf, buff, total_read);
854 if (ret > 0) {
855 ret = -EFAULT;
856 goto _free;
859 ret = total_read;
861 _free:
862 kfree(buff);
863 return ret;
866 /* Do an i2c read operation on chain for HFI 0. */
867 static ssize_t i2c1_debugfs_read(struct file *file, char __user *buf,
868 size_t count, loff_t *ppos)
870 return __i2c_debugfs_read(file, buf, count, ppos, 0);
873 /* Do an i2c read operation on chain for HFI 1. */
874 static ssize_t i2c2_debugfs_read(struct file *file, char __user *buf,
875 size_t count, loff_t *ppos)
877 return __i2c_debugfs_read(file, buf, count, ppos, 1);
880 /* Do a QSFP write operation on the i2c chain for the given HFI. */
881 static ssize_t __qsfp_debugfs_write(struct file *file, const char __user *buf,
882 size_t count, loff_t *ppos, u32 target)
884 struct hfi1_pportdata *ppd;
885 char *buff;
886 int ret;
887 int total_written;
889 if (*ppos + count > QSFP_PAGESIZE * 4) /* base page + page00-page03 */
890 return -EINVAL;
892 ppd = private2ppd(file);
894 buff = memdup_user(buf, count);
895 if (IS_ERR(buff))
896 return PTR_ERR(buff);
898 total_written = qsfp_write(ppd, target, *ppos, buff, count);
899 if (total_written < 0) {
900 ret = total_written;
901 goto _free;
904 *ppos += total_written;
906 ret = total_written;
908 _free:
909 kfree(buff);
910 return ret;
913 /* Do a QSFP write operation on i2c chain for HFI 0. */
914 static ssize_t qsfp1_debugfs_write(struct file *file, const char __user *buf,
915 size_t count, loff_t *ppos)
917 return __qsfp_debugfs_write(file, buf, count, ppos, 0);
920 /* Do a QSFP write operation on i2c chain for HFI 1. */
921 static ssize_t qsfp2_debugfs_write(struct file *file, const char __user *buf,
922 size_t count, loff_t *ppos)
924 return __qsfp_debugfs_write(file, buf, count, ppos, 1);
927 /* Do a QSFP read operation on the i2c chain for the given HFI. */
928 static ssize_t __qsfp_debugfs_read(struct file *file, char __user *buf,
929 size_t count, loff_t *ppos, u32 target)
931 struct hfi1_pportdata *ppd;
932 char *buff;
933 int ret;
934 int total_read;
936 if (*ppos + count > QSFP_PAGESIZE * 4) { /* base page + page00-page03 */
937 ret = -EINVAL;
938 goto _return;
941 ppd = private2ppd(file);
943 buff = kmalloc(count, GFP_KERNEL);
944 if (!buff) {
945 ret = -ENOMEM;
946 goto _return;
949 total_read = qsfp_read(ppd, target, *ppos, buff, count);
950 if (total_read < 0) {
951 ret = total_read;
952 goto _free;
955 *ppos += total_read;
957 ret = copy_to_user(buf, buff, total_read);
958 if (ret > 0) {
959 ret = -EFAULT;
960 goto _free;
963 ret = total_read;
965 _free:
966 kfree(buff);
967 _return:
968 return ret;
971 /* Do a QSFP read operation on i2c chain for HFI 0. */
972 static ssize_t qsfp1_debugfs_read(struct file *file, char __user *buf,
973 size_t count, loff_t *ppos)
975 return __qsfp_debugfs_read(file, buf, count, ppos, 0);
978 /* Do a QSFP read operation on i2c chain for HFI 1. */
979 static ssize_t qsfp2_debugfs_read(struct file *file, char __user *buf,
980 size_t count, loff_t *ppos)
982 return __qsfp_debugfs_read(file, buf, count, ppos, 1);
985 static int __i2c_debugfs_open(struct inode *in, struct file *fp, u32 target)
987 struct hfi1_pportdata *ppd;
988 int ret;
990 ppd = private2ppd(fp);
992 ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
993 if (ret) /* failed - release the module */
994 module_put(THIS_MODULE);
996 return ret;
999 static int i2c1_debugfs_open(struct inode *in, struct file *fp)
1001 return __i2c_debugfs_open(in, fp, 0);
1004 static int i2c2_debugfs_open(struct inode *in, struct file *fp)
1006 return __i2c_debugfs_open(in, fp, 1);
1009 static int __i2c_debugfs_release(struct inode *in, struct file *fp, u32 target)
1011 struct hfi1_pportdata *ppd;
1013 ppd = private2ppd(fp);
1015 release_chip_resource(ppd->dd, i2c_target(target));
1016 module_put(THIS_MODULE);
1018 return 0;
1021 static int i2c1_debugfs_release(struct inode *in, struct file *fp)
1023 return __i2c_debugfs_release(in, fp, 0);
1026 static int i2c2_debugfs_release(struct inode *in, struct file *fp)
1028 return __i2c_debugfs_release(in, fp, 1);
1031 static int __qsfp_debugfs_open(struct inode *in, struct file *fp, u32 target)
1033 struct hfi1_pportdata *ppd;
1034 int ret;
1036 if (!try_module_get(THIS_MODULE))
1037 return -ENODEV;
1039 ppd = private2ppd(fp);
1041 ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
1042 if (ret) /* failed - release the module */
1043 module_put(THIS_MODULE);
1045 return ret;
1048 static int qsfp1_debugfs_open(struct inode *in, struct file *fp)
1050 return __qsfp_debugfs_open(in, fp, 0);
1053 static int qsfp2_debugfs_open(struct inode *in, struct file *fp)
1055 return __qsfp_debugfs_open(in, fp, 1);
1058 static int __qsfp_debugfs_release(struct inode *in, struct file *fp, u32 target)
1060 struct hfi1_pportdata *ppd;
1062 ppd = private2ppd(fp);
1064 release_chip_resource(ppd->dd, i2c_target(target));
1065 module_put(THIS_MODULE);
1067 return 0;
1070 static int qsfp1_debugfs_release(struct inode *in, struct file *fp)
1072 return __qsfp_debugfs_release(in, fp, 0);
1075 static int qsfp2_debugfs_release(struct inode *in, struct file *fp)
1077 return __qsfp_debugfs_release(in, fp, 1);
1080 #define EXPROM_WRITE_ENABLE BIT_ULL(14)
1082 static bool exprom_wp_disabled;
1084 static int exprom_wp_set(struct hfi1_devdata *dd, bool disable)
1086 u64 gpio_val = 0;
1088 if (disable) {
1089 gpio_val = EXPROM_WRITE_ENABLE;
1090 exprom_wp_disabled = true;
1091 dd_dev_info(dd, "Disable Expansion ROM Write Protection\n");
1092 } else {
1093 exprom_wp_disabled = false;
1094 dd_dev_info(dd, "Enable Expansion ROM Write Protection\n");
1097 write_csr(dd, ASIC_GPIO_OUT, gpio_val);
1098 write_csr(dd, ASIC_GPIO_OE, gpio_val);
1100 return 0;
1103 static ssize_t exprom_wp_debugfs_read(struct file *file, char __user *buf,
1104 size_t count, loff_t *ppos)
1106 return 0;
1109 static ssize_t exprom_wp_debugfs_write(struct file *file,
1110 const char __user *buf, size_t count,
1111 loff_t *ppos)
1113 struct hfi1_pportdata *ppd = private2ppd(file);
1114 char cdata;
1116 if (count != 1)
1117 return -EINVAL;
1118 if (get_user(cdata, buf))
1119 return -EFAULT;
1120 if (cdata == '0')
1121 exprom_wp_set(ppd->dd, false);
1122 else if (cdata == '1')
1123 exprom_wp_set(ppd->dd, true);
1124 else
1125 return -EINVAL;
1127 return 1;
1130 static unsigned long exprom_in_use;
1132 static int exprom_wp_debugfs_open(struct inode *in, struct file *fp)
1134 if (test_and_set_bit(0, &exprom_in_use))
1135 return -EBUSY;
1137 return 0;
1140 static int exprom_wp_debugfs_release(struct inode *in, struct file *fp)
1142 struct hfi1_pportdata *ppd = private2ppd(fp);
1144 if (exprom_wp_disabled)
1145 exprom_wp_set(ppd->dd, false);
1146 clear_bit(0, &exprom_in_use);
1148 return 0;
1151 #define DEBUGFS_OPS(nm, readroutine, writeroutine) \
1153 .name = nm, \
1154 .ops = { \
1155 .owner = THIS_MODULE, \
1156 .read = readroutine, \
1157 .write = writeroutine, \
1158 .llseek = generic_file_llseek, \
1159 }, \
1162 #define DEBUGFS_XOPS(nm, readf, writef, openf, releasef) \
1164 .name = nm, \
1165 .ops = { \
1166 .owner = THIS_MODULE, \
1167 .read = readf, \
1168 .write = writef, \
1169 .llseek = generic_file_llseek, \
1170 .open = openf, \
1171 .release = releasef \
1172 }, \
1175 static const struct counter_info cntr_ops[] = {
1176 DEBUGFS_OPS("counter_names", dev_names_read, NULL),
1177 DEBUGFS_OPS("counters", dev_counters_read, NULL),
1178 DEBUGFS_OPS("portcounter_names", portnames_read, NULL),
1181 static const struct counter_info port_cntr_ops[] = {
1182 DEBUGFS_OPS("port%dcounters", portcntrs_debugfs_read, NULL),
1183 DEBUGFS_XOPS("i2c1", i2c1_debugfs_read, i2c1_debugfs_write,
1184 i2c1_debugfs_open, i2c1_debugfs_release),
1185 DEBUGFS_XOPS("i2c2", i2c2_debugfs_read, i2c2_debugfs_write,
1186 i2c2_debugfs_open, i2c2_debugfs_release),
1187 DEBUGFS_OPS("qsfp_dump%d", qsfp_debugfs_dump, NULL),
1188 DEBUGFS_XOPS("qsfp1", qsfp1_debugfs_read, qsfp1_debugfs_write,
1189 qsfp1_debugfs_open, qsfp1_debugfs_release),
1190 DEBUGFS_XOPS("qsfp2", qsfp2_debugfs_read, qsfp2_debugfs_write,
1191 qsfp2_debugfs_open, qsfp2_debugfs_release),
1192 DEBUGFS_XOPS("exprom_wp", exprom_wp_debugfs_read,
1193 exprom_wp_debugfs_write, exprom_wp_debugfs_open,
1194 exprom_wp_debugfs_release),
1195 DEBUGFS_OPS("asic_flags", asic_flags_read, asic_flags_write),
1196 DEBUGFS_OPS("dc8051_memory", dc8051_memory_read, NULL),
1197 DEBUGFS_OPS("lcb", debugfs_lcb_read, debugfs_lcb_write),
1200 static void *_sdma_cpu_list_seq_start(struct seq_file *s, loff_t *pos)
1202 if (*pos >= num_online_cpus())
1203 return NULL;
1205 return pos;
1208 static void *_sdma_cpu_list_seq_next(struct seq_file *s, void *v, loff_t *pos)
1210 ++*pos;
1211 if (*pos >= num_online_cpus())
1212 return NULL;
1214 return pos;
1217 static void _sdma_cpu_list_seq_stop(struct seq_file *s, void *v)
1219 /* nothing allocated */
1222 static int _sdma_cpu_list_seq_show(struct seq_file *s, void *v)
1224 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
1225 struct hfi1_devdata *dd = dd_from_dev(ibd);
1226 loff_t *spos = v;
1227 loff_t i = *spos;
1229 sdma_seqfile_dump_cpu_list(s, dd, (unsigned long)i);
1230 return 0;
1233 DEBUGFS_SEQ_FILE_OPS(sdma_cpu_list);
1234 DEBUGFS_SEQ_FILE_OPEN(sdma_cpu_list)
1235 DEBUGFS_FILE_OPS(sdma_cpu_list);
1237 void hfi1_dbg_ibdev_init(struct hfi1_ibdev *ibd)
1239 char name[sizeof("port0counters") + 1];
1240 char link[10];
1241 struct hfi1_devdata *dd = dd_from_dev(ibd);
1242 struct hfi1_pportdata *ppd;
1243 struct dentry *root;
1244 int unit = dd->unit;
1245 int i, j;
1247 if (!hfi1_dbg_root)
1248 return;
1249 snprintf(name, sizeof(name), "%s_%d", class_name(), unit);
1250 snprintf(link, sizeof(link), "%d", unit);
1251 root = debugfs_create_dir(name, hfi1_dbg_root);
1252 ibd->hfi1_ibdev_dbg = root;
1254 ibd->hfi1_ibdev_link =
1255 debugfs_create_symlink(link, hfi1_dbg_root, name);
1257 debugfs_create_file("opcode_stats", 0444, root, ibd,
1258 &_opcode_stats_file_ops);
1259 debugfs_create_file("tx_opcode_stats", 0444, root, ibd,
1260 &_tx_opcode_stats_file_ops);
1261 debugfs_create_file("ctx_stats", 0444, root, ibd, &_ctx_stats_file_ops);
1262 debugfs_create_file("qp_stats", 0444, root, ibd, &_qp_stats_file_ops);
1263 debugfs_create_file("sdes", 0444, root, ibd, &_sdes_file_ops);
1264 debugfs_create_file("rcds", 0444, root, ibd, &_rcds_file_ops);
1265 debugfs_create_file("pios", 0444, root, ibd, &_pios_file_ops);
1266 debugfs_create_file("sdma_cpu_list", 0444, root, ibd,
1267 &_sdma_cpu_list_file_ops);
1269 /* dev counter files */
1270 for (i = 0; i < ARRAY_SIZE(cntr_ops); i++)
1271 debugfs_create_file(cntr_ops[i].name, 0444, root, dd,
1272 &cntr_ops[i].ops);
1274 /* per port files */
1275 for (ppd = dd->pport, j = 0; j < dd->num_pports; j++, ppd++)
1276 for (i = 0; i < ARRAY_SIZE(port_cntr_ops); i++) {
1277 snprintf(name,
1278 sizeof(name),
1279 port_cntr_ops[i].name,
1280 j + 1);
1281 debugfs_create_file(name,
1282 !port_cntr_ops[i].ops.write ?
1283 S_IRUGO :
1284 S_IRUGO | S_IWUSR,
1285 root, ppd, &port_cntr_ops[i].ops);
1288 hfi1_fault_init_debugfs(ibd);
1291 void hfi1_dbg_ibdev_exit(struct hfi1_ibdev *ibd)
1293 if (!hfi1_dbg_root)
1294 goto out;
1295 hfi1_fault_exit_debugfs(ibd);
1296 debugfs_remove(ibd->hfi1_ibdev_link);
1297 debugfs_remove_recursive(ibd->hfi1_ibdev_dbg);
1298 out:
1299 ibd->hfi1_ibdev_dbg = NULL;
1303 * driver stats field names, one line per stat, single string. Used by
1304 * programs like hfistats to print the stats in a way which works for
1305 * different versions of drivers, without changing program source.
1306 * if hfi1_ib_stats changes, this needs to change. Names need to be
1307 * 12 chars or less (w/o newline), for proper display by hfistats utility.
1309 static const char * const hfi1_statnames[] = {
1310 /* must be element 0*/
1311 "KernIntr",
1312 "ErrorIntr",
1313 "Tx_Errs",
1314 "Rcv_Errs",
1315 "H/W_Errs",
1316 "NoPIOBufs",
1317 "CtxtsOpen",
1318 "RcvLen_Errs",
1319 "EgrBufFull",
1320 "EgrHdrFull"
1323 static void *_driver_stats_names_seq_start(struct seq_file *s, loff_t *pos)
1325 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1326 return NULL;
1327 return pos;
1330 static void *_driver_stats_names_seq_next(
1331 struct seq_file *s,
1332 void *v,
1333 loff_t *pos)
1335 ++*pos;
1336 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1337 return NULL;
1338 return pos;
1341 static void _driver_stats_names_seq_stop(struct seq_file *s, void *v)
1345 static int _driver_stats_names_seq_show(struct seq_file *s, void *v)
1347 loff_t *spos = v;
1349 seq_printf(s, "%s\n", hfi1_statnames[*spos]);
1350 return 0;
1353 DEBUGFS_SEQ_FILE_OPS(driver_stats_names);
1354 DEBUGFS_SEQ_FILE_OPEN(driver_stats_names)
1355 DEBUGFS_FILE_OPS(driver_stats_names);
1357 static void *_driver_stats_seq_start(struct seq_file *s, loff_t *pos)
1359 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1360 return NULL;
1361 return pos;
1364 static void *_driver_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
1366 ++*pos;
1367 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1368 return NULL;
1369 return pos;
1372 static void _driver_stats_seq_stop(struct seq_file *s, void *v)
1376 static u64 hfi1_sps_ints(void)
1378 unsigned long index, flags;
1379 struct hfi1_devdata *dd;
1380 u64 sps_ints = 0;
1382 xa_lock_irqsave(&hfi1_dev_table, flags);
1383 xa_for_each(&hfi1_dev_table, index, dd) {
1384 sps_ints += get_all_cpu_total(dd->int_counter);
1386 xa_unlock_irqrestore(&hfi1_dev_table, flags);
1387 return sps_ints;
1390 static int _driver_stats_seq_show(struct seq_file *s, void *v)
1392 loff_t *spos = v;
1393 char *buffer;
1394 u64 *stats = (u64 *)&hfi1_stats;
1395 size_t sz = seq_get_buf(s, &buffer);
1397 if (sz < sizeof(u64))
1398 return SEQ_SKIP;
1399 /* special case for interrupts */
1400 if (*spos == 0)
1401 *(u64 *)buffer = hfi1_sps_ints();
1402 else
1403 *(u64 *)buffer = stats[*spos];
1404 seq_commit(s, sizeof(u64));
1405 return 0;
1408 DEBUGFS_SEQ_FILE_OPS(driver_stats);
1409 DEBUGFS_SEQ_FILE_OPEN(driver_stats)
1410 DEBUGFS_FILE_OPS(driver_stats);
1412 void hfi1_dbg_init(void)
1414 hfi1_dbg_root = debugfs_create_dir(DRIVER_NAME, NULL);
1415 debugfs_create_file("driver_stats_names", 0444, hfi1_dbg_root, NULL,
1416 &_driver_stats_names_file_ops);
1417 debugfs_create_file("driver_stats", 0444, hfi1_dbg_root, NULL,
1418 &_driver_stats_file_ops);
1421 void hfi1_dbg_exit(void)
1423 debugfs_remove_recursive(hfi1_dbg_root);
1424 hfi1_dbg_root = NULL;