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