htc_drv_beacon.c 17.9 KB
Newer Older
S
Sujith 已提交
1
/*
2
 * Copyright (c) 2010-2011 Atheros Communications Inc.
S
Sujith 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "htc.h"

#define FUDGE 2

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
void ath9k_htc_beaconq_config(struct ath9k_htc_priv *priv)
{
	struct ath_hw *ah = priv->ah;
	struct ath9k_tx_queue_info qi, qi_be;

	memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
	memset(&qi_be, 0, sizeof(struct ath9k_tx_queue_info));

	ath9k_hw_get_txq_props(ah, priv->beaconq, &qi);

	if (priv->ah->opmode == NL80211_IFTYPE_AP) {
		qi.tqi_aifs = 1;
		qi.tqi_cwmin = 0;
		qi.tqi_cwmax = 0;
	} else if (priv->ah->opmode == NL80211_IFTYPE_ADHOC) {
		int qnum = priv->hwq_map[WME_AC_BE];

		ath9k_hw_get_txq_props(ah, qnum, &qi_be);

		qi.tqi_aifs = qi_be.tqi_aifs;

		/*
		 * For WIFI Beacon Distribution
		 * Long slot time  : 2x cwmin
		 * Short slot time : 4x cwmin
		 */
		if (ah->slottime == ATH9K_SLOT_TIME_20)
			qi.tqi_cwmin = 2*qi_be.tqi_cwmin;
		else
			qi.tqi_cwmin = 4*qi_be.tqi_cwmin;

		qi.tqi_cwmax = qi_be.tqi_cwmax;

	}

	if (!ath9k_hw_set_txq_props(ah, priv->beaconq, &qi)) {
		ath_err(ath9k_hw_common(ah),
			"Unable to update beacon queue %u!\n", priv->beaconq);
	} else {
		ath9k_hw_resettxqueue(ah, priv->beaconq);
	}
}


S
Sujith 已提交
65
static void ath9k_htc_beacon_config_sta(struct ath9k_htc_priv *priv,
66
					struct htc_beacon_config *bss_conf)
S
Sujith 已提交
67 68 69 70 71 72
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_beacon_state bs;
	enum ath9k_int imask = 0;
	int dtimperiod, dtimcount, sleepduration;
	int cfpperiod, cfpcount, bmiss_timeout;
S
Sujith 已提交
73 74
	u32 nexttbtt = 0, intval, tsftu;
	__be32 htc_imask = 0;
S
Sujith 已提交
75 76
	u64 tsf;
	int num_beacons, offset, dtim_dec_count, cfp_dec_count;
77
	int ret __attribute__ ((unused));
S
Sujith 已提交
78 79 80 81
	u8 cmd_rsp;

	memset(&bs, 0, sizeof(bs));

82
	intval = bss_conf->beacon_interval;
83
	bmiss_timeout = (ATH_DEFAULT_BMISS_LIMIT * bss_conf->beacon_interval);
