beacon.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. * Copyright (c) 2008-2009 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 "ath9k.h"
  17. #define FUDGE 2
  18. /*
  19. * This function will modify certain transmit queue properties depending on
  20. * the operating mode of the station (AP or AdHoc). Parameters are AIFS
  21. * settings and channel width min/max
  22. */
  23. static int ath_beaconq_config(struct ath_softc *sc)
  24. {
  25. struct ath_hw *ah = sc->sc_ah;
  26. struct ath_common *common = ath9k_hw_common(ah);
  27. struct ath9k_tx_queue_info qi;
  28. ath9k_hw_get_txq_props(ah, sc->beacon.beaconq, &qi);
  29. if (sc->sc_ah->opmode == NL80211_IFTYPE_AP) {
  30. /* Always burst out beacon and CAB traffic. */
  31. qi.tqi_aifs = 1;
  32. qi.tqi_cwmin = 0;
  33. qi.tqi_cwmax = 0;
  34. } else {
  35. /* Adhoc mode; important thing is to use 2x cwmin. */
  36. qi.tqi_aifs = sc->beacon.beacon_qi.tqi_aifs;
  37. qi.tqi_cwmin = 2*sc->beacon.beacon_qi.tqi_cwmin;
  38. qi.tqi_cwmax = sc->beacon.beacon_qi.tqi_cwmax;
  39. }
  40. if (!ath9k_hw_set_txq_props(ah, sc->beacon.beaconq, &qi)) {
  41. ath_print(common, ATH_DBG_FATAL,
  42. "Unable to update h/w beacon queue parameters\n");
  43. return 0;
  44. } else {
  45. ath9k_hw_resettxqueue(ah, sc->beacon.beaconq);
  46. return 1;
  47. }
  48. }
  49. /*
  50. * Associates the beacon frame buffer with a transmit descriptor. Will set
  51. * up all required antenna switch parameters, rate codes, and channel flags.
  52. * Beacons are always sent out at the lowest rate, and are not retried.
  53. */
  54. static void ath_beacon_setup(struct ath_softc *sc, struct ath_vif *avp,
  55. struct ath_buf *bf)
  56. {
  57. struct sk_buff *skb = bf->bf_mpdu;
  58. struct ath_hw *ah = sc->sc_ah;
  59. struct ath_common *common = ath9k_hw_common(ah);
  60. struct ath_desc *ds;
  61. struct ath9k_11n_rate_series series[4];
  62. const struct ath_rate_table *rt;
  63. int flags, antenna, ctsrate = 0, ctsduration = 0;
  64. u8 rate;
  65. ds = bf->bf_desc;
  66. flags = ATH9K_TXDESC_NOACK;
  67. if (((sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC) ||
  68. (sc->sc_ah->opmode == NL80211_IFTYPE_MESH_POINT)) &&
  69. (ah->caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
  70. ds->ds_link = bf->bf_daddr; /* self-linked */
  71. flags |= ATH9K_TXDESC_VEOL;
  72. /* Let hardware handle antenna switching. */
  73. antenna = 0;
  74. } else {
  75. ds->ds_link = 0;
  76. /*
  77. * Switch antenna every beacon.
  78. * Should only switch every beacon period, not for every SWBA
  79. * XXX assumes two antennae
  80. */
  81. antenna = ((sc->beacon.ast_be_xmit / sc->nbcnvifs) & 1 ? 2 : 1);
  82. }
  83. ds->ds_data = bf->bf_buf_addr;
  84. rt = sc->cur_rate_table;
  85. rate = rt->info[0].ratecode;
  86. if (sc->sc_flags & SC_OP_PREAMBLE_SHORT)
  87. rate |= rt->info[0].short_preamble;
  88. ath9k_hw_set11n_txdesc(ah, ds, skb->len + FCS_LEN,
  89. ATH9K_PKT_TYPE_BEACON,
  90. MAX_RATE_POWER,
  91. ATH9K_TXKEYIX_INVALID,
  92. ATH9K_KEY_TYPE_CLEAR,
  93. flags);
  94. /* NB: beacon's BufLen must be a multiple of 4 bytes */
  95. ath9k_hw_filltxdesc(ah, ds, roundup(skb->len, 4),
  96. true, true, ds);
  97. memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
  98. series[0].Tries = 1;
  99. series[0].Rate = rate;
  100. series[0].ChSel = common->tx_chainmask;
  101. series[0].RateFlags = (ctsrate) ? ATH9K_RATESERIES_RTS_CTS : 0;
  102. ath9k_hw_set11n_ratescenario(ah, ds, ds, 0, ctsrate, ctsduration,
  103. series, 4, 0);
  104. }
  105. static struct ath_buf *ath_beacon_generate(struct ieee80211_hw *hw,
  106. struct ieee80211_vif *vif)
  107. {
  108. struct ath_wiphy *aphy = hw->priv;
  109. struct ath_softc *sc = aphy->sc;
  110. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  111. struct ath_buf *bf;
  112. struct ath_vif *avp;
  113. struct sk_buff *skb;
  114. struct ath_txq *cabq;
  115. struct ieee80211_tx_info *info;
  116. int cabq_depth;
  117. if (aphy->state != ATH_WIPHY_ACTIVE)
  118. return NULL;
  119. avp = (void *)vif->drv_priv;
  120. cabq = sc->beacon.cabq;
  121. if (avp->av_bcbuf == NULL)
  122. return NULL;
  123. /* Release the old beacon first */
  124. bf = avp->av_bcbuf;
  125. skb = bf->bf_mpdu;
  126. if (skb) {
  127. dma_unmap_single(sc->dev, bf->bf_dmacontext,
  128. skb->len, DMA_TO_DEVICE);
  129. dev_kfree_skb_any(skb);
  130. }
  131. /* Get a new beacon from mac80211 */
  132. skb = ieee80211_beacon_get(hw, vif);
  133. bf->bf_mpdu = skb;
  134. if (skb == NULL)
  135. return NULL;
  136. ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp =
  137. avp->tsf_adjust;
  138. info = IEEE80211_SKB_CB(skb);
  139. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  140. /*
  141. * TODO: make sure the seq# gets assigned properly (vs. other
  142. * TX frames)
  143. */
  144. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  145. sc->tx.seq_no += 0x10;
  146. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  147. hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
  148. }
  149. bf->bf_buf_addr = bf->bf_dmacontext =
  150. dma_map_single(sc->dev, skb->data,
  151. skb->len, DMA_TO_DEVICE);
  152. if (unlikely(dma_mapping_error(sc->dev, bf->bf_buf_addr))) {
  153. dev_kfree_skb_any(skb);
  154. bf->bf_mpdu = NULL;
  155. ath_print(common, ATH_DBG_FATAL,
  156. "dma_mapping_error on beaconing\n");
  157. return NULL;
  158. }
  159. skb = ieee80211_get_buffered_bc(hw, vif);
  160. /*
  161. * if the CABQ traffic from previous DTIM is pending and the current
  162. * beacon is also a DTIM.
  163. * 1) if there is only one vif let the cab traffic continue.
  164. * 2) if there are more than one vif and we are using staggered
  165. * beacons, then drain the cabq by dropping all the frames in
  166. * the cabq so that the current vifs cab traffic can be scheduled.
  167. */
  168. spin_lock_bh(&cabq->axq_lock);
  169. cabq_depth = cabq->axq_depth;
  170. spin_unlock_bh(&cabq->axq_lock);
  171. if (skb && cabq_depth) {
  172. if (sc->nvifs > 1) {
  173. ath_print(common, ATH_DBG_BEACON,
  174. "Flushing previous cabq traffic\n");
  175. ath_draintxq(sc, cabq, false);
  176. }
  177. }
  178. ath_beacon_setup(sc, avp, bf);
  179. while (skb) {
  180. ath_tx_cabq(hw, skb);
  181. skb = ieee80211_get_buffered_bc(hw, vif);
  182. }
  183. return bf;
  184. }
  185. /*
  186. * Startup beacon transmission for adhoc mode when they are sent entirely
  187. * by the hardware using the self-linked descriptor + veol trick.
  188. */
  189. static void ath_beacon_start_adhoc(struct ath_softc *sc,
  190. struct ieee80211_vif *vif)
  191. {
  192. struct ath_hw *ah = sc->sc_ah;
  193. struct ath_common *common = ath9k_hw_common(ah);
  194. struct ath_buf *bf;
  195. struct ath_vif *avp;
  196. struct sk_buff *skb;
  197. avp = (void *)vif->drv_priv;
  198. if (avp->av_bcbuf == NULL)
  199. return;
  200. bf = avp->av_bcbuf;
  201. skb = bf->bf_mpdu;
  202. ath_beacon_setup(sc, avp, bf);
  203. /* NB: caller is known to have already stopped tx dma */
  204. ath9k_hw_puttxbuf(ah, sc->beacon.beaconq, bf->bf_daddr);
  205. ath9k_hw_txstart(ah, sc->beacon.beaconq);
  206. ath_print(common, ATH_DBG_BEACON, "TXDP%u = %llx (%p)\n",
  207. sc->beacon.beaconq, ito64(bf->bf_daddr), bf->bf_desc);
  208. }
  209. int ath_beacon_alloc(struct ath_wiphy *aphy, struct ieee80211_vif *vif)
  210. {
  211. struct ath_softc *sc = aphy->sc;
  212. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  213. struct ath_vif *avp;
  214. struct ath_buf *bf;
  215. struct sk_buff *skb;
  216. __le64 tstamp;
  217. avp = (void *)vif->drv_priv;
  218. /* Allocate a beacon descriptor if we haven't done so. */
  219. if (!avp->av_bcbuf) {
  220. /* Allocate beacon state for hostap/ibss. We know
  221. * a buffer is available. */
  222. avp->av_bcbuf = list_first_entry(&sc->beacon.bbuf,
  223. struct ath_buf, list);
  224. list_del(&avp->av_bcbuf->list);
  225. if (sc->sc_ah->opmode == NL80211_IFTYPE_AP ||
  226. !(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
  227. int slot;
  228. /*
  229. * Assign the vif to a beacon xmit slot. As
  230. * above, this cannot fail to find one.
  231. */
  232. avp->av_bslot = 0;
  233. for (slot = 0; slot < ATH_BCBUF; slot++)
  234. if (sc->beacon.bslot[slot] == NULL) {
  235. /*
  236. * XXX hack, space out slots to better
  237. * deal with misses
  238. */
  239. if (slot+1 < ATH_BCBUF &&
  240. sc->beacon.bslot[slot+1] == NULL) {
  241. avp->av_bslot = slot+1;
  242. break;
  243. }
  244. avp->av_bslot = slot;
  245. /* NB: keep looking for a double slot */
  246. }
  247. BUG_ON(sc->beacon.bslot[avp->av_bslot] != NULL);
  248. sc->beacon.bslot[avp->av_bslot] = vif;
  249. sc->beacon.bslot_aphy[avp->av_bslot] = aphy;
  250. sc->nbcnvifs++;
  251. }
  252. }
  253. /* release the previous beacon frame, if it already exists. */
  254. bf = avp->av_bcbuf;
  255. if (bf->bf_mpdu != NULL) {
  256. skb = bf->bf_mpdu;
  257. dma_unmap_single(sc->dev, bf->bf_dmacontext,
  258. skb->len, DMA_TO_DEVICE);
  259. dev_kfree_skb_any(skb);
  260. bf->bf_mpdu = NULL;
  261. }
  262. /* NB: the beacon data buffer must be 32-bit aligned. */
  263. skb = ieee80211_beacon_get(sc->hw, vif);
  264. if (skb == NULL) {
  265. ath_print(common, ATH_DBG_BEACON, "cannot get skb\n");
  266. return -ENOMEM;
  267. }
  268. tstamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
  269. sc->beacon.bc_tstamp = le64_to_cpu(tstamp);
  270. /* Calculate a TSF adjustment factor required for staggered beacons. */
  271. if (avp->av_bslot > 0) {
  272. u64 tsfadjust;
  273. int intval;
  274. intval = sc->beacon_interval ? : ATH_DEFAULT_BINTVAL;
  275. /*
  276. * Calculate the TSF offset for this beacon slot, i.e., the
  277. * number of usecs that need to be added to the timestamp field
  278. * in Beacon and Probe Response frames. Beacon slot 0 is
  279. * processed at the correct offset, so it does not require TSF
  280. * adjustment. Other slots are adjusted to get the timestamp
  281. * close to the TBTT for the BSS.
  282. */
  283. tsfadjust = intval * avp->av_bslot / ATH_BCBUF;
  284. avp->tsf_adjust = cpu_to_le64(TU_TO_USEC(tsfadjust));
  285. ath_print(common, ATH_DBG_BEACON,
  286. "stagger beacons, bslot %d intval "
  287. "%u tsfadjust %llu\n",
  288. avp->av_bslot, intval, (unsigned long long)tsfadjust);
  289. ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp =
  290. avp->tsf_adjust;
  291. } else
  292. avp->tsf_adjust = cpu_to_le64(0);
  293. bf->bf_mpdu = skb;
  294. bf->bf_buf_addr = bf->bf_dmacontext =
  295. dma_map_single(sc->dev, skb->data,
  296. skb->len, DMA_TO_DEVICE);
  297. if (unlikely(dma_mapping_error(sc->dev, bf->bf_buf_addr))) {
  298. dev_kfree_skb_any(skb);
  299. bf->bf_mpdu = NULL;
  300. ath_print(common, ATH_DBG_FATAL,
  301. "dma_mapping_error on beacon alloc\n");
  302. return -ENOMEM;
  303. }
  304. return 0;
  305. }
  306. void ath_beacon_return(struct ath_softc *sc, struct ath_vif *avp)
  307. {
  308. if (avp->av_bcbuf != NULL) {
  309. struct ath_buf *bf;
  310. if (avp->av_bslot != -1) {
  311. sc->beacon.bslot[avp->av_bslot] = NULL;
  312. sc->beacon.bslot_aphy[avp->av_bslot] = NULL;
  313. sc->nbcnvifs--;
  314. }
  315. bf = avp->av_bcbuf;
  316. if (bf->bf_mpdu != NULL) {
  317. struct sk_buff *skb = bf->bf_mpdu;
  318. dma_unmap_single(sc->dev, bf->bf_dmacontext,
  319. skb->len, DMA_TO_DEVICE);
  320. dev_kfree_skb_any(skb);
  321. bf->bf_mpdu = NULL;
  322. }
  323. list_add_tail(&bf->list, &sc->beacon.bbuf);
  324. avp->av_bcbuf = NULL;
  325. }
  326. }
  327. void ath_beacon_tasklet(unsigned long data)
  328. {
  329. struct ath_softc *sc = (struct ath_softc *)data;
  330. struct ath_hw *ah = sc->sc_ah;
  331. struct ath_common *common = ath9k_hw_common(ah);
  332. struct ath_buf *bf = NULL;
  333. struct ieee80211_vif *vif;
  334. struct ath_wiphy *aphy;
  335. int slot;
  336. u32 bfaddr, bc = 0, tsftu;
  337. u64 tsf;
  338. u16 intval;
  339. /*
  340. * Check if the previous beacon has gone out. If
  341. * not don't try to post another, skip this period
  342. * and wait for the next. Missed beacons indicate
  343. * a problem and should not occur. If we miss too
  344. * many consecutive beacons reset the device.
  345. */
  346. if (ath9k_hw_numtxpending(ah, sc->beacon.beaconq) != 0) {
  347. sc->beacon.bmisscnt++;
  348. if (sc->beacon.bmisscnt < BSTUCK_THRESH) {
  349. ath_print(common, ATH_DBG_BEACON,
  350. "missed %u consecutive beacons\n",
  351. sc->beacon.bmisscnt);
  352. } else if (sc->beacon.bmisscnt >= BSTUCK_THRESH) {
  353. ath_print(common, ATH_DBG_BEACON,
  354. "beacon is officially stuck\n");
  355. sc->sc_flags |= SC_OP_TSF_RESET;
  356. ath_reset(sc, false);
  357. }
  358. return;
  359. }
  360. if (sc->beacon.bmisscnt != 0) {
  361. ath_print(common, ATH_DBG_BEACON,
  362. "resume beacon xmit after %u misses\n",
  363. sc->beacon.bmisscnt);
  364. sc->beacon.bmisscnt = 0;
  365. }
  366. /*
  367. * Generate beacon frames. we are sending frames
  368. * staggered so calculate the slot for this frame based
  369. * on the tsf to safeguard against missing an swba.
  370. */
  371. intval = sc->beacon_interval ? : ATH_DEFAULT_BINTVAL;
  372. tsf = ath9k_hw_gettsf64(ah);
  373. tsftu = TSF_TO_TU(tsf>>32, tsf);
  374. slot = ((tsftu % intval) * ATH_BCBUF) / intval;
  375. /*
  376. * Reverse the slot order to get slot 0 on the TBTT offset that does
  377. * not require TSF adjustment and other slots adding
  378. * slot/ATH_BCBUF * beacon_int to timestamp. For example, with
  379. * ATH_BCBUF = 4, we process beacon slots as follows: 3 2 1 0 3 2 1 ..
  380. * and slot 0 is at correct offset to TBTT.
  381. */
  382. slot = ATH_BCBUF - slot - 1;
  383. vif = sc->beacon.bslot[slot];
  384. aphy = sc->beacon.bslot_aphy[slot];
  385. ath_print(common, ATH_DBG_BEACON,
  386. "slot %d [tsf %llu tsftu %u intval %u] vif %p\n",
  387. slot, tsf, tsftu, intval, vif);
  388. bfaddr = 0;
  389. if (vif) {
  390. bf = ath_beacon_generate(aphy->hw, vif);
  391. if (bf != NULL) {
  392. bfaddr = bf->bf_daddr;
  393. bc = 1;
  394. }
  395. }
  396. /*
  397. * Handle slot time change when a non-ERP station joins/leaves
  398. * an 11g network. The 802.11 layer notifies us via callback,
  399. * we mark updateslot, then wait one beacon before effecting
  400. * the change. This gives associated stations at least one
  401. * beacon interval to note the state change.
  402. *
  403. * NB: The slot time change state machine is clocked according
  404. * to whether we are bursting or staggering beacons. We
  405. * recognize the request to update and record the current
  406. * slot then don't transition until that slot is reached
  407. * again. If we miss a beacon for that slot then we'll be
  408. * slow to transition but we'll be sure at least one beacon
  409. * interval has passed. When bursting slot is always left
  410. * set to ATH_BCBUF so this check is a noop.
  411. */
  412. if (sc->beacon.updateslot == UPDATE) {
  413. sc->beacon.updateslot = COMMIT; /* commit next beacon */
  414. sc->beacon.slotupdate = slot;
  415. } else if (sc->beacon.updateslot == COMMIT && sc->beacon.slotupdate == slot) {
  416. ath9k_hw_setslottime(sc->sc_ah, sc->beacon.slottime);
  417. sc->beacon.updateslot = OK;
  418. }
  419. if (bfaddr != 0) {
  420. /*
  421. * Stop any current dma and put the new frame(s) on the queue.
  422. * This should never fail since we check above that no frames
  423. * are still pending on the queue.
  424. */
  425. if (!ath9k_hw_stoptxdma(ah, sc->beacon.beaconq)) {
  426. ath_print(common, ATH_DBG_FATAL,
  427. "beacon queue %u did not stop?\n", sc->beacon.beaconq);
  428. }
  429. /* NB: cabq traffic should already be queued and primed */
  430. ath9k_hw_puttxbuf(ah, sc->beacon.beaconq, bfaddr);
  431. ath9k_hw_txstart(ah, sc->beacon.beaconq);
  432. sc->beacon.ast_be_xmit += bc; /* XXX per-vif? */
  433. }
  434. }
  435. static void ath9k_beacon_init(struct ath_softc *sc,
  436. u32 next_beacon,
  437. u32 beacon_period)
  438. {
  439. if (beacon_period & ATH9K_BEACON_RESET_TSF)
  440. ath9k_ps_wakeup(sc);
  441. ath9k_hw_beaconinit(sc->sc_ah, next_beacon, beacon_period);
  442. if (beacon_period & ATH9K_BEACON_RESET_TSF)
  443. ath9k_ps_restore(sc);
  444. }
  445. /*
  446. * For multi-bss ap support beacons are either staggered evenly over N slots or
  447. * burst together. For the former arrange for the SWBA to be delivered for each
  448. * slot. Slots that are not occupied will generate nothing.
  449. */
  450. static void ath_beacon_config_ap(struct ath_softc *sc,
  451. struct ath_beacon_config *conf)
  452. {
  453. u32 nexttbtt, intval;
  454. /* Configure the timers only when the TSF has to be reset */
  455. if (!(sc->sc_flags & SC_OP_TSF_RESET))
  456. return;
  457. /* NB: the beacon interval is kept internally in TU's */
  458. intval = conf->beacon_interval & ATH9K_BEACON_PERIOD;
  459. intval /= ATH_BCBUF; /* for staggered beacons */
  460. nexttbtt = intval;
  461. intval |= ATH9K_BEACON_RESET_TSF;
  462. /*
  463. * In AP mode we enable the beacon timers and SWBA interrupts to
  464. * prepare beacon frames.
  465. */
  466. intval |= ATH9K_BEACON_ENA;
  467. sc->imask |= ATH9K_INT_SWBA;
  468. ath_beaconq_config(sc);
  469. /* Set the computed AP beacon timers */
  470. ath9k_hw_set_interrupts(sc->sc_ah, 0);
  471. ath9k_beacon_init(sc, nexttbtt, intval);
  472. sc->beacon.bmisscnt = 0;
  473. ath9k_hw_set_interrupts(sc->sc_ah, sc->imask);
  474. /* Clear the reset TSF flag, so that subsequent beacon updation
  475. will not reset the HW TSF. */
  476. sc->sc_flags &= ~SC_OP_TSF_RESET;
  477. }
  478. /*
  479. * This sets up the beacon timers according to the timestamp of the last
  480. * received beacon and the current TSF, configures PCF and DTIM
  481. * handling, programs the sleep registers so the hardware will wakeup in
  482. * time to receive beacons, and configures the beacon miss handling so
  483. * we'll receive a BMISS interrupt when we stop seeing beacons from the AP
  484. * we've associated with.
  485. */
  486. static void ath_beacon_config_sta(struct ath_softc *sc,
  487. struct ath_beacon_config *conf)
  488. {
  489. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  490. struct ath9k_beacon_state bs;
  491. int dtimperiod, dtimcount, sleepduration;
  492. int cfpperiod, cfpcount;
  493. u32 nexttbtt = 0, intval, tsftu;
  494. u64 tsf;
  495. int num_beacons, offset, dtim_dec_count, cfp_dec_count;
  496. memset(&bs, 0, sizeof(bs));
  497. intval = conf->beacon_interval & ATH9K_BEACON_PERIOD;
  498. /*
  499. * Setup dtim and cfp parameters according to
  500. * last beacon we received (which may be none).
  501. */
  502. dtimperiod = conf->dtim_period;
  503. if (dtimperiod <= 0) /* NB: 0 if not known */
  504. dtimperiod = 1;
  505. dtimcount = conf->dtim_count;
  506. if (dtimcount >= dtimperiod) /* NB: sanity check */
  507. dtimcount = 0;
  508. cfpperiod = 1; /* NB: no PCF support yet */
  509. cfpcount = 0;
  510. sleepduration = conf->listen_interval * intval;
  511. if (sleepduration <= 0)
  512. sleepduration = intval;
  513. /*
  514. * Pull nexttbtt forward to reflect the current
  515. * TSF and calculate dtim+cfp state for the result.
  516. */
  517. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  518. tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
  519. num_beacons = tsftu / intval + 1;
  520. offset = tsftu % intval;
  521. nexttbtt = tsftu - offset;
  522. if (offset)
  523. nexttbtt += intval;
  524. /* DTIM Beacon every dtimperiod Beacon */
  525. dtim_dec_count = num_beacons % dtimperiod;
  526. /* CFP every cfpperiod DTIM Beacon */
  527. cfp_dec_count = (num_beacons / dtimperiod) % cfpperiod;
  528. if (dtim_dec_count)
  529. cfp_dec_count++;
  530. dtimcount -= dtim_dec_count;
  531. if (dtimcount < 0)
  532. dtimcount += dtimperiod;
  533. cfpcount -= cfp_dec_count;
  534. if (cfpcount < 0)
  535. cfpcount += cfpperiod;
  536. bs.bs_intval = intval;
  537. bs.bs_nexttbtt = nexttbtt;
  538. bs.bs_dtimperiod = dtimperiod*intval;
  539. bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
  540. bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
  541. bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
  542. bs.bs_cfpmaxduration = 0;
  543. /*
  544. * Calculate the number of consecutive beacons to miss* before taking
  545. * a BMISS interrupt. The configuration is specified in TU so we only
  546. * need calculate based on the beacon interval. Note that we clamp the
  547. * result to at most 15 beacons.
  548. */
  549. if (sleepduration > intval) {
  550. bs.bs_bmissthreshold = conf->listen_interval *
  551. ATH_DEFAULT_BMISS_LIMIT / 2;
  552. } else {
  553. bs.bs_bmissthreshold = DIV_ROUND_UP(conf->bmiss_timeout, intval);
  554. if (bs.bs_bmissthreshold > 15)
  555. bs.bs_bmissthreshold = 15;
  556. else if (bs.bs_bmissthreshold <= 0)
  557. bs.bs_bmissthreshold = 1;
  558. }
  559. /*
  560. * Calculate sleep duration. The configuration is given in ms.
  561. * We ensure a multiple of the beacon period is used. Also, if the sleep
  562. * duration is greater than the DTIM period then it makes senses
  563. * to make it a multiple of that.
  564. *
  565. * XXX fixed at 100ms
  566. */
  567. bs.bs_sleepduration = roundup(IEEE80211_MS_TO_TU(100), sleepduration);
  568. if (bs.bs_sleepduration > bs.bs_dtimperiod)
  569. bs.bs_sleepduration = bs.bs_dtimperiod;
  570. /* TSF out of range threshold fixed at 1 second */
  571. bs.bs_tsfoor_threshold = ATH9K_TSFOOR_THRESHOLD;
  572. ath_print(common, ATH_DBG_BEACON, "tsf: %llu tsftu: %u\n", tsf, tsftu);
  573. ath_print(common, ATH_DBG_BEACON,
  574. "bmiss: %u sleep: %u cfp-period: %u maxdur: %u next: %u\n",
  575. bs.bs_bmissthreshold, bs.bs_sleepduration,
  576. bs.bs_cfpperiod, bs.bs_cfpmaxduration, bs.bs_cfpnext);
  577. /* Set the computed STA beacon timers */
  578. ath9k_hw_set_interrupts(sc->sc_ah, 0);
  579. ath9k_hw_set_sta_beacon_timers(sc->sc_ah, &bs);
  580. sc->imask |= ATH9K_INT_BMISS;
  581. ath9k_hw_set_interrupts(sc->sc_ah, sc->imask);
  582. }
  583. static void ath_beacon_config_adhoc(struct ath_softc *sc,
  584. struct ath_beacon_config *conf,
  585. struct ieee80211_vif *vif)
  586. {
  587. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  588. u64 tsf;
  589. u32 tsftu, intval, nexttbtt;
  590. intval = conf->beacon_interval & ATH9K_BEACON_PERIOD;
  591. /* Pull nexttbtt forward to reflect the current TSF */
  592. nexttbtt = TSF_TO_TU(sc->beacon.bc_tstamp >> 32, sc->beacon.bc_tstamp);
  593. if (nexttbtt == 0)
  594. nexttbtt = intval;
  595. else if (intval)
  596. nexttbtt = roundup(nexttbtt, intval);
  597. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  598. tsftu = TSF_TO_TU((u32)(tsf>>32), (u32)tsf) + FUDGE;
  599. do {
  600. nexttbtt += intval;
  601. } while (nexttbtt < tsftu);
  602. ath_print(common, ATH_DBG_BEACON,
  603. "IBSS nexttbtt %u intval %u (%u)\n",
  604. nexttbtt, intval, conf->beacon_interval);
  605. /*
  606. * In IBSS mode enable the beacon timers but only enable SWBA interrupts
  607. * if we need to manually prepare beacon frames. Otherwise we use a
  608. * self-linked tx descriptor and let the hardware deal with things.
  609. */
  610. intval |= ATH9K_BEACON_ENA;
  611. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_VEOL))
  612. sc->imask |= ATH9K_INT_SWBA;
  613. ath_beaconq_config(sc);
  614. /* Set the computed ADHOC beacon timers */
  615. ath9k_hw_set_interrupts(sc->sc_ah, 0);
  616. ath9k_beacon_init(sc, nexttbtt, intval);
  617. sc->beacon.bmisscnt = 0;
  618. ath9k_hw_set_interrupts(sc->sc_ah, sc->imask);
  619. /* FIXME: Handle properly when vif is NULL */
  620. if (vif && sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_VEOL)
  621. ath_beacon_start_adhoc(sc, vif);
  622. }
  623. void ath_beacon_config(struct ath_softc *sc, struct ieee80211_vif *vif)
  624. {
  625. struct ath_beacon_config *cur_conf = &sc->cur_beacon_conf;
  626. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  627. enum nl80211_iftype iftype;
  628. /* Setup the beacon configuration parameters */
  629. if (vif) {
  630. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  631. iftype = vif->type;
  632. cur_conf->beacon_interval = bss_conf->beacon_int;
  633. cur_conf->dtim_period = bss_conf->dtim_period;
  634. cur_conf->listen_interval = 1;
  635. cur_conf->dtim_count = 1;
  636. cur_conf->bmiss_timeout =
  637. ATH_DEFAULT_BMISS_LIMIT * cur_conf->beacon_interval;
  638. } else {
  639. iftype = sc->sc_ah->opmode;
  640. }
  641. /*
  642. * It looks like mac80211 may end up using beacon interval of zero in
  643. * some cases (at least for mesh point). Avoid getting into an
  644. * infinite loop by using a bit safer value instead. To be safe,
  645. * do sanity check on beacon interval for all operating modes.
  646. */
  647. if (cur_conf->beacon_interval == 0)
  648. cur_conf->beacon_interval = 100;
  649. switch (iftype) {
  650. case NL80211_IFTYPE_AP:
  651. ath_beacon_config_ap(sc, cur_conf);
  652. break;
  653. case NL80211_IFTYPE_ADHOC:
  654. case NL80211_IFTYPE_MESH_POINT:
  655. ath_beacon_config_adhoc(sc, cur_conf, vif);
  656. break;
  657. case NL80211_IFTYPE_STATION:
  658. ath_beacon_config_sta(sc, cur_conf);
  659. break;
  660. default:
  661. ath_print(common, ATH_DBG_CONFIG,
  662. "Unsupported beaconing mode\n");
  663. return;
  664. }
  665. sc->sc_flags |= SC_OP_BEACONS;
  666. }