lpfc_debugfs.c 190.8 KB
Newer Older
J
James Smart 已提交
1 2 3
/*******************************************************************
 * This file is part of the Emulex Linux Device Driver for         *
 * Fibre Channel Host Bus Adapters.                                *
4
 * Copyright (C) 2017-2021 Broadcom. All Rights Reserved. The term *
5
 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  *
J
James Smart 已提交
6
 * Copyright (C) 2007-2015 Emulex.  All rights reserved.           *
J
James Smart 已提交
7
 * EMULEX and SLI are trademarks of Emulex.                        *
J
James Smart 已提交
8
 * www.broadcom.com                                                *
J
James Smart 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *                                                                 *
 * This program is free software; you can redistribute it and/or   *
 * modify it under the terms of version 2 of the GNU General       *
 * Public License as published by the Free Software Foundation.    *
 * This program is distributed in the hope that it will be useful. *
 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
 * more details, a copy of which can be found in the file COPYING  *
 * included with this package.                                     *
 *******************************************************************/

#include <linux/blkdev.h>
#include <linux/delay.h>
25
#include <linux/module.h>
J
James Smart 已提交
26 27 28 29
#include <linux/dma-mapping.h>
#include <linux/idr.h>
#include <linux/interrupt.h>
#include <linux/kthread.h>
30
#include <linux/slab.h>
J
James Smart 已提交
31 32 33
#include <linux/pci.h>
#include <linux/spinlock.h>
#include <linux/ctype.h>
34
#include <linux/vmalloc.h>
J
James Smart 已提交
35 36 37 38 39

#include <scsi/scsi.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_transport_fc.h>
40 41
#include <scsi/fc/fc_fs.h>

42
#include "lpfc_hw4.h"
J
James Smart 已提交
43 44
#include "lpfc_hw.h"
#include "lpfc_sli.h"
45
#include "lpfc_sli4.h"
46
#include "lpfc_nl.h"
J
James Smart 已提交
47 48
#include "lpfc_disc.h"
#include "lpfc.h"
49 50
#include "lpfc_scsi.h"
#include "lpfc_nvme.h"
J
James Smart 已提交
51 52 53 54
#include "lpfc_logmsg.h"
#include "lpfc_crtn.h"
#include "lpfc_vport.h"
#include "lpfc_version.h"
55
#include "lpfc_compat.h"
J
James Smart 已提交
56
#include "lpfc_debugfs.h"
57
#include "lpfc_bsg.h"
J
James Smart 已提交
58

59
#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
60
/*
61
 * debugfs interface
J
James Smart 已提交
62 63
 *
 * To access this interface the user should:
64
 * # mount -t debugfs none /sys/kernel/debug
J
James Smart 已提交
65
 *
66
 * The lpfc debugfs directory hierarchy is:
67 68
 * /sys/kernel/debug/lpfc/fnX/vportY
 * where X is the lpfc hba function unique_id
J
James Smart 已提交
69 70 71 72 73 74
 * where Y is the vport VPI on that hba
 *
 * Debugging services available per vport:
 * discovery_trace
 * This is an ACSII readable file that contains a trace of the last
 * lpfc_debugfs_max_disc_trc events that happened on a specific vport.
75 76
 * See lpfc_debugfs.h for different categories of  discovery events.
 * To enable the discovery trace, the following module parameters must be set:
J
James Smart 已提交
77 78 79 80 81
 * lpfc_debugfs_enable=1         Turns on lpfc debugfs filesystem support
 * lpfc_debugfs_max_disc_trc=X   Where X is the event trace depth for
 *                               EACH vport. X MUST also be a power of 2.
 * lpfc_debugfs_mask_disc_trc=Y  Where Y is an event mask as defined in
 *                               lpfc_debugfs.h .
82 83 84 85 86 87 88 89
 *
 * slow_ring_trace
 * This is an ACSII readable file that contains a trace of the last
 * lpfc_debugfs_max_slow_ring_trc events that happened on a specific HBA.
 * To enable the slow ring trace, the following module parameters must be set:
 * lpfc_debugfs_enable=1         Turns on lpfc debugfs filesystem support
 * lpfc_debugfs_max_slow_ring_trc=X   Where X is the event trace depth for
 *                               the HBA. X MUST also be a power of 2.
J
James Smart 已提交
90
 */
91
static int lpfc_debugfs_enable = 1;
92
module_param(lpfc_debugfs_enable, int, S_IRUGO);
J
James Smart 已提交
93 94
MODULE_PARM_DESC(lpfc_debugfs_enable, "Enable debugfs services");

95
/* This MUST be a power of 2 */
96
static int lpfc_debugfs_max_disc_trc;
97
module_param(lpfc_debugfs_max_disc_trc, int, S_IRUGO);
J
James Smart 已提交
98 99 100
MODULE_PARM_DESC(lpfc_debugfs_max_disc_trc,
	"Set debugfs discovery trace depth");

101
/* This MUST be a power of 2 */
102
static int lpfc_debugfs_max_slow_ring_trc;
103
module_param(lpfc_debugfs_max_slow_ring_trc, int, S_IRUGO);
104 105 106
MODULE_PARM_DESC(lpfc_debugfs_max_slow_ring_trc,
	"Set debugfs slow ring trace depth");

107 108 109 110 111 112
/* This MUST be a power of 2 */
static int lpfc_debugfs_max_nvmeio_trc;
module_param(lpfc_debugfs_max_nvmeio_trc, int, 0444);
MODULE_PARM_DESC(lpfc_debugfs_max_nvmeio_trc,
		 "Set debugfs NVME IO trace depth");

113
static int lpfc_debugfs_mask_disc_trc;
114
module_param(lpfc_debugfs_mask_disc_trc, int, S_IRUGO);
J
James Smart 已提交
115 116 117 118 119
MODULE_PARM_DESC(lpfc_debugfs_mask_disc_trc,
	"Set debugfs discovery trace mask");

#include <linux/debugfs.h>

J
James Smart 已提交
120 121
static atomic_t lpfc_debugfs_seq_trc_cnt = ATOMIC_INIT(0);
static unsigned long lpfc_debugfs_start_time = 0L;
J
James Smart 已提交
122

123 124 125
/* iDiag */
static struct lpfc_idiag idiag;

126
/**
127
 * lpfc_debugfs_disc_trc_data - Dump discovery logging to a buffer
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
 * @vport: The vport to gather the log info from.
 * @buf: The buffer to dump log into.
 * @size: The maximum amount of data to process.
 *
 * Description:
 * This routine gathers the lpfc discovery debugfs data from the @vport and
 * dumps it to @buf up to @size number of bytes. It will start at the next entry
 * in the log and process the log until the end of the buffer. Then it will
 * gather from the beginning of the log and process until the current entry.
 *
 * Notes:
 * Discovery logging will be disabled while while this routine dumps the log.
 *
 * Return Value:
 * This routine returns the amount of bytes that were dumped into @buf and will
 * not exceed @size.
 **/
J
James Smart 已提交
145 146 147 148 149
static int
lpfc_debugfs_disc_trc_data(struct lpfc_vport *vport, char *buf, int size)
{
	int i, index, len, enable;
	uint32_t ms;
150
	struct lpfc_debugfs_trc *dtp;
151 152 153 154 155
	char *buffer;

	buffer = kmalloc(LPFC_DEBUG_TRC_ENTRY_SIZE, GFP_KERNEL);
	if (!buffer)
		return 0;
J
James Smart 已提交
156 157 158 159 160 161 162 163 164 165 166 167

	enable = lpfc_debugfs_enable;
	lpfc_debugfs_enable = 0;

	len = 0;
	index = (atomic_read(&vport->disc_trc_cnt) + 1) &
		(lpfc_debugfs_max_disc_trc - 1);
	for (i = index; i < lpfc_debugfs_max_disc_trc; i++) {
		dtp = vport->disc_trc + i;
		if (!dtp->fmt)
			continue;
		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
168 169
		snprintf(buffer,
			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
J
James Smart 已提交
170
			dtp->seq_cnt, ms, dtp->fmt);
171
		len +=  scnprintf(buf+len, size-len, buffer,
J
James Smart 已提交
172 173 174 175 176 177 178
			dtp->data1, dtp->data2, dtp->data3);
	}
	for (i = 0; i < index; i++) {
		dtp = vport->disc_trc + i;
		if (!dtp->fmt)
			continue;
		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
179 180 181
		snprintf(buffer,
			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
			dtp->seq_cnt, ms, dtp->fmt);
182
		len +=  scnprintf(buf+len, size-len, buffer,
183 184 185 186
			dtp->data1, dtp->data2, dtp->data3);
	}

	lpfc_debugfs_enable = enable;
187 188
	kfree(buffer);

189 190 191
	return len;
}

192
/**
193
 * lpfc_debugfs_slow_ring_trc_data - Dump slow ring logging to a buffer
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
 * @phba: The HBA to gather the log info from.
 * @buf: The buffer to dump log into.
 * @size: The maximum amount of data to process.
 *
 * Description:
 * This routine gathers the lpfc slow ring debugfs data from the @phba and
 * dumps it to @buf up to @size number of bytes. It will start at the next entry
 * in the log and process the log until the end of the buffer. Then it will
 * gather from the beginning of the log and process until the current entry.
 *
 * Notes:
 * Slow ring logging will be disabled while while this routine dumps the log.
 *
 * Return Value:
 * This routine returns the amount of bytes that were dumped into @buf and will
 * not exceed @size.
 **/
211 212 213 214 215 216
static int
lpfc_debugfs_slow_ring_trc_data(struct lpfc_hba *phba, char *buf, int size)
{
	int i, index, len, enable;
	uint32_t ms;
	struct lpfc_debugfs_trc *dtp;
217
	char *buffer;
218

219 220 221
	buffer = kmalloc(LPFC_DEBUG_TRC_ENTRY_SIZE, GFP_KERNEL);
	if (!buffer)
		return 0;
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

	enable = lpfc_debugfs_enable;
	lpfc_debugfs_enable = 0;

	len = 0;
	index = (atomic_read(&phba->slow_ring_trc_cnt) + 1) &
		(lpfc_debugfs_max_slow_ring_trc - 1);
	for (i = index; i < lpfc_debugfs_max_slow_ring_trc; i++) {
		dtp = phba->slow_ring_trc + i;
		if (!dtp->fmt)
			continue;
		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
		snprintf(buffer,
			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
			dtp->seq_cnt, ms, dtp->fmt);
237
		len +=  scnprintf(buf+len, size-len, buffer,
238 239 240 241 242 243 244 245 246
			dtp->data1, dtp->data2, dtp->data3);
	}
	for (i = 0; i < index; i++) {
		dtp = phba->slow_ring_trc + i;
		if (!dtp->fmt)
			continue;
		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
		snprintf(buffer,
			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
J
James Smart 已提交
247
			dtp->seq_cnt, ms, dtp->fmt);
248
		len +=  scnprintf(buf+len, size-len, buffer,
J
James Smart 已提交
249 250 251 252
			dtp->data1, dtp->data2, dtp->data3);
	}

	lpfc_debugfs_enable = enable;
253 254
	kfree(buffer);

J
James Smart 已提交
255 256 257
	return len;
}

J
James Smart 已提交
258
static int lpfc_debugfs_last_hbq = -1;
259

260
/**
261
 * lpfc_debugfs_hbqinfo_data - Dump host buffer queue info to a buffer
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
 * @phba: The HBA to gather host buffer info from.
 * @buf: The buffer to dump log into.
 * @size: The maximum amount of data to process.
 *
 * Description:
 * This routine dumps the host buffer queue info from the @phba to @buf up to
 * @size number of bytes. A header that describes the current hbq state will be
 * dumped to @buf first and then info on each hbq entry will be dumped to @buf
 * until @size bytes have been dumped or all the hbq info has been dumped.
 *
 * Notes:
 * This routine will rotate through each configured HBQ each time called.
 *
 * Return Value:
 * This routine returns the amount of bytes that were dumped into @buf and will
 * not exceed @size.
 **/
279 280 281 282
static int
lpfc_debugfs_hbqinfo_data(struct lpfc_hba *phba, char *buf, int size)
{
	int len = 0;
283
	int i, j, found, posted, low;
284 285 286 287 288 289 290
	uint32_t phys, raw_index, getidx;
	struct lpfc_hbq_init *hip;
	struct hbq_s *hbqs;
	struct lpfc_hbq_entry *hbqe;
	struct lpfc_dmabuf *d_buf;
	struct hbq_dmabuf *hbq_buf;

291 292
	if (phba->sli_rev != 3)
		return 0;
293

294 295 296 297 298 299 300 301 302 303 304 305 306 307
	spin_lock_irq(&phba->hbalock);

	/* toggle between multiple hbqs, if any */
	i = lpfc_sli_hbq_count();
	if (i > 1) {
		 lpfc_debugfs_last_hbq++;
		 if (lpfc_debugfs_last_hbq >= i)
			lpfc_debugfs_last_hbq = 0;
	}
	else
		lpfc_debugfs_last_hbq = 0;

	i = lpfc_debugfs_last_hbq;

308
	len +=  scnprintf(buf+len, size-len, "HBQ %d Info\n", i);
309

310
	hbqs =  &phba->hbqs[i];
311
	posted = 0;
312
	list_for_each_entry(d_buf, &hbqs->hbq_buffer_list, list)
313 314 315
		posted++;

	hip =  lpfc_hbq_defs[i];
316
	len +=  scnprintf(buf+len, size-len,
317 318 319 320 321 322
		"idx:%d prof:%d rn:%d bufcnt:%d icnt:%d acnt:%d posted %d\n",
		hip->hbq_index, hip->profile, hip->rn,
		hip->buffer_count, hip->init_count, hip->add_count, posted);

	raw_index = phba->hbq_get[i];
	getidx = le32_to_cpu(raw_index);
323
	len +=  scnprintf(buf+len, size-len,
324
		"entries:%d bufcnt:%d Put:%d nPut:%d localGet:%d hbaGet:%d\n",
325 326
		hbqs->entry_count, hbqs->buffer_count, hbqs->hbqPutIdx,
		hbqs->next_hbqPutIdx, hbqs->local_hbqGetIdx, getidx);
327

328
	hbqe = (struct lpfc_hbq_entry *) phba->hbqs[i].hbq_virt;
329
	for (j=0; j<hbqs->entry_count; j++) {
330
		len +=  scnprintf(buf+len, size-len,
331
			"%03d: %08x %04x %05x ", j,
332 333 334
			le32_to_cpu(hbqe->bde.addrLow),
			le32_to_cpu(hbqe->bde.tus.w),
			le32_to_cpu(hbqe->buffer_tag));
335 336 337 338 339 340 341
		i = 0;
		found = 0;

		/* First calculate if slot has an associated posted buffer */
		low = hbqs->hbqPutIdx - posted;
		if (low >= 0) {
			if ((j >= hbqs->hbqPutIdx) || (j < low)) {
342 343
				len +=  scnprintf(buf + len, size - len,
						"Unused\n");
344 345 346 347 348 349
				goto skipit;
			}
		}
		else {
			if ((j >= hbqs->hbqPutIdx) &&
				(j < (hbqs->entry_count+low))) {
350 351
				len +=  scnprintf(buf + len, size - len,
						"Unused\n");
352 353 354 355 356
				goto skipit;
			}
		}

		/* Get the Buffer info for the posted buffer */
357
		list_for_each_entry(d_buf, &hbqs->hbq_buffer_list, list) {
358 359
			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
			phys = ((uint64_t)hbq_buf->dbuf.phys & 0xffffffff);
360
			if (phys == le32_to_cpu(hbqe->bde.addrLow)) {
361
				len +=  scnprintf(buf+len, size-len,
362
					"Buf%d: x%px %06x\n", i,
363 364 365 366 367 368 369
					hbq_buf->dbuf.virt, hbq_buf->tag);
				found = 1;
				break;
			}
			i++;
		}
		if (!found) {
370
			len +=  scnprintf(buf+len, size-len, "No DMAinfo?\n");
371 372 373 374 375 376 377 378 379 380
		}
skipit:
		hbqe++;
		if (len > LPFC_HBQINFO_SIZE - 54)
			break;
	}
	spin_unlock_irq(&phba->hbalock);
	return len;
}

381 382 383
static int lpfc_debugfs_last_xripool;

/**
384
 * lpfc_debugfs_commonxripools_data - Dump Hardware Queue info to a buffer
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
 * @phba: The HBA to gather host buffer info from.
 * @buf: The buffer to dump log into.
 * @size: The maximum amount of data to process.
 *
 * Description:
 * This routine dumps the Hardware Queue info from the @phba to @buf up to
 * @size number of bytes. A header that describes the current hdwq state will be
 * dumped to @buf first and then info on each hdwq entry will be dumped to @buf
 * until @size bytes have been dumped or all the hdwq info has been dumped.
 *
 * Notes:
 * This routine will rotate through each configured Hardware Queue each
 * time called.
 *
 * Return Value:
 * This routine returns the amount of bytes that were dumped into @buf and will
 * not exceed @size.
 **/
static int
lpfc_debugfs_commonxripools_data(struct lpfc_hba *phba, char *buf, int size)
{
	struct lpfc_sli4_hdw_queue *qp;
	int len = 0;
	int i, out;
	unsigned long iflag;

	for (i = 0; i < phba->cfg_hdw_queue; i++) {
		if (len > (LPFC_DUMP_MULTIXRIPOOL_SIZE - 80))
			break;
		qp = &phba->sli4_hba.hdwq[lpfc_debugfs_last_xripool];

416
		len += scnprintf(buf + len, size - len, "HdwQ %d Info ", i);
417
		spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
418 419 420 421
		spin_lock(&qp->io_buf_list_get_lock);
		spin_lock(&qp->io_buf_list_put_lock);
		out = qp->total_io_bufs - (qp->get_io_bufs + qp->put_io_bufs +
			qp->abts_scsi_io_bufs + qp->abts_nvme_io_bufs);
422
		len += scnprintf(buf + len, size - len,
423 424 425 426 427 428 429
				 "tot:%d get:%d put:%d mt:%d "
				 "ABTS scsi:%d nvme:%d Out:%d\n",
			qp->total_io_bufs, qp->get_io_bufs, qp->put_io_bufs,
			qp->empty_io_bufs, qp->abts_scsi_io_bufs,
			qp->abts_nvme_io_bufs, out);
		spin_unlock(&qp->io_buf_list_put_lock);
		spin_unlock(&qp->io_buf_list_get_lock);
430
		spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
431 432 433 434 435 436 437 438 439

		lpfc_debugfs_last_xripool++;
		if (lpfc_debugfs_last_xripool >= phba->cfg_hdw_queue)
			lpfc_debugfs_last_xripool = 0;
	}

	return len;
}

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
/**
 * lpfc_debugfs_multixripools_data - Display multi-XRI pools information
 * @phba: The HBA to gather host buffer info from.
 * @buf: The buffer to dump log into.
 * @size: The maximum amount of data to process.
 *
 * Description:
 * This routine displays current multi-XRI pools information including XRI
 * count in public, private and txcmplq. It also displays current high and
 * low watermark.
 *
 * Return Value:
 * This routine returns the amount of bytes that were dumped into @buf and will
 * not exceed @size.
 **/
static int
lpfc_debugfs_multixripools_data(struct lpfc_hba *phba, char *buf, int size)
{
	u32 i;
	u32 hwq_count;
	struct lpfc_sli4_hdw_queue *qp;
	struct lpfc_multixri_pool *multixri_pool;
	struct lpfc_pvt_pool *pvt_pool;
	struct lpfc_pbl_pool *pbl_pool;
	u32 txcmplq_cnt;
	char tmp[LPFC_DEBUG_OUT_LINE_SZ] = {0};

467 468 469 470 471 472 473 474 475 476 477
	if (phba->sli_rev != LPFC_SLI_REV4)
		return 0;

	if (!phba->sli4_hba.hdwq)
		return 0;

	if (!phba->cfg_xri_rebalancing) {
		i = lpfc_debugfs_commonxripools_data(phba, buf, size);
		return i;
	}

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	/*
	 * Pbl: Current number of free XRIs in public pool
	 * Pvt: Current number of free XRIs in private pool
	 * Busy: Current number of outstanding XRIs
	 * HWM: Current high watermark
	 * pvt_empty: Incremented by 1 when IO submission fails (no xri)
	 * pbl_empty: Incremented by 1 when all pbl_pool are empty during
	 *            IO submission
	 */
	scnprintf(tmp, sizeof(tmp),
		  "HWQ:  Pbl  Pvt Busy  HWM |  pvt_empty  pbl_empty ");
	if (strlcat(buf, tmp, size) >= size)
		return strnlen(buf, size);

#ifdef LPFC_MXP_STAT
	/*
	 * MAXH: Max high watermark seen so far
	 * above_lmt: Incremented by 1 if xri_owned > xri_limit during
	 *            IO submission
	 * below_lmt: Incremented by 1 if xri_owned <= xri_limit  during
	 *            IO submission
	 * locPbl_hit: Incremented by 1 if successfully get a batch of XRI from
	 *             local pbl_pool
	 * othPbl_hit: Incremented by 1 if successfully get a batch of XRI from
	 *             other pbl_pool
	 */
	scnprintf(tmp, sizeof(tmp),
		  "MAXH  above_lmt  below_lmt locPbl_hit othPbl_hit");
	if (strlcat(buf, tmp, size) >= size)
		return strnlen(buf, size);

	/*
	 * sPbl: snapshot of Pbl 15 sec after stat gets cleared
	 * sPvt: snapshot of Pvt 15 sec after stat gets cleared
	 * sBusy: snapshot of Busy 15 sec after stat gets cleared
	 */
	scnprintf(tmp, sizeof(tmp),
		  " | sPbl sPvt sBusy");
	if (strlcat(buf, tmp, size) >= size)
		return strnlen(buf, size);
#endif

	scnprintf(tmp, sizeof(tmp), "\n");
	if (strlcat(buf, tmp, size) >= size)
		return strnlen(buf, size);

	hwq_count = phba->cfg_hdw_queue;
	for (i = 0; i < hwq_count; i++) {
		qp = &phba->sli4_hba.hdwq[i];
		multixri_pool = qp->p_multixri_pool;
		if (!multixri_pool)
			continue;
		pbl_pool = &multixri_pool->pbl_pool;
		pvt_pool = &multixri_pool->pvt_pool;
532
		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

		scnprintf(tmp, sizeof(tmp),
			  "%03d: %4d %4d %4d %4d | %10d %10d ",
			  i, pbl_pool->count, pvt_pool->count,
			  txcmplq_cnt, pvt_pool->high_watermark,
			  qp->empty_io_bufs, multixri_pool->pbl_empty_count);
		if (strlcat(buf, tmp, size) >= size)
			break;

#ifdef LPFC_MXP_STAT
		scnprintf(tmp, sizeof(tmp),
			  "%4d %10d %10d %10d %10d",
			  multixri_pool->stat_max_hwm,
			  multixri_pool->above_limit_count,
			  multixri_pool->below_limit_count,
			  multixri_pool->local_pbl_hit_count,
			  multixri_pool->other_pbl_hit_count);
		if (strlcat(buf, tmp, size) >= size)
			break;

		scnprintf(tmp, sizeof(tmp),
			  " | %4d %4d %5d",
			  multixri_pool->stat_pbl_count,
			  multixri_pool->stat_pvt_count,
			  multixri_pool->stat_busy_count);
		if (strlcat(buf, tmp, size) >= size)
			break;
#endif

		scnprintf(tmp, sizeof(tmp), "\n");
		if (strlcat(buf, tmp, size) >= size)
			break;
	}
	return strnlen(buf, size);
}

569 570 571

#ifdef LPFC_HDWQ_LOCK_STAT
static int lpfc_debugfs_last_lock;
572 573

/**
574
 * lpfc_debugfs_lockstat_data - Dump Hardware Queue info to a buffer
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
 * @phba: The HBA to gather host buffer info from.
 * @buf: The buffer to dump log into.
 * @size: The maximum amount of data to process.
 *
 * Description:
 * This routine dumps the Hardware Queue info from the @phba to @buf up to
 * @size number of bytes. A header that describes the current hdwq state will be
 * dumped to @buf first and then info on each hdwq entry will be dumped to @buf
 * until @size bytes have been dumped or all the hdwq info has been dumped.
 *
 * Notes:
 * This routine will rotate through each configured Hardware Queue each
 * time called.
 *
 * Return Value:
 * This routine returns the amount of bytes that were dumped into @buf and will
 * not exceed @size.
 **/
static int
594
lpfc_debugfs_lockstat_data(struct lpfc_hba *phba, char *buf, int size)
595 596 597
{
	struct lpfc_sli4_hdw_queue *qp;
	int len = 0;
598
	int i;
599 600 601 602 603 604 605 606

	if (phba->sli_rev != LPFC_SLI_REV4)
		return 0;

	if (!phba->sli4_hba.hdwq)
		return 0;

	for (i = 0; i < phba->cfg_hdw_queue; i++) {
607
		if (len > (LPFC_HDWQINFO_SIZE - 100))
608
			break;
609
		qp = &phba->sli4_hba.hdwq[lpfc_debugfs_last_lock];
610

611
		len += scnprintf(buf + len, size - len, "HdwQ %03d Lock ", i);
612
		if (phba->cfg_xri_rebalancing) {
613
			len += scnprintf(buf + len, size - len,
614 615 616 617 618 619 620 621 622 623 624
					 "get_pvt:%d mv_pvt:%d "
					 "mv2pub:%d mv2pvt:%d "
					 "put_pvt:%d put_pub:%d wq:%d\n",
					 qp->lock_conflict.alloc_pvt_pool,
					 qp->lock_conflict.mv_from_pvt_pool,
					 qp->lock_conflict.mv_to_pub_pool,
					 qp->lock_conflict.mv_to_pvt_pool,
					 qp->lock_conflict.free_pvt_pool,
					 qp->lock_conflict.free_pub_pool,
					 qp->lock_conflict.wq_access);
		} else {
625
			len += scnprintf(buf + len, size - len,
626 627 628 629 630 631
					 "get:%d put:%d free:%d wq:%d\n",
					 qp->lock_conflict.alloc_xri_get,
					 qp->lock_conflict.alloc_xri_put,
					 qp->lock_conflict.free_xri,
					 qp->lock_conflict.wq_access);
		}
632

633 634 635
		lpfc_debugfs_last_lock++;
		if (lpfc_debugfs_last_lock >= phba->cfg_hdw_queue)
			lpfc_debugfs_last_lock = 0;
636 637 638 639
	}

	return len;
}
640
#endif
641

642 643
static int lpfc_debugfs_last_hba_slim_off;

644
/**
645
 * lpfc_debugfs_dumpHBASlim_data - Dump HBA SLIM info to a buffer
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
 * @phba: The HBA to gather SLIM info from.
 * @buf: The buffer to dump log into.
 * @size: The maximum amount of data to process.
 *
 * Description:
 * This routine dumps the current contents of HBA SLIM for the HBA associated
 * with @phba to @buf up to @size bytes of data. This is the raw HBA SLIM data.
 *
 * Notes:
 * This routine will only dump up to 1024 bytes of data each time called and
 * should be called multiple times to dump the entire HBA SLIM.
 *
 * Return Value:
 * This routine returns the amount of bytes that were dumped into @buf and will
 * not exceed @size.
 **/
662 663 664 665 666 667
static int
lpfc_debugfs_dumpHBASlim_data(struct lpfc_hba *phba, char *buf, int size)
{
	int len = 0;
	int i, off;
	uint32_t *ptr;
668 669 670 671 672
	char *buffer;

	buffer = kmalloc(1024, GFP_KERNEL);
	if (!buffer)
		return 0;
673 674 675 676

	off = 0;
	spin_lock_irq(&phba->hbalock);

677
	len +=  scnprintf(buf+len, size-len, "HBA SLIM\n");
678
	lpfc_memcpy_from_slim(buffer,
679
		phba->MBslimaddr + lpfc_debugfs_last_hba_slim_off, 1024);
680 681 682 683 684 685 686 687 688 689 690

	ptr = (uint32_t *)&buffer[0];
	off = lpfc_debugfs_last_hba_slim_off;

	/* Set it up for the next time */
	lpfc_debugfs_last_hba_slim_off += 1024;
	if (lpfc_debugfs_last_hba_slim_off >= 4096)
		lpfc_debugfs_last_hba_slim_off = 0;

	i = 1024;
	while (i > 0) {
691
		len +=  scnprintf(buf+len, size-len,
692 693 694 695 696 697 698 699 700
		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
		*(ptr+5), *(ptr+6), *(ptr+7));
		ptr += 8;
		i -= (8 * sizeof(uint32_t));
		off += (8 * sizeof(uint32_t));
	}

	spin_unlock_irq(&phba->hbalock);
701 702
	kfree(buffer);

703 704 705
	return len;
}

706
/**
707
 * lpfc_debugfs_dumpHostSlim_data - Dump host SLIM info to a buffer
708 709 710 711 712 713 714 715 716 717 718 719 720
 * @phba: The HBA to gather Host SLIM info from.
 * @buf: The buffer to dump log into.
 * @size: The maximum amount of data to process.
 *
 * Description:
 * This routine dumps the current contents of host SLIM for the host associated
 * with @phba to @buf up to @size bytes of data. The dump will contain the
 * Mailbox, PCB, Rings, and Registers that are located in host memory.
 *
 * Return Value:
 * This routine returns the amount of bytes that were dumped into @buf and will
 * not exceed @size.
 **/
