blob: dbab9d9cc288c7edb296ab25d890d3d79c93ef22 [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001#ifdef CONFIG_DEBUG_FS
2/*
Jubin John05d6ac12016-02-14 20:22:17 -08003 * Copyright(c) 2015, 2016 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04004 *
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 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040010 * 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 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040021 * 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>
Dean Luickae993e72016-03-05 08:50:27 -080052#include <linux/module.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040053
54#include "hfi.h"
55#include "debugfs.h"
56#include "device.h"
57#include "qp.h"
58#include "sdma.h"
59
60static 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) \
66static 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}
Jubin Johnf4d507c2016-02-14 20:20:25 -080072
Mike Marciniszyn77241052015-07-30 15:17:43 -040073#define DEBUGFS_SEQ_FILE_OPEN(name) \
74static 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) \
87static 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) \
96do { \
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
Mike Marciniszyn77241052015-07-30 15:17:43 -0400104#define DEBUGFS_SEQ_FILE_CREATE(name, parent, data) \
105 DEBUGFS_FILE_CREATE(#name, parent, data, &_##name##_file_ops, S_IRUGO)
106
107static 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
118static 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
Mike Marciniszyn77241052015-07-30 15:17:43 -0400128static void _opcode_stats_seq_stop(struct seq_file *s, void *v)
129__releases(RCU)
130{
131 rcu_read_unlock();
132}
133
134static 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,
Jubin John17fb4f22016-02-14 20:21:52 -0800151 (unsigned long long)n_packets,
152 (unsigned long long)n_bytes);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400153
154 return 0;
155}
156
157DEBUGFS_SEQ_FILE_OPS(opcode_stats);
158DEBUGFS_SEQ_FILE_OPEN(opcode_stats)
159DEBUGFS_FILE_OPS(opcode_stats);
160
161static 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
173static 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
187static void _ctx_stats_seq_stop(struct seq_file *s, void *v)
188{
189 /* nothing allocated */
190}
191
192static 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
221DEBUGFS_SEQ_FILE_OPS(ctx_stats);
222DEBUGFS_SEQ_FILE_OPEN(ctx_stats)
223DEBUGFS_FILE_OPS(ctx_stats);
224
225static 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
246static void *_qp_stats_seq_next(struct seq_file *s, void *iter_ptr,
Jubin John17fb4f22016-02-14 20:21:52 -0800247 loff_t *pos)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400248{
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
261static void _qp_stats_seq_stop(struct seq_file *s, void *iter_ptr)
262__releases(RCU)
263{
264 rcu_read_unlock();
265}
266
267static 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
279DEBUGFS_SEQ_FILE_OPS(qp_stats);
280DEBUGFS_SEQ_FILE_OPEN(qp_stats)
281DEBUGFS_FILE_OPS(qp_stats);
282
283static 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
297static 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
Mike Marciniszyn77241052015-07-30 15:17:43 -0400308static void _sdes_seq_stop(struct seq_file *s, void *v)
309__releases(RCU)
310{
311 rcu_read_unlock();
312}
313
314static 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
325DEBUGFS_SEQ_FILE_OPS(sdes);
326DEBUGFS_SEQ_FILE_OPEN(sdes)
327DEBUGFS_FILE_OPS(sdes);
328
329/* read the per-device counters */
330static 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);
Dean Luick582e05c2016-02-18 11:13:01 -0800340 avail = hfi1_read_cntrs(dd, NULL, &counters);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400341 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 */
347static 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);
Dean Luick582e05c2016-02-18 11:13:01 -0800357 avail = hfi1_read_cntrs(dd, &names, NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400358 rval = simple_read_from_buffer(buf, count, ppos, names, avail);
359 rcu_read_unlock();
360 return rval;
361}
362
363struct 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) */
374static 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);
Dean Luick582e05c2016-02-18 11:13:01 -0800384 avail = hfi1_read_portcntrs(dd->pport, &names, NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400385 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 */
391static ssize_t portcntrs_debugfs_read(struct file *file, char __user *buf,
Jubin John17fb4f22016-02-14 20:21:52 -0800392 size_t count, loff_t *ppos)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400393{
394 u64 *counters;
395 size_t avail;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400396 struct hfi1_pportdata *ppd;
397 ssize_t rval;
398
399 rcu_read_lock();
400 ppd = private2ppd(file);
Dean Luick582e05c2016-02-18 11:13:01 -0800401 avail = hfi1_read_portcntrs(ppd, NULL, &counters);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400402 rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
403 rcu_read_unlock();
404 return rval;
405}
406
Dean Luickc9c8ea32016-03-05 08:50:33 -0800407static 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
421static 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
474static 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
Mike Marciniszyn77241052015-07-30 15:17:43 -0400531/*
532 * read the per-port QSFP data for ppd
533 */
534static ssize_t qsfp_debugfs_dump(struct file *file, char __user *buf,
Jubin John17fb4f22016-02-14 20:21:52 -0800535 size_t count, loff_t *ppos)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400536{
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. */
558static ssize_t __i2c_debugfs_write(struct file *file, const char __user *buf,
Jubin John17fb4f22016-02-14 20:21:52 -0800559 size_t count, loff_t *ppos, u32 target)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400560{
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
Dean Luick7b476222016-02-18 11:12:51 -0800571 /* 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
Mike Marciniszyn77241052015-07-30 15:17:43 -0400581 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
Mike Marciniszyn77241052015-07-30 15:17:43 -0400593 total_written = i2c_write(ppd, target, i2c_addr, offset, buff, count);
594 if (total_written < 0) {
595 ret = total_written;
Dean Luickae993e72016-03-05 08:50:27 -0800596 goto _free;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400597 }
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. */
611static ssize_t i2c1_debugfs_write(struct file *file, const char __user *buf,
Jubin John17fb4f22016-02-14 20:21:52 -0800612 size_t count, loff_t *ppos)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400613{
614 return __i2c_debugfs_write(file, buf, count, ppos, 0);
615}
616
617/* Do an i2c write operation on chain for HFI 1. */
618static ssize_t i2c2_debugfs_write(struct file *file, const char __user *buf,
Jubin John17fb4f22016-02-14 20:21:52 -0800619 size_t count, loff_t *ppos)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400620{
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. */
625static ssize_t __i2c_debugfs_read(struct file *file, char __user *buf,
Jubin John17fb4f22016-02-14 20:21:52 -0800626 size_t count, loff_t *ppos, u32 target)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400627{
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
Dean Luick7b476222016-02-18 11:12:51 -0800638 /* 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
Mike Marciniszyn77241052015-07-30 15:17:43 -0400648 buff = kmalloc(count, GFP_KERNEL);
649 if (!buff) {
650 ret = -ENOMEM;
651 goto _return;
652 }
653
Mike Marciniszyn77241052015-07-30 15:17:43 -0400654 total_read = i2c_read(ppd, target, i2c_addr, offset, buff, count);
655 if (total_read < 0) {
656 ret = total_read;
Dean Luickae993e72016-03-05 08:50:27 -0800657 goto _free;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400658 }
659
660 *ppos += total_read;
661
662 ret = copy_to_user(buf, buff, total_read);
663 if (ret > 0) {
664 ret = -EFAULT;
Dean Luickae993e72016-03-05 08:50:27 -0800665 goto _free;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400666 }
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. */
678static ssize_t i2c1_debugfs_read(struct file *file, char __user *buf,
Jubin John17fb4f22016-02-14 20:21:52 -0800679 size_t count, loff_t *ppos)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400680{
681 return __i2c_debugfs_read(file, buf, count, ppos, 0);
682}
683
684/* Do an i2c read operation on chain for HFI 1. */
685static ssize_t i2c2_debugfs_read(struct file *file, char __user *buf,
Jubin John17fb4f22016-02-14 20:21:52 -0800686 size_t count, loff_t *ppos)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400687{
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. */
692static ssize_t __qsfp_debugfs_write(struct file *file, const char __user *buf,
Jubin John17fb4f22016-02-14 20:21:52 -0800693 size_t count, loff_t *ppos, u32 target)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400694{
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
Dean Luickae993e72016-03-05 08:50:27 -0800720 total_written = qsfp_write(ppd, target, *ppos, buff, count);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400721 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. */
738static ssize_t qsfp1_debugfs_write(struct file *file, const char __user *buf,
Jubin John17fb4f22016-02-14 20:21:52 -0800739 size_t count, loff_t *ppos)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400740{
741 return __qsfp_debugfs_write(file, buf, count, ppos, 0);
742}
743
744/* Do a QSFP write operation on i2c chain for HFI 1. */
745static ssize_t qsfp2_debugfs_write(struct file *file, const char __user *buf,
Jubin John17fb4f22016-02-14 20:21:52 -0800746 size_t count, loff_t *ppos)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400747{
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. */
752static ssize_t __qsfp_debugfs_read(struct file *file, char __user *buf,
Jubin John17fb4f22016-02-14 20:21:52 -0800753 size_t count, loff_t *ppos, u32 target)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400754{
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
Dean Luickae993e72016-03-05 08:50:27 -0800774 total_read = qsfp_read(ppd, target, *ppos, buff, count);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400775 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. */
798static ssize_t qsfp1_debugfs_read(struct file *file, char __user *buf,
Jubin John17fb4f22016-02-14 20:21:52 -0800799 size_t count, loff_t *ppos)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400800{
801 return __qsfp_debugfs_read(file, buf, count, ppos, 0);
802}
803
804/* Do a QSFP read operation on i2c chain for HFI 1. */
805static ssize_t qsfp2_debugfs_read(struct file *file, char __user *buf,
Jubin John17fb4f22016-02-14 20:21:52 -0800806 size_t count, loff_t *ppos)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400807{
808 return __qsfp_debugfs_read(file, buf, count, ppos, 1);
809}
810
Dean Luickae993e72016-03-05 08:50:27 -0800811static 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
828static int i2c1_debugfs_open(struct inode *in, struct file *fp)
829{
830 return __i2c_debugfs_open(in, fp, 0);
831}
832
833static int i2c2_debugfs_open(struct inode *in, struct file *fp)
834{
835 return __i2c_debugfs_open(in, fp, 1);
836}
837
838static 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
850static int i2c1_debugfs_release(struct inode *in, struct file *fp)
851{
852 return __i2c_debugfs_release(in, fp, 0);
853}
854
855static int i2c2_debugfs_release(struct inode *in, struct file *fp)
856{
857 return __i2c_debugfs_release(in, fp, 1);
858}
859
860static 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
877static int qsfp1_debugfs_open(struct inode *in, struct file *fp)
878{
879 return __qsfp_debugfs_open(in, fp, 0);
880}
881
882static int qsfp2_debugfs_open(struct inode *in, struct file *fp)
883{
884 return __qsfp_debugfs_open(in, fp, 1);
885}
886
887static 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
899static int qsfp1_debugfs_release(struct inode *in, struct file *fp)
900{
901 return __qsfp_debugfs_release(in, fp, 0);
902}
903
904static int qsfp2_debugfs_release(struct inode *in, struct file *fp)
905{
906 return __qsfp_debugfs_release(in, fp, 1);
907}
908
Mike Marciniszyn77241052015-07-30 15:17:43 -0400909#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
Dean Luickae993e72016-03-05 08:50:27 -0800919#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
Mike Marciniszyn77241052015-07-30 15:17:43 -0400931static 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
937static const struct counter_info port_cntr_ops[] = {
938 DEBUGFS_OPS("port%dcounters", portcntrs_debugfs_read, NULL),
Dean Luickae993e72016-03-05 08:50:27 -0800939 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),
Mike Marciniszyn77241052015-07-30 15:17:43 -0400943 DEBUGFS_OPS("qsfp_dump%d", qsfp_debugfs_dump, NULL),
Dean Luickae993e72016-03-05 08:50:27 -0800944 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),
Dean Luickc9c8ea32016-03-05 08:50:33 -0800948 DEBUGFS_OPS("asic_flags", asic_flags_read, asic_flags_write),
Mike Marciniszyn77241052015-07-30 15:17:43 -0400949};
950
951void 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,
Jubin Johnd125a6c2016-02-14 20:19:49 -0800996 !port_cntr_ops[i].ops.write ?
Jubin John8638b772016-02-14 20:19:24 -0800997 S_IRUGO : S_IRUGO | S_IWUSR);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400998 }
999}
1000
1001void 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);
1007out:
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 */
1019static 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
1033static 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
1042static 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
1053static void _driver_stats_names_seq_stop(struct seq_file *s, void *v)
1054__releases(RCU)
1055{
1056 rcu_read_unlock();
1057}
1058
1059static 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
1067DEBUGFS_SEQ_FILE_OPS(driver_stats_names);
1068DEBUGFS_SEQ_FILE_OPEN(driver_stats_names)
1069DEBUGFS_FILE_OPS(driver_stats_names);
1070
1071static 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
1080static 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
1088static void _driver_stats_seq_stop(struct seq_file *s, void *v)
1089__releases(RCU)
1090{
1091 rcu_read_unlock();
1092}
1093
1094static 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
1108static 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
1126DEBUGFS_SEQ_FILE_OPS(driver_stats);
1127DEBUGFS_SEQ_FILE_OPEN(driver_stats)
1128DEBUGFS_FILE_OPS(driver_stats);
1129
1130void 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
1139void hfi1_dbg_exit(void)
1140{
1141 debugfs_remove_recursive(hfi1_dbg_root);
1142 hfi1_dbg_root = NULL;
1143}
1144
1145#endif