recv.c 48 KB

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