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