recv.c 54 KB

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