xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/time-event.c (revision a4128aad8503277614f2d214011ef60a19447b83)
1bfcc09ddSBjoern A. Zeeb // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2bfcc09ddSBjoern A. Zeeb /*
3*a4128aadSBjoern A. Zeeb  * Copyright (C) 2012-2014, 2018-2024 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 
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;
45*a4128aadSBjoern A. Zeeb 	te_data->link_id = -1;
46bfcc09ddSBjoern A. Zeeb }
47bfcc09ddSBjoern A. Zeeb 
48*a4128aadSBjoern A. Zeeb static void iwl_mvm_cleanup_roc(struct iwl_mvm *mvm)
49bfcc09ddSBjoern A. Zeeb {
50*a4128aadSBjoern A. Zeeb 	struct ieee80211_vif *bss_vif = iwl_mvm_get_bss_vif(mvm);
51*a4128aadSBjoern A. Zeeb 	struct ieee80211_vif *vif = mvm->p2p_device_vif;
52*a4128aadSBjoern A. Zeeb 
53*a4128aadSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
54bfcc09ddSBjoern A. Zeeb 
55bfcc09ddSBjoern A. Zeeb 	/*
56*a4128aadSBjoern A. Zeeb 	 * Clear the ROC_P2P_RUNNING status bit.
57bfcc09ddSBjoern A. Zeeb 	 * This will cause the TX path to drop offchannel transmissions.
58bfcc09ddSBjoern A. Zeeb 	 * That would also be done by mac80211, but it is racy, in particular
59*a4128aadSBjoern A. Zeeb 	 * in the case that the time event actually completed in the firmware.
60*a4128aadSBjoern A. Zeeb 	 *
61*a4128aadSBjoern A. Zeeb 	 * Also flush the offchannel queue -- this is called when the time
62bfcc09ddSBjoern A. Zeeb 	 * event finishes or is canceled, so that frames queued for it
63bfcc09ddSBjoern A. Zeeb 	 * won't get stuck on the queue and be transmitted in the next
64bfcc09ddSBjoern A. Zeeb 	 * time event.
65bfcc09ddSBjoern A. Zeeb 	 */
66*a4128aadSBjoern A. Zeeb 	if (test_and_clear_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status)) {
67bfcc09ddSBjoern A. Zeeb 		struct iwl_mvm_vif *mvmvif;
68bfcc09ddSBjoern A. Zeeb 
69*a4128aadSBjoern A. Zeeb 		synchronize_net();
70*a4128aadSBjoern A. Zeeb 
71bfcc09ddSBjoern A. Zeeb 		/*
72bfcc09ddSBjoern A. Zeeb 		 * NB: access to this pointer would be racy, but the flush bit
73bfcc09ddSBjoern A. Zeeb 		 * can only be set when we had a P2P-Device VIF, and we have a
74bfcc09ddSBjoern A. Zeeb 		 * flush of this work in iwl_mvm_prepare_mac_removal() so it's
75bfcc09ddSBjoern A. Zeeb 		 * not really racy.
76bfcc09ddSBjoern A. Zeeb 		 */
77bfcc09ddSBjoern A. Zeeb 
78*a4128aadSBjoern A. Zeeb 		if (!WARN_ON(!vif)) {
79*a4128aadSBjoern A. Zeeb 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
80*a4128aadSBjoern A. Zeeb 			iwl_mvm_flush_sta(mvm, mvmvif->deflink.bcast_sta.sta_id,
81*a4128aadSBjoern A. Zeeb 					  mvmvif->deflink.bcast_sta.tfd_queue_msk);
82*a4128aadSBjoern A. Zeeb 
83*a4128aadSBjoern A. Zeeb 			if (mvm->mld_api_is_used) {
84*a4128aadSBjoern A. Zeeb 				iwl_mvm_mld_rm_bcast_sta(mvm, vif,
85*a4128aadSBjoern A. Zeeb 							 &vif->bss_conf);
86*a4128aadSBjoern A. Zeeb 
87*a4128aadSBjoern A. Zeeb 				iwl_mvm_link_changed(mvm, vif, &vif->bss_conf,
88*a4128aadSBjoern A. Zeeb 						     LINK_CONTEXT_MODIFY_ACTIVE,
89*a4128aadSBjoern A. Zeeb 						     false);
90*a4128aadSBjoern A. Zeeb 			} else {
91*a4128aadSBjoern A. Zeeb 				iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
92*a4128aadSBjoern A. Zeeb 				iwl_mvm_binding_remove_vif(mvm, vif);
93*a4128aadSBjoern A. Zeeb 			}
94*a4128aadSBjoern A. Zeeb 
95*a4128aadSBjoern A. Zeeb 			/* Do not remove the PHY context as removing and adding
96*a4128aadSBjoern A. Zeeb 			 * a PHY context has timing overheads. Leaving it
97*a4128aadSBjoern A. Zeeb 			 * configured in FW would be useful in case the next ROC
98*a4128aadSBjoern A. Zeeb 			 * is with the same channel.
99*a4128aadSBjoern A. Zeeb 			 */
100bfcc09ddSBjoern A. Zeeb 		}
101d9836fb4SBjoern A. Zeeb 	}
102d9836fb4SBjoern A. Zeeb 
103d9836fb4SBjoern A. Zeeb 	/*
104*a4128aadSBjoern A. Zeeb 	 * P2P AUX ROC and HS2.0 ROC do not run simultaneously.
105d9836fb4SBjoern A. Zeeb 	 * Clear the ROC_AUX_RUNNING status bit.
106d9836fb4SBjoern A. Zeeb 	 * This will cause the TX path to drop offchannel transmissions.
107d9836fb4SBjoern A. Zeeb 	 * That would also be done by mac80211, but it is racy, in particular
108d9836fb4SBjoern A. Zeeb 	 * in the case that the time event actually completed in the firmware
109d9836fb4SBjoern A. Zeeb 	 * (which is handled in iwl_mvm_te_handle_notif).
110d9836fb4SBjoern A. Zeeb 	 */
111d9836fb4SBjoern A. Zeeb 	if (test_and_clear_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status)) {
112*a4128aadSBjoern A. Zeeb 		synchronize_net();
113*a4128aadSBjoern A. Zeeb 
114*a4128aadSBjoern A. Zeeb 		iwl_mvm_flush_sta(mvm, mvm->aux_sta.sta_id,
115*a4128aadSBjoern A. Zeeb 				  mvm->aux_sta.tfd_queue_msk);
116d9836fb4SBjoern A. Zeeb 
1179af1bba4SBjoern A. Zeeb 		if (mvm->mld_api_is_used) {
1189af1bba4SBjoern A. Zeeb 			iwl_mvm_mld_rm_aux_sta(mvm);
119*a4128aadSBjoern A. Zeeb 			mutex_unlock(&mvm->mutex);
120*a4128aadSBjoern A. Zeeb 			return;
1219af1bba4SBjoern A. Zeeb 		}
1229af1bba4SBjoern A. Zeeb 
123bfcc09ddSBjoern A. Zeeb 		/* In newer version of this command an aux station is added only
124bfcc09ddSBjoern A. Zeeb 		 * in cases of dedicated tx queue and need to be removed in end
125bfcc09ddSBjoern A. Zeeb 		 * of use */
1269af1bba4SBjoern A. Zeeb 		if (iwl_mvm_has_new_station_api(mvm->fw))
127bfcc09ddSBjoern A. Zeeb 			iwl_mvm_rm_aux_sta(mvm);
128bfcc09ddSBjoern A. Zeeb 	}
129bfcc09ddSBjoern A. Zeeb 
130*a4128aadSBjoern A. Zeeb 	if (!IS_ERR_OR_NULL(bss_vif))
131*a4128aadSBjoern A. Zeeb 		iwl_mvm_unblock_esr(mvm, bss_vif, IWL_MVM_ESR_BLOCKED_ROC);
132bfcc09ddSBjoern A. Zeeb 	mutex_unlock(&mvm->mutex);
133bfcc09ddSBjoern A. Zeeb }
134bfcc09ddSBjoern A. Zeeb 
135*a4128aadSBjoern A. Zeeb void iwl_mvm_roc_done_wk(struct work_struct *wk)
136*a4128aadSBjoern A. Zeeb {
137*a4128aadSBjoern A. Zeeb 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, roc_done_wk);
138*a4128aadSBjoern A. Zeeb 
139*a4128aadSBjoern A. Zeeb 	mutex_lock(&mvm->mutex);
140*a4128aadSBjoern A. Zeeb 	/* Mutex is released inside */
141*a4128aadSBjoern A. Zeeb 	iwl_mvm_cleanup_roc(mvm);
142*a4128aadSBjoern A. Zeeb }
143*a4128aadSBjoern A. Zeeb 
144bfcc09ddSBjoern A. Zeeb static void iwl_mvm_roc_finished(struct iwl_mvm *mvm)
145bfcc09ddSBjoern A. Zeeb {
146bfcc09ddSBjoern A. Zeeb 	/*
147bfcc09ddSBjoern A. Zeeb 	 * Of course, our status bit is just as racy as mac80211, so in
148bfcc09ddSBjoern A. Zeeb 	 * addition, fire off the work struct which will drop all frames
149bfcc09ddSBjoern A. Zeeb 	 * from the hardware queues that made it through the race. First
150bfcc09ddSBjoern A. Zeeb 	 * it will of course synchronize the TX path to make sure that
151bfcc09ddSBjoern A. Zeeb 	 * any *new* TX will be rejected.
152bfcc09ddSBjoern A. Zeeb 	 */
153bfcc09ddSBjoern A. Zeeb 	schedule_work(&mvm->roc_done_wk);
154bfcc09ddSBjoern A. Zeeb }
155bfcc09ddSBjoern A. Zeeb 
156bfcc09ddSBjoern A. Zeeb static void iwl_mvm_csa_noa_start(struct iwl_mvm *mvm)
157bfcc09ddSBjoern A. Zeeb {
158bfcc09ddSBjoern A. Zeeb 	struct ieee80211_vif *csa_vif;
159bfcc09ddSBjoern A. Zeeb 
160bfcc09ddSBjoern A. Zeeb 	rcu_read_lock();
161bfcc09ddSBjoern A. Zeeb 
162bfcc09ddSBjoern A. Zeeb 	csa_vif = rcu_dereference(mvm->csa_vif);
1639af1bba4SBjoern A. Zeeb 	if (!csa_vif || !csa_vif->bss_conf.csa_active)
164bfcc09ddSBjoern A. Zeeb 		goto out_unlock;
165bfcc09ddSBjoern A. Zeeb 
166bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "CSA NOA started\n");
167bfcc09ddSBjoern A. Zeeb 
168bfcc09ddSBjoern A. Zeeb 	/*
169bfcc09ddSBjoern A. Zeeb 	 * CSA NoA is started but we still have beacons to
170bfcc09ddSBjoern A. Zeeb 	 * transmit on the current channel.
171bfcc09ddSBjoern A. Zeeb 	 * So we just do nothing here and the switch
172bfcc09ddSBjoern A. Zeeb 	 * will be performed on the last TBTT.
173bfcc09ddSBjoern A. Zeeb 	 */
174*a4128aadSBjoern A. Zeeb 	if (!ieee80211_beacon_cntdwn_is_complete(csa_vif, 0)) {
175bfcc09ddSBjoern A. Zeeb 		IWL_WARN(mvm, "CSA NOA started too early\n");
176bfcc09ddSBjoern A. Zeeb 		goto out_unlock;
177bfcc09ddSBjoern A. Zeeb 	}
178bfcc09ddSBjoern A. Zeeb 
179*a4128aadSBjoern A. Zeeb 	ieee80211_csa_finish(csa_vif, 0);
180bfcc09ddSBjoern A. Zeeb 
181bfcc09ddSBjoern A. Zeeb 	rcu_read_unlock();
182bfcc09ddSBjoern A. Zeeb 
183bfcc09ddSBjoern A. Zeeb 	RCU_INIT_POINTER(mvm->csa_vif, NULL);
184bfcc09ddSBjoern A. Zeeb 
185bfcc09ddSBjoern A. Zeeb 	return;
186bfcc09ddSBjoern A. Zeeb 
187bfcc09ddSBjoern A. Zeeb out_unlock:
188bfcc09ddSBjoern A. Zeeb 	rcu_read_unlock();
189bfcc09ddSBjoern A. Zeeb }
190bfcc09ddSBjoern A. Zeeb 
191bfcc09ddSBjoern A. Zeeb static bool iwl_mvm_te_check_disconnect(struct iwl_mvm *mvm,
192bfcc09ddSBjoern A. Zeeb 					struct ieee80211_vif *vif,
193bfcc09ddSBjoern A. Zeeb 					const char *errmsg)
194bfcc09ddSBjoern A. Zeeb {
195bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
196bfcc09ddSBjoern A. Zeeb 
197bfcc09ddSBjoern A. Zeeb 	if (vif->type != NL80211_IFTYPE_STATION)
198bfcc09ddSBjoern A. Zeeb 		return false;
199bfcc09ddSBjoern A. Zeeb 
2009af1bba4SBjoern A. Zeeb 	if (!mvmvif->csa_bcn_pending && vif->cfg.assoc &&
201bfcc09ddSBjoern A. Zeeb 	    vif->bss_conf.dtim_period)
202bfcc09ddSBjoern A. Zeeb 		return false;
203bfcc09ddSBjoern A. Zeeb 	if (errmsg)
204bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "%s\n", errmsg);
205bfcc09ddSBjoern A. Zeeb 
206bfcc09ddSBjoern A. Zeeb 	if (mvmvif->csa_bcn_pending) {
207bfcc09ddSBjoern A. Zeeb 		struct iwl_mvm_sta *mvmsta;
208bfcc09ddSBjoern A. Zeeb 
209bfcc09ddSBjoern A. Zeeb 		rcu_read_lock();
2109af1bba4SBjoern A. Zeeb 		mvmsta = iwl_mvm_sta_from_staid_rcu(mvm,
2119af1bba4SBjoern A. Zeeb 						    mvmvif->deflink.ap_sta_id);
212bfcc09ddSBjoern A. Zeeb 		if (!WARN_ON(!mvmsta))
213bfcc09ddSBjoern A. Zeeb 			iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
214bfcc09ddSBjoern A. Zeeb 		rcu_read_unlock();
215bfcc09ddSBjoern A. Zeeb 	}
216bfcc09ddSBjoern A. Zeeb 
2179af1bba4SBjoern A. Zeeb 	if (vif->cfg.assoc) {
218bfcc09ddSBjoern A. Zeeb 		/*
219bfcc09ddSBjoern A. Zeeb 		 * When not associated, this will be called from
220bfcc09ddSBjoern A. Zeeb 		 * iwl_mvm_event_mlme_callback_ini()
221bfcc09ddSBjoern A. Zeeb 		 */
222bfcc09ddSBjoern A. Zeeb 		iwl_dbg_tlv_time_point(&mvm->fwrt,
223bfcc09ddSBjoern A. Zeeb 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
224bfcc09ddSBjoern A. Zeeb 				       NULL);
225*a4128aadSBjoern A. Zeeb 
226*a4128aadSBjoern A. Zeeb 		mvmvif->session_prot_connection_loss = true;
227bfcc09ddSBjoern A. Zeeb 	}
228bfcc09ddSBjoern A. Zeeb 
229bfcc09ddSBjoern A. Zeeb 	iwl_mvm_connection_loss(mvm, vif, errmsg);
230bfcc09ddSBjoern A. Zeeb 	return true;
231bfcc09ddSBjoern A. Zeeb }
232bfcc09ddSBjoern A. Zeeb 
233bfcc09ddSBjoern A. Zeeb static void
234bfcc09ddSBjoern A. Zeeb iwl_mvm_te_handle_notify_csa(struct iwl_mvm *mvm,
235bfcc09ddSBjoern A. Zeeb 			     struct iwl_mvm_time_event_data *te_data,
236bfcc09ddSBjoern A. Zeeb 			     struct iwl_time_event_notif *notif)
237bfcc09ddSBjoern A. Zeeb {
238bfcc09ddSBjoern A. Zeeb 	struct ieee80211_vif *vif = te_data->vif;
239bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
240bfcc09ddSBjoern A. Zeeb 
241bfcc09ddSBjoern A. Zeeb 	if (!notif->status)
242bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm, "CSA time event failed to start\n");
243bfcc09ddSBjoern A. Zeeb 
244bfcc09ddSBjoern A. Zeeb 	switch (te_data->vif->type) {
245bfcc09ddSBjoern A. Zeeb 	case NL80211_IFTYPE_AP:
246bfcc09ddSBjoern A. Zeeb 		if (!notif->status)
247bfcc09ddSBjoern A. Zeeb 			mvmvif->csa_failed = true;
248bfcc09ddSBjoern A. Zeeb 		iwl_mvm_csa_noa_start(mvm);
249bfcc09ddSBjoern A. Zeeb 		break;
250bfcc09ddSBjoern A. Zeeb 	case NL80211_IFTYPE_STATION:
251bfcc09ddSBjoern A. Zeeb 		if (!notif->status) {
252bfcc09ddSBjoern A. Zeeb 			iwl_mvm_connection_loss(mvm, vif,
253bfcc09ddSBjoern A. Zeeb 						"CSA TE failed to start");
254bfcc09ddSBjoern A. Zeeb 			break;
255bfcc09ddSBjoern A. Zeeb 		}
256bfcc09ddSBjoern A. Zeeb 		iwl_mvm_csa_client_absent(mvm, te_data->vif);
257bfcc09ddSBjoern A. Zeeb 		cancel_delayed_work(&mvmvif->csa_work);
258*a4128aadSBjoern A. Zeeb 		ieee80211_chswitch_done(te_data->vif, true, 0);
259bfcc09ddSBjoern A. Zeeb 		break;
260bfcc09ddSBjoern A. Zeeb 	default:
261bfcc09ddSBjoern A. Zeeb 		/* should never happen */
262bfcc09ddSBjoern A. Zeeb 		WARN_ON_ONCE(1);
263bfcc09ddSBjoern A. Zeeb 		break;
264bfcc09ddSBjoern A. Zeeb 	}
265bfcc09ddSBjoern A. Zeeb 
266bfcc09ddSBjoern A. Zeeb 	/* we don't need it anymore */
267bfcc09ddSBjoern A. Zeeb 	iwl_mvm_te_clear_data(mvm, te_data);
268bfcc09ddSBjoern A. Zeeb }
269bfcc09ddSBjoern A. Zeeb 
270bfcc09ddSBjoern A. Zeeb static void iwl_mvm_te_check_trigger(struct iwl_mvm *mvm,
271bfcc09ddSBjoern A. Zeeb 				     struct iwl_time_event_notif *notif,
272bfcc09ddSBjoern A. Zeeb 				     struct iwl_mvm_time_event_data *te_data)
273bfcc09ddSBjoern A. Zeeb {
274bfcc09ddSBjoern A. Zeeb 	struct iwl_fw_dbg_trigger_tlv *trig;
275bfcc09ddSBjoern A. Zeeb 	struct iwl_fw_dbg_trigger_time_event *te_trig;
276bfcc09ddSBjoern A. Zeeb 	int i;
277bfcc09ddSBjoern A. Zeeb 
278bfcc09ddSBjoern A. Zeeb 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
279bfcc09ddSBjoern A. Zeeb 				     ieee80211_vif_to_wdev(te_data->vif),
280bfcc09ddSBjoern A. Zeeb 				     FW_DBG_TRIGGER_TIME_EVENT);
281bfcc09ddSBjoern A. Zeeb 	if (!trig)
282bfcc09ddSBjoern A. Zeeb 		return;
283bfcc09ddSBjoern A. Zeeb 
284bfcc09ddSBjoern A. Zeeb 	te_trig = (void *)trig->data;
285bfcc09ddSBjoern A. Zeeb 
286bfcc09ddSBjoern A. Zeeb 	for (i = 0; i < ARRAY_SIZE(te_trig->time_events); i++) {
287bfcc09ddSBjoern A. Zeeb 		u32 trig_te_id = le32_to_cpu(te_trig->time_events[i].id);
288bfcc09ddSBjoern A. Zeeb 		u32 trig_action_bitmap =
289bfcc09ddSBjoern A. Zeeb 			le32_to_cpu(te_trig->time_events[i].action_bitmap);
290bfcc09ddSBjoern A. Zeeb 		u32 trig_status_bitmap =
291bfcc09ddSBjoern A. Zeeb 			le32_to_cpu(te_trig->time_events[i].status_bitmap);
292bfcc09ddSBjoern A. Zeeb 
293bfcc09ddSBjoern A. Zeeb 		if (trig_te_id != te_data->id ||
294bfcc09ddSBjoern A. Zeeb 		    !(trig_action_bitmap & le32_to_cpu(notif->action)) ||
295bfcc09ddSBjoern A. Zeeb 		    !(trig_status_bitmap & BIT(le32_to_cpu(notif->status))))
296bfcc09ddSBjoern A. Zeeb 			continue;
297bfcc09ddSBjoern A. Zeeb 
298bfcc09ddSBjoern A. Zeeb 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
299bfcc09ddSBjoern A. Zeeb 					"Time event %d Action 0x%x received status: %d",
300bfcc09ddSBjoern A. Zeeb 					te_data->id,
301bfcc09ddSBjoern A. Zeeb 					le32_to_cpu(notif->action),
302bfcc09ddSBjoern A. Zeeb 					le32_to_cpu(notif->status));
303bfcc09ddSBjoern A. Zeeb 		break;
304bfcc09ddSBjoern A. Zeeb 	}
305bfcc09ddSBjoern A. Zeeb }
306bfcc09ddSBjoern A. Zeeb 
307bfcc09ddSBjoern A. Zeeb /*
308bfcc09ddSBjoern A. Zeeb  * Handles a FW notification for an event that is known to the driver.
309bfcc09ddSBjoern A. Zeeb  *
310bfcc09ddSBjoern A. Zeeb  * @mvm: the mvm component
311bfcc09ddSBjoern A. Zeeb  * @te_data: the time event data
312bfcc09ddSBjoern A. Zeeb  * @notif: the notification data corresponding the time event data.
313bfcc09ddSBjoern A. Zeeb  */
314bfcc09ddSBjoern A. Zeeb static void iwl_mvm_te_handle_notif(struct iwl_mvm *mvm,
315bfcc09ddSBjoern A. Zeeb 				    struct iwl_mvm_time_event_data *te_data,
316bfcc09ddSBjoern A. Zeeb 				    struct iwl_time_event_notif *notif)
317bfcc09ddSBjoern A. Zeeb {
318bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->time_event_lock);
319bfcc09ddSBjoern A. Zeeb 
320bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "Handle time event notif - UID = 0x%x action %d\n",
321bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(notif->unique_id),
322bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(notif->action));
323bfcc09ddSBjoern A. Zeeb 
324bfcc09ddSBjoern A. Zeeb 	iwl_mvm_te_check_trigger(mvm, notif, te_data);
325bfcc09ddSBjoern A. Zeeb 
326bfcc09ddSBjoern A. Zeeb 	/*
327bfcc09ddSBjoern A. Zeeb 	 * The FW sends the start/end time event notifications even for events
328bfcc09ddSBjoern A. Zeeb 	 * that it fails to schedule. This is indicated in the status field of
329bfcc09ddSBjoern A. Zeeb 	 * the notification. This happens in cases that the scheduler cannot
330bfcc09ddSBjoern A. Zeeb 	 * find a schedule that can handle the event (for example requesting a
331bfcc09ddSBjoern A. Zeeb 	 * P2P Device discoveribility, while there are other higher priority
332bfcc09ddSBjoern A. Zeeb 	 * events in the system).
333bfcc09ddSBjoern A. Zeeb 	 */
334bfcc09ddSBjoern A. Zeeb 	if (!le32_to_cpu(notif->status)) {
335bfcc09ddSBjoern A. Zeeb 		const char *msg;
336bfcc09ddSBjoern A. Zeeb 
337bfcc09ddSBjoern A. Zeeb 		if (notif->action & cpu_to_le32(TE_V2_NOTIF_HOST_EVENT_START))
338bfcc09ddSBjoern A. Zeeb 			msg = "Time Event start notification failure";
339bfcc09ddSBjoern A. Zeeb 		else
340bfcc09ddSBjoern A. Zeeb 			msg = "Time Event end notification failure";
341bfcc09ddSBjoern A. Zeeb 
342bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm, "%s\n", msg);
343bfcc09ddSBjoern A. Zeeb 
344bfcc09ddSBjoern A. Zeeb 		if (iwl_mvm_te_check_disconnect(mvm, te_data->vif, msg)) {
345bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_clear_data(mvm, te_data);
346bfcc09ddSBjoern A. Zeeb 			return;
347bfcc09ddSBjoern A. Zeeb 		}
348bfcc09ddSBjoern A. Zeeb 	}
349bfcc09ddSBjoern A. Zeeb 
350bfcc09ddSBjoern A. Zeeb 	if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_END) {
351bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm,
352bfcc09ddSBjoern A. Zeeb 			     "TE ended - current time %lu, estimated end %lu\n",
353bfcc09ddSBjoern A. Zeeb 			     jiffies, te_data->end_jiffies);
354bfcc09ddSBjoern A. Zeeb 
355bfcc09ddSBjoern A. Zeeb 		switch (te_data->vif->type) {
356bfcc09ddSBjoern A. Zeeb 		case NL80211_IFTYPE_P2P_DEVICE:
357bfcc09ddSBjoern A. Zeeb 			ieee80211_remain_on_channel_expired(mvm->hw);
358*a4128aadSBjoern A. Zeeb 			iwl_mvm_roc_finished(mvm);
359bfcc09ddSBjoern A. Zeeb 			break;
360bfcc09ddSBjoern A. Zeeb 		case NL80211_IFTYPE_STATION:
361bfcc09ddSBjoern A. Zeeb 			/*
362bfcc09ddSBjoern A. Zeeb 			 * If we are switching channel, don't disconnect
363bfcc09ddSBjoern A. Zeeb 			 * if the time event is already done. Beacons can
364bfcc09ddSBjoern A. Zeeb 			 * be delayed a bit after the switch.
365bfcc09ddSBjoern A. Zeeb 			 */
366bfcc09ddSBjoern A. Zeeb 			if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
367bfcc09ddSBjoern A. Zeeb 				IWL_DEBUG_TE(mvm,
368bfcc09ddSBjoern A. Zeeb 					     "No beacon heard and the CS time event is over, don't disconnect\n");
369bfcc09ddSBjoern A. Zeeb 				break;
370bfcc09ddSBjoern A. Zeeb 			}
371bfcc09ddSBjoern A. Zeeb 
372bfcc09ddSBjoern A. Zeeb 			/*
373bfcc09ddSBjoern A. Zeeb 			 * By now, we should have finished association
374bfcc09ddSBjoern A. Zeeb 			 * and know the dtim period.
375bfcc09ddSBjoern A. Zeeb 			 */
376bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_check_disconnect(mvm, te_data->vif,
3779af1bba4SBjoern A. Zeeb 				!te_data->vif->cfg.assoc ?
378bfcc09ddSBjoern A. Zeeb 				"Not associated and the time event is over already..." :
379bfcc09ddSBjoern A. Zeeb 				"No beacon heard and the time event is over already...");
380bfcc09ddSBjoern A. Zeeb 			break;
381bfcc09ddSBjoern A. Zeeb 		default:
382bfcc09ddSBjoern A. Zeeb 			break;
383bfcc09ddSBjoern A. Zeeb 		}
384bfcc09ddSBjoern A. Zeeb 
385bfcc09ddSBjoern A. Zeeb 		iwl_mvm_te_clear_data(mvm, te_data);
386bfcc09ddSBjoern A. Zeeb 	} else if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_START) {
387bfcc09ddSBjoern A. Zeeb 		te_data->running = true;
388bfcc09ddSBjoern A. Zeeb 		te_data->end_jiffies = TU_TO_EXP_TIME(te_data->duration);
389bfcc09ddSBjoern A. Zeeb 
390bfcc09ddSBjoern A. Zeeb 		if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
391*a4128aadSBjoern A. Zeeb 			set_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status);
392bfcc09ddSBjoern A. Zeeb 			ieee80211_ready_on_channel(mvm->hw);
393bfcc09ddSBjoern A. Zeeb 		} else if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
394bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_handle_notify_csa(mvm, te_data, notif);
395bfcc09ddSBjoern A. Zeeb 		}
396bfcc09ddSBjoern A. Zeeb 	} else {
397bfcc09ddSBjoern A. Zeeb 		IWL_WARN(mvm, "Got TE with unknown action\n");
398bfcc09ddSBjoern A. Zeeb 	}
399bfcc09ddSBjoern A. Zeeb }
400bfcc09ddSBjoern A. Zeeb 
401*a4128aadSBjoern A. Zeeb struct iwl_mvm_rx_roc_iterator_data {
402*a4128aadSBjoern A. Zeeb 	u32 activity;
403*a4128aadSBjoern A. Zeeb 	bool end_activity;
404*a4128aadSBjoern A. Zeeb 	bool found;
405*a4128aadSBjoern A. Zeeb };
406*a4128aadSBjoern A. Zeeb 
407*a4128aadSBjoern A. Zeeb static void iwl_mvm_rx_roc_iterator(void *_data, u8 *mac,
408*a4128aadSBjoern A. Zeeb 				    struct ieee80211_vif *vif)
409*a4128aadSBjoern A. Zeeb {
410*a4128aadSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
411*a4128aadSBjoern A. Zeeb 	struct iwl_mvm_rx_roc_iterator_data *data = _data;
412*a4128aadSBjoern A. Zeeb 
413*a4128aadSBjoern A. Zeeb 	if (mvmvif->roc_activity == data->activity) {
414*a4128aadSBjoern A. Zeeb 		data->found = true;
415*a4128aadSBjoern A. Zeeb 		if (data->end_activity)
416*a4128aadSBjoern A. Zeeb 			mvmvif->roc_activity = ROC_NUM_ACTIVITIES;
417*a4128aadSBjoern A. Zeeb 	}
418*a4128aadSBjoern A. Zeeb }
419*a4128aadSBjoern A. Zeeb 
420*a4128aadSBjoern A. Zeeb void iwl_mvm_rx_roc_notif(struct iwl_mvm *mvm,
421*a4128aadSBjoern A. Zeeb 			  struct iwl_rx_cmd_buffer *rxb)
422*a4128aadSBjoern A. Zeeb {
423*a4128aadSBjoern A. Zeeb 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
424*a4128aadSBjoern A. Zeeb 	struct iwl_roc_notif *notif = (void *)pkt->data;
425*a4128aadSBjoern A. Zeeb 	u32 activity = le32_to_cpu(notif->activity);
426*a4128aadSBjoern A. Zeeb 	bool started = le32_to_cpu(notif->success) &&
427*a4128aadSBjoern A. Zeeb 		le32_to_cpu(notif->started);
428*a4128aadSBjoern A. Zeeb 	struct iwl_mvm_rx_roc_iterator_data data = {
429*a4128aadSBjoern A. Zeeb 		.activity = activity,
430*a4128aadSBjoern A. Zeeb 		.end_activity = !started,
431*a4128aadSBjoern A. Zeeb 	};
432*a4128aadSBjoern A. Zeeb 
433*a4128aadSBjoern A. Zeeb 	/* Clear vif roc_activity if done (set to ROC_NUM_ACTIVITIES) */
434*a4128aadSBjoern A. Zeeb 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
435*a4128aadSBjoern A. Zeeb 						   IEEE80211_IFACE_ITER_NORMAL,
436*a4128aadSBjoern A. Zeeb 						   iwl_mvm_rx_roc_iterator,
437*a4128aadSBjoern A. Zeeb 						   &data);
438*a4128aadSBjoern A. Zeeb 	/*
439*a4128aadSBjoern A. Zeeb 	 * It is possible that the ROC was canceled
440*a4128aadSBjoern A. Zeeb 	 * but the notification was already fired.
441*a4128aadSBjoern A. Zeeb 	 */
442*a4128aadSBjoern A. Zeeb 	if (!data.found)
443*a4128aadSBjoern A. Zeeb 		return;
444*a4128aadSBjoern A. Zeeb 
445*a4128aadSBjoern A. Zeeb 	if (started) {
446*a4128aadSBjoern A. Zeeb 		set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
447*a4128aadSBjoern A. Zeeb 		ieee80211_ready_on_channel(mvm->hw);
448*a4128aadSBjoern A. Zeeb 	} else {
449*a4128aadSBjoern A. Zeeb 		iwl_mvm_roc_finished(mvm);
450*a4128aadSBjoern A. Zeeb 		ieee80211_remain_on_channel_expired(mvm->hw);
451*a4128aadSBjoern A. Zeeb 	}
452*a4128aadSBjoern A. Zeeb }
453*a4128aadSBjoern A. Zeeb 
454bfcc09ddSBjoern A. Zeeb /*
455bfcc09ddSBjoern A. Zeeb  * Handle A Aux ROC time event
456bfcc09ddSBjoern A. Zeeb  */
457bfcc09ddSBjoern A. Zeeb static int iwl_mvm_aux_roc_te_handle_notif(struct iwl_mvm *mvm,
458bfcc09ddSBjoern A. Zeeb 					   struct iwl_time_event_notif *notif)
459bfcc09ddSBjoern A. Zeeb {
4609af1bba4SBjoern A. Zeeb 	struct iwl_mvm_time_event_data *aux_roc_te = NULL, *te_data;
461bfcc09ddSBjoern A. Zeeb 
4629af1bba4SBjoern A. Zeeb 	list_for_each_entry(te_data, &mvm->aux_roc_te_list, list) {
463bfcc09ddSBjoern A. Zeeb 		if (le32_to_cpu(notif->unique_id) == te_data->uid) {
4649af1bba4SBjoern A. Zeeb 			aux_roc_te = te_data;
465bfcc09ddSBjoern A. Zeeb 			break;
466bfcc09ddSBjoern A. Zeeb 		}
467bfcc09ddSBjoern A. Zeeb 	}
468bfcc09ddSBjoern A. Zeeb 	if (!aux_roc_te) /* Not a Aux ROC time event */
469bfcc09ddSBjoern A. Zeeb 		return -EINVAL;
470bfcc09ddSBjoern A. Zeeb 
471bfcc09ddSBjoern A. Zeeb 	iwl_mvm_te_check_trigger(mvm, notif, te_data);
472bfcc09ddSBjoern A. Zeeb 
473bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm,
474bfcc09ddSBjoern A. Zeeb 		     "Aux ROC time event notification  - UID = 0x%x action %d (error = %d)\n",
475bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(notif->unique_id),
476bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(notif->action), le32_to_cpu(notif->status));
477bfcc09ddSBjoern A. Zeeb 
478bfcc09ddSBjoern A. Zeeb 	if (!le32_to_cpu(notif->status) ||
479bfcc09ddSBjoern A. Zeeb 	    le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_END) {
480bfcc09ddSBjoern A. Zeeb 		/* End TE, notify mac80211 */
481bfcc09ddSBjoern A. Zeeb 		ieee80211_remain_on_channel_expired(mvm->hw);
482bfcc09ddSBjoern A. Zeeb 		iwl_mvm_roc_finished(mvm); /* flush aux queue */
483bfcc09ddSBjoern A. Zeeb 		list_del(&te_data->list); /* remove from list */
484bfcc09ddSBjoern A. Zeeb 		te_data->running = false;
485bfcc09ddSBjoern A. Zeeb 		te_data->vif = NULL;
486bfcc09ddSBjoern A. Zeeb 		te_data->uid = 0;
487bfcc09ddSBjoern A. Zeeb 		te_data->id = TE_MAX;
488bfcc09ddSBjoern A. Zeeb 	} else if (le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_START) {
489bfcc09ddSBjoern A. Zeeb 		set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
490bfcc09ddSBjoern A. Zeeb 		te_data->running = true;
491bfcc09ddSBjoern A. Zeeb 		ieee80211_ready_on_channel(mvm->hw); /* Start TE */
492bfcc09ddSBjoern A. Zeeb 	} else {
493bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm,
494bfcc09ddSBjoern A. Zeeb 			     "ERROR: Unknown Aux ROC Time Event (action = %d)\n",
495bfcc09ddSBjoern A. Zeeb 			     le32_to_cpu(notif->action));
496bfcc09ddSBjoern A. Zeeb 		return -EINVAL;
497bfcc09ddSBjoern A. Zeeb 	}
498bfcc09ddSBjoern A. Zeeb 
499bfcc09ddSBjoern A. Zeeb 	return 0;
500bfcc09ddSBjoern A. Zeeb }
501bfcc09ddSBjoern A. Zeeb 
502bfcc09ddSBjoern A. Zeeb /*
503bfcc09ddSBjoern A. Zeeb  * The Rx handler for time event notifications
504bfcc09ddSBjoern A. Zeeb  */
505bfcc09ddSBjoern A. Zeeb void iwl_mvm_rx_time_event_notif(struct iwl_mvm *mvm,
506bfcc09ddSBjoern A. Zeeb 				 struct iwl_rx_cmd_buffer *rxb)
507bfcc09ddSBjoern A. Zeeb {
508bfcc09ddSBjoern A. Zeeb 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
509bfcc09ddSBjoern A. Zeeb 	struct iwl_time_event_notif *notif = (void *)pkt->data;
510bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data, *tmp;
511bfcc09ddSBjoern A. Zeeb 
512bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "Time event notification - UID = 0x%x action %d\n",
513bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(notif->unique_id),
514bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(notif->action));
515bfcc09ddSBjoern A. Zeeb 
516bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
517bfcc09ddSBjoern A. Zeeb 	/* This time event is triggered for Aux ROC request */
518bfcc09ddSBjoern A. Zeeb 	if (!iwl_mvm_aux_roc_te_handle_notif(mvm, notif))
519bfcc09ddSBjoern A. Zeeb 		goto unlock;
520bfcc09ddSBjoern A. Zeeb 
521bfcc09ddSBjoern A. Zeeb 	list_for_each_entry_safe(te_data, tmp, &mvm->time_event_list, list) {
522bfcc09ddSBjoern A. Zeeb 		if (le32_to_cpu(notif->unique_id) == te_data->uid)
523bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_handle_notif(mvm, te_data, notif);
524bfcc09ddSBjoern A. Zeeb 	}
525bfcc09ddSBjoern A. Zeeb unlock:
526bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
527bfcc09ddSBjoern A. Zeeb }
528bfcc09ddSBjoern A. Zeeb 
529bfcc09ddSBjoern A. Zeeb static bool iwl_mvm_te_notif(struct iwl_notif_wait_data *notif_wait,
530bfcc09ddSBjoern A. Zeeb 			     struct iwl_rx_packet *pkt, void *data)
531bfcc09ddSBjoern A. Zeeb {
532bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm *mvm =
533bfcc09ddSBjoern A. Zeeb 		container_of(notif_wait, struct iwl_mvm, notif_wait);
534bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = data;
535bfcc09ddSBjoern A. Zeeb 	struct iwl_time_event_notif *resp;
536bfcc09ddSBjoern A. Zeeb 	int resp_len = iwl_rx_packet_payload_len(pkt);
537bfcc09ddSBjoern A. Zeeb 
538bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_NOTIFICATION))
539bfcc09ddSBjoern A. Zeeb 		return true;
540bfcc09ddSBjoern A. Zeeb 
541bfcc09ddSBjoern A. Zeeb 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
542bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Invalid TIME_EVENT_NOTIFICATION response\n");
543bfcc09ddSBjoern A. Zeeb 		return true;
544bfcc09ddSBjoern A. Zeeb 	}
545bfcc09ddSBjoern A. Zeeb 
546bfcc09ddSBjoern A. Zeeb 	resp = (void *)pkt->data;
547bfcc09ddSBjoern A. Zeeb 
548bfcc09ddSBjoern A. Zeeb 	/* te_data->uid is already set in the TIME_EVENT_CMD response */
549bfcc09ddSBjoern A. Zeeb 	if (le32_to_cpu(resp->unique_id) != te_data->uid)
550bfcc09ddSBjoern A. Zeeb 		return false;
551bfcc09ddSBjoern A. Zeeb 
552bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n",
553bfcc09ddSBjoern A. Zeeb 		     te_data->uid);
554bfcc09ddSBjoern A. Zeeb 	if (!resp->status)
555bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm,
556bfcc09ddSBjoern A. Zeeb 			"TIME_EVENT_NOTIFICATION received but not executed\n");
557bfcc09ddSBjoern A. Zeeb 
558bfcc09ddSBjoern A. Zeeb 	return true;
559bfcc09ddSBjoern A. Zeeb }
560bfcc09ddSBjoern A. Zeeb 
561bfcc09ddSBjoern A. Zeeb static bool iwl_mvm_time_event_response(struct iwl_notif_wait_data *notif_wait,
562bfcc09ddSBjoern A. Zeeb 					struct iwl_rx_packet *pkt, void *data)
563bfcc09ddSBjoern A. Zeeb {
564bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm *mvm =
565bfcc09ddSBjoern A. Zeeb 		container_of(notif_wait, struct iwl_mvm, notif_wait);
566bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = data;
567bfcc09ddSBjoern A. Zeeb 	struct iwl_time_event_resp *resp;
568bfcc09ddSBjoern A. Zeeb 	int resp_len = iwl_rx_packet_payload_len(pkt);
569bfcc09ddSBjoern A. Zeeb 
570bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_CMD))
571bfcc09ddSBjoern A. Zeeb 		return true;
572bfcc09ddSBjoern A. Zeeb 
573bfcc09ddSBjoern A. Zeeb 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
574bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Invalid TIME_EVENT_CMD response\n");
575bfcc09ddSBjoern A. Zeeb 		return true;
576bfcc09ddSBjoern A. Zeeb 	}
577bfcc09ddSBjoern A. Zeeb 
578bfcc09ddSBjoern A. Zeeb 	resp = (void *)pkt->data;
579bfcc09ddSBjoern A. Zeeb 
580bfcc09ddSBjoern A. Zeeb 	/* we should never get a response to another TIME_EVENT_CMD here */
581bfcc09ddSBjoern A. Zeeb 	if (WARN_ON_ONCE(le32_to_cpu(resp->id) != te_data->id))
582bfcc09ddSBjoern A. Zeeb 		return false;
583bfcc09ddSBjoern A. Zeeb 
584bfcc09ddSBjoern A. Zeeb 	te_data->uid = le32_to_cpu(resp->unique_id);
585bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
586bfcc09ddSBjoern A. Zeeb 		     te_data->uid);
587bfcc09ddSBjoern A. Zeeb 	return true;
588bfcc09ddSBjoern A. Zeeb }
589bfcc09ddSBjoern A. Zeeb 
590bfcc09ddSBjoern A. Zeeb static int iwl_mvm_time_event_send_add(struct iwl_mvm *mvm,
591bfcc09ddSBjoern A. Zeeb 				       struct ieee80211_vif *vif,
592bfcc09ddSBjoern A. Zeeb 				       struct iwl_mvm_time_event_data *te_data,
593bfcc09ddSBjoern A. Zeeb 				       struct iwl_time_event_cmd *te_cmd)
594bfcc09ddSBjoern A. Zeeb {
595bfcc09ddSBjoern A. Zeeb 	static const u16 time_event_response[] = { TIME_EVENT_CMD };
596bfcc09ddSBjoern A. Zeeb 	struct iwl_notification_wait wait_time_event;
597bfcc09ddSBjoern A. Zeeb 	int ret;
598bfcc09ddSBjoern A. Zeeb 
599bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
600bfcc09ddSBjoern A. Zeeb 
601bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "Add new TE, duration %d TU\n",
602bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(te_cmd->duration));
603bfcc09ddSBjoern A. Zeeb 
604bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
605bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(te_data->id != TE_MAX)) {
606bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&mvm->time_event_lock);
607bfcc09ddSBjoern A. Zeeb 		return -EIO;
608bfcc09ddSBjoern A. Zeeb 	}
609bfcc09ddSBjoern A. Zeeb 	te_data->vif = vif;
610bfcc09ddSBjoern A. Zeeb 	te_data->duration = le32_to_cpu(te_cmd->duration);
611bfcc09ddSBjoern A. Zeeb 	te_data->id = le32_to_cpu(te_cmd->id);
612bfcc09ddSBjoern A. Zeeb 	list_add_tail(&te_data->list, &mvm->time_event_list);
613bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
614bfcc09ddSBjoern A. Zeeb 
615bfcc09ddSBjoern A. Zeeb 	/*
616bfcc09ddSBjoern A. Zeeb 	 * Use a notification wait, which really just processes the
617bfcc09ddSBjoern A. Zeeb 	 * command response and doesn't wait for anything, in order
618bfcc09ddSBjoern A. Zeeb 	 * to be able to process the response and get the UID inside
619bfcc09ddSBjoern A. Zeeb 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
620bfcc09ddSBjoern A. Zeeb 	 * stores the buffer and then wakes up this thread, by which
621bfcc09ddSBjoern A. Zeeb 	 * time another notification (that the time event started)
622bfcc09ddSBjoern A. Zeeb 	 * might already be processed unsuccessfully.
623bfcc09ddSBjoern A. Zeeb 	 */
624bfcc09ddSBjoern A. Zeeb 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
625bfcc09ddSBjoern A. Zeeb 				   time_event_response,
626bfcc09ddSBjoern A. Zeeb 				   ARRAY_SIZE(time_event_response),
627bfcc09ddSBjoern A. Zeeb 				   iwl_mvm_time_event_response, te_data);
628bfcc09ddSBjoern A. Zeeb 
629bfcc09ddSBjoern A. Zeeb 	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
630bfcc09ddSBjoern A. Zeeb 					    sizeof(*te_cmd), te_cmd);
631bfcc09ddSBjoern A. Zeeb 	if (ret) {
632bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Couldn't send TIME_EVENT_CMD: %d\n", ret);
633bfcc09ddSBjoern A. Zeeb 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
634bfcc09ddSBjoern A. Zeeb 		goto out_clear_te;
635bfcc09ddSBjoern A. Zeeb 	}
636bfcc09ddSBjoern A. Zeeb 
637bfcc09ddSBjoern A. Zeeb 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
638bfcc09ddSBjoern A. Zeeb 	ret = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
639bfcc09ddSBjoern A. Zeeb 	/* should never fail */
640bfcc09ddSBjoern A. Zeeb 	WARN_ON_ONCE(ret);
641bfcc09ddSBjoern A. Zeeb 
642bfcc09ddSBjoern A. Zeeb 	if (ret) {
643bfcc09ddSBjoern A. Zeeb  out_clear_te:
644bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&mvm->time_event_lock);
645bfcc09ddSBjoern A. Zeeb 		iwl_mvm_te_clear_data(mvm, te_data);
646bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&mvm->time_event_lock);
647bfcc09ddSBjoern A. Zeeb 	}
648bfcc09ddSBjoern A. Zeeb 	return ret;
649bfcc09ddSBjoern A. Zeeb }
650bfcc09ddSBjoern A. Zeeb 
651bfcc09ddSBjoern A. Zeeb void iwl_mvm_protect_session(struct iwl_mvm *mvm,
652bfcc09ddSBjoern A. Zeeb 			     struct ieee80211_vif *vif,
653bfcc09ddSBjoern A. Zeeb 			     u32 duration, u32 min_duration,
654bfcc09ddSBjoern A. Zeeb 			     u32 max_delay, bool wait_for_notif)
655bfcc09ddSBjoern A. Zeeb {
656bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
657bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
658bfcc09ddSBjoern A. Zeeb 	const u16 te_notif_response[] = { TIME_EVENT_NOTIFICATION };
659bfcc09ddSBjoern A. Zeeb 	struct iwl_notification_wait wait_te_notif;
660bfcc09ddSBjoern A. Zeeb 	struct iwl_time_event_cmd time_cmd = {};
661bfcc09ddSBjoern A. Zeeb 
662bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
663bfcc09ddSBjoern A. Zeeb 
664bfcc09ddSBjoern A. Zeeb 	if (te_data->running &&
665bfcc09ddSBjoern A. Zeeb 	    time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
666bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
667bfcc09ddSBjoern A. Zeeb 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
668bfcc09ddSBjoern A. Zeeb 		return;
669bfcc09ddSBjoern A. Zeeb 	}
670bfcc09ddSBjoern A. Zeeb 
671bfcc09ddSBjoern A. Zeeb 	if (te_data->running) {
672bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm, "extend 0x%x: only %u ms left\n",
673bfcc09ddSBjoern A. Zeeb 			     te_data->uid,
674bfcc09ddSBjoern A. Zeeb 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
675bfcc09ddSBjoern A. Zeeb 		/*
676bfcc09ddSBjoern A. Zeeb 		 * we don't have enough time
677bfcc09ddSBjoern A. Zeeb 		 * cancel the current TE and issue a new one
678bfcc09ddSBjoern A. Zeeb 		 * Of course it would be better to remove the old one only
679bfcc09ddSBjoern A. Zeeb 		 * when the new one is added, but we don't care if we are off
680bfcc09ddSBjoern A. Zeeb 		 * channel for a bit. All we need to do, is not to return
681bfcc09ddSBjoern A. Zeeb 		 * before we actually begin to be on the channel.
682bfcc09ddSBjoern A. Zeeb 		 */
683bfcc09ddSBjoern A. Zeeb 		iwl_mvm_stop_session_protection(mvm, vif);
684bfcc09ddSBjoern A. Zeeb 	}
685bfcc09ddSBjoern A. Zeeb 
686bfcc09ddSBjoern A. Zeeb 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
687bfcc09ddSBjoern A. Zeeb 	time_cmd.id_and_color =
688bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
689bfcc09ddSBjoern A. Zeeb 	time_cmd.id = cpu_to_le32(TE_BSS_STA_AGGRESSIVE_ASSOC);
690bfcc09ddSBjoern A. Zeeb 
691bfcc09ddSBjoern A. Zeeb 	time_cmd.apply_time = cpu_to_le32(0);
692bfcc09ddSBjoern A. Zeeb 
693bfcc09ddSBjoern A. Zeeb 	time_cmd.max_frags = TE_V2_FRAG_NONE;
694bfcc09ddSBjoern A. Zeeb 	time_cmd.max_delay = cpu_to_le32(max_delay);
695bfcc09ddSBjoern A. Zeeb 	/* TODO: why do we need to interval = bi if it is not periodic? */
696bfcc09ddSBjoern A. Zeeb 	time_cmd.interval = cpu_to_le32(1);
697bfcc09ddSBjoern A. Zeeb 	time_cmd.duration = cpu_to_le32(duration);
698bfcc09ddSBjoern A. Zeeb 	time_cmd.repeat = 1;
699bfcc09ddSBjoern A. Zeeb 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
700bfcc09ddSBjoern A. Zeeb 				      TE_V2_NOTIF_HOST_EVENT_END |
701bfcc09ddSBjoern A. Zeeb 				      TE_V2_START_IMMEDIATELY);
702bfcc09ddSBjoern A. Zeeb 
703bfcc09ddSBjoern A. Zeeb 	if (!wait_for_notif) {
704bfcc09ddSBjoern A. Zeeb 		iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
705bfcc09ddSBjoern A. Zeeb 		return;
706bfcc09ddSBjoern A. Zeeb 	}
707bfcc09ddSBjoern A. Zeeb 
708bfcc09ddSBjoern A. Zeeb 	/*
709bfcc09ddSBjoern A. Zeeb 	 * Create notification_wait for the TIME_EVENT_NOTIFICATION to use
710bfcc09ddSBjoern A. Zeeb 	 * right after we send the time event
711bfcc09ddSBjoern A. Zeeb 	 */
712bfcc09ddSBjoern A. Zeeb 	iwl_init_notification_wait(&mvm->notif_wait, &wait_te_notif,
713bfcc09ddSBjoern A. Zeeb 				   te_notif_response,
714bfcc09ddSBjoern A. Zeeb 				   ARRAY_SIZE(te_notif_response),
715bfcc09ddSBjoern A. Zeeb 				   iwl_mvm_te_notif, te_data);
716bfcc09ddSBjoern A. Zeeb 
717bfcc09ddSBjoern A. Zeeb 	/* If TE was sent OK - wait for the notification that started */
718bfcc09ddSBjoern A. Zeeb 	if (iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd)) {
719bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Failed to add TE to protect session\n");
720bfcc09ddSBjoern A. Zeeb 		iwl_remove_notification(&mvm->notif_wait, &wait_te_notif);
721bfcc09ddSBjoern A. Zeeb 	} else if (iwl_wait_notification(&mvm->notif_wait, &wait_te_notif,
722bfcc09ddSBjoern A. Zeeb 					 TU_TO_JIFFIES(max_delay))) {
723bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Failed to protect session until TE\n");
724bfcc09ddSBjoern A. Zeeb 	}
725bfcc09ddSBjoern A. Zeeb }
726bfcc09ddSBjoern A. Zeeb 
727*a4128aadSBjoern A. Zeeb /* Determine whether mac or link id should be used, and validate the link id */
728*a4128aadSBjoern A. Zeeb static int iwl_mvm_get_session_prot_id(struct iwl_mvm *mvm,
729*a4128aadSBjoern A. Zeeb 				       struct ieee80211_vif *vif,
730*a4128aadSBjoern A. Zeeb 				       s8 link_id)
731bfcc09ddSBjoern A. Zeeb {
732*a4128aadSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
733*a4128aadSBjoern A. Zeeb 	int ver = iwl_fw_lookup_cmd_ver(mvm->fw,
734*a4128aadSBjoern A. Zeeb 					WIDE_ID(MAC_CONF_GROUP,
735*a4128aadSBjoern A. Zeeb 						SESSION_PROTECTION_CMD), 1);
736*a4128aadSBjoern A. Zeeb 
737*a4128aadSBjoern A. Zeeb 	if (ver < 2)
738*a4128aadSBjoern A. Zeeb 		return mvmvif->id;
739*a4128aadSBjoern A. Zeeb 
740*a4128aadSBjoern A. Zeeb 	if (WARN(link_id < 0 || !mvmvif->link[link_id],
741*a4128aadSBjoern A. Zeeb 		 "Invalid link ID for session protection: %u\n", link_id))
742*a4128aadSBjoern A. Zeeb 		return -EINVAL;
743*a4128aadSBjoern A. Zeeb 
744*a4128aadSBjoern A. Zeeb 	if (WARN(!mvmvif->link[link_id]->active,
745*a4128aadSBjoern A. Zeeb 		 "Session Protection on an inactive link: %u\n", link_id))
746*a4128aadSBjoern A. Zeeb 		return -EINVAL;
747*a4128aadSBjoern A. Zeeb 
748*a4128aadSBjoern A. Zeeb 	return mvmvif->link[link_id]->fw_link_id;
749*a4128aadSBjoern A. Zeeb }
750*a4128aadSBjoern A. Zeeb 
751*a4128aadSBjoern A. Zeeb static void iwl_mvm_cancel_session_protection(struct iwl_mvm *mvm,
752*a4128aadSBjoern A. Zeeb 					      struct ieee80211_vif *vif,
753*a4128aadSBjoern A. Zeeb 					      u32 id, s8 link_id)
754*a4128aadSBjoern A. Zeeb {
755*a4128aadSBjoern A. Zeeb 	int mac_link_id = iwl_mvm_get_session_prot_id(mvm, vif, link_id);
756bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_session_prot_cmd cmd = {
757*a4128aadSBjoern A. Zeeb 		.id_and_color = cpu_to_le32(mac_link_id),
758bfcc09ddSBjoern A. Zeeb 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
759bfcc09ddSBjoern A. Zeeb 		.conf_id = cpu_to_le32(id),
760bfcc09ddSBjoern A. Zeeb 	};
761bfcc09ddSBjoern A. Zeeb 	int ret;
762bfcc09ddSBjoern A. Zeeb 
763*a4128aadSBjoern A. Zeeb 	if (mac_link_id < 0)
764*a4128aadSBjoern A. Zeeb 		return;
765*a4128aadSBjoern A. Zeeb 
766d9836fb4SBjoern A. Zeeb 	ret = iwl_mvm_send_cmd_pdu(mvm,
767d9836fb4SBjoern A. Zeeb 				   WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
768bfcc09ddSBjoern A. Zeeb 				   0, sizeof(cmd), &cmd);
769bfcc09ddSBjoern A. Zeeb 	if (ret)
770bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm,
771bfcc09ddSBjoern A. Zeeb 			"Couldn't send the SESSION_PROTECTION_CMD: %d\n", ret);
772bfcc09ddSBjoern A. Zeeb }
773bfcc09ddSBjoern A. Zeeb 
774*a4128aadSBjoern A. Zeeb static void iwl_mvm_roc_rm_cmd(struct iwl_mvm *mvm, u32 activity)
775*a4128aadSBjoern A. Zeeb {
776*a4128aadSBjoern A. Zeeb 	struct iwl_roc_req roc_cmd = {
777*a4128aadSBjoern A. Zeeb 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
778*a4128aadSBjoern A. Zeeb 		.activity = cpu_to_le32(activity),
779*a4128aadSBjoern A. Zeeb 	};
780*a4128aadSBjoern A. Zeeb 	int ret;
781*a4128aadSBjoern A. Zeeb 
782*a4128aadSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
783*a4128aadSBjoern A. Zeeb 	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0,
784*a4128aadSBjoern A. Zeeb 				   sizeof(roc_cmd), &roc_cmd);
785*a4128aadSBjoern A. Zeeb 	if (ret)
786*a4128aadSBjoern A. Zeeb 		IWL_ERR(mvm, "Couldn't send the ROC_CMD: %d\n", ret);
787*a4128aadSBjoern A. Zeeb }
788*a4128aadSBjoern A. Zeeb 
789bfcc09ddSBjoern A. Zeeb static bool __iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
790bfcc09ddSBjoern A. Zeeb 					struct iwl_mvm_time_event_data *te_data,
791bfcc09ddSBjoern A. Zeeb 					u32 *uid)
792bfcc09ddSBjoern A. Zeeb {
793bfcc09ddSBjoern A. Zeeb 	u32 id;
794*a4128aadSBjoern A. Zeeb 	struct ieee80211_vif *vif = te_data->vif;
795bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif;
796bfcc09ddSBjoern A. Zeeb 	enum nl80211_iftype iftype;
797*a4128aadSBjoern A. Zeeb 	s8 link_id;
798*a4128aadSBjoern A. Zeeb 	bool p2p_aux = iwl_mvm_has_p2p_over_aux(mvm);
799*a4128aadSBjoern A. Zeeb 	u8 roc_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
800*a4128aadSBjoern A. Zeeb 					   WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0);
801bfcc09ddSBjoern A. Zeeb 
802*a4128aadSBjoern A. Zeeb 	if (!vif)
803bfcc09ddSBjoern A. Zeeb 		return false;
804bfcc09ddSBjoern A. Zeeb 
805bfcc09ddSBjoern A. Zeeb 	mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
806bfcc09ddSBjoern A. Zeeb 	iftype = te_data->vif->type;
807bfcc09ddSBjoern A. Zeeb 
808bfcc09ddSBjoern A. Zeeb 	/*
809bfcc09ddSBjoern A. Zeeb 	 * It is possible that by the time we got to this point the time
810bfcc09ddSBjoern A. Zeeb 	 * event was already removed.
811bfcc09ddSBjoern A. Zeeb 	 */
812bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
813bfcc09ddSBjoern A. Zeeb 
814bfcc09ddSBjoern A. Zeeb 	/* Save time event uid before clearing its data */
815bfcc09ddSBjoern A. Zeeb 	*uid = te_data->uid;
816bfcc09ddSBjoern A. Zeeb 	id = te_data->id;
817*a4128aadSBjoern A. Zeeb 	link_id = te_data->link_id;
818bfcc09ddSBjoern A. Zeeb 
819bfcc09ddSBjoern A. Zeeb 	/*
820bfcc09ddSBjoern A. Zeeb 	 * The clear_data function handles time events that were already removed
821bfcc09ddSBjoern A. Zeeb 	 */
822bfcc09ddSBjoern A. Zeeb 	iwl_mvm_te_clear_data(mvm, te_data);
823bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
824bfcc09ddSBjoern A. Zeeb 
825*a4128aadSBjoern A. Zeeb 	if ((p2p_aux && iftype == NL80211_IFTYPE_P2P_DEVICE) ||
826*a4128aadSBjoern A. Zeeb 	    (roc_ver >= 3 && mvmvif->roc_activity == ROC_ACTIVITY_HOTSPOT)) {
827*a4128aadSBjoern A. Zeeb 		if (mvmvif->roc_activity < ROC_NUM_ACTIVITIES) {
828*a4128aadSBjoern A. Zeeb 			iwl_mvm_roc_rm_cmd(mvm, mvmvif->roc_activity);
829*a4128aadSBjoern A. Zeeb 			mvmvif->roc_activity = ROC_NUM_ACTIVITIES;
830*a4128aadSBjoern A. Zeeb 			iwl_mvm_roc_finished(mvm);
831*a4128aadSBjoern A. Zeeb 		}
832*a4128aadSBjoern A. Zeeb 		return false;
833*a4128aadSBjoern A. Zeeb 	} else if (fw_has_capa(&mvm->fw->ucode_capa,
834d9836fb4SBjoern A. Zeeb 			       IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD) &&
835d9836fb4SBjoern A. Zeeb 		   id != HOT_SPOT_CMD) {
836*a4128aadSBjoern A. Zeeb 		/* When session protection is used, the te_data->id field
837*a4128aadSBjoern A. Zeeb 		 * is reused to save session protection's configuration.
838*a4128aadSBjoern A. Zeeb 		 * For AUX ROC, HOT_SPOT_CMD is used and the te_data->id
839*a4128aadSBjoern A. Zeeb 		 * field is set to HOT_SPOT_CMD.
840*a4128aadSBjoern A. Zeeb 		 */
841bfcc09ddSBjoern A. Zeeb 		if (mvmvif && id < SESSION_PROTECT_CONF_MAX_ID) {
842bfcc09ddSBjoern A. Zeeb 			/* Session protection is still ongoing. Cancel it */
843*a4128aadSBjoern A. Zeeb 			iwl_mvm_cancel_session_protection(mvm, vif, id,
844*a4128aadSBjoern A. Zeeb 							  link_id);
845bfcc09ddSBjoern A. Zeeb 			if (iftype == NL80211_IFTYPE_P2P_DEVICE) {
846*a4128aadSBjoern A. Zeeb 				iwl_mvm_roc_finished(mvm);
847bfcc09ddSBjoern A. Zeeb 			}
848bfcc09ddSBjoern A. Zeeb 		}
849bfcc09ddSBjoern A. Zeeb 		return false;
850bfcc09ddSBjoern A. Zeeb 	} else {
851bfcc09ddSBjoern A. Zeeb 		/* It is possible that by the time we try to remove it, the
852bfcc09ddSBjoern A. Zeeb 		 * time event has already ended and removed. In such a case
853bfcc09ddSBjoern A. Zeeb 		 * there is no need to send a removal command.
854bfcc09ddSBjoern A. Zeeb 		 */
855bfcc09ddSBjoern A. Zeeb 		if (id == TE_MAX) {
856bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", *uid);
857bfcc09ddSBjoern A. Zeeb 			return false;
858bfcc09ddSBjoern A. Zeeb 		}
859bfcc09ddSBjoern A. Zeeb 	}
860bfcc09ddSBjoern A. Zeeb 
861bfcc09ddSBjoern A. Zeeb 	return true;
862bfcc09ddSBjoern A. Zeeb }
863bfcc09ddSBjoern A. Zeeb 
864bfcc09ddSBjoern A. Zeeb /*
865bfcc09ddSBjoern A. Zeeb  * Explicit request to remove a aux roc time event. The removal of a time
866bfcc09ddSBjoern A. Zeeb  * event needs to be synchronized with the flow of a time event's end
867bfcc09ddSBjoern A. Zeeb  * notification, which also removes the time event from the op mode
868bfcc09ddSBjoern A. Zeeb  * data structures.
869bfcc09ddSBjoern A. Zeeb  */
870bfcc09ddSBjoern A. Zeeb static void iwl_mvm_remove_aux_roc_te(struct iwl_mvm *mvm,
871bfcc09ddSBjoern A. Zeeb 				      struct iwl_mvm_vif *mvmvif,
872bfcc09ddSBjoern A. Zeeb 				      struct iwl_mvm_time_event_data *te_data)
873bfcc09ddSBjoern A. Zeeb {
874bfcc09ddSBjoern A. Zeeb 	struct iwl_hs20_roc_req aux_cmd = {};
875bfcc09ddSBjoern A. Zeeb 	u16 len = sizeof(aux_cmd) - iwl_mvm_chan_info_padding(mvm);
876bfcc09ddSBjoern A. Zeeb 
877bfcc09ddSBjoern A. Zeeb 	u32 uid;
878bfcc09ddSBjoern A. Zeeb 	int ret;
879bfcc09ddSBjoern A. Zeeb 
880bfcc09ddSBjoern A. Zeeb 	if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
881bfcc09ddSBjoern A. Zeeb 		return;
882bfcc09ddSBjoern A. Zeeb 
883bfcc09ddSBjoern A. Zeeb 	aux_cmd.event_unique_id = cpu_to_le32(uid);
884bfcc09ddSBjoern A. Zeeb 	aux_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
885bfcc09ddSBjoern A. Zeeb 	aux_cmd.id_and_color =
886bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
887bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "Removing BSS AUX ROC TE 0x%x\n",
888bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(aux_cmd.event_unique_id));
889bfcc09ddSBjoern A. Zeeb 	ret = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0,
890bfcc09ddSBjoern A. Zeeb 				   len, &aux_cmd);
891bfcc09ddSBjoern A. Zeeb 
892bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(ret))
893bfcc09ddSBjoern A. Zeeb 		return;
894bfcc09ddSBjoern A. Zeeb }
895bfcc09ddSBjoern A. Zeeb 
896bfcc09ddSBjoern A. Zeeb /*
897bfcc09ddSBjoern A. Zeeb  * Explicit request to remove a time event. The removal of a time event needs to
898bfcc09ddSBjoern A. Zeeb  * be synchronized with the flow of a time event's end notification, which also
899bfcc09ddSBjoern A. Zeeb  * removes the time event from the op mode data structures.
900bfcc09ddSBjoern A. Zeeb  */
901bfcc09ddSBjoern A. Zeeb void iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
902bfcc09ddSBjoern A. Zeeb 			       struct iwl_mvm_vif *mvmvif,
903bfcc09ddSBjoern A. Zeeb 			       struct iwl_mvm_time_event_data *te_data)
904bfcc09ddSBjoern A. Zeeb {
905bfcc09ddSBjoern A. Zeeb 	struct iwl_time_event_cmd time_cmd = {};
906bfcc09ddSBjoern A. Zeeb 	u32 uid;
907bfcc09ddSBjoern A. Zeeb 	int ret;
908bfcc09ddSBjoern A. Zeeb 
909bfcc09ddSBjoern A. Zeeb 	if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
910bfcc09ddSBjoern A. Zeeb 		return;
911bfcc09ddSBjoern A. Zeeb 
912bfcc09ddSBjoern A. Zeeb 	/* When we remove a TE, the UID is to be set in the id field */
913bfcc09ddSBjoern A. Zeeb 	time_cmd.id = cpu_to_le32(uid);
914bfcc09ddSBjoern A. Zeeb 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
915bfcc09ddSBjoern A. Zeeb 	time_cmd.id_and_color =
916bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
917bfcc09ddSBjoern A. Zeeb 
918bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "Removing TE 0x%x\n", le32_to_cpu(time_cmd.id));
919bfcc09ddSBjoern A. Zeeb 	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
920bfcc09ddSBjoern A. Zeeb 				   sizeof(time_cmd), &time_cmd);
921bfcc09ddSBjoern A. Zeeb 	if (ret)
922bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Couldn't remove the time event\n");
923bfcc09ddSBjoern A. Zeeb }
924bfcc09ddSBjoern A. Zeeb 
925bfcc09ddSBjoern A. Zeeb void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,
926bfcc09ddSBjoern A. Zeeb 				     struct ieee80211_vif *vif)
927bfcc09ddSBjoern A. Zeeb {
928bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
929bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
930bfcc09ddSBjoern A. Zeeb 	u32 id;
931bfcc09ddSBjoern A. Zeeb 
932bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
933bfcc09ddSBjoern A. Zeeb 
934bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
935bfcc09ddSBjoern A. Zeeb 	id = te_data->id;
936bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
937bfcc09ddSBjoern A. Zeeb 
938bfcc09ddSBjoern A. Zeeb 	if (fw_has_capa(&mvm->fw->ucode_capa,
939bfcc09ddSBjoern A. Zeeb 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
940bfcc09ddSBjoern A. Zeeb 		if (id != SESSION_PROTECT_CONF_ASSOC) {
941bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_TE(mvm,
942bfcc09ddSBjoern A. Zeeb 				     "don't remove session protection id=%u\n",
943bfcc09ddSBjoern A. Zeeb 				     id);
944bfcc09ddSBjoern A. Zeeb 			return;
945bfcc09ddSBjoern A. Zeeb 		}
946bfcc09ddSBjoern A. Zeeb 	} else if (id != TE_BSS_STA_AGGRESSIVE_ASSOC) {
947bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm,
948bfcc09ddSBjoern A. Zeeb 			     "don't remove TE with id=%u (not session protection)\n",
949bfcc09ddSBjoern A. Zeeb 			     id);
950bfcc09ddSBjoern A. Zeeb 		return;
951bfcc09ddSBjoern A. Zeeb 	}
952bfcc09ddSBjoern A. Zeeb 
953bfcc09ddSBjoern A. Zeeb 	iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
954bfcc09ddSBjoern A. Zeeb }
955bfcc09ddSBjoern A. Zeeb 
956bfcc09ddSBjoern A. Zeeb void iwl_mvm_rx_session_protect_notif(struct iwl_mvm *mvm,
957bfcc09ddSBjoern A. Zeeb 				      struct iwl_rx_cmd_buffer *rxb)
958bfcc09ddSBjoern A. Zeeb {
959bfcc09ddSBjoern A. Zeeb 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
960bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_session_prot_notif *notif = (void *)pkt->data;
961*a4128aadSBjoern A. Zeeb 	unsigned int ver =
962*a4128aadSBjoern A. Zeeb 		iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
963*a4128aadSBjoern A. Zeeb 					SESSION_PROTECTION_NOTIF, 2);
964*a4128aadSBjoern A. Zeeb 	int id = le32_to_cpu(notif->mac_link_id);
965bfcc09ddSBjoern A. Zeeb 	struct ieee80211_vif *vif;
966bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif;
967*a4128aadSBjoern A. Zeeb 	unsigned int notif_link_id;
968bfcc09ddSBjoern A. Zeeb 
969bfcc09ddSBjoern A. Zeeb 	rcu_read_lock();
970*a4128aadSBjoern A. Zeeb 
971*a4128aadSBjoern A. Zeeb 	if (ver <= 2) {
972*a4128aadSBjoern A. Zeeb 		vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
973*a4128aadSBjoern A. Zeeb 	} else {
974*a4128aadSBjoern A. Zeeb 		struct ieee80211_bss_conf *link_conf =
975*a4128aadSBjoern A. Zeeb 			iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, id, true);
976*a4128aadSBjoern A. Zeeb 
977*a4128aadSBjoern A. Zeeb 		if (!link_conf)
978*a4128aadSBjoern A. Zeeb 			goto out_unlock;
979*a4128aadSBjoern A. Zeeb 
980*a4128aadSBjoern A. Zeeb 		notif_link_id = link_conf->link_id;
981*a4128aadSBjoern A. Zeeb 		vif = link_conf->vif;
982*a4128aadSBjoern A. Zeeb 	}
983bfcc09ddSBjoern A. Zeeb 
984bfcc09ddSBjoern A. Zeeb 	if (!vif)
985bfcc09ddSBjoern A. Zeeb 		goto out_unlock;
986bfcc09ddSBjoern A. Zeeb 
987bfcc09ddSBjoern A. Zeeb 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
988bfcc09ddSBjoern A. Zeeb 
989*a4128aadSBjoern A. Zeeb 	if (WARN(ver > 2 && mvmvif->time_event_data.link_id >= 0 &&
990*a4128aadSBjoern A. Zeeb 		 mvmvif->time_event_data.link_id != notif_link_id,
991*a4128aadSBjoern A. Zeeb 		 "SESSION_PROTECTION_NOTIF was received for link %u, while the current time event is on link %u\n",
992*a4128aadSBjoern A. Zeeb 		 notif_link_id, mvmvif->time_event_data.link_id))
993*a4128aadSBjoern A. Zeeb 		goto out_unlock;
994*a4128aadSBjoern A. Zeeb 
995bfcc09ddSBjoern A. Zeeb 	/* The vif is not a P2P_DEVICE, maintain its time_event_data */
996bfcc09ddSBjoern A. Zeeb 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
997bfcc09ddSBjoern A. Zeeb 		struct iwl_mvm_time_event_data *te_data =
998bfcc09ddSBjoern A. Zeeb 			&mvmvif->time_event_data;
999bfcc09ddSBjoern A. Zeeb 
1000bfcc09ddSBjoern A. Zeeb 		if (!le32_to_cpu(notif->status)) {
1001bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_check_disconnect(mvm, vif,
1002bfcc09ddSBjoern A. Zeeb 						    "Session protection failure");
1003bfcc09ddSBjoern A. Zeeb 			spin_lock_bh(&mvm->time_event_lock);
1004bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_clear_data(mvm, te_data);
1005bfcc09ddSBjoern A. Zeeb 			spin_unlock_bh(&mvm->time_event_lock);
1006bfcc09ddSBjoern A. Zeeb 		}
1007bfcc09ddSBjoern A. Zeeb 
1008bfcc09ddSBjoern A. Zeeb 		if (le32_to_cpu(notif->start)) {
1009bfcc09ddSBjoern A. Zeeb 			spin_lock_bh(&mvm->time_event_lock);
1010bfcc09ddSBjoern A. Zeeb 			te_data->running = le32_to_cpu(notif->start);
1011bfcc09ddSBjoern A. Zeeb 			te_data->end_jiffies =
1012bfcc09ddSBjoern A. Zeeb 				TU_TO_EXP_TIME(te_data->duration);
1013bfcc09ddSBjoern A. Zeeb 			spin_unlock_bh(&mvm->time_event_lock);
1014bfcc09ddSBjoern A. Zeeb 		} else {
1015bfcc09ddSBjoern A. Zeeb 			/*
1016bfcc09ddSBjoern A. Zeeb 			 * By now, we should have finished association
1017bfcc09ddSBjoern A. Zeeb 			 * and know the dtim period.
1018bfcc09ddSBjoern A. Zeeb 			 */
1019bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_check_disconnect(mvm, vif,
10209af1bba4SBjoern A. Zeeb 						    !vif->cfg.assoc ?
1021bfcc09ddSBjoern A. Zeeb 						    "Not associated and the session protection is over already..." :
1022bfcc09ddSBjoern A. Zeeb 						    "No beacon heard and the session protection is over already...");
1023bfcc09ddSBjoern A. Zeeb 			spin_lock_bh(&mvm->time_event_lock);
1024bfcc09ddSBjoern A. Zeeb 			iwl_mvm_te_clear_data(mvm, te_data);
1025bfcc09ddSBjoern A. Zeeb 			spin_unlock_bh(&mvm->time_event_lock);
1026bfcc09ddSBjoern A. Zeeb 		}
1027bfcc09ddSBjoern A. Zeeb 
1028bfcc09ddSBjoern A. Zeeb 		goto out_unlock;
1029bfcc09ddSBjoern A. Zeeb 	}
1030bfcc09ddSBjoern A. Zeeb 
1031bfcc09ddSBjoern A. Zeeb 	if (!le32_to_cpu(notif->status) || !le32_to_cpu(notif->start)) {
1032bfcc09ddSBjoern A. Zeeb 		/* End TE, notify mac80211 */
1033bfcc09ddSBjoern A. Zeeb 		mvmvif->time_event_data.id = SESSION_PROTECT_CONF_MAX_ID;
1034*a4128aadSBjoern A. Zeeb 		mvmvif->time_event_data.link_id = -1;
1035*a4128aadSBjoern A. Zeeb 		iwl_mvm_roc_finished(mvm);
1036bfcc09ddSBjoern A. Zeeb 		ieee80211_remain_on_channel_expired(mvm->hw);
1037bfcc09ddSBjoern A. Zeeb 	} else if (le32_to_cpu(notif->start)) {
1038bfcc09ddSBjoern A. Zeeb 		if (WARN_ON(mvmvif->time_event_data.id !=
1039bfcc09ddSBjoern A. Zeeb 				le32_to_cpu(notif->conf_id)))
1040bfcc09ddSBjoern A. Zeeb 			goto out_unlock;
1041*a4128aadSBjoern A. Zeeb 		set_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status);
1042bfcc09ddSBjoern A. Zeeb 		ieee80211_ready_on_channel(mvm->hw); /* Start TE */
1043bfcc09ddSBjoern A. Zeeb 	}
1044bfcc09ddSBjoern A. Zeeb 
1045bfcc09ddSBjoern A. Zeeb  out_unlock:
1046bfcc09ddSBjoern A. Zeeb 	rcu_read_unlock();
1047bfcc09ddSBjoern A. Zeeb }
1048bfcc09ddSBjoern A. Zeeb 
1049*a4128aadSBjoern A. Zeeb #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
1050*a4128aadSBjoern A. Zeeb #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
1051*a4128aadSBjoern A. Zeeb #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
1052*a4128aadSBjoern A. Zeeb #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
1053*a4128aadSBjoern A. Zeeb #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
1054*a4128aadSBjoern A. Zeeb 
1055*a4128aadSBjoern A. Zeeb void iwl_mvm_roc_duration_and_delay(struct ieee80211_vif *vif,
1056*a4128aadSBjoern A. Zeeb 				    u32 duration_ms,
1057*a4128aadSBjoern A. Zeeb 				    u32 *duration_tu,
1058*a4128aadSBjoern A. Zeeb 				    u32 *delay)
1059*a4128aadSBjoern A. Zeeb {
1060*a4128aadSBjoern A. Zeeb 	struct ieee80211_bss_conf *link_conf;
1061*a4128aadSBjoern A. Zeeb 	unsigned int link_id;
1062*a4128aadSBjoern A. Zeeb 	u32 dtim_interval = 0;
1063*a4128aadSBjoern A. Zeeb 
1064*a4128aadSBjoern A. Zeeb 	*delay = AUX_ROC_MIN_DELAY;
1065*a4128aadSBjoern A. Zeeb 	*duration_tu = MSEC_TO_TU(duration_ms);
1066*a4128aadSBjoern A. Zeeb 
1067*a4128aadSBjoern A. Zeeb 	rcu_read_lock();
1068*a4128aadSBjoern A. Zeeb 	for_each_vif_active_link(vif, link_conf, link_id) {
1069*a4128aadSBjoern A. Zeeb 		dtim_interval =
1070*a4128aadSBjoern A. Zeeb 			max_t(u32, dtim_interval,
1071*a4128aadSBjoern A. Zeeb 			      link_conf->dtim_period * link_conf->beacon_int);
1072*a4128aadSBjoern A. Zeeb 	}
1073*a4128aadSBjoern A. Zeeb 	rcu_read_unlock();
1074*a4128aadSBjoern A. Zeeb 
1075*a4128aadSBjoern A. Zeeb 	/*
1076*a4128aadSBjoern A. Zeeb 	 * If we are associated we want the delay time to be at least one
1077*a4128aadSBjoern A. Zeeb 	 * dtim interval so that the FW can wait until after the DTIM and
1078*a4128aadSBjoern A. Zeeb 	 * then start the time event, this will potentially allow us to
1079*a4128aadSBjoern A. Zeeb 	 * remain off-channel for the max duration.
1080*a4128aadSBjoern A. Zeeb 	 * Since we want to use almost a whole dtim interval we would also
1081*a4128aadSBjoern A. Zeeb 	 * like the delay to be for 2-3 dtim intervals, in case there are
1082*a4128aadSBjoern A. Zeeb 	 * other time events with higher priority.
1083*a4128aadSBjoern A. Zeeb 	 * dtim_interval should never be 0, it can be 1 if we don't know it
1084*a4128aadSBjoern A. Zeeb 	 * (we haven't heard any beacon yet).
1085*a4128aadSBjoern A. Zeeb 	 */
1086*a4128aadSBjoern A. Zeeb 	if (vif->cfg.assoc && !WARN_ON(!dtim_interval)) {
1087*a4128aadSBjoern A. Zeeb 		*delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
1088*a4128aadSBjoern A. Zeeb 		/* We cannot remain off-channel longer than the DTIM interval */
1089*a4128aadSBjoern A. Zeeb 		if (dtim_interval <= *duration_tu) {
1090*a4128aadSBjoern A. Zeeb 			*duration_tu = dtim_interval - AUX_ROC_SAFETY_BUFFER;
1091*a4128aadSBjoern A. Zeeb 			if (*duration_tu <= AUX_ROC_MIN_DURATION)
1092*a4128aadSBjoern A. Zeeb 				*duration_tu = dtim_interval -
1093*a4128aadSBjoern A. Zeeb 					AUX_ROC_MIN_SAFETY_BUFFER;
1094*a4128aadSBjoern A. Zeeb 		}
1095*a4128aadSBjoern A. Zeeb 	}
1096*a4128aadSBjoern A. Zeeb }
1097*a4128aadSBjoern A. Zeeb 
1098*a4128aadSBjoern A. Zeeb int iwl_mvm_roc_add_cmd(struct iwl_mvm *mvm,
1099*a4128aadSBjoern A. Zeeb 			struct ieee80211_channel *channel,
1100*a4128aadSBjoern A. Zeeb 			struct ieee80211_vif *vif,
1101*a4128aadSBjoern A. Zeeb 			int duration, enum iwl_roc_activity activity)
1102*a4128aadSBjoern A. Zeeb {
1103*a4128aadSBjoern A. Zeeb 	int res;
1104*a4128aadSBjoern A. Zeeb 	u32 duration_tu, delay;
1105*a4128aadSBjoern A. Zeeb 	struct iwl_roc_req roc_req = {
1106*a4128aadSBjoern A. Zeeb 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
1107*a4128aadSBjoern A. Zeeb 		.activity = cpu_to_le32(activity),
1108*a4128aadSBjoern A. Zeeb 		.sta_id = cpu_to_le32(mvm->aux_sta.sta_id),
1109*a4128aadSBjoern A. Zeeb 	};
1110*a4128aadSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1111*a4128aadSBjoern A. Zeeb 
1112*a4128aadSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
1113*a4128aadSBjoern A. Zeeb 
1114*a4128aadSBjoern A. Zeeb 	if (WARN_ON(mvmvif->roc_activity != ROC_NUM_ACTIVITIES))
1115*a4128aadSBjoern A. Zeeb 		return -EBUSY;
1116*a4128aadSBjoern A. Zeeb 
1117*a4128aadSBjoern A. Zeeb 	/* Set the channel info data */
1118*a4128aadSBjoern A. Zeeb 	iwl_mvm_set_chan_info(mvm, &roc_req.channel_info,
1119*a4128aadSBjoern A. Zeeb 			      channel->hw_value,
1120*a4128aadSBjoern A. Zeeb 			      iwl_mvm_phy_band_from_nl80211(channel->band),
1121*a4128aadSBjoern A. Zeeb 			      IWL_PHY_CHANNEL_MODE20, 0);
1122*a4128aadSBjoern A. Zeeb 
1123*a4128aadSBjoern A. Zeeb 	iwl_mvm_roc_duration_and_delay(vif, duration, &duration_tu,
1124*a4128aadSBjoern A. Zeeb 				       &delay);
1125*a4128aadSBjoern A. Zeeb 	roc_req.duration = cpu_to_le32(duration_tu);
1126*a4128aadSBjoern A. Zeeb 	roc_req.max_delay = cpu_to_le32(delay);
1127*a4128aadSBjoern A. Zeeb 
1128*a4128aadSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm,
1129*a4128aadSBjoern A. Zeeb 		     "\t(requested = %ums, max_delay = %ums)\n",
1130*a4128aadSBjoern A. Zeeb 		     duration, delay);
1131*a4128aadSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm,
1132*a4128aadSBjoern A. Zeeb 		     "Requesting to remain on channel %u for %utu. activity %u\n",
1133*a4128aadSBjoern A. Zeeb 		     channel->hw_value, duration_tu, activity);
1134*a4128aadSBjoern A. Zeeb 
1135*a4128aadSBjoern A. Zeeb 	/* Set the node address */
1136*a4128aadSBjoern A. Zeeb 	memcpy(roc_req.node_addr, vif->addr, ETH_ALEN);
1137*a4128aadSBjoern A. Zeeb 
1138*a4128aadSBjoern A. Zeeb 	res = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, ROC_CMD),
1139*a4128aadSBjoern A. Zeeb 				   0, sizeof(roc_req), &roc_req);
1140*a4128aadSBjoern A. Zeeb 	if (!res)
1141*a4128aadSBjoern A. Zeeb 		mvmvif->roc_activity = activity;
1142*a4128aadSBjoern A. Zeeb 
1143*a4128aadSBjoern A. Zeeb 	return res;
1144*a4128aadSBjoern A. Zeeb }
1145*a4128aadSBjoern A. Zeeb 
1146bfcc09ddSBjoern A. Zeeb static int
1147bfcc09ddSBjoern A. Zeeb iwl_mvm_start_p2p_roc_session_protection(struct iwl_mvm *mvm,
1148bfcc09ddSBjoern A. Zeeb 					 struct ieee80211_vif *vif,
1149bfcc09ddSBjoern A. Zeeb 					 int duration,
1150bfcc09ddSBjoern A. Zeeb 					 enum ieee80211_roc_type type)
1151bfcc09ddSBjoern A. Zeeb {
1152bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1153bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_session_prot_cmd cmd = {
1154bfcc09ddSBjoern A. Zeeb 		.id_and_color =
1155*a4128aadSBjoern A. Zeeb 			cpu_to_le32(iwl_mvm_get_session_prot_id(mvm, vif, 0)),
1156bfcc09ddSBjoern A. Zeeb 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
1157bfcc09ddSBjoern A. Zeeb 		.duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
1158bfcc09ddSBjoern A. Zeeb 	};
1159bfcc09ddSBjoern A. Zeeb 
1160bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
1161bfcc09ddSBjoern A. Zeeb 
1162bfcc09ddSBjoern A. Zeeb 	/* The time_event_data.id field is reused to save session
1163bfcc09ddSBjoern A. Zeeb 	 * protection's configuration.
1164bfcc09ddSBjoern A. Zeeb 	 */
1165*a4128aadSBjoern A. Zeeb 
1166*a4128aadSBjoern A. Zeeb 	mvmvif->time_event_data.link_id = 0;
1167*a4128aadSBjoern A. Zeeb 
1168bfcc09ddSBjoern A. Zeeb 	switch (type) {
1169bfcc09ddSBjoern A. Zeeb 	case IEEE80211_ROC_TYPE_NORMAL:
1170bfcc09ddSBjoern A. Zeeb 		mvmvif->time_event_data.id =
1171bfcc09ddSBjoern A. Zeeb 			SESSION_PROTECT_CONF_P2P_DEVICE_DISCOV;
1172bfcc09ddSBjoern A. Zeeb 		break;
1173bfcc09ddSBjoern A. Zeeb 	case IEEE80211_ROC_TYPE_MGMT_TX:
1174bfcc09ddSBjoern A. Zeeb 		mvmvif->time_event_data.id =
1175bfcc09ddSBjoern A. Zeeb 			SESSION_PROTECT_CONF_P2P_GO_NEGOTIATION;
1176bfcc09ddSBjoern A. Zeeb 		break;
1177bfcc09ddSBjoern A. Zeeb 	default:
1178bfcc09ddSBjoern A. Zeeb 		WARN_ONCE(1, "Got an invalid ROC type\n");
1179bfcc09ddSBjoern A. Zeeb 		return -EINVAL;
1180bfcc09ddSBjoern A. Zeeb 	}
1181bfcc09ddSBjoern A. Zeeb 
1182bfcc09ddSBjoern A. Zeeb 	cmd.conf_id = cpu_to_le32(mvmvif->time_event_data.id);
1183d9836fb4SBjoern A. Zeeb 	return iwl_mvm_send_cmd_pdu(mvm,
1184d9836fb4SBjoern A. Zeeb 				    WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1185bfcc09ddSBjoern A. Zeeb 				    0, sizeof(cmd), &cmd);
1186bfcc09ddSBjoern A. Zeeb }
1187bfcc09ddSBjoern A. Zeeb 
1188bfcc09ddSBjoern A. Zeeb int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1189bfcc09ddSBjoern A. Zeeb 			  int duration, enum ieee80211_roc_type type)
1190bfcc09ddSBjoern A. Zeeb {
1191bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1192bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1193bfcc09ddSBjoern A. Zeeb 	struct iwl_time_event_cmd time_cmd = {};
1194bfcc09ddSBjoern A. Zeeb 
1195bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
1196bfcc09ddSBjoern A. Zeeb 	if (te_data->running) {
1197bfcc09ddSBjoern A. Zeeb 		IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");
1198bfcc09ddSBjoern A. Zeeb 		return -EBUSY;
1199bfcc09ddSBjoern A. Zeeb 	}
1200bfcc09ddSBjoern A. Zeeb 
1201bfcc09ddSBjoern A. Zeeb 	if (fw_has_capa(&mvm->fw->ucode_capa,
1202bfcc09ddSBjoern A. Zeeb 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
1203bfcc09ddSBjoern A. Zeeb 		return iwl_mvm_start_p2p_roc_session_protection(mvm, vif,
1204bfcc09ddSBjoern A. Zeeb 								duration,
1205bfcc09ddSBjoern A. Zeeb 								type);
1206bfcc09ddSBjoern A. Zeeb 
1207bfcc09ddSBjoern A. Zeeb 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
1208bfcc09ddSBjoern A. Zeeb 	time_cmd.id_and_color =
1209bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
1210bfcc09ddSBjoern A. Zeeb 
1211bfcc09ddSBjoern A. Zeeb 	switch (type) {
1212bfcc09ddSBjoern A. Zeeb 	case IEEE80211_ROC_TYPE_NORMAL:
1213bfcc09ddSBjoern A. Zeeb 		time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);
1214bfcc09ddSBjoern A. Zeeb 		break;
1215bfcc09ddSBjoern A. Zeeb 	case IEEE80211_ROC_TYPE_MGMT_TX:
1216bfcc09ddSBjoern A. Zeeb 		time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);
1217bfcc09ddSBjoern A. Zeeb 		break;
1218bfcc09ddSBjoern A. Zeeb 	default:
1219bfcc09ddSBjoern A. Zeeb 		WARN_ONCE(1, "Got an invalid ROC type\n");
1220bfcc09ddSBjoern A. Zeeb 		return -EINVAL;
1221bfcc09ddSBjoern A. Zeeb 	}
1222bfcc09ddSBjoern A. Zeeb 
1223bfcc09ddSBjoern A. Zeeb 	time_cmd.apply_time = cpu_to_le32(0);
1224bfcc09ddSBjoern A. Zeeb 	time_cmd.interval = cpu_to_le32(1);
1225bfcc09ddSBjoern A. Zeeb 
1226bfcc09ddSBjoern A. Zeeb 	/*
1227bfcc09ddSBjoern A. Zeeb 	 * The P2P Device TEs can have lower priority than other events
1228bfcc09ddSBjoern A. Zeeb 	 * that are being scheduled by the driver/fw, and thus it might not be
1229bfcc09ddSBjoern A. Zeeb 	 * scheduled. To improve the chances of it being scheduled, allow them
1230bfcc09ddSBjoern A. Zeeb 	 * to be fragmented, and in addition allow them to be delayed.
1231bfcc09ddSBjoern A. Zeeb 	 */
1232bfcc09ddSBjoern A. Zeeb 	time_cmd.max_frags = min(MSEC_TO_TU(duration)/50, TE_V2_FRAG_ENDLESS);
1233bfcc09ddSBjoern A. Zeeb 	time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));
1234bfcc09ddSBjoern A. Zeeb 	time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));
1235bfcc09ddSBjoern A. Zeeb 	time_cmd.repeat = 1;
1236bfcc09ddSBjoern A. Zeeb 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
1237bfcc09ddSBjoern A. Zeeb 				      TE_V2_NOTIF_HOST_EVENT_END |
1238bfcc09ddSBjoern A. Zeeb 				      TE_V2_START_IMMEDIATELY);
1239bfcc09ddSBjoern A. Zeeb 
1240bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
1241bfcc09ddSBjoern A. Zeeb }
1242bfcc09ddSBjoern A. Zeeb 
1243bfcc09ddSBjoern A. Zeeb static struct iwl_mvm_time_event_data *iwl_mvm_get_roc_te(struct iwl_mvm *mvm)
1244bfcc09ddSBjoern A. Zeeb {
1245bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data;
1246bfcc09ddSBjoern A. Zeeb 
1247bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
1248bfcc09ddSBjoern A. Zeeb 
1249bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
1250bfcc09ddSBjoern A. Zeeb 
1251bfcc09ddSBjoern A. Zeeb 	/*
1252bfcc09ddSBjoern A. Zeeb 	 * Iterate over the list of time events and find the time event that is
1253bfcc09ddSBjoern A. Zeeb 	 * associated with a P2P_DEVICE interface.
1254bfcc09ddSBjoern A. Zeeb 	 * This assumes that a P2P_DEVICE interface can have only a single time
1255bfcc09ddSBjoern A. Zeeb 	 * event at any given time and this time event coresponds to a ROC
1256bfcc09ddSBjoern A. Zeeb 	 * request
1257bfcc09ddSBjoern A. Zeeb 	 */
1258bfcc09ddSBjoern A. Zeeb 	list_for_each_entry(te_data, &mvm->time_event_list, list) {
1259bfcc09ddSBjoern A. Zeeb 		if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE)
1260bfcc09ddSBjoern A. Zeeb 			goto out;
1261bfcc09ddSBjoern A. Zeeb 	}
1262bfcc09ddSBjoern A. Zeeb 
1263bfcc09ddSBjoern A. Zeeb 	/* There can only be at most one AUX ROC time event, we just use the
1264bfcc09ddSBjoern A. Zeeb 	 * list to simplify/unify code. Remove it if it exists.
1265bfcc09ddSBjoern A. Zeeb 	 */
1266bfcc09ddSBjoern A. Zeeb 	te_data = list_first_entry_or_null(&mvm->aux_roc_te_list,
1267bfcc09ddSBjoern A. Zeeb 					   struct iwl_mvm_time_event_data,
1268bfcc09ddSBjoern A. Zeeb 					   list);
1269bfcc09ddSBjoern A. Zeeb out:
1270bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
1271bfcc09ddSBjoern A. Zeeb 	return te_data;
1272bfcc09ddSBjoern A. Zeeb }
1273bfcc09ddSBjoern A. Zeeb 
1274bfcc09ddSBjoern A. Zeeb void iwl_mvm_cleanup_roc_te(struct iwl_mvm *mvm)
1275bfcc09ddSBjoern A. Zeeb {
1276bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data;
1277bfcc09ddSBjoern A. Zeeb 	u32 uid;
1278bfcc09ddSBjoern A. Zeeb 
1279bfcc09ddSBjoern A. Zeeb 	te_data = iwl_mvm_get_roc_te(mvm);
1280bfcc09ddSBjoern A. Zeeb 	if (te_data)
1281bfcc09ddSBjoern A. Zeeb 		__iwl_mvm_remove_time_event(mvm, te_data, &uid);
1282bfcc09ddSBjoern A. Zeeb }
1283bfcc09ddSBjoern A. Zeeb 
1284bfcc09ddSBjoern A. Zeeb void iwl_mvm_stop_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1285bfcc09ddSBjoern A. Zeeb {
1286*a4128aadSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1287bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data;
1288*a4128aadSBjoern A. Zeeb 	bool p2p_aux = iwl_mvm_has_p2p_over_aux(mvm);
1289*a4128aadSBjoern A. Zeeb 	u8 roc_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1290*a4128aadSBjoern A. Zeeb 					   WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0);
1291*a4128aadSBjoern A. Zeeb 	int iftype = vif->type;
1292bfcc09ddSBjoern A. Zeeb 
1293*a4128aadSBjoern A. Zeeb 	mutex_lock(&mvm->mutex);
1294*a4128aadSBjoern A. Zeeb 
1295*a4128aadSBjoern A. Zeeb 	if (p2p_aux || (roc_ver >= 3 && iftype != NL80211_IFTYPE_P2P_DEVICE)) {
1296*a4128aadSBjoern A. Zeeb 		if (mvmvif->roc_activity < ROC_NUM_ACTIVITIES) {
1297*a4128aadSBjoern A. Zeeb 			iwl_mvm_roc_rm_cmd(mvm, mvmvif->roc_activity);
1298*a4128aadSBjoern A. Zeeb 			mvmvif->roc_activity = ROC_NUM_ACTIVITIES;
1299*a4128aadSBjoern A. Zeeb 		}
1300*a4128aadSBjoern A. Zeeb 		goto cleanup_roc;
1301*a4128aadSBjoern A. Zeeb 	} else if (fw_has_capa(&mvm->fw->ucode_capa,
1302bfcc09ddSBjoern A. Zeeb 			       IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
1303*a4128aadSBjoern A. Zeeb 		te_data = &mvmvif->time_event_data;
1304bfcc09ddSBjoern A. Zeeb 
1305*a4128aadSBjoern A. Zeeb 		if (iftype == NL80211_IFTYPE_P2P_DEVICE) {
1306*a4128aadSBjoern A. Zeeb 			if (te_data->id >= SESSION_PROTECT_CONF_MAX_ID) {
1307*a4128aadSBjoern A. Zeeb 				IWL_DEBUG_TE(mvm,
1308*a4128aadSBjoern A. Zeeb 					     "No remain on channel event\n");
1309*a4128aadSBjoern A. Zeeb 				mutex_unlock(&mvm->mutex);
1310*a4128aadSBjoern A. Zeeb 				return;
1311*a4128aadSBjoern A. Zeeb 			}
1312*a4128aadSBjoern A. Zeeb 			iwl_mvm_cancel_session_protection(mvm, vif,
1313*a4128aadSBjoern A. Zeeb 							  te_data->id,
1314*a4128aadSBjoern A. Zeeb 							  te_data->link_id);
1315bfcc09ddSBjoern A. Zeeb 		} else {
1316bfcc09ddSBjoern A. Zeeb 			iwl_mvm_remove_aux_roc_te(mvm, mvmvif,
1317d9836fb4SBjoern A. Zeeb 						  &mvmvif->hs_time_event_data);
1318bfcc09ddSBjoern A. Zeeb 		}
1319*a4128aadSBjoern A. Zeeb 		goto cleanup_roc;
1320bfcc09ddSBjoern A. Zeeb 	}
1321bfcc09ddSBjoern A. Zeeb 
1322bfcc09ddSBjoern A. Zeeb 	te_data = iwl_mvm_get_roc_te(mvm);
1323bfcc09ddSBjoern A. Zeeb 	if (!te_data) {
1324bfcc09ddSBjoern A. Zeeb 		IWL_WARN(mvm, "No remain on channel event\n");
1325*a4128aadSBjoern A. Zeeb 		mutex_unlock(&mvm->mutex);
1326bfcc09ddSBjoern A. Zeeb 		return;
1327bfcc09ddSBjoern A. Zeeb 	}
1328bfcc09ddSBjoern A. Zeeb 
1329bfcc09ddSBjoern A. Zeeb 	mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
1330*a4128aadSBjoern A. Zeeb 	iftype = te_data->vif->type;
1331*a4128aadSBjoern A. Zeeb 	if (iftype == NL80211_IFTYPE_P2P_DEVICE)
1332bfcc09ddSBjoern A. Zeeb 		iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1333*a4128aadSBjoern A. Zeeb 	else
1334bfcc09ddSBjoern A. Zeeb 		iwl_mvm_remove_aux_roc_te(mvm, mvmvif, te_data);
1335*a4128aadSBjoern A. Zeeb 
1336*a4128aadSBjoern A. Zeeb cleanup_roc:
1337*a4128aadSBjoern A. Zeeb 	/*
1338*a4128aadSBjoern A. Zeeb 	 * In case we get here before the ROC event started,
1339*a4128aadSBjoern A. Zeeb 	 * (so the status bit isn't set) set it here so iwl_mvm_cleanup_roc will
1340*a4128aadSBjoern A. Zeeb 	 * cleanup things properly
1341*a4128aadSBjoern A. Zeeb 	 */
1342*a4128aadSBjoern A. Zeeb 	if (p2p_aux || iftype != NL80211_IFTYPE_P2P_DEVICE)
1343*a4128aadSBjoern A. Zeeb 		set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
1344*a4128aadSBjoern A. Zeeb 	else
1345*a4128aadSBjoern A. Zeeb 		set_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status);
1346*a4128aadSBjoern A. Zeeb 
1347*a4128aadSBjoern A. Zeeb 	/* Mutex is released inside this function */
1348*a4128aadSBjoern A. Zeeb 	iwl_mvm_cleanup_roc(mvm);
1349bfcc09ddSBjoern A. Zeeb }
1350bfcc09ddSBjoern A. Zeeb 
1351bfcc09ddSBjoern A. Zeeb void iwl_mvm_remove_csa_period(struct iwl_mvm *mvm,
1352bfcc09ddSBjoern A. Zeeb 			       struct ieee80211_vif *vif)
1353bfcc09ddSBjoern A. Zeeb {
1354bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1355bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1356bfcc09ddSBjoern A. Zeeb 	u32 id;
1357bfcc09ddSBjoern A. Zeeb 
1358bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
1359bfcc09ddSBjoern A. Zeeb 
1360bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
1361bfcc09ddSBjoern A. Zeeb 	id = te_data->id;
1362bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
1363bfcc09ddSBjoern A. Zeeb 
1364bfcc09ddSBjoern A. Zeeb 	if (id != TE_CHANNEL_SWITCH_PERIOD)
1365bfcc09ddSBjoern A. Zeeb 		return;
1366bfcc09ddSBjoern A. Zeeb 
1367bfcc09ddSBjoern A. Zeeb 	iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1368bfcc09ddSBjoern A. Zeeb }
1369bfcc09ddSBjoern A. Zeeb 
1370bfcc09ddSBjoern A. Zeeb int iwl_mvm_schedule_csa_period(struct iwl_mvm *mvm,
1371bfcc09ddSBjoern A. Zeeb 				struct ieee80211_vif *vif,
1372bfcc09ddSBjoern A. Zeeb 				u32 duration, u32 apply_time)
1373bfcc09ddSBjoern A. Zeeb {
1374bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1375bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1376bfcc09ddSBjoern A. Zeeb 	struct iwl_time_event_cmd time_cmd = {};
1377bfcc09ddSBjoern A. Zeeb 
1378bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
1379bfcc09ddSBjoern A. Zeeb 
1380bfcc09ddSBjoern A. Zeeb 	if (te_data->running) {
1381bfcc09ddSBjoern A. Zeeb 		u32 id;
1382bfcc09ddSBjoern A. Zeeb 
1383bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&mvm->time_event_lock);
1384bfcc09ddSBjoern A. Zeeb 		id = te_data->id;
1385bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&mvm->time_event_lock);
1386bfcc09ddSBjoern A. Zeeb 
1387bfcc09ddSBjoern A. Zeeb 		if (id == TE_CHANNEL_SWITCH_PERIOD) {
1388bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_TE(mvm, "CS period is already scheduled\n");
1389bfcc09ddSBjoern A. Zeeb 			return -EBUSY;
1390bfcc09ddSBjoern A. Zeeb 		}
1391bfcc09ddSBjoern A. Zeeb 
1392bfcc09ddSBjoern A. Zeeb 		/*
1393bfcc09ddSBjoern A. Zeeb 		 * Remove the session protection time event to allow the
1394bfcc09ddSBjoern A. Zeeb 		 * channel switch. If we got here, we just heard a beacon so
1395bfcc09ddSBjoern A. Zeeb 		 * the session protection is not needed anymore anyway.
1396bfcc09ddSBjoern A. Zeeb 		 */
1397bfcc09ddSBjoern A. Zeeb 		iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1398bfcc09ddSBjoern A. Zeeb 	}
1399bfcc09ddSBjoern A. Zeeb 
1400bfcc09ddSBjoern A. Zeeb 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
1401bfcc09ddSBjoern A. Zeeb 	time_cmd.id_and_color =
1402bfcc09ddSBjoern A. Zeeb 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
1403bfcc09ddSBjoern A. Zeeb 	time_cmd.id = cpu_to_le32(TE_CHANNEL_SWITCH_PERIOD);
1404bfcc09ddSBjoern A. Zeeb 	time_cmd.apply_time = cpu_to_le32(apply_time);
1405bfcc09ddSBjoern A. Zeeb 	time_cmd.max_frags = TE_V2_FRAG_NONE;
1406bfcc09ddSBjoern A. Zeeb 	time_cmd.duration = cpu_to_le32(duration);
1407bfcc09ddSBjoern A. Zeeb 	time_cmd.repeat = 1;
1408bfcc09ddSBjoern A. Zeeb 	time_cmd.interval = cpu_to_le32(1);
1409bfcc09ddSBjoern A. Zeeb 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
1410bfcc09ddSBjoern A. Zeeb 				      TE_V2_ABSENCE);
1411bfcc09ddSBjoern A. Zeeb 	if (!apply_time)
1412bfcc09ddSBjoern A. Zeeb 		time_cmd.policy |= cpu_to_le16(TE_V2_START_IMMEDIATELY);
1413bfcc09ddSBjoern A. Zeeb 
1414bfcc09ddSBjoern A. Zeeb 	return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
1415bfcc09ddSBjoern A. Zeeb }
1416bfcc09ddSBjoern A. Zeeb 
1417bfcc09ddSBjoern A. Zeeb static bool iwl_mvm_session_prot_notif(struct iwl_notif_wait_data *notif_wait,
1418bfcc09ddSBjoern A. Zeeb 				       struct iwl_rx_packet *pkt, void *data)
1419bfcc09ddSBjoern A. Zeeb {
1420bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm *mvm =
1421bfcc09ddSBjoern A. Zeeb 		container_of(notif_wait, struct iwl_mvm, notif_wait);
1422bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_session_prot_notif *resp;
1423bfcc09ddSBjoern A. Zeeb 	int resp_len = iwl_rx_packet_payload_len(pkt);
1424bfcc09ddSBjoern A. Zeeb 
1425bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(pkt->hdr.cmd != SESSION_PROTECTION_NOTIF ||
1426bfcc09ddSBjoern A. Zeeb 		    pkt->hdr.group_id != MAC_CONF_GROUP))
1427bfcc09ddSBjoern A. Zeeb 		return true;
1428bfcc09ddSBjoern A. Zeeb 
1429bfcc09ddSBjoern A. Zeeb 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
1430bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm, "Invalid SESSION_PROTECTION_NOTIF response\n");
1431bfcc09ddSBjoern A. Zeeb 		return true;
1432bfcc09ddSBjoern A. Zeeb 	}
1433bfcc09ddSBjoern A. Zeeb 
1434bfcc09ddSBjoern A. Zeeb 	resp = (void *)pkt->data;
1435bfcc09ddSBjoern A. Zeeb 
1436bfcc09ddSBjoern A. Zeeb 	if (!resp->status)
1437bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm,
1438bfcc09ddSBjoern A. Zeeb 			"TIME_EVENT_NOTIFICATION received but not executed\n");
1439bfcc09ddSBjoern A. Zeeb 
1440bfcc09ddSBjoern A. Zeeb 	return true;
1441bfcc09ddSBjoern A. Zeeb }
1442bfcc09ddSBjoern A. Zeeb 
1443bfcc09ddSBjoern A. Zeeb void iwl_mvm_schedule_session_protection(struct iwl_mvm *mvm,
1444bfcc09ddSBjoern A. Zeeb 					 struct ieee80211_vif *vif,
1445bfcc09ddSBjoern A. Zeeb 					 u32 duration, u32 min_duration,
1446*a4128aadSBjoern A. Zeeb 					 bool wait_for_notif,
1447*a4128aadSBjoern A. Zeeb 					 unsigned int link_id)
1448bfcc09ddSBjoern A. Zeeb {
1449bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1450bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1451d9836fb4SBjoern A. Zeeb 	const u16 notif[] = { WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_NOTIF) };
1452bfcc09ddSBjoern A. Zeeb 	struct iwl_notification_wait wait_notif;
1453*a4128aadSBjoern A. Zeeb 	int mac_link_id = iwl_mvm_get_session_prot_id(mvm, vif, (s8)link_id);
1454bfcc09ddSBjoern A. Zeeb 	struct iwl_mvm_session_prot_cmd cmd = {
1455*a4128aadSBjoern A. Zeeb 		.id_and_color = cpu_to_le32(mac_link_id),
1456bfcc09ddSBjoern A. Zeeb 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
14578f06a2b5SBjoern A. Zeeb 		.conf_id = cpu_to_le32(SESSION_PROTECT_CONF_ASSOC),
1458bfcc09ddSBjoern A. Zeeb 		.duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
1459bfcc09ddSBjoern A. Zeeb 	};
1460bfcc09ddSBjoern A. Zeeb 
1461*a4128aadSBjoern A. Zeeb 	if (mac_link_id < 0)
1462*a4128aadSBjoern A. Zeeb 		return;
1463*a4128aadSBjoern A. Zeeb 
1464bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&mvm->mutex);
1465bfcc09ddSBjoern A. Zeeb 
1466bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
1467*a4128aadSBjoern A. Zeeb 	if (te_data->running && te_data->link_id == link_id &&
1468bfcc09ddSBjoern A. Zeeb 	    time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
1469bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
1470bfcc09ddSBjoern A. Zeeb 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
1471bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&mvm->time_event_lock);
1472bfcc09ddSBjoern A. Zeeb 
1473bfcc09ddSBjoern A. Zeeb 		return;
1474bfcc09ddSBjoern A. Zeeb 	}
1475bfcc09ddSBjoern A. Zeeb 
1476bfcc09ddSBjoern A. Zeeb 	iwl_mvm_te_clear_data(mvm, te_data);
14778f06a2b5SBjoern A. Zeeb 	/*
14788f06a2b5SBjoern A. Zeeb 	 * The time_event_data.id field is reused to save session
14798f06a2b5SBjoern A. Zeeb 	 * protection's configuration.
14808f06a2b5SBjoern A. Zeeb 	 */
14818f06a2b5SBjoern A. Zeeb 	te_data->id = le32_to_cpu(cmd.conf_id);
1482bfcc09ddSBjoern A. Zeeb 	te_data->duration = le32_to_cpu(cmd.duration_tu);
1483bfcc09ddSBjoern A. Zeeb 	te_data->vif = vif;
1484*a4128aadSBjoern A. Zeeb 	te_data->link_id = link_id;
1485bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
1486bfcc09ddSBjoern A. Zeeb 
1487bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TE(mvm, "Add new session protection, duration %d TU\n",
1488bfcc09ddSBjoern A. Zeeb 		     le32_to_cpu(cmd.duration_tu));
1489bfcc09ddSBjoern A. Zeeb 
1490bfcc09ddSBjoern A. Zeeb 	if (!wait_for_notif) {
1491bfcc09ddSBjoern A. Zeeb 		if (iwl_mvm_send_cmd_pdu(mvm,
1492d9836fb4SBjoern A. Zeeb 					 WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1493bfcc09ddSBjoern A. Zeeb 					 0, sizeof(cmd), &cmd)) {
1494*a4128aadSBjoern A. Zeeb 			goto send_cmd_err;
1495bfcc09ddSBjoern A. Zeeb 		}
1496bfcc09ddSBjoern A. Zeeb 
1497bfcc09ddSBjoern A. Zeeb 		return;
1498bfcc09ddSBjoern A. Zeeb 	}
1499bfcc09ddSBjoern A. Zeeb 
1500bfcc09ddSBjoern A. Zeeb 	iwl_init_notification_wait(&mvm->notif_wait, &wait_notif,
1501bfcc09ddSBjoern A. Zeeb 				   notif, ARRAY_SIZE(notif),
1502bfcc09ddSBjoern A. Zeeb 				   iwl_mvm_session_prot_notif, NULL);
1503bfcc09ddSBjoern A. Zeeb 
1504bfcc09ddSBjoern A. Zeeb 	if (iwl_mvm_send_cmd_pdu(mvm,
1505d9836fb4SBjoern A. Zeeb 				 WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1506bfcc09ddSBjoern A. Zeeb 				 0, sizeof(cmd), &cmd)) {
1507bfcc09ddSBjoern A. Zeeb 		iwl_remove_notification(&mvm->notif_wait, &wait_notif);
1508*a4128aadSBjoern A. Zeeb 		goto send_cmd_err;
1509bfcc09ddSBjoern A. Zeeb 	} else if (iwl_wait_notification(&mvm->notif_wait, &wait_notif,
1510bfcc09ddSBjoern A. Zeeb 					 TU_TO_JIFFIES(100))) {
1511bfcc09ddSBjoern A. Zeeb 		IWL_ERR(mvm,
1512bfcc09ddSBjoern A. Zeeb 			"Failed to protect session until session protection\n");
1513bfcc09ddSBjoern A. Zeeb 	}
1514*a4128aadSBjoern A. Zeeb 	return;
1515*a4128aadSBjoern A. Zeeb 
1516*a4128aadSBjoern A. Zeeb send_cmd_err:
1517*a4128aadSBjoern A. Zeeb 	IWL_ERR(mvm,
1518*a4128aadSBjoern A. Zeeb 		"Couldn't send the SESSION_PROTECTION_CMD\n");
1519*a4128aadSBjoern A. Zeeb 	spin_lock_bh(&mvm->time_event_lock);
1520*a4128aadSBjoern A. Zeeb 	iwl_mvm_te_clear_data(mvm, te_data);
1521*a4128aadSBjoern A. Zeeb 	spin_unlock_bh(&mvm->time_event_lock);
1522bfcc09ddSBjoern A. Zeeb }
1523