recv.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. /*
  2. * Copyright (c) 2008-2009 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "ath9k.h"
  17. #include "ar9003_mac.h"
  18. #define SKB_CB_ATHBUF(__skb) (*((struct ath_buf **)__skb->cb))
  19. static inline bool ath_is_alt_ant_ratio_better(int alt_ratio, int maxdelta,
  20. int mindelta, int main_rssi_avg,
  21. int alt_rssi_avg, int pkt_count)
  22. {
  23. return (((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
  24. (alt_rssi_avg > main_rssi_avg + maxdelta)) ||
  25. (alt_rssi_avg > main_rssi_avg + mindelta)) && (pkt_count > 50);
  26. }
  27. static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
  28. {
  29. return sc->ps_enabled &&
  30. (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
  31. }
  32. static struct ieee80211_hw * ath_get_virt_hw(struct ath_softc *sc,
  33. struct ieee80211_hdr *hdr)
  34. {
  35. struct ieee80211_hw *hw = sc->pri_wiphy->hw;
  36. int i;
  37. spin_lock_bh(&sc->wiphy_lock);
  38. for (i = 0; i < sc->num_sec_wiphy; i++) {
  39. struct ath_wiphy *aphy = sc->sec_wiphy[i];
  40. if (aphy == NULL)
  41. continue;
  42. if (compare_ether_addr(hdr->addr1, aphy->hw->wiphy->perm_addr)
  43. == 0) {
  44. hw = aphy->hw;
  45. break;
  46. }
  47. }
  48. spin_unlock_bh(&sc->wiphy_lock);
  49. return hw;
  50. }
  51. /*
  52. * Setup and link descriptors.
  53. *
  54. * 11N: we can no longer afford to self link the last descriptor.
  55. * MAC acknowledges BA status as long as it copies frames to host
  56. * buffer (or rx fifo). This can incorrectly acknowledge packets
  57. * to a sender if last desc is self-linked.
  58. */
  59. static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
  60. {
  61. struct ath_hw *ah = sc->sc_ah;
  62. struct ath_common *common = ath9k_hw_common(ah);
  63. struct ath_desc *ds;
  64. struct sk_buff *skb;
  65. ATH_RXBUF_RESET(bf);
  66. ds = bf->bf_desc;
  67. ds->ds_link = 0; /* link to null */
  68. ds->ds_data = bf->bf_buf_addr;
  69. /* virtual addr of the beginning of the buffer. */
  70. skb = bf->bf_mpdu;
  71. BUG_ON(skb == NULL);
  72. ds->ds_vdata = skb->data;
  73. /*
  74. * setup rx descriptors. The rx_bufsize here tells the hardware
  75. * how much data it can DMA to us and that we are prepared
  76. * to process
  77. */
  78. ath9k_hw_setuprxdesc(ah, ds,
  79. common->rx_bufsize,
  80. 0);
  81. if (sc->rx.rxlink == NULL)
  82. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  83. else
  84. *sc->rx.rxlink = bf->bf_daddr;
  85. sc->rx.rxlink = &ds->ds_link;
  86. ath9k_hw_rxena(ah);
  87. }
  88. static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
  89. {
  90. /* XXX block beacon interrupts */
  91. ath9k_hw_setantenna(sc->sc_ah, antenna);
  92. sc->rx.defant = antenna;
  93. sc->rx.rxotherant = 0;
  94. }
  95. static void ath_opmode_init(struct ath_softc *sc)
  96. {
  97. struct ath_hw *ah = sc->sc_ah;
  98. struct ath_common *common = ath9k_hw_common(ah);
  99. u32 rfilt, mfilt[2];
  100. /* configure rx filter */
  101. rfilt = ath_calcrxfilter(sc);
  102. ath9k_hw_setrxfilter(ah, rfilt);
  103. /* configure bssid mask */
  104. ath_hw_setbssidmask(common);
  105. /* configure operational mode */
  106. ath9k_hw_setopmode(ah);
  107. /* calculate and install multicast filter */
  108. mfilt[0] = mfilt[1] = ~0;
  109. ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
  110. }
  111. static bool ath_rx_edma_buf_link(struct ath_softc *sc,
  112. enum ath9k_rx_qtype qtype)
  113. {
  114. struct ath_hw *ah = sc->sc_ah;
  115. struct ath_rx_edma *rx_edma;
  116. struct sk_buff *skb;
  117. struct ath_buf *bf;
  118. rx_edma = &sc->rx.rx_edma[qtype];
  119. if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
  120. return false;
  121. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  122. list_del_init(&bf->list);
  123. skb = bf->bf_mpdu;
  124. ATH_RXBUF_RESET(bf);
  125. memset(skb->data, 0, ah->caps.rx_status_len);
  126. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  127. ah->caps.rx_status_len, DMA_TO_DEVICE);
  128. SKB_CB_ATHBUF(skb) = bf;
  129. ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
  130. skb_queue_tail(&rx_edma->rx_fifo, skb);
  131. return true;
  132. }
  133. static void ath_rx_addbuffer_edma(struct ath_softc *sc,
  134. enum ath9k_rx_qtype qtype, int size)
  135. {
  136. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  137. u32 nbuf = 0;
  138. if (list_empty(&sc->rx.rxbuf)) {
  139. ath_print(common, ATH_DBG_QUEUE, "No free rx buf available\n");
  140. return;
  141. }
  142. while (!list_empty(&sc->rx.rxbuf)) {
  143. nbuf++;
  144. if (!ath_rx_edma_buf_link(sc, qtype))
  145. break;
  146. if (nbuf >= size)
  147. break;
  148. }
  149. }
  150. static void ath_rx_remove_buffer(struct ath_softc *sc,
  151. enum ath9k_rx_qtype qtype)
  152. {
  153. struct ath_buf *bf;
  154. struct ath_rx_edma *rx_edma;
  155. struct sk_buff *skb;
  156. rx_edma = &sc->rx.rx_edma[qtype];
  157. while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
  158. bf = SKB_CB_ATHBUF(skb);
  159. BUG_ON(!bf);
  160. list_add_tail(&bf->list, &sc->rx.rxbuf);
  161. }
  162. }
  163. static void ath_rx_edma_cleanup(struct ath_softc *sc)
  164. {
  165. struct ath_buf *bf;
  166. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  167. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  168. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  169. if (bf->bf_mpdu)
  170. dev_kfree_skb_any(bf->bf_mpdu);
  171. }
  172. INIT_LIST_HEAD(&sc->rx.rxbuf);
  173. kfree(sc->rx.rx_bufptr);
  174. sc->rx.rx_bufptr = NULL;
  175. }
  176. static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
  177. {
  178. skb_queue_head_init(&rx_edma->rx_fifo);
  179. skb_queue_head_init(&rx_edma->rx_buffers);
  180. rx_edma->rx_fifo_hwsize = size;
  181. }
  182. static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
  183. {
  184. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  185. struct ath_hw *ah = sc->sc_ah;
  186. struct sk_buff *skb;
  187. struct ath_buf *bf;
  188. int error = 0, i;
  189. u32 size;
  190. common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN +
  191. ah->caps.rx_status_len,
  192. min(common->cachelsz, (u16)64));
  193. ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
  194. ah->caps.rx_status_len);
  195. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
  196. ah->caps.rx_lp_qdepth);
  197. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
  198. ah->caps.rx_hp_qdepth);
  199. size = sizeof(struct ath_buf) * nbufs;
  200. bf = kzalloc(size, GFP_KERNEL);
  201. if (!bf)
  202. return -ENOMEM;
  203. INIT_LIST_HEAD(&sc->rx.rxbuf);
  204. sc->rx.rx_bufptr = bf;
  205. for (i = 0; i < nbufs; i++, bf++) {
  206. skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
  207. if (!skb) {
  208. error = -ENOMEM;
  209. goto rx_init_fail;
  210. }
  211. memset(skb->data, 0, common->rx_bufsize);
  212. bf->bf_mpdu = skb;
  213. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  214. common->rx_bufsize,
  215. DMA_BIDIRECTIONAL);
  216. if (unlikely(dma_mapping_error(sc->dev,
  217. bf->bf_buf_addr))) {
  218. dev_kfree_skb_any(skb);
  219. bf->bf_mpdu = NULL;
  220. ath_print(common, ATH_DBG_FATAL,
  221. "dma_mapping_error() on RX init\n");
  222. error = -ENOMEM;
  223. goto rx_init_fail;
  224. }
  225. list_add_tail(&bf->list, &sc->rx.rxbuf);
  226. }
  227. return 0;
  228. rx_init_fail:
  229. ath_rx_edma_cleanup(sc);
  230. return error;
  231. }
  232. static void ath_edma_start_recv(struct ath_softc *sc)
  233. {
  234. spin_lock_bh(&sc->rx.rxbuflock);
  235. ath9k_hw_rxena(sc->sc_ah);
  236. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
  237. sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
  238. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
  239. sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
  240. spin_unlock_bh(&sc->rx.rxbuflock);
  241. ath_opmode_init(sc);
  242. ath9k_hw_startpcureceive(sc->sc_ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
  243. }
  244. static void ath_edma_stop_recv(struct ath_softc *sc)
  245. {
  246. spin_lock_bh(&sc->rx.rxbuflock);
  247. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  248. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  249. spin_unlock_bh(&sc->rx.rxbuflock);
  250. }
  251. int ath_rx_init(struct ath_softc *sc, int nbufs)
  252. {
  253. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  254. struct sk_buff *skb;
  255. struct ath_buf *bf;
  256. int error = 0;
  257. spin_lock_init(&sc->rx.rxflushlock);
  258. sc->sc_flags &= ~SC_OP_RXFLUSH;
  259. spin_lock_init(&sc->rx.rxbuflock);
  260. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  261. return ath_rx_edma_init(sc, nbufs);
  262. } else {
  263. common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN,
  264. min(common->cachelsz, (u16)64));
  265. ath_print(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
  266. common->cachelsz, common->rx_bufsize);
  267. /* Initialize rx descriptors */
  268. error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
  269. "rx", nbufs, 1, 0);
  270. if (error != 0) {
  271. ath_print(common, ATH_DBG_FATAL,
  272. "failed to allocate rx descriptors: %d\n",
  273. error);
  274. goto err;
  275. }
  276. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  277. skb = ath_rxbuf_alloc(common, common->rx_bufsize,
  278. GFP_KERNEL);
  279. if (skb == NULL) {
  280. error = -ENOMEM;
  281. goto err;
  282. }
  283. bf->bf_mpdu = skb;
  284. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  285. common->rx_bufsize,
  286. DMA_FROM_DEVICE);
  287. if (unlikely(dma_mapping_error(sc->dev,
  288. bf->bf_buf_addr))) {
  289. dev_kfree_skb_any(skb);
  290. bf->bf_mpdu = NULL;
  291. ath_print(common, ATH_DBG_FATAL,
  292. "dma_mapping_error() on RX init\n");
  293. error = -ENOMEM;
  294. goto err;
  295. }
  296. bf->bf_dmacontext = bf->bf_buf_addr;
  297. }
  298. sc->rx.rxlink = NULL;
  299. }
  300. err:
  301. if (error)
  302. ath_rx_cleanup(sc);
  303. return error;
  304. }
  305. void ath_rx_cleanup(struct ath_softc *sc)
  306. {
  307. struct ath_hw *ah = sc->sc_ah;
  308. struct ath_common *common = ath9k_hw_common(ah);
  309. struct sk_buff *skb;
  310. struct ath_buf *bf;
  311. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  312. ath_rx_edma_cleanup(sc);
  313. return;
  314. } else {
  315. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  316. skb = bf->bf_mpdu;
  317. if (skb) {
  318. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  319. common->rx_bufsize,
  320. DMA_FROM_DEVICE);
  321. dev_kfree_skb(skb);
  322. }
  323. }
  324. if (sc->rx.rxdma.dd_desc_len != 0)
  325. ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
  326. }
  327. }
  328. /*
  329. * Calculate the receive filter according to the
  330. * operating mode and state:
  331. *
  332. * o always accept unicast, broadcast, and multicast traffic
  333. * o maintain current state of phy error reception (the hal
  334. * may enable phy error frames for noise immunity work)
  335. * o probe request frames are accepted only when operating in
  336. * hostap, adhoc, or monitor modes
  337. * o enable promiscuous mode according to the interface state
  338. * o accept beacons:
  339. * - when operating in adhoc mode so the 802.11 layer creates
  340. * node table entries for peers,
  341. * - when operating in station mode for collecting rssi data when
  342. * the station is otherwise quiet, or
  343. * - when operating as a repeater so we see repeater-sta beacons
  344. * - when scanning
  345. */
  346. u32 ath_calcrxfilter(struct ath_softc *sc)
  347. {
  348. #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
  349. u32 rfilt;
  350. rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
  351. | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
  352. | ATH9K_RX_FILTER_MCAST;
  353. /* If not a STA, enable processing of Probe Requests */
  354. if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
  355. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  356. /*
  357. * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
  358. * mode interface or when in monitor mode. AP mode does not need this
  359. * since it receives all in-BSS frames anyway.
  360. */
  361. if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) &&
  362. (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) ||
  363. (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR))
  364. rfilt |= ATH9K_RX_FILTER_PROM;
  365. if (sc->rx.rxfilter & FIF_CONTROL)
  366. rfilt |= ATH9K_RX_FILTER_CONTROL;
  367. if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
  368. (sc->nvifs <= 1) &&
  369. !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
  370. rfilt |= ATH9K_RX_FILTER_MYBEACON;
  371. else
  372. rfilt |= ATH9K_RX_FILTER_BEACON;
  373. if ((AR_SREV_9280_20_OR_LATER(sc->sc_ah) ||
  374. AR_SREV_9285_12_OR_LATER(sc->sc_ah)) &&
  375. (sc->sc_ah->opmode == NL80211_IFTYPE_AP) &&
  376. (sc->rx.rxfilter & FIF_PSPOLL))
  377. rfilt |= ATH9K_RX_FILTER_PSPOLL;
  378. if (conf_is_ht(&sc->hw->conf))
  379. rfilt |= ATH9K_RX_FILTER_COMP_BAR;
  380. if (sc->sec_wiphy || (sc->nvifs > 1) ||
  381. (sc->rx.rxfilter & FIF_OTHER_BSS)) {
  382. /* The following may also be needed for other older chips */
  383. if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
  384. rfilt |= ATH9K_RX_FILTER_PROM;
  385. rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
  386. }
  387. return rfilt;
  388. #undef RX_FILTER_PRESERVE
  389. }
  390. int ath_startrecv(struct ath_softc *sc)
  391. {
  392. struct ath_hw *ah = sc->sc_ah;
  393. struct ath_buf *bf, *tbf;
  394. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  395. ath_edma_start_recv(sc);
  396. return 0;
  397. }
  398. spin_lock_bh(&sc->rx.rxbuflock);
  399. if (list_empty(&sc->rx.rxbuf))
  400. goto start_recv;
  401. sc->rx.rxlink = NULL;
  402. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
  403. ath_rx_buf_link(sc, bf);
  404. }
  405. /* We could have deleted elements so the list may be empty now */
  406. if (list_empty(&sc->rx.rxbuf))
  407. goto start_recv;
  408. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  409. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  410. ath9k_hw_rxena(ah);
  411. start_recv:
  412. spin_unlock_bh(&sc->rx.rxbuflock);
  413. ath_opmode_init(sc);
  414. ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
  415. return 0;
  416. }
  417. bool ath_stoprecv(struct ath_softc *sc)
  418. {
  419. struct ath_hw *ah = sc->sc_ah;
  420. bool stopped;
  421. ath9k_hw_stoppcurecv(ah);
  422. ath9k_hw_setrxfilter(ah, 0);
  423. stopped = ath9k_hw_stopdmarecv(ah);
  424. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  425. ath_edma_stop_recv(sc);
  426. else
  427. sc->rx.rxlink = NULL;
  428. return stopped;
  429. }
  430. void ath_flushrecv(struct ath_softc *sc)
  431. {
  432. spin_lock_bh(&sc->rx.rxflushlock);
  433. sc->sc_flags |= SC_OP_RXFLUSH;
  434. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  435. ath_rx_tasklet(sc, 1, true);
  436. ath_rx_tasklet(sc, 1, false);
  437. sc->sc_flags &= ~SC_OP_RXFLUSH;
  438. spin_unlock_bh(&sc->rx.rxflushlock);
  439. }
  440. static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
  441. {
  442. /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
  443. struct ieee80211_mgmt *mgmt;
  444. u8 *pos, *end, id, elen;
  445. struct ieee80211_tim_ie *tim;
  446. mgmt = (struct ieee80211_mgmt *)skb->data;
  447. pos = mgmt->u.beacon.variable;
  448. end = skb->data + skb->len;
  449. while (pos + 2 < end) {
  450. id = *pos++;
  451. elen = *pos++;
  452. if (pos + elen > end)
  453. break;
  454. if (id == WLAN_EID_TIM) {
  455. if (elen < sizeof(*tim))
  456. break;
  457. tim = (struct ieee80211_tim_ie *) pos;
  458. if (tim->dtim_count != 0)
  459. break;
  460. return tim->bitmap_ctrl & 0x01;
  461. }
  462. pos += elen;
  463. }
  464. return false;
  465. }
  466. static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
  467. {
  468. struct ieee80211_mgmt *mgmt;
  469. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  470. if (skb->len < 24 + 8 + 2 + 2)
  471. return;
  472. mgmt = (struct ieee80211_mgmt *)skb->data;
  473. if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0)
  474. return; /* not from our current AP */
  475. sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
  476. if (sc->ps_flags & PS_BEACON_SYNC) {
  477. sc->ps_flags &= ~PS_BEACON_SYNC;
  478. ath_print(common, ATH_DBG_PS,
  479. "Reconfigure Beacon timers based on "
  480. "timestamp from the AP\n");
  481. ath_beacon_config(sc, NULL);
  482. }
  483. if (ath_beacon_dtim_pending_cab(skb)) {
  484. /*
  485. * Remain awake waiting for buffered broadcast/multicast
  486. * frames. If the last broadcast/multicast frame is not
  487. * received properly, the next beacon frame will work as
  488. * a backup trigger for returning into NETWORK SLEEP state,
  489. * so we are waiting for it as well.
  490. */
  491. ath_print(common, ATH_DBG_PS, "Received DTIM beacon indicating "
  492. "buffered broadcast/multicast frame(s)\n");
  493. sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
  494. return;
  495. }
  496. if (sc->ps_flags & PS_WAIT_FOR_CAB) {
  497. /*
  498. * This can happen if a broadcast frame is dropped or the AP
  499. * fails to send a frame indicating that all CAB frames have
  500. * been delivered.
  501. */
  502. sc->ps_flags &= ~PS_WAIT_FOR_CAB;
  503. ath_print(common, ATH_DBG_PS,
  504. "PS wait for CAB frames timed out\n");
  505. }
  506. }
  507. static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb)
  508. {
  509. struct ieee80211_hdr *hdr;
  510. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  511. hdr = (struct ieee80211_hdr *)skb->data;
  512. /* Process Beacon and CAB receive in PS state */
  513. if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
  514. && ieee80211_is_beacon(hdr->frame_control))
  515. ath_rx_ps_beacon(sc, skb);
  516. else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
  517. (ieee80211_is_data(hdr->frame_control) ||
  518. ieee80211_is_action(hdr->frame_control)) &&
  519. is_multicast_ether_addr(hdr->addr1) &&
  520. !ieee80211_has_moredata(hdr->frame_control)) {
  521. /*
  522. * No more broadcast/multicast frames to be received at this
  523. * point.
  524. */
  525. sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
  526. ath_print(common, ATH_DBG_PS,
  527. "All PS CAB frames received, back to sleep\n");
  528. } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
  529. !is_multicast_ether_addr(hdr->addr1) &&
  530. !ieee80211_has_morefrags(hdr->frame_control)) {
  531. sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
  532. ath_print(common, ATH_DBG_PS,
  533. "Going back to sleep after having received "
  534. "PS-Poll data (0x%lx)\n",
  535. sc->ps_flags & (PS_WAIT_FOR_BEACON |
  536. PS_WAIT_FOR_CAB |
  537. PS_WAIT_FOR_PSPOLL_DATA |
  538. PS_WAIT_FOR_TX_ACK));
  539. }
  540. }
  541. static void ath_rx_send_to_mac80211(struct ieee80211_hw *hw,
  542. struct ath_softc *sc, struct sk_buff *skb,
  543. struct ieee80211_rx_status *rxs)
  544. {
  545. struct ieee80211_hdr *hdr;
  546. hdr = (struct ieee80211_hdr *)skb->data;
  547. /* Send the frame to mac80211 */
  548. if (is_multicast_ether_addr(hdr->addr1)) {
  549. int i;
  550. /*
  551. * Deliver broadcast/multicast frames to all suitable
  552. * virtual wiphys.
  553. */
  554. /* TODO: filter based on channel configuration */
  555. for (i = 0; i < sc->num_sec_wiphy; i++) {
  556. struct ath_wiphy *aphy = sc->sec_wiphy[i];
  557. struct sk_buff *nskb;
  558. if (aphy == NULL)
  559. continue;
  560. nskb = skb_copy(skb, GFP_ATOMIC);
  561. if (!nskb)
  562. continue;
  563. ieee80211_rx(aphy->hw, nskb);
  564. }
  565. ieee80211_rx(sc->hw, skb);
  566. } else
  567. /* Deliver unicast frames based on receiver address */
  568. ieee80211_rx(hw, skb);
  569. }
  570. static bool ath_edma_get_buffers(struct ath_softc *sc,
  571. enum ath9k_rx_qtype qtype)
  572. {
  573. struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
  574. struct ath_hw *ah = sc->sc_ah;
  575. struct ath_common *common = ath9k_hw_common(ah);
  576. struct sk_buff *skb;
  577. struct ath_buf *bf;
  578. int ret;
  579. skb = skb_peek(&rx_edma->rx_fifo);
  580. if (!skb)
  581. return false;
  582. bf = SKB_CB_ATHBUF(skb);
  583. BUG_ON(!bf);
  584. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  585. common->rx_bufsize, DMA_FROM_DEVICE);
  586. ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
  587. if (ret == -EINPROGRESS) {
  588. /*let device gain the buffer again*/
  589. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  590. common->rx_bufsize, DMA_FROM_DEVICE);
  591. return false;
  592. }
  593. __skb_unlink(skb, &rx_edma->rx_fifo);
  594. if (ret == -EINVAL) {
  595. /* corrupt descriptor, skip this one and the following one */
  596. list_add_tail(&bf->list, &sc->rx.rxbuf);
  597. ath_rx_edma_buf_link(sc, qtype);
  598. skb = skb_peek(&rx_edma->rx_fifo);
  599. if (!skb)
  600. return true;
  601. bf = SKB_CB_ATHBUF(skb);
  602. BUG_ON(!bf);
  603. __skb_unlink(skb, &rx_edma->rx_fifo);
  604. list_add_tail(&bf->list, &sc->rx.rxbuf);
  605. ath_rx_edma_buf_link(sc, qtype);
  606. return true;
  607. }
  608. skb_queue_tail(&rx_edma->rx_buffers, skb);
  609. return true;
  610. }
  611. static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
  612. struct ath_rx_status *rs,
  613. enum ath9k_rx_qtype qtype)
  614. {
  615. struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
  616. struct sk_buff *skb;
  617. struct ath_buf *bf;
  618. while (ath_edma_get_buffers(sc, qtype));
  619. skb = __skb_dequeue(&rx_edma->rx_buffers);
  620. if (!skb)
  621. return NULL;
  622. bf = SKB_CB_ATHBUF(skb);
  623. ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data);
  624. return bf;
  625. }
  626. static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
  627. struct ath_rx_status *rs)
  628. {
  629. struct ath_hw *ah = sc->sc_ah;
  630. struct ath_common *common = ath9k_hw_common(ah);
  631. struct ath_desc *ds;
  632. struct ath_buf *bf;
  633. int ret;
  634. if (list_empty(&sc->rx.rxbuf)) {
  635. sc->rx.rxlink = NULL;
  636. return NULL;
  637. }
  638. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  639. ds = bf->bf_desc;
  640. /*
  641. * Must provide the virtual address of the current
  642. * descriptor, the physical address, and the virtual
  643. * address of the next descriptor in the h/w chain.
  644. * This allows the HAL to look ahead to see if the
  645. * hardware is done with a descriptor by checking the
  646. * done bit in the following descriptor and the address
  647. * of the current descriptor the DMA engine is working
  648. * on. All this is necessary because of our use of
  649. * a self-linked list to avoid rx overruns.
  650. */
  651. ret = ath9k_hw_rxprocdesc(ah, ds, rs, 0);
  652. if (ret == -EINPROGRESS) {
  653. struct ath_rx_status trs;
  654. struct ath_buf *tbf;
  655. struct ath_desc *tds;
  656. memset(&trs, 0, sizeof(trs));
  657. if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
  658. sc->rx.rxlink = NULL;
  659. return NULL;
  660. }
  661. tbf = list_entry(bf->list.next, struct ath_buf, list);
  662. /*
  663. * On some hardware the descriptor status words could
  664. * get corrupted, including the done bit. Because of
  665. * this, check if the next descriptor's done bit is
  666. * set or not.
  667. *
  668. * If the next descriptor's done bit is set, the current
  669. * descriptor has been corrupted. Force s/w to discard
  670. * this descriptor and continue...
  671. */
  672. tds = tbf->bf_desc;
  673. ret = ath9k_hw_rxprocdesc(ah, tds, &trs, 0);
  674. if (ret == -EINPROGRESS)
  675. return NULL;
  676. }
  677. if (!bf->bf_mpdu)
  678. return bf;
  679. /*
  680. * Synchronize the DMA transfer with CPU before
  681. * 1. accessing the frame
  682. * 2. requeueing the same buffer to h/w
  683. */
  684. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  685. common->rx_bufsize,
  686. DMA_FROM_DEVICE);
  687. return bf;
  688. }
  689. /* Assumes you've already done the endian to CPU conversion */
  690. static bool ath9k_rx_accept(struct ath_common *common,
  691. struct ieee80211_hdr *hdr,
  692. struct ieee80211_rx_status *rxs,
  693. struct ath_rx_status *rx_stats,
  694. bool *decrypt_error)
  695. {
  696. struct ath_hw *ah = common->ah;
  697. __le16 fc;
  698. u8 rx_status_len = ah->caps.rx_status_len;
  699. fc = hdr->frame_control;
  700. if (!rx_stats->rs_datalen)
  701. return false;
  702. /*
  703. * rs_status follows rs_datalen so if rs_datalen is too large
  704. * we can take a hint that hardware corrupted it, so ignore
  705. * those frames.
  706. */
  707. if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len))
  708. return false;
  709. /*
  710. * rs_more indicates chained descriptors which can be used
  711. * to link buffers together for a sort of scatter-gather
  712. * operation.
  713. * reject the frame, we don't support scatter-gather yet and
  714. * the frame is probably corrupt anyway
  715. */
  716. if (rx_stats->rs_more)
  717. return false;
  718. /*
  719. * The rx_stats->rs_status will not be set until the end of the
  720. * chained descriptors so it can be ignored if rs_more is set. The
  721. * rs_more will be false at the last element of the chained
  722. * descriptors.
  723. */
  724. if (rx_stats->rs_status != 0) {
  725. if (rx_stats->rs_status & ATH9K_RXERR_CRC)
  726. rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
  727. if (rx_stats->rs_status & ATH9K_RXERR_PHY)
  728. return false;
  729. if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
  730. *decrypt_error = true;
  731. } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
  732. /*
  733. * The MIC error bit is only valid if the frame
  734. * is not a control frame or fragment, and it was
  735. * decrypted using a valid TKIP key.
  736. */
  737. if (!ieee80211_is_ctl(fc) &&
  738. !ieee80211_has_morefrags(fc) &&
  739. !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
  740. test_bit(rx_stats->rs_keyix, common->tkip_keymap))
  741. rxs->flag |= RX_FLAG_MMIC_ERROR;
  742. else
  743. rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
  744. }
  745. /*
  746. * Reject error frames with the exception of
  747. * decryption and MIC failures. For monitor mode,
  748. * we also ignore the CRC error.
  749. */
  750. if (ah->opmode == NL80211_IFTYPE_MONITOR) {
  751. if (rx_stats->rs_status &
  752. ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
  753. ATH9K_RXERR_CRC))
  754. return false;
  755. } else {
  756. if (rx_stats->rs_status &
  757. ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
  758. return false;
  759. }
  760. }
  761. }
  762. return true;
  763. }
  764. static int ath9k_process_rate(struct ath_common *common,
  765. struct ieee80211_hw *hw,
  766. struct ath_rx_status *rx_stats,
  767. struct ieee80211_rx_status *rxs)
  768. {
  769. struct ieee80211_supported_band *sband;
  770. enum ieee80211_band band;
  771. unsigned int i = 0;
  772. band = hw->conf.channel->band;
  773. sband = hw->wiphy->bands[band];
  774. if (rx_stats->rs_rate & 0x80) {
  775. /* HT rate */
  776. rxs->flag |= RX_FLAG_HT;
  777. if (rx_stats->rs_flags & ATH9K_RX_2040)
  778. rxs->flag |= RX_FLAG_40MHZ;
  779. if (rx_stats->rs_flags & ATH9K_RX_GI)
  780. rxs->flag |= RX_FLAG_SHORT_GI;
  781. rxs->rate_idx = rx_stats->rs_rate & 0x7f;
  782. return 0;
  783. }
  784. for (i = 0; i < sband->n_bitrates; i++) {
  785. if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
  786. rxs->rate_idx = i;
  787. return 0;
  788. }
  789. if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
  790. rxs->flag |= RX_FLAG_SHORTPRE;
  791. rxs->rate_idx = i;
  792. return 0;
  793. }
  794. }
  795. /*
  796. * No valid hardware bitrate found -- we should not get here
  797. * because hardware has already validated this frame as OK.
  798. */
  799. ath_print(common, ATH_DBG_XMIT, "unsupported hw bitrate detected "
  800. "0x%02x using 1 Mbit\n", rx_stats->rs_rate);
  801. return -EINVAL;
  802. }
  803. static void ath9k_process_rssi(struct ath_common *common,
  804. struct ieee80211_hw *hw,
  805. struct ieee80211_hdr *hdr,
  806. struct ath_rx_status *rx_stats)
  807. {
  808. struct ath_hw *ah = common->ah;
  809. struct ieee80211_sta *sta;
  810. struct ath_node *an;
  811. int last_rssi = ATH_RSSI_DUMMY_MARKER;
  812. __le16 fc;
  813. fc = hdr->frame_control;
  814. rcu_read_lock();
  815. /*
  816. * XXX: use ieee80211_find_sta! This requires quite a bit of work
  817. * under the current ath9k virtual wiphy implementation as we have
  818. * no way of tying a vif to wiphy. Typically vifs are attached to
  819. * at least one sdata of a wiphy on mac80211 but with ath9k virtual
  820. * wiphy you'd have to iterate over every wiphy and each sdata.
  821. */
  822. if (is_multicast_ether_addr(hdr->addr1))
  823. sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr2, NULL);
  824. else
  825. sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr2, hdr->addr1);
  826. if (sta) {
  827. an = (struct ath_node *) sta->drv_priv;
  828. if (rx_stats->rs_rssi != ATH9K_RSSI_BAD &&
  829. !rx_stats->rs_moreaggr)
  830. ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi);
  831. last_rssi = an->last_rssi;
  832. }
  833. rcu_read_unlock();
  834. if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
  835. rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
  836. ATH_RSSI_EP_MULTIPLIER);
  837. if (rx_stats->rs_rssi < 0)
  838. rx_stats->rs_rssi = 0;
  839. /* Update Beacon RSSI, this is used by ANI. */
  840. if (ieee80211_is_beacon(fc))
  841. ah->stats.avgbrssi = rx_stats->rs_rssi;
  842. }
  843. /*
  844. * For Decrypt or Demic errors, we only mark packet status here and always push
  845. * up the frame up to let mac80211 handle the actual error case, be it no
  846. * decryption key or real decryption error. This let us keep statistics there.
  847. */
  848. static int ath9k_rx_skb_preprocess(struct ath_common *common,
  849. struct ieee80211_hw *hw,
  850. struct ieee80211_hdr *hdr,
  851. struct ath_rx_status *rx_stats,
  852. struct ieee80211_rx_status *rx_status,
  853. bool *decrypt_error)
  854. {
  855. memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
  856. /*
  857. * everything but the rate is checked here, the rate check is done
  858. * separately to avoid doing two lookups for a rate for each frame.
  859. */
  860. if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
  861. return -EINVAL;
  862. ath9k_process_rssi(common, hw, hdr, rx_stats);
  863. if (ath9k_process_rate(common, hw, rx_stats, rx_status))
  864. return -EINVAL;
  865. rx_status->band = hw->conf.channel->band;
  866. rx_status->freq = hw->conf.channel->center_freq;
  867. rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
  868. rx_status->antenna = rx_stats->rs_antenna;
  869. rx_status->flag |= RX_FLAG_TSFT;
  870. return 0;
  871. }
  872. static void ath9k_rx_skb_postprocess(struct ath_common *common,
  873. struct sk_buff *skb,
  874. struct ath_rx_status *rx_stats,
  875. struct ieee80211_rx_status *rxs,
  876. bool decrypt_error)
  877. {
  878. struct ath_hw *ah = common->ah;
  879. struct ieee80211_hdr *hdr;
  880. int hdrlen, padpos, padsize;
  881. u8 keyix;
  882. __le16 fc;
  883. /* see if any padding is done by the hw and remove it */
  884. hdr = (struct ieee80211_hdr *) skb->data;
  885. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  886. fc = hdr->frame_control;
  887. padpos = ath9k_cmn_padpos(hdr->frame_control);
  888. /* The MAC header is padded to have 32-bit boundary if the
  889. * packet payload is non-zero. The general calculation for
  890. * padsize would take into account odd header lengths:
  891. * padsize = (4 - padpos % 4) % 4; However, since only
  892. * even-length headers are used, padding can only be 0 or 2
  893. * bytes and we can optimize this a bit. In addition, we must
  894. * not try to remove padding from short control frames that do
  895. * not have payload. */
  896. padsize = padpos & 3;
  897. if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
  898. memmove(skb->data + padsize, skb->data, padpos);
  899. skb_pull(skb, padsize);
  900. }
  901. keyix = rx_stats->rs_keyix;
  902. if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
  903. ieee80211_has_protected(fc)) {
  904. rxs->flag |= RX_FLAG_DECRYPTED;
  905. } else if (ieee80211_has_protected(fc)
  906. && !decrypt_error && skb->len >= hdrlen + 4) {
  907. keyix = skb->data[hdrlen + 3] >> 6;
  908. if (test_bit(keyix, common->keymap))
  909. rxs->flag |= RX_FLAG_DECRYPTED;
  910. }
  911. if (ah->sw_mgmt_crypto &&
  912. (rxs->flag & RX_FLAG_DECRYPTED) &&
  913. ieee80211_is_mgmt(fc))
  914. /* Use software decrypt for management frames. */
  915. rxs->flag &= ~RX_FLAG_DECRYPTED;
  916. }
  917. static void ath_lnaconf_alt_good_scan(struct ath_ant_comb *antcomb,
  918. struct ath_hw_antcomb_conf ant_conf,
  919. int main_rssi_avg)
  920. {
  921. antcomb->quick_scan_cnt = 0;
  922. if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
  923. antcomb->rssi_lna2 = main_rssi_avg;
  924. else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1)
  925. antcomb->rssi_lna1 = main_rssi_avg;
  926. switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) {
  927. case (0x10): /* LNA2 A-B */
  928. antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
  929. antcomb->first_quick_scan_conf =
  930. ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
  931. antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
  932. break;
  933. case (0x20): /* LNA1 A-B */
  934. antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
  935. antcomb->first_quick_scan_conf =
  936. ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
  937. antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
  938. break;
  939. case (0x21): /* LNA1 LNA2 */
  940. antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2;
  941. antcomb->first_quick_scan_conf =
  942. ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
  943. antcomb->second_quick_scan_conf =
  944. ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
  945. break;
  946. case (0x12): /* LNA2 LNA1 */
  947. antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1;
  948. antcomb->first_quick_scan_conf =
  949. ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
  950. antcomb->second_quick_scan_conf =
  951. ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
  952. break;
  953. case (0x13): /* LNA2 A+B */
  954. antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
  955. antcomb->first_quick_scan_conf =
  956. ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
  957. antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
  958. break;
  959. case (0x23): /* LNA1 A+B */
  960. antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
  961. antcomb->first_quick_scan_conf =
  962. ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
  963. antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
  964. break;
  965. default:
  966. break;
  967. }
  968. }
  969. static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb,
  970. struct ath_hw_antcomb_conf *div_ant_conf,
  971. int main_rssi_avg, int alt_rssi_avg,
  972. int alt_ratio)
  973. {
  974. /* alt_good */
  975. switch (antcomb->quick_scan_cnt) {
  976. case 0:
  977. /* set alt to main, and alt to first conf */
  978. div_ant_conf->main_lna_conf = antcomb->main_conf;
  979. div_ant_conf->alt_lna_conf = antcomb->first_quick_scan_conf;
  980. break;
  981. case 1:
  982. /* set alt to main, and alt to first conf */
  983. div_ant_conf->main_lna_conf = antcomb->main_conf;
  984. div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf;
  985. antcomb->rssi_first = main_rssi_avg;
  986. antcomb->rssi_second = alt_rssi_avg;
  987. if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
  988. /* main is LNA1 */
  989. if (ath_is_alt_ant_ratio_better(alt_ratio,
  990. ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
  991. ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
  992. main_rssi_avg, alt_rssi_avg,
  993. antcomb->total_pkt_count))
  994. antcomb->first_ratio = true;
  995. else
  996. antcomb->first_ratio = false;
  997. } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
  998. if (ath_is_alt_ant_ratio_better(alt_ratio,
  999. ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
  1000. ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
  1001. main_rssi_avg, alt_rssi_avg,
  1002. antcomb->total_pkt_count))
  1003. antcomb->first_ratio = true;
  1004. else
  1005. antcomb->first_ratio = false;
  1006. } else {
  1007. if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
  1008. (alt_rssi_avg > main_rssi_avg +
  1009. ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
  1010. (alt_rssi_avg > main_rssi_avg)) &&
  1011. (antcomb->total_pkt_count > 50))
  1012. antcomb->first_ratio = true;
  1013. else
  1014. antcomb->first_ratio = false;
  1015. }
  1016. break;
  1017. case 2:
  1018. antcomb->alt_good = false;
  1019. antcomb->scan_not_start = false;
  1020. antcomb->scan = false;
  1021. antcomb->rssi_first = main_rssi_avg;
  1022. antcomb->rssi_third = alt_rssi_avg;
  1023. if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1)
  1024. antcomb->rssi_lna1 = alt_rssi_avg;
  1025. else if (antcomb->second_quick_scan_conf ==
  1026. ATH_ANT_DIV_COMB_LNA2)
  1027. antcomb->rssi_lna2 = alt_rssi_avg;
  1028. else if (antcomb->second_quick_scan_conf ==
  1029. ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) {
  1030. if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
  1031. antcomb->rssi_lna2 = main_rssi_avg;
  1032. else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
  1033. antcomb->rssi_lna1 = main_rssi_avg;
  1034. }
  1035. if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
  1036. ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)
  1037. div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
  1038. else
  1039. div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
  1040. if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
  1041. if (ath_is_alt_ant_ratio_better(alt_ratio,
  1042. ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
  1043. ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
  1044. main_rssi_avg, alt_rssi_avg,
  1045. antcomb->total_pkt_count))
  1046. antcomb->second_ratio = true;
  1047. else
  1048. antcomb->second_ratio = false;
  1049. } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
  1050. if (ath_is_alt_ant_ratio_better(alt_ratio,
  1051. ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
  1052. ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
  1053. main_rssi_avg, alt_rssi_avg,
  1054. antcomb->total_pkt_count))
  1055. antcomb->second_ratio = true;
  1056. else
  1057. antcomb->second_ratio = false;
  1058. } else {
  1059. if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
  1060. (alt_rssi_avg > main_rssi_avg +
  1061. ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
  1062. (alt_rssi_avg > main_rssi_avg)) &&
  1063. (antcomb->total_pkt_count > 50))
  1064. antcomb->second_ratio = true;
  1065. else
  1066. antcomb->second_ratio = false;
  1067. }
  1068. /* set alt to the conf with maximun ratio */
  1069. if (antcomb->first_ratio && antcomb->second_ratio) {
  1070. if (antcomb->rssi_second > antcomb->rssi_third) {
  1071. /* first alt*/
  1072. if ((antcomb->first_quick_scan_conf ==
  1073. ATH_ANT_DIV_COMB_LNA1) ||
  1074. (antcomb->first_quick_scan_conf ==
  1075. ATH_ANT_DIV_COMB_LNA2))
  1076. /* Set alt LNA1 or LNA2*/
  1077. if (div_ant_conf->main_lna_conf ==
  1078. ATH_ANT_DIV_COMB_LNA2)
  1079. div_ant_conf->alt_lna_conf =
  1080. ATH_ANT_DIV_COMB_LNA1;
  1081. else
  1082. div_ant_conf->alt_lna_conf =
  1083. ATH_ANT_DIV_COMB_LNA2;
  1084. else
  1085. /* Set alt to A+B or A-B */
  1086. div_ant_conf->alt_lna_conf =
  1087. antcomb->first_quick_scan_conf;
  1088. } else if ((antcomb->second_quick_scan_conf ==
  1089. ATH_ANT_DIV_COMB_LNA1) ||
  1090. (antcomb->second_quick_scan_conf ==
  1091. ATH_ANT_DIV_COMB_LNA2)) {
  1092. /* Set alt LNA1 or LNA2 */
  1093. if (div_ant_conf->main_lna_conf ==
  1094. ATH_ANT_DIV_COMB_LNA2)
  1095. div_ant_conf->alt_lna_conf =
  1096. ATH_ANT_DIV_COMB_LNA1;
  1097. else
  1098. div_ant_conf->alt_lna_conf =
  1099. ATH_ANT_DIV_COMB_LNA2;
  1100. } else {
  1101. /* Set alt to A+B or A-B */
  1102. div_ant_conf->alt_lna_conf =
  1103. antcomb->second_quick_scan_conf;
  1104. }
  1105. } else if (antcomb->first_ratio) {
  1106. /* first alt */
  1107. if ((antcomb->first_quick_scan_conf ==
  1108. ATH_ANT_DIV_COMB_LNA1) ||
  1109. (antcomb->first_quick_scan_conf ==
  1110. ATH_ANT_DIV_COMB_LNA2))
  1111. /* Set alt LNA1 or LNA2 */
  1112. if (div_ant_conf->main_lna_conf ==
  1113. ATH_ANT_DIV_COMB_LNA2)
  1114. div_ant_conf->alt_lna_conf =
  1115. ATH_ANT_DIV_COMB_LNA1;
  1116. else
  1117. div_ant_conf->alt_lna_conf =
  1118. ATH_ANT_DIV_COMB_LNA2;
  1119. else
  1120. /* Set alt to A+B or A-B */
  1121. div_ant_conf->alt_lna_conf =
  1122. antcomb->first_quick_scan_conf;
  1123. } else if (antcomb->second_ratio) {
  1124. /* second alt */
  1125. if ((antcomb->second_quick_scan_conf ==
  1126. ATH_ANT_DIV_COMB_LNA1) ||
  1127. (antcomb->second_quick_scan_conf ==
  1128. ATH_ANT_DIV_COMB_LNA2))
  1129. /* Set alt LNA1 or LNA2 */
  1130. if (div_ant_conf->main_lna_conf ==
  1131. ATH_ANT_DIV_COMB_LNA2)
  1132. div_ant_conf->alt_lna_conf =
  1133. ATH_ANT_DIV_COMB_LNA1;
  1134. else
  1135. div_ant_conf->alt_lna_conf =
  1136. ATH_ANT_DIV_COMB_LNA2;
  1137. else
  1138. /* Set alt to A+B or A-B */
  1139. div_ant_conf->alt_lna_conf =
  1140. antcomb->second_quick_scan_conf;
  1141. } else {
  1142. /* main is largest */
  1143. if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
  1144. (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
  1145. /* Set alt LNA1 or LNA2 */
  1146. if (div_ant_conf->main_lna_conf ==
  1147. ATH_ANT_DIV_COMB_LNA2)
  1148. div_ant_conf->alt_lna_conf =
  1149. ATH_ANT_DIV_COMB_LNA1;
  1150. else
  1151. div_ant_conf->alt_lna_conf =
  1152. ATH_ANT_DIV_COMB_LNA2;
  1153. else
  1154. /* Set alt to A+B or A-B */
  1155. div_ant_conf->alt_lna_conf = antcomb->main_conf;
  1156. }
  1157. break;
  1158. default:
  1159. break;
  1160. }
  1161. }
  1162. static void ath_ant_div_conf_fast_divbias(struct ath_hw_antcomb_conf *ant_conf)
  1163. {
  1164. /* Adjust the fast_div_bias based on main and alt lna conf */
  1165. switch ((ant_conf->main_lna_conf << 4) | ant_conf->alt_lna_conf) {
  1166. case (0x01): /* A-B LNA2 */
  1167. ant_conf->fast_div_bias = 0x3b;
  1168. break;
  1169. case (0x02): /* A-B LNA1 */
  1170. ant_conf->fast_div_bias = 0x3d;
  1171. break;
  1172. case (0x03): /* A-B A+B */
  1173. ant_conf->fast_div_bias = 0x1;
  1174. break;
  1175. case (0x10): /* LNA2 A-B */
  1176. ant_conf->fast_div_bias = 0x7;
  1177. break;
  1178. case (0x12): /* LNA2 LNA1 */
  1179. ant_conf->fast_div_bias = 0x2;
  1180. break;
  1181. case (0x13): /* LNA2 A+B */
  1182. ant_conf->fast_div_bias = 0x7;
  1183. break;
  1184. case (0x20): /* LNA1 A-B */
  1185. ant_conf->fast_div_bias = 0x6;
  1186. break;
  1187. case (0x21): /* LNA1 LNA2 */
  1188. ant_conf->fast_div_bias = 0x0;
  1189. break;
  1190. case (0x23): /* LNA1 A+B */
  1191. ant_conf->fast_div_bias = 0x6;
  1192. break;
  1193. case (0x30): /* A+B A-B */
  1194. ant_conf->fast_div_bias = 0x1;
  1195. break;
  1196. case (0x31): /* A+B LNA2 */
  1197. ant_conf->fast_div_bias = 0x3b;
  1198. break;
  1199. case (0x32): /* A+B LNA1 */
  1200. ant_conf->fast_div_bias = 0x3d;
  1201. break;
  1202. default:
  1203. break;
  1204. }
  1205. }
  1206. /* Antenna diversity and combining */
  1207. static void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
  1208. {
  1209. struct ath_hw_antcomb_conf div_ant_conf;
  1210. struct ath_ant_comb *antcomb = &sc->ant_comb;
  1211. int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
  1212. int curr_main_set, curr_bias;
  1213. int main_rssi = rs->rs_rssi_ctl0;
  1214. int alt_rssi = rs->rs_rssi_ctl1;
  1215. int rx_ant_conf, main_ant_conf;
  1216. bool short_scan = false;
  1217. rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
  1218. ATH_ANT_RX_MASK;
  1219. main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
  1220. ATH_ANT_RX_MASK;
  1221. /* Record packet only when alt_rssi is positive */
  1222. if (alt_rssi > 0) {
  1223. antcomb->total_pkt_count++;
  1224. antcomb->main_total_rssi += main_rssi;
  1225. antcomb->alt_total_rssi += alt_rssi;
  1226. if (main_ant_conf == rx_ant_conf)
  1227. antcomb->main_recv_cnt++;
  1228. else
  1229. antcomb->alt_recv_cnt++;
  1230. }
  1231. /* Short scan check */
  1232. if (antcomb->scan && antcomb->alt_good) {
  1233. if (time_after(jiffies, antcomb->scan_start_time +
  1234. msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
  1235. short_scan = true;
  1236. else
  1237. if (antcomb->total_pkt_count ==
  1238. ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
  1239. alt_ratio = ((antcomb->alt_recv_cnt * 100) /
  1240. antcomb->total_pkt_count);
  1241. if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
  1242. short_scan = true;
  1243. }
  1244. }
  1245. if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
  1246. rs->rs_moreaggr) && !short_scan)
  1247. return;
  1248. if (antcomb->total_pkt_count) {
  1249. alt_ratio = ((antcomb->alt_recv_cnt * 100) /
  1250. antcomb->total_pkt_count);
  1251. main_rssi_avg = (antcomb->main_total_rssi /
  1252. antcomb->total_pkt_count);
  1253. alt_rssi_avg = (antcomb->alt_total_rssi /
  1254. antcomb->total_pkt_count);
  1255. }
  1256. ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
  1257. curr_alt_set = div_ant_conf.alt_lna_conf;
  1258. curr_main_set = div_ant_conf.main_lna_conf;
  1259. curr_bias = div_ant_conf.fast_div_bias;
  1260. antcomb->count++;
  1261. if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
  1262. if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
  1263. ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
  1264. main_rssi_avg);
  1265. antcomb->alt_good = true;
  1266. } else {
  1267. antcomb->alt_good = false;
  1268. }
  1269. antcomb->count = 0;
  1270. antcomb->scan = true;
  1271. antcomb->scan_not_start = true;
  1272. }
  1273. if (!antcomb->scan) {
  1274. if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
  1275. if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
  1276. /* Switch main and alt LNA */
  1277. div_ant_conf.main_lna_conf =
  1278. ATH_ANT_DIV_COMB_LNA2;
  1279. div_ant_conf.alt_lna_conf =
  1280. ATH_ANT_DIV_COMB_LNA1;
  1281. } else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
  1282. div_ant_conf.main_lna_conf =
  1283. ATH_ANT_DIV_COMB_LNA1;
  1284. div_ant_conf.alt_lna_conf =
  1285. ATH_ANT_DIV_COMB_LNA2;
  1286. }
  1287. goto div_comb_done;
  1288. } else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
  1289. (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
  1290. /* Set alt to another LNA */
  1291. if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
  1292. div_ant_conf.alt_lna_conf =
  1293. ATH_ANT_DIV_COMB_LNA1;
  1294. else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
  1295. div_ant_conf.alt_lna_conf =
  1296. ATH_ANT_DIV_COMB_LNA2;
  1297. goto div_comb_done;
  1298. }
  1299. if ((alt_rssi_avg < (main_rssi_avg +
  1300. ATH_ANT_DIV_COMB_LNA1_LNA2_DELTA)))
  1301. goto div_comb_done;
  1302. }
  1303. if (!antcomb->scan_not_start) {
  1304. switch (curr_alt_set) {
  1305. case ATH_ANT_DIV_COMB_LNA2:
  1306. antcomb->rssi_lna2 = alt_rssi_avg;
  1307. antcomb->rssi_lna1 = main_rssi_avg;
  1308. antcomb->scan = true;
  1309. /* set to A+B */
  1310. div_ant_conf.main_lna_conf =
  1311. ATH_ANT_DIV_COMB_LNA1;
  1312. div_ant_conf.alt_lna_conf =
  1313. ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
  1314. break;
  1315. case ATH_ANT_DIV_COMB_LNA1:
  1316. antcomb->rssi_lna1 = alt_rssi_avg;
  1317. antcomb->rssi_lna2 = main_rssi_avg;
  1318. antcomb->scan = true;
  1319. /* set to A+B */
  1320. div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
  1321. div_ant_conf.alt_lna_conf =
  1322. ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
  1323. break;
  1324. case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
  1325. antcomb->rssi_add = alt_rssi_avg;
  1326. antcomb->scan = true;
  1327. /* set to A-B */
  1328. div_ant_conf.alt_lna_conf =
  1329. ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
  1330. break;
  1331. case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
  1332. antcomb->rssi_sub = alt_rssi_avg;
  1333. antcomb->scan = false;
  1334. if (antcomb->rssi_lna2 >
  1335. (antcomb->rssi_lna1 +
  1336. ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
  1337. /* use LNA2 as main LNA */
  1338. if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
  1339. (antcomb->rssi_add > antcomb->rssi_sub)) {
  1340. /* set to A+B */
  1341. div_ant_conf.main_lna_conf =
  1342. ATH_ANT_DIV_COMB_LNA2;
  1343. div_ant_conf.alt_lna_conf =
  1344. ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
  1345. } else if (antcomb->rssi_sub >
  1346. antcomb->rssi_lna1) {
  1347. /* set to A-B */
  1348. div_ant_conf.main_lna_conf =
  1349. ATH_ANT_DIV_COMB_LNA2;
  1350. div_ant_conf.alt_lna_conf =
  1351. ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
  1352. } else {
  1353. /* set to LNA1 */
  1354. div_ant_conf.main_lna_conf =
  1355. ATH_ANT_DIV_COMB_LNA2;
  1356. div_ant_conf.alt_lna_conf =
  1357. ATH_ANT_DIV_COMB_LNA1;
  1358. }
  1359. } else {
  1360. /* use LNA1 as main LNA */
  1361. if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
  1362. (antcomb->rssi_add > antcomb->rssi_sub)) {
  1363. /* set to A+B */
  1364. div_ant_conf.main_lna_conf =
  1365. ATH_ANT_DIV_COMB_LNA1;
  1366. div_ant_conf.alt_lna_conf =
  1367. ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
  1368. } else if (antcomb->rssi_sub >
  1369. antcomb->rssi_lna1) {
  1370. /* set to A-B */
  1371. div_ant_conf.main_lna_conf =
  1372. ATH_ANT_DIV_COMB_LNA1;
  1373. div_ant_conf.alt_lna_conf =
  1374. ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
  1375. } else {
  1376. /* set to LNA2 */
  1377. div_ant_conf.main_lna_conf =
  1378. ATH_ANT_DIV_COMB_LNA1;
  1379. div_ant_conf.alt_lna_conf =
  1380. ATH_ANT_DIV_COMB_LNA2;
  1381. }
  1382. }
  1383. break;
  1384. default:
  1385. break;
  1386. }
  1387. } else {
  1388. if (!antcomb->alt_good) {
  1389. antcomb->scan_not_start = false;
  1390. /* Set alt to another LNA */
  1391. if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
  1392. div_ant_conf.main_lna_conf =
  1393. ATH_ANT_DIV_COMB_LNA2;
  1394. div_ant_conf.alt_lna_conf =
  1395. ATH_ANT_DIV_COMB_LNA1;
  1396. } else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
  1397. div_ant_conf.main_lna_conf =
  1398. ATH_ANT_DIV_COMB_LNA1;
  1399. div_ant_conf.alt_lna_conf =
  1400. ATH_ANT_DIV_COMB_LNA2;
  1401. }
  1402. goto div_comb_done;
  1403. }
  1404. }
  1405. ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
  1406. main_rssi_avg, alt_rssi_avg,
  1407. alt_ratio);
  1408. antcomb->quick_scan_cnt++;
  1409. div_comb_done:
  1410. ath_ant_div_conf_fast_divbias(&div_ant_conf);
  1411. ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
  1412. antcomb->scan_start_time = jiffies;
  1413. antcomb->total_pkt_count = 0;
  1414. antcomb->main_total_rssi = 0;
  1415. antcomb->alt_total_rssi = 0;
  1416. antcomb->main_recv_cnt = 0;
  1417. antcomb->alt_recv_cnt = 0;
  1418. }
  1419. int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
  1420. {
  1421. struct ath_buf *bf;
  1422. struct sk_buff *skb = NULL, *requeue_skb;
  1423. struct ieee80211_rx_status *rxs;
  1424. struct ath_hw *ah = sc->sc_ah;
  1425. struct ath_common *common = ath9k_hw_common(ah);
  1426. /*
  1427. * The hw can techncically differ from common->hw when using ath9k
  1428. * virtual wiphy so to account for that we iterate over the active
  1429. * wiphys and find the appropriate wiphy and therefore hw.
  1430. */
  1431. struct ieee80211_hw *hw = NULL;
  1432. struct ieee80211_hdr *hdr;
  1433. int retval;
  1434. bool decrypt_error = false;
  1435. struct ath_rx_status rs;
  1436. enum ath9k_rx_qtype qtype;
  1437. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  1438. int dma_type;
  1439. u8 rx_status_len = ah->caps.rx_status_len;
  1440. u64 tsf = 0;
  1441. u32 tsf_lower = 0;
  1442. unsigned long flags;
  1443. if (edma)
  1444. dma_type = DMA_BIDIRECTIONAL;
  1445. else
  1446. dma_type = DMA_FROM_DEVICE;
  1447. qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
  1448. spin_lock_bh(&sc->rx.rxbuflock);
  1449. tsf = ath9k_hw_gettsf64(ah);
  1450. tsf_lower = tsf & 0xffffffff;
  1451. do {
  1452. /* If handling rx interrupt and flush is in progress => exit */
  1453. if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
  1454. break;
  1455. memset(&rs, 0, sizeof(rs));
  1456. if (edma)
  1457. bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
  1458. else
  1459. bf = ath_get_next_rx_buf(sc, &rs);
  1460. if (!bf)
  1461. break;
  1462. skb = bf->bf_mpdu;
  1463. if (!skb)
  1464. continue;
  1465. hdr = (struct ieee80211_hdr *) (skb->data + rx_status_len);
  1466. rxs = IEEE80211_SKB_RXCB(skb);
  1467. hw = ath_get_virt_hw(sc, hdr);
  1468. ath_debug_stat_rx(sc, &rs);
  1469. /*
  1470. * If we're asked to flush receive queue, directly
  1471. * chain it back at the queue without processing it.
  1472. */
  1473. if (flush)
  1474. goto requeue;
  1475. retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
  1476. rxs, &decrypt_error);
  1477. if (retval)
  1478. goto requeue;
  1479. rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
  1480. if (rs.rs_tstamp > tsf_lower &&
  1481. unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
  1482. rxs->mactime -= 0x100000000ULL;
  1483. if (rs.rs_tstamp < tsf_lower &&
  1484. unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
  1485. rxs->mactime += 0x100000000ULL;
  1486. /* Ensure we always have an skb to requeue once we are done
  1487. * processing the current buffer's skb */
  1488. requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
  1489. /* If there is no memory we ignore the current RX'd frame,
  1490. * tell hardware it can give us a new frame using the old
  1491. * skb and put it at the tail of the sc->rx.rxbuf list for
  1492. * processing. */
  1493. if (!requeue_skb)
  1494. goto requeue;
  1495. /* Unmap the frame */
  1496. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  1497. common->rx_bufsize,
  1498. dma_type);
  1499. skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
  1500. if (ah->caps.rx_status_len)
  1501. skb_pull(skb, ah->caps.rx_status_len);
  1502. ath9k_rx_skb_postprocess(common, skb, &rs,
  1503. rxs, decrypt_error);
  1504. /* We will now give hardware our shiny new allocated skb */
  1505. bf->bf_mpdu = requeue_skb;
  1506. bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
  1507. common->rx_bufsize,
  1508. dma_type);
  1509. if (unlikely(dma_mapping_error(sc->dev,
  1510. bf->bf_buf_addr))) {
  1511. dev_kfree_skb_any(requeue_skb);
  1512. bf->bf_mpdu = NULL;
  1513. ath_print(common, ATH_DBG_FATAL,
  1514. "dma_mapping_error() on RX\n");
  1515. ath_rx_send_to_mac80211(hw, sc, skb, rxs);
  1516. break;
  1517. }
  1518. bf->bf_dmacontext = bf->bf_buf_addr;
  1519. /*
  1520. * change the default rx antenna if rx diversity chooses the
  1521. * other antenna 3 times in a row.
  1522. */
  1523. if (sc->rx.defant != rs.rs_antenna) {
  1524. if (++sc->rx.rxotherant >= 3)
  1525. ath_setdefantenna(sc, rs.rs_antenna);
  1526. } else {
  1527. sc->rx.rxotherant = 0;
  1528. }
  1529. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  1530. if (unlikely(ath9k_check_auto_sleep(sc) ||
  1531. (sc->ps_flags & (PS_WAIT_FOR_BEACON |
  1532. PS_WAIT_FOR_CAB |
  1533. PS_WAIT_FOR_PSPOLL_DATA))))
  1534. ath_rx_ps(sc, skb);
  1535. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  1536. if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
  1537. ath_ant_comb_scan(sc, &rs);
  1538. ath_rx_send_to_mac80211(hw, sc, skb, rxs);
  1539. requeue:
  1540. if (edma) {
  1541. list_add_tail(&bf->list, &sc->rx.rxbuf);
  1542. ath_rx_edma_buf_link(sc, qtype);
  1543. } else {
  1544. list_move_tail(&bf->list, &sc->rx.rxbuf);
  1545. ath_rx_buf_link(sc, bf);
  1546. }
  1547. } while (1);
  1548. spin_unlock_bh(&sc->rx.rxbuflock);
  1549. return 0;
  1550. }