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