1 /******************************************************************************
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  * Copyright(c) 2017 Intel Deutschland GmbH
10  * Copyright(C) 2018 - 2019 Intel Corporation
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * The full GNU General Public License is included in this distribution
22  * in the file called COPYING.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <linuxwifi@intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  * BSD LICENSE
29  *
30  * Copyright(c) 2014 Intel Mobile Communications GmbH
31  * Copyright(c) 2017 Intel Deutschland GmbH
32  * Copyright(C) 2018 - 2019 Intel Corporation
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  *
39  *  * Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  *  * Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in
43  *    the documentation and/or other materials provided with the
44  *    distribution.
45  *  * Neither the name Intel Corporation nor the names of its
46  *    contributors may be used to endorse or promote products derived
47  *    from this software without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
52  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
53  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60  *
61  *****************************************************************************/
62 
63 #include <linux/etherdevice.h>
64 #include "mvm.h"
65 #include "time-event.h"
66 #include "iwl-io.h"
67 #include "iwl-prph.h"
68 
69 #define TU_TO_US(x) (x * 1024)
70 #define TU_TO_MS(x) (TU_TO_US(x) / 1000)
71 
72 void iwl_mvm_teardown_tdls_peers(struct iwl_mvm *mvm)
73 {
74 	struct ieee80211_sta *sta;
75 	struct iwl_mvm_sta *mvmsta;
76 	int i;
77 
78 	lockdep_assert_held(&mvm->mutex);
79 
80 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
81 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
82 						lockdep_is_held(&mvm->mutex));
83 		if (!sta || IS_ERR(sta) || !sta->tdls)
84 			continue;
85 
86 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
87 		ieee80211_tdls_oper_request(mvmsta->vif, sta->addr,
88 				NL80211_TDLS_TEARDOWN,
89 				WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED,
90 				GFP_KERNEL);
91 	}
92 }
93 
94 int iwl_mvm_tdls_sta_count(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
95 {
96 	struct ieee80211_sta *sta;
97 	struct iwl_mvm_sta *mvmsta;
98 	int count = 0;
99 	int i;
100 
101 	lockdep_assert_held(&mvm->mutex);
102 
103 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
104 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
105 						lockdep_is_held(&mvm->mutex));
106 		if (!sta || IS_ERR(sta) || !sta->tdls)
107 			continue;
108 
109 		if (vif) {
110 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
111 			if (mvmsta->vif != vif)
112 				continue;
113 		}
114 
115 		count++;
116 	}
117 
118 	return count;
119 }
120 
121 static void iwl_mvm_tdls_config(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
122 {
123 	struct iwl_rx_packet *pkt;
124 	struct iwl_tdls_config_res *resp;
125 	struct iwl_tdls_config_cmd tdls_cfg_cmd = {};
126 	struct iwl_host_cmd cmd = {
127 		.id = TDLS_CONFIG_CMD,
128 		.flags = CMD_WANT_SKB,
129 		.data = { &tdls_cfg_cmd, },
130 		.len = { sizeof(struct iwl_tdls_config_cmd), },
131 	};
132 	struct ieee80211_sta *sta;
133 	int ret, i, cnt;
134 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
135 
136 	lockdep_assert_held(&mvm->mutex);
137 
138 	tdls_cfg_cmd.id_and_color =
139 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
140 	tdls_cfg_cmd.tx_to_ap_tid = IWL_MVM_TDLS_FW_TID;
141 	tdls_cfg_cmd.tx_to_ap_ssn = cpu_to_le16(0); /* not used for now */
142 
143 	/* for now the Tx cmd is empty and unused */
144 
145 	/* populate TDLS peer data */
146 	cnt = 0;
147 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
148 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
149 						lockdep_is_held(&mvm->mutex));
150 		if (IS_ERR_OR_NULL(sta) || !sta->tdls)
151 			continue;
152 
153 		tdls_cfg_cmd.sta_info[cnt].sta_id = i;
154 		tdls_cfg_cmd.sta_info[cnt].tx_to_peer_tid =
155 							IWL_MVM_TDLS_FW_TID;
156 		tdls_cfg_cmd.sta_info[cnt].tx_to_peer_ssn = cpu_to_le16(0);
157 		tdls_cfg_cmd.sta_info[cnt].is_initiator =
158 				cpu_to_le32(sta->tdls_initiator ? 1 : 0);
159 
160 		cnt++;
161 	}
162 
163 	tdls_cfg_cmd.tdls_peer_count = cnt;
164 	IWL_DEBUG_TDLS(mvm, "send TDLS config to FW for %d peers\n", cnt);
165 
166 	ret = iwl_mvm_send_cmd(mvm, &cmd);
167 	if (WARN_ON_ONCE(ret))
168 		return;
169 
170 	pkt = cmd.resp_pkt;
171 
172 	WARN_ON_ONCE(iwl_rx_packet_payload_len(pkt) != sizeof(*resp));
173 
174 	/* we don't really care about the response at this point */
175 
176 	iwl_free_resp(&cmd);
177 }
178 
179 void iwl_mvm_recalc_tdls_state(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
180 			       bool sta_added)
181 {
182 	int tdls_sta_cnt = iwl_mvm_tdls_sta_count(mvm, vif);
183 
184 	/* when the first peer joins, send a power update first */
185 	if (tdls_sta_cnt == 1 && sta_added)
186 		iwl_mvm_power_update_mac(mvm);
187 
188 	/* Configure the FW with TDLS peer info only if TDLS channel switch
189 	 * capability is set.
190 	 * TDLS config data is used currently only in TDLS channel switch code.
191 	 * Supposed to serve also TDLS buffer station which is not implemneted
192 	 * yet in FW*/
193 	if (fw_has_capa(&mvm->fw->ucode_capa,
194 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH))
195 		iwl_mvm_tdls_config(mvm, vif);
196 
197 	/* when the last peer leaves, send a power update last */
198 	if (tdls_sta_cnt == 0 && !sta_added)
199 		iwl_mvm_power_update_mac(mvm);
200 }
201 
202 void iwl_mvm_mac_mgd_protect_tdls_discover(struct ieee80211_hw *hw,
203 					   struct ieee80211_vif *vif)
204 {
205 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
206 	u32 duration = 2 * vif->bss_conf.dtim_period * vif->bss_conf.beacon_int;
207 
208 	/*
209 	 * iwl_mvm_protect_session() reads directly from the device
210 	 * (the system time), so make sure it is available.
211 	 */
212 	if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PROTECT_TDLS))
213 		return;
214 
215 	mutex_lock(&mvm->mutex);
216 	/* Protect the session to hear the TDLS setup response on the channel */
217 	iwl_mvm_protect_session(mvm, vif, duration, duration, 100, true);
218 	mutex_unlock(&mvm->mutex);
219 
220 	iwl_mvm_unref(mvm, IWL_MVM_REF_PROTECT_TDLS);
221 }
222 
223 static const char *
224 iwl_mvm_tdls_cs_state_str(enum iwl_mvm_tdls_cs_state state)
225 {
226 	switch (state) {
227 	case IWL_MVM_TDLS_SW_IDLE:
228 		return "IDLE";
229 	case IWL_MVM_TDLS_SW_REQ_SENT:
230 		return "REQ SENT";
231 	case IWL_MVM_TDLS_SW_RESP_RCVD:
232 		return "RESP RECEIVED";
233 	case IWL_MVM_TDLS_SW_REQ_RCVD:
234 		return "REQ RECEIVED";
235 	case IWL_MVM_TDLS_SW_ACTIVE:
236 		return "ACTIVE";
237 	}
238 
239 	return NULL;
240 }
241 
242 static void iwl_mvm_tdls_update_cs_state(struct iwl_mvm *mvm,
243 					 enum iwl_mvm_tdls_cs_state state)
244 {
245 	if (mvm->tdls_cs.state == state)
246 		return;
247 
248 	IWL_DEBUG_TDLS(mvm, "TDLS channel switch state: %s -> %s\n",
249 		       iwl_mvm_tdls_cs_state_str(mvm->tdls_cs.state),
250 		       iwl_mvm_tdls_cs_state_str(state));
251 	mvm->tdls_cs.state = state;
252 
253 	/* we only send requests to our switching peer - update sent time */
254 	if (state == IWL_MVM_TDLS_SW_REQ_SENT)
255 		mvm->tdls_cs.peer.sent_timestamp = iwl_mvm_get_systime(mvm);
256 
257 	if (state == IWL_MVM_TDLS_SW_IDLE)
258 		mvm->tdls_cs.cur_sta_id = IWL_MVM_INVALID_STA;
259 }
260 
261 void iwl_mvm_rx_tdls_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
262 {
263 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
264 	struct iwl_tdls_channel_switch_notif *notif = (void *)pkt->data;
265 	struct ieee80211_sta *sta;
266 	unsigned int delay;
267 	struct iwl_mvm_sta *mvmsta;
268 	struct ieee80211_vif *vif;
269 	u32 sta_id = le32_to_cpu(notif->sta_id);
270 
271 	lockdep_assert_held(&mvm->mutex);
272 
273 	/* can fail sometimes */
274 	if (!le32_to_cpu(notif->status)) {
275 		iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
276 		return;
277 	}
278 
279 	if (WARN_ON(sta_id >= IWL_MVM_STATION_COUNT))
280 		return;
281 
282 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
283 					lockdep_is_held(&mvm->mutex));
284 	/* the station may not be here, but if it is, it must be a TDLS peer */
285 	if (IS_ERR_OR_NULL(sta) || WARN_ON(!sta->tdls))
286 		return;
287 
288 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
289 	vif = mvmsta->vif;
290 
291 	/*
292 	 * Update state and possibly switch again after this is over (DTIM).
293 	 * Also convert TU to msec.
294 	 */
295 	delay = TU_TO_MS(vif->bss_conf.dtim_period * vif->bss_conf.beacon_int);
296 	mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
297 			 msecs_to_jiffies(delay));
298 
299 	iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_ACTIVE);
300 }
301 
302 static int
303 iwl_mvm_tdls_check_action(struct iwl_mvm *mvm,
304 			  enum iwl_tdls_channel_switch_type type,
305 			  const u8 *peer, bool peer_initiator, u32 timestamp)
306 {
307 	bool same_peer = false;
308 	int ret = 0;
309 
310 	/* get the existing peer if it's there */
311 	if (mvm->tdls_cs.state != IWL_MVM_TDLS_SW_IDLE &&
312 	    mvm->tdls_cs.cur_sta_id != IWL_MVM_INVALID_STA) {
313 		struct ieee80211_sta *sta = rcu_dereference_protected(
314 				mvm->fw_id_to_mac_id[mvm->tdls_cs.cur_sta_id],
315 				lockdep_is_held(&mvm->mutex));
316 		if (!IS_ERR_OR_NULL(sta))
317 			same_peer = ether_addr_equal(peer, sta->addr);
318 	}
319 
320 	switch (mvm->tdls_cs.state) {
321 	case IWL_MVM_TDLS_SW_IDLE:
322 		/*
323 		 * might be spurious packet from the peer after the switch is
324 		 * already done
325 		 */
326 		if (type == TDLS_MOVE_CH)
327 			ret = -EINVAL;
328 		break;
329 	case IWL_MVM_TDLS_SW_REQ_SENT:
330 		/* only allow requests from the same peer */
331 		if (!same_peer)
332 			ret = -EBUSY;
333 		else if (type == TDLS_SEND_CHAN_SW_RESP_AND_MOVE_CH &&
334 			 !peer_initiator)
335 			/*
336 			 * We received a ch-switch request while an outgoing
337 			 * one is pending. Allow it if the peer is the link
338 			 * initiator.
339 			 */
340 			ret = -EBUSY;
341 		else if (type == TDLS_SEND_CHAN_SW_REQ)
342 			/* wait for idle before sending another request */
343 			ret = -EBUSY;
344 		else if (timestamp <= mvm->tdls_cs.peer.sent_timestamp)
345 			/* we got a stale response - ignore it */
346 			ret = -EINVAL;
347 		break;
348 	case IWL_MVM_TDLS_SW_RESP_RCVD:
349 		/*
350 		 * we are waiting for the FW to give an "active" notification,
351 		 * so ignore requests in the meantime
352 		 */
353 		ret = -EBUSY;
354 		break;
355 	case IWL_MVM_TDLS_SW_REQ_RCVD:
356 		/* as above, allow the link initiator to proceed */
357 		if (type == TDLS_SEND_CHAN_SW_REQ) {
358 			if (!same_peer)
359 				ret = -EBUSY;
360 			else if (peer_initiator) /* they are the initiator */
361 				ret = -EBUSY;
362 		} else if (type == TDLS_MOVE_CH) {
363 			ret = -EINVAL;
364 		}
365 		break;
366 	case IWL_MVM_TDLS_SW_ACTIVE:
367 		/*
368 		 * the only valid request when active is a request to return
369 		 * to the base channel by the current off-channel peer
370 		 */
371 		if (type != TDLS_MOVE_CH || !same_peer)
372 			ret = -EBUSY;
373 		break;
374 	}
375 
376 	if (ret)
377 		IWL_DEBUG_TDLS(mvm,
378 			       "Invalid TDLS action %d state %d peer %pM same_peer %d initiator %d\n",
379 			       type, mvm->tdls_cs.state, peer, same_peer,
380 			       peer_initiator);
381 
382 	return ret;
383 }
384 
385 static int
386 iwl_mvm_tdls_config_channel_switch(struct iwl_mvm *mvm,
387 				   struct ieee80211_vif *vif,
388 				   enum iwl_tdls_channel_switch_type type,
389 				   const u8 *peer, bool peer_initiator,
390 				   u8 oper_class,
391 				   struct cfg80211_chan_def *chandef,
392 				   u32 timestamp, u16 switch_time,
393 				   u16 switch_timeout, struct sk_buff *skb,
394 				   u32 ch_sw_tm_ie)
395 {
396 	struct ieee80211_sta *sta;
397 	struct iwl_mvm_sta *mvmsta;
398 	struct ieee80211_tx_info *info;
399 	struct ieee80211_hdr *hdr;
400 	struct iwl_tdls_channel_switch_cmd cmd = {0};
401 	struct iwl_tdls_channel_switch_cmd_tail *tail =
402 		iwl_mvm_chan_info_cmd_tail(mvm, &cmd.ci);
403 	u16 len = sizeof(cmd) - iwl_mvm_chan_info_padding(mvm);
404 	int ret;
405 
406 	lockdep_assert_held(&mvm->mutex);
407 
408 	ret = iwl_mvm_tdls_check_action(mvm, type, peer, peer_initiator,
409 					timestamp);
410 	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 	tail->timing.frame_timestamp = cpu_to_le32(timestamp);
420 	tail->timing.switch_time = cpu_to_le32(switch_time);
421 	tail->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 		iwl_mvm_set_chan_info_chandef(mvm, &cmd.ci, chandef);
455 
456 	/* keep quota calculation simple for now - 50% of DTIM for TDLS */
457 	tail->timing.max_offchan_duration =
458 			cpu_to_le32(TU_TO_US(vif->bss_conf.dtim_period *
459 					     vif->bss_conf.beacon_int) / 2);
460 
461 	/* Switch time is the first element in the switch-timing IE. */
462 	tail->frame.switch_time_offset = cpu_to_le32(ch_sw_tm_ie + 2);
463 
464 	info = IEEE80211_SKB_CB(skb);
465 	hdr = (void *)skb->data;
466 	if (info->control.hw_key) {
467 		if (info->control.hw_key->cipher != WLAN_CIPHER_SUITE_CCMP) {
468 			rcu_read_unlock();
469 			ret = -EINVAL;
470 			goto out;
471 		}
472 		iwl_mvm_set_tx_cmd_ccmp(info, &tail->frame.tx_cmd);
473 	}
474 
475 	iwl_mvm_set_tx_cmd(mvm, skb, &tail->frame.tx_cmd, info,
476 			   mvmsta->sta_id);
477 
478 	iwl_mvm_set_tx_cmd_rate(mvm, &tail->frame.tx_cmd, info, sta,
479 				hdr->frame_control);
480 	rcu_read_unlock();
481 
482 	memcpy(tail->frame.data, skb->data, skb->len);
483 
484 	ret = iwl_mvm_send_cmd_pdu(mvm, TDLS_CHANNEL_SWITCH_CMD, 0, len, &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);
498 	} else {
499 		iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_RESP_RCVD);
500 	}
501 
502 out:
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 
511 void 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_INVALID_STA)
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 	schedule_delayed_work(&mvm->tdls_cs.dwork, msecs_to_jiffies(delay));
554 out:
555 	mutex_unlock(&mvm->mutex);
556 }
557 
558 int
559 iwl_mvm_tdls_channel_switch(struct ieee80211_hw *hw,
560 			    struct ieee80211_vif *vif,
561 			    struct ieee80211_sta *sta, u8 oper_class,
562 			    struct cfg80211_chan_def *chandef,
563 			    struct sk_buff *tmpl_skb, u32 ch_sw_tm_ie)
564 {
565 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
566 	struct iwl_mvm_sta *mvmsta;
567 	unsigned int delay;
568 	int ret;
569 
570 	mutex_lock(&mvm->mutex);
571 
572 	IWL_DEBUG_TDLS(mvm, "TDLS channel switch with %pM ch %d width %d\n",
573 		       sta->addr, chandef->chan->center_freq, chandef->width);
574 
575 	/* we only support a single peer for channel switching */
576 	if (mvm->tdls_cs.peer.sta_id != IWL_MVM_INVALID_STA) {
577 		IWL_DEBUG_TDLS(mvm,
578 			       "Existing peer. Can't start switch with %pM\n",
579 			       sta->addr);
580 		ret = -EBUSY;
581 		goto out;
582 	}
583 
584 	ret = iwl_mvm_tdls_config_channel_switch(mvm, vif,
585 						 TDLS_SEND_CHAN_SW_REQ,
586 						 sta->addr, sta->tdls_initiator,
587 						 oper_class, chandef, 0, 0, 0,
588 						 tmpl_skb, ch_sw_tm_ie);
589 	if (ret)
590 		goto out;
591 
592 	/*
593 	 * Mark the peer as "in tdls switch" for this vif. We only allow a
594 	 * single such peer per vif.
595 	 */
596 	mvm->tdls_cs.peer.skb = skb_copy(tmpl_skb, GFP_KERNEL);
597 	if (!mvm->tdls_cs.peer.skb) {
598 		ret = -ENOMEM;
599 		goto out;
600 	}
601 
602 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
603 	mvm->tdls_cs.peer.sta_id = mvmsta->sta_id;
604 	mvm->tdls_cs.peer.chandef = *chandef;
605 	mvm->tdls_cs.peer.initiator = sta->tdls_initiator;
606 	mvm->tdls_cs.peer.op_class = oper_class;
607 	mvm->tdls_cs.peer.ch_sw_tm_ie = ch_sw_tm_ie;
608 
609 	/*
610 	 * Wait for 2 DTIM periods before attempting the next switch. The next
611 	 * switch will be made sooner if the current one completes before that.
612 	 */
613 	delay = 2 * TU_TO_MS(vif->bss_conf.dtim_period *
614 			     vif->bss_conf.beacon_int);
615 	mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
616 			 msecs_to_jiffies(delay));
617 
618 out:
619 	mutex_unlock(&mvm->mutex);
620 	return ret;
621 }
622 
623 void iwl_mvm_tdls_cancel_channel_switch(struct ieee80211_hw *hw,
624 					struct ieee80211_vif *vif,
625 					struct ieee80211_sta *sta)
626 {
627 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
628 	struct ieee80211_sta *cur_sta;
629 	bool wait_for_phy = false;
630 
631 	mutex_lock(&mvm->mutex);
632 
633 	IWL_DEBUG_TDLS(mvm, "TDLS cancel channel switch with %pM\n", sta->addr);
634 
635 	/* we only support a single peer for channel switching */
636 	if (mvm->tdls_cs.peer.sta_id == IWL_MVM_INVALID_STA) {
637 		IWL_DEBUG_TDLS(mvm, "No ch switch peer - %pM\n", sta->addr);
638 		goto out;
639 	}
640 
641 	cur_sta = rcu_dereference_protected(
642 				mvm->fw_id_to_mac_id[mvm->tdls_cs.peer.sta_id],
643 				lockdep_is_held(&mvm->mutex));
644 	/* make sure it's the same peer */
645 	if (cur_sta != sta)
646 		goto out;
647 
648 	/*
649 	 * If we're currently in a switch because of the now canceled peer,
650 	 * wait a DTIM here to make sure the phy is back on the base channel.
651 	 * We can't otherwise force it.
652 	 */
653 	if (mvm->tdls_cs.cur_sta_id == mvm->tdls_cs.peer.sta_id &&
654 	    mvm->tdls_cs.state != IWL_MVM_TDLS_SW_IDLE)
655 		wait_for_phy = true;
656 
657 	mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
658 	dev_kfree_skb(mvm->tdls_cs.peer.skb);
659 	mvm->tdls_cs.peer.skb = NULL;
660 
661 out:
662 	mutex_unlock(&mvm->mutex);
663 
664 	/* make sure the phy is on the base channel */
665 	if (wait_for_phy)
666 		msleep(TU_TO_MS(vif->bss_conf.dtim_period *
667 				vif->bss_conf.beacon_int));
668 
669 	/* flush the channel switch state */
670 	flush_delayed_work(&mvm->tdls_cs.dwork);
671 
672 	IWL_DEBUG_TDLS(mvm, "TDLS ending channel switch with %pM\n", sta->addr);
673 }
674 
675 void
676 iwl_mvm_tdls_recv_channel_switch(struct ieee80211_hw *hw,
677 				 struct ieee80211_vif *vif,
678 				 struct ieee80211_tdls_ch_sw_params *params)
679 {
680 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
681 	enum iwl_tdls_channel_switch_type type;
682 	unsigned int delay;
683 	const char *action_str =
684 		params->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ?
685 		"REQ" : "RESP";
686 
687 	mutex_lock(&mvm->mutex);
688 
689 	IWL_DEBUG_TDLS(mvm,
690 		       "Received TDLS ch switch action %s from %pM status %d\n",
691 		       action_str, params->sta->addr, params->status);
692 
693 	/*
694 	 * we got a non-zero status from a peer we were switching to - move to
695 	 * the idle state and retry again later
696 	 */
697 	if (params->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE &&
698 	    params->status != 0 &&
699 	    mvm->tdls_cs.state == IWL_MVM_TDLS_SW_REQ_SENT &&
700 	    mvm->tdls_cs.cur_sta_id != IWL_MVM_INVALID_STA) {
701 		struct ieee80211_sta *cur_sta;
702 
703 		/* make sure it's the same peer */
704 		cur_sta = rcu_dereference_protected(
705 				mvm->fw_id_to_mac_id[mvm->tdls_cs.cur_sta_id],
706 				lockdep_is_held(&mvm->mutex));
707 		if (cur_sta == params->sta) {
708 			iwl_mvm_tdls_update_cs_state(mvm,
709 						     IWL_MVM_TDLS_SW_IDLE);
710 			goto retry;
711 		}
712 	}
713 
714 	type = (params->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST) ?
715 	       TDLS_SEND_CHAN_SW_RESP_AND_MOVE_CH : TDLS_MOVE_CH;
716 
717 	iwl_mvm_tdls_config_channel_switch(mvm, vif, type, params->sta->addr,
718 					   params->sta->tdls_initiator, 0,
719 					   params->chandef, params->timestamp,
720 					   params->switch_time,
721 					   params->switch_timeout,
722 					   params->tmpl_skb,
723 					   params->ch_sw_tm_ie);
724 
725 retry:
726 	/* register a timeout in case we don't succeed in switching */
727 	delay = vif->bss_conf.dtim_period * vif->bss_conf.beacon_int *
728 		1024 / 1000;
729 	mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
730 			 msecs_to_jiffies(delay));
731 	mutex_unlock(&mvm->mutex);
732 }
733