recv.c 54 KB

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