xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_debugfs.c (revision 1fa6ac37)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2007-2009 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 
52 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
53 /*
54  * debugfs interface
55  *
56  * To access this interface the user should:
57  * # mount -t debugfs none /sys/kernel/debug
58  *
59  * The lpfc debugfs directory hierarchy is:
60  * lpfc/lpfcX/vportY
61  * where X is the lpfc hba unique_id
62  * where Y is the vport VPI on that hba
63  *
64  * Debugging services available per vport:
65  * discovery_trace
66  * This is an ACSII readable file that contains a trace of the last
67  * lpfc_debugfs_max_disc_trc events that happened on a specific vport.
68  * See lpfc_debugfs.h for different categories of  discovery events.
69  * To enable the discovery trace, the following module parameters must be set:
70  * lpfc_debugfs_enable=1         Turns on lpfc debugfs filesystem support
71  * lpfc_debugfs_max_disc_trc=X   Where X is the event trace depth for
72  *                               EACH vport. X MUST also be a power of 2.
73  * lpfc_debugfs_mask_disc_trc=Y  Where Y is an event mask as defined in
74  *                               lpfc_debugfs.h .
75  *
76  * slow_ring_trace
77  * This is an ACSII readable file that contains a trace of the last
78  * lpfc_debugfs_max_slow_ring_trc events that happened on a specific HBA.
79  * To enable the slow ring trace, the following module parameters must be set:
80  * lpfc_debugfs_enable=1         Turns on lpfc debugfs filesystem support
81  * lpfc_debugfs_max_slow_ring_trc=X   Where X is the event trace depth for
82  *                               the HBA. X MUST also be a power of 2.
83  */
84 static int lpfc_debugfs_enable = 1;
85 module_param(lpfc_debugfs_enable, int, 0);
86 MODULE_PARM_DESC(lpfc_debugfs_enable, "Enable debugfs services");
87 
88 /* This MUST be a power of 2 */
89 static int lpfc_debugfs_max_disc_trc;
90 module_param(lpfc_debugfs_max_disc_trc, int, 0);
91 MODULE_PARM_DESC(lpfc_debugfs_max_disc_trc,
92 	"Set debugfs discovery trace depth");
93 
94 /* This MUST be a power of 2 */
95 static int lpfc_debugfs_max_slow_ring_trc;
96 module_param(lpfc_debugfs_max_slow_ring_trc, int, 0);
97 MODULE_PARM_DESC(lpfc_debugfs_max_slow_ring_trc,
98 	"Set debugfs slow ring trace depth");
99 
100 static int lpfc_debugfs_mask_disc_trc;
101 module_param(lpfc_debugfs_mask_disc_trc, int, 0);
102 MODULE_PARM_DESC(lpfc_debugfs_mask_disc_trc,
103 	"Set debugfs discovery trace mask");
104 
105 #include <linux/debugfs.h>
106 
107 /* size of output line, for discovery_trace and slow_ring_trace */
108 #define LPFC_DEBUG_TRC_ENTRY_SIZE 100
109 
110 /* nodelist output buffer size */
111 #define LPFC_NODELIST_SIZE 8192
112 #define LPFC_NODELIST_ENTRY_SIZE 120
113 
114 /* dumpHBASlim output buffer size */
115 #define LPFC_DUMPHBASLIM_SIZE 4096
116 
117 /* dumpHostSlim output buffer size */
118 #define LPFC_DUMPHOSTSLIM_SIZE 4096
119 
120 /* hbqinfo output buffer size */
121 #define LPFC_HBQINFO_SIZE 8192
122 
123 struct lpfc_debug {
124 	char *buffer;
125 	int  len;
126 };
127 
128 static atomic_t lpfc_debugfs_seq_trc_cnt = ATOMIC_INIT(0);
129 static unsigned long lpfc_debugfs_start_time = 0L;
130 
131 /**
132  * lpfc_debugfs_disc_trc_data - Dump discovery logging to a buffer
133  * @vport: The vport to gather the log info from.
134  * @buf: The buffer to dump log into.
135  * @size: The maximum amount of data to process.
136  *
137  * Description:
138  * This routine gathers the lpfc discovery debugfs data from the @vport and
139  * dumps it to @buf up to @size number of bytes. It will start at the next entry
140  * in the log and process the log until the end of the buffer. Then it will
141  * gather from the beginning of the log and process until the current entry.
142  *
143  * Notes:
144  * Discovery logging will be disabled while while this routine dumps the log.
145  *
146  * Return Value:
147  * This routine returns the amount of bytes that were dumped into @buf and will
148  * not exceed @size.
149  **/
150 static int
151 lpfc_debugfs_disc_trc_data(struct lpfc_vport *vport, char *buf, int size)
152 {
153 	int i, index, len, enable;
154 	uint32_t ms;
155 	struct lpfc_debugfs_trc *dtp;
156 	char buffer[LPFC_DEBUG_TRC_ENTRY_SIZE];
157 
158 	enable = lpfc_debugfs_enable;
159 	lpfc_debugfs_enable = 0;
160 
161 	len = 0;
162 	index = (atomic_read(&vport->disc_trc_cnt) + 1) &
163 		(lpfc_debugfs_max_disc_trc - 1);
164 	for (i = index; i < lpfc_debugfs_max_disc_trc; i++) {
165 		dtp = vport->disc_trc + i;
166 		if (!dtp->fmt)
167 			continue;
168 		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
169 		snprintf(buffer,
170 			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
171 			dtp->seq_cnt, ms, dtp->fmt);
172 		len +=  snprintf(buf+len, size-len, buffer,
173 			dtp->data1, dtp->data2, dtp->data3);
174 	}
175 	for (i = 0; i < index; i++) {
176 		dtp = vport->disc_trc + i;
177 		if (!dtp->fmt)
178 			continue;
179 		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
180 		snprintf(buffer,
181 			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
182 			dtp->seq_cnt, ms, dtp->fmt);
183 		len +=  snprintf(buf+len, size-len, buffer,
184 			dtp->data1, dtp->data2, dtp->data3);
185 	}
186 
187 	lpfc_debugfs_enable = enable;
188 	return len;
189 }
190 
191 /**
192  * lpfc_debugfs_slow_ring_trc_data - Dump slow ring logging to a buffer
193  * @phba: The HBA to gather the log info from.
194  * @buf: The buffer to dump log into.
195  * @size: The maximum amount of data to process.
196  *
197  * Description:
198  * This routine gathers the lpfc slow ring debugfs data from the @phba and
199  * dumps it to @buf up to @size number of bytes. It will start at the next entry
200  * in the log and process the log until the end of the buffer. Then it will
201  * gather from the beginning of the log and process until the current entry.
202  *
203  * Notes:
204  * Slow ring logging will be disabled while while this routine dumps the log.
205  *
206  * Return Value:
207  * This routine returns the amount of bytes that were dumped into @buf and will
208  * not exceed @size.
209  **/
210 static int
211 lpfc_debugfs_slow_ring_trc_data(struct lpfc_hba *phba, char *buf, int size)
212 {
213 	int i, index, len, enable;
214 	uint32_t ms;
215 	struct lpfc_debugfs_trc *dtp;
216 	char buffer[LPFC_DEBUG_TRC_ENTRY_SIZE];
217 
218 
219 	enable = lpfc_debugfs_enable;
220 	lpfc_debugfs_enable = 0;
221 
222 	len = 0;
223 	index = (atomic_read(&phba->slow_ring_trc_cnt) + 1) &
224 		(lpfc_debugfs_max_slow_ring_trc - 1);
225 	for (i = index; i < lpfc_debugfs_max_slow_ring_trc; i++) {
226 		dtp = phba->slow_ring_trc + i;
227 		if (!dtp->fmt)
228 			continue;
229 		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
230 		snprintf(buffer,
231 			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
232 			dtp->seq_cnt, ms, dtp->fmt);
233 		len +=  snprintf(buf+len, size-len, buffer,
234 			dtp->data1, dtp->data2, dtp->data3);
235 	}
236 	for (i = 0; i < index; i++) {
237 		dtp = phba->slow_ring_trc + i;
238 		if (!dtp->fmt)
239 			continue;
240 		ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
241 		snprintf(buffer,
242 			LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
243 			dtp->seq_cnt, ms, dtp->fmt);
244 		len +=  snprintf(buf+len, size-len, buffer,
245 			dtp->data1, dtp->data2, dtp->data3);
246 	}
247 
248 	lpfc_debugfs_enable = enable;
249 	return len;
250 }
251 
252 static int lpfc_debugfs_last_hbq = -1;
253 
254 /**
255  * lpfc_debugfs_hbqinfo_data - Dump host buffer queue info to a buffer
256  * @phba: The HBA to gather host buffer info from.
257  * @buf: The buffer to dump log into.
258  * @size: The maximum amount of data to process.
259  *
260  * Description:
261  * This routine dumps the host buffer queue info from the @phba to @buf up to
262  * @size number of bytes. A header that describes the current hbq state will be
263  * dumped to @buf first and then info on each hbq entry will be dumped to @buf
264  * until @size bytes have been dumped or all the hbq info has been dumped.
265  *
266  * Notes:
267  * This routine will rotate through each configured HBQ each time called.
268  *
269  * Return Value:
270  * This routine returns the amount of bytes that were dumped into @buf and will
271  * not exceed @size.
272  **/
273 static int
274 lpfc_debugfs_hbqinfo_data(struct lpfc_hba *phba, char *buf, int size)
275 {
276 	int len = 0;
277 	int cnt, i, j, found, posted, low;
278 	uint32_t phys, raw_index, getidx;
279 	struct lpfc_hbq_init *hip;
280 	struct hbq_s *hbqs;
281 	struct lpfc_hbq_entry *hbqe;
282 	struct lpfc_dmabuf *d_buf;
283 	struct hbq_dmabuf *hbq_buf;
284 
285 	if (phba->sli_rev != 3)
286 		return 0;
287 	cnt = LPFC_HBQINFO_SIZE;
288 	spin_lock_irq(&phba->hbalock);
289 
290 	/* toggle between multiple hbqs, if any */
291 	i = lpfc_sli_hbq_count();
292 	if (i > 1) {
293 		 lpfc_debugfs_last_hbq++;
294 		 if (lpfc_debugfs_last_hbq >= i)
295 			lpfc_debugfs_last_hbq = 0;
296 	}
297 	else
298 		lpfc_debugfs_last_hbq = 0;
299 
300 	i = lpfc_debugfs_last_hbq;
301 
302 	len +=  snprintf(buf+len, size-len, "HBQ %d Info\n", i);
303 
304 	hbqs =  &phba->hbqs[i];
305 	posted = 0;
306 	list_for_each_entry(d_buf, &hbqs->hbq_buffer_list, list)
307 		posted++;
308 
309 	hip =  lpfc_hbq_defs[i];
310 	len +=  snprintf(buf+len, size-len,
311 		"idx:%d prof:%d rn:%d bufcnt:%d icnt:%d acnt:%d posted %d\n",
312 		hip->hbq_index, hip->profile, hip->rn,
313 		hip->buffer_count, hip->init_count, hip->add_count, posted);
314 
315 	raw_index = phba->hbq_get[i];
316 	getidx = le32_to_cpu(raw_index);
317 	len +=  snprintf(buf+len, size-len,
318 		"entrys:%d bufcnt:%d Put:%d nPut:%d localGet:%d hbaGet:%d\n",
319 		hbqs->entry_count, hbqs->buffer_count, hbqs->hbqPutIdx,
320 		hbqs->next_hbqPutIdx, hbqs->local_hbqGetIdx, getidx);
321 
322 	hbqe = (struct lpfc_hbq_entry *) phba->hbqs[i].hbq_virt;
323 	for (j=0; j<hbqs->entry_count; j++) {
324 		len +=  snprintf(buf+len, size-len,
325 			"%03d: %08x %04x %05x ", j,
326 			le32_to_cpu(hbqe->bde.addrLow),
327 			le32_to_cpu(hbqe->bde.tus.w),
328 			le32_to_cpu(hbqe->buffer_tag));
329 		i = 0;
330 		found = 0;
331 
332 		/* First calculate if slot has an associated posted buffer */
333 		low = hbqs->hbqPutIdx - posted;
334 		if (low >= 0) {
335 			if ((j >= hbqs->hbqPutIdx) || (j < low)) {
336 				len +=  snprintf(buf+len, size-len, "Unused\n");
337 				goto skipit;
338 			}
339 		}
340 		else {
341 			if ((j >= hbqs->hbqPutIdx) &&
342 				(j < (hbqs->entry_count+low))) {
343 				len +=  snprintf(buf+len, size-len, "Unused\n");
344 				goto skipit;
345 			}
346 		}
347 
348 		/* Get the Buffer info for the posted buffer */
349 		list_for_each_entry(d_buf, &hbqs->hbq_buffer_list, list) {
350 			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
351 			phys = ((uint64_t)hbq_buf->dbuf.phys & 0xffffffff);
352 			if (phys == le32_to_cpu(hbqe->bde.addrLow)) {
353 				len +=  snprintf(buf+len, size-len,
354 					"Buf%d: %p %06x\n", i,
355 					hbq_buf->dbuf.virt, hbq_buf->tag);
356 				found = 1;
357 				break;
358 			}
359 			i++;
360 		}
361 		if (!found) {
362 			len +=  snprintf(buf+len, size-len, "No DMAinfo?\n");
363 		}
364 skipit:
365 		hbqe++;
366 		if (len > LPFC_HBQINFO_SIZE - 54)
367 			break;
368 	}
369 	spin_unlock_irq(&phba->hbalock);
370 	return len;
371 }
372 
373 static int lpfc_debugfs_last_hba_slim_off;
374 
375 /**
376  * lpfc_debugfs_dumpHBASlim_data - Dump HBA SLIM info to a buffer
377  * @phba: The HBA to gather SLIM info from.
378  * @buf: The buffer to dump log into.
379  * @size: The maximum amount of data to process.
380  *
381  * Description:
382  * This routine dumps the current contents of HBA SLIM for the HBA associated
383  * with @phba to @buf up to @size bytes of data. This is the raw HBA SLIM data.
384  *
385  * Notes:
386  * This routine will only dump up to 1024 bytes of data each time called and
387  * should be called multiple times to dump the entire HBA SLIM.
388  *
389  * Return Value:
390  * This routine returns the amount of bytes that were dumped into @buf and will
391  * not exceed @size.
392  **/
393 static int
394 lpfc_debugfs_dumpHBASlim_data(struct lpfc_hba *phba, char *buf, int size)
395 {
396 	int len = 0;
397 	int i, off;
398 	uint32_t *ptr;
399 	char buffer[1024];
400 
401 	off = 0;
402 	spin_lock_irq(&phba->hbalock);
403 
404 	len +=  snprintf(buf+len, size-len, "HBA SLIM\n");
405 	lpfc_memcpy_from_slim(buffer,
406 		phba->MBslimaddr + lpfc_debugfs_last_hba_slim_off, 1024);
407 
408 	ptr = (uint32_t *)&buffer[0];
409 	off = lpfc_debugfs_last_hba_slim_off;
410 
411 	/* Set it up for the next time */
412 	lpfc_debugfs_last_hba_slim_off += 1024;
413 	if (lpfc_debugfs_last_hba_slim_off >= 4096)
414 		lpfc_debugfs_last_hba_slim_off = 0;
415 
416 	i = 1024;
417 	while (i > 0) {
418 		len +=  snprintf(buf+len, size-len,
419 		"%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
420 		off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
421 		*(ptr+5), *(ptr+6), *(ptr+7));
422 		ptr += 8;
423 		i -= (8 * sizeof(uint32_t));
424 		off += (8 * sizeof(uint32_t));
425 	}
426 
427 	spin_unlock_irq(&phba->hbalock);
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 boundry */
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 boundry */
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 
1001 /**
1002  * lpfc_debugfs_nodelist_open - Open the nodelist debugfs file
1003  * @inode: The inode pointer that contains a vport pointer.
1004  * @file: The file pointer to attach the log output.
1005  *
1006  * Description:
1007  * This routine is the entry point for the debugfs open file operation. It gets
1008  * the vport from the i_private field in @inode, allocates the necessary buffer
1009  * for the log, fills the buffer from the in-memory log for this vport, and then
1010  * returns a pointer to that log in the private_data field in @file.
1011  *
1012  * Returns:
1013  * This function returns zero if successful. On error it will return an negative
1014  * error value.
1015  **/
1016 static int
1017 lpfc_debugfs_nodelist_open(struct inode *inode, struct file *file)
1018 {
1019 	struct lpfc_vport *vport = inode->i_private;
1020 	struct lpfc_debug *debug;
1021 	int rc = -ENOMEM;
1022 
1023 	debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1024 	if (!debug)
1025 		goto out;
1026 
1027 	/* Round to page boundary */
1028 	debug->buffer = kmalloc(LPFC_NODELIST_SIZE, GFP_KERNEL);
1029 	if (!debug->buffer) {
1030 		kfree(debug);
1031 		goto out;
1032 	}
1033 
1034 	debug->len = lpfc_debugfs_nodelist_data(vport, debug->buffer,
1035 		LPFC_NODELIST_SIZE);
1036 	file->private_data = debug;
1037 
1038 	rc = 0;
1039 out:
1040 	return rc;
1041 }
1042 
1043 /**
1044  * lpfc_debugfs_lseek - Seek through a debugfs file
1045  * @file: The file pointer to seek through.
1046  * @off: The offset to seek to or the amount to seek by.
1047  * @whence: Indicates how to seek.
1048  *
1049  * Description:
1050  * This routine is the entry point for the debugfs lseek file operation. The
1051  * @whence parameter indicates whether @off is the offset to directly seek to,
1052  * or if it is a value to seek forward or reverse by. This function figures out
1053  * what the new offset of the debugfs file will be and assigns that value to the
1054  * f_pos field of @file.
1055  *
1056  * Returns:
1057  * This function returns the new offset if successful and returns a negative
1058  * error if unable to process the seek.
1059  **/
1060 static loff_t
1061 lpfc_debugfs_lseek(struct file *file, loff_t off, int whence)
1062 {
1063 	struct lpfc_debug *debug;
1064 	loff_t pos = -1;
1065 
1066 	debug = file->private_data;
1067 
1068 	switch (whence) {
1069 	case 0:
1070 		pos = off;
1071 		break;
1072 	case 1:
1073 		pos = file->f_pos + off;
1074 		break;
1075 	case 2:
1076 		pos = debug->len - off;
1077 	}
1078 	return (pos < 0 || pos > debug->len) ? -EINVAL : (file->f_pos = pos);
1079 }
1080 
1081 /**
1082  * lpfc_debugfs_read - Read a debugfs file
1083  * @file: The file pointer to read from.
1084  * @buf: The buffer to copy the data to.
1085  * @nbytes: The number of bytes to read.
1086  * @ppos: The position in the file to start reading from.
1087  *
1088  * Description:
1089  * This routine reads data from from the buffer indicated in the private_data
1090  * field of @file. It will start reading at @ppos and copy up to @nbytes of
1091  * data to @buf.
1092  *
1093  * Returns:
1094  * This function returns the amount of data that was read (this could be less
1095  * than @nbytes if the end of the file was reached) or a negative error value.
1096  **/
1097 static ssize_t
1098 lpfc_debugfs_read(struct file *file, char __user *buf,
1099 		  size_t nbytes, loff_t *ppos)
1100 {
1101 	struct lpfc_debug *debug = file->private_data;
1102 	return simple_read_from_buffer(buf, nbytes, ppos, debug->buffer,
1103 				       debug->len);
1104 }
1105 
1106 /**
1107  * lpfc_debugfs_release - Release the buffer used to store debugfs file data
1108  * @inode: The inode pointer that contains a vport pointer. (unused)
1109  * @file: The file pointer that contains the buffer to release.
1110  *
1111  * Description:
1112  * This routine frees the buffer that was allocated when the debugfs file was
1113  * opened.
1114  *
1115  * Returns:
1116  * This function returns zero.
1117  **/
1118 static int
1119 lpfc_debugfs_release(struct inode *inode, struct file *file)
1120 {
1121 	struct lpfc_debug *debug = file->private_data;
1122 
1123 	kfree(debug->buffer);
1124 	kfree(debug);
1125 
1126 	return 0;
1127 }
1128 
1129 static int
1130 lpfc_debugfs_dumpDataDif_release(struct inode *inode, struct file *file)
1131 {
1132 	struct lpfc_debug *debug = file->private_data;
1133 
1134 	debug->buffer = NULL;
1135 	kfree(debug);
1136 
1137 	return 0;
1138 }
1139 
1140 #undef lpfc_debugfs_op_disc_trc
1141 static const struct file_operations lpfc_debugfs_op_disc_trc = {
1142 	.owner =        THIS_MODULE,
1143 	.open =         lpfc_debugfs_disc_trc_open,
1144 	.llseek =       lpfc_debugfs_lseek,
1145 	.read =         lpfc_debugfs_read,
1146 	.release =      lpfc_debugfs_release,
1147 };
1148 
1149 #undef lpfc_debugfs_op_nodelist
1150 static const struct file_operations lpfc_debugfs_op_nodelist = {
1151 	.owner =        THIS_MODULE,
1152 	.open =         lpfc_debugfs_nodelist_open,
1153 	.llseek =       lpfc_debugfs_lseek,
1154 	.read =         lpfc_debugfs_read,
1155 	.release =      lpfc_debugfs_release,
1156 };
1157 
1158 #undef lpfc_debugfs_op_hbqinfo
1159 static const struct file_operations lpfc_debugfs_op_hbqinfo = {
1160 	.owner =        THIS_MODULE,
1161 	.open =         lpfc_debugfs_hbqinfo_open,
1162 	.llseek =       lpfc_debugfs_lseek,
1163 	.read =         lpfc_debugfs_read,
1164 	.release =      lpfc_debugfs_release,
1165 };
1166 
1167 #undef lpfc_debugfs_op_dumpHBASlim
1168 static const struct file_operations lpfc_debugfs_op_dumpHBASlim = {
1169 	.owner =        THIS_MODULE,
1170 	.open =         lpfc_debugfs_dumpHBASlim_open,
1171 	.llseek =       lpfc_debugfs_lseek,
1172 	.read =         lpfc_debugfs_read,
1173 	.release =      lpfc_debugfs_release,
1174 };
1175 
1176 #undef lpfc_debugfs_op_dumpHostSlim
1177 static const struct file_operations lpfc_debugfs_op_dumpHostSlim = {
1178 	.owner =        THIS_MODULE,
1179 	.open =         lpfc_debugfs_dumpHostSlim_open,
1180 	.llseek =       lpfc_debugfs_lseek,
1181 	.read =         lpfc_debugfs_read,
1182 	.release =      lpfc_debugfs_release,
1183 };
1184 
1185 #undef lpfc_debugfs_op_dumpData
1186 static const struct file_operations lpfc_debugfs_op_dumpData = {
1187 	.owner =        THIS_MODULE,
1188 	.open =         lpfc_debugfs_dumpData_open,
1189 	.llseek =       lpfc_debugfs_lseek,
1190 	.read =         lpfc_debugfs_read,
1191 	.write =	lpfc_debugfs_dumpDataDif_write,
1192 	.release =      lpfc_debugfs_dumpDataDif_release,
1193 };
1194 
1195 #undef lpfc_debugfs_op_dumpDif
1196 static const struct file_operations lpfc_debugfs_op_dumpDif = {
1197 	.owner =        THIS_MODULE,
1198 	.open =         lpfc_debugfs_dumpDif_open,
1199 	.llseek =       lpfc_debugfs_lseek,
1200 	.read =         lpfc_debugfs_read,
1201 	.write =	lpfc_debugfs_dumpDataDif_write,
1202 	.release =      lpfc_debugfs_dumpDataDif_release,
1203 };
1204 
1205 #undef lpfc_debugfs_op_slow_ring_trc
1206 static const struct file_operations lpfc_debugfs_op_slow_ring_trc = {
1207 	.owner =        THIS_MODULE,
1208 	.open =         lpfc_debugfs_slow_ring_trc_open,
1209 	.llseek =       lpfc_debugfs_lseek,
1210 	.read =         lpfc_debugfs_read,
1211 	.release =      lpfc_debugfs_release,
1212 };
1213 
1214 static struct dentry *lpfc_debugfs_root = NULL;
1215 static atomic_t lpfc_debugfs_hba_count;
1216 #endif
1217 
1218 /**
1219  * lpfc_debugfs_initialize - Initialize debugfs for a vport
1220  * @vport: The vport pointer to initialize.
1221  *
1222  * Description:
1223  * When Debugfs is configured this routine sets up the lpfc debugfs file system.
1224  * If not already created, this routine will create the lpfc directory, and
1225  * lpfcX directory (for this HBA), and vportX directory for this vport. It will
1226  * also create each file used to access lpfc specific debugfs information.
1227  **/
1228 inline void
1229 lpfc_debugfs_initialize(struct lpfc_vport *vport)
1230 {
1231 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1232 	struct lpfc_hba   *phba = vport->phba;
1233 	char name[64];
1234 	uint32_t num, i;
1235 
1236 	if (!lpfc_debugfs_enable)
1237 		return;
1238 
1239 	/* Setup lpfc root directory */
1240 	if (!lpfc_debugfs_root) {
1241 		lpfc_debugfs_root = debugfs_create_dir("lpfc", NULL);
1242 		atomic_set(&lpfc_debugfs_hba_count, 0);
1243 		if (!lpfc_debugfs_root) {
1244 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1245 					 "0408 Cannot create debugfs root\n");
1246 			goto debug_failed;
1247 		}
1248 	}
1249 	if (!lpfc_debugfs_start_time)
1250 		lpfc_debugfs_start_time = jiffies;
1251 
1252 	/* Setup lpfcX directory for specific HBA */
1253 	snprintf(name, sizeof(name), "lpfc%d", phba->brd_no);
1254 	if (!phba->hba_debugfs_root) {
1255 		phba->hba_debugfs_root =
1256 			debugfs_create_dir(name, lpfc_debugfs_root);
1257 		if (!phba->hba_debugfs_root) {
1258 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1259 					 "0412 Cannot create debugfs hba\n");
1260 			goto debug_failed;
1261 		}
1262 		atomic_inc(&lpfc_debugfs_hba_count);
1263 		atomic_set(&phba->debugfs_vport_count, 0);
1264 
1265 		/* Setup hbqinfo */
1266 		snprintf(name, sizeof(name), "hbqinfo");
1267 		phba->debug_hbqinfo =
1268 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
1269 				 phba->hba_debugfs_root,
1270 				 phba, &lpfc_debugfs_op_hbqinfo);
1271 		if (!phba->debug_hbqinfo) {
1272 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1273 				"0411 Cannot create debugfs hbqinfo\n");
1274 			goto debug_failed;
1275 		}
1276 
1277 		/* Setup dumpHBASlim */
1278 		snprintf(name, sizeof(name), "dumpHBASlim");
1279 		phba->debug_dumpHBASlim =
1280 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
1281 				 phba->hba_debugfs_root,
1282 				 phba, &lpfc_debugfs_op_dumpHBASlim);
1283 		if (!phba->debug_dumpHBASlim) {
1284 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1285 				"0413 Cannot create debugfs dumpHBASlim\n");
1286 			goto debug_failed;
1287 		}
1288 
1289 		/* Setup dumpHostSlim */
1290 		snprintf(name, sizeof(name), "dumpHostSlim");
1291 		phba->debug_dumpHostSlim =
1292 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
1293 				 phba->hba_debugfs_root,
1294 				 phba, &lpfc_debugfs_op_dumpHostSlim);
1295 		if (!phba->debug_dumpHostSlim) {
1296 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1297 				"0414 Cannot create debugfs dumpHostSlim\n");
1298 			goto debug_failed;
1299 		}
1300 
1301 		/* Setup dumpData */
1302 		snprintf(name, sizeof(name), "dumpData");
1303 		phba->debug_dumpData =
1304 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
1305 				 phba->hba_debugfs_root,
1306 				 phba, &lpfc_debugfs_op_dumpData);
1307 		if (!phba->debug_dumpData) {
1308 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1309 				"0800 Cannot create debugfs dumpData\n");
1310 			goto debug_failed;
1311 		}
1312 
1313 		/* Setup dumpDif */
1314 		snprintf(name, sizeof(name), "dumpDif");
1315 		phba->debug_dumpDif =
1316 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
1317 				 phba->hba_debugfs_root,
1318 				 phba, &lpfc_debugfs_op_dumpDif);
1319 		if (!phba->debug_dumpDif) {
1320 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1321 				"0801 Cannot create debugfs dumpDif\n");
1322 			goto debug_failed;
1323 		}
1324 
1325 
1326 
1327 		/* Setup slow ring trace */
1328 		if (lpfc_debugfs_max_slow_ring_trc) {
1329 			num = lpfc_debugfs_max_slow_ring_trc - 1;
1330 			if (num & lpfc_debugfs_max_slow_ring_trc) {
1331 				/* Change to be a power of 2 */
1332 				num = lpfc_debugfs_max_slow_ring_trc;
1333 				i = 0;
1334 				while (num > 1) {
1335 					num = num >> 1;
1336 					i++;
1337 				}
1338 				lpfc_debugfs_max_slow_ring_trc = (1 << i);
1339 				printk(KERN_ERR
1340 				       "lpfc_debugfs_max_disc_trc changed to "
1341 				       "%d\n", lpfc_debugfs_max_disc_trc);
1342 			}
1343 		}
1344 
1345 
1346 		snprintf(name, sizeof(name), "slow_ring_trace");
1347 		phba->debug_slow_ring_trc =
1348 			debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
1349 				 phba->hba_debugfs_root,
1350 				 phba, &lpfc_debugfs_op_slow_ring_trc);
1351 		if (!phba->debug_slow_ring_trc) {
1352 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1353 					 "0415 Cannot create debugfs "
1354 					 "slow_ring_trace\n");
1355 			goto debug_failed;
1356 		}
1357 		if (!phba->slow_ring_trc) {
1358 			phba->slow_ring_trc = kmalloc(
1359 				(sizeof(struct lpfc_debugfs_trc) *
1360 				lpfc_debugfs_max_slow_ring_trc),
1361 				GFP_KERNEL);
1362 			if (!phba->slow_ring_trc) {
1363 				lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1364 						 "0416 Cannot create debugfs "
1365 						 "slow_ring buffer\n");
1366 				goto debug_failed;
1367 			}
1368 			atomic_set(&phba->slow_ring_trc_cnt, 0);
1369 			memset(phba->slow_ring_trc, 0,
1370 				(sizeof(struct lpfc_debugfs_trc) *
1371 				lpfc_debugfs_max_slow_ring_trc));
1372 		}
1373 	}
1374 
1375 	snprintf(name, sizeof(name), "vport%d", vport->vpi);
1376 	if (!vport->vport_debugfs_root) {
1377 		vport->vport_debugfs_root =
1378 			debugfs_create_dir(name, phba->hba_debugfs_root);
1379 		if (!vport->vport_debugfs_root) {
1380 			lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1381 					 "0417 Cant create debugfs\n");
1382 			goto debug_failed;
1383 		}
1384 		atomic_inc(&phba->debugfs_vport_count);
1385 	}
1386 
1387 	if (lpfc_debugfs_max_disc_trc) {
1388 		num = lpfc_debugfs_max_disc_trc - 1;
1389 		if (num & lpfc_debugfs_max_disc_trc) {
1390 			/* Change to be a power of 2 */
1391 			num = lpfc_debugfs_max_disc_trc;
1392 			i = 0;
1393 			while (num > 1) {
1394 				num = num >> 1;
1395 				i++;
1396 			}
1397 			lpfc_debugfs_max_disc_trc = (1 << i);
1398 			printk(KERN_ERR
1399 			       "lpfc_debugfs_max_disc_trc changed to %d\n",
1400 			       lpfc_debugfs_max_disc_trc);
1401 		}
1402 	}
1403 
1404 	vport->disc_trc = kzalloc(
1405 		(sizeof(struct lpfc_debugfs_trc) * lpfc_debugfs_max_disc_trc),
1406 		GFP_KERNEL);
1407 
1408 	if (!vport->disc_trc) {
1409 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1410 				 "0418 Cannot create debugfs disc trace "
1411 				 "buffer\n");
1412 		goto debug_failed;
1413 	}
1414 	atomic_set(&vport->disc_trc_cnt, 0);
1415 
1416 	snprintf(name, sizeof(name), "discovery_trace");
1417 	vport->debug_disc_trc =
1418 		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
1419 				 vport->vport_debugfs_root,
1420 				 vport, &lpfc_debugfs_op_disc_trc);
1421 	if (!vport->debug_disc_trc) {
1422 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1423 				 "0419 Cannot create debugfs "
1424 				 "discovery_trace\n");
1425 		goto debug_failed;
1426 	}
1427 	snprintf(name, sizeof(name), "nodelist");
1428 	vport->debug_nodelist =
1429 		debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
1430 				 vport->vport_debugfs_root,
1431 				 vport, &lpfc_debugfs_op_nodelist);
1432 	if (!vport->debug_nodelist) {
1433 		lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
1434 				 "0409 Cant create debugfs nodelist\n");
1435 		goto debug_failed;
1436 	}
1437 debug_failed:
1438 	return;
1439 #endif
1440 }
1441 
1442 /**
1443  * lpfc_debugfs_terminate -  Tear down debugfs infrastructure for this vport
1444  * @vport: The vport pointer to remove from debugfs.
1445  *
1446  * Description:
1447  * When Debugfs is configured this routine removes debugfs file system elements
1448  * that are specific to this vport. It also checks to see if there are any
1449  * users left for the debugfs directories associated with the HBA and driver. If
1450  * this is the last user of the HBA directory or driver directory then it will
1451  * remove those from the debugfs infrastructure as well.
1452  **/
1453 inline void
1454 lpfc_debugfs_terminate(struct lpfc_vport *vport)
1455 {
1456 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1457 	struct lpfc_hba   *phba = vport->phba;
1458 
1459 	if (vport->disc_trc) {
1460 		kfree(vport->disc_trc);
1461 		vport->disc_trc = NULL;
1462 	}
1463 	if (vport->debug_disc_trc) {
1464 		debugfs_remove(vport->debug_disc_trc); /* discovery_trace */
1465 		vport->debug_disc_trc = NULL;
1466 	}
1467 	if (vport->debug_nodelist) {
1468 		debugfs_remove(vport->debug_nodelist); /* nodelist */
1469 		vport->debug_nodelist = NULL;
1470 	}
1471 
1472 	if (vport->vport_debugfs_root) {
1473 		debugfs_remove(vport->vport_debugfs_root); /* vportX */
1474 		vport->vport_debugfs_root = NULL;
1475 		atomic_dec(&phba->debugfs_vport_count);
1476 	}
1477 	if (atomic_read(&phba->debugfs_vport_count) == 0) {
1478 
1479 		if (phba->debug_hbqinfo) {
1480 			debugfs_remove(phba->debug_hbqinfo); /* hbqinfo */
1481 			phba->debug_hbqinfo = NULL;
1482 		}
1483 		if (phba->debug_dumpHBASlim) {
1484 			debugfs_remove(phba->debug_dumpHBASlim); /* HBASlim */
1485 			phba->debug_dumpHBASlim = NULL;
1486 		}
1487 		if (phba->debug_dumpHostSlim) {
1488 			debugfs_remove(phba->debug_dumpHostSlim); /* HostSlim */
1489 			phba->debug_dumpHostSlim = NULL;
1490 		}
1491 		if (phba->debug_dumpData) {
1492 			debugfs_remove(phba->debug_dumpData); /* dumpData */
1493 			phba->debug_dumpData = NULL;
1494 		}
1495 
1496 		if (phba->debug_dumpDif) {
1497 			debugfs_remove(phba->debug_dumpDif); /* dumpDif */
1498 			phba->debug_dumpDif = NULL;
1499 		}
1500 
1501 		if (phba->slow_ring_trc) {
1502 			kfree(phba->slow_ring_trc);
1503 			phba->slow_ring_trc = NULL;
1504 		}
1505 		if (phba->debug_slow_ring_trc) {
1506 			/* slow_ring_trace */
1507 			debugfs_remove(phba->debug_slow_ring_trc);
1508 			phba->debug_slow_ring_trc = NULL;
1509 		}
1510 
1511 		if (phba->hba_debugfs_root) {
1512 			debugfs_remove(phba->hba_debugfs_root); /* lpfcX */
1513 			phba->hba_debugfs_root = NULL;
1514 			atomic_dec(&lpfc_debugfs_hba_count);
1515 		}
1516 
1517 		if (atomic_read(&lpfc_debugfs_hba_count) == 0) {
1518 			debugfs_remove(lpfc_debugfs_root); /* lpfc */
1519 			lpfc_debugfs_root = NULL;
1520 		}
1521 	}
1522 #endif
1523 	return;
1524 }
1525