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