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