xref: /linux/drivers/scsi/lpfc/lpfc_debugfs.c (revision d9afbb3509900a953f5cf90bc57e793ee80c1108)
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 	memset(mybuf, 0, sizeof(mybuf));
2167 
2168 	if (copy_from_user(mybuf, buf, nbytes))
2169 		return -EFAULT;
2170 	pbuf = &mybuf[0];
2171 
2172 	if ((strncmp(pbuf, "reset", strlen("reset")) == 0) ||
2173 	    (strncmp(pbuf, "zero", strlen("zero")) == 0)) {
2174 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
2175 			qp = &phba->sli4_hba.hdwq[i];
2176 			qp->lock_conflict.alloc_xri_get = 0;
2177 			qp->lock_conflict.alloc_xri_put = 0;
2178 			qp->lock_conflict.free_xri = 0;
2179 			qp->lock_conflict.wq_access = 0;
2180 			qp->lock_conflict.alloc_pvt_pool = 0;
2181 			qp->lock_conflict.mv_from_pvt_pool = 0;
2182 			qp->lock_conflict.mv_to_pub_pool = 0;
2183 			qp->lock_conflict.mv_to_pvt_pool = 0;
2184 			qp->lock_conflict.free_pvt_pool = 0;
2185 			qp->lock_conflict.free_pub_pool = 0;
2186 			qp->lock_conflict.wq_access = 0;
2187 		}
2188 	}
2189 	return nbytes;
2190 }
2191 #endif
2192 
2193 static int lpfc_debugfs_ras_log_data(struct lpfc_hba *phba,
2194 				     char *buffer, int size)
2195 {
2196 	int copied = 0;
2197 	struct lpfc_dmabuf *dmabuf, *next;
2198 
2199 	memset(buffer, 0, size);
2200 
2201 	spin_lock_irq(&phba->hbalock);
2202 	if (phba->ras_fwlog.state != ACTIVE) {
2203 		spin_unlock_irq(&phba->hbalock);
2204 		return -EINVAL;
2205 	}
2206 	spin_unlock_irq(&phba->hbalock);
2207 
2208 	list_for_each_entry_safe(dmabuf, next,
2209 				 &phba->ras_fwlog.fwlog_buff_list, list) {
2210 		/* Check if copying will go over size and a '\0' char */
2211 		if ((copied + LPFC_RAS_MAX_ENTRY_SIZE) >= (size - 1)) {
2212 			memcpy(buffer + copied, dmabuf->virt,
2213 			       size - copied - 1);
2214 			copied += size - copied - 1;
2215 			break;
2216 		}
2217 		memcpy(buffer + copied, dmabuf->virt, LPFC_RAS_MAX_ENTRY_SIZE);
2218 		copied += LPFC_RAS_MAX_ENTRY_SIZE;
2219 	}
2220 	return copied;
2221 }
2222 
2223 static int
2224 lpfc_debugfs_ras_log_release(struct inode *inode, struct file *file)
2225 {
2226 	struct lpfc_debug *debug = file->private_data;
2227 
2228 	vfree(debug->buffer);
2229 	kfree(debug);
2230 
2231 	return 0;
2232 }
2233 
2234 /**
2235  * lpfc_debugfs_ras_log_open - Open the RAS log debugfs buffer
2236  * @inode: The inode pointer that contains a vport pointer.
2237  * @file: The file pointer to attach the log output.
2238  *
2239  * Description:
2240  * This routine is the entry point for the debugfs open file operation. It gets
2241  * the vport from the i_private field in @inode, allocates the necessary buffer
2242  * for the log, fills the buffer from the in-memory log for this vport, and then
2243  * returns a pointer to that log in the private_data field in @file.
2244  *
2245  * Returns:
2246  * This function returns zero if successful. On error it will return a negative
2247  * error value.
2248  **/
2249 static int
2250 lpfc_debugfs_ras_log_open(struct inode *inode, struct file *file)
2251 {
2252 	struct lpfc_hba *phba = inode->i_private;
2253 	struct lpfc_debug *debug;
2254 	int size;
2255 	int rc = -ENOMEM;
2256 
2257 	spin_lock_irq(&phba->hbalock);
2258 	if (phba->ras_fwlog.state != ACTIVE) {
2259 		spin_unlock_irq(&phba->hbalock);
2260 		rc = -EINVAL;
2261 		goto out;
2262 	}
2263 	spin_unlock_irq(&phba->hbalock);
2264 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2265 	if (!debug)
2266 		goto out;
2267 
2268 	size = LPFC_RAS_MIN_BUFF_POST_SIZE * phba->cfg_ras_fwlog_buffsize;
2269 	debug->buffer = vmalloc(size);
2270 	if (!debug->buffer)
2271 		goto free_debug;
2272 
2273 	debug->len = lpfc_debugfs_ras_log_data(phba, debug->buffer, size);
2274 	if (debug->len < 0) {
2275 		rc = -EINVAL;
2276 		goto free_buffer;
2277 	}
2278 	file->private_data = debug;
2279 
2280 	return 0;
2281 
2282 free_buffer:
2283 	vfree(debug->buffer);
2284 free_debug:
2285 	kfree(debug);
2286 out:
2287 	return rc;
2288 }
2289 
2290 /**
2291  * lpfc_debugfs_dumpHBASlim_open - Open the Dump HBA SLIM debugfs buffer
2292  * @inode: The inode pointer that contains a vport pointer.
2293  * @file: The file pointer to attach the log output.
2294  *
2295  * Description:
2296  * This routine is the entry point for the debugfs open file operation. It gets
2297  * the vport from the i_private field in @inode, allocates the necessary buffer
2298  * for the log, fills the buffer from the in-memory log for this vport, and then
2299  * returns a pointer to that log in the private_data field in @file.
2300  *
2301  * Returns:
2302  * This function returns zero if successful. On error it will return a negative
2303  * error value.
2304  **/
2305 static int
2306 lpfc_debugfs_dumpHBASlim_open(struct inode *inode, struct file *file)
2307 {
2308 	struct lpfc_hba *phba = inode->i_private;
2309 	struct lpfc_debug *debug;
2310 	int rc = -ENOMEM;
2311 
2312 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2313 	if (!debug)
2314 		goto out;
2315 
2316 	/* Round to page boundary */
2317 	debug->buffer = kmalloc(LPFC_DUMPHBASLIM_SIZE, GFP_KERNEL);
2318 	if (!debug->buffer) {
2319 		kfree(debug);
2320 		goto out;
2321 	}
2322 
2323 	debug->len = lpfc_debugfs_dumpHBASlim_data(phba, debug->buffer,
2324 		LPFC_DUMPHBASLIM_SIZE);
2325 	file->private_data = debug;
2326 
2327 	rc = 0;
2328 out:
2329 	return rc;
2330 }
2331 
2332 /**
2333  * lpfc_debugfs_dumpHostSlim_open - Open the Dump Host SLIM debugfs buffer
2334  * @inode: The inode pointer that contains a vport pointer.
2335  * @file: The file pointer to attach the log output.
2336  *
2337  * Description:
2338  * This routine is the entry point for the debugfs open file operation. It gets
2339  * the vport from the i_private field in @inode, allocates the necessary buffer
2340  * for the log, fills the buffer from the in-memory log for this vport, and then
2341  * returns a pointer to that log in the private_data field in @file.
2342  *
2343  * Returns:
2344  * This function returns zero if successful. On error it will return a negative
2345  * error value.
2346  **/
2347 static int
2348 lpfc_debugfs_dumpHostSlim_open(struct inode *inode, struct file *file)
2349 {
2350 	struct lpfc_hba *phba = inode->i_private;
2351 	struct lpfc_debug *debug;
2352 	int rc = -ENOMEM;
2353 
2354 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2355 	if (!debug)
2356 		goto out;
2357 
2358 	/* Round to page boundary */
2359 	debug->buffer = kmalloc(LPFC_DUMPHOSTSLIM_SIZE, GFP_KERNEL);
2360 	if (!debug->buffer) {
2361 		kfree(debug);
2362 		goto out;
2363 	}
2364 
2365 	debug->len = lpfc_debugfs_dumpHostSlim_data(phba, debug->buffer,
2366 		LPFC_DUMPHOSTSLIM_SIZE);
2367 	file->private_data = debug;
2368 
2369 	rc = 0;
2370 out:
2371 	return rc;
2372 }
2373 
2374 static ssize_t
2375 lpfc_debugfs_dif_err_read(struct file *file, char __user *buf,
2376 	size_t nbytes, loff_t *ppos)
2377 {
2378 	struct dentry *dent = file->f_path.dentry;
2379 	struct lpfc_hba *phba = file->private_data;
2380 	char cbuf[32];
2381 	uint64_t tmp = 0;
2382 	int cnt = 0;
2383 
2384 	if (dent == phba->debug_writeGuard)
2385 		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wgrd_cnt);
2386 	else if (dent == phba->debug_writeApp)
2387 		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wapp_cnt);
2388 	else if (dent == phba->debug_writeRef)
2389 		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wref_cnt);
2390 	else if (dent == phba->debug_readGuard)
2391 		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rgrd_cnt);
2392 	else if (dent == phba->debug_readApp)
2393 		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rapp_cnt);
2394 	else if (dent == phba->debug_readRef)
2395 		cnt = scnprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rref_cnt);
2396 	else if (dent == phba->debug_InjErrNPortID)
2397 		cnt = scnprintf(cbuf, 32, "0x%06x\n",
2398 				phba->lpfc_injerr_nportid);
2399 	else if (dent == phba->debug_InjErrWWPN) {
2400 		memcpy(&tmp, &phba->lpfc_injerr_wwpn, sizeof(struct lpfc_name));
2401 		tmp = cpu_to_be64(tmp);
2402 		cnt = scnprintf(cbuf, 32, "0x%016llx\n", tmp);
2403 	} else if (dent == phba->debug_InjErrLBA) {
2404 		if (phba->lpfc_injerr_lba == (sector_t)(-1))
2405 			cnt = scnprintf(cbuf, 32, "off\n");
2406 		else
2407 			cnt = scnprintf(cbuf, 32, "0x%llx\n",
2408 				 (uint64_t) phba->lpfc_injerr_lba);
2409 	} else
2410 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2411 			 "0547 Unknown debugfs error injection entry\n");
2412 
2413 	return simple_read_from_buffer(buf, nbytes, ppos, &cbuf, cnt);
2414 }
2415 
2416 static ssize_t
2417 lpfc_debugfs_dif_err_write(struct file *file, const char __user *buf,
2418 	size_t nbytes, loff_t *ppos)
2419 {
2420 	struct dentry *dent = file->f_path.dentry;
2421 	struct lpfc_hba *phba = file->private_data;
2422 	char dstbuf[33];
2423 	uint64_t tmp = 0;
2424 	int size;
2425 
2426 	memset(dstbuf, 0, 33);
2427 	size = (nbytes < 32) ? nbytes : 32;
2428 	if (copy_from_user(dstbuf, buf, size))
2429 		return 0;
2430 
2431 	if (dent == phba->debug_InjErrLBA) {
2432 		if ((buf[0] == 'o') && (buf[1] == 'f') && (buf[2] == 'f'))
2433 			tmp = (uint64_t)(-1);
2434 	}
2435 
2436 	if ((tmp == 0) && (kstrtoull(dstbuf, 0, &tmp)))
2437 		return 0;
2438 
2439 	if (dent == phba->debug_writeGuard)
2440 		phba->lpfc_injerr_wgrd_cnt = (uint32_t)tmp;
2441 	else if (dent == phba->debug_writeApp)
2442 		phba->lpfc_injerr_wapp_cnt = (uint32_t)tmp;
2443 	else if (dent == phba->debug_writeRef)
2444 		phba->lpfc_injerr_wref_cnt = (uint32_t)tmp;
2445 	else if (dent == phba->debug_readGuard)
2446 		phba->lpfc_injerr_rgrd_cnt = (uint32_t)tmp;
2447 	else if (dent == phba->debug_readApp)
2448 		phba->lpfc_injerr_rapp_cnt = (uint32_t)tmp;
2449 	else if (dent == phba->debug_readRef)
2450 		phba->lpfc_injerr_rref_cnt = (uint32_t)tmp;
2451 	else if (dent == phba->debug_InjErrLBA)
2452 		phba->lpfc_injerr_lba = (sector_t)tmp;
2453 	else if (dent == phba->debug_InjErrNPortID)
2454 		phba->lpfc_injerr_nportid = (uint32_t)(tmp & Mask_DID);
2455 	else if (dent == phba->debug_InjErrWWPN) {
2456 		tmp = cpu_to_be64(tmp);
2457 		memcpy(&phba->lpfc_injerr_wwpn, &tmp, sizeof(struct lpfc_name));
2458 	} else
2459 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2460 			 "0548 Unknown debugfs error injection entry\n");
2461 
2462 	return nbytes;
2463 }
2464 
2465 static int
2466 lpfc_debugfs_dif_err_release(struct inode *inode, struct file *file)
2467 {
2468 	return 0;
2469 }
2470 
2471 /**
2472  * lpfc_debugfs_nodelist_open - Open the nodelist debugfs file
2473  * @inode: The inode pointer that contains a vport pointer.
2474  * @file: The file pointer to attach the log output.
2475  *
2476  * Description:
2477  * This routine is the entry point for the debugfs open file operation. It gets
2478  * the vport from the i_private field in @inode, allocates the necessary buffer
2479  * for the log, fills the buffer from the in-memory log for this vport, and then
2480  * returns a pointer to that log in the private_data field in @file.
2481  *
2482  * Returns:
2483  * This function returns zero if successful. On error it will return a negative
2484  * error value.
2485  **/
2486 static int
2487 lpfc_debugfs_nodelist_open(struct inode *inode, struct file *file)
2488 {
2489 	struct lpfc_vport *vport = inode->i_private;
2490 	struct lpfc_debug *debug;
2491 	int rc = -ENOMEM;
2492 
2493 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2494 	if (!debug)
2495 		goto out;
2496 
2497 	/* Round to page boundary */
2498 	debug->buffer = kmalloc(LPFC_NODELIST_SIZE, GFP_KERNEL);
2499 	if (!debug->buffer) {
2500 		kfree(debug);
2501 		goto out;
2502 	}
2503 
2504 	debug->len = lpfc_debugfs_nodelist_data(vport, debug->buffer,
2505 		LPFC_NODELIST_SIZE);
2506 	file->private_data = debug;
2507 
2508 	rc = 0;
2509 out:
2510 	return rc;
2511 }
2512 
2513 /**
2514  * lpfc_debugfs_lseek - Seek through a debugfs file
2515  * @file: The file pointer to seek through.
2516  * @off: The offset to seek to or the amount to seek by.
2517  * @whence: Indicates how to seek.
2518  *
2519  * Description:
2520  * This routine is the entry point for the debugfs lseek file operation. The
2521  * @whence parameter indicates whether @off is the offset to directly seek to,
2522  * or if it is a value to seek forward or reverse by. This function figures out
2523  * what the new offset of the debugfs file will be and assigns that value to the
2524  * f_pos field of @file.
2525  *
2526  * Returns:
2527  * This function returns the new offset if successful and returns a negative
2528  * error if unable to process the seek.
2529  **/
2530 static loff_t
2531 lpfc_debugfs_lseek(struct file *file, loff_t off, int whence)
2532 {
2533 	struct lpfc_debug *debug = file->private_data;
2534 	return fixed_size_llseek(file, off, whence, debug->len);
2535 }
2536 
2537 /**
2538  * lpfc_debugfs_read - Read a debugfs file
2539  * @file: The file pointer to read from.
2540  * @buf: The buffer to copy the data to.
2541  * @nbytes: The number of bytes to read.
2542  * @ppos: The position in the file to start reading from.
2543  *
2544  * Description:
2545  * This routine reads data from from the buffer indicated in the private_data
2546  * field of @file. It will start reading at @ppos and copy up to @nbytes of
2547  * data to @buf.
2548  *
2549  * Returns:
2550  * This function returns the amount of data that was read (this could be less
2551  * than @nbytes if the end of the file was reached) or a negative error value.
2552  **/
2553 static ssize_t
2554 lpfc_debugfs_read(struct file *file, char __user *buf,
2555 		  size_t nbytes, loff_t *ppos)
2556 {
2557 	struct lpfc_debug *debug = file->private_data;
2558 
2559 	return simple_read_from_buffer(buf, nbytes, ppos, debug->buffer,
2560 				       debug->len);
2561 }
2562 
2563 /**
2564  * lpfc_debugfs_release - Release the buffer used to store debugfs file data
2565  * @inode: The inode pointer that contains a vport pointer. (unused)
2566  * @file: The file pointer that contains the buffer to release.
2567  *
2568  * Description:
2569  * This routine frees the buffer that was allocated when the debugfs file was
2570  * opened.
2571  *
2572  * Returns:
2573  * This function returns zero.
2574  **/
2575 static int
2576 lpfc_debugfs_release(struct inode *inode, struct file *file)
2577 {
2578 	struct lpfc_debug *debug = file->private_data;
2579 
2580 	kfree(debug->buffer);
2581 	kfree(debug);
2582 
2583 	return 0;
2584 }
2585 
2586 /**
2587  * lpfc_debugfs_multixripools_write - Clear multi-XRI pools statistics
2588  * @file: The file pointer to read from.
2589  * @buf: The buffer to copy the user data from.
2590  * @nbytes: The number of bytes to get.
2591  * @ppos: The position in the file to start reading from.
2592  *
2593  * Description:
2594  * This routine clears multi-XRI pools statistics when buf contains "clear".
2595  *
2596  * Return Value:
2597  * It returns the @nbytges passing in from debugfs user space when successful.
2598  * In case of error conditions, it returns proper error code back to the user
2599  * space.
2600  **/
2601 static ssize_t
2602 lpfc_debugfs_multixripools_write(struct file *file, const char __user *buf,
2603 				 size_t nbytes, loff_t *ppos)
2604 {
2605 	struct lpfc_debug *debug = file->private_data;
2606 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2607 	char mybuf[64];
2608 	char *pbuf;
2609 	u32 i;
2610 	u32 hwq_count;
2611 	struct lpfc_sli4_hdw_queue *qp;
2612 	struct lpfc_multixri_pool *multixri_pool;
2613 
2614 	if (nbytes > 64)
2615 		nbytes = 64;
2616 
2617 	memset(mybuf, 0, sizeof(mybuf));
2618 
2619 	if (copy_from_user(mybuf, buf, nbytes))
2620 		return -EFAULT;
2621 	pbuf = &mybuf[0];
2622 
2623 	if ((strncmp(pbuf, "clear", strlen("clear"))) == 0) {
2624 		hwq_count = phba->cfg_hdw_queue;
2625 		for (i = 0; i < hwq_count; i++) {
2626 			qp = &phba->sli4_hba.hdwq[i];
2627 			multixri_pool = qp->p_multixri_pool;
2628 			if (!multixri_pool)
2629 				continue;
2630 
2631 			qp->empty_io_bufs = 0;
2632 			multixri_pool->pbl_empty_count = 0;
2633 #ifdef LPFC_MXP_STAT
2634 			multixri_pool->above_limit_count = 0;
2635 			multixri_pool->below_limit_count = 0;
2636 			multixri_pool->stat_max_hwm = 0;
2637 			multixri_pool->local_pbl_hit_count = 0;
2638 			multixri_pool->other_pbl_hit_count = 0;
2639 
2640 			multixri_pool->stat_pbl_count = 0;
2641 			multixri_pool->stat_pvt_count = 0;
2642 			multixri_pool->stat_busy_count = 0;
2643 			multixri_pool->stat_snapshot_taken = 0;
2644 #endif
2645 		}
2646 		return strlen(pbuf);
2647 	}
2648 
2649 	return -EINVAL;
2650 }
2651 
2652 static int
2653 lpfc_debugfs_nvmestat_open(struct inode *inode, struct file *file)
2654 {
2655 	struct lpfc_vport *vport = inode->i_private;
2656 	struct lpfc_debug *debug;
2657 	int rc = -ENOMEM;
2658 
2659 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2660 	if (!debug)
2661 		goto out;
2662 
2663 	 /* Round to page boundary */
2664 	debug->buffer = kmalloc(LPFC_NVMESTAT_SIZE, GFP_KERNEL);
2665 	if (!debug->buffer) {
2666 		kfree(debug);
2667 		goto out;
2668 	}
2669 
2670 	debug->len = lpfc_debugfs_nvmestat_data(vport, debug->buffer,
2671 		LPFC_NVMESTAT_SIZE);
2672 
2673 	debug->i_private = inode->i_private;
2674 	file->private_data = debug;
2675 
2676 	rc = 0;
2677 out:
2678 	return rc;
2679 }
2680 
2681 static ssize_t
2682 lpfc_debugfs_nvmestat_write(struct file *file, const char __user *buf,
2683 			    size_t nbytes, loff_t *ppos)
2684 {
2685 	struct lpfc_debug *debug = file->private_data;
2686 	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2687 	struct lpfc_hba   *phba = vport->phba;
2688 	struct lpfc_nvmet_tgtport *tgtp;
2689 	char mybuf[64];
2690 	char *pbuf;
2691 
2692 	if (!phba->targetport)
2693 		return -ENXIO;
2694 
2695 	if (nbytes > 64)
2696 		nbytes = 64;
2697 
2698 	memset(mybuf, 0, sizeof(mybuf));
2699 
2700 	if (copy_from_user(mybuf, buf, nbytes))
2701 		return -EFAULT;
2702 	pbuf = &mybuf[0];
2703 
2704 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2705 	if ((strncmp(pbuf, "reset", strlen("reset")) == 0) ||
2706 	    (strncmp(pbuf, "zero", strlen("zero")) == 0)) {
2707 		atomic_set(&tgtp->rcv_ls_req_in, 0);
2708 		atomic_set(&tgtp->rcv_ls_req_out, 0);
2709 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
2710 		atomic_set(&tgtp->xmt_ls_abort, 0);
2711 		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
2712 		atomic_set(&tgtp->xmt_ls_rsp, 0);
2713 		atomic_set(&tgtp->xmt_ls_drop, 0);
2714 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
2715 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
2716 
2717 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
2718 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
2719 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
2720 		atomic_set(&tgtp->xmt_fcp_drop, 0);
2721 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
2722 		atomic_set(&tgtp->xmt_fcp_read, 0);
2723 		atomic_set(&tgtp->xmt_fcp_write, 0);
2724 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
2725 		atomic_set(&tgtp->xmt_fcp_release, 0);
2726 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
2727 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
2728 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
2729 
2730 		atomic_set(&tgtp->xmt_fcp_abort, 0);
2731 		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
2732 		atomic_set(&tgtp->xmt_abort_sol, 0);
2733 		atomic_set(&tgtp->xmt_abort_unsol, 0);
2734 		atomic_set(&tgtp->xmt_abort_rsp, 0);
2735 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
2736 	}
2737 	return nbytes;
2738 }
2739 
2740 static int
2741 lpfc_debugfs_scsistat_open(struct inode *inode, struct file *file)
2742 {
2743 	struct lpfc_vport *vport = inode->i_private;
2744 	struct lpfc_debug *debug;
2745 	int rc = -ENOMEM;
2746 
2747 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2748 	if (!debug)
2749 		goto out;
2750 
2751 	 /* Round to page boundary */
2752 	debug->buffer = kzalloc(LPFC_SCSISTAT_SIZE, GFP_KERNEL);
2753 	if (!debug->buffer) {
2754 		kfree(debug);
2755 		goto out;
2756 	}
2757 
2758 	debug->len = lpfc_debugfs_scsistat_data(vport, debug->buffer,
2759 		LPFC_SCSISTAT_SIZE);
2760 
2761 	debug->i_private = inode->i_private;
2762 	file->private_data = debug;
2763 
2764 	rc = 0;
2765 out:
2766 	return rc;
2767 }
2768 
2769 static ssize_t
2770 lpfc_debugfs_scsistat_write(struct file *file, const char __user *buf,
2771 			    size_t nbytes, loff_t *ppos)
2772 {
2773 	struct lpfc_debug *debug = file->private_data;
2774 	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2775 	struct lpfc_hba *phba = vport->phba;
2776 	char mybuf[6] = {0};
2777 	int i;
2778 
2779 	if (copy_from_user(mybuf, buf, (nbytes >= sizeof(mybuf)) ?
2780 				       (sizeof(mybuf) - 1) : nbytes))
2781 		return -EFAULT;
2782 
2783 	if ((strncmp(&mybuf[0], "reset", strlen("reset")) == 0) ||
2784 	    (strncmp(&mybuf[0], "zero", strlen("zero")) == 0)) {
2785 		for (i = 0; i < phba->cfg_hdw_queue; i++) {
2786 			memset(&phba->sli4_hba.hdwq[i].scsi_cstat, 0,
2787 			       sizeof(phba->sli4_hba.hdwq[i].scsi_cstat));
2788 		}
2789 	}
2790 
2791 	return nbytes;
2792 }
2793 
2794 static int
2795 lpfc_debugfs_ioktime_open(struct inode *inode, struct file *file)
2796 {
2797 	struct lpfc_vport *vport = inode->i_private;
2798 	struct lpfc_debug *debug;
2799 	int rc = -ENOMEM;
2800 
2801 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2802 	if (!debug)
2803 		goto out;
2804 
2805 	 /* Round to page boundary */
2806 	debug->buffer = kmalloc(LPFC_IOKTIME_SIZE, GFP_KERNEL);
2807 	if (!debug->buffer) {
2808 		kfree(debug);
2809 		goto out;
2810 	}
2811 
2812 	debug->len = lpfc_debugfs_ioktime_data(vport, debug->buffer,
2813 		LPFC_IOKTIME_SIZE);
2814 
2815 	debug->i_private = inode->i_private;
2816 	file->private_data = debug;
2817 
2818 	rc = 0;
2819 out:
2820 	return rc;
2821 }
2822 
2823 static ssize_t
2824 lpfc_debugfs_ioktime_write(struct file *file, const char __user *buf,
2825 			   size_t nbytes, loff_t *ppos)
2826 {
2827 	struct lpfc_debug *debug = file->private_data;
2828 	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2829 	struct lpfc_hba   *phba = vport->phba;
2830 	char mybuf[64];
2831 	char *pbuf;
2832 
2833 	if (nbytes > 64)
2834 		nbytes = 64;
2835 
2836 	memset(mybuf, 0, sizeof(mybuf));
2837 
2838 	if (copy_from_user(mybuf, buf, nbytes))
2839 		return -EFAULT;
2840 	pbuf = &mybuf[0];
2841 
2842 	if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2843 		phba->ktime_data_samples = 0;
2844 		phba->ktime_status_samples = 0;
2845 		phba->ktime_seg1_total = 0;
2846 		phba->ktime_seg1_max = 0;
2847 		phba->ktime_seg1_min = 0xffffffff;
2848 		phba->ktime_seg2_total = 0;
2849 		phba->ktime_seg2_max = 0;
2850 		phba->ktime_seg2_min = 0xffffffff;
2851 		phba->ktime_seg3_total = 0;
2852 		phba->ktime_seg3_max = 0;
2853 		phba->ktime_seg3_min = 0xffffffff;
2854 		phba->ktime_seg4_total = 0;
2855 		phba->ktime_seg4_max = 0;
2856 		phba->ktime_seg4_min = 0xffffffff;
2857 		phba->ktime_seg5_total = 0;
2858 		phba->ktime_seg5_max = 0;
2859 		phba->ktime_seg5_min = 0xffffffff;
2860 		phba->ktime_seg6_total = 0;
2861 		phba->ktime_seg6_max = 0;
2862 		phba->ktime_seg6_min = 0xffffffff;
2863 		phba->ktime_seg7_total = 0;
2864 		phba->ktime_seg7_max = 0;
2865 		phba->ktime_seg7_min = 0xffffffff;
2866 		phba->ktime_seg8_total = 0;
2867 		phba->ktime_seg8_max = 0;
2868 		phba->ktime_seg8_min = 0xffffffff;
2869 		phba->ktime_seg9_total = 0;
2870 		phba->ktime_seg9_max = 0;
2871 		phba->ktime_seg9_min = 0xffffffff;
2872 		phba->ktime_seg10_total = 0;
2873 		phba->ktime_seg10_max = 0;
2874 		phba->ktime_seg10_min = 0xffffffff;
2875 
2876 		phba->ktime_on = 1;
2877 		return strlen(pbuf);
2878 	} else if ((strncmp(pbuf, "off",
2879 		   sizeof("off") - 1) == 0)) {
2880 		phba->ktime_on = 0;
2881 		return strlen(pbuf);
2882 	} else if ((strncmp(pbuf, "zero",
2883 		   sizeof("zero") - 1) == 0)) {
2884 		phba->ktime_data_samples = 0;
2885 		phba->ktime_status_samples = 0;
2886 		phba->ktime_seg1_total = 0;
2887 		phba->ktime_seg1_max = 0;
2888 		phba->ktime_seg1_min = 0xffffffff;
2889 		phba->ktime_seg2_total = 0;
2890 		phba->ktime_seg2_max = 0;
2891 		phba->ktime_seg2_min = 0xffffffff;
2892 		phba->ktime_seg3_total = 0;
2893 		phba->ktime_seg3_max = 0;
2894 		phba->ktime_seg3_min = 0xffffffff;
2895 		phba->ktime_seg4_total = 0;
2896 		phba->ktime_seg4_max = 0;
2897 		phba->ktime_seg4_min = 0xffffffff;
2898 		phba->ktime_seg5_total = 0;
2899 		phba->ktime_seg5_max = 0;
2900 		phba->ktime_seg5_min = 0xffffffff;
2901 		phba->ktime_seg6_total = 0;
2902 		phba->ktime_seg6_max = 0;
2903 		phba->ktime_seg6_min = 0xffffffff;
2904 		phba->ktime_seg7_total = 0;
2905 		phba->ktime_seg7_max = 0;
2906 		phba->ktime_seg7_min = 0xffffffff;
2907 		phba->ktime_seg8_total = 0;
2908 		phba->ktime_seg8_max = 0;
2909 		phba->ktime_seg8_min = 0xffffffff;
2910 		phba->ktime_seg9_total = 0;
2911 		phba->ktime_seg9_max = 0;
2912 		phba->ktime_seg9_min = 0xffffffff;
2913 		phba->ktime_seg10_total = 0;
2914 		phba->ktime_seg10_max = 0;
2915 		phba->ktime_seg10_min = 0xffffffff;
2916 		return strlen(pbuf);
2917 	}
2918 	return -EINVAL;
2919 }
2920 
2921 static int
2922 lpfc_debugfs_nvmeio_trc_open(struct inode *inode, struct file *file)
2923 {
2924 	struct lpfc_hba *phba = inode->i_private;
2925 	struct lpfc_debug *debug;
2926 	int rc = -ENOMEM;
2927 
2928 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2929 	if (!debug)
2930 		goto out;
2931 
2932 	 /* Round to page boundary */
2933 	debug->buffer = kmalloc(LPFC_NVMEIO_TRC_SIZE, GFP_KERNEL);
2934 	if (!debug->buffer) {
2935 		kfree(debug);
2936 		goto out;
2937 	}
2938 
2939 	debug->len = lpfc_debugfs_nvmeio_trc_data(phba, debug->buffer,
2940 		LPFC_NVMEIO_TRC_SIZE);
2941 
2942 	debug->i_private = inode->i_private;
2943 	file->private_data = debug;
2944 
2945 	rc = 0;
2946 out:
2947 	return rc;
2948 }
2949 
2950 static ssize_t
2951 lpfc_debugfs_nvmeio_trc_write(struct file *file, const char __user *buf,
2952 			      size_t nbytes, loff_t *ppos)
2953 {
2954 	struct lpfc_debug *debug = file->private_data;
2955 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2956 	int i;
2957 	unsigned long sz;
2958 	char mybuf[64];
2959 	char *pbuf;
2960 
2961 	if (nbytes > 64)
2962 		nbytes = 64;
2963 
2964 	memset(mybuf, 0, sizeof(mybuf));
2965 
2966 	if (copy_from_user(mybuf, buf, nbytes))
2967 		return -EFAULT;
2968 	pbuf = &mybuf[0];
2969 
2970 	if ((strncmp(pbuf, "off", sizeof("off") - 1) == 0)) {
2971 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2972 				"0570 nvmeio_trc_off\n");
2973 		phba->nvmeio_trc_output_idx = 0;
2974 		phba->nvmeio_trc_on = 0;
2975 		return strlen(pbuf);
2976 	} else if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2977 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2978 				"0571 nvmeio_trc_on\n");
2979 		phba->nvmeio_trc_output_idx = 0;
2980 		phba->nvmeio_trc_on = 1;
2981 		return strlen(pbuf);
2982 	}
2983 
2984 	/* We must be off to allocate the trace buffer */
2985 	if (phba->nvmeio_trc_on != 0)
2986 		return -EINVAL;
2987 
2988 	/* If not on or off, the parameter is the trace buffer size */
2989 	i = kstrtoul(pbuf, 0, &sz);
2990 	if (i)
2991 		return -EINVAL;
2992 	phba->nvmeio_trc_size = (uint32_t)sz;
2993 
2994 	/* It must be a power of 2 - round down */
2995 	i = 0;
2996 	while (sz > 1) {
2997 		sz = sz >> 1;
2998 		i++;
2999 	}
3000 	sz = (1 << i);
3001 	if (phba->nvmeio_trc_size != sz)
3002 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3003 				"0572 nvmeio_trc_size changed to %ld\n",
3004 				sz);
3005 	phba->nvmeio_trc_size = (uint32_t)sz;
3006 
3007 	/* If one previously exists, free it */
3008 	kfree(phba->nvmeio_trc);
3009 
3010 	/* Allocate new trace buffer and initialize */
3011 	phba->nvmeio_trc = kzalloc((sizeof(struct lpfc_debugfs_nvmeio_trc) *
3012 				    sz), GFP_KERNEL);
3013 	if (!phba->nvmeio_trc) {
3014 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3015 				"0573 Cannot create debugfs "
3016 				"nvmeio_trc buffer\n");
3017 		return -ENOMEM;
3018 	}
3019 	atomic_set(&phba->nvmeio_trc_cnt, 0);
3020 	phba->nvmeio_trc_on = 0;
3021 	phba->nvmeio_trc_output_idx = 0;
3022 
3023 	return strlen(pbuf);
3024 }
3025 
3026 static int
3027 lpfc_debugfs_hdwqstat_open(struct inode *inode, struct file *file)
3028 {
3029 	struct lpfc_vport *vport = inode->i_private;
3030 	struct lpfc_debug *debug;
3031 	int rc = -ENOMEM;
3032 
3033 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
3034 	if (!debug)
3035 		goto out;
3036 
3037 	 /* Round to page boundary */
3038 	debug->buffer = kcalloc(1, LPFC_SCSISTAT_SIZE, GFP_KERNEL);
3039 	if (!debug->buffer) {
3040 		kfree(debug);
3041 		goto out;
3042 	}
3043 
3044 	debug->len = lpfc_debugfs_hdwqstat_data(vport, debug->buffer,
3045 						LPFC_SCSISTAT_SIZE);
3046 
3047 	debug->i_private = inode->i_private;
3048 	file->private_data = debug;
3049 
3050 	rc = 0;
3051 out:
3052 	return rc;
3053 }
3054 
3055 static ssize_t
3056 lpfc_debugfs_hdwqstat_write(struct file *file, const char __user *buf,
3057 			    size_t nbytes, loff_t *ppos)
3058 {
3059 	struct lpfc_debug *debug = file->private_data;
3060 	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
3061 	struct lpfc_hba   *phba = vport->phba;
3062 	struct lpfc_hdwq_stat *c_stat;
3063 	char mybuf[64];
3064 	char *pbuf;
3065 	int i;
3066 
3067 	if (nbytes > 64)
3068 		nbytes = 64;
3069 
3070 	memset(mybuf, 0, sizeof(mybuf));
3071 
3072 	if (copy_from_user(mybuf, buf, nbytes))
3073 		return -EFAULT;
3074 	pbuf = &mybuf[0];
3075 
3076 	if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
3077 		if (phba->nvmet_support)
3078 			phba->hdwqstat_on |= LPFC_CHECK_NVMET_IO;
3079 		else
3080 			phba->hdwqstat_on |= (LPFC_CHECK_NVME_IO |
3081 				LPFC_CHECK_SCSI_IO);
3082 		return strlen(pbuf);
3083 	} else if ((strncmp(pbuf, "nvme_on", sizeof("nvme_on") - 1) == 0)) {
3084 		if (phba->nvmet_support)
3085 			phba->hdwqstat_on |= LPFC_CHECK_NVMET_IO;
3086 		else
3087 			phba->hdwqstat_on |= LPFC_CHECK_NVME_IO;
3088 		return strlen(pbuf);
3089 	} else if ((strncmp(pbuf, "scsi_on", sizeof("scsi_on") - 1) == 0)) {
3090 		if (!phba->nvmet_support)
3091 			phba->hdwqstat_on |= LPFC_CHECK_SCSI_IO;
3092 		return strlen(pbuf);
3093 	} else if ((strncmp(pbuf, "nvme_off", sizeof("nvme_off") - 1) == 0)) {
3094 		phba->hdwqstat_on &= ~(LPFC_CHECK_NVME_IO |
3095 				       LPFC_CHECK_NVMET_IO);
3096 		return strlen(pbuf);
3097 	} else if ((strncmp(pbuf, "scsi_off", sizeof("scsi_off") - 1) == 0)) {
3098 		phba->hdwqstat_on &= ~LPFC_CHECK_SCSI_IO;
3099 		return strlen(pbuf);
3100 	} else if ((strncmp(pbuf, "off",
3101 		   sizeof("off") - 1) == 0)) {
3102 		phba->hdwqstat_on = LPFC_CHECK_OFF;
3103 		return strlen(pbuf);
3104 	} else if ((strncmp(pbuf, "zero",
3105 		   sizeof("zero") - 1) == 0)) {
3106 		for_each_present_cpu(i) {
3107 			c_stat = per_cpu_ptr(phba->sli4_hba.c_stat, i);
3108 			c_stat->xmt_io = 0;
3109 			c_stat->cmpl_io = 0;
3110 			c_stat->rcv_io = 0;
3111 		}
3112 		return strlen(pbuf);
3113 	}
3114 	return -EINVAL;
3115 }
3116 
3117 /*
3118  * ---------------------------------
3119  * iDiag debugfs file access methods
3120  * ---------------------------------
3121  *
3122  * All access methods are through the proper SLI4 PCI function's debugfs
3123  * iDiag directory:
3124  *
3125  *     /sys/kernel/debug/lpfc/fn<#>/iDiag
3126  */
3127 
3128 /**
3129  * lpfc_idiag_cmd_get - Get and parse idiag debugfs comands from user space
3130  * @buf: The pointer to the user space buffer.
3131  * @nbytes: The number of bytes in the user space buffer.
3132  * @idiag_cmd: pointer to the idiag command struct.
3133  *
3134  * This routine reads data from debugfs user space buffer and parses the
3135  * buffer for getting the idiag command and arguments. The while space in
3136  * between the set of data is used as the parsing separator.
3137  *
3138  * This routine returns 0 when successful, it returns proper error code
3139  * back to the user space in error conditions.
3140  */
3141 static int lpfc_idiag_cmd_get(const char __user *buf, size_t nbytes,
3142 			      struct lpfc_idiag_cmd *idiag_cmd)
3143 {
3144 	char mybuf[64];
3145 	char *pbuf, *step_str;
3146 	int i;
3147 	size_t bsize;
3148 
3149 	memset(mybuf, 0, sizeof(mybuf));
3150 	memset(idiag_cmd, 0, sizeof(*idiag_cmd));
3151 	bsize = min(nbytes, (sizeof(mybuf)-1));
3152 
3153 	if (copy_from_user(mybuf, buf, bsize))
3154 		return -EFAULT;
3155 	pbuf = &mybuf[0];
3156 	step_str = strsep(&pbuf, "\t ");
3157 
3158 	/* The opcode must present */
3159 	if (!step_str)
3160 		return -EINVAL;
3161 
3162 	idiag_cmd->opcode = simple_strtol(step_str, NULL, 0);
3163 	if (idiag_cmd->opcode == 0)
3164 		return -EINVAL;
3165 
3166 	for (i = 0; i < LPFC_IDIAG_CMD_DATA_SIZE; i++) {
3167 		step_str = strsep(&pbuf, "\t ");
3168 		if (!step_str)
3169 			return i;
3170 		idiag_cmd->data[i] = simple_strtol(step_str, NULL, 0);
3171 	}
3172 	return i;
3173 }
3174 
3175 /**
3176  * lpfc_idiag_open - idiag open debugfs
3177  * @inode: The inode pointer that contains a pointer to phba.
3178  * @file: The file pointer to attach the file operation.
3179  *
3180  * Description:
3181  * This routine is the entry point for the debugfs open file operation. It
3182  * gets the reference to phba from the i_private field in @inode, it then
3183  * allocates buffer for the file operation, performs the necessary PCI config
3184  * space read into the allocated buffer according to the idiag user command
3185  * setup, and then returns a pointer to buffer in the private_data field in
3186  * @file.
3187  *
3188  * Returns:
3189  * This function returns zero if successful. On error it will return an
3190  * negative error value.
3191  **/
3192 static int
3193 lpfc_idiag_open(struct inode *inode, struct file *file)
3194 {
3195 	struct lpfc_debug *debug;
3196 
3197 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
3198 	if (!debug)
3199 		return -ENOMEM;
3200 
3201 	debug->i_private = inode->i_private;
3202 	debug->buffer = NULL;
3203 	file->private_data = debug;
3204 
3205 	return 0;
3206 }
3207 
3208 /**
3209  * lpfc_idiag_release - Release idiag access file operation
3210  * @inode: The inode pointer that contains a vport pointer. (unused)
3211  * @file: The file pointer that contains the buffer to release.
3212  *
3213  * Description:
3214  * This routine is the generic release routine for the idiag access file
3215  * operation, it frees the buffer that was allocated when the debugfs file
3216  * was opened.
3217  *
3218  * Returns:
3219  * This function returns zero.
3220  **/
3221 static int
3222 lpfc_idiag_release(struct inode *inode, struct file *file)
3223 {
3224 	struct lpfc_debug *debug = file->private_data;
3225 
3226 	/* Free the buffers to the file operation */
3227 	kfree(debug->buffer);
3228 	kfree(debug);
3229 
3230 	return 0;
3231 }
3232 
3233 /**
3234  * lpfc_idiag_cmd_release - Release idiag cmd access file operation
3235  * @inode: The inode pointer that contains a vport pointer. (unused)
3236  * @file: The file pointer that contains the buffer to release.
3237  *
3238  * Description:
3239  * This routine frees the buffer that was allocated when the debugfs file
3240  * was opened. It also reset the fields in the idiag command struct in the
3241  * case of command for write operation.
3242  *
3243  * Returns:
3244  * This function returns zero.
3245  **/
3246 static int
3247 lpfc_idiag_cmd_release(struct inode *inode, struct file *file)
3248 {
3249 	struct lpfc_debug *debug = file->private_data;
3250 
3251 	if (debug->op == LPFC_IDIAG_OP_WR) {
3252 		switch (idiag.cmd.opcode) {
3253 		case LPFC_IDIAG_CMD_PCICFG_WR:
3254 		case LPFC_IDIAG_CMD_PCICFG_ST:
3255 		case LPFC_IDIAG_CMD_PCICFG_CL:
3256 		case LPFC_IDIAG_CMD_QUEACC_WR:
3257 		case LPFC_IDIAG_CMD_QUEACC_ST:
3258 		case LPFC_IDIAG_CMD_QUEACC_CL:
3259 			memset(&idiag, 0, sizeof(idiag));
3260 			break;
3261 		default:
3262 			break;
3263 		}
3264 	}
3265 
3266 	/* Free the buffers to the file operation */
3267 	kfree(debug->buffer);
3268 	kfree(debug);
3269 
3270 	return 0;
3271 }
3272 
3273 /**
3274  * lpfc_idiag_pcicfg_read - idiag debugfs read pcicfg
3275  * @file: The file pointer to read from.
3276  * @buf: The buffer to copy the data to.
3277  * @nbytes: The number of bytes to read.
3278  * @ppos: The position in the file to start reading from.
3279  *
3280  * Description:
3281  * This routine reads data from the @phba pci config space according to the
3282  * idiag command, and copies to user @buf. Depending on the PCI config space
3283  * read command setup, it does either a single register read of a byte
3284  * (8 bits), a word (16 bits), or a dword (32 bits) or browsing through all
3285  * registers from the 4K extended PCI config space.
3286  *
3287  * Returns:
3288  * This function returns the amount of data that was read (this could be less
3289  * than @nbytes if the end of the file was reached) or a negative error value.
3290  **/
3291 static ssize_t
3292 lpfc_idiag_pcicfg_read(struct file *file, char __user *buf, size_t nbytes,
3293 		       loff_t *ppos)
3294 {
3295 	struct lpfc_debug *debug = file->private_data;
3296 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3297 	int offset_label, offset, len = 0, index = LPFC_PCI_CFG_RD_SIZE;
3298 	int where, count;
3299 	char *pbuffer;
3300 	struct pci_dev *pdev;
3301 	uint32_t u32val;
3302 	uint16_t u16val;
3303 	uint8_t u8val;
3304 
3305 	pdev = phba->pcidev;
3306 	if (!pdev)
3307 		return 0;
3308 
3309 	/* This is a user read operation */
3310 	debug->op = LPFC_IDIAG_OP_RD;
3311 
3312 	if (!debug->buffer)
3313 		debug->buffer = kmalloc(LPFC_PCI_CFG_SIZE, GFP_KERNEL);
3314 	if (!debug->buffer)
3315 		return 0;
3316 	pbuffer = debug->buffer;
3317 
3318 	if (*ppos)
3319 		return 0;
3320 
3321 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
3322 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
3323 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
3324 	} else
3325 		return 0;
3326 
3327 	/* Read single PCI config space register */
3328 	switch (count) {
3329 	case SIZE_U8: /* byte (8 bits) */
3330 		pci_read_config_byte(pdev, where, &u8val);
3331 		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3332 				"%03x: %02x\n", where, u8val);
3333 		break;
3334 	case SIZE_U16: /* word (16 bits) */
3335 		pci_read_config_word(pdev, where, &u16val);
3336 		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3337 				"%03x: %04x\n", where, u16val);
3338 		break;
3339 	case SIZE_U32: /* double word (32 bits) */
3340 		pci_read_config_dword(pdev, where, &u32val);
3341 		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3342 				"%03x: %08x\n", where, u32val);
3343 		break;
3344 	case LPFC_PCI_CFG_BROWSE: /* browse all */
3345 		goto pcicfg_browse;
3346 		break;
3347 	default:
3348 		/* illegal count */
3349 		len = 0;
3350 		break;
3351 	}
3352 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3353 
3354 pcicfg_browse:
3355 
3356 	/* Browse all PCI config space registers */
3357 	offset_label = idiag.offset.last_rd;
3358 	offset = offset_label;
3359 
3360 	/* Read PCI config space */
3361 	len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3362 			"%03x: ", offset_label);
3363 	while (index > 0) {
3364 		pci_read_config_dword(pdev, offset, &u32val);
3365 		len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3366 				"%08x ", u32val);
3367 		offset += sizeof(uint32_t);
3368 		if (offset >= LPFC_PCI_CFG_SIZE) {
3369 			len += scnprintf(pbuffer+len,
3370 					LPFC_PCI_CFG_SIZE-len, "\n");
3371 			break;
3372 		}
3373 		index -= sizeof(uint32_t);
3374 		if (!index)
3375 			len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3376 					"\n");
3377 		else if (!(index % (8 * sizeof(uint32_t)))) {
3378 			offset_label += (8 * sizeof(uint32_t));
3379 			len += scnprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
3380 					"\n%03x: ", offset_label);
3381 		}
3382 	}
3383 
3384 	/* Set up the offset for next portion of pci cfg read */
3385 	if (index == 0) {
3386 		idiag.offset.last_rd += LPFC_PCI_CFG_RD_SIZE;
3387 		if (idiag.offset.last_rd >= LPFC_PCI_CFG_SIZE)
3388 			idiag.offset.last_rd = 0;
3389 	} else
3390 		idiag.offset.last_rd = 0;
3391 
3392 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3393 }
3394 
3395 /**
3396  * lpfc_idiag_pcicfg_write - Syntax check and set up idiag pcicfg commands
3397  * @file: The file pointer to read from.
3398  * @buf: The buffer to copy the user data from.
3399  * @nbytes: The number of bytes to get.
3400  * @ppos: The position in the file to start reading from.
3401  *
3402  * This routine get the debugfs idiag command struct from user space and
3403  * then perform the syntax check for PCI config space read or write command
3404  * accordingly. In the case of PCI config space read command, it sets up
3405  * the command in the idiag command struct for the debugfs read operation.
3406  * In the case of PCI config space write operation, it executes the write
3407  * operation into the PCI config space accordingly.
3408  *
3409  * It returns the @nbytges passing in from debugfs user space when successful.
3410  * In case of error conditions, it returns proper error code back to the user
3411  * space.
3412  */
3413 static ssize_t
3414 lpfc_idiag_pcicfg_write(struct file *file, const char __user *buf,
3415 			size_t nbytes, loff_t *ppos)
3416 {
3417 	struct lpfc_debug *debug = file->private_data;
3418 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3419 	uint32_t where, value, count;
3420 	uint32_t u32val;
3421 	uint16_t u16val;
3422 	uint8_t u8val;
3423 	struct pci_dev *pdev;
3424 	int rc;
3425 
3426 	pdev = phba->pcidev;
3427 	if (!pdev)
3428 		return -EFAULT;
3429 
3430 	/* This is a user write operation */
3431 	debug->op = LPFC_IDIAG_OP_WR;
3432 
3433 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3434 	if (rc < 0)
3435 		return rc;
3436 
3437 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
3438 		/* Sanity check on PCI config read command line arguments */
3439 		if (rc != LPFC_PCI_CFG_RD_CMD_ARG)
3440 			goto error_out;
3441 		/* Read command from PCI config space, set up command fields */
3442 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
3443 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
3444 		if (count == LPFC_PCI_CFG_BROWSE) {
3445 			if (where % sizeof(uint32_t))
3446 				goto error_out;
3447 			/* Starting offset to browse */
3448 			idiag.offset.last_rd = where;
3449 		} else if ((count != sizeof(uint8_t)) &&
3450 			   (count != sizeof(uint16_t)) &&
3451 			   (count != sizeof(uint32_t)))
3452 			goto error_out;
3453 		if (count == sizeof(uint8_t)) {
3454 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
3455 				goto error_out;
3456 			if (where % sizeof(uint8_t))
3457 				goto error_out;
3458 		}
3459 		if (count == sizeof(uint16_t)) {
3460 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
3461 				goto error_out;
3462 			if (where % sizeof(uint16_t))
3463 				goto error_out;
3464 		}
3465 		if (count == sizeof(uint32_t)) {
3466 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
3467 				goto error_out;
3468 			if (where % sizeof(uint32_t))
3469 				goto error_out;
3470 		}
3471 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR ||
3472 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST ||
3473 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3474 		/* Sanity check on PCI config write command line arguments */
3475 		if (rc != LPFC_PCI_CFG_WR_CMD_ARG)
3476 			goto error_out;
3477 		/* Write command to PCI config space, read-modify-write */
3478 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
3479 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
3480 		value = idiag.cmd.data[IDIAG_PCICFG_VALUE_INDX];
3481 		/* Sanity checks */
3482 		if ((count != sizeof(uint8_t)) &&
3483 		    (count != sizeof(uint16_t)) &&
3484 		    (count != sizeof(uint32_t)))
3485 			goto error_out;
3486 		if (count == sizeof(uint8_t)) {
3487 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
3488 				goto error_out;
3489 			if (where % sizeof(uint8_t))
3490 				goto error_out;
3491 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
3492 				pci_write_config_byte(pdev, where,
3493 						      (uint8_t)value);
3494 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
3495 				rc = pci_read_config_byte(pdev, where, &u8val);
3496 				if (!rc) {
3497 					u8val |= (uint8_t)value;
3498 					pci_write_config_byte(pdev, where,
3499 							      u8val);
3500 				}
3501 			}
3502 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3503 				rc = pci_read_config_byte(pdev, where, &u8val);
3504 				if (!rc) {
3505 					u8val &= (uint8_t)(~value);
3506 					pci_write_config_byte(pdev, where,
3507 							      u8val);
3508 				}
3509 			}
3510 		}
3511 		if (count == sizeof(uint16_t)) {
3512 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
3513 				goto error_out;
3514 			if (where % sizeof(uint16_t))
3515 				goto error_out;
3516 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
3517 				pci_write_config_word(pdev, where,
3518 						      (uint16_t)value);
3519 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
3520 				rc = pci_read_config_word(pdev, where, &u16val);
3521 				if (!rc) {
3522 					u16val |= (uint16_t)value;
3523 					pci_write_config_word(pdev, where,
3524 							      u16val);
3525 				}
3526 			}
3527 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3528 				rc = pci_read_config_word(pdev, where, &u16val);
3529 				if (!rc) {
3530 					u16val &= (uint16_t)(~value);
3531 					pci_write_config_word(pdev, where,
3532 							      u16val);
3533 				}
3534 			}
3535 		}
3536 		if (count == sizeof(uint32_t)) {
3537 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
3538 				goto error_out;
3539 			if (where % sizeof(uint32_t))
3540 				goto error_out;
3541 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
3542 				pci_write_config_dword(pdev, where, value);
3543 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
3544 				rc = pci_read_config_dword(pdev, where,
3545 							   &u32val);
3546 				if (!rc) {
3547 					u32val |= value;
3548 					pci_write_config_dword(pdev, where,
3549 							       u32val);
3550 				}
3551 			}
3552 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
3553 				rc = pci_read_config_dword(pdev, where,
3554 							   &u32val);
3555 				if (!rc) {
3556 					u32val &= ~value;
3557 					pci_write_config_dword(pdev, where,
3558 							       u32val);
3559 				}
3560 			}
3561 		}
3562 	} else
3563 		/* All other opecodes are illegal for now */
3564 		goto error_out;
3565 
3566 	return nbytes;
3567 error_out:
3568 	memset(&idiag, 0, sizeof(idiag));
3569 	return -EINVAL;
3570 }
3571 
3572 /**
3573  * lpfc_idiag_baracc_read - idiag debugfs pci bar access read
3574  * @file: The file pointer to read from.
3575  * @buf: The buffer to copy the data to.
3576  * @nbytes: The number of bytes to read.
3577  * @ppos: The position in the file to start reading from.
3578  *
3579  * Description:
3580  * This routine reads data from the @phba pci bar memory mapped space
3581  * according to the idiag command, and copies to user @buf.
3582  *
3583  * Returns:
3584  * This function returns the amount of data that was read (this could be less
3585  * than @nbytes if the end of the file was reached) or a negative error value.
3586  **/
3587 static ssize_t
3588 lpfc_idiag_baracc_read(struct file *file, char __user *buf, size_t nbytes,
3589 		       loff_t *ppos)
3590 {
3591 	struct lpfc_debug *debug = file->private_data;
3592 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3593 	int offset_label, offset, offset_run, len = 0, index;
3594 	int bar_num, acc_range, bar_size;
3595 	char *pbuffer;
3596 	void __iomem *mem_mapped_bar;
3597 	uint32_t if_type;
3598 	struct pci_dev *pdev;
3599 	uint32_t u32val;
3600 
3601 	pdev = phba->pcidev;
3602 	if (!pdev)
3603 		return 0;
3604 
3605 	/* This is a user read operation */
3606 	debug->op = LPFC_IDIAG_OP_RD;
3607 
3608 	if (!debug->buffer)
3609 		debug->buffer = kmalloc(LPFC_PCI_BAR_RD_BUF_SIZE, GFP_KERNEL);
3610 	if (!debug->buffer)
3611 		return 0;
3612 	pbuffer = debug->buffer;
3613 
3614 	if (*ppos)
3615 		return 0;
3616 
3617 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
3618 		bar_num   = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
3619 		offset    = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
3620 		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
3621 		bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
3622 	} else
3623 		return 0;
3624 
3625 	if (acc_range == 0)
3626 		return 0;
3627 
3628 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
3629 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
3630 		if (bar_num == IDIAG_BARACC_BAR_0)
3631 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3632 		else if (bar_num == IDIAG_BARACC_BAR_1)
3633 			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
3634 		else if (bar_num == IDIAG_BARACC_BAR_2)
3635 			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
3636 		else
3637 			return 0;
3638 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3639 		if (bar_num == IDIAG_BARACC_BAR_0)
3640 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3641 		else
3642 			return 0;
3643 	} else
3644 		return 0;
3645 
3646 	/* Read single PCI bar space register */
3647 	if (acc_range == SINGLE_WORD) {
3648 		offset_run = offset;
3649 		u32val = readl(mem_mapped_bar + offset_run);
3650 		len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3651 				"%05x: %08x\n", offset_run, u32val);
3652 	} else
3653 		goto baracc_browse;
3654 
3655 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3656 
3657 baracc_browse:
3658 
3659 	/* Browse all PCI bar space registers */
3660 	offset_label = idiag.offset.last_rd;
3661 	offset_run = offset_label;
3662 
3663 	/* Read PCI bar memory mapped space */
3664 	len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3665 			"%05x: ", offset_label);
3666 	index = LPFC_PCI_BAR_RD_SIZE;
3667 	while (index > 0) {
3668 		u32val = readl(mem_mapped_bar + offset_run);
3669 		len += scnprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
3670 				"%08x ", u32val);
3671 		offset_run += sizeof(uint32_t);
3672 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
3673 			if (offset_run >= bar_size) {
3674 				len += scnprintf(pbuffer+len,
3675 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
3676 				break;
3677 			}
3678 		} else {
3679 			if (offset_run >= offset +
3680 			    (acc_range * sizeof(uint32_t))) {
3681 				len += scnprintf(pbuffer+len,
3682 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
3683 				break;
3684 			}
3685 		}
3686 		index -= sizeof(uint32_t);
3687 		if (!index)
3688 			len += scnprintf(pbuffer+len,
3689 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
3690 		else if (!(index % (8 * sizeof(uint32_t)))) {
3691 			offset_label += (8 * sizeof(uint32_t));
3692 			len += scnprintf(pbuffer+len,
3693 					LPFC_PCI_BAR_RD_BUF_SIZE-len,
3694 					"\n%05x: ", offset_label);
3695 		}
3696 	}
3697 
3698 	/* Set up the offset for next portion of pci bar read */
3699 	if (index == 0) {
3700 		idiag.offset.last_rd += LPFC_PCI_BAR_RD_SIZE;
3701 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
3702 			if (idiag.offset.last_rd >= bar_size)
3703 				idiag.offset.last_rd = 0;
3704 		} else {
3705 			if (offset_run >= offset +
3706 			    (acc_range * sizeof(uint32_t)))
3707 				idiag.offset.last_rd = offset;
3708 		}
3709 	} else {
3710 		if (acc_range == LPFC_PCI_BAR_BROWSE)
3711 			idiag.offset.last_rd = 0;
3712 		else
3713 			idiag.offset.last_rd = offset;
3714 	}
3715 
3716 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3717 }
3718 
3719 /**
3720  * lpfc_idiag_baracc_write - Syntax check and set up idiag bar access commands
3721  * @file: The file pointer to read from.
3722  * @buf: The buffer to copy the user data from.
3723  * @nbytes: The number of bytes to get.
3724  * @ppos: The position in the file to start reading from.
3725  *
3726  * This routine get the debugfs idiag command struct from user space and
3727  * then perform the syntax check for PCI bar memory mapped space read or
3728  * write command accordingly. In the case of PCI bar memory mapped space
3729  * read command, it sets up the command in the idiag command struct for
3730  * the debugfs read operation. In the case of PCI bar memorpy mapped space
3731  * write operation, it executes the write operation into the PCI bar memory
3732  * mapped space accordingly.
3733  *
3734  * It returns the @nbytges passing in from debugfs user space when successful.
3735  * In case of error conditions, it returns proper error code back to the user
3736  * space.
3737  */
3738 static ssize_t
3739 lpfc_idiag_baracc_write(struct file *file, const char __user *buf,
3740 			size_t nbytes, loff_t *ppos)
3741 {
3742 	struct lpfc_debug *debug = file->private_data;
3743 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3744 	uint32_t bar_num, bar_size, offset, value, acc_range;
3745 	struct pci_dev *pdev;
3746 	void __iomem *mem_mapped_bar;
3747 	uint32_t if_type;
3748 	uint32_t u32val;
3749 	int rc;
3750 
3751 	pdev = phba->pcidev;
3752 	if (!pdev)
3753 		return -EFAULT;
3754 
3755 	/* This is a user write operation */
3756 	debug->op = LPFC_IDIAG_OP_WR;
3757 
3758 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3759 	if (rc < 0)
3760 		return rc;
3761 
3762 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
3763 	bar_num = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
3764 
3765 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
3766 		if ((bar_num != IDIAG_BARACC_BAR_0) &&
3767 		    (bar_num != IDIAG_BARACC_BAR_1) &&
3768 		    (bar_num != IDIAG_BARACC_BAR_2))
3769 			goto error_out;
3770 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3771 		if (bar_num != IDIAG_BARACC_BAR_0)
3772 			goto error_out;
3773 	} else
3774 		goto error_out;
3775 
3776 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
3777 		if (bar_num == IDIAG_BARACC_BAR_0) {
3778 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3779 				LPFC_PCI_IF0_BAR0_SIZE;
3780 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3781 		} else if (bar_num == IDIAG_BARACC_BAR_1) {
3782 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3783 				LPFC_PCI_IF0_BAR1_SIZE;
3784 			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
3785 		} else if (bar_num == IDIAG_BARACC_BAR_2) {
3786 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3787 				LPFC_PCI_IF0_BAR2_SIZE;
3788 			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
3789 		} else
3790 			goto error_out;
3791 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3792 		if (bar_num == IDIAG_BARACC_BAR_0) {
3793 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3794 				LPFC_PCI_IF2_BAR0_SIZE;
3795 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3796 		} else
3797 			goto error_out;
3798 	} else
3799 		goto error_out;
3800 
3801 	offset = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
3802 	if (offset % sizeof(uint32_t))
3803 		goto error_out;
3804 
3805 	bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
3806 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
3807 		/* Sanity check on PCI config read command line arguments */
3808 		if (rc != LPFC_PCI_BAR_RD_CMD_ARG)
3809 			goto error_out;
3810 		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
3811 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
3812 			if (offset > bar_size - sizeof(uint32_t))
3813 				goto error_out;
3814 			/* Starting offset to browse */
3815 			idiag.offset.last_rd = offset;
3816 		} else if (acc_range > SINGLE_WORD) {
3817 			if (offset + acc_range * sizeof(uint32_t) > bar_size)
3818 				goto error_out;
3819 			/* Starting offset to browse */
3820 			idiag.offset.last_rd = offset;
3821 		} else if (acc_range != SINGLE_WORD)
3822 			goto error_out;
3823 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR ||
3824 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST ||
3825 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
3826 		/* Sanity check on PCI bar write command line arguments */
3827 		if (rc != LPFC_PCI_BAR_WR_CMD_ARG)
3828 			goto error_out;
3829 		/* Write command to PCI bar space, read-modify-write */
3830 		acc_range = SINGLE_WORD;
3831 		value = idiag.cmd.data[IDIAG_BARACC_REG_VAL_INDX];
3832 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR) {
3833 			writel(value, mem_mapped_bar + offset);
3834 			readl(mem_mapped_bar + offset);
3835 		}
3836 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST) {
3837 			u32val = readl(mem_mapped_bar + offset);
3838 			u32val |= value;
3839 			writel(u32val, mem_mapped_bar + offset);
3840 			readl(mem_mapped_bar + offset);
3841 		}
3842 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
3843 			u32val = readl(mem_mapped_bar + offset);
3844 			u32val &= ~value;
3845 			writel(u32val, mem_mapped_bar + offset);
3846 			readl(mem_mapped_bar + offset);
3847 		}
3848 	} else
3849 		/* All other opecodes are illegal for now */
3850 		goto error_out;
3851 
3852 	return nbytes;
3853 error_out:
3854 	memset(&idiag, 0, sizeof(idiag));
3855 	return -EINVAL;
3856 }
3857 
3858 static int
3859 __lpfc_idiag_print_wq(struct lpfc_queue *qp, char *wqtype,
3860 			char *pbuffer, int len)
3861 {
3862 	if (!qp)
3863 		return len;
3864 
3865 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3866 			"\t\t%s WQ info: ", wqtype);
3867 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3868 			"AssocCQID[%04d]: WQ-STAT[oflow:x%x posted:x%llx]\n",
3869 			qp->assoc_qid, qp->q_cnt_1,
3870 			(unsigned long long)qp->q_cnt_4);
3871 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3872 			"\t\tWQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3873 			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]",
3874 			qp->queue_id, qp->entry_count,
3875 			qp->entry_size, qp->host_index,
3876 			qp->hba_index, qp->notify_interval);
3877 	len +=  scnprintf(pbuffer + len,
3878 			LPFC_QUE_INFO_GET_BUF_SIZE - len, "\n");
3879 	return len;
3880 }
3881 
3882 static int
3883 lpfc_idiag_wqs_for_cq(struct lpfc_hba *phba, char *wqtype, char *pbuffer,
3884 		int *len, int max_cnt, int cq_id)
3885 {
3886 	struct lpfc_queue *qp;
3887 	int qidx;
3888 
3889 	for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
3890 		qp = phba->sli4_hba.hdwq[qidx].io_wq;
3891 		if (qp->assoc_qid != cq_id)
3892 			continue;
3893 		*len = __lpfc_idiag_print_wq(qp, wqtype, pbuffer, *len);
3894 		if (*len >= max_cnt)
3895 			return 1;
3896 	}
3897 	return 0;
3898 }
3899 
3900 static int
3901 __lpfc_idiag_print_cq(struct lpfc_queue *qp, char *cqtype,
3902 			char *pbuffer, int len)
3903 {
3904 	if (!qp)
3905 		return len;
3906 
3907 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3908 			"\t%s CQ info: ", cqtype);
3909 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3910 			"AssocEQID[%02d]: CQ STAT[max:x%x relw:x%x "
3911 			"xabt:x%x wq:x%llx]\n",
3912 			qp->assoc_qid, qp->q_cnt_1, qp->q_cnt_2,
3913 			qp->q_cnt_3, (unsigned long long)qp->q_cnt_4);
3914 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3915 			"\tCQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3916 			"HST-IDX[%04d], NTFI[%03d], PLMT[%03d]",
3917 			qp->queue_id, qp->entry_count,
3918 			qp->entry_size, qp->host_index,
3919 			qp->notify_interval, qp->max_proc_limit);
3920 
3921 	len +=  scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3922 			"\n");
3923 
3924 	return len;
3925 }
3926 
3927 static int
3928 __lpfc_idiag_print_rqpair(struct lpfc_queue *qp, struct lpfc_queue *datqp,
3929 			char *rqtype, char *pbuffer, int len)
3930 {
3931 	if (!qp || !datqp)
3932 		return len;
3933 
3934 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3935 			"\t\t%s RQ info: ", rqtype);
3936 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3937 			"AssocCQID[%02d]: RQ-STAT[nopost:x%x nobuf:x%x "
3938 			"posted:x%x rcv:x%llx]\n",
3939 			qp->assoc_qid, qp->q_cnt_1, qp->q_cnt_2,
3940 			qp->q_cnt_3, (unsigned long long)qp->q_cnt_4);
3941 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3942 			"\t\tHQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3943 			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]\n",
3944 			qp->queue_id, qp->entry_count, qp->entry_size,
3945 			qp->host_index, qp->hba_index, qp->notify_interval);
3946 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3947 			"\t\tDQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3948 			"HST-IDX[%04d], PRT-IDX[%04d], NTFI[%03d]\n",
3949 			datqp->queue_id, datqp->entry_count,
3950 			datqp->entry_size, datqp->host_index,
3951 			datqp->hba_index, datqp->notify_interval);
3952 	return len;
3953 }
3954 
3955 static int
3956 lpfc_idiag_cqs_for_eq(struct lpfc_hba *phba, char *pbuffer,
3957 		int *len, int max_cnt, int eqidx, int eq_id)
3958 {
3959 	struct lpfc_queue *qp;
3960 	int rc;
3961 
3962 	qp = phba->sli4_hba.hdwq[eqidx].io_cq;
3963 
3964 	*len = __lpfc_idiag_print_cq(qp, "IO", pbuffer, *len);
3965 
3966 	/* Reset max counter */
3967 	qp->CQ_max_cqe = 0;
3968 
3969 	if (*len >= max_cnt)
3970 		return 1;
3971 
3972 	rc = lpfc_idiag_wqs_for_cq(phba, "IO", pbuffer, len,
3973 				   max_cnt, qp->queue_id);
3974 	if (rc)
3975 		return 1;
3976 
3977 	if ((eqidx < phba->cfg_nvmet_mrq) && phba->nvmet_support) {
3978 		/* NVMET CQset */
3979 		qp = phba->sli4_hba.nvmet_cqset[eqidx];
3980 		*len = __lpfc_idiag_print_cq(qp, "NVMET CQset", pbuffer, *len);
3981 
3982 		/* Reset max counter */
3983 		qp->CQ_max_cqe = 0;
3984 
3985 		if (*len >= max_cnt)
3986 			return 1;
3987 
3988 		/* RQ header */
3989 		qp = phba->sli4_hba.nvmet_mrq_hdr[eqidx];
3990 		*len = __lpfc_idiag_print_rqpair(qp,
3991 				phba->sli4_hba.nvmet_mrq_data[eqidx],
3992 				"NVMET MRQ", pbuffer, *len);
3993 
3994 		if (*len >= max_cnt)
3995 			return 1;
3996 	}
3997 
3998 	return 0;
3999 }
4000 
4001 static int
4002 __lpfc_idiag_print_eq(struct lpfc_queue *qp, char *eqtype,
4003 			char *pbuffer, int len)
4004 {
4005 	if (!qp)
4006 		return len;
4007 
4008 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
4009 			"\n%s EQ info: EQ-STAT[max:x%x noE:x%x "
4010 			"cqe_proc:x%x eqe_proc:x%llx eqd %d]\n",
4011 			eqtype, qp->q_cnt_1, qp->q_cnt_2, qp->q_cnt_3,
4012 			(unsigned long long)qp->q_cnt_4, qp->q_mode);
4013 	len += scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
4014 			"EQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
4015 			"HST-IDX[%04d], NTFI[%03d], PLMT[%03d], AFFIN[%03d]",
4016 			qp->queue_id, qp->entry_count, qp->entry_size,
4017 			qp->host_index, qp->notify_interval,
4018 			qp->max_proc_limit, qp->chann);
4019 	len +=  scnprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
4020 			"\n");
4021 
4022 	return len;
4023 }
4024 
4025 /**
4026  * lpfc_idiag_queinfo_read - idiag debugfs read queue information
4027  * @file: The file pointer to read from.
4028  * @buf: The buffer to copy the data to.
4029  * @nbytes: The number of bytes to read.
4030  * @ppos: The position in the file to start reading from.
4031  *
4032  * Description:
4033  * This routine reads data from the @phba SLI4 PCI function queue information,
4034  * and copies to user @buf.
4035  * This routine only returns 1 EQs worth of information. It remembers the last
4036  * EQ read and jumps to the next EQ. Thus subsequent calls to queInfo will
4037  * retrieve all EQs allocated for the phba.
4038  *
4039  * Returns:
4040  * This function returns the amount of data that was read (this could be less
4041  * than @nbytes if the end of the file was reached) or a negative error value.
4042  **/
4043 static ssize_t
4044 lpfc_idiag_queinfo_read(struct file *file, char __user *buf, size_t nbytes,
4045 			loff_t *ppos)
4046 {
4047 	struct lpfc_debug *debug = file->private_data;
4048 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4049 	char *pbuffer;
4050 	int max_cnt, rc, x, len = 0;
4051 	struct lpfc_queue *qp = NULL;
4052 
4053 	if (!debug->buffer)
4054 		debug->buffer = kmalloc(LPFC_QUE_INFO_GET_BUF_SIZE, GFP_KERNEL);
4055 	if (!debug->buffer)
4056 		return 0;
4057 	pbuffer = debug->buffer;
4058 	max_cnt = LPFC_QUE_INFO_GET_BUF_SIZE - 256;
4059 
4060 	if (*ppos)
4061 		return 0;
4062 
4063 	spin_lock_irq(&phba->hbalock);
4064 
4065 	/* Fast-path event queue */
4066 	if (phba->sli4_hba.hdwq && phba->cfg_hdw_queue) {
4067 
4068 		x = phba->lpfc_idiag_last_eq;
4069 		phba->lpfc_idiag_last_eq++;
4070 		if (phba->lpfc_idiag_last_eq >= phba->cfg_hdw_queue)
4071 			phba->lpfc_idiag_last_eq = 0;
4072 
4073 		len += scnprintf(pbuffer + len,
4074 				 LPFC_QUE_INFO_GET_BUF_SIZE - len,
4075 				 "HDWQ %d out of %d HBA HDWQs\n",
4076 				 x, phba->cfg_hdw_queue);
4077 
4078 		/* Fast-path EQ */
4079 		qp = phba->sli4_hba.hdwq[x].hba_eq;
4080 		if (!qp)
4081 			goto out;
4082 
4083 		len = __lpfc_idiag_print_eq(qp, "HBA", pbuffer, len);
4084 
4085 		/* Reset max counter */
4086 		qp->EQ_max_eqe = 0;
4087 
4088 		if (len >= max_cnt)
4089 			goto too_big;
4090 
4091 		/* will dump both fcp and nvme cqs/wqs for the eq */
4092 		rc = lpfc_idiag_cqs_for_eq(phba, pbuffer, &len,
4093 			max_cnt, x, qp->queue_id);
4094 		if (rc)
4095 			goto too_big;
4096 
4097 		/* Only EQ 0 has slow path CQs configured */
4098 		if (x)
4099 			goto out;
4100 
4101 		/* Slow-path mailbox CQ */
4102 		qp = phba->sli4_hba.mbx_cq;
4103 		len = __lpfc_idiag_print_cq(qp, "MBX", pbuffer, len);
4104 		if (len >= max_cnt)
4105 			goto too_big;
4106 
4107 		/* Slow-path MBOX MQ */
4108 		qp = phba->sli4_hba.mbx_wq;
4109 		len = __lpfc_idiag_print_wq(qp, "MBX", pbuffer, len);
4110 		if (len >= max_cnt)
4111 			goto too_big;
4112 
4113 		/* Slow-path ELS response CQ */
4114 		qp = phba->sli4_hba.els_cq;
4115 		len = __lpfc_idiag_print_cq(qp, "ELS", pbuffer, len);
4116 		/* Reset max counter */
4117 		if (qp)
4118 			qp->CQ_max_cqe = 0;
4119 		if (len >= max_cnt)
4120 			goto too_big;
4121 
4122 		/* Slow-path ELS WQ */
4123 		qp = phba->sli4_hba.els_wq;
4124 		len = __lpfc_idiag_print_wq(qp, "ELS", pbuffer, len);
4125 		if (len >= max_cnt)
4126 			goto too_big;
4127 
4128 		qp = phba->sli4_hba.hdr_rq;
4129 		len = __lpfc_idiag_print_rqpair(qp, phba->sli4_hba.dat_rq,
4130 						"ELS RQpair", pbuffer, len);
4131 		if (len >= max_cnt)
4132 			goto too_big;
4133 
4134 		/* Slow-path NVME LS response CQ */
4135 		qp = phba->sli4_hba.nvmels_cq;
4136 		len = __lpfc_idiag_print_cq(qp, "NVME LS",
4137 						pbuffer, len);
4138 		/* Reset max counter */
4139 		if (qp)
4140 			qp->CQ_max_cqe = 0;
4141 		if (len >= max_cnt)
4142 			goto too_big;
4143 
4144 		/* Slow-path NVME LS WQ */
4145 		qp = phba->sli4_hba.nvmels_wq;
4146 		len = __lpfc_idiag_print_wq(qp, "NVME LS",
4147 						pbuffer, len);
4148 		if (len >= max_cnt)
4149 			goto too_big;
4150 
4151 		goto out;
4152 	}
4153 
4154 	spin_unlock_irq(&phba->hbalock);
4155 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4156 
4157 too_big:
4158 	len +=  scnprintf(pbuffer + len,
4159 		LPFC_QUE_INFO_GET_BUF_SIZE - len, "Truncated ...\n");
4160 out:
4161 	spin_unlock_irq(&phba->hbalock);
4162 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4163 }
4164 
4165 /**
4166  * lpfc_idiag_que_param_check - queue access command parameter sanity check
4167  * @q: The pointer to queue structure.
4168  * @index: The index into a queue entry.
4169  * @count: The number of queue entries to access.
4170  *
4171  * Description:
4172  * The routine performs sanity check on device queue access method commands.
4173  *
4174  * Returns:
4175  * This function returns -EINVAL when fails the sanity check, otherwise, it
4176  * returns 0.
4177  **/
4178 static int
4179 lpfc_idiag_que_param_check(struct lpfc_queue *q, int index, int count)
4180 {
4181 	/* Only support single entry read or browsing */
4182 	if ((count != 1) && (count != LPFC_QUE_ACC_BROWSE))
4183 		return -EINVAL;
4184 	if (index > q->entry_count - 1)
4185 		return -EINVAL;
4186 	return 0;
4187 }
4188 
4189 /**
4190  * lpfc_idiag_queacc_read_qe - read a single entry from the given queue index
4191  * @pbuffer: The pointer to buffer to copy the read data into.
4192  * @pque: The pointer to the queue to be read.
4193  * @index: The index into the queue entry.
4194  *
4195  * Description:
4196  * This routine reads out a single entry from the given queue's index location
4197  * and copies it into the buffer provided.
4198  *
4199  * Returns:
4200  * This function returns 0 when it fails, otherwise, it returns the length of
4201  * the data read into the buffer provided.
4202  **/
4203 static int
4204 lpfc_idiag_queacc_read_qe(char *pbuffer, int len, struct lpfc_queue *pque,
4205 			  uint32_t index)
4206 {
4207 	int offset, esize;
4208 	uint32_t *pentry;
4209 
4210 	if (!pbuffer || !pque)
4211 		return 0;
4212 
4213 	esize = pque->entry_size;
4214 	len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
4215 			"QE-INDEX[%04d]:\n", index);
4216 
4217 	offset = 0;
4218 	pentry = lpfc_sli4_qe(pque, index);
4219 	while (esize > 0) {
4220 		len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
4221 				"%08x ", *pentry);
4222 		pentry++;
4223 		offset += sizeof(uint32_t);
4224 		esize -= sizeof(uint32_t);
4225 		if (esize > 0 && !(offset % (4 * sizeof(uint32_t))))
4226 			len += scnprintf(pbuffer+len,
4227 					LPFC_QUE_ACC_BUF_SIZE-len, "\n");
4228 	}
4229 	len += scnprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len, "\n");
4230 
4231 	return len;
4232 }
4233 
4234 /**
4235  * lpfc_idiag_queacc_read - idiag debugfs read port queue
4236  * @file: The file pointer to read from.
4237  * @buf: The buffer to copy the data to.
4238  * @nbytes: The number of bytes to read.
4239  * @ppos: The position in the file to start reading from.
4240  *
4241  * Description:
4242  * This routine reads data from the @phba device queue memory according to the
4243  * idiag command, and copies to user @buf. Depending on the queue dump read
4244  * command setup, it does either a single queue entry read or browing through
4245  * all entries of the queue.
4246  *
4247  * Returns:
4248  * This function returns the amount of data that was read (this could be less
4249  * than @nbytes if the end of the file was reached) or a negative error value.
4250  **/
4251 static ssize_t
4252 lpfc_idiag_queacc_read(struct file *file, char __user *buf, size_t nbytes,
4253 		       loff_t *ppos)
4254 {
4255 	struct lpfc_debug *debug = file->private_data;
4256 	uint32_t last_index, index, count;
4257 	struct lpfc_queue *pque = NULL;
4258 	char *pbuffer;
4259 	int len = 0;
4260 
4261 	/* This is a user read operation */
4262 	debug->op = LPFC_IDIAG_OP_RD;
4263 
4264 	if (!debug->buffer)
4265 		debug->buffer = kmalloc(LPFC_QUE_ACC_BUF_SIZE, GFP_KERNEL);
4266 	if (!debug->buffer)
4267 		return 0;
4268 	pbuffer = debug->buffer;
4269 
4270 	if (*ppos)
4271 		return 0;
4272 
4273 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
4274 		index = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
4275 		count = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
4276 		pque = (struct lpfc_queue *)idiag.ptr_private;
4277 	} else
4278 		return 0;
4279 
4280 	/* Browse the queue starting from index */
4281 	if (count == LPFC_QUE_ACC_BROWSE)
4282 		goto que_browse;
4283 
4284 	/* Read a single entry from the queue */
4285 	len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
4286 
4287 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4288 
4289 que_browse:
4290 
4291 	/* Browse all entries from the queue */
4292 	last_index = idiag.offset.last_rd;
4293 	index = last_index;
4294 
4295 	while (len < LPFC_QUE_ACC_SIZE - pque->entry_size) {
4296 		len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
4297 		index++;
4298 		if (index > pque->entry_count - 1)
4299 			break;
4300 	}
4301 
4302 	/* Set up the offset for next portion of pci cfg read */
4303 	if (index > pque->entry_count - 1)
4304 		index = 0;
4305 	idiag.offset.last_rd = index;
4306 
4307 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4308 }
4309 
4310 /**
4311  * lpfc_idiag_queacc_write - Syntax check and set up idiag queacc commands
4312  * @file: The file pointer to read from.
4313  * @buf: The buffer to copy the user data from.
4314  * @nbytes: The number of bytes to get.
4315  * @ppos: The position in the file to start reading from.
4316  *
4317  * This routine get the debugfs idiag command struct from user space and then
4318  * perform the syntax check for port queue read (dump) or write (set) command
4319  * accordingly. In the case of port queue read command, it sets up the command
4320  * in the idiag command struct for the following debugfs read operation. In
4321  * the case of port queue write operation, it executes the write operation
4322  * into the port queue entry accordingly.
4323  *
4324  * It returns the @nbytges passing in from debugfs user space when successful.
4325  * In case of error conditions, it returns proper error code back to the user
4326  * space.
4327  **/
4328 static ssize_t
4329 lpfc_idiag_queacc_write(struct file *file, const char __user *buf,
4330 			size_t nbytes, loff_t *ppos)
4331 {
4332 	struct lpfc_debug *debug = file->private_data;
4333 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4334 	uint32_t qidx, quetp, queid, index, count, offset, value;
4335 	uint32_t *pentry;
4336 	struct lpfc_queue *pque, *qp;
4337 	int rc;
4338 
4339 	/* This is a user write operation */
4340 	debug->op = LPFC_IDIAG_OP_WR;
4341 
4342 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4343 	if (rc < 0)
4344 		return rc;
4345 
4346 	/* Get and sanity check on command feilds */
4347 	quetp  = idiag.cmd.data[IDIAG_QUEACC_QUETP_INDX];
4348 	queid  = idiag.cmd.data[IDIAG_QUEACC_QUEID_INDX];
4349 	index  = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
4350 	count  = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
4351 	offset = idiag.cmd.data[IDIAG_QUEACC_OFFST_INDX];
4352 	value  = idiag.cmd.data[IDIAG_QUEACC_VALUE_INDX];
4353 
4354 	/* Sanity check on command line arguments */
4355 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
4356 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
4357 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
4358 		if (rc != LPFC_QUE_ACC_WR_CMD_ARG)
4359 			goto error_out;
4360 		if (count != 1)
4361 			goto error_out;
4362 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
4363 		if (rc != LPFC_QUE_ACC_RD_CMD_ARG)
4364 			goto error_out;
4365 	} else
4366 		goto error_out;
4367 
4368 	switch (quetp) {
4369 	case LPFC_IDIAG_EQ:
4370 		/* HBA event queue */
4371 		if (phba->sli4_hba.hdwq) {
4372 			for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
4373 				qp = phba->sli4_hba.hdwq[qidx].hba_eq;
4374 				if (qp && qp->queue_id == queid) {
4375 					/* Sanity check */
4376 					rc = lpfc_idiag_que_param_check(qp,
4377 						index, count);
4378 					if (rc)
4379 						goto error_out;
4380 					idiag.ptr_private = qp;
4381 					goto pass_check;
4382 				}
4383 			}
4384 		}
4385 		goto error_out;
4386 		break;
4387 	case LPFC_IDIAG_CQ:
4388 		/* MBX complete queue */
4389 		if (phba->sli4_hba.mbx_cq &&
4390 		    phba->sli4_hba.mbx_cq->queue_id == queid) {
4391 			/* Sanity check */
4392 			rc = lpfc_idiag_que_param_check(
4393 					phba->sli4_hba.mbx_cq, index, count);
4394 			if (rc)
4395 				goto error_out;
4396 			idiag.ptr_private = phba->sli4_hba.mbx_cq;
4397 			goto pass_check;
4398 		}
4399 		/* ELS complete queue */
4400 		if (phba->sli4_hba.els_cq &&
4401 		    phba->sli4_hba.els_cq->queue_id == queid) {
4402 			/* Sanity check */
4403 			rc = lpfc_idiag_que_param_check(
4404 					phba->sli4_hba.els_cq, index, count);
4405 			if (rc)
4406 				goto error_out;
4407 			idiag.ptr_private = phba->sli4_hba.els_cq;
4408 			goto pass_check;
4409 		}
4410 		/* NVME LS complete queue */
4411 		if (phba->sli4_hba.nvmels_cq &&
4412 		    phba->sli4_hba.nvmels_cq->queue_id == queid) {
4413 			/* Sanity check */
4414 			rc = lpfc_idiag_que_param_check(
4415 					phba->sli4_hba.nvmels_cq, index, count);
4416 			if (rc)
4417 				goto error_out;
4418 			idiag.ptr_private = phba->sli4_hba.nvmels_cq;
4419 			goto pass_check;
4420 		}
4421 		/* FCP complete queue */
4422 		if (phba->sli4_hba.hdwq) {
4423 			for (qidx = 0; qidx < phba->cfg_hdw_queue;
4424 								qidx++) {
4425 				qp = phba->sli4_hba.hdwq[qidx].io_cq;
4426 				if (qp && qp->queue_id == queid) {
4427 					/* Sanity check */
4428 					rc = lpfc_idiag_que_param_check(
4429 						qp, index, count);
4430 					if (rc)
4431 						goto error_out;
4432 					idiag.ptr_private = qp;
4433 					goto pass_check;
4434 				}
4435 			}
4436 		}
4437 		goto error_out;
4438 		break;
4439 	case LPFC_IDIAG_MQ:
4440 		/* MBX work queue */
4441 		if (phba->sli4_hba.mbx_wq &&
4442 		    phba->sli4_hba.mbx_wq->queue_id == queid) {
4443 			/* Sanity check */
4444 			rc = lpfc_idiag_que_param_check(
4445 					phba->sli4_hba.mbx_wq, index, count);
4446 			if (rc)
4447 				goto error_out;
4448 			idiag.ptr_private = phba->sli4_hba.mbx_wq;
4449 			goto pass_check;
4450 		}
4451 		goto error_out;
4452 		break;
4453 	case LPFC_IDIAG_WQ:
4454 		/* ELS work queue */
4455 		if (phba->sli4_hba.els_wq &&
4456 		    phba->sli4_hba.els_wq->queue_id == queid) {
4457 			/* Sanity check */
4458 			rc = lpfc_idiag_que_param_check(
4459 					phba->sli4_hba.els_wq, index, count);
4460 			if (rc)
4461 				goto error_out;
4462 			idiag.ptr_private = phba->sli4_hba.els_wq;
4463 			goto pass_check;
4464 		}
4465 		/* NVME LS work queue */
4466 		if (phba->sli4_hba.nvmels_wq &&
4467 		    phba->sli4_hba.nvmels_wq->queue_id == queid) {
4468 			/* Sanity check */
4469 			rc = lpfc_idiag_que_param_check(
4470 					phba->sli4_hba.nvmels_wq, index, count);
4471 			if (rc)
4472 				goto error_out;
4473 			idiag.ptr_private = phba->sli4_hba.nvmels_wq;
4474 			goto pass_check;
4475 		}
4476 
4477 		if (phba->sli4_hba.hdwq) {
4478 			/* FCP/SCSI work queue */
4479 			for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
4480 				qp = phba->sli4_hba.hdwq[qidx].io_wq;
4481 				if (qp && qp->queue_id == queid) {
4482 					/* Sanity check */
4483 					rc = lpfc_idiag_que_param_check(
4484 						qp, index, count);
4485 					if (rc)
4486 						goto error_out;
4487 					idiag.ptr_private = qp;
4488 					goto pass_check;
4489 				}
4490 			}
4491 		}
4492 
4493 		goto error_out;
4494 		break;
4495 	case LPFC_IDIAG_RQ:
4496 		/* HDR queue */
4497 		if (phba->sli4_hba.hdr_rq &&
4498 		    phba->sli4_hba.hdr_rq->queue_id == queid) {
4499 			/* Sanity check */
4500 			rc = lpfc_idiag_que_param_check(
4501 					phba->sli4_hba.hdr_rq, index, count);
4502 			if (rc)
4503 				goto error_out;
4504 			idiag.ptr_private = phba->sli4_hba.hdr_rq;
4505 			goto pass_check;
4506 		}
4507 		/* DAT queue */
4508 		if (phba->sli4_hba.dat_rq &&
4509 		    phba->sli4_hba.dat_rq->queue_id == queid) {
4510 			/* Sanity check */
4511 			rc = lpfc_idiag_que_param_check(
4512 					phba->sli4_hba.dat_rq, index, count);
4513 			if (rc)
4514 				goto error_out;
4515 			idiag.ptr_private = phba->sli4_hba.dat_rq;
4516 			goto pass_check;
4517 		}
4518 		goto error_out;
4519 		break;
4520 	default:
4521 		goto error_out;
4522 		break;
4523 	}
4524 
4525 pass_check:
4526 
4527 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
4528 		if (count == LPFC_QUE_ACC_BROWSE)
4529 			idiag.offset.last_rd = index;
4530 	}
4531 
4532 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
4533 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
4534 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
4535 		/* Additional sanity checks on write operation */
4536 		pque = (struct lpfc_queue *)idiag.ptr_private;
4537 		if (offset > pque->entry_size/sizeof(uint32_t) - 1)
4538 			goto error_out;
4539 		pentry = lpfc_sli4_qe(pque, index);
4540 		pentry += offset;
4541 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR)
4542 			*pentry = value;
4543 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST)
4544 			*pentry |= value;
4545 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL)
4546 			*pentry &= ~value;
4547 	}
4548 	return nbytes;
4549 
4550 error_out:
4551 	/* Clean out command structure on command error out */
4552 	memset(&idiag, 0, sizeof(idiag));
4553 	return -EINVAL;
4554 }
4555 
4556 /**
4557  * lpfc_idiag_drbacc_read_reg - idiag debugfs read a doorbell register
4558  * @phba: The pointer to hba structure.
4559  * @pbuffer: The pointer to the buffer to copy the data to.
4560  * @len: The length of bytes to copied.
4561  * @drbregid: The id to doorbell registers.
4562  *
4563  * Description:
4564  * This routine reads a doorbell register and copies its content to the
4565  * user buffer pointed to by @pbuffer.
4566  *
4567  * Returns:
4568  * This function returns the amount of data that was copied into @pbuffer.
4569  **/
4570 static int
4571 lpfc_idiag_drbacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
4572 			   int len, uint32_t drbregid)
4573 {
4574 
4575 	if (!pbuffer)
4576 		return 0;
4577 
4578 	switch (drbregid) {
4579 	case LPFC_DRB_EQ:
4580 		len += scnprintf(pbuffer + len, LPFC_DRB_ACC_BUF_SIZE-len,
4581 				"EQ-DRB-REG: 0x%08x\n",
4582 				readl(phba->sli4_hba.EQDBregaddr));
4583 		break;
4584 	case LPFC_DRB_CQ:
4585 		len += scnprintf(pbuffer + len, LPFC_DRB_ACC_BUF_SIZE - len,
4586 				"CQ-DRB-REG: 0x%08x\n",
4587 				readl(phba->sli4_hba.CQDBregaddr));
4588 		break;
4589 	case LPFC_DRB_MQ:
4590 		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4591 				"MQ-DRB-REG:   0x%08x\n",
4592 				readl(phba->sli4_hba.MQDBregaddr));
4593 		break;
4594 	case LPFC_DRB_WQ:
4595 		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4596 				"WQ-DRB-REG:   0x%08x\n",
4597 				readl(phba->sli4_hba.WQDBregaddr));
4598 		break;
4599 	case LPFC_DRB_RQ:
4600 		len += scnprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4601 				"RQ-DRB-REG:   0x%08x\n",
4602 				readl(phba->sli4_hba.RQDBregaddr));
4603 		break;
4604 	default:
4605 		break;
4606 	}
4607 
4608 	return len;
4609 }
4610 
4611 /**
4612  * lpfc_idiag_drbacc_read - idiag debugfs read port doorbell
4613  * @file: The file pointer to read from.
4614  * @buf: The buffer to copy the data to.
4615  * @nbytes: The number of bytes to read.
4616  * @ppos: The position in the file to start reading from.
4617  *
4618  * Description:
4619  * This routine reads data from the @phba device doorbell register according
4620  * to the idiag command, and copies to user @buf. Depending on the doorbell
4621  * register read command setup, it does either a single doorbell register
4622  * read or dump all doorbell registers.
4623  *
4624  * Returns:
4625  * This function returns the amount of data that was read (this could be less
4626  * than @nbytes if the end of the file was reached) or a negative error value.
4627  **/
4628 static ssize_t
4629 lpfc_idiag_drbacc_read(struct file *file, char __user *buf, size_t nbytes,
4630 		       loff_t *ppos)
4631 {
4632 	struct lpfc_debug *debug = file->private_data;
4633 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4634 	uint32_t drb_reg_id, i;
4635 	char *pbuffer;
4636 	int len = 0;
4637 
4638 	/* This is a user read operation */
4639 	debug->op = LPFC_IDIAG_OP_RD;
4640 
4641 	if (!debug->buffer)
4642 		debug->buffer = kmalloc(LPFC_DRB_ACC_BUF_SIZE, GFP_KERNEL);
4643 	if (!debug->buffer)
4644 		return 0;
4645 	pbuffer = debug->buffer;
4646 
4647 	if (*ppos)
4648 		return 0;
4649 
4650 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD)
4651 		drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
4652 	else
4653 		return 0;
4654 
4655 	if (drb_reg_id == LPFC_DRB_ACC_ALL)
4656 		for (i = 1; i <= LPFC_DRB_MAX; i++)
4657 			len = lpfc_idiag_drbacc_read_reg(phba,
4658 							 pbuffer, len, i);
4659 	else
4660 		len = lpfc_idiag_drbacc_read_reg(phba,
4661 						 pbuffer, len, drb_reg_id);
4662 
4663 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4664 }
4665 
4666 /**
4667  * lpfc_idiag_drbacc_write - Syntax check and set up idiag drbacc commands
4668  * @file: The file pointer to read from.
4669  * @buf: The buffer to copy the user data from.
4670  * @nbytes: The number of bytes to get.
4671  * @ppos: The position in the file to start reading from.
4672  *
4673  * This routine get the debugfs idiag command struct from user space and then
4674  * perform the syntax check for port doorbell register read (dump) or write
4675  * (set) command accordingly. In the case of port queue read command, it sets
4676  * up the command in the idiag command struct for the following debugfs read
4677  * operation. In the case of port doorbell register write operation, it
4678  * executes the write operation into the port doorbell register accordingly.
4679  *
4680  * It returns the @nbytges passing in from debugfs user space when successful.
4681  * In case of error conditions, it returns proper error code back to the user
4682  * space.
4683  **/
4684 static ssize_t
4685 lpfc_idiag_drbacc_write(struct file *file, const char __user *buf,
4686 			size_t nbytes, loff_t *ppos)
4687 {
4688 	struct lpfc_debug *debug = file->private_data;
4689 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4690 	uint32_t drb_reg_id, value, reg_val = 0;
4691 	void __iomem *drb_reg;
4692 	int rc;
4693 
4694 	/* This is a user write operation */
4695 	debug->op = LPFC_IDIAG_OP_WR;
4696 
4697 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4698 	if (rc < 0)
4699 		return rc;
4700 
4701 	/* Sanity check on command line arguments */
4702 	drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
4703 	value = idiag.cmd.data[IDIAG_DRBACC_VALUE_INDX];
4704 
4705 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
4706 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
4707 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4708 		if (rc != LPFC_DRB_ACC_WR_CMD_ARG)
4709 			goto error_out;
4710 		if (drb_reg_id > LPFC_DRB_MAX)
4711 			goto error_out;
4712 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD) {
4713 		if (rc != LPFC_DRB_ACC_RD_CMD_ARG)
4714 			goto error_out;
4715 		if ((drb_reg_id > LPFC_DRB_MAX) &&
4716 		    (drb_reg_id != LPFC_DRB_ACC_ALL))
4717 			goto error_out;
4718 	} else
4719 		goto error_out;
4720 
4721 	/* Perform the write access operation */
4722 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
4723 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
4724 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4725 		switch (drb_reg_id) {
4726 		case LPFC_DRB_EQ:
4727 			drb_reg = phba->sli4_hba.EQDBregaddr;
4728 			break;
4729 		case LPFC_DRB_CQ:
4730 			drb_reg = phba->sli4_hba.CQDBregaddr;
4731 			break;
4732 		case LPFC_DRB_MQ:
4733 			drb_reg = phba->sli4_hba.MQDBregaddr;
4734 			break;
4735 		case LPFC_DRB_WQ:
4736 			drb_reg = phba->sli4_hba.WQDBregaddr;
4737 			break;
4738 		case LPFC_DRB_RQ:
4739 			drb_reg = phba->sli4_hba.RQDBregaddr;
4740 			break;
4741 		default:
4742 			goto error_out;
4743 		}
4744 
4745 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR)
4746 			reg_val = value;
4747 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST) {
4748 			reg_val = readl(drb_reg);
4749 			reg_val |= value;
4750 		}
4751 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4752 			reg_val = readl(drb_reg);
4753 			reg_val &= ~value;
4754 		}
4755 		writel(reg_val, drb_reg);
4756 		readl(drb_reg); /* flush */
4757 	}
4758 	return nbytes;
4759 
4760 error_out:
4761 	/* Clean out command structure on command error out */
4762 	memset(&idiag, 0, sizeof(idiag));
4763 	return -EINVAL;
4764 }
4765 
4766 /**
4767  * lpfc_idiag_ctlacc_read_reg - idiag debugfs read a control registers
4768  * @phba: The pointer to hba structure.
4769  * @pbuffer: The pointer to the buffer to copy the data to.
4770  * @len: The length of bytes to copied.
4771  * @drbregid: The id to doorbell registers.
4772  *
4773  * Description:
4774  * This routine reads a control register and copies its content to the
4775  * user buffer pointed to by @pbuffer.
4776  *
4777  * Returns:
4778  * This function returns the amount of data that was copied into @pbuffer.
4779  **/
4780 static int
4781 lpfc_idiag_ctlacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
4782 			   int len, uint32_t ctlregid)
4783 {
4784 
4785 	if (!pbuffer)
4786 		return 0;
4787 
4788 	switch (ctlregid) {
4789 	case LPFC_CTL_PORT_SEM:
4790 		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4791 				"Port SemReg:   0x%08x\n",
4792 				readl(phba->sli4_hba.conf_regs_memmap_p +
4793 				      LPFC_CTL_PORT_SEM_OFFSET));
4794 		break;
4795 	case LPFC_CTL_PORT_STA:
4796 		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4797 				"Port StaReg:   0x%08x\n",
4798 				readl(phba->sli4_hba.conf_regs_memmap_p +
4799 				      LPFC_CTL_PORT_STA_OFFSET));
4800 		break;
4801 	case LPFC_CTL_PORT_CTL:
4802 		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4803 				"Port CtlReg:   0x%08x\n",
4804 				readl(phba->sli4_hba.conf_regs_memmap_p +
4805 				      LPFC_CTL_PORT_CTL_OFFSET));
4806 		break;
4807 	case LPFC_CTL_PORT_ER1:
4808 		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4809 				"Port Er1Reg:   0x%08x\n",
4810 				readl(phba->sli4_hba.conf_regs_memmap_p +
4811 				      LPFC_CTL_PORT_ER1_OFFSET));
4812 		break;
4813 	case LPFC_CTL_PORT_ER2:
4814 		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4815 				"Port Er2Reg:   0x%08x\n",
4816 				readl(phba->sli4_hba.conf_regs_memmap_p +
4817 				      LPFC_CTL_PORT_ER2_OFFSET));
4818 		break;
4819 	case LPFC_CTL_PDEV_CTL:
4820 		len += scnprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4821 				"PDev CtlReg:   0x%08x\n",
4822 				readl(phba->sli4_hba.conf_regs_memmap_p +
4823 				      LPFC_CTL_PDEV_CTL_OFFSET));
4824 		break;
4825 	default:
4826 		break;
4827 	}
4828 	return len;
4829 }
4830 
4831 /**
4832  * lpfc_idiag_ctlacc_read - idiag debugfs read port and device control register
4833  * @file: The file pointer to read from.
4834  * @buf: The buffer to copy the data to.
4835  * @nbytes: The number of bytes to read.
4836  * @ppos: The position in the file to start reading from.
4837  *
4838  * Description:
4839  * This routine reads data from the @phba port and device registers according
4840  * to the idiag command, and copies to user @buf.
4841  *
4842  * Returns:
4843  * This function returns the amount of data that was read (this could be less
4844  * than @nbytes if the end of the file was reached) or a negative error value.
4845  **/
4846 static ssize_t
4847 lpfc_idiag_ctlacc_read(struct file *file, char __user *buf, size_t nbytes,
4848 		       loff_t *ppos)
4849 {
4850 	struct lpfc_debug *debug = file->private_data;
4851 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4852 	uint32_t ctl_reg_id, i;
4853 	char *pbuffer;
4854 	int len = 0;
4855 
4856 	/* This is a user read operation */
4857 	debug->op = LPFC_IDIAG_OP_RD;
4858 
4859 	if (!debug->buffer)
4860 		debug->buffer = kmalloc(LPFC_CTL_ACC_BUF_SIZE, GFP_KERNEL);
4861 	if (!debug->buffer)
4862 		return 0;
4863 	pbuffer = debug->buffer;
4864 
4865 	if (*ppos)
4866 		return 0;
4867 
4868 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD)
4869 		ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
4870 	else
4871 		return 0;
4872 
4873 	if (ctl_reg_id == LPFC_CTL_ACC_ALL)
4874 		for (i = 1; i <= LPFC_CTL_MAX; i++)
4875 			len = lpfc_idiag_ctlacc_read_reg(phba,
4876 							 pbuffer, len, i);
4877 	else
4878 		len = lpfc_idiag_ctlacc_read_reg(phba,
4879 						 pbuffer, len, ctl_reg_id);
4880 
4881 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4882 }
4883 
4884 /**
4885  * lpfc_idiag_ctlacc_write - Syntax check and set up idiag ctlacc commands
4886  * @file: The file pointer to read from.
4887  * @buf: The buffer to copy the user data from.
4888  * @nbytes: The number of bytes to get.
4889  * @ppos: The position in the file to start reading from.
4890  *
4891  * This routine get the debugfs idiag command struct from user space and then
4892  * perform the syntax check for port and device control register read (dump)
4893  * or write (set) command accordingly.
4894  *
4895  * It returns the @nbytges passing in from debugfs user space when successful.
4896  * In case of error conditions, it returns proper error code back to the user
4897  * space.
4898  **/
4899 static ssize_t
4900 lpfc_idiag_ctlacc_write(struct file *file, const char __user *buf,
4901 			size_t nbytes, loff_t *ppos)
4902 {
4903 	struct lpfc_debug *debug = file->private_data;
4904 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4905 	uint32_t ctl_reg_id, value, reg_val = 0;
4906 	void __iomem *ctl_reg;
4907 	int rc;
4908 
4909 	/* This is a user write operation */
4910 	debug->op = LPFC_IDIAG_OP_WR;
4911 
4912 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4913 	if (rc < 0)
4914 		return rc;
4915 
4916 	/* Sanity check on command line arguments */
4917 	ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
4918 	value = idiag.cmd.data[IDIAG_CTLACC_VALUE_INDX];
4919 
4920 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
4921 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
4922 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4923 		if (rc != LPFC_CTL_ACC_WR_CMD_ARG)
4924 			goto error_out;
4925 		if (ctl_reg_id > LPFC_CTL_MAX)
4926 			goto error_out;
4927 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD) {
4928 		if (rc != LPFC_CTL_ACC_RD_CMD_ARG)
4929 			goto error_out;
4930 		if ((ctl_reg_id > LPFC_CTL_MAX) &&
4931 		    (ctl_reg_id != LPFC_CTL_ACC_ALL))
4932 			goto error_out;
4933 	} else
4934 		goto error_out;
4935 
4936 	/* Perform the write access operation */
4937 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
4938 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
4939 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4940 		switch (ctl_reg_id) {
4941 		case LPFC_CTL_PORT_SEM:
4942 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4943 					LPFC_CTL_PORT_SEM_OFFSET;
4944 			break;
4945 		case LPFC_CTL_PORT_STA:
4946 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4947 					LPFC_CTL_PORT_STA_OFFSET;
4948 			break;
4949 		case LPFC_CTL_PORT_CTL:
4950 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4951 					LPFC_CTL_PORT_CTL_OFFSET;
4952 			break;
4953 		case LPFC_CTL_PORT_ER1:
4954 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4955 					LPFC_CTL_PORT_ER1_OFFSET;
4956 			break;
4957 		case LPFC_CTL_PORT_ER2:
4958 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4959 					LPFC_CTL_PORT_ER2_OFFSET;
4960 			break;
4961 		case LPFC_CTL_PDEV_CTL:
4962 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4963 					LPFC_CTL_PDEV_CTL_OFFSET;
4964 			break;
4965 		default:
4966 			goto error_out;
4967 		}
4968 
4969 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR)
4970 			reg_val = value;
4971 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST) {
4972 			reg_val = readl(ctl_reg);
4973 			reg_val |= value;
4974 		}
4975 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4976 			reg_val = readl(ctl_reg);
4977 			reg_val &= ~value;
4978 		}
4979 		writel(reg_val, ctl_reg);
4980 		readl(ctl_reg); /* flush */
4981 	}
4982 	return nbytes;
4983 
4984 error_out:
4985 	/* Clean out command structure on command error out */
4986 	memset(&idiag, 0, sizeof(idiag));
4987 	return -EINVAL;
4988 }
4989 
4990 /**
4991  * lpfc_idiag_mbxacc_get_setup - idiag debugfs get mailbox access setup
4992  * @phba: Pointer to HBA context object.
4993  * @pbuffer: Pointer to data buffer.
4994  *
4995  * Description:
4996  * This routine gets the driver mailbox access debugfs setup information.
4997  *
4998  * Returns:
4999  * This function returns the amount of data that was read (this could be less
5000  * than @nbytes if the end of the file was reached) or a negative error value.
5001  **/
5002 static int
5003 lpfc_idiag_mbxacc_get_setup(struct lpfc_hba *phba, char *pbuffer)
5004 {
5005 	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
5006 	int len = 0;
5007 
5008 	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5009 	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5010 	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5011 	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5012 
5013 	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5014 			"mbx_dump_map: 0x%08x\n", mbx_dump_map);
5015 	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5016 			"mbx_dump_cnt: %04d\n", mbx_dump_cnt);
5017 	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5018 			"mbx_word_cnt: %04d\n", mbx_word_cnt);
5019 	len += scnprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
5020 			"mbx_mbox_cmd: 0x%02x\n", mbx_mbox_cmd);
5021 
5022 	return len;
5023 }
5024 
5025 /**
5026  * lpfc_idiag_mbxacc_read - idiag debugfs read on mailbox access
5027  * @file: The file pointer to read from.
5028  * @buf: The buffer to copy the data to.
5029  * @nbytes: The number of bytes to read.
5030  * @ppos: The position in the file to start reading from.
5031  *
5032  * Description:
5033  * This routine reads data from the @phba driver mailbox access debugfs setup
5034  * information.
5035  *
5036  * Returns:
5037  * This function returns the amount of data that was read (this could be less
5038  * than @nbytes if the end of the file was reached) or a negative error value.
5039  **/
5040 static ssize_t
5041 lpfc_idiag_mbxacc_read(struct file *file, char __user *buf, size_t nbytes,
5042 		       loff_t *ppos)
5043 {
5044 	struct lpfc_debug *debug = file->private_data;
5045 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
5046 	char *pbuffer;
5047 	int len = 0;
5048 
5049 	/* This is a user read operation */
5050 	debug->op = LPFC_IDIAG_OP_RD;
5051 
5052 	if (!debug->buffer)
5053 		debug->buffer = kmalloc(LPFC_MBX_ACC_BUF_SIZE, GFP_KERNEL);
5054 	if (!debug->buffer)
5055 		return 0;
5056 	pbuffer = debug->buffer;
5057 
5058 	if (*ppos)
5059 		return 0;
5060 
5061 	if ((idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP) &&
5062 	    (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP))
5063 		return 0;
5064 
5065 	len = lpfc_idiag_mbxacc_get_setup(phba, pbuffer);
5066 
5067 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
5068 }
5069 
5070 /**
5071  * lpfc_idiag_mbxacc_write - Syntax check and set up idiag mbxacc commands
5072  * @file: The file pointer to read from.
5073  * @buf: The buffer to copy the user data from.
5074  * @nbytes: The number of bytes to get.
5075  * @ppos: The position in the file to start reading from.
5076  *
5077  * This routine get the debugfs idiag command struct from user space and then
5078  * perform the syntax check for driver mailbox command (dump) and sets up the
5079  * necessary states in the idiag command struct accordingly.
5080  *
5081  * It returns the @nbytges passing in from debugfs user space when successful.
5082  * In case of error conditions, it returns proper error code back to the user
5083  * space.
5084  **/
5085 static ssize_t
5086 lpfc_idiag_mbxacc_write(struct file *file, const char __user *buf,
5087 			size_t nbytes, loff_t *ppos)
5088 {
5089 	struct lpfc_debug *debug = file->private_data;
5090 	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
5091 	int rc;
5092 
5093 	/* This is a user write operation */
5094 	debug->op = LPFC_IDIAG_OP_WR;
5095 
5096 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
5097 	if (rc < 0)
5098 		return rc;
5099 
5100 	/* Sanity check on command line arguments */
5101 	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5102 	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5103 	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5104 	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5105 
5106 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_MBXACC_DP) {
5107 		if (!(mbx_dump_map & LPFC_MBX_DMP_MBX_ALL))
5108 			goto error_out;
5109 		if ((mbx_dump_map & ~LPFC_MBX_DMP_MBX_ALL) &&
5110 		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
5111 			goto error_out;
5112 		if (mbx_word_cnt > sizeof(MAILBOX_t))
5113 			goto error_out;
5114 	} else if (idiag.cmd.opcode == LPFC_IDIAG_BSG_MBXACC_DP) {
5115 		if (!(mbx_dump_map & LPFC_BSG_DMP_MBX_ALL))
5116 			goto error_out;
5117 		if ((mbx_dump_map & ~LPFC_BSG_DMP_MBX_ALL) &&
5118 		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
5119 			goto error_out;
5120 		if (mbx_word_cnt > (BSG_MBOX_SIZE)/4)
5121 			goto error_out;
5122 		if (mbx_mbox_cmd != 0x9b)
5123 			goto error_out;
5124 	} else
5125 		goto error_out;
5126 
5127 	if (mbx_word_cnt == 0)
5128 		goto error_out;
5129 	if (rc != LPFC_MBX_DMP_ARG)
5130 		goto error_out;
5131 	if (mbx_mbox_cmd & ~0xff)
5132 		goto error_out;
5133 
5134 	/* condition for stop mailbox dump */
5135 	if (mbx_dump_cnt == 0)
5136 		goto reset_out;
5137 
5138 	return nbytes;
5139 
5140 reset_out:
5141 	/* Clean out command structure on command error out */
5142 	memset(&idiag, 0, sizeof(idiag));
5143 	return nbytes;
5144 
5145 error_out:
5146 	/* Clean out command structure on command error out */
5147 	memset(&idiag, 0, sizeof(idiag));
5148 	return -EINVAL;
5149 }
5150 
5151 /**
5152  * lpfc_idiag_extacc_avail_get - get the available extents information
5153  * @phba: pointer to lpfc hba data structure.
5154  * @pbuffer: pointer to internal buffer.
5155  * @len: length into the internal buffer data has been copied.
5156  *
5157  * Description:
5158  * This routine is to get the available extent information.
5159  *
5160  * Returns:
5161  * overall lenth of the data read into the internal buffer.
5162  **/
5163 static int
5164 lpfc_idiag_extacc_avail_get(struct lpfc_hba *phba, char *pbuffer, int len)
5165 {
5166 	uint16_t ext_cnt, ext_size;
5167 
5168 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5169 			"\nAvailable Extents Information:\n");
5170 
5171 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5172 			"\tPort Available VPI extents: ");
5173 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VPI,
5174 				       &ext_cnt, &ext_size);
5175 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5176 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
5177 
5178 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5179 			"\tPort Available VFI extents: ");
5180 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VFI,
5181 				       &ext_cnt, &ext_size);
5182 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5183 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
5184 
5185 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5186 			"\tPort Available RPI extents: ");
5187 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_RPI,
5188 				       &ext_cnt, &ext_size);
5189 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5190 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
5191 
5192 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5193 			"\tPort Available XRI extents: ");
5194 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_XRI,
5195 				       &ext_cnt, &ext_size);
5196 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5197 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
5198 
5199 	return len;
5200 }
5201 
5202 /**
5203  * lpfc_idiag_extacc_alloc_get - get the allocated extents information
5204  * @phba: pointer to lpfc hba data structure.
5205  * @pbuffer: pointer to internal buffer.
5206  * @len: length into the internal buffer data has been copied.
5207  *
5208  * Description:
5209  * This routine is to get the allocated extent information.
5210  *
5211  * Returns:
5212  * overall lenth of the data read into the internal buffer.
5213  **/
5214 static int
5215 lpfc_idiag_extacc_alloc_get(struct lpfc_hba *phba, char *pbuffer, int len)
5216 {
5217 	uint16_t ext_cnt, ext_size;
5218 	int rc;
5219 
5220 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5221 			"\nAllocated Extents Information:\n");
5222 
5223 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5224 			"\tHost Allocated VPI extents: ");
5225 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VPI,
5226 					    &ext_cnt, &ext_size);
5227 	if (!rc)
5228 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5229 				"Port %d Extent %3d, Size %3d\n",
5230 				phba->brd_no, ext_cnt, ext_size);
5231 	else
5232 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5233 				"N/A\n");
5234 
5235 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5236 			"\tHost Allocated VFI extents: ");
5237 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VFI,
5238 					    &ext_cnt, &ext_size);
5239 	if (!rc)
5240 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5241 				"Port %d Extent %3d, Size %3d\n",
5242 				phba->brd_no, ext_cnt, ext_size);
5243 	else
5244 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5245 				"N/A\n");
5246 
5247 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5248 			"\tHost Allocated RPI extents: ");
5249 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_RPI,
5250 					    &ext_cnt, &ext_size);
5251 	if (!rc)
5252 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5253 				"Port %d Extent %3d, Size %3d\n",
5254 				phba->brd_no, ext_cnt, ext_size);
5255 	else
5256 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5257 				"N/A\n");
5258 
5259 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5260 			"\tHost Allocated XRI extents: ");
5261 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_XRI,
5262 					    &ext_cnt, &ext_size);
5263 	if (!rc)
5264 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5265 				"Port %d Extent %3d, Size %3d\n",
5266 				phba->brd_no, ext_cnt, ext_size);
5267 	else
5268 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5269 				"N/A\n");
5270 
5271 	return len;
5272 }
5273 
5274 /**
5275  * lpfc_idiag_extacc_drivr_get - get driver extent information
5276  * @phba: pointer to lpfc hba data structure.
5277  * @pbuffer: pointer to internal buffer.
5278  * @len: length into the internal buffer data has been copied.
5279  *
5280  * Description:
5281  * This routine is to get the driver extent information.
5282  *
5283  * Returns:
5284  * overall lenth of the data read into the internal buffer.
5285  **/
5286 static int
5287 lpfc_idiag_extacc_drivr_get(struct lpfc_hba *phba, char *pbuffer, int len)
5288 {
5289 	struct lpfc_rsrc_blks *rsrc_blks;
5290 	int index;
5291 
5292 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5293 			"\nDriver Extents Information:\n");
5294 
5295 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5296 			"\tVPI extents:\n");
5297 	index = 0;
5298 	list_for_each_entry(rsrc_blks, &phba->lpfc_vpi_blk_list, list) {
5299 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5300 				"\t\tBlock %3d: Start %4d, Count %4d\n",
5301 				index, rsrc_blks->rsrc_start,
5302 				rsrc_blks->rsrc_size);
5303 		index++;
5304 	}
5305 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5306 			"\tVFI extents:\n");
5307 	index = 0;
5308 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_vfi_blk_list,
5309 			    list) {
5310 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5311 				"\t\tBlock %3d: Start %4d, Count %4d\n",
5312 				index, rsrc_blks->rsrc_start,
5313 				rsrc_blks->rsrc_size);
5314 		index++;
5315 	}
5316 
5317 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5318 			"\tRPI extents:\n");
5319 	index = 0;
5320 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_rpi_blk_list,
5321 			    list) {
5322 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5323 				"\t\tBlock %3d: Start %4d, Count %4d\n",
5324 				index, rsrc_blks->rsrc_start,
5325 				rsrc_blks->rsrc_size);
5326 		index++;
5327 	}
5328 
5329 	len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5330 			"\tXRI extents:\n");
5331 	index = 0;
5332 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_xri_blk_list,
5333 			    list) {
5334 		len += scnprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
5335 				"\t\tBlock %3d: Start %4d, Count %4d\n",
5336 				index, rsrc_blks->rsrc_start,
5337 				rsrc_blks->rsrc_size);
5338 		index++;
5339 	}
5340 
5341 	return len;
5342 }
5343 
5344 /**
5345  * lpfc_idiag_extacc_write - Syntax check and set up idiag extacc commands
5346  * @file: The file pointer to read from.
5347  * @buf: The buffer to copy the user data from.
5348  * @nbytes: The number of bytes to get.
5349  * @ppos: The position in the file to start reading from.
5350  *
5351  * This routine get the debugfs idiag command struct from user space and then
5352  * perform the syntax check for extent information access commands and sets
5353  * up the necessary states in the idiag command struct accordingly.
5354  *
5355  * It returns the @nbytges passing in from debugfs user space when successful.
5356  * In case of error conditions, it returns proper error code back to the user
5357  * space.
5358  **/
5359 static ssize_t
5360 lpfc_idiag_extacc_write(struct file *file, const char __user *buf,
5361 			size_t nbytes, loff_t *ppos)
5362 {
5363 	struct lpfc_debug *debug = file->private_data;
5364 	uint32_t ext_map;
5365 	int rc;
5366 
5367 	/* This is a user write operation */
5368 	debug->op = LPFC_IDIAG_OP_WR;
5369 
5370 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
5371 	if (rc < 0)
5372 		return rc;
5373 
5374 	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
5375 
5376 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
5377 		goto error_out;
5378 	if (rc != LPFC_EXT_ACC_CMD_ARG)
5379 		goto error_out;
5380 	if (!(ext_map & LPFC_EXT_ACC_ALL))
5381 		goto error_out;
5382 
5383 	return nbytes;
5384 error_out:
5385 	/* Clean out command structure on command error out */
5386 	memset(&idiag, 0, sizeof(idiag));
5387 	return -EINVAL;
5388 }
5389 
5390 /**
5391  * lpfc_idiag_extacc_read - idiag debugfs read access to extent information
5392  * @file: The file pointer to read from.
5393  * @buf: The buffer to copy the data to.
5394  * @nbytes: The number of bytes to read.
5395  * @ppos: The position in the file to start reading from.
5396  *
5397  * Description:
5398  * This routine reads data from the proper extent information according to
5399  * the idiag command, and copies to user @buf.
5400  *
5401  * Returns:
5402  * This function returns the amount of data that was read (this could be less
5403  * than @nbytes if the end of the file was reached) or a negative error value.
5404  **/
5405 static ssize_t
5406 lpfc_idiag_extacc_read(struct file *file, char __user *buf, size_t nbytes,
5407 		       loff_t *ppos)
5408 {
5409 	struct lpfc_debug *debug = file->private_data;
5410 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
5411 	char *pbuffer;
5412 	uint32_t ext_map;
5413 	int len = 0;
5414 
5415 	/* This is a user read operation */
5416 	debug->op = LPFC_IDIAG_OP_RD;
5417 
5418 	if (!debug->buffer)
5419 		debug->buffer = kmalloc(LPFC_EXT_ACC_BUF_SIZE, GFP_KERNEL);
5420 	if (!debug->buffer)
5421 		return 0;
5422 	pbuffer = debug->buffer;
5423 	if (*ppos)
5424 		return 0;
5425 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
5426 		return 0;
5427 
5428 	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
5429 	if (ext_map & LPFC_EXT_ACC_AVAIL)
5430 		len = lpfc_idiag_extacc_avail_get(phba, pbuffer, len);
5431 	if (ext_map & LPFC_EXT_ACC_ALLOC)
5432 		len = lpfc_idiag_extacc_alloc_get(phba, pbuffer, len);
5433 	if (ext_map & LPFC_EXT_ACC_DRIVR)
5434 		len = lpfc_idiag_extacc_drivr_get(phba, pbuffer, len);
5435 
5436 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
5437 }
5438 
5439 #undef lpfc_debugfs_op_disc_trc
5440 static const struct file_operations lpfc_debugfs_op_disc_trc = {
5441 	.owner =        THIS_MODULE,
5442 	.open =         lpfc_debugfs_disc_trc_open,
5443 	.llseek =       lpfc_debugfs_lseek,
5444 	.read =         lpfc_debugfs_read,
5445 	.release =      lpfc_debugfs_release,
5446 };
5447 
5448 #undef lpfc_debugfs_op_nodelist
5449 static const struct file_operations lpfc_debugfs_op_nodelist = {
5450 	.owner =        THIS_MODULE,
5451 	.open =         lpfc_debugfs_nodelist_open,
5452 	.llseek =       lpfc_debugfs_lseek,
5453 	.read =         lpfc_debugfs_read,
5454 	.release =      lpfc_debugfs_release,
5455 };
5456 
5457 #undef lpfc_debugfs_op_multixripools
5458 static const struct file_operations lpfc_debugfs_op_multixripools = {
5459 	.owner =        THIS_MODULE,
5460 	.open =         lpfc_debugfs_multixripools_open,
5461 	.llseek =       lpfc_debugfs_lseek,
5462 	.read =         lpfc_debugfs_read,
5463 	.write =	lpfc_debugfs_multixripools_write,
5464 	.release =      lpfc_debugfs_release,
5465 };
5466 
5467 #undef lpfc_debugfs_op_hbqinfo
5468 static const struct file_operations lpfc_debugfs_op_hbqinfo = {
5469 	.owner =        THIS_MODULE,
5470 	.open =         lpfc_debugfs_hbqinfo_open,
5471 	.llseek =       lpfc_debugfs_lseek,
5472 	.read =         lpfc_debugfs_read,
5473 	.release =      lpfc_debugfs_release,
5474 };
5475 
5476 #ifdef LPFC_HDWQ_LOCK_STAT
5477 #undef lpfc_debugfs_op_lockstat
5478 static const struct file_operations lpfc_debugfs_op_lockstat = {
5479 	.owner =        THIS_MODULE,
5480 	.open =         lpfc_debugfs_lockstat_open,
5481 	.llseek =       lpfc_debugfs_lseek,
5482 	.read =         lpfc_debugfs_read,
5483 	.write =        lpfc_debugfs_lockstat_write,
5484 	.release =      lpfc_debugfs_release,
5485 };
5486 #endif
5487 
5488 #undef lpfc_debugfs_ras_log
5489 static const struct file_operations lpfc_debugfs_ras_log = {
5490 	.owner =        THIS_MODULE,
5491 	.open =         lpfc_debugfs_ras_log_open,
5492 	.llseek =       lpfc_debugfs_lseek,
5493 	.read =         lpfc_debugfs_read,
5494 	.release =      lpfc_debugfs_ras_log_release,
5495 };
5496 
5497 #undef lpfc_debugfs_op_dumpHBASlim
5498 static const struct file_operations lpfc_debugfs_op_dumpHBASlim = {
5499 	.owner =        THIS_MODULE,
5500 	.open =         lpfc_debugfs_dumpHBASlim_open,
5501 	.llseek =       lpfc_debugfs_lseek,
5502 	.read =         lpfc_debugfs_read,
5503 	.release =      lpfc_debugfs_release,
5504 };
5505 
5506 #undef lpfc_debugfs_op_dumpHostSlim
5507 static const struct file_operations lpfc_debugfs_op_dumpHostSlim = {
5508 	.owner =        THIS_MODULE,
5509 	.open =         lpfc_debugfs_dumpHostSlim_open,
5510 	.llseek =       lpfc_debugfs_lseek,
5511 	.read =         lpfc_debugfs_read,
5512 	.release =      lpfc_debugfs_release,
5513 };
5514 
5515 #undef lpfc_debugfs_op_nvmestat
5516 static const struct file_operations lpfc_debugfs_op_nvmestat = {
5517 	.owner =        THIS_MODULE,
5518 	.open =         lpfc_debugfs_nvmestat_open,
5519 	.llseek =       lpfc_debugfs_lseek,
5520 	.read =         lpfc_debugfs_read,
5521 	.write =	lpfc_debugfs_nvmestat_write,
5522 	.release =      lpfc_debugfs_release,
5523 };
5524 
5525 #undef lpfc_debugfs_op_scsistat
5526 static const struct file_operations lpfc_debugfs_op_scsistat = {
5527 	.owner =        THIS_MODULE,
5528 	.open =         lpfc_debugfs_scsistat_open,
5529 	.llseek =       lpfc_debugfs_lseek,
5530 	.read =         lpfc_debugfs_read,
5531 	.write =	lpfc_debugfs_scsistat_write,
5532 	.release =      lpfc_debugfs_release,
5533 };
5534 
5535 #undef lpfc_debugfs_op_ioktime
5536 static const struct file_operations lpfc_debugfs_op_ioktime = {
5537 	.owner =        THIS_MODULE,
5538 	.open =         lpfc_debugfs_ioktime_open,
5539 	.llseek =       lpfc_debugfs_lseek,
5540 	.read =         lpfc_debugfs_read,
5541 	.write =	lpfc_debugfs_ioktime_write,
5542 	.release =      lpfc_debugfs_release,
5543 };
5544 
5545 #undef lpfc_debugfs_op_nvmeio_trc
5546 static const struct file_operations lpfc_debugfs_op_nvmeio_trc = {
5547 	.owner =        THIS_MODULE,
5548 	.open =         lpfc_debugfs_nvmeio_trc_open,
5549 	.llseek =       lpfc_debugfs_lseek,
5550 	.read =         lpfc_debugfs_read,
5551 	.write =	lpfc_debugfs_nvmeio_trc_write,
5552 	.release =      lpfc_debugfs_release,
5553 };
5554 
5555 #undef lpfc_debugfs_op_hdwqstat
5556 static const struct file_operations lpfc_debugfs_op_hdwqstat = {
5557 	.owner =        THIS_MODULE,
5558 	.open =         lpfc_debugfs_hdwqstat_open,
5559 	.llseek =       lpfc_debugfs_lseek,
5560 	.read =         lpfc_debugfs_read,
5561 	.write =	lpfc_debugfs_hdwqstat_write,
5562 	.release =      lpfc_debugfs_release,
5563 };
5564 
5565 #undef lpfc_debugfs_op_dif_err
5566 static const struct file_operations lpfc_debugfs_op_dif_err = {
5567 	.owner =	THIS_MODULE,
5568 	.open =		simple_open,
5569 	.llseek =	lpfc_debugfs_lseek,
5570 	.read =		lpfc_debugfs_dif_err_read,
5571 	.write =	lpfc_debugfs_dif_err_write,
5572 	.release =	lpfc_debugfs_dif_err_release,
5573 };
5574 
5575 #undef lpfc_debugfs_op_slow_ring_trc
5576 static const struct file_operations lpfc_debugfs_op_slow_ring_trc = {
5577 	.owner =        THIS_MODULE,
5578 	.open =         lpfc_debugfs_slow_ring_trc_open,
5579 	.llseek =       lpfc_debugfs_lseek,
5580 	.read =         lpfc_debugfs_read,
5581 	.release =      lpfc_debugfs_release,
5582 };
5583 
5584 static struct dentry *lpfc_debugfs_root = NULL;
5585 static atomic_t lpfc_debugfs_hba_count;
5586 
5587 /*
5588  * File operations for the iDiag debugfs
5589  */
5590 #undef lpfc_idiag_op_pciCfg
5591 static const struct file_operations lpfc_idiag_op_pciCfg = {
5592 	.owner =        THIS_MODULE,
5593 	.open =         lpfc_idiag_open,
5594 	.llseek =       lpfc_debugfs_lseek,
5595 	.read =         lpfc_idiag_pcicfg_read,
5596 	.write =        lpfc_idiag_pcicfg_write,
5597 	.release =      lpfc_idiag_cmd_release,
5598 };
5599 
5600 #undef lpfc_idiag_op_barAcc
5601 static const struct file_operations lpfc_idiag_op_barAcc = {
5602 	.owner =        THIS_MODULE,
5603 	.open =         lpfc_idiag_open,
5604 	.llseek =       lpfc_debugfs_lseek,
5605 	.read =         lpfc_idiag_baracc_read,
5606 	.write =        lpfc_idiag_baracc_write,
5607 	.release =      lpfc_idiag_cmd_release,
5608 };
5609 
5610 #undef lpfc_idiag_op_queInfo
5611 static const struct file_operations lpfc_idiag_op_queInfo = {
5612 	.owner =        THIS_MODULE,
5613 	.open =         lpfc_idiag_open,
5614 	.read =         lpfc_idiag_queinfo_read,
5615 	.release =      lpfc_idiag_release,
5616 };
5617 
5618 #undef lpfc_idiag_op_queAcc
5619 static const struct file_operations lpfc_idiag_op_queAcc = {
5620 	.owner =        THIS_MODULE,
5621 	.open =         lpfc_idiag_open,
5622 	.llseek =       lpfc_debugfs_lseek,
5623 	.read =         lpfc_idiag_queacc_read,
5624 	.write =        lpfc_idiag_queacc_write,
5625 	.release =      lpfc_idiag_cmd_release,
5626 };
5627 
5628 #undef lpfc_idiag_op_drbAcc
5629 static const struct file_operations lpfc_idiag_op_drbAcc = {
5630 	.owner =        THIS_MODULE,
5631 	.open =         lpfc_idiag_open,
5632 	.llseek =       lpfc_debugfs_lseek,
5633 	.read =         lpfc_idiag_drbacc_read,
5634 	.write =        lpfc_idiag_drbacc_write,
5635 	.release =      lpfc_idiag_cmd_release,
5636 };
5637 
5638 #undef lpfc_idiag_op_ctlAcc
5639 static const struct file_operations lpfc_idiag_op_ctlAcc = {
5640 	.owner =        THIS_MODULE,
5641 	.open =         lpfc_idiag_open,
5642 	.llseek =       lpfc_debugfs_lseek,
5643 	.read =         lpfc_idiag_ctlacc_read,
5644 	.write =        lpfc_idiag_ctlacc_write,
5645 	.release =      lpfc_idiag_cmd_release,
5646 };
5647 
5648 #undef lpfc_idiag_op_mbxAcc
5649 static const struct file_operations lpfc_idiag_op_mbxAcc = {
5650 	.owner =        THIS_MODULE,
5651 	.open =         lpfc_idiag_open,
5652 	.llseek =       lpfc_debugfs_lseek,
5653 	.read =         lpfc_idiag_mbxacc_read,
5654 	.write =        lpfc_idiag_mbxacc_write,
5655 	.release =      lpfc_idiag_cmd_release,
5656 };
5657 
5658 #undef lpfc_idiag_op_extAcc
5659 static const struct file_operations lpfc_idiag_op_extAcc = {
5660 	.owner =        THIS_MODULE,
5661 	.open =         lpfc_idiag_open,
5662 	.llseek =       lpfc_debugfs_lseek,
5663 	.read =         lpfc_idiag_extacc_read,
5664 	.write =        lpfc_idiag_extacc_write,
5665 	.release =      lpfc_idiag_cmd_release,
5666 };
5667 #endif
5668 
5669 /* lpfc_idiag_mbxacc_dump_bsg_mbox - idiag debugfs dump bsg mailbox command
5670  * @phba: Pointer to HBA context object.
5671  * @dmabuf: Pointer to a DMA buffer descriptor.
5672  *
5673  * Description:
5674  * This routine dump a bsg pass-through non-embedded mailbox command with
5675  * external buffer.
5676  **/
5677 void
5678 lpfc_idiag_mbxacc_dump_bsg_mbox(struct lpfc_hba *phba, enum nemb_type nemb_tp,
5679 				enum mbox_type mbox_tp, enum dma_type dma_tp,
5680 				enum sta_type sta_tp,
5681 				struct lpfc_dmabuf *dmabuf, uint32_t ext_buf)
5682 {
5683 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5684 	uint32_t *mbx_mbox_cmd, *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt;
5685 	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
5686 	int len = 0;
5687 	uint32_t do_dump = 0;
5688 	uint32_t *pword;
5689 	uint32_t i;
5690 
5691 	if (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP)
5692 		return;
5693 
5694 	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5695 	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5696 	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5697 	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5698 
5699 	if (!(*mbx_dump_map & LPFC_MBX_DMP_ALL) ||
5700 	    (*mbx_dump_cnt == 0) ||
5701 	    (*mbx_word_cnt == 0))
5702 		return;
5703 
5704 	if (*mbx_mbox_cmd != 0x9B)
5705 		return;
5706 
5707 	if ((mbox_tp == mbox_rd) && (dma_tp == dma_mbox)) {
5708 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_MBX) {
5709 			do_dump |= LPFC_BSG_DMP_MBX_RD_MBX;
5710 			pr_err("\nRead mbox command (x%x), "
5711 			       "nemb:0x%x, extbuf_cnt:%d:\n",
5712 			       sta_tp, nemb_tp, ext_buf);
5713 		}
5714 	}
5715 	if ((mbox_tp == mbox_rd) && (dma_tp == dma_ebuf)) {
5716 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_BUF) {
5717 			do_dump |= LPFC_BSG_DMP_MBX_RD_BUF;
5718 			pr_err("\nRead mbox buffer (x%x), "
5719 			       "nemb:0x%x, extbuf_seq:%d:\n",
5720 			       sta_tp, nemb_tp, ext_buf);
5721 		}
5722 	}
5723 	if ((mbox_tp == mbox_wr) && (dma_tp == dma_mbox)) {
5724 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_MBX) {
5725 			do_dump |= LPFC_BSG_DMP_MBX_WR_MBX;
5726 			pr_err("\nWrite mbox command (x%x), "
5727 			       "nemb:0x%x, extbuf_cnt:%d:\n",
5728 			       sta_tp, nemb_tp, ext_buf);
5729 		}
5730 	}
5731 	if ((mbox_tp == mbox_wr) && (dma_tp == dma_ebuf)) {
5732 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_BUF) {
5733 			do_dump |= LPFC_BSG_DMP_MBX_WR_BUF;
5734 			pr_err("\nWrite mbox buffer (x%x), "
5735 			       "nemb:0x%x, extbuf_seq:%d:\n",
5736 			       sta_tp, nemb_tp, ext_buf);
5737 		}
5738 	}
5739 
5740 	/* dump buffer content */
5741 	if (do_dump) {
5742 		pword = (uint32_t *)dmabuf->virt;
5743 		for (i = 0; i < *mbx_word_cnt; i++) {
5744 			if (!(i % 8)) {
5745 				if (i != 0)
5746 					pr_err("%s\n", line_buf);
5747 				len = 0;
5748 				len += scnprintf(line_buf+len,
5749 						LPFC_MBX_ACC_LBUF_SZ-len,
5750 						"%03d: ", i);
5751 			}
5752 			len += scnprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
5753 					"%08x ", (uint32_t)*pword);
5754 			pword++;
5755 		}
5756 		if ((i - 1) % 8)
5757 			pr_err("%s\n", line_buf);
5758 		(*mbx_dump_cnt)--;
5759 	}
5760 
5761 	/* Clean out command structure on reaching dump count */
5762 	if (*mbx_dump_cnt == 0)
5763 		memset(&idiag, 0, sizeof(idiag));
5764 	return;
5765 #endif
5766 }
5767 
5768 /* lpfc_idiag_mbxacc_dump_issue_mbox - idiag debugfs dump issue mailbox command
5769  * @phba: Pointer to HBA context object.
5770  * @dmabuf: Pointer to a DMA buffer descriptor.
5771  *
5772  * Description:
5773  * This routine dump a pass-through non-embedded mailbox command from issue
5774  * mailbox command.
5775  **/
5776 void
5777 lpfc_idiag_mbxacc_dump_issue_mbox(struct lpfc_hba *phba, MAILBOX_t *pmbox)
5778 {
5779 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5780 	uint32_t *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt, *mbx_mbox_cmd;
5781 	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
5782 	int len = 0;
5783 	uint32_t *pword;
5784 	uint8_t *pbyte;
5785 	uint32_t i, j;
5786 
5787 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP)
5788 		return;
5789 
5790 	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5791 	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5792 	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5793 	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5794 
5795 	if (!(*mbx_dump_map & LPFC_MBX_DMP_MBX_ALL) ||
5796 	    (*mbx_dump_cnt == 0) ||
5797 	    (*mbx_word_cnt == 0))
5798 		return;
5799 
5800 	if ((*mbx_mbox_cmd != LPFC_MBX_ALL_CMD) &&
5801 	    (*mbx_mbox_cmd != pmbox->mbxCommand))
5802 		return;
5803 
5804 	/* dump buffer content */
5805 	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_WORD) {
5806 		pr_err("Mailbox command:0x%x dump by word:\n",
5807 		       pmbox->mbxCommand);
5808 		pword = (uint32_t *)pmbox;
5809 		for (i = 0; i < *mbx_word_cnt; i++) {
5810 			if (!(i % 8)) {
5811 				if (i != 0)
5812 					pr_err("%s\n", line_buf);
5813 				len = 0;
5814 				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
5815 				len += scnprintf(line_buf+len,
5816 						LPFC_MBX_ACC_LBUF_SZ-len,
5817 						"%03d: ", i);
5818 			}
5819 			len += scnprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
5820 					"%08x ",
5821 					((uint32_t)*pword) & 0xffffffff);
5822 			pword++;
5823 		}
5824 		if ((i - 1) % 8)
5825 			pr_err("%s\n", line_buf);
5826 		pr_err("\n");
5827 	}
5828 	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_BYTE) {
5829 		pr_err("Mailbox command:0x%x dump by byte:\n",
5830 		       pmbox->mbxCommand);
5831 		pbyte = (uint8_t *)pmbox;
5832 		for (i = 0; i < *mbx_word_cnt; i++) {
5833 			if (!(i % 8)) {
5834 				if (i != 0)
5835 					pr_err("%s\n", line_buf);
5836 				len = 0;
5837 				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
5838 				len += scnprintf(line_buf+len,
5839 						LPFC_MBX_ACC_LBUF_SZ-len,
5840 						"%03d: ", i);
5841 			}
5842 			for (j = 0; j < 4; j++) {
5843 				len += scnprintf(line_buf+len,
5844 						LPFC_MBX_ACC_LBUF_SZ-len,
5845 						"%02x",
5846 						((uint8_t)*pbyte) & 0xff);
5847 				pbyte++;
5848 			}
5849 			len += scnprintf(line_buf+len,
5850 					LPFC_MBX_ACC_LBUF_SZ-len, " ");
5851 		}
5852 		if ((i - 1) % 8)
5853 			pr_err("%s\n", line_buf);
5854 		pr_err("\n");
5855 	}
5856 	(*mbx_dump_cnt)--;
5857 
5858 	/* Clean out command structure on reaching dump count */
5859 	if (*mbx_dump_cnt == 0)
5860 		memset(&idiag, 0, sizeof(idiag));
5861 	return;
5862 #endif
5863 }
5864 
5865 /**
5866  * lpfc_debugfs_initialize - Initialize debugfs for a vport
5867  * @vport: The vport pointer to initialize.
5868  *
5869  * Description:
5870  * When Debugfs is configured this routine sets up the lpfc debugfs file system.
5871  * If not already created, this routine will create the lpfc directory, and
5872  * lpfcX directory (for this HBA), and vportX directory for this vport. It will
5873  * also create each file used to access lpfc specific debugfs information.
5874  **/
5875 inline void
5876 lpfc_debugfs_initialize(struct lpfc_vport *vport)
5877 {
5878 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5879 	struct lpfc_hba   *phba = vport->phba;
5880 	char name[64];
5881 	uint32_t num, i;
5882 	bool pport_setup = false;
5883 
5884 	if (!lpfc_debugfs_enable)
5885 		return;
5886 
5887 	/* Setup lpfc root directory */
5888 	if (!lpfc_debugfs_root) {
5889 		lpfc_debugfs_root = debugfs_create_dir("lpfc", NULL);
5890 		atomic_set(&lpfc_debugfs_hba_count, 0);
5891 	}
5892 	if (!lpfc_debugfs_start_time)
5893 		lpfc_debugfs_start_time = jiffies;
5894 
5895 	/* Setup funcX directory for specific HBA PCI function */
5896 	snprintf(name, sizeof(name), "fn%d", phba->brd_no);
5897 	if (!phba->hba_debugfs_root) {
5898 		pport_setup = true;
5899 		phba->hba_debugfs_root =
5900 			debugfs_create_dir(name, lpfc_debugfs_root);
5901 		atomic_inc(&lpfc_debugfs_hba_count);
5902 		atomic_set(&phba->debugfs_vport_count, 0);
5903 
5904 		/* Multi-XRI pools */
5905 		snprintf(name, sizeof(name), "multixripools");
5906 		phba->debug_multixri_pools =
5907 			debugfs_create_file(name, S_IFREG | 0644,
5908 					    phba->hba_debugfs_root,
5909 					    phba,
5910 					    &lpfc_debugfs_op_multixripools);
5911 		if (!phba->debug_multixri_pools) {
5912 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5913 					 "0527 Cannot create debugfs multixripools\n");
5914 			goto debug_failed;
5915 		}
5916 
5917 		/* RAS log */
5918 		snprintf(name, sizeof(name), "ras_log");
5919 		phba->debug_ras_log =
5920 			debugfs_create_file(name, 0644,
5921 					    phba->hba_debugfs_root,
5922 					    phba, &lpfc_debugfs_ras_log);
5923 		if (!phba->debug_ras_log) {
5924 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5925 					 "6148 Cannot create debugfs"
5926 					 " ras_log\n");
5927 			goto debug_failed;
5928 		}
5929 
5930 		/* Setup hbqinfo */
5931 		snprintf(name, sizeof(name), "hbqinfo");
5932 		phba->debug_hbqinfo =
5933 			debugfs_create_file(name, S_IFREG | 0644,
5934 					    phba->hba_debugfs_root,
5935 					    phba, &lpfc_debugfs_op_hbqinfo);
5936 
5937 #ifdef LPFC_HDWQ_LOCK_STAT
5938 		/* Setup lockstat */
5939 		snprintf(name, sizeof(name), "lockstat");
5940 		phba->debug_lockstat =
5941 			debugfs_create_file(name, S_IFREG | 0644,
5942 					    phba->hba_debugfs_root,
5943 					    phba, &lpfc_debugfs_op_lockstat);
5944 		if (!phba->debug_lockstat) {
5945 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5946 					 "4610 Cant create debugfs lockstat\n");
5947 			goto debug_failed;
5948 		}
5949 #endif
5950 
5951 		/* Setup dumpHBASlim */
5952 		if (phba->sli_rev < LPFC_SLI_REV4) {
5953 			snprintf(name, sizeof(name), "dumpHBASlim");
5954 			phba->debug_dumpHBASlim =
5955 				debugfs_create_file(name,
5956 					S_IFREG|S_IRUGO|S_IWUSR,
5957 					phba->hba_debugfs_root,
5958 					phba, &lpfc_debugfs_op_dumpHBASlim);
5959 		} else
5960 			phba->debug_dumpHBASlim = NULL;
5961 
5962 		/* Setup dumpHostSlim */
5963 		if (phba->sli_rev < LPFC_SLI_REV4) {
5964 			snprintf(name, sizeof(name), "dumpHostSlim");
5965 			phba->debug_dumpHostSlim =
5966 				debugfs_create_file(name,
5967 					S_IFREG|S_IRUGO|S_IWUSR,
5968 					phba->hba_debugfs_root,
5969 					phba, &lpfc_debugfs_op_dumpHostSlim);
5970 		} else
5971 			phba->debug_dumpHostSlim = NULL;
5972 
5973 		/* Setup DIF Error Injections */
5974 		snprintf(name, sizeof(name), "InjErrLBA");
5975 		phba->debug_InjErrLBA =
5976 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5977 			phba->hba_debugfs_root,
5978 			phba, &lpfc_debugfs_op_dif_err);
5979 		phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
5980 
5981 		snprintf(name, sizeof(name), "InjErrNPortID");
5982 		phba->debug_InjErrNPortID =
5983 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5984 			phba->hba_debugfs_root,
5985 			phba, &lpfc_debugfs_op_dif_err);
5986 
5987 		snprintf(name, sizeof(name), "InjErrWWPN");
5988 		phba->debug_InjErrWWPN =
5989 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5990 			phba->hba_debugfs_root,
5991 			phba, &lpfc_debugfs_op_dif_err);
5992 
5993 		snprintf(name, sizeof(name), "writeGuardInjErr");
5994 		phba->debug_writeGuard =
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), "writeAppInjErr");
6000 		phba->debug_writeApp =
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), "writeRefInjErr");
6006 		phba->debug_writeRef =
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), "readGuardInjErr");
6012 		phba->debug_readGuard =
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), "readAppInjErr");
6018 		phba->debug_readApp =
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), "readRefInjErr");
6024 		phba->debug_readRef =
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 		/* Setup slow ring trace */
6030 		if (lpfc_debugfs_max_slow_ring_trc) {
6031 			num = lpfc_debugfs_max_slow_ring_trc - 1;
6032 			if (num & lpfc_debugfs_max_slow_ring_trc) {
6033 				/* Change to be a power of 2 */
6034 				num = lpfc_debugfs_max_slow_ring_trc;
6035 				i = 0;
6036 				while (num > 1) {
6037 					num = num >> 1;
6038 					i++;
6039 				}
6040 				lpfc_debugfs_max_slow_ring_trc = (1 << i);
6041 				pr_err("lpfc_debugfs_max_disc_trc changed to "
6042 				       "%d\n", lpfc_debugfs_max_disc_trc);
6043 			}
6044 		}
6045 
6046 		snprintf(name, sizeof(name), "slow_ring_trace");
6047 		phba->debug_slow_ring_trc =
6048 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6049 				 phba->hba_debugfs_root,
6050 				 phba, &lpfc_debugfs_op_slow_ring_trc);
6051 		if (!phba->slow_ring_trc) {
6052 			phba->slow_ring_trc = kmalloc(
6053 				(sizeof(struct lpfc_debugfs_trc) *
6054 				lpfc_debugfs_max_slow_ring_trc),
6055 				GFP_KERNEL);
6056 			if (!phba->slow_ring_trc) {
6057 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6058 						 "0416 Cannot create debugfs "
6059 						 "slow_ring buffer\n");
6060 				goto debug_failed;
6061 			}
6062 			atomic_set(&phba->slow_ring_trc_cnt, 0);
6063 			memset(phba->slow_ring_trc, 0,
6064 				(sizeof(struct lpfc_debugfs_trc) *
6065 				lpfc_debugfs_max_slow_ring_trc));
6066 		}
6067 
6068 		snprintf(name, sizeof(name), "nvmeio_trc");
6069 		phba->debug_nvmeio_trc =
6070 			debugfs_create_file(name, 0644,
6071 					    phba->hba_debugfs_root,
6072 					    phba, &lpfc_debugfs_op_nvmeio_trc);
6073 
6074 		atomic_set(&phba->nvmeio_trc_cnt, 0);
6075 		if (lpfc_debugfs_max_nvmeio_trc) {
6076 			num = lpfc_debugfs_max_nvmeio_trc - 1;
6077 			if (num & lpfc_debugfs_max_disc_trc) {
6078 				/* Change to be a power of 2 */
6079 				num = lpfc_debugfs_max_nvmeio_trc;
6080 				i = 0;
6081 				while (num > 1) {
6082 					num = num >> 1;
6083 					i++;
6084 				}
6085 				lpfc_debugfs_max_nvmeio_trc = (1 << i);
6086 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6087 						"0575 lpfc_debugfs_max_nvmeio_trc "
6088 						"changed to %d\n",
6089 						lpfc_debugfs_max_nvmeio_trc);
6090 			}
6091 			phba->nvmeio_trc_size = lpfc_debugfs_max_nvmeio_trc;
6092 
6093 			/* Allocate trace buffer and initialize */
6094 			phba->nvmeio_trc = kzalloc(
6095 				(sizeof(struct lpfc_debugfs_nvmeio_trc) *
6096 				phba->nvmeio_trc_size), GFP_KERNEL);
6097 
6098 			if (!phba->nvmeio_trc) {
6099 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6100 						"0576 Cannot create debugfs "
6101 						"nvmeio_trc buffer\n");
6102 				goto nvmeio_off;
6103 			}
6104 			phba->nvmeio_trc_on = 1;
6105 			phba->nvmeio_trc_output_idx = 0;
6106 			phba->nvmeio_trc = NULL;
6107 		} else {
6108 nvmeio_off:
6109 			phba->nvmeio_trc_size = 0;
6110 			phba->nvmeio_trc_on = 0;
6111 			phba->nvmeio_trc_output_idx = 0;
6112 			phba->nvmeio_trc = NULL;
6113 		}
6114 	}
6115 
6116 	snprintf(name, sizeof(name), "vport%d", vport->vpi);
6117 	if (!vport->vport_debugfs_root) {
6118 		vport->vport_debugfs_root =
6119 			debugfs_create_dir(name, phba->hba_debugfs_root);
6120 		atomic_inc(&phba->debugfs_vport_count);
6121 	}
6122 
6123 	if (lpfc_debugfs_max_disc_trc) {
6124 		num = lpfc_debugfs_max_disc_trc - 1;
6125 		if (num & lpfc_debugfs_max_disc_trc) {
6126 			/* Change to be a power of 2 */
6127 			num = lpfc_debugfs_max_disc_trc;
6128 			i = 0;
6129 			while (num > 1) {
6130 				num = num >> 1;
6131 				i++;
6132 			}
6133 			lpfc_debugfs_max_disc_trc = (1 << i);
6134 			pr_err("lpfc_debugfs_max_disc_trc changed to %d\n",
6135 			       lpfc_debugfs_max_disc_trc);
6136 		}
6137 	}
6138 
6139 	vport->disc_trc = kzalloc(
6140 		(sizeof(struct lpfc_debugfs_trc) * lpfc_debugfs_max_disc_trc),
6141 		GFP_KERNEL);
6142 
6143 	if (!vport->disc_trc) {
6144 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6145 				 "0418 Cannot create debugfs disc trace "
6146 				 "buffer\n");
6147 		goto debug_failed;
6148 	}
6149 	atomic_set(&vport->disc_trc_cnt, 0);
6150 
6151 	snprintf(name, sizeof(name), "discovery_trace");
6152 	vport->debug_disc_trc =
6153 		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6154 				 vport->vport_debugfs_root,
6155 				 vport, &lpfc_debugfs_op_disc_trc);
6156 	snprintf(name, sizeof(name), "nodelist");
6157 	vport->debug_nodelist =
6158 		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6159 				 vport->vport_debugfs_root,
6160 				 vport, &lpfc_debugfs_op_nodelist);
6161 
6162 	snprintf(name, sizeof(name), "nvmestat");
6163 	vport->debug_nvmestat =
6164 		debugfs_create_file(name, 0644,
6165 				    vport->vport_debugfs_root,
6166 				    vport, &lpfc_debugfs_op_nvmestat);
6167 
6168 	snprintf(name, sizeof(name), "scsistat");
6169 	vport->debug_scsistat =
6170 		debugfs_create_file(name, 0644,
6171 				    vport->vport_debugfs_root,
6172 				    vport, &lpfc_debugfs_op_scsistat);
6173 	if (!vport->debug_scsistat) {
6174 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6175 				 "4611 Cannot create debugfs scsistat\n");
6176 		goto debug_failed;
6177 	}
6178 
6179 	snprintf(name, sizeof(name), "ioktime");
6180 	vport->debug_ioktime =
6181 		debugfs_create_file(name, 0644,
6182 				    vport->vport_debugfs_root,
6183 				    vport, &lpfc_debugfs_op_ioktime);
6184 	if (!vport->debug_ioktime) {
6185 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
6186 				 "0815 Cannot create debugfs ioktime\n");
6187 		goto debug_failed;
6188 	}
6189 
6190 	snprintf(name, sizeof(name), "hdwqstat");
6191 	vport->debug_hdwqstat =
6192 		debugfs_create_file(name, 0644,
6193 				    vport->vport_debugfs_root,
6194 				    vport, &lpfc_debugfs_op_hdwqstat);
6195 
6196 	/*
6197 	 * The following section is for additional directories/files for the
6198 	 * physical port.
6199 	 */
6200 
6201 	if (!pport_setup)
6202 		goto debug_failed;
6203 
6204 	/*
6205 	 * iDiag debugfs root entry points for SLI4 device only
6206 	 */
6207 	if (phba->sli_rev < LPFC_SLI_REV4)
6208 		goto debug_failed;
6209 
6210 	snprintf(name, sizeof(name), "iDiag");
6211 	if (!phba->idiag_root) {
6212 		phba->idiag_root =
6213 			debugfs_create_dir(name, phba->hba_debugfs_root);
6214 		/* Initialize iDiag data structure */
6215 		memset(&idiag, 0, sizeof(idiag));
6216 	}
6217 
6218 	/* iDiag read PCI config space */
6219 	snprintf(name, sizeof(name), "pciCfg");
6220 	if (!phba->idiag_pci_cfg) {
6221 		phba->idiag_pci_cfg =
6222 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6223 				phba->idiag_root, phba, &lpfc_idiag_op_pciCfg);
6224 		idiag.offset.last_rd = 0;
6225 	}
6226 
6227 	/* iDiag PCI BAR access */
6228 	snprintf(name, sizeof(name), "barAcc");
6229 	if (!phba->idiag_bar_acc) {
6230 		phba->idiag_bar_acc =
6231 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6232 				phba->idiag_root, phba, &lpfc_idiag_op_barAcc);
6233 		idiag.offset.last_rd = 0;
6234 	}
6235 
6236 	/* iDiag get PCI function queue information */
6237 	snprintf(name, sizeof(name), "queInfo");
6238 	if (!phba->idiag_que_info) {
6239 		phba->idiag_que_info =
6240 			debugfs_create_file(name, S_IFREG|S_IRUGO,
6241 			phba->idiag_root, phba, &lpfc_idiag_op_queInfo);
6242 	}
6243 
6244 	/* iDiag access PCI function queue */
6245 	snprintf(name, sizeof(name), "queAcc");
6246 	if (!phba->idiag_que_acc) {
6247 		phba->idiag_que_acc =
6248 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6249 				phba->idiag_root, phba, &lpfc_idiag_op_queAcc);
6250 	}
6251 
6252 	/* iDiag access PCI function doorbell registers */
6253 	snprintf(name, sizeof(name), "drbAcc");
6254 	if (!phba->idiag_drb_acc) {
6255 		phba->idiag_drb_acc =
6256 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6257 				phba->idiag_root, phba, &lpfc_idiag_op_drbAcc);
6258 	}
6259 
6260 	/* iDiag access PCI function control registers */
6261 	snprintf(name, sizeof(name), "ctlAcc");
6262 	if (!phba->idiag_ctl_acc) {
6263 		phba->idiag_ctl_acc =
6264 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6265 				phba->idiag_root, phba, &lpfc_idiag_op_ctlAcc);
6266 	}
6267 
6268 	/* iDiag access mbox commands */
6269 	snprintf(name, sizeof(name), "mbxAcc");
6270 	if (!phba->idiag_mbx_acc) {
6271 		phba->idiag_mbx_acc =
6272 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
6273 				phba->idiag_root, phba, &lpfc_idiag_op_mbxAcc);
6274 	}
6275 
6276 	/* iDiag extents access commands */
6277 	if (phba->sli4_hba.extents_in_use) {
6278 		snprintf(name, sizeof(name), "extAcc");
6279 		if (!phba->idiag_ext_acc) {
6280 			phba->idiag_ext_acc =
6281 				debugfs_create_file(name,
6282 						    S_IFREG|S_IRUGO|S_IWUSR,
6283 						    phba->idiag_root, phba,
6284 						    &lpfc_idiag_op_extAcc);
6285 		}
6286 	}
6287 
6288 debug_failed:
6289 	return;
6290 #endif
6291 }
6292 
6293 /**
6294  * lpfc_debugfs_terminate -  Tear down debugfs infrastructure for this vport
6295  * @vport: The vport pointer to remove from debugfs.
6296  *
6297  * Description:
6298  * When Debugfs is configured this routine removes debugfs file system elements
6299  * that are specific to this vport. It also checks to see if there are any
6300  * users left for the debugfs directories associated with the HBA and driver. If
6301  * this is the last user of the HBA directory or driver directory then it will
6302  * remove those from the debugfs infrastructure as well.
6303  **/
6304 inline void
6305 lpfc_debugfs_terminate(struct lpfc_vport *vport)
6306 {
6307 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
6308 	struct lpfc_hba   *phba = vport->phba;
6309 
6310 	kfree(vport->disc_trc);
6311 	vport->disc_trc = NULL;
6312 
6313 	debugfs_remove(vport->debug_disc_trc); /* discovery_trace */
6314 	vport->debug_disc_trc = NULL;
6315 
6316 	debugfs_remove(vport->debug_nodelist); /* nodelist */
6317 	vport->debug_nodelist = NULL;
6318 
6319 	debugfs_remove(vport->debug_nvmestat); /* nvmestat */
6320 	vport->debug_nvmestat = NULL;
6321 
6322 	debugfs_remove(vport->debug_scsistat); /* scsistat */
6323 	vport->debug_scsistat = NULL;
6324 
6325 	debugfs_remove(vport->debug_ioktime); /* ioktime */
6326 	vport->debug_ioktime = NULL;
6327 
6328 	debugfs_remove(vport->debug_hdwqstat); /* hdwqstat */
6329 	vport->debug_hdwqstat = NULL;
6330 
6331 	if (vport->vport_debugfs_root) {
6332 		debugfs_remove(vport->vport_debugfs_root); /* vportX */
6333 		vport->vport_debugfs_root = NULL;
6334 		atomic_dec(&phba->debugfs_vport_count);
6335 	}
6336 
6337 	if (atomic_read(&phba->debugfs_vport_count) == 0) {
6338 
6339 		debugfs_remove(phba->debug_multixri_pools); /* multixripools*/
6340 		phba->debug_multixri_pools = NULL;
6341 
6342 		debugfs_remove(phba->debug_hbqinfo); /* hbqinfo */
6343 		phba->debug_hbqinfo = NULL;
6344 
6345 		debugfs_remove(phba->debug_ras_log);
6346 		phba->debug_ras_log = NULL;
6347 
6348 #ifdef LPFC_HDWQ_LOCK_STAT
6349 		debugfs_remove(phba->debug_lockstat); /* lockstat */
6350 		phba->debug_lockstat = NULL;
6351 #endif
6352 		debugfs_remove(phba->debug_dumpHBASlim); /* HBASlim */
6353 		phba->debug_dumpHBASlim = NULL;
6354 
6355 		debugfs_remove(phba->debug_dumpHostSlim); /* HostSlim */
6356 		phba->debug_dumpHostSlim = NULL;
6357 
6358 		debugfs_remove(phba->debug_InjErrLBA); /* InjErrLBA */
6359 		phba->debug_InjErrLBA = NULL;
6360 
6361 		debugfs_remove(phba->debug_InjErrNPortID);
6362 		phba->debug_InjErrNPortID = NULL;
6363 
6364 		debugfs_remove(phba->debug_InjErrWWPN); /* InjErrWWPN */
6365 		phba->debug_InjErrWWPN = NULL;
6366 
6367 		debugfs_remove(phba->debug_writeGuard); /* writeGuard */
6368 		phba->debug_writeGuard = NULL;
6369 
6370 		debugfs_remove(phba->debug_writeApp); /* writeApp */
6371 		phba->debug_writeApp = NULL;
6372 
6373 		debugfs_remove(phba->debug_writeRef); /* writeRef */
6374 		phba->debug_writeRef = NULL;
6375 
6376 		debugfs_remove(phba->debug_readGuard); /* readGuard */
6377 		phba->debug_readGuard = NULL;
6378 
6379 		debugfs_remove(phba->debug_readApp); /* readApp */
6380 		phba->debug_readApp = NULL;
6381 
6382 		debugfs_remove(phba->debug_readRef); /* readRef */
6383 		phba->debug_readRef = NULL;
6384 
6385 		kfree(phba->slow_ring_trc);
6386 		phba->slow_ring_trc = NULL;
6387 
6388 		/* slow_ring_trace */
6389 		debugfs_remove(phba->debug_slow_ring_trc);
6390 		phba->debug_slow_ring_trc = NULL;
6391 
6392 		debugfs_remove(phba->debug_nvmeio_trc);
6393 		phba->debug_nvmeio_trc = NULL;
6394 
6395 		kfree(phba->nvmeio_trc);
6396 		phba->nvmeio_trc = NULL;
6397 
6398 		/*
6399 		 * iDiag release
6400 		 */
6401 		if (phba->sli_rev == LPFC_SLI_REV4) {
6402 			/* iDiag extAcc */
6403 			debugfs_remove(phba->idiag_ext_acc);
6404 			phba->idiag_ext_acc = NULL;
6405 
6406 			/* iDiag mbxAcc */
6407 			debugfs_remove(phba->idiag_mbx_acc);
6408 			phba->idiag_mbx_acc = NULL;
6409 
6410 			/* iDiag ctlAcc */
6411 			debugfs_remove(phba->idiag_ctl_acc);
6412 			phba->idiag_ctl_acc = NULL;
6413 
6414 			/* iDiag drbAcc */
6415 			debugfs_remove(phba->idiag_drb_acc);
6416 			phba->idiag_drb_acc = NULL;
6417 
6418 			/* iDiag queAcc */
6419 			debugfs_remove(phba->idiag_que_acc);
6420 			phba->idiag_que_acc = NULL;
6421 
6422 			/* iDiag queInfo */
6423 			debugfs_remove(phba->idiag_que_info);
6424 			phba->idiag_que_info = NULL;
6425 
6426 			/* iDiag barAcc */
6427 			debugfs_remove(phba->idiag_bar_acc);
6428 			phba->idiag_bar_acc = NULL;
6429 
6430 			/* iDiag pciCfg */
6431 			debugfs_remove(phba->idiag_pci_cfg);
6432 			phba->idiag_pci_cfg = NULL;
6433 
6434 			/* Finally remove the iDiag debugfs root */
6435 			debugfs_remove(phba->idiag_root);
6436 			phba->idiag_root = NULL;
6437 		}
6438 
6439 		if (phba->hba_debugfs_root) {
6440 			debugfs_remove(phba->hba_debugfs_root); /* fnX */
6441 			phba->hba_debugfs_root = NULL;
6442 			atomic_dec(&lpfc_debugfs_hba_count);
6443 		}
6444 
6445 		if (atomic_read(&lpfc_debugfs_hba_count) == 0) {
6446 			debugfs_remove(lpfc_debugfs_root); /* lpfc */
6447 			lpfc_debugfs_root = NULL;
6448 		}
6449 	}
6450 #endif
6451 	return;
6452 }
6453 
6454 /*
6455  * Driver debug utility routines outside of debugfs. The debug utility
6456  * routines implemented here is intended to be used in the instrumented
6457  * debug driver for debugging host or port issues.
6458  */
6459 
6460 /**
6461  * lpfc_debug_dump_all_queues - dump all the queues with a hba
6462  * @phba: Pointer to HBA context object.
6463  *
6464  * This function dumps entries of all the queues asociated with the @phba.
6465  **/
6466 void
6467 lpfc_debug_dump_all_queues(struct lpfc_hba *phba)
6468 {
6469 	int idx;
6470 
6471 	/*
6472 	 * Dump Work Queues (WQs)
6473 	 */
6474 	lpfc_debug_dump_wq(phba, DUMP_MBX, 0);
6475 	lpfc_debug_dump_wq(phba, DUMP_ELS, 0);
6476 	lpfc_debug_dump_wq(phba, DUMP_NVMELS, 0);
6477 
6478 	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6479 		lpfc_debug_dump_wq(phba, DUMP_IO, idx);
6480 
6481 	lpfc_debug_dump_hdr_rq(phba);
6482 	lpfc_debug_dump_dat_rq(phba);
6483 	/*
6484 	 * Dump Complete Queues (CQs)
6485 	 */
6486 	lpfc_debug_dump_cq(phba, DUMP_MBX, 0);
6487 	lpfc_debug_dump_cq(phba, DUMP_ELS, 0);
6488 	lpfc_debug_dump_cq(phba, DUMP_NVMELS, 0);
6489 
6490 	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6491 		lpfc_debug_dump_cq(phba, DUMP_IO, idx);
6492 
6493 	/*
6494 	 * Dump Event Queues (EQs)
6495 	 */
6496 	for (idx = 0; idx < phba->cfg_hdw_queue; idx++)
6497 		lpfc_debug_dump_hba_eq(phba, idx);
6498 }
6499