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