xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/time-event.c (revision 6c042d7505d082b3bda0909c8a2c9817aa210ea2)
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
1119125cb0SAndrei Otcheretianski  * Copyright(c) 2018 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
3419125cb0SAndrei Otcheretianski  * Copyright(c) 2018 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 
88e705c121SKalle Valo 	if (!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 	 */
109d4e4aa7cSEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
110e705c121SKalle Valo 		iwl_mvm_unref(mvm, IWL_MVM_REF_ROC);
111d4e4aa7cSEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
112e705c121SKalle Valo 		iwl_mvm_unref(mvm, IWL_MVM_REF_ROC_AUX);
113e705c121SKalle Valo 
114e705c121SKalle Valo 	synchronize_net();
115e705c121SKalle Valo 
116e705c121SKalle Valo 	/*
117e705c121SKalle Valo 	 * Flush the offchannel queue -- this is called when the time
118e705c121SKalle Valo 	 * event finishes or is canceled, so that frames queued for it
119e705c121SKalle Valo 	 * won't get stuck on the queue and be transmitted in the next
120e705c121SKalle Valo 	 * time event.
121e705c121SKalle Valo 	 * We have to send the command asynchronously since this cannot
122e705c121SKalle Valo 	 * be under the mutex for locking reasons, but that's not an
123e705c121SKalle Valo 	 * issue as it will have to complete before the next command is
124e705c121SKalle Valo 	 * executed, and a new time event means a new command.
125e705c121SKalle Valo 	 */
126d49394a1SSara Sharon 	iwl_mvm_flush_sta(mvm, &mvm->aux_sta, true, CMD_ASYNC);
1276c2d49fdSJohannes Berg 
1286c2d49fdSJohannes Berg 	/* Do the same for the P2P device queue (STA) */
1296c2d49fdSJohannes Berg 	if (test_and_clear_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status)) {
1306c2d49fdSJohannes Berg 		struct iwl_mvm_vif *mvmvif;
1316c2d49fdSJohannes Berg 
1326c2d49fdSJohannes Berg 		/*
1336c2d49fdSJohannes Berg 		 * NB: access to this pointer would be racy, but the flush bit
1346c2d49fdSJohannes Berg 		 * can only be set when we had a P2P-Device VIF, and we have a
1356c2d49fdSJohannes Berg 		 * flush of this work in iwl_mvm_prepare_mac_removal() so it's
1366c2d49fdSJohannes Berg 		 * not really racy.
1376c2d49fdSJohannes Berg 		 */
1386c2d49fdSJohannes Berg 
1396c2d49fdSJohannes Berg 		if (!WARN_ON(!mvm->p2p_device_vif)) {
1406c2d49fdSJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(mvm->p2p_device_vif);
1416c2d49fdSJohannes Berg 			iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true,
1426c2d49fdSJohannes Berg 					  CMD_ASYNC);
1436c2d49fdSJohannes Berg 		}
1446c2d49fdSJohannes Berg 	}
145e705c121SKalle Valo }
146e705c121SKalle Valo 
147e705c121SKalle Valo static void iwl_mvm_roc_finished(struct iwl_mvm *mvm)
148e705c121SKalle Valo {
149e705c121SKalle Valo 	/*
150e705c121SKalle Valo 	 * Of course, our status bit is just as racy as mac80211, so in
151e705c121SKalle Valo 	 * addition, fire off the work struct which will drop all frames
152e705c121SKalle Valo 	 * from the hardware queues that made it through the race. First
153e705c121SKalle Valo 	 * it will of course synchronize the TX path to make sure that
154e705c121SKalle Valo 	 * any *new* TX will be rejected.
155e705c121SKalle Valo 	 */
156e705c121SKalle Valo 	schedule_work(&mvm->roc_done_wk);
157e705c121SKalle Valo }
158e705c121SKalle Valo 
159e705c121SKalle Valo static void iwl_mvm_csa_noa_start(struct iwl_mvm *mvm)
160e705c121SKalle Valo {
161e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
162e705c121SKalle Valo 
163e705c121SKalle Valo 	rcu_read_lock();
164e705c121SKalle Valo 
165e705c121SKalle Valo 	csa_vif = rcu_dereference(mvm->csa_vif);
166e705c121SKalle Valo 	if (!csa_vif || !csa_vif->csa_active)
167e705c121SKalle Valo 		goto out_unlock;
168e705c121SKalle Valo 
169e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "CSA NOA started\n");
170e705c121SKalle Valo 
171e705c121SKalle Valo 	/*
172e705c121SKalle Valo 	 * CSA NoA is started but we still have beacons to
173e705c121SKalle Valo 	 * transmit on the current channel.
174e705c121SKalle Valo 	 * So we just do nothing here and the switch
175e705c121SKalle Valo 	 * will be performed on the last TBTT.
176e705c121SKalle Valo 	 */
177e705c121SKalle Valo 	if (!ieee80211_csa_is_complete(csa_vif)) {
178e705c121SKalle Valo 		IWL_WARN(mvm, "CSA NOA started too early\n");
179e705c121SKalle Valo 		goto out_unlock;
180e705c121SKalle Valo 	}
181e705c121SKalle Valo 
182e705c121SKalle Valo 	ieee80211_csa_finish(csa_vif);
183e705c121SKalle Valo 
184e705c121SKalle Valo 	rcu_read_unlock();
185e705c121SKalle Valo 
186e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->csa_vif, NULL);
187e705c121SKalle Valo 
188e705c121SKalle Valo 	return;
189e705c121SKalle Valo 
190e705c121SKalle Valo out_unlock:
191e705c121SKalle Valo 	rcu_read_unlock();
192e705c121SKalle Valo }
193e705c121SKalle Valo 
194e705c121SKalle Valo static bool iwl_mvm_te_check_disconnect(struct iwl_mvm *mvm,
195e705c121SKalle Valo 					struct ieee80211_vif *vif,
196e705c121SKalle Valo 					const char *errmsg)
197e705c121SKalle Valo {
19819125cb0SAndrei Otcheretianski 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
19919125cb0SAndrei Otcheretianski 
200e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION)
201e705c121SKalle Valo 		return false;
20219125cb0SAndrei Otcheretianski 
20319125cb0SAndrei Otcheretianski 	if (!mvmvif->csa_bcn_pending && vif->bss_conf.assoc &&
20419125cb0SAndrei Otcheretianski 	    vif->bss_conf.dtim_period)
205e705c121SKalle Valo 		return false;
206e705c121SKalle Valo 	if (errmsg)
207e705c121SKalle Valo 		IWL_ERR(mvm, "%s\n", errmsg);
208e705c121SKalle Valo 
209e705c121SKalle Valo 	iwl_mvm_connection_loss(mvm, vif, errmsg);
210e705c121SKalle Valo 	return true;
211e705c121SKalle Valo }
212e705c121SKalle Valo 
213e705c121SKalle Valo static void
214e705c121SKalle Valo iwl_mvm_te_handle_notify_csa(struct iwl_mvm *mvm,
215e705c121SKalle Valo 			     struct iwl_mvm_time_event_data *te_data,
216e705c121SKalle Valo 			     struct iwl_time_event_notif *notif)
217e705c121SKalle Valo {
218e705c121SKalle Valo 	struct ieee80211_vif *vif = te_data->vif;
219e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
220e705c121SKalle Valo 
221e705c121SKalle Valo 	if (!notif->status)
222e705c121SKalle Valo 		IWL_DEBUG_TE(mvm, "CSA time event failed to start\n");
223e705c121SKalle Valo 
224e705c121SKalle Valo 	switch (te_data->vif->type) {
225e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
226e705c121SKalle Valo 		if (!notif->status)
227e705c121SKalle Valo 			mvmvif->csa_failed = true;
228e705c121SKalle Valo 		iwl_mvm_csa_noa_start(mvm);
229e705c121SKalle Valo 		break;
230e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
231e705c121SKalle Valo 		if (!notif->status) {
232e705c121SKalle Valo 			iwl_mvm_connection_loss(mvm, vif,
233e705c121SKalle Valo 						"CSA TE failed to start");
234e705c121SKalle Valo 			break;
235e705c121SKalle Valo 		}
236e705c121SKalle Valo 		iwl_mvm_csa_client_absent(mvm, te_data->vif);
237e705c121SKalle Valo 		ieee80211_chswitch_done(te_data->vif, true);
238e705c121SKalle Valo 		break;
239e705c121SKalle Valo 	default:
240e705c121SKalle Valo 		/* should never happen */
241e705c121SKalle Valo 		WARN_ON_ONCE(1);
242e705c121SKalle Valo 		break;
243e705c121SKalle Valo 	}
244e705c121SKalle Valo 
245e705c121SKalle Valo 	/* we don't need it anymore */
246e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, te_data);
247e705c121SKalle Valo }
248e705c121SKalle Valo 
249e705c121SKalle Valo static void iwl_mvm_te_check_trigger(struct iwl_mvm *mvm,
250e705c121SKalle Valo 				     struct iwl_time_event_notif *notif,
251e705c121SKalle Valo 				     struct iwl_mvm_time_event_data *te_data)
252e705c121SKalle Valo {
253e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
254e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_time_event *te_trig;
255e705c121SKalle Valo 	int i;
256e705c121SKalle Valo 
257*6c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
2587174beb6SJohannes Berg 				     ieee80211_vif_to_wdev(te_data->vif),
259*6c042d75SSara Sharon 				     FW_DBG_TRIGGER_TIME_EVENT);
260*6c042d75SSara Sharon 	if (!trig)
261e705c121SKalle Valo 		return;
262e705c121SKalle Valo 
263*6c042d75SSara Sharon 	te_trig = (void *)trig->data;
264*6c042d75SSara Sharon 
265e705c121SKalle Valo 	for (i = 0; i < ARRAY_SIZE(te_trig->time_events); i++) {
266e705c121SKalle Valo 		u32 trig_te_id = le32_to_cpu(te_trig->time_events[i].id);
267e705c121SKalle Valo 		u32 trig_action_bitmap =
268e705c121SKalle Valo 			le32_to_cpu(te_trig->time_events[i].action_bitmap);
269e705c121SKalle Valo 		u32 trig_status_bitmap =
270e705c121SKalle Valo 			le32_to_cpu(te_trig->time_events[i].status_bitmap);
271e705c121SKalle Valo 
272e705c121SKalle Valo 		if (trig_te_id != te_data->id ||
273e705c121SKalle Valo 		    !(trig_action_bitmap & le32_to_cpu(notif->action)) ||
274e705c121SKalle Valo 		    !(trig_status_bitmap & BIT(le32_to_cpu(notif->status))))
275e705c121SKalle Valo 			continue;
276e705c121SKalle Valo 
2777174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
278e705c121SKalle Valo 					"Time event %d Action 0x%x received status: %d",
279e705c121SKalle Valo 					te_data->id,
280e705c121SKalle Valo 					le32_to_cpu(notif->action),
281e705c121SKalle Valo 					le32_to_cpu(notif->status));
282e705c121SKalle Valo 		break;
283e705c121SKalle Valo 	}
284e705c121SKalle Valo }
285e705c121SKalle Valo 
286e705c121SKalle Valo /*
287e705c121SKalle Valo  * Handles a FW notification for an event that is known to the driver.
288e705c121SKalle Valo  *
289e705c121SKalle Valo  * @mvm: the mvm component
290e705c121SKalle Valo  * @te_data: the time event data
291e705c121SKalle Valo  * @notif: the notification data corresponding the time event data.
292e705c121SKalle Valo  */
293e705c121SKalle Valo static void iwl_mvm_te_handle_notif(struct iwl_mvm *mvm,
294e705c121SKalle Valo 				    struct iwl_mvm_time_event_data *te_data,
295e705c121SKalle Valo 				    struct iwl_time_event_notif *notif)
296e705c121SKalle Valo {
297e705c121SKalle Valo 	lockdep_assert_held(&mvm->time_event_lock);
298e705c121SKalle Valo 
299e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "Handle time event notif - UID = 0x%x action %d\n",
300e705c121SKalle Valo 		     le32_to_cpu(notif->unique_id),
301e705c121SKalle Valo 		     le32_to_cpu(notif->action));
302e705c121SKalle Valo 
303e705c121SKalle Valo 	iwl_mvm_te_check_trigger(mvm, notif, te_data);
304e705c121SKalle Valo 
305e705c121SKalle Valo 	/*
306e705c121SKalle Valo 	 * The FW sends the start/end time event notifications even for events
307e705c121SKalle Valo 	 * that it fails to schedule. This is indicated in the status field of
308e705c121SKalle Valo 	 * the notification. This happens in cases that the scheduler cannot
309e705c121SKalle Valo 	 * find a schedule that can handle the event (for example requesting a
310e705c121SKalle Valo 	 * P2P Device discoveribility, while there are other higher priority
311e705c121SKalle Valo 	 * events in the system).
312e705c121SKalle Valo 	 */
313e705c121SKalle Valo 	if (!le32_to_cpu(notif->status)) {
314e705c121SKalle Valo 		const char *msg;
315e705c121SKalle Valo 
316e705c121SKalle Valo 		if (notif->action & cpu_to_le32(TE_V2_NOTIF_HOST_EVENT_START))
317e705c121SKalle Valo 			msg = "Time Event start notification failure";
318e705c121SKalle Valo 		else
319e705c121SKalle Valo 			msg = "Time Event end notification failure";
320e705c121SKalle Valo 
321e705c121SKalle Valo 		IWL_DEBUG_TE(mvm, "%s\n", msg);
322e705c121SKalle Valo 
323e705c121SKalle Valo 		if (iwl_mvm_te_check_disconnect(mvm, te_data->vif, msg)) {
324e705c121SKalle Valo 			iwl_mvm_te_clear_data(mvm, te_data);
325e705c121SKalle Valo 			return;
326e705c121SKalle Valo 		}
327e705c121SKalle Valo 	}
328e705c121SKalle Valo 
329e705c121SKalle Valo 	if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_END) {
330e705c121SKalle Valo 		IWL_DEBUG_TE(mvm,
331e705c121SKalle Valo 			     "TE ended - current time %lu, estimated end %lu\n",
332e705c121SKalle Valo 			     jiffies, te_data->end_jiffies);
333e705c121SKalle Valo 
334e705c121SKalle Valo 		switch (te_data->vif->type) {
335e705c121SKalle Valo 		case NL80211_IFTYPE_P2P_DEVICE:
336e705c121SKalle Valo 			ieee80211_remain_on_channel_expired(mvm->hw);
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 			iwl_mvm_ref(mvm, IWL_MVM_REF_ROC);
359e705c121SKalle Valo 			ieee80211_ready_on_channel(mvm->hw);
360e705c121SKalle Valo 		} else if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
361e705c121SKalle Valo 			iwl_mvm_te_handle_notify_csa(mvm, te_data, notif);
362e705c121SKalle Valo 		}
363e705c121SKalle Valo 	} else {
364e705c121SKalle Valo 		IWL_WARN(mvm, "Got TE with unknown action\n");
365e705c121SKalle Valo 	}
366e705c121SKalle Valo }
367e705c121SKalle Valo 
368e705c121SKalle Valo /*
369e705c121SKalle Valo  * Handle A Aux ROC time event
370e705c121SKalle Valo  */
371e705c121SKalle Valo static int iwl_mvm_aux_roc_te_handle_notif(struct iwl_mvm *mvm,
372e705c121SKalle Valo 					   struct iwl_time_event_notif *notif)
373e705c121SKalle Valo {
374e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data, *tmp;
375e705c121SKalle Valo 	bool aux_roc_te = false;
376e705c121SKalle Valo 
377e705c121SKalle Valo 	list_for_each_entry_safe(te_data, tmp, &mvm->aux_roc_te_list, list) {
378e705c121SKalle Valo 		if (le32_to_cpu(notif->unique_id) == te_data->uid) {
379e705c121SKalle Valo 			aux_roc_te = true;
380e705c121SKalle Valo 			break;
381e705c121SKalle Valo 		}
382e705c121SKalle Valo 	}
383e705c121SKalle Valo 	if (!aux_roc_te) /* Not a Aux ROC time event */
384e705c121SKalle Valo 		return -EINVAL;
385e705c121SKalle Valo 
386e705c121SKalle Valo 	iwl_mvm_te_check_trigger(mvm, notif, te_data);
387e705c121SKalle Valo 
388e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
3895151ad95SMatti Gottlieb 		     "Aux ROC time event notification  - UID = 0x%x action %d (error = %d)\n",
390e705c121SKalle Valo 		     le32_to_cpu(notif->unique_id),
3915151ad95SMatti Gottlieb 		     le32_to_cpu(notif->action), le32_to_cpu(notif->status));
392e705c121SKalle Valo 
3935151ad95SMatti Gottlieb 	if (!le32_to_cpu(notif->status) ||
3945151ad95SMatti Gottlieb 	    le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_END) {
395e705c121SKalle Valo 		/* End TE, notify mac80211 */
396e705c121SKalle Valo 		ieee80211_remain_on_channel_expired(mvm->hw);
397e705c121SKalle Valo 		iwl_mvm_roc_finished(mvm); /* flush aux queue */
398e705c121SKalle Valo 		list_del(&te_data->list); /* remove from list */
399e705c121SKalle Valo 		te_data->running = false;
400e705c121SKalle Valo 		te_data->vif = NULL;
401e705c121SKalle Valo 		te_data->uid = 0;
402e705c121SKalle Valo 		te_data->id = TE_MAX;
403e705c121SKalle Valo 	} else if (le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_START) {
404e705c121SKalle Valo 		set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
405e705c121SKalle Valo 		te_data->running = true;
406e705c121SKalle Valo 		iwl_mvm_ref(mvm, IWL_MVM_REF_ROC_AUX);
407e705c121SKalle Valo 		ieee80211_ready_on_channel(mvm->hw); /* Start TE */
408e705c121SKalle Valo 	} else {
409e705c121SKalle Valo 		IWL_DEBUG_TE(mvm,
410e705c121SKalle Valo 			     "ERROR: Unknown Aux ROC Time Event (action = %d)\n",
411e705c121SKalle Valo 			     le32_to_cpu(notif->action));
412e705c121SKalle Valo 		return -EINVAL;
413e705c121SKalle Valo 	}
414e705c121SKalle Valo 
415e705c121SKalle Valo 	return 0;
416e705c121SKalle Valo }
417e705c121SKalle Valo 
418e705c121SKalle Valo /*
419e705c121SKalle Valo  * The Rx handler for time event notifications
420e705c121SKalle Valo  */
421e705c121SKalle Valo void iwl_mvm_rx_time_event_notif(struct iwl_mvm *mvm,
422e705c121SKalle Valo 				 struct iwl_rx_cmd_buffer *rxb)
423e705c121SKalle Valo {
424e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
425e705c121SKalle Valo 	struct iwl_time_event_notif *notif = (void *)pkt->data;
426e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data, *tmp;
427e705c121SKalle Valo 
428e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "Time event notification - UID = 0x%x action %d\n",
429e705c121SKalle Valo 		     le32_to_cpu(notif->unique_id),
430e705c121SKalle Valo 		     le32_to_cpu(notif->action));
431e705c121SKalle Valo 
432e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
433e705c121SKalle Valo 	/* This time event is triggered for Aux ROC request */
434e705c121SKalle Valo 	if (!iwl_mvm_aux_roc_te_handle_notif(mvm, notif))
435e705c121SKalle Valo 		goto unlock;
436e705c121SKalle Valo 
437e705c121SKalle Valo 	list_for_each_entry_safe(te_data, tmp, &mvm->time_event_list, list) {
438e705c121SKalle Valo 		if (le32_to_cpu(notif->unique_id) == te_data->uid)
439e705c121SKalle Valo 			iwl_mvm_te_handle_notif(mvm, te_data, notif);
440e705c121SKalle Valo 	}
441e705c121SKalle Valo unlock:
442e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
443e705c121SKalle Valo }
444e705c121SKalle Valo 
445e705c121SKalle Valo static bool iwl_mvm_te_notif(struct iwl_notif_wait_data *notif_wait,
446e705c121SKalle Valo 			     struct iwl_rx_packet *pkt, void *data)
447e705c121SKalle Valo {
448e705c121SKalle Valo 	struct iwl_mvm *mvm =
449e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
450e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
451e705c121SKalle Valo 	struct iwl_time_event_notif *resp;
452e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
453e705c121SKalle Valo 
454e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_NOTIFICATION))
455e705c121SKalle Valo 		return true;
456e705c121SKalle Valo 
457e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
458e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid TIME_EVENT_NOTIFICATION response\n");
459e705c121SKalle Valo 		return true;
460e705c121SKalle Valo 	}
461e705c121SKalle Valo 
462e705c121SKalle Valo 	resp = (void *)pkt->data;
463e705c121SKalle Valo 
464e705c121SKalle Valo 	/* te_data->uid is already set in the TIME_EVENT_CMD response */
465e705c121SKalle Valo 	if (le32_to_cpu(resp->unique_id) != te_data->uid)
466e705c121SKalle Valo 		return false;
467e705c121SKalle Valo 
468e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n",
469e705c121SKalle Valo 		     te_data->uid);
470e705c121SKalle Valo 	if (!resp->status)
471e705c121SKalle Valo 		IWL_ERR(mvm,
472e705c121SKalle Valo 			"TIME_EVENT_NOTIFICATION received but not executed\n");
473e705c121SKalle Valo 
474e705c121SKalle Valo 	return true;
475e705c121SKalle Valo }
476e705c121SKalle Valo 
477e705c121SKalle Valo static bool iwl_mvm_time_event_response(struct iwl_notif_wait_data *notif_wait,
478e705c121SKalle Valo 					struct iwl_rx_packet *pkt, void *data)
479e705c121SKalle Valo {
480e705c121SKalle Valo 	struct iwl_mvm *mvm =
481e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
482e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
483e705c121SKalle Valo 	struct iwl_time_event_resp *resp;
484e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
485e705c121SKalle Valo 
486e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_CMD))
487e705c121SKalle Valo 		return true;
488e705c121SKalle Valo 
489e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
490e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid TIME_EVENT_CMD response\n");
491e705c121SKalle Valo 		return true;
492e705c121SKalle Valo 	}
493e705c121SKalle Valo 
494e705c121SKalle Valo 	resp = (void *)pkt->data;
495e705c121SKalle Valo 
496e705c121SKalle Valo 	/* we should never get a response to another TIME_EVENT_CMD here */
497e705c121SKalle Valo 	if (WARN_ON_ONCE(le32_to_cpu(resp->id) != te_data->id))
498e705c121SKalle Valo 		return false;
499e705c121SKalle Valo 
500e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->unique_id);
501e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
502e705c121SKalle Valo 		     te_data->uid);
503e705c121SKalle Valo 	return true;
504e705c121SKalle Valo }
505e705c121SKalle Valo 
506e705c121SKalle Valo static int iwl_mvm_time_event_send_add(struct iwl_mvm *mvm,
507e705c121SKalle Valo 				       struct ieee80211_vif *vif,
508e705c121SKalle Valo 				       struct iwl_mvm_time_event_data *te_data,
509e705c121SKalle Valo 				       struct iwl_time_event_cmd *te_cmd)
510e705c121SKalle Valo {
511e705c121SKalle Valo 	static const u16 time_event_response[] = { TIME_EVENT_CMD };
512e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
513e705c121SKalle Valo 	int ret;
514e705c121SKalle Valo 
515e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
516e705c121SKalle Valo 
517e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "Add new TE, duration %d TU\n",
518e705c121SKalle Valo 		     le32_to_cpu(te_cmd->duration));
519e705c121SKalle Valo 
520e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
521e705c121SKalle Valo 	if (WARN_ON(te_data->id != TE_MAX)) {
522e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
523e705c121SKalle Valo 		return -EIO;
524e705c121SKalle Valo 	}
525e705c121SKalle Valo 	te_data->vif = vif;
526e705c121SKalle Valo 	te_data->duration = le32_to_cpu(te_cmd->duration);
527e705c121SKalle Valo 	te_data->id = le32_to_cpu(te_cmd->id);
528e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->time_event_list);
529e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
530e705c121SKalle Valo 
531e705c121SKalle Valo 	/*
532e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
533e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
534e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
535e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
536e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
537e705c121SKalle Valo 	 * time another notification (that the time event started)
538e705c121SKalle Valo 	 * might already be processed unsuccessfully.
539e705c121SKalle Valo 	 */
540e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
541e705c121SKalle Valo 				   time_event_response,
542e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
543e705c121SKalle Valo 				   iwl_mvm_time_event_response, te_data);
544e705c121SKalle Valo 
545e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
546e705c121SKalle Valo 					    sizeof(*te_cmd), te_cmd);
547e705c121SKalle Valo 	if (ret) {
548e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send TIME_EVENT_CMD: %d\n", ret);
549e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
550e705c121SKalle Valo 		goto out_clear_te;
551e705c121SKalle Valo 	}
552e705c121SKalle Valo 
553e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
554e705c121SKalle Valo 	ret = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
555e705c121SKalle Valo 	/* should never fail */
556e705c121SKalle Valo 	WARN_ON_ONCE(ret);
557e705c121SKalle Valo 
558e705c121SKalle Valo 	if (ret) {
559e705c121SKalle Valo  out_clear_te:
560e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
561e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
562e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
563e705c121SKalle Valo 	}
564e705c121SKalle Valo 	return ret;
565e705c121SKalle Valo }
566e705c121SKalle Valo 
567e705c121SKalle Valo void iwl_mvm_protect_session(struct iwl_mvm *mvm,
568e705c121SKalle Valo 			     struct ieee80211_vif *vif,
569e705c121SKalle Valo 			     u32 duration, u32 min_duration,
570e705c121SKalle Valo 			     u32 max_delay, bool wait_for_notif)
571e705c121SKalle Valo {
572e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
573e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
574e705c121SKalle Valo 	const u16 te_notif_response[] = { TIME_EVENT_NOTIFICATION };
575e705c121SKalle Valo 	struct iwl_notification_wait wait_te_notif;
576e705c121SKalle Valo 	struct iwl_time_event_cmd time_cmd = {};
577e705c121SKalle Valo 
578e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
579e705c121SKalle Valo 
580e705c121SKalle Valo 	if (te_data->running &&
581e705c121SKalle Valo 	    time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
582e705c121SKalle Valo 		IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
583e705c121SKalle Valo 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
584e705c121SKalle Valo 		return;
585e705c121SKalle Valo 	}
586e705c121SKalle Valo 
587e705c121SKalle Valo 	if (te_data->running) {
588e705c121SKalle Valo 		IWL_DEBUG_TE(mvm, "extend 0x%x: only %u ms left\n",
589e705c121SKalle Valo 			     te_data->uid,
590e705c121SKalle Valo 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
591e705c121SKalle Valo 		/*
592e705c121SKalle Valo 		 * we don't have enough time
593e705c121SKalle Valo 		 * cancel the current TE and issue a new one
594e705c121SKalle Valo 		 * Of course it would be better to remove the old one only
595e705c121SKalle Valo 		 * when the new one is added, but we don't care if we are off
596e705c121SKalle Valo 		 * channel for a bit. All we need to do, is not to return
597e705c121SKalle Valo 		 * before we actually begin to be on the channel.
598e705c121SKalle Valo 		 */
599e705c121SKalle Valo 		iwl_mvm_stop_session_protection(mvm, vif);
600e705c121SKalle Valo 	}
601e705c121SKalle Valo 
602e705c121SKalle Valo 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
603e705c121SKalle Valo 	time_cmd.id_and_color =
604e705c121SKalle Valo 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
605e705c121SKalle Valo 	time_cmd.id = cpu_to_le32(TE_BSS_STA_AGGRESSIVE_ASSOC);
606e705c121SKalle Valo 
607e705c121SKalle Valo 	time_cmd.apply_time = cpu_to_le32(0);
608e705c121SKalle Valo 
609e705c121SKalle Valo 	time_cmd.max_frags = TE_V2_FRAG_NONE;
610e705c121SKalle Valo 	time_cmd.max_delay = cpu_to_le32(max_delay);
611e705c121SKalle Valo 	/* TODO: why do we need to interval = bi if it is not periodic? */
612e705c121SKalle Valo 	time_cmd.interval = cpu_to_le32(1);
613e705c121SKalle Valo 	time_cmd.duration = cpu_to_le32(duration);
614e705c121SKalle Valo 	time_cmd.repeat = 1;
615e705c121SKalle Valo 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
616e705c121SKalle Valo 				      TE_V2_NOTIF_HOST_EVENT_END |
61740d53f4aSAndrei Otcheretianski 				      TE_V2_START_IMMEDIATELY);
618e705c121SKalle Valo 
619e705c121SKalle Valo 	if (!wait_for_notif) {
620e705c121SKalle Valo 		iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
621e705c121SKalle Valo 		return;
622e705c121SKalle Valo 	}
623e705c121SKalle Valo 
624e705c121SKalle Valo 	/*
625e705c121SKalle Valo 	 * Create notification_wait for the TIME_EVENT_NOTIFICATION to use
626e705c121SKalle Valo 	 * right after we send the time event
627e705c121SKalle Valo 	 */
628e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_te_notif,
629e705c121SKalle Valo 				   te_notif_response,
630e705c121SKalle Valo 				   ARRAY_SIZE(te_notif_response),
631e705c121SKalle Valo 				   iwl_mvm_te_notif, te_data);
632e705c121SKalle Valo 
633e705c121SKalle Valo 	/* If TE was sent OK - wait for the notification that started */
634e705c121SKalle Valo 	if (iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd)) {
635e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add TE to protect session\n");
636e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_te_notif);
637e705c121SKalle Valo 	} else if (iwl_wait_notification(&mvm->notif_wait, &wait_te_notif,
638e705c121SKalle Valo 					 TU_TO_JIFFIES(max_delay))) {
639e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to protect session until TE\n");
640e705c121SKalle Valo 	}
641e705c121SKalle Valo }
642e705c121SKalle Valo 
643e705c121SKalle Valo static bool __iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
644e705c121SKalle Valo 					struct iwl_mvm_time_event_data *te_data,
645e705c121SKalle Valo 					u32 *uid)
646e705c121SKalle Valo {
647e705c121SKalle Valo 	u32 id;
648e705c121SKalle Valo 
649e705c121SKalle Valo 	/*
650e705c121SKalle Valo 	 * It is possible that by the time we got to this point the time
651e705c121SKalle Valo 	 * event was already removed.
652e705c121SKalle Valo 	 */
653e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
654e705c121SKalle Valo 
655e705c121SKalle Valo 	/* Save time event uid before clearing its data */
656e705c121SKalle Valo 	*uid = te_data->uid;
657e705c121SKalle Valo 	id = te_data->id;
658e705c121SKalle Valo 
659e705c121SKalle Valo 	/*
660e705c121SKalle Valo 	 * The clear_data function handles time events that were already removed
661e705c121SKalle Valo 	 */
662e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, te_data);
663e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
664e705c121SKalle Valo 
665e705c121SKalle Valo 	/*
666e705c121SKalle Valo 	 * It is possible that by the time we try to remove it, the time event
667e705c121SKalle Valo 	 * has already ended and removed. In such a case there is no need to
668e705c121SKalle Valo 	 * send a removal command.
669e705c121SKalle Valo 	 */
670e705c121SKalle Valo 	if (id == TE_MAX) {
671e705c121SKalle Valo 		IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", *uid);
672e705c121SKalle Valo 		return false;
673e705c121SKalle Valo 	}
674e705c121SKalle Valo 
675e705c121SKalle Valo 	return true;
676e705c121SKalle Valo }
677e705c121SKalle Valo 
678e705c121SKalle Valo /*
679e705c121SKalle Valo  * Explicit request to remove a aux roc time event. The removal of a time
680e705c121SKalle Valo  * event needs to be synchronized with the flow of a time event's end
681e705c121SKalle Valo  * notification, which also removes the time event from the op mode
682e705c121SKalle Valo  * data structures.
683e705c121SKalle Valo  */
684e705c121SKalle Valo static void iwl_mvm_remove_aux_roc_te(struct iwl_mvm *mvm,
685e705c121SKalle Valo 				      struct iwl_mvm_vif *mvmvif,
686e705c121SKalle Valo 				      struct iwl_mvm_time_event_data *te_data)
687e705c121SKalle Valo {
688e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_cmd = {};
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,
702e705c121SKalle Valo 				   sizeof(aux_cmd), &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 
737e705c121SKalle Valo void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,
738e705c121SKalle Valo 				     struct ieee80211_vif *vif)
739e705c121SKalle Valo {
740e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
741e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
7423edfb5f4SAvraham Stern 	u32 id;
743e705c121SKalle Valo 
744e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
7453edfb5f4SAvraham Stern 
7463edfb5f4SAvraham Stern 	spin_lock_bh(&mvm->time_event_lock);
7473edfb5f4SAvraham Stern 	id = te_data->id;
7483edfb5f4SAvraham Stern 	spin_unlock_bh(&mvm->time_event_lock);
7493edfb5f4SAvraham Stern 
7503edfb5f4SAvraham Stern 	if (id != TE_BSS_STA_AGGRESSIVE_ASSOC) {
7513edfb5f4SAvraham Stern 		IWL_DEBUG_TE(mvm,
7523edfb5f4SAvraham Stern 			     "don't remove TE with id=%u (not session protection)\n",
7533edfb5f4SAvraham Stern 			     id);
7543edfb5f4SAvraham Stern 		return;
7553edfb5f4SAvraham Stern 	}
7563edfb5f4SAvraham Stern 
757e705c121SKalle Valo 	iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
758e705c121SKalle Valo }
759e705c121SKalle Valo 
760e705c121SKalle Valo int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
761e705c121SKalle Valo 			  int duration, enum ieee80211_roc_type type)
762e705c121SKalle Valo {
763e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
764e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
765e705c121SKalle Valo 	struct iwl_time_event_cmd time_cmd = {};
766e705c121SKalle Valo 
767e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
768e705c121SKalle Valo 	if (te_data->running) {
769e705c121SKalle Valo 		IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");
770e705c121SKalle Valo 		return -EBUSY;
771e705c121SKalle Valo 	}
772e705c121SKalle Valo 
773e705c121SKalle Valo 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
774e705c121SKalle Valo 	time_cmd.id_and_color =
775e705c121SKalle Valo 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
776e705c121SKalle Valo 
777e705c121SKalle Valo 	switch (type) {
778e705c121SKalle Valo 	case IEEE80211_ROC_TYPE_NORMAL:
779e705c121SKalle Valo 		time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);
780e705c121SKalle Valo 		break;
781e705c121SKalle Valo 	case IEEE80211_ROC_TYPE_MGMT_TX:
782e705c121SKalle Valo 		time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);
783e705c121SKalle Valo 		break;
784e705c121SKalle Valo 	default:
785e705c121SKalle Valo 		WARN_ONCE(1, "Got an invalid ROC type\n");
786e705c121SKalle Valo 		return -EINVAL;
787e705c121SKalle Valo 	}
788e705c121SKalle Valo 
789e705c121SKalle Valo 	time_cmd.apply_time = cpu_to_le32(0);
790e705c121SKalle Valo 	time_cmd.interval = cpu_to_le32(1);
791e705c121SKalle Valo 
792e705c121SKalle Valo 	/*
793e705c121SKalle Valo 	 * The P2P Device TEs can have lower priority than other events
794e705c121SKalle Valo 	 * that are being scheduled by the driver/fw, and thus it might not be
795e705c121SKalle Valo 	 * scheduled. To improve the chances of it being scheduled, allow them
796e705c121SKalle Valo 	 * to be fragmented, and in addition allow them to be delayed.
797e705c121SKalle Valo 	 */
798e705c121SKalle Valo 	time_cmd.max_frags = min(MSEC_TO_TU(duration)/50, TE_V2_FRAG_ENDLESS);
799e705c121SKalle Valo 	time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));
800e705c121SKalle Valo 	time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));
801e705c121SKalle Valo 	time_cmd.repeat = 1;
802e705c121SKalle Valo 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
803e705c121SKalle Valo 				      TE_V2_NOTIF_HOST_EVENT_END |
80440d53f4aSAndrei Otcheretianski 				      TE_V2_START_IMMEDIATELY);
805e705c121SKalle Valo 
806e705c121SKalle Valo 	return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
807e705c121SKalle Valo }
808e705c121SKalle Valo 
809305d236eSEliad Peller static struct iwl_mvm_time_event_data *iwl_mvm_get_roc_te(struct iwl_mvm *mvm)
810e705c121SKalle Valo {
811e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data;
812e705c121SKalle Valo 
813e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
814e705c121SKalle Valo 
815e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
816e705c121SKalle Valo 
817e705c121SKalle Valo 	/*
818e705c121SKalle Valo 	 * Iterate over the list of time events and find the time event that is
819e705c121SKalle Valo 	 * associated with a P2P_DEVICE interface.
820e705c121SKalle Valo 	 * This assumes that a P2P_DEVICE interface can have only a single time
821e705c121SKalle Valo 	 * event at any given time and this time event coresponds to a ROC
822e705c121SKalle Valo 	 * request
823e705c121SKalle Valo 	 */
824e705c121SKalle Valo 	list_for_each_entry(te_data, &mvm->time_event_list, list) {
825305d236eSEliad Peller 		if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE)
826305d236eSEliad Peller 			goto out;
827e705c121SKalle Valo 	}
828e705c121SKalle Valo 
829e705c121SKalle Valo 	/* There can only be at most one AUX ROC time event, we just use the
830e705c121SKalle Valo 	 * list to simplify/unify code. Remove it if it exists.
831e705c121SKalle Valo 	 */
832e705c121SKalle Valo 	te_data = list_first_entry_or_null(&mvm->aux_roc_te_list,
833e705c121SKalle Valo 					   struct iwl_mvm_time_event_data,
834e705c121SKalle Valo 					   list);
835305d236eSEliad Peller out:
836e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
837305d236eSEliad Peller 	return te_data;
838305d236eSEliad Peller }
839e705c121SKalle Valo 
840305d236eSEliad Peller void iwl_mvm_cleanup_roc_te(struct iwl_mvm *mvm)
841305d236eSEliad Peller {
842305d236eSEliad Peller 	struct iwl_mvm_time_event_data *te_data;
843305d236eSEliad Peller 	u32 uid;
844305d236eSEliad Peller 
845305d236eSEliad Peller 	te_data = iwl_mvm_get_roc_te(mvm);
846305d236eSEliad Peller 	if (te_data)
847305d236eSEliad Peller 		__iwl_mvm_remove_time_event(mvm, te_data, &uid);
848305d236eSEliad Peller }
849305d236eSEliad Peller 
850305d236eSEliad Peller void iwl_mvm_stop_roc(struct iwl_mvm *mvm)
851305d236eSEliad Peller {
852305d236eSEliad Peller 	struct iwl_mvm_vif *mvmvif;
853305d236eSEliad Peller 	struct iwl_mvm_time_event_data *te_data;
854305d236eSEliad Peller 
855305d236eSEliad Peller 	te_data = iwl_mvm_get_roc_te(mvm);
856305d236eSEliad Peller 	if (!te_data) {
857e705c121SKalle Valo 		IWL_WARN(mvm, "No remain on channel event\n");
858e705c121SKalle Valo 		return;
859e705c121SKalle Valo 	}
860e705c121SKalle Valo 
861305d236eSEliad Peller 	mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
862305d236eSEliad Peller 
8636c2d49fdSJohannes Berg 	if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
864e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
8656c2d49fdSJohannes Berg 		set_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status);
8666c2d49fdSJohannes Berg 	} else {
867e705c121SKalle Valo 		iwl_mvm_remove_aux_roc_te(mvm, mvmvif, te_data);
8686c2d49fdSJohannes Berg 	}
869e705c121SKalle Valo 
870e705c121SKalle Valo 	iwl_mvm_roc_finished(mvm);
871e705c121SKalle Valo }
872e705c121SKalle Valo 
873e705c121SKalle Valo int iwl_mvm_schedule_csa_period(struct iwl_mvm *mvm,
874e705c121SKalle Valo 				struct ieee80211_vif *vif,
875e705c121SKalle Valo 				u32 duration, u32 apply_time)
876e705c121SKalle Valo {
877e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
878e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
879e705c121SKalle Valo 	struct iwl_time_event_cmd time_cmd = {};
880e705c121SKalle Valo 
881e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
882e705c121SKalle Valo 
883e705c121SKalle Valo 	if (te_data->running) {
8843edfb5f4SAvraham Stern 		u32 id;
8853edfb5f4SAvraham Stern 
8863edfb5f4SAvraham Stern 		spin_lock_bh(&mvm->time_event_lock);
8873edfb5f4SAvraham Stern 		id = te_data->id;
8883edfb5f4SAvraham Stern 		spin_unlock_bh(&mvm->time_event_lock);
8893edfb5f4SAvraham Stern 
8903edfb5f4SAvraham Stern 		if (id == TE_CHANNEL_SWITCH_PERIOD) {
891e705c121SKalle Valo 			IWL_DEBUG_TE(mvm, "CS period is already scheduled\n");
892e705c121SKalle Valo 			return -EBUSY;
893e705c121SKalle Valo 		}
894e705c121SKalle Valo 
8953edfb5f4SAvraham Stern 		/*
8963edfb5f4SAvraham Stern 		 * Remove the session protection time event to allow the
8973edfb5f4SAvraham Stern 		 * channel switch. If we got here, we just heard a beacon so
8983edfb5f4SAvraham Stern 		 * the session protection is not needed anymore anyway.
8993edfb5f4SAvraham Stern 		 */
9003edfb5f4SAvraham Stern 		iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
9013edfb5f4SAvraham Stern 	}
9023edfb5f4SAvraham Stern 
903e705c121SKalle Valo 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
904e705c121SKalle Valo 	time_cmd.id_and_color =
905e705c121SKalle Valo 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
906e705c121SKalle Valo 	time_cmd.id = cpu_to_le32(TE_CHANNEL_SWITCH_PERIOD);
907e705c121SKalle Valo 	time_cmd.apply_time = cpu_to_le32(apply_time);
908e705c121SKalle Valo 	time_cmd.max_frags = TE_V2_FRAG_NONE;
909e705c121SKalle Valo 	time_cmd.duration = cpu_to_le32(duration);
910e705c121SKalle Valo 	time_cmd.repeat = 1;
911e705c121SKalle Valo 	time_cmd.interval = cpu_to_le32(1);
912e705c121SKalle Valo 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
913e705c121SKalle Valo 				      TE_V2_ABSENCE);
91440d53f4aSAndrei Otcheretianski 	if (!apply_time)
91540d53f4aSAndrei Otcheretianski 		time_cmd.policy |= cpu_to_le16(TE_V2_START_IMMEDIATELY);
916e705c121SKalle Valo 
917e705c121SKalle Valo 	return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
918e705c121SKalle Valo }
919