recv.c 37 KB

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