channel.c 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
/*
 * Copyright (c) 2014 Qualcomm Atheros, Inc.
 *
 * 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 "ath9k.h"

/* Set/change channels.  If the channel is really being changed, it's done
 * by reseting the chip.  To accomplish this we must first cleanup any pending
 * DMA, then restart stuff.
 */
static int ath_set_channel(struct ath_softc *sc)
{
	struct ath_hw *ah = sc->sc_ah;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ieee80211_hw *hw = sc->hw;
	struct ath9k_channel *hchan;
	struct cfg80211_chan_def *chandef = &sc->cur_chan->chandef;
	struct ieee80211_channel *chan = chandef->chan;
	int pos = chan->hw_value;
	int old_pos = -1;
	int r;

	if (test_bit(ATH_OP_INVALID, &common->op_flags))
		return -EIO;

	if (ah->curchan)
		old_pos = ah->curchan - &ah->channels[0];

	ath_dbg(common, CONFIG, "Set channel: %d MHz width: %d\n",
		chan->center_freq, chandef->width);

	/* update survey stats for the old channel before switching */
	spin_lock_bh(&common->cc_lock);
	ath_update_survey_stats(sc);
	spin_unlock_bh(&common->cc_lock);

	ath9k_cmn_get_channel(hw, ah, chandef);

	/* If the operating channel changes, change the survey in-use flags
	 * along with it.
	 * Reset the survey data for the new channel, unless we're switching
	 * back to the operating channel from an off-channel operation.
	 */
	if (!sc->cur_chan->offchannel && sc->cur_survey != &sc->survey[pos]) {
		if (sc->cur_survey)
			sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;

		sc->cur_survey = &sc->survey[pos];

		memset(sc->cur_survey, 0, sizeof(struct survey_info));
		sc->cur_survey->filled |= SURVEY_INFO_IN_USE;
	} else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) {
		memset(&sc->survey[pos], 0, sizeof(struct survey_info));
	}

	hchan = &sc->sc_ah->channels[pos];
	r = ath_reset_internal(sc, hchan);
	if (r)
		return r;

	/* The most recent snapshot of channel->noisefloor for the old
	 * channel is only available after the hardware reset. Copy it to
	 * the survey stats now.
	 */
	if (old_pos >= 0)
		ath_update_survey_nf(sc, old_pos);

	/* Enable radar pulse detection if on a DFS channel. Spectral
	 * scanning and radar detection can not be used concurrently.
	 */
	if (hw->conf.radar_enabled) {
		u32 rxfilter;

		/* set HW specific DFS configuration */
		ath9k_hw_set_radar_params(ah);
		rxfilter = ath9k_hw_getrxfilter(ah);
		rxfilter |= ATH9K_RX_FILTER_PHYRADAR |
				ATH9K_RX_FILTER_PHYERR;
		ath9k_hw_setrxfilter(ah, rxfilter);
		ath_dbg(common, DFS, "DFS enabled at freq %d\n",
			chan->center_freq);
	} else {
		/* perform spectral scan if requested. */
		if (test_bit(ATH_OP_SCANNING, &common->op_flags) &&
			sc->spectral_mode == SPECTRAL_CHANSCAN)
			ath9k_spectral_scan_trigger(hw);
	}

	return 0;
}

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
static bool
ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
			      bool powersave)
{
	struct ieee80211_vif *vif = avp->vif;
	struct ieee80211_sta *sta = NULL;
	struct ieee80211_hdr_3addr *nullfunc;
	struct ath_tx_control txctl;
	struct sk_buff *skb;
	int band = sc->cur_chan->chandef.chan->band;

	switch (vif->type) {
	case NL80211_IFTYPE_STATION:
		if (!vif->bss_conf.assoc)
			return false;

		skb = ieee80211_nullfunc_get(sc->hw, vif);
		if (!skb)
			return false;

		nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
		if (powersave)
			nullfunc->frame_control |=
				cpu_to_le16(IEEE80211_FCTL_PM);

		skb_set_queue_mapping(skb, IEEE80211_AC_VO);
		if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
			dev_kfree_skb_any(skb);
			return false;
		}
		break;
	default:
		return false;
	}

	memset(&txctl, 0, sizeof(txctl));
	txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
	txctl.sta = sta;
	txctl.force_channel = true;
	if (ath_tx_start(sc->hw, skb, &txctl)) {
		ieee80211_free_txskb(sc->hw, skb);
		return false;
	}

	return true;
}

