Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2/*
  3 * Copyright (C) 2014 Intel Mobile Communications GmbH
  4 * Copyright (C) 2017 Intel Deutschland GmbH
  5 * Copyright (C) 2018-2020, 2022-2023 Intel Corporation
  6 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  7#include <linux/etherdevice.h>
  8#include "mvm.h"
  9#include "time-event.h"
 10#include "iwl-io.h"
 11#include "iwl-prph.h"
 12
 13#define TU_TO_US(x) (x * 1024)
 14#define TU_TO_MS(x) (TU_TO_US(x) / 1000)
 15
 16void iwl_mvm_teardown_tdls_peers(struct iwl_mvm *mvm)
 17{
 18	struct ieee80211_sta *sta;
 19	struct iwl_mvm_sta *mvmsta;
 20	int i;
 21
 22	lockdep_assert_held(&mvm->mutex);
 23
 24	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
 25		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
 26						lockdep_is_held(&mvm->mutex));
 27		if (!sta || IS_ERR(sta) || !sta->tdls)
 28			continue;
 29
 30		mvmsta = iwl_mvm_sta_from_mac80211(sta);
 31		ieee80211_tdls_oper_request(mvmsta->vif, sta->addr,
 32				NL80211_TDLS_TEARDOWN,
 33				WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED,
 34				GFP_KERNEL);
 35	}
 36}
 37
 38int iwl_mvm_tdls_sta_count(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
 39{
 40	struct ieee80211_sta *sta;
 41	struct iwl_mvm_sta *mvmsta;
 42	int count = 0;
 43	int i;
 44
 45	lockdep_assert_held(&mvm->mutex);
 46
 47	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
 48		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
 49						lockdep_is_held(&mvm->mutex));
 50		if (!sta || IS_ERR(sta) || !sta->tdls)
 51			continue;
 52
 53		if (vif) {
 54			mvmsta = iwl_mvm_sta_from_mac80211(sta);
 55			if (mvmsta->vif != vif)
 56				continue;
 57		}
 58
 59		count++;
 60	}
 61
 62	return count;
 63}
 64
 65static void iwl_mvm_tdls_config(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
 66{
 67	struct iwl_rx_packet *pkt;
 68	struct iwl_tdls_config_res *resp;
 69	struct iwl_tdls_config_cmd tdls_cfg_cmd = {};
 70	struct iwl_host_cmd cmd = {
 71		.id = TDLS_CONFIG_CMD,
 72		.flags = CMD_WANT_SKB,
 73		.data = { &tdls_cfg_cmd, },
 74		.len = { sizeof(struct iwl_tdls_config_cmd), },
 75	};
 76	struct ieee80211_sta *sta;
 77	int ret, i, cnt;
 78	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 79
 80	lockdep_assert_held(&mvm->mutex);
 81
 82	tdls_cfg_cmd.id_and_color =
 83		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
 84	tdls_cfg_cmd.tx_to_ap_tid = IWL_MVM_TDLS_FW_TID;
 85	tdls_cfg_cmd.tx_to_ap_ssn = cpu_to_le16(0); /* not used for now */
 86
 87	/* for now the Tx cmd is empty and unused */
 88
 89	/* populate TDLS peer data */
 90	cnt = 0;
 91	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
 92		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
 93						lockdep_is_held(&mvm->mutex));
 94		if (IS_ERR_OR_NULL(sta) || !sta->tdls)
 95			continue;
 96
 97		tdls_cfg_cmd.sta_info[cnt].sta_id = i;
 98		tdls_cfg_cmd.sta_info[cnt].tx_to_peer_tid =
 99							IWL_MVM_TDLS_FW_TID;