721
static int
722
lpfc_debugfs_dumpHostSlim_data(struct lpfc_hba *phba, char *buf, int size)
723 724
{
	int len = 0;
725
	int i, off;
726 727 728 729 730 731 732 733 734
	uint32_t word0, word1, word2, word3;
	uint32_t *ptr;
	struct lpfc_pgp *pgpp;
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring *pring;

	off = 0;
	spin_lock_irq(&phba->hbalock);

735
	len +=  scnprintf(buf+len, size-len, "SLIM Mailbox\n");
736
	ptr = (uint32_t *)phba->slim2p.virt;
737 738
	i = sizeof(MAILBOX_t);
	while (i > 0) {
739
		len +=  scnprintf(buf+len, size-len,
740 741 742 743 744 745 746 747
		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
		*(ptr+5), *(ptr+6), *(ptr+7));
		ptr += 8;
		i -= (8 * sizeof(uint32_t));
		off += (8 * sizeof(uint32_t));
	}

748
	len +=  scnprintf(buf+len, size-len, "SLIM PCB\n");
749
	ptr = (uint32_t *)phba->pcb;
750 751
	i = sizeof(PCB_t);
	while (i > 0) {
752
		len +=  scnprintf(buf+len, size-len,
753 754 755 756 757 758 759 760
		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
		*(ptr+5), *(ptr+6), *(ptr+7));
		ptr += 8;
		i -= (8 * sizeof(uint32_t));
		off += (8 * sizeof(uint32_t));
	}

761
	if (phba->sli_rev <= LPFC_SLI_REV3) {
762 763 764
		for (i = 0; i < 4; i++) {
			pgpp = &phba->port_gp[i];
			pring = &psli->sli3_ring[i];
765
			len +=  scnprintf(buf+len, size-len,
766 767 768 769 770 771 772 773 774 775 776 777
					 "Ring %d: CMD GetInx:%d "
					 "(Max:%d Next:%d "
					 "Local:%d flg:x%x)  "
					 "RSP PutInx:%d Max:%d\n",
					 i, pgpp->cmdGetInx,
					 pring->sli.sli3.numCiocb,
					 pring->sli.sli3.next_cmdidx,
					 pring->sli.sli3.local_getidx,
					 pring->flag, pgpp->rspPutInx,
					 pring->sli.sli3.numRiocb);
		}

778 779 780 781
		word0 = readl(phba->HAregaddr);
		word1 = readl(phba->CAregaddr);
		word2 = readl(phba->HSregaddr);
		word3 = readl(phba->HCregaddr);
782
		len +=  scnprintf(buf+len, size-len, "HA:%08x CA:%08x HS:%08x "
783 784
				 "HC:%08x\n", word0, word1, word2, word3);
	}
785 786 787 788
	spin_unlock_irq(&phba->hbalock);
	return len;
}

789
/**
790
 * lpfc_debugfs_nodelist_data - Dump target node list to a buffer
791 792 793 794 795 796 797 798 799 800 801 802 803
 * @vport: The vport to gather target node info from.
 * @buf: The buffer to dump log into.
 * @size: The maximum amount of data to process.
 *
 * Description:
 * This routine dumps the current target node list associated with @vport to
 * @buf up to @size bytes of data. Each node entry in the dump will contain a
 * node state, DID, WWPN, WWNN, RPI, flags, type, and other useful fields.
 *
 * Return Value:
 * This routine returns the amount of bytes that were dumped into @buf and will
 * not exceed @size.
 **/
J
James Smart 已提交
804 805 806 807
static int
lpfc_debugfs_nodelist_data(struct lpfc_vport *vport, char *buf, int size)
{
	int len = 0;
808
	int i, iocnt, outio, cnt;
J
James Smart 已提交
809
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
810
	struct lpfc_hba  *phba = vport->phba;
J
James Smart 已提交
811
	struct lpfc_nodelist *ndlp;
J
James Smart 已提交
812
	unsigned char *statep;
813
	struct nvme_fc_local_port *localport;
814
	struct nvme_fc_remote_port *nrport = NULL;
815
	struct lpfc_nvme_rport *rport;
J
James Smart 已提交
816 817

	cnt = (LPFC_NODELIST_SIZE / LPFC_NODELIST_ENTRY_SIZE);
818
	outio = 0;
J
James Smart 已提交
819

820
	len += scnprintf(buf+len, size-len, "\nFCP Nodelist Entries ...\n");
J
James Smart 已提交
821 822
	spin_lock_irq(shost->host_lock);
	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
823
		iocnt = 0;
J
James Smart 已提交
824
		if (!cnt) {
825
			len +=  scnprintf(buf+len, size-len,
J
James Smart 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
				"Missing Nodelist Entries\n");
			break;
		}
		cnt--;
		switch (ndlp->nlp_state) {
		case NLP_STE_UNUSED_NODE:
			statep = "UNUSED";
			break;
		case NLP_STE_PLOGI_ISSUE:
			statep = "PLOGI ";
			break;
		case NLP_STE_ADISC_ISSUE:
			statep = "ADISC ";
			break;
		case NLP_STE_REG_LOGIN_ISSUE:
			statep = "REGLOG";
			break;
		case NLP_STE_PRLI_ISSUE:
			statep = "PRLI  ";
			break;
846 847 848
		case NLP_STE_LOGO_ISSUE:
			statep = "LOGO  ";
			break;
J
James Smart 已提交
849 850
		case NLP_STE_UNMAPPED_NODE:
			statep = "UNMAP ";
851
			iocnt = 1;
J
James Smart 已提交
852 853 854
			break;
		case NLP_STE_MAPPED_NODE:
			statep = "MAPPED";
855
			iocnt = 1;
J
James Smart 已提交
856 857 858 859 860 861 862
			break;
		case NLP_STE_NPR_NODE:
			statep = "NPR   ";
			break;
		default:
			statep = "UNKNOWN";
		}
863
		len += scnprintf(buf+len, size-len, "%s DID:x%06x ",
J
James Smart 已提交
864
				statep, ndlp->nlp_DID);
865
		len += scnprintf(buf+len, size-len,
866
				"WWPN x%016llx ",
J
James Smart 已提交
867
				wwn_to_u64(ndlp->nlp_portname.u.wwn));
868
		len += scnprintf(buf+len, size-len,
869
				"WWNN x%016llx ",
J
James Smart 已提交
870
				wwn_to_u64(ndlp->nlp_nodename.u.wwn));
871 872
		len += scnprintf(buf+len, size-len, "RPI:x%04x ",
				 ndlp->nlp_rpi);
873
		len +=  scnprintf(buf+len, size-len, "flag:x%08x ",
874
			ndlp->nlp_flag);
J
James Smart 已提交
875
		if (!ndlp->nlp_type)
876
			len += scnprintf(buf+len, size-len, "UNKNOWN_TYPE ");
J
James Smart 已提交
877
		if (ndlp->nlp_type & NLP_FC_NODE)
878
			len += scnprintf(buf+len, size-len, "FC_NODE ");
879
		if (ndlp->nlp_type & NLP_FABRIC) {
880
			len += scnprintf(buf+len, size-len, "FABRIC ");
881 882
			iocnt = 0;
		}
J
James Smart 已提交
883
		if (ndlp->nlp_type & NLP_FCP_TARGET)
884
			len += scnprintf(buf+len, size-len, "FCP_TGT sid:%d ",
J
James Smart 已提交
885 886
				ndlp->nlp_sid);
		if (ndlp->nlp_type & NLP_FCP_INITIATOR)
887
			len += scnprintf(buf+len, size-len, "FCP_INITIATOR ");
888
		if (ndlp->nlp_type & NLP_NVME_TARGET)
889
			len += scnprintf(buf + len,
890 891 892
					size - len, "NVME_TGT sid:%d ",
					NLP_NO_SID);
		if (ndlp->nlp_type & NLP_NVME_INITIATOR)
893
			len += scnprintf(buf + len,
894
					size - len, "NVME_INITIATOR ");
895
		len += scnprintf(buf+len, size-len, "refcnt:%d",
896
			kref_read(&ndlp->kref));
897 898
		if (iocnt) {
			i = atomic_read(&ndlp->cmd_pending);
899
			len += scnprintf(buf + len, size - len,
900 901 902 903
					" OutIO:x%x Qdepth x%x",
					i, ndlp->cmd_qdepth);
			outio += i;
		}
904 905 906 907 908
		len += scnprintf(buf+len, size-len, " xpt:x%x",
				 ndlp->fc4_xpt_flags);
		if (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)
			len += scnprintf(buf+len, size-len, " defer:%x",
					 ndlp->nlp_defer_did);
909
		len +=  scnprintf(buf+len, size-len, "\n");
J
James Smart 已提交
910 911
	}
	spin_unlock_irq(shost->host_lock);
J
James Smart 已提交
912

913
	len += scnprintf(buf + len, size - len,
914 915
			"\nOutstanding IO x%x\n",  outio);

916
	if (phba->nvmet_support && phba->targetport && (vport == phba->pport)) {
917
		len += scnprintf(buf + len, size - len,
918 919 920 921 922 923 924
				"\nNVME Targetport Entry ...\n");

		/* Port state is only one of two values for now. */
		if (phba->targetport->port_id)
			statep = "REGISTERED";
		else
			statep = "INIT";
925
		len += scnprintf(buf + len, size - len,
926 927 928 929
				"TGT WWNN x%llx WWPN x%llx State %s\n",
				wwn_to_u64(vport->fc_nodename.u.wwn),
				wwn_to_u64(vport->fc_portname.u.wwn),
				statep);
930
		len += scnprintf(buf + len, size - len,
931 932 933 934 935
				"    Targetport DID x%06x\n",
				phba->targetport->port_id);
		goto out_exit;
	}

936
	len += scnprintf(buf + len, size - len,
937 938 939 940 941 942 943 944 945 946 947 948 949 950
				"\nNVME Lport/Rport Entries ...\n");

	localport = vport->localport;
	if (!localport)
		goto out_exit;

	spin_lock_irq(shost->host_lock);

	/* Port state is only one of two values for now. */
	if (localport->port_id)
		statep = "ONLINE";
	else
		statep = "UNKNOWN ";

951
	len += scnprintf(buf + len, size - len,
952 953 954
			"Lport DID x%06x PortState %s\n",
			localport->port_id, statep);

955
	len += scnprintf(buf + len, size - len, "\tRport List:\n");
956
	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
957
		/* local short-hand pointer. */
958
		spin_lock(&ndlp->lock);
959
		rport = lpfc_ndlp_get_nrport(ndlp);
960 961
		if (rport)
			nrport = rport->remoteport;
A
Arnd Bergmann 已提交
962 963
		else
			nrport = NULL;
964
		spin_unlock(&ndlp->lock);
965 966
		if (!nrport)
			continue;
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981

		/* Port state is only one of two values for now. */
		switch (nrport->port_state) {
		case FC_OBJSTATE_ONLINE:
			statep = "ONLINE";
			break;
		case FC_OBJSTATE_UNKNOWN:
			statep = "UNKNOWN ";
			break;
		default:
			statep = "UNSUPPORTED";
			break;
		}

		/* Tab in to show lport ownership. */
982
		len += scnprintf(buf + len, size - len,
983 984
				"\t%s Port ID:x%06x ",
				statep, nrport->port_id);
985
		len += scnprintf(buf + len, size - len, "WWPN x%llx ",
986
				nrport->port_name);
987
		len += scnprintf(buf + len, size - len, "WWNN x%llx ",
988
				nrport->node_name);
989 990 991

		/* An NVME rport can have multiple roles. */
		if (nrport->port_role & FC_PORT_ROLE_NVME_INITIATOR)
992
			len +=  scnprintf(buf + len, size - len,
993 994
					 "INITIATOR ");
		if (nrport->port_role & FC_PORT_ROLE_NVME_TARGET)
995
			len +=  scnprintf(buf + len, size - len,
996 997
					 "TARGET ");
		if (nrport->port_role & FC_PORT_ROLE_NVME_DISCOVERY)
998
			len +=  scnprintf(buf + len, size - len,
999 1000 1001 1002
					 "DISCSRVC ");
		if (nrport->port_role & ~(FC_PORT_ROLE_NVME_INITIATOR |
					  FC_PORT_ROLE_NVME_TARGET |
					  FC_PORT_ROLE_NVME_DISCOVERY))
1003
			len +=  scnprintf(buf + len, size - len,
1004 1005 1006
					 "UNKNOWN ROLE x%x",
					 nrport->port_role);
		/* Terminate the string. */
1007
		len +=  scnprintf(buf + len, size - len, "\n");
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
	}

	spin_unlock_irq(shost->host_lock);
 out_exit:
	return len;
}

/**
 * lpfc_debugfs_nvmestat_data - Dump target node list to a buffer
 * @vport: The vport to gather target node info from.
 * @buf: The buffer to dump log into.
 * @size: The maximum amount of data to process.
 *
 * Description:
 * This routine dumps the NVME statistics associated with @vport
 *
 * Return Value:
 * This routine returns the amount of bytes that were dumped into @buf and will
 * not exceed @size.
 **/
static int
lpfc_debugfs_nvmestat_data(struct lpfc_vport *vport, char *buf, int size)
{
	struct lpfc_hba   *phba = vport->phba;
1032
	struct lpfc_nvmet_tgtport *tgtp;
1033
	struct lpfc_async_xchg_ctx *ctxp, *next_ctxp;
1034
	struct nvme_fc_local_port *localport;
1035
	struct lpfc_fc4_ctrl_stat *cstat;
1036
	struct lpfc_nvme_lport *lport;
1037 1038
	uint64_t data1, data2, data3;
	uint64_t tot, totin, totout;
1039
	int cnt, i;
1040 1041
	int len = 0;

1042 1043 1044 1045
	if (phba->nvmet_support) {
		if (!phba->targetport)
			return len;
		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1046
		len += scnprintf(buf + len, size - len,
1047 1048
				"\nNVME Targetport Statistics\n");

1049
		len += scnprintf(buf + len, size - len,
1050 1051 1052 1053 1054 1055
				"LS: Rcv %08x Drop %08x Abort %08x\n",
				atomic_read(&tgtp->rcv_ls_req_in),
				atomic_read(&tgtp->rcv_ls_req_drop),
				atomic_read(&tgtp->xmt_ls_abort));
		if (atomic_read(&tgtp->rcv_ls_req_in) !=
		    atomic_read(&tgtp->rcv_ls_req_out)) {
1056
			len += scnprintf(buf + len, size - len,
1057 1058 1059 1060 1061
					"Rcv LS: in %08x != out %08x\n",
					atomic_read(&tgtp->rcv_ls_req_in),
					atomic_read(&tgtp->rcv_ls_req_out));
		}

1062
		len += scnprintf(buf + len, size - len,
1063
				"LS: Xmt %08x Drop %08x Cmpl %08x\n",
1064 1065
				atomic_read(&tgtp->xmt_ls_rsp),
				atomic_read(&tgtp->xmt_ls_drop),
1066 1067
				atomic_read(&tgtp->xmt_ls_rsp_cmpl));

1068
		len += scnprintf(buf + len, size - len,
1069 1070 1071
				"LS: RSP Abort %08x xb %08x Err %08x\n",
				atomic_read(&tgtp->xmt_ls_rsp_aborted),
				atomic_read(&tgtp->xmt_ls_rsp_xb_set),
1072 1073
				atomic_read(&tgtp->xmt_ls_rsp_error));

1074
		len += scnprintf(buf + len, size - len,
1075 1076
				"FCP: Rcv %08x Defer %08x Release %08x "
				"Drop %08x\n",
1077
				atomic_read(&tgtp->rcv_fcp_cmd_in),
1078 1079
				atomic_read(&tgtp->rcv_fcp_cmd_defer),
				atomic_read(&tgtp->xmt_fcp_release),
1080 1081 1082 1083
				atomic_read(&tgtp->rcv_fcp_cmd_drop));

		if (atomic_read(&tgtp->rcv_fcp_cmd_in) !=
		    atomic_read(&tgtp->rcv_fcp_cmd_out)) {
1084
			len += scnprintf(buf + len, size - len,
1085 1086 1087 1088 1089
					"Rcv FCP: in %08x != out %08x\n",
					atomic_read(&tgtp->rcv_fcp_cmd_in),
					atomic_read(&tgtp->rcv_fcp_cmd_out));
		}

1090
		len += scnprintf(buf + len, size - len,
J
James Smart 已提交
1091 1092
				"FCP Rsp: read %08x readrsp %08x "
				"write %08x rsp %08x\n",
1093 1094 1095 1096 1097
				atomic_read(&tgtp->xmt_fcp_read),
				atomic_read(&tgtp->xmt_fcp_read_rsp),
				atomic_read(&tgtp->xmt_fcp_write),
				atomic_read(&tgtp->xmt_fcp_rsp));

1098
		len += scnprintf(buf + len, size - len,
1099 1100 1101 1102 1103
				"FCP Rsp Cmpl: %08x err %08x drop %08x\n",
				atomic_read(&tgtp->xmt_fcp_rsp_cmpl),
				atomic_read(&tgtp->xmt_fcp_rsp_error),
				atomic_read(&tgtp->xmt_fcp_rsp_drop));

1104
		len += scnprintf(buf + len, size - len,
1105 1106 1107 1108 1109
				"FCP Rsp Abort: %08x xb %08x xricqe  %08x\n",
				atomic_read(&tgtp->xmt_fcp_rsp_aborted),
				atomic_read(&tgtp->xmt_fcp_rsp_xb_set),
				atomic_read(&tgtp->xmt_fcp_xri_abort_cqe));

1110
		len += scnprintf(buf + len, size - len,
1111 1112 1113 1114
				"ABORT: Xmt %08x Cmpl %08x\n",
				atomic_read(&tgtp->xmt_fcp_abort),
				atomic_read(&tgtp->xmt_fcp_abort_cmpl));

1115
		len += scnprintf(buf + len, size - len,
1116 1117 1118
				"ABORT: Sol %08x  Usol %08x Err %08x Cmpl %08x",
				atomic_read(&tgtp->xmt_abort_sol),
				atomic_read(&tgtp->xmt_abort_unsol),
1119
				atomic_read(&tgtp->xmt_abort_rsp),
1120
				atomic_read(&tgtp->xmt_abort_rsp_error));
1121

1122
		len +=  scnprintf(buf + len, size - len, "\n");
J
James Smart 已提交
1123 1124

		cnt = 0;
1125
		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
J
James Smart 已提交
1126 1127 1128 1129 1130
		list_for_each_entry_safe(ctxp, next_ctxp,
				&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
				list) {
			cnt++;
		}
1131
		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
J
James Smart 已提交
1132
		if (cnt) {
1133
			len += scnprintf(buf + len, size - len,
J
James Smart 已提交
1134
					"ABORT: %d ctx entries\n", cnt);
1135
			spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
J
James Smart 已提交
1136 1137 1138 1139 1140
			list_for_each_entry_safe(ctxp, next_ctxp,
				    &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
				    list) {
				if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ))
					break;
1141
				len += scnprintf(buf + len, size - len,
J
James Smart 已提交
1142 1143 1144 1145 1146
						"Entry: oxid %x state %x "
						"flag %x\n",
						ctxp->oxid, ctxp->state,
						ctxp->flag);
			}
1147
			spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
J
James Smart 已提交
1148
		}
1149

1150 1151 1152 1153
		/* Calculate outstanding IOs */
		tot = atomic_read(&tgtp->rcv_fcp_cmd_drop);
		tot += atomic_read(&tgtp->xmt_fcp_release);
		tot = atomic_read(&tgtp->rcv_fcp_cmd_in) - tot;
1154

1155
		len += scnprintf(buf + len, size - len,
1156 1157
				"IO_CTX: %08x  WAIT: cur %08x tot %08x\n"
				"CTX Outstanding %08llx\n",
1158
				phba->sli4_hba.nvmet_xri_cnt,
1159
				phba->sli4_hba.nvmet_io_wait_cnt,
1160 1161
				phba->sli4_hba.nvmet_io_wait_total,
				tot);
1162
	} else {
1163
		if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
1164 1165
			return len;

1166 1167 1168 1169 1170 1171 1172
		localport = vport->localport;
		if (!localport)
			return len;
		lport = (struct lpfc_nvme_lport *)localport->private;
		if (!lport)
			return len;

1173
		len += scnprintf(buf + len, size - len,
1174
				"\nNVME HDWQ Statistics\n");
1175

1176
		len += scnprintf(buf + len, size - len,
1177 1178 1179 1180 1181 1182
				"LS: Xmt %016x Cmpl %016x\n",
				atomic_read(&lport->fc4NvmeLsRequests),
				atomic_read(&lport->fc4NvmeLsCmpls));

		totin = 0;
		totout = 0;
1183
		for (i = 0; i < phba->cfg_hdw_queue; i++) {
1184 1185
			cstat = &phba->sli4_hba.hdwq[i].nvme_cstat;
			tot = cstat->io_cmpls;
1186
			totin += tot;
1187 1188 1189
			data1 = cstat->input_requests;
			data2 = cstat->output_requests;
			data3 = cstat->control_requests;
1190 1191 1192 1193 1194 1195
			totout += (data1 + data2 + data3);

			/* Limit to 32, debugfs display buffer limitation */
			if (i >= 32)
				continue;

1196
			len += scnprintf(buf + len, PAGE_SIZE - len,
1197
					"HDWQ (%d): Rd %016llx Wr %016llx "
1198 1199
					"IO %016llx ",
					i, data1, data2, data3);
1200
			len += scnprintf(buf + len, PAGE_SIZE - len,
1201 1202 1203
					"Cmpl %016llx OutIO %016llx\n",
					tot, ((data1 + data2 + data3) - tot));
		}
1204
		len += scnprintf(buf + len, PAGE_SIZE - len,
1205 1206 1207 1208
				"Total FCP Cmpl %016llx Issue %016llx "
				"OutIO %016llx\n",
				totin, totout, totout - totin);

1209
		len += scnprintf(buf + len, size - len,
1210 1211 1212 1213 1214 1215 1216
				"LS Xmt Err: Abrt %08x Err %08x  "
				"Cmpl Err: xb %08x Err %08x\n",
				atomic_read(&lport->xmt_ls_abort),
				atomic_read(&lport->xmt_ls_err),
				atomic_read(&lport->cmpl_ls_xb),
				atomic_read(&lport->cmpl_ls_err));

1217
		len += scnprintf(buf + len, size - len,
1218
				"FCP Xmt Err: noxri %06x nondlp %06x "
1219
				"qdepth %06x wqerr %06x err %06x Abrt %06x\n",
1220 1221 1222 1223
				atomic_read(&lport->xmt_fcp_noxri),
				atomic_read(&lport->xmt_fcp_bad_ndlp),
				atomic_read(&lport->xmt_fcp_qdepth),
				atomic_read(&lport->xmt_fcp_wqerr),
1224
				atomic_read(&lport->xmt_fcp_err),
1225 1226
				atomic_read(&lport->xmt_fcp_abort));

1227
		len += scnprintf(buf + len, size - len,
1228 1229 1230 1231
				"FCP Cmpl Err: xb %08x Err %08x\n",
				atomic_read(&lport->cmpl_fcp_xb),
				atomic_read(&lport->cmpl_fcp_err));

1232 1233 1234 1235 1236
	}

	return len;
}

1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
/**
 * lpfc_debugfs_scsistat_data - Dump target node list to a buffer
 * @vport: The vport to gather target node info from.
 * @buf: The buffer to dump log into.
 * @size: The maximum amount of data to process.
 *
 * Description:
 * This routine dumps the SCSI statistics associated with @vport
 *
 * Return Value:
 * This routine returns the amount of bytes that were dumped into @buf and will
 * not exceed @size.
 **/
static int
lpfc_debugfs_scsistat_data(struct lpfc_vport *vport, char *buf, int size)
{
	int len;
	struct lpfc_hba *phba = vport->phba;
	struct lpfc_fc4_ctrl_stat *cstat;
	u64 data1, data2, data3;
	u64 tot, totin, totout;
	int i;
	char tmp[LPFC_MAX_SCSI_INFO_TMP_LEN] = {0};

1261
	if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ||
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
	    (phba->sli_rev != LPFC_SLI_REV4))
		return 0;

	scnprintf(buf, size, "SCSI HDWQ Statistics\n");

	totin = 0;
	totout = 0;
	for (i = 0; i < phba->cfg_hdw_queue; i++) {
		cstat = &phba->sli4_hba.hdwq[i].scsi_cstat;
		tot = cstat->io_cmpls;
		totin += tot;
		data1 = cstat->input_requests;
		data2 = cstat->output_requests;
		data3 = cstat->control_requests;
		totout += (data1 + data2 + data3);

		scnprintf(tmp, sizeof(tmp), "HDWQ (%d): Rd %016llx Wr %016llx "
			  "IO %016llx ", i, data1, data2, data3);
		if (strlcat(buf, tmp, size) >= size)
			goto buffer_done;

		scnprintf(tmp, sizeof(tmp), "Cmpl %016llx OutIO %016llx\n",
			  tot, ((data1 + data2 + data3) - tot));
		if (strlcat(buf, tmp, size) >= size)
			goto buffer_done;
	}
	scnprintf(tmp, sizeof(tmp), "Total FCP Cmpl %016llx Issue %016llx "
		  "OutIO %016llx\n", totin, totout, totout - totin);
	strlcat(buf, tmp, size);

buffer_done:
	len = strnlen(buf, size);

	return len;
}
1297

