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 bool __iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
645 					struct iwl_mvm_time_event_data *te_data,
646 					u32 *uid)
647 {
648 	u32 id;
649 
650 	/*
651 	 * It is possible that by the time we got to this point the time
652 	 * event was already removed.
653 	 */
654 	spin_lock_bh(&mvm->time_event_lock);
655 
656 	/* Save time event uid before clearing its data */
657 	*uid = te_data->uid;
658 	id = te_data->id;
659 
660 	/*
661 	 * The clear_data function handles time events that were already removed
662 	 */
663 	iwl_mvm_te_clear_data(mvm, te_data);
664 	spin_unlock_bh(&mvm->time_event_lock);
665 
666 	/*
667 	 * It is possible that by the time we try to remove it, the time event
668 	 * has already ended and removed. In such a case there is no need to
669 	 * send a removal command.
670 	 */
671 	if (id == TE_MAX) {
672 		IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", *uid);
673 		return false;
674 	}
675 
676 	return true;
677 }
678 
679 /*
680  * Explicit request to remove a aux roc time event. The removal of a time
681  * event needs to be synchronized with the flow of a time event's end
682  * notification, which also removes the time event from the op mode
683  * data structures.
684  */
685 static void iwl_mvm_remove_aux_roc_te(struct iwl_mvm *mvm,
686 				      struct iwl_mvm_vif *mvmvif,
687 				      struct iwl_mvm_time_event_data *te_data)
688 {
689 	struct iwl_hs20_roc_req aux_cmd = {};
690 	u16 len = sizeof(aux_cmd) - iwl_mvm_chan_info_padding(mvm);
691 
692 	u32 uid;
693 	int ret;
694 
695 	if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
696 		return;
697 
698 	aux_cmd.event_unique_id = cpu_to_le32(uid);
699 	aux_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
700 	aux_cmd.id_and_color =
701 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
702 	IWL_DEBUG_TE(mvm, "Removing BSS AUX ROC TE 0x%x\n",
703 		     le32_to_cpu(aux_cmd.event_unique_id));
704 	ret = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0,
705 				   len, &aux_cmd);
706 
707 	if (WARN_ON(ret))
708 		return;
709 }
710 
711 /*
712  * Explicit request to remove a time event. The removal of a time event needs to
713  * be synchronized with the flow of a time event's end notification, which also
714  * removes the time event from the op mode data structures.
715  */
716 void iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
717 			       struct iwl_mvm_vif *mvmvif,
718 			       struct iwl_mvm_time_event_data *te_data)
719 {
720 	struct iwl_time_event_cmd time_cmd = {};
721 	u32 uid;
722 	int ret;
723 
724 	if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
725 		return;
726 
727 	/* When we remove a TE, the UID is to be set in the id field */
728 	time_cmd.id = cpu_to_le32(uid);
729 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
730 	time_cmd.id_and_color =
731 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
732 
733 	IWL_DEBUG_TE(mvm, "Removing TE 0x%x\n", le32_to_cpu(time_cmd.id));
734 	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
735 				   sizeof(time_cmd), &time_cmd);
736 	if (WARN_ON(ret))
737 		return;
738 }
739 
740 /*
741  * When the firmware supports the session protection API,
742  * this is not needed since it'll automatically remove the
743  * session protection after association + beacon reception.
744  */
745 void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,
746 				     struct ieee80211_vif *vif)
747 {
748 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
749 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
750 	u32 id;
751 
752 	lockdep_assert_held(&mvm->mutex);
753 
754 	spin_lock_bh(&mvm->time_event_lock);
755 	id = te_data->id;
756 	spin_unlock_bh(&mvm->time_event_lock);
757 
758 	if (id != TE_BSS_STA_AGGRESSIVE_ASSOC) {
759 		IWL_DEBUG_TE(mvm,
760 			     "don't remove TE with id=%u (not session protection)\n",
761 			     id);
762 		return;
763 	}
764 
765 	iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
766 }
767 
768 void iwl_mvm_rx_session_protect_notif(struct iwl_mvm *mvm,
769 				      struct iwl_rx_cmd_buffer *rxb)
770 {
771 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
772 	struct iwl_mvm_session_prot_notif *notif = (void *)pkt->data;
773 	struct ieee80211_vif *vif;
774 
775 	rcu_read_lock();
776 	vif = iwl_mvm_rcu_dereference_vif_id(mvm, le32_to_cpu(notif->mac_id),
777 					     true);
778 
779 	if (!vif)
780 		goto out_unlock;
781 
782 	/* The vif is not a P2P_DEVICE, maintain its time_event_data */
783 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
784 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
785 		struct iwl_mvm_time_event_data *te_data =
786 			&mvmvif->time_event_data;
787 
788 		if (!le32_to_cpu(notif->status)) {
789 			iwl_mvm_te_check_disconnect(mvm, vif,
790 						    "Session protection failure");
791 			spin_lock_bh(&mvm->time_event_lock);
792 			iwl_mvm_te_clear_data(mvm, te_data);
793 			spin_unlock_bh(&mvm->time_event_lock);
794 		}
795 
796 		if (le32_to_cpu(notif->start)) {
797 			spin_lock_bh(&mvm->time_event_lock);
798 			te_data->running = le32_to_cpu(notif->start);
799 			te_data->end_jiffies =
800 				TU_TO_EXP_TIME(te_data->duration);
801 			spin_unlock_bh(&mvm->time_event_lock);
802 		} else {
803 			/*
804 			 * By now, we should have finished association
805 			 * and know the dtim period.
806 			 */
807 			iwl_mvm_te_check_disconnect(mvm, vif,
808 						    "No beacon heard and the session protection is over already...");
809 			spin_lock_bh(&mvm->time_event_lock);
810 			iwl_mvm_te_clear_data(mvm, te_data);
811 			spin_unlock_bh(&mvm->time_event_lock);
812 		}
813 
814 		goto out_unlock;
815 	}
816 
817 	if (!le32_to_cpu(notif->status) || !le32_to_cpu(notif->start)) {
818 		/* End TE, notify mac80211 */
819 		ieee80211_remain_on_channel_expired(mvm->hw);
820 		set_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status);
821 		iwl_mvm_roc_finished(mvm);
822 	} else if (le32_to_cpu(notif->start)) {
823 		set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
824 		ieee80211_ready_on_channel(mvm->hw); /* Start TE */
825 	}
826 
827  out_unlock:
828 	rcu_read_unlock();
829 }
830 
831 static int
832 iwl_mvm_start_p2p_roc_session_protection(struct iwl_mvm *mvm,
833 					 struct ieee80211_vif *vif,
834 					 int duration,
835 					 enum ieee80211_roc_type type)
836 {
837 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
838 	struct iwl_mvm_session_prot_cmd cmd = {
839 		.id_and_color =
840 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
841 							mvmvif->color)),
842 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
843 		.duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
844 	};
845 
846 	lockdep_assert_held(&mvm->mutex);
847 
848 	switch (type) {
849 	case IEEE80211_ROC_TYPE_NORMAL:
850 		cmd.conf_id =
851 			cpu_to_le32(SESSION_PROTECT_CONF_P2P_DEVICE_DISCOV);
852 		break;
853 	case IEEE80211_ROC_TYPE_MGMT_TX:
854 		cmd.conf_id =
855 			cpu_to_le32(SESSION_PROTECT_CONF_P2P_GO_NEGOTIATION);
856 		break;
857 	default:
858 		WARN_ONCE(1, "Got an invalid ROC type\n");
859 		return -EINVAL;
860 	}
861 
862 	return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(SESSION_PROTECTION_CMD,
863 						    MAC_CONF_GROUP, 0),
864 				    0, sizeof(cmd), &cmd);
865 }
866 
867 int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
868 			  int duration, enum ieee80211_roc_type type)
869 {
870 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
871 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
872 	struct iwl_time_event_cmd time_cmd = {};
873 
874 	lockdep_assert_held(&mvm->mutex);
875 	if (te_data->running) {
876 		IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");
877 		return -EBUSY;
878 	}
879 
880 	if (fw_has_capa(&mvm->fw->ucode_capa,
881 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
882 		return iwl_mvm_start_p2p_roc_session_protection(mvm, vif,
883 								duration,
884 								type);
885 
886 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
887 	time_cmd.id_and_color =
888 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
889 
890 	switch (type) {
891 	case IEEE80211_ROC_TYPE_NORMAL:
892 		time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);
893 		break;
894 	case IEEE80211_ROC_TYPE_MGMT_TX:
895 		time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);
896 		break;
897 	default:
898 		WARN_ONCE(1, "Got an invalid ROC type\n");
899 		return -EINVAL;
900 	}
901 
902 	time_cmd.apply_time = cpu_to_le32(0);
903 	time_cmd.interval = cpu_to_le32(1);
904 
905 	/*
906 	 * The P2P Device TEs can have lower priority than other events
907 	 * that are being scheduled by the driver/fw, and thus it might not be
908 	 * scheduled. To improve the chances of it being scheduled, allow them
909 	 * to be fragmented, and in addition allow them to be delayed.
910 	 */
911 	time_cmd.max_frags = min(MSEC_TO_TU(duration)/50, TE_V2_FRAG_ENDLESS);
912 	time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));
913 	time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));
914 	time_cmd.repeat = 1;
915 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
916 				      TE_V2_NOTIF_HOST_EVENT_END |
917 				      TE_V2_START_IMMEDIATELY);
918 
919 	return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
920 }
921 
922 static struct iwl_mvm_time_event_data *iwl_mvm_get_roc_te(struct iwl_mvm *mvm)
923 {
924 	struct iwl_mvm_time_event_data *te_data;
925 
926 	lockdep_assert_held(&mvm->mutex);
927 
928 	spin_lock_bh(&mvm->time_event_lock);
929 
930 	/*
931 	 * Iterate over the list of time events and find the time event that is
932 	 * associated with a P2P_DEVICE interface.
933 	 * This assumes that a P2P_DEVICE interface can have only a single time
934 	 * event at any given time and this time event coresponds to a ROC
935 	 * request
936 	 */
937 	list_for_each_entry(te_data, &mvm->time_event_list, list) {
938 		if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE)
939 			goto out;
940 	}
941 
942 	/* There can only be at most one AUX ROC time event, we just use the
943 	 * list to simplify/unify code. Remove it if it exists.
944 	 */
945 	te_data = list_first_entry_or_null(&mvm->aux_roc_te_list,
946 					   struct iwl_mvm_time_event_data,
947 					   list);
948 out:
949 	spin_unlock_bh(&mvm->time_event_lock);
950 	return te_data;
951 }
952 
953 void iwl_mvm_cleanup_roc_te(struct iwl_mvm *mvm)
954 {
955 	struct iwl_mvm_time_event_data *te_data;
956 	u32 uid;
957 
958 	te_data = iwl_mvm_get_roc_te(mvm);
959 	if (te_data)
960 		__iwl_mvm_remove_time_event(mvm, te_data, &uid);
961 }
962 
963 static void iwl_mvm_cancel_session_protection(struct iwl_mvm *mvm,
964 					      struct iwl_mvm_vif *mvmvif)
965 {
966 	struct iwl_mvm_session_prot_cmd cmd = {
967 		.id_and_color =
968 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
969 							mvmvif->color)),
970 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
971 	};
972 	int ret;
973 
974 	ret = iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(SESSION_PROTECTION_CMD,
975 						   MAC_CONF_GROUP, 0),
976 				   0, sizeof(cmd), &cmd);
977 	if (ret)
978 		IWL_ERR(mvm,
979 			"Couldn't send the SESSION_PROTECTION_CMD: %d\n", ret);
980 }
981 
982 void iwl_mvm_stop_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
983 {
984 	struct iwl_mvm_vif *mvmvif;
985 	struct iwl_mvm_time_event_data *te_data;
986 
987 	if (fw_has_capa(&mvm->fw->ucode_capa,
988 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
989 		mvmvif = iwl_mvm_vif_from_mac80211(vif);
990 
991 		iwl_mvm_cancel_session_protection(mvm, mvmvif);
992 
993 		if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
994 			set_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status);
995 
996 		iwl_mvm_roc_finished(mvm);
997 
998 		return;
999 	}
1000 
1001 	te_data = iwl_mvm_get_roc_te(mvm);
1002 	if (!te_data) {
1003 		IWL_WARN(mvm, "No remain on channel event\n");
1004 		return;
1005 	}
1006 
1007 	mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
1008 
1009 	if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1010 		iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1011 		set_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status);
1012 	} else {
1013 		iwl_mvm_remove_aux_roc_te(mvm, mvmvif, te_data);
1014 	}
1015 
1016 	iwl_mvm_roc_finished(mvm);
1017 }
1018 
1019 void iwl_mvm_remove_csa_period(struct iwl_mvm *mvm,
1020 			       struct ieee80211_vif *vif)
1021 {
1022 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1023 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1024 	u32 id;
1025 
1026 	lockdep_assert_held(&mvm->mutex);
1027 
1028 	if (!te_data->running)
1029 		return;
1030 
1031 	spin_lock_bh(&mvm->time_event_lock);
1032 	id = te_data->id;
1033 	spin_unlock_bh(&mvm->time_event_lock);
1034 
1035 	if (id != TE_CHANNEL_SWITCH_PERIOD)
1036 		return;
1037 
1038 	iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1039 }
1040 
1041 int iwl_mvm_schedule_csa_period(struct iwl_mvm *mvm,
1042 				struct ieee80211_vif *vif,
1043 				u32 duration, u32 apply_time)
1044 {
1045 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1046 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1047 	struct iwl_time_event_cmd time_cmd = {};
1048 
1049 	lockdep_assert_held(&mvm->mutex);
1050 
1051 	if (te_data->running) {
1052 		u32 id;
1053 
1054 		spin_lock_bh(&mvm->time_event_lock);
1055 		id = te_data->id;
1056 		spin_unlock_bh(&mvm->time_event_lock);
1057 
1058 		if (id == TE_CHANNEL_SWITCH_PERIOD) {
1059 			IWL_DEBUG_TE(mvm, "CS period is already scheduled\n");
1060 			return -EBUSY;
1061 		}
1062 
1063 		/*
1064 		 * Remove the session protection time event to allow the
1065 		 * channel switch. If we got here, we just heard a beacon so
1066 		 * the session protection is not needed anymore anyway.
1067 		 */
1068 		iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1069 	}
1070 
1071 	time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
1072 	time_cmd.id_and_color =
1073 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
1074 	time_cmd.id = cpu_to_le32(TE_CHANNEL_SWITCH_PERIOD);
1075 	time_cmd.apply_time = cpu_to_le32(apply_time);
1076 	time_cmd.max_frags = TE_V2_FRAG_NONE;
1077 	time_cmd.duration = cpu_to_le32(duration);
1078 	time_cmd.repeat = 1;
1079 	time_cmd.interval = cpu_to_le32(1);
1080 	time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
1081 				      TE_V2_ABSENCE);
1082 	if (!apply_time)
1083 		time_cmd.policy |= cpu_to_le16(TE_V2_START_IMMEDIATELY);
1084 
1085 	return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
1086 }
1087 
1088 static bool iwl_mvm_session_prot_notif(struct iwl_notif_wait_data *notif_wait,
1089 				       struct iwl_rx_packet *pkt, void *data)
1090 {
1091 	struct iwl_mvm *mvm =
1092 		container_of(notif_wait, struct iwl_mvm, notif_wait);
1093 	struct iwl_mvm_session_prot_notif *resp;
1094 	int resp_len = iwl_rx_packet_payload_len(pkt);
1095 
1096 	if (WARN_ON(pkt->hdr.cmd != SESSION_PROTECTION_NOTIF ||
1097 		    pkt->hdr.group_id != MAC_CONF_GROUP))
1098 		return true;
1099 
1100 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
1101 		IWL_ERR(mvm, "Invalid SESSION_PROTECTION_NOTIF response\n");
1102 		return true;
1103 	}
1104 
1105 	resp = (void *)pkt->data;
1106 
1107 	if (!resp->status)
1108 		IWL_ERR(mvm,
1109 			"TIME_EVENT_NOTIFICATION received but not executed\n");
1110 
1111 	return true;
1112 }
1113 
1114 void iwl_mvm_schedule_session_protection(struct iwl_mvm *mvm,
1115 					 struct ieee80211_vif *vif,
1116 					 u32 duration, u32 min_duration,
1117 					 bool wait_for_notif)
1118 {
1119 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1120 	struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1121 	const u16 notif[] = { iwl_cmd_id(SESSION_PROTECTION_NOTIF,
1122 					 MAC_CONF_GROUP, 0) };
1123 	struct iwl_notification_wait wait_notif;
1124 	struct iwl_mvm_session_prot_cmd cmd = {
1125 		.id_and_color =
1126 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1127 							mvmvif->color)),
1128 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
1129 		.conf_id = cpu_to_le32(SESSION_PROTECT_CONF_ASSOC),
1130 		.duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
1131 	};
1132 
1133 	lockdep_assert_held(&mvm->mutex);
1134 
1135 	spin_lock_bh(&mvm->time_event_lock);
1136 	if (te_data->running &&
1137 	    time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
1138 		IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
1139 			     jiffies_to_msecs(te_data->end_jiffies - jiffies));
1140 		spin_unlock_bh(&mvm->time_event_lock);
1141 
1142 		return;
1143 	}
1144 
1145 	iwl_mvm_te_clear_data(mvm, te_data);
1146 	te_data->duration = le32_to_cpu(cmd.duration_tu);
1147 	spin_unlock_bh(&mvm->time_event_lock);
1148 
1149 	IWL_DEBUG_TE(mvm, "Add new session protection, duration %d TU\n",
1150 		     le32_to_cpu(cmd.duration_tu));
1151 
1152 	if (!wait_for_notif) {
1153 		if (iwl_mvm_send_cmd_pdu(mvm,
1154 					 iwl_cmd_id(SESSION_PROTECTION_CMD,
1155 						    MAC_CONF_GROUP, 0),
1156 					 0, sizeof(cmd), &cmd)) {
1157 			IWL_ERR(mvm,
1158 				"Couldn't send the SESSION_PROTECTION_CMD\n");
1159 			spin_lock_bh(&mvm->time_event_lock);
1160 			iwl_mvm_te_clear_data(mvm, te_data);
1161 			spin_unlock_bh(&mvm->time_event_lock);
1162 		}
1163 
1164 		return;
1165 	}
1166 
1167 	iwl_init_notification_wait(&mvm->notif_wait, &wait_notif,
1168 				   notif, ARRAY_SIZE(notif),
1169 				   iwl_mvm_session_prot_notif, NULL);
1170 
1171 	if (iwl_mvm_send_cmd_pdu(mvm,
1172 				 iwl_cmd_id(SESSION_PROTECTION_CMD,
1173 					    MAC_CONF_GROUP, 0),
1174 				 0, sizeof(cmd), &cmd)) {
1175 		IWL_ERR(mvm,
1176 			"Couldn't send the SESSION_PROTECTION_CMD\n");
1177 		iwl_remove_notification(&mvm->notif_wait, &wait_notif);
1178 	} else if (iwl_wait_notification(&mvm->notif_wait, &wait_notif,
1179 					 TU_TO_JIFFIES(100))) {
1180 		IWL_ERR(mvm,
1181 			"Failed to protect session until session protection\n");
1182 	}
1183 }
1184