htc_drv_beacon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * Copyright (c) 2010-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "htc.h"
  17. #define FUDGE 2
  18. void ath9k_htc_beaconq_config(struct ath9k_htc_priv *priv)
  19. {
  20. struct ath_hw *ah = priv->ah;
  21. struct ath9k_tx_queue_info qi, qi_be;
  22. memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
  23. memset(&qi_be, 0, sizeof(struct ath9k_tx_queue_info));
  24. ath9k_hw_get_txq_props(ah, priv->beacon.beaconq, &qi);
  25. if (priv->ah->opmode == NL80211_IFTYPE_AP ||
  26. priv->ah->opmode == NL80211_IFTYPE_MESH_POINT) {
  27. qi.tqi_aifs = 1;
  28. qi.tqi_cwmin = 0;
  29. qi.tqi_cwmax = 0;
  30. } else if (priv->ah->opmode == NL80211_IFTYPE_ADHOC) {
  31. int qnum = priv->hwq_map[IEEE80211_AC_BE];
  32. ath9k_hw_get_txq_props(ah, qnum, &qi_be);
  33. qi.tqi_aifs = qi_be.tqi_aifs;
  34. /*
  35. * For WIFI Beacon Distribution
  36. * Long slot time : 2x cwmin
  37. * Short slot time : 4x cwmin
  38. */
  39. if (ah->slottime == ATH9K_SLOT_TIME_20)
  40. qi.tqi_cwmin = 2*qi_be.tqi_cwmin;
  41. else
  42. qi.tqi_cwmin = 4*qi_be.tqi_cwmin;
  43. qi.tqi_cwmax = qi_be.tqi_cwmax;
  44. }
  45. if (!ath9k_hw_set_txq_props(ah, priv->beacon.beaconq, &qi)) {
  46. ath_err(ath9k_hw_common(ah),
  47. "Unable to update beacon queue %u!\n", priv->beacon.beaconq);
  48. } else {
  49. ath9k_hw_resettxqueue(ah, priv->beacon.beaconq);
  50. }
  51. }
  52. /*
  53. * Both nexttbtt and intval have to be in usecs.
  54. */
  55. static void ath9k_htc_beacon_init(struct ath9k_htc_priv *priv,
  56. struct ath_beacon_config *conf,
  57. bool reset_tsf)
  58. {
  59. struct ath_hw *ah = priv->ah;
  60. int ret __attribute__ ((unused));
  61. __be32 htc_imask = 0;
  62. u8 cmd_rsp;
  63. if (conf->intval >= TU_TO_USEC(DEFAULT_SWBA_RESPONSE))
  64. ah->config.sw_beacon_response_time = DEFAULT_SWBA_RESPONSE;
  65. else
  66. ah->config.sw_beacon_response_time = MIN_SWBA_RESPONSE;
  67. WMI_CMD(WMI_DISABLE_INTR_CMDID);
  68. if (reset_tsf)
  69. ath9k_hw_reset_tsf(ah);
  70. ath9k_htc_beaconq_config(priv);
  71. ath9k_hw_beaconinit(ah, conf->nexttbtt, conf->intval);
  72. priv->beacon.bmisscnt = 0;
  73. htc_imask = cpu_to_be32(ah->imask);
  74. WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
  75. }
  76. static void ath9k_htc_beacon_config_sta(struct ath9k_htc_priv *priv,
  77. struct ath_beacon_config *bss_conf)
  78. {
  79. struct ath9k_beacon_state bs;
  80. enum ath9k_int imask = 0;
  81. __be32 htc_imask = 0;
  82. int ret __attribute__ ((unused));
  83. u8 cmd_rsp;
  84. if (ath9k_cmn_beacon_config_sta(priv->ah, bss_conf, &bs) == -EPERM)
  85. return;
  86. WMI_CMD(WMI_DISABLE_INTR_CMDID);
  87. ath9k_hw_set_sta_beacon_timers(priv->ah, &bs);
  88. imask |= ATH9K_INT_BMISS;
  89. htc_imask = cpu_to_be32(imask);
  90. WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
  91. }
  92. static void ath9k_htc_beacon_config_ap(struct ath9k_htc_priv *priv,
  93. struct ath_beacon_config *conf)
  94. {
  95. struct ath_hw *ah = priv->ah;
  96. ah->imask = 0;
  97. ath9k_cmn_beacon_config_ap(ah, conf, ATH9K_HTC_MAX_BCN_VIF);
  98. ath9k_htc_beacon_init(priv, conf, false);
  99. }
  100. static void ath9k_htc_beacon_config_adhoc(struct ath9k_htc_priv *priv,
  101. struct ath_beacon_config *conf)
  102. {
  103. struct ath_hw *ah = priv->ah;
  104. ah->imask = 0;
  105. ath9k_cmn_beacon_config_adhoc(ah, conf);
  106. ath9k_htc_beacon_init(priv, conf, conf->ibss_creator);
  107. }
  108. void ath9k_htc_beaconep(void *drv_priv, struct sk_buff *skb,
  109. enum htc_endpoint_id ep_id, bool txok)
  110. {
  111. dev_kfree_skb_any(skb);
  112. }
  113. static void ath9k_htc_send_buffered(struct ath9k_htc_priv *priv,
  114. int slot)
  115. {
  116. struct ath_common *common = ath9k_hw_common(priv->ah);
  117. struct ieee80211_vif *vif;
  118. struct sk_buff *skb;
  119. struct ieee80211_hdr *hdr;
  120. int padpos, padsize, ret, tx_slot;
  121. spin_lock_bh(&priv->beacon_lock);
  122. vif = priv->beacon.bslot[slot];
  123. skb = ieee80211_get_buffered_bc(priv->hw, vif);
  124. while(skb) {
  125. hdr = (struct ieee80211_hdr *) skb->data;
  126. padpos = ieee80211_hdrlen(hdr->frame_control);
  127. padsize = padpos & 3;
  128. if (padsize && skb->len > padpos) {
  129. if (skb_headroom(skb) < padsize) {
  130. dev_kfree_skb_any(skb);
  131. goto next;
  132. }
  133. skb_push(skb, padsize);
  134. memmove(skb->data, skb->data + padsize, padpos);
  135. }
  136. tx_slot = ath9k_htc_tx_get_slot(priv);
  137. if (tx_slot < 0) {
  138. ath_dbg(common, XMIT, "No free CAB slot\n");
  139. dev_kfree_skb_any(skb);
  140. goto next;
  141. }
  142. ret = ath9k_htc_tx_start(priv, NULL, skb, tx_slot, true);
  143. if (ret != 0) {
  144. ath9k_htc_tx_clear_slot(priv, tx_slot);
  145. dev_kfree_skb_any(skb);
  146. ath_dbg(common, XMIT, "Failed to send CAB frame\n");
  147. } else {
  148. spin_lock_bh(&priv->tx.tx_lock);
  149. priv->tx.queued_cnt++;
  150. spin_unlock_bh(&priv->tx.tx_lock);
  151. }
  152. next:
  153. skb = ieee80211_get_buffered_bc(priv->hw, vif);
  154. }
  155. spin_unlock_bh(&priv->beacon_lock);
  156. }
  157. static void ath9k_htc_send_beacon(struct ath9k_htc_priv *priv,
  158. int slot)
  159. {
  160. struct ath_common *common = ath9k_hw_common(priv->ah);
  161. struct ieee80211_vif *vif;
  162. struct ath9k_htc_vif *avp;
  163. struct tx_beacon_header beacon_hdr;
  164. struct ath9k_htc_tx_ctl *tx_ctl;
  165. struct ieee80211_tx_info *info;
  166. struct ieee80211_mgmt *mgmt;
  167. struct sk_buff *beacon;
  168. u8 *tx_fhdr;
  169. int ret;
  170. memset(&beacon_hdr, 0, sizeof(struct tx_beacon_header));
  171. spin_lock_bh(&priv->beacon_lock);
  172. vif = priv->beacon.bslot[slot];
  173. avp = (struct ath9k_htc_vif *)vif->drv_priv;
  174. if (unlikely(test_bit(ATH_OP_SCANNING, &common->op_flags))) {
  175. spin_unlock_bh(&priv->beacon_lock);
  176. return;
  177. }
  178. /* Get a new beacon */
  179. beacon = ieee80211_beacon_get(priv->hw, vif);
  180. if (!beacon) {
  181. spin_unlock_bh(&priv->beacon_lock);
  182. return;
  183. }
  184. /*
  185. * Update the TSF adjust value here, the HW will
  186. * add this value for every beacon.
  187. */
  188. mgmt = (struct ieee80211_mgmt *)beacon->data;
  189. mgmt->u.beacon.timestamp = avp->tsfadjust;
  190. info = IEEE80211_SKB_CB(beacon);
  191. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  192. struct ieee80211_hdr *hdr =
  193. (struct ieee80211_hdr *) beacon->data;
  194. avp->seq_no += 0x10;
  195. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  196. hdr->seq_ctrl |= cpu_to_le16(avp->seq_no);
  197. }
  198. tx_ctl = HTC_SKB_CB(beacon);
  199. memset(tx_ctl, 0, sizeof(*tx_ctl));
  200. tx_ctl->type = ATH9K_HTC_BEACON;
  201. tx_ctl->epid = priv->beacon_ep;
  202. beacon_hdr.vif_index = avp->index;
  203. tx_fhdr = skb_push(beacon, sizeof(beacon_hdr));
  204. memcpy(tx_fhdr, (u8 *) &beacon_hdr, sizeof(beacon_hdr));
  205. ret = htc_send(priv->htc, beacon);
  206. if (ret != 0) {
  207. if (ret == -ENOMEM) {
  208. ath_dbg(common, BSTUCK,
  209. "Failed to send beacon, no free TX buffer\n");
  210. }
  211. dev_kfree_skb_any(beacon);
  212. }
  213. spin_unlock_bh(&priv->beacon_lock);
  214. ath9k_htc_csa_is_finished(priv);
  215. }
  216. static int ath9k_htc_choose_bslot(struct ath9k_htc_priv *priv,
  217. struct wmi_event_swba *swba)
  218. {
  219. struct ath_common *common = ath9k_hw_common(priv->ah);
  220. u64 tsf;
  221. u32 tsftu;
  222. u16 intval;
  223. int slot;
  224. intval = priv->cur_beacon_conf.beacon_interval;
  225. tsf = be64_to_cpu(swba->tsf);
  226. tsftu = TSF_TO_TU(tsf >> 32, tsf);
  227. slot = ((tsftu % intval) * ATH9K_HTC_MAX_BCN_VIF) / intval;
  228. slot = ATH9K_HTC_MAX_BCN_VIF - slot - 1;
  229. ath_dbg(common, BEACON,
  230. "Choose slot: %d, tsf: %llu, tsftu: %u, intval: %u\n",
  231. slot, tsf, tsftu, intval);
  232. return slot;
  233. }
  234. void ath9k_htc_swba(struct ath9k_htc_priv *priv,
  235. struct wmi_event_swba *swba)
  236. {
  237. struct ath_common *common = ath9k_hw_common(priv->ah);
  238. int slot;
  239. if (swba->beacon_pending != 0) {
  240. priv->beacon.bmisscnt++;
  241. if (priv->beacon.bmisscnt > BSTUCK_THRESHOLD) {
  242. ath_dbg(common, BSTUCK, "Beacon stuck, HW reset\n");
  243. ieee80211_queue_work(priv->hw,
  244. &priv->fatal_work);
  245. }
  246. return;
  247. }
  248. if (priv->beacon.bmisscnt) {
  249. ath_dbg(common, BSTUCK,
  250. "Resuming beacon xmit after %u misses\n",
  251. priv->beacon.bmisscnt);
  252. priv->beacon.bmisscnt = 0;
  253. }
  254. slot = ath9k_htc_choose_bslot(priv, swba);
  255. spin_lock_bh(&priv->beacon_lock);
  256. if (priv->beacon.bslot[slot] == NULL) {
  257. spin_unlock_bh(&priv->beacon_lock);
  258. return;
  259. }
  260. spin_unlock_bh(&priv->beacon_lock);
  261. ath9k_htc_send_buffered(priv, slot);
  262. ath9k_htc_send_beacon(priv, slot);
  263. }
  264. void ath9k_htc_assign_bslot(struct ath9k_htc_priv *priv,
  265. struct ieee80211_vif *vif)
  266. {
  267. struct ath_common *common = ath9k_hw_common(priv->ah);
  268. struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *)vif->drv_priv;
  269. int i = 0;
  270. spin_lock_bh(&priv->beacon_lock);
  271. for (i = 0; i < ATH9K_HTC_MAX_BCN_VIF; i++) {
  272. if (priv->beacon.bslot[i] == NULL) {
  273. avp->bslot = i;
  274. break;
  275. }
  276. }
  277. priv->beacon.bslot[avp->bslot] = vif;
  278. spin_unlock_bh(&priv->beacon_lock);
  279. ath_dbg(common, CONFIG, "Added interface at beacon slot: %d\n",
  280. avp->bslot);
  281. }
  282. void ath9k_htc_remove_bslot(struct ath9k_htc_priv *priv,
  283. struct ieee80211_vif *vif)
  284. {
  285. struct ath_common *common = ath9k_hw_common(priv->ah);
  286. struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *)vif->drv_priv;
  287. spin_lock_bh(&priv->beacon_lock);
  288. priv->beacon.bslot[avp->bslot] = NULL;
  289. spin_unlock_bh(&priv->beacon_lock);
  290. ath_dbg(common, CONFIG, "Removed interface at beacon slot: %d\n",
  291. avp->bslot);
  292. }
  293. /*
  294. * Calculate the TSF adjustment value for all slots
  295. * other than zero.
  296. */
  297. void ath9k_htc_set_tsfadjust(struct ath9k_htc_priv *priv,
  298. struct ieee80211_vif *vif)
  299. {
  300. struct ath_common *common = ath9k_hw_common(priv->ah);
  301. struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *)vif->drv_priv;
  302. struct ath_beacon_config *cur_conf = &priv->cur_beacon_conf;
  303. u64 tsfadjust;
  304. if (avp->bslot == 0)
  305. return;
  306. /*
  307. * The beacon interval cannot be different for multi-AP mode,
  308. * and we reach here only for VIF slots greater than zero,
  309. * so beacon_interval is guaranteed to be set in cur_conf.
  310. */
  311. tsfadjust = cur_conf->beacon_interval * avp->bslot / ATH9K_HTC_MAX_BCN_VIF;
  312. avp->tsfadjust = cpu_to_le64(TU_TO_USEC(tsfadjust));
  313. ath_dbg(common, CONFIG, "tsfadjust is: %llu for bslot: %d\n",
  314. (unsigned long long)tsfadjust, avp->bslot);
  315. }
  316. static void ath9k_htc_beacon_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  317. {
  318. bool *beacon_configured = (bool *)data;
  319. struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
  320. if (vif->type == NL80211_IFTYPE_STATION &&
  321. avp->beacon_configured)
  322. *beacon_configured = true;
  323. }
  324. static bool ath9k_htc_check_beacon_config(struct ath9k_htc_priv *priv,
  325. struct ieee80211_vif *vif)
  326. {
  327. struct ath_common *common = ath9k_hw_common(priv->ah);
  328. struct ath_beacon_config *cur_conf = &priv->cur_beacon_conf;
  329. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  330. bool beacon_configured;
  331. /*
  332. * Changing the beacon interval when multiple AP interfaces
  333. * are configured will affect beacon transmission of all
  334. * of them.
  335. */
  336. if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
  337. (priv->num_ap_vif > 1) &&
  338. (vif->type == NL80211_IFTYPE_AP) &&
  339. (cur_conf->beacon_interval != bss_conf->beacon_int)) {
  340. ath_dbg(common, CONFIG,
  341. "Changing beacon interval of multiple AP interfaces !\n");
  342. return false;
  343. }
  344. /*
  345. * If the HW is operating in AP mode, any new station interfaces that
  346. * are added cannot change the beacon parameters.
  347. */
  348. if (priv->num_ap_vif &&
  349. (vif->type != NL80211_IFTYPE_AP)) {
  350. ath_dbg(common, CONFIG,
  351. "HW in AP mode, cannot set STA beacon parameters\n");
  352. return false;
  353. }
  354. /*
  355. * The beacon parameters are configured only for the first
  356. * station interface.
  357. */
  358. if ((priv->ah->opmode == NL80211_IFTYPE_STATION) &&
  359. (priv->num_sta_vif > 1) &&
  360. (vif->type == NL80211_IFTYPE_STATION)) {
  361. beacon_configured = false;
  362. ieee80211_iterate_active_interfaces_atomic(
  363. priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  364. ath9k_htc_beacon_iter, &beacon_configured);
  365. if (beacon_configured) {
  366. ath_dbg(common, CONFIG,
  367. "Beacon already configured for a station interface\n");
  368. return false;
  369. }
  370. }
  371. return true;
  372. }
  373. void ath9k_htc_beacon_config(struct ath9k_htc_priv *priv,
  374. struct ieee80211_vif *vif)
  375. {
  376. struct ath_common *common = ath9k_hw_common(priv->ah);
  377. struct ath_beacon_config *cur_conf = &priv->cur_beacon_conf;
  378. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  379. struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
  380. if (!ath9k_htc_check_beacon_config(priv, vif))
  381. return;
  382. cur_conf->beacon_interval = bss_conf->beacon_int;
  383. if (cur_conf->beacon_interval == 0)
  384. cur_conf->beacon_interval = 100;
  385. cur_conf->dtim_period = bss_conf->dtim_period;
  386. cur_conf->bmiss_timeout =
  387. ATH_DEFAULT_BMISS_LIMIT * cur_conf->beacon_interval;
  388. switch (vif->type) {
  389. case NL80211_IFTYPE_STATION:
  390. ath9k_htc_beacon_config_sta(priv, cur_conf);
  391. avp->beacon_configured = true;
  392. break;
  393. case NL80211_IFTYPE_ADHOC:
  394. ath9k_htc_beacon_config_adhoc(priv, cur_conf);
  395. break;
  396. case NL80211_IFTYPE_MESH_POINT:
  397. case NL80211_IFTYPE_AP:
  398. ath9k_htc_beacon_config_ap(priv, cur_conf);
  399. break;
  400. default:
  401. ath_dbg(common, CONFIG, "Unsupported beaconing mode\n");
  402. return;
  403. }
  404. }
  405. void ath9k_htc_beacon_reconfig(struct ath9k_htc_priv *priv)
  406. {
  407. struct ath_common *common = ath9k_hw_common(priv->ah);
  408. struct ath_beacon_config *cur_conf = &priv->cur_beacon_conf;
  409. switch (priv->ah->opmode) {
  410. case NL80211_IFTYPE_STATION:
  411. ath9k_htc_beacon_config_sta(priv, cur_conf);
  412. break;
  413. case NL80211_IFTYPE_ADHOC:
  414. ath9k_htc_beacon_config_adhoc(priv, cur_conf);
  415. break;
  416. case NL80211_IFTYPE_MESH_POINT:
  417. case NL80211_IFTYPE_AP:
  418. ath9k_htc_beacon_config_ap(priv, cur_conf);
  419. break;
  420. default:
  421. ath_dbg(common, CONFIG, "Unsupported beaconing mode\n");
  422. return;
  423. }
  424. }
  425. bool ath9k_htc_csa_is_finished(struct ath9k_htc_priv *priv)
  426. {
  427. struct ieee80211_vif *vif;
  428. vif = priv->csa_vif;
  429. if (!vif || !vif->csa_active)
  430. return false;
  431. if (!ieee80211_csa_is_complete(vif))
  432. return false;
  433. ieee80211_csa_finish(vif);
  434. priv->csa_vif = NULL;
  435. return true;
  436. }