beacon.c 24 KB

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