File Index
Files and directories
Sign in
FILE

channel.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/wireless/ath/ath9k/channel.c C
   1/*
   2 * Copyright (c) 2014 Qualcomm Atheros, 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
  17#include "ath9k.h"
  18
  19/* Set/change channels.  If the channel is really being changed, it's done
  20 * by resetting the chip.  To accomplish this we must first cleanup any pending
  21 * DMA, then restart stuff.
  22 */
  23static int ath_set_channel(struct ath_softc *sc)
  24{
  25	struct ath_hw *ah = sc->sc_ah;
  26	struct ath_common *common = ath9k_hw_common(ah);
  27	struct ieee80211_hw *hw = sc->hw;
  28	struct ath9k_channel *hchan;
  29	struct cfg80211_chan_def *chandef = &sc->cur_chan->chandef;
  30	struct ieee80211_channel *chan = chandef->chan;
  31	int pos = chan->hw_value;
  32	unsigned long flags;
  33	int old_pos = -1;
  34	int r;
  35
  36	if (test_bit(ATH_OP_INVALID, &common->op_flags))
  37		return -EIO;
  38
  39	if (ah->curchan)
  40		old_pos = ah->curchan - &ah->channels[0];
  41
  42	ath_dbg(common, CONFIG, "Set channel: %d MHz width: %d\n",
  43		chan->center_freq, chandef->width);
  44
  45	/* update survey stats for the old channel before switching */
  46	spin_lock_irqsave(&common->cc_lock, flags);
  47	ath_update_survey_stats(sc);
  48	spin_unlock_irqrestore(&common->cc_lock, flags);
  49
  50	ath9k_cmn_get_channel(hw, ah, chandef);
  51
  52	/* If the operating channel changes, change the survey in-use flags
  53	 * along with it.
  54	 * Reset the survey data for the new channel, unless we're switching
  55	 * back to the operating channel from an off-channel operation.
  56	 */
  57	if (!sc->cur_chan->offchannel && sc->cur_survey != &sc->survey[pos]) {
  58		if (sc->cur_survey)
  59			sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;
  60
  61		sc->cur_survey = &sc->survey[pos];
  62
  63		memset(sc->cur_survey, 0, sizeof(struct survey_info));
  64		sc->cur_survey->filled |= SURVEY_INFO_IN_USE;
  65	} else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) {
  66		memset(&sc->survey[pos], 0, sizeof(struct survey_info));
  67	}
  68
  69	hchan = &sc->sc_ah->channels[pos];
  70	r = ath_reset(sc, hchan);
  71	if (r)
  72		return r;
  73
  74	/* The most recent snapshot of channel->noisefloor for the old
  75	 * channel is only available after the hardware reset. Copy it to
  76	 * the survey stats now.
  77	 */
  78	if (old_pos >= 0)
  79		ath_update_survey_nf(sc, old_pos);
  80
  81	/* Enable radar pulse detection if on a DFS channel. Spectral
  82	 * scanning and radar detection can not be used concurrently.
  83	 */
  84	if (hw->conf.radar_enabled) {
  85		u32 rxfilter;
  86
  87		rxfilter = ath9k_hw_getrxfilter(ah);
  88		rxfilter |= ATH9K_RX_FILTER_PHYRADAR |
  89				ATH9K_RX_FILTER_PHYERR;
  90		ath9k_hw_setrxfilter(ah, rxfilter);
  91		ath_dbg(common, DFS, "DFS enabled at freq %d\n",
  92			chan->center_freq);
  93	} else {
  94		/* perform spectral scan if requested. */
  95		if (test_bit(ATH_OP_SCANNING, &common->op_flags) &&
  96			sc->spec_priv.spectral_mode == SPECTRAL_CHANSCAN)
  97			ath9k_cmn_spectral_scan_trigger(common, &sc->spec_priv);
  98	}
  99
 100	return 0;
 101}
 102
 103void ath_chanctx_init(struct ath_softc *sc)
 104{
 105	struct ath_chanctx *ctx;
 106	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 107	struct ieee80211_supported_band *sband;
 108	struct ieee80211_channel *chan;
 109	int i, j;
 110
 111	sband = &common->sbands[NL80211_BAND_2GHZ];
 112	if (!sband->n_channels)
 113		sband = &common->sbands[NL80211_BAND_5GHZ];
 114
 115	chan = &sband->channels[0];
 116	for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
 117		ctx = &sc->chanctx[i];
 118		cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
 119		INIT_LIST_HEAD(&ctx->vifs);
 120		ctx->txpower = ATH_TXPOWER_MAX;
 121		ctx->flush_timeout = HZ / 5; /* 200ms */
 122		for (j = 0; j < ARRAY_SIZE(ctx->acq); j++) {
 123			INIT_LIST_HEAD(&ctx->acq[j].acq_new);
 124			INIT_LIST_HEAD(&ctx->acq[j].acq_old);
 125			spin_lock_init(&ctx->acq[j].lock);
 126		}
 127	}
 128}
 129
 130void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
 131			     struct cfg80211_chan_def *chandef)
 132{
 133	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 134	bool cur_chan;
 135
 136	spin_lock_bh(&sc->chan_lock);
 137	if (chandef)
 138		memcpy(&ctx->chandef, chandef, sizeof(*chandef));
 139	cur_chan = sc->cur_chan == ctx;
 140	spin_unlock_bh(&sc->chan_lock);
 141
 142	if (!cur_chan) {
 143		ath_dbg(common, CHAN_CTX,
 144			"Current context differs from the new context\n");
 145		return;
 146	}
 147
 148	ath_set_channel(sc);
 149}
 150
 151#ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
 152
 153/*************/
 154/* Utilities */
 155/*************/
 156
 157struct ath_chanctx* ath_is_go_chanctx_present(struct ath_softc *sc)
 158{
 159	struct ath_chanctx *ctx;
 160	struct ath_vif *avp;
 161	struct ieee80211_vif *vif;
 162
 163	spin_lock_bh(&sc->chan_lock);
 164
 165	ath_for_each_chanctx(sc, ctx) {
 166		if (!ctx->active)
 167			continue;
 168
 169		list_for_each_entry(avp, &ctx->vifs, list) {
 170			vif = avp->vif;
 171
 172			if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_P2P_GO) {
 173				spin_unlock_bh(&sc->chan_lock);
 174				return ctx;
 175			}
 176		}
 177	}
 178
 179	spin_unlock_bh(&sc->chan_lock);
 180	return NULL;
 181}
 182
 183/**********************************************************/
 184/* Functions to handle the channel context state machine. */
 185/**********************************************************/
 186
 187static const char *offchannel_state_string(enum ath_offchannel_state state)
 188{
 189	switch (state) {
 190		case_rtn_string(ATH_OFFCHANNEL_IDLE);
 191		case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
 192		case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
 193		case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
 194		case_rtn_string(ATH_OFFCHANNEL_ROC_START);
 195		case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
 196		case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
 197	default:
 198		return "unknown";
 199	}
 200}
 201
 202static const char *chanctx_event_string(enum ath_chanctx_event ev)
 203{
 204	switch (ev) {
 205		case_rtn_string(ATH_CHANCTX_EVENT_BEACON_PREPARE);
 206		case_rtn_string(ATH_CHANCTX_EVENT_BEACON_SENT);
 207		case_rtn_string(ATH_CHANCTX_EVENT_TSF_TIMER);
 208		case_rtn_string(ATH_CHANCTX_EVENT_BEACON_RECEIVED);
 209		case_rtn_string(ATH_CHANCTX_EVENT_AUTHORIZED);
 210		case_rtn_string(ATH_CHANCTX_EVENT_SWITCH);
 211		case_rtn_string(ATH_CHANCTX_EVENT_ASSIGN);
 212		case_rtn_string(ATH_CHANCTX_EVENT_UNASSIGN);
 213		case_rtn_string(ATH_CHANCTX_EVENT_CHANGE);
 214		case_rtn_string(ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
 215	default:
 216		return "unknown";
 217	}
 218}
 219
 220static const char *chanctx_state_string(enum ath_chanctx_state state)
 221{
 222	switch (state) {
 223		case_rtn_string(ATH_CHANCTX_STATE_IDLE);
 224		case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_BEACON);
 225		case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_TIMER);
 226		case_rtn_string(ATH_CHANCTX_STATE_SWITCH);
 227		case_rtn_string(ATH_CHANCTX_STATE_FORCE_ACTIVE);
 228	default:
 229		return "unknown";
 230	}
 231}
 232
 233static u32 chanctx_event_delta(struct ath_softc *sc)
 234{
 235	ktime_t ts = ktime_get_raw();
 236	s64 ms = ktime_ms_delta(ts, sc->last_event_time);
 237
 238	sc->last_event_time = ts;
 239	return ms;
 240}
 241
 242void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
 243{
 244	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 245	struct ath_chanctx *ictx;
 246	struct ath_vif *avp;
 247	bool active = false;
 248	u8 n_active = 0;
 249
 250	if (!ctx)
 251		return;
 252
 253	if (ctx == &sc->offchannel.chan) {
 254		spin_lock_bh(&sc->chan_lock);
 255
 256		if (likely(sc->sched.channel_switch_time))
 257			ctx->flush_timeout =
 258				usecs_to_jiffies(sc->sched.channel_switch_time);
 259		else
 260			ctx->flush_timeout =
 261				msecs_to_jiffies(10);
 262
 263		spin_unlock_bh(&sc->chan_lock);
 264
 265		/*
 266		 * There is no need to iterate over the
 267		 * active/assigned channel contexts if
 268		 * the current context is offchannel.
 269		 */
 270		return;
 271	}
 272
 273	ictx = ctx;
 274
 275	list_for_each_entry(avp, &ctx->vifs, list) {
 276		struct ieee80211_vif *vif = avp->vif;
 277
 278		switch (vif->type) {
 279		case NL80211_IFTYPE_P2P_CLIENT:
 280		case NL80211_IFTYPE_STATION:
 281			if (avp->assoc)
 282				active = true;
 283			break;
 284		default:
 285			active = true;
 286			break;
 287		}
 288	}
 289	ctx->active = active;
 290
 291	ath_for_each_chanctx(sc, ctx) {
 292		if (!ctx->assigned || list_empty(&ctx->vifs))
 293			continue;
 294		n_active++;
 295	}
 296
 297	spin_lock_bh(&sc->chan_lock);
 298
 299	if (n_active <= 1) {
 300		ictx->flush_timeout = HZ / 5;
 301		clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
 302		spin_unlock_bh(&sc->chan_lock);
 303		return;
 304	}
 305
 306	ictx->flush_timeout = usecs_to_jiffies(sc->sched.channel_switch_time);
 307
 308	if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags)) {
 309		spin_unlock_bh(&sc->chan_lock);
 310		return;
 311	}
 312
 313	spin_unlock_bh(&sc->chan_lock);
 314
 315	if (ath9k_is_chanctx_enabled()) {
 316		ath_chanctx_event(sc, NULL,
 317				  ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
 318	}
 319}
 320
 321static struct ath_chanctx *
 322ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
 323{
 324	int idx = ctx - &sc->chanctx[0];
 325
 326	return &sc->chanctx[!idx];
 327}
 328
 329static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
 330{
 331	struct ath_chanctx *prev, *cur;
 332	u32 cur_tsf, prev_tsf, beacon_int;
 333	ktime_t ts;
 334	s32 offset;
 335
 336	beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
 337
 338	cur = sc->cur_chan;
 339	prev = ath_chanctx_get_next(sc, cur);
 340
 341	if (!prev->switch_after_beacon)
 342		return;
 343
 344	ts = ktime_get_raw();
 345	cur_tsf = (u32) cur->tsf_val +
 346		  ath9k_hw_get_tsf_offset(cur->tsf_ts, ts);
 347
 348	prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
 349	prev_tsf -= ath9k_hw_get_tsf_offset(prev->tsf_ts, ts);
 350
 351	/* Adjust the TSF time of the AP chanctx to keep its beacons
 352	 * at half beacon interval offset relative to the STA chanctx.
 353	 */
 354	offset = cur_tsf - prev_tsf;
 355
 356	/* Ignore stale data or spurious timestamps */
 357	if (offset < 0 || offset > 3 * beacon_int)
 358		return;
 359
 360	offset = beacon_int / 2 - (offset % beacon_int);
 361	prev->tsf_val += offset;
 362}
 363
 364/* Configure the TSF based hardware timer for a channel switch.
 365 * Also set up backup software timer, in case the gen timer fails.
 366 * This could be caused by a hardware reset.
 367 */
 368static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
 369{
 370	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 371	struct ath_hw *ah = sc->sc_ah;
 372	unsigned long timeout;
 373
 374	ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
 375	tsf_time -= ath9k_hw_gettsf32(ah);
 376	timeout = msecs_to_jiffies(tsf_time / 1000) + 1;
 377	mod_timer(&sc->sched.timer, jiffies + timeout);
 378
 379	ath_dbg(common, CHAN_CTX,
 380		"Setup chanctx timer with timeout: %d (%d) ms\n",
 381		tsf_time / 1000, jiffies_to_msecs(timeout));
 382}
 383
 384static void ath_chanctx_handle_bmiss(struct ath_softc *sc,
 385				     struct ath_chanctx *ctx,
 386				     struct ath_vif *avp)
 387{
 388	/*
 389	 * Clear the extend_absence flag if it had been
 390	 * set during the previous beacon transmission,
 391	 * since we need to revert to the normal NoA
 392	 * schedule.
 393	 */
 394	if (ctx->active && sc->sched.extend_absence) {
 395		avp->noa_duration = 0;
 396		sc->sched.extend_absence = false;
 397	}
 398
 399	/* If at least two consecutive beacons were missed on the STA
 400	 * chanctx, stay on the STA channel for one extra beacon period,
 401	 * to resync the timer properly.
 402	 */
 403	if (ctx->active && sc->sched.beacon_miss >= 2) {
 404		avp->noa_duration = 0;
 405		sc->sched.extend_absence = true;
 406	}
 407}
 408
 409static void ath_chanctx_offchannel_noa(struct ath_softc *sc,
 410				       struct ath_chanctx *ctx,
 411				       struct ath_vif *avp,
 412				       u32 tsf_time)
 413{
 414	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 415
 416	avp->noa_index++;
 417	avp->offchannel_start = tsf_time;
 418	avp->offchannel_duration = sc->sched.offchannel_duration;
 419
 420	ath_dbg(common, CHAN_CTX,
 421		"offchannel noa_duration: %d, noa_start: %u, noa_index: %d\n",
 422		avp->offchannel_duration,
 423		avp->offchannel_start,
 424		avp->noa_index);
 425
 426	/*
 427	 * When multiple contexts are active, the NoA
 428	 * has to be recalculated and advertised after
 429	 * an offchannel operation.
 430	 */
 431	if (ctx->active && avp->noa_duration)
 432		avp->noa_duration = 0;
 433}
 434
 435static void ath_chanctx_set_periodic_noa(struct ath_softc *sc,
 436					 struct ath_vif *avp,
 437					 struct ath_beacon_config *cur_conf,
 438					 u32 tsf_time,
 439					 u32 beacon_int)
 440{
 441	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 442
 443	avp->noa_index++;
 444	avp->noa_start = tsf_time;
 445
 446	if (sc->sched.extend_absence)
 447		avp->noa_duration = (3 * beacon_int / 2) +
 448			sc->sched.channel_switch_time;
 449	else
 450		avp->noa_duration =
 451			TU_TO_USEC(cur_conf->beacon_interval) / 2 +
 452			sc->sched.channel_switch_time;
 453
 454	if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
 455	    sc->sched.extend_absence)
 456		avp->periodic_noa = false;
 457	else
 458		avp->periodic_noa = true;
 459
 460	ath_dbg(common, CHAN_CTX,
 461		"noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
 462		avp->noa_duration,
 463		avp->noa_start,
 464		avp->noa_index,
 465		avp->periodic_noa);
 466}
 467
 468static void ath_chanctx_set_oneshot_noa(struct ath_softc *sc,
 469					struct ath_vif *avp,
 470					u32 tsf_time,
 471					u32 duration)
 472{
 473	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 474
 475	avp->noa_index++;
 476	avp->noa_start = tsf_time;
 477	avp->periodic_noa = false;
 478	avp->oneshot_noa = true;
 479	avp->noa_duration = duration + sc->sched.channel_switch_time;
 480
 481	ath_dbg(common, CHAN_CTX,
 482		"oneshot noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
 483		avp->noa_duration,
 484		avp->noa_start,
 485		avp->noa_index,
 486		avp->periodic_noa);
 487}
 488
 489void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
 490		       enum ath_chanctx_event ev)
 491{
 492	struct ath_hw *ah = sc->sc_ah;
 493	struct ath_common *common = ath9k_hw_common(ah);
 494	struct ath_beacon_config *cur_conf;
 495	struct ath_vif *avp = NULL;
 496	struct ath_chanctx *ctx;
 497	u32 tsf_time;
 498	u32 beacon_int;
 499
 500	if (vif)
 501		avp = (struct ath_vif *) vif->drv_priv;
 502
 503	spin_lock_bh(&sc->chan_lock);
 504
 505	ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s, delta: %u ms\n",
 506		sc->cur_chan->chandef.center_freq1,
 507		chanctx_event_string(ev),
 508		chanctx_state_string(sc->sched.state),
 509		chanctx_event_delta(sc));
 510
 511	switch (ev) {
 512	case ATH_CHANCTX_EVENT_BEACON_PREPARE:
 513		if (avp->offchannel_duration)
 514			avp->offchannel_duration = 0;
 515
 516		if (avp->oneshot_noa) {
 517			avp->noa_duration = 0;
 518			avp->oneshot_noa = false;
 519
 520			ath_dbg(common, CHAN_CTX,
 521				"Clearing oneshot NoA\n");
 522		}
 523
 524		if (avp->chanctx != sc->cur_chan) {
 525			ath_dbg(common, CHAN_CTX,
 526				"Contexts differ, not preparing beacon\n");
 527			break;
 528		}
 529
 530		if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
 531			sc->sched.offchannel_pending = false;
 532			sc->next_chan = &sc->offchannel.chan;
 533			sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
 534			ath_dbg(common, CHAN_CTX,
 535				"Setting offchannel_pending to false\n");
 536		}
 537
 538		ctx = ath_chanctx_get_next(sc, sc->cur_chan);
 539		if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
 540			sc->next_chan = ctx;
 541			sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
 542			ath_dbg(common, CHAN_CTX,
 543				"Set next context, move chanctx state to WAIT_FOR_BEACON\n");
 544		}
 545
 546		/* if the timer missed its window, use the next interval */
 547		if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
 548			sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
 549			ath_dbg(common, CHAN_CTX,
 550				"Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
 551		}
 552
 553		if (sc->sched.mgd_prepare_tx)
 554			sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
 555
 556		/*
 557		 * When a context becomes inactive, for example,
 558		 * disassociation of a station context, the NoA
 559		 * attribute needs to be removed from subsequent
 560		 * beacons.
 561		 */
 562		if (!ctx->active && avp->noa_duration &&
 563		    sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) {
 564			avp->noa_duration = 0;
 565			avp->periodic_noa = false;
 566
 567			ath_dbg(common, CHAN_CTX,
 568				"Clearing NoA schedule\n");
 569		}
 570
 571		if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
 572			break;
 573
 574		ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
 575
 576		sc->sched.beacon_pending = true;
 577		sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
 578
 579		cur_conf = &sc->cur_chan->beacon;
 580		beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
 581
 582		/* defer channel switch by a quarter beacon interval */
 583		tsf_time = sc->sched.next_tbtt + beacon_int / 4;
 584		sc->sched.switch_start_time = tsf_time;
 585		sc->cur_chan->last_beacon = sc->sched.next_tbtt;
 586
 587		/*
 588		 * If an offchannel switch is scheduled to happen after
 589		 * a beacon transmission, update the NoA with one-shot
 590		 * values and increment the index.
 591		 */
 592		if (sc->next_chan == &sc->offchannel.chan) {
 593			ath_chanctx_offchannel_noa(sc, ctx, avp, tsf_time);
 594			break;
 595		}
 596
 597		ath_chanctx_handle_bmiss(sc, ctx, avp);
 598
 599		/*
 600		 * If a mgd_prepare_tx() has been called by mac80211,
 601		 * a one-shot NoA needs to be sent. This can happen
 602		 * with one or more active channel contexts - in both
 603		 * cases, a new NoA schedule has to be advertised.
 604		 */
 605		if (sc->sched.mgd_prepare_tx) {
 606			ath_chanctx_set_oneshot_noa(sc, avp, tsf_time,
 607						    jiffies_to_usecs(HZ / 5));
 608			break;
 609		}
 610
 611		/* Prevent wrap-around issues */
 612		if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
 613			avp->noa_duration = 0;
 614
 615		/*
 616		 * If multiple contexts are active, start periodic
 617		 * NoA and increment the index for the first
 618		 * announcement.
 619		 */
 620		if (ctx->active &&
 621		    (!avp->noa_duration || sc->sched.force_noa_update))
 622			ath_chanctx_set_periodic_noa(sc, avp, cur_conf,
 623						     tsf_time, beacon_int);
 624
 625		if (ctx->active && sc->sched.force_noa_update)
 626			sc->sched.force_noa_update = false;
 627
 628		break;
 629	case ATH_CHANCTX_EVENT_BEACON_SENT:
 630		if (!sc->sched.beacon_pending) {
 631			ath_dbg(common, CHAN_CTX,
 632				"No pending beacon\n");
 633			break;
 634		}
 635
 636		sc->sched.beacon_pending = false;
 637
 638		if (sc->sched.mgd_prepare_tx) {
 639			sc->sched.mgd_prepare_tx = false;
 640			complete(&sc->go_beacon);
 641			ath_dbg(common, CHAN_CTX,
 642				"Beacon sent, complete go_beacon\n");
 643			break;
 644		}
 645
 646		if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
 647			break;
 648
 649		ath_dbg(common, CHAN_CTX,
 650			"Move chanctx state to WAIT_FOR_TIMER\n");
 651
 652		sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
 653		ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
 654		break;
 655	case ATH_CHANCTX_EVENT_TSF_TIMER:
 656		if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
 657			break;
 658
 659		if (!sc->cur_chan->switch_after_beacon &&
 660		    sc->sched.beacon_pending)
 661			sc->sched.beacon_miss++;
 662
 663		ath_dbg(common, CHAN_CTX,
 664			"Move chanctx state to SWITCH\n");
 665
 666		sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
 667		ieee80211_queue_work(sc->hw, &sc->chanctx_work);
 668		break;
 669	case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
 670		if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
 671		    sc->cur_chan == &sc->offchannel.chan)
 672			break;
 673
 674		sc->sched.beacon_pending = false;
 675		sc->sched.beacon_miss = 0;
 676
 677		if (sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
 678		    !sc->sched.beacon_adjust ||
 679		    !sc->cur_chan->tsf_val)
 680			break;
 681
 682		ath_chanctx_adjust_tbtt_delta(sc);
 683
 684		/* TSF time might have been updated by the incoming beacon,
 685		 * need update the channel switch timer to reflect the change.
 686		 */
 687		tsf_time = sc->sched.switch_start_time;
 688		tsf_time -= (u32) sc->cur_chan->tsf_val +
 689			ath9k_hw_get_tsf_offset(sc->cur_chan->tsf_ts, 0);
 690		tsf_time += ath9k_hw_gettsf32(ah);
 691
 692		sc->sched.beacon_adjust = false;
 693		ath_chanctx_setup_timer(sc, tsf_time);
 694		break;
 695	case ATH_CHANCTX_EVENT_AUTHORIZED:
 696		if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
 697		    avp->chanctx != sc->cur_chan)
 698			break;
 699
 700		ath_dbg(common, CHAN_CTX,
 701			"Move chanctx state from FORCE_ACTIVE to IDLE\n");
 702
 703		sc->sched.state = ATH_CHANCTX_STATE_IDLE;
 704		fallthrough;
 705	case ATH_CHANCTX_EVENT_SWITCH:
 706		if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
 707		    sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
 708		    sc->cur_chan->switch_after_beacon ||
 709		    sc->cur_chan == &sc->offchannel.chan)
 710			break;
 711
 712		/* If this is a station chanctx, stay active for a half
 713		 * beacon period (minus channel switch time)
 714		 */
 715		sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
 716		cur_conf = &sc->cur_chan->beacon;
 717
 718		ath_dbg(common, CHAN_CTX,
 719			"Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
 720
 721		sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
 722		sc->sched.wait_switch = false;
 723
 724		tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
 725
 726		if (sc->sched.extend_absence) {
 727			sc->sched.beacon_miss = 0;
 728			tsf_time *= 3;
 729		}
 730
 731		tsf_time -= sc->sched.channel_switch_time;
 732		tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
 733		sc->sched.switch_start_time = tsf_time;
 734
 735		ath_chanctx_setup_timer(sc, tsf_time);
 736		sc->sched.beacon_pending = true;
 737		sc->sched.beacon_adjust = true;
 738		break;
 739	case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
 740		if (sc->cur_chan == &sc->offchannel.chan ||
 741		    sc->cur_chan->switch_after_beacon)
 742			break;
 743
 744		sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
 745		ieee80211_queue_work(sc->hw, &sc->chanctx_work);
 746		break;
 747	case ATH_CHANCTX_EVENT_UNASSIGN:
 748		if (sc->cur_chan->assigned) {
 749			if (sc->next_chan && !sc->next_chan->assigned &&
 750			    sc->next_chan != &sc->offchannel.chan)
 751				sc->sched.state = ATH_CHANCTX_STATE_IDLE;
 752			break;
 753		}
 754
 755		ctx = ath_chanctx_get_next(sc, sc->cur_chan);
 756		sc->sched.state = ATH_CHANCTX_STATE_IDLE;
 757		if (!ctx->assigned)
 758			break;
 759
 760		sc->next_chan = ctx;
 761		ieee80211_queue_work(sc->hw, &sc->chanctx_work);
 762		break;
 763	case ATH_CHANCTX_EVENT_ASSIGN:
 764		break;
 765	case ATH_CHANCTX_EVENT_CHANGE:
 766		break;
 767	}
 768
 769	spin_unlock_bh(&sc->chan_lock);
 770}
 771
 772void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
 773				enum ath_chanctx_event ev)
 774{
 775	if (sc->sched.beacon_pending)
 776		ath_chanctx_event(sc, NULL, ev);
 777}
 778
 779void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
 780				enum ath_chanctx_event ev)
 781{
 782	ath_chanctx_event(sc, NULL, ev);
 783}
 784
 785static int ath_scan_channel_duration(struct ath_softc *sc,
 786				     struct ieee80211_channel *chan)
 787{
 788	struct cfg80211_scan_request *req = sc->offchannel.scan_req;
 789
 790	if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
 791		return (HZ / 9); /* ~110 ms */
 792
 793	return (HZ / 16); /* ~60 ms */
 794}
 795
 796static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
 797			       struct cfg80211_chan_def *chandef)
 798{
 799	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 800
 801	spin_lock_bh(&sc->chan_lock);
 802
 803	if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
 804	    (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
 805		if (chandef)
 806			ctx->chandef = *chandef;
 807
 808		sc->sched.offchannel_pending = true;
 809		sc->sched.wait_switch = true;
 810		sc->sched.offchannel_duration =
 811			jiffies_to_usecs(sc->offchannel.duration) +
 812			sc->sched.channel_switch_time;
 813
 814		spin_unlock_bh(&sc->chan_lock);
 815		ath_dbg(common, CHAN_CTX,
 816			"Set offchannel_pending to true\n");
 817		return;
 818	}
 819
 820	sc->next_chan = ctx;
 821	if (chandef) {
 822		ctx->chandef = *chandef;
 823		ath_dbg(common, CHAN_CTX,
 824			"Assigned next_chan to %d MHz\n", chandef->center_freq1);
 825	}
 826
 827	if (sc->next_chan == &sc->offchannel.chan) {
 828		sc->sched.offchannel_duration =
 829			jiffies_to_usecs(sc->offchannel.duration) +
 830			sc->sched.channel_switch_time;
 831
 832		if (chandef) {
 833			ath_dbg(common, CHAN_CTX,
 834				"Offchannel duration for chan %d MHz : %u\n",
 835				chandef->center_freq1,
 836				sc->sched.offchannel_duration);
 837		}
 838	}
 839	spin_unlock_bh(&sc->chan_lock);
 840	ieee80211_queue_work(sc->hw, &sc->chanctx_work);
 841}
 842
 843static void ath_chanctx_offchan_switch(struct ath_softc *sc,
 844				       struct ieee80211_channel *chan)
 845{
 846	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 847	struct cfg80211_chan_def chandef;
 848
 849	cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
 850	ath_dbg(common, CHAN_CTX,
 851		"Channel definition created: %d MHz\n", chandef.center_freq1);
 852
 853	ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
 854}
 855
 856static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
 857						     bool active)
 858{
 859	struct ath_chanctx *ctx;
 860
 861	ath_for_each_chanctx(sc, ctx) {
 862		if (!ctx->assigned || list_empty(&ctx->vifs))
 863			continue;
 864		if (active && !ctx->active)
 865			continue;
 866
 867		if (ctx->switch_after_beacon)
 868			return ctx;
 869	}
 870
 871	return &sc->chanctx[0];
 872}
 873
 874static void
 875ath_scan_next_channel(struct ath_softc *sc)
 876{
 877	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 878	struct cfg80211_scan_request *req = sc->offchannel.scan_req;
 879	struct ieee80211_channel *chan;
 880
 881	if (sc->offchannel.scan_idx >= req->n_channels) {
 882		ath_dbg(common, CHAN_CTX,
 883			"Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
 884			"scan_idx: %d, n_channels: %d\n",
 885			sc->offchannel.scan_idx,
 886			req->n_channels);
 887
 888		sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
 889		ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
 890				   NULL);
 891		return;
 892	}
 893
 894	ath_dbg(common, CHAN_CTX,
 895		"Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
 896		sc->offchannel.scan_idx);
 897
 898	chan = req->channels[sc->offchannel.scan_idx++];
 899	sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
 900	sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
 901
 902	ath_chanctx_offchan_switch(sc, chan);
 903}
 904
 905void ath_offchannel_next(struct ath_softc *sc)
 906{
 907	struct ieee80211_vif *vif;
 908
 909	if (sc->offchannel.scan_req) {
 910		vif = sc->offchannel.scan_vif;
 911		sc->offchannel.chan.txpower = vif->bss_conf.txpower;
 912		ath_scan_next_channel(sc);
 913	} else if (sc->offchannel.roc_vif) {
 914		vif = sc->offchannel.roc_vif;
 915		sc->offchannel.chan.txpower = vif->bss_conf.txpower;
 916		sc->offchannel.duration =
 917			msecs_to_jiffies(sc->offchannel.roc_duration);
 918		sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
 919		ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
 920	} else {
 921		spin_lock_bh(&sc->chan_lock);
 922		sc->sched.offchannel_pending = false;
 923		sc->sched.wait_switch = false;
 924		spin_unlock_bh(&sc->chan_lock);
 925
 926		ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
 927				   NULL);
 928		sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
 929		if (sc->ps_idle)
 930			ath_cancel_work(sc);
 931	}
 932}
 933
 934void ath_roc_complete(struct ath_softc *sc, enum ath_roc_complete_reason reason)
 935{
 936	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 937
 938	sc->offchannel.roc_vif = NULL;
 939	sc->offchannel.roc_chan = NULL;
 940
 941	switch (reason) {
 942	case ATH_ROC_COMPLETE_ABORT:
 943		ath_dbg(common, CHAN_CTX, "RoC aborted\n");
 944		ieee80211_remain_on_channel_expired(sc->hw);
 945		break;
 946	case ATH_ROC_COMPLETE_EXPIRE:
 947		ath_dbg(common, CHAN_CTX, "RoC expired\n");
 948		ieee80211_remain_on_channel_expired(sc->hw);
 949		break;
 950	case ATH_ROC_COMPLETE_CANCEL:
 951		ath_dbg(common, CHAN_CTX, "RoC canceled\n");
 952		break;
 953	}
 954
 955	ath_offchannel_next(sc);
 956	ath9k_ps_restore(sc);
 957}
 958
 959void ath_scan_complete(struct ath_softc *sc, bool abort)
 960{
 961	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 962	struct cfg80211_scan_info info = {
 963		.aborted = abort,
 964	};
 965
 966	if (abort)
 967		ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
 968	else
 969		ath_dbg(common, CHAN_CTX, "HW scan complete\n");
 970
 971	sc->offchannel.scan_req = NULL;
 972	sc->offchannel.scan_vif = NULL;
 973	sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
 974	ieee80211_scan_completed(sc->hw, &info);
 975	clear_bit(ATH_OP_SCANNING, &common->op_flags);
 976	spin_lock_bh(&sc->chan_lock);
 977	if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
 978		sc->sched.force_noa_update = true;
 979	spin_unlock_bh(&sc->chan_lock);
 980	ath_offchannel_next(sc);
 981	ath9k_ps_restore(sc);
 982}
 983
 984static void ath_scan_send_probe(struct ath_softc *sc,
 985				struct cfg80211_ssid *ssid)
 986{
 987	struct cfg80211_scan_request *req = sc->offchannel.scan_req;
 988	struct ieee80211_vif *vif = sc->offchannel.scan_vif;
 989	struct ath_tx_control txctl = {};
 990	struct sk_buff *skb;
 991	struct ieee80211_tx_info *info;
 992	int band = sc->offchannel.chan.chandef.chan->band;
 993
 994	skb = ieee80211_probereq_get(sc->hw, vif->addr,
 995			ssid->ssid, ssid->ssid_len, req->ie_len);
 996	if (!skb)
 997		return;
 998
 999	info = IEEE80211_SKB_CB(skb);
1000	if (req->no_cck)
1001		info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
1002
1003	if (req->ie_len)
1004		skb_put_data(skb, req->ie, req->ie_len);
1005
1006	skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1007
1008	if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
1009		return;
1010
1011	txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1012	if (ath_tx_start(sc->hw, skb, &txctl))
1013		goto error;
1014
1015	return;
1016
1017error:
1018	ieee80211_free_txskb(sc->hw, skb);
1019}
1020
1021static void ath_scan_channel_start(struct ath_softc *sc)
1022{
1023	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1024	struct cfg80211_scan_request *req = sc->offchannel.scan_req;
1025	int i;
1026
1027	if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
1028	    req->n_ssids) {
1029		for (i = 0; i < req->n_ssids; i++)
1030			ath_scan_send_probe(sc, &req->ssids[i]);
1031
1032	}
1033
1034	ath_dbg(common, CHAN_CTX,
1035		"Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
1036
1037	sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
1038	mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
1039}
1040
1041static void ath_chanctx_timer(struct timer_list *t)
1042{
1043	struct ath_softc *sc = timer_container_of(sc, t, sched.timer);
1044	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1045
1046	ath_dbg(common, CHAN_CTX,
1047		"Channel context timer invoked\n");
1048
1049	ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1050}
1051
1052static void ath_offchannel_timer(struct timer_list *t)
1053{
1054	struct ath_softc *sc = timer_container_of(sc, t, offchannel.timer);
1055	struct ath_chanctx *ctx;
1056	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1057
1058	ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
1059		__func__, offchannel_state_string(sc->offchannel.state));
1060
1061	switch (sc->offchannel.state) {
1062	case ATH_OFFCHANNEL_PROBE_WAIT:
1063		if (!sc->offchannel.scan_req)
1064			return;
1065
1066		/* get first active channel context */
1067		ctx = ath_chanctx_get_oper_chan(sc, true);
1068		if (ctx->active) {
1069			ath_dbg(common, CHAN_CTX,
1070				"Switch to oper/active context, "
1071				"move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
1072
1073			sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
1074			ath_chanctx_switch(sc, ctx, NULL);
1075			mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
1076			break;
1077		}
1078		fallthrough;
1079	case ATH_OFFCHANNEL_SUSPEND:
1080		if (!sc->offchannel.scan_req)
1081			return;
1082
1083		ath_scan_next_channel(sc);
1084		break;
1085	case ATH_OFFCHANNEL_ROC_START:
1086	case ATH_OFFCHANNEL_ROC_WAIT:
1087		sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
1088		ath_roc_complete(sc, ATH_ROC_COMPLETE_EXPIRE);
1089		break;
1090	default:
1091		break;
1092	}
1093}
1094
1095static bool
1096ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
1097			      bool powersave)
1098{
1099	struct ieee80211_vif *vif = avp->vif;
1100	struct ieee80211_sta *sta = NULL;
1101	struct ieee80211_hdr_3addr *nullfunc;
1102	struct ath_tx_control txctl;
1103	struct sk_buff *skb;
1104	int band = sc->cur_chan->chandef.chan->band;
1105
1106	switch (vif->type) {
1107	case NL80211_IFTYPE_STATION:
1108		if (!avp->assoc)
1109			return false;
1110
1111		skb = ieee80211_nullfunc_get(sc->hw, vif, -1, false);
1112		if (!skb)
1113			return false;
1114
1115		nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1116		if (powersave)
1117			nullfunc->frame_control |=
1118				cpu_to_le16(IEEE80211_FCTL_PM);
1119
1120		skb->priority = 7;
1121		skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1122		if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta))
1123			return false;
1124		break;
1125	default:
1126		return false;
1127	}
1128
1129	memset(&txctl, 0, sizeof(txctl));
1130	txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1131	txctl.sta = sta;
1132	if (ath_tx_start(sc->hw, skb, &txctl)) {
1133		ieee80211_free_txskb(sc->hw, skb);
1134		return false;
1135	}
1136
1137	return true;
1138}
1139
1140static bool
1141ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
1142{
1143	struct ath_vif *avp;
1144	bool sent = false;
1145
1146	rcu_read_lock();
1147	list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
1148		if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
1149			sent = true;
1150	}
1151	rcu_read_unlock();
1152
1153	return sent;
1154}
1155
1156static bool ath_chanctx_defer_switch(struct ath_softc *sc)
1157{
1158	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1159
1160	if (sc->cur_chan == &sc->offchannel.chan)
1161		return false;
1162
1163	switch (sc->sched.state) {
1164	case ATH_CHANCTX_STATE_SWITCH:
1165		return false;
1166	case ATH_CHANCTX_STATE_IDLE:
1167		if (!sc->cur_chan->switch_after_beacon)
1168			return false;
1169
1170		ath_dbg(common, CHAN_CTX,
1171			"Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1172
1173		sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
1174		break;
1175	default:
1176		break;
1177	}
1178
1179	return true;
1180}
1181
1182static void ath_offchannel_channel_change(struct ath_softc *sc)
1183{
1184	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1185
1186	ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
1187		__func__, offchannel_state_string(sc->offchannel.state));
1188
1189	switch (sc->offchannel.state) {
1190	case ATH_OFFCHANNEL_PROBE_SEND:
1191		if (!sc->offchannel.scan_req)
1192			return;
1193
1194		if (sc->cur_chan->chandef.chan !=
1195		    sc->offchannel.chan.chandef.chan)
1196			return;
1197
1198		ath_scan_channel_start(sc);
1199		break;
1200	case ATH_OFFCHANNEL_IDLE:
1201		if (!sc->offchannel.scan_req)
1202			return;
1203
1204		ath_scan_complete(sc, false);
1205		break;
1206	case ATH_OFFCHANNEL_ROC_START:
1207		if (sc->cur_chan != &sc->offchannel.chan)
1208			break;
1209
1210		sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
1211		mod_timer(&sc->offchannel.timer,
1212			  jiffies + sc->offchannel.duration);
1213		ieee80211_ready_on_channel(sc->hw);
1214		break;
1215	case ATH_OFFCHANNEL_ROC_DONE:
1216		break;
1217	default:
1218		break;
1219	}
1220}
1221
1222void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1223{
1224	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1225	struct ath_chanctx *old_ctx;
1226	bool measure_time = false;
1227	bool send_ps = false;
1228	bool queues_stopped = false;
1229	ktime_t ts;
1230
1231	spin_lock_bh(&sc->chan_lock);
1232	if (!sc->next_chan) {
1233		spin_unlock_bh(&sc->chan_lock);
1234		return;
1235	}
1236
1237	if (!force && ath_chanctx_defer_switch(sc)) {
1238		spin_unlock_bh(&sc->chan_lock);
1239		return;
1240	}
1241
1242	ath_dbg(common, CHAN_CTX,
1243		"%s: current: %d MHz, next: %d MHz\n",
1244		__func__,
1245		sc->cur_chan->chandef.center_freq1,
1246		sc->next_chan->chandef.center_freq1);
1247
1248	if (sc->cur_chan != sc->next_chan) {
1249		ath_dbg(common, CHAN_CTX,
1250			"Stopping current chanctx: %d\n",
1251			sc->cur_chan->chandef.center_freq1);
1252		sc->cur_chan->stopped = true;
1253		spin_unlock_bh(&sc->chan_lock);
1254
1255		if (sc->next_chan == &sc->offchannel.chan) {
1256			ts = ktime_get_raw();
1257			measure_time = true;
1258		}
1259
1260		ath9k_chanctx_stop_queues(sc, sc->cur_chan);
1261		queues_stopped = true;
1262
1263		__ath9k_flush(sc->hw, ~0, true, false, false);
1264
1265		if (ath_chanctx_send_ps_frame(sc, true))
1266			__ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
1267				      false, false, false);
1268
1269		send_ps = true;
1270		spin_lock_bh(&sc->chan_lock);
1271
1272		if (sc->cur_chan != &sc->offchannel.chan) {
1273			sc->cur_chan->tsf_ts = ktime_get_raw();
1274			sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1275		}
1276	}
1277	old_ctx = sc->cur_chan;
1278	sc->cur_chan = sc->next_chan;
1279	sc->cur_chan->stopped = false;
1280	sc->next_chan = NULL;
1281
1282	if (!sc->sched.offchannel_pending)
1283		sc->sched.offchannel_duration = 0;
1284
1285	if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1286		sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1287
1288	spin_unlock_bh(&sc->chan_lock);
1289
1290	if (sc->sc_ah->chip_fullsleep ||
1291	    memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1292		   sizeof(sc->cur_chandef))) {
1293		ath_dbg(common, CHAN_CTX,
1294			"%s: Set channel %d MHz\n",
1295			__func__, sc->cur_chan->chandef.center_freq1);
1296		ath_set_channel(sc);
1297		if (measure_time)
1298			sc->sched.channel_switch_time =
1299				ath9k_hw_get_tsf_offset(ts, 0);
1300		/*
1301		 * A reset will ensure that all queues are woken up,
1302		 * so there is no need to awaken them again.
1303		 */
1304		goto out;
1305	}
1306
1307	if (queues_stopped)
1308		ath9k_chanctx_wake_queues(sc, old_ctx);
1309out:
1310	if (send_ps)
1311		ath_chanctx_send_ps_frame(sc, false);
1312
1313	ath_offchannel_channel_change(sc);
1314	ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1315}
1316
1317static void ath_chanctx_work(struct work_struct *work)
1318{
1319	struct ath_softc *sc = container_of(work, struct ath_softc,
1320					    chanctx_work);
1321	mutex_lock(&sc->mutex);
1322	ath_chanctx_set_next(sc, false);
1323	mutex_unlock(&sc->mutex);
1324}
1325
1326void ath9k_offchannel_init(struct ath_softc *sc)
1327{
1328	struct ath_chanctx *ctx;
1329	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1330	struct ieee80211_supported_band *sband;
1331	struct ieee80211_channel *chan;
1332	int i;
1333
1334	sband = &common->sbands[NL80211_BAND_2GHZ];
1335	if (!sband->n_channels)
1336		sband = &common->sbands[NL80211_BAND_5GHZ];
1337
1338	chan = &sband->channels[0];
1339
1340	ctx = &sc->offchannel.chan;
1341	INIT_LIST_HEAD(&ctx->vifs);
1342	ctx->txpower = ATH_TXPOWER_MAX;
1343	cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1344
1345	for (i = 0; i < ARRAY_SIZE(ctx->acq); i++) {
1346		INIT_LIST_HEAD(&ctx->acq[i].acq_new);
1347		INIT_LIST_HEAD(&ctx->acq[i].acq_old);
1348		spin_lock_init(&ctx->acq[i].lock);
1349	}
1350
1351	sc->offchannel.chan.offchannel = true;
1352}
1353
1354void ath9k_init_channel_context(struct ath_softc *sc)
1355{
1356	INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1357
1358	timer_setup(&sc->offchannel.timer, ath_offchannel_timer, 0);
1359	timer_setup(&sc->sched.timer, ath_chanctx_timer, 0);
1360
1361	init_completion(&sc->go_beacon);
1362}
1363
1364void ath9k_deinit_channel_context(struct ath_softc *sc)
1365{
1366	cancel_work_sync(&sc->chanctx_work);
1367}
1368
1369bool ath9k_is_chanctx_enabled(void)
1370{
1371	return (ath9k_use_chanctx == 1);
1372}
1373
1374/********************/
1375/* Queue management */
1376/********************/
1377
1378void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1379{
1380	struct ath_hw *ah = sc->sc_ah;
1381	int i;
1382
1383	if (ctx == &sc->offchannel.chan) {
1384		ieee80211_stop_queue(sc->hw,
1385				     sc->hw->offchannel_tx_hw_queue);
1386	} else {
1387		for (i = 0; i < IEEE80211_NUM_ACS; i++)
1388			ieee80211_stop_queue(sc->hw,
1389					     ctx->hw_queue_base + i);
1390	}
1391
1392	if (ah->opmode == NL80211_IFTYPE_AP)
1393		ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
1394}
1395
1396
1397void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1398{
1399	struct ath_hw *ah = sc->sc_ah;
1400	int i;
1401
1402	if (ctx == &sc->offchannel.chan) {
1403		ieee80211_wake_queue(sc->hw,
1404				     sc->hw->offchannel_tx_hw_queue);
1405	} else {
1406		for (i = 0; i < IEEE80211_NUM_ACS; i++)
1407			ieee80211_wake_queue(sc->hw,
1408					     ctx->hw_queue_base + i);
1409	}
1410
1411	if (ah->opmode == NL80211_IFTYPE_AP)
1412		ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1413}
1414
1415/*****************/
1416/* P2P Powersave */
1417/*****************/
1418
1419static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
1420{
1421	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1422	struct ath_hw *ah = sc->sc_ah;
1423	u32 tsf, target_tsf;
1424
1425	if (!avp || !avp->noa.has_next_tsf)
1426		return;
1427
1428	ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1429
1430	tsf = ath9k_hw_gettsf32(sc->sc_ah);
1431
1432	target_tsf = avp->noa.next_tsf;
1433	if (!avp->noa.absent)
1434		target_tsf -= ATH_P2P_PS_STOP_TIME;
1435	else
1436		target_tsf += ATH_P2P_PS_STOP_TIME;
1437
1438	if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1439		target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1440
1441	ath_dbg(common, CHAN_CTX, "%s absent %d tsf 0x%08X next_tsf 0x%08X (%dms)\n",
1442		__func__, avp->noa.absent, tsf, target_tsf,
1443		(target_tsf - tsf) / 1000);
1444
1445	ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, target_tsf, 1000000);
1446}
1447
1448static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1449{
1450	struct ath_vif *avp = (void *)vif->drv_priv;
1451	u32 tsf;
1452
1453	if (!sc->p2p_ps_timer)
1454		return;
1455
1456	if (vif->type != NL80211_IFTYPE_STATION)
1457		return;
1458
1459	sc->p2p_ps_vif = avp;
1460
1461	if (sc->ps_flags & PS_BEACON_SYNC)
1462		return;
1463
1464	tsf = ath9k_hw_gettsf32(sc->sc_ah);
1465	ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1466	ath9k_update_p2p_ps_timer(sc, avp);
1467}
1468
1469static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1470{
1471	struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1472	u8 switch_time, ctwin;
1473
1474	/*
1475	 * Channel switch in multi-channel mode is deferred
1476	 * by a quarter beacon interval when handling
1477	 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1478	 * interface is guaranteed to be discoverable
1479	 * for that duration after a TBTT.
1480	 */
1481	switch_time = cur_conf->beacon_interval / 4;
1482
1483	ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1484	if (ctwin && (ctwin < switch_time))
1485		return ctwin;
1486
1487	if (switch_time < P2P_DEFAULT_CTWIN)
1488		return 0;
1489
1490	return P2P_DEFAULT_CTWIN;
1491}
1492
1493void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1494			  struct sk_buff *skb)
1495{
1496	static const u8 noa_ie_hdr[] = {
1497		WLAN_EID_VENDOR_SPECIFIC,	/* type */
1498		0,				/* length */
1499		0x50, 0x6f, 0x9a,		/* WFA OUI */
1500		0x09,				/* P2P subtype */
1501		0x0c,				/* Notice of Absence */
1502		0x00,				/* LSB of little-endian len */
1503		0x00,				/* MSB of little-endian len */
1504	};
1505
1506	struct ieee80211_p2p_noa_attr *noa;
1507	int noa_len, noa_desc, i = 0;
1508	u8 *hdr;
1509
1510	if (!avp->offchannel_duration && !avp->noa_duration)
1511		return;
1512
1513	noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
1514	noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1515
1516	hdr = skb_put_data(skb, noa_ie_hdr, sizeof(noa_ie_hdr));
1517	hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1518	hdr[7] = noa_len;
1519
1520	noa = skb_put_zero(skb, noa_len);
1521
1522	noa->index = avp->noa_index;
1523	noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
1524	if (noa->oppps_ctwindow)
1525		noa->oppps_ctwindow |= BIT(7);
1526
1527	if (avp->noa_duration) {
1528		if (avp->periodic_noa) {
1529			u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1530			noa->desc[i].count = 255;
1531			noa->desc[i].interval = cpu_to_le32(interval);
1532		} else {
1533			noa->desc[i].count = 1;
1534		}
1535
1536		noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1537		noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
1538		i++;
1539	}
1540
1541	if (avp->offchannel_duration) {
1542		noa->desc[i].count = 1;
1543		noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1544		noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1545	}
1546}
1547
1548void ath9k_p2p_ps_timer(void *priv)
1549{
1550	struct ath_softc *sc = priv;
1551	struct ath_vif *avp = sc->p2p_ps_vif;
1552	struct ieee80211_vif *vif;
1553	struct ieee80211_sta *sta;
1554	struct ath_node *an;
1555	u32 tsf;
1556
1557	timer_delete_sync(&sc->sched.timer);
1558	ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1559	ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1560
1561	if (!avp || avp->chanctx != sc->cur_chan)
1562		return;
1563
1564	tsf = ath9k_hw_gettsf32(sc->sc_ah);
1565	if (!avp->noa.absent)
1566		tsf += ATH_P2P_PS_STOP_TIME;
1567	else
1568		tsf -= ATH_P2P_PS_STOP_TIME;
1569
1570	if (!avp->noa.has_next_tsf ||
1571	    avp->noa.next_tsf - tsf > BIT(31))
1572		ieee80211_update_p2p_noa(&avp->noa, tsf);
1573
1574	ath9k_update_p2p_ps_timer(sc, avp);
1575
1576	rcu_read_lock();
1577
1578	vif = avp->vif;
1579	sta = ieee80211_find_sta(vif, avp->bssid);
1580	if (!sta)
1581		goto out;
1582
1583	an = (void *) sta->drv_priv;
1584	if (an->sleeping == !!avp->noa.absent)
1585		goto out;
1586
1587	an->sleeping = avp->noa.absent;
1588	if (an->sleeping)
1589		ath_tx_aggr_sleep(sta, sc, an);
1590	else
1591		ath_tx_aggr_wakeup(sc, an);
1592
1593out:
1594	rcu_read_unlock();
1595}
1596
1597void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1598				struct ieee80211_vif *vif)
1599{
1600	unsigned long flags;
1601
1602	spin_lock_bh(&sc->sc_pcu_lock);
1603	spin_lock_irqsave(&sc->sc_pm_lock, flags);
1604	ath9k_update_p2p_ps(sc, vif);
1605	spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1606	spin_unlock_bh(&sc->sc_pcu_lock);
1607}
1608
1609void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1610{
1611	if (sc->p2p_ps_vif)
1612		ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1613}
1614
1615void ath9k_p2p_remove_vif(struct ath_softc *sc,
1616			  struct ieee80211_vif *vif)
1617{
1618	struct ath_vif *avp = (void *)vif->drv_priv;
1619
1620	spin_lock_bh(&sc->sc_pcu_lock);
1621	if (avp == sc->p2p_ps_vif) {
1622		sc->p2p_ps_vif = NULL;
1623		ath9k_update_p2p_ps_timer(sc, NULL);
1624	}
1625	spin_unlock_bh(&sc->sc_pcu_lock);
1626}
1627
1628int ath9k_init_p2p(struct ath_softc *sc)
1629{
1630	sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1631					       NULL, sc, AR_FIRST_NDP_TIMER);
1632	if (!sc->p2p_ps_timer)
1633		return -ENOMEM;
1634
1635	return 0;
1636}
1637
1638void ath9k_deinit_p2p(struct ath_softc *sc)
1639{
1640	if (sc->p2p_ps_timer)
1641		ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1642}
1643
1644#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */
~/ Read-only file preview.