recv.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. /*
  2. * Copyright (c) 2008-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/dma-mapping.h>
  17. #include <linux/relay.h>
  18. #include "ath9k.h"
  19. #include "ar9003_mac.h"
  20. #define SKB_CB_ATHBUF(__skb) (*((struct ath_buf **)__skb->cb))
  21. static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
  22. {
  23. return sc->ps_enabled &&
  24. (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
  25. }
  26. /*
  27. * Setup and link descriptors.
  28. *
  29. * 11N: we can no longer afford to self link the last descriptor.
  30. * MAC acknowledges BA status as long as it copies frames to host
  31. * buffer (or rx fifo). This can incorrectly acknowledge packets
  32. * to a sender if last desc is self-linked.
  33. */
  34. static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
  35. {
  36. struct ath_hw *ah = sc->sc_ah;
  37. struct ath_common *common = ath9k_hw_common(ah);
  38. struct ath_desc *ds;
  39. struct sk_buff *skb;
  40. ATH_RXBUF_RESET(bf);
  41. ds = bf->bf_desc;
  42. ds->ds_link = 0; /* link to null */
  43. ds->ds_data = bf->bf_buf_addr;
  44. /* virtual addr of the beginning of the buffer. */
  45. skb = bf->bf_mpdu;
  46. BUG_ON(skb == NULL);
  47. ds->ds_vdata = skb->data;
  48. /*
  49. * setup rx descriptors. The rx_bufsize here tells the hardware
  50. * how much data it can DMA to us and that we are prepared
  51. * to process
  52. */
  53. ath9k_hw_setuprxdesc(ah, ds,
  54. common->rx_bufsize,
  55. 0);
  56. if (sc->rx.rxlink == NULL)
  57. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  58. else
  59. *sc->rx.rxlink = bf->bf_daddr;
  60. sc->rx.rxlink = &ds->ds_link;
  61. }
  62. static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
  63. {
  64. /* XXX block beacon interrupts */
  65. ath9k_hw_setantenna(sc->sc_ah, antenna);
  66. sc->rx.defant = antenna;
  67. sc->rx.rxotherant = 0;
  68. }
  69. static void ath_opmode_init(struct ath_softc *sc)
  70. {
  71. struct ath_hw *ah = sc->sc_ah;
  72. struct ath_common *common = ath9k_hw_common(ah);
  73. u32 rfilt, mfilt[2];
  74. /* configure rx filter */
  75. rfilt = ath_calcrxfilter(sc);
  76. ath9k_hw_setrxfilter(ah, rfilt);
  77. /* configure bssid mask */
  78. ath_hw_setbssidmask(common);
  79. /* configure operational mode */
  80. ath9k_hw_setopmode(ah);
  81. /* calculate and install multicast filter */
  82. mfilt[0] = mfilt[1] = ~0;
  83. ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
  84. }
  85. static bool ath_rx_edma_buf_link(struct ath_softc *sc,
  86. enum ath9k_rx_qtype qtype)
  87. {
  88. struct ath_hw *ah = sc->sc_ah;
  89. struct ath_rx_edma *rx_edma;
  90. struct sk_buff *skb;
  91. struct ath_buf *bf;
  92. rx_edma = &sc->rx.rx_edma[qtype];
  93. if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
  94. return false;
  95. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  96. list_del_init(&bf->list);
  97. skb = bf->bf_mpdu;
  98. ATH_RXBUF_RESET(bf);
  99. memset(skb->data, 0, ah->caps.rx_status_len);
  100. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  101. ah->caps.rx_status_len, DMA_TO_DEVICE);
  102. SKB_CB_ATHBUF(skb) = bf;
  103. ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
  104. skb_queue_tail(&rx_edma->rx_fifo, skb);
  105. return true;
  106. }
  107. static void ath_rx_addbuffer_edma(struct ath_softc *sc,
  108. enum ath9k_rx_qtype qtype, int size)
  109. {
  110. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  111. struct ath_buf *bf, *tbf;
  112. if (list_empty(&sc->rx.rxbuf)) {
  113. ath_dbg(common, QUEUE, "No free rx buf available\n");
  114. return;
  115. }
  116. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
  117. if (!ath_rx_edma_buf_link(sc, qtype))
  118. break;
  119. }
  120. static void ath_rx_remove_buffer(struct ath_softc *sc,
  121. enum ath9k_rx_qtype qtype)
  122. {
  123. struct ath_buf *bf;
  124. struct ath_rx_edma *rx_edma;
  125. struct sk_buff *skb;
  126. rx_edma = &sc->rx.rx_edma[qtype];
  127. while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
  128. bf = SKB_CB_ATHBUF(skb);
  129. BUG_ON(!bf);
  130. list_add_tail(&bf->list, &sc->rx.rxbuf);
  131. }
  132. }
  133. static void ath_rx_edma_cleanup(struct ath_softc *sc)
  134. {
  135. struct ath_hw *ah = sc->sc_ah;
  136. struct ath_common *common = ath9k_hw_common(ah);
  137. struct ath_buf *bf;
  138. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  139. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  140. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  141. if (bf->bf_mpdu) {
  142. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  143. common->rx_bufsize,
  144. DMA_BIDIRECTIONAL);
  145. dev_kfree_skb_any(bf->bf_mpdu);
  146. bf->bf_buf_addr = 0;
  147. bf->bf_mpdu = NULL;
  148. }
  149. }
  150. }
  151. static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
  152. {
  153. skb_queue_head_init(&rx_edma->rx_fifo);
  154. rx_edma->rx_fifo_hwsize = size;
  155. }
  156. static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
  157. {
  158. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  159. struct ath_hw *ah = sc->sc_ah;
  160. struct sk_buff *skb;
  161. struct ath_buf *bf;
  162. int error = 0, i;
  163. u32 size;
  164. ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
  165. ah->caps.rx_status_len);
  166. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
  167. ah->caps.rx_lp_qdepth);
  168. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
  169. ah->caps.rx_hp_qdepth);
  170. size = sizeof(struct ath_buf) * nbufs;
  171. bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
  172. if (!bf)
  173. return -ENOMEM;
  174. INIT_LIST_HEAD(&sc->rx.rxbuf);
  175. for (i = 0; i < nbufs; i++, bf++) {
  176. skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
  177. if (!skb) {
  178. error = -ENOMEM;
  179. goto rx_init_fail;
  180. }
  181. memset(skb->data, 0, common->rx_bufsize);
  182. bf->bf_mpdu = skb;
  183. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  184. common->rx_bufsize,
  185. DMA_BIDIRECTIONAL);
  186. if (unlikely(dma_mapping_error(sc->dev,
  187. bf->bf_buf_addr))) {
  188. dev_kfree_skb_any(skb);
  189. bf->bf_mpdu = NULL;
  190. bf->bf_buf_addr = 0;
  191. ath_err(common,
  192. "dma_mapping_error() on RX init\n");
  193. error = -ENOMEM;
  194. goto rx_init_fail;
  195. }
  196. list_add_tail(&bf->list, &sc->rx.rxbuf);
  197. }
  198. return 0;
  199. rx_init_fail:
  200. ath_rx_edma_cleanup(sc);
  201. return error;
  202. }
  203. static void ath_edma_start_recv(struct ath_softc *sc)
  204. {
  205. ath9k_hw_rxena(sc->sc_ah);
  206. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
  207. sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
  208. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
  209. sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
  210. ath_opmode_init(sc);
  211. ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
  212. }
  213. static void ath_edma_stop_recv(struct ath_softc *sc)
  214. {
  215. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  216. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  217. }
  218. int ath_rx_init(struct ath_softc *sc, int nbufs)
  219. {
  220. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  221. struct sk_buff *skb;
  222. struct ath_buf *bf;
  223. int error = 0;
  224. spin_lock_init(&sc->sc_pcu_lock);
  225. common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
  226. sc->sc_ah->caps.rx_status_len;
  227. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  228. return ath_rx_edma_init(sc, nbufs);
  229. } else {
  230. ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
  231. common->cachelsz, common->rx_bufsize);
  232. /* Initialize rx descriptors */
  233. error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
  234. "rx", nbufs, 1, 0);
  235. if (error != 0) {
  236. ath_err(common,
  237. "failed to allocate rx descriptors: %d\n",
  238. error);
  239. goto err;
  240. }
  241. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  242. skb = ath_rxbuf_alloc(common, common->rx_bufsize,
  243. GFP_KERNEL);
  244. if (skb == NULL) {
  245. error = -ENOMEM;
  246. goto err;
  247. }
  248. bf->bf_mpdu = skb;
  249. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  250. common->rx_bufsize,
  251. DMA_FROM_DEVICE);
  252. if (unlikely(dma_mapping_error(sc->dev,
  253. bf->bf_buf_addr))) {
  254. dev_kfree_skb_any(skb);
  255. bf->bf_mpdu = NULL;
  256. bf->bf_buf_addr = 0;
  257. ath_err(common,
  258. "dma_mapping_error() on RX init\n");
  259. error = -ENOMEM;
  260. goto err;
  261. }
  262. }
  263. sc->rx.rxlink = NULL;
  264. }
  265. err:
  266. if (error)
  267. ath_rx_cleanup(sc);
  268. return error;
  269. }
  270. void ath_rx_cleanup(struct ath_softc *sc)
  271. {
  272. struct ath_hw *ah = sc->sc_ah;
  273. struct ath_common *common = ath9k_hw_common(ah);
  274. struct sk_buff *skb;
  275. struct ath_buf *bf;
  276. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  277. ath_rx_edma_cleanup(sc);
  278. return;
  279. } else {
  280. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  281. skb = bf->bf_mpdu;
  282. if (skb) {
  283. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  284. common->rx_bufsize,
  285. DMA_FROM_DEVICE);
  286. dev_kfree_skb(skb);
  287. bf->bf_buf_addr = 0;
  288. bf->bf_mpdu = NULL;
  289. }
  290. }
  291. }
  292. }
  293. /*
  294. * Calculate the receive filter according to the
  295. * operating mode and state:
  296. *
  297. * o always accept unicast, broadcast, and multicast traffic
  298. * o maintain current state of phy error reception (the hal
  299. * may enable phy error frames for noise immunity work)
  300. * o probe request frames are accepted only when operating in
  301. * hostap, adhoc, or monitor modes
  302. * o enable promiscuous mode according to the interface state
  303. * o accept beacons:
  304. * - when operating in adhoc mode so the 802.11 layer creates
  305. * node table entries for peers,
  306. * - when operating in station mode for collecting rssi data when
  307. * the station is otherwise quiet, or
  308. * - when operating as a repeater so we see repeater-sta beacons
  309. * - when scanning
  310. */
  311. u32 ath_calcrxfilter(struct ath_softc *sc)
  312. {
  313. u32 rfilt;
  314. rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
  315. | ATH9K_RX_FILTER_MCAST;
  316. if (sc->rx.rxfilter & FIF_PROBE_REQ)
  317. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  318. /*
  319. * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
  320. * mode interface or when in monitor mode. AP mode does not need this
  321. * since it receives all in-BSS frames anyway.
  322. */
  323. if (sc->sc_ah->is_monitoring)
  324. rfilt |= ATH9K_RX_FILTER_PROM;
  325. if (sc->rx.rxfilter & FIF_CONTROL)
  326. rfilt |= ATH9K_RX_FILTER_CONTROL;
  327. if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
  328. (sc->nvifs <= 1) &&
  329. !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
  330. rfilt |= ATH9K_RX_FILTER_MYBEACON;
  331. else
  332. rfilt |= ATH9K_RX_FILTER_BEACON;
  333. if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
  334. (sc->rx.rxfilter & FIF_PSPOLL))
  335. rfilt |= ATH9K_RX_FILTER_PSPOLL;
  336. if (conf_is_ht(&sc->hw->conf))
  337. rfilt |= ATH9K_RX_FILTER_COMP_BAR;
  338. if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
  339. /* This is needed for older chips */
  340. if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
  341. rfilt |= ATH9K_RX_FILTER_PROM;
  342. rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
  343. }
  344. if (AR_SREV_9550(sc->sc_ah))
  345. rfilt |= ATH9K_RX_FILTER_4ADDRESS;
  346. return rfilt;
  347. }
  348. int ath_startrecv(struct ath_softc *sc)
  349. {
  350. struct ath_hw *ah = sc->sc_ah;
  351. struct ath_buf *bf, *tbf;
  352. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  353. ath_edma_start_recv(sc);
  354. return 0;
  355. }
  356. if (list_empty(&sc->rx.rxbuf))
  357. goto start_recv;
  358. sc->rx.rxlink = NULL;
  359. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
  360. ath_rx_buf_link(sc, bf);
  361. }
  362. /* We could have deleted elements so the list may be empty now */
  363. if (list_empty(&sc->rx.rxbuf))
  364. goto start_recv;
  365. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  366. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  367. ath9k_hw_rxena(ah);
  368. start_recv:
  369. ath_opmode_init(sc);
  370. ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
  371. return 0;
  372. }
  373. static void ath_flushrecv(struct ath_softc *sc)
  374. {
  375. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  376. ath_rx_tasklet(sc, 1, true);
  377. ath_rx_tasklet(sc, 1, false);
  378. }
  379. bool ath_stoprecv(struct ath_softc *sc)
  380. {
  381. struct ath_hw *ah = sc->sc_ah;
  382. bool stopped, reset = false;
  383. ath9k_hw_abortpcurecv(ah);
  384. ath9k_hw_setrxfilter(ah, 0);
  385. stopped = ath9k_hw_stopdmarecv(ah, &reset);
  386. ath_flushrecv(sc);
  387. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  388. ath_edma_stop_recv(sc);
  389. else
  390. sc->rx.rxlink = NULL;
  391. if (!(ah->ah_flags & AH_UNPLUGGED) &&
  392. unlikely(!stopped)) {
  393. ath_err(ath9k_hw_common(sc->sc_ah),
  394. "Could not stop RX, we could be "
  395. "confusing the DMA engine when we start RX up\n");
  396. ATH_DBG_WARN_ON_ONCE(!stopped);
  397. }
  398. return stopped && !reset;
  399. }
  400. static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
  401. {
  402. /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
  403. struct ieee80211_mgmt *mgmt;
  404. u8 *pos, *end, id, elen;
  405. struct ieee80211_tim_ie *tim;
  406. mgmt = (struct ieee80211_mgmt *)skb->data;
  407. pos = mgmt->u.beacon.variable;
  408. end = skb->data + skb->len;
  409. while (pos + 2 < end) {
  410. id = *pos++;
  411. elen = *pos++;
  412. if (pos + elen > end)
  413. break;
  414. if (id == WLAN_EID_TIM) {
  415. if (elen < sizeof(*tim))
  416. break;
  417. tim = (struct ieee80211_tim_ie *) pos;
  418. if (tim->dtim_count != 0)
  419. break;
  420. return tim->bitmap_ctrl & 0x01;
  421. }
  422. pos += elen;
  423. }
  424. return false;
  425. }
  426. static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
  427. {
  428. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  429. if (skb->len < 24 + 8 + 2 + 2)
  430. return;
  431. sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
  432. if (sc->ps_flags & PS_BEACON_SYNC) {
  433. sc->ps_flags &= ~PS_BEACON_SYNC;
  434. ath_dbg(common, PS,
  435. "Reconfigure beacon timers based on synchronized timestamp\n");
  436. ath9k_set_beacon(sc);
  437. }
  438. if (ath_beacon_dtim_pending_cab(skb)) {
  439. /*
  440. * Remain awake waiting for buffered broadcast/multicast
  441. * frames. If the last broadcast/multicast frame is not
  442. * received properly, the next beacon frame will work as
  443. * a backup trigger for returning into NETWORK SLEEP state,
  444. * so we are waiting for it as well.
  445. */
  446. ath_dbg(common, PS,
  447. "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
  448. sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
  449. return;
  450. }
  451. if (sc->ps_flags & PS_WAIT_FOR_CAB) {
  452. /*
  453. * This can happen if a broadcast frame is dropped or the AP
  454. * fails to send a frame indicating that all CAB frames have
  455. * been delivered.
  456. */
  457. sc->ps_flags &= ~PS_WAIT_FOR_CAB;
  458. ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
  459. }
  460. }
  461. static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
  462. {
  463. struct ieee80211_hdr *hdr;
  464. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  465. hdr = (struct ieee80211_hdr *)skb->data;
  466. /* Process Beacon and CAB receive in PS state */
  467. if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
  468. && mybeacon) {
  469. ath_rx_ps_beacon(sc, skb);
  470. } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
  471. (ieee80211_is_data(hdr->frame_control) ||
  472. ieee80211_is_action(hdr->frame_control)) &&
  473. is_multicast_ether_addr(hdr->addr1) &&
  474. !ieee80211_has_moredata(hdr->frame_control)) {
  475. /*
  476. * No more broadcast/multicast frames to be received at this
  477. * point.
  478. */
  479. sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
  480. ath_dbg(common, PS,
  481. "All PS CAB frames received, back to sleep\n");
  482. } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
  483. !is_multicast_ether_addr(hdr->addr1) &&
  484. !ieee80211_has_morefrags(hdr->frame_control)) {
  485. sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
  486. ath_dbg(common, PS,
  487. "Going back to sleep after having received PS-Poll data (0x%lx)\n",
  488. sc->ps_flags & (PS_WAIT_FOR_BEACON |
  489. PS_WAIT_FOR_CAB |
  490. PS_WAIT_FOR_PSPOLL_DATA |
  491. PS_WAIT_FOR_TX_ACK));
  492. }
  493. }
  494. static bool ath_edma_get_buffers(struct ath_softc *sc,
  495. enum ath9k_rx_qtype qtype,
  496. struct ath_rx_status *rs,
  497. struct ath_buf **dest)
  498. {
  499. struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
  500. struct ath_hw *ah = sc->sc_ah;
  501. struct ath_common *common = ath9k_hw_common(ah);
  502. struct sk_buff *skb;
  503. struct ath_buf *bf;
  504. int ret;
  505. skb = skb_peek(&rx_edma->rx_fifo);
  506. if (!skb)
  507. return false;
  508. bf = SKB_CB_ATHBUF(skb);
  509. BUG_ON(!bf);
  510. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  511. common->rx_bufsize, DMA_FROM_DEVICE);
  512. ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
  513. if (ret == -EINPROGRESS) {
  514. /*let device gain the buffer again*/
  515. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  516. common->rx_bufsize, DMA_FROM_DEVICE);
  517. return false;
  518. }
  519. __skb_unlink(skb, &rx_edma->rx_fifo);
  520. if (ret == -EINVAL) {
  521. /* corrupt descriptor, skip this one and the following one */
  522. list_add_tail(&bf->list, &sc->rx.rxbuf);
  523. ath_rx_edma_buf_link(sc, qtype);
  524. skb = skb_peek(&rx_edma->rx_fifo);
  525. if (skb) {
  526. bf = SKB_CB_ATHBUF(skb);
  527. BUG_ON(!bf);
  528. __skb_unlink(skb, &rx_edma->rx_fifo);
  529. list_add_tail(&bf->list, &sc->rx.rxbuf);
  530. ath_rx_edma_buf_link(sc, qtype);
  531. }
  532. bf = NULL;
  533. }
  534. *dest = bf;
  535. return true;
  536. }
  537. static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
  538. struct ath_rx_status *rs,
  539. enum ath9k_rx_qtype qtype)
  540. {
  541. struct ath_buf *bf = NULL;
  542. while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
  543. if (!bf)
  544. continue;
  545. return bf;
  546. }
  547. return NULL;
  548. }
  549. static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
  550. struct ath_rx_status *rs)
  551. {
  552. struct ath_hw *ah = sc->sc_ah;
  553. struct ath_common *common = ath9k_hw_common(ah);
  554. struct ath_desc *ds;
  555. struct ath_buf *bf;
  556. int ret;
  557. if (list_empty(&sc->rx.rxbuf)) {
  558. sc->rx.rxlink = NULL;
  559. return NULL;
  560. }
  561. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  562. ds = bf->bf_desc;
  563. /*
  564. * Must provide the virtual address of the current
  565. * descriptor, the physical address, and the virtual
  566. * address of the next descriptor in the h/w chain.
  567. * This allows the HAL to look ahead to see if the
  568. * hardware is done with a descriptor by checking the
  569. * done bit in the following descriptor and the address
  570. * of the current descriptor the DMA engine is working
  571. * on. All this is necessary because of our use of
  572. * a self-linked list to avoid rx overruns.
  573. */
  574. ret = ath9k_hw_rxprocdesc(ah, ds, rs);
  575. if (ret == -EINPROGRESS) {
  576. struct ath_rx_status trs;
  577. struct ath_buf *tbf;
  578. struct ath_desc *tds;
  579. memset(&trs, 0, sizeof(trs));
  580. if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
  581. sc->rx.rxlink = NULL;
  582. return NULL;
  583. }
  584. tbf = list_entry(bf->list.next, struct ath_buf, list);
  585. /*
  586. * On some hardware the descriptor status words could
  587. * get corrupted, including the done bit. Because of
  588. * this, check if the next descriptor's done bit is
  589. * set or not.
  590. *
  591. * If the next descriptor's done bit is set, the current
  592. * descriptor has been corrupted. Force s/w to discard
  593. * this descriptor and continue...
  594. */
  595. tds = tbf->bf_desc;
  596. ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
  597. if (ret == -EINPROGRESS)
  598. return NULL;
  599. }
  600. list_del(&bf->list);
  601. if (!bf->bf_mpdu)
  602. return bf;
  603. /*
  604. * Synchronize the DMA transfer with CPU before
  605. * 1. accessing the frame
  606. * 2. requeueing the same buffer to h/w
  607. */
  608. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  609. common->rx_bufsize,
  610. DMA_FROM_DEVICE);
  611. return bf;
  612. }
  613. /* Assumes you've already done the endian to CPU conversion */
  614. static bool ath9k_rx_accept(struct ath_common *common,
  615. struct ieee80211_hdr *hdr,
  616. struct ieee80211_rx_status *rxs,
  617. struct ath_rx_status *rx_stats,
  618. bool *decrypt_error)
  619. {
  620. struct ath_softc *sc = (struct ath_softc *) common->priv;
  621. bool is_mc, is_valid_tkip, strip_mic, mic_error;
  622. struct ath_hw *ah = common->ah;
  623. __le16 fc;
  624. u8 rx_status_len = ah->caps.rx_status_len;
  625. fc = hdr->frame_control;
  626. is_mc = !!is_multicast_ether_addr(hdr->addr1);
  627. is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
  628. test_bit(rx_stats->rs_keyix, common->tkip_keymap);
  629. strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
  630. ieee80211_has_protected(fc) &&
  631. !(rx_stats->rs_status &
  632. (ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
  633. ATH9K_RXERR_KEYMISS));
  634. /*
  635. * Key miss events are only relevant for pairwise keys where the
  636. * descriptor does contain a valid key index. This has been observed
  637. * mostly with CCMP encryption.
  638. */
  639. if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID ||
  640. !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
  641. rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
  642. if (!rx_stats->rs_datalen) {
  643. RX_STAT_INC(rx_len_err);
  644. return false;
  645. }
  646. /*
  647. * rs_status follows rs_datalen so if rs_datalen is too large
  648. * we can take a hint that hardware corrupted it, so ignore
  649. * those frames.
  650. */
  651. if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len)) {
  652. RX_STAT_INC(rx_len_err);
  653. return false;
  654. }
  655. /* Only use error bits from the last fragment */
  656. if (rx_stats->rs_more)
  657. return true;
  658. mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
  659. !ieee80211_has_morefrags(fc) &&
  660. !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
  661. (rx_stats->rs_status & ATH9K_RXERR_MIC);
  662. /*
  663. * The rx_stats->rs_status will not be set until the end of the
  664. * chained descriptors so it can be ignored if rs_more is set. The
  665. * rs_more will be false at the last element of the chained
  666. * descriptors.
  667. */
  668. if (rx_stats->rs_status != 0) {
  669. u8 status_mask;
  670. if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
  671. rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
  672. mic_error = false;
  673. }
  674. if (rx_stats->rs_status & ATH9K_RXERR_PHY)
  675. return false;
  676. if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
  677. (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
  678. *decrypt_error = true;
  679. mic_error = false;
  680. }
  681. /*
  682. * Reject error frames with the exception of
  683. * decryption and MIC failures. For monitor mode,
  684. * we also ignore the CRC error.
  685. */
  686. status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
  687. ATH9K_RXERR_KEYMISS;
  688. if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
  689. status_mask |= ATH9K_RXERR_CRC;
  690. if (rx_stats->rs_status & ~status_mask)
  691. return false;
  692. }
  693. /*
  694. * For unicast frames the MIC error bit can have false positives,
  695. * so all MIC error reports need to be validated in software.
  696. * False negatives are not common, so skip software verification
  697. * if the hardware considers the MIC valid.
  698. */
  699. if (strip_mic)
  700. rxs->flag |= RX_FLAG_MMIC_STRIPPED;
  701. else if (is_mc && mic_error)
  702. rxs->flag |= RX_FLAG_MMIC_ERROR;
  703. return true;
  704. }
  705. static int ath9k_process_rate(struct ath_common *common,
  706. struct ieee80211_hw *hw,
  707. struct ath_rx_status *rx_stats,
  708. struct ieee80211_rx_status *rxs)
  709. {
  710. struct ieee80211_supported_band *sband;
  711. enum ieee80211_band band;
  712. unsigned int i = 0;
  713. struct ath_softc __maybe_unused *sc = common->priv;
  714. band = hw->conf.channel->band;
  715. sband = hw->wiphy->bands[band];
  716. if (rx_stats->rs_rate & 0x80) {
  717. /* HT rate */
  718. rxs->flag |= RX_FLAG_HT;
  719. if (rx_stats->rs_flags & ATH9K_RX_2040)
  720. rxs->flag |= RX_FLAG_40MHZ;
  721. if (rx_stats->rs_flags & ATH9K_RX_GI)
  722. rxs->flag |= RX_FLAG_SHORT_GI;
  723. rxs->rate_idx = rx_stats->rs_rate & 0x7f;
  724. return 0;
  725. }
  726. for (i = 0; i < sband->n_bitrates; i++) {
  727. if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
  728. rxs->rate_idx = i;
  729. return 0;
  730. }
  731. if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
  732. rxs->flag |= RX_FLAG_SHORTPRE;
  733. rxs->rate_idx = i;
  734. return 0;
  735. }
  736. }
  737. /*
  738. * No valid hardware bitrate found -- we should not get here
  739. * because hardware has already validated this frame as OK.
  740. */
  741. ath_dbg(common, ANY,
  742. "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
  743. rx_stats->rs_rate);
  744. RX_STAT_INC(rx_rate_err);
  745. return -EINVAL;
  746. }
  747. static void ath9k_process_rssi(struct ath_common *common,
  748. struct ieee80211_hw *hw,
  749. struct ieee80211_hdr *hdr,
  750. struct ath_rx_status *rx_stats)
  751. {
  752. struct ath_softc *sc = hw->priv;
  753. struct ath_hw *ah = common->ah;
  754. int last_rssi;
  755. int rssi = rx_stats->rs_rssi;
  756. if (!rx_stats->is_mybeacon ||
  757. ((ah->opmode != NL80211_IFTYPE_STATION) &&
  758. (ah->opmode != NL80211_IFTYPE_ADHOC)))
  759. return;
  760. if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
  761. ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
  762. last_rssi = sc->last_rssi;
  763. if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
  764. rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
  765. if (rssi < 0)
  766. rssi = 0;
  767. /* Update Beacon RSSI, this is used by ANI. */
  768. ah->stats.avgbrssi = rssi;
  769. }
  770. /*
  771. * For Decrypt or Demic errors, we only mark packet status here and always push
  772. * up the frame up to let mac80211 handle the actual error case, be it no
  773. * decryption key or real decryption error. This let us keep statistics there.
  774. */
  775. static int ath9k_rx_skb_preprocess(struct ath_common *common,
  776. struct ieee80211_hw *hw,
  777. struct ieee80211_hdr *hdr,
  778. struct ath_rx_status *rx_stats,
  779. struct ieee80211_rx_status *rx_status,
  780. bool *decrypt_error)
  781. {
  782. struct ath_hw *ah = common->ah;
  783. /*
  784. * everything but the rate is checked here, the rate check is done
  785. * separately to avoid doing two lookups for a rate for each frame.
  786. */
  787. if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
  788. return -EINVAL;
  789. /* Only use status info from the last fragment */
  790. if (rx_stats->rs_more)
  791. return 0;
  792. ath9k_process_rssi(common, hw, hdr, rx_stats);
  793. if (ath9k_process_rate(common, hw, rx_stats, rx_status))
  794. return -EINVAL;
  795. rx_status->band = hw->conf.channel->band;
  796. rx_status->freq = hw->conf.channel->center_freq;
  797. rx_status->signal = ah->noise + rx_stats->rs_rssi;
  798. rx_status->antenna = rx_stats->rs_antenna;
  799. rx_status->flag |= RX_FLAG_MACTIME_END;
  800. if (rx_stats->rs_moreaggr)
  801. rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
  802. return 0;
  803. }
  804. static void ath9k_rx_skb_postprocess(struct ath_common *common,
  805. struct sk_buff *skb,
  806. struct ath_rx_status *rx_stats,
  807. struct ieee80211_rx_status *rxs,
  808. bool decrypt_error)
  809. {
  810. struct ath_hw *ah = common->ah;
  811. struct ieee80211_hdr *hdr;
  812. int hdrlen, padpos, padsize;
  813. u8 keyix;
  814. __le16 fc;
  815. /* see if any padding is done by the hw and remove it */
  816. hdr = (struct ieee80211_hdr *) skb->data;
  817. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  818. fc = hdr->frame_control;
  819. padpos = ath9k_cmn_padpos(hdr->frame_control);
  820. /* The MAC header is padded to have 32-bit boundary if the
  821. * packet payload is non-zero. The general calculation for
  822. * padsize would take into account odd header lengths:
  823. * padsize = (4 - padpos % 4) % 4; However, since only
  824. * even-length headers are used, padding can only be 0 or 2
  825. * bytes and we can optimize this a bit. In addition, we must
  826. * not try to remove padding from short control frames that do
  827. * not have payload. */
  828. padsize = padpos & 3;
  829. if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
  830. memmove(skb->data + padsize, skb->data, padpos);
  831. skb_pull(skb, padsize);
  832. }
  833. keyix = rx_stats->rs_keyix;
  834. if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
  835. ieee80211_has_protected(fc)) {
  836. rxs->flag |= RX_FLAG_DECRYPTED;
  837. } else if (ieee80211_has_protected(fc)
  838. && !decrypt_error && skb->len >= hdrlen + 4) {
  839. keyix = skb->data[hdrlen + 3] >> 6;
  840. if (test_bit(keyix, common->keymap))
  841. rxs->flag |= RX_FLAG_DECRYPTED;
  842. }
  843. if (ah->sw_mgmt_crypto &&
  844. (rxs->flag & RX_FLAG_DECRYPTED) &&
  845. ieee80211_is_mgmt(fc))
  846. /* Use software decrypt for management frames. */
  847. rxs->flag &= ~RX_FLAG_DECRYPTED;
  848. }
  849. #ifdef CONFIG_ATH9K_DEBUGFS
  850. static s8 fix_rssi_inv_only(u8 rssi_val)
  851. {
  852. if (rssi_val == 128)
  853. rssi_val = 0;
  854. return (s8) rssi_val;
  855. }
  856. #endif
  857. /* returns 1 if this was a spectral frame, even if not handled. */
  858. static int ath_process_fft(struct ath_softc *sc, struct ieee80211_hdr *hdr,
  859. struct ath_rx_status *rs, u64 tsf)
  860. {
  861. #ifdef CONFIG_ATH9K_DEBUGFS
  862. struct ath_hw *ah = sc->sc_ah;
  863. u8 bins[SPECTRAL_HT20_NUM_BINS];
  864. u8 *vdata = (u8 *)hdr;
  865. struct fft_sample_ht20 fft_sample;
  866. struct ath_radar_info *radar_info;
  867. struct ath_ht20_mag_info *mag_info;
  868. int len = rs->rs_datalen;
  869. int dc_pos;
  870. u16 length, max_magnitude;
  871. /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
  872. * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
  873. * yet, but this is supposed to be possible as well.
  874. */
  875. if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
  876. rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
  877. rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
  878. return 0;
  879. /* check if spectral scan bit is set. This does not have to be checked
  880. * if received through a SPECTRAL phy error, but shouldn't hurt.
  881. */
  882. radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
  883. if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
  884. return 0;
  885. /* Variation in the data length is possible and will be fixed later.
  886. * Note that we only support HT20 for now.
  887. *
  888. * TODO: add HT20_40 support as well.
  889. */
  890. if ((len > SPECTRAL_HT20_TOTAL_DATA_LEN + 2) ||
  891. (len < SPECTRAL_HT20_TOTAL_DATA_LEN - 1))
  892. return 1;
  893. fft_sample.tlv.type = ATH_FFT_SAMPLE_HT20;
  894. length = sizeof(fft_sample) - sizeof(fft_sample.tlv);
  895. fft_sample.tlv.length = __cpu_to_be16(length);
  896. fft_sample.freq = __cpu_to_be16(ah->curchan->chan->center_freq);
  897. fft_sample.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl0);
  898. fft_sample.noise = ah->noise;
  899. switch (len - SPECTRAL_HT20_TOTAL_DATA_LEN) {
  900. case 0:
  901. /* length correct, nothing to do. */
  902. memcpy(bins, vdata, SPECTRAL_HT20_NUM_BINS);
  903. break;
  904. case -1:
  905. /* first byte missing, duplicate it. */
  906. memcpy(&bins[1], vdata, SPECTRAL_HT20_NUM_BINS - 1);
  907. bins[0] = vdata[0];
  908. break;
  909. case 2:
  910. /* MAC added 2 extra bytes at bin 30 and 32, remove them. */
  911. memcpy(bins, vdata, 30);
  912. bins[30] = vdata[31];
  913. memcpy(&bins[31], &vdata[33], SPECTRAL_HT20_NUM_BINS - 31);
  914. break;
  915. case 1:
  916. /* MAC added 2 extra bytes AND first byte is missing. */
  917. bins[0] = vdata[0];
  918. memcpy(&bins[0], vdata, 30);
  919. bins[31] = vdata[31];
  920. memcpy(&bins[32], &vdata[33], SPECTRAL_HT20_NUM_BINS - 32);
  921. break;
  922. default:
  923. return 1;
  924. }
  925. /* DC value (value in the middle) is the blind spot of the spectral
  926. * sample and invalid, interpolate it.
  927. */
  928. dc_pos = SPECTRAL_HT20_NUM_BINS / 2;
  929. bins[dc_pos] = (bins[dc_pos + 1] + bins[dc_pos - 1]) / 2;
  930. /* mag data is at the end of the frame, in front of radar_info */
  931. mag_info = ((struct ath_ht20_mag_info *)radar_info) - 1;
  932. /* copy raw bins without scaling them */
  933. memcpy(fft_sample.data, bins, SPECTRAL_HT20_NUM_BINS);
  934. fft_sample.max_exp = mag_info->max_exp & 0xf;
  935. max_magnitude = spectral_max_magnitude(mag_info->all_bins);
  936. fft_sample.max_magnitude = __cpu_to_be16(max_magnitude);
  937. fft_sample.max_index = spectral_max_index(mag_info->all_bins);
  938. fft_sample.bitmap_weight = spectral_bitmap_weight(mag_info->all_bins);
  939. fft_sample.tsf = __cpu_to_be64(tsf);
  940. ath_debug_send_fft_sample(sc, &fft_sample.tlv);
  941. return 1;
  942. #else
  943. return 0;
  944. #endif
  945. }
  946. static void ath9k_apply_ampdu_details(struct ath_softc *sc,
  947. struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
  948. {
  949. if (rs->rs_isaggr) {
  950. rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
  951. rxs->ampdu_reference = sc->rx.ampdu_ref;
  952. if (!rs->rs_moreaggr) {
  953. rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
  954. sc->rx.ampdu_ref++;
  955. }
  956. if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
  957. rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
  958. }
  959. }
  960. int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
  961. {
  962. struct ath_buf *bf;
  963. struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
  964. struct ieee80211_rx_status *rxs;
  965. struct ath_hw *ah = sc->sc_ah;
  966. struct ath_common *common = ath9k_hw_common(ah);
  967. struct ieee80211_hw *hw = sc->hw;
  968. struct ieee80211_hdr *hdr;
  969. int retval;
  970. struct ath_rx_status rs;
  971. enum ath9k_rx_qtype qtype;
  972. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  973. int dma_type;
  974. u8 rx_status_len = ah->caps.rx_status_len;
  975. u64 tsf = 0;
  976. u32 tsf_lower = 0;
  977. unsigned long flags;
  978. if (edma)
  979. dma_type = DMA_BIDIRECTIONAL;
  980. else
  981. dma_type = DMA_FROM_DEVICE;
  982. qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
  983. tsf = ath9k_hw_gettsf64(ah);
  984. tsf_lower = tsf & 0xffffffff;
  985. do {
  986. bool decrypt_error = false;
  987. memset(&rs, 0, sizeof(rs));
  988. if (edma)
  989. bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
  990. else
  991. bf = ath_get_next_rx_buf(sc, &rs);
  992. if (!bf)
  993. break;
  994. skb = bf->bf_mpdu;
  995. if (!skb)
  996. continue;
  997. /*
  998. * Take frame header from the first fragment and RX status from
  999. * the last one.
  1000. */
  1001. if (sc->rx.frag)
  1002. hdr_skb = sc->rx.frag;
  1003. else
  1004. hdr_skb = skb;
  1005. hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
  1006. rxs = IEEE80211_SKB_RXCB(hdr_skb);
  1007. if (ieee80211_is_beacon(hdr->frame_control)) {
  1008. RX_STAT_INC(rx_beacons);
  1009. if (!is_zero_ether_addr(common->curbssid) &&
  1010. ether_addr_equal(hdr->addr3, common->curbssid))
  1011. rs.is_mybeacon = true;
  1012. else
  1013. rs.is_mybeacon = false;
  1014. }
  1015. else
  1016. rs.is_mybeacon = false;
  1017. if (ieee80211_is_data_present(hdr->frame_control) &&
  1018. !ieee80211_is_qos_nullfunc(hdr->frame_control))
  1019. sc->rx.num_pkts++;
  1020. ath_debug_stat_rx(sc, &rs);
  1021. memset(rxs, 0, sizeof(struct ieee80211_rx_status));
  1022. rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
  1023. if (rs.rs_tstamp > tsf_lower &&
  1024. unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
  1025. rxs->mactime -= 0x100000000ULL;
  1026. if (rs.rs_tstamp < tsf_lower &&
  1027. unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
  1028. rxs->mactime += 0x100000000ULL;
  1029. if (rs.rs_status & ATH9K_RXERR_PHY) {
  1030. if (ath_process_fft(sc, hdr, &rs, rxs->mactime)) {
  1031. RX_STAT_INC(rx_spectral);
  1032. goto requeue_drop_frag;
  1033. }
  1034. }
  1035. retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
  1036. rxs, &decrypt_error);
  1037. if (retval)
  1038. goto requeue_drop_frag;
  1039. if (rs.is_mybeacon) {
  1040. sc->hw_busy_count = 0;
  1041. ath_start_rx_poll(sc, 3);
  1042. }
  1043. /* Ensure we always have an skb to requeue once we are done
  1044. * processing the current buffer's skb */
  1045. requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
  1046. /* If there is no memory we ignore the current RX'd frame,
  1047. * tell hardware it can give us a new frame using the old
  1048. * skb and put it at the tail of the sc->rx.rxbuf list for
  1049. * processing. */
  1050. if (!requeue_skb) {
  1051. RX_STAT_INC(rx_oom_err);
  1052. goto requeue_drop_frag;
  1053. }
  1054. /* Unmap the frame */
  1055. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  1056. common->rx_bufsize,
  1057. dma_type);
  1058. skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
  1059. if (ah->caps.rx_status_len)
  1060. skb_pull(skb, ah->caps.rx_status_len);
  1061. if (!rs.rs_more)
  1062. ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
  1063. rxs, decrypt_error);
  1064. /* We will now give hardware our shiny new allocated skb */
  1065. bf->bf_mpdu = requeue_skb;
  1066. bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
  1067. common->rx_bufsize,
  1068. dma_type);
  1069. if (unlikely(dma_mapping_error(sc->dev,
  1070. bf->bf_buf_addr))) {
  1071. dev_kfree_skb_any(requeue_skb);
  1072. bf->bf_mpdu = NULL;
  1073. bf->bf_buf_addr = 0;
  1074. ath_err(common, "dma_mapping_error() on RX\n");
  1075. ieee80211_rx(hw, skb);
  1076. break;
  1077. }
  1078. if (rs.rs_more) {
  1079. RX_STAT_INC(rx_frags);
  1080. /*
  1081. * rs_more indicates chained descriptors which can be
  1082. * used to link buffers together for a sort of
  1083. * scatter-gather operation.
  1084. */
  1085. if (sc->rx.frag) {
  1086. /* too many fragments - cannot handle frame */
  1087. dev_kfree_skb_any(sc->rx.frag);
  1088. dev_kfree_skb_any(skb);
  1089. RX_STAT_INC(rx_too_many_frags_err);
  1090. skb = NULL;
  1091. }
  1092. sc->rx.frag = skb;
  1093. goto requeue;
  1094. }
  1095. if (sc->rx.frag) {
  1096. int space = skb->len - skb_tailroom(hdr_skb);
  1097. if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
  1098. dev_kfree_skb(skb);
  1099. RX_STAT_INC(rx_oom_err);
  1100. goto requeue_drop_frag;
  1101. }
  1102. sc->rx.frag = NULL;
  1103. skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
  1104. skb->len);
  1105. dev_kfree_skb_any(skb);
  1106. skb = hdr_skb;
  1107. }
  1108. if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
  1109. /*
  1110. * change the default rx antenna if rx diversity
  1111. * chooses the other antenna 3 times in a row.
  1112. */
  1113. if (sc->rx.defant != rs.rs_antenna) {
  1114. if (++sc->rx.rxotherant >= 3)
  1115. ath_setdefantenna(sc, rs.rs_antenna);
  1116. } else {
  1117. sc->rx.rxotherant = 0;
  1118. }
  1119. }
  1120. if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
  1121. skb_trim(skb, skb->len - 8);
  1122. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  1123. if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
  1124. PS_WAIT_FOR_CAB |
  1125. PS_WAIT_FOR_PSPOLL_DATA)) ||
  1126. ath9k_check_auto_sleep(sc))
  1127. ath_rx_ps(sc, skb, rs.is_mybeacon);
  1128. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  1129. if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx == 3)
  1130. ath_ant_comb_scan(sc, &rs);
  1131. ath9k_apply_ampdu_details(sc, &rs, rxs);
  1132. ieee80211_rx(hw, skb);
  1133. requeue_drop_frag:
  1134. if (sc->rx.frag) {
  1135. dev_kfree_skb_any(sc->rx.frag);
  1136. sc->rx.frag = NULL;
  1137. }
  1138. requeue:
  1139. list_add_tail(&bf->list, &sc->rx.rxbuf);
  1140. if (flush)
  1141. continue;
  1142. if (edma) {
  1143. ath_rx_edma_buf_link(sc, qtype);
  1144. } else {
  1145. ath_rx_buf_link(sc, bf);
  1146. ath9k_hw_rxena(ah);
  1147. }
  1148. } while (1);
  1149. if (!(ah->imask & ATH9K_INT_RXEOL)) {
  1150. ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  1151. ath9k_hw_set_interrupts(ah);
  1152. }
  1153. return 0;
  1154. }