bfad_debugfs.c 13 KB

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