regulatory: fix world regulatory domain data
[linux-2.6-block.git] / net / mac80211 / offchannel.c
CommitLineData
b203ffc3
JM
1/*
2 * Off-channel operation helpers
3 *
4 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
5 * Copyright 2004, Instant802 Networks, Inc.
6 * Copyright 2005, Devicescape Software, Inc.
7 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
8 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
9 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
bc3b2d7f 15#include <linux/export.h>
b203ffc3
JM
16#include <net/mac80211.h>
17#include "ieee80211_i.h"
2eb278e0 18#include "driver-ops.h"
b203ffc3
JM
19
20/*
b23b025f
BG
21 * Tell our hardware to disable PS.
22 * Optionally inform AP that we will go to sleep so that it will buffer
23 * the frames while we are doing off-channel work. This is optional
24 * because we *may* be doing work on-operating channel, and want our
25 * hardware unconditionally awake, but still let the AP send us normal frames.
b203ffc3 26 */
559cef99 27static void ieee80211_offchannel_ps_enable(struct ieee80211_sub_if_data *sdata)
b203ffc3
JM
28{
29 struct ieee80211_local *local = sdata->local;
4730d597 30 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
b203ffc3
JM
31
32 local->offchannel_ps_enabled = false;
33
34 /* FIXME: what to do when local->pspolling is true? */
35
36 del_timer_sync(&local->dynamic_ps_timer);
3bc3c0d7 37 del_timer_sync(&ifmgd->bcn_mon_timer);
4730d597
LR
38 del_timer_sync(&ifmgd->conn_mon_timer);
39
b203ffc3
JM
40 cancel_work_sync(&local->dynamic_ps_enable_work);
41
42 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
43 local->offchannel_ps_enabled = true;
44 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
45 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
46 }
47
559cef99 48 if (!local->offchannel_ps_enabled ||
30686bf7 49 !ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
b203ffc3
JM
50 /*
51 * If power save was enabled, no need to send a nullfunc
52 * frame because AP knows that we are sleeping. But if the
53 * hardware is creating the nullfunc frame for power save
54 * status (ie. IEEE80211_HW_PS_NULLFUNC_STACK is not
55 * enabled) and power save was enabled, the firmware just
56 * sent a null frame with power save disabled. So we need
57 * to send a new nullfunc frame to inform the AP that we
58 * are again sleeping.
59 */
076cdcb1 60 ieee80211_send_nullfunc(local, sdata, true);
b203ffc3
JM
61}
62
63/* inform AP that we are awake again, unless power save is enabled */
64static void ieee80211_offchannel_ps_disable(struct ieee80211_sub_if_data *sdata)
65{
66 struct ieee80211_local *local = sdata->local;
67
68 if (!local->ps_sdata)
076cdcb1 69 ieee80211_send_nullfunc(local, sdata, false);
b203ffc3
JM
70 else if (local->offchannel_ps_enabled) {
71 /*
72 * In !IEEE80211_HW_PS_NULLFUNC_STACK case the hardware
73 * will send a nullfunc frame with the powersave bit set
74 * even though the AP already knows that we are sleeping.
75 * This could be avoided by sending a null frame with power
76 * save bit disabled before enabling the power save, but
77 * this doesn't gain anything.
78 *
79 * When IEEE80211_HW_PS_NULLFUNC_STACK is enabled, no need
80 * to send a nullfunc frame because AP already knows that
81 * we are sleeping, let's just enable power save mode in
82 * hardware.
83 */
b23b025f
BG
84 /* TODO: Only set hardware if CONF_PS changed?
85 * TODO: Should we set offchannel_ps_enabled to false?
86 */
b203ffc3
JM
87 local->hw.conf.flags |= IEEE80211_CONF_PS;
88 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
89 } else if (local->hw.conf.dynamic_ps_timeout > 0) {
90 /*
91 * If IEEE80211_CONF_PS was not set and the dynamic_ps_timer
92 * had been running before leaving the operating channel,
93 * restart the timer now and send a nullfunc frame to inform
94 * the AP that we are awake.
95 */
076cdcb1 96 ieee80211_send_nullfunc(local, sdata, false);
b203ffc3
JM
97 mod_timer(&local->dynamic_ps_timer, jiffies +
98 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
99 }
4730d597 100
3bc3c0d7 101 ieee80211_sta_reset_beacon_monitor(sdata);
4730d597 102 ieee80211_sta_reset_conn_monitor(sdata);
b203ffc3
JM
103}
104
aacde9ee 105void ieee80211_offchannel_stop_vifs(struct ieee80211_local *local)
b203ffc3
JM
106{
107 struct ieee80211_sub_if_data *sdata;
108
fe57d9f5
JB
109 if (WARN_ON(local->use_chanctx))
110 return;
111
b23b025f
BG
112 /*
113 * notify the AP about us leaving the channel and stop all
114 * STA interfaces.
115 */
6c17b77b 116
9c35d7d2
SF
117 /*
118 * Stop queues and transmit all frames queued by the driver
119 * before sending nullfunc to enable powersave at the AP.
120 */
445ea4e8 121 ieee80211_stop_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
cca07b00
LC
122 IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL,
123 false);
3b24f4c6 124 ieee80211_flush_queues(local, NULL, false);
6c17b77b 125
b203ffc3
JM
126 mutex_lock(&local->iflist_mtx);
127 list_for_each_entry(sdata, &local->interfaces, list) {
128 if (!ieee80211_sdata_running(sdata))
129 continue;
130
f142c6b9
JB
131 if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE)
132 continue;
133
b23b025f
BG
134 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
135 set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
136
137 /* Check to see if we should disable beaconing. */
d6a83228
JB
138 if (sdata->vif.bss_conf.enable_beacon) {
139 set_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED,
140 &sdata->state);
141 sdata->vif.bss_conf.enable_beacon = false;
b203ffc3
JM
142 ieee80211_bss_info_change_notify(
143 sdata, BSS_CHANGED_BEACON_ENABLED);
d6a83228 144 }
b203ffc3 145
6c17b77b
SF
146 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
147 sdata->u.mgd.associated)
148 ieee80211_offchannel_ps_enable(sdata);
b203ffc3
JM
149 }
150 mutex_unlock(&local->iflist_mtx);
151}
152
aacde9ee 153void ieee80211_offchannel_return(struct ieee80211_local *local)
b203ffc3
JM
154{
155 struct ieee80211_sub_if_data *sdata;
156
fe57d9f5
JB
157 if (WARN_ON(local->use_chanctx))
158 return;
159
b203ffc3
JM
160 mutex_lock(&local->iflist_mtx);
161 list_for_each_entry(sdata, &local->interfaces, list) {
f142c6b9
JB
162 if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE)
163 continue;
164
f6e8cb72
EP
165 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
166 clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
167
b203ffc3
JM
168 if (!ieee80211_sdata_running(sdata))
169 continue;
170
171 /* Tell AP we're back */
aacde9ee
SG
172 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
173 sdata->u.mgd.associated)
174 ieee80211_offchannel_ps_disable(sdata);
b203ffc3 175
d6a83228
JB
176 if (test_and_clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED,
177 &sdata->state)) {
178 sdata->vif.bss_conf.enable_beacon = true;
b203ffc3
JM
179 ieee80211_bss_info_change_notify(
180 sdata, BSS_CHANGED_BEACON_ENABLED);
d6a83228 181 }
b203ffc3
JM
182 }
183 mutex_unlock(&local->iflist_mtx);
6c17b77b 184
445ea4e8 185 ieee80211_wake_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
cca07b00
LC
186 IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL,
187 false);
b203ffc3 188}
21f83589 189
aaa016cc 190static void ieee80211_roc_notify_destroy(struct ieee80211_roc_work *roc)
2eb278e0 191{
aaa016cc
JB
192 /* was never transmitted */
193 if (roc->frame) {
194 cfg80211_mgmt_tx_status(&roc->sdata->wdev, roc->mgmt_tx_cookie,
195 roc->frame->data, roc->frame->len,
196 false, GFP_KERNEL);
197 ieee80211_free_txskb(&roc->sdata->local->hw, roc->frame);
198 }
199
200 if (!roc->mgmt_tx_cookie)
201 cfg80211_remain_on_channel_expired(&roc->sdata->wdev,
202 roc->cookie, roc->chan,
203 GFP_KERNEL);
204
205 list_del(&roc->list);
206 kfree(roc);
207}
208
209static unsigned long ieee80211_end_finished_rocs(struct ieee80211_local *local,
210 unsigned long now)
211{
212 struct ieee80211_roc_work *roc, *tmp;
213 long remaining_dur_min = LONG_MAX;
214
215 lockdep_assert_held(&local->mtx);
216
217 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
218 long remaining;
219
220 if (!roc->started)
221 break;
222
223 remaining = roc->start_time +
224 msecs_to_jiffies(roc->duration) -
225 now;
226
1b894521
IP
227 /* In case of HW ROC, it is possible that the HW finished the
228 * ROC session before the actual requested time. In such a case
229 * end the ROC session (disregarding the remaining time).
230 */
231 if (roc->abort || roc->hw_begun || remaining <= 0)
aaa016cc
JB
232 ieee80211_roc_notify_destroy(roc);
233 else
234 remaining_dur_min = min(remaining_dur_min, remaining);
235 }
236
237 return remaining_dur_min;
238}
239
240static bool ieee80211_recalc_sw_work(struct ieee80211_local *local,
241 unsigned long now)
242{
243 long dur = ieee80211_end_finished_rocs(local, now);
244
245 if (dur == LONG_MAX)
246 return false;
247
248 mod_delayed_work(local->workqueue, &local->roc_work, dur);
249 return true;
250}
251
252static void ieee80211_handle_roc_started(struct ieee80211_roc_work *roc,
253 unsigned long start_time)
254{
aaa016cc 255 if (WARN_ON(roc->notified))
2eb278e0
JB
256 return;
257
aaa016cc
JB
258 roc->start_time = start_time;
259 roc->started = true;
aaa016cc 260
2eb278e0
JB
261 if (roc->mgmt_tx_cookie) {
262 if (!WARN_ON(!roc->frame)) {
55de908a
JB
263 ieee80211_tx_skb_tid_band(roc->sdata, roc->frame, 7,
264 roc->chan->band);
2eb278e0
JB
265 roc->frame = NULL;
266 }
267 } else {
50febf6a 268 cfg80211_ready_on_channel(&roc->sdata->wdev, roc->cookie,
42d97a59
JB
269 roc->chan, roc->req_duration,
270 GFP_KERNEL);
2eb278e0
JB
271 }
272
273 roc->notified = true;
274}
275
21f83589
JB
276static void ieee80211_hw_roc_start(struct work_struct *work)
277{
278 struct ieee80211_local *local =
279 container_of(work, struct ieee80211_local, hw_roc_start);
aaa016cc 280 struct ieee80211_roc_work *roc;
21f83589
JB
281
282 mutex_lock(&local->mtx);
283
aaa016cc
JB
284 list_for_each_entry(roc, &local->roc_list, list) {
285 if (!roc->started)
286 break;
2eb278e0 287
e6a8a3aa 288 roc->hw_begun = true;
aaa016cc 289 ieee80211_handle_roc_started(roc, local->hw_roc_start_time);
2eb278e0 290 }
aaa016cc 291
21f83589
JB
292 mutex_unlock(&local->mtx);
293}
294
295void ieee80211_ready_on_channel(struct ieee80211_hw *hw)
296{
297 struct ieee80211_local *local = hw_to_local(hw);
298
2eb278e0
JB
299 local->hw_roc_start_time = jiffies;
300
21f83589
JB
301 trace_api_ready_on_channel(local);
302
303 ieee80211_queue_work(hw, &local->hw_roc_start);
304}
305EXPORT_SYMBOL_GPL(ieee80211_ready_on_channel);
306
aaa016cc 307static void _ieee80211_start_next_roc(struct ieee80211_local *local)
21f83589 308{
aaa016cc
JB
309 struct ieee80211_roc_work *roc, *tmp;
310 enum ieee80211_roc_type type;
311 u32 min_dur, max_dur;
21f83589 312
2eb278e0 313 lockdep_assert_held(&local->mtx);
21f83589 314
aaa016cc 315 if (WARN_ON(list_empty(&local->roc_list)))
21f83589 316 return;
21f83589 317
2eb278e0
JB
318 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
319 list);
71ecfa18 320
aaa016cc 321 if (WARN_ON(roc->started))
0f6b3f59
JB
322 return;
323
aaa016cc
JB
324 min_dur = roc->duration;
325 max_dur = roc->duration;
326 type = roc->type;
71ecfa18 327
aaa016cc
JB
328 list_for_each_entry(tmp, &local->roc_list, list) {
329 if (tmp == roc)
330 continue;
331 if (tmp->sdata != roc->sdata || tmp->chan != roc->chan)
332 break;
333 max_dur = max(tmp->duration, max_dur);
334 min_dur = min(tmp->duration, min_dur);
335 type = max(tmp->type, type);
336 }
2eb278e0 337
aaa016cc
JB
338 if (local->ops->remain_on_channel) {
339 int ret = drv_remain_on_channel(local, roc->sdata, roc->chan,
340 max_dur, type);
2eb278e0
JB
341
342 if (ret) {
343 wiphy_warn(local->hw.wiphy,
344 "failed to start next HW ROC (%d)\n", ret);
345 /*
346 * queue the work struct again to avoid recursion
347 * when multiple failures occur
348 */
aaa016cc
JB
349 list_for_each_entry(tmp, &local->roc_list, list) {
350 if (tmp->sdata != roc->sdata ||
351 tmp->chan != roc->chan)
352 break;
353 tmp->started = true;
354 tmp->abort = true;
355 }
356 ieee80211_queue_work(&local->hw, &local->hw_roc_done);
357 return;
2eb278e0 358 }
b4b177a5 359
aaa016cc
JB
360 /* we'll notify about the start once the HW calls back */
361 list_for_each_entry(tmp, &local->roc_list, list) {
362 if (tmp->sdata != roc->sdata || tmp->chan != roc->chan)
363 break;
364 tmp->started = true;
365 }
366 } else {
b4b177a5
JB
367 /* If actually operating on the desired channel (with at least
368 * 20 MHz channel width) don't stop all the operations but still
369 * treat it as though the ROC operation started properly, so
370 * other ROC operations won't interfere with this one.
371 */
372 roc->on_channel = roc->chan == local->_oper_chandef.chan &&
373 local->_oper_chandef.width != NL80211_CHAN_WIDTH_5 &&
374 local->_oper_chandef.width != NL80211_CHAN_WIDTH_10;
21f83589 375
b4b177a5 376 /* start this ROC */
2eb278e0
JB
377 ieee80211_recalc_idle(local);
378
b4b177a5
JB
379 if (!roc->on_channel) {
380 ieee80211_offchannel_stop_vifs(local);
381
382 local->tmp_channel = roc->chan;
383 ieee80211_hw_config(local, 0);
384 }
2eb278e0 385
aaa016cc
JB
386 ieee80211_queue_delayed_work(&local->hw, &local->roc_work,
387 msecs_to_jiffies(min_dur));
388
389 /* tell userspace or send frame(s) */
390 list_for_each_entry(tmp, &local->roc_list, list) {
391 if (tmp->sdata != roc->sdata || tmp->chan != roc->chan)
392 break;
393
394 tmp->on_channel = roc->on_channel;
395 ieee80211_handle_roc_started(tmp, jiffies);
396 }
397 }
398}
399
400void ieee80211_start_next_roc(struct ieee80211_local *local)
401{
402 struct ieee80211_roc_work *roc;
403
404 lockdep_assert_held(&local->mtx);
405
406 if (list_empty(&local->roc_list)) {
407 ieee80211_run_deferred_scan(local);
408 return;
409 }
410
411 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
412 list);
413
414 if (WARN_ON_ONCE(roc->started))
415 return;
416
417 if (local->ops->remain_on_channel) {
418 _ieee80211_start_next_roc(local);
419 } else {
420 /* delay it a bit */
421 ieee80211_queue_delayed_work(&local->hw, &local->roc_work,
422 round_jiffies_relative(HZ/2));
423 }
424}
425
426static void __ieee80211_roc_work(struct ieee80211_local *local)
427{
428 struct ieee80211_roc_work *roc;
429 bool on_channel;
430
431 lockdep_assert_held(&local->mtx);
432
433 if (WARN_ON(local->ops->remain_on_channel))
434 return;
2eb278e0 435
aaa016cc
JB
436 roc = list_first_entry_or_null(&local->roc_list,
437 struct ieee80211_roc_work, list);
438 if (!roc)
439 return;
2eb278e0 440
aaa016cc
JB
441 if (!roc->started) {
442 WARN_ON(local->use_chanctx);
443 _ieee80211_start_next_roc(local);
2eb278e0 444 } else {
b4b177a5 445 on_channel = roc->on_channel;
aaa016cc
JB
446 if (ieee80211_recalc_sw_work(local, jiffies))
447 return;
2eb278e0 448
aaa016cc
JB
449 /* careful - roc pointer became invalid during recalc */
450
451 if (!on_channel) {
3b24f4c6 452 ieee80211_flush_queues(local, NULL, false);
2eb278e0
JB
453
454 local->tmp_channel = NULL;
455 ieee80211_hw_config(local, 0);
456
aacde9ee 457 ieee80211_offchannel_return(local);
2eb278e0
JB
458 }
459
460 ieee80211_recalc_idle(local);
aaa016cc 461 ieee80211_start_next_roc(local);
2eb278e0 462 }
aaa016cc 463}
2eb278e0 464
aaa016cc
JB
465static void ieee80211_roc_work(struct work_struct *work)
466{
467 struct ieee80211_local *local =
468 container_of(work, struct ieee80211_local, roc_work.work);
469
470 mutex_lock(&local->mtx);
471 __ieee80211_roc_work(local);
2eb278e0
JB
472 mutex_unlock(&local->mtx);
473}
474
475static void ieee80211_hw_roc_done(struct work_struct *work)
476{
477 struct ieee80211_local *local =
478 container_of(work, struct ieee80211_local, hw_roc_done);
2eb278e0
JB
479
480 mutex_lock(&local->mtx);
481
aaa016cc 482 ieee80211_end_finished_rocs(local, jiffies);
2eb278e0
JB
483
484 /* if there's another roc, start it now */
485 ieee80211_start_next_roc(local);
486
21f83589
JB
487 mutex_unlock(&local->mtx);
488}
489
490void ieee80211_remain_on_channel_expired(struct ieee80211_hw *hw)
491{
492 struct ieee80211_local *local = hw_to_local(hw);
493
494 trace_api_remain_on_channel_expired(local);
495
496 ieee80211_queue_work(hw, &local->hw_roc_done);
497}
498EXPORT_SYMBOL_GPL(ieee80211_remain_on_channel_expired);
499
aaa016cc
JB
500static bool
501ieee80211_coalesce_hw_started_roc(struct ieee80211_local *local,
502 struct ieee80211_roc_work *new_roc,
503 struct ieee80211_roc_work *cur_roc)
a2fcfccb
JB
504{
505 unsigned long now = jiffies;
aaa016cc
JB
506 unsigned long remaining;
507
508 if (WARN_ON(!cur_roc->started))
509 return false;
a2fcfccb 510
aaa016cc
JB
511 /* if it was scheduled in the hardware, but not started yet,
512 * we can only combine if the older one had a longer duration
513 */
514 if (!cur_roc->hw_begun && new_roc->duration > cur_roc->duration)
a2fcfccb
JB
515 return false;
516
aaa016cc
JB
517 remaining = cur_roc->start_time +
518 msecs_to_jiffies(cur_roc->duration) -
519 now;
520
a2fcfccb
JB
521 /* if it doesn't fit entirely, schedule a new one */
522 if (new_roc->duration > jiffies_to_msecs(remaining))
523 return false;
524
aaa016cc
JB
525 /* add just after the current one so we combine their finish later */
526 list_add(&new_roc->list, &cur_roc->list);
527
528 /* if the existing one has already begun then let this one also
529 * begin, otherwise they'll both be marked properly by the work
530 * struct that runs once the driver notifies us of the beginning
531 */
e6a8a3aa
JB
532 if (cur_roc->hw_begun) {
533 new_roc->hw_begun = true;
aaa016cc 534 ieee80211_handle_roc_started(new_roc, now);
e6a8a3aa 535 }
a2fcfccb 536
a2fcfccb
JB
537 return true;
538}
539
540static int ieee80211_start_roc_work(struct ieee80211_local *local,
541 struct ieee80211_sub_if_data *sdata,
542 struct ieee80211_channel *channel,
543 unsigned int duration, u64 *cookie,
544 struct sk_buff *txskb,
545 enum ieee80211_roc_type type)
546{
547 struct ieee80211_roc_work *roc, *tmp;
aaa016cc 548 bool queued = false, combine_started = true;
a2fcfccb
JB
549 int ret;
550
551 lockdep_assert_held(&local->mtx);
552
553 if (local->use_chanctx && !local->ops->remain_on_channel)
554 return -EOPNOTSUPP;
555
556 roc = kzalloc(sizeof(*roc), GFP_KERNEL);
557 if (!roc)
558 return -ENOMEM;
559
560 /*
561 * If the duration is zero, then the driver
562 * wouldn't actually do anything. Set it to
563 * 10 for now.
564 *
565 * TODO: cancel the off-channel operation
566 * when we get the SKB's TX status and
567 * the wait time was zero before.
568 */
569 if (!duration)
570 duration = 10;
571
572 roc->chan = channel;
573 roc->duration = duration;
574 roc->req_duration = duration;
575 roc->frame = txskb;
576 roc->type = type;
577 roc->sdata = sdata;
a2fcfccb
JB
578
579 /*
580 * cookie is either the roc cookie (for normal roc)
581 * or the SKB (for mgmt TX)
582 */
583 if (!txskb) {
584 roc->cookie = ieee80211_mgmt_tx_cookie(local);
585 *cookie = roc->cookie;
586 } else {
587 roc->mgmt_tx_cookie = *cookie;
588 }
589
aaa016cc
JB
590 /* if there's no need to queue, handle it immediately */
591 if (list_empty(&local->roc_list) &&
592 !local->scanning && !ieee80211_is_radar_required(local)) {
593 /* if not HW assist, just queue & schedule work */
594 if (!local->ops->remain_on_channel) {
595 list_add_tail(&roc->list, &local->roc_list);
596 ieee80211_queue_delayed_work(&local->hw,
597 &local->roc_work, 0);
598 } else {
599 /* otherwise actually kick it off here
600 * (for error handling)
601 */
602 ret = drv_remain_on_channel(local, sdata, channel,
603 duration, type);
604 if (ret) {
605 kfree(roc);
606 return ret;
607 }
608 roc->started = true;
609 list_add_tail(&roc->list, &local->roc_list);
610 }
a2fcfccb 611
aaa016cc 612 return 0;
a2fcfccb
JB
613 }
614
aaa016cc 615 /* otherwise handle queueing */
a2fcfccb 616
a2fcfccb
JB
617 list_for_each_entry(tmp, &local->roc_list, list) {
618 if (tmp->chan != channel || tmp->sdata != sdata)
619 continue;
620
621 /*
aaa016cc
JB
622 * Extend this ROC if possible: If it hasn't started, add
623 * just after the new one to combine.
a2fcfccb
JB
624 */
625 if (!tmp->started) {
aaa016cc 626 list_add(&roc->list, &tmp->list);
a2fcfccb
JB
627 queued = true;
628 break;
629 }
630
aaa016cc
JB
631 if (!combine_started)
632 continue;
a2fcfccb 633
aaa016cc
JB
634 if (!local->ops->remain_on_channel) {
635 /* If there's no hardware remain-on-channel, and
636 * doing so won't push us over the maximum r-o-c
637 * we allow, then we can just add the new one to
638 * the list and mark it as having started now.
639 * If it would push over the limit, don't try to
640 * combine with other started ones (that haven't
641 * been running as long) but potentially sort it
642 * with others that had the same fate.
a2fcfccb 643 */
aaa016cc
JB
644 unsigned long now = jiffies;
645 u32 elapsed = jiffies_to_msecs(now - tmp->start_time);
646 struct wiphy *wiphy = local->hw.wiphy;
647 u32 max_roc = wiphy->max_remain_on_channel_duration;
a2fcfccb 648
aaa016cc
JB
649 if (elapsed + roc->duration > max_roc) {
650 combine_started = false;
651 continue;
652 }
a2fcfccb 653
aaa016cc
JB
654 list_add(&roc->list, &tmp->list);
655 queued = true;
656 roc->on_channel = tmp->on_channel;
657 ieee80211_handle_roc_started(roc, now);
e9db4557 658 ieee80211_recalc_sw_work(local, now);
aaa016cc 659 break;
a2fcfccb 660 }
aaa016cc
JB
661
662 queued = ieee80211_coalesce_hw_started_roc(local, roc, tmp);
663 if (queued)
664 break;
665 /* if it wasn't queued, perhaps it can be combined with
666 * another that also couldn't get combined previously,
667 * but no need to check for already started ones, since
668 * that can't work.
669 */
670 combine_started = false;
a2fcfccb
JB
671 }
672
a2fcfccb
JB
673 if (!queued)
674 list_add_tail(&roc->list, &local->roc_list);
675
676 return 0;
677}
678
679int ieee80211_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
680 struct ieee80211_channel *chan,
681 unsigned int duration, u64 *cookie)
682{
683 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
684 struct ieee80211_local *local = sdata->local;
685 int ret;
686
687 mutex_lock(&local->mtx);
688 ret = ieee80211_start_roc_work(local, sdata, chan,
689 duration, cookie, NULL,
690 IEEE80211_ROC_TYPE_NORMAL);
691 mutex_unlock(&local->mtx);
692
693 return ret;
694}
695
696static int ieee80211_cancel_roc(struct ieee80211_local *local,
697 u64 cookie, bool mgmt_tx)
698{
699 struct ieee80211_roc_work *roc, *tmp, *found = NULL;
700 int ret;
701
7d37fcd4
JB
702 if (!cookie)
703 return -ENOENT;
704
a2fcfccb
JB
705 mutex_lock(&local->mtx);
706 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
a2fcfccb
JB
707 if (!mgmt_tx && roc->cookie != cookie)
708 continue;
709 else if (mgmt_tx && roc->mgmt_tx_cookie != cookie)
710 continue;
711
712 found = roc;
713 break;
714 }
715
716 if (!found) {
717 mutex_unlock(&local->mtx);
718 return -ENOENT;
719 }
720
aaa016cc
JB
721 if (!found->started) {
722 ieee80211_roc_notify_destroy(found);
723 goto out_unlock;
724 }
a2fcfccb
JB
725
726 if (local->ops->remain_on_channel) {
aaa016cc
JB
727 ret = drv_cancel_remain_on_channel(local);
728 if (WARN_ON_ONCE(ret)) {
729 mutex_unlock(&local->mtx);
730 return ret;
a2fcfccb
JB
731 }
732
aaa016cc
JB
733 /* TODO:
734 * if multiple items were combined here then we really shouldn't
735 * cancel them all - we should wait for as much time as needed
736 * for the longest remaining one, and only then cancel ...
737 */
738 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
739 if (!roc->started)
740 break;
741 if (roc == found)
742 found = NULL;
743 ieee80211_roc_notify_destroy(roc);
744 }
a2fcfccb 745
aaa016cc
JB
746 /* that really must not happen - it was started */
747 WARN_ON(found);
a2fcfccb 748
aaa016cc 749 ieee80211_start_next_roc(local);
a2fcfccb 750 } else {
aaa016cc 751 /* go through work struct to return to the operating channel */
a2fcfccb 752 found->abort = true;
aaa016cc 753 mod_delayed_work(local->workqueue, &local->roc_work, 0);
a2fcfccb
JB
754 }
755
aaa016cc
JB
756 out_unlock:
757 mutex_unlock(&local->mtx);
758
a2fcfccb
JB
759 return 0;
760}
761
762int ieee80211_cancel_remain_on_channel(struct wiphy *wiphy,
763 struct wireless_dev *wdev, u64 cookie)
764{
765 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
766 struct ieee80211_local *local = sdata->local;
767
768 return ieee80211_cancel_roc(local, cookie, false);
769}
770
771int ieee80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
772 struct cfg80211_mgmt_tx_params *params, u64 *cookie)
773{
774 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
775 struct ieee80211_local *local = sdata->local;
5ee00dbd 776 struct sk_buff *skb;
a2fcfccb
JB
777 struct sta_info *sta;
778 const struct ieee80211_mgmt *mgmt = (void *)params->buf;
779 bool need_offchan = false;
780 u32 flags;
781 int ret;
782 u8 *data;
783
784 if (params->dont_wait_for_ack)
785 flags = IEEE80211_TX_CTL_NO_ACK;
786 else
787 flags = IEEE80211_TX_INTFL_NL80211_FRAME_TX |
788 IEEE80211_TX_CTL_REQ_TX_STATUS;
789
790 if (params->no_cck)
791 flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
792
793 switch (sdata->vif.type) {
794 case NL80211_IFTYPE_ADHOC:
795 if (!sdata->vif.bss_conf.ibss_joined)
796 need_offchan = true;
797 /* fall through */
798#ifdef CONFIG_MAC80211_MESH
799 case NL80211_IFTYPE_MESH_POINT:
800 if (ieee80211_vif_is_mesh(&sdata->vif) &&
801 !sdata->u.mesh.mesh_id_len)
802 need_offchan = true;
803 /* fall through */
804#endif
805 case NL80211_IFTYPE_AP:
806 case NL80211_IFTYPE_AP_VLAN:
807 case NL80211_IFTYPE_P2P_GO:
808 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
809 !ieee80211_vif_is_mesh(&sdata->vif) &&
810 !rcu_access_pointer(sdata->bss->beacon))
811 need_offchan = true;
812 if (!ieee80211_is_action(mgmt->frame_control) ||
813 mgmt->u.action.category == WLAN_CATEGORY_PUBLIC ||
814 mgmt->u.action.category == WLAN_CATEGORY_SELF_PROTECTED ||
815 mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT)
816 break;
817 rcu_read_lock();
818 sta = sta_info_get(sdata, mgmt->da);
819 rcu_read_unlock();
820 if (!sta)
821 return -ENOLINK;
822 break;
823 case NL80211_IFTYPE_STATION:
824 case NL80211_IFTYPE_P2P_CLIENT:
825 sdata_lock(sdata);
826 if (!sdata->u.mgd.associated ||
827 (params->offchan && params->wait &&
828 local->ops->remain_on_channel &&
829 memcmp(sdata->u.mgd.associated->bssid,
830 mgmt->bssid, ETH_ALEN)))
831 need_offchan = true;
832 sdata_unlock(sdata);
833 break;
834 case NL80211_IFTYPE_P2P_DEVICE:
835 need_offchan = true;
836 break;
837 default:
838 return -EOPNOTSUPP;
839 }
840
841 /* configurations requiring offchan cannot work if no channel has been
842 * specified
843 */
844 if (need_offchan && !params->chan)
845 return -EINVAL;
846
847 mutex_lock(&local->mtx);
848
849 /* Check if the operating channel is the requested channel */
850 if (!need_offchan) {
851 struct ieee80211_chanctx_conf *chanctx_conf;
852
853 rcu_read_lock();
854 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
855
856 if (chanctx_conf) {
857 need_offchan = params->chan &&
858 (params->chan !=
859 chanctx_conf->def.chan);
860 } else if (!params->chan) {
861 ret = -EINVAL;
862 rcu_read_unlock();
863 goto out_unlock;
864 } else {
865 need_offchan = true;
866 }
867 rcu_read_unlock();
868 }
869
870 if (need_offchan && !params->offchan) {
871 ret = -EBUSY;
872 goto out_unlock;
873 }
874
875 skb = dev_alloc_skb(local->hw.extra_tx_headroom + params->len);
876 if (!skb) {
877 ret = -ENOMEM;
878 goto out_unlock;
879 }
880 skb_reserve(skb, local->hw.extra_tx_headroom);
881
882 data = skb_put(skb, params->len);
883 memcpy(data, params->buf, params->len);
884
885 /* Update CSA counters */
886 if (sdata->vif.csa_active &&
887 (sdata->vif.type == NL80211_IFTYPE_AP ||
888 sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
889 sdata->vif.type == NL80211_IFTYPE_ADHOC) &&
890 params->n_csa_offsets) {
891 int i;
892 struct beacon_data *beacon = NULL;
893
894 rcu_read_lock();
895
896 if (sdata->vif.type == NL80211_IFTYPE_AP)
897 beacon = rcu_dereference(sdata->u.ap.beacon);
898 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
899 beacon = rcu_dereference(sdata->u.ibss.presp);
900 else if (ieee80211_vif_is_mesh(&sdata->vif))
901 beacon = rcu_dereference(sdata->u.mesh.beacon);
902
903 if (beacon)
904 for (i = 0; i < params->n_csa_offsets; i++)
905 data[params->csa_offsets[i]] =
906 beacon->csa_current_counter;
907
908 rcu_read_unlock();
909 }
910
911 IEEE80211_SKB_CB(skb)->flags = flags;
912
913 skb->dev = sdata->dev;
914
915 if (!params->dont_wait_for_ack) {
916 /* make a copy to preserve the frame contents
917 * in case of encryption.
918 */
5ee00dbd
JB
919 ret = ieee80211_attach_ack_skb(local, skb, cookie, GFP_KERNEL);
920 if (ret) {
a2fcfccb
JB
921 kfree_skb(skb);
922 goto out_unlock;
923 }
924 } else {
925 /* Assign a dummy non-zero cookie, it's not sent to
926 * userspace in this case but we rely on its value
927 * internally in the need_offchan case to distinguish
928 * mgmt-tx from remain-on-channel.
929 */
930 *cookie = 0xffffffff;
931 }
932
933 if (!need_offchan) {
934 ieee80211_tx_skb(sdata, skb);
935 ret = 0;
936 goto out_unlock;
937 }
938
939 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_TX_OFFCHAN |
940 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
941 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
942 IEEE80211_SKB_CB(skb)->hw_queue =
943 local->hw.offchannel_tx_hw_queue;
944
945 /* This will handle all kinds of coalescing and immediate TX */
946 ret = ieee80211_start_roc_work(local, sdata, params->chan,
947 params->wait, cookie, skb,
948 IEEE80211_ROC_TYPE_MGMT_TX);
949 if (ret)
950 ieee80211_free_txskb(&local->hw, skb);
951 out_unlock:
952 mutex_unlock(&local->mtx);
953 return ret;
954}
955
956int ieee80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
957 struct wireless_dev *wdev, u64 cookie)
958{
959 struct ieee80211_local *local = wiphy_priv(wiphy);
960
961 return ieee80211_cancel_roc(local, cookie, true);
962}
963
2eb278e0 964void ieee80211_roc_setup(struct ieee80211_local *local)
21f83589
JB
965{
966 INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start);
967 INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done);
aaa016cc 968 INIT_DELAYED_WORK(&local->roc_work, ieee80211_roc_work);
2eb278e0
JB
969 INIT_LIST_HEAD(&local->roc_list);
970}
971
c8f994ee
JB
972void ieee80211_roc_purge(struct ieee80211_local *local,
973 struct ieee80211_sub_if_data *sdata)
2eb278e0 974{
2eb278e0 975 struct ieee80211_roc_work *roc, *tmp;
aaa016cc 976 bool work_to_do = false;
2eb278e0
JB
977
978 mutex_lock(&local->mtx);
979 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
c8f994ee 980 if (sdata && roc->sdata != sdata)
2eb278e0
JB
981 continue;
982
aaa016cc
JB
983 if (roc->started) {
984 if (local->ops->remain_on_channel) {
985 /* can race, so ignore return value */
986 drv_cancel_remain_on_channel(local);
987 ieee80211_roc_notify_destroy(roc);
988 } else {
989 roc->abort = true;
990 work_to_do = true;
991 }
2eb278e0 992 } else {
aaa016cc 993 ieee80211_roc_notify_destroy(roc);
2eb278e0
JB
994 }
995 }
aaa016cc
JB
996 if (work_to_do)
997 __ieee80211_roc_work(local);
998 mutex_unlock(&local->mtx);
21f83589 999}