100		tdls_cfg_cmd.sta_info[cnt].tx_to_peer_ssn = cpu_to_le16(0);
101		tdls_cfg_cmd.sta_info[cnt].is_initiator =
102				cpu_to_le32(sta->tdls_initiator ? 1 : 0);
103
104		cnt++;
105	}
106
107	tdls_cfg_cmd.tdls_peer_count = cnt;
108	IWL_DEBUG_TDLS(mvm, "send TDLS config to FW for %d peers\n", cnt);
109
110	ret = iwl_mvm_send_cmd(mvm, &cmd);
111	if (WARN_ON_ONCE(ret))
112		return;
113
114	pkt = cmd.resp_pkt;
115
116	WARN_ON_ONCE(iwl_rx_packet_payload_len(pkt) != sizeof(*resp));
117
118	/* we don't really care about the response at this point */
119
120	iwl_free_resp(&cmd);
121}
122
123void iwl_mvm_recalc_tdls_state(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
124			       bool sta_added)
125{
126	int tdls_sta_cnt = iwl_mvm_tdls_sta_count(mvm, vif);
127
128	/* when the first peer joins, send a power update first */
129	if (tdls_sta_cnt == 1 && sta_added)
130		iwl_mvm_power_update_mac(mvm);
131
132	/* Configure the FW with TDLS peer info only if TDLS channel switch
133	 * capability is set.
134	 * TDLS config data is used currently only in TDLS channel switch code.
135	 * Supposed to serve also TDLS buffer station which is not implemneted
136	 * yet in FW*/
137	if (fw_has_capa(&mvm->fw->ucode_capa,
138			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH))
139		iwl_mvm_tdls_config(mvm, vif);
140
141	/* when the last peer leaves, send a power update last */
142	if (tdls_sta_cnt == 0 && !sta_added)
143		iwl_mvm_power_update_mac(mvm);
144}
145
146void iwl_mvm_mac_mgd_protect_tdls_discover(struct ieee80211_hw *hw,
147					   struct ieee80211_vif *vif,
148					   unsigned int link_id)
149{
150	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
151	u32 duration = 2 * vif->bss_conf.dtim_period * vif->bss_conf.beacon_int;
152
153	/* Protect the session to hear the TDLS setup response on the channel */
154	mutex_lock(&mvm->mutex);
155	if (fw_has_capa(&mvm->fw->ucode_capa,
156			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
157		iwl_mvm_schedule_session_protection(mvm, vif, duration,
158						    duration, true, link_id);
159	else
160		iwl_mvm_protect_session(mvm, vif, duration,
161					duration, 100, true);
162	mutex_unlock(&mvm->mutex);
163}
164
165static const char *
166iwl_mvm_tdls_cs_state_str(enum iwl_mvm_tdls_cs_state state)
167{
168	switch (state) {
169	case IWL_MVM_TDLS_SW_IDLE:
170		return "IDLE";
171	case IWL_MVM_TDLS_SW_REQ_SENT:
172		return "REQ SENT";
173	case IWL_MVM_TDLS_SW_RESP_RCVD:
174		return "RESP RECEIVED";
175	case IWL_MVM_TDLS_SW_REQ_RCVD:
176		return "REQ RECEIVED";
177	case IWL_MVM_TDLS_SW_ACTIVE:
178		return "ACTIVE";
179	}
180
181	return NULL;
182}
183
184static void iwl_mvm_tdls_update_cs_state(struct iwl_mvm *mvm,
185					 enum iwl_mvm_tdls_cs_state state)
186{
187	if (mvm->tdls_cs.state == state)
188		return;
189
190	IWL_DEBUG_TDLS(mvm, "TDLS channel switch state: %s -> %s\n",
191		       iwl_mvm_tdls_cs_state_str(mvm->tdls_cs.state),
192		       iwl_mvm_tdls_cs_state_str(state));
193	mvm->tdls_cs.state = state;
194
195	/* we only send requests to our switching peer - update sent time */
196	if (state == IWL_MVM_TDLS_SW_REQ_SENT)
197		mvm->tdls_cs.peer.sent_timestamp = iwl_mvm_get_systime(mvm);
198
199	if (state == IWL_MVM_TDLS_SW_IDLE)
200		mvm->tdls_cs.cur_sta_id = IWL_MVM_INVALID_STA;
201}
202
203void iwl_mvm_rx_tdls_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
204{
205	struct iwl_rx_packet *pkt = rxb_addr(rxb);
206	struct iwl_tdls_channel_switch_notif *notif = (void *)pkt->data;
207	struct ieee80211_sta *sta;
208	unsigned int delay;
209	struct iwl_mvm_sta *mvmsta;
210	struct ieee80211_vif *vif;
211	u32 sta_id = le32_to_cpu(notif->sta_id);
212
213	lockdep_assert_held(&mvm->mutex);
214
215	/* can fail sometimes */
216	if (!le32_to_cpu(notif->status)) {
217		iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
218		return;
219	}
220
221	if (WARN_ON(sta_id >= mvm->fw->ucode_capa.num_stations))
222		return;
223
224	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
225					lockdep_is_held(&mvm->mutex));
226	/* the station may not be here, but if it is, it must be a TDLS peer */
227	if (IS_ERR_OR_NULL(sta) || WARN_ON(!sta->tdls))
228		return;
229
230	mvmsta = iwl_mvm_sta_from_mac80211(sta);
231	vif = mvmsta->vif;
232
233	/*
234	 * Update state and possibly switch again after this is over (DTIM).
235	 * Also convert TU to msec.
236	 */
237	delay = TU_TO_MS(vif->bss_conf.dtim_period * vif->bss_conf.beacon_int);
238	mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
239			 msecs_to_jiffies(delay));
240
241	iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_ACTIVE);
242}
243
244static int
245iwl_mvm_tdls_check_action(struct iwl_mvm *mvm,
246			  enum iwl_tdls_channel_switch_type type,
247			  const u8 *peer, bool peer_initiator, u32 timestamp)
248{
249	bool same_peer = false;
250	int ret = 0;
251
252	/* get the existing peer if it's there */
253	if (mvm->tdls_cs.state != IWL_MVM_TDLS_SW_IDLE &&
254	    mvm->tdls_cs.cur_sta_id != IWL_MVM_INVALID_STA) {
255		struct ieee80211_sta *sta = rcu_dereference_protected(
256				mvm->fw_id_to_mac_id[mvm->tdls_cs.cur_sta_id],
257				lockdep_is_held(&mvm->mutex));
258		if (!IS_ERR_OR_NULL(sta))
259			same_peer = ether_addr_equal(peer, sta->addr);
260	}
261
262	switch (mvm->tdls_cs.state) {
263	case IWL_MVM_TDLS_SW_IDLE:
264		/*
265		 * might be spurious packet from the peer after the switch is
266		 * already done
267		 */
268		if (type == TDLS_MOVE_CH)
269			ret = -EINVAL;
270		break;
271	case IWL_MVM_TDLS_SW_REQ_SENT:
272		/* only allow requests from the same peer */
273		if (!same_peer)
274			ret = -EBUSY;
275		else if (type == TDLS_SEND_CHAN_SW_RESP_AND_MOVE_CH &&
276			 !peer_initiator)
277			/*
278			 * We received a ch-switch request while an outgoing
279			 * one is pending. Allow it if the peer is the link
280			 * initiator.
281			 */
282			ret = -EBUSY;
283		else if (type == TDLS_SEND_CHAN_SW_REQ)
284			/* wait for idle before sending another request */
285			ret = -EBUSY;
286		else if (timestamp <= mvm->tdls_cs.peer.sent_timestamp)
287			/* we got a stale response - ignore it */
288			ret = -EINVAL;
289		break;
290	case IWL_MVM_TDLS_SW_RESP_RCVD:
291		/*
292		 * we are waiting for the FW to give an "active" notification,
293		 * so ignore requests in the meantime
294		 */
295		ret = -EBUSY;
296		break;
297	case IWL_MVM_TDLS_SW_REQ_RCVD:
298		/* as above, allow the link initiator to proceed */
299		if (type == TDLS_SEND_CHAN_SW_REQ) {
300			if (!same_peer)
301				ret = -EBUSY;
302			else if (peer_initiator) /* they are the initiator */
303				ret = -EBUSY;
304		} else if (type == TDLS_MOVE_CH) {
305			ret = -EINVAL;
306		}
307		break;
308	case IWL_MVM_TDLS_SW_ACTIVE:
309		/*
310		 * the only valid request when active is a request to return
311		 * to the base channel by the current off-channel peer
312		 */
313		if (type != TDLS_MOVE_CH || !same_peer)
314			ret = -EBUSY;
315		break;
316	}
317
318	if (ret)
319		IWL_DEBUG_TDLS(mvm,
320			       "Invalid TDLS action %d state %d peer %pM same_peer %d initiator %d\n",
321			       type, mvm->tdls_cs.state, peer, same_peer,
322			       peer_initiator);
323
324	return ret;
325}
326
327static int
328iwl_mvm_tdls_config_channel_switch(struct iwl_mvm *mvm,
329				   struct ieee80211_vif *vif,
330				   enum iwl_tdls_channel_switch_type type,
331				   const u8 *peer, bool peer_initiator,
332				   u8 oper_class,
333				   struct cfg80211_chan_def *chandef,
334				   u32 timestamp, u16 switch_time,
335				   u16 switch_timeout, struct sk_buff *skb,
336				   u32 ch_sw_tm_ie)
337{
338	struct ieee80211_sta *sta;
339	struct iwl_mvm_sta *mvmsta;
340	struct ieee80211_tx_info *info;
341	struct ieee80211_hdr *hdr;
342	struct iwl_tdls_channel_switch_cmd cmd = {0};
343	struct iwl_tdls_channel_switch_cmd_tail *tail =
344		iwl_mvm_chan_info_cmd_tail(mvm, &cmd.ci);
345	u16 len = sizeof(cmd) - iwl_mvm_chan_info_padding(mvm);
346	int ret;
347
348	lockdep_assert_held(&mvm->mutex);
349
350	ret = iwl_mvm_tdls_check_action(mvm, type, peer, peer_initiator,
351					timestamp);
352	if (ret)
353		return ret;
354
355	if (!skb || WARN_ON(skb->len > IWL_TDLS_CH_SW_FRAME_MAX_SIZE)) {
356		ret = -EINVAL;
357		goto out;
358	}
359
360	cmd.switch_type = type;
361	tail->timing.frame_timestamp = cpu_to_le32(timestamp);
362	tail->timing.switch_time = cpu_to_le32(switch_time);
363	tail->timing.switch_timeout = cpu_to_le32(switch_timeout);
364
365	rcu_read_lock();
366	sta = ieee80211_find_sta(vif, peer);
367	if (!sta) {
368		rcu_read_unlock();
369		ret = -ENOENT;
370		goto out;
371	}
372	mvmsta = iwl_mvm_sta_from_mac80211(sta);
373	cmd.peer_sta_id = cpu_to_le32(mvmsta->deflink.sta_id);
374
375	if (!chandef) {
376		if (mvm->tdls_cs.state == IWL_MVM_TDLS_SW_REQ_SENT &&
377		    mvm->tdls_cs.peer.chandef.chan) {
378			/* actually moving to the channel */
379			chandef = &mvm->tdls_cs.peer.chandef;
380		} else if (mvm->tdls_cs.state == IWL_MVM_TDLS_SW_ACTIVE &&
381			   type == TDLS_MOVE_CH) {
382			/* we need to return to base channel */
383			struct ieee80211_chanctx_conf *chanctx =
384					rcu_dereference(vif->bss_conf.chanctx_conf);
385
386			if (WARN_ON_ONCE(!chanctx)) {
387				rcu_read_unlock();
388				goto out;
389			}
390
391			chandef = &chanctx->def;
392		}
393	}
394
395	if (chandef)
396		iwl_mvm_set_chan_info_chandef(mvm, &cmd.ci, chandef);
397
398	/* keep quota calculation simple for now - 50% of DTIM for TDLS */
399	tail->timing.max_offchan_duration =
400			cpu_to_le32(TU_TO_US(vif->bss_conf.dtim_period *
401					     vif->bss_conf.beacon_int) / 2);
402
403	/* Switch time is the first element in the switch-timing IE. */
404	tail->frame.switch_time_offset = cpu_to_le32(ch_sw_tm_ie + 2);
405
406	info = IEEE80211_SKB_CB(skb);
407	hdr = (void *)skb->data;
408	if (info->control.hw_key) {
409		if (info->control.hw_key->cipher != WLAN_CIPHER_SUITE_CCMP) {
410			rcu_read_unlock();
411			ret = -EINVAL;
412			goto out;
413		}
414		iwl_mvm_set_tx_cmd_ccmp(info, &tail->frame.tx_cmd);
415	}
416
417	iwl_mvm_set_tx_cmd(mvm, skb, &tail->frame.tx_cmd, info,
418			   mvmsta->deflink.sta_id);
419
420	iwl_mvm_set_tx_cmd_rate(mvm, &tail->frame.tx_cmd, info, sta,
421				hdr->frame_control);
422	rcu_read_unlock();
423
424	memcpy(tail->frame.data, skb->data, skb->len);
425
426	ret = iwl_mvm_send_cmd_pdu(mvm, TDLS_CHANNEL_SWITCH_CMD, 0, len, &cmd);
427	if (ret) {
428		IWL_ERR(mvm, "Failed to send TDLS_CHANNEL_SWITCH cmd: %d\n",
429			ret);
430		goto out;
431	}
432
433	/* channel switch has started, update state */
434	if (type != TDLS_MOVE_CH) {
435		mvm->tdls_cs.cur_sta_id = mvmsta->deflink.sta_id;
436		iwl_mvm_tdls_update_cs_state(mvm,
437					     type == TDLS_SEND_CHAN_SW_REQ ?
438					     IWL_MVM_TDLS_SW_REQ_SENT :
439					     IWL_MVM_TDLS_SW_REQ_RCVD);
440	} else {
441		iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_RESP_RCVD);
442	}
443
444out:
445
446	/* channel switch failed - we are idle */
447	if (ret)
448		iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
449
450	return ret;
451}
452
453void iwl_mvm_tdls_ch_switch_work(struct work_struct *work)
454{
455	struct iwl_mvm *mvm;
456	struct ieee80211_sta *sta;
457	struct iwl_mvm_sta *mvmsta;
458	struct ieee80211_vif *vif;
459	unsigned int delay;
460	int ret;
461
462	mvm = container_of(work, struct iwl_mvm, tdls_cs.dwork.work);
463	mutex_lock(&mvm->mutex);
464
465	/* called after an active channel switch has finished or timed-out */
466	iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
467
468	/* station might be gone, in that case do nothing */
469	if (mvm->tdls_cs.peer.sta_id == IWL_MVM_INVALID_STA)
470		goto out;
471
472	sta = rcu_dereference_protected(
473				mvm->fw_id_to_mac_id[mvm->tdls_cs.peer.sta_id],
474				lockdep_is_held(&mvm->mutex));
475	/* the station may not be here, but if it is, it must be a TDLS peer */
476	if (!sta || IS_ERR(sta) || WARN_ON(!sta->tdls))
477		goto out;
478
479	mvmsta = iwl_mvm_sta_from_mac80211(sta);
480	vif = mvmsta->vif;
481	ret = iwl_mvm_tdls_config_channel_switch(mvm, vif,
482						 TDLS_SEND_CHAN_SW_REQ,
483						 sta->addr,
484						 mvm->tdls_cs.peer.initiator,
485						 mvm->tdls_cs.peer.op_class,
486						 &mvm->tdls_cs.peer.chandef,
487						 0, 0, 0,
488						 mvm->tdls_cs.peer.skb,
489						 mvm->tdls_cs.peer.ch_sw_tm_ie);
490	if (ret)
491		IWL_ERR(mvm, "Not sending TDLS channel switch: %d\n", ret);
492
493	/* retry after a DTIM if we failed sending now */
494	delay = TU_TO_MS(vif->bss_conf.dtim_period * vif->bss_conf.beacon_int);
495	schedule_delayed_work(&mvm->tdls_cs.dwork, msecs_to_jiffies(delay));
496out:
497	mutex_unlock(&mvm->mutex);
498}
499
500int
501iwl_mvm_tdls_channel_switch(struct ieee80211_hw *hw,
502			    struct ieee80211_vif *vif,
503			    struct ieee80211_sta *sta, u8 oper_class,
504			    struct cfg80211_chan_def *chandef,
505			    struct sk_buff *tmpl_skb, u32 ch_sw_tm_ie)
506{
507	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
508	struct iwl_mvm_sta *mvmsta;
509	unsigned int delay;
510	int ret;
511
512	mutex_lock(&mvm->mutex);
513
514	IWL_DEBUG_TDLS(mvm, "TDLS channel switch with %pM ch %d width %d\n",
515		       sta->addr, chandef->chan->center_freq, chandef->width);
516
517	/* we only support a single peer for channel switching */
518	if (mvm->tdls_cs.peer.sta_id != IWL_MVM_INVALID_STA) {
519		IWL_DEBUG_TDLS(mvm,
520			       "Existing peer. Can't start switch with %pM\n",
521			       sta->addr);
522		ret = -EBUSY;
523		goto out;
524	}
525
526	ret = iwl_mvm_tdls_config_channel_switch(mvm, vif,
527						 TDLS_SEND_CHAN_SW_REQ,
528						 sta->addr, sta->tdls_initiator,
529						 oper_class, chandef, 0, 0, 0,
530						 tmpl_skb, ch_sw_tm_ie);
531	if (ret)
532		goto out;
533
534	/*
535	 * Mark the peer as "in tdls switch" for this vif. We only allow a
536	 * single such peer per vif.
537	 */
538	mvm->tdls_cs.peer.skb = skb_copy(tmpl_skb, GFP_KERNEL);
539	if (!mvm->tdls_cs.peer.skb) {
540		ret = -ENOMEM;
541		goto out;
542	}
543
544	mvmsta = iwl_mvm_sta_from_mac80211(sta);
545	mvm->tdls_cs.peer.sta_id = mvmsta->deflink.sta_id;
546	mvm->tdls_cs.peer.chandef = *chandef;
547	mvm->tdls_cs.peer.initiator = sta->tdls_initiator;
548	mvm->tdls_cs.peer.op_class = oper_class;
549	mvm->tdls_cs.peer.ch_sw_tm_ie = ch_sw_tm_ie;
550
551	/*
552	 * Wait for 2 DTIM periods before attempting the next switch. The next
553	 * switch will be made sooner if the current one completes before that.
554	 */
555	delay = 2 * TU_TO_MS(vif->bss_conf.dtim_period *
556			     vif->bss_conf.beacon_int);
557	mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
558			 msecs_to_jiffies(delay));
559
560out:
561	mutex_unlock(&mvm->mutex);
562	return ret;
563}
564
565void iwl_mvm_tdls_cancel_channel_switch(struct ieee80211_hw *hw,
566					struct ieee80211_vif *vif,
567					struct ieee80211_sta *sta)
568{
569	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
570	struct ieee80211_sta *cur_sta;
571	bool wait_for_phy = false;
572
573	mutex_lock(&mvm->mutex);
574
575	IWL_DEBUG_TDLS(mvm, "TDLS cancel channel switch with %pM\n", sta->addr);
576
577	/* we only support a single peer for channel switching */
578	if (mvm->tdls_cs.peer.sta_id == IWL_MVM_INVALID_STA) {
579		IWL_DEBUG_TDLS(mvm, "No ch switch peer - %pM\n", sta->addr);
580		goto out;
581	}
582
583	cur_sta = rcu_dereference_protected(
584				mvm->fw_id_to_mac_id[mvm->tdls_cs.peer.sta_id],
585				lockdep_is_held(&mvm->mutex));
586	/* make sure it's the same peer */
587	if (cur_sta != sta)
588		goto out;
589
590	/*
591	 * If we're currently in a switch because of the now canceled peer,
592	 * wait a DTIM here to make sure the phy is back on the base channel.
593	 * We can't otherwise force it.
594	 */
595	if (mvm->tdls_cs.cur_sta_id == mvm->tdls_cs.peer.sta_id &&
596	    mvm->tdls_cs.state != IWL_MVM_TDLS_SW_IDLE)
597		wait_for_phy = true;
598
599	mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
600	dev_kfree_skb(mvm->tdls_cs.peer.skb);
601	mvm->tdls_cs.peer.skb = NULL;
602
603out:
604	mutex_unlock(&mvm->mutex);
605
606	/* make sure the phy is on the base channel */
607	if (wait_for_phy)
608		msleep(TU_TO_MS(vif->bss_conf.dtim_period *
609				vif->bss_conf.beacon_int));
610
611	/* flush the channel switch state */
612	flush_delayed_work(&mvm->tdls_cs.dwork);
613
614	IWL_DEBUG_TDLS(mvm, "TDLS ending channel switch with %pM\n", sta->addr);
615}
616
617void
618iwl_mvm_tdls_recv_channel_switch(struct ieee80211_hw *hw,
619				 struct ieee80211_vif *vif,
620				 struct ieee80211_tdls_ch_sw_params *params)
621{
622	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
623	enum iwl_tdls_channel_switch_type type;
624	unsigned int delay;
625	const char *action_str =
626		params->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ?
627		"REQ" : "RESP";
628
629	mutex_lock(&mvm->mutex);
630
631	IWL_DEBUG_TDLS(mvm,
632		       "Received TDLS ch switch action %s from %pM status %d\n",
633		       action_str, params->sta->addr, params->status);
634
635	/*
636	 * we got a non-zero status from a peer we were switching to - move to
637	 * the idle state and retry again later
638	 */
639	if (params->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE &&
640	    params->status != 0 &&
641	    mvm->tdls_cs.state == IWL_MVM_TDLS_SW_REQ_SENT &&
642	    mvm->tdls_cs.cur_sta_id != IWL_MVM_INVALID_STA) {
643		struct ieee80211_sta *cur_sta;
644
645		/* make sure it's the same peer */
646		cur_sta = rcu_dereference_protected(
647				mvm->fw_id_to_mac_id[mvm->tdls_cs.cur_sta_id],
648				lockdep_is_held(&mvm->mutex));
649		if (cur_sta == params->sta) {
650			iwl_mvm_tdls_update_cs_state(mvm,
651						     IWL_MVM_TDLS_SW_IDLE);
652			goto retry;
653		}
654	}
655
656	type = (params->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST) ?
657	       TDLS_SEND_CHAN_SW_RESP_AND_MOVE_CH : TDLS_MOVE_CH;
658
659	iwl_mvm_tdls_config_channel_switch(mvm, vif, type, params->sta->addr,
660					   params->sta->tdls_initiator, 0,
661					   params->chandef, params->timestamp,
662					   params->switch_time,
663					   params->switch_timeout,
664					   params->tmpl_skb,
665					   params->ch_sw_tm_ie);
666
667retry:
668	/* register a timeout in case we don't succeed in switching */
669	delay = vif->bss_conf.dtim_period * vif->bss_conf.beacon_int *
670		1024 / 1000;
671	mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
672			 msecs_to_jiffies(delay));
673	mutex_unlock(&mvm->mutex);
674}
v5.4
  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
 72void 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
 94int 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
