recv.c 24 KB

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