xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/time-event.c (revision 9410645520e9b820069761f3450ef6661418e279)
18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
33012477cSMiri Korenblit  * Copyright (C) 2012-2014, 2018-2024 Intel Corporation
48e99ea8dSJohannes Berg  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
58e99ea8dSJohannes Berg  * Copyright (C) 2017 Intel Deutschland GmbH
68e99ea8dSJohannes Berg  */
7e705c121SKalle Valo #include <linux/jiffies.h>
8e705c121SKalle Valo #include <net/mac80211.h>
9e705c121SKalle Valo 
109fca9d5cSJohannes Berg #include "fw/notif-wait.h"
11e705c121SKalle Valo #include "iwl-trans.h"
12e705c121SKalle Valo #include "fw-api.h"
13e705c121SKalle Valo #include "time-event.h"
14e705c121SKalle Valo #include "mvm.h"
15e705c121SKalle Valo #include "iwl-io.h"
16e705c121SKalle Valo #include "iwl-prph.h"
17e705c121SKalle Valo 
18e705c121SKalle Valo /*
19e705c121SKalle Valo  * For the high priority TE use a time event type that has similar priority to
20e705c121SKalle Valo  * the FW's action scan priority.
21e705c121SKalle Valo  */
22e705c121SKalle Valo #define IWL_MVM_ROC_TE_TYPE_NORMAL TE_P2P_DEVICE_DISCOVERABLE
23e705c121SKalle Valo #define IWL_MVM_ROC_TE_TYPE_MGMT_TX TE_P2P_CLIENT_ASSOC
24e705c121SKalle Valo 
iwl_mvm_te_clear_data(struct iwl_mvm * mvm,struct iwl_mvm_time_event_data * te_data)25e705c121SKalle Valo void iwl_mvm_te_clear_data(struct iwl_mvm *mvm,
26e705c121SKalle Valo 			   struct iwl_mvm_time_event_data *te_data)
27e705c121SKalle Valo {
28e705c121SKalle Valo 	lockdep_assert_held(&mvm->time_event_lock);
29e705c121SKalle Valo 
30cee859feSAvraham Stern 	if (!te_data || !te_data->vif)
31e705c121SKalle Valo 		return;
32e705c121SKalle Valo 
33e705c121SKalle Valo 	list_del(&te_data->list);
347b3954a1SIlan Peer 
357b3954a1SIlan Peer 	/*
367b3954a1SIlan Peer 	 * the list is only used for AUX ROC events so make sure it is always
377b3954a1SIlan Peer 	 * initialized
387b3954a1SIlan Peer 	 */
397b3954a1SIlan Peer 	INIT_LIST_HEAD(&te_data->list);
407b3954a1SIlan Peer 
41e705c121SKalle Valo 	te_data->running = false;
42e705c121SKalle Valo 	te_data->uid = 0;
43e705c121SKalle Valo 	te_data->id = TE_MAX;
44e705c121SKalle Valo 	te_data->vif = NULL;
4523673041SMiri Korenblit 	te_data->link_id = -1;
46e705c121SKalle Valo }
47e705c121SKalle Valo 
iwl_mvm_cleanup_roc(struct iwl_mvm * mvm)4835c1bbd9SMiri Korenblit static void iwl_mvm_cleanup_roc(struct iwl_mvm *mvm)
49e705c121SKalle Valo {
505f1fee96SJohannes Berg 	struct ieee80211_vif *bss_vif = iwl_mvm_get_bss_vif(mvm);
51a1efeb82SYedidya Benshimol 	struct ieee80211_vif *vif = mvm->p2p_device_vif;
52a1efeb82SYedidya Benshimol 
53a1efeb82SYedidya Benshimol 	lockdep_assert_held(&mvm->mutex);
54a1efeb82SYedidya Benshimol 
55e705c121SKalle Valo 	/*
5654d96912SShaul Triebitz 	 * Clear the ROC_P2P_RUNNING status bit.
57e705c121SKalle Valo 	 * This will cause the TX path to drop offchannel transmissions.
58e705c121SKalle Valo 	 * That would also be done by mac80211, but it is racy, in particular
5935c1bbd9SMiri Korenblit 	 * in the case that the time event actually completed in the firmware.
6035c1bbd9SMiri Korenblit 	 *
6135c1bbd9SMiri Korenblit 	 * Also flush the offchannel queue -- this is called when the time
62e705c121SKalle Valo 	 * event finishes or is canceled, so that frames queued for it
63e705c121SKalle Valo 	 * won't get stuck on the queue and be transmitted in the next
64e705c121SKalle Valo 	 * time event.
65e705c121SKalle Valo 	 */
6654d96912SShaul Triebitz 	if (test_and_clear_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status)) {
676c2d49fdSJohannes Berg 		struct iwl_mvm_vif *mvmvif;
686c2d49fdSJohannes Berg 
6935c1bbd9SMiri Korenblit 		synchronize_net();
7035c1bbd9SMiri Korenblit 
716c2d49fdSJohannes Berg 		/*
726c2d49fdSJohannes Berg 		 * NB: access to this pointer would be racy, but the flush bit
736c2d49fdSJohannes Berg 		 * can only be set when we had a P2P-Device VIF, and we have a
746c2d49fdSJohannes Berg 		 * flush of this work in iwl_mvm_prepare_mac_removal() so it's
756c2d49fdSJohannes Berg 		 * not really racy.
766c2d49fdSJohannes Berg 		 */
776c2d49fdSJohannes Berg 
78a1efeb82SYedidya Benshimol 		if (!WARN_ON(!vif)) {
7984ef7cbeSIlan Peer 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
8039176296SJohannes Berg 			iwl_mvm_flush_sta(mvm, mvmvif->deflink.bcast_sta.sta_id,
8139176296SJohannes Berg 					  mvmvif->deflink.bcast_sta.tfd_queue_msk);
8284ef7cbeSIlan Peer 
8384ef7cbeSIlan Peer 			if (mvm->mld_api_is_used) {
8484ef7cbeSIlan Peer 				iwl_mvm_mld_rm_bcast_sta(mvm, vif,
8584ef7cbeSIlan Peer 							 &vif->bss_conf);
8684ef7cbeSIlan Peer 
8784ef7cbeSIlan Peer 				iwl_mvm_link_changed(mvm, vif, &vif->bss_conf,
8884ef7cbeSIlan Peer 						     LINK_CONTEXT_MODIFY_ACTIVE,
8984ef7cbeSIlan Peer 						     false);
9084ef7cbeSIlan Peer 			} else {
9184ef7cbeSIlan Peer 				iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
9284ef7cbeSIlan Peer 				iwl_mvm_binding_remove_vif(mvm, vif);
9384ef7cbeSIlan Peer 			}
9484ef7cbeSIlan Peer 
9584ef7cbeSIlan Peer 			/* Do not remove the PHY context as removing and adding
9684ef7cbeSIlan Peer 			 * a PHY context has timing overheads. Leaving it
9784ef7cbeSIlan Peer 			 * configured in FW would be useful in case the next ROC
9884ef7cbeSIlan Peer 			 * is with the same channel.
9984ef7cbeSIlan Peer 			 */
1006c2d49fdSJohannes Berg 		}
101998e1abaSNathan Errera 	}
102998e1abaSNathan Errera 
10354d96912SShaul Triebitz 	/*
10454d96912SShaul Triebitz 	 * P2P AUX ROC and HS2.0 ROC do not run simultaneously.
10554d96912SShaul Triebitz 	 * Clear the ROC_AUX_RUNNING status bit.
10654d96912SShaul Triebitz 	 * This will cause the TX path to drop offchannel transmissions.
10754d96912SShaul Triebitz 	 * That would also be done by mac80211, but it is racy, in particular
10854d96912SShaul Triebitz 	 * in the case that the time event actually completed in the firmware
10954d96912SShaul Triebitz 	 * (which is handled in iwl_mvm_te_handle_notif).
11054d96912SShaul Triebitz 	 */
111998e1abaSNathan Errera 	if (test_and_clear_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status)) {
11235c1bbd9SMiri Korenblit 		synchronize_net();
11335c1bbd9SMiri Korenblit 
11439176296SJohannes Berg 		iwl_mvm_flush_sta(mvm, mvm->aux_sta.sta_id,
11539176296SJohannes Berg 				  mvm->aux_sta.tfd_queue_msk);
116998e1abaSNathan Errera 
1172c2c3647SNathan Errera 		/* In newer version of this command an aux station is added only
1182c2c3647SNathan Errera 		 * in cases of dedicated tx queue and need to be removed in end
119*76364f3eSEmmanuel Grumbach 		 * of use. For the even newer mld api, use the appropriate
120*76364f3eSEmmanuel Grumbach 		 * function.
121*76364f3eSEmmanuel Grumbach 		 */
122*76364f3eSEmmanuel Grumbach 		if (mvm->mld_api_is_used)
123*76364f3eSEmmanuel Grumbach 			iwl_mvm_mld_rm_aux_sta(mvm);
124*76364f3eSEmmanuel Grumbach 		else if (iwl_mvm_has_new_station_api(mvm->fw))
1252c2c3647SNathan Errera 			iwl_mvm_rm_aux_sta(mvm);
1266c2d49fdSJohannes Berg 	}
127a1efeb82SYedidya Benshimol 
128f9068fe4SJohannes Berg 	if (!IS_ERR_OR_NULL(bss_vif))
1295f1fee96SJohannes Berg 		iwl_mvm_unblock_esr(mvm, bss_vif, IWL_MVM_ESR_BLOCKED_ROC);
130a1efeb82SYedidya Benshimol 	mutex_unlock(&mvm->mutex);
13135c1bbd9SMiri Korenblit }
132f9084775SNathan Errera 
iwl_mvm_roc_done_wk(struct work_struct * wk)13335c1bbd9SMiri Korenblit void iwl_mvm_roc_done_wk(struct work_struct *wk)
13435c1bbd9SMiri Korenblit {
13535c1bbd9SMiri Korenblit 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, roc_done_wk);
13635c1bbd9SMiri Korenblit 
13735c1bbd9SMiri Korenblit 	mutex_lock(&mvm->mutex);
138a1efeb82SYedidya Benshimol 	/* Mutex is released inside */
13935c1bbd9SMiri Korenblit 	iwl_mvm_cleanup_roc(mvm);
140e705c121SKalle Valo }
141e705c121SKalle Valo 
iwl_mvm_roc_finished(struct iwl_mvm * mvm)142e705c121SKalle Valo static void iwl_mvm_roc_finished(struct iwl_mvm *mvm)
143e705c121SKalle Valo {
144e705c121SKalle Valo 	/*
145e705c121SKalle Valo 	 * Of course, our status bit is just as racy as mac80211, so in
146e705c121SKalle Valo 	 * addition, fire off the work struct which will drop all frames
147e705c121SKalle Valo 	 * from the hardware queues that made it through the race. First
148e705c121SKalle Valo 	 * it will of course synchronize the TX path to make sure that
149e705c121SKalle Valo 	 * any *new* TX will be rejected.
150e705c121SKalle Valo 	 */
151e705c121SKalle Valo 	schedule_work(&mvm->roc_done_wk);
152e705c121SKalle Valo }
153e705c121SKalle Valo 
iwl_mvm_csa_noa_start(struct iwl_mvm * mvm)154e705c121SKalle Valo static void iwl_mvm_csa_noa_start(struct iwl_mvm *mvm)
155e705c121SKalle Valo {
156e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
157e705c121SKalle Valo 
158e705c121SKalle Valo 	rcu_read_lock();
159e705c121SKalle Valo 
160e705c121SKalle Valo 	csa_vif = rcu_dereference(mvm->csa_vif);
161d0a9123eSJohannes Berg 	if (!csa_vif || !csa_vif->bss_conf.csa_active)
162e705c121SKalle Valo 		goto out_unlock;
163e705c121SKalle Valo 
164e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "CSA NOA started\n");
165e705c121SKalle Valo 
166e705c121SKalle Valo 	/*
167e705c121SKalle Valo 	 * CSA NoA is started but we still have beacons to
168e705c121SKalle Valo 	 * transmit on the current channel.
169e705c121SKalle Valo 	 * So we just do nothing here and the switch
170e705c121SKalle Valo 	 * will be performed on the last TBTT.
171e705c121SKalle Valo 	 */
1726030b3a4SAditya Kumar Singh 	if (!ieee80211_beacon_cntdwn_is_complete(csa_vif, 0)) {
173e705c121SKalle Valo 		IWL_WARN(mvm, "CSA NOA started too early\n");
174e705c121SKalle Valo 		goto out_unlock;
175e705c121SKalle Valo 	}
176e705c121SKalle Valo 
17704ada859SAditya Kumar Singh 	ieee80211_csa_finish(csa_vif, 0);
178e705c121SKalle Valo 
179e705c121SKalle Valo 	rcu_read_unlock();
180e705c121SKalle Valo 
181e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->csa_vif, NULL);
182e705c121SKalle Valo 
183e705c121SKalle Valo 	return;
184e705c121SKalle Valo 
185e705c121SKalle Valo out_unlock:
186e705c121SKalle Valo 	rcu_read_unlock();
187e705c121SKalle Valo }
188e705c121SKalle Valo 
iwl_mvm_te_check_disconnect(struct iwl_mvm * mvm,struct ieee80211_vif * vif,const char * errmsg)189e705c121SKalle Valo static bool iwl_mvm_te_check_disconnect(struct iwl_mvm *mvm,
190e705c121SKalle Valo 					struct ieee80211_vif *vif,
191e705c121SKalle Valo 					const char *errmsg)
192e705c121SKalle Valo {
19319125cb0SAndrei Otcheretianski 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
19419125cb0SAndrei Otcheretianski 
195e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION)
196e705c121SKalle Valo 		return false;
19719125cb0SAndrei Otcheretianski 
198f276e20bSJohannes Berg 	if (!mvmvif->csa_bcn_pending && vif->cfg.assoc &&
19919125cb0SAndrei Otcheretianski 	    vif->bss_conf.dtim_period)
200e705c121SKalle Valo 		return false;
201e705c121SKalle Valo 	if (errmsg)
202e705c121SKalle Valo 		IWL_ERR(mvm, "%s\n", errmsg);
203e705c121SKalle Valo 
2047686fd52SSara Sharon 	if (mvmvif->csa_bcn_pending) {
2057686fd52SSara Sharon 		struct iwl_mvm_sta *mvmsta;
2067686fd52SSara Sharon 
2077686fd52SSara Sharon 		rcu_read_lock();
208650cadb7SGregory Greenman 		mvmsta = iwl_mvm_sta_from_staid_rcu(mvm,
209650cadb7SGregory Greenman 						    mvmvif->deflink.ap_sta_id);
2107686fd52SSara Sharon 		if (!WARN_ON(!mvmsta))
2117686fd52SSara Sharon 			iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
2127686fd52SSara Sharon 		rcu_read_unlock();
2137686fd52SSara Sharon 	}
2147686fd52SSara Sharon 
215f276e20bSJohannes Berg 	if (vif->cfg.assoc) {
216b537ffb6SShaul Triebitz 		/*
217b537ffb6SShaul Triebitz 		 * When not associated, this will be called from
218b537ffb6SShaul Triebitz 		 * iwl_mvm_event_mlme_callback_ini()
219b537ffb6SShaul Triebitz 		 */
220b537ffb6SShaul Triebitz 		iwl_dbg_tlv_time_point(&mvm->fwrt,
221b537ffb6SShaul Triebitz 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
222b537ffb6SShaul Triebitz 				       NULL);
2238c626172SJohannes Berg 
2248c626172SJohannes Berg 		mvmvif->session_prot_connection_loss = true;
225b537ffb6SShaul Triebitz 	}
226b537ffb6SShaul Triebitz 
227e705c121SKalle Valo 	iwl_mvm_connection_loss(mvm, vif, errmsg);
228e705c121SKalle Valo 	return true;
229e705c121SKalle Valo }
230e705c121SKalle Valo 
231e705c121SKalle Valo static void
iwl_mvm_te_handle_notify_csa(struct iwl_mvm * mvm,struct iwl_mvm_time_event_data * te_data,struct iwl_time_event_notif * notif)232e705c121SKalle Valo iwl_mvm_te_handle_notify_csa(struct iwl_mvm *mvm,
233e705c121SKalle Valo 			     struct iwl_mvm_time_event_data *te_data,
234e705c121SKalle Valo 			     struct iwl_time_event_notif *notif)
235e705c121SKalle Valo {
236e705c121SKalle Valo 	struct ieee80211_vif *vif = te_data->vif;
237e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
238e705c121SKalle Valo 
239e705c121SKalle Valo 	if (!notif->status)
240e705c121SKalle Valo 		IWL_DEBUG_TE(mvm, "CSA time event failed to start\n");
241e705c121SKalle Valo 
242e705c121SKalle Valo 	switch (te_data->vif->type) {
243e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
244e705c121SKalle Valo 		if (!notif->status)
245e705c121SKalle Valo 			mvmvif->csa_failed = true;
246e705c121SKalle Valo 		iwl_mvm_csa_noa_start(mvm);
247e705c121SKalle Valo 		break;
248e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
249e705c121SKalle Valo 		if (!notif->status) {
250e705c121SKalle Valo 			iwl_mvm_connection_loss(mvm, vif,
251e705c121SKalle Valo 						"CSA TE failed to start");
252e705c121SKalle Valo 			break;
253e705c121SKalle Valo 		}
254e705c121SKalle Valo 		iwl_mvm_csa_client_absent(mvm, te_data->vif);
25511af74adSAndrei Otcheretianski 		cancel_delayed_work(&mvmvif->csa_work);
256a469a593SEmmanuel Grumbach 		ieee80211_chswitch_done(te_data->vif, true, 0);
257e705c121SKalle Valo 		break;
258e705c121SKalle Valo 	default:
259e705c121SKalle Valo 		/* should never happen */
260e705c121SKalle Valo 		WARN_ON_ONCE(1);
261e705c121SKalle Valo 		break;
262e705c121SKalle Valo 	}
263e705c121SKalle Valo 
264e705c121SKalle Valo 	/* we don't need it anymore */
265e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, te_data);
266e705c121SKalle Valo }
267e705c121SKalle Valo 
iwl_mvm_te_check_trigger(struct iwl_mvm * mvm,struct iwl_time_event_notif * notif,struct iwl_mvm_time_event_data * te_data)268e705c121SKalle Valo static void iwl_mvm_te_check_trigger(struct iwl_mvm *mvm,
269e705c121SKalle Valo 				     struct iwl_time_event_notif *notif,
270e705c121SKalle Valo 				     struct iwl_mvm_time_event_data *te_data)
271e705c121SKalle Valo {
272e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
273e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_time_event *te_trig;
274e705c121SKalle Valo 	int i;
275e705c121SKalle Valo 
2766c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
2777174beb6SJohannes Berg 				     ieee80211_vif_to_wdev(te_data->vif),
2786c042d75SSara Sharon 				     FW_DBG_TRIGGER_TIME_EVENT);
2796c042d75SSara Sharon 	if (!trig)
280e705c121SKalle Valo 		return;
281e705c121SKalle Valo 
2826c042d75SSara Sharon 	te_trig = (void *)trig->data;
2836c042d75SSara Sharon 
284e705c121SKalle Valo 	for (i = 0; i < ARRAY_SIZE(te_trig->time_events); i++) {
285e705c121SKalle Valo 		u32 trig_te_id = le32_to_cpu(te_trig->time_events[i].id);
286e705c121SKalle Valo 		u32 trig_action_bitmap =
287e705c121SKalle Valo 			le32_to_cpu(te_trig->time_events[i].action_bitmap);
288e705c121SKalle Valo 		u32 trig_status_bitmap =
289e705c121SKalle Valo 			le32_to_cpu(te_trig->time_events[i].status_bitmap);
290e705c121SKalle Valo 
291e705c121SKalle Valo 		if (trig_te_id != te_data->id ||
292e705c121SKalle Valo 		    !(trig_action_bitmap & le32_to_cpu(notif->action)) ||
293e705c121SKalle Valo 		    !(trig_status_bitmap & BIT(le32_to_cpu(notif->status))))
294e705c121SKalle Valo 			continue;
295e705c121SKalle Valo 
2967174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
297e705c121SKalle Valo 					"Time event %d Action 0x%x received status: %d",
298e705c121SKalle Valo 					te_data->id,
299e705c121SKalle Valo 					le32_to_cpu(notif->action),
300e705c121SKalle Valo 					le32_to_cpu(notif->status));
301e705c121SKalle Valo 		break;
302e705c121SKalle Valo 	}
303e705c121SKalle Valo }
304e705c121SKalle Valo 
305e705c121SKalle Valo /*
306e705c121SKalle Valo  * Handles a FW notification for an event that is known to the driver.
307e705c121SKalle Valo  *
308e705c121SKalle Valo  * @mvm: the mvm component
309e705c121SKalle Valo  * @te_data: the time event data
310e705c121SKalle Valo  * @notif: the notification data corresponding the time event data.
311e705c121SKalle Valo  */
iwl_mvm_te_handle_notif(struct iwl_mvm * mvm,struct iwl_mvm_time_event_data * te_data,struct iwl_time_event_notif * notif)312e705c121SKalle Valo static void iwl_mvm_te_handle_notif(struct iwl_mvm *mvm,
313e705c121SKalle Valo 				    struct iwl_mvm_time_event_data *te_data,
314e705c121SKalle Valo 				    struct iwl_time_event_notif *notif)
315e705c121SKalle Valo {
316e705c121SKalle Valo 	lockdep_assert_held(&mvm->time_event_lock);
317e705c121SKalle Valo 
318e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "Handle time event notif - UID = 0x%x action %d\n",
319e705c121SKalle Valo 		     le32_to_cpu(notif->unique_id),
320e705c121SKalle Valo 		     le32_to_cpu(notif->action));
321e705c121SKalle Valo 
322e705c121SKalle Valo 	iwl_mvm_te_check_trigger(mvm, notif, te_data);
323e705c121SKalle Valo 
324e705c121SKalle Valo 	/*
325e705c121SKalle Valo 	 * The FW sends the start/end time event notifications even for events
326e705c121SKalle Valo 	 * that it fails to schedule. This is indicated in the status field of
327e705c121SKalle Valo 	 * the notification. This happens in cases that the scheduler cannot
328e705c121SKalle Valo 	 * find a schedule that can handle the event (for example requesting a
329e705c121SKalle Valo 	 * P2P Device discoveribility, while there are other higher priority
330e705c121SKalle Valo 	 * events in the system).
331e705c121SKalle Valo 	 */
332e705c121SKalle Valo 	if (!le32_to_cpu(notif->status)) {
333e705c121SKalle Valo 		const char *msg;
334e705c121SKalle Valo 
335e705c121SKalle Valo 		if (notif->action & cpu_to_le32(TE_V2_NOTIF_HOST_EVENT_START))
336e705c121SKalle Valo 			msg = "Time Event start notification failure";
337e705c121SKalle Valo 		else
338e705c121SKalle Valo 			msg = "Time Event end notification failure";
339e705c121SKalle Valo 
340e705c121SKalle Valo 		IWL_DEBUG_TE(mvm, "%s\n", msg);
341e705c121SKalle Valo 
342e705c121SKalle Valo 		if (iwl_mvm_te_check_disconnect(mvm, te_data->vif, msg)) {
343e705c121SKalle Valo 			iwl_mvm_te_clear_data(mvm, te_data);
344e705c121SKalle Valo 			return;
345e705c121SKalle Valo 		}
346e705c121SKalle Valo 	}
347e705c121SKalle Valo 
348e705c121SKalle Valo 	if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_END) {
349e705c121SKalle Valo 		IWL_DEBUG_TE(mvm,
350e705c121SKalle Valo 			     "TE ended - current time %lu, estimated end %lu\n",
351e705c121SKalle Valo 			     jiffies, te_data->end_jiffies);
352e705c121SKalle Valo 
353e705c121SKalle Valo 		switch (te_data->vif->type) {
354e705c121SKalle Valo 		case NL80211_IFTYPE_P2P_DEVICE:
355e705c121SKalle Valo 			ieee80211_remain_on_channel_expired(mvm->hw);
35635c1bbd9SMiri Korenblit 			iwl_mvm_roc_finished(mvm);
357e705c121SKalle Valo 			break;
358e705c121SKalle Valo 		case NL80211_IFTYPE_STATION:
359e705c121SKalle Valo 			/*
360cc61d3ceSEmmanuel Grumbach 			 * If we are switching channel, don't disconnect
361cc61d3ceSEmmanuel Grumbach 			 * if the time event is already done. Beacons can
362cc61d3ceSEmmanuel Grumbach 			 * be delayed a bit after the switch.
363cc61d3ceSEmmanuel Grumbach 			 */
364cc61d3ceSEmmanuel Grumbach 			if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
365cc61d3ceSEmmanuel Grumbach 				IWL_DEBUG_TE(mvm,
366cc61d3ceSEmmanuel Grumbach 					     "No beacon heard and the CS time event is over, don't disconnect\n");
367cc61d3ceSEmmanuel Grumbach 				break;
368cc61d3ceSEmmanuel Grumbach 			}
369cc61d3ceSEmmanuel Grumbach 
370cc61d3ceSEmmanuel Grumbach 			/*
371e705c121SKalle Valo 			 * By now, we should have finished association
372e705c121SKalle Valo 			 * and know the dtim period.
373e705c121SKalle Valo 			 */
374e705c121SKalle Valo 			iwl_mvm_te_check_disconnect(mvm, te_data->vif,
375f276e20bSJohannes Berg 				!te_data->vif->cfg.assoc ?
376976ac0afSShaul Triebitz 				"Not associated and the time event is over already..." :
37719125cb0SAndrei Otcheretianski 				"No beacon heard and the time event is over already...");
378e705c121SKalle Valo 			break;
379e705c121SKalle Valo 		default:
380e705c121SKalle Valo 			break;
381e705c121SKalle Valo 		}
382e705c121SKalle Valo 
383e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
384e705c121SKalle Valo 	} else if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_START) {
385e705c121SKalle Valo 		te_data->running = true;
386e705c121SKalle Valo 		te_data->end_jiffies = TU_TO_EXP_TIME(te_data->duration);
387e705c121SKalle Valo 
388e705c121SKalle Valo 		if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
38954d96912SShaul Triebitz 			set_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status);
390e705c121SKalle Valo 			ieee80211_ready_on_channel(mvm->hw);
391e705c121SKalle Valo 		} else if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
392e705c121SKalle Valo 			iwl_mvm_te_handle_notify_csa(mvm, te_data, notif);
393e705c121SKalle Valo 		}
394e705c121SKalle Valo 	} else {
395e705c121SKalle Valo 		IWL_WARN(mvm, "Got TE with unknown action\n");
396e705c121SKalle Valo 	}
397e705c121SKalle Valo }
398e705c121SKalle Valo 
39954d96912SShaul Triebitz struct iwl_mvm_rx_roc_iterator_data {
40054d96912SShaul Triebitz 	u32 activity;
40154d96912SShaul Triebitz 	bool end_activity;
40254d96912SShaul Triebitz 	bool found;
40354d96912SShaul Triebitz };
40454d96912SShaul Triebitz 
iwl_mvm_rx_roc_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)40554d96912SShaul Triebitz static void iwl_mvm_rx_roc_iterator(void *_data, u8 *mac,
40654d96912SShaul Triebitz 				    struct ieee80211_vif *vif)
40754d96912SShaul Triebitz {
40854d96912SShaul Triebitz 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
40954d96912SShaul Triebitz 	struct iwl_mvm_rx_roc_iterator_data *data = _data;
41054d96912SShaul Triebitz 
41154d96912SShaul Triebitz 	if (mvmvif->roc_activity == data->activity) {
41254d96912SShaul Triebitz 		data->found = true;
41354d96912SShaul Triebitz 		if (data->end_activity)
41454d96912SShaul Triebitz 			mvmvif->roc_activity = ROC_NUM_ACTIVITIES;
41554d96912SShaul Triebitz 	}
41654d96912SShaul Triebitz }
41754d96912SShaul Triebitz 
iwl_mvm_rx_roc_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)41867ac248eSShaul Triebitz void iwl_mvm_rx_roc_notif(struct iwl_mvm *mvm,
41967ac248eSShaul Triebitz 			  struct iwl_rx_cmd_buffer *rxb)
42067ac248eSShaul Triebitz {
42167ac248eSShaul Triebitz 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
42267ac248eSShaul Triebitz 	struct iwl_roc_notif *notif = (void *)pkt->data;
42354d96912SShaul Triebitz 	u32 activity = le32_to_cpu(notif->activity);
42454d96912SShaul Triebitz 	bool started = le32_to_cpu(notif->success) &&
42554d96912SShaul Triebitz 		le32_to_cpu(notif->started);
42654d96912SShaul Triebitz 	struct iwl_mvm_rx_roc_iterator_data data = {
42754d96912SShaul Triebitz 		.activity = activity,
42854d96912SShaul Triebitz 		.end_activity = !started,
42954d96912SShaul Triebitz 	};
43067ac248eSShaul Triebitz 
43154d96912SShaul Triebitz 	/* Clear vif roc_activity if done (set to ROC_NUM_ACTIVITIES) */
43254d96912SShaul Triebitz 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
43354d96912SShaul Triebitz 						   IEEE80211_IFACE_ITER_NORMAL,
43454d96912SShaul Triebitz 						   iwl_mvm_rx_roc_iterator,
43554d96912SShaul Triebitz 						   &data);
43654d96912SShaul Triebitz 	/*
43754d96912SShaul Triebitz 	 * It is possible that the ROC was canceled
43854d96912SShaul Triebitz 	 * but the notification was already fired.
43954d96912SShaul Triebitz 	 */
44054d96912SShaul Triebitz 	if (!data.found)
44154d96912SShaul Triebitz 		return;
44254d96912SShaul Triebitz 
44354d96912SShaul Triebitz 	if (started) {
44467ac248eSShaul Triebitz 		set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
44567ac248eSShaul Triebitz 		ieee80211_ready_on_channel(mvm->hw);
44667ac248eSShaul Triebitz 	} else {
44767ac248eSShaul Triebitz 		iwl_mvm_roc_finished(mvm);
44867ac248eSShaul Triebitz 		ieee80211_remain_on_channel_expired(mvm->hw);
44967ac248eSShaul Triebitz 	}
45067ac248eSShaul Triebitz }
45167ac248eSShaul Triebitz 
452e705c121SKalle Valo /*
453e705c121SKalle Valo  * Handle A Aux ROC time event
454e705c121SKalle Valo  */
iwl_mvm_aux_roc_te_handle_notif(struct iwl_mvm * mvm,struct iwl_time_event_notif * notif)455e705c121SKalle Valo static int iwl_mvm_aux_roc_te_handle_notif(struct iwl_mvm *mvm,
456e705c121SKalle Valo 					   struct iwl_time_event_notif *notif)
457e705c121SKalle Valo {
4586d7cb4a6SJakob Koschel 	struct iwl_mvm_time_event_data *aux_roc_te = NULL, *te_data;
459e705c121SKalle Valo 
4606d7cb4a6SJakob Koschel 	list_for_each_entry(te_data, &mvm->aux_roc_te_list, list) {
461e705c121SKalle Valo 		if (le32_to_cpu(notif->unique_id) == te_data->uid) {
4626d7cb4a6SJakob Koschel 			aux_roc_te = te_data;
463e705c121SKalle Valo 			break;
464e705c121SKalle Valo 		}
465e705c121SKalle Valo 	}
466e705c121SKalle Valo 	if (!aux_roc_te) /* Not a Aux ROC time event */
467e705c121SKalle Valo 		return -EINVAL;
468e705c121SKalle Valo 
469e705c121SKalle Valo 	iwl_mvm_te_check_trigger(mvm, notif, te_data);
470e705c121SKalle Valo 
471e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
4725151ad95SMatti Gottlieb 		     "Aux ROC time event notification  - UID = 0x%x action %d (error = %d)\n",
473e705c121SKalle Valo 		     le32_to_cpu(notif->unique_id),
4745151ad95SMatti Gottlieb 		     le32_to_cpu(notif->action), le32_to_cpu(notif->status));
475e705c121SKalle Valo 
4765151ad95SMatti Gottlieb 	if (!le32_to_cpu(notif->status) ||
4775151ad95SMatti Gottlieb 	    le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_END) {
478e705c121SKalle Valo 		/* End TE, notify mac80211 */
479e705c121SKalle Valo 		ieee80211_remain_on_channel_expired(mvm->hw);
480e705c121SKalle Valo 		iwl_mvm_roc_finished(mvm); /* flush aux queue */
481e705c121SKalle Valo 		list_del(&te_data->list); /* remove from list */
482e705c121SKalle Valo 		te_data->running = false;
483e705c121SKalle Valo 		te_data->vif = NULL;
484e705c121SKalle Valo 		te_data->uid = 0;
485e705c121SKalle Valo 		te_data->id = TE_MAX;
486e705c121SKalle Valo 	} else if (le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_START) {
487e705c121SKalle Valo 		set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
488e705c121SKalle Valo 		te_data->running = true;
489e705c121SKalle Valo 		ieee80211_ready_on_channel(mvm->hw); /* Start TE */
490e705c121SKalle Valo 	} else {
491e705c121SKalle Valo 		IWL_DEBUG_TE(mvm,
492e705c121SKalle Valo 			     "ERROR: Unknown Aux ROC Time Event (action = %d)\n",
493e705c121SKalle Valo 			     le32_to_cpu(notif->action));
494e705c121SKalle Valo 		return -EINVAL;
495e705c121SKalle Valo 	}
496e705c121SKalle Valo 
497e705c121SKalle Valo 	return 0;
498e705c121SKalle Valo }
499e705c121SKalle Valo 
500e705c121SKalle Valo /*
501e705c121SKalle Valo  * The Rx handler for time event notifications
502e705c121SKalle Valo  */
iwl_mvm_rx_time_event_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)503e705c121SKalle Valo void iwl_mvm_rx_time_event_notif(struct iwl_mvm *mvm,
504e705c121SKalle Valo 				 struct iwl_rx_cmd_buffer *rxb)
505e705c121SKalle Valo {
506e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
507e705c121SKalle Valo 	struct iwl_time_event_notif *notif = (void *)pkt->data;
508e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data, *tmp;
509e705c121SKalle Valo 
510e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "Time event notification - UID = 0x%x action %d\n",
511e705c121SKalle Valo 		     le32_to_cpu(notif->unique_id),
512e705c121SKalle Valo 		     le32_to_cpu(notif->action));
513e705c121SKalle Valo 
514e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
515e705c121SKalle Valo 	/* This time event is triggered for Aux ROC request */
516e705c121SKalle Valo 	if (!iwl_mvm_aux_roc_te_handle_notif(mvm, notif))
517e705c121SKalle Valo 		goto unlock;
518e705c121SKalle Valo 
519e705c121SKalle Valo 	list_for_each_entry_safe(te_data, tmp, &mvm->time_event_list, list) {
520e705c121SKalle Valo 		if (le32_to_cpu(notif->unique_id) == te_data->uid)
521e705c121SKalle Valo 			iwl_mvm_te_handle_notif(mvm, te_data, notif);
522e705c121SKalle Valo 	}
523e705c121SKalle Valo unlock:
524e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
525e705c121SKalle Valo }
526e705c121SKalle Valo 
iwl_mvm_te_notif(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)527e705c121SKalle Valo static bool iwl_mvm_te_notif(struct iwl_notif_wait_data *notif_wait,
528e705c121SKalle Valo 			     struct iwl_rx_packet *pkt, void *data)
529e705c121SKalle Valo {
530e705c121SKalle Valo 	struct iwl_mvm *mvm =
531e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
532e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
533e705c121SKalle Valo 	struct iwl_time_event_notif *resp;
534e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
535e705c121SKalle Valo 
536e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_NOTIFICATION))
537e705c121SKalle Valo 		return true;
538e705c121SKalle Valo 
539e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
540e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid TIME_EVENT_NOTIFICATION response\n");
541e705c121SKalle Valo 		return true;
542e705c121SKalle Valo 	}
543e705c121SKalle Valo 
544e705c121SKalle Valo 	resp = (void *)pkt->data;
545e705c121SKalle Valo 
546e705c121SKalle Valo 	/* te_data->uid is already set in the TIME_EVENT_CMD response */
547e705c121SKalle Valo 	if (le32_to_cpu(resp->unique_id) != te_data->uid)
548e705c121SKalle Valo 		return false;
549e705c121SKalle Valo 
550e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n",
551e705c121SKalle Valo 		     te_data->uid);
552e705c121SKalle Valo 	if (!resp->status)
553e705c121SKalle Valo 		IWL_ERR(mvm,
554e705c121SKalle Valo 			"TIME_EVENT_NOTIFICATION received but not executed\n");
555e705c121SKalle Valo 
556e705c121SKalle Valo 	return true;
557e705c121SKalle Valo }
558e705c121SKalle Valo 
iwl_mvm_time_event_response(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)559e705c121SKalle Valo static bool iwl_mvm_time_event_response(struct iwl_notif_wait_data *notif_wait,
560e705c121SKalle Valo 					struct iwl_rx_packet *pkt, void *data)
561e705c121SKalle Valo {
562e705c121SKalle Valo 	struct iwl_mvm *mvm =
563e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
564e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
565e705c121SKalle Valo 	struct iwl_time_event_resp *resp;
566e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
567e705c121SKalle Valo 
568e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_CMD))
569e705c121SKalle Valo 		return true;
570e705c121SKalle Valo 
571e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
572e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid TIME_EVENT_CMD response\n");
573e705c121SKalle Valo 		return true;
574e705c121SKalle Valo 	}
575e705c121SKalle Valo 
576e705c121SKalle Valo 	resp = (void *)pkt->data;
577e705c121SKalle Valo 
578e705c121SKalle Valo 	/* we should never get a response to another TIME_EVENT_CMD here */
579e705c121SKalle Valo 	if (WARN_ON_ONCE(le32_to_cpu(resp->id) != te_data->id))
580e705c121SKalle Valo 		return false;
581e705c121SKalle Valo 
582e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->unique_id);
583e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
584e705c121SKalle Valo 		     te_data->uid);
585e705c121SKalle Valo 	return true;
586e705c121SKalle Valo }
587e705c121SKalle Valo 
iwl_mvm_time_event_send_add(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mvm_time_event_data * te_data,struct iwl_time_event_cmd * te_cmd)588e705c121SKalle Valo static int iwl_mvm_time_event_send_add(struct iwl_mvm *mvm,
589e705c121SKalle Valo 				       struct ieee80211_vif *vif,
590e705c121SKalle Valo 				       struct iwl_mvm_time_event_data *te_data,
591e705c121SKalle Valo 				       struct iwl_time_event_cmd *te_cmd)
592e705c121SKalle Valo {
593e705c121SKalle Valo 	static const u16 time_event_response[] = { TIME_EVENT_CMD };
594e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
595e705c121SKalle Valo 	int ret;
596e705c121SKalle Valo 
597e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
598e705c121SKalle Valo 
599e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "Add new TE, duration %d TU\n",
600e705c121SKalle Valo 		     le32_to_cpu(te_cmd->duration));
601e705c121SKalle Valo 
602e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
603e705c121SKalle Valo 	if (WARN_ON(te_data->id != TE_MAX)) {
604e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
605e705c121SKalle Valo 		return -EIO;
606e705c121SKalle Valo 	}
607e705c121SKalle Valo 	te_data->vif = vif;
608e705c121SKalle Valo 	te_data->duration = le32_to_cpu(te_cmd->duration);
609e705c121SKalle Valo 	te_data->id = le32_to_cpu(te_cmd->id);
610e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->time_event_list);
611e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
612e705c121SKalle Valo 
613e705c121SKalle Valo 	/*
614e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
615e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
616e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
617e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
618e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
619e705c121SKalle Valo 	 * time another notification (that the time event started)
620e705c121SKalle Valo 	 * might already be processed unsuccessfully.
621e705c121SKalle Valo 	 */
622e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
623e705c121SKalle Valo 				   time_event_response,
624e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
625e705c121SKalle Valo 				   iwl_mvm_time_event_response, te_data);
626e705c121SKalle Valo 
627e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
628e705c121SKalle Valo 					    sizeof(*te_cmd), te_cmd);
629e705c121SKalle Valo 	if (ret) {
630e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send TIME_EVENT_CMD: %d\n", ret);
631e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
632e705c121SKalle Valo 		goto out_clear_te;
633e705c121SKalle Valo 	}
634e705c121SKalle Valo 
635e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
636e705c121SKalle Valo 	ret = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
637e705c121SKalle Valo 	/* should never fail */
638e705c121SKalle Valo 	WARN_ON_ONCE(ret);
639e705c121SKalle Valo 
640e705c121SKalle Valo 	if (ret) {
641e705c121SKalle Valo  out_clear_te:
642e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
643e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
644e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
645e705c121SKalle Valo 	}
646e705c121SKalle Valo 	return ret;
647e705c121SKalle Valo }
648e705c121SKalle Valo 
iwl_mvm_protect_session(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 duration,u32 min_duration,u32 max_delay,bool wait_for_notif)649e705c121SKalle Valo void iwl_mvm_protect_session(struct iwl_mvm *mvm,
650e705c121SKalle Valo 			     struct ieee80211_vif *vif,
651e705c121SKalle Valo 			     u32 duration, u32 min_duration,
652e705c121SKalle Valo 			     u32 max_delay, bool wait_for_notif)
653e705c121SKalle Valo {
654e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
655e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
656e705c121SKalle Valo 	const u16 te_notif_response[] = { TIME_EVENT_NOTIFICATION };
657e705c121SKalle Valo 	struct iwl_notification_wait wait_te_notif;
658e705c121SKalle Valo 	struct iwl_time_event_cmd time_cmd = {};
659e705c121SKalle Valo 
660e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
661e705c121SKalle Valo 
662e705c121SKalle Valo 	if (te_data->running &&
663e705c121SKalle Valo 	    time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
664e705c121SKalle Valo 		IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
665e705c121SKalle Valo 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
666e705c121SKalle Valo 		return;
667e705c121SKalle Valo 	}
668e705c121SKalle Valo 
669e705c121SKalle Valo 	if (te_data->running) {
670e705c121SKalle Valo 		IWL_DEBUG_TE(mvm, "extend 0x%x: only %u ms left\n",
671e705c121SKalle Valo 			     te_data->uid,
672e705c121SKalle Valo 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
673e705c121SKalle Valo 		/*
674e705c121SKalle Valo 		 * we don't have enough time
675e705c121SKalle Valo 		 * cancel the current TE and issue a new one
676e705c121SKalle Valo 		 * Of course it would be better to remove the old one only
677e705c121SKalle Valo 		 * when the new one is added, but we don't care if we are off
678e705c121SKalle Valo 		 * channel for a bit. All we need to do, is not to return
679e705c121SKalle Valo 		 * before we actually begin to be on the channel.
680e705c121SKalle Valo 		 */
681e705c121SKalle Valo 		iwl_mvm_stop_session_protection(mvm, vif);
682e705c121SKalle Valo 	}
683e705c121SKalle Valo 
684e705c121SKalle Valo 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
685e705c121SKalle Valo 	time_cmd.id_and_color =
686e705c121SKalle Valo 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
687e705c121SKalle Valo 	time_cmd.id = cpu_to_le32(TE_BSS_STA_AGGRESSIVE_ASSOC);
688e705c121SKalle Valo 
689e705c121SKalle Valo 	time_cmd.apply_time = cpu_to_le32(0);
690e705c121SKalle Valo 
691e705c121SKalle Valo 	time_cmd.max_frags = TE_V2_FRAG_NONE;
692e705c121SKalle Valo 	time_cmd.max_delay = cpu_to_le32(max_delay);
693e705c121SKalle Valo 	/* TODO: why do we need to interval = bi if it is not periodic? */
694e705c121SKalle Valo 	time_cmd.interval = cpu_to_le32(1);
695e705c121SKalle Valo 	time_cmd.duration = cpu_to_le32(duration);
696e705c121SKalle Valo 	time_cmd.repeat = 1;
697e705c121SKalle Valo 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
698e705c121SKalle Valo 				      TE_V2_NOTIF_HOST_EVENT_END |
69940d53f4aSAndrei Otcheretianski 				      TE_V2_START_IMMEDIATELY);
700e705c121SKalle Valo 
701e705c121SKalle Valo 	if (!wait_for_notif) {
702e705c121SKalle Valo 		iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
703e705c121SKalle Valo 		return;
704e705c121SKalle Valo 	}
705e705c121SKalle Valo 
706e705c121SKalle Valo 	/*
707e705c121SKalle Valo 	 * Create notification_wait for the TIME_EVENT_NOTIFICATION to use
708e705c121SKalle Valo 	 * right after we send the time event
709e705c121SKalle Valo 	 */
710e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_te_notif,
711e705c121SKalle Valo 				   te_notif_response,
712e705c121SKalle Valo 				   ARRAY_SIZE(te_notif_response),
713e705c121SKalle Valo 				   iwl_mvm_te_notif, te_data);
714e705c121SKalle Valo 
715e705c121SKalle Valo 	/* If TE was sent OK - wait for the notification that started */
716e705c121SKalle Valo 	if (iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd)) {
717e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add TE to protect session\n");
718e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_te_notif);
719e705c121SKalle Valo 	} else if (iwl_wait_notification(&mvm->notif_wait, &wait_te_notif,
720e705c121SKalle Valo 					 TU_TO_JIFFIES(max_delay))) {
721e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to protect session until TE\n");
722e705c121SKalle Valo 	}
723e705c121SKalle Valo }
724e705c121SKalle Valo 
72513506583SMiri Korenblit /* Determine whether mac or link id should be used, and validate the link id */
iwl_mvm_get_session_prot_id(struct iwl_mvm * mvm,struct ieee80211_vif * vif,s8 link_id)72613506583SMiri Korenblit static int iwl_mvm_get_session_prot_id(struct iwl_mvm *mvm,
72713506583SMiri Korenblit 				       struct ieee80211_vif *vif,
7286c8ce238SMiri Korenblit 				       s8 link_id)
7291cf260e3SEmmanuel Grumbach {
73013506583SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
73113506583SMiri Korenblit 	int ver = iwl_fw_lookup_cmd_ver(mvm->fw,
73213506583SMiri Korenblit 					WIDE_ID(MAC_CONF_GROUP,
73313506583SMiri Korenblit 						SESSION_PROTECTION_CMD), 1);
73413506583SMiri Korenblit 
73513506583SMiri Korenblit 	if (ver < 2)
73613506583SMiri Korenblit 		return mvmvif->id;
73713506583SMiri Korenblit 
73813506583SMiri Korenblit 	if (WARN(link_id < 0 || !mvmvif->link[link_id],
73913506583SMiri Korenblit 		 "Invalid link ID for session protection: %u\n", link_id))
74013506583SMiri Korenblit 		return -EINVAL;
74113506583SMiri Korenblit 
742556c7cd7SMiri Korenblit 	if (WARN(!mvmvif->link[link_id]->active,
74313506583SMiri Korenblit 		 "Session Protection on an inactive link: %u\n", link_id))
74413506583SMiri Korenblit 		return -EINVAL;
74513506583SMiri Korenblit 
74613506583SMiri Korenblit 	return mvmvif->link[link_id]->fw_link_id;
74713506583SMiri Korenblit }
74813506583SMiri Korenblit 
iwl_mvm_cancel_session_protection(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 id,s8 link_id)74913506583SMiri Korenblit static void iwl_mvm_cancel_session_protection(struct iwl_mvm *mvm,
75013506583SMiri Korenblit 					      struct ieee80211_vif *vif,
7516c8ce238SMiri Korenblit 					      u32 id, s8 link_id)
75213506583SMiri Korenblit {
75313506583SMiri Korenblit 	int mac_link_id = iwl_mvm_get_session_prot_id(mvm, vif, link_id);
7541cf260e3SEmmanuel Grumbach 	struct iwl_mvm_session_prot_cmd cmd = {
75513506583SMiri Korenblit 		.id_and_color = cpu_to_le32(mac_link_id),
7561cf260e3SEmmanuel Grumbach 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
7577b3954a1SIlan Peer 		.conf_id = cpu_to_le32(id),
7581cf260e3SEmmanuel Grumbach 	};
7591cf260e3SEmmanuel Grumbach 	int ret;
7601cf260e3SEmmanuel Grumbach 
76113506583SMiri Korenblit 	if (mac_link_id < 0)
76213506583SMiri Korenblit 		return;
76313506583SMiri Korenblit 
764f0c86427SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm,
765f0c86427SJohannes Berg 				   WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
7661cf260e3SEmmanuel Grumbach 				   0, sizeof(cmd), &cmd);
7671cf260e3SEmmanuel Grumbach 	if (ret)
7681cf260e3SEmmanuel Grumbach 		IWL_ERR(mvm,
7691cf260e3SEmmanuel Grumbach 			"Couldn't send the SESSION_PROTECTION_CMD: %d\n", ret);
7701cf260e3SEmmanuel Grumbach }
7711cf260e3SEmmanuel Grumbach 
iwl_mvm_roc_rm_cmd(struct iwl_mvm * mvm,u32 activity)77254d96912SShaul Triebitz static void iwl_mvm_roc_rm_cmd(struct iwl_mvm *mvm, u32 activity)
77354d96912SShaul Triebitz {
77454d96912SShaul Triebitz 	struct iwl_roc_req roc_cmd = {
77554d96912SShaul Triebitz 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
77654d96912SShaul Triebitz 		.activity = cpu_to_le32(activity),
77754d96912SShaul Triebitz 	};
77854d96912SShaul Triebitz 	int ret;
77954d96912SShaul Triebitz 
78054d96912SShaul Triebitz 	lockdep_assert_held(&mvm->mutex);
78154d96912SShaul Triebitz 	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0,
78254d96912SShaul Triebitz 				   sizeof(roc_cmd), &roc_cmd);
78354d96912SShaul Triebitz 	if (ret)
78454d96912SShaul Triebitz 		IWL_ERR(mvm, "Couldn't send the ROC_CMD: %d\n", ret);
78554d96912SShaul Triebitz }
78654d96912SShaul Triebitz 
__iwl_mvm_remove_time_event(struct iwl_mvm * mvm,struct iwl_mvm_time_event_data * te_data,u32 * uid)787e705c121SKalle Valo static bool __iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
788e705c121SKalle Valo 					struct iwl_mvm_time_event_data *te_data,
789e705c121SKalle Valo 					u32 *uid)
790e705c121SKalle Valo {
791e705c121SKalle Valo 	u32 id;
79213506583SMiri Korenblit 	struct ieee80211_vif *vif = te_data->vif;
79324d5f16eSIlan Peer 	struct iwl_mvm_vif *mvmvif;
7947b3954a1SIlan Peer 	enum nl80211_iftype iftype;
7956c8ce238SMiri Korenblit 	s8 link_id;
79654d96912SShaul Triebitz 	bool p2p_aux = iwl_mvm_has_p2p_over_aux(mvm);
79754d96912SShaul Triebitz 	u8 roc_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
79854d96912SShaul Triebitz 					   WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0);
7997b3954a1SIlan Peer 
80013506583SMiri Korenblit 	if (!vif)
8017b3954a1SIlan Peer 		return false;
8027b3954a1SIlan Peer 
80324d5f16eSIlan Peer 	mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
8047b3954a1SIlan Peer 	iftype = te_data->vif->type;
805e705c121SKalle Valo 
806e705c121SKalle Valo 	/*
807e705c121SKalle Valo 	 * It is possible that by the time we got to this point the time
808e705c121SKalle Valo 	 * event was already removed.
809e705c121SKalle Valo 	 */
810e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
811e705c121SKalle Valo 
812e705c121SKalle Valo 	/* Save time event uid before clearing its data */
813e705c121SKalle Valo 	*uid = te_data->uid;
814e705c121SKalle Valo 	id = te_data->id;
81513506583SMiri Korenblit 	link_id = te_data->link_id;
816e705c121SKalle Valo 
817e705c121SKalle Valo 	/*
818e705c121SKalle Valo 	 * The clear_data function handles time events that were already removed
819e705c121SKalle Valo 	 */
820e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, te_data);
821e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
822e705c121SKalle Valo 
82354d96912SShaul Triebitz 	if ((p2p_aux && iftype == NL80211_IFTYPE_P2P_DEVICE) ||
82454d96912SShaul Triebitz 	    (roc_ver >= 3 && mvmvif->roc_activity == ROC_ACTIVITY_HOTSPOT)) {
82554d96912SShaul Triebitz 		if (mvmvif->roc_activity < ROC_NUM_ACTIVITIES) {
82654d96912SShaul Triebitz 			iwl_mvm_roc_rm_cmd(mvm, mvmvif->roc_activity);
82754d96912SShaul Triebitz 			mvmvif->roc_activity = ROC_NUM_ACTIVITIES;
82854d96912SShaul Triebitz 			iwl_mvm_roc_finished(mvm);
82954d96912SShaul Triebitz 		}
83054d96912SShaul Triebitz 		return false;
83154d96912SShaul Triebitz 	} else if (fw_has_capa(&mvm->fw->ucode_capa,
832f0337cb4SAvraham Stern 			       IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD) &&
833f0337cb4SAvraham Stern 		   id != HOT_SPOT_CMD) {
83454d96912SShaul Triebitz 		/* When session protection is used, the te_data->id field
83554d96912SShaul Triebitz 		 * is reused to save session protection's configuration.
83654d96912SShaul Triebitz 		 * For AUX ROC, HOT_SPOT_CMD is used and the te_data->id
83754d96912SShaul Triebitz 		 * field is set to HOT_SPOT_CMD.
83854d96912SShaul Triebitz 		 */
8391cf260e3SEmmanuel Grumbach 		if (mvmvif && id < SESSION_PROTECT_CONF_MAX_ID) {
8401cf260e3SEmmanuel Grumbach 			/* Session protection is still ongoing. Cancel it */
84113506583SMiri Korenblit 			iwl_mvm_cancel_session_protection(mvm, vif, id,
84213506583SMiri Korenblit 							  link_id);
8437b3954a1SIlan Peer 			if (iftype == NL80211_IFTYPE_P2P_DEVICE) {
84435c1bbd9SMiri Korenblit 				iwl_mvm_roc_finished(mvm);
8451cf260e3SEmmanuel Grumbach 			}
8461cf260e3SEmmanuel Grumbach 		}
8471cf260e3SEmmanuel Grumbach 		return false;
8481cf260e3SEmmanuel Grumbach 	} else {
8491cf260e3SEmmanuel Grumbach 		/* It is possible that by the time we try to remove it, the
8501cf260e3SEmmanuel Grumbach 		 * time event has already ended and removed. In such a case
8511cf260e3SEmmanuel Grumbach 		 * there is no need to send a removal command.
852e705c121SKalle Valo 		 */
853e705c121SKalle Valo 		if (id == TE_MAX) {
854e705c121SKalle Valo 			IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", *uid);
855e705c121SKalle Valo 			return false;
856e705c121SKalle Valo 		}
8571cf260e3SEmmanuel Grumbach 	}
858e705c121SKalle Valo 
859e705c121SKalle Valo 	return true;
860e705c121SKalle Valo }
861e705c121SKalle Valo 
862e705c121SKalle Valo /*
863e705c121SKalle Valo  * Explicit request to remove a aux roc time event. The removal of a time
864e705c121SKalle Valo  * event needs to be synchronized with the flow of a time event's end
865e705c121SKalle Valo  * notification, which also removes the time event from the op mode
866e705c121SKalle Valo  * data structures.
867e705c121SKalle Valo  */
iwl_mvm_remove_aux_roc_te(struct iwl_mvm * mvm,struct iwl_mvm_vif * mvmvif,struct iwl_mvm_time_event_data * te_data)868e705c121SKalle Valo static void iwl_mvm_remove_aux_roc_te(struct iwl_mvm *mvm,
869e705c121SKalle Valo 				      struct iwl_mvm_vif *mvmvif,
870e705c121SKalle Valo 				      struct iwl_mvm_time_event_data *te_data)
871e705c121SKalle Valo {
872e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_cmd = {};
87357e861d9SDavid Spinadel 	u16 len = sizeof(aux_cmd) - iwl_mvm_chan_info_padding(mvm);
87457e861d9SDavid Spinadel 
875e705c121SKalle Valo 	u32 uid;
876e705c121SKalle Valo 	int ret;
877e705c121SKalle Valo 
878e705c121SKalle Valo 	if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
879e705c121SKalle Valo 		return;
880e705c121SKalle Valo 
881e705c121SKalle Valo 	aux_cmd.event_unique_id = cpu_to_le32(uid);
882e705c121SKalle Valo 	aux_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
883e705c121SKalle Valo 	aux_cmd.id_and_color =
884e705c121SKalle Valo 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
885e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "Removing BSS AUX ROC TE 0x%x\n",
886e705c121SKalle Valo 		     le32_to_cpu(aux_cmd.event_unique_id));
887e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0,
88857e861d9SDavid Spinadel 				   len, &aux_cmd);
889e705c121SKalle Valo 
890e705c121SKalle Valo 	if (WARN_ON(ret))
891e705c121SKalle Valo 		return;
892e705c121SKalle Valo }
893e705c121SKalle Valo 
894e705c121SKalle Valo /*
895e705c121SKalle Valo  * Explicit request to remove a time event. The removal of a time event needs to
896e705c121SKalle Valo  * be synchronized with the flow of a time event's end notification, which also
897e705c121SKalle Valo  * removes the time event from the op mode data structures.
898e705c121SKalle Valo  */
iwl_mvm_remove_time_event(struct iwl_mvm * mvm,struct iwl_mvm_vif * mvmvif,struct iwl_mvm_time_event_data * te_data)899e705c121SKalle Valo void iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
900e705c121SKalle Valo 			       struct iwl_mvm_vif *mvmvif,
901e705c121SKalle Valo 			       struct iwl_mvm_time_event_data *te_data)
902e705c121SKalle Valo {
903e705c121SKalle Valo 	struct iwl_time_event_cmd time_cmd = {};
904e705c121SKalle Valo 	u32 uid;
905e705c121SKalle Valo 	int ret;
906e705c121SKalle Valo 
907e705c121SKalle Valo 	if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
908e705c121SKalle Valo 		return;
909e705c121SKalle Valo 
910e705c121SKalle Valo 	/* When we remove a TE, the UID is to be set in the id field */
911e705c121SKalle Valo 	time_cmd.id = cpu_to_le32(uid);
912e705c121SKalle Valo 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
913e705c121SKalle Valo 	time_cmd.id_and_color =
914e705c121SKalle Valo 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
915e705c121SKalle Valo 
916e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "Removing TE 0x%x\n", le32_to_cpu(time_cmd.id));
917e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
918e705c121SKalle Valo 				   sizeof(time_cmd), &time_cmd);
919997254a9SEmmanuel Grumbach 	if (ret)
920997254a9SEmmanuel Grumbach 		IWL_ERR(mvm, "Couldn't remove the time event\n");
921e705c121SKalle Valo }
922e705c121SKalle Valo 
iwl_mvm_stop_session_protection(struct iwl_mvm * mvm,struct ieee80211_vif * vif)923e705c121SKalle Valo void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,
924e705c121SKalle Valo 				     struct ieee80211_vif *vif)
925e705c121SKalle Valo {
926e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
927e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
9283edfb5f4SAvraham Stern 	u32 id;
929e705c121SKalle Valo 
930e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
9313edfb5f4SAvraham Stern 
9323edfb5f4SAvraham Stern 	spin_lock_bh(&mvm->time_event_lock);
9333edfb5f4SAvraham Stern 	id = te_data->id;
9343edfb5f4SAvraham Stern 	spin_unlock_bh(&mvm->time_event_lock);
9353edfb5f4SAvraham Stern 
9367b3954a1SIlan Peer 	if (fw_has_capa(&mvm->fw->ucode_capa,
9377b3954a1SIlan Peer 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
9387b3954a1SIlan Peer 		if (id != SESSION_PROTECT_CONF_ASSOC) {
9397b3954a1SIlan Peer 			IWL_DEBUG_TE(mvm,
9407b3954a1SIlan Peer 				     "don't remove session protection id=%u\n",
9417b3954a1SIlan Peer 				     id);
9427b3954a1SIlan Peer 			return;
9437b3954a1SIlan Peer 		}
9447b3954a1SIlan Peer 	} else if (id != TE_BSS_STA_AGGRESSIVE_ASSOC) {
9453edfb5f4SAvraham Stern 		IWL_DEBUG_TE(mvm,
9463edfb5f4SAvraham Stern 			     "don't remove TE with id=%u (not session protection)\n",
9473edfb5f4SAvraham Stern 			     id);
9483edfb5f4SAvraham Stern 		return;
9493edfb5f4SAvraham Stern 	}
9503edfb5f4SAvraham Stern 
951e705c121SKalle Valo 	iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
952e705c121SKalle Valo }
953e705c121SKalle Valo 
iwl_mvm_rx_session_protect_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)954fe959c7bSEmmanuel Grumbach void iwl_mvm_rx_session_protect_notif(struct iwl_mvm *mvm,
955fe959c7bSEmmanuel Grumbach 				      struct iwl_rx_cmd_buffer *rxb)
956fe959c7bSEmmanuel Grumbach {
957fe959c7bSEmmanuel Grumbach 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
958fe959c7bSEmmanuel Grumbach 	struct iwl_mvm_session_prot_notif *notif = (void *)pkt->data;
959085d33c5SMiri Korenblit 	unsigned int ver =
960bbe806c2SEmmanuel Grumbach 		iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
961bbe806c2SEmmanuel Grumbach 					SESSION_PROTECTION_NOTIF, 2);
962085d33c5SMiri Korenblit 	int id = le32_to_cpu(notif->mac_link_id);
963fe959c7bSEmmanuel Grumbach 	struct ieee80211_vif *vif;
9641cf260e3SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif;
965085d33c5SMiri Korenblit 	unsigned int notif_link_id;
966fe959c7bSEmmanuel Grumbach 
967fe959c7bSEmmanuel Grumbach 	rcu_read_lock();
968085d33c5SMiri Korenblit 
969085d33c5SMiri Korenblit 	if (ver <= 2) {
970085d33c5SMiri Korenblit 		vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
971085d33c5SMiri Korenblit 	} else {
972085d33c5SMiri Korenblit 		struct ieee80211_bss_conf *link_conf =
973085d33c5SMiri Korenblit 			iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, id, true);
974085d33c5SMiri Korenblit 
975085d33c5SMiri Korenblit 		if (!link_conf)
976085d33c5SMiri Korenblit 			goto out_unlock;
977085d33c5SMiri Korenblit 
978085d33c5SMiri Korenblit 		notif_link_id = link_conf->link_id;
979085d33c5SMiri Korenblit 		vif = link_conf->vif;
980085d33c5SMiri Korenblit 	}
981fe959c7bSEmmanuel Grumbach 
982fe959c7bSEmmanuel Grumbach 	if (!vif)
983fe959c7bSEmmanuel Grumbach 		goto out_unlock;
984fe959c7bSEmmanuel Grumbach 
9851cf260e3SEmmanuel Grumbach 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
9861cf260e3SEmmanuel Grumbach 
987085d33c5SMiri Korenblit 	if (WARN(ver > 2 && mvmvif->time_event_data.link_id >= 0 &&
988085d33c5SMiri Korenblit 		 mvmvif->time_event_data.link_id != notif_link_id,
98964a06679SColin Ian King 		 "SESSION_PROTECTION_NOTIF was received for link %u, while the current time event is on link %u\n",
990085d33c5SMiri Korenblit 		 notif_link_id, mvmvif->time_event_data.link_id))
991085d33c5SMiri Korenblit 		goto out_unlock;
992085d33c5SMiri Korenblit 
993fe959c7bSEmmanuel Grumbach 	/* The vif is not a P2P_DEVICE, maintain its time_event_data */
994fe959c7bSEmmanuel Grumbach 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
995fe959c7bSEmmanuel Grumbach 		struct iwl_mvm_time_event_data *te_data =
996fe959c7bSEmmanuel Grumbach 			&mvmvif->time_event_data;
997fe959c7bSEmmanuel Grumbach 
998fe959c7bSEmmanuel Grumbach 		if (!le32_to_cpu(notif->status)) {
999fe959c7bSEmmanuel Grumbach 			iwl_mvm_te_check_disconnect(mvm, vif,
1000fe959c7bSEmmanuel Grumbach 						    "Session protection failure");
1001089e5016SAvraham Stern 			spin_lock_bh(&mvm->time_event_lock);
1002fe959c7bSEmmanuel Grumbach 			iwl_mvm_te_clear_data(mvm, te_data);
1003089e5016SAvraham Stern 			spin_unlock_bh(&mvm->time_event_lock);
1004fe959c7bSEmmanuel Grumbach 		}
1005fe959c7bSEmmanuel Grumbach 
1006fe959c7bSEmmanuel Grumbach 		if (le32_to_cpu(notif->start)) {
1007fe959c7bSEmmanuel Grumbach 			spin_lock_bh(&mvm->time_event_lock);
1008fe959c7bSEmmanuel Grumbach 			te_data->running = le32_to_cpu(notif->start);
1009fe959c7bSEmmanuel Grumbach 			te_data->end_jiffies =
1010fe959c7bSEmmanuel Grumbach 				TU_TO_EXP_TIME(te_data->duration);
1011fe959c7bSEmmanuel Grumbach 			spin_unlock_bh(&mvm->time_event_lock);
1012fe959c7bSEmmanuel Grumbach 		} else {
1013fe959c7bSEmmanuel Grumbach 			/*
1014fe959c7bSEmmanuel Grumbach 			 * By now, we should have finished association
1015fe959c7bSEmmanuel Grumbach 			 * and know the dtim period.
1016fe959c7bSEmmanuel Grumbach 			 */
1017fe959c7bSEmmanuel Grumbach 			iwl_mvm_te_check_disconnect(mvm, vif,
1018f276e20bSJohannes Berg 						    !vif->cfg.assoc ?
1019976ac0afSShaul Triebitz 						    "Not associated and the session protection is over already..." :
1020fe959c7bSEmmanuel Grumbach 						    "No beacon heard and the session protection is over already...");
1021089e5016SAvraham Stern 			spin_lock_bh(&mvm->time_event_lock);
1022fe959c7bSEmmanuel Grumbach 			iwl_mvm_te_clear_data(mvm, te_data);
1023089e5016SAvraham Stern 			spin_unlock_bh(&mvm->time_event_lock);
1024fe959c7bSEmmanuel Grumbach 		}
1025fe959c7bSEmmanuel Grumbach 
1026fe959c7bSEmmanuel Grumbach 		goto out_unlock;
1027fe959c7bSEmmanuel Grumbach 	}
1028fe959c7bSEmmanuel Grumbach 
1029fe959c7bSEmmanuel Grumbach 	if (!le32_to_cpu(notif->status) || !le32_to_cpu(notif->start)) {
1030fe959c7bSEmmanuel Grumbach 		/* End TE, notify mac80211 */
10311cf260e3SEmmanuel Grumbach 		mvmvif->time_event_data.id = SESSION_PROTECT_CONF_MAX_ID;
10323012477cSMiri Korenblit 		mvmvif->time_event_data.link_id = -1;
103335c1bbd9SMiri Korenblit 		iwl_mvm_roc_finished(mvm);
103484ef7cbeSIlan Peer 		ieee80211_remain_on_channel_expired(mvm->hw);
1035fe959c7bSEmmanuel Grumbach 	} else if (le32_to_cpu(notif->start)) {
10361cf260e3SEmmanuel Grumbach 		if (WARN_ON(mvmvif->time_event_data.id !=
10371cf260e3SEmmanuel Grumbach 				le32_to_cpu(notif->conf_id)))
10381cf260e3SEmmanuel Grumbach 			goto out_unlock;
103954d96912SShaul Triebitz 		set_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status);
1040fe959c7bSEmmanuel Grumbach 		ieee80211_ready_on_channel(mvm->hw); /* Start TE */
1041fe959c7bSEmmanuel Grumbach 	}
1042fe959c7bSEmmanuel Grumbach 
1043fe959c7bSEmmanuel Grumbach  out_unlock:
1044fe959c7bSEmmanuel Grumbach 	rcu_read_unlock();
1045fe959c7bSEmmanuel Grumbach }
1046fe959c7bSEmmanuel Grumbach 
10475932ad87SShaul Triebitz #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
10485932ad87SShaul Triebitz #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
10495932ad87SShaul Triebitz #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
10505932ad87SShaul Triebitz #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
10515932ad87SShaul Triebitz #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
10525932ad87SShaul Triebitz 
iwl_mvm_roc_duration_and_delay(struct ieee80211_vif * vif,u32 duration_ms,u32 * duration_tu,u32 * delay)10535932ad87SShaul Triebitz void iwl_mvm_roc_duration_and_delay(struct ieee80211_vif *vif,
10545932ad87SShaul Triebitz 				    u32 duration_ms,
10555932ad87SShaul Triebitz 				    u32 *duration_tu,
10565932ad87SShaul Triebitz 				    u32 *delay)
10575932ad87SShaul Triebitz {
1058187accaaSEmmanuel Grumbach 	struct ieee80211_bss_conf *link_conf;
1059187accaaSEmmanuel Grumbach 	unsigned int link_id;
1060187accaaSEmmanuel Grumbach 	u32 dtim_interval = 0;
10615932ad87SShaul Triebitz 
10625932ad87SShaul Triebitz 	*delay = AUX_ROC_MIN_DELAY;
10635932ad87SShaul Triebitz 	*duration_tu = MSEC_TO_TU(duration_ms);
10645932ad87SShaul Triebitz 
1065187accaaSEmmanuel Grumbach 	rcu_read_lock();
1066187accaaSEmmanuel Grumbach 	for_each_vif_active_link(vif, link_conf, link_id) {
1067187accaaSEmmanuel Grumbach 		dtim_interval =
1068187accaaSEmmanuel Grumbach 			max_t(u32, dtim_interval,
1069187accaaSEmmanuel Grumbach 			      link_conf->dtim_period * link_conf->beacon_int);
1070187accaaSEmmanuel Grumbach 	}
1071187accaaSEmmanuel Grumbach 	rcu_read_unlock();
1072187accaaSEmmanuel Grumbach 
10735932ad87SShaul Triebitz 	/*
10745932ad87SShaul Triebitz 	 * If we are associated we want the delay time to be at least one
10755932ad87SShaul Triebitz 	 * dtim interval so that the FW can wait until after the DTIM and
10765932ad87SShaul Triebitz 	 * then start the time event, this will potentially allow us to
10775932ad87SShaul Triebitz 	 * remain off-channel for the max duration.
10785932ad87SShaul Triebitz 	 * Since we want to use almost a whole dtim interval we would also
10795932ad87SShaul Triebitz 	 * like the delay to be for 2-3 dtim intervals, in case there are
10805932ad87SShaul Triebitz 	 * other time events with higher priority.
1081187accaaSEmmanuel Grumbach 	 * dtim_interval should never be 0, it can be 1 if we don't know it
1082187accaaSEmmanuel Grumbach 	 * (we haven't heard any beacon yet).
10835932ad87SShaul Triebitz 	 */
1084187accaaSEmmanuel Grumbach 	if (vif->cfg.assoc && !WARN_ON(!dtim_interval)) {
10855932ad87SShaul Triebitz 		*delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
10865932ad87SShaul Triebitz 		/* We cannot remain off-channel longer than the DTIM interval */
10875932ad87SShaul Triebitz 		if (dtim_interval <= *duration_tu) {
10885932ad87SShaul Triebitz 			*duration_tu = dtim_interval - AUX_ROC_SAFETY_BUFFER;
10895932ad87SShaul Triebitz 			if (*duration_tu <= AUX_ROC_MIN_DURATION)
10905932ad87SShaul Triebitz 				*duration_tu = dtim_interval -
10915932ad87SShaul Triebitz 					AUX_ROC_MIN_SAFETY_BUFFER;
10925932ad87SShaul Triebitz 		}
10935932ad87SShaul Triebitz 	}
10945932ad87SShaul Triebitz }
10955932ad87SShaul Triebitz 
iwl_mvm_roc_add_cmd(struct iwl_mvm * mvm,struct ieee80211_channel * channel,struct ieee80211_vif * vif,int duration,enum iwl_roc_activity activity)10965932ad87SShaul Triebitz int iwl_mvm_roc_add_cmd(struct iwl_mvm *mvm,
10975932ad87SShaul Triebitz 			struct ieee80211_channel *channel,
10985932ad87SShaul Triebitz 			struct ieee80211_vif *vif,
109954d96912SShaul Triebitz 			int duration, enum iwl_roc_activity activity)
11005932ad87SShaul Triebitz {
11015932ad87SShaul Triebitz 	int res;
11025932ad87SShaul Triebitz 	u32 duration_tu, delay;
11035932ad87SShaul Triebitz 	struct iwl_roc_req roc_req = {
11045932ad87SShaul Triebitz 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
11055932ad87SShaul Triebitz 		.activity = cpu_to_le32(activity),
11065932ad87SShaul Triebitz 		.sta_id = cpu_to_le32(mvm->aux_sta.sta_id),
11075932ad87SShaul Triebitz 	};
110854d96912SShaul Triebitz 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
11095932ad87SShaul Triebitz 
11105932ad87SShaul Triebitz 	lockdep_assert_held(&mvm->mutex);
11115932ad87SShaul Triebitz 
111254d96912SShaul Triebitz 	if (WARN_ON(mvmvif->roc_activity != ROC_NUM_ACTIVITIES))
111354d96912SShaul Triebitz 		return -EBUSY;
111454d96912SShaul Triebitz 
11155932ad87SShaul Triebitz 	/* Set the channel info data */
11165932ad87SShaul Triebitz 	iwl_mvm_set_chan_info(mvm, &roc_req.channel_info,
11175932ad87SShaul Triebitz 			      channel->hw_value,
11185932ad87SShaul Triebitz 			      iwl_mvm_phy_band_from_nl80211(channel->band),
11195932ad87SShaul Triebitz 			      IWL_PHY_CHANNEL_MODE20, 0);
11205932ad87SShaul Triebitz 
11215932ad87SShaul Triebitz 	iwl_mvm_roc_duration_and_delay(vif, duration, &duration_tu,
11225932ad87SShaul Triebitz 				       &delay);
11235932ad87SShaul Triebitz 	roc_req.duration = cpu_to_le32(duration_tu);
11245932ad87SShaul Triebitz 	roc_req.max_delay = cpu_to_le32(delay);
11255932ad87SShaul Triebitz 
11265932ad87SShaul Triebitz 	IWL_DEBUG_TE(mvm,
11275932ad87SShaul Triebitz 		     "\t(requested = %ums, max_delay = %ums)\n",
11285932ad87SShaul Triebitz 		     duration, delay);
11295932ad87SShaul Triebitz 	IWL_DEBUG_TE(mvm,
113054d96912SShaul Triebitz 		     "Requesting to remain on channel %u for %utu. activity %u\n",
113154d96912SShaul Triebitz 		     channel->hw_value, duration_tu, activity);
11325932ad87SShaul Triebitz 
11335932ad87SShaul Triebitz 	/* Set the node address */
11345932ad87SShaul Triebitz 	memcpy(roc_req.node_addr, vif->addr, ETH_ALEN);
11355932ad87SShaul Triebitz 
11365932ad87SShaul Triebitz 	res = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, ROC_CMD),
11375932ad87SShaul Triebitz 				   0, sizeof(roc_req), &roc_req);
113854d96912SShaul Triebitz 	if (!res)
113954d96912SShaul Triebitz 		mvmvif->roc_activity = activity;
11405932ad87SShaul Triebitz 
11415932ad87SShaul Triebitz 	return res;
11425932ad87SShaul Triebitz }
11435932ad87SShaul Triebitz 
1144fe959c7bSEmmanuel Grumbach static int
iwl_mvm_start_p2p_roc_session_protection(struct iwl_mvm * mvm,struct ieee80211_vif * vif,int duration,enum ieee80211_roc_type type)1145fe959c7bSEmmanuel Grumbach iwl_mvm_start_p2p_roc_session_protection(struct iwl_mvm *mvm,
1146fe959c7bSEmmanuel Grumbach 					 struct ieee80211_vif *vif,
1147fe959c7bSEmmanuel Grumbach 					 int duration,
1148fe959c7bSEmmanuel Grumbach 					 enum ieee80211_roc_type type)
1149fe959c7bSEmmanuel Grumbach {
1150fe959c7bSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1151fe959c7bSEmmanuel Grumbach 	struct iwl_mvm_session_prot_cmd cmd = {
1152fe959c7bSEmmanuel Grumbach 		.id_and_color =
115313506583SMiri Korenblit 			cpu_to_le32(iwl_mvm_get_session_prot_id(mvm, vif, 0)),
1154fe959c7bSEmmanuel Grumbach 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
1155fe959c7bSEmmanuel Grumbach 		.duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
1156fe959c7bSEmmanuel Grumbach 	};
1157fe959c7bSEmmanuel Grumbach 
1158fe959c7bSEmmanuel Grumbach 	lockdep_assert_held(&mvm->mutex);
1159fe959c7bSEmmanuel Grumbach 
11601cf260e3SEmmanuel Grumbach 	/* The time_event_data.id field is reused to save session
11611cf260e3SEmmanuel Grumbach 	 * protection's configuration.
11621cf260e3SEmmanuel Grumbach 	 */
116323673041SMiri Korenblit 
116423673041SMiri Korenblit 	mvmvif->time_event_data.link_id = 0;
116523673041SMiri Korenblit 
1166fe959c7bSEmmanuel Grumbach 	switch (type) {
1167fe959c7bSEmmanuel Grumbach 	case IEEE80211_ROC_TYPE_NORMAL:
11681cf260e3SEmmanuel Grumbach 		mvmvif->time_event_data.id =
11691cf260e3SEmmanuel Grumbach 			SESSION_PROTECT_CONF_P2P_DEVICE_DISCOV;
1170fe959c7bSEmmanuel Grumbach 		break;
1171fe959c7bSEmmanuel Grumbach 	case IEEE80211_ROC_TYPE_MGMT_TX:
11721cf260e3SEmmanuel Grumbach 		mvmvif->time_event_data.id =
11731cf260e3SEmmanuel Grumbach 			SESSION_PROTECT_CONF_P2P_GO_NEGOTIATION;
1174fe959c7bSEmmanuel Grumbach 		break;
1175fe959c7bSEmmanuel Grumbach 	default:
1176fe959c7bSEmmanuel Grumbach 		WARN_ONCE(1, "Got an invalid ROC type\n");
1177fe959c7bSEmmanuel Grumbach 		return -EINVAL;
1178fe959c7bSEmmanuel Grumbach 	}
1179fe959c7bSEmmanuel Grumbach 
11801cf260e3SEmmanuel Grumbach 	cmd.conf_id = cpu_to_le32(mvmvif->time_event_data.id);
1181f0c86427SJohannes Berg 	return iwl_mvm_send_cmd_pdu(mvm,
1182f0c86427SJohannes Berg 				    WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1183fe959c7bSEmmanuel Grumbach 				    0, sizeof(cmd), &cmd);
1184fe959c7bSEmmanuel Grumbach }
1185fe959c7bSEmmanuel Grumbach 
iwl_mvm_start_p2p_roc(struct iwl_mvm * mvm,struct ieee80211_vif * vif,int duration,enum ieee80211_roc_type type)1186e705c121SKalle Valo int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1187e705c121SKalle Valo 			  int duration, enum ieee80211_roc_type type)
1188e705c121SKalle Valo {
1189e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1190e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1191e705c121SKalle Valo 	struct iwl_time_event_cmd time_cmd = {};
1192e705c121SKalle Valo 
1193e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1194e705c121SKalle Valo 	if (te_data->running) {
1195e705c121SKalle Valo 		IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");
1196e705c121SKalle Valo 		return -EBUSY;
1197e705c121SKalle Valo 	}
1198e705c121SKalle Valo 
1199fe959c7bSEmmanuel Grumbach 	if (fw_has_capa(&mvm->fw->ucode_capa,
1200fe959c7bSEmmanuel Grumbach 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
1201fe959c7bSEmmanuel Grumbach 		return iwl_mvm_start_p2p_roc_session_protection(mvm, vif,
1202fe959c7bSEmmanuel Grumbach 								duration,
1203fe959c7bSEmmanuel Grumbach 								type);
1204fe959c7bSEmmanuel Grumbach 
1205e705c121SKalle Valo 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
1206e705c121SKalle Valo 	time_cmd.id_and_color =
1207e705c121SKalle Valo 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
1208e705c121SKalle Valo 
1209e705c121SKalle Valo 	switch (type) {
1210e705c121SKalle Valo 	case IEEE80211_ROC_TYPE_NORMAL:
1211e705c121SKalle Valo 		time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);
1212e705c121SKalle Valo 		break;
1213e705c121SKalle Valo 	case IEEE80211_ROC_TYPE_MGMT_TX:
1214e705c121SKalle Valo 		time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);
1215e705c121SKalle Valo 		break;
1216e705c121SKalle Valo 	default:
1217e705c121SKalle Valo 		WARN_ONCE(1, "Got an invalid ROC type\n");
1218e705c121SKalle Valo 		return -EINVAL;
1219e705c121SKalle Valo 	}
1220e705c121SKalle Valo 
1221e705c121SKalle Valo 	time_cmd.apply_time = cpu_to_le32(0);
1222e705c121SKalle Valo 	time_cmd.interval = cpu_to_le32(1);
1223e705c121SKalle Valo 
1224e705c121SKalle Valo 	/*
1225e705c121SKalle Valo 	 * The P2P Device TEs can have lower priority than other events
1226e705c121SKalle Valo 	 * that are being scheduled by the driver/fw, and thus it might not be
1227e705c121SKalle Valo 	 * scheduled. To improve the chances of it being scheduled, allow them
1228e705c121SKalle Valo 	 * to be fragmented, and in addition allow them to be delayed.
1229e705c121SKalle Valo 	 */
1230e705c121SKalle Valo 	time_cmd.max_frags = min(MSEC_TO_TU(duration)/50, TE_V2_FRAG_ENDLESS);
1231e705c121SKalle Valo 	time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));
1232e705c121SKalle Valo 	time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));
1233e705c121SKalle Valo 	time_cmd.repeat = 1;
1234e705c121SKalle Valo 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
1235e705c121SKalle Valo 				      TE_V2_NOTIF_HOST_EVENT_END |
123640d53f4aSAndrei Otcheretianski 				      TE_V2_START_IMMEDIATELY);
1237e705c121SKalle Valo 
1238e705c121SKalle Valo 	return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
1239e705c121SKalle Valo }
1240e705c121SKalle Valo 
iwl_mvm_get_roc_te(struct iwl_mvm * mvm)1241305d236eSEliad Peller static struct iwl_mvm_time_event_data *iwl_mvm_get_roc_te(struct iwl_mvm *mvm)
1242e705c121SKalle Valo {
1243e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data;
1244e705c121SKalle Valo 
1245e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1246e705c121SKalle Valo 
1247e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
1248e705c121SKalle Valo 
1249e705c121SKalle Valo 	/*
1250e705c121SKalle Valo 	 * Iterate over the list of time events and find the time event that is
1251e705c121SKalle Valo 	 * associated with a P2P_DEVICE interface.
1252e705c121SKalle Valo 	 * This assumes that a P2P_DEVICE interface can have only a single time
1253e705c121SKalle Valo 	 * event at any given time and this time event coresponds to a ROC
1254e705c121SKalle Valo 	 * request
1255e705c121SKalle Valo 	 */
1256e705c121SKalle Valo 	list_for_each_entry(te_data, &mvm->time_event_list, list) {
1257305d236eSEliad Peller 		if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE)
1258305d236eSEliad Peller 			goto out;
1259e705c121SKalle Valo 	}
1260e705c121SKalle Valo 
1261e705c121SKalle Valo 	/* There can only be at most one AUX ROC time event, we just use the
1262e705c121SKalle Valo 	 * list to simplify/unify code. Remove it if it exists.
1263e705c121SKalle Valo 	 */
1264e705c121SKalle Valo 	te_data = list_first_entry_or_null(&mvm->aux_roc_te_list,
1265e705c121SKalle Valo 					   struct iwl_mvm_time_event_data,
1266e705c121SKalle Valo 					   list);
1267305d236eSEliad Peller out:
1268e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
1269305d236eSEliad Peller 	return te_data;
1270305d236eSEliad Peller }
1271e705c121SKalle Valo 
iwl_mvm_cleanup_roc_te(struct iwl_mvm * mvm)1272305d236eSEliad Peller void iwl_mvm_cleanup_roc_te(struct iwl_mvm *mvm)
1273305d236eSEliad Peller {
1274305d236eSEliad Peller 	struct iwl_mvm_time_event_data *te_data;
1275305d236eSEliad Peller 	u32 uid;
1276305d236eSEliad Peller 
1277305d236eSEliad Peller 	te_data = iwl_mvm_get_roc_te(mvm);
1278305d236eSEliad Peller 	if (te_data)
1279305d236eSEliad Peller 		__iwl_mvm_remove_time_event(mvm, te_data, &uid);
1280305d236eSEliad Peller }
1281305d236eSEliad Peller 
iwl_mvm_stop_roc(struct iwl_mvm * mvm,struct ieee80211_vif * vif)1282fe959c7bSEmmanuel Grumbach void iwl_mvm_stop_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1283305d236eSEliad Peller {
128454d96912SShaul Triebitz 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1285305d236eSEliad Peller 	struct iwl_mvm_time_event_data *te_data;
128654d96912SShaul Triebitz 	bool p2p_aux = iwl_mvm_has_p2p_over_aux(mvm);
128754d96912SShaul Triebitz 	u8 roc_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
128854d96912SShaul Triebitz 					   WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0);
128954d96912SShaul Triebitz 	int iftype = vif->type;
1290305d236eSEliad Peller 
1291a1efeb82SYedidya Benshimol 	mutex_lock(&mvm->mutex);
1292a1efeb82SYedidya Benshimol 
129354d96912SShaul Triebitz 	if (p2p_aux || (roc_ver >= 3 && iftype != NL80211_IFTYPE_P2P_DEVICE)) {
129454d96912SShaul Triebitz 		if (mvmvif->roc_activity < ROC_NUM_ACTIVITIES) {
129554d96912SShaul Triebitz 			iwl_mvm_roc_rm_cmd(mvm, mvmvif->roc_activity);
129654d96912SShaul Triebitz 			mvmvif->roc_activity = ROC_NUM_ACTIVITIES;
129754d96912SShaul Triebitz 		}
129854d96912SShaul Triebitz 		goto cleanup_roc;
129954d96912SShaul Triebitz 	} else if (fw_has_capa(&mvm->fw->ucode_capa,
1300fe959c7bSEmmanuel Grumbach 			       IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
130177770189SMiri Korenblit 		te_data = &mvmvif->time_event_data;
1302fe959c7bSEmmanuel Grumbach 
130354d96912SShaul Triebitz 		if (iftype == NL80211_IFTYPE_P2P_DEVICE) {
130477770189SMiri Korenblit 			if (te_data->id >= SESSION_PROTECT_CONF_MAX_ID) {
130577770189SMiri Korenblit 				IWL_DEBUG_TE(mvm,
130677770189SMiri Korenblit 					     "No remain on channel event\n");
1307d792011bSShaul Triebitz 				mutex_unlock(&mvm->mutex);
130877770189SMiri Korenblit 				return;
130977770189SMiri Korenblit 			}
131013506583SMiri Korenblit 			iwl_mvm_cancel_session_protection(mvm, vif,
131177770189SMiri Korenblit 							  te_data->id,
131277770189SMiri Korenblit 							  te_data->link_id);
131377770189SMiri Korenblit 		} else {
131454d96912SShaul Triebitz 			iwl_mvm_remove_aux_roc_te(mvm, mvmvif,
131554d96912SShaul Triebitz 						  &mvmvif->hs_time_event_data);
131677770189SMiri Korenblit 		}
131735c1bbd9SMiri Korenblit 		goto cleanup_roc;
1318fe959c7bSEmmanuel Grumbach 	}
1319fe959c7bSEmmanuel Grumbach 
1320305d236eSEliad Peller 	te_data = iwl_mvm_get_roc_te(mvm);
1321305d236eSEliad Peller 	if (!te_data) {
1322e705c121SKalle Valo 		IWL_WARN(mvm, "No remain on channel event\n");
1323d792011bSShaul Triebitz 		mutex_unlock(&mvm->mutex);
1324e705c121SKalle Valo 		return;
1325e705c121SKalle Valo 	}
1326e705c121SKalle Valo 
1327305d236eSEliad Peller 	mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
132854d96912SShaul Triebitz 	iftype = te_data->vif->type;
132954d96912SShaul Triebitz 	if (iftype == NL80211_IFTYPE_P2P_DEVICE)
1330e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
133135c1bbd9SMiri Korenblit 	else
1332e705c121SKalle Valo 		iwl_mvm_remove_aux_roc_te(mvm, mvmvif, te_data);
133335c1bbd9SMiri Korenblit 
133435c1bbd9SMiri Korenblit cleanup_roc:
133535c1bbd9SMiri Korenblit 	/*
133635c1bbd9SMiri Korenblit 	 * In case we get here before the ROC event started,
133735c1bbd9SMiri Korenblit 	 * (so the status bit isn't set) set it here so iwl_mvm_cleanup_roc will
133835c1bbd9SMiri Korenblit 	 * cleanup things properly
133935c1bbd9SMiri Korenblit 	 */
134054d96912SShaul Triebitz 	if (p2p_aux || iftype != NL80211_IFTYPE_P2P_DEVICE)
134154d96912SShaul Triebitz 		set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
134254d96912SShaul Triebitz 	else
134354d96912SShaul Triebitz 		set_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status);
1344a1efeb82SYedidya Benshimol 
1345a1efeb82SYedidya Benshimol 	/* Mutex is released inside this function */
134635c1bbd9SMiri Korenblit 	iwl_mvm_cleanup_roc(mvm);
1347a76b5731SAvraham Stern }
1348e705c121SKalle Valo 
iwl_mvm_remove_csa_period(struct iwl_mvm * mvm,struct ieee80211_vif * vif)134958ddd9b6SEmmanuel Grumbach void iwl_mvm_remove_csa_period(struct iwl_mvm *mvm,
135058ddd9b6SEmmanuel Grumbach 			       struct ieee80211_vif *vif)
135158ddd9b6SEmmanuel Grumbach {
135258ddd9b6SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
135358ddd9b6SEmmanuel Grumbach 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
135458ddd9b6SEmmanuel Grumbach 	u32 id;
135558ddd9b6SEmmanuel Grumbach 
135658ddd9b6SEmmanuel Grumbach 	lockdep_assert_held(&mvm->mutex);
135758ddd9b6SEmmanuel Grumbach 
135858ddd9b6SEmmanuel Grumbach 	spin_lock_bh(&mvm->time_event_lock);
135958ddd9b6SEmmanuel Grumbach 	id = te_data->id;
136058ddd9b6SEmmanuel Grumbach 	spin_unlock_bh(&mvm->time_event_lock);
136158ddd9b6SEmmanuel Grumbach 
136258ddd9b6SEmmanuel Grumbach 	if (id != TE_CHANNEL_SWITCH_PERIOD)
136358ddd9b6SEmmanuel Grumbach 		return;
136458ddd9b6SEmmanuel Grumbach 
136558ddd9b6SEmmanuel Grumbach 	iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
136658ddd9b6SEmmanuel Grumbach }
136758ddd9b6SEmmanuel Grumbach 
iwl_mvm_schedule_csa_period(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 duration,u32 apply_time)1368e705c121SKalle Valo int iwl_mvm_schedule_csa_period(struct iwl_mvm *mvm,
1369e705c121SKalle Valo 				struct ieee80211_vif *vif,
1370e705c121SKalle Valo 				u32 duration, u32 apply_time)
1371e705c121SKalle Valo {
1372e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1373e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1374e705c121SKalle Valo 	struct iwl_time_event_cmd time_cmd = {};
1375e705c121SKalle Valo 
1376e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1377e705c121SKalle Valo 
1378e705c121SKalle Valo 	if (te_data->running) {
13793edfb5f4SAvraham Stern 		u32 id;
13803edfb5f4SAvraham Stern 
13813edfb5f4SAvraham Stern 		spin_lock_bh(&mvm->time_event_lock);
13823edfb5f4SAvraham Stern 		id = te_data->id;
13833edfb5f4SAvraham Stern 		spin_unlock_bh(&mvm->time_event_lock);
13843edfb5f4SAvraham Stern 
13853edfb5f4SAvraham Stern 		if (id == TE_CHANNEL_SWITCH_PERIOD) {
1386e705c121SKalle Valo 			IWL_DEBUG_TE(mvm, "CS period is already scheduled\n");
1387e705c121SKalle Valo 			return -EBUSY;
1388e705c121SKalle Valo 		}
1389e705c121SKalle Valo 
13903edfb5f4SAvraham Stern 		/*
13913edfb5f4SAvraham Stern 		 * Remove the session protection time event to allow the
13923edfb5f4SAvraham Stern 		 * channel switch. If we got here, we just heard a beacon so
13933edfb5f4SAvraham Stern 		 * the session protection is not needed anymore anyway.
13943edfb5f4SAvraham Stern 		 */
13953edfb5f4SAvraham Stern 		iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
13963edfb5f4SAvraham Stern 	}
13973edfb5f4SAvraham Stern 
1398e705c121SKalle Valo 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
1399e705c121SKalle Valo 	time_cmd.id_and_color =
1400e705c121SKalle Valo 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
1401e705c121SKalle Valo 	time_cmd.id = cpu_to_le32(TE_CHANNEL_SWITCH_PERIOD);
1402e705c121SKalle Valo 	time_cmd.apply_time = cpu_to_le32(apply_time);
1403e705c121SKalle Valo 	time_cmd.max_frags = TE_V2_FRAG_NONE;
1404e705c121SKalle Valo 	time_cmd.duration = cpu_to_le32(duration);
1405e705c121SKalle Valo 	time_cmd.repeat = 1;
1406e705c121SKalle Valo 	time_cmd.interval = cpu_to_le32(1);
1407e705c121SKalle Valo 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
1408e705c121SKalle Valo 				      TE_V2_ABSENCE);
140940d53f4aSAndrei Otcheretianski 	if (!apply_time)
141040d53f4aSAndrei Otcheretianski 		time_cmd.policy |= cpu_to_le16(TE_V2_START_IMMEDIATELY);
1411e705c121SKalle Valo 
1412e705c121SKalle Valo 	return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
1413e705c121SKalle Valo }
1414fe959c7bSEmmanuel Grumbach 
iwl_mvm_session_prot_notif(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)1415b5b878e3SEmmanuel Grumbach static bool iwl_mvm_session_prot_notif(struct iwl_notif_wait_data *notif_wait,
1416b5b878e3SEmmanuel Grumbach 				       struct iwl_rx_packet *pkt, void *data)
1417b5b878e3SEmmanuel Grumbach {
1418b5b878e3SEmmanuel Grumbach 	struct iwl_mvm *mvm =
1419b5b878e3SEmmanuel Grumbach 		container_of(notif_wait, struct iwl_mvm, notif_wait);
1420b5b878e3SEmmanuel Grumbach 	struct iwl_mvm_session_prot_notif *resp;
1421b5b878e3SEmmanuel Grumbach 	int resp_len = iwl_rx_packet_payload_len(pkt);
1422b5b878e3SEmmanuel Grumbach 
1423b5b878e3SEmmanuel Grumbach 	if (WARN_ON(pkt->hdr.cmd != SESSION_PROTECTION_NOTIF ||
1424b5b878e3SEmmanuel Grumbach 		    pkt->hdr.group_id != MAC_CONF_GROUP))
1425b5b878e3SEmmanuel Grumbach 		return true;
1426b5b878e3SEmmanuel Grumbach 
1427b5b878e3SEmmanuel Grumbach 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
1428b5b878e3SEmmanuel Grumbach 		IWL_ERR(mvm, "Invalid SESSION_PROTECTION_NOTIF response\n");
1429b5b878e3SEmmanuel Grumbach 		return true;
1430b5b878e3SEmmanuel Grumbach 	}
1431b5b878e3SEmmanuel Grumbach 
1432b5b878e3SEmmanuel Grumbach 	resp = (void *)pkt->data;
1433b5b878e3SEmmanuel Grumbach 
1434b5b878e3SEmmanuel Grumbach 	if (!resp->status)
1435b5b878e3SEmmanuel Grumbach 		IWL_ERR(mvm,
1436b5b878e3SEmmanuel Grumbach 			"TIME_EVENT_NOTIFICATION received but not executed\n");
1437b5b878e3SEmmanuel Grumbach 
1438b5b878e3SEmmanuel Grumbach 	return true;
1439b5b878e3SEmmanuel Grumbach }
1440b5b878e3SEmmanuel Grumbach 
iwl_mvm_schedule_session_protection(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 duration,u32 min_duration,bool wait_for_notif,unsigned int link_id)1441fe959c7bSEmmanuel Grumbach void iwl_mvm_schedule_session_protection(struct iwl_mvm *mvm,
1442fe959c7bSEmmanuel Grumbach 					 struct ieee80211_vif *vif,
1443b5b878e3SEmmanuel Grumbach 					 u32 duration, u32 min_duration,
144423673041SMiri Korenblit 					 bool wait_for_notif,
144523673041SMiri Korenblit 					 unsigned int link_id)
1446fe959c7bSEmmanuel Grumbach {
1447fe959c7bSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1448fe959c7bSEmmanuel Grumbach 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1449f0c86427SJohannes Berg 	const u16 notif[] = { WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_NOTIF) };
1450b5b878e3SEmmanuel Grumbach 	struct iwl_notification_wait wait_notif;
14516c8ce238SMiri Korenblit 	int mac_link_id = iwl_mvm_get_session_prot_id(mvm, vif, (s8)link_id);
1452fe959c7bSEmmanuel Grumbach 	struct iwl_mvm_session_prot_cmd cmd = {
145313506583SMiri Korenblit 		.id_and_color = cpu_to_le32(mac_link_id),
1454fe959c7bSEmmanuel Grumbach 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
14558e967c13SShaul Triebitz 		.conf_id = cpu_to_le32(SESSION_PROTECT_CONF_ASSOC),
1456fe959c7bSEmmanuel Grumbach 		.duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
1457fe959c7bSEmmanuel Grumbach 	};
1458fe959c7bSEmmanuel Grumbach 
145913506583SMiri Korenblit 	if (mac_link_id < 0)
146013506583SMiri Korenblit 		return;
146113506583SMiri Korenblit 
1462fe959c7bSEmmanuel Grumbach 	lockdep_assert_held(&mvm->mutex);
1463fe959c7bSEmmanuel Grumbach 
1464fe959c7bSEmmanuel Grumbach 	spin_lock_bh(&mvm->time_event_lock);
146523673041SMiri Korenblit 	if (te_data->running && te_data->link_id == link_id &&
1466fe959c7bSEmmanuel Grumbach 	    time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
1467fe959c7bSEmmanuel Grumbach 		IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
1468fe959c7bSEmmanuel Grumbach 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
1469fe959c7bSEmmanuel Grumbach 		spin_unlock_bh(&mvm->time_event_lock);
1470fe959c7bSEmmanuel Grumbach 
1471fe959c7bSEmmanuel Grumbach 		return;
1472fe959c7bSEmmanuel Grumbach 	}
1473fe959c7bSEmmanuel Grumbach 
1474fe959c7bSEmmanuel Grumbach 	iwl_mvm_te_clear_data(mvm, te_data);
14758e967c13SShaul Triebitz 	/*
14768e967c13SShaul Triebitz 	 * The time_event_data.id field is reused to save session
14778e967c13SShaul Triebitz 	 * protection's configuration.
14788e967c13SShaul Triebitz 	 */
14798e967c13SShaul Triebitz 	te_data->id = le32_to_cpu(cmd.conf_id);
1480fe959c7bSEmmanuel Grumbach 	te_data->duration = le32_to_cpu(cmd.duration_tu);
14817b3954a1SIlan Peer 	te_data->vif = vif;
148223673041SMiri Korenblit 	te_data->link_id = link_id;
1483fe959c7bSEmmanuel Grumbach 	spin_unlock_bh(&mvm->time_event_lock);
1484fe959c7bSEmmanuel Grumbach 
1485fe959c7bSEmmanuel Grumbach 	IWL_DEBUG_TE(mvm, "Add new session protection, duration %d TU\n",
1486fe959c7bSEmmanuel Grumbach 		     le32_to_cpu(cmd.duration_tu));
1487fe959c7bSEmmanuel Grumbach 
1488b5b878e3SEmmanuel Grumbach 	if (!wait_for_notif) {
1489b5b878e3SEmmanuel Grumbach 		if (iwl_mvm_send_cmd_pdu(mvm,
1490f0c86427SJohannes Berg 					 WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1491b5b878e3SEmmanuel Grumbach 					 0, sizeof(cmd), &cmd)) {
149213506583SMiri Korenblit 			goto send_cmd_err;
1493fe959c7bSEmmanuel Grumbach 		}
1494b5b878e3SEmmanuel Grumbach 
1495b5b878e3SEmmanuel Grumbach 		return;
1496b5b878e3SEmmanuel Grumbach 	}
1497b5b878e3SEmmanuel Grumbach 
1498b5b878e3SEmmanuel Grumbach 	iwl_init_notification_wait(&mvm->notif_wait, &wait_notif,
1499b5b878e3SEmmanuel Grumbach 				   notif, ARRAY_SIZE(notif),
1500b5b878e3SEmmanuel Grumbach 				   iwl_mvm_session_prot_notif, NULL);
1501b5b878e3SEmmanuel Grumbach 
1502b5b878e3SEmmanuel Grumbach 	if (iwl_mvm_send_cmd_pdu(mvm,
1503f0c86427SJohannes Berg 				 WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1504b5b878e3SEmmanuel Grumbach 				 0, sizeof(cmd), &cmd)) {
1505b5b878e3SEmmanuel Grumbach 		iwl_remove_notification(&mvm->notif_wait, &wait_notif);
150613506583SMiri Korenblit 		goto send_cmd_err;
1507b5b878e3SEmmanuel Grumbach 	} else if (iwl_wait_notification(&mvm->notif_wait, &wait_notif,
1508b5b878e3SEmmanuel Grumbach 					 TU_TO_JIFFIES(100))) {
1509b5b878e3SEmmanuel Grumbach 		IWL_ERR(mvm,
1510b5b878e3SEmmanuel Grumbach 			"Failed to protect session until session protection\n");
1511b5b878e3SEmmanuel Grumbach 	}
151213506583SMiri Korenblit 	return;
151313506583SMiri Korenblit 
151413506583SMiri Korenblit send_cmd_err:
151513506583SMiri Korenblit 	IWL_ERR(mvm,
151613506583SMiri Korenblit 		"Couldn't send the SESSION_PROTECTION_CMD\n");
151713506583SMiri Korenblit 	spin_lock_bh(&mvm->time_event_lock);
151813506583SMiri Korenblit 	iwl_mvm_te_clear_data(mvm, te_data);
151913506583SMiri Korenblit 	spin_unlock_bh(&mvm->time_event_lock);
1520fe959c7bSEmmanuel Grumbach }
1521