recv.c 49 KB

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