recv.c 54 KB

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