recv.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. /*
  2. * Copyright (c) 2008-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/dma-mapping.h>
  17. #include "ath9k.h"
  18. #include "ar9003_mac.h"
  19. #define SKB_CB_ATHBUF(__skb) (*((struct ath_rxbuf **)__skb->cb))
  20. static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
  21. {
  22. return sc->ps_enabled &&
  23. (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
  24. }
  25. /*
  26. * Setup and link descriptors.
  27. *
  28. * 11N: we can no longer afford to self link the last descriptor.
  29. * MAC acknowledges BA status as long as it copies frames to host
  30. * buffer (or rx fifo). This can incorrectly acknowledge packets
  31. * to a sender if last desc is self-linked.
  32. */
  33. static void ath_rx_buf_link(struct ath_softc *sc, struct ath_rxbuf *bf,
  34. bool flush)
  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. ds = bf->bf_desc;
  41. ds->ds_link = 0; /* link to null */
  42. ds->ds_data = bf->bf_buf_addr;
  43. /* virtual addr of the beginning of the buffer. */
  44. skb = bf->bf_mpdu;
  45. BUG_ON(skb == NULL);
  46. ds->ds_vdata = skb->data;
  47. /*
  48. * setup rx descriptors. The rx_bufsize here tells the hardware
  49. * how much data it can DMA to us and that we are prepared
  50. * to process
  51. */
  52. ath9k_hw_setuprxdesc(ah, ds,
  53. common->rx_bufsize,
  54. 0);
  55. if (sc->rx.rxlink)
  56. *sc->rx.rxlink = bf->bf_daddr;
  57. else if (!flush)
  58. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  59. sc->rx.rxlink = &ds->ds_link;
  60. }
  61. static void ath_rx_buf_relink(struct ath_softc *sc, struct ath_rxbuf *bf,
  62. bool flush)
  63. {
  64. if (sc->rx.buf_hold)
  65. ath_rx_buf_link(sc, sc->rx.buf_hold, flush);
  66. sc->rx.buf_hold = bf;
  67. }
  68. static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
  69. {
  70. /* XXX block beacon interrupts */
  71. ath9k_hw_setantenna(sc->sc_ah, antenna);
  72. sc->rx.defant = antenna;
  73. sc->rx.rxotherant = 0;
  74. }
  75. static void ath_opmode_init(struct ath_softc *sc)
  76. {
  77. struct ath_hw *ah = sc->sc_ah;
  78. struct ath_common *common = ath9k_hw_common(ah);
  79. u32 rfilt, mfilt[2];
  80. /* configure rx filter */
  81. rfilt = ath_calcrxfilter(sc);
  82. ath9k_hw_setrxfilter(ah, rfilt);
  83. /* configure bssid mask */
  84. ath_hw_setbssidmask(common);
  85. /* configure operational mode */
  86. ath9k_hw_setopmode(ah);
  87. /* calculate and install multicast filter */
  88. mfilt[0] = mfilt[1] = ~0;
  89. ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
  90. }
  91. static bool ath_rx_edma_buf_link(struct ath_softc *sc,
  92. enum ath9k_rx_qtype qtype)
  93. {
  94. struct ath_hw *ah = sc->sc_ah;
  95. struct ath_rx_edma *rx_edma;
  96. struct sk_buff *skb;
  97. struct ath_rxbuf *bf;
  98. rx_edma = &sc->rx.rx_edma[qtype];
  99. if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
  100. return false;
  101. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  102. list_del_init(&bf->list);
  103. skb = bf->bf_mpdu;
  104. memset(skb->data, 0, ah->caps.rx_status_len);
  105. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  106. ah->caps.rx_status_len, DMA_TO_DEVICE);
  107. SKB_CB_ATHBUF(skb) = bf;
  108. ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
  109. __skb_queue_tail(&rx_edma->rx_fifo, skb);
  110. return true;
  111. }
  112. static void ath_rx_addbuffer_edma(struct ath_softc *sc,
  113. enum ath9k_rx_qtype qtype)
  114. {
  115. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  116. struct ath_rxbuf *bf, *tbf;
  117. if (list_empty(&sc->rx.rxbuf)) {
  118. ath_dbg(common, QUEUE, "No free rx buf available\n");
  119. return;
  120. }
  121. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
  122. if (!ath_rx_edma_buf_link(sc, qtype))
  123. break;
  124. }
  125. static void ath_rx_remove_buffer(struct ath_softc *sc,
  126. enum ath9k_rx_qtype qtype)
  127. {
  128. struct ath_rxbuf *bf;
  129. struct ath_rx_edma *rx_edma;
  130. struct sk_buff *skb;
  131. rx_edma = &sc->rx.rx_edma[qtype];
  132. while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
  133. bf = SKB_CB_ATHBUF(skb);
  134. BUG_ON(!bf);
  135. list_add_tail(&bf->list, &sc->rx.rxbuf);
  136. }
  137. }
  138. static void ath_rx_edma_cleanup(struct ath_softc *sc)
  139. {
  140. struct ath_hw *ah = sc->sc_ah;
  141. struct ath_common *common = ath9k_hw_common(ah);
  142. struct ath_rxbuf *bf;
  143. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  144. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  145. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  146. if (bf->bf_mpdu) {
  147. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  148. common->rx_bufsize,
  149. DMA_BIDIRECTIONAL);
  150. dev_kfree_skb_any(bf->bf_mpdu);
  151. bf->bf_buf_addr = 0;
  152. bf->bf_mpdu = NULL;
  153. }
  154. }
  155. }
  156. static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
  157. {
  158. __skb_queue_head_init(&rx_edma->rx_fifo);
  159. rx_edma->rx_fifo_hwsize = size;
  160. }
  161. static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
  162. {
  163. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  164. struct ath_hw *ah = sc->sc_ah;
  165. struct sk_buff *skb;
  166. struct ath_rxbuf *bf;
  167. int error = 0, i;
  168. u32 size;
  169. ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
  170. ah->caps.rx_status_len);
  171. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
  172. ah->caps.rx_lp_qdepth);
  173. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
  174. ah->caps.rx_hp_qdepth);
  175. size = sizeof(struct ath_rxbuf) * nbufs;
  176. bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
  177. if (!bf)
  178. return -ENOMEM;
  179. INIT_LIST_HEAD(&sc->rx.rxbuf);
  180. for (i = 0; i < nbufs; i++, bf++) {
  181. skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
  182. if (!skb) {
  183. error = -ENOMEM;
  184. goto rx_init_fail;
  185. }
  186. memset(skb->data, 0, common->rx_bufsize);
  187. bf->bf_mpdu = skb;
  188. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  189. common->rx_bufsize,
  190. DMA_BIDIRECTIONAL);
  191. if (unlikely(dma_mapping_error(sc->dev,
  192. bf->bf_buf_addr))) {
  193. dev_kfree_skb_any(skb);
  194. bf->bf_mpdu = NULL;
  195. bf->bf_buf_addr = 0;
  196. ath_err(common,
  197. "dma_mapping_error() on RX init\n");
  198. error = -ENOMEM;
  199. goto rx_init_fail;
  200. }
  201. list_add_tail(&bf->list, &sc->rx.rxbuf);
  202. }
  203. return 0;
  204. rx_init_fail:
  205. ath_rx_edma_cleanup(sc);
  206. return error;
  207. }
  208. static void ath_edma_start_recv(struct ath_softc *sc)
  209. {
  210. ath9k_hw_rxena(sc->sc_ah);
  211. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP);
  212. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP);
  213. ath_opmode_init(sc);
  214. ath9k_hw_startpcureceive(sc->sc_ah, sc->cur_chan->offchannel);
  215. }
  216. static void ath_edma_stop_recv(struct ath_softc *sc)
  217. {
  218. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  219. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  220. }
  221. int ath_rx_init(struct ath_softc *sc, int nbufs)
  222. {
  223. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  224. struct sk_buff *skb;
  225. struct ath_rxbuf *bf;
  226. int error = 0;
  227. spin_lock_init(&sc->sc_pcu_lock);
  228. common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
  229. sc->sc_ah->caps.rx_status_len;
  230. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  231. return ath_rx_edma_init(sc, nbufs);
  232. ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
  233. common->cachelsz, common->rx_bufsize);
  234. /* Initialize rx descriptors */
  235. error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
  236. "rx", nbufs, 1, 0);
  237. if (error != 0) {
  238. ath_err(common,
  239. "failed to allocate rx descriptors: %d\n",
  240. error);
  241. goto err;
  242. }
  243. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  244. skb = ath_rxbuf_alloc(common, common->rx_bufsize,
  245. GFP_KERNEL);
  246. if (skb == NULL) {
  247. error = -ENOMEM;
  248. goto err;
  249. }
  250. bf->bf_mpdu = skb;
  251. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  252. common->rx_bufsize,
  253. DMA_FROM_DEVICE);
  254. if (unlikely(dma_mapping_error(sc->dev,
  255. bf->bf_buf_addr))) {
  256. dev_kfree_skb_any(skb);
  257. bf->bf_mpdu = NULL;
  258. bf->bf_buf_addr = 0;
  259. ath_err(common,
  260. "dma_mapping_error() on RX init\n");
  261. error = -ENOMEM;
  262. goto err;
  263. }
  264. }
  265. sc->rx.rxlink = NULL;
  266. err:
  267. if (error)
  268. ath_rx_cleanup(sc);
  269. return error;
  270. }
  271. void ath_rx_cleanup(struct ath_softc *sc)
  272. {
  273. struct ath_hw *ah = sc->sc_ah;
  274. struct ath_common *common = ath9k_hw_common(ah);
  275. struct sk_buff *skb;
  276. struct ath_rxbuf *bf;
  277. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  278. ath_rx_edma_cleanup(sc);
  279. return;
  280. }
  281. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  282. skb = bf->bf_mpdu;
  283. if (skb) {
  284. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  285. common->rx_bufsize,
  286. DMA_FROM_DEVICE);
  287. dev_kfree_skb(skb);
  288. bf->bf_buf_addr = 0;
  289. bf->bf_mpdu = NULL;
  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. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  314. u32 rfilt;
  315. if (config_enabled(CONFIG_ATH9K_TX99))
  316. return 0;
  317. rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
  318. | ATH9K_RX_FILTER_MCAST;
  319. /* if operating on a DFS channel, enable radar pulse detection */
  320. if (sc->hw->conf.radar_enabled)
  321. rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
  322. spin_lock_bh(&sc->chan_lock);
  323. if (sc->cur_chan->rxfilter & FIF_PROBE_REQ)
  324. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  325. /*
  326. * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
  327. * mode interface or when in monitor mode. AP mode does not need this
  328. * since it receives all in-BSS frames anyway.
  329. */
  330. if (sc->sc_ah->is_monitoring)
  331. rfilt |= ATH9K_RX_FILTER_PROM;
  332. if ((sc->cur_chan->rxfilter & FIF_CONTROL) ||
  333. sc->sc_ah->dynack.enabled)
  334. rfilt |= ATH9K_RX_FILTER_CONTROL;
  335. if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
  336. (sc->cur_chan->nvifs <= 1) &&
  337. !(sc->cur_chan->rxfilter & FIF_BCN_PRBRESP_PROMISC))
  338. rfilt |= ATH9K_RX_FILTER_MYBEACON;
  339. else
  340. rfilt |= ATH9K_RX_FILTER_BEACON;
  341. if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
  342. (sc->cur_chan->rxfilter & FIF_PSPOLL))
  343. rfilt |= ATH9K_RX_FILTER_PSPOLL;
  344. if (sc->cur_chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
  345. rfilt |= ATH9K_RX_FILTER_COMP_BAR;
  346. if (sc->cur_chan->nvifs > 1 || (sc->cur_chan->rxfilter & FIF_OTHER_BSS)) {
  347. /* This is needed for older chips */
  348. if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
  349. rfilt |= ATH9K_RX_FILTER_PROM;
  350. rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
  351. }
  352. if (AR_SREV_9550(sc->sc_ah) || AR_SREV_9531(sc->sc_ah))
  353. rfilt |= ATH9K_RX_FILTER_4ADDRESS;
  354. if (ath9k_is_chanctx_enabled() &&
  355. test_bit(ATH_OP_SCANNING, &common->op_flags))
  356. rfilt |= ATH9K_RX_FILTER_BEACON;
  357. spin_unlock_bh(&sc->chan_lock);
  358. return rfilt;
  359. }
  360. void ath_startrecv(struct ath_softc *sc)
  361. {
  362. struct ath_hw *ah = sc->sc_ah;
  363. struct ath_rxbuf *bf, *tbf;
  364. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  365. ath_edma_start_recv(sc);
  366. return;
  367. }
  368. if (list_empty(&sc->rx.rxbuf))
  369. goto start_recv;
  370. sc->rx.buf_hold = NULL;
  371. sc->rx.rxlink = NULL;
  372. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
  373. ath_rx_buf_link(sc, bf, false);
  374. }
  375. /* We could have deleted elements so the list may be empty now */
  376. if (list_empty(&sc->rx.rxbuf))
  377. goto start_recv;
  378. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  379. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  380. ath9k_hw_rxena(ah);
  381. start_recv:
  382. ath_opmode_init(sc);
  383. ath9k_hw_startpcureceive(ah, sc->cur_chan->offchannel);
  384. }
  385. static void ath_flushrecv(struct ath_softc *sc)
  386. {
  387. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  388. ath_rx_tasklet(sc, 1, true);
  389. ath_rx_tasklet(sc, 1, false);
  390. }
  391. bool ath_stoprecv(struct ath_softc *sc)
  392. {
  393. struct ath_hw *ah = sc->sc_ah;
  394. bool stopped, reset = false;
  395. ath9k_hw_abortpcurecv(ah);
  396. ath9k_hw_setrxfilter(ah, 0);
  397. stopped = ath9k_hw_stopdmarecv(ah, &reset);
  398. ath_flushrecv(sc);
  399. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  400. ath_edma_stop_recv(sc);
  401. else
  402. sc->rx.rxlink = NULL;
  403. if (!(ah->ah_flags & AH_UNPLUGGED) &&
  404. unlikely(!stopped)) {
  405. ath_err(ath9k_hw_common(sc->sc_ah),
  406. "Could not stop RX, we could be "
  407. "confusing the DMA engine when we start RX up\n");
  408. ATH_DBG_WARN_ON_ONCE(!stopped);
  409. }
  410. return stopped && !reset;
  411. }
  412. static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
  413. {
  414. /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
  415. struct ieee80211_mgmt *mgmt;
  416. u8 *pos, *end, id, elen;
  417. struct ieee80211_tim_ie *tim;
  418. mgmt = (struct ieee80211_mgmt *)skb->data;
  419. pos = mgmt->u.beacon.variable;
  420. end = skb->data + skb->len;
  421. while (pos + 2 < end) {
  422. id = *pos++;
  423. elen = *pos++;
  424. if (pos + elen > end)
  425. break;
  426. if (id == WLAN_EID_TIM) {
  427. if (elen < sizeof(*tim))
  428. break;
  429. tim = (struct ieee80211_tim_ie *) pos;
  430. if (tim->dtim_count != 0)
  431. break;
  432. return tim->bitmap_ctrl & 0x01;
  433. }
  434. pos += elen;
  435. }
  436. return false;
  437. }
  438. static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
  439. {
  440. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  441. bool skip_beacon = false;
  442. if (skb->len < 24 + 8 + 2 + 2)
  443. return;
  444. sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
  445. if (sc->ps_flags & PS_BEACON_SYNC) {
  446. sc->ps_flags &= ~PS_BEACON_SYNC;
  447. ath_dbg(common, PS,
  448. "Reconfigure beacon timers based on synchronized timestamp\n");
  449. #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
  450. if (ath9k_is_chanctx_enabled()) {
  451. if (sc->cur_chan == &sc->offchannel.chan)
  452. skip_beacon = true;
  453. }
  454. #endif
  455. if (!skip_beacon &&
  456. !(WARN_ON_ONCE(sc->cur_chan->beacon.beacon_interval == 0)))
  457. ath9k_set_beacon(sc);
  458. ath9k_p2p_beacon_sync(sc);
  459. }
  460. if (ath_beacon_dtim_pending_cab(skb)) {
  461. /*
  462. * Remain awake waiting for buffered broadcast/multicast
  463. * frames. If the last broadcast/multicast frame is not
  464. * received properly, the next beacon frame will work as
  465. * a backup trigger for returning into NETWORK SLEEP state,
  466. * so we are waiting for it as well.
  467. */
  468. ath_dbg(common, PS,
  469. "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
  470. sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
  471. return;
  472. }
  473. if (sc->ps_flags & PS_WAIT_FOR_CAB) {
  474. /*
  475. * This can happen if a broadcast frame is dropped or the AP
  476. * fails to send a frame indicating that all CAB frames have
  477. * been delivered.
  478. */
  479. sc->ps_flags &= ~PS_WAIT_FOR_CAB;
  480. ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
  481. }
  482. }
  483. static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
  484. {
  485. struct ieee80211_hdr *hdr;
  486. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  487. hdr = (struct ieee80211_hdr *)skb->data;
  488. /* Process Beacon and CAB receive in PS state */
  489. if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
  490. && mybeacon) {
  491. ath_rx_ps_beacon(sc, skb);
  492. } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
  493. (ieee80211_is_data(hdr->frame_control) ||
  494. ieee80211_is_action(hdr->frame_control)) &&
  495. is_multicast_ether_addr(hdr->addr1) &&
  496. !ieee80211_has_moredata(hdr->frame_control)) {
  497. /*
  498. * No more broadcast/multicast frames to be received at this
  499. * point.
  500. */
  501. sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
  502. ath_dbg(common, PS,
  503. "All PS CAB frames received, back to sleep\n");
  504. } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
  505. !is_multicast_ether_addr(hdr->addr1) &&
  506. !ieee80211_has_morefrags(hdr->frame_control)) {
  507. sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
  508. ath_dbg(common, PS,
  509. "Going back to sleep after having received PS-Poll data (0x%lx)\n",
  510. sc->ps_flags & (PS_WAIT_FOR_BEACON |
  511. PS_WAIT_FOR_CAB |
  512. PS_WAIT_FOR_PSPOLL_DATA |
  513. PS_WAIT_FOR_TX_ACK));
  514. }
  515. }
  516. static bool ath_edma_get_buffers(struct ath_softc *sc,
  517. enum ath9k_rx_qtype qtype,
  518. struct ath_rx_status *rs,
  519. struct ath_rxbuf **dest)
  520. {
  521. struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
  522. struct ath_hw *ah = sc->sc_ah;
  523. struct ath_common *common = ath9k_hw_common(ah);
  524. struct sk_buff *skb;
  525. struct ath_rxbuf *bf;
  526. int ret;
  527. skb = skb_peek(&rx_edma->rx_fifo);
  528. if (!skb)
  529. return false;
  530. bf = SKB_CB_ATHBUF(skb);
  531. BUG_ON(!bf);
  532. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  533. common->rx_bufsize, DMA_FROM_DEVICE);
  534. ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
  535. if (ret == -EINPROGRESS) {
  536. /*let device gain the buffer again*/
  537. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  538. common->rx_bufsize, DMA_FROM_DEVICE);
  539. return false;
  540. }
  541. __skb_unlink(skb, &rx_edma->rx_fifo);
  542. if (ret == -EINVAL) {
  543. /* corrupt descriptor, skip this one and the following one */
  544. list_add_tail(&bf->list, &sc->rx.rxbuf);
  545. ath_rx_edma_buf_link(sc, qtype);
  546. skb = skb_peek(&rx_edma->rx_fifo);
  547. if (skb) {
  548. bf = SKB_CB_ATHBUF(skb);
  549. BUG_ON(!bf);
  550. __skb_unlink(skb, &rx_edma->rx_fifo);
  551. list_add_tail(&bf->list, &sc->rx.rxbuf);
  552. ath_rx_edma_buf_link(sc, qtype);
  553. }
  554. bf = NULL;
  555. }
  556. *dest = bf;
  557. return true;
  558. }
  559. static struct ath_rxbuf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
  560. struct ath_rx_status *rs,
  561. enum ath9k_rx_qtype qtype)
  562. {
  563. struct ath_rxbuf *bf = NULL;
  564. while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
  565. if (!bf)
  566. continue;
  567. return bf;
  568. }
  569. return NULL;
  570. }
  571. static struct ath_rxbuf *ath_get_next_rx_buf(struct ath_softc *sc,
  572. struct ath_rx_status *rs)
  573. {
  574. struct ath_hw *ah = sc->sc_ah;
  575. struct ath_common *common = ath9k_hw_common(ah);
  576. struct ath_desc *ds;
  577. struct ath_rxbuf *bf;
  578. int ret;
  579. if (list_empty(&sc->rx.rxbuf)) {
  580. sc->rx.rxlink = NULL;
  581. return NULL;
  582. }
  583. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  584. if (bf == sc->rx.buf_hold)
  585. return NULL;
  586. ds = bf->bf_desc;
  587. /*
  588. * Must provide the virtual address of the current
  589. * descriptor, the physical address, and the virtual
  590. * address of the next descriptor in the h/w chain.
  591. * This allows the HAL to look ahead to see if the
  592. * hardware is done with a descriptor by checking the
  593. * done bit in the following descriptor and the address
  594. * of the current descriptor the DMA engine is working
  595. * on. All this is necessary because of our use of
  596. * a self-linked list to avoid rx overruns.
  597. */
  598. ret = ath9k_hw_rxprocdesc(ah, ds, rs);
  599. if (ret == -EINPROGRESS) {
  600. struct ath_rx_status trs;
  601. struct ath_rxbuf *tbf;
  602. struct ath_desc *tds;
  603. memset(&trs, 0, sizeof(trs));
  604. if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
  605. sc->rx.rxlink = NULL;
  606. return NULL;
  607. }
  608. tbf = list_entry(bf->list.next, struct ath_rxbuf, list);
  609. /*
  610. * On some hardware the descriptor status words could
  611. * get corrupted, including the done bit. Because of
  612. * this, check if the next descriptor's done bit is
  613. * set or not.
  614. *
  615. * If the next descriptor's done bit is set, the current
  616. * descriptor has been corrupted. Force s/w to discard
  617. * this descriptor and continue...
  618. */
  619. tds = tbf->bf_desc;
  620. ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
  621. if (ret == -EINPROGRESS)
  622. return NULL;
  623. /*
  624. * Re-check previous descriptor, in case it has been filled
  625. * in the mean time.
  626. */
  627. ret = ath9k_hw_rxprocdesc(ah, ds, rs);
  628. if (ret == -EINPROGRESS) {
  629. /*
  630. * mark descriptor as zero-length and set the 'more'
  631. * flag to ensure that both buffers get discarded
  632. */
  633. rs->rs_datalen = 0;
  634. rs->rs_more = true;
  635. }
  636. }
  637. list_del(&bf->list);
  638. if (!bf->bf_mpdu)
  639. return bf;
  640. /*
  641. * Synchronize the DMA transfer with CPU before
  642. * 1. accessing the frame
  643. * 2. requeueing the same buffer to h/w
  644. */
  645. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  646. common->rx_bufsize,
  647. DMA_FROM_DEVICE);
  648. return bf;
  649. }
  650. static void ath9k_process_tsf(struct ath_rx_status *rs,
  651. struct ieee80211_rx_status *rxs,
  652. u64 tsf)
  653. {
  654. u32 tsf_lower = tsf & 0xffffffff;
  655. rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp;
  656. if (rs->rs_tstamp > tsf_lower &&
  657. unlikely(rs->rs_tstamp - tsf_lower > 0x10000000))
  658. rxs->mactime -= 0x100000000ULL;
  659. if (rs->rs_tstamp < tsf_lower &&
  660. unlikely(tsf_lower - rs->rs_tstamp > 0x10000000))
  661. rxs->mactime += 0x100000000ULL;
  662. }
  663. /*
  664. * For Decrypt or Demic errors, we only mark packet status here and always push
  665. * up the frame up to let mac80211 handle the actual error case, be it no
  666. * decryption key or real decryption error. This let us keep statistics there.
  667. */
  668. static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
  669. struct sk_buff *skb,
  670. struct ath_rx_status *rx_stats,
  671. struct ieee80211_rx_status *rx_status,
  672. bool *decrypt_error, u64 tsf)
  673. {
  674. struct ieee80211_hw *hw = sc->hw;
  675. struct ath_hw *ah = sc->sc_ah;
  676. struct ath_common *common = ath9k_hw_common(ah);
  677. struct ieee80211_hdr *hdr;
  678. bool discard_current = sc->rx.discard_next;
  679. /*
  680. * Discard corrupt descriptors which are marked in
  681. * ath_get_next_rx_buf().
  682. */
  683. if (discard_current)
  684. goto corrupt;
  685. sc->rx.discard_next = false;
  686. /*
  687. * Discard zero-length packets.
  688. */
  689. if (!rx_stats->rs_datalen) {
  690. RX_STAT_INC(rx_len_err);
  691. goto corrupt;
  692. }
  693. /*
  694. * rs_status follows rs_datalen so if rs_datalen is too large
  695. * we can take a hint that hardware corrupted it, so ignore
  696. * those frames.
  697. */
  698. if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) {
  699. RX_STAT_INC(rx_len_err);
  700. goto corrupt;
  701. }
  702. /* Only use status info from the last fragment */
  703. if (rx_stats->rs_more)
  704. return 0;
  705. /*
  706. * Return immediately if the RX descriptor has been marked
  707. * as corrupt based on the various error bits.
  708. *
  709. * This is different from the other corrupt descriptor
  710. * condition handled above.
  711. */
  712. if (rx_stats->rs_status & ATH9K_RXERR_CORRUPT_DESC)
  713. goto corrupt;
  714. hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len);
  715. ath9k_process_tsf(rx_stats, rx_status, tsf);
  716. ath_debug_stat_rx(sc, rx_stats);
  717. /*
  718. * Process PHY errors and return so that the packet
  719. * can be dropped.
  720. */
  721. if (rx_stats->rs_status & ATH9K_RXERR_PHY) {
  722. ath9k_dfs_process_phyerr(sc, hdr, rx_stats, rx_status->mactime);
  723. if (ath_process_fft(sc, hdr, rx_stats, rx_status->mactime))
  724. RX_STAT_INC(rx_spectral);
  725. return -EINVAL;
  726. }
  727. /*
  728. * everything but the rate is checked here, the rate check is done
  729. * separately to avoid doing two lookups for a rate for each frame.
  730. */
  731. spin_lock_bh(&sc->chan_lock);
  732. if (!ath9k_cmn_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error,
  733. sc->cur_chan->rxfilter)) {
  734. spin_unlock_bh(&sc->chan_lock);
  735. return -EINVAL;
  736. }
  737. spin_unlock_bh(&sc->chan_lock);
  738. if (ath_is_mybeacon(common, hdr)) {
  739. RX_STAT_INC(rx_beacons);
  740. rx_stats->is_mybeacon = true;
  741. }
  742. /*
  743. * This shouldn't happen, but have a safety check anyway.
  744. */
  745. if (WARN_ON(!ah->curchan))
  746. return -EINVAL;
  747. if (ath9k_cmn_process_rate(common, hw, rx_stats, rx_status)) {
  748. /*
  749. * No valid hardware bitrate found -- we should not get here
  750. * because hardware has already validated this frame as OK.
  751. */
  752. ath_dbg(common, ANY, "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
  753. rx_stats->rs_rate);
  754. RX_STAT_INC(rx_rate_err);
  755. return -EINVAL;
  756. }
  757. if (ath9k_is_chanctx_enabled()) {
  758. if (rx_stats->is_mybeacon)
  759. ath_chanctx_beacon_recv_ev(sc,
  760. ATH_CHANCTX_EVENT_BEACON_RECEIVED);
  761. }
  762. ath9k_cmn_process_rssi(common, hw, rx_stats, rx_status);
  763. rx_status->band = ah->curchan->chan->band;
  764. rx_status->freq = ah->curchan->chan->center_freq;
  765. rx_status->antenna = rx_stats->rs_antenna;
  766. rx_status->flag |= RX_FLAG_MACTIME_END;
  767. #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
  768. if (ieee80211_is_data_present(hdr->frame_control) &&
  769. !ieee80211_is_qos_nullfunc(hdr->frame_control))
  770. sc->rx.num_pkts++;
  771. #endif
  772. return 0;
  773. corrupt:
  774. sc->rx.discard_next = rx_stats->rs_more;
  775. return -EINVAL;
  776. }
  777. /*
  778. * Run the LNA combining algorithm only in these cases:
  779. *
  780. * Standalone WLAN cards with both LNA/Antenna diversity
  781. * enabled in the EEPROM.
  782. *
  783. * WLAN+BT cards which are in the supported card list
  784. * in ath_pci_id_table and the user has loaded the
  785. * driver with "bt_ant_diversity" set to true.
  786. */
  787. static void ath9k_antenna_check(struct ath_softc *sc,
  788. struct ath_rx_status *rs)
  789. {
  790. struct ath_hw *ah = sc->sc_ah;
  791. struct ath9k_hw_capabilities *pCap = &ah->caps;
  792. struct ath_common *common = ath9k_hw_common(ah);
  793. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB))
  794. return;
  795. /*
  796. * Change the default rx antenna if rx diversity
  797. * chooses the other antenna 3 times in a row.
  798. */
  799. if (sc->rx.defant != rs->rs_antenna) {
  800. if (++sc->rx.rxotherant >= 3)
  801. ath_setdefantenna(sc, rs->rs_antenna);
  802. } else {
  803. sc->rx.rxotherant = 0;
  804. }
  805. if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) {
  806. if (common->bt_ant_diversity)
  807. ath_ant_comb_scan(sc, rs);
  808. } else {
  809. ath_ant_comb_scan(sc, rs);
  810. }
  811. }
  812. static void ath9k_apply_ampdu_details(struct ath_softc *sc,
  813. struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
  814. {
  815. if (rs->rs_isaggr) {
  816. rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
  817. rxs->ampdu_reference = sc->rx.ampdu_ref;
  818. if (!rs->rs_moreaggr) {
  819. rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
  820. sc->rx.ampdu_ref++;
  821. }
  822. if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
  823. rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
  824. }
  825. }
  826. int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
  827. {
  828. struct ath_rxbuf *bf;
  829. struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
  830. struct ieee80211_rx_status *rxs;
  831. struct ath_hw *ah = sc->sc_ah;
  832. struct ath_common *common = ath9k_hw_common(ah);
  833. struct ieee80211_hw *hw = sc->hw;
  834. int retval;
  835. struct ath_rx_status rs;
  836. enum ath9k_rx_qtype qtype;
  837. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  838. int dma_type;
  839. u64 tsf = 0;
  840. unsigned long flags;
  841. dma_addr_t new_buf_addr;
  842. unsigned int budget = 512;
  843. struct ieee80211_hdr *hdr;
  844. if (edma)
  845. dma_type = DMA_BIDIRECTIONAL;
  846. else
  847. dma_type = DMA_FROM_DEVICE;
  848. qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
  849. tsf = ath9k_hw_gettsf64(ah);
  850. do {
  851. bool decrypt_error = false;
  852. memset(&rs, 0, sizeof(rs));
  853. if (edma)
  854. bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
  855. else
  856. bf = ath_get_next_rx_buf(sc, &rs);
  857. if (!bf)
  858. break;
  859. skb = bf->bf_mpdu;
  860. if (!skb)
  861. continue;
  862. /*
  863. * Take frame header from the first fragment and RX status from
  864. * the last one.
  865. */
  866. if (sc->rx.frag)
  867. hdr_skb = sc->rx.frag;
  868. else
  869. hdr_skb = skb;
  870. rxs = IEEE80211_SKB_RXCB(hdr_skb);
  871. memset(rxs, 0, sizeof(struct ieee80211_rx_status));
  872. retval = ath9k_rx_skb_preprocess(sc, hdr_skb, &rs, rxs,
  873. &decrypt_error, tsf);
  874. if (retval)
  875. goto requeue_drop_frag;
  876. /* Ensure we always have an skb to requeue once we are done
  877. * processing the current buffer's skb */
  878. requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
  879. /* If there is no memory we ignore the current RX'd frame,
  880. * tell hardware it can give us a new frame using the old
  881. * skb and put it at the tail of the sc->rx.rxbuf list for
  882. * processing. */
  883. if (!requeue_skb) {
  884. RX_STAT_INC(rx_oom_err);
  885. goto requeue_drop_frag;
  886. }
  887. /* We will now give hardware our shiny new allocated skb */
  888. new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
  889. common->rx_bufsize, dma_type);
  890. if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
  891. dev_kfree_skb_any(requeue_skb);
  892. goto requeue_drop_frag;
  893. }
  894. /* Unmap the frame */
  895. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  896. common->rx_bufsize, dma_type);
  897. bf->bf_mpdu = requeue_skb;
  898. bf->bf_buf_addr = new_buf_addr;
  899. skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
  900. if (ah->caps.rx_status_len)
  901. skb_pull(skb, ah->caps.rx_status_len);
  902. if (!rs.rs_more)
  903. ath9k_cmn_rx_skb_postprocess(common, hdr_skb, &rs,
  904. rxs, decrypt_error);
  905. if (rs.rs_more) {
  906. RX_STAT_INC(rx_frags);
  907. /*
  908. * rs_more indicates chained descriptors which can be
  909. * used to link buffers together for a sort of
  910. * scatter-gather operation.
  911. */
  912. if (sc->rx.frag) {
  913. /* too many fragments - cannot handle frame */
  914. dev_kfree_skb_any(sc->rx.frag);
  915. dev_kfree_skb_any(skb);
  916. RX_STAT_INC(rx_too_many_frags_err);
  917. skb = NULL;
  918. }
  919. sc->rx.frag = skb;
  920. goto requeue;
  921. }
  922. if (sc->rx.frag) {
  923. int space = skb->len - skb_tailroom(hdr_skb);
  924. if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
  925. dev_kfree_skb(skb);
  926. RX_STAT_INC(rx_oom_err);
  927. goto requeue_drop_frag;
  928. }
  929. sc->rx.frag = NULL;
  930. skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
  931. skb->len);
  932. dev_kfree_skb_any(skb);
  933. skb = hdr_skb;
  934. }
  935. if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
  936. skb_trim(skb, skb->len - 8);
  937. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  938. if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
  939. PS_WAIT_FOR_CAB |
  940. PS_WAIT_FOR_PSPOLL_DATA)) ||
  941. ath9k_check_auto_sleep(sc))
  942. ath_rx_ps(sc, skb, rs.is_mybeacon);
  943. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  944. ath9k_antenna_check(sc, &rs);
  945. ath9k_apply_ampdu_details(sc, &rs, rxs);
  946. ath_debug_rate_stats(sc, &rs, skb);
  947. hdr = (struct ieee80211_hdr *)skb->data;
  948. if (ieee80211_is_ack(hdr->frame_control))
  949. ath_dynack_sample_ack_ts(sc->sc_ah, skb, rs.rs_tstamp);
  950. ieee80211_rx(hw, skb);
  951. requeue_drop_frag:
  952. if (sc->rx.frag) {
  953. dev_kfree_skb_any(sc->rx.frag);
  954. sc->rx.frag = NULL;
  955. }
  956. requeue:
  957. list_add_tail(&bf->list, &sc->rx.rxbuf);
  958. if (!edma) {
  959. ath_rx_buf_relink(sc, bf, flush);
  960. if (!flush)
  961. ath9k_hw_rxena(ah);
  962. } else if (!flush) {
  963. ath_rx_edma_buf_link(sc, qtype);
  964. }
  965. if (!budget--)
  966. break;
  967. } while (1);
  968. if (!(ah->imask & ATH9K_INT_RXEOL)) {
  969. ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  970. ath9k_hw_set_interrupts(ah);
  971. }
  972. return 0;
  973. }