1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2007 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
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 *******************************************************************/
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/pci.h>
28 #include <linux/spinlock.h>
29 #include <linux/ctype.h>
30 #include <linux/version.h>
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_transport_fc.h>
39 #include "lpfc_disc.h"
40 #include "lpfc_scsi.h"
42 #include "lpfc_logmsg.h"
43 #include "lpfc_crtn.h"
44 #include "lpfc_vport.h"
45 #include "lpfc_version.h"
46 #include "lpfc_vport.h"
47 #include "lpfc_debugfs.h"
49 #ifdef CONFIG_LPFC_DEBUG_FS
52 * To access this interface the user should:
54 * # mount -t debugfs none /debug
56 * The lpfc debugfs directory hierachy is:
58 * where X is the lpfc hba unique_id
59 * where Y is the vport VPI on that hba
61 * Debugging services available per vport:
63 * This is an ACSII readable file that contains a trace of the last
64 * lpfc_debugfs_max_disc_trc events that happened on a specific vport.
65 * See lpfc_debugfs.h for different categories of
66 * discovery events. To enable the discovery trace, the following
67 * module parameters must be set:
68 * lpfc_debugfs_enable=1 Turns on lpfc debugfs filesystem support
69 * lpfc_debugfs_max_disc_trc=X Where X is the event trace depth for
70 * EACH vport. X MUST also be a power of 2.
71 * lpfc_debugfs_mask_disc_trc=Y Where Y is an event mask as defined in
74 static int lpfc_debugfs_enable = 0;
75 module_param(lpfc_debugfs_enable, int, 0);
76 MODULE_PARM_DESC(lpfc_debugfs_enable, "Enable debugfs services");
78 static int lpfc_debugfs_max_disc_trc = 0; /* This MUST be a power of 2 */
79 module_param(lpfc_debugfs_max_disc_trc, int, 0);
80 MODULE_PARM_DESC(lpfc_debugfs_max_disc_trc,
81 "Set debugfs discovery trace depth");
83 static int lpfc_debugfs_mask_disc_trc = 0;
84 module_param(lpfc_debugfs_mask_disc_trc, int, 0);
85 MODULE_PARM_DESC(lpfc_debugfs_mask_disc_trc,
86 "Set debugfs discovery trace mask");
88 #include <linux/debugfs.h>
90 /* size of discovery_trace output line */
91 #define LPFC_DISC_TRC_ENTRY_SIZE 80
93 /* nodelist output buffer size */
94 #define LPFC_NODELIST_SIZE 8192
95 #define LPFC_NODELIST_ENTRY_SIZE 120
102 atomic_t lpfc_debugfs_disc_trc_cnt = ATOMIC_INIT(0);
103 unsigned long lpfc_debugfs_start_time = 0L;
106 lpfc_debugfs_disc_trc_data(struct lpfc_vport *vport, char *buf, int size)
108 int i, index, len, enable;
110 struct lpfc_disc_trc *dtp;
114 enable = lpfc_debugfs_enable;
115 lpfc_debugfs_enable = 0;
118 index = (atomic_read(&vport->disc_trc_cnt) + 1) &
119 (lpfc_debugfs_max_disc_trc - 1);
120 for (i = index; i < lpfc_debugfs_max_disc_trc; i++) {
121 dtp = vport->disc_trc + i;
124 ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
125 snprintf(buffer, 80, "%010d:%010d ms:%s\n",
126 dtp->seq_cnt, ms, dtp->fmt);
127 len += snprintf(buf+len, size-len, buffer,
128 dtp->data1, dtp->data2, dtp->data3);
130 for (i = 0; i < index; i++) {
131 dtp = vport->disc_trc + i;
134 ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
135 snprintf(buffer, 80, "%010d:%010d ms:%s\n",
136 dtp->seq_cnt, ms, dtp->fmt);
137 len += snprintf(buf+len, size-len, buffer,
138 dtp->data1, dtp->data2, dtp->data3);
141 lpfc_debugfs_enable = enable;
146 lpfc_debugfs_nodelist_data(struct lpfc_vport *vport, char *buf, int size)
150 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
151 struct lpfc_nodelist *ndlp;
152 unsigned char *statep, *name;
154 cnt = (LPFC_NODELIST_SIZE / LPFC_NODELIST_ENTRY_SIZE);
156 spin_lock_irq(shost->host_lock);
157 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
159 len += snprintf(buf+len, size-len,
160 "Missing Nodelist Entries\n");
164 switch (ndlp->nlp_state) {
165 case NLP_STE_UNUSED_NODE:
168 case NLP_STE_PLOGI_ISSUE:
171 case NLP_STE_ADISC_ISSUE:
174 case NLP_STE_REG_LOGIN_ISSUE:
177 case NLP_STE_PRLI_ISSUE:
180 case NLP_STE_UNMAPPED_NODE:
183 case NLP_STE_MAPPED_NODE:
186 case NLP_STE_NPR_NODE:
192 len += snprintf(buf+len, size-len, "%s DID:x%06x ",
193 statep, ndlp->nlp_DID);
194 name = (unsigned char *)&ndlp->nlp_portname;
195 len += snprintf(buf+len, size-len,
196 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",
197 *name, *(name+1), *(name+2), *(name+3),
198 *(name+4), *(name+5), *(name+6), *(name+7));
199 name = (unsigned char *)&ndlp->nlp_nodename;
200 len += snprintf(buf+len, size-len,
201 "WWNN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",
202 *name, *(name+1), *(name+2), *(name+3),
203 *(name+4), *(name+5), *(name+6), *(name+7));
204 len += snprintf(buf+len, size-len, "RPI:%03d flag:x%08x ",
205 ndlp->nlp_rpi, ndlp->nlp_flag);
207 len += snprintf(buf+len, size-len, "UNKNOWN_TYPE");
208 if (ndlp->nlp_type & NLP_FC_NODE)
209 len += snprintf(buf+len, size-len, "FC_NODE ");
210 if (ndlp->nlp_type & NLP_FABRIC)
211 len += snprintf(buf+len, size-len, "FABRIC ");
212 if (ndlp->nlp_type & NLP_FCP_TARGET)
213 len += snprintf(buf+len, size-len, "FCP_TGT sid:%d ",
215 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
216 len += snprintf(buf+len, size-len, "FCP_INITIATOR");
217 len += snprintf(buf+len, size-len, "\n");
219 spin_unlock_irq(shost->host_lock);
226 lpfc_debugfs_disc_trc(struct lpfc_vport *vport, int mask, char *fmt,
227 uint32_t data1, uint32_t data2, uint32_t data3)
229 #ifdef CONFIG_LPFC_DEBUG_FS
230 struct lpfc_disc_trc *dtp;
233 if (!(lpfc_debugfs_mask_disc_trc & mask))
236 if (!lpfc_debugfs_enable || !lpfc_debugfs_max_disc_trc ||
237 !vport || !vport->disc_trc)
240 index = atomic_inc_return(&vport->disc_trc_cnt) &
241 (lpfc_debugfs_max_disc_trc - 1);
242 dtp = vport->disc_trc + index;
247 dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_disc_trc_cnt);
253 #ifdef CONFIG_LPFC_DEBUG_FS
255 lpfc_debugfs_disc_trc_open(struct inode *inode, struct file *file)
257 struct lpfc_vport *vport = inode->i_private;
258 struct lpfc_debug *debug;
262 if (!lpfc_debugfs_max_disc_trc) {
267 debug = kmalloc(sizeof(*debug), GFP_KERNEL);
271 /* Round to page boundry */
272 size = (lpfc_debugfs_max_disc_trc * LPFC_DISC_TRC_ENTRY_SIZE);
273 size = PAGE_ALIGN(size);
275 debug->buffer = kmalloc(size, GFP_KERNEL);
276 if (!debug->buffer) {
281 debug->len = lpfc_debugfs_disc_trc_data(vport, debug->buffer, size);
282 file->private_data = debug;
290 lpfc_debugfs_nodelist_open(struct inode *inode, struct file *file)
292 struct lpfc_vport *vport = inode->i_private;
293 struct lpfc_debug *debug;
296 debug = kmalloc(sizeof(*debug), GFP_KERNEL);
300 /* Round to page boundry */
301 debug->buffer = kmalloc(LPFC_NODELIST_SIZE, GFP_KERNEL);
302 if (!debug->buffer) {
307 debug->len = lpfc_debugfs_nodelist_data(vport, debug->buffer,
309 file->private_data = debug;
317 lpfc_debugfs_lseek(struct file *file, loff_t off, int whence)
319 struct lpfc_debug *debug;
322 debug = file->private_data;
329 pos = file->f_pos + off;
332 pos = debug->len - off;
334 return (pos < 0 || pos > debug->len) ? -EINVAL : (file->f_pos = pos);
338 lpfc_debugfs_read(struct file *file, char __user *buf,
339 size_t nbytes, loff_t *ppos)
341 struct lpfc_debug *debug = file->private_data;
342 return simple_read_from_buffer(buf, nbytes, ppos, debug->buffer,
347 lpfc_debugfs_release(struct inode *inode, struct file *file)
349 struct lpfc_debug *debug = file->private_data;
351 kfree(debug->buffer);
357 #undef lpfc_debugfs_op_disc_trc
358 static struct file_operations lpfc_debugfs_op_disc_trc = {
359 .owner = THIS_MODULE,
360 .open = lpfc_debugfs_disc_trc_open,
361 .llseek = lpfc_debugfs_lseek,
362 .read = lpfc_debugfs_read,
363 .release = lpfc_debugfs_release,
366 #undef lpfc_debugfs_op_nodelist
367 static struct file_operations lpfc_debugfs_op_nodelist = {
368 .owner = THIS_MODULE,
369 .open = lpfc_debugfs_nodelist_open,
370 .llseek = lpfc_debugfs_lseek,
371 .read = lpfc_debugfs_read,
372 .release = lpfc_debugfs_release,
375 static struct dentry *lpfc_debugfs_root = NULL;
376 static atomic_t lpfc_debugfs_hba_count;
380 lpfc_debugfs_initialize(struct lpfc_vport *vport)
382 #ifdef CONFIG_LPFC_DEBUG_FS
383 struct lpfc_hba *phba = vport->phba;
387 if (!lpfc_debugfs_enable)
390 if (lpfc_debugfs_max_disc_trc) {
391 num = lpfc_debugfs_max_disc_trc - 1;
392 if (num & lpfc_debugfs_max_disc_trc) {
393 /* Change to be a power of 2 */
394 num = lpfc_debugfs_max_disc_trc;
400 lpfc_debugfs_max_disc_trc = (1 << i);
402 "lpfc_debugfs_max_disc_trc changed to %d\n",
403 lpfc_debugfs_max_disc_trc);
407 if (!lpfc_debugfs_root) {
408 lpfc_debugfs_root = debugfs_create_dir("lpfc", NULL);
409 atomic_set(&lpfc_debugfs_hba_count, 0);
410 if (!lpfc_debugfs_root)
414 snprintf(name, sizeof(name), "lpfc%d", phba->brd_no);
415 if (!phba->hba_debugfs_root) {
416 phba->hba_debugfs_root =
417 debugfs_create_dir(name, lpfc_debugfs_root);
418 if (!phba->hba_debugfs_root)
420 atomic_inc(&lpfc_debugfs_hba_count);
421 atomic_set(&phba->debugfs_vport_count, 0);
424 snprintf(name, sizeof(name), "vport%d", vport->vpi);
425 if (!vport->vport_debugfs_root) {
426 vport->vport_debugfs_root =
427 debugfs_create_dir(name, phba->hba_debugfs_root);
428 if (!vport->vport_debugfs_root)
430 atomic_inc(&phba->debugfs_vport_count);
433 if (!lpfc_debugfs_start_time)
434 lpfc_debugfs_start_time = jiffies;
436 vport->disc_trc = kmalloc(
437 (sizeof(struct lpfc_disc_trc) * lpfc_debugfs_max_disc_trc),
440 if (!vport->disc_trc)
442 memset(vport->disc_trc, 0,
443 (sizeof(struct lpfc_disc_trc) * lpfc_debugfs_max_disc_trc));
445 snprintf(name, sizeof(name), "discovery_trace");
446 vport->debug_disc_trc =
447 debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
448 vport->vport_debugfs_root,
449 vport, &lpfc_debugfs_op_disc_trc);
450 if (!vport->debug_disc_trc) {
451 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
452 "%d:0409 Cannot create debugfs",
456 snprintf(name, sizeof(name), "nodelist");
457 vport->debug_nodelist =
458 debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
459 vport->vport_debugfs_root,
460 vport, &lpfc_debugfs_op_nodelist);
461 if (!vport->debug_nodelist) {
462 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
463 "%d:0409 Cannot create debugfs",
474 lpfc_debugfs_terminate(struct lpfc_vport *vport)
476 #ifdef CONFIG_LPFC_DEBUG_FS
477 struct lpfc_hba *phba = vport->phba;
479 if (vport->disc_trc) {
480 kfree(vport->disc_trc);
481 vport->disc_trc = NULL;
483 if (vport->debug_disc_trc) {
484 debugfs_remove(vport->debug_disc_trc); /* discovery_trace */
485 vport->debug_disc_trc = NULL;
487 if (vport->debug_nodelist) {
488 debugfs_remove(vport->debug_nodelist); /* nodelist */
489 vport->debug_nodelist = NULL;
491 if (vport->vport_debugfs_root) {
492 debugfs_remove(vport->vport_debugfs_root); /* vportX */
493 vport->vport_debugfs_root = NULL;
494 atomic_dec(&phba->debugfs_vport_count);
496 if (atomic_read(&phba->debugfs_vport_count) == 0) {
497 debugfs_remove(vport->phba->hba_debugfs_root); /* lpfcX */
498 vport->phba->hba_debugfs_root = NULL;
499 atomic_dec(&lpfc_debugfs_hba_count);
500 if (atomic_read(&lpfc_debugfs_hba_count) == 0) {
501 debugfs_remove(lpfc_debugfs_root); /* lpfc */
502 lpfc_debugfs_root = NULL;