recv.c 48 KB

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