lpfc_debugfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. *
  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. #include <linux/blkdev.h>
  21. #include <linux/delay.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/idr.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/kthread.h>
  26. #include <linux/pci.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/ctype.h>
  29. #include <linux/version.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_transport_fc.h>
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_disc.h"
  37. #include "lpfc_scsi.h"
  38. #include "lpfc.h"
  39. #include "lpfc_logmsg.h"
  40. #include "lpfc_crtn.h"
  41. #include "lpfc_vport.h"
  42. #include "lpfc_version.h"
  43. #include "lpfc_vport.h"
  44. #include "lpfc_debugfs.h"
  45. #ifdef CONFIG_LPFC_DEBUG_FS
  46. /* debugfs interface
  47. *
  48. * To access this interface the user should:
  49. * # mkdir /debug
  50. * # mount -t debugfs none /debug
  51. *
  52. * The lpfc debugfs directory hierachy is:
  53. * lpfc/lpfcX/vportY
  54. * where X is the lpfc hba unique_id
  55. * where Y is the vport VPI on that hba
  56. *
  57. * Debugging services available per vport:
  58. * discovery_trace
  59. * This is an ACSII readable file that contains a trace of the last
  60. * lpfc_debugfs_max_disc_trc events that happened on a specific vport.
  61. * See lpfc_debugfs.h for different categories of
  62. * discovery events. To enable the discovery trace, the following
  63. * module parameters must be set:
  64. * lpfc_debugfs_enable=1 Turns on lpfc debugfs filesystem support
  65. * lpfc_debugfs_max_disc_trc=X Where X is the event trace depth for
  66. * EACH vport. X MUST also be a power of 2.
  67. * lpfc_debugfs_mask_disc_trc=Y Where Y is an event mask as defined in
  68. * lpfc_debugfs.h .
  69. */
  70. static int lpfc_debugfs_enable = 0;
  71. module_param(lpfc_debugfs_enable, int, 0);
  72. MODULE_PARM_DESC(lpfc_debugfs_enable, "Enable debugfs services");
  73. static int lpfc_debugfs_max_disc_trc = 0; /* This MUST be a power of 2 */
  74. module_param(lpfc_debugfs_max_disc_trc, int, 0);
  75. MODULE_PARM_DESC(lpfc_debugfs_max_disc_trc,
  76. "Set debugfs discovery trace depth");
  77. static int lpfc_debugfs_mask_disc_trc = 0;
  78. module_param(lpfc_debugfs_mask_disc_trc, int, 0);
  79. MODULE_PARM_DESC(lpfc_debugfs_mask_disc_trc,
  80. "Set debugfs discovery trace mask");
  81. #include <linux/debugfs.h>
  82. /* size of discovery_trace output line */
  83. #define LPFC_DISC_TRC_ENTRY_SIZE 80
  84. /* nodelist output buffer size */
  85. #define LPFC_NODELIST_SIZE 8192
  86. #define LPFC_NODELIST_ENTRY_SIZE 120
  87. struct lpfc_debug {
  88. char *buffer;
  89. int len;
  90. };
  91. atomic_t lpfc_debugfs_disc_trc_cnt = ATOMIC_INIT(0);
  92. unsigned long lpfc_debugfs_start_time = 0L;
  93. static int
  94. lpfc_debugfs_disc_trc_data(struct lpfc_vport *vport, char *buf, int size)
  95. {
  96. int i, index, len, enable;
  97. uint32_t ms;
  98. struct lpfc_disc_trc *dtp;
  99. char buffer[80];
  100. enable = lpfc_debugfs_enable;
  101. lpfc_debugfs_enable = 0;
  102. len = 0;
  103. index = (atomic_read(&vport->disc_trc_cnt) + 1) &
  104. (lpfc_debugfs_max_disc_trc - 1);
  105. for (i = index; i < lpfc_debugfs_max_disc_trc; i++) {
  106. dtp = vport->disc_trc + i;
  107. if (!dtp->fmt)
  108. continue;
  109. ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
  110. snprintf(buffer, 80, "%010d:%010d ms:%s\n",
  111. dtp->seq_cnt, ms, dtp->fmt);
  112. len += snprintf(buf+len, size-len, buffer,
  113. dtp->data1, dtp->data2, dtp->data3);
  114. }
  115. for (i = 0; i < index; i++) {
  116. dtp = vport->disc_trc + i;
  117. if (!dtp->fmt)
  118. continue;
  119. ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
  120. snprintf(buffer, 80, "%010d:%010d ms:%s\n",
  121. dtp->seq_cnt, ms, dtp->fmt);
  122. len += snprintf(buf+len, size-len, buffer,
  123. dtp->data1, dtp->data2, dtp->data3);
  124. }
  125. lpfc_debugfs_enable = enable;
  126. return len;
  127. }
  128. static int
  129. lpfc_debugfs_nodelist_data(struct lpfc_vport *vport, char *buf, int size)
  130. {
  131. int len = 0;
  132. int cnt;
  133. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  134. struct lpfc_nodelist *ndlp;
  135. unsigned char *statep, *name;
  136. cnt = (LPFC_NODELIST_SIZE / LPFC_NODELIST_ENTRY_SIZE);
  137. spin_lock_irq(shost->host_lock);
  138. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  139. if (!cnt) {
  140. len += snprintf(buf+len, size-len,
  141. "Missing Nodelist Entries\n");
  142. break;
  143. }
  144. cnt--;
  145. switch (ndlp->nlp_state) {
  146. case NLP_STE_UNUSED_NODE:
  147. statep = "UNUSED";
  148. break;
  149. case NLP_STE_PLOGI_ISSUE:
  150. statep = "PLOGI ";
  151. break;
  152. case NLP_STE_ADISC_ISSUE:
  153. statep = "ADISC ";
  154. break;
  155. case NLP_STE_REG_LOGIN_ISSUE:
  156. statep = "REGLOG";
  157. break;
  158. case NLP_STE_PRLI_ISSUE:
  159. statep = "PRLI ";
  160. break;
  161. case NLP_STE_UNMAPPED_NODE:
  162. statep = "UNMAP ";
  163. break;
  164. case NLP_STE_MAPPED_NODE:
  165. statep = "MAPPED";
  166. break;
  167. case NLP_STE_NPR_NODE:
  168. statep = "NPR ";
  169. break;
  170. default:
  171. statep = "UNKNOWN";
  172. }
  173. len += snprintf(buf+len, size-len, "%s DID:x%06x ",
  174. statep, ndlp->nlp_DID);
  175. name = (unsigned char *)&ndlp->nlp_portname;
  176. len += snprintf(buf+len, size-len,
  177. "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",
  178. *name, *(name+1), *(name+2), *(name+3),
  179. *(name+4), *(name+5), *(name+6), *(name+7));
  180. name = (unsigned char *)&ndlp->nlp_nodename;
  181. len += snprintf(buf+len, size-len,
  182. "WWNN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",
  183. *name, *(name+1), *(name+2), *(name+3),
  184. *(name+4), *(name+5), *(name+6), *(name+7));
  185. len += snprintf(buf+len, size-len, "RPI:%03d flag:x%08x ",
  186. ndlp->nlp_rpi, ndlp->nlp_flag);
  187. if (!ndlp->nlp_type)
  188. len += snprintf(buf+len, size-len, "UNKNOWN_TYPE");
  189. if (ndlp->nlp_type & NLP_FC_NODE)
  190. len += snprintf(buf+len, size-len, "FC_NODE ");
  191. if (ndlp->nlp_type & NLP_FABRIC)
  192. len += snprintf(buf+len, size-len, "FABRIC ");
  193. if (ndlp->nlp_type & NLP_FCP_TARGET)
  194. len += snprintf(buf+len, size-len, "FCP_TGT sid:%d ",
  195. ndlp->nlp_sid);
  196. if (ndlp->nlp_type & NLP_FCP_INITIATOR)
  197. len += snprintf(buf+len, size-len, "FCP_INITIATOR");
  198. len += snprintf(buf+len, size-len, "\n");
  199. }
  200. spin_unlock_irq(shost->host_lock);
  201. return len;
  202. }
  203. #endif
  204. inline void
  205. lpfc_debugfs_disc_trc(struct lpfc_vport *vport, int mask, char *fmt,
  206. uint32_t data1, uint32_t data2, uint32_t data3)
  207. {
  208. #ifdef CONFIG_LPFC_DEBUG_FS
  209. struct lpfc_disc_trc *dtp;
  210. int index;
  211. if (!(lpfc_debugfs_mask_disc_trc & mask))
  212. return;
  213. if (!lpfc_debugfs_enable || !lpfc_debugfs_max_disc_trc ||
  214. !vport || !vport->disc_trc)
  215. return;
  216. index = atomic_inc_return(&vport->disc_trc_cnt) &
  217. (lpfc_debugfs_max_disc_trc - 1);
  218. dtp = vport->disc_trc + index;
  219. dtp->fmt = fmt;
  220. dtp->data1 = data1;
  221. dtp->data2 = data2;
  222. dtp->data3 = data3;
  223. dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_disc_trc_cnt);
  224. dtp->jif = jiffies;
  225. #endif
  226. return;
  227. }
  228. #ifdef CONFIG_LPFC_DEBUG_FS
  229. static int
  230. lpfc_debugfs_disc_trc_open(struct inode *inode, struct file *file)
  231. {
  232. struct lpfc_vport *vport = inode->i_private;
  233. struct lpfc_debug *debug;
  234. int size;
  235. int rc = -ENOMEM;
  236. if (!lpfc_debugfs_max_disc_trc) {
  237. rc = -ENOSPC;
  238. goto out;
  239. }
  240. debug = kmalloc(sizeof(*debug), GFP_KERNEL);
  241. if (!debug)
  242. goto out;
  243. /* Round to page boundry */
  244. size = (lpfc_debugfs_max_disc_trc * LPFC_DISC_TRC_ENTRY_SIZE);
  245. size = PAGE_ALIGN(size);
  246. debug->buffer = kmalloc(size, GFP_KERNEL);
  247. if (!debug->buffer) {
  248. kfree(debug);
  249. goto out;
  250. }
  251. debug->len = lpfc_debugfs_disc_trc_data(vport, debug->buffer, size);
  252. file->private_data = debug;
  253. rc = 0;
  254. out:
  255. return rc;
  256. }
  257. static int
  258. lpfc_debugfs_nodelist_open(struct inode *inode, struct file *file)
  259. {
  260. struct lpfc_vport *vport = inode->i_private;
  261. struct lpfc_debug *debug;
  262. int rc = -ENOMEM;
  263. debug = kmalloc(sizeof(*debug), GFP_KERNEL);
  264. if (!debug)
  265. goto out;
  266. /* Round to page boundry */
  267. debug->buffer = kmalloc(LPFC_NODELIST_SIZE, GFP_KERNEL);
  268. if (!debug->buffer) {
  269. kfree(debug);
  270. goto out;
  271. }
  272. debug->len = lpfc_debugfs_nodelist_data(vport, debug->buffer,
  273. LPFC_NODELIST_SIZE);
  274. file->private_data = debug;
  275. rc = 0;
  276. out:
  277. return rc;
  278. }
  279. static loff_t
  280. lpfc_debugfs_lseek(struct file *file, loff_t off, int whence)
  281. {
  282. struct lpfc_debug *debug;
  283. loff_t pos = -1;
  284. debug = file->private_data;
  285. switch (whence) {
  286. case 0:
  287. pos = off;
  288. break;
  289. case 1:
  290. pos = file->f_pos + off;
  291. break;
  292. case 2:
  293. pos = debug->len - off;
  294. }
  295. return (pos < 0 || pos > debug->len) ? -EINVAL : (file->f_pos = pos);
  296. }
  297. static ssize_t
  298. lpfc_debugfs_read(struct file *file, char __user *buf,
  299. size_t nbytes, loff_t *ppos)
  300. {
  301. struct lpfc_debug *debug = file->private_data;
  302. return simple_read_from_buffer(buf, nbytes, ppos, debug->buffer,
  303. debug->len);
  304. }
  305. static int
  306. lpfc_debugfs_release(struct inode *inode, struct file *file)
  307. {
  308. struct lpfc_debug *debug = file->private_data;
  309. kfree(debug->buffer);
  310. kfree(debug);
  311. return 0;
  312. }
  313. #undef lpfc_debugfs_op_disc_trc
  314. static struct file_operations lpfc_debugfs_op_disc_trc = {
  315. .owner = THIS_MODULE,
  316. .open = lpfc_debugfs_disc_trc_open,
  317. .llseek = lpfc_debugfs_lseek,
  318. .read = lpfc_debugfs_read,
  319. .release = lpfc_debugfs_release,
  320. };
  321. #undef lpfc_debugfs_op_nodelist
  322. static struct file_operations lpfc_debugfs_op_nodelist = {
  323. .owner = THIS_MODULE,
  324. .open = lpfc_debugfs_nodelist_open,
  325. .llseek = lpfc_debugfs_lseek,
  326. .read = lpfc_debugfs_read,
  327. .release = lpfc_debugfs_release,
  328. };
  329. static struct dentry *lpfc_debugfs_root = NULL;
  330. static atomic_t lpfc_debugfs_hba_count;
  331. #endif
  332. inline void
  333. lpfc_debugfs_initialize(struct lpfc_vport *vport)
  334. {
  335. #ifdef CONFIG_LPFC_DEBUG_FS
  336. struct lpfc_hba *phba = vport->phba;
  337. char name[64];
  338. uint32_t num, i;
  339. if (!lpfc_debugfs_enable)
  340. return;
  341. if (lpfc_debugfs_max_disc_trc) {
  342. num = lpfc_debugfs_max_disc_trc - 1;
  343. if (num & lpfc_debugfs_max_disc_trc) {
  344. /* Change to be a power of 2 */
  345. num = lpfc_debugfs_max_disc_trc;
  346. i = 0;
  347. while (num > 1) {
  348. num = num >> 1;
  349. i++;
  350. }
  351. lpfc_debugfs_max_disc_trc = (1 << i);
  352. printk(KERN_ERR
  353. "lpfc_debugfs_max_disc_trc changed to %d\n",
  354. lpfc_debugfs_max_disc_trc);
  355. }
  356. }
  357. if (!lpfc_debugfs_root) {
  358. lpfc_debugfs_root = debugfs_create_dir("lpfc", NULL);
  359. atomic_set(&lpfc_debugfs_hba_count, 0);
  360. if (!lpfc_debugfs_root)
  361. goto debug_failed;
  362. }
  363. snprintf(name, sizeof(name), "lpfc%d", phba->brd_no);
  364. if (!phba->hba_debugfs_root) {
  365. phba->hba_debugfs_root =
  366. debugfs_create_dir(name, lpfc_debugfs_root);
  367. if (!phba->hba_debugfs_root)
  368. goto debug_failed;
  369. atomic_inc(&lpfc_debugfs_hba_count);
  370. atomic_set(&phba->debugfs_vport_count, 0);
  371. }
  372. snprintf(name, sizeof(name), "vport%d", vport->vpi);
  373. if (!vport->vport_debugfs_root) {
  374. vport->vport_debugfs_root =
  375. debugfs_create_dir(name, phba->hba_debugfs_root);
  376. if (!vport->vport_debugfs_root)
  377. goto debug_failed;
  378. atomic_inc(&phba->debugfs_vport_count);
  379. }
  380. if (!lpfc_debugfs_start_time)
  381. lpfc_debugfs_start_time = jiffies;
  382. vport->disc_trc = kmalloc(
  383. (sizeof(struct lpfc_disc_trc) * lpfc_debugfs_max_disc_trc),
  384. GFP_KERNEL);
  385. if (!vport->disc_trc)
  386. goto debug_failed;
  387. memset(vport->disc_trc, 0,
  388. (sizeof(struct lpfc_disc_trc) * lpfc_debugfs_max_disc_trc));
  389. snprintf(name, sizeof(name), "discovery_trace");
  390. vport->debug_disc_trc =
  391. debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
  392. vport->vport_debugfs_root,
  393. vport, &lpfc_debugfs_op_disc_trc);
  394. if (!vport->debug_disc_trc) {
  395. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  396. "%d:0409 Cannot create debugfs",
  397. phba->brd_no);
  398. goto debug_failed;
  399. }
  400. snprintf(name, sizeof(name), "nodelist");
  401. vport->debug_nodelist =
  402. debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
  403. vport->vport_debugfs_root,
  404. vport, &lpfc_debugfs_op_nodelist);
  405. if (!vport->debug_nodelist) {
  406. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  407. "%d:0409 Cannot create debugfs",
  408. phba->brd_no);
  409. goto debug_failed;
  410. }
  411. debug_failed:
  412. return;
  413. #endif
  414. }
  415. inline void
  416. lpfc_debugfs_terminate(struct lpfc_vport *vport)
  417. {
  418. #ifdef CONFIG_LPFC_DEBUG_FS
  419. struct lpfc_hba *phba = vport->phba;
  420. if (vport->disc_trc) {
  421. kfree(vport->disc_trc);
  422. vport->disc_trc = NULL;
  423. }
  424. if (vport->debug_disc_trc) {
  425. debugfs_remove(vport->debug_disc_trc); /* discovery_trace */
  426. vport->debug_disc_trc = NULL;
  427. }
  428. if (vport->debug_nodelist) {
  429. debugfs_remove(vport->debug_nodelist); /* nodelist */
  430. vport->debug_nodelist = NULL;
  431. }
  432. if (vport->vport_debugfs_root) {
  433. debugfs_remove(vport->vport_debugfs_root); /* vportX */
  434. vport->vport_debugfs_root = NULL;
  435. atomic_dec(&phba->debugfs_vport_count);
  436. }
  437. if (atomic_read(&phba->debugfs_vport_count) == 0) {
  438. debugfs_remove(vport->phba->hba_debugfs_root); /* lpfcX */
  439. vport->phba->hba_debugfs_root = NULL;
  440. atomic_dec(&lpfc_debugfs_hba_count);
  441. if (atomic_read(&lpfc_debugfs_hba_count) == 0) {
  442. debugfs_remove(lpfc_debugfs_root); /* lpfc */
  443. lpfc_debugfs_root = NULL;
  444. }
  445. }
  446. #endif
  447. }