Path: blob/main/sys/contrib/dev/iwlwifi/mvm/time-event.c
48287 views
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause1/*2* Copyright (C) 2012-2014, 2018-2025 Intel Corporation3* Copyright (C) 2013-2015 Intel Mobile Communications GmbH4* Copyright (C) 2017 Intel Deutschland GmbH5*/6#include <linux/jiffies.h>7#include <net/mac80211.h>89#include "fw/notif-wait.h"10#include "iwl-trans.h"11#include "fw-api.h"12#include "time-event.h"13#include "mvm.h"14#include "iwl-io.h"15#include "iwl-prph.h"1617/*18* For the high priority TE use a time event type that has similar priority to19* the FW's action scan priority.20*/21#define IWL_MVM_ROC_TE_TYPE_NORMAL TE_P2P_DEVICE_DISCOVERABLE22#define IWL_MVM_ROC_TE_TYPE_MGMT_TX TE_P2P_CLIENT_ASSOC2324void iwl_mvm_te_clear_data(struct iwl_mvm *mvm,25struct iwl_mvm_time_event_data *te_data)26{27lockdep_assert_held(&mvm->time_event_lock);2829if (!te_data || !te_data->vif)30return;3132list_del(&te_data->list);3334/*35* the list is only used for AUX ROC events so make sure it is always36* initialized37*/38INIT_LIST_HEAD(&te_data->list);3940te_data->running = false;41te_data->uid = 0;42te_data->id = TE_MAX;43te_data->vif = NULL;44te_data->link_id = -1;45}4647static void iwl_mvm_cleanup_roc(struct iwl_mvm *mvm)48{49struct ieee80211_vif *bss_vif = iwl_mvm_get_bss_vif(mvm);50struct ieee80211_vif *vif = mvm->p2p_device_vif;5152lockdep_assert_held(&mvm->mutex);5354/*55* Clear the ROC_P2P_RUNNING status bit.56* This will cause the TX path to drop offchannel transmissions.57* That would also be done by mac80211, but it is racy, in particular58* in the case that the time event actually completed in the firmware.59*60* Also flush the offchannel queue -- this is called when the time61* event finishes or is canceled, so that frames queued for it62* won't get stuck on the queue and be transmitted in the next63* time event.64*/65if (test_and_clear_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status)) {66struct iwl_mvm_vif *mvmvif;6768synchronize_net();6970/*71* NB: access to this pointer would be racy, but the flush bit72* can only be set when we had a P2P-Device VIF, and we have a73* flush of this work in iwl_mvm_prepare_mac_removal() so it's74* not really racy.75*/7677if (!WARN_ON(!vif)) {78mvmvif = iwl_mvm_vif_from_mac80211(vif);79iwl_mvm_flush_sta(mvm, mvmvif->deflink.bcast_sta.sta_id,80mvmvif->deflink.bcast_sta.tfd_queue_msk);8182if (mvm->mld_api_is_used) {83iwl_mvm_mld_rm_bcast_sta(mvm, vif,84&vif->bss_conf);8586iwl_mvm_link_changed(mvm, vif, &vif->bss_conf,87LINK_CONTEXT_MODIFY_ACTIVE,88false);89} else {90iwl_mvm_rm_p2p_bcast_sta(mvm, vif);91iwl_mvm_binding_remove_vif(mvm, vif);92}9394/* Do not remove the PHY context as removing and adding95* a PHY context has timing overheads. Leaving it96* configured in FW would be useful in case the next ROC97* is with the same channel.98*/99}100}101102/*103* P2P AUX ROC and HS2.0 ROC do not run simultaneously.104* Clear the ROC_AUX_RUNNING status bit.105* This will cause the TX path to drop offchannel transmissions.106* That would also be done by mac80211, but it is racy, in particular107* in the case that the time event actually completed in the firmware108* (which is handled in iwl_mvm_te_handle_notif).109*/110if (test_and_clear_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status)) {111synchronize_net();112113iwl_mvm_flush_sta(mvm, mvm->aux_sta.sta_id,114mvm->aux_sta.tfd_queue_msk);115116/* In newer version of this command an aux station is added only117* in cases of dedicated tx queue and need to be removed in end118* of use. For the even newer mld api, use the appropriate119* function.120*/121if (mvm->mld_api_is_used)122iwl_mvm_mld_rm_aux_sta(mvm);123else if (iwl_mvm_has_new_station_api(mvm->fw))124iwl_mvm_rm_aux_sta(mvm);125}126127if (!IS_ERR_OR_NULL(bss_vif))128iwl_mvm_unblock_esr(mvm, bss_vif, IWL_MVM_ESR_BLOCKED_ROC);129mutex_unlock(&mvm->mutex);130}131132void iwl_mvm_roc_done_wk(struct work_struct *wk)133{134struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, roc_done_wk);135136mutex_lock(&mvm->mutex);137/* Mutex is released inside */138iwl_mvm_cleanup_roc(mvm);139}140141static void iwl_mvm_roc_finished(struct iwl_mvm *mvm)142{143/*144* Of course, our status bit is just as racy as mac80211, so in145* addition, fire off the work struct which will drop all frames146* from the hardware queues that made it through the race. First147* it will of course synchronize the TX path to make sure that148* any *new* TX will be rejected.149*/150schedule_work(&mvm->roc_done_wk);151}152153static void iwl_mvm_csa_noa_start(struct iwl_mvm *mvm)154{155struct ieee80211_vif *csa_vif;156157rcu_read_lock();158159csa_vif = rcu_dereference(mvm->csa_vif);160if (!csa_vif || !csa_vif->bss_conf.csa_active)161goto out_unlock;162163IWL_DEBUG_TE(mvm, "CSA NOA started\n");164165/*166* CSA NoA is started but we still have beacons to167* transmit on the current channel.168* So we just do nothing here and the switch169* will be performed on the last TBTT.170*/171if (!ieee80211_beacon_cntdwn_is_complete(csa_vif, 0)) {172IWL_WARN(mvm, "CSA NOA started too early\n");173goto out_unlock;174}175176ieee80211_csa_finish(csa_vif, 0);177178rcu_read_unlock();179180RCU_INIT_POINTER(mvm->csa_vif, NULL);181182return;183184out_unlock:185rcu_read_unlock();186}187188static bool iwl_mvm_te_check_disconnect(struct iwl_mvm *mvm,189struct ieee80211_vif *vif,190const char *errmsg)191{192struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);193194if (vif->type != NL80211_IFTYPE_STATION)195return false;196197if (!mvmvif->csa_bcn_pending && vif->cfg.assoc &&198vif->bss_conf.dtim_period)199return false;200if (errmsg)201IWL_ERR(mvm, "%s\n", errmsg);202203if (mvmvif->csa_bcn_pending) {204struct iwl_mvm_sta *mvmsta;205206rcu_read_lock();207mvmsta = iwl_mvm_sta_from_staid_rcu(mvm,208mvmvif->deflink.ap_sta_id);209if (!WARN_ON(!mvmsta))210iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);211rcu_read_unlock();212}213214if (vif->cfg.assoc) {215/*216* When not associated, this will be called from217* iwl_mvm_event_mlme_callback_ini()218*/219iwl_dbg_tlv_time_point(&mvm->fwrt,220IWL_FW_INI_TIME_POINT_ASSOC_FAILED,221NULL);222223mvmvif->session_prot_connection_loss = true;224}225226iwl_mvm_connection_loss(mvm, vif, errmsg);227return true;228}229230static void231iwl_mvm_te_handle_notify_csa(struct iwl_mvm *mvm,232struct iwl_mvm_time_event_data *te_data,233struct iwl_time_event_notif *notif)234{235struct ieee80211_vif *vif = te_data->vif;236struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);237238if (!notif->status)239IWL_DEBUG_TE(mvm, "CSA time event failed to start\n");240241switch (te_data->vif->type) {242case NL80211_IFTYPE_AP:243if (!notif->status)244mvmvif->csa_failed = true;245iwl_mvm_csa_noa_start(mvm);246break;247case NL80211_IFTYPE_STATION:248if (!notif->status) {249iwl_mvm_connection_loss(mvm, vif,250"CSA TE failed to start");251break;252}253iwl_mvm_csa_client_absent(mvm, te_data->vif);254cancel_delayed_work(&mvmvif->csa_work);255ieee80211_chswitch_done(te_data->vif, true, 0);256break;257default:258/* should never happen */259WARN_ON_ONCE(1);260break;261}262263/* we don't need it anymore */264iwl_mvm_te_clear_data(mvm, te_data);265}266267static void iwl_mvm_te_check_trigger(struct iwl_mvm *mvm,268struct iwl_time_event_notif *notif,269struct iwl_mvm_time_event_data *te_data)270{271struct iwl_fw_dbg_trigger_tlv *trig;272struct iwl_fw_dbg_trigger_time_event *te_trig;273int i;274275trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,276ieee80211_vif_to_wdev(te_data->vif),277FW_DBG_TRIGGER_TIME_EVENT);278if (!trig)279return;280281te_trig = (void *)trig->data;282283for (i = 0; i < ARRAY_SIZE(te_trig->time_events); i++) {284u32 trig_te_id = le32_to_cpu(te_trig->time_events[i].id);285u32 trig_action_bitmap =286le32_to_cpu(te_trig->time_events[i].action_bitmap);287u32 trig_status_bitmap =288le32_to_cpu(te_trig->time_events[i].status_bitmap);289290if (trig_te_id != te_data->id ||291!(trig_action_bitmap & le32_to_cpu(notif->action)) ||292!(trig_status_bitmap & BIT(le32_to_cpu(notif->status))))293continue;294295iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,296"Time event %d Action 0x%x received status: %d",297te_data->id,298le32_to_cpu(notif->action),299le32_to_cpu(notif->status));300break;301}302}303304/*305* Handles a FW notification for an event that is known to the driver.306*307* @mvm: the mvm component308* @te_data: the time event data309* @notif: the notification data corresponding the time event data.310*/311static void iwl_mvm_te_handle_notif(struct iwl_mvm *mvm,312struct iwl_mvm_time_event_data *te_data,313struct iwl_time_event_notif *notif)314{315lockdep_assert_held(&mvm->time_event_lock);316317IWL_DEBUG_TE(mvm, "Handle time event notif - UID = 0x%x action %d\n",318le32_to_cpu(notif->unique_id),319le32_to_cpu(notif->action));320321iwl_mvm_te_check_trigger(mvm, notif, te_data);322323/*324* The FW sends the start/end time event notifications even for events325* that it fails to schedule. This is indicated in the status field of326* the notification. This happens in cases that the scheduler cannot327* find a schedule that can handle the event (for example requesting a328* P2P Device discoveribility, while there are other higher priority329* events in the system).330*/331if (!le32_to_cpu(notif->status)) {332const char *msg;333334if (notif->action & cpu_to_le32(TE_V2_NOTIF_HOST_EVENT_START))335msg = "Time Event start notification failure";336else337msg = "Time Event end notification failure";338339IWL_DEBUG_TE(mvm, "%s\n", msg);340341if (iwl_mvm_te_check_disconnect(mvm, te_data->vif, msg)) {342iwl_mvm_te_clear_data(mvm, te_data);343return;344}345}346347if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_END) {348IWL_DEBUG_TE(mvm,349"TE ended - current time %lu, estimated end %lu\n",350jiffies, te_data->end_jiffies);351352switch (te_data->vif->type) {353case NL80211_IFTYPE_P2P_DEVICE:354ieee80211_remain_on_channel_expired(mvm->hw);355iwl_mvm_roc_finished(mvm);356break;357case NL80211_IFTYPE_STATION:358/*359* If we are switching channel, don't disconnect360* if the time event is already done. Beacons can361* be delayed a bit after the switch.362*/363if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {364IWL_DEBUG_TE(mvm,365"No beacon heard and the CS time event is over, don't disconnect\n");366break;367}368369/*370* By now, we should have finished association371* and know the dtim period.372*/373iwl_mvm_te_check_disconnect(mvm, te_data->vif,374!te_data->vif->cfg.assoc ?375"Not associated and the time event is over already..." :376"No beacon heard and the time event is over already...");377break;378default:379break;380}381382iwl_mvm_te_clear_data(mvm, te_data);383} else if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_START) {384te_data->running = true;385te_data->end_jiffies = TU_TO_EXP_TIME(te_data->duration);386387if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {388set_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status);389ieee80211_ready_on_channel(mvm->hw);390} else if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {391iwl_mvm_te_handle_notify_csa(mvm, te_data, notif);392}393} else {394IWL_WARN(mvm, "Got TE with unknown action\n");395}396}397398struct iwl_mvm_rx_roc_iterator_data {399u32 activity;400bool end_activity;401bool found;402};403404static void iwl_mvm_rx_roc_iterator(void *_data, u8 *mac,405struct ieee80211_vif *vif)406{407struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);408struct iwl_mvm_rx_roc_iterator_data *data = _data;409410if (mvmvif->roc_activity == data->activity) {411data->found = true;412if (data->end_activity)413mvmvif->roc_activity = ROC_NUM_ACTIVITIES;414}415}416417void iwl_mvm_rx_roc_notif(struct iwl_mvm *mvm,418struct iwl_rx_cmd_buffer *rxb)419{420struct iwl_rx_packet *pkt = rxb_addr(rxb);421struct iwl_roc_notif *notif = (void *)pkt->data;422u32 activity = le32_to_cpu(notif->activity);423bool started = le32_to_cpu(notif->success) &&424le32_to_cpu(notif->started);425struct iwl_mvm_rx_roc_iterator_data data = {426.activity = activity,427.end_activity = !started,428};429430/* Clear vif roc_activity if done (set to ROC_NUM_ACTIVITIES) */431ieee80211_iterate_active_interfaces_atomic(mvm->hw,432IEEE80211_IFACE_ITER_NORMAL,433iwl_mvm_rx_roc_iterator,434&data);435/*436* It is possible that the ROC was canceled437* but the notification was already fired.438*/439if (!data.found)440return;441442if (started) {443set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);444ieee80211_ready_on_channel(mvm->hw);445} else {446iwl_mvm_roc_finished(mvm);447ieee80211_remain_on_channel_expired(mvm->hw);448}449}450451/*452* Handle A Aux ROC time event453*/454static int iwl_mvm_aux_roc_te_handle_notif(struct iwl_mvm *mvm,455struct iwl_time_event_notif *notif)456{457struct iwl_mvm_time_event_data *aux_roc_te = NULL, *te_data;458459list_for_each_entry(te_data, &mvm->aux_roc_te_list, list) {460if (le32_to_cpu(notif->unique_id) == te_data->uid) {461aux_roc_te = te_data;462break;463}464}465if (!aux_roc_te) /* Not a Aux ROC time event */466return -EINVAL;467468iwl_mvm_te_check_trigger(mvm, notif, te_data);469470IWL_DEBUG_TE(mvm,471"Aux ROC time event notification - UID = 0x%x action %d (error = %d)\n",472le32_to_cpu(notif->unique_id),473le32_to_cpu(notif->action), le32_to_cpu(notif->status));474475if (!le32_to_cpu(notif->status) ||476le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_END) {477/* End TE, notify mac80211 */478ieee80211_remain_on_channel_expired(mvm->hw);479iwl_mvm_roc_finished(mvm); /* flush aux queue */480list_del(&te_data->list); /* remove from list */481te_data->running = false;482te_data->vif = NULL;483te_data->uid = 0;484te_data->id = TE_MAX;485} else if (le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_START) {486set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);487te_data->running = true;488ieee80211_ready_on_channel(mvm->hw); /* Start TE */489} else {490IWL_DEBUG_TE(mvm,491"ERROR: Unknown Aux ROC Time Event (action = %d)\n",492le32_to_cpu(notif->action));493return -EINVAL;494}495496return 0;497}498499/*500* The Rx handler for time event notifications501*/502void iwl_mvm_rx_time_event_notif(struct iwl_mvm *mvm,503struct iwl_rx_cmd_buffer *rxb)504{505struct iwl_rx_packet *pkt = rxb_addr(rxb);506struct iwl_time_event_notif *notif = (void *)pkt->data;507struct iwl_mvm_time_event_data *te_data, *tmp;508509IWL_DEBUG_TE(mvm, "Time event notification - UID = 0x%x action %d\n",510le32_to_cpu(notif->unique_id),511le32_to_cpu(notif->action));512513spin_lock_bh(&mvm->time_event_lock);514/* This time event is triggered for Aux ROC request */515if (!iwl_mvm_aux_roc_te_handle_notif(mvm, notif))516goto unlock;517518list_for_each_entry_safe(te_data, tmp, &mvm->time_event_list, list) {519if (le32_to_cpu(notif->unique_id) == te_data->uid)520iwl_mvm_te_handle_notif(mvm, te_data, notif);521}522unlock:523spin_unlock_bh(&mvm->time_event_lock);524}525526static bool iwl_mvm_te_notif(struct iwl_notif_wait_data *notif_wait,527struct iwl_rx_packet *pkt, void *data)528{529struct iwl_mvm *mvm =530container_of(notif_wait, struct iwl_mvm, notif_wait);531struct iwl_mvm_time_event_data *te_data = data;532struct iwl_time_event_notif *resp;533int resp_len = iwl_rx_packet_payload_len(pkt);534535if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_NOTIFICATION))536return true;537538if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {539IWL_ERR(mvm, "Invalid TIME_EVENT_NOTIFICATION response\n");540return true;541}542543resp = (void *)pkt->data;544545/* te_data->uid is already set in the TIME_EVENT_CMD response */546if (le32_to_cpu(resp->unique_id) != te_data->uid)547return false;548549IWL_DEBUG_TE(mvm, "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n",550te_data->uid);551if (!resp->status)552IWL_ERR(mvm,553"TIME_EVENT_NOTIFICATION received but not executed\n");554555return true;556}557558static bool iwl_mvm_time_event_response(struct iwl_notif_wait_data *notif_wait,559struct iwl_rx_packet *pkt, void *data)560{561struct iwl_mvm *mvm =562container_of(notif_wait, struct iwl_mvm, notif_wait);563struct iwl_mvm_time_event_data *te_data = data;564struct iwl_time_event_resp *resp;565int resp_len = iwl_rx_packet_payload_len(pkt);566567if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_CMD))568return true;569570if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {571IWL_ERR(mvm, "Invalid TIME_EVENT_CMD response\n");572return true;573}574575resp = (void *)pkt->data;576577/* we should never get a response to another TIME_EVENT_CMD here */578if (WARN_ON_ONCE(le32_to_cpu(resp->id) != te_data->id))579return false;580581te_data->uid = le32_to_cpu(resp->unique_id);582IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",583te_data->uid);584return true;585}586587static int iwl_mvm_time_event_send_add(struct iwl_mvm *mvm,588struct ieee80211_vif *vif,589struct iwl_mvm_time_event_data *te_data,590struct iwl_time_event_cmd *te_cmd)591{592static const u16 time_event_response[] = { TIME_EVENT_CMD };593struct iwl_notification_wait wait_time_event;594int ret;595596lockdep_assert_held(&mvm->mutex);597598IWL_DEBUG_TE(mvm, "Add new TE, duration %d TU\n",599le32_to_cpu(te_cmd->duration));600601spin_lock_bh(&mvm->time_event_lock);602if (WARN_ON(te_data->id != TE_MAX)) {603spin_unlock_bh(&mvm->time_event_lock);604return -EIO;605}606te_data->vif = vif;607te_data->duration = le32_to_cpu(te_cmd->duration);608te_data->id = le32_to_cpu(te_cmd->id);609list_add_tail(&te_data->list, &mvm->time_event_list);610spin_unlock_bh(&mvm->time_event_lock);611612/*613* Use a notification wait, which really just processes the614* command response and doesn't wait for anything, in order615* to be able to process the response and get the UID inside616* the RX path. Using CMD_WANT_SKB doesn't work because it617* stores the buffer and then wakes up this thread, by which618* time another notification (that the time event started)619* might already be processed unsuccessfully.620*/621iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,622time_event_response,623ARRAY_SIZE(time_event_response),624iwl_mvm_time_event_response, te_data);625626ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,627sizeof(*te_cmd), te_cmd);628if (ret) {629IWL_ERR(mvm, "Couldn't send TIME_EVENT_CMD: %d\n", ret);630iwl_remove_notification(&mvm->notif_wait, &wait_time_event);631goto out_clear_te;632}633634/* No need to wait for anything, so just pass 1 (0 isn't valid) */635ret = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);636/* should never fail */637WARN_ON_ONCE(ret);638639if (ret) {640out_clear_te:641spin_lock_bh(&mvm->time_event_lock);642iwl_mvm_te_clear_data(mvm, te_data);643spin_unlock_bh(&mvm->time_event_lock);644}645return ret;646}647648void iwl_mvm_protect_session(struct iwl_mvm *mvm,649struct ieee80211_vif *vif,650u32 duration, u32 min_duration,651u32 max_delay, bool wait_for_notif)652{653struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);654struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;655const u16 te_notif_response[] = { TIME_EVENT_NOTIFICATION };656struct iwl_notification_wait wait_te_notif;657struct iwl_time_event_cmd time_cmd = {};658659lockdep_assert_held(&mvm->mutex);660661if (te_data->running &&662time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {663IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",664jiffies_to_msecs(te_data->end_jiffies - jiffies));665return;666}667668if (te_data->running) {669IWL_DEBUG_TE(mvm, "extend 0x%x: only %u ms left\n",670te_data->uid,671jiffies_to_msecs(te_data->end_jiffies - jiffies));672/*673* we don't have enough time674* cancel the current TE and issue a new one675* Of course it would be better to remove the old one only676* when the new one is added, but we don't care if we are off677* channel for a bit. All we need to do, is not to return678* before we actually begin to be on the channel.679*/680iwl_mvm_stop_session_protection(mvm, vif);681}682683time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);684time_cmd.id_and_color =685cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));686time_cmd.id = cpu_to_le32(TE_BSS_STA_AGGRESSIVE_ASSOC);687688time_cmd.apply_time = cpu_to_le32(0);689690time_cmd.max_frags = TE_V2_FRAG_NONE;691time_cmd.max_delay = cpu_to_le32(max_delay);692/* TODO: why do we need to interval = bi if it is not periodic? */693time_cmd.interval = cpu_to_le32(1);694time_cmd.duration = cpu_to_le32(duration);695time_cmd.repeat = 1;696time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |697TE_V2_NOTIF_HOST_EVENT_END |698TE_V2_START_IMMEDIATELY);699700if (!wait_for_notif) {701iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);702return;703}704705/*706* Create notification_wait for the TIME_EVENT_NOTIFICATION to use707* right after we send the time event708*/709iwl_init_notification_wait(&mvm->notif_wait, &wait_te_notif,710te_notif_response,711ARRAY_SIZE(te_notif_response),712iwl_mvm_te_notif, te_data);713714/* If TE was sent OK - wait for the notification that started */715if (iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd)) {716IWL_ERR(mvm, "Failed to add TE to protect session\n");717iwl_remove_notification(&mvm->notif_wait, &wait_te_notif);718} else if (iwl_wait_notification(&mvm->notif_wait, &wait_te_notif,719TU_TO_JIFFIES(max_delay))) {720IWL_ERR(mvm, "Failed to protect session until TE\n");721}722}723724/* Determine whether mac or link id should be used, and validate the link id */725static int iwl_mvm_get_session_prot_id(struct iwl_mvm *mvm,726struct ieee80211_vif *vif,727s8 link_id)728{729struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);730int ver = iwl_fw_lookup_cmd_ver(mvm->fw,731WIDE_ID(MAC_CONF_GROUP,732SESSION_PROTECTION_CMD), 1);733734if (ver < 2)735return mvmvif->id;736737if (WARN(link_id < 0 || !mvmvif->link[link_id],738"Invalid link ID for session protection: %u\n", link_id))739return -EINVAL;740741if (WARN(!mvmvif->link[link_id]->active,742"Session Protection on an inactive link: %u\n", link_id))743return -EINVAL;744745return mvmvif->link[link_id]->fw_link_id;746}747748static void iwl_mvm_cancel_session_protection(struct iwl_mvm *mvm,749struct ieee80211_vif *vif,750u32 id, s8 link_id)751{752int mac_link_id = iwl_mvm_get_session_prot_id(mvm, vif, link_id);753struct iwl_session_prot_cmd cmd = {754.id_and_color = cpu_to_le32(mac_link_id),755.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),756.conf_id = cpu_to_le32(id),757};758int ret;759760if (mac_link_id < 0)761return;762763ret = iwl_mvm_send_cmd_pdu(mvm,764WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),7650, sizeof(cmd), &cmd);766if (ret)767IWL_ERR(mvm,768"Couldn't send the SESSION_PROTECTION_CMD: %d\n", ret);769}770771static void iwl_mvm_roc_rm_cmd(struct iwl_mvm *mvm, u32 activity)772{773struct iwl_roc_req roc_cmd = {774.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),775.activity = cpu_to_le32(activity),776};777u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0);778u16 cmd_len = ver < 6 ? sizeof(struct iwl_roc_req_v5) : sizeof(roc_cmd);779int ret;780781lockdep_assert_held(&mvm->mutex);782ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0,783cmd_len, &roc_cmd);784if (ret)785IWL_ERR(mvm, "Couldn't send the ROC_CMD: %d\n", ret);786}787788static bool __iwl_mvm_remove_time_event(struct iwl_mvm *mvm,789struct iwl_mvm_time_event_data *te_data,790u32 *uid)791{792u32 id;793struct ieee80211_vif *vif = te_data->vif;794struct iwl_mvm_vif *mvmvif;795enum nl80211_iftype iftype;796s8 link_id;797bool p2p_aux = iwl_mvm_has_p2p_over_aux(mvm);798u8 roc_ver = iwl_fw_lookup_cmd_ver(mvm->fw,799WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0);800801if (!vif)802return false;803804mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);805iftype = te_data->vif->type;806807/*808* It is possible that by the time we got to this point the time809* event was already removed.810*/811spin_lock_bh(&mvm->time_event_lock);812813/* Save time event uid before clearing its data */814*uid = te_data->uid;815id = te_data->id;816link_id = te_data->link_id;817818/*819* The clear_data function handles time events that were already removed820*/821iwl_mvm_te_clear_data(mvm, te_data);822spin_unlock_bh(&mvm->time_event_lock);823824if ((p2p_aux && iftype == NL80211_IFTYPE_P2P_DEVICE) ||825(roc_ver >= 3 && mvmvif->roc_activity == ROC_ACTIVITY_HOTSPOT)) {826if (mvmvif->roc_activity < ROC_NUM_ACTIVITIES) {827iwl_mvm_roc_rm_cmd(mvm, mvmvif->roc_activity);828mvmvif->roc_activity = ROC_NUM_ACTIVITIES;829iwl_mvm_roc_finished(mvm);830}831return false;832} else if (fw_has_capa(&mvm->fw->ucode_capa,833IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD) &&834id != HOT_SPOT_CMD) {835/* When session protection is used, the te_data->id field836* is reused to save session protection's configuration.837* For AUX ROC, HOT_SPOT_CMD is used and the te_data->id838* field is set to HOT_SPOT_CMD.839*/840if (mvmvif && id < SESSION_PROTECT_CONF_MAX_ID) {841/* Session protection is still ongoing. Cancel it */842iwl_mvm_cancel_session_protection(mvm, vif, id,843link_id);844if (iftype == NL80211_IFTYPE_P2P_DEVICE) {845iwl_mvm_roc_finished(mvm);846}847}848return false;849} else {850/* It is possible that by the time we try to remove it, the851* time event has already ended and removed. In such a case852* there is no need to send a removal command.853*/854if (id == TE_MAX) {855IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", *uid);856return false;857}858}859860return true;861}862863/*864* Explicit request to remove a aux roc time event. The removal of a time865* event needs to be synchronized with the flow of a time event's end866* notification, which also removes the time event from the op mode867* data structures.868*/869static void iwl_mvm_remove_aux_roc_te(struct iwl_mvm *mvm,870struct iwl_mvm_vif *mvmvif,871struct iwl_mvm_time_event_data *te_data)872{873struct iwl_hs20_roc_req aux_cmd = {};874u16 len = sizeof(aux_cmd) - iwl_mvm_chan_info_padding(mvm);875876u32 uid;877int ret;878879if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))880return;881882aux_cmd.event_unique_id = cpu_to_le32(uid);883aux_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);884aux_cmd.id_and_color =885cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));886IWL_DEBUG_TE(mvm, "Removing BSS AUX ROC TE 0x%x\n",887le32_to_cpu(aux_cmd.event_unique_id));888ret = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0,889len, &aux_cmd);890891if (WARN_ON(ret))892return;893}894895/*896* Explicit request to remove a time event. The removal of a time event needs to897* be synchronized with the flow of a time event's end notification, which also898* removes the time event from the op mode data structures.899*/900void iwl_mvm_remove_time_event(struct iwl_mvm *mvm,901struct iwl_mvm_vif *mvmvif,902struct iwl_mvm_time_event_data *te_data)903{904struct iwl_time_event_cmd time_cmd = {};905u32 uid;906int ret;907908if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))909return;910911/* When we remove a TE, the UID is to be set in the id field */912time_cmd.id = cpu_to_le32(uid);913time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);914time_cmd.id_and_color =915cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));916917IWL_DEBUG_TE(mvm, "Removing TE 0x%x\n", le32_to_cpu(time_cmd.id));918ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,919sizeof(time_cmd), &time_cmd);920if (ret)921IWL_ERR(mvm, "Couldn't remove the time event\n");922}923924void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,925struct ieee80211_vif *vif)926{927struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);928struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;929u32 id;930931lockdep_assert_held(&mvm->mutex);932933spin_lock_bh(&mvm->time_event_lock);934id = te_data->id;935spin_unlock_bh(&mvm->time_event_lock);936937if (fw_has_capa(&mvm->fw->ucode_capa,938IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {939if (id != SESSION_PROTECT_CONF_ASSOC) {940IWL_DEBUG_TE(mvm,941"don't remove session protection id=%u\n",942id);943return;944}945} else if (id != TE_BSS_STA_AGGRESSIVE_ASSOC) {946IWL_DEBUG_TE(mvm,947"don't remove TE with id=%u (not session protection)\n",948id);949return;950}951952iwl_mvm_remove_time_event(mvm, mvmvif, te_data);953}954955void iwl_mvm_rx_session_protect_notif(struct iwl_mvm *mvm,956struct iwl_rx_cmd_buffer *rxb)957{958struct iwl_rx_packet *pkt = rxb_addr(rxb);959struct iwl_session_prot_notif *notif = (void *)pkt->data;960int id = le32_to_cpu(notif->mac_link_id);961struct ieee80211_vif *vif;962struct iwl_mvm_vif *mvmvif;963964rcu_read_lock();965966/* note we use link ID == MAC ID */967vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);968if (!vif)969goto out_unlock;970971mvmvif = iwl_mvm_vif_from_mac80211(vif);972973/* The vif is not a P2P_DEVICE, maintain its time_event_data */974if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {975struct iwl_mvm_time_event_data *te_data =976&mvmvif->time_event_data;977978if (!le32_to_cpu(notif->status)) {979iwl_mvm_te_check_disconnect(mvm, vif,980"Session protection failure");981spin_lock_bh(&mvm->time_event_lock);982iwl_mvm_te_clear_data(mvm, te_data);983spin_unlock_bh(&mvm->time_event_lock);984}985986if (le32_to_cpu(notif->start)) {987spin_lock_bh(&mvm->time_event_lock);988te_data->running = le32_to_cpu(notif->start);989te_data->end_jiffies =990TU_TO_EXP_TIME(te_data->duration);991spin_unlock_bh(&mvm->time_event_lock);992} else {993/*994* By now, we should have finished association995* and know the dtim period.996*/997iwl_mvm_te_check_disconnect(mvm, vif,998!vif->cfg.assoc ?999"Not associated and the session protection is over already..." :1000"No beacon heard and the session protection is over already...");1001spin_lock_bh(&mvm->time_event_lock);1002iwl_mvm_te_clear_data(mvm, te_data);1003spin_unlock_bh(&mvm->time_event_lock);1004}10051006goto out_unlock;1007}10081009if (!le32_to_cpu(notif->status) || !le32_to_cpu(notif->start)) {1010/* End TE, notify mac80211 */1011mvmvif->time_event_data.id = SESSION_PROTECT_CONF_MAX_ID;1012mvmvif->time_event_data.link_id = -1;1013/* set the bit so the ROC cleanup will actually clean up */1014set_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status);1015iwl_mvm_roc_finished(mvm);1016ieee80211_remain_on_channel_expired(mvm->hw);1017} else if (le32_to_cpu(notif->start)) {1018if (WARN_ON(mvmvif->time_event_data.id !=1019le32_to_cpu(notif->conf_id)))1020goto out_unlock;1021set_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status);1022ieee80211_ready_on_channel(mvm->hw); /* Start TE */1023}10241025out_unlock:1026rcu_read_unlock();1027}10281029#define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)1030#define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)1031#define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)1032#define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)1033#define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)10341035void iwl_mvm_roc_duration_and_delay(struct ieee80211_vif *vif,1036u32 duration_ms,1037u32 *duration_tu,1038u32 *delay)1039{1040struct ieee80211_bss_conf *link_conf;1041unsigned int link_id;1042u32 dtim_interval = 0;10431044*delay = AUX_ROC_MIN_DELAY;1045*duration_tu = MSEC_TO_TU(duration_ms);10461047rcu_read_lock();1048for_each_vif_active_link(vif, link_conf, link_id) {1049dtim_interval =1050max_t(u32, dtim_interval,1051link_conf->dtim_period * link_conf->beacon_int);1052}1053rcu_read_unlock();10541055/*1056* If we are associated we want the delay time to be at least one1057* dtim interval so that the FW can wait until after the DTIM and1058* then start the time event, this will potentially allow us to1059* remain off-channel for the max duration.1060* Since we want to use almost a whole dtim interval we would also1061* like the delay to be for 2-3 dtim intervals, in case there are1062* other time events with higher priority.1063* dtim_interval should never be 0, it can be 1 if we don't know it1064* (we haven't heard any beacon yet).1065*/1066if (vif->cfg.assoc && !WARN_ON(!dtim_interval)) {1067*delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);1068/* We cannot remain off-channel longer than the DTIM interval */1069if (dtim_interval <= *duration_tu) {1070*duration_tu = dtim_interval - AUX_ROC_SAFETY_BUFFER;1071if (*duration_tu <= AUX_ROC_MIN_DURATION)1072*duration_tu = dtim_interval -1073AUX_ROC_MIN_SAFETY_BUFFER;1074}1075}1076}10771078int iwl_mvm_roc_add_cmd(struct iwl_mvm *mvm,1079struct ieee80211_channel *channel,1080struct ieee80211_vif *vif,1081int duration, enum iwl_roc_activity activity)1082{1083int res;1084u32 duration_tu, delay;1085struct iwl_roc_req roc_req = {1086.action = cpu_to_le32(FW_CTXT_ACTION_ADD),1087.activity = cpu_to_le32(activity),1088.sta_id = cpu_to_le32(mvm->aux_sta.sta_id),1089};1090u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0);1091u16 cmd_len = ver < 6 ? sizeof(struct iwl_roc_req_v5) : sizeof(roc_req);1092struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);10931094lockdep_assert_held(&mvm->mutex);10951096if (WARN_ON(mvmvif->roc_activity != ROC_NUM_ACTIVITIES))1097return -EBUSY;10981099/* Set the channel info data */1100iwl_mvm_set_chan_info(mvm, &roc_req.channel_info,1101channel->hw_value,1102iwl_mvm_phy_band_from_nl80211(channel->band),1103IWL_PHY_CHANNEL_MODE20, 0);11041105iwl_mvm_roc_duration_and_delay(vif, duration, &duration_tu,1106&delay);1107roc_req.duration = cpu_to_le32(duration_tu);1108roc_req.max_delay = cpu_to_le32(delay);11091110IWL_DEBUG_TE(mvm,1111"\t(requested = %ums, max_delay = %ums)\n",1112duration, delay);1113IWL_DEBUG_TE(mvm,1114"Requesting to remain on channel %u for %utu. activity %u\n",1115channel->hw_value, duration_tu, activity);11161117/* Set the node address */1118memcpy(roc_req.node_addr, vif->addr, ETH_ALEN);11191120res = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, ROC_CMD),11210, cmd_len, &roc_req);1122if (!res)1123mvmvif->roc_activity = activity;11241125return res;1126}11271128static int1129iwl_mvm_start_p2p_roc_session_protection(struct iwl_mvm *mvm,1130struct ieee80211_vif *vif,1131int duration,1132enum ieee80211_roc_type type)1133{1134struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1135struct iwl_session_prot_cmd cmd = {1136.id_and_color =1137cpu_to_le32(iwl_mvm_get_session_prot_id(mvm, vif, 0)),1138.action = cpu_to_le32(FW_CTXT_ACTION_ADD),1139.duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),1140};11411142lockdep_assert_held(&mvm->mutex);11431144/* The time_event_data.id field is reused to save session1145* protection's configuration.1146*/11471148mvmvif->time_event_data.link_id = 0;11491150switch (type) {1151case IEEE80211_ROC_TYPE_NORMAL:1152mvmvif->time_event_data.id =1153SESSION_PROTECT_CONF_P2P_DEVICE_DISCOV;1154break;1155case IEEE80211_ROC_TYPE_MGMT_TX:1156mvmvif->time_event_data.id =1157SESSION_PROTECT_CONF_P2P_GO_NEGOTIATION;1158break;1159default:1160WARN_ONCE(1, "Got an invalid ROC type\n");1161return -EINVAL;1162}11631164cmd.conf_id = cpu_to_le32(mvmvif->time_event_data.id);1165return iwl_mvm_send_cmd_pdu(mvm,1166WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),11670, sizeof(cmd), &cmd);1168}11691170int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,1171int duration, enum ieee80211_roc_type type)1172{1173struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1174struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;1175struct iwl_time_event_cmd time_cmd = {};11761177lockdep_assert_held(&mvm->mutex);1178if (te_data->running) {1179IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");1180return -EBUSY;1181}11821183if (fw_has_capa(&mvm->fw->ucode_capa,1184IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))1185return iwl_mvm_start_p2p_roc_session_protection(mvm, vif,1186duration,1187type);11881189time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);1190time_cmd.id_and_color =1191cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));11921193switch (type) {1194case IEEE80211_ROC_TYPE_NORMAL:1195time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);1196break;1197case IEEE80211_ROC_TYPE_MGMT_TX:1198time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);1199break;1200default:1201WARN_ONCE(1, "Got an invalid ROC type\n");1202return -EINVAL;1203}12041205time_cmd.apply_time = cpu_to_le32(0);1206time_cmd.interval = cpu_to_le32(1);12071208/*1209* The P2P Device TEs can have lower priority than other events1210* that are being scheduled by the driver/fw, and thus it might not be1211* scheduled. To improve the chances of it being scheduled, allow them1212* to be fragmented, and in addition allow them to be delayed.1213*/1214time_cmd.max_frags = min(MSEC_TO_TU(duration)/50, TE_V2_FRAG_ENDLESS);1215time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));1216time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));1217time_cmd.repeat = 1;1218time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |1219TE_V2_NOTIF_HOST_EVENT_END |1220TE_V2_START_IMMEDIATELY);12211222return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);1223}12241225static struct iwl_mvm_time_event_data *iwl_mvm_get_roc_te(struct iwl_mvm *mvm)1226{1227struct iwl_mvm_time_event_data *te_data;12281229lockdep_assert_held(&mvm->mutex);12301231spin_lock_bh(&mvm->time_event_lock);12321233/*1234* Iterate over the list of time events and find the time event that is1235* associated with a P2P_DEVICE interface.1236* This assumes that a P2P_DEVICE interface can have only a single time1237* event at any given time and this time event coresponds to a ROC1238* request1239*/1240list_for_each_entry(te_data, &mvm->time_event_list, list) {1241if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE)1242goto out;1243}12441245/* There can only be at most one AUX ROC time event, we just use the1246* list to simplify/unify code. Remove it if it exists.1247*/1248te_data = list_first_entry_or_null(&mvm->aux_roc_te_list,1249struct iwl_mvm_time_event_data,1250list);1251out:1252spin_unlock_bh(&mvm->time_event_lock);1253return te_data;1254}12551256void iwl_mvm_cleanup_roc_te(struct iwl_mvm *mvm)1257{1258struct iwl_mvm_time_event_data *te_data;1259u32 uid;12601261te_data = iwl_mvm_get_roc_te(mvm);1262if (te_data)1263__iwl_mvm_remove_time_event(mvm, te_data, &uid);1264}12651266void iwl_mvm_stop_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)1267{1268struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1269struct iwl_mvm_time_event_data *te_data;1270bool p2p_aux = iwl_mvm_has_p2p_over_aux(mvm);1271u8 roc_ver = iwl_fw_lookup_cmd_ver(mvm->fw,1272WIDE_ID(MAC_CONF_GROUP, ROC_CMD), 0);1273int iftype = vif->type;12741275mutex_lock(&mvm->mutex);12761277if (p2p_aux || (roc_ver >= 3 && iftype != NL80211_IFTYPE_P2P_DEVICE)) {1278if (mvmvif->roc_activity < ROC_NUM_ACTIVITIES) {1279iwl_mvm_roc_rm_cmd(mvm, mvmvif->roc_activity);1280mvmvif->roc_activity = ROC_NUM_ACTIVITIES;1281}1282goto cleanup_roc;1283} else if (fw_has_capa(&mvm->fw->ucode_capa,1284IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {1285te_data = &mvmvif->time_event_data;12861287if (iftype == NL80211_IFTYPE_P2P_DEVICE) {1288if (te_data->id >= SESSION_PROTECT_CONF_MAX_ID) {1289IWL_DEBUG_TE(mvm,1290"No remain on channel event\n");1291mutex_unlock(&mvm->mutex);1292return;1293}1294iwl_mvm_cancel_session_protection(mvm, vif,1295te_data->id,1296te_data->link_id);1297} else {1298iwl_mvm_remove_aux_roc_te(mvm, mvmvif,1299&mvmvif->hs_time_event_data);1300}1301goto cleanup_roc;1302}13031304te_data = iwl_mvm_get_roc_te(mvm);1305if (!te_data) {1306IWL_WARN(mvm, "No remain on channel event\n");1307mutex_unlock(&mvm->mutex);1308return;1309}13101311mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);1312iftype = te_data->vif->type;1313if (iftype == NL80211_IFTYPE_P2P_DEVICE)1314iwl_mvm_remove_time_event(mvm, mvmvif, te_data);1315else1316iwl_mvm_remove_aux_roc_te(mvm, mvmvif, te_data);13171318cleanup_roc:1319/*1320* In case we get here before the ROC event started,1321* (so the status bit isn't set) set it here so iwl_mvm_cleanup_roc will1322* cleanup things properly1323*/1324if (p2p_aux || iftype != NL80211_IFTYPE_P2P_DEVICE)1325set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);1326else1327set_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status);13281329/* Mutex is released inside this function */1330iwl_mvm_cleanup_roc(mvm);1331}13321333void iwl_mvm_remove_csa_period(struct iwl_mvm *mvm,1334struct ieee80211_vif *vif)1335{1336struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1337struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;1338u32 id;13391340lockdep_assert_held(&mvm->mutex);13411342spin_lock_bh(&mvm->time_event_lock);1343id = te_data->id;1344spin_unlock_bh(&mvm->time_event_lock);13451346if (id != TE_CHANNEL_SWITCH_PERIOD)1347return;13481349iwl_mvm_remove_time_event(mvm, mvmvif, te_data);1350}13511352int iwl_mvm_schedule_csa_period(struct iwl_mvm *mvm,1353struct ieee80211_vif *vif,1354u32 duration, u32 apply_time)1355{1356struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1357struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;1358struct iwl_time_event_cmd time_cmd = {};13591360lockdep_assert_held(&mvm->mutex);13611362if (te_data->running) {1363u32 id;13641365spin_lock_bh(&mvm->time_event_lock);1366id = te_data->id;1367spin_unlock_bh(&mvm->time_event_lock);13681369if (id == TE_CHANNEL_SWITCH_PERIOD) {1370IWL_DEBUG_TE(mvm, "CS period is already scheduled\n");1371return -EBUSY;1372}13731374/*1375* Remove the session protection time event to allow the1376* channel switch. If we got here, we just heard a beacon so1377* the session protection is not needed anymore anyway.1378*/1379iwl_mvm_remove_time_event(mvm, mvmvif, te_data);1380}13811382time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);1383time_cmd.id_and_color =1384cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));1385time_cmd.id = cpu_to_le32(TE_CHANNEL_SWITCH_PERIOD);1386time_cmd.apply_time = cpu_to_le32(apply_time);1387time_cmd.max_frags = TE_V2_FRAG_NONE;1388time_cmd.duration = cpu_to_le32(duration);1389time_cmd.repeat = 1;1390time_cmd.interval = cpu_to_le32(1);1391time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |1392TE_V2_ABSENCE);1393if (!apply_time)1394time_cmd.policy |= cpu_to_le16(TE_V2_START_IMMEDIATELY);13951396return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);1397}13981399static bool iwl_mvm_session_prot_notif(struct iwl_notif_wait_data *notif_wait,1400struct iwl_rx_packet *pkt, void *data)1401{1402struct iwl_mvm *mvm =1403container_of(notif_wait, struct iwl_mvm, notif_wait);1404struct iwl_session_prot_notif *resp;1405int resp_len = iwl_rx_packet_payload_len(pkt);14061407if (WARN_ON(pkt->hdr.cmd != SESSION_PROTECTION_NOTIF ||1408pkt->hdr.group_id != MAC_CONF_GROUP))1409return true;14101411if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {1412IWL_ERR(mvm, "Invalid SESSION_PROTECTION_NOTIF response\n");1413return true;1414}14151416resp = (void *)pkt->data;14171418if (!resp->status)1419IWL_ERR(mvm,1420"TIME_EVENT_NOTIFICATION received but not executed\n");14211422return true;1423}14241425void iwl_mvm_schedule_session_protection(struct iwl_mvm *mvm,1426struct ieee80211_vif *vif,1427u32 duration, u32 min_duration,1428bool wait_for_notif,1429unsigned int link_id)1430{1431struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);1432struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;1433const u16 notif[] = { WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_NOTIF) };1434struct iwl_notification_wait wait_notif;1435int mac_link_id = iwl_mvm_get_session_prot_id(mvm, vif, (s8)link_id);1436struct iwl_session_prot_cmd cmd = {1437.id_and_color = cpu_to_le32(mac_link_id),1438.action = cpu_to_le32(FW_CTXT_ACTION_ADD),1439.conf_id = cpu_to_le32(SESSION_PROTECT_CONF_ASSOC),1440.duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),1441};14421443if (mac_link_id < 0)1444return;14451446lockdep_assert_held(&mvm->mutex);14471448spin_lock_bh(&mvm->time_event_lock);1449if (te_data->running && te_data->link_id == link_id &&1450time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {1451IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",1452jiffies_to_msecs(te_data->end_jiffies - jiffies));1453spin_unlock_bh(&mvm->time_event_lock);14541455return;1456}14571458iwl_mvm_te_clear_data(mvm, te_data);1459/*1460* The time_event_data.id field is reused to save session1461* protection's configuration.1462*/1463te_data->id = le32_to_cpu(cmd.conf_id);1464te_data->duration = le32_to_cpu(cmd.duration_tu);1465te_data->vif = vif;1466te_data->link_id = link_id;1467spin_unlock_bh(&mvm->time_event_lock);14681469IWL_DEBUG_TE(mvm, "Add new session protection, duration %d TU\n",1470le32_to_cpu(cmd.duration_tu));14711472if (!wait_for_notif) {1473if (iwl_mvm_send_cmd_pdu(mvm,1474WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),14750, sizeof(cmd), &cmd)) {1476goto send_cmd_err;1477}14781479return;1480}14811482iwl_init_notification_wait(&mvm->notif_wait, &wait_notif,1483notif, ARRAY_SIZE(notif),1484iwl_mvm_session_prot_notif, NULL);14851486if (iwl_mvm_send_cmd_pdu(mvm,1487WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),14880, sizeof(cmd), &cmd)) {1489iwl_remove_notification(&mvm->notif_wait, &wait_notif);1490goto send_cmd_err;1491} else if (iwl_wait_notification(&mvm->notif_wait, &wait_notif,1492TU_TO_JIFFIES(100))) {1493IWL_ERR(mvm,1494"Failed to protect session until session protection\n");1495}1496return;14971498send_cmd_err:1499IWL_ERR(mvm,1500"Couldn't send the SESSION_PROTECTION_CMD\n");1501spin_lock_bh(&mvm->time_event_lock);1502iwl_mvm_te_clear_data(mvm, te_data);1503spin_unlock_bh(&mvm->time_event_lock);1504}150515061507