recv.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. * Copyright (c) 2008-2011 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 <linux/dma-mapping.h>
  17. #include "ath9k.h"
  18. #include "ar9003_mac.h"
  19. #define SKB_CB_ATHBUF(__skb) (*((struct ath_rxbuf **)__skb->cb))
  20. static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
  21. {
  22. return sc->ps_enabled &&
  23. (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
  24. }
  25. /*
  26. * Setup and link descriptors.
  27. *
  28. * 11N: we can no longer afford to self link the last descriptor.
  29. * MAC acknowledges BA status as long as it copies frames to host
  30. * buffer (or rx fifo). This can incorrectly acknowledge packets
  31. * to a sender if last desc is self-linked.
  32. */
  33. static void ath_rx_buf_link(struct ath_softc *sc, struct ath_rxbuf *bf,
  34. bool flush)
  35. {
  36. struct ath_hw *ah = sc->sc_ah;
  37. struct ath_common *common = ath9k_hw_common(ah);
  38. struct ath_desc *ds;
  39. struct sk_buff *skb;
  40. ds = bf->bf_desc;
  41. ds->ds_link = 0; /* link to null */
  42. ds->ds_data = bf->bf_buf_addr;
  43. /* virtual addr of the beginning of the buffer. */
  44. skb = bf->bf_mpdu;
  45. BUG_ON(skb == NULL);
  46. ds->ds_vdata = skb->data;
  47. /*
  48. * setup rx descriptors. The rx_bufsize here tells the hardware
  49. * how much data it can DMA to us and that we are prepared
  50. * to process
  51. */
  52. ath9k_hw_setuprxdesc(ah, ds,
  53. common->rx_bufsize,
  54. 0);
  55. if (sc->rx.rxlink)
  56. *sc->rx.rxlink = bf->bf_daddr;
  57. else if (!flush)
  58. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  59. sc->rx.rxlink = &ds->ds_link;
  60. }
  61. static void ath_rx_buf_relink(struct ath_softc *sc, struct ath_rxbuf *bf,
  62. bool flush)
  63. {
  64. if (sc->rx.buf_hold)
  65. ath_rx_buf_link(sc, sc->rx.buf_hold, flush);
  66. sc->rx.buf_hold = bf;
  67. }
  68. static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
  69. {
  70. /* XXX block beacon interrupts */
  71. ath9k_hw_setantenna(sc->sc_ah, antenna);
  72. sc->rx.defant = antenna;
  73. sc->rx.rxotherant = 0;
  74. }
  75. static void ath_opmode_init(struct ath_softc *sc)
  76. {
  77. struct ath_hw *ah = sc->sc_ah;
  78. struct ath_common *common = ath9k_hw_common(ah);
  79. u32 rfilt, mfilt[2];
  80. /* configure rx filter */
  81. rfilt = ath_calcrxfilter(sc);
  82. ath9k_hw_setrxfilter(ah, rfilt);
  83. /* configure bssid mask */
  84. ath_hw_setbssidmask(common);
  85. /* configure operational mode */
  86. ath9k_hw_setopmode(ah);
  87. /* calculate and install multicast filter */
  88. mfilt[0] = mfilt[1] = ~0;
  89. ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
  90. }
  91. static bool ath_rx_edma_buf_link(struct ath_softc *sc,
  92. enum ath9k_rx_qtype qtype)
  93. {
  94. struct ath_hw *ah = sc->sc_ah;
  95. struct ath_rx_edma *rx_edma;
  96. struct sk_buff *skb;
  97. struct ath_rxbuf *bf;
  98. rx_edma = &sc->rx.rx_edma[qtype];
  99. if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
  100. return false;
  101. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  102. list_del_init(&bf->list);
  103. skb = bf->bf_mpdu;
  104. memset(skb->data, 0, ah->caps.rx_status_len);
  105. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  106. ah->caps.rx_status_len, DMA_TO_DEVICE);
  107. SKB_CB_ATHBUF(skb) = bf;
  108. ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
  109. __skb_queue_tail(&rx_edma->rx_fifo, skb);
  110. return true;
  111. }
  112. static void ath_rx_addbuffer_edma(struct ath_softc *sc,
  113. enum ath9k_rx_qtype qtype)
  114. {
  115. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  116. struct ath_rxbuf *bf, *tbf;
  117. if (list_empty(&sc->rx.rxbuf)) {
  118. ath_dbg(common, QUEUE, "No free rx buf available\n");
  119. return;
  120. }
  121. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
  122. if (!ath_rx_edma_buf_link(sc, qtype))
  123. break;
  124. }
  125. static void ath_rx_remove_buffer(struct ath_softc *sc,
  126. enum ath9k_rx_qtype qtype)
  127. {
  128. struct ath_rxbuf *bf;
  129. struct ath_rx_edma *rx_edma;
  130. struct sk_buff *skb;
  131. rx_edma = &sc->rx.rx_edma[qtype];
  132. while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
  133. bf = SKB_CB_ATHBUF(skb);
  134. BUG_ON(!bf);
  135. list_add_tail(&bf->list, &sc->rx.rxbuf);
  136. }
  137. }
  138. static void ath_rx_edma_cleanup(struct ath_softc *sc)
  139. {
  140. struct ath_hw *ah = sc->sc_ah;
  141. struct ath_common *common = ath9k_hw_common(ah);
  142. struct ath_rxbuf *bf;
  143. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  144. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  145. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  146. if (bf->bf_mpdu) {
  147. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  148. common->rx_bufsize,
  149. DMA_BIDIRECTIONAL);
  150. dev_kfree_skb_any(bf->bf_mpdu);
  151. bf->bf_buf_addr = 0;
  152. bf->bf_mpdu = NULL;
  153. }
  154. }
  155. }
  156. static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
  157. {
  158. __skb_queue_head_init(&rx_edma->rx_fifo);
  159. rx_edma->rx_fifo_hwsize = size;
  160. }
  161. static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
  162. {
  163. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  164. struct ath_hw *ah = sc->sc_ah;
  165. struct sk_buff *skb;
  166. struct ath_rxbuf *bf;
  167. int error = 0, i;
  168. u32 size;
  169. ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
  170. ah->caps.rx_status_len);
  171. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
  172. ah->caps.rx_lp_qdepth);
  173. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
  174. ah->caps.rx_hp_qdepth);
  175. size = sizeof(struct ath_rxbuf) * nbufs;
  176. bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
  177. if (!bf)
  178. return -ENOMEM;
  179. INIT_LIST_HEAD(&sc->rx.rxbuf);
  180. for (i = 0; i < nbufs; i++, bf++) {
  181. skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
  182. if (!skb) {
  183. error = -ENOMEM;
  184. goto rx_init_fail;
  185. }
  186. memset(skb->data, 0, common->rx_bufsize);
  187. bf->bf_mpdu = skb;
  188. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  189. common->rx_bufsize,
  190. DMA_BIDIRECTIONAL);
  191. if (unlikely(dma_mapping_error(sc->dev,
  192. bf->bf_buf_addr))) {
  193. dev_kfree_skb_any(skb);
  194. bf->bf_mpdu = NULL;
  195. bf->bf_buf_addr = 0;
  196. ath_err(common,
  197. "dma_mapping_error() on RX init\n");
  198. error = -ENOMEM;
  199. goto rx_init_fail;
  200. }
  201. list_add_tail(&bf->list, &sc->rx.rxbuf);
  202. }
  203. return 0;
  204. rx_init_fail:
  205. ath_rx_edma_cleanup(sc);
  206. return error;
  207. }
  208. static void ath_edma_start_recv(struct ath_softc *sc)
  209. {
  210. ath9k_hw_rxena(sc->sc_ah);
  211. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP);
  212. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP);
  213. ath_opmode_init(sc);
  214. ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
  215. }
  216. static void ath_edma_stop_recv(struct ath_softc *sc)
  217. {
  218. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  219. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  220. }
  221. int ath_rx_init(struct ath_softc *sc, int nbufs)
  222. {
  223. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  224. struct sk_buff *skb;
  225. struct ath_rxbuf *bf;
  226. int error = 0;
  227. spin_lock_init(&sc->sc_pcu_lock);
  228. common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
  229. sc->sc_ah->caps.rx_status_len;
  230. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  231. return ath_rx_edma_init(sc, nbufs);
  232. ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
  233. common->cachelsz, common->rx_bufsize);
  234. /* Initialize rx descriptors */
  235. error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
  236. "rx", nbufs, 1, 0);
  237. if (error != 0) {
  238. ath_err(common,
  239. "failed to allocate rx descriptors: %d\n",
  240. error);
  241. goto err;
  242. }
  243. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  244. skb = ath_rxbuf_alloc(common, common->rx_bufsize,
  245. GFP_KERNEL);
  246. if (skb == NULL) {
  247. error = -ENOMEM;
  248. goto err;
  249. }
  250. bf->bf_mpdu = skb;
  251. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  252. common->rx_bufsize,
  253. DMA_FROM_DEVICE);
  254. if (unlikely(dma_mapping_error(sc->dev,
  255. bf->bf_buf_addr))) {
  256. dev_kfree_skb_any(skb);
  257. bf->bf_mpdu = NULL;
  258. bf->bf_buf_addr = 0;
  259. ath_err(common,
  260. "dma_mapping_error() on RX init\n");
  261. error = -ENOMEM;
  262. goto err;
  263. }
  264. }
  265. sc->rx.rxlink = NULL;
  266. err:
  267. if (error)
  268. ath_rx_cleanup(sc);
  269. return error;
  270. }
  271. void ath_rx_cleanup(struct ath_softc *sc)
  272. {
  273. struct ath_hw *ah = sc->sc_ah;
  274. struct ath_common *common = ath9k_hw_common(ah);
  275. struct sk_buff *skb;
  276. struct ath_rxbuf *bf;
  277. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  278. ath_rx_edma_cleanup(sc);
  279. return;
  280. }
  281. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  282. skb = bf->bf_mpdu;
  283. if (skb) {
  284. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  285. common->rx_bufsize,
  286. DMA_FROM_DEVICE);
  287. dev_kfree_skb(skb);
  288. bf->bf_buf_addr = 0;
  289. bf->bf_mpdu = NULL;
  290. }
  291. }
  292. }
  293. /*
  294. * Calculate the receive filter according to the
  295. * operating mode and state:
  296. *
  297. * o always accept unicast, broadcast, and multicast traffic
  298. * o maintain current state of phy error reception (the hal
  299. * may enable phy error frames for noise immunity work)
  300. * o probe request frames are accepted only when operating in
  301. * hostap, adhoc, or monitor modes
  302. * o enable promiscuous mode according to the interface state
  303. * o accept beacons:
  304. * - when operating in adhoc mode so the 802.11 layer creates
  305. * node table entries for peers,
  306. * - when operating in station mode for collecting rssi data when
  307. * the station is otherwise quiet, or
  308. * - when operating as a repeater so we see repeater-sta beacons
  309. * - when scanning
  310. */
  311. u32 ath_calcrxfilter(struct ath_softc *sc)
  312. {
  313. u32 rfilt;
  314. if (config_enabled(CONFIG_ATH9K_TX99))
  315. return 0;
  316. rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
  317. | ATH9K_RX_FILTER_MCAST;
  318. /* if operating on a DFS channel, enable radar pulse detection */
  319. if (sc->hw->conf.radar_enabled)
  320. rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
  321. if (sc->rx.rxfilter & FIF_PROBE_REQ)
  322. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  323. /*
  324. * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
  325. * mode interface or when in monitor mode. AP mode does not need this
  326. * since it receives all in-BSS frames anyway.
  327. */
  328. if (sc->sc_ah->is_monitoring)
  329. rfilt |= ATH9K_RX_FILTER_PROM;
  330. if (sc->rx.rxfilter & FIF_CONTROL)
  331. rfilt |= ATH9K_RX_FILTER_CONTROL;
  332. if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
  333. (sc->nvifs <= 1) &&
  334. !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
  335. rfilt |= ATH9K_RX_FILTER_MYBEACON;
  336. else
  337. rfilt |= ATH9K_RX_FILTER_BEACON;
  338. if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
  339. (sc->rx.rxfilter & FIF_PSPOLL))
  340. rfilt |= ATH9K_RX_FILTER_PSPOLL;
  341. if (conf_is_ht(&sc->hw->conf))
  342. rfilt |= ATH9K_RX_FILTER_COMP_BAR;
  343. if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
  344. /* This is needed for older chips */
  345. if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
  346. rfilt |= ATH9K_RX_FILTER_PROM;
  347. rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
  348. }
  349. if (AR_SREV_9550(sc->sc_ah) || AR_SREV_9531(sc->sc_ah))
  350. rfilt |= ATH9K_RX_FILTER_4ADDRESS;
  351. return rfilt;
  352. }
  353. int ath_startrecv(struct ath_softc *sc)
  354. {
  355. struct ath_hw *ah = sc->sc_ah;
  356. struct ath_rxbuf *bf, *tbf;
  357. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  358. ath_edma_start_recv(sc);
  359. return 0;
  360. }
  361. if (list_empty(&sc->rx.rxbuf))
  362. goto start_recv;
  363. sc->rx.buf_hold = NULL;
  364. sc->rx.rxlink = NULL;
  365. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
  366. ath_rx_buf_link(sc, bf, false);
  367. }
  368. /* We could have deleted elements so the list may be empty now */
  369. if (list_empty(&sc->rx.rxbuf))
  370. goto start_recv;
  371. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  372. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  373. ath9k_hw_rxena(ah);
  374. start_recv:
  375. ath_opmode_init(sc);
  376. ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
  377. return 0;
  378. }
  379. static void ath_flushrecv(struct ath_softc *sc)
  380. {
  381. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  382. ath_rx_tasklet(sc, 1, true);
  383. ath_rx_tasklet(sc, 1, false);
  384. }
  385. bool ath_stoprecv(struct ath_softc *sc)
  386. {
  387. struct ath_hw *ah = sc->sc_ah;
  388. bool stopped, reset = false;
  389. ath9k_hw_abortpcurecv(ah);
  390. ath9k_hw_setrxfilter(ah, 0);
  391. stopped = ath9k_hw_stopdmarecv(ah, &reset);
  392. ath_flushrecv(sc);
  393. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  394. ath_edma_stop_recv(sc);
  395. else
  396. sc->rx.rxlink = NULL;
  397. if (!(ah->ah_flags & AH_UNPLUGGED) &&
  398. unlikely(!stopped)) {
  399. ath_err(ath9k_hw_common(sc->sc_ah),
  400. "Could not stop RX, we could be "
  401. "confusing the DMA engine when we start RX up\n");
  402. ATH_DBG_WARN_ON_ONCE(!stopped);
  403. }
  404. return stopped && !reset;
  405. }
  406. static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
  407. {
  408. /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
  409. struct ieee80211_mgmt *mgmt;
  410. u8 *pos, *end, id, elen;
  411. struct ieee80211_tim_ie *tim;
  412. mgmt = (struct ieee80211_mgmt *)skb->data;
  413. pos = mgmt->u.beacon.variable;
  414. end = skb->data + skb->len;
  415. while (pos + 2 < end) {
  416. id = *pos++;
  417. elen = *pos++;
  418. if (pos + elen > end)
  419. break;
  420. if (id == WLAN_EID_TIM) {
  421. if (elen < sizeof(*tim))
  422. break;
  423. tim = (struct ieee80211_tim_ie *) pos;
  424. if (tim->dtim_count != 0)
  425. break;
  426. return tim->bitmap_ctrl & 0x01;
  427. }
  428. pos += elen;
  429. }
  430. return false;
  431. }
  432. static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
  433. {
  434. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  435. if (skb->len < 24 + 8 + 2 + 2)
  436. return;
  437. sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
  438. if (sc->ps_flags & PS_BEACON_SYNC) {
  439. sc->ps_flags &= ~PS_BEACON_SYNC;
  440. ath_dbg(common, PS,
  441. "Reconfigure beacon timers based on synchronized timestamp\n");
  442. if (!(WARN_ON_ONCE(sc->cur_beacon_conf.beacon_interval == 0)))
  443. ath9k_set_beacon(sc);
  444. if (sc->p2p_ps_vif)
  445. ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
  446. }
  447. if (ath_beacon_dtim_pending_cab(skb)) {
  448. /*
  449. * Remain awake waiting for buffered broadcast/multicast
  450. * frames. If the last broadcast/multicast frame is not
  451. * received properly, the next beacon frame will work as
  452. * a backup trigger for returning into NETWORK SLEEP state,
  453. * so we are waiting for it as well.
  454. */
  455. ath_dbg(common, PS,
  456. "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
  457. sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
  458. return;
  459. }
  460. if (sc->ps_flags & PS_WAIT_FOR_CAB) {
  461. /*
  462. * This can happen if a broadcast frame is dropped or the AP
  463. * fails to send a frame indicating that all CAB frames have
  464. * been delivered.
  465. */
  466. sc->ps_flags &= ~PS_WAIT_FOR_CAB;
  467. ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
  468. }
  469. }
  470. static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
  471. {
  472. struct ieee80211_hdr *hdr;
  473. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  474. hdr = (struct ieee80211_hdr *)skb->data;
  475. /* Process Beacon and CAB receive in PS state */
  476. if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
  477. && mybeacon) {
  478. ath_rx_ps_beacon(sc, skb);
  479. } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
  480. (ieee80211_is_data(hdr->frame_control) ||
  481. ieee80211_is_action(hdr->frame_control)) &&
  482. is_multicast_ether_addr(hdr->addr1) &&
  483. !ieee80211_has_moredata(hdr->frame_control)) {
  484. /*
  485. * No more broadcast/multicast frames to be received at this
  486. * point.
  487. */
  488. sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
  489. ath_dbg(common, PS,
  490. "All PS CAB frames received, back to sleep\n");
  491. } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
  492. !is_multicast_ether_addr(hdr->addr1) &&
  493. !ieee80211_has_morefrags(hdr->frame_control)) {
  494. sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
  495. ath_dbg(common, PS,
  496. "Going back to sleep after having received PS-Poll data (0x%lx)\n",
  497. sc->ps_flags & (PS_WAIT_FOR_BEACON |
  498. PS_WAIT_FOR_CAB |
  499. PS_WAIT_FOR_PSPOLL_DATA |
  500. PS_WAIT_FOR_TX_ACK));
  501. }
  502. }
  503. static bool ath_edma_get_buffers(struct ath_softc *sc,
  504. enum ath9k_rx_qtype qtype,
  505. struct ath_rx_status *rs,
  506. struct ath_rxbuf **dest)
  507. {
  508. struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
  509. struct ath_hw *ah = sc->sc_ah;
  510. struct ath_common *common = ath9k_hw_common(ah);
  511. struct sk_buff *skb;
  512. struct ath_rxbuf *bf;
  513. int ret;
  514. skb = skb_peek(&rx_edma->rx_fifo);
  515. if (!skb)
  516. return false;
  517. bf = SKB_CB_ATHBUF(skb);
  518. BUG_ON(!bf);
  519. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  520. common->rx_bufsize, DMA_FROM_DEVICE);
  521. ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
  522. if (ret == -EINPROGRESS) {
  523. /*let device gain the buffer again*/
  524. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  525. common->rx_bufsize, DMA_FROM_DEVICE);
  526. return false;
  527. }
  528. __skb_unlink(skb, &rx_edma->rx_fifo);
  529. if (ret == -EINVAL) {
  530. /* corrupt descriptor, skip this one and the following one */
  531. list_add_tail(&bf->list, &sc->rx.rxbuf);
  532. ath_rx_edma_buf_link(sc, qtype);
  533. skb = skb_peek(&rx_edma->rx_fifo);
  534. if (skb) {
  535. bf = SKB_CB_ATHBUF(skb);
  536. BUG_ON(!bf);
  537. __skb_unlink(skb, &rx_edma->rx_fifo);
  538. list_add_tail(&bf->list, &sc->rx.rxbuf);
  539. ath_rx_edma_buf_link(sc, qtype);
  540. }
  541. bf = NULL;
  542. }
  543. *dest = bf;
  544. return true;
  545. }
  546. static struct ath_rxbuf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
  547. struct ath_rx_status *rs,
  548. enum ath9k_rx_qtype qtype)
  549. {
  550. struct ath_rxbuf *bf = NULL;
  551. while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
  552. if (!bf)
  553. continue;
  554. return bf;
  555. }
  556. return NULL;
  557. }
  558. static struct ath_rxbuf *ath_get_next_rx_buf(struct ath_softc *sc,
  559. struct ath_rx_status *rs)
  560. {
  561. struct ath_hw *ah = sc->sc_ah;
  562. struct ath_common *common = ath9k_hw_common(ah);
  563. struct ath_desc *ds;
  564. struct ath_rxbuf *bf;
  565. int ret;
  566. if (list_empty(&sc->rx.rxbuf)) {
  567. sc->rx.rxlink = NULL;
  568. return NULL;
  569. }
  570. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  571. if (bf == sc->rx.buf_hold)
  572. return NULL;
  573. ds = bf->bf_desc;
  574. /*
  575. * Must provide the virtual address of the current
  576. * descriptor, the physical address, and the virtual
  577. * address of the next descriptor in the h/w chain.
  578. * This allows the HAL to look ahead to see if the
  579. * hardware is done with a descriptor by checking the
  580. * done bit in the following descriptor and the address
  581. * of the current descriptor the DMA engine is working
  582. * on. All this is necessary because of our use of
  583. * a self-linked list to avoid rx overruns.
  584. */
  585. ret = ath9k_hw_rxprocdesc(ah, ds, rs);
  586. if (ret == -EINPROGRESS) {
  587. struct ath_rx_status trs;
  588. struct ath_rxbuf *tbf;
  589. struct ath_desc *tds;
  590. memset(&trs, 0, sizeof(trs));
  591. if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
  592. sc->rx.rxlink = NULL;
  593. return NULL;
  594. }
  595. tbf = list_entry(bf->list.next, struct ath_rxbuf, list);
  596. /*
  597. * On some hardware the descriptor status words could
  598. * get corrupted, including the done bit. Because of
  599. * this, check if the next descriptor's done bit is
  600. * set or not.
  601. *
  602. * If the next descriptor's done bit is set, the current
  603. * descriptor has been corrupted. Force s/w to discard
  604. * this descriptor and continue...
  605. */
  606. tds = tbf->bf_desc;
  607. ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
  608. if (ret == -EINPROGRESS)
  609. return NULL;
  610. /*
  611. * Re-check previous descriptor, in case it has been filled
  612. * in the mean time.
  613. */
  614. ret = ath9k_hw_rxprocdesc(ah, ds, rs);
  615. if (ret == -EINPROGRESS) {
  616. /*
  617. * mark descriptor as zero-length and set the 'more'
  618. * flag to ensure that both buffers get discarded
  619. */
  620. rs->rs_datalen = 0;
  621. rs->rs_more = true;
  622. }
  623. }
  624. list_del(&bf->list);
  625. if (!bf->bf_mpdu)
  626. return bf;
  627. /*
  628. * Synchronize the DMA transfer with CPU before
  629. * 1. accessing the frame
  630. * 2. requeueing the same buffer to h/w
  631. */
  632. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  633. common->rx_bufsize,
  634. DMA_FROM_DEVICE);
  635. return bf;
  636. }
  637. static void ath9k_process_tsf(struct ath_rx_status *rs,
  638. struct ieee80211_rx_status *rxs,
  639. u64 tsf)
  640. {
  641. u32 tsf_lower = tsf & 0xffffffff;
  642. rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp;
  643. if (rs->rs_tstamp > tsf_lower &&
  644. unlikely(rs->rs_tstamp - tsf_lower > 0x10000000))
  645. rxs->mactime -= 0x100000000ULL;
  646. if (rs->rs_tstamp < tsf_lower &&
  647. unlikely(tsf_lower - rs->rs_tstamp > 0x10000000))
  648. rxs->mactime += 0x100000000ULL;
  649. }
  650. /*
  651. * For Decrypt or Demic errors, we only mark packet status here and always push
  652. * up the frame up to let mac80211 handle the actual error case, be it no
  653. * decryption key or real decryption error. This let us keep statistics there.
  654. */
  655. static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
  656. struct sk_buff *skb,
  657. struct ath_rx_status *rx_stats,
  658. struct ieee80211_rx_status *rx_status,
  659. bool *decrypt_error, u64 tsf)
  660. {
  661. struct ieee80211_hw *hw = sc->hw;
  662. struct ath_hw *ah = sc->sc_ah;
  663. struct ath_common *common = ath9k_hw_common(ah);
  664. struct ieee80211_hdr *hdr;
  665. bool discard_current = sc->rx.discard_next;
  666. /*
  667. * Discard corrupt descriptors which are marked in
  668. * ath_get_next_rx_buf().
  669. */
  670. if (discard_current)
  671. goto corrupt;
  672. sc->rx.discard_next = false;
  673. /*
  674. * Discard zero-length packets.
  675. */
  676. if (!rx_stats->rs_datalen) {
  677. RX_STAT_INC(rx_len_err);
  678. goto corrupt;
  679. }
  680. /*
  681. * rs_status follows rs_datalen so if rs_datalen is too large
  682. * we can take a hint that hardware corrupted it, so ignore
  683. * those frames.
  684. */
  685. if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) {
  686. RX_STAT_INC(rx_len_err);
  687. goto corrupt;
  688. }
  689. /* Only use status info from the last fragment */
  690. if (rx_stats->rs_more)
  691. return 0;
  692. /*
  693. * Return immediately if the RX descriptor has been marked
  694. * as corrupt based on the various error bits.
  695. *
  696. * This is different from the other corrupt descriptor
  697. * condition handled above.
  698. */
  699. if (rx_stats->rs_status & ATH9K_RXERR_CORRUPT_DESC)
  700. goto corrupt;
  701. hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len);
  702. ath9k_process_tsf(rx_stats, rx_status, tsf);
  703. ath_debug_stat_rx(sc, rx_stats);
  704. /*
  705. * Process PHY errors and return so that the packet
  706. * can be dropped.
  707. */
  708. if (rx_stats->rs_status & ATH9K_RXERR_PHY) {
  709. ath9k_dfs_process_phyerr(sc, hdr, rx_stats, rx_status->mactime);
  710. if (ath_process_fft(sc, hdr, rx_stats, rx_status->mactime))
  711. RX_STAT_INC(rx_spectral);
  712. return -EINVAL;
  713. }
  714. /*
  715. * everything but the rate is checked here, the rate check is done
  716. * separately to avoid doing two lookups for a rate for each frame.
  717. */
  718. if (!ath9k_cmn_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error, sc->rx.rxfilter))
  719. return -EINVAL;
  720. if (ath_is_mybeacon(common, hdr)) {
  721. RX_STAT_INC(rx_beacons);
  722. rx_stats->is_mybeacon = true;
  723. }
  724. /*
  725. * This shouldn't happen, but have a safety check anyway.
  726. */
  727. if (WARN_ON(!ah->curchan))
  728. return -EINVAL;
  729. if (ath9k_cmn_process_rate(common, hw, rx_stats, rx_status)) {
  730. /*
  731. * No valid hardware bitrate found -- we should not get here
  732. * because hardware has already validated this frame as OK.
  733. */
  734. ath_dbg(common, ANY, "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
  735. rx_stats->rs_rate);
  736. RX_STAT_INC(rx_rate_err);
  737. return -EINVAL;
  738. }
  739. ath9k_cmn_process_rssi(common, hw, rx_stats, rx_status);
  740. rx_status->band = ah->curchan->chan->band;
  741. rx_status->freq = ah->curchan->chan->center_freq;
  742. rx_status->antenna = rx_stats->rs_antenna;
  743. rx_status->flag |= RX_FLAG_MACTIME_END;
  744. #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
  745. if (ieee80211_is_data_present(hdr->frame_control) &&
  746. !ieee80211_is_qos_nullfunc(hdr->frame_control))
  747. sc->rx.num_pkts++;
  748. #endif
  749. return 0;
  750. corrupt:
  751. sc->rx.discard_next = rx_stats->rs_more;
  752. return -EINVAL;
  753. }
  754. /*
  755. * Run the LNA combining algorithm only in these cases:
  756. *
  757. * Standalone WLAN cards with both LNA/Antenna diversity
  758. * enabled in the EEPROM.
  759. *
  760. * WLAN+BT cards which are in the supported card list
  761. * in ath_pci_id_table and the user has loaded the
  762. * driver with "bt_ant_diversity" set to true.
  763. */
  764. static void ath9k_antenna_check(struct ath_softc *sc,
  765. struct ath_rx_status *rs)
  766. {
  767. struct ath_hw *ah = sc->sc_ah;
  768. struct ath9k_hw_capabilities *pCap = &ah->caps;
  769. struct ath_common *common = ath9k_hw_common(ah);
  770. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB))
  771. return;
  772. /*
  773. * Change the default rx antenna if rx diversity
  774. * chooses the other antenna 3 times in a row.
  775. */
  776. if (sc->rx.defant != rs->rs_antenna) {
  777. if (++sc->rx.rxotherant >= 3)
  778. ath_setdefantenna(sc, rs->rs_antenna);
  779. } else {
  780. sc->rx.rxotherant = 0;
  781. }
  782. if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) {
  783. if (common->bt_ant_diversity)
  784. ath_ant_comb_scan(sc, rs);
  785. } else {
  786. ath_ant_comb_scan(sc, rs);
  787. }
  788. }
  789. static void ath9k_apply_ampdu_details(struct ath_softc *sc,
  790. struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
  791. {
  792. if (rs->rs_isaggr) {
  793. rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
  794. rxs->ampdu_reference = sc->rx.ampdu_ref;
  795. if (!rs->rs_moreaggr) {
  796. rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
  797. sc->rx.ampdu_ref++;
  798. }
  799. if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
  800. rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
  801. }
  802. }
  803. int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
  804. {
  805. struct ath_rxbuf *bf;
  806. struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
  807. struct ieee80211_rx_status *rxs;
  808. struct ath_hw *ah = sc->sc_ah;
  809. struct ath_common *common = ath9k_hw_common(ah);
  810. struct ieee80211_hw *hw = sc->hw;
  811. int retval;
  812. struct ath_rx_status rs;
  813. enum ath9k_rx_qtype qtype;
  814. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  815. int dma_type;
  816. u64 tsf = 0;
  817. unsigned long flags;
  818. dma_addr_t new_buf_addr;
  819. unsigned int budget = 512;
  820. if (edma)
  821. dma_type = DMA_BIDIRECTIONAL;
  822. else
  823. dma_type = DMA_FROM_DEVICE;
  824. qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
  825. tsf = ath9k_hw_gettsf64(ah);
  826. do {
  827. bool decrypt_error = false;
  828. memset(&rs, 0, sizeof(rs));
  829. if (edma)
  830. bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
  831. else
  832. bf = ath_get_next_rx_buf(sc, &rs);
  833. if (!bf)
  834. break;
  835. skb = bf->bf_mpdu;
  836. if (!skb)
  837. continue;
  838. /*
  839. * Take frame header from the first fragment and RX status from
  840. * the last one.
  841. */
  842. if (sc->rx.frag)
  843. hdr_skb = sc->rx.frag;
  844. else
  845. hdr_skb = skb;
  846. rxs = IEEE80211_SKB_RXCB(hdr_skb);
  847. memset(rxs, 0, sizeof(struct ieee80211_rx_status));
  848. retval = ath9k_rx_skb_preprocess(sc, hdr_skb, &rs, rxs,
  849. &decrypt_error, tsf);
  850. if (retval)
  851. goto requeue_drop_frag;
  852. /* Ensure we always have an skb to requeue once we are done
  853. * processing the current buffer's skb */
  854. requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
  855. /* If there is no memory we ignore the current RX'd frame,
  856. * tell hardware it can give us a new frame using the old
  857. * skb and put it at the tail of the sc->rx.rxbuf list for
  858. * processing. */
  859. if (!requeue_skb) {
  860. RX_STAT_INC(rx_oom_err);
  861. goto requeue_drop_frag;
  862. }
  863. /* We will now give hardware our shiny new allocated skb */
  864. new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
  865. common->rx_bufsize, dma_type);
  866. if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
  867. dev_kfree_skb_any(requeue_skb);
  868. goto requeue_drop_frag;
  869. }
  870. /* Unmap the frame */
  871. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  872. common->rx_bufsize, dma_type);
  873. bf->bf_mpdu = requeue_skb;
  874. bf->bf_buf_addr = new_buf_addr;
  875. skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
  876. if (ah->caps.rx_status_len)
  877. skb_pull(skb, ah->caps.rx_status_len);
  878. if (!rs.rs_more)
  879. ath9k_cmn_rx_skb_postprocess(common, hdr_skb, &rs,
  880. rxs, decrypt_error);
  881. if (rs.rs_more) {
  882. RX_STAT_INC(rx_frags);
  883. /*
  884. * rs_more indicates chained descriptors which can be
  885. * used to link buffers together for a sort of
  886. * scatter-gather operation.
  887. */
  888. if (sc->rx.frag) {
  889. /* too many fragments - cannot handle frame */
  890. dev_kfree_skb_any(sc->rx.frag);
  891. dev_kfree_skb_any(skb);
  892. RX_STAT_INC(rx_too_many_frags_err);
  893. skb = NULL;
  894. }
  895. sc->rx.frag = skb;
  896. goto requeue;
  897. }
  898. if (sc->rx.frag) {
  899. int space = skb->len - skb_tailroom(hdr_skb);
  900. if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
  901. dev_kfree_skb(skb);
  902. RX_STAT_INC(rx_oom_err);
  903. goto requeue_drop_frag;
  904. }
  905. sc->rx.frag = NULL;
  906. skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
  907. skb->len);
  908. dev_kfree_skb_any(skb);
  909. skb = hdr_skb;
  910. }
  911. if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
  912. skb_trim(skb, skb->len - 8);
  913. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  914. if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
  915. PS_WAIT_FOR_CAB |
  916. PS_WAIT_FOR_PSPOLL_DATA)) ||
  917. ath9k_check_auto_sleep(sc))
  918. ath_rx_ps(sc, skb, rs.is_mybeacon);
  919. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  920. ath9k_antenna_check(sc, &rs);
  921. ath9k_apply_ampdu_details(sc, &rs, rxs);
  922. ath_debug_rate_stats(sc, &rs, skb);
  923. ieee80211_rx(hw, skb);
  924. requeue_drop_frag:
  925. if (sc->rx.frag) {
  926. dev_kfree_skb_any(sc->rx.frag);
  927. sc->rx.frag = NULL;
  928. }
  929. requeue:
  930. list_add_tail(&bf->list, &sc->rx.rxbuf);
  931. if (!edma) {
  932. ath_rx_buf_relink(sc, bf, flush);
  933. if (!flush)
  934. ath9k_hw_rxena(ah);
  935. } else if (!flush) {
  936. ath_rx_edma_buf_link(sc, qtype);
  937. }
  938. if (!budget--)
  939. break;
  940. } while (1);
  941. if (!(ah->imask & ATH9K_INT_RXEOL)) {
  942. ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  943. ath9k_hw_set_interrupts(ah);
  944. }
  945. return 0;
  946. }