1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
void
lpfc_io_ktime(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
{
	uint64_t seg1, seg2, seg3, seg4;
	uint64_t segsum;

	if (!lpfc_cmd->ts_last_cmd ||
	    !lpfc_cmd->ts_cmd_start ||
	    !lpfc_cmd->ts_cmd_wqput ||
	    !lpfc_cmd->ts_isr_cmpl ||
	    !lpfc_cmd->ts_data_io)
		return;

	if (lpfc_cmd->ts_data_io < lpfc_cmd->ts_cmd_start)
		return;
	if (lpfc_cmd->ts_cmd_start < lpfc_cmd->ts_last_cmd)
		return;
	if (lpfc_cmd->ts_cmd_wqput < lpfc_cmd->ts_cmd_start)
		return;
	if (lpfc_cmd->ts_isr_cmpl < lpfc_cmd->ts_cmd_wqput)
		return;
	if (lpfc_cmd->ts_data_io < lpfc_cmd->ts_isr_cmpl)
		return;
	/*
	 * Segment 1 - Time from Last FCP command cmpl is handed
	 * off to NVME Layer to start of next command.
	 * Segment 2 - Time from Driver receives a IO cmd start
	 * from NVME Layer to WQ put is done on IO cmd.
	 * Segment 3 - Time from Driver WQ put is done on IO cmd
	 * to MSI-X ISR for IO cmpl.
	 * Segment 4 - Time from MSI-X ISR for IO cmpl to when
	 * cmpl is handled off to the NVME Layer.
	 */
	seg1 = lpfc_cmd->ts_cmd_start - lpfc_cmd->ts_last_cmd;
	if (seg1 > 5000000)  /* 5 ms - for sequential IOs only */
		seg1 = 0;

	/* Calculate times relative to start of IO */
	seg2 = (lpfc_cmd->ts_cmd_wqput - lpfc_cmd->ts_cmd_start);
	segsum = seg2;
	seg3 = lpfc_cmd->ts_isr_cmpl - lpfc_cmd->ts_cmd_start;
	if (segsum > seg3)
		return;
	seg3 -= segsum;
	segsum += seg3;

	seg4 = lpfc_cmd->ts_data_io - lpfc_cmd->ts_cmd_start;
	if (segsum > seg4)
		return;
	seg4 -= segsum;

	phba->ktime_data_samples++;
	phba->ktime_seg1_total += seg1;
	if (seg1 < phba->ktime_seg1_min)
		phba->ktime_seg1_min = seg1;
	else if (seg1 > phba->ktime_seg1_max)
		phba->ktime_seg1_max = seg1;
	phba->ktime_seg2_total += seg2;
	if (seg2 < phba->ktime_seg2_min)
		phba->ktime_seg2_min = seg2;
	else if (seg2 > phba->ktime_seg2_max)
		phba->ktime_seg2_max = seg2;
	phba->ktime_seg3_total += seg3;
	if (seg3 < phba->ktime_seg3_min)
		phba->ktime_seg3_min = seg3;
	else if (seg3 > phba->ktime_seg3_max)
		phba->ktime_seg3_max = seg3;
	phba->ktime_seg4_total += seg4;
	if (seg4 < phba->ktime_seg4_min)
		phba->ktime_seg4_min = seg4;
	else if (seg4 > phba->ktime_seg4_max)
		phba->ktime_seg4_max = seg4;

	lpfc_cmd->ts_last_cmd = 0;
	lpfc_cmd->ts_cmd_start = 0;
	lpfc_cmd->ts_cmd_wqput  = 0;
	lpfc_cmd->ts_isr_cmpl = 0;
	lpfc_cmd->ts_data_io = 0;
}

1378
/**
1379
 * lpfc_debugfs_ioktime_data - Dump target node list to a buffer
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
 * @vport: The vport to gather target node info from.
 * @buf: The buffer to dump log into.
 * @size: The maximum amount of data to process.
 *
 * Description:
 * This routine dumps the NVME statistics associated with @vport
 *
 * Return Value:
 * This routine returns the amount of bytes that were dumped into @buf and will
 * not exceed @size.
 **/
static int
1392
lpfc_debugfs_ioktime_data(struct lpfc_vport *vport, char *buf, int size)
1393 1394 1395 1396 1397
{
	struct lpfc_hba   *phba = vport->phba;
	int len = 0;

	if (phba->nvmet_support == 0) {
1398
		/* Initiator */
1399
		len += scnprintf(buf + len, PAGE_SIZE - len,
1400 1401 1402 1403 1404 1405
				"ktime %s: Total Samples: %lld\n",
				(phba->ktime_on ?  "Enabled" : "Disabled"),
				phba->ktime_data_samples);
		if (phba->ktime_data_samples == 0)
			return len;

1406
		len += scnprintf(
1407
			buf + len, PAGE_SIZE - len,
1408 1409
			"Segment 1: Last Cmd cmpl "
			"done -to- Start of next Cmd (in driver)\n");
1410
		len += scnprintf(
1411 1412
			buf + len, PAGE_SIZE - len,
			"avg:%08lld min:%08lld max %08lld\n",
1413 1414
			div_u64(phba->ktime_seg1_total,
				phba->ktime_data_samples),
1415 1416
			phba->ktime_seg1_min,
			phba->ktime_seg1_max);
1417
		len += scnprintf(
1418
			buf + len, PAGE_SIZE - len,
1419
			"Segment 2: Driver start of Cmd "
1420
			"-to- Firmware WQ doorbell\n");
1421
		len += scnprintf(
1422 1423
			buf + len, PAGE_SIZE - len,
			"avg:%08lld min:%08lld max %08lld\n",
1424 1425
			div_u64(phba->ktime_seg2_total,
				phba->ktime_data_samples),
1426 1427
			phba->ktime_seg2_min,
			phba->ktime_seg2_max);
1428
		len += scnprintf(
1429 1430 1431
			buf + len, PAGE_SIZE - len,
			"Segment 3: Firmware WQ doorbell -to- "
			"MSI-X ISR cmpl\n");
1432
		len += scnprintf(
1433 1434
			buf + len, PAGE_SIZE - len,
			"avg:%08lld min:%08lld max %08lld\n",
1435 1436
			div_u64(phba->ktime_seg3_total,
				phba->ktime_data_samples),
1437 1438
			phba->ktime_seg3_min,
			phba->ktime_seg3_max);
1439
		len += scnprintf(
1440 1441
			buf + len, PAGE_SIZE - len,
			"Segment 4: MSI-X ISR cmpl -to- "
1442
			"Cmd cmpl done\n");
1443
		len += scnprintf(
1444 1445
			buf + len, PAGE_SIZE - len,
			"avg:%08lld min:%08lld max %08lld\n",
1446 1447
			div_u64(phba->ktime_seg4_total,
				phba->ktime_data_samples),
1448 1449
			phba->ktime_seg4_min,
			phba->ktime_seg4_max);
1450
		len += scnprintf(
1451 1452
			buf + len, PAGE_SIZE - len,
			"Total IO avg time: %08lld\n",
1453
			div_u64(phba->ktime_seg1_total +
1454 1455
			phba->ktime_seg2_total  +
			phba->ktime_seg3_total +
1456
			phba->ktime_seg4_total,
1457 1458 1459
			phba->ktime_data_samples));
		return len;
	}
1460 1461

	/* NVME Target */
1462
	len += scnprintf(buf + len, PAGE_SIZE-len,
1463 1464 1465 1466 1467 1468 1469
			"ktime %s: Total Samples: %lld %lld\n",
			(phba->ktime_on ? "Enabled" : "Disabled"),
			phba->ktime_data_samples,
			phba->ktime_status_samples);
	if (phba->ktime_data_samples == 0)
		return len;

1470
	len += scnprintf(buf + len, PAGE_SIZE-len,
1471 1472
			"Segment 1: MSI-X ISR Rcv cmd -to- "
			"cmd pass to NVME Layer\n");
1473
	len += scnprintf(buf + len, PAGE_SIZE-len,
1474
			"avg:%08lld min:%08lld max %08lld\n",
1475 1476
			div_u64(phba->ktime_seg1_total,
				phba->ktime_data_samples),
1477 1478
			phba->ktime_seg1_min,
			phba->ktime_seg1_max);
1479
	len += scnprintf(buf + len, PAGE_SIZE-len,
1480 1481
			"Segment 2: cmd pass to NVME Layer- "
			"-to- Driver rcv cmd OP (action)\n");
1482
	len += scnprintf(buf + len, PAGE_SIZE-len,
1483
			"avg:%08lld min:%08lld max %08lld\n",
1484 1485
			div_u64(phba->ktime_seg2_total,
				phba->ktime_data_samples),
1486 1487
			phba->ktime_seg2_min,
			phba->ktime_seg2_max);
1488
	len += scnprintf(buf + len, PAGE_SIZE-len,
1489 1490
			"Segment 3: Driver rcv cmd OP -to- "
			"Firmware WQ doorbell: cmd\n");
1491
	len += scnprintf(buf + len, PAGE_SIZE-len,
1492
			"avg:%08lld min:%08lld max %08lld\n",
1493 1494
			div_u64(phba->ktime_seg3_total,
				phba->ktime_data_samples),
1495 1496
			phba->ktime_seg3_min,
			phba->ktime_seg3_max);
1497
	len += scnprintf(buf + len, PAGE_SIZE-len,
1498 1499
			"Segment 4: Firmware WQ doorbell: cmd "
			"-to- MSI-X ISR for cmd cmpl\n");
1500
	len += scnprintf(buf + len, PAGE_SIZE-len,
1501
			"avg:%08lld min:%08lld max %08lld\n",
1502 1503
			div_u64(phba->ktime_seg4_total,
				phba->ktime_data_samples),
1504 1505
			phba->ktime_seg4_min,
			phba->ktime_seg4_max);
1506
	len += scnprintf(buf + len, PAGE_SIZE-len,
1507 1508
			"Segment 5: MSI-X ISR for cmd cmpl "
			"-to- NVME layer passed cmd done\n");
1509
	len += scnprintf(buf + len, PAGE_SIZE-len,
1510
			"avg:%08lld min:%08lld max %08lld\n",
1511 1512
			div_u64(phba->ktime_seg5_total,
				phba->ktime_data_samples),
1513 1514 1515 1516
			phba->ktime_seg5_min,
			phba->ktime_seg5_max);

	if (phba->ktime_status_samples == 0) {
1517
		len += scnprintf(buf + len, PAGE_SIZE-len,
1518 1519
				"Total: cmd received by MSI-X ISR "
				"-to- cmd completed on wire\n");
1520
		len += scnprintf(buf + len, PAGE_SIZE-len,
1521 1522
				"avg:%08lld min:%08lld "
				"max %08lld\n",
1523 1524
				div_u64(phba->ktime_seg10_total,
					phba->ktime_data_samples),
1525 1526 1527 1528 1529
				phba->ktime_seg10_min,
				phba->ktime_seg10_max);
		return len;
	}

1530
	len += scnprintf(buf + len, PAGE_SIZE-len,
1531 1532
			"Segment 6: NVME layer passed cmd done "
			"-to- Driver rcv rsp status OP\n");
1533
	len += scnprintf(buf + len, PAGE_SIZE-len,
1534
			"avg:%08lld min:%08lld max %08lld\n",
1535 1536
			div_u64(phba->ktime_seg6_total,
				phba->ktime_status_samples),
1537 1538
			phba->ktime_seg6_min,
			phba->ktime_seg6_max);
1539
	len += scnprintf(buf + len, PAGE_SIZE-len,
1540 1541
			"Segment 7: Driver rcv rsp status OP "
			"-to- Firmware WQ doorbell: status\n");
1542
	len += scnprintf(buf + len, PAGE_SIZE-len,
1543
			"avg:%08lld min:%08lld max %08lld\n",
1544 1545
			div_u64(phba->ktime_seg7_total,
				phba->ktime_status_samples),
1546 1547
			phba->ktime_seg7_min,
			phba->ktime_seg7_max);
1548
	len += scnprintf(buf + len, PAGE_SIZE-len,
1549 1550
			"Segment 8: Firmware WQ doorbell: status"
			" -to- MSI-X ISR for status cmpl\n");
1551
	len += scnprintf(buf + len, PAGE_SIZE-len,
1552
			"avg:%08lld min:%08lld max %08lld\n",
1553 1554
			div_u64(phba->ktime_seg8_total,
				phba->ktime_status_samples),
1555 1556
			phba->ktime_seg8_min,
			phba->ktime_seg8_max);
1557
	len += scnprintf(buf + len, PAGE_SIZE-len,
1558 1559
			"Segment 9: MSI-X ISR for status cmpl  "
			"-to- NVME layer passed status done\n");
1560
	len += scnprintf(buf + len, PAGE_SIZE-len,
1561
			"avg:%08lld min:%08lld max %08lld\n",
1562 1563
			div_u64(phba->ktime_seg9_total,
				phba->ktime_status_samples),
1564 1565
			phba->ktime_seg9_min,
			phba->ktime_seg9_max);
1566
	len += scnprintf(buf + len, PAGE_SIZE-len,
1567 1568
			"Total: cmd received by MSI-X ISR -to- "
			"cmd completed on wire\n");
1569
	len += scnprintf(buf + len, PAGE_SIZE-len,
1570
			"avg:%08lld min:%08lld max %08lld\n",
1571 1572
			div_u64(phba->ktime_seg10_total,
				phba->ktime_status_samples),
1573 1574
			phba->ktime_seg10_min,
			phba->ktime_seg10_max);
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
	return len;
}

/**
 * lpfc_debugfs_nvmeio_trc_data - Dump NVME IO trace list to a buffer
 * @phba: The phba to gather target node info from.
 * @buf: The buffer to dump log into.
 * @size: The maximum amount of data to process.
 *
 * Description:
 * This routine dumps the NVME IO trace associated with @phba
 *
 * Return Value:
 * This routine returns the amount of bytes that were dumped into @buf and will
 * not exceed @size.
 **/
static int
lpfc_debugfs_nvmeio_trc_data(struct lpfc_hba *phba, char *buf, int size)
{
	struct lpfc_debugfs_nvmeio_trc *dtp;
	int i, state, index, skip;
	int len = 0;

	state = phba->nvmeio_trc_on;

	index = (atomic_read(&phba->nvmeio_trc_cnt) + 1) &
		(phba->nvmeio_trc_size - 1);
	skip = phba->nvmeio_trc_output_idx;

1604
	len += scnprintf(buf + len, size - len,
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
			"%s IO Trace %s: next_idx %d skip %d size %d\n",
			(phba->nvmet_support ? "NVME" : "NVMET"),
			(state ? "Enabled" : "Disabled"),
			index, skip, phba->nvmeio_trc_size);

	if (!phba->nvmeio_trc || state)
		return len;

	/* trace MUST bhe off to continue */

	for (i = index; i < phba->nvmeio_trc_size; i++) {
		if (skip) {
			skip--;
			continue;
		}
		dtp = phba->nvmeio_trc + i;
		phba->nvmeio_trc_output_idx++;

		if (!dtp->fmt)
			continue;

1626
		len +=  scnprintf(buf + len, size - len, dtp->fmt,
1627 1628 1629 1630
			dtp->data1, dtp->data2, dtp->data3);

		if (phba->nvmeio_trc_output_idx >= phba->nvmeio_trc_size) {
			phba->nvmeio_trc_output_idx = 0;
1631
			len += scnprintf(buf + len, size - len,
1632 1633 1634 1635 1636
					"Trace Complete\n");
			goto out;
		}

		if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ)) {
1637
			len += scnprintf(buf + len, size - len,
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
					"Trace Continue (%d of %d)\n",
					phba->nvmeio_trc_output_idx,
					phba->nvmeio_trc_size);
			goto out;
		}
	}
	for (i = 0; i < index; i++) {
		if (skip) {
			skip--;
			continue;
		}
		dtp = phba->nvmeio_trc + i;
		phba->nvmeio_trc_output_idx++;

		if (!dtp->fmt)
			continue;

1655
		len +=  scnprintf(buf + len, size - len, dtp->fmt,
1656 1657 1658 1659
			dtp->data1, dtp->data2, dtp->data3);

		if (phba->nvmeio_trc_output_idx >= phba->nvmeio_trc_size) {
			phba->nvmeio_trc_output_idx = 0;
1660
			len += scnprintf(buf + len, size - len,
1661 1662 1663 1664 1665
					"Trace Complete\n");
			goto out;
		}

		if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ)) {
1666
			len += scnprintf(buf + len, size - len,
1667 1668 1669 1670 1671 1672 1673
					"Trace Continue (%d of %d)\n",
					phba->nvmeio_trc_output_idx,
					phba->nvmeio_trc_size);
			goto out;
		}
	}

1674
	len += scnprintf(buf + len, size - len,
1675 1676 1677 1678 1679 1680
			"Trace Done\n");
out:
	return len;
}

/**
1681
 * lpfc_debugfs_hdwqstat_data - Dump I/O stats to a buffer
1682 1683 1684 1685 1686
 * @vport: The vport to gather target node info from.
 * @buf: The buffer to dump log into.
 * @size: The maximum amount of data to process.
 *
 * Description:
1687
 * This routine dumps the NVME + SCSI statistics associated with @vport
1688 1689 1690 1691 1692 1693
 *
 * Return Value:
 * This routine returns the amount of bytes that were dumped into @buf and will
 * not exceed @size.
 **/
static int
1694
lpfc_debugfs_hdwqstat_data(struct lpfc_vport *vport, char *buf, int size)
1695 1696
{
	struct lpfc_hba   *phba = vport->phba;
1697 1698
	struct lpfc_hdwq_stat *c_stat;
	int i, j, len;
1699 1700 1701
	uint32_t tot_xmt;
	uint32_t tot_rcv;
	uint32_t tot_cmpl;
1702
	char tmp[LPFC_MAX_SCSI_INFO_TMP_LEN] = {0};
1703

1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
	scnprintf(tmp, sizeof(tmp), "HDWQ Stats:\n\n");
	if (strlcat(buf, tmp, size) >= size)
		goto buffer_done;

	scnprintf(tmp, sizeof(tmp), "(NVME Accounting: %s) ",
		  (phba->hdwqstat_on &
		  (LPFC_CHECK_NVME_IO | LPFC_CHECK_NVMET_IO) ?
		  "Enabled" : "Disabled"));
	if (strlcat(buf, tmp, size) >= size)
		goto buffer_done;

	scnprintf(tmp, sizeof(tmp), "(SCSI Accounting: %s) ",
		  (phba->hdwqstat_on & LPFC_CHECK_SCSI_IO ?
		  "Enabled" : "Disabled"));
	if (strlcat(buf, tmp, size) >= size)
		goto buffer_done;

	scnprintf(tmp, sizeof(tmp), "\n\n");
	if (strlcat(buf, tmp, size) >= size)
		goto buffer_done;
1724

1725 1726 1727 1728 1729
	for (i = 0; i < phba->cfg_hdw_queue; i++) {
		tot_rcv = 0;
		tot_xmt = 0;
		tot_cmpl = 0;

1730 1731 1732 1733 1734 1735
		for_each_present_cpu(j) {
			c_stat = per_cpu_ptr(phba->sli4_hba.c_stat, j);

			/* Only display for this HDWQ */
			if (i != c_stat->hdwq_no)
				continue;
1736 1737

			/* Only display non-zero counters */
1738 1739
			if (!c_stat->xmt_io && !c_stat->cmpl_io &&
			    !c_stat->rcv_io)
1740
				continue;
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757

			if (!tot_xmt && !tot_cmpl && !tot_rcv) {
				/* Print HDWQ string only the first time */
				scnprintf(tmp, sizeof(tmp), "[HDWQ %d]:\t", i);
				if (strlcat(buf, tmp, size) >= size)
					goto buffer_done;
			}

			tot_xmt += c_stat->xmt_io;
			tot_cmpl += c_stat->cmpl_io;
			if (phba->nvmet_support)
				tot_rcv += c_stat->rcv_io;

			scnprintf(tmp, sizeof(tmp), "| [CPU %d]: ", j);
			if (strlcat(buf, tmp, size) >= size)
				goto buffer_done;

1758
			if (phba->nvmet_support) {
1759 1760 1761 1762 1763 1764
				scnprintf(tmp, sizeof(tmp),
					  "XMT 0x%x CMPL 0x%x RCV 0x%x |",
					  c_stat->xmt_io, c_stat->cmpl_io,
					  c_stat->rcv_io);
				if (strlcat(buf, tmp, size) >= size)
					goto buffer_done;
1765
			} else {
1766 1767 1768 1769 1770
				scnprintf(tmp, sizeof(tmp),
					  "XMT 0x%x CMPL 0x%x |",
					  c_stat->xmt_io, c_stat->cmpl_io);
				if (strlcat(buf, tmp, size) >= size)
					goto buffer_done;
1771 1772
			}
		}
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793

		/* Check if nothing to display */
		if (!tot_xmt && !tot_cmpl && !tot_rcv)
			continue;

		scnprintf(tmp, sizeof(tmp), "\t->\t[HDWQ Total: ");
		if (strlcat(buf, tmp, size) >= size)
			goto buffer_done;

		if (phba->nvmet_support) {
			scnprintf(tmp, sizeof(tmp),
				  "XMT 0x%x CMPL 0x%x RCV 0x%x]\n\n",
				  tot_xmt, tot_cmpl, tot_rcv);
			if (strlcat(buf, tmp, size) >= size)
				goto buffer_done;
		} else {
			scnprintf(tmp, sizeof(tmp),
				  "XMT 0x%x CMPL 0x%x]\n\n",
				  tot_xmt, tot_cmpl);
			if (strlcat(buf, tmp, size) >= size)
				goto buffer_done;
1794
		}
1795
	}
1796 1797 1798

buffer_done:
	len = strnlen(buf, size);
J
James Smart 已提交
1799 1800
	return len;
}
J
James Smart 已提交
1801

J
James Smart 已提交
1802 1803
#endif

1804
/**
1805
 * lpfc_debugfs_disc_trc - Store discovery trace log
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
 * @vport: The vport to associate this trace string with for retrieval.
 * @mask: Log entry classification.
 * @fmt: Format string to be displayed when dumping the log.
 * @data1: 1st data parameter to be applied to @fmt.
 * @data2: 2nd data parameter to be applied to @fmt.
 * @data3: 3rd data parameter to be applied to @fmt.
 *
 * Description:
 * This routine is used by the driver code to add a debugfs log entry to the
 * discovery trace buffer associated with @vport. Only entries with a @mask that
 * match the current debugfs discovery mask will be saved. Entries that do not
 * match will be thrown away. @fmt, @data1, @data2, and @data3 are used like
 * printf when displaying the log.
 **/
J
James Smart 已提交
1820 1821 1822 1823
inline void
lpfc_debugfs_disc_trc(struct lpfc_vport *vport, int mask, char *fmt,
	uint32_t data1, uint32_t data2, uint32_t data3)
{
1824
#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1825
	struct lpfc_debugfs_trc *dtp;
J
James Smart 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
	int index;

	if (!(lpfc_debugfs_mask_disc_trc & mask))
		return;

	if (!lpfc_debugfs_enable || !lpfc_debugfs_max_disc_trc ||
		!vport || !vport->disc_trc)
		return;

	index = atomic_inc_return(&vport->disc_trc_cnt) &
		(lpfc_debugfs_max_disc_trc - 1);
	dtp = vport->disc_trc + index;
	dtp->fmt = fmt;
	dtp->data1 = data1;
	dtp->data2 = data2;
	dtp->data3 = data3;
1842 1843 1844 1845 1846 1847
	dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_seq_trc_cnt);
	dtp->jif = jiffies;
#endif
	return;
}

1848
/**
1849
 * lpfc_debugfs_slow_ring_trc - Store slow ring trace log
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
 * @phba: The phba to associate this trace string with for retrieval.
 * @fmt: Format string to be displayed when dumping the log.
 * @data1: 1st data parameter to be applied to @fmt.
 * @data2: 2nd data parameter to be applied to @fmt.
 * @data3: 3rd data parameter to be applied to @fmt.
 *
 * Description:
 * This routine is used by the driver code to add a debugfs log entry to the
 * discovery trace buffer associated with @vport. @fmt, @data1, @data2, and
 * @data3 are used like printf when displaying the log.
 **/
1861 1862 1863 1864
inline void
lpfc_debugfs_slow_ring_trc(struct lpfc_hba *phba, char *fmt,
	uint32_t data1, uint32_t data2, uint32_t data3)
{
1865
#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
	struct lpfc_debugfs_trc *dtp;
	int index;

	if (!lpfc_debugfs_enable || !lpfc_debugfs_max_slow_ring_trc ||
		!phba || !phba->slow_ring_trc)
		return;

	index = atomic_inc_return(&phba->slow_ring_trc_cnt) &
		(lpfc_debugfs_max_slow_ring_trc - 1);
	dtp = phba->slow_ring_trc + index;
	dtp->fmt = fmt;
	dtp->data1 = data1;
	dtp->data2 = data2;
	dtp->data3 = data3;
	dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_seq_trc_cnt);
J
James Smart 已提交
1881 1882 1883 1884 1885
	dtp->jif = jiffies;
#endif
	return;
}

1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
/**
 * lpfc_debugfs_nvme_trc - Store NVME/NVMET trace log
 * @phba: The phba to associate this trace string with for retrieval.
 * @fmt: Format string to be displayed when dumping the log.
 * @data1: 1st data parameter to be applied to @fmt.
 * @data2: 2nd data parameter to be applied to @fmt.
 * @data3: 3rd data parameter to be applied to @fmt.
 *
 * Description:
 * This routine is used by the driver code to add a debugfs log entry to the
 * nvme trace buffer associated with @phba. @fmt, @data1, @data2, and
 * @data3 are used like printf when displaying the log.
 **/
inline void
lpfc_debugfs_nvme_trc(struct lpfc_hba *phba, char *fmt,
		      uint16_t data1, uint16_t data2, uint32_t data3)
{
#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
	struct lpfc_debugfs_nvmeio_trc *dtp;
	int index;

	if (!phba->nvmeio_trc_on || !phba->nvmeio_trc)
		return;

	index = atomic_inc_return(&phba->nvmeio_trc_cnt) &
		(phba->nvmeio_trc_size - 1);
	dtp = phba->nvmeio_trc + index;
	dtp->fmt = fmt;
	dtp->data1 = data1;
	dtp->data2 = data2;
	dtp->data3 = data3;
#endif
}

1920
#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1921
/**
1922
 * lpfc_debugfs_disc_trc_open - Open the discovery trace log
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
 * @inode: The inode pointer that contains a vport pointer.
 * @file: The file pointer to attach the log output.
 *
 * Description:
 * This routine is the entry point for the debugfs open file operation. It gets
 * the vport from the i_private field in @inode, allocates the necessary buffer
 * for the log, fills the buffer from the in-memory log for this vport, and then
 * returns a pointer to that log in the private_data field in @file.
 *
 * Returns:
1933
 * This function returns zero if successful. On error it will return a negative
1934 1935
 * error value.
 **/
J
James Smart 已提交
1936 1937 1938 1939 1940 1941 1942 1943 1944
static int
lpfc_debugfs_disc_trc_open(struct inode *inode, struct file *file)
{
	struct lpfc_vport *vport = inode->i_private;
	struct lpfc_debug *debug;
	int size;
	int rc = -ENOMEM;

	if (!lpfc_debugfs_max_disc_trc) {
1945
		rc = -ENOSPC;
J
James Smart 已提交
1946 1947 1948 1949 1950 1951 1952
		goto out;
	}

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

1953
	/* Round to page boundary */
1954
	size =  (lpfc_debugfs_max_disc_trc * LPFC_DEBUG_TRC_ENTRY_SIZE);
J
James Smart 已提交
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
	size = PAGE_ALIGN(size);

	debug->buffer = kmalloc(size, GFP_KERNEL);
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

	debug->len = lpfc_debugfs_disc_trc_data(vport, debug->buffer, size);
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

1971
/**
1972
 * lpfc_debugfs_slow_ring_trc_open - Open the Slow Ring trace log
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
 * @inode: The inode pointer that contains a vport pointer.
 * @file: The file pointer to attach the log output.
 *
 * Description:
 * This routine is the entry point for the debugfs open file operation. It gets
 * the vport from the i_private field in @inode, allocates the necessary buffer
 * for the log, fills the buffer from the in-memory log for this vport, and then
 * returns a pointer to that log in the private_data field in @file.
 *
 * Returns:
1983
 * This function returns zero if successful. On error it will return a negative
1984 1985
 * error value.
 **/
1986 1987 1988 1989 1990 1991 1992 1993 1994
static int
lpfc_debugfs_slow_ring_trc_open(struct inode *inode, struct file *file)
{
	struct lpfc_hba *phba = inode->i_private;
	struct lpfc_debug *debug;
	int size;
	int rc = -ENOMEM;

	if (!lpfc_debugfs_max_slow_ring_trc) {
1995
		rc = -ENOSPC;
1996 1997 1998 1999 2000 2001 2002
		goto out;
	}

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

2003
	/* Round to page boundary */
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
	size =  (lpfc_debugfs_max_slow_ring_trc * LPFC_DEBUG_TRC_ENTRY_SIZE);
	size = PAGE_ALIGN(size);

	debug->buffer = kmalloc(size, GFP_KERNEL);
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

	debug->len = lpfc_debugfs_slow_ring_trc_data(phba, debug->buffer, size);
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

2021
/**
2022
 * lpfc_debugfs_hbqinfo_open - Open the hbqinfo debugfs buffer
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
 * @inode: The inode pointer that contains a vport pointer.
 * @file: The file pointer to attach the log output.
 *
 * Description:
 * This routine is the entry point for the debugfs open file operation. It gets
 * the vport from the i_private field in @inode, allocates the necessary buffer
 * for the log, fills the buffer from the in-memory log for this vport, and then
 * returns a pointer to that log in the private_data field in @file.
 *
 * Returns:
2033
 * This function returns zero if successful. On error it will return a negative
2034 2035
 * error value.
 **/
2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
static int
lpfc_debugfs_hbqinfo_open(struct inode *inode, struct file *file)
{
	struct lpfc_hba *phba = inode->i_private;
	struct lpfc_debug *debug;
	int rc = -ENOMEM;

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

2047
	/* Round to page boundary */
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
	debug->buffer = kmalloc(LPFC_HBQINFO_SIZE, GFP_KERNEL);
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

	debug->len = lpfc_debugfs_hbqinfo_data(phba, debug->buffer,
		LPFC_HBQINFO_SIZE);
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
/**
 * lpfc_debugfs_multixripools_open - Open the multixripool debugfs buffer
 * @inode: The inode pointer that contains a hba pointer.
 * @file: The file pointer to attach the log output.
 *
 * Description:
 * This routine is the entry point for the debugfs open file operation. It gets
 * the hba from the i_private field in @inode, allocates the necessary buffer
 * for the log, fills the buffer from the in-memory log for this hba, and then
 * returns a pointer to that log in the private_data field in @file.
 *
 * Returns:
 * This function returns zero if successful. On error it will return a negative
 * error value.
 **/
static int
lpfc_debugfs_multixripools_open(struct inode *inode, struct file *file)
{
	struct lpfc_hba *phba = inode->i_private;
	struct lpfc_debug *debug;
	int rc = -ENOMEM;

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

	/* Round to page boundary */
	debug->buffer = kzalloc(LPFC_DUMP_MULTIXRIPOOL_SIZE, GFP_KERNEL);
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

2096 2097
	debug->len = lpfc_debugfs_multixripools_data(
		phba, debug->buffer, LPFC_DUMP_MULTIXRIPOOL_SIZE);
2098 2099 2100 2101 2102 2103 2104 2105 2106

	debug->i_private = inode->i_private;
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

2107
#ifdef LPFC_HDWQ_LOCK_STAT
2108
/**
2109
 * lpfc_debugfs_lockstat_open - Open the lockstat debugfs buffer
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
 * @inode: The inode pointer that contains a vport pointer.
 * @file: The file pointer to attach the log output.
 *
 * Description:
 * This routine is the entry point for the debugfs open file operation. It gets
 * the vport from the i_private field in @inode, allocates the necessary buffer
 * for the log, fills the buffer from the in-memory log for this vport, and then
 * returns a pointer to that log in the private_data field in @file.
 *
 * Returns:
 * This function returns zero if successful. On error it will return a negative
 * error value.
 **/
static int
2124
lpfc_debugfs_lockstat_open(struct inode *inode, struct file *file)
2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
{
	struct lpfc_hba *phba = inode->i_private;
	struct lpfc_debug *debug;
	int rc = -ENOMEM;

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

	/* Round to page boundary */
	debug->buffer = kmalloc(LPFC_HDWQINFO_SIZE, GFP_KERNEL);
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

2141
	debug->len = lpfc_debugfs_lockstat_data(phba, debug->buffer,
2142 2143 2144 2145 2146 2147 2148 2149
		LPFC_HBQINFO_SIZE);
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
static ssize_t
lpfc_debugfs_lockstat_write(struct file *file, const char __user *buf,
			    size_t nbytes, loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	struct lpfc_sli4_hdw_queue *qp;
	char mybuf[64];
	char *pbuf;
	int i;

	memset(mybuf, 0, sizeof(mybuf));

	if (copy_from_user(mybuf, buf, nbytes))
		return -EFAULT;
	pbuf = &mybuf[0];

	if ((strncmp(pbuf, "reset", strlen("reset")) == 0) ||
	    (strncmp(pbuf, "zero", strlen("zero")) == 0)) {
		for (i = 0; i < phba->cfg_hdw_queue; i++) {
			qp = &phba->sli4_hba.hdwq[i];
			qp->lock_conflict.alloc_xri_get = 0;
			qp->lock_conflict.alloc_xri_put = 0;
			qp->lock_conflict.free_xri = 0;
			qp->lock_conflict.wq_access = 0;
			qp->lock_conflict.alloc_pvt_pool = 0;
			qp->lock_conflict.mv_from_pvt_pool = 0;
			qp->lock_conflict.mv_to_pub_pool = 0;
			qp->lock_conflict.mv_to_pvt_pool = 0;
			qp->lock_conflict.free_pvt_pool = 0;
			qp->lock_conflict.free_pub_pool = 0;
			qp->lock_conflict.wq_access = 0;
		}
	}
	return nbytes;
}
#endif

2188 2189
static int lpfc_debugfs_ras_log_data(struct lpfc_hba *phba,
				     char *buffer, int size)
2190 2191 2192 2193
{
	int copied = 0;
	struct lpfc_dmabuf *dmabuf, *next;

J
James Smart 已提交
2194 2195
	memset(buffer, 0, size);

2196 2197 2198 2199 2200 2201 2202 2203 2204
	spin_lock_irq(&phba->hbalock);
	if (phba->ras_fwlog.state != ACTIVE) {
		spin_unlock_irq(&phba->hbalock);
		return -EINVAL;
	}
	spin_unlock_irq(&phba->hbalock);

	list_for_each_entry_safe(dmabuf, next,
				 &phba->ras_fwlog.fwlog_buff_list, list) {
J
James Smart 已提交
2205 2206 2207 2208 2209 2210 2211
		/* Check if copying will go over size and a '\0' char */
		if ((copied + LPFC_RAS_MAX_ENTRY_SIZE) >= (size - 1)) {
			memcpy(buffer + copied, dmabuf->virt,
			       size - copied - 1);
			copied += size - copied - 1;
			break;
		}
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
		memcpy(buffer + copied, dmabuf->virt, LPFC_RAS_MAX_ENTRY_SIZE);
		copied += LPFC_RAS_MAX_ENTRY_SIZE;
	}
	return copied;
}

static int
lpfc_debugfs_ras_log_release(struct inode *inode, struct file *file)
{
	struct lpfc_debug *debug = file->private_data;

	vfree(debug->buffer);
	kfree(debug);

	return 0;
}

/**
 * lpfc_debugfs_ras_log_open - Open the RAS log debugfs buffer
 * @inode: The inode pointer that contains a vport pointer.
 * @file: The file pointer to attach the log output.
 *
 * Description:
 * This routine is the entry point for the debugfs open file operation. It gets
 * the vport from the i_private field in @inode, allocates the necessary buffer
 * for the log, fills the buffer from the in-memory log for this vport, and then
 * returns a pointer to that log in the private_data field in @file.
 *
 * Returns:
 * This function returns zero if successful. On error it will return a negative
 * error value.
 **/
static int
lpfc_debugfs_ras_log_open(struct inode *inode, struct file *file)
{
	struct lpfc_hba *phba = inode->i_private;
	struct lpfc_debug *debug;
	int size;
	int rc = -ENOMEM;

	spin_lock_irq(&phba->hbalock);
	if (phba->ras_fwlog.state != ACTIVE) {
		spin_unlock_irq(&phba->hbalock);
		rc = -EINVAL;
		goto out;
	}
	spin_unlock_irq(&phba->hbalock);
	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

	size = LPFC_RAS_MIN_BUFF_POST_SIZE * phba->cfg_ras_fwlog_buffsize;
	debug->buffer = vmalloc(size);
	if (!debug->buffer)
		goto free_debug;

	debug->len = lpfc_debugfs_ras_log_data(phba, debug->buffer, size);
	if (debug->len < 0) {
		rc = -EINVAL;
		goto free_buffer;
	}
	file->private_data = debug;

	return 0;

free_buffer:
	vfree(debug->buffer);
free_debug:
	kfree(debug);
out:
	return rc;
}

2285
/**
2286
 * lpfc_debugfs_dumpHBASlim_open - Open the Dump HBA SLIM debugfs buffer
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
 * @inode: The inode pointer that contains a vport pointer.
 * @file: The file pointer to attach the log output.
 *
 * Description:
 * This routine is the entry point for the debugfs open file operation. It gets
 * the vport from the i_private field in @inode, allocates the necessary buffer
 * for the log, fills the buffer from the in-memory log for this vport, and then
 * returns a pointer to that log in the private_data field in @file.
 *
 * Returns:
2297
 * This function returns zero if successful. On error it will return a negative
2298 2299
 * error value.
 **/
2300
static int
2301
lpfc_debugfs_dumpHBASlim_open(struct inode *inode, struct file *file)
2302 2303 2304 2305 2306 2307 2308 2309 2310
{
	struct lpfc_hba *phba = inode->i_private;
	struct lpfc_debug *debug;
	int rc = -ENOMEM;

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

2311
	/* Round to page boundary */
2312
	debug->buffer = kmalloc(LPFC_DUMPHBASLIM_SIZE, GFP_KERNEL);
2313 2314 2315 2316 2317
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

2318 2319 2320 2321 2322 2323 2324 2325 2326
	debug->len = lpfc_debugfs_dumpHBASlim_data(phba, debug->buffer,
		LPFC_DUMPHBASLIM_SIZE);
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

2327
/**
2328
 * lpfc_debugfs_dumpHostSlim_open - Open the Dump Host SLIM debugfs buffer
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
 * @inode: The inode pointer that contains a vport pointer.
 * @file: The file pointer to attach the log output.
 *
 * Description:
 * This routine is the entry point for the debugfs open file operation. It gets
 * the vport from the i_private field in @inode, allocates the necessary buffer
 * for the log, fills the buffer from the in-memory log for this vport, and then
 * returns a pointer to that log in the private_data field in @file.
 *
 * Returns:
2339
 * This function returns zero if successful. On error it will return a negative
2340 2341
 * error value.
 **/
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
static int
lpfc_debugfs_dumpHostSlim_open(struct inode *inode, struct file *file)
{
	struct lpfc_hba *phba = inode->i_private;
	struct lpfc_debug *debug;
	int rc = -ENOMEM;

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

2353
	/* Round to page boundary */
2354 2355 2356 2357 2358 2359 2360 2361
	debug->buffer = kmalloc(LPFC_DUMPHOSTSLIM_SIZE, GFP_KERNEL);
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

	debug->len = lpfc_debugfs_dumpHostSlim_data(phba, debug->buffer,
		LPFC_DUMPHOSTSLIM_SIZE);
2362 2363 2364 2365 2366 2367 2368
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

2369 2370 2371 2372
static ssize_t
lpfc_debugfs_dif_err_read(struct file *file, char __user *buf,
	size_t nbytes, loff_t *ppos)
{
A
Al Viro 已提交
2373
	struct dentry *dent = file->f_path.dentry;
2374
	struct lpfc_hba *phba = file->private_data;
2375
	char cbuf[32];
2376
	uint64_t tmp = 0;
2377 2378 2379
	int cnt = 0;

	if (dent == phba->debug_writeGuard)
2380
		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wgrd_cnt);
2381
	else if (dent == phba->debug_writeApp)
2382
		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wapp_cnt);
2383
	else if (dent == phba->debug_writeRef)
2384
		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wref_cnt);
2385
	else if (dent == phba->debug_readGuard)
2386
		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rgrd_cnt);
2387
	else if (dent == phba->debug_readApp)
2388
		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rapp_cnt);
2389
	else if (dent == phba->debug_readRef)
2390
		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rref_cnt);
2391
	else if (dent == phba->debug_InjErrNPortID)
2392 2393
		cnt = scnprintf(cbuf, 32, "0x%06x\n",
				phba->lpfc_injerr_nportid);
2394 2395 2396
	else if (dent == phba->debug_InjErrWWPN) {
		memcpy(&tmp, &phba->lpfc_injerr_wwpn, sizeof(struct lpfc_name));
		tmp = cpu_to_be64(tmp);
2397
		cnt = scnprintf(cbuf, 32, "0x%016llx\n", tmp);
2398 2399
	} else if (dent == phba->debug_InjErrLBA) {
		if (phba->lpfc_injerr_lba == (sector_t)(-1))
2400
			cnt = scnprintf(cbuf, 32, "off\n");
2401
		else
2402
			cnt = scnprintf(cbuf, 32, "0x%llx\n",
2403
				 (uint64_t) phba->lpfc_injerr_lba);
2404
	} else
2405 2406 2407 2408 2409 2410 2411 2412 2413 2414
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
			 "0547 Unknown debugfs error injection entry\n");

	return simple_read_from_buffer(buf, nbytes, ppos, &cbuf, cnt);
}