void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
{
F
Felix Fietkau 已提交
153
	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
154 155
	struct ath_vif *avp;
	bool active = false;
F
Felix Fietkau 已提交
156
	u8 n_active = 0;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

	if (!ctx)
		return;

	list_for_each_entry(avp, &ctx->vifs, list) {
		struct ieee80211_vif *vif = avp->vif;

		switch (vif->type) {
		case NL80211_IFTYPE_P2P_CLIENT:
		case NL80211_IFTYPE_STATION:
			if (vif->bss_conf.assoc)
				active = true;
			break;
		default:
			active = true;
			break;
		}
	}
	ctx->active = active;
F
Felix Fietkau 已提交
176 177 178 179 180 181 182 183 184 185 186

	ath_for_each_chanctx(sc, ctx) {
		if (!ctx->assigned || list_empty(&ctx->vifs))
			continue;
		n_active++;
	}

	if (n_active > 1)
		set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
	else
		clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
}

static bool
ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
{
	struct ath_vif *avp;
	bool sent = false;

	rcu_read_lock();
	list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
		if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
			sent = true;
	}
	rcu_read_unlock();

	return sent;
}

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
static bool ath_chanctx_defer_switch(struct ath_softc *sc)
{
	if (sc->cur_chan == &sc->offchannel.chan)
		return false;

	switch (sc->sched.state) {
	case ATH_CHANCTX_STATE_SWITCH:
		return false;
	case ATH_CHANCTX_STATE_IDLE:
		if (!sc->cur_chan->switch_after_beacon)
			return false;

		sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
		break;
	default:
		break;
	}

	return true;
}

226 227 228 229
void ath_chanctx_work(struct work_struct *work)
{
	struct ath_softc *sc = container_of(work, struct ath_softc,
					    chanctx_work);
230 231
	struct timespec ts;
	bool measure_time = false;
232
	bool send_ps = false;
233 234 235 236 237 238 239 240 241

	mutex_lock(&sc->mutex);
	spin_lock_bh(&sc->chan_lock);
	if (!sc->next_chan) {
		spin_unlock_bh(&sc->chan_lock);
		mutex_unlock(&sc->mutex);
		return;
	}

242 243 244 245 246 247
	if (ath_chanctx_defer_switch(sc)) {
		spin_unlock_bh(&sc->chan_lock);
		mutex_unlock(&sc->mutex);
		return;
	}

248 249 250 251
	if (sc->cur_chan != sc->next_chan) {
		sc->cur_chan->stopped = true;
		spin_unlock_bh(&sc->chan_lock);

252 253 254 255
		if (sc->next_chan == &sc->offchannel.chan) {
			getrawmonotonic(&ts);
			measure_time = true;
		}
256 257
		__ath9k_flush(sc->hw, ~0, true);

258 259 260 261
		if (ath_chanctx_send_ps_frame(sc, true))
			__ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);

		send_ps = true;
262
		spin_lock_bh(&sc->chan_lock);
263 264 265 266 267

		if (sc->cur_chan != &sc->offchannel.chan) {
			getrawmonotonic(&sc->cur_chan->tsf_ts);
			sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
		}
268 269 270 271
	}
	sc->cur_chan = sc->next_chan;
	sc->cur_chan->stopped = false;
	sc->next_chan = NULL;
272
	sc->sched.state = ATH_CHANCTX_STATE_IDLE;
F
Felix Fietkau 已提交
273 274
	sc->sched.offchannel_duration = 0;

275 276 277 278
	spin_unlock_bh(&sc->chan_lock);

	if (sc->sc_ah->chip_fullsleep ||
	    memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
279
		   sizeof(sc->cur_chandef))) {
280
		ath_set_channel(sc);
281 282 283 284
		if (measure_time)
			sc->sched.channel_switch_time =
				ath9k_hw_get_tsf_offset(&ts, NULL);
	}
285 286 287
	if (send_ps)
		ath_chanctx_send_ps_frame(sc, false);

F
Felix Fietkau 已提交
288
	ath_offchannel_channel_change(sc);
289 290 291
	mutex_unlock(&sc->mutex);
}

292 293 294 295 296 297
void ath_chanctx_init(struct ath_softc *sc)
{
	struct ath_chanctx *ctx;
	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
	struct ieee80211_supported_band *sband;
	struct ieee80211_channel *chan;
298
	int i, j;
299 300 301 302 303 304 305 306 307 308

	sband = &common->sbands[IEEE80211_BAND_2GHZ];
	if (!sband->n_channels)
		sband = &common->sbands[IEEE80211_BAND_5GHZ];

	chan = &sband->channels[0];
	for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
		ctx = &sc->chanctx[i];
		cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
		INIT_LIST_HEAD(&ctx->vifs);
309
		ctx->txpower = ATH_TXPOWER_MAX;
310 311
		for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
			INIT_LIST_HEAD(&ctx->acq[j]);
312
	}
