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