mac80211_hwsim: stop using pointers as cookies
[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
227 if (roc->abort || remaining <= 0)
228 ieee80211_roc_notify_destroy(roc);
229 else
230 remaining_dur_min = min(remaining_dur_min, remaining);
231 }
232
233 return remaining_dur_min;
234}
235
236static bool ieee80211_recalc_sw_work(struct ieee80211_local *local,
237 unsigned long now)
238{
239 long dur = ieee80211_end_finished_rocs(local, now);
240
241 if (dur == LONG_MAX)
242 return false;
243
244 mod_delayed_work(local->workqueue, &local->roc_work, dur);
245 return true;
246}
247
248static void ieee80211_handle_roc_started(struct ieee80211_roc_work *roc,
249 unsigned long start_time)
250{
251 struct ieee80211_local *local = roc->sdata->local;
252
253 if (WARN_ON(roc->notified))
2eb278e0
JB
254 return;
255
aaa016cc
JB
256 roc->start_time = start_time;
257 roc->started = true;
258 roc->hw_begun = true;
259
2eb278e0
JB
260 if (roc->mgmt_tx_cookie) {
261 if (!WARN_ON(!roc->frame)) {
55de908a
JB
262 ieee80211_tx_skb_tid_band(roc->sdata, roc->frame, 7,
263 roc->chan->band);
2eb278e0
JB
264 roc->frame = NULL;
265 }
266 } else {
50febf6a 267 cfg80211_ready_on_channel(&roc->sdata->wdev, roc->cookie,
42d97a59
JB
268 roc->chan, roc->req_duration,
269 GFP_KERNEL);
2eb278e0
JB
270 }
271
272 roc->notified = true;
aaa016cc
JB
273
274 if (!local->ops->remain_on_channel)
275 ieee80211_recalc_sw_work(local, start_time);
2eb278e0
JB
276}
277
21f83589
JB
278static void ieee80211_hw_roc_start(struct work_struct *work)
279{
280 struct ieee80211_local *local =
281 container_of(work, struct ieee80211_local, hw_roc_start);
aaa016cc 282 struct ieee80211_roc_work *roc;
21f83589
JB
283
284 mutex_lock(&local->mtx);
285
aaa016cc
JB
286 list_for_each_entry(roc, &local->roc_list, list) {
287 if (!roc->started)
288 break;
2eb278e0 289
aaa016cc 290 ieee80211_handle_roc_started(roc, local->hw_roc_start_time);
2eb278e0 291 }
aaa016cc 292
21f83589
JB
293 mutex_unlock(&local->mtx);
294}
295
296void ieee80211_ready_on_channel(struct ieee80211_hw *hw)
297{
298 struct ieee80211_local *local = hw_to_local(hw);
299
2eb278e0
JB
300 local->hw_roc_start_time = jiffies;
301
21f83589
JB
302 trace_api_ready_on_channel(local);
303
304 ieee80211_queue_work(hw, &local->hw_roc_start);
305}
306EXPORT_SYMBOL_GPL(ieee80211_ready_on_channel);
307
aaa016cc 308static void _ieee80211_start_next_roc(struct ieee80211_local *local)
21f83589 309{
aaa016cc
JB
310 struct ieee80211_roc_work *roc, *tmp;
311 enum ieee80211_roc_type type;
312 u32 min_dur, max_dur;
21f83589 313
2eb278e0 314 lockdep_assert_held(&local->mtx);
21f83589 315
aaa016cc 316 if (WARN_ON(list_empty(&local->roc_list)))
21f83589 317 return;
21f83589 318
2eb278e0
JB
319 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
320 list);
71ecfa18 321
aaa016cc 322 if (WARN_ON(roc->started))
0f6b3f59
JB
323 return;
324
aaa016cc
JB
325 min_dur = roc->duration;
326 max_dur = roc->duration;
327 type = roc->type;
71ecfa18 328
aaa016cc
JB
329 list_for_each_entry(tmp, &local->roc_list, list) {
330 if (tmp == roc)
331 continue;
332 if (tmp->sdata != roc->sdata || tmp->chan != roc->chan)
333 break;
334 max_dur = max(tmp->duration, max_dur);
335 min_dur = min(tmp->duration, min_dur);
336 type = max(tmp->type, type);
337 }
2eb278e0 338
aaa016cc
JB
339 if (local->ops->remain_on_channel) {
340 int ret = drv_remain_on_channel(local, roc->sdata, roc->chan,
341 max_dur, type);
2eb278e0
JB
342
343 if (ret) {
344 wiphy_warn(local->hw.wiphy,
345 "failed to start next HW ROC (%d)\n", ret);
346 /*
347 * queue the work struct again to avoid recursion
348 * when multiple failures occur
349 */
aaa016cc
JB
350 list_for_each_entry(tmp, &local->roc_list, list) {
351 if (tmp->sdata != roc->sdata ||
352 tmp->chan != roc->chan)
353 break;
354 tmp->started = true;
355 tmp->abort = true;
356 }
357 ieee80211_queue_work(&local->hw, &local->hw_roc_done);
358 return;
2eb278e0 359 }
b4b177a5 360
aaa016cc
JB
361 /* we'll notify about the start once the HW calls back */
362 list_for_each_entry(tmp, &local->roc_list, list) {
363 if (tmp->sdata != roc->sdata || tmp->chan != roc->chan)
364 break;
365 tmp->started = true;
366 }
367 } else {
b4b177a5
JB
368 /* If actually operating on the desired channel (with at least
369 * 20 MHz channel width) don't stop all the operations but still
370 * treat it as though the ROC operation started properly, so
371 * other ROC operations won't interfere with this one.
372 */
373 roc->on_channel = roc->chan == local->_oper_chandef.chan &&
374 local->_oper_chandef.width != NL80211_CHAN_WIDTH_5 &&
375 local->_oper_chandef.width != NL80211_CHAN_WIDTH_10;
21f83589 376
b4b177a5 377 /* start this ROC */
2eb278e0
JB
378 ieee80211_recalc_idle(local);
379
b4b177a5
JB
380 if (!roc->on_channel) {
381 ieee80211_offchannel_stop_vifs(local);
382
383 local->tmp_channel = roc->chan;
384 ieee80211_hw_config(local, 0);
385 }
2eb278e0 386
aaa016cc
JB
387 ieee80211_queue_delayed_work(&local->hw, &local->roc_work,
388 msecs_to_jiffies(min_dur));
389
390 /* tell userspace or send frame(s) */
391 list_for_each_entry(tmp, &local->roc_list, list) {
392 if (tmp->sdata != roc->sdata || tmp->chan != roc->chan)
393 break;
394
395 tmp->on_channel = roc->on_channel;
396 ieee80211_handle_roc_started(tmp, jiffies);
397 }
398 }
399}
400
401void ieee80211_start_next_roc(struct ieee80211_local *local)
402{
403 struct ieee80211_roc_work *roc;
404
405 lockdep_assert_held(&local->mtx);
406
407 if (list_empty(&local->roc_list)) {
408 ieee80211_run_deferred_scan(local);
409 return;
410 }
411
412 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
413 list);
414
415 if (WARN_ON_ONCE(roc->started))
416 return;
417
418 if (local->ops->remain_on_channel) {
419 _ieee80211_start_next_roc(local);
420 } else {
421 /* delay it a bit */
422 ieee80211_queue_delayed_work(&local->hw, &local->roc_work,
423 round_jiffies_relative(HZ/2));
424 }
425}
426
427static void __ieee80211_roc_work(struct ieee80211_local *local)
428{
429 struct ieee80211_roc_work *roc;
430 bool on_channel;
431
432 lockdep_assert_held(&local->mtx);
433
434 if (WARN_ON(local->ops->remain_on_channel))
435 return;
2eb278e0 436
aaa016cc
JB
437 roc = list_first_entry_or_null(&local->roc_list,
438 struct ieee80211_roc_work, list);
439 if (!roc)
440 return;
2eb278e0 441
aaa016cc
JB
442 if (!roc->started) {
443 WARN_ON(local->use_chanctx);
444 _ieee80211_start_next_roc(local);
2eb278e0 445 } else {
b4b177a5 446 on_channel = roc->on_channel;
aaa016cc
JB
447 if (ieee80211_recalc_sw_work(local, jiffies))
448 return;
2eb278e0 449
aaa016cc
JB
450 /* careful - roc pointer became invalid during recalc */
451
452 if (!on_channel) {
3b24f4c6 453 ieee80211_flush_queues(local, NULL, false);
2eb278e0
JB
454
455 local->tmp_channel = NULL;
456 ieee80211_hw_config(local, 0);
457
aacde9ee 458 ieee80211_offchannel_return(local);
2eb278e0
JB
459 }
460
461 ieee80211_recalc_idle(local);
aaa016cc 462 ieee80211_start_next_roc(local);
2eb278e0 463 }
aaa016cc 464}
2eb278e0 465
aaa016cc
JB
466static void ieee80211_roc_work(struct work_struct *work)
467{
468 struct ieee80211_local *local =
469 container_of(work, struct ieee80211_local, roc_work.work);
470
471 mutex_lock(&local->mtx);
472 __ieee80211_roc_work(local);
2eb278e0
JB
473 mutex_unlock(&local->mtx);
474}
475
476static void ieee80211_hw_roc_done(struct work_struct *work)
477{
478 struct ieee80211_local *local =
479 container_of(work, struct ieee80211_local, hw_roc_done);
2eb278e0
JB
480
481 mutex_lock(&local->mtx);
482
aaa016cc 483 ieee80211_end_finished_rocs(local, jiffies);
2eb278e0
JB
484
485 /* if there's another roc, start it now */
486 ieee80211_start_next_roc(local);
487
21f83589
JB
488 mutex_unlock(&local->mtx);
489}
490
491void ieee80211_remain_on_channel_expired(struct ieee80211_hw *hw)
492{
493 struct ieee80211_local *local = hw_to_local(hw);
494
495 trace_api_remain_on_channel_expired(local);
496
497 ieee80211_queue_work(hw, &local->hw_roc_done);
498}
499EXPORT_SYMBOL_GPL(ieee80211_remain_on_channel_expired);
500
aaa016cc
JB
501static bool
502ieee80211_coalesce_hw_started_roc(struct ieee80211_local *local,
503 struct ieee80211_roc_work *new_roc,
504 struct ieee80211_roc_work *cur_roc)
a2fcfccb
JB
505{
506 unsigned long now = jiffies;
aaa016cc
JB
507 unsigned long remaining;
508
509 if (WARN_ON(!cur_roc->started))
510 return false;
a2fcfccb 511
aaa016cc
JB
512 /* if it was scheduled in the hardware, but not started yet,
513 * we can only combine if the older one had a longer duration
514 */
515 if (!cur_roc->hw_begun && new_roc->duration > cur_roc->duration)
a2fcfccb
JB
516 return false;
517
aaa016cc
JB
518 remaining = cur_roc->start_time +
519 msecs_to_jiffies(cur_roc->duration) -
520 now;
521
a2fcfccb
JB
522 /* if it doesn't fit entirely, schedule a new one */
523 if (new_roc->duration > jiffies_to_msecs(remaining))
524 return false;
525
aaa016cc
JB
526 /* add just after the current one so we combine their finish later */
527 list_add(&new_roc->list, &cur_roc->list);
528
529 /* if the existing one has already begun then let this one also
530 * begin, otherwise they'll both be marked properly by the work
531 * struct that runs once the driver notifies us of the beginning
532 */
533 if (cur_roc->hw_begun)
534 ieee80211_handle_roc_started(new_roc, now);
a2fcfccb 535
a2fcfccb
JB
536 return true;
537}
538
539static int ieee80211_start_roc_work(struct ieee80211_local *local,
540 struct ieee80211_sub_if_data *sdata,
541 struct ieee80211_channel *channel,
542 unsigned int duration, u64 *cookie,
543 struct sk_buff *txskb,
544 enum ieee80211_roc_type type)
545{
546 struct ieee80211_roc_work *roc, *tmp;
aaa016cc 547 bool queued = false, combine_started = true;
a2fcfccb
JB
548 int ret;
549
550 lockdep_assert_held(&local->mtx);
551
552 if (local->use_chanctx && !local->ops->remain_on_channel)
553 return -EOPNOTSUPP;
554
555 roc = kzalloc(sizeof(*roc), GFP_KERNEL);
556 if (!roc)
557 return -ENOMEM;
558
559 /*
560 * If the duration is zero, then the driver
561 * wouldn't actually do anything. Set it to
562 * 10 for now.
563 *
564 * TODO: cancel the off-channel operation
565 * when we get the SKB's TX status and
566 * the wait time was zero before.
567 */
568 if (!duration)
569 duration = 10;
570
571 roc->chan = channel;
572 roc->duration = duration;
573 roc->req_duration = duration;
574 roc->frame = txskb;
575 roc->type = type;
576 roc->sdata = sdata;
a2fcfccb
JB
577
578 /*
579 * cookie is either the roc cookie (for normal roc)
580 * or the SKB (for mgmt TX)
581 */
582 if (!txskb) {
583 roc->cookie = ieee80211_mgmt_tx_cookie(local);
584 *cookie = roc->cookie;
585 } else {
586 roc->mgmt_tx_cookie = *cookie;
587 }
588
aaa016cc
JB
589 /* if there's no need to queue, handle it immediately */
590 if (list_empty(&local->roc_list) &&
591 !local->scanning && !ieee80211_is_radar_required(local)) {
592 /* if not HW assist, just queue & schedule work */
593 if (!local->ops->remain_on_channel) {
594 list_add_tail(&roc->list, &local->roc_list);
595 ieee80211_queue_delayed_work(&local->hw,
596 &local->roc_work, 0);
597 } else {
598 /* otherwise actually kick it off here
599 * (for error handling)
600 */
601 ret = drv_remain_on_channel(local, sdata, channel,
602 duration, type);
603 if (ret) {
604 kfree(roc);
605 return ret;
606 }
607 roc->started = true;
608 list_add_tail(&roc->list, &local->roc_list);
609 }
a2fcfccb 610
aaa016cc 611 return 0;
a2fcfccb
JB
612 }
613
aaa016cc 614 /* otherwise handle queueing */
a2fcfccb 615
a2fcfccb
JB
616 list_for_each_entry(tmp, &local->roc_list, list) {
617 if (tmp->chan != channel || tmp->sdata != sdata)
618 continue;
619
620 /*
aaa016cc
JB
621 * Extend this ROC if possible: If it hasn't started, add
622 * just after the new one to combine.
a2fcfccb
JB
623 */
624 if (!tmp->started) {
aaa016cc 625 list_add(&roc->list, &tmp->list);
a2fcfccb
JB
626 queued = true;
627 break;
628 }
629
aaa016cc
JB
630 if (!combine_started)
631 continue;
a2fcfccb 632
aaa016cc
JB
633 if (!local->ops->remain_on_channel) {
634 /* If there's no hardware remain-on-channel, and
635 * doing so won't push us over the maximum r-o-c
636 * we allow, then we can just add the new one to
637 * the list and mark it as having started now.
638 * If it would push over the limit, don't try to
639 * combine with other started ones (that haven't
640 * been running as long) but potentially sort it
641 * with others that had the same fate.
a2fcfccb 642 */
aaa016cc
JB
643 unsigned long now = jiffies;
644 u32 elapsed = jiffies_to_msecs(now - tmp->start_time);
645 struct wiphy *wiphy = local->hw.wiphy;
646 u32 max_roc = wiphy->max_remain_on_channel_duration;
a2fcfccb 647
aaa016cc
JB
648 if (elapsed + roc->duration > max_roc) {
649 combine_started = false;
650 continue;
651 }
a2fcfccb 652
aaa016cc
JB
653 list_add(&roc->list, &tmp->list);
654 queued = true;
655 roc->on_channel = tmp->on_channel;
656 ieee80211_handle_roc_started(roc, now);
657 break;
a2fcfccb 658 }
aaa016cc
JB
659
660 queued = ieee80211_coalesce_hw_started_roc(local, roc, tmp);
661 if (queued)
662 break;
663 /* if it wasn't queued, perhaps it can be combined with
664 * another that also couldn't get combined previously,
665 * but no need to check for already started ones, since
666 * that can't work.
667 */
668 combine_started = false;
a2fcfccb
JB
669 }
670
a2fcfccb
JB
671 if (!queued)
672 list_add_tail(&roc->list, &local->roc_list);
673
674 return 0;
675}
676
677int ieee80211_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
678 struct ieee80211_channel *chan,
679 unsigned int duration, u64 *cookie)
680{
681 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
682 struct ieee80211_local *local = sdata->local;
683 int ret;
684
685 mutex_lock(&local->mtx);
686 ret = ieee80211_start_roc_work(local, sdata, chan,
687 duration, cookie, NULL,
688 IEEE80211_ROC_TYPE_NORMAL);
689 mutex_unlock(&local->mtx);
690
691 return ret;
692}
693
694static int ieee80211_cancel_roc(struct ieee80211_local *local,
695 u64 cookie, bool mgmt_tx)
696{
697 struct ieee80211_roc_work *roc, *tmp, *found = NULL;
698 int ret;
699
700 mutex_lock(&local->mtx);
701 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
a2fcfccb
JB
702 if (!mgmt_tx && roc->cookie != cookie)
703 continue;
704 else if (mgmt_tx && roc->mgmt_tx_cookie != cookie)
705 continue;
706
707 found = roc;
708 break;
709 }
710
711 if (!found) {
712 mutex_unlock(&local->mtx);
713 return -ENOENT;
714 }
715
aaa016cc
JB
716 if (!found->started) {
717 ieee80211_roc_notify_destroy(found);
718 goto out_unlock;
719 }
a2fcfccb
JB
720
721 if (local->ops->remain_on_channel) {
aaa016cc
JB
722 ret = drv_cancel_remain_on_channel(local);
723 if (WARN_ON_ONCE(ret)) {
724 mutex_unlock(&local->mtx);
725 return ret;
a2fcfccb
JB
726 }
727
aaa016cc
JB
728 /* TODO:
729 * if multiple items were combined here then we really shouldn't
730 * cancel them all - we should wait for as much time as needed
731 * for the longest remaining one, and only then cancel ...
732 */
733 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
734 if (!roc->started)
735 break;
736 if (roc == found)
737 found = NULL;
738 ieee80211_roc_notify_destroy(roc);
739 }
a2fcfccb 740
aaa016cc
JB
741 /* that really must not happen - it was started */
742 WARN_ON(found);
a2fcfccb 743
aaa016cc 744 ieee80211_start_next_roc(local);
a2fcfccb 745 } else {
aaa016cc 746 /* go through work struct to return to the operating channel */
a2fcfccb 747 found->abort = true;
aaa016cc 748 mod_delayed_work(local->workqueue, &local->roc_work, 0);
a2fcfccb
JB
749 }
750
aaa016cc
JB
751 out_unlock:
752 mutex_unlock(&local->mtx);
753
a2fcfccb
JB
754 return 0;
755}
756
757int ieee80211_cancel_remain_on_channel(struct wiphy *wiphy,
758 struct wireless_dev *wdev, u64 cookie)
759{
760 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
761 struct ieee80211_local *local = sdata->local;
762
763 return ieee80211_cancel_roc(local, cookie, false);
764}
765
766int ieee80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
767 struct cfg80211_mgmt_tx_params *params, u64 *cookie)
768{
769 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
770 struct ieee80211_local *local = sdata->local;
5ee00dbd 771 struct sk_buff *skb;
a2fcfccb
JB
772 struct sta_info *sta;
773 const struct ieee80211_mgmt *mgmt = (void *)params->buf;
774 bool need_offchan = false;
775 u32 flags;
776 int ret;
777 u8 *data;
778
779 if (params->dont_wait_for_ack)
780 flags = IEEE80211_TX_CTL_NO_ACK;
781 else
782 flags = IEEE80211_TX_INTFL_NL80211_FRAME_TX |
783 IEEE80211_TX_CTL_REQ_TX_STATUS;
784
785 if (params->no_cck)
786 flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
787
788 switch (sdata->vif.type) {
789 case NL80211_IFTYPE_ADHOC:
790 if (!sdata->vif.bss_conf.ibss_joined)
791 need_offchan = true;
792 /* fall through */
793#ifdef CONFIG_MAC80211_MESH
794 case NL80211_IFTYPE_MESH_POINT:
795 if (ieee80211_vif_is_mesh(&sdata->vif) &&
796 !sdata->u.mesh.mesh_id_len)
797 need_offchan = true;
798 /* fall through */
799#endif
800 case NL80211_IFTYPE_AP:
801 case NL80211_IFTYPE_AP_VLAN:
802 case NL80211_IFTYPE_P2P_GO:
803 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
804 !ieee80211_vif_is_mesh(&sdata->vif) &&
805 !rcu_access_pointer(sdata->bss->beacon))
806 need_offchan = true;
807 if (!ieee80211_is_action(mgmt->frame_control) ||
808 mgmt->u.action.category == WLAN_CATEGORY_PUBLIC ||
809 mgmt->u.action.category == WLAN_CATEGORY_SELF_PROTECTED ||
810 mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT)
811 break;
812 rcu_read_lock();
813 sta = sta_info_get(sdata, mgmt->da);
814 rcu_read_unlock();
815 if (!sta)
816 return -ENOLINK;
817 break;
818 case NL80211_IFTYPE_STATION:
819 case NL80211_IFTYPE_P2P_CLIENT:
820 sdata_lock(sdata);
821 if (!sdata->u.mgd.associated ||
822 (params->offchan && params->wait &&
823 local->ops->remain_on_channel &&
824 memcmp(sdata->u.mgd.associated->bssid,
825 mgmt->bssid, ETH_ALEN)))
826 need_offchan = true;
827 sdata_unlock(sdata);
828 break;
829 case NL80211_IFTYPE_P2P_DEVICE:
830 need_offchan = true;
831 break;
832 default:
833 return -EOPNOTSUPP;
834 }
835
836 /* configurations requiring offchan cannot work if no channel has been
837 * specified
838 */
839 if (need_offchan && !params->chan)
840 return -EINVAL;
841
842 mutex_lock(&local->mtx);
843
844 /* Check if the operating channel is the requested channel */
845 if (!need_offchan) {
846 struct ieee80211_chanctx_conf *chanctx_conf;
847
848 rcu_read_lock();
849 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
850
851 if (chanctx_conf) {
852 need_offchan = params->chan &&
853 (params->chan !=
854 chanctx_conf->def.chan);
855 } else if (!params->chan) {
856 ret = -EINVAL;
857 rcu_read_unlock();
858 goto out_unlock;
859 } else {
860 need_offchan = true;
861 }
862 rcu_read_unlock();
863 }
864
865 if (need_offchan && !params->offchan) {
866 ret = -EBUSY;
867 goto out_unlock;
868 }
869
870 skb = dev_alloc_skb(local->hw.extra_tx_headroom + params->len);
871 if (!skb) {
872 ret = -ENOMEM;
873 goto out_unlock;
874 }
875 skb_reserve(skb, local->hw.extra_tx_headroom);
876
877 data = skb_put(skb, params->len);
878 memcpy(data, params->buf, params->len);
879
880 /* Update CSA counters */
881 if (sdata->vif.csa_active &&
882 (sdata->vif.type == NL80211_IFTYPE_AP ||
883 sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
884 sdata->vif.type == NL80211_IFTYPE_ADHOC) &&
885 params->n_csa_offsets) {
886 int i;
887 struct beacon_data *beacon = NULL;
888
889 rcu_read_lock();
890
891 if (sdata->vif.type == NL80211_IFTYPE_AP)
892 beacon = rcu_dereference(sdata->u.ap.beacon);
893 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
894 beacon = rcu_dereference(sdata->u.ibss.presp);
895 else if (ieee80211_vif_is_mesh(&sdata->vif))
896 beacon = rcu_dereference(sdata->u.mesh.beacon);
897
898 if (beacon)
899 for (i = 0; i < params->n_csa_offsets; i++)
900 data[params->csa_offsets[i]] =
901 beacon->csa_current_counter;
902
903 rcu_read_unlock();
904 }
905
906 IEEE80211_SKB_CB(skb)->flags = flags;
907
908 skb->dev = sdata->dev;
909
910 if (!params->dont_wait_for_ack) {
911 /* make a copy to preserve the frame contents
912 * in case of encryption.
913 */
5ee00dbd
JB
914 ret = ieee80211_attach_ack_skb(local, skb, cookie, GFP_KERNEL);
915 if (ret) {
a2fcfccb
JB
916 kfree_skb(skb);
917 goto out_unlock;
918 }
919 } else {
920 /* Assign a dummy non-zero cookie, it's not sent to
921 * userspace in this case but we rely on its value
922 * internally in the need_offchan case to distinguish
923 * mgmt-tx from remain-on-channel.
924 */
925 *cookie = 0xffffffff;
926 }
927
928 if (!need_offchan) {
929 ieee80211_tx_skb(sdata, skb);
930 ret = 0;
931 goto out_unlock;
932 }
933
934 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_TX_OFFCHAN |
935 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
936 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
937 IEEE80211_SKB_CB(skb)->hw_queue =
938 local->hw.offchannel_tx_hw_queue;
939
940 /* This will handle all kinds of coalescing and immediate TX */
941 ret = ieee80211_start_roc_work(local, sdata, params->chan,
942 params->wait, cookie, skb,
943 IEEE80211_ROC_TYPE_MGMT_TX);
944 if (ret)
945 ieee80211_free_txskb(&local->hw, skb);
946 out_unlock:
947 mutex_unlock(&local->mtx);
948 return ret;
949}
950
951int ieee80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
952 struct wireless_dev *wdev, u64 cookie)
953{
954 struct ieee80211_local *local = wiphy_priv(wiphy);
955
956 return ieee80211_cancel_roc(local, cookie, true);
957}
958
2eb278e0 959void ieee80211_roc_setup(struct ieee80211_local *local)
21f83589
JB
960{
961 INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start);
962 INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done);
aaa016cc 963 INIT_DELAYED_WORK(&local->roc_work, ieee80211_roc_work);
2eb278e0
JB
964 INIT_LIST_HEAD(&local->roc_list);
965}
966
c8f994ee
JB
967void ieee80211_roc_purge(struct ieee80211_local *local,
968 struct ieee80211_sub_if_data *sdata)
2eb278e0 969{
2eb278e0 970 struct ieee80211_roc_work *roc, *tmp;
aaa016cc 971 bool work_to_do = false;
2eb278e0
JB
972
973 mutex_lock(&local->mtx);
974 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
c8f994ee 975 if (sdata && roc->sdata != sdata)
2eb278e0
JB
976 continue;
977
aaa016cc
JB
978 if (roc->started) {
979 if (local->ops->remain_on_channel) {
980 /* can race, so ignore return value */
981 drv_cancel_remain_on_channel(local);
982 ieee80211_roc_notify_destroy(roc);
983 } else {
984 roc->abort = true;
985 work_to_do = true;
986 }
2eb278e0 987 } else {
aaa016cc 988 ieee80211_roc_notify_destroy(roc);
2eb278e0
JB
989 }
990 }
aaa016cc
JB
991 if (work_to_do)
992 __ieee80211_roc_work(local);
993 mutex_unlock(&local->mtx);
21f83589 994}