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