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