xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/time-event.c (revision 8552a434b6a05cc38006733afe6a239ad4d600a2)
1e705c121SKalle Valo /******************************************************************************
2e705c121SKalle Valo  *
3e705c121SKalle Valo  * This file is provided under a dual BSD/GPLv2 license.  When using or
4e705c121SKalle Valo  * redistributing this file, you may do so under either license.
5e705c121SKalle Valo  *
6e705c121SKalle Valo  * GPL LICENSE SUMMARY
7e705c121SKalle Valo  *
8e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
103edfb5f4SAvraham Stern  * Copyright(c) 2017 Intel Deutschland GmbH
11f6780614SSara Sharon  * Copyright(c) 2018 - 2019 Intel Corporation
12e705c121SKalle Valo  *
13e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
14e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
15e705c121SKalle Valo  * published by the Free Software Foundation.
16e705c121SKalle Valo  *
17e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
18e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
19e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20e705c121SKalle Valo  * General Public License for more details.
21e705c121SKalle Valo  *
22e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
23e705c121SKalle Valo  * in the file called COPYING.
24e705c121SKalle Valo  *
25e705c121SKalle Valo  * Contact Information:
26d01c5366SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
27e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28e705c121SKalle Valo  *
29e705c121SKalle Valo  * BSD LICENSE
30e705c121SKalle Valo  *
31e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
333edfb5f4SAvraham Stern  * Copyright(c) 2017 Intel Deutschland GmbH
34f6780614SSara Sharon  * Copyright(c) 2018 - 2019 Intel Corporation
35e705c121SKalle Valo  * All rights reserved.
36e705c121SKalle Valo  *
37e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
38e705c121SKalle Valo  * modification, are permitted provided that the following conditions
39e705c121SKalle Valo  * are met:
40e705c121SKalle Valo  *
41e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
42e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
43e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
44e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
45e705c121SKalle Valo  *    the documentation and/or other materials provided with the
46e705c121SKalle Valo  *    distribution.
47e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
48e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
49e705c121SKalle Valo  *    from this software without specific prior written permission.
50e705c121SKalle Valo  *
51e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62e705c121SKalle Valo  *
63e705c121SKalle Valo  *****************************************************************************/
64e705c121SKalle Valo 
65e705c121SKalle Valo #include <linux/jiffies.h>
66e705c121SKalle Valo #include <net/mac80211.h>
67e705c121SKalle Valo 
689fca9d5cSJohannes Berg #include "fw/notif-wait.h"
69e705c121SKalle Valo #include "iwl-trans.h"
70e705c121SKalle Valo #include "fw-api.h"
71e705c121SKalle Valo #include "time-event.h"
72e705c121SKalle Valo #include "mvm.h"
73e705c121SKalle Valo #include "iwl-io.h"
74e705c121SKalle Valo #include "iwl-prph.h"
75e705c121SKalle Valo 
76e705c121SKalle Valo /*
77e705c121SKalle Valo  * For the high priority TE use a time event type that has similar priority to
78e705c121SKalle Valo  * the FW's action scan priority.
79e705c121SKalle Valo  */
80e705c121SKalle Valo #define IWL_MVM_ROC_TE_TYPE_NORMAL TE_P2P_DEVICE_DISCOVERABLE
81e705c121SKalle Valo #define IWL_MVM_ROC_TE_TYPE_MGMT_TX TE_P2P_CLIENT_ASSOC
82e705c121SKalle Valo 
83e705c121SKalle Valo void iwl_mvm_te_clear_data(struct iwl_mvm *mvm,
84e705c121SKalle Valo 			   struct iwl_mvm_time_event_data *te_data)
85e705c121SKalle Valo {
86e705c121SKalle Valo 	lockdep_assert_held(&mvm->time_event_lock);
87e705c121SKalle Valo 
88cee859feSAvraham Stern 	if (!te_data || !te_data->vif)
89e705c121SKalle Valo 		return;
90e705c121SKalle Valo 
91e705c121SKalle Valo 	list_del(&te_data->list);
92e705c121SKalle Valo 	te_data->running = false;
93e705c121SKalle Valo 	te_data->uid = 0;
94e705c121SKalle Valo 	te_data->id = TE_MAX;
95e705c121SKalle Valo 	te_data->vif = NULL;
96e705c121SKalle Valo }
97e705c121SKalle Valo 
98e705c121SKalle Valo void iwl_mvm_roc_done_wk(struct work_struct *wk)
99e705c121SKalle Valo {
100e705c121SKalle Valo 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, roc_done_wk);
101e705c121SKalle Valo 
102e705c121SKalle Valo 	/*
103e705c121SKalle Valo 	 * Clear the ROC_RUNNING /ROC_AUX_RUNNING status bit.
104e705c121SKalle Valo 	 * This will cause the TX path to drop offchannel transmissions.
105e705c121SKalle Valo 	 * That would also be done by mac80211, but it is racy, in particular
106e705c121SKalle Valo 	 * in the case that the time event actually completed in the firmware
107e705c121SKalle Valo 	 * (which is handled in iwl_mvm_te_handle_notif).
108e705c121SKalle Valo 	 */
10958d3bef4SEmmanuel Grumbach 	clear_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
11058d3bef4SEmmanuel Grumbach 	clear_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
111e705c121SKalle Valo 
112e705c121SKalle Valo 	synchronize_net();
113e705c121SKalle Valo 
114e705c121SKalle Valo 	/*
115e705c121SKalle Valo 	 * Flush the offchannel queue -- this is called when the time
116e705c121SKalle Valo 	 * event finishes or is canceled, so that frames queued for it
117e705c121SKalle Valo 	 * won't get stuck on the queue and be transmitted in the next
118e705c121SKalle Valo 	 * time event.
119e705c121SKalle Valo 	 * We have to send the command asynchronously since this cannot
120e705c121SKalle Valo 	 * be under the mutex for locking reasons, but that's not an
121e705c121SKalle Valo 	 * issue as it will have to complete before the next command is
122e705c121SKalle Valo 	 * executed, and a new time event means a new command.
123e705c121SKalle Valo 	 */
124d49394a1SSara Sharon 	iwl_mvm_flush_sta(mvm, &mvm->aux_sta, true, CMD_ASYNC);
1256c2d49fdSJohannes Berg 
1266c2d49fdSJohannes Berg 	/* Do the same for the P2P device queue (STA) */
1276c2d49fdSJohannes Berg 	if (test_and_clear_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status)) {
1286c2d49fdSJohannes Berg 		struct iwl_mvm_vif *mvmvif;
1296c2d49fdSJohannes Berg 
1306c2d49fdSJohannes Berg 		/*
1316c2d49fdSJohannes Berg 		 * NB: access to this pointer would be racy, but the flush bit
1326c2d49fdSJohannes Berg 		 * can only be set when we had a P2P-Device VIF, and we have a
1336c2d49fdSJohannes Berg 		 * flush of this work in iwl_mvm_prepare_mac_removal() so it's
1346c2d49fdSJohannes Berg 		 * not really racy.
1356c2d49fdSJohannes Berg 		 */
1366c2d49fdSJohannes Berg 
1376c2d49fdSJohannes Berg 		if (!WARN_ON(!mvm->p2p_device_vif)) {
1386c2d49fdSJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(mvm->p2p_device_vif);
1396c2d49fdSJohannes Berg 			iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true,
1406c2d49fdSJohannes Berg 					  CMD_ASYNC);
1416c2d49fdSJohannes Berg 		}
1426c2d49fdSJohannes Berg 	}
143e705c121SKalle Valo }
144e705c121SKalle Valo 
145e705c121SKalle Valo static void iwl_mvm_roc_finished(struct iwl_mvm *mvm)
146e705c121SKalle Valo {
147e705c121SKalle Valo 	/*
148e705c121SKalle Valo 	 * Of course, our status bit is just as racy as mac80211, so in
149e705c121SKalle Valo 	 * addition, fire off the work struct which will drop all frames
150e705c121SKalle Valo 	 * from the hardware queues that made it through the race. First
151e705c121SKalle Valo 	 * it will of course synchronize the TX path to make sure that
152e705c121SKalle Valo 	 * any *new* TX will be rejected.
153e705c121SKalle Valo 	 */
154e705c121SKalle Valo 	schedule_work(&mvm->roc_done_wk);
155e705c121SKalle Valo }
156e705c121SKalle Valo 
157e705c121SKalle Valo static void iwl_mvm_csa_noa_start(struct iwl_mvm *mvm)
158e705c121SKalle Valo {
159e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
160e705c121SKalle Valo 
161e705c121SKalle Valo 	rcu_read_lock();
162e705c121SKalle Valo 
163e705c121SKalle Valo 	csa_vif = rcu_dereference(mvm->csa_vif);
164e705c121SKalle Valo 	if (!csa_vif || !csa_vif->csa_active)
165e705c121SKalle Valo 		goto out_unlock;
166e705c121SKalle Valo 
167e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "CSA NOA started\n");
168e705c121SKalle Valo 
169e705c121SKalle Valo 	/*
170e705c121SKalle Valo 	 * CSA NoA is started but we still have beacons to
171e705c121SKalle Valo 	 * transmit on the current channel.
172e705c121SKalle Valo 	 * So we just do nothing here and the switch
173e705c121SKalle Valo 	 * will be performed on the last TBTT.
174e705c121SKalle Valo 	 */
175*8552a434SJohn Crispin 	if (!ieee80211_beacon_cntdwn_is_complete(csa_vif)) {
176e705c121SKalle Valo 		IWL_WARN(mvm, "CSA NOA started too early\n");
177e705c121SKalle Valo 		goto out_unlock;
178e705c121SKalle Valo 	}
179e705c121SKalle Valo 
180e705c121SKalle Valo 	ieee80211_csa_finish(csa_vif);
181e705c121SKalle Valo 
182e705c121SKalle Valo 	rcu_read_unlock();
183e705c121SKalle Valo 
184e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->csa_vif, NULL);
185e705c121SKalle Valo 
186e705c121SKalle Valo 	return;
187e705c121SKalle Valo 
188e705c121SKalle Valo out_unlock:
189e705c121SKalle Valo 	rcu_read_unlock();
190e705c121SKalle Valo }
191e705c121SKalle Valo 
192e705c121SKalle Valo static bool iwl_mvm_te_check_disconnect(struct iwl_mvm *mvm,
193e705c121SKalle Valo 					struct ieee80211_vif *vif,
194e705c121SKalle Valo 					const char *errmsg)
195e705c121SKalle Valo {
19619125cb0SAndrei Otcheretianski 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
19719125cb0SAndrei Otcheretianski 
198e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION)
199e705c121SKalle Valo 		return false;
20019125cb0SAndrei Otcheretianski 
20119125cb0SAndrei Otcheretianski 	if (!mvmvif->csa_bcn_pending && vif->bss_conf.assoc &&
20219125cb0SAndrei Otcheretianski 	    vif->bss_conf.dtim_period)
203e705c121SKalle Valo 		return false;
204e705c121SKalle Valo 	if (errmsg)
205e705c121SKalle Valo 		IWL_ERR(mvm, "%s\n", errmsg);
206e705c121SKalle Valo 
207e705c121SKalle Valo 	iwl_mvm_connection_loss(mvm, vif, errmsg);
208e705c121SKalle Valo 	return true;
209e705c121SKalle Valo }
210e705c121SKalle Valo 
211e705c121SKalle Valo static void
212e705c121SKalle Valo iwl_mvm_te_handle_notify_csa(struct iwl_mvm *mvm,
213e705c121SKalle Valo 			     struct iwl_mvm_time_event_data *te_data,
214e705c121SKalle Valo 			     struct iwl_time_event_notif *notif)
215e705c121SKalle Valo {
216e705c121SKalle Valo 	struct ieee80211_vif *vif = te_data->vif;
217e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
218e705c121SKalle Valo 
219e705c121SKalle Valo 	if (!notif->status)
220e705c121SKalle Valo 		IWL_DEBUG_TE(mvm, "CSA time event failed to start\n");
221e705c121SKalle Valo 
222e705c121SKalle Valo 	switch (te_data->vif->type) {
223e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
224e705c121SKalle Valo 		if (!notif->status)
225e705c121SKalle Valo 			mvmvif->csa_failed = true;
226e705c121SKalle Valo 		iwl_mvm_csa_noa_start(mvm);
227e705c121SKalle Valo 		break;
228e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
229e705c121SKalle Valo 		if (!notif->status) {
230e705c121SKalle Valo 			iwl_mvm_connection_loss(mvm, vif,
231e705c121SKalle Valo 						"CSA TE failed to start");
232e705c121SKalle Valo 			break;
233e705c121SKalle Valo 		}
234e705c121SKalle Valo 		iwl_mvm_csa_client_absent(mvm, te_data->vif);
23511af74adSAndrei Otcheretianski 		cancel_delayed_work(&mvmvif->csa_work);
236e705c121SKalle Valo 		ieee80211_chswitch_done(te_data->vif, true);
237e705c121SKalle Valo 		break;
238e705c121SKalle Valo 	default:
239e705c121SKalle Valo 		/* should never happen */
240e705c121SKalle Valo 		WARN_ON_ONCE(1);
241e705c121SKalle Valo 		break;
242e705c121SKalle Valo 	}
243e705c121SKalle Valo 
244e705c121SKalle Valo 	/* we don't need it anymore */
245e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, te_data);
246e705c121SKalle Valo }
247e705c121SKalle Valo 
248e705c121SKalle Valo static void iwl_mvm_te_check_trigger(struct iwl_mvm *mvm,
249e705c121SKalle Valo 				     struct iwl_time_event_notif *notif,
250e705c121SKalle Valo 				     struct iwl_mvm_time_event_data *te_data)
251e705c121SKalle Valo {
252e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
253e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_time_event *te_trig;
254e705c121SKalle Valo 	int i;
255e705c121SKalle Valo 
2566c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
2577174beb6SJohannes Berg 				     ieee80211_vif_to_wdev(te_data->vif),
2586c042d75SSara Sharon 				     FW_DBG_TRIGGER_TIME_EVENT);
2596c042d75SSara Sharon 	if (!trig)
260e705c121SKalle Valo 		return;
261e705c121SKalle Valo 
2626c042d75SSara Sharon 	te_trig = (void *)trig->data;
2636c042d75SSara Sharon 
264e705c121SKalle Valo 	for (i = 0; i < ARRAY_SIZE(te_trig->time_events); i++) {
265e705c121SKalle Valo 		u32 trig_te_id = le32_to_cpu(te_trig->time_events[i].id);
266e705c121SKalle Valo 		u32 trig_action_bitmap =
267e705c121SKalle Valo 			le32_to_cpu(te_trig->time_events[i].action_bitmap);
268e705c121SKalle Valo 		u32 trig_status_bitmap =
269e705c121SKalle Valo 			le32_to_cpu(te_trig->time_events[i].status_bitmap);
270e705c121SKalle Valo 
271e705c121SKalle Valo 		if (trig_te_id != te_data->id ||
272e705c121SKalle Valo 		    !(trig_action_bitmap & le32_to_cpu(notif->action)) ||
273e705c121SKalle Valo 		    !(trig_status_bitmap & BIT(le32_to_cpu(notif->status))))
274e705c121SKalle Valo 			continue;
275e705c121SKalle Valo 
2767174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
277e705c121SKalle Valo 					"Time event %d Action 0x%x received status: %d",
278e705c121SKalle Valo 					te_data->id,
279e705c121SKalle Valo 					le32_to_cpu(notif->action),
280e705c121SKalle Valo 					le32_to_cpu(notif->status));
281e705c121SKalle Valo 		break;
282e705c121SKalle Valo 	}
283e705c121SKalle Valo }
284e705c121SKalle Valo 
285e705c121SKalle Valo /*
286e705c121SKalle Valo  * Handles a FW notification for an event that is known to the driver.
287e705c121SKalle Valo  *
288e705c121SKalle Valo  * @mvm: the mvm component
289e705c121SKalle Valo  * @te_data: the time event data
290e705c121SKalle Valo  * @notif: the notification data corresponding the time event data.
291e705c121SKalle Valo  */
292e705c121SKalle Valo static void iwl_mvm_te_handle_notif(struct iwl_mvm *mvm,
293e705c121SKalle Valo 				    struct iwl_mvm_time_event_data *te_data,
294e705c121SKalle Valo 				    struct iwl_time_event_notif *notif)
295e705c121SKalle Valo {
296e705c121SKalle Valo 	lockdep_assert_held(&mvm->time_event_lock);
297e705c121SKalle Valo 
298e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "Handle time event notif - UID = 0x%x action %d\n",
299e705c121SKalle Valo 		     le32_to_cpu(notif->unique_id),
300e705c121SKalle Valo 		     le32_to_cpu(notif->action));
301e705c121SKalle Valo 
302e705c121SKalle Valo 	iwl_mvm_te_check_trigger(mvm, notif, te_data);
303e705c121SKalle Valo 
304e705c121SKalle Valo 	/*
305e705c121SKalle Valo 	 * The FW sends the start/end time event notifications even for events
306e705c121SKalle Valo 	 * that it fails to schedule. This is indicated in the status field of
307e705c121SKalle Valo 	 * the notification. This happens in cases that the scheduler cannot
308e705c121SKalle Valo 	 * find a schedule that can handle the event (for example requesting a
309e705c121SKalle Valo 	 * P2P Device discoveribility, while there are other higher priority
310e705c121SKalle Valo 	 * events in the system).
311e705c121SKalle Valo 	 */
312e705c121SKalle Valo 	if (!le32_to_cpu(notif->status)) {
313e705c121SKalle Valo 		const char *msg;
314e705c121SKalle Valo 
315e705c121SKalle Valo 		if (notif->action & cpu_to_le32(TE_V2_NOTIF_HOST_EVENT_START))
316e705c121SKalle Valo 			msg = "Time Event start notification failure";
317e705c121SKalle Valo 		else
318e705c121SKalle Valo 			msg = "Time Event end notification failure";
319e705c121SKalle Valo 
320e705c121SKalle Valo 		IWL_DEBUG_TE(mvm, "%s\n", msg);
321e705c121SKalle Valo 
322e705c121SKalle Valo 		if (iwl_mvm_te_check_disconnect(mvm, te_data->vif, msg)) {
323e705c121SKalle Valo 			iwl_mvm_te_clear_data(mvm, te_data);
324e705c121SKalle Valo 			return;
325e705c121SKalle Valo 		}
326e705c121SKalle Valo 	}
327e705c121SKalle Valo 
328e705c121SKalle Valo 	if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_END) {
329e705c121SKalle Valo 		IWL_DEBUG_TE(mvm,
330e705c121SKalle Valo 			     "TE ended - current time %lu, estimated end %lu\n",
331e705c121SKalle Valo 			     jiffies, te_data->end_jiffies);
332e705c121SKalle Valo 
333e705c121SKalle Valo 		switch (te_data->vif->type) {
334e705c121SKalle Valo 		case NL80211_IFTYPE_P2P_DEVICE:
335e705c121SKalle Valo 			ieee80211_remain_on_channel_expired(mvm->hw);
3364f1e85f0SIlan Peer 			set_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status);
337e705c121SKalle Valo 			iwl_mvm_roc_finished(mvm);
338e705c121SKalle Valo 			break;
339e705c121SKalle Valo 		case NL80211_IFTYPE_STATION:
340e705c121SKalle Valo 			/*
341e705c121SKalle Valo 			 * By now, we should have finished association
342e705c121SKalle Valo 			 * and know the dtim period.
343e705c121SKalle Valo 			 */
344e705c121SKalle Valo 			iwl_mvm_te_check_disconnect(mvm, te_data->vif,
34519125cb0SAndrei Otcheretianski 				"No beacon heard and the time event is over already...");
346e705c121SKalle Valo 			break;
347e705c121SKalle Valo 		default:
348e705c121SKalle Valo 			break;
349e705c121SKalle Valo 		}
350e705c121SKalle Valo 
351e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
352e705c121SKalle Valo 	} else if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_START) {
353e705c121SKalle Valo 		te_data->running = true;
354e705c121SKalle Valo 		te_data->end_jiffies = TU_TO_EXP_TIME(te_data->duration);
355e705c121SKalle Valo 
356e705c121SKalle Valo 		if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
357e705c121SKalle Valo 			set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
358e705c121SKalle Valo 			ieee80211_ready_on_channel(mvm->hw);
359e705c121SKalle Valo 		} else if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
360e705c121SKalle Valo 			iwl_mvm_te_handle_notify_csa(mvm, te_data, notif);
361e705c121SKalle Valo 		}
362e705c121SKalle Valo 	} else {
363e705c121SKalle Valo 		IWL_WARN(mvm, "Got TE with unknown action\n");
364e705c121SKalle Valo 	}
365e705c121SKalle Valo }
366e705c121SKalle Valo 
367e705c121SKalle Valo /*
368e705c121SKalle Valo  * Handle A Aux ROC time event
369e705c121SKalle Valo  */
370e705c121SKalle Valo static int iwl_mvm_aux_roc_te_handle_notif(struct iwl_mvm *mvm,
371e705c121SKalle Valo 					   struct iwl_time_event_notif *notif)
372e705c121SKalle Valo {
373e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data, *tmp;
374e705c121SKalle Valo 	bool aux_roc_te = false;
375e705c121SKalle Valo 
376e705c121SKalle Valo 	list_for_each_entry_safe(te_data, tmp, &mvm->aux_roc_te_list, list) {
377e705c121SKalle Valo 		if (le32_to_cpu(notif->unique_id) == te_data->uid) {
378e705c121SKalle Valo 			aux_roc_te = true;
379e705c121SKalle Valo 			break;
380e705c121SKalle Valo 		}
381e705c121SKalle Valo 	}
382e705c121SKalle Valo 	if (!aux_roc_te) /* Not a Aux ROC time event */
383e705c121SKalle Valo 		return -EINVAL;
384e705c121SKalle Valo 
385e705c121SKalle Valo 	iwl_mvm_te_check_trigger(mvm, notif, te_data);
386e705c121SKalle Valo 
387e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
3885151ad95SMatti Gottlieb 		     "Aux ROC time event notification  - UID = 0x%x action %d (error = %d)\n",
389e705c121SKalle Valo 		     le32_to_cpu(notif->unique_id),
3905151ad95SMatti Gottlieb 		     le32_to_cpu(notif->action), le32_to_cpu(notif->status));
391e705c121SKalle Valo 
3925151ad95SMatti Gottlieb 	if (!le32_to_cpu(notif->status) ||
3935151ad95SMatti Gottlieb 	    le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_END) {
394e705c121SKalle Valo 		/* End TE, notify mac80211 */
395e705c121SKalle Valo 		ieee80211_remain_on_channel_expired(mvm->hw);
396e705c121SKalle Valo 		iwl_mvm_roc_finished(mvm); /* flush aux queue */
397e705c121SKalle Valo 		list_del(&te_data->list); /* remove from list */
398e705c121SKalle Valo 		te_data->running = false;
399e705c121SKalle Valo 		te_data->vif = NULL;
400e705c121SKalle Valo 		te_data->uid = 0;
401e705c121SKalle Valo 		te_data->id = TE_MAX;
402e705c121SKalle Valo 	} else if (le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_START) {
403e705c121SKalle Valo 		set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
404e705c121SKalle Valo 		te_data->running = true;
405e705c121SKalle Valo 		ieee80211_ready_on_channel(mvm->hw); /* Start TE */
406e705c121SKalle Valo 	} else {
407e705c121SKalle Valo 		IWL_DEBUG_TE(mvm,
408e705c121SKalle Valo 			     "ERROR: Unknown Aux ROC Time Event (action = %d)\n",
409e705c121SKalle Valo 			     le32_to_cpu(notif->action));
410e705c121SKalle Valo 		return -EINVAL;
411e705c121SKalle Valo 	}
412e705c121SKalle Valo 
413e705c121SKalle Valo 	return 0;
414e705c121SKalle Valo }
415e705c121SKalle Valo 
416e705c121SKalle Valo /*
417e705c121SKalle Valo  * The Rx handler for time event notifications
418e705c121SKalle Valo  */
419e705c121SKalle Valo void iwl_mvm_rx_time_event_notif(struct iwl_mvm *mvm,
420e705c121SKalle Valo 				 struct iwl_rx_cmd_buffer *rxb)
421e705c121SKalle Valo {
422e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
423e705c121SKalle Valo 	struct iwl_time_event_notif *notif = (void *)pkt->data;
424e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data, *tmp;
425e705c121SKalle Valo 
426e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "Time event notification - UID = 0x%x action %d\n",
427e705c121SKalle Valo 		     le32_to_cpu(notif->unique_id),
428e705c121SKalle Valo 		     le32_to_cpu(notif->action));
429e705c121SKalle Valo 
430e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
431e705c121SKalle Valo 	/* This time event is triggered for Aux ROC request */
432e705c121SKalle Valo 	if (!iwl_mvm_aux_roc_te_handle_notif(mvm, notif))
433e705c121SKalle Valo 		goto unlock;
434e705c121SKalle Valo 
435e705c121SKalle Valo 	list_for_each_entry_safe(te_data, tmp, &mvm->time_event_list, list) {
436e705c121SKalle Valo 		if (le32_to_cpu(notif->unique_id) == te_data->uid)
437e705c121SKalle Valo 			iwl_mvm_te_handle_notif(mvm, te_data, notif);
438e705c121SKalle Valo 	}
439e705c121SKalle Valo unlock:
440e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
441e705c121SKalle Valo }
442e705c121SKalle Valo 
443e705c121SKalle Valo static bool iwl_mvm_te_notif(struct iwl_notif_wait_data *notif_wait,
444e705c121SKalle Valo 			     struct iwl_rx_packet *pkt, void *data)
445e705c121SKalle Valo {
446e705c121SKalle Valo 	struct iwl_mvm *mvm =
447e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
448e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
449e705c121SKalle Valo 	struct iwl_time_event_notif *resp;
450e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
451e705c121SKalle Valo 
452e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_NOTIFICATION))
453e705c121SKalle Valo 		return true;
454e705c121SKalle Valo 
455e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
456e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid TIME_EVENT_NOTIFICATION response\n");
457e705c121SKalle Valo 		return true;
458e705c121SKalle Valo 	}
459e705c121SKalle Valo 
460e705c121SKalle Valo 	resp = (void *)pkt->data;
461e705c121SKalle Valo 
462e705c121SKalle Valo 	/* te_data->uid is already set in the TIME_EVENT_CMD response */
463e705c121SKalle Valo 	if (le32_to_cpu(resp->unique_id) != te_data->uid)
464e705c121SKalle Valo 		return false;
465e705c121SKalle Valo 
466e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n",
467e705c121SKalle Valo 		     te_data->uid);
468e705c121SKalle Valo 	if (!resp->status)
469e705c121SKalle Valo 		IWL_ERR(mvm,
470e705c121SKalle Valo 			"TIME_EVENT_NOTIFICATION received but not executed\n");
471e705c121SKalle Valo 
472e705c121SKalle Valo 	return true;
473e705c121SKalle Valo }
474e705c121SKalle Valo 
475e705c121SKalle Valo static bool iwl_mvm_time_event_response(struct iwl_notif_wait_data *notif_wait,
476e705c121SKalle Valo 					struct iwl_rx_packet *pkt, void *data)
477e705c121SKalle Valo {
478e705c121SKalle Valo 	struct iwl_mvm *mvm =
479e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
480e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
481e705c121SKalle Valo 	struct iwl_time_event_resp *resp;
482e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
483e705c121SKalle Valo 
484e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_CMD))
485e705c121SKalle Valo 		return true;
486e705c121SKalle Valo 
487e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
488e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid TIME_EVENT_CMD response\n");
489e705c121SKalle Valo 		return true;
490e705c121SKalle Valo 	}
491e705c121SKalle Valo 
492e705c121SKalle Valo 	resp = (void *)pkt->data;
493e705c121SKalle Valo 
494e705c121SKalle Valo 	/* we should never get a response to another TIME_EVENT_CMD here */
495e705c121SKalle Valo 	if (WARN_ON_ONCE(le32_to_cpu(resp->id) != te_data->id))
496e705c121SKalle Valo 		return false;
497e705c121SKalle Valo 
498e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->unique_id);
499e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
500e705c121SKalle Valo 		     te_data->uid);
501e705c121SKalle Valo 	return true;
502e705c121SKalle Valo }
503e705c121SKalle Valo 
504e705c121SKalle Valo static int iwl_mvm_time_event_send_add(struct iwl_mvm *mvm,
505e705c121SKalle Valo 				       struct ieee80211_vif *vif,
506e705c121SKalle Valo 				       struct iwl_mvm_time_event_data *te_data,
507e705c121SKalle Valo 				       struct iwl_time_event_cmd *te_cmd)
508e705c121SKalle Valo {
509e705c121SKalle Valo 	static const u16 time_event_response[] = { TIME_EVENT_CMD };
510e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
511e705c121SKalle Valo 	int ret;
512e705c121SKalle Valo 
513e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
514e705c121SKalle Valo 
515e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "Add new TE, duration %d TU\n",
516e705c121SKalle Valo 		     le32_to_cpu(te_cmd->duration));
517e705c121SKalle Valo 
518e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
519e705c121SKalle Valo 	if (WARN_ON(te_data->id != TE_MAX)) {
520e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
521e705c121SKalle Valo 		return -EIO;
522e705c121SKalle Valo 	}
523e705c121SKalle Valo 	te_data->vif = vif;
524e705c121SKalle Valo 	te_data->duration = le32_to_cpu(te_cmd->duration);
525e705c121SKalle Valo 	te_data->id = le32_to_cpu(te_cmd->id);
526e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->time_event_list);
527e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
528e705c121SKalle Valo 
529e705c121SKalle Valo 	/*
530e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
531e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
532e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
533e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
534e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
535e705c121SKalle Valo 	 * time another notification (that the time event started)
536e705c121SKalle Valo 	 * might already be processed unsuccessfully.
537e705c121SKalle Valo 	 */
538e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
539e705c121SKalle Valo 				   time_event_response,
540e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
541e705c121SKalle Valo 				   iwl_mvm_time_event_response, te_data);
542e705c121SKalle Valo 
543e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
544e705c121SKalle Valo 					    sizeof(*te_cmd), te_cmd);
545e705c121SKalle Valo 	if (ret) {
546e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send TIME_EVENT_CMD: %d\n", ret);
547e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
548e705c121SKalle Valo 		goto out_clear_te;
549e705c121SKalle Valo 	}
550e705c121SKalle Valo 
551e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
552e705c121SKalle Valo 	ret = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
553e705c121SKalle Valo 	/* should never fail */
554e705c121SKalle Valo 	WARN_ON_ONCE(ret);
555e705c121SKalle Valo 
556e705c121SKalle Valo 	if (ret) {
557e705c121SKalle Valo  out_clear_te:
558e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
559e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
560e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
561e705c121SKalle Valo 	}
562e705c121SKalle Valo 	return ret;
563e705c121SKalle Valo }
564e705c121SKalle Valo 
565e705c121SKalle Valo void iwl_mvm_protect_session(struct iwl_mvm *mvm,
566e705c121SKalle Valo 			     struct ieee80211_vif *vif,
567e705c121SKalle Valo 			     u32 duration, u32 min_duration,
568e705c121SKalle Valo 			     u32 max_delay, bool wait_for_notif)
569e705c121SKalle Valo {
570e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
571e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
572e705c121SKalle Valo 	const u16 te_notif_response[] = { TIME_EVENT_NOTIFICATION };
573e705c121SKalle Valo 	struct iwl_notification_wait wait_te_notif;
574e705c121SKalle Valo 	struct iwl_time_event_cmd time_cmd = {};
575e705c121SKalle Valo 
576e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
577e705c121SKalle Valo 
578e705c121SKalle Valo 	if (te_data->running &&
579e705c121SKalle Valo 	    time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
580e705c121SKalle Valo 		IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
581e705c121SKalle Valo 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
582e705c121SKalle Valo 		return;
583e705c121SKalle Valo 	}
584e705c121SKalle Valo 
585e705c121SKalle Valo 	if (te_data->running) {
586e705c121SKalle Valo 		IWL_DEBUG_TE(mvm, "extend 0x%x: only %u ms left\n",
587e705c121SKalle Valo 			     te_data->uid,
588e705c121SKalle Valo 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
589e705c121SKalle Valo 		/*
590e705c121SKalle Valo 		 * we don't have enough time
591e705c121SKalle Valo 		 * cancel the current TE and issue a new one
592e705c121SKalle Valo 		 * Of course it would be better to remove the old one only
593e705c121SKalle Valo 		 * when the new one is added, but we don't care if we are off
594e705c121SKalle Valo 		 * channel for a bit. All we need to do, is not to return
595e705c121SKalle Valo 		 * before we actually begin to be on the channel.
596e705c121SKalle Valo 		 */
597e705c121SKalle Valo 		iwl_mvm_stop_session_protection(mvm, vif);
598e705c121SKalle Valo 	}
599e705c121SKalle Valo 
600e705c121SKalle Valo 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
601e705c121SKalle Valo 	time_cmd.id_and_color =
602e705c121SKalle Valo 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
603e705c121SKalle Valo 	time_cmd.id = cpu_to_le32(TE_BSS_STA_AGGRESSIVE_ASSOC);
604e705c121SKalle Valo 
605e705c121SKalle Valo 	time_cmd.apply_time = cpu_to_le32(0);
606e705c121SKalle Valo 
607e705c121SKalle Valo 	time_cmd.max_frags = TE_V2_FRAG_NONE;
608e705c121SKalle Valo 	time_cmd.max_delay = cpu_to_le32(max_delay);
609e705c121SKalle Valo 	/* TODO: why do we need to interval = bi if it is not periodic? */
610e705c121SKalle Valo 	time_cmd.interval = cpu_to_le32(1);
611e705c121SKalle Valo 	time_cmd.duration = cpu_to_le32(duration);
612e705c121SKalle Valo 	time_cmd.repeat = 1;
613e705c121SKalle Valo 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
614e705c121SKalle Valo 				      TE_V2_NOTIF_HOST_EVENT_END |
61540d53f4aSAndrei Otcheretianski 				      TE_V2_START_IMMEDIATELY);
616e705c121SKalle Valo 
617e705c121SKalle Valo 	if (!wait_for_notif) {
618e705c121SKalle Valo 		iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
619e705c121SKalle Valo 		return;
620e705c121SKalle Valo 	}
621e705c121SKalle Valo 
622e705c121SKalle Valo 	/*
623e705c121SKalle Valo 	 * Create notification_wait for the TIME_EVENT_NOTIFICATION to use
624e705c121SKalle Valo 	 * right after we send the time event
625e705c121SKalle Valo 	 */
626e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_te_notif,
627e705c121SKalle Valo 				   te_notif_response,
628e705c121SKalle Valo 				   ARRAY_SIZE(te_notif_response),
629e705c121SKalle Valo 				   iwl_mvm_te_notif, te_data);
630e705c121SKalle Valo 
631e705c121SKalle Valo 	/* If TE was sent OK - wait for the notification that started */
632e705c121SKalle Valo 	if (iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd)) {
633e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add TE to protect session\n");
634e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_te_notif);
635e705c121SKalle Valo 	} else if (iwl_wait_notification(&mvm->notif_wait, &wait_te_notif,
636e705c121SKalle Valo 					 TU_TO_JIFFIES(max_delay))) {
637e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to protect session until TE\n");
638e705c121SKalle Valo 	}
639e705c121SKalle Valo }
640e705c121SKalle Valo 
641e705c121SKalle Valo static bool __iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
642e705c121SKalle Valo 					struct iwl_mvm_time_event_data *te_data,
643e705c121SKalle Valo 					u32 *uid)
644e705c121SKalle Valo {
645e705c121SKalle Valo 	u32 id;
646e705c121SKalle Valo 
647e705c121SKalle Valo 	/*
648e705c121SKalle Valo 	 * It is possible that by the time we got to this point the time
649e705c121SKalle Valo 	 * event was already removed.
650e705c121SKalle Valo 	 */
651e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
652e705c121SKalle Valo 
653e705c121SKalle Valo 	/* Save time event uid before clearing its data */
654e705c121SKalle Valo 	*uid = te_data->uid;
655e705c121SKalle Valo 	id = te_data->id;
656e705c121SKalle Valo 
657e705c121SKalle Valo 	/*
658e705c121SKalle Valo 	 * The clear_data function handles time events that were already removed
659e705c121SKalle Valo 	 */
660e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, te_data);
661e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
662e705c121SKalle Valo 
663e705c121SKalle Valo 	/*
664e705c121SKalle Valo 	 * It is possible that by the time we try to remove it, the time event
665e705c121SKalle Valo 	 * has already ended and removed. In such a case there is no need to
666e705c121SKalle Valo 	 * send a removal command.
667e705c121SKalle Valo 	 */
668e705c121SKalle Valo 	if (id == TE_MAX) {
669e705c121SKalle Valo 		IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", *uid);
670e705c121SKalle Valo 		return false;
671e705c121SKalle Valo 	}
672e705c121SKalle Valo 
673e705c121SKalle Valo 	return true;
674e705c121SKalle Valo }
675e705c121SKalle Valo 
676e705c121SKalle Valo /*
677e705c121SKalle Valo  * Explicit request to remove a aux roc time event. The removal of a time
678e705c121SKalle Valo  * event needs to be synchronized with the flow of a time event's end
679e705c121SKalle Valo  * notification, which also removes the time event from the op mode
680e705c121SKalle Valo  * data structures.
681e705c121SKalle Valo  */
682e705c121SKalle Valo static void iwl_mvm_remove_aux_roc_te(struct iwl_mvm *mvm,
683e705c121SKalle Valo 				      struct iwl_mvm_vif *mvmvif,
684e705c121SKalle Valo 				      struct iwl_mvm_time_event_data *te_data)
685e705c121SKalle Valo {
686e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_cmd = {};
68757e861d9SDavid Spinadel 	u16 len = sizeof(aux_cmd) - iwl_mvm_chan_info_padding(mvm);
68857e861d9SDavid Spinadel 
689e705c121SKalle Valo 	u32 uid;
690e705c121SKalle Valo 	int ret;
691e705c121SKalle Valo 
692e705c121SKalle Valo 	if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
693e705c121SKalle Valo 		return;
694e705c121SKalle Valo 
695e705c121SKalle Valo 	aux_cmd.event_unique_id = cpu_to_le32(uid);
696e705c121SKalle Valo 	aux_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
697e705c121SKalle Valo 	aux_cmd.id_and_color =
698e705c121SKalle Valo 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
699e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "Removing BSS AUX ROC TE 0x%x\n",
700e705c121SKalle Valo 		     le32_to_cpu(aux_cmd.event_unique_id));
701e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0,
70257e861d9SDavid Spinadel 				   len, &aux_cmd);
703e705c121SKalle Valo 
704e705c121SKalle Valo 	if (WARN_ON(ret))
705e705c121SKalle Valo 		return;
706e705c121SKalle Valo }
707e705c121SKalle Valo 
708e705c121SKalle Valo /*
709e705c121SKalle Valo  * Explicit request to remove a time event. The removal of a time event needs to
710e705c121SKalle Valo  * be synchronized with the flow of a time event's end notification, which also
711e705c121SKalle Valo  * removes the time event from the op mode data structures.
712e705c121SKalle Valo  */
713e705c121SKalle Valo void iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
714e705c121SKalle Valo 			       struct iwl_mvm_vif *mvmvif,
715e705c121SKalle Valo 			       struct iwl_mvm_time_event_data *te_data)
716e705c121SKalle Valo {
717e705c121SKalle Valo 	struct iwl_time_event_cmd time_cmd = {};
718e705c121SKalle Valo 	u32 uid;
719e705c121SKalle Valo 	int ret;
720e705c121SKalle Valo 
721e705c121SKalle Valo 	if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
722e705c121SKalle Valo 		return;
723e705c121SKalle Valo 
724e705c121SKalle Valo 	/* When we remove a TE, the UID is to be set in the id field */
725e705c121SKalle Valo 	time_cmd.id = cpu_to_le32(uid);
726e705c121SKalle Valo 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
727e705c121SKalle Valo 	time_cmd.id_and_color =
728e705c121SKalle Valo 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
729e705c121SKalle Valo 
730e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "Removing TE 0x%x\n", le32_to_cpu(time_cmd.id));
731e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
732e705c121SKalle Valo 				   sizeof(time_cmd), &time_cmd);
733e705c121SKalle Valo 	if (WARN_ON(ret))
734e705c121SKalle Valo 		return;
735e705c121SKalle Valo }
736e705c121SKalle Valo 
737fe959c7bSEmmanuel Grumbach /*
738fe959c7bSEmmanuel Grumbach  * When the firmware supports the session protection API,
739fe959c7bSEmmanuel Grumbach  * this is not needed since it'll automatically remove the
740fe959c7bSEmmanuel Grumbach  * session protection after association + beacon reception.
741fe959c7bSEmmanuel Grumbach  */
742e705c121SKalle Valo void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,
743e705c121SKalle Valo 				     struct ieee80211_vif *vif)
744e705c121SKalle Valo {
745e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
746e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
7473edfb5f4SAvraham Stern 	u32 id;
748e705c121SKalle Valo 
749e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
7503edfb5f4SAvraham Stern 
7513edfb5f4SAvraham Stern 	spin_lock_bh(&mvm->time_event_lock);
7523edfb5f4SAvraham Stern 	id = te_data->id;
7533edfb5f4SAvraham Stern 	spin_unlock_bh(&mvm->time_event_lock);
7543edfb5f4SAvraham Stern 
7553edfb5f4SAvraham Stern 	if (id != TE_BSS_STA_AGGRESSIVE_ASSOC) {
7563edfb5f4SAvraham Stern 		IWL_DEBUG_TE(mvm,
7573edfb5f4SAvraham Stern 			     "don't remove TE with id=%u (not session protection)\n",
7583edfb5f4SAvraham Stern 			     id);
7593edfb5f4SAvraham Stern 		return;
7603edfb5f4SAvraham Stern 	}
7613edfb5f4SAvraham Stern 
762e705c121SKalle Valo 	iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
763e705c121SKalle Valo }
764e705c121SKalle Valo 
765fe959c7bSEmmanuel Grumbach void iwl_mvm_rx_session_protect_notif(struct iwl_mvm *mvm,
766fe959c7bSEmmanuel Grumbach 				      struct iwl_rx_cmd_buffer *rxb)
767fe959c7bSEmmanuel Grumbach {
768fe959c7bSEmmanuel Grumbach 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
769fe959c7bSEmmanuel Grumbach 	struct iwl_mvm_session_prot_notif *notif = (void *)pkt->data;
770fe959c7bSEmmanuel Grumbach 	struct ieee80211_vif *vif;
771fe959c7bSEmmanuel Grumbach 
772fe959c7bSEmmanuel Grumbach 	rcu_read_lock();
773fe959c7bSEmmanuel Grumbach 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, le32_to_cpu(notif->mac_id),
774fe959c7bSEmmanuel Grumbach 					     true);
775fe959c7bSEmmanuel Grumbach 
776fe959c7bSEmmanuel Grumbach 	if (!vif)
777fe959c7bSEmmanuel Grumbach 		goto out_unlock;
778fe959c7bSEmmanuel Grumbach 
779fe959c7bSEmmanuel Grumbach 	/* The vif is not a P2P_DEVICE, maintain its time_event_data */
780fe959c7bSEmmanuel Grumbach 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
781fe959c7bSEmmanuel Grumbach 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
782fe959c7bSEmmanuel Grumbach 		struct iwl_mvm_time_event_data *te_data =
783fe959c7bSEmmanuel Grumbach 			&mvmvif->time_event_data;
784fe959c7bSEmmanuel Grumbach 
785fe959c7bSEmmanuel Grumbach 		if (!le32_to_cpu(notif->status)) {
786fe959c7bSEmmanuel Grumbach 			iwl_mvm_te_check_disconnect(mvm, vif,
787fe959c7bSEmmanuel Grumbach 						    "Session protection failure");
788089e5016SAvraham Stern 			spin_lock_bh(&mvm->time_event_lock);
789fe959c7bSEmmanuel Grumbach 			iwl_mvm_te_clear_data(mvm, te_data);
790089e5016SAvraham Stern 			spin_unlock_bh(&mvm->time_event_lock);
791fe959c7bSEmmanuel Grumbach 		}
792fe959c7bSEmmanuel Grumbach 
793fe959c7bSEmmanuel Grumbach 		if (le32_to_cpu(notif->start)) {
794fe959c7bSEmmanuel Grumbach 			spin_lock_bh(&mvm->time_event_lock);
795fe959c7bSEmmanuel Grumbach 			te_data->running = le32_to_cpu(notif->start);
796fe959c7bSEmmanuel Grumbach 			te_data->end_jiffies =
797fe959c7bSEmmanuel Grumbach 				TU_TO_EXP_TIME(te_data->duration);
798fe959c7bSEmmanuel Grumbach 			spin_unlock_bh(&mvm->time_event_lock);
799fe959c7bSEmmanuel Grumbach 		} else {
800fe959c7bSEmmanuel Grumbach 			/*
801fe959c7bSEmmanuel Grumbach 			 * By now, we should have finished association
802fe959c7bSEmmanuel Grumbach 			 * and know the dtim period.
803fe959c7bSEmmanuel Grumbach 			 */
804fe959c7bSEmmanuel Grumbach 			iwl_mvm_te_check_disconnect(mvm, vif,
805fe959c7bSEmmanuel Grumbach 						    "No beacon heard and the session protection is over already...");
806089e5016SAvraham Stern 			spin_lock_bh(&mvm->time_event_lock);
807fe959c7bSEmmanuel Grumbach 			iwl_mvm_te_clear_data(mvm, te_data);
808089e5016SAvraham Stern 			spin_unlock_bh(&mvm->time_event_lock);
809fe959c7bSEmmanuel Grumbach 		}
810fe959c7bSEmmanuel Grumbach 
811fe959c7bSEmmanuel Grumbach 		goto out_unlock;
812fe959c7bSEmmanuel Grumbach 	}
813fe959c7bSEmmanuel Grumbach 
814fe959c7bSEmmanuel Grumbach 	if (!le32_to_cpu(notif->status) || !le32_to_cpu(notif->start)) {
815fe959c7bSEmmanuel Grumbach 		/* End TE, notify mac80211 */
816fe959c7bSEmmanuel Grumbach 		ieee80211_remain_on_channel_expired(mvm->hw);
817fe959c7bSEmmanuel Grumbach 		set_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status);
818fe959c7bSEmmanuel Grumbach 		iwl_mvm_roc_finished(mvm);
819fe959c7bSEmmanuel Grumbach 	} else if (le32_to_cpu(notif->start)) {
820fe959c7bSEmmanuel Grumbach 		set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
821fe959c7bSEmmanuel Grumbach 		ieee80211_ready_on_channel(mvm->hw); /* Start TE */
822fe959c7bSEmmanuel Grumbach 	}
823fe959c7bSEmmanuel Grumbach 
824fe959c7bSEmmanuel Grumbach  out_unlock:
825fe959c7bSEmmanuel Grumbach 	rcu_read_unlock();
826fe959c7bSEmmanuel Grumbach }
827fe959c7bSEmmanuel Grumbach 
828fe959c7bSEmmanuel Grumbach static int
829fe959c7bSEmmanuel Grumbach iwl_mvm_start_p2p_roc_session_protection(struct iwl_mvm *mvm,
830fe959c7bSEmmanuel Grumbach 					 struct ieee80211_vif *vif,
831fe959c7bSEmmanuel Grumbach 					 int duration,
832fe959c7bSEmmanuel Grumbach 					 enum ieee80211_roc_type type)
833fe959c7bSEmmanuel Grumbach {
834fe959c7bSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
835fe959c7bSEmmanuel Grumbach 	struct iwl_mvm_session_prot_cmd cmd = {
836fe959c7bSEmmanuel Grumbach 		.id_and_color =
837fe959c7bSEmmanuel Grumbach 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
838fe959c7bSEmmanuel Grumbach 							mvmvif->color)),
839fe959c7bSEmmanuel Grumbach 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
840fe959c7bSEmmanuel Grumbach 		.duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
841fe959c7bSEmmanuel Grumbach 	};
842fe959c7bSEmmanuel Grumbach 
843fe959c7bSEmmanuel Grumbach 	lockdep_assert_held(&mvm->mutex);
844fe959c7bSEmmanuel Grumbach 
845fe959c7bSEmmanuel Grumbach 	switch (type) {
846fe959c7bSEmmanuel Grumbach 	case IEEE80211_ROC_TYPE_NORMAL:
847fe959c7bSEmmanuel Grumbach 		cmd.conf_id =
848fe959c7bSEmmanuel Grumbach 			cpu_to_le32(SESSION_PROTECT_CONF_P2P_DEVICE_DISCOV);
849fe959c7bSEmmanuel Grumbach 		break;
850fe959c7bSEmmanuel Grumbach 	case IEEE80211_ROC_TYPE_MGMT_TX:
851fe959c7bSEmmanuel Grumbach 		cmd.conf_id =
852fe959c7bSEmmanuel Grumbach 			cpu_to_le32(SESSION_PROTECT_CONF_P2P_GO_NEGOTIATION);
853fe959c7bSEmmanuel Grumbach 		break;
854fe959c7bSEmmanuel Grumbach 	default:
855fe959c7bSEmmanuel Grumbach 		WARN_ONCE(1, "Got an invalid ROC type\n");
856fe959c7bSEmmanuel Grumbach 		return -EINVAL;
857fe959c7bSEmmanuel Grumbach 	}
858fe959c7bSEmmanuel Grumbach 
859fe959c7bSEmmanuel Grumbach 	return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(SESSION_PROTECTION_CMD,
860fe959c7bSEmmanuel Grumbach 						    MAC_CONF_GROUP, 0),
861fe959c7bSEmmanuel Grumbach 				    0, sizeof(cmd), &cmd);
862fe959c7bSEmmanuel Grumbach }
863fe959c7bSEmmanuel Grumbach 
864e705c121SKalle Valo int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
865e705c121SKalle Valo 			  int duration, enum ieee80211_roc_type type)
866e705c121SKalle Valo {
867e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
868e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
869e705c121SKalle Valo 	struct iwl_time_event_cmd time_cmd = {};
870e705c121SKalle Valo 
871e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
872e705c121SKalle Valo 	if (te_data->running) {
873e705c121SKalle Valo 		IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");
874e705c121SKalle Valo 		return -EBUSY;
875e705c121SKalle Valo 	}
876e705c121SKalle Valo 
877fe959c7bSEmmanuel Grumbach 	if (fw_has_capa(&mvm->fw->ucode_capa,
878fe959c7bSEmmanuel Grumbach 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
879fe959c7bSEmmanuel Grumbach 		return iwl_mvm_start_p2p_roc_session_protection(mvm, vif,
880fe959c7bSEmmanuel Grumbach 								duration,
881fe959c7bSEmmanuel Grumbach 								type);
882fe959c7bSEmmanuel Grumbach 
883e705c121SKalle Valo 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
884e705c121SKalle Valo 	time_cmd.id_and_color =
885e705c121SKalle Valo 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
886e705c121SKalle Valo 
887e705c121SKalle Valo 	switch (type) {
888e705c121SKalle Valo 	case IEEE80211_ROC_TYPE_NORMAL:
889e705c121SKalle Valo 		time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);
890e705c121SKalle Valo 		break;
891e705c121SKalle Valo 	case IEEE80211_ROC_TYPE_MGMT_TX:
892e705c121SKalle Valo 		time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);
893e705c121SKalle Valo 		break;
894e705c121SKalle Valo 	default:
895e705c121SKalle Valo 		WARN_ONCE(1, "Got an invalid ROC type\n");
896e705c121SKalle Valo 		return -EINVAL;
897e705c121SKalle Valo 	}
898e705c121SKalle Valo 
899e705c121SKalle Valo 	time_cmd.apply_time = cpu_to_le32(0);
900e705c121SKalle Valo 	time_cmd.interval = cpu_to_le32(1);
901e705c121SKalle Valo 
902e705c121SKalle Valo 	/*
903e705c121SKalle Valo 	 * The P2P Device TEs can have lower priority than other events
904e705c121SKalle Valo 	 * that are being scheduled by the driver/fw, and thus it might not be
905e705c121SKalle Valo 	 * scheduled. To improve the chances of it being scheduled, allow them
906e705c121SKalle Valo 	 * to be fragmented, and in addition allow them to be delayed.
907e705c121SKalle Valo 	 */
908e705c121SKalle Valo 	time_cmd.max_frags = min(MSEC_TO_TU(duration)/50, TE_V2_FRAG_ENDLESS);
909e705c121SKalle Valo 	time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));
910e705c121SKalle Valo 	time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));
911e705c121SKalle Valo 	time_cmd.repeat = 1;
912e705c121SKalle Valo 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
913e705c121SKalle Valo 				      TE_V2_NOTIF_HOST_EVENT_END |
91440d53f4aSAndrei Otcheretianski 				      TE_V2_START_IMMEDIATELY);
915e705c121SKalle Valo 
916e705c121SKalle Valo 	return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
917e705c121SKalle Valo }
918e705c121SKalle Valo 
919305d236eSEliad Peller static struct iwl_mvm_time_event_data *iwl_mvm_get_roc_te(struct iwl_mvm *mvm)
920e705c121SKalle Valo {
921e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data;
922e705c121SKalle Valo 
923e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
924e705c121SKalle Valo 
925e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
926e705c121SKalle Valo 
927e705c121SKalle Valo 	/*
928e705c121SKalle Valo 	 * Iterate over the list of time events and find the time event that is
929e705c121SKalle Valo 	 * associated with a P2P_DEVICE interface.
930e705c121SKalle Valo 	 * This assumes that a P2P_DEVICE interface can have only a single time
931e705c121SKalle Valo 	 * event at any given time and this time event coresponds to a ROC
932e705c121SKalle Valo 	 * request
933e705c121SKalle Valo 	 */
934e705c121SKalle Valo 	list_for_each_entry(te_data, &mvm->time_event_list, list) {
935305d236eSEliad Peller 		if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE)
936305d236eSEliad Peller 			goto out;
937e705c121SKalle Valo 	}
938e705c121SKalle Valo 
939e705c121SKalle Valo 	/* There can only be at most one AUX ROC time event, we just use the
940e705c121SKalle Valo 	 * list to simplify/unify code. Remove it if it exists.
941e705c121SKalle Valo 	 */
942e705c121SKalle Valo 	te_data = list_first_entry_or_null(&mvm->aux_roc_te_list,
943e705c121SKalle Valo 					   struct iwl_mvm_time_event_data,
944e705c121SKalle Valo 					   list);
945305d236eSEliad Peller out:
946e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
947305d236eSEliad Peller 	return te_data;
948305d236eSEliad Peller }
949e705c121SKalle Valo 
950305d236eSEliad Peller void iwl_mvm_cleanup_roc_te(struct iwl_mvm *mvm)
951305d236eSEliad Peller {
952305d236eSEliad Peller 	struct iwl_mvm_time_event_data *te_data;
953305d236eSEliad Peller 	u32 uid;
954305d236eSEliad Peller 
955305d236eSEliad Peller 	te_data = iwl_mvm_get_roc_te(mvm);
956305d236eSEliad Peller 	if (te_data)
957305d236eSEliad Peller 		__iwl_mvm_remove_time_event(mvm, te_data, &uid);
958305d236eSEliad Peller }
959305d236eSEliad Peller 
960fe959c7bSEmmanuel Grumbach static void iwl_mvm_cancel_session_protection(struct iwl_mvm *mvm,
961fe959c7bSEmmanuel Grumbach 					      struct iwl_mvm_vif *mvmvif)
962fe959c7bSEmmanuel Grumbach {
963fe959c7bSEmmanuel Grumbach 	struct iwl_mvm_session_prot_cmd cmd = {
964fe959c7bSEmmanuel Grumbach 		.id_and_color =
965fe959c7bSEmmanuel Grumbach 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
966fe959c7bSEmmanuel Grumbach 							mvmvif->color)),
967fe959c7bSEmmanuel Grumbach 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
968fe959c7bSEmmanuel Grumbach 	};
969fe959c7bSEmmanuel Grumbach 	int ret;
970fe959c7bSEmmanuel Grumbach 
971fe959c7bSEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(SESSION_PROTECTION_CMD,
972fe959c7bSEmmanuel Grumbach 						   MAC_CONF_GROUP, 0),
973fe959c7bSEmmanuel Grumbach 				   0, sizeof(cmd), &cmd);
974fe959c7bSEmmanuel Grumbach 	if (ret)
975fe959c7bSEmmanuel Grumbach 		IWL_ERR(mvm,
976fe959c7bSEmmanuel Grumbach 			"Couldn't send the SESSION_PROTECTION_CMD: %d\n", ret);
977fe959c7bSEmmanuel Grumbach }
978fe959c7bSEmmanuel Grumbach 
979fe959c7bSEmmanuel Grumbach void iwl_mvm_stop_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
980305d236eSEliad Peller {
981305d236eSEliad Peller 	struct iwl_mvm_vif *mvmvif;
982305d236eSEliad Peller 	struct iwl_mvm_time_event_data *te_data;
983305d236eSEliad Peller 
984fe959c7bSEmmanuel Grumbach 	if (fw_has_capa(&mvm->fw->ucode_capa,
985fe959c7bSEmmanuel Grumbach 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
986fe959c7bSEmmanuel Grumbach 		mvmvif = iwl_mvm_vif_from_mac80211(vif);
987fe959c7bSEmmanuel Grumbach 
988fe959c7bSEmmanuel Grumbach 		iwl_mvm_cancel_session_protection(mvm, mvmvif);
989fe959c7bSEmmanuel Grumbach 
990fe959c7bSEmmanuel Grumbach 		if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
991fe959c7bSEmmanuel Grumbach 			set_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status);
992fe959c7bSEmmanuel Grumbach 
993fe959c7bSEmmanuel Grumbach 		iwl_mvm_roc_finished(mvm);
994fe959c7bSEmmanuel Grumbach 
995fe959c7bSEmmanuel Grumbach 		return;
996fe959c7bSEmmanuel Grumbach 	}
997fe959c7bSEmmanuel Grumbach 
998305d236eSEliad Peller 	te_data = iwl_mvm_get_roc_te(mvm);
999305d236eSEliad Peller 	if (!te_data) {
1000e705c121SKalle Valo 		IWL_WARN(mvm, "No remain on channel event\n");
1001e705c121SKalle Valo 		return;
1002e705c121SKalle Valo 	}
1003e705c121SKalle Valo 
1004305d236eSEliad Peller 	mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
1005305d236eSEliad Peller 
10066c2d49fdSJohannes Berg 	if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1007e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
10086c2d49fdSJohannes Berg 		set_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status);
10096c2d49fdSJohannes Berg 	} else {
1010e705c121SKalle Valo 		iwl_mvm_remove_aux_roc_te(mvm, mvmvif, te_data);
10116c2d49fdSJohannes Berg 	}
1012e705c121SKalle Valo 
1013e705c121SKalle Valo 	iwl_mvm_roc_finished(mvm);
1014e705c121SKalle Valo }
1015e705c121SKalle Valo 
1016e705c121SKalle Valo int iwl_mvm_schedule_csa_period(struct iwl_mvm *mvm,
1017e705c121SKalle Valo 				struct ieee80211_vif *vif,
1018e705c121SKalle Valo 				u32 duration, u32 apply_time)
1019e705c121SKalle Valo {
1020e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1021e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1022e705c121SKalle Valo 	struct iwl_time_event_cmd time_cmd = {};
1023e705c121SKalle Valo 
1024e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1025e705c121SKalle Valo 
1026e705c121SKalle Valo 	if (te_data->running) {
10273edfb5f4SAvraham Stern 		u32 id;
10283edfb5f4SAvraham Stern 
10293edfb5f4SAvraham Stern 		spin_lock_bh(&mvm->time_event_lock);
10303edfb5f4SAvraham Stern 		id = te_data->id;
10313edfb5f4SAvraham Stern 		spin_unlock_bh(&mvm->time_event_lock);
10323edfb5f4SAvraham Stern 
10333edfb5f4SAvraham Stern 		if (id == TE_CHANNEL_SWITCH_PERIOD) {
1034e705c121SKalle Valo 			IWL_DEBUG_TE(mvm, "CS period is already scheduled\n");
1035e705c121SKalle Valo 			return -EBUSY;
1036e705c121SKalle Valo 		}
1037e705c121SKalle Valo 
10383edfb5f4SAvraham Stern 		/*
10393edfb5f4SAvraham Stern 		 * Remove the session protection time event to allow the
10403edfb5f4SAvraham Stern 		 * channel switch. If we got here, we just heard a beacon so
10413edfb5f4SAvraham Stern 		 * the session protection is not needed anymore anyway.
10423edfb5f4SAvraham Stern 		 */
10433edfb5f4SAvraham Stern 		iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
10443edfb5f4SAvraham Stern 	}
10453edfb5f4SAvraham Stern 
1046e705c121SKalle Valo 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
1047e705c121SKalle Valo 	time_cmd.id_and_color =
1048e705c121SKalle Valo 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
1049e705c121SKalle Valo 	time_cmd.id = cpu_to_le32(TE_CHANNEL_SWITCH_PERIOD);
1050e705c121SKalle Valo 	time_cmd.apply_time = cpu_to_le32(apply_time);
1051e705c121SKalle Valo 	time_cmd.max_frags = TE_V2_FRAG_NONE;
1052e705c121SKalle Valo 	time_cmd.duration = cpu_to_le32(duration);
1053e705c121SKalle Valo 	time_cmd.repeat = 1;
1054e705c121SKalle Valo 	time_cmd.interval = cpu_to_le32(1);
1055e705c121SKalle Valo 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
1056e705c121SKalle Valo 				      TE_V2_ABSENCE);
105740d53f4aSAndrei Otcheretianski 	if (!apply_time)
105840d53f4aSAndrei Otcheretianski 		time_cmd.policy |= cpu_to_le16(TE_V2_START_IMMEDIATELY);
1059e705c121SKalle Valo 
1060e705c121SKalle Valo 	return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
1061e705c121SKalle Valo }
1062fe959c7bSEmmanuel Grumbach 
1063b5b878e3SEmmanuel Grumbach static bool iwl_mvm_session_prot_notif(struct iwl_notif_wait_data *notif_wait,
1064b5b878e3SEmmanuel Grumbach 				       struct iwl_rx_packet *pkt, void *data)
1065b5b878e3SEmmanuel Grumbach {
1066b5b878e3SEmmanuel Grumbach 	struct iwl_mvm *mvm =
1067b5b878e3SEmmanuel Grumbach 		container_of(notif_wait, struct iwl_mvm, notif_wait);
1068b5b878e3SEmmanuel Grumbach 	struct iwl_mvm_session_prot_notif *resp;
1069b5b878e3SEmmanuel Grumbach 	int resp_len = iwl_rx_packet_payload_len(pkt);
1070b5b878e3SEmmanuel Grumbach 
1071b5b878e3SEmmanuel Grumbach 	if (WARN_ON(pkt->hdr.cmd != SESSION_PROTECTION_NOTIF ||
1072b5b878e3SEmmanuel Grumbach 		    pkt->hdr.group_id != MAC_CONF_GROUP))
1073b5b878e3SEmmanuel Grumbach 		return true;
1074b5b878e3SEmmanuel Grumbach 
1075b5b878e3SEmmanuel Grumbach 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
1076b5b878e3SEmmanuel Grumbach 		IWL_ERR(mvm, "Invalid SESSION_PROTECTION_NOTIF response\n");
1077b5b878e3SEmmanuel Grumbach 		return true;
1078b5b878e3SEmmanuel Grumbach 	}
1079b5b878e3SEmmanuel Grumbach 
1080b5b878e3SEmmanuel Grumbach 	resp = (void *)pkt->data;
1081b5b878e3SEmmanuel Grumbach 
1082b5b878e3SEmmanuel Grumbach 	if (!resp->status)
1083b5b878e3SEmmanuel Grumbach 		IWL_ERR(mvm,
1084b5b878e3SEmmanuel Grumbach 			"TIME_EVENT_NOTIFICATION received but not executed\n");
1085b5b878e3SEmmanuel Grumbach 
1086b5b878e3SEmmanuel Grumbach 	return true;
1087b5b878e3SEmmanuel Grumbach }
1088b5b878e3SEmmanuel Grumbach 
1089fe959c7bSEmmanuel Grumbach void iwl_mvm_schedule_session_protection(struct iwl_mvm *mvm,
1090fe959c7bSEmmanuel Grumbach 					 struct ieee80211_vif *vif,
1091b5b878e3SEmmanuel Grumbach 					 u32 duration, u32 min_duration,
1092b5b878e3SEmmanuel Grumbach 					 bool wait_for_notif)
1093fe959c7bSEmmanuel Grumbach {
1094fe959c7bSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1095fe959c7bSEmmanuel Grumbach 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1096b5b878e3SEmmanuel Grumbach 	const u16 notif[] = { iwl_cmd_id(SESSION_PROTECTION_NOTIF,
1097b5b878e3SEmmanuel Grumbach 					 MAC_CONF_GROUP, 0) };
1098b5b878e3SEmmanuel Grumbach 	struct iwl_notification_wait wait_notif;
1099fe959c7bSEmmanuel Grumbach 	struct iwl_mvm_session_prot_cmd cmd = {
1100fe959c7bSEmmanuel Grumbach 		.id_and_color =
1101fe959c7bSEmmanuel Grumbach 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1102fe959c7bSEmmanuel Grumbach 							mvmvif->color)),
1103fe959c7bSEmmanuel Grumbach 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
1104fe959c7bSEmmanuel Grumbach 		.conf_id = cpu_to_le32(SESSION_PROTECT_CONF_ASSOC),
1105fe959c7bSEmmanuel Grumbach 		.duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
1106fe959c7bSEmmanuel Grumbach 	};
1107fe959c7bSEmmanuel Grumbach 
1108fe959c7bSEmmanuel Grumbach 	lockdep_assert_held(&mvm->mutex);
1109fe959c7bSEmmanuel Grumbach 
1110fe959c7bSEmmanuel Grumbach 	spin_lock_bh(&mvm->time_event_lock);
1111fe959c7bSEmmanuel Grumbach 	if (te_data->running &&
1112fe959c7bSEmmanuel Grumbach 	    time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
1113fe959c7bSEmmanuel Grumbach 		IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
1114fe959c7bSEmmanuel Grumbach 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
1115fe959c7bSEmmanuel Grumbach 		spin_unlock_bh(&mvm->time_event_lock);
1116fe959c7bSEmmanuel Grumbach 
1117fe959c7bSEmmanuel Grumbach 		return;
1118fe959c7bSEmmanuel Grumbach 	}
1119fe959c7bSEmmanuel Grumbach 
1120fe959c7bSEmmanuel Grumbach 	iwl_mvm_te_clear_data(mvm, te_data);
1121fe959c7bSEmmanuel Grumbach 	te_data->duration = le32_to_cpu(cmd.duration_tu);
1122fe959c7bSEmmanuel Grumbach 	spin_unlock_bh(&mvm->time_event_lock);
1123fe959c7bSEmmanuel Grumbach 
1124fe959c7bSEmmanuel Grumbach 	IWL_DEBUG_TE(mvm, "Add new session protection, duration %d TU\n",
1125fe959c7bSEmmanuel Grumbach 		     le32_to_cpu(cmd.duration_tu));
1126fe959c7bSEmmanuel Grumbach 
1127b5b878e3SEmmanuel Grumbach 	if (!wait_for_notif) {
1128b5b878e3SEmmanuel Grumbach 		if (iwl_mvm_send_cmd_pdu(mvm,
1129b5b878e3SEmmanuel Grumbach 					 iwl_cmd_id(SESSION_PROTECTION_CMD,
1130fe959c7bSEmmanuel Grumbach 						    MAC_CONF_GROUP, 0),
1131b5b878e3SEmmanuel Grumbach 					 0, sizeof(cmd), &cmd)) {
1132fe959c7bSEmmanuel Grumbach 			IWL_ERR(mvm,
1133b5b878e3SEmmanuel Grumbach 				"Couldn't send the SESSION_PROTECTION_CMD\n");
1134fe959c7bSEmmanuel Grumbach 			spin_lock_bh(&mvm->time_event_lock);
1135fe959c7bSEmmanuel Grumbach 			iwl_mvm_te_clear_data(mvm, te_data);
1136fe959c7bSEmmanuel Grumbach 			spin_unlock_bh(&mvm->time_event_lock);
1137fe959c7bSEmmanuel Grumbach 		}
1138b5b878e3SEmmanuel Grumbach 
1139b5b878e3SEmmanuel Grumbach 		return;
1140b5b878e3SEmmanuel Grumbach 	}
1141b5b878e3SEmmanuel Grumbach 
1142b5b878e3SEmmanuel Grumbach 	iwl_init_notification_wait(&mvm->notif_wait, &wait_notif,
1143b5b878e3SEmmanuel Grumbach 				   notif, ARRAY_SIZE(notif),
1144b5b878e3SEmmanuel Grumbach 				   iwl_mvm_session_prot_notif, NULL);
1145b5b878e3SEmmanuel Grumbach 
1146b5b878e3SEmmanuel Grumbach 	if (iwl_mvm_send_cmd_pdu(mvm,
1147b5b878e3SEmmanuel Grumbach 				 iwl_cmd_id(SESSION_PROTECTION_CMD,
1148b5b878e3SEmmanuel Grumbach 					    MAC_CONF_GROUP, 0),
1149b5b878e3SEmmanuel Grumbach 				 0, sizeof(cmd), &cmd)) {
1150b5b878e3SEmmanuel Grumbach 		IWL_ERR(mvm,
1151b5b878e3SEmmanuel Grumbach 			"Couldn't send the SESSION_PROTECTION_CMD\n");
1152b5b878e3SEmmanuel Grumbach 		iwl_remove_notification(&mvm->notif_wait, &wait_notif);
1153b5b878e3SEmmanuel Grumbach 	} else if (iwl_wait_notification(&mvm->notif_wait, &wait_notif,
1154b5b878e3SEmmanuel Grumbach 					 TU_TO_JIFFIES(100))) {
1155b5b878e3SEmmanuel Grumbach 		IWL_ERR(mvm,
1156b5b878e3SEmmanuel Grumbach 			"Failed to protect session until session protection\n");
1157b5b878e3SEmmanuel Grumbach 	}
1158fe959c7bSEmmanuel Grumbach }
1159