Linux Audio

Check our new training course

Loading...
v6.8
   1/*******************************************************************
   2 * This file is part of the Emulex Linux Device Driver for         *
   3 * Fibre Channel Host Bus Adapters.                                *
   4 * Copyright (C) 2017-2023 Broadcom. All Rights Reserved. The term *
   5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  *
   6 * Copyright (C) 2007-2015 Emulex.  All rights reserved.           *
   7 * EMULEX and SLI are trademarks of Emulex.                        *
   8 * www.broadcom.com                                                *
   9 *                                                                 *
  10 * This program is free software; you can redistribute it and/or   *
  11 * modify it under the terms of version 2 of the GNU General       *
  12 * Public License as published by the Free Software Foundation.    *
  13 * This program is distributed in the hope that it will be useful. *
  14 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
  15 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
  16 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
  17 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  18 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
  19 * more details, a copy of which can be found in the file COPYING  *
  20 * included with this package.                                     *
  21 *******************************************************************/
  22
  23#include <linux/blkdev.h>
  24#include <linux/delay.h>
  25#include <linux/module.h>
  26#include <linux/dma-mapping.h>
  27#include <linux/idr.h>
  28#include <linux/interrupt.h>
  29#include <linux/kthread.h>
  30#include <linux/slab.h>
  31#include <linux/pci.h>
  32#include <linux/spinlock.h>
  33#include <linux/ctype.h>
  34#include <linux/vmalloc.h>
  35
  36#include <scsi/scsi.h>
  37#include <scsi/scsi_device.h>
  38#include <scsi/scsi_host.h>
  39#include <scsi/scsi_transport_fc.h>
  40#include <scsi/fc/fc_fs.h>
  41
 
 
  42#include "lpfc_hw4.h"
  43#include "lpfc_hw.h"
  44#include "lpfc_sli.h"
  45#include "lpfc_sli4.h"
  46#include "lpfc_nl.h"
  47#include "lpfc_disc.h"
  48#include "lpfc.h"
  49#include "lpfc_scsi.h"
  50#include "lpfc_nvme.h"
 
  51#include "lpfc_logmsg.h"
  52#include "lpfc_crtn.h"
  53#include "lpfc_vport.h"
  54#include "lpfc_version.h"
  55#include "lpfc_compat.h"
  56#include "lpfc_debugfs.h"
  57#include "lpfc_bsg.h"
  58
  59#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  60/*
  61 * debugfs interface
  62 *
  63 * To access this interface the user should:
  64 * # mount -t debugfs none /sys/kernel/debug
  65 *
  66 * The lpfc debugfs directory hierarchy is:
  67 * /sys/kernel/debug/lpfc/fnX/vportY
  68 * where X is the lpfc hba function unique_id
  69 * where Y is the vport VPI on that hba
  70 *
  71 * Debugging services available per vport:
  72 * discovery_trace
  73 * This is an ACSII readable file that contains a trace of the last
  74 * lpfc_debugfs_max_disc_trc events that happened on a specific vport.
  75 * See lpfc_debugfs.h for different categories of  discovery events.
  76 * To enable the discovery trace, the following module parameters must be set:
  77 * lpfc_debugfs_enable=1         Turns on lpfc debugfs filesystem support
  78 * lpfc_debugfs_max_disc_trc=X   Where X is the event trace depth for
  79 *                               EACH vport. X MUST also be a power of 2.
  80 * lpfc_debugfs_mask_disc_trc=Y  Where Y is an event mask as defined in
  81 *                               lpfc_debugfs.h .
  82 *
  83 * slow_ring_trace
  84 * This is an ACSII readable file that contains a trace of the last
  85 * lpfc_debugfs_max_slow_ring_trc events that happened on a specific HBA.
  86 * To enable the slow ring trace, the following module parameters must be set:
  87 * lpfc_debugfs_enable=1         Turns on lpfc debugfs filesystem support
  88 * lpfc_debugfs_max_slow_ring_trc=X   Where X is the event trace depth for
  89 *                               the HBA. X MUST also be a power of 2.
  90 */
  91static int lpfc_debugfs_enable = 1;
  92module_param(lpfc_debugfs_enable, int, S_IRUGO);
  93MODULE_PARM_DESC(lpfc_debugfs_enable, "Enable debugfs services");
  94
  95/* This MUST be a power of 2 */
  96static int lpfc_debugfs_max_disc_trc;
  97module_param(lpfc_debugfs_max_disc_trc, int, S_IRUGO);
  98MODULE_PARM_DESC(lpfc_debugfs_max_disc_trc,
  99	"Set debugfs discovery trace depth");
 100
 101/* This MUST be a power of 2 */
 102static int lpfc_debugfs_max_slow_ring_trc;
 103module_param(lpfc_debugfs_max_slow_ring_trc, int, S_IRUGO);
 104MODULE_PARM_DESC(lpfc_debugfs_max_slow_ring_trc,
 105	"Set debugfs slow ring trace depth");
 106
 107/* This MUST be a power of 2 */
 108static int lpfc_debugfs_max_nvmeio_trc;
 109module_param(lpfc_debugfs_max_nvmeio_trc, int, 0444);
 110MODULE_PARM_DESC(lpfc_debugfs_max_nvmeio_trc,
 111		 "Set debugfs NVME IO trace depth");
 112
 113static int lpfc_debugfs_mask_disc_trc;
 114module_param(lpfc_debugfs_mask_disc_trc, int, S_IRUGO);
 115MODULE_PARM_DESC(lpfc_debugfs_mask_disc_trc,
 116	"Set debugfs discovery trace mask");
 117
 118#include <linux/debugfs.h>
 119
 120static atomic_t lpfc_debugfs_seq_trc_cnt = ATOMIC_INIT(0);
 121static unsigned long lpfc_debugfs_start_time = 0L;
 122
 123/* iDiag */
 124static struct lpfc_idiag idiag;
 125
 126/**
 127 * lpfc_debugfs_disc_trc_data - Dump discovery logging to a buffer
 128 * @vport: The vport to gather the log info from.
 129 * @buf: The buffer to dump log into.
 130 * @size: The maximum amount of data to process.
 131 *
 132 * Description:
 133 * This routine gathers the lpfc discovery debugfs data from the @vport and
 134 * dumps it to @buf up to @size number of bytes. It will start at the next entry
 135 * in the log and process the log until the end of the buffer. Then it will
 136 * gather from the beginning of the log and process until the current entry.
 137 *
 138 * Notes:
 139 * Discovery logging will be disabled while while this routine dumps the log.
 140 *
 141 * Return Value:
 142 * This routine returns the amount of bytes that were dumped into @buf and will
 143 * not exceed @size.
 144 **/
 145static int
 146lpfc_debugfs_disc_trc_data(struct lpfc_vport *vport, char *buf, int size)
 147{
 148	int i, index, len, enable;
 149	uint32_t ms;
 150	struct lpfc_debugfs_trc *dtp;
 151	char *buffer;
 152
 153	buffer = kmalloc(LPFC_DEBUG_TRC_ENTRY_SIZE, GFP_KERNEL);
 154	if (!buffer)
 155		return 0;
 156
 157	enable = lpfc_debugfs_enable;
 158	lpfc_debugfs_enable = 0;
 159
 160	len = 0;
 161	index = (atomic_read(&vport->disc_trc_cnt) + 1) &
 162		(lpfc_debugfs_max_disc_trc - 1);
 163	for (i = index; i < lpfc_debugfs_max_disc_trc; i++) {
 164		dtp = vport->disc_trc + i;
 165		if (!dtp->fmt)
 166			continue;
 167		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
 168		snprintf(buffer,
 169			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
 170			dtp->seq_cnt, ms, dtp->fmt);
 171		len +=  scnprintf(buf+len, size-len, buffer,
 172			dtp->data1, dtp->data2, dtp->data3);
 173	}
 174	for (i = 0; i < index; i++) {
 175		dtp = vport->disc_trc + i;
 176		if (!dtp->fmt)
 177			continue;
 178		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
 179		snprintf(buffer,
 180			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
 181			dtp->seq_cnt, ms, dtp->fmt);
 182		len +=  scnprintf(buf+len, size-len, buffer,
 183			dtp->data1, dtp->data2, dtp->data3);
 184	}
 185
 186	lpfc_debugfs_enable = enable;
 187	kfree(buffer);
 188
 189	return len;
 190}
 191
 192/**
 193 * lpfc_debugfs_slow_ring_trc_data - Dump slow ring logging to a buffer
 194 * @phba: The HBA to gather the log info from.
 195 * @buf: The buffer to dump log into.
 196 * @size: The maximum amount of data to process.
 197 *
 198 * Description:
 199 * This routine gathers the lpfc slow ring debugfs data from the @phba and
 200 * dumps it to @buf up to @size number of bytes. It will start at the next entry
 201 * in the log and process the log until the end of the buffer. Then it will
 202 * gather from the beginning of the log and process until the current entry.
 203 *
 204 * Notes:
 205 * Slow ring logging will be disabled while while this routine dumps the log.
 206 *
 207 * Return Value:
 208 * This routine returns the amount of bytes that were dumped into @buf and will
 209 * not exceed @size.
 210 **/
 211static int
 212lpfc_debugfs_slow_ring_trc_data(struct lpfc_hba *phba, char *buf, int size)
 213{
 214	int i, index, len, enable;
 215	uint32_t ms;
 216	struct lpfc_debugfs_trc *dtp;
 217	char *buffer;
 218
 219	buffer = kmalloc(LPFC_DEBUG_TRC_ENTRY_SIZE, GFP_KERNEL);
 220	if (!buffer)
 221		return 0;
 222
 223	enable = lpfc_debugfs_enable;
 224	lpfc_debugfs_enable = 0;
 225
 226	len = 0;
 227	index = (atomic_read(&phba->slow_ring_trc_cnt) + 1) &
 228		(lpfc_debugfs_max_slow_ring_trc - 1);
 229	for (i = index; i < lpfc_debugfs_max_slow_ring_trc; i++) {
 230		dtp = phba->slow_ring_trc + i;
 231		if (!dtp->fmt)
 232			continue;
 233		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
 234		snprintf(buffer,
 235			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
 236			dtp->seq_cnt, ms, dtp->fmt);
 237		len +=  scnprintf(buf+len, size-len, buffer,
 238			dtp->data1, dtp->data2, dtp->data3);
 239	}
 240	for (i = 0; i < index; i++) {
 241		dtp = phba->slow_ring_trc + i;
 242		if (!dtp->fmt)
 243			continue;
 244		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
 245		snprintf(buffer,
 246			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
 247			dtp->seq_cnt, ms, dtp->fmt);
 248		len +=  scnprintf(buf+len, size-len, buffer,
 249			dtp->data1, dtp->data2, dtp->data3);
 250	}
 251
 252	lpfc_debugfs_enable = enable;
 253	kfree(buffer);
 254
 255	return len;
 256}
 257
 258static int lpfc_debugfs_last_hbq = -1;
 259
 260/**
 261 * lpfc_debugfs_hbqinfo_data - Dump host buffer queue info to a buffer
 262 * @phba: The HBA to gather host buffer info from.
 263 * @buf: The buffer to dump log into.
 264 * @size: The maximum amount of data to process.
 265 *
 266 * Description:
 267 * This routine dumps the host buffer queue info from the @phba to @buf up to
 268 * @size number of bytes. A header that describes the current hbq state will be
 269 * dumped to @buf first and then info on each hbq entry will be dumped to @buf
 270 * until @size bytes have been dumped or all the hbq info has been dumped.
 271 *
 272 * Notes:
 273 * This routine will rotate through each configured HBQ each time called.
 274 *
 275 * Return Value:
 276 * This routine returns the amount of bytes that were dumped into @buf and will
 277 * not exceed @size.
 278 **/
 279static int
 280lpfc_debugfs_hbqinfo_data(struct lpfc_hba *phba, char *buf, int size)
 281{
 282	int len = 0;
 283	int i, j, found, posted, low;
 284	uint32_t phys, raw_index, getidx;
 285	struct lpfc_hbq_init *hip;
 286	struct hbq_s *hbqs;
 287	struct lpfc_hbq_entry *hbqe;
 288	struct lpfc_dmabuf *d_buf;
 289	struct hbq_dmabuf *hbq_buf;
 290
 291	if (phba->sli_rev != 3)
 292		return 0;
 293
 294	spin_lock_irq(&phba->hbalock);
 295
 296	/* toggle between multiple hbqs, if any */
 297	i = lpfc_sli_hbq_count();
 298	if (i > 1) {
 299		 lpfc_debugfs_last_hbq++;
 300		 if (lpfc_debugfs_last_hbq >= i)
 301			lpfc_debugfs_last_hbq = 0;
 302	}
 303	else
 304		lpfc_debugfs_last_hbq = 0;
 305
 306	i = lpfc_debugfs_last_hbq;
 307
 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		posted++;
 314
 315	hip =  lpfc_hbq_defs[i];
 316	len +=  scnprintf(buf+len, size-len,
 317		"idx:%d prof:%d rn:%d bufcnt:%d icnt:%d acnt:%d posted %d\n",
 318		hip->hbq_index, hip->profile, hip->rn,
 319		hip->buffer_count, hip->init_count, hip->add_count, posted);
 320
 321	raw_index = phba->hbq_get[i];
 322	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		hbqs->entry_count, hbqs->buffer_count, hbqs->hbqPutIdx,
 326		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			le32_to_cpu(hbqe->bde.addrLow),
 333			le32_to_cpu(hbqe->bde.tus.w),
 334			le32_to_cpu(hbqe->buffer_tag));
 335		i = 0;
 336		found = 0;
 337
 338		/* First calculate if slot has an associated posted buffer */
 339		low = hbqs->hbqPutIdx - posted;
 340		if (low >= 0) {
 341			if ((j >= hbqs->hbqPutIdx) || (j < low)) {
 342				len +=  scnprintf(buf + len, size - len,
 343						"Unused\n");
 344				goto skipit;
 345			}
 346		}
 347		else {
 348			if ((j >= hbqs->hbqPutIdx) &&
 349				(j < (hbqs->entry_count+low))) {
 350				len +=  scnprintf(buf + len, size - len,
 351						"Unused\n");
 352				goto skipit;
 353			}
 354		}
 355
 356		/* Get the Buffer info for the posted buffer */
 357		list_for_each_entry(d_buf, &hbqs->hbq_buffer_list, list) {
 358			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
 359			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					hbq_buf->dbuf.virt, hbq_buf->tag);
 364				found = 1;
 365				break;
 366			}
 367			i++;
 368		}
 369		if (!found) {
 370			len +=  scnprintf(buf+len, size-len, "No DMAinfo?\n");
 371		}
 372skipit:
 373		hbqe++;
 374		if (len > LPFC_HBQINFO_SIZE - 54)
 375			break;
 376	}
 377	spin_unlock_irq(&phba->hbalock);
 378	return len;
 379}
 380
 381static int lpfc_debugfs_last_xripool;
 382
 383/**
 384 * lpfc_debugfs_commonxripools_data - Dump Hardware Queue info to a buffer
 385 * @phba: The HBA to gather host buffer info from.
 386 * @buf: The buffer to dump log into.
 387 * @size: The maximum amount of data to process.
 388 *
 389 * Description:
 390 * This routine dumps the Hardware Queue info from the @phba to @buf up to
 391 * @size number of bytes. A header that describes the current hdwq state will be
 392 * dumped to @buf first and then info on each hdwq entry will be dumped to @buf
 393 * until @size bytes have been dumped or all the hdwq info has been dumped.
 394 *
 395 * Notes:
 396 * This routine will rotate through each configured Hardware Queue each
 397 * time called.
 398 *
 399 * Return Value:
 400 * This routine returns the amount of bytes that were dumped into @buf and will
 401 * not exceed @size.
 402 **/
 403static int
 404lpfc_debugfs_commonxripools_data(struct lpfc_hba *phba, char *buf, int size)
 405{
 406	struct lpfc_sli4_hdw_queue *qp;
 407	int len = 0;
 408	int i, out;
 409	unsigned long iflag;
 410
 411	for (i = 0; i < phba->cfg_hdw_queue; i++) {
 412		if (len > (LPFC_DUMP_MULTIXRIPOOL_SIZE - 80))
 413			break;
 414		qp = &phba->sli4_hba.hdwq[lpfc_debugfs_last_xripool];
 415
 416		len += scnprintf(buf + len, size - len, "HdwQ %d Info ", i);
 417		spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
 418		spin_lock(&qp->io_buf_list_get_lock);
 419		spin_lock(&qp->io_buf_list_put_lock);
 420		out = qp->total_io_bufs - (qp->get_io_bufs + qp->put_io_bufs +
 421			qp->abts_scsi_io_bufs + qp->abts_nvme_io_bufs);
 422		len += scnprintf(buf + len, size - len,
 423				 "tot:%d get:%d put:%d mt:%d "
 424				 "ABTS scsi:%d nvme:%d Out:%d\n",
 425			qp->total_io_bufs, qp->get_io_bufs, qp->put_io_bufs,
 426			qp->empty_io_bufs, qp->abts_scsi_io_bufs,
 427			qp->abts_nvme_io_bufs, out);
 428		spin_unlock(&qp->io_buf_list_put_lock);
 429		spin_unlock(&qp->io_buf_list_get_lock);
 430		spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
 431
 432		lpfc_debugfs_last_xripool++;
 433		if (lpfc_debugfs_last_xripool >= phba->cfg_hdw_queue)
 434			lpfc_debugfs_last_xripool = 0;
 435	}
 436
 437	return len;
 438}
 439
 440/**
 441 * lpfc_debugfs_multixripools_data - Display multi-XRI pools information
 442 * @phba: The HBA to gather host buffer info from.
 443 * @buf: The buffer to dump log into.
 444 * @size: The maximum amount of data to process.
 445 *
 446 * Description:
 447 * This routine displays current multi-XRI pools information including XRI
 448 * count in public, private and txcmplq. It also displays current high and
 449 * low watermark.
 450 *
 451 * Return Value:
 452 * This routine returns the amount of bytes that were dumped into @buf and will
 453 * not exceed @size.
 454 **/
 455static int
 456lpfc_debugfs_multixripools_data(struct lpfc_hba *phba, char *buf, int size)
 457{
 458	u32 i;
 459	u32 hwq_count;
 460	struct lpfc_sli4_hdw_queue *qp;
 461	struct lpfc_multixri_pool *multixri_pool;
 462	struct lpfc_pvt_pool *pvt_pool;
 463	struct lpfc_pbl_pool *pbl_pool;
 464	u32 txcmplq_cnt;
 465	char tmp[LPFC_DEBUG_OUT_LINE_SZ] = {0};
 466
 467	if (phba->sli_rev != LPFC_SLI_REV4)
 468		return 0;
 469
 470	if (!phba->sli4_hba.hdwq)
 471		return 0;
 472
 473	if (!phba->cfg_xri_rebalancing) {
 474		i = lpfc_debugfs_commonxripools_data(phba, buf, size);
 475		return i;
 476	}
 477
 478	/*
 479	 * Pbl: Current number of free XRIs in public pool
 480	 * Pvt: Current number of free XRIs in private pool
 481	 * Busy: Current number of outstanding XRIs
 482	 * HWM: Current high watermark
 483	 * pvt_empty: Incremented by 1 when IO submission fails (no xri)
 484	 * pbl_empty: Incremented by 1 when all pbl_pool are empty during
 485	 *            IO submission
 486	 */
 487	scnprintf(tmp, sizeof(tmp),
 488		  "HWQ:  Pbl  Pvt Busy  HWM |  pvt_empty  pbl_empty ");
 489	if (strlcat(buf, tmp, size) >= size)
 490		return strnlen(buf, size);
 491
 492#ifdef LPFC_MXP_STAT
 493	/*
 494	 * MAXH: Max high watermark seen so far
 495	 * above_lmt: Incremented by 1 if xri_owned > xri_limit during
 496	 *            IO submission
 497	 * below_lmt: Incremented by 1 if xri_owned <= xri_limit  during
 498	 *            IO submission
 499	 * locPbl_hit: Incremented by 1 if successfully get a batch of XRI from
 500	 *             local pbl_pool
 501	 * othPbl_hit: Incremented by 1 if successfully get a batch of XRI from
 502	 *             other pbl_pool
 503	 */
 504	scnprintf(tmp, sizeof(tmp),
 505		  "MAXH  above_lmt  below_lmt locPbl_hit othPbl_hit");
 506	if (strlcat(buf, tmp, size) >= size)
 507		return strnlen(buf, size);
 508
 509	/*
 510	 * sPbl: snapshot of Pbl 15 sec after stat gets cleared
 511	 * sPvt: snapshot of Pvt 15 sec after stat gets cleared
 512	 * sBusy: snapshot of Busy 15 sec after stat gets cleared
 513	 */
 514	scnprintf(tmp, sizeof(tmp),
 515		  " | sPbl sPvt sBusy");
 516	if (strlcat(buf, tmp, size) >= size)
 517		return strnlen(buf, size);
 518#endif
 519
 520	scnprintf(tmp, sizeof(tmp), "\n");
 521	if (strlcat(buf, tmp, size) >= size)
 522		return strnlen(buf, size);
 523
 524	hwq_count = phba->cfg_hdw_queue;
 525	for (i = 0; i < hwq_count; i++) {
 526		qp = &phba->sli4_hba.hdwq[i];
 527		multixri_pool = qp->p_multixri_pool;
 528		if (!multixri_pool)
 529			continue;
 530		pbl_pool = &multixri_pool->pbl_pool;
 531		pvt_pool = &multixri_pool->pvt_pool;
 532		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
 533
 534		scnprintf(tmp, sizeof(tmp),
 535			  "%03d: %4d %4d %4d %4d | %10d %10d ",
 536			  i, pbl_pool->count, pvt_pool->count,
 537			  txcmplq_cnt, pvt_pool->high_watermark,
 538			  qp->empty_io_bufs, multixri_pool->pbl_empty_count);
 539		if (strlcat(buf, tmp, size) >= size)
 540			break;
 541
 542#ifdef LPFC_MXP_STAT
 543		scnprintf(tmp, sizeof(tmp),
 544			  "%4d %10d %10d %10d %10d",
 545			  multixri_pool->stat_max_hwm,
 546			  multixri_pool->above_limit_count,
 547			  multixri_pool->below_limit_count,
 548			  multixri_pool->local_pbl_hit_count,
 549			  multixri_pool->other_pbl_hit_count);
 550		if (strlcat(buf, tmp, size) >= size)
 551			break;
 552
 553		scnprintf(tmp, sizeof(tmp),
 554			  " | %4d %4d %5d",
 555			  multixri_pool->stat_pbl_count,
 556			  multixri_pool->stat_pvt_count,
 557			  multixri_pool->stat_busy_count);
 558		if (strlcat(buf, tmp, size) >= size)
 559			break;
 560#endif
 561
 562		scnprintf(tmp, sizeof(tmp), "\n");
 563		if (strlcat(buf, tmp, size) >= size)
 564			break;
 565	}
 566	return strnlen(buf, size);
 567}
 568
 569
 570#ifdef LPFC_HDWQ_LOCK_STAT
 571static int lpfc_debugfs_last_lock;
 572
 573/**
 574 * lpfc_debugfs_lockstat_data - Dump Hardware Queue info to a buffer
 575 * @phba: The HBA to gather host buffer info from.
 576 * @buf: The buffer to dump log into.
 577 * @size: The maximum amount of data to process.
 578 *
 579 * Description:
 580 * This routine dumps the Hardware Queue info from the @phba to @buf up to
 581 * @size number of bytes. A header that describes the current hdwq state will be
 582 * dumped to @buf first and then info on each hdwq entry will be dumped to @buf
 583 * until @size bytes have been dumped or all the hdwq info has been dumped.
 584 *
 585 * Notes:
 586 * This routine will rotate through each configured Hardware Queue each
 587 * time called.
 588 *
 589 * Return Value:
 590 * This routine returns the amount of bytes that were dumped into @buf and will
 591 * not exceed @size.
 592 **/
 593static int
 594lpfc_debugfs_lockstat_data(struct lpfc_hba *phba, char *buf, int size)
 595{
 596	struct lpfc_sli4_hdw_queue *qp;
 597	int len = 0;
 598	int i;
 599
 600	if (phba->sli_rev != LPFC_SLI_REV4)
 601		return 0;
 602
 603	if (!phba->sli4_hba.hdwq)
 604		return 0;
 605
 606	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					 "get_pvt:%d mv_pvt:%d "
 615					 "mv2pub:%d mv2pvt:%d "
 616					 "put_pvt:%d put_pub:%d wq:%d\n",
 617					 qp->lock_conflict.alloc_pvt_pool,
 618					 qp->lock_conflict.mv_from_pvt_pool,
 619					 qp->lock_conflict.mv_to_pub_pool,
 620					 qp->lock_conflict.mv_to_pvt_pool,
 621					 qp->lock_conflict.free_pvt_pool,
 622					 qp->lock_conflict.free_pub_pool,
 623					 qp->lock_conflict.wq_access);
 624		} else {
 625			len += scnprintf(buf + len, size - len,
 626					 "get:%d put:%d free:%d wq:%d\n",
 627					 qp->lock_conflict.alloc_xri_get,
 628					 qp->lock_conflict.alloc_xri_put,
 629					 qp->lock_conflict.free_xri,
 630					 qp->lock_conflict.wq_access);
 631		}
 632
 633		lpfc_debugfs_last_lock++;
 634		if (lpfc_debugfs_last_lock >= phba->cfg_hdw_queue)
 635			lpfc_debugfs_last_lock = 0;
 636	}
 637
 638	return len;
 639}
 640#endif
 641
 642static int lpfc_debugfs_last_hba_slim_off;
 643
 644/**
 645 * lpfc_debugfs_dumpHBASlim_data - Dump HBA SLIM info to a buffer
 646 * @phba: The HBA to gather SLIM info from.
 647 * @buf: The buffer to dump log into.
 648 * @size: The maximum amount of data to process.
 649 *
 650 * Description:
 651 * This routine dumps the current contents of HBA SLIM for the HBA associated
 652 * with @phba to @buf up to @size bytes of data. This is the raw HBA SLIM data.
 653 *
 654 * Notes:
 655 * This routine will only dump up to 1024 bytes of data each time called and
 656 * should be called multiple times to dump the entire HBA SLIM.
 657 *
 658 * Return Value:
 659 * This routine returns the amount of bytes that were dumped into @buf and will
 660 * not exceed @size.
 661 **/
 662static int
 663lpfc_debugfs_dumpHBASlim_data(struct lpfc_hba *phba, char *buf, int size)
 664{
 665	int len = 0;
 666	int i, off;
 667	uint32_t *ptr;
 668	char *buffer;
 669
 670	buffer = kmalloc(1024, GFP_KERNEL);
 671	if (!buffer)
 672		return 0;
 673
 674	off = 0;
 675	spin_lock_irq(&phba->hbalock);
 676
 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	ptr = (uint32_t *)&buffer[0];
 682	off = lpfc_debugfs_last_hba_slim_off;
 683
 684	/* Set it up for the next time */
 685	lpfc_debugfs_last_hba_slim_off += 1024;
 686	if (lpfc_debugfs_last_hba_slim_off >= 4096)
 687		lpfc_debugfs_last_hba_slim_off = 0;
 688
 689	i = 1024;
 690	while (i > 0) {
 691		len +=  scnprintf(buf+len, size-len,
 692		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
 693		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
 694		*(ptr+5), *(ptr+6), *(ptr+7));
 695		ptr += 8;
 696		i -= (8 * sizeof(uint32_t));
 697		off += (8 * sizeof(uint32_t));
 698	}
 699
 700	spin_unlock_irq(&phba->hbalock);
 701	kfree(buffer);
 702
 703	return len;
 704}
 705
 706/**
 707 * lpfc_debugfs_dumpHostSlim_data - Dump host SLIM info to a buffer
 708 * @phba: The HBA to gather Host SLIM info from.
 709 * @buf: The buffer to dump log into.
 710 * @size: The maximum amount of data to process.
 711 *
 712 * Description:
 713 * This routine dumps the current contents of host SLIM for the host associated
 714 * with @phba to @buf up to @size bytes of data. The dump will contain the
 715 * Mailbox, PCB, Rings, and Registers that are located in host memory.
 716 *
 717 * Return Value:
 718 * This routine returns the amount of bytes that were dumped into @buf and will
 719 * not exceed @size.
 720 **/
 721static int
 722lpfc_debugfs_dumpHostSlim_data(struct lpfc_hba *phba, char *buf, int size)
 723{
 724	int len = 0;
 725	int i, off;
 726	uint32_t word0, word1, word2, word3;
 727	uint32_t *ptr;
 728	struct lpfc_pgp *pgpp;
 729	struct lpfc_sli *psli = &phba->sli;
 730	struct lpfc_sli_ring *pring;
 731
 732	off = 0;
 733	spin_lock_irq(&phba->hbalock);
 734
 735	len +=  scnprintf(buf+len, size-len, "SLIM Mailbox\n");
 736	ptr = (uint32_t *)phba->slim2p.virt;
 737	i = sizeof(MAILBOX_t);
 738	while (i > 0) {
 739		len +=  scnprintf(buf+len, size-len,
 740		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
 741		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
 742		*(ptr+5), *(ptr+6), *(ptr+7));
 743		ptr += 8;
 744		i -= (8 * sizeof(uint32_t));
 745		off += (8 * sizeof(uint32_t));
 746	}
 747
 748	len +=  scnprintf(buf+len, size-len, "SLIM PCB\n");
 749	ptr = (uint32_t *)phba->pcb;
 750	i = sizeof(PCB_t);
 751	while (i > 0) {
 752		len +=  scnprintf(buf+len, size-len,
 753		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
 754		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
 755		*(ptr+5), *(ptr+6), *(ptr+7));
 756		ptr += 8;
 757		i -= (8 * sizeof(uint32_t));
 758		off += (8 * sizeof(uint32_t));
 759	}
 760
 761	if (phba->sli_rev <= LPFC_SLI_REV3) {
 762		for (i = 0; i < 4; i++) {
 763			pgpp = &phba->port_gp[i];
 764			pring = &psli->sli3_ring[i];
 765			len +=  scnprintf(buf+len, size-len,
 766					 "Ring %d: CMD GetInx:%d "
 767					 "(Max:%d Next:%d "
 768					 "Local:%d flg:x%x)  "
 769					 "RSP PutInx:%d Max:%d\n",
 770					 i, pgpp->cmdGetInx,
 771					 pring->sli.sli3.numCiocb,
 772					 pring->sli.sli3.next_cmdidx,
 773					 pring->sli.sli3.local_getidx,
 774					 pring->flag, pgpp->rspPutInx,
 775					 pring->sli.sli3.numRiocb);
 776		}
 777
 778		word0 = readl(phba->HAregaddr);
 779		word1 = readl(phba->CAregaddr);
 780		word2 = readl(phba->HSregaddr);
 781		word3 = readl(phba->HCregaddr);
 782		len +=  scnprintf(buf+len, size-len, "HA:%08x CA:%08x HS:%08x "
 783				 "HC:%08x\n", word0, word1, word2, word3);
 784	}
 785	spin_unlock_irq(&phba->hbalock);
 786	return len;
 787}
 788
 789/**
 790 * lpfc_debugfs_nodelist_data - Dump target node list to a buffer
 791 * @vport: The vport to gather target node info from.
 792 * @buf: The buffer to dump log into.
 793 * @size: The maximum amount of data to process.
 794 *
 795 * Description:
 796 * This routine dumps the current target node list associated with @vport to
 797 * @buf up to @size bytes of data. Each node entry in the dump will contain a
 798 * node state, DID, WWPN, WWNN, RPI, flags, type, and other useful fields.
 799 *
 800 * Return Value:
 801 * This routine returns the amount of bytes that were dumped into @buf and will
 802 * not exceed @size.
 803 **/
 804static int
 805lpfc_debugfs_nodelist_data(struct lpfc_vport *vport, char *buf, int size)
 806{
 807	int len = 0;
 808	int i, iocnt, outio, cnt;
 809	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 810	struct lpfc_hba  *phba = vport->phba;
 811	struct lpfc_nodelist *ndlp;
 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;
 816
 817	cnt = (LPFC_NODELIST_SIZE / LPFC_NODELIST_ENTRY_SIZE);
 818	outio = 0;
 819
 820	len += scnprintf(buf+len, size-len, "\nFCP Nodelist Entries ...\n");
 821	spin_lock_irq(shost->host_lock);
 822	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
 823		iocnt = 0;
 824		if (!cnt) {
 825			len +=  scnprintf(buf+len, size-len,
 826				"Missing Nodelist Entries\n");
 827			break;
 828		}
 829		cnt--;
 830		switch (ndlp->nlp_state) {
 831		case NLP_STE_UNUSED_NODE:
 832			statep = "UNUSED";
 833			break;
 834		case NLP_STE_PLOGI_ISSUE:
 835			statep = "PLOGI ";
 836			break;
 837		case NLP_STE_ADISC_ISSUE:
 838			statep = "ADISC ";
 839			break;
 840		case NLP_STE_REG_LOGIN_ISSUE:
 841			statep = "REGLOG";
 842			break;
 843		case NLP_STE_PRLI_ISSUE:
 844			statep = "PRLI  ";
 845			break;
 846		case NLP_STE_LOGO_ISSUE:
 847			statep = "LOGO  ";
 848			break;
 849		case NLP_STE_UNMAPPED_NODE:
 850			statep = "UNMAP ";
 851			iocnt = 1;
 852			break;
 853		case NLP_STE_MAPPED_NODE:
 854			statep = "MAPPED";
 855			iocnt = 1;
 856			break;
 857		case NLP_STE_NPR_NODE:
 858			statep = "NPR   ";
 859			break;
 860		default:
 861			statep = "UNKNOWN";
 862		}
 863		len += scnprintf(buf+len, size-len, "%s DID:x%06x ",
 864				statep, ndlp->nlp_DID);
 865		len += scnprintf(buf+len, size-len,
 866				"WWPN x%016llx ",
 867				wwn_to_u64(ndlp->nlp_portname.u.wwn));
 868		len += scnprintf(buf+len, size-len,
 869				"WWNN x%016llx ",
 870				wwn_to_u64(ndlp->nlp_nodename.u.wwn));
 871		len += scnprintf(buf+len, size-len, "RPI:x%04x ",
 872				 ndlp->nlp_rpi);
 
 
 
 873		len +=  scnprintf(buf+len, size-len, "flag:x%08x ",
 874			ndlp->nlp_flag);
 875		if (!ndlp->nlp_type)
 876			len += scnprintf(buf+len, size-len, "UNKNOWN_TYPE ");
 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			iocnt = 0;
 882		}
 883		if (ndlp->nlp_type & NLP_FCP_TARGET)
 884			len += scnprintf(buf+len, size-len, "FCP_TGT sid:%d ",
 885				ndlp->nlp_sid);
 886		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					size - len, "NVME_TGT sid:%d ",
 891					NLP_NO_SID);
 892		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		if (iocnt) {
 898			i = atomic_read(&ndlp->cmd_pending);
 899			len += scnprintf(buf + len, size - len,
 900					" OutIO:x%x Qdepth x%x",
 901					i, ndlp->cmd_qdepth);
 902			outio += i;
 903		}
 904		len += scnprintf(buf+len, size-len, " xpt:x%x",
 905				 ndlp->fc4_xpt_flags);
 906		if (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)
 907			len += scnprintf(buf+len, size-len, " defer:%x",
 908					 ndlp->nlp_defer_did);
 909		len +=  scnprintf(buf+len, size-len, "\n");
 910	}
 911	spin_unlock_irq(shost->host_lock);
 912
 913	len += scnprintf(buf + len, size - len,
 914			"\nOutstanding IO x%x\n",  outio);
 915
 916	if (phba->nvmet_support && phba->targetport && (vport == phba->pport)) {
 917		len += scnprintf(buf + len, size - len,
 918				"\nNVME Targetport Entry ...\n");
 919
 920		/* Port state is only one of two values for now. */
 921		if (phba->targetport->port_id)
 922			statep = "REGISTERED";
 923		else
 924			statep = "INIT";
 925		len += scnprintf(buf + len, size - len,
 926				"TGT WWNN x%llx WWPN x%llx State %s\n",
 927				wwn_to_u64(vport->fc_nodename.u.wwn),
 928				wwn_to_u64(vport->fc_portname.u.wwn),
 929				statep);
 930		len += scnprintf(buf + len, size - len,
 931				"    Targetport DID x%06x\n",
 932				phba->targetport->port_id);
 933		goto out_exit;
 934	}
 935
 936	len += scnprintf(buf + len, size - len,
 937				"\nNVME Lport/Rport Entries ...\n");
 938
 939	localport = vport->localport;
 940	if (!localport)
 941		goto out_exit;
 942
 943	spin_lock_irq(shost->host_lock);
 944
 945	/* Port state is only one of two values for now. */
 946	if (localport->port_id)
 947		statep = "ONLINE";
 948	else
 949		statep = "UNKNOWN ";
 950
 951	len += scnprintf(buf + len, size - len,
 952			"Lport DID x%06x PortState %s\n",
 953			localport->port_id, statep);
 954
 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		if (rport)
 961			nrport = rport->remoteport;
 962		else
 963			nrport = NULL;
 964		spin_unlock(&ndlp->lock);
 965		if (!nrport)
 966			continue;
 967
 968		/* Port state is only one of two values for now. */
 969		switch (nrport->port_state) {
 970		case FC_OBJSTATE_ONLINE:
 971			statep = "ONLINE";
 972			break;
 973		case FC_OBJSTATE_UNKNOWN:
 974			statep = "UNKNOWN ";
 975			break;
 976		default:
 977			statep = "UNSUPPORTED";
 978			break;
 979		}
 980
 981		/* Tab in to show lport ownership. */
 982		len += scnprintf(buf + len, size - len,
 983				"\t%s Port ID:x%06x ",
 984				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		/* An NVME rport can have multiple roles. */
 991		if (nrport->port_role & FC_PORT_ROLE_NVME_INITIATOR)
 992			len +=  scnprintf(buf + len, size - len,
 993					 "INITIATOR ");
 994		if (nrport->port_role & FC_PORT_ROLE_NVME_TARGET)
 995			len +=  scnprintf(buf + len, size - len,
 996					 "TARGET ");
 997		if (nrport->port_role & FC_PORT_ROLE_NVME_DISCOVERY)
 998			len +=  scnprintf(buf + len, size - len,
 999					 "DISCSRVC ");
1000		if (nrport->port_role & ~(FC_PORT_ROLE_NVME_INITIATOR |
1001					  FC_PORT_ROLE_NVME_TARGET |
1002					  FC_PORT_ROLE_NVME_DISCOVERY))
1003			len +=  scnprintf(buf + len, size - len,
1004					 "UNKNOWN ROLE x%x",
1005					 nrport->port_role);
1006		/* Terminate the string. */
1007		len +=  scnprintf(buf + len, size - len, "\n");
1008	}
1009
1010	spin_unlock_irq(shost->host_lock);
1011 out_exit:
1012	return len;
1013}
1014
1015/**
1016 * lpfc_debugfs_nvmestat_data - Dump target node list to a buffer
1017 * @vport: The vport to gather target node info from.
1018 * @buf: The buffer to dump log into.
1019 * @size: The maximum amount of data to process.
1020 *
1021 * Description:
1022 * This routine dumps the NVME statistics associated with @vport
1023 *
1024 * Return Value:
1025 * This routine returns the amount of bytes that were dumped into @buf and will
1026 * not exceed @size.
1027 **/
1028static int
1029lpfc_debugfs_nvmestat_data(struct lpfc_vport *vport, char *buf, int size)
1030{
1031	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	uint64_t data1, data2, data3;
1038	uint64_t tot, totin, totout;
1039	int cnt, i;
1040	int len = 0;
1041
1042	if (phba->nvmet_support) {
1043		if (!phba->targetport)
1044			return len;
1045		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1046		len += scnprintf(buf + len, size - len,
1047				"\nNVME Targetport Statistics\n");
1048
1049		len += scnprintf(buf + len, size - len,
1050				"LS: Rcv %08x Drop %08x Abort %08x\n",
1051				atomic_read(&tgtp->rcv_ls_req_in),
1052				atomic_read(&tgtp->rcv_ls_req_drop),
1053				atomic_read(&tgtp->xmt_ls_abort));
1054		if (atomic_read(&tgtp->rcv_ls_req_in) !=
1055		    atomic_read(&tgtp->rcv_ls_req_out)) {
1056			len += scnprintf(buf + len, size - len,
1057					"Rcv LS: in %08x != out %08x\n",
1058					atomic_read(&tgtp->rcv_ls_req_in),
1059					atomic_read(&tgtp->rcv_ls_req_out));
1060		}
1061
1062		len += scnprintf(buf + len, size - len,
1063				"LS: Xmt %08x Drop %08x Cmpl %08x\n",
1064				atomic_read(&tgtp->xmt_ls_rsp),
1065				atomic_read(&tgtp->xmt_ls_drop),
1066				atomic_read(&tgtp->xmt_ls_rsp_cmpl));
1067
1068		len += scnprintf(buf + len, size - len,
1069				"LS: RSP Abort %08x xb %08x Err %08x\n",
1070				atomic_read(&tgtp->xmt_ls_rsp_aborted),
1071				atomic_read(&tgtp->xmt_ls_rsp_xb_set),
1072				atomic_read(&tgtp->xmt_ls_rsp_error));
1073
1074		len += scnprintf(buf + len, size - len,
1075				"FCP: Rcv %08x Defer %08x Release %08x "
1076				"Drop %08x\n",
1077				atomic_read(&tgtp->rcv_fcp_cmd_in),
1078				atomic_read(&tgtp->rcv_fcp_cmd_defer),
1079				atomic_read(&tgtp->xmt_fcp_release),
1080				atomic_read(&tgtp->rcv_fcp_cmd_drop));
1081
1082		if (atomic_read(&tgtp->rcv_fcp_cmd_in) !=
1083		    atomic_read(&tgtp->rcv_fcp_cmd_out)) {
1084			len += scnprintf(buf + len, size - len,
1085					"Rcv FCP: in %08x != out %08x\n",
1086					atomic_read(&tgtp->rcv_fcp_cmd_in),
1087					atomic_read(&tgtp->rcv_fcp_cmd_out));
1088		}
1089
1090		len += scnprintf(buf + len, size - len,
1091				"FCP Rsp: read %08x readrsp %08x "
1092				"write %08x rsp %08x\n",
1093				atomic_read(&tgtp->xmt_fcp_read),
1094				atomic_read(&tgtp->xmt_fcp_read_rsp),
1095				atomic_read(&tgtp->xmt_fcp_write),
1096				atomic_read(&tgtp->xmt_fcp_rsp));
1097
1098		len += scnprintf(buf + len, size - len,
1099				"FCP Rsp Cmpl: %08x err %08x drop %08x\n",
1100				atomic_read(&tgtp->xmt_fcp_rsp_cmpl),
1101				atomic_read(&tgtp->xmt_fcp_rsp_error),
1102				atomic_read(&tgtp->xmt_fcp_rsp_drop));
1103
1104		len += scnprintf(buf + len, size - len,
1105				"FCP Rsp Abort: %08x xb %08x xricqe  %08x\n",
1106				atomic_read(&tgtp->xmt_fcp_rsp_aborted),
1107				atomic_read(&tgtp->xmt_fcp_rsp_xb_set),
1108				atomic_read(&tgtp->xmt_fcp_xri_abort_cqe));
1109
1110		len += scnprintf(buf + len, size - len,
1111				"ABORT: Xmt %08x Cmpl %08x\n",
1112				atomic_read(&tgtp->xmt_fcp_abort),
1113				atomic_read(&tgtp->xmt_fcp_abort_cmpl));
1114
1115		len += scnprintf(buf + len, size - len,
1116				"ABORT: Sol %08x  Usol %08x Err %08x Cmpl %08x",
1117				atomic_read(&tgtp->xmt_abort_sol),
1118				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");
1123
1124		cnt = 0;
1125		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1126		list_for_each_entry_safe(ctxp, next_ctxp,
1127				&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1128				list) {
1129			cnt++;
1130		}
1131		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1132		if (cnt) {
1133			len += scnprintf(buf + len, size - len,
1134					"ABORT: %d ctx entries\n", cnt);
1135			spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1136			list_for_each_entry_safe(ctxp, next_ctxp,
1137				    &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1138				    list) {
1139				if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ))
1140					break;
1141				len += scnprintf(buf + len, size - len,
1142						"Entry: oxid %x state %x "
1143						"flag %x\n",
1144						ctxp->oxid, ctxp->state,
1145						ctxp->flag);
1146			}
1147			spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1148		}
1149
1150		/* Calculate outstanding IOs */
1151		tot = atomic_read(&tgtp->rcv_fcp_cmd_drop);
1152		tot += atomic_read(&tgtp->xmt_fcp_release);
1153		tot = atomic_read(&tgtp->rcv_fcp_cmd_in) - tot;
1154
1155		len += scnprintf(buf + len, size - len,
1156				"IO_CTX: %08x  WAIT: cur %08x tot %08x\n"
1157				"CTX Outstanding %08llx\n",
1158				phba->sli4_hba.nvmet_xri_cnt,
1159				phba->sli4_hba.nvmet_io_wait_cnt,
1160				phba->sli4_hba.nvmet_io_wait_total,
1161				tot);
1162	} else {
1163		if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
1164			return len;
1165
1166		localport = vport->localport;
1167		if (!localport)
1168			return len;
1169		lport = (struct lpfc_nvme_lport *)localport->private;
1170		if (!lport)
1171			return len;
1172
1173		len += scnprintf(buf + len, size - len,
1174				"\nNVME HDWQ Statistics\n");
1175
1176		len += scnprintf(buf + len, size - len,
1177				"LS: Xmt %016x Cmpl %016x\n",
1178				atomic_read(&lport->fc4NvmeLsRequests),
1179				atomic_read(&lport->fc4NvmeLsCmpls));
1180
1181		totin = 0;
1182		totout = 0;
1183		for (i = 0; i < phba->cfg_hdw_queue; i++) {
1184			cstat = &phba->sli4_hba.hdwq[i].nvme_cstat;
1185			tot = cstat->io_cmpls;
1186			totin += tot;
1187			data1 = cstat->input_requests;
1188			data2 = cstat->output_requests;
1189			data3 = cstat->control_requests;
1190			totout += (data1 + data2 + data3);
1191
1192			/* Limit to 32, debugfs display buffer limitation */
1193			if (i >= 32)
1194				continue;
1195
1196			len += scnprintf(buf + len, PAGE_SIZE - len,
1197					"HDWQ (%d): Rd %016llx Wr %016llx "
1198					"IO %016llx ",
1199					i, data1, data2, data3);
1200			len += scnprintf(buf + len, PAGE_SIZE - len,
1201					"Cmpl %016llx OutIO %016llx\n",
1202					tot, ((data1 + data2 + data3) - tot));
1203		}
1204		len += scnprintf(buf + len, PAGE_SIZE - len,
1205				"Total FCP Cmpl %016llx Issue %016llx "
1206				"OutIO %016llx\n",
1207				totin, totout, totout - totin);
1208
1209		len += scnprintf(buf + len, size - len,
1210				"LS Xmt Err: Abrt %08x Err %08x  "
1211				"Cmpl Err: xb %08x Err %08x\n",
1212				atomic_read(&lport->xmt_ls_abort),
1213				atomic_read(&lport->xmt_ls_err),
1214				atomic_read(&lport->cmpl_ls_xb),
1215				atomic_read(&lport->cmpl_ls_err));
1216
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				atomic_read(&lport->xmt_fcp_noxri),
1221				atomic_read(&lport->xmt_fcp_bad_ndlp),
1222				atomic_read(&lport->xmt_fcp_qdepth),
1223				atomic_read(&lport->xmt_fcp_wqerr),
1224				atomic_read(&lport->xmt_fcp_err),
1225				atomic_read(&lport->xmt_fcp_abort));
1226
1227		len += scnprintf(buf + len, size - len,
1228				"FCP Cmpl Err: xb %08x Err %08x\n",
1229				atomic_read(&lport->cmpl_fcp_xb),
1230				atomic_read(&lport->cmpl_fcp_err));
1231
1232	}
1233
1234	return len;
1235}
1236
1237/**
1238 * lpfc_debugfs_scsistat_data - Dump target node list to a buffer
1239 * @vport: The vport to gather target node info from.
1240 * @buf: The buffer to dump log into.
1241 * @size: The maximum amount of data to process.
1242 *
1243 * Description:
1244 * This routine dumps the SCSI statistics associated with @vport
1245 *
1246 * Return Value:
1247 * This routine returns the amount of bytes that were dumped into @buf and will
1248 * not exceed @size.
1249 **/
1250static int
1251lpfc_debugfs_scsistat_data(struct lpfc_vport *vport, char *buf, int size)
1252{
1253	int len;
1254	struct lpfc_hba *phba = vport->phba;
1255	struct lpfc_fc4_ctrl_stat *cstat;
1256	u64 data1, data2, data3;
1257	u64 tot, totin, totout;
1258	int i;
1259	char tmp[LPFC_MAX_SCSI_INFO_TMP_LEN] = {0};
1260
1261	if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ||
1262	    (phba->sli_rev != LPFC_SLI_REV4))
1263		return 0;
1264
1265	scnprintf(buf, size, "SCSI HDWQ Statistics\n");
1266
1267	totin = 0;
1268	totout = 0;
1269	for (i = 0; i < phba->cfg_hdw_queue; i++) {
1270		cstat = &phba->sli4_hba.hdwq[i].scsi_cstat;
1271		tot = cstat->io_cmpls;
1272		totin += tot;
1273		data1 = cstat->input_requests;
1274		data2 = cstat->output_requests;
1275		data3 = cstat->control_requests;
1276		totout += (data1 + data2 + data3);
1277
1278		scnprintf(tmp, sizeof(tmp), "HDWQ (%d): Rd %016llx Wr %016llx "
1279			  "IO %016llx ", i, data1, data2, data3);
1280		if (strlcat(buf, tmp, size) >= size)
1281			goto buffer_done;
1282
1283		scnprintf(tmp, sizeof(tmp), "Cmpl %016llx OutIO %016llx\n",
1284			  tot, ((data1 + data2 + data3) - tot));
1285		if (strlcat(buf, tmp, size) >= size)
1286			goto buffer_done;
1287	}
1288	scnprintf(tmp, sizeof(tmp), "Total FCP Cmpl %016llx Issue %016llx "
1289		  "OutIO %016llx\n", totin, totout, totout - totin);
1290	strlcat(buf, tmp, size);
1291
1292buffer_done:
1293	len = strnlen(buf, size);
1294
1295	return len;
1296}
1297
1298void
1299lpfc_io_ktime(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
1300{
1301	uint64_t seg1, seg2, seg3, seg4;
1302	uint64_t segsum;
1303
1304	if (!lpfc_cmd->ts_last_cmd ||
1305	    !lpfc_cmd->ts_cmd_start ||
1306	    !lpfc_cmd->ts_cmd_wqput ||
1307	    !lpfc_cmd->ts_isr_cmpl ||
1308	    !lpfc_cmd->ts_data_io)
1309		return;
1310
1311	if (lpfc_cmd->ts_data_io < lpfc_cmd->ts_cmd_start)
1312		return;
1313	if (lpfc_cmd->ts_cmd_start < lpfc_cmd->ts_last_cmd)
1314		return;
1315	if (lpfc_cmd->ts_cmd_wqput < lpfc_cmd->ts_cmd_start)
1316		return;
1317	if (lpfc_cmd->ts_isr_cmpl < lpfc_cmd->ts_cmd_wqput)
1318		return;
1319	if (lpfc_cmd->ts_data_io < lpfc_cmd->ts_isr_cmpl)
1320		return;
1321	/*
1322	 * Segment 1 - Time from Last FCP command cmpl is handed
1323	 * off to NVME Layer to start of next command.
1324	 * Segment 2 - Time from Driver receives a IO cmd start
1325	 * from NVME Layer to WQ put is done on IO cmd.
1326	 * Segment 3 - Time from Driver WQ put is done on IO cmd
1327	 * to MSI-X ISR for IO cmpl.
1328	 * Segment 4 - Time from MSI-X ISR for IO cmpl to when
1329	 * cmpl is handled off to the NVME Layer.
1330	 */
1331	seg1 = lpfc_cmd->ts_cmd_start - lpfc_cmd->ts_last_cmd;
1332	if (seg1 > 5000000)  /* 5 ms - for sequential IOs only */
1333		seg1 = 0;
1334
1335	/* Calculate times relative to start of IO */
1336	seg2 = (lpfc_cmd->ts_cmd_wqput - lpfc_cmd->ts_cmd_start);
1337	segsum = seg2;
1338	seg3 = lpfc_cmd->ts_isr_cmpl - lpfc_cmd->ts_cmd_start;
1339	if (segsum > seg3)
1340		return;
1341	seg3 -= segsum;
1342	segsum += seg3;
1343
1344	seg4 = lpfc_cmd->ts_data_io - lpfc_cmd->ts_cmd_start;
1345	if (segsum > seg4)
1346		return;
1347	seg4 -= segsum;
1348
1349	phba->ktime_data_samples++;
1350	phba->ktime_seg1_total += seg1;
1351	if (seg1 < phba->ktime_seg1_min)
1352		phba->ktime_seg1_min = seg1;
1353	else if (seg1 > phba->ktime_seg1_max)
1354		phba->ktime_seg1_max = seg1;
1355	phba->ktime_seg2_total += seg2;
1356	if (seg2 < phba->ktime_seg2_min)
1357		phba->ktime_seg2_min = seg2;
1358	else if (seg2 > phba->ktime_seg2_max)
1359		phba->ktime_seg2_max = seg2;
1360	phba->ktime_seg3_total += seg3;
1361	if (seg3 < phba->ktime_seg3_min)
1362		phba->ktime_seg3_min = seg3;
1363	else if (seg3 > phba->ktime_seg3_max)
1364		phba->ktime_seg3_max = seg3;
1365	phba->ktime_seg4_total += seg4;
1366	if (seg4 < phba->ktime_seg4_min)
1367		phba->ktime_seg4_min = seg4;
1368	else if (seg4 > phba->ktime_seg4_max)
1369		phba->ktime_seg4_max = seg4;
1370
1371	lpfc_cmd->ts_last_cmd = 0;
1372	lpfc_cmd->ts_cmd_start = 0;
1373	lpfc_cmd->ts_cmd_wqput  = 0;
1374	lpfc_cmd->ts_isr_cmpl = 0;
1375	lpfc_cmd->ts_data_io = 0;
1376}
1377
1378/**
1379 * lpfc_debugfs_ioktime_data - Dump target node list to a buffer
1380 * @vport: The vport to gather target node info from.
1381 * @buf: The buffer to dump log into.
1382 * @size: The maximum amount of data to process.
1383 *
1384 * Description:
1385 * This routine dumps the NVME statistics associated with @vport
1386 *
1387 * Return Value:
1388 * This routine returns the amount of bytes that were dumped into @buf and will
1389 * not exceed @size.
1390 **/
1391static int
1392lpfc_debugfs_ioktime_data(struct lpfc_vport *vport, char *buf, int size)
1393{
1394	struct lpfc_hba   *phba = vport->phba;
1395	int len = 0;
1396
1397	if (phba->nvmet_support == 0) {
1398		/* Initiator */
1399		len += scnprintf(buf + len, PAGE_SIZE - len,
1400				"ktime %s: Total Samples: %lld\n",
1401				(phba->ktime_on ?  "Enabled" : "Disabled"),
1402				phba->ktime_data_samples);
1403		if (phba->ktime_data_samples == 0)
1404			return len;
1405
1406		len += scnprintf(
1407			buf + len, PAGE_SIZE - len,
1408			"Segment 1: Last Cmd cmpl "
1409			"done -to- Start of next Cmd (in driver)\n");
1410		len += scnprintf(
1411			buf + len, PAGE_SIZE - len,
1412			"avg:%08lld min:%08lld max %08lld\n",
1413			div_u64(phba->ktime_seg1_total,
1414				phba->ktime_data_samples),
1415			phba->ktime_seg1_min,
1416			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			buf + len, PAGE_SIZE - len,
1423			"avg:%08lld min:%08lld max %08lld\n",
1424			div_u64(phba->ktime_seg2_total,
1425				phba->ktime_data_samples),
1426			phba->ktime_seg2_min,
1427			phba->ktime_seg2_max);
1428		len += scnprintf(
1429			buf + len, PAGE_SIZE - len,
1430			"Segment 3: Firmware WQ doorbell -to- "
1431			"MSI-X ISR cmpl\n");
1432		len += scnprintf(
1433			buf + len, PAGE_SIZE - len,
1434			"avg:%08lld min:%08lld max %08lld\n",
1435			div_u64(phba->ktime_seg3_total,
1436				phba->ktime_data_samples),
1437			phba->ktime_seg3_min,
1438			phba->ktime_seg3_max);
1439		len += scnprintf(
1440			buf + len, PAGE_SIZE - len,
1441			"Segment 4: MSI-X ISR cmpl -to- "
1442			"Cmd cmpl done\n");
1443		len += scnprintf(
1444			buf + len, PAGE_SIZE - len,
1445			"avg:%08lld min:%08lld max %08lld\n",
1446			div_u64(phba->ktime_seg4_total,
1447				phba->ktime_data_samples),
1448			phba->ktime_seg4_min,
1449			phba->ktime_seg4_max);
1450		len += scnprintf(
1451			buf + len, PAGE_SIZE - len,
1452			"Total IO avg time: %08lld\n",
1453			div_u64(phba->ktime_seg1_total +
1454			phba->ktime_seg2_total  +
1455			phba->ktime_seg3_total +
1456			phba->ktime_seg4_total,
1457			phba->ktime_data_samples));
1458		return len;
1459	}
1460
1461	/* NVME Target */
1462	len += scnprintf(buf + len, PAGE_SIZE-len,
1463			"ktime %s: Total Samples: %lld %lld\n",
1464			(phba->ktime_on ? "Enabled" : "Disabled"),
1465			phba->ktime_data_samples,
1466			phba->ktime_status_samples);
1467	if (phba->ktime_data_samples == 0)
1468		return len;
1469
1470	len += scnprintf(buf + len, PAGE_SIZE-len,
1471			"Segment 1: MSI-X ISR Rcv cmd -to- "
1472			"cmd pass to NVME Layer\n");
1473	len += scnprintf(buf + len, PAGE_SIZE-len,
1474			"avg:%08lld min:%08lld max %08lld\n",
1475			div_u64(phba->ktime_seg1_total,
1476				phba->ktime_data_samples),
1477			phba->ktime_seg1_min,
1478			phba->ktime_seg1_max);
1479	len += scnprintf(buf + len, PAGE_SIZE-len,
1480			"Segment 2: cmd pass to NVME Layer- "
1481			"-to- Driver rcv cmd OP (action)\n");
1482	len += scnprintf(buf + len, PAGE_SIZE-len,
1483			"avg:%08lld min:%08lld max %08lld\n",
1484			div_u64(phba->ktime_seg2_total,
1485				phba->ktime_data_samples),
1486			phba->ktime_seg2_min,
1487			phba->ktime_seg2_max);
1488	len += scnprintf(buf + len, PAGE_SIZE-len,
1489			"Segment 3: Driver rcv cmd OP -to- "
1490			"Firmware WQ doorbell: cmd\n");
1491	len += scnprintf(buf + len, PAGE_SIZE-len,
1492			"avg:%08lld min:%08lld max %08lld\n",
1493			div_u64(phba->ktime_seg3_total,
1494				phba->ktime_data_samples),
1495			phba->ktime_seg3_min,
1496			phba->ktime_seg3_max);
1497	len += scnprintf(buf + len, PAGE_SIZE-len,
1498			"Segment 4: Firmware WQ doorbell: cmd "
1499			"-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			div_u64(phba->ktime_seg4_total,
1503				phba->ktime_data_samples),
1504			phba->ktime_seg4_min,
1505			phba->ktime_seg4_max);
1506	len += scnprintf(buf + len, PAGE_SIZE-len,
1507			"Segment 5: MSI-X ISR for cmd cmpl "
1508			"-to- NVME layer passed cmd done\n");
1509	len += scnprintf(buf + len, PAGE_SIZE-len,
1510			"avg:%08lld min:%08lld max %08lld\n",
1511			div_u64(phba->ktime_seg5_total,
1512				phba->ktime_data_samples),
1513			phba->ktime_seg5_min,
1514			phba->ktime_seg5_max);
1515
1516	if (phba->ktime_status_samples == 0) {
1517		len += scnprintf(buf + len, PAGE_SIZE-len,
1518				"Total: cmd received by MSI-X ISR "
1519				"-to- cmd completed on wire\n");
1520		len += scnprintf(buf + len, PAGE_SIZE-len,
1521				"avg:%08lld min:%08lld "
1522				"max %08lld\n",
1523				div_u64(phba->ktime_seg10_total,
1524					phba->ktime_data_samples),
1525				phba->ktime_seg10_min,
1526				phba->ktime_seg10_max);
1527		return len;
1528	}
1529
1530	len += scnprintf(buf + len, PAGE_SIZE-len,
1531			"Segment 6: NVME layer passed cmd done "
1532			"-to- Driver rcv rsp status OP\n");
1533	len += scnprintf(buf + len, PAGE_SIZE-len,
1534			"avg:%08lld min:%08lld max %08lld\n",
1535			div_u64(phba->ktime_seg6_total,
1536				phba->ktime_status_samples),
1537			phba->ktime_seg6_min,
1538			phba->ktime_seg6_max);
1539	len += scnprintf(buf + len, PAGE_SIZE-len,
1540			"Segment 7: Driver rcv rsp status OP "
1541			"-to- Firmware WQ doorbell: status\n");
1542	len += scnprintf(buf + len, PAGE_SIZE-len,
1543			"avg:%08lld min:%08lld max %08lld\n",
1544			div_u64(phba->ktime_seg7_total,
1545				phba->ktime_status_samples),
1546			phba->ktime_seg7_min,
1547			phba->ktime_seg7_max);
1548	len += scnprintf(buf + len, PAGE_SIZE-len,
1549			"Segment 8: Firmware WQ doorbell: status"
1550			" -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			div_u64(phba->ktime_seg8_total,
1554				phba->ktime_status_samples),
1555			phba->ktime_seg8_min,
1556			phba->ktime_seg8_max);
1557	len += scnprintf(buf + len, PAGE_SIZE-len,
1558			"Segment 9: MSI-X ISR for status cmpl  "
1559			"-to- NVME layer passed status done\n");
1560	len += scnprintf(buf + len, PAGE_SIZE-len,
1561			"avg:%08lld min:%08lld max %08lld\n",
1562			div_u64(phba->ktime_seg9_total,
1563				phba->ktime_status_samples),
1564			phba->ktime_seg9_min,
1565			phba->ktime_seg9_max);
1566	len += scnprintf(buf + len, PAGE_SIZE-len,
1567			"Total: cmd received by MSI-X ISR -to- "
1568			"cmd completed on wire\n");
1569	len += scnprintf(buf + len, PAGE_SIZE-len,
1570			"avg:%08lld min:%08lld max %08lld\n",
1571			div_u64(phba->ktime_seg10_total,
1572				phba->ktime_status_samples),
1573			phba->ktime_seg10_min,
1574			phba->ktime_seg10_max);
1575	return len;
1576}
1577
1578/**
1579 * lpfc_debugfs_nvmeio_trc_data - Dump NVME IO trace list to a buffer
1580 * @phba: The phba to gather target node info from.
1581 * @buf: The buffer to dump log into.
1582 * @size: The maximum amount of data to process.
1583 *
1584 * Description:
1585 * This routine dumps the NVME IO trace associated with @phba
1586 *
1587 * Return Value:
1588 * This routine returns the amount of bytes that were dumped into @buf and will
1589 * not exceed @size.
1590 **/
1591static int
1592lpfc_debugfs_nvmeio_trc_data(struct lpfc_hba *phba, char *buf, int size)
1593{
1594	struct lpfc_debugfs_nvmeio_trc *dtp;
1595	int i, state, index, skip;
1596	int len = 0;
1597
1598	state = phba->nvmeio_trc_on;
1599
1600	index = (atomic_read(&phba->nvmeio_trc_cnt) + 1) &
1601		(phba->nvmeio_trc_size - 1);
1602	skip = phba->nvmeio_trc_output_idx;
1603
1604	len += scnprintf(buf + len, size - len,
1605			"%s IO Trace %s: next_idx %d skip %d size %d\n",
1606			(phba->nvmet_support ? "NVME" : "NVMET"),
1607			(state ? "Enabled" : "Disabled"),
1608			index, skip, phba->nvmeio_trc_size);
1609
1610	if (!phba->nvmeio_trc || state)
1611		return len;
1612
1613	/* trace MUST bhe off to continue */
1614
1615	for (i = index; i < phba->nvmeio_trc_size; i++) {
1616		if (skip) {
1617			skip--;
1618			continue;
1619		}
1620		dtp = phba->nvmeio_trc + i;
1621		phba->nvmeio_trc_output_idx++;
1622
1623		if (!dtp->fmt)
1624			continue;
1625
1626		len +=  scnprintf(buf + len, size - len, dtp->fmt,
1627			dtp->data1, dtp->data2, dtp->data3);
1628
1629		if (phba->nvmeio_trc_output_idx >= phba->nvmeio_trc_size) {
1630			phba->nvmeio_trc_output_idx = 0;
1631			len += scnprintf(buf + len, size - len,
1632					"Trace Complete\n");
1633			goto out;
1634		}
1635
1636		if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ)) {
1637			len += scnprintf(buf + len, size - len,
1638					"Trace Continue (%d of %d)\n",
1639					phba->nvmeio_trc_output_idx,
1640					phba->nvmeio_trc_size);
1641			goto out;
1642		}
1643	}
1644	for (i = 0; i < index; i++) {
1645		if (skip) {
1646			skip--;
1647			continue;
1648		}
1649		dtp = phba->nvmeio_trc + i;
1650		phba->nvmeio_trc_output_idx++;
1651
1652		if (!dtp->fmt)
1653			continue;
1654
1655		len +=  scnprintf(buf + len, size - len, dtp->fmt,
1656			dtp->data1, dtp->data2, dtp->data3);
1657
1658		if (phba->nvmeio_trc_output_idx >= phba->nvmeio_trc_size) {
1659			phba->nvmeio_trc_output_idx = 0;
1660			len += scnprintf(buf + len, size - len,
1661					"Trace Complete\n");
1662			goto out;
1663		}
1664
1665		if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ)) {
1666			len += scnprintf(buf + len, size - len,
1667					"Trace Continue (%d of %d)\n",
1668					phba->nvmeio_trc_output_idx,
1669					phba->nvmeio_trc_size);
1670			goto out;
1671		}
1672	}
1673
1674	len += scnprintf(buf + len, size - len,
1675			"Trace Done\n");
1676out:
1677	return len;
1678}
1679
1680/**
1681 * lpfc_debugfs_hdwqstat_data - Dump I/O stats to a buffer
1682 * @vport: The vport to gather target node info from.
1683 * @buf: The buffer to dump log into.
1684 * @size: The maximum amount of data to process.
1685 *
1686 * Description:
1687 * This routine dumps the NVME + SCSI statistics associated with @vport
1688 *
1689 * Return Value:
1690 * This routine returns the amount of bytes that were dumped into @buf and will
1691 * not exceed @size.
1692 **/
1693static int
1694lpfc_debugfs_hdwqstat_data(struct lpfc_vport *vport, char *buf, int size)
1695{
1696	struct lpfc_hba   *phba = vport->phba;
1697	struct lpfc_hdwq_stat *c_stat;
1698	int i, j, len;
 
1699	uint32_t tot_xmt;
1700	uint32_t tot_rcv;
1701	uint32_t tot_cmpl;
1702	char tmp[LPFC_MAX_SCSI_INFO_TMP_LEN] = {0};
1703
1704	scnprintf(tmp, sizeof(tmp), "HDWQ Stats:\n\n");
1705	if (strlcat(buf, tmp, size) >= size)
1706		goto buffer_done;
1707
1708	scnprintf(tmp, sizeof(tmp), "(NVME Accounting: %s) ",
1709		  (phba->hdwqstat_on &
1710		  (LPFC_CHECK_NVME_IO | LPFC_CHECK_NVMET_IO) ?
1711		  "Enabled" : "Disabled"));
1712	if (strlcat(buf, tmp, size) >= size)
1713		goto buffer_done;
1714
1715	scnprintf(tmp, sizeof(tmp), "(SCSI Accounting: %s) ",
1716		  (phba->hdwqstat_on & LPFC_CHECK_SCSI_IO ?
1717		  "Enabled" : "Disabled"));
1718	if (strlcat(buf, tmp, size) >= size)
1719		goto buffer_done;
1720
1721	scnprintf(tmp, sizeof(tmp), "\n\n");
1722	if (strlcat(buf, tmp, size) >= size)
1723		goto buffer_done;
 
 
 
 
 
 
 
 
 
 
1724
1725	for (i = 0; i < phba->cfg_hdw_queue; i++) {
 
 
1726		tot_rcv = 0;
1727		tot_xmt = 0;
1728		tot_cmpl = 0;
 
 
 
 
 
 
1729
1730		for_each_present_cpu(j) {
1731			c_stat = per_cpu_ptr(phba->sli4_hba.c_stat, j);
1732
1733			/* Only display for this HDWQ */
1734			if (i != c_stat->hdwq_no)
1735				continue;
1736
 
 
 
1737			/* Only display non-zero counters */
1738			if (!c_stat->xmt_io && !c_stat->cmpl_io &&
1739			    !c_stat->rcv_io)
 
1740				continue;
1741
1742			if (!tot_xmt && !tot_cmpl && !tot_rcv) {
1743				/* Print HDWQ string only the first time */
1744				scnprintf(tmp, sizeof(tmp), "[HDWQ %d]:\t", i);
1745				if (strlcat(buf, tmp, size) >= size)
1746					goto buffer_done;
1747			}
1748
1749			tot_xmt += c_stat->xmt_io;
1750			tot_cmpl += c_stat->cmpl_io;
1751			if (phba->nvmet_support)
1752				tot_rcv += c_stat->rcv_io;
1753
1754			scnprintf(tmp, sizeof(tmp), "| [CPU %d]: ", j);
1755			if (strlcat(buf, tmp, size) >= size)
1756				goto buffer_done;
1757
1758			if (phba->nvmet_support) {
1759				scnprintf(tmp, sizeof(tmp),
1760					  "XMT 0x%x CMPL 0x%x RCV 0x%x |",
1761					  c_stat->xmt_io, c_stat->cmpl_io,
1762					  c_stat->rcv_io);
1763				if (strlcat(buf, tmp, size) >= size)
1764					goto buffer_done;
1765			} else {
1766				scnprintf(tmp, sizeof(tmp),
1767					  "XMT 0x%x CMPL 0x%x |",
1768					  c_stat->xmt_io, c_stat->cmpl_io);
1769				if (strlcat(buf, tmp, size) >= size)
1770					goto buffer_done;
1771			}
1772		}
1773
1774		/* Check if nothing to display */
1775		if (!tot_xmt && !tot_cmpl && !tot_rcv)
1776			continue;
1777
1778		scnprintf(tmp, sizeof(tmp), "\t->\t[HDWQ Total: ");
1779		if (strlcat(buf, tmp, size) >= size)
1780			goto buffer_done;
1781
1782		if (phba->nvmet_support) {
1783			scnprintf(tmp, sizeof(tmp),
1784				  "XMT 0x%x CMPL 0x%x RCV 0x%x]\n\n",
1785				  tot_xmt, tot_cmpl, tot_rcv);
1786			if (strlcat(buf, tmp, size) >= size)
1787				goto buffer_done;
1788		} else {
1789			scnprintf(tmp, sizeof(tmp),
1790				  "XMT 0x%x CMPL 0x%x]\n\n",
1791				  tot_xmt, tot_cmpl);
1792			if (strlcat(buf, tmp, size) >= size)
1793				goto buffer_done;
1794		}
1795	}
1796
1797buffer_done:
1798	len = strnlen(buf, size);
1799	return len;
1800}
1801
1802#endif
1803
1804/**
1805 * lpfc_debugfs_disc_trc - Store discovery trace log
1806 * @vport: The vport to associate this trace string with for retrieval.
1807 * @mask: Log entry classification.
1808 * @fmt: Format string to be displayed when dumping the log.
1809 * @data1: 1st data parameter to be applied to @fmt.
1810 * @data2: 2nd data parameter to be applied to @fmt.
1811 * @data3: 3rd data parameter to be applied to @fmt.
1812 *
1813 * Description:
1814 * This routine is used by the driver code to add a debugfs log entry to the
1815 * discovery trace buffer associated with @vport. Only entries with a @mask that
1816 * match the current debugfs discovery mask will be saved. Entries that do not
1817 * match will be thrown away. @fmt, @data1, @data2, and @data3 are used like
1818 * printf when displaying the log.
1819 **/
1820inline void
1821lpfc_debugfs_disc_trc(struct lpfc_vport *vport, int mask, char *fmt,
1822	uint32_t data1, uint32_t data2, uint32_t data3)
1823{
1824#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1825	struct lpfc_debugfs_trc *dtp;
1826	int index;
1827
1828	if (!(lpfc_debugfs_mask_disc_trc & mask))
1829		return;
1830
1831	if (!lpfc_debugfs_enable || !lpfc_debugfs_max_disc_trc ||
1832		!vport || !vport->disc_trc)
1833		return;
1834
1835	index = atomic_inc_return(&vport->disc_trc_cnt) &
1836		(lpfc_debugfs_max_disc_trc - 1);
1837	dtp = vport->disc_trc + index;
1838	dtp->fmt = fmt;
1839	dtp->data1 = data1;
1840	dtp->data2 = data2;
1841	dtp->data3 = data3;
1842	dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_seq_trc_cnt);
1843	dtp->jif = jiffies;
1844#endif
1845	return;
1846}
1847
1848/**
1849 * lpfc_debugfs_slow_ring_trc - Store slow ring trace log
1850 * @phba: The phba to associate this trace string with for retrieval.
1851 * @fmt: Format string to be displayed when dumping the log.
1852 * @data1: 1st data parameter to be applied to @fmt.
1853 * @data2: 2nd data parameter to be applied to @fmt.
1854 * @data3: 3rd data parameter to be applied to @fmt.
1855 *
1856 * Description:
1857 * This routine is used by the driver code to add a debugfs log entry to the
1858 * discovery trace buffer associated with @vport. @fmt, @data1, @data2, and
1859 * @data3 are used like printf when displaying the log.
1860 **/
1861inline void
1862lpfc_debugfs_slow_ring_trc(struct lpfc_hba *phba, char *fmt,
1863	uint32_t data1, uint32_t data2, uint32_t data3)
1864{
1865#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1866	struct lpfc_debugfs_trc *dtp;
1867	int index;
1868
1869	if (!lpfc_debugfs_enable || !lpfc_debugfs_max_slow_ring_trc ||
1870		!phba || !phba->slow_ring_trc)
1871		return;
1872
1873	index = atomic_inc_return(&phba->slow_ring_trc_cnt) &
1874		(lpfc_debugfs_max_slow_ring_trc - 1);
1875	dtp = phba->slow_ring_trc + index;
1876	dtp->fmt = fmt;
1877	dtp->data1 = data1;
1878	dtp->data2 = data2;
1879	dtp->data3 = data3;
1880	dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_seq_trc_cnt);
1881	dtp->jif = jiffies;
1882#endif
1883	return;
1884}
1885
1886/**
1887 * lpfc_debugfs_nvme_trc - Store NVME/NVMET trace log
1888 * @phba: The phba to associate this trace string with for retrieval.
1889 * @fmt: Format string to be displayed when dumping the log.
1890 * @data1: 1st data parameter to be applied to @fmt.
1891 * @data2: 2nd data parameter to be applied to @fmt.
1892 * @data3: 3rd data parameter to be applied to @fmt.
1893 *
1894 * Description:
1895 * This routine is used by the driver code to add a debugfs log entry to the
1896 * nvme trace buffer associated with @phba. @fmt, @data1, @data2, and
1897 * @data3 are used like printf when displaying the log.
1898 **/
1899inline void
1900lpfc_debugfs_nvme_trc(struct lpfc_hba *phba, char *fmt,
1901		      uint16_t data1, uint16_t data2, uint32_t data3)
1902{
1903#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1904	struct lpfc_debugfs_nvmeio_trc *dtp;
1905	int index;
1906
1907	if (!phba->nvmeio_trc_on || !phba->nvmeio_trc)
1908		return;
1909
1910	index = atomic_inc_return(&phba->nvmeio_trc_cnt) &
1911		(phba->nvmeio_trc_size - 1);
1912	dtp = phba->nvmeio_trc + index;
1913	dtp->fmt = fmt;
1914	dtp->data1 = data1;
1915	dtp->data2 = data2;
1916	dtp->data3 = data3;
1917#endif
1918}
1919
1920#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1921/**
1922 * lpfc_debugfs_disc_trc_open - Open the discovery trace log
1923 * @inode: The inode pointer that contains a vport pointer.
1924 * @file: The file pointer to attach the log output.
1925 *
1926 * Description:
1927 * This routine is the entry point for the debugfs open file operation. It gets
1928 * the vport from the i_private field in @inode, allocates the necessary buffer
1929 * for the log, fills the buffer from the in-memory log for this vport, and then
1930 * returns a pointer to that log in the private_data field in @file.
1931 *
1932 * Returns:
1933 * This function returns zero if successful. On error it will return a negative
1934 * error value.
1935 **/
1936static int
1937lpfc_debugfs_disc_trc_open(struct inode *inode, struct file *file)
1938{
1939	struct lpfc_vport *vport = inode->i_private;
1940	struct lpfc_debug *debug;
1941	int size;
1942	int rc = -ENOMEM;
1943
1944	if (!lpfc_debugfs_max_disc_trc) {
1945		rc = -ENOSPC;
1946		goto out;
1947	}
1948
1949	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1950	if (!debug)
1951		goto out;
1952
1953	/* Round to page boundary */
1954	size =  (lpfc_debugfs_max_disc_trc * LPFC_DEBUG_TRC_ENTRY_SIZE);
1955	size = PAGE_ALIGN(size);
1956
1957	debug->buffer = kmalloc(size, GFP_KERNEL);
1958	if (!debug->buffer) {
1959		kfree(debug);
1960		goto out;
1961	}
1962
1963	debug->len = lpfc_debugfs_disc_trc_data(vport, debug->buffer, size);
1964	file->private_data = debug;
1965
1966	rc = 0;
1967out:
1968	return rc;
1969}
1970
1971/**
1972 * lpfc_debugfs_slow_ring_trc_open - Open the Slow Ring trace log
1973 * @inode: The inode pointer that contains a vport pointer.
1974 * @file: The file pointer to attach the log output.
1975 *
1976 * Description:
1977 * This routine is the entry point for the debugfs open file operation. It gets
1978 * the vport from the i_private field in @inode, allocates the necessary buffer
1979 * for the log, fills the buffer from the in-memory log for this vport, and then
1980 * returns a pointer to that log in the private_data field in @file.
1981 *
1982 * Returns:
1983 * This function returns zero if successful. On error it will return a negative
1984 * error value.
1985 **/
1986static int
1987lpfc_debugfs_slow_ring_trc_open(struct inode *inode, struct file *file)
1988{
1989	struct lpfc_hba *phba = inode->i_private;
1990	struct lpfc_debug *debug;
1991	int size;
1992	int rc = -ENOMEM;
1993
1994	if (!lpfc_debugfs_max_slow_ring_trc) {
1995		rc = -ENOSPC;
1996		goto out;
1997	}
1998
1999	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2000	if (!debug)
2001		goto out;
2002
2003	/* Round to page boundary */
2004	size =  (lpfc_debugfs_max_slow_ring_trc * LPFC_DEBUG_TRC_ENTRY_SIZE);
2005	size = PAGE_ALIGN(size);
2006
2007	debug->buffer = kmalloc(size, GFP_KERNEL);
2008	if (!debug->buffer) {
2009		kfree(debug);
2010		goto out;
2011	}
2012
2013	debug->len = lpfc_debugfs_slow_ring_trc_data(phba, debug->buffer, size);
2014	file->private_data = debug;
2015
2016	rc = 0;
2017out:
2018	return rc;
2019}
2020
2021/**
2022 * lpfc_debugfs_hbqinfo_open - Open the hbqinfo debugfs buffer
2023 * @inode: The inode pointer that contains a vport pointer.
2024 * @file: The file pointer to attach the log output.
2025 *
2026 * Description:
2027 * This routine is the entry point for the debugfs open file operation. It gets
2028 * the vport from the i_private field in @inode, allocates the necessary buffer
2029 * for the log, fills the buffer from the in-memory log for this vport, and then
2030 * returns a pointer to that log in the private_data field in @file.
2031 *
2032 * Returns:
2033 * This function returns zero if successful. On error it will return a negative
2034 * error value.
2035 **/
2036static int
2037lpfc_debugfs_hbqinfo_open(struct inode *inode, struct file *file)
2038{
2039	struct lpfc_hba *phba = inode->i_private;
2040	struct lpfc_debug *debug;
2041	int rc = -ENOMEM;
2042
2043	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2044	if (!debug)
2045		goto out;
2046
2047	/* Round to page boundary */
2048	debug->buffer = kmalloc(LPFC_HBQINFO_SIZE, GFP_KERNEL);
2049	if (!debug->buffer) {
2050		kfree(debug);
2051		goto out;
2052	}
2053
2054	debug->len = lpfc_debugfs_hbqinfo_data(phba, debug->buffer,
2055		LPFC_HBQINFO_SIZE);
2056	file->private_data = debug;
2057
2058	rc = 0;
2059out:
2060	return rc;
2061}
2062
2063/**
2064 * lpfc_debugfs_multixripools_open - Open the multixripool debugfs buffer
2065 * @inode: The inode pointer that contains a hba pointer.
2066 * @file: The file pointer to attach the log output.
2067 *
2068 * Description:
2069 * This routine is the entry point for the debugfs open file operation. It gets
2070 * the hba from the i_private field in @inode, allocates the necessary buffer
2071 * for the log, fills the buffer from the in-memory log for this hba, and then
2072 * returns a pointer to that log in the private_data field in @file.
2073 *
2074 * Returns:
2075 * This function returns zero if successful. On error it will return a negative
2076 * error value.
2077 **/
2078static int
2079lpfc_debugfs_multixripools_open(struct inode *inode, struct file *file)
2080{
2081	struct lpfc_hba *phba = inode->i_private;
2082	struct lpfc_debug *debug;
2083	int rc = -ENOMEM;
2084
2085	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2086	if (!debug)
2087		goto out;
2088
2089	/* Round to page boundary */
2090	debug->buffer = kzalloc(LPFC_DUMP_MULTIXRIPOOL_SIZE, GFP_KERNEL);
2091	if (!debug->buffer) {
2092		kfree(debug);
2093		goto out;
2094	}
2095
2096	debug->len = lpfc_debugfs_multixripools_data(
2097		phba, debug->buffer, LPFC_DUMP_MULTIXRIPOOL_SIZE);
2098
2099	debug->i_private = inode->i_private;
2100	file->private_data = debug;
2101
2102	rc = 0;
2103out:
2104	return rc;
2105}
2106
2107#ifdef LPFC_HDWQ_LOCK_STAT
2108/**
2109 * lpfc_debugfs_lockstat_open - Open the lockstat debugfs buffer
2110 * @inode: The inode pointer that contains a vport pointer.
2111 * @file: The file pointer to attach the log output.
2112 *
2113 * Description:
2114 * This routine is the entry point for the debugfs open file operation. It gets
2115 * the vport from the i_private field in @inode, allocates the necessary buffer
2116 * for the log, fills the buffer from the in-memory log for this vport, and then
2117 * returns a pointer to that log in the private_data field in @file.
2118 *
2119 * Returns:
2120 * This function returns zero if successful. On error it will return a negative
2121 * error value.
2122 **/
2123static int
2124lpfc_debugfs_lockstat_open(struct inode *inode, struct file *file)
2125{
2126	struct lpfc_hba *phba = inode->i_private;
2127	struct lpfc_debug *debug;
2128	int rc = -ENOMEM;
2129
2130	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2131	if (!debug)
2132		goto out;
2133
2134	/* Round to page boundary */
2135	debug->buffer = kmalloc(LPFC_HDWQINFO_SIZE, GFP_KERNEL);
2136	if (!debug->buffer) {
2137		kfree(debug);
2138		goto out;
2139	}
2140
2141	debug->len = lpfc_debugfs_lockstat_data(phba, debug->buffer,
2142		LPFC_HBQINFO_SIZE);
2143	file->private_data = debug;
2144
2145	rc = 0;
2146out:
2147	return rc;
2148}
2149
2150static ssize_t
2151lpfc_debugfs_lockstat_write(struct file *file, const char __user *buf,
2152			    size_t nbytes, loff_t *ppos)
2153{
2154	struct lpfc_debug *debug = file->private_data;
2155	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2156	struct lpfc_sli4_hdw_queue *qp;
2157	char mybuf[64];
2158	char *pbuf;
2159	int i;
2160	size_t bsize;
2161
2162	memset(mybuf, 0, sizeof(mybuf));
 
 
2163
2164	bsize = min(nbytes, (sizeof(mybuf) - 1));
2165
2166	if (copy_from_user(mybuf, buf, bsize))
2167		return -EFAULT;
2168	pbuf = &mybuf[0];
2169
2170	if ((strncmp(pbuf, "reset", strlen("reset")) == 0) ||
2171	    (strncmp(pbuf, "zero", strlen("zero")) == 0)) {
2172		for (i = 0; i < phba->cfg_hdw_queue; i++) {
2173			qp = &phba->sli4_hba.hdwq[i];
2174			qp->lock_conflict.alloc_xri_get = 0;
2175			qp->lock_conflict.alloc_xri_put = 0;
2176			qp->lock_conflict.free_xri = 0;
2177			qp->lock_conflict.wq_access = 0;
2178			qp->lock_conflict.alloc_pvt_pool = 0;
2179			qp->lock_conflict.mv_from_pvt_pool = 0;
2180			qp->lock_conflict.mv_to_pub_pool = 0;
2181			qp->lock_conflict.mv_to_pvt_pool = 0;
2182			qp->lock_conflict.free_pvt_pool = 0;
2183			qp->lock_conflict.free_pub_pool = 0;
2184			qp->lock_conflict.wq_access = 0;
2185		}
2186	}
2187	return bsize;
2188}
2189#endif
2190
2191static int lpfc_debugfs_ras_log_data(struct lpfc_hba *phba,
2192				     char *buffer, int size)
2193{
2194	int copied = 0;
2195	struct lpfc_dmabuf *dmabuf, *next;
2196
2197	memset(buffer, 0, size);
2198
2199	spin_lock_irq(&phba->hbalock);
2200	if (phba->ras_fwlog.state != ACTIVE) {
2201		spin_unlock_irq(&phba->hbalock);
2202		return -EINVAL;
2203	}
2204	spin_unlock_irq(&phba->hbalock);
2205
2206	list_for_each_entry_safe(dmabuf, next,
2207				 &phba->ras_fwlog.fwlog_buff_list, list) {
2208		/* Check if copying will go over size and a '\0' char */
2209		if ((copied + LPFC_RAS_MAX_ENTRY_SIZE) >= (size - 1)) {
2210			memcpy(buffer + copied, dmabuf->virt,
2211			       size - copied - 1);
2212			copied += size - copied - 1;
2213			break;
2214		}
2215		memcpy(buffer + copied, dmabuf->virt, LPFC_RAS_MAX_ENTRY_SIZE);
2216		copied += LPFC_RAS_MAX_ENTRY_SIZE;
2217	}
2218	return copied;
2219}
2220
2221static int
2222lpfc_debugfs_ras_log_release(struct inode *inode, struct file *file)
2223{
2224	struct lpfc_debug *debug = file->private_data;
2225
2226	vfree(debug->buffer);
2227	kfree(debug);
2228
2229	return 0;
2230}
2231
2232/**
2233 * lpfc_debugfs_ras_log_open - Open the RAS log debugfs buffer
2234 * @inode: The inode pointer that contains a vport pointer.
2235 * @file: The file pointer to attach the log output.
2236 *
2237 * Description:
2238 * This routine is the entry point for the debugfs open file operation. It gets
2239 * the vport from the i_private field in @inode, allocates the necessary buffer
2240 * for the log, fills the buffer from the in-memory log for this vport, and then
2241 * returns a pointer to that log in the private_data field in @file.
2242 *
2243 * Returns:
2244 * This function returns zero if successful. On error it will return a negative
2245 * error value.
2246 **/
2247static int
2248lpfc_debugfs_ras_log_open(struct inode *inode, struct file *file)
2249{
2250	struct lpfc_hba *phba = inode->i_private;
2251	struct lpfc_debug *debug;
2252	int size;
2253	int rc = -ENOMEM;
2254
2255	spin_lock_irq(&phba->hbalock);
2256	if (phba->ras_fwlog.state != ACTIVE) {
2257		spin_unlock_irq(&phba->hbalock);
2258		rc = -EINVAL;
2259		goto out;
2260	}
2261	spin_unlock_irq(&phba->hbalock);
2262
2263	if (check_mul_overflow(LPFC_RAS_MIN_BUFF_POST_SIZE,
2264			       phba->cfg_ras_fwlog_buffsize, &size))
2265		goto out;
2266
2267	debug = kzalloc(sizeof(*debug), GFP_KERNEL);
2268	if (!debug)
2269		goto out;
2270
2271	debug->buffer = vmalloc(size);
2272	if (!debug->buffer)
2273		goto free_debug;
2274
2275	debug->len = lpfc_debugfs_ras_log_data(phba, debug->buffer, size);
2276	if (debug->len < 0) {
2277		rc = -EINVAL;
2278		goto free_buffer;
2279	}
2280	file->private_data = debug;
2281
2282	return 0;
2283
2284free_buffer:
2285	vfree(debug->buffer);
2286free_debug:
2287	kfree(debug);
2288out:
2289	return rc;
2290}
2291
2292/**
2293 * lpfc_debugfs_dumpHBASlim_open - Open the Dump HBA SLIM debugfs buffer
2294 * @inode: The inode pointer that contains a vport pointer.
2295 * @file: The file pointer to attach the log output.
2296 *
2297 * Description:
2298 * This routine is the entry point for the debugfs open file operation. It gets
2299 * the vport from the i_private field in @inode, allocates the necessary buffer
2300 * for the log, fills the buffer from the in-memory log for this vport, and then
2301 * returns a pointer to that log in the private_data field in @file.
2302 *
2303 * Returns:
2304 * This function returns zero if successful. On error it will return a negative
2305 * error value.
2306 **/
2307static int
2308lpfc_debugfs_dumpHBASlim_open(struct inode *inode, struct file *file)
2309{
2310	struct lpfc_hba *phba = inode->i_private;
2311	struct lpfc_debug *debug;
2312	int rc = -ENOMEM;
2313
2314	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2315	if (!debug)
2316		goto out;
2317
2318	/* Round to page boundary */
2319	debug->buffer = kmalloc(LPFC_DUMPHBASLIM_SIZE, GFP_KERNEL);
2320	if (!debug->buffer) {
2321		kfree(debug);
2322		goto out;
2323	}
2324
2325	debug->len = lpfc_debugfs_dumpHBASlim_data(phba, debug->buffer,
2326		LPFC_DUMPHBASLIM_SIZE);
2327	file->private_data = debug;
2328
2329	rc = 0;
2330out:
2331	return rc;
2332}
2333
2334/**
2335 * lpfc_debugfs_dumpHostSlim_open - Open the Dump Host SLIM debugfs buffer
2336 * @inode: The inode pointer that contains a vport pointer.
2337 * @file: The file pointer to attach the log output.
2338 *
2339 * Description:
2340 * This routine is the entry point for the debugfs open file operation. It gets
2341 * the vport from the i_private field in @inode, allocates the necessary buffer
2342 * for the log, fills the buffer from the in-memory log for this vport, and then
2343 * returns a pointer to that log in the private_data field in @file.
2344 *
2345 * Returns:
2346 * This function returns zero if successful. On error it will return a negative
2347 * error value.
2348 **/
2349static int
2350lpfc_debugfs_dumpHostSlim_open(struct inode *inode, struct file *file)
2351{
2352	struct lpfc_hba *phba = inode->i_private;
2353	struct lpfc_debug *debug;
2354	int rc = -ENOMEM;
2355
2356	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2357	if (!debug)
2358		goto out;
2359
2360	/* Round to page boundary */
2361	debug->buffer = kmalloc(LPFC_DUMPHOSTSLIM_SIZE, GFP_KERNEL);
2362	if (!debug->buffer) {
2363		kfree(debug);
2364		goto out;
2365	}
2366
2367	debug->len = lpfc_debugfs_dumpHostSlim_data(phba, debug->buffer,
2368		LPFC_DUMPHOSTSLIM_SIZE);
2369	file->private_data = debug;
2370
2371	rc = 0;
2372out:
2373	return rc;
2374}
2375
2376static ssize_t
2377lpfc_debugfs_dif_err_read(struct file *file, char __user *buf,
2378	size_t nbytes, loff_t *ppos)
2379{
2380	struct dentry *dent = file->f_path.dentry;
2381	struct lpfc_hba *phba = file->private_data;
2382	char cbuf[32];
2383	uint64_t tmp = 0;
2384	int cnt = 0;
2385
2386	if (dent == phba->debug_writeGuard)
2387		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wgrd_cnt);
2388	else if (dent == phba->debug_writeApp)
2389		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wapp_cnt);
2390	else if (dent == phba->debug_writeRef)
2391		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wref_cnt);
2392	else if (dent == phba->debug_readGuard)
2393		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rgrd_cnt);
2394	else if (dent == phba->debug_readApp)
2395		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rapp_cnt);
2396	else if (dent == phba->debug_readRef)
2397		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rref_cnt);
2398	else if (dent == phba->debug_InjErrNPortID)
2399		cnt = scnprintf(cbuf, 32, "0x%06x\n",
2400				phba->lpfc_injerr_nportid);
2401	else if (dent == phba->debug_InjErrWWPN) {
2402		memcpy(&tmp, &phba->lpfc_injerr_wwpn, sizeof(struct lpfc_name));
2403		tmp = cpu_to_be64(tmp);
2404		cnt = scnprintf(cbuf, 32, "0x%016llx\n", tmp);
2405	} else if (dent == phba->debug_InjErrLBA) {
2406		if (phba->lpfc_injerr_lba == (sector_t)(-1))
2407			cnt = scnprintf(cbuf, 32, "off\n");
2408		else
2409			cnt = scnprintf(cbuf, 32, "0x%llx\n",
2410				 (uint64_t) phba->lpfc_injerr_lba);
2411	} else
2412		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2413			 "0547 Unknown debugfs error injection entry\n");
2414
2415	return simple_read_from_buffer(buf, nbytes, ppos, &cbuf, cnt);
2416}
2417
2418static ssize_t
2419lpfc_debugfs_dif_err_write(struct file *file, const char __user *buf,
2420	size_t nbytes, loff_t *ppos)
2421{
2422	struct dentry *dent = file->f_path.dentry;
2423	struct lpfc_hba *phba = file->private_data;
2424	char dstbuf[33];
2425	uint64_t tmp = 0;
2426	int size;
2427
2428	memset(dstbuf, 0, 33);
2429	size = (nbytes < 32) ? nbytes : 32;
2430	if (copy_from_user(dstbuf, buf, size))
2431		return -EFAULT;
2432
2433	if (dent == phba->debug_InjErrLBA) {
2434		if ((dstbuf[0] == 'o') && (dstbuf[1] == 'f') &&
2435		    (dstbuf[2] == 'f'))
2436			tmp = (uint64_t)(-1);
2437	}
2438
2439	if ((tmp == 0) && (kstrtoull(dstbuf, 0, &tmp)))
2440		return -EINVAL;
2441
2442	if (dent == phba->debug_writeGuard)
2443		phba->lpfc_injerr_wgrd_cnt = (uint32_t)tmp;
2444	else if (dent == phba->debug_writeApp)
2445		phba->lpfc_injerr_wapp_cnt = (uint32_t)tmp;
2446	else if (dent == phba->debug_writeRef)
2447		phba->lpfc_injerr_wref_cnt = (uint32_t)tmp;
2448	else if (dent == phba->debug_readGuard)
2449		phba->lpfc_injerr_rgrd_cnt = (uint32_t)tmp;
2450	else if (dent == phba->debug_readApp)
2451		phba->lpfc_injerr_rapp_cnt = (uint32_t)tmp;
2452	else if (dent == phba->debug_readRef)
2453		phba->lpfc_injerr_rref_cnt = (uint32_t)tmp;
2454	else if (dent == phba->debug_InjErrLBA)
2455		phba->lpfc_injerr_lba = (sector_t)tmp;
2456	else if (dent == phba->debug_InjErrNPortID)
2457		phba->lpfc_injerr_nportid = (uint32_t)(tmp & Mask_DID);
2458	else if (dent == phba->debug_InjErrWWPN) {
2459		tmp = cpu_to_be64(tmp);
2460		memcpy(&phba->lpfc_injerr_wwpn, &tmp, sizeof(struct lpfc_name));
2461	} else
2462		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2463			 "0548 Unknown debugfs error injection entry\n");
2464
2465	return nbytes;
2466}
2467
2468static int
2469lpfc_debugfs_dif_err_release(struct inode *inode, struct file *file)
2470{
2471	return 0;
2472}
2473
2474/**
2475 * lpfc_debugfs_nodelist_open - Open the nodelist debugfs file
2476 * @inode: The inode pointer that contains a vport pointer.
2477 * @file: The file pointer to attach the log output.
2478 *
2479 * Description:
2480 * This routine is the entry point for the debugfs open file operation. It gets
2481 * the vport from the i_private field in @inode, allocates the necessary buffer
2482 * for the log, fills the buffer from the in-memory log for this vport, and then
2483 * returns a pointer to that log in the private_data field in @file.
2484 *
2485 * Returns:
2486 * This function returns zero if successful. On error it will return a negative
2487 * error value.
2488 **/
2489static int
2490lpfc_debugfs_nodelist_open(struct inode *inode, struct file *file)
2491{
2492	struct lpfc_vport *vport = inode->i_private;
2493	struct lpfc_debug *debug;
2494	int rc = -ENOMEM;
2495
2496	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2497	if (!debug)
2498		goto out;
2499
2500	/* Round to page boundary */
2501	debug->buffer = kmalloc(LPFC_NODELIST_SIZE, GFP_KERNEL);
2502	if (!debug->buffer) {
2503		kfree(debug);
2504		goto out;
2505	}
2506
2507	debug->len = lpfc_debugfs_nodelist_data(vport, debug->buffer,
2508		LPFC_NODELIST_SIZE);
2509	file->private_data = debug;
2510
2511	rc = 0;
2512out:
2513	return rc;
2514}
2515
2516/**
2517 * lpfc_debugfs_lseek - Seek through a debugfs file
2518 * @file: The file pointer to seek through.
2519 * @off: The offset to seek to or the amount to seek by.
2520 * @whence: Indicates how to seek.
2521 *
2522 * Description:
2523 * This routine is the entry point for the debugfs lseek file operation. The
2524 * @whence parameter indicates whether @off is the offset to directly seek to,
2525 * or if it is a value to seek forward or reverse by. This function figures out
2526 * what the new offset of the debugfs file will be and assigns that value to the
2527 * f_pos field of @file.
2528 *
2529 * Returns:
2530 * This function returns the new offset if successful and returns a negative
2531 * error if unable to process the seek.
2532 **/
2533static loff_t
2534lpfc_debugfs_lseek(struct file *file, loff_t off, int whence)
2535{
2536	struct lpfc_debug *debug = file->private_data;
2537	return fixed_size_llseek(file, off, whence, debug->len);
2538}
2539
2540/**
2541 * lpfc_debugfs_read - Read a debugfs file
2542 * @file: The file pointer to read from.
2543 * @buf: The buffer to copy the data to.
2544 * @nbytes: The number of bytes to read.
2545 * @ppos: The position in the file to start reading from.
2546 *
2547 * Description:
2548 * This routine reads data from from the buffer indicated in the private_data
2549 * field of @file. It will start reading at @ppos and copy up to @nbytes of
2550 * data to @buf.
2551 *
2552 * Returns:
2553 * This function returns the amount of data that was read (this could be less
2554 * than @nbytes if the end of the file was reached) or a negative error value.
2555 **/
2556static ssize_t
2557lpfc_debugfs_read(struct file *file, char __user *buf,
2558		  size_t nbytes, loff_t *ppos)
2559{
2560	struct lpfc_debug *debug = file->private_data;
2561
2562	return simple_read_from_buffer(buf, nbytes, ppos, debug->buffer,
2563				       debug->len);
2564}
2565
2566/**
2567 * lpfc_debugfs_release - Release the buffer used to store debugfs file data
2568 * @inode: The inode pointer that contains a vport pointer. (unused)
2569 * @file: The file pointer that contains the buffer to release.
2570 *
2571 * Description:
2572 * This routine frees the buffer that was allocated when the debugfs file was
2573 * opened.
2574 *
2575 * Returns:
2576 * This function returns zero.
2577 **/
2578static int
2579lpfc_debugfs_release(struct inode *inode, struct file *file)
2580{
2581	struct lpfc_debug *debug = file->private_data;
2582
2583	kfree(debug->buffer);
2584	kfree(debug);
2585
2586	return 0;
2587}
2588
2589/**
2590 * lpfc_debugfs_multixripools_write - Clear multi-XRI pools statistics
2591 * @file: The file pointer to read from.
2592 * @buf: The buffer to copy the user data from.
2593 * @nbytes: The number of bytes to get.
2594 * @ppos: The position in the file to start reading from.
2595 *
2596 * Description:
2597 * This routine clears multi-XRI pools statistics when buf contains "clear".
2598 *
2599 * Return Value:
2600 * It returns the @nbytges passing in from debugfs user space when successful.
2601 * In case of error conditions, it returns proper error code back to the user
2602 * space.
2603 **/
2604static ssize_t
2605lpfc_debugfs_multixripools_write(struct file *file, const char __user *buf,
2606				 size_t nbytes, loff_t *ppos)
2607{
2608	struct lpfc_debug *debug = file->private_data;
2609	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2610	char mybuf[64];
2611	char *pbuf;
2612	u32 i;
2613	u32 hwq_count;
2614	struct lpfc_sli4_hdw_queue *qp;
2615	struct lpfc_multixri_pool *multixri_pool;
2616
2617	if (nbytes > sizeof(mybuf) - 1)
2618		nbytes = sizeof(mybuf) - 1;
 
 
 
 
2619
2620	memset(mybuf, 0, sizeof(mybuf));
2621
2622	if (copy_from_user(mybuf, buf, nbytes))
2623		return -EFAULT;
2624	pbuf = &mybuf[0];
2625
2626	if ((strncmp(pbuf, "clear", strlen("clear"))) == 0) {
2627		hwq_count = phba->cfg_hdw_queue;
2628		for (i = 0; i < hwq_count; i++) {
2629			qp = &phba->sli4_hba.hdwq[i];
2630			multixri_pool = qp->p_multixri_pool;
2631			if (!multixri_pool)
2632				continue;
2633
2634			qp->empty_io_bufs = 0;
2635			multixri_pool->pbl_empty_count = 0;
2636#ifdef LPFC_MXP_STAT
2637			multixri_pool->above_limit_count = 0;
2638			multixri_pool->below_limit_count = 0;
2639			multixri_pool->stat_max_hwm = 0;
2640			multixri_pool->local_pbl_hit_count = 0;
2641			multixri_pool->other_pbl_hit_count = 0;
2642
2643			multixri_pool->stat_pbl_count = 0;
2644			multixri_pool->stat_pvt_count = 0;
2645			multixri_pool->stat_busy_count = 0;
2646			multixri_pool->stat_snapshot_taken = 0;
2647#endif
2648		}
2649		return strlen(pbuf);
2650	}
2651
2652	return -EINVAL;
2653}
2654
2655static int
2656lpfc_debugfs_nvmestat_open(struct inode *inode, struct file *file)
2657{
2658	struct lpfc_vport *vport = inode->i_private;
2659	struct lpfc_debug *debug;
2660	int rc = -ENOMEM;
2661
2662	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2663	if (!debug)
2664		goto out;
2665
2666	 /* Round to page boundary */
2667	debug->buffer = kmalloc(LPFC_NVMESTAT_SIZE, GFP_KERNEL);
2668	if (!debug->buffer) {
2669		kfree(debug);
2670		goto out;
2671	}
2672
2673	debug->len = lpfc_debugfs_nvmestat_data(vport, debug->buffer,
2674		LPFC_NVMESTAT_SIZE);
2675
2676	debug->i_private = inode->i_private;
2677	file->private_data = debug;
2678
2679	rc = 0;
2680out:
2681	return rc;
2682}
2683
2684static ssize_t
2685lpfc_debugfs_nvmestat_write(struct file *file, const char __user *buf,
2686			    size_t nbytes, loff_t *ppos)
2687{
2688	struct lpfc_debug *debug = file->private_data;
2689	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2690	struct lpfc_hba   *phba = vport->phba;
2691	struct lpfc_nvmet_tgtport *tgtp;
2692	char mybuf[64];
2693	char *pbuf;
2694
2695	if (!phba->targetport)
2696		return -ENXIO;
2697
2698	if (nbytes > sizeof(mybuf) - 1)
2699		nbytes = sizeof(mybuf) - 1;
2700
2701	memset(mybuf, 0, sizeof(mybuf));
2702
2703	if (copy_from_user(mybuf, buf, nbytes))
2704		return -EFAULT;
2705	pbuf = &mybuf[0];
2706
2707	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2708	if ((strncmp(pbuf, "reset", strlen("reset")) == 0) ||
2709	    (strncmp(pbuf, "zero", strlen("zero")) == 0)) {
2710		atomic_set(&tgtp->rcv_ls_req_in, 0);
2711		atomic_set(&tgtp->rcv_ls_req_out, 0);
2712		atomic_set(&tgtp->rcv_ls_req_drop, 0);
2713		atomic_set(&tgtp->xmt_ls_abort, 0);
2714		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
2715		atomic_set(&tgtp->xmt_ls_rsp, 0);
2716		atomic_set(&tgtp->xmt_ls_drop, 0);
2717		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
2718		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
2719
2720		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
2721		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
2722		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
2723		atomic_set(&tgtp->xmt_fcp_drop, 0);
2724		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
2725		atomic_set(&tgtp->xmt_fcp_read, 0);
2726		atomic_set(&tgtp->xmt_fcp_write, 0);
2727		atomic_set(&tgtp->xmt_fcp_rsp, 0);
2728		atomic_set(&tgtp->xmt_fcp_release, 0);
2729		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
2730		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
2731		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
2732
2733		atomic_set(&tgtp->xmt_fcp_abort, 0);
2734		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
2735		atomic_set(&tgtp->xmt_abort_sol, 0);
2736		atomic_set(&tgtp->xmt_abort_unsol, 0);
2737		atomic_set(&tgtp->xmt_abort_rsp, 0);
2738		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
2739	}
2740	return nbytes;
2741}
2742
2743static int
2744lpfc_debugfs_scsistat_open(struct inode *inode, struct file *file)
2745{
2746	struct lpfc_vport *vport = inode->i_private;
2747	struct lpfc_debug *debug;
2748	int rc = -ENOMEM;
2749
2750	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2751	if (!debug)
2752		goto out;
2753
2754	 /* Round to page boundary */
2755	debug->buffer = kzalloc(LPFC_SCSISTAT_SIZE, GFP_KERNEL);
2756	if (!debug->buffer) {
2757		kfree(debug);
2758		goto out;
2759	}
2760
2761	debug->len = lpfc_debugfs_scsistat_data(vport, debug->buffer,
2762		LPFC_SCSISTAT_SIZE);
2763
2764	debug->i_private = inode->i_private;
2765	file->private_data = debug;
2766
2767	rc = 0;
2768out:
2769	return rc;
2770}
2771
2772static ssize_t
2773lpfc_debugfs_scsistat_write(struct file *file, const char __user *buf,
2774			    size_t nbytes, loff_t *ppos)
2775{
2776	struct lpfc_debug *debug = file->private_data;
2777	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2778	struct lpfc_hba *phba = vport->phba;
2779	char mybuf[6] = {0};
2780	int i;
2781
 
 
 
 
2782	if (copy_from_user(mybuf, buf, (nbytes >= sizeof(mybuf)) ?
2783				       (sizeof(mybuf) - 1) : nbytes))
2784		return -EFAULT;
2785
2786	if ((strncmp(&mybuf[0], "reset", strlen("reset")) == 0) ||
2787	    (strncmp(&mybuf[0], "zero", strlen("zero")) == 0)) {
2788		for (i = 0; i < phba->cfg_hdw_queue; i++) {
2789			memset(&phba->sli4_hba.hdwq[i].scsi_cstat, 0,
2790			       sizeof(phba->sli4_hba.hdwq[i].scsi_cstat));
2791		}
2792	}
2793
2794	return nbytes;
2795}
2796
2797static int
2798lpfc_debugfs_ioktime_open(struct inode *inode, struct file *file)
2799{
2800	struct lpfc_vport *vport = inode->i_private;
2801	struct lpfc_debug *debug;
2802	int rc = -ENOMEM;
2803
2804	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2805	if (!debug)
2806		goto out;
2807
2808	 /* Round to page boundary */
2809	debug->buffer = kmalloc(LPFC_IOKTIME_SIZE, GFP_KERNEL);
2810	if (!debug->buffer) {
2811		kfree(debug);
2812		goto out;
2813	}
2814
2815	debug->len = lpfc_debugfs_ioktime_data(vport, debug->buffer,
2816		LPFC_IOKTIME_SIZE);
2817
2818	debug->i_private = inode->i_private;
2819	file->private_data = debug;
2820
2821	rc = 0;
2822out:
2823	return rc;
2824}
2825
2826static ssize_t
2827lpfc_debugfs_ioktime_write(struct file *file, const char __user *buf,
2828			   size_t nbytes, loff_t *ppos)
2829{
2830	struct lpfc_debug *debug = file->private_data;
2831	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2832	struct lpfc_hba   *phba = vport->phba;
2833	char mybuf[64];
2834	char *pbuf;
2835
2836	if (nbytes > sizeof(mybuf) - 1)
2837		nbytes = sizeof(mybuf) - 1;
2838
2839	memset(mybuf, 0, sizeof(mybuf));
2840
2841	if (copy_from_user(mybuf, buf, nbytes))
2842		return -EFAULT;
2843	pbuf = &mybuf[0];
2844
2845	if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2846		phba->ktime_data_samples = 0;
2847		phba->ktime_status_samples = 0;
2848		phba->ktime_seg1_total = 0;
2849		phba->ktime_seg1_max = 0;
2850		phba->ktime_seg1_min = 0xffffffff;
2851		phba->ktime_seg2_total = 0;
2852		phba->ktime_seg2_max = 0;
2853		phba->ktime_seg2_min = 0xffffffff;
2854		phba->ktime_seg3_total = 0;
2855		phba->ktime_seg3_max = 0;
2856		phba->ktime_seg3_min = 0xffffffff;
2857		phba->ktime_seg4_total = 0;
2858		phba->ktime_seg4_max = 0;
2859		phba->ktime_seg4_min = 0xffffffff;
2860		phba->ktime_seg5_total = 0;
2861		phba->ktime_seg5_max = 0;
2862		phba->ktime_seg5_min = 0xffffffff;
2863		phba->ktime_seg6_total = 0;
2864		phba->ktime_seg6_max = 0;
2865		phba->ktime_seg6_min = 0xffffffff;
2866		phba->ktime_seg7_total = 0;
2867		phba->ktime_seg7_max = 0;
2868		phba->ktime_seg7_min = 0xffffffff;
2869		phba->ktime_seg8_total = 0;
2870		phba->ktime_seg8_max = 0;
2871		phba->ktime_seg8_min = 0xffffffff;
2872		phba->ktime_seg9_total = 0;
2873		phba->ktime_seg9_max = 0;
2874		phba->ktime_seg9_min = 0xffffffff;
2875		phba->ktime_seg10_total = 0;
2876		phba->ktime_seg10_max = 0;
2877		phba->ktime_seg10_min = 0xffffffff;
2878
2879		phba->ktime_on = 1;
2880		return strlen(pbuf);
2881	} else if ((strncmp(pbuf, "off",
2882		   sizeof("off") - 1) == 0)) {
2883		phba->ktime_on = 0;
2884		return strlen(pbuf);
2885	} else if ((strncmp(pbuf, "zero",
2886		   sizeof("zero") - 1) == 0)) {
2887		phba->ktime_data_samples = 0;
2888		phba->ktime_status_samples = 0;
2889		phba->ktime_seg1_total = 0;
2890		phba->ktime_seg1_max = 0;
2891		phba->ktime_seg1_min = 0xffffffff;
2892		phba->ktime_seg2_total = 0;
2893		phba->ktime_seg2_max = 0;
2894		phba->ktime_seg2_min = 0xffffffff;
2895		phba->ktime_seg3_total = 0;
2896		phba->ktime_seg3_max = 0;
2897		phba->ktime_seg3_min = 0xffffffff;
2898		phba->ktime_seg4_total = 0;
2899		phba->ktime_seg4_max = 0;
2900		phba->ktime_seg4_min = 0xffffffff;
2901		phba->ktime_seg5_total = 0;
2902		phba->ktime_seg5_max = 0;
2903		phba->ktime_seg5_min = 0xffffffff;
2904		phba->ktime_seg6_total = 0;
2905		phba->ktime_seg6_max = 0;
2906		phba->ktime_seg6_min = 0xffffffff;
2907		phba->ktime_seg7_total = 0;
2908		phba->ktime_seg7_max = 0;
2909		phba->ktime_seg7_min = 0xffffffff;
2910		phba->ktime_seg8_total = 0;
2911		phba->ktime_seg8_max = 0;
2912		phba->ktime_seg8_min = 0xffffffff;
2913		phba->ktime_seg9_total = 0;
2914		phba->ktime_seg9_max = 0;
2915		phba->ktime_seg9_min = 0xffffffff;
2916		phba->ktime_seg10_total = 0;
2917		phba->ktime_seg10_max = 0;
2918		phba->ktime_seg10_min = 0xffffffff;
2919		return strlen(pbuf);
2920	}
2921	return -EINVAL;
2922}
2923
2924static int
2925lpfc_debugfs_nvmeio_trc_open(struct inode *inode, struct file *file)
2926{
2927	struct lpfc_hba *phba = inode->i_private;
2928	struct lpfc_debug *debug;
2929	int rc = -ENOMEM;
2930
2931	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2932	if (!debug)
2933		goto out;
2934
2935	 /* Round to page boundary */
2936	debug->buffer = kmalloc(LPFC_NVMEIO_TRC_SIZE, GFP_KERNEL);
2937	if (!debug->buffer) {
2938		kfree(debug);
2939		goto out;
2940	}
2941
2942	debug->len = lpfc_debugfs_nvmeio_trc_data(phba, debug->buffer,
2943		LPFC_NVMEIO_TRC_SIZE);
2944
2945	debug->i_private = inode->i_private;
2946	file->private_data = debug;
2947
2948	rc = 0;
2949out:
2950	return rc;
2951}
2952
2953static ssize_t
2954lpfc_debugfs_nvmeio_trc_write(struct file *file, const char __user *buf,
2955			      size_t nbytes, loff_t *ppos)
2956{
2957	struct lpfc_debug *debug = file->private_data;
2958	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2959	int i;
2960	unsigned long sz;
2961	char mybuf[64];
2962	char *pbuf;
2963
2964	if (nbytes > sizeof(mybuf) - 1)
2965		nbytes = sizeof(mybuf) - 1;
2966
2967	memset(mybuf, 0, sizeof(mybuf));
2968
2969	if (copy_from_user(mybuf, buf, nbytes))
2970		return -EFAULT;
2971	pbuf = &mybuf[0];
2972
2973	if ((strncmp(pbuf, "off", sizeof("off") - 1) == 0)) {
2974		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2975				"0570 nvmeio_trc_off\n");
2976		phba->nvmeio_trc_output_idx = 0;
2977		phba->nvmeio_trc_on = 0;
2978		return strlen(pbuf);
2979	} else if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2980		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2981				"0571 nvmeio_trc_on\n");
2982		phba->nvmeio_trc_output_idx = 0;
2983		phba->nvmeio_trc_on = 1;
2984		return strlen(pbuf);
2985	}
2986
2987	/* We must be off to allocate the trace buffer */
2988	if (phba->nvmeio_trc_on != 0)
2989		return -EINVAL;
2990
2991	/* If not on or off, the parameter is the trace buffer size */
2992	i = kstrtoul(pbuf, 0, &sz);
2993	if (i)
2994		return -EINVAL;
2995	phba->nvmeio_trc_size = (uint32_t)sz;
2996
2997	/* It must be a power of 2 - round down */
2998	i = 0;
2999	while (sz > 1) {
3000		sz = sz >> 1;
3001		i++;
3002	}
3003	sz = (1 << i);
3004	if (phba->nvmeio_trc_size != sz)
3005		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3006				"0572 nvmeio_trc_size changed to %ld\n",
3007				sz);
3008	phba->nvmeio_trc_size = (uint32_t)sz;
3009
3010	/* If one previously exists, free it */
3011	kfree(phba->nvmeio_trc);
3012
3013	/* Allocate new trace buffer and initialize */
3014	phba->nvmeio_trc = kzalloc((sizeof(struct lpfc_debugfs_nvmeio_trc) *
3015				    sz), GFP_KERNEL);
3016	if (!phba->nvmeio_trc) {
3017		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3018				"0573 Cannot create debugfs "
3019				"nvmeio_trc buffer\n");
3020		return -ENOMEM;
3021	}
3022	atomic_set(&phba->nvmeio_trc_cnt, 0);
3023	phba->nvmeio_trc_on = 0;
3024	phba->nvmeio_trc_output_idx = 0;
3025
3026	return strlen(pbuf);
3027}
3028
3029static int
3030lpfc_debugfs_hdwqstat_open(struct inode *inode, struct file *file)
3031{
3032	struct lpfc_vport *vport = inode->i_private;
3033	struct lpfc_debug *debug;
3034	int rc = -ENOMEM;
3035
3036	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
3037	if (!debug)
3038		goto out;
3039
3040	 /* Round to page boundary */
3041	debug->buffer = kcalloc(1, LPFC_SCSISTAT_SIZE, GFP_KERNEL);
3042	if (!debug->buffer) {
3043		kfree(debug);
3044		goto out;
3045	}
3046
3047	debug->len = lpfc_debugfs_hdwqstat_data(vport, debug->buffer,
3048						LPFC_SCSISTAT_SIZE);
3049
3050	debug->i_private = inode->i_private;
3051	file->private_data = debug;
3052
3053	rc = 0;
3054out:
3055	return rc;
3056}
3057
3058static ssize_t
3059lpfc_debugfs_hdwqstat_write(struct file *file, const char __user *buf,
3060			    size_t nbytes, loff_t *ppos)
3061{
3062	struct lpfc_debug *debug = file->private_data;
3063	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
3064	struct lpfc_hba   *phba = vport->phba;
3065	struct lpfc_hdwq_stat *c_stat;
3066	char mybuf[64];
3067	char *pbuf;
3068	int i;
3069
3070	if (nbytes > sizeof(mybuf) - 1)
3071		nbytes = sizeof(mybuf) - 1;
3072
3073	memset(mybuf, 0, sizeof(mybuf));
3074
3075	if (copy_from_user(mybuf, buf, nbytes))
3076		return -EFAULT;
3077	pbuf = &mybuf[0];
3078
3079	if ((strncmp(pbuf, "on", sizeof("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				LPFC_CHECK_SCSI_IO);
3085		return strlen(pbuf);
3086	} else if ((strncmp(pbuf, "nvme_on", sizeof("nvme_on") - 1) == 0)) {
3087		if (phba->nvmet_support)
3088			phba->hdwqstat_on |= LPFC_CHECK_NVMET_IO;
3089		else
3090			phba->hdwqstat_on |= LPFC_CHECK_NVME_IO;
3091		return strlen(pbuf);
3092	} else if ((strncmp(pbuf, "scsi_on", sizeof("scsi_on") - 1) == 0)) {
3093		if (!phba->nvmet_support)
3094			phba->hdwqstat_on |= LPFC_CHECK_SCSI_IO;
3095		return strlen(pbuf);
3096	} else if ((strncmp(pbuf, "nvme_off", sizeof("nvme_off") - 1) == 0)) {
3097		phba->hdwqstat_on &= ~(LPFC_CHECK_NVME_IO |
3098				       LPFC_CHECK_NVMET_IO);
3099		return strlen(pbuf);
3100	} else if ((strncmp(pbuf, "scsi_off", sizeof("scsi_off") - 1) == 0)) {
3101		phba->hdwqstat_on &= ~LPFC_CHECK_SCSI_IO;
 
 
 
 
3102		return strlen(pbuf);
3103	} else if ((strncmp(pbuf, "off",
3104		   sizeof("off") - 1) == 0)) {
3105		phba->hdwqstat_on = LPFC_CHECK_OFF;
3106		return strlen(pbuf);
3107	} else if ((strncmp(pbuf, "zero",
3108		   sizeof("zero") - 1) == 0)) {
3109		for_each_present_cpu(i) {
3110			c_stat = per_cpu_ptr(phba->sli4_hba.c_stat, i);
3111			c_stat->xmt_io = 0;
3112			c_stat->cmpl_io = 0;
3113			c_stat->rcv_io = 0;
 
 
 
3114		}
3115		return strlen(pbuf);
3116	}
3117	return -EINVAL;
3118}
3119
3120/*
3121 * ---------------------------------
3122 * iDiag debugfs file access methods
3123 * ---------------------------------
3124 *
3125 * All access methods are through the proper SLI4 PCI function's debugfs
3126 * iDiag directory:
3127 *
3128 *     /sys/kernel/debug/lpfc/fn<#>/iDiag
3129 */
3130
3131/**
3132 * lpfc_idiag_cmd_get - Get and parse idiag debugfs comands from user space
3133 * @buf: The pointer to the user space buffer.
3134 * @nbytes: The number of bytes in the user space buffer.
3135 * @idiag_cmd: pointer to the idiag command struct.
3136 *
3137 * This routine reads data from debugfs user space buffer and parses the
3138 * buffer for getting the idiag command and arguments. The while space in
3139 * between the set of data is used as the parsing separator.
3140 *
3141 * This routine returns 0 when successful, it returns proper error code
3142 * back to the user space in error conditions.
3143 */
3144static int lpfc_idiag_cmd_get(const char __user *buf, size_t nbytes,
3145			      struct lpfc_idiag_cmd *idiag_cmd)
3146{
3147	char mybuf[64];
3148	char *pbuf, *step_str;
3149	int i;
3150	size_t bsize;
3151
3152	memset(mybuf, 0, sizeof(mybuf));
3153	memset(idiag_cmd, 0, sizeof(*idiag_cmd));
3154	bsize = min(nbytes, (sizeof(mybuf)-1));
3155
3156	if (copy_from_user(mybuf, buf, bsize))
3157		return -EFAULT;
3158	pbuf = &mybuf[0];
3159	step_str = strsep(&pbuf, "\t ");
3160
3161	/* The opcode must present */
3162	if (!step_str)
3163		return -EINVAL;
3164
3165	idiag_cmd->opcode = simple_strtol(step_str, NULL, 0);
3166	if (idiag_cmd->opcode == 0)
3167		return -EINVAL;
3168
3169	for (i = 0; i < LPFC_IDIAG_CMD_DATA_SIZE; i++) {
3170		step_str = strsep(&pbuf, "\t ");
3171		if (!step_str)
3172			return i;
3173		idiag_cmd->data[i] = simple_strtol(step_str, NULL, 0);
3174	}
3175	return i;
3176}
3177
3178/**
3179 * lpfc_idiag_open - idiag open debugfs
3180 * @inode: The inode pointer that contains a pointer to phba.
3181 * @file: The file pointer to attach the file operation.
3182 *
3183 * Description:
3184 * This routine is the entry point for the debugfs open file operation. It
3185 * gets the reference to phba from the i_private field in @inode, it then
3186 * allocates buffer for the file operation, performs the necessary PCI config
3187 * space read into the allocated buffer according to the idiag user command
3188 * setup, and then returns a pointer to buffer in the private_data field in
3189 * @file.
3190 *
3191 * Returns:
3192 * This function returns zero if successful. On error it will return an
3193 * negative error value.
3194 **/
3195static int
3196lpfc_idiag_open(struct inode *inode, struct file *file)
3197{
3198	struct lpfc_debug *debug;
3199
3200	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
3201	if (!debug)
3202		return -ENOMEM;
3203
3204	debug->i_private = inode->i_private;
3205	debug->buffer = NULL;
3206	file->private_data = debug;
3207
3208	return 0;
3209}
3210
3211/**
3212 * lpfc_idiag_release - Release idiag access file operation
3213 * @inode: The inode pointer that contains a vport pointer. (unused)
3214 * @file: The file pointer that contains the buffer to release.
3215 *
3216 * Description:
3217 * This routine is the generic release routine for the idiag access file
3218 * operation, it frees the buffer that was allocated when the debugfs file
3219 * was opened.
3220 *
3221 * Returns:
3222 * This function returns zero.
3223 **/
3224static int
3225lpfc_idiag_release(struct inode *inode, struct file *file)
3226{
3227	struct lpfc_debug *debug = file->private_data;
3228
3229	/* Free the buffers to the file operation */
3230	kfree(debug->buffer);
3231	kfree(debug);
3232
3233	return 0;
3234}
3235
3236/**
3237 * lpfc_idiag_cmd_release - Release idiag cmd access file operation
3238 * @inode: The inode pointer that contains a vport pointer. (unused)
3239 * @file: The file pointer that contains the buffer to release.
3240 *
3241 * Description:
3242 * This routine frees the buffer that was allocated when the debugfs file
3243 * was opened. It also reset the fields in the idiag command struct in the
3244 * case of command for write operation.
3245 *
3246 * Returns:
3247 * This function returns zero.
3248 **/
3249static int
3250lpfc_idiag_cmd_release(struct inode *inode, struct file *file)
3251{
3252	struct lpfc_debug *debug = file->private_data;
3253
3254	if (debug->op == LPFC_IDIAG_OP_WR) {
3255		switch (idiag.cmd.opcode) {
3256		case LPFC_IDIAG_CMD_PCICFG_WR:
3257		case LPFC_IDIAG_CMD_PCICFG_ST:
3258		case LPFC_IDIAG_CMD_PCICFG_CL:
3259		case LPFC_IDIAG_CMD_QUEACC_WR:
3260		case LPFC_IDIAG_CMD_QUEACC_ST:
3261		case LPFC_IDIAG_CMD_QUEACC_CL:
3262			memset(&idiag, 0, sizeof(idiag));
3263			break;
3264		default:
3265			break;
3266		}
3267	}
3268
3269	/* Free the buffers to the file operation */
3270	kfree(debug->buffer);
3271	kfree(debug);
3272
3273	return 0;
3274}
3275
3276/**
3277 * lpfc_idiag_pcicfg_read - idiag debugfs read pcicfg
3278 * @file: The file pointer to read from.
3279 * @buf: The buffer to copy the data to.
3280 * @nbytes: The number of bytes to read.
3281 * @ppos: The position in the file to start reading from.
3282 *
3283 * Description:
3284 * This routine reads data from the @phba pci config space according to the
3285 * idiag command, and copies to user @buf. Depending on the PCI config space
3286 * read command setup, it does either a single register read of a byte
3287 * (8 bits), a word (16 bits), or a dword (32 bits) or browsing through all
3288 * registers from the 4K extended PCI config space.
3289 *
3290 * Returns:
3291 * This function returns the amount of data that was read (this could be less
3292 * than @nbytes if the end of the file was reached) or a negative error value.
3293 **/
3294static ssize_t
3295lpfc_idiag_pcicfg_read(struct file *file, char __user *buf, size_t nbytes,
3296		       loff_t *ppos)
3297{
3298	struct lpfc_debug *debug = file->private_data;
3299	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3300	int offset_label, offset, len = 0, index = LPFC_PCI_CFG_RD_SIZE;
3301	int where, count;
3302	char *pbuffer;
3303	struct pci_dev *pdev;
3304	uint32_t u32val;
3305	uint16_t u16val;
3306	uint8_t u8val;
3307
3308	pdev = phba->pcidev;
3309	if (!pdev)
3310		return 0;
3311
3312	/* This is a user read operation */
3313	debug->op = LPFC_IDIAG_OP_RD;
3314
3315	if (!debug->buffer)
3316		debug->buffer = kmalloc(LPFC_PCI_CFG_SIZE, GFP_KERNEL);
3317	if (!debug->buffer)
3318		return 0;
3319	pbuffer = debug->buffer;
3320
3321	if (*ppos)
3322		return 0;
3323
3324	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
3325		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
3326		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
3327	} else
3328		return 0;
3329
3330	/* Read single PCI config space register */
3331	switch (count) {
3332	case SIZE_U8: /* byte (8 bits) */
3333		pci_read_config_byte(pdev, where, &u8val);
3334		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3335				"%03x: %02x\n", where, u8val);
3336		break;
3337	case SIZE_U16: /* word (16 bits) */
3338		pci_read_config_word(pdev, where, &u16val);
3339		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3340				"%03x: %04x\n", where, u16val);
3341		break;
3342	case SIZE_U32: /* double word (32 bits) */
3343		pci_read_config_dword(pdev, where, &u32val);
3344		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3345				"%03x: %08x\n", where, u32val);
3346		break;
3347	case LPFC_PCI_CFG_BROWSE: /* browse all */
3348		goto pcicfg_browse;
 
3349	default:
3350		/* illegal count */
3351		len = 0;
3352		break;
3353	}
3354	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3355
3356pcicfg_browse:
3357
3358	/* Browse all PCI config space registers */
3359	offset_label = idiag.offset.last_rd;
3360	offset = offset_label;
3361
3362	/* Read PCI config space */
3363	len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3364			"%03x: ", offset_label);
3365	while (index > 0) {
3366		pci_read_config_dword(pdev, offset, &u32val);
3367		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3368				"%08x ", u32val);
3369		offset += sizeof(uint32_t);
3370		if (offset >= LPFC_PCI_CFG_SIZE) {
3371			len += scnprintf(pbuffer+len,
3372					LPFC_PCI_CFG_SIZE-len, "\n");
3373			break;
3374		}
3375		index -= sizeof(uint32_t);
3376		if (!index)
3377			len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3378					"\n");
3379		else if (!(index % (8 * sizeof(uint32_t)))) {
3380			offset_label += (8 * sizeof(uint32_t));
3381			len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3382					"\n%03x: ", offset_label);
3383		}
3384	}
3385
3386	/* Set up the offset for next portion of pci cfg read */
3387	if (index == 0) {
3388		idiag.offset.last_rd += LPFC_PCI_CFG_RD_SIZE;
3389		if (idiag.offset.last_rd >= LPFC_PCI_CFG_SIZE)
3390			idiag.offset.last_rd = 0;
3391	} else
3392		idiag.offset.last_rd = 0;
3393
3394	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3395}
3396
3397/**
3398 * lpfc_idiag_pcicfg_write - Syntax check and set up idiag pcicfg commands
3399 * @file: The file pointer to read from.
3400 * @buf: The buffer to copy the user data from.
3401 * @nbytes: The number of bytes to get.
3402 * @ppos: The position in the file to start reading from.
3403 *
3404 * This routine get the debugfs idiag command struct from user space and
3405 * then perform the syntax check for PCI config space read or write command
3406 * accordingly. In the case of PCI config space read command, it sets up
3407 * the command in the idiag command struct for the debugfs read operation.
3408 * In the case of PCI config space write operation, it executes the write
3409 * operation into the PCI config space accordingly.
3410 *
3411 * It returns the @nbytges passing in from debugfs user space when successful.
3412 * In case of error conditions, it returns proper error code back to the user
3413 * space.
3414 */
3415static ssize_t
3416lpfc_idiag_pcicfg_write(struct file *file, const char __user *buf,
3417			size_t nbytes, loff_t *ppos)
3418{
3419	struct lpfc_debug *debug = file->private_data;
3420	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3421	uint32_t where, value, count;
3422	uint32_t u32val;
3423	uint16_t u16val;
3424	uint8_t u8val;
3425	struct pci_dev *pdev;
3426	int rc;
3427
3428	pdev = phba->pcidev;
3429	if (!pdev)
3430		return -EFAULT;
3431
3432	/* This is a user write operation */
3433	debug->op = LPFC_IDIAG_OP_WR;
3434
3435	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3436	if (rc < 0)
3437		return rc;
3438
3439	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
3440		/* Sanity check on PCI config read command line arguments */
3441		if (rc != LPFC_PCI_CFG_RD_CMD_ARG)
3442			goto error_out;
3443		/* Read command from PCI config space, set up command fields */
3444		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
3445		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
3446		if (count == LPFC_PCI_CFG_BROWSE) {
3447			if (where % sizeof(uint32_t))
3448				goto error_out;
3449			/* Starting offset to browse */
3450			idiag.offset.last_rd = where;
3451		} else if ((count != sizeof(uint8_t)) &&
3452			   (count != sizeof(uint16_t)) &&
3453			   (count != sizeof(uint32_t)))
3454			goto error_out;
3455		if (count == sizeof(uint8_t)) {
3456			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
3457				goto error_out;
3458			if (where % sizeof(uint8_t))
3459				goto error_out;
3460		}
3461		if (count == sizeof(uint16_t)) {
3462			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
3463				goto error_out;
3464			if (where % sizeof(uint16_t))
3465				goto error_out;
3466		}
3467		if (count == sizeof(uint32_t)) {
3468			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
3469				goto error_out;
3470			if (where % sizeof(uint32_t))
3471				goto error_out;
3472		}
3473	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR ||
3474		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST ||
3475		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3476		/* Sanity check on PCI config write command line arguments */
3477		if (rc != LPFC_PCI_CFG_WR_CMD_ARG)
3478			goto error_out;
3479		/* Write command to PCI config space, read-modify-write */
3480		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
3481		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
3482		value = idiag.cmd.data[IDIAG_PCICFG_VALUE_INDX];
3483		/* Sanity checks */
3484		if ((count != sizeof(uint8_t)) &&
3485		    (count != sizeof(uint16_t)) &&
3486		    (count != sizeof(uint32_t)))
3487			goto error_out;
3488		if (count == sizeof(uint8_t)) {
3489			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
3490				goto error_out;
3491			if (where % sizeof(uint8_t))
3492				goto error_out;
3493			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
3494				pci_write_config_byte(pdev, where,
3495						      (uint8_t)value);
3496			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
3497				rc = pci_read_config_byte(pdev, where, &u8val);
3498				if (!rc) {
3499					u8val |= (uint8_t)value;
3500					pci_write_config_byte(pdev, where,
3501							      u8val);
3502				}
3503			}
3504			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3505				rc = pci_read_config_byte(pdev, where, &u8val);
3506				if (!rc) {
3507					u8val &= (uint8_t)(~value);
3508					pci_write_config_byte(pdev, where,
3509							      u8val);
3510				}
3511			}
3512		}
3513		if (count == sizeof(uint16_t)) {
3514			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
3515				goto error_out;
3516			if (where % sizeof(uint16_t))
3517				goto error_out;
3518			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
3519				pci_write_config_word(pdev, where,
3520						      (uint16_t)value);
3521			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
3522				rc = pci_read_config_word(pdev, where, &u16val);
3523				if (!rc) {
3524					u16val |= (uint16_t)value;
3525					pci_write_config_word(pdev, where,
3526							      u16val);
3527				}
3528			}
3529			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3530				rc = pci_read_config_word(pdev, where, &u16val);
3531				if (!rc) {
3532					u16val &= (uint16_t)(~value);
3533					pci_write_config_word(pdev, where,
3534							      u16val);
3535				}
3536			}
3537		}
3538		if (count == sizeof(uint32_t)) {
3539			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
3540				goto error_out;
3541			if (where % sizeof(uint32_t))
3542				goto error_out;
3543			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
3544				pci_write_config_dword(pdev, where, value);
3545			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
3546				rc = pci_read_config_dword(pdev, where,
3547							   &u32val);
3548				if (!rc) {
3549					u32val |= value;
3550					pci_write_config_dword(pdev, where,
3551							       u32val);
3552				}
3553			}
3554			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3555				rc = pci_read_config_dword(pdev, where,
3556							   &u32val);
3557				if (!rc) {
3558					u32val &= ~value;
3559					pci_write_config_dword(pdev, where,
3560							       u32val);
3561				}
3562			}
3563		}
3564	} else
3565		/* All other opecodes are illegal for now */
3566		goto error_out;
3567
3568	return nbytes;
3569error_out:
3570	memset(&idiag, 0, sizeof(idiag));
3571	return -EINVAL;
3572}
3573
3574/**
3575 * lpfc_idiag_baracc_read - idiag debugfs pci bar access read
3576 * @file: The file pointer to read from.
3577 * @buf: The buffer to copy the data to.
3578 * @nbytes: The number of bytes to read.
3579 * @ppos: The position in the file to start reading from.
3580 *
3581 * Description:
3582 * This routine reads data from the @phba pci bar memory mapped space
3583 * according to the idiag command, and copies to user @buf.
3584 *
3585 * Returns:
3586 * This function returns the amount of data that was read (this could be less
3587 * than @nbytes if the end of the file was reached) or a negative error value.
3588 **/
3589static ssize_t
3590lpfc_idiag_baracc_read(struct file *file, char __user *buf, size_t nbytes,
3591		       loff_t *ppos)
3592{
3593	struct lpfc_debug *debug = file->private_data;
3594	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3595	int offset_label, offset, offset_run, len = 0, index;
3596	int bar_num, acc_range, bar_size;
3597	char *pbuffer;
3598	void __iomem *mem_mapped_bar;
3599	uint32_t if_type;
3600	struct pci_dev *pdev;
3601	uint32_t u32val;
3602
3603	pdev = phba->pcidev;
3604	if (!pdev)
3605		return 0;
3606
3607	/* This is a user read operation */
3608	debug->op = LPFC_IDIAG_OP_RD;
3609
3610	if (!debug->buffer)
3611		debug->buffer = kmalloc(LPFC_PCI_BAR_RD_BUF_SIZE, GFP_KERNEL);
3612	if (!debug->buffer)
3613		return 0;
3614	pbuffer = debug->buffer;
3615
3616	if (*ppos)
3617		return 0;
3618
3619	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
3620		bar_num   = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
3621		offset    = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
3622		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
3623		bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
3624	} else
3625		return 0;
3626
3627	if (acc_range == 0)
3628		return 0;
3629
3630	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
3631	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
3632		if (bar_num == IDIAG_BARACC_BAR_0)
3633			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3634		else if (bar_num == IDIAG_BARACC_BAR_1)
3635			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
3636		else if (bar_num == IDIAG_BARACC_BAR_2)
3637			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
3638		else
3639			return 0;
3640	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3641		if (bar_num == IDIAG_BARACC_BAR_0)
3642			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3643		else
3644			return 0;
3645	} else
3646		return 0;
3647
3648	/* Read single PCI bar space register */
3649	if (acc_range == SINGLE_WORD) {
3650		offset_run = offset;
3651		u32val = readl(mem_mapped_bar + offset_run);
3652		len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3653				"%05x: %08x\n", offset_run, u32val);
3654	} else
3655		goto baracc_browse;
3656
3657	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3658
3659baracc_browse:
3660
3661	/* Browse all PCI bar space registers */
3662	offset_label = idiag.offset.last_rd;
3663	offset_run = offset_label;
3664
3665	/* Read PCI bar memory mapped space */
3666	len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3667			"%05x: ", offset_label);
3668	index = LPFC_PCI_BAR_RD_SIZE;
3669	while (index > 0) {
3670		u32val = readl(mem_mapped_bar + offset_run);
3671		len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3672				"%08x ", u32val);
3673		offset_run += sizeof(uint32_t);
3674		if (acc_range == LPFC_PCI_BAR_BROWSE) {
3675			if (offset_run >= bar_size) {
3676				len += scnprintf(pbuffer+len,
3677					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
3678				break;
3679			}
3680		} else {
3681			if (offset_run >= offset +
3682			    (acc_range * sizeof(uint32_t))) {
3683				len += scnprintf(pbuffer+len,
3684					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
3685				break;
3686			}
3687		}
3688		index -= sizeof(uint32_t);
3689		if (!index)
3690			len += scnprintf(pbuffer+len,
3691					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
3692		else if (!(index % (8 * sizeof(uint32_t)))) {
3693			offset_label += (8 * sizeof(uint32_t));
3694			len += scnprintf(pbuffer+len,
3695					LPFC_PCI_BAR_RD_BUF_SIZE-len,
3696					"\n%05x: ", offset_label);
3697		}
3698	}
3699
3700	/* Set up the offset for next portion of pci bar read */
3701	if (index == 0) {
3702		idiag.offset.last_rd += LPFC_PCI_BAR_RD_SIZE;
3703		if (acc_range == LPFC_PCI_BAR_BROWSE) {
3704			if (idiag.offset.last_rd >= bar_size)
3705				idiag.offset.last_rd = 0;
3706		} else {
3707			if (offset_run >= offset +
3708			    (acc_range * sizeof(uint32_t)))
3709				idiag.offset.last_rd = offset;
3710		}
3711	} else {
3712		if (acc_range == LPFC_PCI_BAR_BROWSE)
3713			idiag.offset.last_rd = 0;
3714		else
3715			idiag.offset.last_rd = offset;
3716	}
3717
3718	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3719}
3720
3721/**
3722 * lpfc_idiag_baracc_write - Syntax check and set up idiag bar access commands
3723 * @file: The file pointer to read from.
3724 * @buf: The buffer to copy the user data from.
3725 * @nbytes: The number of bytes to get.
3726 * @ppos: The position in the file to start reading from.
3727 *
3728 * This routine get the debugfs idiag command struct from user space and
3729 * then perform the syntax check for PCI bar memory mapped space read or
3730 * write command accordingly. In the case of PCI bar memory mapped space
3731 * read command, it sets up the command in the idiag command struct for
3732 * the debugfs read operation. In the case of PCI bar memorpy mapped space
3733 * write operation, it executes the write operation into the PCI bar memory
3734 * mapped space accordingly.
3735 *
3736 * It returns the @nbytges passing in from debugfs user space when successful.
3737 * In case of error conditions, it returns proper error code back to the user
3738 * space.
3739 */
3740static ssize_t
3741lpfc_idiag_baracc_write(struct file *file, const char __user *buf,
3742			size_t nbytes, loff_t *ppos)
3743{
3744	struct lpfc_debug *debug = file->private_data;
3745	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3746	uint32_t bar_num, bar_size, offset, value, acc_range;
3747	struct pci_dev *pdev;
3748	void __iomem *mem_mapped_bar;
3749	uint32_t if_type;
3750	uint32_t u32val;
3751	int rc;
3752
3753	pdev = phba->pcidev;
3754	if (!pdev)
3755		return -EFAULT;
3756
3757	/* This is a user write operation */
3758	debug->op = LPFC_IDIAG_OP_WR;
3759
3760	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3761	if (rc < 0)
3762		return rc;
3763
3764	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
3765	bar_num = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
3766
3767	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
3768		if ((bar_num != IDIAG_BARACC_BAR_0) &&
3769		    (bar_num != IDIAG_BARACC_BAR_1) &&
3770		    (bar_num != IDIAG_BARACC_BAR_2))
3771			goto error_out;
3772	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3773		if (bar_num != IDIAG_BARACC_BAR_0)
3774			goto error_out;
3775	} else
3776		goto error_out;
3777
3778	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
3779		if (bar_num == IDIAG_BARACC_BAR_0) {
3780			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3781				LPFC_PCI_IF0_BAR0_SIZE;
3782			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3783		} else if (bar_num == IDIAG_BARACC_BAR_1) {
3784			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3785				LPFC_PCI_IF0_BAR1_SIZE;
3786			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
3787		} else if (bar_num == IDIAG_BARACC_BAR_2) {
3788			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3789				LPFC_PCI_IF0_BAR2_SIZE;
3790			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
3791		} else
3792			goto error_out;
3793	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3794		if (bar_num == IDIAG_BARACC_BAR_0) {
3795			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3796				LPFC_PCI_IF2_BAR0_SIZE;
3797			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3798		} else
3799			goto error_out;
3800	} else
3801		goto error_out;
3802
3803	offset = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
3804	if (offset % sizeof(uint32_t))
3805		goto error_out;
3806
3807	bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
3808	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
3809		/* Sanity check on PCI config read command line arguments */
3810		if (rc != LPFC_PCI_BAR_RD_CMD_ARG)
3811			goto error_out;
3812		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
3813		if (acc_range == LPFC_PCI_BAR_BROWSE) {
3814			if (offset > bar_size - sizeof(uint32_t))
3815				goto error_out;
3816			/* Starting offset to browse */
3817			idiag.offset.last_rd = offset;
3818		} else if (acc_range > SINGLE_WORD) {
3819			if (offset + acc_range * sizeof(uint32_t) > bar_size)
3820				goto error_out;
3821			/* Starting offset to browse */
3822			idiag.offset.last_rd = offset;
3823		} else if (acc_range != SINGLE_WORD)
3824			goto error_out;
3825	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR ||
3826		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST ||
3827		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
3828		/* Sanity check on PCI bar write command line arguments */
3829		if (rc != LPFC_PCI_BAR_WR_CMD_ARG)
3830			goto error_out;
3831		/* Write command to PCI bar space, read-modify-write */
3832		acc_range = SINGLE_WORD;
3833		value = idiag.cmd.data[IDIAG_BARACC_REG_VAL_INDX];
3834		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR) {
3835			writel(value, mem_mapped_bar + offset);
3836			readl(mem_mapped_bar + offset);
3837		}
3838		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST) {
3839			u32val = readl(mem_mapped_bar + offset);
3840			u32val |= value;
3841			writel(u32val, mem_mapped_bar + offset);
3842			readl(mem_mapped_bar + offset);
3843		}
3844		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
3845			u32val = readl(mem_mapped_bar + offset);
3846			u32val &= ~value;
3847			writel(u32val, mem_mapped_bar + offset);
3848			readl(mem_mapped_bar + offset);
3849		}
3850	} else
3851		/* All other opecodes are illegal for now */
3852		goto error_out;
3853
3854	return nbytes;
3855error_out:
3856	memset(&idiag, 0, sizeof(idiag));
3857	return -EINVAL;
3858}
3859
3860static int
3861__lpfc_idiag_print_wq(struct lpfc_queue *qp, char *wqtype,
3862			char *pbuffer, int len)
3863{
3864	if (!qp)
3865		return len;
3866
3867	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3868			"\t\t%s WQ info: ", wqtype);
3869	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3870			"AssocCQID[%04d]: WQ-STAT[oflow:x%x posted:x%llx]\n",
3871			qp->assoc_qid, qp->q_cnt_1,
3872			(unsigned long long)qp->q_cnt_4);
3873	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3874			"\t\tWQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3875			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]",
3876			qp->queue_id, qp->entry_count,
3877			qp->entry_size, qp->host_index,
3878			qp->hba_index, qp->notify_interval);
3879	len +=  scnprintf(pbuffer + len,
3880			LPFC_QUE_INFO_GET_BUF_SIZE - len, "\n");
3881	return len;
3882}
3883
3884static int
3885lpfc_idiag_wqs_for_cq(struct lpfc_hba *phba, char *wqtype, char *pbuffer,
3886		int *len, int max_cnt, int cq_id)
3887{
3888	struct lpfc_queue *qp;
3889	int qidx;
3890
3891	for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
3892		qp = phba->sli4_hba.hdwq[qidx].io_wq;
3893		if (qp->assoc_qid != cq_id)
3894			continue;
3895		*len = __lpfc_idiag_print_wq(qp, wqtype, pbuffer, *len);
3896		if (*len >= max_cnt)
3897			return 1;
3898	}
3899	return 0;
3900}
3901
3902static int
3903__lpfc_idiag_print_cq(struct lpfc_queue *qp, char *cqtype,
3904			char *pbuffer, int len)
3905{
3906	if (!qp)
3907		return len;
3908
3909	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3910			"\t%s CQ info: ", cqtype);
3911	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3912			"AssocEQID[%02d]: CQ STAT[max:x%x relw:x%x "
3913			"xabt:x%x wq:x%llx]\n",
3914			qp->assoc_qid, qp->q_cnt_1, qp->q_cnt_2,
3915			qp->q_cnt_3, (unsigned long long)qp->q_cnt_4);
3916	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3917			"\tCQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3918			"HST-IDX[%04d], NTFI[%03d], PLMT[%03d]",
3919			qp->queue_id, qp->entry_count,
3920			qp->entry_size, qp->host_index,
3921			qp->notify_interval, qp->max_proc_limit);
3922
3923	len +=  scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3924			"\n");
3925
3926	return len;
3927}
3928
3929static int
3930__lpfc_idiag_print_rqpair(struct lpfc_queue *qp, struct lpfc_queue *datqp,
3931			char *rqtype, char *pbuffer, int len)
3932{
3933	if (!qp || !datqp)
3934		return len;
3935
3936	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3937			"\t\t%s RQ info: ", rqtype);
3938	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3939			"AssocCQID[%02d]: RQ-STAT[nopost:x%x nobuf:x%x "
3940			"posted:x%x rcv:x%llx]\n",
3941			qp->assoc_qid, qp->q_cnt_1, qp->q_cnt_2,
3942			qp->q_cnt_3, (unsigned long long)qp->q_cnt_4);
3943	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3944			"\t\tHQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3945			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]\n",
3946			qp->queue_id, qp->entry_count, qp->entry_size,
3947			qp->host_index, qp->hba_index, qp->notify_interval);
3948	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3949			"\t\tDQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3950			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]\n",
3951			datqp->queue_id, datqp->entry_count,
3952			datqp->entry_size, datqp->host_index,
3953			datqp->hba_index, datqp->notify_interval);
3954	return len;
3955}
3956
3957static int
3958lpfc_idiag_cqs_for_eq(struct lpfc_hba *phba, char *pbuffer,
3959		int *len, int max_cnt, int eqidx, int eq_id)
3960{
3961	struct lpfc_queue *qp;
3962	int rc;
3963
3964	qp = phba->sli4_hba.hdwq[eqidx].io_cq;
3965
3966	*len = __lpfc_idiag_print_cq(qp, "IO", pbuffer, *len);
3967
3968	/* Reset max counter */
3969	qp->CQ_max_cqe = 0;
3970
3971	if (*len >= max_cnt)
3972		return 1;
3973
3974	rc = lpfc_idiag_wqs_for_cq(phba, "IO", pbuffer, len,
3975				   max_cnt, qp->queue_id);
3976	if (rc)
3977		return 1;
3978
3979	if ((eqidx < phba->cfg_nvmet_mrq) && phba->nvmet_support) {
3980		/* NVMET CQset */
3981		qp = phba->sli4_hba.nvmet_cqset[eqidx];
3982		*len = __lpfc_idiag_print_cq(qp, "NVMET CQset", pbuffer, *len);
3983
3984		/* Reset max counter */
3985		qp->CQ_max_cqe = 0;
3986
3987		if (*len >= max_cnt)
3988			return 1;
3989
3990		/* RQ header */
3991		qp = phba->sli4_hba.nvmet_mrq_hdr[eqidx];
3992		*len = __lpfc_idiag_print_rqpair(qp,
3993				phba->sli4_hba.nvmet_mrq_data[eqidx],
3994				"NVMET MRQ", pbuffer, *len);
3995
3996		if (*len >= max_cnt)
3997			return 1;
3998	}
3999
4000	return 0;
4001}
4002
4003static int
4004__lpfc_idiag_print_eq(struct lpfc_queue *qp, char *eqtype,
4005			char *pbuffer, int len)
4006{
4007	if (!qp)
4008		return len;
4009
4010	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
4011			"\n%s EQ info: EQ-STAT[max:x%x noE:x%x "
4012			"cqe_proc:x%x eqe_proc:x%llx eqd %d]\n",
4013			eqtype, qp->q_cnt_1, qp->q_cnt_2, qp->q_cnt_3,
4014			(unsigned long long)qp->q_cnt_4, qp->q_mode);
4015	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
4016			"EQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
4017			"HST-IDX[%04d], NTFI[%03d], PLMT[%03d], AFFIN[%03d]",
4018			qp->queue_id, qp->entry_count, qp->entry_size,
4019			qp->host_index, qp->notify_interval,
4020			qp->max_proc_limit, qp->chann);
4021	len +=  scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
4022			"\n");
4023
4024	return len;
4025}
4026
4027/**
4028 * lpfc_idiag_queinfo_read - idiag debugfs read queue information
4029 * @file: The file pointer to read from.
4030 * @buf: The buffer to copy the data to.
4031 * @nbytes: The number of bytes to read.
4032 * @ppos: The position in the file to start reading from.
4033 *
4034 * Description:
4035 * This routine reads data from the @phba SLI4 PCI function queue information,
4036 * and copies to user @buf.
4037 * This routine only returns 1 EQs worth of information. It remembers the last
4038 * EQ read and jumps to the next EQ. Thus subsequent calls to queInfo will
4039 * retrieve all EQs allocated for the phba.
4040 *
4041 * Returns:
4042 * This function returns the amount of data that was read (this could be less
4043 * than @nbytes if the end of the file was reached) or a negative error value.
4044 **/
4045static ssize_t
4046lpfc_idiag_queinfo_read(struct file *file, char __user *buf, size_t nbytes,
4047			loff_t *ppos)
4048{
4049	struct lpfc_debug *debug = file->private_data;
4050	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4051	char *pbuffer;
4052	int max_cnt, rc, x, len = 0;
4053	struct lpfc_queue *qp = NULL;
4054
4055	if (!debug->buffer)
4056		debug->buffer = kmalloc(LPFC_QUE_INFO_GET_BUF_SIZE, GFP_KERNEL);
4057	if (!debug->buffer)
4058		return 0;
4059	pbuffer = debug->buffer;
4060	max_cnt = LPFC_QUE_INFO_GET_BUF_SIZE - 256;
4061
4062	if (*ppos)
4063		return 0;
4064
4065	spin_lock_irq(&phba->hbalock);
4066
4067	/* Fast-path event queue */
4068	if (phba->sli4_hba.hdwq && phba->cfg_hdw_queue) {
4069
4070		x = phba->lpfc_idiag_last_eq;
4071		phba->lpfc_idiag_last_eq++;
4072		if (phba->lpfc_idiag_last_eq >= phba->cfg_hdw_queue)
4073			phba->lpfc_idiag_last_eq = 0;
4074
4075		len += scnprintf(pbuffer + len,
4076				 LPFC_QUE_INFO_GET_BUF_SIZE - len,
4077				 "HDWQ %d out of %d HBA HDWQs\n",
4078				 x, phba->cfg_hdw_queue);
4079
4080		/* Fast-path EQ */
4081		qp = phba->sli4_hba.hdwq[x].hba_eq;
4082		if (!qp)
4083			goto out;
4084
4085		len = __lpfc_idiag_print_eq(qp, "HBA", pbuffer, len);
4086
4087		/* Reset max counter */
4088		qp->EQ_max_eqe = 0;
4089
4090		if (len >= max_cnt)
4091			goto too_big;
4092
4093		/* will dump both fcp and nvme cqs/wqs for the eq */
4094		rc = lpfc_idiag_cqs_for_eq(phba, pbuffer, &len,
4095			max_cnt, x, qp->queue_id);
4096		if (rc)
4097			goto too_big;
4098
4099		/* Only EQ 0 has slow path CQs configured */
4100		if (x)
4101			goto out;
4102
4103		/* Slow-path mailbox CQ */
4104		qp = phba->sli4_hba.mbx_cq;
4105		len = __lpfc_idiag_print_cq(qp, "MBX", pbuffer, len);
4106		if (len >= max_cnt)
4107			goto too_big;
4108
4109		/* Slow-path MBOX MQ */
4110		qp = phba->sli4_hba.mbx_wq;
4111		len = __lpfc_idiag_print_wq(qp, "MBX", pbuffer, len);
4112		if (len >= max_cnt)
4113			goto too_big;
4114
4115		/* Slow-path ELS response CQ */
4116		qp = phba->sli4_hba.els_cq;
4117		len = __lpfc_idiag_print_cq(qp, "ELS", pbuffer, len);
4118		/* Reset max counter */
4119		if (qp)
4120			qp->CQ_max_cqe = 0;
4121		if (len >= max_cnt)
4122			goto too_big;
4123
4124		/* Slow-path ELS WQ */
4125		qp = phba->sli4_hba.els_wq;
4126		len = __lpfc_idiag_print_wq(qp, "ELS", pbuffer, len);
4127		if (len >= max_cnt)
4128			goto too_big;
4129
4130		qp = phba->sli4_hba.hdr_rq;
4131		len = __lpfc_idiag_print_rqpair(qp, phba->sli4_hba.dat_rq,
4132						"ELS RQpair", pbuffer, len);
4133		if (len >= max_cnt)
4134			goto too_big;
4135
4136		/* Slow-path NVME LS response CQ */
4137		qp = phba->sli4_hba.nvmels_cq;
4138		len = __lpfc_idiag_print_cq(qp, "NVME LS",
4139						pbuffer, len);
4140		/* Reset max counter */
4141		if (qp)
4142			qp->CQ_max_cqe = 0;
4143		if (len >= max_cnt)
4144			goto too_big;
4145
4146		/* Slow-path NVME LS WQ */
4147		qp = phba->sli4_hba.nvmels_wq;
4148		len = __lpfc_idiag_print_wq(qp, "NVME LS",
4149						pbuffer, len);
4150		if (len >= max_cnt)
4151			goto too_big;
4152
4153		goto out;
4154	}
4155
4156	spin_unlock_irq(&phba->hbalock);
4157	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4158
4159too_big:
4160	len +=  scnprintf(pbuffer + len,
4161		LPFC_QUE_INFO_GET_BUF_SIZE - len, "Truncated ...\n");
4162out:
4163	spin_unlock_irq(&phba->hbalock);
4164	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4165}
4166
4167/**
4168 * lpfc_idiag_que_param_check - queue access command parameter sanity check
4169 * @q: The pointer to queue structure.
4170 * @index: The index into a queue entry.
4171 * @count: The number of queue entries to access.
4172 *
4173 * Description:
4174 * The routine performs sanity check on device queue access method commands.
4175 *
4176 * Returns:
4177 * This function returns -EINVAL when fails the sanity check, otherwise, it
4178 * returns 0.
4179 **/
4180static int
4181lpfc_idiag_que_param_check(struct lpfc_queue *q, int index, int count)
4182{
4183	/* Only support single entry read or browsing */
4184	if ((count != 1) && (count != LPFC_QUE_ACC_BROWSE))
4185		return -EINVAL;
4186	if (index > q->entry_count - 1)
4187		return -EINVAL;
4188	return 0;
4189}
4190
4191/**
4192 * lpfc_idiag_queacc_read_qe - read a single entry from the given queue index
4193 * @pbuffer: The pointer to buffer to copy the read data into.
4194 * @len: Length of the buffer.
4195 * @pque: The pointer to the queue to be read.
4196 * @index: The index into the queue entry.
4197 *
4198 * Description:
4199 * This routine reads out a single entry from the given queue's index location
4200 * and copies it into the buffer provided.
4201 *
4202 * Returns:
4203 * This function returns 0 when it fails, otherwise, it returns the length of
4204 * the data read into the buffer provided.
4205 **/
4206static int
4207lpfc_idiag_queacc_read_qe(char *pbuffer, int len, struct lpfc_queue *pque,
4208			  uint32_t index)
4209{
4210	int offset, esize;
4211	uint32_t *pentry;
4212
4213	if (!pbuffer || !pque)
4214		return 0;
4215
4216	esize = pque->entry_size;
4217	len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
4218			"QE-INDEX[%04d]:\n", index);
4219
4220	offset = 0;
4221	pentry = lpfc_sli4_qe(pque, index);
4222	while (esize > 0) {
4223		len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
4224				"%08x ", *pentry);
4225		pentry++;
4226		offset += sizeof(uint32_t);
4227		esize -= sizeof(uint32_t);
4228		if (esize > 0 && !(offset % (4 * sizeof(uint32_t))))
4229			len += scnprintf(pbuffer+len,
4230					LPFC_QUE_ACC_BUF_SIZE-len, "\n");
4231	}
4232	len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len, "\n");
4233
4234	return len;
4235}
4236
4237/**
4238 * lpfc_idiag_queacc_read - idiag debugfs read port queue
4239 * @file: The file pointer to read from.
4240 * @buf: The buffer to copy the data to.
4241 * @nbytes: The number of bytes to read.
4242 * @ppos: The position in the file to start reading from.
4243 *
4244 * Description:
4245 * This routine reads data from the @phba device queue memory according to the
4246 * idiag command, and copies to user @buf. Depending on the queue dump read
4247 * command setup, it does either a single queue entry read or browing through
4248 * all entries of the queue.
4249 *
4250 * Returns:
4251 * This function returns the amount of data that was read (this could be less
4252 * than @nbytes if the end of the file was reached) or a negative error value.
4253 **/
4254static ssize_t
4255lpfc_idiag_queacc_read(struct file *file, char __user *buf, size_t nbytes,
4256		       loff_t *ppos)
4257{
4258	struct lpfc_debug *debug = file->private_data;
4259	uint32_t last_index, index, count;
4260	struct lpfc_queue *pque = NULL;
4261	char *pbuffer;
4262	int len = 0;
4263
4264	/* This is a user read operation */
4265	debug->op = LPFC_IDIAG_OP_RD;
4266
4267	if (!debug->buffer)
4268		debug->buffer = kmalloc(LPFC_QUE_ACC_BUF_SIZE, GFP_KERNEL);
4269	if (!debug->buffer)
4270		return 0;
4271	pbuffer = debug->buffer;
4272
4273	if (*ppos)
4274		return 0;
4275
4276	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
4277		index = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
4278		count = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
4279		pque = (struct lpfc_queue *)idiag.ptr_private;
4280	} else
4281		return 0;
4282
4283	/* Browse the queue starting from index */
4284	if (count == LPFC_QUE_ACC_BROWSE)
4285		goto que_browse;
4286
4287	/* Read a single entry from the queue */
4288	len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
4289
4290	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4291
4292que_browse:
4293
4294	/* Browse all entries from the queue */
4295	last_index = idiag.offset.last_rd;
4296	index = last_index;
4297
4298	while (len < LPFC_QUE_ACC_SIZE - pque->entry_size) {
4299		len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
4300		index++;
4301		if (index > pque->entry_count - 1)
4302			break;
4303	}
4304
4305	/* Set up the offset for next portion of pci cfg read */
4306	if (index > pque->entry_count - 1)
4307		index = 0;
4308	idiag.offset.last_rd = index;
4309
4310	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4311}
4312
4313/**
4314 * lpfc_idiag_queacc_write - Syntax check and set up idiag queacc commands
4315 * @file: The file pointer to read from.
4316 * @buf: The buffer to copy the user data from.
4317 * @nbytes: The number of bytes to get.
4318 * @ppos: The position in the file to start reading from.
4319 *
4320 * This routine get the debugfs idiag command struct from user space and then
4321 * perform the syntax check for port queue read (dump) or write (set) command
4322 * accordingly. In the case of port queue read command, it sets up the command
4323 * in the idiag command struct for the following debugfs read operation. In
4324 * the case of port queue write operation, it executes the write operation
4325 * into the port queue entry accordingly.
4326 *
4327 * It returns the @nbytges passing in from debugfs user space when successful.
4328 * In case of error conditions, it returns proper error code back to the user
4329 * space.
4330 **/
4331static ssize_t
4332lpfc_idiag_queacc_write(struct file *file, const char __user *buf,
4333			size_t nbytes, loff_t *ppos)
4334{
4335	struct lpfc_debug *debug = file->private_data;
4336	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4337	uint32_t qidx, quetp, queid, index, count, offset, value;
4338	uint32_t *pentry;
4339	struct lpfc_queue *pque, *qp;
4340	int rc;
4341
4342	/* This is a user write operation */
4343	debug->op = LPFC_IDIAG_OP_WR;
4344
4345	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4346	if (rc < 0)
4347		return rc;
4348
4349	/* Get and sanity check on command feilds */
4350	quetp  = idiag.cmd.data[IDIAG_QUEACC_QUETP_INDX];
4351	queid  = idiag.cmd.data[IDIAG_QUEACC_QUEID_INDX];
4352	index  = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
4353	count  = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
4354	offset = idiag.cmd.data[IDIAG_QUEACC_OFFST_INDX];
4355	value  = idiag.cmd.data[IDIAG_QUEACC_VALUE_INDX];
4356
4357	/* Sanity check on command line arguments */
4358	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
4359	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
4360	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
4361		if (rc != LPFC_QUE_ACC_WR_CMD_ARG)
4362			goto error_out;
4363		if (count != 1)
4364			goto error_out;
4365	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
4366		if (rc != LPFC_QUE_ACC_RD_CMD_ARG)
4367			goto error_out;
4368	} else
4369		goto error_out;
4370
4371	switch (quetp) {
4372	case LPFC_IDIAG_EQ:
4373		/* HBA event queue */
4374		if (phba->sli4_hba.hdwq) {
4375			for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
4376				qp = phba->sli4_hba.hdwq[qidx].hba_eq;
4377				if (qp && qp->queue_id == queid) {
4378					/* Sanity check */
4379					rc = lpfc_idiag_que_param_check(qp,
4380						index, count);
4381					if (rc)
4382						goto error_out;
4383					idiag.ptr_private = qp;
4384					goto pass_check;
4385				}
4386			}
4387		}
4388		goto error_out;
4389
4390	case LPFC_IDIAG_CQ:
4391		/* MBX complete queue */
4392		if (phba->sli4_hba.mbx_cq &&
4393		    phba->sli4_hba.mbx_cq->queue_id == queid) {
4394			/* Sanity check */
4395			rc = lpfc_idiag_que_param_check(
4396					phba->sli4_hba.mbx_cq, index, count);
4397			if (rc)
4398				goto error_out;
4399			idiag.ptr_private = phba->sli4_hba.mbx_cq;
4400			goto pass_check;
4401		}
4402		/* ELS complete queue */
4403		if (phba->sli4_hba.els_cq &&
4404		    phba->sli4_hba.els_cq->queue_id == queid) {
4405			/* Sanity check */
4406			rc = lpfc_idiag_que_param_check(
4407					phba->sli4_hba.els_cq, index, count);
4408			if (rc)
4409				goto error_out;
4410			idiag.ptr_private = phba->sli4_hba.els_cq;
4411			goto pass_check;
4412		}
4413		/* NVME LS complete queue */
4414		if (phba->sli4_hba.nvmels_cq &&
4415		    phba->sli4_hba.nvmels_cq->queue_id == queid) {
4416			/* Sanity check */
4417			rc = lpfc_idiag_que_param_check(
4418					phba->sli4_hba.nvmels_cq, index, count);
4419			if (rc)
4420				goto error_out;
4421			idiag.ptr_private = phba->sli4_hba.nvmels_cq;
4422			goto pass_check;
4423		}
4424		/* FCP complete queue */
4425		if (phba->sli4_hba.hdwq) {
4426			for (qidx = 0; qidx < phba->cfg_hdw_queue;
4427								qidx++) {
4428				qp = phba->sli4_hba.hdwq[qidx].io_cq;
4429				if (qp && qp->queue_id == queid) {
4430					/* Sanity check */
4431					rc = lpfc_idiag_que_param_check(
4432						qp, index, count);
4433					if (rc)
4434						goto error_out;
4435					idiag.ptr_private = qp;
4436					goto pass_check;
4437				}
4438			}
4439		}
4440		goto error_out;
4441
4442	case LPFC_IDIAG_MQ:
4443		/* MBX work queue */
4444		if (phba->sli4_hba.mbx_wq &&
4445		    phba->sli4_hba.mbx_wq->queue_id == queid) {
4446			/* Sanity check */
4447			rc = lpfc_idiag_que_param_check(
4448					phba->sli4_hba.mbx_wq, index, count);
4449			if (rc)
4450				goto error_out;
4451			idiag.ptr_private = phba->sli4_hba.mbx_wq;
4452			goto pass_check;
4453		}
4454		goto error_out;
4455
4456	case LPFC_IDIAG_WQ:
4457		/* ELS work queue */
4458		if (phba->sli4_hba.els_wq &&
4459		    phba->sli4_hba.els_wq->queue_id == queid) {
4460			/* Sanity check */
4461			rc = lpfc_idiag_que_param_check(
4462					phba->sli4_hba.els_wq, index, count);
4463			if (rc)
4464				goto error_out;
4465			idiag.ptr_private = phba->sli4_hba.els_wq;
4466			goto pass_check;
4467		}
4468		/* NVME LS work queue */
4469		if (phba->sli4_hba.nvmels_wq &&
4470		    phba->sli4_hba.nvmels_wq->queue_id == queid) {
4471			/* Sanity check */
4472			rc = lpfc_idiag_que_param_check(
4473					phba->sli4_hba.nvmels_wq, index, count);
4474			if (rc)
4475				goto error_out;
4476			idiag.ptr_private = phba->sli4_hba.nvmels_wq;
4477			goto pass_check;
4478		}
4479
4480		if (phba->sli4_hba.hdwq) {
4481			/* FCP/SCSI work queue */
4482			for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
4483				qp = phba->sli4_hba.hdwq[qidx].io_wq;
4484				if (qp && qp->queue_id == queid) {
4485					/* Sanity check */
4486					rc = lpfc_idiag_que_param_check(
4487						qp, index, count);
4488					if (rc)
4489						goto error_out;
4490					idiag.ptr_private = qp;
4491					goto pass_check;
4492				}
4493			}
4494		}
4495		goto error_out;
4496
 
 
4497	case LPFC_IDIAG_RQ:
4498		/* HDR queue */
4499		if (phba->sli4_hba.hdr_rq &&
4500		    phba->sli4_hba.hdr_rq->queue_id == queid) {
4501			/* Sanity check */
4502			rc = lpfc_idiag_que_param_check(
4503					phba->sli4_hba.hdr_rq, index, count);
4504			if (rc)
4505				goto error_out;
4506			idiag.ptr_private = phba->sli4_hba.hdr_rq;
4507			goto pass_check;
4508		}
4509		/* DAT queue */
4510		if (phba->sli4_hba.dat_rq &&
4511		    phba->sli4_hba.dat_rq->queue_id == queid) {
4512			/* Sanity check */
4513			rc = lpfc_idiag_que_param_check(
4514					phba->sli4_hba.dat_rq, index, count);
4515			if (rc)
4516				goto error_out;
4517			idiag.ptr_private = phba->sli4_hba.dat_rq;
4518			goto pass_check;
4519		}
4520		goto error_out;
 
4521	default:
4522		goto error_out;
 
4523	}
4524
4525pass_check:
4526
4527	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
4528		if (count == LPFC_QUE_ACC_BROWSE)
4529			idiag.offset.last_rd = index;
4530	}
4531
4532	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
4533	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
4534	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
4535		/* Additional sanity checks on write operation */
4536		pque = (struct lpfc_queue *)idiag.ptr_private;
4537		if (offset > pque->entry_size/sizeof(uint32_t) - 1)
4538			goto error_out;
4539		pentry = lpfc_sli4_qe(pque, index);
4540		pentry += offset;
4541		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR)
4542			*pentry = value;
4543		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST)
4544			*pentry |= value;
4545		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL)
4546			*pentry &= ~value;
4547	}
4548	return nbytes;
4549
4550error_out:
4551	/* Clean out command structure on command error out */
4552	memset(&idiag, 0, sizeof(idiag));
4553	return -EINVAL;
4554}
4555
4556/**
4557 * lpfc_idiag_drbacc_read_reg - idiag debugfs read a doorbell register
4558 * @phba: The pointer to hba structure.
4559 * @pbuffer: The pointer to the buffer to copy the data to.
4560 * @len: The length of bytes to copied.
4561 * @drbregid: The id to doorbell registers.
4562 *
4563 * Description:
4564 * This routine reads a doorbell register and copies its content to the
4565 * user buffer pointed to by @pbuffer.
4566 *
4567 * Returns:
4568 * This function returns the amount of data that was copied into @pbuffer.
4569 **/
4570static int
4571lpfc_idiag_drbacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
4572			   int len, uint32_t drbregid)
4573{
4574
4575	if (!pbuffer)
4576		return 0;
4577
4578	switch (drbregid) {
4579	case LPFC_DRB_EQ:
4580		len += scnprintf(pbuffer + len, LPFC_DRB_ACC_BUF_SIZE-len,
4581				"EQ-DRB-REG: 0x%08x\n",
4582				readl(phba->sli4_hba.EQDBregaddr));
4583		break;
4584	case LPFC_DRB_CQ:
4585		len += scnprintf(pbuffer + len, LPFC_DRB_ACC_BUF_SIZE - len,
4586				"CQ-DRB-REG: 0x%08x\n",
4587				readl(phba->sli4_hba.CQDBregaddr));
4588		break;
4589	case LPFC_DRB_MQ:
4590		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4591				"MQ-DRB-REG:   0x%08x\n",
4592				readl(phba->sli4_hba.MQDBregaddr));
4593		break;
4594	case LPFC_DRB_WQ:
4595		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4596				"WQ-DRB-REG:   0x%08x\n",
4597				readl(phba->sli4_hba.WQDBregaddr));
4598		break;
4599	case LPFC_DRB_RQ:
4600		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4601				"RQ-DRB-REG:   0x%08x\n",
4602				readl(phba->sli4_hba.RQDBregaddr));
4603		break;
4604	default:
4605		break;
4606	}
4607
4608	return len;
4609}
4610
4611/**
4612 * lpfc_idiag_drbacc_read - idiag debugfs read port doorbell
4613 * @file: The file pointer to read from.
4614 * @buf: The buffer to copy the data to.
4615 * @nbytes: The number of bytes to read.
4616 * @ppos: The position in the file to start reading from.
4617 *
4618 * Description:
4619 * This routine reads data from the @phba device doorbell register according
4620 * to the idiag command, and copies to user @buf. Depending on the doorbell
4621 * register read command setup, it does either a single doorbell register
4622 * read or dump all doorbell registers.
4623 *
4624 * Returns:
4625 * This function returns the amount of data that was read (this could be less
4626 * than @nbytes if the end of the file was reached) or a negative error value.
4627 **/
4628static ssize_t
4629lpfc_idiag_drbacc_read(struct file *file, char __user *buf, size_t nbytes,
4630		       loff_t *ppos)
4631{
4632	struct lpfc_debug *debug = file->private_data;
4633	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4634	uint32_t drb_reg_id, i;
4635	char *pbuffer;
4636	int len = 0;
4637
4638	/* This is a user read operation */
4639	debug->op = LPFC_IDIAG_OP_RD;
4640
4641	if (!debug->buffer)
4642		debug->buffer = kmalloc(LPFC_DRB_ACC_BUF_SIZE, GFP_KERNEL);
4643	if (!debug->buffer)
4644		return 0;
4645	pbuffer = debug->buffer;
4646
4647	if (*ppos)
4648		return 0;
4649
4650	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD)
4651		drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
4652	else
4653		return 0;
4654
4655	if (drb_reg_id == LPFC_DRB_ACC_ALL)
4656		for (i = 1; i <= LPFC_DRB_MAX; i++)
4657			len = lpfc_idiag_drbacc_read_reg(phba,
4658							 pbuffer, len, i);
4659	else
4660		len = lpfc_idiag_drbacc_read_reg(phba,
4661						 pbuffer, len, drb_reg_id);
4662
4663	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4664}
4665
4666/**
4667 * lpfc_idiag_drbacc_write - Syntax check and set up idiag drbacc commands
4668 * @file: The file pointer to read from.
4669 * @buf: The buffer to copy the user data from.
4670 * @nbytes: The number of bytes to get.
4671 * @ppos: The position in the file to start reading from.
4672 *
4673 * This routine get the debugfs idiag command struct from user space and then
4674 * perform the syntax check for port doorbell register read (dump) or write
4675 * (set) command accordingly. In the case of port queue read command, it sets
4676 * up the command in the idiag command struct for the following debugfs read
4677 * operation. In the case of port doorbell register write operation, it
4678 * executes the write operation into the port doorbell register accordingly.
4679 *
4680 * It returns the @nbytges passing in from debugfs user space when successful.
4681 * In case of error conditions, it returns proper error code back to the user
4682 * space.
4683 **/
4684static ssize_t
4685lpfc_idiag_drbacc_write(struct file *file, const char __user *buf,
4686			size_t nbytes, loff_t *ppos)
4687{
4688	struct lpfc_debug *debug = file->private_data;
4689	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4690	uint32_t drb_reg_id, value, reg_val = 0;
4691	void __iomem *drb_reg;
4692	int rc;
4693
4694	/* This is a user write operation */
4695	debug->op = LPFC_IDIAG_OP_WR;
4696
4697	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4698	if (rc < 0)
4699		return rc;
4700
4701	/* Sanity check on command line arguments */
4702	drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
4703	value = idiag.cmd.data[IDIAG_DRBACC_VALUE_INDX];
4704
4705	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
4706	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
4707	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4708		if (rc != LPFC_DRB_ACC_WR_CMD_ARG)
4709			goto error_out;
4710		if (drb_reg_id > LPFC_DRB_MAX)
4711			goto error_out;
4712	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD) {
4713		if (rc != LPFC_DRB_ACC_RD_CMD_ARG)
4714			goto error_out;
4715		if ((drb_reg_id > LPFC_DRB_MAX) &&
4716		    (drb_reg_id != LPFC_DRB_ACC_ALL))
4717			goto error_out;
4718	} else
4719		goto error_out;
4720
4721	/* Perform the write access operation */
4722	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
4723	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
4724	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4725		switch (drb_reg_id) {
4726		case LPFC_DRB_EQ:
4727			drb_reg = phba->sli4_hba.EQDBregaddr;
4728			break;
4729		case LPFC_DRB_CQ:
4730			drb_reg = phba->sli4_hba.CQDBregaddr;
4731			break;
4732		case LPFC_DRB_MQ:
4733			drb_reg = phba->sli4_hba.MQDBregaddr;
4734			break;
4735		case LPFC_DRB_WQ:
4736			drb_reg = phba->sli4_hba.WQDBregaddr;
4737			break;
4738		case LPFC_DRB_RQ:
4739			drb_reg = phba->sli4_hba.RQDBregaddr;
4740			break;
4741		default:
4742			goto error_out;
4743		}
4744
4745		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR)
4746			reg_val = value;
4747		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST) {
4748			reg_val = readl(drb_reg);
4749			reg_val |= value;
4750		}
4751		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4752			reg_val = readl(drb_reg);
4753			reg_val &= ~value;
4754		}
4755		writel(reg_val, drb_reg);
4756		readl(drb_reg); /* flush */
4757	}
4758	return nbytes;
4759
4760error_out:
4761	/* Clean out command structure on command error out */
4762	memset(&idiag, 0, sizeof(idiag));
4763	return -EINVAL;
4764}
4765
4766/**
4767 * lpfc_idiag_ctlacc_read_reg - idiag debugfs read a control registers
4768 * @phba: The pointer to hba structure.
4769 * @pbuffer: The pointer to the buffer to copy the data to.
4770 * @len: The length of bytes to copied.
4771 * @ctlregid: The id to doorbell registers.
4772 *
4773 * Description:
4774 * This routine reads a control register and copies its content to the
4775 * user buffer pointed to by @pbuffer.
4776 *
4777 * Returns:
4778 * This function returns the amount of data that was copied into @pbuffer.
4779 **/
4780static int
4781lpfc_idiag_ctlacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
4782			   int len, uint32_t ctlregid)
4783{
4784
4785	if (!pbuffer)
4786		return 0;
4787
4788	switch (ctlregid) {
4789	case LPFC_CTL_PORT_SEM:
4790		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4791				"Port SemReg:   0x%08x\n",
4792				readl(phba->sli4_hba.conf_regs_memmap_p +
4793				      LPFC_CTL_PORT_SEM_OFFSET));
4794		break;
4795	case LPFC_CTL_PORT_STA:
4796		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4797				"Port StaReg:   0x%08x\n",
4798				readl(phba->sli4_hba.conf_regs_memmap_p +
4799				      LPFC_CTL_PORT_STA_OFFSET));
4800		break;
4801	case LPFC_CTL_PORT_CTL:
4802		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4803				"Port CtlReg:   0x%08x\n",
4804				readl(phba->sli4_hba.conf_regs_memmap_p +
4805				      LPFC_CTL_PORT_CTL_OFFSET));
4806		break;
4807	case LPFC_CTL_PORT_ER1:
4808		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4809				"Port Er1Reg:   0x%08x\n",
4810				readl(phba->sli4_hba.conf_regs_memmap_p +
4811				      LPFC_CTL_PORT_ER1_OFFSET));
4812		break;
4813	case LPFC_CTL_PORT_ER2:
4814		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4815				"Port Er2Reg:   0x%08x\n",
4816				readl(phba->sli4_hba.conf_regs_memmap_p +
4817				      LPFC_CTL_PORT_ER2_OFFSET));
4818		break;
4819	case LPFC_CTL_PDEV_CTL:
4820		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4821				"PDev CtlReg:   0x%08x\n",
4822				readl(phba->sli4_hba.conf_regs_memmap_p +
4823				      LPFC_CTL_PDEV_CTL_OFFSET));
4824		break;
4825	default:
4826		break;
4827	}
4828	return len;
4829}
4830
4831/**
4832 * lpfc_idiag_ctlacc_read - idiag debugfs read port and device control register
4833 * @file: The file pointer to read from.
4834 * @buf: The buffer to copy the data to.
4835 * @nbytes: The number of bytes to read.
4836 * @ppos: The position in the file to start reading from.
4837 *
4838 * Description:
4839 * This routine reads data from the @phba port and device registers according
4840 * to the idiag command, and copies to user @buf.
4841 *
4842 * Returns:
4843 * This function returns the amount of data that was read (this could be less
4844 * than @nbytes if the end of the file was reached) or a negative error value.
4845 **/
4846static ssize_t
4847lpfc_idiag_ctlacc_read(struct file *file, char __user *buf, size_t nbytes,
4848		       loff_t *ppos)
4849{
4850	struct lpfc_debug *debug = file->private_data;
4851	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4852	uint32_t ctl_reg_id, i;
4853	char *pbuffer;
4854	int len = 0;
4855
4856	/* This is a user read operation */
4857	debug->op = LPFC_IDIAG_OP_RD;
4858
4859	if (!debug->buffer)
4860		debug->buffer = kmalloc(LPFC_CTL_ACC_BUF_SIZE, GFP_KERNEL);
4861	if (!debug->buffer)
4862		return 0;
4863	pbuffer = debug->buffer;
4864
4865	if (*ppos)
4866		return 0;
4867
4868	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD)
4869		ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
4870	else
4871		return 0;
4872
4873	if (ctl_reg_id == LPFC_CTL_ACC_ALL)
4874		for (i = 1; i <= LPFC_CTL_MAX; i++)
4875			len = lpfc_idiag_ctlacc_read_reg(phba,
4876							 pbuffer, len, i);
4877	else
4878		len = lpfc_idiag_ctlacc_read_reg(phba,
4879						 pbuffer, len, ctl_reg_id);
4880
4881	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4882}
4883
4884/**
4885 * lpfc_idiag_ctlacc_write - Syntax check and set up idiag ctlacc commands
4886 * @file: The file pointer to read from.
4887 * @buf: The buffer to copy the user data from.
4888 * @nbytes: The number of bytes to get.
4889 * @ppos: The position in the file to start reading from.
4890 *
4891 * This routine get the debugfs idiag command struct from user space and then
4892 * perform the syntax check for port and device control register read (dump)
4893 * or write (set) command accordingly.
4894 *
4895 * It returns the @nbytges passing in from debugfs user space when successful.
4896 * In case of error conditions, it returns proper error code back to the user
4897 * space.
4898 **/
4899static ssize_t
4900lpfc_idiag_ctlacc_write(struct file *file, const char __user *buf,
4901			size_t nbytes, loff_t *ppos)
4902{
4903	struct lpfc_debug *debug = file->private_data;
4904	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4905	uint32_t ctl_reg_id, value, reg_val = 0;
4906	void __iomem *ctl_reg;
4907	int rc;
4908
4909	/* This is a user write operation */
4910	debug->op = LPFC_IDIAG_OP_WR;
4911
4912	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4913	if (rc < 0)
4914		return rc;
4915
4916	/* Sanity check on command line arguments */
4917	ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
4918	value = idiag.cmd.data[IDIAG_CTLACC_VALUE_INDX];
4919
4920	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
4921	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
4922	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4923		if (rc != LPFC_CTL_ACC_WR_CMD_ARG)
4924			goto error_out;
4925		if (ctl_reg_id > LPFC_CTL_MAX)
4926			goto error_out;
4927	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD) {
4928		if (rc != LPFC_CTL_ACC_RD_CMD_ARG)
4929			goto error_out;
4930		if ((ctl_reg_id > LPFC_CTL_MAX) &&
4931		    (ctl_reg_id != LPFC_CTL_ACC_ALL))
4932			goto error_out;
4933	} else
4934		goto error_out;
4935
4936	/* Perform the write access operation */
4937	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
4938	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
4939	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4940		switch (ctl_reg_id) {
4941		case LPFC_CTL_PORT_SEM:
4942			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4943					LPFC_CTL_PORT_SEM_OFFSET;
4944			break;
4945		case LPFC_CTL_PORT_STA:
4946			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4947					LPFC_CTL_PORT_STA_OFFSET;
4948			break;
4949		case LPFC_CTL_PORT_CTL:
4950			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4951					LPFC_CTL_PORT_CTL_OFFSET;
4952			break;
4953		case LPFC_CTL_PORT_ER1:
4954			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4955					LPFC_CTL_PORT_ER1_OFFSET;
4956			break;
4957		case LPFC_CTL_PORT_ER2:
4958			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4959					LPFC_CTL_PORT_ER2_OFFSET;
4960			break;
4961		case LPFC_CTL_PDEV_CTL:
4962			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4963					LPFC_CTL_PDEV_CTL_OFFSET;
4964			break;
4965		default:
4966			goto error_out;
4967		}
4968
4969		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR)
4970			reg_val = value;
4971		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST) {
4972			reg_val = readl(ctl_reg);
4973			reg_val |= value;
4974		}
4975		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4976			reg_val = readl(ctl_reg);
4977			reg_val &= ~value;
4978		}
4979		writel(reg_val, ctl_reg);
4980		readl(ctl_reg); /* flush */
4981	}
4982	return nbytes;
4983
4984error_out:
4985	/* Clean out command structure on command error out */
4986	memset(&idiag, 0, sizeof(idiag));
4987	return -EINVAL;
4988}
4989
4990/**
4991 * lpfc_idiag_mbxacc_get_setup - idiag debugfs get mailbox access setup
4992 * @phba: Pointer to HBA context object.
4993 * @pbuffer: Pointer to data buffer.
4994 *
4995 * Description:
4996 * This routine gets the driver mailbox access debugfs setup information.
4997 *
4998 * Returns:
4999 * This function returns the amount of data that was read (this could be less
5000 * than @nbytes if the end of the file was reached) or a negative error value.
5001 **/
5002static int
5003lpfc_idiag_mbxacc_get_setup(struct lpfc_hba *phba, char *pbuffer)
5004{
5005	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
5006	int len = 0;
5007
5008	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5009	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5010	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5011	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5012
5013	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5014			"mbx_dump_map: 0x%08x\n", mbx_dump_map);
5015	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5016			"mbx_dump_cnt: %04d\n", mbx_dump_cnt);
5017	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5018			"mbx_word_cnt: %04d\n", mbx_word_cnt);
5019	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5020			"mbx_mbox_cmd: 0x%02x\n", mbx_mbox_cmd);
5021
5022	return len;
5023}
5024
5025/**
5026 * lpfc_idiag_mbxacc_read - idiag debugfs read on mailbox access
5027 * @file: The file pointer to read from.
5028 * @buf: The buffer to copy the data to.
5029 * @nbytes: The number of bytes to read.
5030 * @ppos: The position in the file to start reading from.
5031 *
5032 * Description:
5033 * This routine reads data from the @phba driver mailbox access debugfs setup
5034 * information.
5035 *
5036 * Returns:
5037 * This function returns the amount of data that was read (this could be less
5038 * than @nbytes if the end of the file was reached) or a negative error value.
5039 **/
5040static ssize_t
5041lpfc_idiag_mbxacc_read(struct file *file, char __user *buf, size_t nbytes,
5042		       loff_t *ppos)
5043{
5044	struct lpfc_debug *debug = file->private_data;
5045	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
5046	char *pbuffer;
5047	int len = 0;
5048
5049	/* This is a user read operation */
5050	debug->op = LPFC_IDIAG_OP_RD;
5051
5052	if (!debug->buffer)
5053		debug->buffer = kmalloc(LPFC_MBX_ACC_BUF_SIZE, GFP_KERNEL);
5054	if (!debug->buffer)
5055		return 0;
5056	pbuffer = debug->buffer;
5057
5058	if (*ppos)
5059		return 0;
5060
5061	if ((idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP) &&
5062	    (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP))
5063		return 0;
5064
5065	len = lpfc_idiag_mbxacc_get_setup(phba, pbuffer);
5066
5067	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
5068}
5069
5070/**
5071 * lpfc_idiag_mbxacc_write - Syntax check and set up idiag mbxacc commands
5072 * @file: The file pointer to read from.
5073 * @buf: The buffer to copy the user data from.
5074 * @nbytes: The number of bytes to get.
5075 * @ppos: The position in the file to start reading from.
5076 *
5077 * This routine get the debugfs idiag command struct from user space and then
5078 * perform the syntax check for driver mailbox command (dump) and sets up the
5079 * necessary states in the idiag command struct accordingly.
5080 *
5081 * It returns the @nbytges passing in from debugfs user space when successful.
5082 * In case of error conditions, it returns proper error code back to the user
5083 * space.
5084 **/
5085static ssize_t
5086lpfc_idiag_mbxacc_write(struct file *file, const char __user *buf,
5087			size_t nbytes, loff_t *ppos)
5088{
5089	struct lpfc_debug *debug = file->private_data;
5090	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
5091	int rc;
5092
5093	/* This is a user write operation */
5094	debug->op = LPFC_IDIAG_OP_WR;
5095
5096	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
5097	if (rc < 0)
5098		return rc;
5099
5100	/* Sanity check on command line arguments */
5101	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5102	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5103	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5104	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5105
5106	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_MBXACC_DP) {
5107		if (!(mbx_dump_map & LPFC_MBX_DMP_MBX_ALL))
5108			goto error_out;
5109		if ((mbx_dump_map & ~LPFC_MBX_DMP_MBX_ALL) &&
5110		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
5111			goto error_out;
5112		if (mbx_word_cnt > sizeof(MAILBOX_t))
5113			goto error_out;
5114	} else if (idiag.cmd.opcode == LPFC_IDIAG_BSG_MBXACC_DP) {
5115		if (!(mbx_dump_map & LPFC_BSG_DMP_MBX_ALL))
5116			goto error_out;
5117		if ((mbx_dump_map & ~LPFC_BSG_DMP_MBX_ALL) &&
5118		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
5119			goto error_out;
5120		if (mbx_word_cnt > (BSG_MBOX_SIZE)/4)
5121			goto error_out;
5122		if (mbx_mbox_cmd != 0x9b)
5123			goto error_out;
5124	} else
5125		goto error_out;
5126
5127	if (mbx_word_cnt == 0)
5128		goto error_out;
5129	if (rc != LPFC_MBX_DMP_ARG)
5130		goto error_out;
5131	if (mbx_mbox_cmd & ~0xff)
5132		goto error_out;
5133
5134	/* condition for stop mailbox dump */
5135	if (mbx_dump_cnt == 0)
5136		goto reset_out;
5137
5138	return nbytes;
5139
5140reset_out:
5141	/* Clean out command structure on command error out */
5142	memset(&idiag, 0, sizeof(idiag));
5143	return nbytes;
5144
5145error_out:
5146	/* Clean out command structure on command error out */
5147	memset(&idiag, 0, sizeof(idiag));
5148	return -EINVAL;
5149}
5150
5151/**
5152 * lpfc_idiag_extacc_avail_get - get the available extents information
5153 * @phba: pointer to lpfc hba data structure.
5154 * @pbuffer: pointer to internal buffer.
5155 * @len: length into the internal buffer data has been copied.
5156 *
5157 * Description:
5158 * This routine is to get the available extent information.
5159 *
5160 * Returns:
5161 * overall length of the data read into the internal buffer.
5162 **/
5163static int
5164lpfc_idiag_extacc_avail_get(struct lpfc_hba *phba, char *pbuffer, int len)
5165{
5166	uint16_t ext_cnt = 0, ext_size = 0;
5167
5168	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5169			"\nAvailable Extents Information:\n");
5170
5171	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5172			"\tPort Available VPI extents: ");
5173	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VPI,
5174				       &ext_cnt, &ext_size);
5175	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5176			"Count %3d, Size %3d\n", ext_cnt, ext_size);
5177
5178	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5179			"\tPort Available VFI extents: ");
5180	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VFI,
5181				       &ext_cnt, &ext_size);
5182	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5183			"Count %3d, Size %3d\n", ext_cnt, ext_size);
5184
5185	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5186			"\tPort Available RPI extents: ");
5187	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_RPI,
5188				       &ext_cnt, &ext_size);
5189	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5190			"Count %3d, Size %3d\n", ext_cnt, ext_size);
5191
5192	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5193			"\tPort Available XRI extents: ");
5194	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_XRI,
5195				       &ext_cnt, &ext_size);
5196	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5197			"Count %3d, Size %3d\n", ext_cnt, ext_size);
5198
5199	return len;
5200}
5201
5202/**
5203 * lpfc_idiag_extacc_alloc_get - get the allocated extents information
5204 * @phba: pointer to lpfc hba data structure.
5205 * @pbuffer: pointer to internal buffer.
5206 * @len: length into the internal buffer data has been copied.
5207 *
5208 * Description:
5209 * This routine is to get the allocated extent information.
5210 *
5211 * Returns:
5212 * overall length of the data read into the internal buffer.
5213 **/
5214static int
5215lpfc_idiag_extacc_alloc_get(struct lpfc_hba *phba, char *pbuffer, int len)
5216{
5217	uint16_t ext_cnt, ext_size;
5218	int rc;
5219
5220	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5221			"\nAllocated Extents Information:\n");
5222
5223	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5224			"\tHost Allocated VPI extents: ");
5225	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VPI,
5226					    &ext_cnt, &ext_size);
5227	if (!rc)
5228		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5229				"Port %d Extent %3d, Size %3d\n",
5230				phba->brd_no, ext_cnt, ext_size);
5231	else
5232		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5233				"N/A\n");
5234
5235	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5236			"\tHost Allocated VFI extents: ");
5237	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VFI,
5238					    &ext_cnt, &ext_size);
5239	if (!rc)
5240		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5241				"Port %d Extent %3d, Size %3d\n",
5242				phba->brd_no, ext_cnt, ext_size);
5243	else
5244		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5245				"N/A\n");
5246
5247	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5248			"\tHost Allocated RPI extents: ");
5249	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_RPI,
5250					    &ext_cnt, &ext_size);
5251	if (!rc)
5252		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5253				"Port %d Extent %3d, Size %3d\n",
5254				phba->brd_no, ext_cnt, ext_size);
5255	else
5256		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5257				"N/A\n");
5258
5259	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5260			"\tHost Allocated XRI extents: ");
5261	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_XRI,
5262					    &ext_cnt, &ext_size);
5263	if (!rc)
5264		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5265				"Port %d Extent %3d, Size %3d\n",
5266				phba->brd_no, ext_cnt, ext_size);
5267	else
5268		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5269				"N/A\n");
5270
5271	return len;
5272}
5273
5274/**
5275 * lpfc_idiag_extacc_drivr_get - get driver extent information
5276 * @phba: pointer to lpfc hba data structure.
5277 * @pbuffer: pointer to internal buffer.
5278 * @len: length into the internal buffer data has been copied.
5279 *
5280 * Description:
5281 * This routine is to get the driver extent information.
5282 *
5283 * Returns:
5284 * overall length of the data read into the internal buffer.
5285 **/
5286static int
5287lpfc_idiag_extacc_drivr_get(struct lpfc_hba *phba, char *pbuffer, int len)
5288{
5289	struct lpfc_rsrc_blks *rsrc_blks;
5290	int index;
5291
5292	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5293			"\nDriver Extents Information:\n");
5294
5295	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5296			"\tVPI extents:\n");
5297	index = 0;
5298	list_for_each_entry(rsrc_blks, &phba->lpfc_vpi_blk_list, list) {
5299		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5300				"\t\tBlock %3d: Start %4d, Count %4d\n",
5301				index, rsrc_blks->rsrc_start,
5302				rsrc_blks->rsrc_size);
5303		index++;
5304	}
5305	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5306			"\tVFI extents:\n");
5307	index = 0;
5308	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_vfi_blk_list,
5309			    list) {
5310		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5311				"\t\tBlock %3d: Start %4d, Count %4d\n",
5312				index, rsrc_blks->rsrc_start,
5313				rsrc_blks->rsrc_size);
5314		index++;
5315	}
5316
5317	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5318			"\tRPI extents:\n");
5319	index = 0;
5320	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_rpi_blk_list,
5321			    list) {
5322		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5323				"\t\tBlock %3d: Start %4d, Count %4d\n",
5324				index, rsrc_blks->rsrc_start,
5325				rsrc_blks->rsrc_size);
5326		index++;
5327	}
5328
5329	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5330			"\tXRI extents:\n");
5331	index = 0;
5332	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_xri_blk_list,
5333			    list) {
5334		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5335				"\t\tBlock %3d: Start %4d, Count %4d\n",
5336				index, rsrc_blks->rsrc_start,
5337				rsrc_blks->rsrc_size);
5338		index++;
5339	}
5340
5341	return len;
5342}
5343
5344/**
5345 * lpfc_idiag_extacc_write - Syntax check and set up idiag extacc commands
5346 * @file: The file pointer to read from.
5347 * @buf: The buffer to copy the user data from.
5348 * @nbytes: The number of bytes to get.
5349 * @ppos: The position in the file to start reading from.
5350 *
5351 * This routine get the debugfs idiag command struct from user space and then
5352 * perform the syntax check for extent information access commands and sets
5353 * up the necessary states in the idiag command struct accordingly.
5354 *
5355 * It returns the @nbytges passing in from debugfs user space when successful.
5356 * In case of error conditions, it returns proper error code back to the user
5357 * space.
5358 **/
5359static ssize_t
5360lpfc_idiag_extacc_write(struct file *file, const char __user *buf,
5361			size_t nbytes, loff_t *ppos)
5362{
5363	struct lpfc_debug *debug = file->private_data;
5364	uint32_t ext_map;
5365	int rc;
5366
5367	/* This is a user write operation */
5368	debug->op = LPFC_IDIAG_OP_WR;
5369
5370	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
5371	if (rc < 0)
5372		return rc;
5373
5374	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
5375
5376	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
5377		goto error_out;
5378	if (rc != LPFC_EXT_ACC_CMD_ARG)
5379		goto error_out;
5380	if (!(ext_map & LPFC_EXT_ACC_ALL))
5381		goto error_out;
5382
5383	return nbytes;
5384error_out:
5385	/* Clean out command structure on command error out */
5386	memset(&idiag, 0, sizeof(idiag));
5387	return -EINVAL;
5388}
5389
5390/**
5391 * lpfc_idiag_extacc_read - idiag debugfs read access to extent information
5392 * @file: The file pointer to read from.
5393 * @buf: The buffer to copy the data to.
5394 * @nbytes: The number of bytes to read.
5395 * @ppos: The position in the file to start reading from.
5396 *
5397 * Description:
5398 * This routine reads data from the proper extent information according to
5399 * the idiag command, and copies to user @buf.
5400 *
5401 * Returns:
5402 * This function returns the amount of data that was read (this could be less
5403 * than @nbytes if the end of the file was reached) or a negative error value.
5404 **/
5405static ssize_t
5406lpfc_idiag_extacc_read(struct file *file, char __user *buf, size_t nbytes,
5407		       loff_t *ppos)
5408{
5409	struct lpfc_debug *debug = file->private_data;
5410	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
5411	char *pbuffer;
5412	uint32_t ext_map;
5413	int len = 0;
5414
5415	/* This is a user read operation */
5416	debug->op = LPFC_IDIAG_OP_RD;
5417
5418	if (!debug->buffer)
5419		debug->buffer = kmalloc(LPFC_EXT_ACC_BUF_SIZE, GFP_KERNEL);
5420	if (!debug->buffer)
5421		return 0;
5422	pbuffer = debug->buffer;
5423	if (*ppos)
5424		return 0;
5425	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
5426		return 0;
5427
5428	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
5429	if (ext_map & LPFC_EXT_ACC_AVAIL)
5430		len = lpfc_idiag_extacc_avail_get(phba, pbuffer, len);
5431	if (ext_map & LPFC_EXT_ACC_ALLOC)
5432		len = lpfc_idiag_extacc_alloc_get(phba, pbuffer, len);
5433	if (ext_map & LPFC_EXT_ACC_DRIVR)
5434		len = lpfc_idiag_extacc_drivr_get(phba, pbuffer, len);
5435
5436	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
5437}
5438
5439static int
5440lpfc_cgn_buffer_open(struct inode *inode, struct file *file)
5441{
5442	struct lpfc_debug *debug;
5443	int rc = -ENOMEM;
5444
5445	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
5446	if (!debug)
5447		goto out;
5448
5449	debug->buffer = vmalloc(LPFC_CGN_BUF_SIZE);
5450	if (!debug->buffer) {
5451		kfree(debug);
5452		goto out;
5453	}
5454
5455	debug->i_private = inode->i_private;
5456	file->private_data = debug;
5457
5458	rc = 0;
5459out:
5460	return rc;
5461}
5462
5463static ssize_t
5464lpfc_cgn_buffer_read(struct file *file, char __user *buf, size_t nbytes,
5465		     loff_t *ppos)
5466{
5467	struct lpfc_debug *debug = file->private_data;
5468	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
5469	char *buffer = debug->buffer;
5470	uint32_t *ptr;
5471	int cnt, len = 0;
5472
5473	if (!phba->sli4_hba.pc_sli4_params.mi_ver || !phba->cgn_i) {
5474		len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
5475				 "Congestion Mgmt is not supported\n");
5476		goto out;
5477	}
5478	ptr = (uint32_t *)phba->cgn_i->virt;
5479	len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
5480			 "Congestion Buffer Header\n");
5481	/* Dump the first 32 bytes */
5482	cnt = 32;
5483	len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
5484			 "000: %08x %08x %08x %08x %08x %08x %08x %08x\n",
5485			 *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
5486			 *(ptr + 4), *(ptr + 5), *(ptr + 6), *(ptr + 7));
5487	ptr += 8;
5488	len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
5489			 "Congestion Buffer Data\n");
5490	while (cnt < sizeof(struct lpfc_cgn_info)) {
5491		if (len > (LPFC_CGN_BUF_SIZE - LPFC_DEBUG_OUT_LINE_SZ)) {
5492			len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
5493					 "Truncated . . .\n");
5494			goto out;
5495		}
5496		len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
5497				 "%03x: %08x %08x %08x %08x "
5498				 "%08x %08x %08x %08x\n",
5499				 cnt, *ptr, *(ptr + 1), *(ptr + 2),
5500				 *(ptr + 3), *(ptr + 4), *(ptr + 5),
5501				 *(ptr + 6), *(ptr + 7));
5502		cnt += 32;
5503		ptr += 8;
5504	}
5505	if (len > (LPFC_CGN_BUF_SIZE - LPFC_DEBUG_OUT_LINE_SZ)) {
5506		len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
5507				 "Truncated . . .\n");
5508		goto out;
5509	}
5510	len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
5511			 "Parameter Data\n");
5512	ptr = (uint32_t *)&phba->cgn_p;
5513	len += scnprintf(buffer + len, LPFC_CGN_BUF_SIZE - len,
5514			 "%08x %08x %08x %08x\n",
5515			 *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3));
5516out:
5517	return simple_read_from_buffer(buf, nbytes, ppos, buffer, len);
5518}
5519
5520static int
5521lpfc_cgn_buffer_release(struct inode *inode, struct file *file)
5522{
5523	struct lpfc_debug *debug = file->private_data;
5524
5525	vfree(debug->buffer);
5526	kfree(debug);
5527
5528	return 0;
5529}
5530
5531static int
5532lpfc_rx_monitor_open(struct inode *inode, struct file *file)
5533{
5534	struct lpfc_rx_monitor_debug *debug;
5535	int rc = -ENOMEM;
5536
5537	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
5538	if (!debug)
5539		goto out;
5540
5541	debug->buffer = vmalloc(MAX_DEBUGFS_RX_INFO_SIZE);
5542	if (!debug->buffer) {
5543		kfree(debug);
5544		goto out;
5545	}
5546
5547	debug->i_private = inode->i_private;
5548	file->private_data = debug;
5549
5550	rc = 0;
5551out:
5552	return rc;
5553}
5554
5555static ssize_t
5556lpfc_rx_monitor_read(struct file *file, char __user *buf, size_t nbytes,
5557		     loff_t *ppos)
5558{
5559	struct lpfc_rx_monitor_debug *debug = file->private_data;
5560	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
5561	char *buffer = debug->buffer;
5562
5563	if (!phba->rx_monitor) {
5564		scnprintf(buffer, MAX_DEBUGFS_RX_INFO_SIZE,
5565			  "Rx Monitor Info is empty.\n");
5566	} else {
5567		lpfc_rx_monitor_report(phba, phba->rx_monitor, buffer,
5568				       MAX_DEBUGFS_RX_INFO_SIZE,
5569				       LPFC_MAX_RXMONITOR_ENTRY);
5570	}
5571
5572	return simple_read_from_buffer(buf, nbytes, ppos, buffer,
5573				       strlen(buffer));
5574}
5575
5576static int
5577lpfc_rx_monitor_release(struct inode *inode, struct file *file)
5578{
5579	struct lpfc_rx_monitor_debug *debug = file->private_data;
5580
5581	vfree(debug->buffer);
5582	kfree(debug);
5583
5584	return 0;
5585}
5586
5587#undef lpfc_debugfs_op_disc_trc
5588static const struct file_operations lpfc_debugfs_op_disc_trc = {
5589	.owner =        THIS_MODULE,
5590	.open =         lpfc_debugfs_disc_trc_open,
5591	.llseek =       lpfc_debugfs_lseek,
5592	.read =         lpfc_debugfs_read,
5593	.release =      lpfc_debugfs_release,
5594};
5595
5596#undef lpfc_debugfs_op_nodelist
5597static const struct file_operations lpfc_debugfs_op_nodelist = {
5598	.owner =        THIS_MODULE,
5599	.open =         lpfc_debugfs_nodelist_open,
5600	.llseek =       lpfc_debugfs_lseek,
5601	.read =         lpfc_debugfs_read,
5602	.release =      lpfc_debugfs_release,
5603};
5604
5605#undef lpfc_debugfs_op_multixripools
5606static const struct file_operations lpfc_debugfs_op_multixripools = {
5607	.owner =        THIS_MODULE,
5608	.open =         lpfc_debugfs_multixripools_open,
5609	.llseek =       lpfc_debugfs_lseek,
5610	.read =         lpfc_debugfs_read,
5611	.write =	lpfc_debugfs_multixripools_write,
5612	.release =      lpfc_debugfs_release,
5613};
5614
5615#undef lpfc_debugfs_op_hbqinfo
5616static const struct file_operations lpfc_debugfs_op_hbqinfo = {
5617	.owner =        THIS_MODULE,
5618	.open =         lpfc_debugfs_hbqinfo_open,
5619	.llseek =       lpfc_debugfs_lseek,
5620	.read =         lpfc_debugfs_read,
5621	.release =      lpfc_debugfs_release,
5622};
5623
5624#ifdef LPFC_HDWQ_LOCK_STAT
5625#undef lpfc_debugfs_op_lockstat
5626static const struct file_operations lpfc_debugfs_op_lockstat = {
5627	.owner =        THIS_MODULE,
5628	.open =         lpfc_debugfs_lockstat_open,
5629	.llseek =       lpfc_debugfs_lseek,
5630	.read =         lpfc_debugfs_read,
5631	.write =        lpfc_debugfs_lockstat_write,
5632	.release =      lpfc_debugfs_release,
5633};
5634#endif
5635
5636#undef lpfc_debugfs_ras_log
5637static const struct file_operations lpfc_debugfs_ras_log = {
5638	.owner =        THIS_MODULE,
5639	.open =         lpfc_debugfs_ras_log_open,
5640	.llseek =       lpfc_debugfs_lseek,
5641	.read =         lpfc_debugfs_read,
5642	.release =      lpfc_debugfs_ras_log_release,
5643};
5644
5645#undef lpfc_debugfs_op_dumpHBASlim
5646static const struct file_operations lpfc_debugfs_op_dumpHBASlim = {
5647	.owner =        THIS_MODULE,
5648	.open =         lpfc_debugfs_dumpHBASlim_open,
5649	.llseek =       lpfc_debugfs_lseek,
5650	.read =         lpfc_debugfs_read,
5651	.release =      lpfc_debugfs_release,
5652};
5653
5654#undef lpfc_debugfs_op_dumpHostSlim
5655static const struct file_operations lpfc_debugfs_op_dumpHostSlim = {
5656	.owner =        THIS_MODULE,
5657	.open =         lpfc_debugfs_dumpHostSlim_open,
5658	.llseek =       lpfc_debugfs_lseek,
5659	.read =         lpfc_debugfs_read,
5660	.release =      lpfc_debugfs_release,
5661};
5662
5663#undef lpfc_debugfs_op_nvmestat
5664static const struct file_operations lpfc_debugfs_op_nvmestat = {
5665	.owner =        THIS_MODULE,
5666	.open =         lpfc_debugfs_nvmestat_open,
5667	.llseek =       lpfc_debugfs_lseek,
5668	.read =         lpfc_debugfs_read,
5669	.write =	lpfc_debugfs_nvmestat_write,
5670	.release =      lpfc_debugfs_release,
5671};
5672
5673#undef lpfc_debugfs_op_scsistat
5674static const struct file_operations lpfc_debugfs_op_scsistat = {
5675	.owner =        THIS_MODULE,
5676	.open =         lpfc_debugfs_scsistat_open,
5677	.llseek =       lpfc_debugfs_lseek,
5678	.read =         lpfc_debugfs_read,
5679	.write =	lpfc_debugfs_scsistat_write,
5680	.release =      lpfc_debugfs_release,
5681};
5682
5683#undef lpfc_debugfs_op_ioktime
5684static const struct file_operations lpfc_debugfs_op_ioktime = {
5685	.owner =        THIS_MODULE,
5686	.open =         lpfc_debugfs_ioktime_open,
5687	.llseek =       lpfc_debugfs_lseek,
5688	.read =         lpfc_debugfs_read,
5689	.write =	lpfc_debugfs_ioktime_write,
5690	.release =      lpfc_debugfs_release,
5691};
5692
5693#undef lpfc_debugfs_op_nvmeio_trc
5694static const struct file_operations lpfc_debugfs_op_nvmeio_trc = {
5695	.owner =        THIS_MODULE,
5696	.open =         lpfc_debugfs_nvmeio_trc_open,
5697	.llseek =       lpfc_debugfs_lseek,
5698	.read =         lpfc_debugfs_read,
5699	.write =	lpfc_debugfs_nvmeio_trc_write,
5700	.release =      lpfc_debugfs_release,
5701};
5702
5703#undef lpfc_debugfs_op_hdwqstat
5704static const struct file_operations lpfc_debugfs_op_hdwqstat = {
5705	.owner =        THIS_MODULE,
5706	.open =         lpfc_debugfs_hdwqstat_open,
5707	.llseek =       lpfc_debugfs_lseek,
5708	.read =         lpfc_debugfs_read,
5709	.write =	lpfc_debugfs_hdwqstat_write,
5710	.release =      lpfc_debugfs_release,
5711};
5712
5713#undef lpfc_debugfs_op_dif_err
5714static const struct file_operations lpfc_debugfs_op_dif_err = {
5715	.owner =	THIS_MODULE,
5716	.open =		simple_open,
5717	.llseek =	lpfc_debugfs_lseek,
5718	.read =		lpfc_debugfs_dif_err_read,
5719	.write =	lpfc_debugfs_dif_err_write,
5720	.release =	lpfc_debugfs_dif_err_release,
5721};
5722
5723#undef lpfc_debugfs_op_slow_ring_trc
5724static const struct file_operations lpfc_debugfs_op_slow_ring_trc = {
5725	.owner =        THIS_MODULE,
5726	.open =         lpfc_debugfs_slow_ring_trc_open,
5727	.llseek =       lpfc_debugfs_lseek,
5728	.read =         lpfc_debugfs_read,
5729	.release =      lpfc_debugfs_release,
5730};
5731
5732static struct dentry *lpfc_debugfs_root = NULL;
5733static atomic_t lpfc_debugfs_hba_count;
5734
5735/*
5736 * File operations for the iDiag debugfs
5737 */
5738#undef lpfc_idiag_op_pciCfg
5739static const struct file_operations lpfc_idiag_op_pciCfg = {
5740	.owner =        THIS_MODULE,
5741	.open =         lpfc_idiag_open,
5742	.llseek =       lpfc_debugfs_lseek,
5743	.read =         lpfc_idiag_pcicfg_read,
5744	.write =        lpfc_idiag_pcicfg_write,
5745	.release =      lpfc_idiag_cmd_release,
5746};
5747
5748#undef lpfc_idiag_op_barAcc
5749static const struct file_operations lpfc_idiag_op_barAcc = {
5750	.owner =        THIS_MODULE,
5751	.open =         lpfc_idiag_open,
5752	.llseek =       lpfc_debugfs_lseek,
5753	.read =         lpfc_idiag_baracc_read,
5754	.write =        lpfc_idiag_baracc_write,
5755	.release =      lpfc_idiag_cmd_release,
5756};
5757
5758#undef lpfc_idiag_op_queInfo
5759static const struct file_operations lpfc_idiag_op_queInfo = {
5760	.owner =        THIS_MODULE,
5761	.open =         lpfc_idiag_open,
5762	.read =         lpfc_idiag_queinfo_read,
5763	.release =      lpfc_idiag_release,
5764};
5765
5766#undef lpfc_idiag_op_queAcc
5767static const struct file_operations lpfc_idiag_op_queAcc = {
5768	.owner =        THIS_MODULE,
5769	.open =         lpfc_idiag_open,
5770	.llseek =       lpfc_debugfs_lseek,
5771	.read =         lpfc_idiag_queacc_read,
5772	.write =        lpfc_idiag_queacc_write,
5773	.release =      lpfc_idiag_cmd_release,
5774};
5775
5776#undef lpfc_idiag_op_drbAcc
5777static const struct file_operations lpfc_idiag_op_drbAcc = {
5778	.owner =        THIS_MODULE,
5779	.open =         lpfc_idiag_open,
5780	.llseek =       lpfc_debugfs_lseek,
5781	.read =         lpfc_idiag_drbacc_read,
5782	.write =        lpfc_idiag_drbacc_write,
5783	.release =      lpfc_idiag_cmd_release,
5784};
5785
5786#undef lpfc_idiag_op_ctlAcc
5787static const struct file_operations lpfc_idiag_op_ctlAcc = {
5788	.owner =        THIS_MODULE,
5789	.open =         lpfc_idiag_open,
5790	.llseek =       lpfc_debugfs_lseek,
5791	.read =         lpfc_idiag_ctlacc_read,
5792	.write =        lpfc_idiag_ctlacc_write,
5793	.release =      lpfc_idiag_cmd_release,
5794};
5795
5796#undef lpfc_idiag_op_mbxAcc
5797static const struct file_operations lpfc_idiag_op_mbxAcc = {
5798	.owner =        THIS_MODULE,
5799	.open =         lpfc_idiag_open,
5800	.llseek =       lpfc_debugfs_lseek,
5801	.read =         lpfc_idiag_mbxacc_read,
5802	.write =        lpfc_idiag_mbxacc_write,
5803	.release =      lpfc_idiag_cmd_release,
5804};
5805
5806#undef lpfc_idiag_op_extAcc
5807static const struct file_operations lpfc_idiag_op_extAcc = {
5808	.owner =        THIS_MODULE,
5809	.open =         lpfc_idiag_open,
5810	.llseek =       lpfc_debugfs_lseek,
5811	.read =         lpfc_idiag_extacc_read,
5812	.write =        lpfc_idiag_extacc_write,
5813	.release =      lpfc_idiag_cmd_release,
5814};
5815#undef lpfc_cgn_buffer_op
5816static const struct file_operations lpfc_cgn_buffer_op = {
5817	.owner =        THIS_MODULE,
5818	.open =         lpfc_cgn_buffer_open,
5819	.llseek =       lpfc_debugfs_lseek,
5820	.read =         lpfc_cgn_buffer_read,
5821	.release =      lpfc_cgn_buffer_release,
5822};
5823
5824#undef lpfc_rx_monitor_op
5825static const struct file_operations lpfc_rx_monitor_op = {
5826	.owner =        THIS_MODULE,
5827	.open =         lpfc_rx_monitor_open,
5828	.llseek =       lpfc_debugfs_lseek,
5829	.read =         lpfc_rx_monitor_read,
5830	.release =      lpfc_rx_monitor_release,
5831};
5832#endif
5833
5834/* lpfc_idiag_mbxacc_dump_bsg_mbox - idiag debugfs dump bsg mailbox command
5835 * @phba: Pointer to HBA context object.
5836 * @dmabuf: Pointer to a DMA buffer descriptor.
5837 *
5838 * Description:
5839 * This routine dump a bsg pass-through non-embedded mailbox command with
5840 * external buffer.
5841 **/
5842void
5843lpfc_idiag_mbxacc_dump_bsg_mbox(struct lpfc_hba *phba, enum nemb_type nemb_tp,
5844				enum mbox_type mbox_tp, enum dma_type dma_tp,
5845				enum sta_type sta_tp,
5846				struct lpfc_dmabuf *dmabuf, uint32_t ext_buf)
5847{
5848#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5849	uint32_t *mbx_mbox_cmd, *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt;
5850	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
5851	int len = 0;
5852	uint32_t do_dump = 0;
5853	uint32_t *pword;
5854	uint32_t i;
5855
5856	if (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP)
5857		return;
5858
5859	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5860	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5861	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5862	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5863
5864	if (!(*mbx_dump_map & LPFC_MBX_DMP_ALL) ||
5865	    (*mbx_dump_cnt == 0) ||
5866	    (*mbx_word_cnt == 0))
5867		return;
5868
5869	if (*mbx_mbox_cmd != 0x9B)
5870		return;
5871
5872	if ((mbox_tp == mbox_rd) && (dma_tp == dma_mbox)) {
5873		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_MBX) {
5874			do_dump |= LPFC_BSG_DMP_MBX_RD_MBX;
5875			pr_err("\nRead mbox command (x%x), "
5876			       "nemb:0x%x, extbuf_cnt:%d:\n",
5877			       sta_tp, nemb_tp, ext_buf);
5878		}
5879	}
5880	if ((mbox_tp == mbox_rd) && (dma_tp == dma_ebuf)) {
5881		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_BUF) {
5882			do_dump |= LPFC_BSG_DMP_MBX_RD_BUF;
5883			pr_err("\nRead mbox buffer (x%x), "
5884			       "nemb:0x%x, extbuf_seq:%d:\n",
5885			       sta_tp, nemb_tp, ext_buf);
5886		}
5887	}
5888	if ((mbox_tp == mbox_wr) && (dma_tp == dma_mbox)) {
5889		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_MBX) {
5890			do_dump |= LPFC_BSG_DMP_MBX_WR_MBX;
5891			pr_err("\nWrite mbox command (x%x), "
5892			       "nemb:0x%x, extbuf_cnt:%d:\n",
5893			       sta_tp, nemb_tp, ext_buf);
5894		}
5895	}
5896	if ((mbox_tp == mbox_wr) && (dma_tp == dma_ebuf)) {
5897		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_BUF) {
5898			do_dump |= LPFC_BSG_DMP_MBX_WR_BUF;
5899			pr_err("\nWrite mbox buffer (x%x), "
5900			       "nemb:0x%x, extbuf_seq:%d:\n",
5901			       sta_tp, nemb_tp, ext_buf);
5902		}
5903	}
5904
5905	/* dump buffer content */
5906	if (do_dump) {
5907		pword = (uint32_t *)dmabuf->virt;
5908		for (i = 0; i < *mbx_word_cnt; i++) {
5909			if (!(i % 8)) {
5910				if (i != 0)
5911					pr_err("%s\n", line_buf);
5912				len = 0;
5913				len += scnprintf(line_buf+len,
5914						LPFC_MBX_ACC_LBUF_SZ-len,
5915						"%03d: ", i);
5916			}
5917			len += scnprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
5918					"%08x ", (uint32_t)*pword);
5919			pword++;
5920		}
5921		if ((i - 1) % 8)
5922			pr_err("%s\n", line_buf);
5923		(*mbx_dump_cnt)--;
5924	}
5925
5926	/* Clean out command structure on reaching dump count */
5927	if (*mbx_dump_cnt == 0)
5928		memset(&idiag, 0, sizeof(idiag));
5929	return;
5930#endif
5931}
5932
5933/* lpfc_idiag_mbxacc_dump_issue_mbox - idiag debugfs dump issue mailbox command
5934 * @phba: Pointer to HBA context object.
5935 * @dmabuf: Pointer to a DMA buffer descriptor.
5936 *
5937 * Description:
5938 * This routine dump a pass-through non-embedded mailbox command from issue
5939 * mailbox command.
5940 **/
5941void
5942lpfc_idiag_mbxacc_dump_issue_mbox(struct lpfc_hba *phba, MAILBOX_t *pmbox)
5943{
5944#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5945	uint32_t *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt, *mbx_mbox_cmd;
5946	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
5947	int len = 0;
5948	uint32_t *pword;
5949	uint8_t *pbyte;
5950	uint32_t i, j;
5951
5952	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP)
5953		return;
5954
5955	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5956	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5957	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5958	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5959
5960	if (!(*mbx_dump_map & LPFC_MBX_DMP_MBX_ALL) ||
5961	    (*mbx_dump_cnt == 0) ||
5962	    (*mbx_word_cnt == 0))
5963		return;
5964
5965	if ((*mbx_mbox_cmd != LPFC_MBX_ALL_CMD) &&
5966	    (*mbx_mbox_cmd != pmbox->mbxCommand))
5967		return;
5968
5969	/* dump buffer content */
5970	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_WORD) {
5971		pr_err("Mailbox command:0x%x dump by word:\n",
5972		       pmbox->mbxCommand);
5973		pword = (uint32_t *)pmbox;
5974		for (i = 0; i < *mbx_word_cnt; i++) {
5975			if (!(i % 8)) {
5976				if (i != 0)
5977					pr_err("%s\n", line_buf);
5978				len = 0;
5979				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
5980				len += scnprintf(line_buf+len,
5981						LPFC_MBX_ACC_LBUF_SZ-len,
5982						"%03d: ", i);
5983			}
5984			len += scnprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
5985					"%08x ",
5986					((uint32_t)*pword) & 0xffffffff);
5987			pword++;
5988		}
5989		if ((i - 1) % 8)
5990			pr_err("%s\n", line_buf);
5991		pr_err("\n");
5992	}
5993	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_BYTE) {
5994		pr_err("Mailbox command:0x%x dump by byte:\n",
5995		       pmbox->mbxCommand);
5996		pbyte = (uint8_t *)pmbox;
5997		for (i = 0; i < *mbx_word_cnt; i++) {
5998			if (!(i % 8)) {
5999				if (i != 0)
6000					pr_err("%s\n", line_buf);
6001				len = 0;
6002				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
6003				len += scnprintf(line_buf+len,
6004						LPFC_MBX_ACC_LBUF_SZ-len,
6005						"%03d: ", i);
6006			}
6007			for (j = 0; j < 4; j++) {
6008				len += scnprintf(line_buf+len,
6009						LPFC_MBX_ACC_LBUF_SZ-len,
6010						"%02x",
6011						((uint8_t)*pbyte) & 0xff);
6012				pbyte++;
6013			}
6014			len += scnprintf(line_buf+len,
6015					LPFC_MBX_ACC_LBUF_SZ-len, " ");
6016		}
6017		if ((i - 1) % 8)
6018			pr_err("%s\n", line_buf);
6019		pr_err("\n");
6020	}
6021	(*mbx_dump_cnt)--;
6022
6023	/* Clean out command structure on reaching dump count */
6024	if (*mbx_dump_cnt == 0)
6025		memset(&idiag, 0, sizeof(idiag));
6026	return;
6027#endif
6028}
6029
6030/**
6031 * lpfc_debugfs_initialize - Initialize debugfs for a vport
6032 * @vport: The vport pointer to initialize.
6033 *
6034 * Description:
6035 * When Debugfs is configured this routine sets up the lpfc debugfs file system.
6036 * If not already created, this routine will create the lpfc directory, and
6037 * lpfcX directory (for this HBA), and vportX directory for this vport. It will
6038 * also create each file used to access lpfc specific debugfs information.
6039 **/
6040inline void
6041lpfc_debugfs_initialize(struct lpfc_vport *vport)
6042{
6043#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
6044	struct lpfc_hba   *phba = vport->phba;
6045	char name[64];
6046	uint32_t num, i;
6047	bool pport_setup = false;
6048
6049	if (!lpfc_debugfs_enable)
6050		return;
6051
6052	/* Setup lpfc root directory */
6053	if (!lpfc_debugfs_root) {
6054		lpfc_debugfs_root = debugfs_create_dir("lpfc", NULL);
6055		atomic_set(&lpfc_debugfs_hba_count, 0);
6056	}
6057	if (!lpfc_debugfs_start_time)
6058		lpfc_debugfs_start_time = jiffies;
6059
6060	/* Setup funcX directory for specific HBA PCI function */
6061	snprintf(name, sizeof(name), "fn%d", phba->brd_no);
6062	if (!phba->hba_debugfs_root) {
6063		pport_setup = true;
6064		phba->hba_debugfs_root =
6065			debugfs_create_dir(name, lpfc_debugfs_root);
6066		atomic_inc(&lpfc_debugfs_hba_count);
6067		atomic_set(&phba->debugfs_vport_count, 0);
6068
6069		/* Multi-XRI pools */
6070		snprintf(name, sizeof(name), "multixripools");
6071		phba->debug_multixri_pools =
6072			debugfs_create_file(name, S_IFREG | 0644,
6073					    phba->hba_debugfs_root,
6074					    phba,
6075					    &lpfc_debugfs_op_multixripools);
6076		if (IS_ERR(phba->debug_multixri_pools)) {
6077			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6078					 "0527 Cannot create debugfs multixripools\n");
6079			goto debug_failed;
6080		}
6081
6082		/* Congestion Info Buffer */
6083		scnprintf(name, sizeof(name), "cgn_buffer");
6084		phba->debug_cgn_buffer =
6085			debugfs_create_file(name, S_IFREG | 0644,
6086					    phba->hba_debugfs_root,
6087					    phba, &lpfc_cgn_buffer_op);
6088		if (IS_ERR(phba->debug_cgn_buffer)) {
6089			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6090					 "6527 Cannot create debugfs "
6091					 "cgn_buffer\n");
6092			goto debug_failed;
6093		}
6094
6095		/* RX Monitor */
6096		scnprintf(name, sizeof(name), "rx_monitor");
6097		phba->debug_rx_monitor =
6098			debugfs_create_file(name, S_IFREG | 0644,
6099					    phba->hba_debugfs_root,
6100					    phba, &lpfc_rx_monitor_op);
6101		if (IS_ERR(phba->debug_rx_monitor)) {
6102			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6103					 "6528 Cannot create debugfs "
6104					 "rx_monitor\n");
6105			goto debug_failed;
6106		}
6107
6108		/* RAS log */
6109		snprintf(name, sizeof(name), "ras_log");
6110		phba->debug_ras_log =
6111			debugfs_create_file(name, 0644,
6112					    phba->hba_debugfs_root,
6113					    phba, &lpfc_debugfs_ras_log);
6114		if (IS_ERR(phba->debug_ras_log)) {
6115			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6116					 "6148 Cannot create debugfs"
6117					 " ras_log\n");
6118			goto debug_failed;
6119		}
6120
6121		/* Setup hbqinfo */
6122		snprintf(name, sizeof(name), "hbqinfo");
6123		phba->debug_hbqinfo =
6124			debugfs_create_file(name, S_IFREG | 0644,
6125					    phba->hba_debugfs_root,
6126					    phba, &lpfc_debugfs_op_hbqinfo);
6127
6128#ifdef LPFC_HDWQ_LOCK_STAT
6129		/* Setup lockstat */
6130		snprintf(name, sizeof(name), "lockstat");
6131		phba->debug_lockstat =
6132			debugfs_create_file(name, S_IFREG | 0644,
6133					    phba->hba_debugfs_root,
6134					    phba, &lpfc_debugfs_op_lockstat);
6135		if (IS_ERR(phba->debug_lockstat)) {
6136			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6137					 "4610 Can't create debugfs lockstat\n");
6138			goto debug_failed;
6139		}
6140#endif
6141
6142		/* Setup dumpHBASlim */
6143		if (phba->sli_rev < LPFC_SLI_REV4) {
6144			snprintf(name, sizeof(name), "dumpHBASlim");
6145			phba->debug_dumpHBASlim =
6146				debugfs_create_file(name,
6147					S_IFREG|S_IRUGO|S_IWUSR,
6148					phba->hba_debugfs_root,
6149					phba, &lpfc_debugfs_op_dumpHBASlim);
6150		} else
6151			phba->debug_dumpHBASlim = NULL;
6152
6153		/* Setup dumpHostSlim */
6154		if (phba->sli_rev < LPFC_SLI_REV4) {
6155			snprintf(name, sizeof(name), "dumpHostSlim");
6156			phba->debug_dumpHostSlim =
6157				debugfs_create_file(name,
6158					S_IFREG|S_IRUGO|S_IWUSR,
6159					phba->hba_debugfs_root,
6160					phba, &lpfc_debugfs_op_dumpHostSlim);
6161		} else
6162			phba->debug_dumpHostSlim = NULL;
6163
6164		/* Setup DIF Error Injections */
6165		snprintf(name, sizeof(name), "InjErrLBA");
6166		phba->debug_InjErrLBA =
6167			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6168			phba->hba_debugfs_root,
6169			phba, &lpfc_debugfs_op_dif_err);
6170		phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
6171
6172		snprintf(name, sizeof(name), "InjErrNPortID");
6173		phba->debug_InjErrNPortID =
6174			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6175			phba->hba_debugfs_root,
6176			phba, &lpfc_debugfs_op_dif_err);
6177
6178		snprintf(name, sizeof(name), "InjErrWWPN");
6179		phba->debug_InjErrWWPN =
6180			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6181			phba->hba_debugfs_root,
6182			phba, &lpfc_debugfs_op_dif_err);
6183
6184		snprintf(name, sizeof(name), "writeGuardInjErr");
6185		phba->debug_writeGuard =
6186			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6187			phba->hba_debugfs_root,
6188			phba, &lpfc_debugfs_op_dif_err);
6189
6190		snprintf(name, sizeof(name), "writeAppInjErr");
6191		phba->debug_writeApp =
6192			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6193			phba->hba_debugfs_root,
6194			phba, &lpfc_debugfs_op_dif_err);
6195
6196		snprintf(name, sizeof(name), "writeRefInjErr");
6197		phba->debug_writeRef =
6198			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6199			phba->hba_debugfs_root,
6200			phba, &lpfc_debugfs_op_dif_err);
6201
6202		snprintf(name, sizeof(name), "readGuardInjErr");
6203		phba->debug_readGuard =
6204			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6205			phba->hba_debugfs_root,
6206			phba, &lpfc_debugfs_op_dif_err);
6207
6208		snprintf(name, sizeof(name), "readAppInjErr");
6209		phba->debug_readApp =
6210			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6211			phba->hba_debugfs_root,
6212			phba, &lpfc_debugfs_op_dif_err);
6213
6214		snprintf(name, sizeof(name), "readRefInjErr");
6215		phba->debug_readRef =
6216			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6217			phba->hba_debugfs_root,
6218			phba, &lpfc_debugfs_op_dif_err);
6219
6220		/* Setup slow ring trace */
6221		if (lpfc_debugfs_max_slow_ring_trc) {
6222			num = lpfc_debugfs_max_slow_ring_trc - 1;
6223			if (num & lpfc_debugfs_max_slow_ring_trc) {
6224				/* Change to be a power of 2 */
6225				num = lpfc_debugfs_max_slow_ring_trc;
6226				i = 0;
6227				while (num > 1) {
6228					num = num >> 1;
6229					i++;
6230				}
6231				lpfc_debugfs_max_slow_ring_trc = (1 << i);
6232				pr_err("lpfc_debugfs_max_disc_trc changed to "
6233				       "%d\n", lpfc_debugfs_max_disc_trc);
6234			}
6235		}
6236
6237		snprintf(name, sizeof(name), "slow_ring_trace");
6238		phba->debug_slow_ring_trc =
6239			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6240				 phba->hba_debugfs_root,
6241				 phba, &lpfc_debugfs_op_slow_ring_trc);
6242		if (!phba->slow_ring_trc) {
6243			phba->slow_ring_trc = kcalloc(
6244				lpfc_debugfs_max_slow_ring_trc,
6245				sizeof(struct lpfc_debugfs_trc),
6246				GFP_KERNEL);
6247			if (!phba->slow_ring_trc) {
6248				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6249						 "0416 Cannot create debugfs "
6250						 "slow_ring buffer\n");
6251				goto debug_failed;
6252			}
6253			atomic_set(&phba->slow_ring_trc_cnt, 0);
 
 
 
6254		}
6255
6256		snprintf(name, sizeof(name), "nvmeio_trc");
6257		phba->debug_nvmeio_trc =
6258			debugfs_create_file(name, 0644,
6259					    phba->hba_debugfs_root,
6260					    phba, &lpfc_debugfs_op_nvmeio_trc);
6261
6262		atomic_set(&phba->nvmeio_trc_cnt, 0);
6263		if (lpfc_debugfs_max_nvmeio_trc) {
6264			num = lpfc_debugfs_max_nvmeio_trc - 1;
6265			if (num & lpfc_debugfs_max_disc_trc) {
6266				/* Change to be a power of 2 */
6267				num = lpfc_debugfs_max_nvmeio_trc;
6268				i = 0;
6269				while (num > 1) {
6270					num = num >> 1;
6271					i++;
6272				}
6273				lpfc_debugfs_max_nvmeio_trc = (1 << i);
6274				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6275						"0575 lpfc_debugfs_max_nvmeio_trc "
6276						"changed to %d\n",
6277						lpfc_debugfs_max_nvmeio_trc);
6278			}
6279			phba->nvmeio_trc_size = lpfc_debugfs_max_nvmeio_trc;
6280
6281			/* Allocate trace buffer and initialize */
6282			phba->nvmeio_trc = kzalloc(
6283				(sizeof(struct lpfc_debugfs_nvmeio_trc) *
6284				phba->nvmeio_trc_size), GFP_KERNEL);
6285
6286			if (!phba->nvmeio_trc) {
6287				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6288						"0576 Cannot create debugfs "
6289						"nvmeio_trc buffer\n");
6290				goto nvmeio_off;
6291			}
6292			phba->nvmeio_trc_on = 1;
6293			phba->nvmeio_trc_output_idx = 0;
6294			phba->nvmeio_trc = NULL;
6295		} else {
6296nvmeio_off:
6297			phba->nvmeio_trc_size = 0;
6298			phba->nvmeio_trc_on = 0;
6299			phba->nvmeio_trc_output_idx = 0;
6300			phba->nvmeio_trc = NULL;
6301		}
6302	}
6303
6304	snprintf(name, sizeof(name), "vport%d", vport->vpi);
6305	if (!vport->vport_debugfs_root) {
6306		vport->vport_debugfs_root =
6307			debugfs_create_dir(name, phba->hba_debugfs_root);
6308		atomic_inc(&phba->debugfs_vport_count);
6309	}
6310
6311	if (lpfc_debugfs_max_disc_trc) {
6312		num = lpfc_debugfs_max_disc_trc - 1;
6313		if (num & lpfc_debugfs_max_disc_trc) {
6314			/* Change to be a power of 2 */
6315			num = lpfc_debugfs_max_disc_trc;
6316			i = 0;
6317			while (num > 1) {
6318				num = num >> 1;
6319				i++;
6320			}
6321			lpfc_debugfs_max_disc_trc = (1 << i);
6322			pr_err("lpfc_debugfs_max_disc_trc changed to %d\n",
6323			       lpfc_debugfs_max_disc_trc);
6324		}
6325	}
6326
6327	vport->disc_trc = kzalloc(
6328		(sizeof(struct lpfc_debugfs_trc) * lpfc_debugfs_max_disc_trc),
6329		GFP_KERNEL);
6330
6331	if (!vport->disc_trc) {
6332		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6333				 "0418 Cannot create debugfs disc trace "
6334				 "buffer\n");
6335		goto debug_failed;
6336	}
6337	atomic_set(&vport->disc_trc_cnt, 0);
6338
6339	snprintf(name, sizeof(name), "discovery_trace");
6340	vport->debug_disc_trc =
6341		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6342				 vport->vport_debugfs_root,
6343				 vport, &lpfc_debugfs_op_disc_trc);
6344	snprintf(name, sizeof(name), "nodelist");
6345	vport->debug_nodelist =
6346		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6347				 vport->vport_debugfs_root,
6348				 vport, &lpfc_debugfs_op_nodelist);
6349
6350	snprintf(name, sizeof(name), "nvmestat");
6351	vport->debug_nvmestat =
6352		debugfs_create_file(name, 0644,
6353				    vport->vport_debugfs_root,
6354				    vport, &lpfc_debugfs_op_nvmestat);
6355
6356	snprintf(name, sizeof(name), "scsistat");
6357	vport->debug_scsistat =
6358		debugfs_create_file(name, 0644,
6359				    vport->vport_debugfs_root,
6360				    vport, &lpfc_debugfs_op_scsistat);
6361	if (IS_ERR(vport->debug_scsistat)) {
6362		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6363				 "4611 Cannot create debugfs scsistat\n");
6364		goto debug_failed;
6365	}
6366
6367	snprintf(name, sizeof(name), "ioktime");
6368	vport->debug_ioktime =
6369		debugfs_create_file(name, 0644,
6370				    vport->vport_debugfs_root,
6371				    vport, &lpfc_debugfs_op_ioktime);
6372	if (IS_ERR(vport->debug_ioktime)) {
6373		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6374				 "0815 Cannot create debugfs ioktime\n");
6375		goto debug_failed;
6376	}
6377
6378	snprintf(name, sizeof(name), "hdwqstat");
6379	vport->debug_hdwqstat =
6380		debugfs_create_file(name, 0644,
6381				    vport->vport_debugfs_root,
6382				    vport, &lpfc_debugfs_op_hdwqstat);
6383
6384	/*
6385	 * The following section is for additional directories/files for the
6386	 * physical port.
6387	 */
6388
6389	if (!pport_setup)
6390		goto debug_failed;
6391
6392	/*
6393	 * iDiag debugfs root entry points for SLI4 device only
6394	 */
6395	if (phba->sli_rev < LPFC_SLI_REV4)
6396		goto debug_failed;
6397
6398	snprintf(name, sizeof(name), "iDiag");
6399	if (!phba->idiag_root) {
6400		phba->idiag_root =
6401			debugfs_create_dir(name, phba->hba_debugfs_root);
6402		/* Initialize iDiag data structure */
6403		memset(&idiag, 0, sizeof(idiag));
6404	}
6405
6406	/* iDiag read PCI config space */
6407	snprintf(name, sizeof(name), "pciCfg");
6408	if (!phba->idiag_pci_cfg) {
6409		phba->idiag_pci_cfg =
6410			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6411				phba->idiag_root, phba, &lpfc_idiag_op_pciCfg);
6412		idiag.offset.last_rd = 0;
6413	}
6414
6415	/* iDiag PCI BAR access */
6416	snprintf(name, sizeof(name), "barAcc");
6417	if (!phba->idiag_bar_acc) {
6418		phba->idiag_bar_acc =
6419			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6420				phba->idiag_root, phba, &lpfc_idiag_op_barAcc);
6421		idiag.offset.last_rd = 0;
6422	}
6423
6424	/* iDiag get PCI function queue information */
6425	snprintf(name, sizeof(name), "queInfo");
6426	if (!phba->idiag_que_info) {
6427		phba->idiag_que_info =
6428			debugfs_create_file(name, S_IFREG|S_IRUGO,
6429			phba->idiag_root, phba, &lpfc_idiag_op_queInfo);
6430	}
6431
6432	/* iDiag access PCI function queue */
6433	snprintf(name, sizeof(name), "queAcc");
6434	if (!phba->idiag_que_acc) {
6435		phba->idiag_que_acc =
6436			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6437				phba->idiag_root, phba, &lpfc_idiag_op_queAcc);
6438	}
6439
6440	/* iDiag access PCI function doorbell registers */
6441	snprintf(name, sizeof(name), "drbAcc");
6442	if (!phba->idiag_drb_acc) {
6443		phba->idiag_drb_acc =
6444			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6445				phba->idiag_root, phba, &lpfc_idiag_op_drbAcc);
6446	}
6447
6448	/* iDiag access PCI function control registers */
6449	snprintf(name, sizeof(name), "ctlAcc");
6450	if (!phba->idiag_ctl_acc) {
6451		phba->idiag_ctl_acc =
6452			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6453				phba->idiag_root, phba, &lpfc_idiag_op_ctlAcc);
6454	}
6455
6456	/* iDiag access mbox commands */
6457	snprintf(name, sizeof(name), "mbxAcc");
6458	if (!phba->idiag_mbx_acc) {
6459		phba->idiag_mbx_acc =
6460			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6461				phba->idiag_root, phba, &lpfc_idiag_op_mbxAcc);
6462	}
6463
6464	/* iDiag extents access commands */
6465	if (phba->sli4_hba.extents_in_use) {
6466		snprintf(name, sizeof(name), "extAcc");
6467		if (!phba->idiag_ext_acc) {
6468			phba->idiag_ext_acc =
6469				debugfs_create_file(name,
6470						    S_IFREG|S_IRUGO|S_IWUSR,
6471						    phba->idiag_root, phba,
6472						    &lpfc_idiag_op_extAcc);
6473		}
6474	}
6475
6476debug_failed:
6477	return;
6478#endif
6479}
6480
6481/**
6482 * lpfc_debugfs_terminate -  Tear down debugfs infrastructure for this vport
6483 * @vport: The vport pointer to remove from debugfs.
6484 *
6485 * Description:
6486 * When Debugfs is configured this routine removes debugfs file system elements
6487 * that are specific to this vport. It also checks to see if there are any
6488 * users left for the debugfs directories associated with the HBA and driver. If
6489 * this is the last user of the HBA directory or driver directory then it will
6490 * remove those from the debugfs infrastructure as well.
6491 **/
6492inline void
6493lpfc_debugfs_terminate(struct lpfc_vport *vport)
6494{
6495#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
6496	struct lpfc_hba   *phba = vport->phba;
6497
6498	kfree(vport->disc_trc);
6499	vport->disc_trc = NULL;
6500
6501	debugfs_remove(vport->debug_disc_trc); /* discovery_trace */
6502	vport->debug_disc_trc = NULL;
6503
6504	debugfs_remove(vport->debug_nodelist); /* nodelist */
6505	vport->debug_nodelist = NULL;
6506
6507	debugfs_remove(vport->debug_nvmestat); /* nvmestat */
6508	vport->debug_nvmestat = NULL;
6509
6510	debugfs_remove(vport->debug_scsistat); /* scsistat */
6511	vport->debug_scsistat = NULL;
6512
6513	debugfs_remove(vport->debug_ioktime); /* ioktime */
6514	vport->debug_ioktime = NULL;
6515
6516	debugfs_remove(vport->debug_hdwqstat); /* hdwqstat */
6517	vport->debug_hdwqstat = NULL;
6518
6519	if (vport->vport_debugfs_root) {
6520		debugfs_remove(vport->vport_debugfs_root); /* vportX */
6521		vport->vport_debugfs_root = NULL;
6522		atomic_dec(&phba->debugfs_vport_count);
6523	}
6524
6525	if (atomic_read(&phba->debugfs_vport_count) == 0) {
6526
6527		debugfs_remove(phba->debug_multixri_pools); /* multixripools*/
6528		phba->debug_multixri_pools = NULL;
6529
6530		debugfs_remove(phba->debug_hbqinfo); /* hbqinfo */
6531		phba->debug_hbqinfo = NULL;
6532
6533		debugfs_remove(phba->debug_cgn_buffer);
6534		phba->debug_cgn_buffer = NULL;
6535
6536		debugfs_remove(phba->debug_rx_monitor);
6537		phba->debug_rx_monitor = NULL;
6538
6539		debugfs_remove(phba->debug_ras_log);
6540		phba->debug_ras_log = NULL;
6541
6542#ifdef LPFC_HDWQ_LOCK_STAT
6543		debugfs_remove(phba->debug_lockstat); /* lockstat */
6544		phba->debug_lockstat = NULL;
6545#endif
6546		debugfs_remove(phba->debug_dumpHBASlim); /* HBASlim */
6547		phba->debug_dumpHBASlim = NULL;
6548
6549		debugfs_remove(phba->debug_dumpHostSlim); /* HostSlim */
6550		phba->debug_dumpHostSlim = NULL;
6551
6552		debugfs_remove(phba->debug_InjErrLBA); /* InjErrLBA */
6553		phba->debug_InjErrLBA = NULL;
6554
6555		debugfs_remove(phba->debug_InjErrNPortID);
6556		phba->debug_InjErrNPortID = NULL;
6557
6558		debugfs_remove(phba->debug_InjErrWWPN); /* InjErrWWPN */
6559		phba->debug_InjErrWWPN = NULL;
6560
6561		debugfs_remove(phba->debug_writeGuard); /* writeGuard */
6562		phba->debug_writeGuard = NULL;
6563
6564		debugfs_remove(phba->debug_writeApp); /* writeApp */
6565		phba->debug_writeApp = NULL;
6566
6567		debugfs_remove(phba->debug_writeRef); /* writeRef */
6568		phba->debug_writeRef = NULL;
6569
6570		debugfs_remove(phba->debug_readGuard); /* readGuard */
6571		phba->debug_readGuard = NULL;
6572
6573		debugfs_remove(phba->debug_readApp); /* readApp */
6574		phba->debug_readApp = NULL;
6575
6576		debugfs_remove(phba->debug_readRef); /* readRef */
6577		phba->debug_readRef = NULL;
6578
6579		kfree(phba->slow_ring_trc);
6580		phba->slow_ring_trc = NULL;
6581
6582		/* slow_ring_trace */
6583		debugfs_remove(phba->debug_slow_ring_trc);
6584		phba->debug_slow_ring_trc = NULL;
6585
6586		debugfs_remove(phba->debug_nvmeio_trc);
6587		phba->debug_nvmeio_trc = NULL;
6588
6589		kfree(phba->nvmeio_trc);
6590		phba->nvmeio_trc = NULL;
6591
6592		/*
6593		 * iDiag release
6594		 */
6595		if (phba->sli_rev == LPFC_SLI_REV4) {
6596			/* iDiag extAcc */
6597			debugfs_remove(phba->idiag_ext_acc);
6598			phba->idiag_ext_acc = NULL;
6599
6600			/* iDiag mbxAcc */
6601			debugfs_remove(phba->idiag_mbx_acc);
6602			phba->idiag_mbx_acc = NULL;
6603
6604			/* iDiag ctlAcc */
6605			debugfs_remove(phba->idiag_ctl_acc);
6606			phba->idiag_ctl_acc = NULL;
6607
6608			/* iDiag drbAcc */
6609			debugfs_remove(phba->idiag_drb_acc);
6610			phba->idiag_drb_acc = NULL;
6611
6612			/* iDiag queAcc */
6613			debugfs_remove(phba->idiag_que_acc);
6614			phba->idiag_que_acc = NULL;
6615
6616			/* iDiag queInfo */
6617			debugfs_remove(phba->idiag_que_info);
6618			phba->idiag_que_info = NULL;
6619
6620			/* iDiag barAcc */
6621			debugfs_remove(phba->idiag_bar_acc);
6622			phba->idiag_bar_acc = NULL;
6623
6624			/* iDiag pciCfg */
6625			debugfs_remove(phba->idiag_pci_cfg);
6626			phba->idiag_pci_cfg = NULL;
6627
6628			/* Finally remove the iDiag debugfs root */
6629			debugfs_remove(phba->idiag_root);
6630			phba->idiag_root = NULL;
6631		}
6632
6633		if (phba->hba_debugfs_root) {
6634			debugfs_remove(phba->hba_debugfs_root); /* fnX */
6635			phba->hba_debugfs_root = NULL;
6636			atomic_dec(&lpfc_debugfs_hba_count);
6637		}
6638
6639		if (atomic_read(&lpfc_debugfs_hba_count) == 0) {
6640			debugfs_remove(lpfc_debugfs_root); /* lpfc */
6641			lpfc_debugfs_root = NULL;
6642		}
6643	}
6644#endif
6645	return;
6646}
6647
6648/*
6649 * Driver debug utility routines outside of debugfs. The debug utility
6650 * routines implemented here is intended to be used in the instrumented
6651 * debug driver for debugging host or port issues.
6652 */
6653
6654/**
6655 * lpfc_debug_dump_all_queues - dump all the queues with a hba
6656 * @phba: Pointer to HBA context object.
6657 *
6658 * This function dumps entries of all the queues asociated with the @phba.
6659 **/
6660void
6661lpfc_debug_dump_all_queues(struct lpfc_hba *phba)
6662{
6663	int idx;
6664
6665	/*
6666	 * Dump Work Queues (WQs)
6667	 */
6668	lpfc_debug_dump_wq(phba, DUMP_MBX, 0);
6669	lpfc_debug_dump_wq(phba, DUMP_ELS, 0);
6670	lpfc_debug_dump_wq(phba, DUMP_NVMELS, 0);
6671
6672	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6673		lpfc_debug_dump_wq(phba, DUMP_IO, idx);
6674
6675	lpfc_debug_dump_hdr_rq(phba);
6676	lpfc_debug_dump_dat_rq(phba);
6677	/*
6678	 * Dump Complete Queues (CQs)
6679	 */
6680	lpfc_debug_dump_cq(phba, DUMP_MBX, 0);
6681	lpfc_debug_dump_cq(phba, DUMP_ELS, 0);
6682	lpfc_debug_dump_cq(phba, DUMP_NVMELS, 0);
6683
6684	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6685		lpfc_debug_dump_cq(phba, DUMP_IO, idx);
6686
6687	/*
6688	 * Dump Event Queues (EQs)
6689	 */
6690	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6691		lpfc_debug_dump_hba_eq(phba, idx);
6692}
v5.4
   1/*******************************************************************
   2 * This file is part of the Emulex Linux Device Driver for         *
   3 * Fibre Channel Host Bus Adapters.                                *
   4 * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
   5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  *
   6 * Copyright (C) 2007-2015 Emulex.  All rights reserved.           *
   7 * EMULEX and SLI are trademarks of Emulex.                        *
   8 * www.broadcom.com                                                *
   9 *                                                                 *
  10 * This program is free software; you can redistribute it and/or   *
  11 * modify it under the terms of version 2 of the GNU General       *
  12 * Public License as published by the Free Software Foundation.    *
  13 * This program is distributed in the hope that it will be useful. *
  14 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
  15 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
  16 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
  17 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  18 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
  19 * more details, a copy of which can be found in the file COPYING  *
  20 * included with this package.                                     *
  21 *******************************************************************/
  22
  23#include <linux/blkdev.h>
  24#include <linux/delay.h>
  25#include <linux/module.h>
  26#include <linux/dma-mapping.h>
  27#include <linux/idr.h>
  28#include <linux/interrupt.h>
  29#include <linux/kthread.h>
  30#include <linux/slab.h>
  31#include <linux/pci.h>
  32#include <linux/spinlock.h>
  33#include <linux/ctype.h>
 
  34
  35#include <scsi/scsi.h>
  36#include <scsi/scsi_device.h>
  37#include <scsi/scsi_host.h>
  38#include <scsi/scsi_transport_fc.h>
  39#include <scsi/fc/fc_fs.h>
  40
  41#include <linux/nvme-fc-driver.h>
  42
  43#include "lpfc_hw4.h"
  44#include "lpfc_hw.h"
  45#include "lpfc_sli.h"
  46#include "lpfc_sli4.h"
  47#include "lpfc_nl.h"
  48#include "lpfc_disc.h"
  49#include "lpfc.h"
  50#include "lpfc_scsi.h"
  51#include "lpfc_nvme.h"
  52#include "lpfc_nvmet.h"
  53#include "lpfc_logmsg.h"
  54#include "lpfc_crtn.h"
  55#include "lpfc_vport.h"
  56#include "lpfc_version.h"
  57#include "lpfc_compat.h"
  58#include "lpfc_debugfs.h"
  59#include "lpfc_bsg.h"
  60
  61#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  62/*
  63 * debugfs interface
  64 *
  65 * To access this interface the user should:
  66 * # mount -t debugfs none /sys/kernel/debug
  67 *
  68 * The lpfc debugfs directory hierarchy is:
  69 * /sys/kernel/debug/lpfc/fnX/vportY
  70 * where X is the lpfc hba function unique_id
  71 * where Y is the vport VPI on that hba
  72 *
  73 * Debugging services available per vport:
  74 * discovery_trace
  75 * This is an ACSII readable file that contains a trace of the last
  76 * lpfc_debugfs_max_disc_trc events that happened on a specific vport.
  77 * See lpfc_debugfs.h for different categories of  discovery events.
  78 * To enable the discovery trace, the following module parameters must be set:
  79 * lpfc_debugfs_enable=1         Turns on lpfc debugfs filesystem support
  80 * lpfc_debugfs_max_disc_trc=X   Where X is the event trace depth for
  81 *                               EACH vport. X MUST also be a power of 2.
  82 * lpfc_debugfs_mask_disc_trc=Y  Where Y is an event mask as defined in
  83 *                               lpfc_debugfs.h .
  84 *
  85 * slow_ring_trace
  86 * This is an ACSII readable file that contains a trace of the last
  87 * lpfc_debugfs_max_slow_ring_trc events that happened on a specific HBA.
  88 * To enable the slow ring trace, the following module parameters must be set:
  89 * lpfc_debugfs_enable=1         Turns on lpfc debugfs filesystem support
  90 * lpfc_debugfs_max_slow_ring_trc=X   Where X is the event trace depth for
  91 *                               the HBA. X MUST also be a power of 2.
  92 */
  93static int lpfc_debugfs_enable = 1;
  94module_param(lpfc_debugfs_enable, int, S_IRUGO);
  95MODULE_PARM_DESC(lpfc_debugfs_enable, "Enable debugfs services");
  96
  97/* This MUST be a power of 2 */
  98static int lpfc_debugfs_max_disc_trc;
  99module_param(lpfc_debugfs_max_disc_trc, int, S_IRUGO);
 100MODULE_PARM_DESC(lpfc_debugfs_max_disc_trc,
 101	"Set debugfs discovery trace depth");
 102
 103/* This MUST be a power of 2 */
 104static int lpfc_debugfs_max_slow_ring_trc;
 105module_param(lpfc_debugfs_max_slow_ring_trc, int, S_IRUGO);
 106MODULE_PARM_DESC(lpfc_debugfs_max_slow_ring_trc,
 107	"Set debugfs slow ring trace depth");
 108
 109/* This MUST be a power of 2 */
 110static int lpfc_debugfs_max_nvmeio_trc;
 111module_param(lpfc_debugfs_max_nvmeio_trc, int, 0444);
 112MODULE_PARM_DESC(lpfc_debugfs_max_nvmeio_trc,
 113		 "Set debugfs NVME IO trace depth");
 114
 115static int lpfc_debugfs_mask_disc_trc;
 116module_param(lpfc_debugfs_mask_disc_trc, int, S_IRUGO);
 117MODULE_PARM_DESC(lpfc_debugfs_mask_disc_trc,
 118	"Set debugfs discovery trace mask");
 119
 120#include <linux/debugfs.h>
 121
 122static atomic_t lpfc_debugfs_seq_trc_cnt = ATOMIC_INIT(0);
 123static unsigned long lpfc_debugfs_start_time = 0L;
 124
 125/* iDiag */
 126static struct lpfc_idiag idiag;
 127
 128/**
 129 * lpfc_debugfs_disc_trc_data - Dump discovery logging to a buffer
 130 * @vport: The vport to gather the log info from.
 131 * @buf: The buffer to dump log into.
 132 * @size: The maximum amount of data to process.
 133 *
 134 * Description:
 135 * This routine gathers the lpfc discovery debugfs data from the @vport and
 136 * dumps it to @buf up to @size number of bytes. It will start at the next entry
 137 * in the log and process the log until the end of the buffer. Then it will
 138 * gather from the beginning of the log and process until the current entry.
 139 *
 140 * Notes:
 141 * Discovery logging will be disabled while while this routine dumps the log.
 142 *
 143 * Return Value:
 144 * This routine returns the amount of bytes that were dumped into @buf and will
 145 * not exceed @size.
 146 **/
 147static int
 148lpfc_debugfs_disc_trc_data(struct lpfc_vport *vport, char *buf, int size)
 149{
 150	int i, index, len, enable;
 151	uint32_t ms;
 152	struct lpfc_debugfs_trc *dtp;
 153	char *buffer;
 154
 155	buffer = kmalloc(LPFC_DEBUG_TRC_ENTRY_SIZE, GFP_KERNEL);
 156	if (!buffer)
 157		return 0;
 158
 159	enable = lpfc_debugfs_enable;
 160	lpfc_debugfs_enable = 0;
 161
 162	len = 0;
 163	index = (atomic_read(&vport->disc_trc_cnt) + 1) &
 164		(lpfc_debugfs_max_disc_trc - 1);
 165	for (i = index; i < lpfc_debugfs_max_disc_trc; i++) {
 166		dtp = vport->disc_trc + i;
 167		if (!dtp->fmt)
 168			continue;
 169		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
 170		snprintf(buffer,
 171			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
 172			dtp->seq_cnt, ms, dtp->fmt);
 173		len +=  scnprintf(buf+len, size-len, buffer,
 174			dtp->data1, dtp->data2, dtp->data3);
 175	}
 176	for (i = 0; i < index; i++) {
 177		dtp = vport->disc_trc + i;
 178		if (!dtp->fmt)
 179			continue;
 180		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
 181		snprintf(buffer,
 182			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
 183			dtp->seq_cnt, ms, dtp->fmt);
 184		len +=  scnprintf(buf+len, size-len, buffer,
 185			dtp->data1, dtp->data2, dtp->data3);
 186	}
 187
 188	lpfc_debugfs_enable = enable;
 189	kfree(buffer);
 190
 191	return len;
 192}
 193
 194/**
 195 * lpfc_debugfs_slow_ring_trc_data - Dump slow ring logging to a buffer
 196 * @phba: The HBA to gather the log info from.
 197 * @buf: The buffer to dump log into.
 198 * @size: The maximum amount of data to process.
 199 *
 200 * Description:
 201 * This routine gathers the lpfc slow ring debugfs data from the @phba and
 202 * dumps it to @buf up to @size number of bytes. It will start at the next entry
 203 * in the log and process the log until the end of the buffer. Then it will
 204 * gather from the beginning of the log and process until the current entry.
 205 *
 206 * Notes:
 207 * Slow ring logging will be disabled while while this routine dumps the log.
 208 *
 209 * Return Value:
 210 * This routine returns the amount of bytes that were dumped into @buf and will
 211 * not exceed @size.
 212 **/
 213static int
 214lpfc_debugfs_slow_ring_trc_data(struct lpfc_hba *phba, char *buf, int size)
 215{
 216	int i, index, len, enable;
 217	uint32_t ms;
 218	struct lpfc_debugfs_trc *dtp;
 219	char *buffer;
 220
 221	buffer = kmalloc(LPFC_DEBUG_TRC_ENTRY_SIZE, GFP_KERNEL);
 222	if (!buffer)
 223		return 0;
 224
 225	enable = lpfc_debugfs_enable;
 226	lpfc_debugfs_enable = 0;
 227
 228	len = 0;
 229	index = (atomic_read(&phba->slow_ring_trc_cnt) + 1) &
 230		(lpfc_debugfs_max_slow_ring_trc - 1);
 231	for (i = index; i < lpfc_debugfs_max_slow_ring_trc; i++) {
 232		dtp = phba->slow_ring_trc + i;
 233		if (!dtp->fmt)
 234			continue;
 235		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
 236		snprintf(buffer,
 237			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
 238			dtp->seq_cnt, ms, dtp->fmt);
 239		len +=  scnprintf(buf+len, size-len, buffer,
 240			dtp->data1, dtp->data2, dtp->data3);
 241	}
 242	for (i = 0; i < index; i++) {
 243		dtp = phba->slow_ring_trc + i;
 244		if (!dtp->fmt)
 245			continue;
 246		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
 247		snprintf(buffer,
 248			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
 249			dtp->seq_cnt, ms, dtp->fmt);
 250		len +=  scnprintf(buf+len, size-len, buffer,
 251			dtp->data1, dtp->data2, dtp->data3);
 252	}
 253
 254	lpfc_debugfs_enable = enable;
 255	kfree(buffer);
 256
 257	return len;
 258}
 259
 260static int lpfc_debugfs_last_hbq = -1;
 261
 262/**
 263 * lpfc_debugfs_hbqinfo_data - Dump host buffer queue info to a buffer
 264 * @phba: The HBA to gather host buffer info from.
 265 * @buf: The buffer to dump log into.
 266 * @size: The maximum amount of data to process.
 267 *
 268 * Description:
 269 * This routine dumps the host buffer queue info from the @phba to @buf up to
 270 * @size number of bytes. A header that describes the current hbq state will be
 271 * dumped to @buf first and then info on each hbq entry will be dumped to @buf
 272 * until @size bytes have been dumped or all the hbq info has been dumped.
 273 *
 274 * Notes:
 275 * This routine will rotate through each configured HBQ each time called.
 276 *
 277 * Return Value:
 278 * This routine returns the amount of bytes that were dumped into @buf and will
 279 * not exceed @size.
 280 **/
 281static int
 282lpfc_debugfs_hbqinfo_data(struct lpfc_hba *phba, char *buf, int size)
 283{
 284	int len = 0;
 285	int i, j, found, posted, low;
 286	uint32_t phys, raw_index, getidx;
 287	struct lpfc_hbq_init *hip;
 288	struct hbq_s *hbqs;
 289	struct lpfc_hbq_entry *hbqe;
 290	struct lpfc_dmabuf *d_buf;
 291	struct hbq_dmabuf *hbq_buf;
 292
 293	if (phba->sli_rev != 3)
 294		return 0;
 295
 296	spin_lock_irq(&phba->hbalock);
 297
 298	/* toggle between multiple hbqs, if any */
 299	i = lpfc_sli_hbq_count();
 300	if (i > 1) {
 301		 lpfc_debugfs_last_hbq++;
 302		 if (lpfc_debugfs_last_hbq >= i)
 303			lpfc_debugfs_last_hbq = 0;
 304	}
 305	else
 306		lpfc_debugfs_last_hbq = 0;
 307
 308	i = lpfc_debugfs_last_hbq;
 309
 310	len +=  scnprintf(buf+len, size-len, "HBQ %d Info\n", i);
 311
 312	hbqs =  &phba->hbqs[i];
 313	posted = 0;
 314	list_for_each_entry(d_buf, &hbqs->hbq_buffer_list, list)
 315		posted++;
 316
 317	hip =  lpfc_hbq_defs[i];
 318	len +=  scnprintf(buf+len, size-len,
 319		"idx:%d prof:%d rn:%d bufcnt:%d icnt:%d acnt:%d posted %d\n",
 320		hip->hbq_index, hip->profile, hip->rn,
 321		hip->buffer_count, hip->init_count, hip->add_count, posted);
 322
 323	raw_index = phba->hbq_get[i];
 324	getidx = le32_to_cpu(raw_index);
 325	len +=  scnprintf(buf+len, size-len,
 326		"entries:%d bufcnt:%d Put:%d nPut:%d localGet:%d hbaGet:%d\n",
 327		hbqs->entry_count, hbqs->buffer_count, hbqs->hbqPutIdx,
 328		hbqs->next_hbqPutIdx, hbqs->local_hbqGetIdx, getidx);
 329
 330	hbqe = (struct lpfc_hbq_entry *) phba->hbqs[i].hbq_virt;
 331	for (j=0; j<hbqs->entry_count; j++) {
 332		len +=  scnprintf(buf+len, size-len,
 333			"%03d: %08x %04x %05x ", j,
 334			le32_to_cpu(hbqe->bde.addrLow),
 335			le32_to_cpu(hbqe->bde.tus.w),
 336			le32_to_cpu(hbqe->buffer_tag));
 337		i = 0;
 338		found = 0;
 339
 340		/* First calculate if slot has an associated posted buffer */
 341		low = hbqs->hbqPutIdx - posted;
 342		if (low >= 0) {
 343			if ((j >= hbqs->hbqPutIdx) || (j < low)) {
 344				len +=  scnprintf(buf + len, size - len,
 345						"Unused\n");
 346				goto skipit;
 347			}
 348		}
 349		else {
 350			if ((j >= hbqs->hbqPutIdx) &&
 351				(j < (hbqs->entry_count+low))) {
 352				len +=  scnprintf(buf + len, size - len,
 353						"Unused\n");
 354				goto skipit;
 355			}
 356		}
 357
 358		/* Get the Buffer info for the posted buffer */
 359		list_for_each_entry(d_buf, &hbqs->hbq_buffer_list, list) {
 360			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
 361			phys = ((uint64_t)hbq_buf->dbuf.phys & 0xffffffff);
 362			if (phys == le32_to_cpu(hbqe->bde.addrLow)) {
 363				len +=  scnprintf(buf+len, size-len,
 364					"Buf%d: x%px %06x\n", i,
 365					hbq_buf->dbuf.virt, hbq_buf->tag);
 366				found = 1;
 367				break;
 368			}
 369			i++;
 370		}
 371		if (!found) {
 372			len +=  scnprintf(buf+len, size-len, "No DMAinfo?\n");
 373		}
 374skipit:
 375		hbqe++;
 376		if (len > LPFC_HBQINFO_SIZE - 54)
 377			break;
 378	}
 379	spin_unlock_irq(&phba->hbalock);
 380	return len;
 381}
 382
 383static int lpfc_debugfs_last_xripool;
 384
 385/**
 386 * lpfc_debugfs_common_xri_data - Dump Hardware Queue info to a buffer
 387 * @phba: The HBA to gather host buffer info from.
 388 * @buf: The buffer to dump log into.
 389 * @size: The maximum amount of data to process.
 390 *
 391 * Description:
 392 * This routine dumps the Hardware Queue info from the @phba to @buf up to
 393 * @size number of bytes. A header that describes the current hdwq state will be
 394 * dumped to @buf first and then info on each hdwq entry will be dumped to @buf
 395 * until @size bytes have been dumped or all the hdwq info has been dumped.
 396 *
 397 * Notes:
 398 * This routine will rotate through each configured Hardware Queue each
 399 * time called.
 400 *
 401 * Return Value:
 402 * This routine returns the amount of bytes that were dumped into @buf and will
 403 * not exceed @size.
 404 **/
 405static int
 406lpfc_debugfs_commonxripools_data(struct lpfc_hba *phba, char *buf, int size)
 407{
 408	struct lpfc_sli4_hdw_queue *qp;
 409	int len = 0;
 410	int i, out;
 411	unsigned long iflag;
 412
 413	for (i = 0; i < phba->cfg_hdw_queue; i++) {
 414		if (len > (LPFC_DUMP_MULTIXRIPOOL_SIZE - 80))
 415			break;
 416		qp = &phba->sli4_hba.hdwq[lpfc_debugfs_last_xripool];
 417
 418		len += scnprintf(buf + len, size - len, "HdwQ %d Info ", i);
 419		spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
 420		spin_lock(&qp->io_buf_list_get_lock);
 421		spin_lock(&qp->io_buf_list_put_lock);
 422		out = qp->total_io_bufs - (qp->get_io_bufs + qp->put_io_bufs +
 423			qp->abts_scsi_io_bufs + qp->abts_nvme_io_bufs);
 424		len += scnprintf(buf + len, size - len,
 425				 "tot:%d get:%d put:%d mt:%d "
 426				 "ABTS scsi:%d nvme:%d Out:%d\n",
 427			qp->total_io_bufs, qp->get_io_bufs, qp->put_io_bufs,
 428			qp->empty_io_bufs, qp->abts_scsi_io_bufs,
 429			qp->abts_nvme_io_bufs, out);
 430		spin_unlock(&qp->io_buf_list_put_lock);
 431		spin_unlock(&qp->io_buf_list_get_lock);
 432		spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
 433
 434		lpfc_debugfs_last_xripool++;
 435		if (lpfc_debugfs_last_xripool >= phba->cfg_hdw_queue)
 436			lpfc_debugfs_last_xripool = 0;
 437	}
 438
 439	return len;
 440}
 441
 442/**
 443 * lpfc_debugfs_multixripools_data - Display multi-XRI pools information
 444 * @phba: The HBA to gather host buffer info from.
 445 * @buf: The buffer to dump log into.
 446 * @size: The maximum amount of data to process.
 447 *
 448 * Description:
 449 * This routine displays current multi-XRI pools information including XRI
 450 * count in public, private and txcmplq. It also displays current high and
 451 * low watermark.
 452 *
 453 * Return Value:
 454 * This routine returns the amount of bytes that were dumped into @buf and will
 455 * not exceed @size.
 456 **/
 457static int
 458lpfc_debugfs_multixripools_data(struct lpfc_hba *phba, char *buf, int size)
 459{
 460	u32 i;
 461	u32 hwq_count;
 462	struct lpfc_sli4_hdw_queue *qp;
 463	struct lpfc_multixri_pool *multixri_pool;
 464	struct lpfc_pvt_pool *pvt_pool;
 465	struct lpfc_pbl_pool *pbl_pool;
 466	u32 txcmplq_cnt;
 467	char tmp[LPFC_DEBUG_OUT_LINE_SZ] = {0};
 468
 469	if (phba->sli_rev != LPFC_SLI_REV4)
 470		return 0;
 471
 472	if (!phba->sli4_hba.hdwq)
 473		return 0;
 474
 475	if (!phba->cfg_xri_rebalancing) {
 476		i = lpfc_debugfs_commonxripools_data(phba, buf, size);
 477		return i;
 478	}
 479
 480	/*
 481	 * Pbl: Current number of free XRIs in public pool
 482	 * Pvt: Current number of free XRIs in private pool
 483	 * Busy: Current number of outstanding XRIs
 484	 * HWM: Current high watermark
 485	 * pvt_empty: Incremented by 1 when IO submission fails (no xri)
 486	 * pbl_empty: Incremented by 1 when all pbl_pool are empty during
 487	 *            IO submission
 488	 */
 489	scnprintf(tmp, sizeof(tmp),
 490		  "HWQ:  Pbl  Pvt Busy  HWM |  pvt_empty  pbl_empty ");
 491	if (strlcat(buf, tmp, size) >= size)
 492		return strnlen(buf, size);
 493
 494#ifdef LPFC_MXP_STAT
 495	/*
 496	 * MAXH: Max high watermark seen so far
 497	 * above_lmt: Incremented by 1 if xri_owned > xri_limit during
 498	 *            IO submission
 499	 * below_lmt: Incremented by 1 if xri_owned <= xri_limit  during
 500	 *            IO submission
 501	 * locPbl_hit: Incremented by 1 if successfully get a batch of XRI from
 502	 *             local pbl_pool
 503	 * othPbl_hit: Incremented by 1 if successfully get a batch of XRI from
 504	 *             other pbl_pool
 505	 */
 506	scnprintf(tmp, sizeof(tmp),
 507		  "MAXH  above_lmt  below_lmt locPbl_hit othPbl_hit");
 508	if (strlcat(buf, tmp, size) >= size)
 509		return strnlen(buf, size);
 510
 511	/*
 512	 * sPbl: snapshot of Pbl 15 sec after stat gets cleared
 513	 * sPvt: snapshot of Pvt 15 sec after stat gets cleared
 514	 * sBusy: snapshot of Busy 15 sec after stat gets cleared
 515	 */
 516	scnprintf(tmp, sizeof(tmp),
 517		  " | sPbl sPvt sBusy");
 518	if (strlcat(buf, tmp, size) >= size)
 519		return strnlen(buf, size);
 520#endif
 521
 522	scnprintf(tmp, sizeof(tmp), "\n");
 523	if (strlcat(buf, tmp, size) >= size)
 524		return strnlen(buf, size);
 525
 526	hwq_count = phba->cfg_hdw_queue;
 527	for (i = 0; i < hwq_count; i++) {
 528		qp = &phba->sli4_hba.hdwq[i];
 529		multixri_pool = qp->p_multixri_pool;
 530		if (!multixri_pool)
 531			continue;
 532		pbl_pool = &multixri_pool->pbl_pool;
 533		pvt_pool = &multixri_pool->pvt_pool;
 534		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
 535
 536		scnprintf(tmp, sizeof(tmp),
 537			  "%03d: %4d %4d %4d %4d | %10d %10d ",
 538			  i, pbl_pool->count, pvt_pool->count,
 539			  txcmplq_cnt, pvt_pool->high_watermark,
 540			  qp->empty_io_bufs, multixri_pool->pbl_empty_count);
 541		if (strlcat(buf, tmp, size) >= size)
 542			break;
 543
 544#ifdef LPFC_MXP_STAT
 545		scnprintf(tmp, sizeof(tmp),
 546			  "%4d %10d %10d %10d %10d",
 547			  multixri_pool->stat_max_hwm,
 548			  multixri_pool->above_limit_count,
 549			  multixri_pool->below_limit_count,
 550			  multixri_pool->local_pbl_hit_count,
 551			  multixri_pool->other_pbl_hit_count);
 552		if (strlcat(buf, tmp, size) >= size)
 553			break;
 554
 555		scnprintf(tmp, sizeof(tmp),
 556			  " | %4d %4d %5d",
 557			  multixri_pool->stat_pbl_count,
 558			  multixri_pool->stat_pvt_count,
 559			  multixri_pool->stat_busy_count);
 560		if (strlcat(buf, tmp, size) >= size)
 561			break;
 562#endif
 563
 564		scnprintf(tmp, sizeof(tmp), "\n");
 565		if (strlcat(buf, tmp, size) >= size)
 566			break;
 567	}
 568	return strnlen(buf, size);
 569}
 570
 571
 572#ifdef LPFC_HDWQ_LOCK_STAT
 573static int lpfc_debugfs_last_lock;
 574
 575/**
 576 * lpfc_debugfs_lockstat_data - Dump Hardware Queue info to a buffer
 577 * @phba: The HBA to gather host buffer info from.
 578 * @buf: The buffer to dump log into.
 579 * @size: The maximum amount of data to process.
 580 *
 581 * Description:
 582 * This routine dumps the Hardware Queue info from the @phba to @buf up to
 583 * @size number of bytes. A header that describes the current hdwq state will be
 584 * dumped to @buf first and then info on each hdwq entry will be dumped to @buf
 585 * until @size bytes have been dumped or all the hdwq info has been dumped.
 586 *
 587 * Notes:
 588 * This routine will rotate through each configured Hardware Queue each
 589 * time called.
 590 *
 591 * Return Value:
 592 * This routine returns the amount of bytes that were dumped into @buf and will
 593 * not exceed @size.
 594 **/
 595static int
 596lpfc_debugfs_lockstat_data(struct lpfc_hba *phba, char *buf, int size)
 597{
 598	struct lpfc_sli4_hdw_queue *qp;
 599	int len = 0;
 600	int i;
 601
 602	if (phba->sli_rev != LPFC_SLI_REV4)
 603		return 0;
 604
 605	if (!phba->sli4_hba.hdwq)
 606		return 0;
 607
 608	for (i = 0; i < phba->cfg_hdw_queue; i++) {
 609		if (len > (LPFC_HDWQINFO_SIZE - 100))
 610			break;
 611		qp = &phba->sli4_hba.hdwq[lpfc_debugfs_last_lock];
 612
 613		len += scnprintf(buf + len, size - len, "HdwQ %03d Lock ", i);
 614		if (phba->cfg_xri_rebalancing) {
 615			len += scnprintf(buf + len, size - len,
 616					 "get_pvt:%d mv_pvt:%d "
 617					 "mv2pub:%d mv2pvt:%d "
 618					 "put_pvt:%d put_pub:%d wq:%d\n",
 619					 qp->lock_conflict.alloc_pvt_pool,
 620					 qp->lock_conflict.mv_from_pvt_pool,
 621					 qp->lock_conflict.mv_to_pub_pool,
 622					 qp->lock_conflict.mv_to_pvt_pool,
 623					 qp->lock_conflict.free_pvt_pool,
 624					 qp->lock_conflict.free_pub_pool,
 625					 qp->lock_conflict.wq_access);
 626		} else {
 627			len += scnprintf(buf + len, size - len,
 628					 "get:%d put:%d free:%d wq:%d\n",
 629					 qp->lock_conflict.alloc_xri_get,
 630					 qp->lock_conflict.alloc_xri_put,
 631					 qp->lock_conflict.free_xri,
 632					 qp->lock_conflict.wq_access);
 633		}
 634
 635		lpfc_debugfs_last_lock++;
 636		if (lpfc_debugfs_last_lock >= phba->cfg_hdw_queue)
 637			lpfc_debugfs_last_lock = 0;
 638	}
 639
 640	return len;
 641}
 642#endif
 643
 644static int lpfc_debugfs_last_hba_slim_off;
 645
 646/**
 647 * lpfc_debugfs_dumpHBASlim_data - Dump HBA SLIM info to a buffer
 648 * @phba: The HBA to gather SLIM info from.
 649 * @buf: The buffer to dump log into.
 650 * @size: The maximum amount of data to process.
 651 *
 652 * Description:
 653 * This routine dumps the current contents of HBA SLIM for the HBA associated
 654 * with @phba to @buf up to @size bytes of data. This is the raw HBA SLIM data.
 655 *
 656 * Notes:
 657 * This routine will only dump up to 1024 bytes of data each time called and
 658 * should be called multiple times to dump the entire HBA SLIM.
 659 *
 660 * Return Value:
 661 * This routine returns the amount of bytes that were dumped into @buf and will
 662 * not exceed @size.
 663 **/
 664static int
 665lpfc_debugfs_dumpHBASlim_data(struct lpfc_hba *phba, char *buf, int size)
 666{
 667	int len = 0;
 668	int i, off;
 669	uint32_t *ptr;
 670	char *buffer;
 671
 672	buffer = kmalloc(1024, GFP_KERNEL);
 673	if (!buffer)
 674		return 0;
 675
 676	off = 0;
 677	spin_lock_irq(&phba->hbalock);
 678
 679	len +=  scnprintf(buf+len, size-len, "HBA SLIM\n");
 680	lpfc_memcpy_from_slim(buffer,
 681		phba->MBslimaddr + lpfc_debugfs_last_hba_slim_off, 1024);
 682
 683	ptr = (uint32_t *)&buffer[0];
 684	off = lpfc_debugfs_last_hba_slim_off;
 685
 686	/* Set it up for the next time */
 687	lpfc_debugfs_last_hba_slim_off += 1024;
 688	if (lpfc_debugfs_last_hba_slim_off >= 4096)
 689		lpfc_debugfs_last_hba_slim_off = 0;
 690
 691	i = 1024;
 692	while (i > 0) {
 693		len +=  scnprintf(buf+len, size-len,
 694		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
 695		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
 696		*(ptr+5), *(ptr+6), *(ptr+7));
 697		ptr += 8;
 698		i -= (8 * sizeof(uint32_t));
 699		off += (8 * sizeof(uint32_t));
 700	}
 701
 702	spin_unlock_irq(&phba->hbalock);
 703	kfree(buffer);
 704
 705	return len;
 706}
 707
 708/**
 709 * lpfc_debugfs_dumpHostSlim_data - Dump host SLIM info to a buffer
 710 * @phba: The HBA to gather Host SLIM info from.
 711 * @buf: The buffer to dump log into.
 712 * @size: The maximum amount of data to process.
 713 *
 714 * Description:
 715 * This routine dumps the current contents of host SLIM for the host associated
 716 * with @phba to @buf up to @size bytes of data. The dump will contain the
 717 * Mailbox, PCB, Rings, and Registers that are located in host memory.
 718 *
 719 * Return Value:
 720 * This routine returns the amount of bytes that were dumped into @buf and will
 721 * not exceed @size.
 722 **/
 723static int
 724lpfc_debugfs_dumpHostSlim_data(struct lpfc_hba *phba, char *buf, int size)
 725{
 726	int len = 0;
 727	int i, off;
 728	uint32_t word0, word1, word2, word3;
 729	uint32_t *ptr;
 730	struct lpfc_pgp *pgpp;
 731	struct lpfc_sli *psli = &phba->sli;
 732	struct lpfc_sli_ring *pring;
 733
 734	off = 0;
 735	spin_lock_irq(&phba->hbalock);
 736
 737	len +=  scnprintf(buf+len, size-len, "SLIM Mailbox\n");
 738	ptr = (uint32_t *)phba->slim2p.virt;
 739	i = sizeof(MAILBOX_t);
 740	while (i > 0) {
 741		len +=  scnprintf(buf+len, size-len,
 742		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
 743		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
 744		*(ptr+5), *(ptr+6), *(ptr+7));
 745		ptr += 8;
 746		i -= (8 * sizeof(uint32_t));
 747		off += (8 * sizeof(uint32_t));
 748	}
 749
 750	len +=  scnprintf(buf+len, size-len, "SLIM PCB\n");
 751	ptr = (uint32_t *)phba->pcb;
 752	i = sizeof(PCB_t);
 753	while (i > 0) {
 754		len +=  scnprintf(buf+len, size-len,
 755		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
 756		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
 757		*(ptr+5), *(ptr+6), *(ptr+7));
 758		ptr += 8;
 759		i -= (8 * sizeof(uint32_t));
 760		off += (8 * sizeof(uint32_t));
 761	}
 762
 763	if (phba->sli_rev <= LPFC_SLI_REV3) {
 764		for (i = 0; i < 4; i++) {
 765			pgpp = &phba->port_gp[i];
 766			pring = &psli->sli3_ring[i];
 767			len +=  scnprintf(buf+len, size-len,
 768					 "Ring %d: CMD GetInx:%d "
 769					 "(Max:%d Next:%d "
 770					 "Local:%d flg:x%x)  "
 771					 "RSP PutInx:%d Max:%d\n",
 772					 i, pgpp->cmdGetInx,
 773					 pring->sli.sli3.numCiocb,
 774					 pring->sli.sli3.next_cmdidx,
 775					 pring->sli.sli3.local_getidx,
 776					 pring->flag, pgpp->rspPutInx,
 777					 pring->sli.sli3.numRiocb);
 778		}
 779
 780		word0 = readl(phba->HAregaddr);
 781		word1 = readl(phba->CAregaddr);
 782		word2 = readl(phba->HSregaddr);
 783		word3 = readl(phba->HCregaddr);
 784		len +=  scnprintf(buf+len, size-len, "HA:%08x CA:%08x HS:%08x "
 785				 "HC:%08x\n", word0, word1, word2, word3);
 786	}
 787	spin_unlock_irq(&phba->hbalock);
 788	return len;
 789}
 790
 791/**
 792 * lpfc_debugfs_nodelist_data - Dump target node list to a buffer
 793 * @vport: The vport to gather target node info from.
 794 * @buf: The buffer to dump log into.
 795 * @size: The maximum amount of data to process.
 796 *
 797 * Description:
 798 * This routine dumps the current target node list associated with @vport to
 799 * @buf up to @size bytes of data. Each node entry in the dump will contain a
 800 * node state, DID, WWPN, WWNN, RPI, flags, type, and other useful fields.
 801 *
 802 * Return Value:
 803 * This routine returns the amount of bytes that were dumped into @buf and will
 804 * not exceed @size.
 805 **/
 806static int
 807lpfc_debugfs_nodelist_data(struct lpfc_vport *vport, char *buf, int size)
 808{
 809	int len = 0;
 810	int i, iocnt, outio, cnt;
 811	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 812	struct lpfc_hba  *phba = vport->phba;
 813	struct lpfc_nodelist *ndlp;
 814	unsigned char *statep;
 815	struct nvme_fc_local_port *localport;
 816	struct nvme_fc_remote_port *nrport = NULL;
 817	struct lpfc_nvme_rport *rport;
 818
 819	cnt = (LPFC_NODELIST_SIZE / LPFC_NODELIST_ENTRY_SIZE);
 820	outio = 0;
 821
 822	len += scnprintf(buf+len, size-len, "\nFCP Nodelist Entries ...\n");
 823	spin_lock_irq(shost->host_lock);
 824	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
 825		iocnt = 0;
 826		if (!cnt) {
 827			len +=  scnprintf(buf+len, size-len,
 828				"Missing Nodelist Entries\n");
 829			break;
 830		}
 831		cnt--;
 832		switch (ndlp->nlp_state) {
 833		case NLP_STE_UNUSED_NODE:
 834			statep = "UNUSED";
 835			break;
 836		case NLP_STE_PLOGI_ISSUE:
 837			statep = "PLOGI ";
 838			break;
 839		case NLP_STE_ADISC_ISSUE:
 840			statep = "ADISC ";
 841			break;
 842		case NLP_STE_REG_LOGIN_ISSUE:
 843			statep = "REGLOG";
 844			break;
 845		case NLP_STE_PRLI_ISSUE:
 846			statep = "PRLI  ";
 847			break;
 848		case NLP_STE_LOGO_ISSUE:
 849			statep = "LOGO  ";
 850			break;
 851		case NLP_STE_UNMAPPED_NODE:
 852			statep = "UNMAP ";
 853			iocnt = 1;
 854			break;
 855		case NLP_STE_MAPPED_NODE:
 856			statep = "MAPPED";
 857			iocnt = 1;
 858			break;
 859		case NLP_STE_NPR_NODE:
 860			statep = "NPR   ";
 861			break;
 862		default:
 863			statep = "UNKNOWN";
 864		}
 865		len += scnprintf(buf+len, size-len, "%s DID:x%06x ",
 866				statep, ndlp->nlp_DID);
 867		len += scnprintf(buf+len, size-len,
 868				"WWPN x%llx ",
 869				wwn_to_u64(ndlp->nlp_portname.u.wwn));
 870		len += scnprintf(buf+len, size-len,
 871				"WWNN x%llx ",
 872				wwn_to_u64(ndlp->nlp_nodename.u.wwn));
 873		if (ndlp->nlp_flag & NLP_RPI_REGISTERED)
 874			len += scnprintf(buf+len, size-len, "RPI:%03d ",
 875					ndlp->nlp_rpi);
 876		else
 877			len += scnprintf(buf+len, size-len, "RPI:none ");
 878		len +=  scnprintf(buf+len, size-len, "flag:x%08x ",
 879			ndlp->nlp_flag);
 880		if (!ndlp->nlp_type)
 881			len += scnprintf(buf+len, size-len, "UNKNOWN_TYPE ");
 882		if (ndlp->nlp_type & NLP_FC_NODE)
 883			len += scnprintf(buf+len, size-len, "FC_NODE ");
 884		if (ndlp->nlp_type & NLP_FABRIC) {
 885			len += scnprintf(buf+len, size-len, "FABRIC ");
 886			iocnt = 0;
 887		}
 888		if (ndlp->nlp_type & NLP_FCP_TARGET)
 889			len += scnprintf(buf+len, size-len, "FCP_TGT sid:%d ",
 890				ndlp->nlp_sid);
 891		if (ndlp->nlp_type & NLP_FCP_INITIATOR)
 892			len += scnprintf(buf+len, size-len, "FCP_INITIATOR ");
 893		if (ndlp->nlp_type & NLP_NVME_TARGET)
 894			len += scnprintf(buf + len,
 895					size - len, "NVME_TGT sid:%d ",
 896					NLP_NO_SID);
 897		if (ndlp->nlp_type & NLP_NVME_INITIATOR)
 898			len += scnprintf(buf + len,
 899					size - len, "NVME_INITIATOR ");
 900		len += scnprintf(buf+len, size-len, "usgmap:%x ",
 901			ndlp->nlp_usg_map);
 902		len += scnprintf(buf+len, size-len, "refcnt:%x",
 903			kref_read(&ndlp->kref));
 904		if (iocnt) {
 905			i = atomic_read(&ndlp->cmd_pending);
 906			len += scnprintf(buf + len, size - len,
 907					" OutIO:x%x Qdepth x%x",
 908					i, ndlp->cmd_qdepth);
 909			outio += i;
 910		}
 911		len += scnprintf(buf + len, size - len, "defer:%x ",
 912			ndlp->nlp_defer_did);
 
 
 
 913		len +=  scnprintf(buf+len, size-len, "\n");
 914	}
 915	spin_unlock_irq(shost->host_lock);
 916
 917	len += scnprintf(buf + len, size - len,
 918			"\nOutstanding IO x%x\n",  outio);
 919
 920	if (phba->nvmet_support && phba->targetport && (vport == phba->pport)) {
 921		len += scnprintf(buf + len, size - len,
 922				"\nNVME Targetport Entry ...\n");
 923
 924		/* Port state is only one of two values for now. */
 925		if (phba->targetport->port_id)
 926			statep = "REGISTERED";
 927		else
 928			statep = "INIT";
 929		len += scnprintf(buf + len, size - len,
 930				"TGT WWNN x%llx WWPN x%llx State %s\n",
 931				wwn_to_u64(vport->fc_nodename.u.wwn),
 932				wwn_to_u64(vport->fc_portname.u.wwn),
 933				statep);
 934		len += scnprintf(buf + len, size - len,
 935				"    Targetport DID x%06x\n",
 936				phba->targetport->port_id);
 937		goto out_exit;
 938	}
 939
 940	len += scnprintf(buf + len, size - len,
 941				"\nNVME Lport/Rport Entries ...\n");
 942
 943	localport = vport->localport;
 944	if (!localport)
 945		goto out_exit;
 946
 947	spin_lock_irq(shost->host_lock);
 948
 949	/* Port state is only one of two values for now. */
 950	if (localport->port_id)
 951		statep = "ONLINE";
 952	else
 953		statep = "UNKNOWN ";
 954
 955	len += scnprintf(buf + len, size - len,
 956			"Lport DID x%06x PortState %s\n",
 957			localport->port_id, statep);
 958
 959	len += scnprintf(buf + len, size - len, "\tRport List:\n");
 960	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
 961		/* local short-hand pointer. */
 962		spin_lock(&phba->hbalock);
 963		rport = lpfc_ndlp_get_nrport(ndlp);
 964		if (rport)
 965			nrport = rport->remoteport;
 966		else
 967			nrport = NULL;
 968		spin_unlock(&phba->hbalock);
 969		if (!nrport)
 970			continue;
 971
 972		/* Port state is only one of two values for now. */
 973		switch (nrport->port_state) {
 974		case FC_OBJSTATE_ONLINE:
 975			statep = "ONLINE";
 976			break;
 977		case FC_OBJSTATE_UNKNOWN:
 978			statep = "UNKNOWN ";
 979			break;
 980		default:
 981			statep = "UNSUPPORTED";
 982			break;
 983		}
 984
 985		/* Tab in to show lport ownership. */
 986		len += scnprintf(buf + len, size - len,
 987				"\t%s Port ID:x%06x ",
 988				statep, nrport->port_id);
 989		len += scnprintf(buf + len, size - len, "WWPN x%llx ",
 990				nrport->port_name);
 991		len += scnprintf(buf + len, size - len, "WWNN x%llx ",
 992				nrport->node_name);
 993
 994		/* An NVME rport can have multiple roles. */
 995		if (nrport->port_role & FC_PORT_ROLE_NVME_INITIATOR)
 996			len +=  scnprintf(buf + len, size - len,
 997					 "INITIATOR ");
 998		if (nrport->port_role & FC_PORT_ROLE_NVME_TARGET)
 999			len +=  scnprintf(buf + len, size - len,
1000					 "TARGET ");
1001		if (nrport->port_role & FC_PORT_ROLE_NVME_DISCOVERY)
1002			len +=  scnprintf(buf + len, size - len,
1003					 "DISCSRVC ");
1004		if (nrport->port_role & ~(FC_PORT_ROLE_NVME_INITIATOR |
1005					  FC_PORT_ROLE_NVME_TARGET |
1006					  FC_PORT_ROLE_NVME_DISCOVERY))
1007			len +=  scnprintf(buf + len, size - len,
1008					 "UNKNOWN ROLE x%x",
1009					 nrport->port_role);
1010		/* Terminate the string. */
1011		len +=  scnprintf(buf + len, size - len, "\n");
1012	}
1013
1014	spin_unlock_irq(shost->host_lock);
1015 out_exit:
1016	return len;
1017}
1018
1019/**
1020 * lpfc_debugfs_nvmestat_data - Dump target node list to a buffer
1021 * @vport: The vport to gather target node info from.
1022 * @buf: The buffer to dump log into.
1023 * @size: The maximum amount of data to process.
1024 *
1025 * Description:
1026 * This routine dumps the NVME statistics associated with @vport
1027 *
1028 * Return Value:
1029 * This routine returns the amount of bytes that were dumped into @buf and will
1030 * not exceed @size.
1031 **/
1032static int
1033lpfc_debugfs_nvmestat_data(struct lpfc_vport *vport, char *buf, int size)
1034{
1035	struct lpfc_hba   *phba = vport->phba;
1036	struct lpfc_nvmet_tgtport *tgtp;
1037	struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
1038	struct nvme_fc_local_port *localport;
1039	struct lpfc_fc4_ctrl_stat *cstat;
1040	struct lpfc_nvme_lport *lport;
1041	uint64_t data1, data2, data3;
1042	uint64_t tot, totin, totout;
1043	int cnt, i;
1044	int len = 0;
1045
1046	if (phba->nvmet_support) {
1047		if (!phba->targetport)
1048			return len;
1049		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1050		len += scnprintf(buf + len, size - len,
1051				"\nNVME Targetport Statistics\n");
1052
1053		len += scnprintf(buf + len, size - len,
1054				"LS: Rcv %08x Drop %08x Abort %08x\n",
1055				atomic_read(&tgtp->rcv_ls_req_in),
1056				atomic_read(&tgtp->rcv_ls_req_drop),
1057				atomic_read(&tgtp->xmt_ls_abort));
1058		if (atomic_read(&tgtp->rcv_ls_req_in) !=
1059		    atomic_read(&tgtp->rcv_ls_req_out)) {
1060			len += scnprintf(buf + len, size - len,
1061					"Rcv LS: in %08x != out %08x\n",
1062					atomic_read(&tgtp->rcv_ls_req_in),
1063					atomic_read(&tgtp->rcv_ls_req_out));
1064		}
1065
1066		len += scnprintf(buf + len, size - len,
1067				"LS: Xmt %08x Drop %08x Cmpl %08x\n",
1068				atomic_read(&tgtp->xmt_ls_rsp),
1069				atomic_read(&tgtp->xmt_ls_drop),
1070				atomic_read(&tgtp->xmt_ls_rsp_cmpl));
1071
1072		len += scnprintf(buf + len, size - len,
1073				"LS: RSP Abort %08x xb %08x Err %08x\n",
1074				atomic_read(&tgtp->xmt_ls_rsp_aborted),
1075				atomic_read(&tgtp->xmt_ls_rsp_xb_set),
1076				atomic_read(&tgtp->xmt_ls_rsp_error));
1077
1078		len += scnprintf(buf + len, size - len,
1079				"FCP: Rcv %08x Defer %08x Release %08x "
1080				"Drop %08x\n",
1081				atomic_read(&tgtp->rcv_fcp_cmd_in),
1082				atomic_read(&tgtp->rcv_fcp_cmd_defer),
1083				atomic_read(&tgtp->xmt_fcp_release),
1084				atomic_read(&tgtp->rcv_fcp_cmd_drop));
1085
1086		if (atomic_read(&tgtp->rcv_fcp_cmd_in) !=
1087		    atomic_read(&tgtp->rcv_fcp_cmd_out)) {
1088			len += scnprintf(buf + len, size - len,
1089					"Rcv FCP: in %08x != out %08x\n",
1090					atomic_read(&tgtp->rcv_fcp_cmd_in),
1091					atomic_read(&tgtp->rcv_fcp_cmd_out));
1092		}
1093
1094		len += scnprintf(buf + len, size - len,
1095				"FCP Rsp: read %08x readrsp %08x "
1096				"write %08x rsp %08x\n",
1097				atomic_read(&tgtp->xmt_fcp_read),
1098				atomic_read(&tgtp->xmt_fcp_read_rsp),
1099				atomic_read(&tgtp->xmt_fcp_write),
1100				atomic_read(&tgtp->xmt_fcp_rsp));
1101
1102		len += scnprintf(buf + len, size - len,
1103				"FCP Rsp Cmpl: %08x err %08x drop %08x\n",
1104				atomic_read(&tgtp->xmt_fcp_rsp_cmpl),
1105				atomic_read(&tgtp->xmt_fcp_rsp_error),
1106				atomic_read(&tgtp->xmt_fcp_rsp_drop));
1107
1108		len += scnprintf(buf + len, size - len,
1109				"FCP Rsp Abort: %08x xb %08x xricqe  %08x\n",
1110				atomic_read(&tgtp->xmt_fcp_rsp_aborted),
1111				atomic_read(&tgtp->xmt_fcp_rsp_xb_set),
1112				atomic_read(&tgtp->xmt_fcp_xri_abort_cqe));
1113
1114		len += scnprintf(buf + len, size - len,
1115				"ABORT: Xmt %08x Cmpl %08x\n",
1116				atomic_read(&tgtp->xmt_fcp_abort),
1117				atomic_read(&tgtp->xmt_fcp_abort_cmpl));
1118
1119		len += scnprintf(buf + len, size - len,
1120				"ABORT: Sol %08x  Usol %08x Err %08x Cmpl %08x",
1121				atomic_read(&tgtp->xmt_abort_sol),
1122				atomic_read(&tgtp->xmt_abort_unsol),
1123				atomic_read(&tgtp->xmt_abort_rsp),
1124				atomic_read(&tgtp->xmt_abort_rsp_error));
1125
1126		len +=  scnprintf(buf + len, size - len, "\n");
1127
1128		cnt = 0;
1129		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1130		list_for_each_entry_safe(ctxp, next_ctxp,
1131				&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1132				list) {
1133			cnt++;
1134		}
1135		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1136		if (cnt) {
1137			len += scnprintf(buf + len, size - len,
1138					"ABORT: %d ctx entries\n", cnt);
1139			spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1140			list_for_each_entry_safe(ctxp, next_ctxp,
1141				    &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1142				    list) {
1143				if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ))
1144					break;
1145				len += scnprintf(buf + len, size - len,
1146						"Entry: oxid %x state %x "
1147						"flag %x\n",
1148						ctxp->oxid, ctxp->state,
1149						ctxp->flag);
1150			}
1151			spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1152		}
1153
1154		/* Calculate outstanding IOs */
1155		tot = atomic_read(&tgtp->rcv_fcp_cmd_drop);
1156		tot += atomic_read(&tgtp->xmt_fcp_release);
1157		tot = atomic_read(&tgtp->rcv_fcp_cmd_in) - tot;
1158
1159		len += scnprintf(buf + len, size - len,
1160				"IO_CTX: %08x  WAIT: cur %08x tot %08x\n"
1161				"CTX Outstanding %08llx\n",
1162				phba->sli4_hba.nvmet_xri_cnt,
1163				phba->sli4_hba.nvmet_io_wait_cnt,
1164				phba->sli4_hba.nvmet_io_wait_total,
1165				tot);
1166	} else {
1167		if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
1168			return len;
1169
1170		localport = vport->localport;
1171		if (!localport)
1172			return len;
1173		lport = (struct lpfc_nvme_lport *)localport->private;
1174		if (!lport)
1175			return len;
1176
1177		len += scnprintf(buf + len, size - len,
1178				"\nNVME HDWQ Statistics\n");
1179
1180		len += scnprintf(buf + len, size - len,
1181				"LS: Xmt %016x Cmpl %016x\n",
1182				atomic_read(&lport->fc4NvmeLsRequests),
1183				atomic_read(&lport->fc4NvmeLsCmpls));
1184
1185		totin = 0;
1186		totout = 0;
1187		for (i = 0; i < phba->cfg_hdw_queue; i++) {
1188			cstat = &phba->sli4_hba.hdwq[i].nvme_cstat;
1189			tot = cstat->io_cmpls;
1190			totin += tot;
1191			data1 = cstat->input_requests;
1192			data2 = cstat->output_requests;
1193			data3 = cstat->control_requests;
1194			totout += (data1 + data2 + data3);
1195
1196			/* Limit to 32, debugfs display buffer limitation */
1197			if (i >= 32)
1198				continue;
1199
1200			len += scnprintf(buf + len, PAGE_SIZE - len,
1201					"HDWQ (%d): Rd %016llx Wr %016llx "
1202					"IO %016llx ",
1203					i, data1, data2, data3);
1204			len += scnprintf(buf + len, PAGE_SIZE - len,
1205					"Cmpl %016llx OutIO %016llx\n",
1206					tot, ((data1 + data2 + data3) - tot));
1207		}
1208		len += scnprintf(buf + len, PAGE_SIZE - len,
1209				"Total FCP Cmpl %016llx Issue %016llx "
1210				"OutIO %016llx\n",
1211				totin, totout, totout - totin);
1212
1213		len += scnprintf(buf + len, size - len,
1214				"LS Xmt Err: Abrt %08x Err %08x  "
1215				"Cmpl Err: xb %08x Err %08x\n",
1216				atomic_read(&lport->xmt_ls_abort),
1217				atomic_read(&lport->xmt_ls_err),
1218				atomic_read(&lport->cmpl_ls_xb),
1219				atomic_read(&lport->cmpl_ls_err));
1220
1221		len += scnprintf(buf + len, size - len,
1222				"FCP Xmt Err: noxri %06x nondlp %06x "
1223				"qdepth %06x wqerr %06x err %06x Abrt %06x\n",
1224				atomic_read(&lport->xmt_fcp_noxri),
1225				atomic_read(&lport->xmt_fcp_bad_ndlp),
1226				atomic_read(&lport->xmt_fcp_qdepth),
1227				atomic_read(&lport->xmt_fcp_wqerr),
1228				atomic_read(&lport->xmt_fcp_err),
1229				atomic_read(&lport->xmt_fcp_abort));
1230
1231		len += scnprintf(buf + len, size - len,
1232				"FCP Cmpl Err: xb %08x Err %08x\n",
1233				atomic_read(&lport->cmpl_fcp_xb),
1234				atomic_read(&lport->cmpl_fcp_err));
1235
1236	}
1237
1238	return len;
1239}
1240
1241/**
1242 * lpfc_debugfs_scsistat_data - Dump target node list to a buffer
1243 * @vport: The vport to gather target node info from.
1244 * @buf: The buffer to dump log into.
1245 * @size: The maximum amount of data to process.
1246 *
1247 * Description:
1248 * This routine dumps the SCSI statistics associated with @vport
1249 *
1250 * Return Value:
1251 * This routine returns the amount of bytes that were dumped into @buf and will
1252 * not exceed @size.
1253 **/
1254static int
1255lpfc_debugfs_scsistat_data(struct lpfc_vport *vport, char *buf, int size)
1256{
1257	int len;
1258	struct lpfc_hba *phba = vport->phba;
1259	struct lpfc_fc4_ctrl_stat *cstat;
1260	u64 data1, data2, data3;
1261	u64 tot, totin, totout;
1262	int i;
1263	char tmp[LPFC_MAX_SCSI_INFO_TMP_LEN] = {0};
1264
1265	if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ||
1266	    (phba->sli_rev != LPFC_SLI_REV4))
1267		return 0;
1268
1269	scnprintf(buf, size, "SCSI HDWQ Statistics\n");
1270
1271	totin = 0;
1272	totout = 0;
1273	for (i = 0; i < phba->cfg_hdw_queue; i++) {
1274		cstat = &phba->sli4_hba.hdwq[i].scsi_cstat;
1275		tot = cstat->io_cmpls;
1276		totin += tot;
1277		data1 = cstat->input_requests;
1278		data2 = cstat->output_requests;
1279		data3 = cstat->control_requests;
1280		totout += (data1 + data2 + data3);
1281
1282		scnprintf(tmp, sizeof(tmp), "HDWQ (%d): Rd %016llx Wr %016llx "
1283			  "IO %016llx ", i, data1, data2, data3);
1284		if (strlcat(buf, tmp, size) >= size)
1285			goto buffer_done;
1286
1287		scnprintf(tmp, sizeof(tmp), "Cmpl %016llx OutIO %016llx\n",
1288			  tot, ((data1 + data2 + data3) - tot));
1289		if (strlcat(buf, tmp, size) >= size)
1290			goto buffer_done;
1291	}
1292	scnprintf(tmp, sizeof(tmp), "Total FCP Cmpl %016llx Issue %016llx "
1293		  "OutIO %016llx\n", totin, totout, totout - totin);
1294	strlcat(buf, tmp, size);
1295
1296buffer_done:
1297	len = strnlen(buf, size);
1298
1299	return len;
1300}
1301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1302/**
1303 * lpfc_debugfs_nvmektime_data - Dump target node list to a buffer
1304 * @vport: The vport to gather target node info from.
1305 * @buf: The buffer to dump log into.
1306 * @size: The maximum amount of data to process.
1307 *
1308 * Description:
1309 * This routine dumps the NVME statistics associated with @vport
1310 *
1311 * Return Value:
1312 * This routine returns the amount of bytes that were dumped into @buf and will
1313 * not exceed @size.
1314 **/
1315static int
1316lpfc_debugfs_nvmektime_data(struct lpfc_vport *vport, char *buf, int size)
1317{
1318	struct lpfc_hba   *phba = vport->phba;
1319	int len = 0;
1320
1321	if (phba->nvmet_support == 0) {
1322		/* NVME Initiator */
1323		len += scnprintf(buf + len, PAGE_SIZE - len,
1324				"ktime %s: Total Samples: %lld\n",
1325				(phba->ktime_on ?  "Enabled" : "Disabled"),
1326				phba->ktime_data_samples);
1327		if (phba->ktime_data_samples == 0)
1328			return len;
1329
1330		len += scnprintf(
1331			buf + len, PAGE_SIZE - len,
1332			"Segment 1: Last NVME Cmd cmpl "
1333			"done -to- Start of next NVME cnd (in driver)\n");
1334		len += scnprintf(
1335			buf + len, PAGE_SIZE - len,
1336			"avg:%08lld min:%08lld max %08lld\n",
1337			div_u64(phba->ktime_seg1_total,
1338				phba->ktime_data_samples),
1339			phba->ktime_seg1_min,
1340			phba->ktime_seg1_max);
1341		len += scnprintf(
1342			buf + len, PAGE_SIZE - len,
1343			"Segment 2: Driver start of NVME cmd "
1344			"-to- Firmware WQ doorbell\n");
1345		len += scnprintf(
1346			buf + len, PAGE_SIZE - len,
1347			"avg:%08lld min:%08lld max %08lld\n",
1348			div_u64(phba->ktime_seg2_total,
1349				phba->ktime_data_samples),
1350			phba->ktime_seg2_min,
1351			phba->ktime_seg2_max);
1352		len += scnprintf(
1353			buf + len, PAGE_SIZE - len,
1354			"Segment 3: Firmware WQ doorbell -to- "
1355			"MSI-X ISR cmpl\n");
1356		len += scnprintf(
1357			buf + len, PAGE_SIZE - len,
1358			"avg:%08lld min:%08lld max %08lld\n",
1359			div_u64(phba->ktime_seg3_total,
1360				phba->ktime_data_samples),
1361			phba->ktime_seg3_min,
1362			phba->ktime_seg3_max);
1363		len += scnprintf(
1364			buf + len, PAGE_SIZE - len,
1365			"Segment 4: MSI-X ISR cmpl -to- "
1366			"NVME cmpl done\n");
1367		len += scnprintf(
1368			buf + len, PAGE_SIZE - len,
1369			"avg:%08lld min:%08lld max %08lld\n",
1370			div_u64(phba->ktime_seg4_total,
1371				phba->ktime_data_samples),
1372			phba->ktime_seg4_min,
1373			phba->ktime_seg4_max);
1374		len += scnprintf(
1375			buf + len, PAGE_SIZE - len,
1376			"Total IO avg time: %08lld\n",
1377			div_u64(phba->ktime_seg1_total +
1378			phba->ktime_seg2_total  +
1379			phba->ktime_seg3_total +
1380			phba->ktime_seg4_total,
1381			phba->ktime_data_samples));
1382		return len;
1383	}
1384
1385	/* NVME Target */
1386	len += scnprintf(buf + len, PAGE_SIZE-len,
1387			"ktime %s: Total Samples: %lld %lld\n",
1388			(phba->ktime_on ? "Enabled" : "Disabled"),
1389			phba->ktime_data_samples,
1390			phba->ktime_status_samples);
1391	if (phba->ktime_data_samples == 0)
1392		return len;
1393
1394	len += scnprintf(buf + len, PAGE_SIZE-len,
1395			"Segment 1: MSI-X ISR Rcv cmd -to- "
1396			"cmd pass to NVME Layer\n");
1397	len += scnprintf(buf + len, PAGE_SIZE-len,
1398			"avg:%08lld min:%08lld max %08lld\n",
1399			div_u64(phba->ktime_seg1_total,
1400				phba->ktime_data_samples),
1401			phba->ktime_seg1_min,
1402			phba->ktime_seg1_max);
1403	len += scnprintf(buf + len, PAGE_SIZE-len,
1404			"Segment 2: cmd pass to NVME Layer- "
1405			"-to- Driver rcv cmd OP (action)\n");
1406	len += scnprintf(buf + len, PAGE_SIZE-len,
1407			"avg:%08lld min:%08lld max %08lld\n",
1408			div_u64(phba->ktime_seg2_total,
1409				phba->ktime_data_samples),
1410			phba->ktime_seg2_min,
1411			phba->ktime_seg2_max);
1412	len += scnprintf(buf + len, PAGE_SIZE-len,
1413			"Segment 3: Driver rcv cmd OP -to- "
1414			"Firmware WQ doorbell: cmd\n");
1415	len += scnprintf(buf + len, PAGE_SIZE-len,
1416			"avg:%08lld min:%08lld max %08lld\n",
1417			div_u64(phba->ktime_seg3_total,
1418				phba->ktime_data_samples),
1419			phba->ktime_seg3_min,
1420			phba->ktime_seg3_max);
1421	len += scnprintf(buf + len, PAGE_SIZE-len,
1422			"Segment 4: Firmware WQ doorbell: cmd "
1423			"-to- MSI-X ISR for cmd cmpl\n");
1424	len += scnprintf(buf + len, PAGE_SIZE-len,
1425			"avg:%08lld min:%08lld max %08lld\n",
1426			div_u64(phba->ktime_seg4_total,
1427				phba->ktime_data_samples),
1428			phba->ktime_seg4_min,
1429			phba->ktime_seg4_max);
1430	len += scnprintf(buf + len, PAGE_SIZE-len,
1431			"Segment 5: MSI-X ISR for cmd cmpl "
1432			"-to- NVME layer passed cmd done\n");
1433	len += scnprintf(buf + len, PAGE_SIZE-len,
1434			"avg:%08lld min:%08lld max %08lld\n",
1435			div_u64(phba->ktime_seg5_total,
1436				phba->ktime_data_samples),
1437			phba->ktime_seg5_min,
1438			phba->ktime_seg5_max);
1439
1440	if (phba->ktime_status_samples == 0) {
1441		len += scnprintf(buf + len, PAGE_SIZE-len,
1442				"Total: cmd received by MSI-X ISR "
1443				"-to- cmd completed on wire\n");
1444		len += scnprintf(buf + len, PAGE_SIZE-len,
1445				"avg:%08lld min:%08lld "
1446				"max %08lld\n",
1447				div_u64(phba->ktime_seg10_total,
1448					phba->ktime_data_samples),
1449				phba->ktime_seg10_min,
1450				phba->ktime_seg10_max);
1451		return len;
1452	}
1453
1454	len += scnprintf(buf + len, PAGE_SIZE-len,
1455			"Segment 6: NVME layer passed cmd done "
1456			"-to- Driver rcv rsp status OP\n");
1457	len += scnprintf(buf + len, PAGE_SIZE-len,
1458			"avg:%08lld min:%08lld max %08lld\n",
1459			div_u64(phba->ktime_seg6_total,
1460				phba->ktime_status_samples),
1461			phba->ktime_seg6_min,
1462			phba->ktime_seg6_max);
1463	len += scnprintf(buf + len, PAGE_SIZE-len,
1464			"Segment 7: Driver rcv rsp status OP "
1465			"-to- Firmware WQ doorbell: status\n");
1466	len += scnprintf(buf + len, PAGE_SIZE-len,
1467			"avg:%08lld min:%08lld max %08lld\n",
1468			div_u64(phba->ktime_seg7_total,
1469				phba->ktime_status_samples),
1470			phba->ktime_seg7_min,
1471			phba->ktime_seg7_max);
1472	len += scnprintf(buf + len, PAGE_SIZE-len,
1473			"Segment 8: Firmware WQ doorbell: status"
1474			" -to- MSI-X ISR for status cmpl\n");
1475	len += scnprintf(buf + len, PAGE_SIZE-len,
1476			"avg:%08lld min:%08lld max %08lld\n",
1477			div_u64(phba->ktime_seg8_total,
1478				phba->ktime_status_samples),
1479			phba->ktime_seg8_min,
1480			phba->ktime_seg8_max);
1481	len += scnprintf(buf + len, PAGE_SIZE-len,
1482			"Segment 9: MSI-X ISR for status cmpl  "
1483			"-to- NVME layer passed status done\n");
1484	len += scnprintf(buf + len, PAGE_SIZE-len,
1485			"avg:%08lld min:%08lld max %08lld\n",
1486			div_u64(phba->ktime_seg9_total,
1487				phba->ktime_status_samples),
1488			phba->ktime_seg9_min,
1489			phba->ktime_seg9_max);
1490	len += scnprintf(buf + len, PAGE_SIZE-len,
1491			"Total: cmd received by MSI-X ISR -to- "
1492			"cmd completed on wire\n");
1493	len += scnprintf(buf + len, PAGE_SIZE-len,
1494			"avg:%08lld min:%08lld max %08lld\n",
1495			div_u64(phba->ktime_seg10_total,
1496				phba->ktime_status_samples),
1497			phba->ktime_seg10_min,
1498			phba->ktime_seg10_max);
1499	return len;
1500}
1501
1502/**
1503 * lpfc_debugfs_nvmeio_trc_data - Dump NVME IO trace list to a buffer
1504 * @phba: The phba to gather target node info from.
1505 * @buf: The buffer to dump log into.
1506 * @size: The maximum amount of data to process.
1507 *
1508 * Description:
1509 * This routine dumps the NVME IO trace associated with @phba
1510 *
1511 * Return Value:
1512 * This routine returns the amount of bytes that were dumped into @buf and will
1513 * not exceed @size.
1514 **/
1515static int
1516lpfc_debugfs_nvmeio_trc_data(struct lpfc_hba *phba, char *buf, int size)
1517{
1518	struct lpfc_debugfs_nvmeio_trc *dtp;
1519	int i, state, index, skip;
1520	int len = 0;
1521
1522	state = phba->nvmeio_trc_on;
1523
1524	index = (atomic_read(&phba->nvmeio_trc_cnt) + 1) &
1525		(phba->nvmeio_trc_size - 1);
1526	skip = phba->nvmeio_trc_output_idx;
1527
1528	len += scnprintf(buf + len, size - len,
1529			"%s IO Trace %s: next_idx %d skip %d size %d\n",
1530			(phba->nvmet_support ? "NVME" : "NVMET"),
1531			(state ? "Enabled" : "Disabled"),
1532			index, skip, phba->nvmeio_trc_size);
1533
1534	if (!phba->nvmeio_trc || state)
1535		return len;
1536
1537	/* trace MUST bhe off to continue */
1538
1539	for (i = index; i < phba->nvmeio_trc_size; i++) {
1540		if (skip) {
1541			skip--;
1542			continue;
1543		}
1544		dtp = phba->nvmeio_trc + i;
1545		phba->nvmeio_trc_output_idx++;
1546
1547		if (!dtp->fmt)
1548			continue;
1549
1550		len +=  scnprintf(buf + len, size - len, dtp->fmt,
1551			dtp->data1, dtp->data2, dtp->data3);
1552
1553		if (phba->nvmeio_trc_output_idx >= phba->nvmeio_trc_size) {
1554			phba->nvmeio_trc_output_idx = 0;
1555			len += scnprintf(buf + len, size - len,
1556					"Trace Complete\n");
1557			goto out;
1558		}
1559
1560		if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ)) {
1561			len += scnprintf(buf + len, size - len,
1562					"Trace Continue (%d of %d)\n",
1563					phba->nvmeio_trc_output_idx,
1564					phba->nvmeio_trc_size);
1565			goto out;
1566		}
1567	}
1568	for (i = 0; i < index; i++) {
1569		if (skip) {
1570			skip--;
1571			continue;
1572		}
1573		dtp = phba->nvmeio_trc + i;
1574		phba->nvmeio_trc_output_idx++;
1575
1576		if (!dtp->fmt)
1577			continue;
1578
1579		len +=  scnprintf(buf + len, size - len, dtp->fmt,
1580			dtp->data1, dtp->data2, dtp->data3);
1581
1582		if (phba->nvmeio_trc_output_idx >= phba->nvmeio_trc_size) {
1583			phba->nvmeio_trc_output_idx = 0;
1584			len += scnprintf(buf + len, size - len,
1585					"Trace Complete\n");
1586			goto out;
1587		}
1588
1589		if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ)) {
1590			len += scnprintf(buf + len, size - len,
1591					"Trace Continue (%d of %d)\n",
1592					phba->nvmeio_trc_output_idx,
1593					phba->nvmeio_trc_size);
1594			goto out;
1595		}
1596	}
1597
1598	len += scnprintf(buf + len, size - len,
1599			"Trace Done\n");
1600out:
1601	return len;
1602}
1603
1604/**
1605 * lpfc_debugfs_cpucheck_data - Dump target node list to a buffer
1606 * @vport: The vport to gather target node info from.
1607 * @buf: The buffer to dump log into.
1608 * @size: The maximum amount of data to process.
1609 *
1610 * Description:
1611 * This routine dumps the NVME statistics associated with @vport
1612 *
1613 * Return Value:
1614 * This routine returns the amount of bytes that were dumped into @buf and will
1615 * not exceed @size.
1616 **/
1617static int
1618lpfc_debugfs_cpucheck_data(struct lpfc_vport *vport, char *buf, int size)
1619{
1620	struct lpfc_hba   *phba = vport->phba;
1621	struct lpfc_sli4_hdw_queue *qp;
1622	int i, j, max_cnt;
1623	int len = 0;
1624	uint32_t tot_xmt;
1625	uint32_t tot_rcv;
1626	uint32_t tot_cmpl;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1627
1628	len += scnprintf(buf + len, PAGE_SIZE - len,
1629			"CPUcheck %s ",
1630			(phba->cpucheck_on & LPFC_CHECK_NVME_IO ?
1631				"Enabled" : "Disabled"));
1632	if (phba->nvmet_support) {
1633		len += scnprintf(buf + len, PAGE_SIZE - len,
1634				"%s\n",
1635				(phba->cpucheck_on & LPFC_CHECK_NVMET_RCV ?
1636					"Rcv Enabled\n" : "Rcv Disabled\n"));
1637	} else {
1638		len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
1639	}
1640	max_cnt = size - LPFC_DEBUG_OUT_LINE_SZ;
1641
1642	for (i = 0; i < phba->cfg_hdw_queue; i++) {
1643		qp = &phba->sli4_hba.hdwq[i];
1644
1645		tot_rcv = 0;
1646		tot_xmt = 0;
1647		tot_cmpl = 0;
1648		for (j = 0; j < LPFC_CHECK_CPU_CNT; j++) {
1649			tot_xmt += qp->cpucheck_xmt_io[j];
1650			tot_cmpl += qp->cpucheck_cmpl_io[j];
1651			if (phba->nvmet_support)
1652				tot_rcv += qp->cpucheck_rcv_io[j];
1653		}
1654
1655		/* Only display Hardware Qs with something */
1656		if (!tot_xmt && !tot_cmpl && !tot_rcv)
1657			continue;
 
 
 
1658
1659		len += scnprintf(buf + len, PAGE_SIZE - len,
1660				"HDWQ %03d: ", i);
1661		for (j = 0; j < LPFC_CHECK_CPU_CNT; j++) {
1662			/* Only display non-zero counters */
1663			if (!qp->cpucheck_xmt_io[j] &&
1664			    !qp->cpucheck_cmpl_io[j] &&
1665			    !qp->cpucheck_rcv_io[j])
1666				continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1667			if (phba->nvmet_support) {
1668				len += scnprintf(buf + len, PAGE_SIZE - len,
1669						"CPU %03d: %x/%x/%x ", j,
1670						qp->cpucheck_rcv_io[j],
1671						qp->cpucheck_xmt_io[j],
1672						qp->cpucheck_cmpl_io[j]);
 
1673			} else {
1674				len += scnprintf(buf + len, PAGE_SIZE - len,
1675						"CPU %03d: %x/%x ", j,
1676						qp->cpucheck_xmt_io[j],
1677						qp->cpucheck_cmpl_io[j]);
 
1678			}
1679		}
1680		len += scnprintf(buf + len, PAGE_SIZE - len,
1681				"Total: %x\n", tot_xmt);
1682		if (len >= max_cnt) {
1683			len += scnprintf(buf + len, PAGE_SIZE - len,
1684					"Truncated ...\n");
1685			return len;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1686		}
1687	}
 
 
 
1688	return len;
1689}
1690
1691#endif
1692
1693/**
1694 * lpfc_debugfs_disc_trc - Store discovery trace log
1695 * @vport: The vport to associate this trace string with for retrieval.
1696 * @mask: Log entry classification.
1697 * @fmt: Format string to be displayed when dumping the log.
1698 * @data1: 1st data parameter to be applied to @fmt.
1699 * @data2: 2nd data parameter to be applied to @fmt.
1700 * @data3: 3rd data parameter to be applied to @fmt.
1701 *
1702 * Description:
1703 * This routine is used by the driver code to add a debugfs log entry to the
1704 * discovery trace buffer associated with @vport. Only entries with a @mask that
1705 * match the current debugfs discovery mask will be saved. Entries that do not
1706 * match will be thrown away. @fmt, @data1, @data2, and @data3 are used like
1707 * printf when displaying the log.
1708 **/
1709inline void
1710lpfc_debugfs_disc_trc(struct lpfc_vport *vport, int mask, char *fmt,
1711	uint32_t data1, uint32_t data2, uint32_t data3)
1712{
1713#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1714	struct lpfc_debugfs_trc *dtp;
1715	int index;
1716
1717	if (!(lpfc_debugfs_mask_disc_trc & mask))
1718		return;
1719
1720	if (!lpfc_debugfs_enable || !lpfc_debugfs_max_disc_trc ||
1721		!vport || !vport->disc_trc)
1722		return;
1723
1724	index = atomic_inc_return(&vport->disc_trc_cnt) &
1725		(lpfc_debugfs_max_disc_trc - 1);
1726	dtp = vport->disc_trc + index;
1727	dtp->fmt = fmt;
1728	dtp->data1 = data1;
1729	dtp->data2 = data2;
1730	dtp->data3 = data3;
1731	dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_seq_trc_cnt);
1732	dtp->jif = jiffies;
1733#endif
1734	return;
1735}
1736
1737/**
1738 * lpfc_debugfs_slow_ring_trc - Store slow ring trace log
1739 * @phba: The phba to associate this trace string with for retrieval.
1740 * @fmt: Format string to be displayed when dumping the log.
1741 * @data1: 1st data parameter to be applied to @fmt.
1742 * @data2: 2nd data parameter to be applied to @fmt.
1743 * @data3: 3rd data parameter to be applied to @fmt.
1744 *
1745 * Description:
1746 * This routine is used by the driver code to add a debugfs log entry to the
1747 * discovery trace buffer associated with @vport. @fmt, @data1, @data2, and
1748 * @data3 are used like printf when displaying the log.
1749 **/
1750inline void
1751lpfc_debugfs_slow_ring_trc(struct lpfc_hba *phba, char *fmt,
1752	uint32_t data1, uint32_t data2, uint32_t data3)
1753{
1754#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1755	struct lpfc_debugfs_trc *dtp;
1756	int index;
1757
1758	if (!lpfc_debugfs_enable || !lpfc_debugfs_max_slow_ring_trc ||
1759		!phba || !phba->slow_ring_trc)
1760		return;
1761
1762	index = atomic_inc_return(&phba->slow_ring_trc_cnt) &
1763		(lpfc_debugfs_max_slow_ring_trc - 1);
1764	dtp = phba->slow_ring_trc + index;
1765	dtp->fmt = fmt;
1766	dtp->data1 = data1;
1767	dtp->data2 = data2;
1768	dtp->data3 = data3;
1769	dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_seq_trc_cnt);
1770	dtp->jif = jiffies;
1771#endif
1772	return;
1773}
1774
1775/**
1776 * lpfc_debugfs_nvme_trc - Store NVME/NVMET trace log
1777 * @phba: The phba to associate this trace string with for retrieval.
1778 * @fmt: Format string to be displayed when dumping the log.
1779 * @data1: 1st data parameter to be applied to @fmt.
1780 * @data2: 2nd data parameter to be applied to @fmt.
1781 * @data3: 3rd data parameter to be applied to @fmt.
1782 *
1783 * Description:
1784 * This routine is used by the driver code to add a debugfs log entry to the
1785 * nvme trace buffer associated with @phba. @fmt, @data1, @data2, and
1786 * @data3 are used like printf when displaying the log.
1787 **/
1788inline void
1789lpfc_debugfs_nvme_trc(struct lpfc_hba *phba, char *fmt,
1790		      uint16_t data1, uint16_t data2, uint32_t data3)
1791{
1792#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1793	struct lpfc_debugfs_nvmeio_trc *dtp;
1794	int index;
1795
1796	if (!phba->nvmeio_trc_on || !phba->nvmeio_trc)
1797		return;
1798
1799	index = atomic_inc_return(&phba->nvmeio_trc_cnt) &
1800		(phba->nvmeio_trc_size - 1);
1801	dtp = phba->nvmeio_trc + index;
1802	dtp->fmt = fmt;
1803	dtp->data1 = data1;
1804	dtp->data2 = data2;
1805	dtp->data3 = data3;
1806#endif
1807}
1808
1809#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1810/**
1811 * lpfc_debugfs_disc_trc_open - Open the discovery trace log
1812 * @inode: The inode pointer that contains a vport pointer.
1813 * @file: The file pointer to attach the log output.
1814 *
1815 * Description:
1816 * This routine is the entry point for the debugfs open file operation. It gets
1817 * the vport from the i_private field in @inode, allocates the necessary buffer
1818 * for the log, fills the buffer from the in-memory log for this vport, and then
1819 * returns a pointer to that log in the private_data field in @file.
1820 *
1821 * Returns:
1822 * This function returns zero if successful. On error it will return a negative
1823 * error value.
1824 **/
1825static int
1826lpfc_debugfs_disc_trc_open(struct inode *inode, struct file *file)
1827{
1828	struct lpfc_vport *vport = inode->i_private;
1829	struct lpfc_debug *debug;
1830	int size;
1831	int rc = -ENOMEM;
1832
1833	if (!lpfc_debugfs_max_disc_trc) {
1834		rc = -ENOSPC;
1835		goto out;
1836	}
1837
1838	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1839	if (!debug)
1840		goto out;
1841
1842	/* Round to page boundary */
1843	size =  (lpfc_debugfs_max_disc_trc * LPFC_DEBUG_TRC_ENTRY_SIZE);
1844	size = PAGE_ALIGN(size);
1845
1846	debug->buffer = kmalloc(size, GFP_KERNEL);
1847	if (!debug->buffer) {
1848		kfree(debug);
1849		goto out;
1850	}
1851
1852	debug->len = lpfc_debugfs_disc_trc_data(vport, debug->buffer, size);
1853	file->private_data = debug;
1854
1855	rc = 0;
1856out:
1857	return rc;
1858}
1859
1860/**
1861 * lpfc_debugfs_slow_ring_trc_open - Open the Slow Ring trace log
1862 * @inode: The inode pointer that contains a vport pointer.
1863 * @file: The file pointer to attach the log output.
1864 *
1865 * Description:
1866 * This routine is the entry point for the debugfs open file operation. It gets
1867 * the vport from the i_private field in @inode, allocates the necessary buffer
1868 * for the log, fills the buffer from the in-memory log for this vport, and then
1869 * returns a pointer to that log in the private_data field in @file.
1870 *
1871 * Returns:
1872 * This function returns zero if successful. On error it will return a negative
1873 * error value.
1874 **/
1875static int
1876lpfc_debugfs_slow_ring_trc_open(struct inode *inode, struct file *file)
1877{
1878	struct lpfc_hba *phba = inode->i_private;
1879	struct lpfc_debug *debug;
1880	int size;
1881	int rc = -ENOMEM;
1882
1883	if (!lpfc_debugfs_max_slow_ring_trc) {
1884		rc = -ENOSPC;
1885		goto out;
1886	}
1887
1888	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1889	if (!debug)
1890		goto out;
1891
1892	/* Round to page boundary */
1893	size =  (lpfc_debugfs_max_slow_ring_trc * LPFC_DEBUG_TRC_ENTRY_SIZE);
1894	size = PAGE_ALIGN(size);
1895
1896	debug->buffer = kmalloc(size, GFP_KERNEL);
1897	if (!debug->buffer) {
1898		kfree(debug);
1899		goto out;
1900	}
1901
1902	debug->len = lpfc_debugfs_slow_ring_trc_data(phba, debug->buffer, size);
1903	file->private_data = debug;
1904
1905	rc = 0;
1906out:
1907	return rc;
1908}
1909
1910/**
1911 * lpfc_debugfs_hbqinfo_open - Open the hbqinfo debugfs buffer
1912 * @inode: The inode pointer that contains a vport pointer.
1913 * @file: The file pointer to attach the log output.
1914 *
1915 * Description:
1916 * This routine is the entry point for the debugfs open file operation. It gets
1917 * the vport from the i_private field in @inode, allocates the necessary buffer
1918 * for the log, fills the buffer from the in-memory log for this vport, and then
1919 * returns a pointer to that log in the private_data field in @file.
1920 *
1921 * Returns:
1922 * This function returns zero if successful. On error it will return a negative
1923 * error value.
1924 **/
1925static int
1926lpfc_debugfs_hbqinfo_open(struct inode *inode, struct file *file)
1927{
1928	struct lpfc_hba *phba = inode->i_private;
1929	struct lpfc_debug *debug;
1930	int rc = -ENOMEM;
1931
1932	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1933	if (!debug)
1934		goto out;
1935
1936	/* Round to page boundary */
1937	debug->buffer = kmalloc(LPFC_HBQINFO_SIZE, GFP_KERNEL);
1938	if (!debug->buffer) {
1939		kfree(debug);
1940		goto out;
1941	}
1942
1943	debug->len = lpfc_debugfs_hbqinfo_data(phba, debug->buffer,
1944		LPFC_HBQINFO_SIZE);
1945	file->private_data = debug;
1946
1947	rc = 0;
1948out:
1949	return rc;
1950}
1951
1952/**
1953 * lpfc_debugfs_multixripools_open - Open the multixripool debugfs buffer
1954 * @inode: The inode pointer that contains a hba pointer.
1955 * @file: The file pointer to attach the log output.
1956 *
1957 * Description:
1958 * This routine is the entry point for the debugfs open file operation. It gets
1959 * the hba from the i_private field in @inode, allocates the necessary buffer
1960 * for the log, fills the buffer from the in-memory log for this hba, and then
1961 * returns a pointer to that log in the private_data field in @file.
1962 *
1963 * Returns:
1964 * This function returns zero if successful. On error it will return a negative
1965 * error value.
1966 **/
1967static int
1968lpfc_debugfs_multixripools_open(struct inode *inode, struct file *file)
1969{
1970	struct lpfc_hba *phba = inode->i_private;
1971	struct lpfc_debug *debug;
1972	int rc = -ENOMEM;
1973
1974	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1975	if (!debug)
1976		goto out;
1977
1978	/* Round to page boundary */
1979	debug->buffer = kzalloc(LPFC_DUMP_MULTIXRIPOOL_SIZE, GFP_KERNEL);
1980	if (!debug->buffer) {
1981		kfree(debug);
1982		goto out;
1983	}
1984
1985	debug->len = lpfc_debugfs_multixripools_data(
1986		phba, debug->buffer, LPFC_DUMP_MULTIXRIPOOL_SIZE);
1987
1988	debug->i_private = inode->i_private;
1989	file->private_data = debug;
1990
1991	rc = 0;
1992out:
1993	return rc;
1994}
1995
1996#ifdef LPFC_HDWQ_LOCK_STAT
1997/**
1998 * lpfc_debugfs_lockstat_open - Open the lockstat debugfs buffer
1999 * @inode: The inode pointer that contains a vport pointer.
2000 * @file: The file pointer to attach the log output.
2001 *
2002 * Description:
2003 * This routine is the entry point for the debugfs open file operation. It gets
2004 * the vport from the i_private field in @inode, allocates the necessary buffer
2005 * for the log, fills the buffer from the in-memory log for this vport, and then
2006 * returns a pointer to that log in the private_data field in @file.
2007 *
2008 * Returns:
2009 * This function returns zero if successful. On error it will return a negative
2010 * error value.
2011 **/
2012static int
2013lpfc_debugfs_lockstat_open(struct inode *inode, struct file *file)
2014{
2015	struct lpfc_hba *phba = inode->i_private;
2016	struct lpfc_debug *debug;
2017	int rc = -ENOMEM;
2018
2019	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2020	if (!debug)
2021		goto out;
2022
2023	/* Round to page boundary */
2024	debug->buffer = kmalloc(LPFC_HDWQINFO_SIZE, GFP_KERNEL);
2025	if (!debug->buffer) {
2026		kfree(debug);
2027		goto out;
2028	}
2029
2030	debug->len = lpfc_debugfs_lockstat_data(phba, debug->buffer,
2031		LPFC_HBQINFO_SIZE);
2032	file->private_data = debug;
2033
2034	rc = 0;
2035out:
2036	return rc;
2037}
2038
2039static ssize_t
2040lpfc_debugfs_lockstat_write(struct file *file, const char __user *buf,
2041			    size_t nbytes, loff_t *ppos)
2042{
2043	struct lpfc_debug *debug = file->private_data;
2044	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2045	struct lpfc_sli4_hdw_queue *qp;
2046	char mybuf[64];
2047	char *pbuf;
2048	int i;
 
2049
2050	/* Protect copy from user */
2051	if (!access_ok(buf, nbytes))
2052		return -EFAULT;
2053
2054	memset(mybuf, 0, sizeof(mybuf));
2055
2056	if (copy_from_user(mybuf, buf, nbytes))
2057		return -EFAULT;
2058	pbuf = &mybuf[0];
2059
2060	if ((strncmp(pbuf, "reset", strlen("reset")) == 0) ||
2061	    (strncmp(pbuf, "zero", strlen("zero")) == 0)) {
2062		for (i = 0; i < phba->cfg_hdw_queue; i++) {
2063			qp = &phba->sli4_hba.hdwq[i];
2064			qp->lock_conflict.alloc_xri_get = 0;
2065			qp->lock_conflict.alloc_xri_put = 0;
2066			qp->lock_conflict.free_xri = 0;
2067			qp->lock_conflict.wq_access = 0;
2068			qp->lock_conflict.alloc_pvt_pool = 0;
2069			qp->lock_conflict.mv_from_pvt_pool = 0;
2070			qp->lock_conflict.mv_to_pub_pool = 0;
2071			qp->lock_conflict.mv_to_pvt_pool = 0;
2072			qp->lock_conflict.free_pvt_pool = 0;
2073			qp->lock_conflict.free_pub_pool = 0;
2074			qp->lock_conflict.wq_access = 0;
2075		}
2076	}
2077	return nbytes;
2078}
2079#endif
2080
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2081/**
2082 * lpfc_debugfs_dumpHBASlim_open - Open the Dump HBA SLIM debugfs buffer
2083 * @inode: The inode pointer that contains a vport pointer.
2084 * @file: The file pointer to attach the log output.
2085 *
2086 * Description:
2087 * This routine is the entry point for the debugfs open file operation. It gets
2088 * the vport from the i_private field in @inode, allocates the necessary buffer
2089 * for the log, fills the buffer from the in-memory log for this vport, and then
2090 * returns a pointer to that log in the private_data field in @file.
2091 *
2092 * Returns:
2093 * This function returns zero if successful. On error it will return a negative
2094 * error value.
2095 **/
2096static int
2097lpfc_debugfs_dumpHBASlim_open(struct inode *inode, struct file *file)
2098{
2099	struct lpfc_hba *phba = inode->i_private;
2100	struct lpfc_debug *debug;
2101	int rc = -ENOMEM;
2102
2103	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2104	if (!debug)
2105		goto out;
2106
2107	/* Round to page boundary */
2108	debug->buffer = kmalloc(LPFC_DUMPHBASLIM_SIZE, GFP_KERNEL);
2109	if (!debug->buffer) {
2110		kfree(debug);
2111		goto out;
2112	}
2113
2114	debug->len = lpfc_debugfs_dumpHBASlim_data(phba, debug->buffer,
2115		LPFC_DUMPHBASLIM_SIZE);
2116	file->private_data = debug;
2117
2118	rc = 0;
2119out:
2120	return rc;
2121}
2122
2123/**
2124 * lpfc_debugfs_dumpHostSlim_open - Open the Dump Host SLIM debugfs buffer
2125 * @inode: The inode pointer that contains a vport pointer.
2126 * @file: The file pointer to attach the log output.
2127 *
2128 * Description:
2129 * This routine is the entry point for the debugfs open file operation. It gets
2130 * the vport from the i_private field in @inode, allocates the necessary buffer
2131 * for the log, fills the buffer from the in-memory log for this vport, and then
2132 * returns a pointer to that log in the private_data field in @file.
2133 *
2134 * Returns:
2135 * This function returns zero if successful. On error it will return a negative
2136 * error value.
2137 **/
2138static int
2139lpfc_debugfs_dumpHostSlim_open(struct inode *inode, struct file *file)
2140{
2141	struct lpfc_hba *phba = inode->i_private;
2142	struct lpfc_debug *debug;
2143	int rc = -ENOMEM;
2144
2145	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2146	if (!debug)
2147		goto out;
2148
2149	/* Round to page boundary */
2150	debug->buffer = kmalloc(LPFC_DUMPHOSTSLIM_SIZE, GFP_KERNEL);
2151	if (!debug->buffer) {
2152		kfree(debug);
2153		goto out;
2154	}
2155
2156	debug->len = lpfc_debugfs_dumpHostSlim_data(phba, debug->buffer,
2157		LPFC_DUMPHOSTSLIM_SIZE);
2158	file->private_data = debug;
2159
2160	rc = 0;
2161out:
2162	return rc;
2163}
2164
2165static ssize_t
2166lpfc_debugfs_dif_err_read(struct file *file, char __user *buf,
2167	size_t nbytes, loff_t *ppos)
2168{
2169	struct dentry *dent = file->f_path.dentry;
2170	struct lpfc_hba *phba = file->private_data;
2171	char cbuf[32];
2172	uint64_t tmp = 0;
2173	int cnt = 0;
2174
2175	if (dent == phba->debug_writeGuard)
2176		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wgrd_cnt);
2177	else if (dent == phba->debug_writeApp)
2178		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wapp_cnt);
2179	else if (dent == phba->debug_writeRef)
2180		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wref_cnt);
2181	else if (dent == phba->debug_readGuard)
2182		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rgrd_cnt);
2183	else if (dent == phba->debug_readApp)
2184		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rapp_cnt);
2185	else if (dent == phba->debug_readRef)
2186		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rref_cnt);
2187	else if (dent == phba->debug_InjErrNPortID)
2188		cnt = scnprintf(cbuf, 32, "0x%06x\n",
2189				phba->lpfc_injerr_nportid);
2190	else if (dent == phba->debug_InjErrWWPN) {
2191		memcpy(&tmp, &phba->lpfc_injerr_wwpn, sizeof(struct lpfc_name));
2192		tmp = cpu_to_be64(tmp);
2193		cnt = scnprintf(cbuf, 32, "0x%016llx\n", tmp);
2194	} else if (dent == phba->debug_InjErrLBA) {
2195		if (phba->lpfc_injerr_lba == (sector_t)(-1))
2196			cnt = scnprintf(cbuf, 32, "off\n");
2197		else
2198			cnt = scnprintf(cbuf, 32, "0x%llx\n",
2199				 (uint64_t) phba->lpfc_injerr_lba);
2200	} else
2201		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2202			 "0547 Unknown debugfs error injection entry\n");
2203
2204	return simple_read_from_buffer(buf, nbytes, ppos, &cbuf, cnt);
2205}
2206
2207static ssize_t
2208lpfc_debugfs_dif_err_write(struct file *file, const char __user *buf,
2209	size_t nbytes, loff_t *ppos)
2210{
2211	struct dentry *dent = file->f_path.dentry;
2212	struct lpfc_hba *phba = file->private_data;
2213	char dstbuf[33];
2214	uint64_t tmp = 0;
2215	int size;
2216
2217	memset(dstbuf, 0, 33);
2218	size = (nbytes < 32) ? nbytes : 32;
2219	if (copy_from_user(dstbuf, buf, size))
2220		return 0;
2221
2222	if (dent == phba->debug_InjErrLBA) {
2223		if ((buf[0] == 'o') && (buf[1] == 'f') && (buf[2] == 'f'))
 
2224			tmp = (uint64_t)(-1);
2225	}
2226
2227	if ((tmp == 0) && (kstrtoull(dstbuf, 0, &tmp)))
2228		return 0;
2229
2230	if (dent == phba->debug_writeGuard)
2231		phba->lpfc_injerr_wgrd_cnt = (uint32_t)tmp;
2232	else if (dent == phba->debug_writeApp)
2233		phba->lpfc_injerr_wapp_cnt = (uint32_t)tmp;
2234	else if (dent == phba->debug_writeRef)
2235		phba->lpfc_injerr_wref_cnt = (uint32_t)tmp;
2236	else if (dent == phba->debug_readGuard)
2237		phba->lpfc_injerr_rgrd_cnt = (uint32_t)tmp;
2238	else if (dent == phba->debug_readApp)
2239		phba->lpfc_injerr_rapp_cnt = (uint32_t)tmp;
2240	else if (dent == phba->debug_readRef)
2241		phba->lpfc_injerr_rref_cnt = (uint32_t)tmp;
2242	else if (dent == phba->debug_InjErrLBA)
2243		phba->lpfc_injerr_lba = (sector_t)tmp;
2244	else if (dent == phba->debug_InjErrNPortID)
2245		phba->lpfc_injerr_nportid = (uint32_t)(tmp & Mask_DID);
2246	else if (dent == phba->debug_InjErrWWPN) {
2247		tmp = cpu_to_be64(tmp);
2248		memcpy(&phba->lpfc_injerr_wwpn, &tmp, sizeof(struct lpfc_name));
2249	} else
2250		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2251			 "0548 Unknown debugfs error injection entry\n");
2252
2253	return nbytes;
2254}
2255
2256static int
2257lpfc_debugfs_dif_err_release(struct inode *inode, struct file *file)
2258{
2259	return 0;
2260}
2261
2262/**
2263 * lpfc_debugfs_nodelist_open - Open the nodelist debugfs file
2264 * @inode: The inode pointer that contains a vport pointer.
2265 * @file: The file pointer to attach the log output.
2266 *
2267 * Description:
2268 * This routine is the entry point for the debugfs open file operation. It gets
2269 * the vport from the i_private field in @inode, allocates the necessary buffer
2270 * for the log, fills the buffer from the in-memory log for this vport, and then
2271 * returns a pointer to that log in the private_data field in @file.
2272 *
2273 * Returns:
2274 * This function returns zero if successful. On error it will return a negative
2275 * error value.
2276 **/
2277static int
2278lpfc_debugfs_nodelist_open(struct inode *inode, struct file *file)
2279{
2280	struct lpfc_vport *vport = inode->i_private;
2281	struct lpfc_debug *debug;
2282	int rc = -ENOMEM;
2283
2284	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2285	if (!debug)
2286		goto out;
2287
2288	/* Round to page boundary */
2289	debug->buffer = kmalloc(LPFC_NODELIST_SIZE, GFP_KERNEL);
2290	if (!debug->buffer) {
2291		kfree(debug);
2292		goto out;
2293	}
2294
2295	debug->len = lpfc_debugfs_nodelist_data(vport, debug->buffer,
2296		LPFC_NODELIST_SIZE);
2297	file->private_data = debug;
2298
2299	rc = 0;
2300out:
2301	return rc;
2302}
2303
2304/**
2305 * lpfc_debugfs_lseek - Seek through a debugfs file
2306 * @file: The file pointer to seek through.
2307 * @off: The offset to seek to or the amount to seek by.
2308 * @whence: Indicates how to seek.
2309 *
2310 * Description:
2311 * This routine is the entry point for the debugfs lseek file operation. The
2312 * @whence parameter indicates whether @off is the offset to directly seek to,
2313 * or if it is a value to seek forward or reverse by. This function figures out
2314 * what the new offset of the debugfs file will be and assigns that value to the
2315 * f_pos field of @file.
2316 *
2317 * Returns:
2318 * This function returns the new offset if successful and returns a negative
2319 * error if unable to process the seek.
2320 **/
2321static loff_t
2322lpfc_debugfs_lseek(struct file *file, loff_t off, int whence)
2323{
2324	struct lpfc_debug *debug = file->private_data;
2325	return fixed_size_llseek(file, off, whence, debug->len);
2326}
2327
2328/**
2329 * lpfc_debugfs_read - Read a debugfs file
2330 * @file: The file pointer to read from.
2331 * @buf: The buffer to copy the data to.
2332 * @nbytes: The number of bytes to read.
2333 * @ppos: The position in the file to start reading from.
2334 *
2335 * Description:
2336 * This routine reads data from from the buffer indicated in the private_data
2337 * field of @file. It will start reading at @ppos and copy up to @nbytes of
2338 * data to @buf.
2339 *
2340 * Returns:
2341 * This function returns the amount of data that was read (this could be less
2342 * than @nbytes if the end of the file was reached) or a negative error value.
2343 **/
2344static ssize_t
2345lpfc_debugfs_read(struct file *file, char __user *buf,
2346		  size_t nbytes, loff_t *ppos)
2347{
2348	struct lpfc_debug *debug = file->private_data;
2349
2350	return simple_read_from_buffer(buf, nbytes, ppos, debug->buffer,
2351				       debug->len);
2352}
2353
2354/**
2355 * lpfc_debugfs_release - Release the buffer used to store debugfs file data
2356 * @inode: The inode pointer that contains a vport pointer. (unused)
2357 * @file: The file pointer that contains the buffer to release.
2358 *
2359 * Description:
2360 * This routine frees the buffer that was allocated when the debugfs file was
2361 * opened.
2362 *
2363 * Returns:
2364 * This function returns zero.
2365 **/
2366static int
2367lpfc_debugfs_release(struct inode *inode, struct file *file)
2368{
2369	struct lpfc_debug *debug = file->private_data;
2370
2371	kfree(debug->buffer);
2372	kfree(debug);
2373
2374	return 0;
2375}
2376
2377/**
2378 * lpfc_debugfs_multixripools_write - Clear multi-XRI pools statistics
2379 * @file: The file pointer to read from.
2380 * @buf: The buffer to copy the user data from.
2381 * @nbytes: The number of bytes to get.
2382 * @ppos: The position in the file to start reading from.
2383 *
2384 * Description:
2385 * This routine clears multi-XRI pools statistics when buf contains "clear".
2386 *
2387 * Return Value:
2388 * It returns the @nbytges passing in from debugfs user space when successful.
2389 * In case of error conditions, it returns proper error code back to the user
2390 * space.
2391 **/
2392static ssize_t
2393lpfc_debugfs_multixripools_write(struct file *file, const char __user *buf,
2394				 size_t nbytes, loff_t *ppos)
2395{
2396	struct lpfc_debug *debug = file->private_data;
2397	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2398	char mybuf[64];
2399	char *pbuf;
2400	u32 i;
2401	u32 hwq_count;
2402	struct lpfc_sli4_hdw_queue *qp;
2403	struct lpfc_multixri_pool *multixri_pool;
2404
2405	if (nbytes > 64)
2406		nbytes = 64;
2407
2408	/* Protect copy from user */
2409	if (!access_ok(buf, nbytes))
2410		return -EFAULT;
2411
2412	memset(mybuf, 0, sizeof(mybuf));
2413
2414	if (copy_from_user(mybuf, buf, nbytes))
2415		return -EFAULT;
2416	pbuf = &mybuf[0];
2417
2418	if ((strncmp(pbuf, "clear", strlen("clear"))) == 0) {
2419		hwq_count = phba->cfg_hdw_queue;
2420		for (i = 0; i < hwq_count; i++) {
2421			qp = &phba->sli4_hba.hdwq[i];
2422			multixri_pool = qp->p_multixri_pool;
2423			if (!multixri_pool)
2424				continue;
2425
2426			qp->empty_io_bufs = 0;
2427			multixri_pool->pbl_empty_count = 0;
2428#ifdef LPFC_MXP_STAT
2429			multixri_pool->above_limit_count = 0;
2430			multixri_pool->below_limit_count = 0;
2431			multixri_pool->stat_max_hwm = 0;
2432			multixri_pool->local_pbl_hit_count = 0;
2433			multixri_pool->other_pbl_hit_count = 0;
2434
2435			multixri_pool->stat_pbl_count = 0;
2436			multixri_pool->stat_pvt_count = 0;
2437			multixri_pool->stat_busy_count = 0;
2438			multixri_pool->stat_snapshot_taken = 0;
2439#endif
2440		}
2441		return strlen(pbuf);
2442	}
2443
2444	return -EINVAL;
2445}
2446
2447static int
2448lpfc_debugfs_nvmestat_open(struct inode *inode, struct file *file)
2449{
2450	struct lpfc_vport *vport = inode->i_private;
2451	struct lpfc_debug *debug;
2452	int rc = -ENOMEM;
2453
2454	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2455	if (!debug)
2456		goto out;
2457
2458	 /* Round to page boundary */
2459	debug->buffer = kmalloc(LPFC_NVMESTAT_SIZE, GFP_KERNEL);
2460	if (!debug->buffer) {
2461		kfree(debug);
2462		goto out;
2463	}
2464
2465	debug->len = lpfc_debugfs_nvmestat_data(vport, debug->buffer,
2466		LPFC_NVMESTAT_SIZE);
2467
2468	debug->i_private = inode->i_private;
2469	file->private_data = debug;
2470
2471	rc = 0;
2472out:
2473	return rc;
2474}
2475
2476static ssize_t
2477lpfc_debugfs_nvmestat_write(struct file *file, const char __user *buf,
2478			    size_t nbytes, loff_t *ppos)
2479{
2480	struct lpfc_debug *debug = file->private_data;
2481	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2482	struct lpfc_hba   *phba = vport->phba;
2483	struct lpfc_nvmet_tgtport *tgtp;
2484	char mybuf[64];
2485	char *pbuf;
2486
2487	if (!phba->targetport)
2488		return -ENXIO;
2489
2490	if (nbytes > 64)
2491		nbytes = 64;
2492
2493	memset(mybuf, 0, sizeof(mybuf));
2494
2495	if (copy_from_user(mybuf, buf, nbytes))
2496		return -EFAULT;
2497	pbuf = &mybuf[0];
2498
2499	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2500	if ((strncmp(pbuf, "reset", strlen("reset")) == 0) ||
2501	    (strncmp(pbuf, "zero", strlen("zero")) == 0)) {
2502		atomic_set(&tgtp->rcv_ls_req_in, 0);
2503		atomic_set(&tgtp->rcv_ls_req_out, 0);
2504		atomic_set(&tgtp->rcv_ls_req_drop, 0);
2505		atomic_set(&tgtp->xmt_ls_abort, 0);
2506		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
2507		atomic_set(&tgtp->xmt_ls_rsp, 0);
2508		atomic_set(&tgtp->xmt_ls_drop, 0);
2509		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
2510		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
2511
2512		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
2513		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
2514		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
2515		atomic_set(&tgtp->xmt_fcp_drop, 0);
2516		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
2517		atomic_set(&tgtp->xmt_fcp_read, 0);
2518		atomic_set(&tgtp->xmt_fcp_write, 0);
2519		atomic_set(&tgtp->xmt_fcp_rsp, 0);
2520		atomic_set(&tgtp->xmt_fcp_release, 0);
2521		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
2522		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
2523		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
2524
2525		atomic_set(&tgtp->xmt_fcp_abort, 0);
2526		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
2527		atomic_set(&tgtp->xmt_abort_sol, 0);
2528		atomic_set(&tgtp->xmt_abort_unsol, 0);
2529		atomic_set(&tgtp->xmt_abort_rsp, 0);
2530		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
2531	}
2532	return nbytes;
2533}
2534
2535static int
2536lpfc_debugfs_scsistat_open(struct inode *inode, struct file *file)
2537{
2538	struct lpfc_vport *vport = inode->i_private;
2539	struct lpfc_debug *debug;
2540	int rc = -ENOMEM;
2541
2542	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2543	if (!debug)
2544		goto out;
2545
2546	 /* Round to page boundary */
2547	debug->buffer = kzalloc(LPFC_SCSISTAT_SIZE, GFP_KERNEL);
2548	if (!debug->buffer) {
2549		kfree(debug);
2550		goto out;
2551	}
2552
2553	debug->len = lpfc_debugfs_scsistat_data(vport, debug->buffer,
2554		LPFC_SCSISTAT_SIZE);
2555
2556	debug->i_private = inode->i_private;
2557	file->private_data = debug;
2558
2559	rc = 0;
2560out:
2561	return rc;
2562}
2563
2564static ssize_t
2565lpfc_debugfs_scsistat_write(struct file *file, const char __user *buf,
2566			    size_t nbytes, loff_t *ppos)
2567{
2568	struct lpfc_debug *debug = file->private_data;
2569	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2570	struct lpfc_hba *phba = vport->phba;
2571	char mybuf[6] = {0};
2572	int i;
2573
2574	/* Protect copy from user */
2575	if (!access_ok(buf, nbytes))
2576		return -EFAULT;
2577
2578	if (copy_from_user(mybuf, buf, (nbytes >= sizeof(mybuf)) ?
2579				       (sizeof(mybuf) - 1) : nbytes))
2580		return -EFAULT;
2581
2582	if ((strncmp(&mybuf[0], "reset", strlen("reset")) == 0) ||
2583	    (strncmp(&mybuf[0], "zero", strlen("zero")) == 0)) {
2584		for (i = 0; i < phba->cfg_hdw_queue; i++) {
2585			memset(&phba->sli4_hba.hdwq[i].scsi_cstat, 0,
2586			       sizeof(phba->sli4_hba.hdwq[i].scsi_cstat));
2587		}
2588	}
2589
2590	return nbytes;
2591}
2592
2593static int
2594lpfc_debugfs_nvmektime_open(struct inode *inode, struct file *file)
2595{
2596	struct lpfc_vport *vport = inode->i_private;
2597	struct lpfc_debug *debug;
2598	int rc = -ENOMEM;
2599
2600	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2601	if (!debug)
2602		goto out;
2603
2604	 /* Round to page boundary */
2605	debug->buffer = kmalloc(LPFC_NVMEKTIME_SIZE, GFP_KERNEL);
2606	if (!debug->buffer) {
2607		kfree(debug);
2608		goto out;
2609	}
2610
2611	debug->len = lpfc_debugfs_nvmektime_data(vport, debug->buffer,
2612		LPFC_NVMEKTIME_SIZE);
2613
2614	debug->i_private = inode->i_private;
2615	file->private_data = debug;
2616
2617	rc = 0;
2618out:
2619	return rc;
2620}
2621
2622static ssize_t
2623lpfc_debugfs_nvmektime_write(struct file *file, const char __user *buf,
2624			     size_t nbytes, loff_t *ppos)
2625{
2626	struct lpfc_debug *debug = file->private_data;
2627	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2628	struct lpfc_hba   *phba = vport->phba;
2629	char mybuf[64];
2630	char *pbuf;
2631
2632	if (nbytes > 64)
2633		nbytes = 64;
2634
2635	memset(mybuf, 0, sizeof(mybuf));
2636
2637	if (copy_from_user(mybuf, buf, nbytes))
2638		return -EFAULT;
2639	pbuf = &mybuf[0];
2640
2641	if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2642		phba->ktime_data_samples = 0;
2643		phba->ktime_status_samples = 0;
2644		phba->ktime_seg1_total = 0;
2645		phba->ktime_seg1_max = 0;
2646		phba->ktime_seg1_min = 0xffffffff;
2647		phba->ktime_seg2_total = 0;
2648		phba->ktime_seg2_max = 0;
2649		phba->ktime_seg2_min = 0xffffffff;
2650		phba->ktime_seg3_total = 0;
2651		phba->ktime_seg3_max = 0;
2652		phba->ktime_seg3_min = 0xffffffff;
2653		phba->ktime_seg4_total = 0;
2654		phba->ktime_seg4_max = 0;
2655		phba->ktime_seg4_min = 0xffffffff;
2656		phba->ktime_seg5_total = 0;
2657		phba->ktime_seg5_max = 0;
2658		phba->ktime_seg5_min = 0xffffffff;
2659		phba->ktime_seg6_total = 0;
2660		phba->ktime_seg6_max = 0;
2661		phba->ktime_seg6_min = 0xffffffff;
2662		phba->ktime_seg7_total = 0;
2663		phba->ktime_seg7_max = 0;
2664		phba->ktime_seg7_min = 0xffffffff;
2665		phba->ktime_seg8_total = 0;
2666		phba->ktime_seg8_max = 0;
2667		phba->ktime_seg8_min = 0xffffffff;
2668		phba->ktime_seg9_total = 0;
2669		phba->ktime_seg9_max = 0;
2670		phba->ktime_seg9_min = 0xffffffff;
2671		phba->ktime_seg10_total = 0;
2672		phba->ktime_seg10_max = 0;
2673		phba->ktime_seg10_min = 0xffffffff;
2674
2675		phba->ktime_on = 1;
2676		return strlen(pbuf);
2677	} else if ((strncmp(pbuf, "off",
2678		   sizeof("off") - 1) == 0)) {
2679		phba->ktime_on = 0;
2680		return strlen(pbuf);
2681	} else if ((strncmp(pbuf, "zero",
2682		   sizeof("zero") - 1) == 0)) {
2683		phba->ktime_data_samples = 0;
2684		phba->ktime_status_samples = 0;
2685		phba->ktime_seg1_total = 0;
2686		phba->ktime_seg1_max = 0;
2687		phba->ktime_seg1_min = 0xffffffff;
2688		phba->ktime_seg2_total = 0;
2689		phba->ktime_seg2_max = 0;
2690		phba->ktime_seg2_min = 0xffffffff;
2691		phba->ktime_seg3_total = 0;
2692		phba->ktime_seg3_max = 0;
2693		phba->ktime_seg3_min = 0xffffffff;
2694		phba->ktime_seg4_total = 0;
2695		phba->ktime_seg4_max = 0;
2696		phba->ktime_seg4_min = 0xffffffff;
2697		phba->ktime_seg5_total = 0;
2698		phba->ktime_seg5_max = 0;
2699		phba->ktime_seg5_min = 0xffffffff;
2700		phba->ktime_seg6_total = 0;
2701		phba->ktime_seg6_max = 0;
2702		phba->ktime_seg6_min = 0xffffffff;
2703		phba->ktime_seg7_total = 0;
2704		phba->ktime_seg7_max = 0;
2705		phba->ktime_seg7_min = 0xffffffff;
2706		phba->ktime_seg8_total = 0;
2707		phba->ktime_seg8_max = 0;
2708		phba->ktime_seg8_min = 0xffffffff;
2709		phba->ktime_seg9_total = 0;
2710		phba->ktime_seg9_max = 0;
2711		phba->ktime_seg9_min = 0xffffffff;
2712		phba->ktime_seg10_total = 0;
2713		phba->ktime_seg10_max = 0;
2714		phba->ktime_seg10_min = 0xffffffff;
2715		return strlen(pbuf);
2716	}
2717	return -EINVAL;
2718}
2719
2720static int
2721lpfc_debugfs_nvmeio_trc_open(struct inode *inode, struct file *file)
2722{
2723	struct lpfc_hba *phba = inode->i_private;
2724	struct lpfc_debug *debug;
2725	int rc = -ENOMEM;
2726
2727	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2728	if (!debug)
2729		goto out;
2730
2731	 /* Round to page boundary */
2732	debug->buffer = kmalloc(LPFC_NVMEIO_TRC_SIZE, GFP_KERNEL);
2733	if (!debug->buffer) {
2734		kfree(debug);
2735		goto out;
2736	}
2737
2738	debug->len = lpfc_debugfs_nvmeio_trc_data(phba, debug->buffer,
2739		LPFC_NVMEIO_TRC_SIZE);
2740
2741	debug->i_private = inode->i_private;
2742	file->private_data = debug;
2743
2744	rc = 0;
2745out:
2746	return rc;
2747}
2748
2749static ssize_t
2750lpfc_debugfs_nvmeio_trc_write(struct file *file, const char __user *buf,
2751			      size_t nbytes, loff_t *ppos)
2752{
2753	struct lpfc_debug *debug = file->private_data;
2754	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2755	int i;
2756	unsigned long sz;
2757	char mybuf[64];
2758	char *pbuf;
2759
2760	if (nbytes > 64)
2761		nbytes = 64;
2762
2763	memset(mybuf, 0, sizeof(mybuf));
2764
2765	if (copy_from_user(mybuf, buf, nbytes))
2766		return -EFAULT;
2767	pbuf = &mybuf[0];
2768
2769	if ((strncmp(pbuf, "off", sizeof("off") - 1) == 0)) {
2770		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2771				"0570 nvmeio_trc_off\n");
2772		phba->nvmeio_trc_output_idx = 0;
2773		phba->nvmeio_trc_on = 0;
2774		return strlen(pbuf);
2775	} else if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2776		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2777				"0571 nvmeio_trc_on\n");
2778		phba->nvmeio_trc_output_idx = 0;
2779		phba->nvmeio_trc_on = 1;
2780		return strlen(pbuf);
2781	}
2782
2783	/* We must be off to allocate the trace buffer */
2784	if (phba->nvmeio_trc_on != 0)
2785		return -EINVAL;
2786
2787	/* If not on or off, the parameter is the trace buffer size */
2788	i = kstrtoul(pbuf, 0, &sz);
2789	if (i)
2790		return -EINVAL;
2791	phba->nvmeio_trc_size = (uint32_t)sz;
2792
2793	/* It must be a power of 2 - round down */
2794	i = 0;
2795	while (sz > 1) {
2796		sz = sz >> 1;
2797		i++;
2798	}
2799	sz = (1 << i);
2800	if (phba->nvmeio_trc_size != sz)
2801		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2802				"0572 nvmeio_trc_size changed to %ld\n",
2803				sz);
2804	phba->nvmeio_trc_size = (uint32_t)sz;
2805
2806	/* If one previously exists, free it */
2807	kfree(phba->nvmeio_trc);
2808
2809	/* Allocate new trace buffer and initialize */
2810	phba->nvmeio_trc = kzalloc((sizeof(struct lpfc_debugfs_nvmeio_trc) *
2811				    sz), GFP_KERNEL);
2812	if (!phba->nvmeio_trc) {
2813		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2814				"0573 Cannot create debugfs "
2815				"nvmeio_trc buffer\n");
2816		return -ENOMEM;
2817	}
2818	atomic_set(&phba->nvmeio_trc_cnt, 0);
2819	phba->nvmeio_trc_on = 0;
2820	phba->nvmeio_trc_output_idx = 0;
2821
2822	return strlen(pbuf);
2823}
2824
2825static int
2826lpfc_debugfs_cpucheck_open(struct inode *inode, struct file *file)
2827{
2828	struct lpfc_vport *vport = inode->i_private;
2829	struct lpfc_debug *debug;
2830	int rc = -ENOMEM;
2831
2832	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2833	if (!debug)
2834		goto out;
2835
2836	 /* Round to page boundary */
2837	debug->buffer = kmalloc(LPFC_CPUCHECK_SIZE, GFP_KERNEL);
2838	if (!debug->buffer) {
2839		kfree(debug);
2840		goto out;
2841	}
2842
2843	debug->len = lpfc_debugfs_cpucheck_data(vport, debug->buffer,
2844		LPFC_CPUCHECK_SIZE);
2845
2846	debug->i_private = inode->i_private;
2847	file->private_data = debug;
2848
2849	rc = 0;
2850out:
2851	return rc;
2852}
2853
2854static ssize_t
2855lpfc_debugfs_cpucheck_write(struct file *file, const char __user *buf,
2856			    size_t nbytes, loff_t *ppos)
2857{
2858	struct lpfc_debug *debug = file->private_data;
2859	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2860	struct lpfc_hba   *phba = vport->phba;
2861	struct lpfc_sli4_hdw_queue *qp;
2862	char mybuf[64];
2863	char *pbuf;
2864	int i, j;
2865
2866	if (nbytes > 64)
2867		nbytes = 64;
2868
2869	memset(mybuf, 0, sizeof(mybuf));
2870
2871	if (copy_from_user(mybuf, buf, nbytes))
2872		return -EFAULT;
2873	pbuf = &mybuf[0];
2874
2875	if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2876		if (phba->nvmet_support)
2877			phba->cpucheck_on |= LPFC_CHECK_NVMET_IO;
2878		else
2879			phba->cpucheck_on |= (LPFC_CHECK_NVME_IO |
2880				LPFC_CHECK_SCSI_IO);
2881		return strlen(pbuf);
2882	} else if ((strncmp(pbuf, "nvme_on", sizeof("nvme_on") - 1) == 0)) {
2883		if (phba->nvmet_support)
2884			phba->cpucheck_on |= LPFC_CHECK_NVMET_IO;
2885		else
2886			phba->cpucheck_on |= LPFC_CHECK_NVME_IO;
2887		return strlen(pbuf);
2888	} else if ((strncmp(pbuf, "scsi_on", sizeof("scsi_on") - 1) == 0)) {
2889		phba->cpucheck_on |= LPFC_CHECK_SCSI_IO;
 
 
 
 
 
2890		return strlen(pbuf);
2891	} else if ((strncmp(pbuf, "rcv",
2892		   sizeof("rcv") - 1) == 0)) {
2893		if (phba->nvmet_support)
2894			phba->cpucheck_on |= LPFC_CHECK_NVMET_RCV;
2895		else
2896			return -EINVAL;
2897		return strlen(pbuf);
2898	} else if ((strncmp(pbuf, "off",
2899		   sizeof("off") - 1) == 0)) {
2900		phba->cpucheck_on = LPFC_CHECK_OFF;
2901		return strlen(pbuf);
2902	} else if ((strncmp(pbuf, "zero",
2903		   sizeof("zero") - 1) == 0)) {
2904		for (i = 0; i < phba->cfg_hdw_queue; i++) {
2905			qp = &phba->sli4_hba.hdwq[i];
2906
2907			for (j = 0; j < LPFC_CHECK_CPU_CNT; j++) {
2908				qp->cpucheck_rcv_io[j] = 0;
2909				qp->cpucheck_xmt_io[j] = 0;
2910				qp->cpucheck_cmpl_io[j] = 0;
2911			}
2912		}
2913		return strlen(pbuf);
2914	}
2915	return -EINVAL;
2916}
2917
2918/*
2919 * ---------------------------------
2920 * iDiag debugfs file access methods
2921 * ---------------------------------
2922 *
2923 * All access methods are through the proper SLI4 PCI function's debugfs
2924 * iDiag directory:
2925 *
2926 *     /sys/kernel/debug/lpfc/fn<#>/iDiag
2927 */
2928
2929/**
2930 * lpfc_idiag_cmd_get - Get and parse idiag debugfs comands from user space
2931 * @buf: The pointer to the user space buffer.
2932 * @nbytes: The number of bytes in the user space buffer.
2933 * @idiag_cmd: pointer to the idiag command struct.
2934 *
2935 * This routine reads data from debugfs user space buffer and parses the
2936 * buffer for getting the idiag command and arguments. The while space in
2937 * between the set of data is used as the parsing separator.
2938 *
2939 * This routine returns 0 when successful, it returns proper error code
2940 * back to the user space in error conditions.
2941 */
2942static int lpfc_idiag_cmd_get(const char __user *buf, size_t nbytes,
2943			      struct lpfc_idiag_cmd *idiag_cmd)
2944{
2945	char mybuf[64];
2946	char *pbuf, *step_str;
2947	int i;
2948	size_t bsize;
2949
2950	memset(mybuf, 0, sizeof(mybuf));
2951	memset(idiag_cmd, 0, sizeof(*idiag_cmd));
2952	bsize = min(nbytes, (sizeof(mybuf)-1));
2953
2954	if (copy_from_user(mybuf, buf, bsize))
2955		return -EFAULT;
2956	pbuf = &mybuf[0];
2957	step_str = strsep(&pbuf, "\t ");
2958
2959	/* The opcode must present */
2960	if (!step_str)
2961		return -EINVAL;
2962
2963	idiag_cmd->opcode = simple_strtol(step_str, NULL, 0);
2964	if (idiag_cmd->opcode == 0)
2965		return -EINVAL;
2966
2967	for (i = 0; i < LPFC_IDIAG_CMD_DATA_SIZE; i++) {
2968		step_str = strsep(&pbuf, "\t ");
2969		if (!step_str)
2970			return i;
2971		idiag_cmd->data[i] = simple_strtol(step_str, NULL, 0);
2972	}
2973	return i;
2974}
2975
2976/**
2977 * lpfc_idiag_open - idiag open debugfs
2978 * @inode: The inode pointer that contains a pointer to phba.
2979 * @file: The file pointer to attach the file operation.
2980 *
2981 * Description:
2982 * This routine is the entry point for the debugfs open file operation. It
2983 * gets the reference to phba from the i_private field in @inode, it then
2984 * allocates buffer for the file operation, performs the necessary PCI config
2985 * space read into the allocated buffer according to the idiag user command
2986 * setup, and then returns a pointer to buffer in the private_data field in
2987 * @file.
2988 *
2989 * Returns:
2990 * This function returns zero if successful. On error it will return an
2991 * negative error value.
2992 **/
2993static int
2994lpfc_idiag_open(struct inode *inode, struct file *file)
2995{
2996	struct lpfc_debug *debug;
2997
2998	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2999	if (!debug)
3000		return -ENOMEM;
3001
3002	debug->i_private = inode->i_private;
3003	debug->buffer = NULL;
3004	file->private_data = debug;
3005
3006	return 0;
3007}
3008
3009/**
3010 * lpfc_idiag_release - Release idiag access file operation
3011 * @inode: The inode pointer that contains a vport pointer. (unused)
3012 * @file: The file pointer that contains the buffer to release.
3013 *
3014 * Description:
3015 * This routine is the generic release routine for the idiag access file
3016 * operation, it frees the buffer that was allocated when the debugfs file
3017 * was opened.
3018 *
3019 * Returns:
3020 * This function returns zero.
3021 **/
3022static int
3023lpfc_idiag_release(struct inode *inode, struct file *file)
3024{
3025	struct lpfc_debug *debug = file->private_data;
3026
3027	/* Free the buffers to the file operation */
3028	kfree(debug->buffer);
3029	kfree(debug);
3030
3031	return 0;
3032}
3033
3034/**
3035 * lpfc_idiag_cmd_release - Release idiag cmd access file operation
3036 * @inode: The inode pointer that contains a vport pointer. (unused)
3037 * @file: The file pointer that contains the buffer to release.
3038 *
3039 * Description:
3040 * This routine frees the buffer that was allocated when the debugfs file
3041 * was opened. It also reset the fields in the idiag command struct in the
3042 * case of command for write operation.
3043 *
3044 * Returns:
3045 * This function returns zero.
3046 **/
3047static int
3048lpfc_idiag_cmd_release(struct inode *inode, struct file *file)
3049{
3050	struct lpfc_debug *debug = file->private_data;
3051
3052	if (debug->op == LPFC_IDIAG_OP_WR) {
3053		switch (idiag.cmd.opcode) {
3054		case LPFC_IDIAG_CMD_PCICFG_WR:
3055		case LPFC_IDIAG_CMD_PCICFG_ST:
3056		case LPFC_IDIAG_CMD_PCICFG_CL:
3057		case LPFC_IDIAG_CMD_QUEACC_WR:
3058		case LPFC_IDIAG_CMD_QUEACC_ST:
3059		case LPFC_IDIAG_CMD_QUEACC_CL:
3060			memset(&idiag, 0, sizeof(idiag));
3061			break;
3062		default:
3063			break;
3064		}
3065	}
3066
3067	/* Free the buffers to the file operation */
3068	kfree(debug->buffer);
3069	kfree(debug);
3070
3071	return 0;
3072}
3073
3074/**
3075 * lpfc_idiag_pcicfg_read - idiag debugfs read pcicfg
3076 * @file: The file pointer to read from.
3077 * @buf: The buffer to copy the data to.
3078 * @nbytes: The number of bytes to read.
3079 * @ppos: The position in the file to start reading from.
3080 *
3081 * Description:
3082 * This routine reads data from the @phba pci config space according to the
3083 * idiag command, and copies to user @buf. Depending on the PCI config space
3084 * read command setup, it does either a single register read of a byte
3085 * (8 bits), a word (16 bits), or a dword (32 bits) or browsing through all
3086 * registers from the 4K extended PCI config space.
3087 *
3088 * Returns:
3089 * This function returns the amount of data that was read (this could be less
3090 * than @nbytes if the end of the file was reached) or a negative error value.
3091 **/
3092static ssize_t
3093lpfc_idiag_pcicfg_read(struct file *file, char __user *buf, size_t nbytes,
3094		       loff_t *ppos)
3095{
3096	struct lpfc_debug *debug = file->private_data;
3097	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3098	int offset_label, offset, len = 0, index = LPFC_PCI_CFG_RD_SIZE;
3099	int where, count;
3100	char *pbuffer;
3101	struct pci_dev *pdev;
3102	uint32_t u32val;
3103	uint16_t u16val;
3104	uint8_t u8val;
3105
3106	pdev = phba->pcidev;
3107	if (!pdev)
3108		return 0;
3109
3110	/* This is a user read operation */
3111	debug->op = LPFC_IDIAG_OP_RD;
3112
3113	if (!debug->buffer)
3114		debug->buffer = kmalloc(LPFC_PCI_CFG_SIZE, GFP_KERNEL);
3115	if (!debug->buffer)
3116		return 0;
3117	pbuffer = debug->buffer;
3118
3119	if (*ppos)
3120		return 0;
3121
3122	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
3123		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
3124		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
3125	} else
3126		return 0;
3127
3128	/* Read single PCI config space register */
3129	switch (count) {
3130	case SIZE_U8: /* byte (8 bits) */
3131		pci_read_config_byte(pdev, where, &u8val);
3132		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3133				"%03x: %02x\n", where, u8val);
3134		break;
3135	case SIZE_U16: /* word (16 bits) */
3136		pci_read_config_word(pdev, where, &u16val);
3137		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3138				"%03x: %04x\n", where, u16val);
3139		break;
3140	case SIZE_U32: /* double word (32 bits) */
3141		pci_read_config_dword(pdev, where, &u32val);
3142		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3143				"%03x: %08x\n", where, u32val);
3144		break;
3145	case LPFC_PCI_CFG_BROWSE: /* browse all */
3146		goto pcicfg_browse;
3147		break;
3148	default:
3149		/* illegal count */
3150		len = 0;
3151		break;
3152	}
3153	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3154
3155pcicfg_browse:
3156
3157	/* Browse all PCI config space registers */
3158	offset_label = idiag.offset.last_rd;
3159	offset = offset_label;
3160
3161	/* Read PCI config space */
3162	len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3163			"%03x: ", offset_label);
3164	while (index > 0) {
3165		pci_read_config_dword(pdev, offset, &u32val);
3166		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3167				"%08x ", u32val);
3168		offset += sizeof(uint32_t);
3169		if (offset >= LPFC_PCI_CFG_SIZE) {
3170			len += scnprintf(pbuffer+len,
3171					LPFC_PCI_CFG_SIZE-len, "\n");
3172			break;
3173		}
3174		index -= sizeof(uint32_t);
3175		if (!index)
3176			len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3177					"\n");
3178		else if (!(index % (8 * sizeof(uint32_t)))) {
3179			offset_label += (8 * sizeof(uint32_t));
3180			len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3181					"\n%03x: ", offset_label);
3182		}
3183	}
3184
3185	/* Set up the offset for next portion of pci cfg read */
3186	if (index == 0) {
3187		idiag.offset.last_rd += LPFC_PCI_CFG_RD_SIZE;
3188		if (idiag.offset.last_rd >= LPFC_PCI_CFG_SIZE)
3189			idiag.offset.last_rd = 0;
3190	} else
3191		idiag.offset.last_rd = 0;
3192
3193	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3194}
3195
3196/**
3197 * lpfc_idiag_pcicfg_write - Syntax check and set up idiag pcicfg commands
3198 * @file: The file pointer to read from.
3199 * @buf: The buffer to copy the user data from.
3200 * @nbytes: The number of bytes to get.
3201 * @ppos: The position in the file to start reading from.
3202 *
3203 * This routine get the debugfs idiag command struct from user space and
3204 * then perform the syntax check for PCI config space read or write command
3205 * accordingly. In the case of PCI config space read command, it sets up
3206 * the command in the idiag command struct for the debugfs read operation.
3207 * In the case of PCI config space write operation, it executes the write
3208 * operation into the PCI config space accordingly.
3209 *
3210 * It returns the @nbytges passing in from debugfs user space when successful.
3211 * In case of error conditions, it returns proper error code back to the user
3212 * space.
3213 */
3214static ssize_t
3215lpfc_idiag_pcicfg_write(struct file *file, const char __user *buf,
3216			size_t nbytes, loff_t *ppos)
3217{
3218	struct lpfc_debug *debug = file->private_data;
3219	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3220	uint32_t where, value, count;
3221	uint32_t u32val;
3222	uint16_t u16val;
3223	uint8_t u8val;
3224	struct pci_dev *pdev;
3225	int rc;
3226
3227	pdev = phba->pcidev;
3228	if (!pdev)
3229		return -EFAULT;
3230
3231	/* This is a user write operation */
3232	debug->op = LPFC_IDIAG_OP_WR;
3233
3234	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3235	if (rc < 0)
3236		return rc;
3237
3238	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
3239		/* Sanity check on PCI config read command line arguments */
3240		if (rc != LPFC_PCI_CFG_RD_CMD_ARG)
3241			goto error_out;
3242		/* Read command from PCI config space, set up command fields */
3243		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
3244		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
3245		if (count == LPFC_PCI_CFG_BROWSE) {
3246			if (where % sizeof(uint32_t))
3247				goto error_out;
3248			/* Starting offset to browse */
3249			idiag.offset.last_rd = where;
3250		} else if ((count != sizeof(uint8_t)) &&
3251			   (count != sizeof(uint16_t)) &&
3252			   (count != sizeof(uint32_t)))
3253			goto error_out;
3254		if (count == sizeof(uint8_t)) {
3255			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
3256				goto error_out;
3257			if (where % sizeof(uint8_t))
3258				goto error_out;
3259		}
3260		if (count == sizeof(uint16_t)) {
3261			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
3262				goto error_out;
3263			if (where % sizeof(uint16_t))
3264				goto error_out;
3265		}
3266		if (count == sizeof(uint32_t)) {
3267			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
3268				goto error_out;
3269			if (where % sizeof(uint32_t))
3270				goto error_out;
3271		}
3272	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR ||
3273		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST ||
3274		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3275		/* Sanity check on PCI config write command line arguments */
3276		if (rc != LPFC_PCI_CFG_WR_CMD_ARG)
3277			goto error_out;
3278		/* Write command to PCI config space, read-modify-write */
3279		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
3280		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
3281		value = idiag.cmd.data[IDIAG_PCICFG_VALUE_INDX];
3282		/* Sanity checks */
3283		if ((count != sizeof(uint8_t)) &&
3284		    (count != sizeof(uint16_t)) &&
3285		    (count != sizeof(uint32_t)))
3286			goto error_out;
3287		if (count == sizeof(uint8_t)) {
3288			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
3289				goto error_out;
3290			if (where % sizeof(uint8_t))
3291				goto error_out;
3292			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
3293				pci_write_config_byte(pdev, where,
3294						      (uint8_t)value);
3295			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
3296				rc = pci_read_config_byte(pdev, where, &u8val);
3297				if (!rc) {
3298					u8val |= (uint8_t)value;
3299					pci_write_config_byte(pdev, where,
3300							      u8val);
3301				}
3302			}
3303			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3304				rc = pci_read_config_byte(pdev, where, &u8val);
3305				if (!rc) {
3306					u8val &= (uint8_t)(~value);
3307					pci_write_config_byte(pdev, where,
3308							      u8val);
3309				}
3310			}
3311		}
3312		if (count == sizeof(uint16_t)) {
3313			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
3314				goto error_out;
3315			if (where % sizeof(uint16_t))
3316				goto error_out;
3317			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
3318				pci_write_config_word(pdev, where,
3319						      (uint16_t)value);
3320			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
3321				rc = pci_read_config_word(pdev, where, &u16val);
3322				if (!rc) {
3323					u16val |= (uint16_t)value;
3324					pci_write_config_word(pdev, where,
3325							      u16val);
3326				}
3327			}
3328			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3329				rc = pci_read_config_word(pdev, where, &u16val);
3330				if (!rc) {
3331					u16val &= (uint16_t)(~value);
3332					pci_write_config_word(pdev, where,
3333							      u16val);
3334				}
3335			}
3336		}
3337		if (count == sizeof(uint32_t)) {
3338			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
3339				goto error_out;
3340			if (where % sizeof(uint32_t))
3341				goto error_out;
3342			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
3343				pci_write_config_dword(pdev, where, value);
3344			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
3345				rc = pci_read_config_dword(pdev, where,
3346							   &u32val);
3347				if (!rc) {
3348					u32val |= value;
3349					pci_write_config_dword(pdev, where,
3350							       u32val);
3351				}
3352			}
3353			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3354				rc = pci_read_config_dword(pdev, where,
3355							   &u32val);
3356				if (!rc) {
3357					u32val &= ~value;
3358					pci_write_config_dword(pdev, where,
3359							       u32val);
3360				}
3361			}
3362		}
3363	} else
3364		/* All other opecodes are illegal for now */
3365		goto error_out;
3366
3367	return nbytes;
3368error_out:
3369	memset(&idiag, 0, sizeof(idiag));
3370	return -EINVAL;
3371}
3372
3373/**
3374 * lpfc_idiag_baracc_read - idiag debugfs pci bar access read
3375 * @file: The file pointer to read from.
3376 * @buf: The buffer to copy the data to.
3377 * @nbytes: The number of bytes to read.
3378 * @ppos: The position in the file to start reading from.
3379 *
3380 * Description:
3381 * This routine reads data from the @phba pci bar memory mapped space
3382 * according to the idiag command, and copies to user @buf.
3383 *
3384 * Returns:
3385 * This function returns the amount of data that was read (this could be less
3386 * than @nbytes if the end of the file was reached) or a negative error value.
3387 **/
3388static ssize_t
3389lpfc_idiag_baracc_read(struct file *file, char __user *buf, size_t nbytes,
3390		       loff_t *ppos)
3391{
3392	struct lpfc_debug *debug = file->private_data;
3393	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3394	int offset_label, offset, offset_run, len = 0, index;
3395	int bar_num, acc_range, bar_size;
3396	char *pbuffer;
3397	void __iomem *mem_mapped_bar;
3398	uint32_t if_type;
3399	struct pci_dev *pdev;
3400	uint32_t u32val;
3401
3402	pdev = phba->pcidev;
3403	if (!pdev)
3404		return 0;
3405
3406	/* This is a user read operation */
3407	debug->op = LPFC_IDIAG_OP_RD;
3408
3409	if (!debug->buffer)
3410		debug->buffer = kmalloc(LPFC_PCI_BAR_RD_BUF_SIZE, GFP_KERNEL);
3411	if (!debug->buffer)
3412		return 0;
3413	pbuffer = debug->buffer;
3414
3415	if (*ppos)
3416		return 0;
3417
3418	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
3419		bar_num   = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
3420		offset    = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
3421		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
3422		bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
3423	} else
3424		return 0;
3425
3426	if (acc_range == 0)
3427		return 0;
3428
3429	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
3430	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
3431		if (bar_num == IDIAG_BARACC_BAR_0)
3432			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3433		else if (bar_num == IDIAG_BARACC_BAR_1)
3434			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
3435		else if (bar_num == IDIAG_BARACC_BAR_2)
3436			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
3437		else
3438			return 0;
3439	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3440		if (bar_num == IDIAG_BARACC_BAR_0)
3441			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3442		else
3443			return 0;
3444	} else
3445		return 0;
3446
3447	/* Read single PCI bar space register */
3448	if (acc_range == SINGLE_WORD) {
3449		offset_run = offset;
3450		u32val = readl(mem_mapped_bar + offset_run);
3451		len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3452				"%05x: %08x\n", offset_run, u32val);
3453	} else
3454		goto baracc_browse;
3455
3456	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3457
3458baracc_browse:
3459
3460	/* Browse all PCI bar space registers */
3461	offset_label = idiag.offset.last_rd;
3462	offset_run = offset_label;
3463
3464	/* Read PCI bar memory mapped space */
3465	len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3466			"%05x: ", offset_label);
3467	index = LPFC_PCI_BAR_RD_SIZE;
3468	while (index > 0) {
3469		u32val = readl(mem_mapped_bar + offset_run);
3470		len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3471				"%08x ", u32val);
3472		offset_run += sizeof(uint32_t);
3473		if (acc_range == LPFC_PCI_BAR_BROWSE) {
3474			if (offset_run >= bar_size) {
3475				len += scnprintf(pbuffer+len,
3476					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
3477				break;
3478			}
3479		} else {
3480			if (offset_run >= offset +
3481			    (acc_range * sizeof(uint32_t))) {
3482				len += scnprintf(pbuffer+len,
3483					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
3484				break;
3485			}
3486		}
3487		index -= sizeof(uint32_t);
3488		if (!index)
3489			len += scnprintf(pbuffer+len,
3490					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
3491		else if (!(index % (8 * sizeof(uint32_t)))) {
3492			offset_label += (8 * sizeof(uint32_t));
3493			len += scnprintf(pbuffer+len,
3494					LPFC_PCI_BAR_RD_BUF_SIZE-len,
3495					"\n%05x: ", offset_label);
3496		}
3497	}
3498
3499	/* Set up the offset for next portion of pci bar read */
3500	if (index == 0) {
3501		idiag.offset.last_rd += LPFC_PCI_BAR_RD_SIZE;
3502		if (acc_range == LPFC_PCI_BAR_BROWSE) {
3503			if (idiag.offset.last_rd >= bar_size)
3504				idiag.offset.last_rd = 0;
3505		} else {
3506			if (offset_run >= offset +
3507			    (acc_range * sizeof(uint32_t)))
3508				idiag.offset.last_rd = offset;
3509		}
3510	} else {
3511		if (acc_range == LPFC_PCI_BAR_BROWSE)
3512			idiag.offset.last_rd = 0;
3513		else
3514			idiag.offset.last_rd = offset;
3515	}
3516
3517	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3518}
3519
3520/**
3521 * lpfc_idiag_baracc_write - Syntax check and set up idiag bar access commands
3522 * @file: The file pointer to read from.
3523 * @buf: The buffer to copy the user data from.
3524 * @nbytes: The number of bytes to get.
3525 * @ppos: The position in the file to start reading from.
3526 *
3527 * This routine get the debugfs idiag command struct from user space and
3528 * then perform the syntax check for PCI bar memory mapped space read or
3529 * write command accordingly. In the case of PCI bar memory mapped space
3530 * read command, it sets up the command in the idiag command struct for
3531 * the debugfs read operation. In the case of PCI bar memorpy mapped space
3532 * write operation, it executes the write operation into the PCI bar memory
3533 * mapped space accordingly.
3534 *
3535 * It returns the @nbytges passing in from debugfs user space when successful.
3536 * In case of error conditions, it returns proper error code back to the user
3537 * space.
3538 */
3539static ssize_t
3540lpfc_idiag_baracc_write(struct file *file, const char __user *buf,
3541			size_t nbytes, loff_t *ppos)
3542{
3543	struct lpfc_debug *debug = file->private_data;
3544	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3545	uint32_t bar_num, bar_size, offset, value, acc_range;
3546	struct pci_dev *pdev;
3547	void __iomem *mem_mapped_bar;
3548	uint32_t if_type;
3549	uint32_t u32val;
3550	int rc;
3551
3552	pdev = phba->pcidev;
3553	if (!pdev)
3554		return -EFAULT;
3555
3556	/* This is a user write operation */
3557	debug->op = LPFC_IDIAG_OP_WR;
3558
3559	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3560	if (rc < 0)
3561		return rc;
3562
3563	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
3564	bar_num = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
3565
3566	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
3567		if ((bar_num != IDIAG_BARACC_BAR_0) &&
3568		    (bar_num != IDIAG_BARACC_BAR_1) &&
3569		    (bar_num != IDIAG_BARACC_BAR_2))
3570			goto error_out;
3571	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3572		if (bar_num != IDIAG_BARACC_BAR_0)
3573			goto error_out;
3574	} else
3575		goto error_out;
3576
3577	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
3578		if (bar_num == IDIAG_BARACC_BAR_0) {
3579			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3580				LPFC_PCI_IF0_BAR0_SIZE;
3581			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3582		} else if (bar_num == IDIAG_BARACC_BAR_1) {
3583			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3584				LPFC_PCI_IF0_BAR1_SIZE;
3585			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
3586		} else if (bar_num == IDIAG_BARACC_BAR_2) {
3587			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3588				LPFC_PCI_IF0_BAR2_SIZE;
3589			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
3590		} else
3591			goto error_out;
3592	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3593		if (bar_num == IDIAG_BARACC_BAR_0) {
3594			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3595				LPFC_PCI_IF2_BAR0_SIZE;
3596			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3597		} else
3598			goto error_out;
3599	} else
3600		goto error_out;
3601
3602	offset = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
3603	if (offset % sizeof(uint32_t))
3604		goto error_out;
3605
3606	bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
3607	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
3608		/* Sanity check on PCI config read command line arguments */
3609		if (rc != LPFC_PCI_BAR_RD_CMD_ARG)
3610			goto error_out;
3611		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
3612		if (acc_range == LPFC_PCI_BAR_BROWSE) {
3613			if (offset > bar_size - sizeof(uint32_t))
3614				goto error_out;
3615			/* Starting offset to browse */
3616			idiag.offset.last_rd = offset;
3617		} else if (acc_range > SINGLE_WORD) {
3618			if (offset + acc_range * sizeof(uint32_t) > bar_size)
3619				goto error_out;
3620			/* Starting offset to browse */
3621			idiag.offset.last_rd = offset;
3622		} else if (acc_range != SINGLE_WORD)
3623			goto error_out;
3624	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR ||
3625		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST ||
3626		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
3627		/* Sanity check on PCI bar write command line arguments */
3628		if (rc != LPFC_PCI_BAR_WR_CMD_ARG)
3629			goto error_out;
3630		/* Write command to PCI bar space, read-modify-write */
3631		acc_range = SINGLE_WORD;
3632		value = idiag.cmd.data[IDIAG_BARACC_REG_VAL_INDX];
3633		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR) {
3634			writel(value, mem_mapped_bar + offset);
3635			readl(mem_mapped_bar + offset);
3636		}
3637		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST) {
3638			u32val = readl(mem_mapped_bar + offset);
3639			u32val |= value;
3640			writel(u32val, mem_mapped_bar + offset);
3641			readl(mem_mapped_bar + offset);
3642		}
3643		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
3644			u32val = readl(mem_mapped_bar + offset);
3645			u32val &= ~value;
3646			writel(u32val, mem_mapped_bar + offset);
3647			readl(mem_mapped_bar + offset);
3648		}
3649	} else
3650		/* All other opecodes are illegal for now */
3651		goto error_out;
3652
3653	return nbytes;
3654error_out:
3655	memset(&idiag, 0, sizeof(idiag));
3656	return -EINVAL;
3657}
3658
3659static int
3660__lpfc_idiag_print_wq(struct lpfc_queue *qp, char *wqtype,
3661			char *pbuffer, int len)
3662{
3663	if (!qp)
3664		return len;
3665
3666	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3667			"\t\t%s WQ info: ", wqtype);
3668	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3669			"AssocCQID[%04d]: WQ-STAT[oflow:x%x posted:x%llx]\n",
3670			qp->assoc_qid, qp->q_cnt_1,
3671			(unsigned long long)qp->q_cnt_4);
3672	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3673			"\t\tWQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3674			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]",
3675			qp->queue_id, qp->entry_count,
3676			qp->entry_size, qp->host_index,
3677			qp->hba_index, qp->notify_interval);
3678	len +=  scnprintf(pbuffer + len,
3679			LPFC_QUE_INFO_GET_BUF_SIZE - len, "\n");
3680	return len;
3681}
3682
3683static int
3684lpfc_idiag_wqs_for_cq(struct lpfc_hba *phba, char *wqtype, char *pbuffer,
3685		int *len, int max_cnt, int cq_id)
3686{
3687	struct lpfc_queue *qp;
3688	int qidx;
3689
3690	for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
3691		qp = phba->sli4_hba.hdwq[qidx].io_wq;
3692		if (qp->assoc_qid != cq_id)
3693			continue;
3694		*len = __lpfc_idiag_print_wq(qp, wqtype, pbuffer, *len);
3695		if (*len >= max_cnt)
3696			return 1;
3697	}
3698	return 0;
3699}
3700
3701static int
3702__lpfc_idiag_print_cq(struct lpfc_queue *qp, char *cqtype,
3703			char *pbuffer, int len)
3704{
3705	if (!qp)
3706		return len;
3707
3708	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3709			"\t%s CQ info: ", cqtype);
3710	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3711			"AssocEQID[%02d]: CQ STAT[max:x%x relw:x%x "
3712			"xabt:x%x wq:x%llx]\n",
3713			qp->assoc_qid, qp->q_cnt_1, qp->q_cnt_2,
3714			qp->q_cnt_3, (unsigned long long)qp->q_cnt_4);
3715	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3716			"\tCQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3717			"HST-IDX[%04d], NTFI[%03d], PLMT[%03d]",
3718			qp->queue_id, qp->entry_count,
3719			qp->entry_size, qp->host_index,
3720			qp->notify_interval, qp->max_proc_limit);
3721
3722	len +=  scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3723			"\n");
3724
3725	return len;
3726}
3727
3728static int
3729__lpfc_idiag_print_rqpair(struct lpfc_queue *qp, struct lpfc_queue *datqp,
3730			char *rqtype, char *pbuffer, int len)
3731{
3732	if (!qp || !datqp)
3733		return len;
3734
3735	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3736			"\t\t%s RQ info: ", rqtype);
3737	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3738			"AssocCQID[%02d]: RQ-STAT[nopost:x%x nobuf:x%x "
3739			"posted:x%x rcv:x%llx]\n",
3740			qp->assoc_qid, qp->q_cnt_1, qp->q_cnt_2,
3741			qp->q_cnt_3, (unsigned long long)qp->q_cnt_4);
3742	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3743			"\t\tHQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3744			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]\n",
3745			qp->queue_id, qp->entry_count, qp->entry_size,
3746			qp->host_index, qp->hba_index, qp->notify_interval);
3747	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3748			"\t\tDQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3749			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]\n",
3750			datqp->queue_id, datqp->entry_count,
3751			datqp->entry_size, datqp->host_index,
3752			datqp->hba_index, datqp->notify_interval);
3753	return len;
3754}
3755
3756static int
3757lpfc_idiag_cqs_for_eq(struct lpfc_hba *phba, char *pbuffer,
3758		int *len, int max_cnt, int eqidx, int eq_id)
3759{
3760	struct lpfc_queue *qp;
3761	int rc;
3762
3763	qp = phba->sli4_hba.hdwq[eqidx].io_cq;
3764
3765	*len = __lpfc_idiag_print_cq(qp, "IO", pbuffer, *len);
3766
3767	/* Reset max counter */
3768	qp->CQ_max_cqe = 0;
3769
3770	if (*len >= max_cnt)
3771		return 1;
3772
3773	rc = lpfc_idiag_wqs_for_cq(phba, "IO", pbuffer, len,
3774				   max_cnt, qp->queue_id);
3775	if (rc)
3776		return 1;
3777
3778	if ((eqidx < phba->cfg_nvmet_mrq) && phba->nvmet_support) {
3779		/* NVMET CQset */
3780		qp = phba->sli4_hba.nvmet_cqset[eqidx];
3781		*len = __lpfc_idiag_print_cq(qp, "NVMET CQset", pbuffer, *len);
3782
3783		/* Reset max counter */
3784		qp->CQ_max_cqe = 0;
3785
3786		if (*len >= max_cnt)
3787			return 1;
3788
3789		/* RQ header */
3790		qp = phba->sli4_hba.nvmet_mrq_hdr[eqidx];
3791		*len = __lpfc_idiag_print_rqpair(qp,
3792				phba->sli4_hba.nvmet_mrq_data[eqidx],
3793				"NVMET MRQ", pbuffer, *len);
3794
3795		if (*len >= max_cnt)
3796			return 1;
3797	}
3798
3799	return 0;
3800}
3801
3802static int
3803__lpfc_idiag_print_eq(struct lpfc_queue *qp, char *eqtype,
3804			char *pbuffer, int len)
3805{
3806	if (!qp)
3807		return len;
3808
3809	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3810			"\n%s EQ info: EQ-STAT[max:x%x noE:x%x "
3811			"cqe_proc:x%x eqe_proc:x%llx eqd %d]\n",
3812			eqtype, qp->q_cnt_1, qp->q_cnt_2, qp->q_cnt_3,
3813			(unsigned long long)qp->q_cnt_4, qp->q_mode);
3814	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3815			"EQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3816			"HST-IDX[%04d], NTFI[%03d], PLMT[%03d], AFFIN[%03d]",
3817			qp->queue_id, qp->entry_count, qp->entry_size,
3818			qp->host_index, qp->notify_interval,
3819			qp->max_proc_limit, qp->chann);
3820	len +=  scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3821			"\n");
3822
3823	return len;
3824}
3825
3826/**
3827 * lpfc_idiag_queinfo_read - idiag debugfs read queue information
3828 * @file: The file pointer to read from.
3829 * @buf: The buffer to copy the data to.
3830 * @nbytes: The number of bytes to read.
3831 * @ppos: The position in the file to start reading from.
3832 *
3833 * Description:
3834 * This routine reads data from the @phba SLI4 PCI function queue information,
3835 * and copies to user @buf.
3836 * This routine only returns 1 EQs worth of information. It remembers the last
3837 * EQ read and jumps to the next EQ. Thus subsequent calls to queInfo will
3838 * retrieve all EQs allocated for the phba.
3839 *
3840 * Returns:
3841 * This function returns the amount of data that was read (this could be less
3842 * than @nbytes if the end of the file was reached) or a negative error value.
3843 **/
3844static ssize_t
3845lpfc_idiag_queinfo_read(struct file *file, char __user *buf, size_t nbytes,
3846			loff_t *ppos)
3847{
3848	struct lpfc_debug *debug = file->private_data;
3849	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3850	char *pbuffer;
3851	int max_cnt, rc, x, len = 0;
3852	struct lpfc_queue *qp = NULL;
3853
3854	if (!debug->buffer)
3855		debug->buffer = kmalloc(LPFC_QUE_INFO_GET_BUF_SIZE, GFP_KERNEL);
3856	if (!debug->buffer)
3857		return 0;
3858	pbuffer = debug->buffer;
3859	max_cnt = LPFC_QUE_INFO_GET_BUF_SIZE - 256;
3860
3861	if (*ppos)
3862		return 0;
3863
3864	spin_lock_irq(&phba->hbalock);
3865
3866	/* Fast-path event queue */
3867	if (phba->sli4_hba.hdwq && phba->cfg_hdw_queue) {
3868
3869		x = phba->lpfc_idiag_last_eq;
3870		phba->lpfc_idiag_last_eq++;
3871		if (phba->lpfc_idiag_last_eq >= phba->cfg_hdw_queue)
3872			phba->lpfc_idiag_last_eq = 0;
3873
3874		len += scnprintf(pbuffer + len,
3875				 LPFC_QUE_INFO_GET_BUF_SIZE - len,
3876				 "HDWQ %d out of %d HBA HDWQs\n",
3877				 x, phba->cfg_hdw_queue);
3878
3879		/* Fast-path EQ */
3880		qp = phba->sli4_hba.hdwq[x].hba_eq;
3881		if (!qp)
3882			goto out;
3883
3884		len = __lpfc_idiag_print_eq(qp, "HBA", pbuffer, len);
3885
3886		/* Reset max counter */
3887		qp->EQ_max_eqe = 0;
3888
3889		if (len >= max_cnt)
3890			goto too_big;
3891
3892		/* will dump both fcp and nvme cqs/wqs for the eq */
3893		rc = lpfc_idiag_cqs_for_eq(phba, pbuffer, &len,
3894			max_cnt, x, qp->queue_id);
3895		if (rc)
3896			goto too_big;
3897
3898		/* Only EQ 0 has slow path CQs configured */
3899		if (x)
3900			goto out;
3901
3902		/* Slow-path mailbox CQ */
3903		qp = phba->sli4_hba.mbx_cq;
3904		len = __lpfc_idiag_print_cq(qp, "MBX", pbuffer, len);
3905		if (len >= max_cnt)
3906			goto too_big;
3907
3908		/* Slow-path MBOX MQ */
3909		qp = phba->sli4_hba.mbx_wq;
3910		len = __lpfc_idiag_print_wq(qp, "MBX", pbuffer, len);
3911		if (len >= max_cnt)
3912			goto too_big;
3913
3914		/* Slow-path ELS response CQ */
3915		qp = phba->sli4_hba.els_cq;
3916		len = __lpfc_idiag_print_cq(qp, "ELS", pbuffer, len);
3917		/* Reset max counter */
3918		if (qp)
3919			qp->CQ_max_cqe = 0;
3920		if (len >= max_cnt)
3921			goto too_big;
3922
3923		/* Slow-path ELS WQ */
3924		qp = phba->sli4_hba.els_wq;
3925		len = __lpfc_idiag_print_wq(qp, "ELS", pbuffer, len);
3926		if (len >= max_cnt)
3927			goto too_big;
3928
3929		qp = phba->sli4_hba.hdr_rq;
3930		len = __lpfc_idiag_print_rqpair(qp, phba->sli4_hba.dat_rq,
3931						"ELS RQpair", pbuffer, len);
3932		if (len >= max_cnt)
3933			goto too_big;
3934
3935		/* Slow-path NVME LS response CQ */
3936		qp = phba->sli4_hba.nvmels_cq;
3937		len = __lpfc_idiag_print_cq(qp, "NVME LS",
3938						pbuffer, len);
3939		/* Reset max counter */
3940		if (qp)
3941			qp->CQ_max_cqe = 0;
3942		if (len >= max_cnt)
3943			goto too_big;
3944
3945		/* Slow-path NVME LS WQ */
3946		qp = phba->sli4_hba.nvmels_wq;
3947		len = __lpfc_idiag_print_wq(qp, "NVME LS",
3948						pbuffer, len);
3949		if (len >= max_cnt)
3950			goto too_big;
3951
3952		goto out;
3953	}
3954
3955	spin_unlock_irq(&phba->hbalock);
3956	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3957
3958too_big:
3959	len +=  scnprintf(pbuffer + len,
3960		LPFC_QUE_INFO_GET_BUF_SIZE - len, "Truncated ...\n");
3961out:
3962	spin_unlock_irq(&phba->hbalock);
3963	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3964}
3965
3966/**
3967 * lpfc_idiag_que_param_check - queue access command parameter sanity check
3968 * @q: The pointer to queue structure.
3969 * @index: The index into a queue entry.
3970 * @count: The number of queue entries to access.
3971 *
3972 * Description:
3973 * The routine performs sanity check on device queue access method commands.
3974 *
3975 * Returns:
3976 * This function returns -EINVAL when fails the sanity check, otherwise, it
3977 * returns 0.
3978 **/
3979static int
3980lpfc_idiag_que_param_check(struct lpfc_queue *q, int index, int count)
3981{
3982	/* Only support single entry read or browsing */
3983	if ((count != 1) && (count != LPFC_QUE_ACC_BROWSE))
3984		return -EINVAL;
3985	if (index > q->entry_count - 1)
3986		return -EINVAL;
3987	return 0;
3988}
3989
3990/**
3991 * lpfc_idiag_queacc_read_qe - read a single entry from the given queue index
3992 * @pbuffer: The pointer to buffer to copy the read data into.
 
3993 * @pque: The pointer to the queue to be read.
3994 * @index: The index into the queue entry.
3995 *
3996 * Description:
3997 * This routine reads out a single entry from the given queue's index location
3998 * and copies it into the buffer provided.
3999 *
4000 * Returns:
4001 * This function returns 0 when it fails, otherwise, it returns the length of
4002 * the data read into the buffer provided.
4003 **/
4004static int
4005lpfc_idiag_queacc_read_qe(char *pbuffer, int len, struct lpfc_queue *pque,
4006			  uint32_t index)
4007{
4008	int offset, esize;
4009	uint32_t *pentry;
4010
4011	if (!pbuffer || !pque)
4012		return 0;
4013
4014	esize = pque->entry_size;
4015	len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
4016			"QE-INDEX[%04d]:\n", index);
4017
4018	offset = 0;
4019	pentry = lpfc_sli4_qe(pque, index);
4020	while (esize > 0) {
4021		len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
4022				"%08x ", *pentry);
4023		pentry++;
4024		offset += sizeof(uint32_t);
4025		esize -= sizeof(uint32_t);
4026		if (esize > 0 && !(offset % (4 * sizeof(uint32_t))))
4027			len += scnprintf(pbuffer+len,
4028					LPFC_QUE_ACC_BUF_SIZE-len, "\n");
4029	}
4030	len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len, "\n");
4031
4032	return len;
4033}
4034
4035/**
4036 * lpfc_idiag_queacc_read - idiag debugfs read port queue
4037 * @file: The file pointer to read from.
4038 * @buf: The buffer to copy the data to.
4039 * @nbytes: The number of bytes to read.
4040 * @ppos: The position in the file to start reading from.
4041 *
4042 * Description:
4043 * This routine reads data from the @phba device queue memory according to the
4044 * idiag command, and copies to user @buf. Depending on the queue dump read
4045 * command setup, it does either a single queue entry read or browing through
4046 * all entries of the queue.
4047 *
4048 * Returns:
4049 * This function returns the amount of data that was read (this could be less
4050 * than @nbytes if the end of the file was reached) or a negative error value.
4051 **/
4052static ssize_t
4053lpfc_idiag_queacc_read(struct file *file, char __user *buf, size_t nbytes,
4054		       loff_t *ppos)
4055{
4056	struct lpfc_debug *debug = file->private_data;
4057	uint32_t last_index, index, count;
4058	struct lpfc_queue *pque = NULL;
4059	char *pbuffer;
4060	int len = 0;
4061
4062	/* This is a user read operation */
4063	debug->op = LPFC_IDIAG_OP_RD;
4064
4065	if (!debug->buffer)
4066		debug->buffer = kmalloc(LPFC_QUE_ACC_BUF_SIZE, GFP_KERNEL);
4067	if (!debug->buffer)
4068		return 0;
4069	pbuffer = debug->buffer;
4070
4071	if (*ppos)
4072		return 0;
4073
4074	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
4075		index = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
4076		count = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
4077		pque = (struct lpfc_queue *)idiag.ptr_private;
4078	} else
4079		return 0;
4080
4081	/* Browse the queue starting from index */
4082	if (count == LPFC_QUE_ACC_BROWSE)
4083		goto que_browse;
4084
4085	/* Read a single entry from the queue */
4086	len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
4087
4088	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4089
4090que_browse:
4091
4092	/* Browse all entries from the queue */
4093	last_index = idiag.offset.last_rd;
4094	index = last_index;
4095
4096	while (len < LPFC_QUE_ACC_SIZE - pque->entry_size) {
4097		len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
4098		index++;
4099		if (index > pque->entry_count - 1)
4100			break;
4101	}
4102
4103	/* Set up the offset for next portion of pci cfg read */
4104	if (index > pque->entry_count - 1)
4105		index = 0;
4106	idiag.offset.last_rd = index;
4107
4108	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4109}
4110
4111/**
4112 * lpfc_idiag_queacc_write - Syntax check and set up idiag queacc commands
4113 * @file: The file pointer to read from.
4114 * @buf: The buffer to copy the user data from.
4115 * @nbytes: The number of bytes to get.
4116 * @ppos: The position in the file to start reading from.
4117 *
4118 * This routine get the debugfs idiag command struct from user space and then
4119 * perform the syntax check for port queue read (dump) or write (set) command
4120 * accordingly. In the case of port queue read command, it sets up the command
4121 * in the idiag command struct for the following debugfs read operation. In
4122 * the case of port queue write operation, it executes the write operation
4123 * into the port queue entry accordingly.
4124 *
4125 * It returns the @nbytges passing in from debugfs user space when successful.
4126 * In case of error conditions, it returns proper error code back to the user
4127 * space.
4128 **/
4129static ssize_t
4130lpfc_idiag_queacc_write(struct file *file, const char __user *buf,
4131			size_t nbytes, loff_t *ppos)
4132{
4133	struct lpfc_debug *debug = file->private_data;
4134	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4135	uint32_t qidx, quetp, queid, index, count, offset, value;
4136	uint32_t *pentry;
4137	struct lpfc_queue *pque, *qp;
4138	int rc;
4139
4140	/* This is a user write operation */
4141	debug->op = LPFC_IDIAG_OP_WR;
4142
4143	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4144	if (rc < 0)
4145		return rc;
4146
4147	/* Get and sanity check on command feilds */
4148	quetp  = idiag.cmd.data[IDIAG_QUEACC_QUETP_INDX];
4149	queid  = idiag.cmd.data[IDIAG_QUEACC_QUEID_INDX];
4150	index  = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
4151	count  = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
4152	offset = idiag.cmd.data[IDIAG_QUEACC_OFFST_INDX];
4153	value  = idiag.cmd.data[IDIAG_QUEACC_VALUE_INDX];
4154
4155	/* Sanity check on command line arguments */
4156	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
4157	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
4158	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
4159		if (rc != LPFC_QUE_ACC_WR_CMD_ARG)
4160			goto error_out;
4161		if (count != 1)
4162			goto error_out;
4163	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
4164		if (rc != LPFC_QUE_ACC_RD_CMD_ARG)
4165			goto error_out;
4166	} else
4167		goto error_out;
4168
4169	switch (quetp) {
4170	case LPFC_IDIAG_EQ:
4171		/* HBA event queue */
4172		if (phba->sli4_hba.hdwq) {
4173			for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
4174				qp = phba->sli4_hba.hdwq[qidx].hba_eq;
4175				if (qp && qp->queue_id == queid) {
4176					/* Sanity check */
4177					rc = lpfc_idiag_que_param_check(qp,
4178						index, count);
4179					if (rc)
4180						goto error_out;
4181					idiag.ptr_private = qp;
4182					goto pass_check;
4183				}
4184			}
4185		}
4186		goto error_out;
4187		break;
4188	case LPFC_IDIAG_CQ:
4189		/* MBX complete queue */
4190		if (phba->sli4_hba.mbx_cq &&
4191		    phba->sli4_hba.mbx_cq->queue_id == queid) {
4192			/* Sanity check */
4193			rc = lpfc_idiag_que_param_check(
4194					phba->sli4_hba.mbx_cq, index, count);
4195			if (rc)
4196				goto error_out;
4197			idiag.ptr_private = phba->sli4_hba.mbx_cq;
4198			goto pass_check;
4199		}
4200		/* ELS complete queue */
4201		if (phba->sli4_hba.els_cq &&
4202		    phba->sli4_hba.els_cq->queue_id == queid) {
4203			/* Sanity check */
4204			rc = lpfc_idiag_que_param_check(
4205					phba->sli4_hba.els_cq, index, count);
4206			if (rc)
4207				goto error_out;
4208			idiag.ptr_private = phba->sli4_hba.els_cq;
4209			goto pass_check;
4210		}
4211		/* NVME LS complete queue */
4212		if (phba->sli4_hba.nvmels_cq &&
4213		    phba->sli4_hba.nvmels_cq->queue_id == queid) {
4214			/* Sanity check */
4215			rc = lpfc_idiag_que_param_check(
4216					phba->sli4_hba.nvmels_cq, index, count);
4217			if (rc)
4218				goto error_out;
4219			idiag.ptr_private = phba->sli4_hba.nvmels_cq;
4220			goto pass_check;
4221		}
4222		/* FCP complete queue */
4223		if (phba->sli4_hba.hdwq) {
4224			for (qidx = 0; qidx < phba->cfg_hdw_queue;
4225								qidx++) {
4226				qp = phba->sli4_hba.hdwq[qidx].io_cq;
4227				if (qp && qp->queue_id == queid) {
4228					/* Sanity check */
4229					rc = lpfc_idiag_que_param_check(
4230						qp, index, count);
4231					if (rc)
4232						goto error_out;
4233					idiag.ptr_private = qp;
4234					goto pass_check;
4235				}
4236			}
4237		}
4238		goto error_out;
4239		break;
4240	case LPFC_IDIAG_MQ:
4241		/* MBX work queue */
4242		if (phba->sli4_hba.mbx_wq &&
4243		    phba->sli4_hba.mbx_wq->queue_id == queid) {
4244			/* Sanity check */
4245			rc = lpfc_idiag_que_param_check(
4246					phba->sli4_hba.mbx_wq, index, count);
4247			if (rc)
4248				goto error_out;
4249			idiag.ptr_private = phba->sli4_hba.mbx_wq;
4250			goto pass_check;
4251		}
4252		goto error_out;
4253		break;
4254	case LPFC_IDIAG_WQ:
4255		/* ELS work queue */
4256		if (phba->sli4_hba.els_wq &&
4257		    phba->sli4_hba.els_wq->queue_id == queid) {
4258			/* Sanity check */
4259			rc = lpfc_idiag_que_param_check(
4260					phba->sli4_hba.els_wq, index, count);
4261			if (rc)
4262				goto error_out;
4263			idiag.ptr_private = phba->sli4_hba.els_wq;
4264			goto pass_check;
4265		}
4266		/* NVME LS work queue */
4267		if (phba->sli4_hba.nvmels_wq &&
4268		    phba->sli4_hba.nvmels_wq->queue_id == queid) {
4269			/* Sanity check */
4270			rc = lpfc_idiag_que_param_check(
4271					phba->sli4_hba.nvmels_wq, index, count);
4272			if (rc)
4273				goto error_out;
4274			idiag.ptr_private = phba->sli4_hba.nvmels_wq;
4275			goto pass_check;
4276		}
4277
4278		if (phba->sli4_hba.hdwq) {
4279			/* FCP/SCSI work queue */
4280			for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
4281				qp = phba->sli4_hba.hdwq[qidx].io_wq;
4282				if (qp && qp->queue_id == queid) {
4283					/* Sanity check */
4284					rc = lpfc_idiag_que_param_check(
4285						qp, index, count);
4286					if (rc)
4287						goto error_out;
4288					idiag.ptr_private = qp;
4289					goto pass_check;
4290				}
4291			}
4292		}
 
4293
4294		goto error_out;
4295		break;
4296	case LPFC_IDIAG_RQ:
4297		/* HDR queue */
4298		if (phba->sli4_hba.hdr_rq &&
4299		    phba->sli4_hba.hdr_rq->queue_id == queid) {
4300			/* Sanity check */
4301			rc = lpfc_idiag_que_param_check(
4302					phba->sli4_hba.hdr_rq, index, count);
4303			if (rc)
4304				goto error_out;
4305			idiag.ptr_private = phba->sli4_hba.hdr_rq;
4306			goto pass_check;
4307		}
4308		/* DAT queue */
4309		if (phba->sli4_hba.dat_rq &&
4310		    phba->sli4_hba.dat_rq->queue_id == queid) {
4311			/* Sanity check */
4312			rc = lpfc_idiag_que_param_check(
4313					phba->sli4_hba.dat_rq, index, count);
4314			if (rc)
4315				goto error_out;
4316			idiag.ptr_private = phba->sli4_hba.dat_rq;
4317			goto pass_check;
4318		}
4319		goto error_out;
4320		break;
4321	default:
4322		goto error_out;
4323		break;
4324	}
4325
4326pass_check:
4327
4328	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
4329		if (count == LPFC_QUE_ACC_BROWSE)
4330			idiag.offset.last_rd = index;
4331	}
4332
4333	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
4334	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
4335	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
4336		/* Additional sanity checks on write operation */
4337		pque = (struct lpfc_queue *)idiag.ptr_private;
4338		if (offset > pque->entry_size/sizeof(uint32_t) - 1)
4339			goto error_out;
4340		pentry = lpfc_sli4_qe(pque, index);
4341		pentry += offset;
4342		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR)
4343			*pentry = value;
4344		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST)
4345			*pentry |= value;
4346		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL)
4347			*pentry &= ~value;
4348	}
4349	return nbytes;
4350
4351error_out:
4352	/* Clean out command structure on command error out */
4353	memset(&idiag, 0, sizeof(idiag));
4354	return -EINVAL;
4355}
4356
4357/**
4358 * lpfc_idiag_drbacc_read_reg - idiag debugfs read a doorbell register
4359 * @phba: The pointer to hba structure.
4360 * @pbuffer: The pointer to the buffer to copy the data to.
4361 * @len: The length of bytes to copied.
4362 * @drbregid: The id to doorbell registers.
4363 *
4364 * Description:
4365 * This routine reads a doorbell register and copies its content to the
4366 * user buffer pointed to by @pbuffer.
4367 *
4368 * Returns:
4369 * This function returns the amount of data that was copied into @pbuffer.
4370 **/
4371static int
4372lpfc_idiag_drbacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
4373			   int len, uint32_t drbregid)
4374{
4375
4376	if (!pbuffer)
4377		return 0;
4378
4379	switch (drbregid) {
4380	case LPFC_DRB_EQ:
4381		len += scnprintf(pbuffer + len, LPFC_DRB_ACC_BUF_SIZE-len,
4382				"EQ-DRB-REG: 0x%08x\n",
4383				readl(phba->sli4_hba.EQDBregaddr));
4384		break;
4385	case LPFC_DRB_CQ:
4386		len += scnprintf(pbuffer + len, LPFC_DRB_ACC_BUF_SIZE - len,
4387				"CQ-DRB-REG: 0x%08x\n",
4388				readl(phba->sli4_hba.CQDBregaddr));
4389		break;
4390	case LPFC_DRB_MQ:
4391		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4392				"MQ-DRB-REG:   0x%08x\n",
4393				readl(phba->sli4_hba.MQDBregaddr));
4394		break;
4395	case LPFC_DRB_WQ:
4396		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4397				"WQ-DRB-REG:   0x%08x\n",
4398				readl(phba->sli4_hba.WQDBregaddr));
4399		break;
4400	case LPFC_DRB_RQ:
4401		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4402				"RQ-DRB-REG:   0x%08x\n",
4403				readl(phba->sli4_hba.RQDBregaddr));
4404		break;
4405	default:
4406		break;
4407	}
4408
4409	return len;
4410}
4411
4412/**
4413 * lpfc_idiag_drbacc_read - idiag debugfs read port doorbell
4414 * @file: The file pointer to read from.
4415 * @buf: The buffer to copy the data to.
4416 * @nbytes: The number of bytes to read.
4417 * @ppos: The position in the file to start reading from.
4418 *
4419 * Description:
4420 * This routine reads data from the @phba device doorbell register according
4421 * to the idiag command, and copies to user @buf. Depending on the doorbell
4422 * register read command setup, it does either a single doorbell register
4423 * read or dump all doorbell registers.
4424 *
4425 * Returns:
4426 * This function returns the amount of data that was read (this could be less
4427 * than @nbytes if the end of the file was reached) or a negative error value.
4428 **/
4429static ssize_t
4430lpfc_idiag_drbacc_read(struct file *file, char __user *buf, size_t nbytes,
4431		       loff_t *ppos)
4432{
4433	struct lpfc_debug *debug = file->private_data;
4434	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4435	uint32_t drb_reg_id, i;
4436	char *pbuffer;
4437	int len = 0;
4438
4439	/* This is a user read operation */
4440	debug->op = LPFC_IDIAG_OP_RD;
4441
4442	if (!debug->buffer)
4443		debug->buffer = kmalloc(LPFC_DRB_ACC_BUF_SIZE, GFP_KERNEL);
4444	if (!debug->buffer)
4445		return 0;
4446	pbuffer = debug->buffer;
4447
4448	if (*ppos)
4449		return 0;
4450
4451	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD)
4452		drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
4453	else
4454		return 0;
4455
4456	if (drb_reg_id == LPFC_DRB_ACC_ALL)
4457		for (i = 1; i <= LPFC_DRB_MAX; i++)
4458			len = lpfc_idiag_drbacc_read_reg(phba,
4459							 pbuffer, len, i);
4460	else
4461		len = lpfc_idiag_drbacc_read_reg(phba,
4462						 pbuffer, len, drb_reg_id);
4463
4464	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4465}
4466
4467/**
4468 * lpfc_idiag_drbacc_write - Syntax check and set up idiag drbacc commands
4469 * @file: The file pointer to read from.
4470 * @buf: The buffer to copy the user data from.
4471 * @nbytes: The number of bytes to get.
4472 * @ppos: The position in the file to start reading from.
4473 *
4474 * This routine get the debugfs idiag command struct from user space and then
4475 * perform the syntax check for port doorbell register read (dump) or write
4476 * (set) command accordingly. In the case of port queue read command, it sets
4477 * up the command in the idiag command struct for the following debugfs read
4478 * operation. In the case of port doorbell register write operation, it
4479 * executes the write operation into the port doorbell register accordingly.
4480 *
4481 * It returns the @nbytges passing in from debugfs user space when successful.
4482 * In case of error conditions, it returns proper error code back to the user
4483 * space.
4484 **/
4485static ssize_t
4486lpfc_idiag_drbacc_write(struct file *file, const char __user *buf,
4487			size_t nbytes, loff_t *ppos)
4488{
4489	struct lpfc_debug *debug = file->private_data;
4490	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4491	uint32_t drb_reg_id, value, reg_val = 0;
4492	void __iomem *drb_reg;
4493	int rc;
4494
4495	/* This is a user write operation */
4496	debug->op = LPFC_IDIAG_OP_WR;
4497
4498	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4499	if (rc < 0)
4500		return rc;
4501
4502	/* Sanity check on command line arguments */
4503	drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
4504	value = idiag.cmd.data[IDIAG_DRBACC_VALUE_INDX];
4505
4506	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
4507	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
4508	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4509		if (rc != LPFC_DRB_ACC_WR_CMD_ARG)
4510			goto error_out;
4511		if (drb_reg_id > LPFC_DRB_MAX)
4512			goto error_out;
4513	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD) {
4514		if (rc != LPFC_DRB_ACC_RD_CMD_ARG)
4515			goto error_out;
4516		if ((drb_reg_id > LPFC_DRB_MAX) &&
4517		    (drb_reg_id != LPFC_DRB_ACC_ALL))
4518			goto error_out;
4519	} else
4520		goto error_out;
4521
4522	/* Perform the write access operation */
4523	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
4524	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
4525	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4526		switch (drb_reg_id) {
4527		case LPFC_DRB_EQ:
4528			drb_reg = phba->sli4_hba.EQDBregaddr;
4529			break;
4530		case LPFC_DRB_CQ:
4531			drb_reg = phba->sli4_hba.CQDBregaddr;
4532			break;
4533		case LPFC_DRB_MQ:
4534			drb_reg = phba->sli4_hba.MQDBregaddr;
4535			break;
4536		case LPFC_DRB_WQ:
4537			drb_reg = phba->sli4_hba.WQDBregaddr;
4538			break;
4539		case LPFC_DRB_RQ:
4540			drb_reg = phba->sli4_hba.RQDBregaddr;
4541			break;
4542		default:
4543			goto error_out;
4544		}
4545
4546		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR)
4547			reg_val = value;
4548		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST) {
4549			reg_val = readl(drb_reg);
4550			reg_val |= value;
4551		}
4552		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4553			reg_val = readl(drb_reg);
4554			reg_val &= ~value;
4555		}
4556		writel(reg_val, drb_reg);
4557		readl(drb_reg); /* flush */
4558	}
4559	return nbytes;
4560
4561error_out:
4562	/* Clean out command structure on command error out */
4563	memset(&idiag, 0, sizeof(idiag));
4564	return -EINVAL;
4565}
4566
4567/**
4568 * lpfc_idiag_ctlacc_read_reg - idiag debugfs read a control registers
4569 * @phba: The pointer to hba structure.
4570 * @pbuffer: The pointer to the buffer to copy the data to.
4571 * @len: The length of bytes to copied.
4572 * @drbregid: The id to doorbell registers.
4573 *
4574 * Description:
4575 * This routine reads a control register and copies its content to the
4576 * user buffer pointed to by @pbuffer.
4577 *
4578 * Returns:
4579 * This function returns the amount of data that was copied into @pbuffer.
4580 **/
4581static int
4582lpfc_idiag_ctlacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
4583			   int len, uint32_t ctlregid)
4584{
4585
4586	if (!pbuffer)
4587		return 0;
4588
4589	switch (ctlregid) {
4590	case LPFC_CTL_PORT_SEM:
4591		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4592				"Port SemReg:   0x%08x\n",
4593				readl(phba->sli4_hba.conf_regs_memmap_p +
4594				      LPFC_CTL_PORT_SEM_OFFSET));
4595		break;
4596	case LPFC_CTL_PORT_STA:
4597		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4598				"Port StaReg:   0x%08x\n",
4599				readl(phba->sli4_hba.conf_regs_memmap_p +
4600				      LPFC_CTL_PORT_STA_OFFSET));
4601		break;
4602	case LPFC_CTL_PORT_CTL:
4603		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4604				"Port CtlReg:   0x%08x\n",
4605				readl(phba->sli4_hba.conf_regs_memmap_p +
4606				      LPFC_CTL_PORT_CTL_OFFSET));
4607		break;
4608	case LPFC_CTL_PORT_ER1:
4609		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4610				"Port Er1Reg:   0x%08x\n",
4611				readl(phba->sli4_hba.conf_regs_memmap_p +
4612				      LPFC_CTL_PORT_ER1_OFFSET));
4613		break;
4614	case LPFC_CTL_PORT_ER2:
4615		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4616				"Port Er2Reg:   0x%08x\n",
4617				readl(phba->sli4_hba.conf_regs_memmap_p +
4618				      LPFC_CTL_PORT_ER2_OFFSET));
4619		break;
4620	case LPFC_CTL_PDEV_CTL:
4621		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4622				"PDev CtlReg:   0x%08x\n",
4623				readl(phba->sli4_hba.conf_regs_memmap_p +
4624				      LPFC_CTL_PDEV_CTL_OFFSET));
4625		break;
4626	default:
4627		break;
4628	}
4629	return len;
4630}
4631
4632/**
4633 * lpfc_idiag_ctlacc_read - idiag debugfs read port and device control register
4634 * @file: The file pointer to read from.
4635 * @buf: The buffer to copy the data to.
4636 * @nbytes: The number of bytes to read.
4637 * @ppos: The position in the file to start reading from.
4638 *
4639 * Description:
4640 * This routine reads data from the @phba port and device registers according
4641 * to the idiag command, and copies to user @buf.
4642 *
4643 * Returns:
4644 * This function returns the amount of data that was read (this could be less
4645 * than @nbytes if the end of the file was reached) or a negative error value.
4646 **/
4647static ssize_t
4648lpfc_idiag_ctlacc_read(struct file *file, char __user *buf, size_t nbytes,
4649		       loff_t *ppos)
4650{
4651	struct lpfc_debug *debug = file->private_data;
4652	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4653	uint32_t ctl_reg_id, i;
4654	char *pbuffer;
4655	int len = 0;
4656
4657	/* This is a user read operation */
4658	debug->op = LPFC_IDIAG_OP_RD;
4659
4660	if (!debug->buffer)
4661		debug->buffer = kmalloc(LPFC_CTL_ACC_BUF_SIZE, GFP_KERNEL);
4662	if (!debug->buffer)
4663		return 0;
4664	pbuffer = debug->buffer;
4665
4666	if (*ppos)
4667		return 0;
4668
4669	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD)
4670		ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
4671	else
4672		return 0;
4673
4674	if (ctl_reg_id == LPFC_CTL_ACC_ALL)
4675		for (i = 1; i <= LPFC_CTL_MAX; i++)
4676			len = lpfc_idiag_ctlacc_read_reg(phba,
4677							 pbuffer, len, i);
4678	else
4679		len = lpfc_idiag_ctlacc_read_reg(phba,
4680						 pbuffer, len, ctl_reg_id);
4681
4682	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4683}
4684
4685/**
4686 * lpfc_idiag_ctlacc_write - Syntax check and set up idiag ctlacc commands
4687 * @file: The file pointer to read from.
4688 * @buf: The buffer to copy the user data from.
4689 * @nbytes: The number of bytes to get.
4690 * @ppos: The position in the file to start reading from.
4691 *
4692 * This routine get the debugfs idiag command struct from user space and then
4693 * perform the syntax check for port and device control register read (dump)
4694 * or write (set) command accordingly.
4695 *
4696 * It returns the @nbytges passing in from debugfs user space when successful.
4697 * In case of error conditions, it returns proper error code back to the user
4698 * space.
4699 **/
4700static ssize_t
4701lpfc_idiag_ctlacc_write(struct file *file, const char __user *buf,
4702			size_t nbytes, loff_t *ppos)
4703{
4704	struct lpfc_debug *debug = file->private_data;
4705	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4706	uint32_t ctl_reg_id, value, reg_val = 0;
4707	void __iomem *ctl_reg;
4708	int rc;
4709
4710	/* This is a user write operation */
4711	debug->op = LPFC_IDIAG_OP_WR;
4712
4713	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4714	if (rc < 0)
4715		return rc;
4716
4717	/* Sanity check on command line arguments */
4718	ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
4719	value = idiag.cmd.data[IDIAG_CTLACC_VALUE_INDX];
4720
4721	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
4722	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
4723	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4724		if (rc != LPFC_CTL_ACC_WR_CMD_ARG)
4725			goto error_out;
4726		if (ctl_reg_id > LPFC_CTL_MAX)
4727			goto error_out;
4728	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD) {
4729		if (rc != LPFC_CTL_ACC_RD_CMD_ARG)
4730			goto error_out;
4731		if ((ctl_reg_id > LPFC_CTL_MAX) &&
4732		    (ctl_reg_id != LPFC_CTL_ACC_ALL))
4733			goto error_out;
4734	} else
4735		goto error_out;
4736
4737	/* Perform the write access operation */
4738	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
4739	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
4740	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4741		switch (ctl_reg_id) {
4742		case LPFC_CTL_PORT_SEM:
4743			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4744					LPFC_CTL_PORT_SEM_OFFSET;
4745			break;
4746		case LPFC_CTL_PORT_STA:
4747			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4748					LPFC_CTL_PORT_STA_OFFSET;
4749			break;
4750		case LPFC_CTL_PORT_CTL:
4751			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4752					LPFC_CTL_PORT_CTL_OFFSET;
4753			break;
4754		case LPFC_CTL_PORT_ER1:
4755			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4756					LPFC_CTL_PORT_ER1_OFFSET;
4757			break;
4758		case LPFC_CTL_PORT_ER2:
4759			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4760					LPFC_CTL_PORT_ER2_OFFSET;
4761			break;
4762		case LPFC_CTL_PDEV_CTL:
4763			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4764					LPFC_CTL_PDEV_CTL_OFFSET;
4765			break;
4766		default:
4767			goto error_out;
4768		}
4769
4770		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR)
4771			reg_val = value;
4772		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST) {
4773			reg_val = readl(ctl_reg);
4774			reg_val |= value;
4775		}
4776		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4777			reg_val = readl(ctl_reg);
4778			reg_val &= ~value;
4779		}
4780		writel(reg_val, ctl_reg);
4781		readl(ctl_reg); /* flush */
4782	}
4783	return nbytes;
4784
4785error_out:
4786	/* Clean out command structure on command error out */
4787	memset(&idiag, 0, sizeof(idiag));
4788	return -EINVAL;
4789}
4790
4791/**
4792 * lpfc_idiag_mbxacc_get_setup - idiag debugfs get mailbox access setup
4793 * @phba: Pointer to HBA context object.
4794 * @pbuffer: Pointer to data buffer.
4795 *
4796 * Description:
4797 * This routine gets the driver mailbox access debugfs setup information.
4798 *
4799 * Returns:
4800 * This function returns the amount of data that was read (this could be less
4801 * than @nbytes if the end of the file was reached) or a negative error value.
4802 **/
4803static int
4804lpfc_idiag_mbxacc_get_setup(struct lpfc_hba *phba, char *pbuffer)
4805{
4806	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
4807	int len = 0;
4808
4809	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
4810	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
4811	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
4812	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
4813
4814	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
4815			"mbx_dump_map: 0x%08x\n", mbx_dump_map);
4816	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
4817			"mbx_dump_cnt: %04d\n", mbx_dump_cnt);
4818	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
4819			"mbx_word_cnt: %04d\n", mbx_word_cnt);
4820	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
4821			"mbx_mbox_cmd: 0x%02x\n", mbx_mbox_cmd);
4822
4823	return len;
4824}
4825
4826/**
4827 * lpfc_idiag_mbxacc_read - idiag debugfs read on mailbox access
4828 * @file: The file pointer to read from.
4829 * @buf: The buffer to copy the data to.
4830 * @nbytes: The number of bytes to read.
4831 * @ppos: The position in the file to start reading from.
4832 *
4833 * Description:
4834 * This routine reads data from the @phba driver mailbox access debugfs setup
4835 * information.
4836 *
4837 * Returns:
4838 * This function returns the amount of data that was read (this could be less
4839 * than @nbytes if the end of the file was reached) or a negative error value.
4840 **/
4841static ssize_t
4842lpfc_idiag_mbxacc_read(struct file *file, char __user *buf, size_t nbytes,
4843		       loff_t *ppos)
4844{
4845	struct lpfc_debug *debug = file->private_data;
4846	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4847	char *pbuffer;
4848	int len = 0;
4849
4850	/* This is a user read operation */
4851	debug->op = LPFC_IDIAG_OP_RD;
4852
4853	if (!debug->buffer)
4854		debug->buffer = kmalloc(LPFC_MBX_ACC_BUF_SIZE, GFP_KERNEL);
4855	if (!debug->buffer)
4856		return 0;
4857	pbuffer = debug->buffer;
4858
4859	if (*ppos)
4860		return 0;
4861
4862	if ((idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP) &&
4863	    (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP))
4864		return 0;
4865
4866	len = lpfc_idiag_mbxacc_get_setup(phba, pbuffer);
4867
4868	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4869}
4870
4871/**
4872 * lpfc_idiag_mbxacc_write - Syntax check and set up idiag mbxacc commands
4873 * @file: The file pointer to read from.
4874 * @buf: The buffer to copy the user data from.
4875 * @nbytes: The number of bytes to get.
4876 * @ppos: The position in the file to start reading from.
4877 *
4878 * This routine get the debugfs idiag command struct from user space and then
4879 * perform the syntax check for driver mailbox command (dump) and sets up the
4880 * necessary states in the idiag command struct accordingly.
4881 *
4882 * It returns the @nbytges passing in from debugfs user space when successful.
4883 * In case of error conditions, it returns proper error code back to the user
4884 * space.
4885 **/
4886static ssize_t
4887lpfc_idiag_mbxacc_write(struct file *file, const char __user *buf,
4888			size_t nbytes, loff_t *ppos)
4889{
4890	struct lpfc_debug *debug = file->private_data;
4891	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
4892	int rc;
4893
4894	/* This is a user write operation */
4895	debug->op = LPFC_IDIAG_OP_WR;
4896
4897	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4898	if (rc < 0)
4899		return rc;
4900
4901	/* Sanity check on command line arguments */
4902	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
4903	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
4904	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
4905	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
4906
4907	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_MBXACC_DP) {
4908		if (!(mbx_dump_map & LPFC_MBX_DMP_MBX_ALL))
4909			goto error_out;
4910		if ((mbx_dump_map & ~LPFC_MBX_DMP_MBX_ALL) &&
4911		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
4912			goto error_out;
4913		if (mbx_word_cnt > sizeof(MAILBOX_t))
4914			goto error_out;
4915	} else if (idiag.cmd.opcode == LPFC_IDIAG_BSG_MBXACC_DP) {
4916		if (!(mbx_dump_map & LPFC_BSG_DMP_MBX_ALL))
4917			goto error_out;
4918		if ((mbx_dump_map & ~LPFC_BSG_DMP_MBX_ALL) &&
4919		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
4920			goto error_out;
4921		if (mbx_word_cnt > (BSG_MBOX_SIZE)/4)
4922			goto error_out;
4923		if (mbx_mbox_cmd != 0x9b)
4924			goto error_out;
4925	} else
4926		goto error_out;
4927
4928	if (mbx_word_cnt == 0)
4929		goto error_out;
4930	if (rc != LPFC_MBX_DMP_ARG)
4931		goto error_out;
4932	if (mbx_mbox_cmd & ~0xff)
4933		goto error_out;
4934
4935	/* condition for stop mailbox dump */
4936	if (mbx_dump_cnt == 0)
4937		goto reset_out;
4938
4939	return nbytes;
4940
4941reset_out:
4942	/* Clean out command structure on command error out */
4943	memset(&idiag, 0, sizeof(idiag));
4944	return nbytes;
4945
4946error_out:
4947	/* Clean out command structure on command error out */
4948	memset(&idiag, 0, sizeof(idiag));
4949	return -EINVAL;
4950}
4951
4952/**
4953 * lpfc_idiag_extacc_avail_get - get the available extents information
4954 * @phba: pointer to lpfc hba data structure.
4955 * @pbuffer: pointer to internal buffer.
4956 * @len: length into the internal buffer data has been copied.
4957 *
4958 * Description:
4959 * This routine is to get the available extent information.
4960 *
4961 * Returns:
4962 * overall lenth of the data read into the internal buffer.
4963 **/
4964static int
4965lpfc_idiag_extacc_avail_get(struct lpfc_hba *phba, char *pbuffer, int len)
4966{
4967	uint16_t ext_cnt, ext_size;
4968
4969	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4970			"\nAvailable Extents Information:\n");
4971
4972	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4973			"\tPort Available VPI extents: ");
4974	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VPI,
4975				       &ext_cnt, &ext_size);
4976	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4977			"Count %3d, Size %3d\n", ext_cnt, ext_size);
4978
4979	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4980			"\tPort Available VFI extents: ");
4981	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VFI,
4982				       &ext_cnt, &ext_size);
4983	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4984			"Count %3d, Size %3d\n", ext_cnt, ext_size);
4985
4986	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4987			"\tPort Available RPI extents: ");
4988	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_RPI,
4989				       &ext_cnt, &ext_size);
4990	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4991			"Count %3d, Size %3d\n", ext_cnt, ext_size);
4992
4993	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4994			"\tPort Available XRI extents: ");
4995	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_XRI,
4996				       &ext_cnt, &ext_size);
4997	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4998			"Count %3d, Size %3d\n", ext_cnt, ext_size);
4999
5000	return len;
5001}
5002
5003/**
5004 * lpfc_idiag_extacc_alloc_get - get the allocated extents information
5005 * @phba: pointer to lpfc hba data structure.
5006 * @pbuffer: pointer to internal buffer.
5007 * @len: length into the internal buffer data has been copied.
5008 *
5009 * Description:
5010 * This routine is to get the allocated extent information.
5011 *
5012 * Returns:
5013 * overall lenth of the data read into the internal buffer.
5014 **/
5015static int
5016lpfc_idiag_extacc_alloc_get(struct lpfc_hba *phba, char *pbuffer, int len)
5017{
5018	uint16_t ext_cnt, ext_size;
5019	int rc;
5020
5021	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5022			"\nAllocated Extents Information:\n");
5023
5024	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5025			"\tHost Allocated VPI extents: ");
5026	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VPI,
5027					    &ext_cnt, &ext_size);
5028	if (!rc)
5029		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5030				"Port %d Extent %3d, Size %3d\n",
5031				phba->brd_no, ext_cnt, ext_size);
5032	else
5033		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5034				"N/A\n");
5035
5036	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5037			"\tHost Allocated VFI extents: ");
5038	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VFI,
5039					    &ext_cnt, &ext_size);
5040	if (!rc)
5041		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5042				"Port %d Extent %3d, Size %3d\n",
5043				phba->brd_no, ext_cnt, ext_size);
5044	else
5045		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5046				"N/A\n");
5047
5048	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5049			"\tHost Allocated RPI extents: ");
5050	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_RPI,
5051					    &ext_cnt, &ext_size);
5052	if (!rc)
5053		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5054				"Port %d Extent %3d, Size %3d\n",
5055				phba->brd_no, ext_cnt, ext_size);
5056	else
5057		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5058				"N/A\n");
5059
5060	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5061			"\tHost Allocated XRI extents: ");
5062	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_XRI,
5063					    &ext_cnt, &ext_size);
5064	if (!rc)
5065		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5066				"Port %d Extent %3d, Size %3d\n",
5067				phba->brd_no, ext_cnt, ext_size);
5068	else
5069		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5070				"N/A\n");
5071
5072	return len;
5073}
5074
5075/**
5076 * lpfc_idiag_extacc_drivr_get - get driver extent information
5077 * @phba: pointer to lpfc hba data structure.
5078 * @pbuffer: pointer to internal buffer.
5079 * @len: length into the internal buffer data has been copied.
5080 *
5081 * Description:
5082 * This routine is to get the driver extent information.
5083 *
5084 * Returns:
5085 * overall lenth of the data read into the internal buffer.
5086 **/
5087static int
5088lpfc_idiag_extacc_drivr_get(struct lpfc_hba *phba, char *pbuffer, int len)
5089{
5090	struct lpfc_rsrc_blks *rsrc_blks;
5091	int index;
5092
5093	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5094			"\nDriver Extents Information:\n");
5095
5096	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5097			"\tVPI extents:\n");
5098	index = 0;
5099	list_for_each_entry(rsrc_blks, &phba->lpfc_vpi_blk_list, list) {
5100		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5101				"\t\tBlock %3d: Start %4d, Count %4d\n",
5102				index, rsrc_blks->rsrc_start,
5103				rsrc_blks->rsrc_size);
5104		index++;
5105	}
5106	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5107			"\tVFI extents:\n");
5108	index = 0;
5109	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_vfi_blk_list,
5110			    list) {
5111		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5112				"\t\tBlock %3d: Start %4d, Count %4d\n",
5113				index, rsrc_blks->rsrc_start,
5114				rsrc_blks->rsrc_size);
5115		index++;
5116	}
5117
5118	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5119			"\tRPI extents:\n");
5120	index = 0;
5121	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_rpi_blk_list,
5122			    list) {
5123		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5124				"\t\tBlock %3d: Start %4d, Count %4d\n",
5125				index, rsrc_blks->rsrc_start,
5126				rsrc_blks->rsrc_size);
5127		index++;
5128	}
5129
5130	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5131			"\tXRI extents:\n");
5132	index = 0;
5133	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_xri_blk_list,
5134			    list) {
5135		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5136				"\t\tBlock %3d: Start %4d, Count %4d\n",
5137				index, rsrc_blks->rsrc_start,
5138				rsrc_blks->rsrc_size);
5139		index++;
5140	}
5141
5142	return len;
5143}
5144
5145/**
5146 * lpfc_idiag_extacc_write - Syntax check and set up idiag extacc commands
5147 * @file: The file pointer to read from.
5148 * @buf: The buffer to copy the user data from.
5149 * @nbytes: The number of bytes to get.
5150 * @ppos: The position in the file to start reading from.
5151 *
5152 * This routine get the debugfs idiag command struct from user space and then
5153 * perform the syntax check for extent information access commands and sets
5154 * up the necessary states in the idiag command struct accordingly.
5155 *
5156 * It returns the @nbytges passing in from debugfs user space when successful.
5157 * In case of error conditions, it returns proper error code back to the user
5158 * space.
5159 **/
5160static ssize_t
5161lpfc_idiag_extacc_write(struct file *file, const char __user *buf,
5162			size_t nbytes, loff_t *ppos)
5163{
5164	struct lpfc_debug *debug = file->private_data;
5165	uint32_t ext_map;
5166	int rc;
5167
5168	/* This is a user write operation */
5169	debug->op = LPFC_IDIAG_OP_WR;
5170
5171	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
5172	if (rc < 0)
5173		return rc;
5174
5175	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
5176
5177	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
5178		goto error_out;
5179	if (rc != LPFC_EXT_ACC_CMD_ARG)
5180		goto error_out;
5181	if (!(ext_map & LPFC_EXT_ACC_ALL))
5182		goto error_out;
5183
5184	return nbytes;
5185error_out:
5186	/* Clean out command structure on command error out */
5187	memset(&idiag, 0, sizeof(idiag));
5188	return -EINVAL;
5189}
5190
5191/**
5192 * lpfc_idiag_extacc_read - idiag debugfs read access to extent information
5193 * @file: The file pointer to read from.
5194 * @buf: The buffer to copy the data to.
5195 * @nbytes: The number of bytes to read.
5196 * @ppos: The position in the file to start reading from.
5197 *
5198 * Description:
5199 * This routine reads data from the proper extent information according to
5200 * the idiag command, and copies to user @buf.
5201 *
5202 * Returns:
5203 * This function returns the amount of data that was read (this could be less
5204 * than @nbytes if the end of the file was reached) or a negative error value.
5205 **/
5206static ssize_t
5207lpfc_idiag_extacc_read(struct file *file, char __user *buf, size_t nbytes,
5208		       loff_t *ppos)
5209{
5210	struct lpfc_debug *debug = file->private_data;
5211	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
5212	char *pbuffer;
5213	uint32_t ext_map;
5214	int len = 0;
5215
5216	/* This is a user read operation */
5217	debug->op = LPFC_IDIAG_OP_RD;
5218
5219	if (!debug->buffer)
5220		debug->buffer = kmalloc(LPFC_EXT_ACC_BUF_SIZE, GFP_KERNEL);
5221	if (!debug->buffer)
5222		return 0;
5223	pbuffer = debug->buffer;
5224	if (*ppos)
5225		return 0;
5226	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
5227		return 0;
5228
5229	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
5230	if (ext_map & LPFC_EXT_ACC_AVAIL)
5231		len = lpfc_idiag_extacc_avail_get(phba, pbuffer, len);
5232	if (ext_map & LPFC_EXT_ACC_ALLOC)
5233		len = lpfc_idiag_extacc_alloc_get(phba, pbuffer, len);
5234	if (ext_map & LPFC_EXT_ACC_DRIVR)
5235		len = lpfc_idiag_extacc_drivr_get(phba, pbuffer, len);
5236
5237	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
5238}
5239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5240#undef lpfc_debugfs_op_disc_trc
5241static const struct file_operations lpfc_debugfs_op_disc_trc = {
5242	.owner =        THIS_MODULE,
5243	.open =         lpfc_debugfs_disc_trc_open,
5244	.llseek =       lpfc_debugfs_lseek,
5245	.read =         lpfc_debugfs_read,
5246	.release =      lpfc_debugfs_release,
5247};
5248
5249#undef lpfc_debugfs_op_nodelist
5250static const struct file_operations lpfc_debugfs_op_nodelist = {
5251	.owner =        THIS_MODULE,
5252	.open =         lpfc_debugfs_nodelist_open,
5253	.llseek =       lpfc_debugfs_lseek,
5254	.read =         lpfc_debugfs_read,
5255	.release =      lpfc_debugfs_release,
5256};
5257
5258#undef lpfc_debugfs_op_multixripools
5259static const struct file_operations lpfc_debugfs_op_multixripools = {
5260	.owner =        THIS_MODULE,
5261	.open =         lpfc_debugfs_multixripools_open,
5262	.llseek =       lpfc_debugfs_lseek,
5263	.read =         lpfc_debugfs_read,
5264	.write =	lpfc_debugfs_multixripools_write,
5265	.release =      lpfc_debugfs_release,
5266};
5267
5268#undef lpfc_debugfs_op_hbqinfo
5269static const struct file_operations lpfc_debugfs_op_hbqinfo = {
5270	.owner =        THIS_MODULE,
5271	.open =         lpfc_debugfs_hbqinfo_open,
5272	.llseek =       lpfc_debugfs_lseek,
5273	.read =         lpfc_debugfs_read,
5274	.release =      lpfc_debugfs_release,
5275};
5276
5277#ifdef LPFC_HDWQ_LOCK_STAT
5278#undef lpfc_debugfs_op_lockstat
5279static const struct file_operations lpfc_debugfs_op_lockstat = {
5280	.owner =        THIS_MODULE,
5281	.open =         lpfc_debugfs_lockstat_open,
5282	.llseek =       lpfc_debugfs_lseek,
5283	.read =         lpfc_debugfs_read,
5284	.write =        lpfc_debugfs_lockstat_write,
5285	.release =      lpfc_debugfs_release,
5286};
5287#endif
5288
 
 
 
 
 
 
 
 
 
5289#undef lpfc_debugfs_op_dumpHBASlim
5290static const struct file_operations lpfc_debugfs_op_dumpHBASlim = {
5291	.owner =        THIS_MODULE,
5292	.open =         lpfc_debugfs_dumpHBASlim_open,
5293	.llseek =       lpfc_debugfs_lseek,
5294	.read =         lpfc_debugfs_read,
5295	.release =      lpfc_debugfs_release,
5296};
5297
5298#undef lpfc_debugfs_op_dumpHostSlim
5299static const struct file_operations lpfc_debugfs_op_dumpHostSlim = {
5300	.owner =        THIS_MODULE,
5301	.open =         lpfc_debugfs_dumpHostSlim_open,
5302	.llseek =       lpfc_debugfs_lseek,
5303	.read =         lpfc_debugfs_read,
5304	.release =      lpfc_debugfs_release,
5305};
5306
5307#undef lpfc_debugfs_op_nvmestat
5308static const struct file_operations lpfc_debugfs_op_nvmestat = {
5309	.owner =        THIS_MODULE,
5310	.open =         lpfc_debugfs_nvmestat_open,
5311	.llseek =       lpfc_debugfs_lseek,
5312	.read =         lpfc_debugfs_read,
5313	.write =	lpfc_debugfs_nvmestat_write,
5314	.release =      lpfc_debugfs_release,
5315};
5316
5317#undef lpfc_debugfs_op_scsistat
5318static const struct file_operations lpfc_debugfs_op_scsistat = {
5319	.owner =        THIS_MODULE,
5320	.open =         lpfc_debugfs_scsistat_open,
5321	.llseek =       lpfc_debugfs_lseek,
5322	.read =         lpfc_debugfs_read,
5323	.write =	lpfc_debugfs_scsistat_write,
5324	.release =      lpfc_debugfs_release,
5325};
5326
5327#undef lpfc_debugfs_op_nvmektime
5328static const struct file_operations lpfc_debugfs_op_nvmektime = {
5329	.owner =        THIS_MODULE,
5330	.open =         lpfc_debugfs_nvmektime_open,
5331	.llseek =       lpfc_debugfs_lseek,
5332	.read =         lpfc_debugfs_read,
5333	.write =	lpfc_debugfs_nvmektime_write,
5334	.release =      lpfc_debugfs_release,
5335};
5336
5337#undef lpfc_debugfs_op_nvmeio_trc
5338static const struct file_operations lpfc_debugfs_op_nvmeio_trc = {
5339	.owner =        THIS_MODULE,
5340	.open =         lpfc_debugfs_nvmeio_trc_open,
5341	.llseek =       lpfc_debugfs_lseek,
5342	.read =         lpfc_debugfs_read,
5343	.write =	lpfc_debugfs_nvmeio_trc_write,
5344	.release =      lpfc_debugfs_release,
5345};
5346
5347#undef lpfc_debugfs_op_cpucheck
5348static const struct file_operations lpfc_debugfs_op_cpucheck = {
5349	.owner =        THIS_MODULE,
5350	.open =         lpfc_debugfs_cpucheck_open,
5351	.llseek =       lpfc_debugfs_lseek,
5352	.read =         lpfc_debugfs_read,
5353	.write =	lpfc_debugfs_cpucheck_write,
5354	.release =      lpfc_debugfs_release,
5355};
5356
5357#undef lpfc_debugfs_op_dif_err
5358static const struct file_operations lpfc_debugfs_op_dif_err = {
5359	.owner =	THIS_MODULE,
5360	.open =		simple_open,
5361	.llseek =	lpfc_debugfs_lseek,
5362	.read =		lpfc_debugfs_dif_err_read,
5363	.write =	lpfc_debugfs_dif_err_write,
5364	.release =	lpfc_debugfs_dif_err_release,
5365};
5366
5367#undef lpfc_debugfs_op_slow_ring_trc
5368static const struct file_operations lpfc_debugfs_op_slow_ring_trc = {
5369	.owner =        THIS_MODULE,
5370	.open =         lpfc_debugfs_slow_ring_trc_open,
5371	.llseek =       lpfc_debugfs_lseek,
5372	.read =         lpfc_debugfs_read,
5373	.release =      lpfc_debugfs_release,
5374};
5375
5376static struct dentry *lpfc_debugfs_root = NULL;
5377static atomic_t lpfc_debugfs_hba_count;
5378
5379/*
5380 * File operations for the iDiag debugfs
5381 */
5382#undef lpfc_idiag_op_pciCfg
5383static const struct file_operations lpfc_idiag_op_pciCfg = {
5384	.owner =        THIS_MODULE,
5385	.open =         lpfc_idiag_open,
5386	.llseek =       lpfc_debugfs_lseek,
5387	.read =         lpfc_idiag_pcicfg_read,
5388	.write =        lpfc_idiag_pcicfg_write,
5389	.release =      lpfc_idiag_cmd_release,
5390};
5391
5392#undef lpfc_idiag_op_barAcc
5393static const struct file_operations lpfc_idiag_op_barAcc = {
5394	.owner =        THIS_MODULE,
5395	.open =         lpfc_idiag_open,
5396	.llseek =       lpfc_debugfs_lseek,
5397	.read =         lpfc_idiag_baracc_read,
5398	.write =        lpfc_idiag_baracc_write,
5399	.release =      lpfc_idiag_cmd_release,
5400};
5401
5402#undef lpfc_idiag_op_queInfo
5403static const struct file_operations lpfc_idiag_op_queInfo = {
5404	.owner =        THIS_MODULE,
5405	.open =         lpfc_idiag_open,
5406	.read =         lpfc_idiag_queinfo_read,
5407	.release =      lpfc_idiag_release,
5408};
5409
5410#undef lpfc_idiag_op_queAcc
5411static const struct file_operations lpfc_idiag_op_queAcc = {
5412	.owner =        THIS_MODULE,
5413	.open =         lpfc_idiag_open,
5414	.llseek =       lpfc_debugfs_lseek,
5415	.read =         lpfc_idiag_queacc_read,
5416	.write =        lpfc_idiag_queacc_write,
5417	.release =      lpfc_idiag_cmd_release,
5418};
5419
5420#undef lpfc_idiag_op_drbAcc
5421static const struct file_operations lpfc_idiag_op_drbAcc = {
5422	.owner =        THIS_MODULE,
5423	.open =         lpfc_idiag_open,
5424	.llseek =       lpfc_debugfs_lseek,
5425	.read =         lpfc_idiag_drbacc_read,
5426	.write =        lpfc_idiag_drbacc_write,
5427	.release =      lpfc_idiag_cmd_release,
5428};
5429
5430#undef lpfc_idiag_op_ctlAcc
5431static const struct file_operations lpfc_idiag_op_ctlAcc = {
5432	.owner =        THIS_MODULE,
5433	.open =         lpfc_idiag_open,
5434	.llseek =       lpfc_debugfs_lseek,
5435	.read =         lpfc_idiag_ctlacc_read,
5436	.write =        lpfc_idiag_ctlacc_write,
5437	.release =      lpfc_idiag_cmd_release,
5438};
5439
5440#undef lpfc_idiag_op_mbxAcc
5441static const struct file_operations lpfc_idiag_op_mbxAcc = {
5442	.owner =        THIS_MODULE,
5443	.open =         lpfc_idiag_open,
5444	.llseek =       lpfc_debugfs_lseek,
5445	.read =         lpfc_idiag_mbxacc_read,
5446	.write =        lpfc_idiag_mbxacc_write,
5447	.release =      lpfc_idiag_cmd_release,
5448};
5449
5450#undef lpfc_idiag_op_extAcc
5451static const struct file_operations lpfc_idiag_op_extAcc = {
5452	.owner =        THIS_MODULE,
5453	.open =         lpfc_idiag_open,
5454	.llseek =       lpfc_debugfs_lseek,
5455	.read =         lpfc_idiag_extacc_read,
5456	.write =        lpfc_idiag_extacc_write,
5457	.release =      lpfc_idiag_cmd_release,
5458};
 
 
 
 
 
 
 
 
5459
 
 
 
 
 
 
 
 
5460#endif
5461
5462/* lpfc_idiag_mbxacc_dump_bsg_mbox - idiag debugfs dump bsg mailbox command
5463 * @phba: Pointer to HBA context object.
5464 * @dmabuf: Pointer to a DMA buffer descriptor.
5465 *
5466 * Description:
5467 * This routine dump a bsg pass-through non-embedded mailbox command with
5468 * external buffer.
5469 **/
5470void
5471lpfc_idiag_mbxacc_dump_bsg_mbox(struct lpfc_hba *phba, enum nemb_type nemb_tp,
5472				enum mbox_type mbox_tp, enum dma_type dma_tp,
5473				enum sta_type sta_tp,
5474				struct lpfc_dmabuf *dmabuf, uint32_t ext_buf)
5475{
5476#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5477	uint32_t *mbx_mbox_cmd, *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt;
5478	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
5479	int len = 0;
5480	uint32_t do_dump = 0;
5481	uint32_t *pword;
5482	uint32_t i;
5483
5484	if (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP)
5485		return;
5486
5487	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5488	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5489	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5490	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5491
5492	if (!(*mbx_dump_map & LPFC_MBX_DMP_ALL) ||
5493	    (*mbx_dump_cnt == 0) ||
5494	    (*mbx_word_cnt == 0))
5495		return;
5496
5497	if (*mbx_mbox_cmd != 0x9B)
5498		return;
5499
5500	if ((mbox_tp == mbox_rd) && (dma_tp == dma_mbox)) {
5501		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_MBX) {
5502			do_dump |= LPFC_BSG_DMP_MBX_RD_MBX;
5503			pr_err("\nRead mbox command (x%x), "
5504			       "nemb:0x%x, extbuf_cnt:%d:\n",
5505			       sta_tp, nemb_tp, ext_buf);
5506		}
5507	}
5508	if ((mbox_tp == mbox_rd) && (dma_tp == dma_ebuf)) {
5509		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_BUF) {
5510			do_dump |= LPFC_BSG_DMP_MBX_RD_BUF;
5511			pr_err("\nRead mbox buffer (x%x), "
5512			       "nemb:0x%x, extbuf_seq:%d:\n",
5513			       sta_tp, nemb_tp, ext_buf);
5514		}
5515	}
5516	if ((mbox_tp == mbox_wr) && (dma_tp == dma_mbox)) {
5517		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_MBX) {
5518			do_dump |= LPFC_BSG_DMP_MBX_WR_MBX;
5519			pr_err("\nWrite mbox command (x%x), "
5520			       "nemb:0x%x, extbuf_cnt:%d:\n",
5521			       sta_tp, nemb_tp, ext_buf);
5522		}
5523	}
5524	if ((mbox_tp == mbox_wr) && (dma_tp == dma_ebuf)) {
5525		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_BUF) {
5526			do_dump |= LPFC_BSG_DMP_MBX_WR_BUF;
5527			pr_err("\nWrite mbox buffer (x%x), "
5528			       "nemb:0x%x, extbuf_seq:%d:\n",
5529			       sta_tp, nemb_tp, ext_buf);
5530		}
5531	}
5532
5533	/* dump buffer content */
5534	if (do_dump) {
5535		pword = (uint32_t *)dmabuf->virt;
5536		for (i = 0; i < *mbx_word_cnt; i++) {
5537			if (!(i % 8)) {
5538				if (i != 0)
5539					pr_err("%s\n", line_buf);
5540				len = 0;
5541				len += scnprintf(line_buf+len,
5542						LPFC_MBX_ACC_LBUF_SZ-len,
5543						"%03d: ", i);
5544			}
5545			len += scnprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
5546					"%08x ", (uint32_t)*pword);
5547			pword++;
5548		}
5549		if ((i - 1) % 8)
5550			pr_err("%s\n", line_buf);
5551		(*mbx_dump_cnt)--;
5552	}
5553
5554	/* Clean out command structure on reaching dump count */
5555	if (*mbx_dump_cnt == 0)
5556		memset(&idiag, 0, sizeof(idiag));
5557	return;
5558#endif
5559}
5560
5561/* lpfc_idiag_mbxacc_dump_issue_mbox - idiag debugfs dump issue mailbox command
5562 * @phba: Pointer to HBA context object.
5563 * @dmabuf: Pointer to a DMA buffer descriptor.
5564 *
5565 * Description:
5566 * This routine dump a pass-through non-embedded mailbox command from issue
5567 * mailbox command.
5568 **/
5569void
5570lpfc_idiag_mbxacc_dump_issue_mbox(struct lpfc_hba *phba, MAILBOX_t *pmbox)
5571{
5572#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5573	uint32_t *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt, *mbx_mbox_cmd;
5574	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
5575	int len = 0;
5576	uint32_t *pword;
5577	uint8_t *pbyte;
5578	uint32_t i, j;
5579
5580	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP)
5581		return;
5582
5583	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5584	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5585	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5586	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5587
5588	if (!(*mbx_dump_map & LPFC_MBX_DMP_MBX_ALL) ||
5589	    (*mbx_dump_cnt == 0) ||
5590	    (*mbx_word_cnt == 0))
5591		return;
5592
5593	if ((*mbx_mbox_cmd != LPFC_MBX_ALL_CMD) &&
5594	    (*mbx_mbox_cmd != pmbox->mbxCommand))
5595		return;
5596
5597	/* dump buffer content */
5598	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_WORD) {
5599		pr_err("Mailbox command:0x%x dump by word:\n",
5600		       pmbox->mbxCommand);
5601		pword = (uint32_t *)pmbox;
5602		for (i = 0; i < *mbx_word_cnt; i++) {
5603			if (!(i % 8)) {
5604				if (i != 0)
5605					pr_err("%s\n", line_buf);
5606				len = 0;
5607				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
5608				len += scnprintf(line_buf+len,
5609						LPFC_MBX_ACC_LBUF_SZ-len,
5610						"%03d: ", i);
5611			}
5612			len += scnprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
5613					"%08x ",
5614					((uint32_t)*pword) & 0xffffffff);
5615			pword++;
5616		}
5617		if ((i - 1) % 8)
5618			pr_err("%s\n", line_buf);
5619		pr_err("\n");
5620	}
5621	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_BYTE) {
5622		pr_err("Mailbox command:0x%x dump by byte:\n",
5623		       pmbox->mbxCommand);
5624		pbyte = (uint8_t *)pmbox;
5625		for (i = 0; i < *mbx_word_cnt; i++) {
5626			if (!(i % 8)) {
5627				if (i != 0)
5628					pr_err("%s\n", line_buf);
5629				len = 0;
5630				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
5631				len += scnprintf(line_buf+len,
5632						LPFC_MBX_ACC_LBUF_SZ-len,
5633						"%03d: ", i);
5634			}
5635			for (j = 0; j < 4; j++) {
5636				len += scnprintf(line_buf+len,
5637						LPFC_MBX_ACC_LBUF_SZ-len,
5638						"%02x",
5639						((uint8_t)*pbyte) & 0xff);
5640				pbyte++;
5641			}
5642			len += scnprintf(line_buf+len,
5643					LPFC_MBX_ACC_LBUF_SZ-len, " ");
5644		}
5645		if ((i - 1) % 8)
5646			pr_err("%s\n", line_buf);
5647		pr_err("\n");
5648	}
5649	(*mbx_dump_cnt)--;
5650
5651	/* Clean out command structure on reaching dump count */
5652	if (*mbx_dump_cnt == 0)
5653		memset(&idiag, 0, sizeof(idiag));
5654	return;
5655#endif
5656}
5657
5658/**
5659 * lpfc_debugfs_initialize - Initialize debugfs for a vport
5660 * @vport: The vport pointer to initialize.
5661 *
5662 * Description:
5663 * When Debugfs is configured this routine sets up the lpfc debugfs file system.
5664 * If not already created, this routine will create the lpfc directory, and
5665 * lpfcX directory (for this HBA), and vportX directory for this vport. It will
5666 * also create each file used to access lpfc specific debugfs information.
5667 **/
5668inline void
5669lpfc_debugfs_initialize(struct lpfc_vport *vport)
5670{
5671#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5672	struct lpfc_hba   *phba = vport->phba;
5673	char name[64];
5674	uint32_t num, i;
5675	bool pport_setup = false;
5676
5677	if (!lpfc_debugfs_enable)
5678		return;
5679
5680	/* Setup lpfc root directory */
5681	if (!lpfc_debugfs_root) {
5682		lpfc_debugfs_root = debugfs_create_dir("lpfc", NULL);
5683		atomic_set(&lpfc_debugfs_hba_count, 0);
5684	}
5685	if (!lpfc_debugfs_start_time)
5686		lpfc_debugfs_start_time = jiffies;
5687
5688	/* Setup funcX directory for specific HBA PCI function */
5689	snprintf(name, sizeof(name), "fn%d", phba->brd_no);
5690	if (!phba->hba_debugfs_root) {
5691		pport_setup = true;
5692		phba->hba_debugfs_root =
5693			debugfs_create_dir(name, lpfc_debugfs_root);
5694		atomic_inc(&lpfc_debugfs_hba_count);
5695		atomic_set(&phba->debugfs_vport_count, 0);
5696
5697		/* Multi-XRI pools */
5698		snprintf(name, sizeof(name), "multixripools");
5699		phba->debug_multixri_pools =
5700			debugfs_create_file(name, S_IFREG | 0644,
5701					    phba->hba_debugfs_root,
5702					    phba,
5703					    &lpfc_debugfs_op_multixripools);
5704		if (!phba->debug_multixri_pools) {
5705			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5706					 "0527 Cannot create debugfs multixripools\n");
5707			goto debug_failed;
5708		}
5709
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5710		/* Setup hbqinfo */
5711		snprintf(name, sizeof(name), "hbqinfo");
5712		phba->debug_hbqinfo =
5713			debugfs_create_file(name, S_IFREG | 0644,
5714					    phba->hba_debugfs_root,
5715					    phba, &lpfc_debugfs_op_hbqinfo);
5716
5717#ifdef LPFC_HDWQ_LOCK_STAT
5718		/* Setup lockstat */
5719		snprintf(name, sizeof(name), "lockstat");
5720		phba->debug_lockstat =
5721			debugfs_create_file(name, S_IFREG | 0644,
5722					    phba->hba_debugfs_root,
5723					    phba, &lpfc_debugfs_op_lockstat);
5724		if (!phba->debug_lockstat) {
5725			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5726					 "4610 Cant create debugfs lockstat\n");
5727			goto debug_failed;
5728		}
5729#endif
5730
5731		/* Setup dumpHBASlim */
5732		if (phba->sli_rev < LPFC_SLI_REV4) {
5733			snprintf(name, sizeof(name), "dumpHBASlim");
5734			phba->debug_dumpHBASlim =
5735				debugfs_create_file(name,
5736					S_IFREG|S_IRUGO|S_IWUSR,
5737					phba->hba_debugfs_root,
5738					phba, &lpfc_debugfs_op_dumpHBASlim);
5739		} else
5740			phba->debug_dumpHBASlim = NULL;
5741
5742		/* Setup dumpHostSlim */
5743		if (phba->sli_rev < LPFC_SLI_REV4) {
5744			snprintf(name, sizeof(name), "dumpHostSlim");
5745			phba->debug_dumpHostSlim =
5746				debugfs_create_file(name,
5747					S_IFREG|S_IRUGO|S_IWUSR,
5748					phba->hba_debugfs_root,
5749					phba, &lpfc_debugfs_op_dumpHostSlim);
5750		} else
5751			phba->debug_dumpHostSlim = NULL;
5752
5753		/* Setup DIF Error Injections */
5754		snprintf(name, sizeof(name), "InjErrLBA");
5755		phba->debug_InjErrLBA =
5756			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5757			phba->hba_debugfs_root,
5758			phba, &lpfc_debugfs_op_dif_err);
5759		phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
5760
5761		snprintf(name, sizeof(name), "InjErrNPortID");
5762		phba->debug_InjErrNPortID =
5763			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5764			phba->hba_debugfs_root,
5765			phba, &lpfc_debugfs_op_dif_err);
5766
5767		snprintf(name, sizeof(name), "InjErrWWPN");
5768		phba->debug_InjErrWWPN =
5769			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5770			phba->hba_debugfs_root,
5771			phba, &lpfc_debugfs_op_dif_err);
5772
5773		snprintf(name, sizeof(name), "writeGuardInjErr");
5774		phba->debug_writeGuard =
5775			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5776			phba->hba_debugfs_root,
5777			phba, &lpfc_debugfs_op_dif_err);
5778
5779		snprintf(name, sizeof(name), "writeAppInjErr");
5780		phba->debug_writeApp =
5781			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5782			phba->hba_debugfs_root,
5783			phba, &lpfc_debugfs_op_dif_err);
5784
5785		snprintf(name, sizeof(name), "writeRefInjErr");
5786		phba->debug_writeRef =
5787			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5788			phba->hba_debugfs_root,
5789			phba, &lpfc_debugfs_op_dif_err);
5790
5791		snprintf(name, sizeof(name), "readGuardInjErr");
5792		phba->debug_readGuard =
5793			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5794			phba->hba_debugfs_root,
5795			phba, &lpfc_debugfs_op_dif_err);
5796
5797		snprintf(name, sizeof(name), "readAppInjErr");
5798		phba->debug_readApp =
5799			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5800			phba->hba_debugfs_root,
5801			phba, &lpfc_debugfs_op_dif_err);
5802
5803		snprintf(name, sizeof(name), "readRefInjErr");
5804		phba->debug_readRef =
5805			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5806			phba->hba_debugfs_root,
5807			phba, &lpfc_debugfs_op_dif_err);
5808
5809		/* Setup slow ring trace */
5810		if (lpfc_debugfs_max_slow_ring_trc) {
5811			num = lpfc_debugfs_max_slow_ring_trc - 1;
5812			if (num & lpfc_debugfs_max_slow_ring_trc) {
5813				/* Change to be a power of 2 */
5814				num = lpfc_debugfs_max_slow_ring_trc;
5815				i = 0;
5816				while (num > 1) {
5817					num = num >> 1;
5818					i++;
5819				}
5820				lpfc_debugfs_max_slow_ring_trc = (1 << i);
5821				pr_err("lpfc_debugfs_max_disc_trc changed to "
5822				       "%d\n", lpfc_debugfs_max_disc_trc);
5823			}
5824		}
5825
5826		snprintf(name, sizeof(name), "slow_ring_trace");
5827		phba->debug_slow_ring_trc =
5828			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5829				 phba->hba_debugfs_root,
5830				 phba, &lpfc_debugfs_op_slow_ring_trc);
5831		if (!phba->slow_ring_trc) {
5832			phba->slow_ring_trc = kmalloc(
5833				(sizeof(struct lpfc_debugfs_trc) *
5834				lpfc_debugfs_max_slow_ring_trc),
5835				GFP_KERNEL);
5836			if (!phba->slow_ring_trc) {
5837				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5838						 "0416 Cannot create debugfs "
5839						 "slow_ring buffer\n");
5840				goto debug_failed;
5841			}
5842			atomic_set(&phba->slow_ring_trc_cnt, 0);
5843			memset(phba->slow_ring_trc, 0,
5844				(sizeof(struct lpfc_debugfs_trc) *
5845				lpfc_debugfs_max_slow_ring_trc));
5846		}
5847
5848		snprintf(name, sizeof(name), "nvmeio_trc");
5849		phba->debug_nvmeio_trc =
5850			debugfs_create_file(name, 0644,
5851					    phba->hba_debugfs_root,
5852					    phba, &lpfc_debugfs_op_nvmeio_trc);
5853
5854		atomic_set(&phba->nvmeio_trc_cnt, 0);
5855		if (lpfc_debugfs_max_nvmeio_trc) {
5856			num = lpfc_debugfs_max_nvmeio_trc - 1;
5857			if (num & lpfc_debugfs_max_disc_trc) {
5858				/* Change to be a power of 2 */
5859				num = lpfc_debugfs_max_nvmeio_trc;
5860				i = 0;
5861				while (num > 1) {
5862					num = num >> 1;
5863					i++;
5864				}
5865				lpfc_debugfs_max_nvmeio_trc = (1 << i);
5866				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5867						"0575 lpfc_debugfs_max_nvmeio_trc "
5868						"changed to %d\n",
5869						lpfc_debugfs_max_nvmeio_trc);
5870			}
5871			phba->nvmeio_trc_size = lpfc_debugfs_max_nvmeio_trc;
5872
5873			/* Allocate trace buffer and initialize */
5874			phba->nvmeio_trc = kzalloc(
5875				(sizeof(struct lpfc_debugfs_nvmeio_trc) *
5876				phba->nvmeio_trc_size), GFP_KERNEL);
5877
5878			if (!phba->nvmeio_trc) {
5879				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5880						"0576 Cannot create debugfs "
5881						"nvmeio_trc buffer\n");
5882				goto nvmeio_off;
5883			}
5884			phba->nvmeio_trc_on = 1;
5885			phba->nvmeio_trc_output_idx = 0;
5886			phba->nvmeio_trc = NULL;
5887		} else {
5888nvmeio_off:
5889			phba->nvmeio_trc_size = 0;
5890			phba->nvmeio_trc_on = 0;
5891			phba->nvmeio_trc_output_idx = 0;
5892			phba->nvmeio_trc = NULL;
5893		}
5894	}
5895
5896	snprintf(name, sizeof(name), "vport%d", vport->vpi);
5897	if (!vport->vport_debugfs_root) {
5898		vport->vport_debugfs_root =
5899			debugfs_create_dir(name, phba->hba_debugfs_root);
5900		atomic_inc(&phba->debugfs_vport_count);
5901	}
5902
5903	if (lpfc_debugfs_max_disc_trc) {
5904		num = lpfc_debugfs_max_disc_trc - 1;
5905		if (num & lpfc_debugfs_max_disc_trc) {
5906			/* Change to be a power of 2 */
5907			num = lpfc_debugfs_max_disc_trc;
5908			i = 0;
5909			while (num > 1) {
5910				num = num >> 1;
5911				i++;
5912			}
5913			lpfc_debugfs_max_disc_trc = (1 << i);
5914			pr_err("lpfc_debugfs_max_disc_trc changed to %d\n",
5915			       lpfc_debugfs_max_disc_trc);
5916		}
5917	}
5918
5919	vport->disc_trc = kzalloc(
5920		(sizeof(struct lpfc_debugfs_trc) * lpfc_debugfs_max_disc_trc),
5921		GFP_KERNEL);
5922
5923	if (!vport->disc_trc) {
5924		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5925				 "0418 Cannot create debugfs disc trace "
5926				 "buffer\n");
5927		goto debug_failed;
5928	}
5929	atomic_set(&vport->disc_trc_cnt, 0);
5930
5931	snprintf(name, sizeof(name), "discovery_trace");
5932	vport->debug_disc_trc =
5933		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5934				 vport->vport_debugfs_root,
5935				 vport, &lpfc_debugfs_op_disc_trc);
5936	snprintf(name, sizeof(name), "nodelist");
5937	vport->debug_nodelist =
5938		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5939				 vport->vport_debugfs_root,
5940				 vport, &lpfc_debugfs_op_nodelist);
5941
5942	snprintf(name, sizeof(name), "nvmestat");
5943	vport->debug_nvmestat =
5944		debugfs_create_file(name, 0644,
5945				    vport->vport_debugfs_root,
5946				    vport, &lpfc_debugfs_op_nvmestat);
5947
5948	snprintf(name, sizeof(name), "scsistat");
5949	vport->debug_scsistat =
5950		debugfs_create_file(name, 0644,
5951				    vport->vport_debugfs_root,
5952				    vport, &lpfc_debugfs_op_scsistat);
5953	if (!vport->debug_scsistat) {
5954		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5955				 "4611 Cannot create debugfs scsistat\n");
5956		goto debug_failed;
5957	}
5958
5959	snprintf(name, sizeof(name), "nvmektime");
5960	vport->debug_nvmektime =
5961		debugfs_create_file(name, 0644,
5962				    vport->vport_debugfs_root,
5963				    vport, &lpfc_debugfs_op_nvmektime);
 
 
 
 
 
5964
5965	snprintf(name, sizeof(name), "cpucheck");
5966	vport->debug_cpucheck =
5967		debugfs_create_file(name, 0644,
5968				    vport->vport_debugfs_root,
5969				    vport, &lpfc_debugfs_op_cpucheck);
5970
5971	/*
5972	 * The following section is for additional directories/files for the
5973	 * physical port.
5974	 */
5975
5976	if (!pport_setup)
5977		goto debug_failed;
5978
5979	/*
5980	 * iDiag debugfs root entry points for SLI4 device only
5981	 */
5982	if (phba->sli_rev < LPFC_SLI_REV4)
5983		goto debug_failed;
5984
5985	snprintf(name, sizeof(name), "iDiag");
5986	if (!phba->idiag_root) {
5987		phba->idiag_root =
5988			debugfs_create_dir(name, phba->hba_debugfs_root);
5989		/* Initialize iDiag data structure */
5990		memset(&idiag, 0, sizeof(idiag));
5991	}
5992
5993	/* iDiag read PCI config space */
5994	snprintf(name, sizeof(name), "pciCfg");
5995	if (!phba->idiag_pci_cfg) {
5996		phba->idiag_pci_cfg =
5997			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5998				phba->idiag_root, phba, &lpfc_idiag_op_pciCfg);
5999		idiag.offset.last_rd = 0;
6000	}
6001
6002	/* iDiag PCI BAR access */
6003	snprintf(name, sizeof(name), "barAcc");
6004	if (!phba->idiag_bar_acc) {
6005		phba->idiag_bar_acc =
6006			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6007				phba->idiag_root, phba, &lpfc_idiag_op_barAcc);
6008		idiag.offset.last_rd = 0;
6009	}
6010
6011	/* iDiag get PCI function queue information */
6012	snprintf(name, sizeof(name), "queInfo");
6013	if (!phba->idiag_que_info) {
6014		phba->idiag_que_info =
6015			debugfs_create_file(name, S_IFREG|S_IRUGO,
6016			phba->idiag_root, phba, &lpfc_idiag_op_queInfo);
6017	}
6018
6019	/* iDiag access PCI function queue */
6020	snprintf(name, sizeof(name), "queAcc");
6021	if (!phba->idiag_que_acc) {
6022		phba->idiag_que_acc =
6023			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6024				phba->idiag_root, phba, &lpfc_idiag_op_queAcc);
6025	}
6026
6027	/* iDiag access PCI function doorbell registers */
6028	snprintf(name, sizeof(name), "drbAcc");
6029	if (!phba->idiag_drb_acc) {
6030		phba->idiag_drb_acc =
6031			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6032				phba->idiag_root, phba, &lpfc_idiag_op_drbAcc);
6033	}
6034
6035	/* iDiag access PCI function control registers */
6036	snprintf(name, sizeof(name), "ctlAcc");
6037	if (!phba->idiag_ctl_acc) {
6038		phba->idiag_ctl_acc =
6039			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6040				phba->idiag_root, phba, &lpfc_idiag_op_ctlAcc);
6041	}
6042
6043	/* iDiag access mbox commands */
6044	snprintf(name, sizeof(name), "mbxAcc");
6045	if (!phba->idiag_mbx_acc) {
6046		phba->idiag_mbx_acc =
6047			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6048				phba->idiag_root, phba, &lpfc_idiag_op_mbxAcc);
6049	}
6050
6051	/* iDiag extents access commands */
6052	if (phba->sli4_hba.extents_in_use) {
6053		snprintf(name, sizeof(name), "extAcc");
6054		if (!phba->idiag_ext_acc) {
6055			phba->idiag_ext_acc =
6056				debugfs_create_file(name,
6057						    S_IFREG|S_IRUGO|S_IWUSR,
6058						    phba->idiag_root, phba,
6059						    &lpfc_idiag_op_extAcc);
6060		}
6061	}
6062
6063debug_failed:
6064	return;
6065#endif
6066}
6067
6068/**
6069 * lpfc_debugfs_terminate -  Tear down debugfs infrastructure for this vport
6070 * @vport: The vport pointer to remove from debugfs.
6071 *
6072 * Description:
6073 * When Debugfs is configured this routine removes debugfs file system elements
6074 * that are specific to this vport. It also checks to see if there are any
6075 * users left for the debugfs directories associated with the HBA and driver. If
6076 * this is the last user of the HBA directory or driver directory then it will
6077 * remove those from the debugfs infrastructure as well.
6078 **/
6079inline void
6080lpfc_debugfs_terminate(struct lpfc_vport *vport)
6081{
6082#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
6083	struct lpfc_hba   *phba = vport->phba;
6084
6085	kfree(vport->disc_trc);
6086	vport->disc_trc = NULL;
6087
6088	debugfs_remove(vport->debug_disc_trc); /* discovery_trace */
6089	vport->debug_disc_trc = NULL;
6090
6091	debugfs_remove(vport->debug_nodelist); /* nodelist */
6092	vport->debug_nodelist = NULL;
6093
6094	debugfs_remove(vport->debug_nvmestat); /* nvmestat */
6095	vport->debug_nvmestat = NULL;
6096
6097	debugfs_remove(vport->debug_scsistat); /* scsistat */
6098	vport->debug_scsistat = NULL;
6099
6100	debugfs_remove(vport->debug_nvmektime); /* nvmektime */
6101	vport->debug_nvmektime = NULL;
6102
6103	debugfs_remove(vport->debug_cpucheck); /* cpucheck */
6104	vport->debug_cpucheck = NULL;
6105
6106	if (vport->vport_debugfs_root) {
6107		debugfs_remove(vport->vport_debugfs_root); /* vportX */
6108		vport->vport_debugfs_root = NULL;
6109		atomic_dec(&phba->debugfs_vport_count);
6110	}
6111
6112	if (atomic_read(&phba->debugfs_vport_count) == 0) {
6113
6114		debugfs_remove(phba->debug_multixri_pools); /* multixripools*/
6115		phba->debug_multixri_pools = NULL;
6116
6117		debugfs_remove(phba->debug_hbqinfo); /* hbqinfo */
6118		phba->debug_hbqinfo = NULL;
 
 
 
 
 
 
 
 
 
6119
6120#ifdef LPFC_HDWQ_LOCK_STAT
6121		debugfs_remove(phba->debug_lockstat); /* lockstat */
6122		phba->debug_lockstat = NULL;
6123#endif
6124		debugfs_remove(phba->debug_dumpHBASlim); /* HBASlim */
6125		phba->debug_dumpHBASlim = NULL;
6126
6127		debugfs_remove(phba->debug_dumpHostSlim); /* HostSlim */
6128		phba->debug_dumpHostSlim = NULL;
6129
6130		debugfs_remove(phba->debug_InjErrLBA); /* InjErrLBA */
6131		phba->debug_InjErrLBA = NULL;
6132
6133		debugfs_remove(phba->debug_InjErrNPortID);
6134		phba->debug_InjErrNPortID = NULL;
6135
6136		debugfs_remove(phba->debug_InjErrWWPN); /* InjErrWWPN */
6137		phba->debug_InjErrWWPN = NULL;
6138
6139		debugfs_remove(phba->debug_writeGuard); /* writeGuard */
6140		phba->debug_writeGuard = NULL;
6141
6142		debugfs_remove(phba->debug_writeApp); /* writeApp */
6143		phba->debug_writeApp = NULL;
6144
6145		debugfs_remove(phba->debug_writeRef); /* writeRef */
6146		phba->debug_writeRef = NULL;
6147
6148		debugfs_remove(phba->debug_readGuard); /* readGuard */
6149		phba->debug_readGuard = NULL;
6150
6151		debugfs_remove(phba->debug_readApp); /* readApp */
6152		phba->debug_readApp = NULL;
6153
6154		debugfs_remove(phba->debug_readRef); /* readRef */
6155		phba->debug_readRef = NULL;
6156
6157		kfree(phba->slow_ring_trc);
6158		phba->slow_ring_trc = NULL;
6159
6160		/* slow_ring_trace */
6161		debugfs_remove(phba->debug_slow_ring_trc);
6162		phba->debug_slow_ring_trc = NULL;
6163
6164		debugfs_remove(phba->debug_nvmeio_trc);
6165		phba->debug_nvmeio_trc = NULL;
6166
6167		kfree(phba->nvmeio_trc);
6168		phba->nvmeio_trc = NULL;
6169
6170		/*
6171		 * iDiag release
6172		 */
6173		if (phba->sli_rev == LPFC_SLI_REV4) {
6174			/* iDiag extAcc */
6175			debugfs_remove(phba->idiag_ext_acc);
6176			phba->idiag_ext_acc = NULL;
6177
6178			/* iDiag mbxAcc */
6179			debugfs_remove(phba->idiag_mbx_acc);
6180			phba->idiag_mbx_acc = NULL;
6181
6182			/* iDiag ctlAcc */
6183			debugfs_remove(phba->idiag_ctl_acc);
6184			phba->idiag_ctl_acc = NULL;
6185
6186			/* iDiag drbAcc */
6187			debugfs_remove(phba->idiag_drb_acc);
6188			phba->idiag_drb_acc = NULL;
6189
6190			/* iDiag queAcc */
6191			debugfs_remove(phba->idiag_que_acc);
6192			phba->idiag_que_acc = NULL;
6193
6194			/* iDiag queInfo */
6195			debugfs_remove(phba->idiag_que_info);
6196			phba->idiag_que_info = NULL;
6197
6198			/* iDiag barAcc */
6199			debugfs_remove(phba->idiag_bar_acc);
6200			phba->idiag_bar_acc = NULL;
6201
6202			/* iDiag pciCfg */
6203			debugfs_remove(phba->idiag_pci_cfg);
6204			phba->idiag_pci_cfg = NULL;
6205
6206			/* Finally remove the iDiag debugfs root */
6207			debugfs_remove(phba->idiag_root);
6208			phba->idiag_root = NULL;
6209		}
6210
6211		if (phba->hba_debugfs_root) {
6212			debugfs_remove(phba->hba_debugfs_root); /* fnX */
6213			phba->hba_debugfs_root = NULL;
6214			atomic_dec(&lpfc_debugfs_hba_count);
6215		}
6216
6217		if (atomic_read(&lpfc_debugfs_hba_count) == 0) {
6218			debugfs_remove(lpfc_debugfs_root); /* lpfc */
6219			lpfc_debugfs_root = NULL;
6220		}
6221	}
6222#endif
6223	return;
6224}
6225
6226/*
6227 * Driver debug utility routines outside of debugfs. The debug utility
6228 * routines implemented here is intended to be used in the instrumented
6229 * debug driver for debugging host or port issues.
6230 */
6231
6232/**
6233 * lpfc_debug_dump_all_queues - dump all the queues with a hba
6234 * @phba: Pointer to HBA context object.
6235 *
6236 * This function dumps entries of all the queues asociated with the @phba.
6237 **/
6238void
6239lpfc_debug_dump_all_queues(struct lpfc_hba *phba)
6240{
6241	int idx;
6242
6243	/*
6244	 * Dump Work Queues (WQs)
6245	 */
6246	lpfc_debug_dump_wq(phba, DUMP_MBX, 0);
6247	lpfc_debug_dump_wq(phba, DUMP_ELS, 0);
6248	lpfc_debug_dump_wq(phba, DUMP_NVMELS, 0);
6249
6250	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6251		lpfc_debug_dump_wq(phba, DUMP_IO, idx);
6252
6253	lpfc_debug_dump_hdr_rq(phba);
6254	lpfc_debug_dump_dat_rq(phba);
6255	/*
6256	 * Dump Complete Queues (CQs)
6257	 */
6258	lpfc_debug_dump_cq(phba, DUMP_MBX, 0);
6259	lpfc_debug_dump_cq(phba, DUMP_ELS, 0);
6260	lpfc_debug_dump_cq(phba, DUMP_NVMELS, 0);
6261
6262	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6263		lpfc_debug_dump_cq(phba, DUMP_IO, idx);
6264
6265	/*
6266	 * Dump Event Queues (EQs)
6267	 */
6268	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6269		lpfc_debug_dump_hba_eq(phba, idx);
6270}