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. #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_common *common = ath9k_hw_common(sc->sc_ah);
  127. u32 nbuf = 0;
  128. if (list_empty(&sc->rx.rxbuf)) {
  129. ath_print(common, ATH_DBG_QUEUE, "No free rx buf available\n");
  130. return;
  131. }
  132. while (!list_empty(&sc->rx.rxbuf)) {
  133. nbuf++;
  134. if (!ath_rx_edma_buf_link(sc, qtype))
  135. break;
  136. if (nbuf >= size)
  137. break;
  138. }
  139. }
  140. static void ath_rx_remove_buffer(struct ath_softc *sc,
  141. enum ath9k_rx_qtype qtype)
  142. {
  143. struct ath_buf *bf;
  144. struct ath_rx_edma *rx_edma;
  145. struct sk_buff *skb;
  146. rx_edma = &sc->rx.rx_edma[qtype];
  147. while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
  148. bf = SKB_CB_ATHBUF(skb);
  149. BUG_ON(!bf);
  150. list_add_tail(&bf->list, &sc->rx.rxbuf);
  151. }
  152. }
  153. static void ath_rx_edma_cleanup(struct ath_softc *sc)
  154. {
  155. struct ath_buf *bf;
  156. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  157. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  158. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  159. if (bf->bf_mpdu)
  160. dev_kfree_skb_any(bf->bf_mpdu);
  161. }
  162. INIT_LIST_HEAD(&sc->rx.rxbuf);
  163. kfree(sc->rx.rx_bufptr);
  164. sc->rx.rx_bufptr = NULL;
  165. }
  166. static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
  167. {
  168. skb_queue_head_init(&rx_edma->rx_fifo);
  169. skb_queue_head_init(&rx_edma->rx_buffers);
  170. rx_edma->rx_fifo_hwsize = size;
  171. }
  172. static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
  173. {
  174. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  175. struct ath_hw *ah = sc->sc_ah;
  176. struct sk_buff *skb;
  177. struct ath_buf *bf;
  178. int error = 0, i;
  179. u32 size;
  180. common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN +
  181. ah->caps.rx_status_len,
  182. min(common->cachelsz, (u16)64));
  183. ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
  184. ah->caps.rx_status_len);
  185. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
  186. ah->caps.rx_lp_qdepth);
  187. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
  188. ah->caps.rx_hp_qdepth);
  189. size = sizeof(struct ath_buf) * nbufs;
  190. bf = kzalloc(size, GFP_KERNEL);
  191. if (!bf)
  192. return -ENOMEM;
  193. INIT_LIST_HEAD(&sc->rx.rxbuf);
  194. sc->rx.rx_bufptr = bf;
  195. for (i = 0; i < nbufs; i++, bf++) {
  196. skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
  197. if (!skb) {
  198. error = -ENOMEM;
  199. goto rx_init_fail;
  200. }
  201. memset(skb->data, 0, common->rx_bufsize);
  202. bf->bf_mpdu = skb;
  203. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  204. common->rx_bufsize,
  205. DMA_BIDIRECTIONAL);
  206. if (unlikely(dma_mapping_error(sc->dev,
  207. bf->bf_buf_addr))) {
  208. dev_kfree_skb_any(skb);
  209. bf->bf_mpdu = NULL;
  210. ath_print(common, ATH_DBG_FATAL,
  211. "dma_mapping_error() on RX init\n");
  212. error = -ENOMEM;
  213. goto rx_init_fail;
  214. }
  215. list_add_tail(&bf->list, &sc->rx.rxbuf);
  216. }
  217. return 0;
  218. rx_init_fail:
  219. ath_rx_edma_cleanup(sc);
  220. return error;
  221. }
  222. static void ath_edma_start_recv(struct ath_softc *sc)
  223. {
  224. spin_lock_bh(&sc->rx.rxbuflock);
  225. ath9k_hw_rxena(sc->sc_ah);
  226. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
  227. sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
  228. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
  229. sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
  230. spin_unlock_bh(&sc->rx.rxbuflock);
  231. ath_opmode_init(sc);
  232. ath9k_hw_startpcureceive(sc->sc_ah);
  233. }
  234. static void ath_edma_stop_recv(struct ath_softc *sc)
  235. {
  236. spin_lock_bh(&sc->rx.rxbuflock);
  237. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  238. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  239. spin_unlock_bh(&sc->rx.rxbuflock);
  240. }
  241. int ath_rx_init(struct ath_softc *sc, int nbufs)
  242. {
  243. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  244. struct sk_buff *skb;
  245. struct ath_buf *bf;
  246. int error = 0;
  247. spin_lock_init(&sc->rx.rxflushlock);
  248. sc->sc_flags &= ~SC_OP_RXFLUSH;
  249. spin_lock_init(&sc->rx.rxbuflock);
  250. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  251. return ath_rx_edma_init(sc, nbufs);
  252. } else {
  253. common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN,
  254. min(common->cachelsz, (u16)64));
  255. ath_print(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
  256. common->cachelsz, common->rx_bufsize);
  257. /* Initialize rx descriptors */
  258. error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
  259. "rx", nbufs, 1, 0);
  260. if (error != 0) {
  261. ath_print(common, ATH_DBG_FATAL,
  262. "failed to allocate rx descriptors: %d\n",
  263. error);
  264. goto err;
  265. }
  266. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  267. skb = ath_rxbuf_alloc(common, common->rx_bufsize,
  268. GFP_KERNEL);
  269. if (skb == NULL) {
  270. error = -ENOMEM;
  271. goto err;
  272. }
  273. bf->bf_mpdu = skb;
  274. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  275. common->rx_bufsize,
  276. DMA_FROM_DEVICE);
  277. if (unlikely(dma_mapping_error(sc->dev,
  278. bf->bf_buf_addr))) {
  279. dev_kfree_skb_any(skb);
  280. bf->bf_mpdu = NULL;
  281. ath_print(common, ATH_DBG_FATAL,
  282. "dma_mapping_error() on RX init\n");
  283. error = -ENOMEM;
  284. goto err;
  285. }
  286. bf->bf_dmacontext = bf->bf_buf_addr;
  287. }
  288. sc->rx.rxlink = NULL;
  289. }
  290. err:
  291. if (error)
  292. ath_rx_cleanup(sc);
  293. return error;
  294. }
  295. void ath_rx_cleanup(struct ath_softc *sc)
  296. {
  297. struct ath_hw *ah = sc->sc_ah;
  298. struct ath_common *common = ath9k_hw_common(ah);
  299. struct sk_buff *skb;
  300. struct ath_buf *bf;
  301. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  302. ath_rx_edma_cleanup(sc);
  303. return;
  304. } else {
  305. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  306. skb = bf->bf_mpdu;
  307. if (skb) {
  308. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  309. common->rx_bufsize,
  310. DMA_FROM_DEVICE);
  311. dev_kfree_skb(skb);
  312. }
  313. }
  314. if (sc->rx.rxdma.dd_desc_len != 0)
  315. ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
  316. }
  317. }
  318. /*
  319. * Calculate the receive filter according to the
  320. * operating mode and state:
  321. *
  322. * o always accept unicast, broadcast, and multicast traffic
  323. * o maintain current state of phy error reception (the hal
  324. * may enable phy error frames for noise immunity work)
  325. * o probe request frames are accepted only when operating in
  326. * hostap, adhoc, or monitor modes
  327. * o enable promiscuous mode according to the interface state
  328. * o accept beacons:
  329. * - when operating in adhoc mode so the 802.11 layer creates
  330. * node table entries for peers,
  331. * - when operating in station mode for collecting rssi data when
  332. * the station is otherwise quiet, or
  333. * - when operating as a repeater so we see repeater-sta beacons
  334. * - when scanning
  335. */
  336. u32 ath_calcrxfilter(struct ath_softc *sc)
  337. {
  338. #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
  339. u32 rfilt;
  340. rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
  341. | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
  342. | ATH9K_RX_FILTER_MCAST;
  343. /* If not a STA, enable processing of Probe Requests */
  344. if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
  345. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  346. /*
  347. * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
  348. * mode interface or when in monitor mode. AP mode does not need this
  349. * since it receives all in-BSS frames anyway.
  350. */
  351. if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) &&
  352. (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) ||
  353. (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR))
  354. rfilt |= ATH9K_RX_FILTER_PROM;
  355. if (sc->rx.rxfilter & FIF_CONTROL)
  356. rfilt |= ATH9K_RX_FILTER_CONTROL;
  357. if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
  358. !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
  359. rfilt |= ATH9K_RX_FILTER_MYBEACON;
  360. else
  361. rfilt |= ATH9K_RX_FILTER_BEACON;
  362. if ((AR_SREV_9280_10_OR_LATER(sc->sc_ah) ||
  363. AR_SREV_9285_10_OR_LATER(sc->sc_ah)) &&
  364. (sc->sc_ah->opmode == NL80211_IFTYPE_AP) &&
  365. (sc->rx.rxfilter & FIF_PSPOLL))
  366. rfilt |= ATH9K_RX_FILTER_PSPOLL;
  367. if (conf_is_ht(&sc->hw->conf))
  368. rfilt |= ATH9K_RX_FILTER_COMP_BAR;
  369. if (sc->sec_wiphy || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
  370. /* TODO: only needed if more than one BSSID is in use in
  371. * station/adhoc mode */
  372. /* The following may also be needed for other older chips */
  373. if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
  374. rfilt |= ATH9K_RX_FILTER_PROM;
  375. rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
  376. }
  377. return rfilt;
  378. #undef RX_FILTER_PRESERVE
  379. }
  380. int ath_startrecv(struct ath_softc *sc)
  381. {
  382. struct ath_hw *ah = sc->sc_ah;
  383. struct ath_buf *bf, *tbf;
  384. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  385. ath_edma_start_recv(sc);
  386. return 0;
  387. }
  388. spin_lock_bh(&sc->rx.rxbuflock);
  389. if (list_empty(&sc->rx.rxbuf))
  390. goto start_recv;
  391. sc->rx.rxlink = NULL;
  392. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
  393. ath_rx_buf_link(sc, bf);
  394. }
  395. /* We could have deleted elements so the list may be empty now */
  396. if (list_empty(&sc->rx.rxbuf))
  397. goto start_recv;
  398. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  399. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  400. ath9k_hw_rxena(ah);
  401. start_recv:
  402. spin_unlock_bh(&sc->rx.rxbuflock);
  403. ath_opmode_init(sc);
  404. ath9k_hw_startpcureceive(ah);
  405. return 0;
  406. }
  407. bool ath_stoprecv(struct ath_softc *sc)
  408. {
  409. struct ath_hw *ah = sc->sc_ah;
  410. bool stopped;
  411. ath9k_hw_stoppcurecv(ah);
  412. ath9k_hw_setrxfilter(ah, 0);
  413. stopped = ath9k_hw_stopdmarecv(ah);
  414. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  415. ath_edma_stop_recv(sc);
  416. else
  417. sc->rx.rxlink = NULL;
  418. return stopped;
  419. }
  420. void ath_flushrecv(struct ath_softc *sc)
  421. {
  422. spin_lock_bh(&sc->rx.rxflushlock);
  423. sc->sc_flags |= SC_OP_RXFLUSH;
  424. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  425. ath_rx_tasklet(sc, 1, true);
  426. ath_rx_tasklet(sc, 1, false);
  427. sc->sc_flags &= ~SC_OP_RXFLUSH;
  428. spin_unlock_bh(&sc->rx.rxflushlock);
  429. }
  430. static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
  431. {
  432. /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
  433. struct ieee80211_mgmt *mgmt;
  434. u8 *pos, *end, id, elen;
  435. struct ieee80211_tim_ie *tim;
  436. mgmt = (struct ieee80211_mgmt *)skb->data;
  437. pos = mgmt->u.beacon.variable;
  438. end = skb->data + skb->len;
  439. while (pos + 2 < end) {
  440. id = *pos++;
  441. elen = *pos++;
  442. if (pos + elen > end)
  443. break;
  444. if (id == WLAN_EID_TIM) {
  445. if (elen < sizeof(*tim))
  446. break;
  447. tim = (struct ieee80211_tim_ie *) pos;
  448. if (tim->dtim_count != 0)
  449. break;
  450. return tim->bitmap_ctrl & 0x01;
  451. }
  452. pos += elen;
  453. }
  454. return false;
  455. }
  456. static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
  457. {
  458. struct ieee80211_mgmt *mgmt;
  459. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  460. if (skb->len < 24 + 8 + 2 + 2)
  461. return;
  462. mgmt = (struct ieee80211_mgmt *)skb->data;
  463. if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0)
  464. return; /* not from our current AP */
  465. sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
  466. if (sc->ps_flags & PS_BEACON_SYNC) {
  467. sc->ps_flags &= ~PS_BEACON_SYNC;
  468. ath_print(common, ATH_DBG_PS,
  469. "Reconfigure Beacon timers based on "
  470. "timestamp from the AP\n");
  471. ath_beacon_config(sc, NULL);
  472. }
  473. if (ath_beacon_dtim_pending_cab(skb)) {
  474. /*
  475. * Remain awake waiting for buffered broadcast/multicast
  476. * frames. If the last broadcast/multicast frame is not
  477. * received properly, the next beacon frame will work as
  478. * a backup trigger for returning into NETWORK SLEEP state,
  479. * so we are waiting for it as well.
  480. */
  481. ath_print(common, ATH_DBG_PS, "Received DTIM beacon indicating "
  482. "buffered broadcast/multicast frame(s)\n");
  483. sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
  484. return;
  485. }
  486. if (sc->ps_flags & PS_WAIT_FOR_CAB) {
  487. /*
  488. * This can happen if a broadcast frame is dropped or the AP
  489. * fails to send a frame indicating that all CAB frames have
  490. * been delivered.
  491. */
  492. sc->ps_flags &= ~PS_WAIT_FOR_CAB;
  493. ath_print(common, ATH_DBG_PS,
  494. "PS wait for CAB frames timed out\n");
  495. }
  496. }
  497. static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb)
  498. {
  499. struct ieee80211_hdr *hdr;
  500. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  501. hdr = (struct ieee80211_hdr *)skb->data;
  502. /* Process Beacon and CAB receive in PS state */
  503. if ((sc->ps_flags & PS_WAIT_FOR_BEACON) &&
  504. ieee80211_is_beacon(hdr->frame_control))
  505. ath_rx_ps_beacon(sc, skb);
  506. else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
  507. (ieee80211_is_data(hdr->frame_control) ||
  508. ieee80211_is_action(hdr->frame_control)) &&
  509. is_multicast_ether_addr(hdr->addr1) &&
  510. !ieee80211_has_moredata(hdr->frame_control)) {
  511. /*
  512. * No more broadcast/multicast frames to be received at this
  513. * point.
  514. */
  515. sc->ps_flags &= ~PS_WAIT_FOR_CAB;
  516. ath_print(common, ATH_DBG_PS,
  517. "All PS CAB frames received, back to sleep\n");
  518. } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
  519. !is_multicast_ether_addr(hdr->addr1) &&
  520. !ieee80211_has_morefrags(hdr->frame_control)) {
  521. sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
  522. ath_print(common, ATH_DBG_PS,
  523. "Going back to sleep after having received "
  524. "PS-Poll data (0x%lx)\n",
  525. sc->ps_flags & (PS_WAIT_FOR_BEACON |
  526. PS_WAIT_FOR_CAB |
  527. PS_WAIT_FOR_PSPOLL_DATA |
  528. PS_WAIT_FOR_TX_ACK));
  529. }
  530. }
  531. static void ath_rx_send_to_mac80211(struct ieee80211_hw *hw,
  532. struct ath_softc *sc, struct sk_buff *skb,
  533. struct ieee80211_rx_status *rxs)
  534. {
  535. struct ieee80211_hdr *hdr;
  536. hdr = (struct ieee80211_hdr *)skb->data;
  537. /* Send the frame to mac80211 */
  538. if (is_multicast_ether_addr(hdr->addr1)) {
  539. int i;
  540. /*
  541. * Deliver broadcast/multicast frames to all suitable
  542. * virtual wiphys.
  543. */
  544. /* TODO: filter based on channel configuration */
  545. for (i = 0; i < sc->num_sec_wiphy; i++) {
  546. struct ath_wiphy *aphy = sc->sec_wiphy[i];
  547. struct sk_buff *nskb;
  548. if (aphy == NULL)
  549. continue;
  550. nskb = skb_copy(skb, GFP_ATOMIC);
  551. if (!nskb)
  552. continue;
  553. ieee80211_rx(aphy->hw, nskb);
  554. }
  555. ieee80211_rx(sc->hw, skb);
  556. } else
  557. /* Deliver unicast frames based on receiver address */
  558. ieee80211_rx(hw, skb);
  559. }
  560. static bool ath_edma_get_buffers(struct ath_softc *sc,
  561. enum ath9k_rx_qtype qtype)
  562. {
  563. struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
  564. struct ath_hw *ah = sc->sc_ah;
  565. struct ath_common *common = ath9k_hw_common(ah);
  566. struct sk_buff *skb;
  567. struct ath_buf *bf;
  568. int ret;
  569. skb = skb_peek(&rx_edma->rx_fifo);
  570. if (!skb)
  571. return false;
  572. bf = SKB_CB_ATHBUF(skb);
  573. BUG_ON(!bf);
  574. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  575. common->rx_bufsize, DMA_FROM_DEVICE);
  576. ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
  577. if (ret == -EINPROGRESS)
  578. return false;
  579. __skb_unlink(skb, &rx_edma->rx_fifo);
  580. if (ret == -EINVAL) {
  581. /* corrupt descriptor, skip this one and the following one */
  582. list_add_tail(&bf->list, &sc->rx.rxbuf);
  583. ath_rx_edma_buf_link(sc, qtype);
  584. skb = skb_peek(&rx_edma->rx_fifo);
  585. if (!skb)
  586. return true;
  587. bf = SKB_CB_ATHBUF(skb);
  588. BUG_ON(!bf);
  589. __skb_unlink(skb, &rx_edma->rx_fifo);
  590. list_add_tail(&bf->list, &sc->rx.rxbuf);
  591. ath_rx_edma_buf_link(sc, qtype);
  592. return true;
  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. }