blob: 71802e7f7448e887b1028f802e3aea6fbb8d3292 [file] [log] [blame]
Arik Nemtsovd4317252014-09-07 19:18:31 +03001/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2014 Intel Mobile Communications GmbH
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called COPYING.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2014 Intel Mobile Communications GmbH
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +030064#include <linux/etherdevice.h>
Arik Nemtsovd4317252014-09-07 19:18:31 +030065#include "mvm.h"
66#include "time-event.h"
Arik Nemtsovb9dccdb2015-01-22 14:24:44 +020067#include "iwl-io.h"
68#include "iwl-prph.h"
Arik Nemtsovd4317252014-09-07 19:18:31 +030069
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +030070#define TU_TO_US(x) (x * 1024)
71#define TU_TO_MS(x) (TU_TO_US(x) / 1000)
72
Arik Nemtsovd4317252014-09-07 19:18:31 +030073void iwl_mvm_teardown_tdls_peers(struct iwl_mvm *mvm)
74{
75 struct ieee80211_sta *sta;
76 struct iwl_mvm_sta *mvmsta;
77 int i;
78
79 lockdep_assert_held(&mvm->mutex);
80
81 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
82 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
83 lockdep_is_held(&mvm->mutex));
84 if (!sta || IS_ERR(sta) || !sta->tdls)
85 continue;
86
87 mvmsta = iwl_mvm_sta_from_mac80211(sta);
88 ieee80211_tdls_oper_request(mvmsta->vif, sta->addr,
89 NL80211_TDLS_TEARDOWN,
90 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED,
91 GFP_KERNEL);
92 }
93}
94
95int iwl_mvm_tdls_sta_count(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
96{
97 struct ieee80211_sta *sta;
98 struct iwl_mvm_sta *mvmsta;
99 int count = 0;
100 int i;
101
102 lockdep_assert_held(&mvm->mutex);
103
104 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
105 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
106 lockdep_is_held(&mvm->mutex));
107 if (!sta || IS_ERR(sta) || !sta->tdls)
108 continue;
109
110 if (vif) {
111 mvmsta = iwl_mvm_sta_from_mac80211(sta);
112 if (mvmsta->vif != vif)
113 continue;
114 }
115
116 count++;
117 }
118
119 return count;
120}
121
Arik Nemtsov307e4722014-09-15 18:48:59 +0300122static void iwl_mvm_tdls_config(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
123{
124 struct iwl_rx_packet *pkt;
125 struct iwl_tdls_config_res *resp;
126 struct iwl_tdls_config_cmd tdls_cfg_cmd = {};
127 struct iwl_host_cmd cmd = {
128 .id = TDLS_CONFIG_CMD,
129 .flags = CMD_WANT_SKB,
130 .data = { &tdls_cfg_cmd, },
131 .len = { sizeof(struct iwl_tdls_config_cmd), },
132 };
133 struct ieee80211_sta *sta;
134 int ret, i, cnt;
135 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
136
137 lockdep_assert_held(&mvm->mutex);
138
139 tdls_cfg_cmd.id_and_color =
140 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
141 tdls_cfg_cmd.tx_to_ap_tid = IWL_MVM_TDLS_FW_TID;
142 tdls_cfg_cmd.tx_to_ap_ssn = cpu_to_le16(0); /* not used for now */
143
144 /* for now the Tx cmd is empty and unused */
145
146 /* populate TDLS peer data */
147 cnt = 0;
148 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
149 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
150 lockdep_is_held(&mvm->mutex));
151 if (IS_ERR_OR_NULL(sta) || !sta->tdls)
152 continue;
153
154 tdls_cfg_cmd.sta_info[cnt].sta_id = i;
155 tdls_cfg_cmd.sta_info[cnt].tx_to_peer_tid =
156 IWL_MVM_TDLS_FW_TID;
157 tdls_cfg_cmd.sta_info[cnt].tx_to_peer_ssn = cpu_to_le16(0);
158 tdls_cfg_cmd.sta_info[cnt].is_initiator =
159 cpu_to_le32(sta->tdls_initiator ? 1 : 0);
160
161 cnt++;
162 }
163
164 tdls_cfg_cmd.tdls_peer_count = cnt;
165 IWL_DEBUG_TDLS(mvm, "send TDLS config to FW for %d peers\n", cnt);
166
167 ret = iwl_mvm_send_cmd(mvm, &cmd);
168 if (WARN_ON_ONCE(ret))
169 return;
170
171 pkt = cmd.resp_pkt;
172 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
173 IWL_ERR(mvm, "Bad return from TDLS_CONFIG_COMMAND (0x%08X)\n",
174 pkt->hdr.flags);
175 goto exit;
176 }
177
178 if (WARN_ON_ONCE(iwl_rx_packet_payload_len(pkt) != sizeof(*resp)))
179 goto exit;
180
181 /* we don't really care about the response at this point */
182
183exit:
184 iwl_free_resp(&cmd);
185}
186
Arik Nemtsovd4317252014-09-07 19:18:31 +0300187void iwl_mvm_recalc_tdls_state(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
188 bool sta_added)
189{
190 int tdls_sta_cnt = iwl_mvm_tdls_sta_count(mvm, vif);
191
Arik Nemtsov307e4722014-09-15 18:48:59 +0300192 /* when the first peer joins, send a power update first */
193 if (tdls_sta_cnt == 1 && sta_added)
194 iwl_mvm_power_update_mac(mvm);
195
196 /* configure the FW with TDLS peer info */
197 iwl_mvm_tdls_config(mvm, vif);
198
199 /* when the last peer leaves, send a power update last */
200 if (tdls_sta_cnt == 0 && !sta_added)
Arik Nemtsovd4317252014-09-07 19:18:31 +0300201 iwl_mvm_power_update_mac(mvm);
202}
203
204void iwl_mvm_mac_mgd_protect_tdls_discover(struct ieee80211_hw *hw,
205 struct ieee80211_vif *vif)
206{
207 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
208 u32 duration = 2 * vif->bss_conf.dtim_period * vif->bss_conf.beacon_int;
209
210 /*
211 * iwl_mvm_protect_session() reads directly from the device
212 * (the system time), so make sure it is available.
213 */
214 if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PROTECT_TDLS))
215 return;
216
217 mutex_lock(&mvm->mutex);
218 /* Protect the session to hear the TDLS setup response on the channel */
219 iwl_mvm_protect_session(mvm, vif, duration, duration, 100, true);
220 mutex_unlock(&mvm->mutex);
221
222 iwl_mvm_unref(mvm, IWL_MVM_REF_PROTECT_TDLS);
223}
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300224
225static const char *
226iwl_mvm_tdls_cs_state_str(enum iwl_mvm_tdls_cs_state state)
227{
228 switch (state) {
229 case IWL_MVM_TDLS_SW_IDLE:
230 return "IDLE";
231 case IWL_MVM_TDLS_SW_REQ_SENT:
232 return "REQ SENT";
Arik Nemtsov5cb12702015-01-22 12:19:26 +0200233 case IWL_MVM_TDLS_SW_RESP_RCVD:
234 return "RESP RECEIVED";
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300235 case IWL_MVM_TDLS_SW_REQ_RCVD:
236 return "REQ RECEIVED";
237 case IWL_MVM_TDLS_SW_ACTIVE:
238 return "ACTIVE";
239 }
240
241 return NULL;
242}
243
244static void iwl_mvm_tdls_update_cs_state(struct iwl_mvm *mvm,
245 enum iwl_mvm_tdls_cs_state state)
246{
247 if (mvm->tdls_cs.state == state)
248 return;
249
250 IWL_DEBUG_TDLS(mvm, "TDLS channel switch state: %s -> %s\n",
251 iwl_mvm_tdls_cs_state_str(mvm->tdls_cs.state),
252 iwl_mvm_tdls_cs_state_str(state));
253 mvm->tdls_cs.state = state;
254
Arik Nemtsovb9dccdb2015-01-22 14:24:44 +0200255 /* we only send requests to our switching peer - update sent time */
256 if (state == IWL_MVM_TDLS_SW_REQ_SENT)
257 mvm->tdls_cs.peer.sent_timestamp =
258 iwl_read_prph(mvm->trans, DEVICE_SYSTEM_TIME_REG);
259
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300260 if (state == IWL_MVM_TDLS_SW_IDLE)
261 mvm->tdls_cs.cur_sta_id = IWL_MVM_STATION_COUNT;
262}
263
Johannes Berg04168412015-06-23 21:22:09 +0200264void iwl_mvm_rx_tdls_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300265{
266 struct iwl_rx_packet *pkt = rxb_addr(rxb);
267 struct iwl_tdls_channel_switch_notif *notif = (void *)pkt->data;
268 struct ieee80211_sta *sta;
269 unsigned int delay;
270 struct iwl_mvm_sta *mvmsta;
271 struct ieee80211_vif *vif;
272 u32 sta_id = le32_to_cpu(notif->sta_id);
273
274 lockdep_assert_held(&mvm->mutex);
275
276 /* can fail sometimes */
277 if (!le32_to_cpu(notif->status)) {
278 iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
Johannes Berg04168412015-06-23 21:22:09 +0200279 return;
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300280 }
281
282 if (WARN_ON(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +0200283 return;
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300284
285 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
286 lockdep_is_held(&mvm->mutex));
287 /* the station may not be here, but if it is, it must be a TDLS peer */
288 if (IS_ERR_OR_NULL(sta) || WARN_ON(!sta->tdls))
Johannes Berg04168412015-06-23 21:22:09 +0200289 return;
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300290
291 mvmsta = iwl_mvm_sta_from_mac80211(sta);
292 vif = mvmsta->vif;
293
294 /*
295 * Update state and possibly switch again after this is over (DTIM).
296 * Also convert TU to msec.
297 */
298 delay = TU_TO_MS(vif->bss_conf.dtim_period * vif->bss_conf.beacon_int);
299 mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
300 msecs_to_jiffies(delay));
301
302 iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_ACTIVE);
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300303}
304
305static int
306iwl_mvm_tdls_check_action(struct iwl_mvm *mvm,
307 enum iwl_tdls_channel_switch_type type,
Arik Nemtsovb9dccdb2015-01-22 14:24:44 +0200308 const u8 *peer, bool peer_initiator, u32 timestamp)
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300309{
310 bool same_peer = false;
311 int ret = 0;
312
313 /* get the existing peer if it's there */
314 if (mvm->tdls_cs.state != IWL_MVM_TDLS_SW_IDLE &&
315 mvm->tdls_cs.cur_sta_id != IWL_MVM_STATION_COUNT) {
316 struct ieee80211_sta *sta = rcu_dereference_protected(
317 mvm->fw_id_to_mac_id[mvm->tdls_cs.cur_sta_id],
318 lockdep_is_held(&mvm->mutex));
319 if (!IS_ERR_OR_NULL(sta))
320 same_peer = ether_addr_equal(peer, sta->addr);
321 }
322
323 switch (mvm->tdls_cs.state) {
324 case IWL_MVM_TDLS_SW_IDLE:
325 /*
326 * might be spurious packet from the peer after the switch is
327 * already done
328 */
329 if (type == TDLS_MOVE_CH)
330 ret = -EINVAL;
331 break;
332 case IWL_MVM_TDLS_SW_REQ_SENT:
Arik Nemtsov5cb12702015-01-22 12:19:26 +0200333 /* only allow requests from the same peer */
334 if (!same_peer)
335 ret = -EBUSY;
336 else if (type == TDLS_SEND_CHAN_SW_RESP_AND_MOVE_CH &&
337 !peer_initiator)
338 /*
339 * We received a ch-switch request while an outgoing
340 * one is pending. Allow it if the peer is the link
341 * initiator.
342 */
343 ret = -EBUSY;
344 else if (type == TDLS_SEND_CHAN_SW_REQ)
345 /* wait for idle before sending another request */
346 ret = -EBUSY;
Arik Nemtsovb9dccdb2015-01-22 14:24:44 +0200347 else if (timestamp <= mvm->tdls_cs.peer.sent_timestamp)
348 /* we got a stale response - ignore it */
349 ret = -EINVAL;
Arik Nemtsov5cb12702015-01-22 12:19:26 +0200350 break;
351 case IWL_MVM_TDLS_SW_RESP_RCVD:
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300352 /*
Arik Nemtsov5cb12702015-01-22 12:19:26 +0200353 * we are waiting for the FW to give an "active" notification,
354 * so ignore requests in the meantime
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300355 */
Arik Nemtsov5cb12702015-01-22 12:19:26 +0200356 ret = -EBUSY;
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300357 break;
358 case IWL_MVM_TDLS_SW_REQ_RCVD:
359 /* as above, allow the link initiator to proceed */
360 if (type == TDLS_SEND_CHAN_SW_REQ) {
361 if (!same_peer)
362 ret = -EBUSY;
363 else if (peer_initiator) /* they are the initiator */
364 ret = -EBUSY;
365 } else if (type == TDLS_MOVE_CH) {
366 ret = -EINVAL;
367 }
368 break;
369 case IWL_MVM_TDLS_SW_ACTIVE:
Arik Nemtsov5cb12702015-01-22 12:19:26 +0200370 /*
371 * the only valid request when active is a request to return
372 * to the base channel by the current off-channel peer
373 */
374 if (type != TDLS_MOVE_CH || !same_peer)
375 ret = -EBUSY;
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300376 break;
377 }
378
379 if (ret)
380 IWL_DEBUG_TDLS(mvm,
381 "Invalid TDLS action %d state %d peer %pM same_peer %d initiator %d\n",
382 type, mvm->tdls_cs.state, peer, same_peer,
383 peer_initiator);
384
385 return ret;
386}
387
388static int
389iwl_mvm_tdls_config_channel_switch(struct iwl_mvm *mvm,
390 struct ieee80211_vif *vif,
391 enum iwl_tdls_channel_switch_type type,
392 const u8 *peer, bool peer_initiator,
393 u8 oper_class,
394 struct cfg80211_chan_def *chandef,
395 u32 timestamp, u16 switch_time,
396 u16 switch_timeout, struct sk_buff *skb,
397 u32 ch_sw_tm_ie)
398{
399 struct ieee80211_sta *sta;
400 struct iwl_mvm_sta *mvmsta;
401 struct ieee80211_tx_info *info;
402 struct ieee80211_hdr *hdr;
403 struct iwl_tdls_channel_switch_cmd cmd = {0};
404 int ret;
405
406 lockdep_assert_held(&mvm->mutex);
407
Arik Nemtsovb9dccdb2015-01-22 14:24:44 +0200408 ret = iwl_mvm_tdls_check_action(mvm, type, peer, peer_initiator,
409 timestamp);
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300410 if (ret)
411 return ret;
412
413 if (!skb || WARN_ON(skb->len > IWL_TDLS_CH_SW_FRAME_MAX_SIZE)) {
414 ret = -EINVAL;
415 goto out;
416 }
417
418 cmd.switch_type = type;
419 cmd.timing.frame_timestamp = cpu_to_le32(timestamp);
420 cmd.timing.switch_time = cpu_to_le32(switch_time);
421 cmd.timing.switch_timeout = cpu_to_le32(switch_timeout);
422
423 rcu_read_lock();
424 sta = ieee80211_find_sta(vif, peer);
425 if (!sta) {
426 rcu_read_unlock();
427 ret = -ENOENT;
428 goto out;
429 }
430 mvmsta = iwl_mvm_sta_from_mac80211(sta);
431 cmd.peer_sta_id = cpu_to_le32(mvmsta->sta_id);
432
433 if (!chandef) {
434 if (mvm->tdls_cs.state == IWL_MVM_TDLS_SW_REQ_SENT &&
435 mvm->tdls_cs.peer.chandef.chan) {
436 /* actually moving to the channel */
437 chandef = &mvm->tdls_cs.peer.chandef;
438 } else if (mvm->tdls_cs.state == IWL_MVM_TDLS_SW_ACTIVE &&
439 type == TDLS_MOVE_CH) {
440 /* we need to return to base channel */
441 struct ieee80211_chanctx_conf *chanctx =
442 rcu_dereference(vif->chanctx_conf);
443
444 if (WARN_ON_ONCE(!chanctx)) {
445 rcu_read_unlock();
446 goto out;
447 }
448
449 chandef = &chanctx->def;
450 }
451 }
452
453 if (chandef) {
454 cmd.ci.band = (chandef->chan->band == IEEE80211_BAND_2GHZ ?
455 PHY_BAND_24 : PHY_BAND_5);
456 cmd.ci.channel = chandef->chan->hw_value;
457 cmd.ci.width = iwl_mvm_get_channel_width(chandef);
458 cmd.ci.ctrl_pos = iwl_mvm_get_ctrl_pos(chandef);
459 }
460
461 /* keep quota calculation simple for now - 50% of DTIM for TDLS */
462 cmd.timing.max_offchan_duration =
463 cpu_to_le32(TU_TO_US(vif->bss_conf.dtim_period *
464 vif->bss_conf.beacon_int) / 2);
465
466 /* Switch time is the first element in the switch-timing IE. */
467 cmd.frame.switch_time_offset = cpu_to_le32(ch_sw_tm_ie + 2);
468
469 info = IEEE80211_SKB_CB(skb);
470 if (info->control.hw_key)
471 iwl_mvm_set_tx_cmd_crypto(mvm, info, &cmd.frame.tx_cmd, skb);
472
473 iwl_mvm_set_tx_cmd(mvm, skb, &cmd.frame.tx_cmd, info,
474 mvmsta->sta_id);
475
476 hdr = (void *)skb->data;
477 iwl_mvm_set_tx_cmd_rate(mvm, &cmd.frame.tx_cmd, info, sta,
478 hdr->frame_control);
479 rcu_read_unlock();
480
481 memcpy(cmd.frame.data, skb->data, skb->len);
482
483 ret = iwl_mvm_send_cmd_pdu(mvm, TDLS_CHANNEL_SWITCH_CMD, 0,
484 sizeof(cmd), &cmd);
485 if (ret) {
486 IWL_ERR(mvm, "Failed to send TDLS_CHANNEL_SWITCH cmd: %d\n",
487 ret);
488 goto out;
489 }
490
491 /* channel switch has started, update state */
492 if (type != TDLS_MOVE_CH) {
493 mvm->tdls_cs.cur_sta_id = mvmsta->sta_id;
494 iwl_mvm_tdls_update_cs_state(mvm,
495 type == TDLS_SEND_CHAN_SW_REQ ?
496 IWL_MVM_TDLS_SW_REQ_SENT :
497 IWL_MVM_TDLS_SW_REQ_RCVD);
Arik Nemtsov5cb12702015-01-22 12:19:26 +0200498 } else {
499 iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_RESP_RCVD);
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300500 }
501
502out:
503
504 /* channel switch failed - we are idle */
505 if (ret)
506 iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
507
508 return ret;
509}
510
511void iwl_mvm_tdls_ch_switch_work(struct work_struct *work)
512{
513 struct iwl_mvm *mvm;
514 struct ieee80211_sta *sta;
515 struct iwl_mvm_sta *mvmsta;
516 struct ieee80211_vif *vif;
517 unsigned int delay;
518 int ret;
519
520 mvm = container_of(work, struct iwl_mvm, tdls_cs.dwork.work);
521 mutex_lock(&mvm->mutex);
522
523 /* called after an active channel switch has finished or timed-out */
524 iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
525
526 /* station might be gone, in that case do nothing */
527 if (mvm->tdls_cs.peer.sta_id == IWL_MVM_STATION_COUNT)
528 goto out;
529
530 sta = rcu_dereference_protected(
531 mvm->fw_id_to_mac_id[mvm->tdls_cs.peer.sta_id],
532 lockdep_is_held(&mvm->mutex));
533 /* the station may not be here, but if it is, it must be a TDLS peer */
534 if (!sta || IS_ERR(sta) || WARN_ON(!sta->tdls))
535 goto out;
536
537 mvmsta = iwl_mvm_sta_from_mac80211(sta);
538 vif = mvmsta->vif;
539 ret = iwl_mvm_tdls_config_channel_switch(mvm, vif,
540 TDLS_SEND_CHAN_SW_REQ,
541 sta->addr,
542 mvm->tdls_cs.peer.initiator,
543 mvm->tdls_cs.peer.op_class,
544 &mvm->tdls_cs.peer.chandef,
545 0, 0, 0,
546 mvm->tdls_cs.peer.skb,
547 mvm->tdls_cs.peer.ch_sw_tm_ie);
548 if (ret)
549 IWL_ERR(mvm, "Not sending TDLS channel switch: %d\n", ret);
550
551 /* retry after a DTIM if we failed sending now */
552 delay = TU_TO_MS(vif->bss_conf.dtim_period * vif->bss_conf.beacon_int);
553 queue_delayed_work(system_wq, &mvm->tdls_cs.dwork,
554 msecs_to_jiffies(delay));
555out:
556 mutex_unlock(&mvm->mutex);
557}
558
559int
560iwl_mvm_tdls_channel_switch(struct ieee80211_hw *hw,
561 struct ieee80211_vif *vif,
562 struct ieee80211_sta *sta, u8 oper_class,
563 struct cfg80211_chan_def *chandef,
564 struct sk_buff *tmpl_skb, u32 ch_sw_tm_ie)
565{
566 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
567 struct iwl_mvm_sta *mvmsta;
568 unsigned int delay;
569 int ret;
570
571 mutex_lock(&mvm->mutex);
572
573 IWL_DEBUG_TDLS(mvm, "TDLS channel switch with %pM ch %d width %d\n",
574 sta->addr, chandef->chan->center_freq, chandef->width);
575
576 /* we only support a single peer for channel switching */
577 if (mvm->tdls_cs.peer.sta_id != IWL_MVM_STATION_COUNT) {
578 IWL_DEBUG_TDLS(mvm,
579 "Existing peer. Can't start switch with %pM\n",
580 sta->addr);
581 ret = -EBUSY;
582 goto out;
583 }
584
585 ret = iwl_mvm_tdls_config_channel_switch(mvm, vif,
586 TDLS_SEND_CHAN_SW_REQ,
587 sta->addr, sta->tdls_initiator,
588 oper_class, chandef, 0, 0, 0,
589 tmpl_skb, ch_sw_tm_ie);
590 if (ret)
591 goto out;
592
593 /*
594 * Mark the peer as "in tdls switch" for this vif. We only allow a
595 * single such peer per vif.
596 */
597 mvm->tdls_cs.peer.skb = skb_copy(tmpl_skb, GFP_KERNEL);
598 if (!mvm->tdls_cs.peer.skb) {
599 ret = -ENOMEM;
600 goto out;
601 }
602
603 mvmsta = iwl_mvm_sta_from_mac80211(sta);
604 mvm->tdls_cs.peer.sta_id = mvmsta->sta_id;
605 mvm->tdls_cs.peer.chandef = *chandef;
606 mvm->tdls_cs.peer.initiator = sta->tdls_initiator;
607 mvm->tdls_cs.peer.op_class = oper_class;
608 mvm->tdls_cs.peer.ch_sw_tm_ie = ch_sw_tm_ie;
609
610 /*
611 * Wait for 2 DTIM periods before attempting the next switch. The next
612 * switch will be made sooner if the current one completes before that.
613 */
614 delay = 2 * TU_TO_MS(vif->bss_conf.dtim_period *
615 vif->bss_conf.beacon_int);
616 mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
617 msecs_to_jiffies(delay));
618
619out:
620 mutex_unlock(&mvm->mutex);
621 return ret;
622}
623
624void iwl_mvm_tdls_cancel_channel_switch(struct ieee80211_hw *hw,
625 struct ieee80211_vif *vif,
626 struct ieee80211_sta *sta)
627{
628 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
629 struct ieee80211_sta *cur_sta;
630 bool wait_for_phy = false;
631
632 mutex_lock(&mvm->mutex);
633
634 IWL_DEBUG_TDLS(mvm, "TDLS cancel channel switch with %pM\n", sta->addr);
635
636 /* we only support a single peer for channel switching */
637 if (mvm->tdls_cs.peer.sta_id == IWL_MVM_STATION_COUNT) {
638 IWL_DEBUG_TDLS(mvm, "No ch switch peer - %pM\n", sta->addr);
639 goto out;
640 }
641
642 cur_sta = rcu_dereference_protected(
643 mvm->fw_id_to_mac_id[mvm->tdls_cs.peer.sta_id],
644 lockdep_is_held(&mvm->mutex));
645 /* make sure it's the same peer */
646 if (cur_sta != sta)
647 goto out;
648
649 /*
650 * If we're currently in a switch because of the now canceled peer,
651 * wait a DTIM here to make sure the phy is back on the base channel.
652 * We can't otherwise force it.
653 */
654 if (mvm->tdls_cs.cur_sta_id == mvm->tdls_cs.peer.sta_id &&
655 mvm->tdls_cs.state != IWL_MVM_TDLS_SW_IDLE)
656 wait_for_phy = true;
657
658 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
659 dev_kfree_skb(mvm->tdls_cs.peer.skb);
660 mvm->tdls_cs.peer.skb = NULL;
661
662out:
663 mutex_unlock(&mvm->mutex);
664
665 /* make sure the phy is on the base channel */
666 if (wait_for_phy)
667 msleep(TU_TO_MS(vif->bss_conf.dtim_period *
668 vif->bss_conf.beacon_int));
669
670 /* flush the channel switch state */
671 flush_delayed_work(&mvm->tdls_cs.dwork);
672
673 IWL_DEBUG_TDLS(mvm, "TDLS ending channel switch with %pM\n", sta->addr);
674}
675
676void
677iwl_mvm_tdls_recv_channel_switch(struct ieee80211_hw *hw,
678 struct ieee80211_vif *vif,
679 struct ieee80211_tdls_ch_sw_params *params)
680{
681 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
682 enum iwl_tdls_channel_switch_type type;
683 unsigned int delay;
Arik Nemtsov5cb12702015-01-22 12:19:26 +0200684 const char *action_str =
685 params->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ?
686 "REQ" : "RESP";
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300687
688 mutex_lock(&mvm->mutex);
689
690 IWL_DEBUG_TDLS(mvm,
Arik Nemtsov5cb12702015-01-22 12:19:26 +0200691 "Received TDLS ch switch action %s from %pM status %d\n",
692 action_str, params->sta->addr, params->status);
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300693
694 /*
695 * we got a non-zero status from a peer we were switching to - move to
696 * the idle state and retry again later
697 */
698 if (params->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE &&
699 params->status != 0 &&
700 mvm->tdls_cs.state == IWL_MVM_TDLS_SW_REQ_SENT &&
701 mvm->tdls_cs.cur_sta_id != IWL_MVM_STATION_COUNT) {
702 struct ieee80211_sta *cur_sta;
703
704 /* make sure it's the same peer */
705 cur_sta = rcu_dereference_protected(
706 mvm->fw_id_to_mac_id[mvm->tdls_cs.cur_sta_id],
707 lockdep_is_held(&mvm->mutex));
708 if (cur_sta == params->sta) {
709 iwl_mvm_tdls_update_cs_state(mvm,
710 IWL_MVM_TDLS_SW_IDLE);
711 goto retry;
712 }
713 }
714
715 type = (params->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST) ?
716 TDLS_SEND_CHAN_SW_RESP_AND_MOVE_CH : TDLS_MOVE_CH;
717
718 iwl_mvm_tdls_config_channel_switch(mvm, vif, type, params->sta->addr,
719 params->sta->tdls_initiator, 0,
720 params->chandef, params->timestamp,
721 params->switch_time,
722 params->switch_timeout,
723 params->tmpl_skb,
724 params->ch_sw_tm_ie);
725
726retry:
727 /* register a timeout in case we don't succeed in switching */
728 delay = vif->bss_conf.dtim_period * vif->bss_conf.beacon_int *
729 1024 / 1000;
730 mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
731 msecs_to_jiffies(delay));
732 mutex_unlock(&mvm->mutex);
733}