recv.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. /*
  2. * Copyright (c) 2008-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/dma-mapping.h>
  17. #include <linux/relay.h>
  18. #include "ath9k.h"
  19. #include "ar9003_mac.h"
  20. #define SKB_CB_ATHBUF(__skb) (*((struct ath_buf **)__skb->cb))
  21. static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
  22. {
  23. return sc->ps_enabled &&
  24. (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
  25. }
  26. /*
  27. * Setup and link descriptors.
  28. *
  29. * 11N: we can no longer afford to self link the last descriptor.
  30. * MAC acknowledges BA status as long as it copies frames to host
  31. * buffer (or rx fifo). This can incorrectly acknowledge packets
  32. * to a sender if last desc is self-linked.
  33. */
  34. static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
  35. {
  36. struct ath_hw *ah = sc->sc_ah;
  37. struct ath_common *common = ath9k_hw_common(ah);
  38. struct ath_desc *ds;
  39. struct sk_buff *skb;
  40. ATH_RXBUF_RESET(bf);
  41. ds = bf->bf_desc;
  42. ds->ds_link = 0; /* link to null */
  43. ds->ds_data = bf->bf_buf_addr;
  44. /* virtual addr of the beginning of the buffer. */
  45. skb = bf->bf_mpdu;
  46. BUG_ON(skb == NULL);
  47. ds->ds_vdata = skb->data;
  48. /*
  49. * setup rx descriptors. The rx_bufsize here tells the hardware
  50. * how much data it can DMA to us and that we are prepared
  51. * to process
  52. */
  53. ath9k_hw_setuprxdesc(ah, ds,
  54. common->rx_bufsize,
  55. 0);
  56. if (sc->rx.rxlink == NULL)
  57. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  58. else
  59. *sc->rx.rxlink = bf->bf_daddr;
  60. sc->rx.rxlink = &ds->ds_link;
  61. }
  62. static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
  63. {
  64. /* XXX block beacon interrupts */
  65. ath9k_hw_setantenna(sc->sc_ah, antenna);
  66. sc->rx.defant = antenna;
  67. sc->rx.rxotherant = 0;
  68. }
  69. static void ath_opmode_init(struct ath_softc *sc)
  70. {
  71. struct ath_hw *ah = sc->sc_ah;
  72. struct ath_common *common = ath9k_hw_common(ah);
  73. u32 rfilt, mfilt[2];
  74. /* configure rx filter */
  75. rfilt = ath_calcrxfilter(sc);
  76. ath9k_hw_setrxfilter(ah, rfilt);
  77. /* configure bssid mask */
  78. ath_hw_setbssidmask(common);
  79. /* configure operational mode */
  80. ath9k_hw_setopmode(ah);
  81. /* calculate and install multicast filter */
  82. mfilt[0] = mfilt[1] = ~0;
  83. ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
  84. }
  85. static bool ath_rx_edma_buf_link(struct ath_softc *sc,
  86. enum ath9k_rx_qtype qtype)
  87. {
  88. struct ath_hw *ah = sc->sc_ah;
  89. struct ath_rx_edma *rx_edma;
  90. struct sk_buff *skb;
  91. struct ath_buf *bf;
  92. rx_edma = &sc->rx.rx_edma[qtype];
  93. if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
  94. return false;
  95. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  96. list_del_init(&bf->list);
  97. skb = bf->bf_mpdu;
  98. ATH_RXBUF_RESET(bf);
  99. memset(skb->data, 0, ah->caps.rx_status_len);
  100. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  101. ah->caps.rx_status_len, DMA_TO_DEVICE);
  102. SKB_CB_ATHBUF(skb) = bf;
  103. ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
  104. skb_queue_tail(&rx_edma->rx_fifo, skb);
  105. return true;
  106. }
  107. static void ath_rx_addbuffer_edma(struct ath_softc *sc,
  108. enum ath9k_rx_qtype qtype, int size)
  109. {
  110. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  111. struct ath_buf *bf, *tbf;
  112. if (list_empty(&sc->rx.rxbuf)) {
  113. ath_dbg(common, QUEUE, "No free rx buf available\n");
  114. return;
  115. }
  116. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
  117. if (!ath_rx_edma_buf_link(sc, qtype))
  118. break;
  119. }
  120. static void ath_rx_remove_buffer(struct ath_softc *sc,
  121. enum ath9k_rx_qtype qtype)
  122. {
  123. struct ath_buf *bf;
  124. struct ath_rx_edma *rx_edma;
  125. struct sk_buff *skb;
  126. rx_edma = &sc->rx.rx_edma[qtype];
  127. while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
  128. bf = SKB_CB_ATHBUF(skb);
  129. BUG_ON(!bf);
  130. list_add_tail(&bf->list, &sc->rx.rxbuf);
  131. }
  132. }
  133. static void ath_rx_edma_cleanup(struct ath_softc *sc)
  134. {
  135. struct ath_hw *ah = sc->sc_ah;
  136. struct ath_common *common = ath9k_hw_common(ah);
  137. struct ath_buf *bf;
  138. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  139. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  140. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  141. if (bf->bf_mpdu) {
  142. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  143. common->rx_bufsize,
  144. DMA_BIDIRECTIONAL);
  145. dev_kfree_skb_any(bf->bf_mpdu);
  146. bf->bf_buf_addr = 0;
  147. bf->bf_mpdu = NULL;
  148. }
  149. }
  150. }
  151. static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
  152. {
  153. skb_queue_head_init(&rx_edma->rx_fifo);
  154. rx_edma->rx_fifo_hwsize = size;
  155. }
  156. static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
  157. {
  158. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  159. struct ath_hw *ah = sc->sc_ah;
  160. struct sk_buff *skb;
  161. struct ath_buf *bf;
  162. int error = 0, i;
  163. u32 size;
  164. ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
  165. ah->caps.rx_status_len);
  166. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
  167. ah->caps.rx_lp_qdepth);
  168. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
  169. ah->caps.rx_hp_qdepth);
  170. size = sizeof(struct ath_buf) * nbufs;
  171. bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
  172. if (!bf)
  173. return -ENOMEM;
  174. INIT_LIST_HEAD(&sc->rx.rxbuf);
  175. for (i = 0; i < nbufs; i++, bf++) {
  176. skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
  177. if (!skb) {
  178. error = -ENOMEM;
  179. goto rx_init_fail;
  180. }
  181. memset(skb->data, 0, common->rx_bufsize);
  182. bf->bf_mpdu = skb;
  183. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  184. common->rx_bufsize,
  185. DMA_BIDIRECTIONAL);
  186. if (unlikely(dma_mapping_error(sc->dev,
  187. bf->bf_buf_addr))) {
  188. dev_kfree_skb_any(skb);
  189. bf->bf_mpdu = NULL;
  190. bf->bf_buf_addr = 0;
  191. ath_err(common,
  192. "dma_mapping_error() on RX init\n");
  193. error = -ENOMEM;
  194. goto rx_init_fail;
  195. }
  196. list_add_tail(&bf->list, &sc->rx.rxbuf);
  197. }
  198. return 0;
  199. rx_init_fail:
  200. ath_rx_edma_cleanup(sc);
  201. return error;
  202. }
  203. static void ath_edma_start_recv(struct ath_softc *sc)
  204. {
  205. ath9k_hw_rxena(sc->sc_ah);
  206. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
  207. sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
  208. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
  209. sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
  210. ath_opmode_init(sc);
  211. ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
  212. }
  213. static void ath_edma_stop_recv(struct ath_softc *sc)
  214. {
  215. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  216. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  217. }
  218. int ath_rx_init(struct ath_softc *sc, int nbufs)
  219. {
  220. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  221. struct sk_buff *skb;
  222. struct ath_buf *bf;
  223. int error = 0;
  224. spin_lock_init(&sc->sc_pcu_lock);
  225. common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
  226. sc->sc_ah->caps.rx_status_len;
  227. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  228. return ath_rx_edma_init(sc, nbufs);
  229. } else {
  230. ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
  231. common->cachelsz, common->rx_bufsize);
  232. /* Initialize rx descriptors */
  233. error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
  234. "rx", nbufs, 1, 0);
  235. if (error != 0) {
  236. ath_err(common,
  237. "failed to allocate rx descriptors: %d\n",
  238. error);
  239. goto err;
  240. }
  241. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  242. skb = ath_rxbuf_alloc(common, common->rx_bufsize,
  243. GFP_KERNEL);
  244. if (skb == NULL) {
  245. error = -ENOMEM;
  246. goto err;
  247. }
  248. bf->bf_mpdu = skb;
  249. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  250. common->rx_bufsize,
  251. DMA_FROM_DEVICE);
  252. if (unlikely(dma_mapping_error(sc->dev,
  253. bf->bf_buf_addr))) {
  254. dev_kfree_skb_any(skb);
  255. bf->bf_mpdu = NULL;
  256. bf->bf_buf_addr = 0;
  257. ath_err(common,
  258. "dma_mapping_error() on RX init\n");
  259. error = -ENOMEM;
  260. goto err;
  261. }
  262. }
  263. sc->rx.rxlink = NULL;
  264. }
  265. err:
  266. if (error)
  267. ath_rx_cleanup(sc);
  268. return error;
  269. }
  270. void ath_rx_cleanup(struct ath_softc *sc)
  271. {
  272. struct ath_hw *ah = sc->sc_ah;
  273. struct ath_common *common = ath9k_hw_common(ah);
  274. struct sk_buff *skb;
  275. struct ath_buf *bf;
  276. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  277. ath_rx_edma_cleanup(sc);
  278. return;
  279. } else {
  280. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  281. skb = bf->bf_mpdu;
  282. if (skb) {
  283. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  284. common->rx_bufsize,
  285. DMA_FROM_DEVICE);
  286. dev_kfree_skb(skb);
  287. bf->bf_buf_addr = 0;
  288. bf->bf_mpdu = NULL;
  289. }
  290. }
  291. }
  292. }
  293. /*
  294. * Calculate the receive filter according to the
  295. * operating mode and state:
  296. *
  297. * o always accept unicast, broadcast, and multicast traffic
  298. * o maintain current state of phy error reception (the hal
  299. * may enable phy error frames for noise immunity work)
  300. * o probe request frames are accepted only when operating in
  301. * hostap, adhoc, or monitor modes
  302. * o enable promiscuous mode according to the interface state
  303. * o accept beacons:
  304. * - when operating in adhoc mode so the 802.11 layer creates
  305. * node table entries for peers,
  306. * - when operating in station mode for collecting rssi data when
  307. * the station is otherwise quiet, or
  308. * - when operating as a repeater so we see repeater-sta beacons
  309. * - when scanning
  310. */
  311. u32 ath_calcrxfilter(struct ath_softc *sc)
  312. {
  313. u32 rfilt;
  314. rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
  315. | ATH9K_RX_FILTER_MCAST;
  316. /* if operating on a DFS channel, enable radar pulse detection */
  317. if (sc->hw->conf.radar_enabled)
  318. rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
  319. if (sc->rx.rxfilter & FIF_PROBE_REQ)
  320. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  321. /*
  322. * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
  323. * mode interface or when in monitor mode. AP mode does not need this
  324. * since it receives all in-BSS frames anyway.
  325. */
  326. if (sc->sc_ah->is_monitoring)
  327. rfilt |= ATH9K_RX_FILTER_PROM;
  328. if (sc->rx.rxfilter & FIF_CONTROL)
  329. rfilt |= ATH9K_RX_FILTER_CONTROL;
  330. if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
  331. (sc->nvifs <= 1) &&
  332. !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
  333. rfilt |= ATH9K_RX_FILTER_MYBEACON;
  334. else
  335. rfilt |= ATH9K_RX_FILTER_BEACON;
  336. if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
  337. (sc->rx.rxfilter & FIF_PSPOLL))
  338. rfilt |= ATH9K_RX_FILTER_PSPOLL;
  339. if (conf_is_ht(&sc->hw->conf))
  340. rfilt |= ATH9K_RX_FILTER_COMP_BAR;
  341. if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
  342. /* This is needed for older chips */
  343. if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
  344. rfilt |= ATH9K_RX_FILTER_PROM;
  345. rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
  346. }
  347. if (AR_SREV_9550(sc->sc_ah))
  348. rfilt |= ATH9K_RX_FILTER_4ADDRESS;
  349. return rfilt;
  350. }
  351. int ath_startrecv(struct ath_softc *sc)
  352. {
  353. struct ath_hw *ah = sc->sc_ah;
  354. struct ath_buf *bf, *tbf;
  355. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  356. ath_edma_start_recv(sc);
  357. return 0;
  358. }
  359. if (list_empty(&sc->rx.rxbuf))
  360. goto start_recv;
  361. sc->rx.rxlink = NULL;
  362. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
  363. ath_rx_buf_link(sc, bf);
  364. }
  365. /* We could have deleted elements so the list may be empty now */
  366. if (list_empty(&sc->rx.rxbuf))
  367. goto start_recv;
  368. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  369. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  370. ath9k_hw_rxena(ah);
  371. start_recv:
  372. ath_opmode_init(sc);
  373. ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
  374. return 0;
  375. }
  376. static void ath_flushrecv(struct ath_softc *sc)
  377. {
  378. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  379. ath_rx_tasklet(sc, 1, true);
  380. ath_rx_tasklet(sc, 1, false);
  381. }
  382. bool ath_stoprecv(struct ath_softc *sc)
  383. {
  384. struct ath_hw *ah = sc->sc_ah;
  385. bool stopped, reset = false;
  386. ath9k_hw_abortpcurecv(ah);
  387. ath9k_hw_setrxfilter(ah, 0);
  388. stopped = ath9k_hw_stopdmarecv(ah, &reset);
  389. ath_flushrecv(sc);
  390. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  391. ath_edma_stop_recv(sc);
  392. else
  393. sc->rx.rxlink = NULL;
  394. if (!(ah->ah_flags & AH_UNPLUGGED) &&
  395. unlikely(!stopped)) {
  396. ath_err(ath9k_hw_common(sc->sc_ah),
  397. "Could not stop RX, we could be "
  398. "confusing the DMA engine when we start RX up\n");
  399. ATH_DBG_WARN_ON_ONCE(!stopped);
  400. }
  401. return stopped && !reset;
  402. }
  403. static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
  404. {
  405. /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
  406. struct ieee80211_mgmt *mgmt;
  407. u8 *pos, *end, id, elen;
  408. struct ieee80211_tim_ie *tim;
  409. mgmt = (struct ieee80211_mgmt *)skb->data;
  410. pos = mgmt->u.beacon.variable;
  411. end = skb->data + skb->len;
  412. while (pos + 2 < end) {
  413. id = *pos++;
  414. elen = *pos++;
  415. if (pos + elen > end)
  416. break;
  417. if (id == WLAN_EID_TIM) {
  418. if (elen < sizeof(*tim))
  419. break;
  420. tim = (struct ieee80211_tim_ie *) pos;
  421. if (tim->dtim_count != 0)
  422. break;
  423. return tim->bitmap_ctrl & 0x01;
  424. }
  425. pos += elen;
  426. }
  427. return false;
  428. }
  429. static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
  430. {
  431. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  432. if (skb->len < 24 + 8 + 2 + 2)
  433. return;
  434. sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
  435. if (sc->ps_flags & PS_BEACON_SYNC) {
  436. sc->ps_flags &= ~PS_BEACON_SYNC;
  437. ath_dbg(common, PS,
  438. "Reconfigure beacon timers based on synchronized timestamp\n");
  439. ath9k_set_beacon(sc);
  440. }
  441. if (ath_beacon_dtim_pending_cab(skb)) {
  442. /*
  443. * Remain awake waiting for buffered broadcast/multicast
  444. * frames. If the last broadcast/multicast frame is not
  445. * received properly, the next beacon frame will work as
  446. * a backup trigger for returning into NETWORK SLEEP state,
  447. * so we are waiting for it as well.
  448. */
  449. ath_dbg(common, PS,
  450. "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
  451. sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
  452. return;
  453. }
  454. if (sc->ps_flags & PS_WAIT_FOR_CAB) {
  455. /*
  456. * This can happen if a broadcast frame is dropped or the AP
  457. * fails to send a frame indicating that all CAB frames have
  458. * been delivered.
  459. */
  460. sc->ps_flags &= ~PS_WAIT_FOR_CAB;
  461. ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
  462. }
  463. }
  464. static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
  465. {
  466. struct ieee80211_hdr *hdr;
  467. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  468. hdr = (struct ieee80211_hdr *)skb->data;
  469. /* Process Beacon and CAB receive in PS state */
  470. if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
  471. && mybeacon) {
  472. ath_rx_ps_beacon(sc, skb);
  473. } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
  474. (ieee80211_is_data(hdr->frame_control) ||
  475. ieee80211_is_action(hdr->frame_control)) &&
  476. is_multicast_ether_addr(hdr->addr1) &&
  477. !ieee80211_has_moredata(hdr->frame_control)) {
  478. /*
  479. * No more broadcast/multicast frames to be received at this
  480. * point.
  481. */
  482. sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
  483. ath_dbg(common, PS,
  484. "All PS CAB frames received, back to sleep\n");
  485. } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
  486. !is_multicast_ether_addr(hdr->addr1) &&
  487. !ieee80211_has_morefrags(hdr->frame_control)) {
  488. sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
  489. ath_dbg(common, PS,
  490. "Going back to sleep after having received PS-Poll data (0x%lx)\n",
  491. sc->ps_flags & (PS_WAIT_FOR_BEACON |
  492. PS_WAIT_FOR_CAB |
  493. PS_WAIT_FOR_PSPOLL_DATA |
  494. PS_WAIT_FOR_TX_ACK));
  495. }
  496. }
  497. static bool ath_edma_get_buffers(struct ath_softc *sc,
  498. enum ath9k_rx_qtype qtype,
  499. struct ath_rx_status *rs,
  500. struct ath_buf **dest)
  501. {
  502. struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
  503. struct ath_hw *ah = sc->sc_ah;
  504. struct ath_common *common = ath9k_hw_common(ah);
  505. struct sk_buff *skb;
  506. struct ath_buf *bf;
  507. int ret;
  508. skb = skb_peek(&rx_edma->rx_fifo);
  509. if (!skb)
  510. return false;
  511. bf = SKB_CB_ATHBUF(skb);
  512. BUG_ON(!bf);
  513. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  514. common->rx_bufsize, DMA_FROM_DEVICE);
  515. ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
  516. if (ret == -EINPROGRESS) {
  517. /*let device gain the buffer again*/
  518. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  519. common->rx_bufsize, DMA_FROM_DEVICE);
  520. return false;
  521. }
  522. __skb_unlink(skb, &rx_edma->rx_fifo);
  523. if (ret == -EINVAL) {
  524. /* corrupt descriptor, skip this one and the following one */
  525. list_add_tail(&bf->list, &sc->rx.rxbuf);
  526. ath_rx_edma_buf_link(sc, qtype);
  527. skb = skb_peek(&rx_edma->rx_fifo);
  528. if (skb) {
  529. bf = SKB_CB_ATHBUF(skb);
  530. BUG_ON(!bf);
  531. __skb_unlink(skb, &rx_edma->rx_fifo);
  532. list_add_tail(&bf->list, &sc->rx.rxbuf);
  533. ath_rx_edma_buf_link(sc, qtype);
  534. }
  535. bf = NULL;
  536. }
  537. *dest = bf;
  538. return true;
  539. }
  540. static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
  541. struct ath_rx_status *rs,
  542. enum ath9k_rx_qtype qtype)
  543. {
  544. struct ath_buf *bf = NULL;
  545. while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
  546. if (!bf)
  547. continue;
  548. return bf;
  549. }
  550. return NULL;
  551. }
  552. static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
  553. struct ath_rx_status *rs)
  554. {
  555. struct ath_hw *ah = sc->sc_ah;
  556. struct ath_common *common = ath9k_hw_common(ah);
  557. struct ath_desc *ds;
  558. struct ath_buf *bf;
  559. int ret;
  560. if (list_empty(&sc->rx.rxbuf)) {
  561. sc->rx.rxlink = NULL;
  562. return NULL;
  563. }
  564. bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
  565. ds = bf->bf_desc;
  566. /*
  567. * Must provide the virtual address of the current
  568. * descriptor, the physical address, and the virtual
  569. * address of the next descriptor in the h/w chain.
  570. * This allows the HAL to look ahead to see if the
  571. * hardware is done with a descriptor by checking the
  572. * done bit in the following descriptor and the address
  573. * of the current descriptor the DMA engine is working
  574. * on. All this is necessary because of our use of
  575. * a self-linked list to avoid rx overruns.
  576. */
  577. ret = ath9k_hw_rxprocdesc(ah, ds, rs);
  578. if (ret == -EINPROGRESS) {
  579. struct ath_rx_status trs;
  580. struct ath_buf *tbf;
  581. struct ath_desc *tds;
  582. memset(&trs, 0, sizeof(trs));
  583. if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
  584. sc->rx.rxlink = NULL;
  585. return NULL;
  586. }
  587. tbf = list_entry(bf->list.next, struct ath_buf, list);
  588. /*
  589. * On some hardware the descriptor status words could
  590. * get corrupted, including the done bit. Because of
  591. * this, check if the next descriptor's done bit is
  592. * set or not.
  593. *
  594. * If the next descriptor's done bit is set, the current
  595. * descriptor has been corrupted. Force s/w to discard
  596. * this descriptor and continue...
  597. */
  598. tds = tbf->bf_desc;
  599. ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
  600. if (ret == -EINPROGRESS)
  601. return NULL;
  602. /*
  603. * mark descriptor as zero-length and set the 'more'
  604. * flag to ensure that both buffers get discarded
  605. */
  606. rs->rs_datalen = 0;
  607. rs->rs_more = true;
  608. }
  609. list_del(&bf->list);
  610. if (!bf->bf_mpdu)
  611. return bf;
  612. /*
  613. * Synchronize the DMA transfer with CPU before
  614. * 1. accessing the frame
  615. * 2. requeueing the same buffer to h/w
  616. */
  617. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  618. common->rx_bufsize,
  619. DMA_FROM_DEVICE);
  620. return bf;
  621. }
  622. /* Assumes you've already done the endian to CPU conversion */
  623. static bool ath9k_rx_accept(struct ath_common *common,
  624. struct ieee80211_hdr *hdr,
  625. struct ieee80211_rx_status *rxs,
  626. struct ath_rx_status *rx_stats,
  627. bool *decrypt_error)
  628. {
  629. struct ath_softc *sc = (struct ath_softc *) common->priv;
  630. bool is_mc, is_valid_tkip, strip_mic, mic_error;
  631. struct ath_hw *ah = common->ah;
  632. __le16 fc;
  633. u8 rx_status_len = ah->caps.rx_status_len;
  634. fc = hdr->frame_control;
  635. is_mc = !!is_multicast_ether_addr(hdr->addr1);
  636. is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
  637. test_bit(rx_stats->rs_keyix, common->tkip_keymap);
  638. strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
  639. ieee80211_has_protected(fc) &&
  640. !(rx_stats->rs_status &
  641. (ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
  642. ATH9K_RXERR_KEYMISS));
  643. /*
  644. * Key miss events are only relevant for pairwise keys where the
  645. * descriptor does contain a valid key index. This has been observed
  646. * mostly with CCMP encryption.
  647. */
  648. if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID ||
  649. !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
  650. rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
  651. if (!rx_stats->rs_datalen) {
  652. RX_STAT_INC(rx_len_err);
  653. return false;
  654. }
  655. /*
  656. * rs_status follows rs_datalen so if rs_datalen is too large
  657. * we can take a hint that hardware corrupted it, so ignore
  658. * those frames.
  659. */
  660. if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len)) {
  661. RX_STAT_INC(rx_len_err);
  662. return false;
  663. }
  664. /* Only use error bits from the last fragment */
  665. if (rx_stats->rs_more)
  666. return true;
  667. mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
  668. !ieee80211_has_morefrags(fc) &&
  669. !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
  670. (rx_stats->rs_status & ATH9K_RXERR_MIC);
  671. /*
  672. * The rx_stats->rs_status will not be set until the end of the
  673. * chained descriptors so it can be ignored if rs_more is set. The
  674. * rs_more will be false at the last element of the chained
  675. * descriptors.
  676. */
  677. if (rx_stats->rs_status != 0) {
  678. u8 status_mask;
  679. if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
  680. rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
  681. mic_error = false;
  682. }
  683. if (rx_stats->rs_status & ATH9K_RXERR_PHY)
  684. return false;
  685. if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
  686. (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
  687. *decrypt_error = true;
  688. mic_error = false;
  689. }
  690. /*
  691. * Reject error frames with the exception of
  692. * decryption and MIC failures. For monitor mode,
  693. * we also ignore the CRC error.
  694. */
  695. status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
  696. ATH9K_RXERR_KEYMISS;
  697. if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
  698. status_mask |= ATH9K_RXERR_CRC;
  699. if (rx_stats->rs_status & ~status_mask)
  700. return false;
  701. }
  702. /*
  703. * For unicast frames the MIC error bit can have false positives,
  704. * so all MIC error reports need to be validated in software.
  705. * False negatives are not common, so skip software verification
  706. * if the hardware considers the MIC valid.
  707. */
  708. if (strip_mic)
  709. rxs->flag |= RX_FLAG_MMIC_STRIPPED;
  710. else if (is_mc && mic_error)
  711. rxs->flag |= RX_FLAG_MMIC_ERROR;
  712. return true;
  713. }
  714. static int ath9k_process_rate(struct ath_common *common,
  715. struct ieee80211_hw *hw,
  716. struct ath_rx_status *rx_stats,
  717. struct ieee80211_rx_status *rxs)
  718. {
  719. struct ieee80211_supported_band *sband;
  720. enum ieee80211_band band;
  721. unsigned int i = 0;
  722. struct ath_softc __maybe_unused *sc = common->priv;
  723. band = hw->conf.chandef.chan->band;
  724. sband = hw->wiphy->bands[band];
  725. if (rx_stats->rs_rate & 0x80) {
  726. /* HT rate */
  727. rxs->flag |= RX_FLAG_HT;
  728. if (rx_stats->rs_flags & ATH9K_RX_2040)
  729. rxs->flag |= RX_FLAG_40MHZ;
  730. if (rx_stats->rs_flags & ATH9K_RX_GI)
  731. rxs->flag |= RX_FLAG_SHORT_GI;
  732. rxs->rate_idx = rx_stats->rs_rate & 0x7f;
  733. return 0;
  734. }
  735. for (i = 0; i < sband->n_bitrates; i++) {
  736. if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
  737. rxs->rate_idx = i;
  738. return 0;
  739. }
  740. if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
  741. rxs->flag |= RX_FLAG_SHORTPRE;
  742. rxs->rate_idx = i;
  743. return 0;
  744. }
  745. }
  746. /*
  747. * No valid hardware bitrate found -- we should not get here
  748. * because hardware has already validated this frame as OK.
  749. */
  750. ath_dbg(common, ANY,
  751. "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
  752. rx_stats->rs_rate);
  753. RX_STAT_INC(rx_rate_err);
  754. return -EINVAL;
  755. }
  756. static void ath9k_process_rssi(struct ath_common *common,
  757. struct ieee80211_hw *hw,
  758. struct ieee80211_hdr *hdr,
  759. struct ath_rx_status *rx_stats)
  760. {
  761. struct ath_softc *sc = hw->priv;
  762. struct ath_hw *ah = common->ah;
  763. int last_rssi;
  764. int rssi = rx_stats->rs_rssi;
  765. if (!rx_stats->is_mybeacon ||
  766. ((ah->opmode != NL80211_IFTYPE_STATION) &&
  767. (ah->opmode != NL80211_IFTYPE_ADHOC)))
  768. return;
  769. if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
  770. ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
  771. last_rssi = sc->last_rssi;
  772. if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
  773. rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
  774. if (rssi < 0)
  775. rssi = 0;
  776. /* Update Beacon RSSI, this is used by ANI. */
  777. ah->stats.avgbrssi = rssi;
  778. }
  779. /*
  780. * For Decrypt or Demic errors, we only mark packet status here and always push
  781. * up the frame up to let mac80211 handle the actual error case, be it no
  782. * decryption key or real decryption error. This let us keep statistics there.
  783. */
  784. static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
  785. struct ieee80211_hdr *hdr,
  786. struct ath_rx_status *rx_stats,
  787. struct ieee80211_rx_status *rx_status,
  788. bool *decrypt_error)
  789. {
  790. struct ieee80211_hw *hw = sc->hw;
  791. struct ath_hw *ah = sc->sc_ah;
  792. struct ath_common *common = ath9k_hw_common(ah);
  793. bool discard_current = sc->rx.discard_next;
  794. sc->rx.discard_next = rx_stats->rs_more;
  795. if (discard_current)
  796. return -EINVAL;
  797. /*
  798. * everything but the rate is checked here, the rate check is done
  799. * separately to avoid doing two lookups for a rate for each frame.
  800. */
  801. if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
  802. return -EINVAL;
  803. /* Only use status info from the last fragment */
  804. if (rx_stats->rs_more)
  805. return 0;
  806. ath9k_process_rssi(common, hw, hdr, rx_stats);
  807. if (ath9k_process_rate(common, hw, rx_stats, rx_status))
  808. return -EINVAL;
  809. rx_status->band = hw->conf.chandef.chan->band;
  810. rx_status->freq = hw->conf.chandef.chan->center_freq;
  811. rx_status->signal = ah->noise + rx_stats->rs_rssi;
  812. rx_status->antenna = rx_stats->rs_antenna;
  813. rx_status->flag |= RX_FLAG_MACTIME_END;
  814. if (rx_stats->rs_moreaggr)
  815. rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
  816. sc->rx.discard_next = false;
  817. return 0;
  818. }
  819. static void ath9k_rx_skb_postprocess(struct ath_common *common,
  820. struct sk_buff *skb,
  821. struct ath_rx_status *rx_stats,
  822. struct ieee80211_rx_status *rxs,
  823. bool decrypt_error)
  824. {
  825. struct ath_hw *ah = common->ah;
  826. struct ieee80211_hdr *hdr;
  827. int hdrlen, padpos, padsize;
  828. u8 keyix;
  829. __le16 fc;
  830. /* see if any padding is done by the hw and remove it */
  831. hdr = (struct ieee80211_hdr *) skb->data;
  832. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  833. fc = hdr->frame_control;
  834. padpos = ieee80211_hdrlen(fc);
  835. /* The MAC header is padded to have 32-bit boundary if the
  836. * packet payload is non-zero. The general calculation for
  837. * padsize would take into account odd header lengths:
  838. * padsize = (4 - padpos % 4) % 4; However, since only
  839. * even-length headers are used, padding can only be 0 or 2
  840. * bytes and we can optimize this a bit. In addition, we must
  841. * not try to remove padding from short control frames that do
  842. * not have payload. */
  843. padsize = padpos & 3;
  844. if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
  845. memmove(skb->data + padsize, skb->data, padpos);
  846. skb_pull(skb, padsize);
  847. }
  848. keyix = rx_stats->rs_keyix;
  849. if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
  850. ieee80211_has_protected(fc)) {
  851. rxs->flag |= RX_FLAG_DECRYPTED;
  852. } else if (ieee80211_has_protected(fc)
  853. && !decrypt_error && skb->len >= hdrlen + 4) {
  854. keyix = skb->data[hdrlen + 3] >> 6;
  855. if (test_bit(keyix, common->keymap))
  856. rxs->flag |= RX_FLAG_DECRYPTED;
  857. }
  858. if (ah->sw_mgmt_crypto &&
  859. (rxs->flag & RX_FLAG_DECRYPTED) &&
  860. ieee80211_is_mgmt(fc))
  861. /* Use software decrypt for management frames. */
  862. rxs->flag &= ~RX_FLAG_DECRYPTED;
  863. }
  864. #ifdef CONFIG_ATH9K_DEBUGFS
  865. static s8 fix_rssi_inv_only(u8 rssi_val)
  866. {
  867. if (rssi_val == 128)
  868. rssi_val = 0;
  869. return (s8) rssi_val;
  870. }
  871. #endif
  872. /* returns 1 if this was a spectral frame, even if not handled. */
  873. static int ath_process_fft(struct ath_softc *sc, struct ieee80211_hdr *hdr,
  874. struct ath_rx_status *rs, u64 tsf)
  875. {
  876. #ifdef CONFIG_ATH9K_DEBUGFS
  877. struct ath_hw *ah = sc->sc_ah;
  878. u8 bins[SPECTRAL_HT20_NUM_BINS];
  879. u8 *vdata = (u8 *)hdr;
  880. struct fft_sample_ht20 fft_sample;
  881. struct ath_radar_info *radar_info;
  882. struct ath_ht20_mag_info *mag_info;
  883. int len = rs->rs_datalen;
  884. int dc_pos;
  885. u16 length, max_magnitude;
  886. /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
  887. * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
  888. * yet, but this is supposed to be possible as well.
  889. */
  890. if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
  891. rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
  892. rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
  893. return 0;
  894. /* check if spectral scan bit is set. This does not have to be checked
  895. * if received through a SPECTRAL phy error, but shouldn't hurt.
  896. */
  897. radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
  898. if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
  899. return 0;
  900. /* Variation in the data length is possible and will be fixed later.
  901. * Note that we only support HT20 for now.
  902. *
  903. * TODO: add HT20_40 support as well.
  904. */
  905. if ((len > SPECTRAL_HT20_TOTAL_DATA_LEN + 2) ||
  906. (len < SPECTRAL_HT20_TOTAL_DATA_LEN - 1))
  907. return 1;
  908. fft_sample.tlv.type = ATH_FFT_SAMPLE_HT20;
  909. length = sizeof(fft_sample) - sizeof(fft_sample.tlv);
  910. fft_sample.tlv.length = __cpu_to_be16(length);
  911. fft_sample.freq = __cpu_to_be16(ah->curchan->chan->center_freq);
  912. fft_sample.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl0);
  913. fft_sample.noise = ah->noise;
  914. switch (len - SPECTRAL_HT20_TOTAL_DATA_LEN) {
  915. case 0:
  916. /* length correct, nothing to do. */
  917. memcpy(bins, vdata, SPECTRAL_HT20_NUM_BINS);
  918. break;
  919. case -1:
  920. /* first byte missing, duplicate it. */
  921. memcpy(&bins[1], vdata, SPECTRAL_HT20_NUM_BINS - 1);
  922. bins[0] = vdata[0];
  923. break;
  924. case 2:
  925. /* MAC added 2 extra bytes at bin 30 and 32, remove them. */
  926. memcpy(bins, vdata, 30);
  927. bins[30] = vdata[31];
  928. memcpy(&bins[31], &vdata[33], SPECTRAL_HT20_NUM_BINS - 31);
  929. break;
  930. case 1:
  931. /* MAC added 2 extra bytes AND first byte is missing. */
  932. bins[0] = vdata[0];
  933. memcpy(&bins[0], vdata, 30);
  934. bins[31] = vdata[31];
  935. memcpy(&bins[32], &vdata[33], SPECTRAL_HT20_NUM_BINS - 32);
  936. break;
  937. default:
  938. return 1;
  939. }
  940. /* DC value (value in the middle) is the blind spot of the spectral
  941. * sample and invalid, interpolate it.
  942. */
  943. dc_pos = SPECTRAL_HT20_NUM_BINS / 2;
  944. bins[dc_pos] = (bins[dc_pos + 1] + bins[dc_pos - 1]) / 2;
  945. /* mag data is at the end of the frame, in front of radar_info */
  946. mag_info = ((struct ath_ht20_mag_info *)radar_info) - 1;
  947. /* copy raw bins without scaling them */
  948. memcpy(fft_sample.data, bins, SPECTRAL_HT20_NUM_BINS);
  949. fft_sample.max_exp = mag_info->max_exp & 0xf;
  950. max_magnitude = spectral_max_magnitude(mag_info->all_bins);
  951. fft_sample.max_magnitude = __cpu_to_be16(max_magnitude);
  952. fft_sample.max_index = spectral_max_index(mag_info->all_bins);
  953. fft_sample.bitmap_weight = spectral_bitmap_weight(mag_info->all_bins);
  954. fft_sample.tsf = __cpu_to_be64(tsf);
  955. ath_debug_send_fft_sample(sc, &fft_sample.tlv);
  956. return 1;
  957. #else
  958. return 0;
  959. #endif
  960. }
  961. static void ath9k_apply_ampdu_details(struct ath_softc *sc,
  962. struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
  963. {
  964. if (rs->rs_isaggr) {
  965. rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
  966. rxs->ampdu_reference = sc->rx.ampdu_ref;
  967. if (!rs->rs_moreaggr) {
  968. rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
  969. sc->rx.ampdu_ref++;
  970. }
  971. if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
  972. rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
  973. }
  974. }
  975. int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
  976. {
  977. struct ath_buf *bf;
  978. struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
  979. struct ieee80211_rx_status *rxs;
  980. struct ath_hw *ah = sc->sc_ah;
  981. struct ath_common *common = ath9k_hw_common(ah);
  982. struct ieee80211_hw *hw = sc->hw;
  983. struct ieee80211_hdr *hdr;
  984. int retval;
  985. struct ath_rx_status rs;
  986. enum ath9k_rx_qtype qtype;
  987. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  988. int dma_type;
  989. u8 rx_status_len = ah->caps.rx_status_len;
  990. u64 tsf = 0;
  991. u32 tsf_lower = 0;
  992. unsigned long flags;
  993. dma_addr_t new_buf_addr;
  994. if (edma)
  995. dma_type = DMA_BIDIRECTIONAL;
  996. else
  997. dma_type = DMA_FROM_DEVICE;
  998. qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
  999. tsf = ath9k_hw_gettsf64(ah);
  1000. tsf_lower = tsf & 0xffffffff;
  1001. do {
  1002. bool decrypt_error = false;
  1003. memset(&rs, 0, sizeof(rs));
  1004. if (edma)
  1005. bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
  1006. else
  1007. bf = ath_get_next_rx_buf(sc, &rs);
  1008. if (!bf)
  1009. break;
  1010. skb = bf->bf_mpdu;
  1011. if (!skb)
  1012. continue;
  1013. /*
  1014. * Take frame header from the first fragment and RX status from
  1015. * the last one.
  1016. */
  1017. if (sc->rx.frag)
  1018. hdr_skb = sc->rx.frag;
  1019. else
  1020. hdr_skb = skb;
  1021. hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
  1022. rxs = IEEE80211_SKB_RXCB(hdr_skb);
  1023. if (ieee80211_is_beacon(hdr->frame_control)) {
  1024. RX_STAT_INC(rx_beacons);
  1025. if (!is_zero_ether_addr(common->curbssid) &&
  1026. ether_addr_equal(hdr->addr3, common->curbssid))
  1027. rs.is_mybeacon = true;
  1028. else
  1029. rs.is_mybeacon = false;
  1030. }
  1031. else
  1032. rs.is_mybeacon = false;
  1033. if (ieee80211_is_data_present(hdr->frame_control) &&
  1034. !ieee80211_is_qos_nullfunc(hdr->frame_control))
  1035. sc->rx.num_pkts++;
  1036. ath_debug_stat_rx(sc, &rs);
  1037. memset(rxs, 0, sizeof(struct ieee80211_rx_status));
  1038. rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
  1039. if (rs.rs_tstamp > tsf_lower &&
  1040. unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
  1041. rxs->mactime -= 0x100000000ULL;
  1042. if (rs.rs_tstamp < tsf_lower &&
  1043. unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
  1044. rxs->mactime += 0x100000000ULL;
  1045. if (rs.rs_phyerr == ATH9K_PHYERR_RADAR)
  1046. ath9k_dfs_process_phyerr(sc, hdr, &rs, rxs->mactime);
  1047. if (rs.rs_status & ATH9K_RXERR_PHY) {
  1048. if (ath_process_fft(sc, hdr, &rs, rxs->mactime)) {
  1049. RX_STAT_INC(rx_spectral);
  1050. goto requeue_drop_frag;
  1051. }
  1052. }
  1053. retval = ath9k_rx_skb_preprocess(sc, hdr, &rs, rxs,
  1054. &decrypt_error);
  1055. if (retval)
  1056. goto requeue_drop_frag;
  1057. if (rs.is_mybeacon) {
  1058. sc->hw_busy_count = 0;
  1059. ath_start_rx_poll(sc, 3);
  1060. }
  1061. /* Ensure we always have an skb to requeue once we are done
  1062. * processing the current buffer's skb */
  1063. requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
  1064. /* If there is no memory we ignore the current RX'd frame,
  1065. * tell hardware it can give us a new frame using the old
  1066. * skb and put it at the tail of the sc->rx.rxbuf list for
  1067. * processing. */
  1068. if (!requeue_skb) {
  1069. RX_STAT_INC(rx_oom_err);
  1070. goto requeue_drop_frag;
  1071. }
  1072. /* We will now give hardware our shiny new allocated skb */
  1073. new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
  1074. common->rx_bufsize, dma_type);
  1075. if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
  1076. dev_kfree_skb_any(requeue_skb);
  1077. goto requeue_drop_frag;
  1078. }
  1079. bf->bf_mpdu = requeue_skb;
  1080. bf->bf_buf_addr = new_buf_addr;
  1081. /* Unmap the frame */
  1082. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  1083. common->rx_bufsize, dma_type);
  1084. skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
  1085. if (ah->caps.rx_status_len)
  1086. skb_pull(skb, ah->caps.rx_status_len);
  1087. if (!rs.rs_more)
  1088. ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
  1089. rxs, decrypt_error);
  1090. if (rs.rs_more) {
  1091. RX_STAT_INC(rx_frags);
  1092. /*
  1093. * rs_more indicates chained descriptors which can be
  1094. * used to link buffers together for a sort of
  1095. * scatter-gather operation.
  1096. */
  1097. if (sc->rx.frag) {
  1098. /* too many fragments - cannot handle frame */
  1099. dev_kfree_skb_any(sc->rx.frag);
  1100. dev_kfree_skb_any(skb);
  1101. RX_STAT_INC(rx_too_many_frags_err);
  1102. skb = NULL;
  1103. }
  1104. sc->rx.frag = skb;
  1105. goto requeue;
  1106. }
  1107. if (rs.rs_status & ATH9K_RXERR_CORRUPT_DESC)
  1108. goto requeue_drop_frag;
  1109. if (sc->rx.frag) {
  1110. int space = skb->len - skb_tailroom(hdr_skb);
  1111. if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
  1112. dev_kfree_skb(skb);
  1113. RX_STAT_INC(rx_oom_err);
  1114. goto requeue_drop_frag;
  1115. }
  1116. sc->rx.frag = NULL;
  1117. skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
  1118. skb->len);
  1119. dev_kfree_skb_any(skb);
  1120. skb = hdr_skb;
  1121. }
  1122. if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
  1123. /*
  1124. * change the default rx antenna if rx diversity
  1125. * chooses the other antenna 3 times in a row.
  1126. */
  1127. if (sc->rx.defant != rs.rs_antenna) {
  1128. if (++sc->rx.rxotherant >= 3)
  1129. ath_setdefantenna(sc, rs.rs_antenna);
  1130. } else {
  1131. sc->rx.rxotherant = 0;
  1132. }
  1133. }
  1134. if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
  1135. skb_trim(skb, skb->len - 8);
  1136. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  1137. if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
  1138. PS_WAIT_FOR_CAB |
  1139. PS_WAIT_FOR_PSPOLL_DATA)) ||
  1140. ath9k_check_auto_sleep(sc))
  1141. ath_rx_ps(sc, skb, rs.is_mybeacon);
  1142. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  1143. if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx == 3)
  1144. ath_ant_comb_scan(sc, &rs);
  1145. ath9k_apply_ampdu_details(sc, &rs, rxs);
  1146. ieee80211_rx(hw, skb);
  1147. requeue_drop_frag:
  1148. if (sc->rx.frag) {
  1149. dev_kfree_skb_any(sc->rx.frag);
  1150. sc->rx.frag = NULL;
  1151. }
  1152. requeue:
  1153. list_add_tail(&bf->list, &sc->rx.rxbuf);
  1154. if (flush)
  1155. continue;
  1156. if (edma) {
  1157. ath_rx_edma_buf_link(sc, qtype);
  1158. } else {
  1159. ath_rx_buf_link(sc, bf);
  1160. ath9k_hw_rxena(ah);
  1161. }
  1162. } while (1);
  1163. if (!(ah->imask & ATH9K_INT_RXEOL)) {
  1164. ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  1165. ath9k_hw_set_interrupts(ah);
  1166. }
  1167. return 0;
  1168. }