recv.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  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_rxbuf **)__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_rxbuf *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_rxbuf *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_rxbuf *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_rxbuf, 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_rxbuf *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_rxbuf *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_rxbuf *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_rxbuf *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_rxbuf) * 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_rxbuf *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_rxbuf *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. if (config_enabled(CONFIG_ATH9K_TX99))
  314. return 0;
  315. rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
  316. | ATH9K_RX_FILTER_MCAST;
  317. /* if operating on a DFS channel, enable radar pulse detection */
  318. if (sc->hw->conf.radar_enabled)
  319. rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
  320. if (sc->rx.rxfilter & FIF_PROBE_REQ)
  321. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  322. /*
  323. * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
  324. * mode interface or when in monitor mode. AP mode does not need this
  325. * since it receives all in-BSS frames anyway.
  326. */
  327. if (sc->sc_ah->is_monitoring)
  328. rfilt |= ATH9K_RX_FILTER_PROM;
  329. if (sc->rx.rxfilter & FIF_CONTROL)
  330. rfilt |= ATH9K_RX_FILTER_CONTROL;
  331. if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
  332. (sc->nvifs <= 1) &&
  333. !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
  334. rfilt |= ATH9K_RX_FILTER_MYBEACON;
  335. else
  336. rfilt |= ATH9K_RX_FILTER_BEACON;
  337. if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
  338. (sc->rx.rxfilter & FIF_PSPOLL))
  339. rfilt |= ATH9K_RX_FILTER_PSPOLL;
  340. if (conf_is_ht(&sc->hw->conf))
  341. rfilt |= ATH9K_RX_FILTER_COMP_BAR;
  342. if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
  343. /* This is needed for older chips */
  344. if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
  345. rfilt |= ATH9K_RX_FILTER_PROM;
  346. rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
  347. }
  348. if (AR_SREV_9550(sc->sc_ah))
  349. rfilt |= ATH9K_RX_FILTER_4ADDRESS;
  350. return rfilt;
  351. }
  352. int ath_startrecv(struct ath_softc *sc)
  353. {
  354. struct ath_hw *ah = sc->sc_ah;
  355. struct ath_rxbuf *bf, *tbf;
  356. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  357. ath_edma_start_recv(sc);
  358. return 0;
  359. }
  360. if (list_empty(&sc->rx.rxbuf))
  361. goto start_recv;
  362. sc->rx.buf_hold = NULL;
  363. sc->rx.rxlink = NULL;
  364. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
  365. ath_rx_buf_link(sc, bf);
  366. }
  367. /* We could have deleted elements so the list may be empty now */
  368. if (list_empty(&sc->rx.rxbuf))
  369. goto start_recv;
  370. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  371. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  372. ath9k_hw_rxena(ah);
  373. start_recv:
  374. ath_opmode_init(sc);
  375. ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
  376. return 0;
  377. }
  378. static void ath_flushrecv(struct ath_softc *sc)
  379. {
  380. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  381. ath_rx_tasklet(sc, 1, true);
  382. ath_rx_tasklet(sc, 1, false);
  383. }
  384. bool ath_stoprecv(struct ath_softc *sc)
  385. {
  386. struct ath_hw *ah = sc->sc_ah;
  387. bool stopped, reset = false;
  388. ath9k_hw_abortpcurecv(ah);
  389. ath9k_hw_setrxfilter(ah, 0);
  390. stopped = ath9k_hw_stopdmarecv(ah, &reset);
  391. ath_flushrecv(sc);
  392. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  393. ath_edma_stop_recv(sc);
  394. else
  395. sc->rx.rxlink = NULL;
  396. if (!(ah->ah_flags & AH_UNPLUGGED) &&
  397. unlikely(!stopped)) {
  398. ath_err(ath9k_hw_common(sc->sc_ah),
  399. "Could not stop RX, we could be "
  400. "confusing the DMA engine when we start RX up\n");
  401. ATH_DBG_WARN_ON_ONCE(!stopped);
  402. }
  403. return stopped && !reset;
  404. }
  405. static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
  406. {
  407. /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
  408. struct ieee80211_mgmt *mgmt;
  409. u8 *pos, *end, id, elen;
  410. struct ieee80211_tim_ie *tim;
  411. mgmt = (struct ieee80211_mgmt *)skb->data;
  412. pos = mgmt->u.beacon.variable;
  413. end = skb->data + skb->len;
  414. while (pos + 2 < end) {
  415. id = *pos++;
  416. elen = *pos++;
  417. if (pos + elen > end)
  418. break;
  419. if (id == WLAN_EID_TIM) {
  420. if (elen < sizeof(*tim))
  421. break;
  422. tim = (struct ieee80211_tim_ie *) pos;
  423. if (tim->dtim_count != 0)
  424. break;
  425. return tim->bitmap_ctrl & 0x01;
  426. }
  427. pos += elen;
  428. }
  429. return false;
  430. }
  431. static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
  432. {
  433. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  434. if (skb->len < 24 + 8 + 2 + 2)
  435. return;
  436. sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
  437. if (sc->ps_flags & PS_BEACON_SYNC) {
  438. sc->ps_flags &= ~PS_BEACON_SYNC;
  439. ath_dbg(common, PS,
  440. "Reconfigure beacon timers based on synchronized timestamp\n");
  441. ath9k_set_beacon(sc);
  442. }
  443. if (ath_beacon_dtim_pending_cab(skb)) {
  444. /*
  445. * Remain awake waiting for buffered broadcast/multicast
  446. * frames. If the last broadcast/multicast frame is not
  447. * received properly, the next beacon frame will work as
  448. * a backup trigger for returning into NETWORK SLEEP state,
  449. * so we are waiting for it as well.
  450. */
  451. ath_dbg(common, PS,
  452. "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
  453. sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
  454. return;
  455. }
  456. if (sc->ps_flags & PS_WAIT_FOR_CAB) {
  457. /*
  458. * This can happen if a broadcast frame is dropped or the AP
  459. * fails to send a frame indicating that all CAB frames have
  460. * been delivered.
  461. */
  462. sc->ps_flags &= ~PS_WAIT_FOR_CAB;
  463. ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
  464. }
  465. }
  466. static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
  467. {
  468. struct ieee80211_hdr *hdr;
  469. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  470. hdr = (struct ieee80211_hdr *)skb->data;
  471. /* Process Beacon and CAB receive in PS state */
  472. if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
  473. && mybeacon) {
  474. ath_rx_ps_beacon(sc, skb);
  475. } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
  476. (ieee80211_is_data(hdr->frame_control) ||
  477. ieee80211_is_action(hdr->frame_control)) &&
  478. is_multicast_ether_addr(hdr->addr1) &&
  479. !ieee80211_has_moredata(hdr->frame_control)) {
  480. /*
  481. * No more broadcast/multicast frames to be received at this
  482. * point.
  483. */
  484. sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
  485. ath_dbg(common, PS,
  486. "All PS CAB frames received, back to sleep\n");
  487. } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
  488. !is_multicast_ether_addr(hdr->addr1) &&
  489. !ieee80211_has_morefrags(hdr->frame_control)) {
  490. sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
  491. ath_dbg(common, PS,
  492. "Going back to sleep after having received PS-Poll data (0x%lx)\n",
  493. sc->ps_flags & (PS_WAIT_FOR_BEACON |
  494. PS_WAIT_FOR_CAB |
  495. PS_WAIT_FOR_PSPOLL_DATA |
  496. PS_WAIT_FOR_TX_ACK));
  497. }
  498. }
  499. static bool ath_edma_get_buffers(struct ath_softc *sc,
  500. enum ath9k_rx_qtype qtype,
  501. struct ath_rx_status *rs,
  502. struct ath_rxbuf **dest)
  503. {
  504. struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
  505. struct ath_hw *ah = sc->sc_ah;
  506. struct ath_common *common = ath9k_hw_common(ah);
  507. struct sk_buff *skb;
  508. struct ath_rxbuf *bf;
  509. int ret;
  510. skb = skb_peek(&rx_edma->rx_fifo);
  511. if (!skb)
  512. return false;
  513. bf = SKB_CB_ATHBUF(skb);
  514. BUG_ON(!bf);
  515. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  516. common->rx_bufsize, DMA_FROM_DEVICE);
  517. ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
  518. if (ret == -EINPROGRESS) {
  519. /*let device gain the buffer again*/
  520. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  521. common->rx_bufsize, DMA_FROM_DEVICE);
  522. return false;
  523. }
  524. __skb_unlink(skb, &rx_edma->rx_fifo);
  525. if (ret == -EINVAL) {
  526. /* corrupt descriptor, skip this one and the following one */
  527. list_add_tail(&bf->list, &sc->rx.rxbuf);
  528. ath_rx_edma_buf_link(sc, qtype);
  529. skb = skb_peek(&rx_edma->rx_fifo);
  530. if (skb) {
  531. bf = SKB_CB_ATHBUF(skb);
  532. BUG_ON(!bf);
  533. __skb_unlink(skb, &rx_edma->rx_fifo);
  534. list_add_tail(&bf->list, &sc->rx.rxbuf);
  535. ath_rx_edma_buf_link(sc, qtype);
  536. }
  537. bf = NULL;
  538. }
  539. *dest = bf;
  540. return true;
  541. }
  542. static struct ath_rxbuf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
  543. struct ath_rx_status *rs,
  544. enum ath9k_rx_qtype qtype)
  545. {
  546. struct ath_rxbuf *bf = NULL;
  547. while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
  548. if (!bf)
  549. continue;
  550. return bf;
  551. }
  552. return NULL;
  553. }
  554. static struct ath_rxbuf *ath_get_next_rx_buf(struct ath_softc *sc,
  555. struct ath_rx_status *rs)
  556. {
  557. struct ath_hw *ah = sc->sc_ah;
  558. struct ath_common *common = ath9k_hw_common(ah);
  559. struct ath_desc *ds;
  560. struct ath_rxbuf *bf;
  561. int ret;
  562. if (list_empty(&sc->rx.rxbuf)) {
  563. sc->rx.rxlink = NULL;
  564. return NULL;
  565. }
  566. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  567. if (bf == sc->rx.buf_hold)
  568. return NULL;
  569. ds = bf->bf_desc;
  570. /*
  571. * Must provide the virtual address of the current
  572. * descriptor, the physical address, and the virtual
  573. * address of the next descriptor in the h/w chain.
  574. * This allows the HAL to look ahead to see if the
  575. * hardware is done with a descriptor by checking the
  576. * done bit in the following descriptor and the address
  577. * of the current descriptor the DMA engine is working
  578. * on. All this is necessary because of our use of
  579. * a self-linked list to avoid rx overruns.
  580. */
  581. ret = ath9k_hw_rxprocdesc(ah, ds, rs);
  582. if (ret == -EINPROGRESS) {
  583. struct ath_rx_status trs;
  584. struct ath_rxbuf *tbf;
  585. struct ath_desc *tds;
  586. memset(&trs, 0, sizeof(trs));
  587. if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
  588. sc->rx.rxlink = NULL;
  589. return NULL;
  590. }
  591. tbf = list_entry(bf->list.next, struct ath_rxbuf, list);
  592. /*
  593. * On some hardware the descriptor status words could
  594. * get corrupted, including the done bit. Because of
  595. * this, check if the next descriptor's done bit is
  596. * set or not.
  597. *
  598. * If the next descriptor's done bit is set, the current
  599. * descriptor has been corrupted. Force s/w to discard
  600. * this descriptor and continue...
  601. */
  602. tds = tbf->bf_desc;
  603. ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
  604. if (ret == -EINPROGRESS)
  605. return NULL;
  606. /*
  607. * mark descriptor as zero-length and set the 'more'
  608. * flag to ensure that both buffers get discarded
  609. */
  610. rs->rs_datalen = 0;
  611. rs->rs_more = true;
  612. }
  613. list_del(&bf->list);
  614. if (!bf->bf_mpdu)
  615. return bf;
  616. /*
  617. * Synchronize the DMA transfer with CPU before
  618. * 1. accessing the frame
  619. * 2. requeueing the same buffer to h/w
  620. */
  621. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  622. common->rx_bufsize,
  623. DMA_FROM_DEVICE);
  624. return bf;
  625. }
  626. /* Assumes you've already done the endian to CPU conversion */
  627. static bool ath9k_rx_accept(struct ath_common *common,
  628. struct ieee80211_hdr *hdr,
  629. struct ieee80211_rx_status *rxs,
  630. struct ath_rx_status *rx_stats,
  631. bool *decrypt_error)
  632. {
  633. struct ath_softc *sc = (struct ath_softc *) common->priv;
  634. bool is_mc, is_valid_tkip, strip_mic, mic_error;
  635. struct ath_hw *ah = common->ah;
  636. __le16 fc;
  637. fc = hdr->frame_control;
  638. is_mc = !!is_multicast_ether_addr(hdr->addr1);
  639. is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
  640. test_bit(rx_stats->rs_keyix, common->tkip_keymap);
  641. strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
  642. ieee80211_has_protected(fc) &&
  643. !(rx_stats->rs_status &
  644. (ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
  645. ATH9K_RXERR_KEYMISS));
  646. /*
  647. * Key miss events are only relevant for pairwise keys where the
  648. * descriptor does contain a valid key index. This has been observed
  649. * mostly with CCMP encryption.
  650. */
  651. if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID ||
  652. !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
  653. rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
  654. mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
  655. !ieee80211_has_morefrags(fc) &&
  656. !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
  657. (rx_stats->rs_status & ATH9K_RXERR_MIC);
  658. /*
  659. * The rx_stats->rs_status will not be set until the end of the
  660. * chained descriptors so it can be ignored if rs_more is set. The
  661. * rs_more will be false at the last element of the chained
  662. * descriptors.
  663. */
  664. if (rx_stats->rs_status != 0) {
  665. u8 status_mask;
  666. if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
  667. rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
  668. mic_error = false;
  669. }
  670. if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
  671. (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
  672. *decrypt_error = true;
  673. mic_error = false;
  674. }
  675. /*
  676. * Reject error frames with the exception of
  677. * decryption and MIC failures. For monitor mode,
  678. * we also ignore the CRC error.
  679. */
  680. status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
  681. ATH9K_RXERR_KEYMISS;
  682. if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
  683. status_mask |= ATH9K_RXERR_CRC;
  684. if (rx_stats->rs_status & ~status_mask)
  685. return false;
  686. }
  687. /*
  688. * For unicast frames the MIC error bit can have false positives,
  689. * so all MIC error reports need to be validated in software.
  690. * False negatives are not common, so skip software verification
  691. * if the hardware considers the MIC valid.
  692. */
  693. if (strip_mic)
  694. rxs->flag |= RX_FLAG_MMIC_STRIPPED;
  695. else if (is_mc && mic_error)
  696. rxs->flag |= RX_FLAG_MMIC_ERROR;
  697. return true;
  698. }
  699. static int ath9k_process_rate(struct ath_common *common,
  700. struct ieee80211_hw *hw,
  701. struct ath_rx_status *rx_stats,
  702. struct ieee80211_rx_status *rxs)
  703. {
  704. struct ieee80211_supported_band *sband;
  705. enum ieee80211_band band;
  706. unsigned int i = 0;
  707. struct ath_softc __maybe_unused *sc = common->priv;
  708. band = hw->conf.chandef.chan->band;
  709. sband = hw->wiphy->bands[band];
  710. switch (hw->conf.chandef.width) {
  711. case NL80211_CHAN_WIDTH_5:
  712. rxs->flag |= RX_FLAG_5MHZ;
  713. break;
  714. case NL80211_CHAN_WIDTH_10:
  715. rxs->flag |= RX_FLAG_10MHZ;
  716. break;
  717. default:
  718. break;
  719. }
  720. if (rx_stats->rs_rate & 0x80) {
  721. /* HT rate */
  722. rxs->flag |= RX_FLAG_HT;
  723. rxs->flag |= rx_stats->flag;
  724. rxs->rate_idx = rx_stats->rs_rate & 0x7f;
  725. return 0;
  726. }
  727. for (i = 0; i < sband->n_bitrates; i++) {
  728. if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
  729. rxs->rate_idx = i;
  730. return 0;
  731. }
  732. if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
  733. rxs->flag |= RX_FLAG_SHORTPRE;
  734. rxs->rate_idx = i;
  735. return 0;
  736. }
  737. }
  738. /*
  739. * No valid hardware bitrate found -- we should not get here
  740. * because hardware has already validated this frame as OK.
  741. */
  742. ath_dbg(common, ANY,
  743. "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
  744. rx_stats->rs_rate);
  745. RX_STAT_INC(rx_rate_err);
  746. return -EINVAL;
  747. }
  748. static void ath9k_process_rssi(struct ath_common *common,
  749. struct ieee80211_hw *hw,
  750. struct ath_rx_status *rx_stats,
  751. struct ieee80211_rx_status *rxs)
  752. {
  753. struct ath_softc *sc = hw->priv;
  754. struct ath_hw *ah = common->ah;
  755. int last_rssi;
  756. int rssi = rx_stats->rs_rssi;
  757. /*
  758. * RSSI is not available for subframes in an A-MPDU.
  759. */
  760. if (rx_stats->rs_moreaggr) {
  761. rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
  762. return;
  763. }
  764. /*
  765. * Check if the RSSI for the last subframe in an A-MPDU
  766. * or an unaggregated frame is valid.
  767. */
  768. if (rx_stats->rs_rssi == ATH9K_RSSI_BAD) {
  769. rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
  770. return;
  771. }
  772. /*
  773. * Update Beacon RSSI, this is used by ANI.
  774. */
  775. if (rx_stats->is_mybeacon &&
  776. ((ah->opmode == NL80211_IFTYPE_STATION) ||
  777. (ah->opmode == NL80211_IFTYPE_ADHOC))) {
  778. ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
  779. last_rssi = sc->last_rssi;
  780. if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
  781. rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
  782. if (rssi < 0)
  783. rssi = 0;
  784. ah->stats.avgbrssi = rssi;
  785. }
  786. rxs->signal = ah->noise + rx_stats->rs_rssi;
  787. }
  788. static void ath9k_process_tsf(struct ath_rx_status *rs,
  789. struct ieee80211_rx_status *rxs,
  790. u64 tsf)
  791. {
  792. u32 tsf_lower = tsf & 0xffffffff;
  793. rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp;
  794. if (rs->rs_tstamp > tsf_lower &&
  795. unlikely(rs->rs_tstamp - tsf_lower > 0x10000000))
  796. rxs->mactime -= 0x100000000ULL;
  797. if (rs->rs_tstamp < tsf_lower &&
  798. unlikely(tsf_lower - rs->rs_tstamp > 0x10000000))
  799. rxs->mactime += 0x100000000ULL;
  800. }
  801. #ifdef CONFIG_ATH9K_DEBUGFS
  802. static s8 fix_rssi_inv_only(u8 rssi_val)
  803. {
  804. if (rssi_val == 128)
  805. rssi_val = 0;
  806. return (s8) rssi_val;
  807. }
  808. #endif
  809. /* returns 1 if this was a spectral frame, even if not handled. */
  810. static int ath_process_fft(struct ath_softc *sc, struct ieee80211_hdr *hdr,
  811. struct ath_rx_status *rs, u64 tsf)
  812. {
  813. #ifdef CONFIG_ATH9K_DEBUGFS
  814. struct ath_hw *ah = sc->sc_ah;
  815. u8 num_bins, *bins, *vdata = (u8 *)hdr;
  816. struct fft_sample_ht20 fft_sample_20;
  817. struct fft_sample_ht20_40 fft_sample_40;
  818. struct fft_sample_tlv *tlv;
  819. struct ath_radar_info *radar_info;
  820. int len = rs->rs_datalen;
  821. int dc_pos;
  822. u16 fft_len, length, freq = ah->curchan->chan->center_freq;
  823. enum nl80211_channel_type chan_type;
  824. /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
  825. * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
  826. * yet, but this is supposed to be possible as well.
  827. */
  828. if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
  829. rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
  830. rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
  831. return 0;
  832. /* check if spectral scan bit is set. This does not have to be checked
  833. * if received through a SPECTRAL phy error, but shouldn't hurt.
  834. */
  835. radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
  836. if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
  837. return 0;
  838. chan_type = cfg80211_get_chandef_type(&sc->hw->conf.chandef);
  839. if ((chan_type == NL80211_CHAN_HT40MINUS) ||
  840. (chan_type == NL80211_CHAN_HT40PLUS)) {
  841. fft_len = SPECTRAL_HT20_40_TOTAL_DATA_LEN;
  842. num_bins = SPECTRAL_HT20_40_NUM_BINS;
  843. bins = (u8 *)fft_sample_40.data;
  844. } else {
  845. fft_len = SPECTRAL_HT20_TOTAL_DATA_LEN;
  846. num_bins = SPECTRAL_HT20_NUM_BINS;
  847. bins = (u8 *)fft_sample_20.data;
  848. }
  849. /* Variation in the data length is possible and will be fixed later */
  850. if ((len > fft_len + 2) || (len < fft_len - 1))
  851. return 1;
  852. switch (len - fft_len) {
  853. case 0:
  854. /* length correct, nothing to do. */
  855. memcpy(bins, vdata, num_bins);
  856. break;
  857. case -1:
  858. /* first byte missing, duplicate it. */
  859. memcpy(&bins[1], vdata, num_bins - 1);
  860. bins[0] = vdata[0];
  861. break;
  862. case 2:
  863. /* MAC added 2 extra bytes at bin 30 and 32, remove them. */
  864. memcpy(bins, vdata, 30);
  865. bins[30] = vdata[31];
  866. memcpy(&bins[31], &vdata[33], num_bins - 31);
  867. break;
  868. case 1:
  869. /* MAC added 2 extra bytes AND first byte is missing. */
  870. bins[0] = vdata[0];
  871. memcpy(&bins[1], vdata, 30);
  872. bins[31] = vdata[31];
  873. memcpy(&bins[32], &vdata[33], num_bins - 32);
  874. break;
  875. default:
  876. return 1;
  877. }
  878. /* DC value (value in the middle) is the blind spot of the spectral
  879. * sample and invalid, interpolate it.
  880. */
  881. dc_pos = num_bins / 2;
  882. bins[dc_pos] = (bins[dc_pos + 1] + bins[dc_pos - 1]) / 2;
  883. if ((chan_type == NL80211_CHAN_HT40MINUS) ||
  884. (chan_type == NL80211_CHAN_HT40PLUS)) {
  885. s8 lower_rssi, upper_rssi;
  886. s16 ext_nf;
  887. u8 lower_max_index, upper_max_index;
  888. u8 lower_bitmap_w, upper_bitmap_w;
  889. u16 lower_mag, upper_mag;
  890. struct ath9k_hw_cal_data *caldata = ah->caldata;
  891. struct ath_ht20_40_mag_info *mag_info;
  892. if (caldata)
  893. ext_nf = ath9k_hw_getchan_noise(ah, ah->curchan,
  894. caldata->nfCalHist[3].privNF);
  895. else
  896. ext_nf = ATH_DEFAULT_NOISE_FLOOR;
  897. length = sizeof(fft_sample_40) - sizeof(struct fft_sample_tlv);
  898. fft_sample_40.tlv.type = ATH_FFT_SAMPLE_HT20_40;
  899. fft_sample_40.tlv.length = __cpu_to_be16(length);
  900. fft_sample_40.freq = __cpu_to_be16(freq);
  901. fft_sample_40.channel_type = chan_type;
  902. if (chan_type == NL80211_CHAN_HT40PLUS) {
  903. lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl0);
  904. upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ext0);
  905. fft_sample_40.lower_noise = ah->noise;
  906. fft_sample_40.upper_noise = ext_nf;
  907. } else {
  908. lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ext0);
  909. upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl0);
  910. fft_sample_40.lower_noise = ext_nf;
  911. fft_sample_40.upper_noise = ah->noise;
  912. }
  913. fft_sample_40.lower_rssi = lower_rssi;
  914. fft_sample_40.upper_rssi = upper_rssi;
  915. mag_info = ((struct ath_ht20_40_mag_info *)radar_info) - 1;
  916. lower_mag = spectral_max_magnitude(mag_info->lower_bins);
  917. upper_mag = spectral_max_magnitude(mag_info->upper_bins);
  918. fft_sample_40.lower_max_magnitude = __cpu_to_be16(lower_mag);
  919. fft_sample_40.upper_max_magnitude = __cpu_to_be16(upper_mag);
  920. lower_max_index = spectral_max_index(mag_info->lower_bins);
  921. upper_max_index = spectral_max_index(mag_info->upper_bins);
  922. fft_sample_40.lower_max_index = lower_max_index;
  923. fft_sample_40.upper_max_index = upper_max_index;
  924. lower_bitmap_w = spectral_bitmap_weight(mag_info->lower_bins);
  925. upper_bitmap_w = spectral_bitmap_weight(mag_info->upper_bins);
  926. fft_sample_40.lower_bitmap_weight = lower_bitmap_w;
  927. fft_sample_40.upper_bitmap_weight = upper_bitmap_w;
  928. fft_sample_40.max_exp = mag_info->max_exp & 0xf;
  929. fft_sample_40.tsf = __cpu_to_be64(tsf);
  930. tlv = (struct fft_sample_tlv *)&fft_sample_40;
  931. } else {
  932. u8 max_index, bitmap_w;
  933. u16 magnitude;
  934. struct ath_ht20_mag_info *mag_info;
  935. length = sizeof(fft_sample_20) - sizeof(struct fft_sample_tlv);
  936. fft_sample_20.tlv.type = ATH_FFT_SAMPLE_HT20;
  937. fft_sample_20.tlv.length = __cpu_to_be16(length);
  938. fft_sample_20.freq = __cpu_to_be16(freq);
  939. fft_sample_20.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl0);
  940. fft_sample_20.noise = ah->noise;
  941. mag_info = ((struct ath_ht20_mag_info *)radar_info) - 1;
  942. magnitude = spectral_max_magnitude(mag_info->all_bins);
  943. fft_sample_20.max_magnitude = __cpu_to_be16(magnitude);
  944. max_index = spectral_max_index(mag_info->all_bins);
  945. fft_sample_20.max_index = max_index;
  946. bitmap_w = spectral_bitmap_weight(mag_info->all_bins);
  947. fft_sample_20.bitmap_weight = bitmap_w;
  948. fft_sample_20.max_exp = mag_info->max_exp & 0xf;
  949. fft_sample_20.tsf = __cpu_to_be64(tsf);
  950. tlv = (struct fft_sample_tlv *)&fft_sample_20;
  951. }
  952. ath_debug_send_fft_sample(sc, tlv);
  953. return 1;
  954. #else
  955. return 0;
  956. #endif
  957. }
  958. static bool ath9k_is_mybeacon(struct ath_softc *sc, struct ieee80211_hdr *hdr)
  959. {
  960. struct ath_hw *ah = sc->sc_ah;
  961. struct ath_common *common = ath9k_hw_common(ah);
  962. if (ieee80211_is_beacon(hdr->frame_control)) {
  963. RX_STAT_INC(rx_beacons);
  964. if (!is_zero_ether_addr(common->curbssid) &&
  965. ether_addr_equal(hdr->addr3, common->curbssid))
  966. return true;
  967. }
  968. return false;
  969. }
  970. /*
  971. * For Decrypt or Demic errors, we only mark packet status here and always push
  972. * up the frame up to let mac80211 handle the actual error case, be it no
  973. * decryption key or real decryption error. This let us keep statistics there.
  974. */
  975. static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
  976. struct sk_buff *skb,
  977. struct ath_rx_status *rx_stats,
  978. struct ieee80211_rx_status *rx_status,
  979. bool *decrypt_error, u64 tsf)
  980. {
  981. struct ieee80211_hw *hw = sc->hw;
  982. struct ath_hw *ah = sc->sc_ah;
  983. struct ath_common *common = ath9k_hw_common(ah);
  984. struct ieee80211_hdr *hdr;
  985. bool discard_current = sc->rx.discard_next;
  986. int ret = 0;
  987. /*
  988. * Discard corrupt descriptors which are marked in
  989. * ath_get_next_rx_buf().
  990. */
  991. sc->rx.discard_next = rx_stats->rs_more;
  992. if (discard_current)
  993. return -EINVAL;
  994. /*
  995. * Discard zero-length packets.
  996. */
  997. if (!rx_stats->rs_datalen) {
  998. RX_STAT_INC(rx_len_err);
  999. return -EINVAL;
  1000. }
  1001. /*
  1002. * rs_status follows rs_datalen so if rs_datalen is too large
  1003. * we can take a hint that hardware corrupted it, so ignore
  1004. * those frames.
  1005. */
  1006. if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) {
  1007. RX_STAT_INC(rx_len_err);
  1008. return -EINVAL;
  1009. }
  1010. /* Only use status info from the last fragment */
  1011. if (rx_stats->rs_more)
  1012. return 0;
  1013. /*
  1014. * Return immediately if the RX descriptor has been marked
  1015. * as corrupt based on the various error bits.
  1016. *
  1017. * This is different from the other corrupt descriptor
  1018. * condition handled above.
  1019. */
  1020. if (rx_stats->rs_status & ATH9K_RXERR_CORRUPT_DESC) {
  1021. ret = -EINVAL;
  1022. goto exit;
  1023. }
  1024. hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len);
  1025. ath9k_process_tsf(rx_stats, rx_status, tsf);
  1026. ath_debug_stat_rx(sc, rx_stats);
  1027. /*
  1028. * Process PHY errors and return so that the packet
  1029. * can be dropped.
  1030. */
  1031. if (rx_stats->rs_status & ATH9K_RXERR_PHY) {
  1032. ath9k_dfs_process_phyerr(sc, hdr, rx_stats, rx_status->mactime);
  1033. if (ath_process_fft(sc, hdr, rx_stats, rx_status->mactime))
  1034. RX_STAT_INC(rx_spectral);
  1035. ret = -EINVAL;
  1036. goto exit;
  1037. }
  1038. /*
  1039. * everything but the rate is checked here, the rate check is done
  1040. * separately to avoid doing two lookups for a rate for each frame.
  1041. */
  1042. if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error)) {
  1043. ret = -EINVAL;
  1044. goto exit;
  1045. }
  1046. rx_stats->is_mybeacon = ath9k_is_mybeacon(sc, hdr);
  1047. if (rx_stats->is_mybeacon) {
  1048. sc->hw_busy_count = 0;
  1049. ath_start_rx_poll(sc, 3);
  1050. }
  1051. if (ath9k_process_rate(common, hw, rx_stats, rx_status)) {
  1052. ret =-EINVAL;
  1053. goto exit;
  1054. }
  1055. ath9k_process_rssi(common, hw, rx_stats, rx_status);
  1056. rx_status->band = hw->conf.chandef.chan->band;
  1057. rx_status->freq = hw->conf.chandef.chan->center_freq;
  1058. rx_status->antenna = rx_stats->rs_antenna;
  1059. rx_status->flag |= RX_FLAG_MACTIME_END;
  1060. #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
  1061. if (ieee80211_is_data_present(hdr->frame_control) &&
  1062. !ieee80211_is_qos_nullfunc(hdr->frame_control))
  1063. sc->rx.num_pkts++;
  1064. #endif
  1065. exit:
  1066. sc->rx.discard_next = false;
  1067. return ret;
  1068. }
  1069. static void ath9k_rx_skb_postprocess(struct ath_common *common,
  1070. struct sk_buff *skb,
  1071. struct ath_rx_status *rx_stats,
  1072. struct ieee80211_rx_status *rxs,
  1073. bool decrypt_error)
  1074. {
  1075. struct ath_hw *ah = common->ah;
  1076. struct ieee80211_hdr *hdr;
  1077. int hdrlen, padpos, padsize;
  1078. u8 keyix;
  1079. __le16 fc;
  1080. /* see if any padding is done by the hw and remove it */
  1081. hdr = (struct ieee80211_hdr *) skb->data;
  1082. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1083. fc = hdr->frame_control;
  1084. padpos = ieee80211_hdrlen(fc);
  1085. /* The MAC header is padded to have 32-bit boundary if the
  1086. * packet payload is non-zero. The general calculation for
  1087. * padsize would take into account odd header lengths:
  1088. * padsize = (4 - padpos % 4) % 4; However, since only
  1089. * even-length headers are used, padding can only be 0 or 2
  1090. * bytes and we can optimize this a bit. In addition, we must
  1091. * not try to remove padding from short control frames that do
  1092. * not have payload. */
  1093. padsize = padpos & 3;
  1094. if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
  1095. memmove(skb->data + padsize, skb->data, padpos);
  1096. skb_pull(skb, padsize);
  1097. }
  1098. keyix = rx_stats->rs_keyix;
  1099. if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
  1100. ieee80211_has_protected(fc)) {
  1101. rxs->flag |= RX_FLAG_DECRYPTED;
  1102. } else if (ieee80211_has_protected(fc)
  1103. && !decrypt_error && skb->len >= hdrlen + 4) {
  1104. keyix = skb->data[hdrlen + 3] >> 6;
  1105. if (test_bit(keyix, common->keymap))
  1106. rxs->flag |= RX_FLAG_DECRYPTED;
  1107. }
  1108. if (ah->sw_mgmt_crypto &&
  1109. (rxs->flag & RX_FLAG_DECRYPTED) &&
  1110. ieee80211_is_mgmt(fc))
  1111. /* Use software decrypt for management frames. */
  1112. rxs->flag &= ~RX_FLAG_DECRYPTED;
  1113. }
  1114. /*
  1115. * Run the LNA combining algorithm only in these cases:
  1116. *
  1117. * Standalone WLAN cards with both LNA/Antenna diversity
  1118. * enabled in the EEPROM.
  1119. *
  1120. * WLAN+BT cards which are in the supported card list
  1121. * in ath_pci_id_table and the user has loaded the
  1122. * driver with "bt_ant_diversity" set to true.
  1123. */
  1124. static void ath9k_antenna_check(struct ath_softc *sc,
  1125. struct ath_rx_status *rs)
  1126. {
  1127. struct ath_hw *ah = sc->sc_ah;
  1128. struct ath9k_hw_capabilities *pCap = &ah->caps;
  1129. struct ath_common *common = ath9k_hw_common(ah);
  1130. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB))
  1131. return;
  1132. /*
  1133. * All MPDUs in an aggregate will use the same LNA
  1134. * as the first MPDU.
  1135. */
  1136. if (rs->rs_isaggr && !rs->rs_firstaggr)
  1137. return;
  1138. /*
  1139. * Change the default rx antenna if rx diversity
  1140. * chooses the other antenna 3 times in a row.
  1141. */
  1142. if (sc->rx.defant != rs->rs_antenna) {
  1143. if (++sc->rx.rxotherant >= 3)
  1144. ath_setdefantenna(sc, rs->rs_antenna);
  1145. } else {
  1146. sc->rx.rxotherant = 0;
  1147. }
  1148. if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) {
  1149. if (common->bt_ant_diversity)
  1150. ath_ant_comb_scan(sc, rs);
  1151. } else {
  1152. ath_ant_comb_scan(sc, rs);
  1153. }
  1154. }
  1155. static void ath9k_apply_ampdu_details(struct ath_softc *sc,
  1156. struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
  1157. {
  1158. if (rs->rs_isaggr) {
  1159. rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
  1160. rxs->ampdu_reference = sc->rx.ampdu_ref;
  1161. if (!rs->rs_moreaggr) {
  1162. rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
  1163. sc->rx.ampdu_ref++;
  1164. }
  1165. if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
  1166. rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
  1167. }
  1168. }
  1169. int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
  1170. {
  1171. struct ath_rxbuf *bf;
  1172. struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
  1173. struct ieee80211_rx_status *rxs;
  1174. struct ath_hw *ah = sc->sc_ah;
  1175. struct ath_common *common = ath9k_hw_common(ah);
  1176. struct ieee80211_hw *hw = sc->hw;
  1177. int retval;
  1178. struct ath_rx_status rs;
  1179. enum ath9k_rx_qtype qtype;
  1180. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  1181. int dma_type;
  1182. u64 tsf = 0;
  1183. unsigned long flags;
  1184. dma_addr_t new_buf_addr;
  1185. if (edma)
  1186. dma_type = DMA_BIDIRECTIONAL;
  1187. else
  1188. dma_type = DMA_FROM_DEVICE;
  1189. qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
  1190. tsf = ath9k_hw_gettsf64(ah);
  1191. do {
  1192. bool decrypt_error = false;
  1193. memset(&rs, 0, sizeof(rs));
  1194. if (edma)
  1195. bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
  1196. else
  1197. bf = ath_get_next_rx_buf(sc, &rs);
  1198. if (!bf)
  1199. break;
  1200. skb = bf->bf_mpdu;
  1201. if (!skb)
  1202. continue;
  1203. /*
  1204. * Take frame header from the first fragment and RX status from
  1205. * the last one.
  1206. */
  1207. if (sc->rx.frag)
  1208. hdr_skb = sc->rx.frag;
  1209. else
  1210. hdr_skb = skb;
  1211. rxs = IEEE80211_SKB_RXCB(hdr_skb);
  1212. memset(rxs, 0, sizeof(struct ieee80211_rx_status));
  1213. retval = ath9k_rx_skb_preprocess(sc, hdr_skb, &rs, rxs,
  1214. &decrypt_error, tsf);
  1215. if (retval)
  1216. goto requeue_drop_frag;
  1217. /* Ensure we always have an skb to requeue once we are done
  1218. * processing the current buffer's skb */
  1219. requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
  1220. /* If there is no memory we ignore the current RX'd frame,
  1221. * tell hardware it can give us a new frame using the old
  1222. * skb and put it at the tail of the sc->rx.rxbuf list for
  1223. * processing. */
  1224. if (!requeue_skb) {
  1225. RX_STAT_INC(rx_oom_err);
  1226. goto requeue_drop_frag;
  1227. }
  1228. /* We will now give hardware our shiny new allocated skb */
  1229. new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
  1230. common->rx_bufsize, dma_type);
  1231. if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
  1232. dev_kfree_skb_any(requeue_skb);
  1233. goto requeue_drop_frag;
  1234. }
  1235. /* Unmap the frame */
  1236. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  1237. common->rx_bufsize, dma_type);
  1238. bf->bf_mpdu = requeue_skb;
  1239. bf->bf_buf_addr = new_buf_addr;
  1240. skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
  1241. if (ah->caps.rx_status_len)
  1242. skb_pull(skb, ah->caps.rx_status_len);
  1243. if (!rs.rs_more)
  1244. ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
  1245. rxs, decrypt_error);
  1246. if (rs.rs_more) {
  1247. RX_STAT_INC(rx_frags);
  1248. /*
  1249. * rs_more indicates chained descriptors which can be
  1250. * used to link buffers together for a sort of
  1251. * scatter-gather operation.
  1252. */
  1253. if (sc->rx.frag) {
  1254. /* too many fragments - cannot handle frame */
  1255. dev_kfree_skb_any(sc->rx.frag);
  1256. dev_kfree_skb_any(skb);
  1257. RX_STAT_INC(rx_too_many_frags_err);
  1258. skb = NULL;
  1259. }
  1260. sc->rx.frag = skb;
  1261. goto requeue;
  1262. }
  1263. if (sc->rx.frag) {
  1264. int space = skb->len - skb_tailroom(hdr_skb);
  1265. if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
  1266. dev_kfree_skb(skb);
  1267. RX_STAT_INC(rx_oom_err);
  1268. goto requeue_drop_frag;
  1269. }
  1270. sc->rx.frag = NULL;
  1271. skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
  1272. skb->len);
  1273. dev_kfree_skb_any(skb);
  1274. skb = hdr_skb;
  1275. }
  1276. if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
  1277. skb_trim(skb, skb->len - 8);
  1278. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  1279. if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
  1280. PS_WAIT_FOR_CAB |
  1281. PS_WAIT_FOR_PSPOLL_DATA)) ||
  1282. ath9k_check_auto_sleep(sc))
  1283. ath_rx_ps(sc, skb, rs.is_mybeacon);
  1284. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  1285. ath9k_antenna_check(sc, &rs);
  1286. ath9k_apply_ampdu_details(sc, &rs, rxs);
  1287. ieee80211_rx(hw, skb);
  1288. requeue_drop_frag:
  1289. if (sc->rx.frag) {
  1290. dev_kfree_skb_any(sc->rx.frag);
  1291. sc->rx.frag = NULL;
  1292. }
  1293. requeue:
  1294. list_add_tail(&bf->list, &sc->rx.rxbuf);
  1295. if (flush)
  1296. continue;
  1297. if (edma) {
  1298. ath_rx_edma_buf_link(sc, qtype);
  1299. } else {
  1300. ath_rx_buf_relink(sc, bf);
  1301. ath9k_hw_rxena(ah);
  1302. }
  1303. } while (1);
  1304. if (!(ah->imask & ATH9K_INT_RXEOL)) {
  1305. ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  1306. ath9k_hw_set_interrupts(ah);
  1307. }
  1308. return 0;
  1309. }