recv.c 48 KB

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