recv.c 54 KB

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