bnad_debugfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. struct bnad_debug_info *debug = file->private_data;
  200. if (!debug)
  201. return -EINVAL;
  202. return fixed_size_llseek(file, offset, orig, debug->buffer_len);
  203. }
  204. static ssize_t
  205. bnad_debugfs_read(struct file *file, char __user *buf,
  206. size_t nbytes, loff_t *pos)
  207. {
  208. struct bnad_debug_info *debug = file->private_data;
  209. if (!debug || !debug->debug_buffer)
  210. return 0;
  211. return simple_read_from_buffer(buf, nbytes, pos,
  212. debug->debug_buffer, debug->buffer_len);
  213. }
  214. #define BFA_REG_CT_ADDRSZ (0x40000)
  215. #define BFA_REG_CB_ADDRSZ (0x20000)
  216. #define BFA_REG_ADDRSZ(__ioc) \
  217. ((u32)(bfa_asic_id_ctc(bfa_ioc_devid(__ioc)) ? \
  218. BFA_REG_CT_ADDRSZ : BFA_REG_CB_ADDRSZ))
  219. #define BFA_REG_ADDRMSK(__ioc) (BFA_REG_ADDRSZ(__ioc) - 1)
  220. /*
  221. * Function to check if the register offset passed is valid.
  222. */
  223. static int
  224. bna_reg_offset_check(struct bfa_ioc *ioc, u32 offset, u32 len)
  225. {
  226. u8 area;
  227. /* check [16:15] */
  228. area = (offset >> 15) & 0x7;
  229. if (area == 0) {
  230. /* PCIe core register */
  231. if ((offset + (len<<2)) > 0x8000) /* 8k dwords or 32KB */
  232. return BFA_STATUS_EINVAL;
  233. } else if (area == 0x1) {
  234. /* CB 32 KB memory page */
  235. if ((offset + (len<<2)) > 0x10000) /* 8k dwords or 32KB */
  236. return BFA_STATUS_EINVAL;
  237. } else {
  238. /* CB register space 64KB */
  239. if ((offset + (len<<2)) > BFA_REG_ADDRMSK(ioc))
  240. return BFA_STATUS_EINVAL;
  241. }
  242. return BFA_STATUS_OK;
  243. }
  244. static ssize_t
  245. bnad_debugfs_read_regrd(struct file *file, char __user *buf,
  246. size_t nbytes, loff_t *pos)
  247. {
  248. struct bnad_debug_info *regrd_debug = file->private_data;
  249. struct bnad *bnad = (struct bnad *)regrd_debug->i_private;
  250. ssize_t rc;
  251. if (!bnad->regdata)
  252. return 0;
  253. rc = simple_read_from_buffer(buf, nbytes, pos,
  254. bnad->regdata, bnad->reglen);
  255. if ((*pos + nbytes) >= bnad->reglen) {
  256. kfree(bnad->regdata);
  257. bnad->regdata = NULL;
  258. bnad->reglen = 0;
  259. }
  260. return rc;
  261. }
  262. static ssize_t
  263. bnad_debugfs_write_regrd(struct file *file, const char __user *buf,
  264. size_t nbytes, loff_t *ppos)
  265. {
  266. struct bnad_debug_info *regrd_debug = file->private_data;
  267. struct bnad *bnad = (struct bnad *)regrd_debug->i_private;
  268. struct bfa_ioc *ioc = &bnad->bna.ioceth.ioc;
  269. int addr, len, rc, i;
  270. u32 *regbuf;
  271. void __iomem *rb, *reg_addr;
  272. unsigned long flags;
  273. void *kern_buf;
  274. /* Allocate memory to store the user space buf */
  275. kern_buf = kzalloc(nbytes, GFP_KERNEL);
  276. if (!kern_buf)
  277. return -ENOMEM;
  278. if (copy_from_user(kern_buf, (void __user *)buf, nbytes)) {
  279. kfree(kern_buf);
  280. return -ENOMEM;
  281. }
  282. rc = sscanf(kern_buf, "%x:%x", &addr, &len);
  283. if (rc < 2) {
  284. pr_warn("bna %s: Failed to read user buffer\n",
  285. pci_name(bnad->pcidev));
  286. kfree(kern_buf);
  287. return -EINVAL;
  288. }
  289. kfree(kern_buf);
  290. kfree(bnad->regdata);
  291. bnad->regdata = NULL;
  292. bnad->reglen = 0;
  293. bnad->regdata = kzalloc(len << 2, GFP_KERNEL);
  294. if (!bnad->regdata)
  295. return -ENOMEM;
  296. bnad->reglen = len << 2;
  297. rb = bfa_ioc_bar0(ioc);
  298. addr &= BFA_REG_ADDRMSK(ioc);
  299. /* offset and len sanity check */
  300. rc = bna_reg_offset_check(ioc, addr, len);
  301. if (rc) {
  302. pr_warn("bna %s: Failed reg offset check\n",
  303. pci_name(bnad->pcidev));
  304. kfree(bnad->regdata);
  305. bnad->regdata = NULL;
  306. bnad->reglen = 0;
  307. return -EINVAL;
  308. }
  309. reg_addr = rb + addr;
  310. regbuf = (u32 *)bnad->regdata;
  311. spin_lock_irqsave(&bnad->bna_lock, flags);
  312. for (i = 0; i < len; i++) {
  313. *regbuf = readl(reg_addr);
  314. regbuf++;
  315. reg_addr += sizeof(u32);
  316. }
  317. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  318. return nbytes;
  319. }
  320. static ssize_t
  321. bnad_debugfs_write_regwr(struct file *file, const char __user *buf,
  322. size_t nbytes, loff_t *ppos)
  323. {
  324. struct bnad_debug_info *debug = file->private_data;
  325. struct bnad *bnad = (struct bnad *)debug->i_private;
  326. struct bfa_ioc *ioc = &bnad->bna.ioceth.ioc;
  327. int addr, val, rc;
  328. void __iomem *reg_addr;
  329. unsigned long flags;
  330. void *kern_buf;
  331. /* Allocate memory to store the user space buf */
  332. kern_buf = kzalloc(nbytes, GFP_KERNEL);
  333. if (!kern_buf)
  334. return -ENOMEM;
  335. if (copy_from_user(kern_buf, (void __user *)buf, nbytes)) {
  336. kfree(kern_buf);
  337. return -ENOMEM;
  338. }
  339. rc = sscanf(kern_buf, "%x:%x", &addr, &val);
  340. if (rc < 2) {
  341. pr_warn("bna %s: Failed to read user buffer\n",
  342. pci_name(bnad->pcidev));
  343. kfree(kern_buf);
  344. return -EINVAL;
  345. }
  346. kfree(kern_buf);
  347. addr &= BFA_REG_ADDRMSK(ioc); /* offset only 17 bit and word align */
  348. /* offset and len sanity check */
  349. rc = bna_reg_offset_check(ioc, addr, 1);
  350. if (rc) {
  351. pr_warn("bna %s: Failed reg offset check\n",
  352. pci_name(bnad->pcidev));
  353. return -EINVAL;
  354. }
  355. reg_addr = (bfa_ioc_bar0(ioc)) + addr;
  356. spin_lock_irqsave(&bnad->bna_lock, flags);
  357. writel(val, reg_addr);
  358. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  359. return nbytes;
  360. }
  361. static int
  362. bnad_debugfs_release(struct inode *inode, struct file *file)
  363. {
  364. struct bnad_debug_info *debug = file->private_data;
  365. if (!debug)
  366. return 0;
  367. file->private_data = NULL;
  368. kfree(debug);
  369. return 0;
  370. }
  371. static int
  372. bnad_debugfs_buffer_release(struct inode *inode, struct file *file)
  373. {
  374. struct bnad_debug_info *debug = file->private_data;
  375. if (!debug)
  376. return 0;
  377. kfree(debug->debug_buffer);
  378. file->private_data = NULL;
  379. kfree(debug);
  380. debug = NULL;
  381. return 0;
  382. }
  383. static const struct file_operations bnad_debugfs_op_fwtrc = {
  384. .owner = THIS_MODULE,
  385. .open = bnad_debugfs_open_fwtrc,
  386. .llseek = bnad_debugfs_lseek,
  387. .read = bnad_debugfs_read,
  388. .release = bnad_debugfs_buffer_release,
  389. };
  390. static const struct file_operations bnad_debugfs_op_fwsave = {
  391. .owner = THIS_MODULE,
  392. .open = bnad_debugfs_open_fwsave,
  393. .llseek = bnad_debugfs_lseek,
  394. .read = bnad_debugfs_read,
  395. .release = bnad_debugfs_buffer_release,
  396. };
  397. static const struct file_operations bnad_debugfs_op_regrd = {
  398. .owner = THIS_MODULE,
  399. .open = bnad_debugfs_open_reg,
  400. .llseek = bnad_debugfs_lseek,
  401. .read = bnad_debugfs_read_regrd,
  402. .write = bnad_debugfs_write_regrd,
  403. .release = bnad_debugfs_release,
  404. };
  405. static const struct file_operations bnad_debugfs_op_regwr = {
  406. .owner = THIS_MODULE,
  407. .open = bnad_debugfs_open_reg,
  408. .llseek = bnad_debugfs_lseek,
  409. .write = bnad_debugfs_write_regwr,
  410. .release = bnad_debugfs_release,
  411. };
  412. static const struct file_operations bnad_debugfs_op_drvinfo = {
  413. .owner = THIS_MODULE,
  414. .open = bnad_debugfs_open_drvinfo,
  415. .llseek = bnad_debugfs_lseek,
  416. .read = bnad_debugfs_read,
  417. .release = bnad_debugfs_buffer_release,
  418. };
  419. struct bnad_debugfs_entry {
  420. const char *name;
  421. umode_t mode;
  422. const struct file_operations *fops;
  423. };
  424. static const struct bnad_debugfs_entry bnad_debugfs_files[] = {
  425. { "fwtrc", S_IFREG|S_IRUGO, &bnad_debugfs_op_fwtrc, },
  426. { "fwsave", S_IFREG|S_IRUGO, &bnad_debugfs_op_fwsave, },
  427. { "regrd", S_IFREG|S_IRUGO|S_IWUSR, &bnad_debugfs_op_regrd, },
  428. { "regwr", S_IFREG|S_IWUSR, &bnad_debugfs_op_regwr, },
  429. { "drvinfo", S_IFREG|S_IRUGO, &bnad_debugfs_op_drvinfo, },
  430. };
  431. static struct dentry *bna_debugfs_root;
  432. static atomic_t bna_debugfs_port_count;
  433. /* Initialize debugfs interface for BNA */
  434. void
  435. bnad_debugfs_init(struct bnad *bnad)
  436. {
  437. const struct bnad_debugfs_entry *file;
  438. char name[64];
  439. int i;
  440. /* Setup the BNA debugfs root directory*/
  441. if (!bna_debugfs_root) {
  442. bna_debugfs_root = debugfs_create_dir("bna", NULL);
  443. atomic_set(&bna_debugfs_port_count, 0);
  444. if (!bna_debugfs_root) {
  445. pr_warn("BNA: debugfs root dir creation failed\n");
  446. return;
  447. }
  448. }
  449. /* Setup the pci_dev debugfs directory for the port */
  450. snprintf(name, sizeof(name), "pci_dev:%s", pci_name(bnad->pcidev));
  451. if (!bnad->port_debugfs_root) {
  452. bnad->port_debugfs_root =
  453. debugfs_create_dir(name, bna_debugfs_root);
  454. if (!bnad->port_debugfs_root) {
  455. pr_warn("bna pci_dev %s: root dir creation failed\n",
  456. pci_name(bnad->pcidev));
  457. return;
  458. }
  459. atomic_inc(&bna_debugfs_port_count);
  460. for (i = 0; i < ARRAY_SIZE(bnad_debugfs_files); i++) {
  461. file = &bnad_debugfs_files[i];
  462. bnad->bnad_dentry_files[i] =
  463. debugfs_create_file(file->name,
  464. file->mode,
  465. bnad->port_debugfs_root,
  466. bnad,
  467. file->fops);
  468. if (!bnad->bnad_dentry_files[i]) {
  469. pr_warn(
  470. "BNA pci_dev:%s: create %s entry failed\n",
  471. pci_name(bnad->pcidev), file->name);
  472. return;
  473. }
  474. }
  475. }
  476. }
  477. /* Uninitialize debugfs interface for BNA */
  478. void
  479. bnad_debugfs_uninit(struct bnad *bnad)
  480. {
  481. int i;
  482. for (i = 0; i < ARRAY_SIZE(bnad_debugfs_files); i++) {
  483. if (bnad->bnad_dentry_files[i]) {
  484. debugfs_remove(bnad->bnad_dentry_files[i]);
  485. bnad->bnad_dentry_files[i] = NULL;
  486. }
  487. }
  488. /* Remove the pci_dev debugfs directory for the port */
  489. if (bnad->port_debugfs_root) {
  490. debugfs_remove(bnad->port_debugfs_root);
  491. bnad->port_debugfs_root = NULL;
  492. atomic_dec(&bna_debugfs_port_count);
  493. }
  494. /* Remove the BNA debugfs root directory */
  495. if (atomic_read(&bna_debugfs_port_count) == 0) {
  496. debugfs_remove(bna_debugfs_root);
  497. bna_debugfs_root = NULL;
  498. }
  499. }