bfad_debugfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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;
  146. loff_t pos = file->f_pos;
  147. debug = file->private_data;
  148. switch (orig) {
  149. case 0:
  150. file->f_pos = offset;
  151. break;
  152. case 1:
  153. file->f_pos += offset;
  154. break;
  155. case 2:
  156. file->f_pos = debug->buffer_len - offset;
  157. break;
  158. default:
  159. return -EINVAL;
  160. }
  161. if (file->f_pos < 0 || file->f_pos > debug->buffer_len) {
  162. file->f_pos = pos;
  163. return -EINVAL;
  164. }
  165. return file->f_pos;
  166. }
  167. static ssize_t
  168. bfad_debugfs_read(struct file *file, char __user *buf,
  169. size_t nbytes, loff_t *pos)
  170. {
  171. struct bfad_debug_info *debug = file->private_data;
  172. if (!debug || !debug->debug_buffer)
  173. return 0;
  174. return simple_read_from_buffer(buf, nbytes, pos,
  175. debug->debug_buffer, debug->buffer_len);
  176. }
  177. #define BFA_REG_CT_ADDRSZ (0x40000)
  178. #define BFA_REG_CB_ADDRSZ (0x20000)
  179. #define BFA_REG_ADDRSZ(__ioc) \
  180. ((u32)(bfa_asic_id_ctc(bfa_ioc_devid(__ioc)) ? \
  181. BFA_REG_CT_ADDRSZ : BFA_REG_CB_ADDRSZ))
  182. #define BFA_REG_ADDRMSK(__ioc) (BFA_REG_ADDRSZ(__ioc) - 1)
  183. static bfa_status_t
  184. bfad_reg_offset_check(struct bfa_s *bfa, u32 offset, u32 len)
  185. {
  186. u8 area;
  187. /* check [16:15] */
  188. area = (offset >> 15) & 0x7;
  189. if (area == 0) {
  190. /* PCIe core register */
  191. if ((offset + (len<<2)) > 0x8000) /* 8k dwords or 32KB */
  192. return BFA_STATUS_EINVAL;
  193. } else if (area == 0x1) {
  194. /* CB 32 KB memory page */
  195. if ((offset + (len<<2)) > 0x10000) /* 8k dwords or 32KB */
  196. return BFA_STATUS_EINVAL;
  197. } else {
  198. /* CB register space 64KB */
  199. if ((offset + (len<<2)) > BFA_REG_ADDRMSK(&bfa->ioc))
  200. return BFA_STATUS_EINVAL;
  201. }
  202. return BFA_STATUS_OK;
  203. }
  204. static ssize_t
  205. bfad_debugfs_read_regrd(struct file *file, char __user *buf,
  206. size_t nbytes, loff_t *pos)
  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. ssize_t rc;
  212. if (!bfad->regdata)
  213. return 0;
  214. rc = simple_read_from_buffer(buf, nbytes, pos,
  215. bfad->regdata, bfad->reglen);
  216. if ((*pos + nbytes) >= bfad->reglen) {
  217. kfree(bfad->regdata);
  218. bfad->regdata = NULL;
  219. bfad->reglen = 0;
  220. }
  221. return rc;
  222. }
  223. static ssize_t
  224. bfad_debugfs_write_regrd(struct file *file, const char __user *buf,
  225. size_t nbytes, loff_t *ppos)
  226. {
  227. struct bfad_debug_info *regrd_debug = file->private_data;
  228. struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private;
  229. struct bfad_s *bfad = port->bfad;
  230. struct bfa_s *bfa = &bfad->bfa;
  231. struct bfa_ioc_s *ioc = &bfa->ioc;
  232. int addr, len, rc, i;
  233. u32 *regbuf;
  234. void __iomem *rb, *reg_addr;
  235. unsigned long flags;
  236. void *kern_buf;
  237. kern_buf = kzalloc(nbytes, GFP_KERNEL);
  238. if (!kern_buf) {
  239. printk(KERN_INFO "bfad[%d]: Failed to allocate buffer\n",
  240. bfad->inst_no);
  241. return -ENOMEM;
  242. }
  243. if (copy_from_user(kern_buf, (void __user *)buf, nbytes)) {
  244. kfree(kern_buf);
  245. return -ENOMEM;
  246. }
  247. rc = sscanf(kern_buf, "%x:%x", &addr, &len);
  248. if (rc < 2) {
  249. printk(KERN_INFO
  250. "bfad[%d]: %s failed to read user buf\n",
  251. bfad->inst_no, __func__);
  252. kfree(kern_buf);
  253. return -EINVAL;
  254. }
  255. kfree(kern_buf);
  256. kfree(bfad->regdata);
  257. bfad->regdata = NULL;
  258. bfad->reglen = 0;
  259. bfad->regdata = kzalloc(len << 2, GFP_KERNEL);
  260. if (!bfad->regdata) {
  261. printk(KERN_INFO "bfad[%d]: Failed to allocate regrd buffer\n",
  262. bfad->inst_no);
  263. return -ENOMEM;
  264. }
  265. bfad->reglen = len << 2;
  266. rb = bfa_ioc_bar0(ioc);
  267. addr &= BFA_REG_ADDRMSK(ioc);
  268. /* offset and len sanity check */
  269. rc = bfad_reg_offset_check(bfa, addr, len);
  270. if (rc) {
  271. printk(KERN_INFO "bfad[%d]: Failed reg offset check\n",
  272. bfad->inst_no);
  273. kfree(bfad->regdata);
  274. bfad->regdata = NULL;
  275. bfad->reglen = 0;
  276. return -EINVAL;
  277. }
  278. reg_addr = rb + addr;
  279. regbuf = (u32 *)bfad->regdata;
  280. spin_lock_irqsave(&bfad->bfad_lock, flags);
  281. for (i = 0; i < len; i++) {
  282. *regbuf = readl(reg_addr);
  283. regbuf++;
  284. reg_addr += sizeof(u32);
  285. }
  286. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  287. return nbytes;
  288. }
  289. static ssize_t
  290. bfad_debugfs_write_regwr(struct file *file, const char __user *buf,
  291. size_t nbytes, loff_t *ppos)
  292. {
  293. struct bfad_debug_info *debug = file->private_data;
  294. struct bfad_port_s *port = (struct bfad_port_s *)debug->i_private;
  295. struct bfad_s *bfad = port->bfad;
  296. struct bfa_s *bfa = &bfad->bfa;
  297. struct bfa_ioc_s *ioc = &bfa->ioc;
  298. int addr, val, rc;
  299. void __iomem *reg_addr;
  300. unsigned long flags;
  301. void *kern_buf;
  302. kern_buf = kzalloc(nbytes, GFP_KERNEL);
  303. if (!kern_buf) {
  304. printk(KERN_INFO "bfad[%d]: Failed to allocate buffer\n",
  305. bfad->inst_no);
  306. return -ENOMEM;
  307. }
  308. if (copy_from_user(kern_buf, (void __user *)buf, nbytes)) {
  309. kfree(kern_buf);
  310. return -ENOMEM;
  311. }
  312. rc = sscanf(kern_buf, "%x:%x", &addr, &val);
  313. if (rc < 2) {
  314. printk(KERN_INFO
  315. "bfad[%d]: %s failed to read user buf\n",
  316. bfad->inst_no, __func__);
  317. kfree(kern_buf);
  318. return -EINVAL;
  319. }
  320. kfree(kern_buf);
  321. addr &= BFA_REG_ADDRMSK(ioc); /* offset only 17 bit and word align */
  322. /* offset and len sanity check */
  323. rc = bfad_reg_offset_check(bfa, addr, 1);
  324. if (rc) {
  325. printk(KERN_INFO
  326. "bfad[%d]: Failed reg offset check\n",
  327. bfad->inst_no);
  328. return -EINVAL;
  329. }
  330. reg_addr = (bfa_ioc_bar0(ioc)) + addr;
  331. spin_lock_irqsave(&bfad->bfad_lock, flags);
  332. writel(val, reg_addr);
  333. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  334. return nbytes;
  335. }
  336. static int
  337. bfad_debugfs_release(struct inode *inode, struct file *file)
  338. {
  339. struct bfad_debug_info *debug = file->private_data;
  340. if (!debug)
  341. return 0;
  342. file->private_data = NULL;
  343. kfree(debug);
  344. return 0;
  345. }
  346. static int
  347. bfad_debugfs_release_fwtrc(struct inode *inode, struct file *file)
  348. {
  349. struct bfad_debug_info *fw_debug = file->private_data;
  350. if (!fw_debug)
  351. return 0;
  352. if (fw_debug->debug_buffer)
  353. vfree(fw_debug->debug_buffer);
  354. file->private_data = NULL;
  355. kfree(fw_debug);
  356. return 0;
  357. }
  358. static const struct file_operations bfad_debugfs_op_drvtrc = {
  359. .owner = THIS_MODULE,
  360. .open = bfad_debugfs_open_drvtrc,
  361. .llseek = bfad_debugfs_lseek,
  362. .read = bfad_debugfs_read,
  363. .release = bfad_debugfs_release,
  364. };
  365. static const struct file_operations bfad_debugfs_op_fwtrc = {
  366. .owner = THIS_MODULE,
  367. .open = bfad_debugfs_open_fwtrc,
  368. .llseek = bfad_debugfs_lseek,
  369. .read = bfad_debugfs_read,
  370. .release = bfad_debugfs_release_fwtrc,
  371. };
  372. static const struct file_operations bfad_debugfs_op_fwsave = {
  373. .owner = THIS_MODULE,
  374. .open = bfad_debugfs_open_fwsave,
  375. .llseek = bfad_debugfs_lseek,
  376. .read = bfad_debugfs_read,
  377. .release = bfad_debugfs_release_fwtrc,
  378. };
  379. static const struct file_operations bfad_debugfs_op_regrd = {
  380. .owner = THIS_MODULE,
  381. .open = bfad_debugfs_open_reg,
  382. .llseek = bfad_debugfs_lseek,
  383. .read = bfad_debugfs_read_regrd,
  384. .write = bfad_debugfs_write_regrd,
  385. .release = bfad_debugfs_release,
  386. };
  387. static const struct file_operations bfad_debugfs_op_regwr = {
  388. .owner = THIS_MODULE,
  389. .open = bfad_debugfs_open_reg,
  390. .llseek = bfad_debugfs_lseek,
  391. .write = bfad_debugfs_write_regwr,
  392. .release = bfad_debugfs_release,
  393. };
  394. struct bfad_debugfs_entry {
  395. const char *name;
  396. umode_t mode;
  397. const struct file_operations *fops;
  398. };
  399. static const struct bfad_debugfs_entry bfad_debugfs_files[] = {
  400. { "drvtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_drvtrc, },
  401. { "fwtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwtrc, },
  402. { "fwsave", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwsave, },
  403. { "regrd", S_IFREG|S_IRUGO|S_IWUSR, &bfad_debugfs_op_regrd, },
  404. { "regwr", S_IFREG|S_IWUSR, &bfad_debugfs_op_regwr, },
  405. };
  406. static struct dentry *bfa_debugfs_root;
  407. static atomic_t bfa_debugfs_port_count;
  408. inline void
  409. bfad_debugfs_init(struct bfad_port_s *port)
  410. {
  411. struct bfad_s *bfad = port->bfad;
  412. const struct bfad_debugfs_entry *file;
  413. char name[64];
  414. int i;
  415. if (!bfa_debugfs_enable)
  416. return;
  417. /* Setup the BFA debugfs root directory*/
  418. if (!bfa_debugfs_root) {
  419. bfa_debugfs_root = debugfs_create_dir("bfa", NULL);
  420. atomic_set(&bfa_debugfs_port_count, 0);
  421. if (!bfa_debugfs_root) {
  422. printk(KERN_WARNING
  423. "BFA debugfs root dir creation failed\n");
  424. goto err;
  425. }
  426. }
  427. /* Setup the pci_dev debugfs directory for the port */
  428. snprintf(name, sizeof(name), "pci_dev:%s", bfad->pci_name);
  429. if (!port->port_debugfs_root) {
  430. port->port_debugfs_root =
  431. debugfs_create_dir(name, bfa_debugfs_root);
  432. if (!port->port_debugfs_root) {
  433. printk(KERN_WARNING
  434. "bfa %s: debugfs root creation failed\n",
  435. bfad->pci_name);
  436. goto err;
  437. }
  438. atomic_inc(&bfa_debugfs_port_count);
  439. for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
  440. file = &bfad_debugfs_files[i];
  441. bfad->bfad_dentry_files[i] =
  442. debugfs_create_file(file->name,
  443. file->mode,
  444. port->port_debugfs_root,
  445. port,
  446. file->fops);
  447. if (!bfad->bfad_dentry_files[i]) {
  448. printk(KERN_WARNING
  449. "bfa %s: debugfs %s creation failed\n",
  450. bfad->pci_name, file->name);
  451. goto err;
  452. }
  453. }
  454. }
  455. err:
  456. return;
  457. }
  458. inline void
  459. bfad_debugfs_exit(struct bfad_port_s *port)
  460. {
  461. struct bfad_s *bfad = port->bfad;
  462. int i;
  463. for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
  464. if (bfad->bfad_dentry_files[i]) {
  465. debugfs_remove(bfad->bfad_dentry_files[i]);
  466. bfad->bfad_dentry_files[i] = NULL;
  467. }
  468. }
  469. /* Remove the pci_dev debugfs directory for the port */
  470. if (port->port_debugfs_root) {
  471. debugfs_remove(port->port_debugfs_root);
  472. port->port_debugfs_root = NULL;
  473. atomic_dec(&bfa_debugfs_port_count);
  474. }
  475. /* Remove the BFA debugfs root directory */
  476. if (atomic_read(&bfa_debugfs_port_count) == 0) {
  477. debugfs_remove(bfa_debugfs_root);
  478. bfa_debugfs_root = NULL;
  479. }
  480. }