F
Felix Fietkau 已提交
313 314 315 316 317 318 319 320
	ctx = &sc->offchannel.chan;
	cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
	INIT_LIST_HEAD(&ctx->vifs);
	ctx->txpower = ATH_TXPOWER_MAX;
	for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
		INIT_LIST_HEAD(&ctx->acq[j]);
	sc->offchannel.chan.offchannel = true;

321 322
}

323 324
void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
			struct cfg80211_chan_def *chandef)
325 326
{

327 328 329 330
	spin_lock_bh(&sc->chan_lock);
	sc->next_chan = ctx;
	if (chandef)
		ctx->chandef = *chandef;
F
Felix Fietkau 已提交
331 332 333 334 335 336

	if (sc->next_chan == &sc->offchannel.chan) {
		sc->sched.offchannel_duration =
			TU_TO_USEC(sc->offchannel.duration) +
			sc->sched.channel_switch_time;
	}
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	spin_unlock_bh(&sc->chan_lock);
	ieee80211_queue_work(sc->hw, &sc->chanctx_work);
}

void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
			     struct cfg80211_chan_def *chandef)
{
	bool cur_chan;

	spin_lock_bh(&sc->chan_lock);
	if (chandef)
		memcpy(&ctx->chandef, chandef, sizeof(*chandef));
	cur_chan = sc->cur_chan == ctx;
	spin_unlock_bh(&sc->chan_lock);

	if (!cur_chan)
		return;

	ath_set_channel(sc);
356
}
F
Felix Fietkau 已提交
357

358
struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc, bool active)
F
Felix Fietkau 已提交
359
{
360 361 362 363 364 365 366
	struct ath_chanctx *ctx;

	ath_for_each_chanctx(sc, ctx) {
		if (!ctx->assigned || list_empty(&ctx->vifs))
			continue;
		if (active && !ctx->active)
			continue;
F
Felix Fietkau 已提交
367

368
		return ctx;
F
Felix Fietkau 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382
	}

	return &sc->chanctx[0];
}

void ath_chanctx_offchan_switch(struct ath_softc *sc,
				struct ieee80211_channel *chan)
{
	struct cfg80211_chan_def chandef;

	cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);

	ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
}
383 384 385 386 387

void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
		       enum ath_chanctx_event ev)
{
	struct ath_hw *ah = sc->sc_ah;
F
Felix Fietkau 已提交
388
	struct ath_vif *avp = NULL;
389
	u32 tsf_time;
F
Felix Fietkau 已提交
390 391 392 393
	bool noa_changed = false;

	if (vif)
		avp = (struct ath_vif *) vif->drv_priv;
394 395 396 397 398

	spin_lock_bh(&sc->chan_lock);

	switch (ev) {
	case ATH_CHANCTX_EVENT_BEACON_PREPARE:
F
Felix Fietkau 已提交
399 400 401
		if (avp->offchannel_duration)
			avp->offchannel_duration = 0;

402 403 404 405 406
		if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
			break;

		sc->sched.beacon_pending = true;
		sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
F
Felix Fietkau 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

		/* defer channel switch by a quarter beacon interval */
		tsf_time = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
		tsf_time = sc->sched.next_tbtt + tsf_time / 4;
		sc->sched.switch_start_time = tsf_time;

		if (sc->sched.offchannel_duration) {
			noa_changed = true;
			avp->offchannel_start = tsf_time;
			avp->offchannel_duration =
				sc->sched.offchannel_duration;
		}

		if (noa_changed)
			avp->noa_index++;
422 423 424 425 426 427 428 429 430 431
		break;
	case ATH_CHANCTX_EVENT_BEACON_SENT:
		if (!sc->sched.beacon_pending)
			break;

		sc->sched.beacon_pending = false;
		if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
			break;

		sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
F
Felix Fietkau 已提交
432 433 434
		ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer,
					 sc->sched.switch_start_time,
					 1000000);
435 436 437 438 439 440 441 442 443 444 445 446
		break;
	case ATH_CHANCTX_EVENT_TSF_TIMER:
		if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
			break;

		sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
		ieee80211_queue_work(sc->hw, &sc->chanctx_work);
		break;
	}

	spin_unlock_bh(&sc->chan_lock);
}