Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /******************************************************************************
0003  *
0004  * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
0005  *
0006  * Portions of this file are derived from the ipw3945 project, as well
0007  * as portions of the ieee80211 subsystem header files.
0008  *****************************************************************************/
0009 #include <linux/etherdevice.h>
0010 #include <net/mac80211.h>
0011 #include "iwl-trans.h"
0012 #include "dev.h"
0013 #include "agn.h"
0014 
0015 const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
0016 
0017 static int iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
0018 {
0019     lockdep_assert_held(&priv->sta_lock);
0020 
0021     if (sta_id >= IWLAGN_STATION_COUNT) {
0022         IWL_ERR(priv, "invalid sta_id %u\n", sta_id);
0023         return -EINVAL;
0024     }
0025     if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
0026         IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u "
0027             "addr %pM\n",
0028             sta_id, priv->stations[sta_id].sta.sta.addr);
0029 
0030     if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
0031         IWL_DEBUG_ASSOC(priv,
0032                 "STA id %u addr %pM already present in uCode "
0033                 "(according to driver)\n",
0034                 sta_id, priv->stations[sta_id].sta.sta.addr);
0035     } else {
0036         priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
0037         IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
0038                 sta_id, priv->stations[sta_id].sta.sta.addr);
0039     }
0040     return 0;
0041 }
0042 
0043 static void iwl_process_add_sta_resp(struct iwl_priv *priv,
0044                      struct iwl_rx_packet *pkt)
0045 {
0046     struct iwl_add_sta_resp *add_sta_resp = (void *)pkt->data;
0047 
0048     IWL_DEBUG_INFO(priv, "Processing response for adding station\n");
0049 
0050     spin_lock_bh(&priv->sta_lock);
0051 
0052     switch (add_sta_resp->status) {
0053     case ADD_STA_SUCCESS_MSK:
0054         IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
0055         break;
0056     case ADD_STA_NO_ROOM_IN_TABLE:
0057         IWL_ERR(priv, "Adding station failed, no room in table.\n");
0058         break;
0059     case ADD_STA_NO_BLOCK_ACK_RESOURCE:
0060         IWL_ERR(priv,
0061             "Adding station failed, no block ack resource.\n");
0062         break;
0063     case ADD_STA_MODIFY_NON_EXIST_STA:
0064         IWL_ERR(priv, "Attempting to modify non-existing station\n");
0065         break;
0066     default:
0067         IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
0068                 add_sta_resp->status);
0069         break;
0070     }
0071 
0072     spin_unlock_bh(&priv->sta_lock);
0073 }
0074 
0075 void iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb)
0076 {
0077     struct iwl_rx_packet *pkt = rxb_addr(rxb);
0078 
0079     iwl_process_add_sta_resp(priv, pkt);
0080 }
0081 
0082 int iwl_send_add_sta(struct iwl_priv *priv,
0083              struct iwl_addsta_cmd *sta, u8 flags)
0084 {
0085     int ret = 0;
0086     struct iwl_host_cmd cmd = {
0087         .id = REPLY_ADD_STA,
0088         .flags = flags,
0089         .data = { sta, },
0090         .len = { sizeof(*sta), },
0091     };
0092     u8 sta_id __maybe_unused = sta->sta.sta_id;
0093     struct iwl_rx_packet *pkt;
0094     struct iwl_add_sta_resp *add_sta_resp;
0095 
0096     IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
0097                sta_id, sta->sta.addr, flags & CMD_ASYNC ?  "a" : "");
0098 
0099     if (!(flags & CMD_ASYNC)) {
0100         cmd.flags |= CMD_WANT_SKB;
0101         might_sleep();
0102     }
0103 
0104     ret = iwl_dvm_send_cmd(priv, &cmd);
0105 
0106     if (ret || (flags & CMD_ASYNC))
0107         return ret;
0108 
0109     pkt = cmd.resp_pkt;
0110     add_sta_resp = (void *)pkt->data;
0111 
0112     /* debug messages are printed in the handler */
0113     if (add_sta_resp->status == ADD_STA_SUCCESS_MSK) {
0114         spin_lock_bh(&priv->sta_lock);
0115         ret = iwl_sta_ucode_activate(priv, sta_id);
0116         spin_unlock_bh(&priv->sta_lock);
0117     } else {
0118         ret = -EIO;
0119     }
0120 
0121     iwl_free_resp(&cmd);
0122 
0123     return ret;
0124 }
0125 
0126 bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
0127                 struct iwl_rxon_context *ctx,
0128                 struct ieee80211_sta *sta)
0129 {
0130     if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
0131         return false;
0132 
0133 #ifdef CONFIG_IWLWIFI_DEBUGFS
0134     if (priv->disable_ht40)
0135         return false;
0136 #endif
0137 
0138     /* special case for RXON */
0139     if (!sta)
0140         return true;
0141 
0142     return sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40;
0143 }
0144 
0145 static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
0146                   struct ieee80211_sta *sta,
0147                   struct iwl_rxon_context *ctx,
0148                   __le32 *flags, __le32 *mask)
0149 {
0150     struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->deflink.ht_cap;
0151 
0152     *mask = STA_FLG_RTS_MIMO_PROT_MSK |
0153         STA_FLG_MIMO_DIS_MSK |
0154         STA_FLG_HT40_EN_MSK |
0155         STA_FLG_MAX_AGG_SIZE_MSK |
0156         STA_FLG_AGG_MPDU_DENSITY_MSK;
0157     *flags = 0;
0158 
0159     if (!sta || !sta_ht_inf->ht_supported)
0160         return;
0161 
0162     IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",
0163             sta->addr,
0164             (sta->smps_mode == IEEE80211_SMPS_STATIC) ?
0165             "static" :
0166             (sta->smps_mode == IEEE80211_SMPS_DYNAMIC) ?
0167             "dynamic" : "disabled");
0168 
0169     switch (sta->smps_mode) {
0170     case IEEE80211_SMPS_STATIC:
0171         *flags |= STA_FLG_MIMO_DIS_MSK;
0172         break;
0173     case IEEE80211_SMPS_DYNAMIC:
0174         *flags |= STA_FLG_RTS_MIMO_PROT_MSK;
0175         break;
0176     case IEEE80211_SMPS_OFF:
0177         break;
0178     default:
0179         IWL_WARN(priv, "Invalid MIMO PS mode %d\n", sta->smps_mode);
0180         break;
0181     }
0182 
0183     *flags |= cpu_to_le32(
0184         (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
0185 
0186     *flags |= cpu_to_le32(
0187         (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
0188 
0189     if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
0190         *flags |= STA_FLG_HT40_EN_MSK;
0191 }
0192 
0193 int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
0194               struct ieee80211_sta *sta)
0195 {
0196     u8 sta_id = iwl_sta_id(sta);
0197     __le32 flags, mask;
0198     struct iwl_addsta_cmd cmd;
0199 
0200     if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
0201         return -EINVAL;
0202 
0203     iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
0204 
0205     spin_lock_bh(&priv->sta_lock);
0206     priv->stations[sta_id].sta.station_flags &= ~mask;
0207     priv->stations[sta_id].sta.station_flags |= flags;
0208     spin_unlock_bh(&priv->sta_lock);
0209 
0210     memset(&cmd, 0, sizeof(cmd));
0211     cmd.mode = STA_CONTROL_MODIFY_MSK;
0212     cmd.station_flags_msk = mask;
0213     cmd.station_flags = flags;
0214     cmd.sta.sta_id = sta_id;
0215 
0216     return iwl_send_add_sta(priv, &cmd, 0);
0217 }
0218 
0219 static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
0220                    struct ieee80211_sta *sta,
0221                    struct iwl_rxon_context *ctx)
0222 {
0223     __le32 flags, mask;
0224 
0225     iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
0226 
0227     lockdep_assert_held(&priv->sta_lock);
0228     priv->stations[index].sta.station_flags &= ~mask;
0229     priv->stations[index].sta.station_flags |= flags;
0230 }
0231 
0232 /*
0233  * iwl_prep_station - Prepare station information for addition
0234  *
0235  * should be called with sta_lock held
0236  */
0237 u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
0238             const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
0239 {
0240     struct iwl_station_entry *station;
0241     int i;
0242     u8 sta_id = IWL_INVALID_STATION;
0243 
0244     if (is_ap)
0245         sta_id = ctx->ap_sta_id;
0246     else if (is_broadcast_ether_addr(addr))
0247         sta_id = ctx->bcast_sta_id;
0248     else
0249         for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
0250             if (ether_addr_equal(priv->stations[i].sta.sta.addr,
0251                          addr)) {
0252                 sta_id = i;
0253                 break;
0254             }
0255 
0256             if (!priv->stations[i].used &&
0257                 sta_id == IWL_INVALID_STATION)
0258                 sta_id = i;
0259         }
0260 
0261     /*
0262      * These two conditions have the same outcome, but keep them
0263      * separate
0264      */
0265     if (unlikely(sta_id == IWL_INVALID_STATION))
0266         return sta_id;
0267 
0268     /*
0269      * uCode is not able to deal with multiple requests to add a
0270      * station. Keep track if one is in progress so that we do not send
0271      * another.
0272      */
0273     if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
0274         IWL_DEBUG_INFO(priv, "STA %d already in process of being "
0275                    "added.\n", sta_id);
0276         return sta_id;
0277     }
0278 
0279     if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
0280         (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
0281         ether_addr_equal(priv->stations[sta_id].sta.sta.addr, addr)) {
0282         IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
0283                 "adding again.\n", sta_id, addr);
0284         return sta_id;
0285     }
0286 
0287     station = &priv->stations[sta_id];
0288     station->used = IWL_STA_DRIVER_ACTIVE;
0289     IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
0290             sta_id, addr);
0291     priv->num_stations++;
0292 
0293     /* Set up the REPLY_ADD_STA command to send to device */
0294     memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
0295     memcpy(station->sta.sta.addr, addr, ETH_ALEN);
0296     station->sta.mode = 0;
0297     station->sta.sta.sta_id = sta_id;
0298     station->sta.station_flags = ctx->station_flags;
0299     station->ctxid = ctx->ctxid;
0300 
0301     if (sta) {
0302         struct iwl_station_priv *sta_priv;
0303 
0304         sta_priv = (void *)sta->drv_priv;
0305         sta_priv->ctx = ctx;
0306     }
0307 
0308     /*
0309      * OK to call unconditionally, since local stations (IBSS BSSID
0310      * STA and broadcast STA) pass in a NULL sta, and mac80211
0311      * doesn't allow HT IBSS.
0312      */
0313     iwl_set_ht_add_station(priv, sta_id, sta, ctx);
0314 
0315     return sta_id;
0316 
0317 }
0318 
0319 #define STA_WAIT_TIMEOUT (HZ/2)
0320 
0321 /*
0322  * iwl_add_station_common -
0323  */
0324 int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
0325                const u8 *addr, bool is_ap,
0326                struct ieee80211_sta *sta, u8 *sta_id_r)
0327 {
0328     int ret = 0;
0329     u8 sta_id;
0330     struct iwl_addsta_cmd sta_cmd;
0331 
0332     *sta_id_r = 0;
0333     spin_lock_bh(&priv->sta_lock);
0334     sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
0335     if (sta_id == IWL_INVALID_STATION) {
0336         IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
0337             addr);
0338         spin_unlock_bh(&priv->sta_lock);
0339         return -EINVAL;
0340     }
0341 
0342     /*
0343      * uCode is not able to deal with multiple requests to add a
0344      * station. Keep track if one is in progress so that we do not send
0345      * another.
0346      */
0347     if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
0348         IWL_DEBUG_INFO(priv, "STA %d already in process of being "
0349                    "added.\n", sta_id);
0350         spin_unlock_bh(&priv->sta_lock);
0351         return -EEXIST;
0352     }
0353 
0354     if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
0355         (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
0356         IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
0357                 "adding again.\n", sta_id, addr);
0358         spin_unlock_bh(&priv->sta_lock);
0359         return -EEXIST;
0360     }
0361 
0362     priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
0363     memcpy(&sta_cmd, &priv->stations[sta_id].sta,
0364            sizeof(struct iwl_addsta_cmd));
0365     spin_unlock_bh(&priv->sta_lock);
0366 
0367     /* Add station to device's station table */
0368     ret = iwl_send_add_sta(priv, &sta_cmd, 0);
0369     if (ret) {
0370         spin_lock_bh(&priv->sta_lock);
0371         IWL_ERR(priv, "Adding station %pM failed.\n",
0372             priv->stations[sta_id].sta.sta.addr);
0373         priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
0374         priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
0375         spin_unlock_bh(&priv->sta_lock);
0376     }
0377     *sta_id_r = sta_id;
0378     return ret;
0379 }
0380 
0381 /*
0382  * iwl_sta_ucode_deactivate - deactivate ucode status for a station
0383  */
0384 static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
0385 {
0386     lockdep_assert_held(&priv->sta_lock);
0387 
0388     /* Ucode must be active and driver must be non active */
0389     if ((priv->stations[sta_id].used &
0390          (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
0391           IWL_STA_UCODE_ACTIVE)
0392         IWL_ERR(priv, "removed non active STA %u\n", sta_id);
0393 
0394     priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
0395 
0396     memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
0397     IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
0398 }
0399 
0400 static int iwl_send_remove_station(struct iwl_priv *priv,
0401                    const u8 *addr, int sta_id,
0402                    bool temporary)
0403 {
0404     struct iwl_rx_packet *pkt;
0405     int ret;
0406     struct iwl_rem_sta_cmd rm_sta_cmd;
0407     struct iwl_rem_sta_resp *rem_sta_resp;
0408 
0409     struct iwl_host_cmd cmd = {
0410         .id = REPLY_REMOVE_STA,
0411         .len = { sizeof(struct iwl_rem_sta_cmd), },
0412         .data = { &rm_sta_cmd, },
0413     };
0414 
0415     memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
0416     rm_sta_cmd.num_sta = 1;
0417     memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
0418 
0419     cmd.flags |= CMD_WANT_SKB;
0420 
0421     ret = iwl_dvm_send_cmd(priv, &cmd);
0422 
0423     if (ret)
0424         return ret;
0425 
0426     pkt = cmd.resp_pkt;
0427     rem_sta_resp = (void *)pkt->data;
0428 
0429     switch (rem_sta_resp->status) {
0430     case REM_STA_SUCCESS_MSK:
0431         if (!temporary) {
0432             spin_lock_bh(&priv->sta_lock);
0433             iwl_sta_ucode_deactivate(priv, sta_id);
0434             spin_unlock_bh(&priv->sta_lock);
0435         }
0436         IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
0437         break;
0438     default:
0439         ret = -EIO;
0440         IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
0441         break;
0442     }
0443 
0444     iwl_free_resp(&cmd);
0445 
0446     return ret;
0447 }
0448 
0449 /*
0450  * iwl_remove_station - Remove driver's knowledge of station.
0451  */
0452 int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
0453                const u8 *addr)
0454 {
0455     u8 tid;
0456 
0457     if (!iwl_is_ready(priv)) {
0458         IWL_DEBUG_INFO(priv,
0459             "Unable to remove station %pM, device not ready.\n",
0460             addr);
0461         /*
0462          * It is typical for stations to be removed when we are
0463          * going down. Return success since device will be down
0464          * soon anyway
0465          */
0466         return 0;
0467     }
0468 
0469     IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d  %pM\n",
0470             sta_id, addr);
0471 
0472     if (WARN_ON(sta_id == IWL_INVALID_STATION))
0473         return -EINVAL;
0474 
0475     spin_lock_bh(&priv->sta_lock);
0476 
0477     if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
0478         IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
0479                 addr);
0480         goto out_err;
0481     }
0482 
0483     if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
0484         IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
0485                 addr);
0486         goto out_err;
0487     }
0488 
0489     if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
0490         kfree(priv->stations[sta_id].lq);
0491         priv->stations[sta_id].lq = NULL;
0492     }
0493 
0494     for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
0495         memset(&priv->tid_data[sta_id][tid], 0,
0496             sizeof(priv->tid_data[sta_id][tid]));
0497 
0498     priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
0499 
0500     priv->num_stations--;
0501 
0502     if (WARN_ON(priv->num_stations < 0))
0503         priv->num_stations = 0;
0504 
0505     spin_unlock_bh(&priv->sta_lock);
0506 
0507     return iwl_send_remove_station(priv, addr, sta_id, false);
0508 out_err:
0509     spin_unlock_bh(&priv->sta_lock);
0510     return -EINVAL;
0511 }
0512 
0513 void iwl_deactivate_station(struct iwl_priv *priv, const u8 sta_id,
0514                 const u8 *addr)
0515 {
0516     u8 tid;
0517 
0518     if (!iwl_is_ready(priv)) {
0519         IWL_DEBUG_INFO(priv,
0520             "Unable to remove station %pM, device not ready.\n",
0521             addr);
0522         return;
0523     }
0524 
0525     IWL_DEBUG_ASSOC(priv, "Deactivating STA: %pM (%d)\n", addr, sta_id);
0526 
0527     if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
0528         return;
0529 
0530     spin_lock_bh(&priv->sta_lock);
0531 
0532     WARN_ON_ONCE(!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE));
0533 
0534     for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
0535         memset(&priv->tid_data[sta_id][tid], 0,
0536             sizeof(priv->tid_data[sta_id][tid]));
0537 
0538     priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
0539     priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
0540 
0541     priv->num_stations--;
0542 
0543     if (WARN_ON_ONCE(priv->num_stations < 0))
0544         priv->num_stations = 0;
0545 
0546     spin_unlock_bh(&priv->sta_lock);
0547 }
0548 
0549 static void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
0550                 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
0551 {
0552     int i, r;
0553     u32 rate_flags = 0;
0554     __le32 rate_n_flags;
0555 
0556     lockdep_assert_held(&priv->mutex);
0557 
0558     memset(link_cmd, 0, sizeof(*link_cmd));
0559 
0560     /* Set up the rate scaling to start at selected rate, fall back
0561      * all the way down to 1M in IEEE order, and then spin on 1M */
0562     if (priv->band == NL80211_BAND_5GHZ)
0563         r = IWL_RATE_6M_INDEX;
0564     else if (ctx && ctx->vif && ctx->vif->p2p)
0565         r = IWL_RATE_6M_INDEX;
0566     else
0567         r = IWL_RATE_1M_INDEX;
0568 
0569     if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
0570         rate_flags |= RATE_MCS_CCK_MSK;
0571 
0572     rate_flags |= first_antenna(priv->nvm_data->valid_tx_ant) <<
0573                 RATE_MCS_ANT_POS;
0574     rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
0575     for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
0576         link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
0577 
0578     link_cmd->general_params.single_stream_ant_msk =
0579             first_antenna(priv->nvm_data->valid_tx_ant);
0580 
0581     link_cmd->general_params.dual_stream_ant_msk =
0582         priv->nvm_data->valid_tx_ant &
0583         ~first_antenna(priv->nvm_data->valid_tx_ant);
0584     if (!link_cmd->general_params.dual_stream_ant_msk) {
0585         link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
0586     } else if (num_of_ant(priv->nvm_data->valid_tx_ant) == 2) {
0587         link_cmd->general_params.dual_stream_ant_msk =
0588             priv->nvm_data->valid_tx_ant;
0589     }
0590 
0591     link_cmd->agg_params.agg_dis_start_th =
0592         LINK_QUAL_AGG_DISABLE_START_DEF;
0593     link_cmd->agg_params.agg_time_limit =
0594         cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
0595 
0596     link_cmd->sta_id = sta_id;
0597 }
0598 
0599 /*
0600  * iwl_clear_ucode_stations - clear ucode station table bits
0601  *
0602  * This function clears all the bits in the driver indicating
0603  * which stations are active in the ucode. Call when something
0604  * other than explicit station management would cause this in
0605  * the ucode, e.g. unassociated RXON.
0606  */
0607 void iwl_clear_ucode_stations(struct iwl_priv *priv,
0608                   struct iwl_rxon_context *ctx)
0609 {
0610     int i;
0611     bool cleared = false;
0612 
0613     IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
0614 
0615     spin_lock_bh(&priv->sta_lock);
0616     for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
0617         if (ctx && ctx->ctxid != priv->stations[i].ctxid)
0618             continue;
0619 
0620         if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
0621             IWL_DEBUG_INFO(priv,
0622                 "Clearing ucode active for station %d\n", i);
0623             priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
0624             cleared = true;
0625         }
0626     }
0627     spin_unlock_bh(&priv->sta_lock);
0628 
0629     if (!cleared)
0630         IWL_DEBUG_INFO(priv,
0631                    "No active stations found to be cleared\n");
0632 }
0633 
0634 /*
0635  * iwl_restore_stations() - Restore driver known stations to device
0636  *
0637  * All stations considered active by driver, but not present in ucode, is
0638  * restored.
0639  *
0640  * Function sleeps.
0641  */
0642 void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
0643 {
0644     struct iwl_addsta_cmd sta_cmd;
0645     static const struct iwl_link_quality_cmd zero_lq = {};
0646     struct iwl_link_quality_cmd lq;
0647     int i;
0648     bool found = false;
0649     int ret;
0650     bool send_lq;
0651 
0652     if (!iwl_is_ready(priv)) {
0653         IWL_DEBUG_INFO(priv,
0654                    "Not ready yet, not restoring any stations.\n");
0655         return;
0656     }
0657 
0658     IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
0659     spin_lock_bh(&priv->sta_lock);
0660     for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
0661         if (ctx->ctxid != priv->stations[i].ctxid)
0662             continue;
0663         if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
0664                 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
0665             IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
0666                     priv->stations[i].sta.sta.addr);
0667             priv->stations[i].sta.mode = 0;
0668             priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
0669             found = true;
0670         }
0671     }
0672 
0673     for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
0674         if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
0675             memcpy(&sta_cmd, &priv->stations[i].sta,
0676                    sizeof(struct iwl_addsta_cmd));
0677             send_lq = false;
0678             if (priv->stations[i].lq) {
0679                 if (priv->wowlan)
0680                     iwl_sta_fill_lq(priv, ctx, i, &lq);
0681                 else
0682                     memcpy(&lq, priv->stations[i].lq,
0683                            sizeof(struct iwl_link_quality_cmd));
0684 
0685                 if (memcmp(&lq, &zero_lq, sizeof(lq)))
0686                     send_lq = true;
0687             }
0688             spin_unlock_bh(&priv->sta_lock);
0689             ret = iwl_send_add_sta(priv, &sta_cmd, 0);
0690             if (ret) {
0691                 spin_lock_bh(&priv->sta_lock);
0692                 IWL_ERR(priv, "Adding station %pM failed.\n",
0693                     priv->stations[i].sta.sta.addr);
0694                 priv->stations[i].used &=
0695                         ~IWL_STA_DRIVER_ACTIVE;
0696                 priv->stations[i].used &=
0697                         ~IWL_STA_UCODE_INPROGRESS;
0698                 continue;
0699             }
0700             /*
0701              * Rate scaling has already been initialized, send
0702              * current LQ command
0703              */
0704             if (send_lq)
0705                 iwl_send_lq_cmd(priv, ctx, &lq, 0, true);
0706             spin_lock_bh(&priv->sta_lock);
0707             priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
0708         }
0709     }
0710 
0711     spin_unlock_bh(&priv->sta_lock);
0712     if (!found)
0713         IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
0714             "no stations to be restored.\n");
0715     else
0716         IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
0717             "complete.\n");
0718 }
0719 
0720 int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
0721 {
0722     int i;
0723 
0724     for (i = 0; i < priv->sta_key_max_num; i++)
0725         if (!test_and_set_bit(i, &priv->ucode_key_table))
0726             return i;
0727 
0728     return WEP_INVALID_OFFSET;
0729 }
0730 
0731 void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
0732 {
0733     int i;
0734 
0735     spin_lock_bh(&priv->sta_lock);
0736     for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
0737         if (!(priv->stations[i].used & IWL_STA_BCAST))
0738             continue;
0739 
0740         priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
0741         priv->num_stations--;
0742         if (WARN_ON(priv->num_stations < 0))
0743             priv->num_stations = 0;
0744         kfree(priv->stations[i].lq);
0745         priv->stations[i].lq = NULL;
0746     }
0747     spin_unlock_bh(&priv->sta_lock);
0748 }
0749 
0750 #ifdef CONFIG_IWLWIFI_DEBUG
0751 static void iwl_dump_lq_cmd(struct iwl_priv *priv,
0752                struct iwl_link_quality_cmd *lq)
0753 {
0754     int i;
0755     IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
0756     IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
0757                lq->general_params.single_stream_ant_msk,
0758                lq->general_params.dual_stream_ant_msk);
0759 
0760     for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
0761         IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
0762                    i, lq->rs_table[i].rate_n_flags);
0763 }
0764 #else
0765 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
0766                    struct iwl_link_quality_cmd *lq)
0767 {
0768 }
0769 #endif
0770 
0771 /*
0772  * is_lq_table_valid() - Test one aspect of LQ cmd for validity
0773  *
0774  * It sometimes happens when a HT rate has been in use and we
0775  * loose connectivity with AP then mac80211 will first tell us that the
0776  * current channel is not HT anymore before removing the station. In such a
0777  * scenario the RXON flags will be updated to indicate we are not
0778  * communicating HT anymore, but the LQ command may still contain HT rates.
0779  * Test for this to prevent driver from sending LQ command between the time
0780  * RXON flags are updated and when LQ command is updated.
0781  */
0782 static bool is_lq_table_valid(struct iwl_priv *priv,
0783                   struct iwl_rxon_context *ctx,
0784                   struct iwl_link_quality_cmd *lq)
0785 {
0786     int i;
0787 
0788     if (ctx->ht.enabled)
0789         return true;
0790 
0791     IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
0792                ctx->active.channel);
0793     for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
0794         if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
0795             RATE_MCS_HT_MSK) {
0796             IWL_DEBUG_INFO(priv,
0797                        "index %d of LQ expects HT channel\n",
0798                        i);
0799             return false;
0800         }
0801     }
0802     return true;
0803 }
0804 
0805 /*
0806  * iwl_send_lq_cmd() - Send link quality command
0807  * @init: This command is sent as part of station initialization right
0808  *        after station has been added.
0809  *
0810  * The link quality command is sent as the last step of station creation.
0811  * This is the special case in which init is set and we call a callback in
0812  * this case to clear the state indicating that station creation is in
0813  * progress.
0814  */
0815 int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
0816             struct iwl_link_quality_cmd *lq, u8 flags, bool init)
0817 {
0818     int ret = 0;
0819     struct iwl_host_cmd cmd = {
0820         .id = REPLY_TX_LINK_QUALITY_CMD,
0821         .len = { sizeof(struct iwl_link_quality_cmd), },
0822         .flags = flags,
0823         .data = { lq, },
0824     };
0825 
0826     if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
0827         return -EINVAL;
0828 
0829 
0830     spin_lock_bh(&priv->sta_lock);
0831     if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
0832         spin_unlock_bh(&priv->sta_lock);
0833         return -EINVAL;
0834     }
0835     spin_unlock_bh(&priv->sta_lock);
0836 
0837     iwl_dump_lq_cmd(priv, lq);
0838     if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
0839         return -EINVAL;
0840 
0841     if (is_lq_table_valid(priv, ctx, lq))
0842         ret = iwl_dvm_send_cmd(priv, &cmd);
0843     else
0844         ret = -EINVAL;
0845 
0846     if (cmd.flags & CMD_ASYNC)
0847         return ret;
0848 
0849     if (init) {
0850         IWL_DEBUG_INFO(priv, "init LQ command complete, "
0851                    "clearing sta addition status for sta %d\n",
0852                    lq->sta_id);
0853         spin_lock_bh(&priv->sta_lock);
0854         priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
0855         spin_unlock_bh(&priv->sta_lock);
0856     }
0857     return ret;
0858 }
0859 
0860 
0861 static struct iwl_link_quality_cmd *
0862 iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
0863          u8 sta_id)
0864 {
0865     struct iwl_link_quality_cmd *link_cmd;
0866 
0867     link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
0868     if (!link_cmd) {
0869         IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
0870         return NULL;
0871     }
0872 
0873     iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
0874 
0875     return link_cmd;
0876 }
0877 
0878 /*
0879  * iwlagn_add_bssid_station - Add the special IBSS BSSID station
0880  *
0881  * Function sleeps.
0882  */
0883 int iwlagn_add_bssid_station(struct iwl_priv *priv,
0884                  struct iwl_rxon_context *ctx,
0885                  const u8 *addr, u8 *sta_id_r)
0886 {
0887     int ret;
0888     u8 sta_id;
0889     struct iwl_link_quality_cmd *link_cmd;
0890 
0891     if (sta_id_r)
0892         *sta_id_r = IWL_INVALID_STATION;
0893 
0894     ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
0895     if (ret) {
0896         IWL_ERR(priv, "Unable to add station %pM\n", addr);
0897         return ret;
0898     }
0899 
0900     if (sta_id_r)
0901         *sta_id_r = sta_id;
0902 
0903     spin_lock_bh(&priv->sta_lock);
0904     priv->stations[sta_id].used |= IWL_STA_LOCAL;
0905     spin_unlock_bh(&priv->sta_lock);
0906 
0907     /* Set up default rate scaling table in device's station table */
0908     link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
0909     if (!link_cmd) {
0910         IWL_ERR(priv,
0911             "Unable to initialize rate scaling for station %pM.\n",
0912             addr);
0913         return -ENOMEM;
0914     }
0915 
0916     ret = iwl_send_lq_cmd(priv, ctx, link_cmd, 0, true);
0917     if (ret)
0918         IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
0919 
0920     spin_lock_bh(&priv->sta_lock);
0921     priv->stations[sta_id].lq = link_cmd;
0922     spin_unlock_bh(&priv->sta_lock);
0923 
0924     return 0;
0925 }
0926 
0927 /*
0928  * static WEP keys
0929  *
0930  * For each context, the device has a table of 4 static WEP keys
0931  * (one for each key index) that is updated with the following
0932  * commands.
0933  */
0934 
0935 static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
0936                       struct iwl_rxon_context *ctx,
0937                       bool send_if_empty)
0938 {
0939     int i, not_empty = 0;
0940     u8 buff[sizeof(struct iwl_wep_cmd) +
0941         sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
0942     struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
0943     size_t cmd_size  = sizeof(struct iwl_wep_cmd);
0944     struct iwl_host_cmd cmd = {
0945         .id = ctx->wep_key_cmd,
0946         .data = { wep_cmd, },
0947     };
0948 
0949     might_sleep();
0950 
0951     memset(wep_cmd, 0, cmd_size +
0952             (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
0953 
0954     for (i = 0; i < WEP_KEYS_MAX ; i++) {
0955         wep_cmd->key[i].key_index = i;
0956         if (ctx->wep_keys[i].key_size) {
0957             wep_cmd->key[i].key_offset = i;
0958             not_empty = 1;
0959         } else {
0960             wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
0961         }
0962 
0963         wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
0964         memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
0965                 ctx->wep_keys[i].key_size);
0966     }
0967 
0968     wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
0969     wep_cmd->num_keys = WEP_KEYS_MAX;
0970 
0971     cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
0972 
0973     cmd.len[0] = cmd_size;
0974 
0975     if (not_empty || send_if_empty)
0976         return iwl_dvm_send_cmd(priv, &cmd);
0977     else
0978         return 0;
0979 }
0980 
0981 int iwl_restore_default_wep_keys(struct iwl_priv *priv,
0982                  struct iwl_rxon_context *ctx)
0983 {
0984     lockdep_assert_held(&priv->mutex);
0985 
0986     return iwl_send_static_wepkey_cmd(priv, ctx, false);
0987 }
0988 
0989 int iwl_remove_default_wep_key(struct iwl_priv *priv,
0990                    struct iwl_rxon_context *ctx,
0991                    struct ieee80211_key_conf *keyconf)
0992 {
0993     int ret;
0994 
0995     lockdep_assert_held(&priv->mutex);
0996 
0997     IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
0998               keyconf->keyidx);
0999 
1000     memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
1001     if (iwl_is_rfkill(priv)) {
1002         IWL_DEBUG_WEP(priv,
1003             "Not sending REPLY_WEPKEY command due to RFKILL.\n");
1004         /* but keys in device are clear anyway so return success */
1005         return 0;
1006     }
1007     ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1008     IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1009               keyconf->keyidx, ret);
1010 
1011     return ret;
1012 }
1013 
1014 int iwl_set_default_wep_key(struct iwl_priv *priv,
1015                 struct iwl_rxon_context *ctx,
1016                 struct ieee80211_key_conf *keyconf)
1017 {
1018     int ret;
1019 
1020     lockdep_assert_held(&priv->mutex);
1021 
1022     if (keyconf->keylen != WEP_KEY_LEN_128 &&
1023         keyconf->keylen != WEP_KEY_LEN_64) {
1024         IWL_DEBUG_WEP(priv,
1025                   "Bad WEP key length %d\n", keyconf->keylen);
1026         return -EINVAL;
1027     }
1028 
1029     keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
1030 
1031     ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1032     memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1033                             keyconf->keylen);
1034 
1035     ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1036     IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1037         keyconf->keylen, keyconf->keyidx, ret);
1038 
1039     return ret;
1040 }
1041 
1042 /*
1043  * dynamic (per-station) keys
1044  *
1045  * The dynamic keys are a little more complicated. The device has
1046  * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1047  * These are linked to stations by a table that contains an index
1048  * into the key table for each station/key index/{mcast,unicast},
1049  * i.e. it's basically an array of pointers like this:
1050  *  key_offset_t key_mapping[NUM_STATIONS][4][2];
1051  * (it really works differently, but you can think of it as such)
1052  *
1053  * The key uploading and linking happens in the same command, the
1054  * add station command with STA_MODIFY_KEY_MASK.
1055  */
1056 
1057 static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1058                 struct ieee80211_vif *vif,
1059                 struct ieee80211_sta *sta)
1060 {
1061     struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1062 
1063     if (sta)
1064         return iwl_sta_id(sta);
1065 
1066     /*
1067      * The device expects GTKs for station interfaces to be
1068      * installed as GTKs for the AP station. If we have no
1069      * station ID, then use the ap_sta_id in that case.
1070      */
1071     if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1072         return vif_priv->ctx->ap_sta_id;
1073 
1074     return IWL_INVALID_STATION;
1075 }
1076 
1077 static int iwlagn_send_sta_key(struct iwl_priv *priv,
1078                    struct ieee80211_key_conf *keyconf,
1079                    u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1080                    u32 cmd_flags)
1081 {
1082     __le16 key_flags;
1083     struct iwl_addsta_cmd sta_cmd;
1084     int i;
1085 
1086     spin_lock_bh(&priv->sta_lock);
1087     memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1088     spin_unlock_bh(&priv->sta_lock);
1089 
1090     key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1091     key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
1092 
1093     switch (keyconf->cipher) {
1094     case WLAN_CIPHER_SUITE_CCMP:
1095         key_flags |= STA_KEY_FLG_CCMP;
1096         memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1097         break;
1098     case WLAN_CIPHER_SUITE_TKIP:
1099         key_flags |= STA_KEY_FLG_TKIP;
1100         sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1101         for (i = 0; i < 5; i++)
1102             sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1103         memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1104         break;
1105     case WLAN_CIPHER_SUITE_WEP104:
1106         key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
1107         fallthrough;
1108     case WLAN_CIPHER_SUITE_WEP40:
1109         key_flags |= STA_KEY_FLG_WEP;
1110         memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1111         break;
1112     default:
1113         WARN_ON(1);
1114         return -EINVAL;
1115     }
1116 
1117     if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1118         key_flags |= STA_KEY_MULTICAST_MSK;
1119 
1120     /* key pointer (offset) */
1121     sta_cmd.key.key_offset = keyconf->hw_key_idx;
1122 
1123     sta_cmd.key.key_flags = key_flags;
1124     sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1125     sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1126 
1127     return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
1128 }
1129 
1130 void iwl_update_tkip_key(struct iwl_priv *priv,
1131              struct ieee80211_vif *vif,
1132              struct ieee80211_key_conf *keyconf,
1133              struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1134 {
1135     u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1136 
1137     if (sta_id == IWL_INVALID_STATION)
1138         return;
1139 
1140     if (iwl_scan_cancel(priv)) {
1141         /* cancel scan failed, just live w/ bad key and rely
1142            briefly on SW decryption */
1143         return;
1144     }
1145 
1146     iwlagn_send_sta_key(priv, keyconf, sta_id,
1147                 iv32, phase1key, CMD_ASYNC);
1148 }
1149 
1150 int iwl_remove_dynamic_key(struct iwl_priv *priv,
1151                struct iwl_rxon_context *ctx,
1152                struct ieee80211_key_conf *keyconf,
1153                struct ieee80211_sta *sta)
1154 {
1155     struct iwl_addsta_cmd sta_cmd;
1156     u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1157     __le16 key_flags;
1158 
1159     /* if station isn't there, neither is the key */
1160     if (sta_id == IWL_INVALID_STATION)
1161         return -ENOENT;
1162 
1163     spin_lock_bh(&priv->sta_lock);
1164     memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1165     if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1166         sta_id = IWL_INVALID_STATION;
1167     spin_unlock_bh(&priv->sta_lock);
1168 
1169     if (sta_id == IWL_INVALID_STATION)
1170         return 0;
1171 
1172     lockdep_assert_held(&priv->mutex);
1173 
1174     ctx->key_mapping_keys--;
1175 
1176     IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1177               keyconf->keyidx, sta_id);
1178 
1179     if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1180         IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1181             keyconf->hw_key_idx);
1182 
1183     key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1184     key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1185              STA_KEY_FLG_INVALID;
1186 
1187     if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1188         key_flags |= STA_KEY_MULTICAST_MSK;
1189 
1190     sta_cmd.key.key_flags = key_flags;
1191     sta_cmd.key.key_offset = keyconf->hw_key_idx;
1192     sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1193     sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1194 
1195     return iwl_send_add_sta(priv, &sta_cmd, 0);
1196 }
1197 
1198 int iwl_set_dynamic_key(struct iwl_priv *priv,
1199             struct iwl_rxon_context *ctx,
1200             struct ieee80211_key_conf *keyconf,
1201             struct ieee80211_sta *sta)
1202 {
1203     struct ieee80211_key_seq seq;
1204     u16 p1k[5];
1205     int ret;
1206     u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1207     const u8 *addr;
1208 
1209     if (sta_id == IWL_INVALID_STATION)
1210         return -EINVAL;
1211 
1212     lockdep_assert_held(&priv->mutex);
1213 
1214     keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1215     if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1216         return -ENOSPC;
1217 
1218     ctx->key_mapping_keys++;
1219 
1220     switch (keyconf->cipher) {
1221     case WLAN_CIPHER_SUITE_TKIP:
1222         if (sta)
1223             addr = sta->addr;
1224         else /* station mode case only */
1225             addr = ctx->active.bssid_addr;
1226 
1227         /* pre-fill phase 1 key into device cache */
1228         ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1229         ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1230         ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1231                       seq.tkip.iv32, p1k, 0);
1232         break;
1233     case WLAN_CIPHER_SUITE_CCMP:
1234     case WLAN_CIPHER_SUITE_WEP40:
1235     case WLAN_CIPHER_SUITE_WEP104:
1236         ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1237                       0, NULL, 0);
1238         break;
1239     default:
1240         IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
1241         ret = -EINVAL;
1242     }
1243 
1244     if (ret) {
1245         ctx->key_mapping_keys--;
1246         clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1247     }
1248 
1249     IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1250               keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1251               sta ? sta->addr : NULL, ret);
1252 
1253     return ret;
1254 }
1255 
1256 /*
1257  * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1258  *
1259  * This adds the broadcast station into the driver's station table
1260  * and marks it driver active, so that it will be restored to the
1261  * device at the next best time.
1262  */
1263 int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1264                    struct iwl_rxon_context *ctx)
1265 {
1266     struct iwl_link_quality_cmd *link_cmd;
1267     u8 sta_id;
1268 
1269     spin_lock_bh(&priv->sta_lock);
1270     sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1271     if (sta_id == IWL_INVALID_STATION) {
1272         IWL_ERR(priv, "Unable to prepare broadcast station\n");
1273         spin_unlock_bh(&priv->sta_lock);
1274 
1275         return -EINVAL;
1276     }
1277 
1278     priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1279     priv->stations[sta_id].used |= IWL_STA_BCAST;
1280     spin_unlock_bh(&priv->sta_lock);
1281 
1282     link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
1283     if (!link_cmd) {
1284         IWL_ERR(priv,
1285             "Unable to initialize rate scaling for bcast station.\n");
1286         return -ENOMEM;
1287     }
1288 
1289     spin_lock_bh(&priv->sta_lock);
1290     priv->stations[sta_id].lq = link_cmd;
1291     spin_unlock_bh(&priv->sta_lock);
1292 
1293     return 0;
1294 }
1295 
1296 /*
1297  * iwl_update_bcast_station - update broadcast station's LQ command
1298  *
1299  * Only used by iwlagn. Placed here to have all bcast station management
1300  * code together.
1301  */
1302 int iwl_update_bcast_station(struct iwl_priv *priv,
1303                  struct iwl_rxon_context *ctx)
1304 {
1305     struct iwl_link_quality_cmd *link_cmd;
1306     u8 sta_id = ctx->bcast_sta_id;
1307 
1308     link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
1309     if (!link_cmd) {
1310         IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1311         return -ENOMEM;
1312     }
1313 
1314     spin_lock_bh(&priv->sta_lock);
1315     if (priv->stations[sta_id].lq)
1316         kfree(priv->stations[sta_id].lq);
1317     else
1318         IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1319     priv->stations[sta_id].lq = link_cmd;
1320     spin_unlock_bh(&priv->sta_lock);
1321 
1322     return 0;
1323 }
1324 
1325 int iwl_update_bcast_stations(struct iwl_priv *priv)
1326 {
1327     struct iwl_rxon_context *ctx;
1328     int ret = 0;
1329 
1330     for_each_context(priv, ctx) {
1331         ret = iwl_update_bcast_station(priv, ctx);
1332         if (ret)
1333             break;
1334     }
1335 
1336     return ret;
1337 }
1338 
1339 /*
1340  * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1341  */
1342 int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1343 {
1344     struct iwl_addsta_cmd sta_cmd;
1345 
1346     lockdep_assert_held(&priv->mutex);
1347 
1348     /* Remove "disable" flag, to enable Tx for this TID */
1349     spin_lock_bh(&priv->sta_lock);
1350     priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1351     priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1352     priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1353     memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1354     spin_unlock_bh(&priv->sta_lock);
1355 
1356     return iwl_send_add_sta(priv, &sta_cmd, 0);
1357 }
1358 
1359 int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1360              int tid, u16 ssn)
1361 {
1362     int sta_id;
1363     struct iwl_addsta_cmd sta_cmd;
1364 
1365     lockdep_assert_held(&priv->mutex);
1366 
1367     sta_id = iwl_sta_id(sta);
1368     if (sta_id == IWL_INVALID_STATION)
1369         return -ENXIO;
1370 
1371     spin_lock_bh(&priv->sta_lock);
1372     priv->stations[sta_id].sta.station_flags_msk = 0;
1373     priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1374     priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1375     priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1376     priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1377     memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1378     spin_unlock_bh(&priv->sta_lock);
1379 
1380     return iwl_send_add_sta(priv, &sta_cmd, 0);
1381 }
1382 
1383 int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1384             int tid)
1385 {
1386     int sta_id;
1387     struct iwl_addsta_cmd sta_cmd;
1388 
1389     lockdep_assert_held(&priv->mutex);
1390 
1391     sta_id = iwl_sta_id(sta);
1392     if (sta_id == IWL_INVALID_STATION) {
1393         IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1394         return -ENXIO;
1395     }
1396 
1397     spin_lock_bh(&priv->sta_lock);
1398     priv->stations[sta_id].sta.station_flags_msk = 0;
1399     priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1400     priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1401     priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1402     memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1403     spin_unlock_bh(&priv->sta_lock);
1404 
1405     return iwl_send_add_sta(priv, &sta_cmd, 0);
1406 }
1407 
1408 
1409 
1410 void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1411 {
1412     struct iwl_addsta_cmd cmd = {
1413         .mode = STA_CONTROL_MODIFY_MSK,
1414         .station_flags = STA_FLG_PWR_SAVE_MSK,
1415         .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1416         .sta.sta_id = sta_id,
1417         .sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1418         .sleep_tx_count = cpu_to_le16(cnt),
1419     };
1420 
1421     iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
1422 }