xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_debugfs.c (revision 1ab142d4)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2007-2011 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  *                                                                 *
8  * This program is free software; you can redistribute it and/or   *
9  * modify it under the terms of version 2 of the GNU General       *
10  * Public License as published by the Free Software Foundation.    *
11  * This program is distributed in the hope that it will be useful. *
12  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
13  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
14  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
15  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
17  * more details, a copy of which can be found in the file COPYING  *
18  * included with this package.                                     *
19  *******************************************************************/
20 
21 #include <linux/blkdev.h>
22 #include <linux/delay.h>
23 #include <linux/module.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/idr.h>
26 #include <linux/interrupt.h>
27 #include <linux/kthread.h>
28 #include <linux/slab.h>
29 #include <linux/pci.h>
30 #include <linux/spinlock.h>
31 #include <linux/ctype.h>
32 
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_host.h>
36 #include <scsi/scsi_transport_fc.h>
37 
38 #include "lpfc_hw4.h"
39 #include "lpfc_hw.h"
40 #include "lpfc_sli.h"
41 #include "lpfc_sli4.h"
42 #include "lpfc_nl.h"
43 #include "lpfc_disc.h"
44 #include "lpfc_scsi.h"
45 #include "lpfc.h"
46 #include "lpfc_logmsg.h"
47 #include "lpfc_crtn.h"
48 #include "lpfc_vport.h"
49 #include "lpfc_version.h"
50 #include "lpfc_compat.h"
51 #include "lpfc_debugfs.h"
52 #include "lpfc_bsg.h"
53 
54 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
55 /*
56  * debugfs interface
57  *
58  * To access this interface the user should:
59  * # mount -t debugfs none /sys/kernel/debug
60  *
61  * The lpfc debugfs directory hierarchy is:
62  * /sys/kernel/debug/lpfc/fnX/vportY
63  * where X is the lpfc hba function unique_id
64  * where Y is the vport VPI on that hba
65  *
66  * Debugging services available per vport:
67  * discovery_trace
68  * This is an ACSII readable file that contains a trace of the last
69  * lpfc_debugfs_max_disc_trc events that happened on a specific vport.
70  * See lpfc_debugfs.h for different categories of  discovery events.
71  * To enable the discovery trace, the following module parameters must be set:
72  * lpfc_debugfs_enable=1         Turns on lpfc debugfs filesystem support
73  * lpfc_debugfs_max_disc_trc=X   Where X is the event trace depth for
74  *                               EACH vport. X MUST also be a power of 2.
75  * lpfc_debugfs_mask_disc_trc=Y  Where Y is an event mask as defined in
76  *                               lpfc_debugfs.h .
77  *
78  * slow_ring_trace
79  * This is an ACSII readable file that contains a trace of the last
80  * lpfc_debugfs_max_slow_ring_trc events that happened on a specific HBA.
81  * To enable the slow ring trace, the following module parameters must be set:
82  * lpfc_debugfs_enable=1         Turns on lpfc debugfs filesystem support
83  * lpfc_debugfs_max_slow_ring_trc=X   Where X is the event trace depth for
84  *                               the HBA. X MUST also be a power of 2.
85  */
86 static int lpfc_debugfs_enable = 1;
87 module_param(lpfc_debugfs_enable, int, S_IRUGO);
88 MODULE_PARM_DESC(lpfc_debugfs_enable, "Enable debugfs services");
89 
90 /* This MUST be a power of 2 */
91 static int lpfc_debugfs_max_disc_trc;
92 module_param(lpfc_debugfs_max_disc_trc, int, S_IRUGO);
93 MODULE_PARM_DESC(lpfc_debugfs_max_disc_trc,
94 	"Set debugfs discovery trace depth");
95 
96 /* This MUST be a power of 2 */
97 static int lpfc_debugfs_max_slow_ring_trc;
98 module_param(lpfc_debugfs_max_slow_ring_trc, int, S_IRUGO);
99 MODULE_PARM_DESC(lpfc_debugfs_max_slow_ring_trc,
100 	"Set debugfs slow ring trace depth");
101 
102 static int lpfc_debugfs_mask_disc_trc;
103 module_param(lpfc_debugfs_mask_disc_trc, int, S_IRUGO);
104 MODULE_PARM_DESC(lpfc_debugfs_mask_disc_trc,
105 	"Set debugfs discovery trace mask");
106 
107 #include <linux/debugfs.h>
108 
109 static atomic_t lpfc_debugfs_seq_trc_cnt = ATOMIC_INIT(0);
110 static unsigned long lpfc_debugfs_start_time = 0L;
111 
112 /* iDiag */
113 static struct lpfc_idiag idiag;
114 
115 /**
116  * lpfc_debugfs_disc_trc_data - Dump discovery logging to a buffer
117  * @vport: The vport to gather the log info from.
118  * @buf: The buffer to dump log into.
119  * @size: The maximum amount of data to process.
120  *
121  * Description:
122  * This routine gathers the lpfc discovery debugfs data from the @vport and
123  * dumps it to @buf up to @size number of bytes. It will start at the next entry
124  * in the log and process the log until the end of the buffer. Then it will
125  * gather from the beginning of the log and process until the current entry.
126  *
127  * Notes:
128  * Discovery logging will be disabled while while this routine dumps the log.
129  *
130  * Return Value:
131  * This routine returns the amount of bytes that were dumped into @buf and will
132  * not exceed @size.
133  **/
134 static int
135 lpfc_debugfs_disc_trc_data(struct lpfc_vport *vport, char *buf, int size)
136 {
137 	int i, index, len, enable;
138 	uint32_t ms;
139 	struct lpfc_debugfs_trc *dtp;
140 	char *buffer;
141 
142 	buffer = kmalloc(LPFC_DEBUG_TRC_ENTRY_SIZE, GFP_KERNEL);
143 	if (!buffer)
144 		return 0;
145 
146 	enable = lpfc_debugfs_enable;
147 	lpfc_debugfs_enable = 0;
148 
149 	len = 0;
150 	index = (atomic_read(&vport->disc_trc_cnt) + 1) &
151 		(lpfc_debugfs_max_disc_trc - 1);
152 	for (i = index; i < lpfc_debugfs_max_disc_trc; i++) {
153 		dtp = vport->disc_trc + i;
154 		if (!dtp->fmt)
155 			continue;
156 		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
157 		snprintf(buffer,
158 			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
159 			dtp->seq_cnt, ms, dtp->fmt);
160 		len +=  snprintf(buf+len, size-len, buffer,
161 			dtp->data1, dtp->data2, dtp->data3);
162 	}
163 	for (i = 0; i < index; i++) {
164 		dtp = vport->disc_trc + i;
165 		if (!dtp->fmt)
166 			continue;
167 		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
168 		snprintf(buffer,
169 			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
170 			dtp->seq_cnt, ms, dtp->fmt);
171 		len +=  snprintf(buf+len, size-len, buffer,
172 			dtp->data1, dtp->data2, dtp->data3);
173 	}
174 
175 	lpfc_debugfs_enable = enable;
176 	kfree(buffer);
177 
178 	return len;
179 }
180 
181 /**
182  * lpfc_debugfs_slow_ring_trc_data - Dump slow ring logging to a buffer
183  * @phba: The HBA to gather the log info from.
184  * @buf: The buffer to dump log into.
185  * @size: The maximum amount of data to process.
186  *
187  * Description:
188  * This routine gathers the lpfc slow ring debugfs data from the @phba and
189  * dumps it to @buf up to @size number of bytes. It will start at the next entry
190  * in the log and process the log until the end of the buffer. Then it will
191  * gather from the beginning of the log and process until the current entry.
192  *
193  * Notes:
194  * Slow ring logging will be disabled while while this routine dumps the log.
195  *
196  * Return Value:
197  * This routine returns the amount of bytes that were dumped into @buf and will
198  * not exceed @size.
199  **/
200 static int
201 lpfc_debugfs_slow_ring_trc_data(struct lpfc_hba *phba, char *buf, int size)
202 {
203 	int i, index, len, enable;
204 	uint32_t ms;
205 	struct lpfc_debugfs_trc *dtp;
206 	char *buffer;
207 
208 	buffer = kmalloc(LPFC_DEBUG_TRC_ENTRY_SIZE, GFP_KERNEL);
209 	if (!buffer)
210 		return 0;
211 
212 	enable = lpfc_debugfs_enable;
213 	lpfc_debugfs_enable = 0;
214 
215 	len = 0;
216 	index = (atomic_read(&phba->slow_ring_trc_cnt) + 1) &
217 		(lpfc_debugfs_max_slow_ring_trc - 1);
218 	for (i = index; i < lpfc_debugfs_max_slow_ring_trc; i++) {
219 		dtp = phba->slow_ring_trc + i;
220 		if (!dtp->fmt)
221 			continue;
222 		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
223 		snprintf(buffer,
224 			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
225 			dtp->seq_cnt, ms, dtp->fmt);
226 		len +=  snprintf(buf+len, size-len, buffer,
227 			dtp->data1, dtp->data2, dtp->data3);
228 	}
229 	for (i = 0; i < index; i++) {
230 		dtp = phba->slow_ring_trc + i;
231 		if (!dtp->fmt)
232 			continue;
233 		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
234 		snprintf(buffer,
235 			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
236 			dtp->seq_cnt, ms, dtp->fmt);
237 		len +=  snprintf(buf+len, size-len, buffer,
238 			dtp->data1, dtp->data2, dtp->data3);
239 	}
240 
241 	lpfc_debugfs_enable = enable;
242 	kfree(buffer);
243 
244 	return len;
245 }
246 
247 static int lpfc_debugfs_last_hbq = -1;
248 
249 /**
250  * lpfc_debugfs_hbqinfo_data - Dump host buffer queue info to a buffer
251  * @phba: The HBA to gather host buffer info from.
252  * @buf: The buffer to dump log into.
253  * @size: The maximum amount of data to process.
254  *
255  * Description:
256  * This routine dumps the host buffer queue info from the @phba to @buf up to
257  * @size number of bytes. A header that describes the current hbq state will be
258  * dumped to @buf first and then info on each hbq entry will be dumped to @buf
259  * until @size bytes have been dumped or all the hbq info has been dumped.
260  *
261  * Notes:
262  * This routine will rotate through each configured HBQ each time called.
263  *
264  * Return Value:
265  * This routine returns the amount of bytes that were dumped into @buf and will
266  * not exceed @size.
267  **/
268 static int
269 lpfc_debugfs_hbqinfo_data(struct lpfc_hba *phba, char *buf, int size)
270 {
271 	int len = 0;
272 	int cnt, i, j, found, posted, low;
273 	uint32_t phys, raw_index, getidx;
274 	struct lpfc_hbq_init *hip;
275 	struct hbq_s *hbqs;
276 	struct lpfc_hbq_entry *hbqe;
277 	struct lpfc_dmabuf *d_buf;
278 	struct hbq_dmabuf *hbq_buf;
279 
280 	if (phba->sli_rev != 3)
281 		return 0;
282 	cnt = LPFC_HBQINFO_SIZE;
283 	spin_lock_irq(&phba->hbalock);
284 
285 	/* toggle between multiple hbqs, if any */
286 	i = lpfc_sli_hbq_count();
287 	if (i > 1) {
288 		 lpfc_debugfs_last_hbq++;
289 		 if (lpfc_debugfs_last_hbq >= i)
290 			lpfc_debugfs_last_hbq = 0;
291 	}
292 	else
293 		lpfc_debugfs_last_hbq = 0;
294 
295 	i = lpfc_debugfs_last_hbq;
296 
297 	len +=  snprintf(buf+len, size-len, "HBQ %d Info\n", i);
298 
299 	hbqs =  &phba->hbqs[i];
300 	posted = 0;
301 	list_for_each_entry(d_buf, &hbqs->hbq_buffer_list, list)
302 		posted++;
303 
304 	hip =  lpfc_hbq_defs[i];
305 	len +=  snprintf(buf+len, size-len,
306 		"idx:%d prof:%d rn:%d bufcnt:%d icnt:%d acnt:%d posted %d\n",
307 		hip->hbq_index, hip->profile, hip->rn,
308 		hip->buffer_count, hip->init_count, hip->add_count, posted);
309 
310 	raw_index = phba->hbq_get[i];
311 	getidx = le32_to_cpu(raw_index);
312 	len +=  snprintf(buf+len, size-len,
313 		"entrys:%d bufcnt:%d Put:%d nPut:%d localGet:%d hbaGet:%d\n",
314 		hbqs->entry_count, hbqs->buffer_count, hbqs->hbqPutIdx,
315 		hbqs->next_hbqPutIdx, hbqs->local_hbqGetIdx, getidx);
316 
317 	hbqe = (struct lpfc_hbq_entry *) phba->hbqs[i].hbq_virt;
318 	for (j=0; j<hbqs->entry_count; j++) {
319 		len +=  snprintf(buf+len, size-len,
320 			"%03d: %08x %04x %05x ", j,
321 			le32_to_cpu(hbqe->bde.addrLow),
322 			le32_to_cpu(hbqe->bde.tus.w),
323 			le32_to_cpu(hbqe->buffer_tag));
324 		i = 0;
325 		found = 0;
326 
327 		/* First calculate if slot has an associated posted buffer */
328 		low = hbqs->hbqPutIdx - posted;
329 		if (low >= 0) {
330 			if ((j >= hbqs->hbqPutIdx) || (j < low)) {
331 				len +=  snprintf(buf+len, size-len, "Unused\n");
332 				goto skipit;
333 			}
334 		}
335 		else {
336 			if ((j >= hbqs->hbqPutIdx) &&
337 				(j < (hbqs->entry_count+low))) {
338 				len +=  snprintf(buf+len, size-len, "Unused\n");
339 				goto skipit;
340 			}
341 		}
342 
343 		/* Get the Buffer info for the posted buffer */
344 		list_for_each_entry(d_buf, &hbqs->hbq_buffer_list, list) {
345 			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
346 			phys = ((uint64_t)hbq_buf->dbuf.phys & 0xffffffff);
347 			if (phys == le32_to_cpu(hbqe->bde.addrLow)) {
348 				len +=  snprintf(buf+len, size-len,
349 					"Buf%d: %p %06x\n", i,
350 					hbq_buf->dbuf.virt, hbq_buf->tag);
351 				found = 1;
352 				break;
353 			}
354 			i++;
355 		}
356 		if (!found) {
357 			len +=  snprintf(buf+len, size-len, "No DMAinfo?\n");
358 		}
359 skipit:
360 		hbqe++;
361 		if (len > LPFC_HBQINFO_SIZE - 54)
362 			break;
363 	}
364 	spin_unlock_irq(&phba->hbalock);
365 	return len;
366 }
367 
368 static int lpfc_debugfs_last_hba_slim_off;
369 
370 /**
371  * lpfc_debugfs_dumpHBASlim_data - Dump HBA SLIM info to a buffer
372  * @phba: The HBA to gather SLIM info from.
373  * @buf: The buffer to dump log into.
374  * @size: The maximum amount of data to process.
375  *
376  * Description:
377  * This routine dumps the current contents of HBA SLIM for the HBA associated
378  * with @phba to @buf up to @size bytes of data. This is the raw HBA SLIM data.
379  *
380  * Notes:
381  * This routine will only dump up to 1024 bytes of data each time called and
382  * should be called multiple times to dump the entire HBA SLIM.
383  *
384  * Return Value:
385  * This routine returns the amount of bytes that were dumped into @buf and will
386  * not exceed @size.
387  **/
388 static int
389 lpfc_debugfs_dumpHBASlim_data(struct lpfc_hba *phba, char *buf, int size)
390 {
391 	int len = 0;
392 	int i, off;
393 	uint32_t *ptr;
394 	char *buffer;
395 
396 	buffer = kmalloc(1024, GFP_KERNEL);
397 	if (!buffer)
398 		return 0;
399 
400 	off = 0;
401 	spin_lock_irq(&phba->hbalock);
402 
403 	len +=  snprintf(buf+len, size-len, "HBA SLIM\n");
404 	lpfc_memcpy_from_slim(buffer,
405 		phba->MBslimaddr + lpfc_debugfs_last_hba_slim_off, 1024);
406 
407 	ptr = (uint32_t *)&buffer[0];
408 	off = lpfc_debugfs_last_hba_slim_off;
409 
410 	/* Set it up for the next time */
411 	lpfc_debugfs_last_hba_slim_off += 1024;
412 	if (lpfc_debugfs_last_hba_slim_off >= 4096)
413 		lpfc_debugfs_last_hba_slim_off = 0;
414 
415 	i = 1024;
416 	while (i > 0) {
417 		len +=  snprintf(buf+len, size-len,
418 		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
419 		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
420 		*(ptr+5), *(ptr+6), *(ptr+7));
421 		ptr += 8;
422 		i -= (8 * sizeof(uint32_t));
423 		off += (8 * sizeof(uint32_t));
424 	}
425 
426 	spin_unlock_irq(&phba->hbalock);
427 	kfree(buffer);
428 
429 	return len;
430 }
431 
432 /**
433  * lpfc_debugfs_dumpHostSlim_data - Dump host SLIM info to a buffer
434  * @phba: The HBA to gather Host SLIM info from.
435  * @buf: The buffer to dump log into.
436  * @size: The maximum amount of data to process.
437  *
438  * Description:
439  * This routine dumps the current contents of host SLIM for the host associated
440  * with @phba to @buf up to @size bytes of data. The dump will contain the
441  * Mailbox, PCB, Rings, and Registers that are located in host memory.
442  *
443  * Return Value:
444  * This routine returns the amount of bytes that were dumped into @buf and will
445  * not exceed @size.
446  **/
447 static int
448 lpfc_debugfs_dumpHostSlim_data(struct lpfc_hba *phba, char *buf, int size)
449 {
450 	int len = 0;
451 	int i, off;
452 	uint32_t word0, word1, word2, word3;
453 	uint32_t *ptr;
454 	struct lpfc_pgp *pgpp;
455 	struct lpfc_sli *psli = &phba->sli;
456 	struct lpfc_sli_ring *pring;
457 
458 	off = 0;
459 	spin_lock_irq(&phba->hbalock);
460 
461 	len +=  snprintf(buf+len, size-len, "SLIM Mailbox\n");
462 	ptr = (uint32_t *)phba->slim2p.virt;
463 	i = sizeof(MAILBOX_t);
464 	while (i > 0) {
465 		len +=  snprintf(buf+len, size-len,
466 		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
467 		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
468 		*(ptr+5), *(ptr+6), *(ptr+7));
469 		ptr += 8;
470 		i -= (8 * sizeof(uint32_t));
471 		off += (8 * sizeof(uint32_t));
472 	}
473 
474 	len +=  snprintf(buf+len, size-len, "SLIM PCB\n");
475 	ptr = (uint32_t *)phba->pcb;
476 	i = sizeof(PCB_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 	for (i = 0; i < 4; i++) {
488 		pgpp = &phba->port_gp[i];
489 		pring = &psli->ring[i];
490 		len +=  snprintf(buf+len, size-len,
491 				 "Ring %d: CMD GetInx:%d (Max:%d Next:%d "
492 				 "Local:%d flg:x%x)  RSP PutInx:%d Max:%d\n",
493 				 i, pgpp->cmdGetInx, pring->numCiocb,
494 				 pring->next_cmdidx, pring->local_getidx,
495 				 pring->flag, pgpp->rspPutInx, pring->numRiocb);
496 	}
497 
498 	if (phba->sli_rev <= LPFC_SLI_REV3) {
499 		word0 = readl(phba->HAregaddr);
500 		word1 = readl(phba->CAregaddr);
501 		word2 = readl(phba->HSregaddr);
502 		word3 = readl(phba->HCregaddr);
503 		len +=  snprintf(buf+len, size-len, "HA:%08x CA:%08x HS:%08x "
504 				 "HC:%08x\n", word0, word1, word2, word3);
505 	}
506 	spin_unlock_irq(&phba->hbalock);
507 	return len;
508 }
509 
510 /**
511  * lpfc_debugfs_nodelist_data - Dump target node list to a buffer
512  * @vport: The vport to gather target node info from.
513  * @buf: The buffer to dump log into.
514  * @size: The maximum amount of data to process.
515  *
516  * Description:
517  * This routine dumps the current target node list associated with @vport to
518  * @buf up to @size bytes of data. Each node entry in the dump will contain a
519  * node state, DID, WWPN, WWNN, RPI, flags, type, and other useful fields.
520  *
521  * Return Value:
522  * This routine returns the amount of bytes that were dumped into @buf and will
523  * not exceed @size.
524  **/
525 static int
526 lpfc_debugfs_nodelist_data(struct lpfc_vport *vport, char *buf, int size)
527 {
528 	int len = 0;
529 	int cnt;
530 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
531 	struct lpfc_nodelist *ndlp;
532 	unsigned char *statep, *name;
533 
534 	cnt = (LPFC_NODELIST_SIZE / LPFC_NODELIST_ENTRY_SIZE);
535 
536 	spin_lock_irq(shost->host_lock);
537 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
538 		if (!cnt) {
539 			len +=  snprintf(buf+len, size-len,
540 				"Missing Nodelist Entries\n");
541 			break;
542 		}
543 		cnt--;
544 		switch (ndlp->nlp_state) {
545 		case NLP_STE_UNUSED_NODE:
546 			statep = "UNUSED";
547 			break;
548 		case NLP_STE_PLOGI_ISSUE:
549 			statep = "PLOGI ";
550 			break;
551 		case NLP_STE_ADISC_ISSUE:
552 			statep = "ADISC ";
553 			break;
554 		case NLP_STE_REG_LOGIN_ISSUE:
555 			statep = "REGLOG";
556 			break;
557 		case NLP_STE_PRLI_ISSUE:
558 			statep = "PRLI  ";
559 			break;
560 		case NLP_STE_UNMAPPED_NODE:
561 			statep = "UNMAP ";
562 			break;
563 		case NLP_STE_MAPPED_NODE:
564 			statep = "MAPPED";
565 			break;
566 		case NLP_STE_NPR_NODE:
567 			statep = "NPR   ";
568 			break;
569 		default:
570 			statep = "UNKNOWN";
571 		}
572 		len +=  snprintf(buf+len, size-len, "%s DID:x%06x ",
573 			statep, ndlp->nlp_DID);
574 		name = (unsigned char *)&ndlp->nlp_portname;
575 		len +=  snprintf(buf+len, size-len,
576 			"WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",
577 			*name, *(name+1), *(name+2), *(name+3),
578 			*(name+4), *(name+5), *(name+6), *(name+7));
579 		name = (unsigned char *)&ndlp->nlp_nodename;
580 		len +=  snprintf(buf+len, size-len,
581 			"WWNN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",
582 			*name, *(name+1), *(name+2), *(name+3),
583 			*(name+4), *(name+5), *(name+6), *(name+7));
584 		len +=  snprintf(buf+len, size-len, "RPI:%03d flag:x%08x ",
585 			ndlp->nlp_rpi, ndlp->nlp_flag);
586 		if (!ndlp->nlp_type)
587 			len +=  snprintf(buf+len, size-len, "UNKNOWN_TYPE ");
588 		if (ndlp->nlp_type & NLP_FC_NODE)
589 			len +=  snprintf(buf+len, size-len, "FC_NODE ");
590 		if (ndlp->nlp_type & NLP_FABRIC)
591 			len +=  snprintf(buf+len, size-len, "FABRIC ");
592 		if (ndlp->nlp_type & NLP_FCP_TARGET)
593 			len +=  snprintf(buf+len, size-len, "FCP_TGT sid:%d ",
594 				ndlp->nlp_sid);
595 		if (ndlp->nlp_type & NLP_FCP_INITIATOR)
596 			len +=  snprintf(buf+len, size-len, "FCP_INITIATOR ");
597 		len += snprintf(buf+len, size-len, "usgmap:%x ",
598 			ndlp->nlp_usg_map);
599 		len += snprintf(buf+len, size-len, "refcnt:%x",
600 			atomic_read(&ndlp->kref.refcount));
601 		len +=  snprintf(buf+len, size-len, "\n");
602 	}
603 	spin_unlock_irq(shost->host_lock);
604 	return len;
605 }
606 #endif
607 
608 /**
609  * lpfc_debugfs_disc_trc - Store discovery trace log
610  * @vport: The vport to associate this trace string with for retrieval.
611  * @mask: Log entry classification.
612  * @fmt: Format string to be displayed when dumping the log.
613  * @data1: 1st data parameter to be applied to @fmt.
614  * @data2: 2nd data parameter to be applied to @fmt.
615  * @data3: 3rd data parameter to be applied to @fmt.
616  *
617  * Description:
618  * This routine is used by the driver code to add a debugfs log entry to the
619  * discovery trace buffer associated with @vport. Only entries with a @mask that
620  * match the current debugfs discovery mask will be saved. Entries that do not
621  * match will be thrown away. @fmt, @data1, @data2, and @data3 are used like
622  * printf when displaying the log.
623  **/
624 inline void
625 lpfc_debugfs_disc_trc(struct lpfc_vport *vport, int mask, char *fmt,
626 	uint32_t data1, uint32_t data2, uint32_t data3)
627 {
628 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
629 	struct lpfc_debugfs_trc *dtp;
630 	int index;
631 
632 	if (!(lpfc_debugfs_mask_disc_trc & mask))
633 		return;
634 
635 	if (!lpfc_debugfs_enable || !lpfc_debugfs_max_disc_trc ||
636 		!vport || !vport->disc_trc)
637 		return;
638 
639 	index = atomic_inc_return(&vport->disc_trc_cnt) &
640 		(lpfc_debugfs_max_disc_trc - 1);
641 	dtp = vport->disc_trc + index;
642 	dtp->fmt = fmt;
643 	dtp->data1 = data1;
644 	dtp->data2 = data2;
645 	dtp->data3 = data3;
646 	dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_seq_trc_cnt);
647 	dtp->jif = jiffies;
648 #endif
649 	return;
650 }
651 
652 /**
653  * lpfc_debugfs_slow_ring_trc - Store slow ring trace log
654  * @phba: The phba to associate this trace string with for retrieval.
655  * @fmt: Format string to be displayed when dumping the log.
656  * @data1: 1st data parameter to be applied to @fmt.
657  * @data2: 2nd data parameter to be applied to @fmt.
658  * @data3: 3rd data parameter to be applied to @fmt.
659  *
660  * Description:
661  * This routine is used by the driver code to add a debugfs log entry to the
662  * discovery trace buffer associated with @vport. @fmt, @data1, @data2, and
663  * @data3 are used like printf when displaying the log.
664  **/
665 inline void
666 lpfc_debugfs_slow_ring_trc(struct lpfc_hba *phba, char *fmt,
667 	uint32_t data1, uint32_t data2, uint32_t data3)
668 {
669 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
670 	struct lpfc_debugfs_trc *dtp;
671 	int index;
672 
673 	if (!lpfc_debugfs_enable || !lpfc_debugfs_max_slow_ring_trc ||
674 		!phba || !phba->slow_ring_trc)
675 		return;
676 
677 	index = atomic_inc_return(&phba->slow_ring_trc_cnt) &
678 		(lpfc_debugfs_max_slow_ring_trc - 1);
679 	dtp = phba->slow_ring_trc + index;
680 	dtp->fmt = fmt;
681 	dtp->data1 = data1;
682 	dtp->data2 = data2;
683 	dtp->data3 = data3;
684 	dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_seq_trc_cnt);
685 	dtp->jif = jiffies;
686 #endif
687 	return;
688 }
689 
690 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
691 /**
692  * lpfc_debugfs_disc_trc_open - Open the discovery trace log
693  * @inode: The inode pointer that contains a vport pointer.
694  * @file: The file pointer to attach the log output.
695  *
696  * Description:
697  * This routine is the entry point for the debugfs open file operation. It gets
698  * the vport from the i_private field in @inode, allocates the necessary buffer
699  * for the log, fills the buffer from the in-memory log for this vport, and then
700  * returns a pointer to that log in the private_data field in @file.
701  *
702  * Returns:
703  * This function returns zero if successful. On error it will return an negative
704  * error value.
705  **/
706 static int
707 lpfc_debugfs_disc_trc_open(struct inode *inode, struct file *file)
708 {
709 	struct lpfc_vport *vport = inode->i_private;
710 	struct lpfc_debug *debug;
711 	int size;
712 	int rc = -ENOMEM;
713 
714 	if (!lpfc_debugfs_max_disc_trc) {
715 		 rc = -ENOSPC;
716 		goto out;
717 	}
718 
719 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
720 	if (!debug)
721 		goto out;
722 
723 	/* Round to page boundary */
724 	size =  (lpfc_debugfs_max_disc_trc * LPFC_DEBUG_TRC_ENTRY_SIZE);
725 	size = PAGE_ALIGN(size);
726 
727 	debug->buffer = kmalloc(size, GFP_KERNEL);
728 	if (!debug->buffer) {
729 		kfree(debug);
730 		goto out;
731 	}
732 
733 	debug->len = lpfc_debugfs_disc_trc_data(vport, debug->buffer, size);
734 	file->private_data = debug;
735 
736 	rc = 0;
737 out:
738 	return rc;
739 }
740 
741 /**
742  * lpfc_debugfs_slow_ring_trc_open - Open the Slow Ring trace log
743  * @inode: The inode pointer that contains a vport pointer.
744  * @file: The file pointer to attach the log output.
745  *
746  * Description:
747  * This routine is the entry point for the debugfs open file operation. It gets
748  * the vport from the i_private field in @inode, allocates the necessary buffer
749  * for the log, fills the buffer from the in-memory log for this vport, and then
750  * returns a pointer to that log in the private_data field in @file.
751  *
752  * Returns:
753  * This function returns zero if successful. On error it will return an negative
754  * error value.
755  **/
756 static int
757 lpfc_debugfs_slow_ring_trc_open(struct inode *inode, struct file *file)
758 {
759 	struct lpfc_hba *phba = inode->i_private;
760 	struct lpfc_debug *debug;
761 	int size;
762 	int rc = -ENOMEM;
763 
764 	if (!lpfc_debugfs_max_slow_ring_trc) {
765 		 rc = -ENOSPC;
766 		goto out;
767 	}
768 
769 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
770 	if (!debug)
771 		goto out;
772 
773 	/* Round to page boundary */
774 	size =  (lpfc_debugfs_max_slow_ring_trc * LPFC_DEBUG_TRC_ENTRY_SIZE);
775 	size = PAGE_ALIGN(size);
776 
777 	debug->buffer = kmalloc(size, GFP_KERNEL);
778 	if (!debug->buffer) {
779 		kfree(debug);
780 		goto out;
781 	}
782 
783 	debug->len = lpfc_debugfs_slow_ring_trc_data(phba, debug->buffer, size);
784 	file->private_data = debug;
785 
786 	rc = 0;
787 out:
788 	return rc;
789 }
790 
791 /**
792  * lpfc_debugfs_hbqinfo_open - Open the hbqinfo debugfs buffer
793  * @inode: The inode pointer that contains a vport pointer.
794  * @file: The file pointer to attach the log output.
795  *
796  * Description:
797  * This routine is the entry point for the debugfs open file operation. It gets
798  * the vport from the i_private field in @inode, allocates the necessary buffer
799  * for the log, fills the buffer from the in-memory log for this vport, and then
800  * returns a pointer to that log in the private_data field in @file.
801  *
802  * Returns:
803  * This function returns zero if successful. On error it will return an negative
804  * error value.
805  **/
806 static int
807 lpfc_debugfs_hbqinfo_open(struct inode *inode, struct file *file)
808 {
809 	struct lpfc_hba *phba = inode->i_private;
810 	struct lpfc_debug *debug;
811 	int rc = -ENOMEM;
812 
813 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
814 	if (!debug)
815 		goto out;
816 
817 	/* Round to page boundary */
818 	debug->buffer = kmalloc(LPFC_HBQINFO_SIZE, GFP_KERNEL);
819 	if (!debug->buffer) {
820 		kfree(debug);
821 		goto out;
822 	}
823 
824 	debug->len = lpfc_debugfs_hbqinfo_data(phba, debug->buffer,
825 		LPFC_HBQINFO_SIZE);
826 	file->private_data = debug;
827 
828 	rc = 0;
829 out:
830 	return rc;
831 }
832 
833 /**
834  * lpfc_debugfs_dumpHBASlim_open - Open the Dump HBA SLIM debugfs buffer
835  * @inode: The inode pointer that contains a vport pointer.
836  * @file: The file pointer to attach the log output.
837  *
838  * Description:
839  * This routine is the entry point for the debugfs open file operation. It gets
840  * the vport from the i_private field in @inode, allocates the necessary buffer
841  * for the log, fills the buffer from the in-memory log for this vport, and then
842  * returns a pointer to that log in the private_data field in @file.
843  *
844  * Returns:
845  * This function returns zero if successful. On error it will return an negative
846  * error value.
847  **/
848 static int
849 lpfc_debugfs_dumpHBASlim_open(struct inode *inode, struct file *file)
850 {
851 	struct lpfc_hba *phba = inode->i_private;
852 	struct lpfc_debug *debug;
853 	int rc = -ENOMEM;
854 
855 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
856 	if (!debug)
857 		goto out;
858 
859 	/* Round to page boundary */
860 	debug->buffer = kmalloc(LPFC_DUMPHBASLIM_SIZE, GFP_KERNEL);
861 	if (!debug->buffer) {
862 		kfree(debug);
863 		goto out;
864 	}
865 
866 	debug->len = lpfc_debugfs_dumpHBASlim_data(phba, debug->buffer,
867 		LPFC_DUMPHBASLIM_SIZE);
868 	file->private_data = debug;
869 
870 	rc = 0;
871 out:
872 	return rc;
873 }
874 
875 /**
876  * lpfc_debugfs_dumpHostSlim_open - Open the Dump Host SLIM debugfs buffer
877  * @inode: The inode pointer that contains a vport pointer.
878  * @file: The file pointer to attach the log output.
879  *
880  * Description:
881  * This routine is the entry point for the debugfs open file operation. It gets
882  * the vport from the i_private field in @inode, allocates the necessary buffer
883  * for the log, fills the buffer from the in-memory log for this vport, and then
884  * returns a pointer to that log in the private_data field in @file.
885  *
886  * Returns:
887  * This function returns zero if successful. On error it will return an negative
888  * error value.
889  **/
890 static int
891 lpfc_debugfs_dumpHostSlim_open(struct inode *inode, struct file *file)
892 {
893 	struct lpfc_hba *phba = inode->i_private;
894 	struct lpfc_debug *debug;
895 	int rc = -ENOMEM;
896 
897 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
898 	if (!debug)
899 		goto out;
900 
901 	/* Round to page boundary */
902 	debug->buffer = kmalloc(LPFC_DUMPHOSTSLIM_SIZE, GFP_KERNEL);
903 	if (!debug->buffer) {
904 		kfree(debug);
905 		goto out;
906 	}
907 
908 	debug->len = lpfc_debugfs_dumpHostSlim_data(phba, debug->buffer,
909 		LPFC_DUMPHOSTSLIM_SIZE);
910 	file->private_data = debug;
911 
912 	rc = 0;
913 out:
914 	return rc;
915 }
916 
917 static int
918 lpfc_debugfs_dumpData_open(struct inode *inode, struct file *file)
919 {
920 	struct lpfc_debug *debug;
921 	int rc = -ENOMEM;
922 
923 	if (!_dump_buf_data)
924 		return -EBUSY;
925 
926 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
927 	if (!debug)
928 		goto out;
929 
930 	/* Round to page boundary */
931 	printk(KERN_ERR "9059 BLKGRD:  %s: _dump_buf_data=0x%p\n",
932 			__func__, _dump_buf_data);
933 	debug->buffer = _dump_buf_data;
934 	if (!debug->buffer) {
935 		kfree(debug);
936 		goto out;
937 	}
938 
939 	debug->len = (1 << _dump_buf_data_order) << PAGE_SHIFT;
940 	file->private_data = debug;
941 
942 	rc = 0;
943 out:
944 	return rc;
945 }
946 
947 static int
948 lpfc_debugfs_dumpDif_open(struct inode *inode, struct file *file)
949 {
950 	struct lpfc_debug *debug;
951 	int rc = -ENOMEM;
952 
953 	if (!_dump_buf_dif)
954 		return -EBUSY;
955 
956 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
957 	if (!debug)
958 		goto out;
959 
960 	/* Round to page boundary */
961 	printk(KERN_ERR	"9060 BLKGRD: %s: _dump_buf_dif=0x%p file=%s\n",
962 		__func__, _dump_buf_dif, file->f_dentry->d_name.name);
963 	debug->buffer = _dump_buf_dif;
964 	if (!debug->buffer) {
965 		kfree(debug);
966 		goto out;
967 	}
968 
969 	debug->len = (1 << _dump_buf_dif_order) << PAGE_SHIFT;
970 	file->private_data = debug;
971 
972 	rc = 0;
973 out:
974 	return rc;
975 }
976 
977 static ssize_t
978 lpfc_debugfs_dumpDataDif_write(struct file *file, const char __user *buf,
979 		  size_t nbytes, loff_t *ppos)
980 {
981 	/*
982 	 * The Data/DIF buffers only save one failing IO
983 	 * The write op is used as a reset mechanism after an IO has
984 	 * already been saved to the next one can be saved
985 	 */
986 	spin_lock(&_dump_buf_lock);
987 
988 	memset((void *)_dump_buf_data, 0,
989 			((1 << PAGE_SHIFT) << _dump_buf_data_order));
990 	memset((void *)_dump_buf_dif, 0,
991 			((1 << PAGE_SHIFT) << _dump_buf_dif_order));
992 
993 	_dump_buf_done = 0;
994 
995 	spin_unlock(&_dump_buf_lock);
996 
997 	return nbytes;
998 }
999 
1000 static int
1001 lpfc_debugfs_dif_err_open(struct inode *inode, struct file *file)
1002 {
1003 	file->private_data = inode->i_private;
1004 	return 0;
1005 }
1006 
1007 static ssize_t
1008 lpfc_debugfs_dif_err_read(struct file *file, char __user *buf,
1009 	size_t nbytes, loff_t *ppos)
1010 {
1011 	struct dentry *dent = file->f_dentry;
1012 	struct lpfc_hba *phba = file->private_data;
1013 	char cbuf[16];
1014 	int cnt = 0;
1015 
1016 	if (dent == phba->debug_writeGuard)
1017 		cnt = snprintf(cbuf, 16, "%u\n", phba->lpfc_injerr_wgrd_cnt);
1018 	else if (dent == phba->debug_writeApp)
1019 		cnt = snprintf(cbuf, 16, "%u\n", phba->lpfc_injerr_wapp_cnt);
1020 	else if (dent == phba->debug_writeRef)
1021 		cnt = snprintf(cbuf, 16, "%u\n", phba->lpfc_injerr_wref_cnt);
1022 	else if (dent == phba->debug_readApp)
1023 		cnt = snprintf(cbuf, 16, "%u\n", phba->lpfc_injerr_rapp_cnt);
1024 	else if (dent == phba->debug_readRef)
1025 		cnt = snprintf(cbuf, 16, "%u\n", phba->lpfc_injerr_rref_cnt);
1026 	else if (dent == phba->debug_InjErrLBA)
1027 		cnt = snprintf(cbuf, 16, "0x%lx\n",
1028 				 (unsigned long) phba->lpfc_injerr_lba);
1029 	else
1030 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1031 			 "0547 Unknown debugfs error injection entry\n");
1032 
1033 	return simple_read_from_buffer(buf, nbytes, ppos, &cbuf, cnt);
1034 }
1035 
1036 static ssize_t
1037 lpfc_debugfs_dif_err_write(struct file *file, const char __user *buf,
1038 	size_t nbytes, loff_t *ppos)
1039 {
1040 	struct dentry *dent = file->f_dentry;
1041 	struct lpfc_hba *phba = file->private_data;
1042 	char dstbuf[32];
1043 	unsigned long tmp;
1044 	int size;
1045 
1046 	memset(dstbuf, 0, 32);
1047 	size = (nbytes < 32) ? nbytes : 32;
1048 	if (copy_from_user(dstbuf, buf, size))
1049 		return 0;
1050 
1051 	if (strict_strtoul(dstbuf, 0, &tmp))
1052 		return 0;
1053 
1054 	if (dent == phba->debug_writeGuard)
1055 		phba->lpfc_injerr_wgrd_cnt = (uint32_t)tmp;
1056 	else if (dent == phba->debug_writeApp)
1057 		phba->lpfc_injerr_wapp_cnt = (uint32_t)tmp;
1058 	else if (dent == phba->debug_writeRef)
1059 		phba->lpfc_injerr_wref_cnt = (uint32_t)tmp;
1060 	else if (dent == phba->debug_readApp)
1061 		phba->lpfc_injerr_rapp_cnt = (uint32_t)tmp;
1062 	else if (dent == phba->debug_readRef)
1063 		phba->lpfc_injerr_rref_cnt = (uint32_t)tmp;
1064 	else if (dent == phba->debug_InjErrLBA)
1065 		phba->lpfc_injerr_lba = (sector_t)tmp;
1066 	else
1067 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1068 			 "0548 Unknown debugfs error injection entry\n");
1069 
1070 	return nbytes;
1071 }
1072 
1073 static int
1074 lpfc_debugfs_dif_err_release(struct inode *inode, struct file *file)
1075 {
1076 	return 0;
1077 }
1078 
1079 /**
1080  * lpfc_debugfs_nodelist_open - Open the nodelist debugfs file
1081  * @inode: The inode pointer that contains a vport pointer.
1082  * @file: The file pointer to attach the log output.
1083  *
1084  * Description:
1085  * This routine is the entry point for the debugfs open file operation. It gets
1086  * the vport from the i_private field in @inode, allocates the necessary buffer
1087  * for the log, fills the buffer from the in-memory log for this vport, and then
1088  * returns a pointer to that log in the private_data field in @file.
1089  *
1090  * Returns:
1091  * This function returns zero if successful. On error it will return an negative
1092  * error value.
1093  **/
1094 static int
1095 lpfc_debugfs_nodelist_open(struct inode *inode, struct file *file)
1096 {
1097 	struct lpfc_vport *vport = inode->i_private;
1098 	struct lpfc_debug *debug;
1099 	int rc = -ENOMEM;
1100 
1101 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1102 	if (!debug)
1103 		goto out;
1104 
1105 	/* Round to page boundary */
1106 	debug->buffer = kmalloc(LPFC_NODELIST_SIZE, GFP_KERNEL);
1107 	if (!debug->buffer) {
1108 		kfree(debug);
1109 		goto out;
1110 	}
1111 
1112 	debug->len = lpfc_debugfs_nodelist_data(vport, debug->buffer,
1113 		LPFC_NODELIST_SIZE);
1114 	file->private_data = debug;
1115 
1116 	rc = 0;
1117 out:
1118 	return rc;
1119 }
1120 
1121 /**
1122  * lpfc_debugfs_lseek - Seek through a debugfs file
1123  * @file: The file pointer to seek through.
1124  * @off: The offset to seek to or the amount to seek by.
1125  * @whence: Indicates how to seek.
1126  *
1127  * Description:
1128  * This routine is the entry point for the debugfs lseek file operation. The
1129  * @whence parameter indicates whether @off is the offset to directly seek to,
1130  * or if it is a value to seek forward or reverse by. This function figures out
1131  * what the new offset of the debugfs file will be and assigns that value to the
1132  * f_pos field of @file.
1133  *
1134  * Returns:
1135  * This function returns the new offset if successful and returns a negative
1136  * error if unable to process the seek.
1137  **/
1138 static loff_t
1139 lpfc_debugfs_lseek(struct file *file, loff_t off, int whence)
1140 {
1141 	struct lpfc_debug *debug;
1142 	loff_t pos = -1;
1143 
1144 	debug = file->private_data;
1145 
1146 	switch (whence) {
1147 	case 0:
1148 		pos = off;
1149 		break;
1150 	case 1:
1151 		pos = file->f_pos + off;
1152 		break;
1153 	case 2:
1154 		pos = debug->len - off;
1155 	}
1156 	return (pos < 0 || pos > debug->len) ? -EINVAL : (file->f_pos = pos);
1157 }
1158 
1159 /**
1160  * lpfc_debugfs_read - Read a debugfs file
1161  * @file: The file pointer to read from.
1162  * @buf: The buffer to copy the data to.
1163  * @nbytes: The number of bytes to read.
1164  * @ppos: The position in the file to start reading from.
1165  *
1166  * Description:
1167  * This routine reads data from from the buffer indicated in the private_data
1168  * field of @file. It will start reading at @ppos and copy up to @nbytes of
1169  * data to @buf.
1170  *
1171  * Returns:
1172  * This function returns the amount of data that was read (this could be less
1173  * than @nbytes if the end of the file was reached) or a negative error value.
1174  **/
1175 static ssize_t
1176 lpfc_debugfs_read(struct file *file, char __user *buf,
1177 		  size_t nbytes, loff_t *ppos)
1178 {
1179 	struct lpfc_debug *debug = file->private_data;
1180 
1181 	return simple_read_from_buffer(buf, nbytes, ppos, debug->buffer,
1182 				       debug->len);
1183 }
1184 
1185 /**
1186  * lpfc_debugfs_release - Release the buffer used to store debugfs file data
1187  * @inode: The inode pointer that contains a vport pointer. (unused)
1188  * @file: The file pointer that contains the buffer to release.
1189  *
1190  * Description:
1191  * This routine frees the buffer that was allocated when the debugfs file was
1192  * opened.
1193  *
1194  * Returns:
1195  * This function returns zero.
1196  **/
1197 static int
1198 lpfc_debugfs_release(struct inode *inode, struct file *file)
1199 {
1200 	struct lpfc_debug *debug = file->private_data;
1201 
1202 	kfree(debug->buffer);
1203 	kfree(debug);
1204 
1205 	return 0;
1206 }
1207 
1208 static int
1209 lpfc_debugfs_dumpDataDif_release(struct inode *inode, struct file *file)
1210 {
1211 	struct lpfc_debug *debug = file->private_data;
1212 
1213 	debug->buffer = NULL;
1214 	kfree(debug);
1215 
1216 	return 0;
1217 }
1218 
1219 /*
1220  * ---------------------------------
1221  * iDiag debugfs file access methods
1222  * ---------------------------------
1223  *
1224  * All access methods are through the proper SLI4 PCI function's debugfs
1225  * iDiag directory:
1226  *
1227  *     /sys/kernel/debug/lpfc/fn<#>/iDiag
1228  */
1229 
1230 /**
1231  * lpfc_idiag_cmd_get - Get and parse idiag debugfs comands from user space
1232  * @buf: The pointer to the user space buffer.
1233  * @nbytes: The number of bytes in the user space buffer.
1234  * @idiag_cmd: pointer to the idiag command struct.
1235  *
1236  * This routine reads data from debugfs user space buffer and parses the
1237  * buffer for getting the idiag command and arguments. The while space in
1238  * between the set of data is used as the parsing separator.
1239  *
1240  * This routine returns 0 when successful, it returns proper error code
1241  * back to the user space in error conditions.
1242  */
1243 static int lpfc_idiag_cmd_get(const char __user *buf, size_t nbytes,
1244 			      struct lpfc_idiag_cmd *idiag_cmd)
1245 {
1246 	char mybuf[64];
1247 	char *pbuf, *step_str;
1248 	int i;
1249 	size_t bsize;
1250 
1251 	/* Protect copy from user */
1252 	if (!access_ok(VERIFY_READ, buf, nbytes))
1253 		return -EFAULT;
1254 
1255 	memset(mybuf, 0, sizeof(mybuf));
1256 	memset(idiag_cmd, 0, sizeof(*idiag_cmd));
1257 	bsize = min(nbytes, (sizeof(mybuf)-1));
1258 
1259 	if (copy_from_user(mybuf, buf, bsize))
1260 		return -EFAULT;
1261 	pbuf = &mybuf[0];
1262 	step_str = strsep(&pbuf, "\t ");
1263 
1264 	/* The opcode must present */
1265 	if (!step_str)
1266 		return -EINVAL;
1267 
1268 	idiag_cmd->opcode = simple_strtol(step_str, NULL, 0);
1269 	if (idiag_cmd->opcode == 0)
1270 		return -EINVAL;
1271 
1272 	for (i = 0; i < LPFC_IDIAG_CMD_DATA_SIZE; i++) {
1273 		step_str = strsep(&pbuf, "\t ");
1274 		if (!step_str)
1275 			return i;
1276 		idiag_cmd->data[i] = simple_strtol(step_str, NULL, 0);
1277 	}
1278 	return i;
1279 }
1280 
1281 /**
1282  * lpfc_idiag_open - idiag open debugfs
1283  * @inode: The inode pointer that contains a pointer to phba.
1284  * @file: The file pointer to attach the file operation.
1285  *
1286  * Description:
1287  * This routine is the entry point for the debugfs open file operation. It
1288  * gets the reference to phba from the i_private field in @inode, it then
1289  * allocates buffer for the file operation, performs the necessary PCI config
1290  * space read into the allocated buffer according to the idiag user command
1291  * setup, and then returns a pointer to buffer in the private_data field in
1292  * @file.
1293  *
1294  * Returns:
1295  * This function returns zero if successful. On error it will return an
1296  * negative error value.
1297  **/
1298 static int
1299 lpfc_idiag_open(struct inode *inode, struct file *file)
1300 {
1301 	struct lpfc_debug *debug;
1302 
1303 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1304 	if (!debug)
1305 		return -ENOMEM;
1306 
1307 	debug->i_private = inode->i_private;
1308 	debug->buffer = NULL;
1309 	file->private_data = debug;
1310 
1311 	return 0;
1312 }
1313 
1314 /**
1315  * lpfc_idiag_release - Release idiag access file operation
1316  * @inode: The inode pointer that contains a vport pointer. (unused)
1317  * @file: The file pointer that contains the buffer to release.
1318  *
1319  * Description:
1320  * This routine is the generic release routine for the idiag access file
1321  * operation, it frees the buffer that was allocated when the debugfs file
1322  * was opened.
1323  *
1324  * Returns:
1325  * This function returns zero.
1326  **/
1327 static int
1328 lpfc_idiag_release(struct inode *inode, struct file *file)
1329 {
1330 	struct lpfc_debug *debug = file->private_data;
1331 
1332 	/* Free the buffers to the file operation */
1333 	kfree(debug->buffer);
1334 	kfree(debug);
1335 
1336 	return 0;
1337 }
1338 
1339 /**
1340  * lpfc_idiag_cmd_release - Release idiag cmd access file operation
1341  * @inode: The inode pointer that contains a vport pointer. (unused)
1342  * @file: The file pointer that contains the buffer to release.
1343  *
1344  * Description:
1345  * This routine frees the buffer that was allocated when the debugfs file
1346  * was opened. It also reset the fields in the idiag command struct in the
1347  * case of command for write operation.
1348  *
1349  * Returns:
1350  * This function returns zero.
1351  **/
1352 static int
1353 lpfc_idiag_cmd_release(struct inode *inode, struct file *file)
1354 {
1355 	struct lpfc_debug *debug = file->private_data;
1356 
1357 	if (debug->op == LPFC_IDIAG_OP_WR) {
1358 		switch (idiag.cmd.opcode) {
1359 		case LPFC_IDIAG_CMD_PCICFG_WR:
1360 		case LPFC_IDIAG_CMD_PCICFG_ST:
1361 		case LPFC_IDIAG_CMD_PCICFG_CL:
1362 		case LPFC_IDIAG_CMD_QUEACC_WR:
1363 		case LPFC_IDIAG_CMD_QUEACC_ST:
1364 		case LPFC_IDIAG_CMD_QUEACC_CL:
1365 			memset(&idiag, 0, sizeof(idiag));
1366 			break;
1367 		default:
1368 			break;
1369 		}
1370 	}
1371 
1372 	/* Free the buffers to the file operation */
1373 	kfree(debug->buffer);
1374 	kfree(debug);
1375 
1376 	return 0;
1377 }
1378 
1379 /**
1380  * lpfc_idiag_pcicfg_read - idiag debugfs read pcicfg
1381  * @file: The file pointer to read from.
1382  * @buf: The buffer to copy the data to.
1383  * @nbytes: The number of bytes to read.
1384  * @ppos: The position in the file to start reading from.
1385  *
1386  * Description:
1387  * This routine reads data from the @phba pci config space according to the
1388  * idiag command, and copies to user @buf. Depending on the PCI config space
1389  * read command setup, it does either a single register read of a byte
1390  * (8 bits), a word (16 bits), or a dword (32 bits) or browsing through all
1391  * registers from the 4K extended PCI config space.
1392  *
1393  * Returns:
1394  * This function returns the amount of data that was read (this could be less
1395  * than @nbytes if the end of the file was reached) or a negative error value.
1396  **/
1397 static ssize_t
1398 lpfc_idiag_pcicfg_read(struct file *file, char __user *buf, size_t nbytes,
1399 		       loff_t *ppos)
1400 {
1401 	struct lpfc_debug *debug = file->private_data;
1402 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
1403 	int offset_label, offset, len = 0, index = LPFC_PCI_CFG_RD_SIZE;
1404 	int where, count;
1405 	char *pbuffer;
1406 	struct pci_dev *pdev;
1407 	uint32_t u32val;
1408 	uint16_t u16val;
1409 	uint8_t u8val;
1410 
1411 	pdev = phba->pcidev;
1412 	if (!pdev)
1413 		return 0;
1414 
1415 	/* This is a user read operation */
1416 	debug->op = LPFC_IDIAG_OP_RD;
1417 
1418 	if (!debug->buffer)
1419 		debug->buffer = kmalloc(LPFC_PCI_CFG_SIZE, GFP_KERNEL);
1420 	if (!debug->buffer)
1421 		return 0;
1422 	pbuffer = debug->buffer;
1423 
1424 	if (*ppos)
1425 		return 0;
1426 
1427 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
1428 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
1429 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
1430 	} else
1431 		return 0;
1432 
1433 	/* Read single PCI config space register */
1434 	switch (count) {
1435 	case SIZE_U8: /* byte (8 bits) */
1436 		pci_read_config_byte(pdev, where, &u8val);
1437 		len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
1438 				"%03x: %02x\n", where, u8val);
1439 		break;
1440 	case SIZE_U16: /* word (16 bits) */
1441 		pci_read_config_word(pdev, where, &u16val);
1442 		len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
1443 				"%03x: %04x\n", where, u16val);
1444 		break;
1445 	case SIZE_U32: /* double word (32 bits) */
1446 		pci_read_config_dword(pdev, where, &u32val);
1447 		len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
1448 				"%03x: %08x\n", where, u32val);
1449 		break;
1450 	case LPFC_PCI_CFG_BROWSE: /* browse all */
1451 		goto pcicfg_browse;
1452 		break;
1453 	default:
1454 		/* illegal count */
1455 		len = 0;
1456 		break;
1457 	}
1458 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
1459 
1460 pcicfg_browse:
1461 
1462 	/* Browse all PCI config space registers */
1463 	offset_label = idiag.offset.last_rd;
1464 	offset = offset_label;
1465 
1466 	/* Read PCI config space */
1467 	len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
1468 			"%03x: ", offset_label);
1469 	while (index > 0) {
1470 		pci_read_config_dword(pdev, offset, &u32val);
1471 		len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
1472 				"%08x ", u32val);
1473 		offset += sizeof(uint32_t);
1474 		if (offset >= LPFC_PCI_CFG_SIZE) {
1475 			len += snprintf(pbuffer+len,
1476 					LPFC_PCI_CFG_SIZE-len, "\n");
1477 			break;
1478 		}
1479 		index -= sizeof(uint32_t);
1480 		if (!index)
1481 			len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
1482 					"\n");
1483 		else if (!(index % (8 * sizeof(uint32_t)))) {
1484 			offset_label += (8 * sizeof(uint32_t));
1485 			len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
1486 					"\n%03x: ", offset_label);
1487 		}
1488 	}
1489 
1490 	/* Set up the offset for next portion of pci cfg read */
1491 	if (index == 0) {
1492 		idiag.offset.last_rd += LPFC_PCI_CFG_RD_SIZE;
1493 		if (idiag.offset.last_rd >= LPFC_PCI_CFG_SIZE)
1494 			idiag.offset.last_rd = 0;
1495 	} else
1496 		idiag.offset.last_rd = 0;
1497 
1498 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
1499 }
1500 
1501 /**
1502  * lpfc_idiag_pcicfg_write - Syntax check and set up idiag pcicfg commands
1503  * @file: The file pointer to read from.
1504  * @buf: The buffer to copy the user data from.
1505  * @nbytes: The number of bytes to get.
1506  * @ppos: The position in the file to start reading from.
1507  *
1508  * This routine get the debugfs idiag command struct from user space and
1509  * then perform the syntax check for PCI config space read or write command
1510  * accordingly. In the case of PCI config space read command, it sets up
1511  * the command in the idiag command struct for the debugfs read operation.
1512  * In the case of PCI config space write operation, it executes the write
1513  * operation into the PCI config space accordingly.
1514  *
1515  * It returns the @nbytges passing in from debugfs user space when successful.
1516  * In case of error conditions, it returns proper error code back to the user
1517  * space.
1518  */
1519 static ssize_t
1520 lpfc_idiag_pcicfg_write(struct file *file, const char __user *buf,
1521 			size_t nbytes, loff_t *ppos)
1522 {
1523 	struct lpfc_debug *debug = file->private_data;
1524 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
1525 	uint32_t where, value, count;
1526 	uint32_t u32val;
1527 	uint16_t u16val;
1528 	uint8_t u8val;
1529 	struct pci_dev *pdev;
1530 	int rc;
1531 
1532 	pdev = phba->pcidev;
1533 	if (!pdev)
1534 		return -EFAULT;
1535 
1536 	/* This is a user write operation */
1537 	debug->op = LPFC_IDIAG_OP_WR;
1538 
1539 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
1540 	if (rc < 0)
1541 		return rc;
1542 
1543 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
1544 		/* Sanity check on PCI config read command line arguments */
1545 		if (rc != LPFC_PCI_CFG_RD_CMD_ARG)
1546 			goto error_out;
1547 		/* Read command from PCI config space, set up command fields */
1548 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
1549 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
1550 		if (count == LPFC_PCI_CFG_BROWSE) {
1551 			if (where % sizeof(uint32_t))
1552 				goto error_out;
1553 			/* Starting offset to browse */
1554 			idiag.offset.last_rd = where;
1555 		} else if ((count != sizeof(uint8_t)) &&
1556 			   (count != sizeof(uint16_t)) &&
1557 			   (count != sizeof(uint32_t)))
1558 			goto error_out;
1559 		if (count == sizeof(uint8_t)) {
1560 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
1561 				goto error_out;
1562 			if (where % sizeof(uint8_t))
1563 				goto error_out;
1564 		}
1565 		if (count == sizeof(uint16_t)) {
1566 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
1567 				goto error_out;
1568 			if (where % sizeof(uint16_t))
1569 				goto error_out;
1570 		}
1571 		if (count == sizeof(uint32_t)) {
1572 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
1573 				goto error_out;
1574 			if (where % sizeof(uint32_t))
1575 				goto error_out;
1576 		}
1577 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR ||
1578 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST ||
1579 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
1580 		/* Sanity check on PCI config write command line arguments */
1581 		if (rc != LPFC_PCI_CFG_WR_CMD_ARG)
1582 			goto error_out;
1583 		/* Write command to PCI config space, read-modify-write */
1584 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
1585 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
1586 		value = idiag.cmd.data[IDIAG_PCICFG_VALUE_INDX];
1587 		/* Sanity checks */
1588 		if ((count != sizeof(uint8_t)) &&
1589 		    (count != sizeof(uint16_t)) &&
1590 		    (count != sizeof(uint32_t)))
1591 			goto error_out;
1592 		if (count == sizeof(uint8_t)) {
1593 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
1594 				goto error_out;
1595 			if (where % sizeof(uint8_t))
1596 				goto error_out;
1597 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
1598 				pci_write_config_byte(pdev, where,
1599 						      (uint8_t)value);
1600 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
1601 				rc = pci_read_config_byte(pdev, where, &u8val);
1602 				if (!rc) {
1603 					u8val |= (uint8_t)value;
1604 					pci_write_config_byte(pdev, where,
1605 							      u8val);
1606 				}
1607 			}
1608 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
1609 				rc = pci_read_config_byte(pdev, where, &u8val);
1610 				if (!rc) {
1611 					u8val &= (uint8_t)(~value);
1612 					pci_write_config_byte(pdev, where,
1613 							      u8val);
1614 				}
1615 			}
1616 		}
1617 		if (count == sizeof(uint16_t)) {
1618 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
1619 				goto error_out;
1620 			if (where % sizeof(uint16_t))
1621 				goto error_out;
1622 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
1623 				pci_write_config_word(pdev, where,
1624 						      (uint16_t)value);
1625 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
1626 				rc = pci_read_config_word(pdev, where, &u16val);
1627 				if (!rc) {
1628 					u16val |= (uint16_t)value;
1629 					pci_write_config_word(pdev, where,
1630 							      u16val);
1631 				}
1632 			}
1633 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
1634 				rc = pci_read_config_word(pdev, where, &u16val);
1635 				if (!rc) {
1636 					u16val &= (uint16_t)(~value);
1637 					pci_write_config_word(pdev, where,
1638 							      u16val);
1639 				}
1640 			}
1641 		}
1642 		if (count == sizeof(uint32_t)) {
1643 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
1644 				goto error_out;
1645 			if (where % sizeof(uint32_t))
1646 				goto error_out;
1647 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
1648 				pci_write_config_dword(pdev, where, value);
1649 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
1650 				rc = pci_read_config_dword(pdev, where,
1651 							   &u32val);
1652 				if (!rc) {
1653 					u32val |= value;
1654 					pci_write_config_dword(pdev, where,
1655 							       u32val);
1656 				}
1657 			}
1658 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
1659 				rc = pci_read_config_dword(pdev, where,
1660 							   &u32val);
1661 				if (!rc) {
1662 					u32val &= ~value;
1663 					pci_write_config_dword(pdev, where,
1664 							       u32val);
1665 				}
1666 			}
1667 		}
1668 	} else
1669 		/* All other opecodes are illegal for now */
1670 		goto error_out;
1671 
1672 	return nbytes;
1673 error_out:
1674 	memset(&idiag, 0, sizeof(idiag));
1675 	return -EINVAL;
1676 }
1677 
1678 /**
1679  * lpfc_idiag_baracc_read - idiag debugfs pci bar access read
1680  * @file: The file pointer to read from.
1681  * @buf: The buffer to copy the data to.
1682  * @nbytes: The number of bytes to read.
1683  * @ppos: The position in the file to start reading from.
1684  *
1685  * Description:
1686  * This routine reads data from the @phba pci bar memory mapped space
1687  * according to the idiag command, and copies to user @buf.
1688  *
1689  * Returns:
1690  * This function returns the amount of data that was read (this could be less
1691  * than @nbytes if the end of the file was reached) or a negative error value.
1692  **/
1693 static ssize_t
1694 lpfc_idiag_baracc_read(struct file *file, char __user *buf, size_t nbytes,
1695 		       loff_t *ppos)
1696 {
1697 	struct lpfc_debug *debug = file->private_data;
1698 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
1699 	int offset_label, offset, offset_run, len = 0, index;
1700 	int bar_num, acc_range, bar_size;
1701 	char *pbuffer;
1702 	void __iomem *mem_mapped_bar;
1703 	uint32_t if_type;
1704 	struct pci_dev *pdev;
1705 	uint32_t u32val;
1706 
1707 	pdev = phba->pcidev;
1708 	if (!pdev)
1709 		return 0;
1710 
1711 	/* This is a user read operation */
1712 	debug->op = LPFC_IDIAG_OP_RD;
1713 
1714 	if (!debug->buffer)
1715 		debug->buffer = kmalloc(LPFC_PCI_BAR_RD_BUF_SIZE, GFP_KERNEL);
1716 	if (!debug->buffer)
1717 		return 0;
1718 	pbuffer = debug->buffer;
1719 
1720 	if (*ppos)
1721 		return 0;
1722 
1723 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
1724 		bar_num   = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
1725 		offset    = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
1726 		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
1727 		bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
1728 	} else
1729 		return 0;
1730 
1731 	if (acc_range == 0)
1732 		return 0;
1733 
1734 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1735 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
1736 		if (bar_num == IDIAG_BARACC_BAR_0)
1737 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
1738 		else if (bar_num == IDIAG_BARACC_BAR_1)
1739 			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
1740 		else if (bar_num == IDIAG_BARACC_BAR_2)
1741 			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
1742 		else
1743 			return 0;
1744 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
1745 		if (bar_num == IDIAG_BARACC_BAR_0)
1746 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
1747 		else
1748 			return 0;
1749 	} else
1750 		return 0;
1751 
1752 	/* Read single PCI bar space register */
1753 	if (acc_range == SINGLE_WORD) {
1754 		offset_run = offset;
1755 		u32val = readl(mem_mapped_bar + offset_run);
1756 		len += snprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
1757 				"%05x: %08x\n", offset_run, u32val);
1758 	} else
1759 		goto baracc_browse;
1760 
1761 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
1762 
1763 baracc_browse:
1764 
1765 	/* Browse all PCI bar space registers */
1766 	offset_label = idiag.offset.last_rd;
1767 	offset_run = offset_label;
1768 
1769 	/* Read PCI bar memory mapped space */
1770 	len += snprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
1771 			"%05x: ", offset_label);
1772 	index = LPFC_PCI_BAR_RD_SIZE;
1773 	while (index > 0) {
1774 		u32val = readl(mem_mapped_bar + offset_run);
1775 		len += snprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
1776 				"%08x ", u32val);
1777 		offset_run += sizeof(uint32_t);
1778 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
1779 			if (offset_run >= bar_size) {
1780 				len += snprintf(pbuffer+len,
1781 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
1782 				break;
1783 			}
1784 		} else {
1785 			if (offset_run >= offset +
1786 			    (acc_range * sizeof(uint32_t))) {
1787 				len += snprintf(pbuffer+len,
1788 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
1789 				break;
1790 			}
1791 		}
1792 		index -= sizeof(uint32_t);
1793 		if (!index)
1794 			len += snprintf(pbuffer+len,
1795 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
1796 		else if (!(index % (8 * sizeof(uint32_t)))) {
1797 			offset_label += (8 * sizeof(uint32_t));
1798 			len += snprintf(pbuffer+len,
1799 					LPFC_PCI_BAR_RD_BUF_SIZE-len,
1800 					"\n%05x: ", offset_label);
1801 		}
1802 	}
1803 
1804 	/* Set up the offset for next portion of pci bar read */
1805 	if (index == 0) {
1806 		idiag.offset.last_rd += LPFC_PCI_BAR_RD_SIZE;
1807 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
1808 			if (idiag.offset.last_rd >= bar_size)
1809 				idiag.offset.last_rd = 0;
1810 		} else {
1811 			if (offset_run >= offset +
1812 			    (acc_range * sizeof(uint32_t)))
1813 				idiag.offset.last_rd = offset;
1814 		}
1815 	} else {
1816 		if (acc_range == LPFC_PCI_BAR_BROWSE)
1817 			idiag.offset.last_rd = 0;
1818 		else
1819 			idiag.offset.last_rd = offset;
1820 	}
1821 
1822 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
1823 }
1824 
1825 /**
1826  * lpfc_idiag_baracc_write - Syntax check and set up idiag bar access commands
1827  * @file: The file pointer to read from.
1828  * @buf: The buffer to copy the user data from.
1829  * @nbytes: The number of bytes to get.
1830  * @ppos: The position in the file to start reading from.
1831  *
1832  * This routine get the debugfs idiag command struct from user space and
1833  * then perform the syntax check for PCI bar memory mapped space read or
1834  * write command accordingly. In the case of PCI bar memory mapped space
1835  * read command, it sets up the command in the idiag command struct for
1836  * the debugfs read operation. In the case of PCI bar memorpy mapped space
1837  * write operation, it executes the write operation into the PCI bar memory
1838  * mapped space accordingly.
1839  *
1840  * It returns the @nbytges passing in from debugfs user space when successful.
1841  * In case of error conditions, it returns proper error code back to the user
1842  * space.
1843  */
1844 static ssize_t
1845 lpfc_idiag_baracc_write(struct file *file, const char __user *buf,
1846 			size_t nbytes, loff_t *ppos)
1847 {
1848 	struct lpfc_debug *debug = file->private_data;
1849 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
1850 	uint32_t bar_num, bar_size, offset, value, acc_range;
1851 	struct pci_dev *pdev;
1852 	void __iomem *mem_mapped_bar;
1853 	uint32_t if_type;
1854 	uint32_t u32val;
1855 	int rc;
1856 
1857 	pdev = phba->pcidev;
1858 	if (!pdev)
1859 		return -EFAULT;
1860 
1861 	/* This is a user write operation */
1862 	debug->op = LPFC_IDIAG_OP_WR;
1863 
1864 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
1865 	if (rc < 0)
1866 		return rc;
1867 
1868 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1869 	bar_num = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
1870 
1871 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
1872 		if ((bar_num != IDIAG_BARACC_BAR_0) &&
1873 		    (bar_num != IDIAG_BARACC_BAR_1) &&
1874 		    (bar_num != IDIAG_BARACC_BAR_2))
1875 			goto error_out;
1876 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
1877 		if (bar_num != IDIAG_BARACC_BAR_0)
1878 			goto error_out;
1879 	} else
1880 		goto error_out;
1881 
1882 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
1883 		if (bar_num == IDIAG_BARACC_BAR_0) {
1884 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
1885 				LPFC_PCI_IF0_BAR0_SIZE;
1886 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
1887 		} else if (bar_num == IDIAG_BARACC_BAR_1) {
1888 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
1889 				LPFC_PCI_IF0_BAR1_SIZE;
1890 			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
1891 		} else if (bar_num == IDIAG_BARACC_BAR_2) {
1892 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
1893 				LPFC_PCI_IF0_BAR2_SIZE;
1894 			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
1895 		} else
1896 			goto error_out;
1897 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
1898 		if (bar_num == IDIAG_BARACC_BAR_0) {
1899 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
1900 				LPFC_PCI_IF2_BAR0_SIZE;
1901 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
1902 		} else
1903 			goto error_out;
1904 	} else
1905 		goto error_out;
1906 
1907 	offset = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
1908 	if (offset % sizeof(uint32_t))
1909 		goto error_out;
1910 
1911 	bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
1912 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
1913 		/* Sanity check on PCI config read command line arguments */
1914 		if (rc != LPFC_PCI_BAR_RD_CMD_ARG)
1915 			goto error_out;
1916 		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
1917 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
1918 			if (offset > bar_size - sizeof(uint32_t))
1919 				goto error_out;
1920 			/* Starting offset to browse */
1921 			idiag.offset.last_rd = offset;
1922 		} else if (acc_range > SINGLE_WORD) {
1923 			if (offset + acc_range * sizeof(uint32_t) > bar_size)
1924 				goto error_out;
1925 			/* Starting offset to browse */
1926 			idiag.offset.last_rd = offset;
1927 		} else if (acc_range != SINGLE_WORD)
1928 			goto error_out;
1929 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR ||
1930 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST ||
1931 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
1932 		/* Sanity check on PCI bar write command line arguments */
1933 		if (rc != LPFC_PCI_BAR_WR_CMD_ARG)
1934 			goto error_out;
1935 		/* Write command to PCI bar space, read-modify-write */
1936 		acc_range = SINGLE_WORD;
1937 		value = idiag.cmd.data[IDIAG_BARACC_REG_VAL_INDX];
1938 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR) {
1939 			writel(value, mem_mapped_bar + offset);
1940 			readl(mem_mapped_bar + offset);
1941 		}
1942 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST) {
1943 			u32val = readl(mem_mapped_bar + offset);
1944 			u32val |= value;
1945 			writel(u32val, mem_mapped_bar + offset);
1946 			readl(mem_mapped_bar + offset);
1947 		}
1948 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
1949 			u32val = readl(mem_mapped_bar + offset);
1950 			u32val &= ~value;
1951 			writel(u32val, mem_mapped_bar + offset);
1952 			readl(mem_mapped_bar + offset);
1953 		}
1954 	} else
1955 		/* All other opecodes are illegal for now */
1956 		goto error_out;
1957 
1958 	return nbytes;
1959 error_out:
1960 	memset(&idiag, 0, sizeof(idiag));
1961 	return -EINVAL;
1962 }
1963 
1964 /**
1965  * lpfc_idiag_queinfo_read - idiag debugfs read queue information
1966  * @file: The file pointer to read from.
1967  * @buf: The buffer to copy the data to.
1968  * @nbytes: The number of bytes to read.
1969  * @ppos: The position in the file to start reading from.
1970  *
1971  * Description:
1972  * This routine reads data from the @phba SLI4 PCI function queue information,
1973  * and copies to user @buf.
1974  *
1975  * Returns:
1976  * This function returns the amount of data that was read (this could be less
1977  * than @nbytes if the end of the file was reached) or a negative error value.
1978  **/
1979 static ssize_t
1980 lpfc_idiag_queinfo_read(struct file *file, char __user *buf, size_t nbytes,
1981 			loff_t *ppos)
1982 {
1983 	struct lpfc_debug *debug = file->private_data;
1984 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
1985 	int len = 0, fcp_qidx;
1986 	char *pbuffer;
1987 
1988 	if (!debug->buffer)
1989 		debug->buffer = kmalloc(LPFC_QUE_INFO_GET_BUF_SIZE, GFP_KERNEL);
1990 	if (!debug->buffer)
1991 		return 0;
1992 	pbuffer = debug->buffer;
1993 
1994 	if (*ppos)
1995 		return 0;
1996 
1997 	/* Get slow-path event queue information */
1998 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
1999 			"Slow-path EQ information:\n");
2000 	if (phba->sli4_hba.sp_eq) {
2001 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2002 			"\tEQID[%02d], "
2003 			"QE-COUNT[%04d], QE-SIZE[%04d], "
2004 			"HOST-INDEX[%04d], PORT-INDEX[%04d]\n\n",
2005 			phba->sli4_hba.sp_eq->queue_id,
2006 			phba->sli4_hba.sp_eq->entry_count,
2007 			phba->sli4_hba.sp_eq->entry_size,
2008 			phba->sli4_hba.sp_eq->host_index,
2009 			phba->sli4_hba.sp_eq->hba_index);
2010 	}
2011 
2012 	/* Get fast-path event queue information */
2013 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2014 			"Fast-path EQ information:\n");
2015 	if (phba->sli4_hba.fp_eq) {
2016 		for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count;
2017 		     fcp_qidx++) {
2018 			if (phba->sli4_hba.fp_eq[fcp_qidx]) {
2019 				len += snprintf(pbuffer+len,
2020 					LPFC_QUE_INFO_GET_BUF_SIZE-len,
2021 				"\tEQID[%02d], "
2022 				"QE-COUNT[%04d], QE-SIZE[%04d], "
2023 				"HOST-INDEX[%04d], PORT-INDEX[%04d]\n",
2024 				phba->sli4_hba.fp_eq[fcp_qidx]->queue_id,
2025 				phba->sli4_hba.fp_eq[fcp_qidx]->entry_count,
2026 				phba->sli4_hba.fp_eq[fcp_qidx]->entry_size,
2027 				phba->sli4_hba.fp_eq[fcp_qidx]->host_index,
2028 				phba->sli4_hba.fp_eq[fcp_qidx]->hba_index);
2029 			}
2030 		}
2031 	}
2032 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len, "\n");
2033 
2034 	/* Get mailbox complete queue information */
2035 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2036 			"Slow-path MBX CQ information:\n");
2037 	if (phba->sli4_hba.mbx_cq) {
2038 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2039 			"Associated EQID[%02d]:\n",
2040 			phba->sli4_hba.mbx_cq->assoc_qid);
2041 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2042 			"\tCQID[%02d], "
2043 			"QE-COUNT[%04d], QE-SIZE[%04d], "
2044 			"HOST-INDEX[%04d], PORT-INDEX[%04d]\n\n",
2045 			phba->sli4_hba.mbx_cq->queue_id,
2046 			phba->sli4_hba.mbx_cq->entry_count,
2047 			phba->sli4_hba.mbx_cq->entry_size,
2048 			phba->sli4_hba.mbx_cq->host_index,
2049 			phba->sli4_hba.mbx_cq->hba_index);
2050 	}
2051 
2052 	/* Get slow-path complete queue information */
2053 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2054 			"Slow-path ELS CQ information:\n");
2055 	if (phba->sli4_hba.els_cq) {
2056 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2057 			"Associated EQID[%02d]:\n",
2058 			phba->sli4_hba.els_cq->assoc_qid);
2059 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2060 			"\tCQID [%02d], "
2061 			"QE-COUNT[%04d], QE-SIZE[%04d], "
2062 			"HOST-INDEX[%04d], PORT-INDEX[%04d]\n\n",
2063 			phba->sli4_hba.els_cq->queue_id,
2064 			phba->sli4_hba.els_cq->entry_count,
2065 			phba->sli4_hba.els_cq->entry_size,
2066 			phba->sli4_hba.els_cq->host_index,
2067 			phba->sli4_hba.els_cq->hba_index);
2068 	}
2069 
2070 	/* Get fast-path complete queue information */
2071 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2072 			"Fast-path FCP CQ information:\n");
2073 	fcp_qidx = 0;
2074 	if (phba->sli4_hba.fcp_cq) {
2075 		do {
2076 			if (phba->sli4_hba.fcp_cq[fcp_qidx]) {
2077 				len += snprintf(pbuffer+len,
2078 					LPFC_QUE_INFO_GET_BUF_SIZE-len,
2079 				"Associated EQID[%02d]:\n",
2080 				phba->sli4_hba.fcp_cq[fcp_qidx]->assoc_qid);
2081 				len += snprintf(pbuffer+len,
2082 					LPFC_QUE_INFO_GET_BUF_SIZE-len,
2083 				"\tCQID[%02d], "
2084 				"QE-COUNT[%04d], QE-SIZE[%04d], "
2085 				"HOST-INDEX[%04d], PORT-INDEX[%04d]\n",
2086 				phba->sli4_hba.fcp_cq[fcp_qidx]->queue_id,
2087 				phba->sli4_hba.fcp_cq[fcp_qidx]->entry_count,
2088 				phba->sli4_hba.fcp_cq[fcp_qidx]->entry_size,
2089 				phba->sli4_hba.fcp_cq[fcp_qidx]->host_index,
2090 				phba->sli4_hba.fcp_cq[fcp_qidx]->hba_index);
2091 			}
2092 		} while (++fcp_qidx < phba->cfg_fcp_eq_count);
2093 		len += snprintf(pbuffer+len,
2094 				LPFC_QUE_INFO_GET_BUF_SIZE-len, "\n");
2095 	}
2096 
2097 	/* Get mailbox queue information */
2098 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2099 			"Slow-path MBX MQ information:\n");
2100 	if (phba->sli4_hba.mbx_wq) {
2101 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2102 			"Associated CQID[%02d]:\n",
2103 			phba->sli4_hba.mbx_wq->assoc_qid);
2104 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2105 			"\tWQID[%02d], "
2106 			"QE-COUNT[%04d], QE-SIZE[%04d], "
2107 			"HOST-INDEX[%04d], PORT-INDEX[%04d]\n\n",
2108 			phba->sli4_hba.mbx_wq->queue_id,
2109 			phba->sli4_hba.mbx_wq->entry_count,
2110 			phba->sli4_hba.mbx_wq->entry_size,
2111 			phba->sli4_hba.mbx_wq->host_index,
2112 			phba->sli4_hba.mbx_wq->hba_index);
2113 	}
2114 
2115 	/* Get slow-path work queue information */
2116 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2117 			"Slow-path ELS WQ information:\n");
2118 	if (phba->sli4_hba.els_wq) {
2119 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2120 			"Associated CQID[%02d]:\n",
2121 			phba->sli4_hba.els_wq->assoc_qid);
2122 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2123 			"\tWQID[%02d], "
2124 			"QE-COUNT[%04d], QE-SIZE[%04d], "
2125 			"HOST-INDEX[%04d], PORT-INDEX[%04d]\n\n",
2126 			phba->sli4_hba.els_wq->queue_id,
2127 			phba->sli4_hba.els_wq->entry_count,
2128 			phba->sli4_hba.els_wq->entry_size,
2129 			phba->sli4_hba.els_wq->host_index,
2130 			phba->sli4_hba.els_wq->hba_index);
2131 	}
2132 
2133 	/* Get fast-path work queue information */
2134 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2135 			"Fast-path FCP WQ information:\n");
2136 	if (phba->sli4_hba.fcp_wq) {
2137 		for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count;
2138 		     fcp_qidx++) {
2139 			if (!phba->sli4_hba.fcp_wq[fcp_qidx])
2140 				continue;
2141 			len += snprintf(pbuffer+len,
2142 					LPFC_QUE_INFO_GET_BUF_SIZE-len,
2143 				"Associated CQID[%02d]:\n",
2144 				phba->sli4_hba.fcp_wq[fcp_qidx]->assoc_qid);
2145 			len += snprintf(pbuffer+len,
2146 					LPFC_QUE_INFO_GET_BUF_SIZE-len,
2147 				"\tWQID[%02d], "
2148 				"QE-COUNT[%04d], WQE-SIZE[%04d], "
2149 				"HOST-INDEX[%04d], PORT-INDEX[%04d]\n",
2150 				phba->sli4_hba.fcp_wq[fcp_qidx]->queue_id,
2151 				phba->sli4_hba.fcp_wq[fcp_qidx]->entry_count,
2152 				phba->sli4_hba.fcp_wq[fcp_qidx]->entry_size,
2153 				phba->sli4_hba.fcp_wq[fcp_qidx]->host_index,
2154 				phba->sli4_hba.fcp_wq[fcp_qidx]->hba_index);
2155 		}
2156 		len += snprintf(pbuffer+len,
2157 				LPFC_QUE_INFO_GET_BUF_SIZE-len, "\n");
2158 	}
2159 
2160 	/* Get receive queue information */
2161 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2162 			"Slow-path RQ information:\n");
2163 	if (phba->sli4_hba.hdr_rq && phba->sli4_hba.dat_rq) {
2164 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2165 			"Associated CQID[%02d]:\n",
2166 			phba->sli4_hba.hdr_rq->assoc_qid);
2167 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2168 			"\tHQID[%02d], "
2169 			"QE-COUNT[%04d], QE-SIZE[%04d], "
2170 			"HOST-INDEX[%04d], PORT-INDEX[%04d]\n",
2171 			phba->sli4_hba.hdr_rq->queue_id,
2172 			phba->sli4_hba.hdr_rq->entry_count,
2173 			phba->sli4_hba.hdr_rq->entry_size,
2174 			phba->sli4_hba.hdr_rq->host_index,
2175 			phba->sli4_hba.hdr_rq->hba_index);
2176 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2177 			"\tDQID[%02d], "
2178 			"QE-COUNT[%04d], QE-SIZE[%04d], "
2179 			"HOST-INDEX[%04d], PORT-INDEX[%04d]\n",
2180 			phba->sli4_hba.dat_rq->queue_id,
2181 			phba->sli4_hba.dat_rq->entry_count,
2182 			phba->sli4_hba.dat_rq->entry_size,
2183 			phba->sli4_hba.dat_rq->host_index,
2184 			phba->sli4_hba.dat_rq->hba_index);
2185 	}
2186 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2187 }
2188 
2189 /**
2190  * lpfc_idiag_que_param_check - queue access command parameter sanity check
2191  * @q: The pointer to queue structure.
2192  * @index: The index into a queue entry.
2193  * @count: The number of queue entries to access.
2194  *
2195  * Description:
2196  * The routine performs sanity check on device queue access method commands.
2197  *
2198  * Returns:
2199  * This function returns -EINVAL when fails the sanity check, otherwise, it
2200  * returns 0.
2201  **/
2202 static int
2203 lpfc_idiag_que_param_check(struct lpfc_queue *q, int index, int count)
2204 {
2205 	/* Only support single entry read or browsing */
2206 	if ((count != 1) && (count != LPFC_QUE_ACC_BROWSE))
2207 		return -EINVAL;
2208 	if (index > q->entry_count - 1)
2209 		return -EINVAL;
2210 	return 0;
2211 }
2212 
2213 /**
2214  * lpfc_idiag_queacc_read_qe - read a single entry from the given queue index
2215  * @pbuffer: The pointer to buffer to copy the read data into.
2216  * @pque: The pointer to the queue to be read.
2217  * @index: The index into the queue entry.
2218  *
2219  * Description:
2220  * This routine reads out a single entry from the given queue's index location
2221  * and copies it into the buffer provided.
2222  *
2223  * Returns:
2224  * This function returns 0 when it fails, otherwise, it returns the length of
2225  * the data read into the buffer provided.
2226  **/
2227 static int
2228 lpfc_idiag_queacc_read_qe(char *pbuffer, int len, struct lpfc_queue *pque,
2229 			  uint32_t index)
2230 {
2231 	int offset, esize;
2232 	uint32_t *pentry;
2233 
2234 	if (!pbuffer || !pque)
2235 		return 0;
2236 
2237 	esize = pque->entry_size;
2238 	len += snprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
2239 			"QE-INDEX[%04d]:\n", index);
2240 
2241 	offset = 0;
2242 	pentry = pque->qe[index].address;
2243 	while (esize > 0) {
2244 		len += snprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
2245 				"%08x ", *pentry);
2246 		pentry++;
2247 		offset += sizeof(uint32_t);
2248 		esize -= sizeof(uint32_t);
2249 		if (esize > 0 && !(offset % (4 * sizeof(uint32_t))))
2250 			len += snprintf(pbuffer+len,
2251 					LPFC_QUE_ACC_BUF_SIZE-len, "\n");
2252 	}
2253 	len += snprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len, "\n");
2254 
2255 	return len;
2256 }
2257 
2258 /**
2259  * lpfc_idiag_queacc_read - idiag debugfs read port queue
2260  * @file: The file pointer to read from.
2261  * @buf: The buffer to copy the data to.
2262  * @nbytes: The number of bytes to read.
2263  * @ppos: The position in the file to start reading from.
2264  *
2265  * Description:
2266  * This routine reads data from the @phba device queue memory according to the
2267  * idiag command, and copies to user @buf. Depending on the queue dump read
2268  * command setup, it does either a single queue entry read or browing through
2269  * all entries of the queue.
2270  *
2271  * Returns:
2272  * This function returns the amount of data that was read (this could be less
2273  * than @nbytes if the end of the file was reached) or a negative error value.
2274  **/
2275 static ssize_t
2276 lpfc_idiag_queacc_read(struct file *file, char __user *buf, size_t nbytes,
2277 		       loff_t *ppos)
2278 {
2279 	struct lpfc_debug *debug = file->private_data;
2280 	uint32_t last_index, index, count;
2281 	struct lpfc_queue *pque = NULL;
2282 	char *pbuffer;
2283 	int len = 0;
2284 
2285 	/* This is a user read operation */
2286 	debug->op = LPFC_IDIAG_OP_RD;
2287 
2288 	if (!debug->buffer)
2289 		debug->buffer = kmalloc(LPFC_QUE_ACC_BUF_SIZE, GFP_KERNEL);
2290 	if (!debug->buffer)
2291 		return 0;
2292 	pbuffer = debug->buffer;
2293 
2294 	if (*ppos)
2295 		return 0;
2296 
2297 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
2298 		index = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
2299 		count = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
2300 		pque = (struct lpfc_queue *)idiag.ptr_private;
2301 	} else
2302 		return 0;
2303 
2304 	/* Browse the queue starting from index */
2305 	if (count == LPFC_QUE_ACC_BROWSE)
2306 		goto que_browse;
2307 
2308 	/* Read a single entry from the queue */
2309 	len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
2310 
2311 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2312 
2313 que_browse:
2314 
2315 	/* Browse all entries from the queue */
2316 	last_index = idiag.offset.last_rd;
2317 	index = last_index;
2318 
2319 	while (len < LPFC_QUE_ACC_SIZE - pque->entry_size) {
2320 		len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
2321 		index++;
2322 		if (index > pque->entry_count - 1)
2323 			break;
2324 	}
2325 
2326 	/* Set up the offset for next portion of pci cfg read */
2327 	if (index > pque->entry_count - 1)
2328 		index = 0;
2329 	idiag.offset.last_rd = index;
2330 
2331 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2332 }
2333 
2334 /**
2335  * lpfc_idiag_queacc_write - Syntax check and set up idiag queacc commands
2336  * @file: The file pointer to read from.
2337  * @buf: The buffer to copy the user data from.
2338  * @nbytes: The number of bytes to get.
2339  * @ppos: The position in the file to start reading from.
2340  *
2341  * This routine get the debugfs idiag command struct from user space and then
2342  * perform the syntax check for port queue read (dump) or write (set) command
2343  * accordingly. In the case of port queue read command, it sets up the command
2344  * in the idiag command struct for the following debugfs read operation. In
2345  * the case of port queue write operation, it executes the write operation
2346  * into the port queue entry accordingly.
2347  *
2348  * It returns the @nbytges passing in from debugfs user space when successful.
2349  * In case of error conditions, it returns proper error code back to the user
2350  * space.
2351  **/
2352 static ssize_t
2353 lpfc_idiag_queacc_write(struct file *file, const char __user *buf,
2354 			size_t nbytes, loff_t *ppos)
2355 {
2356 	struct lpfc_debug *debug = file->private_data;
2357 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2358 	uint32_t qidx, quetp, queid, index, count, offset, value;
2359 	uint32_t *pentry;
2360 	struct lpfc_queue *pque;
2361 	int rc;
2362 
2363 	/* This is a user write operation */
2364 	debug->op = LPFC_IDIAG_OP_WR;
2365 
2366 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
2367 	if (rc < 0)
2368 		return rc;
2369 
2370 	/* Get and sanity check on command feilds */
2371 	quetp  = idiag.cmd.data[IDIAG_QUEACC_QUETP_INDX];
2372 	queid  = idiag.cmd.data[IDIAG_QUEACC_QUEID_INDX];
2373 	index  = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
2374 	count  = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
2375 	offset = idiag.cmd.data[IDIAG_QUEACC_OFFST_INDX];
2376 	value  = idiag.cmd.data[IDIAG_QUEACC_VALUE_INDX];
2377 
2378 	/* Sanity check on command line arguments */
2379 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
2380 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
2381 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
2382 		if (rc != LPFC_QUE_ACC_WR_CMD_ARG)
2383 			goto error_out;
2384 		if (count != 1)
2385 			goto error_out;
2386 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
2387 		if (rc != LPFC_QUE_ACC_RD_CMD_ARG)
2388 			goto error_out;
2389 	} else
2390 		goto error_out;
2391 
2392 	switch (quetp) {
2393 	case LPFC_IDIAG_EQ:
2394 		/* Slow-path event queue */
2395 		if (phba->sli4_hba.sp_eq &&
2396 		    phba->sli4_hba.sp_eq->queue_id == queid) {
2397 			/* Sanity check */
2398 			rc = lpfc_idiag_que_param_check(
2399 					phba->sli4_hba.sp_eq, index, count);
2400 			if (rc)
2401 				goto error_out;
2402 			idiag.ptr_private = phba->sli4_hba.sp_eq;
2403 			goto pass_check;
2404 		}
2405 		/* Fast-path event queue */
2406 		if (phba->sli4_hba.fp_eq) {
2407 			for (qidx = 0; qidx < phba->cfg_fcp_eq_count; qidx++) {
2408 				if (phba->sli4_hba.fp_eq[qidx] &&
2409 				    phba->sli4_hba.fp_eq[qidx]->queue_id ==
2410 				    queid) {
2411 					/* Sanity check */
2412 					rc = lpfc_idiag_que_param_check(
2413 						phba->sli4_hba.fp_eq[qidx],
2414 						index, count);
2415 					if (rc)
2416 						goto error_out;
2417 					idiag.ptr_private =
2418 						phba->sli4_hba.fp_eq[qidx];
2419 					goto pass_check;
2420 				}
2421 			}
2422 		}
2423 		goto error_out;
2424 		break;
2425 	case LPFC_IDIAG_CQ:
2426 		/* MBX complete queue */
2427 		if (phba->sli4_hba.mbx_cq &&
2428 		    phba->sli4_hba.mbx_cq->queue_id == queid) {
2429 			/* Sanity check */
2430 			rc = lpfc_idiag_que_param_check(
2431 					phba->sli4_hba.mbx_cq, index, count);
2432 			if (rc)
2433 				goto error_out;
2434 			idiag.ptr_private = phba->sli4_hba.mbx_cq;
2435 			goto pass_check;
2436 		}
2437 		/* ELS complete queue */
2438 		if (phba->sli4_hba.els_cq &&
2439 		    phba->sli4_hba.els_cq->queue_id == queid) {
2440 			/* Sanity check */
2441 			rc = lpfc_idiag_que_param_check(
2442 					phba->sli4_hba.els_cq, index, count);
2443 			if (rc)
2444 				goto error_out;
2445 			idiag.ptr_private = phba->sli4_hba.els_cq;
2446 			goto pass_check;
2447 		}
2448 		/* FCP complete queue */
2449 		if (phba->sli4_hba.fcp_cq) {
2450 			qidx = 0;
2451 			do {
2452 				if (phba->sli4_hba.fcp_cq[qidx] &&
2453 				    phba->sli4_hba.fcp_cq[qidx]->queue_id ==
2454 				    queid) {
2455 					/* Sanity check */
2456 					rc = lpfc_idiag_que_param_check(
2457 						phba->sli4_hba.fcp_cq[qidx],
2458 						index, count);
2459 					if (rc)
2460 						goto error_out;
2461 					idiag.ptr_private =
2462 						phba->sli4_hba.fcp_cq[qidx];
2463 					goto pass_check;
2464 				}
2465 			} while (++qidx < phba->cfg_fcp_eq_count);
2466 		}
2467 		goto error_out;
2468 		break;
2469 	case LPFC_IDIAG_MQ:
2470 		/* MBX work queue */
2471 		if (phba->sli4_hba.mbx_wq &&
2472 		    phba->sli4_hba.mbx_wq->queue_id == queid) {
2473 			/* Sanity check */
2474 			rc = lpfc_idiag_que_param_check(
2475 					phba->sli4_hba.mbx_wq, index, count);
2476 			if (rc)
2477 				goto error_out;
2478 			idiag.ptr_private = phba->sli4_hba.mbx_wq;
2479 			goto pass_check;
2480 		}
2481 		goto error_out;
2482 		break;
2483 	case LPFC_IDIAG_WQ:
2484 		/* ELS work queue */
2485 		if (phba->sli4_hba.els_wq &&
2486 		    phba->sli4_hba.els_wq->queue_id == queid) {
2487 			/* Sanity check */
2488 			rc = lpfc_idiag_que_param_check(
2489 					phba->sli4_hba.els_wq, index, count);
2490 			if (rc)
2491 				goto error_out;
2492 			idiag.ptr_private = phba->sli4_hba.els_wq;
2493 			goto pass_check;
2494 		}
2495 		/* FCP work queue */
2496 		if (phba->sli4_hba.fcp_wq) {
2497 			for (qidx = 0; qidx < phba->cfg_fcp_wq_count; qidx++) {
2498 				if (!phba->sli4_hba.fcp_wq[qidx])
2499 					continue;
2500 				if (phba->sli4_hba.fcp_wq[qidx]->queue_id ==
2501 				    queid) {
2502 					/* Sanity check */
2503 					rc = lpfc_idiag_que_param_check(
2504 						phba->sli4_hba.fcp_wq[qidx],
2505 						index, count);
2506 					if (rc)
2507 						goto error_out;
2508 					idiag.ptr_private =
2509 						phba->sli4_hba.fcp_wq[qidx];
2510 					goto pass_check;
2511 				}
2512 			}
2513 		}
2514 		goto error_out;
2515 		break;
2516 	case LPFC_IDIAG_RQ:
2517 		/* HDR queue */
2518 		if (phba->sli4_hba.hdr_rq &&
2519 		    phba->sli4_hba.hdr_rq->queue_id == queid) {
2520 			/* Sanity check */
2521 			rc = lpfc_idiag_que_param_check(
2522 					phba->sli4_hba.hdr_rq, index, count);
2523 			if (rc)
2524 				goto error_out;
2525 			idiag.ptr_private = phba->sli4_hba.hdr_rq;
2526 			goto pass_check;
2527 		}
2528 		/* DAT queue */
2529 		if (phba->sli4_hba.dat_rq &&
2530 		    phba->sli4_hba.dat_rq->queue_id == queid) {
2531 			/* Sanity check */
2532 			rc = lpfc_idiag_que_param_check(
2533 					phba->sli4_hba.dat_rq, index, count);
2534 			if (rc)
2535 				goto error_out;
2536 			idiag.ptr_private = phba->sli4_hba.dat_rq;
2537 			goto pass_check;
2538 		}
2539 		goto error_out;
2540 		break;
2541 	default:
2542 		goto error_out;
2543 		break;
2544 	}
2545 
2546 pass_check:
2547 
2548 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
2549 		if (count == LPFC_QUE_ACC_BROWSE)
2550 			idiag.offset.last_rd = index;
2551 	}
2552 
2553 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
2554 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
2555 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
2556 		/* Additional sanity checks on write operation */
2557 		pque = (struct lpfc_queue *)idiag.ptr_private;
2558 		if (offset > pque->entry_size/sizeof(uint32_t) - 1)
2559 			goto error_out;
2560 		pentry = pque->qe[index].address;
2561 		pentry += offset;
2562 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR)
2563 			*pentry = value;
2564 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST)
2565 			*pentry |= value;
2566 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL)
2567 			*pentry &= ~value;
2568 	}
2569 	return nbytes;
2570 
2571 error_out:
2572 	/* Clean out command structure on command error out */
2573 	memset(&idiag, 0, sizeof(idiag));
2574 	return -EINVAL;
2575 }
2576 
2577 /**
2578  * lpfc_idiag_drbacc_read_reg - idiag debugfs read a doorbell register
2579  * @phba: The pointer to hba structure.
2580  * @pbuffer: The pointer to the buffer to copy the data to.
2581  * @len: The lenght of bytes to copied.
2582  * @drbregid: The id to doorbell registers.
2583  *
2584  * Description:
2585  * This routine reads a doorbell register and copies its content to the
2586  * user buffer pointed to by @pbuffer.
2587  *
2588  * Returns:
2589  * This function returns the amount of data that was copied into @pbuffer.
2590  **/
2591 static int
2592 lpfc_idiag_drbacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
2593 			   int len, uint32_t drbregid)
2594 {
2595 
2596 	if (!pbuffer)
2597 		return 0;
2598 
2599 	switch (drbregid) {
2600 	case LPFC_DRB_EQCQ:
2601 		len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
2602 				"EQCQ-DRB-REG: 0x%08x\n",
2603 				readl(phba->sli4_hba.EQCQDBregaddr));
2604 		break;
2605 	case LPFC_DRB_MQ:
2606 		len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
2607 				"MQ-DRB-REG:   0x%08x\n",
2608 				readl(phba->sli4_hba.MQDBregaddr));
2609 		break;
2610 	case LPFC_DRB_WQ:
2611 		len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
2612 				"WQ-DRB-REG:   0x%08x\n",
2613 				readl(phba->sli4_hba.WQDBregaddr));
2614 		break;
2615 	case LPFC_DRB_RQ:
2616 		len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
2617 				"RQ-DRB-REG:   0x%08x\n",
2618 				readl(phba->sli4_hba.RQDBregaddr));
2619 		break;
2620 	default:
2621 		break;
2622 	}
2623 
2624 	return len;
2625 }
2626 
2627 /**
2628  * lpfc_idiag_drbacc_read - idiag debugfs read port doorbell
2629  * @file: The file pointer to read from.
2630  * @buf: The buffer to copy the data to.
2631  * @nbytes: The number of bytes to read.
2632  * @ppos: The position in the file to start reading from.
2633  *
2634  * Description:
2635  * This routine reads data from the @phba device doorbell register according
2636  * to the idiag command, and copies to user @buf. Depending on the doorbell
2637  * register read command setup, it does either a single doorbell register
2638  * read or dump all doorbell registers.
2639  *
2640  * Returns:
2641  * This function returns the amount of data that was read (this could be less
2642  * than @nbytes if the end of the file was reached) or a negative error value.
2643  **/
2644 static ssize_t
2645 lpfc_idiag_drbacc_read(struct file *file, char __user *buf, size_t nbytes,
2646 		       loff_t *ppos)
2647 {
2648 	struct lpfc_debug *debug = file->private_data;
2649 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2650 	uint32_t drb_reg_id, i;
2651 	char *pbuffer;
2652 	int len = 0;
2653 
2654 	/* This is a user read operation */
2655 	debug->op = LPFC_IDIAG_OP_RD;
2656 
2657 	if (!debug->buffer)
2658 		debug->buffer = kmalloc(LPFC_DRB_ACC_BUF_SIZE, GFP_KERNEL);
2659 	if (!debug->buffer)
2660 		return 0;
2661 	pbuffer = debug->buffer;
2662 
2663 	if (*ppos)
2664 		return 0;
2665 
2666 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD)
2667 		drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
2668 	else
2669 		return 0;
2670 
2671 	if (drb_reg_id == LPFC_DRB_ACC_ALL)
2672 		for (i = 1; i <= LPFC_DRB_MAX; i++)
2673 			len = lpfc_idiag_drbacc_read_reg(phba,
2674 							 pbuffer, len, i);
2675 	else
2676 		len = lpfc_idiag_drbacc_read_reg(phba,
2677 						 pbuffer, len, drb_reg_id);
2678 
2679 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2680 }
2681 
2682 /**
2683  * lpfc_idiag_drbacc_write - Syntax check and set up idiag drbacc commands
2684  * @file: The file pointer to read from.
2685  * @buf: The buffer to copy the user data from.
2686  * @nbytes: The number of bytes to get.
2687  * @ppos: The position in the file to start reading from.
2688  *
2689  * This routine get the debugfs idiag command struct from user space and then
2690  * perform the syntax check for port doorbell register read (dump) or write
2691  * (set) command accordingly. In the case of port queue read command, it sets
2692  * up the command in the idiag command struct for the following debugfs read
2693  * operation. In the case of port doorbell register write operation, it
2694  * executes the write operation into the port doorbell register accordingly.
2695  *
2696  * It returns the @nbytges passing in from debugfs user space when successful.
2697  * In case of error conditions, it returns proper error code back to the user
2698  * space.
2699  **/
2700 static ssize_t
2701 lpfc_idiag_drbacc_write(struct file *file, const char __user *buf,
2702 			size_t nbytes, loff_t *ppos)
2703 {
2704 	struct lpfc_debug *debug = file->private_data;
2705 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2706 	uint32_t drb_reg_id, value, reg_val = 0;
2707 	void __iomem *drb_reg;
2708 	int rc;
2709 
2710 	/* This is a user write operation */
2711 	debug->op = LPFC_IDIAG_OP_WR;
2712 
2713 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
2714 	if (rc < 0)
2715 		return rc;
2716 
2717 	/* Sanity check on command line arguments */
2718 	drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
2719 	value = idiag.cmd.data[IDIAG_DRBACC_VALUE_INDX];
2720 
2721 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
2722 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
2723 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
2724 		if (rc != LPFC_DRB_ACC_WR_CMD_ARG)
2725 			goto error_out;
2726 		if (drb_reg_id > LPFC_DRB_MAX)
2727 			goto error_out;
2728 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD) {
2729 		if (rc != LPFC_DRB_ACC_RD_CMD_ARG)
2730 			goto error_out;
2731 		if ((drb_reg_id > LPFC_DRB_MAX) &&
2732 		    (drb_reg_id != LPFC_DRB_ACC_ALL))
2733 			goto error_out;
2734 	} else
2735 		goto error_out;
2736 
2737 	/* Perform the write access operation */
2738 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
2739 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
2740 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
2741 		switch (drb_reg_id) {
2742 		case LPFC_DRB_EQCQ:
2743 			drb_reg = phba->sli4_hba.EQCQDBregaddr;
2744 			break;
2745 		case LPFC_DRB_MQ:
2746 			drb_reg = phba->sli4_hba.MQDBregaddr;
2747 			break;
2748 		case LPFC_DRB_WQ:
2749 			drb_reg = phba->sli4_hba.WQDBregaddr;
2750 			break;
2751 		case LPFC_DRB_RQ:
2752 			drb_reg = phba->sli4_hba.RQDBregaddr;
2753 			break;
2754 		default:
2755 			goto error_out;
2756 		}
2757 
2758 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR)
2759 			reg_val = value;
2760 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST) {
2761 			reg_val = readl(drb_reg);
2762 			reg_val |= value;
2763 		}
2764 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
2765 			reg_val = readl(drb_reg);
2766 			reg_val &= ~value;
2767 		}
2768 		writel(reg_val, drb_reg);
2769 		readl(drb_reg); /* flush */
2770 	}
2771 	return nbytes;
2772 
2773 error_out:
2774 	/* Clean out command structure on command error out */
2775 	memset(&idiag, 0, sizeof(idiag));
2776 	return -EINVAL;
2777 }
2778 
2779 /**
2780  * lpfc_idiag_ctlacc_read_reg - idiag debugfs read a control registers
2781  * @phba: The pointer to hba structure.
2782  * @pbuffer: The pointer to the buffer to copy the data to.
2783  * @len: The lenght of bytes to copied.
2784  * @drbregid: The id to doorbell registers.
2785  *
2786  * Description:
2787  * This routine reads a control register and copies its content to the
2788  * user buffer pointed to by @pbuffer.
2789  *
2790  * Returns:
2791  * This function returns the amount of data that was copied into @pbuffer.
2792  **/
2793 static int
2794 lpfc_idiag_ctlacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
2795 			   int len, uint32_t ctlregid)
2796 {
2797 
2798 	if (!pbuffer)
2799 		return 0;
2800 
2801 	switch (ctlregid) {
2802 	case LPFC_CTL_PORT_SEM:
2803 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
2804 				"Port SemReg:   0x%08x\n",
2805 				readl(phba->sli4_hba.conf_regs_memmap_p +
2806 				      LPFC_CTL_PORT_SEM_OFFSET));
2807 		break;
2808 	case LPFC_CTL_PORT_STA:
2809 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
2810 				"Port StaReg:   0x%08x\n",
2811 				readl(phba->sli4_hba.conf_regs_memmap_p +
2812 				      LPFC_CTL_PORT_STA_OFFSET));
2813 		break;
2814 	case LPFC_CTL_PORT_CTL:
2815 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
2816 				"Port CtlReg:   0x%08x\n",
2817 				readl(phba->sli4_hba.conf_regs_memmap_p +
2818 				      LPFC_CTL_PORT_CTL_OFFSET));
2819 		break;
2820 	case LPFC_CTL_PORT_ER1:
2821 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
2822 				"Port Er1Reg:   0x%08x\n",
2823 				readl(phba->sli4_hba.conf_regs_memmap_p +
2824 				      LPFC_CTL_PORT_ER1_OFFSET));
2825 		break;
2826 	case LPFC_CTL_PORT_ER2:
2827 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
2828 				"Port Er2Reg:   0x%08x\n",
2829 				readl(phba->sli4_hba.conf_regs_memmap_p +
2830 				      LPFC_CTL_PORT_ER2_OFFSET));
2831 		break;
2832 	case LPFC_CTL_PDEV_CTL:
2833 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
2834 				"PDev CtlReg:   0x%08x\n",
2835 				readl(phba->sli4_hba.conf_regs_memmap_p +
2836 				      LPFC_CTL_PDEV_CTL_OFFSET));
2837 		break;
2838 	default:
2839 		break;
2840 	}
2841 	return len;
2842 }
2843 
2844 /**
2845  * lpfc_idiag_ctlacc_read - idiag debugfs read port and device control register
2846  * @file: The file pointer to read from.
2847  * @buf: The buffer to copy the data to.
2848  * @nbytes: The number of bytes to read.
2849  * @ppos: The position in the file to start reading from.
2850  *
2851  * Description:
2852  * This routine reads data from the @phba port and device registers according
2853  * to the idiag command, and copies to user @buf.
2854  *
2855  * Returns:
2856  * This function returns the amount of data that was read (this could be less
2857  * than @nbytes if the end of the file was reached) or a negative error value.
2858  **/
2859 static ssize_t
2860 lpfc_idiag_ctlacc_read(struct file *file, char __user *buf, size_t nbytes,
2861 		       loff_t *ppos)
2862 {
2863 	struct lpfc_debug *debug = file->private_data;
2864 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2865 	uint32_t ctl_reg_id, i;
2866 	char *pbuffer;
2867 	int len = 0;
2868 
2869 	/* This is a user read operation */
2870 	debug->op = LPFC_IDIAG_OP_RD;
2871 
2872 	if (!debug->buffer)
2873 		debug->buffer = kmalloc(LPFC_CTL_ACC_BUF_SIZE, GFP_KERNEL);
2874 	if (!debug->buffer)
2875 		return 0;
2876 	pbuffer = debug->buffer;
2877 
2878 	if (*ppos)
2879 		return 0;
2880 
2881 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD)
2882 		ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
2883 	else
2884 		return 0;
2885 
2886 	if (ctl_reg_id == LPFC_CTL_ACC_ALL)
2887 		for (i = 1; i <= LPFC_CTL_MAX; i++)
2888 			len = lpfc_idiag_ctlacc_read_reg(phba,
2889 							 pbuffer, len, i);
2890 	else
2891 		len = lpfc_idiag_ctlacc_read_reg(phba,
2892 						 pbuffer, len, ctl_reg_id);
2893 
2894 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2895 }
2896 
2897 /**
2898  * lpfc_idiag_ctlacc_write - Syntax check and set up idiag ctlacc commands
2899  * @file: The file pointer to read from.
2900  * @buf: The buffer to copy the user data from.
2901  * @nbytes: The number of bytes to get.
2902  * @ppos: The position in the file to start reading from.
2903  *
2904  * This routine get the debugfs idiag command struct from user space and then
2905  * perform the syntax check for port and device control register read (dump)
2906  * or write (set) command accordingly.
2907  *
2908  * It returns the @nbytges passing in from debugfs user space when successful.
2909  * In case of error conditions, it returns proper error code back to the user
2910  * space.
2911  **/
2912 static ssize_t
2913 lpfc_idiag_ctlacc_write(struct file *file, const char __user *buf,
2914 			size_t nbytes, loff_t *ppos)
2915 {
2916 	struct lpfc_debug *debug = file->private_data;
2917 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2918 	uint32_t ctl_reg_id, value, reg_val = 0;
2919 	void __iomem *ctl_reg;
2920 	int rc;
2921 
2922 	/* This is a user write operation */
2923 	debug->op = LPFC_IDIAG_OP_WR;
2924 
2925 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
2926 	if (rc < 0)
2927 		return rc;
2928 
2929 	/* Sanity check on command line arguments */
2930 	ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
2931 	value = idiag.cmd.data[IDIAG_CTLACC_VALUE_INDX];
2932 
2933 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
2934 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
2935 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
2936 		if (rc != LPFC_CTL_ACC_WR_CMD_ARG)
2937 			goto error_out;
2938 		if (ctl_reg_id > LPFC_CTL_MAX)
2939 			goto error_out;
2940 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD) {
2941 		if (rc != LPFC_CTL_ACC_RD_CMD_ARG)
2942 			goto error_out;
2943 		if ((ctl_reg_id > LPFC_CTL_MAX) &&
2944 		    (ctl_reg_id != LPFC_CTL_ACC_ALL))
2945 			goto error_out;
2946 	} else
2947 		goto error_out;
2948 
2949 	/* Perform the write access operation */
2950 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
2951 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
2952 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
2953 		switch (ctl_reg_id) {
2954 		case LPFC_CTL_PORT_SEM:
2955 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
2956 					LPFC_CTL_PORT_SEM_OFFSET;
2957 			break;
2958 		case LPFC_CTL_PORT_STA:
2959 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
2960 					LPFC_CTL_PORT_STA_OFFSET;
2961 			break;
2962 		case LPFC_CTL_PORT_CTL:
2963 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
2964 					LPFC_CTL_PORT_CTL_OFFSET;
2965 			break;
2966 		case LPFC_CTL_PORT_ER1:
2967 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
2968 					LPFC_CTL_PORT_ER1_OFFSET;
2969 			break;
2970 		case LPFC_CTL_PORT_ER2:
2971 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
2972 					LPFC_CTL_PORT_ER2_OFFSET;
2973 			break;
2974 		case LPFC_CTL_PDEV_CTL:
2975 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
2976 					LPFC_CTL_PDEV_CTL_OFFSET;
2977 			break;
2978 		default:
2979 			goto error_out;
2980 		}
2981 
2982 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR)
2983 			reg_val = value;
2984 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST) {
2985 			reg_val = readl(ctl_reg);
2986 			reg_val |= value;
2987 		}
2988 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
2989 			reg_val = readl(ctl_reg);
2990 			reg_val &= ~value;
2991 		}
2992 		writel(reg_val, ctl_reg);
2993 		readl(ctl_reg); /* flush */
2994 	}
2995 	return nbytes;
2996 
2997 error_out:
2998 	/* Clean out command structure on command error out */
2999 	memset(&idiag, 0, sizeof(idiag));
3000 	return -EINVAL;
3001 }
3002 
3003 /**
3004  * lpfc_idiag_mbxacc_get_setup - idiag debugfs get mailbox access setup
3005  * @phba: Pointer to HBA context object.
3006  * @pbuffer: Pointer to data buffer.
3007  *
3008  * Description:
3009  * This routine gets the driver mailbox access debugfs setup information.
3010  *
3011  * Returns:
3012  * This function returns the amount of data that was read (this could be less
3013  * than @nbytes if the end of the file was reached) or a negative error value.
3014  **/
3015 static int
3016 lpfc_idiag_mbxacc_get_setup(struct lpfc_hba *phba, char *pbuffer)
3017 {
3018 	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
3019 	int len = 0;
3020 
3021 	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
3022 	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
3023 	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
3024 	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
3025 
3026 	len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
3027 			"mbx_dump_map: 0x%08x\n", mbx_dump_map);
3028 	len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
3029 			"mbx_dump_cnt: %04d\n", mbx_dump_cnt);
3030 	len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
3031 			"mbx_word_cnt: %04d\n", mbx_word_cnt);
3032 	len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
3033 			"mbx_mbox_cmd: 0x%02x\n", mbx_mbox_cmd);
3034 
3035 	return len;
3036 }
3037 
3038 /**
3039  * lpfc_idiag_mbxacc_read - idiag debugfs read on mailbox access
3040  * @file: The file pointer to read from.
3041  * @buf: The buffer to copy the data to.
3042  * @nbytes: The number of bytes to read.
3043  * @ppos: The position in the file to start reading from.
3044  *
3045  * Description:
3046  * This routine reads data from the @phba driver mailbox access debugfs setup
3047  * information.
3048  *
3049  * Returns:
3050  * This function returns the amount of data that was read (this could be less
3051  * than @nbytes if the end of the file was reached) or a negative error value.
3052  **/
3053 static ssize_t
3054 lpfc_idiag_mbxacc_read(struct file *file, char __user *buf, size_t nbytes,
3055 		       loff_t *ppos)
3056 {
3057 	struct lpfc_debug *debug = file->private_data;
3058 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3059 	char *pbuffer;
3060 	int len = 0;
3061 
3062 	/* This is a user read operation */
3063 	debug->op = LPFC_IDIAG_OP_RD;
3064 
3065 	if (!debug->buffer)
3066 		debug->buffer = kmalloc(LPFC_MBX_ACC_BUF_SIZE, GFP_KERNEL);
3067 	if (!debug->buffer)
3068 		return 0;
3069 	pbuffer = debug->buffer;
3070 
3071 	if (*ppos)
3072 		return 0;
3073 
3074 	if ((idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP) &&
3075 	    (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP))
3076 		return 0;
3077 
3078 	len = lpfc_idiag_mbxacc_get_setup(phba, pbuffer);
3079 
3080 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3081 }
3082 
3083 /**
3084  * lpfc_idiag_mbxacc_write - Syntax check and set up idiag mbxacc commands
3085  * @file: The file pointer to read from.
3086  * @buf: The buffer to copy the user data from.
3087  * @nbytes: The number of bytes to get.
3088  * @ppos: The position in the file to start reading from.
3089  *
3090  * This routine get the debugfs idiag command struct from user space and then
3091  * perform the syntax check for driver mailbox command (dump) and sets up the
3092  * necessary states in the idiag command struct accordingly.
3093  *
3094  * It returns the @nbytges passing in from debugfs user space when successful.
3095  * In case of error conditions, it returns proper error code back to the user
3096  * space.
3097  **/
3098 static ssize_t
3099 lpfc_idiag_mbxacc_write(struct file *file, const char __user *buf,
3100 			size_t nbytes, loff_t *ppos)
3101 {
3102 	struct lpfc_debug *debug = file->private_data;
3103 	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
3104 	int rc;
3105 
3106 	/* This is a user write operation */
3107 	debug->op = LPFC_IDIAG_OP_WR;
3108 
3109 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3110 	if (rc < 0)
3111 		return rc;
3112 
3113 	/* Sanity check on command line arguments */
3114 	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
3115 	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
3116 	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
3117 	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
3118 
3119 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_MBXACC_DP) {
3120 		if (!(mbx_dump_map & LPFC_MBX_DMP_MBX_ALL))
3121 			goto error_out;
3122 		if ((mbx_dump_map & ~LPFC_MBX_DMP_MBX_ALL) &&
3123 		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
3124 			goto error_out;
3125 		if (mbx_word_cnt > sizeof(MAILBOX_t))
3126 			goto error_out;
3127 	} else if (idiag.cmd.opcode == LPFC_IDIAG_BSG_MBXACC_DP) {
3128 		if (!(mbx_dump_map & LPFC_BSG_DMP_MBX_ALL))
3129 			goto error_out;
3130 		if ((mbx_dump_map & ~LPFC_BSG_DMP_MBX_ALL) &&
3131 		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
3132 			goto error_out;
3133 		if (mbx_word_cnt > (BSG_MBOX_SIZE)/4)
3134 			goto error_out;
3135 		if (mbx_mbox_cmd != 0x9b)
3136 			goto error_out;
3137 	} else
3138 		goto error_out;
3139 
3140 	if (mbx_word_cnt == 0)
3141 		goto error_out;
3142 	if (rc != LPFC_MBX_DMP_ARG)
3143 		goto error_out;
3144 	if (mbx_mbox_cmd & ~0xff)
3145 		goto error_out;
3146 
3147 	/* condition for stop mailbox dump */
3148 	if (mbx_dump_cnt == 0)
3149 		goto reset_out;
3150 
3151 	return nbytes;
3152 
3153 reset_out:
3154 	/* Clean out command structure on command error out */
3155 	memset(&idiag, 0, sizeof(idiag));
3156 	return nbytes;
3157 
3158 error_out:
3159 	/* Clean out command structure on command error out */
3160 	memset(&idiag, 0, sizeof(idiag));
3161 	return -EINVAL;
3162 }
3163 
3164 /**
3165  * lpfc_idiag_extacc_avail_get - get the available extents information
3166  * @phba: pointer to lpfc hba data structure.
3167  * @pbuffer: pointer to internal buffer.
3168  * @len: length into the internal buffer data has been copied.
3169  *
3170  * Description:
3171  * This routine is to get the available extent information.
3172  *
3173  * Returns:
3174  * overall lenth of the data read into the internal buffer.
3175  **/
3176 static int
3177 lpfc_idiag_extacc_avail_get(struct lpfc_hba *phba, char *pbuffer, int len)
3178 {
3179 	uint16_t ext_cnt, ext_size;
3180 
3181 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3182 			"\nAvailable Extents Information:\n");
3183 
3184 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3185 			"\tPort Available VPI extents: ");
3186 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VPI,
3187 				       &ext_cnt, &ext_size);
3188 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3189 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
3190 
3191 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3192 			"\tPort Available VFI extents: ");
3193 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VFI,
3194 				       &ext_cnt, &ext_size);
3195 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3196 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
3197 
3198 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3199 			"\tPort Available RPI extents: ");
3200 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_RPI,
3201 				       &ext_cnt, &ext_size);
3202 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3203 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
3204 
3205 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3206 			"\tPort Available XRI extents: ");
3207 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_XRI,
3208 				       &ext_cnt, &ext_size);
3209 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3210 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
3211 
3212 	return len;
3213 }
3214 
3215 /**
3216  * lpfc_idiag_extacc_alloc_get - get the allocated extents information
3217  * @phba: pointer to lpfc hba data structure.
3218  * @pbuffer: pointer to internal buffer.
3219  * @len: length into the internal buffer data has been copied.
3220  *
3221  * Description:
3222  * This routine is to get the allocated extent information.
3223  *
3224  * Returns:
3225  * overall lenth of the data read into the internal buffer.
3226  **/
3227 static int
3228 lpfc_idiag_extacc_alloc_get(struct lpfc_hba *phba, char *pbuffer, int len)
3229 {
3230 	uint16_t ext_cnt, ext_size;
3231 	int rc;
3232 
3233 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3234 			"\nAllocated Extents Information:\n");
3235 
3236 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3237 			"\tHost Allocated VPI extents: ");
3238 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VPI,
3239 					    &ext_cnt, &ext_size);
3240 	if (!rc)
3241 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3242 				"Port %d Extent %3d, Size %3d\n",
3243 				phba->brd_no, ext_cnt, ext_size);
3244 	else
3245 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3246 				"N/A\n");
3247 
3248 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3249 			"\tHost Allocated VFI extents: ");
3250 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VFI,
3251 					    &ext_cnt, &ext_size);
3252 	if (!rc)
3253 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3254 				"Port %d Extent %3d, Size %3d\n",
3255 				phba->brd_no, ext_cnt, ext_size);
3256 	else
3257 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3258 				"N/A\n");
3259 
3260 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3261 			"\tHost Allocated RPI extents: ");
3262 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_RPI,
3263 					    &ext_cnt, &ext_size);
3264 	if (!rc)
3265 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3266 				"Port %d Extent %3d, Size %3d\n",
3267 				phba->brd_no, ext_cnt, ext_size);
3268 	else
3269 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3270 				"N/A\n");
3271 
3272 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3273 			"\tHost Allocated XRI extents: ");
3274 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_XRI,
3275 					    &ext_cnt, &ext_size);
3276 	if (!rc)
3277 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3278 				"Port %d Extent %3d, Size %3d\n",
3279 				phba->brd_no, ext_cnt, ext_size);
3280 	else
3281 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3282 				"N/A\n");
3283 
3284 	return len;
3285 }
3286 
3287 /**
3288  * lpfc_idiag_extacc_drivr_get - get driver extent information
3289  * @phba: pointer to lpfc hba data structure.
3290  * @pbuffer: pointer to internal buffer.
3291  * @len: length into the internal buffer data has been copied.
3292  *
3293  * Description:
3294  * This routine is to get the driver extent information.
3295  *
3296  * Returns:
3297  * overall lenth of the data read into the internal buffer.
3298  **/
3299 static int
3300 lpfc_idiag_extacc_drivr_get(struct lpfc_hba *phba, char *pbuffer, int len)
3301 {
3302 	struct lpfc_rsrc_blks *rsrc_blks;
3303 	int index;
3304 
3305 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3306 			"\nDriver Extents Information:\n");
3307 
3308 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3309 			"\tVPI extents:\n");
3310 	index = 0;
3311 	list_for_each_entry(rsrc_blks, &phba->lpfc_vpi_blk_list, list) {
3312 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3313 				"\t\tBlock %3d: Start %4d, Count %4d\n",
3314 				index, rsrc_blks->rsrc_start,
3315 				rsrc_blks->rsrc_size);
3316 		index++;
3317 	}
3318 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3319 			"\tVFI extents:\n");
3320 	index = 0;
3321 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_vfi_blk_list,
3322 			    list) {
3323 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3324 				"\t\tBlock %3d: Start %4d, Count %4d\n",
3325 				index, rsrc_blks->rsrc_start,
3326 				rsrc_blks->rsrc_size);
3327 		index++;
3328 	}
3329 
3330 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3331 			"\tRPI extents:\n");
3332 	index = 0;
3333 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_rpi_blk_list,
3334 			    list) {
3335 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3336 				"\t\tBlock %3d: Start %4d, Count %4d\n",
3337 				index, rsrc_blks->rsrc_start,
3338 				rsrc_blks->rsrc_size);
3339 		index++;
3340 	}
3341 
3342 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3343 			"\tXRI extents:\n");
3344 	index = 0;
3345 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_xri_blk_list,
3346 			    list) {
3347 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3348 				"\t\tBlock %3d: Start %4d, Count %4d\n",
3349 				index, rsrc_blks->rsrc_start,
3350 				rsrc_blks->rsrc_size);
3351 		index++;
3352 	}
3353 
3354 	return len;
3355 }
3356 
3357 /**
3358  * lpfc_idiag_extacc_write - Syntax check and set up idiag extacc commands
3359  * @file: The file pointer to read from.
3360  * @buf: The buffer to copy the user data from.
3361  * @nbytes: The number of bytes to get.
3362  * @ppos: The position in the file to start reading from.
3363  *
3364  * This routine get the debugfs idiag command struct from user space and then
3365  * perform the syntax check for extent information access commands and sets
3366  * up the necessary states in the idiag command struct accordingly.
3367  *
3368  * It returns the @nbytges passing in from debugfs user space when successful.
3369  * In case of error conditions, it returns proper error code back to the user
3370  * space.
3371  **/
3372 static ssize_t
3373 lpfc_idiag_extacc_write(struct file *file, const char __user *buf,
3374 			size_t nbytes, loff_t *ppos)
3375 {
3376 	struct lpfc_debug *debug = file->private_data;
3377 	uint32_t ext_map;
3378 	int rc;
3379 
3380 	/* This is a user write operation */
3381 	debug->op = LPFC_IDIAG_OP_WR;
3382 
3383 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3384 	if (rc < 0)
3385 		return rc;
3386 
3387 	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
3388 
3389 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
3390 		goto error_out;
3391 	if (rc != LPFC_EXT_ACC_CMD_ARG)
3392 		goto error_out;
3393 	if (!(ext_map & LPFC_EXT_ACC_ALL))
3394 		goto error_out;
3395 
3396 	return nbytes;
3397 error_out:
3398 	/* Clean out command structure on command error out */
3399 	memset(&idiag, 0, sizeof(idiag));
3400 	return -EINVAL;
3401 }
3402 
3403 /**
3404  * lpfc_idiag_extacc_read - idiag debugfs read access to extent information
3405  * @file: The file pointer to read from.
3406  * @buf: The buffer to copy the data to.
3407  * @nbytes: The number of bytes to read.
3408  * @ppos: The position in the file to start reading from.
3409  *
3410  * Description:
3411  * This routine reads data from the proper extent information according to
3412  * the idiag command, and copies to user @buf.
3413  *
3414  * Returns:
3415  * This function returns the amount of data that was read (this could be less
3416  * than @nbytes if the end of the file was reached) or a negative error value.
3417  **/
3418 static ssize_t
3419 lpfc_idiag_extacc_read(struct file *file, char __user *buf, size_t nbytes,
3420 		       loff_t *ppos)
3421 {
3422 	struct lpfc_debug *debug = file->private_data;
3423 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3424 	char *pbuffer;
3425 	uint32_t ext_map;
3426 	int len = 0;
3427 
3428 	/* This is a user read operation */
3429 	debug->op = LPFC_IDIAG_OP_RD;
3430 
3431 	if (!debug->buffer)
3432 		debug->buffer = kmalloc(LPFC_EXT_ACC_BUF_SIZE, GFP_KERNEL);
3433 	if (!debug->buffer)
3434 		return 0;
3435 	pbuffer = debug->buffer;
3436 	if (*ppos)
3437 		return 0;
3438 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
3439 		return 0;
3440 
3441 	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
3442 	if (ext_map & LPFC_EXT_ACC_AVAIL)
3443 		len = lpfc_idiag_extacc_avail_get(phba, pbuffer, len);
3444 	if (ext_map & LPFC_EXT_ACC_ALLOC)
3445 		len = lpfc_idiag_extacc_alloc_get(phba, pbuffer, len);
3446 	if (ext_map & LPFC_EXT_ACC_DRIVR)
3447 		len = lpfc_idiag_extacc_drivr_get(phba, pbuffer, len);
3448 
3449 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3450 }
3451 
3452 #undef lpfc_debugfs_op_disc_trc
3453 static const struct file_operations lpfc_debugfs_op_disc_trc = {
3454 	.owner =        THIS_MODULE,
3455 	.open =         lpfc_debugfs_disc_trc_open,
3456 	.llseek =       lpfc_debugfs_lseek,
3457 	.read =         lpfc_debugfs_read,
3458 	.release =      lpfc_debugfs_release,
3459 };
3460 
3461 #undef lpfc_debugfs_op_nodelist
3462 static const struct file_operations lpfc_debugfs_op_nodelist = {
3463 	.owner =        THIS_MODULE,
3464 	.open =         lpfc_debugfs_nodelist_open,
3465 	.llseek =       lpfc_debugfs_lseek,
3466 	.read =         lpfc_debugfs_read,
3467 	.release =      lpfc_debugfs_release,
3468 };
3469 
3470 #undef lpfc_debugfs_op_hbqinfo
3471 static const struct file_operations lpfc_debugfs_op_hbqinfo = {
3472 	.owner =        THIS_MODULE,
3473 	.open =         lpfc_debugfs_hbqinfo_open,
3474 	.llseek =       lpfc_debugfs_lseek,
3475 	.read =         lpfc_debugfs_read,
3476 	.release =      lpfc_debugfs_release,
3477 };
3478 
3479 #undef lpfc_debugfs_op_dumpHBASlim
3480 static const struct file_operations lpfc_debugfs_op_dumpHBASlim = {
3481 	.owner =        THIS_MODULE,
3482 	.open =         lpfc_debugfs_dumpHBASlim_open,
3483 	.llseek =       lpfc_debugfs_lseek,
3484 	.read =         lpfc_debugfs_read,
3485 	.release =      lpfc_debugfs_release,
3486 };
3487 
3488 #undef lpfc_debugfs_op_dumpHostSlim
3489 static const struct file_operations lpfc_debugfs_op_dumpHostSlim = {
3490 	.owner =        THIS_MODULE,
3491 	.open =         lpfc_debugfs_dumpHostSlim_open,
3492 	.llseek =       lpfc_debugfs_lseek,
3493 	.read =         lpfc_debugfs_read,
3494 	.release =      lpfc_debugfs_release,
3495 };
3496 
3497 #undef lpfc_debugfs_op_dumpData
3498 static const struct file_operations lpfc_debugfs_op_dumpData = {
3499 	.owner =        THIS_MODULE,
3500 	.open =         lpfc_debugfs_dumpData_open,
3501 	.llseek =       lpfc_debugfs_lseek,
3502 	.read =         lpfc_debugfs_read,
3503 	.write =	lpfc_debugfs_dumpDataDif_write,
3504 	.release =      lpfc_debugfs_dumpDataDif_release,
3505 };
3506 
3507 #undef lpfc_debugfs_op_dumpDif
3508 static const struct file_operations lpfc_debugfs_op_dumpDif = {
3509 	.owner =        THIS_MODULE,
3510 	.open =         lpfc_debugfs_dumpDif_open,
3511 	.llseek =       lpfc_debugfs_lseek,
3512 	.read =         lpfc_debugfs_read,
3513 	.write =	lpfc_debugfs_dumpDataDif_write,
3514 	.release =      lpfc_debugfs_dumpDataDif_release,
3515 };
3516 
3517 #undef lpfc_debugfs_op_dif_err
3518 static const struct file_operations lpfc_debugfs_op_dif_err = {
3519 	.owner =	THIS_MODULE,
3520 	.open =		lpfc_debugfs_dif_err_open,
3521 	.llseek =	lpfc_debugfs_lseek,
3522 	.read =		lpfc_debugfs_dif_err_read,
3523 	.write =	lpfc_debugfs_dif_err_write,
3524 	.release =	lpfc_debugfs_dif_err_release,
3525 };
3526 
3527 #undef lpfc_debugfs_op_slow_ring_trc
3528 static const struct file_operations lpfc_debugfs_op_slow_ring_trc = {
3529 	.owner =        THIS_MODULE,
3530 	.open =         lpfc_debugfs_slow_ring_trc_open,
3531 	.llseek =       lpfc_debugfs_lseek,
3532 	.read =         lpfc_debugfs_read,
3533 	.release =      lpfc_debugfs_release,
3534 };
3535 
3536 static struct dentry *lpfc_debugfs_root = NULL;
3537 static atomic_t lpfc_debugfs_hba_count;
3538 
3539 /*
3540  * File operations for the iDiag debugfs
3541  */
3542 #undef lpfc_idiag_op_pciCfg
3543 static const struct file_operations lpfc_idiag_op_pciCfg = {
3544 	.owner =        THIS_MODULE,
3545 	.open =         lpfc_idiag_open,
3546 	.llseek =       lpfc_debugfs_lseek,
3547 	.read =         lpfc_idiag_pcicfg_read,
3548 	.write =        lpfc_idiag_pcicfg_write,
3549 	.release =      lpfc_idiag_cmd_release,
3550 };
3551 
3552 #undef lpfc_idiag_op_barAcc
3553 static const struct file_operations lpfc_idiag_op_barAcc = {
3554 	.owner =        THIS_MODULE,
3555 	.open =         lpfc_idiag_open,
3556 	.llseek =       lpfc_debugfs_lseek,
3557 	.read =         lpfc_idiag_baracc_read,
3558 	.write =        lpfc_idiag_baracc_write,
3559 	.release =      lpfc_idiag_cmd_release,
3560 };
3561 
3562 #undef lpfc_idiag_op_queInfo
3563 static const struct file_operations lpfc_idiag_op_queInfo = {
3564 	.owner =        THIS_MODULE,
3565 	.open =         lpfc_idiag_open,
3566 	.read =         lpfc_idiag_queinfo_read,
3567 	.release =      lpfc_idiag_release,
3568 };
3569 
3570 #undef lpfc_idiag_op_queAcc
3571 static const struct file_operations lpfc_idiag_op_queAcc = {
3572 	.owner =        THIS_MODULE,
3573 	.open =         lpfc_idiag_open,
3574 	.llseek =       lpfc_debugfs_lseek,
3575 	.read =         lpfc_idiag_queacc_read,
3576 	.write =        lpfc_idiag_queacc_write,
3577 	.release =      lpfc_idiag_cmd_release,
3578 };
3579 
3580 #undef lpfc_idiag_op_drbAcc
3581 static const struct file_operations lpfc_idiag_op_drbAcc = {
3582 	.owner =        THIS_MODULE,
3583 	.open =         lpfc_idiag_open,
3584 	.llseek =       lpfc_debugfs_lseek,
3585 	.read =         lpfc_idiag_drbacc_read,
3586 	.write =        lpfc_idiag_drbacc_write,
3587 	.release =      lpfc_idiag_cmd_release,
3588 };
3589 
3590 #undef lpfc_idiag_op_ctlAcc
3591 static const struct file_operations lpfc_idiag_op_ctlAcc = {
3592 	.owner =        THIS_MODULE,
3593 	.open =         lpfc_idiag_open,
3594 	.llseek =       lpfc_debugfs_lseek,
3595 	.read =         lpfc_idiag_ctlacc_read,
3596 	.write =        lpfc_idiag_ctlacc_write,
3597 	.release =      lpfc_idiag_cmd_release,
3598 };
3599 
3600 #undef lpfc_idiag_op_mbxAcc
3601 static const struct file_operations lpfc_idiag_op_mbxAcc = {
3602 	.owner =        THIS_MODULE,
3603 	.open =         lpfc_idiag_open,
3604 	.llseek =       lpfc_debugfs_lseek,
3605 	.read =         lpfc_idiag_mbxacc_read,
3606 	.write =        lpfc_idiag_mbxacc_write,
3607 	.release =      lpfc_idiag_cmd_release,
3608 };
3609 
3610 #undef lpfc_idiag_op_extAcc
3611 static const struct file_operations lpfc_idiag_op_extAcc = {
3612 	.owner =        THIS_MODULE,
3613 	.open =         lpfc_idiag_open,
3614 	.llseek =       lpfc_debugfs_lseek,
3615 	.read =         lpfc_idiag_extacc_read,
3616 	.write =        lpfc_idiag_extacc_write,
3617 	.release =      lpfc_idiag_cmd_release,
3618 };
3619 
3620 #endif
3621 
3622 /* lpfc_idiag_mbxacc_dump_bsg_mbox - idiag debugfs dump bsg mailbox command
3623  * @phba: Pointer to HBA context object.
3624  * @dmabuf: Pointer to a DMA buffer descriptor.
3625  *
3626  * Description:
3627  * This routine dump a bsg pass-through non-embedded mailbox command with
3628  * external buffer.
3629  **/
3630 void
3631 lpfc_idiag_mbxacc_dump_bsg_mbox(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3632 				enum mbox_type mbox_tp, enum dma_type dma_tp,
3633 				enum sta_type sta_tp,
3634 				struct lpfc_dmabuf *dmabuf, uint32_t ext_buf)
3635 {
3636 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3637 	uint32_t *mbx_mbox_cmd, *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt;
3638 	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
3639 	int len = 0;
3640 	uint32_t do_dump = 0;
3641 	uint32_t *pword;
3642 	uint32_t i;
3643 
3644 	if (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP)
3645 		return;
3646 
3647 	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
3648 	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
3649 	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
3650 	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
3651 
3652 	if (!(*mbx_dump_map & LPFC_MBX_DMP_ALL) ||
3653 	    (*mbx_dump_cnt == 0) ||
3654 	    (*mbx_word_cnt == 0))
3655 		return;
3656 
3657 	if (*mbx_mbox_cmd != 0x9B)
3658 		return;
3659 
3660 	if ((mbox_tp == mbox_rd) && (dma_tp == dma_mbox)) {
3661 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_MBX) {
3662 			do_dump |= LPFC_BSG_DMP_MBX_RD_MBX;
3663 			printk(KERN_ERR "\nRead mbox command (x%x), "
3664 			       "nemb:0x%x, extbuf_cnt:%d:\n",
3665 			       sta_tp, nemb_tp, ext_buf);
3666 		}
3667 	}
3668 	if ((mbox_tp == mbox_rd) && (dma_tp == dma_ebuf)) {
3669 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_BUF) {
3670 			do_dump |= LPFC_BSG_DMP_MBX_RD_BUF;
3671 			printk(KERN_ERR "\nRead mbox buffer (x%x), "
3672 			       "nemb:0x%x, extbuf_seq:%d:\n",
3673 			       sta_tp, nemb_tp, ext_buf);
3674 		}
3675 	}
3676 	if ((mbox_tp == mbox_wr) && (dma_tp == dma_mbox)) {
3677 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_MBX) {
3678 			do_dump |= LPFC_BSG_DMP_MBX_WR_MBX;
3679 			printk(KERN_ERR "\nWrite mbox command (x%x), "
3680 			       "nemb:0x%x, extbuf_cnt:%d:\n",
3681 			       sta_tp, nemb_tp, ext_buf);
3682 		}
3683 	}
3684 	if ((mbox_tp == mbox_wr) && (dma_tp == dma_ebuf)) {
3685 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_BUF) {
3686 			do_dump |= LPFC_BSG_DMP_MBX_WR_BUF;
3687 			printk(KERN_ERR "\nWrite mbox buffer (x%x), "
3688 			       "nemb:0x%x, extbuf_seq:%d:\n",
3689 			       sta_tp, nemb_tp, ext_buf);
3690 		}
3691 	}
3692 
3693 	/* dump buffer content */
3694 	if (do_dump) {
3695 		pword = (uint32_t *)dmabuf->virt;
3696 		for (i = 0; i < *mbx_word_cnt; i++) {
3697 			if (!(i % 8)) {
3698 				if (i != 0)
3699 					printk(KERN_ERR "%s\n", line_buf);
3700 				len = 0;
3701 				len += snprintf(line_buf+len,
3702 						LPFC_MBX_ACC_LBUF_SZ-len,
3703 						"%03d: ", i);
3704 			}
3705 			len += snprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
3706 					"%08x ", (uint32_t)*pword);
3707 			pword++;
3708 		}
3709 		if ((i - 1) % 8)
3710 			printk(KERN_ERR "%s\n", line_buf);
3711 		(*mbx_dump_cnt)--;
3712 	}
3713 
3714 	/* Clean out command structure on reaching dump count */
3715 	if (*mbx_dump_cnt == 0)
3716 		memset(&idiag, 0, sizeof(idiag));
3717 	return;
3718 #endif
3719 }
3720 
3721 /* lpfc_idiag_mbxacc_dump_issue_mbox - idiag debugfs dump issue mailbox command
3722  * @phba: Pointer to HBA context object.
3723  * @dmabuf: Pointer to a DMA buffer descriptor.
3724  *
3725  * Description:
3726  * This routine dump a pass-through non-embedded mailbox command from issue
3727  * mailbox command.
3728  **/
3729 void
3730 lpfc_idiag_mbxacc_dump_issue_mbox(struct lpfc_hba *phba, MAILBOX_t *pmbox)
3731 {
3732 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3733 	uint32_t *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt, *mbx_mbox_cmd;
3734 	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
3735 	int len = 0;
3736 	uint32_t *pword;
3737 	uint8_t *pbyte;
3738 	uint32_t i, j;
3739 
3740 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP)
3741 		return;
3742 
3743 	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
3744 	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
3745 	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
3746 	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
3747 
3748 	if (!(*mbx_dump_map & LPFC_MBX_DMP_MBX_ALL) ||
3749 	    (*mbx_dump_cnt == 0) ||
3750 	    (*mbx_word_cnt == 0))
3751 		return;
3752 
3753 	if ((*mbx_mbox_cmd != LPFC_MBX_ALL_CMD) &&
3754 	    (*mbx_mbox_cmd != pmbox->mbxCommand))
3755 		return;
3756 
3757 	/* dump buffer content */
3758 	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_WORD) {
3759 		printk(KERN_ERR "Mailbox command:0x%x dump by word:\n",
3760 		       pmbox->mbxCommand);
3761 		pword = (uint32_t *)pmbox;
3762 		for (i = 0; i < *mbx_word_cnt; i++) {
3763 			if (!(i % 8)) {
3764 				if (i != 0)
3765 					printk(KERN_ERR "%s\n", line_buf);
3766 				len = 0;
3767 				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
3768 				len += snprintf(line_buf+len,
3769 						LPFC_MBX_ACC_LBUF_SZ-len,
3770 						"%03d: ", i);
3771 			}
3772 			len += snprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
3773 					"%08x ",
3774 					((uint32_t)*pword) & 0xffffffff);
3775 			pword++;
3776 		}
3777 		if ((i - 1) % 8)
3778 			printk(KERN_ERR "%s\n", line_buf);
3779 		printk(KERN_ERR "\n");
3780 	}
3781 	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_BYTE) {
3782 		printk(KERN_ERR "Mailbox command:0x%x dump by byte:\n",
3783 		       pmbox->mbxCommand);
3784 		pbyte = (uint8_t *)pmbox;
3785 		for (i = 0; i < *mbx_word_cnt; i++) {
3786 			if (!(i % 8)) {
3787 				if (i != 0)
3788 					printk(KERN_ERR "%s\n", line_buf);
3789 				len = 0;
3790 				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
3791 				len += snprintf(line_buf+len,
3792 						LPFC_MBX_ACC_LBUF_SZ-len,
3793 						"%03d: ", i);
3794 			}
3795 			for (j = 0; j < 4; j++) {
3796 				len += snprintf(line_buf+len,
3797 						LPFC_MBX_ACC_LBUF_SZ-len,
3798 						"%02x",
3799 						((uint8_t)*pbyte) & 0xff);
3800 				pbyte++;
3801 			}
3802 			len += snprintf(line_buf+len,
3803 					LPFC_MBX_ACC_LBUF_SZ-len, " ");
3804 		}
3805 		if ((i - 1) % 8)
3806 			printk(KERN_ERR "%s\n", line_buf);
3807 		printk(KERN_ERR "\n");
3808 	}
3809 	(*mbx_dump_cnt)--;
3810 
3811 	/* Clean out command structure on reaching dump count */
3812 	if (*mbx_dump_cnt == 0)
3813 		memset(&idiag, 0, sizeof(idiag));
3814 	return;
3815 #endif
3816 }
3817 
3818 /**
3819  * lpfc_debugfs_initialize - Initialize debugfs for a vport
3820  * @vport: The vport pointer to initialize.
3821  *
3822  * Description:
3823  * When Debugfs is configured this routine sets up the lpfc debugfs file system.
3824  * If not already created, this routine will create the lpfc directory, and
3825  * lpfcX directory (for this HBA), and vportX directory for this vport. It will
3826  * also create each file used to access lpfc specific debugfs information.
3827  **/
3828 inline void
3829 lpfc_debugfs_initialize(struct lpfc_vport *vport)
3830 {
3831 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3832 	struct lpfc_hba   *phba = vport->phba;
3833 	char name[64];
3834 	uint32_t num, i;
3835 
3836 	if (!lpfc_debugfs_enable)
3837 		return;
3838 
3839 	/* Setup lpfc root directory */
3840 	if (!lpfc_debugfs_root) {
3841 		lpfc_debugfs_root = debugfs_create_dir("lpfc", NULL);
3842 		atomic_set(&lpfc_debugfs_hba_count, 0);
3843 		if (!lpfc_debugfs_root) {
3844 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3845 					 "0408 Cannot create debugfs root\n");
3846 			goto debug_failed;
3847 		}
3848 	}
3849 	if (!lpfc_debugfs_start_time)
3850 		lpfc_debugfs_start_time = jiffies;
3851 
3852 	/* Setup funcX directory for specific HBA PCI function */
3853 	snprintf(name, sizeof(name), "fn%d", phba->brd_no);
3854 	if (!phba->hba_debugfs_root) {
3855 		phba->hba_debugfs_root =
3856 			debugfs_create_dir(name, lpfc_debugfs_root);
3857 		if (!phba->hba_debugfs_root) {
3858 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3859 					 "0412 Cannot create debugfs hba\n");
3860 			goto debug_failed;
3861 		}
3862 		atomic_inc(&lpfc_debugfs_hba_count);
3863 		atomic_set(&phba->debugfs_vport_count, 0);
3864 
3865 		/* Setup hbqinfo */
3866 		snprintf(name, sizeof(name), "hbqinfo");
3867 		phba->debug_hbqinfo =
3868 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3869 				 phba->hba_debugfs_root,
3870 				 phba, &lpfc_debugfs_op_hbqinfo);
3871 		if (!phba->debug_hbqinfo) {
3872 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3873 				"0411 Cannot create debugfs hbqinfo\n");
3874 			goto debug_failed;
3875 		}
3876 
3877 		/* Setup dumpHBASlim */
3878 		if (phba->sli_rev < LPFC_SLI_REV4) {
3879 			snprintf(name, sizeof(name), "dumpHBASlim");
3880 			phba->debug_dumpHBASlim =
3881 				debugfs_create_file(name,
3882 					S_IFREG|S_IRUGO|S_IWUSR,
3883 					phba->hba_debugfs_root,
3884 					phba, &lpfc_debugfs_op_dumpHBASlim);
3885 			if (!phba->debug_dumpHBASlim) {
3886 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3887 						 "0413 Cannot create debugfs "
3888 						"dumpHBASlim\n");
3889 				goto debug_failed;
3890 			}
3891 		} else
3892 			phba->debug_dumpHBASlim = NULL;
3893 
3894 		/* Setup dumpHostSlim */
3895 		if (phba->sli_rev < LPFC_SLI_REV4) {
3896 			snprintf(name, sizeof(name), "dumpHostSlim");
3897 			phba->debug_dumpHostSlim =
3898 				debugfs_create_file(name,
3899 					S_IFREG|S_IRUGO|S_IWUSR,
3900 					phba->hba_debugfs_root,
3901 					phba, &lpfc_debugfs_op_dumpHostSlim);
3902 			if (!phba->debug_dumpHostSlim) {
3903 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3904 						 "0414 Cannot create debugfs "
3905 						 "dumpHostSlim\n");
3906 				goto debug_failed;
3907 			}
3908 		} else
3909 			phba->debug_dumpHBASlim = NULL;
3910 
3911 		/* Setup dumpData */
3912 		snprintf(name, sizeof(name), "dumpData");
3913 		phba->debug_dumpData =
3914 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3915 				 phba->hba_debugfs_root,
3916 				 phba, &lpfc_debugfs_op_dumpData);
3917 		if (!phba->debug_dumpData) {
3918 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3919 				"0800 Cannot create debugfs dumpData\n");
3920 			goto debug_failed;
3921 		}
3922 
3923 		/* Setup dumpDif */
3924 		snprintf(name, sizeof(name), "dumpDif");
3925 		phba->debug_dumpDif =
3926 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3927 				 phba->hba_debugfs_root,
3928 				 phba, &lpfc_debugfs_op_dumpDif);
3929 		if (!phba->debug_dumpDif) {
3930 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3931 				"0801 Cannot create debugfs dumpDif\n");
3932 			goto debug_failed;
3933 		}
3934 
3935 		/* Setup DIF Error Injections */
3936 		snprintf(name, sizeof(name), "InjErrLBA");
3937 		phba->debug_InjErrLBA =
3938 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3939 			phba->hba_debugfs_root,
3940 			phba, &lpfc_debugfs_op_dif_err);
3941 		if (!phba->debug_InjErrLBA) {
3942 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3943 				"0807 Cannot create debugfs InjErrLBA\n");
3944 			goto debug_failed;
3945 		}
3946 		phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
3947 
3948 		snprintf(name, sizeof(name), "writeGuardInjErr");
3949 		phba->debug_writeGuard =
3950 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3951 			phba->hba_debugfs_root,
3952 			phba, &lpfc_debugfs_op_dif_err);
3953 		if (!phba->debug_writeGuard) {
3954 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3955 				"0802 Cannot create debugfs writeGuard\n");
3956 			goto debug_failed;
3957 		}
3958 
3959 		snprintf(name, sizeof(name), "writeAppInjErr");
3960 		phba->debug_writeApp =
3961 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3962 			phba->hba_debugfs_root,
3963 			phba, &lpfc_debugfs_op_dif_err);
3964 		if (!phba->debug_writeApp) {
3965 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3966 				"0803 Cannot create debugfs writeApp\n");
3967 			goto debug_failed;
3968 		}
3969 
3970 		snprintf(name, sizeof(name), "writeRefInjErr");
3971 		phba->debug_writeRef =
3972 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3973 			phba->hba_debugfs_root,
3974 			phba, &lpfc_debugfs_op_dif_err);
3975 		if (!phba->debug_writeRef) {
3976 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3977 				"0804 Cannot create debugfs writeRef\n");
3978 			goto debug_failed;
3979 		}
3980 
3981 		snprintf(name, sizeof(name), "readAppInjErr");
3982 		phba->debug_readApp =
3983 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3984 			phba->hba_debugfs_root,
3985 			phba, &lpfc_debugfs_op_dif_err);
3986 		if (!phba->debug_readApp) {
3987 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3988 				"0805 Cannot create debugfs readApp\n");
3989 			goto debug_failed;
3990 		}
3991 
3992 		snprintf(name, sizeof(name), "readRefInjErr");
3993 		phba->debug_readRef =
3994 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3995 			phba->hba_debugfs_root,
3996 			phba, &lpfc_debugfs_op_dif_err);
3997 		if (!phba->debug_readRef) {
3998 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3999 				"0806 Cannot create debugfs readApp\n");
4000 			goto debug_failed;
4001 		}
4002 
4003 		/* Setup slow ring trace */
4004 		if (lpfc_debugfs_max_slow_ring_trc) {
4005 			num = lpfc_debugfs_max_slow_ring_trc - 1;
4006 			if (num & lpfc_debugfs_max_slow_ring_trc) {
4007 				/* Change to be a power of 2 */
4008 				num = lpfc_debugfs_max_slow_ring_trc;
4009 				i = 0;
4010 				while (num > 1) {
4011 					num = num >> 1;
4012 					i++;
4013 				}
4014 				lpfc_debugfs_max_slow_ring_trc = (1 << i);
4015 				printk(KERN_ERR
4016 				       "lpfc_debugfs_max_disc_trc changed to "
4017 				       "%d\n", lpfc_debugfs_max_disc_trc);
4018 			}
4019 		}
4020 
4021 		snprintf(name, sizeof(name), "slow_ring_trace");
4022 		phba->debug_slow_ring_trc =
4023 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4024 				 phba->hba_debugfs_root,
4025 				 phba, &lpfc_debugfs_op_slow_ring_trc);
4026 		if (!phba->debug_slow_ring_trc) {
4027 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4028 					 "0415 Cannot create debugfs "
4029 					 "slow_ring_trace\n");
4030 			goto debug_failed;
4031 		}
4032 		if (!phba->slow_ring_trc) {
4033 			phba->slow_ring_trc = kmalloc(
4034 				(sizeof(struct lpfc_debugfs_trc) *
4035 				lpfc_debugfs_max_slow_ring_trc),
4036 				GFP_KERNEL);
4037 			if (!phba->slow_ring_trc) {
4038 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4039 						 "0416 Cannot create debugfs "
4040 						 "slow_ring buffer\n");
4041 				goto debug_failed;
4042 			}
4043 			atomic_set(&phba->slow_ring_trc_cnt, 0);
4044 			memset(phba->slow_ring_trc, 0,
4045 				(sizeof(struct lpfc_debugfs_trc) *
4046 				lpfc_debugfs_max_slow_ring_trc));
4047 		}
4048 	}
4049 
4050 	snprintf(name, sizeof(name), "vport%d", vport->vpi);
4051 	if (!vport->vport_debugfs_root) {
4052 		vport->vport_debugfs_root =
4053 			debugfs_create_dir(name, phba->hba_debugfs_root);
4054 		if (!vport->vport_debugfs_root) {
4055 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4056 					 "0417 Can't create debugfs\n");
4057 			goto debug_failed;
4058 		}
4059 		atomic_inc(&phba->debugfs_vport_count);
4060 	}
4061 
4062 	if (lpfc_debugfs_max_disc_trc) {
4063 		num = lpfc_debugfs_max_disc_trc - 1;
4064 		if (num & lpfc_debugfs_max_disc_trc) {
4065 			/* Change to be a power of 2 */
4066 			num = lpfc_debugfs_max_disc_trc;
4067 			i = 0;
4068 			while (num > 1) {
4069 				num = num >> 1;
4070 				i++;
4071 			}
4072 			lpfc_debugfs_max_disc_trc = (1 << i);
4073 			printk(KERN_ERR
4074 			       "lpfc_debugfs_max_disc_trc changed to %d\n",
4075 			       lpfc_debugfs_max_disc_trc);
4076 		}
4077 	}
4078 
4079 	vport->disc_trc = kzalloc(
4080 		(sizeof(struct lpfc_debugfs_trc) * lpfc_debugfs_max_disc_trc),
4081 		GFP_KERNEL);
4082 
4083 	if (!vport->disc_trc) {
4084 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4085 				 "0418 Cannot create debugfs disc trace "
4086 				 "buffer\n");
4087 		goto debug_failed;
4088 	}
4089 	atomic_set(&vport->disc_trc_cnt, 0);
4090 
4091 	snprintf(name, sizeof(name), "discovery_trace");
4092 	vport->debug_disc_trc =
4093 		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4094 				 vport->vport_debugfs_root,
4095 				 vport, &lpfc_debugfs_op_disc_trc);
4096 	if (!vport->debug_disc_trc) {
4097 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4098 				 "0419 Cannot create debugfs "
4099 				 "discovery_trace\n");
4100 		goto debug_failed;
4101 	}
4102 	snprintf(name, sizeof(name), "nodelist");
4103 	vport->debug_nodelist =
4104 		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4105 				 vport->vport_debugfs_root,
4106 				 vport, &lpfc_debugfs_op_nodelist);
4107 	if (!vport->debug_nodelist) {
4108 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4109 				 "2985 Can't create debugfs nodelist\n");
4110 		goto debug_failed;
4111 	}
4112 
4113 	/*
4114 	 * iDiag debugfs root entry points for SLI4 device only
4115 	 */
4116 	if (phba->sli_rev < LPFC_SLI_REV4)
4117 		goto debug_failed;
4118 
4119 	snprintf(name, sizeof(name), "iDiag");
4120 	if (!phba->idiag_root) {
4121 		phba->idiag_root =
4122 			debugfs_create_dir(name, phba->hba_debugfs_root);
4123 		if (!phba->idiag_root) {
4124 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4125 					 "2922 Can't create idiag debugfs\n");
4126 			goto debug_failed;
4127 		}
4128 		/* Initialize iDiag data structure */
4129 		memset(&idiag, 0, sizeof(idiag));
4130 	}
4131 
4132 	/* iDiag read PCI config space */
4133 	snprintf(name, sizeof(name), "pciCfg");
4134 	if (!phba->idiag_pci_cfg) {
4135 		phba->idiag_pci_cfg =
4136 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4137 				phba->idiag_root, phba, &lpfc_idiag_op_pciCfg);
4138 		if (!phba->idiag_pci_cfg) {
4139 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4140 					 "2923 Can't create idiag debugfs\n");
4141 			goto debug_failed;
4142 		}
4143 		idiag.offset.last_rd = 0;
4144 	}
4145 
4146 	/* iDiag PCI BAR access */
4147 	snprintf(name, sizeof(name), "barAcc");
4148 	if (!phba->idiag_bar_acc) {
4149 		phba->idiag_bar_acc =
4150 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4151 				phba->idiag_root, phba, &lpfc_idiag_op_barAcc);
4152 		if (!phba->idiag_bar_acc) {
4153 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4154 					"3056 Can't create idiag debugfs\n");
4155 			goto debug_failed;
4156 		}
4157 		idiag.offset.last_rd = 0;
4158 	}
4159 
4160 	/* iDiag get PCI function queue information */
4161 	snprintf(name, sizeof(name), "queInfo");
4162 	if (!phba->idiag_que_info) {
4163 		phba->idiag_que_info =
4164 			debugfs_create_file(name, S_IFREG|S_IRUGO,
4165 			phba->idiag_root, phba, &lpfc_idiag_op_queInfo);
4166 		if (!phba->idiag_que_info) {
4167 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4168 					 "2924 Can't create idiag debugfs\n");
4169 			goto debug_failed;
4170 		}
4171 	}
4172 
4173 	/* iDiag access PCI function queue */
4174 	snprintf(name, sizeof(name), "queAcc");
4175 	if (!phba->idiag_que_acc) {
4176 		phba->idiag_que_acc =
4177 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4178 				phba->idiag_root, phba, &lpfc_idiag_op_queAcc);
4179 		if (!phba->idiag_que_acc) {
4180 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4181 					 "2926 Can't create idiag debugfs\n");
4182 			goto debug_failed;
4183 		}
4184 	}
4185 
4186 	/* iDiag access PCI function doorbell registers */
4187 	snprintf(name, sizeof(name), "drbAcc");
4188 	if (!phba->idiag_drb_acc) {
4189 		phba->idiag_drb_acc =
4190 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4191 				phba->idiag_root, phba, &lpfc_idiag_op_drbAcc);
4192 		if (!phba->idiag_drb_acc) {
4193 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4194 					 "2927 Can't create idiag debugfs\n");
4195 			goto debug_failed;
4196 		}
4197 	}
4198 
4199 	/* iDiag access PCI function control registers */
4200 	snprintf(name, sizeof(name), "ctlAcc");
4201 	if (!phba->idiag_ctl_acc) {
4202 		phba->idiag_ctl_acc =
4203 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4204 				phba->idiag_root, phba, &lpfc_idiag_op_ctlAcc);
4205 		if (!phba->idiag_ctl_acc) {
4206 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4207 					 "2981 Can't create idiag debugfs\n");
4208 			goto debug_failed;
4209 		}
4210 	}
4211 
4212 	/* iDiag access mbox commands */
4213 	snprintf(name, sizeof(name), "mbxAcc");
4214 	if (!phba->idiag_mbx_acc) {
4215 		phba->idiag_mbx_acc =
4216 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4217 				phba->idiag_root, phba, &lpfc_idiag_op_mbxAcc);
4218 		if (!phba->idiag_mbx_acc) {
4219 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4220 					"2980 Can't create idiag debugfs\n");
4221 			goto debug_failed;
4222 		}
4223 	}
4224 
4225 	/* iDiag extents access commands */
4226 	if (phba->sli4_hba.extents_in_use) {
4227 		snprintf(name, sizeof(name), "extAcc");
4228 		if (!phba->idiag_ext_acc) {
4229 			phba->idiag_ext_acc =
4230 				debugfs_create_file(name,
4231 						    S_IFREG|S_IRUGO|S_IWUSR,
4232 						    phba->idiag_root, phba,
4233 						    &lpfc_idiag_op_extAcc);
4234 			if (!phba->idiag_ext_acc) {
4235 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4236 						"2986 Cant create "
4237 						"idiag debugfs\n");
4238 				goto debug_failed;
4239 			}
4240 		}
4241 	}
4242 
4243 debug_failed:
4244 	return;
4245 #endif
4246 }
4247 
4248 /**
4249  * lpfc_debugfs_terminate -  Tear down debugfs infrastructure for this vport
4250  * @vport: The vport pointer to remove from debugfs.
4251  *
4252  * Description:
4253  * When Debugfs is configured this routine removes debugfs file system elements
4254  * that are specific to this vport. It also checks to see if there are any
4255  * users left for the debugfs directories associated with the HBA and driver. If
4256  * this is the last user of the HBA directory or driver directory then it will
4257  * remove those from the debugfs infrastructure as well.
4258  **/
4259 inline void
4260 lpfc_debugfs_terminate(struct lpfc_vport *vport)
4261 {
4262 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4263 	struct lpfc_hba   *phba = vport->phba;
4264 
4265 	if (vport->disc_trc) {
4266 		kfree(vport->disc_trc);
4267 		vport->disc_trc = NULL;
4268 	}
4269 	if (vport->debug_disc_trc) {
4270 		debugfs_remove(vport->debug_disc_trc); /* discovery_trace */
4271 		vport->debug_disc_trc = NULL;
4272 	}
4273 	if (vport->debug_nodelist) {
4274 		debugfs_remove(vport->debug_nodelist); /* nodelist */
4275 		vport->debug_nodelist = NULL;
4276 	}
4277 	if (vport->vport_debugfs_root) {
4278 		debugfs_remove(vport->vport_debugfs_root); /* vportX */
4279 		vport->vport_debugfs_root = NULL;
4280 		atomic_dec(&phba->debugfs_vport_count);
4281 	}
4282 	if (atomic_read(&phba->debugfs_vport_count) == 0) {
4283 
4284 		if (phba->debug_hbqinfo) {
4285 			debugfs_remove(phba->debug_hbqinfo); /* hbqinfo */
4286 			phba->debug_hbqinfo = NULL;
4287 		}
4288 		if (phba->debug_dumpHBASlim) {
4289 			debugfs_remove(phba->debug_dumpHBASlim); /* HBASlim */
4290 			phba->debug_dumpHBASlim = NULL;
4291 		}
4292 		if (phba->debug_dumpHostSlim) {
4293 			debugfs_remove(phba->debug_dumpHostSlim); /* HostSlim */
4294 			phba->debug_dumpHostSlim = NULL;
4295 		}
4296 		if (phba->debug_dumpData) {
4297 			debugfs_remove(phba->debug_dumpData); /* dumpData */
4298 			phba->debug_dumpData = NULL;
4299 		}
4300 
4301 		if (phba->debug_dumpDif) {
4302 			debugfs_remove(phba->debug_dumpDif); /* dumpDif */
4303 			phba->debug_dumpDif = NULL;
4304 		}
4305 		if (phba->debug_InjErrLBA) {
4306 			debugfs_remove(phba->debug_InjErrLBA); /* InjErrLBA */
4307 			phba->debug_InjErrLBA = NULL;
4308 		}
4309 		if (phba->debug_writeGuard) {
4310 			debugfs_remove(phba->debug_writeGuard); /* writeGuard */
4311 			phba->debug_writeGuard = NULL;
4312 		}
4313 		if (phba->debug_writeApp) {
4314 			debugfs_remove(phba->debug_writeApp); /* writeApp */
4315 			phba->debug_writeApp = NULL;
4316 		}
4317 		if (phba->debug_writeRef) {
4318 			debugfs_remove(phba->debug_writeRef); /* writeRef */
4319 			phba->debug_writeRef = NULL;
4320 		}
4321 		if (phba->debug_readApp) {
4322 			debugfs_remove(phba->debug_readApp); /* readApp */
4323 			phba->debug_readApp = NULL;
4324 		}
4325 		if (phba->debug_readRef) {
4326 			debugfs_remove(phba->debug_readRef); /* readRef */
4327 			phba->debug_readRef = NULL;
4328 		}
4329 
4330 		if (phba->slow_ring_trc) {
4331 			kfree(phba->slow_ring_trc);
4332 			phba->slow_ring_trc = NULL;
4333 		}
4334 		if (phba->debug_slow_ring_trc) {
4335 			/* slow_ring_trace */
4336 			debugfs_remove(phba->debug_slow_ring_trc);
4337 			phba->debug_slow_ring_trc = NULL;
4338 		}
4339 
4340 		/*
4341 		 * iDiag release
4342 		 */
4343 		if (phba->sli_rev == LPFC_SLI_REV4) {
4344 			if (phba->idiag_ext_acc) {
4345 				/* iDiag extAcc */
4346 				debugfs_remove(phba->idiag_ext_acc);
4347 				phba->idiag_ext_acc = NULL;
4348 			}
4349 			if (phba->idiag_mbx_acc) {
4350 				/* iDiag mbxAcc */
4351 				debugfs_remove(phba->idiag_mbx_acc);
4352 				phba->idiag_mbx_acc = NULL;
4353 			}
4354 			if (phba->idiag_ctl_acc) {
4355 				/* iDiag ctlAcc */
4356 				debugfs_remove(phba->idiag_ctl_acc);
4357 				phba->idiag_ctl_acc = NULL;
4358 			}
4359 			if (phba->idiag_drb_acc) {
4360 				/* iDiag drbAcc */
4361 				debugfs_remove(phba->idiag_drb_acc);
4362 				phba->idiag_drb_acc = NULL;
4363 			}
4364 			if (phba->idiag_que_acc) {
4365 				/* iDiag queAcc */
4366 				debugfs_remove(phba->idiag_que_acc);
4367 				phba->idiag_que_acc = NULL;
4368 			}
4369 			if (phba->idiag_que_info) {
4370 				/* iDiag queInfo */
4371 				debugfs_remove(phba->idiag_que_info);
4372 				phba->idiag_que_info = NULL;
4373 			}
4374 			if (phba->idiag_bar_acc) {
4375 				/* iDiag barAcc */
4376 				debugfs_remove(phba->idiag_bar_acc);
4377 				phba->idiag_bar_acc = NULL;
4378 			}
4379 			if (phba->idiag_pci_cfg) {
4380 				/* iDiag pciCfg */
4381 				debugfs_remove(phba->idiag_pci_cfg);
4382 				phba->idiag_pci_cfg = NULL;
4383 			}
4384 
4385 			/* Finally remove the iDiag debugfs root */
4386 			if (phba->idiag_root) {
4387 				/* iDiag root */
4388 				debugfs_remove(phba->idiag_root);
4389 				phba->idiag_root = NULL;
4390 			}
4391 		}
4392 
4393 		if (phba->hba_debugfs_root) {
4394 			debugfs_remove(phba->hba_debugfs_root); /* fnX */
4395 			phba->hba_debugfs_root = NULL;
4396 			atomic_dec(&lpfc_debugfs_hba_count);
4397 		}
4398 
4399 		if (atomic_read(&lpfc_debugfs_hba_count) == 0) {
4400 			debugfs_remove(lpfc_debugfs_root); /* lpfc */
4401 			lpfc_debugfs_root = NULL;
4402 		}
4403 	}
4404 #endif
4405 	return;
4406 }
4407