Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 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 | */ |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 15 | #include <linux/export.h> |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 16 | #include <net/mac80211.h> |
| 17 | #include "ieee80211_i.h" |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 18 | #include "driver-ops.h" |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 19 | |
| 20 | /* |
Ben Greear | b23b025 | 2011-02-04 11:54:17 -0800 | [diff] [blame] | 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. |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 26 | */ |
Rajkumar Manoharan | 559cef9 | 2012-06-18 19:03:52 +0530 | [diff] [blame] | 27 | static void ieee80211_offchannel_ps_enable(struct ieee80211_sub_if_data *sdata) |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 28 | { |
| 29 | struct ieee80211_local *local = sdata->local; |
Luis R. Rodriguez | 4730d59 | 2010-09-16 15:12:31 -0400 | [diff] [blame] | 30 | struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 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); |
Luis R. Rodriguez | 3bc3c0d | 2010-09-16 15:12:33 -0400 | [diff] [blame] | 37 | del_timer_sync(&ifmgd->bcn_mon_timer); |
Luis R. Rodriguez | 4730d59 | 2010-09-16 15:12:31 -0400 | [diff] [blame] | 38 | del_timer_sync(&ifmgd->conn_mon_timer); |
| 39 | |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 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 | |
Rajkumar Manoharan | 559cef9 | 2012-06-18 19:03:52 +0530 | [diff] [blame] | 48 | if (!local->offchannel_ps_enabled || |
Johannes Berg | 30686bf | 2015-06-02 21:39:54 +0200 | [diff] [blame] | 49 | !ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 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 | */ |
Johannes Berg | 076cdcb | 2015-09-24 16:14:55 +0200 | [diff] [blame] | 60 | ieee80211_send_nullfunc(local, sdata, true); |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* inform AP that we are awake again, unless power save is enabled */ |
| 64 | static 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) |
Johannes Berg | 076cdcb | 2015-09-24 16:14:55 +0200 | [diff] [blame] | 69 | ieee80211_send_nullfunc(local, sdata, false); |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 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 | */ |
Ben Greear | b23b025 | 2011-02-04 11:54:17 -0800 | [diff] [blame] | 84 | /* TODO: Only set hardware if CONF_PS changed? |
| 85 | * TODO: Should we set offchannel_ps_enabled to false? |
| 86 | */ |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 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 | */ |
Johannes Berg | 076cdcb | 2015-09-24 16:14:55 +0200 | [diff] [blame] | 96 | ieee80211_send_nullfunc(local, sdata, false); |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 97 | mod_timer(&local->dynamic_ps_timer, jiffies + |
| 98 | msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout)); |
| 99 | } |
Luis R. Rodriguez | 4730d59 | 2010-09-16 15:12:31 -0400 | [diff] [blame] | 100 | |
Luis R. Rodriguez | 3bc3c0d | 2010-09-16 15:12:33 -0400 | [diff] [blame] | 101 | ieee80211_sta_reset_beacon_monitor(sdata); |
Luis R. Rodriguez | 4730d59 | 2010-09-16 15:12:31 -0400 | [diff] [blame] | 102 | ieee80211_sta_reset_conn_monitor(sdata); |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Stanislaw Gruszka | aacde9e | 2012-12-20 14:41:18 +0100 | [diff] [blame] | 105 | void ieee80211_offchannel_stop_vifs(struct ieee80211_local *local) |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 106 | { |
| 107 | struct ieee80211_sub_if_data *sdata; |
| 108 | |
Johannes Berg | fe57d9f | 2012-07-26 14:55:08 +0200 | [diff] [blame] | 109 | if (WARN_ON(local->use_chanctx)) |
| 110 | return; |
| 111 | |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 112 | /* |
Ben Greear | b23b025 | 2011-02-04 11:54:17 -0800 | [diff] [blame] | 113 | * notify the AP about us leaving the channel and stop all |
| 114 | * STA interfaces. |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 115 | */ |
Seth Forshee | 6c17b77 | 2013-02-11 11:21:07 -0600 | [diff] [blame] | 116 | |
Seth Forshee | 9c35d7d | 2013-02-11 11:21:08 -0600 | [diff] [blame] | 117 | /* |
| 118 | * Stop queues and transmit all frames queued by the driver |
| 119 | * before sending nullfunc to enable powersave at the AP. |
| 120 | */ |
Johannes Berg | 445ea4e | 2013-02-13 12:25:28 +0100 | [diff] [blame] | 121 | ieee80211_stop_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP, |
Luciano Coelho | cca07b0 | 2014-06-13 16:30:05 +0300 | [diff] [blame] | 122 | IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL, |
| 123 | false); |
Emmanuel Grumbach | 3b24f4c | 2015-01-07 15:42:39 +0200 | [diff] [blame] | 124 | ieee80211_flush_queues(local, NULL, false); |
Seth Forshee | 6c17b77 | 2013-02-11 11:21:07 -0600 | [diff] [blame] | 125 | |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 126 | mutex_lock(&local->iflist_mtx); |
| 127 | list_for_each_entry(sdata, &local->interfaces, list) { |
| 128 | if (!ieee80211_sdata_running(sdata)) |
| 129 | continue; |
| 130 | |
Johannes Berg | f142c6b | 2012-06-18 20:07:15 +0200 | [diff] [blame] | 131 | if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) |
| 132 | continue; |
| 133 | |
Ben Greear | b23b025 | 2011-02-04 11:54:17 -0800 | [diff] [blame] | 134 | if (sdata->vif.type != NL80211_IFTYPE_MONITOR) |
Johannes Berg | 5b714c6 | 2010-08-27 13:45:28 +0200 | [diff] [blame] | 135 | set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state); |
Ben Greear | b23b025 | 2011-02-04 11:54:17 -0800 | [diff] [blame] | 136 | |
| 137 | /* Check to see if we should disable beaconing. */ |
Johannes Berg | d6a8322 | 2012-12-14 14:06:28 +0100 | [diff] [blame] | 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; |
Ben Greear | b23b025 | 2011-02-04 11:54:17 -0800 | [diff] [blame] | 142 | ieee80211_bss_info_change_notify( |
| 143 | sdata, BSS_CHANGED_BEACON_ENABLED); |
Johannes Berg | d6a8322 | 2012-12-14 14:06:28 +0100 | [diff] [blame] | 144 | } |
Ben Greear | b23b025 | 2011-02-04 11:54:17 -0800 | [diff] [blame] | 145 | |
Seth Forshee | 6c17b77 | 2013-02-11 11:21:07 -0600 | [diff] [blame] | 146 | if (sdata->vif.type == NL80211_IFTYPE_STATION && |
| 147 | sdata->u.mgd.associated) |
| 148 | ieee80211_offchannel_ps_enable(sdata); |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 149 | } |
| 150 | mutex_unlock(&local->iflist_mtx); |
| 151 | } |
| 152 | |
Stanislaw Gruszka | aacde9e | 2012-12-20 14:41:18 +0100 | [diff] [blame] | 153 | void ieee80211_offchannel_return(struct ieee80211_local *local) |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 154 | { |
| 155 | struct ieee80211_sub_if_data *sdata; |
| 156 | |
Johannes Berg | fe57d9f | 2012-07-26 14:55:08 +0200 | [diff] [blame] | 157 | if (WARN_ON(local->use_chanctx)) |
| 158 | return; |
| 159 | |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 160 | mutex_lock(&local->iflist_mtx); |
| 161 | list_for_each_entry(sdata, &local->interfaces, list) { |
Johannes Berg | f142c6b | 2012-06-18 20:07:15 +0200 | [diff] [blame] | 162 | if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) |
| 163 | continue; |
| 164 | |
Eliad Peller | f6e8cb7 | 2011-12-23 01:48:06 +0200 | [diff] [blame] | 165 | if (sdata->vif.type != NL80211_IFTYPE_MONITOR) |
| 166 | clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state); |
| 167 | |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 168 | if (!ieee80211_sdata_running(sdata)) |
| 169 | continue; |
| 170 | |
| 171 | /* Tell AP we're back */ |
Stanislaw Gruszka | aacde9e | 2012-12-20 14:41:18 +0100 | [diff] [blame] | 172 | if (sdata->vif.type == NL80211_IFTYPE_STATION && |
| 173 | sdata->u.mgd.associated) |
| 174 | ieee80211_offchannel_ps_disable(sdata); |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 175 | |
Johannes Berg | d6a8322 | 2012-12-14 14:06:28 +0100 | [diff] [blame] | 176 | if (test_and_clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, |
| 177 | &sdata->state)) { |
| 178 | sdata->vif.bss_conf.enable_beacon = true; |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 179 | ieee80211_bss_info_change_notify( |
| 180 | sdata, BSS_CHANGED_BEACON_ENABLED); |
Johannes Berg | d6a8322 | 2012-12-14 14:06:28 +0100 | [diff] [blame] | 181 | } |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 182 | } |
| 183 | mutex_unlock(&local->iflist_mtx); |
Seth Forshee | 6c17b77 | 2013-02-11 11:21:07 -0600 | [diff] [blame] | 184 | |
Johannes Berg | 445ea4e | 2013-02-13 12:25:28 +0100 | [diff] [blame] | 185 | ieee80211_wake_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP, |
Luciano Coelho | cca07b0 | 2014-06-13 16:30:05 +0300 | [diff] [blame] | 186 | IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL, |
| 187 | false); |
Jouni Malinen | b203ffc | 2009-12-23 13:15:40 +0100 | [diff] [blame] | 188 | } |
Johannes Berg | 21f8358 | 2010-12-18 17:20:47 +0100 | [diff] [blame] | 189 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 190 | static void ieee80211_roc_notify_destroy(struct ieee80211_roc_work *roc) |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 191 | { |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 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 | |
| 209 | static 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 | |
Ilan Peer | 1b89452 | 2015-12-06 21:19:15 +0200 | [diff] [blame] | 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) |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 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 | |
| 240 | static 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 | |
| 252 | static void ieee80211_handle_roc_started(struct ieee80211_roc_work *roc, |
| 253 | unsigned long start_time) |
| 254 | { |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 255 | if (WARN_ON(roc->notified)) |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 256 | return; |
| 257 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 258 | roc->start_time = start_time; |
| 259 | roc->started = true; |
| 260 | roc->hw_begun = true; |
| 261 | |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 262 | if (roc->mgmt_tx_cookie) { |
| 263 | if (!WARN_ON(!roc->frame)) { |
Johannes Berg | 55de908 | 2012-07-26 17:24:39 +0200 | [diff] [blame] | 264 | ieee80211_tx_skb_tid_band(roc->sdata, roc->frame, 7, |
| 265 | roc->chan->band); |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 266 | roc->frame = NULL; |
| 267 | } |
| 268 | } else { |
Johannes Berg | 50febf6 | 2012-10-26 16:13:06 +0200 | [diff] [blame] | 269 | cfg80211_ready_on_channel(&roc->sdata->wdev, roc->cookie, |
Johannes Berg | 42d97a5 | 2012-11-08 18:31:02 +0100 | [diff] [blame] | 270 | roc->chan, roc->req_duration, |
| 271 | GFP_KERNEL); |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | roc->notified = true; |
| 275 | } |
| 276 | |
Johannes Berg | 21f8358 | 2010-12-18 17:20:47 +0100 | [diff] [blame] | 277 | static void ieee80211_hw_roc_start(struct work_struct *work) |
| 278 | { |
| 279 | struct ieee80211_local *local = |
| 280 | container_of(work, struct ieee80211_local, hw_roc_start); |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 281 | struct ieee80211_roc_work *roc; |
Johannes Berg | 21f8358 | 2010-12-18 17:20:47 +0100 | [diff] [blame] | 282 | |
| 283 | mutex_lock(&local->mtx); |
| 284 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 285 | list_for_each_entry(roc, &local->roc_list, list) { |
| 286 | if (!roc->started) |
| 287 | break; |
Johannes Berg | 21f8358 | 2010-12-18 17:20:47 +0100 | [diff] [blame] | 288 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 289 | ieee80211_handle_roc_started(roc, local->hw_roc_start_time); |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 290 | } |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 291 | |
Johannes Berg | 21f8358 | 2010-12-18 17:20:47 +0100 | [diff] [blame] | 292 | mutex_unlock(&local->mtx); |
| 293 | } |
| 294 | |
| 295 | void ieee80211_ready_on_channel(struct ieee80211_hw *hw) |
| 296 | { |
| 297 | struct ieee80211_local *local = hw_to_local(hw); |
| 298 | |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 299 | local->hw_roc_start_time = jiffies; |
| 300 | |
Johannes Berg | 21f8358 | 2010-12-18 17:20:47 +0100 | [diff] [blame] | 301 | trace_api_ready_on_channel(local); |
| 302 | |
| 303 | ieee80211_queue_work(hw, &local->hw_roc_start); |
| 304 | } |
| 305 | EXPORT_SYMBOL_GPL(ieee80211_ready_on_channel); |
| 306 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 307 | static void _ieee80211_start_next_roc(struct ieee80211_local *local) |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 308 | { |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 309 | struct ieee80211_roc_work *roc, *tmp; |
| 310 | enum ieee80211_roc_type type; |
| 311 | u32 min_dur, max_dur; |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 312 | |
| 313 | lockdep_assert_held(&local->mtx); |
| 314 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 315 | if (WARN_ON(list_empty(&local->roc_list))) |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 316 | return; |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 317 | |
| 318 | roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work, |
| 319 | list); |
| 320 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 321 | if (WARN_ON(roc->started)) |
Johannes Berg | 0f6b3f5 | 2012-06-20 20:11:33 +0200 | [diff] [blame] | 322 | return; |
| 323 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 324 | min_dur = roc->duration; |
| 325 | max_dur = roc->duration; |
| 326 | type = roc->type; |
| 327 | |
| 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 | } |
| 337 | |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 338 | if (local->ops->remain_on_channel) { |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 339 | int ret = drv_remain_on_channel(local, roc->sdata, roc->chan, |
| 340 | max_dur, type); |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 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 | */ |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 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; |
| 358 | } |
| 359 | |
| 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; |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 365 | } |
| 366 | } else { |
Johannes Berg | b4b177a | 2014-05-14 15:34:41 +0200 | [diff] [blame] | 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; |
| 375 | |
| 376 | /* start this ROC */ |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 377 | ieee80211_recalc_idle(local); |
| 378 | |
Johannes Berg | b4b177a | 2014-05-14 15:34:41 +0200 | [diff] [blame] | 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 | } |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 385 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 386 | ieee80211_queue_delayed_work(&local->hw, &local->roc_work, |
| 387 | msecs_to_jiffies(min_dur)); |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 388 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 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; |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 393 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 394 | tmp->on_channel = roc->on_channel; |
| 395 | ieee80211_handle_roc_started(tmp, jiffies); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | void 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); |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 419 | } else { |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 420 | /* delay it a bit */ |
| 421 | ieee80211_queue_delayed_work(&local->hw, &local->roc_work, |
| 422 | round_jiffies_relative(HZ/2)); |
| 423 | } |
| 424 | } |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 425 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 426 | static 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; |
| 435 | |
| 436 | roc = list_first_entry_or_null(&local->roc_list, |
| 437 | struct ieee80211_roc_work, list); |
| 438 | if (!roc) |
| 439 | return; |
| 440 | |
| 441 | if (!roc->started) { |
| 442 | WARN_ON(local->use_chanctx); |
| 443 | _ieee80211_start_next_roc(local); |
| 444 | } else { |
| 445 | on_channel = roc->on_channel; |
| 446 | if (ieee80211_recalc_sw_work(local, jiffies)) |
| 447 | return; |
| 448 | |
| 449 | /* careful - roc pointer became invalid during recalc */ |
| 450 | |
| 451 | if (!on_channel) { |
Emmanuel Grumbach | 3b24f4c | 2015-01-07 15:42:39 +0200 | [diff] [blame] | 452 | ieee80211_flush_queues(local, NULL, false); |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 453 | |
| 454 | local->tmp_channel = NULL; |
| 455 | ieee80211_hw_config(local, 0); |
| 456 | |
Stanislaw Gruszka | aacde9e | 2012-12-20 14:41:18 +0100 | [diff] [blame] | 457 | ieee80211_offchannel_return(local); |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | ieee80211_recalc_idle(local); |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 461 | ieee80211_start_next_roc(local); |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 462 | } |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 463 | } |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 464 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 465 | static 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); |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 472 | mutex_unlock(&local->mtx); |
| 473 | } |
| 474 | |
Johannes Berg | 21f8358 | 2010-12-18 17:20:47 +0100 | [diff] [blame] | 475 | static 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); |
| 479 | |
| 480 | mutex_lock(&local->mtx); |
| 481 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 482 | ieee80211_end_finished_rocs(local, jiffies); |
Johannes Berg | 71ecfa1 | 2012-05-31 15:09:27 +0200 | [diff] [blame] | 483 | |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 484 | /* if there's another roc, start it now */ |
| 485 | ieee80211_start_next_roc(local); |
Johannes Berg | 21f8358 | 2010-12-18 17:20:47 +0100 | [diff] [blame] | 486 | |
Johannes Berg | 21f8358 | 2010-12-18 17:20:47 +0100 | [diff] [blame] | 487 | mutex_unlock(&local->mtx); |
| 488 | } |
| 489 | |
| 490 | void 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 | } |
| 498 | EXPORT_SYMBOL_GPL(ieee80211_remain_on_channel_expired); |
| 499 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 500 | static bool |
| 501 | ieee80211_coalesce_hw_started_roc(struct ieee80211_local *local, |
| 502 | struct ieee80211_roc_work *new_roc, |
| 503 | struct ieee80211_roc_work *cur_roc) |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 504 | { |
| 505 | unsigned long now = jiffies; |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 506 | unsigned long remaining; |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 507 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 508 | if (WARN_ON(!cur_roc->started)) |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 509 | return false; |
| 510 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 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) |
| 515 | return false; |
| 516 | |
| 517 | remaining = cur_roc->start_time + |
| 518 | msecs_to_jiffies(cur_roc->duration) - |
| 519 | now; |
| 520 | |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 521 | /* if it doesn't fit entirely, schedule a new one */ |
| 522 | if (new_roc->duration > jiffies_to_msecs(remaining)) |
| 523 | return false; |
| 524 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 525 | /* add just after the current one so we combine their finish later */ |
| 526 | list_add(&new_roc->list, &cur_roc->list); |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 527 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 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 | */ |
| 532 | if (cur_roc->hw_begun) |
| 533 | ieee80211_handle_roc_started(new_roc, now); |
| 534 | |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 535 | return true; |
| 536 | } |
| 537 | |
| 538 | static int ieee80211_start_roc_work(struct ieee80211_local *local, |
| 539 | struct ieee80211_sub_if_data *sdata, |
| 540 | struct ieee80211_channel *channel, |
| 541 | unsigned int duration, u64 *cookie, |
| 542 | struct sk_buff *txskb, |
| 543 | enum ieee80211_roc_type type) |
| 544 | { |
| 545 | struct ieee80211_roc_work *roc, *tmp; |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 546 | bool queued = false, combine_started = true; |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 547 | int ret; |
| 548 | |
| 549 | lockdep_assert_held(&local->mtx); |
| 550 | |
| 551 | if (local->use_chanctx && !local->ops->remain_on_channel) |
| 552 | return -EOPNOTSUPP; |
| 553 | |
| 554 | roc = kzalloc(sizeof(*roc), GFP_KERNEL); |
| 555 | if (!roc) |
| 556 | return -ENOMEM; |
| 557 | |
| 558 | /* |
| 559 | * If the duration is zero, then the driver |
| 560 | * wouldn't actually do anything. Set it to |
| 561 | * 10 for now. |
| 562 | * |
| 563 | * TODO: cancel the off-channel operation |
| 564 | * when we get the SKB's TX status and |
| 565 | * the wait time was zero before. |
| 566 | */ |
| 567 | if (!duration) |
| 568 | duration = 10; |
| 569 | |
| 570 | roc->chan = channel; |
| 571 | roc->duration = duration; |
| 572 | roc->req_duration = duration; |
| 573 | roc->frame = txskb; |
| 574 | roc->type = type; |
| 575 | roc->sdata = sdata; |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 576 | |
| 577 | /* |
| 578 | * cookie is either the roc cookie (for normal roc) |
| 579 | * or the SKB (for mgmt TX) |
| 580 | */ |
| 581 | if (!txskb) { |
| 582 | roc->cookie = ieee80211_mgmt_tx_cookie(local); |
| 583 | *cookie = roc->cookie; |
| 584 | } else { |
| 585 | roc->mgmt_tx_cookie = *cookie; |
| 586 | } |
| 587 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 588 | /* if there's no need to queue, handle it immediately */ |
| 589 | if (list_empty(&local->roc_list) && |
| 590 | !local->scanning && !ieee80211_is_radar_required(local)) { |
| 591 | /* if not HW assist, just queue & schedule work */ |
| 592 | if (!local->ops->remain_on_channel) { |
| 593 | list_add_tail(&roc->list, &local->roc_list); |
| 594 | ieee80211_queue_delayed_work(&local->hw, |
| 595 | &local->roc_work, 0); |
| 596 | } else { |
| 597 | /* otherwise actually kick it off here |
| 598 | * (for error handling) |
| 599 | */ |
| 600 | ret = drv_remain_on_channel(local, sdata, channel, |
| 601 | duration, type); |
| 602 | if (ret) { |
| 603 | kfree(roc); |
| 604 | return ret; |
| 605 | } |
| 606 | roc->started = true; |
| 607 | list_add_tail(&roc->list, &local->roc_list); |
| 608 | } |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 609 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 610 | return 0; |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 611 | } |
| 612 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 613 | /* otherwise handle queueing */ |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 614 | |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 615 | list_for_each_entry(tmp, &local->roc_list, list) { |
| 616 | if (tmp->chan != channel || tmp->sdata != sdata) |
| 617 | continue; |
| 618 | |
| 619 | /* |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 620 | * Extend this ROC if possible: If it hasn't started, add |
| 621 | * just after the new one to combine. |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 622 | */ |
| 623 | if (!tmp->started) { |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 624 | list_add(&roc->list, &tmp->list); |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 625 | queued = true; |
| 626 | break; |
| 627 | } |
| 628 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 629 | if (!combine_started) |
| 630 | continue; |
| 631 | |
| 632 | if (!local->ops->remain_on_channel) { |
| 633 | /* If there's no hardware remain-on-channel, and |
| 634 | * doing so won't push us over the maximum r-o-c |
| 635 | * we allow, then we can just add the new one to |
| 636 | * the list and mark it as having started now. |
| 637 | * If it would push over the limit, don't try to |
| 638 | * combine with other started ones (that haven't |
| 639 | * been running as long) but potentially sort it |
| 640 | * with others that had the same fate. |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 641 | */ |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 642 | unsigned long now = jiffies; |
| 643 | u32 elapsed = jiffies_to_msecs(now - tmp->start_time); |
| 644 | struct wiphy *wiphy = local->hw.wiphy; |
| 645 | u32 max_roc = wiphy->max_remain_on_channel_duration; |
| 646 | |
| 647 | if (elapsed + roc->duration > max_roc) { |
| 648 | combine_started = false; |
| 649 | continue; |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 652 | list_add(&roc->list, &tmp->list); |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 653 | queued = true; |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 654 | roc->on_channel = tmp->on_channel; |
| 655 | ieee80211_handle_roc_started(roc, now); |
Johannes Berg | e9db455 | 2015-12-08 13:16:41 +0100 | [diff] [blame^] | 656 | ieee80211_recalc_sw_work(local, now); |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 657 | break; |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 658 | } |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 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; |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 669 | } |
| 670 | |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 671 | if (!queued) |
| 672 | list_add_tail(&roc->list, &local->roc_list); |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | int 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 | |
| 694 | static 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 | |
Johannes Berg | 7d37fcd | 2015-12-01 23:15:26 +0100 | [diff] [blame] | 700 | if (!cookie) |
| 701 | return -ENOENT; |
| 702 | |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 703 | mutex_lock(&local->mtx); |
| 704 | list_for_each_entry_safe(roc, tmp, &local->roc_list, list) { |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 705 | if (!mgmt_tx && roc->cookie != cookie) |
| 706 | continue; |
| 707 | else if (mgmt_tx && roc->mgmt_tx_cookie != cookie) |
| 708 | continue; |
| 709 | |
| 710 | found = roc; |
| 711 | break; |
| 712 | } |
| 713 | |
| 714 | if (!found) { |
| 715 | mutex_unlock(&local->mtx); |
| 716 | return -ENOENT; |
| 717 | } |
| 718 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 719 | if (!found->started) { |
| 720 | ieee80211_roc_notify_destroy(found); |
| 721 | goto out_unlock; |
| 722 | } |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 723 | |
| 724 | if (local->ops->remain_on_channel) { |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 725 | ret = drv_cancel_remain_on_channel(local); |
| 726 | if (WARN_ON_ONCE(ret)) { |
| 727 | mutex_unlock(&local->mtx); |
| 728 | return ret; |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 729 | } |
| 730 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 731 | /* TODO: |
| 732 | * if multiple items were combined here then we really shouldn't |
| 733 | * cancel them all - we should wait for as much time as needed |
| 734 | * for the longest remaining one, and only then cancel ... |
| 735 | */ |
| 736 | list_for_each_entry_safe(roc, tmp, &local->roc_list, list) { |
| 737 | if (!roc->started) |
| 738 | break; |
| 739 | if (roc == found) |
| 740 | found = NULL; |
| 741 | ieee80211_roc_notify_destroy(roc); |
| 742 | } |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 743 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 744 | /* that really must not happen - it was started */ |
| 745 | WARN_ON(found); |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 746 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 747 | ieee80211_start_next_roc(local); |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 748 | } else { |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 749 | /* go through work struct to return to the operating channel */ |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 750 | found->abort = true; |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 751 | mod_delayed_work(local->workqueue, &local->roc_work, 0); |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 752 | } |
| 753 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 754 | out_unlock: |
| 755 | mutex_unlock(&local->mtx); |
| 756 | |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | int ieee80211_cancel_remain_on_channel(struct wiphy *wiphy, |
| 761 | struct wireless_dev *wdev, u64 cookie) |
| 762 | { |
| 763 | struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev); |
| 764 | struct ieee80211_local *local = sdata->local; |
| 765 | |
| 766 | return ieee80211_cancel_roc(local, cookie, false); |
| 767 | } |
| 768 | |
| 769 | int ieee80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev, |
| 770 | struct cfg80211_mgmt_tx_params *params, u64 *cookie) |
| 771 | { |
| 772 | struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev); |
| 773 | struct ieee80211_local *local = sdata->local; |
Johannes Berg | 5ee00db | 2015-11-24 14:25:49 +0100 | [diff] [blame] | 774 | struct sk_buff *skb; |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 775 | struct sta_info *sta; |
| 776 | const struct ieee80211_mgmt *mgmt = (void *)params->buf; |
| 777 | bool need_offchan = false; |
| 778 | u32 flags; |
| 779 | int ret; |
| 780 | u8 *data; |
| 781 | |
| 782 | if (params->dont_wait_for_ack) |
| 783 | flags = IEEE80211_TX_CTL_NO_ACK; |
| 784 | else |
| 785 | flags = IEEE80211_TX_INTFL_NL80211_FRAME_TX | |
| 786 | IEEE80211_TX_CTL_REQ_TX_STATUS; |
| 787 | |
| 788 | if (params->no_cck) |
| 789 | flags |= IEEE80211_TX_CTL_NO_CCK_RATE; |
| 790 | |
| 791 | switch (sdata->vif.type) { |
| 792 | case NL80211_IFTYPE_ADHOC: |
| 793 | if (!sdata->vif.bss_conf.ibss_joined) |
| 794 | need_offchan = true; |
| 795 | /* fall through */ |
| 796 | #ifdef CONFIG_MAC80211_MESH |
| 797 | case NL80211_IFTYPE_MESH_POINT: |
| 798 | if (ieee80211_vif_is_mesh(&sdata->vif) && |
| 799 | !sdata->u.mesh.mesh_id_len) |
| 800 | need_offchan = true; |
| 801 | /* fall through */ |
| 802 | #endif |
| 803 | case NL80211_IFTYPE_AP: |
| 804 | case NL80211_IFTYPE_AP_VLAN: |
| 805 | case NL80211_IFTYPE_P2P_GO: |
| 806 | if (sdata->vif.type != NL80211_IFTYPE_ADHOC && |
| 807 | !ieee80211_vif_is_mesh(&sdata->vif) && |
| 808 | !rcu_access_pointer(sdata->bss->beacon)) |
| 809 | need_offchan = true; |
| 810 | if (!ieee80211_is_action(mgmt->frame_control) || |
| 811 | mgmt->u.action.category == WLAN_CATEGORY_PUBLIC || |
| 812 | mgmt->u.action.category == WLAN_CATEGORY_SELF_PROTECTED || |
| 813 | mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) |
| 814 | break; |
| 815 | rcu_read_lock(); |
| 816 | sta = sta_info_get(sdata, mgmt->da); |
| 817 | rcu_read_unlock(); |
| 818 | if (!sta) |
| 819 | return -ENOLINK; |
| 820 | break; |
| 821 | case NL80211_IFTYPE_STATION: |
| 822 | case NL80211_IFTYPE_P2P_CLIENT: |
| 823 | sdata_lock(sdata); |
| 824 | if (!sdata->u.mgd.associated || |
| 825 | (params->offchan && params->wait && |
| 826 | local->ops->remain_on_channel && |
| 827 | memcmp(sdata->u.mgd.associated->bssid, |
| 828 | mgmt->bssid, ETH_ALEN))) |
| 829 | need_offchan = true; |
| 830 | sdata_unlock(sdata); |
| 831 | break; |
| 832 | case NL80211_IFTYPE_P2P_DEVICE: |
| 833 | need_offchan = true; |
| 834 | break; |
| 835 | default: |
| 836 | return -EOPNOTSUPP; |
| 837 | } |
| 838 | |
| 839 | /* configurations requiring offchan cannot work if no channel has been |
| 840 | * specified |
| 841 | */ |
| 842 | if (need_offchan && !params->chan) |
| 843 | return -EINVAL; |
| 844 | |
| 845 | mutex_lock(&local->mtx); |
| 846 | |
| 847 | /* Check if the operating channel is the requested channel */ |
| 848 | if (!need_offchan) { |
| 849 | struct ieee80211_chanctx_conf *chanctx_conf; |
| 850 | |
| 851 | rcu_read_lock(); |
| 852 | chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf); |
| 853 | |
| 854 | if (chanctx_conf) { |
| 855 | need_offchan = params->chan && |
| 856 | (params->chan != |
| 857 | chanctx_conf->def.chan); |
| 858 | } else if (!params->chan) { |
| 859 | ret = -EINVAL; |
| 860 | rcu_read_unlock(); |
| 861 | goto out_unlock; |
| 862 | } else { |
| 863 | need_offchan = true; |
| 864 | } |
| 865 | rcu_read_unlock(); |
| 866 | } |
| 867 | |
| 868 | if (need_offchan && !params->offchan) { |
| 869 | ret = -EBUSY; |
| 870 | goto out_unlock; |
| 871 | } |
| 872 | |
| 873 | skb = dev_alloc_skb(local->hw.extra_tx_headroom + params->len); |
| 874 | if (!skb) { |
| 875 | ret = -ENOMEM; |
| 876 | goto out_unlock; |
| 877 | } |
| 878 | skb_reserve(skb, local->hw.extra_tx_headroom); |
| 879 | |
| 880 | data = skb_put(skb, params->len); |
| 881 | memcpy(data, params->buf, params->len); |
| 882 | |
| 883 | /* Update CSA counters */ |
| 884 | if (sdata->vif.csa_active && |
| 885 | (sdata->vif.type == NL80211_IFTYPE_AP || |
| 886 | sdata->vif.type == NL80211_IFTYPE_MESH_POINT || |
| 887 | sdata->vif.type == NL80211_IFTYPE_ADHOC) && |
| 888 | params->n_csa_offsets) { |
| 889 | int i; |
| 890 | struct beacon_data *beacon = NULL; |
| 891 | |
| 892 | rcu_read_lock(); |
| 893 | |
| 894 | if (sdata->vif.type == NL80211_IFTYPE_AP) |
| 895 | beacon = rcu_dereference(sdata->u.ap.beacon); |
| 896 | else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) |
| 897 | beacon = rcu_dereference(sdata->u.ibss.presp); |
| 898 | else if (ieee80211_vif_is_mesh(&sdata->vif)) |
| 899 | beacon = rcu_dereference(sdata->u.mesh.beacon); |
| 900 | |
| 901 | if (beacon) |
| 902 | for (i = 0; i < params->n_csa_offsets; i++) |
| 903 | data[params->csa_offsets[i]] = |
| 904 | beacon->csa_current_counter; |
| 905 | |
| 906 | rcu_read_unlock(); |
| 907 | } |
| 908 | |
| 909 | IEEE80211_SKB_CB(skb)->flags = flags; |
| 910 | |
| 911 | skb->dev = sdata->dev; |
| 912 | |
| 913 | if (!params->dont_wait_for_ack) { |
| 914 | /* make a copy to preserve the frame contents |
| 915 | * in case of encryption. |
| 916 | */ |
Johannes Berg | 5ee00db | 2015-11-24 14:25:49 +0100 | [diff] [blame] | 917 | ret = ieee80211_attach_ack_skb(local, skb, cookie, GFP_KERNEL); |
| 918 | if (ret) { |
Johannes Berg | a2fcfccb | 2015-11-23 17:18:35 +0100 | [diff] [blame] | 919 | kfree_skb(skb); |
| 920 | goto out_unlock; |
| 921 | } |
| 922 | } else { |
| 923 | /* Assign a dummy non-zero cookie, it's not sent to |
| 924 | * userspace in this case but we rely on its value |
| 925 | * internally in the need_offchan case to distinguish |
| 926 | * mgmt-tx from remain-on-channel. |
| 927 | */ |
| 928 | *cookie = 0xffffffff; |
| 929 | } |
| 930 | |
| 931 | if (!need_offchan) { |
| 932 | ieee80211_tx_skb(sdata, skb); |
| 933 | ret = 0; |
| 934 | goto out_unlock; |
| 935 | } |
| 936 | |
| 937 | IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_TX_OFFCHAN | |
| 938 | IEEE80211_TX_INTFL_OFFCHAN_TX_OK; |
| 939 | if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) |
| 940 | IEEE80211_SKB_CB(skb)->hw_queue = |
| 941 | local->hw.offchannel_tx_hw_queue; |
| 942 | |
| 943 | /* This will handle all kinds of coalescing and immediate TX */ |
| 944 | ret = ieee80211_start_roc_work(local, sdata, params->chan, |
| 945 | params->wait, cookie, skb, |
| 946 | IEEE80211_ROC_TYPE_MGMT_TX); |
| 947 | if (ret) |
| 948 | ieee80211_free_txskb(&local->hw, skb); |
| 949 | out_unlock: |
| 950 | mutex_unlock(&local->mtx); |
| 951 | return ret; |
| 952 | } |
| 953 | |
| 954 | int ieee80211_mgmt_tx_cancel_wait(struct wiphy *wiphy, |
| 955 | struct wireless_dev *wdev, u64 cookie) |
| 956 | { |
| 957 | struct ieee80211_local *local = wiphy_priv(wiphy); |
| 958 | |
| 959 | return ieee80211_cancel_roc(local, cookie, true); |
| 960 | } |
| 961 | |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 962 | void ieee80211_roc_setup(struct ieee80211_local *local) |
Johannes Berg | 21f8358 | 2010-12-18 17:20:47 +0100 | [diff] [blame] | 963 | { |
| 964 | INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start); |
| 965 | INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done); |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 966 | INIT_DELAYED_WORK(&local->roc_work, ieee80211_roc_work); |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 967 | INIT_LIST_HEAD(&local->roc_list); |
| 968 | } |
| 969 | |
Johannes Berg | c8f994e | 2013-03-27 22:49:19 +0100 | [diff] [blame] | 970 | void ieee80211_roc_purge(struct ieee80211_local *local, |
| 971 | struct ieee80211_sub_if_data *sdata) |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 972 | { |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 973 | struct ieee80211_roc_work *roc, *tmp; |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 974 | bool work_to_do = false; |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 975 | |
| 976 | mutex_lock(&local->mtx); |
| 977 | list_for_each_entry_safe(roc, tmp, &local->roc_list, list) { |
Johannes Berg | c8f994e | 2013-03-27 22:49:19 +0100 | [diff] [blame] | 978 | if (sdata && roc->sdata != sdata) |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 979 | continue; |
| 980 | |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 981 | if (roc->started) { |
| 982 | if (local->ops->remain_on_channel) { |
| 983 | /* can race, so ignore return value */ |
| 984 | drv_cancel_remain_on_channel(local); |
| 985 | ieee80211_roc_notify_destroy(roc); |
| 986 | } else { |
| 987 | roc->abort = true; |
| 988 | work_to_do = true; |
| 989 | } |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 990 | } else { |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 991 | ieee80211_roc_notify_destroy(roc); |
Johannes Berg | 2eb278e | 2012-06-05 14:28:42 +0200 | [diff] [blame] | 992 | } |
| 993 | } |
Johannes Berg | aaa016c | 2015-11-23 23:53:51 +0100 | [diff] [blame] | 994 | if (work_to_do) |
| 995 | __ieee80211_roc_work(local); |
| 996 | mutex_unlock(&local->mtx); |
Johannes Berg | 21f8358 | 2010-12-18 17:20:47 +0100 | [diff] [blame] | 997 | } |