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