S
Sujith 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

	/*
	 * Setup dtim and cfp parameters according to
	 * last beacon we received (which may be none).
	 */
	dtimperiod = bss_conf->dtim_period;
	if (dtimperiod <= 0)		/* NB: 0 if not known */
		dtimperiod = 1;
	dtimcount = 1;
	if (dtimcount >= dtimperiod)	/* NB: sanity check */
		dtimcount = 0;
	cfpperiod = 1;			/* NB: no PCF support yet */
	cfpcount = 0;

	sleepduration = intval;
	if (sleepduration <= 0)
		sleepduration = intval;

	/*
	 * Pull nexttbtt forward to reflect the current
	 * TSF and calculate dtim+cfp state for the result.
	 */
	tsf = ath9k_hw_gettsf64(priv->ah);
	tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;

	num_beacons = tsftu / intval + 1;
	offset = tsftu % intval;
	nexttbtt = tsftu - offset;
	if (offset)
		nexttbtt += intval;

	/* DTIM Beacon every dtimperiod Beacon */
	dtim_dec_count = num_beacons % dtimperiod;
	/* CFP every cfpperiod DTIM Beacon */
	cfp_dec_count = (num_beacons / dtimperiod) % cfpperiod;
	if (dtim_dec_count)
		cfp_dec_count++;

	dtimcount -= dtim_dec_count;
	if (dtimcount < 0)
		dtimcount += dtimperiod;

	cfpcount -= cfp_dec_count;
	if (cfpcount < 0)
		cfpcount += cfpperiod;

	bs.bs_intval = intval;
	bs.bs_nexttbtt = nexttbtt;
	bs.bs_dtimperiod = dtimperiod*intval;
	bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
	bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
	bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
	bs.bs_cfpmaxduration = 0;

	/*
	 * Calculate the number of consecutive beacons to miss* before taking
	 * a BMISS interrupt. The configuration is specified in TU so we only
	 * need calculate based	on the beacon interval.  Note that we clamp the
	 * result to at most 15 beacons.
	 */
	if (sleepduration > intval) {
		bs.bs_bmissthreshold = ATH_DEFAULT_BMISS_LIMIT / 2;
	} else {
		bs.bs_bmissthreshold = DIV_ROUND_UP(bmiss_timeout, intval);
		if (bs.bs_bmissthreshold > 15)
			bs.bs_bmissthreshold = 15;
		else if (bs.bs_bmissthreshold <= 0)
			bs.bs_bmissthreshold = 1;
	}

	/*
	 * Calculate sleep duration. The configuration is given in ms.
	 * We ensure a multiple of the beacon period is used. Also, if the sleep
	 * duration is greater than the DTIM period then it makes senses
	 * to make it a multiple of that.
	 *
	 * XXX fixed at 100ms
	 */

	bs.bs_sleepduration = roundup(IEEE80211_MS_TO_TU(100), sleepduration);
	if (bs.bs_sleepduration > bs.bs_dtimperiod)
		bs.bs_sleepduration = bs.bs_dtimperiod;

	/* TSF out of range threshold fixed at 1 second */
	bs.bs_tsfoor_threshold = ATH9K_TSFOOR_THRESHOLD;

170
	ath_dbg(common, CONFIG, "intval: %u tsf: %llu tsftu: %u\n",
171
		intval, tsf, tsftu);
172
	ath_dbg(common, CONFIG,
J
Joe Perches 已提交
173 174 175
		"bmiss: %u sleep: %u cfp-period: %u maxdur: %u next: %u\n",
		bs.bs_bmissthreshold, bs.bs_sleepduration,
		bs.bs_cfpperiod, bs.bs_cfpmaxduration, bs.bs_cfpnext);
S
Sujith 已提交
176 177 178 179 180 181 182 183 184 185

	/* Set the computed STA beacon timers */

	WMI_CMD(WMI_DISABLE_INTR_CMDID);
	ath9k_hw_set_sta_beacon_timers(priv->ah, &bs);
	imask |= ATH9K_INT_BMISS;
	htc_imask = cpu_to_be32(imask);
	WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
}

186 187 188 189 190 191 192
static void ath9k_htc_beacon_config_ap(struct ath9k_htc_priv *priv,
				       struct htc_beacon_config *bss_conf)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	enum ath9k_int imask = 0;
	u32 nexttbtt, intval, tsftu;
	__be32 htc_imask = 0;
193
	int ret __attribute__ ((unused));
194 195 196
	u8 cmd_rsp;
	u64 tsf;

197
	intval = bss_conf->beacon_interval;
198 199 200
	intval /= ATH9K_HTC_MAX_BCN_VIF;
	nexttbtt = intval;

201 202 203 204 205 206 207 208 209
	/*
	 * To reduce beacon misses under heavy TX load,
	 * set the beacon response time to a larger value.
	 */
	if (intval > DEFAULT_SWBA_RESPONSE)
		priv->ah->config.sw_beacon_response_time = DEFAULT_SWBA_RESPONSE;
	else
		priv->ah->config.sw_beacon_response_time = MIN_SWBA_RESPONSE;

