xref: /linux/drivers/scsi/lpfc/lpfc_debugfs.c (revision 9ffc93f203c18a70623f21950f1dd473c9ec48cd)
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_readGuard)
1023 		cnt = snprintf(cbuf, 16, "%u\n", phba->lpfc_injerr_rgrd_cnt);
1024 	else if (dent == phba->debug_readApp)
1025 		cnt = snprintf(cbuf, 16, "%u\n", phba->lpfc_injerr_rapp_cnt);
1026 	else if (dent == phba->debug_readRef)
1027 		cnt = snprintf(cbuf, 16, "%u\n", phba->lpfc_injerr_rref_cnt);
1028 	else if (dent == phba->debug_InjErrLBA)
1029 		cnt = snprintf(cbuf, 16, "0x%lx\n",
1030 				 (unsigned long) phba->lpfc_injerr_lba);
1031 	else
1032 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1033 			 "0547 Unknown debugfs error injection entry\n");
1034 
1035 	return simple_read_from_buffer(buf, nbytes, ppos, &cbuf, cnt);
1036 }
1037 
1038 static ssize_t
1039 lpfc_debugfs_dif_err_write(struct file *file, const char __user *buf,
1040 	size_t nbytes, loff_t *ppos)
1041 {
1042 	struct dentry *dent = file->f_dentry;
1043 	struct lpfc_hba *phba = file->private_data;
1044 	char dstbuf[32];
1045 	unsigned long tmp;
1046 	int size;
1047 
1048 	memset(dstbuf, 0, 32);
1049 	size = (nbytes < 32) ? nbytes : 32;
1050 	if (copy_from_user(dstbuf, buf, size))
1051 		return 0;
1052 
1053 	if (strict_strtoul(dstbuf, 0, &tmp))
1054 		return 0;
1055 
1056 	if (dent == phba->debug_writeGuard)
1057 		phba->lpfc_injerr_wgrd_cnt = (uint32_t)tmp;
1058 	else if (dent == phba->debug_writeApp)
1059 		phba->lpfc_injerr_wapp_cnt = (uint32_t)tmp;
1060 	else if (dent == phba->debug_writeRef)
1061 		phba->lpfc_injerr_wref_cnt = (uint32_t)tmp;
1062 	else if (dent == phba->debug_readGuard)
1063 		phba->lpfc_injerr_rgrd_cnt = (uint32_t)tmp;
1064 	else if (dent == phba->debug_readApp)
1065 		phba->lpfc_injerr_rapp_cnt = (uint32_t)tmp;
1066 	else if (dent == phba->debug_readRef)
1067 		phba->lpfc_injerr_rref_cnt = (uint32_t)tmp;
1068 	else if (dent == phba->debug_InjErrLBA)
1069 		phba->lpfc_injerr_lba = (sector_t)tmp;
1070 	else
1071 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1072 			 "0548 Unknown debugfs error injection entry\n");
1073 
1074 	return nbytes;
1075 }
1076 
1077 static int
1078 lpfc_debugfs_dif_err_release(struct inode *inode, struct file *file)
1079 {
1080 	return 0;
1081 }
1082 
1083 /**
1084  * lpfc_debugfs_nodelist_open - Open the nodelist debugfs file
1085  * @inode: The inode pointer that contains a vport pointer.
1086  * @file: The file pointer to attach the log output.
1087  *
1088  * Description:
1089  * This routine is the entry point for the debugfs open file operation. It gets
1090  * the vport from the i_private field in @inode, allocates the necessary buffer
1091  * for the log, fills the buffer from the in-memory log for this vport, and then
1092  * returns a pointer to that log in the private_data field in @file.
1093  *
1094  * Returns:
1095  * This function returns zero if successful. On error it will return an negative
1096  * error value.
1097  **/
1098 static int
1099 lpfc_debugfs_nodelist_open(struct inode *inode, struct file *file)
1100 {
1101 	struct lpfc_vport *vport = inode->i_private;
1102 	struct lpfc_debug *debug;
1103 	int rc = -ENOMEM;
1104 
1105 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1106 	if (!debug)
1107 		goto out;
1108 
1109 	/* Round to page boundary */
1110 	debug->buffer = kmalloc(LPFC_NODELIST_SIZE, GFP_KERNEL);
1111 	if (!debug->buffer) {
1112 		kfree(debug);
1113 		goto out;
1114 	}
1115 
1116 	debug->len = lpfc_debugfs_nodelist_data(vport, debug->buffer,
1117 		LPFC_NODELIST_SIZE);
1118 	file->private_data = debug;
1119 
1120 	rc = 0;
1121 out:
1122 	return rc;
1123 }
1124 
1125 /**
1126  * lpfc_debugfs_lseek - Seek through a debugfs file
1127  * @file: The file pointer to seek through.
1128  * @off: The offset to seek to or the amount to seek by.
1129  * @whence: Indicates how to seek.
1130  *
1131  * Description:
1132  * This routine is the entry point for the debugfs lseek file operation. The
1133  * @whence parameter indicates whether @off is the offset to directly seek to,
1134  * or if it is a value to seek forward or reverse by. This function figures out
1135  * what the new offset of the debugfs file will be and assigns that value to the
1136  * f_pos field of @file.
1137  *
1138  * Returns:
1139  * This function returns the new offset if successful and returns a negative
1140  * error if unable to process the seek.
1141  **/
1142 static loff_t
1143 lpfc_debugfs_lseek(struct file *file, loff_t off, int whence)
1144 {
1145 	struct lpfc_debug *debug;
1146 	loff_t pos = -1;
1147 
1148 	debug = file->private_data;
1149 
1150 	switch (whence) {
1151 	case 0:
1152 		pos = off;
1153 		break;
1154 	case 1:
1155 		pos = file->f_pos + off;
1156 		break;
1157 	case 2:
1158 		pos = debug->len - off;
1159 	}
1160 	return (pos < 0 || pos > debug->len) ? -EINVAL : (file->f_pos = pos);
1161 }
1162 
1163 /**
1164  * lpfc_debugfs_read - Read a debugfs file
1165  * @file: The file pointer to read from.
1166  * @buf: The buffer to copy the data to.
1167  * @nbytes: The number of bytes to read.
1168  * @ppos: The position in the file to start reading from.
1169  *
1170  * Description:
1171  * This routine reads data from from the buffer indicated in the private_data
1172  * field of @file. It will start reading at @ppos and copy up to @nbytes of
1173  * data to @buf.
1174  *
1175  * Returns:
1176  * This function returns the amount of data that was read (this could be less
1177  * than @nbytes if the end of the file was reached) or a negative error value.
1178  **/
1179 static ssize_t
1180 lpfc_debugfs_read(struct file *file, char __user *buf,
1181 		  size_t nbytes, loff_t *ppos)
1182 {
1183 	struct lpfc_debug *debug = file->private_data;
1184 
1185 	return simple_read_from_buffer(buf, nbytes, ppos, debug->buffer,
1186 				       debug->len);
1187 }
1188 
1189 /**
1190  * lpfc_debugfs_release - Release the buffer used to store debugfs file data
1191  * @inode: The inode pointer that contains a vport pointer. (unused)
1192  * @file: The file pointer that contains the buffer to release.
1193  *
1194  * Description:
1195  * This routine frees the buffer that was allocated when the debugfs file was
1196  * opened.
1197  *
1198  * Returns:
1199  * This function returns zero.
1200  **/
1201 static int
1202 lpfc_debugfs_release(struct inode *inode, struct file *file)
1203 {
1204 	struct lpfc_debug *debug = file->private_data;
1205 
1206 	kfree(debug->buffer);
1207 	kfree(debug);
1208 
1209 	return 0;
1210 }
1211 
1212 static int
1213 lpfc_debugfs_dumpDataDif_release(struct inode *inode, struct file *file)
1214 {
1215 	struct lpfc_debug *debug = file->private_data;
1216 
1217 	debug->buffer = NULL;
1218 	kfree(debug);
1219 
1220 	return 0;
1221 }
1222 
1223 /*
1224  * ---------------------------------
1225  * iDiag debugfs file access methods
1226  * ---------------------------------
1227  *
1228  * All access methods are through the proper SLI4 PCI function's debugfs
1229  * iDiag directory:
1230  *
1231  *     /sys/kernel/debug/lpfc/fn<#>/iDiag
1232  */
1233 
1234 /**
1235  * lpfc_idiag_cmd_get - Get and parse idiag debugfs comands from user space
1236  * @buf: The pointer to the user space buffer.
1237  * @nbytes: The number of bytes in the user space buffer.
1238  * @idiag_cmd: pointer to the idiag command struct.
1239  *
1240  * This routine reads data from debugfs user space buffer and parses the
1241  * buffer for getting the idiag command and arguments. The while space in
1242  * between the set of data is used as the parsing separator.
1243  *
1244  * This routine returns 0 when successful, it returns proper error code
1245  * back to the user space in error conditions.
1246  */
1247 static int lpfc_idiag_cmd_get(const char __user *buf, size_t nbytes,
1248 			      struct lpfc_idiag_cmd *idiag_cmd)
1249 {
1250 	char mybuf[64];
1251 	char *pbuf, *step_str;
1252 	int i;
1253 	size_t bsize;
1254 
1255 	/* Protect copy from user */
1256 	if (!access_ok(VERIFY_READ, buf, nbytes))
1257 		return -EFAULT;
1258 
1259 	memset(mybuf, 0, sizeof(mybuf));
1260 	memset(idiag_cmd, 0, sizeof(*idiag_cmd));
1261 	bsize = min(nbytes, (sizeof(mybuf)-1));
1262 
1263 	if (copy_from_user(mybuf, buf, bsize))
1264 		return -EFAULT;
1265 	pbuf = &mybuf[0];
1266 	step_str = strsep(&pbuf, "\t ");
1267 
1268 	/* The opcode must present */
1269 	if (!step_str)
1270 		return -EINVAL;
1271 
1272 	idiag_cmd->opcode = simple_strtol(step_str, NULL, 0);
1273 	if (idiag_cmd->opcode == 0)
1274 		return -EINVAL;
1275 
1276 	for (i = 0; i < LPFC_IDIAG_CMD_DATA_SIZE; i++) {
1277 		step_str = strsep(&pbuf, "\t ");
1278 		if (!step_str)
1279 			return i;
1280 		idiag_cmd->data[i] = simple_strtol(step_str, NULL, 0);
1281 	}
1282 	return i;
1283 }
1284 
1285 /**
1286  * lpfc_idiag_open - idiag open debugfs
1287  * @inode: The inode pointer that contains a pointer to phba.
1288  * @file: The file pointer to attach the file operation.
1289  *
1290  * Description:
1291  * This routine is the entry point for the debugfs open file operation. It
1292  * gets the reference to phba from the i_private field in @inode, it then
1293  * allocates buffer for the file operation, performs the necessary PCI config
1294  * space read into the allocated buffer according to the idiag user command
1295  * setup, and then returns a pointer to buffer in the private_data field in
1296  * @file.
1297  *
1298  * Returns:
1299  * This function returns zero if successful. On error it will return an
1300  * negative error value.
1301  **/
1302 static int
1303 lpfc_idiag_open(struct inode *inode, struct file *file)
1304 {
1305 	struct lpfc_debug *debug;
1306 
1307 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1308 	if (!debug)
1309 		return -ENOMEM;
1310 
1311 	debug->i_private = inode->i_private;
1312 	debug->buffer = NULL;
1313 	file->private_data = debug;
1314 
1315 	return 0;
1316 }
1317 
1318 /**
1319  * lpfc_idiag_release - Release idiag access file operation
1320  * @inode: The inode pointer that contains a vport pointer. (unused)
1321  * @file: The file pointer that contains the buffer to release.
1322  *
1323  * Description:
1324  * This routine is the generic release routine for the idiag access file
1325  * operation, it frees the buffer that was allocated when the debugfs file
1326  * was opened.
1327  *
1328  * Returns:
1329  * This function returns zero.
1330  **/
1331 static int
1332 lpfc_idiag_release(struct inode *inode, struct file *file)
1333 {
1334 	struct lpfc_debug *debug = file->private_data;
1335 
1336 	/* Free the buffers to the file operation */
1337 	kfree(debug->buffer);
1338 	kfree(debug);
1339 
1340 	return 0;
1341 }
1342 
1343 /**
1344  * lpfc_idiag_cmd_release - Release idiag cmd access file operation
1345  * @inode: The inode pointer that contains a vport pointer. (unused)
1346  * @file: The file pointer that contains the buffer to release.
1347  *
1348  * Description:
1349  * This routine frees the buffer that was allocated when the debugfs file
1350  * was opened. It also reset the fields in the idiag command struct in the
1351  * case of command for write operation.
1352  *
1353  * Returns:
1354  * This function returns zero.
1355  **/
1356 static int
1357 lpfc_idiag_cmd_release(struct inode *inode, struct file *file)
1358 {
1359 	struct lpfc_debug *debug = file->private_data;
1360 
1361 	if (debug->op == LPFC_IDIAG_OP_WR) {
1362 		switch (idiag.cmd.opcode) {
1363 		case LPFC_IDIAG_CMD_PCICFG_WR:
1364 		case LPFC_IDIAG_CMD_PCICFG_ST:
1365 		case LPFC_IDIAG_CMD_PCICFG_CL:
1366 		case LPFC_IDIAG_CMD_QUEACC_WR:
1367 		case LPFC_IDIAG_CMD_QUEACC_ST:
1368 		case LPFC_IDIAG_CMD_QUEACC_CL:
1369 			memset(&idiag, 0, sizeof(idiag));
1370 			break;
1371 		default:
1372 			break;
1373 		}
1374 	}
1375 
1376 	/* Free the buffers to the file operation */
1377 	kfree(debug->buffer);
1378 	kfree(debug);
1379 
1380 	return 0;
1381 }
1382 
1383 /**
1384  * lpfc_idiag_pcicfg_read - idiag debugfs read pcicfg
1385  * @file: The file pointer to read from.
1386  * @buf: The buffer to copy the data to.
1387  * @nbytes: The number of bytes to read.
1388  * @ppos: The position in the file to start reading from.
1389  *
1390  * Description:
1391  * This routine reads data from the @phba pci config space according to the
1392  * idiag command, and copies to user @buf. Depending on the PCI config space
1393  * read command setup, it does either a single register read of a byte
1394  * (8 bits), a word (16 bits), or a dword (32 bits) or browsing through all
1395  * registers from the 4K extended PCI config space.
1396  *
1397  * Returns:
1398  * This function returns the amount of data that was read (this could be less
1399  * than @nbytes if the end of the file was reached) or a negative error value.
1400  **/
1401 static ssize_t
1402 lpfc_idiag_pcicfg_read(struct file *file, char __user *buf, size_t nbytes,
1403 		       loff_t *ppos)
1404 {
1405 	struct lpfc_debug *debug = file->private_data;
1406 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
1407 	int offset_label, offset, len = 0, index = LPFC_PCI_CFG_RD_SIZE;
1408 	int where, count;
1409 	char *pbuffer;
1410 	struct pci_dev *pdev;
1411 	uint32_t u32val;
1412 	uint16_t u16val;
1413 	uint8_t u8val;
1414 
1415 	pdev = phba->pcidev;
1416 	if (!pdev)
1417 		return 0;
1418 
1419 	/* This is a user read operation */
1420 	debug->op = LPFC_IDIAG_OP_RD;
1421 
1422 	if (!debug->buffer)
1423 		debug->buffer = kmalloc(LPFC_PCI_CFG_SIZE, GFP_KERNEL);
1424 	if (!debug->buffer)
1425 		return 0;
1426 	pbuffer = debug->buffer;
1427 
1428 	if (*ppos)
1429 		return 0;
1430 
1431 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
1432 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
1433 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
1434 	} else
1435 		return 0;
1436 
1437 	/* Read single PCI config space register */
1438 	switch (count) {
1439 	case SIZE_U8: /* byte (8 bits) */
1440 		pci_read_config_byte(pdev, where, &u8val);
1441 		len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
1442 				"%03x: %02x\n", where, u8val);
1443 		break;
1444 	case SIZE_U16: /* word (16 bits) */
1445 		pci_read_config_word(pdev, where, &u16val);
1446 		len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
1447 				"%03x: %04x\n", where, u16val);
1448 		break;
1449 	case SIZE_U32: /* double word (32 bits) */
1450 		pci_read_config_dword(pdev, where, &u32val);
1451 		len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
1452 				"%03x: %08x\n", where, u32val);
1453 		break;
1454 	case LPFC_PCI_CFG_BROWSE: /* browse all */
1455 		goto pcicfg_browse;
1456 		break;
1457 	default:
1458 		/* illegal count */
1459 		len = 0;
1460 		break;
1461 	}
1462 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
1463 
1464 pcicfg_browse:
1465 
1466 	/* Browse all PCI config space registers */
1467 	offset_label = idiag.offset.last_rd;
1468 	offset = offset_label;
1469 
1470 	/* Read PCI config space */
1471 	len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
1472 			"%03x: ", offset_label);
1473 	while (index > 0) {
1474 		pci_read_config_dword(pdev, offset, &u32val);
1475 		len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
1476 				"%08x ", u32val);
1477 		offset += sizeof(uint32_t);
1478 		if (offset >= LPFC_PCI_CFG_SIZE) {
1479 			len += snprintf(pbuffer+len,
1480 					LPFC_PCI_CFG_SIZE-len, "\n");
1481 			break;
1482 		}
1483 		index -= sizeof(uint32_t);
1484 		if (!index)
1485 			len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
1486 					"\n");
1487 		else if (!(index % (8 * sizeof(uint32_t)))) {
1488 			offset_label += (8 * sizeof(uint32_t));
1489 			len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
1490 					"\n%03x: ", offset_label);
1491 		}
1492 	}
1493 
1494 	/* Set up the offset for next portion of pci cfg read */
1495 	if (index == 0) {
1496 		idiag.offset.last_rd += LPFC_PCI_CFG_RD_SIZE;
1497 		if (idiag.offset.last_rd >= LPFC_PCI_CFG_SIZE)
1498 			idiag.offset.last_rd = 0;
1499 	} else
1500 		idiag.offset.last_rd = 0;
1501 
1502 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
1503 }
1504 
1505 /**
1506  * lpfc_idiag_pcicfg_write - Syntax check and set up idiag pcicfg commands
1507  * @file: The file pointer to read from.
1508  * @buf: The buffer to copy the user data from.
1509  * @nbytes: The number of bytes to get.
1510  * @ppos: The position in the file to start reading from.
1511  *
1512  * This routine get the debugfs idiag command struct from user space and
1513  * then perform the syntax check for PCI config space read or write command
1514  * accordingly. In the case of PCI config space read command, it sets up
1515  * the command in the idiag command struct for the debugfs read operation.
1516  * In the case of PCI config space write operation, it executes the write
1517  * operation into the PCI config space accordingly.
1518  *
1519  * It returns the @nbytges passing in from debugfs user space when successful.
1520  * In case of error conditions, it returns proper error code back to the user
1521  * space.
1522  */
1523 static ssize_t
1524 lpfc_idiag_pcicfg_write(struct file *file, const char __user *buf,
1525 			size_t nbytes, loff_t *ppos)
1526 {
1527 	struct lpfc_debug *debug = file->private_data;
1528 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
1529 	uint32_t where, value, count;
1530 	uint32_t u32val;
1531 	uint16_t u16val;
1532 	uint8_t u8val;
1533 	struct pci_dev *pdev;
1534 	int rc;
1535 
1536 	pdev = phba->pcidev;
1537 	if (!pdev)
1538 		return -EFAULT;
1539 
1540 	/* This is a user write operation */
1541 	debug->op = LPFC_IDIAG_OP_WR;
1542 
1543 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
1544 	if (rc < 0)
1545 		return rc;
1546 
1547 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
1548 		/* Sanity check on PCI config read command line arguments */
1549 		if (rc != LPFC_PCI_CFG_RD_CMD_ARG)
1550 			goto error_out;
1551 		/* Read command from PCI config space, set up command fields */
1552 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
1553 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
1554 		if (count == LPFC_PCI_CFG_BROWSE) {
1555 			if (where % sizeof(uint32_t))
1556 				goto error_out;
1557 			/* Starting offset to browse */
1558 			idiag.offset.last_rd = where;
1559 		} else if ((count != sizeof(uint8_t)) &&
1560 			   (count != sizeof(uint16_t)) &&
1561 			   (count != sizeof(uint32_t)))
1562 			goto error_out;
1563 		if (count == sizeof(uint8_t)) {
1564 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
1565 				goto error_out;
1566 			if (where % sizeof(uint8_t))
1567 				goto error_out;
1568 		}
1569 		if (count == sizeof(uint16_t)) {
1570 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
1571 				goto error_out;
1572 			if (where % sizeof(uint16_t))
1573 				goto error_out;
1574 		}
1575 		if (count == sizeof(uint32_t)) {
1576 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
1577 				goto error_out;
1578 			if (where % sizeof(uint32_t))
1579 				goto error_out;
1580 		}
1581 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR ||
1582 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST ||
1583 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
1584 		/* Sanity check on PCI config write command line arguments */
1585 		if (rc != LPFC_PCI_CFG_WR_CMD_ARG)
1586 			goto error_out;
1587 		/* Write command to PCI config space, read-modify-write */
1588 		where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
1589 		count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
1590 		value = idiag.cmd.data[IDIAG_PCICFG_VALUE_INDX];
1591 		/* Sanity checks */
1592 		if ((count != sizeof(uint8_t)) &&
1593 		    (count != sizeof(uint16_t)) &&
1594 		    (count != sizeof(uint32_t)))
1595 			goto error_out;
1596 		if (count == sizeof(uint8_t)) {
1597 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
1598 				goto error_out;
1599 			if (where % sizeof(uint8_t))
1600 				goto error_out;
1601 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
1602 				pci_write_config_byte(pdev, where,
1603 						      (uint8_t)value);
1604 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
1605 				rc = pci_read_config_byte(pdev, where, &u8val);
1606 				if (!rc) {
1607 					u8val |= (uint8_t)value;
1608 					pci_write_config_byte(pdev, where,
1609 							      u8val);
1610 				}
1611 			}
1612 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
1613 				rc = pci_read_config_byte(pdev, where, &u8val);
1614 				if (!rc) {
1615 					u8val &= (uint8_t)(~value);
1616 					pci_write_config_byte(pdev, where,
1617 							      u8val);
1618 				}
1619 			}
1620 		}
1621 		if (count == sizeof(uint16_t)) {
1622 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
1623 				goto error_out;
1624 			if (where % sizeof(uint16_t))
1625 				goto error_out;
1626 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
1627 				pci_write_config_word(pdev, where,
1628 						      (uint16_t)value);
1629 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
1630 				rc = pci_read_config_word(pdev, where, &u16val);
1631 				if (!rc) {
1632 					u16val |= (uint16_t)value;
1633 					pci_write_config_word(pdev, where,
1634 							      u16val);
1635 				}
1636 			}
1637 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
1638 				rc = pci_read_config_word(pdev, where, &u16val);
1639 				if (!rc) {
1640 					u16val &= (uint16_t)(~value);
1641 					pci_write_config_word(pdev, where,
1642 							      u16val);
1643 				}
1644 			}
1645 		}
1646 		if (count == sizeof(uint32_t)) {
1647 			if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
1648 				goto error_out;
1649 			if (where % sizeof(uint32_t))
1650 				goto error_out;
1651 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
1652 				pci_write_config_dword(pdev, where, value);
1653 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
1654 				rc = pci_read_config_dword(pdev, where,
1655 							   &u32val);
1656 				if (!rc) {
1657 					u32val |= value;
1658 					pci_write_config_dword(pdev, where,
1659 							       u32val);
1660 				}
1661 			}
1662 			if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
1663 				rc = pci_read_config_dword(pdev, where,
1664 							   &u32val);
1665 				if (!rc) {
1666 					u32val &= ~value;
1667 					pci_write_config_dword(pdev, where,
1668 							       u32val);
1669 				}
1670 			}
1671 		}
1672 	} else
1673 		/* All other opecodes are illegal for now */
1674 		goto error_out;
1675 
1676 	return nbytes;
1677 error_out:
1678 	memset(&idiag, 0, sizeof(idiag));
1679 	return -EINVAL;
1680 }
1681 
1682 /**
1683  * lpfc_idiag_baracc_read - idiag debugfs pci bar access read
1684  * @file: The file pointer to read from.
1685  * @buf: The buffer to copy the data to.
1686  * @nbytes: The number of bytes to read.
1687  * @ppos: The position in the file to start reading from.
1688  *
1689  * Description:
1690  * This routine reads data from the @phba pci bar memory mapped space
1691  * according to the idiag command, and copies to user @buf.
1692  *
1693  * Returns:
1694  * This function returns the amount of data that was read (this could be less
1695  * than @nbytes if the end of the file was reached) or a negative error value.
1696  **/
1697 static ssize_t
1698 lpfc_idiag_baracc_read(struct file *file, char __user *buf, size_t nbytes,
1699 		       loff_t *ppos)
1700 {
1701 	struct lpfc_debug *debug = file->private_data;
1702 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
1703 	int offset_label, offset, offset_run, len = 0, index;
1704 	int bar_num, acc_range, bar_size;
1705 	char *pbuffer;
1706 	void __iomem *mem_mapped_bar;
1707 	uint32_t if_type;
1708 	struct pci_dev *pdev;
1709 	uint32_t u32val;
1710 
1711 	pdev = phba->pcidev;
1712 	if (!pdev)
1713 		return 0;
1714 
1715 	/* This is a user read operation */
1716 	debug->op = LPFC_IDIAG_OP_RD;
1717 
1718 	if (!debug->buffer)
1719 		debug->buffer = kmalloc(LPFC_PCI_BAR_RD_BUF_SIZE, GFP_KERNEL);
1720 	if (!debug->buffer)
1721 		return 0;
1722 	pbuffer = debug->buffer;
1723 
1724 	if (*ppos)
1725 		return 0;
1726 
1727 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
1728 		bar_num   = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
1729 		offset    = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
1730 		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
1731 		bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
1732 	} else
1733 		return 0;
1734 
1735 	if (acc_range == 0)
1736 		return 0;
1737 
1738 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1739 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
1740 		if (bar_num == IDIAG_BARACC_BAR_0)
1741 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
1742 		else if (bar_num == IDIAG_BARACC_BAR_1)
1743 			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
1744 		else if (bar_num == IDIAG_BARACC_BAR_2)
1745 			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
1746 		else
1747 			return 0;
1748 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
1749 		if (bar_num == IDIAG_BARACC_BAR_0)
1750 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
1751 		else
1752 			return 0;
1753 	} else
1754 		return 0;
1755 
1756 	/* Read single PCI bar space register */
1757 	if (acc_range == SINGLE_WORD) {
1758 		offset_run = offset;
1759 		u32val = readl(mem_mapped_bar + offset_run);
1760 		len += snprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
1761 				"%05x: %08x\n", offset_run, u32val);
1762 	} else
1763 		goto baracc_browse;
1764 
1765 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
1766 
1767 baracc_browse:
1768 
1769 	/* Browse all PCI bar space registers */
1770 	offset_label = idiag.offset.last_rd;
1771 	offset_run = offset_label;
1772 
1773 	/* Read PCI bar memory mapped space */
1774 	len += snprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
1775 			"%05x: ", offset_label);
1776 	index = LPFC_PCI_BAR_RD_SIZE;
1777 	while (index > 0) {
1778 		u32val = readl(mem_mapped_bar + offset_run);
1779 		len += snprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
1780 				"%08x ", u32val);
1781 		offset_run += sizeof(uint32_t);
1782 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
1783 			if (offset_run >= bar_size) {
1784 				len += snprintf(pbuffer+len,
1785 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
1786 				break;
1787 			}
1788 		} else {
1789 			if (offset_run >= offset +
1790 			    (acc_range * sizeof(uint32_t))) {
1791 				len += snprintf(pbuffer+len,
1792 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
1793 				break;
1794 			}
1795 		}
1796 		index -= sizeof(uint32_t);
1797 		if (!index)
1798 			len += snprintf(pbuffer+len,
1799 					LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
1800 		else if (!(index % (8 * sizeof(uint32_t)))) {
1801 			offset_label += (8 * sizeof(uint32_t));
1802 			len += snprintf(pbuffer+len,
1803 					LPFC_PCI_BAR_RD_BUF_SIZE-len,
1804 					"\n%05x: ", offset_label);
1805 		}
1806 	}
1807 
1808 	/* Set up the offset for next portion of pci bar read */
1809 	if (index == 0) {
1810 		idiag.offset.last_rd += LPFC_PCI_BAR_RD_SIZE;
1811 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
1812 			if (idiag.offset.last_rd >= bar_size)
1813 				idiag.offset.last_rd = 0;
1814 		} else {
1815 			if (offset_run >= offset +
1816 			    (acc_range * sizeof(uint32_t)))
1817 				idiag.offset.last_rd = offset;
1818 		}
1819 	} else {
1820 		if (acc_range == LPFC_PCI_BAR_BROWSE)
1821 			idiag.offset.last_rd = 0;
1822 		else
1823 			idiag.offset.last_rd = offset;
1824 	}
1825 
1826 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
1827 }
1828 
1829 /**
1830  * lpfc_idiag_baracc_write - Syntax check and set up idiag bar access commands
1831  * @file: The file pointer to read from.
1832  * @buf: The buffer to copy the user data from.
1833  * @nbytes: The number of bytes to get.
1834  * @ppos: The position in the file to start reading from.
1835  *
1836  * This routine get the debugfs idiag command struct from user space and
1837  * then perform the syntax check for PCI bar memory mapped space read or
1838  * write command accordingly. In the case of PCI bar memory mapped space
1839  * read command, it sets up the command in the idiag command struct for
1840  * the debugfs read operation. In the case of PCI bar memorpy mapped space
1841  * write operation, it executes the write operation into the PCI bar memory
1842  * mapped space accordingly.
1843  *
1844  * It returns the @nbytges passing in from debugfs user space when successful.
1845  * In case of error conditions, it returns proper error code back to the user
1846  * space.
1847  */
1848 static ssize_t
1849 lpfc_idiag_baracc_write(struct file *file, const char __user *buf,
1850 			size_t nbytes, loff_t *ppos)
1851 {
1852 	struct lpfc_debug *debug = file->private_data;
1853 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
1854 	uint32_t bar_num, bar_size, offset, value, acc_range;
1855 	struct pci_dev *pdev;
1856 	void __iomem *mem_mapped_bar;
1857 	uint32_t if_type;
1858 	uint32_t u32val;
1859 	int rc;
1860 
1861 	pdev = phba->pcidev;
1862 	if (!pdev)
1863 		return -EFAULT;
1864 
1865 	/* This is a user write operation */
1866 	debug->op = LPFC_IDIAG_OP_WR;
1867 
1868 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
1869 	if (rc < 0)
1870 		return rc;
1871 
1872 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1873 	bar_num = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
1874 
1875 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
1876 		if ((bar_num != IDIAG_BARACC_BAR_0) &&
1877 		    (bar_num != IDIAG_BARACC_BAR_1) &&
1878 		    (bar_num != IDIAG_BARACC_BAR_2))
1879 			goto error_out;
1880 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
1881 		if (bar_num != IDIAG_BARACC_BAR_0)
1882 			goto error_out;
1883 	} else
1884 		goto error_out;
1885 
1886 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
1887 		if (bar_num == IDIAG_BARACC_BAR_0) {
1888 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
1889 				LPFC_PCI_IF0_BAR0_SIZE;
1890 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
1891 		} else if (bar_num == IDIAG_BARACC_BAR_1) {
1892 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
1893 				LPFC_PCI_IF0_BAR1_SIZE;
1894 			mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
1895 		} else if (bar_num == IDIAG_BARACC_BAR_2) {
1896 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
1897 				LPFC_PCI_IF0_BAR2_SIZE;
1898 			mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
1899 		} else
1900 			goto error_out;
1901 	} else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
1902 		if (bar_num == IDIAG_BARACC_BAR_0) {
1903 			idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
1904 				LPFC_PCI_IF2_BAR0_SIZE;
1905 			mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
1906 		} else
1907 			goto error_out;
1908 	} else
1909 		goto error_out;
1910 
1911 	offset = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
1912 	if (offset % sizeof(uint32_t))
1913 		goto error_out;
1914 
1915 	bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
1916 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
1917 		/* Sanity check on PCI config read command line arguments */
1918 		if (rc != LPFC_PCI_BAR_RD_CMD_ARG)
1919 			goto error_out;
1920 		acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
1921 		if (acc_range == LPFC_PCI_BAR_BROWSE) {
1922 			if (offset > bar_size - sizeof(uint32_t))
1923 				goto error_out;
1924 			/* Starting offset to browse */
1925 			idiag.offset.last_rd = offset;
1926 		} else if (acc_range > SINGLE_WORD) {
1927 			if (offset + acc_range * sizeof(uint32_t) > bar_size)
1928 				goto error_out;
1929 			/* Starting offset to browse */
1930 			idiag.offset.last_rd = offset;
1931 		} else if (acc_range != SINGLE_WORD)
1932 			goto error_out;
1933 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR ||
1934 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST ||
1935 		   idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
1936 		/* Sanity check on PCI bar write command line arguments */
1937 		if (rc != LPFC_PCI_BAR_WR_CMD_ARG)
1938 			goto error_out;
1939 		/* Write command to PCI bar space, read-modify-write */
1940 		acc_range = SINGLE_WORD;
1941 		value = idiag.cmd.data[IDIAG_BARACC_REG_VAL_INDX];
1942 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR) {
1943 			writel(value, mem_mapped_bar + offset);
1944 			readl(mem_mapped_bar + offset);
1945 		}
1946 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST) {
1947 			u32val = readl(mem_mapped_bar + offset);
1948 			u32val |= value;
1949 			writel(u32val, mem_mapped_bar + offset);
1950 			readl(mem_mapped_bar + offset);
1951 		}
1952 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
1953 			u32val = readl(mem_mapped_bar + offset);
1954 			u32val &= ~value;
1955 			writel(u32val, mem_mapped_bar + offset);
1956 			readl(mem_mapped_bar + offset);
1957 		}
1958 	} else
1959 		/* All other opecodes are illegal for now */
1960 		goto error_out;
1961 
1962 	return nbytes;
1963 error_out:
1964 	memset(&idiag, 0, sizeof(idiag));
1965 	return -EINVAL;
1966 }
1967 
1968 /**
1969  * lpfc_idiag_queinfo_read - idiag debugfs read queue information
1970  * @file: The file pointer to read from.
1971  * @buf: The buffer to copy the data to.
1972  * @nbytes: The number of bytes to read.
1973  * @ppos: The position in the file to start reading from.
1974  *
1975  * Description:
1976  * This routine reads data from the @phba SLI4 PCI function queue information,
1977  * and copies to user @buf.
1978  *
1979  * Returns:
1980  * This function returns the amount of data that was read (this could be less
1981  * than @nbytes if the end of the file was reached) or a negative error value.
1982  **/
1983 static ssize_t
1984 lpfc_idiag_queinfo_read(struct file *file, char __user *buf, size_t nbytes,
1985 			loff_t *ppos)
1986 {
1987 	struct lpfc_debug *debug = file->private_data;
1988 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
1989 	int len = 0, fcp_qidx;
1990 	char *pbuffer;
1991 
1992 	if (!debug->buffer)
1993 		debug->buffer = kmalloc(LPFC_QUE_INFO_GET_BUF_SIZE, GFP_KERNEL);
1994 	if (!debug->buffer)
1995 		return 0;
1996 	pbuffer = debug->buffer;
1997 
1998 	if (*ppos)
1999 		return 0;
2000 
2001 	/* Get slow-path event queue information */
2002 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2003 			"Slow-path EQ information:\n");
2004 	if (phba->sli4_hba.sp_eq) {
2005 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2006 			"\tEQID[%02d], "
2007 			"QE-COUNT[%04d], QE-SIZE[%04d], "
2008 			"HOST-INDEX[%04d], PORT-INDEX[%04d]\n\n",
2009 			phba->sli4_hba.sp_eq->queue_id,
2010 			phba->sli4_hba.sp_eq->entry_count,
2011 			phba->sli4_hba.sp_eq->entry_size,
2012 			phba->sli4_hba.sp_eq->host_index,
2013 			phba->sli4_hba.sp_eq->hba_index);
2014 	}
2015 
2016 	/* Get fast-path event queue information */
2017 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2018 			"Fast-path EQ information:\n");
2019 	if (phba->sli4_hba.fp_eq) {
2020 		for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count;
2021 		     fcp_qidx++) {
2022 			if (phba->sli4_hba.fp_eq[fcp_qidx]) {
2023 				len += snprintf(pbuffer+len,
2024 					LPFC_QUE_INFO_GET_BUF_SIZE-len,
2025 				"\tEQID[%02d], "
2026 				"QE-COUNT[%04d], QE-SIZE[%04d], "
2027 				"HOST-INDEX[%04d], PORT-INDEX[%04d]\n",
2028 				phba->sli4_hba.fp_eq[fcp_qidx]->queue_id,
2029 				phba->sli4_hba.fp_eq[fcp_qidx]->entry_count,
2030 				phba->sli4_hba.fp_eq[fcp_qidx]->entry_size,
2031 				phba->sli4_hba.fp_eq[fcp_qidx]->host_index,
2032 				phba->sli4_hba.fp_eq[fcp_qidx]->hba_index);
2033 			}
2034 		}
2035 	}
2036 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len, "\n");
2037 
2038 	/* Get mailbox complete queue information */
2039 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2040 			"Slow-path MBX CQ information:\n");
2041 	if (phba->sli4_hba.mbx_cq) {
2042 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2043 			"Associated EQID[%02d]:\n",
2044 			phba->sli4_hba.mbx_cq->assoc_qid);
2045 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2046 			"\tCQID[%02d], "
2047 			"QE-COUNT[%04d], QE-SIZE[%04d], "
2048 			"HOST-INDEX[%04d], PORT-INDEX[%04d]\n\n",
2049 			phba->sli4_hba.mbx_cq->queue_id,
2050 			phba->sli4_hba.mbx_cq->entry_count,
2051 			phba->sli4_hba.mbx_cq->entry_size,
2052 			phba->sli4_hba.mbx_cq->host_index,
2053 			phba->sli4_hba.mbx_cq->hba_index);
2054 	}
2055 
2056 	/* Get slow-path complete queue information */
2057 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2058 			"Slow-path ELS CQ information:\n");
2059 	if (phba->sli4_hba.els_cq) {
2060 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2061 			"Associated EQID[%02d]:\n",
2062 			phba->sli4_hba.els_cq->assoc_qid);
2063 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2064 			"\tCQID [%02d], "
2065 			"QE-COUNT[%04d], QE-SIZE[%04d], "
2066 			"HOST-INDEX[%04d], PORT-INDEX[%04d]\n\n",
2067 			phba->sli4_hba.els_cq->queue_id,
2068 			phba->sli4_hba.els_cq->entry_count,
2069 			phba->sli4_hba.els_cq->entry_size,
2070 			phba->sli4_hba.els_cq->host_index,
2071 			phba->sli4_hba.els_cq->hba_index);
2072 	}
2073 
2074 	/* Get fast-path complete queue information */
2075 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2076 			"Fast-path FCP CQ information:\n");
2077 	fcp_qidx = 0;
2078 	if (phba->sli4_hba.fcp_cq) {
2079 		do {
2080 			if (phba->sli4_hba.fcp_cq[fcp_qidx]) {
2081 				len += snprintf(pbuffer+len,
2082 					LPFC_QUE_INFO_GET_BUF_SIZE-len,
2083 				"Associated EQID[%02d]:\n",
2084 				phba->sli4_hba.fcp_cq[fcp_qidx]->assoc_qid);
2085 				len += snprintf(pbuffer+len,
2086 					LPFC_QUE_INFO_GET_BUF_SIZE-len,
2087 				"\tCQID[%02d], "
2088 				"QE-COUNT[%04d], QE-SIZE[%04d], "
2089 				"HOST-INDEX[%04d], PORT-INDEX[%04d]\n",
2090 				phba->sli4_hba.fcp_cq[fcp_qidx]->queue_id,
2091 				phba->sli4_hba.fcp_cq[fcp_qidx]->entry_count,
2092 				phba->sli4_hba.fcp_cq[fcp_qidx]->entry_size,
2093 				phba->sli4_hba.fcp_cq[fcp_qidx]->host_index,
2094 				phba->sli4_hba.fcp_cq[fcp_qidx]->hba_index);
2095 			}
2096 		} while (++fcp_qidx < phba->cfg_fcp_eq_count);
2097 		len += snprintf(pbuffer+len,
2098 				LPFC_QUE_INFO_GET_BUF_SIZE-len, "\n");
2099 	}
2100 
2101 	/* Get mailbox queue information */
2102 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2103 			"Slow-path MBX MQ information:\n");
2104 	if (phba->sli4_hba.mbx_wq) {
2105 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2106 			"Associated CQID[%02d]:\n",
2107 			phba->sli4_hba.mbx_wq->assoc_qid);
2108 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2109 			"\tWQID[%02d], "
2110 			"QE-COUNT[%04d], QE-SIZE[%04d], "
2111 			"HOST-INDEX[%04d], PORT-INDEX[%04d]\n\n",
2112 			phba->sli4_hba.mbx_wq->queue_id,
2113 			phba->sli4_hba.mbx_wq->entry_count,
2114 			phba->sli4_hba.mbx_wq->entry_size,
2115 			phba->sli4_hba.mbx_wq->host_index,
2116 			phba->sli4_hba.mbx_wq->hba_index);
2117 	}
2118 
2119 	/* Get slow-path work queue information */
2120 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2121 			"Slow-path ELS WQ information:\n");
2122 	if (phba->sli4_hba.els_wq) {
2123 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2124 			"Associated CQID[%02d]:\n",
2125 			phba->sli4_hba.els_wq->assoc_qid);
2126 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2127 			"\tWQID[%02d], "
2128 			"QE-COUNT[%04d], QE-SIZE[%04d], "
2129 			"HOST-INDEX[%04d], PORT-INDEX[%04d]\n\n",
2130 			phba->sli4_hba.els_wq->queue_id,
2131 			phba->sli4_hba.els_wq->entry_count,
2132 			phba->sli4_hba.els_wq->entry_size,
2133 			phba->sli4_hba.els_wq->host_index,
2134 			phba->sli4_hba.els_wq->hba_index);
2135 	}
2136 
2137 	/* Get fast-path work queue information */
2138 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2139 			"Fast-path FCP WQ information:\n");
2140 	if (phba->sli4_hba.fcp_wq) {
2141 		for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count;
2142 		     fcp_qidx++) {
2143 			if (!phba->sli4_hba.fcp_wq[fcp_qidx])
2144 				continue;
2145 			len += snprintf(pbuffer+len,
2146 					LPFC_QUE_INFO_GET_BUF_SIZE-len,
2147 				"Associated CQID[%02d]:\n",
2148 				phba->sli4_hba.fcp_wq[fcp_qidx]->assoc_qid);
2149 			len += snprintf(pbuffer+len,
2150 					LPFC_QUE_INFO_GET_BUF_SIZE-len,
2151 				"\tWQID[%02d], "
2152 				"QE-COUNT[%04d], WQE-SIZE[%04d], "
2153 				"HOST-INDEX[%04d], PORT-INDEX[%04d]\n",
2154 				phba->sli4_hba.fcp_wq[fcp_qidx]->queue_id,
2155 				phba->sli4_hba.fcp_wq[fcp_qidx]->entry_count,
2156 				phba->sli4_hba.fcp_wq[fcp_qidx]->entry_size,
2157 				phba->sli4_hba.fcp_wq[fcp_qidx]->host_index,
2158 				phba->sli4_hba.fcp_wq[fcp_qidx]->hba_index);
2159 		}
2160 		len += snprintf(pbuffer+len,
2161 				LPFC_QUE_INFO_GET_BUF_SIZE-len, "\n");
2162 	}
2163 
2164 	/* Get receive queue information */
2165 	len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2166 			"Slow-path RQ information:\n");
2167 	if (phba->sli4_hba.hdr_rq && phba->sli4_hba.dat_rq) {
2168 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2169 			"Associated CQID[%02d]:\n",
2170 			phba->sli4_hba.hdr_rq->assoc_qid);
2171 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2172 			"\tHQID[%02d], "
2173 			"QE-COUNT[%04d], QE-SIZE[%04d], "
2174 			"HOST-INDEX[%04d], PORT-INDEX[%04d]\n",
2175 			phba->sli4_hba.hdr_rq->queue_id,
2176 			phba->sli4_hba.hdr_rq->entry_count,
2177 			phba->sli4_hba.hdr_rq->entry_size,
2178 			phba->sli4_hba.hdr_rq->host_index,
2179 			phba->sli4_hba.hdr_rq->hba_index);
2180 		len += snprintf(pbuffer+len, LPFC_QUE_INFO_GET_BUF_SIZE-len,
2181 			"\tDQID[%02d], "
2182 			"QE-COUNT[%04d], QE-SIZE[%04d], "
2183 			"HOST-INDEX[%04d], PORT-INDEX[%04d]\n",
2184 			phba->sli4_hba.dat_rq->queue_id,
2185 			phba->sli4_hba.dat_rq->entry_count,
2186 			phba->sli4_hba.dat_rq->entry_size,
2187 			phba->sli4_hba.dat_rq->host_index,
2188 			phba->sli4_hba.dat_rq->hba_index);
2189 	}
2190 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2191 }
2192 
2193 /**
2194  * lpfc_idiag_que_param_check - queue access command parameter sanity check
2195  * @q: The pointer to queue structure.
2196  * @index: The index into a queue entry.
2197  * @count: The number of queue entries to access.
2198  *
2199  * Description:
2200  * The routine performs sanity check on device queue access method commands.
2201  *
2202  * Returns:
2203  * This function returns -EINVAL when fails the sanity check, otherwise, it
2204  * returns 0.
2205  **/
2206 static int
2207 lpfc_idiag_que_param_check(struct lpfc_queue *q, int index, int count)
2208 {
2209 	/* Only support single entry read or browsing */
2210 	if ((count != 1) && (count != LPFC_QUE_ACC_BROWSE))
2211 		return -EINVAL;
2212 	if (index > q->entry_count - 1)
2213 		return -EINVAL;
2214 	return 0;
2215 }
2216 
2217 /**
2218  * lpfc_idiag_queacc_read_qe - read a single entry from the given queue index
2219  * @pbuffer: The pointer to buffer to copy the read data into.
2220  * @pque: The pointer to the queue to be read.
2221  * @index: The index into the queue entry.
2222  *
2223  * Description:
2224  * This routine reads out a single entry from the given queue's index location
2225  * and copies it into the buffer provided.
2226  *
2227  * Returns:
2228  * This function returns 0 when it fails, otherwise, it returns the length of
2229  * the data read into the buffer provided.
2230  **/
2231 static int
2232 lpfc_idiag_queacc_read_qe(char *pbuffer, int len, struct lpfc_queue *pque,
2233 			  uint32_t index)
2234 {
2235 	int offset, esize;
2236 	uint32_t *pentry;
2237 
2238 	if (!pbuffer || !pque)
2239 		return 0;
2240 
2241 	esize = pque->entry_size;
2242 	len += snprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
2243 			"QE-INDEX[%04d]:\n", index);
2244 
2245 	offset = 0;
2246 	pentry = pque->qe[index].address;
2247 	while (esize > 0) {
2248 		len += snprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
2249 				"%08x ", *pentry);
2250 		pentry++;
2251 		offset += sizeof(uint32_t);
2252 		esize -= sizeof(uint32_t);
2253 		if (esize > 0 && !(offset % (4 * sizeof(uint32_t))))
2254 			len += snprintf(pbuffer+len,
2255 					LPFC_QUE_ACC_BUF_SIZE-len, "\n");
2256 	}
2257 	len += snprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len, "\n");
2258 
2259 	return len;
2260 }
2261 
2262 /**
2263  * lpfc_idiag_queacc_read - idiag debugfs read port queue
2264  * @file: The file pointer to read from.
2265  * @buf: The buffer to copy the data to.
2266  * @nbytes: The number of bytes to read.
2267  * @ppos: The position in the file to start reading from.
2268  *
2269  * Description:
2270  * This routine reads data from the @phba device queue memory according to the
2271  * idiag command, and copies to user @buf. Depending on the queue dump read
2272  * command setup, it does either a single queue entry read or browing through
2273  * all entries of the queue.
2274  *
2275  * Returns:
2276  * This function returns the amount of data that was read (this could be less
2277  * than @nbytes if the end of the file was reached) or a negative error value.
2278  **/
2279 static ssize_t
2280 lpfc_idiag_queacc_read(struct file *file, char __user *buf, size_t nbytes,
2281 		       loff_t *ppos)
2282 {
2283 	struct lpfc_debug *debug = file->private_data;
2284 	uint32_t last_index, index, count;
2285 	struct lpfc_queue *pque = NULL;
2286 	char *pbuffer;
2287 	int len = 0;
2288 
2289 	/* This is a user read operation */
2290 	debug->op = LPFC_IDIAG_OP_RD;
2291 
2292 	if (!debug->buffer)
2293 		debug->buffer = kmalloc(LPFC_QUE_ACC_BUF_SIZE, GFP_KERNEL);
2294 	if (!debug->buffer)
2295 		return 0;
2296 	pbuffer = debug->buffer;
2297 
2298 	if (*ppos)
2299 		return 0;
2300 
2301 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
2302 		index = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
2303 		count = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
2304 		pque = (struct lpfc_queue *)idiag.ptr_private;
2305 	} else
2306 		return 0;
2307 
2308 	/* Browse the queue starting from index */
2309 	if (count == LPFC_QUE_ACC_BROWSE)
2310 		goto que_browse;
2311 
2312 	/* Read a single entry from the queue */
2313 	len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
2314 
2315 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2316 
2317 que_browse:
2318 
2319 	/* Browse all entries from the queue */
2320 	last_index = idiag.offset.last_rd;
2321 	index = last_index;
2322 
2323 	while (len < LPFC_QUE_ACC_SIZE - pque->entry_size) {
2324 		len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
2325 		index++;
2326 		if (index > pque->entry_count - 1)
2327 			break;
2328 	}
2329 
2330 	/* Set up the offset for next portion of pci cfg read */
2331 	if (index > pque->entry_count - 1)
2332 		index = 0;
2333 	idiag.offset.last_rd = index;
2334 
2335 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2336 }
2337 
2338 /**
2339  * lpfc_idiag_queacc_write - Syntax check and set up idiag queacc commands
2340  * @file: The file pointer to read from.
2341  * @buf: The buffer to copy the user data from.
2342  * @nbytes: The number of bytes to get.
2343  * @ppos: The position in the file to start reading from.
2344  *
2345  * This routine get the debugfs idiag command struct from user space and then
2346  * perform the syntax check for port queue read (dump) or write (set) command
2347  * accordingly. In the case of port queue read command, it sets up the command
2348  * in the idiag command struct for the following debugfs read operation. In
2349  * the case of port queue write operation, it executes the write operation
2350  * into the port queue entry accordingly.
2351  *
2352  * It returns the @nbytges passing in from debugfs user space when successful.
2353  * In case of error conditions, it returns proper error code back to the user
2354  * space.
2355  **/
2356 static ssize_t
2357 lpfc_idiag_queacc_write(struct file *file, const char __user *buf,
2358 			size_t nbytes, loff_t *ppos)
2359 {
2360 	struct lpfc_debug *debug = file->private_data;
2361 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2362 	uint32_t qidx, quetp, queid, index, count, offset, value;
2363 	uint32_t *pentry;
2364 	struct lpfc_queue *pque;
2365 	int rc;
2366 
2367 	/* This is a user write operation */
2368 	debug->op = LPFC_IDIAG_OP_WR;
2369 
2370 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
2371 	if (rc < 0)
2372 		return rc;
2373 
2374 	/* Get and sanity check on command feilds */
2375 	quetp  = idiag.cmd.data[IDIAG_QUEACC_QUETP_INDX];
2376 	queid  = idiag.cmd.data[IDIAG_QUEACC_QUEID_INDX];
2377 	index  = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
2378 	count  = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
2379 	offset = idiag.cmd.data[IDIAG_QUEACC_OFFST_INDX];
2380 	value  = idiag.cmd.data[IDIAG_QUEACC_VALUE_INDX];
2381 
2382 	/* Sanity check on command line arguments */
2383 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
2384 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
2385 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
2386 		if (rc != LPFC_QUE_ACC_WR_CMD_ARG)
2387 			goto error_out;
2388 		if (count != 1)
2389 			goto error_out;
2390 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
2391 		if (rc != LPFC_QUE_ACC_RD_CMD_ARG)
2392 			goto error_out;
2393 	} else
2394 		goto error_out;
2395 
2396 	switch (quetp) {
2397 	case LPFC_IDIAG_EQ:
2398 		/* Slow-path event queue */
2399 		if (phba->sli4_hba.sp_eq &&
2400 		    phba->sli4_hba.sp_eq->queue_id == queid) {
2401 			/* Sanity check */
2402 			rc = lpfc_idiag_que_param_check(
2403 					phba->sli4_hba.sp_eq, index, count);
2404 			if (rc)
2405 				goto error_out;
2406 			idiag.ptr_private = phba->sli4_hba.sp_eq;
2407 			goto pass_check;
2408 		}
2409 		/* Fast-path event queue */
2410 		if (phba->sli4_hba.fp_eq) {
2411 			for (qidx = 0; qidx < phba->cfg_fcp_eq_count; qidx++) {
2412 				if (phba->sli4_hba.fp_eq[qidx] &&
2413 				    phba->sli4_hba.fp_eq[qidx]->queue_id ==
2414 				    queid) {
2415 					/* Sanity check */
2416 					rc = lpfc_idiag_que_param_check(
2417 						phba->sli4_hba.fp_eq[qidx],
2418 						index, count);
2419 					if (rc)
2420 						goto error_out;
2421 					idiag.ptr_private =
2422 						phba->sli4_hba.fp_eq[qidx];
2423 					goto pass_check;
2424 				}
2425 			}
2426 		}
2427 		goto error_out;
2428 		break;
2429 	case LPFC_IDIAG_CQ:
2430 		/* MBX complete queue */
2431 		if (phba->sli4_hba.mbx_cq &&
2432 		    phba->sli4_hba.mbx_cq->queue_id == queid) {
2433 			/* Sanity check */
2434 			rc = lpfc_idiag_que_param_check(
2435 					phba->sli4_hba.mbx_cq, index, count);
2436 			if (rc)
2437 				goto error_out;
2438 			idiag.ptr_private = phba->sli4_hba.mbx_cq;
2439 			goto pass_check;
2440 		}
2441 		/* ELS complete queue */
2442 		if (phba->sli4_hba.els_cq &&
2443 		    phba->sli4_hba.els_cq->queue_id == queid) {
2444 			/* Sanity check */
2445 			rc = lpfc_idiag_que_param_check(
2446 					phba->sli4_hba.els_cq, index, count);
2447 			if (rc)
2448 				goto error_out;
2449 			idiag.ptr_private = phba->sli4_hba.els_cq;
2450 			goto pass_check;
2451 		}
2452 		/* FCP complete queue */
2453 		if (phba->sli4_hba.fcp_cq) {
2454 			qidx = 0;
2455 			do {
2456 				if (phba->sli4_hba.fcp_cq[qidx] &&
2457 				    phba->sli4_hba.fcp_cq[qidx]->queue_id ==
2458 				    queid) {
2459 					/* Sanity check */
2460 					rc = lpfc_idiag_que_param_check(
2461 						phba->sli4_hba.fcp_cq[qidx],
2462 						index, count);
2463 					if (rc)
2464 						goto error_out;
2465 					idiag.ptr_private =
2466 						phba->sli4_hba.fcp_cq[qidx];
2467 					goto pass_check;
2468 				}
2469 			} while (++qidx < phba->cfg_fcp_eq_count);
2470 		}
2471 		goto error_out;
2472 		break;
2473 	case LPFC_IDIAG_MQ:
2474 		/* MBX work queue */
2475 		if (phba->sli4_hba.mbx_wq &&
2476 		    phba->sli4_hba.mbx_wq->queue_id == queid) {
2477 			/* Sanity check */
2478 			rc = lpfc_idiag_que_param_check(
2479 					phba->sli4_hba.mbx_wq, index, count);
2480 			if (rc)
2481 				goto error_out;
2482 			idiag.ptr_private = phba->sli4_hba.mbx_wq;
2483 			goto pass_check;
2484 		}
2485 		goto error_out;
2486 		break;
2487 	case LPFC_IDIAG_WQ:
2488 		/* ELS work queue */
2489 		if (phba->sli4_hba.els_wq &&
2490 		    phba->sli4_hba.els_wq->queue_id == queid) {
2491 			/* Sanity check */
2492 			rc = lpfc_idiag_que_param_check(
2493 					phba->sli4_hba.els_wq, index, count);
2494 			if (rc)
2495 				goto error_out;
2496 			idiag.ptr_private = phba->sli4_hba.els_wq;
2497 			goto pass_check;
2498 		}
2499 		/* FCP work queue */
2500 		if (phba->sli4_hba.fcp_wq) {
2501 			for (qidx = 0; qidx < phba->cfg_fcp_wq_count; qidx++) {
2502 				if (!phba->sli4_hba.fcp_wq[qidx])
2503 					continue;
2504 				if (phba->sli4_hba.fcp_wq[qidx]->queue_id ==
2505 				    queid) {
2506 					/* Sanity check */
2507 					rc = lpfc_idiag_que_param_check(
2508 						phba->sli4_hba.fcp_wq[qidx],
2509 						index, count);
2510 					if (rc)
2511 						goto error_out;
2512 					idiag.ptr_private =
2513 						phba->sli4_hba.fcp_wq[qidx];
2514 					goto pass_check;
2515 				}
2516 			}
2517 		}
2518 		goto error_out;
2519 		break;
2520 	case LPFC_IDIAG_RQ:
2521 		/* HDR queue */
2522 		if (phba->sli4_hba.hdr_rq &&
2523 		    phba->sli4_hba.hdr_rq->queue_id == queid) {
2524 			/* Sanity check */
2525 			rc = lpfc_idiag_que_param_check(
2526 					phba->sli4_hba.hdr_rq, index, count);
2527 			if (rc)
2528 				goto error_out;
2529 			idiag.ptr_private = phba->sli4_hba.hdr_rq;
2530 			goto pass_check;
2531 		}
2532 		/* DAT queue */
2533 		if (phba->sli4_hba.dat_rq &&
2534 		    phba->sli4_hba.dat_rq->queue_id == queid) {
2535 			/* Sanity check */
2536 			rc = lpfc_idiag_que_param_check(
2537 					phba->sli4_hba.dat_rq, index, count);
2538 			if (rc)
2539 				goto error_out;
2540 			idiag.ptr_private = phba->sli4_hba.dat_rq;
2541 			goto pass_check;
2542 		}
2543 		goto error_out;
2544 		break;
2545 	default:
2546 		goto error_out;
2547 		break;
2548 	}
2549 
2550 pass_check:
2551 
2552 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
2553 		if (count == LPFC_QUE_ACC_BROWSE)
2554 			idiag.offset.last_rd = index;
2555 	}
2556 
2557 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
2558 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
2559 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
2560 		/* Additional sanity checks on write operation */
2561 		pque = (struct lpfc_queue *)idiag.ptr_private;
2562 		if (offset > pque->entry_size/sizeof(uint32_t) - 1)
2563 			goto error_out;
2564 		pentry = pque->qe[index].address;
2565 		pentry += offset;
2566 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR)
2567 			*pentry = value;
2568 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST)
2569 			*pentry |= value;
2570 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL)
2571 			*pentry &= ~value;
2572 	}
2573 	return nbytes;
2574 
2575 error_out:
2576 	/* Clean out command structure on command error out */
2577 	memset(&idiag, 0, sizeof(idiag));
2578 	return -EINVAL;
2579 }
2580 
2581 /**
2582  * lpfc_idiag_drbacc_read_reg - idiag debugfs read a doorbell register
2583  * @phba: The pointer to hba structure.
2584  * @pbuffer: The pointer to the buffer to copy the data to.
2585  * @len: The lenght of bytes to copied.
2586  * @drbregid: The id to doorbell registers.
2587  *
2588  * Description:
2589  * This routine reads a doorbell register and copies its content to the
2590  * user buffer pointed to by @pbuffer.
2591  *
2592  * Returns:
2593  * This function returns the amount of data that was copied into @pbuffer.
2594  **/
2595 static int
2596 lpfc_idiag_drbacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
2597 			   int len, uint32_t drbregid)
2598 {
2599 
2600 	if (!pbuffer)
2601 		return 0;
2602 
2603 	switch (drbregid) {
2604 	case LPFC_DRB_EQCQ:
2605 		len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
2606 				"EQCQ-DRB-REG: 0x%08x\n",
2607 				readl(phba->sli4_hba.EQCQDBregaddr));
2608 		break;
2609 	case LPFC_DRB_MQ:
2610 		len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
2611 				"MQ-DRB-REG:   0x%08x\n",
2612 				readl(phba->sli4_hba.MQDBregaddr));
2613 		break;
2614 	case LPFC_DRB_WQ:
2615 		len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
2616 				"WQ-DRB-REG:   0x%08x\n",
2617 				readl(phba->sli4_hba.WQDBregaddr));
2618 		break;
2619 	case LPFC_DRB_RQ:
2620 		len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
2621 				"RQ-DRB-REG:   0x%08x\n",
2622 				readl(phba->sli4_hba.RQDBregaddr));
2623 		break;
2624 	default:
2625 		break;
2626 	}
2627 
2628 	return len;
2629 }
2630 
2631 /**
2632  * lpfc_idiag_drbacc_read - idiag debugfs read port doorbell
2633  * @file: The file pointer to read from.
2634  * @buf: The buffer to copy the data to.
2635  * @nbytes: The number of bytes to read.
2636  * @ppos: The position in the file to start reading from.
2637  *
2638  * Description:
2639  * This routine reads data from the @phba device doorbell register according
2640  * to the idiag command, and copies to user @buf. Depending on the doorbell
2641  * register read command setup, it does either a single doorbell register
2642  * read or dump all doorbell registers.
2643  *
2644  * Returns:
2645  * This function returns the amount of data that was read (this could be less
2646  * than @nbytes if the end of the file was reached) or a negative error value.
2647  **/
2648 static ssize_t
2649 lpfc_idiag_drbacc_read(struct file *file, char __user *buf, size_t nbytes,
2650 		       loff_t *ppos)
2651 {
2652 	struct lpfc_debug *debug = file->private_data;
2653 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2654 	uint32_t drb_reg_id, i;
2655 	char *pbuffer;
2656 	int len = 0;
2657 
2658 	/* This is a user read operation */
2659 	debug->op = LPFC_IDIAG_OP_RD;
2660 
2661 	if (!debug->buffer)
2662 		debug->buffer = kmalloc(LPFC_DRB_ACC_BUF_SIZE, GFP_KERNEL);
2663 	if (!debug->buffer)
2664 		return 0;
2665 	pbuffer = debug->buffer;
2666 
2667 	if (*ppos)
2668 		return 0;
2669 
2670 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD)
2671 		drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
2672 	else
2673 		return 0;
2674 
2675 	if (drb_reg_id == LPFC_DRB_ACC_ALL)
2676 		for (i = 1; i <= LPFC_DRB_MAX; i++)
2677 			len = lpfc_idiag_drbacc_read_reg(phba,
2678 							 pbuffer, len, i);
2679 	else
2680 		len = lpfc_idiag_drbacc_read_reg(phba,
2681 						 pbuffer, len, drb_reg_id);
2682 
2683 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2684 }
2685 
2686 /**
2687  * lpfc_idiag_drbacc_write - Syntax check and set up idiag drbacc commands
2688  * @file: The file pointer to read from.
2689  * @buf: The buffer to copy the user data from.
2690  * @nbytes: The number of bytes to get.
2691  * @ppos: The position in the file to start reading from.
2692  *
2693  * This routine get the debugfs idiag command struct from user space and then
2694  * perform the syntax check for port doorbell register read (dump) or write
2695  * (set) command accordingly. In the case of port queue read command, it sets
2696  * up the command in the idiag command struct for the following debugfs read
2697  * operation. In the case of port doorbell register write operation, it
2698  * executes the write operation into the port doorbell register accordingly.
2699  *
2700  * It returns the @nbytges passing in from debugfs user space when successful.
2701  * In case of error conditions, it returns proper error code back to the user
2702  * space.
2703  **/
2704 static ssize_t
2705 lpfc_idiag_drbacc_write(struct file *file, const char __user *buf,
2706 			size_t nbytes, loff_t *ppos)
2707 {
2708 	struct lpfc_debug *debug = file->private_data;
2709 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2710 	uint32_t drb_reg_id, value, reg_val = 0;
2711 	void __iomem *drb_reg;
2712 	int rc;
2713 
2714 	/* This is a user write operation */
2715 	debug->op = LPFC_IDIAG_OP_WR;
2716 
2717 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
2718 	if (rc < 0)
2719 		return rc;
2720 
2721 	/* Sanity check on command line arguments */
2722 	drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
2723 	value = idiag.cmd.data[IDIAG_DRBACC_VALUE_INDX];
2724 
2725 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
2726 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
2727 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
2728 		if (rc != LPFC_DRB_ACC_WR_CMD_ARG)
2729 			goto error_out;
2730 		if (drb_reg_id > LPFC_DRB_MAX)
2731 			goto error_out;
2732 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD) {
2733 		if (rc != LPFC_DRB_ACC_RD_CMD_ARG)
2734 			goto error_out;
2735 		if ((drb_reg_id > LPFC_DRB_MAX) &&
2736 		    (drb_reg_id != LPFC_DRB_ACC_ALL))
2737 			goto error_out;
2738 	} else
2739 		goto error_out;
2740 
2741 	/* Perform the write access operation */
2742 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
2743 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
2744 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
2745 		switch (drb_reg_id) {
2746 		case LPFC_DRB_EQCQ:
2747 			drb_reg = phba->sli4_hba.EQCQDBregaddr;
2748 			break;
2749 		case LPFC_DRB_MQ:
2750 			drb_reg = phba->sli4_hba.MQDBregaddr;
2751 			break;
2752 		case LPFC_DRB_WQ:
2753 			drb_reg = phba->sli4_hba.WQDBregaddr;
2754 			break;
2755 		case LPFC_DRB_RQ:
2756 			drb_reg = phba->sli4_hba.RQDBregaddr;
2757 			break;
2758 		default:
2759 			goto error_out;
2760 		}
2761 
2762 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR)
2763 			reg_val = value;
2764 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST) {
2765 			reg_val = readl(drb_reg);
2766 			reg_val |= value;
2767 		}
2768 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
2769 			reg_val = readl(drb_reg);
2770 			reg_val &= ~value;
2771 		}
2772 		writel(reg_val, drb_reg);
2773 		readl(drb_reg); /* flush */
2774 	}
2775 	return nbytes;
2776 
2777 error_out:
2778 	/* Clean out command structure on command error out */
2779 	memset(&idiag, 0, sizeof(idiag));
2780 	return -EINVAL;
2781 }
2782 
2783 /**
2784  * lpfc_idiag_ctlacc_read_reg - idiag debugfs read a control registers
2785  * @phba: The pointer to hba structure.
2786  * @pbuffer: The pointer to the buffer to copy the data to.
2787  * @len: The lenght of bytes to copied.
2788  * @drbregid: The id to doorbell registers.
2789  *
2790  * Description:
2791  * This routine reads a control register and copies its content to the
2792  * user buffer pointed to by @pbuffer.
2793  *
2794  * Returns:
2795  * This function returns the amount of data that was copied into @pbuffer.
2796  **/
2797 static int
2798 lpfc_idiag_ctlacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
2799 			   int len, uint32_t ctlregid)
2800 {
2801 
2802 	if (!pbuffer)
2803 		return 0;
2804 
2805 	switch (ctlregid) {
2806 	case LPFC_CTL_PORT_SEM:
2807 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
2808 				"Port SemReg:   0x%08x\n",
2809 				readl(phba->sli4_hba.conf_regs_memmap_p +
2810 				      LPFC_CTL_PORT_SEM_OFFSET));
2811 		break;
2812 	case LPFC_CTL_PORT_STA:
2813 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
2814 				"Port StaReg:   0x%08x\n",
2815 				readl(phba->sli4_hba.conf_regs_memmap_p +
2816 				      LPFC_CTL_PORT_STA_OFFSET));
2817 		break;
2818 	case LPFC_CTL_PORT_CTL:
2819 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
2820 				"Port CtlReg:   0x%08x\n",
2821 				readl(phba->sli4_hba.conf_regs_memmap_p +
2822 				      LPFC_CTL_PORT_CTL_OFFSET));
2823 		break;
2824 	case LPFC_CTL_PORT_ER1:
2825 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
2826 				"Port Er1Reg:   0x%08x\n",
2827 				readl(phba->sli4_hba.conf_regs_memmap_p +
2828 				      LPFC_CTL_PORT_ER1_OFFSET));
2829 		break;
2830 	case LPFC_CTL_PORT_ER2:
2831 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
2832 				"Port Er2Reg:   0x%08x\n",
2833 				readl(phba->sli4_hba.conf_regs_memmap_p +
2834 				      LPFC_CTL_PORT_ER2_OFFSET));
2835 		break;
2836 	case LPFC_CTL_PDEV_CTL:
2837 		len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
2838 				"PDev CtlReg:   0x%08x\n",
2839 				readl(phba->sli4_hba.conf_regs_memmap_p +
2840 				      LPFC_CTL_PDEV_CTL_OFFSET));
2841 		break;
2842 	default:
2843 		break;
2844 	}
2845 	return len;
2846 }
2847 
2848 /**
2849  * lpfc_idiag_ctlacc_read - idiag debugfs read port and device control register
2850  * @file: The file pointer to read from.
2851  * @buf: The buffer to copy the data to.
2852  * @nbytes: The number of bytes to read.
2853  * @ppos: The position in the file to start reading from.
2854  *
2855  * Description:
2856  * This routine reads data from the @phba port and device registers according
2857  * to the idiag command, and copies to user @buf.
2858  *
2859  * Returns:
2860  * This function returns the amount of data that was read (this could be less
2861  * than @nbytes if the end of the file was reached) or a negative error value.
2862  **/
2863 static ssize_t
2864 lpfc_idiag_ctlacc_read(struct file *file, char __user *buf, size_t nbytes,
2865 		       loff_t *ppos)
2866 {
2867 	struct lpfc_debug *debug = file->private_data;
2868 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2869 	uint32_t ctl_reg_id, i;
2870 	char *pbuffer;
2871 	int len = 0;
2872 
2873 	/* This is a user read operation */
2874 	debug->op = LPFC_IDIAG_OP_RD;
2875 
2876 	if (!debug->buffer)
2877 		debug->buffer = kmalloc(LPFC_CTL_ACC_BUF_SIZE, GFP_KERNEL);
2878 	if (!debug->buffer)
2879 		return 0;
2880 	pbuffer = debug->buffer;
2881 
2882 	if (*ppos)
2883 		return 0;
2884 
2885 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD)
2886 		ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
2887 	else
2888 		return 0;
2889 
2890 	if (ctl_reg_id == LPFC_CTL_ACC_ALL)
2891 		for (i = 1; i <= LPFC_CTL_MAX; i++)
2892 			len = lpfc_idiag_ctlacc_read_reg(phba,
2893 							 pbuffer, len, i);
2894 	else
2895 		len = lpfc_idiag_ctlacc_read_reg(phba,
2896 						 pbuffer, len, ctl_reg_id);
2897 
2898 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2899 }
2900 
2901 /**
2902  * lpfc_idiag_ctlacc_write - Syntax check and set up idiag ctlacc commands
2903  * @file: The file pointer to read from.
2904  * @buf: The buffer to copy the user data from.
2905  * @nbytes: The number of bytes to get.
2906  * @ppos: The position in the file to start reading from.
2907  *
2908  * This routine get the debugfs idiag command struct from user space and then
2909  * perform the syntax check for port and device control register read (dump)
2910  * or write (set) command accordingly.
2911  *
2912  * It returns the @nbytges passing in from debugfs user space when successful.
2913  * In case of error conditions, it returns proper error code back to the user
2914  * space.
2915  **/
2916 static ssize_t
2917 lpfc_idiag_ctlacc_write(struct file *file, const char __user *buf,
2918 			size_t nbytes, loff_t *ppos)
2919 {
2920 	struct lpfc_debug *debug = file->private_data;
2921 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2922 	uint32_t ctl_reg_id, value, reg_val = 0;
2923 	void __iomem *ctl_reg;
2924 	int rc;
2925 
2926 	/* This is a user write operation */
2927 	debug->op = LPFC_IDIAG_OP_WR;
2928 
2929 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
2930 	if (rc < 0)
2931 		return rc;
2932 
2933 	/* Sanity check on command line arguments */
2934 	ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
2935 	value = idiag.cmd.data[IDIAG_CTLACC_VALUE_INDX];
2936 
2937 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
2938 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
2939 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
2940 		if (rc != LPFC_CTL_ACC_WR_CMD_ARG)
2941 			goto error_out;
2942 		if (ctl_reg_id > LPFC_CTL_MAX)
2943 			goto error_out;
2944 	} else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD) {
2945 		if (rc != LPFC_CTL_ACC_RD_CMD_ARG)
2946 			goto error_out;
2947 		if ((ctl_reg_id > LPFC_CTL_MAX) &&
2948 		    (ctl_reg_id != LPFC_CTL_ACC_ALL))
2949 			goto error_out;
2950 	} else
2951 		goto error_out;
2952 
2953 	/* Perform the write access operation */
2954 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
2955 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
2956 	    idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
2957 		switch (ctl_reg_id) {
2958 		case LPFC_CTL_PORT_SEM:
2959 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
2960 					LPFC_CTL_PORT_SEM_OFFSET;
2961 			break;
2962 		case LPFC_CTL_PORT_STA:
2963 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
2964 					LPFC_CTL_PORT_STA_OFFSET;
2965 			break;
2966 		case LPFC_CTL_PORT_CTL:
2967 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
2968 					LPFC_CTL_PORT_CTL_OFFSET;
2969 			break;
2970 		case LPFC_CTL_PORT_ER1:
2971 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
2972 					LPFC_CTL_PORT_ER1_OFFSET;
2973 			break;
2974 		case LPFC_CTL_PORT_ER2:
2975 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
2976 					LPFC_CTL_PORT_ER2_OFFSET;
2977 			break;
2978 		case LPFC_CTL_PDEV_CTL:
2979 			ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
2980 					LPFC_CTL_PDEV_CTL_OFFSET;
2981 			break;
2982 		default:
2983 			goto error_out;
2984 		}
2985 
2986 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR)
2987 			reg_val = value;
2988 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST) {
2989 			reg_val = readl(ctl_reg);
2990 			reg_val |= value;
2991 		}
2992 		if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
2993 			reg_val = readl(ctl_reg);
2994 			reg_val &= ~value;
2995 		}
2996 		writel(reg_val, ctl_reg);
2997 		readl(ctl_reg); /* flush */
2998 	}
2999 	return nbytes;
3000 
3001 error_out:
3002 	/* Clean out command structure on command error out */
3003 	memset(&idiag, 0, sizeof(idiag));
3004 	return -EINVAL;
3005 }
3006 
3007 /**
3008  * lpfc_idiag_mbxacc_get_setup - idiag debugfs get mailbox access setup
3009  * @phba: Pointer to HBA context object.
3010  * @pbuffer: Pointer to data buffer.
3011  *
3012  * Description:
3013  * This routine gets the driver mailbox access debugfs setup information.
3014  *
3015  * Returns:
3016  * This function returns the amount of data that was read (this could be less
3017  * than @nbytes if the end of the file was reached) or a negative error value.
3018  **/
3019 static int
3020 lpfc_idiag_mbxacc_get_setup(struct lpfc_hba *phba, char *pbuffer)
3021 {
3022 	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
3023 	int len = 0;
3024 
3025 	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
3026 	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
3027 	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
3028 	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
3029 
3030 	len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
3031 			"mbx_dump_map: 0x%08x\n", mbx_dump_map);
3032 	len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
3033 			"mbx_dump_cnt: %04d\n", mbx_dump_cnt);
3034 	len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
3035 			"mbx_word_cnt: %04d\n", mbx_word_cnt);
3036 	len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
3037 			"mbx_mbox_cmd: 0x%02x\n", mbx_mbox_cmd);
3038 
3039 	return len;
3040 }
3041 
3042 /**
3043  * lpfc_idiag_mbxacc_read - idiag debugfs read on mailbox access
3044  * @file: The file pointer to read from.
3045  * @buf: The buffer to copy the data to.
3046  * @nbytes: The number of bytes to read.
3047  * @ppos: The position in the file to start reading from.
3048  *
3049  * Description:
3050  * This routine reads data from the @phba driver mailbox access debugfs setup
3051  * information.
3052  *
3053  * Returns:
3054  * This function returns the amount of data that was read (this could be less
3055  * than @nbytes if the end of the file was reached) or a negative error value.
3056  **/
3057 static ssize_t
3058 lpfc_idiag_mbxacc_read(struct file *file, char __user *buf, size_t nbytes,
3059 		       loff_t *ppos)
3060 {
3061 	struct lpfc_debug *debug = file->private_data;
3062 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3063 	char *pbuffer;
3064 	int len = 0;
3065 
3066 	/* This is a user read operation */
3067 	debug->op = LPFC_IDIAG_OP_RD;
3068 
3069 	if (!debug->buffer)
3070 		debug->buffer = kmalloc(LPFC_MBX_ACC_BUF_SIZE, GFP_KERNEL);
3071 	if (!debug->buffer)
3072 		return 0;
3073 	pbuffer = debug->buffer;
3074 
3075 	if (*ppos)
3076 		return 0;
3077 
3078 	if ((idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP) &&
3079 	    (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP))
3080 		return 0;
3081 
3082 	len = lpfc_idiag_mbxacc_get_setup(phba, pbuffer);
3083 
3084 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3085 }
3086 
3087 /**
3088  * lpfc_idiag_mbxacc_write - Syntax check and set up idiag mbxacc commands
3089  * @file: The file pointer to read from.
3090  * @buf: The buffer to copy the user data from.
3091  * @nbytes: The number of bytes to get.
3092  * @ppos: The position in the file to start reading from.
3093  *
3094  * This routine get the debugfs idiag command struct from user space and then
3095  * perform the syntax check for driver mailbox command (dump) and sets up the
3096  * necessary states in the idiag command struct accordingly.
3097  *
3098  * It returns the @nbytges passing in from debugfs user space when successful.
3099  * In case of error conditions, it returns proper error code back to the user
3100  * space.
3101  **/
3102 static ssize_t
3103 lpfc_idiag_mbxacc_write(struct file *file, const char __user *buf,
3104 			size_t nbytes, loff_t *ppos)
3105 {
3106 	struct lpfc_debug *debug = file->private_data;
3107 	uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
3108 	int rc;
3109 
3110 	/* This is a user write operation */
3111 	debug->op = LPFC_IDIAG_OP_WR;
3112 
3113 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3114 	if (rc < 0)
3115 		return rc;
3116 
3117 	/* Sanity check on command line arguments */
3118 	mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
3119 	mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
3120 	mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
3121 	mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
3122 
3123 	if (idiag.cmd.opcode == LPFC_IDIAG_CMD_MBXACC_DP) {
3124 		if (!(mbx_dump_map & LPFC_MBX_DMP_MBX_ALL))
3125 			goto error_out;
3126 		if ((mbx_dump_map & ~LPFC_MBX_DMP_MBX_ALL) &&
3127 		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
3128 			goto error_out;
3129 		if (mbx_word_cnt > sizeof(MAILBOX_t))
3130 			goto error_out;
3131 	} else if (idiag.cmd.opcode == LPFC_IDIAG_BSG_MBXACC_DP) {
3132 		if (!(mbx_dump_map & LPFC_BSG_DMP_MBX_ALL))
3133 			goto error_out;
3134 		if ((mbx_dump_map & ~LPFC_BSG_DMP_MBX_ALL) &&
3135 		    (mbx_dump_map != LPFC_MBX_DMP_ALL))
3136 			goto error_out;
3137 		if (mbx_word_cnt > (BSG_MBOX_SIZE)/4)
3138 			goto error_out;
3139 		if (mbx_mbox_cmd != 0x9b)
3140 			goto error_out;
3141 	} else
3142 		goto error_out;
3143 
3144 	if (mbx_word_cnt == 0)
3145 		goto error_out;
3146 	if (rc != LPFC_MBX_DMP_ARG)
3147 		goto error_out;
3148 	if (mbx_mbox_cmd & ~0xff)
3149 		goto error_out;
3150 
3151 	/* condition for stop mailbox dump */
3152 	if (mbx_dump_cnt == 0)
3153 		goto reset_out;
3154 
3155 	return nbytes;
3156 
3157 reset_out:
3158 	/* Clean out command structure on command error out */
3159 	memset(&idiag, 0, sizeof(idiag));
3160 	return nbytes;
3161 
3162 error_out:
3163 	/* Clean out command structure on command error out */
3164 	memset(&idiag, 0, sizeof(idiag));
3165 	return -EINVAL;
3166 }
3167 
3168 /**
3169  * lpfc_idiag_extacc_avail_get - get the available extents information
3170  * @phba: pointer to lpfc hba data structure.
3171  * @pbuffer: pointer to internal buffer.
3172  * @len: length into the internal buffer data has been copied.
3173  *
3174  * Description:
3175  * This routine is to get the available extent information.
3176  *
3177  * Returns:
3178  * overall lenth of the data read into the internal buffer.
3179  **/
3180 static int
3181 lpfc_idiag_extacc_avail_get(struct lpfc_hba *phba, char *pbuffer, int len)
3182 {
3183 	uint16_t ext_cnt, ext_size;
3184 
3185 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3186 			"\nAvailable Extents Information:\n");
3187 
3188 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3189 			"\tPort Available VPI extents: ");
3190 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VPI,
3191 				       &ext_cnt, &ext_size);
3192 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3193 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
3194 
3195 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3196 			"\tPort Available VFI extents: ");
3197 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VFI,
3198 				       &ext_cnt, &ext_size);
3199 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3200 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
3201 
3202 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3203 			"\tPort Available RPI extents: ");
3204 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_RPI,
3205 				       &ext_cnt, &ext_size);
3206 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3207 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
3208 
3209 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3210 			"\tPort Available XRI extents: ");
3211 	lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_XRI,
3212 				       &ext_cnt, &ext_size);
3213 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3214 			"Count %3d, Size %3d\n", ext_cnt, ext_size);
3215 
3216 	return len;
3217 }
3218 
3219 /**
3220  * lpfc_idiag_extacc_alloc_get - get the allocated extents information
3221  * @phba: pointer to lpfc hba data structure.
3222  * @pbuffer: pointer to internal buffer.
3223  * @len: length into the internal buffer data has been copied.
3224  *
3225  * Description:
3226  * This routine is to get the allocated extent information.
3227  *
3228  * Returns:
3229  * overall lenth of the data read into the internal buffer.
3230  **/
3231 static int
3232 lpfc_idiag_extacc_alloc_get(struct lpfc_hba *phba, char *pbuffer, int len)
3233 {
3234 	uint16_t ext_cnt, ext_size;
3235 	int rc;
3236 
3237 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3238 			"\nAllocated Extents Information:\n");
3239 
3240 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3241 			"\tHost Allocated VPI extents: ");
3242 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VPI,
3243 					    &ext_cnt, &ext_size);
3244 	if (!rc)
3245 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3246 				"Port %d Extent %3d, Size %3d\n",
3247 				phba->brd_no, ext_cnt, ext_size);
3248 	else
3249 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3250 				"N/A\n");
3251 
3252 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3253 			"\tHost Allocated VFI extents: ");
3254 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VFI,
3255 					    &ext_cnt, &ext_size);
3256 	if (!rc)
3257 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3258 				"Port %d Extent %3d, Size %3d\n",
3259 				phba->brd_no, ext_cnt, ext_size);
3260 	else
3261 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3262 				"N/A\n");
3263 
3264 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3265 			"\tHost Allocated RPI extents: ");
3266 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_RPI,
3267 					    &ext_cnt, &ext_size);
3268 	if (!rc)
3269 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3270 				"Port %d Extent %3d, Size %3d\n",
3271 				phba->brd_no, ext_cnt, ext_size);
3272 	else
3273 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3274 				"N/A\n");
3275 
3276 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3277 			"\tHost Allocated XRI extents: ");
3278 	rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_XRI,
3279 					    &ext_cnt, &ext_size);
3280 	if (!rc)
3281 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3282 				"Port %d Extent %3d, Size %3d\n",
3283 				phba->brd_no, ext_cnt, ext_size);
3284 	else
3285 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3286 				"N/A\n");
3287 
3288 	return len;
3289 }
3290 
3291 /**
3292  * lpfc_idiag_extacc_drivr_get - get driver extent information
3293  * @phba: pointer to lpfc hba data structure.
3294  * @pbuffer: pointer to internal buffer.
3295  * @len: length into the internal buffer data has been copied.
3296  *
3297  * Description:
3298  * This routine is to get the driver extent information.
3299  *
3300  * Returns:
3301  * overall lenth of the data read into the internal buffer.
3302  **/
3303 static int
3304 lpfc_idiag_extacc_drivr_get(struct lpfc_hba *phba, char *pbuffer, int len)
3305 {
3306 	struct lpfc_rsrc_blks *rsrc_blks;
3307 	int index;
3308 
3309 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3310 			"\nDriver Extents Information:\n");
3311 
3312 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3313 			"\tVPI extents:\n");
3314 	index = 0;
3315 	list_for_each_entry(rsrc_blks, &phba->lpfc_vpi_blk_list, list) {
3316 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3317 				"\t\tBlock %3d: Start %4d, Count %4d\n",
3318 				index, rsrc_blks->rsrc_start,
3319 				rsrc_blks->rsrc_size);
3320 		index++;
3321 	}
3322 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3323 			"\tVFI extents:\n");
3324 	index = 0;
3325 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_vfi_blk_list,
3326 			    list) {
3327 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3328 				"\t\tBlock %3d: Start %4d, Count %4d\n",
3329 				index, rsrc_blks->rsrc_start,
3330 				rsrc_blks->rsrc_size);
3331 		index++;
3332 	}
3333 
3334 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3335 			"\tRPI extents:\n");
3336 	index = 0;
3337 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_rpi_blk_list,
3338 			    list) {
3339 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3340 				"\t\tBlock %3d: Start %4d, Count %4d\n",
3341 				index, rsrc_blks->rsrc_start,
3342 				rsrc_blks->rsrc_size);
3343 		index++;
3344 	}
3345 
3346 	len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3347 			"\tXRI extents:\n");
3348 	index = 0;
3349 	list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_xri_blk_list,
3350 			    list) {
3351 		len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
3352 				"\t\tBlock %3d: Start %4d, Count %4d\n",
3353 				index, rsrc_blks->rsrc_start,
3354 				rsrc_blks->rsrc_size);
3355 		index++;
3356 	}
3357 
3358 	return len;
3359 }
3360 
3361 /**
3362  * lpfc_idiag_extacc_write - Syntax check and set up idiag extacc commands
3363  * @file: The file pointer to read from.
3364  * @buf: The buffer to copy the user data from.
3365  * @nbytes: The number of bytes to get.
3366  * @ppos: The position in the file to start reading from.
3367  *
3368  * This routine get the debugfs idiag command struct from user space and then
3369  * perform the syntax check for extent information access commands and sets
3370  * up the necessary states in the idiag command struct accordingly.
3371  *
3372  * It returns the @nbytges passing in from debugfs user space when successful.
3373  * In case of error conditions, it returns proper error code back to the user
3374  * space.
3375  **/
3376 static ssize_t
3377 lpfc_idiag_extacc_write(struct file *file, const char __user *buf,
3378 			size_t nbytes, loff_t *ppos)
3379 {
3380 	struct lpfc_debug *debug = file->private_data;
3381 	uint32_t ext_map;
3382 	int rc;
3383 
3384 	/* This is a user write operation */
3385 	debug->op = LPFC_IDIAG_OP_WR;
3386 
3387 	rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3388 	if (rc < 0)
3389 		return rc;
3390 
3391 	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
3392 
3393 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
3394 		goto error_out;
3395 	if (rc != LPFC_EXT_ACC_CMD_ARG)
3396 		goto error_out;
3397 	if (!(ext_map & LPFC_EXT_ACC_ALL))
3398 		goto error_out;
3399 
3400 	return nbytes;
3401 error_out:
3402 	/* Clean out command structure on command error out */
3403 	memset(&idiag, 0, sizeof(idiag));
3404 	return -EINVAL;
3405 }
3406 
3407 /**
3408  * lpfc_idiag_extacc_read - idiag debugfs read access to extent information
3409  * @file: The file pointer to read from.
3410  * @buf: The buffer to copy the data to.
3411  * @nbytes: The number of bytes to read.
3412  * @ppos: The position in the file to start reading from.
3413  *
3414  * Description:
3415  * This routine reads data from the proper extent information according to
3416  * the idiag command, and copies to user @buf.
3417  *
3418  * Returns:
3419  * This function returns the amount of data that was read (this could be less
3420  * than @nbytes if the end of the file was reached) or a negative error value.
3421  **/
3422 static ssize_t
3423 lpfc_idiag_extacc_read(struct file *file, char __user *buf, size_t nbytes,
3424 		       loff_t *ppos)
3425 {
3426 	struct lpfc_debug *debug = file->private_data;
3427 	struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3428 	char *pbuffer;
3429 	uint32_t ext_map;
3430 	int len = 0;
3431 
3432 	/* This is a user read operation */
3433 	debug->op = LPFC_IDIAG_OP_RD;
3434 
3435 	if (!debug->buffer)
3436 		debug->buffer = kmalloc(LPFC_EXT_ACC_BUF_SIZE, GFP_KERNEL);
3437 	if (!debug->buffer)
3438 		return 0;
3439 	pbuffer = debug->buffer;
3440 	if (*ppos)
3441 		return 0;
3442 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
3443 		return 0;
3444 
3445 	ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
3446 	if (ext_map & LPFC_EXT_ACC_AVAIL)
3447 		len = lpfc_idiag_extacc_avail_get(phba, pbuffer, len);
3448 	if (ext_map & LPFC_EXT_ACC_ALLOC)
3449 		len = lpfc_idiag_extacc_alloc_get(phba, pbuffer, len);
3450 	if (ext_map & LPFC_EXT_ACC_DRIVR)
3451 		len = lpfc_idiag_extacc_drivr_get(phba, pbuffer, len);
3452 
3453 	return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3454 }
3455 
3456 #undef lpfc_debugfs_op_disc_trc
3457 static const struct file_operations lpfc_debugfs_op_disc_trc = {
3458 	.owner =        THIS_MODULE,
3459 	.open =         lpfc_debugfs_disc_trc_open,
3460 	.llseek =       lpfc_debugfs_lseek,
3461 	.read =         lpfc_debugfs_read,
3462 	.release =      lpfc_debugfs_release,
3463 };
3464 
3465 #undef lpfc_debugfs_op_nodelist
3466 static const struct file_operations lpfc_debugfs_op_nodelist = {
3467 	.owner =        THIS_MODULE,
3468 	.open =         lpfc_debugfs_nodelist_open,
3469 	.llseek =       lpfc_debugfs_lseek,
3470 	.read =         lpfc_debugfs_read,
3471 	.release =      lpfc_debugfs_release,
3472 };
3473 
3474 #undef lpfc_debugfs_op_hbqinfo
3475 static const struct file_operations lpfc_debugfs_op_hbqinfo = {
3476 	.owner =        THIS_MODULE,
3477 	.open =         lpfc_debugfs_hbqinfo_open,
3478 	.llseek =       lpfc_debugfs_lseek,
3479 	.read =         lpfc_debugfs_read,
3480 	.release =      lpfc_debugfs_release,
3481 };
3482 
3483 #undef lpfc_debugfs_op_dumpHBASlim
3484 static const struct file_operations lpfc_debugfs_op_dumpHBASlim = {
3485 	.owner =        THIS_MODULE,
3486 	.open =         lpfc_debugfs_dumpHBASlim_open,
3487 	.llseek =       lpfc_debugfs_lseek,
3488 	.read =         lpfc_debugfs_read,
3489 	.release =      lpfc_debugfs_release,
3490 };
3491 
3492 #undef lpfc_debugfs_op_dumpHostSlim
3493 static const struct file_operations lpfc_debugfs_op_dumpHostSlim = {
3494 	.owner =        THIS_MODULE,
3495 	.open =         lpfc_debugfs_dumpHostSlim_open,
3496 	.llseek =       lpfc_debugfs_lseek,
3497 	.read =         lpfc_debugfs_read,
3498 	.release =      lpfc_debugfs_release,
3499 };
3500 
3501 #undef lpfc_debugfs_op_dumpData
3502 static const struct file_operations lpfc_debugfs_op_dumpData = {
3503 	.owner =        THIS_MODULE,
3504 	.open =         lpfc_debugfs_dumpData_open,
3505 	.llseek =       lpfc_debugfs_lseek,
3506 	.read =         lpfc_debugfs_read,
3507 	.write =	lpfc_debugfs_dumpDataDif_write,
3508 	.release =      lpfc_debugfs_dumpDataDif_release,
3509 };
3510 
3511 #undef lpfc_debugfs_op_dumpDif
3512 static const struct file_operations lpfc_debugfs_op_dumpDif = {
3513 	.owner =        THIS_MODULE,
3514 	.open =         lpfc_debugfs_dumpDif_open,
3515 	.llseek =       lpfc_debugfs_lseek,
3516 	.read =         lpfc_debugfs_read,
3517 	.write =	lpfc_debugfs_dumpDataDif_write,
3518 	.release =      lpfc_debugfs_dumpDataDif_release,
3519 };
3520 
3521 #undef lpfc_debugfs_op_dif_err
3522 static const struct file_operations lpfc_debugfs_op_dif_err = {
3523 	.owner =	THIS_MODULE,
3524 	.open =		lpfc_debugfs_dif_err_open,
3525 	.llseek =	lpfc_debugfs_lseek,
3526 	.read =		lpfc_debugfs_dif_err_read,
3527 	.write =	lpfc_debugfs_dif_err_write,
3528 	.release =	lpfc_debugfs_dif_err_release,
3529 };
3530 
3531 #undef lpfc_debugfs_op_slow_ring_trc
3532 static const struct file_operations lpfc_debugfs_op_slow_ring_trc = {
3533 	.owner =        THIS_MODULE,
3534 	.open =         lpfc_debugfs_slow_ring_trc_open,
3535 	.llseek =       lpfc_debugfs_lseek,
3536 	.read =         lpfc_debugfs_read,
3537 	.release =      lpfc_debugfs_release,
3538 };
3539 
3540 static struct dentry *lpfc_debugfs_root = NULL;
3541 static atomic_t lpfc_debugfs_hba_count;
3542 
3543 /*
3544  * File operations for the iDiag debugfs
3545  */
3546 #undef lpfc_idiag_op_pciCfg
3547 static const struct file_operations lpfc_idiag_op_pciCfg = {
3548 	.owner =        THIS_MODULE,
3549 	.open =         lpfc_idiag_open,
3550 	.llseek =       lpfc_debugfs_lseek,
3551 	.read =         lpfc_idiag_pcicfg_read,
3552 	.write =        lpfc_idiag_pcicfg_write,
3553 	.release =      lpfc_idiag_cmd_release,
3554 };
3555 
3556 #undef lpfc_idiag_op_barAcc
3557 static const struct file_operations lpfc_idiag_op_barAcc = {
3558 	.owner =        THIS_MODULE,
3559 	.open =         lpfc_idiag_open,
3560 	.llseek =       lpfc_debugfs_lseek,
3561 	.read =         lpfc_idiag_baracc_read,
3562 	.write =        lpfc_idiag_baracc_write,
3563 	.release =      lpfc_idiag_cmd_release,
3564 };
3565 
3566 #undef lpfc_idiag_op_queInfo
3567 static const struct file_operations lpfc_idiag_op_queInfo = {
3568 	.owner =        THIS_MODULE,
3569 	.open =         lpfc_idiag_open,
3570 	.read =         lpfc_idiag_queinfo_read,
3571 	.release =      lpfc_idiag_release,
3572 };
3573 
3574 #undef lpfc_idiag_op_queAcc
3575 static const struct file_operations lpfc_idiag_op_queAcc = {
3576 	.owner =        THIS_MODULE,
3577 	.open =         lpfc_idiag_open,
3578 	.llseek =       lpfc_debugfs_lseek,
3579 	.read =         lpfc_idiag_queacc_read,
3580 	.write =        lpfc_idiag_queacc_write,
3581 	.release =      lpfc_idiag_cmd_release,
3582 };
3583 
3584 #undef lpfc_idiag_op_drbAcc
3585 static const struct file_operations lpfc_idiag_op_drbAcc = {
3586 	.owner =        THIS_MODULE,
3587 	.open =         lpfc_idiag_open,
3588 	.llseek =       lpfc_debugfs_lseek,
3589 	.read =         lpfc_idiag_drbacc_read,
3590 	.write =        lpfc_idiag_drbacc_write,
3591 	.release =      lpfc_idiag_cmd_release,
3592 };
3593 
3594 #undef lpfc_idiag_op_ctlAcc
3595 static const struct file_operations lpfc_idiag_op_ctlAcc = {
3596 	.owner =        THIS_MODULE,
3597 	.open =         lpfc_idiag_open,
3598 	.llseek =       lpfc_debugfs_lseek,
3599 	.read =         lpfc_idiag_ctlacc_read,
3600 	.write =        lpfc_idiag_ctlacc_write,
3601 	.release =      lpfc_idiag_cmd_release,
3602 };
3603 
3604 #undef lpfc_idiag_op_mbxAcc
3605 static const struct file_operations lpfc_idiag_op_mbxAcc = {
3606 	.owner =        THIS_MODULE,
3607 	.open =         lpfc_idiag_open,
3608 	.llseek =       lpfc_debugfs_lseek,
3609 	.read =         lpfc_idiag_mbxacc_read,
3610 	.write =        lpfc_idiag_mbxacc_write,
3611 	.release =      lpfc_idiag_cmd_release,
3612 };
3613 
3614 #undef lpfc_idiag_op_extAcc
3615 static const struct file_operations lpfc_idiag_op_extAcc = {
3616 	.owner =        THIS_MODULE,
3617 	.open =         lpfc_idiag_open,
3618 	.llseek =       lpfc_debugfs_lseek,
3619 	.read =         lpfc_idiag_extacc_read,
3620 	.write =        lpfc_idiag_extacc_write,
3621 	.release =      lpfc_idiag_cmd_release,
3622 };
3623 
3624 #endif
3625 
3626 /* lpfc_idiag_mbxacc_dump_bsg_mbox - idiag debugfs dump bsg mailbox command
3627  * @phba: Pointer to HBA context object.
3628  * @dmabuf: Pointer to a DMA buffer descriptor.
3629  *
3630  * Description:
3631  * This routine dump a bsg pass-through non-embedded mailbox command with
3632  * external buffer.
3633  **/
3634 void
3635 lpfc_idiag_mbxacc_dump_bsg_mbox(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3636 				enum mbox_type mbox_tp, enum dma_type dma_tp,
3637 				enum sta_type sta_tp,
3638 				struct lpfc_dmabuf *dmabuf, uint32_t ext_buf)
3639 {
3640 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3641 	uint32_t *mbx_mbox_cmd, *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt;
3642 	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
3643 	int len = 0;
3644 	uint32_t do_dump = 0;
3645 	uint32_t *pword;
3646 	uint32_t i;
3647 
3648 	if (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP)
3649 		return;
3650 
3651 	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
3652 	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
3653 	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
3654 	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
3655 
3656 	if (!(*mbx_dump_map & LPFC_MBX_DMP_ALL) ||
3657 	    (*mbx_dump_cnt == 0) ||
3658 	    (*mbx_word_cnt == 0))
3659 		return;
3660 
3661 	if (*mbx_mbox_cmd != 0x9B)
3662 		return;
3663 
3664 	if ((mbox_tp == mbox_rd) && (dma_tp == dma_mbox)) {
3665 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_MBX) {
3666 			do_dump |= LPFC_BSG_DMP_MBX_RD_MBX;
3667 			printk(KERN_ERR "\nRead mbox command (x%x), "
3668 			       "nemb:0x%x, extbuf_cnt:%d:\n",
3669 			       sta_tp, nemb_tp, ext_buf);
3670 		}
3671 	}
3672 	if ((mbox_tp == mbox_rd) && (dma_tp == dma_ebuf)) {
3673 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_BUF) {
3674 			do_dump |= LPFC_BSG_DMP_MBX_RD_BUF;
3675 			printk(KERN_ERR "\nRead mbox buffer (x%x), "
3676 			       "nemb:0x%x, extbuf_seq:%d:\n",
3677 			       sta_tp, nemb_tp, ext_buf);
3678 		}
3679 	}
3680 	if ((mbox_tp == mbox_wr) && (dma_tp == dma_mbox)) {
3681 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_MBX) {
3682 			do_dump |= LPFC_BSG_DMP_MBX_WR_MBX;
3683 			printk(KERN_ERR "\nWrite mbox command (x%x), "
3684 			       "nemb:0x%x, extbuf_cnt:%d:\n",
3685 			       sta_tp, nemb_tp, ext_buf);
3686 		}
3687 	}
3688 	if ((mbox_tp == mbox_wr) && (dma_tp == dma_ebuf)) {
3689 		if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_BUF) {
3690 			do_dump |= LPFC_BSG_DMP_MBX_WR_BUF;
3691 			printk(KERN_ERR "\nWrite mbox buffer (x%x), "
3692 			       "nemb:0x%x, extbuf_seq:%d:\n",
3693 			       sta_tp, nemb_tp, ext_buf);
3694 		}
3695 	}
3696 
3697 	/* dump buffer content */
3698 	if (do_dump) {
3699 		pword = (uint32_t *)dmabuf->virt;
3700 		for (i = 0; i < *mbx_word_cnt; i++) {
3701 			if (!(i % 8)) {
3702 				if (i != 0)
3703 					printk(KERN_ERR "%s\n", line_buf);
3704 				len = 0;
3705 				len += snprintf(line_buf+len,
3706 						LPFC_MBX_ACC_LBUF_SZ-len,
3707 						"%03d: ", i);
3708 			}
3709 			len += snprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
3710 					"%08x ", (uint32_t)*pword);
3711 			pword++;
3712 		}
3713 		if ((i - 1) % 8)
3714 			printk(KERN_ERR "%s\n", line_buf);
3715 		(*mbx_dump_cnt)--;
3716 	}
3717 
3718 	/* Clean out command structure on reaching dump count */
3719 	if (*mbx_dump_cnt == 0)
3720 		memset(&idiag, 0, sizeof(idiag));
3721 	return;
3722 #endif
3723 }
3724 
3725 /* lpfc_idiag_mbxacc_dump_issue_mbox - idiag debugfs dump issue mailbox command
3726  * @phba: Pointer to HBA context object.
3727  * @dmabuf: Pointer to a DMA buffer descriptor.
3728  *
3729  * Description:
3730  * This routine dump a pass-through non-embedded mailbox command from issue
3731  * mailbox command.
3732  **/
3733 void
3734 lpfc_idiag_mbxacc_dump_issue_mbox(struct lpfc_hba *phba, MAILBOX_t *pmbox)
3735 {
3736 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3737 	uint32_t *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt, *mbx_mbox_cmd;
3738 	char line_buf[LPFC_MBX_ACC_LBUF_SZ];
3739 	int len = 0;
3740 	uint32_t *pword;
3741 	uint8_t *pbyte;
3742 	uint32_t i, j;
3743 
3744 	if (idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP)
3745 		return;
3746 
3747 	mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
3748 	mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
3749 	mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
3750 	mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
3751 
3752 	if (!(*mbx_dump_map & LPFC_MBX_DMP_MBX_ALL) ||
3753 	    (*mbx_dump_cnt == 0) ||
3754 	    (*mbx_word_cnt == 0))
3755 		return;
3756 
3757 	if ((*mbx_mbox_cmd != LPFC_MBX_ALL_CMD) &&
3758 	    (*mbx_mbox_cmd != pmbox->mbxCommand))
3759 		return;
3760 
3761 	/* dump buffer content */
3762 	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_WORD) {
3763 		printk(KERN_ERR "Mailbox command:0x%x dump by word:\n",
3764 		       pmbox->mbxCommand);
3765 		pword = (uint32_t *)pmbox;
3766 		for (i = 0; i < *mbx_word_cnt; i++) {
3767 			if (!(i % 8)) {
3768 				if (i != 0)
3769 					printk(KERN_ERR "%s\n", line_buf);
3770 				len = 0;
3771 				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
3772 				len += snprintf(line_buf+len,
3773 						LPFC_MBX_ACC_LBUF_SZ-len,
3774 						"%03d: ", i);
3775 			}
3776 			len += snprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
3777 					"%08x ",
3778 					((uint32_t)*pword) & 0xffffffff);
3779 			pword++;
3780 		}
3781 		if ((i - 1) % 8)
3782 			printk(KERN_ERR "%s\n", line_buf);
3783 		printk(KERN_ERR "\n");
3784 	}
3785 	if (*mbx_dump_map & LPFC_MBX_DMP_MBX_BYTE) {
3786 		printk(KERN_ERR "Mailbox command:0x%x dump by byte:\n",
3787 		       pmbox->mbxCommand);
3788 		pbyte = (uint8_t *)pmbox;
3789 		for (i = 0; i < *mbx_word_cnt; i++) {
3790 			if (!(i % 8)) {
3791 				if (i != 0)
3792 					printk(KERN_ERR "%s\n", line_buf);
3793 				len = 0;
3794 				memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
3795 				len += snprintf(line_buf+len,
3796 						LPFC_MBX_ACC_LBUF_SZ-len,
3797 						"%03d: ", i);
3798 			}
3799 			for (j = 0; j < 4; j++) {
3800 				len += snprintf(line_buf+len,
3801 						LPFC_MBX_ACC_LBUF_SZ-len,
3802 						"%02x",
3803 						((uint8_t)*pbyte) & 0xff);
3804 				pbyte++;
3805 			}
3806 			len += snprintf(line_buf+len,
3807 					LPFC_MBX_ACC_LBUF_SZ-len, " ");
3808 		}
3809 		if ((i - 1) % 8)
3810 			printk(KERN_ERR "%s\n", line_buf);
3811 		printk(KERN_ERR "\n");
3812 	}
3813 	(*mbx_dump_cnt)--;
3814 
3815 	/* Clean out command structure on reaching dump count */
3816 	if (*mbx_dump_cnt == 0)
3817 		memset(&idiag, 0, sizeof(idiag));
3818 	return;
3819 #endif
3820 }
3821 
3822 /**
3823  * lpfc_debugfs_initialize - Initialize debugfs for a vport
3824  * @vport: The vport pointer to initialize.
3825  *
3826  * Description:
3827  * When Debugfs is configured this routine sets up the lpfc debugfs file system.
3828  * If not already created, this routine will create the lpfc directory, and
3829  * lpfcX directory (for this HBA), and vportX directory for this vport. It will
3830  * also create each file used to access lpfc specific debugfs information.
3831  **/
3832 inline void
3833 lpfc_debugfs_initialize(struct lpfc_vport *vport)
3834 {
3835 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3836 	struct lpfc_hba   *phba = vport->phba;
3837 	char name[64];
3838 	uint32_t num, i;
3839 
3840 	if (!lpfc_debugfs_enable)
3841 		return;
3842 
3843 	/* Setup lpfc root directory */
3844 	if (!lpfc_debugfs_root) {
3845 		lpfc_debugfs_root = debugfs_create_dir("lpfc", NULL);
3846 		atomic_set(&lpfc_debugfs_hba_count, 0);
3847 		if (!lpfc_debugfs_root) {
3848 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3849 					 "0408 Cannot create debugfs root\n");
3850 			goto debug_failed;
3851 		}
3852 	}
3853 	if (!lpfc_debugfs_start_time)
3854 		lpfc_debugfs_start_time = jiffies;
3855 
3856 	/* Setup funcX directory for specific HBA PCI function */
3857 	snprintf(name, sizeof(name), "fn%d", phba->brd_no);
3858 	if (!phba->hba_debugfs_root) {
3859 		phba->hba_debugfs_root =
3860 			debugfs_create_dir(name, lpfc_debugfs_root);
3861 		if (!phba->hba_debugfs_root) {
3862 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3863 					 "0412 Cannot create debugfs hba\n");
3864 			goto debug_failed;
3865 		}
3866 		atomic_inc(&lpfc_debugfs_hba_count);
3867 		atomic_set(&phba->debugfs_vport_count, 0);
3868 
3869 		/* Setup hbqinfo */
3870 		snprintf(name, sizeof(name), "hbqinfo");
3871 		phba->debug_hbqinfo =
3872 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3873 				 phba->hba_debugfs_root,
3874 				 phba, &lpfc_debugfs_op_hbqinfo);
3875 		if (!phba->debug_hbqinfo) {
3876 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3877 				"0411 Cannot create debugfs hbqinfo\n");
3878 			goto debug_failed;
3879 		}
3880 
3881 		/* Setup dumpHBASlim */
3882 		if (phba->sli_rev < LPFC_SLI_REV4) {
3883 			snprintf(name, sizeof(name), "dumpHBASlim");
3884 			phba->debug_dumpHBASlim =
3885 				debugfs_create_file(name,
3886 					S_IFREG|S_IRUGO|S_IWUSR,
3887 					phba->hba_debugfs_root,
3888 					phba, &lpfc_debugfs_op_dumpHBASlim);
3889 			if (!phba->debug_dumpHBASlim) {
3890 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3891 						 "0413 Cannot create debugfs "
3892 						"dumpHBASlim\n");
3893 				goto debug_failed;
3894 			}
3895 		} else
3896 			phba->debug_dumpHBASlim = NULL;
3897 
3898 		/* Setup dumpHostSlim */
3899 		if (phba->sli_rev < LPFC_SLI_REV4) {
3900 			snprintf(name, sizeof(name), "dumpHostSlim");
3901 			phba->debug_dumpHostSlim =
3902 				debugfs_create_file(name,
3903 					S_IFREG|S_IRUGO|S_IWUSR,
3904 					phba->hba_debugfs_root,
3905 					phba, &lpfc_debugfs_op_dumpHostSlim);
3906 			if (!phba->debug_dumpHostSlim) {
3907 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3908 						 "0414 Cannot create debugfs "
3909 						 "dumpHostSlim\n");
3910 				goto debug_failed;
3911 			}
3912 		} else
3913 			phba->debug_dumpHBASlim = NULL;
3914 
3915 		/* Setup dumpData */
3916 		snprintf(name, sizeof(name), "dumpData");
3917 		phba->debug_dumpData =
3918 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3919 				 phba->hba_debugfs_root,
3920 				 phba, &lpfc_debugfs_op_dumpData);
3921 		if (!phba->debug_dumpData) {
3922 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3923 				"0800 Cannot create debugfs dumpData\n");
3924 			goto debug_failed;
3925 		}
3926 
3927 		/* Setup dumpDif */
3928 		snprintf(name, sizeof(name), "dumpDif");
3929 		phba->debug_dumpDif =
3930 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3931 				 phba->hba_debugfs_root,
3932 				 phba, &lpfc_debugfs_op_dumpDif);
3933 		if (!phba->debug_dumpDif) {
3934 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3935 				"0801 Cannot create debugfs dumpDif\n");
3936 			goto debug_failed;
3937 		}
3938 
3939 		/* Setup DIF Error Injections */
3940 		snprintf(name, sizeof(name), "InjErrLBA");
3941 		phba->debug_InjErrLBA =
3942 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3943 			phba->hba_debugfs_root,
3944 			phba, &lpfc_debugfs_op_dif_err);
3945 		if (!phba->debug_InjErrLBA) {
3946 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3947 				"0807 Cannot create debugfs InjErrLBA\n");
3948 			goto debug_failed;
3949 		}
3950 		phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
3951 
3952 		snprintf(name, sizeof(name), "writeGuardInjErr");
3953 		phba->debug_writeGuard =
3954 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3955 			phba->hba_debugfs_root,
3956 			phba, &lpfc_debugfs_op_dif_err);
3957 		if (!phba->debug_writeGuard) {
3958 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3959 				"0802 Cannot create debugfs writeGuard\n");
3960 			goto debug_failed;
3961 		}
3962 
3963 		snprintf(name, sizeof(name), "writeAppInjErr");
3964 		phba->debug_writeApp =
3965 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3966 			phba->hba_debugfs_root,
3967 			phba, &lpfc_debugfs_op_dif_err);
3968 		if (!phba->debug_writeApp) {
3969 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3970 				"0803 Cannot create debugfs writeApp\n");
3971 			goto debug_failed;
3972 		}
3973 
3974 		snprintf(name, sizeof(name), "writeRefInjErr");
3975 		phba->debug_writeRef =
3976 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3977 			phba->hba_debugfs_root,
3978 			phba, &lpfc_debugfs_op_dif_err);
3979 		if (!phba->debug_writeRef) {
3980 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3981 				"0804 Cannot create debugfs writeRef\n");
3982 			goto debug_failed;
3983 		}
3984 
3985 		snprintf(name, sizeof(name), "readGuardInjErr");
3986 		phba->debug_readGuard =
3987 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3988 			phba->hba_debugfs_root,
3989 			phba, &lpfc_debugfs_op_dif_err);
3990 		if (!phba->debug_readGuard) {
3991 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3992 				"0808 Cannot create debugfs readGuard\n");
3993 			goto debug_failed;
3994 		}
3995 
3996 		snprintf(name, sizeof(name), "readAppInjErr");
3997 		phba->debug_readApp =
3998 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
3999 			phba->hba_debugfs_root,
4000 			phba, &lpfc_debugfs_op_dif_err);
4001 		if (!phba->debug_readApp) {
4002 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4003 				"0805 Cannot create debugfs readApp\n");
4004 			goto debug_failed;
4005 		}
4006 
4007 		snprintf(name, sizeof(name), "readRefInjErr");
4008 		phba->debug_readRef =
4009 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4010 			phba->hba_debugfs_root,
4011 			phba, &lpfc_debugfs_op_dif_err);
4012 		if (!phba->debug_readRef) {
4013 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4014 				"0806 Cannot create debugfs readApp\n");
4015 			goto debug_failed;
4016 		}
4017 
4018 		/* Setup slow ring trace */
4019 		if (lpfc_debugfs_max_slow_ring_trc) {
4020 			num = lpfc_debugfs_max_slow_ring_trc - 1;
4021 			if (num & lpfc_debugfs_max_slow_ring_trc) {
4022 				/* Change to be a power of 2 */
4023 				num = lpfc_debugfs_max_slow_ring_trc;
4024 				i = 0;
4025 				while (num > 1) {
4026 					num = num >> 1;
4027 					i++;
4028 				}
4029 				lpfc_debugfs_max_slow_ring_trc = (1 << i);
4030 				printk(KERN_ERR
4031 				       "lpfc_debugfs_max_disc_trc changed to "
4032 				       "%d\n", lpfc_debugfs_max_disc_trc);
4033 			}
4034 		}
4035 
4036 		snprintf(name, sizeof(name), "slow_ring_trace");
4037 		phba->debug_slow_ring_trc =
4038 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4039 				 phba->hba_debugfs_root,
4040 				 phba, &lpfc_debugfs_op_slow_ring_trc);
4041 		if (!phba->debug_slow_ring_trc) {
4042 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4043 					 "0415 Cannot create debugfs "
4044 					 "slow_ring_trace\n");
4045 			goto debug_failed;
4046 		}
4047 		if (!phba->slow_ring_trc) {
4048 			phba->slow_ring_trc = kmalloc(
4049 				(sizeof(struct lpfc_debugfs_trc) *
4050 				lpfc_debugfs_max_slow_ring_trc),
4051 				GFP_KERNEL);
4052 			if (!phba->slow_ring_trc) {
4053 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4054 						 "0416 Cannot create debugfs "
4055 						 "slow_ring buffer\n");
4056 				goto debug_failed;
4057 			}
4058 			atomic_set(&phba->slow_ring_trc_cnt, 0);
4059 			memset(phba->slow_ring_trc, 0,
4060 				(sizeof(struct lpfc_debugfs_trc) *
4061 				lpfc_debugfs_max_slow_ring_trc));
4062 		}
4063 	}
4064 
4065 	snprintf(name, sizeof(name), "vport%d", vport->vpi);
4066 	if (!vport->vport_debugfs_root) {
4067 		vport->vport_debugfs_root =
4068 			debugfs_create_dir(name, phba->hba_debugfs_root);
4069 		if (!vport->vport_debugfs_root) {
4070 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4071 					 "0417 Can't create debugfs\n");
4072 			goto debug_failed;
4073 		}
4074 		atomic_inc(&phba->debugfs_vport_count);
4075 	}
4076 
4077 	if (lpfc_debugfs_max_disc_trc) {
4078 		num = lpfc_debugfs_max_disc_trc - 1;
4079 		if (num & lpfc_debugfs_max_disc_trc) {
4080 			/* Change to be a power of 2 */
4081 			num = lpfc_debugfs_max_disc_trc;
4082 			i = 0;
4083 			while (num > 1) {
4084 				num = num >> 1;
4085 				i++;
4086 			}
4087 			lpfc_debugfs_max_disc_trc = (1 << i);
4088 			printk(KERN_ERR
4089 			       "lpfc_debugfs_max_disc_trc changed to %d\n",
4090 			       lpfc_debugfs_max_disc_trc);
4091 		}
4092 	}
4093 
4094 	vport->disc_trc = kzalloc(
4095 		(sizeof(struct lpfc_debugfs_trc) * lpfc_debugfs_max_disc_trc),
4096 		GFP_KERNEL);
4097 
4098 	if (!vport->disc_trc) {
4099 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4100 				 "0418 Cannot create debugfs disc trace "
4101 				 "buffer\n");
4102 		goto debug_failed;
4103 	}
4104 	atomic_set(&vport->disc_trc_cnt, 0);
4105 
4106 	snprintf(name, sizeof(name), "discovery_trace");
4107 	vport->debug_disc_trc =
4108 		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4109 				 vport->vport_debugfs_root,
4110 				 vport, &lpfc_debugfs_op_disc_trc);
4111 	if (!vport->debug_disc_trc) {
4112 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4113 				 "0419 Cannot create debugfs "
4114 				 "discovery_trace\n");
4115 		goto debug_failed;
4116 	}
4117 	snprintf(name, sizeof(name), "nodelist");
4118 	vport->debug_nodelist =
4119 		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4120 				 vport->vport_debugfs_root,
4121 				 vport, &lpfc_debugfs_op_nodelist);
4122 	if (!vport->debug_nodelist) {
4123 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4124 				 "2985 Can't create debugfs nodelist\n");
4125 		goto debug_failed;
4126 	}
4127 
4128 	/*
4129 	 * iDiag debugfs root entry points for SLI4 device only
4130 	 */
4131 	if (phba->sli_rev < LPFC_SLI_REV4)
4132 		goto debug_failed;
4133 
4134 	snprintf(name, sizeof(name), "iDiag");
4135 	if (!phba->idiag_root) {
4136 		phba->idiag_root =
4137 			debugfs_create_dir(name, phba->hba_debugfs_root);
4138 		if (!phba->idiag_root) {
4139 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4140 					 "2922 Can't create idiag debugfs\n");
4141 			goto debug_failed;
4142 		}
4143 		/* Initialize iDiag data structure */
4144 		memset(&idiag, 0, sizeof(idiag));
4145 	}
4146 
4147 	/* iDiag read PCI config space */
4148 	snprintf(name, sizeof(name), "pciCfg");
4149 	if (!phba->idiag_pci_cfg) {
4150 		phba->idiag_pci_cfg =
4151 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4152 				phba->idiag_root, phba, &lpfc_idiag_op_pciCfg);
4153 		if (!phba->idiag_pci_cfg) {
4154 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4155 					 "2923 Can't create idiag debugfs\n");
4156 			goto debug_failed;
4157 		}
4158 		idiag.offset.last_rd = 0;
4159 	}
4160 
4161 	/* iDiag PCI BAR access */
4162 	snprintf(name, sizeof(name), "barAcc");
4163 	if (!phba->idiag_bar_acc) {
4164 		phba->idiag_bar_acc =
4165 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4166 				phba->idiag_root, phba, &lpfc_idiag_op_barAcc);
4167 		if (!phba->idiag_bar_acc) {
4168 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4169 					"3056 Can't create idiag debugfs\n");
4170 			goto debug_failed;
4171 		}
4172 		idiag.offset.last_rd = 0;
4173 	}
4174 
4175 	/* iDiag get PCI function queue information */
4176 	snprintf(name, sizeof(name), "queInfo");
4177 	if (!phba->idiag_que_info) {
4178 		phba->idiag_que_info =
4179 			debugfs_create_file(name, S_IFREG|S_IRUGO,
4180 			phba->idiag_root, phba, &lpfc_idiag_op_queInfo);
4181 		if (!phba->idiag_que_info) {
4182 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4183 					 "2924 Can't create idiag debugfs\n");
4184 			goto debug_failed;
4185 		}
4186 	}
4187 
4188 	/* iDiag access PCI function queue */
4189 	snprintf(name, sizeof(name), "queAcc");
4190 	if (!phba->idiag_que_acc) {
4191 		phba->idiag_que_acc =
4192 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4193 				phba->idiag_root, phba, &lpfc_idiag_op_queAcc);
4194 		if (!phba->idiag_que_acc) {
4195 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4196 					 "2926 Can't create idiag debugfs\n");
4197 			goto debug_failed;
4198 		}
4199 	}
4200 
4201 	/* iDiag access PCI function doorbell registers */
4202 	snprintf(name, sizeof(name), "drbAcc");
4203 	if (!phba->idiag_drb_acc) {
4204 		phba->idiag_drb_acc =
4205 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4206 				phba->idiag_root, phba, &lpfc_idiag_op_drbAcc);
4207 		if (!phba->idiag_drb_acc) {
4208 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4209 					 "2927 Can't create idiag debugfs\n");
4210 			goto debug_failed;
4211 		}
4212 	}
4213 
4214 	/* iDiag access PCI function control registers */
4215 	snprintf(name, sizeof(name), "ctlAcc");
4216 	if (!phba->idiag_ctl_acc) {
4217 		phba->idiag_ctl_acc =
4218 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4219 				phba->idiag_root, phba, &lpfc_idiag_op_ctlAcc);
4220 		if (!phba->idiag_ctl_acc) {
4221 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4222 					 "2981 Can't create idiag debugfs\n");
4223 			goto debug_failed;
4224 		}
4225 	}
4226 
4227 	/* iDiag access mbox commands */
4228 	snprintf(name, sizeof(name), "mbxAcc");
4229 	if (!phba->idiag_mbx_acc) {
4230 		phba->idiag_mbx_acc =
4231 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
4232 				phba->idiag_root, phba, &lpfc_idiag_op_mbxAcc);
4233 		if (!phba->idiag_mbx_acc) {
4234 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4235 					"2980 Can't create idiag debugfs\n");
4236 			goto debug_failed;
4237 		}
4238 	}
4239 
4240 	/* iDiag extents access commands */
4241 	if (phba->sli4_hba.extents_in_use) {
4242 		snprintf(name, sizeof(name), "extAcc");
4243 		if (!phba->idiag_ext_acc) {
4244 			phba->idiag_ext_acc =
4245 				debugfs_create_file(name,
4246 						    S_IFREG|S_IRUGO|S_IWUSR,
4247 						    phba->idiag_root, phba,
4248 						    &lpfc_idiag_op_extAcc);
4249 			if (!phba->idiag_ext_acc) {
4250 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
4251 						"2986 Cant create "
4252 						"idiag debugfs\n");
4253 				goto debug_failed;
4254 			}
4255 		}
4256 	}
4257 
4258 debug_failed:
4259 	return;
4260 #endif
4261 }
4262 
4263 /**
4264  * lpfc_debugfs_terminate -  Tear down debugfs infrastructure for this vport
4265  * @vport: The vport pointer to remove from debugfs.
4266  *
4267  * Description:
4268  * When Debugfs is configured this routine removes debugfs file system elements
4269  * that are specific to this vport. It also checks to see if there are any
4270  * users left for the debugfs directories associated with the HBA and driver. If
4271  * this is the last user of the HBA directory or driver directory then it will
4272  * remove those from the debugfs infrastructure as well.
4273  **/
4274 inline void
4275 lpfc_debugfs_terminate(struct lpfc_vport *vport)
4276 {
4277 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4278 	struct lpfc_hba   *phba = vport->phba;
4279 
4280 	if (vport->disc_trc) {
4281 		kfree(vport->disc_trc);
4282 		vport->disc_trc = NULL;
4283 	}
4284 	if (vport->debug_disc_trc) {
4285 		debugfs_remove(vport->debug_disc_trc); /* discovery_trace */
4286 		vport->debug_disc_trc = NULL;
4287 	}
4288 	if (vport->debug_nodelist) {
4289 		debugfs_remove(vport->debug_nodelist); /* nodelist */
4290 		vport->debug_nodelist = NULL;
4291 	}
4292 	if (vport->vport_debugfs_root) {
4293 		debugfs_remove(vport->vport_debugfs_root); /* vportX */
4294 		vport->vport_debugfs_root = NULL;
4295 		atomic_dec(&phba->debugfs_vport_count);
4296 	}
4297 	if (atomic_read(&phba->debugfs_vport_count) == 0) {
4298 
4299 		if (phba->debug_hbqinfo) {
4300 			debugfs_remove(phba->debug_hbqinfo); /* hbqinfo */
4301 			phba->debug_hbqinfo = NULL;
4302 		}
4303 		if (phba->debug_dumpHBASlim) {
4304 			debugfs_remove(phba->debug_dumpHBASlim); /* HBASlim */
4305 			phba->debug_dumpHBASlim = NULL;
4306 		}
4307 		if (phba->debug_dumpHostSlim) {
4308 			debugfs_remove(phba->debug_dumpHostSlim); /* HostSlim */
4309 			phba->debug_dumpHostSlim = NULL;
4310 		}
4311 		if (phba->debug_dumpData) {
4312 			debugfs_remove(phba->debug_dumpData); /* dumpData */
4313 			phba->debug_dumpData = NULL;
4314 		}
4315 
4316 		if (phba->debug_dumpDif) {
4317 			debugfs_remove(phba->debug_dumpDif); /* dumpDif */
4318 			phba->debug_dumpDif = NULL;
4319 		}
4320 		if (phba->debug_InjErrLBA) {
4321 			debugfs_remove(phba->debug_InjErrLBA); /* InjErrLBA */
4322 			phba->debug_InjErrLBA = NULL;
4323 		}
4324 		if (phba->debug_writeGuard) {
4325 			debugfs_remove(phba->debug_writeGuard); /* writeGuard */
4326 			phba->debug_writeGuard = NULL;
4327 		}
4328 		if (phba->debug_writeApp) {
4329 			debugfs_remove(phba->debug_writeApp); /* writeApp */
4330 			phba->debug_writeApp = NULL;
4331 		}
4332 		if (phba->debug_writeRef) {
4333 			debugfs_remove(phba->debug_writeRef); /* writeRef */
4334 			phba->debug_writeRef = NULL;
4335 		}
4336 		if (phba->debug_readGuard) {
4337 			debugfs_remove(phba->debug_readGuard); /* readGuard */
4338 			phba->debug_readGuard = NULL;
4339 		}
4340 		if (phba->debug_readApp) {
4341 			debugfs_remove(phba->debug_readApp); /* readApp */
4342 			phba->debug_readApp = NULL;
4343 		}
4344 		if (phba->debug_readRef) {
4345 			debugfs_remove(phba->debug_readRef); /* readRef */
4346 			phba->debug_readRef = NULL;
4347 		}
4348 
4349 		if (phba->slow_ring_trc) {
4350 			kfree(phba->slow_ring_trc);
4351 			phba->slow_ring_trc = NULL;
4352 		}
4353 		if (phba->debug_slow_ring_trc) {
4354 			/* slow_ring_trace */
4355 			debugfs_remove(phba->debug_slow_ring_trc);
4356 			phba->debug_slow_ring_trc = NULL;
4357 		}
4358 
4359 		/*
4360 		 * iDiag release
4361 		 */
4362 		if (phba->sli_rev == LPFC_SLI_REV4) {
4363 			if (phba->idiag_ext_acc) {
4364 				/* iDiag extAcc */
4365 				debugfs_remove(phba->idiag_ext_acc);
4366 				phba->idiag_ext_acc = NULL;
4367 			}
4368 			if (phba->idiag_mbx_acc) {
4369 				/* iDiag mbxAcc */
4370 				debugfs_remove(phba->idiag_mbx_acc);
4371 				phba->idiag_mbx_acc = NULL;
4372 			}
4373 			if (phba->idiag_ctl_acc) {
4374 				/* iDiag ctlAcc */
4375 				debugfs_remove(phba->idiag_ctl_acc);
4376 				phba->idiag_ctl_acc = NULL;
4377 			}
4378 			if (phba->idiag_drb_acc) {
4379 				/* iDiag drbAcc */
4380 				debugfs_remove(phba->idiag_drb_acc);
4381 				phba->idiag_drb_acc = NULL;
4382 			}
4383 			if (phba->idiag_que_acc) {
4384 				/* iDiag queAcc */
4385 				debugfs_remove(phba->idiag_que_acc);
4386 				phba->idiag_que_acc = NULL;
4387 			}
4388 			if (phba->idiag_que_info) {
4389 				/* iDiag queInfo */
4390 				debugfs_remove(phba->idiag_que_info);
4391 				phba->idiag_que_info = NULL;
4392 			}
4393 			if (phba->idiag_bar_acc) {
4394 				/* iDiag barAcc */
4395 				debugfs_remove(phba->idiag_bar_acc);
4396 				phba->idiag_bar_acc = NULL;
4397 			}
4398 			if (phba->idiag_pci_cfg) {
4399 				/* iDiag pciCfg */
4400 				debugfs_remove(phba->idiag_pci_cfg);
4401 				phba->idiag_pci_cfg = NULL;
4402 			}
4403 
4404 			/* Finally remove the iDiag debugfs root */
4405 			if (phba->idiag_root) {
4406 				/* iDiag root */
4407 				debugfs_remove(phba->idiag_root);
4408 				phba->idiag_root = NULL;
4409 			}
4410 		}
4411 
4412 		if (phba->hba_debugfs_root) {
4413 			debugfs_remove(phba->hba_debugfs_root); /* fnX */
4414 			phba->hba_debugfs_root = NULL;
4415 			atomic_dec(&lpfc_debugfs_hba_count);
4416 		}
4417 
4418 		if (atomic_read(&lpfc_debugfs_hba_count) == 0) {
4419 			debugfs_remove(lpfc_debugfs_root); /* lpfc */
4420 			lpfc_debugfs_root = NULL;
4421 		}
4422 	}
4423 #endif
4424 	return;
4425 }
4426