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