recv.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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 <linux/relay.h>
  18. #include "ath9k.h"
  19. #include "ar9003_mac.h"
  20. #define SKB_CB_ATHBUF(__skb) (*((struct ath_buf **)__skb->cb))
  21. static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
  22. {
  23. return sc->ps_enabled &&
  24. (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
  25. }
  26. /*
  27. * Setup and link descriptors.
  28. *
  29. * 11N: we can no longer afford to self link the last descriptor.
  30. * MAC acknowledges BA status as long as it copies frames to host
  31. * buffer (or rx fifo). This can incorrectly acknowledge packets
  32. * to a sender if last desc is self-linked.
  33. */
  34. static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
  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 == NULL)
  56. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  57. else
  58. *sc->rx.rxlink = bf->bf_daddr;
  59. sc->rx.rxlink = &ds->ds_link;
  60. }
  61. static void ath_rx_buf_relink(struct ath_softc *sc, struct ath_buf *bf)
  62. {
  63. if (sc->rx.buf_hold)
  64. ath_rx_buf_link(sc, sc->rx.buf_hold);
  65. sc->rx.buf_hold = bf;
  66. }
  67. static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
  68. {
  69. /* XXX block beacon interrupts */
  70. ath9k_hw_setantenna(sc->sc_ah, antenna);
  71. sc->rx.defant = antenna;
  72. sc->rx.rxotherant = 0;
  73. }
  74. static void ath_opmode_init(struct ath_softc *sc)
  75. {
  76. struct ath_hw *ah = sc->sc_ah;
  77. struct ath_common *common = ath9k_hw_common(ah);
  78. u32 rfilt, mfilt[2];
  79. /* configure rx filter */
  80. rfilt = ath_calcrxfilter(sc);
  81. ath9k_hw_setrxfilter(ah, rfilt);
  82. /* configure bssid mask */
  83. ath_hw_setbssidmask(common);
  84. /* configure operational mode */
  85. ath9k_hw_setopmode(ah);
  86. /* calculate and install multicast filter */
  87. mfilt[0] = mfilt[1] = ~0;
  88. ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
  89. }
  90. static bool ath_rx_edma_buf_link(struct ath_softc *sc,
  91. enum ath9k_rx_qtype qtype)
  92. {
  93. struct ath_hw *ah = sc->sc_ah;
  94. struct ath_rx_edma *rx_edma;
  95. struct sk_buff *skb;
  96. struct ath_buf *bf;
  97. rx_edma = &sc->rx.rx_edma[qtype];
  98. if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
  99. return false;
  100. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  101. list_del_init(&bf->list);
  102. skb = bf->bf_mpdu;
  103. memset(skb->data, 0, ah->caps.rx_status_len);
  104. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  105. ah->caps.rx_status_len, DMA_TO_DEVICE);
  106. SKB_CB_ATHBUF(skb) = bf;
  107. ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
  108. __skb_queue_tail(&rx_edma->rx_fifo, skb);
  109. return true;
  110. }
  111. static void ath_rx_addbuffer_edma(struct ath_softc *sc,
  112. enum ath9k_rx_qtype qtype)
  113. {
  114. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  115. struct ath_buf *bf, *tbf;
  116. if (list_empty(&sc->rx.rxbuf)) {
  117. ath_dbg(common, QUEUE, "No free rx buf available\n");
  118. return;
  119. }
  120. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
  121. if (!ath_rx_edma_buf_link(sc, qtype))
  122. break;
  123. }
  124. static void ath_rx_remove_buffer(struct ath_softc *sc,
  125. enum ath9k_rx_qtype qtype)
  126. {
  127. struct ath_buf *bf;
  128. struct ath_rx_edma *rx_edma;
  129. struct sk_buff *skb;
  130. rx_edma = &sc->rx.rx_edma[qtype];
  131. while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
  132. bf = SKB_CB_ATHBUF(skb);
  133. BUG_ON(!bf);
  134. list_add_tail(&bf->list, &sc->rx.rxbuf);
  135. }
  136. }
  137. static void ath_rx_edma_cleanup(struct ath_softc *sc)
  138. {
  139. struct ath_hw *ah = sc->sc_ah;
  140. struct ath_common *common = ath9k_hw_common(ah);
  141. struct ath_buf *bf;
  142. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  143. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  144. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  145. if (bf->bf_mpdu) {
  146. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  147. common->rx_bufsize,
  148. DMA_BIDIRECTIONAL);
  149. dev_kfree_skb_any(bf->bf_mpdu);
  150. bf->bf_buf_addr = 0;
  151. bf->bf_mpdu = NULL;
  152. }
  153. }
  154. }
  155. static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
  156. {
  157. __skb_queue_head_init(&rx_edma->rx_fifo);
  158. rx_edma->rx_fifo_hwsize = size;
  159. }
  160. static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
  161. {
  162. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  163. struct ath_hw *ah = sc->sc_ah;
  164. struct sk_buff *skb;
  165. struct ath_buf *bf;
  166. int error = 0, i;
  167. u32 size;
  168. ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
  169. ah->caps.rx_status_len);
  170. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
  171. ah->caps.rx_lp_qdepth);
  172. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
  173. ah->caps.rx_hp_qdepth);
  174. size = sizeof(struct ath_buf) * nbufs;
  175. bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
  176. if (!bf)
  177. return -ENOMEM;
  178. INIT_LIST_HEAD(&sc->rx.rxbuf);
  179. for (i = 0; i < nbufs; i++, bf++) {
  180. skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
  181. if (!skb) {
  182. error = -ENOMEM;
  183. goto rx_init_fail;
  184. }
  185. memset(skb->data, 0, common->rx_bufsize);
  186. bf->bf_mpdu = skb;
  187. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  188. common->rx_bufsize,
  189. DMA_BIDIRECTIONAL);
  190. if (unlikely(dma_mapping_error(sc->dev,
  191. bf->bf_buf_addr))) {
  192. dev_kfree_skb_any(skb);
  193. bf->bf_mpdu = NULL;
  194. bf->bf_buf_addr = 0;
  195. ath_err(common,
  196. "dma_mapping_error() on RX init\n");
  197. error = -ENOMEM;
  198. goto rx_init_fail;
  199. }
  200. list_add_tail(&bf->list, &sc->rx.rxbuf);
  201. }
  202. return 0;
  203. rx_init_fail:
  204. ath_rx_edma_cleanup(sc);
  205. return error;
  206. }
  207. static void ath_edma_start_recv(struct ath_softc *sc)
  208. {
  209. ath9k_hw_rxena(sc->sc_ah);
  210. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP);
  211. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP);
  212. ath_opmode_init(sc);
  213. ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
  214. }
  215. static void ath_edma_stop_recv(struct ath_softc *sc)
  216. {
  217. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  218. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  219. }
  220. int ath_rx_init(struct ath_softc *sc, int nbufs)
  221. {
  222. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  223. struct sk_buff *skb;
  224. struct ath_buf *bf;
  225. int error = 0;
  226. spin_lock_init(&sc->sc_pcu_lock);
  227. common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
  228. sc->sc_ah->caps.rx_status_len;
  229. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  230. return ath_rx_edma_init(sc, nbufs);
  231. ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
  232. common->cachelsz, common->rx_bufsize);
  233. /* Initialize rx descriptors */
  234. error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
  235. "rx", nbufs, 1, 0);
  236. if (error != 0) {
  237. ath_err(common,
  238. "failed to allocate rx descriptors: %d\n",
  239. error);
  240. goto err;
  241. }
  242. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  243. skb = ath_rxbuf_alloc(common, common->rx_bufsize,
  244. GFP_KERNEL);
  245. if (skb == NULL) {
  246. error = -ENOMEM;
  247. goto err;
  248. }
  249. bf->bf_mpdu = skb;
  250. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  251. common->rx_bufsize,
  252. DMA_FROM_DEVICE);
  253. if (unlikely(dma_mapping_error(sc->dev,
  254. bf->bf_buf_addr))) {
  255. dev_kfree_skb_any(skb);
  256. bf->bf_mpdu = NULL;
  257. bf->bf_buf_addr = 0;
  258. ath_err(common,
  259. "dma_mapping_error() on RX init\n");
  260. error = -ENOMEM;
  261. goto err;
  262. }
  263. }
  264. sc->rx.rxlink = NULL;
  265. err:
  266. if (error)
  267. ath_rx_cleanup(sc);
  268. return error;
  269. }
  270. void ath_rx_cleanup(struct ath_softc *sc)
  271. {
  272. struct ath_hw *ah = sc->sc_ah;
  273. struct ath_common *common = ath9k_hw_common(ah);
  274. struct sk_buff *skb;
  275. struct ath_buf *bf;
  276. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  277. ath_rx_edma_cleanup(sc);
  278. return;
  279. }
  280. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  281. skb = bf->bf_mpdu;
  282. if (skb) {
  283. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  284. common->rx_bufsize,
  285. DMA_FROM_DEVICE);
  286. dev_kfree_skb(skb);
  287. bf->bf_buf_addr = 0;
  288. bf->bf_mpdu = NULL;
  289. }
  290. }
  291. }
  292. /*
  293. * Calculate the receive filter according to the
  294. * operating mode and state:
  295. *
  296. * o always accept unicast, broadcast, and multicast traffic
  297. * o maintain current state of phy error reception (the hal
  298. * may enable phy error frames for noise immunity work)
  299. * o probe request frames are accepted only when operating in
  300. * hostap, adhoc, or monitor modes
  301. * o enable promiscuous mode according to the interface state
  302. * o accept beacons:
  303. * - when operating in adhoc mode so the 802.11 layer creates
  304. * node table entries for peers,
  305. * - when operating in station mode for collecting rssi data when
  306. * the station is otherwise quiet, or
  307. * - when operating as a repeater so we see repeater-sta beacons
  308. * - when scanning
  309. */
  310. u32 ath_calcrxfilter(struct ath_softc *sc)
  311. {
  312. u32 rfilt;
  313. rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
  314. | ATH9K_RX_FILTER_MCAST;
  315. /* if operating on a DFS channel, enable radar pulse detection */
  316. if (sc->hw->conf.radar_enabled)
  317. rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
  318. if (sc->rx.rxfilter & FIF_PROBE_REQ)
  319. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  320. /*
  321. * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
  322. * mode interface or when in monitor mode. AP mode does not need this
  323. * since it receives all in-BSS frames anyway.
  324. */
  325. if (sc->sc_ah->is_monitoring)
  326. rfilt |= ATH9K_RX_FILTER_PROM;
  327. if (sc->rx.rxfilter & FIF_CONTROL)
  328. rfilt |= ATH9K_RX_FILTER_CONTROL;
  329. if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
  330. (sc->nvifs <= 1) &&
  331. !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
  332. rfilt |= ATH9K_RX_FILTER_MYBEACON;
  333. else
  334. rfilt |= ATH9K_RX_FILTER_BEACON;
  335. if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
  336. (sc->rx.rxfilter & FIF_PSPOLL))
  337. rfilt |= ATH9K_RX_FILTER_PSPOLL;
  338. if (conf_is_ht(&sc->hw->conf))
  339. rfilt |= ATH9K_RX_FILTER_COMP_BAR;
  340. if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
  341. /* This is needed for older chips */
  342. if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
  343. rfilt |= ATH9K_RX_FILTER_PROM;
  344. rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
  345. }
  346. if (AR_SREV_9550(sc->sc_ah))
  347. rfilt |= ATH9K_RX_FILTER_4ADDRESS;
  348. return rfilt;
  349. }
  350. int ath_startrecv(struct ath_softc *sc)
  351. {
  352. struct ath_hw *ah = sc->sc_ah;
  353. struct ath_buf *bf, *tbf;
  354. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  355. ath_edma_start_recv(sc);
  356. return 0;
  357. }
  358. if (list_empty(&sc->rx.rxbuf))
  359. goto start_recv;
  360. sc->rx.buf_hold = NULL;
  361. sc->rx.rxlink = NULL;
  362. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
  363. ath_rx_buf_link(sc, bf);
  364. }
  365. /* We could have deleted elements so the list may be empty now */
  366. if (list_empty(&sc->rx.rxbuf))
  367. goto start_recv;
  368. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  369. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  370. ath9k_hw_rxena(ah);
  371. start_recv:
  372. ath_opmode_init(sc);
  373. ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
  374. return 0;
  375. }
  376. static void ath_flushrecv(struct ath_softc *sc)
  377. {
  378. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  379. ath_rx_tasklet(sc, 1, true);
  380. ath_rx_tasklet(sc, 1, false);
  381. }
  382. bool ath_stoprecv(struct ath_softc *sc)
  383. {
  384. struct ath_hw *ah = sc->sc_ah;
  385. bool stopped, reset = false;
  386. ath9k_hw_abortpcurecv(ah);
  387. ath9k_hw_setrxfilter(ah, 0);
  388. stopped = ath9k_hw_stopdmarecv(ah, &reset);
  389. ath_flushrecv(sc);
  390. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  391. ath_edma_stop_recv(sc);
  392. else
  393. sc->rx.rxlink = NULL;
  394. if (!(ah->ah_flags & AH_UNPLUGGED) &&
  395. unlikely(!stopped)) {
  396. ath_err(ath9k_hw_common(sc->sc_ah),
  397. "Could not stop RX, we could be "
  398. "confusing the DMA engine when we start RX up\n");
  399. ATH_DBG_WARN_ON_ONCE(!stopped);
  400. }
  401. return stopped && !reset;
  402. }
  403. static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
  404. {
  405. /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
  406. struct ieee80211_mgmt *mgmt;
  407. u8 *pos, *end, id, elen;
  408. struct ieee80211_tim_ie *tim;
  409. mgmt = (struct ieee80211_mgmt *)skb->data;
  410. pos = mgmt->u.beacon.variable;
  411. end = skb->data + skb->len;
  412. while (pos + 2 < end) {
  413. id = *pos++;
  414. elen = *pos++;
  415. if (pos + elen > end)
  416. break;
  417. if (id == WLAN_EID_TIM) {
  418. if (elen < sizeof(*tim))
  419. break;
  420. tim = (struct ieee80211_tim_ie *) pos;
  421. if (tim->dtim_count != 0)
  422. break;
  423. return tim->bitmap_ctrl & 0x01;
  424. }
  425. pos += elen;
  426. }
  427. return false;
  428. }
  429. static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
  430. {
  431. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  432. if (skb->len < 24 + 8 + 2 + 2)
  433. return;
  434. sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
  435. if (sc->ps_flags & PS_BEACON_SYNC) {
  436. sc->ps_flags &= ~PS_BEACON_SYNC;
  437. ath_dbg(common, PS,
  438. "Reconfigure beacon timers based on synchronized timestamp\n");
  439. ath9k_set_beacon(sc);
  440. }
  441. if (ath_beacon_dtim_pending_cab(skb)) {
  442. /*
  443. * Remain awake waiting for buffered broadcast/multicast
  444. * frames. If the last broadcast/multicast frame is not
  445. * received properly, the next beacon frame will work as
  446. * a backup trigger for returning into NETWORK SLEEP state,
  447. * so we are waiting for it as well.
  448. */
  449. ath_dbg(common, PS,
  450. "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
  451. sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
  452. return;
  453. }
  454. if (sc->ps_flags & PS_WAIT_FOR_CAB) {
  455. /*
  456. * This can happen if a broadcast frame is dropped or the AP
  457. * fails to send a frame indicating that all CAB frames have
  458. * been delivered.
  459. */
  460. sc->ps_flags &= ~PS_WAIT_FOR_CAB;
  461. ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
  462. }
  463. }
  464. static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
  465. {
  466. struct ieee80211_hdr *hdr;
  467. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  468. hdr = (struct ieee80211_hdr *)skb->data;
  469. /* Process Beacon and CAB receive in PS state */
  470. if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
  471. && mybeacon) {
  472. ath_rx_ps_beacon(sc, skb);
  473. } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
  474. (ieee80211_is_data(hdr->frame_control) ||
  475. ieee80211_is_action(hdr->frame_control)) &&
  476. is_multicast_ether_addr(hdr->addr1) &&
  477. !ieee80211_has_moredata(hdr->frame_control)) {
  478. /*
  479. * No more broadcast/multicast frames to be received at this
  480. * point.
  481. */
  482. sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
  483. ath_dbg(common, PS,
  484. "All PS CAB frames received, back to sleep\n");
  485. } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
  486. !is_multicast_ether_addr(hdr->addr1) &&
  487. !ieee80211_has_morefrags(hdr->frame_control)) {
  488. sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
  489. ath_dbg(common, PS,
  490. "Going back to sleep after having received PS-Poll data (0x%lx)\n",
  491. sc->ps_flags & (PS_WAIT_FOR_BEACON |
  492. PS_WAIT_FOR_CAB |
  493. PS_WAIT_FOR_PSPOLL_DATA |
  494. PS_WAIT_FOR_TX_ACK));
  495. }
  496. }
  497. static bool ath_edma_get_buffers(struct ath_softc *sc,
  498. enum ath9k_rx_qtype qtype,
  499. struct ath_rx_status *rs,
  500. struct ath_buf **dest)
  501. {
  502. struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
  503. struct ath_hw *ah = sc->sc_ah;
  504. struct ath_common *common = ath9k_hw_common(ah);
  505. struct sk_buff *skb;
  506. struct ath_buf *bf;
  507. int ret;
  508. skb = skb_peek(&rx_edma->rx_fifo);
  509. if (!skb)
  510. return false;
  511. bf = SKB_CB_ATHBUF(skb);
  512. BUG_ON(!bf);
  513. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  514. common->rx_bufsize, DMA_FROM_DEVICE);
  515. ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
  516. if (ret == -EINPROGRESS) {
  517. /*let device gain the buffer again*/
  518. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  519. common->rx_bufsize, DMA_FROM_DEVICE);
  520. return false;
  521. }
  522. __skb_unlink(skb, &rx_edma->rx_fifo);
  523. if (ret == -EINVAL) {
  524. /* corrupt descriptor, skip this one and the following one */
  525. list_add_tail(&bf->list, &sc->rx.rxbuf);
  526. ath_rx_edma_buf_link(sc, qtype);
  527. skb = skb_peek(&rx_edma->rx_fifo);
  528. if (skb) {
  529. bf = SKB_CB_ATHBUF(skb);
  530. BUG_ON(!bf);
  531. __skb_unlink(skb, &rx_edma->rx_fifo);
  532. list_add_tail(&bf->list, &sc->rx.rxbuf);
  533. ath_rx_edma_buf_link(sc, qtype);
  534. }
  535. bf = NULL;
  536. }
  537. *dest = bf;
  538. return true;
  539. }
  540. static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
  541. struct ath_rx_status *rs,
  542. enum ath9k_rx_qtype qtype)
  543. {
  544. struct ath_buf *bf = NULL;
  545. while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
  546. if (!bf)
  547. continue;
  548. return bf;
  549. }
  550. return NULL;
  551. }
  552. static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
  553. struct ath_rx_status *rs)
  554. {
  555. struct ath_hw *ah = sc->sc_ah;
  556. struct ath_common *common = ath9k_hw_common(ah);
  557. struct ath_desc *ds;
  558. struct ath_buf *bf;
  559. int ret;
  560. if (list_empty(&sc->rx.rxbuf)) {
  561. sc->rx.rxlink = NULL;
  562. return NULL;
  563. }
  564. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  565. if (bf == sc->rx.buf_hold)
  566. return NULL;
  567. ds = bf->bf_desc;
  568. /*
  569. * Must provide the virtual address of the current
  570. * descriptor, the physical address, and the virtual
  571. * address of the next descriptor in the h/w chain.
  572. * This allows the HAL to look ahead to see if the
  573. * hardware is done with a descriptor by checking the
  574. * done bit in the following descriptor and the address
  575. * of the current descriptor the DMA engine is working
  576. * on. All this is necessary because of our use of
  577. * a self-linked list to avoid rx overruns.
  578. */
  579. ret = ath9k_hw_rxprocdesc(ah, ds, rs);
  580. if (ret == -EINPROGRESS) {
  581. struct ath_rx_status trs;
  582. struct ath_buf *tbf;
  583. struct ath_desc *tds;
  584. memset(&trs, 0, sizeof(trs));
  585. if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
  586. sc->rx.rxlink = NULL;
  587. return NULL;
  588. }
  589. tbf = list_entry(bf->list.next, struct ath_buf, list);
  590. /*
  591. * On some hardware the descriptor status words could
  592. * get corrupted, including the done bit. Because of
  593. * this, check if the next descriptor's done bit is
  594. * set or not.
  595. *
  596. * If the next descriptor's done bit is set, the current
  597. * descriptor has been corrupted. Force s/w to discard
  598. * this descriptor and continue...
  599. */
  600. tds = tbf->bf_desc;
  601. ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
  602. if (ret == -EINPROGRESS)
  603. return NULL;
  604. /*
  605. * mark descriptor as zero-length and set the 'more'
  606. * flag to ensure that both buffers get discarded
  607. */
  608. rs->rs_datalen = 0;
  609. rs->rs_more = true;
  610. }
  611. list_del(&bf->list);
  612. if (!bf->bf_mpdu)
  613. return bf;
  614. /*
  615. * Synchronize the DMA transfer with CPU before
  616. * 1. accessing the frame
  617. * 2. requeueing the same buffer to h/w
  618. */
  619. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  620. common->rx_bufsize,
  621. DMA_FROM_DEVICE);
  622. return bf;
  623. }
  624. /* Assumes you've already done the endian to CPU conversion */
  625. static bool ath9k_rx_accept(struct ath_common *common,
  626. struct ieee80211_hdr *hdr,
  627. struct ieee80211_rx_status *rxs,
  628. struct ath_rx_status *rx_stats,
  629. bool *decrypt_error)
  630. {
  631. struct ath_softc *sc = (struct ath_softc *) common->priv;
  632. bool is_mc, is_valid_tkip, strip_mic, mic_error;
  633. struct ath_hw *ah = common->ah;
  634. __le16 fc;
  635. fc = hdr->frame_control;
  636. is_mc = !!is_multicast_ether_addr(hdr->addr1);
  637. is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
  638. test_bit(rx_stats->rs_keyix, common->tkip_keymap);
  639. strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
  640. ieee80211_has_protected(fc) &&
  641. !(rx_stats->rs_status &
  642. (ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
  643. ATH9K_RXERR_KEYMISS));
  644. /*
  645. * Key miss events are only relevant for pairwise keys where the
  646. * descriptor does contain a valid key index. This has been observed
  647. * mostly with CCMP encryption.
  648. */
  649. if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID ||
  650. !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
  651. rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
  652. mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
  653. !ieee80211_has_morefrags(fc) &&
  654. !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
  655. (rx_stats->rs_status & ATH9K_RXERR_MIC);
  656. /*
  657. * The rx_stats->rs_status will not be set until the end of the
  658. * chained descriptors so it can be ignored if rs_more is set. The
  659. * rs_more will be false at the last element of the chained
  660. * descriptors.
  661. */
  662. if (rx_stats->rs_status != 0) {
  663. u8 status_mask;
  664. if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
  665. rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
  666. mic_error = false;
  667. }
  668. if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
  669. (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
  670. *decrypt_error = true;
  671. mic_error = false;
  672. }
  673. /*
  674. * Reject error frames with the exception of
  675. * decryption and MIC failures. For monitor mode,
  676. * we also ignore the CRC error.
  677. */
  678. status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
  679. ATH9K_RXERR_KEYMISS;
  680. if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
  681. status_mask |= ATH9K_RXERR_CRC;
  682. if (rx_stats->rs_status & ~status_mask)
  683. return false;
  684. }
  685. /*
  686. * For unicast frames the MIC error bit can have false positives,
  687. * so all MIC error reports need to be validated in software.
  688. * False negatives are not common, so skip software verification
  689. * if the hardware considers the MIC valid.
  690. */
  691. if (strip_mic)
  692. rxs->flag |= RX_FLAG_MMIC_STRIPPED;
  693. else if (is_mc && mic_error)
  694. rxs->flag |= RX_FLAG_MMIC_ERROR;
  695. return true;
  696. }
  697. static int ath9k_process_rate(struct ath_common *common,
  698. struct ieee80211_hw *hw,
  699. struct ath_rx_status *rx_stats,
  700. struct ieee80211_rx_status *rxs)
  701. {
  702. struct ieee80211_supported_band *sband;
  703. enum ieee80211_band band;
  704. unsigned int i = 0;
  705. struct ath_softc __maybe_unused *sc = common->priv;
  706. band = hw->conf.chandef.chan->band;
  707. sband = hw->wiphy->bands[band];
  708. switch (hw->conf.chandef.width) {
  709. case NL80211_CHAN_WIDTH_5:
  710. rxs->flag |= RX_FLAG_5MHZ;
  711. break;
  712. case NL80211_CHAN_WIDTH_10:
  713. rxs->flag |= RX_FLAG_10MHZ;
  714. break;
  715. default:
  716. break;
  717. }
  718. if (rx_stats->rs_rate & 0x80) {
  719. /* HT rate */
  720. rxs->flag |= RX_FLAG_HT;
  721. rxs->flag |= rx_stats->flag;
  722. rxs->rate_idx = rx_stats->rs_rate & 0x7f;
  723. return 0;
  724. }
  725. for (i = 0; i < sband->n_bitrates; i++) {
  726. if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
  727. rxs->rate_idx = i;
  728. return 0;
  729. }
  730. if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
  731. rxs->flag |= RX_FLAG_SHORTPRE;
  732. rxs->rate_idx = i;
  733. return 0;
  734. }
  735. }
  736. /*
  737. * No valid hardware bitrate found -- we should not get here
  738. * because hardware has already validated this frame as OK.
  739. */
  740. ath_dbg(common, ANY,
  741. "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
  742. rx_stats->rs_rate);
  743. RX_STAT_INC(rx_rate_err);
  744. return -EINVAL;
  745. }
  746. static void ath9k_process_rssi(struct ath_common *common,
  747. struct ieee80211_hw *hw,
  748. struct ath_rx_status *rx_stats,
  749. struct ieee80211_rx_status *rxs)
  750. {
  751. struct ath_softc *sc = hw->priv;
  752. struct ath_hw *ah = common->ah;
  753. int last_rssi;
  754. int rssi = rx_stats->rs_rssi;
  755. /*
  756. * RSSI is not available for subframes in an A-MPDU.
  757. */
  758. if (rx_stats->rs_moreaggr) {
  759. rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
  760. return;
  761. }
  762. /*
  763. * Check if the RSSI for the last subframe in an A-MPDU
  764. * or an unaggregated frame is valid.
  765. */
  766. if (rx_stats->rs_rssi == ATH9K_RSSI_BAD) {
  767. rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
  768. return;
  769. }
  770. /*
  771. * Update Beacon RSSI, this is used by ANI.
  772. */
  773. if (rx_stats->is_mybeacon &&
  774. ((ah->opmode == NL80211_IFTYPE_STATION) ||
  775. (ah->opmode == NL80211_IFTYPE_ADHOC))) {
  776. ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
  777. last_rssi = sc->last_rssi;
  778. if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
  779. rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
  780. if (rssi < 0)
  781. rssi = 0;
  782. ah->stats.avgbrssi = rssi;
  783. }
  784. rxs->signal = ah->noise + rx_stats->rs_rssi;
  785. }
  786. static void ath9k_process_tsf(struct ath_rx_status *rs,
  787. struct ieee80211_rx_status *rxs,
  788. u64 tsf)
  789. {
  790. u32 tsf_lower = tsf & 0xffffffff;
  791. rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp;
  792. if (rs->rs_tstamp > tsf_lower &&
  793. unlikely(rs->rs_tstamp - tsf_lower > 0x10000000))
  794. rxs->mactime -= 0x100000000ULL;
  795. if (rs->rs_tstamp < tsf_lower &&
  796. unlikely(tsf_lower - rs->rs_tstamp > 0x10000000))
  797. rxs->mactime += 0x100000000ULL;
  798. }
  799. #ifdef CONFIG_ATH9K_DEBUGFS
  800. static s8 fix_rssi_inv_only(u8 rssi_val)
  801. {
  802. if (rssi_val == 128)
  803. rssi_val = 0;
  804. return (s8) rssi_val;
  805. }
  806. #endif
  807. /* returns 1 if this was a spectral frame, even if not handled. */
  808. static int ath_process_fft(struct ath_softc *sc, struct ieee80211_hdr *hdr,
  809. struct ath_rx_status *rs, u64 tsf)
  810. {
  811. #ifdef CONFIG_ATH9K_DEBUGFS
  812. struct ath_hw *ah = sc->sc_ah;
  813. u8 bins[SPECTRAL_HT20_NUM_BINS];
  814. u8 *vdata = (u8 *)hdr;
  815. struct fft_sample_ht20 fft_sample;
  816. struct ath_radar_info *radar_info;
  817. struct ath_ht20_mag_info *mag_info;
  818. int len = rs->rs_datalen;
  819. int dc_pos;
  820. u16 length, max_magnitude;
  821. /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
  822. * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
  823. * yet, but this is supposed to be possible as well.
  824. */
  825. if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
  826. rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
  827. rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
  828. return 0;
  829. /* check if spectral scan bit is set. This does not have to be checked
  830. * if received through a SPECTRAL phy error, but shouldn't hurt.
  831. */
  832. radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
  833. if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
  834. return 0;
  835. /* Variation in the data length is possible and will be fixed later.
  836. * Note that we only support HT20 for now.
  837. *
  838. * TODO: add HT20_40 support as well.
  839. */
  840. if ((len > SPECTRAL_HT20_TOTAL_DATA_LEN + 2) ||
  841. (len < SPECTRAL_HT20_TOTAL_DATA_LEN - 1))
  842. return 1;
  843. fft_sample.tlv.type = ATH_FFT_SAMPLE_HT20;
  844. length = sizeof(fft_sample) - sizeof(fft_sample.tlv);
  845. fft_sample.tlv.length = __cpu_to_be16(length);
  846. fft_sample.freq = __cpu_to_be16(ah->curchan->chan->center_freq);
  847. fft_sample.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl0);
  848. fft_sample.noise = ah->noise;
  849. switch (len - SPECTRAL_HT20_TOTAL_DATA_LEN) {
  850. case 0:
  851. /* length correct, nothing to do. */
  852. memcpy(bins, vdata, SPECTRAL_HT20_NUM_BINS);
  853. break;
  854. case -1:
  855. /* first byte missing, duplicate it. */
  856. memcpy(&bins[1], vdata, SPECTRAL_HT20_NUM_BINS - 1);
  857. bins[0] = vdata[0];
  858. break;
  859. case 2:
  860. /* MAC added 2 extra bytes at bin 30 and 32, remove them. */
  861. memcpy(bins, vdata, 30);
  862. bins[30] = vdata[31];
  863. memcpy(&bins[31], &vdata[33], SPECTRAL_HT20_NUM_BINS - 31);
  864. break;
  865. case 1:
  866. /* MAC added 2 extra bytes AND first byte is missing. */
  867. bins[0] = vdata[0];
  868. memcpy(&bins[0], vdata, 30);
  869. bins[31] = vdata[31];
  870. memcpy(&bins[32], &vdata[33], SPECTRAL_HT20_NUM_BINS - 32);
  871. break;
  872. default:
  873. return 1;
  874. }
  875. /* DC value (value in the middle) is the blind spot of the spectral
  876. * sample and invalid, interpolate it.
  877. */
  878. dc_pos = SPECTRAL_HT20_NUM_BINS / 2;
  879. bins[dc_pos] = (bins[dc_pos + 1] + bins[dc_pos - 1]) / 2;
  880. /* mag data is at the end of the frame, in front of radar_info */
  881. mag_info = ((struct ath_ht20_mag_info *)radar_info) - 1;
  882. /* copy raw bins without scaling them */
  883. memcpy(fft_sample.data, bins, SPECTRAL_HT20_NUM_BINS);
  884. fft_sample.max_exp = mag_info->max_exp & 0xf;
  885. max_magnitude = spectral_max_magnitude(mag_info->all_bins);
  886. fft_sample.max_magnitude = __cpu_to_be16(max_magnitude);
  887. fft_sample.max_index = spectral_max_index(mag_info->all_bins);
  888. fft_sample.bitmap_weight = spectral_bitmap_weight(mag_info->all_bins);
  889. fft_sample.tsf = __cpu_to_be64(tsf);
  890. ath_debug_send_fft_sample(sc, &fft_sample.tlv);
  891. return 1;
  892. #else
  893. return 0;
  894. #endif
  895. }
  896. static bool ath9k_is_mybeacon(struct ath_softc *sc, struct ieee80211_hdr *hdr)
  897. {
  898. struct ath_hw *ah = sc->sc_ah;
  899. struct ath_common *common = ath9k_hw_common(ah);
  900. if (ieee80211_is_beacon(hdr->frame_control)) {
  901. RX_STAT_INC(rx_beacons);
  902. if (!is_zero_ether_addr(common->curbssid) &&
  903. ether_addr_equal(hdr->addr3, common->curbssid))
  904. return true;
  905. }
  906. return false;
  907. }
  908. /*
  909. * For Decrypt or Demic errors, we only mark packet status here and always push
  910. * up the frame up to let mac80211 handle the actual error case, be it no
  911. * decryption key or real decryption error. This let us keep statistics there.
  912. */
  913. static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
  914. struct sk_buff *skb,
  915. struct ath_rx_status *rx_stats,
  916. struct ieee80211_rx_status *rx_status,
  917. bool *decrypt_error, u64 tsf)
  918. {
  919. struct ieee80211_hw *hw = sc->hw;
  920. struct ath_hw *ah = sc->sc_ah;
  921. struct ath_common *common = ath9k_hw_common(ah);
  922. struct ieee80211_hdr *hdr;
  923. bool discard_current = sc->rx.discard_next;
  924. int ret = 0;
  925. /*
  926. * Discard corrupt descriptors which are marked in
  927. * ath_get_next_rx_buf().
  928. */
  929. sc->rx.discard_next = rx_stats->rs_more;
  930. if (discard_current)
  931. return -EINVAL;
  932. /*
  933. * Discard zero-length packets.
  934. */
  935. if (!rx_stats->rs_datalen) {
  936. RX_STAT_INC(rx_len_err);
  937. return -EINVAL;
  938. }
  939. /*
  940. * rs_status follows rs_datalen so if rs_datalen is too large
  941. * we can take a hint that hardware corrupted it, so ignore
  942. * those frames.
  943. */
  944. if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) {
  945. RX_STAT_INC(rx_len_err);
  946. return -EINVAL;
  947. }
  948. /* Only use status info from the last fragment */
  949. if (rx_stats->rs_more)
  950. return 0;
  951. /*
  952. * Return immediately if the RX descriptor has been marked
  953. * as corrupt based on the various error bits.
  954. *
  955. * This is different from the other corrupt descriptor
  956. * condition handled above.
  957. */
  958. if (rx_stats->rs_status & ATH9K_RXERR_CORRUPT_DESC) {
  959. ret = -EINVAL;
  960. goto exit;
  961. }
  962. hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len);
  963. ath9k_process_tsf(rx_stats, rx_status, tsf);
  964. ath_debug_stat_rx(sc, rx_stats);
  965. /*
  966. * Process PHY errors and return so that the packet
  967. * can be dropped.
  968. */
  969. if (rx_stats->rs_status & ATH9K_RXERR_PHY) {
  970. ath9k_dfs_process_phyerr(sc, hdr, rx_stats, rx_status->mactime);
  971. if (ath_process_fft(sc, hdr, rx_stats, rx_status->mactime))
  972. RX_STAT_INC(rx_spectral);
  973. ret = -EINVAL;
  974. goto exit;
  975. }
  976. /*
  977. * everything but the rate is checked here, the rate check is done
  978. * separately to avoid doing two lookups for a rate for each frame.
  979. */
  980. if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error)) {
  981. ret = -EINVAL;
  982. goto exit;
  983. }
  984. rx_stats->is_mybeacon = ath9k_is_mybeacon(sc, hdr);
  985. if (rx_stats->is_mybeacon) {
  986. sc->hw_busy_count = 0;
  987. ath_start_rx_poll(sc, 3);
  988. }
  989. if (ath9k_process_rate(common, hw, rx_stats, rx_status)) {
  990. ret =-EINVAL;
  991. goto exit;
  992. }
  993. ath9k_process_rssi(common, hw, rx_stats, rx_status);
  994. rx_status->band = hw->conf.chandef.chan->band;
  995. rx_status->freq = hw->conf.chandef.chan->center_freq;
  996. rx_status->antenna = rx_stats->rs_antenna;
  997. rx_status->flag |= RX_FLAG_MACTIME_END;
  998. #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
  999. if (ieee80211_is_data_present(hdr->frame_control) &&
  1000. !ieee80211_is_qos_nullfunc(hdr->frame_control))
  1001. sc->rx.num_pkts++;
  1002. #endif
  1003. exit:
  1004. sc->rx.discard_next = false;
  1005. return ret;
  1006. }
  1007. static void ath9k_rx_skb_postprocess(struct ath_common *common,
  1008. struct sk_buff *skb,
  1009. struct ath_rx_status *rx_stats,
  1010. struct ieee80211_rx_status *rxs,
  1011. bool decrypt_error)
  1012. {
  1013. struct ath_hw *ah = common->ah;
  1014. struct ieee80211_hdr *hdr;
  1015. int hdrlen, padpos, padsize;
  1016. u8 keyix;
  1017. __le16 fc;
  1018. /* see if any padding is done by the hw and remove it */
  1019. hdr = (struct ieee80211_hdr *) skb->data;
  1020. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1021. fc = hdr->frame_control;
  1022. padpos = ieee80211_hdrlen(fc);
  1023. /* The MAC header is padded to have 32-bit boundary if the
  1024. * packet payload is non-zero. The general calculation for
  1025. * padsize would take into account odd header lengths:
  1026. * padsize = (4 - padpos % 4) % 4; However, since only
  1027. * even-length headers are used, padding can only be 0 or 2
  1028. * bytes and we can optimize this a bit. In addition, we must
  1029. * not try to remove padding from short control frames that do
  1030. * not have payload. */
  1031. padsize = padpos & 3;
  1032. if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
  1033. memmove(skb->data + padsize, skb->data, padpos);
  1034. skb_pull(skb, padsize);
  1035. }
  1036. keyix = rx_stats->rs_keyix;
  1037. if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
  1038. ieee80211_has_protected(fc)) {
  1039. rxs->flag |= RX_FLAG_DECRYPTED;
  1040. } else if (ieee80211_has_protected(fc)
  1041. && !decrypt_error && skb->len >= hdrlen + 4) {
  1042. keyix = skb->data[hdrlen + 3] >> 6;
  1043. if (test_bit(keyix, common->keymap))
  1044. rxs->flag |= RX_FLAG_DECRYPTED;
  1045. }
  1046. if (ah->sw_mgmt_crypto &&
  1047. (rxs->flag & RX_FLAG_DECRYPTED) &&
  1048. ieee80211_is_mgmt(fc))
  1049. /* Use software decrypt for management frames. */
  1050. rxs->flag &= ~RX_FLAG_DECRYPTED;
  1051. }
  1052. /*
  1053. * Run the LNA combining algorithm only in these cases:
  1054. *
  1055. * Standalone WLAN cards with both LNA/Antenna diversity
  1056. * enabled in the EEPROM.
  1057. *
  1058. * WLAN+BT cards which are in the supported card list
  1059. * in ath_pci_id_table and the user has loaded the
  1060. * driver with "bt_ant_diversity" set to true.
  1061. */
  1062. static void ath9k_antenna_check(struct ath_softc *sc,
  1063. struct ath_rx_status *rs)
  1064. {
  1065. struct ath_hw *ah = sc->sc_ah;
  1066. struct ath9k_hw_capabilities *pCap = &ah->caps;
  1067. struct ath_common *common = ath9k_hw_common(ah);
  1068. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB))
  1069. return;
  1070. /*
  1071. * Change the default rx antenna if rx diversity
  1072. * chooses the other antenna 3 times in a row.
  1073. */
  1074. if (sc->rx.defant != rs->rs_antenna) {
  1075. if (++sc->rx.rxotherant >= 3)
  1076. ath_setdefantenna(sc, rs->rs_antenna);
  1077. } else {
  1078. sc->rx.rxotherant = 0;
  1079. }
  1080. if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) {
  1081. if (common->bt_ant_diversity)
  1082. ath_ant_comb_scan(sc, rs);
  1083. } else {
  1084. ath_ant_comb_scan(sc, rs);
  1085. }
  1086. }
  1087. static void ath9k_apply_ampdu_details(struct ath_softc *sc,
  1088. struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
  1089. {
  1090. if (rs->rs_isaggr) {
  1091. rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
  1092. rxs->ampdu_reference = sc->rx.ampdu_ref;
  1093. if (!rs->rs_moreaggr) {
  1094. rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
  1095. sc->rx.ampdu_ref++;
  1096. }
  1097. if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
  1098. rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
  1099. }
  1100. }
  1101. int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
  1102. {
  1103. struct ath_buf *bf;
  1104. struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
  1105. struct ieee80211_rx_status *rxs;
  1106. struct ath_hw *ah = sc->sc_ah;
  1107. struct ath_common *common = ath9k_hw_common(ah);
  1108. struct ieee80211_hw *hw = sc->hw;
  1109. int retval;
  1110. struct ath_rx_status rs;
  1111. enum ath9k_rx_qtype qtype;
  1112. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  1113. int dma_type;
  1114. u64 tsf = 0;
  1115. unsigned long flags;
  1116. dma_addr_t new_buf_addr;
  1117. if (edma)
  1118. dma_type = DMA_BIDIRECTIONAL;
  1119. else
  1120. dma_type = DMA_FROM_DEVICE;
  1121. qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
  1122. tsf = ath9k_hw_gettsf64(ah);
  1123. do {
  1124. bool decrypt_error = false;
  1125. memset(&rs, 0, sizeof(rs));
  1126. if (edma)
  1127. bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
  1128. else
  1129. bf = ath_get_next_rx_buf(sc, &rs);
  1130. if (!bf)
  1131. break;
  1132. skb = bf->bf_mpdu;
  1133. if (!skb)
  1134. continue;
  1135. /*
  1136. * Take frame header from the first fragment and RX status from
  1137. * the last one.
  1138. */
  1139. if (sc->rx.frag)
  1140. hdr_skb = sc->rx.frag;
  1141. else
  1142. hdr_skb = skb;
  1143. rxs = IEEE80211_SKB_RXCB(hdr_skb);
  1144. memset(rxs, 0, sizeof(struct ieee80211_rx_status));
  1145. retval = ath9k_rx_skb_preprocess(sc, hdr_skb, &rs, rxs,
  1146. &decrypt_error, tsf);
  1147. if (retval)
  1148. goto requeue_drop_frag;
  1149. /* Ensure we always have an skb to requeue once we are done
  1150. * processing the current buffer's skb */
  1151. requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
  1152. /* If there is no memory we ignore the current RX'd frame,
  1153. * tell hardware it can give us a new frame using the old
  1154. * skb and put it at the tail of the sc->rx.rxbuf list for
  1155. * processing. */
  1156. if (!requeue_skb) {
  1157. RX_STAT_INC(rx_oom_err);
  1158. goto requeue_drop_frag;
  1159. }
  1160. /* We will now give hardware our shiny new allocated skb */
  1161. new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
  1162. common->rx_bufsize, dma_type);
  1163. if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
  1164. dev_kfree_skb_any(requeue_skb);
  1165. goto requeue_drop_frag;
  1166. }
  1167. /* Unmap the frame */
  1168. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  1169. common->rx_bufsize, dma_type);
  1170. bf->bf_mpdu = requeue_skb;
  1171. bf->bf_buf_addr = new_buf_addr;
  1172. skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
  1173. if (ah->caps.rx_status_len)
  1174. skb_pull(skb, ah->caps.rx_status_len);
  1175. if (!rs.rs_more)
  1176. ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
  1177. rxs, decrypt_error);
  1178. if (rs.rs_more) {
  1179. RX_STAT_INC(rx_frags);
  1180. /*
  1181. * rs_more indicates chained descriptors which can be
  1182. * used to link buffers together for a sort of
  1183. * scatter-gather operation.
  1184. */
  1185. if (sc->rx.frag) {
  1186. /* too many fragments - cannot handle frame */
  1187. dev_kfree_skb_any(sc->rx.frag);
  1188. dev_kfree_skb_any(skb);
  1189. RX_STAT_INC(rx_too_many_frags_err);
  1190. skb = NULL;
  1191. }
  1192. sc->rx.frag = skb;
  1193. goto requeue;
  1194. }
  1195. if (sc->rx.frag) {
  1196. int space = skb->len - skb_tailroom(hdr_skb);
  1197. if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
  1198. dev_kfree_skb(skb);
  1199. RX_STAT_INC(rx_oom_err);
  1200. goto requeue_drop_frag;
  1201. }
  1202. sc->rx.frag = NULL;
  1203. skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
  1204. skb->len);
  1205. dev_kfree_skb_any(skb);
  1206. skb = hdr_skb;
  1207. }
  1208. if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
  1209. skb_trim(skb, skb->len - 8);
  1210. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  1211. if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
  1212. PS_WAIT_FOR_CAB |
  1213. PS_WAIT_FOR_PSPOLL_DATA)) ||
  1214. ath9k_check_auto_sleep(sc))
  1215. ath_rx_ps(sc, skb, rs.is_mybeacon);
  1216. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  1217. ath9k_antenna_check(sc, &rs);
  1218. ath9k_apply_ampdu_details(sc, &rs, rxs);
  1219. ieee80211_rx(hw, skb);
  1220. requeue_drop_frag:
  1221. if (sc->rx.frag) {
  1222. dev_kfree_skb_any(sc->rx.frag);
  1223. sc->rx.frag = NULL;
  1224. }
  1225. requeue:
  1226. list_add_tail(&bf->list, &sc->rx.rxbuf);
  1227. if (flush)
  1228. continue;
  1229. if (edma) {
  1230. ath_rx_edma_buf_link(sc, qtype);
  1231. } else {
  1232. ath_rx_buf_relink(sc, bf);
  1233. ath9k_hw_rxena(ah);
  1234. }
  1235. } while (1);
  1236. if (!(ah->imask & ATH9K_INT_RXEOL)) {
  1237. ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  1238. ath9k_hw_set_interrupts(ah);
  1239. }
  1240. return 0;
  1241. }