xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_debugfs.c (revision 7aacf86b)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017 Broadcom. All Rights Reserved. The term      *
5  * “Broadcom” refers to Broadcom Limited and/or its subsidiaries.  *
6  * Copyright (C) 2007-2015 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  *                                                                 *
10  * This program is free software; you can redistribute it and/or   *
11  * modify it under the terms of version 2 of the GNU General       *
12  * Public License as published by the Free Software Foundation.    *
13  * This program is distributed in the hope that it will be useful. *
14  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
15  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
16  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
17  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
18  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
19  * more details, a copy of which can be found in the file COPYING  *
20  * included with this package.                                     *
21  *******************************************************************/
22 
23 #include <linux/blkdev.h>
24 #include <linux/delay.h>
25 #include <linux/module.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/idr.h>
28 #include <linux/interrupt.h>
29 #include <linux/kthread.h>
30 #include <linux/slab.h>
31 #include <linux/pci.h>
32 #include <linux/spinlock.h>
33 #include <linux/ctype.h>
34 
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_device.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_transport_fc.h>
39 #include <scsi/fc/fc_fs.h>
40 
41 #include <linux/nvme-fc-driver.h>
42 
43 #include "lpfc_hw4.h"
44 #include "lpfc_hw.h"
45 #include "lpfc_sli.h"
46 #include "lpfc_sli4.h"
47 #include "lpfc_nl.h"
48 #include "lpfc_disc.h"
49 #include "lpfc.h"
50 #include "lpfc_scsi.h"
51 #include "lpfc_nvme.h"
52 #include "lpfc_nvmet.h"
53 #include "lpfc_logmsg.h"
54 #include "lpfc_crtn.h"
55 #include "lpfc_vport.h"
56 #include "lpfc_version.h"
57 #include "lpfc_compat.h"
58 #include "lpfc_debugfs.h"
59 #include "lpfc_bsg.h"
60 
61 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
62 /*
63  * debugfs interface
64  *
65  * To access this interface the user should:
66  * # mount -t debugfs none /sys/kernel/debug
67  *
68  * The lpfc debugfs directory hierarchy is:
69  * /sys/kernel/debug/lpfc/fnX/vportY
70  * where X is the lpfc hba function unique_id
71  * where Y is the vport VPI on that hba
72  *
73  * Debugging services available per vport:
74  * discovery_trace
75  * This is an ACSII readable file that contains a trace of the last
76  * lpfc_debugfs_max_disc_trc events that happened on a specific vport.
77  * See lpfc_debugfs.h for different categories of  discovery events.
78  * To enable the discovery trace, the following module parameters must be set:
79  * lpfc_debugfs_enable=1         Turns on lpfc debugfs filesystem support
80  * lpfc_debugfs_max_disc_trc=X   Where X is the event trace depth for
81  *                               EACH vport. X MUST also be a power of 2.
82  * lpfc_debugfs_mask_disc_trc=Y  Where Y is an event mask as defined in
83  *                               lpfc_debugfs.h .
84  *
85  * slow_ring_trace
86  * This is an ACSII readable file that contains a trace of the last
87  * lpfc_debugfs_max_slow_ring_trc events that happened on a specific HBA.
88  * To enable the slow ring trace, the following module parameters must be set:
89  * lpfc_debugfs_enable=1         Turns on lpfc debugfs filesystem support
90  * lpfc_debugfs_max_slow_ring_trc=X   Where X is the event trace depth for
91  *                               the HBA. X MUST also be a power of 2.
92  */
93 static int lpfc_debugfs_enable = 1;
94 module_param(lpfc_debugfs_enable, int, S_IRUGO);
95 MODULE_PARM_DESC(lpfc_debugfs_enable, "Enable debugfs services");
96 
97 /* This MUST be a power of 2 */
98 static int lpfc_debugfs_max_disc_trc;
99 module_param(lpfc_debugfs_max_disc_trc, int, S_IRUGO);
100 MODULE_PARM_DESC(lpfc_debugfs_max_disc_trc,
101 	"Set debugfs discovery trace depth");
102 
103 /* This MUST be a power of 2 */
104 static int lpfc_debugfs_max_slow_ring_trc;
105 module_param(lpfc_debugfs_max_slow_ring_trc, int, S_IRUGO);
106 MODULE_PARM_DESC(lpfc_debugfs_max_slow_ring_trc,
107 	"Set debugfs slow ring trace depth");
108 
109 /* This MUST be a power of 2 */
110 static int lpfc_debugfs_max_nvmeio_trc;
111 module_param(lpfc_debugfs_max_nvmeio_trc, int, 0444);
112 MODULE_PARM_DESC(lpfc_debugfs_max_nvmeio_trc,
113 		 "Set debugfs NVME IO trace depth");
114 
115 static int lpfc_debugfs_mask_disc_trc;
116 module_param(lpfc_debugfs_mask_disc_trc, int, S_IRUGO);
117 MODULE_PARM_DESC(lpfc_debugfs_mask_disc_trc,
118 	"Set debugfs discovery trace mask");
119 
120 #include <linux/debugfs.h>
121 
122 static atomic_t lpfc_debugfs_seq_trc_cnt = ATOMIC_INIT(0);
123 static unsigned long lpfc_debugfs_start_time = 0L;
124 
125 /* iDiag */
126 static struct lpfc_idiag idiag;
127 
128 /**
129  * lpfc_debugfs_disc_trc_data - Dump discovery logging to a buffer
130  * @vport: The vport to gather the log info from.
131  * @buf: The buffer to dump log into.
132  * @size: The maximum amount of data to process.
133  *
134  * Description:
135  * This routine gathers the lpfc discovery debugfs data from the @vport and
136  * dumps it to @buf up to @size number of bytes. It will start at the next entry
137  * in the log and process the log until the end of the buffer. Then it will
138  * gather from the beginning of the log and process until the current entry.
139  *
140  * Notes:
141  * Discovery logging will be disabled while while this routine dumps the log.
142  *
143  * Return Value:
144  * This routine returns the amount of bytes that were dumped into @buf and will
145  * not exceed @size.
146  **/
147 static int
148 lpfc_debugfs_disc_trc_data(struct lpfc_vport *vport, char *buf, int size)
149 {
150 	int i, index, len, enable;
151 	uint32_t ms;
152 	struct lpfc_debugfs_trc *dtp;
153 	char *buffer;
154 
155 	buffer = kmalloc(LPFC_DEBUG_TRC_ENTRY_SIZE, GFP_KERNEL);
156 	if (!buffer)
157 		return 0;
158 
159 	enable = lpfc_debugfs_enable;
160 	lpfc_debugfs_enable = 0;
161 
162 	len = 0;
163 	index = (atomic_read(&vport->disc_trc_cnt) + 1) &
164 		(lpfc_debugfs_max_disc_trc - 1);
165 	for (i = index; i < lpfc_debugfs_max_disc_trc; i++) {
166 		dtp = vport->disc_trc + i;
167 		if (!dtp->fmt)
168 			continue;
169 		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
170 		snprintf(buffer,
171 			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
172 			dtp->seq_cnt, ms, dtp->fmt);
173 		len +=  snprintf(buf+len, size-len, buffer,
174 			dtp->data1, dtp->data2, dtp->data3);
175 	}
176 	for (i = 0; i < index; i++) {
177 		dtp = vport->disc_trc + i;
178 		if (!dtp->fmt)
179 			continue;
180 		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
181 		snprintf(buffer,
182 			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
183 			dtp->seq_cnt, ms, dtp->fmt);
184 		len +=  snprintf(buf+len, size-len, buffer,
185 			dtp->data1, dtp->data2, dtp->data3);
186 	}
187 
188 	lpfc_debugfs_enable = enable;
189 	kfree(buffer);
190 
191 	return len;
192 }
193 
194 /**
195  * lpfc_debugfs_slow_ring_trc_data - Dump slow ring logging to a buffer
196  * @phba: The HBA to gather the log info from.
197  * @buf: The buffer to dump log into.
198  * @size: The maximum amount of data to process.
199  *
200  * Description:
201  * This routine gathers the lpfc slow ring debugfs data from the @phba and
202  * dumps it to @buf up to @size number of bytes. It will start at the next entry
203  * in the log and process the log until the end of the buffer. Then it will
204  * gather from the beginning of the log and process until the current entry.
205  *
206  * Notes:
207  * Slow ring logging will be disabled while while this routine dumps the log.
208  *
209  * Return Value:
210  * This routine returns the amount of bytes that were dumped into @buf and will
211  * not exceed @size.
212  **/
213 static int
214 lpfc_debugfs_slow_ring_trc_data(struct lpfc_hba *phba, char *buf, int size)
215 {
216 	int i, index, len, enable;
217 	uint32_t ms;
218 	struct lpfc_debugfs_trc *dtp;
219 	char *buffer;
220 
221 	buffer = kmalloc(LPFC_DEBUG_TRC_ENTRY_SIZE, GFP_KERNEL);
222 	if (!buffer)
223 		return 0;
224 
225 	enable = lpfc_debugfs_enable;
226 	lpfc_debugfs_enable = 0;
227 
228 	len = 0;
229 	index = (atomic_read(&phba->slow_ring_trc_cnt) + 1) &
230 		(lpfc_debugfs_max_slow_ring_trc - 1);
231 	for (i = index; i < lpfc_debugfs_max_slow_ring_trc; i++) {
232 		dtp = phba->slow_ring_trc + i;
233 		if (!dtp->fmt)
234 			continue;
235 		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
236 		snprintf(buffer,
237 			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
238 			dtp->seq_cnt, ms, dtp->fmt);
239 		len +=  snprintf(buf+len, size-len, buffer,
240 			dtp->data1, dtp->data2, dtp->data3);
241 	}
242 	for (i = 0; i < index; i++) {
243 		dtp = phba->slow_ring_trc + i;
244 		if (!dtp->fmt)
245 			continue;
246 		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
247 		snprintf(buffer,
248 			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
249 			dtp->seq_cnt, ms, dtp->fmt);
250 		len +=  snprintf(buf+len, size-len, buffer,
251 			dtp->data1, dtp->data2, dtp->data3);
252 	}
253 
254 	lpfc_debugfs_enable = enable;
255 	kfree(buffer);
256 
257 	return len;
258 }
259 
260 static int lpfc_debugfs_last_hbq = -1;
261 
262 /**
263  * lpfc_debugfs_hbqinfo_data - Dump host buffer queue info to a buffer
264  * @phba: The HBA to gather host buffer info from.
265  * @buf: The buffer to dump log into.
266  * @size: The maximum amount of data to process.
267  *
268  * Description:
269  * This routine dumps the host buffer queue info from the @phba to @buf up to
270  * @size number of bytes. A header that describes the current hbq state will be
271  * dumped to @buf first and then info on each hbq entry will be dumped to @buf
272  * until @size bytes have been dumped or all the hbq info has been dumped.
273  *
274  * Notes:
275  * This routine will rotate through each configured HBQ each time called.
276  *
277  * Return Value:
278  * This routine returns the amount of bytes that were dumped into @buf and will
279  * not exceed @size.
280  **/
281 static int
282 lpfc_debugfs_hbqinfo_data(struct lpfc_hba *phba, char *buf, int size)
283 {
284 	int len = 0;
285 	int i, j, found, posted, low;
286 	uint32_t phys, raw_index, getidx;
287 	struct lpfc_hbq_init *hip;
288 	struct hbq_s *hbqs;
289 	struct lpfc_hbq_entry *hbqe;
290 	struct lpfc_dmabuf *d_buf;
291 	struct hbq_dmabuf *hbq_buf;
292 
293 	if (phba->sli_rev != 3)
294 		return 0;
295 
296 	spin_lock_irq(&phba->hbalock);
297 
298 	/* toggle between multiple hbqs, if any */
299 	i = lpfc_sli_hbq_count();
300 	if (i > 1) {
301 		 lpfc_debugfs_last_hbq++;
302 		 if (lpfc_debugfs_last_hbq >= i)
303 			lpfc_debugfs_last_hbq = 0;
304 	}
305 	else
306 		lpfc_debugfs_last_hbq = 0;
307 
308 	i = lpfc_debugfs_last_hbq;
309 
310 	len +=  snprintf(buf+len, size-len, "HBQ %d Info\n", i);
311 
312 	hbqs =  &phba->hbqs[i];
313 	posted = 0;
314 	list_for_each_entry(d_buf, &hbqs->hbq_buffer_list, list)
315 		posted++;
316 
317 	hip =  lpfc_hbq_defs[i];
318 	len +=  snprintf(buf+len, size-len,
319 		"idx:%d prof:%d rn:%d bufcnt:%d icnt:%d acnt:%d posted %d\n",
320 		hip->hbq_index, hip->profile, hip->rn,
321 		hip->buffer_count, hip->init_count, hip->add_count, posted);
322 
323 	raw_index = phba->hbq_get[i];
324 	getidx = le32_to_cpu(raw_index);
325 	len +=  snprintf(buf+len, size-len,
326 		"entries:%d bufcnt:%d Put:%d nPut:%d localGet:%d hbaGet:%d\n",
327 		hbqs->entry_count, hbqs->buffer_count, hbqs->hbqPutIdx,
328 		hbqs->next_hbqPutIdx, hbqs->local_hbqGetIdx, getidx);
329 
330 	hbqe = (struct lpfc_hbq_entry *) phba->hbqs[i].hbq_virt;
331 	for (j=0; j<hbqs->entry_count; j++) {
332 		len +=  snprintf(buf+len, size-len,
333 			"%03d: %08x %04x %05x ", j,
334 			le32_to_cpu(hbqe->bde.addrLow),
335 			le32_to_cpu(hbqe->bde.tus.w),
336 			le32_to_cpu(hbqe->buffer_tag));
337 		i = 0;
338 		found = 0;
339 
340 		/* First calculate if slot has an associated posted buffer */
341 		low = hbqs->hbqPutIdx - posted;
342 		if (low >= 0) {
343 			if ((j >= hbqs->hbqPutIdx) || (j < low)) {
344 				len +=  snprintf(buf+len, size-len, "Unused\n");
345 				goto skipit;
346 			}
347 		}
348 		else {
349 			if ((j >= hbqs->hbqPutIdx) &&
350 				(j < (hbqs->entry_count+low))) {
351 				len +=  snprintf(buf+len, size-len, "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 +=  snprintf(buf+len, size-len,
362 					"Buf%d: %p %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 +=  snprintf(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_hba_slim_off;
382 
383 /**
384  * lpfc_debugfs_dumpHBASlim_data - Dump HBA SLIM info to a buffer
385  * @phba: The HBA to gather SLIM 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 current contents of HBA SLIM for the HBA associated
391  * with @phba to @buf up to @size bytes of data. This is the raw HBA SLIM data.
392  *
393  * Notes:
394  * This routine will only dump up to 1024 bytes of data each time called and
395  * should be called multiple times to dump the entire HBA SLIM.
396  *
397  * Return Value:
398  * This routine returns the amount of bytes that were dumped into @buf and will
399  * not exceed @size.
400  **/
401 static int
402 lpfc_debugfs_dumpHBASlim_data(struct lpfc_hba *phba, char *buf, int size)
403 {
404 	int len = 0;
405 	int i, off;
406 	uint32_t *ptr;
407 	char *buffer;
408 
409 	buffer = kmalloc(1024, GFP_KERNEL);
410 	if (!buffer)
411 		return 0;
412 
413 	off = 0;
414 	spin_lock_irq(&phba->hbalock);
415 
416 	len +=  snprintf(buf+len, size-len, "HBA SLIM\n");
417 	lpfc_memcpy_from_slim(buffer,
418 		phba->MBslimaddr + lpfc_debugfs_last_hba_slim_off, 1024);
419 
420 	ptr = (uint32_t *)&buffer[0];
421 	off = lpfc_debugfs_last_hba_slim_off;
422 
423 	/* Set it up for the next time */
424 	lpfc_debugfs_last_hba_slim_off += 1024;
425 	if (lpfc_debugfs_last_hba_slim_off >= 4096)
426 		lpfc_debugfs_last_hba_slim_off = 0;
427 
428 	i = 1024;
429 	while (i > 0) {
430 		len +=  snprintf(buf+len, size-len,
431 		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
432 		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
433 		*(ptr+5), *(ptr+6), *(ptr+7));
434 		ptr += 8;
435 		i -= (8 * sizeof(uint32_t));
436 		off += (8 * sizeof(uint32_t));
437 	}
438 
439 	spin_unlock_irq(&phba->hbalock);
440 	kfree(buffer);
441 
442 	return len;
443 }
444 
445 /**
446  * lpfc_debugfs_dumpHostSlim_data - Dump host SLIM info to a buffer
447  * @phba: The HBA to gather Host SLIM info from.
448  * @buf: The buffer to dump log into.
449  * @size: The maximum amount of data to process.
450  *
451  * Description:
452  * This routine dumps the current contents of host SLIM for the host associated
453  * with @phba to @buf up to @size bytes of data. The dump will contain the
454  * Mailbox, PCB, Rings, and Registers that are located in host memory.
455  *
456  * Return Value:
457  * This routine returns the amount of bytes that were dumped into @buf and will
458  * not exceed @size.
459  **/
460 static int
461 lpfc_debugfs_dumpHostSlim_data(struct lpfc_hba *phba, char *buf, int size)
462 {
463 	int len = 0;
464 	int i, off;
465 	uint32_t word0, word1, word2, word3;
466 	uint32_t *ptr;
467 	struct lpfc_pgp *pgpp;
468 	struct lpfc_sli *psli = &phba->sli;
469 	struct lpfc_sli_ring *pring;
470 
471 	off = 0;
472 	spin_lock_irq(&phba->hbalock);
473 
474 	len +=  snprintf(buf+len, size-len, "SLIM Mailbox\n");
475 	ptr = (uint32_t *)phba->slim2p.virt;
476 	i = sizeof(MAILBOX_t);
477 	while (i > 0) {
478 		len +=  snprintf(buf+len, size-len,
479 		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
480 		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
481 		*(ptr+5), *(ptr+6), *(ptr+7));
482 		ptr += 8;
483 		i -= (8 * sizeof(uint32_t));
484 		off += (8 * sizeof(uint32_t));
485 	}
486 
487 	len +=  snprintf(buf+len, size-len, "SLIM PCB\n");
488 	ptr = (uint32_t *)phba->pcb;
489 	i = sizeof(PCB_t);
490 	while (i > 0) {
491 		len +=  snprintf(buf+len, size-len,
492 		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
493 		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
494 		*(ptr+5), *(ptr+6), *(ptr+7));
495 		ptr += 8;
496 		i -= (8 * sizeof(uint32_t));
497 		off += (8 * sizeof(uint32_t));
498 	}
499 
500 	if (phba->sli_rev <= LPFC_SLI_REV3) {
501 		for (i = 0; i < 4; i++) {
502 			pgpp = &phba->port_gp[i];
503 			pring = &psli->sli3_ring[i];
504 			len +=  snprintf(buf+len, size-len,
505 					 "Ring %d: CMD GetInx:%d "
506 					 "(Max:%d Next:%d "
507 					 "Local:%d flg:x%x)  "
508 					 "RSP PutInx:%d Max:%d\n",
509 					 i, pgpp->cmdGetInx,
510 					 pring->sli.sli3.numCiocb,
511 					 pring->sli.sli3.next_cmdidx,
512 					 pring->sli.sli3.local_getidx,
513 					 pring->flag, pgpp->rspPutInx,
514 					 pring->sli.sli3.numRiocb);
515 		}
516 
517 		word0 = readl(phba->HAregaddr);
518 		word1 = readl(phba->CAregaddr);
519 		word2 = readl(phba->HSregaddr);
520 		word3 = readl(phba->HCregaddr);
521 		len +=  snprintf(buf+len, size-len, "HA:%08x CA:%08x HS:%08x "
522 				 "HC:%08x\n", word0, word1, word2, word3);
523 	}
524 	spin_unlock_irq(&phba->hbalock);
525 	return len;
526 }
527 
528 /**
529  * lpfc_debugfs_nodelist_data - Dump target node list to a buffer
530  * @vport: The vport to gather target node info from.
531  * @buf: The buffer to dump log into.
532  * @size: The maximum amount of data to process.
533  *
534  * Description:
535  * This routine dumps the current target node list associated with @vport to
536  * @buf up to @size bytes of data. Each node entry in the dump will contain a
537  * node state, DID, WWPN, WWNN, RPI, flags, type, and other useful fields.
538  *
539  * Return Value:
540  * This routine returns the amount of bytes that were dumped into @buf and will
541  * not exceed @size.
542  **/
543 static int
544 lpfc_debugfs_nodelist_data(struct lpfc_vport *vport, char *buf, int size)
545 {
546 	int len = 0;
547 	int cnt;
548 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
549 	struct lpfc_hba  *phba = vport->phba;
550 	struct lpfc_nodelist *ndlp;
551 	unsigned char *statep;
552 	struct nvme_fc_local_port *localport;
553 	struct lpfc_nvmet_tgtport *tgtp;
554 	struct nvme_fc_remote_port *nrport;
555 
556 	cnt = (LPFC_NODELIST_SIZE / LPFC_NODELIST_ENTRY_SIZE);
557 
558 	len += snprintf(buf+len, size-len, "\nFCP Nodelist Entries ...\n");
559 	spin_lock_irq(shost->host_lock);
560 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
561 		if (!cnt) {
562 			len +=  snprintf(buf+len, size-len,
563 				"Missing Nodelist Entries\n");
564 			break;
565 		}
566 		cnt--;
567 		switch (ndlp->nlp_state) {
568 		case NLP_STE_UNUSED_NODE:
569 			statep = "UNUSED";
570 			break;
571 		case NLP_STE_PLOGI_ISSUE:
572 			statep = "PLOGI ";
573 			break;
574 		case NLP_STE_ADISC_ISSUE:
575 			statep = "ADISC ";
576 			break;
577 		case NLP_STE_REG_LOGIN_ISSUE:
578 			statep = "REGLOG";
579 			break;
580 		case NLP_STE_PRLI_ISSUE:
581 			statep = "PRLI  ";
582 			break;
583 		case NLP_STE_LOGO_ISSUE:
584 			statep = "LOGO  ";
585 			break;
586 		case NLP_STE_UNMAPPED_NODE:
587 			statep = "UNMAP ";
588 			break;
589 		case NLP_STE_MAPPED_NODE:
590 			statep = "MAPPED";
591 			break;
592 		case NLP_STE_NPR_NODE:
593 			statep = "NPR   ";
594 			break;
595 		default:
596 			statep = "UNKNOWN";
597 		}
598 		len += snprintf(buf+len, size-len, "%s DID:x%06x ",
599 				statep, ndlp->nlp_DID);
600 		len += snprintf(buf+len, size-len,
601 				"WWPN x%llx ",
602 				wwn_to_u64(ndlp->nlp_portname.u.wwn));
603 		len += snprintf(buf+len, size-len,
604 				"WWNN x%llx ",
605 				wwn_to_u64(ndlp->nlp_nodename.u.wwn));
606 		if (ndlp->nlp_flag & NLP_RPI_REGISTERED)
607 			len += snprintf(buf+len, size-len, "RPI:%03d ",
608 					ndlp->nlp_rpi);
609 		else
610 			len += snprintf(buf+len, size-len, "RPI:none ");
611 		len +=  snprintf(buf+len, size-len, "flag:x%08x ",
612 			ndlp->nlp_flag);
613 		if (!ndlp->nlp_type)
614 			len += snprintf(buf+len, size-len, "UNKNOWN_TYPE ");
615 		if (ndlp->nlp_type & NLP_FC_NODE)
616 			len += snprintf(buf+len, size-len, "FC_NODE ");
617 		if (ndlp->nlp_type & NLP_FABRIC)
618 			len += snprintf(buf+len, size-len, "FABRIC ");
619 		if (ndlp->nlp_type & NLP_FCP_TARGET)
620 			len += snprintf(buf+len, size-len, "FCP_TGT sid:%d ",
621 				ndlp->nlp_sid);
622 		if (ndlp->nlp_type & NLP_FCP_INITIATOR)
623 			len += snprintf(buf+len, size-len, "FCP_INITIATOR ");
624 		if (ndlp->nlp_type & NLP_NVME_TARGET)
625 			len += snprintf(buf + len,
626 					size - len, "NVME_TGT sid:%d ",
627 					NLP_NO_SID);
628 		if (ndlp->nlp_type & NLP_NVME_INITIATOR)
629 			len += snprintf(buf + len,
630 					size - len, "NVME_INITIATOR ");
631 		len += snprintf(buf+len, size-len, "usgmap:%x ",
632 			ndlp->nlp_usg_map);
633 		len += snprintf(buf+len, size-len, "refcnt:%x",
634 			kref_read(&ndlp->kref));
635 		len +=  snprintf(buf+len, size-len, "\n");
636 	}
637 	spin_unlock_irq(shost->host_lock);
638 
639 	if (phba->nvmet_support && phba->targetport && (vport == phba->pport)) {
640 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
641 		len += snprintf(buf + len, size - len,
642 				"\nNVME Targetport Entry ...\n");
643 
644 		/* Port state is only one of two values for now. */
645 		if (phba->targetport->port_id)
646 			statep = "REGISTERED";
647 		else
648 			statep = "INIT";
649 		len += snprintf(buf + len, size - len,
650 				"TGT WWNN x%llx WWPN x%llx State %s\n",
651 				wwn_to_u64(vport->fc_nodename.u.wwn),
652 				wwn_to_u64(vport->fc_portname.u.wwn),
653 				statep);
654 		len += snprintf(buf + len, size - len,
655 				"    Targetport DID x%06x\n",
656 				phba->targetport->port_id);
657 		goto out_exit;
658 	}
659 
660 	len += snprintf(buf + len, size - len,
661 				"\nNVME Lport/Rport Entries ...\n");
662 
663 	localport = vport->localport;
664 	if (!localport)
665 		goto out_exit;
666 
667 	spin_lock_irq(shost->host_lock);
668 
669 	/* Port state is only one of two values for now. */
670 	if (localport->port_id)
671 		statep = "ONLINE";
672 	else
673 		statep = "UNKNOWN ";
674 
675 	len += snprintf(buf + len, size - len,
676 			"Lport DID x%06x PortState %s\n",
677 			localport->port_id, statep);
678 
679 	len += snprintf(buf + len, size - len, "\tRport List:\n");
680 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
681 		/* local short-hand pointer. */
682 		if (!ndlp->nrport)
683 			continue;
684 
685 		nrport = ndlp->nrport->remoteport;
686 
687 		/* Port state is only one of two values for now. */
688 		switch (nrport->port_state) {
689 		case FC_OBJSTATE_ONLINE:
690 			statep = "ONLINE";
691 			break;
692 		case FC_OBJSTATE_UNKNOWN:
693 			statep = "UNKNOWN ";
694 			break;
695 		default:
696 			statep = "UNSUPPORTED";
697 			break;
698 		}
699 
700 		/* Tab in to show lport ownership. */
701 		len += snprintf(buf + len, size - len,
702 				"\t%s Port ID:x%06x ",
703 				statep, nrport->port_id);
704 		len += snprintf(buf + len, size - len, "WWPN x%llx ",
705 				nrport->port_name);
706 		len += snprintf(buf + len, size - len, "WWNN x%llx ",
707 				nrport->node_name);
708 
709 		/* An NVME rport can have multiple roles. */
710 		if (nrport->port_role & FC_PORT_ROLE_NVME_INITIATOR)
711 			len +=  snprintf(buf + len, size - len,
712 					 "INITIATOR ");
713 		if (nrport->port_role & FC_PORT_ROLE_NVME_TARGET)
714 			len +=  snprintf(buf + len, size - len,
715 					 "TARGET ");
716 		if (nrport->port_role & FC_PORT_ROLE_NVME_DISCOVERY)
717 			len +=  snprintf(buf + len, size - len,
718 					 "DISCSRVC ");
719 		if (nrport->port_role & ~(FC_PORT_ROLE_NVME_INITIATOR |
720 					  FC_PORT_ROLE_NVME_TARGET |
721 					  FC_PORT_ROLE_NVME_DISCOVERY))
722 			len +=  snprintf(buf + len, size - len,
723 					 "UNKNOWN ROLE x%x",
724 					 nrport->port_role);
725 		/* Terminate the string. */
726 		len +=  snprintf(buf + len, size - len, "\n");
727 	}
728 
729 	spin_unlock_irq(shost->host_lock);
730  out_exit:
731 	return len;
732 }
733 
734 /**
735  * lpfc_debugfs_nvmestat_data - Dump target node list to a buffer
736  * @vport: The vport to gather target node info from.
737  * @buf: The buffer to dump log into.
738  * @size: The maximum amount of data to process.
739  *
740  * Description:
741  * This routine dumps the NVME statistics associated with @vport
742  *
743  * Return Value:
744  * This routine returns the amount of bytes that were dumped into @buf and will
745  * not exceed @size.
746  **/
747 static int
748 lpfc_debugfs_nvmestat_data(struct lpfc_vport *vport, char *buf, int size)
749 {
750 	struct lpfc_hba   *phba = vport->phba;
751 	struct lpfc_nvmet_tgtport *tgtp;
752 	struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
753 	uint64_t tot, data1, data2, data3;
754 	int len = 0;
755 	int cnt;
756 
757 	if (phba->nvmet_support) {
758 		if (!phba->targetport)
759 			return len;
760 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
761 		len += snprintf(buf + len, size - len,
762 				"\nNVME Targetport Statistics\n");
763 
764 		len += snprintf(buf + len, size - len,
765 				"LS: Rcv %08x Drop %08x Abort %08x\n",
766 				atomic_read(&tgtp->rcv_ls_req_in),
767 				atomic_read(&tgtp->rcv_ls_req_drop),
768 				atomic_read(&tgtp->xmt_ls_abort));
769 		if (atomic_read(&tgtp->rcv_ls_req_in) !=
770 		    atomic_read(&tgtp->rcv_ls_req_out)) {
771 			len += snprintf(buf + len, size - len,
772 					"Rcv LS: in %08x != out %08x\n",
773 					atomic_read(&tgtp->rcv_ls_req_in),
774 					atomic_read(&tgtp->rcv_ls_req_out));
775 		}
776 
777 		len += snprintf(buf + len, size - len,
778 				"LS: Xmt %08x Drop %08x Cmpl %08x Err %08x\n",
779 				atomic_read(&tgtp->xmt_ls_rsp),
780 				atomic_read(&tgtp->xmt_ls_drop),
781 				atomic_read(&tgtp->xmt_ls_rsp_cmpl),
782 				atomic_read(&tgtp->xmt_ls_rsp_error));
783 
784 		len += snprintf(buf + len, size - len,
785 				"FCP: Rcv %08x Defer %08x Release %08x "
786 				"Drop %08x\n",
787 				atomic_read(&tgtp->rcv_fcp_cmd_in),
788 				atomic_read(&tgtp->rcv_fcp_cmd_defer),
789 				atomic_read(&tgtp->xmt_fcp_release),
790 				atomic_read(&tgtp->rcv_fcp_cmd_drop));
791 
792 		if (atomic_read(&tgtp->rcv_fcp_cmd_in) !=
793 		    atomic_read(&tgtp->rcv_fcp_cmd_out)) {
794 			len += snprintf(buf + len, size - len,
795 					"Rcv FCP: in %08x != out %08x\n",
796 					atomic_read(&tgtp->rcv_fcp_cmd_in),
797 					atomic_read(&tgtp->rcv_fcp_cmd_out));
798 		}
799 
800 		len += snprintf(buf + len, size - len,
801 				"FCP Rsp: read %08x readrsp %08x "
802 				"write %08x rsp %08x\n",
803 				atomic_read(&tgtp->xmt_fcp_read),
804 				atomic_read(&tgtp->xmt_fcp_read_rsp),
805 				atomic_read(&tgtp->xmt_fcp_write),
806 				atomic_read(&tgtp->xmt_fcp_rsp));
807 
808 		len += snprintf(buf + len, size - len,
809 				"FCP Rsp Cmpl: %08x err %08x drop %08x\n",
810 				atomic_read(&tgtp->xmt_fcp_rsp_cmpl),
811 				atomic_read(&tgtp->xmt_fcp_rsp_error),
812 				atomic_read(&tgtp->xmt_fcp_rsp_drop));
813 
814 		len += snprintf(buf + len, size - len,
815 				"ABORT: Xmt %08x Cmpl %08x\n",
816 				atomic_read(&tgtp->xmt_fcp_abort),
817 				atomic_read(&tgtp->xmt_fcp_abort_cmpl));
818 
819 		len += snprintf(buf + len, size - len,
820 				"ABORT: Sol %08x  Usol %08x Err %08x Cmpl %08x",
821 				atomic_read(&tgtp->xmt_abort_sol),
822 				atomic_read(&tgtp->xmt_abort_unsol),
823 				atomic_read(&tgtp->xmt_abort_rsp),
824 				atomic_read(&tgtp->xmt_abort_rsp_error));
825 
826 		len +=  snprintf(buf + len, size - len, "\n");
827 
828 		cnt = 0;
829 		spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
830 		list_for_each_entry_safe(ctxp, next_ctxp,
831 				&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
832 				list) {
833 			cnt++;
834 		}
835 		spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
836 		if (cnt) {
837 			len += snprintf(buf + len, size - len,
838 					"ABORT: %d ctx entries\n", cnt);
839 			spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
840 			list_for_each_entry_safe(ctxp, next_ctxp,
841 				    &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
842 				    list) {
843 				if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ))
844 					break;
845 				len += snprintf(buf + len, size - len,
846 						"Entry: oxid %x state %x "
847 						"flag %x\n",
848 						ctxp->oxid, ctxp->state,
849 						ctxp->flag);
850 			}
851 			spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
852 		}
853 
854 		spin_lock(&phba->sli4_hba.nvmet_ctx_get_lock);
855 		spin_lock(&phba->sli4_hba.nvmet_ctx_put_lock);
856 		tot = phba->sli4_hba.nvmet_xri_cnt -
857 			(phba->sli4_hba.nvmet_ctx_get_cnt +
858 			phba->sli4_hba.nvmet_ctx_put_cnt);
859 		spin_unlock(&phba->sli4_hba.nvmet_ctx_put_lock);
860 		spin_unlock(&phba->sli4_hba.nvmet_ctx_get_lock);
861 
862 		len += snprintf(buf + len, size - len,
863 				"IO_CTX: %08x  WAIT: cur %08x tot %08x\n"
864 				"CTX Outstanding %08llx\n",
865 				phba->sli4_hba.nvmet_xri_cnt,
866 				phba->sli4_hba.nvmet_io_wait_cnt,
867 				phba->sli4_hba.nvmet_io_wait_total,
868 				tot);
869 	} else {
870 		if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
871 			return len;
872 
873 		len += snprintf(buf + len, size - len,
874 				"\nNVME Lport Statistics\n");
875 
876 		len += snprintf(buf + len, size - len,
877 				"LS: Xmt %016x Cmpl %016x\n",
878 				atomic_read(&phba->fc4NvmeLsRequests),
879 				atomic_read(&phba->fc4NvmeLsCmpls));
880 
881 		tot = atomic_read(&phba->fc4NvmeIoCmpls);
882 		data1 = atomic_read(&phba->fc4NvmeInputRequests);
883 		data2 = atomic_read(&phba->fc4NvmeOutputRequests);
884 		data3 = atomic_read(&phba->fc4NvmeControlRequests);
885 
886 		len += snprintf(buf + len, size - len,
887 				"FCP: Rd %016llx Wr %016llx IO %016llx\n",
888 				data1, data2, data3);
889 
890 		len += snprintf(buf + len, size - len,
891 				"    Cmpl %016llx Outstanding %016llx\n",
892 				tot, (data1 + data2 + data3) - tot);
893 	}
894 
895 	return len;
896 }
897 
898 
899 /**
900  * lpfc_debugfs_nvmektime_data - Dump target node list to a buffer
901  * @vport: The vport to gather target node info from.
902  * @buf: The buffer to dump log into.
903  * @size: The maximum amount of data to process.
904  *
905  * Description:
906  * This routine dumps the NVME statistics associated with @vport
907  *
908  * Return Value:
909  * This routine returns the amount of bytes that were dumped into @buf and will
910  * not exceed @size.
911  **/
912 static int
913 lpfc_debugfs_nvmektime_data(struct lpfc_vport *vport, char *buf, int size)
914 {
915 	struct lpfc_hba   *phba = vport->phba;
916 	int len = 0;
917 
918 	if (phba->nvmet_support == 0) {
919 		/* NVME Initiator */
920 		len += snprintf(buf + len, PAGE_SIZE - len,
921 				"ktime %s: Total Samples: %lld\n",
922 				(phba->ktime_on ?  "Enabled" : "Disabled"),
923 				phba->ktime_data_samples);
924 		if (phba->ktime_data_samples == 0)
925 			return len;
926 
927 		len += snprintf(
928 			buf + len, PAGE_SIZE - len,
929 			"Segment 1: Last NVME Cmd cmpl "
930 			"done -to- Start of next NVME cnd (in driver)\n");
931 		len += snprintf(
932 			buf + len, PAGE_SIZE - len,
933 			"avg:%08lld min:%08lld max %08lld\n",
934 			div_u64(phba->ktime_seg1_total,
935 				phba->ktime_data_samples),
936 			phba->ktime_seg1_min,
937 			phba->ktime_seg1_max);
938 		len += snprintf(
939 			buf + len, PAGE_SIZE - len,
940 			"Segment 2: Driver start of NVME cmd "
941 			"-to- Firmware WQ doorbell\n");
942 		len += snprintf(
943 			buf + len, PAGE_SIZE - len,
944 			"avg:%08lld min:%08lld max %08lld\n",
945 			div_u64(phba->ktime_seg2_total,
946 				phba->ktime_data_samples),
947 			phba->ktime_seg2_min,
948 			phba->ktime_seg2_max);
949 		len += snprintf(
950 			buf + len, PAGE_SIZE - len,
951 			"Segment 3: Firmware WQ doorbell -to- "
952 			"MSI-X ISR cmpl\n");
953 		len += snprintf(
954 			buf + len, PAGE_SIZE - len,
955 			"avg:%08lld min:%08lld max %08lld\n",
956 			div_u64(phba->ktime_seg3_total,
957 				phba->ktime_data_samples),
958 			phba->ktime_seg3_min,
959 			phba->ktime_seg3_max);
960 		len += snprintf(
961 			buf + len, PAGE_SIZE - len,
962 			"Segment 4: MSI-X ISR cmpl -to- "
963 			"NVME cmpl done\n");
964 		len += snprintf(
965 			buf + len, PAGE_SIZE - len,
966 			"avg:%08lld min:%08lld max %08lld\n",
967 			div_u64(phba->ktime_seg4_total,
968 				phba->ktime_data_samples),
969 			phba->ktime_seg4_min,
970 			phba->ktime_seg4_max);
971 		len += snprintf(
972 			buf + len, PAGE_SIZE - len,
973 			"Total IO avg time: %08lld\n",
974 			div_u64(phba->ktime_seg1_total +
975 			phba->ktime_seg2_total  +
976 			phba->ktime_seg3_total +
977 			phba->ktime_seg4_total,
978 			phba->ktime_data_samples));
979 		return len;
980 	}
981 
982 	/* NVME Target */
983 	len += snprintf(buf + len, PAGE_SIZE-len,
984 			"ktime %s: Total Samples: %lld %lld\n",
985 			(phba->ktime_on ? "Enabled" : "Disabled"),
986 			phba->ktime_data_samples,
987 			phba->ktime_status_samples);
988 	if (phba->ktime_data_samples == 0)
989 		return len;
990 
991 	len += snprintf(buf + len, PAGE_SIZE-len,
992 			"Segment 1: MSI-X ISR Rcv cmd -to- "
993 			"cmd pass to NVME Layer\n");
994 	len += snprintf(buf + len, PAGE_SIZE-len,
995 			"avg:%08lld min:%08lld max %08lld\n",
996 			div_u64(phba->ktime_seg1_total,
997 				phba->ktime_data_samples),
998 			phba->ktime_seg1_min,
999 			phba->ktime_seg1_max);
1000 	len += snprintf(buf + len, PAGE_SIZE-len,
1001 			"Segment 2: cmd pass to NVME Layer- "
1002 			"-to- Driver rcv cmd OP (action)\n");
1003 	len += snprintf(buf + len, PAGE_SIZE-len,
1004 			"avg:%08lld min:%08lld max %08lld\n",
1005 			div_u64(phba->ktime_seg2_total,
1006 				phba->ktime_data_samples),
1007 			phba->ktime_seg2_min,
1008 			phba->ktime_seg2_max);
1009 	len += snprintf(buf + len, PAGE_SIZE-len,
1010 			"Segment 3: Driver rcv cmd OP -to- "
1011 			"Firmware WQ doorbell: cmd\n");
1012 	len += snprintf(buf + len, PAGE_SIZE-len,
1013 			"avg:%08lld min:%08lld max %08lld\n",
1014 			div_u64(phba->ktime_seg3_total,
1015 				phba->ktime_data_samples),
1016 			phba->ktime_seg3_min,
1017 			phba->ktime_seg3_max);
1018 	len += snprintf(buf + len, PAGE_SIZE-len,
1019 			"Segment 4: Firmware WQ doorbell: cmd "
1020 			"-to- MSI-X ISR for cmd cmpl\n");
1021 	len += snprintf(buf + len, PAGE_SIZE-len,
1022 			"avg:%08lld min:%08lld max %08lld\n",
1023 			div_u64(phba->ktime_seg4_total,
1024 				phba->ktime_data_samples),
1025 			phba->ktime_seg4_min,
1026 			phba->ktime_seg4_max);
1027 	len += snprintf(buf + len, PAGE_SIZE-len,
1028 			"Segment 5: MSI-X ISR for cmd cmpl "
1029 			"-to- NVME layer passed cmd done\n");
1030 	len += snprintf(buf + len, PAGE_SIZE-len,
1031 			"avg:%08lld min:%08lld max %08lld\n",
1032 			div_u64(phba->ktime_seg5_total,
1033 				phba->ktime_data_samples),
1034 			phba->ktime_seg5_min,
1035 			phba->ktime_seg5_max);
1036 
1037 	if (phba->ktime_status_samples == 0) {
1038 		len += snprintf(buf + len, PAGE_SIZE-len,
1039 				"Total: cmd received by MSI-X ISR "
1040 				"-to- cmd completed on wire\n");
1041 		len += snprintf(buf + len, PAGE_SIZE-len,
1042 				"avg:%08lld min:%08lld "
1043 				"max %08lld\n",
1044 				div_u64(phba->ktime_seg10_total,
1045 					phba->ktime_data_samples),
1046 				phba->ktime_seg10_min,
1047 				phba->ktime_seg10_max);
1048 		return len;
1049 	}
1050 
1051 	len += snprintf(buf + len, PAGE_SIZE-len,
1052 			"Segment 6: NVME layer passed cmd done "
1053 			"-to- Driver rcv rsp status OP\n");
1054 	len += snprintf(buf + len, PAGE_SIZE-len,
1055 			"avg:%08lld min:%08lld max %08lld\n",
1056 			div_u64(phba->ktime_seg6_total,
1057 				phba->ktime_status_samples),
1058 			phba->ktime_seg6_min,
1059 			phba->ktime_seg6_max);
1060 	len += snprintf(buf + len, PAGE_SIZE-len,
1061 			"Segment 7: Driver rcv rsp status OP "
1062 			"-to- Firmware WQ doorbell: status\n");
1063 	len += snprintf(buf + len, PAGE_SIZE-len,
1064 			"avg:%08lld min:%08lld max %08lld\n",
1065 			div_u64(phba->ktime_seg7_total,
1066 				phba->ktime_status_samples),
1067 			phba->ktime_seg7_min,
1068 			phba->ktime_seg7_max);
1069 	len += snprintf(buf + len, PAGE_SIZE-len,
1070 			"Segment 8: Firmware WQ doorbell: status"
1071 			" -to- MSI-X ISR for status cmpl\n");
1072 	len += snprintf(buf + len, PAGE_SIZE-len,
1073 			"avg:%08lld min:%08lld max %08lld\n",
1074 			div_u64(phba->ktime_seg8_total,
1075 				phba->ktime_status_samples),
1076 			phba->ktime_seg8_min,
1077 			phba->ktime_seg8_max);
1078 	len += snprintf(buf + len, PAGE_SIZE-len,
1079 			"Segment 9: MSI-X ISR for status cmpl  "
1080 			"-to- NVME layer passed status done\n");
1081 	len += snprintf(buf + len, PAGE_SIZE-len,
1082 			"avg:%08lld min:%08lld max %08lld\n",
1083 			div_u64(phba->ktime_seg9_total,
1084 				phba->ktime_status_samples),
1085 			phba->ktime_seg9_min,
1086 			phba->ktime_seg9_max);
1087 	len += snprintf(buf + len, PAGE_SIZE-len,
1088 			"Total: cmd received by MSI-X ISR -to- "
1089 			"cmd completed on wire\n");
1090 	len += snprintf(buf + len, PAGE_SIZE-len,
1091 			"avg:%08lld min:%08lld max %08lld\n",
1092 			div_u64(phba->ktime_seg10_total,
1093 				phba->ktime_status_samples),
1094 			phba->ktime_seg10_min,
1095 			phba->ktime_seg10_max);
1096 	return len;
1097 }
1098 
1099 /**
1100  * lpfc_debugfs_nvmeio_trc_data - Dump NVME IO trace list to a buffer
1101  * @phba: The phba to gather target node info from.
1102  * @buf: The buffer to dump log into.
1103  * @size: The maximum amount of data to process.
1104  *
1105  * Description:
1106  * This routine dumps the NVME IO trace associated with @phba
1107  *
1108  * Return Value:
1109  * This routine returns the amount of bytes that were dumped into @buf and will
1110  * not exceed @size.
1111  **/
1112 static int
1113 lpfc_debugfs_nvmeio_trc_data(struct lpfc_hba *phba, char *buf, int size)
1114 {
1115 	struct lpfc_debugfs_nvmeio_trc *dtp;
1116 	int i, state, index, skip;
1117 	int len = 0;
1118 
1119 	state = phba->nvmeio_trc_on;
1120 
1121 	index = (atomic_read(&phba->nvmeio_trc_cnt) + 1) &
1122 		(phba->nvmeio_trc_size - 1);
1123 	skip = phba->nvmeio_trc_output_idx;
1124 
1125 	len += snprintf(buf + len, size - len,
1126 			"%s IO Trace %s: next_idx %d skip %d size %d\n",
1127 			(phba->nvmet_support ? "NVME" : "NVMET"),
1128 			(state ? "Enabled" : "Disabled"),
1129 			index, skip, phba->nvmeio_trc_size);
1130 
1131 	if (!phba->nvmeio_trc || state)
1132 		return len;
1133 
1134 	/* trace MUST bhe off to continue */
1135 
1136 	for (i = index; i < phba->nvmeio_trc_size; i++) {
1137 		if (skip) {
1138 			skip--;
1139 			continue;
1140 		}
1141 		dtp = phba->nvmeio_trc + i;
1142 		phba->nvmeio_trc_output_idx++;
1143 
1144 		if (!dtp->fmt)
1145 			continue;
1146 
1147 		len +=  snprintf(buf + len, size - len, dtp->fmt,
1148 			dtp->data1, dtp->data2, dtp->data3);
1149 
1150 		if (phba->nvmeio_trc_output_idx >= phba->nvmeio_trc_size) {
1151 			phba->nvmeio_trc_output_idx = 0;
1152 			len += snprintf(buf + len, size - len,
1153 					"Trace Complete\n");
1154 			goto out;
1155 		}
1156 
1157 		if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ)) {
1158 			len += snprintf(buf + len, size - len,
1159 					"Trace Continue (%d of %d)\n",
1160 					phba->nvmeio_trc_output_idx,
1161 					phba->nvmeio_trc_size);
1162 			goto out;
1163 		}
1164 	}
1165 	for (i = 0; i < index; i++) {
1166 		if (skip) {
1167 			skip--;
1168 			continue;
1169 		}
1170 		dtp = phba->nvmeio_trc + i;
1171 		phba->nvmeio_trc_output_idx++;
1172 
1173 		if (!dtp->fmt)
1174 			continue;
1175 
1176 		len +=  snprintf(buf + len, size - len, dtp->fmt,
1177 			dtp->data1, dtp->data2, dtp->data3);
1178 
1179 		if (phba->nvmeio_trc_output_idx >= phba->nvmeio_trc_size) {
1180 			phba->nvmeio_trc_output_idx = 0;
1181 			len += snprintf(buf + len, size - len,
1182 					"Trace Complete\n");
1183 			goto out;
1184 		}
1185 
1186 		if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ)) {
1187 			len += snprintf(buf + len, size - len,
1188 					"Trace Continue (%d of %d)\n",
1189 					phba->nvmeio_trc_output_idx,
1190 					phba->nvmeio_trc_size);
1191 			goto out;
1192 		}
1193 	}
1194 
1195 	len += snprintf(buf + len, size - len,
1196 			"Trace Done\n");
1197 out:
1198 	return len;
1199 }
1200 
1201 /**
1202  * lpfc_debugfs_cpucheck_data - Dump target node list to a buffer
1203  * @vport: The vport to gather target node info from.
1204  * @buf: The buffer to dump log into.
1205  * @size: The maximum amount of data to process.
1206  *
1207  * Description:
1208  * This routine dumps the NVME statistics associated with @vport
1209  *
1210  * Return Value:
1211  * This routine returns the amount of bytes that were dumped into @buf and will
1212  * not exceed @size.
1213  **/
1214 static int
1215 lpfc_debugfs_cpucheck_data(struct lpfc_vport *vport, char *buf, int size)
1216 {
1217 	struct lpfc_hba   *phba = vport->phba;
1218 	int i;
1219 	int len = 0;
1220 	uint32_t tot_xmt = 0;
1221 	uint32_t tot_rcv = 0;
1222 	uint32_t tot_cmpl = 0;
1223 	uint32_t tot_ccmpl = 0;
1224 
1225 	if (phba->nvmet_support == 0) {
1226 		/* NVME Initiator */
1227 		len += snprintf(buf + len, PAGE_SIZE - len,
1228 				"CPUcheck %s\n",
1229 				(phba->cpucheck_on & LPFC_CHECK_NVME_IO ?
1230 					"Enabled" : "Disabled"));
1231 		for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
1232 			if (i >= LPFC_CHECK_CPU_CNT)
1233 				break;
1234 			len += snprintf(buf + len, PAGE_SIZE - len,
1235 					"%02d: xmit x%08x cmpl x%08x\n",
1236 					i, phba->cpucheck_xmt_io[i],
1237 					phba->cpucheck_cmpl_io[i]);
1238 			tot_xmt += phba->cpucheck_xmt_io[i];
1239 			tot_cmpl += phba->cpucheck_cmpl_io[i];
1240 		}
1241 		len += snprintf(buf + len, PAGE_SIZE - len,
1242 				"tot:xmit x%08x cmpl x%08x\n",
1243 				tot_xmt, tot_cmpl);
1244 		return len;
1245 	}
1246 
1247 	/* NVME Target */
1248 	len += snprintf(buf + len, PAGE_SIZE - len,
1249 			"CPUcheck %s ",
1250 			(phba->cpucheck_on & LPFC_CHECK_NVMET_IO ?
1251 				"IO Enabled - " : "IO Disabled - "));
1252 	len += snprintf(buf + len, PAGE_SIZE - len,
1253 			"%s\n",
1254 			(phba->cpucheck_on & LPFC_CHECK_NVMET_RCV ?
1255 				"Rcv Enabled\n" : "Rcv Disabled\n"));
1256 	for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
1257 		if (i >= LPFC_CHECK_CPU_CNT)
1258 			break;
1259 		len += snprintf(buf + len, PAGE_SIZE - len,
1260 				"%02d: xmit x%08x ccmpl x%08x "
1261 				"cmpl x%08x rcv x%08x\n",
1262 				i, phba->cpucheck_xmt_io[i],
1263 				phba->cpucheck_ccmpl_io[i],
1264 				phba->cpucheck_cmpl_io[i],
1265 				phba->cpucheck_rcv_io[i]);
1266 		tot_xmt += phba->cpucheck_xmt_io[i];
1267 		tot_rcv += phba->cpucheck_rcv_io[i];
1268 		tot_cmpl += phba->cpucheck_cmpl_io[i];
1269 		tot_ccmpl += phba->cpucheck_ccmpl_io[i];
1270 	}
1271 	len += snprintf(buf + len, PAGE_SIZE - len,
1272 			"tot:xmit x%08x ccmpl x%08x cmpl x%08x rcv x%08x\n",
1273 			tot_xmt, tot_ccmpl, tot_cmpl, tot_rcv);
1274 	return len;
1275 }
1276 
1277 #endif
1278 
1279 /**
1280  * lpfc_debugfs_disc_trc - Store discovery trace log
1281  * @vport: The vport to associate this trace string with for retrieval.
1282  * @mask: Log entry classification.
1283  * @fmt: Format string to be displayed when dumping the log.
1284  * @data1: 1st data parameter to be applied to @fmt.
1285  * @data2: 2nd data parameter to be applied to @fmt.
1286  * @data3: 3rd data parameter to be applied to @fmt.
1287  *
1288  * Description:
1289  * This routine is used by the driver code to add a debugfs log entry to the
1290  * discovery trace buffer associated with @vport. Only entries with a @mask that
1291  * match the current debugfs discovery mask will be saved. Entries that do not
1292  * match will be thrown away. @fmt, @data1, @data2, and @data3 are used like
1293  * printf when displaying the log.
1294  **/
1295 inline void
1296 lpfc_debugfs_disc_trc(struct lpfc_vport *vport, int mask, char *fmt,
1297 	uint32_t data1, uint32_t data2, uint32_t data3)
1298 {
1299 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1300 	struct lpfc_debugfs_trc *dtp;
1301 	int index;
1302 
1303 	if (!(lpfc_debugfs_mask_disc_trc & mask))
1304 		return;
1305 
1306 	if (!lpfc_debugfs_enable || !lpfc_debugfs_max_disc_trc ||
1307 		!vport || !vport->disc_trc)
1308 		return;
1309 
1310 	index = atomic_inc_return(&vport->disc_trc_cnt) &
1311 		(lpfc_debugfs_max_disc_trc - 1);
1312 	dtp = vport->disc_trc + index;
1313 	dtp->fmt = fmt;
1314 	dtp->data1 = data1;
1315 	dtp->data2 = data2;
1316 	dtp->data3 = data3;
1317 	dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_seq_trc_cnt);
1318 	dtp->jif = jiffies;
1319 #endif
1320 	return;
1321 }
1322 
1323 /**
1324  * lpfc_debugfs_slow_ring_trc - Store slow ring trace log
1325  * @phba: The phba to associate this trace string with for retrieval.
1326  * @fmt: Format string to be displayed when dumping the log.
1327  * @data1: 1st data parameter to be applied to @fmt.
1328  * @data2: 2nd data parameter to be applied to @fmt.
1329  * @data3: 3rd data parameter to be applied to @fmt.
1330  *
1331  * Description:
1332  * This routine is used by the driver code to add a debugfs log entry to the
1333  * discovery trace buffer associated with @vport. @fmt, @data1, @data2, and
1334  * @data3 are used like printf when displaying the log.
1335  **/
1336 inline void
1337 lpfc_debugfs_slow_ring_trc(struct lpfc_hba *phba, char *fmt,
1338 	uint32_t data1, uint32_t data2, uint32_t data3)
1339 {
1340 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1341 	struct lpfc_debugfs_trc *dtp;
1342 	int index;
1343 
1344 	if (!lpfc_debugfs_enable || !lpfc_debugfs_max_slow_ring_trc ||
1345 		!phba || !phba->slow_ring_trc)
1346 		return;
1347 
1348 	index = atomic_inc_return(&phba->slow_ring_trc_cnt) &
1349 		(lpfc_debugfs_max_slow_ring_trc - 1);
1350 	dtp = phba->slow_ring_trc + index;
1351 	dtp->fmt = fmt;
1352 	dtp->data1 = data1;
1353 	dtp->data2 = data2;
1354 	dtp->data3 = data3;
1355 	dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_seq_trc_cnt);
1356 	dtp->jif = jiffies;
1357 #endif
1358 	return;
1359 }
1360 
1361 /**
1362  * lpfc_debugfs_nvme_trc - Store NVME/NVMET trace log
1363  * @phba: The phba to associate this trace string with for retrieval.
1364  * @fmt: Format string to be displayed when dumping the log.
1365  * @data1: 1st data parameter to be applied to @fmt.
1366  * @data2: 2nd data parameter to be applied to @fmt.
1367  * @data3: 3rd data parameter to be applied to @fmt.
1368  *
1369  * Description:
1370  * This routine is used by the driver code to add a debugfs log entry to the
1371  * nvme trace buffer associated with @phba. @fmt, @data1, @data2, and
1372  * @data3 are used like printf when displaying the log.
1373  **/
1374 inline void
1375 lpfc_debugfs_nvme_trc(struct lpfc_hba *phba, char *fmt,
1376 		      uint16_t data1, uint16_t data2, uint32_t data3)
1377 {
1378 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1379 	struct lpfc_debugfs_nvmeio_trc *dtp;
1380 	int index;
1381 
1382 	if (!phba->nvmeio_trc_on || !phba->nvmeio_trc)
1383 		return;
1384 
1385 	index = atomic_inc_return(&phba->nvmeio_trc_cnt) &
1386 		(phba->nvmeio_trc_size - 1);
1387 	dtp = phba->nvmeio_trc + index;
1388 	dtp->fmt = fmt;
1389 	dtp->data1 = data1;
1390 	dtp->data2 = data2;
1391 	dtp->data3 = data3;
1392 #endif
1393 }
1394 
1395 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1396 /**
1397  * lpfc_debugfs_disc_trc_open - Open the discovery trace log
1398  * @inode: The inode pointer that contains a vport pointer.
1399  * @file: The file pointer to attach the log output.
1400  *
1401  * Description:
1402  * This routine is the entry point for the debugfs open file operation. It gets
1403  * the vport from the i_private field in @inode, allocates the necessary buffer
1404  * for the log, fills the buffer from the in-memory log for this vport, and then
1405  * returns a pointer to that log in the private_data field in @file.
1406  *
1407  * Returns:
1408  * This function returns zero if successful. On error it will return a negative
1409  * error value.
1410  **/
1411 static int
1412 lpfc_debugfs_disc_trc_open(struct inode *inode, struct file *file)
1413 {
1414 	struct lpfc_vport *vport = inode->i_private;
1415 	struct lpfc_debug *debug;
1416 	int size;
1417 	int rc = -ENOMEM;
1418 
1419 	if (!lpfc_debugfs_max_disc_trc) {
1420 		 rc = -ENOSPC;
1421 		goto out;
1422 	}
1423 
1424 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1425 	if (!debug)
1426 		goto out;
1427 
1428 	/* Round to page boundary */
1429 	size =  (lpfc_debugfs_max_disc_trc * LPFC_DEBUG_TRC_ENTRY_SIZE);
1430 	size = PAGE_ALIGN(size);
1431 
1432 	debug->buffer = kmalloc(size, GFP_KERNEL);
1433 	if (!debug->buffer) {
1434 		kfree(debug);
1435 		goto out;
1436 	}
1437 
1438 	debug->len = lpfc_debugfs_disc_trc_data(vport, debug->buffer, size);
1439 	file->private_data = debug;
1440 
1441 	rc = 0;
1442 out:
1443 	return rc;
1444 }
1445 
1446 /**
1447  * lpfc_debugfs_slow_ring_trc_open - Open the Slow Ring trace log
1448  * @inode: The inode pointer that contains a vport pointer.
1449  * @file: The file pointer to attach the log output.
1450  *
1451  * Description:
1452  * This routine is the entry point for the debugfs open file operation. It gets
1453  * the vport from the i_private field in @inode, allocates the necessary buffer
1454  * for the log, fills the buffer from the in-memory log for this vport, and then
1455  * returns a pointer to that log in the private_data field in @file.
1456  *
1457  * Returns:
1458  * This function returns zero if successful. On error it will return a negative
1459  * error value.
1460  **/
1461 static int
1462 lpfc_debugfs_slow_ring_trc_open(struct inode *inode, struct file *file)
1463 {
1464 	struct lpfc_hba *phba = inode->i_private;
1465 	struct lpfc_debug *debug;
1466 	int size;
1467 	int rc = -ENOMEM;
1468 
1469 	if (!lpfc_debugfs_max_slow_ring_trc) {
1470 		 rc = -ENOSPC;
1471 		goto out;
1472 	}
1473 
1474 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1475 	if (!debug)
1476 		goto out;
1477 
1478 	/* Round to page boundary */
1479 	size =  (lpfc_debugfs_max_slow_ring_trc * LPFC_DEBUG_TRC_ENTRY_SIZE);
1480 	size = PAGE_ALIGN(size);
1481 
1482 	debug->buffer = kmalloc(size, GFP_KERNEL);
1483 	if (!debug->buffer) {
1484 		kfree(debug);
1485 		goto out;
1486 	}
1487 
1488 	debug->len = lpfc_debugfs_slow_ring_trc_data(phba, debug->buffer, size);
1489 	file->private_data = debug;
1490 
1491 	rc = 0;
1492 out:
1493 	return rc;
1494 }
1495 
1496 /**
1497  * lpfc_debugfs_hbqinfo_open - Open the hbqinfo debugfs buffer
1498  * @inode: The inode pointer that contains a vport pointer.
1499  * @file: The file pointer to attach the log output.
1500  *
1501  * Description:
1502  * This routine is the entry point for the debugfs open file operation. It gets
1503  * the vport from the i_private field in @inode, allocates the necessary buffer
1504  * for the log, fills the buffer from the in-memory log for this vport, and then
1505  * returns a pointer to that log in the private_data field in @file.
1506  *
1507  * Returns:
1508  * This function returns zero if successful. On error it will return a negative
1509  * error value.
1510  **/
1511 static int
1512 lpfc_debugfs_hbqinfo_open(struct inode *inode, struct file *file)
1513 {
1514 	struct lpfc_hba *phba = inode->i_private;
1515 	struct lpfc_debug *debug;
1516 	int rc = -ENOMEM;
1517 
1518 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1519 	if (!debug)
1520 		goto out;
1521 
1522 	/* Round to page boundary */
1523 	debug->buffer = kmalloc(LPFC_HBQINFO_SIZE, GFP_KERNEL);
1524 	if (!debug->buffer) {
1525 		kfree(debug);
1526 		goto out;
1527 	}
1528 
1529 	debug->len = lpfc_debugfs_hbqinfo_data(phba, debug->buffer,
1530 		LPFC_HBQINFO_SIZE);
1531 	file->private_data = debug;
1532 
1533 	rc = 0;
1534 out:
1535 	return rc;
1536 }
1537 
1538 /**
1539  * lpfc_debugfs_dumpHBASlim_open - Open the Dump HBA SLIM debugfs buffer
1540  * @inode: The inode pointer that contains a vport pointer.
1541  * @file: The file pointer to attach the log output.
1542  *
1543  * Description:
1544  * This routine is the entry point for the debugfs open file operation. It gets
1545  * the vport from the i_private field in @inode, allocates the necessary buffer
1546  * for the log, fills the buffer from the in-memory log for this vport, and then
1547  * returns a pointer to that log in the private_data field in @file.
1548  *
1549  * Returns:
1550  * This function returns zero if successful. On error it will return a negative
1551  * error value.
1552  **/
1553 static int
1554 lpfc_debugfs_dumpHBASlim_open(struct inode *inode, struct file *file)
1555 {
1556 	struct lpfc_hba *phba = inode->i_private;
1557 	struct lpfc_debug *debug;
1558 	int rc = -ENOMEM;
1559 
1560 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1561 	if (!debug)
1562 		goto out;
1563 
1564 	/* Round to page boundary */
1565 	debug->buffer = kmalloc(LPFC_DUMPHBASLIM_SIZE, GFP_KERNEL);
1566 	if (!debug->buffer) {
1567 		kfree(debug);
1568 		goto out;
1569 	}
1570 
1571 	debug->len = lpfc_debugfs_dumpHBASlim_data(phba, debug->buffer,
1572 		LPFC_DUMPHBASLIM_SIZE);
1573 	file->private_data = debug;
1574 
1575 	rc = 0;
1576 out:
1577 	return rc;
1578 }
1579 
1580 /**
1581  * lpfc_debugfs_dumpHostSlim_open - Open the Dump Host SLIM debugfs buffer
1582  * @inode: The inode pointer that contains a vport pointer.
1583  * @file: The file pointer to attach the log output.
1584  *
1585  * Description:
1586  * This routine is the entry point for the debugfs open file operation. It gets
1587  * the vport from the i_private field in @inode, allocates the necessary buffer
1588  * for the log, fills the buffer from the in-memory log for this vport, and then
1589  * returns a pointer to that log in the private_data field in @file.
1590  *
1591  * Returns:
1592  * This function returns zero if successful. On error it will return a negative
1593  * error value.
1594  **/
1595 static int
1596 lpfc_debugfs_dumpHostSlim_open(struct inode *inode, struct file *file)
1597 {
1598 	struct lpfc_hba *phba = inode->i_private;
1599 	struct lpfc_debug *debug;
1600 	int rc = -ENOMEM;
1601 
1602 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1603 	if (!debug)
1604 		goto out;
1605 
1606 	/* Round to page boundary */
1607 	debug->buffer = kmalloc(LPFC_DUMPHOSTSLIM_SIZE, GFP_KERNEL);
1608 	if (!debug->buffer) {
1609 		kfree(debug);
1610 		goto out;
1611 	}
1612 
1613 	debug->len = lpfc_debugfs_dumpHostSlim_data(phba, debug->buffer,
1614 		LPFC_DUMPHOSTSLIM_SIZE);
1615 	file->private_data = debug;
1616 
1617 	rc = 0;
1618 out:
1619 	return rc;
1620 }
1621 
1622 static int
1623 lpfc_debugfs_dumpData_open(struct inode *inode, struct file *file)
1624 {
1625 	struct lpfc_debug *debug;
1626 	int rc = -ENOMEM;
1627 
1628 	if (!_dump_buf_data)
1629 		return -EBUSY;
1630 
1631 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1632 	if (!debug)
1633 		goto out;
1634 
1635 	/* Round to page boundary */
1636 	pr_err("9059 BLKGRD:  %s: _dump_buf_data=0x%p\n",
1637 			__func__, _dump_buf_data);
1638 	debug->buffer = _dump_buf_data;
1639 	if (!debug->buffer) {
1640 		kfree(debug);
1641 		goto out;
1642 	}
1643 
1644 	debug->len = (1 << _dump_buf_data_order) << PAGE_SHIFT;
1645 	file->private_data = debug;
1646 
1647 	rc = 0;
1648 out:
1649 	return rc;
1650 }
1651 
1652 static int
1653 lpfc_debugfs_dumpDif_open(struct inode *inode, struct file *file)
1654 {
1655 	struct lpfc_debug *debug;
1656 	int rc = -ENOMEM;
1657 
1658 	if (!_dump_buf_dif)
1659 		return -EBUSY;
1660 
1661 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1662 	if (!debug)
1663 		goto out;
1664 
1665 	/* Round to page boundary */
1666 	pr_err("9060 BLKGRD: %s: _dump_buf_dif=0x%p file=%pD\n",
1667 			__func__, _dump_buf_dif, file);
1668 	debug->buffer = _dump_buf_dif;
1669 	if (!debug->buffer) {
1670 		kfree(debug);
1671 		goto out;
1672 	}
1673 
1674 	debug->len = (1 << _dump_buf_dif_order) << PAGE_SHIFT;
1675 	file->private_data = debug;
1676 
1677 	rc = 0;
1678 out:
1679 	return rc;
1680 }
1681 
1682 static ssize_t
1683 lpfc_debugfs_dumpDataDif_write(struct file *file, const char __user *buf,
1684 		  size_t nbytes, loff_t *ppos)
1685 {
1686 	/*
1687 	 * The Data/DIF buffers only save one failing IO
1688 	 * The write op is used as a reset mechanism after an IO has
1689 	 * already been saved to the next one can be saved
1690 	 */
1691 	spin_lock(&_dump_buf_lock);
1692 
1693 	memset((void *)_dump_buf_data, 0,
1694 			((1 << PAGE_SHIFT) << _dump_buf_data_order));
1695 	memset((void *)_dump_buf_dif, 0,
1696 			((1 << PAGE_SHIFT) << _dump_buf_dif_order));
1697 
1698 	_dump_buf_done = 0;
1699 
1700 	spin_unlock(&_dump_buf_lock);
1701 
1702 	return nbytes;
1703 }
1704 
1705 static ssize_t
1706 lpfc_debugfs_dif_err_read(struct file *file, char __user *buf,
1707 	size_t nbytes, loff_t *ppos)
1708 {
1709 	struct dentry *dent = file->f_path.dentry;
1710 	struct lpfc_hba *phba = file->private_data;
1711 	char cbuf[32];
1712 	uint64_t tmp = 0;
1713 	int cnt = 0;
1714 
1715 	if (dent == phba->debug_writeGuard)
1716 		cnt = snprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wgrd_cnt);
1717 	else if (dent == phba->debug_writeApp)
1718 		cnt = snprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wapp_cnt);
1719 	else if (dent == phba->debug_writeRef)
1720 		cnt = snprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wref_cnt);
1721 	else if (dent == phba->debug_readGuard)
1722 		cnt = snprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rgrd_cnt);
1723 	else if (dent == phba->debug_readApp)
1724 		cnt = snprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rapp_cnt);
1725 	else if (dent == phba->debug_readRef)
1726 		cnt = snprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rref_cnt);
1727 	else if (dent == phba->debug_InjErrNPortID)
1728 		cnt = snprintf(cbuf, 32, "0x%06x\n", phba->lpfc_injerr_nportid);
1729 	else if (dent == phba->debug_InjErrWWPN) {
1730 		memcpy(&tmp, &phba->lpfc_injerr_wwpn, sizeof(struct lpfc_name));
1731 		tmp = cpu_to_be64(tmp);
1732 		cnt = snprintf(cbuf, 32, "0x%016llx\n", tmp);
1733 	} else if (dent == phba->debug_InjErrLBA) {
1734 		if (phba->lpfc_injerr_lba == (sector_t)(-1))
1735 			cnt = snprintf(cbuf, 32, "off\n");
1736 		else
1737 			cnt = snprintf(cbuf, 32, "0x%llx\n",
1738 				 (uint64_t) phba->lpfc_injerr_lba);
1739 	} else
1740 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1741 			 "0547 Unknown debugfs error injection entry\n");
1742 
1743 	return simple_read_from_buffer(buf, nbytes, ppos, &cbuf, cnt);
1744 }
1745 
1746 static ssize_t
1747 lpfc_debugfs_dif_err_write(struct file *file, const char __user *buf,
1748 	size_t nbytes, loff_t *ppos)
1749 {
1750 	struct dentry *dent = file->f_path.dentry;
1751 	struct lpfc_hba *phba = file->private_data;
1752 	char dstbuf[33];
1753 	uint64_t tmp = 0;
1754 	int size;
1755 
1756 	memset(dstbuf, 0, 33);
1757 	size = (nbytes < 32) ? nbytes : 32;
1758 	if (copy_from_user(dstbuf, buf, size))
1759 		return 0;
1760 
1761 	if (dent == phba->debug_InjErrLBA) {
1762 		if ((buf[0] == 'o') && (buf[1] == 'f') && (buf[2] == 'f'))
1763 			tmp = (uint64_t)(-1);
1764 	}
1765 
1766 	if ((tmp == 0) && (kstrtoull(dstbuf, 0, &tmp)))
1767 		return 0;
1768 
1769 	if (dent == phba->debug_writeGuard)
1770 		phba->lpfc_injerr_wgrd_cnt = (uint32_t)tmp;
1771 	else if (dent == phba->debug_writeApp)
1772 		phba->lpfc_injerr_wapp_cnt = (uint32_t)tmp;
1773 	else if (dent == phba->debug_writeRef)
1774 		phba->lpfc_injerr_wref_cnt = (uint32_t)tmp;
1775 	else if (dent == phba->debug_readGuard)
1776 		phba->lpfc_injerr_rgrd_cnt = (uint32_t)tmp;
1777 	else if (dent == phba->debug_readApp)
1778 		phba->lpfc_injerr_rapp_cnt = (uint32_t)tmp;
1779 	else if (dent == phba->debug_readRef)
1780 		phba->lpfc_injerr_rref_cnt = (uint32_t)tmp;
1781 	else if (dent == phba->debug_InjErrLBA)
1782 		phba->lpfc_injerr_lba = (sector_t)tmp;
1783 	else if (dent == phba->debug_InjErrNPortID)
1784 		phba->lpfc_injerr_nportid = (uint32_t)(tmp & Mask_DID);
1785 	else if (dent == phba->debug_InjErrWWPN) {
1786 		tmp = cpu_to_be64(tmp);
1787 		memcpy(&phba->lpfc_injerr_wwpn, &tmp, sizeof(struct lpfc_name));
1788 	} else
1789 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1790 			 "0548 Unknown debugfs error injection entry\n");
1791 
1792 	return nbytes;
1793 }
1794 
1795 static int
1796 lpfc_debugfs_dif_err_release(struct inode *inode, struct file *file)
1797 {
1798 	return 0;
1799 }
1800 
1801 /**
1802  * lpfc_debugfs_nodelist_open - Open the nodelist debugfs file
1803  * @inode: The inode pointer that contains a vport pointer.
1804  * @file: The file pointer to attach the log output.
1805  *
1806  * Description:
1807  * This routine is the entry point for the debugfs open file operation. It gets
1808  * the vport from the i_private field in @inode, allocates the necessary buffer
1809  * for the log, fills the buffer from the in-memory log for this vport, and then
1810  * returns a pointer to that log in the private_data field in @file.
1811  *
1812  * Returns:
1813  * This function returns zero if successful. On error it will return a negative
1814  * error value.
1815  **/
1816 static int
1817 lpfc_debugfs_nodelist_open(struct inode *inode, struct file *file)
1818 {
1819 	struct lpfc_vport *vport = inode->i_private;
1820 	struct lpfc_debug *debug;
1821 	int rc = -ENOMEM;
1822 
1823 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1824 	if (!debug)
1825 		goto out;
1826 
1827 	/* Round to page boundary */
1828 	debug->buffer = kmalloc(LPFC_NODELIST_SIZE, GFP_KERNEL);
1829 	if (!debug->buffer) {
1830 		kfree(debug);
1831 		goto out;
1832 	}
1833 
1834 	debug->len = lpfc_debugfs_nodelist_data(vport, debug->buffer,
1835 		LPFC_NODELIST_SIZE);
1836 	file->private_data = debug;
1837 
1838 	rc = 0;
1839 out:
1840 	return rc;
1841 }
1842 
1843 /**
1844  * lpfc_debugfs_lseek - Seek through a debugfs file
1845  * @file: The file pointer to seek through.
1846  * @off: The offset to seek to or the amount to seek by.
1847  * @whence: Indicates how to seek.
1848  *
1849  * Description:
1850  * This routine is the entry point for the debugfs lseek file operation. The
1851  * @whence parameter indicates whether @off is the offset to directly seek to,
1852  * or if it is a value to seek forward or reverse by. This function figures out
1853  * what the new offset of the debugfs file will be and assigns that value to the
1854  * f_pos field of @file.
1855  *
1856  * Returns:
1857  * This function returns the new offset if successful and returns a negative
1858  * error if unable to process the seek.
1859  **/
1860 static loff_t
1861 lpfc_debugfs_lseek(struct file *file, loff_t off, int whence)
1862 {
1863 	struct lpfc_debug *debug = file->private_data;
1864 	return fixed_size_llseek(file, off, whence, debug->len);
1865 }
1866 
1867 /**
1868  * lpfc_debugfs_read - Read a debugfs file
1869  * @file: The file pointer to read from.
1870  * @buf: The buffer to copy the data to.
1871  * @nbytes: The number of bytes to read.
1872  * @ppos: The position in the file to start reading from.
1873  *
1874  * Description:
1875  * This routine reads data from from the buffer indicated in the private_data
1876  * field of @file. It will start reading at @ppos and copy up to @nbytes of
1877  * data to @buf.
1878  *
1879  * Returns:
1880  * This function returns the amount of data that was read (this could be less
1881  * than @nbytes if the end of the file was reached) or a negative error value.
1882  **/
1883 static ssize_t
1884 lpfc_debugfs_read(struct file *file, char __user *buf,
1885 		  size_t nbytes, loff_t *ppos)
1886 {
1887 	struct lpfc_debug *debug = file->private_data;
1888 
1889 	return simple_read_from_buffer(buf, nbytes, ppos, debug->buffer,
1890 				       debug->len);
1891 }
1892 
1893 /**
1894  * lpfc_debugfs_release - Release the buffer used to store debugfs file data
1895  * @inode: The inode pointer that contains a vport pointer. (unused)
1896  * @file: The file pointer that contains the buffer to release.
1897  *
1898  * Description:
1899  * This routine frees the buffer that was allocated when the debugfs file was
1900  * opened.
1901  *
1902  * Returns:
1903  * This function returns zero.
1904  **/
1905 static int
1906 lpfc_debugfs_release(struct inode *inode, struct file *file)
1907 {
1908 	struct lpfc_debug *debug = file->private_data;
1909 
1910 	kfree(debug->buffer);
1911 	kfree(debug);
1912 
1913 	return 0;
1914 }
1915 
1916 static int
1917 lpfc_debugfs_dumpDataDif_release(struct inode *inode, struct file *file)
1918 {
1919 	struct lpfc_debug *debug = file->private_data;
1920 
1921 	debug->buffer = NULL;
1922 	kfree(debug);
1923 
1924 	return 0;
1925 }
1926 
1927 
1928 static int
1929 lpfc_debugfs_nvmestat_open(struct inode *inode, struct file *file)
1930 {
1931 	struct lpfc_vport *vport = inode->i_private;
1932 	struct lpfc_debug *debug;
1933 	int rc = -ENOMEM;
1934 
1935 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1936 	if (!debug)
1937 		goto out;
1938 
1939 	 /* Round to page boundary */
1940 	debug->buffer = kmalloc(LPFC_NVMESTAT_SIZE, GFP_KERNEL);
1941 	if (!debug->buffer) {
1942 		kfree(debug);
1943 		goto out;
1944 	}
1945 
1946 	debug->len = lpfc_debugfs_nvmestat_data(vport, debug->buffer,
1947 		LPFC_NVMESTAT_SIZE);
1948 
1949 	debug->i_private = inode->i_private;
1950 	file->private_data = debug;
1951 
1952 	rc = 0;
1953 out:
1954 	return rc;
1955 }
1956 
1957 static ssize_t
1958 lpfc_debugfs_nvmestat_write(struct file *file, const char __user *buf,
1959 			    size_t nbytes, loff_t *ppos)
1960 {
1961 	struct lpfc_debug *debug = file->private_data;
1962 	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
1963 	struct lpfc_hba   *phba = vport->phba;
1964 	struct lpfc_nvmet_tgtport *tgtp;
1965 	char mybuf[64];
1966 	char *pbuf;
1967 
1968 	if (!phba->targetport)
1969 		return -ENXIO;
1970 
1971 	if (nbytes > 64)
1972 		nbytes = 64;
1973 
1974 	memset(mybuf, 0, sizeof(mybuf));
1975 
1976 	if (copy_from_user(mybuf, buf, nbytes))
1977 		return -EFAULT;
1978 	pbuf = &mybuf[0];
1979 
1980 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1981 	if ((strncmp(pbuf, "reset", strlen("reset")) == 0) ||
1982 	    (strncmp(pbuf, "zero", strlen("zero")) == 0)) {
1983 		atomic_set(&tgtp->rcv_ls_req_in, 0);
1984 		atomic_set(&tgtp->rcv_ls_req_out, 0);
1985 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
1986 		atomic_set(&tgtp->xmt_ls_abort, 0);
1987 		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
1988 		atomic_set(&tgtp->xmt_ls_rsp, 0);
1989 		atomic_set(&tgtp->xmt_ls_drop, 0);
1990 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
1991 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
1992 
1993 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
1994 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
1995 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
1996 		atomic_set(&tgtp->xmt_fcp_drop, 0);
1997 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
1998 		atomic_set(&tgtp->xmt_fcp_read, 0);
1999 		atomic_set(&tgtp->xmt_fcp_write, 0);
2000 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
2001 		atomic_set(&tgtp->xmt_fcp_release, 0);
2002 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
2003 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
2004 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
2005 
2006 		atomic_set(&tgtp->xmt_fcp_abort, 0);
2007 		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
2008 		atomic_set(&tgtp->xmt_abort_sol, 0);
2009 		atomic_set(&tgtp->xmt_abort_unsol, 0);
2010 		atomic_set(&tgtp->xmt_abort_rsp, 0);
2011 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
2012 	}
2013 	return nbytes;
2014 }
2015 
2016 static int
2017 lpfc_debugfs_nvmektime_open(struct inode *inode, struct file *file)
2018 {
2019 	struct lpfc_vport *vport = inode->i_private;
2020 	struct lpfc_debug *debug;
2021 	int rc = -ENOMEM;
2022 
2023 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2024 	if (!debug)
2025 		goto out;
2026 
2027 	 /* Round to page boundary */
2028 	debug->buffer = kmalloc(LPFC_NVMEKTIME_SIZE, GFP_KERNEL);
2029 	if (!debug->buffer) {
2030 		kfree(debug);
2031 		goto out;
2032 	}
2033 
2034 	debug->len = lpfc_debugfs_nvmektime_data(vport, debug->buffer,
2035 		LPFC_NVMEKTIME_SIZE);
2036 
2037 	debug->i_private = inode->i_private;
2038 	file->private_data = debug;
2039 
2040 	rc = 0;
2041 out:
2042 	return rc;
2043 }
2044 
2045 static ssize_t
2046 lpfc_debugfs_nvmektime_write(struct file *file, const char __user *buf,
2047 			     size_t nbytes, loff_t *ppos)
2048 {
2049 	struct lpfc_debug *debug = file->private_data;
2050 	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2051 	struct lpfc_hba   *phba = vport->phba;
2052 	char mybuf[64];
2053 	char *pbuf;
2054 
2055 	if (nbytes > 64)
2056 		nbytes = 64;
2057 
2058 	memset(mybuf, 0, sizeof(mybuf));
2059 
2060 	if (copy_from_user(mybuf, buf, nbytes))
2061 		return -EFAULT;
2062 	pbuf = &mybuf[0];
2063 
2064 	if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2065 		phba->ktime_data_samples = 0;
2066 		phba->ktime_status_samples = 0;
2067 		phba->ktime_seg1_total = 0;
2068 		phba->ktime_seg1_max = 0;
2069 		phba->ktime_seg1_min = 0xffffffff;
2070 		phba->ktime_seg2_total = 0;
2071 		phba->ktime_seg2_max = 0;
2072 		phba->ktime_seg2_min = 0xffffffff;
2073 		phba->ktime_seg3_total = 0;
2074 		phba->ktime_seg3_max = 0;
2075 		phba->ktime_seg3_min = 0xffffffff;
2076 		phba->ktime_seg4_total = 0;
2077 		phba->ktime_seg4_max = 0;
2078 		phba->ktime_seg4_min = 0xffffffff;
2079 		phba->ktime_seg5_total = 0;
2080 		phba->ktime_seg5_max = 0;
2081 		phba->ktime_seg5_min = 0xffffffff;
2082 		phba->ktime_seg6_total = 0;
2083 		phba->ktime_seg6_max = 0;
2084 		phba->ktime_seg6_min = 0xffffffff;
2085 		phba->ktime_seg7_total = 0;
2086 		phba->ktime_seg7_max = 0;
2087 		phba->ktime_seg7_min = 0xffffffff;
2088 		phba->ktime_seg8_total = 0;
2089 		phba->ktime_seg8_max = 0;
2090 		phba->ktime_seg8_min = 0xffffffff;
2091 		phba->ktime_seg9_total = 0;
2092 		phba->ktime_seg9_max = 0;
2093 		phba->ktime_seg9_min = 0xffffffff;
2094 		phba->ktime_seg10_total = 0;
2095 		phba->ktime_seg10_max = 0;
2096 		phba->ktime_seg10_min = 0xffffffff;
2097 
2098 		phba->ktime_on = 1;
2099 		return strlen(pbuf);
2100 	} else if ((strncmp(pbuf, "off",
2101 		   sizeof("off") - 1) == 0)) {
2102 		phba->ktime_on = 0;
2103 		return strlen(pbuf);
2104 	} else if ((strncmp(pbuf, "zero",
2105 		   sizeof("zero") - 1) == 0)) {
2106 		phba->ktime_data_samples = 0;
2107 		phba->ktime_status_samples = 0;
2108 		phba->ktime_seg1_total = 0;
2109 		phba->ktime_seg1_max = 0;
2110 		phba->ktime_seg1_min = 0xffffffff;
2111 		phba->ktime_seg2_total = 0;
2112 		phba->ktime_seg2_max = 0;
2113 		phba->ktime_seg2_min = 0xffffffff;
2114 		phba->ktime_seg3_total = 0;
2115 		phba->ktime_seg3_max = 0;
2116 		phba->ktime_seg3_min = 0xffffffff;
2117 		phba->ktime_seg4_total = 0;
2118 		phba->ktime_seg4_max = 0;
2119 		phba->ktime_seg4_min = 0xffffffff;
2120 		phba->ktime_seg5_total = 0;
2121 		phba->ktime_seg5_max = 0;
2122 		phba->ktime_seg5_min = 0xffffffff;
2123 		phba->ktime_seg6_total = 0;
2124 		phba->ktime_seg6_max = 0;
2125 		phba->ktime_seg6_min = 0xffffffff;
2126 		phba->ktime_seg7_total = 0;
2127 		phba->ktime_seg7_max = 0;
2128 		phba->ktime_seg7_min = 0xffffffff;
2129 		phba->ktime_seg8_total = 0;
2130 		phba->ktime_seg8_max = 0;
2131 		phba->ktime_seg8_min = 0xffffffff;
2132 		phba->ktime_seg9_total = 0;
2133 		phba->ktime_seg9_max = 0;
2134 		phba->ktime_seg9_min = 0xffffffff;
2135 		phba->ktime_seg10_total = 0;
2136 		phba->ktime_seg10_max = 0;
2137 		phba->ktime_seg10_min = 0xffffffff;
2138 		return strlen(pbuf);
2139 	}
2140 	return -EINVAL;
2141 }
2142 
2143 static int
2144 lpfc_debugfs_nvmeio_trc_open(struct inode *inode, struct file *file)
2145 {
2146 	struct lpfc_hba *phba = inode->i_private;
2147 	struct lpfc_debug *debug;
2148 	int rc = -ENOMEM;
2149 
2150 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2151 	if (!debug)
2152 		goto out;
2153 
2154 	 /* Round to page boundary */
2155 	debug->buffer = kmalloc(LPFC_NVMEIO_TRC_SIZE, GFP_KERNEL);
2156 	if (!debug->buffer) {
2157 		kfree(debug);
2158 		goto out;
2159 	}
2160 
2161 	debug->len = lpfc_debugfs_nvmeio_trc_data(phba, debug->buffer,
2162 		LPFC_NVMEIO_TRC_SIZE);
2163 
2164 	debug->i_private = inode->i_private;
2165 	file->private_data = debug;
2166 
2167 	rc = 0;
2168 out:
2169 	return rc;
2170 }
2171 
2172 static ssize_t
2173 lpfc_debugfs_nvmeio_trc_write(struct file *file, const char __user *buf,
2174 			      size_t nbytes, loff_t *ppos)
2175 {
2176 	struct lpfc_debug *debug = file->private_data;
2177 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2178 	int i;
2179 	unsigned long sz;
2180 	char mybuf[64];
2181 	char *pbuf;
2182 
2183 	if (nbytes > 64)
2184 		nbytes = 64;
2185 
2186 	memset(mybuf, 0, sizeof(mybuf));
2187 
2188 	if (copy_from_user(mybuf, buf, nbytes))
2189 		return -EFAULT;
2190 	pbuf = &mybuf[0];
2191 
2192 	if ((strncmp(pbuf, "off", sizeof("off") - 1) == 0)) {
2193 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2194 				"0570 nvmeio_trc_off\n");
2195 		phba->nvmeio_trc_output_idx = 0;
2196 		phba->nvmeio_trc_on = 0;
2197 		return strlen(pbuf);
2198 	} else if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2199 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2200 				"0571 nvmeio_trc_on\n");
2201 		phba->nvmeio_trc_output_idx = 0;
2202 		phba->nvmeio_trc_on = 1;
2203 		return strlen(pbuf);
2204 	}
2205 
2206 	/* We must be off to allocate the trace buffer */
2207 	if (phba->nvmeio_trc_on != 0)
2208 		return -EINVAL;
2209 
2210 	/* If not on or off, the parameter is the trace buffer size */
2211 	i = kstrtoul(pbuf, 0, &sz);
2212 	if (i)
2213 		return -EINVAL;
2214 	phba->nvmeio_trc_size = (uint32_t)sz;
2215 
2216 	/* It must be a power of 2 - round down */
2217 	i = 0;
2218 	while (sz > 1) {
2219 		sz = sz >> 1;
2220 		i++;
2221 	}
2222 	sz = (1 << i);
2223 	if (phba->nvmeio_trc_size != sz)
2224 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2225 				"0572 nvmeio_trc_size changed to %ld\n",
2226 				sz);
2227 	phba->nvmeio_trc_size = (uint32_t)sz;
2228 
2229 	/* If one previously exists, free it */
2230 	kfree(phba->nvmeio_trc);
2231 
2232 	/* Allocate new trace buffer and initialize */
2233 	phba->nvmeio_trc = kmalloc((sizeof(struct lpfc_debugfs_nvmeio_trc) *
2234 				    sz), GFP_KERNEL);
2235 	if (!phba->nvmeio_trc) {
2236 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2237 				"0573 Cannot create debugfs "
2238 				"nvmeio_trc buffer\n");
2239 		return -ENOMEM;
2240 	}
2241 	memset(phba->nvmeio_trc, 0,
2242 	       (sizeof(struct lpfc_debugfs_nvmeio_trc) * sz));
2243 	atomic_set(&phba->nvmeio_trc_cnt, 0);
2244 	phba->nvmeio_trc_on = 0;
2245 	phba->nvmeio_trc_output_idx = 0;
2246 
2247 	return strlen(pbuf);
2248 }
2249 
2250 static int
2251 lpfc_debugfs_cpucheck_open(struct inode *inode, struct file *file)
2252 {
2253 	struct lpfc_vport *vport = inode->i_private;
2254 	struct lpfc_debug *debug;
2255 	int rc = -ENOMEM;
2256 
2257 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2258 	if (!debug)
2259 		goto out;
2260 
2261 	 /* Round to page boundary */
2262 	debug->buffer = kmalloc(LPFC_CPUCHECK_SIZE, GFP_KERNEL);
2263 	if (!debug->buffer) {
2264 		kfree(debug);
2265 		goto out;
2266 	}
2267 
2268 	debug->len = lpfc_debugfs_cpucheck_data(vport, debug->buffer,
2269 		LPFC_NVMEKTIME_SIZE);
2270 
2271 	debug->i_private = inode->i_private;
2272 	file->private_data = debug;
2273 
2274 	rc = 0;
2275 out:
2276 	return rc;
2277 }
2278 
2279 static ssize_t
2280 lpfc_debugfs_cpucheck_write(struct file *file, const char __user *buf,
2281 			    size_t nbytes, loff_t *ppos)
2282 {
2283 	struct lpfc_debug *debug = file->private_data;
2284 	struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2285 	struct lpfc_hba   *phba = vport->phba;
2286 	char mybuf[64];
2287 	char *pbuf;
2288 	int i;
2289 
2290 	if (nbytes > 64)
2291 		nbytes = 64;
2292 
2293 	memset(mybuf, 0, sizeof(mybuf));
2294 
2295 	if (copy_from_user(mybuf, buf, nbytes))
2296 		return -EFAULT;
2297 	pbuf = &mybuf[0];
2298 
2299 	if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2300 		if (phba->nvmet_support)
2301 			phba->cpucheck_on |= LPFC_CHECK_NVMET_IO;
2302 		else
2303 			phba->cpucheck_on |= LPFC_CHECK_NVME_IO;
2304 		return strlen(pbuf);
2305 	} else if ((strncmp(pbuf, "rcv",
2306 		   sizeof("rcv") - 1) == 0)) {
2307 		if (phba->nvmet_support)
2308 			phba->cpucheck_on |= LPFC_CHECK_NVMET_RCV;
2309 		else
2310 			return -EINVAL;
2311 		return strlen(pbuf);
2312 	} else if ((strncmp(pbuf, "off",
2313 		   sizeof("off") - 1) == 0)) {
2314 		phba->cpucheck_on = LPFC_CHECK_OFF;
2315 		return strlen(pbuf);
2316 	} else if ((strncmp(pbuf, "zero",
2317 		   sizeof("zero") - 1) == 0)) {
2318 		for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
2319 			if (i >= LPFC_CHECK_CPU_CNT)
2320 				break;
2321 			phba->cpucheck_rcv_io[i] = 0;
2322 			phba->cpucheck_xmt_io[i] = 0;
2323 			phba->cpucheck_cmpl_io[i] = 0;
2324 			phba->cpucheck_ccmpl_io[i] = 0;
2325 		}
2326 		return strlen(pbuf);
2327 	}
2328 	return -EINVAL;
2329 }
2330 
2331 /*
2332  * ---------------------------------
2333  * iDiag debugfs file access methods
2334  * ---------------------------------
2335  *
2336  * All access methods are through the proper SLI4 PCI function's debugfs
2337  * iDiag directory:
2338  *
2339  *     /sys/kernel/debug/lpfc/fn<#>/iDiag
2340  */
2341 
2342 /**
2343  * lpfc_idiag_cmd_get - Get and parse idiag debugfs comands from user space
2344  * @buf: The pointer to the user space buffer.
2345  * @nbytes: The number of bytes in the user space buffer.
2346  * @idiag_cmd: pointer to the idiag command struct.
2347  *
2348  * This routine reads data from debugfs user space buffer and parses the
2349  * buffer for getting the idiag command and arguments. The while space in
2350  * between the set of data is used as the parsing separator.
2351  *
2352  * This routine returns 0 when successful, it returns proper error code
2353  * back to the user space in error conditions.
2354  */
2355 static int lpfc_idiag_cmd_get(const char __user *buf, size_t nbytes,
2356 			      struct lpfc_idiag_cmd *idiag_cmd)
2357 {
2358 	char mybuf[64];
2359 	char *pbuf, *step_str;
2360 	int i;
2361 	size_t bsize;
2362 
2363 	memset(mybuf, 0, sizeof(mybuf));
2364 	memset(idiag_cmd, 0, sizeof(*idiag_cmd));
2365 	bsize = min(nbytes, (sizeof(mybuf)-1));
2366 
2367 	if (copy_from_user(mybuf, buf, bsize))
2368 		return -EFAULT;
2369 	pbuf = &mybuf[0];
2370 	step_str = strsep(&pbuf, "\t ");
2371 
2372 	/* The opcode must present */
2373 	if (!step_str)
2374 		return -EINVAL;
2375 
2376 	idiag_cmd->opcode = simple_strtol(step_str, NULL, 0);
2377 	if (idiag_cmd->opcode == 0)
2378 		return -EINVAL;
2379 
2380 	for (i = 0; i < LPFC_IDIAG_CMD_DATA_SIZE; i++) {
2381 		step_str = strsep(&pbuf, "\t ");
2382 		if (!step_str)
2383 			return i;
2384 		idiag_cmd->data[i] = simple_strtol(step_str, NULL, 0);
2385 	}
2386 	return i;
2387 }
2388 
2389 /**
2390  * lpfc_idiag_open - idiag open debugfs
2391  * @inode: The inode pointer that contains a pointer to phba.
2392  * @file: The file pointer to attach the file operation.
2393  *
2394  * Description:
2395  * This routine is the entry point for the debugfs open file operation. It
2396  * gets the reference to phba from the i_private field in @inode, it then
2397  * allocates buffer for the file operation, performs the necessary PCI config
2398  * space read into the allocated buffer according to the idiag user command
2399  * setup, and then returns a pointer to buffer in the private_data field in
2400  * @file.
2401  *
2402  * Returns:
2403  * This function returns zero if successful. On error it will return an
2404  * negative error value.
2405  **/
2406 static int
2407 lpfc_idiag_open(struct inode *inode, struct file *file)
2408 {
2409 	struct lpfc_debug *debug;
2410 
2411 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2412 	if (!debug)
2413 		return -ENOMEM;
2414 
2415 	debug->i_private = inode->i_private;
2416 	debug->buffer = NULL;
2417 	file->private_data = debug;
2418 
2419 	return 0;
2420 }
2421 
2422 /**
2423  * lpfc_idiag_release - Release idiag access file operation
2424  * @inode: The inode pointer that contains a vport pointer. (unused)
2425  * @file: The file pointer that contains the buffer to release.
2426  *
2427  * Description:
2428  * This routine is the generic release routine for the idiag access file
2429  * operation, it frees the buffer that was allocated when the debugfs file
2430  * was opened.
2431  *
2432  * Returns:
2433  * This function returns zero.
2434  **/
2435 static int
2436 lpfc_idiag_release(struct inode *inode, struct file *file)
2437 {
2438 	struct lpfc_debug *debug = file->private_data;
2439 
2440 	/* Free the buffers to the file operation */
2441 	kfree(debug->buffer);
2442 	kfree(debug);
2443 
2444 	return 0;
2445 }
2446 
2447 /**
2448  * lpfc_idiag_cmd_release - Release idiag cmd access file operation
2449  * @inode: The inode pointer that contains a vport pointer. (unused)
2450  * @file: The file pointer that contains the buffer to release.
2451  *
2452  * Description:
2453  * This routine frees the buffer that was allocated when the debugfs file
2454  * was opened. It also reset the fields in the idiag command struct in the
2455  * case of command for write operation.
2456  *
2457  * Returns:
2458  * This function returns zero.
2459  **/
2460 static int
2461 lpfc_idiag_cmd_release(struct inode *inode, struct file *file)
2462 {
2463 	struct lpfc_debug *debug = file->private_data;
2464 
2465 	if (debug->op == LPFC_IDIAG_OP_WR) {
2466 		switch (idiag.cmd.opcode) {
2467 		case LPFC_IDIAG_CMD_PCICFG_WR:
2468 		case LPFC_IDIAG_CMD_PCICFG_ST:
2469 		case LPFC_IDIAG_CMD_PCICFG_CL:
2470 		case LPFC_IDIAG_CMD_QUEACC_WR:
2471 		case LPFC_IDIAG_CMD_QUEACC_ST:
2472 		case LPFC_IDIAG_CMD_QUEACC_CL:
2473 			memset(&idiag, 0, sizeof(idiag));
2474 			break;
2475 		default:
2476 			break;
2477 		}
2478 	}
2479 
2480 	/* Free the buffers to the file operation */
2481 	kfree(debug->buffer);
2482 	kfree(debug);
2483 
2484 	return 0;
2485 }
2486 
2487 /**
2488  * lpfc_idiag_pcicfg_read - idiag debugfs read pcicfg
2489  * @file: The file pointer to read from.
2490  * @buf: The buffer to copy the data to.
2491  * @nbytes: The number of bytes to read.
2492  * @ppos: The position in the file to start reading from.
2493  *
2494  * Description:
2495  * This routine reads data from the @phba pci config space according to the
2496  * idiag command, and copies to user @buf. Depending on the PCI config space
2497  * read command setup, it does either a single register read of a byte
2498  * (8 bits), a word (16 bits), or a dword (32 bits) or browsing through all
2499  * registers from the 4K extended PCI config space.
2500  *
2501  * Returns:
2502  * This function returns the amount of data that was read (this could be less
2503  * than @nbytes if the end of the file was reached) or a negative error value.
2504  **/
2505 static ssize_t
2506 lpfc_idiag_pcicfg_read(struct file *file, char __user *buf, size_t nbytes,
2507 		       loff_t *ppos)
2508 {
2509 	struct lpfc_debug *debug = file->private_data;
2510 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2511 	int offset_label, offset, len = 0, index = LPFC_PCI_CFG_RD_SIZE;
2512 	int where, count;
2513 	char *pbuffer;
2514 	struct pci_dev *pdev;
2515 	uint32_t u32val;
2516 	uint16_t u16val;
2517 	uint8_t u8val;
2518 
2519 	pdev = phba->pcidev;
2520 	if (!pdev)
2521 		return 0;
2522 
2523 	/* This is a user read operation */
2524 	debug->op = LPFC_IDIAG_OP_RD;
2525 
2526 	if (!debug->buffer)
2527 		debug->buffer = kmalloc(LPFC_PCI_CFG_SIZE, GFP_KERNEL);
2528 	if (!debug->buffer)
2529 		return 0;
2530 	pbuffer = debug->buffer;
2531 
2532 	if (*ppos)
2533 		return 0;
2534 
2535 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
2536 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
2537 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
2538 	} else
2539 		return 0;
2540 
2541 	/* Read single PCI config space register */
2542 	switch (count) {
2543 	case SIZE_U8: /* byte (8 bits) */
2544 		pci_read_config_byte(pdev, where, &u8val);
2545 		len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
2546 				"%03x: %02x\n", where, u8val);
2547 		break;
2548 	case SIZE_U16: /* word (16 bits) */
2549 		pci_read_config_word(pdev, where, &u16val);
2550 		len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
2551 				"%03x: %04x\n", where, u16val);
2552 		break;
2553 	case SIZE_U32: /* double word (32 bits) */
2554 		pci_read_config_dword(pdev, where, &u32val);
2555 		len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
2556 				"%03x: %08x\n", where, u32val);
2557 		break;
2558 	case LPFC_PCI_CFG_BROWSE: /* browse all */
2559 		goto pcicfg_browse;
2560 		break;
2561 	default:
2562 		/* illegal count */
2563 		len = 0;
2564 		break;
2565 	}
2566 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2567 
2568 pcicfg_browse:
2569 
2570 	/* Browse all PCI config space registers */
2571 	offset_label = idiag.offset.last_rd;
2572 	offset = offset_label;
2573 
2574 	/* Read PCI config space */
2575 	len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
2576 			"%03x: ", offset_label);
2577 	while (index > 0) {
2578 		pci_read_config_dword(pdev, offset, &u32val);
2579 		len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
2580 				"%08x ", u32val);
2581 		offset += sizeof(uint32_t);
2582 		if (offset >= LPFC_PCI_CFG_SIZE) {
2583 			len += snprintf(pbuffer+len,
2584 					LPFC_PCI_CFG_SIZE-len, "\n");
2585 			break;
2586 		}
2587 		index -= sizeof(uint32_t);
2588 		if (!index)
2589 			len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
2590 					"\n");
2591 		else if (!(index % (8 * sizeof(uint32_t)))) {
2592 			offset_label += (8 * sizeof(uint32_t));
2593 			len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
2594 					"\n%03x: ", offset_label);
2595 		}
2596 	}
2597 
2598 	/* Set up the offset for next portion of pci cfg read */
2599 	if (index == 0) {
2600 		idiag.offset.last_rd += LPFC_PCI_CFG_RD_SIZE;
2601 		if (idiag.offset.last_rd >= LPFC_PCI_CFG_SIZE)
2602 			idiag.offset.last_rd = 0;
2603 	} else
2604 		idiag.offset.last_rd = 0;
2605 
2606 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2607 }
2608 
2609 /**
2610  * lpfc_idiag_pcicfg_write - Syntax check and set up idiag pcicfg commands
2611  * @file: The file pointer to read from.
2612  * @buf: The buffer to copy the user data from.
2613  * @nbytes: The number of bytes to get.
2614  * @ppos: The position in the file to start reading from.
2615  *
2616  * This routine get the debugfs idiag command struct from user space and
2617  * then perform the syntax check for PCI config space read or write command
2618  * accordingly. In the case of PCI config space read command, it sets up
2619  * the command in the idiag command struct for the debugfs read operation.
2620  * In the case of PCI config space write operation, it executes the write
2621  * operation into the PCI config space accordingly.
2622  *
2623  * It returns the @nbytges passing in from debugfs user space when successful.
2624  * In case of error conditions, it returns proper error code back to the user
2625  * space.
2626  */
2627 static ssize_t
2628 lpfc_idiag_pcicfg_write(struct file *file, const char __user *buf,
2629 			size_t nbytes, loff_t *ppos)
2630 {
2631 	struct lpfc_debug *debug = file->private_data;
2632 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2633 	uint32_t where, value, count;
2634 	uint32_t u32val;
2635 	uint16_t u16val;
2636 	uint8_t u8val;
2637 	struct pci_dev *pdev;
2638 	int rc;
2639 
2640 	pdev = phba->pcidev;
2641 	if (!pdev)
2642 		return -EFAULT;
2643 
2644 	/* This is a user write operation */
2645 	debug->op = LPFC_IDIAG_OP_WR;
2646 
2647 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
2648 	if (rc < 0)
2649 		return rc;
2650 
2651 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
2652 		/* Sanity check on PCI config read command line arguments */
2653 		if (rc != LPFC_PCI_CFG_RD_CMD_ARG)
2654 			goto error_out;
2655 		/* Read command from PCI config space, set up command fields */
2656 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
2657 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
2658 		if (count == LPFC_PCI_CFG_BROWSE) {
2659 			if (where % sizeof(uint32_t))
2660 				goto error_out;
2661 			/* Starting offset to browse */
2662 			idiag.offset.last_rd = where;
2663 		} else if ((count != sizeof(uint8_t)) &&
2664 			   (count != sizeof(uint16_t)) &&
2665 			   (count != sizeof(uint32_t)))
2666 			goto error_out;
2667 		if (count == sizeof(uint8_t)) {
2668 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
2669 				goto error_out;
2670 			if (where % sizeof(uint8_t))
2671 				goto error_out;
2672 		}
2673 		if (count == sizeof(uint16_t)) {
2674 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
2675 				goto error_out;
2676 			if (where % sizeof(uint16_t))
2677 				goto error_out;
2678 		}
2679 		if (count == sizeof(uint32_t)) {
2680 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
2681 				goto error_out;
2682 			if (where % sizeof(uint32_t))
2683 				goto error_out;
2684 		}
2685 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR ||
2686 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST ||
2687 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
2688 		/* Sanity check on PCI config write command line arguments */
2689 		if (rc != LPFC_PCI_CFG_WR_CMD_ARG)
2690 			goto error_out;
2691 		/* Write command to PCI config space, read-modify-write */
2692 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
2693 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
2694 		value = idiag.cmd.data[IDIAG_PCICFG_VALUE_INDX];
2695 		/* Sanity checks */
2696 		if ((count != sizeof(uint8_t)) &&
2697 		    (count != sizeof(uint16_t)) &&
2698 		    (count != sizeof(uint32_t)))
2699 			goto error_out;
2700 		if (count == sizeof(uint8_t)) {
2701 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
2702 				goto error_out;
2703 			if (where % sizeof(uint8_t))
2704 				goto error_out;
2705 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
2706 				pci_write_config_byte(pdev, where,
2707 						      (uint8_t)value);
2708 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
2709 				rc = pci_read_config_byte(pdev, where, &u8val);
2710 				if (!rc) {
2711 					u8val |= (uint8_t)value;
2712 					pci_write_config_byte(pdev, where,
2713 							      u8val);
2714 				}
2715 			}
2716 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
2717 				rc = pci_read_config_byte(pdev, where, &u8val);
2718 				if (!rc) {
2719 					u8val &= (uint8_t)(~value);
2720 					pci_write_config_byte(pdev, where,
2721 							      u8val);
2722 				}
2723 			}
2724 		}
2725 		if (count == sizeof(uint16_t)) {
2726 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
2727 				goto error_out;
2728 			if (where % sizeof(uint16_t))
2729 				goto error_out;
2730 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
2731 				pci_write_config_word(pdev, where,
2732 						      (uint16_t)value);
2733 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
2734 				rc = pci_read_config_word(pdev, where, &u16val);
2735 				if (!rc) {
2736 					u16val |= (uint16_t)value;
2737 					pci_write_config_word(pdev, where,
2738 							      u16val);
2739 				}
2740 			}
2741 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
2742 				rc = pci_read_config_word(pdev, where, &u16val);
2743 				if (!rc) {
2744 					u16val &= (uint16_t)(~value);
2745 					pci_write_config_word(pdev, where,
2746 							      u16val);
2747 				}
2748 			}
2749 		}
2750 		if (count == sizeof(uint32_t)) {
2751 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
2752 				goto error_out;
2753 			if (where % sizeof(uint32_t))
2754 				goto error_out;
2755 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
2756 				pci_write_config_dword(pdev, where, value);
2757 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
2758 				rc = pci_read_config_dword(pdev, where,
2759 							   &u32val);
2760 				if (!rc) {
2761 					u32val |= value;
2762 					pci_write_config_dword(pdev, where,
2763 							       u32val);
2764 				}
2765 			}
2766 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
2767 				rc = pci_read_config_dword(pdev, where,
2768 							   &u32val);
2769 				if (!rc) {
2770 					u32val &= ~value;
2771 					pci_write_config_dword(pdev, where,
2772 							       u32val);
2773 				}
2774 			}
2775 		}
2776 	} else
2777 		/* All other opecodes are illegal for now */
2778 		goto error_out;
2779 
2780 	return nbytes;
2781 error_out:
2782 	memset(&idiag, 0, sizeof(idiag));
2783 	return -EINVAL;
2784 }
2785 
2786 /**
2787  * lpfc_idiag_baracc_read - idiag debugfs pci bar access read
2788  * @file: The file pointer to read from.
2789  * @buf: The buffer to copy the data to.
2790  * @nbytes: The number of bytes to read.
2791  * @ppos: The position in the file to start reading from.
2792  *
2793  * Description:
2794  * This routine reads data from the @phba pci bar memory mapped space
2795  * according to the idiag command, and copies to user @buf.
2796  *
2797  * Returns:
2798  * This function returns the amount of data that was read (this could be less
2799  * than @nbytes if the end of the file was reached) or a negative error value.
2800  **/
2801 static ssize_t
2802 lpfc_idiag_baracc_read(struct file *file, char __user *buf, size_t nbytes,
2803 		       loff_t *ppos)
2804 {
2805 	struct lpfc_debug *debug = file->private_data;
2806 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2807 	int offset_label, offset, offset_run, len = 0, index;
2808 	int bar_num, acc_range, bar_size;
2809 	char *pbuffer;
2810 	void __iomem *mem_mapped_bar;
2811 	uint32_t if_type;
2812 	struct pci_dev *pdev;
2813 	uint32_t u32val;
2814 
2815 	pdev = phba->pcidev;
2816 	if (!pdev)
2817 		return 0;
2818 
2819 	/* This is a user read operation */
2820 	debug->op = LPFC_IDIAG_OP_RD;
2821 
2822 	if (!debug->buffer)
2823 		debug->buffer = kmalloc(LPFC_PCI_BAR_RD_BUF_SIZE, GFP_KERNEL);
2824 	if (!debug->buffer)
2825 		return 0;
2826 	pbuffer = debug->buffer;
2827 
2828 	if (*ppos)
2829 		return 0;
2830 
2831 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
2832 		bar_num   = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
2833 		offset    = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
2834 		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
2835 		bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
2836 	} else
2837 		return 0;
2838 
2839 	if (acc_range == 0)
2840 		return 0;
2841 
2842 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
2843 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
2844 		if (bar_num == IDIAG_BARACC_BAR_0)
2845 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
2846 		else if (bar_num == IDIAG_BARACC_BAR_1)
2847 			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
2848 		else if (bar_num == IDIAG_BARACC_BAR_2)
2849 			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
2850 		else
2851 			return 0;
2852 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
2853 		if (bar_num == IDIAG_BARACC_BAR_0)
2854 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
2855 		else
2856 			return 0;
2857 	} else
2858 		return 0;
2859 
2860 	/* Read single PCI bar space register */
2861 	if (acc_range == SINGLE_WORD) {
2862 		offset_run = offset;
2863 		u32val = readl(mem_mapped_bar + offset_run);
2864 		len += snprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
2865 				"%05x: %08x\n", offset_run, u32val);
2866 	} else
2867 		goto baracc_browse;
2868 
2869 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2870 
2871 baracc_browse:
2872 
2873 	/* Browse all PCI bar space registers */
2874 	offset_label = idiag.offset.last_rd;
2875 	offset_run = offset_label;
2876 
2877 	/* Read PCI bar memory mapped space */
2878 	len += snprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
2879 			"%05x: ", offset_label);
2880 	index = LPFC_PCI_BAR_RD_SIZE;
2881 	while (index > 0) {
2882 		u32val = readl(mem_mapped_bar + offset_run);
2883 		len += snprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
2884 				"%08x ", u32val);
2885 		offset_run += sizeof(uint32_t);
2886 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
2887 			if (offset_run >= bar_size) {
2888 				len += snprintf(pbuffer+len,
2889 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
2890 				break;
2891 			}
2892 		} else {
2893 			if (offset_run >= offset +
2894 			    (acc_range * sizeof(uint32_t))) {
2895 				len += snprintf(pbuffer+len,
2896 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
2897 				break;
2898 			}
2899 		}
2900 		index -= sizeof(uint32_t);
2901 		if (!index)
2902 			len += snprintf(pbuffer+len,
2903 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
2904 		else if (!(index % (8 * sizeof(uint32_t)))) {
2905 			offset_label += (8 * sizeof(uint32_t));
2906 			len += snprintf(pbuffer+len,
2907 					LPFC_PCI_BAR_RD_BUF_SIZE-len,
2908 					"\n%05x: ", offset_label);
2909 		}
2910 	}
2911 
2912 	/* Set up the offset for next portion of pci bar read */
2913 	if (index == 0) {
2914 		idiag.offset.last_rd += LPFC_PCI_BAR_RD_SIZE;
2915 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
2916 			if (idiag.offset.last_rd >= bar_size)
2917 				idiag.offset.last_rd = 0;
2918 		} else {
2919 			if (offset_run >= offset +
2920 			    (acc_range * sizeof(uint32_t)))
2921 				idiag.offset.last_rd = offset;
2922 		}
2923 	} else {
2924 		if (acc_range == LPFC_PCI_BAR_BROWSE)
2925 			idiag.offset.last_rd = 0;
2926 		else
2927 			idiag.offset.last_rd = offset;
2928 	}
2929 
2930 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2931 }
2932 
2933 /**
2934  * lpfc_idiag_baracc_write - Syntax check and set up idiag bar access commands
2935  * @file: The file pointer to read from.
2936  * @buf: The buffer to copy the user data from.
2937  * @nbytes: The number of bytes to get.
2938  * @ppos: The position in the file to start reading from.
2939  *
2940  * This routine get the debugfs idiag command struct from user space and
2941  * then perform the syntax check for PCI bar memory mapped space read or
2942  * write command accordingly. In the case of PCI bar memory mapped space
2943  * read command, it sets up the command in the idiag command struct for
2944  * the debugfs read operation. In the case of PCI bar memorpy mapped space
2945  * write operation, it executes the write operation into the PCI bar memory
2946  * mapped space accordingly.
2947  *
2948  * It returns the @nbytges passing in from debugfs user space when successful.
2949  * In case of error conditions, it returns proper error code back to the user
2950  * space.
2951  */
2952 static ssize_t
2953 lpfc_idiag_baracc_write(struct file *file, const char __user *buf,
2954 			size_t nbytes, loff_t *ppos)
2955 {
2956 	struct lpfc_debug *debug = file->private_data;
2957 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2958 	uint32_t bar_num, bar_size, offset, value, acc_range;
2959 	struct pci_dev *pdev;
2960 	void __iomem *mem_mapped_bar;
2961 	uint32_t if_type;
2962 	uint32_t u32val;
2963 	int rc;
2964 
2965 	pdev = phba->pcidev;
2966 	if (!pdev)
2967 		return -EFAULT;
2968 
2969 	/* This is a user write operation */
2970 	debug->op = LPFC_IDIAG_OP_WR;
2971 
2972 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
2973 	if (rc < 0)
2974 		return rc;
2975 
2976 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
2977 	bar_num = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
2978 
2979 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
2980 		if ((bar_num != IDIAG_BARACC_BAR_0) &&
2981 		    (bar_num != IDIAG_BARACC_BAR_1) &&
2982 		    (bar_num != IDIAG_BARACC_BAR_2))
2983 			goto error_out;
2984 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
2985 		if (bar_num != IDIAG_BARACC_BAR_0)
2986 			goto error_out;
2987 	} else
2988 		goto error_out;
2989 
2990 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
2991 		if (bar_num == IDIAG_BARACC_BAR_0) {
2992 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
2993 				LPFC_PCI_IF0_BAR0_SIZE;
2994 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
2995 		} else if (bar_num == IDIAG_BARACC_BAR_1) {
2996 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
2997 				LPFC_PCI_IF0_BAR1_SIZE;
2998 			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
2999 		} else if (bar_num == IDIAG_BARACC_BAR_2) {
3000 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3001 				LPFC_PCI_IF0_BAR2_SIZE;
3002 			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
3003 		} else
3004 			goto error_out;
3005 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3006 		if (bar_num == IDIAG_BARACC_BAR_0) {
3007 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3008 				LPFC_PCI_IF2_BAR0_SIZE;
3009 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3010 		} else
3011 			goto error_out;
3012 	} else
3013 		goto error_out;
3014 
3015 	offset = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
3016 	if (offset % sizeof(uint32_t))
3017 		goto error_out;
3018 
3019 	bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
3020 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
3021 		/* Sanity check on PCI config read command line arguments */
3022 		if (rc != LPFC_PCI_BAR_RD_CMD_ARG)
3023 			goto error_out;
3024 		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
3025 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
3026 			if (offset > bar_size - sizeof(uint32_t))
3027 				goto error_out;
3028 			/* Starting offset to browse */
3029 			idiag.offset.last_rd = offset;
3030 		} else if (acc_range > SINGLE_WORD) {
3031 			if (offset + acc_range * sizeof(uint32_t) > bar_size)
3032 				goto error_out;
3033 			/* Starting offset to browse */
3034 			idiag.offset.last_rd = offset;
3035 		} else if (acc_range != SINGLE_WORD)
3036 			goto error_out;
3037 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR ||
3038 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST ||
3039 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
3040 		/* Sanity check on PCI bar write command line arguments */
3041 		if (rc != LPFC_PCI_BAR_WR_CMD_ARG)
3042 			goto error_out;
3043 		/* Write command to PCI bar space, read-modify-write */
3044 		acc_range = SINGLE_WORD;
3045 		value = idiag.cmd.data[IDIAG_BARACC_REG_VAL_INDX];
3046 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR) {
3047 			writel(value, mem_mapped_bar + offset);
3048 			readl(mem_mapped_bar + offset);
3049 		}
3050 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST) {
3051 			u32val = readl(mem_mapped_bar + offset);
3052 			u32val |= value;
3053 			writel(u32val, mem_mapped_bar + offset);
3054 			readl(mem_mapped_bar + offset);
3055 		}
3056 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
3057 			u32val = readl(mem_mapped_bar + offset);
3058 			u32val &= ~value;
3059 			writel(u32val, mem_mapped_bar + offset);
3060 			readl(mem_mapped_bar + offset);
3061 		}
3062 	} else
3063 		/* All other opecodes are illegal for now */
3064 		goto error_out;
3065 
3066 	return nbytes;
3067 error_out:
3068 	memset(&idiag, 0, sizeof(idiag));
3069 	return -EINVAL;
3070 }
3071 
3072 static int
3073 __lpfc_idiag_print_wq(struct lpfc_queue *qp, char *wqtype,
3074 			char *pbuffer, int len)
3075 {
3076 	if (!qp)
3077 		return len;
3078 
3079 	len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3080 			"\t\t%s WQ info: ", wqtype);
3081 	len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3082 			"AssocCQID[%04d]: WQ-STAT[oflow:x%x posted:x%llx]\n",
3083 			qp->assoc_qid, qp->q_cnt_1,
3084 			(unsigned long long)qp->q_cnt_4);
3085 	len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3086 			"\t\tWQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3087 			"HST-IDX[%04d], PRT-IDX[%04d], PST[%03d]",
3088 			qp->queue_id, qp->entry_count,
3089 			qp->entry_size, qp->host_index,
3090 			qp->hba_index, qp->entry_repost);
3091 	len +=  snprintf(pbuffer + len,
3092 			LPFC_QUE_INFO_GET_BUF_SIZE - len, "\n");
3093 	return len;
3094 }
3095 
3096 static int
3097 lpfc_idiag_wqs_for_cq(struct lpfc_hba *phba, char *wqtype, char *pbuffer,
3098 		int *len, int max_cnt, int cq_id)
3099 {
3100 	struct lpfc_queue *qp;
3101 	int qidx;
3102 
3103 	for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
3104 		qp = phba->sli4_hba.fcp_wq[qidx];
3105 		if (qp->assoc_qid != cq_id)
3106 			continue;
3107 		*len = __lpfc_idiag_print_wq(qp, wqtype, pbuffer, *len);
3108 		if (*len >= max_cnt)
3109 			return 1;
3110 	}
3111 	for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
3112 		qp = phba->sli4_hba.nvme_wq[qidx];
3113 		if (qp->assoc_qid != cq_id)
3114 			continue;
3115 		*len = __lpfc_idiag_print_wq(qp, wqtype, pbuffer, *len);
3116 		if (*len >= max_cnt)
3117 			return 1;
3118 	}
3119 	return 0;
3120 }
3121 
3122 static int
3123 __lpfc_idiag_print_cq(struct lpfc_queue *qp, char *cqtype,
3124 			char *pbuffer, int len)
3125 {
3126 	if (!qp)
3127 		return len;
3128 
3129 	len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3130 			"\t%s CQ info: ", cqtype);
3131 	len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3132 			"AssocEQID[%02d]: CQ STAT[max:x%x relw:x%x "
3133 			"xabt:x%x wq:x%llx]\n",
3134 			qp->assoc_qid, qp->q_cnt_1, qp->q_cnt_2,
3135 			qp->q_cnt_3, (unsigned long long)qp->q_cnt_4);
3136 	len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3137 			"\tCQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3138 			"HST-IDX[%04d], PRT-IDX[%04d], PST[%03d]",
3139 			qp->queue_id, qp->entry_count,
3140 			qp->entry_size, qp->host_index,
3141 			qp->hba_index, qp->entry_repost);
3142 
3143 	len +=  snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len, "\n");
3144 
3145 	return len;
3146 }
3147 
3148 static int
3149 __lpfc_idiag_print_rqpair(struct lpfc_queue *qp, struct lpfc_queue *datqp,
3150 			char *rqtype, char *pbuffer, int len)
3151 {
3152 	if (!qp || !datqp)
3153 		return len;
3154 
3155 	len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3156 			"\t\t%s RQ info: ", rqtype);
3157 	len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3158 			"AssocCQID[%02d]: RQ-STAT[nopost:x%x nobuf:x%x "
3159 			"posted:x%x rcv:x%llx]\n",
3160 			qp->assoc_qid, qp->q_cnt_1, qp->q_cnt_2,
3161 			qp->q_cnt_3, (unsigned long long)qp->q_cnt_4);
3162 	len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3163 			"\t\tHQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3164 			"HST-IDX[%04d], PRT-IDX[%04d], PST[%03d]\n",
3165 			qp->queue_id, qp->entry_count, qp->entry_size,
3166 			qp->host_index, qp->hba_index, qp->entry_repost);
3167 	len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3168 			"\t\tDQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3169 			"HST-IDX[%04d], PRT-IDX[%04d], PST[%03d]\n",
3170 			datqp->queue_id, datqp->entry_count,
3171 			datqp->entry_size, datqp->host_index,
3172 			datqp->hba_index, datqp->entry_repost);
3173 	return len;
3174 }
3175 
3176 static int
3177 lpfc_idiag_cqs_for_eq(struct lpfc_hba *phba, char *pbuffer,
3178 		int *len, int max_cnt, int eqidx, int eq_id)
3179 {
3180 	struct lpfc_queue *qp;
3181 	int qidx, rc;
3182 
3183 	for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
3184 		qp = phba->sli4_hba.fcp_cq[qidx];
3185 		if (qp->assoc_qid != eq_id)
3186 			continue;
3187 
3188 		*len = __lpfc_idiag_print_cq(qp, "FCP", pbuffer, *len);
3189 
3190 		/* Reset max counter */
3191 		qp->CQ_max_cqe = 0;
3192 
3193 		if (*len >= max_cnt)
3194 			return 1;
3195 
3196 		rc = lpfc_idiag_wqs_for_cq(phba, "FCP", pbuffer, len,
3197 				max_cnt, qp->queue_id);
3198 		if (rc)
3199 			return 1;
3200 	}
3201 
3202 	for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
3203 		qp = phba->sli4_hba.nvme_cq[qidx];
3204 		if (qp->assoc_qid != eq_id)
3205 			continue;
3206 
3207 		*len = __lpfc_idiag_print_cq(qp, "NVME", pbuffer, *len);
3208 
3209 		/* Reset max counter */
3210 		qp->CQ_max_cqe = 0;
3211 
3212 		if (*len >= max_cnt)
3213 			return 1;
3214 
3215 		rc = lpfc_idiag_wqs_for_cq(phba, "NVME", pbuffer, len,
3216 				max_cnt, qp->queue_id);
3217 		if (rc)
3218 			return 1;
3219 	}
3220 
3221 	if (eqidx < phba->cfg_nvmet_mrq) {
3222 		/* NVMET CQset */
3223 		qp = phba->sli4_hba.nvmet_cqset[eqidx];
3224 		*len = __lpfc_idiag_print_cq(qp, "NVMET CQset", pbuffer, *len);
3225 
3226 		/* Reset max counter */
3227 		qp->CQ_max_cqe = 0;
3228 
3229 		if (*len >= max_cnt)
3230 			return 1;
3231 
3232 		/* RQ header */
3233 		qp = phba->sli4_hba.nvmet_mrq_hdr[eqidx];
3234 		*len = __lpfc_idiag_print_rqpair(qp,
3235 				phba->sli4_hba.nvmet_mrq_data[eqidx],
3236 				"NVMET MRQ", pbuffer, *len);
3237 
3238 		if (*len >= max_cnt)
3239 			return 1;
3240 	}
3241 
3242 	return 0;
3243 }
3244 
3245 static int
3246 __lpfc_idiag_print_eq(struct lpfc_queue *qp, char *eqtype,
3247 			char *pbuffer, int len)
3248 {
3249 	if (!qp)
3250 		return len;
3251 
3252 	len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3253 			"\n%s EQ info: EQ-STAT[max:x%x noE:x%x "
3254 			"bs:x%x proc:x%llx eqd %d]\n",
3255 			eqtype, qp->q_cnt_1, qp->q_cnt_2, qp->q_cnt_3,
3256 			(unsigned long long)qp->q_cnt_4, qp->q_mode);
3257 	len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3258 			"EQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3259 			"HST-IDX[%04d], PRT-IDX[%04d], PST[%03d]",
3260 			qp->queue_id, qp->entry_count, qp->entry_size,
3261 			qp->host_index, qp->hba_index, qp->entry_repost);
3262 	len +=  snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len, "\n");
3263 
3264 	return len;
3265 }
3266 
3267 /**
3268  * lpfc_idiag_queinfo_read - idiag debugfs read queue information
3269  * @file: The file pointer to read from.
3270  * @buf: The buffer to copy the data to.
3271  * @nbytes: The number of bytes to read.
3272  * @ppos: The position in the file to start reading from.
3273  *
3274  * Description:
3275  * This routine reads data from the @phba SLI4 PCI function queue information,
3276  * and copies to user @buf.
3277  * This routine only returns 1 EQs worth of information. It remembers the last
3278  * EQ read and jumps to the next EQ. Thus subsequent calls to queInfo will
3279  * retrieve all EQs allocated for the phba.
3280  *
3281  * Returns:
3282  * This function returns the amount of data that was read (this could be less
3283  * than @nbytes if the end of the file was reached) or a negative error value.
3284  **/
3285 static ssize_t
3286 lpfc_idiag_queinfo_read(struct file *file, char __user *buf, size_t nbytes,
3287 			loff_t *ppos)
3288 {
3289 	struct lpfc_debug *debug = file->private_data;
3290 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3291 	char *pbuffer;
3292 	int max_cnt, rc, x, len = 0;
3293 	struct lpfc_queue *qp = NULL;
3294 
3295 	if (!debug->buffer)
3296 		debug->buffer = kmalloc(LPFC_QUE_INFO_GET_BUF_SIZE, GFP_KERNEL);
3297 	if (!debug->buffer)
3298 		return 0;
3299 	pbuffer = debug->buffer;
3300 	max_cnt = LPFC_QUE_INFO_GET_BUF_SIZE - 256;
3301 
3302 	if (*ppos)
3303 		return 0;
3304 
3305 	spin_lock_irq(&phba->hbalock);
3306 
3307 	/* Fast-path event queue */
3308 	if (phba->sli4_hba.hba_eq && phba->io_channel_irqs) {
3309 
3310 		x = phba->lpfc_idiag_last_eq;
3311 		if (phba->cfg_fof && (x >= phba->io_channel_irqs)) {
3312 			phba->lpfc_idiag_last_eq = 0;
3313 			goto fof;
3314 		}
3315 		phba->lpfc_idiag_last_eq++;
3316 		if (phba->lpfc_idiag_last_eq >= phba->io_channel_irqs)
3317 			if (phba->cfg_fof == 0)
3318 				phba->lpfc_idiag_last_eq = 0;
3319 
3320 		len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3321 					"EQ %d out of %d HBA EQs\n",
3322 					x, phba->io_channel_irqs);
3323 
3324 		/* Fast-path EQ */
3325 		qp = phba->sli4_hba.hba_eq[x];
3326 		if (!qp)
3327 			goto out;
3328 
3329 		len = __lpfc_idiag_print_eq(qp, "HBA", pbuffer, len);
3330 
3331 		/* Reset max counter */
3332 		qp->EQ_max_eqe = 0;
3333 
3334 		if (len >= max_cnt)
3335 			goto too_big;
3336 
3337 		/* will dump both fcp and nvme cqs/wqs for the eq */
3338 		rc = lpfc_idiag_cqs_for_eq(phba, pbuffer, &len,
3339 			max_cnt, x, qp->queue_id);
3340 		if (rc)
3341 			goto too_big;
3342 
3343 		/* Only EQ 0 has slow path CQs configured */
3344 		if (x)
3345 			goto out;
3346 
3347 		/* Slow-path mailbox CQ */
3348 		qp = phba->sli4_hba.mbx_cq;
3349 		len = __lpfc_idiag_print_cq(qp, "MBX", pbuffer, len);
3350 		if (len >= max_cnt)
3351 			goto too_big;
3352 
3353 		/* Slow-path MBOX MQ */
3354 		qp = phba->sli4_hba.mbx_wq;
3355 		len = __lpfc_idiag_print_wq(qp, "MBX", pbuffer, len);
3356 		if (len >= max_cnt)
3357 			goto too_big;
3358 
3359 		/* Slow-path ELS response CQ */
3360 		qp = phba->sli4_hba.els_cq;
3361 		len = __lpfc_idiag_print_cq(qp, "ELS", pbuffer, len);
3362 		/* Reset max counter */
3363 		if (qp)
3364 			qp->CQ_max_cqe = 0;
3365 		if (len >= max_cnt)
3366 			goto too_big;
3367 
3368 		/* Slow-path ELS WQ */
3369 		qp = phba->sli4_hba.els_wq;
3370 		len = __lpfc_idiag_print_wq(qp, "ELS", pbuffer, len);
3371 		if (len >= max_cnt)
3372 			goto too_big;
3373 
3374 		/* Slow-path NVME LS response CQ */
3375 		qp = phba->sli4_hba.nvmels_cq;
3376 		len = __lpfc_idiag_print_cq(qp, "NVME LS",
3377 						pbuffer, len);
3378 		/* Reset max counter */
3379 		if (qp)
3380 			qp->CQ_max_cqe = 0;
3381 		if (len >= max_cnt)
3382 			goto too_big;
3383 
3384 		/* Slow-path NVME LS WQ */
3385 		qp = phba->sli4_hba.nvmels_wq;
3386 		len = __lpfc_idiag_print_wq(qp, "NVME LS",
3387 						pbuffer, len);
3388 		if (len >= max_cnt)
3389 			goto too_big;
3390 
3391 		qp = phba->sli4_hba.hdr_rq;
3392 		len = __lpfc_idiag_print_rqpair(qp, phba->sli4_hba.dat_rq,
3393 				"RQpair", pbuffer, len);
3394 		if (len >= max_cnt)
3395 			goto too_big;
3396 
3397 		goto out;
3398 	}
3399 
3400 fof:
3401 	if (phba->cfg_fof) {
3402 		/* FOF EQ */
3403 		qp = phba->sli4_hba.fof_eq;
3404 		len = __lpfc_idiag_print_eq(qp, "FOF", pbuffer, len);
3405 
3406 		/* Reset max counter */
3407 		if (qp)
3408 			qp->EQ_max_eqe = 0;
3409 
3410 		if (len >= max_cnt)
3411 			goto too_big;
3412 
3413 		/* OAS CQ */
3414 		qp = phba->sli4_hba.oas_cq;
3415 		len = __lpfc_idiag_print_cq(qp, "OAS", pbuffer, len);
3416 		/* Reset max counter */
3417 		if (qp)
3418 			qp->CQ_max_cqe = 0;
3419 		if (len >= max_cnt)
3420 			goto too_big;
3421 
3422 		/* OAS WQ */
3423 		qp = phba->sli4_hba.oas_wq;
3424 		len = __lpfc_idiag_print_wq(qp, "OAS", pbuffer, len);
3425 		if (len >= max_cnt)
3426 			goto too_big;
3427 	}
3428 
3429 	spin_unlock_irq(&phba->hbalock);
3430 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3431 
3432 too_big:
3433 	len +=  snprintf(pbuffer + len,
3434 		LPFC_QUE_INFO_GET_BUF_SIZE - len, "Truncated ...\n");
3435 out:
3436 	spin_unlock_irq(&phba->hbalock);
3437 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3438 }
3439 
3440 /**
3441  * lpfc_idiag_que_param_check - queue access command parameter sanity check
3442  * @q: The pointer to queue structure.
3443  * @index: The index into a queue entry.
3444  * @count: The number of queue entries to access.
3445  *
3446  * Description:
3447  * The routine performs sanity check on device queue access method commands.
3448  *
3449  * Returns:
3450  * This function returns -EINVAL when fails the sanity check, otherwise, it
3451  * returns 0.
3452  **/
3453 static int
3454 lpfc_idiag_que_param_check(struct lpfc_queue *q, int index, int count)
3455 {
3456 	/* Only support single entry read or browsing */
3457 	if ((count != 1) && (count != LPFC_QUE_ACC_BROWSE))
3458 		return -EINVAL;
3459 	if (index > q->entry_count - 1)
3460 		return -EINVAL;
3461 	return 0;
3462 }
3463 
3464 /**
3465  * lpfc_idiag_queacc_read_qe - read a single entry from the given queue index
3466  * @pbuffer: The pointer to buffer to copy the read data into.
3467  * @pque: The pointer to the queue to be read.
3468  * @index: The index into the queue entry.
3469  *
3470  * Description:
3471  * This routine reads out a single entry from the given queue's index location
3472  * and copies it into the buffer provided.
3473  *
3474  * Returns:
3475  * This function returns 0 when it fails, otherwise, it returns the length of
3476  * the data read into the buffer provided.
3477  **/
3478 static int
3479 lpfc_idiag_queacc_read_qe(char *pbuffer, int len, struct lpfc_queue *pque,
3480 			  uint32_t index)
3481 {
3482 	int offset, esize;
3483 	uint32_t *pentry;
3484 
3485 	if (!pbuffer || !pque)
3486 		return 0;
3487 
3488 	esize = pque->entry_size;
3489 	len += snprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
3490 			"QE-INDEX[%04d]:\n", index);
3491 
3492 	offset = 0;
3493 	pentry = pque->qe[index].address;
3494 	while (esize > 0) {
3495 		len += snprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
3496 				"%08x ", *pentry);
3497 		pentry++;
3498 		offset += sizeof(uint32_t);
3499 		esize -= sizeof(uint32_t);
3500 		if (esize > 0 && !(offset % (4 * sizeof(uint32_t))))
3501 			len += snprintf(pbuffer+len,
3502 					LPFC_QUE_ACC_BUF_SIZE-len, "\n");
3503 	}
3504 	len += snprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len, "\n");
3505 
3506 	return len;
3507 }
3508 
3509 /**
3510  * lpfc_idiag_queacc_read - idiag debugfs read port queue
3511  * @file: The file pointer to read from.
3512  * @buf: The buffer to copy the data to.
3513  * @nbytes: The number of bytes to read.
3514  * @ppos: The position in the file to start reading from.
3515  *
3516  * Description:
3517  * This routine reads data from the @phba device queue memory according to the
3518  * idiag command, and copies to user @buf. Depending on the queue dump read
3519  * command setup, it does either a single queue entry read or browing through
3520  * all entries of the queue.
3521  *
3522  * Returns:
3523  * This function returns the amount of data that was read (this could be less
3524  * than @nbytes if the end of the file was reached) or a negative error value.
3525  **/
3526 static ssize_t
3527 lpfc_idiag_queacc_read(struct file *file, char __user *buf, size_t nbytes,
3528 		       loff_t *ppos)
3529 {
3530 	struct lpfc_debug *debug = file->private_data;
3531 	uint32_t last_index, index, count;
3532 	struct lpfc_queue *pque = NULL;
3533 	char *pbuffer;
3534 	int len = 0;
3535 
3536 	/* This is a user read operation */
3537 	debug->op = LPFC_IDIAG_OP_RD;
3538 
3539 	if (!debug->buffer)
3540 		debug->buffer = kmalloc(LPFC_QUE_ACC_BUF_SIZE, GFP_KERNEL);
3541 	if (!debug->buffer)
3542 		return 0;
3543 	pbuffer = debug->buffer;
3544 
3545 	if (*ppos)
3546 		return 0;
3547 
3548 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
3549 		index = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
3550 		count = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
3551 		pque = (struct lpfc_queue *)idiag.ptr_private;
3552 	} else
3553 		return 0;
3554 
3555 	/* Browse the queue starting from index */
3556 	if (count == LPFC_QUE_ACC_BROWSE)
3557 		goto que_browse;
3558 
3559 	/* Read a single entry from the queue */
3560 	len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
3561 
3562 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3563 
3564 que_browse:
3565 
3566 	/* Browse all entries from the queue */
3567 	last_index = idiag.offset.last_rd;
3568 	index = last_index;
3569 
3570 	while (len < LPFC_QUE_ACC_SIZE - pque->entry_size) {
3571 		len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
3572 		index++;
3573 		if (index > pque->entry_count - 1)
3574 			break;
3575 	}
3576 
3577 	/* Set up the offset for next portion of pci cfg read */
3578 	if (index > pque->entry_count - 1)
3579 		index = 0;
3580 	idiag.offset.last_rd = index;
3581 
3582 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3583 }
3584 
3585 /**
3586  * lpfc_idiag_queacc_write - Syntax check and set up idiag queacc commands
3587  * @file: The file pointer to read from.
3588  * @buf: The buffer to copy the user data from.
3589  * @nbytes: The number of bytes to get.
3590  * @ppos: The position in the file to start reading from.
3591  *
3592  * This routine get the debugfs idiag command struct from user space and then
3593  * perform the syntax check for port queue read (dump) or write (set) command
3594  * accordingly. In the case of port queue read command, it sets up the command
3595  * in the idiag command struct for the following debugfs read operation. In
3596  * the case of port queue write operation, it executes the write operation
3597  * into the port queue entry accordingly.
3598  *
3599  * It returns the @nbytges passing in from debugfs user space when successful.
3600  * In case of error conditions, it returns proper error code back to the user
3601  * space.
3602  **/
3603 static ssize_t
3604 lpfc_idiag_queacc_write(struct file *file, const char __user *buf,
3605 			size_t nbytes, loff_t *ppos)
3606 {
3607 	struct lpfc_debug *debug = file->private_data;
3608 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3609 	uint32_t qidx, quetp, queid, index, count, offset, value;
3610 	uint32_t *pentry;
3611 	struct lpfc_queue *pque, *qp;
3612 	int rc;
3613 
3614 	/* This is a user write operation */
3615 	debug->op = LPFC_IDIAG_OP_WR;
3616 
3617 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3618 	if (rc < 0)
3619 		return rc;
3620 
3621 	/* Get and sanity check on command feilds */
3622 	quetp  = idiag.cmd.data[IDIAG_QUEACC_QUETP_INDX];
3623 	queid  = idiag.cmd.data[IDIAG_QUEACC_QUEID_INDX];
3624 	index  = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
3625 	count  = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
3626 	offset = idiag.cmd.data[IDIAG_QUEACC_OFFST_INDX];
3627 	value  = idiag.cmd.data[IDIAG_QUEACC_VALUE_INDX];
3628 
3629 	/* Sanity check on command line arguments */
3630 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
3631 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
3632 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
3633 		if (rc != LPFC_QUE_ACC_WR_CMD_ARG)
3634 			goto error_out;
3635 		if (count != 1)
3636 			goto error_out;
3637 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
3638 		if (rc != LPFC_QUE_ACC_RD_CMD_ARG)
3639 			goto error_out;
3640 	} else
3641 		goto error_out;
3642 
3643 	switch (quetp) {
3644 	case LPFC_IDIAG_EQ:
3645 		/* HBA event queue */
3646 		if (phba->sli4_hba.hba_eq) {
3647 			for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
3648 				qp = phba->sli4_hba.hba_eq[qidx];
3649 				if (qp && qp->queue_id == queid) {
3650 					/* Sanity check */
3651 					rc = lpfc_idiag_que_param_check(qp,
3652 						index, count);
3653 					if (rc)
3654 						goto error_out;
3655 					idiag.ptr_private = qp;
3656 					goto pass_check;
3657 				}
3658 			}
3659 		}
3660 		goto error_out;
3661 		break;
3662 	case LPFC_IDIAG_CQ:
3663 		/* MBX complete queue */
3664 		if (phba->sli4_hba.mbx_cq &&
3665 		    phba->sli4_hba.mbx_cq->queue_id == queid) {
3666 			/* Sanity check */
3667 			rc = lpfc_idiag_que_param_check(
3668 					phba->sli4_hba.mbx_cq, index, count);
3669 			if (rc)
3670 				goto error_out;
3671 			idiag.ptr_private = phba->sli4_hba.mbx_cq;
3672 			goto pass_check;
3673 		}
3674 		/* ELS complete queue */
3675 		if (phba->sli4_hba.els_cq &&
3676 		    phba->sli4_hba.els_cq->queue_id == queid) {
3677 			/* Sanity check */
3678 			rc = lpfc_idiag_que_param_check(
3679 					phba->sli4_hba.els_cq, index, count);
3680 			if (rc)
3681 				goto error_out;
3682 			idiag.ptr_private = phba->sli4_hba.els_cq;
3683 			goto pass_check;
3684 		}
3685 		/* NVME LS complete queue */
3686 		if (phba->sli4_hba.nvmels_cq &&
3687 		    phba->sli4_hba.nvmels_cq->queue_id == queid) {
3688 			/* Sanity check */
3689 			rc = lpfc_idiag_que_param_check(
3690 					phba->sli4_hba.nvmels_cq, index, count);
3691 			if (rc)
3692 				goto error_out;
3693 			idiag.ptr_private = phba->sli4_hba.nvmels_cq;
3694 			goto pass_check;
3695 		}
3696 		/* FCP complete queue */
3697 		if (phba->sli4_hba.fcp_cq) {
3698 			for (qidx = 0; qidx < phba->cfg_fcp_io_channel;
3699 								qidx++) {
3700 				qp = phba->sli4_hba.fcp_cq[qidx];
3701 				if (qp && qp->queue_id == queid) {
3702 					/* Sanity check */
3703 					rc = lpfc_idiag_que_param_check(
3704 						qp, index, count);
3705 					if (rc)
3706 						goto error_out;
3707 					idiag.ptr_private = qp;
3708 					goto pass_check;
3709 				}
3710 			}
3711 		}
3712 		/* NVME complete queue */
3713 		if (phba->sli4_hba.nvme_cq) {
3714 			qidx = 0;
3715 			do {
3716 				if (phba->sli4_hba.nvme_cq[qidx] &&
3717 				    phba->sli4_hba.nvme_cq[qidx]->queue_id ==
3718 				    queid) {
3719 					/* Sanity check */
3720 					rc = lpfc_idiag_que_param_check(
3721 						phba->sli4_hba.nvme_cq[qidx],
3722 						index, count);
3723 					if (rc)
3724 						goto error_out;
3725 					idiag.ptr_private =
3726 						phba->sli4_hba.nvme_cq[qidx];
3727 					goto pass_check;
3728 				}
3729 			} while (++qidx < phba->cfg_nvme_io_channel);
3730 		}
3731 		goto error_out;
3732 		break;
3733 	case LPFC_IDIAG_MQ:
3734 		/* MBX work queue */
3735 		if (phba->sli4_hba.mbx_wq &&
3736 		    phba->sli4_hba.mbx_wq->queue_id == queid) {
3737 			/* Sanity check */
3738 			rc = lpfc_idiag_que_param_check(
3739 					phba->sli4_hba.mbx_wq, index, count);
3740 			if (rc)
3741 				goto error_out;
3742 			idiag.ptr_private = phba->sli4_hba.mbx_wq;
3743 			goto pass_check;
3744 		}
3745 		goto error_out;
3746 		break;
3747 	case LPFC_IDIAG_WQ:
3748 		/* ELS work queue */
3749 		if (phba->sli4_hba.els_wq &&
3750 		    phba->sli4_hba.els_wq->queue_id == queid) {
3751 			/* Sanity check */
3752 			rc = lpfc_idiag_que_param_check(
3753 					phba->sli4_hba.els_wq, index, count);
3754 			if (rc)
3755 				goto error_out;
3756 			idiag.ptr_private = phba->sli4_hba.els_wq;
3757 			goto pass_check;
3758 		}
3759 		/* NVME LS work queue */
3760 		if (phba->sli4_hba.nvmels_wq &&
3761 		    phba->sli4_hba.nvmels_wq->queue_id == queid) {
3762 			/* Sanity check */
3763 			rc = lpfc_idiag_que_param_check(
3764 					phba->sli4_hba.nvmels_wq, index, count);
3765 			if (rc)
3766 				goto error_out;
3767 			idiag.ptr_private = phba->sli4_hba.nvmels_wq;
3768 			goto pass_check;
3769 		}
3770 		/* FCP work queue */
3771 		if (phba->sli4_hba.fcp_wq) {
3772 			for (qidx = 0; qidx < phba->cfg_fcp_io_channel;
3773 								qidx++) {
3774 				qp = phba->sli4_hba.fcp_wq[qidx];
3775 				if (qp && qp->queue_id == queid) {
3776 					/* Sanity check */
3777 					rc = lpfc_idiag_que_param_check(
3778 						qp, index, count);
3779 					if (rc)
3780 						goto error_out;
3781 					idiag.ptr_private = qp;
3782 					goto pass_check;
3783 				}
3784 			}
3785 		}
3786 		/* NVME work queue */
3787 		if (phba->sli4_hba.nvme_wq) {
3788 			for (qidx = 0; qidx < phba->cfg_nvme_io_channel;
3789 								qidx++) {
3790 				qp = phba->sli4_hba.nvme_wq[qidx];
3791 				if (qp && qp->queue_id == queid) {
3792 					/* Sanity check */
3793 					rc = lpfc_idiag_que_param_check(
3794 						qp, index, count);
3795 					if (rc)
3796 						goto error_out;
3797 					idiag.ptr_private = qp;
3798 					goto pass_check;
3799 				}
3800 			}
3801 		}
3802 
3803 		/* NVME work queues */
3804 		if (phba->sli4_hba.nvme_wq) {
3805 			for (qidx = 0; qidx < phba->cfg_nvme_io_channel;
3806 				qidx++) {
3807 				if (!phba->sli4_hba.nvme_wq[qidx])
3808 					continue;
3809 				if (phba->sli4_hba.nvme_wq[qidx]->queue_id ==
3810 				    queid) {
3811 					/* Sanity check */
3812 					rc = lpfc_idiag_que_param_check(
3813 						phba->sli4_hba.nvme_wq[qidx],
3814 						index, count);
3815 					if (rc)
3816 						goto error_out;
3817 					idiag.ptr_private =
3818 						phba->sli4_hba.nvme_wq[qidx];
3819 					goto pass_check;
3820 				}
3821 			}
3822 		}
3823 		goto error_out;
3824 		break;
3825 	case LPFC_IDIAG_RQ:
3826 		/* HDR queue */
3827 		if (phba->sli4_hba.hdr_rq &&
3828 		    phba->sli4_hba.hdr_rq->queue_id == queid) {
3829 			/* Sanity check */
3830 			rc = lpfc_idiag_que_param_check(
3831 					phba->sli4_hba.hdr_rq, index, count);
3832 			if (rc)
3833 				goto error_out;
3834 			idiag.ptr_private = phba->sli4_hba.hdr_rq;
3835 			goto pass_check;
3836 		}
3837 		/* DAT queue */
3838 		if (phba->sli4_hba.dat_rq &&
3839 		    phba->sli4_hba.dat_rq->queue_id == queid) {
3840 			/* Sanity check */
3841 			rc = lpfc_idiag_que_param_check(
3842 					phba->sli4_hba.dat_rq, index, count);
3843 			if (rc)
3844 				goto error_out;
3845 			idiag.ptr_private = phba->sli4_hba.dat_rq;
3846 			goto pass_check;
3847 		}
3848 		goto error_out;
3849 		break;
3850 	default:
3851 		goto error_out;
3852 		break;
3853 	}
3854 
3855 pass_check:
3856 
3857 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
3858 		if (count == LPFC_QUE_ACC_BROWSE)
3859 			idiag.offset.last_rd = index;
3860 	}
3861 
3862 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
3863 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
3864 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
3865 		/* Additional sanity checks on write operation */
3866 		pque = (struct lpfc_queue *)idiag.ptr_private;
3867 		if (offset > pque->entry_size/sizeof(uint32_t) - 1)
3868 			goto error_out;
3869 		pentry = pque->qe[index].address;
3870 		pentry += offset;
3871 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR)
3872 			*pentry = value;
3873 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST)
3874 			*pentry |= value;
3875 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL)
3876 			*pentry &= ~value;
3877 	}
3878 	return nbytes;
3879 
3880 error_out:
3881 	/* Clean out command structure on command error out */
3882 	memset(&idiag, 0, sizeof(idiag));
3883 	return -EINVAL;
3884 }
3885 
3886 /**
3887  * lpfc_idiag_drbacc_read_reg - idiag debugfs read a doorbell register
3888  * @phba: The pointer to hba structure.
3889  * @pbuffer: The pointer to the buffer to copy the data to.
3890  * @len: The lenght of bytes to copied.
3891  * @drbregid: The id to doorbell registers.
3892  *
3893  * Description:
3894  * This routine reads a doorbell register and copies its content to the
3895  * user buffer pointed to by @pbuffer.
3896  *
3897  * Returns:
3898  * This function returns the amount of data that was copied into @pbuffer.
3899  **/
3900 static int
3901 lpfc_idiag_drbacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
3902 			   int len, uint32_t drbregid)
3903 {
3904 
3905 	if (!pbuffer)
3906 		return 0;
3907 
3908 	switch (drbregid) {
3909 	case LPFC_DRB_EQCQ:
3910 		len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
3911 				"EQCQ-DRB-REG: 0x%08x\n",
3912 				readl(phba->sli4_hba.EQCQDBregaddr));
3913 		break;
3914 	case LPFC_DRB_MQ:
3915 		len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
3916 				"MQ-DRB-REG:   0x%08x\n",
3917 				readl(phba->sli4_hba.MQDBregaddr));
3918 		break;
3919 	case LPFC_DRB_WQ:
3920 		len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
3921 				"WQ-DRB-REG:   0x%08x\n",
3922 				readl(phba->sli4_hba.WQDBregaddr));
3923 		break;
3924 	case LPFC_DRB_RQ:
3925 		len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
3926 				"RQ-DRB-REG:   0x%08x\n",
3927 				readl(phba->sli4_hba.RQDBregaddr));
3928 		break;
3929 	default:
3930 		break;
3931 	}
3932 
3933 	return len;
3934 }
3935 
3936 /**
3937  * lpfc_idiag_drbacc_read - idiag debugfs read port doorbell
3938  * @file: The file pointer to read from.
3939  * @buf: The buffer to copy the data to.
3940  * @nbytes: The number of bytes to read.
3941  * @ppos: The position in the file to start reading from.
3942  *
3943  * Description:
3944  * This routine reads data from the @phba device doorbell register according
3945  * to the idiag command, and copies to user @buf. Depending on the doorbell
3946  * register read command setup, it does either a single doorbell register
3947  * read or dump all doorbell registers.
3948  *
3949  * Returns:
3950  * This function returns the amount of data that was read (this could be less
3951  * than @nbytes if the end of the file was reached) or a negative error value.
3952  **/
3953 static ssize_t
3954 lpfc_idiag_drbacc_read(struct file *file, char __user *buf, size_t nbytes,
3955 		       loff_t *ppos)
3956 {
3957 	struct lpfc_debug *debug = file->private_data;
3958 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3959 	uint32_t drb_reg_id, i;
3960 	char *pbuffer;
3961 	int len = 0;
3962 
3963 	/* This is a user read operation */
3964 	debug->op = LPFC_IDIAG_OP_RD;
3965 
3966 	if (!debug->buffer)
3967 		debug->buffer = kmalloc(LPFC_DRB_ACC_BUF_SIZE, GFP_KERNEL);
3968 	if (!debug->buffer)
3969 		return 0;
3970 	pbuffer = debug->buffer;
3971 
3972 	if (*ppos)
3973 		return 0;
3974 
3975 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD)
3976 		drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
3977 	else
3978 		return 0;
3979 
3980 	if (drb_reg_id == LPFC_DRB_ACC_ALL)
3981 		for (i = 1; i <= LPFC_DRB_MAX; i++)
3982 			len = lpfc_idiag_drbacc_read_reg(phba,
3983 							 pbuffer, len, i);
3984 	else
3985 		len = lpfc_idiag_drbacc_read_reg(phba,
3986 						 pbuffer, len, drb_reg_id);
3987 
3988 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3989 }
3990 
3991 /**
3992  * lpfc_idiag_drbacc_write - Syntax check and set up idiag drbacc commands
3993  * @file: The file pointer to read from.
3994  * @buf: The buffer to copy the user data from.
3995  * @nbytes: The number of bytes to get.
3996  * @ppos: The position in the file to start reading from.
3997  *
3998  * This routine get the debugfs idiag command struct from user space and then
3999  * perform the syntax check for port doorbell register read (dump) or write
4000  * (set) command accordingly. In the case of port queue read command, it sets
4001  * up the command in the idiag command struct for the following debugfs read
4002  * operation. In the case of port doorbell register write operation, it
4003  * executes the write operation into the port doorbell register accordingly.
4004  *
4005  * It returns the @nbytges passing in from debugfs user space when successful.
4006  * In case of error conditions, it returns proper error code back to the user
4007  * space.
4008  **/
4009 static ssize_t
4010 lpfc_idiag_drbacc_write(struct file *file, const char __user *buf,
4011 			size_t nbytes, loff_t *ppos)
4012 {
4013 	struct lpfc_debug *debug = file->private_data;
4014 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4015 	uint32_t drb_reg_id, value, reg_val = 0;
4016 	void __iomem *drb_reg;
4017 	int rc;
4018 
4019 	/* This is a user write operation */
4020 	debug->op = LPFC_IDIAG_OP_WR;
4021 
4022 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4023 	if (rc < 0)
4024 		return rc;
4025 
4026 	/* Sanity check on command line arguments */
4027 	drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
4028 	value = idiag.cmd.data[IDIAG_DRBACC_VALUE_INDX];
4029 
4030 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
4031 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
4032 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4033 		if (rc != LPFC_DRB_ACC_WR_CMD_ARG)
4034 			goto error_out;
4035 		if (drb_reg_id > LPFC_DRB_MAX)
4036 			goto error_out;
4037 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD) {
4038 		if (rc != LPFC_DRB_ACC_RD_CMD_ARG)
4039 			goto error_out;
4040 		if ((drb_reg_id > LPFC_DRB_MAX) &&
4041 		    (drb_reg_id != LPFC_DRB_ACC_ALL))
4042 			goto error_out;
4043 	} else
4044 		goto error_out;
4045 
4046 	/* Perform the write access operation */
4047 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
4048 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
4049 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4050 		switch (drb_reg_id) {
4051 		case LPFC_DRB_EQCQ:
4052 			drb_reg = phba->sli4_hba.EQCQDBregaddr;
4053 			break;
4054 		case LPFC_DRB_MQ:
4055 			drb_reg = phba->sli4_hba.MQDBregaddr;
4056 			break;
4057 		case LPFC_DRB_WQ:
4058 			drb_reg = phba->sli4_hba.WQDBregaddr;
4059 			break;
4060 		case LPFC_DRB_RQ:
4061 			drb_reg = phba->sli4_hba.RQDBregaddr;
4062 			break;
4063 		default:
4064 			goto error_out;
4065 		}
4066 
4067 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR)
4068 			reg_val = value;
4069 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST) {
4070 			reg_val = readl(drb_reg);
4071 			reg_val |= value;
4072 		}
4073 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4074 			reg_val = readl(drb_reg);
4075 			reg_val &= ~value;
4076 		}
4077 		writel(reg_val, drb_reg);
4078 		readl(drb_reg); /* flush */
4079 	}
4080 	return nbytes;
4081 
4082 error_out:
4083 	/* Clean out command structure on command error out */
4084 	memset(&idiag, 0, sizeof(idiag));
4085 	return -EINVAL;
4086 }
4087 
4088 /**
4089  * lpfc_idiag_ctlacc_read_reg - idiag debugfs read a control registers
4090  * @phba: The pointer to hba structure.
4091  * @pbuffer: The pointer to the buffer to copy the data to.
4092  * @len: The lenght of bytes to copied.
4093  * @drbregid: The id to doorbell registers.
4094  *
4095  * Description:
4096  * This routine reads a control register and copies its content to the
4097  * user buffer pointed to by @pbuffer.
4098  *
4099  * Returns:
4100  * This function returns the amount of data that was copied into @pbuffer.
4101  **/
4102 static int
4103 lpfc_idiag_ctlacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
4104 			   int len, uint32_t ctlregid)
4105 {
4106 
4107 	if (!pbuffer)
4108 		return 0;
4109 
4110 	switch (ctlregid) {
4111 	case LPFC_CTL_PORT_SEM:
4112 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4113 				"Port SemReg:   0x%08x\n",
4114 				readl(phba->sli4_hba.conf_regs_memmap_p +
4115 				      LPFC_CTL_PORT_SEM_OFFSET));
4116 		break;
4117 	case LPFC_CTL_PORT_STA:
4118 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4119 				"Port StaReg:   0x%08x\n",
4120 				readl(phba->sli4_hba.conf_regs_memmap_p +
4121 				      LPFC_CTL_PORT_STA_OFFSET));
4122 		break;
4123 	case LPFC_CTL_PORT_CTL:
4124 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4125 				"Port CtlReg:   0x%08x\n",
4126 				readl(phba->sli4_hba.conf_regs_memmap_p +
4127 				      LPFC_CTL_PORT_CTL_OFFSET));
4128 		break;
4129 	case LPFC_CTL_PORT_ER1:
4130 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4131 				"Port Er1Reg:   0x%08x\n",
4132 				readl(phba->sli4_hba.conf_regs_memmap_p +
4133 				      LPFC_CTL_PORT_ER1_OFFSET));
4134 		break;
4135 	case LPFC_CTL_PORT_ER2:
4136 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4137 				"Port Er2Reg:   0x%08x\n",
4138 				readl(phba->sli4_hba.conf_regs_memmap_p +
4139 				      LPFC_CTL_PORT_ER2_OFFSET));
4140 		break;
4141 	case LPFC_CTL_PDEV_CTL:
4142 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4143 				"PDev CtlReg:   0x%08x\n",
4144 				readl(phba->sli4_hba.conf_regs_memmap_p +
4145 				      LPFC_CTL_PDEV_CTL_OFFSET));
4146 		break;
4147 	default:
4148 		break;
4149 	}
4150 	return len;
4151 }
4152 
4153 /**
4154  * lpfc_idiag_ctlacc_read - idiag debugfs read port and device control register
4155  * @file: The file pointer to read from.
4156  * @buf: The buffer to copy the data to.
4157  * @nbytes: The number of bytes to read.
4158  * @ppos: The position in the file to start reading from.
4159  *
4160  * Description:
4161  * This routine reads data from the @phba port and device registers according
4162  * to the idiag command, and copies to user @buf.
4163  *
4164  * Returns:
4165  * This function returns the amount of data that was read (this could be less
4166  * than @nbytes if the end of the file was reached) or a negative error value.
4167  **/
4168 static ssize_t
4169 lpfc_idiag_ctlacc_read(struct file *file, char __user *buf, size_t nbytes,
4170 		       loff_t *ppos)
4171 {
4172 	struct lpfc_debug *debug = file->private_data;
4173 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4174 	uint32_t ctl_reg_id, i;
4175 	char *pbuffer;
4176 	int len = 0;
4177 
4178 	/* This is a user read operation */
4179 	debug->op = LPFC_IDIAG_OP_RD;
4180 
4181 	if (!debug->buffer)
4182 		debug->buffer = kmalloc(LPFC_CTL_ACC_BUF_SIZE, GFP_KERNEL);
4183 	if (!debug->buffer)
4184 		return 0;
4185 	pbuffer = debug->buffer;
4186 
4187 	if (*ppos)
4188 		return 0;
4189 
4190 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD)
4191 		ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
4192 	else
4193 		return 0;
4194 
4195 	if (ctl_reg_id == LPFC_CTL_ACC_ALL)
4196 		for (i = 1; i <= LPFC_CTL_MAX; i++)
4197 			len = lpfc_idiag_ctlacc_read_reg(phba,
4198 							 pbuffer, len, i);
4199 	else
4200 		len = lpfc_idiag_ctlacc_read_reg(phba,
4201 						 pbuffer, len, ctl_reg_id);
4202 
4203 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4204 }
4205 
4206 /**
4207  * lpfc_idiag_ctlacc_write - Syntax check and set up idiag ctlacc commands
4208  * @file: The file pointer to read from.
4209  * @buf: The buffer to copy the user data from.
4210  * @nbytes: The number of bytes to get.
4211  * @ppos: The position in the file to start reading from.
4212  *
4213  * This routine get the debugfs idiag command struct from user space and then
4214  * perform the syntax check for port and device control register read (dump)
4215  * or write (set) command accordingly.
4216  *
4217  * It returns the @nbytges passing in from debugfs user space when successful.
4218  * In case of error conditions, it returns proper error code back to the user
4219  * space.
4220  **/
4221 static ssize_t
4222 lpfc_idiag_ctlacc_write(struct file *file, const char __user *buf,
4223 			size_t nbytes, loff_t *ppos)
4224 {
4225 	struct lpfc_debug *debug = file->private_data;
4226 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4227 	uint32_t ctl_reg_id, value, reg_val = 0;
4228 	void __iomem *ctl_reg;
4229 	int rc;
4230 
4231 	/* This is a user write operation */
4232 	debug->op = LPFC_IDIAG_OP_WR;
4233 
4234 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4235 	if (rc < 0)
4236 		return rc;
4237 
4238 	/* Sanity check on command line arguments */
4239 	ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
4240 	value = idiag.cmd.data[IDIAG_CTLACC_VALUE_INDX];
4241 
4242 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
4243 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
4244 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4245 		if (rc != LPFC_CTL_ACC_WR_CMD_ARG)
4246 			goto error_out;
4247 		if (ctl_reg_id > LPFC_CTL_MAX)
4248 			goto error_out;
4249 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD) {
4250 		if (rc != LPFC_CTL_ACC_RD_CMD_ARG)
4251 			goto error_out;
4252 		if ((ctl_reg_id > LPFC_CTL_MAX) &&
4253 		    (ctl_reg_id != LPFC_CTL_ACC_ALL))
4254 			goto error_out;
4255 	} else
4256 		goto error_out;
4257 
4258 	/* Perform the write access operation */
4259 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
4260 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
4261 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4262 		switch (ctl_reg_id) {
4263 		case LPFC_CTL_PORT_SEM:
4264 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4265 					LPFC_CTL_PORT_SEM_OFFSET;
4266 			break;
4267 		case LPFC_CTL_PORT_STA:
4268 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4269 					LPFC_CTL_PORT_STA_OFFSET;
4270 			break;
4271 		case LPFC_CTL_PORT_CTL:
4272 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4273 					LPFC_CTL_PORT_CTL_OFFSET;
4274 			break;
4275 		case LPFC_CTL_PORT_ER1:
4276 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4277 					LPFC_CTL_PORT_ER1_OFFSET;
4278 			break;
4279 		case LPFC_CTL_PORT_ER2:
4280 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4281 					LPFC_CTL_PORT_ER2_OFFSET;
4282 			break;
4283 		case LPFC_CTL_PDEV_CTL:
4284 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4285 					LPFC_CTL_PDEV_CTL_OFFSET;
4286 			break;
4287 		default:
4288 			goto error_out;
4289 		}
4290 
4291 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR)
4292 			reg_val = value;
4293 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST) {
4294 			reg_val = readl(ctl_reg);
4295 			reg_val |= value;
4296 		}
4297 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4298 			reg_val = readl(ctl_reg);
4299 			reg_val &= ~value;
4300 		}
4301 		writel(reg_val, ctl_reg);
4302 		readl(ctl_reg); /* flush */
4303 	}
4304 	return nbytes;
4305 
4306 error_out:
4307 	/* Clean out command structure on command error out */
4308 	memset(&idiag, 0, sizeof(idiag));
4309 	return -EINVAL;
4310 }
4311 
4312 /**
4313  * lpfc_idiag_mbxacc_get_setup - idiag debugfs get mailbox access setup
4314  * @phba: Pointer to HBA context object.
4315  * @pbuffer: Pointer to data buffer.
4316  *
4317  * Description:
4318  * This routine gets the driver mailbox access debugfs setup information.
4319  *
4320  * Returns:
4321  * This function returns the amount of data that was read (this could be less
4322  * than @nbytes if the end of the file was reached) or a negative error value.
4323  **/
4324 static int
4325 lpfc_idiag_mbxacc_get_setup(struct lpfc_hba *phba, char *pbuffer)
4326 {
4327 	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
4328 	int len = 0;
4329 
4330 	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
4331 	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
4332 	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
4333 	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
4334 
4335 	len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
4336 			"mbx_dump_map: 0x%08x\n", mbx_dump_map);
4337 	len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
4338 			"mbx_dump_cnt: %04d\n", mbx_dump_cnt);
4339 	len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
4340 			"mbx_word_cnt: %04d\n", mbx_word_cnt);
4341 	len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
4342 			"mbx_mbox_cmd: 0x%02x\n", mbx_mbox_cmd);
4343 
4344 	return len;
4345 }
4346 
4347 /**
4348  * lpfc_idiag_mbxacc_read - idiag debugfs read on mailbox access
4349  * @file: The file pointer to read from.
4350  * @buf: The buffer to copy the data to.
4351  * @nbytes: The number of bytes to read.
4352  * @ppos: The position in the file to start reading from.
4353  *
4354  * Description:
4355  * This routine reads data from the @phba driver mailbox access debugfs setup
4356  * information.
4357  *
4358  * Returns:
4359  * This function returns the amount of data that was read (this could be less
4360  * than @nbytes if the end of the file was reached) or a negative error value.
4361  **/
4362 static ssize_t
4363 lpfc_idiag_mbxacc_read(struct file *file, char __user *buf, size_t nbytes,
4364 		       loff_t *ppos)
4365 {
4366 	struct lpfc_debug *debug = file->private_data;
4367 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4368 	char *pbuffer;
4369 	int len = 0;
4370 
4371 	/* This is a user read operation */
4372 	debug->op = LPFC_IDIAG_OP_RD;
4373 
4374 	if (!debug->buffer)
4375 		debug->buffer = kmalloc(LPFC_MBX_ACC_BUF_SIZE, GFP_KERNEL);
4376 	if (!debug->buffer)
4377 		return 0;
4378 	pbuffer = debug->buffer;
4379 
4380 	if (*ppos)
4381 		return 0;
4382 
4383 	if ((idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP) &&
4384 	    (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP))
4385 		return 0;
4386 
4387 	len = lpfc_idiag_mbxacc_get_setup(phba, pbuffer);
4388 
4389 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4390 }
4391 
4392 /**
4393  * lpfc_idiag_mbxacc_write - Syntax check and set up idiag mbxacc commands
4394  * @file: The file pointer to read from.
4395  * @buf: The buffer to copy the user data from.
4396  * @nbytes: The number of bytes to get.
4397  * @ppos: The position in the file to start reading from.
4398  *
4399  * This routine get the debugfs idiag command struct from user space and then
4400  * perform the syntax check for driver mailbox command (dump) and sets up the
4401  * necessary states in the idiag command struct accordingly.
4402  *
4403  * It returns the @nbytges passing in from debugfs user space when successful.
4404  * In case of error conditions, it returns proper error code back to the user
4405  * space.
4406  **/
4407 static ssize_t
4408 lpfc_idiag_mbxacc_write(struct file *file, const char __user *buf,
4409 			size_t nbytes, loff_t *ppos)
4410 {
4411 	struct lpfc_debug *debug = file->private_data;
4412 	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
4413 	int rc;
4414 
4415 	/* This is a user write operation */
4416 	debug->op = LPFC_IDIAG_OP_WR;
4417 
4418 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4419 	if (rc < 0)
4420 		return rc;
4421 
4422 	/* Sanity check on command line arguments */
4423 	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
4424 	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
4425 	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
4426 	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
4427 
4428 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_MBXACC_DP) {
4429 		if (!(mbx_dump_map & LPFC_MBX_DMP_MBX_ALL))
4430 			goto error_out;
4431 		if ((mbx_dump_map & ~LPFC_MBX_DMP_MBX_ALL) &&
4432 		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
4433 			goto error_out;
4434 		if (mbx_word_cnt > sizeof(MAILBOX_t))
4435 			goto error_out;
4436 	} else if (idiag.cmd.opcode == LPFC_IDIAG_BSG_MBXACC_DP) {
4437 		if (!(mbx_dump_map & LPFC_BSG_DMP_MBX_ALL))
4438 			goto error_out;
4439 		if ((mbx_dump_map & ~LPFC_BSG_DMP_MBX_ALL) &&
4440 		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
4441 			goto error_out;
4442 		if (mbx_word_cnt > (BSG_MBOX_SIZE)/4)
4443 			goto error_out;
4444 		if (mbx_mbox_cmd != 0x9b)
4445 			goto error_out;
4446 	} else
4447 		goto error_out;
4448 
4449 	if (mbx_word_cnt == 0)
4450 		goto error_out;
4451 	if (rc != LPFC_MBX_DMP_ARG)
4452 		goto error_out;
4453 	if (mbx_mbox_cmd & ~0xff)
4454 		goto error_out;
4455 
4456 	/* condition for stop mailbox dump */
4457 	if (mbx_dump_cnt == 0)
4458 		goto reset_out;
4459 
4460 	return nbytes;
4461 
4462 reset_out:
4463 	/* Clean out command structure on command error out */
4464 	memset(&idiag, 0, sizeof(idiag));
4465 	return nbytes;
4466 
4467 error_out:
4468 	/* Clean out command structure on command error out */
4469 	memset(&idiag, 0, sizeof(idiag));
4470 	return -EINVAL;
4471 }
4472 
4473 /**
4474  * lpfc_idiag_extacc_avail_get - get the available extents information
4475  * @phba: pointer to lpfc hba data structure.
4476  * @pbuffer: pointer to internal buffer.
4477  * @len: length into the internal buffer data has been copied.
4478  *
4479  * Description:
4480  * This routine is to get the available extent information.
4481  *
4482  * Returns:
4483  * overall lenth of the data read into the internal buffer.
4484  **/
4485 static int
4486 lpfc_idiag_extacc_avail_get(struct lpfc_hba *phba, char *pbuffer, int len)
4487 {
4488 	uint16_t ext_cnt, ext_size;
4489 
4490 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4491 			"\nAvailable Extents Information:\n");
4492 
4493 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4494 			"\tPort Available VPI extents: ");
4495 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VPI,
4496 				       &ext_cnt, &ext_size);
4497 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4498 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
4499 
4500 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4501 			"\tPort Available VFI extents: ");
4502 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VFI,
4503 				       &ext_cnt, &ext_size);
4504 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4505 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
4506 
4507 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4508 			"\tPort Available RPI extents: ");
4509 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_RPI,
4510 				       &ext_cnt, &ext_size);
4511 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4512 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
4513 
4514 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4515 			"\tPort Available XRI extents: ");
4516 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_XRI,
4517 				       &ext_cnt, &ext_size);
4518 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4519 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
4520 
4521 	return len;
4522 }
4523 
4524 /**
4525  * lpfc_idiag_extacc_alloc_get - get the allocated extents information
4526  * @phba: pointer to lpfc hba data structure.
4527  * @pbuffer: pointer to internal buffer.
4528  * @len: length into the internal buffer data has been copied.
4529  *
4530  * Description:
4531  * This routine is to get the allocated extent information.
4532  *
4533  * Returns:
4534  * overall lenth of the data read into the internal buffer.
4535  **/
4536 static int
4537 lpfc_idiag_extacc_alloc_get(struct lpfc_hba *phba, char *pbuffer, int len)
4538 {
4539 	uint16_t ext_cnt, ext_size;
4540 	int rc;
4541 
4542 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4543 			"\nAllocated Extents Information:\n");
4544 
4545 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4546 			"\tHost Allocated VPI extents: ");
4547 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VPI,
4548 					    &ext_cnt, &ext_size);
4549 	if (!rc)
4550 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4551 				"Port %d Extent %3d, Size %3d\n",
4552 				phba->brd_no, ext_cnt, ext_size);
4553 	else
4554 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4555 				"N/A\n");
4556 
4557 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4558 			"\tHost Allocated VFI extents: ");
4559 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VFI,
4560 					    &ext_cnt, &ext_size);
4561 	if (!rc)
4562 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4563 				"Port %d Extent %3d, Size %3d\n",
4564 				phba->brd_no, ext_cnt, ext_size);
4565 	else
4566 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4567 				"N/A\n");
4568 
4569 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4570 			"\tHost Allocated RPI extents: ");
4571 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_RPI,
4572 					    &ext_cnt, &ext_size);
4573 	if (!rc)
4574 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4575 				"Port %d Extent %3d, Size %3d\n",
4576 				phba->brd_no, ext_cnt, ext_size);
4577 	else
4578 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4579 				"N/A\n");
4580 
4581 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4582 			"\tHost Allocated XRI extents: ");
4583 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_XRI,
4584 					    &ext_cnt, &ext_size);
4585 	if (!rc)
4586 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4587 				"Port %d Extent %3d, Size %3d\n",
4588 				phba->brd_no, ext_cnt, ext_size);
4589 	else
4590 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4591 				"N/A\n");
4592 
4593 	return len;
4594 }
4595 
4596 /**
4597  * lpfc_idiag_extacc_drivr_get - get driver extent information
4598  * @phba: pointer to lpfc hba data structure.
4599  * @pbuffer: pointer to internal buffer.
4600  * @len: length into the internal buffer data has been copied.
4601  *
4602  * Description:
4603  * This routine is to get the driver extent information.
4604  *
4605  * Returns:
4606  * overall lenth of the data read into the internal buffer.
4607  **/
4608 static int
4609 lpfc_idiag_extacc_drivr_get(struct lpfc_hba *phba, char *pbuffer, int len)
4610 {
4611 	struct lpfc_rsrc_blks *rsrc_blks;
4612 	int index;
4613 
4614 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4615 			"\nDriver Extents Information:\n");
4616 
4617 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4618 			"\tVPI extents:\n");
4619 	index = 0;
4620 	list_for_each_entry(rsrc_blks, &phba->lpfc_vpi_blk_list, list) {
4621 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4622 				"\t\tBlock %3d: Start %4d, Count %4d\n",
4623 				index, rsrc_blks->rsrc_start,
4624 				rsrc_blks->rsrc_size);
4625 		index++;
4626 	}
4627 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4628 			"\tVFI extents:\n");
4629 	index = 0;
4630 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_vfi_blk_list,
4631 			    list) {
4632 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4633 				"\t\tBlock %3d: Start %4d, Count %4d\n",
4634 				index, rsrc_blks->rsrc_start,
4635 				rsrc_blks->rsrc_size);
4636 		index++;
4637 	}
4638 
4639 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4640 			"\tRPI extents:\n");
4641 	index = 0;
4642 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_rpi_blk_list,
4643 			    list) {
4644 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4645 				"\t\tBlock %3d: Start %4d, Count %4d\n",
4646 				index, rsrc_blks->rsrc_start,
4647 				rsrc_blks->rsrc_size);
4648 		index++;
4649 	}
4650 
4651 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4652 			"\tXRI extents:\n");
4653 	index = 0;
4654 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_xri_blk_list,
4655 			    list) {
4656 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4657 				"\t\tBlock %3d: Start %4d, Count %4d\n",
4658 				index, rsrc_blks->rsrc_start,
4659 				rsrc_blks->rsrc_size);
4660 		index++;
4661 	}
4662 
4663 	return len;
4664 }
4665 
4666 /**
4667  * lpfc_idiag_extacc_write - Syntax check and set up idiag extacc 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 extent information access commands and sets
4675  * up the necessary states in the idiag command struct accordingly.
4676  *
4677  * It returns the @nbytges passing in from debugfs user space when successful.
4678  * In case of error conditions, it returns proper error code back to the user
4679  * space.
4680  **/
4681 static ssize_t
4682 lpfc_idiag_extacc_write(struct file *file, const char __user *buf,
4683 			size_t nbytes, loff_t *ppos)
4684 {
4685 	struct lpfc_debug *debug = file->private_data;
4686 	uint32_t ext_map;
4687 	int rc;
4688 
4689 	/* This is a user write operation */
4690 	debug->op = LPFC_IDIAG_OP_WR;
4691 
4692 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4693 	if (rc < 0)
4694 		return rc;
4695 
4696 	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
4697 
4698 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
4699 		goto error_out;
4700 	if (rc != LPFC_EXT_ACC_CMD_ARG)
4701 		goto error_out;
4702 	if (!(ext_map & LPFC_EXT_ACC_ALL))
4703 		goto error_out;
4704 
4705 	return nbytes;
4706 error_out:
4707 	/* Clean out command structure on command error out */
4708 	memset(&idiag, 0, sizeof(idiag));
4709 	return -EINVAL;
4710 }
4711 
4712 /**
4713  * lpfc_idiag_extacc_read - idiag debugfs read access to extent information
4714  * @file: The file pointer to read from.
4715  * @buf: The buffer to copy the data to.
4716  * @nbytes: The number of bytes to read.
4717  * @ppos: The position in the file to start reading from.
4718  *
4719  * Description:
4720  * This routine reads data from the proper extent information according to
4721  * the idiag command, and copies to user @buf.
4722  *
4723  * Returns:
4724  * This function returns the amount of data that was read (this could be less
4725  * than @nbytes if the end of the file was reached) or a negative error value.
4726  **/
4727 static ssize_t
4728 lpfc_idiag_extacc_read(struct file *file, char __user *buf, size_t nbytes,
4729 		       loff_t *ppos)
4730 {
4731 	struct lpfc_debug *debug = file->private_data;
4732 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4733 	char *pbuffer;
4734 	uint32_t ext_map;
4735 	int len = 0;
4736 
4737 	/* This is a user read operation */
4738 	debug->op = LPFC_IDIAG_OP_RD;
4739 
4740 	if (!debug->buffer)
4741 		debug->buffer = kmalloc(LPFC_EXT_ACC_BUF_SIZE, GFP_KERNEL);
4742 	if (!debug->buffer)
4743 		return 0;
4744 	pbuffer = debug->buffer;
4745 	if (*ppos)
4746 		return 0;
4747 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
4748 		return 0;
4749 
4750 	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
4751 	if (ext_map & LPFC_EXT_ACC_AVAIL)
4752 		len = lpfc_idiag_extacc_avail_get(phba, pbuffer, len);
4753 	if (ext_map & LPFC_EXT_ACC_ALLOC)
4754 		len = lpfc_idiag_extacc_alloc_get(phba, pbuffer, len);
4755 	if (ext_map & LPFC_EXT_ACC_DRIVR)
4756 		len = lpfc_idiag_extacc_drivr_get(phba, pbuffer, len);
4757 
4758 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4759 }
4760 
4761 #undef lpfc_debugfs_op_disc_trc
4762 static const struct file_operations lpfc_debugfs_op_disc_trc = {
4763 	.owner =        THIS_MODULE,
4764 	.open =         lpfc_debugfs_disc_trc_open,
4765 	.llseek =       lpfc_debugfs_lseek,
4766 	.read =         lpfc_debugfs_read,
4767 	.release =      lpfc_debugfs_release,
4768 };
4769 
4770 #undef lpfc_debugfs_op_nodelist
4771 static const struct file_operations lpfc_debugfs_op_nodelist = {
4772 	.owner =        THIS_MODULE,
4773 	.open =         lpfc_debugfs_nodelist_open,
4774 	.llseek =       lpfc_debugfs_lseek,
4775 	.read =         lpfc_debugfs_read,
4776 	.release =      lpfc_debugfs_release,
4777 };
4778 
4779 #undef lpfc_debugfs_op_hbqinfo
4780 static const struct file_operations lpfc_debugfs_op_hbqinfo = {
4781 	.owner =        THIS_MODULE,
4782 	.open =         lpfc_debugfs_hbqinfo_open,
4783 	.llseek =       lpfc_debugfs_lseek,
4784 	.read =         lpfc_debugfs_read,
4785 	.release =      lpfc_debugfs_release,
4786 };
4787 
4788 #undef lpfc_debugfs_op_dumpHBASlim
4789 static const struct file_operations lpfc_debugfs_op_dumpHBASlim = {
4790 	.owner =        THIS_MODULE,
4791 	.open =         lpfc_debugfs_dumpHBASlim_open,
4792 	.llseek =       lpfc_debugfs_lseek,
4793 	.read =         lpfc_debugfs_read,
4794 	.release =      lpfc_debugfs_release,
4795 };
4796 
4797 #undef lpfc_debugfs_op_dumpHostSlim
4798 static const struct file_operations lpfc_debugfs_op_dumpHostSlim = {
4799 	.owner =        THIS_MODULE,
4800 	.open =         lpfc_debugfs_dumpHostSlim_open,
4801 	.llseek =       lpfc_debugfs_lseek,
4802 	.read =         lpfc_debugfs_read,
4803 	.release =      lpfc_debugfs_release,
4804 };
4805 
4806 #undef lpfc_debugfs_op_nvmestat
4807 static const struct file_operations lpfc_debugfs_op_nvmestat = {
4808 	.owner =        THIS_MODULE,
4809 	.open =         lpfc_debugfs_nvmestat_open,
4810 	.llseek =       lpfc_debugfs_lseek,
4811 	.read =         lpfc_debugfs_read,
4812 	.write =	lpfc_debugfs_nvmestat_write,
4813 	.release =      lpfc_debugfs_release,
4814 };
4815 
4816 #undef lpfc_debugfs_op_nvmektime
4817 static const struct file_operations lpfc_debugfs_op_nvmektime = {
4818 	.owner =        THIS_MODULE,
4819 	.open =         lpfc_debugfs_nvmektime_open,
4820 	.llseek =       lpfc_debugfs_lseek,
4821 	.read =         lpfc_debugfs_read,
4822 	.write =	lpfc_debugfs_nvmektime_write,
4823 	.release =      lpfc_debugfs_release,
4824 };
4825 
4826 #undef lpfc_debugfs_op_nvmeio_trc
4827 static const struct file_operations lpfc_debugfs_op_nvmeio_trc = {
4828 	.owner =        THIS_MODULE,
4829 	.open =         lpfc_debugfs_nvmeio_trc_open,
4830 	.llseek =       lpfc_debugfs_lseek,
4831 	.read =         lpfc_debugfs_read,
4832 	.write =	lpfc_debugfs_nvmeio_trc_write,
4833 	.release =      lpfc_debugfs_release,
4834 };
4835 
4836 #undef lpfc_debugfs_op_cpucheck
4837 static const struct file_operations lpfc_debugfs_op_cpucheck = {
4838 	.owner =        THIS_MODULE,
4839 	.open =         lpfc_debugfs_cpucheck_open,
4840 	.llseek =       lpfc_debugfs_lseek,
4841 	.read =         lpfc_debugfs_read,
4842 	.write =	lpfc_debugfs_cpucheck_write,
4843 	.release =      lpfc_debugfs_release,
4844 };
4845 
4846 #undef lpfc_debugfs_op_dumpData
4847 static const struct file_operations lpfc_debugfs_op_dumpData = {
4848 	.owner =        THIS_MODULE,
4849 	.open =         lpfc_debugfs_dumpData_open,
4850 	.llseek =       lpfc_debugfs_lseek,
4851 	.read =         lpfc_debugfs_read,
4852 	.write =	lpfc_debugfs_dumpDataDif_write,
4853 	.release =      lpfc_debugfs_dumpDataDif_release,
4854 };
4855 
4856 #undef lpfc_debugfs_op_dumpDif
4857 static const struct file_operations lpfc_debugfs_op_dumpDif = {
4858 	.owner =        THIS_MODULE,
4859 	.open =         lpfc_debugfs_dumpDif_open,
4860 	.llseek =       lpfc_debugfs_lseek,
4861 	.read =         lpfc_debugfs_read,
4862 	.write =	lpfc_debugfs_dumpDataDif_write,
4863 	.release =      lpfc_debugfs_dumpDataDif_release,
4864 };
4865 
4866 #undef lpfc_debugfs_op_dif_err
4867 static const struct file_operations lpfc_debugfs_op_dif_err = {
4868 	.owner =	THIS_MODULE,
4869 	.open =		simple_open,
4870 	.llseek =	lpfc_debugfs_lseek,
4871 	.read =		lpfc_debugfs_dif_err_read,
4872 	.write =	lpfc_debugfs_dif_err_write,
4873 	.release =	lpfc_debugfs_dif_err_release,
4874 };
4875 
4876 #undef lpfc_debugfs_op_slow_ring_trc
4877 static const struct file_operations lpfc_debugfs_op_slow_ring_trc = {
4878 	.owner =        THIS_MODULE,
4879 	.open =         lpfc_debugfs_slow_ring_trc_open,
4880 	.llseek =       lpfc_debugfs_lseek,
4881 	.read =         lpfc_debugfs_read,
4882 	.release =      lpfc_debugfs_release,
4883 };
4884 
4885 static struct dentry *lpfc_debugfs_root = NULL;
4886 static atomic_t lpfc_debugfs_hba_count;
4887 
4888 /*
4889  * File operations for the iDiag debugfs
4890  */
4891 #undef lpfc_idiag_op_pciCfg
4892 static const struct file_operations lpfc_idiag_op_pciCfg = {
4893 	.owner =        THIS_MODULE,
4894 	.open =         lpfc_idiag_open,
4895 	.llseek =       lpfc_debugfs_lseek,
4896 	.read =         lpfc_idiag_pcicfg_read,
4897 	.write =        lpfc_idiag_pcicfg_write,
4898 	.release =      lpfc_idiag_cmd_release,
4899 };
4900 
4901 #undef lpfc_idiag_op_barAcc
4902 static const struct file_operations lpfc_idiag_op_barAcc = {
4903 	.owner =        THIS_MODULE,
4904 	.open =         lpfc_idiag_open,
4905 	.llseek =       lpfc_debugfs_lseek,
4906 	.read =         lpfc_idiag_baracc_read,
4907 	.write =        lpfc_idiag_baracc_write,
4908 	.release =      lpfc_idiag_cmd_release,
4909 };
4910 
4911 #undef lpfc_idiag_op_queInfo
4912 static const struct file_operations lpfc_idiag_op_queInfo = {
4913 	.owner =        THIS_MODULE,
4914 	.open =         lpfc_idiag_open,
4915 	.read =         lpfc_idiag_queinfo_read,
4916 	.release =      lpfc_idiag_release,
4917 };
4918 
4919 #undef lpfc_idiag_op_queAcc
4920 static const struct file_operations lpfc_idiag_op_queAcc = {
4921 	.owner =        THIS_MODULE,
4922 	.open =         lpfc_idiag_open,
4923 	.llseek =       lpfc_debugfs_lseek,
4924 	.read =         lpfc_idiag_queacc_read,
4925 	.write =        lpfc_idiag_queacc_write,
4926 	.release =      lpfc_idiag_cmd_release,
4927 };
4928 
4929 #undef lpfc_idiag_op_drbAcc
4930 static const struct file_operations lpfc_idiag_op_drbAcc = {
4931 	.owner =        THIS_MODULE,
4932 	.open =         lpfc_idiag_open,
4933 	.llseek =       lpfc_debugfs_lseek,
4934 	.read =         lpfc_idiag_drbacc_read,
4935 	.write =        lpfc_idiag_drbacc_write,
4936 	.release =      lpfc_idiag_cmd_release,
4937 };
4938 
4939 #undef lpfc_idiag_op_ctlAcc
4940 static const struct file_operations lpfc_idiag_op_ctlAcc = {
4941 	.owner =        THIS_MODULE,
4942 	.open =         lpfc_idiag_open,
4943 	.llseek =       lpfc_debugfs_lseek,
4944 	.read =         lpfc_idiag_ctlacc_read,
4945 	.write =        lpfc_idiag_ctlacc_write,
4946 	.release =      lpfc_idiag_cmd_release,
4947 };
4948 
4949 #undef lpfc_idiag_op_mbxAcc
4950 static const struct file_operations lpfc_idiag_op_mbxAcc = {
4951 	.owner =        THIS_MODULE,
4952 	.open =         lpfc_idiag_open,
4953 	.llseek =       lpfc_debugfs_lseek,
4954 	.read =         lpfc_idiag_mbxacc_read,
4955 	.write =        lpfc_idiag_mbxacc_write,
4956 	.release =      lpfc_idiag_cmd_release,
4957 };
4958 
4959 #undef lpfc_idiag_op_extAcc
4960 static const struct file_operations lpfc_idiag_op_extAcc = {
4961 	.owner =        THIS_MODULE,
4962 	.open =         lpfc_idiag_open,
4963 	.llseek =       lpfc_debugfs_lseek,
4964 	.read =         lpfc_idiag_extacc_read,
4965 	.write =        lpfc_idiag_extacc_write,
4966 	.release =      lpfc_idiag_cmd_release,
4967 };
4968 
4969 #endif
4970 
4971 /* lpfc_idiag_mbxacc_dump_bsg_mbox - idiag debugfs dump bsg mailbox command
4972  * @phba: Pointer to HBA context object.
4973  * @dmabuf: Pointer to a DMA buffer descriptor.
4974  *
4975  * Description:
4976  * This routine dump a bsg pass-through non-embedded mailbox command with
4977  * external buffer.
4978  **/
4979 void
4980 lpfc_idiag_mbxacc_dump_bsg_mbox(struct lpfc_hba *phba, enum nemb_type nemb_tp,
4981 				enum mbox_type mbox_tp, enum dma_type dma_tp,
4982 				enum sta_type sta_tp,
4983 				struct lpfc_dmabuf *dmabuf, uint32_t ext_buf)
4984 {
4985 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4986 	uint32_t *mbx_mbox_cmd, *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt;
4987 	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
4988 	int len = 0;
4989 	uint32_t do_dump = 0;
4990 	uint32_t *pword;
4991 	uint32_t i;
4992 
4993 	if (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP)
4994 		return;
4995 
4996 	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
4997 	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
4998 	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
4999 	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5000 
5001 	if (!(*mbx_dump_map & LPFC_MBX_DMP_ALL) ||
5002 	    (*mbx_dump_cnt == 0) ||
5003 	    (*mbx_word_cnt == 0))
5004 		return;
5005 
5006 	if (*mbx_mbox_cmd != 0x9B)
5007 		return;
5008 
5009 	if ((mbox_tp == mbox_rd) && (dma_tp == dma_mbox)) {
5010 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_MBX) {
5011 			do_dump |= LPFC_BSG_DMP_MBX_RD_MBX;
5012 			pr_err("\nRead mbox command (x%x), "
5013 			       "nemb:0x%x, extbuf_cnt:%d:\n",
5014 			       sta_tp, nemb_tp, ext_buf);
5015 		}
5016 	}
5017 	if ((mbox_tp == mbox_rd) && (dma_tp == dma_ebuf)) {
5018 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_BUF) {
5019 			do_dump |= LPFC_BSG_DMP_MBX_RD_BUF;
5020 			pr_err("\nRead mbox buffer (x%x), "
5021 			       "nemb:0x%x, extbuf_seq:%d:\n",
5022 			       sta_tp, nemb_tp, ext_buf);
5023 		}
5024 	}
5025 	if ((mbox_tp == mbox_wr) && (dma_tp == dma_mbox)) {
5026 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_MBX) {
5027 			do_dump |= LPFC_BSG_DMP_MBX_WR_MBX;
5028 			pr_err("\nWrite mbox command (x%x), "
5029 			       "nemb:0x%x, extbuf_cnt:%d:\n",
5030 			       sta_tp, nemb_tp, ext_buf);
5031 		}
5032 	}
5033 	if ((mbox_tp == mbox_wr) && (dma_tp == dma_ebuf)) {
5034 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_BUF) {
5035 			do_dump |= LPFC_BSG_DMP_MBX_WR_BUF;
5036 			pr_err("\nWrite mbox buffer (x%x), "
5037 			       "nemb:0x%x, extbuf_seq:%d:\n",
5038 			       sta_tp, nemb_tp, ext_buf);
5039 		}
5040 	}
5041 
5042 	/* dump buffer content */
5043 	if (do_dump) {
5044 		pword = (uint32_t *)dmabuf->virt;
5045 		for (i = 0; i < *mbx_word_cnt; i++) {
5046 			if (!(i % 8)) {
5047 				if (i != 0)
5048 					pr_err("%s\n", line_buf);
5049 				len = 0;
5050 				len += snprintf(line_buf+len,
5051 						LPFC_MBX_ACC_LBUF_SZ-len,
5052 						"%03d: ", i);
5053 			}
5054 			len += snprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
5055 					"%08x ", (uint32_t)*pword);
5056 			pword++;
5057 		}
5058 		if ((i - 1) % 8)
5059 			pr_err("%s\n", line_buf);
5060 		(*mbx_dump_cnt)--;
5061 	}
5062 
5063 	/* Clean out command structure on reaching dump count */
5064 	if (*mbx_dump_cnt == 0)
5065 		memset(&idiag, 0, sizeof(idiag));
5066 	return;
5067 #endif
5068 }
5069 
5070 /* lpfc_idiag_mbxacc_dump_issue_mbox - idiag debugfs dump issue mailbox command
5071  * @phba: Pointer to HBA context object.
5072  * @dmabuf: Pointer to a DMA buffer descriptor.
5073  *
5074  * Description:
5075  * This routine dump a pass-through non-embedded mailbox command from issue
5076  * mailbox command.
5077  **/
5078 void
5079 lpfc_idiag_mbxacc_dump_issue_mbox(struct lpfc_hba *phba, MAILBOX_t *pmbox)
5080 {
5081 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5082 	uint32_t *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt, *mbx_mbox_cmd;
5083 	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
5084 	int len = 0;
5085 	uint32_t *pword;
5086 	uint8_t *pbyte;
5087 	uint32_t i, j;
5088 
5089 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP)
5090 		return;
5091 
5092 	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5093 	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5094 	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5095 	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5096 
5097 	if (!(*mbx_dump_map & LPFC_MBX_DMP_MBX_ALL) ||
5098 	    (*mbx_dump_cnt == 0) ||
5099 	    (*mbx_word_cnt == 0))
5100 		return;
5101 
5102 	if ((*mbx_mbox_cmd != LPFC_MBX_ALL_CMD) &&
5103 	    (*mbx_mbox_cmd != pmbox->mbxCommand))
5104 		return;
5105 
5106 	/* dump buffer content */
5107 	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_WORD) {
5108 		pr_err("Mailbox command:0x%x dump by word:\n",
5109 		       pmbox->mbxCommand);
5110 		pword = (uint32_t *)pmbox;
5111 		for (i = 0; i < *mbx_word_cnt; i++) {
5112 			if (!(i % 8)) {
5113 				if (i != 0)
5114 					pr_err("%s\n", line_buf);
5115 				len = 0;
5116 				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
5117 				len += snprintf(line_buf+len,
5118 						LPFC_MBX_ACC_LBUF_SZ-len,
5119 						"%03d: ", i);
5120 			}
5121 			len += snprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
5122 					"%08x ",
5123 					((uint32_t)*pword) & 0xffffffff);
5124 			pword++;
5125 		}
5126 		if ((i - 1) % 8)
5127 			pr_err("%s\n", line_buf);
5128 		pr_err("\n");
5129 	}
5130 	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_BYTE) {
5131 		pr_err("Mailbox command:0x%x dump by byte:\n",
5132 		       pmbox->mbxCommand);
5133 		pbyte = (uint8_t *)pmbox;
5134 		for (i = 0; i < *mbx_word_cnt; i++) {
5135 			if (!(i % 8)) {
5136 				if (i != 0)
5137 					pr_err("%s\n", line_buf);
5138 				len = 0;
5139 				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
5140 				len += snprintf(line_buf+len,
5141 						LPFC_MBX_ACC_LBUF_SZ-len,
5142 						"%03d: ", i);
5143 			}
5144 			for (j = 0; j < 4; j++) {
5145 				len += snprintf(line_buf+len,
5146 						LPFC_MBX_ACC_LBUF_SZ-len,
5147 						"%02x",
5148 						((uint8_t)*pbyte) & 0xff);
5149 				pbyte++;
5150 			}
5151 			len += snprintf(line_buf+len,
5152 					LPFC_MBX_ACC_LBUF_SZ-len, " ");
5153 		}
5154 		if ((i - 1) % 8)
5155 			pr_err("%s\n", line_buf);
5156 		pr_err("\n");
5157 	}
5158 	(*mbx_dump_cnt)--;
5159 
5160 	/* Clean out command structure on reaching dump count */
5161 	if (*mbx_dump_cnt == 0)
5162 		memset(&idiag, 0, sizeof(idiag));
5163 	return;
5164 #endif
5165 }
5166 
5167 /**
5168  * lpfc_debugfs_initialize - Initialize debugfs for a vport
5169  * @vport: The vport pointer to initialize.
5170  *
5171  * Description:
5172  * When Debugfs is configured this routine sets up the lpfc debugfs file system.
5173  * If not already created, this routine will create the lpfc directory, and
5174  * lpfcX directory (for this HBA), and vportX directory for this vport. It will
5175  * also create each file used to access lpfc specific debugfs information.
5176  **/
5177 inline void
5178 lpfc_debugfs_initialize(struct lpfc_vport *vport)
5179 {
5180 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5181 	struct lpfc_hba   *phba = vport->phba;
5182 	char name[64];
5183 	uint32_t num, i;
5184 	bool pport_setup = false;
5185 
5186 	if (!lpfc_debugfs_enable)
5187 		return;
5188 
5189 	/* Setup lpfc root directory */
5190 	if (!lpfc_debugfs_root) {
5191 		lpfc_debugfs_root = debugfs_create_dir("lpfc", NULL);
5192 		atomic_set(&lpfc_debugfs_hba_count, 0);
5193 		if (!lpfc_debugfs_root) {
5194 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5195 					 "0408 Cannot create debugfs root\n");
5196 			goto debug_failed;
5197 		}
5198 	}
5199 	if (!lpfc_debugfs_start_time)
5200 		lpfc_debugfs_start_time = jiffies;
5201 
5202 	/* Setup funcX directory for specific HBA PCI function */
5203 	snprintf(name, sizeof(name), "fn%d", phba->brd_no);
5204 	if (!phba->hba_debugfs_root) {
5205 		pport_setup = true;
5206 		phba->hba_debugfs_root =
5207 			debugfs_create_dir(name, lpfc_debugfs_root);
5208 		if (!phba->hba_debugfs_root) {
5209 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5210 					 "0412 Cannot create debugfs hba\n");
5211 			goto debug_failed;
5212 		}
5213 		atomic_inc(&lpfc_debugfs_hba_count);
5214 		atomic_set(&phba->debugfs_vport_count, 0);
5215 
5216 		/* Setup hbqinfo */
5217 		snprintf(name, sizeof(name), "hbqinfo");
5218 		phba->debug_hbqinfo =
5219 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5220 				 phba->hba_debugfs_root,
5221 				 phba, &lpfc_debugfs_op_hbqinfo);
5222 		if (!phba->debug_hbqinfo) {
5223 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5224 				"0411 Cannot create debugfs hbqinfo\n");
5225 			goto debug_failed;
5226 		}
5227 
5228 		/* Setup dumpHBASlim */
5229 		if (phba->sli_rev < LPFC_SLI_REV4) {
5230 			snprintf(name, sizeof(name), "dumpHBASlim");
5231 			phba->debug_dumpHBASlim =
5232 				debugfs_create_file(name,
5233 					S_IFREG|S_IRUGO|S_IWUSR,
5234 					phba->hba_debugfs_root,
5235 					phba, &lpfc_debugfs_op_dumpHBASlim);
5236 			if (!phba->debug_dumpHBASlim) {
5237 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5238 						 "0413 Cannot create debugfs "
5239 						"dumpHBASlim\n");
5240 				goto debug_failed;
5241 			}
5242 		} else
5243 			phba->debug_dumpHBASlim = NULL;
5244 
5245 		/* Setup dumpHostSlim */
5246 		if (phba->sli_rev < LPFC_SLI_REV4) {
5247 			snprintf(name, sizeof(name), "dumpHostSlim");
5248 			phba->debug_dumpHostSlim =
5249 				debugfs_create_file(name,
5250 					S_IFREG|S_IRUGO|S_IWUSR,
5251 					phba->hba_debugfs_root,
5252 					phba, &lpfc_debugfs_op_dumpHostSlim);
5253 			if (!phba->debug_dumpHostSlim) {
5254 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5255 						 "0414 Cannot create debugfs "
5256 						 "dumpHostSlim\n");
5257 				goto debug_failed;
5258 			}
5259 		} else
5260 			phba->debug_dumpHostSlim = NULL;
5261 
5262 		/* Setup dumpData */
5263 		snprintf(name, sizeof(name), "dumpData");
5264 		phba->debug_dumpData =
5265 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5266 				 phba->hba_debugfs_root,
5267 				 phba, &lpfc_debugfs_op_dumpData);
5268 		if (!phba->debug_dumpData) {
5269 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5270 				"0800 Cannot create debugfs dumpData\n");
5271 			goto debug_failed;
5272 		}
5273 
5274 		/* Setup dumpDif */
5275 		snprintf(name, sizeof(name), "dumpDif");
5276 		phba->debug_dumpDif =
5277 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5278 				 phba->hba_debugfs_root,
5279 				 phba, &lpfc_debugfs_op_dumpDif);
5280 		if (!phba->debug_dumpDif) {
5281 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5282 				"0801 Cannot create debugfs dumpDif\n");
5283 			goto debug_failed;
5284 		}
5285 
5286 		/* Setup DIF Error Injections */
5287 		snprintf(name, sizeof(name), "InjErrLBA");
5288 		phba->debug_InjErrLBA =
5289 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5290 			phba->hba_debugfs_root,
5291 			phba, &lpfc_debugfs_op_dif_err);
5292 		if (!phba->debug_InjErrLBA) {
5293 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5294 				"0807 Cannot create debugfs InjErrLBA\n");
5295 			goto debug_failed;
5296 		}
5297 		phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
5298 
5299 		snprintf(name, sizeof(name), "InjErrNPortID");
5300 		phba->debug_InjErrNPortID =
5301 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5302 			phba->hba_debugfs_root,
5303 			phba, &lpfc_debugfs_op_dif_err);
5304 		if (!phba->debug_InjErrNPortID) {
5305 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5306 				"0809 Cannot create debugfs InjErrNPortID\n");
5307 			goto debug_failed;
5308 		}
5309 
5310 		snprintf(name, sizeof(name), "InjErrWWPN");
5311 		phba->debug_InjErrWWPN =
5312 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5313 			phba->hba_debugfs_root,
5314 			phba, &lpfc_debugfs_op_dif_err);
5315 		if (!phba->debug_InjErrWWPN) {
5316 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5317 				"0810 Cannot create debugfs InjErrWWPN\n");
5318 			goto debug_failed;
5319 		}
5320 
5321 		snprintf(name, sizeof(name), "writeGuardInjErr");
5322 		phba->debug_writeGuard =
5323 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5324 			phba->hba_debugfs_root,
5325 			phba, &lpfc_debugfs_op_dif_err);
5326 		if (!phba->debug_writeGuard) {
5327 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5328 				"0802 Cannot create debugfs writeGuard\n");
5329 			goto debug_failed;
5330 		}
5331 
5332 		snprintf(name, sizeof(name), "writeAppInjErr");
5333 		phba->debug_writeApp =
5334 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5335 			phba->hba_debugfs_root,
5336 			phba, &lpfc_debugfs_op_dif_err);
5337 		if (!phba->debug_writeApp) {
5338 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5339 				"0803 Cannot create debugfs writeApp\n");
5340 			goto debug_failed;
5341 		}
5342 
5343 		snprintf(name, sizeof(name), "writeRefInjErr");
5344 		phba->debug_writeRef =
5345 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5346 			phba->hba_debugfs_root,
5347 			phba, &lpfc_debugfs_op_dif_err);
5348 		if (!phba->debug_writeRef) {
5349 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5350 				"0804 Cannot create debugfs writeRef\n");
5351 			goto debug_failed;
5352 		}
5353 
5354 		snprintf(name, sizeof(name), "readGuardInjErr");
5355 		phba->debug_readGuard =
5356 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5357 			phba->hba_debugfs_root,
5358 			phba, &lpfc_debugfs_op_dif_err);
5359 		if (!phba->debug_readGuard) {
5360 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5361 				"0808 Cannot create debugfs readGuard\n");
5362 			goto debug_failed;
5363 		}
5364 
5365 		snprintf(name, sizeof(name), "readAppInjErr");
5366 		phba->debug_readApp =
5367 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5368 			phba->hba_debugfs_root,
5369 			phba, &lpfc_debugfs_op_dif_err);
5370 		if (!phba->debug_readApp) {
5371 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5372 				"0805 Cannot create debugfs readApp\n");
5373 			goto debug_failed;
5374 		}
5375 
5376 		snprintf(name, sizeof(name), "readRefInjErr");
5377 		phba->debug_readRef =
5378 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5379 			phba->hba_debugfs_root,
5380 			phba, &lpfc_debugfs_op_dif_err);
5381 		if (!phba->debug_readRef) {
5382 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5383 				"0806 Cannot create debugfs readApp\n");
5384 			goto debug_failed;
5385 		}
5386 
5387 		/* Setup slow ring trace */
5388 		if (lpfc_debugfs_max_slow_ring_trc) {
5389 			num = lpfc_debugfs_max_slow_ring_trc - 1;
5390 			if (num & lpfc_debugfs_max_slow_ring_trc) {
5391 				/* Change to be a power of 2 */
5392 				num = lpfc_debugfs_max_slow_ring_trc;
5393 				i = 0;
5394 				while (num > 1) {
5395 					num = num >> 1;
5396 					i++;
5397 				}
5398 				lpfc_debugfs_max_slow_ring_trc = (1 << i);
5399 				pr_err("lpfc_debugfs_max_disc_trc changed to "
5400 				       "%d\n", lpfc_debugfs_max_disc_trc);
5401 			}
5402 		}
5403 
5404 		snprintf(name, sizeof(name), "slow_ring_trace");
5405 		phba->debug_slow_ring_trc =
5406 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5407 				 phba->hba_debugfs_root,
5408 				 phba, &lpfc_debugfs_op_slow_ring_trc);
5409 		if (!phba->debug_slow_ring_trc) {
5410 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5411 					 "0415 Cannot create debugfs "
5412 					 "slow_ring_trace\n");
5413 			goto debug_failed;
5414 		}
5415 		if (!phba->slow_ring_trc) {
5416 			phba->slow_ring_trc = kmalloc(
5417 				(sizeof(struct lpfc_debugfs_trc) *
5418 				lpfc_debugfs_max_slow_ring_trc),
5419 				GFP_KERNEL);
5420 			if (!phba->slow_ring_trc) {
5421 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5422 						 "0416 Cannot create debugfs "
5423 						 "slow_ring buffer\n");
5424 				goto debug_failed;
5425 			}
5426 			atomic_set(&phba->slow_ring_trc_cnt, 0);
5427 			memset(phba->slow_ring_trc, 0,
5428 				(sizeof(struct lpfc_debugfs_trc) *
5429 				lpfc_debugfs_max_slow_ring_trc));
5430 		}
5431 
5432 		snprintf(name, sizeof(name), "nvmeio_trc");
5433 		phba->debug_nvmeio_trc =
5434 			debugfs_create_file(name, 0644,
5435 					    phba->hba_debugfs_root,
5436 					    phba, &lpfc_debugfs_op_nvmeio_trc);
5437 		if (!phba->debug_nvmeio_trc) {
5438 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5439 					 "0574 No create debugfs nvmeio_trc\n");
5440 			goto debug_failed;
5441 		}
5442 
5443 		atomic_set(&phba->nvmeio_trc_cnt, 0);
5444 		if (lpfc_debugfs_max_nvmeio_trc) {
5445 			num = lpfc_debugfs_max_nvmeio_trc - 1;
5446 			if (num & lpfc_debugfs_max_disc_trc) {
5447 				/* Change to be a power of 2 */
5448 				num = lpfc_debugfs_max_nvmeio_trc;
5449 				i = 0;
5450 				while (num > 1) {
5451 					num = num >> 1;
5452 					i++;
5453 				}
5454 				lpfc_debugfs_max_nvmeio_trc = (1 << i);
5455 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5456 						"0575 lpfc_debugfs_max_nvmeio_trc "
5457 						"changed to %d\n",
5458 						lpfc_debugfs_max_nvmeio_trc);
5459 			}
5460 			phba->nvmeio_trc_size = lpfc_debugfs_max_nvmeio_trc;
5461 
5462 			/* Allocate trace buffer and initialize */
5463 			phba->nvmeio_trc = kmalloc(
5464 				(sizeof(struct lpfc_debugfs_nvmeio_trc) *
5465 				phba->nvmeio_trc_size), GFP_KERNEL);
5466 
5467 			if (!phba->nvmeio_trc) {
5468 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5469 						"0576 Cannot create debugfs "
5470 						"nvmeio_trc buffer\n");
5471 				goto nvmeio_off;
5472 			}
5473 			memset(phba->nvmeio_trc, 0,
5474 			       (sizeof(struct lpfc_debugfs_nvmeio_trc) *
5475 			       phba->nvmeio_trc_size));
5476 			phba->nvmeio_trc_on = 1;
5477 			phba->nvmeio_trc_output_idx = 0;
5478 			phba->nvmeio_trc = NULL;
5479 		} else {
5480 nvmeio_off:
5481 			phba->nvmeio_trc_size = 0;
5482 			phba->nvmeio_trc_on = 0;
5483 			phba->nvmeio_trc_output_idx = 0;
5484 			phba->nvmeio_trc = NULL;
5485 		}
5486 	}
5487 
5488 	snprintf(name, sizeof(name), "vport%d", vport->vpi);
5489 	if (!vport->vport_debugfs_root) {
5490 		vport->vport_debugfs_root =
5491 			debugfs_create_dir(name, phba->hba_debugfs_root);
5492 		if (!vport->vport_debugfs_root) {
5493 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5494 					 "0417 Can't create debugfs\n");
5495 			goto debug_failed;
5496 		}
5497 		atomic_inc(&phba->debugfs_vport_count);
5498 	}
5499 
5500 	if (lpfc_debugfs_max_disc_trc) {
5501 		num = lpfc_debugfs_max_disc_trc - 1;
5502 		if (num & lpfc_debugfs_max_disc_trc) {
5503 			/* Change to be a power of 2 */
5504 			num = lpfc_debugfs_max_disc_trc;
5505 			i = 0;
5506 			while (num > 1) {
5507 				num = num >> 1;
5508 				i++;
5509 			}
5510 			lpfc_debugfs_max_disc_trc = (1 << i);
5511 			pr_err("lpfc_debugfs_max_disc_trc changed to %d\n",
5512 			       lpfc_debugfs_max_disc_trc);
5513 		}
5514 	}
5515 
5516 	vport->disc_trc = kzalloc(
5517 		(sizeof(struct lpfc_debugfs_trc) * lpfc_debugfs_max_disc_trc),
5518 		GFP_KERNEL);
5519 
5520 	if (!vport->disc_trc) {
5521 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5522 				 "0418 Cannot create debugfs disc trace "
5523 				 "buffer\n");
5524 		goto debug_failed;
5525 	}
5526 	atomic_set(&vport->disc_trc_cnt, 0);
5527 
5528 	snprintf(name, sizeof(name), "discovery_trace");
5529 	vport->debug_disc_trc =
5530 		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5531 				 vport->vport_debugfs_root,
5532 				 vport, &lpfc_debugfs_op_disc_trc);
5533 	if (!vport->debug_disc_trc) {
5534 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5535 				 "0419 Cannot create debugfs "
5536 				 "discovery_trace\n");
5537 		goto debug_failed;
5538 	}
5539 	snprintf(name, sizeof(name), "nodelist");
5540 	vport->debug_nodelist =
5541 		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5542 				 vport->vport_debugfs_root,
5543 				 vport, &lpfc_debugfs_op_nodelist);
5544 	if (!vport->debug_nodelist) {
5545 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5546 				 "2985 Can't create debugfs nodelist\n");
5547 		goto debug_failed;
5548 	}
5549 
5550 	snprintf(name, sizeof(name), "nvmestat");
5551 	vport->debug_nvmestat =
5552 		debugfs_create_file(name, 0644,
5553 				    vport->vport_debugfs_root,
5554 				    vport, &lpfc_debugfs_op_nvmestat);
5555 	if (!vport->debug_nvmestat) {
5556 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5557 				 "0811 Cannot create debugfs nvmestat\n");
5558 		goto debug_failed;
5559 	}
5560 
5561 	snprintf(name, sizeof(name), "nvmektime");
5562 	vport->debug_nvmektime =
5563 		debugfs_create_file(name, 0644,
5564 				    vport->vport_debugfs_root,
5565 				    vport, &lpfc_debugfs_op_nvmektime);
5566 	if (!vport->debug_nvmektime) {
5567 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5568 				 "0815 Cannot create debugfs nvmektime\n");
5569 		goto debug_failed;
5570 	}
5571 
5572 	snprintf(name, sizeof(name), "cpucheck");
5573 	vport->debug_cpucheck =
5574 		debugfs_create_file(name, 0644,
5575 				    vport->vport_debugfs_root,
5576 				    vport, &lpfc_debugfs_op_cpucheck);
5577 	if (!vport->debug_cpucheck) {
5578 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5579 				 "0819 Cannot create debugfs cpucheck\n");
5580 		goto debug_failed;
5581 	}
5582 
5583 	/*
5584 	 * The following section is for additional directories/files for the
5585 	 * physical port.
5586 	 */
5587 
5588 	if (!pport_setup)
5589 		goto debug_failed;
5590 
5591 	/*
5592 	 * iDiag debugfs root entry points for SLI4 device only
5593 	 */
5594 	if (phba->sli_rev < LPFC_SLI_REV4)
5595 		goto debug_failed;
5596 
5597 	snprintf(name, sizeof(name), "iDiag");
5598 	if (!phba->idiag_root) {
5599 		phba->idiag_root =
5600 			debugfs_create_dir(name, phba->hba_debugfs_root);
5601 		if (!phba->idiag_root) {
5602 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5603 					 "2922 Can't create idiag debugfs\n");
5604 			goto debug_failed;
5605 		}
5606 		/* Initialize iDiag data structure */
5607 		memset(&idiag, 0, sizeof(idiag));
5608 	}
5609 
5610 	/* iDiag read PCI config space */
5611 	snprintf(name, sizeof(name), "pciCfg");
5612 	if (!phba->idiag_pci_cfg) {
5613 		phba->idiag_pci_cfg =
5614 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5615 				phba->idiag_root, phba, &lpfc_idiag_op_pciCfg);
5616 		if (!phba->idiag_pci_cfg) {
5617 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5618 					 "2923 Can't create idiag debugfs\n");
5619 			goto debug_failed;
5620 		}
5621 		idiag.offset.last_rd = 0;
5622 	}
5623 
5624 	/* iDiag PCI BAR access */
5625 	snprintf(name, sizeof(name), "barAcc");
5626 	if (!phba->idiag_bar_acc) {
5627 		phba->idiag_bar_acc =
5628 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5629 				phba->idiag_root, phba, &lpfc_idiag_op_barAcc);
5630 		if (!phba->idiag_bar_acc) {
5631 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5632 					"3056 Can't create idiag debugfs\n");
5633 			goto debug_failed;
5634 		}
5635 		idiag.offset.last_rd = 0;
5636 	}
5637 
5638 	/* iDiag get PCI function queue information */
5639 	snprintf(name, sizeof(name), "queInfo");
5640 	if (!phba->idiag_que_info) {
5641 		phba->idiag_que_info =
5642 			debugfs_create_file(name, S_IFREG|S_IRUGO,
5643 			phba->idiag_root, phba, &lpfc_idiag_op_queInfo);
5644 		if (!phba->idiag_que_info) {
5645 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5646 					 "2924 Can't create idiag debugfs\n");
5647 			goto debug_failed;
5648 		}
5649 	}
5650 
5651 	/* iDiag access PCI function queue */
5652 	snprintf(name, sizeof(name), "queAcc");
5653 	if (!phba->idiag_que_acc) {
5654 		phba->idiag_que_acc =
5655 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5656 				phba->idiag_root, phba, &lpfc_idiag_op_queAcc);
5657 		if (!phba->idiag_que_acc) {
5658 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5659 					 "2926 Can't create idiag debugfs\n");
5660 			goto debug_failed;
5661 		}
5662 	}
5663 
5664 	/* iDiag access PCI function doorbell registers */
5665 	snprintf(name, sizeof(name), "drbAcc");
5666 	if (!phba->idiag_drb_acc) {
5667 		phba->idiag_drb_acc =
5668 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5669 				phba->idiag_root, phba, &lpfc_idiag_op_drbAcc);
5670 		if (!phba->idiag_drb_acc) {
5671 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5672 					 "2927 Can't create idiag debugfs\n");
5673 			goto debug_failed;
5674 		}
5675 	}
5676 
5677 	/* iDiag access PCI function control registers */
5678 	snprintf(name, sizeof(name), "ctlAcc");
5679 	if (!phba->idiag_ctl_acc) {
5680 		phba->idiag_ctl_acc =
5681 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5682 				phba->idiag_root, phba, &lpfc_idiag_op_ctlAcc);
5683 		if (!phba->idiag_ctl_acc) {
5684 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5685 					 "2981 Can't create idiag debugfs\n");
5686 			goto debug_failed;
5687 		}
5688 	}
5689 
5690 	/* iDiag access mbox commands */
5691 	snprintf(name, sizeof(name), "mbxAcc");
5692 	if (!phba->idiag_mbx_acc) {
5693 		phba->idiag_mbx_acc =
5694 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5695 				phba->idiag_root, phba, &lpfc_idiag_op_mbxAcc);
5696 		if (!phba->idiag_mbx_acc) {
5697 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5698 					"2980 Can't create idiag debugfs\n");
5699 			goto debug_failed;
5700 		}
5701 	}
5702 
5703 	/* iDiag extents access commands */
5704 	if (phba->sli4_hba.extents_in_use) {
5705 		snprintf(name, sizeof(name), "extAcc");
5706 		if (!phba->idiag_ext_acc) {
5707 			phba->idiag_ext_acc =
5708 				debugfs_create_file(name,
5709 						    S_IFREG|S_IRUGO|S_IWUSR,
5710 						    phba->idiag_root, phba,
5711 						    &lpfc_idiag_op_extAcc);
5712 			if (!phba->idiag_ext_acc) {
5713 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5714 						"2986 Cant create "
5715 						"idiag debugfs\n");
5716 				goto debug_failed;
5717 			}
5718 		}
5719 	}
5720 
5721 debug_failed:
5722 	return;
5723 #endif
5724 }
5725 
5726 /**
5727  * lpfc_debugfs_terminate -  Tear down debugfs infrastructure for this vport
5728  * @vport: The vport pointer to remove from debugfs.
5729  *
5730  * Description:
5731  * When Debugfs is configured this routine removes debugfs file system elements
5732  * that are specific to this vport. It also checks to see if there are any
5733  * users left for the debugfs directories associated with the HBA and driver. If
5734  * this is the last user of the HBA directory or driver directory then it will
5735  * remove those from the debugfs infrastructure as well.
5736  **/
5737 inline void
5738 lpfc_debugfs_terminate(struct lpfc_vport *vport)
5739 {
5740 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5741 	struct lpfc_hba   *phba = vport->phba;
5742 
5743 	kfree(vport->disc_trc);
5744 	vport->disc_trc = NULL;
5745 
5746 	debugfs_remove(vport->debug_disc_trc); /* discovery_trace */
5747 	vport->debug_disc_trc = NULL;
5748 
5749 	debugfs_remove(vport->debug_nodelist); /* nodelist */
5750 	vport->debug_nodelist = NULL;
5751 
5752 	debugfs_remove(vport->debug_nvmestat); /* nvmestat */
5753 	vport->debug_nvmestat = NULL;
5754 
5755 	debugfs_remove(vport->debug_nvmektime); /* nvmektime */
5756 	vport->debug_nvmektime = NULL;
5757 
5758 	debugfs_remove(vport->debug_cpucheck); /* cpucheck */
5759 	vport->debug_cpucheck = NULL;
5760 
5761 	if (vport->vport_debugfs_root) {
5762 		debugfs_remove(vport->vport_debugfs_root); /* vportX */
5763 		vport->vport_debugfs_root = NULL;
5764 		atomic_dec(&phba->debugfs_vport_count);
5765 	}
5766 
5767 	if (atomic_read(&phba->debugfs_vport_count) == 0) {
5768 
5769 		debugfs_remove(phba->debug_hbqinfo); /* hbqinfo */
5770 		phba->debug_hbqinfo = NULL;
5771 
5772 		debugfs_remove(phba->debug_dumpHBASlim); /* HBASlim */
5773 		phba->debug_dumpHBASlim = NULL;
5774 
5775 		debugfs_remove(phba->debug_dumpHostSlim); /* HostSlim */
5776 		phba->debug_dumpHostSlim = NULL;
5777 
5778 		debugfs_remove(phba->debug_dumpData); /* dumpData */
5779 		phba->debug_dumpData = NULL;
5780 
5781 		debugfs_remove(phba->debug_dumpDif); /* dumpDif */
5782 		phba->debug_dumpDif = NULL;
5783 
5784 		debugfs_remove(phba->debug_InjErrLBA); /* InjErrLBA */
5785 		phba->debug_InjErrLBA = NULL;
5786 
5787 		debugfs_remove(phba->debug_InjErrNPortID);
5788 		phba->debug_InjErrNPortID = NULL;
5789 
5790 		debugfs_remove(phba->debug_InjErrWWPN); /* InjErrWWPN */
5791 		phba->debug_InjErrWWPN = NULL;
5792 
5793 		debugfs_remove(phba->debug_writeGuard); /* writeGuard */
5794 		phba->debug_writeGuard = NULL;
5795 
5796 		debugfs_remove(phba->debug_writeApp); /* writeApp */
5797 		phba->debug_writeApp = NULL;
5798 
5799 		debugfs_remove(phba->debug_writeRef); /* writeRef */
5800 		phba->debug_writeRef = NULL;
5801 
5802 		debugfs_remove(phba->debug_readGuard); /* readGuard */
5803 		phba->debug_readGuard = NULL;
5804 
5805 		debugfs_remove(phba->debug_readApp); /* readApp */
5806 		phba->debug_readApp = NULL;
5807 
5808 		debugfs_remove(phba->debug_readRef); /* readRef */
5809 		phba->debug_readRef = NULL;
5810 
5811 		kfree(phba->slow_ring_trc);
5812 		phba->slow_ring_trc = NULL;
5813 
5814 		/* slow_ring_trace */
5815 		debugfs_remove(phba->debug_slow_ring_trc);
5816 		phba->debug_slow_ring_trc = NULL;
5817 
5818 		debugfs_remove(phba->debug_nvmeio_trc);
5819 		phba->debug_nvmeio_trc = NULL;
5820 
5821 		kfree(phba->nvmeio_trc);
5822 		phba->nvmeio_trc = NULL;
5823 
5824 		/*
5825 		 * iDiag release
5826 		 */
5827 		if (phba->sli_rev == LPFC_SLI_REV4) {
5828 			/* iDiag extAcc */
5829 			debugfs_remove(phba->idiag_ext_acc);
5830 			phba->idiag_ext_acc = NULL;
5831 
5832 			/* iDiag mbxAcc */
5833 			debugfs_remove(phba->idiag_mbx_acc);
5834 			phba->idiag_mbx_acc = NULL;
5835 
5836 			/* iDiag ctlAcc */
5837 			debugfs_remove(phba->idiag_ctl_acc);
5838 			phba->idiag_ctl_acc = NULL;
5839 
5840 			/* iDiag drbAcc */
5841 			debugfs_remove(phba->idiag_drb_acc);
5842 			phba->idiag_drb_acc = NULL;
5843 
5844 			/* iDiag queAcc */
5845 			debugfs_remove(phba->idiag_que_acc);
5846 			phba->idiag_que_acc = NULL;
5847 
5848 			/* iDiag queInfo */
5849 			debugfs_remove(phba->idiag_que_info);
5850 			phba->idiag_que_info = NULL;
5851 
5852 			/* iDiag barAcc */
5853 			debugfs_remove(phba->idiag_bar_acc);
5854 			phba->idiag_bar_acc = NULL;
5855 
5856 			/* iDiag pciCfg */
5857 			debugfs_remove(phba->idiag_pci_cfg);
5858 			phba->idiag_pci_cfg = NULL;
5859 
5860 			/* Finally remove the iDiag debugfs root */
5861 			debugfs_remove(phba->idiag_root);
5862 			phba->idiag_root = NULL;
5863 		}
5864 
5865 		if (phba->hba_debugfs_root) {
5866 			debugfs_remove(phba->hba_debugfs_root); /* fnX */
5867 			phba->hba_debugfs_root = NULL;
5868 			atomic_dec(&lpfc_debugfs_hba_count);
5869 		}
5870 
5871 		if (atomic_read(&lpfc_debugfs_hba_count) == 0) {
5872 			debugfs_remove(lpfc_debugfs_root); /* lpfc */
5873 			lpfc_debugfs_root = NULL;
5874 		}
5875 	}
5876 #endif
5877 	return;
5878 }
5879 
5880 /*
5881  * Driver debug utility routines outside of debugfs. The debug utility
5882  * routines implemented here is intended to be used in the instrumented
5883  * debug driver for debugging host or port issues.
5884  */
5885 
5886 /**
5887  * lpfc_debug_dump_all_queues - dump all the queues with a hba
5888  * @phba: Pointer to HBA context object.
5889  *
5890  * This function dumps entries of all the queues asociated with the @phba.
5891  **/
5892 void
5893 lpfc_debug_dump_all_queues(struct lpfc_hba *phba)
5894 {
5895 	int idx;
5896 
5897 	/*
5898 	 * Dump Work Queues (WQs)
5899 	 */
5900 	lpfc_debug_dump_wq(phba, DUMP_MBX, 0);
5901 	lpfc_debug_dump_wq(phba, DUMP_ELS, 0);
5902 	lpfc_debug_dump_wq(phba, DUMP_NVMELS, 0);
5903 
5904 	for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
5905 		lpfc_debug_dump_wq(phba, DUMP_FCP, idx);
5906 
5907 	for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
5908 		lpfc_debug_dump_wq(phba, DUMP_NVME, idx);
5909 
5910 	lpfc_debug_dump_hdr_rq(phba);
5911 	lpfc_debug_dump_dat_rq(phba);
5912 	/*
5913 	 * Dump Complete Queues (CQs)
5914 	 */
5915 	lpfc_debug_dump_cq(phba, DUMP_MBX, 0);
5916 	lpfc_debug_dump_cq(phba, DUMP_ELS, 0);
5917 	lpfc_debug_dump_cq(phba, DUMP_NVMELS, 0);
5918 
5919 	for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
5920 		lpfc_debug_dump_cq(phba, DUMP_FCP, idx);
5921 
5922 	for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
5923 		lpfc_debug_dump_cq(phba, DUMP_NVME, idx);
5924 
5925 	/*
5926 	 * Dump Event Queues (EQs)
5927 	 */
5928 	for (idx = 0; idx < phba->io_channel_irqs; idx++)
5929 		lpfc_debug_dump_hba_eq(phba, idx);
5930 }
5931