debug.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /*
  2. * Atheros CARL9170 driver
  3. *
  4. * debug(fs) probing
  5. *
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; see the file COPYING. If not, see
  21. * http://www.gnu.org/licenses/.
  22. *
  23. * This file incorporates work covered by the following copyright and
  24. * permission notice:
  25. * Copyright (c) 2008-2009 Atheros Communications, Inc.
  26. *
  27. * Permission to use, copy, modify, and/or distribute this software for any
  28. * purpose with or without fee is hereby granted, provided that the above
  29. * copyright notice and this permission notice appear in all copies.
  30. *
  31. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  32. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  33. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  34. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  35. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  36. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  37. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  38. */
  39. #include <linux/init.h>
  40. #include <linux/slab.h>
  41. #include <linux/module.h>
  42. #include <linux/seq_file.h>
  43. #include <linux/vmalloc.h>
  44. #include "carl9170.h"
  45. #include "cmd.h"
  46. #define ADD(buf, off, max, fmt, args...) \
  47. off += snprintf(&buf[off], max - off, fmt, ##args);
  48. static int carl9170_debugfs_open(struct inode *inode, struct file *file)
  49. {
  50. file->private_data = inode->i_private;
  51. return 0;
  52. }
  53. struct carl9170_debugfs_fops {
  54. unsigned int read_bufsize;
  55. mode_t attr;
  56. char *(*read)(struct ar9170 *ar, char *buf, size_t bufsize,
  57. ssize_t *len);
  58. ssize_t (*write)(struct ar9170 *aru, const char *buf, size_t size);
  59. const struct file_operations fops;
  60. enum carl9170_device_state req_dev_state;
  61. };
  62. static ssize_t carl9170_debugfs_read(struct file *file, char __user *userbuf,
  63. size_t count, loff_t *ppos)
  64. {
  65. struct carl9170_debugfs_fops *dfops;
  66. struct ar9170 *ar;
  67. char *buf = NULL, *res_buf = NULL;
  68. ssize_t ret = 0;
  69. int err = 0;
  70. if (!count)
  71. return 0;
  72. ar = file->private_data;
  73. if (!ar)
  74. return -ENODEV;
  75. dfops = container_of(file->f_op, struct carl9170_debugfs_fops, fops);
  76. if (!dfops->read)
  77. return -ENOSYS;
  78. if (dfops->read_bufsize) {
  79. buf = vmalloc(dfops->read_bufsize);
  80. if (!buf)
  81. return -ENOMEM;
  82. }
  83. mutex_lock(&ar->mutex);
  84. if (!CHK_DEV_STATE(ar, dfops->req_dev_state)) {
  85. err = -ENODEV;
  86. res_buf = buf;
  87. goto out_free;
  88. }
  89. res_buf = dfops->read(ar, buf, dfops->read_bufsize, &ret);
  90. if (ret > 0)
  91. err = simple_read_from_buffer(userbuf, count, ppos,
  92. res_buf, ret);
  93. else
  94. err = ret;
  95. WARN_ON_ONCE(dfops->read_bufsize && (res_buf != buf));
  96. out_free:
  97. vfree(res_buf);
  98. mutex_unlock(&ar->mutex);
  99. return err;
  100. }
  101. static ssize_t carl9170_debugfs_write(struct file *file,
  102. const char __user *userbuf, size_t count, loff_t *ppos)
  103. {
  104. struct carl9170_debugfs_fops *dfops;
  105. struct ar9170 *ar;
  106. char *buf = NULL;
  107. int err = 0;
  108. if (!count)
  109. return 0;
  110. if (count > PAGE_SIZE)
  111. return -E2BIG;
  112. ar = file->private_data;
  113. if (!ar)
  114. return -ENODEV;
  115. dfops = container_of(file->f_op, struct carl9170_debugfs_fops, fops);
  116. if (!dfops->write)
  117. return -ENOSYS;
  118. buf = vmalloc(count);
  119. if (!buf)
  120. return -ENOMEM;
  121. if (copy_from_user(buf, userbuf, count)) {
  122. err = -EFAULT;
  123. goto out_free;
  124. }
  125. if (mutex_trylock(&ar->mutex) == 0) {
  126. err = -EAGAIN;
  127. goto out_free;
  128. }
  129. if (!CHK_DEV_STATE(ar, dfops->req_dev_state)) {
  130. err = -ENODEV;
  131. goto out_unlock;
  132. }
  133. err = dfops->write(ar, buf, count);
  134. if (err)
  135. goto out_unlock;
  136. out_unlock:
  137. mutex_unlock(&ar->mutex);
  138. out_free:
  139. vfree(buf);
  140. return err;
  141. }
  142. #define __DEBUGFS_DECLARE_FILE(name, _read, _write, _read_bufsize, \
  143. _attr, _dstate) \
  144. static const struct carl9170_debugfs_fops carl_debugfs_##name ##_ops = {\
  145. .read_bufsize = _read_bufsize, \
  146. .read = _read, \
  147. .write = _write, \
  148. .attr = _attr, \
  149. .req_dev_state = _dstate, \
  150. .fops = { \
  151. .open = carl9170_debugfs_open, \
  152. .read = carl9170_debugfs_read, \
  153. .write = carl9170_debugfs_write, \
  154. .owner = THIS_MODULE \
  155. }, \
  156. }
  157. #define DEBUGFS_DECLARE_FILE(name, _read, _write, _read_bufsize, _attr) \
  158. __DEBUGFS_DECLARE_FILE(name, _read, _write, _read_bufsize, \
  159. _attr, CARL9170_STARTED) \
  160. #define DEBUGFS_DECLARE_RO_FILE(name, _read_bufsize) \
  161. DEBUGFS_DECLARE_FILE(name, carl9170_debugfs_##name ##_read, \
  162. NULL, _read_bufsize, S_IRUSR)
  163. #define DEBUGFS_DECLARE_WO_FILE(name) \
  164. DEBUGFS_DECLARE_FILE(name, NULL, carl9170_debugfs_##name ##_write,\
  165. 0, S_IWUSR)
  166. #define DEBUGFS_DECLARE_RW_FILE(name, _read_bufsize) \
  167. DEBUGFS_DECLARE_FILE(name, carl9170_debugfs_##name ##_read, \
  168. carl9170_debugfs_##name ##_write, \
  169. _read_bufsize, S_IRUSR | S_IWUSR)
  170. #define __DEBUGFS_DECLARE_RW_FILE(name, _read_bufsize, _dstate) \
  171. __DEBUGFS_DECLARE_FILE(name, carl9170_debugfs_##name ##_read, \
  172. carl9170_debugfs_##name ##_write, \
  173. _read_bufsize, S_IRUSR | S_IWUSR, _dstate)
  174. #define DEBUGFS_READONLY_FILE(name, _read_bufsize, fmt, value...) \
  175. static char *carl9170_debugfs_ ##name ## _read(struct ar9170 *ar, \
  176. char *buf, size_t buf_size,\
  177. ssize_t *len) \
  178. { \
  179. ADD(buf, *len, buf_size, fmt "\n", ##value); \
  180. return buf; \
  181. } \
  182. DEBUGFS_DECLARE_RO_FILE(name, _read_bufsize)
  183. static char *carl9170_debugfs_mem_usage_read(struct ar9170 *ar, char *buf,
  184. size_t bufsize, ssize_t *len)
  185. {
  186. ADD(buf, *len, bufsize, "jar: [");
  187. spin_lock_bh(&ar->mem_lock);
  188. *len += bitmap_scnprintf(&buf[*len], bufsize - *len,
  189. ar->mem_bitmap, ar->fw.mem_blocks);
  190. ADD(buf, *len, bufsize, "]\n");
  191. ADD(buf, *len, bufsize, "cookies: used:%3d / total:%3d, allocs:%d\n",
  192. bitmap_weight(ar->mem_bitmap, ar->fw.mem_blocks),
  193. ar->fw.mem_blocks, atomic_read(&ar->mem_allocs));
  194. ADD(buf, *len, bufsize, "memory: free:%3d (%3d KiB) / total:%3d KiB)\n",
  195. atomic_read(&ar->mem_free_blocks),
  196. (atomic_read(&ar->mem_free_blocks) * ar->fw.mem_block_size) / 1024,
  197. (ar->fw.mem_blocks * ar->fw.mem_block_size) / 1024);
  198. spin_unlock_bh(&ar->mem_lock);
  199. return buf;
  200. }
  201. DEBUGFS_DECLARE_RO_FILE(mem_usage, 512);
  202. static char *carl9170_debugfs_qos_stat_read(struct ar9170 *ar, char *buf,
  203. size_t bufsize, ssize_t *len)
  204. {
  205. ADD(buf, *len, bufsize, "%s QoS AC\n", modparam_noht ? "Hardware" :
  206. "Software");
  207. ADD(buf, *len, bufsize, "[ VO VI "
  208. " BE BK ]\n");
  209. spin_lock_bh(&ar->tx_stats_lock);
  210. ADD(buf, *len, bufsize, "[length/limit length/limit "
  211. "length/limit length/limit ]\n"
  212. "[ %3d/%3d %3d/%3d "
  213. " %3d/%3d %3d/%3d ]\n\n",
  214. ar->tx_stats[0].len, ar->tx_stats[0].limit,
  215. ar->tx_stats[1].len, ar->tx_stats[1].limit,
  216. ar->tx_stats[2].len, ar->tx_stats[2].limit,
  217. ar->tx_stats[3].len, ar->tx_stats[3].limit);
  218. ADD(buf, *len, bufsize, "[ total total "
  219. " total total ]\n"
  220. "[%10d %10d %10d %10d ]\n\n",
  221. ar->tx_stats[0].count, ar->tx_stats[1].count,
  222. ar->tx_stats[2].count, ar->tx_stats[3].count);
  223. spin_unlock_bh(&ar->tx_stats_lock);
  224. ADD(buf, *len, bufsize, "[ pend/waittx pend/waittx "
  225. " pend/waittx pend/waittx]\n"
  226. "[ %3d/%3d %3d/%3d "
  227. " %3d/%3d %3d/%3d ]\n\n",
  228. skb_queue_len(&ar->tx_pending[0]),
  229. skb_queue_len(&ar->tx_status[0]),
  230. skb_queue_len(&ar->tx_pending[1]),
  231. skb_queue_len(&ar->tx_status[1]),
  232. skb_queue_len(&ar->tx_pending[2]),
  233. skb_queue_len(&ar->tx_status[2]),
  234. skb_queue_len(&ar->tx_pending[3]),
  235. skb_queue_len(&ar->tx_status[3]));
  236. return buf;
  237. }
  238. DEBUGFS_DECLARE_RO_FILE(qos_stat, 512);
  239. static void carl9170_debugfs_format_frame(struct ar9170 *ar,
  240. struct sk_buff *skb, const char *prefix, char *buf,
  241. ssize_t *off, ssize_t bufsize)
  242. {
  243. struct _carl9170_tx_superframe *txc = (void *) skb->data;
  244. struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
  245. struct carl9170_tx_info *arinfo = (void *) txinfo->rate_driver_data;
  246. struct ieee80211_hdr *hdr = (void *) txc->frame_data;
  247. ADD(buf, *off, bufsize, "%s %p, c:%2x, DA:%pM, sq:%4d, mc:%.4x, "
  248. "pc:%.8x, to:%d ms\n", prefix, skb, txc->s.cookie,
  249. ieee80211_get_DA(hdr), get_seq_h(hdr),
  250. le16_to_cpu(txc->f.mac_control), le32_to_cpu(txc->f.phy_control),
  251. jiffies_to_msecs(jiffies - arinfo->timeout));
  252. }
  253. static char *carl9170_debugfs_ampdu_state_read(struct ar9170 *ar, char *buf,
  254. size_t bufsize, ssize_t *len)
  255. {
  256. struct carl9170_sta_tid *iter;
  257. struct sk_buff *skb;
  258. int cnt = 0, fc;
  259. int offset;
  260. rcu_read_lock();
  261. list_for_each_entry_rcu(iter, &ar->tx_ampdu_list, list) {
  262. spin_lock_bh(&iter->lock);
  263. ADD(buf, *len, bufsize, "Entry: #%2d TID:%1d, BSN:%4d, "
  264. "SNX:%4d, HSN:%4d, BAW:%2d, state:%1d, toggles:%d\n",
  265. cnt, iter->tid, iter->bsn, iter->snx, iter->hsn,
  266. iter->max, iter->state, iter->counter);
  267. ADD(buf, *len, bufsize, "\tWindow: [");
  268. *len += bitmap_scnprintf(&buf[*len], bufsize - *len,
  269. iter->bitmap, CARL9170_BAW_BITS);
  270. #define BM_STR_OFF(offset) \
  271. ((CARL9170_BAW_BITS - (offset) - 1) / 4 + \
  272. (CARL9170_BAW_BITS - (offset) - 1) / 32 + 1)
  273. ADD(buf, *len, bufsize, ",W]\n");
  274. offset = BM_STR_OFF(0);
  275. ADD(buf, *len, bufsize, "\tBase Seq: %*s\n", offset, "T");
  276. offset = BM_STR_OFF(SEQ_DIFF(iter->snx, iter->bsn));
  277. ADD(buf, *len, bufsize, "\tNext Seq: %*s\n", offset, "W");
  278. offset = BM_STR_OFF(((int)iter->hsn - (int)iter->bsn) %
  279. CARL9170_BAW_BITS);
  280. ADD(buf, *len, bufsize, "\tLast Seq: %*s\n", offset, "N");
  281. ADD(buf, *len, bufsize, "\tPre-Aggregation reorder buffer: "
  282. " currently queued:%d\n", skb_queue_len(&iter->queue));
  283. fc = 0;
  284. skb_queue_walk(&iter->queue, skb) {
  285. char prefix[32];
  286. snprintf(prefix, sizeof(prefix), "\t\t%3d :", fc);
  287. carl9170_debugfs_format_frame(ar, skb, prefix, buf,
  288. len, bufsize);
  289. fc++;
  290. }
  291. spin_unlock_bh(&iter->lock);
  292. cnt++;
  293. }
  294. rcu_read_unlock();
  295. return buf;
  296. }
  297. DEBUGFS_DECLARE_RO_FILE(ampdu_state, 8000);
  298. static void carl9170_debugfs_queue_dump(struct ar9170 *ar, char *buf,
  299. ssize_t *len, size_t bufsize, struct sk_buff_head *queue)
  300. {
  301. struct sk_buff *skb;
  302. char prefix[16];
  303. int fc = 0;
  304. spin_lock_bh(&queue->lock);
  305. skb_queue_walk(queue, skb) {
  306. snprintf(prefix, sizeof(prefix), "%3d :", fc);
  307. carl9170_debugfs_format_frame(ar, skb, prefix, buf,
  308. len, bufsize);
  309. fc++;
  310. }
  311. spin_unlock_bh(&queue->lock);
  312. }
  313. #define DEBUGFS_QUEUE_DUMP(q, qi) \
  314. static char *carl9170_debugfs_##q ##_##qi ##_read(struct ar9170 *ar, \
  315. char *buf, size_t bufsize, ssize_t *len) \
  316. { \
  317. carl9170_debugfs_queue_dump(ar, buf, len, bufsize, &ar->q[qi]); \
  318. return buf; \
  319. } \
  320. DEBUGFS_DECLARE_RO_FILE(q##_##qi, 8000);
  321. static char *carl9170_debugfs_sta_psm_read(struct ar9170 *ar, char *buf,
  322. size_t bufsize, ssize_t *len)
  323. {
  324. ADD(buf, *len, bufsize, "psm state: %s\n", (ar->ps.off_override ?
  325. "FORCE CAM" : (ar->ps.state ? "PSM" : "CAM")));
  326. ADD(buf, *len, bufsize, "sleep duration: %d ms.\n", ar->ps.sleep_ms);
  327. ADD(buf, *len, bufsize, "last power-state transition: %d ms ago.\n",
  328. jiffies_to_msecs(jiffies - ar->ps.last_action));
  329. ADD(buf, *len, bufsize, "last CAM->PSM transition: %d ms ago.\n",
  330. jiffies_to_msecs(jiffies - ar->ps.last_slept));
  331. return buf;
  332. }
  333. DEBUGFS_DECLARE_RO_FILE(sta_psm, 160);
  334. static char *carl9170_debugfs_tx_stuck_read(struct ar9170 *ar, char *buf,
  335. size_t bufsize, ssize_t *len)
  336. {
  337. int i;
  338. for (i = 0; i < ar->hw->queues; i++) {
  339. ADD(buf, *len, bufsize, "TX queue [%d]: %10d max:%10d ms.\n",
  340. i, ieee80211_queue_stopped(ar->hw, i) ?
  341. jiffies_to_msecs(jiffies - ar->queue_stop_timeout[i]) : 0,
  342. jiffies_to_msecs(ar->max_queue_stop_timeout[i]));
  343. ar->max_queue_stop_timeout[i] = 0;
  344. }
  345. return buf;
  346. }
  347. DEBUGFS_DECLARE_RO_FILE(tx_stuck, 180);
  348. static char *carl9170_debugfs_phy_noise_read(struct ar9170 *ar, char *buf,
  349. size_t bufsize, ssize_t *len)
  350. {
  351. int err;
  352. err = carl9170_get_noisefloor(ar);
  353. if (err) {
  354. *len = err;
  355. return buf;
  356. }
  357. ADD(buf, *len, bufsize, "Chain 0: %10d dBm, ext. chan.:%10d dBm\n",
  358. ar->noise[0], ar->noise[2]);
  359. ADD(buf, *len, bufsize, "Chain 2: %10d dBm, ext. chan.:%10d dBm\n",
  360. ar->noise[1], ar->noise[3]);
  361. return buf;
  362. }
  363. DEBUGFS_DECLARE_RO_FILE(phy_noise, 180);
  364. static char *carl9170_debugfs_vif_dump_read(struct ar9170 *ar, char *buf,
  365. size_t bufsize, ssize_t *len)
  366. {
  367. struct carl9170_vif_info *iter;
  368. int i = 0;
  369. ADD(buf, *len, bufsize, "registered VIFs:%d \\ %d\n",
  370. ar->vifs, ar->fw.vif_num);
  371. ADD(buf, *len, bufsize, "VIF bitmap: [");
  372. *len += bitmap_scnprintf(&buf[*len], bufsize - *len,
  373. &ar->vif_bitmap, ar->fw.vif_num);
  374. ADD(buf, *len, bufsize, "]\n");
  375. rcu_read_lock();
  376. list_for_each_entry_rcu(iter, &ar->vif_list, list) {
  377. struct ieee80211_vif *vif = carl9170_get_vif(iter);
  378. ADD(buf, *len, bufsize, "\t%d = [%s VIF, id:%d, type:%x "
  379. " mac:%pM %s]\n", i, (carl9170_get_main_vif(ar) == vif ?
  380. "Master" : " Slave"), iter->id, vif->type, vif->addr,
  381. iter->enable_beacon ? "beaconing " : "");
  382. i++;
  383. }
  384. rcu_read_unlock();
  385. return buf;
  386. }
  387. DEBUGFS_DECLARE_RO_FILE(vif_dump, 8000);
  388. #define UPDATE_COUNTER(ar, name) ({ \
  389. u32 __tmp[ARRAY_SIZE(name##_regs)]; \
  390. unsigned int __i, __err = -ENODEV; \
  391. \
  392. for (__i = 0; __i < ARRAY_SIZE(name##_regs); __i++) { \
  393. __tmp[__i] = name##_regs[__i].reg; \
  394. ar->debug.stats.name##_counter[__i] = 0; \
  395. } \
  396. \
  397. if (IS_STARTED(ar)) \
  398. __err = carl9170_read_mreg(ar, ARRAY_SIZE(name##_regs), \
  399. __tmp, ar->debug.stats.name##_counter); \
  400. (__err); })
  401. #define TALLY_SUM_UP(ar, name) do { \
  402. unsigned int __i; \
  403. \
  404. for (__i = 0; __i < ARRAY_SIZE(name##_regs); __i++) { \
  405. ar->debug.stats.name##_sum[__i] += \
  406. ar->debug.stats.name##_counter[__i]; \
  407. } \
  408. } while (0)
  409. #define DEBUGFS_HW_TALLY_FILE(name, f) \
  410. static char *carl9170_debugfs_##name ## _read(struct ar9170 *ar, \
  411. char *dum, size_t bufsize, ssize_t *ret) \
  412. { \
  413. char *buf; \
  414. int i, max_len, err; \
  415. \
  416. max_len = ARRAY_SIZE(name##_regs) * 80; \
  417. buf = vmalloc(max_len); \
  418. if (!buf) \
  419. return NULL; \
  420. \
  421. err = UPDATE_COUNTER(ar, name); \
  422. if (err) { \
  423. *ret = err; \
  424. return buf; \
  425. } \
  426. \
  427. TALLY_SUM_UP(ar, name); \
  428. \
  429. for (i = 0; i < ARRAY_SIZE(name##_regs); i++) { \
  430. ADD(buf, *ret, max_len, "%22s = %" f "[+%" f "]\n", \
  431. name##_regs[i].nreg, ar->debug.stats.name ##_sum[i],\
  432. ar->debug.stats.name ##_counter[i]); \
  433. } \
  434. \
  435. return buf; \
  436. } \
  437. DEBUGFS_DECLARE_RO_FILE(name, 0);
  438. #define DEBUGFS_HW_REG_FILE(name, f) \
  439. static char *carl9170_debugfs_##name ## _read(struct ar9170 *ar, \
  440. char *dum, size_t bufsize, ssize_t *ret) \
  441. { \
  442. char *buf; \
  443. int i, max_len, err; \
  444. \
  445. max_len = ARRAY_SIZE(name##_regs) * 80; \
  446. buf = vmalloc(max_len); \
  447. if (!buf) \
  448. return NULL; \
  449. \
  450. err = UPDATE_COUNTER(ar, name); \
  451. if (err) { \
  452. *ret = err; \
  453. return buf; \
  454. } \
  455. \
  456. for (i = 0; i < ARRAY_SIZE(name##_regs); i++) { \
  457. ADD(buf, *ret, max_len, "%22s = %" f "\n", \
  458. name##_regs[i].nreg, \
  459. ar->debug.stats.name##_counter[i]); \
  460. } \
  461. \
  462. return buf; \
  463. } \
  464. DEBUGFS_DECLARE_RO_FILE(name, 0);
  465. static ssize_t carl9170_debugfs_hw_ioread32_write(struct ar9170 *ar,
  466. const char *buf, size_t count)
  467. {
  468. int err = 0, i, n = 0, max_len = 32, res;
  469. unsigned int reg, tmp;
  470. if (!count)
  471. return 0;
  472. if (count > max_len)
  473. return -E2BIG;
  474. res = sscanf(buf, "0x%X %d", &reg, &n);
  475. if (res < 1) {
  476. err = -EINVAL;
  477. goto out;
  478. }
  479. if (res == 1)
  480. n = 1;
  481. if (n > 15) {
  482. err = -EMSGSIZE;
  483. goto out;
  484. }
  485. if ((reg >= 0x280000) || ((reg + (n << 2)) >= 0x280000)) {
  486. err = -EADDRNOTAVAIL;
  487. goto out;
  488. }
  489. if (reg & 3) {
  490. err = -EINVAL;
  491. goto out;
  492. }
  493. for (i = 0; i < n; i++) {
  494. err = carl9170_read_reg(ar, reg + (i << 2), &tmp);
  495. if (err)
  496. goto out;
  497. ar->debug.ring[ar->debug.ring_tail].reg = reg + (i << 2);
  498. ar->debug.ring[ar->debug.ring_tail].value = tmp;
  499. ar->debug.ring_tail++;
  500. ar->debug.ring_tail %= CARL9170_DEBUG_RING_SIZE;
  501. }
  502. out:
  503. return err ? err : count;
  504. }
  505. static char *carl9170_debugfs_hw_ioread32_read(struct ar9170 *ar, char *buf,
  506. size_t bufsize, ssize_t *ret)
  507. {
  508. int i = 0;
  509. while (ar->debug.ring_head != ar->debug.ring_tail) {
  510. ADD(buf, *ret, bufsize, "%.8x = %.8x\n",
  511. ar->debug.ring[ar->debug.ring_head].reg,
  512. ar->debug.ring[ar->debug.ring_head].value);
  513. ar->debug.ring_head++;
  514. ar->debug.ring_head %= CARL9170_DEBUG_RING_SIZE;
  515. if (i++ == 64)
  516. break;
  517. }
  518. ar->debug.ring_head = ar->debug.ring_tail;
  519. return buf;
  520. }
  521. DEBUGFS_DECLARE_RW_FILE(hw_ioread32, CARL9170_DEBUG_RING_SIZE * 40);
  522. static ssize_t carl9170_debugfs_bug_write(struct ar9170 *ar, const char *buf,
  523. size_t count)
  524. {
  525. int err;
  526. if (count < 1)
  527. return -EINVAL;
  528. switch (buf[0]) {
  529. case 'F':
  530. ar->needs_full_reset = true;
  531. break;
  532. case 'R':
  533. if (!IS_STARTED(ar)) {
  534. err = -EAGAIN;
  535. goto out;
  536. }
  537. ar->needs_full_reset = false;
  538. break;
  539. case 'M':
  540. err = carl9170_mac_reset(ar);
  541. if (err < 0)
  542. count = err;
  543. goto out;
  544. case 'P':
  545. err = carl9170_set_channel(ar, ar->hw->conf.channel,
  546. ar->hw->conf.channel_type, CARL9170_RFI_COLD);
  547. if (err < 0)
  548. count = err;
  549. goto out;
  550. default:
  551. return -EINVAL;
  552. }
  553. carl9170_restart(ar, CARL9170_RR_USER_REQUEST);
  554. out:
  555. return count;
  556. }
  557. static char *carl9170_debugfs_bug_read(struct ar9170 *ar, char *buf,
  558. size_t bufsize, ssize_t *ret)
  559. {
  560. ADD(buf, *ret, bufsize, "[P]hy reinit, [R]estart, [F]ull usb reset, "
  561. "[M]ac reset\n");
  562. ADD(buf, *ret, bufsize, "firmware restarts:%d, last reason:%d\n",
  563. ar->restart_counter, ar->last_reason);
  564. ADD(buf, *ret, bufsize, "phy reinit errors:%d (%d)\n",
  565. ar->total_chan_fail, ar->chan_fail);
  566. ADD(buf, *ret, bufsize, "reported firmware errors:%d\n",
  567. ar->fw.err_counter);
  568. ADD(buf, *ret, bufsize, "reported firmware BUGs:%d\n",
  569. ar->fw.bug_counter);
  570. ADD(buf, *ret, bufsize, "pending restart requests:%d\n",
  571. atomic_read(&ar->pending_restarts));
  572. return buf;
  573. }
  574. __DEBUGFS_DECLARE_RW_FILE(bug, 400, CARL9170_STOPPED);
  575. static const char *erp_modes[] = {
  576. [CARL9170_ERP_INVALID] = "INVALID",
  577. [CARL9170_ERP_AUTO] = "Automatic",
  578. [CARL9170_ERP_MAC80211] = "Set by MAC80211",
  579. [CARL9170_ERP_OFF] = "Force Off",
  580. [CARL9170_ERP_RTS] = "Force RTS",
  581. [CARL9170_ERP_CTS] = "Force CTS"
  582. };
  583. static char *carl9170_debugfs_erp_read(struct ar9170 *ar, char *buf,
  584. size_t bufsize, ssize_t *ret)
  585. {
  586. ADD(buf, *ret, bufsize, "ERP Setting: (%d) -> %s\n", ar->erp_mode,
  587. erp_modes[ar->erp_mode]);
  588. return buf;
  589. }
  590. static ssize_t carl9170_debugfs_erp_write(struct ar9170 *ar, const char *buf,
  591. size_t count)
  592. {
  593. int res, val;
  594. if (count < 1)
  595. return -EINVAL;
  596. res = sscanf(buf, "%d", &val);
  597. if (res != 1)
  598. return -EINVAL;
  599. if (!((val > CARL9170_ERP_INVALID) &&
  600. (val < __CARL9170_ERP_NUM)))
  601. return -EINVAL;
  602. ar->erp_mode = val;
  603. return count;
  604. }
  605. DEBUGFS_DECLARE_RW_FILE(erp, 80);
  606. static ssize_t carl9170_debugfs_hw_iowrite32_write(struct ar9170 *ar,
  607. const char *buf, size_t count)
  608. {
  609. int err = 0, max_len = 22, res;
  610. u32 reg, val;
  611. if (!count)
  612. return 0;
  613. if (count > max_len)
  614. return -E2BIG;
  615. res = sscanf(buf, "0x%X 0x%X", &reg, &val);
  616. if (res != 2) {
  617. err = -EINVAL;
  618. goto out;
  619. }
  620. if (reg <= 0x100000 || reg >= 0x280000) {
  621. err = -EADDRNOTAVAIL;
  622. goto out;
  623. }
  624. if (reg & 3) {
  625. err = -EINVAL;
  626. goto out;
  627. }
  628. err = carl9170_write_reg(ar, reg, val);
  629. if (err)
  630. goto out;
  631. out:
  632. return err ? err : count;
  633. }
  634. DEBUGFS_DECLARE_WO_FILE(hw_iowrite32);
  635. DEBUGFS_HW_TALLY_FILE(hw_tx_tally, "u");
  636. DEBUGFS_HW_TALLY_FILE(hw_rx_tally, "u");
  637. DEBUGFS_HW_TALLY_FILE(hw_phy_errors, "u");
  638. DEBUGFS_HW_REG_FILE(hw_wlan_queue, ".8x");
  639. DEBUGFS_HW_REG_FILE(hw_pta_queue, ".8x");
  640. DEBUGFS_HW_REG_FILE(hw_ampdu_info, ".8x");
  641. DEBUGFS_QUEUE_DUMP(tx_status, 0);
  642. DEBUGFS_QUEUE_DUMP(tx_status, 1);
  643. DEBUGFS_QUEUE_DUMP(tx_status, 2);
  644. DEBUGFS_QUEUE_DUMP(tx_status, 3);
  645. DEBUGFS_QUEUE_DUMP(tx_pending, 0);
  646. DEBUGFS_QUEUE_DUMP(tx_pending, 1);
  647. DEBUGFS_QUEUE_DUMP(tx_pending, 2);
  648. DEBUGFS_QUEUE_DUMP(tx_pending, 3);
  649. DEBUGFS_READONLY_FILE(usb_tx_anch_urbs, 20, "%d",
  650. atomic_read(&ar->tx_anch_urbs));
  651. DEBUGFS_READONLY_FILE(usb_rx_anch_urbs, 20, "%d",
  652. atomic_read(&ar->rx_anch_urbs));
  653. DEBUGFS_READONLY_FILE(usb_rx_work_urbs, 20, "%d",
  654. atomic_read(&ar->rx_work_urbs));
  655. DEBUGFS_READONLY_FILE(usb_rx_pool_urbs, 20, "%d",
  656. atomic_read(&ar->rx_pool_urbs));
  657. DEBUGFS_READONLY_FILE(tx_total_queued, 20, "%d",
  658. atomic_read(&ar->tx_total_queued));
  659. DEBUGFS_READONLY_FILE(tx_ampdu_scheduler, 20, "%d",
  660. atomic_read(&ar->tx_ampdu_scheduler));
  661. DEBUGFS_READONLY_FILE(tx_total_pending, 20, "%d",
  662. atomic_read(&ar->tx_total_pending));
  663. DEBUGFS_READONLY_FILE(tx_ampdu_list_len, 20, "%d",
  664. ar->tx_ampdu_list_len);
  665. DEBUGFS_READONLY_FILE(tx_ampdu_upload, 20, "%d",
  666. atomic_read(&ar->tx_ampdu_upload));
  667. DEBUGFS_READONLY_FILE(tx_janitor_last_run, 64, "last run:%d ms ago",
  668. jiffies_to_msecs(jiffies - ar->tx_janitor_last_run));
  669. DEBUGFS_READONLY_FILE(tx_dropped, 20, "%d", ar->tx_dropped);
  670. DEBUGFS_READONLY_FILE(rx_dropped, 20, "%d", ar->rx_dropped);
  671. DEBUGFS_READONLY_FILE(sniffer_enabled, 20, "%d", ar->sniffer_enabled);
  672. DEBUGFS_READONLY_FILE(rx_software_decryption, 20, "%d",
  673. ar->rx_software_decryption);
  674. DEBUGFS_READONLY_FILE(ampdu_factor, 20, "%d",
  675. ar->current_factor);
  676. DEBUGFS_READONLY_FILE(ampdu_density, 20, "%d",
  677. ar->current_density);
  678. DEBUGFS_READONLY_FILE(beacon_int, 20, "%d TU", ar->global_beacon_int);
  679. DEBUGFS_READONLY_FILE(pretbtt, 20, "%d TU", ar->global_pretbtt);
  680. void carl9170_debugfs_register(struct ar9170 *ar)
  681. {
  682. ar->debug_dir = debugfs_create_dir(KBUILD_MODNAME,
  683. ar->hw->wiphy->debugfsdir);
  684. #define DEBUGFS_ADD(name) \
  685. debugfs_create_file(#name, carl_debugfs_##name ##_ops.attr, \
  686. ar->debug_dir, ar, \
  687. &carl_debugfs_##name ## _ops.fops);
  688. DEBUGFS_ADD(usb_tx_anch_urbs);
  689. DEBUGFS_ADD(usb_rx_pool_urbs);
  690. DEBUGFS_ADD(usb_rx_anch_urbs);
  691. DEBUGFS_ADD(usb_rx_work_urbs);
  692. DEBUGFS_ADD(tx_total_queued);
  693. DEBUGFS_ADD(tx_total_pending);
  694. DEBUGFS_ADD(tx_dropped);
  695. DEBUGFS_ADD(tx_stuck);
  696. DEBUGFS_ADD(tx_ampdu_upload);
  697. DEBUGFS_ADD(tx_ampdu_scheduler);
  698. DEBUGFS_ADD(tx_ampdu_list_len);
  699. DEBUGFS_ADD(rx_dropped);
  700. DEBUGFS_ADD(sniffer_enabled);
  701. DEBUGFS_ADD(rx_software_decryption);
  702. DEBUGFS_ADD(mem_usage);
  703. DEBUGFS_ADD(qos_stat);
  704. DEBUGFS_ADD(sta_psm);
  705. DEBUGFS_ADD(ampdu_state);
  706. DEBUGFS_ADD(hw_tx_tally);
  707. DEBUGFS_ADD(hw_rx_tally);
  708. DEBUGFS_ADD(hw_phy_errors);
  709. DEBUGFS_ADD(phy_noise);
  710. DEBUGFS_ADD(hw_wlan_queue);
  711. DEBUGFS_ADD(hw_pta_queue);
  712. DEBUGFS_ADD(hw_ampdu_info);
  713. DEBUGFS_ADD(ampdu_density);
  714. DEBUGFS_ADD(ampdu_factor);
  715. DEBUGFS_ADD(tx_janitor_last_run);
  716. DEBUGFS_ADD(tx_status_0);
  717. DEBUGFS_ADD(tx_status_1);
  718. DEBUGFS_ADD(tx_status_2);
  719. DEBUGFS_ADD(tx_status_3);
  720. DEBUGFS_ADD(tx_pending_0);
  721. DEBUGFS_ADD(tx_pending_1);
  722. DEBUGFS_ADD(tx_pending_2);
  723. DEBUGFS_ADD(tx_pending_3);
  724. DEBUGFS_ADD(hw_ioread32);
  725. DEBUGFS_ADD(hw_iowrite32);
  726. DEBUGFS_ADD(bug);
  727. DEBUGFS_ADD(erp);
  728. DEBUGFS_ADD(vif_dump);
  729. DEBUGFS_ADD(beacon_int);
  730. DEBUGFS_ADD(pretbtt);
  731. #undef DEBUGFS_ADD
  732. }
  733. void carl9170_debugfs_unregister(struct ar9170 *ar)
  734. {
  735. debugfs_remove_recursive(ar->debug_dir);
  736. }