PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel-source/drivers/scsi/bfa/bfad_debugfs.c

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