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, len, rc, i;
  215. u32 *regbuf;
  216. void __iomem *rb, *reg_addr;
  217. unsigned long flags;
  218. void *kern_buf;
  219. kern_buf = memdup_user(buf, nbytes);
  220. if (IS_ERR(kern_buf))
  221. return PTR_ERR(kern_buf);
  222. rc = sscanf(kern_buf, "%x:%x", &addr, &len);
  223. if (rc < 2) {
  224. printk(KERN_INFO
  225. "bfad[%d]: %s failed to read user buf\n",
  226. bfad->inst_no, __func__);
  227. kfree(kern_buf);
  228. return -EINVAL;
  229. }
  230. kfree(kern_buf);
  231. kfree(bfad->regdata);
  232. bfad->regdata = NULL;
  233. bfad->reglen = 0;
  234. bfad->regdata = kzalloc(len << 2, GFP_KERNEL);
  235. if (!bfad->regdata) {
  236. printk(KERN_INFO "bfad[%d]: Failed to allocate regrd buffer\n",
  237. bfad->inst_no);
  238. return -ENOMEM;
  239. }
  240. bfad->reglen = len << 2;
  241. rb = bfa_ioc_bar0(ioc);
  242. addr &= BFA_REG_ADDRMSK(ioc);
  243. /* offset and len sanity check */
  244. rc = bfad_reg_offset_check(bfa, addr, len);
  245. if (rc) {
  246. printk(KERN_INFO "bfad[%d]: Failed reg offset check\n",
  247. bfad->inst_no);
  248. kfree(bfad->regdata);
  249. bfad->regdata = NULL;
  250. bfad->reglen = 0;
  251. return -EINVAL;
  252. }
  253. reg_addr = rb + addr;
  254. regbuf = (u32 *)bfad->regdata;
  255. spin_lock_irqsave(&bfad->bfad_lock, flags);
  256. for (i = 0; i < len; i++) {
  257. *regbuf = readl(reg_addr);
  258. regbuf++;
  259. reg_addr += sizeof(u32);
  260. }
  261. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  262. return nbytes;
  263. }
  264. static ssize_t
  265. bfad_debugfs_write_regwr(struct file *file, const char __user *buf,
  266. size_t nbytes, loff_t *ppos)
  267. {
  268. struct bfad_debug_info *debug = file->private_data;
  269. struct bfad_port_s *port = (struct bfad_port_s *)debug->i_private;
  270. struct bfad_s *bfad = port->bfad;
  271. struct bfa_s *bfa = &bfad->bfa;
  272. struct bfa_ioc_s *ioc = &bfa->ioc;
  273. int addr, val, rc;
  274. void __iomem *reg_addr;
  275. unsigned long flags;
  276. void *kern_buf;
  277. kern_buf = memdup_user(buf, nbytes);
  278. if (IS_ERR(kern_buf))
  279. return PTR_ERR(kern_buf);
  280. rc = sscanf(kern_buf, "%x:%x", &addr, &val);
  281. if (rc < 2) {
  282. printk(KERN_INFO
  283. "bfad[%d]: %s failed to read user buf\n",
  284. bfad->inst_no, __func__);
  285. kfree(kern_buf);
  286. return -EINVAL;
  287. }
  288. kfree(kern_buf);
  289. addr &= BFA_REG_ADDRMSK(ioc); /* offset only 17 bit and word align */
  290. /* offset and len sanity check */
  291. rc = bfad_reg_offset_check(bfa, addr, 1);
  292. if (rc) {
  293. printk(KERN_INFO
  294. "bfad[%d]: Failed reg offset check\n",
  295. bfad->inst_no);
  296. return -EINVAL;
  297. }
  298. reg_addr = (bfa_ioc_bar0(ioc)) + addr;
  299. spin_lock_irqsave(&bfad->bfad_lock, flags);
  300. writel(val, reg_addr);
  301. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  302. return nbytes;
  303. }
  304. static int
  305. bfad_debugfs_release(struct inode *inode, struct file *file)
  306. {
  307. struct bfad_debug_info *debug = file->private_data;
  308. if (!debug)
  309. return 0;
  310. file->private_data = NULL;
  311. kfree(debug);
  312. return 0;
  313. }
  314. static int
  315. bfad_debugfs_release_fwtrc(struct inode *inode, struct file *file)
  316. {
  317. struct bfad_debug_info *fw_debug = file->private_data;
  318. if (!fw_debug)
  319. return 0;
  320. if (fw_debug->debug_buffer)
  321. vfree(fw_debug->debug_buffer);
  322. file->private_data = NULL;
  323. kfree(fw_debug);
  324. return 0;
  325. }
  326. static const struct file_operations bfad_debugfs_op_drvtrc = {
  327. .owner = THIS_MODULE,
  328. .open = bfad_debugfs_open_drvtrc,
  329. .llseek = bfad_debugfs_lseek,
  330. .read = bfad_debugfs_read,
  331. .release = bfad_debugfs_release,
  332. };
  333. static const struct file_operations bfad_debugfs_op_fwtrc = {
  334. .owner = THIS_MODULE,
  335. .open = bfad_debugfs_open_fwtrc,
  336. .llseek = bfad_debugfs_lseek,
  337. .read = bfad_debugfs_read,
  338. .release = bfad_debugfs_release_fwtrc,
  339. };
  340. static const struct file_operations bfad_debugfs_op_fwsave = {
  341. .owner = THIS_MODULE,
  342. .open = bfad_debugfs_open_fwsave,
  343. .llseek = bfad_debugfs_lseek,
  344. .read = bfad_debugfs_read,
  345. .release = bfad_debugfs_release_fwtrc,
  346. };
  347. static const struct file_operations bfad_debugfs_op_regrd = {
  348. .owner = THIS_MODULE,
  349. .open = bfad_debugfs_open_reg,
  350. .llseek = bfad_debugfs_lseek,
  351. .read = bfad_debugfs_read_regrd,
  352. .write = bfad_debugfs_write_regrd,
  353. .release = bfad_debugfs_release,
  354. };
  355. static const struct file_operations bfad_debugfs_op_regwr = {
  356. .owner = THIS_MODULE,
  357. .open = bfad_debugfs_open_reg,
  358. .llseek = bfad_debugfs_lseek,
  359. .write = bfad_debugfs_write_regwr,
  360. .release = bfad_debugfs_release,
  361. };
  362. struct bfad_debugfs_entry {
  363. const char *name;
  364. umode_t mode;
  365. const struct file_operations *fops;
  366. };
  367. static const struct bfad_debugfs_entry bfad_debugfs_files[] = {
  368. { "drvtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_drvtrc, },
  369. { "fwtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwtrc, },
  370. { "fwsave", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwsave, },
  371. { "regrd", S_IFREG|S_IRUGO|S_IWUSR, &bfad_debugfs_op_regrd, },
  372. { "regwr", S_IFREG|S_IWUSR, &bfad_debugfs_op_regwr, },
  373. };
  374. static struct dentry *bfa_debugfs_root;
  375. static atomic_t bfa_debugfs_port_count;
  376. inline void
  377. bfad_debugfs_init(struct bfad_port_s *port)
  378. {
  379. struct bfad_s *bfad = port->bfad;
  380. const struct bfad_debugfs_entry *file;
  381. char name[64];
  382. int i;
  383. if (!bfa_debugfs_enable)
  384. return;
  385. /* Setup the BFA debugfs root directory*/
  386. if (!bfa_debugfs_root) {
  387. bfa_debugfs_root = debugfs_create_dir("bfa", NULL);
  388. atomic_set(&bfa_debugfs_port_count, 0);
  389. if (!bfa_debugfs_root) {
  390. printk(KERN_WARNING
  391. "BFA debugfs root dir creation failed\n");
  392. goto err;
  393. }
  394. }
  395. /* Setup the pci_dev debugfs directory for the port */
  396. snprintf(name, sizeof(name), "pci_dev:%s", bfad->pci_name);
  397. if (!port->port_debugfs_root) {
  398. port->port_debugfs_root =
  399. debugfs_create_dir(name, bfa_debugfs_root);
  400. if (!port->port_debugfs_root) {
  401. printk(KERN_WARNING
  402. "bfa %s: debugfs root creation failed\n",
  403. bfad->pci_name);
  404. goto err;
  405. }
  406. atomic_inc(&bfa_debugfs_port_count);
  407. for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
  408. file = &bfad_debugfs_files[i];
  409. bfad->bfad_dentry_files[i] =
  410. debugfs_create_file(file->name,
  411. file->mode,
  412. port->port_debugfs_root,
  413. port,
  414. file->fops);
  415. if (!bfad->bfad_dentry_files[i]) {
  416. printk(KERN_WARNING
  417. "bfa %s: debugfs %s creation failed\n",
  418. bfad->pci_name, file->name);
  419. goto err;
  420. }
  421. }
  422. }
  423. err:
  424. return;
  425. }
  426. inline void
  427. bfad_debugfs_exit(struct bfad_port_s *port)
  428. {
  429. struct bfad_s *bfad = port->bfad;
  430. int i;
  431. for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
  432. if (bfad->bfad_dentry_files[i]) {
  433. debugfs_remove(bfad->bfad_dentry_files[i]);
  434. bfad->bfad_dentry_files[i] = NULL;
  435. }
  436. }
  437. /* Remove the pci_dev debugfs directory for the port */
  438. if (port->port_debugfs_root) {
  439. debugfs_remove(port->port_debugfs_root);
  440. port->port_debugfs_root = NULL;
  441. atomic_dec(&bfa_debugfs_port_count);
  442. }
  443. /* Remove the BFA debugfs root directory */
  444. if (atomic_read(&bfa_debugfs_port_count) == 0) {
  445. debugfs_remove(bfa_debugfs_root);
  446. bfa_debugfs_root = NULL;
  447. }
  448. }