Linux Audio

Check our new training course

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