210
	if (test_bit(OP_TSF_RESET, &priv->op_flags)) {
211
		ath9k_hw_reset_tsf(priv->ah);
212
		clear_bit(OP_TSF_RESET, &priv->op_flags);
213 214 215 216 217 218 219 220 221 222 223
	} else {
		/*
		 * Pull nexttbtt forward to reflect the current TSF.
		 */
		tsf = ath9k_hw_gettsf64(priv->ah);
		tsftu = TSF_TO_TU(tsf >> 32, tsf) + FUDGE;
		do {
			nexttbtt += intval;
		} while (nexttbtt < tsftu);
	}

224
	if (test_bit(OP_ENABLE_BEACON, &priv->op_flags))
225 226
		imask |= ATH9K_INT_SWBA;

227 228
	ath_dbg(common, CONFIG,
		"AP Beacon config, intval: %d, nexttbtt: %u, resp_time: %d imask: 0x%x\n",
229 230
		bss_conf->beacon_interval, nexttbtt,
		priv->ah->config.sw_beacon_response_time, imask);
231

232 233
	ath9k_htc_beaconq_config(priv);

234
	WMI_CMD(WMI_DISABLE_INTR_CMDID);
235
	ath9k_hw_beaconinit(priv->ah, TU_TO_USEC(nexttbtt), TU_TO_USEC(intval));
S
Sujith Manoharan 已提交
236
	priv->cur_beacon_conf.bmiss_cnt = 0;
237 238 239 240
	htc_imask = cpu_to_be32(imask);
	WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
}

S
Sujith 已提交
241
static void ath9k_htc_beacon_config_adhoc(struct ath9k_htc_priv *priv,
242
					  struct htc_beacon_config *bss_conf)
S
Sujith 已提交
243 244 245
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	enum ath9k_int imask = 0;
246
	u32 nexttbtt, intval, tsftu;
S
Sujith 已提交
247
	__be32 htc_imask = 0;
248
	int ret __attribute__ ((unused));
S
Sujith 已提交
249
	u8 cmd_rsp;
250
	u64 tsf;
S
Sujith 已提交
251

252
	intval = bss_conf->beacon_interval;
S
Sujith 已提交
253
	nexttbtt = intval;
254 255 256 257 258 259 260 261 262 263

	/*
	 * Pull nexttbtt forward to reflect the current TSF.
	 */
	tsf = ath9k_hw_gettsf64(priv->ah);
	tsftu = TSF_TO_TU(tsf >> 32, tsf) + FUDGE;
	do {
		nexttbtt += intval;
	} while (nexttbtt < tsftu);

264 265 266 267 268 269 270 271
	/*
	 * Only one IBSS interfce is allowed.
	 */
	if (intval > DEFAULT_SWBA_RESPONSE)
		priv->ah->config.sw_beacon_response_time = DEFAULT_SWBA_RESPONSE;
	else
		priv->ah->config.sw_beacon_response_time = MIN_SWBA_RESPONSE;

272
	if (test_bit(OP_ENABLE_BEACON, &priv->op_flags))
S
Sujith 已提交
273 274
		imask |= ATH9K_INT_SWBA;

275 276
	ath_dbg(common, CONFIG,
		"IBSS Beacon config, intval: %d, nexttbtt: %u, resp_time: %d, imask: 0x%x\n",
277 278
		bss_conf->beacon_interval, nexttbtt,
		priv->ah->config.sw_beacon_response_time, imask);
S
Sujith 已提交
279 280

	WMI_CMD(WMI_DISABLE_INTR_CMDID);
281
	ath9k_hw_beaconinit(priv->ah, TU_TO_USEC(nexttbtt), TU_TO_USEC(intval));
S
Sujith Manoharan 已提交
282
	priv->cur_beacon_conf.bmiss_cnt = 0;
S
Sujith 已提交
283 284 285 286
	htc_imask = cpu_to_be32(imask);
	WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
}

