xref: /linux/drivers/scsi/lpfc/lpfc_debugfs.c (revision e72e8bf1c9847a12de74f2fd3ea1f5511866526b)
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 #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  */
91 static int lpfc_debugfs_enable = 1;
92 module_param(lpfc_debugfs_enable, int, S_IRUGO);
93 MODULE_PARM_DESC(lpfc_debugfs_enable, "Enable debugfs services");
94 
95 /* This MUST be a power of 2 */
96 static int lpfc_debugfs_max_disc_trc;
97 module_param(lpfc_debugfs_max_disc_trc, int, S_IRUGO);
98 MODULE_PARM_DESC(lpfc_debugfs_max_disc_trc,
99 	"Set debugfs discovery trace depth");
100 
101 /* This MUST be a power of 2 */
102 static int lpfc_debugfs_max_slow_ring_trc;
103 module_param(lpfc_debugfs_max_slow_ring_trc, int, S_IRUGO);
104 MODULE_PARM_DESC(lpfc_debugfs_max_slow_ring_trc,
105 	"Set debugfs slow ring trace depth");
106 
107 /* This MUST be a power of 2 */
108 static int lpfc_debugfs_max_nvmeio_trc;
109 module_param(lpfc_debugfs_max_nvmeio_trc, int, 0444);
110 MODULE_PARM_DESC(lpfc_debugfs_max_nvmeio_trc,
111 		 "Set debugfs NVME IO trace depth");
112 
113 static int lpfc_debugfs_mask_disc_trc;
114 module_param(lpfc_debugfs_mask_disc_trc, int, S_IRUGO);
115 MODULE_PARM_DESC(lpfc_debugfs_mask_disc_trc,
116 	"Set debugfs discovery trace mask");
117 
118 #include <linux/debugfs.h>
119 
120 static atomic_t lpfc_debugfs_seq_trc_cnt = ATOMIC_INIT(0);
121 static unsigned long lpfc_debugfs_start_time = 0L;
122 
123 /* iDiag */
124 static 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  **/
145 static int
146 lpfc_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  **/
211 static int
212 lpfc_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 
258 static 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  **/
279 static int
280 lpfc_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 		}
372 skipit:
373 		hbqe++;
374 		if (len > LPFC_HBQINFO_SIZE - 54)
375 			break;
376 	}
377 	spin_unlock_irq(&phba->hbalock);
378 	return len;
379 }
380 
381 static int lpfc_debugfs_last_xripool;
382 
383 /**
384  * lpfc_debugfs_common_xri_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  **/
403 static int
404 lpfc_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  **/
455 static int
456 lpfc_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
571 static 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  **/
593 static int
594 lpfc_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 
642 static 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  **/
662 static int
663 lpfc_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  **/
721 static int
722 lpfc_debugfs_dumpHostSlim_data(struct lpfc_hba *phba, char *buf, int size)
723 {
724 	int len = 0;
725 	int i, off;
726 	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  **/
804 static int
805 lpfc_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%llx ",
867 				wwn_to_u64(ndlp->nlp_portname.u.wwn));
868 		len += scnprintf(buf+len, size-len,
869 				"WWNN x%llx ",
870 				wwn_to_u64(ndlp->nlp_nodename.u.wwn));
871 		if (ndlp->nlp_flag & NLP_RPI_REGISTERED)
872 			len += scnprintf(buf+len, size-len, "RPI:%03d ",
873 					ndlp->nlp_rpi);
874 		else
875 			len += scnprintf(buf+len, size-len, "RPI:none ");
876 		len +=  scnprintf(buf+len, size-len, "flag:x%08x ",
877 			ndlp->nlp_flag);
878 		if (!ndlp->nlp_type)
879 			len += scnprintf(buf+len, size-len, "UNKNOWN_TYPE ");
880 		if (ndlp->nlp_type & NLP_FC_NODE)
881 			len += scnprintf(buf+len, size-len, "FC_NODE ");
882 		if (ndlp->nlp_type & NLP_FABRIC) {
883 			len += scnprintf(buf+len, size-len, "FABRIC ");
884 			iocnt = 0;
885 		}
886 		if (ndlp->nlp_type & NLP_FCP_TARGET)
887 			len += scnprintf(buf+len, size-len, "FCP_TGT sid:%d ",
888 				ndlp->nlp_sid);
889 		if (ndlp->nlp_type & NLP_FCP_INITIATOR)
890 			len += scnprintf(buf+len, size-len, "FCP_INITIATOR ");
891 		if (ndlp->nlp_type & NLP_NVME_TARGET)
892 			len += scnprintf(buf + len,
893 					size - len, "NVME_TGT sid:%d ",
894 					NLP_NO_SID);
895 		if (ndlp->nlp_type & NLP_NVME_INITIATOR)
896 			len += scnprintf(buf + len,
897 					size - len, "NVME_INITIATOR ");
898 		len += scnprintf(buf+len, size-len, "usgmap:%x ",
899 			ndlp->nlp_usg_map);
900 		len += scnprintf(buf+len, size-len, "refcnt:%x",
901 			kref_read(&ndlp->kref));
902 		if (iocnt) {
903 			i = atomic_read(&ndlp->cmd_pending);
904 			len += scnprintf(buf + len, size - len,
905 					" OutIO:x%x Qdepth x%x",
906 					i, ndlp->cmd_qdepth);
907 			outio += i;
908 		}
909 		len += scnprintf(buf + len, size - len, "defer:%x ",
910 			ndlp->nlp_defer_did);
911 		len +=  scnprintf(buf+len, size-len, "\n");
912 	}
913 	spin_unlock_irq(shost->host_lock);
914 
915 	len += scnprintf(buf + len, size - len,
916 			"\nOutstanding IO x%x\n",  outio);
917 
918 	if (phba->nvmet_support && phba->targetport && (vport == phba->pport)) {
919 		len += scnprintf(buf + len, size - len,
920 				"\nNVME Targetport Entry ...\n");
921 
922 		/* Port state is only one of two values for now. */
923 		if (phba->targetport->port_id)
924 			statep = "REGISTERED";
925 		else
926 			statep = "INIT";
927 		len += scnprintf(buf + len, size - len,
928 				"TGT WWNN x%llx WWPN x%llx State %s\n",
929 				wwn_to_u64(vport->fc_nodename.u.wwn),
930 				wwn_to_u64(vport->fc_portname.u.wwn),
931 				statep);
932 		len += scnprintf(buf + len, size - len,
933 				"    Targetport DID x%06x\n",
934 				phba->targetport->port_id);
935 		goto out_exit;
936 	}
937 
938 	len += scnprintf(buf + len, size - len,
939 				"\nNVME Lport/Rport Entries ...\n");
940 
941 	localport = vport->localport;
942 	if (!localport)
943 		goto out_exit;
944 
945 	spin_lock_irq(shost->host_lock);
946 
947 	/* Port state is only one of two values for now. */
948 	if (localport->port_id)
949 		statep = "ONLINE";
950 	else
951 		statep = "UNKNOWN ";
952 
953 	len += scnprintf(buf + len, size - len,
954 			"Lport DID x%06x PortState %s\n",
955 			localport->port_id, statep);
956 
957 	len += scnprintf(buf + len, size - len, "\tRport List:\n");
958 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
959 		/* local short-hand pointer. */
960 		spin_lock(&phba->hbalock);
961 		rport = lpfc_ndlp_get_nrport(ndlp);
962 		if (rport)
963 			nrport = rport->remoteport;
964 		else
965 			nrport = NULL;
966 		spin_unlock(&phba->hbalock);
967 		if (!nrport)
968 			continue;
969 
970 		/* Port state is only one of two values for now. */
971 		switch (nrport->port_state) {
972 		case FC_OBJSTATE_ONLINE:
973 			statep = "ONLINE";
974 			break;
975 		case FC_OBJSTATE_UNKNOWN:
976 			statep = "UNKNOWN ";
977 			break;
978 		default:
979 			statep = "UNSUPPORTED";
980 			break;
981 		}
982 
983 		/* Tab in to show lport ownership. */
984 		len += scnprintf(buf + len, size - len,
985 				"\t%s Port ID:x%06x ",
986 				statep, nrport->port_id);
987 		len += scnprintf(buf + len, size - len, "WWPN x%llx ",
988 				nrport->port_name);
989 		len += scnprintf(buf + len, size - len, "WWNN x%llx ",
990 				nrport->node_name);
991 
992 		/* An NVME rport can have multiple roles. */
993 		if (nrport->port_role & FC_PORT_ROLE_NVME_INITIATOR)
994 			len +=  scnprintf(buf + len, size - len,
995 					 "INITIATOR ");
996 		if (nrport->port_role & FC_PORT_ROLE_NVME_TARGET)
997 			len +=  scnprintf(buf + len, size - len,
998 					 "TARGET ");
999 		if (nrport->port_role & FC_PORT_ROLE_NVME_DISCOVERY)
1000 			len +=  scnprintf(buf + len, size - len,
1001 					 "DISCSRVC ");
1002 		if (nrport->port_role & ~(FC_PORT_ROLE_NVME_INITIATOR |
1003 					  FC_PORT_ROLE_NVME_TARGET |
1004 					  FC_PORT_ROLE_NVME_DISCOVERY))
1005 			len +=  scnprintf(buf + len, size - len,
1006 					 "UNKNOWN ROLE x%x",
1007 					 nrport->port_role);
1008 		/* Terminate the string. */
1009 		len +=  scnprintf(buf + len, size - len, "\n");
1010 	}
1011 
1012 	spin_unlock_irq(shost->host_lock);
1013  out_exit:
1014 	return len;
1015 }
1016 
1017 /**
1018  * lpfc_debugfs_nvmestat_data - Dump target node list to a buffer
1019  * @vport: The vport to gather target node info from.
1020  * @buf: The buffer to dump log into.
1021  * @size: The maximum amount of data to process.
1022  *
1023  * Description:
1024  * This routine dumps the NVME statistics associated with @vport
1025  *
1026  * Return Value:
1027  * This routine returns the amount of bytes that were dumped into @buf and will
1028  * not exceed @size.
1029  **/
1030 static int
1031 lpfc_debugfs_nvmestat_data(struct lpfc_vport *vport, char *buf, int size)
1032 {
1033 	struct lpfc_hba   *phba = vport->phba;
1034 	struct lpfc_nvmet_tgtport *tgtp;
1035 	struct lpfc_async_xchg_ctx *ctxp, *next_ctxp;
1036 	struct nvme_fc_local_port *localport;
1037 	struct lpfc_fc4_ctrl_stat *cstat;
1038 	struct lpfc_nvme_lport *lport;
1039 	uint64_t data1, data2, data3;
1040 	uint64_t tot, totin, totout;
1041 	int cnt, i;
1042 	int len = 0;
1043 
1044 	if (phba->nvmet_support) {
1045 		if (!phba->targetport)
1046 			return len;
1047 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1048 		len += scnprintf(buf + len, size - len,
1049 				"\nNVME Targetport Statistics\n");
1050 
1051 		len += scnprintf(buf + len, size - len,
1052 				"LS: Rcv %08x Drop %08x Abort %08x\n",
1053 				atomic_read(&tgtp->rcv_ls_req_in),
1054 				atomic_read(&tgtp->rcv_ls_req_drop),
1055 				atomic_read(&tgtp->xmt_ls_abort));
1056 		if (atomic_read(&tgtp->rcv_ls_req_in) !=
1057 		    atomic_read(&tgtp->rcv_ls_req_out)) {
1058 			len += scnprintf(buf + len, size - len,
1059 					"Rcv LS: in %08x != out %08x\n",
1060 					atomic_read(&tgtp->rcv_ls_req_in),
1061 					atomic_read(&tgtp->rcv_ls_req_out));
1062 		}
1063 
1064 		len += scnprintf(buf + len, size - len,
1065 				"LS: Xmt %08x Drop %08x Cmpl %08x\n",
1066 				atomic_read(&tgtp->xmt_ls_rsp),
1067 				atomic_read(&tgtp->xmt_ls_drop),
1068 				atomic_read(&tgtp->xmt_ls_rsp_cmpl));
1069 
1070 		len += scnprintf(buf + len, size - len,
1071 				"LS: RSP Abort %08x xb %08x Err %08x\n",
1072 				atomic_read(&tgtp->xmt_ls_rsp_aborted),
1073 				atomic_read(&tgtp->xmt_ls_rsp_xb_set),
1074 				atomic_read(&tgtp->xmt_ls_rsp_error));
1075 
1076 		len += scnprintf(buf + len, size - len,
1077 				"FCP: Rcv %08x Defer %08x Release %08x "
1078 				"Drop %08x\n",
1079 				atomic_read(&tgtp->rcv_fcp_cmd_in),
1080 				atomic_read(&tgtp->rcv_fcp_cmd_defer),
1081 				atomic_read(&tgtp->xmt_fcp_release),
1082 				atomic_read(&tgtp->rcv_fcp_cmd_drop));
1083 
1084 		if (atomic_read(&tgtp->rcv_fcp_cmd_in) !=
1085 		    atomic_read(&tgtp->rcv_fcp_cmd_out)) {
1086 			len += scnprintf(buf + len, size - len,
1087 					"Rcv FCP: in %08x != out %08x\n",
1088 					atomic_read(&tgtp->rcv_fcp_cmd_in),
1089 					atomic_read(&tgtp->rcv_fcp_cmd_out));
1090 		}
1091 
1092 		len += scnprintf(buf + len, size - len,
1093 				"FCP Rsp: read %08x readrsp %08x "
1094 				"write %08x rsp %08x\n",
1095 				atomic_read(&tgtp->xmt_fcp_read),
1096 				atomic_read(&tgtp->xmt_fcp_read_rsp),
1097 				atomic_read(&tgtp->xmt_fcp_write),
1098 				atomic_read(&tgtp->xmt_fcp_rsp));
1099 
1100 		len += scnprintf(buf + len, size - len,
1101 				"FCP Rsp Cmpl: %08x err %08x drop %08x\n",
1102 				atomic_read(&tgtp->xmt_fcp_rsp_cmpl),
1103 				atomic_read(&tgtp->xmt_fcp_rsp_error),
1104 				atomic_read(&tgtp->xmt_fcp_rsp_drop));
1105 
1106 		len += scnprintf(buf + len, size - len,
1107 				"FCP Rsp Abort: %08x xb %08x xricqe  %08x\n",
1108 				atomic_read(&tgtp->xmt_fcp_rsp_aborted),
1109 				atomic_read(&tgtp->xmt_fcp_rsp_xb_set),
1110 				atomic_read(&tgtp->xmt_fcp_xri_abort_cqe));
1111 
1112 		len += scnprintf(buf + len, size - len,
1113 				"ABORT: Xmt %08x Cmpl %08x\n",
1114 				atomic_read(&tgtp->xmt_fcp_abort),
1115 				atomic_read(&tgtp->xmt_fcp_abort_cmpl));
1116 
1117 		len += scnprintf(buf + len, size - len,
1118 				"ABORT: Sol %08x  Usol %08x Err %08x Cmpl %08x",
1119 				atomic_read(&tgtp->xmt_abort_sol),
1120 				atomic_read(&tgtp->xmt_abort_unsol),
1121 				atomic_read(&tgtp->xmt_abort_rsp),
1122 				atomic_read(&tgtp->xmt_abort_rsp_error));
1123 
1124 		len +=  scnprintf(buf + len, size - len, "\n");
1125 
1126 		cnt = 0;
1127 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1128 		list_for_each_entry_safe(ctxp, next_ctxp,
1129 				&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1130 				list) {
1131 			cnt++;
1132 		}
1133 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1134 		if (cnt) {
1135 			len += scnprintf(buf + len, size - len,
1136 					"ABORT: %d ctx entries\n", cnt);
1137 			spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1138 			list_for_each_entry_safe(ctxp, next_ctxp,
1139 				    &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1140 				    list) {
1141 				if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ))
1142 					break;
1143 				len += scnprintf(buf + len, size - len,
1144 						"Entry: oxid %x state %x "
1145 						"flag %x\n",
1146 						ctxp->oxid, ctxp->state,
1147 						ctxp->flag);
1148 			}
1149 			spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1150 		}
1151 
1152 		/* Calculate outstanding IOs */
1153 		tot = atomic_read(&tgtp->rcv_fcp_cmd_drop);
1154 		tot += atomic_read(&tgtp->xmt_fcp_release);
1155 		tot = atomic_read(&tgtp->rcv_fcp_cmd_in) - tot;
1156 
1157 		len += scnprintf(buf + len, size - len,
1158 				"IO_CTX: %08x  WAIT: cur %08x tot %08x\n"
1159 				"CTX Outstanding %08llx\n",
1160 				phba->sli4_hba.nvmet_xri_cnt,
1161 				phba->sli4_hba.nvmet_io_wait_cnt,
1162 				phba->sli4_hba.nvmet_io_wait_total,
1163 				tot);
1164 	} else {
1165 		if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
1166 			return len;
1167 
1168 		localport = vport->localport;
1169 		if (!localport)
1170 			return len;
1171 		lport = (struct lpfc_nvme_lport *)localport->private;
1172 		if (!lport)
1173 			return len;
1174 
1175 		len += scnprintf(buf + len, size - len,
1176 				"\nNVME HDWQ Statistics\n");
1177 
1178 		len += scnprintf(buf + len, size - len,
1179 				"LS: Xmt %016x Cmpl %016x\n",
1180 				atomic_read(&lport->fc4NvmeLsRequests),
1181 				atomic_read(&lport->fc4NvmeLsCmpls));
1182 
1183 		totin = 0;
1184 		totout = 0;
1185 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
1186 			cstat = &phba->sli4_hba.hdwq[i].nvme_cstat;
1187 			tot = cstat->io_cmpls;
1188 			totin += tot;
1189 			data1 = cstat->input_requests;
1190 			data2 = cstat->output_requests;
1191 			data3 = cstat->control_requests;
1192 			totout += (data1 + data2 + data3);
1193 
1194 			/* Limit to 32, debugfs display buffer limitation */
1195 			if (i >= 32)
1196 				continue;
1197 
1198 			len += scnprintf(buf + len, PAGE_SIZE - len,
1199 					"HDWQ (%d): Rd %016llx Wr %016llx "
1200 					"IO %016llx ",
1201 					i, data1, data2, data3);
1202 			len += scnprintf(buf + len, PAGE_SIZE - len,
1203 					"Cmpl %016llx OutIO %016llx\n",
1204 					tot, ((data1 + data2 + data3) - tot));
1205 		}
1206 		len += scnprintf(buf + len, PAGE_SIZE - len,
1207 				"Total FCP Cmpl %016llx Issue %016llx "
1208 				"OutIO %016llx\n",
1209 				totin, totout, totout - totin);
1210 
1211 		len += scnprintf(buf + len, size - len,
1212 				"LS Xmt Err: Abrt %08x Err %08x  "
1213 				"Cmpl Err: xb %08x Err %08x\n",
1214 				atomic_read(&lport->xmt_ls_abort),
1215 				atomic_read(&lport->xmt_ls_err),
1216 				atomic_read(&lport->cmpl_ls_xb),
1217 				atomic_read(&lport->cmpl_ls_err));
1218 
1219 		len += scnprintf(buf + len, size - len,
1220 				"FCP Xmt Err: noxri %06x nondlp %06x "
1221 				"qdepth %06x wqerr %06x err %06x Abrt %06x\n",
1222 				atomic_read(&lport->xmt_fcp_noxri),
1223 				atomic_read(&lport->xmt_fcp_bad_ndlp),
1224 				atomic_read(&lport->xmt_fcp_qdepth),
1225 				atomic_read(&lport->xmt_fcp_wqerr),
1226 				atomic_read(&lport->xmt_fcp_err),
1227 				atomic_read(&lport->xmt_fcp_abort));
1228 
1229 		len += scnprintf(buf + len, size - len,
1230 				"FCP Cmpl Err: xb %08x Err %08x\n",
1231 				atomic_read(&lport->cmpl_fcp_xb),
1232 				atomic_read(&lport->cmpl_fcp_err));
1233 
1234 	}
1235 
1236 	return len;
1237 }
1238 
1239 /**
1240  * lpfc_debugfs_scsistat_data - Dump target node list to a buffer
1241  * @vport: The vport to gather target node info from.
1242  * @buf: The buffer to dump log into.
1243  * @size: The maximum amount of data to process.
1244  *
1245  * Description:
1246  * This routine dumps the SCSI statistics associated with @vport
1247  *
1248  * Return Value:
1249  * This routine returns the amount of bytes that were dumped into @buf and will
1250  * not exceed @size.
1251  **/
1252 static int
1253 lpfc_debugfs_scsistat_data(struct lpfc_vport *vport, char *buf, int size)
1254 {
1255 	int len;
1256 	struct lpfc_hba *phba = vport->phba;
1257 	struct lpfc_fc4_ctrl_stat *cstat;
1258 	u64 data1, data2, data3;
1259 	u64 tot, totin, totout;
1260 	int i;
1261 	char tmp[LPFC_MAX_SCSI_INFO_TMP_LEN] = {0};
1262 
1263 	if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ||
1264 	    (phba->sli_rev != LPFC_SLI_REV4))
1265 		return 0;
1266 
1267 	scnprintf(buf, size, "SCSI HDWQ Statistics\n");
1268 
1269 	totin = 0;
1270 	totout = 0;
1271 	for (i = 0; i < phba->cfg_hdw_queue; i++) {
1272 		cstat = &phba->sli4_hba.hdwq[i].scsi_cstat;
1273 		tot = cstat->io_cmpls;
1274 		totin += tot;
1275 		data1 = cstat->input_requests;
1276 		data2 = cstat->output_requests;
1277 		data3 = cstat->control_requests;
1278 		totout += (data1 + data2 + data3);
1279 
1280 		scnprintf(tmp, sizeof(tmp), "HDWQ (%d): Rd %016llx Wr %016llx "
1281 			  "IO %016llx ", i, data1, data2, data3);
1282 		if (strlcat(buf, tmp, size) >= size)
1283 			goto buffer_done;
1284 
1285 		scnprintf(tmp, sizeof(tmp), "Cmpl %016llx OutIO %016llx\n",
1286 			  tot, ((data1 + data2 + data3) - tot));
1287 		if (strlcat(buf, tmp, size) >= size)
1288 			goto buffer_done;
1289 	}
1290 	scnprintf(tmp, sizeof(tmp), "Total FCP Cmpl %016llx Issue %016llx "
1291 		  "OutIO %016llx\n", totin, totout, totout - totin);
1292 	strlcat(buf, tmp, size);
1293 
1294 buffer_done:
1295 	len = strnlen(buf, size);
1296 
1297 	return len;
1298 }
1299 
1300 void
1301 lpfc_io_ktime(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
1302 {
1303 	uint64_t seg1, seg2, seg3, seg4;
1304 	uint64_t segsum;
1305 
1306 	if (!lpfc_cmd->ts_last_cmd ||
1307 	    !lpfc_cmd->ts_cmd_start ||
1308 	    !lpfc_cmd->ts_cmd_wqput ||
1309 	    !lpfc_cmd->ts_isr_cmpl ||
1310 	    !lpfc_cmd->ts_data_io)
1311 		return;
1312 
1313 	if (lpfc_cmd->ts_data_io < lpfc_cmd->ts_cmd_start)
1314 		return;
1315 	if (lpfc_cmd->ts_cmd_start < lpfc_cmd->ts_last_cmd)
1316 		return;
1317 	if (lpfc_cmd->ts_cmd_wqput < lpfc_cmd->ts_cmd_start)
1318 		return;
1319 	if (lpfc_cmd->ts_isr_cmpl < lpfc_cmd->ts_cmd_wqput)
1320 		return;
1321 	if (lpfc_cmd->ts_data_io < lpfc_cmd->ts_isr_cmpl)
1322 		return;
1323 	/*
1324 	 * Segment 1 - Time from Last FCP command cmpl is handed
1325 	 * off to NVME Layer to start of next command.
1326 	 * Segment 2 - Time from Driver receives a IO cmd start
1327 	 * from NVME Layer to WQ put is done on IO cmd.
1328 	 * Segment 3 - Time from Driver WQ put is done on IO cmd
1329 	 * to MSI-X ISR for IO cmpl.
1330 	 * Segment 4 - Time from MSI-X ISR for IO cmpl to when
1331 	 * cmpl is handled off to the NVME Layer.
1332 	 */
1333 	seg1 = lpfc_cmd->ts_cmd_start - lpfc_cmd->ts_last_cmd;
1334 	if (seg1 > 5000000)  /* 5 ms - for sequential IOs only */
1335 		seg1 = 0;
1336 
1337 	/* Calculate times relative to start of IO */
1338 	seg2 = (lpfc_cmd->ts_cmd_wqput - lpfc_cmd->ts_cmd_start);
1339 	segsum = seg2;
1340 	seg3 = lpfc_cmd->ts_isr_cmpl - lpfc_cmd->ts_cmd_start;
1341 	if (segsum > seg3)
1342 		return;
1343 	seg3 -= segsum;
1344 	segsum += seg3;
1345 
1346 	seg4 = lpfc_cmd->ts_data_io - lpfc_cmd->ts_cmd_start;
1347 	if (segsum > seg4)
1348 		return;
1349 	seg4 -= segsum;
1350 
1351 	phba->ktime_data_samples++;
1352 	phba->ktime_seg1_total += seg1;
1353 	if (seg1 < phba->ktime_seg1_min)
1354 		phba->ktime_seg1_min = seg1;
1355 	else if (seg1 > phba->ktime_seg1_max)
1356 		phba->ktime_seg1_max = seg1;
1357 	phba->ktime_seg2_total += seg2;
1358 	if (seg2 < phba->ktime_seg2_min)
1359 		phba->ktime_seg2_min = seg2;
1360 	else if (seg2 > phba->ktime_seg2_max)
1361 		phba->ktime_seg2_max = seg2;
1362 	phba->ktime_seg3_total += seg3;
1363 	if (seg3 < phba->ktime_seg3_min)
1364 		phba->ktime_seg3_min = seg3;
1365 	else if (seg3 > phba->ktime_seg3_max)
1366 		phba->ktime_seg3_max = seg3;
1367 	phba->ktime_seg4_total += seg4;
1368 	if (seg4 < phba->ktime_seg4_min)
1369 		phba->ktime_seg4_min = seg4;
1370 	else if (seg4 > phba->ktime_seg4_max)
1371 		phba->ktime_seg4_max = seg4;
1372 
1373 	lpfc_cmd->ts_last_cmd = 0;
1374 	lpfc_cmd->ts_cmd_start = 0;
1375 	lpfc_cmd->ts_cmd_wqput  = 0;
1376 	lpfc_cmd->ts_isr_cmpl = 0;
1377 	lpfc_cmd->ts_data_io = 0;
1378 }
1379 
1380 /**
1381  * lpfc_debugfs_ioktime_data - Dump target node list to a buffer
1382  * @vport: The vport to gather target node info from.
1383  * @buf: The buffer to dump log into.
1384  * @size: The maximum amount of data to process.
1385  *
1386  * Description:
1387  * This routine dumps the NVME statistics associated with @vport
1388  *
1389  * Return Value:
1390  * This routine returns the amount of bytes that were dumped into @buf and will
1391  * not exceed @size.
1392  **/
1393 static int
1394 lpfc_debugfs_ioktime_data(struct lpfc_vport *vport, char *buf, int size)
1395 {
1396 	struct lpfc_hba   *phba = vport->phba;
1397 	int len = 0;
1398 
1399 	if (phba->nvmet_support == 0) {
1400 		/* Initiator */
1401 		len += scnprintf(buf + len, PAGE_SIZE - len,
1402 				"ktime %s: Total Samples: %lld\n",
1403 				(phba->ktime_on ?  "Enabled" : "Disabled"),
1404 				phba->ktime_data_samples);
1405 		if (phba->ktime_data_samples == 0)
1406 			return len;
1407 
1408 		len += scnprintf(
1409 			buf + len, PAGE_SIZE - len,
1410 			"Segment 1: Last Cmd cmpl "
1411 			"done -to- Start of next Cmd (in driver)\n");
1412 		len += scnprintf(
1413 			buf + len, PAGE_SIZE - len,
1414 			"avg:%08lld min:%08lld max %08lld\n",
1415 			div_u64(phba->ktime_seg1_total,
1416 				phba->ktime_data_samples),
1417 			phba->ktime_seg1_min,
1418 			phba->ktime_seg1_max);
1419 		len += scnprintf(
1420 			buf + len, PAGE_SIZE - len,
1421 			"Segment 2: Driver start of Cmd "
1422 			"-to- Firmware WQ doorbell\n");
1423 		len += scnprintf(
1424 			buf + len, PAGE_SIZE - len,
1425 			"avg:%08lld min:%08lld max %08lld\n",
1426 			div_u64(phba->ktime_seg2_total,
1427 				phba->ktime_data_samples),
1428 			phba->ktime_seg2_min,
1429 			phba->ktime_seg2_max);
1430 		len += scnprintf(
1431 			buf + len, PAGE_SIZE - len,
1432 			"Segment 3: Firmware WQ doorbell -to- "
1433 			"MSI-X ISR cmpl\n");
1434 		len += scnprintf(
1435 			buf + len, PAGE_SIZE - len,
1436 			"avg:%08lld min:%08lld max %08lld\n",
1437 			div_u64(phba->ktime_seg3_total,
1438 				phba->ktime_data_samples),
1439 			phba->ktime_seg3_min,
1440 			phba->ktime_seg3_max);
1441 		len += scnprintf(
1442 			buf + len, PAGE_SIZE - len,
1443 			"Segment 4: MSI-X ISR cmpl -to- "
1444 			"Cmd cmpl done\n");
1445 		len += scnprintf(
1446 			buf + len, PAGE_SIZE - len,
1447 			"avg:%08lld min:%08lld max %08lld\n",
1448 			div_u64(phba->ktime_seg4_total,
1449 				phba->ktime_data_samples),
1450 			phba->ktime_seg4_min,
1451 			phba->ktime_seg4_max);
1452 		len += scnprintf(
1453 			buf + len, PAGE_SIZE - len,
1454 			"Total IO avg time: %08lld\n",
1455 			div_u64(phba->ktime_seg1_total +
1456 			phba->ktime_seg2_total  +
1457 			phba->ktime_seg3_total +
1458 			phba->ktime_seg4_total,
1459 			phba->ktime_data_samples));
1460 		return len;
1461 	}
1462 
1463 	/* NVME Target */
1464 	len += scnprintf(buf + len, PAGE_SIZE-len,
1465 			"ktime %s: Total Samples: %lld %lld\n",
1466 			(phba->ktime_on ? "Enabled" : "Disabled"),
1467 			phba->ktime_data_samples,
1468 			phba->ktime_status_samples);
1469 	if (phba->ktime_data_samples == 0)
1470 		return len;
1471 
1472 	len += scnprintf(buf + len, PAGE_SIZE-len,
1473 			"Segment 1: MSI-X ISR Rcv cmd -to- "
1474 			"cmd pass to NVME Layer\n");
1475 	len += scnprintf(buf + len, PAGE_SIZE-len,
1476 			"avg:%08lld min:%08lld max %08lld\n",
1477 			div_u64(phba->ktime_seg1_total,
1478 				phba->ktime_data_samples),
1479 			phba->ktime_seg1_min,
1480 			phba->ktime_seg1_max);
1481 	len += scnprintf(buf + len, PAGE_SIZE-len,
1482 			"Segment 2: cmd pass to NVME Layer- "
1483 			"-to- Driver rcv cmd OP (action)\n");
1484 	len += scnprintf(buf + len, PAGE_SIZE-len,
1485 			"avg:%08lld min:%08lld max %08lld\n",
1486 			div_u64(phba->ktime_seg2_total,
1487 				phba->ktime_data_samples),
1488 			phba->ktime_seg2_min,
1489 			phba->ktime_seg2_max);
1490 	len += scnprintf(buf + len, PAGE_SIZE-len,
1491 			"Segment 3: Driver rcv cmd OP -to- "
1492 			"Firmware WQ doorbell: cmd\n");
1493 	len += scnprintf(buf + len, PAGE_SIZE-len,
1494 			"avg:%08lld min:%08lld max %08lld\n",
1495 			div_u64(phba->ktime_seg3_total,
1496 				phba->ktime_data_samples),
1497 			phba->ktime_seg3_min,
1498 			phba->ktime_seg3_max);
1499 	len += scnprintf(buf + len, PAGE_SIZE-len,
1500 			"Segment 4: Firmware WQ doorbell: cmd "
1501 			"-to- MSI-X ISR for cmd cmpl\n");
1502 	len += scnprintf(buf + len, PAGE_SIZE-len,
1503 			"avg:%08lld min:%08lld max %08lld\n",
1504 			div_u64(phba->ktime_seg4_total,
1505 				phba->ktime_data_samples),
1506 			phba->ktime_seg4_min,
1507 			phba->ktime_seg4_max);
1508 	len += scnprintf(buf + len, PAGE_SIZE-len,
1509 			"Segment 5: MSI-X ISR for cmd cmpl "
1510 			"-to- NVME layer passed cmd done\n");
1511 	len += scnprintf(buf + len, PAGE_SIZE-len,
1512 			"avg:%08lld min:%08lld max %08lld\n",
1513 			div_u64(phba->ktime_seg5_total,
1514 				phba->ktime_data_samples),
1515 			phba->ktime_seg5_min,
1516 			phba->ktime_seg5_max);
1517 
1518 	if (phba->ktime_status_samples == 0) {
1519 		len += scnprintf(buf + len, PAGE_SIZE-len,
1520 				"Total: cmd received by MSI-X ISR "
1521 				"-to- cmd completed on wire\n");
1522 		len += scnprintf(buf + len, PAGE_SIZE-len,
1523 				"avg:%08lld min:%08lld "
1524 				"max %08lld\n",
1525 				div_u64(phba->ktime_seg10_total,
1526 					phba->ktime_data_samples),
1527 				phba->ktime_seg10_min,
1528 				phba->ktime_seg10_max);
1529 		return len;
1530 	}
1531 
1532 	len += scnprintf(buf + len, PAGE_SIZE-len,
1533 			"Segment 6: NVME layer passed cmd done "
1534 			"-to- Driver rcv rsp status OP\n");
1535 	len += scnprintf(buf + len, PAGE_SIZE-len,
1536 			"avg:%08lld min:%08lld max %08lld\n",
1537 			div_u64(phba->ktime_seg6_total,
1538 				phba->ktime_status_samples),
1539 			phba->ktime_seg6_min,
1540 			phba->ktime_seg6_max);
1541 	len += scnprintf(buf + len, PAGE_SIZE-len,
1542 			"Segment 7: Driver rcv rsp status OP "
1543 			"-to- Firmware WQ doorbell: status\n");
1544 	len += scnprintf(buf + len, PAGE_SIZE-len,
1545 			"avg:%08lld min:%08lld max %08lld\n",
1546 			div_u64(phba->ktime_seg7_total,
1547 				phba->ktime_status_samples),
1548 			phba->ktime_seg7_min,
1549 			phba->ktime_seg7_max);
1550 	len += scnprintf(buf + len, PAGE_SIZE-len,
1551 			"Segment 8: Firmware WQ doorbell: status"
1552 			" -to- MSI-X ISR for status cmpl\n");
1553 	len += scnprintf(buf + len, PAGE_SIZE-len,
1554 			"avg:%08lld min:%08lld max %08lld\n",
1555 			div_u64(phba->ktime_seg8_total,
1556 				phba->ktime_status_samples),
1557 			phba->ktime_seg8_min,
1558 			phba->ktime_seg8_max);
1559 	len += scnprintf(buf + len, PAGE_SIZE-len,
1560 			"Segment 9: MSI-X ISR for status cmpl  "
1561 			"-to- NVME layer passed status done\n");
1562 	len += scnprintf(buf + len, PAGE_SIZE-len,
1563 			"avg:%08lld min:%08lld max %08lld\n",
1564 			div_u64(phba->ktime_seg9_total,
1565 				phba->ktime_status_samples),
1566 			phba->ktime_seg9_min,
1567 			phba->ktime_seg9_max);
1568 	len += scnprintf(buf + len, PAGE_SIZE-len,
1569 			"Total: cmd received by MSI-X ISR -to- "
1570 			"cmd completed on wire\n");
1571 	len += scnprintf(buf + len, PAGE_SIZE-len,
1572 			"avg:%08lld min:%08lld max %08lld\n",
1573 			div_u64(phba->ktime_seg10_total,
1574 				phba->ktime_status_samples),
1575 			phba->ktime_seg10_min,
1576 			phba->ktime_seg10_max);
1577 	return len;
1578 }
1579 
1580 /**
1581  * lpfc_debugfs_nvmeio_trc_data - Dump NVME IO trace list to a buffer
1582  * @phba: The phba to gather target node info from.
1583  * @buf: The buffer to dump log into.
1584  * @size: The maximum amount of data to process.
1585  *
1586  * Description:
1587  * This routine dumps the NVME IO trace associated with @phba
1588  *
1589  * Return Value:
1590  * This routine returns the amount of bytes that were dumped into @buf and will
1591  * not exceed @size.
1592  **/
1593 static int
1594 lpfc_debugfs_nvmeio_trc_data(struct lpfc_hba *phba, char *buf, int size)
1595 {
1596 	struct lpfc_debugfs_nvmeio_trc *dtp;
1597 	int i, state, index, skip;
1598 	int len = 0;
1599 
1600 	state = phba->nvmeio_trc_on;
1601 
1602 	index = (atomic_read(&phba->nvmeio_trc_cnt) + 1) &
1603 		(phba->nvmeio_trc_size - 1);
1604 	skip = phba->nvmeio_trc_output_idx;
1605 
1606 	len += scnprintf(buf + len, size - len,
1607 			"%s IO Trace %s: next_idx %d skip %d size %d\n",
1608 			(phba->nvmet_support ? "NVME" : "NVMET"),
1609 			(state ? "Enabled" : "Disabled"),
1610 			index, skip, phba->nvmeio_trc_size);
1611 
1612 	if (!phba->nvmeio_trc || state)
1613 		return len;
1614 
1615 	/* trace MUST bhe off to continue */
1616 
1617 	for (i = index; i < phba->nvmeio_trc_size; i++) {
1618 		if (skip) {
1619 			skip--;
1620 			continue;
1621 		}
1622 		dtp = phba->nvmeio_trc + i;
1623 		phba->nvmeio_trc_output_idx++;
1624 
1625 		if (!dtp->fmt)
1626 			continue;
1627 
1628 		len +=  scnprintf(buf + len, size - len, dtp->fmt,
1629 			dtp->data1, dtp->data2, dtp->data3);
1630 
1631 		if (phba->nvmeio_trc_output_idx >= phba->nvmeio_trc_size) {
1632 			phba->nvmeio_trc_output_idx = 0;
1633 			len += scnprintf(buf + len, size - len,
1634 					"Trace Complete\n");
1635 			goto out;
1636 		}
1637 
1638 		if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ)) {
1639 			len += scnprintf(buf + len, size - len,
1640 					"Trace Continue (%d of %d)\n",
1641 					phba->nvmeio_trc_output_idx,
1642 					phba->nvmeio_trc_size);
1643 			goto out;
1644 		}
1645 	}
1646 	for (i = 0; i < index; i++) {
1647 		if (skip) {
1648 			skip--;
1649 			continue;
1650 		}
1651 		dtp = phba->nvmeio_trc + i;
1652 		phba->nvmeio_trc_output_idx++;
1653 
1654 		if (!dtp->fmt)
1655 			continue;
1656 
1657 		len +=  scnprintf(buf + len, size - len, dtp->fmt,
1658 			dtp->data1, dtp->data2, dtp->data3);
1659 
1660 		if (phba->nvmeio_trc_output_idx >= phba->nvmeio_trc_size) {
1661 			phba->nvmeio_trc_output_idx = 0;
1662 			len += scnprintf(buf + len, size - len,
1663 					"Trace Complete\n");
1664 			goto out;
1665 		}
1666 
1667 		if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ)) {
1668 			len += scnprintf(buf + len, size - len,
1669 					"Trace Continue (%d of %d)\n",
1670 					phba->nvmeio_trc_output_idx,
1671 					phba->nvmeio_trc_size);
1672 			goto out;
1673 		}
1674 	}
1675 
1676 	len += scnprintf(buf + len, size - len,
1677 			"Trace Done\n");
1678 out:
1679 	return len;
1680 }
1681 
1682 /**
1683  * lpfc_debugfs_hdwqstat_data - Dump I/O stats to a buffer
1684  * @vport: The vport to gather target node info from.
1685  * @buf: The buffer to dump log into.
1686  * @size: The maximum amount of data to process.
1687  *
1688  * Description:
1689  * This routine dumps the NVME + SCSI statistics associated with @vport
1690  *
1691  * Return Value:
1692  * This routine returns the amount of bytes that were dumped into @buf and will
1693  * not exceed @size.
1694  **/
1695 static int
1696 lpfc_debugfs_hdwqstat_data(struct lpfc_vport *vport, char *buf, int size)
1697 {
1698 	struct lpfc_hba   *phba = vport->phba;
1699 	struct lpfc_sli4_hdw_queue *qp;
1700 	struct lpfc_hdwq_stat *c_stat;
1701 	int i, j, len;
1702 	uint32_t tot_xmt;
1703 	uint32_t tot_rcv;
1704 	uint32_t tot_cmpl;
1705 	char tmp[LPFC_MAX_SCSI_INFO_TMP_LEN] = {0};
1706 
1707 	scnprintf(tmp, sizeof(tmp), "HDWQ Stats:\n\n");
1708 	if (strlcat(buf, tmp, size) >= size)
1709 		goto buffer_done;
1710 
1711 	scnprintf(tmp, sizeof(tmp), "(NVME Accounting: %s) ",
1712 		  (phba->hdwqstat_on &
1713 		  (LPFC_CHECK_NVME_IO | LPFC_CHECK_NVMET_IO) ?
1714 		  "Enabled" : "Disabled"));
1715 	if (strlcat(buf, tmp, size) >= size)
1716 		goto buffer_done;
1717 
1718 	scnprintf(tmp, sizeof(tmp), "(SCSI Accounting: %s) ",
1719 		  (phba->hdwqstat_on & LPFC_CHECK_SCSI_IO ?
1720 		  "Enabled" : "Disabled"));
1721 	if (strlcat(buf, tmp, size) >= size)
1722 		goto buffer_done;
1723 
1724 	scnprintf(tmp, sizeof(tmp), "\n\n");
1725 	if (strlcat(buf, tmp, size) >= size)
1726 		goto buffer_done;
1727 
1728 	for (i = 0; i < phba->cfg_hdw_queue; i++) {
1729 		qp = &phba->sli4_hba.hdwq[i];
1730 
1731 		tot_rcv = 0;
1732 		tot_xmt = 0;
1733 		tot_cmpl = 0;
1734 
1735 		for_each_present_cpu(j) {
1736 			c_stat = per_cpu_ptr(phba->sli4_hba.c_stat, j);
1737 
1738 			/* Only display for this HDWQ */
1739 			if (i != c_stat->hdwq_no)
1740 				continue;
1741 
1742 			/* Only display non-zero counters */
1743 			if (!c_stat->xmt_io && !c_stat->cmpl_io &&
1744 			    !c_stat->rcv_io)
1745 				continue;
1746 
1747 			if (!tot_xmt && !tot_cmpl && !tot_rcv) {
1748 				/* Print HDWQ string only the first time */
1749 				scnprintf(tmp, sizeof(tmp), "[HDWQ %d]:\t", i);
1750 				if (strlcat(buf, tmp, size) >= size)
1751 					goto buffer_done;
1752 			}
1753 
1754 			tot_xmt += c_stat->xmt_io;
1755 			tot_cmpl += c_stat->cmpl_io;
1756 			if (phba->nvmet_support)
1757 				tot_rcv += c_stat->rcv_io;
1758 
1759 			scnprintf(tmp, sizeof(tmp), "| [CPU %d]: ", j);
1760 			if (strlcat(buf, tmp, size) >= size)
1761 				goto buffer_done;
1762 
1763 			if (phba->nvmet_support) {
1764 				scnprintf(tmp, sizeof(tmp),
1765 					  "XMT 0x%x CMPL 0x%x RCV 0x%x |",
1766 					  c_stat->xmt_io, c_stat->cmpl_io,
1767 					  c_stat->rcv_io);
1768 				if (strlcat(buf, tmp, size) >= size)
1769 					goto buffer_done;
1770 			} else {
1771 				scnprintf(tmp, sizeof(tmp),
1772 					  "XMT 0x%x CMPL 0x%x |",
1773 					  c_stat->xmt_io, c_stat->cmpl_io);
1774 				if (strlcat(buf, tmp, size) >= size)
1775 					goto buffer_done;
1776 			}
1777 		}
1778 
1779 		/* Check if nothing to display */
1780 		if (!tot_xmt && !tot_cmpl && !tot_rcv)
1781 			continue;
1782 
1783 		scnprintf(tmp, sizeof(tmp), "\t->\t[HDWQ Total: ");
1784 		if (strlcat(buf, tmp, size) >= size)
1785 			goto buffer_done;
1786 
1787 		if (phba->nvmet_support) {
1788 			scnprintf(tmp, sizeof(tmp),
1789 				  "XMT 0x%x CMPL 0x%x RCV 0x%x]\n\n",
1790 				  tot_xmt, tot_cmpl, tot_rcv);
1791 			if (strlcat(buf, tmp, size) >= size)
1792 				goto buffer_done;
1793 		} else {
1794 			scnprintf(tmp, sizeof(tmp),
1795 				  "XMT 0x%x CMPL 0x%x]\n\n",
1796 				  tot_xmt, tot_cmpl);
1797 			if (strlcat(buf, tmp, size) >= size)
1798 				goto buffer_done;
1799 		}
1800 	}
1801 
1802 buffer_done:
1803 	len = strnlen(buf, size);
1804 	return len;
1805 }
1806 
1807 #endif
1808 
1809 /**
1810  * lpfc_debugfs_disc_trc - Store discovery trace log
1811  * @vport: The vport to associate this trace string with for retrieval.
1812  * @mask: Log entry classification.
1813  * @fmt: Format string to be displayed when dumping the log.
1814  * @data1: 1st data parameter to be applied to @fmt.
1815  * @data2: 2nd data parameter to be applied to @fmt.
1816  * @data3: 3rd data parameter to be applied to @fmt.
1817  *
1818  * Description:
1819  * This routine is used by the driver code to add a debugfs log entry to the
1820  * discovery trace buffer associated with @vport. Only entries with a @mask that
1821  * match the current debugfs discovery mask will be saved. Entries that do not
1822  * match will be thrown away. @fmt, @data1, @data2, and @data3 are used like
1823  * printf when displaying the log.
1824  **/
1825 inline void
1826 lpfc_debugfs_disc_trc(struct lpfc_vport *vport, int mask, char *fmt,
1827 	uint32_t data1, uint32_t data2, uint32_t data3)
1828 {
1829 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1830 	struct lpfc_debugfs_trc *dtp;
1831 	int index;
1832 
1833 	if (!(lpfc_debugfs_mask_disc_trc & mask))
1834 		return;
1835 
1836 	if (!lpfc_debugfs_enable || !lpfc_debugfs_max_disc_trc ||
1837 		!vport || !vport->disc_trc)
1838 		return;
1839 
1840 	index = atomic_inc_return(&vport->disc_trc_cnt) &
1841 		(lpfc_debugfs_max_disc_trc - 1);
1842 	dtp = vport->disc_trc + index;
1843 	dtp->fmt = fmt;
1844 	dtp->data1 = data1;
1845 	dtp->data2 = data2;
1846 	dtp->data3 = data3;
1847 	dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_seq_trc_cnt);
1848 	dtp->jif = jiffies;
1849 #endif
1850 	return;
1851 }
1852 
1853 /**
1854  * lpfc_debugfs_slow_ring_trc - Store slow ring trace log
1855  * @phba: The phba to associate this trace string with for retrieval.
1856  * @fmt: Format string to be displayed when dumping the log.
1857  * @data1: 1st data parameter to be applied to @fmt.
1858  * @data2: 2nd data parameter to be applied to @fmt.
1859  * @data3: 3rd data parameter to be applied to @fmt.
1860  *
1861  * Description:
1862  * This routine is used by the driver code to add a debugfs log entry to the
1863  * discovery trace buffer associated with @vport. @fmt, @data1, @data2, and
1864  * @data3 are used like printf when displaying the log.
1865  **/
1866 inline void
1867 lpfc_debugfs_slow_ring_trc(struct lpfc_hba *phba, char *fmt,
1868 	uint32_t data1, uint32_t data2, uint32_t data3)
1869 {
1870 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1871 	struct lpfc_debugfs_trc *dtp;
1872 	int index;
1873 
1874 	if (!lpfc_debugfs_enable || !lpfc_debugfs_max_slow_ring_trc ||
1875 		!phba || !phba->slow_ring_trc)
1876 		return;
1877 
1878 	index = atomic_inc_return(&phba->slow_ring_trc_cnt) &
1879 		(lpfc_debugfs_max_slow_ring_trc - 1);
1880 	dtp = phba->slow_ring_trc + index;
1881 	dtp->fmt = fmt;
1882 	dtp->data1 = data1;
1883 	dtp->data2 = data2;
1884 	dtp->data3 = data3;
1885 	dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_seq_trc_cnt);
1886 	dtp->jif = jiffies;
1887 #endif
1888 	return;
1889 }
1890 
1891 /**
1892  * lpfc_debugfs_nvme_trc - Store NVME/NVMET trace log
1893  * @phba: The phba to associate this trace string with for retrieval.
1894  * @fmt: Format string to be displayed when dumping the log.
1895  * @data1: 1st data parameter to be applied to @fmt.
1896  * @data2: 2nd data parameter to be applied to @fmt.
1897  * @data3: 3rd data parameter to be applied to @fmt.
1898  *
1899  * Description:
1900  * This routine is used by the driver code to add a debugfs log entry to the
1901  * nvme trace buffer associated with @phba. @fmt, @data1, @data2, and
1902  * @data3 are used like printf when displaying the log.
1903  **/
1904 inline void
1905 lpfc_debugfs_nvme_trc(struct lpfc_hba *phba, char *fmt,
1906 		      uint16_t data1, uint16_t data2, uint32_t data3)
1907 {
1908 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1909 	struct lpfc_debugfs_nvmeio_trc *dtp;
1910 	int index;
1911 
1912 	if (!phba->nvmeio_trc_on || !phba->nvmeio_trc)
1913 		return;
1914 
1915 	index = atomic_inc_return(&phba->nvmeio_trc_cnt) &
1916 		(phba->nvmeio_trc_size - 1);
1917 	dtp = phba->nvmeio_trc + index;
1918 	dtp->fmt = fmt;
1919 	dtp->data1 = data1;
1920 	dtp->data2 = data2;
1921 	dtp->data3 = data3;
1922 #endif
1923 }
1924 
1925 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1926 /**
1927  * lpfc_debugfs_disc_trc_open - Open the discovery trace log
1928  * @inode: The inode pointer that contains a vport pointer.
1929  * @file: The file pointer to attach the log output.
1930  *
1931  * Description:
1932  * This routine is the entry point for the debugfs open file operation. It gets
1933  * the vport from the i_private field in @inode, allocates the necessary buffer
1934  * for the log, fills the buffer from the in-memory log for this vport, and then
1935  * returns a pointer to that log in the private_data field in @file.
1936  *
1937  * Returns:
1938  * This function returns zero if successful. On error it will return a negative
1939  * error value.
1940  **/
1941 static int
1942 lpfc_debugfs_disc_trc_open(struct inode *inode, struct file *file)
1943 {
1944 	struct lpfc_vport *vport = inode->i_private;
1945 	struct lpfc_debug *debug;
1946 	int size;
1947 	int rc = -ENOMEM;
1948 
1949 	if (!lpfc_debugfs_max_disc_trc) {
1950 		rc = -ENOSPC;
1951 		goto out;
1952 	}
1953 
1954 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1955 	if (!debug)
1956 		goto out;
1957 
1958 	/* Round to page boundary */
1959 	size =  (lpfc_debugfs_max_disc_trc * LPFC_DEBUG_TRC_ENTRY_SIZE);
1960 	size = PAGE_ALIGN(size);
1961 
1962 	debug->buffer = kmalloc(size, GFP_KERNEL);
1963 	if (!debug->buffer) {
1964 		kfree(debug);
1965 		goto out;
1966 	}
1967 
1968 	debug->len = lpfc_debugfs_disc_trc_data(vport, debug->buffer, size);
1969 	file->private_data = debug;
1970 
1971 	rc = 0;
1972 out:
1973 	return rc;
1974 }
1975 
1976 /**
1977  * lpfc_debugfs_slow_ring_trc_open - Open the Slow Ring trace log
1978  * @inode: The inode pointer that contains a vport pointer.
1979  * @file: The file pointer to attach the log output.
1980  *
1981  * Description:
1982  * This routine is the entry point for the debugfs open file operation. It gets
1983  * the vport from the i_private field in @inode, allocates the necessary buffer
1984  * for the log, fills the buffer from the in-memory log for this vport, and then
1985  * returns a pointer to that log in the private_data field in @file.
1986  *
1987  * Returns:
1988  * This function returns zero if successful. On error it will return a negative
1989  * error value.
1990  **/
1991 static int
1992 lpfc_debugfs_slow_ring_trc_open(struct inode *inode, struct file *file)
1993 {
1994 	struct lpfc_hba *phba = inode->i_private;
1995 	struct lpfc_debug *debug;
1996 	int size;
1997 	int rc = -ENOMEM;
1998 
1999 	if (!lpfc_debugfs_max_slow_ring_trc) {
2000 		rc = -ENOSPC;
2001 		goto out;
2002 	}
2003 
2004 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2005 	if (!debug)
2006 		goto out;
2007 
2008 	/* Round to page boundary */
2009 	size =  (lpfc_debugfs_max_slow_ring_trc * LPFC_DEBUG_TRC_ENTRY_SIZE);
2010 	size = PAGE_ALIGN(size);
2011 
2012 	debug->buffer = kmalloc(size, GFP_KERNEL);
2013 	if (!debug->buffer) {
2014 		kfree(debug);
2015 		goto out;
2016 	}
2017 
2018 	debug->len = lpfc_debugfs_slow_ring_trc_data(phba, debug->buffer, size);
2019 	file->private_data = debug;
2020 
2021 	rc = 0;
2022 out:
2023 	return rc;
2024 }
2025 
2026 /**
2027  * lpfc_debugfs_hbqinfo_open - Open the hbqinfo debugfs buffer
2028  * @inode: The inode pointer that contains a vport pointer.
2029  * @file: The file pointer to attach the log output.
2030  *
2031  * Description:
2032  * This routine is the entry point for the debugfs open file operation. It gets
2033  * the vport from the i_private field in @inode, allocates the necessary buffer
2034  * for the log, fills the buffer from the in-memory log for this vport, and then
2035  * returns a pointer to that log in the private_data field in @file.
2036  *
2037  * Returns:
2038  * This function returns zero if successful. On error it will return a negative
2039  * error value.
2040  **/
2041 static int
2042 lpfc_debugfs_hbqinfo_open(struct inode *inode, struct file *file)
2043 {
2044 	struct lpfc_hba *phba = inode->i_private;
2045 	struct lpfc_debug *debug;
2046 	int rc = -ENOMEM;
2047 
2048 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2049 	if (!debug)
2050 		goto out;
2051 
2052 	/* Round to page boundary */
2053 	debug->buffer = kmalloc(LPFC_HBQINFO_SIZE, GFP_KERNEL);
2054 	if (!debug->buffer) {
2055 		kfree(debug);
2056 		goto out;
2057 	}
2058 
2059 	debug->len = lpfc_debugfs_hbqinfo_data(phba, debug->buffer,
2060 		LPFC_HBQINFO_SIZE);
2061 	file->private_data = debug;
2062 
2063 	rc = 0;
2064 out:
2065 	return rc;
2066 }
2067 
2068 /**
2069  * lpfc_debugfs_multixripools_open - Open the multixripool debugfs buffer
2070  * @inode: The inode pointer that contains a hba pointer.
2071  * @file: The file pointer to attach the log output.
2072  *
2073  * Description:
2074  * This routine is the entry point for the debugfs open file operation. It gets
2075  * the hba from the i_private field in @inode, allocates the necessary buffer
2076  * for the log, fills the buffer from the in-memory log for this hba, and then
2077  * returns a pointer to that log in the private_data field in @file.
2078  *
2079  * Returns:
2080  * This function returns zero if successful. On error it will return a negative
2081  * error value.
2082  **/
2083 static int
2084 lpfc_debugfs_multixripools_open(struct inode *inode, struct file *file)
2085 {
2086 	struct lpfc_hba *phba = inode->i_private;
2087 	struct lpfc_debug *debug;
2088 	int rc = -ENOMEM;
2089 
2090 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2091 	if (!debug)
2092 		goto out;
2093 
2094 	/* Round to page boundary */
2095 	debug->buffer = kzalloc(LPFC_DUMP_MULTIXRIPOOL_SIZE, GFP_KERNEL);
2096 	if (!debug->buffer) {
2097 		kfree(debug);
2098 		goto out;
2099 	}
2100 
2101 	debug->len = lpfc_debugfs_multixripools_data(
2102 		phba, debug->buffer, LPFC_DUMP_MULTIXRIPOOL_SIZE);
2103 
2104 	debug->i_private = inode->i_private;
2105 	file->private_data = debug;
2106 
2107 	rc = 0;
2108 out:
2109 	return rc;
2110 }
2111 
2112 #ifdef LPFC_HDWQ_LOCK_STAT
2113 /**
2114  * lpfc_debugfs_lockstat_open - Open the lockstat debugfs buffer
2115  * @inode: The inode pointer that contains a vport pointer.
2116  * @file: The file pointer to attach the log output.
2117  *
2118  * Description:
2119  * This routine is the entry point for the debugfs open file operation. It gets
2120  * the vport from the i_private field in @inode, allocates the necessary buffer
2121  * for the log, fills the buffer from the in-memory log for this vport, and then
2122  * returns a pointer to that log in the private_data field in @file.
2123  *
2124  * Returns:
2125  * This function returns zero if successful. On error it will return a negative
2126  * error value.
2127  **/
2128 static int
2129 lpfc_debugfs_lockstat_open(struct inode *inode, struct file *file)
2130 {
2131 	struct lpfc_hba *phba = inode->i_private;
2132 	struct lpfc_debug *debug;
2133 	int rc = -ENOMEM;
2134 
2135 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2136 	if (!debug)
2137 		goto out;
2138 
2139 	/* Round to page boundary */
2140 	debug->buffer = kmalloc(LPFC_HDWQINFO_SIZE, GFP_KERNEL);
2141 	if (!debug->buffer) {
2142 		kfree(debug);
2143 		goto out;
2144 	}
2145 
2146 	debug->len = lpfc_debugfs_lockstat_data(phba, debug->buffer,
2147 		LPFC_HBQINFO_SIZE);
2148 	file->private_data = debug;
2149 
2150 	rc = 0;
2151 out:
2152 	return rc;
2153 }
2154 
2155 static ssize_t
2156 lpfc_debugfs_lockstat_write(struct file *file, const char __user *buf,
2157 			    size_t nbytes, loff_t *ppos)
2158 {
2159 	struct lpfc_debug *debug = file->private_data;
2160 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2161 	struct lpfc_sli4_hdw_queue *qp;
2162 	char mybuf[64];
2163 	char *pbuf;
2164 	int i;
2165 
2166 	/* Protect copy from user */
2167 	if (!access_ok(buf, nbytes))
2168 		return -EFAULT;
2169 
2170 	memset(mybuf, 0, sizeof(mybuf));
2171 
2172 	if (copy_from_user(mybuf, buf, nbytes))
2173 		return -EFAULT;
2174 	pbuf = &mybuf[0];
2175 
2176 	if ((strncmp(pbuf, "reset", strlen("reset")) == 0) ||
2177 	    (strncmp(pbuf, "zero", strlen("zero")) == 0)) {
2178 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
2179 			qp = &phba->sli4_hba.hdwq[i];
2180 			qp->lock_conflict.alloc_xri_get = 0;
2181 			qp->lock_conflict.alloc_xri_put = 0;
2182 			qp->lock_conflict.free_xri = 0;
2183 			qp->lock_conflict.wq_access = 0;
2184 			qp->lock_conflict.alloc_pvt_pool = 0;
2185 			qp->lock_conflict.mv_from_pvt_pool = 0;
2186 			qp->lock_conflict.mv_to_pub_pool = 0;
2187 			qp->lock_conflict.mv_to_pvt_pool = 0;
2188 			qp->lock_conflict.free_pvt_pool = 0;
2189 			qp->lock_conflict.free_pub_pool = 0;
2190 			qp->lock_conflict.wq_access = 0;
2191 		}
2192 	}
2193 	return nbytes;
2194 }
2195 #endif
2196 
2197 static int lpfc_debugfs_ras_log_data(struct lpfc_hba *phba,
2198 				     char *buffer, int size)
2199 {
2200 	int copied = 0;
2201 	struct lpfc_dmabuf *dmabuf, *next;
2202 
2203 	memset(buffer, 0, size);
2204 
2205 	spin_lock_irq(&phba->hbalock);
2206 	if (phba->ras_fwlog.state != ACTIVE) {
2207 		spin_unlock_irq(&phba->hbalock);
2208 		return -EINVAL;
2209 	}
2210 	spin_unlock_irq(&phba->hbalock);
2211 
2212 	list_for_each_entry_safe(dmabuf, next,
2213 				 &phba->ras_fwlog.fwlog_buff_list, list) {
2214 		/* Check if copying will go over size and a '\0' char */
2215 		if ((copied + LPFC_RAS_MAX_ENTRY_SIZE) >= (size - 1)) {
2216 			memcpy(buffer + copied, dmabuf->virt,
2217 			       size - copied - 1);
2218 			copied += size - copied - 1;
2219 			break;
2220 		}
2221 		memcpy(buffer + copied, dmabuf->virt, LPFC_RAS_MAX_ENTRY_SIZE);
2222 		copied += LPFC_RAS_MAX_ENTRY_SIZE;
2223 	}
2224 	return copied;
2225 }
2226 
2227 static int
2228 lpfc_debugfs_ras_log_release(struct inode *inode, struct file *file)
2229 {
2230 	struct lpfc_debug *debug = file->private_data;
2231 
2232 	vfree(debug->buffer);
2233 	kfree(debug);
2234 
2235 	return 0;
2236 }
2237 
2238 /**
2239  * lpfc_debugfs_ras_log_open - Open the RAS log debugfs buffer
2240  * @inode: The inode pointer that contains a vport pointer.
2241  * @file: The file pointer to attach the log output.
2242  *
2243  * Description:
2244  * This routine is the entry point for the debugfs open file operation. It gets
2245  * the vport from the i_private field in @inode, allocates the necessary buffer
2246  * for the log, fills the buffer from the in-memory log for this vport, and then
2247  * returns a pointer to that log in the private_data field in @file.
2248  *
2249  * Returns:
2250  * This function returns zero if successful. On error it will return a negative
2251  * error value.
2252  **/
2253 static int
2254 lpfc_debugfs_ras_log_open(struct inode *inode, struct file *file)
2255 {
2256 	struct lpfc_hba *phba = inode->i_private;
2257 	struct lpfc_debug *debug;
2258 	int size;
2259 	int rc = -ENOMEM;
2260 
2261 	spin_lock_irq(&phba->hbalock);
2262 	if (phba->ras_fwlog.state != ACTIVE) {
2263 		spin_unlock_irq(&phba->hbalock);
2264 		rc = -EINVAL;
2265 		goto out;
2266 	}
2267 	spin_unlock_irq(&phba->hbalock);
2268 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2269 	if (!debug)
2270 		goto out;
2271 
2272 	size = LPFC_RAS_MIN_BUFF_POST_SIZE * phba->cfg_ras_fwlog_buffsize;
2273 	debug->buffer = vmalloc(size);
2274 	if (!debug->buffer)
2275 		goto free_debug;
2276 
2277 	debug->len = lpfc_debugfs_ras_log_data(phba, debug->buffer, size);
2278 	if (debug->len < 0) {
2279 		rc = -EINVAL;
2280 		goto free_buffer;
2281 	}
2282 	file->private_data = debug;
2283 
2284 	return 0;
2285 
2286 free_buffer:
2287 	vfree(debug->buffer);
2288 free_debug:
2289 	kfree(debug);
2290 out:
2291 	return rc;
2292 }
2293 
2294 /**
2295  * lpfc_debugfs_dumpHBASlim_open - Open the Dump HBA SLIM debugfs buffer
2296  * @inode: The inode pointer that contains a vport pointer.
2297  * @file: The file pointer to attach the log output.
2298  *
2299  * Description:
2300  * This routine is the entry point for the debugfs open file operation. It gets
2301  * the vport from the i_private field in @inode, allocates the necessary buffer
2302  * for the log, fills the buffer from the in-memory log for this vport, and then
2303  * returns a pointer to that log in the private_data field in @file.
2304  *
2305  * Returns:
2306  * This function returns zero if successful. On error it will return a negative
2307  * error value.
2308  **/
2309 static int
2310 lpfc_debugfs_dumpHBASlim_open(struct inode *inode, struct file *file)
2311 {
2312 	struct lpfc_hba *phba = inode->i_private;
2313 	struct lpfc_debug *debug;
2314 	int rc = -ENOMEM;
2315 
2316 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2317 	if (!debug)
2318 		goto out;
2319 
2320 	/* Round to page boundary */
2321 	debug->buffer = kmalloc(LPFC_DUMPHBASLIM_SIZE, GFP_KERNEL);
2322 	if (!debug->buffer) {
2323 		kfree(debug);
2324 		goto out;
2325 	}
2326 
2327 	debug->len = lpfc_debugfs_dumpHBASlim_data(phba, debug->buffer,
2328 		LPFC_DUMPHBASLIM_SIZE);
2329 	file->private_data = debug;
2330 
2331 	rc = 0;
2332 out:
2333 	return rc;
2334 }
2335 
2336 /**
2337  * lpfc_debugfs_dumpHostSlim_open - Open the Dump Host SLIM debugfs buffer
2338  * @inode: The inode pointer that contains a vport pointer.
2339  * @file: The file pointer to attach the log output.
2340  *
2341  * Description:
2342  * This routine is the entry point for the debugfs open file operation. It gets
2343  * the vport from the i_private field in @inode, allocates the necessary buffer
2344  * for the log, fills the buffer from the in-memory log for this vport, and then
2345  * returns a pointer to that log in the private_data field in @file.
2346  *
2347  * Returns:
2348  * This function returns zero if successful. On error it will return a negative
2349  * error value.
2350  **/
2351 static int
2352 lpfc_debugfs_dumpHostSlim_open(struct inode *inode, struct file *file)
2353 {
2354 	struct lpfc_hba *phba = inode->i_private;
2355 	struct lpfc_debug *debug;
2356 	int rc = -ENOMEM;
2357 
2358 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2359 	if (!debug)
2360 		goto out;
2361 
2362 	/* Round to page boundary */
2363 	debug->buffer = kmalloc(LPFC_DUMPHOSTSLIM_SIZE, GFP_KERNEL);
2364 	if (!debug->buffer) {
2365 		kfree(debug);
2366 		goto out;
2367 	}
2368 
2369 	debug->len = lpfc_debugfs_dumpHostSlim_data(phba, debug->buffer,
2370 		LPFC_DUMPHOSTSLIM_SIZE);
2371 	file->private_data = debug;
2372 
2373 	rc = 0;
2374 out:
2375 	return rc;
2376 }
2377 
2378 static ssize_t
2379 lpfc_debugfs_dif_err_read(struct file *file, char __user *buf,
2380 	size_t nbytes, loff_t *ppos)
2381 {
2382 	struct dentry *dent = file->f_path.dentry;
2383 	struct lpfc_hba *phba = file->private_data;
2384 	char cbuf[32];
2385 	uint64_t tmp = 0;
2386 	int cnt = 0;
2387 
2388 	if (dent == phba->debug_writeGuard)
2389 		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wgrd_cnt);
2390 	else if (dent == phba->debug_writeApp)
2391 		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wapp_cnt);
2392 	else if (dent == phba->debug_writeRef)
2393 		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wref_cnt);
2394 	else if (dent == phba->debug_readGuard)
2395 		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rgrd_cnt);
2396 	else if (dent == phba->debug_readApp)
2397 		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rapp_cnt);
2398 	else if (dent == phba->debug_readRef)
2399 		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rref_cnt);
2400 	else if (dent == phba->debug_InjErrNPortID)
2401 		cnt = scnprintf(cbuf, 32, "0x%06x\n",
2402 				phba->lpfc_injerr_nportid);
2403 	else if (dent == phba->debug_InjErrWWPN) {
2404 		memcpy(&tmp, &phba->lpfc_injerr_wwpn, sizeof(struct lpfc_name));
2405 		tmp = cpu_to_be64(tmp);
2406 		cnt = scnprintf(cbuf, 32, "0x%016llx\n", tmp);
2407 	} else if (dent == phba->debug_InjErrLBA) {
2408 		if (phba->lpfc_injerr_lba == (sector_t)(-1))
2409 			cnt = scnprintf(cbuf, 32, "off\n");
2410 		else
2411 			cnt = scnprintf(cbuf, 32, "0x%llx\n",
2412 				 (uint64_t) phba->lpfc_injerr_lba);
2413 	} else
2414 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2415 			 "0547 Unknown debugfs error injection entry\n");
2416 
2417 	return simple_read_from_buffer(buf, nbytes, ppos, &cbuf, cnt);
2418 }
2419 
2420 static ssize_t
2421 lpfc_debugfs_dif_err_write(struct file *file, const char __user *buf,
2422 	size_t nbytes, loff_t *ppos)
2423 {
2424 	struct dentry *dent = file->f_path.dentry;
2425 	struct lpfc_hba *phba = file->private_data;
2426 	char dstbuf[33];
2427 	uint64_t tmp = 0;
2428 	int size;
2429 
2430 	memset(dstbuf, 0, 33);
2431 	size = (nbytes < 32) ? nbytes : 32;
2432 	if (copy_from_user(dstbuf, buf, size))
2433 		return 0;
2434 
2435 	if (dent == phba->debug_InjErrLBA) {
2436 		if ((buf[0] == 'o') && (buf[1] == 'f') && (buf[2] == 'f'))
2437 			tmp = (uint64_t)(-1);
2438 	}
2439 
2440 	if ((tmp == 0) && (kstrtoull(dstbuf, 0, &tmp)))
2441 		return 0;
2442 
2443 	if (dent == phba->debug_writeGuard)
2444 		phba->lpfc_injerr_wgrd_cnt = (uint32_t)tmp;
2445 	else if (dent == phba->debug_writeApp)
2446 		phba->lpfc_injerr_wapp_cnt = (uint32_t)tmp;
2447 	else if (dent == phba->debug_writeRef)
2448 		phba->lpfc_injerr_wref_cnt = (uint32_t)tmp;
2449 	else if (dent == phba->debug_readGuard)
2450 		phba->lpfc_injerr_rgrd_cnt = (uint32_t)tmp;
2451 	else if (dent == phba->debug_readApp)
2452 		phba->lpfc_injerr_rapp_cnt = (uint32_t)tmp;
2453 	else if (dent == phba->debug_readRef)
2454 		phba->lpfc_injerr_rref_cnt = (uint32_t)tmp;
2455 	else if (dent == phba->debug_InjErrLBA)
2456 		phba->lpfc_injerr_lba = (sector_t)tmp;
2457 	else if (dent == phba->debug_InjErrNPortID)
2458 		phba->lpfc_injerr_nportid = (uint32_t)(tmp & Mask_DID);
2459 	else if (dent == phba->debug_InjErrWWPN) {
2460 		tmp = cpu_to_be64(tmp);
2461 		memcpy(&phba->lpfc_injerr_wwpn, &tmp, sizeof(struct lpfc_name));
2462 	} else
2463 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2464 			 "0548 Unknown debugfs error injection entry\n");
2465 
2466 	return nbytes;
2467 }
2468 
2469 static int
2470 lpfc_debugfs_dif_err_release(struct inode *inode, struct file *file)
2471 {
2472 	return 0;
2473 }
2474 
2475 /**
2476  * lpfc_debugfs_nodelist_open - Open the nodelist debugfs file
2477  * @inode: The inode pointer that contains a vport pointer.
2478  * @file: The file pointer to attach the log output.
2479  *
2480  * Description:
2481  * This routine is the entry point for the debugfs open file operation. It gets
2482  * the vport from the i_private field in @inode, allocates the necessary buffer
2483  * for the log, fills the buffer from the in-memory log for this vport, and then
2484  * returns a pointer to that log in the private_data field in @file.
2485  *
2486  * Returns:
2487  * This function returns zero if successful. On error it will return a negative
2488  * error value.
2489  **/
2490 static int
2491 lpfc_debugfs_nodelist_open(struct inode *inode, struct file *file)
2492 {
2493 	struct lpfc_vport *vport = inode->i_private;
2494 	struct lpfc_debug *debug;
2495 	int rc = -ENOMEM;
2496 
2497 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2498 	if (!debug)
2499 		goto out;
2500 
2501 	/* Round to page boundary */
2502 	debug->buffer = kmalloc(LPFC_NODELIST_SIZE, GFP_KERNEL);
2503 	if (!debug->buffer) {
2504 		kfree(debug);
2505 		goto out;
2506 	}
2507 
2508 	debug->len = lpfc_debugfs_nodelist_data(vport, debug->buffer,
2509 		LPFC_NODELIST_SIZE);
2510 	file->private_data = debug;
2511 
2512 	rc = 0;
2513 out:
2514 	return rc;
2515 }
2516 
2517 /**
2518  * lpfc_debugfs_lseek - Seek through a debugfs file
2519  * @file: The file pointer to seek through.
2520  * @off: The offset to seek to or the amount to seek by.
2521  * @whence: Indicates how to seek.
2522  *
2523  * Description:
2524  * This routine is the entry point for the debugfs lseek file operation. The
2525  * @whence parameter indicates whether @off is the offset to directly seek to,
2526  * or if it is a value to seek forward or reverse by. This function figures out
2527  * what the new offset of the debugfs file will be and assigns that value to the
2528  * f_pos field of @file.
2529  *
2530  * Returns:
2531  * This function returns the new offset if successful and returns a negative
2532  * error if unable to process the seek.
2533  **/
2534 static loff_t
2535 lpfc_debugfs_lseek(struct file *file, loff_t off, int whence)
2536 {
2537 	struct lpfc_debug *debug = file->private_data;
2538 	return fixed_size_llseek(file, off, whence, debug->len);
2539 }
2540 
2541 /**
2542  * lpfc_debugfs_read - Read a debugfs file
2543  * @file: The file pointer to read from.
2544  * @buf: The buffer to copy the data to.
2545  * @nbytes: The number of bytes to read.
2546  * @ppos: The position in the file to start reading from.
2547  *
2548  * Description:
2549  * This routine reads data from from the buffer indicated in the private_data
2550  * field of @file. It will start reading at @ppos and copy up to @nbytes of
2551  * data to @buf.
2552  *
2553  * Returns:
2554  * This function returns the amount of data that was read (this could be less
2555  * than @nbytes if the end of the file was reached) or a negative error value.
2556  **/
2557 static ssize_t
2558 lpfc_debugfs_read(struct file *file, char __user *buf,
2559 		  size_t nbytes, loff_t *ppos)
2560 {
2561 	struct lpfc_debug *debug = file->private_data;
2562 
2563 	return simple_read_from_buffer(buf, nbytes, ppos, debug->buffer,
2564 				       debug->len);
2565 }
2566 
2567 /**
2568  * lpfc_debugfs_release - Release the buffer used to store debugfs file data
2569  * @inode: The inode pointer that contains a vport pointer. (unused)
2570  * @file: The file pointer that contains the buffer to release.
2571  *
2572  * Description:
2573  * This routine frees the buffer that was allocated when the debugfs file was
2574  * opened.
2575  *
2576  * Returns:
2577  * This function returns zero.
2578  **/
2579 static int
2580 lpfc_debugfs_release(struct inode *inode, struct file *file)
2581 {
2582 	struct lpfc_debug *debug = file->private_data;
2583 
2584 	kfree(debug->buffer);
2585 	kfree(debug);
2586 
2587 	return 0;
2588 }
2589 
2590 /**
2591  * lpfc_debugfs_multixripools_write - Clear multi-XRI pools statistics
2592  * @file: The file pointer to read from.
2593  * @buf: The buffer to copy the user data from.
2594  * @nbytes: The number of bytes to get.
2595  * @ppos: The position in the file to start reading from.
2596  *
2597  * Description:
2598  * This routine clears multi-XRI pools statistics when buf contains "clear".
2599  *
2600  * Return Value:
2601  * It returns the @nbytges passing in from debugfs user space when successful.
2602  * In case of error conditions, it returns proper error code back to the user
2603  * space.
2604  **/
2605 static ssize_t
2606 lpfc_debugfs_multixripools_write(struct file *file, const char __user *buf,
2607 				 size_t nbytes, loff_t *ppos)
2608 {
2609 	struct lpfc_debug *debug = file->private_data;
2610 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2611 	char mybuf[64];
2612 	char *pbuf;
2613 	u32 i;
2614 	u32 hwq_count;
2615 	struct lpfc_sli4_hdw_queue *qp;
2616 	struct lpfc_multixri_pool *multixri_pool;
2617 
2618 	if (nbytes > 64)
2619 		nbytes = 64;
2620 
2621 	/* Protect copy from user */
2622 	if (!access_ok(buf, nbytes))
2623 		return -EFAULT;
2624 
2625 	memset(mybuf, 0, sizeof(mybuf));
2626 
2627 	if (copy_from_user(mybuf, buf, nbytes))
2628 		return -EFAULT;
2629 	pbuf = &mybuf[0];
2630 
2631 	if ((strncmp(pbuf, "clear", strlen("clear"))) == 0) {
2632 		hwq_count = phba->cfg_hdw_queue;
2633 		for (i = 0; i < hwq_count; i++) {
2634 			qp = &phba->sli4_hba.hdwq[i];
2635 			multixri_pool = qp->p_multixri_pool;
2636 			if (!multixri_pool)
2637 				continue;
2638 
2639 			qp->empty_io_bufs = 0;
2640 			multixri_pool->pbl_empty_count = 0;
2641 #ifdef LPFC_MXP_STAT
2642 			multixri_pool->above_limit_count = 0;
2643 			multixri_pool->below_limit_count = 0;
2644 			multixri_pool->stat_max_hwm = 0;
2645 			multixri_pool->local_pbl_hit_count = 0;
2646 			multixri_pool->other_pbl_hit_count = 0;
2647 
2648 			multixri_pool->stat_pbl_count = 0;
2649 			multixri_pool->stat_pvt_count = 0;
2650 			multixri_pool->stat_busy_count = 0;
2651 			multixri_pool->stat_snapshot_taken = 0;
2652 #endif
2653 		}
2654 		return strlen(pbuf);
2655 	}
2656 
2657 	return -EINVAL;
2658 }
2659 
2660 static int
2661 lpfc_debugfs_nvmestat_open(struct inode *inode, struct file *file)
2662 {
2663 	struct lpfc_vport *vport = inode->i_private;
2664 	struct lpfc_debug *debug;
2665 	int rc = -ENOMEM;
2666 
2667 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2668 	if (!debug)
2669 		goto out;
2670 
2671 	 /* Round to page boundary */
2672 	debug->buffer = kmalloc(LPFC_NVMESTAT_SIZE, GFP_KERNEL);
2673 	if (!debug->buffer) {
2674 		kfree(debug);
2675 		goto out;
2676 	}
2677 
2678 	debug->len = lpfc_debugfs_nvmestat_data(vport, debug->buffer,
2679 		LPFC_NVMESTAT_SIZE);
2680 
2681 	debug->i_private = inode->i_private;
2682 	file->private_data = debug;
2683 
2684 	rc = 0;
2685 out:
2686 	return rc;
2687 }
2688 
2689 static ssize_t
2690 lpfc_debugfs_nvmestat_write(struct file *file, const char __user *buf,
2691 			    size_t nbytes, loff_t *ppos)
2692 {
2693 	struct lpfc_debug *debug = file->private_data;
2694 	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2695 	struct lpfc_hba   *phba = vport->phba;
2696 	struct lpfc_nvmet_tgtport *tgtp;
2697 	char mybuf[64];
2698 	char *pbuf;
2699 
2700 	if (!phba->targetport)
2701 		return -ENXIO;
2702 
2703 	if (nbytes > 64)
2704 		nbytes = 64;
2705 
2706 	memset(mybuf, 0, sizeof(mybuf));
2707 
2708 	if (copy_from_user(mybuf, buf, nbytes))
2709 		return -EFAULT;
2710 	pbuf = &mybuf[0];
2711 
2712 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2713 	if ((strncmp(pbuf, "reset", strlen("reset")) == 0) ||
2714 	    (strncmp(pbuf, "zero", strlen("zero")) == 0)) {
2715 		atomic_set(&tgtp->rcv_ls_req_in, 0);
2716 		atomic_set(&tgtp->rcv_ls_req_out, 0);
2717 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
2718 		atomic_set(&tgtp->xmt_ls_abort, 0);
2719 		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
2720 		atomic_set(&tgtp->xmt_ls_rsp, 0);
2721 		atomic_set(&tgtp->xmt_ls_drop, 0);
2722 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
2723 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
2724 
2725 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
2726 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
2727 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
2728 		atomic_set(&tgtp->xmt_fcp_drop, 0);
2729 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
2730 		atomic_set(&tgtp->xmt_fcp_read, 0);
2731 		atomic_set(&tgtp->xmt_fcp_write, 0);
2732 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
2733 		atomic_set(&tgtp->xmt_fcp_release, 0);
2734 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
2735 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
2736 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
2737 
2738 		atomic_set(&tgtp->xmt_fcp_abort, 0);
2739 		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
2740 		atomic_set(&tgtp->xmt_abort_sol, 0);
2741 		atomic_set(&tgtp->xmt_abort_unsol, 0);
2742 		atomic_set(&tgtp->xmt_abort_rsp, 0);
2743 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
2744 	}
2745 	return nbytes;
2746 }
2747 
2748 static int
2749 lpfc_debugfs_scsistat_open(struct inode *inode, struct file *file)
2750 {
2751 	struct lpfc_vport *vport = inode->i_private;
2752 	struct lpfc_debug *debug;
2753 	int rc = -ENOMEM;
2754 
2755 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2756 	if (!debug)
2757 		goto out;
2758 
2759 	 /* Round to page boundary */
2760 	debug->buffer = kzalloc(LPFC_SCSISTAT_SIZE, GFP_KERNEL);
2761 	if (!debug->buffer) {
2762 		kfree(debug);
2763 		goto out;
2764 	}
2765 
2766 	debug->len = lpfc_debugfs_scsistat_data(vport, debug->buffer,
2767 		LPFC_SCSISTAT_SIZE);
2768 
2769 	debug->i_private = inode->i_private;
2770 	file->private_data = debug;
2771 
2772 	rc = 0;
2773 out:
2774 	return rc;
2775 }
2776 
2777 static ssize_t
2778 lpfc_debugfs_scsistat_write(struct file *file, const char __user *buf,
2779 			    size_t nbytes, loff_t *ppos)
2780 {
2781 	struct lpfc_debug *debug = file->private_data;
2782 	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2783 	struct lpfc_hba *phba = vport->phba;
2784 	char mybuf[6] = {0};
2785 	int i;
2786 
2787 	/* Protect copy from user */
2788 	if (!access_ok(buf, nbytes))
2789 		return -EFAULT;
2790 
2791 	if (copy_from_user(mybuf, buf, (nbytes >= sizeof(mybuf)) ?
2792 				       (sizeof(mybuf) - 1) : nbytes))
2793 		return -EFAULT;
2794 
2795 	if ((strncmp(&mybuf[0], "reset", strlen("reset")) == 0) ||
2796 	    (strncmp(&mybuf[0], "zero", strlen("zero")) == 0)) {
2797 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
2798 			memset(&phba->sli4_hba.hdwq[i].scsi_cstat, 0,
2799 			       sizeof(phba->sli4_hba.hdwq[i].scsi_cstat));
2800 		}
2801 	}
2802 
2803 	return nbytes;
2804 }
2805 
2806 static int
2807 lpfc_debugfs_ioktime_open(struct inode *inode, struct file *file)
2808 {
2809 	struct lpfc_vport *vport = inode->i_private;
2810 	struct lpfc_debug *debug;
2811 	int rc = -ENOMEM;
2812 
2813 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2814 	if (!debug)
2815 		goto out;
2816 
2817 	 /* Round to page boundary */
2818 	debug->buffer = kmalloc(LPFC_IOKTIME_SIZE, GFP_KERNEL);
2819 	if (!debug->buffer) {
2820 		kfree(debug);
2821 		goto out;
2822 	}
2823 
2824 	debug->len = lpfc_debugfs_ioktime_data(vport, debug->buffer,
2825 		LPFC_IOKTIME_SIZE);
2826 
2827 	debug->i_private = inode->i_private;
2828 	file->private_data = debug;
2829 
2830 	rc = 0;
2831 out:
2832 	return rc;
2833 }
2834 
2835 static ssize_t
2836 lpfc_debugfs_ioktime_write(struct file *file, const char __user *buf,
2837 			   size_t nbytes, loff_t *ppos)
2838 {
2839 	struct lpfc_debug *debug = file->private_data;
2840 	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2841 	struct lpfc_hba   *phba = vport->phba;
2842 	char mybuf[64];
2843 	char *pbuf;
2844 
2845 	if (nbytes > 64)
2846 		nbytes = 64;
2847 
2848 	memset(mybuf, 0, sizeof(mybuf));
2849 
2850 	if (copy_from_user(mybuf, buf, nbytes))
2851 		return -EFAULT;
2852 	pbuf = &mybuf[0];
2853 
2854 	if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2855 		phba->ktime_data_samples = 0;
2856 		phba->ktime_status_samples = 0;
2857 		phba->ktime_seg1_total = 0;
2858 		phba->ktime_seg1_max = 0;
2859 		phba->ktime_seg1_min = 0xffffffff;
2860 		phba->ktime_seg2_total = 0;
2861 		phba->ktime_seg2_max = 0;
2862 		phba->ktime_seg2_min = 0xffffffff;
2863 		phba->ktime_seg3_total = 0;
2864 		phba->ktime_seg3_max = 0;
2865 		phba->ktime_seg3_min = 0xffffffff;
2866 		phba->ktime_seg4_total = 0;
2867 		phba->ktime_seg4_max = 0;
2868 		phba->ktime_seg4_min = 0xffffffff;
2869 		phba->ktime_seg5_total = 0;
2870 		phba->ktime_seg5_max = 0;
2871 		phba->ktime_seg5_min = 0xffffffff;
2872 		phba->ktime_seg6_total = 0;
2873 		phba->ktime_seg6_max = 0;
2874 		phba->ktime_seg6_min = 0xffffffff;
2875 		phba->ktime_seg7_total = 0;
2876 		phba->ktime_seg7_max = 0;
2877 		phba->ktime_seg7_min = 0xffffffff;
2878 		phba->ktime_seg8_total = 0;
2879 		phba->ktime_seg8_max = 0;
2880 		phba->ktime_seg8_min = 0xffffffff;
2881 		phba->ktime_seg9_total = 0;
2882 		phba->ktime_seg9_max = 0;
2883 		phba->ktime_seg9_min = 0xffffffff;
2884 		phba->ktime_seg10_total = 0;
2885 		phba->ktime_seg10_max = 0;
2886 		phba->ktime_seg10_min = 0xffffffff;
2887 
2888 		phba->ktime_on = 1;
2889 		return strlen(pbuf);
2890 	} else if ((strncmp(pbuf, "off",
2891 		   sizeof("off") - 1) == 0)) {
2892 		phba->ktime_on = 0;
2893 		return strlen(pbuf);
2894 	} else if ((strncmp(pbuf, "zero",
2895 		   sizeof("zero") - 1) == 0)) {
2896 		phba->ktime_data_samples = 0;
2897 		phba->ktime_status_samples = 0;
2898 		phba->ktime_seg1_total = 0;
2899 		phba->ktime_seg1_max = 0;
2900 		phba->ktime_seg1_min = 0xffffffff;
2901 		phba->ktime_seg2_total = 0;
2902 		phba->ktime_seg2_max = 0;
2903 		phba->ktime_seg2_min = 0xffffffff;
2904 		phba->ktime_seg3_total = 0;
2905 		phba->ktime_seg3_max = 0;
2906 		phba->ktime_seg3_min = 0xffffffff;
2907 		phba->ktime_seg4_total = 0;
2908 		phba->ktime_seg4_max = 0;
2909 		phba->ktime_seg4_min = 0xffffffff;
2910 		phba->ktime_seg5_total = 0;
2911 		phba->ktime_seg5_max = 0;
2912 		phba->ktime_seg5_min = 0xffffffff;
2913 		phba->ktime_seg6_total = 0;
2914 		phba->ktime_seg6_max = 0;
2915 		phba->ktime_seg6_min = 0xffffffff;
2916 		phba->ktime_seg7_total = 0;
2917 		phba->ktime_seg7_max = 0;
2918 		phba->ktime_seg7_min = 0xffffffff;
2919 		phba->ktime_seg8_total = 0;
2920 		phba->ktime_seg8_max = 0;
2921 		phba->ktime_seg8_min = 0xffffffff;
2922 		phba->ktime_seg9_total = 0;
2923 		phba->ktime_seg9_max = 0;
2924 		phba->ktime_seg9_min = 0xffffffff;
2925 		phba->ktime_seg10_total = 0;
2926 		phba->ktime_seg10_max = 0;
2927 		phba->ktime_seg10_min = 0xffffffff;
2928 		return strlen(pbuf);
2929 	}
2930 	return -EINVAL;
2931 }
2932 
2933 static int
2934 lpfc_debugfs_nvmeio_trc_open(struct inode *inode, struct file *file)
2935 {
2936 	struct lpfc_hba *phba = inode->i_private;
2937 	struct lpfc_debug *debug;
2938 	int rc = -ENOMEM;
2939 
2940 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2941 	if (!debug)
2942 		goto out;
2943 
2944 	 /* Round to page boundary */
2945 	debug->buffer = kmalloc(LPFC_NVMEIO_TRC_SIZE, GFP_KERNEL);
2946 	if (!debug->buffer) {
2947 		kfree(debug);
2948 		goto out;
2949 	}
2950 
2951 	debug->len = lpfc_debugfs_nvmeio_trc_data(phba, debug->buffer,
2952 		LPFC_NVMEIO_TRC_SIZE);
2953 
2954 	debug->i_private = inode->i_private;
2955 	file->private_data = debug;
2956 
2957 	rc = 0;
2958 out:
2959 	return rc;
2960 }
2961 
2962 static ssize_t
2963 lpfc_debugfs_nvmeio_trc_write(struct file *file, const char __user *buf,
2964 			      size_t nbytes, loff_t *ppos)
2965 {
2966 	struct lpfc_debug *debug = file->private_data;
2967 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2968 	int i;
2969 	unsigned long sz;
2970 	char mybuf[64];
2971 	char *pbuf;
2972 
2973 	if (nbytes > 64)
2974 		nbytes = 64;
2975 
2976 	memset(mybuf, 0, sizeof(mybuf));
2977 
2978 	if (copy_from_user(mybuf, buf, nbytes))
2979 		return -EFAULT;
2980 	pbuf = &mybuf[0];
2981 
2982 	if ((strncmp(pbuf, "off", sizeof("off") - 1) == 0)) {
2983 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2984 				"0570 nvmeio_trc_off\n");
2985 		phba->nvmeio_trc_output_idx = 0;
2986 		phba->nvmeio_trc_on = 0;
2987 		return strlen(pbuf);
2988 	} else if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2989 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2990 				"0571 nvmeio_trc_on\n");
2991 		phba->nvmeio_trc_output_idx = 0;
2992 		phba->nvmeio_trc_on = 1;
2993 		return strlen(pbuf);
2994 	}
2995 
2996 	/* We must be off to allocate the trace buffer */
2997 	if (phba->nvmeio_trc_on != 0)
2998 		return -EINVAL;
2999 
3000 	/* If not on or off, the parameter is the trace buffer size */
3001 	i = kstrtoul(pbuf, 0, &sz);
3002 	if (i)
3003 		return -EINVAL;
3004 	phba->nvmeio_trc_size = (uint32_t)sz;
3005 
3006 	/* It must be a power of 2 - round down */
3007 	i = 0;
3008 	while (sz > 1) {
3009 		sz = sz >> 1;
3010 		i++;
3011 	}
3012 	sz = (1 << i);
3013 	if (phba->nvmeio_trc_size != sz)
3014 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3015 				"0572 nvmeio_trc_size changed to %ld\n",
3016 				sz);
3017 	phba->nvmeio_trc_size = (uint32_t)sz;
3018 
3019 	/* If one previously exists, free it */
3020 	kfree(phba->nvmeio_trc);
3021 
3022 	/* Allocate new trace buffer and initialize */
3023 	phba->nvmeio_trc = kzalloc((sizeof(struct lpfc_debugfs_nvmeio_trc) *
3024 				    sz), GFP_KERNEL);
3025 	if (!phba->nvmeio_trc) {
3026 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3027 				"0573 Cannot create debugfs "
3028 				"nvmeio_trc buffer\n");
3029 		return -ENOMEM;
3030 	}
3031 	atomic_set(&phba->nvmeio_trc_cnt, 0);
3032 	phba->nvmeio_trc_on = 0;
3033 	phba->nvmeio_trc_output_idx = 0;
3034 
3035 	return strlen(pbuf);
3036 }
3037 
3038 static int
3039 lpfc_debugfs_hdwqstat_open(struct inode *inode, struct file *file)
3040 {
3041 	struct lpfc_vport *vport = inode->i_private;
3042 	struct lpfc_debug *debug;
3043 	int rc = -ENOMEM;
3044 
3045 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
3046 	if (!debug)
3047 		goto out;
3048 
3049 	 /* Round to page boundary */
3050 	debug->buffer = kcalloc(1, LPFC_SCSISTAT_SIZE, GFP_KERNEL);
3051 	if (!debug->buffer) {
3052 		kfree(debug);
3053 		goto out;
3054 	}
3055 
3056 	debug->len = lpfc_debugfs_hdwqstat_data(vport, debug->buffer,
3057 						LPFC_SCSISTAT_SIZE);
3058 
3059 	debug->i_private = inode->i_private;
3060 	file->private_data = debug;
3061 
3062 	rc = 0;
3063 out:
3064 	return rc;
3065 }
3066 
3067 static ssize_t
3068 lpfc_debugfs_hdwqstat_write(struct file *file, const char __user *buf,
3069 			    size_t nbytes, loff_t *ppos)
3070 {
3071 	struct lpfc_debug *debug = file->private_data;
3072 	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
3073 	struct lpfc_hba   *phba = vport->phba;
3074 	struct lpfc_hdwq_stat *c_stat;
3075 	char mybuf[64];
3076 	char *pbuf;
3077 	int i;
3078 
3079 	if (nbytes > 64)
3080 		nbytes = 64;
3081 
3082 	memset(mybuf, 0, sizeof(mybuf));
3083 
3084 	if (copy_from_user(mybuf, buf, nbytes))
3085 		return -EFAULT;
3086 	pbuf = &mybuf[0];
3087 
3088 	if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
3089 		if (phba->nvmet_support)
3090 			phba->hdwqstat_on |= LPFC_CHECK_NVMET_IO;
3091 		else
3092 			phba->hdwqstat_on |= (LPFC_CHECK_NVME_IO |
3093 				LPFC_CHECK_SCSI_IO);
3094 		return strlen(pbuf);
3095 	} else if ((strncmp(pbuf, "nvme_on", sizeof("nvme_on") - 1) == 0)) {
3096 		if (phba->nvmet_support)
3097 			phba->hdwqstat_on |= LPFC_CHECK_NVMET_IO;
3098 		else
3099 			phba->hdwqstat_on |= LPFC_CHECK_NVME_IO;
3100 		return strlen(pbuf);
3101 	} else if ((strncmp(pbuf, "scsi_on", sizeof("scsi_on") - 1) == 0)) {
3102 		if (!phba->nvmet_support)
3103 			phba->hdwqstat_on |= LPFC_CHECK_SCSI_IO;
3104 		return strlen(pbuf);
3105 	} else if ((strncmp(pbuf, "nvme_off", sizeof("nvme_off") - 1) == 0)) {
3106 		phba->hdwqstat_on &= ~(LPFC_CHECK_NVME_IO |
3107 				       LPFC_CHECK_NVMET_IO);
3108 		return strlen(pbuf);
3109 	} else if ((strncmp(pbuf, "scsi_off", sizeof("scsi_off") - 1) == 0)) {
3110 		phba->hdwqstat_on &= ~LPFC_CHECK_SCSI_IO;
3111 		return strlen(pbuf);
3112 	} else if ((strncmp(pbuf, "off",
3113 		   sizeof("off") - 1) == 0)) {
3114 		phba->hdwqstat_on = LPFC_CHECK_OFF;
3115 		return strlen(pbuf);
3116 	} else if ((strncmp(pbuf, "zero",
3117 		   sizeof("zero") - 1) == 0)) {
3118 		for_each_present_cpu(i) {
3119 			c_stat = per_cpu_ptr(phba->sli4_hba.c_stat, i);
3120 			c_stat->xmt_io = 0;
3121 			c_stat->cmpl_io = 0;
3122 			c_stat->rcv_io = 0;
3123 		}
3124 		return strlen(pbuf);
3125 	}
3126 	return -EINVAL;
3127 }
3128 
3129 /*
3130  * ---------------------------------
3131  * iDiag debugfs file access methods
3132  * ---------------------------------
3133  *
3134  * All access methods are through the proper SLI4 PCI function's debugfs
3135  * iDiag directory:
3136  *
3137  *     /sys/kernel/debug/lpfc/fn<#>/iDiag
3138  */
3139 
3140 /**
3141  * lpfc_idiag_cmd_get - Get and parse idiag debugfs comands from user space
3142  * @buf: The pointer to the user space buffer.
3143  * @nbytes: The number of bytes in the user space buffer.
3144  * @idiag_cmd: pointer to the idiag command struct.
3145  *
3146  * This routine reads data from debugfs user space buffer and parses the
3147  * buffer for getting the idiag command and arguments. The while space in
3148  * between the set of data is used as the parsing separator.
3149  *
3150  * This routine returns 0 when successful, it returns proper error code
3151  * back to the user space in error conditions.
3152  */
3153 static int lpfc_idiag_cmd_get(const char __user *buf, size_t nbytes,
3154 			      struct lpfc_idiag_cmd *idiag_cmd)
3155 {
3156 	char mybuf[64];
3157 	char *pbuf, *step_str;
3158 	int i;
3159 	size_t bsize;
3160 
3161 	memset(mybuf, 0, sizeof(mybuf));
3162 	memset(idiag_cmd, 0, sizeof(*idiag_cmd));
3163 	bsize = min(nbytes, (sizeof(mybuf)-1));
3164 
3165 	if (copy_from_user(mybuf, buf, bsize))
3166 		return -EFAULT;
3167 	pbuf = &mybuf[0];
3168 	step_str = strsep(&pbuf, "\t ");
3169 
3170 	/* The opcode must present */
3171 	if (!step_str)
3172 		return -EINVAL;
3173 
3174 	idiag_cmd->opcode = simple_strtol(step_str, NULL, 0);
3175 	if (idiag_cmd->opcode == 0)
3176 		return -EINVAL;
3177 
3178 	for (i = 0; i < LPFC_IDIAG_CMD_DATA_SIZE; i++) {
3179 		step_str = strsep(&pbuf, "\t ");
3180 		if (!step_str)
3181 			return i;
3182 		idiag_cmd->data[i] = simple_strtol(step_str, NULL, 0);
3183 	}
3184 	return i;
3185 }
3186 
3187 /**
3188  * lpfc_idiag_open - idiag open debugfs
3189  * @inode: The inode pointer that contains a pointer to phba.
3190  * @file: The file pointer to attach the file operation.
3191  *
3192  * Description:
3193  * This routine is the entry point for the debugfs open file operation. It
3194  * gets the reference to phba from the i_private field in @inode, it then
3195  * allocates buffer for the file operation, performs the necessary PCI config
3196  * space read into the allocated buffer according to the idiag user command
3197  * setup, and then returns a pointer to buffer in the private_data field in
3198  * @file.
3199  *
3200  * Returns:
3201  * This function returns zero if successful. On error it will return an
3202  * negative error value.
3203  **/
3204 static int
3205 lpfc_idiag_open(struct inode *inode, struct file *file)
3206 {
3207 	struct lpfc_debug *debug;
3208 
3209 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
3210 	if (!debug)
3211 		return -ENOMEM;
3212 
3213 	debug->i_private = inode->i_private;
3214 	debug->buffer = NULL;
3215 	file->private_data = debug;
3216 
3217 	return 0;
3218 }
3219 
3220 /**
3221  * lpfc_idiag_release - Release idiag access file operation
3222  * @inode: The inode pointer that contains a vport pointer. (unused)
3223  * @file: The file pointer that contains the buffer to release.
3224  *
3225  * Description:
3226  * This routine is the generic release routine for the idiag access file
3227  * operation, it frees the buffer that was allocated when the debugfs file
3228  * was opened.
3229  *
3230  * Returns:
3231  * This function returns zero.
3232  **/
3233 static int
3234 lpfc_idiag_release(struct inode *inode, struct file *file)
3235 {
3236 	struct lpfc_debug *debug = file->private_data;
3237 
3238 	/* Free the buffers to the file operation */
3239 	kfree(debug->buffer);
3240 	kfree(debug);
3241 
3242 	return 0;
3243 }
3244 
3245 /**
3246  * lpfc_idiag_cmd_release - Release idiag cmd access file operation
3247  * @inode: The inode pointer that contains a vport pointer. (unused)
3248  * @file: The file pointer that contains the buffer to release.
3249  *
3250  * Description:
3251  * This routine frees the buffer that was allocated when the debugfs file
3252  * was opened. It also reset the fields in the idiag command struct in the
3253  * case of command for write operation.
3254  *
3255  * Returns:
3256  * This function returns zero.
3257  **/
3258 static int
3259 lpfc_idiag_cmd_release(struct inode *inode, struct file *file)
3260 {
3261 	struct lpfc_debug *debug = file->private_data;
3262 
3263 	if (debug->op == LPFC_IDIAG_OP_WR) {
3264 		switch (idiag.cmd.opcode) {
3265 		case LPFC_IDIAG_CMD_PCICFG_WR:
3266 		case LPFC_IDIAG_CMD_PCICFG_ST:
3267 		case LPFC_IDIAG_CMD_PCICFG_CL:
3268 		case LPFC_IDIAG_CMD_QUEACC_WR:
3269 		case LPFC_IDIAG_CMD_QUEACC_ST:
3270 		case LPFC_IDIAG_CMD_QUEACC_CL:
3271 			memset(&idiag, 0, sizeof(idiag));
3272 			break;
3273 		default:
3274 			break;
3275 		}
3276 	}
3277 
3278 	/* Free the buffers to the file operation */
3279 	kfree(debug->buffer);
3280 	kfree(debug);
3281 
3282 	return 0;
3283 }
3284 
3285 /**
3286  * lpfc_idiag_pcicfg_read - idiag debugfs read pcicfg
3287  * @file: The file pointer to read from.
3288  * @buf: The buffer to copy the data to.
3289  * @nbytes: The number of bytes to read.
3290  * @ppos: The position in the file to start reading from.
3291  *
3292  * Description:
3293  * This routine reads data from the @phba pci config space according to the
3294  * idiag command, and copies to user @buf. Depending on the PCI config space
3295  * read command setup, it does either a single register read of a byte
3296  * (8 bits), a word (16 bits), or a dword (32 bits) or browsing through all
3297  * registers from the 4K extended PCI config space.
3298  *
3299  * Returns:
3300  * This function returns the amount of data that was read (this could be less
3301  * than @nbytes if the end of the file was reached) or a negative error value.
3302  **/
3303 static ssize_t
3304 lpfc_idiag_pcicfg_read(struct file *file, char __user *buf, size_t nbytes,
3305 		       loff_t *ppos)
3306 {
3307 	struct lpfc_debug *debug = file->private_data;
3308 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3309 	int offset_label, offset, len = 0, index = LPFC_PCI_CFG_RD_SIZE;
3310 	int where, count;
3311 	char *pbuffer;
3312 	struct pci_dev *pdev;
3313 	uint32_t u32val;
3314 	uint16_t u16val;
3315 	uint8_t u8val;
3316 
3317 	pdev = phba->pcidev;
3318 	if (!pdev)
3319 		return 0;
3320 
3321 	/* This is a user read operation */
3322 	debug->op = LPFC_IDIAG_OP_RD;
3323 
3324 	if (!debug->buffer)
3325 		debug->buffer = kmalloc(LPFC_PCI_CFG_SIZE, GFP_KERNEL);
3326 	if (!debug->buffer)
3327 		return 0;
3328 	pbuffer = debug->buffer;
3329 
3330 	if (*ppos)
3331 		return 0;
3332 
3333 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
3334 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
3335 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
3336 	} else
3337 		return 0;
3338 
3339 	/* Read single PCI config space register */
3340 	switch (count) {
3341 	case SIZE_U8: /* byte (8 bits) */
3342 		pci_read_config_byte(pdev, where, &u8val);
3343 		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3344 				"%03x: %02x\n", where, u8val);
3345 		break;
3346 	case SIZE_U16: /* word (16 bits) */
3347 		pci_read_config_word(pdev, where, &u16val);
3348 		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3349 				"%03x: %04x\n", where, u16val);
3350 		break;
3351 	case SIZE_U32: /* double word (32 bits) */
3352 		pci_read_config_dword(pdev, where, &u32val);
3353 		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3354 				"%03x: %08x\n", where, u32val);
3355 		break;
3356 	case LPFC_PCI_CFG_BROWSE: /* browse all */
3357 		goto pcicfg_browse;
3358 		break;
3359 	default:
3360 		/* illegal count */
3361 		len = 0;
3362 		break;
3363 	}
3364 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3365 
3366 pcicfg_browse:
3367 
3368 	/* Browse all PCI config space registers */
3369 	offset_label = idiag.offset.last_rd;
3370 	offset = offset_label;
3371 
3372 	/* Read PCI config space */
3373 	len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3374 			"%03x: ", offset_label);
3375 	while (index > 0) {
3376 		pci_read_config_dword(pdev, offset, &u32val);
3377 		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3378 				"%08x ", u32val);
3379 		offset += sizeof(uint32_t);
3380 		if (offset >= LPFC_PCI_CFG_SIZE) {
3381 			len += scnprintf(pbuffer+len,
3382 					LPFC_PCI_CFG_SIZE-len, "\n");
3383 			break;
3384 		}
3385 		index -= sizeof(uint32_t);
3386 		if (!index)
3387 			len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3388 					"\n");
3389 		else if (!(index % (8 * sizeof(uint32_t)))) {
3390 			offset_label += (8 * sizeof(uint32_t));
3391 			len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3392 					"\n%03x: ", offset_label);
3393 		}
3394 	}
3395 
3396 	/* Set up the offset for next portion of pci cfg read */
3397 	if (index == 0) {
3398 		idiag.offset.last_rd += LPFC_PCI_CFG_RD_SIZE;
3399 		if (idiag.offset.last_rd >= LPFC_PCI_CFG_SIZE)
3400 			idiag.offset.last_rd = 0;
3401 	} else
3402 		idiag.offset.last_rd = 0;
3403 
3404 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3405 }
3406 
3407 /**
3408  * lpfc_idiag_pcicfg_write - Syntax check and set up idiag pcicfg commands
3409  * @file: The file pointer to read from.
3410  * @buf: The buffer to copy the user data from.
3411  * @nbytes: The number of bytes to get.
3412  * @ppos: The position in the file to start reading from.
3413  *
3414  * This routine get the debugfs idiag command struct from user space and
3415  * then perform the syntax check for PCI config space read or write command
3416  * accordingly. In the case of PCI config space read command, it sets up
3417  * the command in the idiag command struct for the debugfs read operation.
3418  * In the case of PCI config space write operation, it executes the write
3419  * operation into the PCI config space accordingly.
3420  *
3421  * It returns the @nbytges passing in from debugfs user space when successful.
3422  * In case of error conditions, it returns proper error code back to the user
3423  * space.
3424  */
3425 static ssize_t
3426 lpfc_idiag_pcicfg_write(struct file *file, const char __user *buf,
3427 			size_t nbytes, loff_t *ppos)
3428 {
3429 	struct lpfc_debug *debug = file->private_data;
3430 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3431 	uint32_t where, value, count;
3432 	uint32_t u32val;
3433 	uint16_t u16val;
3434 	uint8_t u8val;
3435 	struct pci_dev *pdev;
3436 	int rc;
3437 
3438 	pdev = phba->pcidev;
3439 	if (!pdev)
3440 		return -EFAULT;
3441 
3442 	/* This is a user write operation */
3443 	debug->op = LPFC_IDIAG_OP_WR;
3444 
3445 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3446 	if (rc < 0)
3447 		return rc;
3448 
3449 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
3450 		/* Sanity check on PCI config read command line arguments */
3451 		if (rc != LPFC_PCI_CFG_RD_CMD_ARG)
3452 			goto error_out;
3453 		/* Read command from PCI config space, set up command fields */
3454 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
3455 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
3456 		if (count == LPFC_PCI_CFG_BROWSE) {
3457 			if (where % sizeof(uint32_t))
3458 				goto error_out;
3459 			/* Starting offset to browse */
3460 			idiag.offset.last_rd = where;
3461 		} else if ((count != sizeof(uint8_t)) &&
3462 			   (count != sizeof(uint16_t)) &&
3463 			   (count != sizeof(uint32_t)))
3464 			goto error_out;
3465 		if (count == sizeof(uint8_t)) {
3466 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
3467 				goto error_out;
3468 			if (where % sizeof(uint8_t))
3469 				goto error_out;
3470 		}
3471 		if (count == sizeof(uint16_t)) {
3472 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
3473 				goto error_out;
3474 			if (where % sizeof(uint16_t))
3475 				goto error_out;
3476 		}
3477 		if (count == sizeof(uint32_t)) {
3478 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
3479 				goto error_out;
3480 			if (where % sizeof(uint32_t))
3481 				goto error_out;
3482 		}
3483 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR ||
3484 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST ||
3485 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3486 		/* Sanity check on PCI config write command line arguments */
3487 		if (rc != LPFC_PCI_CFG_WR_CMD_ARG)
3488 			goto error_out;
3489 		/* Write command to PCI config space, read-modify-write */
3490 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
3491 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
3492 		value = idiag.cmd.data[IDIAG_PCICFG_VALUE_INDX];
3493 		/* Sanity checks */
3494 		if ((count != sizeof(uint8_t)) &&
3495 		    (count != sizeof(uint16_t)) &&
3496 		    (count != sizeof(uint32_t)))
3497 			goto error_out;
3498 		if (count == sizeof(uint8_t)) {
3499 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
3500 				goto error_out;
3501 			if (where % sizeof(uint8_t))
3502 				goto error_out;
3503 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
3504 				pci_write_config_byte(pdev, where,
3505 						      (uint8_t)value);
3506 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
3507 				rc = pci_read_config_byte(pdev, where, &u8val);
3508 				if (!rc) {
3509 					u8val |= (uint8_t)value;
3510 					pci_write_config_byte(pdev, where,
3511 							      u8val);
3512 				}
3513 			}
3514 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3515 				rc = pci_read_config_byte(pdev, where, &u8val);
3516 				if (!rc) {
3517 					u8val &= (uint8_t)(~value);
3518 					pci_write_config_byte(pdev, where,
3519 							      u8val);
3520 				}
3521 			}
3522 		}
3523 		if (count == sizeof(uint16_t)) {
3524 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
3525 				goto error_out;
3526 			if (where % sizeof(uint16_t))
3527 				goto error_out;
3528 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
3529 				pci_write_config_word(pdev, where,
3530 						      (uint16_t)value);
3531 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
3532 				rc = pci_read_config_word(pdev, where, &u16val);
3533 				if (!rc) {
3534 					u16val |= (uint16_t)value;
3535 					pci_write_config_word(pdev, where,
3536 							      u16val);
3537 				}
3538 			}
3539 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3540 				rc = pci_read_config_word(pdev, where, &u16val);
3541 				if (!rc) {
3542 					u16val &= (uint16_t)(~value);
3543 					pci_write_config_word(pdev, where,
3544 							      u16val);
3545 				}
3546 			}
3547 		}
3548 		if (count == sizeof(uint32_t)) {
3549 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
3550 				goto error_out;
3551 			if (where % sizeof(uint32_t))
3552 				goto error_out;
3553 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
3554 				pci_write_config_dword(pdev, where, value);
3555 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
3556 				rc = pci_read_config_dword(pdev, where,
3557 							   &u32val);
3558 				if (!rc) {
3559 					u32val |= value;
3560 					pci_write_config_dword(pdev, where,
3561 							       u32val);
3562 				}
3563 			}
3564 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3565 				rc = pci_read_config_dword(pdev, where,
3566 							   &u32val);
3567 				if (!rc) {
3568 					u32val &= ~value;
3569 					pci_write_config_dword(pdev, where,
3570 							       u32val);
3571 				}
3572 			}
3573 		}
3574 	} else
3575 		/* All other opecodes are illegal for now */
3576 		goto error_out;
3577 
3578 	return nbytes;
3579 error_out:
3580 	memset(&idiag, 0, sizeof(idiag));
3581 	return -EINVAL;
3582 }
3583 
3584 /**
3585  * lpfc_idiag_baracc_read - idiag debugfs pci bar access read
3586  * @file: The file pointer to read from.
3587  * @buf: The buffer to copy the data to.
3588  * @nbytes: The number of bytes to read.
3589  * @ppos: The position in the file to start reading from.
3590  *
3591  * Description:
3592  * This routine reads data from the @phba pci bar memory mapped space
3593  * according to the idiag command, and copies to user @buf.
3594  *
3595  * Returns:
3596  * This function returns the amount of data that was read (this could be less
3597  * than @nbytes if the end of the file was reached) or a negative error value.
3598  **/
3599 static ssize_t
3600 lpfc_idiag_baracc_read(struct file *file, char __user *buf, size_t nbytes,
3601 		       loff_t *ppos)
3602 {
3603 	struct lpfc_debug *debug = file->private_data;
3604 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3605 	int offset_label, offset, offset_run, len = 0, index;
3606 	int bar_num, acc_range, bar_size;
3607 	char *pbuffer;
3608 	void __iomem *mem_mapped_bar;
3609 	uint32_t if_type;
3610 	struct pci_dev *pdev;
3611 	uint32_t u32val;
3612 
3613 	pdev = phba->pcidev;
3614 	if (!pdev)
3615 		return 0;
3616 
3617 	/* This is a user read operation */
3618 	debug->op = LPFC_IDIAG_OP_RD;
3619 
3620 	if (!debug->buffer)
3621 		debug->buffer = kmalloc(LPFC_PCI_BAR_RD_BUF_SIZE, GFP_KERNEL);
3622 	if (!debug->buffer)
3623 		return 0;
3624 	pbuffer = debug->buffer;
3625 
3626 	if (*ppos)
3627 		return 0;
3628 
3629 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
3630 		bar_num   = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
3631 		offset    = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
3632 		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
3633 		bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
3634 	} else
3635 		return 0;
3636 
3637 	if (acc_range == 0)
3638 		return 0;
3639 
3640 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
3641 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
3642 		if (bar_num == IDIAG_BARACC_BAR_0)
3643 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3644 		else if (bar_num == IDIAG_BARACC_BAR_1)
3645 			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
3646 		else if (bar_num == IDIAG_BARACC_BAR_2)
3647 			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
3648 		else
3649 			return 0;
3650 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3651 		if (bar_num == IDIAG_BARACC_BAR_0)
3652 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3653 		else
3654 			return 0;
3655 	} else
3656 		return 0;
3657 
3658 	/* Read single PCI bar space register */
3659 	if (acc_range == SINGLE_WORD) {
3660 		offset_run = offset;
3661 		u32val = readl(mem_mapped_bar + offset_run);
3662 		len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3663 				"%05x: %08x\n", offset_run, u32val);
3664 	} else
3665 		goto baracc_browse;
3666 
3667 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3668 
3669 baracc_browse:
3670 
3671 	/* Browse all PCI bar space registers */
3672 	offset_label = idiag.offset.last_rd;
3673 	offset_run = offset_label;
3674 
3675 	/* Read PCI bar memory mapped space */
3676 	len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3677 			"%05x: ", offset_label);
3678 	index = LPFC_PCI_BAR_RD_SIZE;
3679 	while (index > 0) {
3680 		u32val = readl(mem_mapped_bar + offset_run);
3681 		len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3682 				"%08x ", u32val);
3683 		offset_run += sizeof(uint32_t);
3684 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
3685 			if (offset_run >= bar_size) {
3686 				len += scnprintf(pbuffer+len,
3687 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
3688 				break;
3689 			}
3690 		} else {
3691 			if (offset_run >= offset +
3692 			    (acc_range * sizeof(uint32_t))) {
3693 				len += scnprintf(pbuffer+len,
3694 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
3695 				break;
3696 			}
3697 		}
3698 		index -= sizeof(uint32_t);
3699 		if (!index)
3700 			len += scnprintf(pbuffer+len,
3701 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
3702 		else if (!(index % (8 * sizeof(uint32_t)))) {
3703 			offset_label += (8 * sizeof(uint32_t));
3704 			len += scnprintf(pbuffer+len,
3705 					LPFC_PCI_BAR_RD_BUF_SIZE-len,
3706 					"\n%05x: ", offset_label);
3707 		}
3708 	}
3709 
3710 	/* Set up the offset for next portion of pci bar read */
3711 	if (index == 0) {
3712 		idiag.offset.last_rd += LPFC_PCI_BAR_RD_SIZE;
3713 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
3714 			if (idiag.offset.last_rd >= bar_size)
3715 				idiag.offset.last_rd = 0;
3716 		} else {
3717 			if (offset_run >= offset +
3718 			    (acc_range * sizeof(uint32_t)))
3719 				idiag.offset.last_rd = offset;
3720 		}
3721 	} else {
3722 		if (acc_range == LPFC_PCI_BAR_BROWSE)
3723 			idiag.offset.last_rd = 0;
3724 		else
3725 			idiag.offset.last_rd = offset;
3726 	}
3727 
3728 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3729 }
3730 
3731 /**
3732  * lpfc_idiag_baracc_write - Syntax check and set up idiag bar access commands
3733  * @file: The file pointer to read from.
3734  * @buf: The buffer to copy the user data from.
3735  * @nbytes: The number of bytes to get.
3736  * @ppos: The position in the file to start reading from.
3737  *
3738  * This routine get the debugfs idiag command struct from user space and
3739  * then perform the syntax check for PCI bar memory mapped space read or
3740  * write command accordingly. In the case of PCI bar memory mapped space
3741  * read command, it sets up the command in the idiag command struct for
3742  * the debugfs read operation. In the case of PCI bar memorpy mapped space
3743  * write operation, it executes the write operation into the PCI bar memory
3744  * mapped space accordingly.
3745  *
3746  * It returns the @nbytges passing in from debugfs user space when successful.
3747  * In case of error conditions, it returns proper error code back to the user
3748  * space.
3749  */
3750 static ssize_t
3751 lpfc_idiag_baracc_write(struct file *file, const char __user *buf,
3752 			size_t nbytes, loff_t *ppos)
3753 {
3754 	struct lpfc_debug *debug = file->private_data;
3755 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3756 	uint32_t bar_num, bar_size, offset, value, acc_range;
3757 	struct pci_dev *pdev;
3758 	void __iomem *mem_mapped_bar;
3759 	uint32_t if_type;
3760 	uint32_t u32val;
3761 	int rc;
3762 
3763 	pdev = phba->pcidev;
3764 	if (!pdev)
3765 		return -EFAULT;
3766 
3767 	/* This is a user write operation */
3768 	debug->op = LPFC_IDIAG_OP_WR;
3769 
3770 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3771 	if (rc < 0)
3772 		return rc;
3773 
3774 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
3775 	bar_num = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
3776 
3777 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
3778 		if ((bar_num != IDIAG_BARACC_BAR_0) &&
3779 		    (bar_num != IDIAG_BARACC_BAR_1) &&
3780 		    (bar_num != IDIAG_BARACC_BAR_2))
3781 			goto error_out;
3782 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3783 		if (bar_num != IDIAG_BARACC_BAR_0)
3784 			goto error_out;
3785 	} else
3786 		goto error_out;
3787 
3788 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
3789 		if (bar_num == IDIAG_BARACC_BAR_0) {
3790 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3791 				LPFC_PCI_IF0_BAR0_SIZE;
3792 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3793 		} else if (bar_num == IDIAG_BARACC_BAR_1) {
3794 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3795 				LPFC_PCI_IF0_BAR1_SIZE;
3796 			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
3797 		} else if (bar_num == IDIAG_BARACC_BAR_2) {
3798 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3799 				LPFC_PCI_IF0_BAR2_SIZE;
3800 			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
3801 		} else
3802 			goto error_out;
3803 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3804 		if (bar_num == IDIAG_BARACC_BAR_0) {
3805 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3806 				LPFC_PCI_IF2_BAR0_SIZE;
3807 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3808 		} else
3809 			goto error_out;
3810 	} else
3811 		goto error_out;
3812 
3813 	offset = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
3814 	if (offset % sizeof(uint32_t))
3815 		goto error_out;
3816 
3817 	bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
3818 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
3819 		/* Sanity check on PCI config read command line arguments */
3820 		if (rc != LPFC_PCI_BAR_RD_CMD_ARG)
3821 			goto error_out;
3822 		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
3823 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
3824 			if (offset > bar_size - sizeof(uint32_t))
3825 				goto error_out;
3826 			/* Starting offset to browse */
3827 			idiag.offset.last_rd = offset;
3828 		} else if (acc_range > SINGLE_WORD) {
3829 			if (offset + acc_range * sizeof(uint32_t) > bar_size)
3830 				goto error_out;
3831 			/* Starting offset to browse */
3832 			idiag.offset.last_rd = offset;
3833 		} else if (acc_range != SINGLE_WORD)
3834 			goto error_out;
3835 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR ||
3836 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST ||
3837 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
3838 		/* Sanity check on PCI bar write command line arguments */
3839 		if (rc != LPFC_PCI_BAR_WR_CMD_ARG)
3840 			goto error_out;
3841 		/* Write command to PCI bar space, read-modify-write */
3842 		acc_range = SINGLE_WORD;
3843 		value = idiag.cmd.data[IDIAG_BARACC_REG_VAL_INDX];
3844 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR) {
3845 			writel(value, mem_mapped_bar + offset);
3846 			readl(mem_mapped_bar + offset);
3847 		}
3848 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST) {
3849 			u32val = readl(mem_mapped_bar + offset);
3850 			u32val |= value;
3851 			writel(u32val, mem_mapped_bar + offset);
3852 			readl(mem_mapped_bar + offset);
3853 		}
3854 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
3855 			u32val = readl(mem_mapped_bar + offset);
3856 			u32val &= ~value;
3857 			writel(u32val, mem_mapped_bar + offset);
3858 			readl(mem_mapped_bar + offset);
3859 		}
3860 	} else
3861 		/* All other opecodes are illegal for now */
3862 		goto error_out;
3863 
3864 	return nbytes;
3865 error_out:
3866 	memset(&idiag, 0, sizeof(idiag));
3867 	return -EINVAL;
3868 }
3869 
3870 static int
3871 __lpfc_idiag_print_wq(struct lpfc_queue *qp, char *wqtype,
3872 			char *pbuffer, int len)
3873 {
3874 	if (!qp)
3875 		return len;
3876 
3877 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3878 			"\t\t%s WQ info: ", wqtype);
3879 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3880 			"AssocCQID[%04d]: WQ-STAT[oflow:x%x posted:x%llx]\n",
3881 			qp->assoc_qid, qp->q_cnt_1,
3882 			(unsigned long long)qp->q_cnt_4);
3883 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3884 			"\t\tWQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3885 			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]",
3886 			qp->queue_id, qp->entry_count,
3887 			qp->entry_size, qp->host_index,
3888 			qp->hba_index, qp->notify_interval);
3889 	len +=  scnprintf(pbuffer + len,
3890 			LPFC_QUE_INFO_GET_BUF_SIZE - len, "\n");
3891 	return len;
3892 }
3893 
3894 static int
3895 lpfc_idiag_wqs_for_cq(struct lpfc_hba *phba, char *wqtype, char *pbuffer,
3896 		int *len, int max_cnt, int cq_id)
3897 {
3898 	struct lpfc_queue *qp;
3899 	int qidx;
3900 
3901 	for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
3902 		qp = phba->sli4_hba.hdwq[qidx].io_wq;
3903 		if (qp->assoc_qid != cq_id)
3904 			continue;
3905 		*len = __lpfc_idiag_print_wq(qp, wqtype, pbuffer, *len);
3906 		if (*len >= max_cnt)
3907 			return 1;
3908 	}
3909 	return 0;
3910 }
3911 
3912 static int
3913 __lpfc_idiag_print_cq(struct lpfc_queue *qp, char *cqtype,
3914 			char *pbuffer, int len)
3915 {
3916 	if (!qp)
3917 		return len;
3918 
3919 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3920 			"\t%s CQ info: ", cqtype);
3921 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3922 			"AssocEQID[%02d]: CQ STAT[max:x%x relw:x%x "
3923 			"xabt:x%x wq:x%llx]\n",
3924 			qp->assoc_qid, qp->q_cnt_1, qp->q_cnt_2,
3925 			qp->q_cnt_3, (unsigned long long)qp->q_cnt_4);
3926 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3927 			"\tCQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3928 			"HST-IDX[%04d], NTFI[%03d], PLMT[%03d]",
3929 			qp->queue_id, qp->entry_count,
3930 			qp->entry_size, qp->host_index,
3931 			qp->notify_interval, qp->max_proc_limit);
3932 
3933 	len +=  scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3934 			"\n");
3935 
3936 	return len;
3937 }
3938 
3939 static int
3940 __lpfc_idiag_print_rqpair(struct lpfc_queue *qp, struct lpfc_queue *datqp,
3941 			char *rqtype, char *pbuffer, int len)
3942 {
3943 	if (!qp || !datqp)
3944 		return len;
3945 
3946 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3947 			"\t\t%s RQ info: ", rqtype);
3948 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3949 			"AssocCQID[%02d]: RQ-STAT[nopost:x%x nobuf:x%x "
3950 			"posted:x%x rcv:x%llx]\n",
3951 			qp->assoc_qid, qp->q_cnt_1, qp->q_cnt_2,
3952 			qp->q_cnt_3, (unsigned long long)qp->q_cnt_4);
3953 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3954 			"\t\tHQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3955 			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]\n",
3956 			qp->queue_id, qp->entry_count, qp->entry_size,
3957 			qp->host_index, qp->hba_index, qp->notify_interval);
3958 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3959 			"\t\tDQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3960 			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]\n",
3961 			datqp->queue_id, datqp->entry_count,
3962 			datqp->entry_size, datqp->host_index,
3963 			datqp->hba_index, datqp->notify_interval);
3964 	return len;
3965 }
3966 
3967 static int
3968 lpfc_idiag_cqs_for_eq(struct lpfc_hba *phba, char *pbuffer,
3969 		int *len, int max_cnt, int eqidx, int eq_id)
3970 {
3971 	struct lpfc_queue *qp;
3972 	int rc;
3973 
3974 	qp = phba->sli4_hba.hdwq[eqidx].io_cq;
3975 
3976 	*len = __lpfc_idiag_print_cq(qp, "IO", pbuffer, *len);
3977 
3978 	/* Reset max counter */
3979 	qp->CQ_max_cqe = 0;
3980 
3981 	if (*len >= max_cnt)
3982 		return 1;
3983 
3984 	rc = lpfc_idiag_wqs_for_cq(phba, "IO", pbuffer, len,
3985 				   max_cnt, qp->queue_id);
3986 	if (rc)
3987 		return 1;
3988 
3989 	if ((eqidx < phba->cfg_nvmet_mrq) && phba->nvmet_support) {
3990 		/* NVMET CQset */
3991 		qp = phba->sli4_hba.nvmet_cqset[eqidx];
3992 		*len = __lpfc_idiag_print_cq(qp, "NVMET CQset", pbuffer, *len);
3993 
3994 		/* Reset max counter */
3995 		qp->CQ_max_cqe = 0;
3996 
3997 		if (*len >= max_cnt)
3998 			return 1;
3999 
4000 		/* RQ header */
4001 		qp = phba->sli4_hba.nvmet_mrq_hdr[eqidx];
4002 		*len = __lpfc_idiag_print_rqpair(qp,
4003 				phba->sli4_hba.nvmet_mrq_data[eqidx],
4004 				"NVMET MRQ", pbuffer, *len);
4005 
4006 		if (*len >= max_cnt)
4007 			return 1;
4008 	}
4009 
4010 	return 0;
4011 }
4012 
4013 static int
4014 __lpfc_idiag_print_eq(struct lpfc_queue *qp, char *eqtype,
4015 			char *pbuffer, int len)
4016 {
4017 	if (!qp)
4018 		return len;
4019 
4020 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
4021 			"\n%s EQ info: EQ-STAT[max:x%x noE:x%x "
4022 			"cqe_proc:x%x eqe_proc:x%llx eqd %d]\n",
4023 			eqtype, qp->q_cnt_1, qp->q_cnt_2, qp->q_cnt_3,
4024 			(unsigned long long)qp->q_cnt_4, qp->q_mode);
4025 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
4026 			"EQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
4027 			"HST-IDX[%04d], NTFI[%03d], PLMT[%03d], AFFIN[%03d]",
4028 			qp->queue_id, qp->entry_count, qp->entry_size,
4029 			qp->host_index, qp->notify_interval,
4030 			qp->max_proc_limit, qp->chann);
4031 	len +=  scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
4032 			"\n");
4033 
4034 	return len;
4035 }
4036 
4037 /**
4038  * lpfc_idiag_queinfo_read - idiag debugfs read queue information
4039  * @file: The file pointer to read from.
4040  * @buf: The buffer to copy the data to.
4041  * @nbytes: The number of bytes to read.
4042  * @ppos: The position in the file to start reading from.
4043  *
4044  * Description:
4045  * This routine reads data from the @phba SLI4 PCI function queue information,
4046  * and copies to user @buf.
4047  * This routine only returns 1 EQs worth of information. It remembers the last
4048  * EQ read and jumps to the next EQ. Thus subsequent calls to queInfo will
4049  * retrieve all EQs allocated for the phba.
4050  *
4051  * Returns:
4052  * This function returns the amount of data that was read (this could be less
4053  * than @nbytes if the end of the file was reached) or a negative error value.
4054  **/
4055 static ssize_t
4056 lpfc_idiag_queinfo_read(struct file *file, char __user *buf, size_t nbytes,
4057 			loff_t *ppos)
4058 {
4059 	struct lpfc_debug *debug = file->private_data;
4060 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4061 	char *pbuffer;
4062 	int max_cnt, rc, x, len = 0;
4063 	struct lpfc_queue *qp = NULL;
4064 
4065 	if (!debug->buffer)
4066 		debug->buffer = kmalloc(LPFC_QUE_INFO_GET_BUF_SIZE, GFP_KERNEL);
4067 	if (!debug->buffer)
4068 		return 0;
4069 	pbuffer = debug->buffer;
4070 	max_cnt = LPFC_QUE_INFO_GET_BUF_SIZE - 256;
4071 
4072 	if (*ppos)
4073 		return 0;
4074 
4075 	spin_lock_irq(&phba->hbalock);
4076 
4077 	/* Fast-path event queue */
4078 	if (phba->sli4_hba.hdwq && phba->cfg_hdw_queue) {
4079 
4080 		x = phba->lpfc_idiag_last_eq;
4081 		phba->lpfc_idiag_last_eq++;
4082 		if (phba->lpfc_idiag_last_eq >= phba->cfg_hdw_queue)
4083 			phba->lpfc_idiag_last_eq = 0;
4084 
4085 		len += scnprintf(pbuffer + len,
4086 				 LPFC_QUE_INFO_GET_BUF_SIZE - len,
4087 				 "HDWQ %d out of %d HBA HDWQs\n",
4088 				 x, phba->cfg_hdw_queue);
4089 
4090 		/* Fast-path EQ */
4091 		qp = phba->sli4_hba.hdwq[x].hba_eq;
4092 		if (!qp)
4093 			goto out;
4094 
4095 		len = __lpfc_idiag_print_eq(qp, "HBA", pbuffer, len);
4096 
4097 		/* Reset max counter */
4098 		qp->EQ_max_eqe = 0;
4099 
4100 		if (len >= max_cnt)
4101 			goto too_big;
4102 
4103 		/* will dump both fcp and nvme cqs/wqs for the eq */
4104 		rc = lpfc_idiag_cqs_for_eq(phba, pbuffer, &len,
4105 			max_cnt, x, qp->queue_id);
4106 		if (rc)
4107 			goto too_big;
4108 
4109 		/* Only EQ 0 has slow path CQs configured */
4110 		if (x)
4111 			goto out;
4112 
4113 		/* Slow-path mailbox CQ */
4114 		qp = phba->sli4_hba.mbx_cq;
4115 		len = __lpfc_idiag_print_cq(qp, "MBX", pbuffer, len);
4116 		if (len >= max_cnt)
4117 			goto too_big;
4118 
4119 		/* Slow-path MBOX MQ */
4120 		qp = phba->sli4_hba.mbx_wq;
4121 		len = __lpfc_idiag_print_wq(qp, "MBX", pbuffer, len);
4122 		if (len >= max_cnt)
4123 			goto too_big;
4124 
4125 		/* Slow-path ELS response CQ */
4126 		qp = phba->sli4_hba.els_cq;
4127 		len = __lpfc_idiag_print_cq(qp, "ELS", pbuffer, len);
4128 		/* Reset max counter */
4129 		if (qp)
4130 			qp->CQ_max_cqe = 0;
4131 		if (len >= max_cnt)
4132 			goto too_big;
4133 
4134 		/* Slow-path ELS WQ */
4135 		qp = phba->sli4_hba.els_wq;
4136 		len = __lpfc_idiag_print_wq(qp, "ELS", pbuffer, len);
4137 		if (len >= max_cnt)
4138 			goto too_big;
4139 
4140 		qp = phba->sli4_hba.hdr_rq;
4141 		len = __lpfc_idiag_print_rqpair(qp, phba->sli4_hba.dat_rq,
4142 						"ELS RQpair", pbuffer, len);
4143 		if (len >= max_cnt)
4144 			goto too_big;
4145 
4146 		/* Slow-path NVME LS response CQ */
4147 		qp = phba->sli4_hba.nvmels_cq;
4148 		len = __lpfc_idiag_print_cq(qp, "NVME LS",
4149 						pbuffer, len);
4150 		/* Reset max counter */
4151 		if (qp)
4152 			qp->CQ_max_cqe = 0;
4153 		if (len >= max_cnt)
4154 			goto too_big;
4155 
4156 		/* Slow-path NVME LS WQ */
4157 		qp = phba->sli4_hba.nvmels_wq;
4158 		len = __lpfc_idiag_print_wq(qp, "NVME LS",
4159 						pbuffer, len);
4160 		if (len >= max_cnt)
4161 			goto too_big;
4162 
4163 		goto out;
4164 	}
4165 
4166 	spin_unlock_irq(&phba->hbalock);
4167 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4168 
4169 too_big:
4170 	len +=  scnprintf(pbuffer + len,
4171 		LPFC_QUE_INFO_GET_BUF_SIZE - len, "Truncated ...\n");
4172 out:
4173 	spin_unlock_irq(&phba->hbalock);
4174 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4175 }
4176 
4177 /**
4178  * lpfc_idiag_que_param_check - queue access command parameter sanity check
4179  * @q: The pointer to queue structure.
4180  * @index: The index into a queue entry.
4181  * @count: The number of queue entries to access.
4182  *
4183  * Description:
4184  * The routine performs sanity check on device queue access method commands.
4185  *
4186  * Returns:
4187  * This function returns -EINVAL when fails the sanity check, otherwise, it
4188  * returns 0.
4189  **/
4190 static int
4191 lpfc_idiag_que_param_check(struct lpfc_queue *q, int index, int count)
4192 {
4193 	/* Only support single entry read or browsing */
4194 	if ((count != 1) && (count != LPFC_QUE_ACC_BROWSE))
4195 		return -EINVAL;
4196 	if (index > q->entry_count - 1)
4197 		return -EINVAL;
4198 	return 0;
4199 }
4200 
4201 /**
4202  * lpfc_idiag_queacc_read_qe - read a single entry from the given queue index
4203  * @pbuffer: The pointer to buffer to copy the read data into.
4204  * @pque: The pointer to the queue to be read.
4205  * @index: The index into the queue entry.
4206  *
4207  * Description:
4208  * This routine reads out a single entry from the given queue's index location
4209  * and copies it into the buffer provided.
4210  *
4211  * Returns:
4212  * This function returns 0 when it fails, otherwise, it returns the length of
4213  * the data read into the buffer provided.
4214  **/
4215 static int
4216 lpfc_idiag_queacc_read_qe(char *pbuffer, int len, struct lpfc_queue *pque,
4217 			  uint32_t index)
4218 {
4219 	int offset, esize;
4220 	uint32_t *pentry;
4221 
4222 	if (!pbuffer || !pque)
4223 		return 0;
4224 
4225 	esize = pque->entry_size;
4226 	len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
4227 			"QE-INDEX[%04d]:\n", index);
4228 
4229 	offset = 0;
4230 	pentry = lpfc_sli4_qe(pque, index);
4231 	while (esize > 0) {
4232 		len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
4233 				"%08x ", *pentry);
4234 		pentry++;
4235 		offset += sizeof(uint32_t);
4236 		esize -= sizeof(uint32_t);
4237 		if (esize > 0 && !(offset % (4 * sizeof(uint32_t))))
4238 			len += scnprintf(pbuffer+len,
4239 					LPFC_QUE_ACC_BUF_SIZE-len, "\n");
4240 	}
4241 	len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len, "\n");
4242 
4243 	return len;
4244 }
4245 
4246 /**
4247  * lpfc_idiag_queacc_read - idiag debugfs read port queue
4248  * @file: The file pointer to read from.
4249  * @buf: The buffer to copy the data to.
4250  * @nbytes: The number of bytes to read.
4251  * @ppos: The position in the file to start reading from.
4252  *
4253  * Description:
4254  * This routine reads data from the @phba device queue memory according to the
4255  * idiag command, and copies to user @buf. Depending on the queue dump read
4256  * command setup, it does either a single queue entry read or browing through
4257  * all entries of the queue.
4258  *
4259  * Returns:
4260  * This function returns the amount of data that was read (this could be less
4261  * than @nbytes if the end of the file was reached) or a negative error value.
4262  **/
4263 static ssize_t
4264 lpfc_idiag_queacc_read(struct file *file, char __user *buf, size_t nbytes,
4265 		       loff_t *ppos)
4266 {
4267 	struct lpfc_debug *debug = file->private_data;
4268 	uint32_t last_index, index, count;
4269 	struct lpfc_queue *pque = NULL;
4270 	char *pbuffer;
4271 	int len = 0;
4272 
4273 	/* This is a user read operation */
4274 	debug->op = LPFC_IDIAG_OP_RD;
4275 
4276 	if (!debug->buffer)
4277 		debug->buffer = kmalloc(LPFC_QUE_ACC_BUF_SIZE, GFP_KERNEL);
4278 	if (!debug->buffer)
4279 		return 0;
4280 	pbuffer = debug->buffer;
4281 
4282 	if (*ppos)
4283 		return 0;
4284 
4285 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
4286 		index = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
4287 		count = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
4288 		pque = (struct lpfc_queue *)idiag.ptr_private;
4289 	} else
4290 		return 0;
4291 
4292 	/* Browse the queue starting from index */
4293 	if (count == LPFC_QUE_ACC_BROWSE)
4294 		goto que_browse;
4295 
4296 	/* Read a single entry from the queue */
4297 	len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
4298 
4299 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4300 
4301 que_browse:
4302 
4303 	/* Browse all entries from the queue */
4304 	last_index = idiag.offset.last_rd;
4305 	index = last_index;
4306 
4307 	while (len < LPFC_QUE_ACC_SIZE - pque->entry_size) {
4308 		len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
4309 		index++;
4310 		if (index > pque->entry_count - 1)
4311 			break;
4312 	}
4313 
4314 	/* Set up the offset for next portion of pci cfg read */
4315 	if (index > pque->entry_count - 1)
4316 		index = 0;
4317 	idiag.offset.last_rd = index;
4318 
4319 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4320 }
4321 
4322 /**
4323  * lpfc_idiag_queacc_write - Syntax check and set up idiag queacc commands
4324  * @file: The file pointer to read from.
4325  * @buf: The buffer to copy the user data from.
4326  * @nbytes: The number of bytes to get.
4327  * @ppos: The position in the file to start reading from.
4328  *
4329  * This routine get the debugfs idiag command struct from user space and then
4330  * perform the syntax check for port queue read (dump) or write (set) command
4331  * accordingly. In the case of port queue read command, it sets up the command
4332  * in the idiag command struct for the following debugfs read operation. In
4333  * the case of port queue write operation, it executes the write operation
4334  * into the port queue entry accordingly.
4335  *
4336  * It returns the @nbytges passing in from debugfs user space when successful.
4337  * In case of error conditions, it returns proper error code back to the user
4338  * space.
4339  **/
4340 static ssize_t
4341 lpfc_idiag_queacc_write(struct file *file, const char __user *buf,
4342 			size_t nbytes, loff_t *ppos)
4343 {
4344 	struct lpfc_debug *debug = file->private_data;
4345 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4346 	uint32_t qidx, quetp, queid, index, count, offset, value;
4347 	uint32_t *pentry;
4348 	struct lpfc_queue *pque, *qp;
4349 	int rc;
4350 
4351 	/* This is a user write operation */
4352 	debug->op = LPFC_IDIAG_OP_WR;
4353 
4354 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4355 	if (rc < 0)
4356 		return rc;
4357 
4358 	/* Get and sanity check on command feilds */
4359 	quetp  = idiag.cmd.data[IDIAG_QUEACC_QUETP_INDX];
4360 	queid  = idiag.cmd.data[IDIAG_QUEACC_QUEID_INDX];
4361 	index  = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
4362 	count  = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
4363 	offset = idiag.cmd.data[IDIAG_QUEACC_OFFST_INDX];
4364 	value  = idiag.cmd.data[IDIAG_QUEACC_VALUE_INDX];
4365 
4366 	/* Sanity check on command line arguments */
4367 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
4368 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
4369 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
4370 		if (rc != LPFC_QUE_ACC_WR_CMD_ARG)
4371 			goto error_out;
4372 		if (count != 1)
4373 			goto error_out;
4374 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
4375 		if (rc != LPFC_QUE_ACC_RD_CMD_ARG)
4376 			goto error_out;
4377 	} else
4378 		goto error_out;
4379 
4380 	switch (quetp) {
4381 	case LPFC_IDIAG_EQ:
4382 		/* HBA event queue */
4383 		if (phba->sli4_hba.hdwq) {
4384 			for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
4385 				qp = phba->sli4_hba.hdwq[qidx].hba_eq;
4386 				if (qp && qp->queue_id == queid) {
4387 					/* Sanity check */
4388 					rc = lpfc_idiag_que_param_check(qp,
4389 						index, count);
4390 					if (rc)
4391 						goto error_out;
4392 					idiag.ptr_private = qp;
4393 					goto pass_check;
4394 				}
4395 			}
4396 		}
4397 		goto error_out;
4398 		break;
4399 	case LPFC_IDIAG_CQ:
4400 		/* MBX complete queue */
4401 		if (phba->sli4_hba.mbx_cq &&
4402 		    phba->sli4_hba.mbx_cq->queue_id == queid) {
4403 			/* Sanity check */
4404 			rc = lpfc_idiag_que_param_check(
4405 					phba->sli4_hba.mbx_cq, index, count);
4406 			if (rc)
4407 				goto error_out;
4408 			idiag.ptr_private = phba->sli4_hba.mbx_cq;
4409 			goto pass_check;
4410 		}
4411 		/* ELS complete queue */
4412 		if (phba->sli4_hba.els_cq &&
4413 		    phba->sli4_hba.els_cq->queue_id == queid) {
4414 			/* Sanity check */
4415 			rc = lpfc_idiag_que_param_check(
4416 					phba->sli4_hba.els_cq, index, count);
4417 			if (rc)
4418 				goto error_out;
4419 			idiag.ptr_private = phba->sli4_hba.els_cq;
4420 			goto pass_check;
4421 		}
4422 		/* NVME LS complete queue */
4423 		if (phba->sli4_hba.nvmels_cq &&
4424 		    phba->sli4_hba.nvmels_cq->queue_id == queid) {
4425 			/* Sanity check */
4426 			rc = lpfc_idiag_que_param_check(
4427 					phba->sli4_hba.nvmels_cq, index, count);
4428 			if (rc)
4429 				goto error_out;
4430 			idiag.ptr_private = phba->sli4_hba.nvmels_cq;
4431 			goto pass_check;
4432 		}
4433 		/* FCP complete queue */
4434 		if (phba->sli4_hba.hdwq) {
4435 			for (qidx = 0; qidx < phba->cfg_hdw_queue;
4436 								qidx++) {
4437 				qp = phba->sli4_hba.hdwq[qidx].io_cq;
4438 				if (qp && qp->queue_id == queid) {
4439 					/* Sanity check */
4440 					rc = lpfc_idiag_que_param_check(
4441 						qp, index, count);
4442 					if (rc)
4443 						goto error_out;
4444 					idiag.ptr_private = qp;
4445 					goto pass_check;
4446 				}
4447 			}
4448 		}
4449 		goto error_out;
4450 		break;
4451 	case LPFC_IDIAG_MQ:
4452 		/* MBX work queue */
4453 		if (phba->sli4_hba.mbx_wq &&
4454 		    phba->sli4_hba.mbx_wq->queue_id == queid) {
4455 			/* Sanity check */
4456 			rc = lpfc_idiag_que_param_check(
4457 					phba->sli4_hba.mbx_wq, index, count);
4458 			if (rc)
4459 				goto error_out;
4460 			idiag.ptr_private = phba->sli4_hba.mbx_wq;
4461 			goto pass_check;
4462 		}
4463 		goto error_out;
4464 		break;
4465 	case LPFC_IDIAG_WQ:
4466 		/* ELS work queue */
4467 		if (phba->sli4_hba.els_wq &&
4468 		    phba->sli4_hba.els_wq->queue_id == queid) {
4469 			/* Sanity check */
4470 			rc = lpfc_idiag_que_param_check(
4471 					phba->sli4_hba.els_wq, index, count);
4472 			if (rc)
4473 				goto error_out;
4474 			idiag.ptr_private = phba->sli4_hba.els_wq;
4475 			goto pass_check;
4476 		}
4477 		/* NVME LS work queue */
4478 		if (phba->sli4_hba.nvmels_wq &&
4479 		    phba->sli4_hba.nvmels_wq->queue_id == queid) {
4480 			/* Sanity check */
4481 			rc = lpfc_idiag_que_param_check(
4482 					phba->sli4_hba.nvmels_wq, index, count);
4483 			if (rc)
4484 				goto error_out;
4485 			idiag.ptr_private = phba->sli4_hba.nvmels_wq;
4486 			goto pass_check;
4487 		}
4488 
4489 		if (phba->sli4_hba.hdwq) {
4490 			/* FCP/SCSI work queue */
4491 			for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
4492 				qp = phba->sli4_hba.hdwq[qidx].io_wq;
4493 				if (qp && qp->queue_id == queid) {
4494 					/* Sanity check */
4495 					rc = lpfc_idiag_que_param_check(
4496 						qp, index, count);
4497 					if (rc)
4498 						goto error_out;
4499 					idiag.ptr_private = qp;
4500 					goto pass_check;
4501 				}
4502 			}
4503 		}
4504 
4505 		goto error_out;
4506 		break;
4507 	case LPFC_IDIAG_RQ:
4508 		/* HDR queue */
4509 		if (phba->sli4_hba.hdr_rq &&
4510 		    phba->sli4_hba.hdr_rq->queue_id == queid) {
4511 			/* Sanity check */
4512 			rc = lpfc_idiag_que_param_check(
4513 					phba->sli4_hba.hdr_rq, index, count);
4514 			if (rc)
4515 				goto error_out;
4516 			idiag.ptr_private = phba->sli4_hba.hdr_rq;
4517 			goto pass_check;
4518 		}
4519 		/* DAT queue */
4520 		if (phba->sli4_hba.dat_rq &&
4521 		    phba->sli4_hba.dat_rq->queue_id == queid) {
4522 			/* Sanity check */
4523 			rc = lpfc_idiag_que_param_check(
4524 					phba->sli4_hba.dat_rq, index, count);
4525 			if (rc)
4526 				goto error_out;
4527 			idiag.ptr_private = phba->sli4_hba.dat_rq;
4528 			goto pass_check;
4529 		}
4530 		goto error_out;
4531 		break;
4532 	default:
4533 		goto error_out;
4534 		break;
4535 	}
4536 
4537 pass_check:
4538 
4539 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
4540 		if (count == LPFC_QUE_ACC_BROWSE)
4541 			idiag.offset.last_rd = index;
4542 	}
4543 
4544 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
4545 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
4546 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
4547 		/* Additional sanity checks on write operation */
4548 		pque = (struct lpfc_queue *)idiag.ptr_private;
4549 		if (offset > pque->entry_size/sizeof(uint32_t) - 1)
4550 			goto error_out;
4551 		pentry = lpfc_sli4_qe(pque, index);
4552 		pentry += offset;
4553 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR)
4554 			*pentry = value;
4555 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST)
4556 			*pentry |= value;
4557 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL)
4558 			*pentry &= ~value;
4559 	}
4560 	return nbytes;
4561 
4562 error_out:
4563 	/* Clean out command structure on command error out */
4564 	memset(&idiag, 0, sizeof(idiag));
4565 	return -EINVAL;
4566 }
4567 
4568 /**
4569  * lpfc_idiag_drbacc_read_reg - idiag debugfs read a doorbell register
4570  * @phba: The pointer to hba structure.
4571  * @pbuffer: The pointer to the buffer to copy the data to.
4572  * @len: The length of bytes to copied.
4573  * @drbregid: The id to doorbell registers.
4574  *
4575  * Description:
4576  * This routine reads a doorbell register and copies its content to the
4577  * user buffer pointed to by @pbuffer.
4578  *
4579  * Returns:
4580  * This function returns the amount of data that was copied into @pbuffer.
4581  **/
4582 static int
4583 lpfc_idiag_drbacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
4584 			   int len, uint32_t drbregid)
4585 {
4586 
4587 	if (!pbuffer)
4588 		return 0;
4589 
4590 	switch (drbregid) {
4591 	case LPFC_DRB_EQ:
4592 		len += scnprintf(pbuffer + len, LPFC_DRB_ACC_BUF_SIZE-len,
4593 				"EQ-DRB-REG: 0x%08x\n",
4594 				readl(phba->sli4_hba.EQDBregaddr));
4595 		break;
4596 	case LPFC_DRB_CQ:
4597 		len += scnprintf(pbuffer + len, LPFC_DRB_ACC_BUF_SIZE - len,
4598 				"CQ-DRB-REG: 0x%08x\n",
4599 				readl(phba->sli4_hba.CQDBregaddr));
4600 		break;
4601 	case LPFC_DRB_MQ:
4602 		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4603 				"MQ-DRB-REG:   0x%08x\n",
4604 				readl(phba->sli4_hba.MQDBregaddr));
4605 		break;
4606 	case LPFC_DRB_WQ:
4607 		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4608 				"WQ-DRB-REG:   0x%08x\n",
4609 				readl(phba->sli4_hba.WQDBregaddr));
4610 		break;
4611 	case LPFC_DRB_RQ:
4612 		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4613 				"RQ-DRB-REG:   0x%08x\n",
4614 				readl(phba->sli4_hba.RQDBregaddr));
4615 		break;
4616 	default:
4617 		break;
4618 	}
4619 
4620 	return len;
4621 }
4622 
4623 /**
4624  * lpfc_idiag_drbacc_read - idiag debugfs read port doorbell
4625  * @file: The file pointer to read from.
4626  * @buf: The buffer to copy the data to.
4627  * @nbytes: The number of bytes to read.
4628  * @ppos: The position in the file to start reading from.
4629  *
4630  * Description:
4631  * This routine reads data from the @phba device doorbell register according
4632  * to the idiag command, and copies to user @buf. Depending on the doorbell
4633  * register read command setup, it does either a single doorbell register
4634  * read or dump all doorbell registers.
4635  *
4636  * Returns:
4637  * This function returns the amount of data that was read (this could be less
4638  * than @nbytes if the end of the file was reached) or a negative error value.
4639  **/
4640 static ssize_t
4641 lpfc_idiag_drbacc_read(struct file *file, char __user *buf, size_t nbytes,
4642 		       loff_t *ppos)
4643 {
4644 	struct lpfc_debug *debug = file->private_data;
4645 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4646 	uint32_t drb_reg_id, i;
4647 	char *pbuffer;
4648 	int len = 0;
4649 
4650 	/* This is a user read operation */
4651 	debug->op = LPFC_IDIAG_OP_RD;
4652 
4653 	if (!debug->buffer)
4654 		debug->buffer = kmalloc(LPFC_DRB_ACC_BUF_SIZE, GFP_KERNEL);
4655 	if (!debug->buffer)
4656 		return 0;
4657 	pbuffer = debug->buffer;
4658 
4659 	if (*ppos)
4660 		return 0;
4661 
4662 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD)
4663 		drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
4664 	else
4665 		return 0;
4666 
4667 	if (drb_reg_id == LPFC_DRB_ACC_ALL)
4668 		for (i = 1; i <= LPFC_DRB_MAX; i++)
4669 			len = lpfc_idiag_drbacc_read_reg(phba,
4670 							 pbuffer, len, i);
4671 	else
4672 		len = lpfc_idiag_drbacc_read_reg(phba,
4673 						 pbuffer, len, drb_reg_id);
4674 
4675 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4676 }
4677 
4678 /**
4679  * lpfc_idiag_drbacc_write - Syntax check and set up idiag drbacc commands
4680  * @file: The file pointer to read from.
4681  * @buf: The buffer to copy the user data from.
4682  * @nbytes: The number of bytes to get.
4683  * @ppos: The position in the file to start reading from.
4684  *
4685  * This routine get the debugfs idiag command struct from user space and then
4686  * perform the syntax check for port doorbell register read (dump) or write
4687  * (set) command accordingly. In the case of port queue read command, it sets
4688  * up the command in the idiag command struct for the following debugfs read
4689  * operation. In the case of port doorbell register write operation, it
4690  * executes the write operation into the port doorbell register accordingly.
4691  *
4692  * It returns the @nbytges passing in from debugfs user space when successful.
4693  * In case of error conditions, it returns proper error code back to the user
4694  * space.
4695  **/
4696 static ssize_t
4697 lpfc_idiag_drbacc_write(struct file *file, const char __user *buf,
4698 			size_t nbytes, loff_t *ppos)
4699 {
4700 	struct lpfc_debug *debug = file->private_data;
4701 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4702 	uint32_t drb_reg_id, value, reg_val = 0;
4703 	void __iomem *drb_reg;
4704 	int rc;
4705 
4706 	/* This is a user write operation */
4707 	debug->op = LPFC_IDIAG_OP_WR;
4708 
4709 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4710 	if (rc < 0)
4711 		return rc;
4712 
4713 	/* Sanity check on command line arguments */
4714 	drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
4715 	value = idiag.cmd.data[IDIAG_DRBACC_VALUE_INDX];
4716 
4717 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
4718 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
4719 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4720 		if (rc != LPFC_DRB_ACC_WR_CMD_ARG)
4721 			goto error_out;
4722 		if (drb_reg_id > LPFC_DRB_MAX)
4723 			goto error_out;
4724 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD) {
4725 		if (rc != LPFC_DRB_ACC_RD_CMD_ARG)
4726 			goto error_out;
4727 		if ((drb_reg_id > LPFC_DRB_MAX) &&
4728 		    (drb_reg_id != LPFC_DRB_ACC_ALL))
4729 			goto error_out;
4730 	} else
4731 		goto error_out;
4732 
4733 	/* Perform the write access operation */
4734 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
4735 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
4736 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4737 		switch (drb_reg_id) {
4738 		case LPFC_DRB_EQ:
4739 			drb_reg = phba->sli4_hba.EQDBregaddr;
4740 			break;
4741 		case LPFC_DRB_CQ:
4742 			drb_reg = phba->sli4_hba.CQDBregaddr;
4743 			break;
4744 		case LPFC_DRB_MQ:
4745 			drb_reg = phba->sli4_hba.MQDBregaddr;
4746 			break;
4747 		case LPFC_DRB_WQ:
4748 			drb_reg = phba->sli4_hba.WQDBregaddr;
4749 			break;
4750 		case LPFC_DRB_RQ:
4751 			drb_reg = phba->sli4_hba.RQDBregaddr;
4752 			break;
4753 		default:
4754 			goto error_out;
4755 		}
4756 
4757 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR)
4758 			reg_val = value;
4759 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST) {
4760 			reg_val = readl(drb_reg);
4761 			reg_val |= value;
4762 		}
4763 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4764 			reg_val = readl(drb_reg);
4765 			reg_val &= ~value;
4766 		}
4767 		writel(reg_val, drb_reg);
4768 		readl(drb_reg); /* flush */
4769 	}
4770 	return nbytes;
4771 
4772 error_out:
4773 	/* Clean out command structure on command error out */
4774 	memset(&idiag, 0, sizeof(idiag));
4775 	return -EINVAL;
4776 }
4777 
4778 /**
4779  * lpfc_idiag_ctlacc_read_reg - idiag debugfs read a control registers
4780  * @phba: The pointer to hba structure.
4781  * @pbuffer: The pointer to the buffer to copy the data to.
4782  * @len: The length of bytes to copied.
4783  * @drbregid: The id to doorbell registers.
4784  *
4785  * Description:
4786  * This routine reads a control register and copies its content to the
4787  * user buffer pointed to by @pbuffer.
4788  *
4789  * Returns:
4790  * This function returns the amount of data that was copied into @pbuffer.
4791  **/
4792 static int
4793 lpfc_idiag_ctlacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
4794 			   int len, uint32_t ctlregid)
4795 {
4796 
4797 	if (!pbuffer)
4798 		return 0;
4799 
4800 	switch (ctlregid) {
4801 	case LPFC_CTL_PORT_SEM:
4802 		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4803 				"Port SemReg:   0x%08x\n",
4804 				readl(phba->sli4_hba.conf_regs_memmap_p +
4805 				      LPFC_CTL_PORT_SEM_OFFSET));
4806 		break;
4807 	case LPFC_CTL_PORT_STA:
4808 		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4809 				"Port StaReg:   0x%08x\n",
4810 				readl(phba->sli4_hba.conf_regs_memmap_p +
4811 				      LPFC_CTL_PORT_STA_OFFSET));
4812 		break;
4813 	case LPFC_CTL_PORT_CTL:
4814 		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4815 				"Port CtlReg:   0x%08x\n",
4816 				readl(phba->sli4_hba.conf_regs_memmap_p +
4817 				      LPFC_CTL_PORT_CTL_OFFSET));
4818 		break;
4819 	case LPFC_CTL_PORT_ER1:
4820 		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4821 				"Port Er1Reg:   0x%08x\n",
4822 				readl(phba->sli4_hba.conf_regs_memmap_p +
4823 				      LPFC_CTL_PORT_ER1_OFFSET));
4824 		break;
4825 	case LPFC_CTL_PORT_ER2:
4826 		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4827 				"Port Er2Reg:   0x%08x\n",
4828 				readl(phba->sli4_hba.conf_regs_memmap_p +
4829 				      LPFC_CTL_PORT_ER2_OFFSET));
4830 		break;
4831 	case LPFC_CTL_PDEV_CTL:
4832 		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4833 				"PDev CtlReg:   0x%08x\n",
4834 				readl(phba->sli4_hba.conf_regs_memmap_p +
4835 				      LPFC_CTL_PDEV_CTL_OFFSET));
4836 		break;
4837 	default:
4838 		break;
4839 	}
4840 	return len;
4841 }
4842 
4843 /**
4844  * lpfc_idiag_ctlacc_read - idiag debugfs read port and device control register
4845  * @file: The file pointer to read from.
4846  * @buf: The buffer to copy the data to.
4847  * @nbytes: The number of bytes to read.
4848  * @ppos: The position in the file to start reading from.
4849  *
4850  * Description:
4851  * This routine reads data from the @phba port and device registers according
4852  * to the idiag command, and copies to user @buf.
4853  *
4854  * Returns:
4855  * This function returns the amount of data that was read (this could be less
4856  * than @nbytes if the end of the file was reached) or a negative error value.
4857  **/
4858 static ssize_t
4859 lpfc_idiag_ctlacc_read(struct file *file, char __user *buf, size_t nbytes,
4860 		       loff_t *ppos)
4861 {
4862 	struct lpfc_debug *debug = file->private_data;
4863 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4864 	uint32_t ctl_reg_id, i;
4865 	char *pbuffer;
4866 	int len = 0;
4867 
4868 	/* This is a user read operation */
4869 	debug->op = LPFC_IDIAG_OP_RD;
4870 
4871 	if (!debug->buffer)
4872 		debug->buffer = kmalloc(LPFC_CTL_ACC_BUF_SIZE, GFP_KERNEL);
4873 	if (!debug->buffer)
4874 		return 0;
4875 	pbuffer = debug->buffer;
4876 
4877 	if (*ppos)
4878 		return 0;
4879 
4880 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD)
4881 		ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
4882 	else
4883 		return 0;
4884 
4885 	if (ctl_reg_id == LPFC_CTL_ACC_ALL)
4886 		for (i = 1; i <= LPFC_CTL_MAX; i++)
4887 			len = lpfc_idiag_ctlacc_read_reg(phba,
4888 							 pbuffer, len, i);
4889 	else
4890 		len = lpfc_idiag_ctlacc_read_reg(phba,
4891 						 pbuffer, len, ctl_reg_id);
4892 
4893 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4894 }
4895 
4896 /**
4897  * lpfc_idiag_ctlacc_write - Syntax check and set up idiag ctlacc commands
4898  * @file: The file pointer to read from.
4899  * @buf: The buffer to copy the user data from.
4900  * @nbytes: The number of bytes to get.
4901  * @ppos: The position in the file to start reading from.
4902  *
4903  * This routine get the debugfs idiag command struct from user space and then
4904  * perform the syntax check for port and device control register read (dump)
4905  * or write (set) command accordingly.
4906  *
4907  * It returns the @nbytges passing in from debugfs user space when successful.
4908  * In case of error conditions, it returns proper error code back to the user
4909  * space.
4910  **/
4911 static ssize_t
4912 lpfc_idiag_ctlacc_write(struct file *file, const char __user *buf,
4913 			size_t nbytes, loff_t *ppos)
4914 {
4915 	struct lpfc_debug *debug = file->private_data;
4916 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4917 	uint32_t ctl_reg_id, value, reg_val = 0;
4918 	void __iomem *ctl_reg;
4919 	int rc;
4920 
4921 	/* This is a user write operation */
4922 	debug->op = LPFC_IDIAG_OP_WR;
4923 
4924 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4925 	if (rc < 0)
4926 		return rc;
4927 
4928 	/* Sanity check on command line arguments */
4929 	ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
4930 	value = idiag.cmd.data[IDIAG_CTLACC_VALUE_INDX];
4931 
4932 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
4933 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
4934 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4935 		if (rc != LPFC_CTL_ACC_WR_CMD_ARG)
4936 			goto error_out;
4937 		if (ctl_reg_id > LPFC_CTL_MAX)
4938 			goto error_out;
4939 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD) {
4940 		if (rc != LPFC_CTL_ACC_RD_CMD_ARG)
4941 			goto error_out;
4942 		if ((ctl_reg_id > LPFC_CTL_MAX) &&
4943 		    (ctl_reg_id != LPFC_CTL_ACC_ALL))
4944 			goto error_out;
4945 	} else
4946 		goto error_out;
4947 
4948 	/* Perform the write access operation */
4949 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
4950 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
4951 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4952 		switch (ctl_reg_id) {
4953 		case LPFC_CTL_PORT_SEM:
4954 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4955 					LPFC_CTL_PORT_SEM_OFFSET;
4956 			break;
4957 		case LPFC_CTL_PORT_STA:
4958 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4959 					LPFC_CTL_PORT_STA_OFFSET;
4960 			break;
4961 		case LPFC_CTL_PORT_CTL:
4962 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4963 					LPFC_CTL_PORT_CTL_OFFSET;
4964 			break;
4965 		case LPFC_CTL_PORT_ER1:
4966 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4967 					LPFC_CTL_PORT_ER1_OFFSET;
4968 			break;
4969 		case LPFC_CTL_PORT_ER2:
4970 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4971 					LPFC_CTL_PORT_ER2_OFFSET;
4972 			break;
4973 		case LPFC_CTL_PDEV_CTL:
4974 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4975 					LPFC_CTL_PDEV_CTL_OFFSET;
4976 			break;
4977 		default:
4978 			goto error_out;
4979 		}
4980 
4981 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR)
4982 			reg_val = value;
4983 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST) {
4984 			reg_val = readl(ctl_reg);
4985 			reg_val |= value;
4986 		}
4987 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4988 			reg_val = readl(ctl_reg);
4989 			reg_val &= ~value;
4990 		}
4991 		writel(reg_val, ctl_reg);
4992 		readl(ctl_reg); /* flush */
4993 	}
4994 	return nbytes;
4995 
4996 error_out:
4997 	/* Clean out command structure on command error out */
4998 	memset(&idiag, 0, sizeof(idiag));
4999 	return -EINVAL;
5000 }
5001 
5002 /**
5003  * lpfc_idiag_mbxacc_get_setup - idiag debugfs get mailbox access setup
5004  * @phba: Pointer to HBA context object.
5005  * @pbuffer: Pointer to data buffer.
5006  *
5007  * Description:
5008  * This routine gets the driver mailbox access debugfs setup information.
5009  *
5010  * Returns:
5011  * This function returns the amount of data that was read (this could be less
5012  * than @nbytes if the end of the file was reached) or a negative error value.
5013  **/
5014 static int
5015 lpfc_idiag_mbxacc_get_setup(struct lpfc_hba *phba, char *pbuffer)
5016 {
5017 	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
5018 	int len = 0;
5019 
5020 	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5021 	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5022 	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5023 	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5024 
5025 	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5026 			"mbx_dump_map: 0x%08x\n", mbx_dump_map);
5027 	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5028 			"mbx_dump_cnt: %04d\n", mbx_dump_cnt);
5029 	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5030 			"mbx_word_cnt: %04d\n", mbx_word_cnt);
5031 	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5032 			"mbx_mbox_cmd: 0x%02x\n", mbx_mbox_cmd);
5033 
5034 	return len;
5035 }
5036 
5037 /**
5038  * lpfc_idiag_mbxacc_read - idiag debugfs read on mailbox access
5039  * @file: The file pointer to read from.
5040  * @buf: The buffer to copy the data to.
5041  * @nbytes: The number of bytes to read.
5042  * @ppos: The position in the file to start reading from.
5043  *
5044  * Description:
5045  * This routine reads data from the @phba driver mailbox access debugfs setup
5046  * information.
5047  *
5048  * Returns:
5049  * This function returns the amount of data that was read (this could be less
5050  * than @nbytes if the end of the file was reached) or a negative error value.
5051  **/
5052 static ssize_t
5053 lpfc_idiag_mbxacc_read(struct file *file, char __user *buf, size_t nbytes,
5054 		       loff_t *ppos)
5055 {
5056 	struct lpfc_debug *debug = file->private_data;
5057 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
5058 	char *pbuffer;
5059 	int len = 0;
5060 
5061 	/* This is a user read operation */
5062 	debug->op = LPFC_IDIAG_OP_RD;
5063 
5064 	if (!debug->buffer)
5065 		debug->buffer = kmalloc(LPFC_MBX_ACC_BUF_SIZE, GFP_KERNEL);
5066 	if (!debug->buffer)
5067 		return 0;
5068 	pbuffer = debug->buffer;
5069 
5070 	if (*ppos)
5071 		return 0;
5072 
5073 	if ((idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP) &&
5074 	    (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP))
5075 		return 0;
5076 
5077 	len = lpfc_idiag_mbxacc_get_setup(phba, pbuffer);
5078 
5079 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
5080 }
5081 
5082 /**
5083  * lpfc_idiag_mbxacc_write - Syntax check and set up idiag mbxacc commands
5084  * @file: The file pointer to read from.
5085  * @buf: The buffer to copy the user data from.
5086  * @nbytes: The number of bytes to get.
5087  * @ppos: The position in the file to start reading from.
5088  *
5089  * This routine get the debugfs idiag command struct from user space and then
5090  * perform the syntax check for driver mailbox command (dump) and sets up the
5091  * necessary states in the idiag command struct accordingly.
5092  *
5093  * It returns the @nbytges passing in from debugfs user space when successful.
5094  * In case of error conditions, it returns proper error code back to the user
5095  * space.
5096  **/
5097 static ssize_t
5098 lpfc_idiag_mbxacc_write(struct file *file, const char __user *buf,
5099 			size_t nbytes, loff_t *ppos)
5100 {
5101 	struct lpfc_debug *debug = file->private_data;
5102 	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
5103 	int rc;
5104 
5105 	/* This is a user write operation */
5106 	debug->op = LPFC_IDIAG_OP_WR;
5107 
5108 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
5109 	if (rc < 0)
5110 		return rc;
5111 
5112 	/* Sanity check on command line arguments */
5113 	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5114 	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5115 	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5116 	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5117 
5118 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_MBXACC_DP) {
5119 		if (!(mbx_dump_map & LPFC_MBX_DMP_MBX_ALL))
5120 			goto error_out;
5121 		if ((mbx_dump_map & ~LPFC_MBX_DMP_MBX_ALL) &&
5122 		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
5123 			goto error_out;
5124 		if (mbx_word_cnt > sizeof(MAILBOX_t))
5125 			goto error_out;
5126 	} else if (idiag.cmd.opcode == LPFC_IDIAG_BSG_MBXACC_DP) {
5127 		if (!(mbx_dump_map & LPFC_BSG_DMP_MBX_ALL))
5128 			goto error_out;
5129 		if ((mbx_dump_map & ~LPFC_BSG_DMP_MBX_ALL) &&
5130 		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
5131 			goto error_out;
5132 		if (mbx_word_cnt > (BSG_MBOX_SIZE)/4)
5133 			goto error_out;
5134 		if (mbx_mbox_cmd != 0x9b)
5135 			goto error_out;
5136 	} else
5137 		goto error_out;
5138 
5139 	if (mbx_word_cnt == 0)
5140 		goto error_out;
5141 	if (rc != LPFC_MBX_DMP_ARG)
5142 		goto error_out;
5143 	if (mbx_mbox_cmd & ~0xff)
5144 		goto error_out;
5145 
5146 	/* condition for stop mailbox dump */
5147 	if (mbx_dump_cnt == 0)
5148 		goto reset_out;
5149 
5150 	return nbytes;
5151 
5152 reset_out:
5153 	/* Clean out command structure on command error out */
5154 	memset(&idiag, 0, sizeof(idiag));
5155 	return nbytes;
5156 
5157 error_out:
5158 	/* Clean out command structure on command error out */
5159 	memset(&idiag, 0, sizeof(idiag));
5160 	return -EINVAL;
5161 }
5162 
5163 /**
5164  * lpfc_idiag_extacc_avail_get - get the available extents information
5165  * @phba: pointer to lpfc hba data structure.
5166  * @pbuffer: pointer to internal buffer.
5167  * @len: length into the internal buffer data has been copied.
5168  *
5169  * Description:
5170  * This routine is to get the available extent information.
5171  *
5172  * Returns:
5173  * overall lenth of the data read into the internal buffer.
5174  **/
5175 static int
5176 lpfc_idiag_extacc_avail_get(struct lpfc_hba *phba, char *pbuffer, int len)
5177 {
5178 	uint16_t ext_cnt, ext_size;
5179 
5180 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5181 			"\nAvailable Extents Information:\n");
5182 
5183 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5184 			"\tPort Available VPI extents: ");
5185 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VPI,
5186 				       &ext_cnt, &ext_size);
5187 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5188 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
5189 
5190 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5191 			"\tPort Available VFI extents: ");
5192 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VFI,
5193 				       &ext_cnt, &ext_size);
5194 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5195 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
5196 
5197 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5198 			"\tPort Available RPI extents: ");
5199 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_RPI,
5200 				       &ext_cnt, &ext_size);
5201 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5202 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
5203 
5204 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5205 			"\tPort Available XRI extents: ");
5206 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_XRI,
5207 				       &ext_cnt, &ext_size);
5208 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5209 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
5210 
5211 	return len;
5212 }
5213 
5214 /**
5215  * lpfc_idiag_extacc_alloc_get - get the allocated extents information
5216  * @phba: pointer to lpfc hba data structure.
5217  * @pbuffer: pointer to internal buffer.
5218  * @len: length into the internal buffer data has been copied.
5219  *
5220  * Description:
5221  * This routine is to get the allocated extent information.
5222  *
5223  * Returns:
5224  * overall lenth of the data read into the internal buffer.
5225  **/
5226 static int
5227 lpfc_idiag_extacc_alloc_get(struct lpfc_hba *phba, char *pbuffer, int len)
5228 {
5229 	uint16_t ext_cnt, ext_size;
5230 	int rc;
5231 
5232 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5233 			"\nAllocated Extents Information:\n");
5234 
5235 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5236 			"\tHost Allocated VPI extents: ");
5237 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VPI,
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 VFI extents: ");
5249 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VFI,
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 RPI extents: ");
5261 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_RPI,
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 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5272 			"\tHost Allocated XRI extents: ");
5273 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_XRI,
5274 					    &ext_cnt, &ext_size);
5275 	if (!rc)
5276 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5277 				"Port %d Extent %3d, Size %3d\n",
5278 				phba->brd_no, ext_cnt, ext_size);
5279 	else
5280 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5281 				"N/A\n");
5282 
5283 	return len;
5284 }
5285 
5286 /**
5287  * lpfc_idiag_extacc_drivr_get - get driver extent information
5288  * @phba: pointer to lpfc hba data structure.
5289  * @pbuffer: pointer to internal buffer.
5290  * @len: length into the internal buffer data has been copied.
5291  *
5292  * Description:
5293  * This routine is to get the driver extent information.
5294  *
5295  * Returns:
5296  * overall lenth of the data read into the internal buffer.
5297  **/
5298 static int
5299 lpfc_idiag_extacc_drivr_get(struct lpfc_hba *phba, char *pbuffer, int len)
5300 {
5301 	struct lpfc_rsrc_blks *rsrc_blks;
5302 	int index;
5303 
5304 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5305 			"\nDriver Extents Information:\n");
5306 
5307 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5308 			"\tVPI extents:\n");
5309 	index = 0;
5310 	list_for_each_entry(rsrc_blks, &phba->lpfc_vpi_blk_list, list) {
5311 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5312 				"\t\tBlock %3d: Start %4d, Count %4d\n",
5313 				index, rsrc_blks->rsrc_start,
5314 				rsrc_blks->rsrc_size);
5315 		index++;
5316 	}
5317 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5318 			"\tVFI extents:\n");
5319 	index = 0;
5320 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_vfi_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 			"\tRPI extents:\n");
5331 	index = 0;
5332 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_rpi_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 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5342 			"\tXRI extents:\n");
5343 	index = 0;
5344 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_xri_blk_list,
5345 			    list) {
5346 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5347 				"\t\tBlock %3d: Start %4d, Count %4d\n",
5348 				index, rsrc_blks->rsrc_start,
5349 				rsrc_blks->rsrc_size);
5350 		index++;
5351 	}
5352 
5353 	return len;
5354 }
5355 
5356 /**
5357  * lpfc_idiag_extacc_write - Syntax check and set up idiag extacc commands
5358  * @file: The file pointer to read from.
5359  * @buf: The buffer to copy the user data from.
5360  * @nbytes: The number of bytes to get.
5361  * @ppos: The position in the file to start reading from.
5362  *
5363  * This routine get the debugfs idiag command struct from user space and then
5364  * perform the syntax check for extent information access commands and sets
5365  * up the necessary states in the idiag command struct accordingly.
5366  *
5367  * It returns the @nbytges passing in from debugfs user space when successful.
5368  * In case of error conditions, it returns proper error code back to the user
5369  * space.
5370  **/
5371 static ssize_t
5372 lpfc_idiag_extacc_write(struct file *file, const char __user *buf,
5373 			size_t nbytes, loff_t *ppos)
5374 {
5375 	struct lpfc_debug *debug = file->private_data;
5376 	uint32_t ext_map;
5377 	int rc;
5378 
5379 	/* This is a user write operation */
5380 	debug->op = LPFC_IDIAG_OP_WR;
5381 
5382 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
5383 	if (rc < 0)
5384 		return rc;
5385 
5386 	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
5387 
5388 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
5389 		goto error_out;
5390 	if (rc != LPFC_EXT_ACC_CMD_ARG)
5391 		goto error_out;
5392 	if (!(ext_map & LPFC_EXT_ACC_ALL))
5393 		goto error_out;
5394 
5395 	return nbytes;
5396 error_out:
5397 	/* Clean out command structure on command error out */
5398 	memset(&idiag, 0, sizeof(idiag));
5399 	return -EINVAL;
5400 }
5401 
5402 /**
5403  * lpfc_idiag_extacc_read - idiag debugfs read access to extent information
5404  * @file: The file pointer to read from.
5405  * @buf: The buffer to copy the data to.
5406  * @nbytes: The number of bytes to read.
5407  * @ppos: The position in the file to start reading from.
5408  *
5409  * Description:
5410  * This routine reads data from the proper extent information according to
5411  * the idiag command, and copies to user @buf.
5412  *
5413  * Returns:
5414  * This function returns the amount of data that was read (this could be less
5415  * than @nbytes if the end of the file was reached) or a negative error value.
5416  **/
5417 static ssize_t
5418 lpfc_idiag_extacc_read(struct file *file, char __user *buf, size_t nbytes,
5419 		       loff_t *ppos)
5420 {
5421 	struct lpfc_debug *debug = file->private_data;
5422 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
5423 	char *pbuffer;
5424 	uint32_t ext_map;
5425 	int len = 0;
5426 
5427 	/* This is a user read operation */
5428 	debug->op = LPFC_IDIAG_OP_RD;
5429 
5430 	if (!debug->buffer)
5431 		debug->buffer = kmalloc(LPFC_EXT_ACC_BUF_SIZE, GFP_KERNEL);
5432 	if (!debug->buffer)
5433 		return 0;
5434 	pbuffer = debug->buffer;
5435 	if (*ppos)
5436 		return 0;
5437 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
5438 		return 0;
5439 
5440 	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
5441 	if (ext_map & LPFC_EXT_ACC_AVAIL)
5442 		len = lpfc_idiag_extacc_avail_get(phba, pbuffer, len);
5443 	if (ext_map & LPFC_EXT_ACC_ALLOC)
5444 		len = lpfc_idiag_extacc_alloc_get(phba, pbuffer, len);
5445 	if (ext_map & LPFC_EXT_ACC_DRIVR)
5446 		len = lpfc_idiag_extacc_drivr_get(phba, pbuffer, len);
5447 
5448 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
5449 }
5450 
5451 #undef lpfc_debugfs_op_disc_trc
5452 static const struct file_operations lpfc_debugfs_op_disc_trc = {
5453 	.owner =        THIS_MODULE,
5454 	.open =         lpfc_debugfs_disc_trc_open,
5455 	.llseek =       lpfc_debugfs_lseek,
5456 	.read =         lpfc_debugfs_read,
5457 	.release =      lpfc_debugfs_release,
5458 };
5459 
5460 #undef lpfc_debugfs_op_nodelist
5461 static const struct file_operations lpfc_debugfs_op_nodelist = {
5462 	.owner =        THIS_MODULE,
5463 	.open =         lpfc_debugfs_nodelist_open,
5464 	.llseek =       lpfc_debugfs_lseek,
5465 	.read =         lpfc_debugfs_read,
5466 	.release =      lpfc_debugfs_release,
5467 };
5468 
5469 #undef lpfc_debugfs_op_multixripools
5470 static const struct file_operations lpfc_debugfs_op_multixripools = {
5471 	.owner =        THIS_MODULE,
5472 	.open =         lpfc_debugfs_multixripools_open,
5473 	.llseek =       lpfc_debugfs_lseek,
5474 	.read =         lpfc_debugfs_read,
5475 	.write =	lpfc_debugfs_multixripools_write,
5476 	.release =      lpfc_debugfs_release,
5477 };
5478 
5479 #undef lpfc_debugfs_op_hbqinfo
5480 static const struct file_operations lpfc_debugfs_op_hbqinfo = {
5481 	.owner =        THIS_MODULE,
5482 	.open =         lpfc_debugfs_hbqinfo_open,
5483 	.llseek =       lpfc_debugfs_lseek,
5484 	.read =         lpfc_debugfs_read,
5485 	.release =      lpfc_debugfs_release,
5486 };
5487 
5488 #ifdef LPFC_HDWQ_LOCK_STAT
5489 #undef lpfc_debugfs_op_lockstat
5490 static const struct file_operations lpfc_debugfs_op_lockstat = {
5491 	.owner =        THIS_MODULE,
5492 	.open =         lpfc_debugfs_lockstat_open,
5493 	.llseek =       lpfc_debugfs_lseek,
5494 	.read =         lpfc_debugfs_read,
5495 	.write =        lpfc_debugfs_lockstat_write,
5496 	.release =      lpfc_debugfs_release,
5497 };
5498 #endif
5499 
5500 #undef lpfc_debugfs_ras_log
5501 static const struct file_operations lpfc_debugfs_ras_log = {
5502 	.owner =        THIS_MODULE,
5503 	.open =         lpfc_debugfs_ras_log_open,
5504 	.llseek =       lpfc_debugfs_lseek,
5505 	.read =         lpfc_debugfs_read,
5506 	.release =      lpfc_debugfs_ras_log_release,
5507 };
5508 
5509 #undef lpfc_debugfs_op_dumpHBASlim
5510 static const struct file_operations lpfc_debugfs_op_dumpHBASlim = {
5511 	.owner =        THIS_MODULE,
5512 	.open =         lpfc_debugfs_dumpHBASlim_open,
5513 	.llseek =       lpfc_debugfs_lseek,
5514 	.read =         lpfc_debugfs_read,
5515 	.release =      lpfc_debugfs_release,
5516 };
5517 
5518 #undef lpfc_debugfs_op_dumpHostSlim
5519 static const struct file_operations lpfc_debugfs_op_dumpHostSlim = {
5520 	.owner =        THIS_MODULE,
5521 	.open =         lpfc_debugfs_dumpHostSlim_open,
5522 	.llseek =       lpfc_debugfs_lseek,
5523 	.read =         lpfc_debugfs_read,
5524 	.release =      lpfc_debugfs_release,
5525 };
5526 
5527 #undef lpfc_debugfs_op_nvmestat
5528 static const struct file_operations lpfc_debugfs_op_nvmestat = {
5529 	.owner =        THIS_MODULE,
5530 	.open =         lpfc_debugfs_nvmestat_open,
5531 	.llseek =       lpfc_debugfs_lseek,
5532 	.read =         lpfc_debugfs_read,
5533 	.write =	lpfc_debugfs_nvmestat_write,
5534 	.release =      lpfc_debugfs_release,
5535 };
5536 
5537 #undef lpfc_debugfs_op_scsistat
5538 static const struct file_operations lpfc_debugfs_op_scsistat = {
5539 	.owner =        THIS_MODULE,
5540 	.open =         lpfc_debugfs_scsistat_open,
5541 	.llseek =       lpfc_debugfs_lseek,
5542 	.read =         lpfc_debugfs_read,
5543 	.write =	lpfc_debugfs_scsistat_write,
5544 	.release =      lpfc_debugfs_release,
5545 };
5546 
5547 #undef lpfc_debugfs_op_ioktime
5548 static const struct file_operations lpfc_debugfs_op_ioktime = {
5549 	.owner =        THIS_MODULE,
5550 	.open =         lpfc_debugfs_ioktime_open,
5551 	.llseek =       lpfc_debugfs_lseek,
5552 	.read =         lpfc_debugfs_read,
5553 	.write =	lpfc_debugfs_ioktime_write,
5554 	.release =      lpfc_debugfs_release,
5555 };
5556 
5557 #undef lpfc_debugfs_op_nvmeio_trc
5558 static const struct file_operations lpfc_debugfs_op_nvmeio_trc = {
5559 	.owner =        THIS_MODULE,
5560 	.open =         lpfc_debugfs_nvmeio_trc_open,
5561 	.llseek =       lpfc_debugfs_lseek,
5562 	.read =         lpfc_debugfs_read,
5563 	.write =	lpfc_debugfs_nvmeio_trc_write,
5564 	.release =      lpfc_debugfs_release,
5565 };
5566 
5567 #undef lpfc_debugfs_op_hdwqstat
5568 static const struct file_operations lpfc_debugfs_op_hdwqstat = {
5569 	.owner =        THIS_MODULE,
5570 	.open =         lpfc_debugfs_hdwqstat_open,
5571 	.llseek =       lpfc_debugfs_lseek,
5572 	.read =         lpfc_debugfs_read,
5573 	.write =	lpfc_debugfs_hdwqstat_write,
5574 	.release =      lpfc_debugfs_release,
5575 };
5576 
5577 #undef lpfc_debugfs_op_dif_err
5578 static const struct file_operations lpfc_debugfs_op_dif_err = {
5579 	.owner =	THIS_MODULE,
5580 	.open =		simple_open,
5581 	.llseek =	lpfc_debugfs_lseek,
5582 	.read =		lpfc_debugfs_dif_err_read,
5583 	.write =	lpfc_debugfs_dif_err_write,
5584 	.release =	lpfc_debugfs_dif_err_release,
5585 };
5586 
5587 #undef lpfc_debugfs_op_slow_ring_trc
5588 static const struct file_operations lpfc_debugfs_op_slow_ring_trc = {
5589 	.owner =        THIS_MODULE,
5590 	.open =         lpfc_debugfs_slow_ring_trc_open,
5591 	.llseek =       lpfc_debugfs_lseek,
5592 	.read =         lpfc_debugfs_read,
5593 	.release =      lpfc_debugfs_release,
5594 };
5595 
5596 static struct dentry *lpfc_debugfs_root = NULL;
5597 static atomic_t lpfc_debugfs_hba_count;
5598 
5599 /*
5600  * File operations for the iDiag debugfs
5601  */
5602 #undef lpfc_idiag_op_pciCfg
5603 static const struct file_operations lpfc_idiag_op_pciCfg = {
5604 	.owner =        THIS_MODULE,
5605 	.open =         lpfc_idiag_open,
5606 	.llseek =       lpfc_debugfs_lseek,
5607 	.read =         lpfc_idiag_pcicfg_read,
5608 	.write =        lpfc_idiag_pcicfg_write,
5609 	.release =      lpfc_idiag_cmd_release,
5610 };
5611 
5612 #undef lpfc_idiag_op_barAcc
5613 static const struct file_operations lpfc_idiag_op_barAcc = {
5614 	.owner =        THIS_MODULE,
5615 	.open =         lpfc_idiag_open,
5616 	.llseek =       lpfc_debugfs_lseek,
5617 	.read =         lpfc_idiag_baracc_read,
5618 	.write =        lpfc_idiag_baracc_write,
5619 	.release =      lpfc_idiag_cmd_release,
5620 };
5621 
5622 #undef lpfc_idiag_op_queInfo
5623 static const struct file_operations lpfc_idiag_op_queInfo = {
5624 	.owner =        THIS_MODULE,
5625 	.open =         lpfc_idiag_open,
5626 	.read =         lpfc_idiag_queinfo_read,
5627 	.release =      lpfc_idiag_release,
5628 };
5629 
5630 #undef lpfc_idiag_op_queAcc
5631 static const struct file_operations lpfc_idiag_op_queAcc = {
5632 	.owner =        THIS_MODULE,
5633 	.open =         lpfc_idiag_open,
5634 	.llseek =       lpfc_debugfs_lseek,
5635 	.read =         lpfc_idiag_queacc_read,
5636 	.write =        lpfc_idiag_queacc_write,
5637 	.release =      lpfc_idiag_cmd_release,
5638 };
5639 
5640 #undef lpfc_idiag_op_drbAcc
5641 static const struct file_operations lpfc_idiag_op_drbAcc = {
5642 	.owner =        THIS_MODULE,
5643 	.open =         lpfc_idiag_open,
5644 	.llseek =       lpfc_debugfs_lseek,
5645 	.read =         lpfc_idiag_drbacc_read,
5646 	.write =        lpfc_idiag_drbacc_write,
5647 	.release =      lpfc_idiag_cmd_release,
5648 };
5649 
5650 #undef lpfc_idiag_op_ctlAcc
5651 static const struct file_operations lpfc_idiag_op_ctlAcc = {
5652 	.owner =        THIS_MODULE,
5653 	.open =         lpfc_idiag_open,
5654 	.llseek =       lpfc_debugfs_lseek,
5655 	.read =         lpfc_idiag_ctlacc_read,
5656 	.write =        lpfc_idiag_ctlacc_write,
5657 	.release =      lpfc_idiag_cmd_release,
5658 };
5659 
5660 #undef lpfc_idiag_op_mbxAcc
5661 static const struct file_operations lpfc_idiag_op_mbxAcc = {
5662 	.owner =        THIS_MODULE,
5663 	.open =         lpfc_idiag_open,
5664 	.llseek =       lpfc_debugfs_lseek,
5665 	.read =         lpfc_idiag_mbxacc_read,
5666 	.write =        lpfc_idiag_mbxacc_write,
5667 	.release =      lpfc_idiag_cmd_release,
5668 };
5669 
5670 #undef lpfc_idiag_op_extAcc
5671 static const struct file_operations lpfc_idiag_op_extAcc = {
5672 	.owner =        THIS_MODULE,
5673 	.open =         lpfc_idiag_open,
5674 	.llseek =       lpfc_debugfs_lseek,
5675 	.read =         lpfc_idiag_extacc_read,
5676 	.write =        lpfc_idiag_extacc_write,
5677 	.release =      lpfc_idiag_cmd_release,
5678 };
5679 #endif
5680 
5681 /* lpfc_idiag_mbxacc_dump_bsg_mbox - idiag debugfs dump bsg mailbox command
5682  * @phba: Pointer to HBA context object.
5683  * @dmabuf: Pointer to a DMA buffer descriptor.
5684  *
5685  * Description:
5686  * This routine dump a bsg pass-through non-embedded mailbox command with
5687  * external buffer.
5688  **/
5689 void
5690 lpfc_idiag_mbxacc_dump_bsg_mbox(struct lpfc_hba *phba, enum nemb_type nemb_tp,
5691 				enum mbox_type mbox_tp, enum dma_type dma_tp,
5692 				enum sta_type sta_tp,
5693 				struct lpfc_dmabuf *dmabuf, uint32_t ext_buf)
5694 {
5695 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5696 	uint32_t *mbx_mbox_cmd, *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt;
5697 	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
5698 	int len = 0;
5699 	uint32_t do_dump = 0;
5700 	uint32_t *pword;
5701 	uint32_t i;
5702 
5703 	if (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP)
5704 		return;
5705 
5706 	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5707 	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5708 	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5709 	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5710 
5711 	if (!(*mbx_dump_map & LPFC_MBX_DMP_ALL) ||
5712 	    (*mbx_dump_cnt == 0) ||
5713 	    (*mbx_word_cnt == 0))
5714 		return;
5715 
5716 	if (*mbx_mbox_cmd != 0x9B)
5717 		return;
5718 
5719 	if ((mbox_tp == mbox_rd) && (dma_tp == dma_mbox)) {
5720 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_MBX) {
5721 			do_dump |= LPFC_BSG_DMP_MBX_RD_MBX;
5722 			pr_err("\nRead mbox command (x%x), "
5723 			       "nemb:0x%x, extbuf_cnt:%d:\n",
5724 			       sta_tp, nemb_tp, ext_buf);
5725 		}
5726 	}
5727 	if ((mbox_tp == mbox_rd) && (dma_tp == dma_ebuf)) {
5728 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_BUF) {
5729 			do_dump |= LPFC_BSG_DMP_MBX_RD_BUF;
5730 			pr_err("\nRead mbox buffer (x%x), "
5731 			       "nemb:0x%x, extbuf_seq:%d:\n",
5732 			       sta_tp, nemb_tp, ext_buf);
5733 		}
5734 	}
5735 	if ((mbox_tp == mbox_wr) && (dma_tp == dma_mbox)) {
5736 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_MBX) {
5737 			do_dump |= LPFC_BSG_DMP_MBX_WR_MBX;
5738 			pr_err("\nWrite mbox command (x%x), "
5739 			       "nemb:0x%x, extbuf_cnt:%d:\n",
5740 			       sta_tp, nemb_tp, ext_buf);
5741 		}
5742 	}
5743 	if ((mbox_tp == mbox_wr) && (dma_tp == dma_ebuf)) {
5744 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_BUF) {
5745 			do_dump |= LPFC_BSG_DMP_MBX_WR_BUF;
5746 			pr_err("\nWrite mbox buffer (x%x), "
5747 			       "nemb:0x%x, extbuf_seq:%d:\n",
5748 			       sta_tp, nemb_tp, ext_buf);
5749 		}
5750 	}
5751 
5752 	/* dump buffer content */
5753 	if (do_dump) {
5754 		pword = (uint32_t *)dmabuf->virt;
5755 		for (i = 0; i < *mbx_word_cnt; i++) {
5756 			if (!(i % 8)) {
5757 				if (i != 0)
5758 					pr_err("%s\n", line_buf);
5759 				len = 0;
5760 				len += scnprintf(line_buf+len,
5761 						LPFC_MBX_ACC_LBUF_SZ-len,
5762 						"%03d: ", i);
5763 			}
5764 			len += scnprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
5765 					"%08x ", (uint32_t)*pword);
5766 			pword++;
5767 		}
5768 		if ((i - 1) % 8)
5769 			pr_err("%s\n", line_buf);
5770 		(*mbx_dump_cnt)--;
5771 	}
5772 
5773 	/* Clean out command structure on reaching dump count */
5774 	if (*mbx_dump_cnt == 0)
5775 		memset(&idiag, 0, sizeof(idiag));
5776 	return;
5777 #endif
5778 }
5779 
5780 /* lpfc_idiag_mbxacc_dump_issue_mbox - idiag debugfs dump issue mailbox command
5781  * @phba: Pointer to HBA context object.
5782  * @dmabuf: Pointer to a DMA buffer descriptor.
5783  *
5784  * Description:
5785  * This routine dump a pass-through non-embedded mailbox command from issue
5786  * mailbox command.
5787  **/
5788 void
5789 lpfc_idiag_mbxacc_dump_issue_mbox(struct lpfc_hba *phba, MAILBOX_t *pmbox)
5790 {
5791 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5792 	uint32_t *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt, *mbx_mbox_cmd;
5793 	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
5794 	int len = 0;
5795 	uint32_t *pword;
5796 	uint8_t *pbyte;
5797 	uint32_t i, j;
5798 
5799 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP)
5800 		return;
5801 
5802 	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5803 	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5804 	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5805 	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5806 
5807 	if (!(*mbx_dump_map & LPFC_MBX_DMP_MBX_ALL) ||
5808 	    (*mbx_dump_cnt == 0) ||
5809 	    (*mbx_word_cnt == 0))
5810 		return;
5811 
5812 	if ((*mbx_mbox_cmd != LPFC_MBX_ALL_CMD) &&
5813 	    (*mbx_mbox_cmd != pmbox->mbxCommand))
5814 		return;
5815 
5816 	/* dump buffer content */
5817 	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_WORD) {
5818 		pr_err("Mailbox command:0x%x dump by word:\n",
5819 		       pmbox->mbxCommand);
5820 		pword = (uint32_t *)pmbox;
5821 		for (i = 0; i < *mbx_word_cnt; i++) {
5822 			if (!(i % 8)) {
5823 				if (i != 0)
5824 					pr_err("%s\n", line_buf);
5825 				len = 0;
5826 				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
5827 				len += scnprintf(line_buf+len,
5828 						LPFC_MBX_ACC_LBUF_SZ-len,
5829 						"%03d: ", i);
5830 			}
5831 			len += scnprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
5832 					"%08x ",
5833 					((uint32_t)*pword) & 0xffffffff);
5834 			pword++;
5835 		}
5836 		if ((i - 1) % 8)
5837 			pr_err("%s\n", line_buf);
5838 		pr_err("\n");
5839 	}
5840 	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_BYTE) {
5841 		pr_err("Mailbox command:0x%x dump by byte:\n",
5842 		       pmbox->mbxCommand);
5843 		pbyte = (uint8_t *)pmbox;
5844 		for (i = 0; i < *mbx_word_cnt; i++) {
5845 			if (!(i % 8)) {
5846 				if (i != 0)
5847 					pr_err("%s\n", line_buf);
5848 				len = 0;
5849 				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
5850 				len += scnprintf(line_buf+len,
5851 						LPFC_MBX_ACC_LBUF_SZ-len,
5852 						"%03d: ", i);
5853 			}
5854 			for (j = 0; j < 4; j++) {
5855 				len += scnprintf(line_buf+len,
5856 						LPFC_MBX_ACC_LBUF_SZ-len,
5857 						"%02x",
5858 						((uint8_t)*pbyte) & 0xff);
5859 				pbyte++;
5860 			}
5861 			len += scnprintf(line_buf+len,
5862 					LPFC_MBX_ACC_LBUF_SZ-len, " ");
5863 		}
5864 		if ((i - 1) % 8)
5865 			pr_err("%s\n", line_buf);
5866 		pr_err("\n");
5867 	}
5868 	(*mbx_dump_cnt)--;
5869 
5870 	/* Clean out command structure on reaching dump count */
5871 	if (*mbx_dump_cnt == 0)
5872 		memset(&idiag, 0, sizeof(idiag));
5873 	return;
5874 #endif
5875 }
5876 
5877 /**
5878  * lpfc_debugfs_initialize - Initialize debugfs for a vport
5879  * @vport: The vport pointer to initialize.
5880  *
5881  * Description:
5882  * When Debugfs is configured this routine sets up the lpfc debugfs file system.
5883  * If not already created, this routine will create the lpfc directory, and
5884  * lpfcX directory (for this HBA), and vportX directory for this vport. It will
5885  * also create each file used to access lpfc specific debugfs information.
5886  **/
5887 inline void
5888 lpfc_debugfs_initialize(struct lpfc_vport *vport)
5889 {
5890 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5891 	struct lpfc_hba   *phba = vport->phba;
5892 	char name[64];
5893 	uint32_t num, i;
5894 	bool pport_setup = false;
5895 
5896 	if (!lpfc_debugfs_enable)
5897 		return;
5898 
5899 	/* Setup lpfc root directory */
5900 	if (!lpfc_debugfs_root) {
5901 		lpfc_debugfs_root = debugfs_create_dir("lpfc", NULL);
5902 		atomic_set(&lpfc_debugfs_hba_count, 0);
5903 	}
5904 	if (!lpfc_debugfs_start_time)
5905 		lpfc_debugfs_start_time = jiffies;
5906 
5907 	/* Setup funcX directory for specific HBA PCI function */
5908 	snprintf(name, sizeof(name), "fn%d", phba->brd_no);
5909 	if (!phba->hba_debugfs_root) {
5910 		pport_setup = true;
5911 		phba->hba_debugfs_root =
5912 			debugfs_create_dir(name, lpfc_debugfs_root);
5913 		atomic_inc(&lpfc_debugfs_hba_count);
5914 		atomic_set(&phba->debugfs_vport_count, 0);
5915 
5916 		/* Multi-XRI pools */
5917 		snprintf(name, sizeof(name), "multixripools");
5918 		phba->debug_multixri_pools =
5919 			debugfs_create_file(name, S_IFREG | 0644,
5920 					    phba->hba_debugfs_root,
5921 					    phba,
5922 					    &lpfc_debugfs_op_multixripools);
5923 		if (!phba->debug_multixri_pools) {
5924 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5925 					 "0527 Cannot create debugfs multixripools\n");
5926 			goto debug_failed;
5927 		}
5928 
5929 		/* RAS log */
5930 		snprintf(name, sizeof(name), "ras_log");
5931 		phba->debug_ras_log =
5932 			debugfs_create_file(name, 0644,
5933 					    phba->hba_debugfs_root,
5934 					    phba, &lpfc_debugfs_ras_log);
5935 		if (!phba->debug_ras_log) {
5936 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5937 					 "6148 Cannot create debugfs"
5938 					 " ras_log\n");
5939 			goto debug_failed;
5940 		}
5941 
5942 		/* Setup hbqinfo */
5943 		snprintf(name, sizeof(name), "hbqinfo");
5944 		phba->debug_hbqinfo =
5945 			debugfs_create_file(name, S_IFREG | 0644,
5946 					    phba->hba_debugfs_root,
5947 					    phba, &lpfc_debugfs_op_hbqinfo);
5948 
5949 #ifdef LPFC_HDWQ_LOCK_STAT
5950 		/* Setup lockstat */
5951 		snprintf(name, sizeof(name), "lockstat");
5952 		phba->debug_lockstat =
5953 			debugfs_create_file(name, S_IFREG | 0644,
5954 					    phba->hba_debugfs_root,
5955 					    phba, &lpfc_debugfs_op_lockstat);
5956 		if (!phba->debug_lockstat) {
5957 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5958 					 "4610 Cant create debugfs lockstat\n");
5959 			goto debug_failed;
5960 		}
5961 #endif
5962 
5963 		/* Setup dumpHBASlim */
5964 		if (phba->sli_rev < LPFC_SLI_REV4) {
5965 			snprintf(name, sizeof(name), "dumpHBASlim");
5966 			phba->debug_dumpHBASlim =
5967 				debugfs_create_file(name,
5968 					S_IFREG|S_IRUGO|S_IWUSR,
5969 					phba->hba_debugfs_root,
5970 					phba, &lpfc_debugfs_op_dumpHBASlim);
5971 		} else
5972 			phba->debug_dumpHBASlim = NULL;
5973 
5974 		/* Setup dumpHostSlim */
5975 		if (phba->sli_rev < LPFC_SLI_REV4) {
5976 			snprintf(name, sizeof(name), "dumpHostSlim");
5977 			phba->debug_dumpHostSlim =
5978 				debugfs_create_file(name,
5979 					S_IFREG|S_IRUGO|S_IWUSR,
5980 					phba->hba_debugfs_root,
5981 					phba, &lpfc_debugfs_op_dumpHostSlim);
5982 		} else
5983 			phba->debug_dumpHostSlim = NULL;
5984 
5985 		/* Setup DIF Error Injections */
5986 		snprintf(name, sizeof(name), "InjErrLBA");
5987 		phba->debug_InjErrLBA =
5988 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5989 			phba->hba_debugfs_root,
5990 			phba, &lpfc_debugfs_op_dif_err);
5991 		phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
5992 
5993 		snprintf(name, sizeof(name), "InjErrNPortID");
5994 		phba->debug_InjErrNPortID =
5995 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5996 			phba->hba_debugfs_root,
5997 			phba, &lpfc_debugfs_op_dif_err);
5998 
5999 		snprintf(name, sizeof(name), "InjErrWWPN");
6000 		phba->debug_InjErrWWPN =
6001 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6002 			phba->hba_debugfs_root,
6003 			phba, &lpfc_debugfs_op_dif_err);
6004 
6005 		snprintf(name, sizeof(name), "writeGuardInjErr");
6006 		phba->debug_writeGuard =
6007 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6008 			phba->hba_debugfs_root,
6009 			phba, &lpfc_debugfs_op_dif_err);
6010 
6011 		snprintf(name, sizeof(name), "writeAppInjErr");
6012 		phba->debug_writeApp =
6013 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6014 			phba->hba_debugfs_root,
6015 			phba, &lpfc_debugfs_op_dif_err);
6016 
6017 		snprintf(name, sizeof(name), "writeRefInjErr");
6018 		phba->debug_writeRef =
6019 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6020 			phba->hba_debugfs_root,
6021 			phba, &lpfc_debugfs_op_dif_err);
6022 
6023 		snprintf(name, sizeof(name), "readGuardInjErr");
6024 		phba->debug_readGuard =
6025 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6026 			phba->hba_debugfs_root,
6027 			phba, &lpfc_debugfs_op_dif_err);
6028 
6029 		snprintf(name, sizeof(name), "readAppInjErr");
6030 		phba->debug_readApp =
6031 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6032 			phba->hba_debugfs_root,
6033 			phba, &lpfc_debugfs_op_dif_err);
6034 
6035 		snprintf(name, sizeof(name), "readRefInjErr");
6036 		phba->debug_readRef =
6037 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6038 			phba->hba_debugfs_root,
6039 			phba, &lpfc_debugfs_op_dif_err);
6040 
6041 		/* Setup slow ring trace */
6042 		if (lpfc_debugfs_max_slow_ring_trc) {
6043 			num = lpfc_debugfs_max_slow_ring_trc - 1;
6044 			if (num & lpfc_debugfs_max_slow_ring_trc) {
6045 				/* Change to be a power of 2 */
6046 				num = lpfc_debugfs_max_slow_ring_trc;
6047 				i = 0;
6048 				while (num > 1) {
6049 					num = num >> 1;
6050 					i++;
6051 				}
6052 				lpfc_debugfs_max_slow_ring_trc = (1 << i);
6053 				pr_err("lpfc_debugfs_max_disc_trc changed to "
6054 				       "%d\n", lpfc_debugfs_max_disc_trc);
6055 			}
6056 		}
6057 
6058 		snprintf(name, sizeof(name), "slow_ring_trace");
6059 		phba->debug_slow_ring_trc =
6060 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6061 				 phba->hba_debugfs_root,
6062 				 phba, &lpfc_debugfs_op_slow_ring_trc);
6063 		if (!phba->slow_ring_trc) {
6064 			phba->slow_ring_trc = kmalloc(
6065 				(sizeof(struct lpfc_debugfs_trc) *
6066 				lpfc_debugfs_max_slow_ring_trc),
6067 				GFP_KERNEL);
6068 			if (!phba->slow_ring_trc) {
6069 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6070 						 "0416 Cannot create debugfs "
6071 						 "slow_ring buffer\n");
6072 				goto debug_failed;
6073 			}
6074 			atomic_set(&phba->slow_ring_trc_cnt, 0);
6075 			memset(phba->slow_ring_trc, 0,
6076 				(sizeof(struct lpfc_debugfs_trc) *
6077 				lpfc_debugfs_max_slow_ring_trc));
6078 		}
6079 
6080 		snprintf(name, sizeof(name), "nvmeio_trc");
6081 		phba->debug_nvmeio_trc =
6082 			debugfs_create_file(name, 0644,
6083 					    phba->hba_debugfs_root,
6084 					    phba, &lpfc_debugfs_op_nvmeio_trc);
6085 
6086 		atomic_set(&phba->nvmeio_trc_cnt, 0);
6087 		if (lpfc_debugfs_max_nvmeio_trc) {
6088 			num = lpfc_debugfs_max_nvmeio_trc - 1;
6089 			if (num & lpfc_debugfs_max_disc_trc) {
6090 				/* Change to be a power of 2 */
6091 				num = lpfc_debugfs_max_nvmeio_trc;
6092 				i = 0;
6093 				while (num > 1) {
6094 					num = num >> 1;
6095 					i++;
6096 				}
6097 				lpfc_debugfs_max_nvmeio_trc = (1 << i);
6098 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6099 						"0575 lpfc_debugfs_max_nvmeio_trc "
6100 						"changed to %d\n",
6101 						lpfc_debugfs_max_nvmeio_trc);
6102 			}
6103 			phba->nvmeio_trc_size = lpfc_debugfs_max_nvmeio_trc;
6104 
6105 			/* Allocate trace buffer and initialize */
6106 			phba->nvmeio_trc = kzalloc(
6107 				(sizeof(struct lpfc_debugfs_nvmeio_trc) *
6108 				phba->nvmeio_trc_size), GFP_KERNEL);
6109 
6110 			if (!phba->nvmeio_trc) {
6111 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6112 						"0576 Cannot create debugfs "
6113 						"nvmeio_trc buffer\n");
6114 				goto nvmeio_off;
6115 			}
6116 			phba->nvmeio_trc_on = 1;
6117 			phba->nvmeio_trc_output_idx = 0;
6118 			phba->nvmeio_trc = NULL;
6119 		} else {
6120 nvmeio_off:
6121 			phba->nvmeio_trc_size = 0;
6122 			phba->nvmeio_trc_on = 0;
6123 			phba->nvmeio_trc_output_idx = 0;
6124 			phba->nvmeio_trc = NULL;
6125 		}
6126 	}
6127 
6128 	snprintf(name, sizeof(name), "vport%d", vport->vpi);
6129 	if (!vport->vport_debugfs_root) {
6130 		vport->vport_debugfs_root =
6131 			debugfs_create_dir(name, phba->hba_debugfs_root);
6132 		atomic_inc(&phba->debugfs_vport_count);
6133 	}
6134 
6135 	if (lpfc_debugfs_max_disc_trc) {
6136 		num = lpfc_debugfs_max_disc_trc - 1;
6137 		if (num & lpfc_debugfs_max_disc_trc) {
6138 			/* Change to be a power of 2 */
6139 			num = lpfc_debugfs_max_disc_trc;
6140 			i = 0;
6141 			while (num > 1) {
6142 				num = num >> 1;
6143 				i++;
6144 			}
6145 			lpfc_debugfs_max_disc_trc = (1 << i);
6146 			pr_err("lpfc_debugfs_max_disc_trc changed to %d\n",
6147 			       lpfc_debugfs_max_disc_trc);
6148 		}
6149 	}
6150 
6151 	vport->disc_trc = kzalloc(
6152 		(sizeof(struct lpfc_debugfs_trc) * lpfc_debugfs_max_disc_trc),
6153 		GFP_KERNEL);
6154 
6155 	if (!vport->disc_trc) {
6156 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6157 				 "0418 Cannot create debugfs disc trace "
6158 				 "buffer\n");
6159 		goto debug_failed;
6160 	}
6161 	atomic_set(&vport->disc_trc_cnt, 0);
6162 
6163 	snprintf(name, sizeof(name), "discovery_trace");
6164 	vport->debug_disc_trc =
6165 		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6166 				 vport->vport_debugfs_root,
6167 				 vport, &lpfc_debugfs_op_disc_trc);
6168 	snprintf(name, sizeof(name), "nodelist");
6169 	vport->debug_nodelist =
6170 		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6171 				 vport->vport_debugfs_root,
6172 				 vport, &lpfc_debugfs_op_nodelist);
6173 
6174 	snprintf(name, sizeof(name), "nvmestat");
6175 	vport->debug_nvmestat =
6176 		debugfs_create_file(name, 0644,
6177 				    vport->vport_debugfs_root,
6178 				    vport, &lpfc_debugfs_op_nvmestat);
6179 
6180 	snprintf(name, sizeof(name), "scsistat");
6181 	vport->debug_scsistat =
6182 		debugfs_create_file(name, 0644,
6183 				    vport->vport_debugfs_root,
6184 				    vport, &lpfc_debugfs_op_scsistat);
6185 	if (!vport->debug_scsistat) {
6186 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6187 				 "4611 Cannot create debugfs scsistat\n");
6188 		goto debug_failed;
6189 	}
6190 
6191 	snprintf(name, sizeof(name), "ioktime");
6192 	vport->debug_ioktime =
6193 		debugfs_create_file(name, 0644,
6194 				    vport->vport_debugfs_root,
6195 				    vport, &lpfc_debugfs_op_ioktime);
6196 	if (!vport->debug_ioktime) {
6197 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6198 				 "0815 Cannot create debugfs ioktime\n");
6199 		goto debug_failed;
6200 	}
6201 
6202 	snprintf(name, sizeof(name), "hdwqstat");
6203 	vport->debug_hdwqstat =
6204 		debugfs_create_file(name, 0644,
6205 				    vport->vport_debugfs_root,
6206 				    vport, &lpfc_debugfs_op_hdwqstat);
6207 
6208 	/*
6209 	 * The following section is for additional directories/files for the
6210 	 * physical port.
6211 	 */
6212 
6213 	if (!pport_setup)
6214 		goto debug_failed;
6215 
6216 	/*
6217 	 * iDiag debugfs root entry points for SLI4 device only
6218 	 */
6219 	if (phba->sli_rev < LPFC_SLI_REV4)
6220 		goto debug_failed;
6221 
6222 	snprintf(name, sizeof(name), "iDiag");
6223 	if (!phba->idiag_root) {
6224 		phba->idiag_root =
6225 			debugfs_create_dir(name, phba->hba_debugfs_root);
6226 		/* Initialize iDiag data structure */
6227 		memset(&idiag, 0, sizeof(idiag));
6228 	}
6229 
6230 	/* iDiag read PCI config space */
6231 	snprintf(name, sizeof(name), "pciCfg");
6232 	if (!phba->idiag_pci_cfg) {
6233 		phba->idiag_pci_cfg =
6234 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6235 				phba->idiag_root, phba, &lpfc_idiag_op_pciCfg);
6236 		idiag.offset.last_rd = 0;
6237 	}
6238 
6239 	/* iDiag PCI BAR access */
6240 	snprintf(name, sizeof(name), "barAcc");
6241 	if (!phba->idiag_bar_acc) {
6242 		phba->idiag_bar_acc =
6243 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6244 				phba->idiag_root, phba, &lpfc_idiag_op_barAcc);
6245 		idiag.offset.last_rd = 0;
6246 	}
6247 
6248 	/* iDiag get PCI function queue information */
6249 	snprintf(name, sizeof(name), "queInfo");
6250 	if (!phba->idiag_que_info) {
6251 		phba->idiag_que_info =
6252 			debugfs_create_file(name, S_IFREG|S_IRUGO,
6253 			phba->idiag_root, phba, &lpfc_idiag_op_queInfo);
6254 	}
6255 
6256 	/* iDiag access PCI function queue */
6257 	snprintf(name, sizeof(name), "queAcc");
6258 	if (!phba->idiag_que_acc) {
6259 		phba->idiag_que_acc =
6260 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6261 				phba->idiag_root, phba, &lpfc_idiag_op_queAcc);
6262 	}
6263 
6264 	/* iDiag access PCI function doorbell registers */
6265 	snprintf(name, sizeof(name), "drbAcc");
6266 	if (!phba->idiag_drb_acc) {
6267 		phba->idiag_drb_acc =
6268 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6269 				phba->idiag_root, phba, &lpfc_idiag_op_drbAcc);
6270 	}
6271 
6272 	/* iDiag access PCI function control registers */
6273 	snprintf(name, sizeof(name), "ctlAcc");
6274 	if (!phba->idiag_ctl_acc) {
6275 		phba->idiag_ctl_acc =
6276 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6277 				phba->idiag_root, phba, &lpfc_idiag_op_ctlAcc);
6278 	}
6279 
6280 	/* iDiag access mbox commands */
6281 	snprintf(name, sizeof(name), "mbxAcc");
6282 	if (!phba->idiag_mbx_acc) {
6283 		phba->idiag_mbx_acc =
6284 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6285 				phba->idiag_root, phba, &lpfc_idiag_op_mbxAcc);
6286 	}
6287 
6288 	/* iDiag extents access commands */
6289 	if (phba->sli4_hba.extents_in_use) {
6290 		snprintf(name, sizeof(name), "extAcc");
6291 		if (!phba->idiag_ext_acc) {
6292 			phba->idiag_ext_acc =
6293 				debugfs_create_file(name,
6294 						    S_IFREG|S_IRUGO|S_IWUSR,
6295 						    phba->idiag_root, phba,
6296 						    &lpfc_idiag_op_extAcc);
6297 		}
6298 	}
6299 
6300 debug_failed:
6301 	return;
6302 #endif
6303 }
6304 
6305 /**
6306  * lpfc_debugfs_terminate -  Tear down debugfs infrastructure for this vport
6307  * @vport: The vport pointer to remove from debugfs.
6308  *
6309  * Description:
6310  * When Debugfs is configured this routine removes debugfs file system elements
6311  * that are specific to this vport. It also checks to see if there are any
6312  * users left for the debugfs directories associated with the HBA and driver. If
6313  * this is the last user of the HBA directory or driver directory then it will
6314  * remove those from the debugfs infrastructure as well.
6315  **/
6316 inline void
6317 lpfc_debugfs_terminate(struct lpfc_vport *vport)
6318 {
6319 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
6320 	struct lpfc_hba   *phba = vport->phba;
6321 
6322 	kfree(vport->disc_trc);
6323 	vport->disc_trc = NULL;
6324 
6325 	debugfs_remove(vport->debug_disc_trc); /* discovery_trace */
6326 	vport->debug_disc_trc = NULL;
6327 
6328 	debugfs_remove(vport->debug_nodelist); /* nodelist */
6329 	vport->debug_nodelist = NULL;
6330 
6331 	debugfs_remove(vport->debug_nvmestat); /* nvmestat */
6332 	vport->debug_nvmestat = NULL;
6333 
6334 	debugfs_remove(vport->debug_scsistat); /* scsistat */
6335 	vport->debug_scsistat = NULL;
6336 
6337 	debugfs_remove(vport->debug_ioktime); /* ioktime */
6338 	vport->debug_ioktime = NULL;
6339 
6340 	debugfs_remove(vport->debug_hdwqstat); /* hdwqstat */
6341 	vport->debug_hdwqstat = NULL;
6342 
6343 	if (vport->vport_debugfs_root) {
6344 		debugfs_remove(vport->vport_debugfs_root); /* vportX */
6345 		vport->vport_debugfs_root = NULL;
6346 		atomic_dec(&phba->debugfs_vport_count);
6347 	}
6348 
6349 	if (atomic_read(&phba->debugfs_vport_count) == 0) {
6350 
6351 		debugfs_remove(phba->debug_multixri_pools); /* multixripools*/
6352 		phba->debug_multixri_pools = NULL;
6353 
6354 		debugfs_remove(phba->debug_hbqinfo); /* hbqinfo */
6355 		phba->debug_hbqinfo = NULL;
6356 
6357 		debugfs_remove(phba->debug_ras_log);
6358 		phba->debug_ras_log = NULL;
6359 
6360 #ifdef LPFC_HDWQ_LOCK_STAT
6361 		debugfs_remove(phba->debug_lockstat); /* lockstat */
6362 		phba->debug_lockstat = NULL;
6363 #endif
6364 		debugfs_remove(phba->debug_dumpHBASlim); /* HBASlim */
6365 		phba->debug_dumpHBASlim = NULL;
6366 
6367 		debugfs_remove(phba->debug_dumpHostSlim); /* HostSlim */
6368 		phba->debug_dumpHostSlim = NULL;
6369 
6370 		debugfs_remove(phba->debug_InjErrLBA); /* InjErrLBA */
6371 		phba->debug_InjErrLBA = NULL;
6372 
6373 		debugfs_remove(phba->debug_InjErrNPortID);
6374 		phba->debug_InjErrNPortID = NULL;
6375 
6376 		debugfs_remove(phba->debug_InjErrWWPN); /* InjErrWWPN */
6377 		phba->debug_InjErrWWPN = NULL;
6378 
6379 		debugfs_remove(phba->debug_writeGuard); /* writeGuard */
6380 		phba->debug_writeGuard = NULL;
6381 
6382 		debugfs_remove(phba->debug_writeApp); /* writeApp */
6383 		phba->debug_writeApp = NULL;
6384 
6385 		debugfs_remove(phba->debug_writeRef); /* writeRef */
6386 		phba->debug_writeRef = NULL;
6387 
6388 		debugfs_remove(phba->debug_readGuard); /* readGuard */
6389 		phba->debug_readGuard = NULL;
6390 
6391 		debugfs_remove(phba->debug_readApp); /* readApp */
6392 		phba->debug_readApp = NULL;
6393 
6394 		debugfs_remove(phba->debug_readRef); /* readRef */
6395 		phba->debug_readRef = NULL;
6396 
6397 		kfree(phba->slow_ring_trc);
6398 		phba->slow_ring_trc = NULL;
6399 
6400 		/* slow_ring_trace */
6401 		debugfs_remove(phba->debug_slow_ring_trc);
6402 		phba->debug_slow_ring_trc = NULL;
6403 
6404 		debugfs_remove(phba->debug_nvmeio_trc);
6405 		phba->debug_nvmeio_trc = NULL;
6406 
6407 		kfree(phba->nvmeio_trc);
6408 		phba->nvmeio_trc = NULL;
6409 
6410 		/*
6411 		 * iDiag release
6412 		 */
6413 		if (phba->sli_rev == LPFC_SLI_REV4) {
6414 			/* iDiag extAcc */
6415 			debugfs_remove(phba->idiag_ext_acc);
6416 			phba->idiag_ext_acc = NULL;
6417 
6418 			/* iDiag mbxAcc */
6419 			debugfs_remove(phba->idiag_mbx_acc);
6420 			phba->idiag_mbx_acc = NULL;
6421 
6422 			/* iDiag ctlAcc */
6423 			debugfs_remove(phba->idiag_ctl_acc);
6424 			phba->idiag_ctl_acc = NULL;
6425 
6426 			/* iDiag drbAcc */
6427 			debugfs_remove(phba->idiag_drb_acc);
6428 			phba->idiag_drb_acc = NULL;
6429 
6430 			/* iDiag queAcc */
6431 			debugfs_remove(phba->idiag_que_acc);
6432 			phba->idiag_que_acc = NULL;
6433 
6434 			/* iDiag queInfo */
6435 			debugfs_remove(phba->idiag_que_info);
6436 			phba->idiag_que_info = NULL;
6437 
6438 			/* iDiag barAcc */
6439 			debugfs_remove(phba->idiag_bar_acc);
6440 			phba->idiag_bar_acc = NULL;
6441 
6442 			/* iDiag pciCfg */
6443 			debugfs_remove(phba->idiag_pci_cfg);
6444 			phba->idiag_pci_cfg = NULL;
6445 
6446 			/* Finally remove the iDiag debugfs root */
6447 			debugfs_remove(phba->idiag_root);
6448 			phba->idiag_root = NULL;
6449 		}
6450 
6451 		if (phba->hba_debugfs_root) {
6452 			debugfs_remove(phba->hba_debugfs_root); /* fnX */
6453 			phba->hba_debugfs_root = NULL;
6454 			atomic_dec(&lpfc_debugfs_hba_count);
6455 		}
6456 
6457 		if (atomic_read(&lpfc_debugfs_hba_count) == 0) {
6458 			debugfs_remove(lpfc_debugfs_root); /* lpfc */
6459 			lpfc_debugfs_root = NULL;
6460 		}
6461 	}
6462 #endif
6463 	return;
6464 }
6465 
6466 /*
6467  * Driver debug utility routines outside of debugfs. The debug utility
6468  * routines implemented here is intended to be used in the instrumented
6469  * debug driver for debugging host or port issues.
6470  */
6471 
6472 /**
6473  * lpfc_debug_dump_all_queues - dump all the queues with a hba
6474  * @phba: Pointer to HBA context object.
6475  *
6476  * This function dumps entries of all the queues asociated with the @phba.
6477  **/
6478 void
6479 lpfc_debug_dump_all_queues(struct lpfc_hba *phba)
6480 {
6481 	int idx;
6482 
6483 	/*
6484 	 * Dump Work Queues (WQs)
6485 	 */
6486 	lpfc_debug_dump_wq(phba, DUMP_MBX, 0);
6487 	lpfc_debug_dump_wq(phba, DUMP_ELS, 0);
6488 	lpfc_debug_dump_wq(phba, DUMP_NVMELS, 0);
6489 
6490 	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6491 		lpfc_debug_dump_wq(phba, DUMP_IO, idx);
6492 
6493 	lpfc_debug_dump_hdr_rq(phba);
6494 	lpfc_debug_dump_dat_rq(phba);
6495 	/*
6496 	 * Dump Complete Queues (CQs)
6497 	 */
6498 	lpfc_debug_dump_cq(phba, DUMP_MBX, 0);
6499 	lpfc_debug_dump_cq(phba, DUMP_ELS, 0);
6500 	lpfc_debug_dump_cq(phba, DUMP_NVMELS, 0);
6501 
6502 	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6503 		lpfc_debug_dump_cq(phba, DUMP_IO, idx);
6504 
6505 	/*
6506 	 * Dump Event Queues (EQs)
6507 	 */
6508 	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6509 		lpfc_debug_dump_hba_eq(phba, idx);
6510 }
6511