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