debug.c 23 KB

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