static ssize_t
lpfc_debugfs_dif_err_write(struct file *file, const char __user *buf,
	size_t nbytes, loff_t *ppos)
{
A
Al Viro 已提交
2415
	struct dentry *dent = file->f_path.dentry;
2416
	struct lpfc_hba *phba = file->private_data;
2417
	char dstbuf[33];
2418
	uint64_t tmp = 0;
2419 2420
	int size;

2421
	memset(dstbuf, 0, 33);
2422 2423
	size = (nbytes < 32) ? nbytes : 32;
	if (copy_from_user(dstbuf, buf, size))
2424
		return -EFAULT;
2425

2426
	if (dent == phba->debug_InjErrLBA) {
2427 2428
		if ((dstbuf[0] == 'o') && (dstbuf[1] == 'f') &&
		    (dstbuf[2] == 'f'))
2429
			tmp = (uint64_t)(-1);
2430 2431
	}

2432
	if ((tmp == 0) && (kstrtoull(dstbuf, 0, &tmp)))
2433
		return -EINVAL;
2434 2435 2436 2437 2438 2439 2440

	if (dent == phba->debug_writeGuard)
		phba->lpfc_injerr_wgrd_cnt = (uint32_t)tmp;
	else if (dent == phba->debug_writeApp)
		phba->lpfc_injerr_wapp_cnt = (uint32_t)tmp;
	else if (dent == phba->debug_writeRef)
		phba->lpfc_injerr_wref_cnt = (uint32_t)tmp;
2441 2442
	else if (dent == phba->debug_readGuard)
		phba->lpfc_injerr_rgrd_cnt = (uint32_t)tmp;
2443 2444 2445 2446 2447 2448
	else if (dent == phba->debug_readApp)
		phba->lpfc_injerr_rapp_cnt = (uint32_t)tmp;
	else if (dent == phba->debug_readRef)
		phba->lpfc_injerr_rref_cnt = (uint32_t)tmp;
	else if (dent == phba->debug_InjErrLBA)
		phba->lpfc_injerr_lba = (sector_t)tmp;
2449 2450 2451 2452 2453 2454
	else if (dent == phba->debug_InjErrNPortID)
		phba->lpfc_injerr_nportid = (uint32_t)(tmp & Mask_DID);
	else if (dent == phba->debug_InjErrWWPN) {
		tmp = cpu_to_be64(tmp);
		memcpy(&phba->lpfc_injerr_wwpn, &tmp, sizeof(struct lpfc_name));
	} else
2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
			 "0548 Unknown debugfs error injection entry\n");

	return nbytes;
}

static int
lpfc_debugfs_dif_err_release(struct inode *inode, struct file *file)
{
	return 0;
}

2467
/**
2468
 * lpfc_debugfs_nodelist_open - Open the nodelist debugfs file
2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
 * @inode: The inode pointer that contains a vport pointer.
 * @file: The file pointer to attach the log output.
 *
 * Description:
 * This routine is the entry point for the debugfs open file operation. It gets
 * the vport from the i_private field in @inode, allocates the necessary buffer
 * for the log, fills the buffer from the in-memory log for this vport, and then
 * returns a pointer to that log in the private_data field in @file.
 *
 * Returns:
2479
 * This function returns zero if successful. On error it will return a negative
2480 2481
 * error value.
 **/
J
James Smart 已提交
2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
static int
lpfc_debugfs_nodelist_open(struct inode *inode, struct file *file)
{
	struct lpfc_vport *vport = inode->i_private;
	struct lpfc_debug *debug;
	int rc = -ENOMEM;

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

2493
	/* Round to page boundary */
J
James Smart 已提交
2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508
	debug->buffer = kmalloc(LPFC_NODELIST_SIZE, GFP_KERNEL);
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

	debug->len = lpfc_debugfs_nodelist_data(vport, debug->buffer,
		LPFC_NODELIST_SIZE);
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

2509
/**
2510
 * lpfc_debugfs_lseek - Seek through a debugfs file
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525
 * @file: The file pointer to seek through.
 * @off: The offset to seek to or the amount to seek by.
 * @whence: Indicates how to seek.
 *
 * Description:
 * This routine is the entry point for the debugfs lseek file operation. The
 * @whence parameter indicates whether @off is the offset to directly seek to,
 * or if it is a value to seek forward or reverse by. This function figures out
 * what the new offset of the debugfs file will be and assigns that value to the
 * f_pos field of @file.
 *
 * Returns:
 * This function returns the new offset if successful and returns a negative
 * error if unable to process the seek.
 **/
J
James Smart 已提交
2526 2527 2528
static loff_t
lpfc_debugfs_lseek(struct file *file, loff_t off, int whence)
{
A
Al Viro 已提交
2529 2530
	struct lpfc_debug *debug = file->private_data;
	return fixed_size_llseek(file, off, whence, debug->len);
J
James Smart 已提交
2531 2532
}

2533
/**
2534
 * lpfc_debugfs_read - Read a debugfs file
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the data to.
 * @nbytes: The number of bytes to read.
 * @ppos: The position in the file to start reading from.
 *
 * Description:
 * This routine reads data from from the buffer indicated in the private_data
 * field of @file. It will start reading at @ppos and copy up to @nbytes of
 * data to @buf.
 *
 * Returns:
 * This function returns the amount of data that was read (this could be less
 * than @nbytes if the end of the file was reached) or a negative error value.
 **/
J
James Smart 已提交
2549 2550 2551 2552 2553
static ssize_t
lpfc_debugfs_read(struct file *file, char __user *buf,
		  size_t nbytes, loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
2554

J
James Smart 已提交
2555 2556 2557 2558
	return simple_read_from_buffer(buf, nbytes, ppos, debug->buffer,
				       debug->len);
}

2559
/**
2560
 * lpfc_debugfs_release - Release the buffer used to store debugfs file data
2561 2562 2563 2564 2565 2566 2567 2568 2569 2570
 * @inode: The inode pointer that contains a vport pointer. (unused)
 * @file: The file pointer that contains the buffer to release.
 *
 * Description:
 * This routine frees the buffer that was allocated when the debugfs file was
 * opened.
 *
 * Returns:
 * This function returns zero.
 **/
J
James Smart 已提交
2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
static int
lpfc_debugfs_release(struct inode *inode, struct file *file)
{
	struct lpfc_debug *debug = file->private_data;

	kfree(debug->buffer);
	kfree(debug);

	return 0;
}

2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646
/**
 * lpfc_debugfs_multixripools_write - Clear multi-XRI pools statistics
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the user data from.
 * @nbytes: The number of bytes to get.
 * @ppos: The position in the file to start reading from.
 *
 * Description:
 * This routine clears multi-XRI pools statistics when buf contains "clear".
 *
 * Return Value:
 * It returns the @nbytges passing in from debugfs user space when successful.
 * In case of error conditions, it returns proper error code back to the user
 * space.
 **/
static ssize_t
lpfc_debugfs_multixripools_write(struct file *file, const char __user *buf,
				 size_t nbytes, loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	char mybuf[64];
	char *pbuf;
	u32 i;
	u32 hwq_count;
	struct lpfc_sli4_hdw_queue *qp;
	struct lpfc_multixri_pool *multixri_pool;

	if (nbytes > 64)
		nbytes = 64;

	memset(mybuf, 0, sizeof(mybuf));

	if (copy_from_user(mybuf, buf, nbytes))
		return -EFAULT;
	pbuf = &mybuf[0];

	if ((strncmp(pbuf, "clear", strlen("clear"))) == 0) {
		hwq_count = phba->cfg_hdw_queue;
		for (i = 0; i < hwq_count; i++) {
			qp = &phba->sli4_hba.hdwq[i];
			multixri_pool = qp->p_multixri_pool;
			if (!multixri_pool)
				continue;

			qp->empty_io_bufs = 0;
			multixri_pool->pbl_empty_count = 0;
#ifdef LPFC_MXP_STAT
			multixri_pool->above_limit_count = 0;
			multixri_pool->below_limit_count = 0;
			multixri_pool->stat_max_hwm = 0;
			multixri_pool->local_pbl_hit_count = 0;
			multixri_pool->other_pbl_hit_count = 0;

			multixri_pool->stat_pbl_count = 0;
			multixri_pool->stat_pvt_count = 0;
			multixri_pool->stat_busy_count = 0;
			multixri_pool->stat_snapshot_taken = 0;
#endif
		}
		return strlen(pbuf);
	}

	return -EINVAL;
}
2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676

static int
lpfc_debugfs_nvmestat_open(struct inode *inode, struct file *file)
{
	struct lpfc_vport *vport = inode->i_private;
	struct lpfc_debug *debug;
	int rc = -ENOMEM;

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

	 /* Round to page boundary */
	debug->buffer = kmalloc(LPFC_NVMESTAT_SIZE, GFP_KERNEL);
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

	debug->len = lpfc_debugfs_nvmestat_data(vport, debug->buffer,
		LPFC_NVMESTAT_SIZE);

	debug->i_private = inode->i_private;
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706
static ssize_t
lpfc_debugfs_nvmestat_write(struct file *file, const char __user *buf,
			    size_t nbytes, loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
	struct lpfc_hba   *phba = vport->phba;
	struct lpfc_nvmet_tgtport *tgtp;
	char mybuf[64];
	char *pbuf;

	if (!phba->targetport)
		return -ENXIO;

	if (nbytes > 64)
		nbytes = 64;

	memset(mybuf, 0, sizeof(mybuf));

	if (copy_from_user(mybuf, buf, nbytes))
		return -EFAULT;
	pbuf = &mybuf[0];

	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
	if ((strncmp(pbuf, "reset", strlen("reset")) == 0) ||
	    (strncmp(pbuf, "zero", strlen("zero")) == 0)) {
		atomic_set(&tgtp->rcv_ls_req_in, 0);
		atomic_set(&tgtp->rcv_ls_req_out, 0);
		atomic_set(&tgtp->rcv_ls_req_drop, 0);
		atomic_set(&tgtp->xmt_ls_abort, 0);
2707
		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
		atomic_set(&tgtp->xmt_ls_rsp, 0);
		atomic_set(&tgtp->xmt_ls_drop, 0);
		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);

		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
		atomic_set(&tgtp->xmt_fcp_drop, 0);
		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
		atomic_set(&tgtp->xmt_fcp_read, 0);
		atomic_set(&tgtp->xmt_fcp_write, 0);
		atomic_set(&tgtp->xmt_fcp_rsp, 0);
2721
		atomic_set(&tgtp->xmt_fcp_release, 0);
2722 2723 2724 2725
		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);

2726 2727 2728 2729
		atomic_set(&tgtp->xmt_fcp_abort, 0);
		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
		atomic_set(&tgtp->xmt_abort_sol, 0);
		atomic_set(&tgtp->xmt_abort_unsol, 0);
2730 2731 2732 2733 2734 2735
		atomic_set(&tgtp->xmt_abort_rsp, 0);
		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
	}
	return nbytes;
}

2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789
static int
lpfc_debugfs_scsistat_open(struct inode *inode, struct file *file)
{
	struct lpfc_vport *vport = inode->i_private;
	struct lpfc_debug *debug;
	int rc = -ENOMEM;

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

	 /* Round to page boundary */
	debug->buffer = kzalloc(LPFC_SCSISTAT_SIZE, GFP_KERNEL);
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

	debug->len = lpfc_debugfs_scsistat_data(vport, debug->buffer,
		LPFC_SCSISTAT_SIZE);

	debug->i_private = inode->i_private;
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

static ssize_t
lpfc_debugfs_scsistat_write(struct file *file, const char __user *buf,
			    size_t nbytes, loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
	struct lpfc_hba *phba = vport->phba;
	char mybuf[6] = {0};
	int i;

	if (copy_from_user(mybuf, buf, (nbytes >= sizeof(mybuf)) ?
				       (sizeof(mybuf) - 1) : nbytes))
		return -EFAULT;

	if ((strncmp(&mybuf[0], "reset", strlen("reset")) == 0) ||
	    (strncmp(&mybuf[0], "zero", strlen("zero")) == 0)) {
		for (i = 0; i < phba->cfg_hdw_queue; i++) {
			memset(&phba->sli4_hba.hdwq[i].scsi_cstat, 0,
			       sizeof(phba->sli4_hba.hdwq[i].scsi_cstat));
		}
	}

	return nbytes;
}

2790
static int
2791
lpfc_debugfs_ioktime_open(struct inode *inode, struct file *file)
2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
{
	struct lpfc_vport *vport = inode->i_private;
	struct lpfc_debug *debug;
	int rc = -ENOMEM;

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

	 /* Round to page boundary */
2802
	debug->buffer = kmalloc(LPFC_IOKTIME_SIZE, GFP_KERNEL);
2803 2804 2805 2806 2807
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

2808 2809
	debug->len = lpfc_debugfs_ioktime_data(vport, debug->buffer,
		LPFC_IOKTIME_SIZE);
2810 2811 2812 2813 2814 2815 2816 2817 2818 2819

	debug->i_private = inode->i_private;
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

static ssize_t
2820 2821
lpfc_debugfs_ioktime_write(struct file *file, const char __user *buf,
			   size_t nbytes, loff_t *ppos)
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
	struct lpfc_hba   *phba = vport->phba;
	char mybuf[64];
	char *pbuf;

	if (nbytes > 64)
		nbytes = 64;

	memset(mybuf, 0, sizeof(mybuf));

	if (copy_from_user(mybuf, buf, nbytes))
		return -EFAULT;
	pbuf = &mybuf[0];

	if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
		phba->ktime_data_samples = 0;
		phba->ktime_status_samples = 0;
		phba->ktime_seg1_total = 0;
		phba->ktime_seg1_max = 0;
		phba->ktime_seg1_min = 0xffffffff;
		phba->ktime_seg2_total = 0;
		phba->ktime_seg2_max = 0;
		phba->ktime_seg2_min = 0xffffffff;
		phba->ktime_seg3_total = 0;
		phba->ktime_seg3_max = 0;
		phba->ktime_seg3_min = 0xffffffff;
		phba->ktime_seg4_total = 0;
		phba->ktime_seg4_max = 0;
		phba->ktime_seg4_min = 0xffffffff;
		phba->ktime_seg5_total = 0;
		phba->ktime_seg5_max = 0;
		phba->ktime_seg5_min = 0xffffffff;
		phba->ktime_seg6_total = 0;
		phba->ktime_seg6_max = 0;
		phba->ktime_seg6_min = 0xffffffff;
		phba->ktime_seg7_total = 0;
		phba->ktime_seg7_max = 0;
		phba->ktime_seg7_min = 0xffffffff;
		phba->ktime_seg8_total = 0;
		phba->ktime_seg8_max = 0;
		phba->ktime_seg8_min = 0xffffffff;
		phba->ktime_seg9_total = 0;
		phba->ktime_seg9_max = 0;
		phba->ktime_seg9_min = 0xffffffff;
		phba->ktime_seg10_total = 0;
		phba->ktime_seg10_max = 0;
		phba->ktime_seg10_min = 0xffffffff;

		phba->ktime_on = 1;
		return strlen(pbuf);
	} else if ((strncmp(pbuf, "off",
		   sizeof("off") - 1) == 0)) {
		phba->ktime_on = 0;
		return strlen(pbuf);
	} else if ((strncmp(pbuf, "zero",
		   sizeof("zero") - 1) == 0)) {
		phba->ktime_data_samples = 0;
		phba->ktime_status_samples = 0;
		phba->ktime_seg1_total = 0;
		phba->ktime_seg1_max = 0;
		phba->ktime_seg1_min = 0xffffffff;
		phba->ktime_seg2_total = 0;
		phba->ktime_seg2_max = 0;
		phba->ktime_seg2_min = 0xffffffff;
		phba->ktime_seg3_total = 0;
		phba->ktime_seg3_max = 0;
		phba->ktime_seg3_min = 0xffffffff;
		phba->ktime_seg4_total = 0;
		phba->ktime_seg4_max = 0;
		phba->ktime_seg4_min = 0xffffffff;
		phba->ktime_seg5_total = 0;
		phba->ktime_seg5_max = 0;
		phba->ktime_seg5_min = 0xffffffff;
		phba->ktime_seg6_total = 0;
		phba->ktime_seg6_max = 0;
		phba->ktime_seg6_min = 0xffffffff;
		phba->ktime_seg7_total = 0;
		phba->ktime_seg7_max = 0;
		phba->ktime_seg7_min = 0xffffffff;
		phba->ktime_seg8_total = 0;
		phba->ktime_seg8_max = 0;
		phba->ktime_seg8_min = 0xffffffff;
		phba->ktime_seg9_total = 0;
		phba->ktime_seg9_max = 0;
		phba->ktime_seg9_min = 0xffffffff;
		phba->ktime_seg10_total = 0;
		phba->ktime_seg10_max = 0;
		phba->ktime_seg10_min = 0xffffffff;
		return strlen(pbuf);
	}
	return -EINVAL;
}

static int
lpfc_debugfs_nvmeio_trc_open(struct inode *inode, struct file *file)
{
	struct lpfc_hba *phba = inode->i_private;
	struct lpfc_debug *debug;
	int rc = -ENOMEM;

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

	 /* Round to page boundary */
	debug->buffer = kmalloc(LPFC_NVMEIO_TRC_SIZE, GFP_KERNEL);
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

	debug->len = lpfc_debugfs_nvmeio_trc_data(phba, debug->buffer,
		LPFC_NVMEIO_TRC_SIZE);

	debug->i_private = inode->i_private;
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

static ssize_t
lpfc_debugfs_nvmeio_trc_write(struct file *file, const char __user *buf,
			      size_t nbytes, loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	int i;
	unsigned long sz;
	char mybuf[64];
	char *pbuf;

	if (nbytes > 64)
		nbytes = 64;

	memset(mybuf, 0, sizeof(mybuf));

	if (copy_from_user(mybuf, buf, nbytes))
		return -EFAULT;
	pbuf = &mybuf[0];

	if ((strncmp(pbuf, "off", sizeof("off") - 1) == 0)) {
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
				"0570 nvmeio_trc_off\n");
		phba->nvmeio_trc_output_idx = 0;
		phba->nvmeio_trc_on = 0;
		return strlen(pbuf);
	} else if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
				"0571 nvmeio_trc_on\n");
		phba->nvmeio_trc_output_idx = 0;
		phba->nvmeio_trc_on = 1;
		return strlen(pbuf);
	}

	/* We must be off to allocate the trace buffer */
	if (phba->nvmeio_trc_on != 0)
		return -EINVAL;

	/* If not on or off, the parameter is the trace buffer size */
	i = kstrtoul(pbuf, 0, &sz);
	if (i)
		return -EINVAL;
	phba->nvmeio_trc_size = (uint32_t)sz;

	/* It must be a power of 2 - round down */
	i = 0;
	while (sz > 1) {
		sz = sz >> 1;
		i++;
	}
	sz = (1 << i);
	if (phba->nvmeio_trc_size != sz)
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
				"0572 nvmeio_trc_size changed to %ld\n",
				sz);
	phba->nvmeio_trc_size = (uint32_t)sz;

	/* If one previously exists, free it */
	kfree(phba->nvmeio_trc);

	/* Allocate new trace buffer and initialize */
3007
	phba->nvmeio_trc = kzalloc((sizeof(struct lpfc_debugfs_nvmeio_trc) *
3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022
				    sz), GFP_KERNEL);
	if (!phba->nvmeio_trc) {
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
				"0573 Cannot create debugfs "
				"nvmeio_trc buffer\n");
		return -ENOMEM;
	}
	atomic_set(&phba->nvmeio_trc_cnt, 0);
	phba->nvmeio_trc_on = 0;
	phba->nvmeio_trc_output_idx = 0;

	return strlen(pbuf);
}

static int
3023
lpfc_debugfs_hdwqstat_open(struct inode *inode, struct file *file)
3024 3025 3026 3027 3028 3029 3030 3031 3032 3033
{
	struct lpfc_vport *vport = inode->i_private;
	struct lpfc_debug *debug;
	int rc = -ENOMEM;

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

	 /* Round to page boundary */
3034
	debug->buffer = kcalloc(1, LPFC_SCSISTAT_SIZE, GFP_KERNEL);
3035 3036 3037 3038 3039
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

3040 3041
	debug->len = lpfc_debugfs_hdwqstat_data(vport, debug->buffer,
						LPFC_SCSISTAT_SIZE);
3042 3043 3044 3045 3046 3047 3048 3049 3050 3051

	debug->i_private = inode->i_private;
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

static ssize_t
3052
lpfc_debugfs_hdwqstat_write(struct file *file, const char __user *buf,
3053 3054 3055 3056 3057
			    size_t nbytes, loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
	struct lpfc_hba   *phba = vport->phba;
3058
	struct lpfc_hdwq_stat *c_stat;
3059 3060
	char mybuf[64];
	char *pbuf;
3061
	int i;
3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072

	if (nbytes > 64)
		nbytes = 64;

	memset(mybuf, 0, sizeof(mybuf));

	if (copy_from_user(mybuf, buf, nbytes))
		return -EFAULT;
	pbuf = &mybuf[0];

	if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
3073
		if (phba->nvmet_support)
3074
			phba->hdwqstat_on |= LPFC_CHECK_NVMET_IO;
3075
		else
3076
			phba->hdwqstat_on |= (LPFC_CHECK_NVME_IO |
3077 3078 3079
				LPFC_CHECK_SCSI_IO);
		return strlen(pbuf);
	} else if ((strncmp(pbuf, "nvme_on", sizeof("nvme_on") - 1) == 0)) {
3080
		if (phba->nvmet_support)
3081
			phba->hdwqstat_on |= LPFC_CHECK_NVMET_IO;
3082
		else
3083
			phba->hdwqstat_on |= LPFC_CHECK_NVME_IO;
3084
		return strlen(pbuf);
3085
	} else if ((strncmp(pbuf, "scsi_on", sizeof("scsi_on") - 1) == 0)) {
3086 3087
		if (!phba->nvmet_support)
			phba->hdwqstat_on |= LPFC_CHECK_SCSI_IO;
3088
		return strlen(pbuf);
3089 3090 3091 3092 3093 3094
	} else if ((strncmp(pbuf, "nvme_off", sizeof("nvme_off") - 1) == 0)) {
		phba->hdwqstat_on &= ~(LPFC_CHECK_NVME_IO |
				       LPFC_CHECK_NVMET_IO);
		return strlen(pbuf);
	} else if ((strncmp(pbuf, "scsi_off", sizeof("scsi_off") - 1) == 0)) {
		phba->hdwqstat_on &= ~LPFC_CHECK_SCSI_IO;
3095
		return strlen(pbuf);
3096 3097
	} else if ((strncmp(pbuf, "off",
		   sizeof("off") - 1) == 0)) {
3098
		phba->hdwqstat_on = LPFC_CHECK_OFF;
3099 3100 3101
		return strlen(pbuf);
	} else if ((strncmp(pbuf, "zero",
		   sizeof("zero") - 1) == 0)) {
3102 3103 3104 3105 3106
		for_each_present_cpu(i) {
			c_stat = per_cpu_ptr(phba->sli4_hba.c_stat, i);
			c_stat->xmt_io = 0;
			c_stat->cmpl_io = 0;
			c_stat->rcv_io = 0;
3107 3108 3109 3110 3111 3112
		}
		return strlen(pbuf);
	}
	return -EINVAL;
}

3113
/*
3114
 * ---------------------------------
3115
 * iDiag debugfs file access methods
3116
 * ---------------------------------
3117
 *
3118 3119
 * All access methods are through the proper SLI4 PCI function's debugfs
 * iDiag directory:
3120
 *
3121
 *     /sys/kernel/debug/lpfc/fn<#>/iDiag
3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141
 */

