recv.c 53 KB

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