beacon.c 25 KB

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