recv.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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. {
  35. struct ath_hw *ah = sc->sc_ah;
  36. struct ath_common *common = ath9k_hw_common(ah);
  37. struct ath_desc *ds;
  38. struct sk_buff *skb;
  39. ds = bf->bf_desc;
  40. ds->ds_link = 0; /* link to null */
  41. ds->ds_data = bf->bf_buf_addr;
  42. /* virtual addr of the beginning of the buffer. */
  43. skb = bf->bf_mpdu;
  44. BUG_ON(skb == NULL);
  45. ds->ds_vdata = skb->data;
  46. /*
  47. * setup rx descriptors. The rx_bufsize here tells the hardware
  48. * how much data it can DMA to us and that we are prepared
  49. * to process
  50. */
  51. ath9k_hw_setuprxdesc(ah, ds,
  52. common->rx_bufsize,
  53. 0);
  54. if (sc->rx.rxlink == NULL)
  55. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  56. else
  57. *sc->rx.rxlink = bf->bf_daddr;
  58. sc->rx.rxlink = &ds->ds_link;
  59. }
  60. static void ath_rx_buf_relink(struct ath_softc *sc, struct ath_rxbuf *bf)
  61. {
  62. if (sc->rx.buf_hold)
  63. ath_rx_buf_link(sc, sc->rx.buf_hold);
  64. sc->rx.buf_hold = bf;
  65. }
  66. static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
  67. {
  68. /* XXX block beacon interrupts */
  69. ath9k_hw_setantenna(sc->sc_ah, antenna);
  70. sc->rx.defant = antenna;
  71. sc->rx.rxotherant = 0;
  72. }
  73. static void ath_opmode_init(struct ath_softc *sc)
  74. {
  75. struct ath_hw *ah = sc->sc_ah;
  76. struct ath_common *common = ath9k_hw_common(ah);
  77. u32 rfilt, mfilt[2];
  78. /* configure rx filter */
  79. rfilt = ath_calcrxfilter(sc);
  80. ath9k_hw_setrxfilter(ah, rfilt);
  81. /* configure bssid mask */
  82. ath_hw_setbssidmask(common);
  83. /* configure operational mode */
  84. ath9k_hw_setopmode(ah);
  85. /* calculate and install multicast filter */
  86. mfilt[0] = mfilt[1] = ~0;
  87. ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
  88. }
  89. static bool ath_rx_edma_buf_link(struct ath_softc *sc,
  90. enum ath9k_rx_qtype qtype)
  91. {
  92. struct ath_hw *ah = sc->sc_ah;
  93. struct ath_rx_edma *rx_edma;
  94. struct sk_buff *skb;
  95. struct ath_rxbuf *bf;
  96. rx_edma = &sc->rx.rx_edma[qtype];
  97. if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
  98. return false;
  99. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  100. list_del_init(&bf->list);
  101. skb = bf->bf_mpdu;
  102. memset(skb->data, 0, ah->caps.rx_status_len);
  103. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  104. ah->caps.rx_status_len, DMA_TO_DEVICE);
  105. SKB_CB_ATHBUF(skb) = bf;
  106. ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
  107. __skb_queue_tail(&rx_edma->rx_fifo, skb);
  108. return true;
  109. }
  110. static void ath_rx_addbuffer_edma(struct ath_softc *sc,
  111. enum ath9k_rx_qtype qtype)
  112. {
  113. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  114. struct ath_rxbuf *bf, *tbf;
  115. if (list_empty(&sc->rx.rxbuf)) {
  116. ath_dbg(common, QUEUE, "No free rx buf available\n");
  117. return;
  118. }
  119. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
  120. if (!ath_rx_edma_buf_link(sc, qtype))
  121. break;
  122. }
  123. static void ath_rx_remove_buffer(struct ath_softc *sc,
  124. enum ath9k_rx_qtype qtype)
  125. {
  126. struct ath_rxbuf *bf;
  127. struct ath_rx_edma *rx_edma;
  128. struct sk_buff *skb;
  129. rx_edma = &sc->rx.rx_edma[qtype];
  130. while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
  131. bf = SKB_CB_ATHBUF(skb);
  132. BUG_ON(!bf);
  133. list_add_tail(&bf->list, &sc->rx.rxbuf);
  134. }
  135. }
  136. static void ath_rx_edma_cleanup(struct ath_softc *sc)
  137. {
  138. struct ath_hw *ah = sc->sc_ah;
  139. struct ath_common *common = ath9k_hw_common(ah);
  140. struct ath_rxbuf *bf;
  141. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  142. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  143. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  144. if (bf->bf_mpdu) {
  145. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  146. common->rx_bufsize,
  147. DMA_BIDIRECTIONAL);
  148. dev_kfree_skb_any(bf->bf_mpdu);
  149. bf->bf_buf_addr = 0;
  150. bf->bf_mpdu = NULL;
  151. }
  152. }
  153. }
  154. static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
  155. {
  156. __skb_queue_head_init(&rx_edma->rx_fifo);
  157. rx_edma->rx_fifo_hwsize = size;
  158. }
  159. static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
  160. {
  161. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  162. struct ath_hw *ah = sc->sc_ah;
  163. struct sk_buff *skb;
  164. struct ath_rxbuf *bf;
  165. int error = 0, i;
  166. u32 size;
  167. ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
  168. ah->caps.rx_status_len);
  169. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
  170. ah->caps.rx_lp_qdepth);
  171. ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
  172. ah->caps.rx_hp_qdepth);
  173. size = sizeof(struct ath_rxbuf) * nbufs;
  174. bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
  175. if (!bf)
  176. return -ENOMEM;
  177. INIT_LIST_HEAD(&sc->rx.rxbuf);
  178. for (i = 0; i < nbufs; i++, bf++) {
  179. skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
  180. if (!skb) {
  181. error = -ENOMEM;
  182. goto rx_init_fail;
  183. }
  184. memset(skb->data, 0, common->rx_bufsize);
  185. bf->bf_mpdu = skb;
  186. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  187. common->rx_bufsize,
  188. DMA_BIDIRECTIONAL);
  189. if (unlikely(dma_mapping_error(sc->dev,
  190. bf->bf_buf_addr))) {
  191. dev_kfree_skb_any(skb);
  192. bf->bf_mpdu = NULL;
  193. bf->bf_buf_addr = 0;
  194. ath_err(common,
  195. "dma_mapping_error() on RX init\n");
  196. error = -ENOMEM;
  197. goto rx_init_fail;
  198. }
  199. list_add_tail(&bf->list, &sc->rx.rxbuf);
  200. }
  201. return 0;
  202. rx_init_fail:
  203. ath_rx_edma_cleanup(sc);
  204. return error;
  205. }
  206. static void ath_edma_start_recv(struct ath_softc *sc)
  207. {
  208. ath9k_hw_rxena(sc->sc_ah);
  209. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP);
  210. ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP);
  211. ath_opmode_init(sc);
  212. ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
  213. }
  214. static void ath_edma_stop_recv(struct ath_softc *sc)
  215. {
  216. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
  217. ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
  218. }
  219. int ath_rx_init(struct ath_softc *sc, int nbufs)
  220. {
  221. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  222. struct sk_buff *skb;
  223. struct ath_rxbuf *bf;
  224. int error = 0;
  225. spin_lock_init(&sc->sc_pcu_lock);
  226. common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
  227. sc->sc_ah->caps.rx_status_len;
  228. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  229. return ath_rx_edma_init(sc, nbufs);
  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. err:
  265. if (error)
  266. ath_rx_cleanup(sc);
  267. return error;
  268. }
  269. void ath_rx_cleanup(struct ath_softc *sc)
  270. {
  271. struct ath_hw *ah = sc->sc_ah;
  272. struct ath_common *common = ath9k_hw_common(ah);
  273. struct sk_buff *skb;
  274. struct ath_rxbuf *bf;
  275. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  276. ath_rx_edma_cleanup(sc);
  277. return;
  278. }
  279. list_for_each_entry(bf, &sc->rx.rxbuf, list) {
  280. skb = bf->bf_mpdu;
  281. if (skb) {
  282. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  283. common->rx_bufsize,
  284. DMA_FROM_DEVICE);
  285. dev_kfree_skb(skb);
  286. bf->bf_buf_addr = 0;
  287. bf->bf_mpdu = NULL;
  288. }
  289. }
  290. }
  291. /*
  292. * Calculate the receive filter according to the
  293. * operating mode and state:
  294. *
  295. * o always accept unicast, broadcast, and multicast traffic
  296. * o maintain current state of phy error reception (the hal
  297. * may enable phy error frames for noise immunity work)
  298. * o probe request frames are accepted only when operating in
  299. * hostap, adhoc, or monitor modes
  300. * o enable promiscuous mode according to the interface state
  301. * o accept beacons:
  302. * - when operating in adhoc mode so the 802.11 layer creates
  303. * node table entries for peers,
  304. * - when operating in station mode for collecting rssi data when
  305. * the station is otherwise quiet, or
  306. * - when operating as a repeater so we see repeater-sta beacons
  307. * - when scanning
  308. */
  309. u32 ath_calcrxfilter(struct ath_softc *sc)
  310. {
  311. u32 rfilt;
  312. if (config_enabled(CONFIG_ATH9K_TX99))
  313. return 0;
  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) || AR_SREV_9531(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_rxbuf *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.buf_hold = NULL;
  362. sc->rx.rxlink = NULL;
  363. list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
  364. ath_rx_buf_link(sc, bf);
  365. }
  366. /* We could have deleted elements so the list may be empty now */
  367. if (list_empty(&sc->rx.rxbuf))
  368. goto start_recv;
  369. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  370. ath9k_hw_putrxbuf(ah, bf->bf_daddr);
  371. ath9k_hw_rxena(ah);
  372. start_recv:
  373. ath_opmode_init(sc);
  374. ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
  375. return 0;
  376. }
  377. static void ath_flushrecv(struct ath_softc *sc)
  378. {
  379. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  380. ath_rx_tasklet(sc, 1, true);
  381. ath_rx_tasklet(sc, 1, false);
  382. }
  383. bool ath_stoprecv(struct ath_softc *sc)
  384. {
  385. struct ath_hw *ah = sc->sc_ah;
  386. bool stopped, reset = false;
  387. ath9k_hw_abortpcurecv(ah);
  388. ath9k_hw_setrxfilter(ah, 0);
  389. stopped = ath9k_hw_stopdmarecv(ah, &reset);
  390. ath_flushrecv(sc);
  391. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  392. ath_edma_stop_recv(sc);
  393. else
  394. sc->rx.rxlink = NULL;
  395. if (!(ah->ah_flags & AH_UNPLUGGED) &&
  396. unlikely(!stopped)) {
  397. ath_err(ath9k_hw_common(sc->sc_ah),
  398. "Could not stop RX, we could be "
  399. "confusing the DMA engine when we start RX up\n");
  400. ATH_DBG_WARN_ON_ONCE(!stopped);
  401. }
  402. return stopped && !reset;
  403. }
  404. static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
  405. {
  406. /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
  407. struct ieee80211_mgmt *mgmt;
  408. u8 *pos, *end, id, elen;
  409. struct ieee80211_tim_ie *tim;
  410. mgmt = (struct ieee80211_mgmt *)skb->data;
  411. pos = mgmt->u.beacon.variable;
  412. end = skb->data + skb->len;
  413. while (pos + 2 < end) {
  414. id = *pos++;
  415. elen = *pos++;
  416. if (pos + elen > end)
  417. break;
  418. if (id == WLAN_EID_TIM) {
  419. if (elen < sizeof(*tim))
  420. break;
  421. tim = (struct ieee80211_tim_ie *) pos;
  422. if (tim->dtim_count != 0)
  423. break;
  424. return tim->bitmap_ctrl & 0x01;
  425. }
  426. pos += elen;
  427. }
  428. return false;
  429. }
  430. static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
  431. {
  432. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  433. if (skb->len < 24 + 8 + 2 + 2)
  434. return;
  435. sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
  436. if (sc->ps_flags & PS_BEACON_SYNC) {
  437. sc->ps_flags &= ~PS_BEACON_SYNC;
  438. ath_dbg(common, PS,
  439. "Reconfigure beacon timers based on synchronized timestamp\n");
  440. if (!(WARN_ON_ONCE(sc->cur_beacon_conf.beacon_interval == 0)))
  441. ath9k_set_beacon(sc);
  442. if (sc->p2p_ps_vif)
  443. ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
  444. }
  445. if (ath_beacon_dtim_pending_cab(skb)) {
  446. /*
  447. * Remain awake waiting for buffered broadcast/multicast
  448. * frames. If the last broadcast/multicast frame is not
  449. * received properly, the next beacon frame will work as
  450. * a backup trigger for returning into NETWORK SLEEP state,
  451. * so we are waiting for it as well.
  452. */
  453. ath_dbg(common, PS,
  454. "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
  455. sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
  456. return;
  457. }
  458. if (sc->ps_flags & PS_WAIT_FOR_CAB) {
  459. /*
  460. * This can happen if a broadcast frame is dropped or the AP
  461. * fails to send a frame indicating that all CAB frames have
  462. * been delivered.
  463. */
  464. sc->ps_flags &= ~PS_WAIT_FOR_CAB;
  465. ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
  466. }
  467. }
  468. static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
  469. {
  470. struct ieee80211_hdr *hdr;
  471. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  472. hdr = (struct ieee80211_hdr *)skb->data;
  473. /* Process Beacon and CAB receive in PS state */
  474. if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
  475. && mybeacon) {
  476. ath_rx_ps_beacon(sc, skb);
  477. } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
  478. (ieee80211_is_data(hdr->frame_control) ||
  479. ieee80211_is_action(hdr->frame_control)) &&
  480. is_multicast_ether_addr(hdr->addr1) &&
  481. !ieee80211_has_moredata(hdr->frame_control)) {
  482. /*
  483. * No more broadcast/multicast frames to be received at this
  484. * point.
  485. */
  486. sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
  487. ath_dbg(common, PS,
  488. "All PS CAB frames received, back to sleep\n");
  489. } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
  490. !is_multicast_ether_addr(hdr->addr1) &&
  491. !ieee80211_has_morefrags(hdr->frame_control)) {
  492. sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
  493. ath_dbg(common, PS,
  494. "Going back to sleep after having received PS-Poll data (0x%lx)\n",
  495. sc->ps_flags & (PS_WAIT_FOR_BEACON |
  496. PS_WAIT_FOR_CAB |
  497. PS_WAIT_FOR_PSPOLL_DATA |
  498. PS_WAIT_FOR_TX_ACK));
  499. }
  500. }
  501. static bool ath_edma_get_buffers(struct ath_softc *sc,
  502. enum ath9k_rx_qtype qtype,
  503. struct ath_rx_status *rs,
  504. struct ath_rxbuf **dest)
  505. {
  506. struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
  507. struct ath_hw *ah = sc->sc_ah;
  508. struct ath_common *common = ath9k_hw_common(ah);
  509. struct sk_buff *skb;
  510. struct ath_rxbuf *bf;
  511. int ret;
  512. skb = skb_peek(&rx_edma->rx_fifo);
  513. if (!skb)
  514. return false;
  515. bf = SKB_CB_ATHBUF(skb);
  516. BUG_ON(!bf);
  517. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  518. common->rx_bufsize, DMA_FROM_DEVICE);
  519. ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
  520. if (ret == -EINPROGRESS) {
  521. /*let device gain the buffer again*/
  522. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  523. common->rx_bufsize, DMA_FROM_DEVICE);
  524. return false;
  525. }
  526. __skb_unlink(skb, &rx_edma->rx_fifo);
  527. if (ret == -EINVAL) {
  528. /* corrupt descriptor, skip this one and the following one */
  529. list_add_tail(&bf->list, &sc->rx.rxbuf);
  530. ath_rx_edma_buf_link(sc, qtype);
  531. skb = skb_peek(&rx_edma->rx_fifo);
  532. if (skb) {
  533. bf = SKB_CB_ATHBUF(skb);
  534. BUG_ON(!bf);
  535. __skb_unlink(skb, &rx_edma->rx_fifo);
  536. list_add_tail(&bf->list, &sc->rx.rxbuf);
  537. ath_rx_edma_buf_link(sc, qtype);
  538. }
  539. bf = NULL;
  540. }
  541. *dest = bf;
  542. return true;
  543. }
  544. static struct ath_rxbuf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
  545. struct ath_rx_status *rs,
  546. enum ath9k_rx_qtype qtype)
  547. {
  548. struct ath_rxbuf *bf = NULL;
  549. while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
  550. if (!bf)
  551. continue;
  552. return bf;
  553. }
  554. return NULL;
  555. }
  556. static struct ath_rxbuf *ath_get_next_rx_buf(struct ath_softc *sc,
  557. struct ath_rx_status *rs)
  558. {
  559. struct ath_hw *ah = sc->sc_ah;
  560. struct ath_common *common = ath9k_hw_common(ah);
  561. struct ath_desc *ds;
  562. struct ath_rxbuf *bf;
  563. int ret;
  564. if (list_empty(&sc->rx.rxbuf)) {
  565. sc->rx.rxlink = NULL;
  566. return NULL;
  567. }
  568. bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
  569. if (bf == sc->rx.buf_hold)
  570. return NULL;
  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_rxbuf *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_rxbuf, 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. * Re-check previous descriptor, in case it has been filled
  610. * in the mean time.
  611. */
  612. ret = ath9k_hw_rxprocdesc(ah, ds, rs);
  613. if (ret == -EINPROGRESS) {
  614. /*
  615. * mark descriptor as zero-length and set the 'more'
  616. * flag to ensure that both buffers get discarded
  617. */
  618. rs->rs_datalen = 0;
  619. rs->rs_more = true;
  620. }
  621. }
  622. list_del(&bf->list);
  623. if (!bf->bf_mpdu)
  624. return bf;
  625. /*
  626. * Synchronize the DMA transfer with CPU before
  627. * 1. accessing the frame
  628. * 2. requeueing the same buffer to h/w
  629. */
  630. dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
  631. common->rx_bufsize,
  632. DMA_FROM_DEVICE);
  633. return bf;
  634. }
  635. static void ath9k_process_tsf(struct ath_rx_status *rs,
  636. struct ieee80211_rx_status *rxs,
  637. u64 tsf)
  638. {
  639. u32 tsf_lower = tsf & 0xffffffff;
  640. rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp;
  641. if (rs->rs_tstamp > tsf_lower &&
  642. unlikely(rs->rs_tstamp - tsf_lower > 0x10000000))
  643. rxs->mactime -= 0x100000000ULL;
  644. if (rs->rs_tstamp < tsf_lower &&
  645. unlikely(tsf_lower - rs->rs_tstamp > 0x10000000))
  646. rxs->mactime += 0x100000000ULL;
  647. }
  648. /*
  649. * For Decrypt or Demic errors, we only mark packet status here and always push
  650. * up the frame up to let mac80211 handle the actual error case, be it no
  651. * decryption key or real decryption error. This let us keep statistics there.
  652. */
  653. static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
  654. struct sk_buff *skb,
  655. struct ath_rx_status *rx_stats,
  656. struct ieee80211_rx_status *rx_status,
  657. bool *decrypt_error, u64 tsf)
  658. {
  659. struct ieee80211_hw *hw = sc->hw;
  660. struct ath_hw *ah = sc->sc_ah;
  661. struct ath_common *common = ath9k_hw_common(ah);
  662. struct ieee80211_hdr *hdr;
  663. bool discard_current = sc->rx.discard_next;
  664. /*
  665. * Discard corrupt descriptors which are marked in
  666. * ath_get_next_rx_buf().
  667. */
  668. if (discard_current)
  669. goto corrupt;
  670. sc->rx.discard_next = false;
  671. /*
  672. * Discard zero-length packets.
  673. */
  674. if (!rx_stats->rs_datalen) {
  675. RX_STAT_INC(rx_len_err);
  676. goto corrupt;
  677. }
  678. /*
  679. * rs_status follows rs_datalen so if rs_datalen is too large
  680. * we can take a hint that hardware corrupted it, so ignore
  681. * those frames.
  682. */
  683. if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) {
  684. RX_STAT_INC(rx_len_err);
  685. goto corrupt;
  686. }
  687. /* Only use status info from the last fragment */
  688. if (rx_stats->rs_more)
  689. return 0;
  690. /*
  691. * Return immediately if the RX descriptor has been marked
  692. * as corrupt based on the various error bits.
  693. *
  694. * This is different from the other corrupt descriptor
  695. * condition handled above.
  696. */
  697. if (rx_stats->rs_status & ATH9K_RXERR_CORRUPT_DESC)
  698. goto corrupt;
  699. hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len);
  700. ath9k_process_tsf(rx_stats, rx_status, tsf);
  701. ath_debug_stat_rx(sc, rx_stats);
  702. /*
  703. * Process PHY errors and return so that the packet
  704. * can be dropped.
  705. */
  706. if (rx_stats->rs_status & ATH9K_RXERR_PHY) {
  707. ath9k_dfs_process_phyerr(sc, hdr, rx_stats, rx_status->mactime);
  708. if (ath_process_fft(sc, hdr, rx_stats, rx_status->mactime))
  709. RX_STAT_INC(rx_spectral);
  710. return -EINVAL;
  711. }
  712. /*
  713. * everything but the rate is checked here, the rate check is done
  714. * separately to avoid doing two lookups for a rate for each frame.
  715. */
  716. if (!ath9k_cmn_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error, sc->rx.rxfilter))
  717. return -EINVAL;
  718. if (ath_is_mybeacon(common, hdr)) {
  719. RX_STAT_INC(rx_beacons);
  720. rx_stats->is_mybeacon = true;
  721. }
  722. /*
  723. * This shouldn't happen, but have a safety check anyway.
  724. */
  725. if (WARN_ON(!ah->curchan))
  726. return -EINVAL;
  727. if (ath9k_cmn_process_rate(common, hw, rx_stats, rx_status)) {
  728. /*
  729. * No valid hardware bitrate found -- we should not get here
  730. * because hardware has already validated this frame as OK.
  731. */
  732. ath_dbg(common, ANY, "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
  733. rx_stats->rs_rate);
  734. RX_STAT_INC(rx_rate_err);
  735. return -EINVAL;
  736. }
  737. ath9k_cmn_process_rssi(common, hw, rx_stats, rx_status);
  738. rx_status->band = ah->curchan->chan->band;
  739. rx_status->freq = ah->curchan->chan->center_freq;
  740. rx_status->antenna = rx_stats->rs_antenna;
  741. rx_status->flag |= RX_FLAG_MACTIME_END;
  742. #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
  743. if (ieee80211_is_data_present(hdr->frame_control) &&
  744. !ieee80211_is_qos_nullfunc(hdr->frame_control))
  745. sc->rx.num_pkts++;
  746. #endif
  747. return 0;
  748. corrupt:
  749. sc->rx.discard_next = rx_stats->rs_more;
  750. return -EINVAL;
  751. }
  752. /*
  753. * Run the LNA combining algorithm only in these cases:
  754. *
  755. * Standalone WLAN cards with both LNA/Antenna diversity
  756. * enabled in the EEPROM.
  757. *
  758. * WLAN+BT cards which are in the supported card list
  759. * in ath_pci_id_table and the user has loaded the
  760. * driver with "bt_ant_diversity" set to true.
  761. */
  762. static void ath9k_antenna_check(struct ath_softc *sc,
  763. struct ath_rx_status *rs)
  764. {
  765. struct ath_hw *ah = sc->sc_ah;
  766. struct ath9k_hw_capabilities *pCap = &ah->caps;
  767. struct ath_common *common = ath9k_hw_common(ah);
  768. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB))
  769. return;
  770. /*
  771. * Change the default rx antenna if rx diversity
  772. * chooses the other antenna 3 times in a row.
  773. */
  774. if (sc->rx.defant != rs->rs_antenna) {
  775. if (++sc->rx.rxotherant >= 3)
  776. ath_setdefantenna(sc, rs->rs_antenna);
  777. } else {
  778. sc->rx.rxotherant = 0;
  779. }
  780. if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) {
  781. if (common->bt_ant_diversity)
  782. ath_ant_comb_scan(sc, rs);
  783. } else {
  784. ath_ant_comb_scan(sc, rs);
  785. }
  786. }
  787. static void ath9k_apply_ampdu_details(struct ath_softc *sc,
  788. struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
  789. {
  790. if (rs->rs_isaggr) {
  791. rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
  792. rxs->ampdu_reference = sc->rx.ampdu_ref;
  793. if (!rs->rs_moreaggr) {
  794. rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
  795. sc->rx.ampdu_ref++;
  796. }
  797. if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
  798. rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
  799. }
  800. }
  801. int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
  802. {
  803. struct ath_rxbuf *bf;
  804. struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
  805. struct ieee80211_rx_status *rxs;
  806. struct ath_hw *ah = sc->sc_ah;
  807. struct ath_common *common = ath9k_hw_common(ah);
  808. struct ieee80211_hw *hw = sc->hw;
  809. int retval;
  810. struct ath_rx_status rs;
  811. enum ath9k_rx_qtype qtype;
  812. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  813. int dma_type;
  814. u64 tsf = 0;
  815. unsigned long flags;
  816. dma_addr_t new_buf_addr;
  817. unsigned int budget = 512;
  818. if (edma)
  819. dma_type = DMA_BIDIRECTIONAL;
  820. else
  821. dma_type = DMA_FROM_DEVICE;
  822. qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
  823. tsf = ath9k_hw_gettsf64(ah);
  824. do {
  825. bool decrypt_error = false;
  826. memset(&rs, 0, sizeof(rs));
  827. if (edma)
  828. bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
  829. else
  830. bf = ath_get_next_rx_buf(sc, &rs);
  831. if (!bf)
  832. break;
  833. skb = bf->bf_mpdu;
  834. if (!skb)
  835. continue;
  836. /*
  837. * Take frame header from the first fragment and RX status from
  838. * the last one.
  839. */
  840. if (sc->rx.frag)
  841. hdr_skb = sc->rx.frag;
  842. else
  843. hdr_skb = skb;
  844. rxs = IEEE80211_SKB_RXCB(hdr_skb);
  845. memset(rxs, 0, sizeof(struct ieee80211_rx_status));
  846. retval = ath9k_rx_skb_preprocess(sc, hdr_skb, &rs, rxs,
  847. &decrypt_error, tsf);
  848. if (retval)
  849. goto requeue_drop_frag;
  850. /* Ensure we always have an skb to requeue once we are done
  851. * processing the current buffer's skb */
  852. requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
  853. /* If there is no memory we ignore the current RX'd frame,
  854. * tell hardware it can give us a new frame using the old
  855. * skb and put it at the tail of the sc->rx.rxbuf list for
  856. * processing. */
  857. if (!requeue_skb) {
  858. RX_STAT_INC(rx_oom_err);
  859. goto requeue_drop_frag;
  860. }
  861. /* We will now give hardware our shiny new allocated skb */
  862. new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
  863. common->rx_bufsize, dma_type);
  864. if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
  865. dev_kfree_skb_any(requeue_skb);
  866. goto requeue_drop_frag;
  867. }
  868. /* Unmap the frame */
  869. dma_unmap_single(sc->dev, bf->bf_buf_addr,
  870. common->rx_bufsize, dma_type);
  871. bf->bf_mpdu = requeue_skb;
  872. bf->bf_buf_addr = new_buf_addr;
  873. skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
  874. if (ah->caps.rx_status_len)
  875. skb_pull(skb, ah->caps.rx_status_len);
  876. if (!rs.rs_more)
  877. ath9k_cmn_rx_skb_postprocess(common, hdr_skb, &rs,
  878. rxs, decrypt_error);
  879. if (rs.rs_more) {
  880. RX_STAT_INC(rx_frags);
  881. /*
  882. * rs_more indicates chained descriptors which can be
  883. * used to link buffers together for a sort of
  884. * scatter-gather operation.
  885. */
  886. if (sc->rx.frag) {
  887. /* too many fragments - cannot handle frame */
  888. dev_kfree_skb_any(sc->rx.frag);
  889. dev_kfree_skb_any(skb);
  890. RX_STAT_INC(rx_too_many_frags_err);
  891. skb = NULL;
  892. }
  893. sc->rx.frag = skb;
  894. goto requeue;
  895. }
  896. if (sc->rx.frag) {
  897. int space = skb->len - skb_tailroom(hdr_skb);
  898. if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
  899. dev_kfree_skb(skb);
  900. RX_STAT_INC(rx_oom_err);
  901. goto requeue_drop_frag;
  902. }
  903. sc->rx.frag = NULL;
  904. skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
  905. skb->len);
  906. dev_kfree_skb_any(skb);
  907. skb = hdr_skb;
  908. }
  909. if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
  910. skb_trim(skb, skb->len - 8);
  911. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  912. if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
  913. PS_WAIT_FOR_CAB |
  914. PS_WAIT_FOR_PSPOLL_DATA)) ||
  915. ath9k_check_auto_sleep(sc))
  916. ath_rx_ps(sc, skb, rs.is_mybeacon);
  917. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  918. ath9k_antenna_check(sc, &rs);
  919. ath9k_apply_ampdu_details(sc, &rs, rxs);
  920. ath_debug_rate_stats(sc, &rs, skb);
  921. ieee80211_rx(hw, skb);
  922. requeue_drop_frag:
  923. if (sc->rx.frag) {
  924. dev_kfree_skb_any(sc->rx.frag);
  925. sc->rx.frag = NULL;
  926. }
  927. requeue:
  928. list_add_tail(&bf->list, &sc->rx.rxbuf);
  929. if (edma) {
  930. ath_rx_edma_buf_link(sc, qtype);
  931. } else {
  932. ath_rx_buf_relink(sc, bf);
  933. if (!flush)
  934. ath9k_hw_rxena(ah);
  935. }
  936. if (!budget--)
  937. break;
  938. } while (1);
  939. if (!(ah->imask & ATH9K_INT_RXEOL)) {
  940. ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  941. ath9k_hw_set_interrupts(ah);
  942. }
  943. return 0;
  944. }