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