recv.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. /*
  2. * Copyright (c) 2008-2009 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 "ath9k.h"
  17. #include "ar9003_mac.h"
  18. #define SKB_CB_ATHBUF(__skb) (*((struct ath_buf **)__skb->cb))
  19. static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
  20. {
  21. return sc->ps_enabled &&
  22. (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
  23. }
  24. static struct ieee80211_hw * ath_get_virt_hw(struct ath_softc *sc,
  25. struct ieee80211_hdr *hdr)
  26. {
  27. struct ieee80211_hw *hw = sc->pri_wiphy->hw;
  28. int i;
  29. spin_lock_bh(&sc->wiphy_lock);
  30. for (i = 0; i < sc->num_sec_wiphy; i++) {
  31. struct ath_wiphy *aphy = sc->sec_wiphy[i];
  32. if (aphy == NULL)
  33. continue;
  34. if (compare_ether_addr(hdr->addr1, aphy->hw->wiphy->perm_addr)
  35. == 0) {
  36. hw = aphy->hw;
  37. break;
  38. }
  39. }
  40. spin_unlock_bh(&sc->wiphy_lock);
  41. return hw;
  42. }
  43. /*
  44. * Setup and link descriptors.
  45. *
  46. * 11N: we can no longer afford to self link the last descriptor.
  47. * MAC acknowledges BA status as long as it copies frames to host
  48. * buffer (or rx fifo). This can incorrectly acknowledge packets
  49. * to a sender if last desc is self-linked.
  50. */
  51. static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
  52. {
  53. struct ath_hw *ah = sc->sc_ah;
  54. struct ath_common *common = ath9k_hw_common(ah);
  55. struct ath_desc *ds;
  56. struct sk_buff *skb;
  57. ATH_RXBUF_RESET(bf);
  58. ds = bf->bf_desc;
  59. ds->ds_link = 0; /* link to null */
  60. ds->ds_data = bf->bf_buf_addr;
  61. /* virtual addr of the beginning of the buffer. */
  62. skb = bf->bf_mpdu;
  63. BUG_ON(skb == NULL);
  64. ds->ds_vdata = skb->data;
  65. /*
  66. * setup rx descriptors. The rx_bufsize here tells the hardware
  67. * how much data it can DMA to us and that we are prepared
  68. * to process
  69. */
  70. ath9k_hw_setuprxdesc(ah, ds,
  71. common->rx_bufsize,
  72. 0);
  73. if (sc->rx.rxlink == NULL)
  74. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  75. else
  76. *sc->rx.rxlink = bf->bf_daddr;
  77. sc->rx.rxlink = &ds->ds_link;
  78. ath9k_hw_rxena(ah);
  79. }
  80. static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
  81. {
  82. /* XXX block beacon interrupts */
  83. ath9k_hw_setantenna(sc->sc_ah, antenna);
  84. sc->rx.defant = antenna;
  85. sc->rx.rxotherant = 0;
  86. }
  87. static void ath_opmode_init(struct ath_softc *sc)
  88. {
  89. struct ath_hw *ah = sc->sc_ah;
  90. struct ath_common *common = ath9k_hw_common(ah);
  91. u32 rfilt, mfilt[2];
  92. /* configure rx filter */
  93. rfilt = ath_calcrxfilter(sc);
  94. ath9k_hw_setrxfilter(ah, rfilt);
  95. /* configure bssid mask */
  96. if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
  97. ath_hw_setbssidmask(common);
  98. /* configure operational mode */
  99. ath9k_hw_setopmode(ah);
  100. /* Handle any link-level address change. */
  101. ath9k_hw_setmac(ah, common->macaddr);
  102. /* calculate and install multicast filter */
  103. mfilt[0] = mfilt[1] = ~0;
  104. ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
  105. }
  106. static bool ath_rx_edma_buf_link(struct ath_softc *sc,
  107. enum ath9k_rx_qtype qtype)
  108. {
  109. struct ath_hw *ah = sc->sc_ah;
  110. struct ath_rx_edma *rx_edma;
  111. struct sk_buff *skb;
  112. struct ath_buf *bf;
  113. rx_edma = &sc->rx.rx_edma[qtype];
  114. if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
  115. return false;
  116. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  117. list_del_init(&bf->list);
  118. skb = bf->bf_mpdu;
  119. ATH_RXBUF_RESET(bf);
  120. memset(skb->data, 0, ah->caps.rx_status_len);
  121. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  122. ah->caps.rx_status_len, DMA_TO_DEVICE);
  123. SKB_CB_ATHBUF(skb) = bf;
  124. ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
  125. skb_queue_tail(&rx_edma->rx_fifo, skb);
  126. return true;
  127. }
  128. static void ath_rx_addbuffer_edma(struct ath_softc *sc,
  129. enum ath9k_rx_qtype qtype, int size)
  130. {
  131. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  132. u32 nbuf = 0;
  133. if (list_empty(&sc->rx.rxbuf)) {
  134. ath_print(common, ATH_DBG_QUEUE, "No free rx buf available\n");
  135. return;
  136. }
  137. while (!list_empty(&sc->rx.rxbuf)) {
  138. nbuf++;
  139. if (!ath_rx_edma_buf_link(sc, qtype))
  140. break;
  141. if (nbuf >= size)
  142. break;
  143. }
  144. }
  145. static void ath_rx_remove_buffer(struct ath_softc *sc,
  146. enum ath9k_rx_qtype qtype)
  147. {
  148. struct ath_buf *bf;
  149. struct ath_rx_edma *rx_edma;
  150. struct sk_buff *skb;
  151. rx_edma = &sc->rx.rx_edma[qtype];
  152. while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
  153. bf = SKB_CB_ATHBUF(skb);
  154. BUG_ON(!bf);
  155. list_add_tail(&bf->list, &sc->rx.rxbuf);
  156. }
  157. }
  158. static void ath_rx_edma_cleanup(struct ath_softc *sc)
  159. {
  160. struct ath_buf *bf;
  161. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  162. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  163. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  164. if (bf->bf_mpdu)
  165. dev_kfree_skb_any(bf->bf_mpdu);
  166. }
  167. INIT_LIST_HEAD(&sc->rx.rxbuf);
  168. kfree(sc->rx.rx_bufptr);
  169. sc->rx.rx_bufptr = NULL;
  170. }
  171. static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
  172. {
  173. skb_queue_head_init(&rx_edma->rx_fifo);
  174. skb_queue_head_init(&rx_edma->rx_buffers);
  175. rx_edma->rx_fifo_hwsize = size;
  176. }
  177. static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
  178. {
  179. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  180. struct ath_hw *ah = sc->sc_ah;
  181. struct sk_buff *skb;
  182. struct ath_buf *bf;
  183. int error = 0, i;
  184. u32 size;
  185. common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN +
  186. ah->caps.rx_status_len,
  187. min(common->cachelsz, (u16)64));
  188. ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
  189. ah->caps.rx_status_len);
  190. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
  191. ah->caps.rx_lp_qdepth);
  192. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
  193. ah->caps.rx_hp_qdepth);
  194. size = sizeof(struct ath_buf) * nbufs;
  195. bf = kzalloc(size, GFP_KERNEL);
  196. if (!bf)
  197. return -ENOMEM;
  198. INIT_LIST_HEAD(&sc->rx.rxbuf);
  199. sc->rx.rx_bufptr = bf;
  200. for (i = 0; i < nbufs; i++, bf++) {
  201. skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
  202. if (!skb) {
  203. error = -ENOMEM;
  204. goto rx_init_fail;
  205. }
  206. memset(skb->data, 0, common->rx_bufsize);
  207. bf->bf_mpdu = skb;
  208. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  209. common->rx_bufsize,
  210. DMA_BIDIRECTIONAL);
  211. if (unlikely(dma_mapping_error(sc->dev,
  212. bf->bf_buf_addr))) {
  213. dev_kfree_skb_any(skb);
  214. bf->bf_mpdu = NULL;
  215. ath_print(common, ATH_DBG_FATAL,
  216. "dma_mapping_error() on RX init\n");
  217. error = -ENOMEM;
  218. goto rx_init_fail;
  219. }
  220. list_add_tail(&bf->list, &sc->rx.rxbuf);
  221. }
  222. return 0;
  223. rx_init_fail:
  224. ath_rx_edma_cleanup(sc);
  225. return error;
  226. }
  227. static void ath_edma_start_recv(struct ath_softc *sc)
  228. {
  229. spin_lock_bh(&sc->rx.rxbuflock);
  230. ath9k_hw_rxena(sc->sc_ah);
  231. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
  232. sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
  233. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
  234. sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
  235. spin_unlock_bh(&sc->rx.rxbuflock);
  236. ath_opmode_init(sc);
  237. ath9k_hw_startpcureceive(sc->sc_ah);
  238. }
  239. static void ath_edma_stop_recv(struct ath_softc *sc)
  240. {
  241. spin_lock_bh(&sc->rx.rxbuflock);
  242. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  243. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  244. spin_unlock_bh(&sc->rx.rxbuflock);
  245. }
  246. int ath_rx_init(struct ath_softc *sc, int nbufs)
  247. {
  248. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  249. struct sk_buff *skb;
  250. struct ath_buf *bf;
  251. int error = 0;
  252. spin_lock_init(&sc->rx.rxflushlock);
  253. sc->sc_flags &= ~SC_OP_RXFLUSH;
  254. spin_lock_init(&sc->rx.rxbuflock);
  255. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  256. return ath_rx_edma_init(sc, nbufs);
  257. } else {
  258. common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN,
  259. min(common->cachelsz, (u16)64));
  260. ath_print(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
  261. common->cachelsz, common->rx_bufsize);
  262. /* Initialize rx descriptors */
  263. error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
  264. "rx", nbufs, 1, 0);
  265. if (error != 0) {
  266. ath_print(common, ATH_DBG_FATAL,
  267. "failed to allocate rx descriptors: %d\n",
  268. error);
  269. goto err;
  270. }
  271. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  272. skb = ath_rxbuf_alloc(common, common->rx_bufsize,
  273. GFP_KERNEL);
  274. if (skb == NULL) {
  275. error = -ENOMEM;
  276. goto err;
  277. }
  278. bf->bf_mpdu = skb;
  279. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  280. common->rx_bufsize,
  281. DMA_FROM_DEVICE);
  282. if (unlikely(dma_mapping_error(sc->dev,
  283. bf->bf_buf_addr))) {
  284. dev_kfree_skb_any(skb);
  285. bf->bf_mpdu = NULL;
  286. ath_print(common, ATH_DBG_FATAL,
  287. "dma_mapping_error() on RX init\n");
  288. error = -ENOMEM;
  289. goto err;
  290. }
  291. bf->bf_dmacontext = bf->bf_buf_addr;
  292. }
  293. sc->rx.rxlink = NULL;
  294. }
  295. err:
  296. if (error)
  297. ath_rx_cleanup(sc);
  298. return error;
  299. }
  300. void ath_rx_cleanup(struct ath_softc *sc)
  301. {
  302. struct ath_hw *ah = sc->sc_ah;
  303. struct ath_common *common = ath9k_hw_common(ah);
  304. struct sk_buff *skb;
  305. struct ath_buf *bf;
  306. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  307. ath_rx_edma_cleanup(sc);
  308. return;
  309. } else {
  310. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  311. skb = bf->bf_mpdu;
  312. if (skb) {
  313. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  314. common->rx_bufsize,
  315. DMA_FROM_DEVICE);
  316. dev_kfree_skb(skb);
  317. }
  318. }
  319. if (sc->rx.rxdma.dd_desc_len != 0)
  320. ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
  321. }
  322. }
  323. /*
  324. * Calculate the receive filter according to the
  325. * operating mode and state:
  326. *
  327. * o always accept unicast, broadcast, and multicast traffic
  328. * o maintain current state of phy error reception (the hal
  329. * may enable phy error frames for noise immunity work)
  330. * o probe request frames are accepted only when operating in
  331. * hostap, adhoc, or monitor modes
  332. * o enable promiscuous mode according to the interface state
  333. * o accept beacons:
  334. * - when operating in adhoc mode so the 802.11 layer creates
  335. * node table entries for peers,
  336. * - when operating in station mode for collecting rssi data when
  337. * the station is otherwise quiet, or
  338. * - when operating as a repeater so we see repeater-sta beacons
  339. * - when scanning
  340. */
  341. u32 ath_calcrxfilter(struct ath_softc *sc)
  342. {
  343. #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
  344. u32 rfilt;
  345. rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
  346. | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
  347. | ATH9K_RX_FILTER_MCAST;
  348. /* If not a STA, enable processing of Probe Requests */
  349. if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
  350. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  351. /*
  352. * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
  353. * mode interface or when in monitor mode. AP mode does not need this
  354. * since it receives all in-BSS frames anyway.
  355. */
  356. if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) &&
  357. (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) ||
  358. (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR))
  359. rfilt |= ATH9K_RX_FILTER_PROM;
  360. if (sc->rx.rxfilter & FIF_CONTROL)
  361. rfilt |= ATH9K_RX_FILTER_CONTROL;
  362. if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
  363. !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
  364. rfilt |= ATH9K_RX_FILTER_MYBEACON;
  365. else
  366. rfilt |= ATH9K_RX_FILTER_BEACON;
  367. if ((AR_SREV_9280_10_OR_LATER(sc->sc_ah) ||
  368. AR_SREV_9285_10_OR_LATER(sc->sc_ah)) &&
  369. (sc->sc_ah->opmode == NL80211_IFTYPE_AP) &&
  370. (sc->rx.rxfilter & FIF_PSPOLL))
  371. rfilt |= ATH9K_RX_FILTER_PSPOLL;
  372. if (conf_is_ht(&sc->hw->conf))
  373. rfilt |= ATH9K_RX_FILTER_COMP_BAR;
  374. if (sc->sec_wiphy || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
  375. /* TODO: only needed if more than one BSSID is in use in
  376. * station/adhoc mode */
  377. /* The following may also be needed for other older chips */
  378. if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
  379. rfilt |= ATH9K_RX_FILTER_PROM;
  380. rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
  381. }
  382. return rfilt;
  383. #undef RX_FILTER_PRESERVE
  384. }
  385. int ath_startrecv(struct ath_softc *sc)
  386. {
  387. struct ath_hw *ah = sc->sc_ah;
  388. struct ath_buf *bf, *tbf;
  389. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  390. ath_edma_start_recv(sc);
  391. return 0;
  392. }
  393. spin_lock_bh(&sc->rx.rxbuflock);
  394. if (list_empty(&sc->rx.rxbuf))
  395. goto start_recv;
  396. sc->rx.rxlink = NULL;
  397. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
  398. ath_rx_buf_link(sc, bf);
  399. }
  400. /* We could have deleted elements so the list may be empty now */
  401. if (list_empty(&sc->rx.rxbuf))
  402. goto start_recv;
  403. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  404. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  405. ath9k_hw_rxena(ah);
  406. start_recv:
  407. spin_unlock_bh(&sc->rx.rxbuflock);
  408. ath_opmode_init(sc);
  409. ath9k_hw_startpcureceive(ah);
  410. return 0;
  411. }
  412. bool ath_stoprecv(struct ath_softc *sc)
  413. {
  414. struct ath_hw *ah = sc->sc_ah;
  415. bool stopped;
  416. ath9k_hw_stoppcurecv(ah);
  417. ath9k_hw_setrxfilter(ah, 0);
  418. stopped = ath9k_hw_stopdmarecv(ah);
  419. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  420. ath_edma_stop_recv(sc);
  421. else
  422. sc->rx.rxlink = NULL;
  423. return stopped;
  424. }
  425. void ath_flushrecv(struct ath_softc *sc)
  426. {
  427. spin_lock_bh(&sc->rx.rxflushlock);
  428. sc->sc_flags |= SC_OP_RXFLUSH;
  429. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  430. ath_rx_tasklet(sc, 1, true);
  431. ath_rx_tasklet(sc, 1, false);
  432. sc->sc_flags &= ~SC_OP_RXFLUSH;
  433. spin_unlock_bh(&sc->rx.rxflushlock);
  434. }
  435. static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
  436. {
  437. /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
  438. struct ieee80211_mgmt *mgmt;
  439. u8 *pos, *end, id, elen;
  440. struct ieee80211_tim_ie *tim;
  441. mgmt = (struct ieee80211_mgmt *)skb->data;
  442. pos = mgmt->u.beacon.variable;
  443. end = skb->data + skb->len;
  444. while (pos + 2 < end) {
  445. id = *pos++;
  446. elen = *pos++;
  447. if (pos + elen > end)
  448. break;
  449. if (id == WLAN_EID_TIM) {
  450. if (elen < sizeof(*tim))
  451. break;
  452. tim = (struct ieee80211_tim_ie *) pos;
  453. if (tim->dtim_count != 0)
  454. break;
  455. return tim->bitmap_ctrl & 0x01;
  456. }
  457. pos += elen;
  458. }
  459. return false;
  460. }
  461. static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
  462. {
  463. struct ieee80211_mgmt *mgmt;
  464. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  465. if (skb->len < 24 + 8 + 2 + 2)
  466. return;
  467. mgmt = (struct ieee80211_mgmt *)skb->data;
  468. if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0)
  469. return; /* not from our current AP */
  470. sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
  471. if (sc->ps_flags & PS_BEACON_SYNC) {
  472. sc->ps_flags &= ~PS_BEACON_SYNC;
  473. ath_print(common, ATH_DBG_PS,
  474. "Reconfigure Beacon timers based on "
  475. "timestamp from the AP\n");
  476. ath_beacon_config(sc, NULL);
  477. }
  478. if (ath_beacon_dtim_pending_cab(skb)) {
  479. /*
  480. * Remain awake waiting for buffered broadcast/multicast
  481. * frames. If the last broadcast/multicast frame is not
  482. * received properly, the next beacon frame will work as
  483. * a backup trigger for returning into NETWORK SLEEP state,
  484. * so we are waiting for it as well.
  485. */
  486. ath_print(common, ATH_DBG_PS, "Received DTIM beacon indicating "
  487. "buffered broadcast/multicast frame(s)\n");
  488. sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
  489. return;
  490. }
  491. if (sc->ps_flags & PS_WAIT_FOR_CAB) {
  492. /*
  493. * This can happen if a broadcast frame is dropped or the AP
  494. * fails to send a frame indicating that all CAB frames have
  495. * been delivered.
  496. */
  497. sc->ps_flags &= ~PS_WAIT_FOR_CAB;
  498. ath_print(common, ATH_DBG_PS,
  499. "PS wait for CAB frames timed out\n");
  500. }
  501. }
  502. static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb)
  503. {
  504. struct ieee80211_hdr *hdr;
  505. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  506. hdr = (struct ieee80211_hdr *)skb->data;
  507. /* Process Beacon and CAB receive in PS state */
  508. if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
  509. && ieee80211_is_beacon(hdr->frame_control))
  510. ath_rx_ps_beacon(sc, skb);
  511. else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
  512. (ieee80211_is_data(hdr->frame_control) ||
  513. ieee80211_is_action(hdr->frame_control)) &&
  514. is_multicast_ether_addr(hdr->addr1) &&
  515. !ieee80211_has_moredata(hdr->frame_control)) {
  516. /*
  517. * No more broadcast/multicast frames to be received at this
  518. * point.
  519. */
  520. sc->ps_flags &= ~PS_WAIT_FOR_CAB;
  521. ath_print(common, ATH_DBG_PS,
  522. "All PS CAB frames received, back to sleep\n");
  523. } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
  524. !is_multicast_ether_addr(hdr->addr1) &&
  525. !ieee80211_has_morefrags(hdr->frame_control)) {
  526. sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
  527. ath_print(common, ATH_DBG_PS,
  528. "Going back to sleep after having received "
  529. "PS-Poll data (0x%lx)\n",
  530. sc->ps_flags & (PS_WAIT_FOR_BEACON |
  531. PS_WAIT_FOR_CAB |
  532. PS_WAIT_FOR_PSPOLL_DATA |
  533. PS_WAIT_FOR_TX_ACK));
  534. }
  535. }
  536. static void ath_rx_send_to_mac80211(struct ieee80211_hw *hw,
  537. struct ath_softc *sc, struct sk_buff *skb,
  538. struct ieee80211_rx_status *rxs)
  539. {
  540. struct ieee80211_hdr *hdr;
  541. hdr = (struct ieee80211_hdr *)skb->data;
  542. /* Send the frame to mac80211 */
  543. if (is_multicast_ether_addr(hdr->addr1)) {
  544. int i;
  545. /*
  546. * Deliver broadcast/multicast frames to all suitable
  547. * virtual wiphys.
  548. */
  549. /* TODO: filter based on channel configuration */
  550. for (i = 0; i < sc->num_sec_wiphy; i++) {
  551. struct ath_wiphy *aphy = sc->sec_wiphy[i];
  552. struct sk_buff *nskb;
  553. if (aphy == NULL)
  554. continue;
  555. nskb = skb_copy(skb, GFP_ATOMIC);
  556. if (!nskb)
  557. continue;
  558. ieee80211_rx(aphy->hw, nskb);
  559. }
  560. ieee80211_rx(sc->hw, skb);
  561. } else
  562. /* Deliver unicast frames based on receiver address */
  563. ieee80211_rx(hw, skb);
  564. }
  565. static bool ath_edma_get_buffers(struct ath_softc *sc,
  566. enum ath9k_rx_qtype qtype)
  567. {
  568. struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
  569. struct ath_hw *ah = sc->sc_ah;
  570. struct ath_common *common = ath9k_hw_common(ah);
  571. struct sk_buff *skb;
  572. struct ath_buf *bf;
  573. int ret;
  574. skb = skb_peek(&rx_edma->rx_fifo);
  575. if (!skb)
  576. return false;
  577. bf = SKB_CB_ATHBUF(skb);
  578. BUG_ON(!bf);
  579. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  580. common->rx_bufsize, DMA_FROM_DEVICE);
  581. ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
  582. if (ret == -EINPROGRESS)
  583. return false;
  584. __skb_unlink(skb, &rx_edma->rx_fifo);
  585. if (ret == -EINVAL) {
  586. /* corrupt descriptor, skip this one and the following one */
  587. list_add_tail(&bf->list, &sc->rx.rxbuf);
  588. ath_rx_edma_buf_link(sc, qtype);
  589. skb = skb_peek(&rx_edma->rx_fifo);
  590. if (!skb)
  591. return true;
  592. bf = SKB_CB_ATHBUF(skb);
  593. BUG_ON(!bf);
  594. __skb_unlink(skb, &rx_edma->rx_fifo);
  595. list_add_tail(&bf->list, &sc->rx.rxbuf);
  596. ath_rx_edma_buf_link(sc, qtype);
  597. return true;
  598. }
  599. skb_queue_tail(&rx_edma->rx_buffers, skb);
  600. return true;
  601. }
  602. static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
  603. struct ath_rx_status *rs,
  604. enum ath9k_rx_qtype qtype)
  605. {
  606. struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
  607. struct sk_buff *skb;
  608. struct ath_buf *bf;
  609. while (ath_edma_get_buffers(sc, qtype));
  610. skb = __skb_dequeue(&rx_edma->rx_buffers);
  611. if (!skb)
  612. return NULL;
  613. bf = SKB_CB_ATHBUF(skb);
  614. ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data);
  615. return bf;
  616. }
  617. static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
  618. struct ath_rx_status *rs)
  619. {
  620. struct ath_hw *ah = sc->sc_ah;
  621. struct ath_common *common = ath9k_hw_common(ah);
  622. struct ath_desc *ds;
  623. struct ath_buf *bf;
  624. int ret;
  625. if (list_empty(&sc->rx.rxbuf)) {
  626. sc->rx.rxlink = NULL;
  627. return NULL;
  628. }
  629. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  630. ds = bf->bf_desc;
  631. /*
  632. * Must provide the virtual address of the current
  633. * descriptor, the physical address, and the virtual
  634. * address of the next descriptor in the h/w chain.
  635. * This allows the HAL to look ahead to see if the
  636. * hardware is done with a descriptor by checking the
  637. * done bit in the following descriptor and the address
  638. * of the current descriptor the DMA engine is working
  639. * on. All this is necessary because of our use of
  640. * a self-linked list to avoid rx overruns.
  641. */
  642. ret = ath9k_hw_rxprocdesc(ah, ds, rs, 0);
  643. if (ret == -EINPROGRESS) {
  644. struct ath_rx_status trs;
  645. struct ath_buf *tbf;
  646. struct ath_desc *tds;
  647. memset(&trs, 0, sizeof(trs));
  648. if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
  649. sc->rx.rxlink = NULL;
  650. return NULL;
  651. }
  652. tbf = list_entry(bf->list.next, struct ath_buf, list);
  653. /*
  654. * On some hardware the descriptor status words could
  655. * get corrupted, including the done bit. Because of
  656. * this, check if the next descriptor's done bit is
  657. * set or not.
  658. *
  659. * If the next descriptor's done bit is set, the current
  660. * descriptor has been corrupted. Force s/w to discard
  661. * this descriptor and continue...
  662. */
  663. tds = tbf->bf_desc;
  664. ret = ath9k_hw_rxprocdesc(ah, tds, &trs, 0);
  665. if (ret == -EINPROGRESS)
  666. return NULL;
  667. }
  668. if (!bf->bf_mpdu)
  669. return bf;
  670. /*
  671. * Synchronize the DMA transfer with CPU before
  672. * 1. accessing the frame
  673. * 2. requeueing the same buffer to h/w
  674. */
  675. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  676. common->rx_bufsize,
  677. DMA_FROM_DEVICE);
  678. return bf;
  679. }
  680. int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
  681. {
  682. struct ath_buf *bf;
  683. struct sk_buff *skb = NULL, *requeue_skb;
  684. struct ieee80211_rx_status *rxs;
  685. struct ath_hw *ah = sc->sc_ah;
  686. struct ath_common *common = ath9k_hw_common(ah);
  687. /*
  688. * The hw can techncically differ from common->hw when using ath9k
  689. * virtual wiphy so to account for that we iterate over the active
  690. * wiphys and find the appropriate wiphy and therefore hw.
  691. */
  692. struct ieee80211_hw *hw = NULL;
  693. struct ieee80211_hdr *hdr;
  694. int retval;
  695. bool decrypt_error = false;
  696. struct ath_rx_status rs;
  697. enum ath9k_rx_qtype qtype;
  698. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  699. int dma_type;
  700. if (edma)
  701. dma_type = DMA_FROM_DEVICE;
  702. else
  703. dma_type = DMA_BIDIRECTIONAL;
  704. qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
  705. spin_lock_bh(&sc->rx.rxbuflock);
  706. do {
  707. /* If handling rx interrupt and flush is in progress => exit */
  708. if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
  709. break;
  710. memset(&rs, 0, sizeof(rs));
  711. if (edma)
  712. bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
  713. else
  714. bf = ath_get_next_rx_buf(sc, &rs);
  715. if (!bf)
  716. break;
  717. skb = bf->bf_mpdu;
  718. if (!skb)
  719. continue;
  720. hdr = (struct ieee80211_hdr *) skb->data;
  721. rxs = IEEE80211_SKB_RXCB(skb);
  722. hw = ath_get_virt_hw(sc, hdr);
  723. ath_debug_stat_rx(sc, &rs);
  724. /*
  725. * If we're asked to flush receive queue, directly
  726. * chain it back at the queue without processing it.
  727. */
  728. if (flush)
  729. goto requeue;
  730. retval = ath9k_cmn_rx_skb_preprocess(common, hw, skb, &rs,
  731. rxs, &decrypt_error);
  732. if (retval)
  733. goto requeue;
  734. /* Ensure we always have an skb to requeue once we are done
  735. * processing the current buffer's skb */
  736. requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
  737. /* If there is no memory we ignore the current RX'd frame,
  738. * tell hardware it can give us a new frame using the old
  739. * skb and put it at the tail of the sc->rx.rxbuf list for
  740. * processing. */
  741. if (!requeue_skb)
  742. goto requeue;
  743. /* Unmap the frame */
  744. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  745. common->rx_bufsize,
  746. dma_type);
  747. skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
  748. if (ah->caps.rx_status_len)
  749. skb_pull(skb, ah->caps.rx_status_len);
  750. ath9k_cmn_rx_skb_postprocess(common, skb, &rs,
  751. rxs, decrypt_error);
  752. /* We will now give hardware our shiny new allocated skb */
  753. bf->bf_mpdu = requeue_skb;
  754. bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
  755. common->rx_bufsize,
  756. dma_type);
  757. if (unlikely(dma_mapping_error(sc->dev,
  758. bf->bf_buf_addr))) {
  759. dev_kfree_skb_any(requeue_skb);
  760. bf->bf_mpdu = NULL;
  761. ath_print(common, ATH_DBG_FATAL,
  762. "dma_mapping_error() on RX\n");
  763. ath_rx_send_to_mac80211(hw, sc, skb, rxs);
  764. break;
  765. }
  766. bf->bf_dmacontext = bf->bf_buf_addr;
  767. /*
  768. * change the default rx antenna if rx diversity chooses the
  769. * other antenna 3 times in a row.
  770. */
  771. if (sc->rx.defant != rs.rs_antenna) {
  772. if (++sc->rx.rxotherant >= 3)
  773. ath_setdefantenna(sc, rs.rs_antenna);
  774. } else {
  775. sc->rx.rxotherant = 0;
  776. }
  777. if (unlikely(ath9k_check_auto_sleep(sc) ||
  778. (sc->ps_flags & (PS_WAIT_FOR_BEACON |
  779. PS_WAIT_FOR_CAB |
  780. PS_WAIT_FOR_PSPOLL_DATA))))
  781. ath_rx_ps(sc, skb);
  782. ath_rx_send_to_mac80211(hw, sc, skb, rxs);
  783. requeue:
  784. if (edma) {
  785. list_add_tail(&bf->list, &sc->rx.rxbuf);
  786. ath_rx_edma_buf_link(sc, qtype);
  787. } else {
  788. list_move_tail(&bf->list, &sc->rx.rxbuf);
  789. ath_rx_buf_link(sc, bf);
  790. }
  791. } while (1);
  792. spin_unlock_bh(&sc->rx.rxbuflock);
  793. return 0;
  794. }