recv.c 48 KB

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