recv.c 47 KB

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