recv.c 49 KB

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