/**
 * lpfc_idiag_cmd_get - Get and parse idiag debugfs comands from user space
 * @buf: The pointer to the user space buffer.
 * @nbytes: The number of bytes in the user space buffer.
 * @idiag_cmd: pointer to the idiag command struct.
 *
 * This routine reads data from debugfs user space buffer and parses the
 * buffer for getting the idiag command and arguments. The while space in
 * between the set of data is used as the parsing separator.
 *
 * This routine returns 0 when successful, it returns proper error code
 * back to the user space in error conditions.
 */
static int lpfc_idiag_cmd_get(const char __user *buf, size_t nbytes,
			      struct lpfc_idiag_cmd *idiag_cmd)
{
	char mybuf[64];
	char *pbuf, *step_str;
3142 3143
	int i;
	size_t bsize;
3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164

	memset(mybuf, 0, sizeof(mybuf));
	memset(idiag_cmd, 0, sizeof(*idiag_cmd));
	bsize = min(nbytes, (sizeof(mybuf)-1));

	if (copy_from_user(mybuf, buf, bsize))
		return -EFAULT;
	pbuf = &mybuf[0];
	step_str = strsep(&pbuf, "\t ");

	/* The opcode must present */
	if (!step_str)
		return -EINVAL;

	idiag_cmd->opcode = simple_strtol(step_str, NULL, 0);
	if (idiag_cmd->opcode == 0)
		return -EINVAL;

	for (i = 0; i < LPFC_IDIAG_CMD_DATA_SIZE; i++) {
		step_str = strsep(&pbuf, "\t ");
		if (!step_str)
3165
			return i;
3166 3167
		idiag_cmd->data[i] = simple_strtol(step_str, NULL, 0);
	}
3168
	return i;
3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236
}

/**
 * lpfc_idiag_open - idiag open debugfs
 * @inode: The inode pointer that contains a pointer to phba.
 * @file: The file pointer to attach the file operation.
 *
 * Description:
 * This routine is the entry point for the debugfs open file operation. It
 * gets the reference to phba from the i_private field in @inode, it then
 * allocates buffer for the file operation, performs the necessary PCI config
 * space read into the allocated buffer according to the idiag user command
 * setup, and then returns a pointer to buffer in the private_data field in
 * @file.
 *
 * Returns:
 * This function returns zero if successful. On error it will return an
 * negative error value.
 **/
static int
lpfc_idiag_open(struct inode *inode, struct file *file)
{
	struct lpfc_debug *debug;

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		return -ENOMEM;

	debug->i_private = inode->i_private;
	debug->buffer = NULL;
	file->private_data = debug;

	return 0;
}

/**
 * lpfc_idiag_release - Release idiag access file operation
 * @inode: The inode pointer that contains a vport pointer. (unused)
 * @file: The file pointer that contains the buffer to release.
 *
 * Description:
 * This routine is the generic release routine for the idiag access file
 * operation, it frees the buffer that was allocated when the debugfs file
 * was opened.
 *
 * Returns:
 * This function returns zero.
 **/
static int
lpfc_idiag_release(struct inode *inode, struct file *file)
{
	struct lpfc_debug *debug = file->private_data;

	/* Free the buffers to the file operation */
	kfree(debug->buffer);
	kfree(debug);

	return 0;
}

/**
 * lpfc_idiag_cmd_release - Release idiag cmd access file operation
 * @inode: The inode pointer that contains a vport pointer. (unused)
 * @file: The file pointer that contains the buffer to release.
 *
 * Description:
 * This routine frees the buffer that was allocated when the debugfs file
 * was opened. It also reset the fields in the idiag command struct in the
3237
 * case of command for write operation.
3238 3239 3240 3241 3242 3243 3244 3245 3246
 *
 * Returns:
 * This function returns zero.
 **/
static int
lpfc_idiag_cmd_release(struct inode *inode, struct file *file)
{
	struct lpfc_debug *debug = file->private_data;

3247 3248 3249 3250 3251 3252 3253 3254
	if (debug->op == LPFC_IDIAG_OP_WR) {
		switch (idiag.cmd.opcode) {
		case LPFC_IDIAG_CMD_PCICFG_WR:
		case LPFC_IDIAG_CMD_PCICFG_ST:
		case LPFC_IDIAG_CMD_PCICFG_CL:
		case LPFC_IDIAG_CMD_QUEACC_WR:
		case LPFC_IDIAG_CMD_QUEACC_ST:
		case LPFC_IDIAG_CMD_QUEACC_CL:
3255
			memset(&idiag, 0, sizeof(idiag));
3256 3257 3258 3259 3260
			break;
		default:
			break;
		}
	}
3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317

	/* Free the buffers to the file operation */
	kfree(debug->buffer);
	kfree(debug);

	return 0;
}

/**
 * lpfc_idiag_pcicfg_read - idiag debugfs read pcicfg
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the data to.
 * @nbytes: The number of bytes to read.
 * @ppos: The position in the file to start reading from.
 *
 * Description:
 * This routine reads data from the @phba pci config space according to the
 * idiag command, and copies to user @buf. Depending on the PCI config space
 * read command setup, it does either a single register read of a byte
 * (8 bits), a word (16 bits), or a dword (32 bits) or browsing through all
 * registers from the 4K extended PCI config space.
 *
 * Returns:
 * This function returns the amount of data that was read (this could be less
 * than @nbytes if the end of the file was reached) or a negative error value.
 **/
static ssize_t
lpfc_idiag_pcicfg_read(struct file *file, char __user *buf, size_t nbytes,
		       loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	int offset_label, offset, len = 0, index = LPFC_PCI_CFG_RD_SIZE;
	int where, count;
	char *pbuffer;
	struct pci_dev *pdev;
	uint32_t u32val;
	uint16_t u16val;
	uint8_t u8val;

	pdev = phba->pcidev;
	if (!pdev)
		return 0;

	/* This is a user read operation */
	debug->op = LPFC_IDIAG_OP_RD;

	if (!debug->buffer)
		debug->buffer = kmalloc(LPFC_PCI_CFG_SIZE, GFP_KERNEL);
	if (!debug->buffer)
		return 0;
	pbuffer = debug->buffer;

	if (*ppos)
		return 0;

	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
3318 3319
		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
3320 3321 3322 3323 3324 3325 3326
	} else
		return 0;

	/* Read single PCI config space register */
	switch (count) {
	case SIZE_U8: /* byte (8 bits) */
		pci_read_config_byte(pdev, where, &u8val);
3327
		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3328 3329 3330 3331
				"%03x: %02x\n", where, u8val);
		break;
	case SIZE_U16: /* word (16 bits) */
		pci_read_config_word(pdev, where, &u16val);
3332
		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3333 3334 3335 3336
				"%03x: %04x\n", where, u16val);
		break;
	case SIZE_U32: /* double word (32 bits) */
		pci_read_config_dword(pdev, where, &u32val);
3337
		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3338 3339
				"%03x: %08x\n", where, u32val);
		break;
3340
	case LPFC_PCI_CFG_BROWSE: /* browse all */
3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355
		goto pcicfg_browse;
	default:
		/* illegal count */
		len = 0;
		break;
	}
	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);

pcicfg_browse:

	/* Browse all PCI config space registers */
	offset_label = idiag.offset.last_rd;
	offset = offset_label;

	/* Read PCI config space */
3356
	len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3357 3358 3359
			"%03x: ", offset_label);
	while (index > 0) {
		pci_read_config_dword(pdev, offset, &u32val);
3360
		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3361 3362
				"%08x ", u32val);
		offset += sizeof(uint32_t);
3363
		if (offset >= LPFC_PCI_CFG_SIZE) {
3364
			len += scnprintf(pbuffer+len,
3365 3366 3367
					LPFC_PCI_CFG_SIZE-len, "\n");
			break;
		}
3368 3369
		index -= sizeof(uint32_t);
		if (!index)
3370
			len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3371 3372 3373
					"\n");
		else if (!(index % (8 * sizeof(uint32_t)))) {
			offset_label += (8 * sizeof(uint32_t));
3374
			len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3375 3376 3377 3378 3379
					"\n%03x: ", offset_label);
		}
	}

	/* Set up the offset for next portion of pci cfg read */
3380 3381 3382 3383 3384
	if (index == 0) {
		idiag.offset.last_rd += LPFC_PCI_CFG_RD_SIZE;
		if (idiag.offset.last_rd >= LPFC_PCI_CFG_SIZE)
			idiag.offset.last_rd = 0;
	} else
3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428
		idiag.offset.last_rd = 0;

	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
}

/**
 * lpfc_idiag_pcicfg_write - Syntax check and set up idiag pcicfg commands
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the user data from.
 * @nbytes: The number of bytes to get.
 * @ppos: The position in the file to start reading from.
 *
 * This routine get the debugfs idiag command struct from user space and
 * then perform the syntax check for PCI config space read or write command
 * accordingly. In the case of PCI config space read command, it sets up
 * the command in the idiag command struct for the debugfs read operation.
 * In the case of PCI config space write operation, it executes the write
 * operation into the PCI config space accordingly.
 *
 * It returns the @nbytges passing in from debugfs user space when successful.
 * In case of error conditions, it returns proper error code back to the user
 * space.
 */
static ssize_t
lpfc_idiag_pcicfg_write(struct file *file, const char __user *buf,
			size_t nbytes, loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	uint32_t where, value, count;
	uint32_t u32val;
	uint16_t u16val;
	uint8_t u8val;
	struct pci_dev *pdev;
	int rc;

	pdev = phba->pcidev;
	if (!pdev)
		return -EFAULT;

	/* This is a user write operation */
	debug->op = LPFC_IDIAG_OP_WR;

	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3429
	if (rc < 0)
3430 3431 3432
		return rc;

	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
3433 3434 3435
		/* Sanity check on PCI config read command line arguments */
		if (rc != LPFC_PCI_CFG_RD_CMD_ARG)
			goto error_out;
3436
		/* Read command from PCI config space, set up command fields */
3437 3438
		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
3439 3440
		if (count == LPFC_PCI_CFG_BROWSE) {
			if (where % sizeof(uint32_t))
3441
				goto error_out;
3442 3443
			/* Starting offset to browse */
			idiag.offset.last_rd = where;
3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468
		} else if ((count != sizeof(uint8_t)) &&
			   (count != sizeof(uint16_t)) &&
			   (count != sizeof(uint32_t)))
			goto error_out;
		if (count == sizeof(uint8_t)) {
			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
				goto error_out;
			if (where % sizeof(uint8_t))
				goto error_out;
		}
		if (count == sizeof(uint16_t)) {
			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
				goto error_out;
			if (where % sizeof(uint16_t))
				goto error_out;
		}
		if (count == sizeof(uint32_t)) {
			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
				goto error_out;
			if (where % sizeof(uint32_t))
				goto error_out;
		}
	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR ||
		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST ||
		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3469 3470 3471
		/* Sanity check on PCI config write command line arguments */
		if (rc != LPFC_PCI_CFG_WR_CMD_ARG)
			goto error_out;
3472
		/* Write command to PCI config space, read-modify-write */
3473 3474 3475
		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
		value = idiag.cmd.data[IDIAG_PCICFG_VALUE_INDX];
3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566
		/* Sanity checks */
		if ((count != sizeof(uint8_t)) &&
		    (count != sizeof(uint16_t)) &&
		    (count != sizeof(uint32_t)))
			goto error_out;
		if (count == sizeof(uint8_t)) {
			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
				goto error_out;
			if (where % sizeof(uint8_t))
				goto error_out;
			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
				pci_write_config_byte(pdev, where,
						      (uint8_t)value);
			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
				rc = pci_read_config_byte(pdev, where, &u8val);
				if (!rc) {
					u8val |= (uint8_t)value;
					pci_write_config_byte(pdev, where,
							      u8val);
				}
			}
			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
				rc = pci_read_config_byte(pdev, where, &u8val);
				if (!rc) {
					u8val &= (uint8_t)(~value);
					pci_write_config_byte(pdev, where,
							      u8val);
				}
			}
		}
		if (count == sizeof(uint16_t)) {
			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
				goto error_out;
			if (where % sizeof(uint16_t))
				goto error_out;
			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
				pci_write_config_word(pdev, where,
						      (uint16_t)value);
			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
				rc = pci_read_config_word(pdev, where, &u16val);
				if (!rc) {
					u16val |= (uint16_t)value;
					pci_write_config_word(pdev, where,
							      u16val);
				}
			}
			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
				rc = pci_read_config_word(pdev, where, &u16val);
				if (!rc) {
					u16val &= (uint16_t)(~value);
					pci_write_config_word(pdev, where,
							      u16val);
				}
			}
		}
		if (count == sizeof(uint32_t)) {
			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
				goto error_out;
			if (where % sizeof(uint32_t))
				goto error_out;
			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
				pci_write_config_dword(pdev, where, value);
			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
				rc = pci_read_config_dword(pdev, where,
							   &u32val);
				if (!rc) {
					u32val |= value;
					pci_write_config_dword(pdev, where,
							       u32val);
				}
			}
			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
				rc = pci_read_config_dword(pdev, where,
							   &u32val);
				if (!rc) {
					u32val &= ~value;
					pci_write_config_dword(pdev, where,
							       u32val);
				}
			}
		}
	} else
		/* All other opecodes are illegal for now */
		goto error_out;

	return nbytes;
error_out:
	memset(&idiag, 0, sizeof(idiag));
	return -EINVAL;
}

3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644
/**
 * lpfc_idiag_baracc_read - idiag debugfs pci bar access read
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the data to.
 * @nbytes: The number of bytes to read.
 * @ppos: The position in the file to start reading from.
 *
 * Description:
 * This routine reads data from the @phba pci bar memory mapped space
 * according to the idiag command, and copies to user @buf.
 *
 * Returns:
 * This function returns the amount of data that was read (this could be less
 * than @nbytes if the end of the file was reached) or a negative error value.
 **/
static ssize_t
lpfc_idiag_baracc_read(struct file *file, char __user *buf, size_t nbytes,
		       loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	int offset_label, offset, offset_run, len = 0, index;
	int bar_num, acc_range, bar_size;
	char *pbuffer;
	void __iomem *mem_mapped_bar;
	uint32_t if_type;
	struct pci_dev *pdev;
	uint32_t u32val;

	pdev = phba->pcidev;
	if (!pdev)
		return 0;

	/* This is a user read operation */
	debug->op = LPFC_IDIAG_OP_RD;

	if (!debug->buffer)
		debug->buffer = kmalloc(LPFC_PCI_BAR_RD_BUF_SIZE, GFP_KERNEL);
	if (!debug->buffer)
		return 0;
	pbuffer = debug->buffer;

	if (*ppos)
		return 0;

	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
		bar_num   = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
		offset    = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
		bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
	} else
		return 0;

	if (acc_range == 0)
		return 0;

	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
		if (bar_num == IDIAG_BARACC_BAR_0)
			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
		else if (bar_num == IDIAG_BARACC_BAR_1)
			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
		else if (bar_num == IDIAG_BARACC_BAR_2)
			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
		else
			return 0;
	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
		if (bar_num == IDIAG_BARACC_BAR_0)
			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
		else
			return 0;
	} else
		return 0;

	/* Read single PCI bar space register */
	if (acc_range == SINGLE_WORD) {
		offset_run = offset;
		u32val = readl(mem_mapped_bar + offset_run);
3645
		len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658
				"%05x: %08x\n", offset_run, u32val);
	} else
		goto baracc_browse;

	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);

baracc_browse:

	/* Browse all PCI bar space registers */
	offset_label = idiag.offset.last_rd;
	offset_run = offset_label;

	/* Read PCI bar memory mapped space */
3659
	len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3660 3661 3662 3663
			"%05x: ", offset_label);
	index = LPFC_PCI_BAR_RD_SIZE;
	while (index > 0) {
		u32val = readl(mem_mapped_bar + offset_run);
3664
		len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3665 3666 3667 3668
				"%08x ", u32val);
		offset_run += sizeof(uint32_t);
		if (acc_range == LPFC_PCI_BAR_BROWSE) {
			if (offset_run >= bar_size) {
3669
				len += scnprintf(pbuffer+len,
3670 3671 3672 3673 3674 3675
					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
				break;
			}
		} else {
			if (offset_run >= offset +
			    (acc_range * sizeof(uint32_t))) {
3676
				len += scnprintf(pbuffer+len,
3677 3678 3679 3680 3681 3682
					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
				break;
			}
		}
		index -= sizeof(uint32_t);
		if (!index)
3683
			len += scnprintf(pbuffer+len,
3684 3685 3686
					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
		else if (!(index % (8 * sizeof(uint32_t)))) {
			offset_label += (8 * sizeof(uint32_t));
3687
			len += scnprintf(pbuffer+len,
3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852
					LPFC_PCI_BAR_RD_BUF_SIZE-len,
					"\n%05x: ", offset_label);
		}
	}

	/* Set up the offset for next portion of pci bar read */
	if (index == 0) {
		idiag.offset.last_rd += LPFC_PCI_BAR_RD_SIZE;
		if (acc_range == LPFC_PCI_BAR_BROWSE) {
			if (idiag.offset.last_rd >= bar_size)
				idiag.offset.last_rd = 0;
		} else {
			if (offset_run >= offset +
			    (acc_range * sizeof(uint32_t)))
				idiag.offset.last_rd = offset;
		}
	} else {
		if (acc_range == LPFC_PCI_BAR_BROWSE)
			idiag.offset.last_rd = 0;
		else
			idiag.offset.last_rd = offset;
	}

	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
}

/**
 * lpfc_idiag_baracc_write - Syntax check and set up idiag bar access commands
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the user data from.
 * @nbytes: The number of bytes to get.
 * @ppos: The position in the file to start reading from.
 *
 * This routine get the debugfs idiag command struct from user space and
 * then perform the syntax check for PCI bar memory mapped space read or
 * write command accordingly. In the case of PCI bar memory mapped space
 * read command, it sets up the command in the idiag command struct for
 * the debugfs read operation. In the case of PCI bar memorpy mapped space
 * write operation, it executes the write operation into the PCI bar memory
 * mapped space accordingly.
 *
 * It returns the @nbytges passing in from debugfs user space when successful.
 * In case of error conditions, it returns proper error code back to the user
 * space.
 */
static ssize_t
lpfc_idiag_baracc_write(struct file *file, const char __user *buf,
			size_t nbytes, loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	uint32_t bar_num, bar_size, offset, value, acc_range;
	struct pci_dev *pdev;
	void __iomem *mem_mapped_bar;
	uint32_t if_type;
	uint32_t u32val;
	int rc;

	pdev = phba->pcidev;
	if (!pdev)
		return -EFAULT;

	/* This is a user write operation */
	debug->op = LPFC_IDIAG_OP_WR;

	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
	if (rc < 0)
		return rc;

	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
	bar_num = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];

	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
		if ((bar_num != IDIAG_BARACC_BAR_0) &&
		    (bar_num != IDIAG_BARACC_BAR_1) &&
		    (bar_num != IDIAG_BARACC_BAR_2))
			goto error_out;
	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
		if (bar_num != IDIAG_BARACC_BAR_0)
			goto error_out;
	} else
		goto error_out;

	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
		if (bar_num == IDIAG_BARACC_BAR_0) {
			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
				LPFC_PCI_IF0_BAR0_SIZE;
			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
		} else if (bar_num == IDIAG_BARACC_BAR_1) {
			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
				LPFC_PCI_IF0_BAR1_SIZE;
			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
		} else if (bar_num == IDIAG_BARACC_BAR_2) {
			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
				LPFC_PCI_IF0_BAR2_SIZE;
			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
		} else
			goto error_out;
	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
		if (bar_num == IDIAG_BARACC_BAR_0) {
			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
				LPFC_PCI_IF2_BAR0_SIZE;
			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
		} else
			goto error_out;
	} else
		goto error_out;

	offset = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
	if (offset % sizeof(uint32_t))
		goto error_out;

	bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
		/* Sanity check on PCI config read command line arguments */
		if (rc != LPFC_PCI_BAR_RD_CMD_ARG)
			goto error_out;
		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
		if (acc_range == LPFC_PCI_BAR_BROWSE) {
			if (offset > bar_size - sizeof(uint32_t))
				goto error_out;
			/* Starting offset to browse */
			idiag.offset.last_rd = offset;
		} else if (acc_range > SINGLE_WORD) {
			if (offset + acc_range * sizeof(uint32_t) > bar_size)
				goto error_out;
			/* Starting offset to browse */
			idiag.offset.last_rd = offset;
		} else if (acc_range != SINGLE_WORD)
			goto error_out;
	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR ||
		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST ||
		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
		/* Sanity check on PCI bar write command line arguments */
		if (rc != LPFC_PCI_BAR_WR_CMD_ARG)
			goto error_out;
		/* Write command to PCI bar space, read-modify-write */
		acc_range = SINGLE_WORD;
		value = idiag.cmd.data[IDIAG_BARACC_REG_VAL_INDX];
		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR) {
			writel(value, mem_mapped_bar + offset);
			readl(mem_mapped_bar + offset);
		}
		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST) {
			u32val = readl(mem_mapped_bar + offset);
			u32val |= value;
			writel(u32val, mem_mapped_bar + offset);
			readl(mem_mapped_bar + offset);
		}
		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
			u32val = readl(mem_mapped_bar + offset);
			u32val &= ~value;
			writel(u32val, mem_mapped_bar + offset);
			readl(mem_mapped_bar + offset);
		}
	} else
		/* All other opecodes are illegal for now */
		goto error_out;

	return nbytes;
error_out:
	memset(&idiag, 0, sizeof(idiag));
	return -EINVAL;
}

3853 3854 3855 3856 3857 3858 3859
static int
__lpfc_idiag_print_wq(struct lpfc_queue *qp, char *wqtype,
			char *pbuffer, int len)
{
	if (!qp)
		return len;

3860
	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3861
			"\t\t%s WQ info: ", wqtype);
3862
	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3863 3864 3865
			"AssocCQID[%04d]: WQ-STAT[oflow:x%x posted:x%llx]\n",
			qp->assoc_qid, qp->q_cnt_1,
			(unsigned long long)qp->q_cnt_4);
3866
	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3867
			"\t\tWQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3868
			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]",
3869 3870
			qp->queue_id, qp->entry_count,
			qp->entry_size, qp->host_index,
3871
			qp->hba_index, qp->notify_interval);
3872
	len +=  scnprintf(pbuffer + len,
3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883
			LPFC_QUE_INFO_GET_BUF_SIZE - len, "\n");
	return len;
}

static int
lpfc_idiag_wqs_for_cq(struct lpfc_hba *phba, char *wqtype, char *pbuffer,
		int *len, int max_cnt, int cq_id)
{
	struct lpfc_queue *qp;
	int qidx;

3884
	for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
3885
		qp = phba->sli4_hba.hdwq[qidx].io_wq;
3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901
		if (qp->assoc_qid != cq_id)
			continue;
		*len = __lpfc_idiag_print_wq(qp, wqtype, pbuffer, *len);
		if (*len >= max_cnt)
			return 1;
	}
	return 0;
}

static int
__lpfc_idiag_print_cq(struct lpfc_queue *qp, char *cqtype,
			char *pbuffer, int len)
{
	if (!qp)
		return len;

3902
	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3903
			"\t%s CQ info: ", cqtype);
3904
	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3905 3906 3907 3908
			"AssocEQID[%02d]: CQ STAT[max:x%x relw:x%x "
			"xabt:x%x wq:x%llx]\n",
			qp->assoc_qid, qp->q_cnt_1, qp->q_cnt_2,
			qp->q_cnt_3, (unsigned long long)qp->q_cnt_4);
3909
	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3910
			"\tCQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3911
			"HST-IDX[%04d], NTFI[%03d], PLMT[%03d]",
3912 3913
			qp->queue_id, qp->entry_count,
			qp->entry_size, qp->host_index,
3914
			qp->notify_interval, qp->max_proc_limit);
3915

3916 3917
	len +=  scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
			"\n");
3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928

	return len;
}

static int
__lpfc_idiag_print_rqpair(struct lpfc_queue *qp, struct lpfc_queue *datqp,
			char *rqtype, char *pbuffer, int len)
{
	if (!qp || !datqp)
		return len;

3929
	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3930
			"\t\t%s RQ info: ", rqtype);
3931
	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3932
			"AssocCQID[%02d]: RQ-STAT[nopost:x%x nobuf:x%x "
3933
			"posted:x%x rcv:x%llx]\n",
3934 3935
			qp->assoc_qid, qp->q_cnt_1, qp->q_cnt_2,
			qp->q_cnt_3, (unsigned long long)qp->q_cnt_4);
3936
	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3937
			"\t\tHQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3938
			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]\n",
3939
			qp->queue_id, qp->entry_count, qp->entry_size,
3940
			qp->host_index, qp->hba_index, qp->notify_interval);
3941
	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3942
			"\t\tDQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3943
			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]\n",
3944 3945
			datqp->queue_id, datqp->entry_count,
			datqp->entry_size, datqp->host_index,
3946
			datqp->hba_index, datqp->notify_interval);
3947 3948 3949 3950 3951
	return len;
}

static int
lpfc_idiag_cqs_for_eq(struct lpfc_hba *phba, char *pbuffer,
3952
		int *len, int max_cnt, int eqidx, int eq_id)
3953 3954
{
	struct lpfc_queue *qp;
3955
	int rc;
3956

3957
	qp = phba->sli4_hba.hdwq[eqidx].io_cq;
3958

3959
	*len = __lpfc_idiag_print_cq(qp, "IO", pbuffer, *len);
3960

3961 3962
	/* Reset max counter */
	qp->CQ_max_cqe = 0;
3963

3964 3965
	if (*len >= max_cnt)
		return 1;
3966

3967
	rc = lpfc_idiag_wqs_for_cq(phba, "IO", pbuffer, len,
3968 3969 3970
				   max_cnt, qp->queue_id);
	if (rc)
		return 1;
3971

3972
	if ((eqidx < phba->cfg_nvmet_mrq) && phba->nvmet_support) {
3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992
		/* NVMET CQset */
		qp = phba->sli4_hba.nvmet_cqset[eqidx];
		*len = __lpfc_idiag_print_cq(qp, "NVMET CQset", pbuffer, *len);

		/* Reset max counter */
		qp->CQ_max_cqe = 0;

		if (*len >= max_cnt)
			return 1;

		/* RQ header */
		qp = phba->sli4_hba.nvmet_mrq_hdr[eqidx];
		*len = __lpfc_idiag_print_rqpair(qp,
				phba->sli4_hba.nvmet_mrq_data[eqidx],
				"NVMET MRQ", pbuffer, *len);

		if (*len >= max_cnt)
			return 1;
	}

3993 3994 3995 3996 3997 3998 3999 4000 4001 4002
	return 0;
}

static int
__lpfc_idiag_print_eq(struct lpfc_queue *qp, char *eqtype,
			char *pbuffer, int len)
{
	if (!qp)
		return len;

4003
	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
4004
			"\n%s EQ info: EQ-STAT[max:x%x noE:x%x "
4005
			"cqe_proc:x%x eqe_proc:x%llx eqd %d]\n",
4006
			eqtype, qp->q_cnt_1, qp->q_cnt_2, qp->q_cnt_3,
4007
			(unsigned long long)qp->q_cnt_4, qp->q_mode);
4008
	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
4009
			"EQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
4010
			"HST-IDX[%04d], NTFI[%03d], PLMT[%03d], AFFIN[%03d]",
4011
			qp->queue_id, qp->entry_count, qp->entry_size,
4012 4013
			qp->host_index, qp->notify_interval,
			qp->max_proc_limit, qp->chann);
4014 4015
	len +=  scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
			"\n");
4016 4017 4018 4019

	return len;
}

4020 4021 4022 4023 4024 4025 4026 4027 4028 4029
/**
 * lpfc_idiag_queinfo_read - idiag debugfs read queue information
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the data to.
 * @nbytes: The number of bytes to read.
 * @ppos: The position in the file to start reading from.
 *
 * Description:
 * This routine reads data from the @phba SLI4 PCI function queue information,
 * and copies to user @buf.
4030 4031 4032
 * This routine only returns 1 EQs worth of information. It remembers the last
 * EQ read and jumps to the next EQ. Thus subsequent calls to queInfo will
 * retrieve all EQs allocated for the phba.
4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044
 *
 * Returns:
 * This function returns the amount of data that was read (this could be less
 * than @nbytes if the end of the file was reached) or a negative error value.
 **/
