bnad_debugfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. * Linux network driver for Brocade Converged Network Adapter.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License (GPL) Version 2 as
  6. * published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. /*
  14. * Copyright (c) 2005-2011 Brocade Communications Systems, Inc.
  15. * All rights reserved
  16. * www.brocade.com
  17. */
  18. #include <linux/debugfs.h>
  19. #include <linux/module.h>
  20. #include "bnad.h"
  21. /*
  22. * BNA 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. * BNA Hierarchy:
  29. * - bna/pci_dev:<pci_name>
  30. * where the pci_name corresponds to the one under /sys/bus/pci/drivers/bna
  31. *
  32. * Debugging service available per pci_dev:
  33. * fwtrc: To collect current firmware 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 bnad_debug_info {
  39. char *debug_buffer;
  40. void *i_private;
  41. int buffer_len;
  42. };
  43. static int
  44. bnad_debugfs_open_fwtrc(struct inode *inode, struct file *file)
  45. {
  46. struct bnad *bnad = inode->i_private;
  47. struct bnad_debug_info *fw_debug;
  48. unsigned long flags;
  49. int rc;
  50. fw_debug = kzalloc(sizeof(struct bnad_debug_info), GFP_KERNEL);
  51. if (!fw_debug)
  52. return -ENOMEM;
  53. fw_debug->buffer_len = BNA_DBG_FWTRC_LEN;
  54. fw_debug->debug_buffer = kzalloc(fw_debug->buffer_len, GFP_KERNEL);
  55. if (!fw_debug->debug_buffer) {
  56. kfree(fw_debug);
  57. fw_debug = NULL;
  58. return -ENOMEM;
  59. }
  60. spin_lock_irqsave(&bnad->bna_lock, flags);
  61. rc = bfa_nw_ioc_debug_fwtrc(&bnad->bna.ioceth.ioc,
  62. fw_debug->debug_buffer,
  63. &fw_debug->buffer_len);
  64. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  65. if (rc != BFA_STATUS_OK) {
  66. kfree(fw_debug->debug_buffer);
  67. fw_debug->debug_buffer = NULL;
  68. kfree(fw_debug);
  69. fw_debug = NULL;
  70. pr_warn("bnad %s: Failed to collect fwtrc\n",
  71. pci_name(bnad->pcidev));
  72. return -ENOMEM;
  73. }
  74. file->private_data = fw_debug;
  75. return 0;
  76. }
  77. static int
  78. bnad_debugfs_open_fwsave(struct inode *inode, struct file *file)
  79. {
  80. struct bnad *bnad = inode->i_private;
  81. struct bnad_debug_info *fw_debug;
  82. unsigned long flags;
  83. int rc;
  84. fw_debug = kzalloc(sizeof(struct bnad_debug_info), GFP_KERNEL);
  85. if (!fw_debug)
  86. return -ENOMEM;
  87. fw_debug->buffer_len = BNA_DBG_FWTRC_LEN;
  88. fw_debug->debug_buffer = kzalloc(fw_debug->buffer_len, GFP_KERNEL);
  89. if (!fw_debug->debug_buffer) {
  90. kfree(fw_debug);
  91. fw_debug = NULL;
  92. return -ENOMEM;
  93. }
  94. spin_lock_irqsave(&bnad->bna_lock, flags);
  95. rc = bfa_nw_ioc_debug_fwsave(&bnad->bna.ioceth.ioc,
  96. fw_debug->debug_buffer,
  97. &fw_debug->buffer_len);
  98. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  99. if (rc != BFA_STATUS_OK && rc != BFA_STATUS_ENOFSAVE) {
  100. kfree(fw_debug->debug_buffer);
  101. fw_debug->debug_buffer = NULL;
  102. kfree(fw_debug);
  103. fw_debug = NULL;
  104. pr_warn("bna %s: Failed to collect fwsave\n",
  105. pci_name(bnad->pcidev));
  106. return -ENOMEM;
  107. }
  108. file->private_data = fw_debug;
  109. return 0;
  110. }
  111. static int
  112. bnad_debugfs_open_reg(struct inode *inode, struct file *file)
  113. {
  114. struct bnad_debug_info *reg_debug;
  115. reg_debug = kzalloc(sizeof(struct bnad_debug_info), GFP_KERNEL);
  116. if (!reg_debug)
  117. return -ENOMEM;
  118. reg_debug->i_private = inode->i_private;
  119. file->private_data = reg_debug;
  120. return 0;
  121. }
  122. static int
  123. bnad_get_debug_drvinfo(struct bnad *bnad, void *buffer, u32 len)
  124. {
  125. struct bnad_drvinfo *drvinfo = (struct bnad_drvinfo *) buffer;
  126. struct bnad_iocmd_comp fcomp;
  127. unsigned long flags = 0;
  128. int ret = BFA_STATUS_FAILED;
  129. /* Get IOC info */
  130. spin_lock_irqsave(&bnad->bna_lock, flags);
  131. bfa_nw_ioc_get_attr(&bnad->bna.ioceth.ioc, &drvinfo->ioc_attr);
  132. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  133. /* Retrieve CEE related info */
  134. fcomp.bnad = bnad;
  135. fcomp.comp_status = 0;
  136. init_completion(&fcomp.comp);
  137. spin_lock_irqsave(&bnad->bna_lock, flags);
  138. ret = bfa_nw_cee_get_attr(&bnad->bna.cee, &drvinfo->cee_attr,
  139. bnad_cb_completion, &fcomp);
  140. if (ret != BFA_STATUS_OK) {
  141. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  142. goto out;
  143. }
  144. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  145. wait_for_completion(&fcomp.comp);
  146. drvinfo->cee_status = fcomp.comp_status;
  147. /* Retrieve flash partition info */
  148. fcomp.comp_status = 0;
  149. init_completion(&fcomp.comp);
  150. spin_lock_irqsave(&bnad->bna_lock, flags);
  151. ret = bfa_nw_flash_get_attr(&bnad->bna.flash, &drvinfo->flash_attr,
  152. bnad_cb_completion, &fcomp);
  153. if (ret != BFA_STATUS_OK) {
  154. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  155. goto out;
  156. }
  157. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  158. wait_for_completion(&fcomp.comp);
  159. drvinfo->flash_status = fcomp.comp_status;
  160. out:
  161. return ret;
  162. }
  163. static int
  164. bnad_debugfs_open_drvinfo(struct inode *inode, struct file *file)
  165. {
  166. struct bnad *bnad = inode->i_private;
  167. struct bnad_debug_info *drv_info;
  168. int rc;
  169. drv_info = kzalloc(sizeof(struct bnad_debug_info), GFP_KERNEL);
  170. if (!drv_info)
  171. return -ENOMEM;
  172. drv_info->buffer_len = sizeof(struct bnad_drvinfo);
  173. drv_info->debug_buffer = kzalloc(drv_info->buffer_len, GFP_KERNEL);
  174. if (!drv_info->debug_buffer) {
  175. kfree(drv_info);
  176. drv_info = NULL;
  177. return -ENOMEM;
  178. }
  179. mutex_lock(&bnad->conf_mutex);
  180. rc = bnad_get_debug_drvinfo(bnad, drv_info->debug_buffer,
  181. drv_info->buffer_len);
  182. mutex_unlock(&bnad->conf_mutex);
  183. if (rc != BFA_STATUS_OK) {
  184. kfree(drv_info->debug_buffer);
  185. drv_info->debug_buffer = NULL;
  186. kfree(drv_info);
  187. drv_info = NULL;
  188. pr_warn("bna %s: Failed to collect drvinfo\n",
  189. pci_name(bnad->pcidev));
  190. return -ENOMEM;
  191. }
  192. file->private_data = drv_info;
  193. return 0;
  194. }
  195. /* Changes the current file position */
  196. static loff_t
  197. bnad_debugfs_lseek(struct file *file, loff_t offset, int orig)
  198. {
  199. loff_t pos = file->f_pos;
  200. struct bnad_debug_info *debug = file->private_data;
  201. if (!debug)
  202. return -EINVAL;
  203. switch (orig) {
  204. case 0:
  205. file->f_pos = offset;
  206. break;
  207. case 1:
  208. file->f_pos += offset;
  209. break;
  210. case 2:
  211. file->f_pos = debug->buffer_len - offset;
  212. break;
  213. default:
  214. return -EINVAL;
  215. }
  216. if (file->f_pos < 0 || file->f_pos > debug->buffer_len) {
  217. file->f_pos = pos;
  218. return -EINVAL;
  219. }
  220. return file->f_pos;
  221. }
  222. static ssize_t
  223. bnad_debugfs_read(struct file *file, char __user *buf,
  224. size_t nbytes, loff_t *pos)
  225. {
  226. struct bnad_debug_info *debug = file->private_data;
  227. if (!debug || !debug->debug_buffer)
  228. return 0;
  229. return simple_read_from_buffer(buf, nbytes, pos,
  230. debug->debug_buffer, debug->buffer_len);
  231. }
  232. #define BFA_REG_CT_ADDRSZ (0x40000)
  233. #define BFA_REG_CB_ADDRSZ (0x20000)
  234. #define BFA_REG_ADDRSZ(__ioc) \
  235. ((u32)(bfa_asic_id_ctc(bfa_ioc_devid(__ioc)) ? \
  236. BFA_REG_CT_ADDRSZ : BFA_REG_CB_ADDRSZ))
  237. #define BFA_REG_ADDRMSK(__ioc) (BFA_REG_ADDRSZ(__ioc) - 1)
  238. /*
  239. * Function to check if the register offset passed is valid.
  240. */
  241. static int
  242. bna_reg_offset_check(struct bfa_ioc *ioc, u32 offset, u32 len)
  243. {
  244. u8 area;
  245. /* check [16:15] */
  246. area = (offset >> 15) & 0x7;
  247. if (area == 0) {
  248. /* PCIe core register */
  249. if ((offset + (len<<2)) > 0x8000) /* 8k dwords or 32KB */
  250. return BFA_STATUS_EINVAL;
  251. } else if (area == 0x1) {
  252. /* CB 32 KB memory page */
  253. if ((offset + (len<<2)) > 0x10000) /* 8k dwords or 32KB */
  254. return BFA_STATUS_EINVAL;
  255. } else {
  256. /* CB register space 64KB */
  257. if ((offset + (len<<2)) > BFA_REG_ADDRMSK(ioc))
  258. return BFA_STATUS_EINVAL;
  259. }
  260. return BFA_STATUS_OK;
  261. }
  262. static ssize_t
  263. bnad_debugfs_read_regrd(struct file *file, char __user *buf,
  264. size_t nbytes, loff_t *pos)
  265. {
  266. struct bnad_debug_info *regrd_debug = file->private_data;
  267. struct bnad *bnad = (struct bnad *)regrd_debug->i_private;
  268. ssize_t rc;
  269. if (!bnad->regdata)
  270. return 0;
  271. rc = simple_read_from_buffer(buf, nbytes, pos,
  272. bnad->regdata, bnad->reglen);
  273. if ((*pos + nbytes) >= bnad->reglen) {
  274. kfree(bnad->regdata);
  275. bnad->regdata = NULL;
  276. bnad->reglen = 0;
  277. }
  278. return rc;
  279. }
  280. static ssize_t
  281. bnad_debugfs_write_regrd(struct file *file, const char __user *buf,
  282. size_t nbytes, loff_t *ppos)
  283. {
  284. struct bnad_debug_info *regrd_debug = file->private_data;
  285. struct bnad *bnad = (struct bnad *)regrd_debug->i_private;
  286. struct bfa_ioc *ioc = &bnad->bna.ioceth.ioc;
  287. int addr, len, rc, i;
  288. u32 *regbuf;
  289. void __iomem *rb, *reg_addr;
  290. unsigned long flags;
  291. void *kern_buf;
  292. /* Allocate memory to store the user space buf */
  293. kern_buf = kzalloc(nbytes, GFP_KERNEL);
  294. if (!kern_buf)
  295. return -ENOMEM;
  296. if (copy_from_user(kern_buf, (void __user *)buf, nbytes)) {
  297. kfree(kern_buf);
  298. return -ENOMEM;
  299. }
  300. rc = sscanf(kern_buf, "%x:%x", &addr, &len);
  301. if (rc < 2) {
  302. pr_warn("bna %s: Failed to read user buffer\n",
  303. pci_name(bnad->pcidev));
  304. kfree(kern_buf);
  305. return -EINVAL;
  306. }
  307. kfree(kern_buf);
  308. kfree(bnad->regdata);
  309. bnad->regdata = NULL;
  310. bnad->reglen = 0;
  311. bnad->regdata = kzalloc(len << 2, GFP_KERNEL);
  312. if (!bnad->regdata)
  313. return -ENOMEM;
  314. bnad->reglen = len << 2;
  315. rb = bfa_ioc_bar0(ioc);
  316. addr &= BFA_REG_ADDRMSK(ioc);
  317. /* offset and len sanity check */
  318. rc = bna_reg_offset_check(ioc, addr, len);
  319. if (rc) {
  320. pr_warn("bna %s: Failed reg offset check\n",
  321. pci_name(bnad->pcidev));
  322. kfree(bnad->regdata);
  323. bnad->regdata = NULL;
  324. bnad->reglen = 0;
  325. return -EINVAL;
  326. }
  327. reg_addr = rb + addr;
  328. regbuf = (u32 *)bnad->regdata;
  329. spin_lock_irqsave(&bnad->bna_lock, flags);
  330. for (i = 0; i < len; i++) {
  331. *regbuf = readl(reg_addr);
  332. regbuf++;
  333. reg_addr += sizeof(u32);
  334. }
  335. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  336. return nbytes;
  337. }
  338. static ssize_t
  339. bnad_debugfs_write_regwr(struct file *file, const char __user *buf,
  340. size_t nbytes, loff_t *ppos)
  341. {
  342. struct bnad_debug_info *debug = file->private_data;
  343. struct bnad *bnad = (struct bnad *)debug->i_private;
  344. struct bfa_ioc *ioc = &bnad->bna.ioceth.ioc;
  345. int addr, val, rc;
  346. void __iomem *reg_addr;
  347. unsigned long flags;
  348. void *kern_buf;
  349. /* Allocate memory to store the user space buf */
  350. kern_buf = kzalloc(nbytes, GFP_KERNEL);
  351. if (!kern_buf)
  352. return -ENOMEM;
  353. if (copy_from_user(kern_buf, (void __user *)buf, nbytes)) {
  354. kfree(kern_buf);
  355. return -ENOMEM;
  356. }
  357. rc = sscanf(kern_buf, "%x:%x", &addr, &val);
  358. if (rc < 2) {
  359. pr_warn("bna %s: Failed to read user buffer\n",
  360. pci_name(bnad->pcidev));
  361. kfree(kern_buf);
  362. return -EINVAL;
  363. }
  364. kfree(kern_buf);
  365. addr &= BFA_REG_ADDRMSK(ioc); /* offset only 17 bit and word align */
  366. /* offset and len sanity check */
  367. rc = bna_reg_offset_check(ioc, addr, 1);
  368. if (rc) {
  369. pr_warn("bna %s: Failed reg offset check\n",
  370. pci_name(bnad->pcidev));
  371. return -EINVAL;
  372. }
  373. reg_addr = (bfa_ioc_bar0(ioc)) + addr;
  374. spin_lock_irqsave(&bnad->bna_lock, flags);
  375. writel(val, reg_addr);
  376. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  377. return nbytes;
  378. }
  379. static int
  380. bnad_debugfs_release(struct inode *inode, struct file *file)
  381. {
  382. struct bnad_debug_info *debug = file->private_data;
  383. if (!debug)
  384. return 0;
  385. file->private_data = NULL;
  386. kfree(debug);
  387. return 0;
  388. }
  389. static int
  390. bnad_debugfs_buffer_release(struct inode *inode, struct file *file)
  391. {
  392. struct bnad_debug_info *debug = file->private_data;
  393. if (!debug)
  394. return 0;
  395. kfree(debug->debug_buffer);
  396. file->private_data = NULL;
  397. kfree(debug);
  398. debug = NULL;
  399. return 0;
  400. }
  401. static const struct file_operations bnad_debugfs_op_fwtrc = {
  402. .owner = THIS_MODULE,
  403. .open = bnad_debugfs_open_fwtrc,
  404. .llseek = bnad_debugfs_lseek,
  405. .read = bnad_debugfs_read,
  406. .release = bnad_debugfs_buffer_release,
  407. };
  408. static const struct file_operations bnad_debugfs_op_fwsave = {
  409. .owner = THIS_MODULE,
  410. .open = bnad_debugfs_open_fwsave,
  411. .llseek = bnad_debugfs_lseek,
  412. .read = bnad_debugfs_read,
  413. .release = bnad_debugfs_buffer_release,
  414. };
  415. static const struct file_operations bnad_debugfs_op_regrd = {
  416. .owner = THIS_MODULE,
  417. .open = bnad_debugfs_open_reg,
  418. .llseek = bnad_debugfs_lseek,
  419. .read = bnad_debugfs_read_regrd,
  420. .write = bnad_debugfs_write_regrd,
  421. .release = bnad_debugfs_release,
  422. };
  423. static const struct file_operations bnad_debugfs_op_regwr = {
  424. .owner = THIS_MODULE,
  425. .open = bnad_debugfs_open_reg,
  426. .llseek = bnad_debugfs_lseek,
  427. .write = bnad_debugfs_write_regwr,
  428. .release = bnad_debugfs_release,
  429. };
  430. static const struct file_operations bnad_debugfs_op_drvinfo = {
  431. .owner = THIS_MODULE,
  432. .open = bnad_debugfs_open_drvinfo,
  433. .llseek = bnad_debugfs_lseek,
  434. .read = bnad_debugfs_read,
  435. .release = bnad_debugfs_buffer_release,
  436. };
  437. struct bnad_debugfs_entry {
  438. const char *name;
  439. umode_t mode;
  440. const struct file_operations *fops;
  441. };
  442. static const struct bnad_debugfs_entry bnad_debugfs_files[] = {
  443. { "fwtrc", S_IFREG|S_IRUGO, &bnad_debugfs_op_fwtrc, },
  444. { "fwsave", S_IFREG|S_IRUGO, &bnad_debugfs_op_fwsave, },
  445. { "regrd", S_IFREG|S_IRUGO|S_IWUSR, &bnad_debugfs_op_regrd, },
  446. { "regwr", S_IFREG|S_IWUSR, &bnad_debugfs_op_regwr, },
  447. { "drvinfo", S_IFREG|S_IRUGO, &bnad_debugfs_op_drvinfo, },
  448. };
  449. static struct dentry *bna_debugfs_root;
  450. static atomic_t bna_debugfs_port_count;
  451. /* Initialize debugfs interface for BNA */
  452. void
  453. bnad_debugfs_init(struct bnad *bnad)
  454. {
  455. const struct bnad_debugfs_entry *file;
  456. char name[64];
  457. int i;
  458. /* Setup the BNA debugfs root directory*/
  459. if (!bna_debugfs_root) {
  460. bna_debugfs_root = debugfs_create_dir("bna", NULL);
  461. atomic_set(&bna_debugfs_port_count, 0);
  462. if (!bna_debugfs_root) {
  463. pr_warn("BNA: debugfs root dir creation failed\n");
  464. return;
  465. }
  466. }
  467. /* Setup the pci_dev debugfs directory for the port */
  468. snprintf(name, sizeof(name), "pci_dev:%s", pci_name(bnad->pcidev));
  469. if (!bnad->port_debugfs_root) {
  470. bnad->port_debugfs_root =
  471. debugfs_create_dir(name, bna_debugfs_root);
  472. if (!bnad->port_debugfs_root) {
  473. pr_warn("bna pci_dev %s: root dir creation failed\n",
  474. pci_name(bnad->pcidev));
  475. return;
  476. }
  477. atomic_inc(&bna_debugfs_port_count);
  478. for (i = 0; i < ARRAY_SIZE(bnad_debugfs_files); i++) {
  479. file = &bnad_debugfs_files[i];
  480. bnad->bnad_dentry_files[i] =
  481. debugfs_create_file(file->name,
  482. file->mode,
  483. bnad->port_debugfs_root,
  484. bnad,
  485. file->fops);
  486. if (!bnad->bnad_dentry_files[i]) {
  487. pr_warn(
  488. "BNA pci_dev:%s: create %s entry failed\n",
  489. pci_name(bnad->pcidev), file->name);
  490. return;
  491. }
  492. }
  493. }
  494. }
  495. /* Uninitialize debugfs interface for BNA */
  496. void
  497. bnad_debugfs_uninit(struct bnad *bnad)
  498. {
  499. int i;
  500. for (i = 0; i < ARRAY_SIZE(bnad_debugfs_files); i++) {
  501. if (bnad->bnad_dentry_files[i]) {
  502. debugfs_remove(bnad->bnad_dentry_files[i]);
  503. bnad->bnad_dentry_files[i] = NULL;
  504. }
  505. }
  506. /* Remove the pci_dev debugfs directory for the port */
  507. if (bnad->port_debugfs_root) {
  508. debugfs_remove(bnad->port_debugfs_root);
  509. bnad->port_debugfs_root = NULL;
  510. atomic_dec(&bna_debugfs_port_count);
  511. }
  512. /* Remove the BNA debugfs root directory */
  513. if (atomic_read(&bna_debugfs_port_count) == 0) {
  514. debugfs_remove(bna_debugfs_root);
  515. bna_debugfs_root = NULL;
  516. }
  517. }