xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/time-event.c (revision 9af1bba4)
1bfcc09ddSBjoern A. Zeeb // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2bfcc09ddSBjoern A. Zeeb /*
39af1bba4SBjoern A. Zeeb  * Copyright (C) 2012-2014, 2018-2023 Intel Corporation
4bfcc09ddSBjoern A. Zeeb  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5bfcc09ddSBjoern A. Zeeb  * Copyright (C) 2017 Intel Deutschland GmbH
6bfcc09ddSBjoern A. Zeeb  */
7bfcc09ddSBjoern A. Zeeb #include <linux/jiffies.h>
8bfcc09ddSBjoern A. Zeeb #include <net/mac80211.h>
9bfcc09ddSBjoern A. Zeeb 
10bfcc09ddSBjoern A. Zeeb #include "fw/notif-wait.h"
11bfcc09ddSBjoern A. Zeeb #include "iwl-trans.h"
12bfcc09ddSBjoern A. Zeeb #include "fw-api.h"
13bfcc09ddSBjoern A. Zeeb #include "time-event.h"
14bfcc09ddSBjoern A. Zeeb #include "mvm.h"
15bfcc09ddSBjoern A. Zeeb #include "iwl-io.h"
16bfcc09ddSBjoern A. Zeeb #include "iwl-prph.h"
17bfcc09ddSBjoern A. Zeeb 
18bfcc09ddSBjoern A. Zeeb /*
19bfcc09ddSBjoern A. Zeeb  * For the high priority TE use a time event type that has similar priority to
20bfcc09ddSBjoern A. Zeeb  * the FW's action scan priority.
21bfcc09ddSBjoern A. Zeeb  */
22bfcc09ddSBjoern A. Zeeb #define IWL_MVM_ROC_TE_TYPE_NORMAL TE_P2P_DEVICE_DISCOVERABLE
23bfcc09ddSBjoern A. Zeeb #define IWL_MVM_ROC_TE_TYPE_MGMT_TX TE_P2P_CLIENT_ASSOC
24bfcc09ddSBjoern A. Zeeb 
iwl_mvm_te_clear_data(struct iwl_mvm * mvm,struct iwl_mvm_time_event_data * te_data)25bfcc09ddSBjoern A. Zeeb void iwl_mvm_te_clear_data(struct iwl_mvm *mvm,
26bfcc09ddSBjoern A. Zeeb 			   struct iwl_mvm_time_event_data *te_data)
27bfcc09ddSBjoern A. Zeeb {
28bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->time_event_lock);
29bfcc09ddSBjoern A. Zeeb 
30bfcc09ddSBjoern A. Zeeb 	if (!te_data || !te_data->vif)
31bfcc09ddSBjoern A. Zeeb 		return;
32bfcc09ddSBjoern A. Zeeb 
33bfcc09ddSBjoern A. Zeeb 	list_del(&te_data->list);
34bfcc09ddSBjoern A. Zeeb 
35bfcc09ddSBjoern A. Zeeb 	/*
36bfcc09ddSBjoern A. Zeeb 	 * the list is only used for AUX ROC events so make sure it is always
37bfcc09ddSBjoern A. Zeeb 	 * initialized
38bfcc09ddSBjoern A. Zeeb 	 */
39bfcc09ddSBjoern A. Zeeb 	INIT_LIST_HEAD(&te_data->list);
40bfcc09ddSBjoern A. Zeeb 
41bfcc09ddSBjoern A. Zeeb 	te_data->running = false;
42bfcc09ddSBjoern A. Zeeb 	te_data->uid = 0;
43bfcc09ddSBjoern A. Zeeb 	te_data->id = TE_MAX;
44bfcc09ddSBjoern A. Zeeb 	te_data->vif = NULL;
45bfcc09ddSBjoern A. Zeeb }
46bfcc09ddSBjoern A. Zeeb 
iwl_mvm_roc_done_wk(struct work_struct * wk)47bfcc09ddSBjoern A. Zeeb void iwl_mvm_roc_done_wk(struct work_struct *wk)
48bfcc09ddSBjoern A. Zeeb {
49bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, roc_done_wk);
50bfcc09ddSBjoern A. Zeeb 
51bfcc09ddSBjoern A. Zeeb 	/*
52d9836fb4SBjoern A. Zeeb 	 * Clear the ROC_RUNNING status bit.
53bfcc09ddSBjoern A. Zeeb 	 * This will cause the TX path to drop offchannel transmissions.
54bfcc09ddSBjoern A. Zeeb 	 * That would also be done by mac80211, but it is racy, in particular
55bfcc09ddSBjoern A. Zeeb 	 * in the case that the time event actually completed in the firmware
56bfcc09ddSBjoern A. Zeeb 	 * (which is handled in iwl_mvm_te_handle_notif).
57bfcc09ddSBjoern A. Zeeb 	 */
58bfcc09ddSBjoern A. Zeeb 	clear_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
59bfcc09ddSBjoern A. Zeeb 
60bfcc09ddSBjoern A. Zeeb 	synchronize_net();
61bfcc09ddSBjoern A. Zeeb 
62bfcc09ddSBjoern A. Zeeb 	/*
63bfcc09ddSBjoern A. Zeeb 	 * Flush the offchannel queue -- this is called when the time
64bfcc09ddSBjoern A. Zeeb 	 * event finishes or is canceled, so that frames queued for it
65bfcc09ddSBjoern A. Zeeb 	 * won't get stuck on the queue and be transmitted in the next
66bfcc09ddSBjoern A. Zeeb 	 * time event.
67bfcc09ddSBjoern A. Zeeb 	 */
68bfcc09ddSBjoern A. Zeeb 
69bfcc09ddSBjoern A. Zeeb 	mutex_lock(&mvm->mutex);
70bfcc09ddSBjoern A. Zeeb 	if (test_and_clear_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status)) {
71bfcc09ddSBjoern A. Zeeb 		struct iwl_mvm_vif *mvmvif;
72bfcc09ddSBjoern A. Zeeb 
73bfcc09ddSBjoern A. Zeeb 		/*
74bfcc09ddSBjoern A. Zeeb 		 * NB: access to this pointer would be racy, but the flush bit
75bfcc09ddSBjoern A. Zeeb 		 * can only be set when we had a P2P-Device VIF, and we have a
76bfcc09ddSBjoern A. Zeeb 		 * flush of this work in iwl_mvm_prepare_mac_removal() so it's
77bfcc09ddSBjoern A. Zeeb 		 * not really racy.
78bfcc09ddSBjoern A. Zeeb 		 */
79bfcc09ddSBjoern A. Zeeb 
80bfcc09ddSBjoern A. Zeeb 		if (!WARN_ON(!mvm->p2p_device_vif)) {
81bfcc09ddSBjoern A. Zeeb 			mvmvif = iwl_mvm_vif_from_mac80211(mvm->p2p_device_vif);
829af1bba4SBjoern A. Zeeb 			iwl_mvm_flush_sta(mvm, &mvmvif->deflink.bcast_sta,
839af1bba4SBjoern A. Zeeb 					  true);
84bfcc09ddSBjoern A. Zeeb 		}
85d9836fb4SBjoern A. Zeeb 	}
86d9836fb4SBjoern A. Zeeb 
87d9836fb4SBjoern A. Zeeb 	/*
88d9836fb4SBjoern A. Zeeb 	 * Clear the ROC_AUX_RUNNING status bit.
89d9836fb4SBjoern A. Zeeb 	 * This will cause the TX path to drop offchannel transmissions.
90d9836fb4SBjoern A. Zeeb 	 * That would also be done by mac80211, but it is racy, in particular
91d9836fb4SBjoern A. Zeeb 	 * in the case that the time event actually completed in the firmware
92d9836fb4SBjoern A. Zeeb 	 * (which is handled in iwl_mvm_te_handle_notif).
93d9836fb4SBjoern A. Zeeb 	 */
94d9836fb4SBjoern A. Zeeb 	if (test_and_clear_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status)) {
95bfcc09ddSBjoern A. Zeeb 		/* do the same in case of hot spot 2.0 */
96bfcc09ddSBjoern A. Zeeb 		iwl_mvm_flush_sta(mvm, &mvm->aux_sta, true);
97d9836fb4SBjoern A. Zeeb 
989af1bba4SBjoern A. Zeeb 		if (mvm->mld_api_is_used) {
999af1bba4SBjoern A. Zeeb 			iwl_mvm_mld_rm_aux_sta(mvm);
1009af1bba4SBjoern A. Zeeb 			goto out_unlock;
1019af1bba4SBjoern A. Zeeb 		}
1029af1bba4SBjoern A. Zeeb 
103bfcc09ddSBjoern A. Zeeb 		/* In newer version of this command an aux station is added only
104bfcc09ddSBjoern A. Zeeb 		 * in cases of dedicated tx queue and need to be removed in end
105bfcc09ddSBjoern A. Zeeb 		 * of use */
1069af1bba4SBjoern A. Zeeb 		if (iwl_mvm_has_new_station_api(mvm->fw))
107bfcc09ddSBjoern A. Zeeb 			iwl_mvm_rm_aux_sta(mvm);
108bfcc09ddSBjoern A. Zeeb 	}
109bfcc09ddSBjoern A. Zeeb 
1109af1bba4SBjoern A. Zeeb out_unlock:
111bfcc09ddSBjoern A. Zeeb 	mutex_unlock(&mvm->mutex);
112bfcc09ddSBjoern A. Zeeb }
113bfcc09ddSBjoern A. Zeeb 
iwl_mvm_roc_finished(struct iwl_mvm * mvm)114bfcc09ddSBjoern A. Zeeb static void iwl_mvm_roc_finished(struct iwl_mvm *mvm)
115bfcc09ddSBjoern A. Zeeb {
116bfcc09ddSBjoern A. Zeeb 	/*
117bfcc09ddSBjoern A. Zeeb 	 * Of course, our status bit is just as racy as mac80211, so in
118bfcc09ddSBjoern A. Zeeb 	 * addition, fire off the work struct which will drop all frames
119bfcc09ddSBjoern A. Zeeb 	 * from the hardware queues that made it through the race. First
120bfcc09ddSBjoern A. Zeeb 	 * it will of course synchronize the TX path to make sure that
121bfcc09ddSBjoern A. Zeeb 	 * any *new* TX will be rejected.
122bfcc09ddSBjoern A. Zeeb 	 */
123bfcc09ddSBjoern A. Zeeb 	schedule_work(&mvm->roc_done_wk);
124bfcc09ddSBjoern A. Zeeb }
125bfcc09ddSBjoern A. Zeeb 
iwl_mvm_csa_noa_start(struct iwl_mvm * mvm)126bfcc09ddSBjoern A. Zeeb static void iwl_mvm_csa_noa_start(struct iwl_mvm *mvm)
127bfcc09ddSBjoern A. Zeeb {
128bfcc09ddSBjoern A. Zeeb 	struct ieee80211_vif *csa_vif;
129bfcc09ddSBjoern A. Zeeb 
130bfcc09ddSBjoern A. Zeeb 	rcu_read_lock();
131bfcc09ddSBjoern A. Zeeb 
132bfcc09ddSBjoern A. Zeeb 	csa_vif = rcu_dereference(mvm->csa_vif);
1339af1bba4SBjoern A. Zeeb 	if (!csa_vif || !csa_vif->bss_conf.csa_active)
134bfcc09ddSBjoern A. Zeeb 		goto out_unlock;
135bfcc09ddSBjoern A. Zeeb 
136bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "CSA NOA started\n");
137bfcc09ddSBjoern A. Zeeb 
138bfcc09ddSBjoern A. Zeeb 	/*
139bfcc09ddSBjoern A. Zeeb 	 * CSA NoA is started but we still have beacons to
140bfcc09ddSBjoern A. Zeeb 	 * transmit on the current channel.
141bfcc09ddSBjoern A. Zeeb 	 * So we just do nothing here and the switch
142bfcc09ddSBjoern A. Zeeb 	 * will be performed on the last TBTT.
143bfcc09ddSBjoern A. Zeeb 	 */
144bfcc09ddSBjoern A. Zeeb 	if (!ieee80211_beacon_cntdwn_is_complete(csa_vif)) {
145bfcc09ddSBjoern A. Zeeb 		IWL_WARN(mvm, "CSA NOA started too early\n");
146bfcc09ddSBjoern A. Zeeb 		goto out_unlock;
147bfcc09ddSBjoern A. Zeeb 	}
148bfcc09ddSBjoern A. Zeeb 
149bfcc09ddSBjoern A. Zeeb 	ieee80211_csa_finish(csa_vif);
150bfcc09ddSBjoern A. Zeeb 
151bfcc09ddSBjoern A. Zeeb 	rcu_read_unlock();
152bfcc09ddSBjoern A. Zeeb 
153bfcc09ddSBjoern A. Zeeb 	RCU_INIT_POINTER(mvm->csa_vif, NULL);
154bfcc09ddSBjoern A. Zeeb 
155bfcc09ddSBjoern A. Zeeb 	return;
156bfcc09ddSBjoern A. Zeeb 
157bfcc09ddSBjoern A. Zeeb out_unlock:
158bfcc09ddSBjoern A. Zeeb 	rcu_read_unlock();
159bfcc09ddSBjoern A. Zeeb }
160bfcc09ddSBjoern A. Zeeb 
iwl_mvm_te_check_disconnect(struct iwl_mvm * mvm,struct ieee80211_vif * vif,const char * errmsg)161bfcc09ddSBjoern A. Zeeb static bool iwl_mvm_te_check_disconnect(struct iwl_mvm *mvm,
162bfcc09ddSBjoern A. Zeeb 					struct ieee80211_vif *vif,
163bfcc09ddSBjoern A. Zeeb 					const char *errmsg)
164bfcc09ddSBjoern A. Zeeb {
165bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
166bfcc09ddSBjoern A. Zeeb 
167bfcc09ddSBjoern A. Zeeb 	if (vif->type != NL80211_IFTYPE_STATION)
168bfcc09ddSBjoern A. Zeeb 		return false;
169bfcc09ddSBjoern A. Zeeb 
1709af1bba4SBjoern A. Zeeb 	if (!mvmvif->csa_bcn_pending && vif->cfg.assoc &&
171bfcc09ddSBjoern A. Zeeb 	    vif->bss_conf.dtim_period)
172bfcc09ddSBjoern A. Zeeb 		return false;
173bfcc09ddSBjoern A. Zeeb 	if (errmsg)
174bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "%s\n", errmsg);
175bfcc09ddSBjoern A. Zeeb 
176bfcc09ddSBjoern A. Zeeb 	if (mvmvif->csa_bcn_pending) {
177bfcc09ddSBjoern A. Zeeb 		struct iwl_mvm_sta *mvmsta;
178bfcc09ddSBjoern A. Zeeb 
179bfcc09ddSBjoern A. Zeeb 		rcu_read_lock();
1809af1bba4SBjoern A. Zeeb 		mvmsta = iwl_mvm_sta_from_staid_rcu(mvm,
1819af1bba4SBjoern A. Zeeb 						    mvmvif->deflink.ap_sta_id);
182bfcc09ddSBjoern A. Zeeb 		if (!WARN_ON(!mvmsta))
183bfcc09ddSBjoern A. Zeeb 			iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
184bfcc09ddSBjoern A. Zeeb 		rcu_read_unlock();
185bfcc09ddSBjoern A. Zeeb 	}
186bfcc09ddSBjoern A. Zeeb 
1879af1bba4SBjoern A. Zeeb 	if (vif->cfg.assoc) {
188bfcc09ddSBjoern A. Zeeb 		/*
189bfcc09ddSBjoern A. Zeeb 		 * When not associated, this will be called from
190bfcc09ddSBjoern A. Zeeb 		 * iwl_mvm_event_mlme_callback_ini()
191bfcc09ddSBjoern A. Zeeb 		 */
192bfcc09ddSBjoern A. Zeeb 		iwl_dbg_tlv_time_point(&mvm->fwrt,
193bfcc09ddSBjoern A. Zeeb 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
194bfcc09ddSBjoern A. Zeeb 				       NULL);
195bfcc09ddSBjoern A. Zeeb 	}
196bfcc09ddSBjoern A. Zeeb 
197bfcc09ddSBjoern A. Zeeb 	iwl_mvm_connection_loss(mvm, vif, errmsg);
198bfcc09ddSBjoern A. Zeeb 	return true;
199bfcc09ddSBjoern A. Zeeb }
200bfcc09ddSBjoern A. Zeeb 
201bfcc09ddSBjoern A. Zeeb static void
iwl_mvm_te_handle_notify_csa(struct iwl_mvm * mvm,struct iwl_mvm_time_event_data * te_data,struct iwl_time_event_notif * notif)202bfcc09ddSBjoern A. Zeeb iwl_mvm_te_handle_notify_csa(struct iwl_mvm *mvm,
203bfcc09ddSBjoern A. Zeeb 			     struct iwl_mvm_time_event_data *te_data,
204bfcc09ddSBjoern A. Zeeb 			     struct iwl_time_event_notif *notif)
205bfcc09ddSBjoern A. Zeeb {
206bfcc09ddSBjoern A. Zeeb 	struct ieee80211_vif *vif = te_data->vif;
207bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
208bfcc09ddSBjoern A. Zeeb 
209bfcc09ddSBjoern A. Zeeb 	if (!notif->status)
210bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm, "CSA time event failed to start\n");
211bfcc09ddSBjoern A. Zeeb 
212bfcc09ddSBjoern A. Zeeb 	switch (te_data->vif->type) {
213bfcc09ddSBjoern A. Zeeb 	case NL80211_IFTYPE_AP:
214bfcc09ddSBjoern A. Zeeb 		if (!notif->status)
215bfcc09ddSBjoern A. Zeeb 			mvmvif->csa_failed = true;
216bfcc09ddSBjoern A. Zeeb 		iwl_mvm_csa_noa_start(mvm);
217bfcc09ddSBjoern A. Zeeb 		break;
218bfcc09ddSBjoern A. Zeeb 	case NL80211_IFTYPE_STATION:
219bfcc09ddSBjoern A. Zeeb 		if (!notif->status) {
220bfcc09ddSBjoern A. Zeeb 			iwl_mvm_connection_loss(mvm, vif,
221bfcc09ddSBjoern A. Zeeb 						"CSA TE failed to start");
222bfcc09ddSBjoern A. Zeeb 			break;
223bfcc09ddSBjoern A. Zeeb 		}
224bfcc09ddSBjoern A. Zeeb 		iwl_mvm_csa_client_absent(mvm, te_data->vif);
225bfcc09ddSBjoern A. Zeeb 		cancel_delayed_work(&mvmvif->csa_work);
226bfcc09ddSBjoern A. Zeeb 		ieee80211_chswitch_done(te_data->vif, true);
227bfcc09ddSBjoern A. Zeeb 		break;
228bfcc09ddSBjoern A. Zeeb 	default:
229bfcc09ddSBjoern A. Zeeb 		/* should never happen */
230bfcc09ddSBjoern A. Zeeb 		WARN_ON_ONCE(1);
231bfcc09ddSBjoern A. Zeeb 		break;
232bfcc09ddSBjoern A. Zeeb 	}
233bfcc09ddSBjoern A. Zeeb 
234bfcc09ddSBjoern A. Zeeb 	/* we don't need it anymore */
235bfcc09ddSBjoern A. Zeeb 	iwl_mvm_te_clear_data(mvm, te_data);
236bfcc09ddSBjoern A. Zeeb }
237bfcc09ddSBjoern A. Zeeb 
iwl_mvm_te_check_trigger(struct iwl_mvm * mvm,struct iwl_time_event_notif * notif,struct iwl_mvm_time_event_data * te_data)238bfcc09ddSBjoern A. Zeeb static void iwl_mvm_te_check_trigger(struct iwl_mvm *mvm,
239bfcc09ddSBjoern A. Zeeb 				     struct iwl_time_event_notif *notif,
240bfcc09ddSBjoern A. Zeeb 				     struct iwl_mvm_time_event_data *te_data)
241bfcc09ddSBjoern A. Zeeb {
242bfcc09ddSBjoern A. Zeeb 	struct iwl_fw_dbg_trigger_tlv *trig;
243bfcc09ddSBjoern A. Zeeb 	struct iwl_fw_dbg_trigger_time_event *te_trig;
244bfcc09ddSBjoern A. Zeeb 	int i;
245bfcc09ddSBjoern A. Zeeb 
246bfcc09ddSBjoern A. Zeeb 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
247bfcc09ddSBjoern A. Zeeb 				     ieee80211_vif_to_wdev(te_data->vif),
248bfcc09ddSBjoern A. Zeeb 				     FW_DBG_TRIGGER_TIME_EVENT);
249bfcc09ddSBjoern A. Zeeb 	if (!trig)
250bfcc09ddSBjoern A. Zeeb 		return;
251bfcc09ddSBjoern A. Zeeb 
252bfcc09ddSBjoern A. Zeeb 	te_trig = (void *)trig->data;
253bfcc09ddSBjoern A. Zeeb 
254bfcc09ddSBjoern A. Zeeb 	for (i = 0; i < ARRAY_SIZE(te_trig->time_events); i++) {
255bfcc09ddSBjoern A. Zeeb 		u32 trig_te_id = le32_to_cpu(te_trig->time_events[i].id);
256bfcc09ddSBjoern A. Zeeb 		u32 trig_action_bitmap =
257bfcc09ddSBjoern A. Zeeb 			le32_to_cpu(te_trig->time_events[i].action_bitmap);
258bfcc09ddSBjoern A. Zeeb 		u32 trig_status_bitmap =
259bfcc09ddSBjoern A. Zeeb 			le32_to_cpu(te_trig->time_events[i].status_bitmap);
260bfcc09ddSBjoern A. Zeeb 
261bfcc09ddSBjoern A. Zeeb 		if (trig_te_id != te_data->id ||
262bfcc09ddSBjoern A. Zeeb 		    !(trig_action_bitmap & le32_to_cpu(notif->action)) ||
263bfcc09ddSBjoern A. Zeeb 		    !(trig_status_bitmap & BIT(le32_to_cpu(notif->status))))
264bfcc09ddSBjoern A. Zeeb 			continue;
265bfcc09ddSBjoern A. Zeeb 
266bfcc09ddSBjoern A. Zeeb 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
267bfcc09ddSBjoern A. Zeeb 					"Time event %d Action 0x%x received status: %d",
268bfcc09ddSBjoern A. Zeeb 					te_data->id,
269bfcc09ddSBjoern A. Zeeb 					le32_to_cpu(notif->action),
270bfcc09ddSBjoern A. Zeeb 					le32_to_cpu(notif->status));
271bfcc09ddSBjoern A. Zeeb 		break;
272bfcc09ddSBjoern A. Zeeb 	}
273bfcc09ddSBjoern A. Zeeb }
274bfcc09ddSBjoern A. Zeeb 
iwl_mvm_p2p_roc_finished(struct iwl_mvm * mvm)275bfcc09ddSBjoern A. Zeeb static void iwl_mvm_p2p_roc_finished(struct iwl_mvm *mvm)
276bfcc09ddSBjoern A. Zeeb {
277bfcc09ddSBjoern A. Zeeb 	/*
278bfcc09ddSBjoern A. Zeeb 	 * If the IWL_MVM_STATUS_NEED_FLUSH_P2P is already set, then the
279bfcc09ddSBjoern A. Zeeb 	 * roc_done_wk is already scheduled or running, so don't schedule it
280bfcc09ddSBjoern A. Zeeb 	 * again to avoid a race where the roc_done_wk clears this bit after
281bfcc09ddSBjoern A. Zeeb 	 * it is set here, affecting the next run of the roc_done_wk.
282bfcc09ddSBjoern A. Zeeb 	 */
283bfcc09ddSBjoern A. Zeeb 	if (!test_and_set_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status))
284bfcc09ddSBjoern A. Zeeb 		iwl_mvm_roc_finished(mvm);
285bfcc09ddSBjoern A. Zeeb }
286bfcc09ddSBjoern A. Zeeb 
287bfcc09ddSBjoern A. Zeeb /*
288bfcc09ddSBjoern A. Zeeb  * Handles a FW notification for an event that is known to the driver.
289bfcc09ddSBjoern A. Zeeb  *
290bfcc09ddSBjoern A. Zeeb  * @mvm: the mvm component
291bfcc09ddSBjoern A. Zeeb  * @te_data: the time event data
292bfcc09ddSBjoern A. Zeeb  * @notif: the notification data corresponding the time event data.
293bfcc09ddSBjoern A. Zeeb  */
iwl_mvm_te_handle_notif(struct iwl_mvm * mvm,struct iwl_mvm_time_event_data * te_data,struct iwl_time_event_notif * notif)294bfcc09ddSBjoern A. Zeeb static void iwl_mvm_te_handle_notif(struct iwl_mvm *mvm,
295bfcc09ddSBjoern A. Zeeb 				    struct iwl_mvm_time_event_data *te_data,
296bfcc09ddSBjoern A. Zeeb 				    struct iwl_time_event_notif *notif)
297bfcc09ddSBjoern A. Zeeb {
298bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->time_event_lock);
299bfcc09ddSBjoern A. Zeeb 
300bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "Handle time event notif - UID = 0x%x action %d\n",
301bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(notif->unique_id),
302bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(notif->action));
303bfcc09ddSBjoern A. Zeeb 
304bfcc09ddSBjoern A. Zeeb 	iwl_mvm_te_check_trigger(mvm, notif, te_data);
305bfcc09ddSBjoern A. Zeeb 
306bfcc09ddSBjoern A. Zeeb 	/*
307bfcc09ddSBjoern A. Zeeb 	 * The FW sends the start/end time event notifications even for events
308bfcc09ddSBjoern A. Zeeb 	 * that it fails to schedule. This is indicated in the status field of
309bfcc09ddSBjoern A. Zeeb 	 * the notification. This happens in cases that the scheduler cannot
310bfcc09ddSBjoern A. Zeeb 	 * find a schedule that can handle the event (for example requesting a
311bfcc09ddSBjoern A. Zeeb 	 * P2P Device discoveribility, while there are other higher priority
312bfcc09ddSBjoern A. Zeeb 	 * events in the system).
313bfcc09ddSBjoern A. Zeeb 	 */
314bfcc09ddSBjoern A. Zeeb 	if (!le32_to_cpu(notif->status)) {
315bfcc09ddSBjoern A. Zeeb 		const char *msg;
316bfcc09ddSBjoern A. Zeeb 
317bfcc09ddSBjoern A. Zeeb 		if (notif->action & cpu_to_le32(TE_V2_NOTIF_HOST_EVENT_START))
318bfcc09ddSBjoern A. Zeeb 			msg = "Time Event start notification failure";
319bfcc09ddSBjoern A. Zeeb 		else
320bfcc09ddSBjoern A. Zeeb 			msg = "Time Event end notification failure";
321bfcc09ddSBjoern A. Zeeb 
322bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm, "%s\n", msg);
323bfcc09ddSBjoern A. Zeeb 
324bfcc09ddSBjoern A. Zeeb 		if (iwl_mvm_te_check_disconnect(mvm, te_data->vif, msg)) {
325bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_clear_data(mvm, te_data);
326bfcc09ddSBjoern A. Zeeb 			return;
327bfcc09ddSBjoern A. Zeeb 		}
328bfcc09ddSBjoern A. Zeeb 	}
329bfcc09ddSBjoern A. Zeeb 
330bfcc09ddSBjoern A. Zeeb 	if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_END) {
331bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm,
332bfcc09ddSBjoern A. Zeeb 			     "TE ended - current time %lu, estimated end %lu\n",
333bfcc09ddSBjoern A. Zeeb 			     jiffies, te_data->end_jiffies);
334bfcc09ddSBjoern A. Zeeb 
335bfcc09ddSBjoern A. Zeeb 		switch (te_data->vif->type) {
336bfcc09ddSBjoern A. Zeeb 		case NL80211_IFTYPE_P2P_DEVICE:
337bfcc09ddSBjoern A. Zeeb 			ieee80211_remain_on_channel_expired(mvm->hw);
338bfcc09ddSBjoern A. Zeeb 			iwl_mvm_p2p_roc_finished(mvm);
339bfcc09ddSBjoern A. Zeeb 			break;
340bfcc09ddSBjoern A. Zeeb 		case NL80211_IFTYPE_STATION:
341bfcc09ddSBjoern A. Zeeb 			/*
342bfcc09ddSBjoern A. Zeeb 			 * If we are switching channel, don't disconnect
343bfcc09ddSBjoern A. Zeeb 			 * if the time event is already done. Beacons can
344bfcc09ddSBjoern A. Zeeb 			 * be delayed a bit after the switch.
345bfcc09ddSBjoern A. Zeeb 			 */
346bfcc09ddSBjoern A. Zeeb 			if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
347bfcc09ddSBjoern A. Zeeb 				IWL_DEBUG_TE(mvm,
348bfcc09ddSBjoern A. Zeeb 					     "No beacon heard and the CS time event is over, don't disconnect\n");
349bfcc09ddSBjoern A. Zeeb 				break;
350bfcc09ddSBjoern A. Zeeb 			}
351bfcc09ddSBjoern A. Zeeb 
352bfcc09ddSBjoern A. Zeeb 			/*
353bfcc09ddSBjoern A. Zeeb 			 * By now, we should have finished association
354bfcc09ddSBjoern A. Zeeb 			 * and know the dtim period.
355bfcc09ddSBjoern A. Zeeb 			 */
356bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_check_disconnect(mvm, te_data->vif,
3579af1bba4SBjoern A. Zeeb 				!te_data->vif->cfg.assoc ?
358bfcc09ddSBjoern A. Zeeb 				"Not associated and the time event is over already..." :
359bfcc09ddSBjoern A. Zeeb 				"No beacon heard and the time event is over already...");
360bfcc09ddSBjoern A. Zeeb 			break;
361bfcc09ddSBjoern A. Zeeb 		default:
362bfcc09ddSBjoern A. Zeeb 			break;
363bfcc09ddSBjoern A. Zeeb 		}
364bfcc09ddSBjoern A. Zeeb 
365bfcc09ddSBjoern A. Zeeb 		iwl_mvm_te_clear_data(mvm, te_data);
366bfcc09ddSBjoern A. Zeeb 	} else if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_START) {
367bfcc09ddSBjoern A. Zeeb 		te_data->running = true;
368bfcc09ddSBjoern A. Zeeb 		te_data->end_jiffies = TU_TO_EXP_TIME(te_data->duration);
369bfcc09ddSBjoern A. Zeeb 
370bfcc09ddSBjoern A. Zeeb 		if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
371bfcc09ddSBjoern A. Zeeb 			set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
372bfcc09ddSBjoern A. Zeeb 			ieee80211_ready_on_channel(mvm->hw);
373bfcc09ddSBjoern A. Zeeb 		} else if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
374bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_handle_notify_csa(mvm, te_data, notif);
375bfcc09ddSBjoern A. Zeeb 		}
376bfcc09ddSBjoern A. Zeeb 	} else {
377bfcc09ddSBjoern A. Zeeb 		IWL_WARN(mvm, "Got TE with unknown action\n");
378bfcc09ddSBjoern A. Zeeb 	}
379bfcc09ddSBjoern A. Zeeb }
380bfcc09ddSBjoern A. Zeeb 
381bfcc09ddSBjoern A. Zeeb /*
382bfcc09ddSBjoern A. Zeeb  * Handle A Aux ROC time event
383bfcc09ddSBjoern A. Zeeb  */
iwl_mvm_aux_roc_te_handle_notif(struct iwl_mvm * mvm,struct iwl_time_event_notif * notif)384bfcc09ddSBjoern A. Zeeb static int iwl_mvm_aux_roc_te_handle_notif(struct iwl_mvm *mvm,
385bfcc09ddSBjoern A. Zeeb 					   struct iwl_time_event_notif *notif)
386bfcc09ddSBjoern A. Zeeb {
3879af1bba4SBjoern A. Zeeb 	struct iwl_mvm_time_event_data *aux_roc_te = NULL, *te_data;
388bfcc09ddSBjoern A. Zeeb 
3899af1bba4SBjoern A. Zeeb 	list_for_each_entry(te_data, &mvm->aux_roc_te_list, list) {
390bfcc09ddSBjoern A. Zeeb 		if (le32_to_cpu(notif->unique_id) == te_data->uid) {
3919af1bba4SBjoern A. Zeeb 			aux_roc_te = te_data;
392bfcc09ddSBjoern A. Zeeb 			break;
393bfcc09ddSBjoern A. Zeeb 		}
394bfcc09ddSBjoern A. Zeeb 	}
395bfcc09ddSBjoern A. Zeeb 	if (!aux_roc_te) /* Not a Aux ROC time event */
396bfcc09ddSBjoern A. Zeeb 		return -EINVAL;
397bfcc09ddSBjoern A. Zeeb 
398bfcc09ddSBjoern A. Zeeb 	iwl_mvm_te_check_trigger(mvm, notif, te_data);
399bfcc09ddSBjoern A. Zeeb 
400bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm,
401bfcc09ddSBjoern A. Zeeb 		     "Aux ROC time event notification  - UID = 0x%x action %d (error = %d)\n",
402bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(notif->unique_id),
403bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(notif->action), le32_to_cpu(notif->status));
404bfcc09ddSBjoern A. Zeeb 
405bfcc09ddSBjoern A. Zeeb 	if (!le32_to_cpu(notif->status) ||
406bfcc09ddSBjoern A. Zeeb 	    le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_END) {
407bfcc09ddSBjoern A. Zeeb 		/* End TE, notify mac80211 */
408bfcc09ddSBjoern A. Zeeb 		ieee80211_remain_on_channel_expired(mvm->hw);
409bfcc09ddSBjoern A. Zeeb 		iwl_mvm_roc_finished(mvm); /* flush aux queue */
410bfcc09ddSBjoern A. Zeeb 		list_del(&te_data->list); /* remove from list */
411bfcc09ddSBjoern A. Zeeb 		te_data->running = false;
412bfcc09ddSBjoern A. Zeeb 		te_data->vif = NULL;
413bfcc09ddSBjoern A. Zeeb 		te_data->uid = 0;
414bfcc09ddSBjoern A. Zeeb 		te_data->id = TE_MAX;
415bfcc09ddSBjoern A. Zeeb 	} else if (le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_START) {
416bfcc09ddSBjoern A. Zeeb 		set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
417bfcc09ddSBjoern A. Zeeb 		te_data->running = true;
418bfcc09ddSBjoern A. Zeeb 		ieee80211_ready_on_channel(mvm->hw); /* Start TE */
419bfcc09ddSBjoern A. Zeeb 	} else {
420bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm,
421bfcc09ddSBjoern A. Zeeb 			     "ERROR: Unknown Aux ROC Time Event (action = %d)\n",
422bfcc09ddSBjoern A. Zeeb 			     le32_to_cpu(notif->action));
423bfcc09ddSBjoern A. Zeeb 		return -EINVAL;
424bfcc09ddSBjoern A. Zeeb 	}
425bfcc09ddSBjoern A. Zeeb 
426bfcc09ddSBjoern A. Zeeb 	return 0;
427bfcc09ddSBjoern A. Zeeb }
428bfcc09ddSBjoern A. Zeeb 
429bfcc09ddSBjoern A. Zeeb /*
430bfcc09ddSBjoern A. Zeeb  * The Rx handler for time event notifications
431bfcc09ddSBjoern A. Zeeb  */
iwl_mvm_rx_time_event_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)432bfcc09ddSBjoern A. Zeeb void iwl_mvm_rx_time_event_notif(struct iwl_mvm *mvm,
433bfcc09ddSBjoern A. Zeeb 				 struct iwl_rx_cmd_buffer *rxb)
434bfcc09ddSBjoern A. Zeeb {
435bfcc09ddSBjoern A. Zeeb 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
436bfcc09ddSBjoern A. Zeeb 	struct iwl_time_event_notif *notif = (void *)pkt->data;
437bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data, *tmp;
438bfcc09ddSBjoern A. Zeeb 
439bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "Time event notification - UID = 0x%x action %d\n",
440bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(notif->unique_id),
441bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(notif->action));
442bfcc09ddSBjoern A. Zeeb 
443bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
444bfcc09ddSBjoern A. Zeeb 	/* This time event is triggered for Aux ROC request */
445bfcc09ddSBjoern A. Zeeb 	if (!iwl_mvm_aux_roc_te_handle_notif(mvm, notif))
446bfcc09ddSBjoern A. Zeeb 		goto unlock;
447bfcc09ddSBjoern A. Zeeb 
448bfcc09ddSBjoern A. Zeeb 	list_for_each_entry_safe(te_data, tmp, &mvm->time_event_list, list) {
449bfcc09ddSBjoern A. Zeeb 		if (le32_to_cpu(notif->unique_id) == te_data->uid)
450bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_handle_notif(mvm, te_data, notif);
451bfcc09ddSBjoern A. Zeeb 	}
452bfcc09ddSBjoern A. Zeeb unlock:
453bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
454bfcc09ddSBjoern A. Zeeb }
455bfcc09ddSBjoern A. Zeeb 
iwl_mvm_te_notif(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)456bfcc09ddSBjoern A. Zeeb static bool iwl_mvm_te_notif(struct iwl_notif_wait_data *notif_wait,
457bfcc09ddSBjoern A. Zeeb 			     struct iwl_rx_packet *pkt, void *data)
458bfcc09ddSBjoern A. Zeeb {
459bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm *mvm =
460bfcc09ddSBjoern A. Zeeb 		container_of(notif_wait, struct iwl_mvm, notif_wait);
461bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = data;
462bfcc09ddSBjoern A. Zeeb 	struct iwl_time_event_notif *resp;
463bfcc09ddSBjoern A. Zeeb 	int resp_len = iwl_rx_packet_payload_len(pkt);
464bfcc09ddSBjoern A. Zeeb 
465bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_NOTIFICATION))
466bfcc09ddSBjoern A. Zeeb 		return true;
467bfcc09ddSBjoern A. Zeeb 
468bfcc09ddSBjoern A. Zeeb 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
469bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Invalid TIME_EVENT_NOTIFICATION response\n");
470bfcc09ddSBjoern A. Zeeb 		return true;
471bfcc09ddSBjoern A. Zeeb 	}
472bfcc09ddSBjoern A. Zeeb 
473bfcc09ddSBjoern A. Zeeb 	resp = (void *)pkt->data;
474bfcc09ddSBjoern A. Zeeb 
475bfcc09ddSBjoern A. Zeeb 	/* te_data->uid is already set in the TIME_EVENT_CMD response */
476bfcc09ddSBjoern A. Zeeb 	if (le32_to_cpu(resp->unique_id) != te_data->uid)
477bfcc09ddSBjoern A. Zeeb 		return false;
478bfcc09ddSBjoern A. Zeeb 
479bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n",
480bfcc09ddSBjoern A. Zeeb 		     te_data->uid);
481bfcc09ddSBjoern A. Zeeb 	if (!resp->status)
482bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm,
483bfcc09ddSBjoern A. Zeeb 			"TIME_EVENT_NOTIFICATION received but not executed\n");
484bfcc09ddSBjoern A. Zeeb 
485bfcc09ddSBjoern A. Zeeb 	return true;
486bfcc09ddSBjoern A. Zeeb }
487bfcc09ddSBjoern A. Zeeb 
iwl_mvm_time_event_response(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)488bfcc09ddSBjoern A. Zeeb static bool iwl_mvm_time_event_response(struct iwl_notif_wait_data *notif_wait,
489bfcc09ddSBjoern A. Zeeb 					struct iwl_rx_packet *pkt, void *data)
490bfcc09ddSBjoern A. Zeeb {
491bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm *mvm =
492bfcc09ddSBjoern A. Zeeb 		container_of(notif_wait, struct iwl_mvm, notif_wait);
493bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = data;
494bfcc09ddSBjoern A. Zeeb 	struct iwl_time_event_resp *resp;
495bfcc09ddSBjoern A. Zeeb 	int resp_len = iwl_rx_packet_payload_len(pkt);
496bfcc09ddSBjoern A. Zeeb 
497bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_CMD))
498bfcc09ddSBjoern A. Zeeb 		return true;
499bfcc09ddSBjoern A. Zeeb 
500bfcc09ddSBjoern A. Zeeb 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
501bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Invalid TIME_EVENT_CMD response\n");
502bfcc09ddSBjoern A. Zeeb 		return true;
503bfcc09ddSBjoern A. Zeeb 	}
504bfcc09ddSBjoern A. Zeeb 
505bfcc09ddSBjoern A. Zeeb 	resp = (void *)pkt->data;
506bfcc09ddSBjoern A. Zeeb 
507bfcc09ddSBjoern A. Zeeb 	/* we should never get a response to another TIME_EVENT_CMD here */
508bfcc09ddSBjoern A. Zeeb 	if (WARN_ON_ONCE(le32_to_cpu(resp->id) != te_data->id))
509bfcc09ddSBjoern A. Zeeb 		return false;
510bfcc09ddSBjoern A. Zeeb 
511bfcc09ddSBjoern A. Zeeb 	te_data->uid = le32_to_cpu(resp->unique_id);
512bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
513bfcc09ddSBjoern A. Zeeb 		     te_data->uid);
514bfcc09ddSBjoern A. Zeeb 	return true;
515bfcc09ddSBjoern A. Zeeb }
516bfcc09ddSBjoern A. Zeeb 
iwl_mvm_time_event_send_add(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mvm_time_event_data * te_data,struct iwl_time_event_cmd * te_cmd)517bfcc09ddSBjoern A. Zeeb static int iwl_mvm_time_event_send_add(struct iwl_mvm *mvm,
518bfcc09ddSBjoern A. Zeeb 				       struct ieee80211_vif *vif,
519bfcc09ddSBjoern A. Zeeb 				       struct iwl_mvm_time_event_data *te_data,
520bfcc09ddSBjoern A. Zeeb 				       struct iwl_time_event_cmd *te_cmd)
521bfcc09ddSBjoern A. Zeeb {
522bfcc09ddSBjoern A. Zeeb 	static const u16 time_event_response[] = { TIME_EVENT_CMD };
523bfcc09ddSBjoern A. Zeeb 	struct iwl_notification_wait wait_time_event;
524bfcc09ddSBjoern A. Zeeb 	int ret;
525bfcc09ddSBjoern A. Zeeb 
526bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
527bfcc09ddSBjoern A. Zeeb 
528bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "Add new TE, duration %d TU\n",
529bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(te_cmd->duration));
530bfcc09ddSBjoern A. Zeeb 
531bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
532bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(te_data->id != TE_MAX)) {
533bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&mvm->time_event_lock);
534bfcc09ddSBjoern A. Zeeb 		return -EIO;
535bfcc09ddSBjoern A. Zeeb 	}
536bfcc09ddSBjoern A. Zeeb 	te_data->vif = vif;
537bfcc09ddSBjoern A. Zeeb 	te_data->duration = le32_to_cpu(te_cmd->duration);
538bfcc09ddSBjoern A. Zeeb 	te_data->id = le32_to_cpu(te_cmd->id);
539bfcc09ddSBjoern A. Zeeb 	list_add_tail(&te_data->list, &mvm->time_event_list);
540bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
541bfcc09ddSBjoern A. Zeeb 
542bfcc09ddSBjoern A. Zeeb 	/*
543bfcc09ddSBjoern A. Zeeb 	 * Use a notification wait, which really just processes the
544bfcc09ddSBjoern A. Zeeb 	 * command response and doesn't wait for anything, in order
545bfcc09ddSBjoern A. Zeeb 	 * to be able to process the response and get the UID inside
546bfcc09ddSBjoern A. Zeeb 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
547bfcc09ddSBjoern A. Zeeb 	 * stores the buffer and then wakes up this thread, by which
548bfcc09ddSBjoern A. Zeeb 	 * time another notification (that the time event started)
549bfcc09ddSBjoern A. Zeeb 	 * might already be processed unsuccessfully.
550bfcc09ddSBjoern A. Zeeb 	 */
551bfcc09ddSBjoern A. Zeeb 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
552bfcc09ddSBjoern A. Zeeb 				   time_event_response,
553bfcc09ddSBjoern A. Zeeb 				   ARRAY_SIZE(time_event_response),
554bfcc09ddSBjoern A. Zeeb 				   iwl_mvm_time_event_response, te_data);
555bfcc09ddSBjoern A. Zeeb 
556bfcc09ddSBjoern A. Zeeb 	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
557bfcc09ddSBjoern A. Zeeb 					    sizeof(*te_cmd), te_cmd);
558bfcc09ddSBjoern A. Zeeb 	if (ret) {
559bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Couldn't send TIME_EVENT_CMD: %d\n", ret);
560bfcc09ddSBjoern A. Zeeb 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
561bfcc09ddSBjoern A. Zeeb 		goto out_clear_te;
562bfcc09ddSBjoern A. Zeeb 	}
563bfcc09ddSBjoern A. Zeeb 
564bfcc09ddSBjoern A. Zeeb 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
565bfcc09ddSBjoern A. Zeeb 	ret = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
566bfcc09ddSBjoern A. Zeeb 	/* should never fail */
567bfcc09ddSBjoern A. Zeeb 	WARN_ON_ONCE(ret);
568bfcc09ddSBjoern A. Zeeb 
569bfcc09ddSBjoern A. Zeeb 	if (ret) {
570bfcc09ddSBjoern A. Zeeb  out_clear_te:
571bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&mvm->time_event_lock);
572bfcc09ddSBjoern A. Zeeb 		iwl_mvm_te_clear_data(mvm, te_data);
573bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&mvm->time_event_lock);
574bfcc09ddSBjoern A. Zeeb 	}
575bfcc09ddSBjoern A. Zeeb 	return ret;
576bfcc09ddSBjoern A. Zeeb }
577bfcc09ddSBjoern A. Zeeb 
iwl_mvm_protect_session(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 duration,u32 min_duration,u32 max_delay,bool wait_for_notif)578bfcc09ddSBjoern A. Zeeb void iwl_mvm_protect_session(struct iwl_mvm *mvm,
579bfcc09ddSBjoern A. Zeeb 			     struct ieee80211_vif *vif,
580bfcc09ddSBjoern A. Zeeb 			     u32 duration, u32 min_duration,
581bfcc09ddSBjoern A. Zeeb 			     u32 max_delay, bool wait_for_notif)
582bfcc09ddSBjoern A. Zeeb {
583bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
584bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
585bfcc09ddSBjoern A. Zeeb 	const u16 te_notif_response[] = { TIME_EVENT_NOTIFICATION };
586bfcc09ddSBjoern A. Zeeb 	struct iwl_notification_wait wait_te_notif;
587bfcc09ddSBjoern A. Zeeb 	struct iwl_time_event_cmd time_cmd = {};
588bfcc09ddSBjoern A. Zeeb 
589bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
590bfcc09ddSBjoern A. Zeeb 
591bfcc09ddSBjoern A. Zeeb 	if (te_data->running &&
592bfcc09ddSBjoern A. Zeeb 	    time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
593bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
594bfcc09ddSBjoern A. Zeeb 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
595bfcc09ddSBjoern A. Zeeb 		return;
596bfcc09ddSBjoern A. Zeeb 	}
597bfcc09ddSBjoern A. Zeeb 
598bfcc09ddSBjoern A. Zeeb 	if (te_data->running) {
599bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm, "extend 0x%x: only %u ms left\n",
600bfcc09ddSBjoern A. Zeeb 			     te_data->uid,
601bfcc09ddSBjoern A. Zeeb 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
602bfcc09ddSBjoern A. Zeeb 		/*
603bfcc09ddSBjoern A. Zeeb 		 * we don't have enough time
604bfcc09ddSBjoern A. Zeeb 		 * cancel the current TE and issue a new one
605bfcc09ddSBjoern A. Zeeb 		 * Of course it would be better to remove the old one only
606bfcc09ddSBjoern A. Zeeb 		 * when the new one is added, but we don't care if we are off
607bfcc09ddSBjoern A. Zeeb 		 * channel for a bit. All we need to do, is not to return
608bfcc09ddSBjoern A. Zeeb 		 * before we actually begin to be on the channel.
609bfcc09ddSBjoern A. Zeeb 		 */
610bfcc09ddSBjoern A. Zeeb 		iwl_mvm_stop_session_protection(mvm, vif);
611bfcc09ddSBjoern A. Zeeb 	}
612bfcc09ddSBjoern A. Zeeb 
613bfcc09ddSBjoern A. Zeeb 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
614bfcc09ddSBjoern A. Zeeb 	time_cmd.id_and_color =
615bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
616bfcc09ddSBjoern A. Zeeb 	time_cmd.id = cpu_to_le32(TE_BSS_STA_AGGRESSIVE_ASSOC);
617bfcc09ddSBjoern A. Zeeb 
618bfcc09ddSBjoern A. Zeeb 	time_cmd.apply_time = cpu_to_le32(0);
619bfcc09ddSBjoern A. Zeeb 
620bfcc09ddSBjoern A. Zeeb 	time_cmd.max_frags = TE_V2_FRAG_NONE;
621bfcc09ddSBjoern A. Zeeb 	time_cmd.max_delay = cpu_to_le32(max_delay);
622bfcc09ddSBjoern A. Zeeb 	/* TODO: why do we need to interval = bi if it is not periodic? */
623bfcc09ddSBjoern A. Zeeb 	time_cmd.interval = cpu_to_le32(1);
624bfcc09ddSBjoern A. Zeeb 	time_cmd.duration = cpu_to_le32(duration);
625bfcc09ddSBjoern A. Zeeb 	time_cmd.repeat = 1;
626bfcc09ddSBjoern A. Zeeb 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
627bfcc09ddSBjoern A. Zeeb 				      TE_V2_NOTIF_HOST_EVENT_END |
628bfcc09ddSBjoern A. Zeeb 				      TE_V2_START_IMMEDIATELY);
629bfcc09ddSBjoern A. Zeeb 
630bfcc09ddSBjoern A. Zeeb 	if (!wait_for_notif) {
631bfcc09ddSBjoern A. Zeeb 		iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
632bfcc09ddSBjoern A. Zeeb 		return;
633bfcc09ddSBjoern A. Zeeb 	}
634bfcc09ddSBjoern A. Zeeb 
635bfcc09ddSBjoern A. Zeeb 	/*
636bfcc09ddSBjoern A. Zeeb 	 * Create notification_wait for the TIME_EVENT_NOTIFICATION to use
637bfcc09ddSBjoern A. Zeeb 	 * right after we send the time event
638bfcc09ddSBjoern A. Zeeb 	 */
639bfcc09ddSBjoern A. Zeeb 	iwl_init_notification_wait(&mvm->notif_wait, &wait_te_notif,
640bfcc09ddSBjoern A. Zeeb 				   te_notif_response,
641bfcc09ddSBjoern A. Zeeb 				   ARRAY_SIZE(te_notif_response),
642bfcc09ddSBjoern A. Zeeb 				   iwl_mvm_te_notif, te_data);
643bfcc09ddSBjoern A. Zeeb 
644bfcc09ddSBjoern A. Zeeb 	/* If TE was sent OK - wait for the notification that started */
645bfcc09ddSBjoern A. Zeeb 	if (iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd)) {
646bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Failed to add TE to protect session\n");
647bfcc09ddSBjoern A. Zeeb 		iwl_remove_notification(&mvm->notif_wait, &wait_te_notif);
648bfcc09ddSBjoern A. Zeeb 	} else if (iwl_wait_notification(&mvm->notif_wait, &wait_te_notif,
649bfcc09ddSBjoern A. Zeeb 					 TU_TO_JIFFIES(max_delay))) {
650bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Failed to protect session until TE\n");
651bfcc09ddSBjoern A. Zeeb 	}
652bfcc09ddSBjoern A. Zeeb }
653bfcc09ddSBjoern A. Zeeb 
iwl_mvm_cancel_session_protection(struct iwl_mvm * mvm,struct iwl_mvm_vif * mvmvif,u32 id)654bfcc09ddSBjoern A. Zeeb static void iwl_mvm_cancel_session_protection(struct iwl_mvm *mvm,
655bfcc09ddSBjoern A. Zeeb 					      struct iwl_mvm_vif *mvmvif,
656bfcc09ddSBjoern A. Zeeb 					      u32 id)
657bfcc09ddSBjoern A. Zeeb {
658bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_session_prot_cmd cmd = {
659bfcc09ddSBjoern A. Zeeb 		.id_and_color =
660bfcc09ddSBjoern A. Zeeb 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
661bfcc09ddSBjoern A. Zeeb 							mvmvif->color)),
662bfcc09ddSBjoern A. Zeeb 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
663bfcc09ddSBjoern A. Zeeb 		.conf_id = cpu_to_le32(id),
664bfcc09ddSBjoern A. Zeeb 	};
665bfcc09ddSBjoern A. Zeeb 	int ret;
666bfcc09ddSBjoern A. Zeeb 
667d9836fb4SBjoern A. Zeeb 	ret = iwl_mvm_send_cmd_pdu(mvm,
668d9836fb4SBjoern A. Zeeb 				   WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
669bfcc09ddSBjoern A. Zeeb 				   0, sizeof(cmd), &cmd);
670bfcc09ddSBjoern A. Zeeb 	if (ret)
671bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm,
672bfcc09ddSBjoern A. Zeeb 			"Couldn't send the SESSION_PROTECTION_CMD: %d\n", ret);
673bfcc09ddSBjoern A. Zeeb }
674bfcc09ddSBjoern A. Zeeb 
__iwl_mvm_remove_time_event(struct iwl_mvm * mvm,struct iwl_mvm_time_event_data * te_data,u32 * uid)675bfcc09ddSBjoern A. Zeeb static bool __iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
676bfcc09ddSBjoern A. Zeeb 					struct iwl_mvm_time_event_data *te_data,
677bfcc09ddSBjoern A. Zeeb 					u32 *uid)
678bfcc09ddSBjoern A. Zeeb {
679bfcc09ddSBjoern A. Zeeb 	u32 id;
680bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif;
681bfcc09ddSBjoern A. Zeeb 	enum nl80211_iftype iftype;
682bfcc09ddSBjoern A. Zeeb 
683bfcc09ddSBjoern A. Zeeb 	if (!te_data->vif)
684bfcc09ddSBjoern A. Zeeb 		return false;
685bfcc09ddSBjoern A. Zeeb 
686bfcc09ddSBjoern A. Zeeb 	mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
687bfcc09ddSBjoern A. Zeeb 	iftype = te_data->vif->type;
688bfcc09ddSBjoern A. Zeeb 
689bfcc09ddSBjoern A. Zeeb 	/*
690bfcc09ddSBjoern A. Zeeb 	 * It is possible that by the time we got to this point the time
691bfcc09ddSBjoern A. Zeeb 	 * event was already removed.
692bfcc09ddSBjoern A. Zeeb 	 */
693bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
694bfcc09ddSBjoern A. Zeeb 
695bfcc09ddSBjoern A. Zeeb 	/* Save time event uid before clearing its data */
696bfcc09ddSBjoern A. Zeeb 	*uid = te_data->uid;
697bfcc09ddSBjoern A. Zeeb 	id = te_data->id;
698bfcc09ddSBjoern A. Zeeb 
699bfcc09ddSBjoern A. Zeeb 	/*
700bfcc09ddSBjoern A. Zeeb 	 * The clear_data function handles time events that were already removed
701bfcc09ddSBjoern A. Zeeb 	 */
702bfcc09ddSBjoern A. Zeeb 	iwl_mvm_te_clear_data(mvm, te_data);
703bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
704bfcc09ddSBjoern A. Zeeb 
705d9836fb4SBjoern A. Zeeb 	/* When session protection is used, the te_data->id field
706bfcc09ddSBjoern A. Zeeb 	 * is reused to save session protection's configuration.
707d9836fb4SBjoern A. Zeeb 	 * For AUX ROC, HOT_SPOT_CMD is used and the te_data->id field is set
708d9836fb4SBjoern A. Zeeb 	 * to HOT_SPOT_CMD.
709bfcc09ddSBjoern A. Zeeb 	 */
710bfcc09ddSBjoern A. Zeeb 	if (fw_has_capa(&mvm->fw->ucode_capa,
711d9836fb4SBjoern A. Zeeb 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD) &&
712d9836fb4SBjoern A. Zeeb 	    id != HOT_SPOT_CMD) {
713bfcc09ddSBjoern A. Zeeb 		if (mvmvif && id < SESSION_PROTECT_CONF_MAX_ID) {
714bfcc09ddSBjoern A. Zeeb 			/* Session protection is still ongoing. Cancel it */
715bfcc09ddSBjoern A. Zeeb 			iwl_mvm_cancel_session_protection(mvm, mvmvif, id);
716bfcc09ddSBjoern A. Zeeb 			if (iftype == NL80211_IFTYPE_P2P_DEVICE) {
717bfcc09ddSBjoern A. Zeeb 				iwl_mvm_p2p_roc_finished(mvm);
718bfcc09ddSBjoern A. Zeeb 			}
719bfcc09ddSBjoern A. Zeeb 		}
720bfcc09ddSBjoern A. Zeeb 		return false;
721bfcc09ddSBjoern A. Zeeb 	} else {
722bfcc09ddSBjoern A. Zeeb 		/* It is possible that by the time we try to remove it, the
723bfcc09ddSBjoern A. Zeeb 		 * time event has already ended and removed. In such a case
724bfcc09ddSBjoern A. Zeeb 		 * there is no need to send a removal command.
725bfcc09ddSBjoern A. Zeeb 		 */
726bfcc09ddSBjoern A. Zeeb 		if (id == TE_MAX) {
727bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", *uid);
728bfcc09ddSBjoern A. Zeeb 			return false;
729bfcc09ddSBjoern A. Zeeb 		}
730bfcc09ddSBjoern A. Zeeb 	}
731bfcc09ddSBjoern A. Zeeb 
732bfcc09ddSBjoern A. Zeeb 	return true;
733bfcc09ddSBjoern A. Zeeb }
734bfcc09ddSBjoern A. Zeeb 
735bfcc09ddSBjoern A. Zeeb /*
736bfcc09ddSBjoern A. Zeeb  * Explicit request to remove a aux roc time event. The removal of a time
737bfcc09ddSBjoern A. Zeeb  * event needs to be synchronized with the flow of a time event's end
738bfcc09ddSBjoern A. Zeeb  * notification, which also removes the time event from the op mode
739bfcc09ddSBjoern A. Zeeb  * data structures.
740bfcc09ddSBjoern A. Zeeb  */
iwl_mvm_remove_aux_roc_te(struct iwl_mvm * mvm,struct iwl_mvm_vif * mvmvif,struct iwl_mvm_time_event_data * te_data)741bfcc09ddSBjoern A. Zeeb static void iwl_mvm_remove_aux_roc_te(struct iwl_mvm *mvm,
742bfcc09ddSBjoern A. Zeeb 				      struct iwl_mvm_vif *mvmvif,
743bfcc09ddSBjoern A. Zeeb 				      struct iwl_mvm_time_event_data *te_data)
744bfcc09ddSBjoern A. Zeeb {
745bfcc09ddSBjoern A. Zeeb 	struct iwl_hs20_roc_req aux_cmd = {};
746bfcc09ddSBjoern A. Zeeb 	u16 len = sizeof(aux_cmd) - iwl_mvm_chan_info_padding(mvm);
747bfcc09ddSBjoern A. Zeeb 
748bfcc09ddSBjoern A. Zeeb 	u32 uid;
749bfcc09ddSBjoern A. Zeeb 	int ret;
750bfcc09ddSBjoern A. Zeeb 
751bfcc09ddSBjoern A. Zeeb 	if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
752bfcc09ddSBjoern A. Zeeb 		return;
753bfcc09ddSBjoern A. Zeeb 
754bfcc09ddSBjoern A. Zeeb 	aux_cmd.event_unique_id = cpu_to_le32(uid);
755bfcc09ddSBjoern A. Zeeb 	aux_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
756bfcc09ddSBjoern A. Zeeb 	aux_cmd.id_and_color =
757bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
758bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "Removing BSS AUX ROC TE 0x%x\n",
759bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(aux_cmd.event_unique_id));
760bfcc09ddSBjoern A. Zeeb 	ret = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0,
761bfcc09ddSBjoern A. Zeeb 				   len, &aux_cmd);
762bfcc09ddSBjoern A. Zeeb 
763bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(ret))
764bfcc09ddSBjoern A. Zeeb 		return;
765bfcc09ddSBjoern A. Zeeb }
766bfcc09ddSBjoern A. Zeeb 
767bfcc09ddSBjoern A. Zeeb /*
768bfcc09ddSBjoern A. Zeeb  * Explicit request to remove a time event. The removal of a time event needs to
769bfcc09ddSBjoern A. Zeeb  * be synchronized with the flow of a time event's end notification, which also
770bfcc09ddSBjoern A. Zeeb  * removes the time event from the op mode data structures.
771bfcc09ddSBjoern A. Zeeb  */
iwl_mvm_remove_time_event(struct iwl_mvm * mvm,struct iwl_mvm_vif * mvmvif,struct iwl_mvm_time_event_data * te_data)772bfcc09ddSBjoern A. Zeeb void iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
773bfcc09ddSBjoern A. Zeeb 			       struct iwl_mvm_vif *mvmvif,
774bfcc09ddSBjoern A. Zeeb 			       struct iwl_mvm_time_event_data *te_data)
775bfcc09ddSBjoern A. Zeeb {
776bfcc09ddSBjoern A. Zeeb 	struct iwl_time_event_cmd time_cmd = {};
777bfcc09ddSBjoern A. Zeeb 	u32 uid;
778bfcc09ddSBjoern A. Zeeb 	int ret;
779bfcc09ddSBjoern A. Zeeb 
780bfcc09ddSBjoern A. Zeeb 	if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
781bfcc09ddSBjoern A. Zeeb 		return;
782bfcc09ddSBjoern A. Zeeb 
783bfcc09ddSBjoern A. Zeeb 	/* When we remove a TE, the UID is to be set in the id field */
784bfcc09ddSBjoern A. Zeeb 	time_cmd.id = cpu_to_le32(uid);
785bfcc09ddSBjoern A. Zeeb 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
786bfcc09ddSBjoern A. Zeeb 	time_cmd.id_and_color =
787bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
788bfcc09ddSBjoern A. Zeeb 
789bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "Removing TE 0x%x\n", le32_to_cpu(time_cmd.id));
790bfcc09ddSBjoern A. Zeeb 	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
791bfcc09ddSBjoern A. Zeeb 				   sizeof(time_cmd), &time_cmd);
792bfcc09ddSBjoern A. Zeeb 	if (ret)
793bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Couldn't remove the time event\n");
794bfcc09ddSBjoern A. Zeeb }
795bfcc09ddSBjoern A. Zeeb 
iwl_mvm_stop_session_protection(struct iwl_mvm * mvm,struct ieee80211_vif * vif)796bfcc09ddSBjoern A. Zeeb void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,
797bfcc09ddSBjoern A. Zeeb 				     struct ieee80211_vif *vif)
798bfcc09ddSBjoern A. Zeeb {
799bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
800bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
801bfcc09ddSBjoern A. Zeeb 	u32 id;
802bfcc09ddSBjoern A. Zeeb 
803bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
804bfcc09ddSBjoern A. Zeeb 
805bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
806bfcc09ddSBjoern A. Zeeb 	id = te_data->id;
807bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
808bfcc09ddSBjoern A. Zeeb 
809bfcc09ddSBjoern A. Zeeb 	if (fw_has_capa(&mvm->fw->ucode_capa,
810bfcc09ddSBjoern A. Zeeb 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
811bfcc09ddSBjoern A. Zeeb 		if (id != SESSION_PROTECT_CONF_ASSOC) {
812bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_TE(mvm,
813bfcc09ddSBjoern A. Zeeb 				     "don't remove session protection id=%u\n",
814bfcc09ddSBjoern A. Zeeb 				     id);
815bfcc09ddSBjoern A. Zeeb 			return;
816bfcc09ddSBjoern A. Zeeb 		}
817bfcc09ddSBjoern A. Zeeb 	} else if (id != TE_BSS_STA_AGGRESSIVE_ASSOC) {
818bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm,
819bfcc09ddSBjoern A. Zeeb 			     "don't remove TE with id=%u (not session protection)\n",
820bfcc09ddSBjoern A. Zeeb 			     id);
821bfcc09ddSBjoern A. Zeeb 		return;
822bfcc09ddSBjoern A. Zeeb 	}
823bfcc09ddSBjoern A. Zeeb 
824bfcc09ddSBjoern A. Zeeb 	iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
825bfcc09ddSBjoern A. Zeeb }
826bfcc09ddSBjoern A. Zeeb 
iwl_mvm_rx_session_protect_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)827bfcc09ddSBjoern A. Zeeb void iwl_mvm_rx_session_protect_notif(struct iwl_mvm *mvm,
828bfcc09ddSBjoern A. Zeeb 				      struct iwl_rx_cmd_buffer *rxb)
829bfcc09ddSBjoern A. Zeeb {
830bfcc09ddSBjoern A. Zeeb 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
831bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_session_prot_notif *notif = (void *)pkt->data;
832bfcc09ddSBjoern A. Zeeb 	struct ieee80211_vif *vif;
833bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif;
834bfcc09ddSBjoern A. Zeeb 
835bfcc09ddSBjoern A. Zeeb 	rcu_read_lock();
836bfcc09ddSBjoern A. Zeeb 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, le32_to_cpu(notif->mac_id),
837bfcc09ddSBjoern A. Zeeb 					     true);
838bfcc09ddSBjoern A. Zeeb 
839bfcc09ddSBjoern A. Zeeb 	if (!vif)
840bfcc09ddSBjoern A. Zeeb 		goto out_unlock;
841bfcc09ddSBjoern A. Zeeb 
842bfcc09ddSBjoern A. Zeeb 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
843bfcc09ddSBjoern A. Zeeb 
844bfcc09ddSBjoern A. Zeeb 	/* The vif is not a P2P_DEVICE, maintain its time_event_data */
845bfcc09ddSBjoern A. Zeeb 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
846bfcc09ddSBjoern A. Zeeb 		struct iwl_mvm_time_event_data *te_data =
847bfcc09ddSBjoern A. Zeeb 			&mvmvif->time_event_data;
848bfcc09ddSBjoern A. Zeeb 
849bfcc09ddSBjoern A. Zeeb 		if (!le32_to_cpu(notif->status)) {
850bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_check_disconnect(mvm, vif,
851bfcc09ddSBjoern A. Zeeb 						    "Session protection failure");
852bfcc09ddSBjoern A. Zeeb 			spin_lock_bh(&mvm->time_event_lock);
853bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_clear_data(mvm, te_data);
854bfcc09ddSBjoern A. Zeeb 			spin_unlock_bh(&mvm->time_event_lock);
855bfcc09ddSBjoern A. Zeeb 		}
856bfcc09ddSBjoern A. Zeeb 
857bfcc09ddSBjoern A. Zeeb 		if (le32_to_cpu(notif->start)) {
858bfcc09ddSBjoern A. Zeeb 			spin_lock_bh(&mvm->time_event_lock);
859bfcc09ddSBjoern A. Zeeb 			te_data->running = le32_to_cpu(notif->start);
860bfcc09ddSBjoern A. Zeeb 			te_data->end_jiffies =
861bfcc09ddSBjoern A. Zeeb 				TU_TO_EXP_TIME(te_data->duration);
862bfcc09ddSBjoern A. Zeeb 			spin_unlock_bh(&mvm->time_event_lock);
863bfcc09ddSBjoern A. Zeeb 		} else {
864bfcc09ddSBjoern A. Zeeb 			/*
865bfcc09ddSBjoern A. Zeeb 			 * By now, we should have finished association
866bfcc09ddSBjoern A. Zeeb 			 * and know the dtim period.
867bfcc09ddSBjoern A. Zeeb 			 */
868bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_check_disconnect(mvm, vif,
8699af1bba4SBjoern A. Zeeb 						    !vif->cfg.assoc ?
870bfcc09ddSBjoern A. Zeeb 						    "Not associated and the session protection is over already..." :
871bfcc09ddSBjoern A. Zeeb 						    "No beacon heard and the session protection is over already...");
872bfcc09ddSBjoern A. Zeeb 			spin_lock_bh(&mvm->time_event_lock);
873bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_clear_data(mvm, te_data);
874bfcc09ddSBjoern A. Zeeb 			spin_unlock_bh(&mvm->time_event_lock);
875bfcc09ddSBjoern A. Zeeb 		}
876bfcc09ddSBjoern A. Zeeb 
877bfcc09ddSBjoern A. Zeeb 		goto out_unlock;
878bfcc09ddSBjoern A. Zeeb 	}
879bfcc09ddSBjoern A. Zeeb 
880bfcc09ddSBjoern A. Zeeb 	if (!le32_to_cpu(notif->status) || !le32_to_cpu(notif->start)) {
881bfcc09ddSBjoern A. Zeeb 		/* End TE, notify mac80211 */
882bfcc09ddSBjoern A. Zeeb 		mvmvif->time_event_data.id = SESSION_PROTECT_CONF_MAX_ID;
883bfcc09ddSBjoern A. Zeeb 		ieee80211_remain_on_channel_expired(mvm->hw);
884bfcc09ddSBjoern A. Zeeb 		iwl_mvm_p2p_roc_finished(mvm);
885bfcc09ddSBjoern A. Zeeb 	} else if (le32_to_cpu(notif->start)) {
886bfcc09ddSBjoern A. Zeeb 		if (WARN_ON(mvmvif->time_event_data.id !=
887bfcc09ddSBjoern A. Zeeb 				le32_to_cpu(notif->conf_id)))
888bfcc09ddSBjoern A. Zeeb 			goto out_unlock;
889bfcc09ddSBjoern A. Zeeb 		set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
890bfcc09ddSBjoern A. Zeeb 		ieee80211_ready_on_channel(mvm->hw); /* Start TE */
891bfcc09ddSBjoern A. Zeeb 	}
892bfcc09ddSBjoern A. Zeeb 
893bfcc09ddSBjoern A. Zeeb  out_unlock:
894bfcc09ddSBjoern A. Zeeb 	rcu_read_unlock();
895bfcc09ddSBjoern A. Zeeb }
896bfcc09ddSBjoern A. Zeeb 
897bfcc09ddSBjoern A. Zeeb static int
iwl_mvm_start_p2p_roc_session_protection(struct iwl_mvm * mvm,struct ieee80211_vif * vif,int duration,enum ieee80211_roc_type type)898bfcc09ddSBjoern A. Zeeb iwl_mvm_start_p2p_roc_session_protection(struct iwl_mvm *mvm,
899bfcc09ddSBjoern A. Zeeb 					 struct ieee80211_vif *vif,
900bfcc09ddSBjoern A. Zeeb 					 int duration,
901bfcc09ddSBjoern A. Zeeb 					 enum ieee80211_roc_type type)
902bfcc09ddSBjoern A. Zeeb {
903bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
904bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_session_prot_cmd cmd = {
905bfcc09ddSBjoern A. Zeeb 		.id_and_color =
906bfcc09ddSBjoern A. Zeeb 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
907bfcc09ddSBjoern A. Zeeb 							mvmvif->color)),
908bfcc09ddSBjoern A. Zeeb 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
909bfcc09ddSBjoern A. Zeeb 		.duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
910bfcc09ddSBjoern A. Zeeb 	};
911bfcc09ddSBjoern A. Zeeb 
912bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
913bfcc09ddSBjoern A. Zeeb 
914bfcc09ddSBjoern A. Zeeb 	/* The time_event_data.id field is reused to save session
915bfcc09ddSBjoern A. Zeeb 	 * protection's configuration.
916bfcc09ddSBjoern A. Zeeb 	 */
917bfcc09ddSBjoern A. Zeeb 	switch (type) {
918bfcc09ddSBjoern A. Zeeb 	case IEEE80211_ROC_TYPE_NORMAL:
919bfcc09ddSBjoern A. Zeeb 		mvmvif->time_event_data.id =
920bfcc09ddSBjoern A. Zeeb 			SESSION_PROTECT_CONF_P2P_DEVICE_DISCOV;
921bfcc09ddSBjoern A. Zeeb 		break;
922bfcc09ddSBjoern A. Zeeb 	case IEEE80211_ROC_TYPE_MGMT_TX:
923bfcc09ddSBjoern A. Zeeb 		mvmvif->time_event_data.id =
924bfcc09ddSBjoern A. Zeeb 			SESSION_PROTECT_CONF_P2P_GO_NEGOTIATION;
925bfcc09ddSBjoern A. Zeeb 		break;
926bfcc09ddSBjoern A. Zeeb 	default:
927bfcc09ddSBjoern A. Zeeb 		WARN_ONCE(1, "Got an invalid ROC type\n");
928bfcc09ddSBjoern A. Zeeb 		return -EINVAL;
929bfcc09ddSBjoern A. Zeeb 	}
930bfcc09ddSBjoern A. Zeeb 
931bfcc09ddSBjoern A. Zeeb 	cmd.conf_id = cpu_to_le32(mvmvif->time_event_data.id);
932d9836fb4SBjoern A. Zeeb 	return iwl_mvm_send_cmd_pdu(mvm,
933d9836fb4SBjoern A. Zeeb 				    WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
934bfcc09ddSBjoern A. Zeeb 				    0, sizeof(cmd), &cmd);
935bfcc09ddSBjoern A. Zeeb }
936bfcc09ddSBjoern A. Zeeb 
iwl_mvm_start_p2p_roc(struct iwl_mvm * mvm,struct ieee80211_vif * vif,int duration,enum ieee80211_roc_type type)937bfcc09ddSBjoern A. Zeeb int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
938bfcc09ddSBjoern A. Zeeb 			  int duration, enum ieee80211_roc_type type)
939bfcc09ddSBjoern A. Zeeb {
940bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
941bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
942bfcc09ddSBjoern A. Zeeb 	struct iwl_time_event_cmd time_cmd = {};
943bfcc09ddSBjoern A. Zeeb 
944bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
945bfcc09ddSBjoern A. Zeeb 	if (te_data->running) {
946bfcc09ddSBjoern A. Zeeb 		IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");
947bfcc09ddSBjoern A. Zeeb 		return -EBUSY;
948bfcc09ddSBjoern A. Zeeb 	}
949bfcc09ddSBjoern A. Zeeb 
950bfcc09ddSBjoern A. Zeeb 	if (fw_has_capa(&mvm->fw->ucode_capa,
951bfcc09ddSBjoern A. Zeeb 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
952bfcc09ddSBjoern A. Zeeb 		return iwl_mvm_start_p2p_roc_session_protection(mvm, vif,
953bfcc09ddSBjoern A. Zeeb 								duration,
954bfcc09ddSBjoern A. Zeeb 								type);
955bfcc09ddSBjoern A. Zeeb 
956bfcc09ddSBjoern A. Zeeb 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
957bfcc09ddSBjoern A. Zeeb 	time_cmd.id_and_color =
958bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
959bfcc09ddSBjoern A. Zeeb 
960bfcc09ddSBjoern A. Zeeb 	switch (type) {
961bfcc09ddSBjoern A. Zeeb 	case IEEE80211_ROC_TYPE_NORMAL:
962bfcc09ddSBjoern A. Zeeb 		time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);
963bfcc09ddSBjoern A. Zeeb 		break;
964bfcc09ddSBjoern A. Zeeb 	case IEEE80211_ROC_TYPE_MGMT_TX:
965bfcc09ddSBjoern A. Zeeb 		time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);
966bfcc09ddSBjoern A. Zeeb 		break;
967bfcc09ddSBjoern A. Zeeb 	default:
968bfcc09ddSBjoern A. Zeeb 		WARN_ONCE(1, "Got an invalid ROC type\n");
969bfcc09ddSBjoern A. Zeeb 		return -EINVAL;
970bfcc09ddSBjoern A. Zeeb 	}
971bfcc09ddSBjoern A. Zeeb 
972bfcc09ddSBjoern A. Zeeb 	time_cmd.apply_time = cpu_to_le32(0);
973bfcc09ddSBjoern A. Zeeb 	time_cmd.interval = cpu_to_le32(1);
974bfcc09ddSBjoern A. Zeeb 
975bfcc09ddSBjoern A. Zeeb 	/*
976bfcc09ddSBjoern A. Zeeb 	 * The P2P Device TEs can have lower priority than other events
977bfcc09ddSBjoern A. Zeeb 	 * that are being scheduled by the driver/fw, and thus it might not be
978bfcc09ddSBjoern A. Zeeb 	 * scheduled. To improve the chances of it being scheduled, allow them
979bfcc09ddSBjoern A. Zeeb 	 * to be fragmented, and in addition allow them to be delayed.
980bfcc09ddSBjoern A. Zeeb 	 */
981bfcc09ddSBjoern A. Zeeb 	time_cmd.max_frags = min(MSEC_TO_TU(duration)/50, TE_V2_FRAG_ENDLESS);
982bfcc09ddSBjoern A. Zeeb 	time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));
983bfcc09ddSBjoern A. Zeeb 	time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));
984bfcc09ddSBjoern A. Zeeb 	time_cmd.repeat = 1;
985bfcc09ddSBjoern A. Zeeb 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
986bfcc09ddSBjoern A. Zeeb 				      TE_V2_NOTIF_HOST_EVENT_END |
987bfcc09ddSBjoern A. Zeeb 				      TE_V2_START_IMMEDIATELY);
988bfcc09ddSBjoern A. Zeeb 
989bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
990bfcc09ddSBjoern A. Zeeb }
991bfcc09ddSBjoern A. Zeeb 
iwl_mvm_get_roc_te(struct iwl_mvm * mvm)992bfcc09ddSBjoern A. Zeeb static struct iwl_mvm_time_event_data *iwl_mvm_get_roc_te(struct iwl_mvm *mvm)
993bfcc09ddSBjoern A. Zeeb {
994bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data;
995bfcc09ddSBjoern A. Zeeb 
996bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
997bfcc09ddSBjoern A. Zeeb 
998bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
999bfcc09ddSBjoern A. Zeeb 
1000bfcc09ddSBjoern A. Zeeb 	/*
1001bfcc09ddSBjoern A. Zeeb 	 * Iterate over the list of time events and find the time event that is
1002bfcc09ddSBjoern A. Zeeb 	 * associated with a P2P_DEVICE interface.
1003bfcc09ddSBjoern A. Zeeb 	 * This assumes that a P2P_DEVICE interface can have only a single time
1004bfcc09ddSBjoern A. Zeeb 	 * event at any given time and this time event coresponds to a ROC
1005bfcc09ddSBjoern A. Zeeb 	 * request
1006bfcc09ddSBjoern A. Zeeb 	 */
1007bfcc09ddSBjoern A. Zeeb 	list_for_each_entry(te_data, &mvm->time_event_list, list) {
1008bfcc09ddSBjoern A. Zeeb 		if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE)
1009bfcc09ddSBjoern A. Zeeb 			goto out;
1010bfcc09ddSBjoern A. Zeeb 	}
1011bfcc09ddSBjoern A. Zeeb 
1012bfcc09ddSBjoern A. Zeeb 	/* There can only be at most one AUX ROC time event, we just use the
1013bfcc09ddSBjoern A. Zeeb 	 * list to simplify/unify code. Remove it if it exists.
1014bfcc09ddSBjoern A. Zeeb 	 */
1015bfcc09ddSBjoern A. Zeeb 	te_data = list_first_entry_or_null(&mvm->aux_roc_te_list,
1016bfcc09ddSBjoern A. Zeeb 					   struct iwl_mvm_time_event_data,
1017bfcc09ddSBjoern A. Zeeb 					   list);
1018bfcc09ddSBjoern A. Zeeb out:
1019bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
1020bfcc09ddSBjoern A. Zeeb 	return te_data;
1021bfcc09ddSBjoern A. Zeeb }
1022bfcc09ddSBjoern A. Zeeb 
iwl_mvm_cleanup_roc_te(struct iwl_mvm * mvm)1023bfcc09ddSBjoern A. Zeeb void iwl_mvm_cleanup_roc_te(struct iwl_mvm *mvm)
1024bfcc09ddSBjoern A. Zeeb {
1025bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data;
1026bfcc09ddSBjoern A. Zeeb 	u32 uid;
1027bfcc09ddSBjoern A. Zeeb 
1028bfcc09ddSBjoern A. Zeeb 	te_data = iwl_mvm_get_roc_te(mvm);
1029bfcc09ddSBjoern A. Zeeb 	if (te_data)
1030bfcc09ddSBjoern A. Zeeb 		__iwl_mvm_remove_time_event(mvm, te_data, &uid);
1031bfcc09ddSBjoern A. Zeeb }
1032bfcc09ddSBjoern A. Zeeb 
iwl_mvm_stop_roc(struct iwl_mvm * mvm,struct ieee80211_vif * vif)1033bfcc09ddSBjoern A. Zeeb void iwl_mvm_stop_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1034bfcc09ddSBjoern A. Zeeb {
1035bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif;
1036bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data;
1037bfcc09ddSBjoern A. Zeeb 
1038bfcc09ddSBjoern A. Zeeb 	if (fw_has_capa(&mvm->fw->ucode_capa,
1039bfcc09ddSBjoern A. Zeeb 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
1040bfcc09ddSBjoern A. Zeeb 		mvmvif = iwl_mvm_vif_from_mac80211(vif);
1041bfcc09ddSBjoern A. Zeeb 
1042bfcc09ddSBjoern A. Zeeb 		if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1043bfcc09ddSBjoern A. Zeeb 			iwl_mvm_cancel_session_protection(mvm, mvmvif,
1044bfcc09ddSBjoern A. Zeeb 							  mvmvif->time_event_data.id);
1045bfcc09ddSBjoern A. Zeeb 			iwl_mvm_p2p_roc_finished(mvm);
1046bfcc09ddSBjoern A. Zeeb 		} else {
1047bfcc09ddSBjoern A. Zeeb 			iwl_mvm_remove_aux_roc_te(mvm, mvmvif,
1048d9836fb4SBjoern A. Zeeb 						  &mvmvif->hs_time_event_data);
1049bfcc09ddSBjoern A. Zeeb 			iwl_mvm_roc_finished(mvm);
1050bfcc09ddSBjoern A. Zeeb 		}
1051bfcc09ddSBjoern A. Zeeb 
1052bfcc09ddSBjoern A. Zeeb 		return;
1053bfcc09ddSBjoern A. Zeeb 	}
1054bfcc09ddSBjoern A. Zeeb 
1055bfcc09ddSBjoern A. Zeeb 	te_data = iwl_mvm_get_roc_te(mvm);
1056bfcc09ddSBjoern A. Zeeb 	if (!te_data) {
1057bfcc09ddSBjoern A. Zeeb 		IWL_WARN(mvm, "No remain on channel event\n");
1058bfcc09ddSBjoern A. Zeeb 		return;
1059bfcc09ddSBjoern A. Zeeb 	}
1060bfcc09ddSBjoern A. Zeeb 
1061bfcc09ddSBjoern A. Zeeb 	mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
1062bfcc09ddSBjoern A. Zeeb 
1063bfcc09ddSBjoern A. Zeeb 	if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1064bfcc09ddSBjoern A. Zeeb 		iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1065bfcc09ddSBjoern A. Zeeb 		iwl_mvm_p2p_roc_finished(mvm);
1066bfcc09ddSBjoern A. Zeeb 	} else {
1067bfcc09ddSBjoern A. Zeeb 		iwl_mvm_remove_aux_roc_te(mvm, mvmvif, te_data);
1068bfcc09ddSBjoern A. Zeeb 		iwl_mvm_roc_finished(mvm);
1069bfcc09ddSBjoern A. Zeeb 	}
1070bfcc09ddSBjoern A. Zeeb }
1071bfcc09ddSBjoern A. Zeeb 
iwl_mvm_remove_csa_period(struct iwl_mvm * mvm,struct ieee80211_vif * vif)1072bfcc09ddSBjoern A. Zeeb void iwl_mvm_remove_csa_period(struct iwl_mvm *mvm,
1073bfcc09ddSBjoern A. Zeeb 			       struct ieee80211_vif *vif)
1074bfcc09ddSBjoern A. Zeeb {
1075bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1076bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1077bfcc09ddSBjoern A. Zeeb 	u32 id;
1078bfcc09ddSBjoern A. Zeeb 
1079bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
1080bfcc09ddSBjoern A. Zeeb 
1081bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
1082bfcc09ddSBjoern A. Zeeb 	id = te_data->id;
1083bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
1084bfcc09ddSBjoern A. Zeeb 
1085bfcc09ddSBjoern A. Zeeb 	if (id != TE_CHANNEL_SWITCH_PERIOD)
1086bfcc09ddSBjoern A. Zeeb 		return;
1087bfcc09ddSBjoern A. Zeeb 
1088bfcc09ddSBjoern A. Zeeb 	iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1089bfcc09ddSBjoern A. Zeeb }
1090bfcc09ddSBjoern A. Zeeb 
iwl_mvm_schedule_csa_period(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 duration,u32 apply_time)1091bfcc09ddSBjoern A. Zeeb int iwl_mvm_schedule_csa_period(struct iwl_mvm *mvm,
1092bfcc09ddSBjoern A. Zeeb 				struct ieee80211_vif *vif,
1093bfcc09ddSBjoern A. Zeeb 				u32 duration, u32 apply_time)
1094bfcc09ddSBjoern A. Zeeb {
1095bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1096bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1097bfcc09ddSBjoern A. Zeeb 	struct iwl_time_event_cmd time_cmd = {};
1098bfcc09ddSBjoern A. Zeeb 
1099bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
1100bfcc09ddSBjoern A. Zeeb 
1101bfcc09ddSBjoern A. Zeeb 	if (te_data->running) {
1102bfcc09ddSBjoern A. Zeeb 		u32 id;
1103bfcc09ddSBjoern A. Zeeb 
1104bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&mvm->time_event_lock);
1105bfcc09ddSBjoern A. Zeeb 		id = te_data->id;
1106bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&mvm->time_event_lock);
1107bfcc09ddSBjoern A. Zeeb 
1108bfcc09ddSBjoern A. Zeeb 		if (id == TE_CHANNEL_SWITCH_PERIOD) {
1109bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_TE(mvm, "CS period is already scheduled\n");
1110bfcc09ddSBjoern A. Zeeb 			return -EBUSY;
1111bfcc09ddSBjoern A. Zeeb 		}
1112bfcc09ddSBjoern A. Zeeb 
1113bfcc09ddSBjoern A. Zeeb 		/*
1114bfcc09ddSBjoern A. Zeeb 		 * Remove the session protection time event to allow the
1115bfcc09ddSBjoern A. Zeeb 		 * channel switch. If we got here, we just heard a beacon so
1116bfcc09ddSBjoern A. Zeeb 		 * the session protection is not needed anymore anyway.
1117bfcc09ddSBjoern A. Zeeb 		 */
1118bfcc09ddSBjoern A. Zeeb 		iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1119bfcc09ddSBjoern A. Zeeb 	}
1120bfcc09ddSBjoern A. Zeeb 
1121bfcc09ddSBjoern A. Zeeb 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
1122bfcc09ddSBjoern A. Zeeb 	time_cmd.id_and_color =
1123bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
1124bfcc09ddSBjoern A. Zeeb 	time_cmd.id = cpu_to_le32(TE_CHANNEL_SWITCH_PERIOD);
1125bfcc09ddSBjoern A. Zeeb 	time_cmd.apply_time = cpu_to_le32(apply_time);
1126bfcc09ddSBjoern A. Zeeb 	time_cmd.max_frags = TE_V2_FRAG_NONE;
1127bfcc09ddSBjoern A. Zeeb 	time_cmd.duration = cpu_to_le32(duration);
1128bfcc09ddSBjoern A. Zeeb 	time_cmd.repeat = 1;
1129bfcc09ddSBjoern A. Zeeb 	time_cmd.interval = cpu_to_le32(1);
1130bfcc09ddSBjoern A. Zeeb 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
1131bfcc09ddSBjoern A. Zeeb 				      TE_V2_ABSENCE);
1132bfcc09ddSBjoern A. Zeeb 	if (!apply_time)
1133bfcc09ddSBjoern A. Zeeb 		time_cmd.policy |= cpu_to_le16(TE_V2_START_IMMEDIATELY);
1134bfcc09ddSBjoern A. Zeeb 
1135bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
1136bfcc09ddSBjoern A. Zeeb }
1137bfcc09ddSBjoern A. Zeeb 
iwl_mvm_session_prot_notif(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)1138bfcc09ddSBjoern A. Zeeb static bool iwl_mvm_session_prot_notif(struct iwl_notif_wait_data *notif_wait,
1139bfcc09ddSBjoern A. Zeeb 				       struct iwl_rx_packet *pkt, void *data)
1140bfcc09ddSBjoern A. Zeeb {
1141bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm *mvm =
1142bfcc09ddSBjoern A. Zeeb 		container_of(notif_wait, struct iwl_mvm, notif_wait);
1143bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_session_prot_notif *resp;
1144bfcc09ddSBjoern A. Zeeb 	int resp_len = iwl_rx_packet_payload_len(pkt);
1145bfcc09ddSBjoern A. Zeeb 
1146bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(pkt->hdr.cmd != SESSION_PROTECTION_NOTIF ||
1147bfcc09ddSBjoern A. Zeeb 		    pkt->hdr.group_id != MAC_CONF_GROUP))
1148bfcc09ddSBjoern A. Zeeb 		return true;
1149bfcc09ddSBjoern A. Zeeb 
1150bfcc09ddSBjoern A. Zeeb 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
1151bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Invalid SESSION_PROTECTION_NOTIF response\n");
1152bfcc09ddSBjoern A. Zeeb 		return true;
1153bfcc09ddSBjoern A. Zeeb 	}
1154bfcc09ddSBjoern A. Zeeb 
1155bfcc09ddSBjoern A. Zeeb 	resp = (void *)pkt->data;
1156bfcc09ddSBjoern A. Zeeb 
1157bfcc09ddSBjoern A. Zeeb 	if (!resp->status)
1158bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm,
1159bfcc09ddSBjoern A. Zeeb 			"TIME_EVENT_NOTIFICATION received but not executed\n");
1160bfcc09ddSBjoern A. Zeeb 
1161bfcc09ddSBjoern A. Zeeb 	return true;
1162bfcc09ddSBjoern A. Zeeb }
1163bfcc09ddSBjoern A. Zeeb 
iwl_mvm_schedule_session_protection(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 duration,u32 min_duration,bool wait_for_notif)1164bfcc09ddSBjoern A. Zeeb void iwl_mvm_schedule_session_protection(struct iwl_mvm *mvm,
1165bfcc09ddSBjoern A. Zeeb 					 struct ieee80211_vif *vif,
1166bfcc09ddSBjoern A. Zeeb 					 u32 duration, u32 min_duration,
1167bfcc09ddSBjoern A. Zeeb 					 bool wait_for_notif)
1168bfcc09ddSBjoern A. Zeeb {
1169bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1170bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1171d9836fb4SBjoern A. Zeeb 	const u16 notif[] = { WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_NOTIF) };
1172bfcc09ddSBjoern A. Zeeb 	struct iwl_notification_wait wait_notif;
1173bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_session_prot_cmd cmd = {
1174bfcc09ddSBjoern A. Zeeb 		.id_and_color =
1175bfcc09ddSBjoern A. Zeeb 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1176bfcc09ddSBjoern A. Zeeb 							mvmvif->color)),
1177bfcc09ddSBjoern A. Zeeb 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
11788f06a2b5SBjoern A. Zeeb 		.conf_id = cpu_to_le32(SESSION_PROTECT_CONF_ASSOC),
1179bfcc09ddSBjoern A. Zeeb 		.duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
1180bfcc09ddSBjoern A. Zeeb 	};
1181bfcc09ddSBjoern A. Zeeb 
1182bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
1183bfcc09ddSBjoern A. Zeeb 
1184bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
1185bfcc09ddSBjoern A. Zeeb 	if (te_data->running &&
1186bfcc09ddSBjoern A. Zeeb 	    time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
1187bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
1188bfcc09ddSBjoern A. Zeeb 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
1189bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&mvm->time_event_lock);
1190bfcc09ddSBjoern A. Zeeb 
1191bfcc09ddSBjoern A. Zeeb 		return;
1192bfcc09ddSBjoern A. Zeeb 	}
1193bfcc09ddSBjoern A. Zeeb 
1194bfcc09ddSBjoern A. Zeeb 	iwl_mvm_te_clear_data(mvm, te_data);
11958f06a2b5SBjoern A. Zeeb 	/*
11968f06a2b5SBjoern A. Zeeb 	 * The time_event_data.id field is reused to save session
11978f06a2b5SBjoern A. Zeeb 	 * protection's configuration.
11988f06a2b5SBjoern A. Zeeb 	 */
11998f06a2b5SBjoern A. Zeeb 	te_data->id = le32_to_cpu(cmd.conf_id);
1200bfcc09ddSBjoern A. Zeeb 	te_data->duration = le32_to_cpu(cmd.duration_tu);
1201bfcc09ddSBjoern A. Zeeb 	te_data->vif = vif;
1202bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
1203bfcc09ddSBjoern A. Zeeb 
1204bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "Add new session protection, duration %d TU\n",
1205bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(cmd.duration_tu));
1206bfcc09ddSBjoern A. Zeeb 
1207bfcc09ddSBjoern A. Zeeb 	if (!wait_for_notif) {
1208bfcc09ddSBjoern A. Zeeb 		if (iwl_mvm_send_cmd_pdu(mvm,
1209d9836fb4SBjoern A. Zeeb 					 WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1210bfcc09ddSBjoern A. Zeeb 					 0, sizeof(cmd), &cmd)) {
1211bfcc09ddSBjoern A. Zeeb 			IWL_ERR(mvm,
1212bfcc09ddSBjoern A. Zeeb 				"Couldn't send the SESSION_PROTECTION_CMD\n");
1213bfcc09ddSBjoern A. Zeeb 			spin_lock_bh(&mvm->time_event_lock);
1214bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_clear_data(mvm, te_data);
1215bfcc09ddSBjoern A. Zeeb 			spin_unlock_bh(&mvm->time_event_lock);
1216bfcc09ddSBjoern A. Zeeb 		}
1217bfcc09ddSBjoern A. Zeeb 
1218bfcc09ddSBjoern A. Zeeb 		return;
1219bfcc09ddSBjoern A. Zeeb 	}
1220bfcc09ddSBjoern A. Zeeb 
1221bfcc09ddSBjoern A. Zeeb 	iwl_init_notification_wait(&mvm->notif_wait, &wait_notif,
1222bfcc09ddSBjoern A. Zeeb 				   notif, ARRAY_SIZE(notif),
1223bfcc09ddSBjoern A. Zeeb 				   iwl_mvm_session_prot_notif, NULL);
1224bfcc09ddSBjoern A. Zeeb 
1225bfcc09ddSBjoern A. Zeeb 	if (iwl_mvm_send_cmd_pdu(mvm,
1226d9836fb4SBjoern A. Zeeb 				 WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1227bfcc09ddSBjoern A. Zeeb 				 0, sizeof(cmd), &cmd)) {
1228bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm,
1229bfcc09ddSBjoern A. Zeeb 			"Couldn't send the SESSION_PROTECTION_CMD\n");
1230bfcc09ddSBjoern A. Zeeb 		iwl_remove_notification(&mvm->notif_wait, &wait_notif);
1231bfcc09ddSBjoern A. Zeeb 	} else if (iwl_wait_notification(&mvm->notif_wait, &wait_notif,
1232bfcc09ddSBjoern A. Zeeb 					 TU_TO_JIFFIES(100))) {
1233bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm,
1234bfcc09ddSBjoern A. Zeeb 			"Failed to protect session until session protection\n");
1235bfcc09ddSBjoern A. Zeeb 	}
1236bfcc09ddSBjoern A. Zeeb }
1237