Linux Audio

Check our new training course

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