debug.c 24 KB

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