bcm43xx_debugfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. Broadcom BCM43xx wireless driver
  3. debugfs driver debugging code
  4. Copyright (c) 2005 Michael Buesch <mbuesch@freenet.de>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; see the file COPYING. If not, write to
  15. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  16. Boston, MA 02110-1301, USA.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/slab.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/pci.h>
  23. #include <asm/io.h>
  24. #include "bcm43xx.h"
  25. #include "bcm43xx_main.h"
  26. #include "bcm43xx_debugfs.h"
  27. #include "bcm43xx_dma.h"
  28. #include "bcm43xx_pio.h"
  29. #include "bcm43xx_xmit.h"
  30. #define REALLY_BIG_BUFFER_SIZE (1024*256)
  31. static struct bcm43xx_debugfs fs;
  32. static char really_big_buffer[REALLY_BIG_BUFFER_SIZE];
  33. static DECLARE_MUTEX(big_buffer_sem);
  34. static ssize_t write_file_dummy(struct file *file, const char __user *buf,
  35. size_t count, loff_t *ppos)
  36. {
  37. return count;
  38. }
  39. static int open_file_generic(struct inode *inode, struct file *file)
  40. {
  41. file->private_data = inode->i_private;
  42. return 0;
  43. }
  44. #define fappend(fmt, x...) pos += snprintf(buf + pos, len - pos, fmt , ##x)
  45. static ssize_t devinfo_read_file(struct file *file, char __user *userbuf,
  46. size_t count, loff_t *ppos)
  47. {
  48. const size_t len = REALLY_BIG_BUFFER_SIZE;
  49. struct bcm43xx_private *bcm = file->private_data;
  50. char *buf = really_big_buffer;
  51. size_t pos = 0;
  52. ssize_t res;
  53. struct net_device *net_dev;
  54. struct pci_dev *pci_dev;
  55. unsigned long flags;
  56. u16 tmp16;
  57. int i;
  58. down(&big_buffer_sem);
  59. mutex_lock(&bcm->mutex);
  60. spin_lock_irqsave(&bcm->irq_lock, flags);
  61. if (bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED) {
  62. fappend("Board not initialized.\n");
  63. goto out;
  64. }
  65. net_dev = bcm->net_dev;
  66. pci_dev = bcm->pci_dev;
  67. /* This is where the information is written to the "devinfo" file */
  68. fappend("*** %s devinfo ***\n", net_dev->name);
  69. fappend("vendor: 0x%04x device: 0x%04x\n",
  70. pci_dev->vendor, pci_dev->device);
  71. fappend("subsystem_vendor: 0x%04x subsystem_device: 0x%04x\n",
  72. pci_dev->subsystem_vendor, pci_dev->subsystem_device);
  73. fappend("IRQ: %d\n", bcm->irq);
  74. fappend("mmio_addr: 0x%p\n", bcm->mmio_addr);
  75. fappend("chip_id: 0x%04x chip_rev: 0x%02x\n", bcm->chip_id, bcm->chip_rev);
  76. if ((bcm->core_80211[0].rev >= 3) && (bcm43xx_read32(bcm, 0x0158) & (1 << 16)))
  77. fappend("Radio disabled by hardware!\n");
  78. if ((bcm->core_80211[0].rev < 3) && !(bcm43xx_read16(bcm, 0x049A) & (1 << 4)))
  79. fappend("Radio disabled by hardware!\n");
  80. fappend("board_vendor: 0x%04x board_type: 0x%04x\n", bcm->board_vendor,
  81. bcm->board_type);
  82. fappend("\nCores:\n");
  83. #define fappend_core(name, info) fappend("core \"" name "\" %s, %s, id: 0x%04x, " \
  84. "rev: 0x%02x, index: 0x%02x\n", \
  85. (info).available \
  86. ? "available" : "nonavailable", \
  87. (info).enabled \
  88. ? "enabled" : "disabled", \
  89. (info).id, (info).rev, (info).index)
  90. fappend_core("CHIPCOMMON", bcm->core_chipcommon);
  91. fappend_core("PCI", bcm->core_pci);
  92. fappend_core("first 80211", bcm->core_80211[0]);
  93. fappend_core("second 80211", bcm->core_80211[1]);
  94. #undef fappend_core
  95. tmp16 = bcm43xx_read16(bcm, BCM43xx_MMIO_GPIO_CONTROL);
  96. fappend("LEDs: ");
  97. for (i = 0; i < BCM43xx_NR_LEDS; i++)
  98. fappend("%d ", !!(tmp16 & (1 << i)));
  99. fappend("\n");
  100. out:
  101. spin_unlock_irqrestore(&bcm->irq_lock, flags);
  102. mutex_unlock(&bcm->mutex);
  103. res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
  104. up(&big_buffer_sem);
  105. return res;
  106. }
  107. static ssize_t drvinfo_read_file(struct file *file, char __user *userbuf,
  108. size_t count, loff_t *ppos)
  109. {
  110. const size_t len = REALLY_BIG_BUFFER_SIZE;
  111. char *buf = really_big_buffer;
  112. size_t pos = 0;
  113. ssize_t res;
  114. down(&big_buffer_sem);
  115. /* This is where the information is written to the "driver" file */
  116. fappend(KBUILD_MODNAME " driver\n");
  117. fappend("Compiled at: %s %s\n", __DATE__, __TIME__);
  118. res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
  119. up(&big_buffer_sem);
  120. return res;
  121. }
  122. static ssize_t spromdump_read_file(struct file *file, char __user *userbuf,
  123. size_t count, loff_t *ppos)
  124. {
  125. const size_t len = REALLY_BIG_BUFFER_SIZE;
  126. struct bcm43xx_private *bcm = file->private_data;
  127. char *buf = really_big_buffer;
  128. size_t pos = 0;
  129. ssize_t res;
  130. unsigned long flags;
  131. down(&big_buffer_sem);
  132. mutex_lock(&bcm->mutex);
  133. spin_lock_irqsave(&bcm->irq_lock, flags);
  134. if (bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED) {
  135. fappend("Board not initialized.\n");
  136. goto out;
  137. }
  138. /* This is where the information is written to the "sprom_dump" file */
  139. fappend("boardflags: 0x%04x\n", bcm->sprom.boardflags);
  140. out:
  141. spin_unlock_irqrestore(&bcm->irq_lock, flags);
  142. mutex_unlock(&bcm->mutex);
  143. res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
  144. up(&big_buffer_sem);
  145. return res;
  146. }
  147. static ssize_t tsf_read_file(struct file *file, char __user *userbuf,
  148. size_t count, loff_t *ppos)
  149. {
  150. const size_t len = REALLY_BIG_BUFFER_SIZE;
  151. struct bcm43xx_private *bcm = file->private_data;
  152. char *buf = really_big_buffer;
  153. size_t pos = 0;
  154. ssize_t res;
  155. unsigned long flags;
  156. u64 tsf;
  157. down(&big_buffer_sem);
  158. mutex_lock(&bcm->mutex);
  159. spin_lock_irqsave(&bcm->irq_lock, flags);
  160. if (bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED) {
  161. fappend("Board not initialized.\n");
  162. goto out;
  163. }
  164. bcm43xx_tsf_read(bcm, &tsf);
  165. fappend("0x%08x%08x\n",
  166. (unsigned int)((tsf & 0xFFFFFFFF00000000ULL) >> 32),
  167. (unsigned int)(tsf & 0xFFFFFFFFULL));
  168. out:
  169. spin_unlock_irqrestore(&bcm->irq_lock, flags);
  170. mutex_unlock(&bcm->mutex);
  171. res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
  172. up(&big_buffer_sem);
  173. return res;
  174. }
  175. static ssize_t tsf_write_file(struct file *file, const char __user *user_buf,
  176. size_t count, loff_t *ppos)
  177. {
  178. struct bcm43xx_private *bcm = file->private_data;
  179. char *buf = really_big_buffer;
  180. ssize_t buf_size;
  181. ssize_t res;
  182. unsigned long flags;
  183. u64 tsf;
  184. buf_size = min(count, sizeof (really_big_buffer) - 1);
  185. down(&big_buffer_sem);
  186. if (copy_from_user(buf, user_buf, buf_size)) {
  187. res = -EFAULT;
  188. goto out_up;
  189. }
  190. mutex_lock(&bcm->mutex);
  191. spin_lock_irqsave(&bcm->irq_lock, flags);
  192. if (bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED) {
  193. printk(KERN_INFO PFX "debugfs: Board not initialized.\n");
  194. res = -EFAULT;
  195. goto out_unlock;
  196. }
  197. if (sscanf(buf, "%lli", &tsf) != 1) {
  198. printk(KERN_INFO PFX "debugfs: invalid values for \"tsf\"\n");
  199. res = -EINVAL;
  200. goto out_unlock;
  201. }
  202. bcm43xx_tsf_write(bcm, tsf);
  203. mmiowb();
  204. res = buf_size;
  205. out_unlock:
  206. spin_unlock_irqrestore(&bcm->irq_lock, flags);
  207. mutex_unlock(&bcm->mutex);
  208. out_up:
  209. up(&big_buffer_sem);
  210. return res;
  211. }
  212. static ssize_t txstat_read_file(struct file *file, char __user *userbuf,
  213. size_t count, loff_t *ppos)
  214. {
  215. const size_t len = REALLY_BIG_BUFFER_SIZE;
  216. struct bcm43xx_private *bcm = file->private_data;
  217. char *buf = really_big_buffer;
  218. size_t pos = 0;
  219. ssize_t res;
  220. unsigned long flags;
  221. struct bcm43xx_dfsentry *e;
  222. struct bcm43xx_xmitstatus *status;
  223. int i, cnt, j = 0;
  224. down(&big_buffer_sem);
  225. mutex_lock(&bcm->mutex);
  226. spin_lock_irqsave(&bcm->irq_lock, flags);
  227. fappend("Last %d logged xmitstatus blobs (Latest first):\n\n",
  228. BCM43xx_NR_LOGGED_XMITSTATUS);
  229. e = bcm->dfsentry;
  230. if (e->xmitstatus_printing == 0) {
  231. /* At the beginning, make a copy of all data to avoid
  232. * concurrency, as this function is called multiple
  233. * times for big logs. Without copying, the data might
  234. * change between reads. This would result in total trash.
  235. */
  236. e->xmitstatus_printing = 1;
  237. e->saved_xmitstatus_ptr = e->xmitstatus_ptr;
  238. e->saved_xmitstatus_cnt = e->xmitstatus_cnt;
  239. memcpy(e->xmitstatus_print_buffer, e->xmitstatus_buffer,
  240. BCM43xx_NR_LOGGED_XMITSTATUS * sizeof(*(e->xmitstatus_buffer)));
  241. }
  242. i = e->saved_xmitstatus_ptr - 1;
  243. if (i < 0)
  244. i = BCM43xx_NR_LOGGED_XMITSTATUS - 1;
  245. cnt = e->saved_xmitstatus_cnt;
  246. while (cnt) {
  247. status = e->xmitstatus_print_buffer + i;
  248. fappend("0x%02x: cookie: 0x%04x, flags: 0x%02x, "
  249. "cnt1: 0x%02x, cnt2: 0x%02x, seq: 0x%04x, "
  250. "unk: 0x%04x\n", j,
  251. status->cookie, status->flags,
  252. status->cnt1, status->cnt2, status->seq,
  253. status->unknown);
  254. j++;
  255. cnt--;
  256. i--;
  257. if (i < 0)
  258. i = BCM43xx_NR_LOGGED_XMITSTATUS - 1;
  259. }
  260. spin_unlock_irqrestore(&bcm->irq_lock, flags);
  261. res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
  262. spin_lock_irqsave(&bcm->irq_lock, flags);
  263. if (*ppos == pos) {
  264. /* Done. Drop the copied data. */
  265. e->xmitstatus_printing = 0;
  266. }
  267. spin_unlock_irqrestore(&bcm->irq_lock, flags);
  268. mutex_unlock(&bcm->mutex);
  269. up(&big_buffer_sem);
  270. return res;
  271. }
  272. static ssize_t restart_write_file(struct file *file, const char __user *user_buf,
  273. size_t count, loff_t *ppos)
  274. {
  275. struct bcm43xx_private *bcm = file->private_data;
  276. char *buf = really_big_buffer;
  277. ssize_t buf_size;
  278. ssize_t res;
  279. unsigned long flags;
  280. buf_size = min(count, sizeof (really_big_buffer) - 1);
  281. down(&big_buffer_sem);
  282. if (copy_from_user(buf, user_buf, buf_size)) {
  283. res = -EFAULT;
  284. goto out_up;
  285. }
  286. mutex_lock(&(bcm)->mutex);
  287. spin_lock_irqsave(&(bcm)->irq_lock, flags);
  288. if (bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED) {
  289. printk(KERN_INFO PFX "debugfs: Board not initialized.\n");
  290. res = -EFAULT;
  291. goto out_unlock;
  292. }
  293. if (count > 0 && buf[0] == '1') {
  294. bcm43xx_controller_restart(bcm, "manually restarted");
  295. res = count;
  296. } else
  297. res = -EINVAL;
  298. out_unlock:
  299. spin_unlock_irqrestore(&(bcm)->irq_lock, flags);
  300. mutex_unlock(&(bcm)->mutex);
  301. out_up:
  302. up(&big_buffer_sem);
  303. return res;
  304. }
  305. #undef fappend
  306. static const struct file_operations devinfo_fops = {
  307. .read = devinfo_read_file,
  308. .write = write_file_dummy,
  309. .open = open_file_generic,
  310. };
  311. static const struct file_operations spromdump_fops = {
  312. .read = spromdump_read_file,
  313. .write = write_file_dummy,
  314. .open = open_file_generic,
  315. };
  316. static const struct file_operations drvinfo_fops = {
  317. .read = drvinfo_read_file,
  318. .write = write_file_dummy,
  319. .open = open_file_generic,
  320. };
  321. static const struct file_operations tsf_fops = {
  322. .read = tsf_read_file,
  323. .write = tsf_write_file,
  324. .open = open_file_generic,
  325. };
  326. static const struct file_operations txstat_fops = {
  327. .read = txstat_read_file,
  328. .write = write_file_dummy,
  329. .open = open_file_generic,
  330. };
  331. static const struct file_operations restart_fops = {
  332. .write = restart_write_file,
  333. .open = open_file_generic,
  334. };
  335. void bcm43xx_debugfs_add_device(struct bcm43xx_private *bcm)
  336. {
  337. struct bcm43xx_dfsentry *e;
  338. char devdir[IFNAMSIZ];
  339. assert(bcm);
  340. e = kzalloc(sizeof(*e), GFP_KERNEL);
  341. if (!e) {
  342. printk(KERN_ERR PFX "out of memory\n");
  343. return;
  344. }
  345. e->bcm = bcm;
  346. e->xmitstatus_buffer = kzalloc(BCM43xx_NR_LOGGED_XMITSTATUS
  347. * sizeof(*(e->xmitstatus_buffer)),
  348. GFP_KERNEL);
  349. if (!e->xmitstatus_buffer) {
  350. printk(KERN_ERR PFX "out of memory\n");
  351. kfree(e);
  352. return;
  353. }
  354. e->xmitstatus_print_buffer = kzalloc(BCM43xx_NR_LOGGED_XMITSTATUS
  355. * sizeof(*(e->xmitstatus_buffer)),
  356. GFP_KERNEL);
  357. if (!e->xmitstatus_print_buffer) {
  358. printk(KERN_ERR PFX "out of memory\n");
  359. kfree(e);
  360. return;
  361. }
  362. bcm->dfsentry = e;
  363. strncpy(devdir, bcm->net_dev->name, ARRAY_SIZE(devdir));
  364. e->subdir = debugfs_create_dir(devdir, fs.root);
  365. e->dentry_devinfo = debugfs_create_file("devinfo", 0444, e->subdir,
  366. bcm, &devinfo_fops);
  367. if (!e->dentry_devinfo)
  368. printk(KERN_ERR PFX "debugfs: creating \"devinfo\" for \"%s\" failed!\n", devdir);
  369. e->dentry_spromdump = debugfs_create_file("sprom_dump", 0444, e->subdir,
  370. bcm, &spromdump_fops);
  371. if (!e->dentry_spromdump)
  372. printk(KERN_ERR PFX "debugfs: creating \"sprom_dump\" for \"%s\" failed!\n", devdir);
  373. e->dentry_tsf = debugfs_create_file("tsf", 0666, e->subdir,
  374. bcm, &tsf_fops);
  375. if (!e->dentry_tsf)
  376. printk(KERN_ERR PFX "debugfs: creating \"tsf\" for \"%s\" failed!\n", devdir);
  377. e->dentry_txstat = debugfs_create_file("tx_status", 0444, e->subdir,
  378. bcm, &txstat_fops);
  379. if (!e->dentry_txstat)
  380. printk(KERN_ERR PFX "debugfs: creating \"tx_status\" for \"%s\" failed!\n", devdir);
  381. e->dentry_restart = debugfs_create_file("restart", 0222, e->subdir,
  382. bcm, &restart_fops);
  383. if (!e->dentry_restart)
  384. printk(KERN_ERR PFX "debugfs: creating \"restart\" for \"%s\" failed!\n", devdir);
  385. }
  386. void bcm43xx_debugfs_remove_device(struct bcm43xx_private *bcm)
  387. {
  388. struct bcm43xx_dfsentry *e;
  389. if (!bcm)
  390. return;
  391. e = bcm->dfsentry;
  392. assert(e);
  393. debugfs_remove(e->dentry_spromdump);
  394. debugfs_remove(e->dentry_devinfo);
  395. debugfs_remove(e->dentry_tsf);
  396. debugfs_remove(e->dentry_txstat);
  397. debugfs_remove(e->dentry_restart);
  398. debugfs_remove(e->subdir);
  399. kfree(e->xmitstatus_buffer);
  400. kfree(e->xmitstatus_print_buffer);
  401. kfree(e);
  402. }
  403. void bcm43xx_debugfs_log_txstat(struct bcm43xx_private *bcm,
  404. struct bcm43xx_xmitstatus *status)
  405. {
  406. struct bcm43xx_dfsentry *e;
  407. struct bcm43xx_xmitstatus *savedstatus;
  408. /* This is protected by bcm->_lock */
  409. e = bcm->dfsentry;
  410. assert(e);
  411. savedstatus = e->xmitstatus_buffer + e->xmitstatus_ptr;
  412. memcpy(savedstatus, status, sizeof(*status));
  413. e->xmitstatus_ptr++;
  414. if (e->xmitstatus_ptr >= BCM43xx_NR_LOGGED_XMITSTATUS)
  415. e->xmitstatus_ptr = 0;
  416. if (e->xmitstatus_cnt < BCM43xx_NR_LOGGED_XMITSTATUS)
  417. e->xmitstatus_cnt++;
  418. }
  419. void bcm43xx_debugfs_init(void)
  420. {
  421. memset(&fs, 0, sizeof(fs));
  422. fs.root = debugfs_create_dir(KBUILD_MODNAME, NULL);
  423. if (!fs.root)
  424. printk(KERN_ERR PFX "debugfs: creating \"" KBUILD_MODNAME "\" subdir failed!\n");
  425. fs.dentry_driverinfo = debugfs_create_file("driver", 0444, fs.root, NULL, &drvinfo_fops);
  426. if (!fs.dentry_driverinfo)
  427. printk(KERN_ERR PFX "debugfs: creating \"" KBUILD_MODNAME "/driver\" failed!\n");
  428. }
  429. void bcm43xx_debugfs_exit(void)
  430. {
  431. debugfs_remove(fs.dentry_driverinfo);
  432. debugfs_remove(fs.root);
  433. }
  434. void bcm43xx_printk_dump(const char *data,
  435. size_t size,
  436. const char *description)
  437. {
  438. size_t i;
  439. char c;
  440. printk(KERN_INFO PFX "Data dump (%s, %zd bytes):",
  441. description, size);
  442. for (i = 0; i < size; i++) {
  443. c = data[i];
  444. if (i % 8 == 0)
  445. printk("\n" KERN_INFO PFX "0x%08zx: 0x%02x, ", i, c & 0xff);
  446. else
  447. printk("0x%02x, ", c & 0xff);
  448. }
  449. printk("\n");
  450. }
  451. void bcm43xx_printk_bitdump(const unsigned char *data,
  452. size_t bytes, int msb_to_lsb,
  453. const char *description)
  454. {
  455. size_t i;
  456. int j;
  457. const unsigned char *d;
  458. printk(KERN_INFO PFX "*** Bitdump (%s, %zd bytes, %s) ***",
  459. description, bytes, msb_to_lsb ? "MSB to LSB" : "LSB to MSB");
  460. for (i = 0; i < bytes; i++) {
  461. d = data + i;
  462. if (i % 8 == 0)
  463. printk("\n" KERN_INFO PFX "0x%08zx: ", i);
  464. if (msb_to_lsb) {
  465. for (j = 7; j >= 0; j--) {
  466. if (*d & (1 << j))
  467. printk("1");
  468. else
  469. printk("0");
  470. }
  471. } else {
  472. for (j = 0; j < 8; j++) {
  473. if (*d & (1 << j))
  474. printk("1");
  475. else
  476. printk("0");
  477. }
  478. }
  479. printk(" ");
  480. }
  481. printk("\n");
  482. }