bfad_debugfs.c 13 KB

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