debug.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * Atheros CARL9170 driver
  3. *
  4. * debug(fs) probing
  5. *
  6. * Copyright 2008, Johannes Berg <[email protected]>
  7. * Copyright 2009, 2010, Christian Lamparter <[email protected]>
  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 += scnprintf(&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(debugfs_real_fops(file),
  70. 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(debugfs_real_fops(file),
  111. struct carl9170_debugfs_fops, fops);
  112. if (!dfops->write)
  113. return -ENOSYS;
  114. buf = vmalloc(count);
  115. if (!buf)
  116. return -ENOMEM;
  117. if (copy_from_user(buf, userbuf, count)) {
  118. err = -EFAULT;
  119. goto out_free;
  120. }
  121. if (mutex_trylock(&ar->mutex) == 0) {
  122. err = -EAGAIN;
  123. goto out_free;
  124. }
  125. if (!CHK_DEV_STATE(ar, dfops->req_dev_state)) {
  126. err = -ENODEV;
  127. goto out_unlock;
  128. }
  129. err = dfops->write(ar, buf, count);
  130. if (err)
  131. goto out_unlock;
  132. out_unlock:
  133. mutex_unlock(&ar->mutex);
  134. out_free:
  135. vfree(buf);
  136. return err;
  137. }
  138. #define __DEBUGFS_DECLARE_FILE(name, _read, _write, _read_bufsize, \
  139. _attr, _dstate) \
  140. static const struct carl9170_debugfs_fops carl_debugfs_##name ##_ops = {\
  141. .read_bufsize = _read_bufsize, \
  142. .read = _read, \
  143. .write = _write, \
  144. .attr = _attr, \
  145. .req_dev_state = _dstate, \
  146. .fops = { \
  147. .open = simple_open, \
  148. .read = carl9170_debugfs_read, \
  149. .write = carl9170_debugfs_write, \
  150. .owner = THIS_MODULE \
  151. }, \
  152. }
  153. #define DEBUGFS_DECLARE_FILE(name, _read, _write, _read_bufsize, _attr) \
  154. __DEBUGFS_DECLARE_FILE(name, _read, _write, _read_bufsize, \
  155. _attr, CARL9170_STARTED) \
  156. #define DEBUGFS_DECLARE_RO_FILE(name, _read_bufsize) \
  157. DEBUGFS_DECLARE_FILE(name, carl9170_debugfs_##name ##_read, \
  158. NULL, _read_bufsize, 0400)
  159. #define DEBUGFS_DECLARE_WO_FILE(name) \
  160. DEBUGFS_DECLARE_FILE(name, NULL, carl9170_debugfs_##name ##_write,\
  161. 0, 0200)
  162. #define DEBUGFS_DECLARE_RW_FILE(name, _read_bufsize) \
  163. DEBUGFS_DECLARE_FILE(name, carl9170_debugfs_##name ##_read, \
  164. carl9170_debugfs_##name ##_write, \
  165. _read_bufsize, 0600)
  166. #define __DEBUGFS_DECLARE_RW_FILE(name, _read_bufsize, _dstate) \
  167. __DEBUGFS_DECLARE_FILE(name, carl9170_debugfs_##name ##_read, \
  168. carl9170_debugfs_##name ##_write, \
  169. _read_bufsize, 0600, _dstate)
  170. #define DEBUGFS_READONLY_FILE(name, _read_bufsize, fmt, value...) \
  171. static char *carl9170_debugfs_ ##name ## _read(struct ar9170 *ar, \
  172. char *buf, size_t buf_size,\
  173. ssize_t *len) \
  174. { \
  175. ADD(buf, *len, buf_size, fmt "\n", ##value); \
  176. return buf; \
  177. } \
  178. DEBUGFS_DECLARE_RO_FILE(name, _read_bufsize)
  179. static char *carl9170_debugfs_mem_usage_read(struct ar9170 *ar, char *buf,
  180. size_t bufsize, ssize_t *len)
  181. {
  182. spin_lock_bh(&ar->mem_lock);
  183. ADD(buf, *len, bufsize, "jar: [%*pb]\n",
  184. ar->fw.mem_blocks, ar->mem_bitmap);
  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: [%*pb,W]\n",
  262. CARL9170_BAW_BITS, iter->bitmap);
  263. #define BM_STR_OFF(offset) \
  264. ((CARL9170_BAW_BITS - (offset) - 1) / 4 + \
  265. (CARL9170_BAW_BITS - (offset) - 1) / 32 + 1)
  266. offset = BM_STR_OFF(0);
  267. ADD(buf, *len, bufsize, "\tBase Seq: %*s\n", offset, "T");
  268. offset = BM_STR_OFF(SEQ_DIFF(iter->snx, iter->bsn));
  269. ADD(buf, *len, bufsize, "\tNext Seq: %*s\n", offset, "W");
  270. offset = BM_STR_OFF(((int)iter->hsn - (int)iter->bsn) %
  271. CARL9170_BAW_BITS);
  272. ADD(buf, *len, bufsize, "\tLast Seq: %*s\n", offset, "N");
  273. ADD(buf, *len, bufsize, "\tPre-Aggregation reorder buffer: "
  274. " currently queued:%d\n", skb_queue_len(&iter->queue));
  275. fc = 0;
  276. skb_queue_walk(&iter->queue, skb) {
  277. char prefix[32];
  278. snprintf(prefix, sizeof(prefix), "\t\t%3d :", fc);
  279. carl9170_debugfs_format_frame(ar, skb, prefix, buf,
  280. len, bufsize);
  281. fc++;
  282. }
  283. spin_unlock_bh(&iter->lock);
  284. cnt++;
  285. }
  286. rcu_read_unlock();
  287. return buf;
  288. }
  289. DEBUGFS_DECLARE_RO_FILE(ampdu_state, 8000);
  290. static void carl9170_debugfs_queue_dump(struct ar9170 *ar, char *buf,
  291. ssize_t *len, size_t bufsize, struct sk_buff_head *queue)
  292. {
  293. struct sk_buff *skb;
  294. char prefix[16];
  295. int fc = 0;
  296. spin_lock_bh(&queue->lock);
  297. skb_queue_walk(queue, skb) {
  298. snprintf(prefix, sizeof(prefix), "%3d :", fc);
  299. carl9170_debugfs_format_frame(ar, skb, prefix, buf,
  300. len, bufsize);
  301. fc++;
  302. }
  303. spin_unlock_bh(&queue->lock);
  304. }
  305. #define DEBUGFS_QUEUE_DUMP(q, qi) \
  306. static char *carl9170_debugfs_##q ##_##qi ##_read(struct ar9170 *ar, \
  307. char *buf, size_t bufsize, ssize_t *len) \
  308. { \
  309. carl9170_debugfs_queue_dump(ar, buf, len, bufsize, &ar->q[qi]); \
  310. return buf; \
  311. } \
  312. DEBUGFS_DECLARE_RO_FILE(q##_##qi, 8000);
  313. static char *carl9170_debugfs_sta_psm_read(struct ar9170 *ar, char *buf,
  314. size_t bufsize, ssize_t *len)
  315. {
  316. ADD(buf, *len, bufsize, "psm state: %s\n", (ar->ps.off_override ?
  317. "FORCE CAM" : (ar->ps.state ? "PSM" : "CAM")));
  318. ADD(buf, *len, bufsize, "sleep duration: %d ms.\n", ar->ps.sleep_ms);
  319. ADD(buf, *len, bufsize, "last power-state transition: %d ms ago.\n",
  320. jiffies_to_msecs(jiffies - ar->ps.last_action));
  321. ADD(buf, *len, bufsize, "last CAM->PSM transition: %d ms ago.\n",
  322. jiffies_to_msecs(jiffies - ar->ps.last_slept));
  323. return buf;
  324. }
  325. DEBUGFS_DECLARE_RO_FILE(sta_psm, 160);
  326. static char *carl9170_debugfs_tx_stuck_read(struct ar9170 *ar, char *buf,
  327. size_t bufsize, ssize_t *len)
  328. {
  329. int i;
  330. for (i = 0; i < ar->hw->queues; i++) {
  331. ADD(buf, *len, bufsize, "TX queue [%d]: %10d max:%10d ms.\n",
  332. i, ieee80211_queue_stopped(ar->hw, i) ?
  333. jiffies_to_msecs(jiffies - ar->queue_stop_timeout[i]) : 0,
  334. jiffies_to_msecs(ar->max_queue_stop_timeout[i]));
  335. ar->max_queue_stop_timeout[i] = 0;
  336. }
  337. return buf;
  338. }
  339. DEBUGFS_DECLARE_RO_FILE(tx_stuck, 180);
  340. static char *carl9170_debugfs_phy_noise_read(struct ar9170 *ar, char *buf,
  341. size_t bufsize, ssize_t *len)
  342. {
  343. int err;
  344. err = carl9170_get_noisefloor(ar);
  345. if (err) {
  346. *len = err;
  347. return buf;
  348. }
  349. ADD(buf, *len, bufsize, "Chain 0: %10d dBm, ext. chan.:%10d dBm\n",
  350. ar->noise[0], ar->noise[2]);
  351. ADD(buf, *len, bufsize, "Chain 2: %10d dBm, ext. chan.:%10d dBm\n",
  352. ar->noise[1], ar->noise[3]);
  353. return buf;
  354. }
  355. DEBUGFS_DECLARE_RO_FILE(phy_noise, 180);
  356. static char *carl9170_debugfs_vif_dump_read(struct ar9170 *ar, char *buf,
  357. size_t bufsize, ssize_t *len)
  358. {
  359. struct carl9170_vif_info *iter;
  360. int i = 0;
  361. ADD(buf, *len, bufsize, "registered VIFs:%d \\ %d\n",
  362. ar->vifs, ar->fw.vif_num);
  363. ADD(buf, *len, bufsize, "VIF bitmap: [%*pb]\n",
  364. ar->fw.vif_num, &ar->vif_bitmap);
  365. rcu_read_lock();
  366. list_for_each_entry_rcu(iter, &ar->vif_list, list) {
  367. struct ieee80211_vif *vif = carl9170_get_vif(iter);
  368. ADD(buf, *len, bufsize, "\t%d = [%s VIF, id:%d, type:%x "
  369. " mac:%pM %s]\n", i, (carl9170_get_main_vif(ar) == vif ?
  370. "Master" : " Slave"), iter->id, vif->type, vif->addr,
  371. iter->enable_beacon ? "beaconing " : "");
  372. i++;
  373. }
  374. rcu_read_unlock();
  375. return buf;
  376. }
  377. DEBUGFS_DECLARE_RO_FILE(vif_dump, 8000);
  378. #define UPDATE_COUNTER(ar, name) ({ \
  379. u32 __tmp[ARRAY_SIZE(name##_regs)]; \
  380. unsigned int __i, __err = -ENODEV; \
  381. \
  382. for (__i = 0; __i < ARRAY_SIZE(name##_regs); __i++) { \
  383. __tmp[__i] = name##_regs[__i].reg; \
  384. ar->debug.stats.name##_counter[__i] = 0; \
  385. } \
  386. \
  387. if (IS_STARTED(ar)) \
  388. __err = carl9170_read_mreg(ar, ARRAY_SIZE(name##_regs), \
  389. __tmp, ar->debug.stats.name##_counter); \
  390. (__err); })
  391. #define TALLY_SUM_UP(ar, name) do { \
  392. unsigned int __i; \
  393. \
  394. for (__i = 0; __i < ARRAY_SIZE(name##_regs); __i++) { \
  395. ar->debug.stats.name##_sum[__i] += \
  396. ar->debug.stats.name##_counter[__i]; \
  397. } \
  398. } while (0)
  399. #define DEBUGFS_HW_TALLY_FILE(name, f) \
  400. static char *carl9170_debugfs_##name ## _read(struct ar9170 *ar, \
  401. char *dum, size_t bufsize, ssize_t *ret) \
  402. { \
  403. char *buf; \
  404. int i, max_len, err; \
  405. \
  406. max_len = ARRAY_SIZE(name##_regs) * 80; \
  407. buf = vmalloc(max_len); \
  408. if (!buf) \
  409. return NULL; \
  410. \
  411. err = UPDATE_COUNTER(ar, name); \
  412. if (err) { \
  413. *ret = err; \
  414. return buf; \
  415. } \
  416. \
  417. TALLY_SUM_UP(ar, name); \
  418. \
  419. for (i = 0; i < ARRAY_SIZE(name##_regs); i++) { \
  420. ADD(buf, *ret, max_len, "%22s = %" f "[+%" f "]\n", \
  421. name##_regs[i].nreg, ar->debug.stats.name ##_sum[i],\
  422. ar->debug.stats.name ##_counter[i]); \
  423. } \
  424. \
  425. return buf; \
  426. } \
  427. DEBUGFS_DECLARE_RO_FILE(name, 0);
  428. #define DEBUGFS_HW_REG_FILE(name, f) \
  429. static char *carl9170_debugfs_##name ## _read(struct ar9170 *ar, \
  430. char *dum, size_t bufsize, ssize_t *ret) \
  431. { \
  432. char *buf; \
  433. int i, max_len, err; \
  434. \
  435. max_len = ARRAY_SIZE(name##_regs) * 80; \
  436. buf = vmalloc(max_len); \
  437. if (!buf) \
  438. return NULL; \
  439. \
  440. err = UPDATE_COUNTER(ar, name); \
  441. if (err) { \
  442. *ret = err; \
  443. return buf; \
  444. } \
  445. \
  446. for (i = 0; i < ARRAY_SIZE(name##_regs); i++) { \
  447. ADD(buf, *ret, max_len, "%22s = %" f "\n", \
  448. name##_regs[i].nreg, \
  449. ar->debug.stats.name##_counter[i]); \
  450. } \
  451. \
  452. return buf; \
  453. } \
  454. DEBUGFS_DECLARE_RO_FILE(name, 0);
  455. static ssize_t carl9170_debugfs_hw_ioread32_write(struct ar9170 *ar,
  456. const char *buf, size_t count)
  457. {
  458. int err = 0, i, n = 0, max_len = 32, res;
  459. unsigned int reg, tmp;
  460. if (!count)
  461. return 0;
  462. if (count > max_len)
  463. return -E2BIG;
  464. res = sscanf(buf, "0x%X %d", &reg, &n);
  465. if (res < 1) {
  466. err = -EINVAL;
  467. goto out;
  468. }
  469. if (res == 1)
  470. n = 1;
  471. if (n > 15) {
  472. err = -EMSGSIZE;
  473. goto out;
  474. }
  475. if ((reg >= 0x280000) || ((reg + (n << 2)) >= 0x280000)) {
  476. err = -EADDRNOTAVAIL;
  477. goto out;
  478. }
  479. if (reg & 3) {
  480. err = -EINVAL;
  481. goto out;
  482. }
  483. for (i = 0; i < n; i++) {
  484. err = carl9170_read_reg(ar, reg + (i << 2), &tmp);
  485. if (err)
  486. goto out;
  487. ar->debug.ring[ar->debug.ring_tail].reg = reg + (i << 2);
  488. ar->debug.ring[ar->debug.ring_tail].value = tmp;
  489. ar->debug.ring_tail++;
  490. ar->debug.ring_tail %= CARL9170_DEBUG_RING_SIZE;
  491. }
  492. out:
  493. return err ? err : count;
  494. }
  495. static char *carl9170_debugfs_hw_ioread32_read(struct ar9170 *ar, char *buf,
  496. size_t bufsize, ssize_t *ret)
  497. {
  498. int i = 0;
  499. while (ar->debug.ring_head != ar->debug.ring_tail) {
  500. ADD(buf, *ret, bufsize, "%.8x = %.8x\n",
  501. ar->debug.ring[ar->debug.ring_head].reg,
  502. ar->debug.ring[ar->debug.ring_head].value);
  503. ar->debug.ring_head++;
  504. ar->debug.ring_head %= CARL9170_DEBUG_RING_SIZE;
  505. if (i++ == 64)
  506. break;
  507. }
  508. ar->debug.ring_head = ar->debug.ring_tail;
  509. return buf;
  510. }
  511. DEBUGFS_DECLARE_RW_FILE(hw_ioread32, CARL9170_DEBUG_RING_SIZE * 40);
  512. static ssize_t carl9170_debugfs_bug_write(struct ar9170 *ar, const char *buf,
  513. size_t count)
  514. {
  515. int err;
  516. if (count < 1)
  517. return -EINVAL;
  518. switch (buf[0]) {
  519. case 'F':
  520. ar->needs_full_reset = true;
  521. break;
  522. case 'R':
  523. if (!IS_STARTED(ar)) {
  524. err = -EAGAIN;
  525. goto out;
  526. }
  527. ar->needs_full_reset = false;
  528. break;
  529. case 'M':
  530. err = carl9170_mac_reset(ar);
  531. if (err < 0)
  532. count = err;
  533. goto out;
  534. case 'P':
  535. err = carl9170_set_channel(ar, ar->hw->conf.chandef.chan,
  536. cfg80211_get_chandef_type(&ar->hw->conf.chandef));
  537. if (err < 0)
  538. count = err;
  539. goto out;
  540. default:
  541. return -EINVAL;
  542. }
  543. carl9170_restart(ar, CARL9170_RR_USER_REQUEST);
  544. out:
  545. return count;
  546. }
  547. static char *carl9170_debugfs_bug_read(struct ar9170 *ar, char *buf,
  548. size_t bufsize, ssize_t *ret)
  549. {
  550. ADD(buf, *ret, bufsize, "[P]hy reinit, [R]estart, [F]ull usb reset, "
  551. "[M]ac reset\n");
  552. ADD(buf, *ret, bufsize, "firmware restarts:%d, last reason:%d\n",
  553. ar->restart_counter, ar->last_reason);
  554. ADD(buf, *ret, bufsize, "phy reinit errors:%d (%d)\n",
  555. ar->total_chan_fail, ar->chan_fail);
  556. ADD(buf, *ret, bufsize, "reported firmware errors:%d\n",
  557. ar->fw.err_counter);
  558. ADD(buf, *ret, bufsize, "reported firmware BUGs:%d\n",
  559. ar->fw.bug_counter);
  560. ADD(buf, *ret, bufsize, "pending restart requests:%d\n",
  561. atomic_read(&ar->pending_restarts));
  562. return buf;
  563. }
  564. __DEBUGFS_DECLARE_RW_FILE(bug, 400, CARL9170_STOPPED);
  565. static const char *const erp_modes[] = {
  566. [CARL9170_ERP_INVALID] = "INVALID",
  567. [CARL9170_ERP_AUTO] = "Automatic",
  568. [CARL9170_ERP_MAC80211] = "Set by MAC80211",
  569. [CARL9170_ERP_OFF] = "Force Off",
  570. [CARL9170_ERP_RTS] = "Force RTS",
  571. [CARL9170_ERP_CTS] = "Force CTS"
  572. };
  573. static char *carl9170_debugfs_erp_read(struct ar9170 *ar, char *buf,
  574. size_t bufsize, ssize_t *ret)
  575. {
  576. ADD(buf, *ret, bufsize, "ERP Setting: (%d) -> %s\n", ar->erp_mode,
  577. erp_modes[ar->erp_mode]);
  578. return buf;
  579. }
  580. static ssize_t carl9170_debugfs_erp_write(struct ar9170 *ar, const char *buf,
  581. size_t count)
  582. {
  583. int res, val;
  584. if (count < 1)
  585. return -EINVAL;
  586. res = sscanf(buf, "%d", &val);
  587. if (res != 1)
  588. return -EINVAL;
  589. if (!((val > CARL9170_ERP_INVALID) &&
  590. (val < __CARL9170_ERP_NUM)))
  591. return -EINVAL;
  592. ar->erp_mode = val;
  593. return count;
  594. }
  595. DEBUGFS_DECLARE_RW_FILE(erp, 80);
  596. static ssize_t carl9170_debugfs_hw_iowrite32_write(struct ar9170 *ar,
  597. const char *buf, size_t count)
  598. {
  599. int err = 0, max_len = 22, res;
  600. u32 reg, val;
  601. if (!count)
  602. return 0;
  603. if (count > max_len)
  604. return -E2BIG;
  605. res = sscanf(buf, "0x%X 0x%X", &reg, &val);
  606. if (res != 2) {
  607. err = -EINVAL;
  608. goto out;
  609. }
  610. if (reg <= 0x100000 || reg >= 0x280000) {
  611. err = -EADDRNOTAVAIL;
  612. goto out;
  613. }
  614. if (reg & 3) {
  615. err = -EINVAL;
  616. goto out;
  617. }
  618. err = carl9170_write_reg(ar, reg, val);
  619. if (err)
  620. goto out;
  621. out:
  622. return err ? err : count;
  623. }
  624. DEBUGFS_DECLARE_WO_FILE(hw_iowrite32);
  625. DEBUGFS_HW_TALLY_FILE(hw_tx_tally, "u");
  626. DEBUGFS_HW_TALLY_FILE(hw_rx_tally, "u");
  627. DEBUGFS_HW_TALLY_FILE(hw_phy_errors, "u");
  628. DEBUGFS_HW_REG_FILE(hw_wlan_queue, ".8x");
  629. DEBUGFS_HW_REG_FILE(hw_pta_queue, ".8x");
  630. DEBUGFS_HW_REG_FILE(hw_ampdu_info, ".8x");
  631. DEBUGFS_QUEUE_DUMP(tx_status, 0);
  632. DEBUGFS_QUEUE_DUMP(tx_status, 1);
  633. DEBUGFS_QUEUE_DUMP(tx_status, 2);
  634. DEBUGFS_QUEUE_DUMP(tx_status, 3);
  635. DEBUGFS_QUEUE_DUMP(tx_pending, 0);
  636. DEBUGFS_QUEUE_DUMP(tx_pending, 1);
  637. DEBUGFS_QUEUE_DUMP(tx_pending, 2);
  638. DEBUGFS_QUEUE_DUMP(tx_pending, 3);
  639. DEBUGFS_READONLY_FILE(usb_tx_anch_urbs, 20, "%d",
  640. atomic_read(&ar->tx_anch_urbs));
  641. DEBUGFS_READONLY_FILE(usb_rx_anch_urbs, 20, "%d",
  642. atomic_read(&ar->rx_anch_urbs));
  643. DEBUGFS_READONLY_FILE(usb_rx_work_urbs, 20, "%d",
  644. atomic_read(&ar->rx_work_urbs));
  645. DEBUGFS_READONLY_FILE(usb_rx_pool_urbs, 20, "%d",
  646. atomic_read(&ar->rx_pool_urbs));
  647. DEBUGFS_READONLY_FILE(tx_total_queued, 20, "%d",
  648. atomic_read(&ar->tx_total_queued));
  649. DEBUGFS_READONLY_FILE(tx_ampdu_scheduler, 20, "%d",
  650. atomic_read(&ar->tx_ampdu_scheduler));
  651. DEBUGFS_READONLY_FILE(tx_total_pending, 20, "%d",
  652. atomic_read(&ar->tx_total_pending));
  653. DEBUGFS_READONLY_FILE(tx_ampdu_list_len, 20, "%d",
  654. ar->tx_ampdu_list_len);
  655. DEBUGFS_READONLY_FILE(tx_ampdu_upload, 20, "%d",
  656. atomic_read(&ar->tx_ampdu_upload));
  657. DEBUGFS_READONLY_FILE(tx_janitor_last_run, 64, "last run:%d ms ago",
  658. jiffies_to_msecs(jiffies - ar->tx_janitor_last_run));
  659. DEBUGFS_READONLY_FILE(tx_dropped, 20, "%d", ar->tx_dropped);
  660. DEBUGFS_READONLY_FILE(rx_dropped, 20, "%d", ar->rx_dropped);
  661. DEBUGFS_READONLY_FILE(sniffer_enabled, 20, "%d", ar->sniffer_enabled);
  662. DEBUGFS_READONLY_FILE(rx_software_decryption, 20, "%d",
  663. ar->rx_software_decryption);
  664. DEBUGFS_READONLY_FILE(ampdu_factor, 20, "%d",
  665. ar->current_factor);
  666. DEBUGFS_READONLY_FILE(ampdu_density, 20, "%d",
  667. ar->current_density);
  668. DEBUGFS_READONLY_FILE(beacon_int, 20, "%d TU", ar->global_beacon_int);
  669. DEBUGFS_READONLY_FILE(pretbtt, 20, "%d TU", ar->global_pretbtt);
  670. void carl9170_debugfs_register(struct ar9170 *ar)
  671. {
  672. ar->debug_dir = debugfs_create_dir(KBUILD_MODNAME,
  673. ar->hw->wiphy->debugfsdir);
  674. #define DEBUGFS_ADD(name) \
  675. debugfs_create_file(#name, carl_debugfs_##name ##_ops.attr, \
  676. ar->debug_dir, ar, \
  677. &carl_debugfs_##name ## _ops.fops)
  678. DEBUGFS_ADD(usb_tx_anch_urbs);
  679. DEBUGFS_ADD(usb_rx_pool_urbs);
  680. DEBUGFS_ADD(usb_rx_anch_urbs);
  681. DEBUGFS_ADD(usb_rx_work_urbs);
  682. DEBUGFS_ADD(tx_total_queued);
  683. DEBUGFS_ADD(tx_total_pending);
  684. DEBUGFS_ADD(tx_dropped);
  685. DEBUGFS_ADD(tx_stuck);
  686. DEBUGFS_ADD(tx_ampdu_upload);
  687. DEBUGFS_ADD(tx_ampdu_scheduler);
  688. DEBUGFS_ADD(tx_ampdu_list_len);
  689. DEBUGFS_ADD(rx_dropped);
  690. DEBUGFS_ADD(sniffer_enabled);
  691. DEBUGFS_ADD(rx_software_decryption);
  692. DEBUGFS_ADD(mem_usage);
  693. DEBUGFS_ADD(qos_stat);
  694. DEBUGFS_ADD(sta_psm);
  695. DEBUGFS_ADD(ampdu_state);
  696. DEBUGFS_ADD(hw_tx_tally);
  697. DEBUGFS_ADD(hw_rx_tally);
  698. DEBUGFS_ADD(hw_phy_errors);
  699. DEBUGFS_ADD(phy_noise);
  700. DEBUGFS_ADD(hw_wlan_queue);
  701. DEBUGFS_ADD(hw_pta_queue);
  702. DEBUGFS_ADD(hw_ampdu_info);
  703. DEBUGFS_ADD(ampdu_density);
  704. DEBUGFS_ADD(ampdu_factor);
  705. DEBUGFS_ADD(tx_janitor_last_run);
  706. DEBUGFS_ADD(tx_status_0);
  707. DEBUGFS_ADD(tx_status_1);
  708. DEBUGFS_ADD(tx_status_2);
  709. DEBUGFS_ADD(tx_status_3);
  710. DEBUGFS_ADD(tx_pending_0);
  711. DEBUGFS_ADD(tx_pending_1);
  712. DEBUGFS_ADD(tx_pending_2);
  713. DEBUGFS_ADD(tx_pending_3);
  714. DEBUGFS_ADD(hw_ioread32);
  715. DEBUGFS_ADD(hw_iowrite32);
  716. DEBUGFS_ADD(bug);
  717. DEBUGFS_ADD(erp);
  718. DEBUGFS_ADD(vif_dump);
  719. DEBUGFS_ADD(beacon_int);
  720. DEBUGFS_ADD(pretbtt);
  721. #undef DEBUGFS_ADD
  722. }
  723. void carl9170_debugfs_unregister(struct ar9170 *ar)
  724. {
  725. debugfs_remove_recursive(ar->debug_dir);
  726. }