recv.c 48 KB

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