S
Sujith 已提交
287 288
void ath9k_htc_beaconep(void *drv_priv, struct sk_buff *skb,
			enum htc_endpoint_id ep_id, bool txok)
S
Sujith 已提交
289
{
S
Sujith 已提交
290
	dev_kfree_skb_any(skb);
S
Sujith 已提交
291 292
}

293 294 295 296 297 298 299
static void ath9k_htc_send_buffered(struct ath9k_htc_priv *priv,
				    int slot)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ieee80211_vif *vif;
	struct sk_buff *skb;
	struct ieee80211_hdr *hdr;
S
Sujith Manoharan 已提交
300
	int padpos, padsize, ret, tx_slot;
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

	spin_lock_bh(&priv->beacon_lock);

	vif = priv->cur_beacon_conf.bslot[slot];

	skb = ieee80211_get_buffered_bc(priv->hw, vif);

	while(skb) {
		hdr = (struct ieee80211_hdr *) skb->data;

		padpos = ath9k_cmn_padpos(hdr->frame_control);
		padsize = padpos & 3;
		if (padsize && skb->len > padpos) {
			if (skb_headroom(skb) < padsize) {
				dev_kfree_skb_any(skb);
				goto next;
			}
			skb_push(skb, padsize);
			memmove(skb->data, skb->data + padsize, padpos);
		}

S
Sujith Manoharan 已提交
322
		tx_slot = ath9k_htc_tx_get_slot(priv);
323
		if (tx_slot < 0) {
324
			ath_dbg(common, XMIT, "No free CAB slot\n");
S
Sujith Manoharan 已提交
325 326 327 328 329
			dev_kfree_skb_any(skb);
			goto next;
		}

		ret = ath9k_htc_tx_start(priv, skb, tx_slot, true);
330
		if (ret != 0) {
S
Sujith Manoharan 已提交
331
			ath9k_htc_tx_clear_slot(priv, tx_slot);
332
			dev_kfree_skb_any(skb);
S
Sujith Manoharan 已提交
333

334
			ath_dbg(common, XMIT, "Failed to send CAB frame\n");
335 336 337 338
		} else {
			spin_lock_bh(&priv->tx.tx_lock);
			priv->tx.queued_cnt++;
			spin_unlock_bh(&priv->tx.tx_lock);
339 340 341 342 343 344 345 346
		}
	next:
		skb = ieee80211_get_buffered_bc(priv->hw, vif);
	}

	spin_unlock_bh(&priv->beacon_lock);
}

S
Sujith Manoharan 已提交
347 348
static void ath9k_htc_send_beacon(struct ath9k_htc_priv *priv,
				  int slot)
S
Sujith 已提交
349
{
350
	struct ath_common *common = ath9k_hw_common(priv->ah);
S
Sujith Manoharan 已提交
351 352
	struct ieee80211_vif *vif;
	struct ath9k_htc_vif *avp;
S
Sujith 已提交
353
	struct tx_beacon_header beacon_hdr;
354
	struct ath9k_htc_tx_ctl *tx_ctl;
S
Sujith 已提交
355
	struct ieee80211_tx_info *info;
356
	struct ieee80211_mgmt *mgmt;
S
Sujith 已提交
357
	struct sk_buff *beacon;
S
Sujith 已提交
358
	u8 *tx_fhdr;
359
	int ret;
S
Sujith 已提交
360 361 362 363 364

	memset(&beacon_hdr, 0, sizeof(struct tx_beacon_header));

	spin_lock_bh(&priv->beacon_lock);

S
Sujith Manoharan 已提交
365 366 367
	vif = priv->cur_beacon_conf.bslot[slot];
	avp = (struct ath9k_htc_vif *)vif->drv_priv;

368
	if (unlikely(test_bit(OP_SCANNING, &priv->op_flags))) {
S
Sujith 已提交
369 370 371 372 373
		spin_unlock_bh(&priv->beacon_lock);
		return;
	}

	/* Get a new beacon */
S
Sujith Manoharan 已提交
374
	beacon = ieee80211_beacon_get(priv->hw, vif);
S
Sujith 已提交
375
	if (!beacon) {
S
Sujith 已提交
376 377 378 379
		spin_unlock_bh(&priv->beacon_lock);
		return;
	}

380 381 382 383 384 385 386
	/*
	 * Update the TSF adjust value here, the HW will
	 * add this value for every beacon.
	 */
	mgmt = (struct ieee80211_mgmt *)beacon->data;
	mgmt->u.beacon.timestamp = avp->tsfadjust;

S
Sujith 已提交
387
	info = IEEE80211_SKB_CB(beacon);
S
Sujith 已提交
388 389
	if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
		struct ieee80211_hdr *hdr =
S
Sujith 已提交
390
			(struct ieee80211_hdr *) beacon->data;
391
		avp->seq_no += 0x10;
S
Sujith 已提交
392
		hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
393
		hdr->seq_ctrl |= cpu_to_le16(avp->seq_no);
S
Sujith 已提交
394 395
	}

