recv.c 54 KB

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