bfad_debugfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 = vzalloc(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. 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 = vzalloc(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. spin_lock_irqsave(&bfad->bfad_lock, flags);
  114. rc = bfa_ioc_debug_fwsave(&bfad->bfa.ioc,
  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 = file->private_data;
  145. return fixed_size_llseek(file, offset, orig,
  146. debug->buffer_len);
  147. }
  148. static ssize_t
  149. bfad_debugfs_read(struct file *file, char __user *buf,
  150. size_t nbytes, loff_t *pos)
  151. {
  152. struct bfad_debug_info *debug = file->private_data;
  153. if (!debug || !debug->debug_buffer)
  154. return 0;
  155. return simple_read_from_buffer(buf, nbytes, pos,
  156. debug->debug_buffer, debug->buffer_len);
  157. }
  158. #define BFA_REG_CT_ADDRSZ (0x40000)
  159. #define BFA_REG_CB_ADDRSZ (0x20000)
  160. #define BFA_REG_ADDRSZ(__ioc) \
  161. ((u32)(bfa_asic_id_ctc(bfa_ioc_devid(__ioc)) ? \
  162. BFA_REG_CT_ADDRSZ : BFA_REG_CB_ADDRSZ))
  163. #define BFA_REG_ADDRMSK(__ioc) (BFA_REG_ADDRSZ(__ioc) - 1)
  164. static bfa_status_t
  165. bfad_reg_offset_check(struct bfa_s *bfa, u32 offset, u32 len)
  166. {
  167. u8 area;
  168. /* check [16:15] */
  169. area = (offset >> 15) & 0x7;
  170. if (area == 0) {
  171. /* PCIe core register */
  172. if ((offset + (len<<2)) > 0x8000) /* 8k dwords or 32KB */
  173. return BFA_STATUS_EINVAL;
  174. } else if (area == 0x1) {
  175. /* CB 32 KB memory page */
  176. if ((offset + (len<<2)) > 0x10000) /* 8k dwords or 32KB */
  177. return BFA_STATUS_EINVAL;
  178. } else {
  179. /* CB register space 64KB */
  180. if ((offset + (len<<2)) > BFA_REG_ADDRMSK(&bfa->ioc))
  181. return BFA_STATUS_EINVAL;
  182. }
  183. return BFA_STATUS_OK;
  184. }
  185. static ssize_t
  186. bfad_debugfs_read_regrd(struct file *file, char __user *buf,
  187. size_t nbytes, loff_t *pos)
  188. {
  189. struct bfad_debug_info *regrd_debug = file->private_data;
  190. struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private;
  191. struct bfad_s *bfad = port->bfad;
  192. ssize_t rc;
  193. if (!bfad->regdata)
  194. return 0;
  195. rc = simple_read_from_buffer(buf, nbytes, pos,
  196. bfad->regdata, bfad->reglen);
  197. if ((*pos + nbytes) >= bfad->reglen) {
  198. kfree(bfad->regdata);
  199. bfad->regdata = NULL;
  200. bfad->reglen = 0;
  201. }
  202. return rc;
  203. }
  204. static ssize_t
  205. bfad_debugfs_write_regrd(struct file *file, const char __user *buf,
  206. size_t nbytes, loff_t *ppos)
  207. {
  208. struct bfad_debug_info *regrd_debug = file->private_data;
  209. struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private;
  210. struct bfad_s *bfad = port->bfad;
  211. struct bfa_s *bfa = &bfad->bfa;
  212. struct bfa_ioc_s *ioc = &bfa->ioc;
  213. int addr, rc, i;
  214. u32 len;
  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 || len > (UINT_MAX >> 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. }