Linux Audio

Check our new training course

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