recv.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. * Copyright (c) 2008 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. /*
  17. * Implementation of receive path.
  18. */
  19. #include "core.h"
  20. /*
  21. * Setup and link descriptors.
  22. *
  23. * 11N: we can no longer afford to self link the last descriptor.
  24. * MAC acknowledges BA status as long as it copies frames to host
  25. * buffer (or rx fifo). This can incorrectly acknowledge packets
  26. * to a sender if last desc is self-linked.
  27. *
  28. * NOTE: Caller should hold the rxbuf lock.
  29. */
  30. static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
  31. {
  32. struct ath_hal *ah = sc->sc_ah;
  33. struct ath_desc *ds;
  34. struct sk_buff *skb;
  35. ATH_RXBUF_RESET(bf);
  36. ds = bf->bf_desc;
  37. ds->ds_link = 0; /* link to null */
  38. ds->ds_data = bf->bf_buf_addr;
  39. /* XXX For RADAR?
  40. * virtual addr of the beginning of the buffer. */
  41. skb = bf->bf_mpdu;
  42. ASSERT(skb != NULL);
  43. ds->ds_vdata = skb->data;
  44. /* setup rx descriptors */
  45. ath9k_hw_setuprxdesc(ah,
  46. ds,
  47. skb_tailroom(skb), /* buffer size */
  48. 0);
  49. if (sc->sc_rxlink == NULL)
  50. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  51. else
  52. *sc->sc_rxlink = bf->bf_daddr;
  53. sc->sc_rxlink = &ds->ds_link;
  54. ath9k_hw_rxena(ah);
  55. }
  56. static struct sk_buff *ath_rxbuf_alloc(struct ath_softc *sc,
  57. u32 len)
  58. {
  59. struct sk_buff *skb;
  60. u32 off;
  61. /*
  62. * Cache-line-align. This is important (for the
  63. * 5210 at least) as not doing so causes bogus data
  64. * in rx'd frames.
  65. */
  66. skb = dev_alloc_skb(len + sc->sc_cachelsz - 1);
  67. if (skb != NULL) {
  68. off = ((unsigned long) skb->data) % sc->sc_cachelsz;
  69. if (off != 0)
  70. skb_reserve(skb, sc->sc_cachelsz - off);
  71. } else {
  72. DPRINTF(sc, ATH_DBG_FATAL,
  73. "%s: skbuff alloc of size %u failed\n",
  74. __func__, len);
  75. return NULL;
  76. }
  77. return skb;
  78. }
  79. static void ath_rx_requeue(struct ath_softc *sc, struct sk_buff *skb)
  80. {
  81. struct ath_buf *bf = ATH_RX_CONTEXT(skb)->ctx_rxbuf;
  82. ASSERT(bf != NULL);
  83. spin_lock_bh(&sc->sc_rxbuflock);
  84. if (bf->bf_status & ATH_BUFSTATUS_STALE) {
  85. /*
  86. * This buffer is still held for hw acess.
  87. * Mark it as free to be re-queued it later.
  88. */
  89. bf->bf_status |= ATH_BUFSTATUS_FREE;
  90. } else {
  91. /* XXX: we probably never enter here, remove after
  92. * verification */
  93. list_add_tail(&bf->list, &sc->sc_rxbuf);
  94. ath_rx_buf_link(sc, bf);
  95. }
  96. spin_unlock_bh(&sc->sc_rxbuflock);
  97. }
  98. /*
  99. * The skb indicated to upper stack won't be returned to us.
  100. * So we have to allocate a new one and queue it by ourselves.
  101. */
  102. static int ath_rx_indicate(struct ath_softc *sc,
  103. struct sk_buff *skb,
  104. struct ath_recv_status *status,
  105. u16 keyix)
  106. {
  107. struct ath_buf *bf = ATH_RX_CONTEXT(skb)->ctx_rxbuf;
  108. struct sk_buff *nskb;
  109. int type;
  110. /* indicate frame to the stack, which will free the old skb. */
  111. type = _ath_rx_indicate(sc, skb, status, keyix);
  112. /* allocate a new skb and queue it to for H/W processing */
  113. nskb = ath_rxbuf_alloc(sc, sc->sc_rxbufsize);
  114. if (nskb != NULL) {
  115. bf->bf_mpdu = nskb;
  116. bf->bf_buf_addr = pci_map_single(sc->pdev, nskb->data,
  117. skb_end_pointer(nskb) - nskb->head,
  118. PCI_DMA_FROMDEVICE);
  119. bf->bf_dmacontext = bf->bf_buf_addr;
  120. ATH_RX_CONTEXT(nskb)->ctx_rxbuf = bf;
  121. /* queue the new wbuf to H/W */
  122. ath_rx_requeue(sc, nskb);
  123. }
  124. return type;
  125. }
  126. static void ath_opmode_init(struct ath_softc *sc)
  127. {
  128. struct ath_hal *ah = sc->sc_ah;
  129. u32 rfilt, mfilt[2];
  130. /* configure rx filter */
  131. rfilt = ath_calcrxfilter(sc);
  132. ath9k_hw_setrxfilter(ah, rfilt);
  133. /* configure bssid mask */
  134. if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
  135. ath9k_hw_setbssidmask(ah, sc->sc_bssidmask);
  136. /* configure operational mode */
  137. ath9k_hw_setopmode(ah);
  138. /* Handle any link-level address change. */
  139. ath9k_hw_setmac(ah, sc->sc_myaddr);
  140. /* calculate and install multicast filter */
  141. mfilt[0] = mfilt[1] = ~0;
  142. ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
  143. DPRINTF(sc, ATH_DBG_CONFIG ,
  144. "%s: RX filter 0x%x, MC filter %08x:%08x\n",
  145. __func__, rfilt, mfilt[0], mfilt[1]);
  146. }
  147. int ath_rx_init(struct ath_softc *sc, int nbufs)
  148. {
  149. struct sk_buff *skb;
  150. struct ath_buf *bf;
  151. int error = 0;
  152. do {
  153. spin_lock_init(&sc->sc_rxflushlock);
  154. sc->sc_flags &= ~SC_OP_RXFLUSH;
  155. spin_lock_init(&sc->sc_rxbuflock);
  156. /*
  157. * Cisco's VPN software requires that drivers be able to
  158. * receive encapsulated frames that are larger than the MTU.
  159. * Since we can't be sure how large a frame we'll get, setup
  160. * to handle the larges on possible.
  161. */
  162. sc->sc_rxbufsize = roundup(IEEE80211_MAX_MPDU_LEN,
  163. min(sc->sc_cachelsz,
  164. (u16)64));
  165. DPRINTF(sc, ATH_DBG_CONFIG, "%s: cachelsz %u rxbufsize %u\n",
  166. __func__, sc->sc_cachelsz, sc->sc_rxbufsize);
  167. /* Initialize rx descriptors */
  168. error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
  169. "rx", nbufs, 1);
  170. if (error != 0) {
  171. DPRINTF(sc, ATH_DBG_FATAL,
  172. "%s: failed to allocate rx descriptors: %d\n",
  173. __func__, error);
  174. break;
  175. }
  176. /* Pre-allocate a wbuf for each rx buffer */
  177. list_for_each_entry(bf, &sc->sc_rxbuf, list) {
  178. skb = ath_rxbuf_alloc(sc, sc->sc_rxbufsize);
  179. if (skb == NULL) {
  180. error = -ENOMEM;
  181. break;
  182. }
  183. bf->bf_mpdu = skb;
  184. bf->bf_buf_addr = pci_map_single(sc->pdev, skb->data,
  185. skb_end_pointer(skb) - skb->head,
  186. PCI_DMA_FROMDEVICE);
  187. bf->bf_dmacontext = bf->bf_buf_addr;
  188. ATH_RX_CONTEXT(skb)->ctx_rxbuf = bf;
  189. }
  190. sc->sc_rxlink = NULL;
  191. } while (0);
  192. if (error)
  193. ath_rx_cleanup(sc);
  194. return error;
  195. }
  196. /* Reclaim all rx queue resources */
  197. void ath_rx_cleanup(struct ath_softc *sc)
  198. {
  199. struct sk_buff *skb;
  200. struct ath_buf *bf;
  201. list_for_each_entry(bf, &sc->sc_rxbuf, list) {
  202. skb = bf->bf_mpdu;
  203. if (skb)
  204. dev_kfree_skb(skb);
  205. }
  206. /* cleanup rx descriptors */
  207. if (sc->sc_rxdma.dd_desc_len != 0)
  208. ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
  209. }
  210. /*
  211. * Calculate the receive filter according to the
  212. * operating mode and state:
  213. *
  214. * o always accept unicast, broadcast, and multicast traffic
  215. * o maintain current state of phy error reception (the hal
  216. * may enable phy error frames for noise immunity work)
  217. * o probe request frames are accepted only when operating in
  218. * hostap, adhoc, or monitor modes
  219. * o enable promiscuous mode according to the interface state
  220. * o accept beacons:
  221. * - when operating in adhoc mode so the 802.11 layer creates
  222. * node table entries for peers,
  223. * - when operating in station mode for collecting rssi data when
  224. * the station is otherwise quiet, or
  225. * - when operating as a repeater so we see repeater-sta beacons
  226. * - when scanning
  227. */
  228. u32 ath_calcrxfilter(struct ath_softc *sc)
  229. {
  230. #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
  231. u32 rfilt;
  232. rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
  233. | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
  234. | ATH9K_RX_FILTER_MCAST;
  235. /* If not a STA, enable processing of Probe Requests */
  236. if (sc->sc_ah->ah_opmode != ATH9K_M_STA)
  237. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  238. /* Can't set HOSTAP into promiscous mode */
  239. if (((sc->sc_ah->ah_opmode != ATH9K_M_HOSTAP) &&
  240. (sc->rx_filter & FIF_PROMISC_IN_BSS)) ||
  241. (sc->sc_ah->ah_opmode == ATH9K_M_MONITOR)) {
  242. rfilt |= ATH9K_RX_FILTER_PROM;
  243. /* ??? To prevent from sending ACK */
  244. rfilt &= ~ATH9K_RX_FILTER_UCAST;
  245. }
  246. if (((sc->sc_ah->ah_opmode == ATH9K_M_STA) &&
  247. (sc->rx_filter & FIF_BCN_PRBRESP_PROMISC)) ||
  248. (sc->sc_ah->ah_opmode == ATH9K_M_IBSS))
  249. rfilt |= ATH9K_RX_FILTER_BEACON;
  250. /* If in HOSTAP mode, want to enable reception of PSPOLL frames
  251. & beacon frames */
  252. if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP)
  253. rfilt |= (ATH9K_RX_FILTER_BEACON | ATH9K_RX_FILTER_PSPOLL);
  254. return rfilt;
  255. #undef RX_FILTER_PRESERVE
  256. }
  257. /* Enable the receive h/w following a reset. */
  258. int ath_startrecv(struct ath_softc *sc)
  259. {
  260. struct ath_hal *ah = sc->sc_ah;
  261. struct ath_buf *bf, *tbf;
  262. spin_lock_bh(&sc->sc_rxbuflock);
  263. if (list_empty(&sc->sc_rxbuf))
  264. goto start_recv;
  265. sc->sc_rxlink = NULL;
  266. list_for_each_entry_safe(bf, tbf, &sc->sc_rxbuf, list) {
  267. if (bf->bf_status & ATH_BUFSTATUS_STALE) {
  268. /* restarting h/w, no need for holding descriptors */
  269. bf->bf_status &= ~ATH_BUFSTATUS_STALE;
  270. /*
  271. * Upper layer may not be done with the frame yet so
  272. * we can't just re-queue it to hardware. Remove it
  273. * from h/w queue. It'll be re-queued when upper layer
  274. * returns the frame and ath_rx_requeue_mpdu is called.
  275. */
  276. if (!(bf->bf_status & ATH_BUFSTATUS_FREE)) {
  277. list_del(&bf->list);
  278. continue;
  279. }
  280. }
  281. /* chain descriptors */
  282. ath_rx_buf_link(sc, bf);
  283. }
  284. /* We could have deleted elements so the list may be empty now */
  285. if (list_empty(&sc->sc_rxbuf))
  286. goto start_recv;
  287. bf = list_first_entry(&sc->sc_rxbuf, struct ath_buf, list);
  288. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  289. ath9k_hw_rxena(ah); /* enable recv descriptors */
  290. start_recv:
  291. spin_unlock_bh(&sc->sc_rxbuflock);
  292. ath_opmode_init(sc); /* set filters, etc. */
  293. ath9k_hw_startpcureceive(ah); /* re-enable PCU/DMA engine */
  294. return 0;
  295. }
  296. /* Disable the receive h/w in preparation for a reset. */
  297. bool ath_stoprecv(struct ath_softc *sc)
  298. {
  299. struct ath_hal *ah = sc->sc_ah;
  300. u64 tsf;
  301. bool stopped;
  302. ath9k_hw_stoppcurecv(ah); /* disable PCU */
  303. ath9k_hw_setrxfilter(ah, 0); /* clear recv filter */
  304. stopped = ath9k_hw_stopdmarecv(ah); /* disable DMA engine */
  305. mdelay(3); /* 3ms is long enough for 1 frame */
  306. tsf = ath9k_hw_gettsf64(ah);
  307. sc->sc_rxlink = NULL; /* just in case */
  308. return stopped;
  309. }
  310. /* Flush receive queue */
  311. void ath_flushrecv(struct ath_softc *sc)
  312. {
  313. /*
  314. * ath_rx_tasklet may be used to handle rx interrupt and flush receive
  315. * queue at the same time. Use a lock to serialize the access of rx
  316. * queue.
  317. * ath_rx_tasklet cannot hold the spinlock while indicating packets.
  318. * Instead, do not claim the spinlock but check for a flush in
  319. * progress (see references to sc_rxflush)
  320. */
  321. spin_lock_bh(&sc->sc_rxflushlock);
  322. sc->sc_flags |= SC_OP_RXFLUSH;
  323. ath_rx_tasklet(sc, 1);
  324. sc->sc_flags &= ~SC_OP_RXFLUSH;
  325. spin_unlock_bh(&sc->sc_rxflushlock);
  326. }
  327. /* Process receive queue, as well as LED, etc. */
  328. int ath_rx_tasklet(struct ath_softc *sc, int flush)
  329. {
  330. #define PA2DESC(_sc, _pa) \
  331. ((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
  332. ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
  333. struct ath_buf *bf, *bf_held = NULL;
  334. struct ath_desc *ds;
  335. struct ieee80211_hdr *hdr;
  336. struct sk_buff *skb = NULL;
  337. struct ath_recv_status rx_status;
  338. struct ath_hal *ah = sc->sc_ah;
  339. int type, rx_processed = 0;
  340. u32 phyerr;
  341. u8 chainreset = 0;
  342. int retval;
  343. __le16 fc;
  344. do {
  345. /* If handling rx interrupt and flush is in progress => exit */
  346. if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
  347. break;
  348. spin_lock_bh(&sc->sc_rxbuflock);
  349. if (list_empty(&sc->sc_rxbuf)) {
  350. sc->sc_rxlink = NULL;
  351. spin_unlock_bh(&sc->sc_rxbuflock);
  352. break;
  353. }
  354. bf = list_first_entry(&sc->sc_rxbuf, struct ath_buf, list);
  355. /*
  356. * There is a race condition that BH gets scheduled after sw
  357. * writes RxE and before hw re-load the last descriptor to get
  358. * the newly chained one. Software must keep the last DONE
  359. * descriptor as a holding descriptor - software does so by
  360. * marking it with the STALE flag.
  361. */
  362. if (bf->bf_status & ATH_BUFSTATUS_STALE) {
  363. bf_held = bf;
  364. if (list_is_last(&bf_held->list, &sc->sc_rxbuf)) {
  365. /*
  366. * The holding descriptor is the last
  367. * descriptor in queue. It's safe to
  368. * remove the last holding descriptor
  369. * in BH context.
  370. */
  371. list_del(&bf_held->list);
  372. bf_held->bf_status &= ~ATH_BUFSTATUS_STALE;
  373. sc->sc_rxlink = NULL;
  374. if (bf_held->bf_status & ATH_BUFSTATUS_FREE) {
  375. list_add_tail(&bf_held->list,
  376. &sc->sc_rxbuf);
  377. ath_rx_buf_link(sc, bf_held);
  378. }
  379. spin_unlock_bh(&sc->sc_rxbuflock);
  380. break;
  381. }
  382. bf = list_entry(bf->list.next, struct ath_buf, list);
  383. }
  384. ds = bf->bf_desc;
  385. ++rx_processed;
  386. /*
  387. * Must provide the virtual address of the current
  388. * descriptor, the physical address, and the virtual
  389. * address of the next descriptor in the h/w chain.
  390. * This allows the HAL to look ahead to see if the
  391. * hardware is done with a descriptor by checking the
  392. * done bit in the following descriptor and the address
  393. * of the current descriptor the DMA engine is working
  394. * on. All this is necessary because of our use of
  395. * a self-linked list to avoid rx overruns.
  396. */
  397. retval = ath9k_hw_rxprocdesc(ah,
  398. ds,
  399. bf->bf_daddr,
  400. PA2DESC(sc, ds->ds_link),
  401. 0);
  402. if (retval == -EINPROGRESS) {
  403. struct ath_buf *tbf;
  404. struct ath_desc *tds;
  405. if (list_is_last(&bf->list, &sc->sc_rxbuf)) {
  406. spin_unlock_bh(&sc->sc_rxbuflock);
  407. break;
  408. }
  409. tbf = list_entry(bf->list.next, struct ath_buf, list);
  410. /*
  411. * On some hardware the descriptor status words could
  412. * get corrupted, including the done bit. Because of
  413. * this, check if the next descriptor's done bit is
  414. * set or not.
  415. *
  416. * If the next descriptor's done bit is set, the current
  417. * descriptor has been corrupted. Force s/w to discard
  418. * this descriptor and continue...
  419. */
  420. tds = tbf->bf_desc;
  421. retval = ath9k_hw_rxprocdesc(ah,
  422. tds, tbf->bf_daddr,
  423. PA2DESC(sc, tds->ds_link), 0);
  424. if (retval == -EINPROGRESS) {
  425. spin_unlock_bh(&sc->sc_rxbuflock);
  426. break;
  427. }
  428. }
  429. /* XXX: we do not support frames spanning
  430. * multiple descriptors */
  431. bf->bf_status |= ATH_BUFSTATUS_DONE;
  432. skb = bf->bf_mpdu;
  433. if (skb == NULL) { /* XXX ??? can this happen */
  434. spin_unlock_bh(&sc->sc_rxbuflock);
  435. continue;
  436. }
  437. /*
  438. * Now we know it's a completed frame, we can indicate the
  439. * frame. Remove the previous holding descriptor and leave
  440. * this one in the queue as the new holding descriptor.
  441. */
  442. if (bf_held) {
  443. list_del(&bf_held->list);
  444. bf_held->bf_status &= ~ATH_BUFSTATUS_STALE;
  445. if (bf_held->bf_status & ATH_BUFSTATUS_FREE) {
  446. list_add_tail(&bf_held->list, &sc->sc_rxbuf);
  447. /* try to requeue this descriptor */
  448. ath_rx_buf_link(sc, bf_held);
  449. }
  450. }
  451. bf->bf_status |= ATH_BUFSTATUS_STALE;
  452. bf_held = bf;
  453. /*
  454. * Release the lock here in case ieee80211_input() return
  455. * the frame immediately by calling ath_rx_mpdu_requeue().
  456. */
  457. spin_unlock_bh(&sc->sc_rxbuflock);
  458. if (flush) {
  459. /*
  460. * If we're asked to flush receive queue, directly
  461. * chain it back at the queue without processing it.
  462. */
  463. goto rx_next;
  464. }
  465. hdr = (struct ieee80211_hdr *)skb->data;
  466. fc = hdr->frame_control;
  467. memset(&rx_status, 0, sizeof(struct ath_recv_status));
  468. if (ds->ds_rxstat.rs_more) {
  469. /*
  470. * Frame spans multiple descriptors; this
  471. * cannot happen yet as we don't support
  472. * jumbograms. If not in monitor mode,
  473. * discard the frame.
  474. */
  475. #ifndef ERROR_FRAMES
  476. /*
  477. * Enable this if you want to see
  478. * error frames in Monitor mode.
  479. */
  480. if (sc->sc_ah->ah_opmode != ATH9K_M_MONITOR)
  481. goto rx_next;
  482. #endif
  483. /* fall thru for monitor mode handling... */
  484. } else if (ds->ds_rxstat.rs_status != 0) {
  485. if (ds->ds_rxstat.rs_status & ATH9K_RXERR_CRC)
  486. rx_status.flags |= ATH_RX_FCS_ERROR;
  487. if (ds->ds_rxstat.rs_status & ATH9K_RXERR_PHY) {
  488. phyerr = ds->ds_rxstat.rs_phyerr & 0x1f;
  489. goto rx_next;
  490. }
  491. if (ds->ds_rxstat.rs_status & ATH9K_RXERR_DECRYPT) {
  492. /*
  493. * Decrypt error. We only mark packet status
  494. * here and always push up the frame up to let
  495. * mac80211 handle the actual error case, be
  496. * it no decryption key or real decryption
  497. * error. This let us keep statistics there.
  498. */
  499. rx_status.flags |= ATH_RX_DECRYPT_ERROR;
  500. } else if (ds->ds_rxstat.rs_status & ATH9K_RXERR_MIC) {
  501. /*
  502. * Demic error. We only mark frame status here
  503. * and always push up the frame up to let
  504. * mac80211 handle the actual error case. This
  505. * let us keep statistics there. Hardware may
  506. * post a false-positive MIC error.
  507. */
  508. if (ieee80211_is_ctl(fc))
  509. /*
  510. * Sometimes, we get invalid
  511. * MIC failures on valid control frames.
  512. * Remove these mic errors.
  513. */
  514. ds->ds_rxstat.rs_status &=
  515. ~ATH9K_RXERR_MIC;
  516. else
  517. rx_status.flags |= ATH_RX_MIC_ERROR;
  518. }
  519. /*
  520. * Reject error frames with the exception of
  521. * decryption and MIC failures. For monitor mode,
  522. * we also ignore the CRC error.
  523. */
  524. if (sc->sc_ah->ah_opmode == ATH9K_M_MONITOR) {
  525. if (ds->ds_rxstat.rs_status &
  526. ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
  527. ATH9K_RXERR_CRC))
  528. goto rx_next;
  529. } else {
  530. if (ds->ds_rxstat.rs_status &
  531. ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
  532. goto rx_next;
  533. }
  534. }
  535. }
  536. /*
  537. * The status portion of the descriptor could get corrupted.
  538. */
  539. if (sc->sc_rxbufsize < ds->ds_rxstat.rs_datalen)
  540. goto rx_next;
  541. /*
  542. * Sync and unmap the frame. At this point we're
  543. * committed to passing the sk_buff somewhere so
  544. * clear buf_skb; this means a new sk_buff must be
  545. * allocated when the rx descriptor is setup again
  546. * to receive another frame.
  547. */
  548. skb_put(skb, ds->ds_rxstat.rs_datalen);
  549. skb->protocol = cpu_to_be16(ETH_P_CONTROL);
  550. rx_status.tsf = ath_extend_tsf(sc, ds->ds_rxstat.rs_tstamp);
  551. rx_status.rateieee =
  552. sc->sc_hwmap[ds->ds_rxstat.rs_rate].ieeerate;
  553. rx_status.rateKbps =
  554. sc->sc_hwmap[ds->ds_rxstat.rs_rate].rateKbps;
  555. rx_status.ratecode = ds->ds_rxstat.rs_rate;
  556. /* HT rate */
  557. if (rx_status.ratecode & 0x80) {
  558. /* TODO - add table to avoid division */
  559. if (ds->ds_rxstat.rs_flags & ATH9K_RX_2040) {
  560. rx_status.flags |= ATH_RX_40MHZ;
  561. rx_status.rateKbps =
  562. (rx_status.rateKbps * 27) / 13;
  563. }
  564. if (ds->ds_rxstat.rs_flags & ATH9K_RX_GI)
  565. rx_status.rateKbps =
  566. (rx_status.rateKbps * 10) / 9;
  567. else
  568. rx_status.flags |= ATH_RX_SHORT_GI;
  569. }
  570. /* sc_noise_floor is only available when the station
  571. attaches to an AP, so we use a default value
  572. if we are not yet attached. */
  573. rx_status.abs_rssi =
  574. ds->ds_rxstat.rs_rssi + sc->sc_ani.sc_noise_floor;
  575. pci_dma_sync_single_for_cpu(sc->pdev,
  576. bf->bf_buf_addr,
  577. skb_tailroom(skb),
  578. PCI_DMA_FROMDEVICE);
  579. pci_unmap_single(sc->pdev,
  580. bf->bf_buf_addr,
  581. sc->sc_rxbufsize,
  582. PCI_DMA_FROMDEVICE);
  583. /* XXX: Ah! make me more readable, use a helper */
  584. if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) {
  585. if (ds->ds_rxstat.rs_moreaggr == 0) {
  586. rx_status.rssictl[0] =
  587. ds->ds_rxstat.rs_rssi_ctl0;
  588. rx_status.rssictl[1] =
  589. ds->ds_rxstat.rs_rssi_ctl1;
  590. rx_status.rssictl[2] =
  591. ds->ds_rxstat.rs_rssi_ctl2;
  592. rx_status.rssi = ds->ds_rxstat.rs_rssi;
  593. if (ds->ds_rxstat.rs_flags & ATH9K_RX_2040) {
  594. rx_status.rssiextn[0] =
  595. ds->ds_rxstat.rs_rssi_ext0;
  596. rx_status.rssiextn[1] =
  597. ds->ds_rxstat.rs_rssi_ext1;
  598. rx_status.rssiextn[2] =
  599. ds->ds_rxstat.rs_rssi_ext2;
  600. rx_status.flags |=
  601. ATH_RX_RSSI_EXTN_VALID;
  602. }
  603. rx_status.flags |= ATH_RX_RSSI_VALID |
  604. ATH_RX_CHAIN_RSSI_VALID;
  605. }
  606. } else {
  607. /*
  608. * Need to insert the "combined" rssi into the
  609. * status structure for upper layer processing
  610. */
  611. rx_status.rssi = ds->ds_rxstat.rs_rssi;
  612. rx_status.flags |= ATH_RX_RSSI_VALID;
  613. }
  614. /* Pass frames up to the stack. */
  615. type = ath_rx_indicate(sc, skb,
  616. &rx_status, ds->ds_rxstat.rs_keyix);
  617. /*
  618. * change the default rx antenna if rx diversity chooses the
  619. * other antenna 3 times in a row.
  620. */
  621. if (sc->sc_defant != ds->ds_rxstat.rs_antenna) {
  622. if (++sc->sc_rxotherant >= 3)
  623. ath_setdefantenna(sc,
  624. ds->ds_rxstat.rs_antenna);
  625. } else {
  626. sc->sc_rxotherant = 0;
  627. }
  628. #ifdef CONFIG_SLOW_ANT_DIV
  629. if ((rx_status.flags & ATH_RX_RSSI_VALID) &&
  630. ieee80211_is_beacon(fc)) {
  631. ath_slow_ant_div(&sc->sc_antdiv, hdr, &ds->ds_rxstat);
  632. }
  633. #endif
  634. /*
  635. * For frames successfully indicated, the buffer will be
  636. * returned to us by upper layers by calling
  637. * ath_rx_mpdu_requeue, either synchronusly or asynchronously.
  638. * So we don't want to do it here in this loop.
  639. */
  640. continue;
  641. rx_next:
  642. bf->bf_status |= ATH_BUFSTATUS_FREE;
  643. } while (TRUE);
  644. if (chainreset) {
  645. DPRINTF(sc, ATH_DBG_CONFIG,
  646. "%s: Reset rx chain mask. "
  647. "Do internal reset\n", __func__);
  648. ASSERT(flush == 0);
  649. ath_reset(sc, false);
  650. }
  651. return 0;
  652. #undef PA2DESC
  653. }