recv.c 35 KB

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