1 /*
2  * Copyright(c) 2015-2017 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 
64 static struct dentry *hfi1_dbg_root;
65 
66 /* wrappers to enforce srcu in seq file */
67 static ssize_t hfi1_seq_read(
68 	struct file *file,
69 	char __user *buf,
70 	size_t size,
71 	loff_t *ppos)
72 {
73 	struct dentry *d = file->f_path.dentry;
74 	int srcu_idx;
75 	ssize_t r;
76 
77 	r = debugfs_use_file_start(d, &srcu_idx);
78 	if (likely(!r))
79 		r = seq_read(file, buf, size, ppos);
80 	debugfs_use_file_finish(srcu_idx);
81 	return r;
82 }
83 
84 static loff_t hfi1_seq_lseek(
85 	struct file *file,
86 	loff_t offset,
87 	int whence)
88 {
89 	struct dentry *d = file->f_path.dentry;
90 	int srcu_idx;
91 	loff_t r;
92 
93 	r = debugfs_use_file_start(d, &srcu_idx);
94 	if (likely(!r))
95 		r = seq_lseek(file, offset, whence);
96 	debugfs_use_file_finish(srcu_idx);
97 	return r;
98 }
99 
100 #define private2dd(file) (file_inode(file)->i_private)
101 #define private2ppd(file) (file_inode(file)->i_private)
102 
103 #define DEBUGFS_SEQ_FILE_OPS(name) \
104 static const struct seq_operations _##name##_seq_ops = { \
105 	.start = _##name##_seq_start, \
106 	.next  = _##name##_seq_next, \
107 	.stop  = _##name##_seq_stop, \
108 	.show  = _##name##_seq_show \
109 }
110 
111 #define DEBUGFS_SEQ_FILE_OPEN(name) \
112 static int _##name##_open(struct inode *inode, struct file *s) \
113 { \
114 	struct seq_file *seq; \
115 	int ret; \
116 	ret =  seq_open(s, &_##name##_seq_ops); \
117 	if (ret) \
118 		return ret; \
119 	seq = s->private_data; \
120 	seq->private = inode->i_private; \
121 	return 0; \
122 }
123 
124 #define DEBUGFS_FILE_OPS(name) \
125 static const struct file_operations _##name##_file_ops = { \
126 	.owner   = THIS_MODULE, \
127 	.open    = _##name##_open, \
128 	.read    = hfi1_seq_read, \
129 	.llseek  = hfi1_seq_lseek, \
130 	.release = seq_release \
131 }
132 
133 #define DEBUGFS_FILE_CREATE(name, parent, data, ops, mode)	\
134 do { \
135 	struct dentry *ent; \
136 	ent = debugfs_create_file(name, mode, parent, \
137 		data, ops); \
138 	if (!ent) \
139 		pr_warn("create of %s failed\n", name); \
140 } while (0)
141 
142 #define DEBUGFS_SEQ_FILE_CREATE(name, parent, data) \
143 	DEBUGFS_FILE_CREATE(#name, parent, data, &_##name##_file_ops, S_IRUGO)
144 
145 static void *_opcode_stats_seq_start(struct seq_file *s, loff_t *pos)
146 {
147 	struct hfi1_opcode_stats_perctx *opstats;
148 
149 	if (*pos >= ARRAY_SIZE(opstats->stats))
150 		return NULL;
151 	return pos;
152 }
153 
154 static void *_opcode_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
155 {
156 	struct hfi1_opcode_stats_perctx *opstats;
157 
158 	++*pos;
159 	if (*pos >= ARRAY_SIZE(opstats->stats))
160 		return NULL;
161 	return pos;
162 }
163 
164 static void _opcode_stats_seq_stop(struct seq_file *s, void *v)
165 {
166 }
167 
168 static int _opcode_stats_seq_show(struct seq_file *s, void *v)
169 {
170 	loff_t *spos = v;
171 	loff_t i = *spos, j;
172 	u64 n_packets = 0, n_bytes = 0;
173 	struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
174 	struct hfi1_devdata *dd = dd_from_dev(ibd);
175 	struct hfi1_ctxtdata *rcd;
176 
177 	for (j = 0; j < dd->first_dyn_alloc_ctxt; j++) {
178 		rcd = hfi1_rcd_get_by_index(dd, j);
179 		if (rcd) {
180 			n_packets += rcd->opstats->stats[i].n_packets;
181 			n_bytes += rcd->opstats->stats[i].n_bytes;
182 		}
183 		hfi1_rcd_put(rcd);
184 	}
185 	if (!n_packets && !n_bytes)
186 		return SEQ_SKIP;
187 	seq_printf(s, "%02llx %llu/%llu\n", i,
188 		   (unsigned long long)n_packets,
189 		   (unsigned long long)n_bytes);
190 
191 	return 0;
192 }
193 
194 DEBUGFS_SEQ_FILE_OPS(opcode_stats);
195 DEBUGFS_SEQ_FILE_OPEN(opcode_stats)
196 DEBUGFS_FILE_OPS(opcode_stats);
197 
198 static void *_ctx_stats_seq_start(struct seq_file *s, loff_t *pos)
199 {
200 	struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
201 	struct hfi1_devdata *dd = dd_from_dev(ibd);
202 
203 	if (!*pos)
204 		return SEQ_START_TOKEN;
205 	if (*pos >= dd->first_dyn_alloc_ctxt)
206 		return NULL;
207 	return pos;
208 }
209 
210 static void *_ctx_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
211 {
212 	struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
213 	struct hfi1_devdata *dd = dd_from_dev(ibd);
214 
215 	if (v == SEQ_START_TOKEN)
216 		return pos;
217 
218 	++*pos;
219 	if (*pos >= dd->first_dyn_alloc_ctxt)
220 		return NULL;
221 	return pos;
222 }
223 
224 static void _ctx_stats_seq_stop(struct seq_file *s, void *v)
225 {
226 	/* nothing allocated */
227 }
228 
229 static int _ctx_stats_seq_show(struct seq_file *s, void *v)
230 {
231 	loff_t *spos;
232 	loff_t i, j;
233 	u64 n_packets = 0;
234 	struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
235 	struct hfi1_devdata *dd = dd_from_dev(ibd);
236 	struct hfi1_ctxtdata *rcd;
237 
238 	if (v == SEQ_START_TOKEN) {
239 		seq_puts(s, "Ctx:npkts\n");
240 		return 0;
241 	}
242 
243 	spos = v;
244 	i = *spos;
245 
246 	rcd = hfi1_rcd_get_by_index(dd, i);
247 	if (!rcd)
248 		return SEQ_SKIP;
249 
250 	for (j = 0; j < ARRAY_SIZE(rcd->opstats->stats); j++)
251 		n_packets += rcd->opstats->stats[j].n_packets;
252 
253 	hfi1_rcd_put(rcd);
254 
255 	if (!n_packets)
256 		return SEQ_SKIP;
257 
258 	seq_printf(s, "  %llu:%llu\n", i, n_packets);
259 	return 0;
260 }
261 
262 DEBUGFS_SEQ_FILE_OPS(ctx_stats);
263 DEBUGFS_SEQ_FILE_OPEN(ctx_stats)
264 DEBUGFS_FILE_OPS(ctx_stats);
265 
266 static void *_qp_stats_seq_start(struct seq_file *s, loff_t *pos)
267 	__acquires(RCU)
268 {
269 	struct rvt_qp_iter *iter;
270 	loff_t n = *pos;
271 
272 	iter = rvt_qp_iter_init(s->private, 0, NULL);
273 
274 	/* stop calls rcu_read_unlock */
275 	rcu_read_lock();
276 
277 	if (!iter)
278 		return NULL;
279 
280 	do {
281 		if (rvt_qp_iter_next(iter)) {
282 			kfree(iter);
283 			return NULL;
284 		}
285 	} while (n--);
286 
287 	return iter;
288 }
289 
290 static void *_qp_stats_seq_next(struct seq_file *s, void *iter_ptr,
291 				loff_t *pos)
292 	__must_hold(RCU)
293 {
294 	struct rvt_qp_iter *iter = iter_ptr;
295 
296 	(*pos)++;
297 
298 	if (rvt_qp_iter_next(iter)) {
299 		kfree(iter);
300 		return NULL;
301 	}
302 
303 	return iter;
304 }
305 
306 static void _qp_stats_seq_stop(struct seq_file *s, void *iter_ptr)
307 	__releases(RCU)
308 {
309 	rcu_read_unlock();
310 }
311 
312 static int _qp_stats_seq_show(struct seq_file *s, void *iter_ptr)
313 {
314 	struct rvt_qp_iter *iter = iter_ptr;
315 
316 	if (!iter)
317 		return 0;
318 
319 	qp_iter_print(s, iter);
320 
321 	return 0;
322 }
323 
324 DEBUGFS_SEQ_FILE_OPS(qp_stats);
325 DEBUGFS_SEQ_FILE_OPEN(qp_stats)
326 DEBUGFS_FILE_OPS(qp_stats);
327 
328 static void *_sdes_seq_start(struct seq_file *s, loff_t *pos)
329 {
330 	struct hfi1_ibdev *ibd;
331 	struct hfi1_devdata *dd;
332 
333 	ibd = (struct hfi1_ibdev *)s->private;
334 	dd = dd_from_dev(ibd);
335 	if (!dd->per_sdma || *pos >= dd->num_sdma)
336 		return NULL;
337 	return pos;
338 }
339 
340 static void *_sdes_seq_next(struct seq_file *s, void *v, loff_t *pos)
341 {
342 	struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
343 	struct hfi1_devdata *dd = dd_from_dev(ibd);
344 
345 	++*pos;
346 	if (!dd->per_sdma || *pos >= dd->num_sdma)
347 		return NULL;
348 	return pos;
349 }
350 
351 static void _sdes_seq_stop(struct seq_file *s, void *v)
352 {
353 }
354 
355 static int _sdes_seq_show(struct seq_file *s, void *v)
356 {
357 	struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
358 	struct hfi1_devdata *dd = dd_from_dev(ibd);
359 	loff_t *spos = v;
360 	loff_t i = *spos;
361 
362 	sdma_seqfile_dump_sde(s, &dd->per_sdma[i]);
363 	return 0;
364 }
365 
366 DEBUGFS_SEQ_FILE_OPS(sdes);
367 DEBUGFS_SEQ_FILE_OPEN(sdes)
368 DEBUGFS_FILE_OPS(sdes);
369 
370 static void *_rcds_seq_start(struct seq_file *s, loff_t *pos)
371 {
372 	struct hfi1_ibdev *ibd;
373 	struct hfi1_devdata *dd;
374 
375 	ibd = (struct hfi1_ibdev *)s->private;
376 	dd = dd_from_dev(ibd);
377 	if (!dd->rcd || *pos >= dd->n_krcv_queues)
378 		return NULL;
379 	return pos;
380 }
381 
382 static void *_rcds_seq_next(struct seq_file *s, void *v, loff_t *pos)
383 {
384 	struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
385 	struct hfi1_devdata *dd = dd_from_dev(ibd);
386 
387 	++*pos;
388 	if (!dd->rcd || *pos >= dd->n_krcv_queues)
389 		return NULL;
390 	return pos;
391 }
392 
393 static void _rcds_seq_stop(struct seq_file *s, void *v)
394 {
395 }
396 
397 static int _rcds_seq_show(struct seq_file *s, void *v)
398 {
399 	struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
400 	struct hfi1_devdata *dd = dd_from_dev(ibd);
401 	struct hfi1_ctxtdata *rcd;
402 	loff_t *spos = v;
403 	loff_t i = *spos;
404 
405 	rcd = hfi1_rcd_get_by_index(dd, i);
406 	if (rcd)
407 		seqfile_dump_rcd(s, rcd);
408 	hfi1_rcd_put(rcd);
409 	return 0;
410 }
411 
412 DEBUGFS_SEQ_FILE_OPS(rcds);
413 DEBUGFS_SEQ_FILE_OPEN(rcds)
414 DEBUGFS_FILE_OPS(rcds);
415 
416 /* read the per-device counters */
417 static ssize_t dev_counters_read(struct file *file, char __user *buf,
418 				 size_t count, loff_t *ppos)
419 {
420 	u64 *counters;
421 	size_t avail;
422 	struct hfi1_devdata *dd;
423 	ssize_t rval;
424 
425 	dd = private2dd(file);
426 	avail = hfi1_read_cntrs(dd, NULL, &counters);
427 	rval =  simple_read_from_buffer(buf, count, ppos, counters, avail);
428 	return rval;
429 }
430 
431 /* read the per-device counters */
432 static ssize_t dev_names_read(struct file *file, char __user *buf,
433 			      size_t count, loff_t *ppos)
434 {
435 	char *names;
436 	size_t avail;
437 	struct hfi1_devdata *dd;
438 	ssize_t rval;
439 
440 	dd = private2dd(file);
441 	avail = hfi1_read_cntrs(dd, &names, NULL);
442 	rval =  simple_read_from_buffer(buf, count, ppos, names, avail);
443 	return rval;
444 }
445 
446 struct counter_info {
447 	char *name;
448 	const struct file_operations ops;
449 };
450 
451 /*
452  * Could use file_inode(file)->i_ino to figure out which file,
453  * instead of separate routine for each, but for now, this works...
454  */
455 
456 /* read the per-port names (same for each port) */
457 static ssize_t portnames_read(struct file *file, char __user *buf,
458 			      size_t count, loff_t *ppos)
459 {
460 	char *names;
461 	size_t avail;
462 	struct hfi1_devdata *dd;
463 	ssize_t rval;
464 
465 	dd = private2dd(file);
466 	avail = hfi1_read_portcntrs(dd->pport, &names, NULL);
467 	rval = simple_read_from_buffer(buf, count, ppos, names, avail);
468 	return rval;
469 }
470 
471 /* read the per-port counters */
472 static ssize_t portcntrs_debugfs_read(struct file *file, char __user *buf,
473 				      size_t count, loff_t *ppos)
474 {
475 	u64 *counters;
476 	size_t avail;
477 	struct hfi1_pportdata *ppd;
478 	ssize_t rval;
479 
480 	ppd = private2ppd(file);
481 	avail = hfi1_read_portcntrs(ppd, NULL, &counters);
482 	rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
483 	return rval;
484 }
485 
486 static void check_dyn_flag(u64 scratch0, char *p, int size, int *used,
487 			   int this_hfi, int hfi, u32 flag, const char *what)
488 {
489 	u32 mask;
490 
491 	mask = flag << (hfi ? CR_DYN_SHIFT : 0);
492 	if (scratch0 & mask) {
493 		*used += scnprintf(p + *used, size - *used,
494 				   "  0x%08x - HFI%d %s in use, %s device\n",
495 				   mask, hfi, what,
496 				   this_hfi == hfi ? "this" : "other");
497 	}
498 }
499 
500 static ssize_t asic_flags_read(struct file *file, char __user *buf,
501 			       size_t count, loff_t *ppos)
502 {
503 	struct hfi1_pportdata *ppd;
504 	struct hfi1_devdata *dd;
505 	u64 scratch0;
506 	char *tmp;
507 	int ret = 0;
508 	int size;
509 	int used;
510 	int i;
511 
512 	ppd = private2ppd(file);
513 	dd = ppd->dd;
514 	size = PAGE_SIZE;
515 	used = 0;
516 	tmp = kmalloc(size, GFP_KERNEL);
517 	if (!tmp)
518 		return -ENOMEM;
519 
520 	scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
521 	used += scnprintf(tmp + used, size - used,
522 			  "Resource flags: 0x%016llx\n", scratch0);
523 
524 	/* check permanent flag */
525 	if (scratch0 & CR_THERM_INIT) {
526 		used += scnprintf(tmp + used, size - used,
527 				  "  0x%08x - thermal monitoring initialized\n",
528 				  (u32)CR_THERM_INIT);
529 	}
530 
531 	/* check each dynamic flag on each HFI */
532 	for (i = 0; i < 2; i++) {
533 		check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
534 			       CR_SBUS, "SBus");
535 		check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
536 			       CR_EPROM, "EPROM");
537 		check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
538 			       CR_I2C1, "i2c chain 1");
539 		check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
540 			       CR_I2C2, "i2c chain 2");
541 	}
542 	used += scnprintf(tmp + used, size - used, "Write bits to clear\n");
543 
544 	ret = simple_read_from_buffer(buf, count, ppos, tmp, used);
545 	kfree(tmp);
546 	return ret;
547 }
548 
549 static ssize_t asic_flags_write(struct file *file, const char __user *buf,
550 				size_t count, loff_t *ppos)
551 {
552 	struct hfi1_pportdata *ppd;
553 	struct hfi1_devdata *dd;
554 	char *buff;
555 	int ret;
556 	unsigned long long value;
557 	u64 scratch0;
558 	u64 clear;
559 
560 	ppd = private2ppd(file);
561 	dd = ppd->dd;
562 
563 	/* zero terminate and read the expected integer */
564 	buff = memdup_user_nul(buf, count);
565 	if (IS_ERR(buff))
566 		return PTR_ERR(buff);
567 
568 	ret = kstrtoull(buff, 0, &value);
569 	if (ret)
570 		goto do_free;
571 	clear = value;
572 
573 	/* obtain exclusive access */
574 	mutex_lock(&dd->asic_data->asic_resource_mutex);
575 	acquire_hw_mutex(dd);
576 
577 	scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
578 	scratch0 &= ~clear;
579 	write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
580 	/* force write to be visible to other HFI on another OS */
581 	(void)read_csr(dd, ASIC_CFG_SCRATCH);
582 
583 	release_hw_mutex(dd);
584 	mutex_unlock(&dd->asic_data->asic_resource_mutex);
585 
586 	/* return the number of bytes written */
587 	ret = count;
588 
589  do_free:
590 	kfree(buff);
591 	return ret;
592 }
593 
594 /* read the dc8051 memory */
595 static ssize_t dc8051_memory_read(struct file *file, char __user *buf,
596 				  size_t count, loff_t *ppos)
597 {
598 	struct hfi1_pportdata *ppd = private2ppd(file);
599 	ssize_t rval;
600 	void *tmp;
601 	loff_t start, end;
602 
603 	/* the checks below expect the position to be positive */
604 	if (*ppos < 0)
605 		return -EINVAL;
606 
607 	tmp = kzalloc(DC8051_DATA_MEM_SIZE, GFP_KERNEL);
608 	if (!tmp)
609 		return -ENOMEM;
610 
611 	/*
612 	 * Fill in the requested portion of the temporary buffer from the
613 	 * 8051 memory.  The 8051 memory read is done in terms of 8 bytes.
614 	 * Adjust start and end to fit.  Skip reading anything if out of
615 	 * range.
616 	 */
617 	start = *ppos & ~0x7;	/* round down */
618 	if (start < DC8051_DATA_MEM_SIZE) {
619 		end = (*ppos + count + 7) & ~0x7; /* round up */
620 		if (end > DC8051_DATA_MEM_SIZE)
621 			end = DC8051_DATA_MEM_SIZE;
622 		rval = read_8051_data(ppd->dd, start, end - start,
623 				      (u64 *)(tmp + start));
624 		if (rval)
625 			goto done;
626 	}
627 
628 	rval = simple_read_from_buffer(buf, count, ppos, tmp,
629 				       DC8051_DATA_MEM_SIZE);
630 done:
631 	kfree(tmp);
632 	return rval;
633 }
634 
635 static ssize_t debugfs_lcb_read(struct file *file, char __user *buf,
636 				size_t count, loff_t *ppos)
637 {
638 	struct hfi1_pportdata *ppd = private2ppd(file);
639 	struct hfi1_devdata *dd = ppd->dd;
640 	unsigned long total, csr_off;
641 	u64 data;
642 
643 	if (*ppos < 0)
644 		return -EINVAL;
645 	/* only read 8 byte quantities */
646 	if ((count % 8) != 0)
647 		return -EINVAL;
648 	/* offset must be 8-byte aligned */
649 	if ((*ppos % 8) != 0)
650 		return -EINVAL;
651 	/* do nothing if out of range or zero count */
652 	if (*ppos >= (LCB_END - LCB_START) || !count)
653 		return 0;
654 	/* reduce count if needed */
655 	if (*ppos + count > LCB_END - LCB_START)
656 		count = (LCB_END - LCB_START) - *ppos;
657 
658 	csr_off = LCB_START + *ppos;
659 	for (total = 0; total < count; total += 8, csr_off += 8) {
660 		if (read_lcb_csr(dd, csr_off, (u64 *)&data))
661 			break; /* failed */
662 		if (put_user(data, (unsigned long __user *)(buf + total)))
663 			break;
664 	}
665 	*ppos += total;
666 	return total;
667 }
668 
669 static ssize_t debugfs_lcb_write(struct file *file, const char __user *buf,
670 				 size_t count, loff_t *ppos)
671 {
672 	struct hfi1_pportdata *ppd = private2ppd(file);
673 	struct hfi1_devdata *dd = ppd->dd;
674 	unsigned long total, csr_off, data;
675 
676 	if (*ppos < 0)
677 		return -EINVAL;
678 	/* only write 8 byte quantities */
679 	if ((count % 8) != 0)
680 		return -EINVAL;
681 	/* offset must be 8-byte aligned */
682 	if ((*ppos % 8) != 0)
683 		return -EINVAL;
684 	/* do nothing if out of range or zero count */
685 	if (*ppos >= (LCB_END - LCB_START) || !count)
686 		return 0;
687 	/* reduce count if needed */
688 	if (*ppos + count > LCB_END - LCB_START)
689 		count = (LCB_END - LCB_START) - *ppos;
690 
691 	csr_off = LCB_START + *ppos;
692 	for (total = 0; total < count; total += 8, csr_off += 8) {
693 		if (get_user(data, (unsigned long __user *)(buf + total)))
694 			break;
695 		if (write_lcb_csr(dd, csr_off, data))
696 			break; /* failed */
697 	}
698 	*ppos += total;
699 	return total;
700 }
701 
702 /*
703  * read the per-port QSFP data for ppd
704  */
705 static ssize_t qsfp_debugfs_dump(struct file *file, char __user *buf,
706 				 size_t count, loff_t *ppos)
707 {
708 	struct hfi1_pportdata *ppd;
709 	char *tmp;
710 	int ret;
711 
712 	ppd = private2ppd(file);
713 	tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
714 	if (!tmp)
715 		return -ENOMEM;
716 
717 	ret = qsfp_dump(ppd, tmp, PAGE_SIZE);
718 	if (ret > 0)
719 		ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
720 	kfree(tmp);
721 	return ret;
722 }
723 
724 /* Do an i2c write operation on the chain for the given HFI. */
725 static ssize_t __i2c_debugfs_write(struct file *file, const char __user *buf,
726 				   size_t count, loff_t *ppos, u32 target)
727 {
728 	struct hfi1_pportdata *ppd;
729 	char *buff;
730 	int ret;
731 	int i2c_addr;
732 	int offset;
733 	int total_written;
734 
735 	ppd = private2ppd(file);
736 
737 	/* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
738 	i2c_addr = (*ppos >> 16) & 0xffff;
739 	offset = *ppos & 0xffff;
740 
741 	/* explicitly reject invalid address 0 to catch cp and cat */
742 	if (i2c_addr == 0)
743 		return -EINVAL;
744 
745 	buff = memdup_user(buf, count);
746 	if (IS_ERR(buff))
747 		return PTR_ERR(buff);
748 
749 	total_written = i2c_write(ppd, target, i2c_addr, offset, buff, count);
750 	if (total_written < 0) {
751 		ret = total_written;
752 		goto _free;
753 	}
754 
755 	*ppos += total_written;
756 
757 	ret = total_written;
758 
759  _free:
760 	kfree(buff);
761 	return ret;
762 }
763 
764 /* Do an i2c write operation on chain for HFI 0. */
765 static ssize_t i2c1_debugfs_write(struct file *file, const char __user *buf,
766 				  size_t count, loff_t *ppos)
767 {
768 	return __i2c_debugfs_write(file, buf, count, ppos, 0);
769 }
770 
771 /* Do an i2c write operation on chain for HFI 1. */
772 static ssize_t i2c2_debugfs_write(struct file *file, const char __user *buf,
773 				  size_t count, loff_t *ppos)
774 {
775 	return __i2c_debugfs_write(file, buf, count, ppos, 1);
776 }
777 
778 /* Do an i2c read operation on the chain for the given HFI. */
779 static ssize_t __i2c_debugfs_read(struct file *file, char __user *buf,
780 				  size_t count, loff_t *ppos, u32 target)
781 {
782 	struct hfi1_pportdata *ppd;
783 	char *buff;
784 	int ret;
785 	int i2c_addr;
786 	int offset;
787 	int total_read;
788 
789 	ppd = private2ppd(file);
790 
791 	/* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
792 	i2c_addr = (*ppos >> 16) & 0xffff;
793 	offset = *ppos & 0xffff;
794 
795 	/* explicitly reject invalid address 0 to catch cp and cat */
796 	if (i2c_addr == 0)
797 		return -EINVAL;
798 
799 	buff = kmalloc(count, GFP_KERNEL);
800 	if (!buff)
801 		return -ENOMEM;
802 
803 	total_read = i2c_read(ppd, target, i2c_addr, offset, buff, count);
804 	if (total_read < 0) {
805 		ret = total_read;
806 		goto _free;
807 	}
808 
809 	*ppos += total_read;
810 
811 	ret = copy_to_user(buf, buff, total_read);
812 	if (ret > 0) {
813 		ret = -EFAULT;
814 		goto _free;
815 	}
816 
817 	ret = total_read;
818 
819  _free:
820 	kfree(buff);
821 	return ret;
822 }
823 
824 /* Do an i2c read operation on chain for HFI 0. */
825 static ssize_t i2c1_debugfs_read(struct file *file, char __user *buf,
826 				 size_t count, loff_t *ppos)
827 {
828 	return __i2c_debugfs_read(file, buf, count, ppos, 0);
829 }
830 
831 /* Do an i2c read operation on chain for HFI 1. */
832 static ssize_t i2c2_debugfs_read(struct file *file, char __user *buf,
833 				 size_t count, loff_t *ppos)
834 {
835 	return __i2c_debugfs_read(file, buf, count, ppos, 1);
836 }
837 
838 /* Do a QSFP write operation on the i2c chain for the given HFI. */
839 static ssize_t __qsfp_debugfs_write(struct file *file, const char __user *buf,
840 				    size_t count, loff_t *ppos, u32 target)
841 {
842 	struct hfi1_pportdata *ppd;
843 	char *buff;
844 	int ret;
845 	int total_written;
846 
847 	if (*ppos + count > QSFP_PAGESIZE * 4) /* base page + page00-page03 */
848 		return -EINVAL;
849 
850 	ppd = private2ppd(file);
851 
852 	buff = memdup_user(buf, count);
853 	if (IS_ERR(buff))
854 		return PTR_ERR(buff);
855 
856 	total_written = qsfp_write(ppd, target, *ppos, buff, count);
857 	if (total_written < 0) {
858 		ret = total_written;
859 		goto _free;
860 	}
861 
862 	*ppos += total_written;
863 
864 	ret = total_written;
865 
866  _free:
867 	kfree(buff);
868 	return ret;
869 }
870 
871 /* Do a QSFP write operation on i2c chain for HFI 0. */
872 static ssize_t qsfp1_debugfs_write(struct file *file, const char __user *buf,
873 				   size_t count, loff_t *ppos)
874 {
875 	return __qsfp_debugfs_write(file, buf, count, ppos, 0);
876 }
877 
878 /* Do a QSFP write operation on i2c chain for HFI 1. */
879 static ssize_t qsfp2_debugfs_write(struct file *file, const char __user *buf,
880 				   size_t count, loff_t *ppos)
881 {
882 	return __qsfp_debugfs_write(file, buf, count, ppos, 1);
883 }
884 
885 /* Do a QSFP read operation on the i2c chain for the given HFI. */
886 static ssize_t __qsfp_debugfs_read(struct file *file, char __user *buf,
887 				   size_t count, loff_t *ppos, u32 target)
888 {
889 	struct hfi1_pportdata *ppd;
890 	char *buff;
891 	int ret;
892 	int total_read;
893 
894 	if (*ppos + count > QSFP_PAGESIZE * 4) { /* base page + page00-page03 */
895 		ret = -EINVAL;
896 		goto _return;
897 	}
898 
899 	ppd = private2ppd(file);
900 
901 	buff = kmalloc(count, GFP_KERNEL);
902 	if (!buff) {
903 		ret = -ENOMEM;
904 		goto _return;
905 	}
906 
907 	total_read = qsfp_read(ppd, target, *ppos, buff, count);
908 	if (total_read < 0) {
909 		ret = total_read;
910 		goto _free;
911 	}
912 
913 	*ppos += total_read;
914 
915 	ret = copy_to_user(buf, buff, total_read);
916 	if (ret > 0) {
917 		ret = -EFAULT;
918 		goto _free;
919 	}
920 
921 	ret = total_read;
922 
923  _free:
924 	kfree(buff);
925  _return:
926 	return ret;
927 }
928 
929 /* Do a QSFP read operation on i2c chain for HFI 0. */
930 static ssize_t qsfp1_debugfs_read(struct file *file, char __user *buf,
931 				  size_t count, loff_t *ppos)
932 {
933 	return __qsfp_debugfs_read(file, buf, count, ppos, 0);
934 }
935 
936 /* Do a QSFP read operation on i2c chain for HFI 1. */
937 static ssize_t qsfp2_debugfs_read(struct file *file, char __user *buf,
938 				  size_t count, loff_t *ppos)
939 {
940 	return __qsfp_debugfs_read(file, buf, count, ppos, 1);
941 }
942 
943 static int __i2c_debugfs_open(struct inode *in, struct file *fp, u32 target)
944 {
945 	struct hfi1_pportdata *ppd;
946 	int ret;
947 
948 	if (!try_module_get(THIS_MODULE))
949 		return -ENODEV;
950 
951 	ppd = private2ppd(fp);
952 
953 	ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
954 	if (ret) /* failed - release the module */
955 		module_put(THIS_MODULE);
956 
957 	return ret;
958 }
959 
960 static int i2c1_debugfs_open(struct inode *in, struct file *fp)
961 {
962 	return __i2c_debugfs_open(in, fp, 0);
963 }
964 
965 static int i2c2_debugfs_open(struct inode *in, struct file *fp)
966 {
967 	return __i2c_debugfs_open(in, fp, 1);
968 }
969 
970 static int __i2c_debugfs_release(struct inode *in, struct file *fp, u32 target)
971 {
972 	struct hfi1_pportdata *ppd;
973 
974 	ppd = private2ppd(fp);
975 
976 	release_chip_resource(ppd->dd, i2c_target(target));
977 	module_put(THIS_MODULE);
978 
979 	return 0;
980 }
981 
982 static int i2c1_debugfs_release(struct inode *in, struct file *fp)
983 {
984 	return __i2c_debugfs_release(in, fp, 0);
985 }
986 
987 static int i2c2_debugfs_release(struct inode *in, struct file *fp)
988 {
989 	return __i2c_debugfs_release(in, fp, 1);
990 }
991 
992 static int __qsfp_debugfs_open(struct inode *in, struct file *fp, u32 target)
993 {
994 	struct hfi1_pportdata *ppd;
995 	int ret;
996 
997 	if (!try_module_get(THIS_MODULE))
998 		return -ENODEV;
999 
1000 	ppd = private2ppd(fp);
1001 
1002 	ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
1003 	if (ret) /* failed - release the module */
1004 		module_put(THIS_MODULE);
1005 
1006 	return ret;
1007 }
1008 
1009 static int qsfp1_debugfs_open(struct inode *in, struct file *fp)
1010 {
1011 	return __qsfp_debugfs_open(in, fp, 0);
1012 }
1013 
1014 static int qsfp2_debugfs_open(struct inode *in, struct file *fp)
1015 {
1016 	return __qsfp_debugfs_open(in, fp, 1);
1017 }
1018 
1019 static int __qsfp_debugfs_release(struct inode *in, struct file *fp, u32 target)
1020 {
1021 	struct hfi1_pportdata *ppd;
1022 
1023 	ppd = private2ppd(fp);
1024 
1025 	release_chip_resource(ppd->dd, i2c_target(target));
1026 	module_put(THIS_MODULE);
1027 
1028 	return 0;
1029 }
1030 
1031 static int qsfp1_debugfs_release(struct inode *in, struct file *fp)
1032 {
1033 	return __qsfp_debugfs_release(in, fp, 0);
1034 }
1035 
1036 static int qsfp2_debugfs_release(struct inode *in, struct file *fp)
1037 {
1038 	return __qsfp_debugfs_release(in, fp, 1);
1039 }
1040 
1041 #define DEBUGFS_OPS(nm, readroutine, writeroutine)	\
1042 { \
1043 	.name = nm, \
1044 	.ops = { \
1045 		.read = readroutine, \
1046 		.write = writeroutine, \
1047 		.llseek = generic_file_llseek, \
1048 	}, \
1049 }
1050 
1051 #define DEBUGFS_XOPS(nm, readf, writef, openf, releasef) \
1052 { \
1053 	.name = nm, \
1054 	.ops = { \
1055 		.read = readf, \
1056 		.write = writef, \
1057 		.llseek = generic_file_llseek, \
1058 		.open = openf, \
1059 		.release = releasef \
1060 	}, \
1061 }
1062 
1063 static const struct counter_info cntr_ops[] = {
1064 	DEBUGFS_OPS("counter_names", dev_names_read, NULL),
1065 	DEBUGFS_OPS("counters", dev_counters_read, NULL),
1066 	DEBUGFS_OPS("portcounter_names", portnames_read, NULL),
1067 };
1068 
1069 static const struct counter_info port_cntr_ops[] = {
1070 	DEBUGFS_OPS("port%dcounters", portcntrs_debugfs_read, NULL),
1071 	DEBUGFS_XOPS("i2c1", i2c1_debugfs_read, i2c1_debugfs_write,
1072 		     i2c1_debugfs_open, i2c1_debugfs_release),
1073 	DEBUGFS_XOPS("i2c2", i2c2_debugfs_read, i2c2_debugfs_write,
1074 		     i2c2_debugfs_open, i2c2_debugfs_release),
1075 	DEBUGFS_OPS("qsfp_dump%d", qsfp_debugfs_dump, NULL),
1076 	DEBUGFS_XOPS("qsfp1", qsfp1_debugfs_read, qsfp1_debugfs_write,
1077 		     qsfp1_debugfs_open, qsfp1_debugfs_release),
1078 	DEBUGFS_XOPS("qsfp2", qsfp2_debugfs_read, qsfp2_debugfs_write,
1079 		     qsfp2_debugfs_open, qsfp2_debugfs_release),
1080 	DEBUGFS_OPS("asic_flags", asic_flags_read, asic_flags_write),
1081 	DEBUGFS_OPS("dc8051_memory", dc8051_memory_read, NULL),
1082 	DEBUGFS_OPS("lcb", debugfs_lcb_read, debugfs_lcb_write),
1083 };
1084 
1085 static void *_sdma_cpu_list_seq_start(struct seq_file *s, loff_t *pos)
1086 {
1087 	if (*pos >= num_online_cpus())
1088 		return NULL;
1089 
1090 	return pos;
1091 }
1092 
1093 static void *_sdma_cpu_list_seq_next(struct seq_file *s, void *v, loff_t *pos)
1094 {
1095 	++*pos;
1096 	if (*pos >= num_online_cpus())
1097 		return NULL;
1098 
1099 	return pos;
1100 }
1101 
1102 static void _sdma_cpu_list_seq_stop(struct seq_file *s, void *v)
1103 {
1104 	/* nothing allocated */
1105 }
1106 
1107 static int _sdma_cpu_list_seq_show(struct seq_file *s, void *v)
1108 {
1109 	struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
1110 	struct hfi1_devdata *dd = dd_from_dev(ibd);
1111 	loff_t *spos = v;
1112 	loff_t i = *spos;
1113 
1114 	sdma_seqfile_dump_cpu_list(s, dd, (unsigned long)i);
1115 	return 0;
1116 }
1117 
1118 DEBUGFS_SEQ_FILE_OPS(sdma_cpu_list);
1119 DEBUGFS_SEQ_FILE_OPEN(sdma_cpu_list)
1120 DEBUGFS_FILE_OPS(sdma_cpu_list);
1121 
1122 #ifdef CONFIG_FAULT_INJECTION
1123 static void *_fault_stats_seq_start(struct seq_file *s, loff_t *pos)
1124 {
1125 	struct hfi1_opcode_stats_perctx *opstats;
1126 
1127 	if (*pos >= ARRAY_SIZE(opstats->stats))
1128 		return NULL;
1129 	return pos;
1130 }
1131 
1132 static void *_fault_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
1133 {
1134 	struct hfi1_opcode_stats_perctx *opstats;
1135 
1136 	++*pos;
1137 	if (*pos >= ARRAY_SIZE(opstats->stats))
1138 		return NULL;
1139 	return pos;
1140 }
1141 
1142 static void _fault_stats_seq_stop(struct seq_file *s, void *v)
1143 {
1144 }
1145 
1146 static int _fault_stats_seq_show(struct seq_file *s, void *v)
1147 {
1148 	loff_t *spos = v;
1149 	loff_t i = *spos, j;
1150 	u64 n_packets = 0, n_bytes = 0;
1151 	struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
1152 	struct hfi1_devdata *dd = dd_from_dev(ibd);
1153 	struct hfi1_ctxtdata *rcd;
1154 
1155 	for (j = 0; j < dd->first_dyn_alloc_ctxt; j++) {
1156 		rcd = hfi1_rcd_get_by_index(dd, j);
1157 		if (rcd) {
1158 			n_packets += rcd->opstats->stats[i].n_packets;
1159 			n_bytes += rcd->opstats->stats[i].n_bytes;
1160 		}
1161 		hfi1_rcd_put(rcd);
1162 	}
1163 	if (!n_packets && !n_bytes)
1164 		return SEQ_SKIP;
1165 	if (!ibd->fault_opcode->n_rxfaults[i] &&
1166 	    !ibd->fault_opcode->n_txfaults[i])
1167 		return SEQ_SKIP;
1168 	seq_printf(s, "%02llx %llu/%llu (faults rx:%llu faults: tx:%llu)\n", i,
1169 		   (unsigned long long)n_packets,
1170 		   (unsigned long long)n_bytes,
1171 		   (unsigned long long)ibd->fault_opcode->n_rxfaults[i],
1172 		   (unsigned long long)ibd->fault_opcode->n_txfaults[i]);
1173 	return 0;
1174 }
1175 
1176 DEBUGFS_SEQ_FILE_OPS(fault_stats);
1177 DEBUGFS_SEQ_FILE_OPEN(fault_stats);
1178 DEBUGFS_FILE_OPS(fault_stats);
1179 
1180 static void fault_exit_opcode_debugfs(struct hfi1_ibdev *ibd)
1181 {
1182 	debugfs_remove_recursive(ibd->fault_opcode->dir);
1183 	kfree(ibd->fault_opcode);
1184 	ibd->fault_opcode = NULL;
1185 }
1186 
1187 static int fault_init_opcode_debugfs(struct hfi1_ibdev *ibd)
1188 {
1189 	struct dentry *parent = ibd->hfi1_ibdev_dbg;
1190 
1191 	ibd->fault_opcode = kzalloc(sizeof(*ibd->fault_opcode), GFP_KERNEL);
1192 	if (!ibd->fault_opcode)
1193 		return -ENOMEM;
1194 
1195 	ibd->fault_opcode->attr.interval = 1;
1196 	ibd->fault_opcode->attr.require_end = ULONG_MAX;
1197 	ibd->fault_opcode->attr.stacktrace_depth = 32;
1198 	ibd->fault_opcode->attr.dname = NULL;
1199 	ibd->fault_opcode->attr.verbose = 0;
1200 	ibd->fault_opcode->fault_by_opcode = false;
1201 	ibd->fault_opcode->opcode = 0;
1202 	ibd->fault_opcode->mask = 0xff;
1203 
1204 	ibd->fault_opcode->dir =
1205 		fault_create_debugfs_attr("fault_opcode",
1206 					  parent,
1207 					  &ibd->fault_opcode->attr);
1208 	if (IS_ERR(ibd->fault_opcode->dir)) {
1209 		kfree(ibd->fault_opcode);
1210 		return -ENOENT;
1211 	}
1212 
1213 	DEBUGFS_SEQ_FILE_CREATE(fault_stats, ibd->fault_opcode->dir, ibd);
1214 	if (!debugfs_create_bool("fault_by_opcode", 0600,
1215 				 ibd->fault_opcode->dir,
1216 				 &ibd->fault_opcode->fault_by_opcode))
1217 		goto fail;
1218 	if (!debugfs_create_x8("opcode", 0600, ibd->fault_opcode->dir,
1219 			       &ibd->fault_opcode->opcode))
1220 		goto fail;
1221 	if (!debugfs_create_x8("mask", 0600, ibd->fault_opcode->dir,
1222 			       &ibd->fault_opcode->mask))
1223 		goto fail;
1224 
1225 	return 0;
1226 fail:
1227 	fault_exit_opcode_debugfs(ibd);
1228 	return -ENOMEM;
1229 }
1230 
1231 static void fault_exit_packet_debugfs(struct hfi1_ibdev *ibd)
1232 {
1233 	debugfs_remove_recursive(ibd->fault_packet->dir);
1234 	kfree(ibd->fault_packet);
1235 	ibd->fault_packet = NULL;
1236 }
1237 
1238 static int fault_init_packet_debugfs(struct hfi1_ibdev *ibd)
1239 {
1240 	struct dentry *parent = ibd->hfi1_ibdev_dbg;
1241 
1242 	ibd->fault_packet = kzalloc(sizeof(*ibd->fault_packet), GFP_KERNEL);
1243 	if (!ibd->fault_packet)
1244 		return -ENOMEM;
1245 
1246 	ibd->fault_packet->attr.interval = 1;
1247 	ibd->fault_packet->attr.require_end = ULONG_MAX;
1248 	ibd->fault_packet->attr.stacktrace_depth = 32;
1249 	ibd->fault_packet->attr.dname = NULL;
1250 	ibd->fault_packet->attr.verbose = 0;
1251 	ibd->fault_packet->fault_by_packet = false;
1252 
1253 	ibd->fault_packet->dir =
1254 		fault_create_debugfs_attr("fault_packet",
1255 					  parent,
1256 					  &ibd->fault_opcode->attr);
1257 	if (IS_ERR(ibd->fault_packet->dir)) {
1258 		kfree(ibd->fault_packet);
1259 		return -ENOENT;
1260 	}
1261 
1262 	if (!debugfs_create_bool("fault_by_packet", 0600,
1263 				 ibd->fault_packet->dir,
1264 				 &ibd->fault_packet->fault_by_packet))
1265 		goto fail;
1266 	if (!debugfs_create_u64("fault_stats", 0400,
1267 				ibd->fault_packet->dir,
1268 				&ibd->fault_packet->n_faults))
1269 		goto fail;
1270 
1271 	return 0;
1272 fail:
1273 	fault_exit_packet_debugfs(ibd);
1274 	return -ENOMEM;
1275 }
1276 
1277 static void fault_exit_debugfs(struct hfi1_ibdev *ibd)
1278 {
1279 	fault_exit_opcode_debugfs(ibd);
1280 	fault_exit_packet_debugfs(ibd);
1281 }
1282 
1283 static int fault_init_debugfs(struct hfi1_ibdev *ibd)
1284 {
1285 	int ret = 0;
1286 
1287 	ret = fault_init_opcode_debugfs(ibd);
1288 	if (ret)
1289 		return ret;
1290 
1291 	ret = fault_init_packet_debugfs(ibd);
1292 	if (ret)
1293 		fault_exit_opcode_debugfs(ibd);
1294 
1295 	return ret;
1296 }
1297 
1298 bool hfi1_dbg_fault_suppress_err(struct hfi1_ibdev *ibd)
1299 {
1300 	return ibd->fault_suppress_err;
1301 }
1302 
1303 bool hfi1_dbg_fault_opcode(struct rvt_qp *qp, u32 opcode, bool rx)
1304 {
1305 	bool ret = false;
1306 	struct hfi1_ibdev *ibd = to_idev(qp->ibqp.device);
1307 
1308 	if (!ibd->fault_opcode || !ibd->fault_opcode->fault_by_opcode)
1309 		return false;
1310 	if (ibd->fault_opcode->opcode != (opcode & ibd->fault_opcode->mask))
1311 		return false;
1312 	ret = should_fail(&ibd->fault_opcode->attr, 1);
1313 	if (ret) {
1314 		trace_hfi1_fault_opcode(qp, opcode);
1315 		if (rx)
1316 			ibd->fault_opcode->n_rxfaults[opcode]++;
1317 		else
1318 			ibd->fault_opcode->n_txfaults[opcode]++;
1319 	}
1320 	return ret;
1321 }
1322 
1323 bool hfi1_dbg_fault_packet(struct hfi1_packet *packet)
1324 {
1325 	struct rvt_dev_info *rdi = &packet->rcd->ppd->dd->verbs_dev.rdi;
1326 	struct hfi1_ibdev *ibd = dev_from_rdi(rdi);
1327 	bool ret = false;
1328 
1329 	if (!ibd->fault_packet || !ibd->fault_packet->fault_by_packet)
1330 		return false;
1331 
1332 	ret = should_fail(&ibd->fault_packet->attr, 1);
1333 	if (ret) {
1334 		++ibd->fault_packet->n_faults;
1335 		trace_hfi1_fault_packet(packet);
1336 	}
1337 	return ret;
1338 }
1339 #endif
1340 
1341 void hfi1_dbg_ibdev_init(struct hfi1_ibdev *ibd)
1342 {
1343 	char name[sizeof("port0counters") + 1];
1344 	char link[10];
1345 	struct hfi1_devdata *dd = dd_from_dev(ibd);
1346 	struct hfi1_pportdata *ppd;
1347 	int unit = dd->unit;
1348 	int i, j;
1349 
1350 	if (!hfi1_dbg_root)
1351 		return;
1352 	snprintf(name, sizeof(name), "%s_%d", class_name(), unit);
1353 	snprintf(link, sizeof(link), "%d", unit);
1354 	ibd->hfi1_ibdev_dbg = debugfs_create_dir(name, hfi1_dbg_root);
1355 	if (!ibd->hfi1_ibdev_dbg) {
1356 		pr_warn("create of %s failed\n", name);
1357 		return;
1358 	}
1359 	ibd->hfi1_ibdev_link =
1360 		debugfs_create_symlink(link, hfi1_dbg_root, name);
1361 	if (!ibd->hfi1_ibdev_link) {
1362 		pr_warn("create of %s symlink failed\n", name);
1363 		return;
1364 	}
1365 	DEBUGFS_SEQ_FILE_CREATE(opcode_stats, ibd->hfi1_ibdev_dbg, ibd);
1366 	DEBUGFS_SEQ_FILE_CREATE(ctx_stats, ibd->hfi1_ibdev_dbg, ibd);
1367 	DEBUGFS_SEQ_FILE_CREATE(qp_stats, ibd->hfi1_ibdev_dbg, ibd);
1368 	DEBUGFS_SEQ_FILE_CREATE(sdes, ibd->hfi1_ibdev_dbg, ibd);
1369 	DEBUGFS_SEQ_FILE_CREATE(rcds, ibd->hfi1_ibdev_dbg, ibd);
1370 	DEBUGFS_SEQ_FILE_CREATE(sdma_cpu_list, ibd->hfi1_ibdev_dbg, ibd);
1371 	/* dev counter files */
1372 	for (i = 0; i < ARRAY_SIZE(cntr_ops); i++)
1373 		DEBUGFS_FILE_CREATE(cntr_ops[i].name,
1374 				    ibd->hfi1_ibdev_dbg,
1375 				    dd,
1376 				    &cntr_ops[i].ops, S_IRUGO);
1377 	/* per port files */
1378 	for (ppd = dd->pport, j = 0; j < dd->num_pports; j++, ppd++)
1379 		for (i = 0; i < ARRAY_SIZE(port_cntr_ops); i++) {
1380 			snprintf(name,
1381 				 sizeof(name),
1382 				 port_cntr_ops[i].name,
1383 				 j + 1);
1384 			DEBUGFS_FILE_CREATE(name,
1385 					    ibd->hfi1_ibdev_dbg,
1386 					    ppd,
1387 					    &port_cntr_ops[i].ops,
1388 					    !port_cntr_ops[i].ops.write ?
1389 					    S_IRUGO : S_IRUGO | S_IWUSR);
1390 		}
1391 
1392 #ifdef CONFIG_FAULT_INJECTION
1393 	debugfs_create_bool("fault_suppress_err", 0600,
1394 			    ibd->hfi1_ibdev_dbg,
1395 			    &ibd->fault_suppress_err);
1396 	fault_init_debugfs(ibd);
1397 #endif
1398 }
1399 
1400 void hfi1_dbg_ibdev_exit(struct hfi1_ibdev *ibd)
1401 {
1402 	if (!hfi1_dbg_root)
1403 		goto out;
1404 #ifdef CONFIG_FAULT_INJECTION
1405 	fault_exit_debugfs(ibd);
1406 #endif
1407 	debugfs_remove(ibd->hfi1_ibdev_link);
1408 	debugfs_remove_recursive(ibd->hfi1_ibdev_dbg);
1409 out:
1410 	ibd->hfi1_ibdev_dbg = NULL;
1411 }
1412 
1413 /*
1414  * driver stats field names, one line per stat, single string.  Used by
1415  * programs like hfistats to print the stats in a way which works for
1416  * different versions of drivers, without changing program source.
1417  * if hfi1_ib_stats changes, this needs to change.  Names need to be
1418  * 12 chars or less (w/o newline), for proper display by hfistats utility.
1419  */
1420 static const char * const hfi1_statnames[] = {
1421 	/* must be element 0*/
1422 	"KernIntr",
1423 	"ErrorIntr",
1424 	"Tx_Errs",
1425 	"Rcv_Errs",
1426 	"H/W_Errs",
1427 	"NoPIOBufs",
1428 	"CtxtsOpen",
1429 	"RcvLen_Errs",
1430 	"EgrBufFull",
1431 	"EgrHdrFull"
1432 };
1433 
1434 static void *_driver_stats_names_seq_start(struct seq_file *s, loff_t *pos)
1435 {
1436 	if (*pos >= ARRAY_SIZE(hfi1_statnames))
1437 		return NULL;
1438 	return pos;
1439 }
1440 
1441 static void *_driver_stats_names_seq_next(
1442 	struct seq_file *s,
1443 	void *v,
1444 	loff_t *pos)
1445 {
1446 	++*pos;
1447 	if (*pos >= ARRAY_SIZE(hfi1_statnames))
1448 		return NULL;
1449 	return pos;
1450 }
1451 
1452 static void _driver_stats_names_seq_stop(struct seq_file *s, void *v)
1453 {
1454 }
1455 
1456 static int _driver_stats_names_seq_show(struct seq_file *s, void *v)
1457 {
1458 	loff_t *spos = v;
1459 
1460 	seq_printf(s, "%s\n", hfi1_statnames[*spos]);
1461 	return 0;
1462 }
1463 
1464 DEBUGFS_SEQ_FILE_OPS(driver_stats_names);
1465 DEBUGFS_SEQ_FILE_OPEN(driver_stats_names)
1466 DEBUGFS_FILE_OPS(driver_stats_names);
1467 
1468 static void *_driver_stats_seq_start(struct seq_file *s, loff_t *pos)
1469 {
1470 	if (*pos >= ARRAY_SIZE(hfi1_statnames))
1471 		return NULL;
1472 	return pos;
1473 }
1474 
1475 static void *_driver_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
1476 {
1477 	++*pos;
1478 	if (*pos >= ARRAY_SIZE(hfi1_statnames))
1479 		return NULL;
1480 	return pos;
1481 }
1482 
1483 static void _driver_stats_seq_stop(struct seq_file *s, void *v)
1484 {
1485 }
1486 
1487 static u64 hfi1_sps_ints(void)
1488 {
1489 	unsigned long flags;
1490 	struct hfi1_devdata *dd;
1491 	u64 sps_ints = 0;
1492 
1493 	spin_lock_irqsave(&hfi1_devs_lock, flags);
1494 	list_for_each_entry(dd, &hfi1_dev_list, list) {
1495 		sps_ints += get_all_cpu_total(dd->int_counter);
1496 	}
1497 	spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1498 	return sps_ints;
1499 }
1500 
1501 static int _driver_stats_seq_show(struct seq_file *s, void *v)
1502 {
1503 	loff_t *spos = v;
1504 	char *buffer;
1505 	u64 *stats = (u64 *)&hfi1_stats;
1506 	size_t sz = seq_get_buf(s, &buffer);
1507 
1508 	if (sz < sizeof(u64))
1509 		return SEQ_SKIP;
1510 	/* special case for interrupts */
1511 	if (*spos == 0)
1512 		*(u64 *)buffer = hfi1_sps_ints();
1513 	else
1514 		*(u64 *)buffer = stats[*spos];
1515 	seq_commit(s,  sizeof(u64));
1516 	return 0;
1517 }
1518 
1519 DEBUGFS_SEQ_FILE_OPS(driver_stats);
1520 DEBUGFS_SEQ_FILE_OPEN(driver_stats)
1521 DEBUGFS_FILE_OPS(driver_stats);
1522 
1523 void hfi1_dbg_init(void)
1524 {
1525 	hfi1_dbg_root  = debugfs_create_dir(DRIVER_NAME, NULL);
1526 	if (!hfi1_dbg_root)
1527 		pr_warn("init of debugfs failed\n");
1528 	DEBUGFS_SEQ_FILE_CREATE(driver_stats_names, hfi1_dbg_root, NULL);
1529 	DEBUGFS_SEQ_FILE_CREATE(driver_stats, hfi1_dbg_root, NULL);
1530 }
1531 
1532 void hfi1_dbg_exit(void)
1533 {
1534 	debugfs_remove_recursive(hfi1_dbg_root);
1535 	hfi1_dbg_root = NULL;
1536 }
1537