recv.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. static struct ieee80211_hw * ath_get_virt_hw(struct ath_softc *sc,
  18. struct ieee80211_hdr *hdr)
  19. {
  20. struct ieee80211_hw *hw = sc->pri_wiphy->hw;
  21. int i;
  22. spin_lock_bh(&sc->wiphy_lock);
  23. for (i = 0; i < sc->num_sec_wiphy; i++) {
  24. struct ath_wiphy *aphy = sc->sec_wiphy[i];
  25. if (aphy == NULL)
  26. continue;
  27. if (compare_ether_addr(hdr->addr1, aphy->hw->wiphy->perm_addr)
  28. == 0) {
  29. hw = aphy->hw;
  30. break;
  31. }
  32. }
  33. spin_unlock_bh(&sc->wiphy_lock);
  34. return hw;
  35. }
  36. /*
  37. * Setup and link descriptors.
  38. *
  39. * 11N: we can no longer afford to self link the last descriptor.
  40. * MAC acknowledges BA status as long as it copies frames to host
  41. * buffer (or rx fifo). This can incorrectly acknowledge packets
  42. * to a sender if last desc is self-linked.
  43. */
  44. static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
  45. {
  46. struct ath_hw *ah = sc->sc_ah;
  47. struct ath_desc *ds;
  48. struct sk_buff *skb;
  49. ATH_RXBUF_RESET(bf);
  50. ds = bf->bf_desc;
  51. ds->ds_link = 0; /* link to null */
  52. ds->ds_data = bf->bf_buf_addr;
  53. /* virtual addr of the beginning of the buffer. */
  54. skb = bf->bf_mpdu;
  55. ASSERT(skb != NULL);
  56. ds->ds_vdata = skb->data;
  57. /* setup rx descriptors. The rx.bufsize here tells the harware
  58. * how much data it can DMA to us and that we are prepared
  59. * to process */
  60. ath9k_hw_setuprxdesc(ah, ds,
  61. sc->rx.bufsize,
  62. 0);
  63. if (sc->rx.rxlink == NULL)
  64. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  65. else
  66. *sc->rx.rxlink = bf->bf_daddr;
  67. sc->rx.rxlink = &ds->ds_link;
  68. ath9k_hw_rxena(ah);
  69. }
  70. static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
  71. {
  72. /* XXX block beacon interrupts */
  73. ath9k_hw_setantenna(sc->sc_ah, antenna);
  74. sc->rx.defant = antenna;
  75. sc->rx.rxotherant = 0;
  76. }
  77. /*
  78. * Extend 15-bit time stamp from rx descriptor to
  79. * a full 64-bit TSF using the current h/w TSF.
  80. */
  81. static u64 ath_extend_tsf(struct ath_softc *sc, u32 rstamp)
  82. {
  83. u64 tsf;
  84. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  85. if ((tsf & 0x7fff) < rstamp)
  86. tsf -= 0x8000;
  87. return (tsf & ~0x7fff) | rstamp;
  88. }
  89. static struct sk_buff *ath_rxbuf_alloc(struct ath_softc *sc, u32 len, gfp_t gfp_mask)
  90. {
  91. struct sk_buff *skb;
  92. u32 off;
  93. /*
  94. * Cache-line-align. This is important (for the
  95. * 5210 at least) as not doing so causes bogus data
  96. * in rx'd frames.
  97. */
  98. /* Note: the kernel can allocate a value greater than
  99. * what we ask it to give us. We really only need 4 KB as that
  100. * is this hardware supports and in fact we need at least 3849
  101. * as that is the MAX AMSDU size this hardware supports.
  102. * Unfortunately this means we may get 8 KB here from the
  103. * kernel... and that is actually what is observed on some
  104. * systems :( */
  105. skb = __dev_alloc_skb(len + sc->cachelsz - 1, gfp_mask);
  106. if (skb != NULL) {
  107. off = ((unsigned long) skb->data) % sc->cachelsz;
  108. if (off != 0)
  109. skb_reserve(skb, sc->cachelsz - off);
  110. } else {
  111. DPRINTF(sc, ATH_DBG_FATAL,
  112. "skbuff alloc of size %u failed\n", len);
  113. return NULL;
  114. }
  115. return skb;
  116. }
  117. /*
  118. * For Decrypt or Demic errors, we only mark packet status here and always push
  119. * up the frame up to let mac80211 handle the actual error case, be it no
  120. * decryption key or real decryption error. This let us keep statistics there.
  121. */
  122. static int ath_rx_prepare(struct sk_buff *skb, struct ath_desc *ds,
  123. struct ieee80211_rx_status *rx_status, bool *decrypt_error,
  124. struct ath_softc *sc)
  125. {
  126. struct ieee80211_hdr *hdr;
  127. u8 ratecode;
  128. __le16 fc;
  129. struct ieee80211_hw *hw;
  130. hdr = (struct ieee80211_hdr *)skb->data;
  131. fc = hdr->frame_control;
  132. memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
  133. hw = ath_get_virt_hw(sc, hdr);
  134. if (ds->ds_rxstat.rs_more) {
  135. /*
  136. * Frame spans multiple descriptors; this cannot happen yet
  137. * as we don't support jumbograms. If not in monitor mode,
  138. * discard the frame. Enable this if you want to see
  139. * error frames in Monitor mode.
  140. */
  141. if (sc->sc_ah->opmode != NL80211_IFTYPE_MONITOR)
  142. goto rx_next;
  143. } else if (ds->ds_rxstat.rs_status != 0) {
  144. if (ds->ds_rxstat.rs_status & ATH9K_RXERR_CRC)
  145. rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
  146. if (ds->ds_rxstat.rs_status & ATH9K_RXERR_PHY)
  147. goto rx_next;
  148. if (ds->ds_rxstat.rs_status & ATH9K_RXERR_DECRYPT) {
  149. *decrypt_error = true;
  150. } else if (ds->ds_rxstat.rs_status & ATH9K_RXERR_MIC) {
  151. if (ieee80211_is_ctl(fc))
  152. /*
  153. * Sometimes, we get invalid
  154. * MIC failures on valid control frames.
  155. * Remove these mic errors.
  156. */
  157. ds->ds_rxstat.rs_status &= ~ATH9K_RXERR_MIC;
  158. else
  159. rx_status->flag |= RX_FLAG_MMIC_ERROR;
  160. }
  161. /*
  162. * Reject error frames with the exception of
  163. * decryption and MIC failures. For monitor mode,
  164. * we also ignore the CRC error.
  165. */
  166. if (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR) {
  167. if (ds->ds_rxstat.rs_status &
  168. ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
  169. ATH9K_RXERR_CRC))
  170. goto rx_next;
  171. } else {
  172. if (ds->ds_rxstat.rs_status &
  173. ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
  174. goto rx_next;
  175. }
  176. }
  177. }
  178. ratecode = ds->ds_rxstat.rs_rate;
  179. if (ratecode & 0x80) {
  180. /* HT rate */
  181. rx_status->flag |= RX_FLAG_HT;
  182. if (ds->ds_rxstat.rs_flags & ATH9K_RX_2040)
  183. rx_status->flag |= RX_FLAG_40MHZ;
  184. if (ds->ds_rxstat.rs_flags & ATH9K_RX_GI)
  185. rx_status->flag |= RX_FLAG_SHORT_GI;
  186. rx_status->rate_idx = ratecode & 0x7f;
  187. } else {
  188. int i = 0, cur_band, n_rates;
  189. cur_band = hw->conf.channel->band;
  190. n_rates = sc->sbands[cur_band].n_bitrates;
  191. for (i = 0; i < n_rates; i++) {
  192. if (sc->sbands[cur_band].bitrates[i].hw_value ==
  193. ratecode) {
  194. rx_status->rate_idx = i;
  195. break;
  196. }
  197. if (sc->sbands[cur_band].bitrates[i].hw_value_short ==
  198. ratecode) {
  199. rx_status->rate_idx = i;
  200. rx_status->flag |= RX_FLAG_SHORTPRE;
  201. break;
  202. }
  203. }
  204. }
  205. rx_status->mactime = ath_extend_tsf(sc, ds->ds_rxstat.rs_tstamp);
  206. rx_status->band = hw->conf.channel->band;
  207. rx_status->freq = hw->conf.channel->center_freq;
  208. rx_status->noise = sc->ani.noise_floor;
  209. rx_status->signal = rx_status->noise + ds->ds_rxstat.rs_rssi;
  210. rx_status->antenna = ds->ds_rxstat.rs_antenna;
  211. /* at 45 you will be able to use MCS 15 reliably. A more elaborate
  212. * scheme can be used here but it requires tables of SNR/throughput for
  213. * each possible mode used. */
  214. rx_status->qual = ds->ds_rxstat.rs_rssi * 100 / 45;
  215. /* rssi can be more than 45 though, anything above that
  216. * should be considered at 100% */
  217. if (rx_status->qual > 100)
  218. rx_status->qual = 100;
  219. rx_status->flag |= RX_FLAG_TSFT;
  220. return 1;
  221. rx_next:
  222. return 0;
  223. }
  224. static void ath_opmode_init(struct ath_softc *sc)
  225. {
  226. struct ath_hw *ah = sc->sc_ah;
  227. u32 rfilt, mfilt[2];
  228. /* configure rx filter */
  229. rfilt = ath_calcrxfilter(sc);
  230. ath9k_hw_setrxfilter(ah, rfilt);
  231. /* configure bssid mask */
  232. if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
  233. ath9k_hw_setbssidmask(sc);
  234. /* configure operational mode */
  235. ath9k_hw_setopmode(ah);
  236. /* Handle any link-level address change. */
  237. ath9k_hw_setmac(ah, sc->sc_ah->macaddr);
  238. /* calculate and install multicast filter */
  239. mfilt[0] = mfilt[1] = ~0;
  240. ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
  241. }
  242. int ath_rx_init(struct ath_softc *sc, int nbufs)
  243. {
  244. struct sk_buff *skb;
  245. struct ath_buf *bf;
  246. int error = 0;
  247. do {
  248. spin_lock_init(&sc->rx.rxflushlock);
  249. sc->sc_flags &= ~SC_OP_RXFLUSH;
  250. spin_lock_init(&sc->rx.rxbuflock);
  251. sc->rx.bufsize = roundup(IEEE80211_MAX_MPDU_LEN,
  252. min(sc->cachelsz,
  253. (u16)64));
  254. DPRINTF(sc, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
  255. sc->cachelsz, sc->rx.bufsize);
  256. /* Initialize rx descriptors */
  257. error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
  258. "rx", nbufs, 1);
  259. if (error != 0) {
  260. DPRINTF(sc, ATH_DBG_FATAL,
  261. "failed to allocate rx descriptors: %d\n", error);
  262. break;
  263. }
  264. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  265. skb = ath_rxbuf_alloc(sc, sc->rx.bufsize, GFP_KERNEL);
  266. if (skb == NULL) {
  267. error = -ENOMEM;
  268. break;
  269. }
  270. bf->bf_mpdu = skb;
  271. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  272. sc->rx.bufsize,
  273. DMA_FROM_DEVICE);
  274. if (unlikely(dma_mapping_error(sc->dev,
  275. bf->bf_buf_addr))) {
  276. dev_kfree_skb_any(skb);
  277. bf->bf_mpdu = NULL;
  278. DPRINTF(sc, ATH_DBG_CONFIG,
  279. "dma_mapping_error() on RX init\n");
  280. error = -ENOMEM;
  281. break;
  282. }
  283. bf->bf_dmacontext = bf->bf_buf_addr;
  284. }
  285. sc->rx.rxlink = NULL;
  286. } while (0);
  287. if (error)
  288. ath_rx_cleanup(sc);
  289. return error;
  290. }
  291. void ath_rx_cleanup(struct ath_softc *sc)
  292. {
  293. struct sk_buff *skb;
  294. struct ath_buf *bf;
  295. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  296. skb = bf->bf_mpdu;
  297. if (skb) {
  298. dma_unmap_single(sc->dev,
  299. bf->bf_buf_addr,
  300. sc->rx.bufsize,
  301. DMA_FROM_DEVICE);
  302. dev_kfree_skb(skb);
  303. }
  304. }
  305. if (sc->rx.rxdma.dd_desc_len != 0)
  306. ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
  307. }
  308. /*
  309. * Calculate the receive filter according to the
  310. * operating mode and state:
  311. *
  312. * o always accept unicast, broadcast, and multicast traffic
  313. * o maintain current state of phy error reception (the hal
  314. * may enable phy error frames for noise immunity work)
  315. * o probe request frames are accepted only when operating in
  316. * hostap, adhoc, or monitor modes
  317. * o enable promiscuous mode according to the interface state
  318. * o accept beacons:
  319. * - when operating in adhoc mode so the 802.11 layer creates
  320. * node table entries for peers,
  321. * - when operating in station mode for collecting rssi data when
  322. * the station is otherwise quiet, or
  323. * - when operating as a repeater so we see repeater-sta beacons
  324. * - when scanning
  325. */
  326. u32 ath_calcrxfilter(struct ath_softc *sc)
  327. {
  328. #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
  329. u32 rfilt;
  330. rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
  331. | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
  332. | ATH9K_RX_FILTER_MCAST;
  333. /* If not a STA, enable processing of Probe Requests */
  334. if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
  335. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  336. /*
  337. * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
  338. * mode interface or when in monitor mode. AP mode does not need this
  339. * since it receives all in-BSS frames anyway.
  340. */
  341. if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) &&
  342. (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) ||
  343. (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR))
  344. rfilt |= ATH9K_RX_FILTER_PROM;
  345. if (sc->rx.rxfilter & FIF_CONTROL)
  346. rfilt |= ATH9K_RX_FILTER_CONTROL;
  347. if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
  348. !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
  349. rfilt |= ATH9K_RX_FILTER_MYBEACON;
  350. else
  351. rfilt |= ATH9K_RX_FILTER_BEACON;
  352. /* If in HOSTAP mode, want to enable reception of PSPOLL frames */
  353. if (sc->sc_ah->opmode == NL80211_IFTYPE_AP)
  354. rfilt |= ATH9K_RX_FILTER_PSPOLL;
  355. if (sc->sec_wiphy) {
  356. /* TODO: only needed if more than one BSSID is in use in
  357. * station/adhoc mode */
  358. /* TODO: for older chips, may need to add ATH9K_RX_FILTER_PROM
  359. */
  360. rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
  361. }
  362. return rfilt;
  363. #undef RX_FILTER_PRESERVE
  364. }
  365. int ath_startrecv(struct ath_softc *sc)
  366. {
  367. struct ath_hw *ah = sc->sc_ah;
  368. struct ath_buf *bf, *tbf;
  369. spin_lock_bh(&sc->rx.rxbuflock);
  370. if (list_empty(&sc->rx.rxbuf))
  371. goto start_recv;
  372. sc->rx.rxlink = NULL;
  373. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
  374. ath_rx_buf_link(sc, bf);
  375. }
  376. /* We could have deleted elements so the list may be empty now */
  377. if (list_empty(&sc->rx.rxbuf))
  378. goto start_recv;
  379. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  380. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  381. ath9k_hw_rxena(ah);
  382. start_recv:
  383. spin_unlock_bh(&sc->rx.rxbuflock);
  384. ath_opmode_init(sc);
  385. ath9k_hw_startpcureceive(ah);
  386. return 0;
  387. }
  388. bool ath_stoprecv(struct ath_softc *sc)
  389. {
  390. struct ath_hw *ah = sc->sc_ah;
  391. bool stopped;
  392. ath9k_hw_stoppcurecv(ah);
  393. ath9k_hw_setrxfilter(ah, 0);
  394. stopped = ath9k_hw_stopdmarecv(ah);
  395. sc->rx.rxlink = NULL;
  396. return stopped;
  397. }
  398. void ath_flushrecv(struct ath_softc *sc)
  399. {
  400. spin_lock_bh(&sc->rx.rxflushlock);
  401. sc->sc_flags |= SC_OP_RXFLUSH;
  402. ath_rx_tasklet(sc, 1);
  403. sc->sc_flags &= ~SC_OP_RXFLUSH;
  404. spin_unlock_bh(&sc->rx.rxflushlock);
  405. }
  406. int ath_rx_tasklet(struct ath_softc *sc, int flush)
  407. {
  408. #define PA2DESC(_sc, _pa) \
  409. ((struct ath_desc *)((caddr_t)(_sc)->rx.rxdma.dd_desc + \
  410. ((_pa) - (_sc)->rx.rxdma.dd_desc_paddr)))
  411. struct ath_buf *bf;
  412. struct ath_desc *ds;
  413. struct sk_buff *skb = NULL, *requeue_skb;
  414. struct ieee80211_rx_status rx_status;
  415. struct ath_hw *ah = sc->sc_ah;
  416. struct ieee80211_hdr *hdr;
  417. int hdrlen, padsize, retval;
  418. bool decrypt_error = false;
  419. u8 keyix;
  420. __le16 fc;
  421. spin_lock_bh(&sc->rx.rxbuflock);
  422. do {
  423. /* If handling rx interrupt and flush is in progress => exit */
  424. if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
  425. break;
  426. if (list_empty(&sc->rx.rxbuf)) {
  427. sc->rx.rxlink = NULL;
  428. break;
  429. }
  430. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  431. ds = bf->bf_desc;
  432. /*
  433. * Must provide the virtual address of the current
  434. * descriptor, the physical address, and the virtual
  435. * address of the next descriptor in the h/w chain.
  436. * This allows the HAL to look ahead to see if the
  437. * hardware is done with a descriptor by checking the
  438. * done bit in the following descriptor and the address
  439. * of the current descriptor the DMA engine is working
  440. * on. All this is necessary because of our use of
  441. * a self-linked list to avoid rx overruns.
  442. */
  443. retval = ath9k_hw_rxprocdesc(ah, ds,
  444. bf->bf_daddr,
  445. PA2DESC(sc, ds->ds_link),
  446. 0);
  447. if (retval == -EINPROGRESS) {
  448. struct ath_buf *tbf;
  449. struct ath_desc *tds;
  450. if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
  451. sc->rx.rxlink = NULL;
  452. break;
  453. }
  454. tbf = list_entry(bf->list.next, struct ath_buf, list);
  455. /*
  456. * On some hardware the descriptor status words could
  457. * get corrupted, including the done bit. Because of
  458. * this, check if the next descriptor's done bit is
  459. * set or not.
  460. *
  461. * If the next descriptor's done bit is set, the current
  462. * descriptor has been corrupted. Force s/w to discard
  463. * this descriptor and continue...
  464. */
  465. tds = tbf->bf_desc;
  466. retval = ath9k_hw_rxprocdesc(ah, tds, tbf->bf_daddr,
  467. PA2DESC(sc, tds->ds_link), 0);
  468. if (retval == -EINPROGRESS) {
  469. break;
  470. }
  471. }
  472. skb = bf->bf_mpdu;
  473. if (!skb)
  474. continue;
  475. /*
  476. * Synchronize the DMA transfer with CPU before
  477. * 1. accessing the frame
  478. * 2. requeueing the same buffer to h/w
  479. */
  480. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  481. sc->rx.bufsize,
  482. DMA_FROM_DEVICE);
  483. /*
  484. * If we're asked to flush receive queue, directly
  485. * chain it back at the queue without processing it.
  486. */
  487. if (flush)
  488. goto requeue;
  489. if (!ds->ds_rxstat.rs_datalen)
  490. goto requeue;
  491. /* The status portion of the descriptor could get corrupted. */
  492. if (sc->rx.bufsize < ds->ds_rxstat.rs_datalen)
  493. goto requeue;
  494. if (!ath_rx_prepare(skb, ds, &rx_status, &decrypt_error, sc))
  495. goto requeue;
  496. /* Ensure we always have an skb to requeue once we are done
  497. * processing the current buffer's skb */
  498. requeue_skb = ath_rxbuf_alloc(sc, sc->rx.bufsize, GFP_ATOMIC);
  499. /* If there is no memory we ignore the current RX'd frame,
  500. * tell hardware it can give us a new frame using the old
  501. * skb and put it at the tail of the sc->rx.rxbuf list for
  502. * processing. */
  503. if (!requeue_skb)
  504. goto requeue;
  505. /* Unmap the frame */
  506. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  507. sc->rx.bufsize,
  508. DMA_FROM_DEVICE);
  509. skb_put(skb, ds->ds_rxstat.rs_datalen);
  510. skb->protocol = cpu_to_be16(ETH_P_CONTROL);
  511. /* see if any padding is done by the hw and remove it */
  512. hdr = (struct ieee80211_hdr *)skb->data;
  513. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  514. fc = hdr->frame_control;
  515. /* The MAC header is padded to have 32-bit boundary if the
  516. * packet payload is non-zero. The general calculation for
  517. * padsize would take into account odd header lengths:
  518. * padsize = (4 - hdrlen % 4) % 4; However, since only
  519. * even-length headers are used, padding can only be 0 or 2
  520. * bytes and we can optimize this a bit. In addition, we must
  521. * not try to remove padding from short control frames that do
  522. * not have payload. */
  523. padsize = hdrlen & 3;
  524. if (padsize && hdrlen >= 24) {
  525. memmove(skb->data + padsize, skb->data, hdrlen);
  526. skb_pull(skb, padsize);
  527. }
  528. keyix = ds->ds_rxstat.rs_keyix;
  529. if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error) {
  530. rx_status.flag |= RX_FLAG_DECRYPTED;
  531. } else if ((le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_PROTECTED)
  532. && !decrypt_error && skb->len >= hdrlen + 4) {
  533. keyix = skb->data[hdrlen + 3] >> 6;
  534. if (test_bit(keyix, sc->keymap))
  535. rx_status.flag |= RX_FLAG_DECRYPTED;
  536. }
  537. if (ah->sw_mgmt_crypto &&
  538. (rx_status.flag & RX_FLAG_DECRYPTED) &&
  539. ieee80211_is_mgmt(hdr->frame_control)) {
  540. /* Use software decrypt for management frames. */
  541. rx_status.flag &= ~RX_FLAG_DECRYPTED;
  542. }
  543. /* Send the frame to mac80211 */
  544. if (hdr->addr1[5] & 0x01) {
  545. int i;
  546. /*
  547. * Deliver broadcast/multicast frames to all suitable
  548. * virtual wiphys.
  549. */
  550. /* TODO: filter based on channel configuration */
  551. for (i = 0; i < sc->num_sec_wiphy; i++) {
  552. struct ath_wiphy *aphy = sc->sec_wiphy[i];
  553. struct sk_buff *nskb;
  554. if (aphy == NULL)
  555. continue;
  556. nskb = skb_copy(skb, GFP_ATOMIC);
  557. if (nskb)
  558. __ieee80211_rx(aphy->hw, nskb,
  559. &rx_status);
  560. }
  561. __ieee80211_rx(sc->hw, skb, &rx_status);
  562. } else {
  563. /* Deliver unicast frames based on receiver address */
  564. __ieee80211_rx(ath_get_virt_hw(sc, hdr), skb,
  565. &rx_status);
  566. }
  567. /* We will now give hardware our shiny new allocated skb */
  568. bf->bf_mpdu = requeue_skb;
  569. bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
  570. sc->rx.bufsize,
  571. DMA_FROM_DEVICE);
  572. if (unlikely(dma_mapping_error(sc->dev,
  573. bf->bf_buf_addr))) {
  574. dev_kfree_skb_any(requeue_skb);
  575. bf->bf_mpdu = NULL;
  576. DPRINTF(sc, ATH_DBG_CONFIG,
  577. "dma_mapping_error() on RX\n");
  578. break;
  579. }
  580. bf->bf_dmacontext = bf->bf_buf_addr;
  581. /*
  582. * change the default rx antenna if rx diversity chooses the
  583. * other antenna 3 times in a row.
  584. */
  585. if (sc->rx.defant != ds->ds_rxstat.rs_antenna) {
  586. if (++sc->rx.rxotherant >= 3)
  587. ath_setdefantenna(sc, ds->ds_rxstat.rs_antenna);
  588. } else {
  589. sc->rx.rxotherant = 0;
  590. }
  591. if (ieee80211_is_beacon(fc) &&
  592. (sc->sc_flags & SC_OP_WAIT_FOR_BEACON)) {
  593. sc->sc_flags &= ~SC_OP_WAIT_FOR_BEACON;
  594. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
  595. }
  596. requeue:
  597. list_move_tail(&bf->list, &sc->rx.rxbuf);
  598. ath_rx_buf_link(sc, bf);
  599. } while (1);
  600. spin_unlock_bh(&sc->rx.rxbuflock);
  601. return 0;
  602. #undef PA2DESC
  603. }