396 397 398 399
	tx_ctl = HTC_SKB_CB(beacon);
	memset(tx_ctl, 0, sizeof(*tx_ctl));

	tx_ctl->type = ATH9K_HTC_BEACON;
400
	tx_ctl->epid = priv->beacon_ep;
401

S
Sujith 已提交
402
	beacon_hdr.vif_index = avp->index;
S
Sujith 已提交
403
	tx_fhdr = skb_push(beacon, sizeof(beacon_hdr));
S
Sujith 已提交
404 405
	memcpy(tx_fhdr, (u8 *) &beacon_hdr, sizeof(beacon_hdr));

406
	ret = htc_send(priv->htc, beacon);
407 408
	if (ret != 0) {
		if (ret == -ENOMEM) {
409
			ath_dbg(common, BSTUCK,
410 411 412 413
				"Failed to send beacon, no free TX buffer\n");
		}
		dev_kfree_skb_any(beacon);
	}
S
Sujith 已提交
414 415 416 417

	spin_unlock_bh(&priv->beacon_lock);
}

S
Sujith Manoharan 已提交
418 419
static int ath9k_htc_choose_bslot(struct ath9k_htc_priv *priv,
				  struct wmi_event_swba *swba)
S
Sujith Manoharan 已提交
420 421 422 423 424 425 426
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	u64 tsf;
	u32 tsftu;
	u16 intval;
	int slot;

427
	intval = priv->cur_beacon_conf.beacon_interval;
S
Sujith Manoharan 已提交
428

S
Sujith Manoharan 已提交
429
	tsf = be64_to_cpu(swba->tsf);
S
Sujith Manoharan 已提交
430 431 432 433
	tsftu = TSF_TO_TU(tsf >> 32, tsf);
	slot = ((tsftu % intval) * ATH9K_HTC_MAX_BCN_VIF) / intval;
	slot = ATH9K_HTC_MAX_BCN_VIF - slot - 1;

434
	ath_dbg(common, BEACON,
S
Sujith Manoharan 已提交
435 436 437 438 439 440
		"Choose slot: %d, tsf: %llu, tsftu: %u, intval: %u\n",
		slot, tsf, tsftu, intval);

	return slot;
}

S
Sujith Manoharan 已提交
441 442
void ath9k_htc_swba(struct ath9k_htc_priv *priv,
		    struct wmi_event_swba *swba)
S
Sujith Manoharan 已提交
443 444 445 446
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	int slot;

