recv.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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. bool flush)
  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. ds = bf->bf_desc;
  41. ds->ds_link = 0; /* link to null */
  42. ds->ds_data = bf->bf_buf_addr;
  43. /* virtual addr of the beginning of the buffer. */
  44. skb = bf->bf_mpdu;
  45. BUG_ON(skb == NULL);
  46. ds->ds_vdata = skb->data;
  47. /*
  48. * setup rx descriptors. The rx_bufsize here tells the hardware
  49. * how much data it can DMA to us and that we are prepared
  50. * to process
  51. */
  52. ath9k_hw_setuprxdesc(ah, ds,
  53. common->rx_bufsize,
  54. 0);
  55. if (sc->rx.rxlink)
  56. *sc->rx.rxlink = bf->bf_daddr;
  57. else if (!flush)
  58. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  59. sc->rx.rxlink = &ds->ds_link;
  60. }
  61. static void ath_rx_buf_relink(struct ath_softc *sc, struct ath_rxbuf *bf,
  62. bool flush)
  63. {
  64. if (sc->rx.buf_hold)
  65. ath_rx_buf_link(sc, sc->rx.buf_hold, flush);
  66. sc->rx.buf_hold = bf;
  67. }
  68. static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
  69. {
  70. /* XXX block beacon interrupts */
  71. ath9k_hw_setantenna(sc->sc_ah, antenna);
  72. sc->rx.defant = antenna;
  73. sc->rx.rxotherant = 0;
  74. }
  75. static void ath_opmode_init(struct ath_softc *sc)
  76. {
  77. struct ath_hw *ah = sc->sc_ah;
  78. struct ath_common *common = ath9k_hw_common(ah);
  79. u32 rfilt, mfilt[2];
  80. /* configure rx filter */
  81. rfilt = ath_calcrxfilter(sc);
  82. ath9k_hw_setrxfilter(ah, rfilt);
  83. /* configure bssid mask */
  84. ath_hw_setbssidmask(common);
  85. /* configure operational mode */
  86. ath9k_hw_setopmode(ah);
  87. /* calculate and install multicast filter */
  88. mfilt[0] = mfilt[1] = ~0;
  89. ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
  90. }
  91. static bool ath_rx_edma_buf_link(struct ath_softc *sc,
  92. enum ath9k_rx_qtype qtype)
  93. {
  94. struct ath_hw *ah = sc->sc_ah;
  95. struct ath_rx_edma *rx_edma;
  96. struct sk_buff *skb;
  97. struct ath_rxbuf *bf;
  98. rx_edma = &sc->rx.rx_edma[qtype];
  99. if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
  100. return false;
  101. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  102. list_del_init(&bf->list);
  103. skb = bf->bf_mpdu;
  104. memset(skb->data, 0, ah->caps.rx_status_len);
  105. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  106. ah->caps.rx_status_len, DMA_TO_DEVICE);
  107. SKB_CB_ATHBUF(skb) = bf;
  108. ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
  109. __skb_queue_tail(&rx_edma->rx_fifo, skb);
  110. return true;
  111. }
  112. static void ath_rx_addbuffer_edma(struct ath_softc *sc,
  113. enum ath9k_rx_qtype qtype)
  114. {
  115. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  116. struct ath_rxbuf *bf, *tbf;
  117. if (list_empty(&sc->rx.rxbuf)) {
  118. ath_dbg(common, QUEUE, "No free rx buf available\n");
  119. return;
  120. }
  121. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
  122. if (!ath_rx_edma_buf_link(sc, qtype))
  123. break;
  124. }
  125. static void ath_rx_remove_buffer(struct ath_softc *sc,
  126. enum ath9k_rx_qtype qtype)
  127. {
  128. struct ath_rxbuf *bf;
  129. struct ath_rx_edma *rx_edma;
  130. struct sk_buff *skb;
  131. rx_edma = &sc->rx.rx_edma[qtype];
  132. while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
  133. bf = SKB_CB_ATHBUF(skb);
  134. BUG_ON(!bf);
  135. list_add_tail(&bf->list, &sc->rx.rxbuf);
  136. }
  137. }
  138. static void ath_rx_edma_cleanup(struct ath_softc *sc)
  139. {
  140. struct ath_hw *ah = sc->sc_ah;
  141. struct ath_common *common = ath9k_hw_common(ah);
  142. struct ath_rxbuf *bf;
  143. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  144. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  145. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  146. if (bf->bf_mpdu) {
  147. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  148. common->rx_bufsize,
  149. DMA_BIDIRECTIONAL);
  150. dev_kfree_skb_any(bf->bf_mpdu);
  151. bf->bf_buf_addr = 0;
  152. bf->bf_mpdu = NULL;
  153. }
  154. }
  155. }
  156. static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
  157. {
  158. __skb_queue_head_init(&rx_edma->rx_fifo);
  159. rx_edma->rx_fifo_hwsize = size;
  160. }
  161. static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
  162. {
  163. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  164. struct ath_hw *ah = sc->sc_ah;
  165. struct sk_buff *skb;
  166. struct ath_rxbuf *bf;
  167. int error = 0, i;
  168. u32 size;
  169. ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
  170. ah->caps.rx_status_len);
  171. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
  172. ah->caps.rx_lp_qdepth);
  173. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
  174. ah->caps.rx_hp_qdepth);
  175. size = sizeof(struct ath_rxbuf) * nbufs;
  176. bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
  177. if (!bf)
  178. return -ENOMEM;
  179. INIT_LIST_HEAD(&sc->rx.rxbuf);
  180. for (i = 0; i < nbufs; i++, bf++) {
  181. skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
  182. if (!skb) {
  183. error = -ENOMEM;
  184. goto rx_init_fail;
  185. }
  186. memset(skb->data, 0, common->rx_bufsize);
  187. bf->bf_mpdu = skb;
  188. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  189. common->rx_bufsize,
  190. DMA_BIDIRECTIONAL);
  191. if (unlikely(dma_mapping_error(sc->dev,
  192. bf->bf_buf_addr))) {
  193. dev_kfree_skb_any(skb);
  194. bf->bf_mpdu = NULL;
  195. bf->bf_buf_addr = 0;
  196. ath_err(common,
  197. "dma_mapping_error() on RX init\n");
  198. error = -ENOMEM;
  199. goto rx_init_fail;
  200. }
  201. list_add_tail(&bf->list, &sc->rx.rxbuf);
  202. }
  203. return 0;
  204. rx_init_fail:
  205. ath_rx_edma_cleanup(sc);
  206. return error;
  207. }
  208. static void ath_edma_start_recv(struct ath_softc *sc)
  209. {
  210. ath9k_hw_rxena(sc->sc_ah);
  211. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP);
  212. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP);
  213. ath_opmode_init(sc);
  214. ath9k_hw_startpcureceive(sc->sc_ah, sc->cur_chan->offchannel);
  215. }
  216. static void ath_edma_stop_recv(struct ath_softc *sc)
  217. {
  218. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  219. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  220. }
  221. int ath_rx_init(struct ath_softc *sc, int nbufs)
  222. {
  223. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  224. struct sk_buff *skb;
  225. struct ath_rxbuf *bf;
  226. int error = 0;
  227. spin_lock_init(&sc->sc_pcu_lock);
  228. common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
  229. sc->sc_ah->caps.rx_status_len;
  230. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  231. return ath_rx_edma_init(sc, nbufs);
  232. ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
  233. common->cachelsz, common->rx_bufsize);
  234. /* Initialize rx descriptors */
  235. error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
  236. "rx", nbufs, 1, 0);
  237. if (error != 0) {
  238. ath_err(common,
  239. "failed to allocate rx descriptors: %d\n",
  240. error);
  241. goto err;
  242. }
  243. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  244. skb = ath_rxbuf_alloc(common, common->rx_bufsize,
  245. GFP_KERNEL);
  246. if (skb == NULL) {
  247. error = -ENOMEM;
  248. goto err;
  249. }
  250. bf->bf_mpdu = skb;
  251. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  252. common->rx_bufsize,
  253. DMA_FROM_DEVICE);
  254. if (unlikely(dma_mapping_error(sc->dev,
  255. bf->bf_buf_addr))) {
  256. dev_kfree_skb_any(skb);
  257. bf->bf_mpdu = NULL;
  258. bf->bf_buf_addr = 0;
  259. ath_err(common,
  260. "dma_mapping_error() on RX init\n");
  261. error = -ENOMEM;
  262. goto err;
  263. }
  264. }
  265. sc->rx.rxlink = NULL;
  266. err:
  267. if (error)
  268. ath_rx_cleanup(sc);
  269. return error;
  270. }
  271. void ath_rx_cleanup(struct ath_softc *sc)
  272. {
  273. struct ath_hw *ah = sc->sc_ah;
  274. struct ath_common *common = ath9k_hw_common(ah);
  275. struct sk_buff *skb;
  276. struct ath_rxbuf *bf;
  277. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  278. ath_rx_edma_cleanup(sc);
  279. return;
  280. }
  281. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  282. skb = bf->bf_mpdu;
  283. if (skb) {
  284. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  285. common->rx_bufsize,
  286. DMA_FROM_DEVICE);
  287. dev_kfree_skb(skb);
  288. bf->bf_buf_addr = 0;
  289. bf->bf_mpdu = NULL;
  290. }
  291. }
  292. }
  293. /*
  294. * Calculate the receive filter according to the
  295. * operating mode and state:
  296. *
  297. * o always accept unicast, broadcast, and multicast traffic
  298. * o maintain current state of phy error reception (the hal
  299. * may enable phy error frames for noise immunity work)
  300. * o probe request frames are accepted only when operating in
  301. * hostap, adhoc, or monitor modes
  302. * o enable promiscuous mode according to the interface state
  303. * o accept beacons:
  304. * - when operating in adhoc mode so the 802.11 layer creates
  305. * node table entries for peers,
  306. * - when operating in station mode for collecting rssi data when
  307. * the station is otherwise quiet, or
  308. * - when operating as a repeater so we see repeater-sta beacons
  309. * - when scanning
  310. */
  311. u32 ath_calcrxfilter(struct ath_softc *sc)
  312. {
  313. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  314. u32 rfilt;
  315. if (config_enabled(CONFIG_ATH9K_TX99))
  316. return 0;
  317. rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
  318. | ATH9K_RX_FILTER_MCAST;
  319. /* if operating on a DFS channel, enable radar pulse detection */
  320. if (sc->hw->conf.radar_enabled)
  321. rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
  322. spin_lock_bh(&sc->chan_lock);
  323. if (sc->cur_chan->rxfilter & FIF_PROBE_REQ)
  324. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  325. if (sc->sc_ah->is_monitoring)
  326. rfilt |= ATH9K_RX_FILTER_PROM;
  327. if ((sc->cur_chan->rxfilter & FIF_CONTROL) ||
  328. sc->sc_ah->dynack.enabled)
  329. rfilt |= ATH9K_RX_FILTER_CONTROL;
  330. if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
  331. (sc->cur_chan->nvifs <= 1) &&
  332. !(sc->cur_chan->rxfilter & FIF_BCN_PRBRESP_PROMISC))
  333. rfilt |= ATH9K_RX_FILTER_MYBEACON;
  334. else if (sc->sc_ah->opmode != NL80211_IFTYPE_OCB)
  335. rfilt |= ATH9K_RX_FILTER_BEACON;
  336. if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
  337. (sc->cur_chan->rxfilter & FIF_PSPOLL))
  338. rfilt |= ATH9K_RX_FILTER_PSPOLL;
  339. if (sc->cur_chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
  340. rfilt |= ATH9K_RX_FILTER_COMP_BAR;
  341. if (sc->cur_chan->nvifs > 1 || (sc->cur_chan->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. AR_SREV_9561(sc->sc_ah))
  349. rfilt |= ATH9K_RX_FILTER_4ADDRESS;
  350. if (ath9k_is_chanctx_enabled() &&
  351. test_bit(ATH_OP_SCANNING, &common->op_flags))
  352. rfilt |= ATH9K_RX_FILTER_BEACON;
  353. spin_unlock_bh(&sc->chan_lock);
  354. return rfilt;
  355. }
  356. void ath_startrecv(struct ath_softc *sc)
  357. {
  358. struct ath_hw *ah = sc->sc_ah;
  359. struct ath_rxbuf *bf, *tbf;
  360. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  361. ath_edma_start_recv(sc);
  362. return;
  363. }
  364. if (list_empty(&sc->rx.rxbuf))
  365. goto start_recv;
  366. sc->rx.buf_hold = NULL;
  367. sc->rx.rxlink = NULL;
  368. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
  369. ath_rx_buf_link(sc, bf, false);
  370. }
  371. /* We could have deleted elements so the list may be empty now */
  372. if (list_empty(&sc->rx.rxbuf))
  373. goto start_recv;
  374. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  375. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  376. ath9k_hw_rxena(ah);
  377. start_recv:
  378. ath_opmode_init(sc);
  379. ath9k_hw_startpcureceive(ah, sc->cur_chan->offchannel);
  380. }
  381. static void ath_flushrecv(struct ath_softc *sc)
  382. {
  383. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  384. ath_rx_tasklet(sc, 1, true);
  385. ath_rx_tasklet(sc, 1, false);
  386. }
  387. bool ath_stoprecv(struct ath_softc *sc)
  388. {
  389. struct ath_hw *ah = sc->sc_ah;
  390. bool stopped, reset = false;
  391. ath9k_hw_abortpcurecv(ah);
  392. ath9k_hw_setrxfilter(ah, 0);
  393. stopped = ath9k_hw_stopdmarecv(ah, &reset);
  394. ath_flushrecv(sc);
  395. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  396. ath_edma_stop_recv(sc);
  397. else
  398. sc->rx.rxlink = NULL;
  399. if (!(ah->ah_flags & AH_UNPLUGGED) &&
  400. unlikely(!stopped)) {
  401. ath_dbg(ath9k_hw_common(sc->sc_ah), RESET,
  402. "Failed to stop Rx DMA\n");
  403. RESET_STAT_INC(sc, RESET_RX_DMA_ERROR);
  404. }
  405. return stopped && !reset;
  406. }
  407. static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
  408. {
  409. /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
  410. struct ieee80211_mgmt *mgmt;
  411. u8 *pos, *end, id, elen;
  412. struct ieee80211_tim_ie *tim;
  413. mgmt = (struct ieee80211_mgmt *)skb->data;
  414. pos = mgmt->u.beacon.variable;
  415. end = skb->data + skb->len;
  416. while (pos + 2 < end) {
  417. id = *pos++;
  418. elen = *pos++;
  419. if (pos + elen > end)
  420. break;
  421. if (id == WLAN_EID_TIM) {
  422. if (elen < sizeof(*tim))
  423. break;
  424. tim = (struct ieee80211_tim_ie *) pos;
  425. if (tim->dtim_count != 0)
  426. break;
  427. return tim->bitmap_ctrl & 0x01;
  428. }
  429. pos += elen;
  430. }
  431. return false;
  432. }
  433. static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
  434. {
  435. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  436. bool skip_beacon = false;
  437. if (skb->len < 24 + 8 + 2 + 2)
  438. return;
  439. sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
  440. if (sc->ps_flags & PS_BEACON_SYNC) {
  441. sc->ps_flags &= ~PS_BEACON_SYNC;
  442. ath_dbg(common, PS,
  443. "Reconfigure beacon timers based on synchronized timestamp\n");
  444. #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
  445. if (ath9k_is_chanctx_enabled()) {
  446. if (sc->cur_chan == &sc->offchannel.chan)
  447. skip_beacon = true;
  448. }
  449. #endif
  450. if (!skip_beacon &&
  451. !(WARN_ON_ONCE(sc->cur_chan->beacon.beacon_interval == 0)))
  452. ath9k_set_beacon(sc);
  453. ath9k_p2p_beacon_sync(sc);
  454. }
  455. if (ath_beacon_dtim_pending_cab(skb)) {
  456. /*
  457. * Remain awake waiting for buffered broadcast/multicast
  458. * frames. If the last broadcast/multicast frame is not
  459. * received properly, the next beacon frame will work as
  460. * a backup trigger for returning into NETWORK SLEEP state,
  461. * so we are waiting for it as well.
  462. */
  463. ath_dbg(common, PS,
  464. "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
  465. sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
  466. return;
  467. }
  468. if (sc->ps_flags & PS_WAIT_FOR_CAB) {
  469. /*
  470. * This can happen if a broadcast frame is dropped or the AP
  471. * fails to send a frame indicating that all CAB frames have
  472. * been delivered.
  473. */
  474. sc->ps_flags &= ~PS_WAIT_FOR_CAB;
  475. ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
  476. }
  477. }
  478. static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
  479. {
  480. struct ieee80211_hdr *hdr;
  481. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  482. hdr = (struct ieee80211_hdr *)skb->data;
  483. /* Process Beacon and CAB receive in PS state */
  484. if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
  485. && mybeacon) {
  486. ath_rx_ps_beacon(sc, skb);
  487. } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
  488. (ieee80211_is_data(hdr->frame_control) ||
  489. ieee80211_is_action(hdr->frame_control)) &&
  490. is_multicast_ether_addr(hdr->addr1) &&
  491. !ieee80211_has_moredata(hdr->frame_control)) {
  492. /*
  493. * No more broadcast/multicast frames to be received at this
  494. * point.
  495. */
  496. sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
  497. ath_dbg(common, PS,
  498. "All PS CAB frames received, back to sleep\n");
  499. } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
  500. !is_multicast_ether_addr(hdr->addr1) &&
  501. !ieee80211_has_morefrags(hdr->frame_control)) {
  502. sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
  503. ath_dbg(common, PS,
  504. "Going back to sleep after having received PS-Poll data (0x%lx)\n",
  505. sc->ps_flags & (PS_WAIT_FOR_BEACON |
  506. PS_WAIT_FOR_CAB |
  507. PS_WAIT_FOR_PSPOLL_DATA |
  508. PS_WAIT_FOR_TX_ACK));
  509. }
  510. }
  511. static bool ath_edma_get_buffers(struct ath_softc *sc,
  512. enum ath9k_rx_qtype qtype,
  513. struct ath_rx_status *rs,
  514. struct ath_rxbuf **dest)
  515. {
  516. struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
  517. struct ath_hw *ah = sc->sc_ah;
  518. struct ath_common *common = ath9k_hw_common(ah);
  519. struct sk_buff *skb;
  520. struct ath_rxbuf *bf;
  521. int ret;
  522. skb = skb_peek(&rx_edma->rx_fifo);
  523. if (!skb)
  524. return false;
  525. bf = SKB_CB_ATHBUF(skb);
  526. BUG_ON(!bf);
  527. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  528. common->rx_bufsize, DMA_FROM_DEVICE);
  529. ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
  530. if (ret == -EINPROGRESS) {
  531. /*let device gain the buffer again*/
  532. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  533. common->rx_bufsize, DMA_FROM_DEVICE);
  534. return false;
  535. }
  536. __skb_unlink(skb, &rx_edma->rx_fifo);
  537. if (ret == -EINVAL) {
  538. /* corrupt descriptor, skip this one and the following one */
  539. list_add_tail(&bf->list, &sc->rx.rxbuf);
  540. ath_rx_edma_buf_link(sc, qtype);
  541. skb = skb_peek(&rx_edma->rx_fifo);
  542. if (skb) {
  543. bf = SKB_CB_ATHBUF(skb);
  544. BUG_ON(!bf);
  545. __skb_unlink(skb, &rx_edma->rx_fifo);
  546. list_add_tail(&bf->list, &sc->rx.rxbuf);
  547. ath_rx_edma_buf_link(sc, qtype);
  548. }
  549. bf = NULL;
  550. }
  551. *dest = bf;
  552. return true;
  553. }
  554. static struct ath_rxbuf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
  555. struct ath_rx_status *rs,
  556. enum ath9k_rx_qtype qtype)
  557. {
  558. struct ath_rxbuf *bf = NULL;
  559. while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
  560. if (!bf)
  561. continue;
  562. return bf;
  563. }
  564. return NULL;
  565. }
  566. static struct ath_rxbuf *ath_get_next_rx_buf(struct ath_softc *sc,
  567. struct ath_rx_status *rs)
  568. {
  569. struct ath_hw *ah = sc->sc_ah;
  570. struct ath_common *common = ath9k_hw_common(ah);
  571. struct ath_desc *ds;
  572. struct ath_rxbuf *bf;
  573. int ret;
  574. if (list_empty(&sc->rx.rxbuf)) {
  575. sc->rx.rxlink = NULL;
  576. return NULL;
  577. }
  578. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  579. if (bf == sc->rx.buf_hold)
  580. return NULL;
  581. ds = bf->bf_desc;
  582. /*
  583. * Must provide the virtual address of the current
  584. * descriptor, the physical address, and the virtual
  585. * address of the next descriptor in the h/w chain.
  586. * This allows the HAL to look ahead to see if the
  587. * hardware is done with a descriptor by checking the
  588. * done bit in the following descriptor and the address
  589. * of the current descriptor the DMA engine is working
  590. * on. All this is necessary because of our use of
  591. * a self-linked list to avoid rx overruns.
  592. */
  593. ret = ath9k_hw_rxprocdesc(ah, ds, rs);
  594. if (ret == -EINPROGRESS) {
  595. struct ath_rx_status trs;
  596. struct ath_rxbuf *tbf;
  597. struct ath_desc *tds;
  598. memset(&trs, 0, sizeof(trs));
  599. if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
  600. sc->rx.rxlink = NULL;
  601. return NULL;
  602. }
  603. tbf = list_entry(bf->list.next, struct ath_rxbuf, list);
  604. /*
  605. * On some hardware the descriptor status words could
  606. * get corrupted, including the done bit. Because of
  607. * this, check if the next descriptor's done bit is
  608. * set or not.
  609. *
  610. * If the next descriptor's done bit is set, the current
  611. * descriptor has been corrupted. Force s/w to discard
  612. * this descriptor and continue...
  613. */
  614. tds = tbf->bf_desc;
  615. ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
  616. if (ret == -EINPROGRESS)
  617. return NULL;
  618. /*
  619. * Re-check previous descriptor, in case it has been filled
  620. * in the mean time.
  621. */
  622. ret = ath9k_hw_rxprocdesc(ah, ds, rs);
  623. if (ret == -EINPROGRESS) {
  624. /*
  625. * mark descriptor as zero-length and set the 'more'
  626. * flag to ensure that both buffers get discarded
  627. */
  628. rs->rs_datalen = 0;
  629. rs->rs_more = true;
  630. }
  631. }
  632. list_del(&bf->list);
  633. if (!bf->bf_mpdu)
  634. return bf;
  635. /*
  636. * Synchronize the DMA transfer with CPU before
  637. * 1. accessing the frame
  638. * 2. requeueing the same buffer to h/w
  639. */
  640. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  641. common->rx_bufsize,
  642. DMA_FROM_DEVICE);
  643. return bf;
  644. }
  645. static void ath9k_process_tsf(struct ath_rx_status *rs,
  646. struct ieee80211_rx_status *rxs,
  647. u64 tsf)
  648. {
  649. u32 tsf_lower = tsf & 0xffffffff;
  650. rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp;
  651. if (rs->rs_tstamp > tsf_lower &&
  652. unlikely(rs->rs_tstamp - tsf_lower > 0x10000000))
  653. rxs->mactime -= 0x100000000ULL;
  654. if (rs->rs_tstamp < tsf_lower &&
  655. unlikely(tsf_lower - rs->rs_tstamp > 0x10000000))
  656. rxs->mactime += 0x100000000ULL;
  657. }
  658. /*
  659. * For Decrypt or Demic errors, we only mark packet status here and always push
  660. * up the frame up to let mac80211 handle the actual error case, be it no
  661. * decryption key or real decryption error. This let us keep statistics there.
  662. */
  663. static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
  664. struct sk_buff *skb,
  665. struct ath_rx_status *rx_stats,
  666. struct ieee80211_rx_status *rx_status,
  667. bool *decrypt_error, u64 tsf)
  668. {
  669. struct ieee80211_hw *hw = sc->hw;
  670. struct ath_hw *ah = sc->sc_ah;
  671. struct ath_common *common = ath9k_hw_common(ah);
  672. struct ieee80211_hdr *hdr;
  673. bool discard_current = sc->rx.discard_next;
  674. /*
  675. * Discard corrupt descriptors which are marked in
  676. * ath_get_next_rx_buf().
  677. */
  678. if (discard_current)
  679. goto corrupt;
  680. sc->rx.discard_next = false;
  681. /*
  682. * Discard zero-length packets.
  683. */
  684. if (!rx_stats->rs_datalen) {
  685. RX_STAT_INC(rx_len_err);
  686. goto corrupt;
  687. }
  688. /*
  689. * rs_status follows rs_datalen so if rs_datalen is too large
  690. * we can take a hint that hardware corrupted it, so ignore
  691. * those frames.
  692. */
  693. if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) {
  694. RX_STAT_INC(rx_len_err);
  695. goto corrupt;
  696. }
  697. /* Only use status info from the last fragment */
  698. if (rx_stats->rs_more)
  699. return 0;
  700. /*
  701. * Return immediately if the RX descriptor has been marked
  702. * as corrupt based on the various error bits.
  703. *
  704. * This is different from the other corrupt descriptor
  705. * condition handled above.
  706. */
  707. if (rx_stats->rs_status & ATH9K_RXERR_CORRUPT_DESC)
  708. goto corrupt;
  709. hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len);
  710. ath9k_process_tsf(rx_stats, rx_status, tsf);
  711. ath_debug_stat_rx(sc, rx_stats);
  712. /*
  713. * Process PHY errors and return so that the packet
  714. * can be dropped.
  715. */
  716. if (rx_stats->rs_status & ATH9K_RXERR_PHY) {
  717. ath9k_dfs_process_phyerr(sc, hdr, rx_stats, rx_status->mactime);
  718. if (ath_cmn_process_fft(&sc->spec_priv, hdr, rx_stats, rx_status->mactime))
  719. RX_STAT_INC(rx_spectral);
  720. return -EINVAL;
  721. }
  722. /*
  723. * everything but the rate is checked here, the rate check is done
  724. * separately to avoid doing two lookups for a rate for each frame.
  725. */
  726. spin_lock_bh(&sc->chan_lock);
  727. if (!ath9k_cmn_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error,
  728. sc->cur_chan->rxfilter)) {
  729. spin_unlock_bh(&sc->chan_lock);
  730. return -EINVAL;
  731. }
  732. spin_unlock_bh(&sc->chan_lock);
  733. if (ath_is_mybeacon(common, hdr)) {
  734. RX_STAT_INC(rx_beacons);
  735. rx_stats->is_mybeacon = true;
  736. }
  737. /*
  738. * This shouldn't happen, but have a safety check anyway.
  739. */
  740. if (WARN_ON(!ah->curchan))
  741. return -EINVAL;
  742. if (ath9k_cmn_process_rate(common, hw, rx_stats, rx_status)) {
  743. /*
  744. * No valid hardware bitrate found -- we should not get here
  745. * because hardware has already validated this frame as OK.
  746. */
  747. ath_dbg(common, ANY, "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
  748. rx_stats->rs_rate);
  749. RX_STAT_INC(rx_rate_err);
  750. return -EINVAL;
  751. }
  752. if (ath9k_is_chanctx_enabled()) {
  753. if (rx_stats->is_mybeacon)
  754. ath_chanctx_beacon_recv_ev(sc,
  755. ATH_CHANCTX_EVENT_BEACON_RECEIVED);
  756. }
  757. ath9k_cmn_process_rssi(common, hw, rx_stats, rx_status);
  758. rx_status->band = ah->curchan->chan->band;
  759. rx_status->freq = ah->curchan->chan->center_freq;
  760. rx_status->antenna = rx_stats->rs_antenna;
  761. rx_status->flag |= RX_FLAG_MACTIME_END;
  762. #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
  763. if (ieee80211_is_data_present(hdr->frame_control) &&
  764. !ieee80211_is_qos_nullfunc(hdr->frame_control))
  765. sc->rx.num_pkts++;
  766. #endif
  767. return 0;
  768. corrupt:
  769. sc->rx.discard_next = rx_stats->rs_more;
  770. return -EINVAL;
  771. }
  772. /*
  773. * Run the LNA combining algorithm only in these cases:
  774. *
  775. * Standalone WLAN cards with both LNA/Antenna diversity
  776. * enabled in the EEPROM.
  777. *
  778. * WLAN+BT cards which are in the supported card list
  779. * in ath_pci_id_table and the user has loaded the
  780. * driver with "bt_ant_diversity" set to true.
  781. */
  782. static void ath9k_antenna_check(struct ath_softc *sc,
  783. struct ath_rx_status *rs)
  784. {
  785. struct ath_hw *ah = sc->sc_ah;
  786. struct ath9k_hw_capabilities *pCap = &ah->caps;
  787. struct ath_common *common = ath9k_hw_common(ah);
  788. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB))
  789. return;
  790. /*
  791. * Change the default rx antenna if rx diversity
  792. * chooses the other antenna 3 times in a row.
  793. */
  794. if (sc->rx.defant != rs->rs_antenna) {
  795. if (++sc->rx.rxotherant >= 3)
  796. ath_setdefantenna(sc, rs->rs_antenna);
  797. } else {
  798. sc->rx.rxotherant = 0;
  799. }
  800. if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) {
  801. if (common->bt_ant_diversity)
  802. ath_ant_comb_scan(sc, rs);
  803. } else {
  804. ath_ant_comb_scan(sc, rs);
  805. }
  806. }
  807. static void ath9k_apply_ampdu_details(struct ath_softc *sc,
  808. struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
  809. {
  810. if (rs->rs_isaggr) {
  811. rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
  812. rxs->ampdu_reference = sc->rx.ampdu_ref;
  813. if (!rs->rs_moreaggr) {
  814. rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
  815. sc->rx.ampdu_ref++;
  816. }
  817. if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
  818. rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
  819. }
  820. }
  821. int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
  822. {
  823. struct ath_rxbuf *bf;
  824. struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
  825. struct ieee80211_rx_status *rxs;
  826. struct ath_hw *ah = sc->sc_ah;
  827. struct ath_common *common = ath9k_hw_common(ah);
  828. struct ieee80211_hw *hw = sc->hw;
  829. int retval;
  830. struct ath_rx_status rs;
  831. enum ath9k_rx_qtype qtype;
  832. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  833. int dma_type;
  834. u64 tsf = 0;
  835. unsigned long flags;
  836. dma_addr_t new_buf_addr;
  837. unsigned int budget = 512;
  838. struct ieee80211_hdr *hdr;
  839. if (edma)
  840. dma_type = DMA_BIDIRECTIONAL;
  841. else
  842. dma_type = DMA_FROM_DEVICE;
  843. qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
  844. tsf = ath9k_hw_gettsf64(ah);
  845. do {
  846. bool decrypt_error = false;
  847. memset(&rs, 0, sizeof(rs));
  848. if (edma)
  849. bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
  850. else
  851. bf = ath_get_next_rx_buf(sc, &rs);
  852. if (!bf)
  853. break;
  854. skb = bf->bf_mpdu;
  855. if (!skb)
  856. continue;
  857. /*
  858. * Take frame header from the first fragment and RX status from
  859. * the last one.
  860. */
  861. if (sc->rx.frag)
  862. hdr_skb = sc->rx.frag;
  863. else
  864. hdr_skb = skb;
  865. rxs = IEEE80211_SKB_RXCB(hdr_skb);
  866. memset(rxs, 0, sizeof(struct ieee80211_rx_status));
  867. retval = ath9k_rx_skb_preprocess(sc, hdr_skb, &rs, rxs,
  868. &decrypt_error, tsf);
  869. if (retval)
  870. goto requeue_drop_frag;
  871. /* Ensure we always have an skb to requeue once we are done
  872. * processing the current buffer's skb */
  873. requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
  874. /* If there is no memory we ignore the current RX'd frame,
  875. * tell hardware it can give us a new frame using the old
  876. * skb and put it at the tail of the sc->rx.rxbuf list for
  877. * processing. */
  878. if (!requeue_skb) {
  879. RX_STAT_INC(rx_oom_err);
  880. goto requeue_drop_frag;
  881. }
  882. /* We will now give hardware our shiny new allocated skb */
  883. new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
  884. common->rx_bufsize, dma_type);
  885. if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
  886. dev_kfree_skb_any(requeue_skb);
  887. goto requeue_drop_frag;
  888. }
  889. /* Unmap the frame */
  890. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  891. common->rx_bufsize, dma_type);
  892. bf->bf_mpdu = requeue_skb;
  893. bf->bf_buf_addr = new_buf_addr;
  894. skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
  895. if (ah->caps.rx_status_len)
  896. skb_pull(skb, ah->caps.rx_status_len);
  897. if (!rs.rs_more)
  898. ath9k_cmn_rx_skb_postprocess(common, hdr_skb, &rs,
  899. rxs, decrypt_error);
  900. if (rs.rs_more) {
  901. RX_STAT_INC(rx_frags);
  902. /*
  903. * rs_more indicates chained descriptors which can be
  904. * used to link buffers together for a sort of
  905. * scatter-gather operation.
  906. */
  907. if (sc->rx.frag) {
  908. /* too many fragments - cannot handle frame */
  909. dev_kfree_skb_any(sc->rx.frag);
  910. dev_kfree_skb_any(skb);
  911. RX_STAT_INC(rx_too_many_frags_err);
  912. skb = NULL;
  913. }
  914. sc->rx.frag = skb;
  915. goto requeue;
  916. }
  917. if (sc->rx.frag) {
  918. int space = skb->len - skb_tailroom(hdr_skb);
  919. if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
  920. dev_kfree_skb(skb);
  921. RX_STAT_INC(rx_oom_err);
  922. goto requeue_drop_frag;
  923. }
  924. sc->rx.frag = NULL;
  925. skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
  926. skb->len);
  927. dev_kfree_skb_any(skb);
  928. skb = hdr_skb;
  929. }
  930. if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
  931. skb_trim(skb, skb->len - 8);
  932. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  933. if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
  934. PS_WAIT_FOR_CAB |
  935. PS_WAIT_FOR_PSPOLL_DATA)) ||
  936. ath9k_check_auto_sleep(sc))
  937. ath_rx_ps(sc, skb, rs.is_mybeacon);
  938. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  939. ath9k_antenna_check(sc, &rs);
  940. ath9k_apply_ampdu_details(sc, &rs, rxs);
  941. ath_debug_rate_stats(sc, &rs, skb);
  942. hdr = (struct ieee80211_hdr *)skb->data;
  943. if (ieee80211_is_ack(hdr->frame_control))
  944. ath_dynack_sample_ack_ts(sc->sc_ah, skb, rs.rs_tstamp);
  945. ieee80211_rx(hw, skb);
  946. requeue_drop_frag:
  947. if (sc->rx.frag) {
  948. dev_kfree_skb_any(sc->rx.frag);
  949. sc->rx.frag = NULL;
  950. }
  951. requeue:
  952. list_add_tail(&bf->list, &sc->rx.rxbuf);
  953. if (!edma) {
  954. ath_rx_buf_relink(sc, bf, flush);
  955. if (!flush)
  956. ath9k_hw_rxena(ah);
  957. } else if (!flush) {
  958. ath_rx_edma_buf_link(sc, qtype);
  959. }
  960. if (!budget--)
  961. break;
  962. } while (1);
  963. if (!(ah->imask & ATH9K_INT_RXEOL)) {
  964. ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  965. ath9k_hw_set_interrupts(ah);
  966. }
  967. return 0;
  968. }