Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
0002 /*
0003  * Copyright (C) 2012-2014, 2018-2022 Intel Corporation
0004  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
0005  * Copyright (C) 2017 Intel Deutschland GmbH
0006  */
0007 #include <linux/jiffies.h>
0008 #include <net/mac80211.h>
0009 
0010 #include "fw/notif-wait.h"
0011 #include "iwl-trans.h"
0012 #include "fw-api.h"
0013 #include "time-event.h"
0014 #include "mvm.h"
0015 #include "iwl-io.h"
0016 #include "iwl-prph.h"
0017 
0018 /*
0019  * For the high priority TE use a time event type that has similar priority to
0020  * the FW's action scan priority.
0021  */
0022 #define IWL_MVM_ROC_TE_TYPE_NORMAL TE_P2P_DEVICE_DISCOVERABLE
0023 #define IWL_MVM_ROC_TE_TYPE_MGMT_TX TE_P2P_CLIENT_ASSOC
0024 
0025 void iwl_mvm_te_clear_data(struct iwl_mvm *mvm,
0026                struct iwl_mvm_time_event_data *te_data)
0027 {
0028     lockdep_assert_held(&mvm->time_event_lock);
0029 
0030     if (!te_data || !te_data->vif)
0031         return;
0032 
0033     list_del(&te_data->list);
0034 
0035     /*
0036      * the list is only used for AUX ROC events so make sure it is always
0037      * initialized
0038      */
0039     INIT_LIST_HEAD(&te_data->list);
0040 
0041     te_data->running = false;
0042     te_data->uid = 0;
0043     te_data->id = TE_MAX;
0044     te_data->vif = NULL;
0045 }
0046 
0047 void iwl_mvm_roc_done_wk(struct work_struct *wk)
0048 {
0049     struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, roc_done_wk);
0050 
0051     /*
0052      * Clear the ROC_RUNNING status bit.
0053      * This will cause the TX path to drop offchannel transmissions.
0054      * That would also be done by mac80211, but it is racy, in particular
0055      * in the case that the time event actually completed in the firmware
0056      * (which is handled in iwl_mvm_te_handle_notif).
0057      */
0058     clear_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
0059 
0060     synchronize_net();
0061 
0062     /*
0063      * Flush the offchannel queue -- this is called when the time
0064      * event finishes or is canceled, so that frames queued for it
0065      * won't get stuck on the queue and be transmitted in the next
0066      * time event.
0067      */
0068 
0069     mutex_lock(&mvm->mutex);
0070     if (test_and_clear_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status)) {
0071         struct iwl_mvm_vif *mvmvif;
0072 
0073         /*
0074          * NB: access to this pointer would be racy, but the flush bit
0075          * can only be set when we had a P2P-Device VIF, and we have a
0076          * flush of this work in iwl_mvm_prepare_mac_removal() so it's
0077          * not really racy.
0078          */
0079 
0080         if (!WARN_ON(!mvm->p2p_device_vif)) {
0081             mvmvif = iwl_mvm_vif_from_mac80211(mvm->p2p_device_vif);
0082             iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true);
0083         }
0084     }
0085 
0086     /*
0087      * Clear the ROC_AUX_RUNNING status bit.
0088      * This will cause the TX path to drop offchannel transmissions.
0089      * That would also be done by mac80211, but it is racy, in particular
0090      * in the case that the time event actually completed in the firmware
0091      * (which is handled in iwl_mvm_te_handle_notif).
0092      */
0093     if (test_and_clear_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status)) {
0094         /* do the same in case of hot spot 2.0 */
0095         iwl_mvm_flush_sta(mvm, &mvm->aux_sta, true);
0096 
0097         /* In newer version of this command an aux station is added only
0098          * in cases of dedicated tx queue and need to be removed in end
0099          * of use */
0100         if (iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA, 0) >= 12)
0101             iwl_mvm_rm_aux_sta(mvm);
0102     }
0103 
0104     mutex_unlock(&mvm->mutex);
0105 }
0106 
0107 static void iwl_mvm_roc_finished(struct iwl_mvm *mvm)
0108 {
0109     /*
0110      * Of course, our status bit is just as racy as mac80211, so in
0111      * addition, fire off the work struct which will drop all frames
0112      * from the hardware queues that made it through the race. First
0113      * it will of course synchronize the TX path to make sure that
0114      * any *new* TX will be rejected.
0115      */
0116     schedule_work(&mvm->roc_done_wk);
0117 }
0118 
0119 static void iwl_mvm_csa_noa_start(struct iwl_mvm *mvm)
0120 {
0121     struct ieee80211_vif *csa_vif;
0122 
0123     rcu_read_lock();
0124 
0125     csa_vif = rcu_dereference(mvm->csa_vif);
0126     if (!csa_vif || !csa_vif->bss_conf.csa_active)
0127         goto out_unlock;
0128 
0129     IWL_DEBUG_TE(mvm, "CSA NOA started\n");
0130 
0131     /*
0132      * CSA NoA is started but we still have beacons to
0133      * transmit on the current channel.
0134      * So we just do nothing here and the switch
0135      * will be performed on the last TBTT.
0136      */
0137     if (!ieee80211_beacon_cntdwn_is_complete(csa_vif)) {
0138         IWL_WARN(mvm, "CSA NOA started too early\n");
0139         goto out_unlock;
0140     }
0141 
0142     ieee80211_csa_finish(csa_vif);
0143 
0144     rcu_read_unlock();
0145 
0146     RCU_INIT_POINTER(mvm->csa_vif, NULL);
0147 
0148     return;
0149 
0150 out_unlock:
0151     rcu_read_unlock();
0152 }
0153 
0154 static bool iwl_mvm_te_check_disconnect(struct iwl_mvm *mvm,
0155                     struct ieee80211_vif *vif,
0156                     const char *errmsg)
0157 {
0158     struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
0159 
0160     if (vif->type != NL80211_IFTYPE_STATION)
0161         return false;
0162 
0163     if (!mvmvif->csa_bcn_pending && vif->cfg.assoc &&
0164         vif->bss_conf.dtim_period)
0165         return false;
0166     if (errmsg)
0167         IWL_ERR(mvm, "%s\n", errmsg);
0168 
0169     if (mvmvif->csa_bcn_pending) {
0170         struct iwl_mvm_sta *mvmsta;
0171 
0172         rcu_read_lock();
0173         mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
0174         if (!WARN_ON(!mvmsta))
0175             iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
0176         rcu_read_unlock();
0177     }
0178 
0179     if (vif->cfg.assoc) {
0180         /*
0181          * When not associated, this will be called from
0182          * iwl_mvm_event_mlme_callback_ini()
0183          */
0184         iwl_dbg_tlv_time_point(&mvm->fwrt,
0185                        IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
0186                        NULL);
0187     }
0188 
0189     iwl_mvm_connection_loss(mvm, vif, errmsg);
0190     return true;
0191 }
0192 
0193 static void
0194 iwl_mvm_te_handle_notify_csa(struct iwl_mvm *mvm,
0195                  struct iwl_mvm_time_event_data *te_data,
0196                  struct iwl_time_event_notif *notif)
0197 {
0198     struct ieee80211_vif *vif = te_data->vif;
0199     struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
0200 
0201     if (!notif->status)
0202         IWL_DEBUG_TE(mvm, "CSA time event failed to start\n");
0203 
0204     switch (te_data->vif->type) {
0205     case NL80211_IFTYPE_AP:
0206         if (!notif->status)
0207             mvmvif->csa_failed = true;
0208         iwl_mvm_csa_noa_start(mvm);
0209         break;
0210     case NL80211_IFTYPE_STATION:
0211         if (!notif->status) {
0212             iwl_mvm_connection_loss(mvm, vif,
0213                         "CSA TE failed to start");
0214             break;
0215         }
0216         iwl_mvm_csa_client_absent(mvm, te_data->vif);
0217         cancel_delayed_work(&mvmvif->csa_work);
0218         ieee80211_chswitch_done(te_data->vif, true);
0219         break;
0220     default:
0221         /* should never happen */
0222         WARN_ON_ONCE(1);
0223         break;
0224     }
0225 
0226     /* we don't need it anymore */
0227     iwl_mvm_te_clear_data(mvm, te_data);
0228 }
0229 
0230 static void iwl_mvm_te_check_trigger(struct iwl_mvm *mvm,
0231                      struct iwl_time_event_notif *notif,
0232                      struct iwl_mvm_time_event_data *te_data)
0233 {
0234     struct iwl_fw_dbg_trigger_tlv *trig;
0235     struct iwl_fw_dbg_trigger_time_event *te_trig;
0236     int i;
0237 
0238     trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
0239                      ieee80211_vif_to_wdev(te_data->vif),
0240                      FW_DBG_TRIGGER_TIME_EVENT);
0241     if (!trig)
0242         return;
0243 
0244     te_trig = (void *)trig->data;
0245 
0246     for (i = 0; i < ARRAY_SIZE(te_trig->time_events); i++) {
0247         u32 trig_te_id = le32_to_cpu(te_trig->time_events[i].id);
0248         u32 trig_action_bitmap =
0249             le32_to_cpu(te_trig->time_events[i].action_bitmap);
0250         u32 trig_status_bitmap =
0251             le32_to_cpu(te_trig->time_events[i].status_bitmap);
0252 
0253         if (trig_te_id != te_data->id ||
0254             !(trig_action_bitmap & le32_to_cpu(notif->action)) ||
0255             !(trig_status_bitmap & BIT(le32_to_cpu(notif->status))))
0256             continue;
0257 
0258         iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
0259                     "Time event %d Action 0x%x received status: %d",
0260                     te_data->id,
0261                     le32_to_cpu(notif->action),
0262                     le32_to_cpu(notif->status));
0263         break;
0264     }
0265 }
0266 
0267 static void iwl_mvm_p2p_roc_finished(struct iwl_mvm *mvm)
0268 {
0269     /*
0270      * If the IWL_MVM_STATUS_NEED_FLUSH_P2P is already set, then the
0271      * roc_done_wk is already scheduled or running, so don't schedule it
0272      * again to avoid a race where the roc_done_wk clears this bit after
0273      * it is set here, affecting the next run of the roc_done_wk.
0274      */
0275     if (!test_and_set_bit(IWL_MVM_STATUS_NEED_FLUSH_P2P, &mvm->status))
0276         iwl_mvm_roc_finished(mvm);
0277 }
0278 
0279 /*
0280  * Handles a FW notification for an event that is known to the driver.
0281  *
0282  * @mvm: the mvm component
0283  * @te_data: the time event data
0284  * @notif: the notification data corresponding the time event data.
0285  */
0286 static void iwl_mvm_te_handle_notif(struct iwl_mvm *mvm,
0287                     struct iwl_mvm_time_event_data *te_data,
0288                     struct iwl_time_event_notif *notif)
0289 {
0290     lockdep_assert_held(&mvm->time_event_lock);
0291 
0292     IWL_DEBUG_TE(mvm, "Handle time event notif - UID = 0x%x action %d\n",
0293              le32_to_cpu(notif->unique_id),
0294              le32_to_cpu(notif->action));
0295 
0296     iwl_mvm_te_check_trigger(mvm, notif, te_data);
0297 
0298     /*
0299      * The FW sends the start/end time event notifications even for events
0300      * that it fails to schedule. This is indicated in the status field of
0301      * the notification. This happens in cases that the scheduler cannot
0302      * find a schedule that can handle the event (for example requesting a
0303      * P2P Device discoveribility, while there are other higher priority
0304      * events in the system).
0305      */
0306     if (!le32_to_cpu(notif->status)) {
0307         const char *msg;
0308 
0309         if (notif->action & cpu_to_le32(TE_V2_NOTIF_HOST_EVENT_START))
0310             msg = "Time Event start notification failure";
0311         else
0312             msg = "Time Event end notification failure";
0313 
0314         IWL_DEBUG_TE(mvm, "%s\n", msg);
0315 
0316         if (iwl_mvm_te_check_disconnect(mvm, te_data->vif, msg)) {
0317             iwl_mvm_te_clear_data(mvm, te_data);
0318             return;
0319         }
0320     }
0321 
0322     if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_END) {
0323         IWL_DEBUG_TE(mvm,
0324                  "TE ended - current time %lu, estimated end %lu\n",
0325                  jiffies, te_data->end_jiffies);
0326 
0327         switch (te_data->vif->type) {
0328         case NL80211_IFTYPE_P2P_DEVICE:
0329             ieee80211_remain_on_channel_expired(mvm->hw);
0330             iwl_mvm_p2p_roc_finished(mvm);
0331             break;
0332         case NL80211_IFTYPE_STATION:
0333             /*
0334              * If we are switching channel, don't disconnect
0335              * if the time event is already done. Beacons can
0336              * be delayed a bit after the switch.
0337              */
0338             if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
0339                 IWL_DEBUG_TE(mvm,
0340                          "No beacon heard and the CS time event is over, don't disconnect\n");
0341                 break;
0342             }
0343 
0344             /*
0345              * By now, we should have finished association
0346              * and know the dtim period.
0347              */
0348             iwl_mvm_te_check_disconnect(mvm, te_data->vif,
0349                 !te_data->vif->cfg.assoc ?
0350                 "Not associated and the time event is over already..." :
0351                 "No beacon heard and the time event is over already...");
0352             break;
0353         default:
0354             break;
0355         }
0356 
0357         iwl_mvm_te_clear_data(mvm, te_data);
0358     } else if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_START) {
0359         te_data->running = true;
0360         te_data->end_jiffies = TU_TO_EXP_TIME(te_data->duration);
0361 
0362         if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
0363             set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
0364             ieee80211_ready_on_channel(mvm->hw);
0365         } else if (te_data->id == TE_CHANNEL_SWITCH_PERIOD) {
0366             iwl_mvm_te_handle_notify_csa(mvm, te_data, notif);
0367         }
0368     } else {
0369         IWL_WARN(mvm, "Got TE with unknown action\n");
0370     }
0371 }
0372 
0373 /*
0374  * Handle A Aux ROC time event
0375  */
0376 static int iwl_mvm_aux_roc_te_handle_notif(struct iwl_mvm *mvm,
0377                        struct iwl_time_event_notif *notif)
0378 {
0379     struct iwl_mvm_time_event_data *te_data, *tmp;
0380     bool aux_roc_te = false;
0381 
0382     list_for_each_entry_safe(te_data, tmp, &mvm->aux_roc_te_list, list) {
0383         if (le32_to_cpu(notif->unique_id) == te_data->uid) {
0384             aux_roc_te = true;
0385             break;
0386         }
0387     }
0388     if (!aux_roc_te) /* Not a Aux ROC time event */
0389         return -EINVAL;
0390 
0391     iwl_mvm_te_check_trigger(mvm, notif, te_data);
0392 
0393     IWL_DEBUG_TE(mvm,
0394              "Aux ROC time event notification  - UID = 0x%x action %d (error = %d)\n",
0395              le32_to_cpu(notif->unique_id),
0396              le32_to_cpu(notif->action), le32_to_cpu(notif->status));
0397 
0398     if (!le32_to_cpu(notif->status) ||
0399         le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_END) {
0400         /* End TE, notify mac80211 */
0401         ieee80211_remain_on_channel_expired(mvm->hw);
0402         iwl_mvm_roc_finished(mvm); /* flush aux queue */
0403         list_del(&te_data->list); /* remove from list */
0404         te_data->running = false;
0405         te_data->vif = NULL;
0406         te_data->uid = 0;
0407         te_data->id = TE_MAX;
0408     } else if (le32_to_cpu(notif->action) == TE_V2_NOTIF_HOST_EVENT_START) {
0409         set_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status);
0410         te_data->running = true;
0411         ieee80211_ready_on_channel(mvm->hw); /* Start TE */
0412     } else {
0413         IWL_DEBUG_TE(mvm,
0414                  "ERROR: Unknown Aux ROC Time Event (action = %d)\n",
0415                  le32_to_cpu(notif->action));
0416         return -EINVAL;
0417     }
0418 
0419     return 0;
0420 }
0421 
0422 /*
0423  * The Rx handler for time event notifications
0424  */
0425 void iwl_mvm_rx_time_event_notif(struct iwl_mvm *mvm,
0426                  struct iwl_rx_cmd_buffer *rxb)
0427 {
0428     struct iwl_rx_packet *pkt = rxb_addr(rxb);
0429     struct iwl_time_event_notif *notif = (void *)pkt->data;
0430     struct iwl_mvm_time_event_data *te_data, *tmp;
0431 
0432     IWL_DEBUG_TE(mvm, "Time event notification - UID = 0x%x action %d\n",
0433              le32_to_cpu(notif->unique_id),
0434              le32_to_cpu(notif->action));
0435 
0436     spin_lock_bh(&mvm->time_event_lock);
0437     /* This time event is triggered for Aux ROC request */
0438     if (!iwl_mvm_aux_roc_te_handle_notif(mvm, notif))
0439         goto unlock;
0440 
0441     list_for_each_entry_safe(te_data, tmp, &mvm->time_event_list, list) {
0442         if (le32_to_cpu(notif->unique_id) == te_data->uid)
0443             iwl_mvm_te_handle_notif(mvm, te_data, notif);
0444     }
0445 unlock:
0446     spin_unlock_bh(&mvm->time_event_lock);
0447 }
0448 
0449 static bool iwl_mvm_te_notif(struct iwl_notif_wait_data *notif_wait,
0450                  struct iwl_rx_packet *pkt, void *data)
0451 {
0452     struct iwl_mvm *mvm =
0453         container_of(notif_wait, struct iwl_mvm, notif_wait);
0454     struct iwl_mvm_time_event_data *te_data = data;
0455     struct iwl_time_event_notif *resp;
0456     int resp_len = iwl_rx_packet_payload_len(pkt);
0457 
0458     if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_NOTIFICATION))
0459         return true;
0460 
0461     if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
0462         IWL_ERR(mvm, "Invalid TIME_EVENT_NOTIFICATION response\n");
0463         return true;
0464     }
0465 
0466     resp = (void *)pkt->data;
0467 
0468     /* te_data->uid is already set in the TIME_EVENT_CMD response */
0469     if (le32_to_cpu(resp->unique_id) != te_data->uid)
0470         return false;
0471 
0472     IWL_DEBUG_TE(mvm, "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n",
0473              te_data->uid);
0474     if (!resp->status)
0475         IWL_ERR(mvm,
0476             "TIME_EVENT_NOTIFICATION received but not executed\n");
0477 
0478     return true;
0479 }
0480 
0481 static bool iwl_mvm_time_event_response(struct iwl_notif_wait_data *notif_wait,
0482                     struct iwl_rx_packet *pkt, void *data)
0483 {
0484     struct iwl_mvm *mvm =
0485         container_of(notif_wait, struct iwl_mvm, notif_wait);
0486     struct iwl_mvm_time_event_data *te_data = data;
0487     struct iwl_time_event_resp *resp;
0488     int resp_len = iwl_rx_packet_payload_len(pkt);
0489 
0490     if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_CMD))
0491         return true;
0492 
0493     if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
0494         IWL_ERR(mvm, "Invalid TIME_EVENT_CMD response\n");
0495         return true;
0496     }
0497 
0498     resp = (void *)pkt->data;
0499 
0500     /* we should never get a response to another TIME_EVENT_CMD here */
0501     if (WARN_ON_ONCE(le32_to_cpu(resp->id) != te_data->id))
0502         return false;
0503 
0504     te_data->uid = le32_to_cpu(resp->unique_id);
0505     IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
0506              te_data->uid);
0507     return true;
0508 }
0509 
0510 static int iwl_mvm_time_event_send_add(struct iwl_mvm *mvm,
0511                        struct ieee80211_vif *vif,
0512                        struct iwl_mvm_time_event_data *te_data,
0513                        struct iwl_time_event_cmd *te_cmd)
0514 {
0515     static const u16 time_event_response[] = { TIME_EVENT_CMD };
0516     struct iwl_notification_wait wait_time_event;
0517     int ret;
0518 
0519     lockdep_assert_held(&mvm->mutex);
0520 
0521     IWL_DEBUG_TE(mvm, "Add new TE, duration %d TU\n",
0522              le32_to_cpu(te_cmd->duration));
0523 
0524     spin_lock_bh(&mvm->time_event_lock);
0525     if (WARN_ON(te_data->id != TE_MAX)) {
0526         spin_unlock_bh(&mvm->time_event_lock);
0527         return -EIO;
0528     }
0529     te_data->vif = vif;
0530     te_data->duration = le32_to_cpu(te_cmd->duration);
0531     te_data->id = le32_to_cpu(te_cmd->id);
0532     list_add_tail(&te_data->list, &mvm->time_event_list);
0533     spin_unlock_bh(&mvm->time_event_lock);
0534 
0535     /*
0536      * Use a notification wait, which really just processes the
0537      * command response and doesn't wait for anything, in order
0538      * to be able to process the response and get the UID inside
0539      * the RX path. Using CMD_WANT_SKB doesn't work because it
0540      * stores the buffer and then wakes up this thread, by which
0541      * time another notification (that the time event started)
0542      * might already be processed unsuccessfully.
0543      */
0544     iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
0545                    time_event_response,
0546                    ARRAY_SIZE(time_event_response),
0547                    iwl_mvm_time_event_response, te_data);
0548 
0549     ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
0550                         sizeof(*te_cmd), te_cmd);
0551     if (ret) {
0552         IWL_ERR(mvm, "Couldn't send TIME_EVENT_CMD: %d\n", ret);
0553         iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
0554         goto out_clear_te;
0555     }
0556 
0557     /* No need to wait for anything, so just pass 1 (0 isn't valid) */
0558     ret = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
0559     /* should never fail */
0560     WARN_ON_ONCE(ret);
0561 
0562     if (ret) {
0563  out_clear_te:
0564         spin_lock_bh(&mvm->time_event_lock);
0565         iwl_mvm_te_clear_data(mvm, te_data);
0566         spin_unlock_bh(&mvm->time_event_lock);
0567     }
0568     return ret;
0569 }
0570 
0571 void iwl_mvm_protect_session(struct iwl_mvm *mvm,
0572                  struct ieee80211_vif *vif,
0573                  u32 duration, u32 min_duration,
0574                  u32 max_delay, bool wait_for_notif)
0575 {
0576     struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
0577     struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
0578     const u16 te_notif_response[] = { TIME_EVENT_NOTIFICATION };
0579     struct iwl_notification_wait wait_te_notif;
0580     struct iwl_time_event_cmd time_cmd = {};
0581 
0582     lockdep_assert_held(&mvm->mutex);
0583 
0584     if (te_data->running &&
0585         time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
0586         IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
0587                  jiffies_to_msecs(te_data->end_jiffies - jiffies));
0588         return;
0589     }
0590 
0591     if (te_data->running) {
0592         IWL_DEBUG_TE(mvm, "extend 0x%x: only %u ms left\n",
0593                  te_data->uid,
0594                  jiffies_to_msecs(te_data->end_jiffies - jiffies));
0595         /*
0596          * we don't have enough time
0597          * cancel the current TE and issue a new one
0598          * Of course it would be better to remove the old one only
0599          * when the new one is added, but we don't care if we are off
0600          * channel for a bit. All we need to do, is not to return
0601          * before we actually begin to be on the channel.
0602          */
0603         iwl_mvm_stop_session_protection(mvm, vif);
0604     }
0605 
0606     time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
0607     time_cmd.id_and_color =
0608         cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
0609     time_cmd.id = cpu_to_le32(TE_BSS_STA_AGGRESSIVE_ASSOC);
0610 
0611     time_cmd.apply_time = cpu_to_le32(0);
0612 
0613     time_cmd.max_frags = TE_V2_FRAG_NONE;
0614     time_cmd.max_delay = cpu_to_le32(max_delay);
0615     /* TODO: why do we need to interval = bi if it is not periodic? */
0616     time_cmd.interval = cpu_to_le32(1);
0617     time_cmd.duration = cpu_to_le32(duration);
0618     time_cmd.repeat = 1;
0619     time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
0620                       TE_V2_NOTIF_HOST_EVENT_END |
0621                       TE_V2_START_IMMEDIATELY);
0622 
0623     if (!wait_for_notif) {
0624         iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
0625         return;
0626     }
0627 
0628     /*
0629      * Create notification_wait for the TIME_EVENT_NOTIFICATION to use
0630      * right after we send the time event
0631      */
0632     iwl_init_notification_wait(&mvm->notif_wait, &wait_te_notif,
0633                    te_notif_response,
0634                    ARRAY_SIZE(te_notif_response),
0635                    iwl_mvm_te_notif, te_data);
0636 
0637     /* If TE was sent OK - wait for the notification that started */
0638     if (iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd)) {
0639         IWL_ERR(mvm, "Failed to add TE to protect session\n");
0640         iwl_remove_notification(&mvm->notif_wait, &wait_te_notif);
0641     } else if (iwl_wait_notification(&mvm->notif_wait, &wait_te_notif,
0642                      TU_TO_JIFFIES(max_delay))) {
0643         IWL_ERR(mvm, "Failed to protect session until TE\n");
0644     }
0645 }
0646 
0647 static void iwl_mvm_cancel_session_protection(struct iwl_mvm *mvm,
0648                           struct iwl_mvm_vif *mvmvif,
0649                           u32 id)
0650 {
0651     struct iwl_mvm_session_prot_cmd cmd = {
0652         .id_and_color =
0653             cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
0654                             mvmvif->color)),
0655         .action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
0656         .conf_id = cpu_to_le32(id),
0657     };
0658     int ret;
0659 
0660     ret = iwl_mvm_send_cmd_pdu(mvm,
0661                    WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
0662                    0, sizeof(cmd), &cmd);
0663     if (ret)
0664         IWL_ERR(mvm,
0665             "Couldn't send the SESSION_PROTECTION_CMD: %d\n", ret);
0666 }
0667 
0668 static bool __iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
0669                     struct iwl_mvm_time_event_data *te_data,
0670                     u32 *uid)
0671 {
0672     u32 id;
0673     struct iwl_mvm_vif *mvmvif;
0674     enum nl80211_iftype iftype;
0675 
0676     if (!te_data->vif)
0677         return false;
0678 
0679     mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
0680     iftype = te_data->vif->type;
0681 
0682     /*
0683      * It is possible that by the time we got to this point the time
0684      * event was already removed.
0685      */
0686     spin_lock_bh(&mvm->time_event_lock);
0687 
0688     /* Save time event uid before clearing its data */
0689     *uid = te_data->uid;
0690     id = te_data->id;
0691 
0692     /*
0693      * The clear_data function handles time events that were already removed
0694      */
0695     iwl_mvm_te_clear_data(mvm, te_data);
0696     spin_unlock_bh(&mvm->time_event_lock);
0697 
0698     /* When session protection is used, the te_data->id field
0699      * is reused to save session protection's configuration.
0700      * For AUX ROC, HOT_SPOT_CMD is used and the te_data->id field is set
0701      * to HOT_SPOT_CMD.
0702      */
0703     if (fw_has_capa(&mvm->fw->ucode_capa,
0704             IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD) &&
0705         id != HOT_SPOT_CMD) {
0706         if (mvmvif && id < SESSION_PROTECT_CONF_MAX_ID) {
0707             /* Session protection is still ongoing. Cancel it */
0708             iwl_mvm_cancel_session_protection(mvm, mvmvif, id);
0709             if (iftype == NL80211_IFTYPE_P2P_DEVICE) {
0710                 iwl_mvm_p2p_roc_finished(mvm);
0711             }
0712         }
0713         return false;
0714     } else {
0715         /* It is possible that by the time we try to remove it, the
0716          * time event has already ended and removed. In such a case
0717          * there is no need to send a removal command.
0718          */
0719         if (id == TE_MAX) {
0720             IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", *uid);
0721             return false;
0722         }
0723     }
0724 
0725     return true;
0726 }
0727 
0728 /*
0729  * Explicit request to remove a aux roc time event. The removal of a time
0730  * event needs to be synchronized with the flow of a time event's end
0731  * notification, which also removes the time event from the op mode
0732  * data structures.
0733  */
0734 static void iwl_mvm_remove_aux_roc_te(struct iwl_mvm *mvm,
0735                       struct iwl_mvm_vif *mvmvif,
0736                       struct iwl_mvm_time_event_data *te_data)
0737 {
0738     struct iwl_hs20_roc_req aux_cmd = {};
0739     u16 len = sizeof(aux_cmd) - iwl_mvm_chan_info_padding(mvm);
0740 
0741     u32 uid;
0742     int ret;
0743 
0744     if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
0745         return;
0746 
0747     aux_cmd.event_unique_id = cpu_to_le32(uid);
0748     aux_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
0749     aux_cmd.id_and_color =
0750         cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
0751     IWL_DEBUG_TE(mvm, "Removing BSS AUX ROC TE 0x%x\n",
0752              le32_to_cpu(aux_cmd.event_unique_id));
0753     ret = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0,
0754                    len, &aux_cmd);
0755 
0756     if (WARN_ON(ret))
0757         return;
0758 }
0759 
0760 /*
0761  * Explicit request to remove a time event. The removal of a time event needs to
0762  * be synchronized with the flow of a time event's end notification, which also
0763  * removes the time event from the op mode data structures.
0764  */
0765 void iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
0766                    struct iwl_mvm_vif *mvmvif,
0767                    struct iwl_mvm_time_event_data *te_data)
0768 {
0769     struct iwl_time_event_cmd time_cmd = {};
0770     u32 uid;
0771     int ret;
0772 
0773     if (!__iwl_mvm_remove_time_event(mvm, te_data, &uid))
0774         return;
0775 
0776     /* When we remove a TE, the UID is to be set in the id field */
0777     time_cmd.id = cpu_to_le32(uid);
0778     time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
0779     time_cmd.id_and_color =
0780         cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
0781 
0782     IWL_DEBUG_TE(mvm, "Removing TE 0x%x\n", le32_to_cpu(time_cmd.id));
0783     ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
0784                    sizeof(time_cmd), &time_cmd);
0785     if (ret)
0786         IWL_ERR(mvm, "Couldn't remove the time event\n");
0787 }
0788 
0789 void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,
0790                      struct ieee80211_vif *vif)
0791 {
0792     struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
0793     struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
0794     u32 id;
0795 
0796     lockdep_assert_held(&mvm->mutex);
0797 
0798     spin_lock_bh(&mvm->time_event_lock);
0799     id = te_data->id;
0800     spin_unlock_bh(&mvm->time_event_lock);
0801 
0802     if (fw_has_capa(&mvm->fw->ucode_capa,
0803             IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
0804         if (id != SESSION_PROTECT_CONF_ASSOC) {
0805             IWL_DEBUG_TE(mvm,
0806                      "don't remove session protection id=%u\n",
0807                      id);
0808             return;
0809         }
0810     } else if (id != TE_BSS_STA_AGGRESSIVE_ASSOC) {
0811         IWL_DEBUG_TE(mvm,
0812                  "don't remove TE with id=%u (not session protection)\n",
0813                  id);
0814         return;
0815     }
0816 
0817     iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
0818 }
0819 
0820 void iwl_mvm_rx_session_protect_notif(struct iwl_mvm *mvm,
0821                       struct iwl_rx_cmd_buffer *rxb)
0822 {
0823     struct iwl_rx_packet *pkt = rxb_addr(rxb);
0824     struct iwl_mvm_session_prot_notif *notif = (void *)pkt->data;
0825     struct ieee80211_vif *vif;
0826     struct iwl_mvm_vif *mvmvif;
0827 
0828     rcu_read_lock();
0829     vif = iwl_mvm_rcu_dereference_vif_id(mvm, le32_to_cpu(notif->mac_id),
0830                          true);
0831 
0832     if (!vif)
0833         goto out_unlock;
0834 
0835     mvmvif = iwl_mvm_vif_from_mac80211(vif);
0836 
0837     /* The vif is not a P2P_DEVICE, maintain its time_event_data */
0838     if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
0839         struct iwl_mvm_time_event_data *te_data =
0840             &mvmvif->time_event_data;
0841 
0842         if (!le32_to_cpu(notif->status)) {
0843             iwl_mvm_te_check_disconnect(mvm, vif,
0844                             "Session protection failure");
0845             spin_lock_bh(&mvm->time_event_lock);
0846             iwl_mvm_te_clear_data(mvm, te_data);
0847             spin_unlock_bh(&mvm->time_event_lock);
0848         }
0849 
0850         if (le32_to_cpu(notif->start)) {
0851             spin_lock_bh(&mvm->time_event_lock);
0852             te_data->running = le32_to_cpu(notif->start);
0853             te_data->end_jiffies =
0854                 TU_TO_EXP_TIME(te_data->duration);
0855             spin_unlock_bh(&mvm->time_event_lock);
0856         } else {
0857             /*
0858              * By now, we should have finished association
0859              * and know the dtim period.
0860              */
0861             iwl_mvm_te_check_disconnect(mvm, vif,
0862                             !vif->cfg.assoc ?
0863                             "Not associated and the session protection is over already..." :
0864                             "No beacon heard and the session protection is over already...");
0865             spin_lock_bh(&mvm->time_event_lock);
0866             iwl_mvm_te_clear_data(mvm, te_data);
0867             spin_unlock_bh(&mvm->time_event_lock);
0868         }
0869 
0870         goto out_unlock;
0871     }
0872 
0873     if (!le32_to_cpu(notif->status) || !le32_to_cpu(notif->start)) {
0874         /* End TE, notify mac80211 */
0875         mvmvif->time_event_data.id = SESSION_PROTECT_CONF_MAX_ID;
0876         ieee80211_remain_on_channel_expired(mvm->hw);
0877         iwl_mvm_p2p_roc_finished(mvm);
0878     } else if (le32_to_cpu(notif->start)) {
0879         if (WARN_ON(mvmvif->time_event_data.id !=
0880                 le32_to_cpu(notif->conf_id)))
0881             goto out_unlock;
0882         set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
0883         ieee80211_ready_on_channel(mvm->hw); /* Start TE */
0884     }
0885 
0886  out_unlock:
0887     rcu_read_unlock();
0888 }
0889 
0890 static int
0891 iwl_mvm_start_p2p_roc_session_protection(struct iwl_mvm *mvm,
0892                      struct ieee80211_vif *vif,
0893                      int duration,
0894                      enum ieee80211_roc_type type)
0895 {
0896     struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
0897     struct iwl_mvm_session_prot_cmd cmd = {
0898         .id_and_color =
0899             cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
0900                             mvmvif->color)),
0901         .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
0902         .duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
0903     };
0904 
0905     lockdep_assert_held(&mvm->mutex);
0906 
0907     /* The time_event_data.id field is reused to save session
0908      * protection's configuration.
0909      */
0910     switch (type) {
0911     case IEEE80211_ROC_TYPE_NORMAL:
0912         mvmvif->time_event_data.id =
0913             SESSION_PROTECT_CONF_P2P_DEVICE_DISCOV;
0914         break;
0915     case IEEE80211_ROC_TYPE_MGMT_TX:
0916         mvmvif->time_event_data.id =
0917             SESSION_PROTECT_CONF_P2P_GO_NEGOTIATION;
0918         break;
0919     default:
0920         WARN_ONCE(1, "Got an invalid ROC type\n");
0921         return -EINVAL;
0922     }
0923 
0924     cmd.conf_id = cpu_to_le32(mvmvif->time_event_data.id);
0925     return iwl_mvm_send_cmd_pdu(mvm,
0926                     WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
0927                     0, sizeof(cmd), &cmd);
0928 }
0929 
0930 int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
0931               int duration, enum ieee80211_roc_type type)
0932 {
0933     struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
0934     struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
0935     struct iwl_time_event_cmd time_cmd = {};
0936 
0937     lockdep_assert_held(&mvm->mutex);
0938     if (te_data->running) {
0939         IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");
0940         return -EBUSY;
0941     }
0942 
0943     if (fw_has_capa(&mvm->fw->ucode_capa,
0944             IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
0945         return iwl_mvm_start_p2p_roc_session_protection(mvm, vif,
0946                                 duration,
0947                                 type);
0948 
0949     time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
0950     time_cmd.id_and_color =
0951         cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
0952 
0953     switch (type) {
0954     case IEEE80211_ROC_TYPE_NORMAL:
0955         time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);
0956         break;
0957     case IEEE80211_ROC_TYPE_MGMT_TX:
0958         time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);
0959         break;
0960     default:
0961         WARN_ONCE(1, "Got an invalid ROC type\n");
0962         return -EINVAL;
0963     }
0964 
0965     time_cmd.apply_time = cpu_to_le32(0);
0966     time_cmd.interval = cpu_to_le32(1);
0967 
0968     /*
0969      * The P2P Device TEs can have lower priority than other events
0970      * that are being scheduled by the driver/fw, and thus it might not be
0971      * scheduled. To improve the chances of it being scheduled, allow them
0972      * to be fragmented, and in addition allow them to be delayed.
0973      */
0974     time_cmd.max_frags = min(MSEC_TO_TU(duration)/50, TE_V2_FRAG_ENDLESS);
0975     time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));
0976     time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));
0977     time_cmd.repeat = 1;
0978     time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
0979                       TE_V2_NOTIF_HOST_EVENT_END |
0980                       TE_V2_START_IMMEDIATELY);
0981 
0982     return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
0983 }
0984 
0985 static struct iwl_mvm_time_event_data *iwl_mvm_get_roc_te(struct iwl_mvm *mvm)
0986 {
0987     struct iwl_mvm_time_event_data *te_data;
0988 
0989     lockdep_assert_held(&mvm->mutex);
0990 
0991     spin_lock_bh(&mvm->time_event_lock);
0992 
0993     /*
0994      * Iterate over the list of time events and find the time event that is
0995      * associated with a P2P_DEVICE interface.
0996      * This assumes that a P2P_DEVICE interface can have only a single time
0997      * event at any given time and this time event coresponds to a ROC
0998      * request
0999      */
1000     list_for_each_entry(te_data, &mvm->time_event_list, list) {
1001         if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE)
1002             goto out;
1003     }
1004 
1005     /* There can only be at most one AUX ROC time event, we just use the
1006      * list to simplify/unify code. Remove it if it exists.
1007      */
1008     te_data = list_first_entry_or_null(&mvm->aux_roc_te_list,
1009                        struct iwl_mvm_time_event_data,
1010                        list);
1011 out:
1012     spin_unlock_bh(&mvm->time_event_lock);
1013     return te_data;
1014 }
1015 
1016 void iwl_mvm_cleanup_roc_te(struct iwl_mvm *mvm)
1017 {
1018     struct iwl_mvm_time_event_data *te_data;
1019     u32 uid;
1020 
1021     te_data = iwl_mvm_get_roc_te(mvm);
1022     if (te_data)
1023         __iwl_mvm_remove_time_event(mvm, te_data, &uid);
1024 }
1025 
1026 void iwl_mvm_stop_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1027 {
1028     struct iwl_mvm_vif *mvmvif;
1029     struct iwl_mvm_time_event_data *te_data;
1030 
1031     if (fw_has_capa(&mvm->fw->ucode_capa,
1032             IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
1033         mvmvif = iwl_mvm_vif_from_mac80211(vif);
1034 
1035         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1036             iwl_mvm_cancel_session_protection(mvm, mvmvif,
1037                               mvmvif->time_event_data.id);
1038             iwl_mvm_p2p_roc_finished(mvm);
1039         } else {
1040             iwl_mvm_remove_aux_roc_te(mvm, mvmvif,
1041                           &mvmvif->hs_time_event_data);
1042             iwl_mvm_roc_finished(mvm);
1043         }
1044 
1045         return;
1046     }
1047 
1048     te_data = iwl_mvm_get_roc_te(mvm);
1049     if (!te_data) {
1050         IWL_WARN(mvm, "No remain on channel event\n");
1051         return;
1052     }
1053 
1054     mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
1055 
1056     if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1057         iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1058         iwl_mvm_p2p_roc_finished(mvm);
1059     } else {
1060         iwl_mvm_remove_aux_roc_te(mvm, mvmvif, te_data);
1061         iwl_mvm_roc_finished(mvm);
1062     }
1063 }
1064 
1065 void iwl_mvm_remove_csa_period(struct iwl_mvm *mvm,
1066                    struct ieee80211_vif *vif)
1067 {
1068     struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1069     struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1070     u32 id;
1071 
1072     lockdep_assert_held(&mvm->mutex);
1073 
1074     spin_lock_bh(&mvm->time_event_lock);
1075     id = te_data->id;
1076     spin_unlock_bh(&mvm->time_event_lock);
1077 
1078     if (id != TE_CHANNEL_SWITCH_PERIOD)
1079         return;
1080 
1081     iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1082 }
1083 
1084 int iwl_mvm_schedule_csa_period(struct iwl_mvm *mvm,
1085                 struct ieee80211_vif *vif,
1086                 u32 duration, u32 apply_time)
1087 {
1088     struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1089     struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1090     struct iwl_time_event_cmd time_cmd = {};
1091 
1092     lockdep_assert_held(&mvm->mutex);
1093 
1094     if (te_data->running) {
1095         u32 id;
1096 
1097         spin_lock_bh(&mvm->time_event_lock);
1098         id = te_data->id;
1099         spin_unlock_bh(&mvm->time_event_lock);
1100 
1101         if (id == TE_CHANNEL_SWITCH_PERIOD) {
1102             IWL_DEBUG_TE(mvm, "CS period is already scheduled\n");
1103             return -EBUSY;
1104         }
1105 
1106         /*
1107          * Remove the session protection time event to allow the
1108          * channel switch. If we got here, we just heard a beacon so
1109          * the session protection is not needed anymore anyway.
1110          */
1111         iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
1112     }
1113 
1114     time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
1115     time_cmd.id_and_color =
1116         cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
1117     time_cmd.id = cpu_to_le32(TE_CHANNEL_SWITCH_PERIOD);
1118     time_cmd.apply_time = cpu_to_le32(apply_time);
1119     time_cmd.max_frags = TE_V2_FRAG_NONE;
1120     time_cmd.duration = cpu_to_le32(duration);
1121     time_cmd.repeat = 1;
1122     time_cmd.interval = cpu_to_le32(1);
1123     time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
1124                       TE_V2_ABSENCE);
1125     if (!apply_time)
1126         time_cmd.policy |= cpu_to_le16(TE_V2_START_IMMEDIATELY);
1127 
1128     return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
1129 }
1130 
1131 static bool iwl_mvm_session_prot_notif(struct iwl_notif_wait_data *notif_wait,
1132                        struct iwl_rx_packet *pkt, void *data)
1133 {
1134     struct iwl_mvm *mvm =
1135         container_of(notif_wait, struct iwl_mvm, notif_wait);
1136     struct iwl_mvm_session_prot_notif *resp;
1137     int resp_len = iwl_rx_packet_payload_len(pkt);
1138 
1139     if (WARN_ON(pkt->hdr.cmd != SESSION_PROTECTION_NOTIF ||
1140             pkt->hdr.group_id != MAC_CONF_GROUP))
1141         return true;
1142 
1143     if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
1144         IWL_ERR(mvm, "Invalid SESSION_PROTECTION_NOTIF response\n");
1145         return true;
1146     }
1147 
1148     resp = (void *)pkt->data;
1149 
1150     if (!resp->status)
1151         IWL_ERR(mvm,
1152             "TIME_EVENT_NOTIFICATION received but not executed\n");
1153 
1154     return true;
1155 }
1156 
1157 void iwl_mvm_schedule_session_protection(struct iwl_mvm *mvm,
1158                      struct ieee80211_vif *vif,
1159                      u32 duration, u32 min_duration,
1160                      bool wait_for_notif)
1161 {
1162     struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1163     struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
1164     const u16 notif[] = { WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_NOTIF) };
1165     struct iwl_notification_wait wait_notif;
1166     struct iwl_mvm_session_prot_cmd cmd = {
1167         .id_and_color =
1168             cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1169                             mvmvif->color)),
1170         .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
1171         .conf_id = cpu_to_le32(SESSION_PROTECT_CONF_ASSOC),
1172         .duration_tu = cpu_to_le32(MSEC_TO_TU(duration)),
1173     };
1174 
1175     lockdep_assert_held(&mvm->mutex);
1176 
1177     spin_lock_bh(&mvm->time_event_lock);
1178     if (te_data->running &&
1179         time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
1180         IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
1181                  jiffies_to_msecs(te_data->end_jiffies - jiffies));
1182         spin_unlock_bh(&mvm->time_event_lock);
1183 
1184         return;
1185     }
1186 
1187     iwl_mvm_te_clear_data(mvm, te_data);
1188     /*
1189      * The time_event_data.id field is reused to save session
1190      * protection's configuration.
1191      */
1192     te_data->id = le32_to_cpu(cmd.conf_id);
1193     te_data->duration = le32_to_cpu(cmd.duration_tu);
1194     te_data->vif = vif;
1195     spin_unlock_bh(&mvm->time_event_lock);
1196 
1197     IWL_DEBUG_TE(mvm, "Add new session protection, duration %d TU\n",
1198              le32_to_cpu(cmd.duration_tu));
1199 
1200     if (!wait_for_notif) {
1201         if (iwl_mvm_send_cmd_pdu(mvm,
1202                      WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1203                      0, sizeof(cmd), &cmd)) {
1204             IWL_ERR(mvm,
1205                 "Couldn't send the SESSION_PROTECTION_CMD\n");
1206             spin_lock_bh(&mvm->time_event_lock);
1207             iwl_mvm_te_clear_data(mvm, te_data);
1208             spin_unlock_bh(&mvm->time_event_lock);
1209         }
1210 
1211         return;
1212     }
1213 
1214     iwl_init_notification_wait(&mvm->notif_wait, &wait_notif,
1215                    notif, ARRAY_SIZE(notif),
1216                    iwl_mvm_session_prot_notif, NULL);
1217 
1218     if (iwl_mvm_send_cmd_pdu(mvm,
1219                  WIDE_ID(MAC_CONF_GROUP, SESSION_PROTECTION_CMD),
1220                  0, sizeof(cmd), &cmd)) {
1221         IWL_ERR(mvm,
1222             "Couldn't send the SESSION_PROTECTION_CMD\n");
1223         iwl_remove_notification(&mvm->notif_wait, &wait_notif);
1224     } else if (iwl_wait_notification(&mvm->notif_wait, &wait_notif,
1225                      TU_TO_JIFFIES(100))) {
1226         IWL_ERR(mvm,
1227             "Failed to protect session until session protection\n");
1228     }
1229 }