static ssize_t
lpfc_idiag_queinfo_read(struct file *file, char __user *buf, size_t nbytes,
			loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	char *pbuffer;
4045
	int max_cnt, rc, x, len = 0;
4046 4047
	struct lpfc_queue *qp = NULL;

4048 4049 4050 4051 4052
	if (!debug->buffer)
		debug->buffer = kmalloc(LPFC_QUE_INFO_GET_BUF_SIZE, GFP_KERNEL);
	if (!debug->buffer)
		return 0;
	pbuffer = debug->buffer;
4053
	max_cnt = LPFC_QUE_INFO_GET_BUF_SIZE - 256;
4054 4055 4056 4057

	if (*ppos)
		return 0;

4058 4059 4060
	spin_lock_irq(&phba->hbalock);

	/* Fast-path event queue */
4061
	if (phba->sli4_hba.hdwq && phba->cfg_hdw_queue) {
4062

4063 4064
		x = phba->lpfc_idiag_last_eq;
		phba->lpfc_idiag_last_eq++;
4065
		if (phba->lpfc_idiag_last_eq >= phba->cfg_hdw_queue)
4066
			phba->lpfc_idiag_last_eq = 0;
4067

4068 4069 4070 4071
		len += scnprintf(pbuffer + len,
				 LPFC_QUE_INFO_GET_BUF_SIZE - len,
				 "HDWQ %d out of %d HBA HDWQs\n",
				 x, phba->cfg_hdw_queue);
4072

4073
		/* Fast-path EQ */
4074
		qp = phba->sli4_hba.hdwq[x].hba_eq;
4075 4076
		if (!qp)
			goto out;
4077

4078
		len = __lpfc_idiag_print_eq(qp, "HBA", pbuffer, len);
4079

4080 4081
		/* Reset max counter */
		qp->EQ_max_eqe = 0;
4082

4083 4084
		if (len >= max_cnt)
			goto too_big;
4085

4086
		/* will dump both fcp and nvme cqs/wqs for the eq */
4087
		rc = lpfc_idiag_cqs_for_eq(phba, pbuffer, &len,
4088
			max_cnt, x, qp->queue_id);
4089 4090
		if (rc)
			goto too_big;
4091

4092 4093 4094
		/* Only EQ 0 has slow path CQs configured */
		if (x)
			goto out;
4095

4096 4097 4098 4099 4100
		/* Slow-path mailbox CQ */
		qp = phba->sli4_hba.mbx_cq;
		len = __lpfc_idiag_print_cq(qp, "MBX", pbuffer, len);
		if (len >= max_cnt)
			goto too_big;
4101

4102 4103 4104 4105 4106
		/* Slow-path MBOX MQ */
		qp = phba->sli4_hba.mbx_wq;
		len = __lpfc_idiag_print_wq(qp, "MBX", pbuffer, len);
		if (len >= max_cnt)
			goto too_big;
4107

4108 4109 4110 4111 4112 4113 4114 4115
		/* Slow-path ELS response CQ */
		qp = phba->sli4_hba.els_cq;
		len = __lpfc_idiag_print_cq(qp, "ELS", pbuffer, len);
		/* Reset max counter */
		if (qp)
			qp->CQ_max_cqe = 0;
		if (len >= max_cnt)
			goto too_big;
4116

4117 4118 4119 4120 4121
		/* Slow-path ELS WQ */
		qp = phba->sli4_hba.els_wq;
		len = __lpfc_idiag_print_wq(qp, "ELS", pbuffer, len);
		if (len >= max_cnt)
			goto too_big;
4122

4123 4124 4125 4126 4127 4128
		qp = phba->sli4_hba.hdr_rq;
		len = __lpfc_idiag_print_rqpair(qp, phba->sli4_hba.dat_rq,
						"ELS RQpair", pbuffer, len);
		if (len >= max_cnt)
			goto too_big;

4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145
		/* Slow-path NVME LS response CQ */
		qp = phba->sli4_hba.nvmels_cq;
		len = __lpfc_idiag_print_cq(qp, "NVME LS",
						pbuffer, len);
		/* Reset max counter */
		if (qp)
			qp->CQ_max_cqe = 0;
		if (len >= max_cnt)
			goto too_big;

		/* Slow-path NVME LS WQ */
		qp = phba->sli4_hba.nvmels_wq;
		len = __lpfc_idiag_print_wq(qp, "NVME LS",
						pbuffer, len);
		if (len >= max_cnt)
			goto too_big;

4146
		goto out;
4147 4148
	}

4149 4150 4151 4152
	spin_unlock_irq(&phba->hbalock);
	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);

too_big:
4153
	len +=  scnprintf(pbuffer + len,
4154 4155
		LPFC_QUE_INFO_GET_BUF_SIZE - len, "Truncated ...\n");
out:
4156
	spin_unlock_irq(&phba->hbalock);
4157 4158 4159
	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
}

4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186
/**
 * lpfc_idiag_que_param_check - queue access command parameter sanity check
 * @q: The pointer to queue structure.
 * @index: The index into a queue entry.
 * @count: The number of queue entries to access.
 *
 * Description:
 * The routine performs sanity check on device queue access method commands.
 *
 * Returns:
 * This function returns -EINVAL when fails the sanity check, otherwise, it
 * returns 0.
 **/
static int
lpfc_idiag_que_param_check(struct lpfc_queue *q, int index, int count)
{
	/* Only support single entry read or browsing */
	if ((count != 1) && (count != LPFC_QUE_ACC_BROWSE))
		return -EINVAL;
	if (index > q->entry_count - 1)
		return -EINVAL;
	return 0;
}

/**
 * lpfc_idiag_queacc_read_qe - read a single entry from the given queue index
 * @pbuffer: The pointer to buffer to copy the read data into.
4187
 * @len: Length of the buffer.
4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209
 * @pque: The pointer to the queue to be read.
 * @index: The index into the queue entry.
 *
 * Description:
 * This routine reads out a single entry from the given queue's index location
 * and copies it into the buffer provided.
 *
 * Returns:
 * This function returns 0 when it fails, otherwise, it returns the length of
 * the data read into the buffer provided.
 **/
static int
lpfc_idiag_queacc_read_qe(char *pbuffer, int len, struct lpfc_queue *pque,
			  uint32_t index)
{
	int offset, esize;
	uint32_t *pentry;

	if (!pbuffer || !pque)
		return 0;

	esize = pque->entry_size;
4210
	len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
4211 4212 4213
			"QE-INDEX[%04d]:\n", index);

	offset = 0;
4214
	pentry = lpfc_sli4_qe(pque, index);
4215
	while (esize > 0) {
4216
		len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
4217 4218 4219 4220 4221
				"%08x ", *pentry);
		pentry++;
		offset += sizeof(uint32_t);
		esize -= sizeof(uint32_t);
		if (esize > 0 && !(offset % (4 * sizeof(uint32_t))))
4222
			len += scnprintf(pbuffer+len,
4223 4224
					LPFC_QUE_ACC_BUF_SIZE-len, "\n");
	}
4225
	len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len, "\n");
4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269

	return len;
}

/**
 * lpfc_idiag_queacc_read - idiag debugfs read port queue
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the data to.
 * @nbytes: The number of bytes to read.
 * @ppos: The position in the file to start reading from.
 *
 * Description:
 * This routine reads data from the @phba device queue memory according to the
 * idiag command, and copies to user @buf. Depending on the queue dump read
 * command setup, it does either a single queue entry read or browing through
 * all entries of the queue.
 *
 * Returns:
 * This function returns the amount of data that was read (this could be less
 * than @nbytes if the end of the file was reached) or a negative error value.
 **/
static ssize_t
lpfc_idiag_queacc_read(struct file *file, char __user *buf, size_t nbytes,
		       loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	uint32_t last_index, index, count;
	struct lpfc_queue *pque = NULL;
	char *pbuffer;
	int len = 0;

	/* This is a user read operation */
	debug->op = LPFC_IDIAG_OP_RD;

	if (!debug->buffer)
		debug->buffer = kmalloc(LPFC_QUE_ACC_BUF_SIZE, GFP_KERNEL);
	if (!debug->buffer)
		return 0;
	pbuffer = debug->buffer;

	if (*ppos)
		return 0;

	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
4270 4271
		index = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
		count = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331
		pque = (struct lpfc_queue *)idiag.ptr_private;
	} else
		return 0;

	/* Browse the queue starting from index */
	if (count == LPFC_QUE_ACC_BROWSE)
		goto que_browse;

	/* Read a single entry from the queue */
	len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);

	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);

que_browse:

	/* Browse all entries from the queue */
	last_index = idiag.offset.last_rd;
	index = last_index;

	while (len < LPFC_QUE_ACC_SIZE - pque->entry_size) {
		len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
		index++;
		if (index > pque->entry_count - 1)
			break;
	}

	/* Set up the offset for next portion of pci cfg read */
	if (index > pque->entry_count - 1)
		index = 0;
	idiag.offset.last_rd = index;

	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
}

/**
 * lpfc_idiag_queacc_write - Syntax check and set up idiag queacc commands
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the user data from.
 * @nbytes: The number of bytes to get.
 * @ppos: The position in the file to start reading from.
 *
 * This routine get the debugfs idiag command struct from user space and then
 * perform the syntax check for port queue read (dump) or write (set) command
 * accordingly. In the case of port queue read command, it sets up the command
 * in the idiag command struct for the following debugfs read operation. In
 * the case of port queue write operation, it executes the write operation
 * into the port queue entry accordingly.
 *
 * It returns the @nbytges passing in from debugfs user space when successful.
 * In case of error conditions, it returns proper error code back to the user
 * space.
 **/
static ssize_t
lpfc_idiag_queacc_write(struct file *file, const char __user *buf,
			size_t nbytes, loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	uint32_t qidx, quetp, queid, index, count, offset, value;
	uint32_t *pentry;
4332
	struct lpfc_queue *pque, *qp;
4333 4334 4335 4336 4337 4338 4339 4340 4341 4342
	int rc;

	/* This is a user write operation */
	debug->op = LPFC_IDIAG_OP_WR;

	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
	if (rc < 0)
		return rc;

	/* Get and sanity check on command feilds */
4343 4344 4345 4346 4347 4348
	quetp  = idiag.cmd.data[IDIAG_QUEACC_QUETP_INDX];
	queid  = idiag.cmd.data[IDIAG_QUEACC_QUEID_INDX];
	index  = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
	count  = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
	offset = idiag.cmd.data[IDIAG_QUEACC_OFFST_INDX];
	value  = idiag.cmd.data[IDIAG_QUEACC_VALUE_INDX];
4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365

	/* Sanity check on command line arguments */
	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
		if (rc != LPFC_QUE_ACC_WR_CMD_ARG)
			goto error_out;
		if (count != 1)
			goto error_out;
	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
		if (rc != LPFC_QUE_ACC_RD_CMD_ARG)
			goto error_out;
	} else
		goto error_out;

	switch (quetp) {
	case LPFC_IDIAG_EQ:
4366
		/* HBA event queue */
4367 4368 4369
		if (phba->sli4_hba.hdwq) {
			for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
				qp = phba->sli4_hba.hdwq[qidx].hba_eq;
4370
				if (qp && qp->queue_id == queid) {
4371
					/* Sanity check */
4372
					rc = lpfc_idiag_que_param_check(qp,
4373
						index, count);
4374 4375
					if (rc)
						goto error_out;
4376
					idiag.ptr_private = qp;
4377 4378
					goto pass_check;
				}
4379 4380 4381
			}
		}
		goto error_out;
T
Tom Rix 已提交
4382

4383 4384
	case LPFC_IDIAG_CQ:
		/* MBX complete queue */
4385 4386
		if (phba->sli4_hba.mbx_cq &&
		    phba->sli4_hba.mbx_cq->queue_id == queid) {
4387 4388 4389 4390 4391 4392 4393 4394 4395
			/* Sanity check */
			rc = lpfc_idiag_que_param_check(
					phba->sli4_hba.mbx_cq, index, count);
			if (rc)
				goto error_out;
			idiag.ptr_private = phba->sli4_hba.mbx_cq;
			goto pass_check;
		}
		/* ELS complete queue */
4396 4397
		if (phba->sli4_hba.els_cq &&
		    phba->sli4_hba.els_cq->queue_id == queid) {
4398 4399 4400 4401 4402 4403 4404 4405
			/* Sanity check */
			rc = lpfc_idiag_que_param_check(
					phba->sli4_hba.els_cq, index, count);
			if (rc)
				goto error_out;
			idiag.ptr_private = phba->sli4_hba.els_cq;
			goto pass_check;
		}
4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416
		/* NVME LS complete queue */
		if (phba->sli4_hba.nvmels_cq &&
		    phba->sli4_hba.nvmels_cq->queue_id == queid) {
			/* Sanity check */
			rc = lpfc_idiag_que_param_check(
					phba->sli4_hba.nvmels_cq, index, count);
			if (rc)
				goto error_out;
			idiag.ptr_private = phba->sli4_hba.nvmels_cq;
			goto pass_check;
		}
4417
		/* FCP complete queue */
4418 4419
		if (phba->sli4_hba.hdwq) {
			for (qidx = 0; qidx < phba->cfg_hdw_queue;
4420
								qidx++) {
4421
				qp = phba->sli4_hba.hdwq[qidx].io_cq;
4422
				if (qp && qp->queue_id == queid) {
4423 4424
					/* Sanity check */
					rc = lpfc_idiag_que_param_check(
4425
						qp, index, count);
4426 4427
					if (rc)
						goto error_out;
4428
					idiag.ptr_private = qp;
4429 4430
					goto pass_check;
				}
4431
			}
4432
		}
4433
		goto error_out;
T
Tom Rix 已提交
4434

4435 4436
	case LPFC_IDIAG_MQ:
		/* MBX work queue */
4437 4438
		if (phba->sli4_hba.mbx_wq &&
		    phba->sli4_hba.mbx_wq->queue_id == queid) {
4439 4440 4441 4442 4443 4444 4445 4446
			/* Sanity check */
			rc = lpfc_idiag_que_param_check(
					phba->sli4_hba.mbx_wq, index, count);
			if (rc)
				goto error_out;
			idiag.ptr_private = phba->sli4_hba.mbx_wq;
			goto pass_check;
		}
4447
		goto error_out;
T
Tom Rix 已提交
4448

4449 4450
	case LPFC_IDIAG_WQ:
		/* ELS work queue */
4451 4452
		if (phba->sli4_hba.els_wq &&
		    phba->sli4_hba.els_wq->queue_id == queid) {
4453 4454 4455 4456 4457 4458 4459 4460
			/* Sanity check */
			rc = lpfc_idiag_que_param_check(
					phba->sli4_hba.els_wq, index, count);
			if (rc)
				goto error_out;
			idiag.ptr_private = phba->sli4_hba.els_wq;
			goto pass_check;
		}
4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471
		/* NVME LS work queue */
		if (phba->sli4_hba.nvmels_wq &&
		    phba->sli4_hba.nvmels_wq->queue_id == queid) {
			/* Sanity check */
			rc = lpfc_idiag_que_param_check(
					phba->sli4_hba.nvmels_wq, index, count);
			if (rc)
				goto error_out;
			idiag.ptr_private = phba->sli4_hba.nvmels_wq;
			goto pass_check;
		}
4472 4473 4474 4475

		if (phba->sli4_hba.hdwq) {
			/* FCP/SCSI work queue */
			for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
4476
				qp = phba->sli4_hba.hdwq[qidx].io_wq;
4477 4478 4479 4480
				if (qp && qp->queue_id == queid) {
					/* Sanity check */
					rc = lpfc_idiag_que_param_check(
						qp, index, count);
4481 4482
					if (rc)
						goto error_out;
4483
					idiag.ptr_private = qp;
4484 4485
					goto pass_check;
				}
4486 4487 4488
			}
		}
		goto error_out;
T
Tom Rix 已提交
4489

4490 4491
	case LPFC_IDIAG_RQ:
		/* HDR queue */
4492 4493
		if (phba->sli4_hba.hdr_rq &&
		    phba->sli4_hba.hdr_rq->queue_id == queid) {
4494 4495 4496 4497 4498 4499 4500 4501 4502
			/* Sanity check */
			rc = lpfc_idiag_que_param_check(
					phba->sli4_hba.hdr_rq, index, count);
			if (rc)
				goto error_out;
			idiag.ptr_private = phba->sli4_hba.hdr_rq;
			goto pass_check;
		}
		/* DAT queue */
4503 4504
		if (phba->sli4_hba.dat_rq &&
		    phba->sli4_hba.dat_rq->queue_id == queid) {
4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531
			/* Sanity check */
			rc = lpfc_idiag_que_param_check(
					phba->sli4_hba.dat_rq, index, count);
			if (rc)
				goto error_out;
			idiag.ptr_private = phba->sli4_hba.dat_rq;
			goto pass_check;
		}
		goto error_out;
	default:
		goto error_out;
	}

pass_check:

	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
		if (count == LPFC_QUE_ACC_BROWSE)
			idiag.offset.last_rd = index;
	}

	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
		/* Additional sanity checks on write operation */
		pque = (struct lpfc_queue *)idiag.ptr_private;
		if (offset > pque->entry_size/sizeof(uint32_t) - 1)
			goto error_out;
4532
		pentry = lpfc_sli4_qe(pque, index);
4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552
		pentry += offset;
		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR)
			*pentry = value;
		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST)
			*pentry |= value;
		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL)
			*pentry &= ~value;
	}
	return nbytes;

error_out:
	/* Clean out command structure on command error out */
	memset(&idiag, 0, sizeof(idiag));
	return -EINVAL;
}

/**
 * lpfc_idiag_drbacc_read_reg - idiag debugfs read a doorbell register
 * @phba: The pointer to hba structure.
 * @pbuffer: The pointer to the buffer to copy the data to.
M
Matteo Croce 已提交
4553
 * @len: The length of bytes to copied.
4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571
 * @drbregid: The id to doorbell registers.
 *
 * Description:
 * This routine reads a doorbell register and copies its content to the
 * user buffer pointed to by @pbuffer.
 *
 * Returns:
 * This function returns the amount of data that was copied into @pbuffer.
 **/
static int
lpfc_idiag_drbacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
			   int len, uint32_t drbregid)
{

	if (!pbuffer)
		return 0;

	switch (drbregid) {
4572
	case LPFC_DRB_EQ:
4573
		len += scnprintf(pbuffer + len, LPFC_DRB_ACC_BUF_SIZE-len,
4574 4575 4576 4577
				"EQ-DRB-REG: 0x%08x\n",
				readl(phba->sli4_hba.EQDBregaddr));
		break;
	case LPFC_DRB_CQ:
4578
		len += scnprintf(pbuffer + len, LPFC_DRB_ACC_BUF_SIZE - len,
4579 4580
				"CQ-DRB-REG: 0x%08x\n",
				readl(phba->sli4_hba.CQDBregaddr));
4581 4582
		break;
	case LPFC_DRB_MQ:
4583
		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4584 4585 4586 4587
				"MQ-DRB-REG:   0x%08x\n",
				readl(phba->sli4_hba.MQDBregaddr));
		break;
	case LPFC_DRB_WQ:
4588
		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4589 4590 4591 4592
				"WQ-DRB-REG:   0x%08x\n",
				readl(phba->sli4_hba.WQDBregaddr));
		break;
	case LPFC_DRB_RQ:
4593
		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643
				"RQ-DRB-REG:   0x%08x\n",
				readl(phba->sli4_hba.RQDBregaddr));
		break;
	default:
		break;
	}

	return len;
}

/**
 * lpfc_idiag_drbacc_read - idiag debugfs read port doorbell
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the data to.
 * @nbytes: The number of bytes to read.
 * @ppos: The position in the file to start reading from.
 *
 * Description:
 * This routine reads data from the @phba device doorbell register according
 * to the idiag command, and copies to user @buf. Depending on the doorbell
 * register read command setup, it does either a single doorbell register
 * read or dump all doorbell registers.
 *
 * Returns:
 * This function returns the amount of data that was read (this could be less
 * than @nbytes if the end of the file was reached) or a negative error value.
 **/
static ssize_t
lpfc_idiag_drbacc_read(struct file *file, char __user *buf, size_t nbytes,
		       loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	uint32_t drb_reg_id, i;
	char *pbuffer;
	int len = 0;

	/* This is a user read operation */
	debug->op = LPFC_IDIAG_OP_RD;

	if (!debug->buffer)
		debug->buffer = kmalloc(LPFC_DRB_ACC_BUF_SIZE, GFP_KERNEL);
	if (!debug->buffer)
		return 0;
	pbuffer = debug->buffer;

	if (*ppos)
		return 0;

	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD)
4644
		drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682
	else
		return 0;

	if (drb_reg_id == LPFC_DRB_ACC_ALL)
		for (i = 1; i <= LPFC_DRB_MAX; i++)
			len = lpfc_idiag_drbacc_read_reg(phba,
							 pbuffer, len, i);
	else
		len = lpfc_idiag_drbacc_read_reg(phba,
						 pbuffer, len, drb_reg_id);

	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
}

/**
 * lpfc_idiag_drbacc_write - Syntax check and set up idiag drbacc commands
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the user data from.
 * @nbytes: The number of bytes to get.
 * @ppos: The position in the file to start reading from.
 *
 * This routine get the debugfs idiag command struct from user space and then
 * perform the syntax check for port doorbell register read (dump) or write
 * (set) command accordingly. In the case of port queue read command, it sets
 * up the command in the idiag command struct for the following debugfs read
 * operation. In the case of port doorbell register write operation, it
 * executes the write operation into the port doorbell register accordingly.
 *
 * It returns the @nbytges passing in from debugfs user space when successful.
 * In case of error conditions, it returns proper error code back to the user
 * space.
 **/
static ssize_t
lpfc_idiag_drbacc_write(struct file *file, const char __user *buf,
			size_t nbytes, loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4683
	uint32_t drb_reg_id, value, reg_val = 0;
4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694
	void __iomem *drb_reg;
	int rc;

	/* This is a user write operation */
	debug->op = LPFC_IDIAG_OP_WR;

	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
	if (rc < 0)
		return rc;

	/* Sanity check on command line arguments */
4695 4696
	drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
	value = idiag.cmd.data[IDIAG_DRBACC_VALUE_INDX];
4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718

	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
		if (rc != LPFC_DRB_ACC_WR_CMD_ARG)
			goto error_out;
		if (drb_reg_id > LPFC_DRB_MAX)
			goto error_out;
	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD) {
		if (rc != LPFC_DRB_ACC_RD_CMD_ARG)
			goto error_out;
		if ((drb_reg_id > LPFC_DRB_MAX) &&
		    (drb_reg_id != LPFC_DRB_ACC_ALL))
			goto error_out;
	} else
		goto error_out;

	/* Perform the write access operation */
	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
		switch (drb_reg_id) {
4719 4720 4721 4722 4723
		case LPFC_DRB_EQ:
			drb_reg = phba->sli4_hba.EQDBregaddr;
			break;
		case LPFC_DRB_CQ:
			drb_reg = phba->sli4_hba.CQDBregaddr;
4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758
			break;
		case LPFC_DRB_MQ:
			drb_reg = phba->sli4_hba.MQDBregaddr;
			break;
		case LPFC_DRB_WQ:
			drb_reg = phba->sli4_hba.WQDBregaddr;
			break;
		case LPFC_DRB_RQ:
			drb_reg = phba->sli4_hba.RQDBregaddr;
			break;
		default:
			goto error_out;
		}

		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR)
			reg_val = value;
		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST) {
			reg_val = readl(drb_reg);
			reg_val |= value;
		}
		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
			reg_val = readl(drb_reg);
			reg_val &= ~value;
		}
		writel(reg_val, drb_reg);
		readl(drb_reg); /* flush */
	}
	return nbytes;

error_out:
	/* Clean out command structure on command error out */
	memset(&idiag, 0, sizeof(idiag));
	return -EINVAL;
}

4759 4760 4761 4762
/**
 * lpfc_idiag_ctlacc_read_reg - idiag debugfs read a control registers
 * @phba: The pointer to hba structure.
 * @pbuffer: The pointer to the buffer to copy the data to.
M
Matteo Croce 已提交
4763
 * @len: The length of bytes to copied.
4764
 * @ctlregid: The id to doorbell registers.
4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776
 *
 * Description:
 * This routine reads a control register and copies its content to the
 * user buffer pointed to by @pbuffer.
 *
 * Returns:
 * This function returns the amount of data that was copied into @pbuffer.
 **/
static int
lpfc_idiag_ctlacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
			   int len, uint32_t ctlregid)
{
J
James Smart 已提交
4777

4778 4779
	if (!pbuffer)
		return 0;
J
James Smart 已提交
4780

4781 4782
	switch (ctlregid) {
	case LPFC_CTL_PORT_SEM:
4783
		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4784 4785 4786 4787 4788
				"Port SemReg:   0x%08x\n",
				readl(phba->sli4_hba.conf_regs_memmap_p +
				      LPFC_CTL_PORT_SEM_OFFSET));
		break;
	case LPFC_CTL_PORT_STA:
4789
		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4790 4791 4792 4793 4794
				"Port StaReg:   0x%08x\n",
				readl(phba->sli4_hba.conf_regs_memmap_p +
				      LPFC_CTL_PORT_STA_OFFSET));
		break;
	case LPFC_CTL_PORT_CTL:
4795
		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4796 4797 4798 4799 4800
				"Port CtlReg:   0x%08x\n",
				readl(phba->sli4_hba.conf_regs_memmap_p +
				      LPFC_CTL_PORT_CTL_OFFSET));
		break;
	case LPFC_CTL_PORT_ER1:
4801
		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4802 4803 4804 4805 4806
				"Port Er1Reg:   0x%08x\n",
				readl(phba->sli4_hba.conf_regs_memmap_p +
				      LPFC_CTL_PORT_ER1_OFFSET));
		break;
	case LPFC_CTL_PORT_ER2:
4807
		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4808 4809 4810 4811 4812
				"Port Er2Reg:   0x%08x\n",
				readl(phba->sli4_hba.conf_regs_memmap_p +
				      LPFC_CTL_PORT_ER2_OFFSET));
		break;
	case LPFC_CTL_PDEV_CTL:
4813
		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4814 4815 4816 4817 4818 4819 4820 4821 4822
				"PDev CtlReg:   0x%08x\n",
				readl(phba->sli4_hba.conf_regs_memmap_p +
				      LPFC_CTL_PDEV_CTL_OFFSET));
		break;
	default:
		break;
	}
	return len;
}
4823

4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847
/**
 * lpfc_idiag_ctlacc_read - idiag debugfs read port and device control register
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the data to.
 * @nbytes: The number of bytes to read.
 * @ppos: The position in the file to start reading from.
 *
 * Description:
 * This routine reads data from the @phba port and device registers according
 * to the idiag command, and copies to user @buf.
 *
 * Returns:
 * This function returns the amount of data that was read (this could be less
 * than @nbytes if the end of the file was reached) or a negative error value.
 **/
static ssize_t
lpfc_idiag_ctlacc_read(struct file *file, char __user *buf, size_t nbytes,
		       loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	uint32_t ctl_reg_id, i;
	char *pbuffer;
	int len = 0;
4848

4849 4850
	/* This is a user read operation */
	debug->op = LPFC_IDIAG_OP_RD;
4851

4852 4853 4854 4855 4856
	if (!debug->buffer)
		debug->buffer = kmalloc(LPFC_CTL_ACC_BUF_SIZE, GFP_KERNEL);
	if (!debug->buffer)
		return 0;
	pbuffer = debug->buffer;
4857

4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005
	if (*ppos)
		return 0;

	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD)
		ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
	else
		return 0;

	if (ctl_reg_id == LPFC_CTL_ACC_ALL)
		for (i = 1; i <= LPFC_CTL_MAX; i++)
			len = lpfc_idiag_ctlacc_read_reg(phba,
							 pbuffer, len, i);
	else
		len = lpfc_idiag_ctlacc_read_reg(phba,
						 pbuffer, len, ctl_reg_id);

	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
}

/**
 * lpfc_idiag_ctlacc_write - Syntax check and set up idiag ctlacc commands
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the user data from.
 * @nbytes: The number of bytes to get.
 * @ppos: The position in the file to start reading from.
 *
 * This routine get the debugfs idiag command struct from user space and then
 * perform the syntax check for port and device control register read (dump)
 * or write (set) command accordingly.
 *
 * It returns the @nbytges passing in from debugfs user space when successful.
 * In case of error conditions, it returns proper error code back to the user
 * space.
 **/
static ssize_t
lpfc_idiag_ctlacc_write(struct file *file, const char __user *buf,
			size_t nbytes, loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	uint32_t ctl_reg_id, value, reg_val = 0;
	void __iomem *ctl_reg;
	int rc;

	/* This is a user write operation */
	debug->op = LPFC_IDIAG_OP_WR;

	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
	if (rc < 0)
		return rc;

	/* Sanity check on command line arguments */
	ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
	value = idiag.cmd.data[IDIAG_CTLACC_VALUE_INDX];

	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
		if (rc != LPFC_CTL_ACC_WR_CMD_ARG)
			goto error_out;
		if (ctl_reg_id > LPFC_CTL_MAX)
			goto error_out;
	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD) {
		if (rc != LPFC_CTL_ACC_RD_CMD_ARG)
			goto error_out;
		if ((ctl_reg_id > LPFC_CTL_MAX) &&
		    (ctl_reg_id != LPFC_CTL_ACC_ALL))
			goto error_out;
	} else
		goto error_out;

	/* Perform the write access operation */
	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
		switch (ctl_reg_id) {
		case LPFC_CTL_PORT_SEM:
			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
					LPFC_CTL_PORT_SEM_OFFSET;
			break;
		case LPFC_CTL_PORT_STA:
			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
					LPFC_CTL_PORT_STA_OFFSET;
			break;
		case LPFC_CTL_PORT_CTL:
			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
					LPFC_CTL_PORT_CTL_OFFSET;
			break;
		case LPFC_CTL_PORT_ER1:
			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
					LPFC_CTL_PORT_ER1_OFFSET;
			break;
		case LPFC_CTL_PORT_ER2:
			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
					LPFC_CTL_PORT_ER2_OFFSET;
			break;
		case LPFC_CTL_PDEV_CTL:
			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
					LPFC_CTL_PDEV_CTL_OFFSET;
			break;
		default:
			goto error_out;
		}

		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR)
			reg_val = value;
		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST) {
			reg_val = readl(ctl_reg);
			reg_val |= value;
		}
		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
			reg_val = readl(ctl_reg);
			reg_val &= ~value;
		}
		writel(reg_val, ctl_reg);
		readl(ctl_reg); /* flush */
	}
	return nbytes;

error_out:
	/* Clean out command structure on command error out */
	memset(&idiag, 0, sizeof(idiag));
	return -EINVAL;
}

/**
 * lpfc_idiag_mbxacc_get_setup - idiag debugfs get mailbox access setup
 * @phba: Pointer to HBA context object.
 * @pbuffer: Pointer to data buffer.
 *
 * Description:
 * This routine gets the driver mailbox access debugfs setup information.
 *
 * Returns:
 * This function returns the amount of data that was read (this could be less
 * than @nbytes if the end of the file was reached) or a negative error value.
 **/
static int
lpfc_idiag_mbxacc_get_setup(struct lpfc_hba *phba, char *pbuffer)
{
	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
	int len = 0;

	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];

5006
	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5007
			"mbx_dump_map: 0x%08x\n", mbx_dump_map);
5008
	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5009
			"mbx_dump_cnt: %04d\n", mbx_dump_cnt);
5010
	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5011
			"mbx_word_cnt: %04d\n", mbx_word_cnt);