S
Sujith Manoharan 已提交
447
	if (swba->beacon_pending != 0) {
S
Sujith Manoharan 已提交
448 449
		priv->cur_beacon_conf.bmiss_cnt++;
		if (priv->cur_beacon_conf.bmiss_cnt > BSTUCK_THRESHOLD) {
450
			ath_dbg(common, BSTUCK, "Beacon stuck, HW reset\n");
S
Sujith Manoharan 已提交
451 452
			ieee80211_queue_work(priv->hw,
					     &priv->fatal_work);
S
Sujith Manoharan 已提交
453 454 455 456 457
		}
		return;
	}

	if (priv->cur_beacon_conf.bmiss_cnt) {
458
		ath_dbg(common, BSTUCK,
S
Sujith Manoharan 已提交
459 460 461 462 463
			"Resuming beacon xmit after %u misses\n",
			priv->cur_beacon_conf.bmiss_cnt);
		priv->cur_beacon_conf.bmiss_cnt = 0;
	}

S
Sujith Manoharan 已提交
464
	slot = ath9k_htc_choose_bslot(priv, swba);
S
Sujith Manoharan 已提交
465 466 467 468 469 470 471
	spin_lock_bh(&priv->beacon_lock);
	if (priv->cur_beacon_conf.bslot[slot] == NULL) {
		spin_unlock_bh(&priv->beacon_lock);
		return;
	}
	spin_unlock_bh(&priv->beacon_lock);

472
	ath9k_htc_send_buffered(priv, slot);
S
Sujith Manoharan 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
	ath9k_htc_send_beacon(priv, slot);
}

void ath9k_htc_assign_bslot(struct ath9k_htc_priv *priv,
			    struct ieee80211_vif *vif)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *)vif->drv_priv;
	int i = 0;

	spin_lock_bh(&priv->beacon_lock);
	for (i = 0; i < ATH9K_HTC_MAX_BCN_VIF; i++) {
		if (priv->cur_beacon_conf.bslot[i] == NULL) {
			avp->bslot = i;
			break;
		}
	}

	priv->cur_beacon_conf.bslot[avp->bslot] = vif;
	spin_unlock_bh(&priv->beacon_lock);

494 495
	ath_dbg(common, CONFIG, "Added interface at beacon slot: %d\n",
		avp->bslot);
S
Sujith Manoharan 已提交
496 497 498 499 500 501 502 503 504 505 506 507
}

void ath9k_htc_remove_bslot(struct ath9k_htc_priv *priv,
			    struct ieee80211_vif *vif)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *)vif->drv_priv;

	spin_lock_bh(&priv->beacon_lock);
	priv->cur_beacon_conf.bslot[avp->bslot] = NULL;
	spin_unlock_bh(&priv->beacon_lock);

508 509
	ath_dbg(common, CONFIG, "Removed interface at beacon slot: %d\n",
		avp->bslot);
S
Sujith Manoharan 已提交
510 511
}

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
/*
 * Calculate the TSF adjustment value for all slots
 * other than zero.
 */
void ath9k_htc_set_tsfadjust(struct ath9k_htc_priv *priv,
			     struct ieee80211_vif *vif)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *)vif->drv_priv;
	struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;
	u64 tsfadjust;

	if (avp->bslot == 0)
		return;

	/*
	 * The beacon interval cannot be different for multi-AP mode,
	 * and we reach here only for VIF slots greater than zero,
	 * so beacon_interval is guaranteed to be set in cur_conf.
	 */
	tsfadjust = cur_conf->beacon_interval * avp->bslot / ATH9K_HTC_MAX_BCN_VIF;
	avp->tsfadjust = cpu_to_le64(TU_TO_USEC(tsfadjust));

535
	ath_dbg(common, CONFIG, "tsfadjust is: %llu for bslot: %d\n",
536 537 538
		(unsigned long long)tsfadjust, avp->bslot);
}

539 540 541 542 543 544 545 546 547 548 549 550
static void ath9k_htc_beacon_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
{
	bool *beacon_configured = (bool *)data;
	struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;

	if (vif->type == NL80211_IFTYPE_STATION &&
	    avp->beacon_configured)
		*beacon_configured = true;
}

static bool ath9k_htc_check_beacon_config(struct ath9k_htc_priv *priv,
					  struct ieee80211_vif *vif)
S
Sujith 已提交
551 552
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
553
	struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;
