beacon.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /*
  2. * Copyright (c) 2008 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. /* Implementation of beacon processing. */
  17. #include <asm/unaligned.h>
  18. #include "core.h"
  19. /*
  20. * Configure parameters for the beacon queue
  21. *
  22. * This function will modify certain transmit queue properties depending on
  23. * the operating mode of the station (AP or AdHoc). Parameters are AIFS
  24. * settings and channel width min/max
  25. */
  26. static int ath_beaconq_config(struct ath_softc *sc)
  27. {
  28. struct ath_hal *ah = sc->sc_ah;
  29. struct ath9k_tx_queue_info qi;
  30. ath9k_hw_get_txq_props(ah, sc->sc_bhalq, &qi);
  31. if (sc->sc_opmode == ATH9K_M_HOSTAP) {
  32. /* Always burst out beacon and CAB traffic. */
  33. qi.tqi_aifs = 1;
  34. qi.tqi_cwmin = 0;
  35. qi.tqi_cwmax = 0;
  36. } else {
  37. /* Adhoc mode; important thing is to use 2x cwmin. */
  38. qi.tqi_aifs = sc->sc_beacon_qi.tqi_aifs;
  39. qi.tqi_cwmin = 2*sc->sc_beacon_qi.tqi_cwmin;
  40. qi.tqi_cwmax = sc->sc_beacon_qi.tqi_cwmax;
  41. }
  42. if (!ath9k_hw_set_txq_props(ah, sc->sc_bhalq, &qi)) {
  43. DPRINTF(sc, ATH_DBG_FATAL,
  44. "%s: unable to update h/w beacon queue parameters\n",
  45. __func__);
  46. return 0;
  47. } else {
  48. ath9k_hw_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */
  49. return 1;
  50. }
  51. }
  52. /*
  53. * Setup the beacon frame for transmit.
  54. *
  55. * Associates the beacon frame buffer with a transmit descriptor. Will set
  56. * up all required antenna switch parameters, rate codes, and channel flags.
  57. * Beacons are always sent out at the lowest rate, and are not retried.
  58. */
  59. static void ath_beacon_setup(struct ath_softc *sc,
  60. struct ath_vap *avp, struct ath_buf *bf)
  61. {
  62. struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
  63. struct ath_hal *ah = sc->sc_ah;
  64. struct ath_desc *ds;
  65. int flags, antenna;
  66. const struct ath9k_rate_table *rt;
  67. u8 rix, rate;
  68. int ctsrate = 0;
  69. int ctsduration = 0;
  70. struct ath9k_11n_rate_series series[4];
  71. DPRINTF(sc, ATH_DBG_BEACON, "%s: m %p len %u\n",
  72. __func__, skb, skb->len);
  73. /* setup descriptors */
  74. ds = bf->bf_desc;
  75. flags = ATH9K_TXDESC_NOACK;
  76. if (sc->sc_opmode == ATH9K_M_IBSS &&
  77. (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
  78. ds->ds_link = bf->bf_daddr; /* self-linked */
  79. flags |= ATH9K_TXDESC_VEOL;
  80. /* Let hardware handle antenna switching. */
  81. antenna = 0;
  82. } else {
  83. ds->ds_link = 0;
  84. /*
  85. * Switch antenna every beacon.
  86. * Should only switch every beacon period, not for every
  87. * SWBA's
  88. * XXX assumes two antenna
  89. */
  90. antenna = ((sc->ast_be_xmit / sc->sc_nbcnvaps) & 1 ? 2 : 1);
  91. }
  92. ds->ds_data = bf->bf_buf_addr;
  93. /*
  94. * Calculate rate code.
  95. * XXX everything at min xmit rate
  96. */
  97. rix = 0;
  98. rt = sc->sc_currates;
  99. rate = rt->info[rix].rateCode;
  100. if (sc->sc_flags & ATH_PREAMBLE_SHORT)
  101. rate |= rt->info[rix].shortPreamble;
  102. ath9k_hw_set11n_txdesc(ah, ds
  103. , skb->len + FCS_LEN /* frame length */
  104. , ATH9K_PKT_TYPE_BEACON /* Atheros packet type */
  105. , avp->av_btxctl.txpower /* txpower XXX */
  106. , ATH9K_TXKEYIX_INVALID /* no encryption */
  107. , ATH9K_KEY_TYPE_CLEAR /* no encryption */
  108. , flags /* no ack, veol for beacons */
  109. );
  110. /* NB: beacon's BufLen must be a multiple of 4 bytes */
  111. ath9k_hw_filltxdesc(ah, ds
  112. , roundup(skb->len, 4) /* buffer length */
  113. , true /* first segment */
  114. , true /* last segment */
  115. , ds /* first descriptor */
  116. );
  117. memzero(series, sizeof(struct ath9k_11n_rate_series) * 4);
  118. series[0].Tries = 1;
  119. series[0].Rate = rate;
  120. series[0].ChSel = sc->sc_tx_chainmask;
  121. series[0].RateFlags = (ctsrate) ? ATH9K_RATESERIES_RTS_CTS : 0;
  122. ath9k_hw_set11n_ratescenario(ah, ds, ds, 0,
  123. ctsrate, ctsduration, series, 4, 0);
  124. }
  125. /* Move everything from the vap's mcast queue to the hardware cab queue.
  126. * Caller must hold mcasq lock and cabq lock
  127. * XXX MORE_DATA bit?
  128. */
  129. static void empty_mcastq_into_cabq(struct ath_hal *ah,
  130. struct ath_txq *mcastq, struct ath_txq *cabq)
  131. {
  132. struct ath_buf *bfmcast;
  133. BUG_ON(list_empty(&mcastq->axq_q));
  134. bfmcast = list_first_entry(&mcastq->axq_q, struct ath_buf, list);
  135. /* link the descriptors */
  136. if (!cabq->axq_link)
  137. ath9k_hw_puttxbuf(ah, cabq->axq_qnum, bfmcast->bf_daddr);
  138. else
  139. *cabq->axq_link = bfmcast->bf_daddr;
  140. /* append the private vap mcast list to the cabq */
  141. cabq->axq_depth += mcastq->axq_depth;
  142. cabq->axq_totalqueued += mcastq->axq_totalqueued;
  143. cabq->axq_linkbuf = mcastq->axq_linkbuf;
  144. cabq->axq_link = mcastq->axq_link;
  145. list_splice_tail_init(&mcastq->axq_q, &cabq->axq_q);
  146. mcastq->axq_depth = 0;
  147. mcastq->axq_totalqueued = 0;
  148. mcastq->axq_linkbuf = NULL;
  149. mcastq->axq_link = NULL;
  150. }
  151. /* This is only run at DTIM. We move everything from the vap's mcast queue
  152. * to the hardware cab queue. Caller must hold the mcastq lock. */
  153. static void trigger_mcastq(struct ath_hal *ah,
  154. struct ath_txq *mcastq, struct ath_txq *cabq)
  155. {
  156. spin_lock_bh(&cabq->axq_lock);
  157. if (!list_empty(&mcastq->axq_q))
  158. empty_mcastq_into_cabq(ah, mcastq, cabq);
  159. /* cabq is gated by beacon so it is safe to start here */
  160. if (!list_empty(&cabq->axq_q))
  161. ath9k_hw_txstart(ah, cabq->axq_qnum);
  162. spin_unlock_bh(&cabq->axq_lock);
  163. }
  164. /*
  165. * Generate beacon frame and queue cab data for a vap.
  166. *
  167. * Updates the contents of the beacon frame. It is assumed that the buffer for
  168. * the beacon frame has been allocated in the ATH object, and simply needs to
  169. * be filled for this cycle. Also, any CAB (crap after beacon?) traffic will
  170. * be added to the beacon frame at this point.
  171. */
  172. static struct ath_buf *ath_beacon_generate(struct ath_softc *sc, int if_id)
  173. {
  174. struct ath_hal *ah = sc->sc_ah;
  175. struct ath_buf *bf;
  176. struct ath_vap *avp;
  177. struct sk_buff *skb;
  178. int cabq_depth;
  179. int mcastq_depth;
  180. int is_beacon_dtim = 0;
  181. unsigned int curlen;
  182. struct ath_txq *cabq;
  183. struct ath_txq *mcastq;
  184. avp = sc->sc_vaps[if_id];
  185. mcastq = &avp->av_mcastq;
  186. cabq = sc->sc_cabq;
  187. ASSERT(avp);
  188. if (avp->av_bcbuf == NULL) {
  189. DPRINTF(sc, ATH_DBG_BEACON, "%s: avp=%p av_bcbuf=%p\n",
  190. __func__, avp, avp->av_bcbuf);
  191. return NULL;
  192. }
  193. bf = avp->av_bcbuf;
  194. skb = (struct sk_buff *) bf->bf_mpdu;
  195. /*
  196. * Update dynamic beacon contents. If this returns
  197. * non-zero then we need to remap the memory because
  198. * the beacon frame changed size (probably because
  199. * of the TIM bitmap).
  200. */
  201. curlen = skb->len;
  202. /* XXX: spin_lock_bh should not be used here, but sparse bitches
  203. * otherwise. We should fix sparse :) */
  204. spin_lock_bh(&mcastq->axq_lock);
  205. mcastq_depth = avp->av_mcastq.axq_depth;
  206. if (ath_update_beacon(sc, if_id, &avp->av_boff, skb, mcastq_depth) ==
  207. 1) {
  208. ath_skb_unmap_single(sc, skb, PCI_DMA_TODEVICE,
  209. get_dma_mem_context(bf, bf_dmacontext));
  210. bf->bf_buf_addr = ath_skb_map_single(sc, skb, PCI_DMA_TODEVICE,
  211. get_dma_mem_context(bf, bf_dmacontext));
  212. } else {
  213. pci_dma_sync_single_for_cpu(sc->pdev,
  214. bf->bf_buf_addr,
  215. skb_tailroom(skb),
  216. PCI_DMA_TODEVICE);
  217. }
  218. /*
  219. * if the CABQ traffic from previous DTIM is pending and the current
  220. * beacon is also a DTIM.
  221. * 1) if there is only one vap let the cab traffic continue.
  222. * 2) if there are more than one vap and we are using staggered
  223. * beacons, then drain the cabq by dropping all the frames in
  224. * the cabq so that the current vaps cab traffic can be scheduled.
  225. */
  226. spin_lock_bh(&cabq->axq_lock);
  227. cabq_depth = cabq->axq_depth;
  228. spin_unlock_bh(&cabq->axq_lock);
  229. is_beacon_dtim = avp->av_boff.bo_tim[4] & 1;
  230. if (mcastq_depth && is_beacon_dtim && cabq_depth) {
  231. /*
  232. * Unlock the cabq lock as ath_tx_draintxq acquires
  233. * the lock again which is a common function and that
  234. * acquires txq lock inside.
  235. */
  236. if (sc->sc_nvaps > 1) {
  237. ath_tx_draintxq(sc, cabq, false);
  238. DPRINTF(sc, ATH_DBG_BEACON,
  239. "%s: flush previous cabq traffic\n", __func__);
  240. }
  241. }
  242. /* Construct tx descriptor. */
  243. ath_beacon_setup(sc, avp, bf);
  244. /*
  245. * Enable the CAB queue before the beacon queue to
  246. * insure cab frames are triggered by this beacon.
  247. */
  248. if (is_beacon_dtim)
  249. trigger_mcastq(ah, mcastq, cabq);
  250. spin_unlock_bh(&mcastq->axq_lock);
  251. return bf;
  252. }
  253. /*
  254. * Startup beacon transmission for adhoc mode when they are sent entirely
  255. * by the hardware using the self-linked descriptor + veol trick.
  256. */
  257. static void ath_beacon_start_adhoc(struct ath_softc *sc, int if_id)
  258. {
  259. struct ath_hal *ah = sc->sc_ah;
  260. struct ath_buf *bf;
  261. struct ath_vap *avp;
  262. struct sk_buff *skb;
  263. avp = sc->sc_vaps[if_id];
  264. ASSERT(avp);
  265. if (avp->av_bcbuf == NULL) {
  266. DPRINTF(sc, ATH_DBG_BEACON, "%s: avp=%p av_bcbuf=%p\n",
  267. __func__, avp, avp != NULL ? avp->av_bcbuf : NULL);
  268. return;
  269. }
  270. bf = avp->av_bcbuf;
  271. skb = (struct sk_buff *) bf->bf_mpdu;
  272. /* Construct tx descriptor. */
  273. ath_beacon_setup(sc, avp, bf);
  274. /* NB: caller is known to have already stopped tx dma */
  275. ath9k_hw_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
  276. ath9k_hw_txstart(ah, sc->sc_bhalq);
  277. DPRINTF(sc, ATH_DBG_BEACON, "%s: TXDP%u = %llx (%p)\n", __func__,
  278. sc->sc_bhalq, ito64(bf->bf_daddr), bf->bf_desc);
  279. }
  280. /*
  281. * Setup a h/w transmit queue for beacons.
  282. *
  283. * This function allocates an information structure (struct ath9k_txq_info)
  284. * on the stack, sets some specific parameters (zero out channel width
  285. * min/max, and enable aifs). The info structure does not need to be
  286. * persistant.
  287. */
  288. int ath_beaconq_setup(struct ath_hal *ah)
  289. {
  290. struct ath9k_tx_queue_info qi;
  291. memzero(&qi, sizeof(qi));
  292. qi.tqi_aifs = 1;
  293. qi.tqi_cwmin = 0;
  294. qi.tqi_cwmax = 0;
  295. /* NB: don't enable any interrupts */
  296. return ath9k_hw_setuptxqueue(ah, ATH9K_TX_QUEUE_BEACON, &qi);
  297. }
  298. /*
  299. * Allocate and setup an initial beacon frame.
  300. *
  301. * Allocate a beacon state variable for a specific VAP instance created on
  302. * the ATH interface. This routine also calculates the beacon "slot" for
  303. * staggared beacons in the mBSSID case.
  304. */
  305. int ath_beacon_alloc(struct ath_softc *sc, int if_id)
  306. {
  307. struct ath_vap *avp;
  308. struct ieee80211_hdr *wh;
  309. struct ath_buf *bf;
  310. struct sk_buff *skb;
  311. avp = sc->sc_vaps[if_id];
  312. ASSERT(avp);
  313. /* Allocate a beacon descriptor if we haven't done so. */
  314. if (!avp->av_bcbuf) {
  315. /*
  316. * Allocate beacon state for hostap/ibss. We know
  317. * a buffer is available.
  318. */
  319. avp->av_bcbuf = list_first_entry(&sc->sc_bbuf,
  320. struct ath_buf, list);
  321. list_del(&avp->av_bcbuf->list);
  322. if (sc->sc_opmode == ATH9K_M_HOSTAP ||
  323. !(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
  324. int slot;
  325. /*
  326. * Assign the vap to a beacon xmit slot. As
  327. * above, this cannot fail to find one.
  328. */
  329. avp->av_bslot = 0;
  330. for (slot = 0; slot < ATH_BCBUF; slot++)
  331. if (sc->sc_bslot[slot] == ATH_IF_ID_ANY) {
  332. /*
  333. * XXX hack, space out slots to better
  334. * deal with misses
  335. */
  336. if (slot+1 < ATH_BCBUF &&
  337. sc->sc_bslot[slot+1] ==
  338. ATH_IF_ID_ANY) {
  339. avp->av_bslot = slot+1;
  340. break;
  341. }
  342. avp->av_bslot = slot;
  343. /* NB: keep looking for a double slot */
  344. }
  345. BUG_ON(sc->sc_bslot[avp->av_bslot] != ATH_IF_ID_ANY);
  346. sc->sc_bslot[avp->av_bslot] = if_id;
  347. sc->sc_nbcnvaps++;
  348. }
  349. }
  350. /* release the previous beacon frame , if it already exists. */
  351. bf = avp->av_bcbuf;
  352. if (bf->bf_mpdu != NULL) {
  353. skb = (struct sk_buff *)bf->bf_mpdu;
  354. ath_skb_unmap_single(sc, skb, PCI_DMA_TODEVICE,
  355. get_dma_mem_context(bf, bf_dmacontext));
  356. dev_kfree_skb_any(skb);
  357. bf->bf_mpdu = NULL;
  358. }
  359. /*
  360. * NB: the beacon data buffer must be 32-bit aligned;
  361. * we assume the wbuf routines will return us something
  362. * with this alignment (perhaps should assert).
  363. * FIXME: Fill avp->av_boff.bo_tim,avp->av_btxctl.txpower and
  364. * avp->av_btxctl.shortPreamble
  365. */
  366. skb = ieee80211_beacon_get(sc->hw, avp->av_if_data);
  367. if (skb == NULL) {
  368. DPRINTF(sc, ATH_DBG_BEACON, "%s: cannot get skb\n",
  369. __func__);
  370. return -ENOMEM;
  371. }
  372. /*
  373. * Calculate a TSF adjustment factor required for
  374. * staggered beacons. Note that we assume the format
  375. * of the beacon frame leaves the tstamp field immediately
  376. * following the header.
  377. */
  378. if (avp->av_bslot > 0) {
  379. u64 tsfadjust;
  380. __le64 val;
  381. int intval;
  382. /* FIXME: Use default value for now: Sujith */
  383. intval = ATH_DEFAULT_BINTVAL;
  384. /*
  385. * The beacon interval is in TU's; the TSF in usecs.
  386. * We figure out how many TU's to add to align the
  387. * timestamp then convert to TSF units and handle
  388. * byte swapping before writing it in the frame.
  389. * The hardware will then add this each time a beacon
  390. * frame is sent. Note that we align vap's 1..N
  391. * and leave vap 0 untouched. This means vap 0
  392. * has a timestamp in one beacon interval while the
  393. * others get a timestamp aligned to the next interval.
  394. */
  395. tsfadjust = (intval * (ATH_BCBUF - avp->av_bslot)) / ATH_BCBUF;
  396. val = cpu_to_le64(tsfadjust << 10); /* TU->TSF */
  397. DPRINTF(sc, ATH_DBG_BEACON,
  398. "%s: %s beacons, bslot %d intval %u tsfadjust %llu\n",
  399. __func__, "stagger",
  400. avp->av_bslot, intval, (unsigned long long)tsfadjust);
  401. wh = (struct ieee80211_hdr *)skb->data;
  402. memcpy(&wh[1], &val, sizeof(val));
  403. }
  404. bf->bf_buf_addr = ath_skb_map_single(sc, skb, PCI_DMA_TODEVICE,
  405. get_dma_mem_context(bf, bf_dmacontext));
  406. bf->bf_mpdu = skb;
  407. return 0;
  408. }
  409. /*
  410. * Reclaim beacon resources and return buffer to the pool.
  411. *
  412. * Checks the VAP to put the beacon frame buffer back to the ATH object
  413. * queue, and de-allocates any wbuf frames that were sent as CAB traffic.
  414. */
  415. void ath_beacon_return(struct ath_softc *sc, struct ath_vap *avp)
  416. {
  417. if (avp->av_bcbuf != NULL) {
  418. struct ath_buf *bf;
  419. if (avp->av_bslot != -1) {
  420. sc->sc_bslot[avp->av_bslot] = ATH_IF_ID_ANY;
  421. sc->sc_nbcnvaps--;
  422. }
  423. bf = avp->av_bcbuf;
  424. if (bf->bf_mpdu != NULL) {
  425. struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
  426. ath_skb_unmap_single(sc, skb, PCI_DMA_TODEVICE,
  427. get_dma_mem_context(bf, bf_dmacontext));
  428. dev_kfree_skb_any(skb);
  429. bf->bf_mpdu = NULL;
  430. }
  431. list_add_tail(&bf->list, &sc->sc_bbuf);
  432. avp->av_bcbuf = NULL;
  433. }
  434. }
  435. /*
  436. * Reclaim beacon resources and return buffer to the pool.
  437. *
  438. * This function will free any wbuf frames that are still attached to the
  439. * beacon buffers in the ATH object. Note that this does not de-allocate
  440. * any wbuf objects that are in the transmit queue and have not yet returned
  441. * to the ATH object.
  442. */
  443. void ath_beacon_free(struct ath_softc *sc)
  444. {
  445. struct ath_buf *bf;
  446. list_for_each_entry(bf, &sc->sc_bbuf, list) {
  447. if (bf->bf_mpdu != NULL) {
  448. struct sk_buff *skb = (struct sk_buff *) bf->bf_mpdu;
  449. ath_skb_unmap_single(sc, skb, PCI_DMA_TODEVICE,
  450. get_dma_mem_context(bf, bf_dmacontext));
  451. dev_kfree_skb_any(skb);
  452. bf->bf_mpdu = NULL;
  453. }
  454. }
  455. }
  456. /*
  457. * Tasklet for Sending Beacons
  458. *
  459. * Transmit one or more beacon frames at SWBA. Dynamic updates to the frame
  460. * contents are done as needed and the slot time is also adjusted based on
  461. * current state.
  462. *
  463. * This tasklet is not scheduled, it's called in ISR context.
  464. */
  465. void ath9k_beacon_tasklet(unsigned long data)
  466. {
  467. #define TSF_TO_TU(_h,_l) \
  468. ((((u32)(_h)) << 22) | (((u32)(_l)) >> 10))
  469. struct ath_softc *sc = (struct ath_softc *)data;
  470. struct ath_hal *ah = sc->sc_ah;
  471. struct ath_buf *bf = NULL;
  472. int slot, if_id;
  473. u32 bfaddr;
  474. u32 rx_clear = 0, rx_frame = 0, tx_frame = 0;
  475. u32 show_cycles = 0;
  476. u32 bc = 0; /* beacon count */
  477. u64 tsf;
  478. u32 tsftu;
  479. u16 intval;
  480. if (sc->sc_noreset) {
  481. show_cycles = ath9k_hw_GetMibCycleCountsPct(ah,
  482. &rx_clear,
  483. &rx_frame,
  484. &tx_frame);
  485. }
  486. /*
  487. * Check if the previous beacon has gone out. If
  488. * not don't try to post another, skip this period
  489. * and wait for the next. Missed beacons indicate
  490. * a problem and should not occur. If we miss too
  491. * many consecutive beacons reset the device.
  492. */
  493. if (ath9k_hw_numtxpending(ah, sc->sc_bhalq) != 0) {
  494. sc->sc_bmisscount++;
  495. /* XXX: doth needs the chanchange IE countdown decremented.
  496. * We should consider adding a mac80211 call to indicate
  497. * a beacon miss so appropriate action could be taken
  498. * (in that layer).
  499. */
  500. if (sc->sc_bmisscount < BSTUCK_THRESH) {
  501. if (sc->sc_noreset) {
  502. DPRINTF(sc, ATH_DBG_BEACON,
  503. "%s: missed %u consecutive beacons\n",
  504. __func__, sc->sc_bmisscount);
  505. if (show_cycles) {
  506. /*
  507. * Display cycle counter stats
  508. * from HW to aide in debug of
  509. * stickiness.
  510. */
  511. DPRINTF(sc,
  512. ATH_DBG_BEACON,
  513. "%s: busy times: rx_clear=%d, "
  514. "rx_frame=%d, tx_frame=%d\n",
  515. __func__, rx_clear, rx_frame,
  516. tx_frame);
  517. } else {
  518. DPRINTF(sc,
  519. ATH_DBG_BEACON,
  520. "%s: unable to obtain "
  521. "busy times\n", __func__);
  522. }
  523. } else {
  524. DPRINTF(sc, ATH_DBG_BEACON,
  525. "%s: missed %u consecutive beacons\n",
  526. __func__, sc->sc_bmisscount);
  527. }
  528. } else if (sc->sc_bmisscount >= BSTUCK_THRESH) {
  529. if (sc->sc_noreset) {
  530. if (sc->sc_bmisscount == BSTUCK_THRESH) {
  531. DPRINTF(sc,
  532. ATH_DBG_BEACON,
  533. "%s: beacon is officially "
  534. "stuck\n", __func__);
  535. ath9k_hw_dmaRegDump(ah);
  536. }
  537. } else {
  538. DPRINTF(sc, ATH_DBG_BEACON,
  539. "%s: beacon is officially stuck\n",
  540. __func__);
  541. ath_bstuck_process(sc);
  542. }
  543. }
  544. return;
  545. }
  546. if (sc->sc_bmisscount != 0) {
  547. if (sc->sc_noreset) {
  548. DPRINTF(sc,
  549. ATH_DBG_BEACON,
  550. "%s: resume beacon xmit after %u misses\n",
  551. __func__, sc->sc_bmisscount);
  552. } else {
  553. DPRINTF(sc, ATH_DBG_BEACON,
  554. "%s: resume beacon xmit after %u misses\n",
  555. __func__, sc->sc_bmisscount);
  556. }
  557. sc->sc_bmisscount = 0;
  558. }
  559. /*
  560. * Generate beacon frames. we are sending frames
  561. * staggered so calculate the slot for this frame based
  562. * on the tsf to safeguard against missing an swba.
  563. */
  564. /* FIXME: Use default value for now - Sujith */
  565. intval = ATH_DEFAULT_BINTVAL;
  566. tsf = ath9k_hw_gettsf64(ah);
  567. tsftu = TSF_TO_TU(tsf>>32, tsf);
  568. slot = ((tsftu % intval) * ATH_BCBUF) / intval;
  569. if_id = sc->sc_bslot[(slot + 1) % ATH_BCBUF];
  570. DPRINTF(sc, ATH_DBG_BEACON,
  571. "%s: slot %d [tsf %llu tsftu %u intval %u] if_id %d\n",
  572. __func__, slot, (unsigned long long) tsf, tsftu,
  573. intval, if_id);
  574. bfaddr = 0;
  575. if (if_id != ATH_IF_ID_ANY) {
  576. bf = ath_beacon_generate(sc, if_id);
  577. if (bf != NULL) {
  578. bfaddr = bf->bf_daddr;
  579. bc = 1;
  580. }
  581. }
  582. /*
  583. * Handle slot time change when a non-ERP station joins/leaves
  584. * an 11g network. The 802.11 layer notifies us via callback,
  585. * we mark updateslot, then wait one beacon before effecting
  586. * the change. This gives associated stations at least one
  587. * beacon interval to note the state change.
  588. *
  589. * NB: The slot time change state machine is clocked according
  590. * to whether we are bursting or staggering beacons. We
  591. * recognize the request to update and record the current
  592. * slot then don't transition until that slot is reached
  593. * again. If we miss a beacon for that slot then we'll be
  594. * slow to transition but we'll be sure at least one beacon
  595. * interval has passed. When bursting slot is always left
  596. * set to ATH_BCBUF so this check is a noop.
  597. */
  598. /* XXX locking */
  599. if (sc->sc_updateslot == UPDATE) {
  600. sc->sc_updateslot = COMMIT; /* commit next beacon */
  601. sc->sc_slotupdate = slot;
  602. } else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot)
  603. ath_setslottime(sc); /* commit change to hardware */
  604. if (bfaddr != 0) {
  605. /*
  606. * Stop any current dma and put the new frame(s) on the queue.
  607. * This should never fail since we check above that no frames
  608. * are still pending on the queue.
  609. */
  610. if (!ath9k_hw_stoptxdma(ah, sc->sc_bhalq)) {
  611. DPRINTF(sc, ATH_DBG_FATAL,
  612. "%s: beacon queue %u did not stop?\n",
  613. __func__, sc->sc_bhalq);
  614. /* NB: the HAL still stops DMA, so proceed */
  615. }
  616. /* NB: cabq traffic should already be queued and primed */
  617. ath9k_hw_puttxbuf(ah, sc->sc_bhalq, bfaddr);
  618. ath9k_hw_txstart(ah, sc->sc_bhalq);
  619. sc->ast_be_xmit += bc; /* XXX per-vap? */
  620. }
  621. #undef TSF_TO_TU
  622. }
  623. /*
  624. * Tasklet for Beacon Stuck processing
  625. *
  626. * Processing for Beacon Stuck.
  627. * Basically calls the ath_internal_reset function to reset the chip.
  628. */
  629. void ath_bstuck_process(struct ath_softc *sc)
  630. {
  631. DPRINTF(sc, ATH_DBG_BEACON,
  632. "%s: stuck beacon; resetting (bmiss count %u)\n",
  633. __func__, sc->sc_bmisscount);
  634. ath_internal_reset(sc);
  635. }
  636. /*
  637. * Configure the beacon and sleep timers.
  638. *
  639. * When operating as an AP this resets the TSF and sets
  640. * up the hardware to notify us when we need to issue beacons.
  641. *
  642. * When operating in station mode this sets up the beacon
  643. * timers according to the timestamp of the last received
  644. * beacon and the current TSF, configures PCF and DTIM
  645. * handling, programs the sleep registers so the hardware
  646. * will wakeup in time to receive beacons, and configures
  647. * the beacon miss handling so we'll receive a BMISS
  648. * interrupt when we stop seeing beacons from the AP
  649. * we've associated with.
  650. */
  651. void ath_beacon_config(struct ath_softc *sc, int if_id)
  652. {
  653. #define TSF_TO_TU(_h,_l) \
  654. ((((u32)(_h)) << 22) | (((u32)(_l)) >> 10))
  655. struct ath_hal *ah = sc->sc_ah;
  656. u32 nexttbtt, intval;
  657. struct ath_beacon_config conf;
  658. enum ath9k_opmode av_opmode;
  659. if (if_id != ATH_IF_ID_ANY)
  660. av_opmode = sc->sc_vaps[if_id]->av_opmode;
  661. else
  662. av_opmode = sc->sc_opmode;
  663. memzero(&conf, sizeof(struct ath_beacon_config));
  664. /* FIXME: Use default values for now - Sujith */
  665. /* Query beacon configuration first */
  666. /*
  667. * Protocol stack doesn't support dynamic beacon configuration,
  668. * use default configurations.
  669. */
  670. conf.beacon_interval = ATH_DEFAULT_BINTVAL;
  671. conf.listen_interval = 1;
  672. conf.dtim_period = conf.beacon_interval;
  673. conf.dtim_count = 1;
  674. conf.bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf.beacon_interval;
  675. /* extract tstamp from last beacon and convert to TU */
  676. nexttbtt = TSF_TO_TU(get_unaligned_le32(conf.u.last_tstamp + 4),
  677. get_unaligned_le32(conf.u.last_tstamp));
  678. /* XXX conditionalize multi-bss support? */
  679. if (sc->sc_opmode == ATH9K_M_HOSTAP) {
  680. /*
  681. * For multi-bss ap support beacons are either staggered
  682. * evenly over N slots or burst together. For the former
  683. * arrange for the SWBA to be delivered for each slot.
  684. * Slots that are not occupied will generate nothing.
  685. */
  686. /* NB: the beacon interval is kept internally in TU's */
  687. intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
  688. intval /= ATH_BCBUF; /* for staggered beacons */
  689. } else {
  690. intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
  691. }
  692. if (nexttbtt == 0) /* e.g. for ap mode */
  693. nexttbtt = intval;
  694. else if (intval) /* NB: can be 0 for monitor mode */
  695. nexttbtt = roundup(nexttbtt, intval);
  696. DPRINTF(sc, ATH_DBG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
  697. __func__, nexttbtt, intval, conf.beacon_interval);
  698. /* Check for ATH9K_M_HOSTAP and sc_nostabeacons for WDS client */
  699. if (sc->sc_opmode == ATH9K_M_STA) {
  700. struct ath9k_beacon_state bs;
  701. u64 tsf;
  702. u32 tsftu;
  703. int dtimperiod, dtimcount, sleepduration;
  704. int cfpperiod, cfpcount;
  705. /*
  706. * Setup dtim and cfp parameters according to
  707. * last beacon we received (which may be none).
  708. */
  709. dtimperiod = conf.dtim_period;
  710. if (dtimperiod <= 0) /* NB: 0 if not known */
  711. dtimperiod = 1;
  712. dtimcount = conf.dtim_count;
  713. if (dtimcount >= dtimperiod) /* NB: sanity check */
  714. dtimcount = 0; /* XXX? */
  715. cfpperiod = 1; /* NB: no PCF support yet */
  716. cfpcount = 0;
  717. sleepduration = conf.listen_interval * intval;
  718. if (sleepduration <= 0)
  719. sleepduration = intval;
  720. #define FUDGE 2
  721. /*
  722. * Pull nexttbtt forward to reflect the current
  723. * TSF and calculate dtim+cfp state for the result.
  724. */
  725. tsf = ath9k_hw_gettsf64(ah);
  726. tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
  727. do {
  728. nexttbtt += intval;
  729. if (--dtimcount < 0) {
  730. dtimcount = dtimperiod - 1;
  731. if (--cfpcount < 0)
  732. cfpcount = cfpperiod - 1;
  733. }
  734. } while (nexttbtt < tsftu);
  735. #undef FUDGE
  736. memzero(&bs, sizeof(bs));
  737. bs.bs_intval = intval;
  738. bs.bs_nexttbtt = nexttbtt;
  739. bs.bs_dtimperiod = dtimperiod*intval;
  740. bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
  741. bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
  742. bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
  743. bs.bs_cfpmaxduration = 0;
  744. /*
  745. * Calculate the number of consecutive beacons to miss
  746. * before taking a BMISS interrupt. The configuration
  747. * is specified in TU so we only need calculate based
  748. * on the beacon interval. Note that we clamp the
  749. * result to at most 15 beacons.
  750. */
  751. if (sleepduration > intval) {
  752. bs.bs_bmissthreshold =
  753. conf.listen_interval *
  754. ATH_DEFAULT_BMISS_LIMIT / 2;
  755. } else {
  756. bs.bs_bmissthreshold =
  757. DIV_ROUND_UP(conf.bmiss_timeout, intval);
  758. if (bs.bs_bmissthreshold > 15)
  759. bs.bs_bmissthreshold = 15;
  760. else if (bs.bs_bmissthreshold <= 0)
  761. bs.bs_bmissthreshold = 1;
  762. }
  763. /*
  764. * Calculate sleep duration. The configuration is
  765. * given in ms. We insure a multiple of the beacon
  766. * period is used. Also, if the sleep duration is
  767. * greater than the DTIM period then it makes senses
  768. * to make it a multiple of that.
  769. *
  770. * XXX fixed at 100ms
  771. */
  772. bs.bs_sleepduration =
  773. roundup(IEEE80211_MS_TO_TU(100), sleepduration);
  774. if (bs.bs_sleepduration > bs.bs_dtimperiod)
  775. bs.bs_sleepduration = bs.bs_dtimperiod;
  776. DPRINTF(sc, ATH_DBG_BEACON,
  777. "%s: tsf %llu "
  778. "tsf:tu %u "
  779. "intval %u "
  780. "nexttbtt %u "
  781. "dtim %u "
  782. "nextdtim %u "
  783. "bmiss %u "
  784. "sleep %u "
  785. "cfp:period %u "
  786. "maxdur %u "
  787. "next %u "
  788. "timoffset %u\n"
  789. , __func__
  790. , (unsigned long long)tsf, tsftu
  791. , bs.bs_intval
  792. , bs.bs_nexttbtt
  793. , bs.bs_dtimperiod
  794. , bs.bs_nextdtim
  795. , bs.bs_bmissthreshold
  796. , bs.bs_sleepduration
  797. , bs.bs_cfpperiod
  798. , bs.bs_cfpmaxduration
  799. , bs.bs_cfpnext
  800. , bs.bs_timoffset
  801. );
  802. ath9k_hw_set_interrupts(ah, 0);
  803. ath9k_hw_set_sta_beacon_timers(ah, &bs);
  804. sc->sc_imask |= ATH9K_INT_BMISS;
  805. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  806. } else {
  807. u64 tsf;
  808. u32 tsftu;
  809. ath9k_hw_set_interrupts(ah, 0);
  810. if (nexttbtt == intval)
  811. intval |= ATH9K_BEACON_RESET_TSF;
  812. if (sc->sc_opmode == ATH9K_M_IBSS) {
  813. /*
  814. * Pull nexttbtt forward to reflect the current
  815. * TSF .
  816. */
  817. #define FUDGE 2
  818. if (!(intval & ATH9K_BEACON_RESET_TSF)) {
  819. tsf = ath9k_hw_gettsf64(ah);
  820. tsftu = TSF_TO_TU((u32)(tsf>>32),
  821. (u32)tsf) + FUDGE;
  822. do {
  823. nexttbtt += intval;
  824. } while (nexttbtt < tsftu);
  825. }
  826. #undef FUDGE
  827. DPRINTF(sc, ATH_DBG_BEACON,
  828. "%s: IBSS nexttbtt %u intval %u (%u)\n",
  829. __func__, nexttbtt,
  830. intval & ~ATH9K_BEACON_RESET_TSF,
  831. conf.beacon_interval);
  832. /*
  833. * In IBSS mode enable the beacon timers but only
  834. * enable SWBA interrupts if we need to manually
  835. * prepare beacon frames. Otherwise we use a
  836. * self-linked tx descriptor and let the hardware
  837. * deal with things.
  838. */
  839. intval |= ATH9K_BEACON_ENA;
  840. if (!(ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
  841. sc->sc_imask |= ATH9K_INT_SWBA;
  842. ath_beaconq_config(sc);
  843. } else if (sc->sc_opmode == ATH9K_M_HOSTAP) {
  844. /*
  845. * In AP mode we enable the beacon timers and
  846. * SWBA interrupts to prepare beacon frames.
  847. */
  848. intval |= ATH9K_BEACON_ENA;
  849. sc->sc_imask |= ATH9K_INT_SWBA; /* beacon prepare */
  850. ath_beaconq_config(sc);
  851. }
  852. ath9k_hw_beaconinit(ah, nexttbtt, intval);
  853. sc->sc_bmisscount = 0;
  854. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  855. /*
  856. * When using a self-linked beacon descriptor in
  857. * ibss mode load it once here.
  858. */
  859. if (sc->sc_opmode == ATH9K_M_IBSS &&
  860. (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
  861. ath_beacon_start_adhoc(sc, 0);
  862. }
  863. #undef TSF_TO_TU
  864. }
  865. /* Function to collect beacon rssi data and resync beacon if necessary */
  866. void ath_beacon_sync(struct ath_softc *sc, int if_id)
  867. {
  868. /*
  869. * Resync beacon timers using the tsf of the
  870. * beacon frame we just received.
  871. */
  872. ath_beacon_config(sc, if_id);
  873. sc->sc_beacons = 1;
  874. }