recv.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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. if (sc->rx.rxfilter & FIF_PROBE_REQ)
  323. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  324. /*
  325. * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
  326. * mode interface or when in monitor mode. AP mode does not need this
  327. * since it receives all in-BSS frames anyway.
  328. */
  329. if (sc->sc_ah->is_monitoring)
  330. rfilt |= ATH9K_RX_FILTER_PROM;
  331. if (sc->rx.rxfilter & FIF_CONTROL)
  332. rfilt |= ATH9K_RX_FILTER_CONTROL;
  333. if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
  334. (sc->nvifs <= 1) &&
  335. !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
  336. rfilt |= ATH9K_RX_FILTER_MYBEACON;
  337. else
  338. rfilt |= ATH9K_RX_FILTER_BEACON;
  339. if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
  340. (sc->rx.rxfilter & FIF_PSPOLL))
  341. rfilt |= ATH9K_RX_FILTER_PSPOLL;
  342. if (conf_is_ht(&sc->hw->conf))
  343. rfilt |= ATH9K_RX_FILTER_COMP_BAR;
  344. if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
  345. /* This is needed for older chips */
  346. if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
  347. rfilt |= ATH9K_RX_FILTER_PROM;
  348. rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
  349. }
  350. if (AR_SREV_9550(sc->sc_ah) || AR_SREV_9531(sc->sc_ah))
  351. rfilt |= ATH9K_RX_FILTER_4ADDRESS;
  352. if (ath9k_use_chanctx &&
  353. test_bit(ATH_OP_SCANNING, &common->op_flags))
  354. rfilt |= ATH9K_RX_FILTER_BEACON;
  355. return rfilt;
  356. }
  357. int ath_startrecv(struct ath_softc *sc)
  358. {
  359. struct ath_hw *ah = sc->sc_ah;
  360. struct ath_rxbuf *bf, *tbf;
  361. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  362. ath_edma_start_recv(sc);
  363. return 0;
  364. }
  365. if (list_empty(&sc->rx.rxbuf))
  366. goto start_recv;
  367. sc->rx.buf_hold = NULL;
  368. sc->rx.rxlink = NULL;
  369. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
  370. ath_rx_buf_link(sc, bf, false);
  371. }
  372. /* We could have deleted elements so the list may be empty now */
  373. if (list_empty(&sc->rx.rxbuf))
  374. goto start_recv;
  375. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  376. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  377. ath9k_hw_rxena(ah);
  378. start_recv:
  379. ath_opmode_init(sc);
  380. ath9k_hw_startpcureceive(ah, sc->cur_chan->offchannel);
  381. return 0;
  382. }
  383. static void ath_flushrecv(struct ath_softc *sc)
  384. {
  385. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  386. ath_rx_tasklet(sc, 1, true);
  387. ath_rx_tasklet(sc, 1, false);
  388. }
  389. bool ath_stoprecv(struct ath_softc *sc)
  390. {
  391. struct ath_hw *ah = sc->sc_ah;
  392. bool stopped, reset = false;
  393. ath9k_hw_abortpcurecv(ah);
  394. ath9k_hw_setrxfilter(ah, 0);
  395. stopped = ath9k_hw_stopdmarecv(ah, &reset);
  396. ath_flushrecv(sc);
  397. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  398. ath_edma_stop_recv(sc);
  399. else
  400. sc->rx.rxlink = NULL;
  401. if (!(ah->ah_flags & AH_UNPLUGGED) &&
  402. unlikely(!stopped)) {
  403. ath_err(ath9k_hw_common(sc->sc_ah),
  404. "Could not stop RX, we could be "
  405. "confusing the DMA engine when we start RX up\n");
  406. ATH_DBG_WARN_ON_ONCE(!stopped);
  407. }
  408. return stopped && !reset;
  409. }
  410. static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
  411. {
  412. /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
  413. struct ieee80211_mgmt *mgmt;
  414. u8 *pos, *end, id, elen;
  415. struct ieee80211_tim_ie *tim;
  416. mgmt = (struct ieee80211_mgmt *)skb->data;
  417. pos = mgmt->u.beacon.variable;
  418. end = skb->data + skb->len;
  419. while (pos + 2 < end) {
  420. id = *pos++;
  421. elen = *pos++;
  422. if (pos + elen > end)
  423. break;
  424. if (id == WLAN_EID_TIM) {
  425. if (elen < sizeof(*tim))
  426. break;
  427. tim = (struct ieee80211_tim_ie *) pos;
  428. if (tim->dtim_count != 0)
  429. break;
  430. return tim->bitmap_ctrl & 0x01;
  431. }
  432. pos += elen;
  433. }
  434. return false;
  435. }
  436. static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
  437. {
  438. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  439. if (skb->len < 24 + 8 + 2 + 2)
  440. return;
  441. sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
  442. if (sc->ps_flags & PS_BEACON_SYNC) {
  443. sc->ps_flags &= ~PS_BEACON_SYNC;
  444. ath_dbg(common, PS,
  445. "Reconfigure beacon timers based on synchronized timestamp\n");
  446. if (!(WARN_ON_ONCE(sc->cur_beacon_conf.beacon_interval == 0)))
  447. ath9k_set_beacon(sc);
  448. if (sc->p2p_ps_vif)
  449. ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
  450. }
  451. if (ath_beacon_dtim_pending_cab(skb)) {
  452. /*
  453. * Remain awake waiting for buffered broadcast/multicast
  454. * frames. If the last broadcast/multicast frame is not
  455. * received properly, the next beacon frame will work as
  456. * a backup trigger for returning into NETWORK SLEEP state,
  457. * so we are waiting for it as well.
  458. */
  459. ath_dbg(common, PS,
  460. "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
  461. sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
  462. return;
  463. }
  464. if (sc->ps_flags & PS_WAIT_FOR_CAB) {
  465. /*
  466. * This can happen if a broadcast frame is dropped or the AP
  467. * fails to send a frame indicating that all CAB frames have
  468. * been delivered.
  469. */
  470. sc->ps_flags &= ~PS_WAIT_FOR_CAB;
  471. ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
  472. }
  473. }
  474. static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
  475. {
  476. struct ieee80211_hdr *hdr;
  477. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  478. hdr = (struct ieee80211_hdr *)skb->data;
  479. /* Process Beacon and CAB receive in PS state */
  480. if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
  481. && mybeacon) {
  482. ath_rx_ps_beacon(sc, skb);
  483. } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
  484. (ieee80211_is_data(hdr->frame_control) ||
  485. ieee80211_is_action(hdr->frame_control)) &&
  486. is_multicast_ether_addr(hdr->addr1) &&
  487. !ieee80211_has_moredata(hdr->frame_control)) {
  488. /*
  489. * No more broadcast/multicast frames to be received at this
  490. * point.
  491. */
  492. sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
  493. ath_dbg(common, PS,
  494. "All PS CAB frames received, back to sleep\n");
  495. } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
  496. !is_multicast_ether_addr(hdr->addr1) &&
  497. !ieee80211_has_morefrags(hdr->frame_control)) {
  498. sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
  499. ath_dbg(common, PS,
  500. "Going back to sleep after having received PS-Poll data (0x%lx)\n",
  501. sc->ps_flags & (PS_WAIT_FOR_BEACON |
  502. PS_WAIT_FOR_CAB |
  503. PS_WAIT_FOR_PSPOLL_DATA |
  504. PS_WAIT_FOR_TX_ACK));
  505. }
  506. }
  507. static bool ath_edma_get_buffers(struct ath_softc *sc,
  508. enum ath9k_rx_qtype qtype,
  509. struct ath_rx_status *rs,
  510. struct ath_rxbuf **dest)
  511. {
  512. struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
  513. struct ath_hw *ah = sc->sc_ah;
  514. struct ath_common *common = ath9k_hw_common(ah);
  515. struct sk_buff *skb;
  516. struct ath_rxbuf *bf;
  517. int ret;
  518. skb = skb_peek(&rx_edma->rx_fifo);
  519. if (!skb)
  520. return false;
  521. bf = SKB_CB_ATHBUF(skb);
  522. BUG_ON(!bf);
  523. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  524. common->rx_bufsize, DMA_FROM_DEVICE);
  525. ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
  526. if (ret == -EINPROGRESS) {
  527. /*let device gain the buffer again*/
  528. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  529. common->rx_bufsize, DMA_FROM_DEVICE);
  530. return false;
  531. }
  532. __skb_unlink(skb, &rx_edma->rx_fifo);
  533. if (ret == -EINVAL) {
  534. /* corrupt descriptor, skip this one and the following one */
  535. list_add_tail(&bf->list, &sc->rx.rxbuf);
  536. ath_rx_edma_buf_link(sc, qtype);
  537. skb = skb_peek(&rx_edma->rx_fifo);
  538. if (skb) {
  539. bf = SKB_CB_ATHBUF(skb);
  540. BUG_ON(!bf);
  541. __skb_unlink(skb, &rx_edma->rx_fifo);
  542. list_add_tail(&bf->list, &sc->rx.rxbuf);
  543. ath_rx_edma_buf_link(sc, qtype);
  544. }
  545. bf = NULL;
  546. }
  547. *dest = bf;
  548. return true;
  549. }
  550. static struct ath_rxbuf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
  551. struct ath_rx_status *rs,
  552. enum ath9k_rx_qtype qtype)
  553. {
  554. struct ath_rxbuf *bf = NULL;
  555. while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
  556. if (!bf)
  557. continue;
  558. return bf;
  559. }
  560. return NULL;
  561. }
  562. static struct ath_rxbuf *ath_get_next_rx_buf(struct ath_softc *sc,
  563. struct ath_rx_status *rs)
  564. {
  565. struct ath_hw *ah = sc->sc_ah;
  566. struct ath_common *common = ath9k_hw_common(ah);
  567. struct ath_desc *ds;
  568. struct ath_rxbuf *bf;
  569. int ret;
  570. if (list_empty(&sc->rx.rxbuf)) {
  571. sc->rx.rxlink = NULL;
  572. return NULL;
  573. }
  574. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  575. if (bf == sc->rx.buf_hold)
  576. return NULL;
  577. ds = bf->bf_desc;
  578. /*
  579. * Must provide the virtual address of the current
  580. * descriptor, the physical address, and the virtual
  581. * address of the next descriptor in the h/w chain.
  582. * This allows the HAL to look ahead to see if the
  583. * hardware is done with a descriptor by checking the
  584. * done bit in the following descriptor and the address
  585. * of the current descriptor the DMA engine is working
  586. * on. All this is necessary because of our use of
  587. * a self-linked list to avoid rx overruns.
  588. */
  589. ret = ath9k_hw_rxprocdesc(ah, ds, rs);
  590. if (ret == -EINPROGRESS) {
  591. struct ath_rx_status trs;
  592. struct ath_rxbuf *tbf;
  593. struct ath_desc *tds;
  594. memset(&trs, 0, sizeof(trs));
  595. if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
  596. sc->rx.rxlink = NULL;
  597. return NULL;
  598. }
  599. tbf = list_entry(bf->list.next, struct ath_rxbuf, list);
  600. /*
  601. * On some hardware the descriptor status words could
  602. * get corrupted, including the done bit. Because of
  603. * this, check if the next descriptor's done bit is
  604. * set or not.
  605. *
  606. * If the next descriptor's done bit is set, the current
  607. * descriptor has been corrupted. Force s/w to discard
  608. * this descriptor and continue...
  609. */
  610. tds = tbf->bf_desc;
  611. ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
  612. if (ret == -EINPROGRESS)
  613. return NULL;
  614. /*
  615. * Re-check previous descriptor, in case it has been filled
  616. * in the mean time.
  617. */
  618. ret = ath9k_hw_rxprocdesc(ah, ds, rs);
  619. if (ret == -EINPROGRESS) {
  620. /*
  621. * mark descriptor as zero-length and set the 'more'
  622. * flag to ensure that both buffers get discarded
  623. */
  624. rs->rs_datalen = 0;
  625. rs->rs_more = true;
  626. }
  627. }
  628. list_del(&bf->list);
  629. if (!bf->bf_mpdu)
  630. return bf;
  631. /*
  632. * Synchronize the DMA transfer with CPU before
  633. * 1. accessing the frame
  634. * 2. requeueing the same buffer to h/w
  635. */
  636. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  637. common->rx_bufsize,
  638. DMA_FROM_DEVICE);
  639. return bf;
  640. }
  641. static void ath9k_process_tsf(struct ath_rx_status *rs,
  642. struct ieee80211_rx_status *rxs,
  643. u64 tsf)
  644. {
  645. u32 tsf_lower = tsf & 0xffffffff;
  646. rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp;
  647. if (rs->rs_tstamp > tsf_lower &&
  648. unlikely(rs->rs_tstamp - tsf_lower > 0x10000000))
  649. rxs->mactime -= 0x100000000ULL;
  650. if (rs->rs_tstamp < tsf_lower &&
  651. unlikely(tsf_lower - rs->rs_tstamp > 0x10000000))
  652. rxs->mactime += 0x100000000ULL;
  653. }
  654. /*
  655. * For Decrypt or Demic errors, we only mark packet status here and always push
  656. * up the frame up to let mac80211 handle the actual error case, be it no
  657. * decryption key or real decryption error. This let us keep statistics there.
  658. */
  659. static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
  660. struct sk_buff *skb,
  661. struct ath_rx_status *rx_stats,
  662. struct ieee80211_rx_status *rx_status,
  663. bool *decrypt_error, u64 tsf)
  664. {
  665. struct ieee80211_hw *hw = sc->hw;
  666. struct ath_hw *ah = sc->sc_ah;
  667. struct ath_common *common = ath9k_hw_common(ah);
  668. struct ieee80211_hdr *hdr;
  669. bool discard_current = sc->rx.discard_next;
  670. /*
  671. * Discard corrupt descriptors which are marked in
  672. * ath_get_next_rx_buf().
  673. */
  674. if (discard_current)
  675. goto corrupt;
  676. sc->rx.discard_next = false;
  677. /*
  678. * Discard zero-length packets.
  679. */
  680. if (!rx_stats->rs_datalen) {
  681. RX_STAT_INC(rx_len_err);
  682. goto corrupt;
  683. }
  684. /*
  685. * rs_status follows rs_datalen so if rs_datalen is too large
  686. * we can take a hint that hardware corrupted it, so ignore
  687. * those frames.
  688. */
  689. if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) {
  690. RX_STAT_INC(rx_len_err);
  691. goto corrupt;
  692. }
  693. /* Only use status info from the last fragment */
  694. if (rx_stats->rs_more)
  695. return 0;
  696. /*
  697. * Return immediately if the RX descriptor has been marked
  698. * as corrupt based on the various error bits.
  699. *
  700. * This is different from the other corrupt descriptor
  701. * condition handled above.
  702. */
  703. if (rx_stats->rs_status & ATH9K_RXERR_CORRUPT_DESC)
  704. goto corrupt;
  705. hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len);
  706. ath9k_process_tsf(rx_stats, rx_status, tsf);
  707. ath_debug_stat_rx(sc, rx_stats);
  708. /*
  709. * Process PHY errors and return so that the packet
  710. * can be dropped.
  711. */
  712. if (rx_stats->rs_status & ATH9K_RXERR_PHY) {
  713. ath9k_dfs_process_phyerr(sc, hdr, rx_stats, rx_status->mactime);
  714. if (ath_process_fft(sc, hdr, rx_stats, rx_status->mactime))
  715. RX_STAT_INC(rx_spectral);
  716. return -EINVAL;
  717. }
  718. /*
  719. * everything but the rate is checked here, the rate check is done
  720. * separately to avoid doing two lookups for a rate for each frame.
  721. */
  722. if (!ath9k_cmn_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error, sc->rx.rxfilter))
  723. return -EINVAL;
  724. if (ath_is_mybeacon(common, hdr)) {
  725. RX_STAT_INC(rx_beacons);
  726. rx_stats->is_mybeacon = true;
  727. }
  728. /*
  729. * This shouldn't happen, but have a safety check anyway.
  730. */
  731. if (WARN_ON(!ah->curchan))
  732. return -EINVAL;
  733. if (ath9k_cmn_process_rate(common, hw, rx_stats, rx_status)) {
  734. /*
  735. * No valid hardware bitrate found -- we should not get here
  736. * because hardware has already validated this frame as OK.
  737. */
  738. ath_dbg(common, ANY, "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
  739. rx_stats->rs_rate);
  740. RX_STAT_INC(rx_rate_err);
  741. return -EINVAL;
  742. }
  743. ath9k_cmn_process_rssi(common, hw, rx_stats, rx_status);
  744. rx_status->band = ah->curchan->chan->band;
  745. rx_status->freq = ah->curchan->chan->center_freq;
  746. rx_status->antenna = rx_stats->rs_antenna;
  747. rx_status->flag |= RX_FLAG_MACTIME_END;
  748. #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
  749. if (ieee80211_is_data_present(hdr->frame_control) &&
  750. !ieee80211_is_qos_nullfunc(hdr->frame_control))
  751. sc->rx.num_pkts++;
  752. #endif
  753. return 0;
  754. corrupt:
  755. sc->rx.discard_next = rx_stats->rs_more;
  756. return -EINVAL;
  757. }
  758. /*
  759. * Run the LNA combining algorithm only in these cases:
  760. *
  761. * Standalone WLAN cards with both LNA/Antenna diversity
  762. * enabled in the EEPROM.
  763. *
  764. * WLAN+BT cards which are in the supported card list
  765. * in ath_pci_id_table and the user has loaded the
  766. * driver with "bt_ant_diversity" set to true.
  767. */
  768. static void ath9k_antenna_check(struct ath_softc *sc,
  769. struct ath_rx_status *rs)
  770. {
  771. struct ath_hw *ah = sc->sc_ah;
  772. struct ath9k_hw_capabilities *pCap = &ah->caps;
  773. struct ath_common *common = ath9k_hw_common(ah);
  774. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB))
  775. return;
  776. /*
  777. * Change the default rx antenna if rx diversity
  778. * chooses the other antenna 3 times in a row.
  779. */
  780. if (sc->rx.defant != rs->rs_antenna) {
  781. if (++sc->rx.rxotherant >= 3)
  782. ath_setdefantenna(sc, rs->rs_antenna);
  783. } else {
  784. sc->rx.rxotherant = 0;
  785. }
  786. if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) {
  787. if (common->bt_ant_diversity)
  788. ath_ant_comb_scan(sc, rs);
  789. } else {
  790. ath_ant_comb_scan(sc, rs);
  791. }
  792. }
  793. static void ath9k_apply_ampdu_details(struct ath_softc *sc,
  794. struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
  795. {
  796. if (rs->rs_isaggr) {
  797. rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
  798. rxs->ampdu_reference = sc->rx.ampdu_ref;
  799. if (!rs->rs_moreaggr) {
  800. rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
  801. sc->rx.ampdu_ref++;
  802. }
  803. if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
  804. rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
  805. }
  806. }
  807. int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
  808. {
  809. struct ath_rxbuf *bf;
  810. struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
  811. struct ieee80211_rx_status *rxs;
  812. struct ath_hw *ah = sc->sc_ah;
  813. struct ath_common *common = ath9k_hw_common(ah);
  814. struct ieee80211_hw *hw = sc->hw;
  815. int retval;
  816. struct ath_rx_status rs;
  817. enum ath9k_rx_qtype qtype;
  818. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  819. int dma_type;
  820. u64 tsf = 0;
  821. unsigned long flags;
  822. dma_addr_t new_buf_addr;
  823. unsigned int budget = 512;
  824. if (edma)
  825. dma_type = DMA_BIDIRECTIONAL;
  826. else
  827. dma_type = DMA_FROM_DEVICE;
  828. qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
  829. tsf = ath9k_hw_gettsf64(ah);
  830. do {
  831. bool decrypt_error = false;
  832. memset(&rs, 0, sizeof(rs));
  833. if (edma)
  834. bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
  835. else
  836. bf = ath_get_next_rx_buf(sc, &rs);
  837. if (!bf)
  838. break;
  839. skb = bf->bf_mpdu;
  840. if (!skb)
  841. continue;
  842. /*
  843. * Take frame header from the first fragment and RX status from
  844. * the last one.
  845. */
  846. if (sc->rx.frag)
  847. hdr_skb = sc->rx.frag;
  848. else
  849. hdr_skb = skb;
  850. rxs = IEEE80211_SKB_RXCB(hdr_skb);
  851. memset(rxs, 0, sizeof(struct ieee80211_rx_status));
  852. retval = ath9k_rx_skb_preprocess(sc, hdr_skb, &rs, rxs,
  853. &decrypt_error, tsf);
  854. if (retval)
  855. goto requeue_drop_frag;
  856. /* Ensure we always have an skb to requeue once we are done
  857. * processing the current buffer's skb */
  858. requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
  859. /* If there is no memory we ignore the current RX'd frame,
  860. * tell hardware it can give us a new frame using the old
  861. * skb and put it at the tail of the sc->rx.rxbuf list for
  862. * processing. */
  863. if (!requeue_skb) {
  864. RX_STAT_INC(rx_oom_err);
  865. goto requeue_drop_frag;
  866. }
  867. /* We will now give hardware our shiny new allocated skb */
  868. new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
  869. common->rx_bufsize, dma_type);
  870. if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
  871. dev_kfree_skb_any(requeue_skb);
  872. goto requeue_drop_frag;
  873. }
  874. /* Unmap the frame */
  875. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  876. common->rx_bufsize, dma_type);
  877. bf->bf_mpdu = requeue_skb;
  878. bf->bf_buf_addr = new_buf_addr;
  879. skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
  880. if (ah->caps.rx_status_len)
  881. skb_pull(skb, ah->caps.rx_status_len);
  882. if (!rs.rs_more)
  883. ath9k_cmn_rx_skb_postprocess(common, hdr_skb, &rs,
  884. rxs, decrypt_error);
  885. if (rs.rs_more) {
  886. RX_STAT_INC(rx_frags);
  887. /*
  888. * rs_more indicates chained descriptors which can be
  889. * used to link buffers together for a sort of
  890. * scatter-gather operation.
  891. */
  892. if (sc->rx.frag) {
  893. /* too many fragments - cannot handle frame */
  894. dev_kfree_skb_any(sc->rx.frag);
  895. dev_kfree_skb_any(skb);
  896. RX_STAT_INC(rx_too_many_frags_err);
  897. skb = NULL;
  898. }
  899. sc->rx.frag = skb;
  900. goto requeue;
  901. }
  902. if (sc->rx.frag) {
  903. int space = skb->len - skb_tailroom(hdr_skb);
  904. if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
  905. dev_kfree_skb(skb);
  906. RX_STAT_INC(rx_oom_err);
  907. goto requeue_drop_frag;
  908. }
  909. sc->rx.frag = NULL;
  910. skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
  911. skb->len);
  912. dev_kfree_skb_any(skb);
  913. skb = hdr_skb;
  914. }
  915. if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
  916. skb_trim(skb, skb->len - 8);
  917. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  918. if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
  919. PS_WAIT_FOR_CAB |
  920. PS_WAIT_FOR_PSPOLL_DATA)) ||
  921. ath9k_check_auto_sleep(sc))
  922. ath_rx_ps(sc, skb, rs.is_mybeacon);
  923. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  924. ath9k_antenna_check(sc, &rs);
  925. ath9k_apply_ampdu_details(sc, &rs, rxs);
  926. ath_debug_rate_stats(sc, &rs, skb);
  927. ieee80211_rx(hw, skb);
  928. requeue_drop_frag:
  929. if (sc->rx.frag) {
  930. dev_kfree_skb_any(sc->rx.frag);
  931. sc->rx.frag = NULL;
  932. }
  933. requeue:
  934. list_add_tail(&bf->list, &sc->rx.rxbuf);
  935. if (!edma) {
  936. ath_rx_buf_relink(sc, bf, flush);
  937. if (!flush)
  938. ath9k_hw_rxena(ah);
  939. } else if (!flush) {
  940. ath_rx_edma_buf_link(sc, qtype);
  941. }
  942. if (!budget--)
  943. break;
  944. } while (1);
  945. if (!(ah->imask & ATH9K_INT_RXEOL)) {
  946. ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  947. ath9k_hw_set_interrupts(ah);
  948. }
  949. return 0;
  950. }