S
Sujith 已提交
554
	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
555
	bool beacon_configured;
556

557 558 559 560 561 562 563 564 565
	/*
	 * Changing the beacon interval when multiple AP interfaces
	 * are configured will affect beacon transmission of all
	 * of them.
	 */
	if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
	    (priv->num_ap_vif > 1) &&
	    (vif->type == NL80211_IFTYPE_AP) &&
	    (cur_conf->beacon_interval != bss_conf->beacon_int)) {
566
		ath_dbg(common, CONFIG,
567
			"Changing beacon interval of multiple AP interfaces !\n");
568
		return false;
569 570 571 572 573 574 575 576
	}

	/*
	 * If the HW is operating in AP mode, any new station interfaces that
	 * are added cannot change the beacon parameters.
	 */
	if (priv->num_ap_vif &&
	    (vif->type != NL80211_IFTYPE_AP)) {
577
		ath_dbg(common, CONFIG,
578
			"HW in AP mode, cannot set STA beacon parameters\n");
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
		return false;
	}

	/*
	 * The beacon parameters are configured only for the first
	 * station interface.
	 */
	if ((priv->ah->opmode == NL80211_IFTYPE_STATION) &&
	    (priv->num_sta_vif > 1) &&
	    (vif->type == NL80211_IFTYPE_STATION)) {
		beacon_configured = false;
		ieee80211_iterate_active_interfaces_atomic(priv->hw,
							   ath9k_htc_beacon_iter,
							   &beacon_configured);

		if (beacon_configured) {
595
			ath_dbg(common, CONFIG,
596 597 598
				"Beacon already configured for a station interface\n");
			return false;
		}
599 600
	}

601 602 603 604 605 606 607 608 609 610 611 612 613 614
	return true;
}

void ath9k_htc_beacon_config(struct ath9k_htc_priv *priv,
			     struct ieee80211_vif *vif)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;
	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
	struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;

	if (!ath9k_htc_check_beacon_config(priv, vif))
		return;

S
Sujith 已提交
615
	cur_conf->beacon_interval = bss_conf->beacon_int;
616 617 618
	if (cur_conf->beacon_interval == 0)
		cur_conf->beacon_interval = 100;

S
Sujith 已提交
619 620 621 622 623
	cur_conf->dtim_period = bss_conf->dtim_period;
	cur_conf->bmiss_timeout =
		ATH_DEFAULT_BMISS_LIMIT * cur_conf->beacon_interval;

	switch (vif->type) {
S
Sujith 已提交
624
	case NL80211_IFTYPE_STATION:
625
		ath9k_htc_beacon_config_sta(priv, cur_conf);
626
		avp->beacon_configured = true;
S
Sujith 已提交
627 628
		break;
	case NL80211_IFTYPE_ADHOC:
629
		ath9k_htc_beacon_config_adhoc(priv, cur_conf);
S
Sujith 已提交
630
		break;
631 632 633
	case NL80211_IFTYPE_AP:
		ath9k_htc_beacon_config_ap(priv, cur_conf);
		break;
S
Sujith 已提交
634
	default:
635
		ath_dbg(common, CONFIG, "Unsupported beaconing mode\n");
S
Sujith 已提交
636 637 638
		return;
	}
}
639 640 641 642 643 644 645 646 647 648 649 650 651

void ath9k_htc_beacon_reconfig(struct ath9k_htc_priv *priv)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;

	switch (priv->ah->opmode) {
	case NL80211_IFTYPE_STATION:
		ath9k_htc_beacon_config_sta(priv, cur_conf);
		break;
	case NL80211_IFTYPE_ADHOC:
		ath9k_htc_beacon_config_adhoc(priv, cur_conf);
		break;
652 653 654
	case NL80211_IFTYPE_AP:
		ath9k_htc_beacon_config_ap(priv, cur_conf);
		break;
655
	default:
656
		ath_dbg(common, CONFIG, "Unsupported beaconing mode\n");
657 658 659
		return;
	}
}