recv.c 36 KB

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