1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
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
7 #include <linux/jiffies.h>
8 #include <net/mac80211.h>
10 #include "fw/notif-wait.h"
11 #include "iwl-trans.h"
13 #include "time-event.h"
19 * For the high priority TE use a time event type that has similar priority to
20 * the FW's action scan priority.
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
25 void iwl_mvm_te_clear_data(struct iwl_mvm *mvm,
26 struct iwl_mvm_time_event_data *te_data)
28 lockdep_assert_held(&mvm->time_event_lock);
30 if (!te_data || !te_data->vif)
33 list_del(&te_data->list);
36 * the list is only used for AUX ROC events so make sure it is always
39 INIT_LIST_HEAD(&te_data->list);
41 te_data->running = false;
47 void iwl_mvm_roc_done_wk(struct work_struct *wk)
49 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, roc_done_wk);
52 * Clear the ROC_RUNNING /ROC_AUX_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).
58 clear_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
59 clear_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
64 * Flush the offchannel queue -- this is called when the time
65 * event finishes or is canceled, so that frames queued for it
66 * won't get stuck on the queue and be transmitted in the next
70 mutex_lock(&mvm->mutex);
71 if (test_and_clear_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status)) {
72 struct iwl_mvm_vif *mvmvif;
75 * NB: access to this pointer would be racy, but the flush bit
76 * can only be set when we had a P2P-Device VIF, and we have a
77 * flush of this work in iwl_mvm_prepare_mac_removal() so it's
81 if (!WARN_ON(!mvm->p2p_device_vif)) {
82 mvmvif = iwl_mvm_vif_from_mac80211(mvm->p2p_device_vif);
83 iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true);
86 /* do the same in case of hot spot 2.0 */
87 iwl_mvm_flush_sta(mvm, &mvm->aux_sta, true);
88 /* In newer version of this command an aux station is added only
89 * in cases of dedicated tx queue and need to be removed in end
91 if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
93 iwl_mvm_rm_aux_sta(mvm);
96 mutex_unlock(&mvm->mutex);
99 static void iwl_mvm_roc_finished(struct iwl_mvm *mvm)
102 * Of course, our status bit is just as racy as mac80211, so in
103 * addition, fire off the work struct which will drop all frames
104 * from the hardware queues that made it through the race. First
105 * it will of course synchronize the TX path to make sure that
106 * any *new* TX will be rejected.
108 schedule_work(&mvm->roc_done_wk);
111 static void iwl_mvm_csa_noa_start(struct iwl_mvm *mvm)
113 struct ieee80211_vif *csa_vif;
117 csa_vif = rcu_dereference(mvm->csa_vif);
118 if (!csa_vif || !csa_vif->csa_active)
121 IWL_DEBUG_TE(mvm, "CSA NOA started\n");
124 * CSA NoA is started but we still have beacons to
125 * transmit on the current channel.
126 * So we just do nothing here and the switch
127 * will be performed on the last TBTT.
129 if (!ieee80211_beacon_cntdwn_is_complete(csa_vif)) {
130 IWL_WARN(mvm, "CSA NOA started too early\n");
134 ieee80211_csa_finish(csa_vif);
138 RCU_INIT_POINTER(mvm->csa_vif, NULL);
146 static bool iwl_mvm_te_check_disconnect(struct iwl_mvm *mvm,
147 struct ieee80211_vif *vif,
150 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
152 if (vif->type != NL80211_IFTYPE_STATION)
155 if (!mvmvif->csa_bcn_pending && vif->bss_conf.assoc &&
156 vif->bss_conf.dtim_period)
159 IWL_ERR(mvm, "%s\n", errmsg);
161 if (mvmvif->csa_bcn_pending) {
162 struct iwl_mvm_sta *mvmsta;
165 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
166 if (!WARN_ON(!mvmsta))
167 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
171 if (vif->bss_conf.assoc) {
173 * When not associated, this will be called from
174 * iwl_mvm_event_mlme_callback_ini()
176 iwl_dbg_tlv_time_point(&mvm->fwrt,
177 IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
181 iwl_mvm_connection_loss(mvm, vif, errmsg);
186 iwl_mvm_te_handle_notify_csa(struct iwl_mvm *mvm,
187 struct iwl_mvm_time_event_data *te_data,
188 struct iwl_time_event_notif *notif)
190 struct ieee80211_vif *vif = te_data->vif;
191 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
194 IWL_DEBUG_TE(mvm, "CSA time event failed to start\n");
196 switch (te_data->vif->type) {
197 case NL80211_IFTYPE_AP:
199 mvmvif->csa_failed = true;
200 iwl_mvm_csa_noa_start(mvm);
202 case NL80211_IFTYPE_STATION:
203 if (!notif->status) {
204 iwl_mvm_connection_loss(mvm, vif,
205 "CSA TE failed to start");
208 iwl_mvm_csa_client_absent(mvm, te_data->vif);
209 cancel_delayed_work(&mvmvif->csa_work);
210 ieee80211_chswitch_done(te_data->vif, true);
213 /* should never happen */
218 /* we don't need it anymore */
219 iwl_mvm_te_clear_data(mvm, te_data);
222 static void iwl_mvm_te_check_trigger(struct iwl_mvm *mvm,
223 struct iwl_time_event_notif *notif,
224 struct iwl_mvm_time_event_data *te_data)
226 struct iwl_fw_dbg_trigger_tlv *trig;
227 struct iwl_fw_dbg_trigger_time_event *te_trig;
230 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
231 ieee80211_vif_to_wdev(te_data->vif),
232 FW_DBG_TRIGGER_TIME_EVENT);
236 te_trig = (void *)trig->data;
238 for (i = 0; i < ARRAY_SIZE(te_trig->time_events); i++) {
239 u32 trig_te_id = le32_to_cpu(te_trig->time_events[i].id);
240 u32 trig_action_bitmap =
241 le32_to_cpu(te_trig->time_events[i].action_bitmap);
242 u32 trig_status_bitmap =
243 le32_to_cpu(te_trig->time_events[i].status_bitmap);
245 if (trig_te_id != te_data->id ||
246 !(trig_action_bitmap & le32_to_cpu(notif->action)) ||
247 !(trig_status_bitmap & BIT(le32_to_cpu(notif->status))))
250 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
251 "Time event %d Action 0x%x received status: %d",
253 le32_to_cpu(notif->action),
254 le32_to_cpu(notif->status));
259 static void iwl_mvm_p2p_roc_finished(struct iwl_mvm *mvm)
262 * If the IWL_MVM_STATUS_NEED_FLUSH_P2P is already set, then the
263 * roc_done_wk is already scheduled or running, so don't schedule it
264 * again to avoid a race where the roc_done_wk clears this bit after
265 * it is set here, affecting the next run of the roc_done_wk.
267 if (!test_and_set_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status))
268 iwl_mvm_roc_finished(mvm);
272 * Handles a FW notification for an event that is known to the driver.
274 * @mvm: the mvm component
275 * @te_data: the time event data
276 * @notif: the notification data corresponding the time event data.
278 static void iwl_mvm_te_handle_notif(struct iwl_mvm *mvm,
279 struct iwl_mvm_time_event_data *te_data,
280 struct iwl_time_event_notif *notif)
282 lockdep_assert_held(&mvm->time_event_lock);
284 IWL_DEBUG_TE(mvm, "Handle time event notif - UID = 0x%x action %d\n",
285 le32_to_cpu(notif->unique_id),
286 le32_to_cpu(notif->action));
288 iwl_mvm_te_check_trigger(mvm, notif, te_data);
291 * The FW sends the start/end time event notifications even for events
292 * that it fails to schedule. This is indicated in the status field of
293 * the notification. This happens in cases that the scheduler cannot
294 * find a schedule that can handle the event (for example requesting a
295 * P2P Device discoveribility, while there are other higher priority
296 * events in the system).
298 if (!le32_to_cpu(notif->status)) {
301 if (notif->action & cpu_to_le32(TE_V2_NOTIF_HOST_EVENT_START))
302 msg = "Time Event start notification failure";
304 msg = "Time Event end notification failure";
306 IWL_DEBUG_TE(mvm, "%s\n", msg);
308 if (iwl_mvm_te_check_disconnect(mvm, te_data->vif, msg)) {
309 iwl_mvm_te_clear_data(mvm, te_data);
314 if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_END) {
316 "TE ended - current time %lu, estimated end %lu\n",
317 jiffies, te_data->end_jiffies);
319 switch (te_data->vif->type) {
320 case NL80211_IFTYPE_P2P_DEVICE:
321 ieee80211_remain_on_channel_expired(mvm->hw);
322 iwl_mvm_p2p_roc_finished(mvm);
324 case NL80211_IFTYPE_STATION:
326 * If we are switching channel, don't disconnect
327 * if the time event is already done. Beacons can
328 * be delayed a bit after the switch.
330 if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
332 "No beacon heard and the CS time event is over, don't disconnect\n");
337 * By now, we should have finished association
338 * and know the dtim period.
340 iwl_mvm_te_check_disconnect(mvm, te_data->vif,
341 !te_data->vif->bss_conf.assoc ?
342 "Not associated and the time event is over already..." :
343 "No beacon heard and the time event is over already...");
349 iwl_mvm_te_clear_data(mvm, te_data);
350 } else if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_START) {
351 te_data->running = true;
352 te_data->end_jiffies = TU_TO_EXP_TIME(te_data->duration);
354 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
355 set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
356 ieee80211_ready_on_channel(mvm->hw);
357 } else if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
358 iwl_mvm_te_handle_notify_csa(mvm, te_data, notif);
361 IWL_WARN(mvm, "Got TE with unknown action\n");
366 * Handle A Aux ROC time event
368 static int iwl_mvm_aux_roc_te_handle_notif(struct iwl_mvm *mvm,
369 struct iwl_time_event_notif *notif)
371 struct iwl_mvm_time_event_data *te_data, *tmp;
372 bool aux_roc_te = false;
374 list_for_each_entry_safe(te_data, tmp, &mvm->aux_roc_te_list, list) {
375 if (le32_to_cpu(notif->unique_id) == te_data->uid) {
380 if (!aux_roc_te) /* Not a Aux ROC time event */
383 iwl_mvm_te_check_trigger(mvm, notif, te_data);
386 "Aux ROC time event notification - UID = 0x%x action %d (error = %d)\n",
387 le32_to_cpu(notif->unique_id),
388 le32_to_cpu(notif->action), le32_to_cpu(notif->status));
390 if (!le32_to_cpu(notif->status) ||
391 le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_END) {
392 /* End TE, notify mac80211 */
393 ieee80211_remain_on_channel_expired(mvm->hw);
394 iwl_mvm_roc_finished(mvm); /* flush aux queue */
395 list_del(&te_data->list); /* remove from list */
396 te_data->running = false;
399 te_data->id = TE_MAX;
400 } else if (le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_START) {
401 set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
402 te_data->running = true;
403 ieee80211_ready_on_channel(mvm->hw); /* Start TE */
406 "ERROR: Unknown Aux ROC Time Event (action = %d)\n",
407 le32_to_cpu(notif->action));
415 * The Rx handler for time event notifications
417 void iwl_mvm_rx_time_event_notif(struct iwl_mvm *mvm,
418 struct iwl_rx_cmd_buffer *rxb)
420 struct iwl_rx_packet *pkt = rxb_addr(rxb);
421 struct iwl_time_event_notif *notif = (void *)pkt->data;
422 struct iwl_mvm_time_event_data *te_data, *tmp;
424 IWL_DEBUG_TE(mvm, "Time event notification - UID = 0x%x action %d\n",
425 le32_to_cpu(notif->unique_id),
426 le32_to_cpu(notif->action));
428 spin_lock_bh(&mvm->time_event_lock);
429 /* This time event is triggered for Aux ROC request */
430 if (!iwl_mvm_aux_roc_te_handle_notif(mvm, notif))
433 list_for_each_entry_safe(te_data, tmp, &mvm->time_event_list, list) {
434 if (le32_to_cpu(notif->unique_id) == te_data->uid)
435 iwl_mvm_te_handle_notif(mvm, te_data, notif);
438 spin_unlock_bh(&mvm->time_event_lock);
441 static bool iwl_mvm_te_notif(struct iwl_notif_wait_data *notif_wait,
442 struct iwl_rx_packet *pkt, void *data)
444 struct iwl_mvm *mvm =
445 container_of(notif_wait, struct iwl_mvm, notif_wait);
446 struct iwl_mvm_time_event_data *te_data = data;
447 struct iwl_time_event_notif *resp;
448 int resp_len = iwl_rx_packet_payload_len(pkt);
450 if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_NOTIFICATION))
453 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
454 IWL_ERR(mvm, "Invalid TIME_EVENT_NOTIFICATION response\n");
458 resp = (void *)pkt->data;
460 /* te_data->uid is already set in the TIME_EVENT_CMD response */
461 if (le32_to_cpu(resp->unique_id) != te_data->uid)
464 IWL_DEBUG_TE(mvm, "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n",
468 "TIME_EVENT_NOTIFICATION received but not executed\n");
473 static bool iwl_mvm_time_event_response(struct iwl_notif_wait_data *notif_wait,
474 struct iwl_rx_packet *pkt, void *data)
476 struct iwl_mvm *mvm =
477 container_of(notif_wait, struct iwl_mvm, notif_wait);
478 struct iwl_mvm_time_event_data *te_data = data;
479 struct iwl_time_event_resp *resp;
480 int resp_len = iwl_rx_packet_payload_len(pkt);
482 if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_CMD))
485 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
486 IWL_ERR(mvm, "Invalid TIME_EVENT_CMD response\n");
490 resp = (void *)pkt->data;
492 /* we should never get a response to another TIME_EVENT_CMD here */
493 if (WARN_ON_ONCE(le32_to_cpu(resp->id) != te_data->id))
496 te_data->uid = le32_to_cpu(resp->unique_id);
497 IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
502 static int iwl_mvm_time_event_send_add(struct iwl_mvm *mvm,
503 struct ieee80211_vif *vif,
504 struct iwl_mvm_time_event_data *te_data,
505 struct iwl_time_event_cmd *te_cmd)
507 static const u16 time_event_response[] = { TIME_EVENT_CMD };
508 struct iwl_notification_wait wait_time_event;
511 lockdep_assert_held(&mvm->mutex);
513 IWL_DEBUG_TE(mvm, "Add new TE, duration %d TU\n",
514 le32_to_cpu(te_cmd->duration));
516 spin_lock_bh(&mvm->time_event_lock);
517 if (WARN_ON(te_data->id != TE_MAX)) {
518 spin_unlock_bh(&mvm->time_event_lock);
522 te_data->duration = le32_to_cpu(te_cmd->duration);
523 te_data->id = le32_to_cpu(te_cmd->id);
524 list_add_tail(&te_data->list, &mvm->time_event_list);
525 spin_unlock_bh(&mvm->time_event_lock);
528 * Use a notification wait, which really just processes the
529 * command response and doesn't wait for anything, in order
530 * to be able to process the response and get the UID inside
531 * the RX path. Using CMD_WANT_SKB doesn't work because it
532 * stores the buffer and then wakes up this thread, by which
533 * time another notification (that the time event started)
534 * might already be processed unsuccessfully.
536 iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
538 ARRAY_SIZE(time_event_response),
539 iwl_mvm_time_event_response, te_data);
541 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
542 sizeof(*te_cmd), te_cmd);
544 IWL_ERR(mvm, "Couldn't send TIME_EVENT_CMD: %d\n", ret);
545 iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
549 /* No need to wait for anything, so just pass 1 (0 isn't valid) */
550 ret = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
551 /* should never fail */
556 spin_lock_bh(&mvm->time_event_lock);
557 iwl_mvm_te_clear_data(mvm, te_data);
558 spin_unlock_bh(&mvm->time_event_lock);
563 void iwl_mvm_protect_session(struct iwl_mvm *mvm,
564 struct ieee80211_vif *vif,
565 u32 duration, u32 min_duration,
566 u32 max_delay, bool wait_for_notif)
568 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
569 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
570 const u16 te_notif_response[] = { TIME_EVENT_NOTIFICATION };
571 struct iwl_notification_wait wait_te_notif;
572 struct iwl_time_event_cmd time_cmd = {};
574 lockdep_assert_held(&mvm->mutex);
576 if (te_data->running &&
577 time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
578 IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
579 jiffies_to_msecs(te_data->end_jiffies - jiffies));
583 if (te_data->running) {
584 IWL_DEBUG_TE(mvm, "extend 0x%x: only %u ms left\n",
586 jiffies_to_msecs(te_data->end_jiffies - jiffies));
588 * we don't have enough time
589 * cancel the current TE and issue a new one
590 * Of course it would be better to remove the old one only
591 * when the new one is added, but we don't care if we are off
592 * channel for a bit. All we need to do, is not to return
593 * before we actually begin to be on the channel.
595 iwl_mvm_stop_session_protection(mvm, vif);
598 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
599 time_cmd.id_and_color =
600 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
601 time_cmd.id = cpu_to_le32(TE_BSS_STA_AGGRESSIVE_ASSOC);
603 time_cmd.apply_time = cpu_to_le32(0);
605 time_cmd.max_frags = TE_V2_FRAG_NONE;
606 time_cmd.max_delay = cpu_to_le32(max_delay);
607 /* TODO: why do we need to interval = bi if it is not periodic? */
608 time_cmd.interval = cpu_to_le32(1);
609 time_cmd.duration = cpu_to_le32(duration);
611 time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
612 TE_V2_NOTIF_HOST_EVENT_END |
613 TE_V2_START_IMMEDIATELY);
615 if (!wait_for_notif) {
616 iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
621 * Create notification_wait for the TIME_EVENT_NOTIFICATION to use
622 * right after we send the time event
624 iwl_init_notification_wait(&mvm->notif_wait, &wait_te_notif,
626 ARRAY_SIZE(te_notif_response),
627 iwl_mvm_te_notif, te_data);
629 /* If TE was sent OK - wait for the notification that started */
630 if (iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd)) {
631 IWL_ERR(mvm, "Failed to add TE to protect session\n");
632 iwl_remove_notification(&mvm->notif_wait, &wait_te_notif);
633 } else if (iwl_wait_notification(&mvm->notif_wait, &wait_te_notif,
634 TU_TO_JIFFIES(max_delay))) {
635 IWL_ERR(mvm, "Failed to protect session until TE\n");
639 static void iwl_mvm_cancel_session_protection(struct iwl_mvm *mvm,
640 struct iwl_mvm_vif *mvmvif,
643 struct iwl_mvm_session_prot_cmd cmd = {
645 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
647 .action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
648 .conf_id = cpu_to_le32(id),
652 ret = iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(SESSION_PROTECTION_CMD,
654 0, sizeof(cmd), &cmd);
657 "Couldn't send the SESSION_PROTECTION_CMD: %d\n", ret);
660 static bool __iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
661 struct iwl_mvm_time_event_data *te_data,
665 struct iwl_mvm_vif *mvmvif;
666 enum nl80211_iftype iftype;
671 mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
672 iftype = te_data->vif->type;
675 * It is possible that by the time we got to this point the time
676 * event was already removed.
678 spin_lock_bh(&mvm->time_event_lock);
680 /* Save time event uid before clearing its data */
685 * The clear_data function handles time events that were already removed
687 iwl_mvm_te_clear_data(mvm, te_data);
688 spin_unlock_bh(&mvm->time_event_lock);
690 /* When session protection is supported, the te_data->id field
691 * is reused to save session protection's configuration.
693 if (fw_has_capa(&mvm->fw->ucode_capa,
694 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
695 if (mvmvif && id < SESSION_PROTECT_CONF_MAX_ID) {
696 /* Session protection is still ongoing. Cancel it */
697 iwl_mvm_cancel_session_protection(mvm, mvmvif, id);
698 if (iftype == NL80211_IFTYPE_P2P_DEVICE) {
699 iwl_mvm_p2p_roc_finished(mvm);
704 /* It is possible that by the time we try to remove it, the
705 * time event has already ended and removed. In such a case
706 * there is no need to send a removal command.
709 IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", *uid);
718 * Explicit request to remove a aux roc time event. The removal of a time
719 * event needs to be synchronized with the flow of a time event's end
720 * notification, which also removes the time event from the op mode
723 static void iwl_mvm_remove_aux_roc_te(struct iwl_mvm *mvm,
724 struct iwl_mvm_vif *mvmvif,
725 struct iwl_mvm_time_event_data *te_data)
727 struct iwl_hs20_roc_req aux_cmd = {};
728 u16 len = sizeof(aux_cmd) - iwl_mvm_chan_info_padding(mvm);
733 if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
736 aux_cmd.event_unique_id = cpu_to_le32(uid);
737 aux_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
738 aux_cmd.id_and_color =
739 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
740 IWL_DEBUG_TE(mvm, "Removing BSS AUX ROC TE 0x%x\n",
741 le32_to_cpu(aux_cmd.event_unique_id));
742 ret = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0,
750 * Explicit request to remove a time event. The removal of a time event needs to
751 * be synchronized with the flow of a time event's end notification, which also
752 * removes the time event from the op mode data structures.
754 void iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
755 struct iwl_mvm_vif *mvmvif,
756 struct iwl_mvm_time_event_data *te_data)
758 struct iwl_time_event_cmd time_cmd = {};
762 if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
765 /* When we remove a TE, the UID is to be set in the id field */
766 time_cmd.id = cpu_to_le32(uid);
767 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
768 time_cmd.id_and_color =
769 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
771 IWL_DEBUG_TE(mvm, "Removing TE 0x%x\n", le32_to_cpu(time_cmd.id));
772 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
773 sizeof(time_cmd), &time_cmd);
775 IWL_ERR(mvm, "Couldn't remove the time event\n");
778 void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,
779 struct ieee80211_vif *vif)
781 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
782 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
785 lockdep_assert_held(&mvm->mutex);
787 spin_lock_bh(&mvm->time_event_lock);
789 spin_unlock_bh(&mvm->time_event_lock);
791 if (fw_has_capa(&mvm->fw->ucode_capa,
792 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
793 if (id != SESSION_PROTECT_CONF_ASSOC) {
795 "don't remove session protection id=%u\n",
799 } else if (id != TE_BSS_STA_AGGRESSIVE_ASSOC) {
801 "don't remove TE with id=%u (not session protection)\n",
806 iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
809 void iwl_mvm_rx_session_protect_notif(struct iwl_mvm *mvm,
810 struct iwl_rx_cmd_buffer *rxb)
812 struct iwl_rx_packet *pkt = rxb_addr(rxb);
813 struct iwl_mvm_session_prot_notif *notif = (void *)pkt->data;
814 struct ieee80211_vif *vif;
815 struct iwl_mvm_vif *mvmvif;
818 vif = iwl_mvm_rcu_dereference_vif_id(mvm, le32_to_cpu(notif->mac_id),
824 mvmvif = iwl_mvm_vif_from_mac80211(vif);
826 /* The vif is not a P2P_DEVICE, maintain its time_event_data */
827 if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
828 struct iwl_mvm_time_event_data *te_data =
829 &mvmvif->time_event_data;
831 if (!le32_to_cpu(notif->status)) {
832 iwl_mvm_te_check_disconnect(mvm, vif,
833 "Session protection failure");
834 spin_lock_bh(&mvm->time_event_lock);
835 iwl_mvm_te_clear_data(mvm, te_data);
836 spin_unlock_bh(&mvm->time_event_lock);
839 if (le32_to_cpu(notif->start)) {
840 spin_lock_bh(&mvm->time_event_lock);
841 te_data->running = le32_to_cpu(notif->start);
842 te_data->end_jiffies =
843 TU_TO_EXP_TIME(te_data->duration);
844 spin_unlock_bh(&mvm->time_event_lock);
847 * By now, we should have finished association
848 * and know the dtim period.
850 iwl_mvm_te_check_disconnect(mvm, vif,
851 !vif->bss_conf.assoc ?
852 "Not associated and the session protection is over already..." :
853 "No beacon heard and the session protection is over already...");
854 spin_lock_bh(&mvm->time_event_lock);
855 iwl_mvm_te_clear_data(mvm, te_data);
856 spin_unlock_bh(&mvm->time_event_lock);
862 if (!le32_to_cpu(notif->status) || !le32_to_cpu(notif->start)) {
863 /* End TE, notify mac80211 */
864 mvmvif->time_event_data.id = SESSION_PROTECT_CONF_MAX_ID;
865 ieee80211_remain_on_channel_expired(mvm->hw);
866 iwl_mvm_p2p_roc_finished(mvm);
867 } else if (le32_to_cpu(notif->start)) {
868 if (WARN_ON(mvmvif->time_event_data.id !=
869 le32_to_cpu(notif->conf_id)))
871 set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
872 ieee80211_ready_on_channel(mvm->hw); /* Start TE */
880 iwl_mvm_start_p2p_roc_session_protection(struct iwl_mvm *mvm,
881 struct ieee80211_vif *vif,
883 enum ieee80211_roc_type type)
885 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
886 struct iwl_mvm_session_prot_cmd cmd = {
888 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
890 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
891 .duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
894 lockdep_assert_held(&mvm->mutex);
896 /* The time_event_data.id field is reused to save session
897 * protection's configuration.
900 case IEEE80211_ROC_TYPE_NORMAL:
901 mvmvif->time_event_data.id =
902 SESSION_PROTECT_CONF_P2P_DEVICE_DISCOV;
904 case IEEE80211_ROC_TYPE_MGMT_TX:
905 mvmvif->time_event_data.id =
906 SESSION_PROTECT_CONF_P2P_GO_NEGOTIATION;
909 WARN_ONCE(1, "Got an invalid ROC type\n");
913 cmd.conf_id = cpu_to_le32(mvmvif->time_event_data.id);
914 return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(SESSION_PROTECTION_CMD,
916 0, sizeof(cmd), &cmd);
919 int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
920 int duration, enum ieee80211_roc_type type)
922 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
923 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
924 struct iwl_time_event_cmd time_cmd = {};
926 lockdep_assert_held(&mvm->mutex);
927 if (te_data->running) {
928 IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");
932 if (fw_has_capa(&mvm->fw->ucode_capa,
933 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
934 return iwl_mvm_start_p2p_roc_session_protection(mvm, vif,
938 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
939 time_cmd.id_and_color =
940 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
943 case IEEE80211_ROC_TYPE_NORMAL:
944 time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);
946 case IEEE80211_ROC_TYPE_MGMT_TX:
947 time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);
950 WARN_ONCE(1, "Got an invalid ROC type\n");
954 time_cmd.apply_time = cpu_to_le32(0);
955 time_cmd.interval = cpu_to_le32(1);
958 * The P2P Device TEs can have lower priority than other events
959 * that are being scheduled by the driver/fw, and thus it might not be
960 * scheduled. To improve the chances of it being scheduled, allow them
961 * to be fragmented, and in addition allow them to be delayed.
963 time_cmd.max_frags = min(MSEC_TO_TU(duration)/50, TE_V2_FRAG_ENDLESS);
964 time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));
965 time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));
967 time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
968 TE_V2_NOTIF_HOST_EVENT_END |
969 TE_V2_START_IMMEDIATELY);
971 return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
974 static struct iwl_mvm_time_event_data *iwl_mvm_get_roc_te(struct iwl_mvm *mvm)
976 struct iwl_mvm_time_event_data *te_data;
978 lockdep_assert_held(&mvm->mutex);
980 spin_lock_bh(&mvm->time_event_lock);
983 * Iterate over the list of time events and find the time event that is
984 * associated with a P2P_DEVICE interface.
985 * This assumes that a P2P_DEVICE interface can have only a single time
986 * event at any given time and this time event coresponds to a ROC
989 list_for_each_entry(te_data, &mvm->time_event_list, list) {
990 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE)
994 /* There can only be at most one AUX ROC time event, we just use the
995 * list to simplify/unify code. Remove it if it exists.
997 te_data = list_first_entry_or_null(&mvm->aux_roc_te_list,
998 struct iwl_mvm_time_event_data,
1001 spin_unlock_bh(&mvm->time_event_lock);
1005 void iwl_mvm_cleanup_roc_te(struct iwl_mvm *mvm)
1007 struct iwl_mvm_time_event_data *te_data;
1010 te_data = iwl_mvm_get_roc_te(mvm);
1012 __iwl_mvm_remove_time_event(mvm, te_data, &uid);
1015 void iwl_mvm_stop_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1017 struct iwl_mvm_vif *mvmvif;
1018 struct iwl_mvm_time_event_data *te_data;
1020 if (fw_has_capa(&mvm->fw->ucode_capa,
1021 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
1022 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1024 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1025 iwl_mvm_cancel_session_protection(mvm, mvmvif,
1026 mvmvif->time_event_data.id);
1027 iwl_mvm_p2p_roc_finished(mvm);
1029 iwl_mvm_remove_aux_roc_te(mvm, mvmvif,
1030 &mvmvif->time_event_data);
1031 iwl_mvm_roc_finished(mvm);
1037 te_data = iwl_mvm_get_roc_te(mvm);
1039 IWL_WARN(mvm, "No remain on channel event\n");
1043 mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
1045 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1046 iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1047 iwl_mvm_p2p_roc_finished(mvm);
1049 iwl_mvm_remove_aux_roc_te(mvm, mvmvif, te_data);
1050 iwl_mvm_roc_finished(mvm);
1054 void iwl_mvm_remove_csa_period(struct iwl_mvm *mvm,
1055 struct ieee80211_vif *vif)
1057 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1058 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1061 lockdep_assert_held(&mvm->mutex);
1063 spin_lock_bh(&mvm->time_event_lock);
1065 spin_unlock_bh(&mvm->time_event_lock);
1067 if (id != TE_CHANNEL_SWITCH_PERIOD)
1070 iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1073 int iwl_mvm_schedule_csa_period(struct iwl_mvm *mvm,
1074 struct ieee80211_vif *vif,
1075 u32 duration, u32 apply_time)
1077 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1078 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1079 struct iwl_time_event_cmd time_cmd = {};
1081 lockdep_assert_held(&mvm->mutex);
1083 if (te_data->running) {
1086 spin_lock_bh(&mvm->time_event_lock);
1088 spin_unlock_bh(&mvm->time_event_lock);
1090 if (id == TE_CHANNEL_SWITCH_PERIOD) {
1091 IWL_DEBUG_TE(mvm, "CS period is already scheduled\n");
1096 * Remove the session protection time event to allow the
1097 * channel switch. If we got here, we just heard a beacon so
1098 * the session protection is not needed anymore anyway.
1100 iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1103 time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
1104 time_cmd.id_and_color =
1105 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
1106 time_cmd.id = cpu_to_le32(TE_CHANNEL_SWITCH_PERIOD);
1107 time_cmd.apply_time = cpu_to_le32(apply_time);
1108 time_cmd.max_frags = TE_V2_FRAG_NONE;
1109 time_cmd.duration = cpu_to_le32(duration);
1110 time_cmd.repeat = 1;
1111 time_cmd.interval = cpu_to_le32(1);
1112 time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
1115 time_cmd.policy |= cpu_to_le16(TE_V2_START_IMMEDIATELY);
1117 return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
1120 static bool iwl_mvm_session_prot_notif(struct iwl_notif_wait_data *notif_wait,
1121 struct iwl_rx_packet *pkt, void *data)
1123 struct iwl_mvm *mvm =
1124 container_of(notif_wait, struct iwl_mvm, notif_wait);
1125 struct iwl_mvm_session_prot_notif *resp;
1126 int resp_len = iwl_rx_packet_payload_len(pkt);
1128 if (WARN_ON(pkt->hdr.cmd != SESSION_PROTECTION_NOTIF ||
1129 pkt->hdr.group_id != MAC_CONF_GROUP))
1132 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
1133 IWL_ERR(mvm, "Invalid SESSION_PROTECTION_NOTIF response\n");
1137 resp = (void *)pkt->data;
1141 "TIME_EVENT_NOTIFICATION received but not executed\n");
1146 void iwl_mvm_schedule_session_protection(struct iwl_mvm *mvm,
1147 struct ieee80211_vif *vif,
1148 u32 duration, u32 min_duration,
1149 bool wait_for_notif)
1151 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1152 struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1153 const u16 notif[] = { iwl_cmd_id(SESSION_PROTECTION_NOTIF,
1154 MAC_CONF_GROUP, 0) };
1155 struct iwl_notification_wait wait_notif;
1156 struct iwl_mvm_session_prot_cmd cmd = {
1158 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1160 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
1161 .duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
1164 /* The time_event_data.id field is reused to save session
1165 * protection's configuration.
1167 mvmvif->time_event_data.id = SESSION_PROTECT_CONF_ASSOC;
1168 cmd.conf_id = cpu_to_le32(mvmvif->time_event_data.id);
1170 lockdep_assert_held(&mvm->mutex);
1172 spin_lock_bh(&mvm->time_event_lock);
1173 if (te_data->running &&
1174 time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
1175 IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
1176 jiffies_to_msecs(te_data->end_jiffies - jiffies));
1177 spin_unlock_bh(&mvm->time_event_lock);
1182 iwl_mvm_te_clear_data(mvm, te_data);
1183 te_data->duration = le32_to_cpu(cmd.duration_tu);
1185 spin_unlock_bh(&mvm->time_event_lock);
1187 IWL_DEBUG_TE(mvm, "Add new session protection, duration %d TU\n",
1188 le32_to_cpu(cmd.duration_tu));
1190 if (!wait_for_notif) {
1191 if (iwl_mvm_send_cmd_pdu(mvm,
1192 iwl_cmd_id(SESSION_PROTECTION_CMD,
1194 0, sizeof(cmd), &cmd)) {
1196 "Couldn't send the SESSION_PROTECTION_CMD\n");
1197 spin_lock_bh(&mvm->time_event_lock);
1198 iwl_mvm_te_clear_data(mvm, te_data);
1199 spin_unlock_bh(&mvm->time_event_lock);
1205 iwl_init_notification_wait(&mvm->notif_wait, &wait_notif,
1206 notif, ARRAY_SIZE(notif),
1207 iwl_mvm_session_prot_notif, NULL);
1209 if (iwl_mvm_send_cmd_pdu(mvm,
1210 iwl_cmd_id(SESSION_PROTECTION_CMD,
1212 0, sizeof(cmd), &cmd)) {
1214 "Couldn't send the SESSION_PROTECTION_CMD\n");
1215 iwl_remove_notification(&mvm->notif_wait, &wait_notif);
1216 } else if (iwl_wait_notification(&mvm->notif_wait, &wait_notif,
1217 TU_TO_JIFFIES(100))) {
1219 "Failed to protect session until session protection\n");