5012
	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153
			"mbx_mbox_cmd: 0x%02x\n", mbx_mbox_cmd);

	return len;
}

/**
 * lpfc_idiag_mbxacc_read - idiag debugfs read on mailbox access
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the data to.
 * @nbytes: The number of bytes to read.
 * @ppos: The position in the file to start reading from.
 *
 * Description:
 * This routine reads data from the @phba driver mailbox access debugfs setup
 * information.
 *
 * Returns:
 * This function returns the amount of data that was read (this could be less
 * than @nbytes if the end of the file was reached) or a negative error value.
 **/
static ssize_t
lpfc_idiag_mbxacc_read(struct file *file, char __user *buf, size_t nbytes,
		       loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	char *pbuffer;
	int len = 0;

	/* This is a user read operation */
	debug->op = LPFC_IDIAG_OP_RD;

	if (!debug->buffer)
		debug->buffer = kmalloc(LPFC_MBX_ACC_BUF_SIZE, GFP_KERNEL);
	if (!debug->buffer)
		return 0;
	pbuffer = debug->buffer;

	if (*ppos)
		return 0;

	if ((idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP) &&
	    (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP))
		return 0;

	len = lpfc_idiag_mbxacc_get_setup(phba, pbuffer);

	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
}

/**
 * lpfc_idiag_mbxacc_write - Syntax check and set up idiag mbxacc commands
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the user data from.
 * @nbytes: The number of bytes to get.
 * @ppos: The position in the file to start reading from.
 *
 * This routine get the debugfs idiag command struct from user space and then
 * perform the syntax check for driver mailbox command (dump) and sets up the
 * necessary states in the idiag command struct accordingly.
 *
 * It returns the @nbytges passing in from debugfs user space when successful.
 * In case of error conditions, it returns proper error code back to the user
 * space.
 **/
static ssize_t
lpfc_idiag_mbxacc_write(struct file *file, const char __user *buf,
			size_t nbytes, loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
	int rc;

	/* This is a user write operation */
	debug->op = LPFC_IDIAG_OP_WR;

	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
	if (rc < 0)
		return rc;

	/* Sanity check on command line arguments */
	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];

	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_MBXACC_DP) {
		if (!(mbx_dump_map & LPFC_MBX_DMP_MBX_ALL))
			goto error_out;
		if ((mbx_dump_map & ~LPFC_MBX_DMP_MBX_ALL) &&
		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
			goto error_out;
		if (mbx_word_cnt > sizeof(MAILBOX_t))
			goto error_out;
	} else if (idiag.cmd.opcode == LPFC_IDIAG_BSG_MBXACC_DP) {
		if (!(mbx_dump_map & LPFC_BSG_DMP_MBX_ALL))
			goto error_out;
		if ((mbx_dump_map & ~LPFC_BSG_DMP_MBX_ALL) &&
		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
			goto error_out;
		if (mbx_word_cnt > (BSG_MBOX_SIZE)/4)
			goto error_out;
		if (mbx_mbox_cmd != 0x9b)
			goto error_out;
	} else
		goto error_out;

	if (mbx_word_cnt == 0)
		goto error_out;
	if (rc != LPFC_MBX_DMP_ARG)
		goto error_out;
	if (mbx_mbox_cmd & ~0xff)
		goto error_out;

	/* condition for stop mailbox dump */
	if (mbx_dump_cnt == 0)
		goto reset_out;

	return nbytes;

reset_out:
	/* Clean out command structure on command error out */
	memset(&idiag, 0, sizeof(idiag));
	return nbytes;

error_out:
	/* Clean out command structure on command error out */
	memset(&idiag, 0, sizeof(idiag));
	return -EINVAL;
}

/**
 * lpfc_idiag_extacc_avail_get - get the available extents information
 * @phba: pointer to lpfc hba data structure.
 * @pbuffer: pointer to internal buffer.
 * @len: length into the internal buffer data has been copied.
 *
 * Description:
 * This routine is to get the available extent information.
 *
 * Returns:
W
wengjianfeng 已提交
5154
 * overall length of the data read into the internal buffer.
5155 5156 5157 5158 5159 5160
 **/
static int
lpfc_idiag_extacc_avail_get(struct lpfc_hba *phba, char *pbuffer, int len)
{
	uint16_t ext_cnt, ext_size;

5161
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5162 5163
			"\nAvailable Extents Information:\n");

5164
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5165 5166 5167
			"\tPort Available VPI extents: ");
	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VPI,
				       &ext_cnt, &ext_size);
5168
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5169 5170
			"Count %3d, Size %3d\n", ext_cnt, ext_size);

5171
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5172 5173 5174
			"\tPort Available VFI extents: ");
	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VFI,
				       &ext_cnt, &ext_size);
5175
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5176 5177
			"Count %3d, Size %3d\n", ext_cnt, ext_size);

5178
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5179 5180 5181
			"\tPort Available RPI extents: ");
	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_RPI,
				       &ext_cnt, &ext_size);
5182
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5183 5184
			"Count %3d, Size %3d\n", ext_cnt, ext_size);

5185
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5186 5187 5188
			"\tPort Available XRI extents: ");
	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_XRI,
				       &ext_cnt, &ext_size);
5189
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204
			"Count %3d, Size %3d\n", ext_cnt, ext_size);

	return len;
}

/**
 * lpfc_idiag_extacc_alloc_get - get the allocated extents information
 * @phba: pointer to lpfc hba data structure.
 * @pbuffer: pointer to internal buffer.
 * @len: length into the internal buffer data has been copied.
 *
 * Description:
 * This routine is to get the allocated extent information.
 *
 * Returns:
W
wengjianfeng 已提交
5205
 * overall length of the data read into the internal buffer.
5206 5207 5208 5209 5210 5211 5212
 **/
static int
lpfc_idiag_extacc_alloc_get(struct lpfc_hba *phba, char *pbuffer, int len)
{
	uint16_t ext_cnt, ext_size;
	int rc;

5213
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5214 5215
			"\nAllocated Extents Information:\n");

5216
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5217 5218 5219 5220
			"\tHost Allocated VPI extents: ");
	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VPI,
					    &ext_cnt, &ext_size);
	if (!rc)
5221
		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5222 5223 5224
				"Port %d Extent %3d, Size %3d\n",
				phba->brd_no, ext_cnt, ext_size);
	else
5225
		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5226 5227
				"N/A\n");

5228
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5229 5230 5231 5232
			"\tHost Allocated VFI extents: ");
	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VFI,
					    &ext_cnt, &ext_size);
	if (!rc)
5233
		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5234 5235 5236
				"Port %d Extent %3d, Size %3d\n",
				phba->brd_no, ext_cnt, ext_size);
	else
5237
		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5238 5239
				"N/A\n");

5240
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5241 5242 5243 5244
			"\tHost Allocated RPI extents: ");
	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_RPI,
					    &ext_cnt, &ext_size);
	if (!rc)
5245
		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5246 5247 5248
				"Port %d Extent %3d, Size %3d\n",
				phba->brd_no, ext_cnt, ext_size);
	else
5249
		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5250 5251
				"N/A\n");

5252
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5253 5254 5255 5256
			"\tHost Allocated XRI extents: ");
	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_XRI,
					    &ext_cnt, &ext_size);
	if (!rc)
5257
		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5258 5259 5260
				"Port %d Extent %3d, Size %3d\n",
				phba->brd_no, ext_cnt, ext_size);
	else
5261
		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276
				"N/A\n");

	return len;
}

/**
 * lpfc_idiag_extacc_drivr_get - get driver extent information
 * @phba: pointer to lpfc hba data structure.
 * @pbuffer: pointer to internal buffer.
 * @len: length into the internal buffer data has been copied.
 *
 * Description:
 * This routine is to get the driver extent information.
 *
 * Returns:
W
wengjianfeng 已提交
5277
 * overall length of the data read into the internal buffer.
5278 5279 5280 5281 5282 5283 5284
 **/
static int
lpfc_idiag_extacc_drivr_get(struct lpfc_hba *phba, char *pbuffer, int len)
{
	struct lpfc_rsrc_blks *rsrc_blks;
	int index;

5285
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5286 5287
			"\nDriver Extents Information:\n");

5288
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5289 5290 5291
			"\tVPI extents:\n");
	index = 0;
	list_for_each_entry(rsrc_blks, &phba->lpfc_vpi_blk_list, list) {
5292
		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5293 5294 5295 5296 5297
				"\t\tBlock %3d: Start %4d, Count %4d\n",
				index, rsrc_blks->rsrc_start,
				rsrc_blks->rsrc_size);
		index++;
	}
5298
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5299 5300 5301 5302
			"\tVFI extents:\n");
	index = 0;
	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_vfi_blk_list,
			    list) {
5303
		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5304 5305 5306 5307 5308 5309
				"\t\tBlock %3d: Start %4d, Count %4d\n",
				index, rsrc_blks->rsrc_start,
				rsrc_blks->rsrc_size);
		index++;
	}

5310
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5311 5312 5313 5314
			"\tRPI extents:\n");
	index = 0;
	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_rpi_blk_list,
			    list) {
5315
		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5316 5317 5318 5319 5320 5321
				"\t\tBlock %3d: Start %4d, Count %4d\n",
				index, rsrc_blks->rsrc_start,
				rsrc_blks->rsrc_size);
		index++;
	}

5322
	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5323 5324 5325 5326
			"\tXRI extents:\n");
	index = 0;
	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_xri_blk_list,
			    list) {
5327
		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431
				"\t\tBlock %3d: Start %4d, Count %4d\n",
				index, rsrc_blks->rsrc_start,
				rsrc_blks->rsrc_size);
		index++;
	}

	return len;
}

/**
 * lpfc_idiag_extacc_write - Syntax check and set up idiag extacc commands
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the user data from.
 * @nbytes: The number of bytes to get.
 * @ppos: The position in the file to start reading from.
 *
 * This routine get the debugfs idiag command struct from user space and then
 * perform the syntax check for extent information access commands and sets
 * up the necessary states in the idiag command struct accordingly.
 *
 * It returns the @nbytges passing in from debugfs user space when successful.
 * In case of error conditions, it returns proper error code back to the user
 * space.
 **/
static ssize_t
lpfc_idiag_extacc_write(struct file *file, const char __user *buf,
			size_t nbytes, loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	uint32_t ext_map;
	int rc;

	/* This is a user write operation */
	debug->op = LPFC_IDIAG_OP_WR;

	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
	if (rc < 0)
		return rc;

	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];

	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
		goto error_out;
	if (rc != LPFC_EXT_ACC_CMD_ARG)
		goto error_out;
	if (!(ext_map & LPFC_EXT_ACC_ALL))
		goto error_out;

	return nbytes;
error_out:
	/* Clean out command structure on command error out */
	memset(&idiag, 0, sizeof(idiag));
	return -EINVAL;
}

/**
 * lpfc_idiag_extacc_read - idiag debugfs read access to extent information
 * @file: The file pointer to read from.
 * @buf: The buffer to copy the data to.
 * @nbytes: The number of bytes to read.
 * @ppos: The position in the file to start reading from.
 *
 * Description:
 * This routine reads data from the proper extent information according to
 * the idiag command, and copies to user @buf.
 *
 * Returns:
 * This function returns the amount of data that was read (this could be less
 * than @nbytes if the end of the file was reached) or a negative error value.
 **/
static ssize_t
lpfc_idiag_extacc_read(struct file *file, char __user *buf, size_t nbytes,
		       loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	char *pbuffer;
	uint32_t ext_map;
	int len = 0;

	/* This is a user read operation */
	debug->op = LPFC_IDIAG_OP_RD;

	if (!debug->buffer)
		debug->buffer = kmalloc(LPFC_EXT_ACC_BUF_SIZE, GFP_KERNEL);
	if (!debug->buffer)
		return 0;
	pbuffer = debug->buffer;
	if (*ppos)
		return 0;
	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
		return 0;

	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
	if (ext_map & LPFC_EXT_ACC_AVAIL)
		len = lpfc_idiag_extacc_avail_get(phba, pbuffer, len);
	if (ext_map & LPFC_EXT_ACC_ALLOC)
		len = lpfc_idiag_extacc_alloc_get(phba, pbuffer, len);
	if (ext_map & LPFC_EXT_ACC_DRIVR)
		len = lpfc_idiag_extacc_drivr_get(phba, pbuffer, len);

	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
}

5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605
static int
lpfc_cgn_buffer_open(struct inode *inode, struct file *file)
{
	struct lpfc_debug *debug;
	int rc = -ENOMEM;

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

	debug->buffer = vmalloc(LPFC_CGN_BUF_SIZE);
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

	debug->i_private = inode->i_private;
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

static ssize_t
lpfc_cgn_buffer_read(struct file *file, char __user *buf, size_t nbytes,
		     loff_t *ppos)
{
	struct lpfc_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	char *buffer = debug->buffer;
	uint32_t *ptr;
	int cnt, len = 0;

	if (!phba->sli4_hba.pc_sli4_params.mi_ver || !phba->cgn_i) {
		len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
				 "Congestion Mgmt is not supported\n");
		goto out;
	}
	ptr = (uint32_t *)phba->cgn_i->virt;
	len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
			 "Congestion Buffer Header\n");
	/* Dump the first 32 bytes */
	cnt = 32;
	len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
			 "000: %08x %08x %08x %08x %08x %08x %08x %08x\n",
			 *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
			 *(ptr + 4), *(ptr + 5), *(ptr + 6), *(ptr + 7));
	ptr += 8;
	len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
			 "Congestion Buffer Data\n");
	while (cnt < sizeof(struct lpfc_cgn_info)) {
		if (len > (LPFC_CGN_BUF_SIZE - LPFC_DEBUG_OUT_LINE_SZ)) {
			len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
					 "Truncated . . .\n");
			break;
		}
		len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
				 "%03x: %08x %08x %08x %08x "
				 "%08x %08x %08x %08x\n",
				 cnt, *ptr, *(ptr + 1), *(ptr + 2),
				 *(ptr + 3), *(ptr + 4), *(ptr + 5),
				 *(ptr + 6), *(ptr + 7));
		cnt += 32;
		ptr += 8;
	}
out:
	return simple_read_from_buffer(buf, nbytes, ppos, buffer, len);
}

static int
lpfc_cgn_buffer_release(struct inode *inode, struct file *file)
{
	struct lpfc_debug *debug = file->private_data;

	vfree(debug->buffer);
	kfree(debug);

	return 0;
}

static int
lpfc_rx_monitor_open(struct inode *inode, struct file *file)
{
	struct lpfc_rx_monitor_debug *debug;
	int rc = -ENOMEM;

	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
	if (!debug)
		goto out;

	debug->buffer = vmalloc(MAX_DEBUGFS_RX_TABLE_SIZE);
	if (!debug->buffer) {
		kfree(debug);
		goto out;
	}

	debug->i_private = inode->i_private;
	file->private_data = debug;

	rc = 0;
out:
	return rc;
}

static ssize_t
lpfc_rx_monitor_read(struct file *file, char __user *buf, size_t nbytes,
		     loff_t *ppos)
{
	struct lpfc_rx_monitor_debug *debug = file->private_data;
	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
	char *buffer = debug->buffer;
	struct rxtable_entry *entry;
	int i, len = 0, head, tail, last, start;

	head = atomic_read(&phba->rxtable_idx_head);
	while (head == LPFC_RXMONITOR_TABLE_IN_USE) {
		/* Table is getting updated */
		msleep(20);
		head = atomic_read(&phba->rxtable_idx_head);
	}

	tail = atomic_xchg(&phba->rxtable_idx_tail, head);
	if (!phba->rxtable || head == tail) {
		len += scnprintf(buffer + len, MAX_DEBUGFS_RX_TABLE_SIZE - len,
				"Rxtable is empty\n");
		goto out;
	}
	last = (head > tail) ?  head : LPFC_MAX_RXMONITOR_ENTRY;
	start = tail;

	len += scnprintf(buffer + len, MAX_DEBUGFS_RX_TABLE_SIZE - len,
			"        MaxBPI\t Total Data Cmd  Total Data Cmpl "
			"  Latency(us)    Avg IO Size\tMax IO Size   IO cnt "
			"Info BWutil(ms)\n");
get_table:
	for (i = start; i < last; i++) {
		entry = &phba->rxtable[i];
		len += scnprintf(buffer + len, MAX_DEBUGFS_RX_TABLE_SIZE - len,
				"%3d:%12lld  %12lld\t%12lld\t"
				"%8lldus\t%8lld\t%10lld "
				"%8d   %2d %2d(%2d)\n",
				i, entry->max_bytes_per_interval,
				entry->total_bytes,
				entry->rcv_bytes,
				entry->avg_io_latency,
				entry->avg_io_size,
				entry->max_read_cnt,
				entry->io_cnt,
				entry->cmf_info,
				entry->timer_utilization,
				entry->timer_interval);
	}

	if (head != last) {
		start = 0;
		last = head;
		goto get_table;
	}
out:
	return simple_read_from_buffer(buf, nbytes, ppos, buffer, len);
}

static int
lpfc_rx_monitor_release(struct inode *inode, struct file *file)
{
	struct lpfc_rx_monitor_debug *debug = file->private_data;

	vfree(debug->buffer);
	kfree(debug);

	return 0;
}

5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623
#undef lpfc_debugfs_op_disc_trc
static const struct file_operations lpfc_debugfs_op_disc_trc = {
	.owner =        THIS_MODULE,
	.open =         lpfc_debugfs_disc_trc_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_debugfs_read,
	.release =      lpfc_debugfs_release,
};

#undef lpfc_debugfs_op_nodelist
static const struct file_operations lpfc_debugfs_op_nodelist = {
	.owner =        THIS_MODULE,
	.open =         lpfc_debugfs_nodelist_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_debugfs_read,
	.release =      lpfc_debugfs_release,
};

5624 5625 5626 5627 5628 5629 5630 5631 5632 5633
#undef lpfc_debugfs_op_multixripools
static const struct file_operations lpfc_debugfs_op_multixripools = {
	.owner =        THIS_MODULE,
	.open =         lpfc_debugfs_multixripools_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_debugfs_read,
	.write =	lpfc_debugfs_multixripools_write,
	.release =      lpfc_debugfs_release,
};

5634 5635 5636 5637 5638 5639 5640 5641 5642
#undef lpfc_debugfs_op_hbqinfo
static const struct file_operations lpfc_debugfs_op_hbqinfo = {
	.owner =        THIS_MODULE,
	.open =         lpfc_debugfs_hbqinfo_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_debugfs_read,
	.release =      lpfc_debugfs_release,
};

5643 5644 5645
#ifdef LPFC_HDWQ_LOCK_STAT
#undef lpfc_debugfs_op_lockstat
static const struct file_operations lpfc_debugfs_op_lockstat = {
5646
	.owner =        THIS_MODULE,
5647
	.open =         lpfc_debugfs_lockstat_open,
5648 5649
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_debugfs_read,
5650
	.write =        lpfc_debugfs_lockstat_write,
5651 5652
	.release =      lpfc_debugfs_release,
};
5653
#endif
5654

5655 5656 5657 5658 5659 5660 5661 5662 5663
#undef lpfc_debugfs_ras_log
static const struct file_operations lpfc_debugfs_ras_log = {
	.owner =        THIS_MODULE,
	.open =         lpfc_debugfs_ras_log_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_debugfs_read,
	.release =      lpfc_debugfs_ras_log_release,
};

5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681
#undef lpfc_debugfs_op_dumpHBASlim
static const struct file_operations lpfc_debugfs_op_dumpHBASlim = {
	.owner =        THIS_MODULE,
	.open =         lpfc_debugfs_dumpHBASlim_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_debugfs_read,
	.release =      lpfc_debugfs_release,
};

#undef lpfc_debugfs_op_dumpHostSlim
static const struct file_operations lpfc_debugfs_op_dumpHostSlim = {
	.owner =        THIS_MODULE,
	.open =         lpfc_debugfs_dumpHostSlim_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_debugfs_read,
	.release =      lpfc_debugfs_release,
};

5682 5683 5684 5685 5686 5687
#undef lpfc_debugfs_op_nvmestat
static const struct file_operations lpfc_debugfs_op_nvmestat = {
	.owner =        THIS_MODULE,
	.open =         lpfc_debugfs_nvmestat_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_debugfs_read,
5688
	.write =	lpfc_debugfs_nvmestat_write,
5689 5690 5691
	.release =      lpfc_debugfs_release,
};

5692 5693 5694 5695 5696 5697 5698 5699 5700 5701
#undef lpfc_debugfs_op_scsistat
static const struct file_operations lpfc_debugfs_op_scsistat = {
	.owner =        THIS_MODULE,
	.open =         lpfc_debugfs_scsistat_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_debugfs_read,
	.write =	lpfc_debugfs_scsistat_write,
	.release =      lpfc_debugfs_release,
};

5702 5703
#undef lpfc_debugfs_op_ioktime
static const struct file_operations lpfc_debugfs_op_ioktime = {
5704
	.owner =        THIS_MODULE,
5705
	.open =         lpfc_debugfs_ioktime_open,
5706 5707
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_debugfs_read,
5708
	.write =	lpfc_debugfs_ioktime_write,
5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721
	.release =      lpfc_debugfs_release,
};

#undef lpfc_debugfs_op_nvmeio_trc
static const struct file_operations lpfc_debugfs_op_nvmeio_trc = {
	.owner =        THIS_MODULE,
	.open =         lpfc_debugfs_nvmeio_trc_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_debugfs_read,
	.write =	lpfc_debugfs_nvmeio_trc_write,
	.release =      lpfc_debugfs_release,
};

5722 5723
#undef lpfc_debugfs_op_hdwqstat
static const struct file_operations lpfc_debugfs_op_hdwqstat = {
5724
	.owner =        THIS_MODULE,
5725
	.open =         lpfc_debugfs_hdwqstat_open,
5726 5727
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_debugfs_read,
5728
	.write =	lpfc_debugfs_hdwqstat_write,
5729 5730 5731
	.release =      lpfc_debugfs_release,
};

5732 5733 5734
#undef lpfc_debugfs_op_dif_err
static const struct file_operations lpfc_debugfs_op_dif_err = {
	.owner =	THIS_MODULE,
5735
	.open =		simple_open,
5736 5737 5738 5739 5740 5741
	.llseek =	lpfc_debugfs_lseek,
	.read =		lpfc_debugfs_dif_err_read,
	.write =	lpfc_debugfs_dif_err_write,
	.release =	lpfc_debugfs_dif_err_release,
};

5742
#undef lpfc_debugfs_op_slow_ring_trc
5743
static const struct file_operations lpfc_debugfs_op_slow_ring_trc = {
5744 5745 5746 5747 5748 5749 5750
	.owner =        THIS_MODULE,
	.open =         lpfc_debugfs_slow_ring_trc_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_debugfs_read,
	.release =      lpfc_debugfs_release,
};

J
James Smart 已提交
5751 5752
static struct dentry *lpfc_debugfs_root = NULL;
static atomic_t lpfc_debugfs_hba_count;
5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766

/*
 * File operations for the iDiag debugfs
 */
#undef lpfc_idiag_op_pciCfg
static const struct file_operations lpfc_idiag_op_pciCfg = {
	.owner =        THIS_MODULE,
	.open =         lpfc_idiag_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_idiag_pcicfg_read,
	.write =        lpfc_idiag_pcicfg_write,
	.release =      lpfc_idiag_cmd_release,
};

5767 5768 5769 5770 5771 5772 5773 5774 5775 5776
#undef lpfc_idiag_op_barAcc
static const struct file_operations lpfc_idiag_op_barAcc = {
	.owner =        THIS_MODULE,
	.open =         lpfc_idiag_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_idiag_baracc_read,
	.write =        lpfc_idiag_baracc_write,
	.release =      lpfc_idiag_cmd_release,
};

5777 5778 5779 5780 5781 5782 5783 5784
#undef lpfc_idiag_op_queInfo
static const struct file_operations lpfc_idiag_op_queInfo = {
	.owner =        THIS_MODULE,
	.open =         lpfc_idiag_open,
	.read =         lpfc_idiag_queinfo_read,
	.release =      lpfc_idiag_release,
};

5785
#undef lpfc_idiag_op_queAcc
5786 5787 5788 5789 5790 5791 5792 5793 5794
static const struct file_operations lpfc_idiag_op_queAcc = {
	.owner =        THIS_MODULE,
	.open =         lpfc_idiag_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_idiag_queacc_read,
	.write =        lpfc_idiag_queacc_write,
	.release =      lpfc_idiag_cmd_release,
};

5795
#undef lpfc_idiag_op_drbAcc
5796 5797 5798 5799 5800 5801 5802 5803 5804
static const struct file_operations lpfc_idiag_op_drbAcc = {
	.owner =        THIS_MODULE,
	.open =         lpfc_idiag_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_idiag_drbacc_read,
	.write =        lpfc_idiag_drbacc_write,
	.release =      lpfc_idiag_cmd_release,
};

5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833
#undef lpfc_idiag_op_ctlAcc
static const struct file_operations lpfc_idiag_op_ctlAcc = {
	.owner =        THIS_MODULE,
	.open =         lpfc_idiag_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_idiag_ctlacc_read,
	.write =        lpfc_idiag_ctlacc_write,
	.release =      lpfc_idiag_cmd_release,
};

#undef lpfc_idiag_op_mbxAcc
static const struct file_operations lpfc_idiag_op_mbxAcc = {
	.owner =        THIS_MODULE,
	.open =         lpfc_idiag_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_idiag_mbxacc_read,
	.write =        lpfc_idiag_mbxacc_write,
	.release =      lpfc_idiag_cmd_release,
};

#undef lpfc_idiag_op_extAcc
static const struct file_operations lpfc_idiag_op_extAcc = {
	.owner =        THIS_MODULE,
	.open =         lpfc_idiag_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_idiag_extacc_read,
	.write =        lpfc_idiag_extacc_write,
	.release =      lpfc_idiag_cmd_release,
};
5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850
#undef lpfc_cgn_buffer_op
static const struct file_operations lpfc_cgn_buffer_op = {
	.owner =        THIS_MODULE,
	.open =         lpfc_cgn_buffer_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_cgn_buffer_read,
	.release =      lpfc_cgn_buffer_release,
};

#undef lpfc_rx_monitor_op
static const struct file_operations lpfc_rx_monitor_op = {
	.owner =        THIS_MODULE,
	.open =         lpfc_rx_monitor_open,
	.llseek =       lpfc_debugfs_lseek,
	.read =         lpfc_rx_monitor_read,
	.release =      lpfc_rx_monitor_release,
};
5851
#endif
J
James Smart 已提交
5852

5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893
/* lpfc_idiag_mbxacc_dump_bsg_mbox - idiag debugfs dump bsg mailbox command
 * @phba: Pointer to HBA context object.
 * @dmabuf: Pointer to a DMA buffer descriptor.
 *
 * Description:
 * This routine dump a bsg pass-through non-embedded mailbox command with
 * external buffer.
 **/
void
lpfc_idiag_mbxacc_dump_bsg_mbox(struct lpfc_hba *phba, enum nemb_type nemb_tp,
				enum mbox_type mbox_tp, enum dma_type dma_tp,
				enum sta_type sta_tp,
				struct lpfc_dmabuf *dmabuf, uint32_t ext_buf)
{
#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
	uint32_t *mbx_mbox_cmd, *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt;
	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
	int len = 0;
	uint32_t do_dump = 0;
	uint32_t *pword;
	uint32_t i;

	if (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP)
		return;

	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];

	if (!(*mbx_dump_map & LPFC_MBX_DMP_ALL) ||
	    (*mbx_dump_cnt == 0) ||
	    (*mbx_word_cnt == 0))
		return;

	if (*mbx_mbox_cmd != 0x9B)
		return;

	if ((mbox_tp == mbox_rd) && (dma_tp == dma_mbox)) {
		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_MBX) {
			do_dump |= LPFC_BSG_DMP_MBX_RD_MBX;
J
James Smart 已提交
5894
			pr_err("\nRead mbox command (x%x), "
5895 5896 5897 5898 5899 5900 5901
			       "nemb:0x%x, extbuf_cnt:%d:\n",
			       sta_tp, nemb_tp, ext_buf);
		}
	}
	if ((mbox_tp == mbox_rd) && (dma_tp == dma_ebuf)) {
		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_BUF) {
			do_dump |= LPFC_BSG_DMP_MBX_RD_BUF;
J
James Smart 已提交
5902
			pr_err("\nRead mbox buffer (x%x), "
5903 5904 5905 5906 5907 5908 5909
			       "nemb:0x%x, extbuf_seq:%d:\n",
			       sta_tp, nemb_tp, ext_buf);
		}
	}
	if ((mbox_tp == mbox_wr) && (dma_tp == dma_mbox)) {
		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_MBX) {
			do_dump |= LPFC_BSG_DMP_MBX_WR_MBX;
J
James Smart 已提交
5910
			pr_err("\nWrite mbox command (x%x), "
5911 5912 5913 5914 5915 5916 5917
			       "nemb:0x%x, extbuf_cnt:%d:\n",
			       sta_tp, nemb_tp, ext_buf);
		}
	}
	if ((mbox_tp == mbox_wr) && (dma_tp == dma_ebuf)) {
		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_BUF) {
			do_dump |= LPFC_BSG_DMP_MBX_WR_BUF;
J
James Smart 已提交
5918
			pr_err("\nWrite mbox buffer (x%x), "
5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929
			       "nemb:0x%x, extbuf_seq:%d:\n",
			       sta_tp, nemb_tp, ext_buf);
		}
	}

	/* dump buffer content */
	if (do_dump) {
		pword = (uint32_t *)dmabuf->virt;
		for (i = 0; i < *mbx_word_cnt; i++) {
			if (!(i % 8)) {
				if (i != 0)
J
James Smart 已提交
5930
					pr_err("%s\n", line_buf);
5931
				len = 0;
5932
				len += scnprintf(line_buf+len,
5933 5934 5935
						LPFC_MBX_ACC_LBUF_SZ-len,
						"%03d: ", i);
			}
5936
			len += scnprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
5937 5938 5939 5940
					"%08x ", (uint32_t)*pword);
			pword++;
		}
		if ((i - 1) % 8)
