debug.c 24 KB

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