121static 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
179void 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
202void 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	mutex_lock(&mvm->mutex);
209	/* Protect the session to hear the TDLS setup response on the channel */
210	iwl_mvm_protect_session(mvm, vif, duration, duration, 100, true);
 
 
 
 
 
211	mutex_unlock(&mvm->mutex);
212}
213
214static const char *
215iwl_mvm_tdls_cs_state_str(enum iwl_mvm_tdls_cs_state state)
216{
217	switch (state) {
218	case IWL_MVM_TDLS_SW_IDLE:
219		return "IDLE";
220	case IWL_MVM_TDLS_SW_REQ_SENT:
221		return "REQ SENT";
222	case IWL_MVM_TDLS_SW_RESP_RCVD:
223		return "RESP RECEIVED";
224	case IWL_MVM_TDLS_SW_REQ_RCVD:
225		return "REQ RECEIVED";
226	case IWL_MVM_TDLS_SW_ACTIVE:
227		return "ACTIVE";
228	}
229
230	return NULL;
231}
232
233static void iwl_mvm_tdls_update_cs_state(struct iwl_mvm *mvm,
234					 enum iwl_mvm_tdls_cs_state state)
235{
236	if (mvm->tdls_cs.state == state)
237		return;
238
239	IWL_DEBUG_TDLS(mvm, "TDLS channel switch state: %s -> %s\n",
240		       iwl_mvm_tdls_cs_state_str(mvm->tdls_cs.state),
241		       iwl_mvm_tdls_cs_state_str(state));
242	mvm->tdls_cs.state = state;
243
244	/* we only send requests to our switching peer - update sent time */
245	if (state == IWL_MVM_TDLS_SW_REQ_SENT)
246		mvm->tdls_cs.peer.sent_timestamp = iwl_mvm_get_systime(mvm);
247
248	if (state == IWL_MVM_TDLS_SW_IDLE)
249		mvm->tdls_cs.cur_sta_id = IWL_MVM_INVALID_STA;
250}
251
252void iwl_mvm_rx_tdls_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
253{
254	struct iwl_rx_packet *pkt = rxb_addr(rxb);
255	struct iwl_tdls_channel_switch_notif *notif = (void *)pkt->data;
256	struct ieee80211_sta *sta;
257	unsigned int delay;
258	struct iwl_mvm_sta *mvmsta;
259	struct ieee80211_vif *vif;
260	u32 sta_id = le32_to_cpu(notif->sta_id);
261
262	lockdep_assert_held(&mvm->mutex);
263
264	/* can fail sometimes */
265	if (!le32_to_cpu(notif->status)) {
266		iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
267		return;
268	}
269
270	if (WARN_ON(sta_id >= IWL_MVM_STATION_COUNT))
271		return;
272
273	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
274					lockdep_is_held(&mvm->mutex));
275	/* the station may not be here, but if it is, it must be a TDLS peer */
276	if (IS_ERR_OR_NULL(sta) || WARN_ON(!sta->tdls))
277		return;
278
279	mvmsta = iwl_mvm_sta_from_mac80211(sta);
280	vif = mvmsta->vif;
281
282	/*
283	 * Update state and possibly switch again after this is over (DTIM).
284	 * Also convert TU to msec.
285	 */
286	delay = TU_TO_MS(vif->bss_conf.dtim_period * vif->bss_conf.beacon_int);
287	mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
288			 msecs_to_jiffies(delay));
289
290	iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_ACTIVE);
291}
292
293static int
294iwl_mvm_tdls_check_action(struct iwl_mvm *mvm,
295			  enum iwl_tdls_channel_switch_type type,
296			  const u8 *peer, bool peer_initiator, u32 timestamp)
297{
298	bool same_peer = false;
299	int ret = 0;
300
301	/* get the existing peer if it's there */
302	if (mvm->tdls_cs.state != IWL_MVM_TDLS_SW_IDLE &&
303	    mvm->tdls_cs.cur_sta_id != IWL_MVM_INVALID_STA) {
304		struct ieee80211_sta *sta = rcu_dereference_protected(
305				mvm->fw_id_to_mac_id[mvm->tdls_cs.cur_sta_id],
306				lockdep_is_held(&mvm->mutex));
307		if (!IS_ERR_OR_NULL(sta))
308			same_peer = ether_addr_equal(peer, sta->addr);
309	}
310
311	switch (mvm->tdls_cs.state) {
312	case IWL_MVM_TDLS_SW_IDLE:
313		/*
314		 * might be spurious packet from the peer after the switch is
315		 * already done
316		 */
317		if (type == TDLS_MOVE_CH)
318			ret = -EINVAL;
319		break;
320	case IWL_MVM_TDLS_SW_REQ_SENT:
321		/* only allow requests from the same peer */
322		if (!same_peer)
323			ret = -EBUSY;
324		else if (type == TDLS_SEND_CHAN_SW_RESP_AND_MOVE_CH &&
325			 !peer_initiator)
326			/*
327			 * We received a ch-switch request while an outgoing
328			 * one is pending. Allow it if the peer is the link
329			 * initiator.
330			 */
331			ret = -EBUSY;
332		else if (type == TDLS_SEND_CHAN_SW_REQ)
333			/* wait for idle before sending another request */
334			ret = -EBUSY;
335		else if (timestamp <= mvm->tdls_cs.peer.sent_timestamp)
336			/* we got a stale response - ignore it */
337			ret = -EINVAL;
338		break;
339	case IWL_MVM_TDLS_SW_RESP_RCVD:
340		/*
341		 * we are waiting for the FW to give an "active" notification,
342		 * so ignore requests in the meantime
343		 */
344		ret = -EBUSY;
345		break;
346	case IWL_MVM_TDLS_SW_REQ_RCVD:
347		/* as above, allow the link initiator to proceed */
348		if (type == TDLS_SEND_CHAN_SW_REQ) {
349			if (!same_peer)
350				ret = -EBUSY;
351			else if (peer_initiator) /* they are the initiator */
352				ret = -EBUSY;
353		} else if (type == TDLS_MOVE_CH) {
354			ret = -EINVAL;
355		}
356		break;
357	case IWL_MVM_TDLS_SW_ACTIVE:
358		/*
359		 * the only valid request when active is a request to return
360		 * to the base channel by the current off-channel peer
361		 */
362		if (type != TDLS_MOVE_CH || !same_peer)
363			ret = -EBUSY;
364		break;
365	}
366
367	if (ret)
368		IWL_DEBUG_TDLS(mvm,
369			       "Invalid TDLS action %d state %d peer %pM same_peer %d initiator %d\n",
370			       type, mvm->tdls_cs.state, peer, same_peer,
371			       peer_initiator);
372
373	return ret;
374}
375
376static int
377iwl_mvm_tdls_config_channel_switch(struct iwl_mvm *mvm,
378				   struct ieee80211_vif *vif,
379				   enum iwl_tdls_channel_switch_type type,
380				   const u8 *peer, bool peer_initiator,
381				   u8 oper_class,
382				   struct cfg80211_chan_def *chandef,
383				   u32 timestamp, u16 switch_time,
384				   u16 switch_timeout, struct sk_buff *skb,
385				   u32 ch_sw_tm_ie)
386{
387	struct ieee80211_sta *sta;
388	struct iwl_mvm_sta *mvmsta;
389	struct ieee80211_tx_info *info;
390	struct ieee80211_hdr *hdr;
391	struct iwl_tdls_channel_switch_cmd cmd = {0};
392	struct iwl_tdls_channel_switch_cmd_tail *tail =
393		iwl_mvm_chan_info_cmd_tail(mvm, &cmd.ci);
394	u16 len = sizeof(cmd) - iwl_mvm_chan_info_padding(mvm);
395	int ret;
396
397	lockdep_assert_held(&mvm->mutex);
398
399	ret = iwl_mvm_tdls_check_action(mvm, type, peer, peer_initiator,
400					timestamp);
401	if (ret)
402		return ret;
403
404	if (!skb || WARN_ON(skb->len > IWL_TDLS_CH_SW_FRAME_MAX_SIZE)) {
405		ret = -EINVAL;
406		goto out;
407	}
408
409	cmd.switch_type = type;
410	tail->timing.frame_timestamp = cpu_to_le32(timestamp);
411	tail->timing.switch_time = cpu_to_le32(switch_time);
412	tail->timing.switch_timeout = cpu_to_le32(switch_timeout);
413
414	rcu_read_lock();
415	sta = ieee80211_find_sta(vif, peer);
416	if (!sta) {
417		rcu_read_unlock();
418		ret = -ENOENT;
419		goto out;
420	}
421	mvmsta = iwl_mvm_sta_from_mac80211(sta);
422	cmd.peer_sta_id = cpu_to_le32(mvmsta->sta_id);
423
424	if (!chandef) {
425		if (mvm->tdls_cs.state == IWL_MVM_TDLS_SW_REQ_SENT &&
426		    mvm->tdls_cs.peer.chandef.chan) {
427			/* actually moving to the channel */
428			chandef = &mvm->tdls_cs.peer.chandef;
429		} else if (mvm->tdls_cs.state == IWL_MVM_TDLS_SW_ACTIVE &&
430			   type == TDLS_MOVE_CH) {
431			/* we need to return to base channel */
432			struct ieee80211_chanctx_conf *chanctx =
433					rcu_dereference(vif->chanctx_conf);
434
435			if (WARN_ON_ONCE(!chanctx)) {
436				rcu_read_unlock();
437				goto out;
438			}
439
440			chandef = &chanctx->def;
441		}
442	}
443
444	if (chandef)
445		iwl_mvm_set_chan_info_chandef(mvm, &cmd.ci, chandef);
446
447	/* keep quota calculation simple for now - 50% of DTIM for TDLS */
448	tail->timing.max_offchan_duration =
449			cpu_to_le32(TU_TO_US(vif->bss_conf.dtim_period *
450					     vif->bss_conf.beacon_int) / 2);
451
452	/* Switch time is the first element in the switch-timing IE. */
453	tail->frame.switch_time_offset = cpu_to_le32(ch_sw_tm_ie + 2);
454
455	info = IEEE80211_SKB_CB(skb);
456	hdr = (void *)skb->data;
457	if (info->control.hw_key) {
458		if (info->control.hw_key->cipher != WLAN_CIPHER_SUITE_CCMP) {
459			rcu_read_unlock();
460			ret = -EINVAL;
461			goto out;
462		}
463		iwl_mvm_set_tx_cmd_ccmp(info, &tail->frame.tx_cmd);
464	}
465
466	iwl_mvm_set_tx_cmd(mvm, skb, &tail->frame.tx_cmd, info,
467			   mvmsta->sta_id);
468
469	iwl_mvm_set_tx_cmd_rate(mvm, &tail->frame.tx_cmd, info, sta,
470				hdr->frame_control);
471	rcu_read_unlock();
472
473	memcpy(tail->frame.data, skb->data, skb->len);
474
475	ret = iwl_mvm_send_cmd_pdu(mvm, TDLS_CHANNEL_SWITCH_CMD, 0, len, &cmd);
476	if (ret) {
477		IWL_ERR(mvm, "Failed to send TDLS_CHANNEL_SWITCH cmd: %d\n",
478			ret);
479		goto out;
480	}
481
482	/* channel switch has started, update state */
483	if (type != TDLS_MOVE_CH) {
484		mvm->tdls_cs.cur_sta_id = mvmsta->sta_id;
485		iwl_mvm_tdls_update_cs_state(mvm,
486					     type == TDLS_SEND_CHAN_SW_REQ ?
487					     IWL_MVM_TDLS_SW_REQ_SENT :
488					     IWL_MVM_TDLS_SW_REQ_RCVD);
489	} else {
490		iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_RESP_RCVD);
491	}
492
493out:
494
495	/* channel switch failed - we are idle */
496	if (ret)
497		iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
498
499	return ret;
500}
501
502void iwl_mvm_tdls_ch_switch_work(struct work_struct *work)
503{
504	struct iwl_mvm *mvm;
505	struct ieee80211_sta *sta;
506	struct iwl_mvm_sta *mvmsta;
507	struct ieee80211_vif *vif;
508	unsigned int delay;
509	int ret;
510
511	mvm = container_of(work, struct iwl_mvm, tdls_cs.dwork.work);
512	mutex_lock(&mvm->mutex);
513
514	/* called after an active channel switch has finished or timed-out */
515	iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
516
517	/* station might be gone, in that case do nothing */
518	if (mvm->tdls_cs.peer.sta_id == IWL_MVM_INVALID_STA)
519		goto out;
520
521	sta = rcu_dereference_protected(
522				mvm->fw_id_to_mac_id[mvm->tdls_cs.peer.sta_id],
523				lockdep_is_held(&mvm->mutex));
524	/* the station may not be here, but if it is, it must be a TDLS peer */
525	if (!sta || IS_ERR(sta) || WARN_ON(!sta->tdls))
526		goto out;
527
528	mvmsta = iwl_mvm_sta_from_mac80211(sta);
529	vif = mvmsta->vif;
530	ret = iwl_mvm_tdls_config_channel_switch(mvm, vif,
531						 TDLS_SEND_CHAN_SW_REQ,
532						 sta->addr,
533						 mvm->tdls_cs.peer.initiator,
534						 mvm->tdls_cs.peer.op_class,
535						 &mvm->tdls_cs.peer.chandef,
536						 0, 0, 0,
537						 mvm->tdls_cs.peer.skb,
538						 mvm->tdls_cs.peer.ch_sw_tm_ie);
539	if (ret)
540		IWL_ERR(mvm, "Not sending TDLS channel switch: %d\n", ret);
541
542	/* retry after a DTIM if we failed sending now */
543	delay = TU_TO_MS(vif->bss_conf.dtim_period * vif->bss_conf.beacon_int);
544	schedule_delayed_work(&mvm->tdls_cs.dwork, msecs_to_jiffies(delay));
545out:
546	mutex_unlock(&mvm->mutex);
547}
548
549int
550iwl_mvm_tdls_channel_switch(struct ieee80211_hw *hw,
551			    struct ieee80211_vif *vif,
552			    struct ieee80211_sta *sta, u8 oper_class,
553			    struct cfg80211_chan_def *chandef,
554			    struct sk_buff *tmpl_skb, u32 ch_sw_tm_ie)
555{
556	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
557	struct iwl_mvm_sta *mvmsta;
558	unsigned int delay;
559	int ret;
560
561	mutex_lock(&mvm->mutex);
562
563	IWL_DEBUG_TDLS(mvm, "TDLS channel switch with %pM ch %d width %d\n",
564		       sta->addr, chandef->chan->center_freq, chandef->width);
565
566	/* we only support a single peer for channel switching */
567	if (mvm->tdls_cs.peer.sta_id != IWL_MVM_INVALID_STA) {
568		IWL_DEBUG_TDLS(mvm,
569			       "Existing peer. Can't start switch with %pM\n",
570			       sta->addr);
571		ret = -EBUSY;
572		goto out;
573	}
574
575	ret = iwl_mvm_tdls_config_channel_switch(mvm, vif,
576						 TDLS_SEND_CHAN_SW_REQ,
577						 sta->addr, sta->tdls_initiator,
578						 oper_class, chandef, 0, 0, 0,
579						 tmpl_skb, ch_sw_tm_ie);
580	if (ret)
581		goto out;
582
583	/*
584	 * Mark the peer as "in tdls switch" for this vif. We only allow a
585	 * single such peer per vif.
586	 */
587	mvm->tdls_cs.peer.skb = skb_copy(tmpl_skb, GFP_KERNEL);
588	if (!mvm->tdls_cs.peer.skb) {
589		ret = -ENOMEM;
590		goto out;
591	}
592
593	mvmsta = iwl_mvm_sta_from_mac80211(sta);
594	mvm->tdls_cs.peer.sta_id = mvmsta->sta_id;
595	mvm->tdls_cs.peer.chandef = *chandef;
596	mvm->tdls_cs.peer.initiator = sta->tdls_initiator;
597	mvm->tdls_cs.peer.op_class = oper_class;
598	mvm->tdls_cs.peer.ch_sw_tm_ie = ch_sw_tm_ie;
599
600	/*
601	 * Wait for 2 DTIM periods before attempting the next switch. The next
602	 * switch will be made sooner if the current one completes before that.
603	 */
604	delay = 2 * TU_TO_MS(vif->bss_conf.dtim_period *
605			     vif->bss_conf.beacon_int);
606	mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
607			 msecs_to_jiffies(delay));
608
609out:
610	mutex_unlock(&mvm->mutex);
611	return ret;
612}
613
614void iwl_mvm_tdls_cancel_channel_switch(struct ieee80211_hw *hw,
615					struct ieee80211_vif *vif,
616					struct ieee80211_sta *sta)
617{
618	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
619	struct ieee80211_sta *cur_sta;
620	bool wait_for_phy = false;
621
622	mutex_lock(&mvm->mutex);
623
624	IWL_DEBUG_TDLS(mvm, "TDLS cancel channel switch with %pM\n", sta->addr);
625
626	/* we only support a single peer for channel switching */
627	if (mvm->tdls_cs.peer.sta_id == IWL_MVM_INVALID_STA) {
628		IWL_DEBUG_TDLS(mvm, "No ch switch peer - %pM\n", sta->addr);
629		goto out;
630	}
631
632	cur_sta = rcu_dereference_protected(
633				mvm->fw_id_to_mac_id[mvm->tdls_cs.peer.sta_id],
634				lockdep_is_held(&mvm->mutex));
635	/* make sure it's the same peer */
636	if (cur_sta != sta)
637		goto out;
638
639	/*
640	 * If we're currently in a switch because of the now canceled peer,
641	 * wait a DTIM here to make sure the phy is back on the base channel.
642	 * We can't otherwise force it.
643	 */
644	if (mvm->tdls_cs.cur_sta_id == mvm->tdls_cs.peer.sta_id &&
645	    mvm->tdls_cs.state != IWL_MVM_TDLS_SW_IDLE)
646		wait_for_phy = true;
647
648	mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
649	dev_kfree_skb(mvm->tdls_cs.peer.skb);
650	mvm->tdls_cs.peer.skb = NULL;
651
652out:
653	mutex_unlock(&mvm->mutex);
654
655	/* make sure the phy is on the base channel */
656	if (wait_for_phy)
657		msleep(TU_TO_MS(vif->bss_conf.dtim_period *
658				vif->bss_conf.beacon_int));
659
660	/* flush the channel switch state */
661	flush_delayed_work(&mvm->tdls_cs.dwork);
662
663	IWL_DEBUG_TDLS(mvm, "TDLS ending channel switch with %pM\n", sta->addr);
664}
665
666void
667iwl_mvm_tdls_recv_channel_switch(struct ieee80211_hw *hw,
668				 struct ieee80211_vif *vif,
669				 struct ieee80211_tdls_ch_sw_params *params)
670{
671	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
672	enum iwl_tdls_channel_switch_type type;
673	unsigned int delay;
674	const char *action_str =
675		params->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ?
676		"REQ" : "RESP";
677
678	mutex_lock(&mvm->mutex);
679
680	IWL_DEBUG_TDLS(mvm,
681		       "Received TDLS ch switch action %s from %pM status %d\n",
682		       action_str, params->sta->addr, params->status);
683
684	/*
685	 * we got a non-zero status from a peer we were switching to - move to
686	 * the idle state and retry again later
687	 */
688	if (params->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE &&
689	    params->status != 0 &&
690	    mvm->tdls_cs.state == IWL_MVM_TDLS_SW_REQ_SENT &&
691	    mvm->tdls_cs.cur_sta_id != IWL_MVM_INVALID_STA) {
692		struct ieee80211_sta *cur_sta;
693
694		/* make sure it's the same peer */
695		cur_sta = rcu_dereference_protected(
696				mvm->fw_id_to_mac_id[mvm->tdls_cs.cur_sta_id],
697				lockdep_is_held(&mvm->mutex));
698		if (cur_sta == params->sta) {
699			iwl_mvm_tdls_update_cs_state(mvm,
700						     IWL_MVM_TDLS_SW_IDLE);
701			goto retry;
702		}
703	}
704
705	type = (params->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST) ?
706	       TDLS_SEND_CHAN_SW_RESP_AND_MOVE_CH : TDLS_MOVE_CH;
707
708	iwl_mvm_tdls_config_channel_switch(mvm, vif, type, params->sta->addr,
709					   params->sta->tdls_initiator, 0,
710					   params->chandef, params->timestamp,
711					   params->switch_time,
712					   params->switch_timeout,
713					   params->tmpl_skb,
714					   params->ch_sw_tm_ie);
715
716retry:
717	/* register a timeout in case we don't succeed in switching */
718	delay = vif->bss_conf.dtim_period * vif->bss_conf.beacon_int *
719		1024 / 1000;
720	mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
721			 msecs_to_jiffies(delay));
722	mutex_unlock(&mvm->mutex);
723}