J
James Smart 已提交
5941
			pr_err("%s\n", line_buf);
5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989
		(*mbx_dump_cnt)--;
	}

	/* Clean out command structure on reaching dump count */
	if (*mbx_dump_cnt == 0)
		memset(&idiag, 0, sizeof(idiag));
	return;
#endif
}

/* lpfc_idiag_mbxacc_dump_issue_mbox - idiag debugfs dump issue mailbox command
 * @phba: Pointer to HBA context object.
 * @dmabuf: Pointer to a DMA buffer descriptor.
 *
 * Description:
 * This routine dump a pass-through non-embedded mailbox command from issue
 * mailbox command.
 **/
void
lpfc_idiag_mbxacc_dump_issue_mbox(struct lpfc_hba *phba, MAILBOX_t *pmbox)
{
#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
	uint32_t *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt, *mbx_mbox_cmd;
	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
	int len = 0;
	uint32_t *pword;
	uint8_t *pbyte;
	uint32_t i, j;

	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP)
		return;

	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];

	if (!(*mbx_dump_map & LPFC_MBX_DMP_MBX_ALL) ||
	    (*mbx_dump_cnt == 0) ||
	    (*mbx_word_cnt == 0))
		return;

	if ((*mbx_mbox_cmd != LPFC_MBX_ALL_CMD) &&
	    (*mbx_mbox_cmd != pmbox->mbxCommand))
		return;

	/* dump buffer content */
	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_WORD) {
J
James Smart 已提交
5990
		pr_err("Mailbox command:0x%x dump by word:\n",
5991 5992 5993 5994 5995
		       pmbox->mbxCommand);
		pword = (uint32_t *)pmbox;
		for (i = 0; i < *mbx_word_cnt; i++) {
			if (!(i % 8)) {
				if (i != 0)
J
James Smart 已提交
5996
					pr_err("%s\n", line_buf);
5997 5998
				len = 0;
				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
5999
				len += scnprintf(line_buf+len,
6000 6001 6002
						LPFC_MBX_ACC_LBUF_SZ-len,
						"%03d: ", i);
			}
6003
			len += scnprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
6004 6005 6006 6007 6008
					"%08x ",
					((uint32_t)*pword) & 0xffffffff);
			pword++;
		}
		if ((i - 1) % 8)
J
James Smart 已提交
6009 6010
			pr_err("%s\n", line_buf);
		pr_err("\n");
6011 6012
	}
	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_BYTE) {
J
James Smart 已提交
6013
		pr_err("Mailbox command:0x%x dump by byte:\n",
6014 6015 6016 6017 6018
		       pmbox->mbxCommand);
		pbyte = (uint8_t *)pmbox;
		for (i = 0; i < *mbx_word_cnt; i++) {
			if (!(i % 8)) {
				if (i != 0)
J
James Smart 已提交
6019
					pr_err("%s\n", line_buf);
6020 6021
				len = 0;
				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
6022
				len += scnprintf(line_buf+len,
6023 6024 6025 6026
						LPFC_MBX_ACC_LBUF_SZ-len,
						"%03d: ", i);
			}
			for (j = 0; j < 4; j++) {
6027
				len += scnprintf(line_buf+len,
6028 6029 6030 6031 6032
						LPFC_MBX_ACC_LBUF_SZ-len,
						"%02x",
						((uint8_t)*pbyte) & 0xff);
				pbyte++;
			}
6033
			len += scnprintf(line_buf+len,
6034 6035 6036
					LPFC_MBX_ACC_LBUF_SZ-len, " ");
		}
		if ((i - 1) % 8)
J
James Smart 已提交
6037 6038
			pr_err("%s\n", line_buf);
		pr_err("\n");
6039 6040 6041 6042 6043 6044 6045 6046 6047 6048
	}
	(*mbx_dump_cnt)--;

	/* Clean out command structure on reaching dump count */
	if (*mbx_dump_cnt == 0)
		memset(&idiag, 0, sizeof(idiag));
	return;
#endif
}

6049
/**
6050
 * lpfc_debugfs_initialize - Initialize debugfs for a vport
6051 6052 6053 6054 6055 6056 6057 6058
 * @vport: The vport pointer to initialize.
 *
 * Description:
 * When Debugfs is configured this routine sets up the lpfc debugfs file system.
 * If not already created, this routine will create the lpfc directory, and
 * lpfcX directory (for this HBA), and vportX directory for this vport. It will
 * also create each file used to access lpfc specific debugfs information.
 **/
J
James Smart 已提交
6059 6060 6061
inline void
lpfc_debugfs_initialize(struct lpfc_vport *vport)
{
6062
#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
J
James Smart 已提交
6063 6064 6065
	struct lpfc_hba   *phba = vport->phba;
	char name[64];
	uint32_t num, i;
6066
	bool pport_setup = false;
J
James Smart 已提交
6067 6068 6069 6070

	if (!lpfc_debugfs_enable)
		return;

6071
	/* Setup lpfc root directory */
J
James Smart 已提交
6072 6073 6074 6075
	if (!lpfc_debugfs_root) {
		lpfc_debugfs_root = debugfs_create_dir("lpfc", NULL);
		atomic_set(&lpfc_debugfs_hba_count, 0);
	}
6076 6077 6078
	if (!lpfc_debugfs_start_time)
		lpfc_debugfs_start_time = jiffies;

6079 6080
	/* Setup funcX directory for specific HBA PCI function */
	snprintf(name, sizeof(name), "fn%d", phba->brd_no);
J
James Smart 已提交
6081
	if (!phba->hba_debugfs_root) {
6082
		pport_setup = true;
J
James Smart 已提交
6083 6084 6085 6086
		phba->hba_debugfs_root =
			debugfs_create_dir(name, lpfc_debugfs_root);
		atomic_inc(&lpfc_debugfs_hba_count);
		atomic_set(&phba->debugfs_vport_count, 0);
6087

6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100
		/* Multi-XRI pools */
		snprintf(name, sizeof(name), "multixripools");
		phba->debug_multixri_pools =
			debugfs_create_file(name, S_IFREG | 0644,
					    phba->hba_debugfs_root,
					    phba,
					    &lpfc_debugfs_op_multixripools);
		if (!phba->debug_multixri_pools) {
			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
					 "0527 Cannot create debugfs multixripools\n");
			goto debug_failed;
		}

6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126
		/* Congestion Info Buffer */
		scnprintf(name, sizeof(name), "cgn_buffer");
		phba->debug_cgn_buffer =
			debugfs_create_file(name, S_IFREG | 0644,
					    phba->hba_debugfs_root,
					    phba, &lpfc_cgn_buffer_op);
		if (!phba->debug_cgn_buffer) {
			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
					 "6527 Cannot create debugfs "
					 "cgn_buffer\n");
			goto debug_failed;
		}

		/* RX Monitor */
		scnprintf(name, sizeof(name), "rx_monitor");
		phba->debug_rx_monitor =
			debugfs_create_file(name, S_IFREG | 0644,
					    phba->hba_debugfs_root,
					    phba, &lpfc_rx_monitor_op);
		if (!phba->debug_rx_monitor) {
			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
					 "6528 Cannot create debugfs "
					 "rx_monitor\n");
			goto debug_failed;
		}

6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139
		/* RAS log */
		snprintf(name, sizeof(name), "ras_log");
		phba->debug_ras_log =
			debugfs_create_file(name, 0644,
					    phba->hba_debugfs_root,
					    phba, &lpfc_debugfs_ras_log);
		if (!phba->debug_ras_log) {
			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
					 "6148 Cannot create debugfs"
					 " ras_log\n");
			goto debug_failed;
		}

6140 6141 6142
		/* Setup hbqinfo */
		snprintf(name, sizeof(name), "hbqinfo");
		phba->debug_hbqinfo =
6143 6144 6145
			debugfs_create_file(name, S_IFREG | 0644,
					    phba->hba_debugfs_root,
					    phba, &lpfc_debugfs_op_hbqinfo);
6146

6147 6148 6149 6150
#ifdef LPFC_HDWQ_LOCK_STAT
		/* Setup lockstat */
		snprintf(name, sizeof(name), "lockstat");
		phba->debug_lockstat =
6151 6152
			debugfs_create_file(name, S_IFREG | 0644,
					    phba->hba_debugfs_root,
6153 6154
					    phba, &lpfc_debugfs_op_lockstat);
		if (!phba->debug_lockstat) {
6155
			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6156
					 "4610 Can't create debugfs lockstat\n");
6157 6158
			goto debug_failed;
		}
6159
#endif
6160

6161
		/* Setup dumpHBASlim */
6162 6163 6164 6165 6166 6167 6168 6169 6170
		if (phba->sli_rev < LPFC_SLI_REV4) {
			snprintf(name, sizeof(name), "dumpHBASlim");
			phba->debug_dumpHBASlim =
				debugfs_create_file(name,
					S_IFREG|S_IRUGO|S_IWUSR,
					phba->hba_debugfs_root,
					phba, &lpfc_debugfs_op_dumpHBASlim);
		} else
			phba->debug_dumpHBASlim = NULL;
6171 6172

		/* Setup dumpHostSlim */
6173 6174 6175 6176 6177 6178 6179 6180
		if (phba->sli_rev < LPFC_SLI_REV4) {
			snprintf(name, sizeof(name), "dumpHostSlim");
			phba->debug_dumpHostSlim =
				debugfs_create_file(name,
					S_IFREG|S_IRUGO|S_IWUSR,
					phba->hba_debugfs_root,
					phba, &lpfc_debugfs_op_dumpHostSlim);
		} else
6181
			phba->debug_dumpHostSlim = NULL;
6182

6183 6184 6185 6186 6187 6188 6189 6190
		/* Setup DIF Error Injections */
		snprintf(name, sizeof(name), "InjErrLBA");
		phba->debug_InjErrLBA =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
			phba->hba_debugfs_root,
			phba, &lpfc_debugfs_op_dif_err);
		phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;

6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202
		snprintf(name, sizeof(name), "InjErrNPortID");
		phba->debug_InjErrNPortID =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
			phba->hba_debugfs_root,
			phba, &lpfc_debugfs_op_dif_err);

		snprintf(name, sizeof(name), "InjErrWWPN");
		phba->debug_InjErrWWPN =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
			phba->hba_debugfs_root,
			phba, &lpfc_debugfs_op_dif_err);

6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220
		snprintf(name, sizeof(name), "writeGuardInjErr");
		phba->debug_writeGuard =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
			phba->hba_debugfs_root,
			phba, &lpfc_debugfs_op_dif_err);

		snprintf(name, sizeof(name), "writeAppInjErr");
		phba->debug_writeApp =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
			phba->hba_debugfs_root,
			phba, &lpfc_debugfs_op_dif_err);

		snprintf(name, sizeof(name), "writeRefInjErr");
		phba->debug_writeRef =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
			phba->hba_debugfs_root,
			phba, &lpfc_debugfs_op_dif_err);

6221 6222 6223 6224 6225 6226
		snprintf(name, sizeof(name), "readGuardInjErr");
		phba->debug_readGuard =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
			phba->hba_debugfs_root,
			phba, &lpfc_debugfs_op_dif_err);

6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238
		snprintf(name, sizeof(name), "readAppInjErr");
		phba->debug_readApp =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
			phba->hba_debugfs_root,
			phba, &lpfc_debugfs_op_dif_err);

		snprintf(name, sizeof(name), "readRefInjErr");
		phba->debug_readRef =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
			phba->hba_debugfs_root,
			phba, &lpfc_debugfs_op_dif_err);

6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250
		/* Setup slow ring trace */
		if (lpfc_debugfs_max_slow_ring_trc) {
			num = lpfc_debugfs_max_slow_ring_trc - 1;
			if (num & lpfc_debugfs_max_slow_ring_trc) {
				/* Change to be a power of 2 */
				num = lpfc_debugfs_max_slow_ring_trc;
				i = 0;
				while (num > 1) {
					num = num >> 1;
					i++;
				}
				lpfc_debugfs_max_slow_ring_trc = (1 << i);
J
James Smart 已提交
6251
				pr_err("lpfc_debugfs_max_disc_trc changed to "
6252
				       "%d\n", lpfc_debugfs_max_disc_trc);
6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266
			}
		}

		snprintf(name, sizeof(name), "slow_ring_trace");
		phba->debug_slow_ring_trc =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
				 phba->hba_debugfs_root,
				 phba, &lpfc_debugfs_op_slow_ring_trc);
		if (!phba->slow_ring_trc) {
			phba->slow_ring_trc = kmalloc(
				(sizeof(struct lpfc_debugfs_trc) *
				lpfc_debugfs_max_slow_ring_trc),
				GFP_KERNEL);
			if (!phba->slow_ring_trc) {
6267
				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6268
						 "0416 Cannot create debugfs "
6269
						 "slow_ring buffer\n");
6270 6271 6272 6273 6274 6275 6276
				goto debug_failed;
			}
			atomic_set(&phba->slow_ring_trc_cnt, 0);
			memset(phba->slow_ring_trc, 0,
				(sizeof(struct lpfc_debugfs_trc) *
				lpfc_debugfs_max_slow_ring_trc));
		}
J
James Smart 已提交
6277

6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303
		snprintf(name, sizeof(name), "nvmeio_trc");
		phba->debug_nvmeio_trc =
			debugfs_create_file(name, 0644,
					    phba->hba_debugfs_root,
					    phba, &lpfc_debugfs_op_nvmeio_trc);

		atomic_set(&phba->nvmeio_trc_cnt, 0);
		if (lpfc_debugfs_max_nvmeio_trc) {
			num = lpfc_debugfs_max_nvmeio_trc - 1;
			if (num & lpfc_debugfs_max_disc_trc) {
				/* Change to be a power of 2 */
				num = lpfc_debugfs_max_nvmeio_trc;
				i = 0;
				while (num > 1) {
					num = num >> 1;
					i++;
				}
				lpfc_debugfs_max_nvmeio_trc = (1 << i);
				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
						"0575 lpfc_debugfs_max_nvmeio_trc "
						"changed to %d\n",
						lpfc_debugfs_max_nvmeio_trc);
			}
			phba->nvmeio_trc_size = lpfc_debugfs_max_nvmeio_trc;

			/* Allocate trace buffer and initialize */
6304
			phba->nvmeio_trc = kzalloc(
6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323
				(sizeof(struct lpfc_debugfs_nvmeio_trc) *
				phba->nvmeio_trc_size), GFP_KERNEL);

			if (!phba->nvmeio_trc) {
				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
						"0576 Cannot create debugfs "
						"nvmeio_trc buffer\n");
				goto nvmeio_off;
			}
			phba->nvmeio_trc_on = 1;
			phba->nvmeio_trc_output_idx = 0;
			phba->nvmeio_trc = NULL;
		} else {
nvmeio_off:
			phba->nvmeio_trc_size = 0;
			phba->nvmeio_trc_on = 0;
			phba->nvmeio_trc_output_idx = 0;
			phba->nvmeio_trc = NULL;
		}
J
James Smart 已提交
6324 6325 6326 6327 6328 6329 6330 6331 6332
	}

	snprintf(name, sizeof(name), "vport%d", vport->vpi);
	if (!vport->vport_debugfs_root) {
		vport->vport_debugfs_root =
			debugfs_create_dir(name, phba->hba_debugfs_root);
		atomic_inc(&phba->debugfs_vport_count);
	}

6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343
	if (lpfc_debugfs_max_disc_trc) {
		num = lpfc_debugfs_max_disc_trc - 1;
		if (num & lpfc_debugfs_max_disc_trc) {
			/* Change to be a power of 2 */
			num = lpfc_debugfs_max_disc_trc;
			i = 0;
			while (num > 1) {
				num = num >> 1;
				i++;
			}
			lpfc_debugfs_max_disc_trc = (1 << i);
J
James Smart 已提交
6344
			pr_err("lpfc_debugfs_max_disc_trc changed to %d\n",
6345
			       lpfc_debugfs_max_disc_trc);
6346 6347
		}
	}
J
James Smart 已提交
6348

6349
	vport->disc_trc = kzalloc(
6350
		(sizeof(struct lpfc_debugfs_trc) * lpfc_debugfs_max_disc_trc),
J
James Smart 已提交
6351 6352
		GFP_KERNEL);

6353
	if (!vport->disc_trc) {
6354
		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6355
				 "0418 Cannot create debugfs disc trace "
6356
				 "buffer\n");
J
James Smart 已提交
6357
		goto debug_failed;
6358 6359
	}
	atomic_set(&vport->disc_trc_cnt, 0);
J
James Smart 已提交
6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370

	snprintf(name, sizeof(name), "discovery_trace");
	vport->debug_disc_trc =
		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
				 vport->vport_debugfs_root,
				 vport, &lpfc_debugfs_op_disc_trc);
	snprintf(name, sizeof(name), "nodelist");
	vport->debug_nodelist =
		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
				 vport->vport_debugfs_root,
				 vport, &lpfc_debugfs_op_nodelist);
6371

6372 6373 6374 6375 6376 6377
	snprintf(name, sizeof(name), "nvmestat");
	vport->debug_nvmestat =
		debugfs_create_file(name, 0644,
				    vport->vport_debugfs_root,
				    vport, &lpfc_debugfs_op_nvmestat);

6378 6379 6380 6381 6382 6383 6384
	snprintf(name, sizeof(name), "scsistat");
	vport->debug_scsistat =
		debugfs_create_file(name, 0644,
				    vport->vport_debugfs_root,
				    vport, &lpfc_debugfs_op_scsistat);
	if (!vport->debug_scsistat) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6385
				 "4611 Cannot create debugfs scsistat\n");
6386 6387 6388
		goto debug_failed;
	}

6389 6390
	snprintf(name, sizeof(name), "ioktime");
	vport->debug_ioktime =
6391 6392
		debugfs_create_file(name, 0644,
				    vport->vport_debugfs_root,
6393 6394 6395 6396 6397 6398
				    vport, &lpfc_debugfs_op_ioktime);
	if (!vport->debug_ioktime) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
				 "0815 Cannot create debugfs ioktime\n");
		goto debug_failed;
	}
6399

6400 6401
	snprintf(name, sizeof(name), "hdwqstat");
	vport->debug_hdwqstat =
6402 6403
		debugfs_create_file(name, 0644,
				    vport->vport_debugfs_root,
6404
				    vport, &lpfc_debugfs_op_hdwqstat);
6405

6406 6407 6408 6409 6410 6411 6412 6413
	/*
	 * The following section is for additional directories/files for the
	 * physical port.
	 */

	if (!pport_setup)
		goto debug_failed;

6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436
	/*
	 * iDiag debugfs root entry points for SLI4 device only
	 */
	if (phba->sli_rev < LPFC_SLI_REV4)
		goto debug_failed;

	snprintf(name, sizeof(name), "iDiag");
	if (!phba->idiag_root) {
		phba->idiag_root =
			debugfs_create_dir(name, phba->hba_debugfs_root);
		/* Initialize iDiag data structure */
		memset(&idiag, 0, sizeof(idiag));
	}

	/* iDiag read PCI config space */
	snprintf(name, sizeof(name), "pciCfg");
	if (!phba->idiag_pci_cfg) {
		phba->idiag_pci_cfg =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
				phba->idiag_root, phba, &lpfc_idiag_op_pciCfg);
		idiag.offset.last_rd = 0;
	}

6437 6438 6439 6440 6441 6442 6443 6444 6445
	/* iDiag PCI BAR access */
	snprintf(name, sizeof(name), "barAcc");
	if (!phba->idiag_bar_acc) {
		phba->idiag_bar_acc =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
				phba->idiag_root, phba, &lpfc_idiag_op_barAcc);
		idiag.offset.last_rd = 0;
	}

6446 6447 6448 6449 6450 6451 6452 6453
	/* iDiag get PCI function queue information */
	snprintf(name, sizeof(name), "queInfo");
	if (!phba->idiag_que_info) {
		phba->idiag_que_info =
			debugfs_create_file(name, S_IFREG|S_IRUGO,
			phba->idiag_root, phba, &lpfc_idiag_op_queInfo);
	}

6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469
	/* iDiag access PCI function queue */
	snprintf(name, sizeof(name), "queAcc");
	if (!phba->idiag_que_acc) {
		phba->idiag_que_acc =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
				phba->idiag_root, phba, &lpfc_idiag_op_queAcc);
	}

	/* iDiag access PCI function doorbell registers */
	snprintf(name, sizeof(name), "drbAcc");
	if (!phba->idiag_drb_acc) {
		phba->idiag_drb_acc =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
				phba->idiag_root, phba, &lpfc_idiag_op_drbAcc);
	}

6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497
	/* iDiag access PCI function control registers */
	snprintf(name, sizeof(name), "ctlAcc");
	if (!phba->idiag_ctl_acc) {
		phba->idiag_ctl_acc =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
				phba->idiag_root, phba, &lpfc_idiag_op_ctlAcc);
	}

	/* iDiag access mbox commands */
	snprintf(name, sizeof(name), "mbxAcc");
	if (!phba->idiag_mbx_acc) {
		phba->idiag_mbx_acc =
			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
				phba->idiag_root, phba, &lpfc_idiag_op_mbxAcc);
	}

	/* iDiag extents access commands */
	if (phba->sli4_hba.extents_in_use) {
		snprintf(name, sizeof(name), "extAcc");
		if (!phba->idiag_ext_acc) {
			phba->idiag_ext_acc =
				debugfs_create_file(name,
						    S_IFREG|S_IRUGO|S_IWUSR,
						    phba->idiag_root, phba,
						    &lpfc_idiag_op_extAcc);
		}
	}

J
James Smart 已提交
6498 6499 6500 6501 6502
debug_failed:
	return;
#endif
}

6503
/**
6504
 * lpfc_debugfs_terminate -  Tear down debugfs infrastructure for this vport
6505 6506 6507 6508 6509 6510 6511 6512 6513
 * @vport: The vport pointer to remove from debugfs.
 *
 * Description:
 * When Debugfs is configured this routine removes debugfs file system elements
 * that are specific to this vport. It also checks to see if there are any
 * users left for the debugfs directories associated with the HBA and driver. If
 * this is the last user of the HBA directory or driver directory then it will
 * remove those from the debugfs infrastructure as well.
 **/
J
James Smart 已提交
6514 6515 6516
inline void
lpfc_debugfs_terminate(struct lpfc_vport *vport)
{
6517
#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
J
James Smart 已提交
6518 6519
	struct lpfc_hba   *phba = vport->phba;

6520 6521
	kfree(vport->disc_trc);
	vport->disc_trc = NULL;
6522 6523 6524 6525 6526 6527 6528 6529 6530 6531

	debugfs_remove(vport->debug_disc_trc); /* discovery_trace */
	vport->debug_disc_trc = NULL;

	debugfs_remove(vport->debug_nodelist); /* nodelist */
	vport->debug_nodelist = NULL;

	debugfs_remove(vport->debug_nvmestat); /* nvmestat */
	vport->debug_nvmestat = NULL;

6532 6533 6534
	debugfs_remove(vport->debug_scsistat); /* scsistat */
	vport->debug_scsistat = NULL;

6535 6536
	debugfs_remove(vport->debug_ioktime); /* ioktime */
	vport->debug_ioktime = NULL;
6537

6538 6539
	debugfs_remove(vport->debug_hdwqstat); /* hdwqstat */
	vport->debug_hdwqstat = NULL;
6540

J
James Smart 已提交
6541 6542 6543 6544 6545
	if (vport->vport_debugfs_root) {
		debugfs_remove(vport->vport_debugfs_root); /* vportX */
		vport->vport_debugfs_root = NULL;
		atomic_dec(&phba->debugfs_vport_count);
	}
6546

J
James Smart 已提交
6547
	if (atomic_read(&phba->debugfs_vport_count) == 0) {
6548

6549 6550 6551
		debugfs_remove(phba->debug_multixri_pools); /* multixripools*/
		phba->debug_multixri_pools = NULL;

6552 6553
		debugfs_remove(phba->debug_hbqinfo); /* hbqinfo */
		phba->debug_hbqinfo = NULL;
6554

6555 6556 6557 6558 6559 6560
		debugfs_remove(phba->debug_cgn_buffer);
		phba->debug_cgn_buffer = NULL;

		debugfs_remove(phba->debug_rx_monitor);
		phba->debug_rx_monitor = NULL;

6561 6562 6563
		debugfs_remove(phba->debug_ras_log);
		phba->debug_ras_log = NULL;

6564 6565 6566 6567
#ifdef LPFC_HDWQ_LOCK_STAT
		debugfs_remove(phba->debug_lockstat); /* lockstat */
		phba->debug_lockstat = NULL;
#endif
6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599
		debugfs_remove(phba->debug_dumpHBASlim); /* HBASlim */
		phba->debug_dumpHBASlim = NULL;

		debugfs_remove(phba->debug_dumpHostSlim); /* HostSlim */
		phba->debug_dumpHostSlim = NULL;

		debugfs_remove(phba->debug_InjErrLBA); /* InjErrLBA */
		phba->debug_InjErrLBA = NULL;

		debugfs_remove(phba->debug_InjErrNPortID);
		phba->debug_InjErrNPortID = NULL;

		debugfs_remove(phba->debug_InjErrWWPN); /* InjErrWWPN */
		phba->debug_InjErrWWPN = NULL;

		debugfs_remove(phba->debug_writeGuard); /* writeGuard */
		phba->debug_writeGuard = NULL;

		debugfs_remove(phba->debug_writeApp); /* writeApp */
		phba->debug_writeApp = NULL;

		debugfs_remove(phba->debug_writeRef); /* writeRef */
		phba->debug_writeRef = NULL;

		debugfs_remove(phba->debug_readGuard); /* readGuard */
		phba->debug_readGuard = NULL;

		debugfs_remove(phba->debug_readApp); /* readApp */
		phba->debug_readApp = NULL;

		debugfs_remove(phba->debug_readRef); /* readRef */
		phba->debug_readRef = NULL;
6600

6601 6602
		kfree(phba->slow_ring_trc);
		phba->slow_ring_trc = NULL;
6603 6604 6605 6606 6607 6608 6609 6610 6611 6612

		/* slow_ring_trace */
		debugfs_remove(phba->debug_slow_ring_trc);
		phba->debug_slow_ring_trc = NULL;

		debugfs_remove(phba->debug_nvmeio_trc);
		phba->debug_nvmeio_trc = NULL;

		kfree(phba->nvmeio_trc);
		phba->nvmeio_trc = NULL;
6613

6614 6615 6616 6617
		/*
		 * iDiag release
		 */
		if (phba->sli_rev == LPFC_SLI_REV4) {
6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648
			/* iDiag extAcc */
			debugfs_remove(phba->idiag_ext_acc);
			phba->idiag_ext_acc = NULL;

			/* iDiag mbxAcc */
			debugfs_remove(phba->idiag_mbx_acc);
			phba->idiag_mbx_acc = NULL;

			/* iDiag ctlAcc */
			debugfs_remove(phba->idiag_ctl_acc);
			phba->idiag_ctl_acc = NULL;

			/* iDiag drbAcc */
			debugfs_remove(phba->idiag_drb_acc);
			phba->idiag_drb_acc = NULL;

			/* iDiag queAcc */
			debugfs_remove(phba->idiag_que_acc);
			phba->idiag_que_acc = NULL;

			/* iDiag queInfo */
			debugfs_remove(phba->idiag_que_info);
			phba->idiag_que_info = NULL;

			/* iDiag barAcc */
			debugfs_remove(phba->idiag_bar_acc);
			phba->idiag_bar_acc = NULL;

			/* iDiag pciCfg */
			debugfs_remove(phba->idiag_pci_cfg);
			phba->idiag_pci_cfg = NULL;
6649 6650

			/* Finally remove the iDiag debugfs root */
6651 6652
			debugfs_remove(phba->idiag_root);
			phba->idiag_root = NULL;
6653 6654
		}

6655
		if (phba->hba_debugfs_root) {
6656
			debugfs_remove(phba->hba_debugfs_root); /* fnX */
6657 6658 6659 6660
			phba->hba_debugfs_root = NULL;
			atomic_dec(&lpfc_debugfs_hba_count);
		}

6661 6662 6663 6664
		if (atomic_read(&lpfc_debugfs_hba_count) == 0) {
			debugfs_remove(lpfc_debugfs_root); /* lpfc */
			lpfc_debugfs_root = NULL;
		}
J
James Smart 已提交
6665 6666
	}
#endif
6667
	return;
J
James Smart 已提交
6668
}
6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684

/*
 * Driver debug utility routines outside of debugfs. The debug utility
 * routines implemented here is intended to be used in the instrumented
 * debug driver for debugging host or port issues.
 */

/**
 * lpfc_debug_dump_all_queues - dump all the queues with a hba
 * @phba: Pointer to HBA context object.
 *
 * This function dumps entries of all the queues asociated with the @phba.
 **/
void
lpfc_debug_dump_all_queues(struct lpfc_hba *phba)
{
6685
	int idx;
6686 6687 6688 6689

	/*
	 * Dump Work Queues (WQs)
	 */
6690 6691
	lpfc_debug_dump_wq(phba, DUMP_MBX, 0);
	lpfc_debug_dump_wq(phba, DUMP_ELS, 0);
6692
	lpfc_debug_dump_wq(phba, DUMP_NVMELS, 0);
6693

6694
	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6695
		lpfc_debug_dump_wq(phba, DUMP_IO, idx);
6696

6697 6698 6699 6700 6701
	lpfc_debug_dump_hdr_rq(phba);
	lpfc_debug_dump_dat_rq(phba);
	/*
	 * Dump Complete Queues (CQs)
	 */
6702 6703
	lpfc_debug_dump_cq(phba, DUMP_MBX, 0);
	lpfc_debug_dump_cq(phba, DUMP_ELS, 0);
6704
	lpfc_debug_dump_cq(phba, DUMP_NVMELS, 0);
6705

6706
	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6707
		lpfc_debug_dump_cq(phba, DUMP_IO, idx);
6708

6709 6710 6711
	/*
	 * Dump Event Queues (EQs)
	 */
6712
	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6713
		lpfc_debug_dump_hba_eq(phba, idx);
6714
}