recv.c 24 KB

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