debugfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Broadcom B43legacy wireless driver
  4. debugfs driver debugging code
  5. Copyright (c) 2005-2007 Michael Buesch <m@bues.ch>
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/slab.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/pci.h>
  12. #include <linux/mutex.h>
  13. #include "b43legacy.h"
  14. #include "main.h"
  15. #include "debugfs.h"
  16. #include "dma.h"
  17. #include "pio.h"
  18. #include "xmit.h"
  19. /* The root directory. */
  20. static struct dentry *rootdir;
  21. struct b43legacy_debugfs_fops {
  22. ssize_t (*read)(struct b43legacy_wldev *dev, char *buf, size_t bufsize);
  23. int (*write)(struct b43legacy_wldev *dev, const char *buf, size_t count);
  24. struct file_operations fops;
  25. /* Offset of struct b43legacy_dfs_file in struct b43legacy_dfsentry */
  26. size_t file_struct_offset;
  27. /* Take wl->irq_lock before calling read/write? */
  28. bool take_irqlock;
  29. };
  30. static inline
  31. struct b43legacy_dfs_file * fops_to_dfs_file(struct b43legacy_wldev *dev,
  32. const struct b43legacy_debugfs_fops *dfops)
  33. {
  34. void *p;
  35. p = dev->dfsentry;
  36. p += dfops->file_struct_offset;
  37. return p;
  38. }
  39. #define fappend(fmt, x...) \
  40. do { \
  41. if (bufsize - count) \
  42. count += scnprintf(buf + count, \
  43. bufsize - count, \
  44. fmt , ##x); \
  45. else \
  46. printk(KERN_ERR "b43legacy: fappend overflow\n"); \
  47. } while (0)
  48. /* wl->irq_lock is locked */
  49. static ssize_t tsf_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
  50. {
  51. ssize_t count = 0;
  52. u64 tsf;
  53. b43legacy_tsf_read(dev, &tsf);
  54. fappend("0x%08x%08x\n",
  55. (unsigned int)((tsf & 0xFFFFFFFF00000000ULL) >> 32),
  56. (unsigned int)(tsf & 0xFFFFFFFFULL));
  57. return count;
  58. }
  59. /* wl->irq_lock is locked */
  60. static int tsf_write_file(struct b43legacy_wldev *dev, const char *buf, size_t count)
  61. {
  62. u64 tsf;
  63. if (sscanf(buf, "%llu", (unsigned long long *)(&tsf)) != 1)
  64. return -EINVAL;
  65. b43legacy_tsf_write(dev, tsf);
  66. return 0;
  67. }
  68. /* wl->irq_lock is locked */
  69. static ssize_t ucode_regs_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
  70. {
  71. ssize_t count = 0;
  72. int i;
  73. for (i = 0; i < 64; i++) {
  74. fappend("r%d = 0x%04x\n", i,
  75. b43legacy_shm_read16(dev, B43legacy_SHM_WIRELESS, i));
  76. }
  77. return count;
  78. }
  79. /* wl->irq_lock is locked */
  80. static ssize_t shm_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
  81. {
  82. ssize_t count = 0;
  83. int i;
  84. u16 tmp;
  85. __le16 *le16buf = (__le16 *)buf;
  86. for (i = 0; i < 0x1000; i++) {
  87. if (bufsize < sizeof(tmp))
  88. break;
  89. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 2 * i);
  90. le16buf[i] = cpu_to_le16(tmp);
  91. count += sizeof(tmp);
  92. bufsize -= sizeof(tmp);
  93. }
  94. return count;
  95. }
  96. static ssize_t txstat_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
  97. {
  98. struct b43legacy_txstatus_log *log = &dev->dfsentry->txstatlog;
  99. ssize_t count = 0;
  100. unsigned long flags;
  101. int i, idx;
  102. struct b43legacy_txstatus *stat;
  103. spin_lock_irqsave(&log->lock, flags);
  104. if (log->end < 0) {
  105. fappend("Nothing transmitted, yet\n");
  106. goto out_unlock;
  107. }
  108. fappend("b43legacy TX status reports:\n\n"
  109. "index | cookie | seq | phy_stat | frame_count | "
  110. "rts_count | supp_reason | pm_indicated | "
  111. "intermediate | for_ampdu | acked\n" "---\n");
  112. i = log->end + 1;
  113. idx = 0;
  114. while (1) {
  115. if (i == B43legacy_NR_LOGGED_TXSTATUS)
  116. i = 0;
  117. stat = &(log->log[i]);
  118. if (stat->cookie) {
  119. fappend("%03d | "
  120. "0x%04X | 0x%04X | 0x%02X | "
  121. "0x%X | 0x%X | "
  122. "%u | %u | "
  123. "%u | %u | %u\n",
  124. idx,
  125. stat->cookie, stat->seq, stat->phy_stat,
  126. stat->frame_count, stat->rts_count,
  127. stat->supp_reason, stat->pm_indicated,
  128. stat->intermediate, stat->for_ampdu,
  129. stat->acked);
  130. idx++;
  131. }
  132. if (i == log->end)
  133. break;
  134. i++;
  135. }
  136. out_unlock:
  137. spin_unlock_irqrestore(&log->lock, flags);
  138. return count;
  139. }
  140. /* wl->irq_lock is locked */
  141. static int restart_write_file(struct b43legacy_wldev *dev, const char *buf, size_t count)
  142. {
  143. int err = 0;
  144. if (count > 0 && buf[0] == '1') {
  145. b43legacy_controller_restart(dev, "manually restarted");
  146. } else
  147. err = -EINVAL;
  148. return err;
  149. }
  150. #undef fappend
  151. static ssize_t b43legacy_debugfs_read(struct file *file, char __user *userbuf,
  152. size_t count, loff_t *ppos)
  153. {
  154. struct b43legacy_wldev *dev;
  155. struct b43legacy_debugfs_fops *dfops;
  156. struct b43legacy_dfs_file *dfile;
  157. ssize_t ret;
  158. char *buf;
  159. const size_t bufsize = 1024 * 16; /* 16 KiB buffer */
  160. const size_t buforder = get_order(bufsize);
  161. int err = 0;
  162. if (!count)
  163. return 0;
  164. dev = file->private_data;
  165. if (!dev)
  166. return -ENODEV;
  167. mutex_lock(&dev->wl->mutex);
  168. if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
  169. err = -ENODEV;
  170. goto out_unlock;
  171. }
  172. dfops = container_of(debugfs_real_fops(file),
  173. struct b43legacy_debugfs_fops, fops);
  174. if (!dfops->read) {
  175. err = -ENOSYS;
  176. goto out_unlock;
  177. }
  178. dfile = fops_to_dfs_file(dev, dfops);
  179. if (!dfile->buffer) {
  180. buf = (char *)__get_free_pages(GFP_KERNEL, buforder);
  181. if (!buf) {
  182. err = -ENOMEM;
  183. goto out_unlock;
  184. }
  185. memset(buf, 0, bufsize);
  186. if (dfops->take_irqlock) {
  187. spin_lock_irq(&dev->wl->irq_lock);
  188. ret = dfops->read(dev, buf, bufsize);
  189. spin_unlock_irq(&dev->wl->irq_lock);
  190. } else
  191. ret = dfops->read(dev, buf, bufsize);
  192. if (ret <= 0) {
  193. free_pages((unsigned long)buf, buforder);
  194. err = ret;
  195. goto out_unlock;
  196. }
  197. dfile->data_len = ret;
  198. dfile->buffer = buf;
  199. }
  200. ret = simple_read_from_buffer(userbuf, count, ppos,
  201. dfile->buffer,
  202. dfile->data_len);
  203. if (*ppos >= dfile->data_len) {
  204. free_pages((unsigned long)dfile->buffer, buforder);
  205. dfile->buffer = NULL;
  206. dfile->data_len = 0;
  207. }
  208. out_unlock:
  209. mutex_unlock(&dev->wl->mutex);
  210. return err ? err : ret;
  211. }
  212. static ssize_t b43legacy_debugfs_write(struct file *file,
  213. const char __user *userbuf,
  214. size_t count, loff_t *ppos)
  215. {
  216. struct b43legacy_wldev *dev;
  217. struct b43legacy_debugfs_fops *dfops;
  218. char *buf;
  219. int err = 0;
  220. if (!count)
  221. return 0;
  222. if (count > PAGE_SIZE)
  223. return -E2BIG;
  224. dev = file->private_data;
  225. if (!dev)
  226. return -ENODEV;
  227. mutex_lock(&dev->wl->mutex);
  228. if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
  229. err = -ENODEV;
  230. goto out_unlock;
  231. }
  232. dfops = container_of(debugfs_real_fops(file),
  233. struct b43legacy_debugfs_fops, fops);
  234. if (!dfops->write) {
  235. err = -ENOSYS;
  236. goto out_unlock;
  237. }
  238. buf = (char *)get_zeroed_page(GFP_KERNEL);
  239. if (!buf) {
  240. err = -ENOMEM;
  241. goto out_unlock;
  242. }
  243. if (copy_from_user(buf, userbuf, count)) {
  244. err = -EFAULT;
  245. goto out_freepage;
  246. }
  247. if (dfops->take_irqlock) {
  248. spin_lock_irq(&dev->wl->irq_lock);
  249. err = dfops->write(dev, buf, count);
  250. spin_unlock_irq(&dev->wl->irq_lock);
  251. } else
  252. err = dfops->write(dev, buf, count);
  253. if (err)
  254. goto out_freepage;
  255. out_freepage:
  256. free_page((unsigned long)buf);
  257. out_unlock:
  258. mutex_unlock(&dev->wl->mutex);
  259. return err ? err : count;
  260. }
  261. #define B43legacy_DEBUGFS_FOPS(name, _read, _write, _take_irqlock) \
  262. static struct b43legacy_debugfs_fops fops_##name = { \
  263. .read = _read, \
  264. .write = _write, \
  265. .fops = { \
  266. .open = simple_open, \
  267. .read = b43legacy_debugfs_read, \
  268. .write = b43legacy_debugfs_write, \
  269. .llseek = generic_file_llseek, \
  270. }, \
  271. .file_struct_offset = offsetof(struct b43legacy_dfsentry, \
  272. file_##name), \
  273. .take_irqlock = _take_irqlock, \
  274. }
  275. B43legacy_DEBUGFS_FOPS(tsf, tsf_read_file, tsf_write_file, 1);
  276. B43legacy_DEBUGFS_FOPS(ucode_regs, ucode_regs_read_file, NULL, 1);
  277. B43legacy_DEBUGFS_FOPS(shm, shm_read_file, NULL, 1);
  278. B43legacy_DEBUGFS_FOPS(txstat, txstat_read_file, NULL, 0);
  279. B43legacy_DEBUGFS_FOPS(restart, NULL, restart_write_file, 1);
  280. int b43legacy_debug(struct b43legacy_wldev *dev, enum b43legacy_dyndbg feature)
  281. {
  282. return !!(dev->dfsentry && dev->dfsentry->dyn_debug[feature]);
  283. }
  284. static void b43legacy_add_dynamic_debug(struct b43legacy_wldev *dev)
  285. {
  286. struct b43legacy_dfsentry *e = dev->dfsentry;
  287. #define add_dyn_dbg(name, id, initstate) do { \
  288. e->dyn_debug[id] = (initstate); \
  289. debugfs_create_bool(name, 0600, e->subdir, \
  290. &(e->dyn_debug[id])); \
  291. } while (0)
  292. add_dyn_dbg("debug_xmitpower", B43legacy_DBG_XMITPOWER, false);
  293. add_dyn_dbg("debug_dmaoverflow", B43legacy_DBG_DMAOVERFLOW, false);
  294. add_dyn_dbg("debug_dmaverbose", B43legacy_DBG_DMAVERBOSE, false);
  295. add_dyn_dbg("debug_pwork_fast", B43legacy_DBG_PWORK_FAST, false);
  296. add_dyn_dbg("debug_pwork_stop", B43legacy_DBG_PWORK_STOP, false);
  297. #undef add_dyn_dbg
  298. }
  299. void b43legacy_debugfs_add_device(struct b43legacy_wldev *dev)
  300. {
  301. struct b43legacy_dfsentry *e;
  302. struct b43legacy_txstatus_log *log;
  303. char devdir[16];
  304. B43legacy_WARN_ON(!dev);
  305. e = kzalloc(sizeof(*e), GFP_KERNEL);
  306. if (!e) {
  307. b43legacyerr(dev->wl, "debugfs: add device OOM\n");
  308. return;
  309. }
  310. e->dev = dev;
  311. log = &e->txstatlog;
  312. log->log = kcalloc(B43legacy_NR_LOGGED_TXSTATUS,
  313. sizeof(struct b43legacy_txstatus), GFP_KERNEL);
  314. if (!log->log) {
  315. b43legacyerr(dev->wl, "debugfs: add device txstatus OOM\n");
  316. kfree(e);
  317. return;
  318. }
  319. log->end = -1;
  320. spin_lock_init(&log->lock);
  321. dev->dfsentry = e;
  322. snprintf(devdir, sizeof(devdir), "%s", wiphy_name(dev->wl->hw->wiphy));
  323. e->subdir = debugfs_create_dir(devdir, rootdir);
  324. #define ADD_FILE(name, mode) \
  325. do { \
  326. debugfs_create_file(__stringify(name), mode, \
  327. e->subdir, dev, \
  328. &fops_##name.fops); \
  329. } while (0)
  330. ADD_FILE(tsf, 0600);
  331. ADD_FILE(ucode_regs, 0400);
  332. ADD_FILE(shm, 0400);
  333. ADD_FILE(txstat, 0400);
  334. ADD_FILE(restart, 0200);
  335. #undef ADD_FILE
  336. b43legacy_add_dynamic_debug(dev);
  337. }
  338. void b43legacy_debugfs_remove_device(struct b43legacy_wldev *dev)
  339. {
  340. struct b43legacy_dfsentry *e;
  341. if (!dev)
  342. return;
  343. e = dev->dfsentry;
  344. if (!e)
  345. return;
  346. debugfs_remove(e->subdir);
  347. kfree(e->txstatlog.log);
  348. kfree(e);
  349. }
  350. void b43legacy_debugfs_log_txstat(struct b43legacy_wldev *dev,
  351. const struct b43legacy_txstatus *status)
  352. {
  353. struct b43legacy_dfsentry *e = dev->dfsentry;
  354. struct b43legacy_txstatus_log *log;
  355. struct b43legacy_txstatus *cur;
  356. int i;
  357. if (!e)
  358. return;
  359. log = &e->txstatlog;
  360. B43legacy_WARN_ON(!irqs_disabled());
  361. spin_lock(&log->lock);
  362. i = log->end + 1;
  363. if (i == B43legacy_NR_LOGGED_TXSTATUS)
  364. i = 0;
  365. log->end = i;
  366. cur = &(log->log[i]);
  367. memcpy(cur, status, sizeof(*cur));
  368. spin_unlock(&log->lock);
  369. }
  370. void b43legacy_debugfs_init(void)
  371. {
  372. rootdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  373. }
  374. void b43legacy_debugfs_exit(void)
  375. {
  376. debugfs_remove(rootdir);
  377. }