bfad_debugfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
  3. * All rights reserved
  4. * www.brocade.com
  5. *
  6. * Linux driver for Brocade Fibre Channel Host Bus Adapter.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License (GPL) Version 2 as
  10. * published by the Free Software Foundation
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. */
  17. #include <linux/debugfs.h>
  18. #include <bfad_drv.h>
  19. #include <bfad_im.h>
  20. /*
  21. * BFA debufs interface
  22. *
  23. * To access the interface, debugfs file system should be mounted
  24. * if not already mounted using:
  25. * mount -t debugfs none /sys/kernel/debug
  26. *
  27. * BFA Hierarchy:
  28. * - bfa/host#
  29. * where the host number corresponds to the one under /sys/class/scsi_host/host#
  30. *
  31. * Debugging service available per host:
  32. * fwtrc: To collect current firmware trace.
  33. * drvtrc: To collect current driver trace
  34. * fwsave: To collect last saved fw trace as a result of firmware crash.
  35. * regwr: To write one word to chip register
  36. * regrd: To read one or more words from chip register.
  37. */
  38. struct bfad_debug_info {
  39. char *debug_buffer;
  40. void *i_private;
  41. int buffer_len;
  42. };
  43. static int
  44. bfad_debugfs_open_drvtrc(struct inode *inode, struct file *file)
  45. {
  46. struct bfad_port_s *port = inode->i_private;
  47. struct bfad_s *bfad = port->bfad;
  48. struct bfad_debug_info *debug;
  49. debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
  50. if (!debug)
  51. return -ENOMEM;
  52. debug->debug_buffer = (void *) bfad->trcmod;
  53. debug->buffer_len = sizeof(struct bfa_trc_mod_s);
  54. file->private_data = debug;
  55. return 0;
  56. }
  57. static int
  58. bfad_debugfs_open_fwtrc(struct inode *inode, struct file *file)
  59. {
  60. struct bfad_port_s *port = inode->i_private;
  61. struct bfad_s *bfad = port->bfad;
  62. struct bfad_debug_info *fw_debug;
  63. unsigned long flags;
  64. int rc;
  65. fw_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
  66. if (!fw_debug)
  67. return -ENOMEM;
  68. fw_debug->buffer_len = sizeof(struct bfa_trc_mod_s);
  69. fw_debug->debug_buffer = vmalloc(fw_debug->buffer_len);
  70. if (!fw_debug->debug_buffer) {
  71. kfree(fw_debug);
  72. printk(KERN_INFO "bfad[%d]: Failed to allocate fwtrc buffer\n",
  73. bfad->inst_no);
  74. return -ENOMEM;
  75. }
  76. memset(fw_debug->debug_buffer, 0, fw_debug->buffer_len);
  77. spin_lock_irqsave(&bfad->bfad_lock, flags);
  78. rc = bfa_debug_fwtrc(&bfad->bfa,
  79. fw_debug->debug_buffer,
  80. &fw_debug->buffer_len);
  81. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  82. if (rc != BFA_STATUS_OK) {
  83. vfree(fw_debug->debug_buffer);
  84. fw_debug->debug_buffer = NULL;
  85. kfree(fw_debug);
  86. printk(KERN_INFO "bfad[%d]: Failed to collect fwtrc\n",
  87. bfad->inst_no);
  88. return -ENOMEM;
  89. }
  90. file->private_data = fw_debug;
  91. return 0;
  92. }
  93. static int
  94. bfad_debugfs_open_fwsave(struct inode *inode, struct file *file)
  95. {
  96. struct bfad_port_s *port = inode->i_private;
  97. struct bfad_s *bfad = port->bfad;
  98. struct bfad_debug_info *fw_debug;
  99. unsigned long flags;
  100. int rc;
  101. fw_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
  102. if (!fw_debug)
  103. return -ENOMEM;
  104. fw_debug->buffer_len = sizeof(struct bfa_trc_mod_s);
  105. fw_debug->debug_buffer = vmalloc(fw_debug->buffer_len);
  106. if (!fw_debug->debug_buffer) {
  107. kfree(fw_debug);
  108. printk(KERN_INFO "bfad[%d]: Failed to allocate fwsave buffer\n",
  109. bfad->inst_no);
  110. return -ENOMEM;
  111. }
  112. memset(fw_debug->debug_buffer, 0, fw_debug->buffer_len);
  113. spin_lock_irqsave(&bfad->bfad_lock, flags);
  114. rc = bfa_debug_fwsave(&bfad->bfa,
  115. fw_debug->debug_buffer,
  116. &fw_debug->buffer_len);
  117. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  118. if (rc != BFA_STATUS_OK) {
  119. vfree(fw_debug->debug_buffer);
  120. fw_debug->debug_buffer = NULL;
  121. kfree(fw_debug);
  122. printk(KERN_INFO "bfad[%d]: Failed to collect fwsave\n",
  123. bfad->inst_no);
  124. return -ENOMEM;
  125. }
  126. file->private_data = fw_debug;
  127. return 0;
  128. }
  129. static int
  130. bfad_debugfs_open_reg(struct inode *inode, struct file *file)
  131. {
  132. struct bfad_debug_info *reg_debug;
  133. reg_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
  134. if (!reg_debug)
  135. return -ENOMEM;
  136. reg_debug->i_private = inode->i_private;
  137. file->private_data = reg_debug;
  138. return 0;
  139. }
  140. /* Changes the current file position */
  141. static loff_t
  142. bfad_debugfs_lseek(struct file *file, loff_t offset, int orig)
  143. {
  144. struct bfad_debug_info *debug;
  145. loff_t pos = file->f_pos;
  146. debug = file->private_data;
  147. switch (orig) {
  148. case 0:
  149. file->f_pos = offset;
  150. break;
  151. case 1:
  152. file->f_pos += offset;
  153. break;
  154. case 2:
  155. file->f_pos = debug->buffer_len - offset;
  156. break;
  157. default:
  158. return -EINVAL;
  159. }
  160. if (file->f_pos < 0 || file->f_pos > debug->buffer_len) {
  161. file->f_pos = pos;
  162. return -EINVAL;
  163. }
  164. return file->f_pos;
  165. }
  166. static ssize_t
  167. bfad_debugfs_read(struct file *file, char __user *buf,
  168. size_t nbytes, loff_t *pos)
  169. {
  170. struct bfad_debug_info *debug = file->private_data;
  171. if (!debug || !debug->debug_buffer)
  172. return 0;
  173. return memory_read_from_buffer(buf, nbytes, pos,
  174. debug->debug_buffer, debug->buffer_len);
  175. }
  176. #define BFA_REG_CT_ADDRSZ (0x40000)
  177. #define BFA_REG_CB_ADDRSZ (0x20000)
  178. #define BFA_REG_ADDRSZ(__bfa) \
  179. ((bfa_ioc_devid(&(__bfa)->ioc) == BFA_PCI_DEVICE_ID_CT) ? \
  180. BFA_REG_CT_ADDRSZ : BFA_REG_CB_ADDRSZ)
  181. #define BFA_REG_ADDRMSK(__bfa) ((uint32_t)(BFA_REG_ADDRSZ(__bfa) - 1))
  182. static bfa_status_t
  183. bfad_reg_offset_check(struct bfa_s *bfa, u32 offset, u32 len)
  184. {
  185. u8 area;
  186. /* check [16:15] */
  187. area = (offset >> 15) & 0x7;
  188. if (area == 0) {
  189. /* PCIe core register */
  190. if ((offset + (len<<2)) > 0x8000) /* 8k dwords or 32KB */
  191. return BFA_STATUS_EINVAL;
  192. } else if (area == 0x1) {
  193. /* CB 32 KB memory page */
  194. if ((offset + (len<<2)) > 0x10000) /* 8k dwords or 32KB */
  195. return BFA_STATUS_EINVAL;
  196. } else {
  197. /* CB register space 64KB */
  198. if ((offset + (len<<2)) > BFA_REG_ADDRMSK(bfa))
  199. return BFA_STATUS_EINVAL;
  200. }
  201. return BFA_STATUS_OK;
  202. }
  203. static ssize_t
  204. bfad_debugfs_read_regrd(struct file *file, char __user *buf,
  205. size_t nbytes, loff_t *pos)
  206. {
  207. struct bfad_debug_info *regrd_debug = file->private_data;
  208. struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private;
  209. struct bfad_s *bfad = port->bfad;
  210. ssize_t rc;
  211. if (!bfad->regdata)
  212. return 0;
  213. rc = memory_read_from_buffer(buf, nbytes, pos,
  214. bfad->regdata, bfad->reglen);
  215. if ((*pos + nbytes) >= bfad->reglen) {
  216. kfree(bfad->regdata);
  217. bfad->regdata = NULL;
  218. bfad->reglen = 0;
  219. }
  220. return rc;
  221. }
  222. static ssize_t
  223. bfad_debugfs_write_regrd(struct file *file, const char __user *buf,
  224. size_t nbytes, loff_t *ppos)
  225. {
  226. struct bfad_debug_info *regrd_debug = file->private_data;
  227. struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private;
  228. struct bfad_s *bfad = port->bfad;
  229. struct bfa_s *bfa = &bfad->bfa;
  230. struct bfa_ioc_s *ioc = &bfa->ioc;
  231. int addr, len, rc, i;
  232. u32 *regbuf;
  233. void __iomem *rb, *reg_addr;
  234. unsigned long flags;
  235. rc = sscanf(buf, "%x:%x", &addr, &len);
  236. if (rc < 2) {
  237. printk(KERN_INFO
  238. "bfad[%d]: %s failed to read user buf\n",
  239. bfad->inst_no, __func__);
  240. return -EINVAL;
  241. }
  242. kfree(bfad->regdata);
  243. bfad->regdata = NULL;
  244. bfad->reglen = 0;
  245. bfad->regdata = kzalloc(len << 2, GFP_KERNEL);
  246. if (!bfad->regdata) {
  247. printk(KERN_INFO "bfad[%d]: Failed to allocate regrd buffer\n",
  248. bfad->inst_no);
  249. return -ENOMEM;
  250. }
  251. bfad->reglen = len << 2;
  252. rb = bfa_ioc_bar0(ioc);
  253. addr &= BFA_REG_ADDRMSK(bfa);
  254. /* offset and len sanity check */
  255. rc = bfad_reg_offset_check(bfa, addr, len);
  256. if (rc) {
  257. printk(KERN_INFO "bfad[%d]: Failed reg offset check\n",
  258. bfad->inst_no);
  259. kfree(bfad->regdata);
  260. bfad->regdata = NULL;
  261. bfad->reglen = 0;
  262. return -EINVAL;
  263. }
  264. reg_addr = rb + addr;
  265. regbuf = (u32 *)bfad->regdata;
  266. spin_lock_irqsave(&bfad->bfad_lock, flags);
  267. for (i = 0; i < len; i++) {
  268. *regbuf = bfa_reg_read(reg_addr);
  269. regbuf++;
  270. reg_addr += sizeof(u32);
  271. }
  272. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  273. return nbytes;
  274. }
  275. static ssize_t
  276. bfad_debugfs_write_regwr(struct file *file, const char __user *buf,
  277. size_t nbytes, loff_t *ppos)
  278. {
  279. struct bfad_debug_info *debug = file->private_data;
  280. struct bfad_port_s *port = (struct bfad_port_s *)debug->i_private;
  281. struct bfad_s *bfad = port->bfad;
  282. struct bfa_s *bfa = &bfad->bfa;
  283. struct bfa_ioc_s *ioc = &bfa->ioc;
  284. int addr, val, rc;
  285. void __iomem *reg_addr;
  286. unsigned long flags;
  287. rc = sscanf(buf, "%x:%x", &addr, &val);
  288. if (rc < 2) {
  289. printk(KERN_INFO
  290. "bfad[%d]: %s failed to read user buf\n",
  291. bfad->inst_no, __func__);
  292. return -EINVAL;
  293. }
  294. addr &= BFA_REG_ADDRMSK(bfa); /* offset only 17 bit and word align */
  295. /* offset and len sanity check */
  296. rc = bfad_reg_offset_check(bfa, addr, 1);
  297. if (rc) {
  298. printk(KERN_INFO
  299. "bfad[%d]: Failed reg offset check\n",
  300. bfad->inst_no);
  301. return -EINVAL;
  302. }
  303. reg_addr = (uint32_t *) ((uint8_t *) bfa_ioc_bar0(ioc) + addr);
  304. spin_lock_irqsave(&bfad->bfad_lock, flags);
  305. bfa_reg_write(reg_addr, val);
  306. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  307. return nbytes;
  308. }
  309. static int
  310. bfad_debugfs_release(struct inode *inode, struct file *file)
  311. {
  312. struct bfad_debug_info *debug = file->private_data;
  313. if (!debug)
  314. return 0;
  315. file->private_data = NULL;
  316. kfree(debug);
  317. return 0;
  318. }
  319. static int
  320. bfad_debugfs_release_fwtrc(struct inode *inode, struct file *file)
  321. {
  322. struct bfad_debug_info *fw_debug = file->private_data;
  323. if (!fw_debug)
  324. return 0;
  325. if (fw_debug->debug_buffer)
  326. vfree(fw_debug->debug_buffer);
  327. file->private_data = NULL;
  328. kfree(fw_debug);
  329. return 0;
  330. }
  331. static const struct file_operations bfad_debugfs_op_drvtrc = {
  332. .owner = THIS_MODULE,
  333. .open = bfad_debugfs_open_drvtrc,
  334. .llseek = bfad_debugfs_lseek,
  335. .read = bfad_debugfs_read,
  336. .release = bfad_debugfs_release,
  337. };
  338. static const struct file_operations bfad_debugfs_op_fwtrc = {
  339. .owner = THIS_MODULE,
  340. .open = bfad_debugfs_open_fwtrc,
  341. .llseek = bfad_debugfs_lseek,
  342. .read = bfad_debugfs_read,
  343. .release = bfad_debugfs_release_fwtrc,
  344. };
  345. static const struct file_operations bfad_debugfs_op_fwsave = {
  346. .owner = THIS_MODULE,
  347. .open = bfad_debugfs_open_fwsave,
  348. .llseek = bfad_debugfs_lseek,
  349. .read = bfad_debugfs_read,
  350. .release = bfad_debugfs_release_fwtrc,
  351. };
  352. static const struct file_operations bfad_debugfs_op_regrd = {
  353. .owner = THIS_MODULE,
  354. .open = bfad_debugfs_open_reg,
  355. .llseek = bfad_debugfs_lseek,
  356. .read = bfad_debugfs_read_regrd,
  357. .write = bfad_debugfs_write_regrd,
  358. .release = bfad_debugfs_release,
  359. };
  360. static const struct file_operations bfad_debugfs_op_regwr = {
  361. .owner = THIS_MODULE,
  362. .open = bfad_debugfs_open_reg,
  363. .llseek = bfad_debugfs_lseek,
  364. .write = bfad_debugfs_write_regwr,
  365. .release = bfad_debugfs_release,
  366. };
  367. struct bfad_debugfs_entry {
  368. const char *name;
  369. mode_t mode;
  370. const struct file_operations *fops;
  371. };
  372. static const struct bfad_debugfs_entry bfad_debugfs_files[] = {
  373. { "drvtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_drvtrc, },
  374. { "fwtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwtrc, },
  375. { "fwsave", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwsave, },
  376. { "regrd", S_IFREG|S_IRUGO|S_IWUSR, &bfad_debugfs_op_regrd, },
  377. { "regwr", S_IFREG|S_IWUSR, &bfad_debugfs_op_regwr, },
  378. };
  379. static struct dentry *bfa_debugfs_root;
  380. static atomic_t bfa_debugfs_port_count;
  381. inline void
  382. bfad_debugfs_init(struct bfad_port_s *port)
  383. {
  384. struct bfad_im_port_s *im_port = port->im_port;
  385. struct bfad_s *bfad = im_port->bfad;
  386. struct Scsi_Host *shost = im_port->shost;
  387. const struct bfad_debugfs_entry *file;
  388. char name[16];
  389. int i;
  390. if (!bfa_debugfs_enable)
  391. return;
  392. /* Setup the BFA debugfs root directory*/
  393. if (!bfa_debugfs_root) {
  394. bfa_debugfs_root = debugfs_create_dir("bfa", NULL);
  395. atomic_set(&bfa_debugfs_port_count, 0);
  396. if (!bfa_debugfs_root) {
  397. printk(KERN_WARNING
  398. "BFA debugfs root dir creation failed\n");
  399. goto err;
  400. }
  401. }
  402. /*
  403. * Setup the host# directory for the port,
  404. * corresponds to the scsi_host num of this port.
  405. */
  406. snprintf(name, sizeof(name), "host%d", shost->host_no);
  407. if (!port->port_debugfs_root) {
  408. port->port_debugfs_root =
  409. debugfs_create_dir(name, bfa_debugfs_root);
  410. if (!port->port_debugfs_root) {
  411. printk(KERN_WARNING
  412. "BFA host root dir creation failed\n");
  413. goto err;
  414. }
  415. atomic_inc(&bfa_debugfs_port_count);
  416. for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
  417. file = &bfad_debugfs_files[i];
  418. bfad->bfad_dentry_files[i] =
  419. debugfs_create_file(file->name,
  420. file->mode,
  421. port->port_debugfs_root,
  422. port,
  423. file->fops);
  424. if (!bfad->bfad_dentry_files[i]) {
  425. printk(KERN_WARNING
  426. "BFA host%d: create %s entry failed\n",
  427. shost->host_no, file->name);
  428. goto err;
  429. }
  430. }
  431. }
  432. err:
  433. return;
  434. }
  435. inline void
  436. bfad_debugfs_exit(struct bfad_port_s *port)
  437. {
  438. struct bfad_im_port_s *im_port = port->im_port;
  439. struct bfad_s *bfad = im_port->bfad;
  440. int i;
  441. for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
  442. if (bfad->bfad_dentry_files[i]) {
  443. debugfs_remove(bfad->bfad_dentry_files[i]);
  444. bfad->bfad_dentry_files[i] = NULL;
  445. }
  446. }
  447. /*
  448. * Remove the host# directory for the port,
  449. * corresponds to the scsi_host num of this port.
  450. */
  451. if (port->port_debugfs_root) {
  452. debugfs_remove(port->port_debugfs_root);
  453. port->port_debugfs_root = NULL;
  454. atomic_dec(&bfa_debugfs_port_count);
  455. }
  456. /* Remove the BFA debugfs root directory */
  457. if (atomic_read(&bfa_debugfs_port_count) == 0) {
  458. debugfs_remove(bfa_debugfs_root);
  459. bfa_debugfs_root = NULL;
  460. }
  461. }