recv.c 54 KB

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