Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
0002 /*
0003  * Copyright (C) 2014 Intel Mobile Communications GmbH
0004  * Copyright (C) 2017 Intel Deutschland GmbH
0005  * Copyright (C) 2018-2020, 2022 Intel Corporation
0006  */
0007 #include <linux/etherdevice.h>
0008 #include "mvm.h"
0009 #include "time-event.h"
0010 #include "iwl-io.h"
0011 #include "iwl-prph.h"
0012 
0013 #define TU_TO_US(x) (x * 1024)
0014 #define TU_TO_MS(x) (TU_TO_US(x) / 1000)
0015 
0016 void iwl_mvm_teardown_tdls_peers(struct iwl_mvm *mvm)
0017 {
0018     struct ieee80211_sta *sta;
0019     struct iwl_mvm_sta *mvmsta;
0020     int i;
0021 
0022     lockdep_assert_held(&mvm->mutex);
0023 
0024     for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
0025         sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
0026                         lockdep_is_held(&mvm->mutex));
0027         if (!sta || IS_ERR(sta) || !sta->tdls)
0028             continue;
0029 
0030         mvmsta = iwl_mvm_sta_from_mac80211(sta);
0031         ieee80211_tdls_oper_request(mvmsta->vif, sta->addr,
0032                 NL80211_TDLS_TEARDOWN,
0033                 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED,
0034                 GFP_KERNEL);
0035     }
0036 }
0037 
0038 int iwl_mvm_tdls_sta_count(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
0039 {
0040     struct ieee80211_sta *sta;
0041     struct iwl_mvm_sta *mvmsta;
0042     int count = 0;
0043     int i;
0044 
0045     lockdep_assert_held(&mvm->mutex);
0046 
0047     for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
0048         sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
0049                         lockdep_is_held(&mvm->mutex));
0050         if (!sta || IS_ERR(sta) || !sta->tdls)
0051             continue;
0052 
0053         if (vif) {
0054             mvmsta = iwl_mvm_sta_from_mac80211(sta);
0055             if (mvmsta->vif != vif)
0056                 continue;
0057         }
0058 
0059         count++;
0060     }
0061 
0062     return count;
0063 }
0064 
0065 static void iwl_mvm_tdls_config(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
0066 {
0067     struct iwl_rx_packet *pkt;
0068     struct iwl_tdls_config_res *resp;
0069     struct iwl_tdls_config_cmd tdls_cfg_cmd = {};
0070     struct iwl_host_cmd cmd = {
0071         .id = TDLS_CONFIG_CMD,
0072         .flags = CMD_WANT_SKB,
0073         .data = { &tdls_cfg_cmd, },
0074         .len = { sizeof(struct iwl_tdls_config_cmd), },
0075     };
0076     struct ieee80211_sta *sta;
0077     int ret, i, cnt;
0078     struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
0079 
0080     lockdep_assert_held(&mvm->mutex);
0081 
0082     tdls_cfg_cmd.id_and_color =
0083         cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
0084     tdls_cfg_cmd.tx_to_ap_tid = IWL_MVM_TDLS_FW_TID;
0085     tdls_cfg_cmd.tx_to_ap_ssn = cpu_to_le16(0); /* not used for now */
0086 
0087     /* for now the Tx cmd is empty and unused */
0088 
0089     /* populate TDLS peer data */
0090     cnt = 0;
0091     for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
0092         sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
0093                         lockdep_is_held(&mvm->mutex));
0094         if (IS_ERR_OR_NULL(sta) || !sta->tdls)
0095             continue;
0096 
0097         tdls_cfg_cmd.sta_info[cnt].sta_id = i;
0098         tdls_cfg_cmd.sta_info[cnt].tx_to_peer_tid =
0099                             IWL_MVM_TDLS_FW_TID;
0100         tdls_cfg_cmd.sta_info[cnt].tx_to_peer_ssn = cpu_to_le16(0);
0101         tdls_cfg_cmd.sta_info[cnt].is_initiator =
0102                 cpu_to_le32(sta->tdls_initiator ? 1 : 0);
0103 
0104         cnt++;
0105     }
0106 
0107     tdls_cfg_cmd.tdls_peer_count = cnt;
0108     IWL_DEBUG_TDLS(mvm, "send TDLS config to FW for %d peers\n", cnt);
0109 
0110     ret = iwl_mvm_send_cmd(mvm, &cmd);
0111     if (WARN_ON_ONCE(ret))
0112         return;
0113 
0114     pkt = cmd.resp_pkt;
0115 
0116     WARN_ON_ONCE(iwl_rx_packet_payload_len(pkt) != sizeof(*resp));
0117 
0118     /* we don't really care about the response at this point */
0119 
0120     iwl_free_resp(&cmd);
0121 }
0122 
0123 void iwl_mvm_recalc_tdls_state(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
0124                    bool sta_added)
0125 {
0126     int tdls_sta_cnt = iwl_mvm_tdls_sta_count(mvm, vif);
0127 
0128     /* when the first peer joins, send a power update first */
0129     if (tdls_sta_cnt == 1 && sta_added)
0130         iwl_mvm_power_update_mac(mvm);
0131 
0132     /* Configure the FW with TDLS peer info only if TDLS channel switch
0133      * capability is set.
0134      * TDLS config data is used currently only in TDLS channel switch code.
0135      * Supposed to serve also TDLS buffer station which is not implemneted
0136      * yet in FW*/
0137     if (fw_has_capa(&mvm->fw->ucode_capa,
0138             IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH))
0139         iwl_mvm_tdls_config(mvm, vif);
0140 
0141     /* when the last peer leaves, send a power update last */
0142     if (tdls_sta_cnt == 0 && !sta_added)
0143         iwl_mvm_power_update_mac(mvm);
0144 }
0145 
0146 void iwl_mvm_mac_mgd_protect_tdls_discover(struct ieee80211_hw *hw,
0147                        struct ieee80211_vif *vif)
0148 {
0149     struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
0150     u32 duration = 2 * vif->bss_conf.dtim_period * vif->bss_conf.beacon_int;
0151 
0152     /* Protect the session to hear the TDLS setup response on the channel */
0153     mutex_lock(&mvm->mutex);
0154     if (fw_has_capa(&mvm->fw->ucode_capa,
0155             IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
0156         iwl_mvm_schedule_session_protection(mvm, vif, duration,
0157                             duration, true);
0158     else
0159         iwl_mvm_protect_session(mvm, vif, duration,
0160                     duration, 100, true);
0161     mutex_unlock(&mvm->mutex);
0162 }
0163 
0164 static const char *
0165 iwl_mvm_tdls_cs_state_str(enum iwl_mvm_tdls_cs_state state)
0166 {
0167     switch (state) {
0168     case IWL_MVM_TDLS_SW_IDLE:
0169         return "IDLE";
0170     case IWL_MVM_TDLS_SW_REQ_SENT:
0171         return "REQ SENT";
0172     case IWL_MVM_TDLS_SW_RESP_RCVD:
0173         return "RESP RECEIVED";
0174     case IWL_MVM_TDLS_SW_REQ_RCVD:
0175         return "REQ RECEIVED";
0176     case IWL_MVM_TDLS_SW_ACTIVE:
0177         return "ACTIVE";
0178     }
0179 
0180     return NULL;
0181 }
0182 
0183 static void iwl_mvm_tdls_update_cs_state(struct iwl_mvm *mvm,
0184                      enum iwl_mvm_tdls_cs_state state)
0185 {
0186     if (mvm->tdls_cs.state == state)
0187         return;
0188 
0189     IWL_DEBUG_TDLS(mvm, "TDLS channel switch state: %s -> %s\n",
0190                iwl_mvm_tdls_cs_state_str(mvm->tdls_cs.state),
0191                iwl_mvm_tdls_cs_state_str(state));
0192     mvm->tdls_cs.state = state;
0193 
0194     /* we only send requests to our switching peer - update sent time */
0195     if (state == IWL_MVM_TDLS_SW_REQ_SENT)
0196         mvm->tdls_cs.peer.sent_timestamp = iwl_mvm_get_systime(mvm);
0197 
0198     if (state == IWL_MVM_TDLS_SW_IDLE)
0199         mvm->tdls_cs.cur_sta_id = IWL_MVM_INVALID_STA;
0200 }
0201 
0202 void iwl_mvm_rx_tdls_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
0203 {
0204     struct iwl_rx_packet *pkt = rxb_addr(rxb);
0205     struct iwl_tdls_channel_switch_notif *notif = (void *)pkt->data;
0206     struct ieee80211_sta *sta;
0207     unsigned int delay;
0208     struct iwl_mvm_sta *mvmsta;
0209     struct ieee80211_vif *vif;
0210     u32 sta_id = le32_to_cpu(notif->sta_id);
0211 
0212     lockdep_assert_held(&mvm->mutex);
0213 
0214     /* can fail sometimes */
0215     if (!le32_to_cpu(notif->status)) {
0216         iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
0217         return;
0218     }
0219 
0220     if (WARN_ON(sta_id >= mvm->fw->ucode_capa.num_stations))
0221         return;
0222 
0223     sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
0224                     lockdep_is_held(&mvm->mutex));
0225     /* the station may not be here, but if it is, it must be a TDLS peer */
0226     if (IS_ERR_OR_NULL(sta) || WARN_ON(!sta->tdls))
0227         return;
0228 
0229     mvmsta = iwl_mvm_sta_from_mac80211(sta);
0230     vif = mvmsta->vif;
0231 
0232     /*
0233      * Update state and possibly switch again after this is over (DTIM).
0234      * Also convert TU to msec.
0235      */
0236     delay = TU_TO_MS(vif->bss_conf.dtim_period * vif->bss_conf.beacon_int);
0237     mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
0238              msecs_to_jiffies(delay));
0239 
0240     iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_ACTIVE);
0241 }
0242 
0243 static int
0244 iwl_mvm_tdls_check_action(struct iwl_mvm *mvm,
0245               enum iwl_tdls_channel_switch_type type,
0246               const u8 *peer, bool peer_initiator, u32 timestamp)
0247 {
0248     bool same_peer = false;
0249     int ret = 0;
0250 
0251     /* get the existing peer if it's there */
0252     if (mvm->tdls_cs.state != IWL_MVM_TDLS_SW_IDLE &&
0253         mvm->tdls_cs.cur_sta_id != IWL_MVM_INVALID_STA) {
0254         struct ieee80211_sta *sta = rcu_dereference_protected(
0255                 mvm->fw_id_to_mac_id[mvm->tdls_cs.cur_sta_id],
0256                 lockdep_is_held(&mvm->mutex));
0257         if (!IS_ERR_OR_NULL(sta))
0258             same_peer = ether_addr_equal(peer, sta->addr);
0259     }
0260 
0261     switch (mvm->tdls_cs.state) {
0262     case IWL_MVM_TDLS_SW_IDLE:
0263         /*
0264          * might be spurious packet from the peer after the switch is
0265          * already done
0266          */
0267         if (type == TDLS_MOVE_CH)
0268             ret = -EINVAL;
0269         break;
0270     case IWL_MVM_TDLS_SW_REQ_SENT:
0271         /* only allow requests from the same peer */
0272         if (!same_peer)
0273             ret = -EBUSY;
0274         else if (type == TDLS_SEND_CHAN_SW_RESP_AND_MOVE_CH &&
0275              !peer_initiator)
0276             /*
0277              * We received a ch-switch request while an outgoing
0278              * one is pending. Allow it if the peer is the link
0279              * initiator.
0280              */
0281             ret = -EBUSY;
0282         else if (type == TDLS_SEND_CHAN_SW_REQ)
0283             /* wait for idle before sending another request */
0284             ret = -EBUSY;
0285         else if (timestamp <= mvm->tdls_cs.peer.sent_timestamp)
0286             /* we got a stale response - ignore it */
0287             ret = -EINVAL;
0288         break;
0289     case IWL_MVM_TDLS_SW_RESP_RCVD:
0290         /*
0291          * we are waiting for the FW to give an "active" notification,
0292          * so ignore requests in the meantime
0293          */
0294         ret = -EBUSY;
0295         break;
0296     case IWL_MVM_TDLS_SW_REQ_RCVD:
0297         /* as above, allow the link initiator to proceed */
0298         if (type == TDLS_SEND_CHAN_SW_REQ) {
0299             if (!same_peer)
0300                 ret = -EBUSY;
0301             else if (peer_initiator) /* they are the initiator */
0302                 ret = -EBUSY;
0303         } else if (type == TDLS_MOVE_CH) {
0304             ret = -EINVAL;
0305         }
0306         break;
0307     case IWL_MVM_TDLS_SW_ACTIVE:
0308         /*
0309          * the only valid request when active is a request to return
0310          * to the base channel by the current off-channel peer
0311          */
0312         if (type != TDLS_MOVE_CH || !same_peer)
0313             ret = -EBUSY;
0314         break;
0315     }
0316 
0317     if (ret)
0318         IWL_DEBUG_TDLS(mvm,
0319                    "Invalid TDLS action %d state %d peer %pM same_peer %d initiator %d\n",
0320                    type, mvm->tdls_cs.state, peer, same_peer,
0321                    peer_initiator);
0322 
0323     return ret;
0324 }
0325 
0326 static int
0327 iwl_mvm_tdls_config_channel_switch(struct iwl_mvm *mvm,
0328                    struct ieee80211_vif *vif,
0329                    enum iwl_tdls_channel_switch_type type,
0330                    const u8 *peer, bool peer_initiator,
0331                    u8 oper_class,
0332                    struct cfg80211_chan_def *chandef,
0333                    u32 timestamp, u16 switch_time,
0334                    u16 switch_timeout, struct sk_buff *skb,
0335                    u32 ch_sw_tm_ie)
0336 {
0337     struct ieee80211_sta *sta;
0338     struct iwl_mvm_sta *mvmsta;
0339     struct ieee80211_tx_info *info;
0340     struct ieee80211_hdr *hdr;
0341     struct iwl_tdls_channel_switch_cmd cmd = {0};
0342     struct iwl_tdls_channel_switch_cmd_tail *tail =
0343         iwl_mvm_chan_info_cmd_tail(mvm, &cmd.ci);
0344     u16 len = sizeof(cmd) - iwl_mvm_chan_info_padding(mvm);
0345     int ret;
0346 
0347     lockdep_assert_held(&mvm->mutex);
0348 
0349     ret = iwl_mvm_tdls_check_action(mvm, type, peer, peer_initiator,
0350                     timestamp);
0351     if (ret)
0352         return ret;
0353 
0354     if (!skb || WARN_ON(skb->len > IWL_TDLS_CH_SW_FRAME_MAX_SIZE)) {
0355         ret = -EINVAL;
0356         goto out;
0357     }
0358 
0359     cmd.switch_type = type;
0360     tail->timing.frame_timestamp = cpu_to_le32(timestamp);
0361     tail->timing.switch_time = cpu_to_le32(switch_time);
0362     tail->timing.switch_timeout = cpu_to_le32(switch_timeout);
0363 
0364     rcu_read_lock();
0365     sta = ieee80211_find_sta(vif, peer);
0366     if (!sta) {
0367         rcu_read_unlock();
0368         ret = -ENOENT;
0369         goto out;
0370     }
0371     mvmsta = iwl_mvm_sta_from_mac80211(sta);
0372     cmd.peer_sta_id = cpu_to_le32(mvmsta->sta_id);
0373 
0374     if (!chandef) {
0375         if (mvm->tdls_cs.state == IWL_MVM_TDLS_SW_REQ_SENT &&
0376             mvm->tdls_cs.peer.chandef.chan) {
0377             /* actually moving to the channel */
0378             chandef = &mvm->tdls_cs.peer.chandef;
0379         } else if (mvm->tdls_cs.state == IWL_MVM_TDLS_SW_ACTIVE &&
0380                type == TDLS_MOVE_CH) {
0381             /* we need to return to base channel */
0382             struct ieee80211_chanctx_conf *chanctx =
0383                     rcu_dereference(vif->bss_conf.chanctx_conf);
0384 
0385             if (WARN_ON_ONCE(!chanctx)) {
0386                 rcu_read_unlock();
0387                 goto out;
0388             }
0389 
0390             chandef = &chanctx->def;
0391         }
0392     }
0393 
0394     if (chandef)
0395         iwl_mvm_set_chan_info_chandef(mvm, &cmd.ci, chandef);
0396 
0397     /* keep quota calculation simple for now - 50% of DTIM for TDLS */
0398     tail->timing.max_offchan_duration =
0399             cpu_to_le32(TU_TO_US(vif->bss_conf.dtim_period *
0400                          vif->bss_conf.beacon_int) / 2);
0401 
0402     /* Switch time is the first element in the switch-timing IE. */
0403     tail->frame.switch_time_offset = cpu_to_le32(ch_sw_tm_ie + 2);
0404 
0405     info = IEEE80211_SKB_CB(skb);
0406     hdr = (void *)skb->data;
0407     if (info->control.hw_key) {
0408         if (info->control.hw_key->cipher != WLAN_CIPHER_SUITE_CCMP) {
0409             rcu_read_unlock();
0410             ret = -EINVAL;
0411             goto out;
0412         }
0413         iwl_mvm_set_tx_cmd_ccmp(info, &tail->frame.tx_cmd);
0414     }
0415 
0416     iwl_mvm_set_tx_cmd(mvm, skb, &tail->frame.tx_cmd, info,
0417                mvmsta->sta_id);
0418 
0419     iwl_mvm_set_tx_cmd_rate(mvm, &tail->frame.tx_cmd, info, sta,
0420                 hdr->frame_control);
0421     rcu_read_unlock();
0422 
0423     memcpy(tail->frame.data, skb->data, skb->len);
0424 
0425     ret = iwl_mvm_send_cmd_pdu(mvm, TDLS_CHANNEL_SWITCH_CMD, 0, len, &cmd);
0426     if (ret) {
0427         IWL_ERR(mvm, "Failed to send TDLS_CHANNEL_SWITCH cmd: %d\n",
0428             ret);
0429         goto out;
0430     }
0431 
0432     /* channel switch has started, update state */
0433     if (type != TDLS_MOVE_CH) {
0434         mvm->tdls_cs.cur_sta_id = mvmsta->sta_id;
0435         iwl_mvm_tdls_update_cs_state(mvm,
0436                          type == TDLS_SEND_CHAN_SW_REQ ?
0437                          IWL_MVM_TDLS_SW_REQ_SENT :
0438                          IWL_MVM_TDLS_SW_REQ_RCVD);
0439     } else {
0440         iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_RESP_RCVD);
0441     }
0442 
0443 out:
0444 
0445     /* channel switch failed - we are idle */
0446     if (ret)
0447         iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
0448 
0449     return ret;
0450 }
0451 
0452 void iwl_mvm_tdls_ch_switch_work(struct work_struct *work)
0453 {
0454     struct iwl_mvm *mvm;
0455     struct ieee80211_sta *sta;
0456     struct iwl_mvm_sta *mvmsta;
0457     struct ieee80211_vif *vif;
0458     unsigned int delay;
0459     int ret;
0460 
0461     mvm = container_of(work, struct iwl_mvm, tdls_cs.dwork.work);
0462     mutex_lock(&mvm->mutex);
0463 
0464     /* called after an active channel switch has finished or timed-out */
0465     iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
0466 
0467     /* station might be gone, in that case do nothing */
0468     if (mvm->tdls_cs.peer.sta_id == IWL_MVM_INVALID_STA)
0469         goto out;
0470 
0471     sta = rcu_dereference_protected(
0472                 mvm->fw_id_to_mac_id[mvm->tdls_cs.peer.sta_id],
0473                 lockdep_is_held(&mvm->mutex));
0474     /* the station may not be here, but if it is, it must be a TDLS peer */
0475     if (!sta || IS_ERR(sta) || WARN_ON(!sta->tdls))
0476         goto out;
0477 
0478     mvmsta = iwl_mvm_sta_from_mac80211(sta);
0479     vif = mvmsta->vif;
0480     ret = iwl_mvm_tdls_config_channel_switch(mvm, vif,
0481                          TDLS_SEND_CHAN_SW_REQ,
0482                          sta->addr,
0483                          mvm->tdls_cs.peer.initiator,
0484                          mvm->tdls_cs.peer.op_class,
0485                          &mvm->tdls_cs.peer.chandef,
0486                          0, 0, 0,
0487                          mvm->tdls_cs.peer.skb,
0488                          mvm->tdls_cs.peer.ch_sw_tm_ie);
0489     if (ret)
0490         IWL_ERR(mvm, "Not sending TDLS channel switch: %d\n", ret);
0491 
0492     /* retry after a DTIM if we failed sending now */
0493     delay = TU_TO_MS(vif->bss_conf.dtim_period * vif->bss_conf.beacon_int);
0494     schedule_delayed_work(&mvm->tdls_cs.dwork, msecs_to_jiffies(delay));
0495 out:
0496     mutex_unlock(&mvm->mutex);
0497 }
0498 
0499 int
0500 iwl_mvm_tdls_channel_switch(struct ieee80211_hw *hw,
0501                 struct ieee80211_vif *vif,
0502                 struct ieee80211_sta *sta, u8 oper_class,
0503                 struct cfg80211_chan_def *chandef,
0504                 struct sk_buff *tmpl_skb, u32 ch_sw_tm_ie)
0505 {
0506     struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
0507     struct iwl_mvm_sta *mvmsta;
0508     unsigned int delay;
0509     int ret;
0510 
0511     mutex_lock(&mvm->mutex);
0512 
0513     IWL_DEBUG_TDLS(mvm, "TDLS channel switch with %pM ch %d width %d\n",
0514                sta->addr, chandef->chan->center_freq, chandef->width);
0515 
0516     /* we only support a single peer for channel switching */
0517     if (mvm->tdls_cs.peer.sta_id != IWL_MVM_INVALID_STA) {
0518         IWL_DEBUG_TDLS(mvm,
0519                    "Existing peer. Can't start switch with %pM\n",
0520                    sta->addr);
0521         ret = -EBUSY;
0522         goto out;
0523     }
0524 
0525     ret = iwl_mvm_tdls_config_channel_switch(mvm, vif,
0526                          TDLS_SEND_CHAN_SW_REQ,
0527                          sta->addr, sta->tdls_initiator,
0528                          oper_class, chandef, 0, 0, 0,
0529                          tmpl_skb, ch_sw_tm_ie);
0530     if (ret)
0531         goto out;
0532 
0533     /*
0534      * Mark the peer as "in tdls switch" for this vif. We only allow a
0535      * single such peer per vif.
0536      */
0537     mvm->tdls_cs.peer.skb = skb_copy(tmpl_skb, GFP_KERNEL);
0538     if (!mvm->tdls_cs.peer.skb) {
0539         ret = -ENOMEM;
0540         goto out;
0541     }
0542 
0543     mvmsta = iwl_mvm_sta_from_mac80211(sta);
0544     mvm->tdls_cs.peer.sta_id = mvmsta->sta_id;
0545     mvm->tdls_cs.peer.chandef = *chandef;
0546     mvm->tdls_cs.peer.initiator = sta->tdls_initiator;
0547     mvm->tdls_cs.peer.op_class = oper_class;
0548     mvm->tdls_cs.peer.ch_sw_tm_ie = ch_sw_tm_ie;
0549 
0550     /*
0551      * Wait for 2 DTIM periods before attempting the next switch. The next
0552      * switch will be made sooner if the current one completes before that.
0553      */
0554     delay = 2 * TU_TO_MS(vif->bss_conf.dtim_period *
0555                  vif->bss_conf.beacon_int);
0556     mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
0557              msecs_to_jiffies(delay));
0558 
0559 out:
0560     mutex_unlock(&mvm->mutex);
0561     return ret;
0562 }
0563 
0564 void iwl_mvm_tdls_cancel_channel_switch(struct ieee80211_hw *hw,
0565                     struct ieee80211_vif *vif,
0566                     struct ieee80211_sta *sta)
0567 {
0568     struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
0569     struct ieee80211_sta *cur_sta;
0570     bool wait_for_phy = false;
0571 
0572     mutex_lock(&mvm->mutex);
0573 
0574     IWL_DEBUG_TDLS(mvm, "TDLS cancel channel switch with %pM\n", sta->addr);
0575 
0576     /* we only support a single peer for channel switching */
0577     if (mvm->tdls_cs.peer.sta_id == IWL_MVM_INVALID_STA) {
0578         IWL_DEBUG_TDLS(mvm, "No ch switch peer - %pM\n", sta->addr);
0579         goto out;
0580     }
0581 
0582     cur_sta = rcu_dereference_protected(
0583                 mvm->fw_id_to_mac_id[mvm->tdls_cs.peer.sta_id],
0584                 lockdep_is_held(&mvm->mutex));
0585     /* make sure it's the same peer */
0586     if (cur_sta != sta)
0587         goto out;
0588 
0589     /*
0590      * If we're currently in a switch because of the now canceled peer,
0591      * wait a DTIM here to make sure the phy is back on the base channel.
0592      * We can't otherwise force it.
0593      */
0594     if (mvm->tdls_cs.cur_sta_id == mvm->tdls_cs.peer.sta_id &&
0595         mvm->tdls_cs.state != IWL_MVM_TDLS_SW_IDLE)
0596         wait_for_phy = true;
0597 
0598     mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
0599     dev_kfree_skb(mvm->tdls_cs.peer.skb);
0600     mvm->tdls_cs.peer.skb = NULL;
0601 
0602 out:
0603     mutex_unlock(&mvm->mutex);
0604 
0605     /* make sure the phy is on the base channel */
0606     if (wait_for_phy)
0607         msleep(TU_TO_MS(vif->bss_conf.dtim_period *
0608                 vif->bss_conf.beacon_int));
0609 
0610     /* flush the channel switch state */
0611     flush_delayed_work(&mvm->tdls_cs.dwork);
0612 
0613     IWL_DEBUG_TDLS(mvm, "TDLS ending channel switch with %pM\n", sta->addr);
0614 }
0615 
0616 void
0617 iwl_mvm_tdls_recv_channel_switch(struct ieee80211_hw *hw,
0618                  struct ieee80211_vif *vif,
0619                  struct ieee80211_tdls_ch_sw_params *params)
0620 {
0621     struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
0622     enum iwl_tdls_channel_switch_type type;
0623     unsigned int delay;
0624     const char *action_str =
0625         params->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ?
0626         "REQ" : "RESP";
0627 
0628     mutex_lock(&mvm->mutex);
0629 
0630     IWL_DEBUG_TDLS(mvm,
0631                "Received TDLS ch switch action %s from %pM status %d\n",
0632                action_str, params->sta->addr, params->status);
0633 
0634     /*
0635      * we got a non-zero status from a peer we were switching to - move to
0636      * the idle state and retry again later
0637      */
0638     if (params->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE &&
0639         params->status != 0 &&
0640         mvm->tdls_cs.state == IWL_MVM_TDLS_SW_REQ_SENT &&
0641         mvm->tdls_cs.cur_sta_id != IWL_MVM_INVALID_STA) {
0642         struct ieee80211_sta *cur_sta;
0643 
0644         /* make sure it's the same peer */
0645         cur_sta = rcu_dereference_protected(
0646                 mvm->fw_id_to_mac_id[mvm->tdls_cs.cur_sta_id],
0647                 lockdep_is_held(&mvm->mutex));
0648         if (cur_sta == params->sta) {
0649             iwl_mvm_tdls_update_cs_state(mvm,
0650                              IWL_MVM_TDLS_SW_IDLE);
0651             goto retry;
0652         }
0653     }
0654 
0655     type = (params->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST) ?
0656            TDLS_SEND_CHAN_SW_RESP_AND_MOVE_CH : TDLS_MOVE_CH;
0657 
0658     iwl_mvm_tdls_config_channel_switch(mvm, vif, type, params->sta->addr,
0659                        params->sta->tdls_initiator, 0,
0660                        params->chandef, params->timestamp,
0661                        params->switch_time,
0662                        params->switch_timeout,
0663                        params->tmpl_skb,
0664                        params->ch_sw_tm_ie);
0665 
0666 retry:
0667     /* register a timeout in case we don't succeed in switching */
0668     delay = vif->bss_conf.dtim_period * vif->bss_conf.beacon_int *
0669         1024 / 1000;
0670     mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
0671              msecs_to_jiffies(delay));
0672     mutex_unlock(&mvm->mutex);
0673 }