Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * This file is provided under a dual BSD/GPLv2 license. When using or |
| 4 | * redistributing this file, you may do so under either license. |
| 5 | * |
| 6 | * GPL LICENSE SUMMARY |
| 7 | * |
| 8 | * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of version 2 of the GNU General Public License as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, |
| 22 | * USA |
| 23 | * |
| 24 | * The full GNU General Public License is included in this distribution |
Emmanuel Grumbach | 410dc5a | 2013-02-18 09:22:28 +0200 | [diff] [blame] | 25 | * in the file called COPYING. |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 26 | * |
| 27 | * Contact Information: |
| 28 | * Intel Linux Wireless <ilw@linux.intel.com> |
| 29 | * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 |
| 30 | * |
| 31 | * BSD LICENSE |
| 32 | * |
| 33 | * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved. |
| 34 | * All rights reserved. |
| 35 | * |
| 36 | * Redistribution and use in source and binary forms, with or without |
| 37 | * modification, are permitted provided that the following conditions |
| 38 | * are met: |
| 39 | * |
| 40 | * * Redistributions of source code must retain the above copyright |
| 41 | * notice, this list of conditions and the following disclaimer. |
| 42 | * * Redistributions in binary form must reproduce the above copyright |
| 43 | * notice, this list of conditions and the following disclaimer in |
| 44 | * the documentation and/or other materials provided with the |
| 45 | * distribution. |
| 46 | * * Neither the name Intel Corporation nor the names of its |
| 47 | * contributors may be used to endorse or promote products derived |
| 48 | * from this software without specific prior written permission. |
| 49 | * |
| 50 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 51 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 52 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 53 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 54 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 55 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 56 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 57 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 58 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 59 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 60 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 61 | * |
| 62 | *****************************************************************************/ |
| 63 | #include <net/mac80211.h> |
| 64 | |
| 65 | #include "mvm.h" |
| 66 | #include "sta.h" |
| 67 | |
| 68 | static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm) |
| 69 | { |
| 70 | int sta_id; |
| 71 | |
| 72 | WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)); |
| 73 | |
| 74 | lockdep_assert_held(&mvm->mutex); |
| 75 | |
| 76 | /* Don't take rcu_read_lock() since we are protected by mvm->mutex */ |
| 77 | for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) |
| 78 | if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id], |
| 79 | lockdep_is_held(&mvm->mutex))) |
| 80 | return sta_id; |
| 81 | return IWL_MVM_STATION_COUNT; |
| 82 | } |
| 83 | |
Johannes Berg | 7a45397 | 2013-02-12 13:10:44 +0100 | [diff] [blame] | 84 | /* send station add/update command to firmware */ |
| 85 | int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta, |
| 86 | bool update) |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 87 | { |
| 88 | struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv; |
| 89 | struct iwl_mvm_add_sta_cmd add_sta_cmd; |
| 90 | int ret; |
| 91 | u32 status; |
| 92 | u32 agg_size = 0, mpdu_dens = 0; |
| 93 | |
| 94 | memset(&add_sta_cmd, 0, sizeof(add_sta_cmd)); |
| 95 | |
| 96 | add_sta_cmd.sta_id = mvm_sta->sta_id; |
| 97 | add_sta_cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color); |
Johannes Berg | 7a45397 | 2013-02-12 13:10:44 +0100 | [diff] [blame] | 98 | if (!update) { |
| 99 | add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk); |
| 100 | memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN); |
| 101 | } |
| 102 | add_sta_cmd.add_modify = update ? 1 : 0; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 103 | |
| 104 | /* STA_FLG_FAT_EN_MSK ? */ |
| 105 | /* STA_FLG_MIMO_EN_MSK ? */ |
| 106 | |
| 107 | if (sta->ht_cap.ht_supported) { |
| 108 | add_sta_cmd.station_flags_msk |= |
| 109 | cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK | |
| 110 | STA_FLG_AGG_MPDU_DENS_MSK); |
| 111 | |
| 112 | mpdu_dens = sta->ht_cap.ampdu_density; |
| 113 | } |
| 114 | |
| 115 | if (sta->vht_cap.vht_supported) { |
| 116 | agg_size = sta->vht_cap.cap & |
| 117 | IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK; |
| 118 | agg_size >>= |
| 119 | IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT; |
| 120 | } else if (sta->ht_cap.ht_supported) { |
| 121 | agg_size = sta->ht_cap.ampdu_factor; |
| 122 | } |
| 123 | |
| 124 | add_sta_cmd.station_flags |= |
| 125 | cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT); |
| 126 | add_sta_cmd.station_flags |= |
| 127 | cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT); |
| 128 | |
| 129 | status = ADD_STA_SUCCESS; |
| 130 | ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(add_sta_cmd), |
| 131 | &add_sta_cmd, &status); |
| 132 | if (ret) |
| 133 | return ret; |
| 134 | |
| 135 | switch (status) { |
| 136 | case ADD_STA_SUCCESS: |
| 137 | IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n"); |
| 138 | break; |
| 139 | default: |
| 140 | ret = -EIO; |
| 141 | IWL_ERR(mvm, "ADD_STA failed\n"); |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | int iwl_mvm_add_sta(struct iwl_mvm *mvm, |
| 149 | struct ieee80211_vif *vif, |
| 150 | struct ieee80211_sta *sta) |
| 151 | { |
| 152 | struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); |
| 153 | struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv; |
| 154 | int i, ret, sta_id; |
| 155 | |
| 156 | lockdep_assert_held(&mvm->mutex); |
| 157 | |
| 158 | if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) |
| 159 | sta_id = iwl_mvm_find_free_sta_id(mvm); |
| 160 | else |
| 161 | sta_id = mvm_sta->sta_id; |
| 162 | |
| 163 | if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT)) |
| 164 | return -ENOSPC; |
| 165 | |
| 166 | spin_lock_init(&mvm_sta->lock); |
| 167 | |
| 168 | mvm_sta->sta_id = sta_id; |
| 169 | mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id, |
| 170 | mvmvif->color); |
| 171 | mvm_sta->vif = vif; |
| 172 | mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF; |
| 173 | |
| 174 | /* HW restart, don't assume the memory has been zeroed */ |
| 175 | atomic_set(&mvm_sta->pending_frames, 0); |
| 176 | mvm_sta->tid_disable_agg = 0; |
| 177 | mvm_sta->tfd_queue_msk = 0; |
| 178 | for (i = 0; i < IEEE80211_NUM_ACS; i++) |
| 179 | if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE) |
| 180 | mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]); |
| 181 | |
| 182 | if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE) |
| 183 | mvm_sta->tfd_queue_msk |= BIT(vif->cab_queue); |
| 184 | |
| 185 | /* for HW restart - need to reset the seq_number etc... */ |
| 186 | memset(mvm_sta->tid_data, 0, sizeof(mvm_sta->tid_data)); |
| 187 | |
Johannes Berg | 7a45397 | 2013-02-12 13:10:44 +0100 | [diff] [blame] | 188 | ret = iwl_mvm_sta_send_to_fw(mvm, sta, false); |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 189 | if (ret) |
| 190 | return ret; |
| 191 | |
| 192 | /* The first station added is the AP, the others are TDLS STAs */ |
| 193 | if (vif->type == NL80211_IFTYPE_STATION && |
| 194 | mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT) |
| 195 | mvmvif->ap_sta_id = sta_id; |
| 196 | |
| 197 | rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Johannes Berg | 7a45397 | 2013-02-12 13:10:44 +0100 | [diff] [blame] | 202 | int iwl_mvm_update_sta(struct iwl_mvm *mvm, |
| 203 | struct ieee80211_vif *vif, |
| 204 | struct ieee80211_sta *sta) |
| 205 | { |
| 206 | return iwl_mvm_sta_send_to_fw(mvm, sta, true); |
| 207 | } |
| 208 | |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 209 | int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta, |
| 210 | bool drain) |
| 211 | { |
| 212 | struct iwl_mvm_add_sta_cmd cmd = {}; |
| 213 | int ret; |
| 214 | u32 status; |
| 215 | |
| 216 | lockdep_assert_held(&mvm->mutex); |
| 217 | |
| 218 | cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color); |
| 219 | cmd.sta_id = mvmsta->sta_id; |
| 220 | cmd.add_modify = STA_MODE_MODIFY; |
| 221 | cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0; |
| 222 | cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW); |
| 223 | |
| 224 | status = ADD_STA_SUCCESS; |
| 225 | ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd), |
| 226 | &cmd, &status); |
| 227 | if (ret) |
| 228 | return ret; |
| 229 | |
| 230 | switch (status) { |
| 231 | case ADD_STA_SUCCESS: |
| 232 | IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n", |
| 233 | mvmsta->sta_id); |
| 234 | break; |
| 235 | default: |
| 236 | ret = -EIO; |
| 237 | IWL_ERR(mvm, "Couldn't drain frames for staid %d\n", |
| 238 | mvmsta->sta_id); |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Remove a station from the FW table. Before sending the command to remove |
| 247 | * the station validate that the station is indeed known to the driver (sanity |
| 248 | * only). |
| 249 | */ |
| 250 | static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id) |
| 251 | { |
| 252 | struct ieee80211_sta *sta; |
| 253 | struct iwl_mvm_rm_sta_cmd rm_sta_cmd = { |
| 254 | .sta_id = sta_id, |
| 255 | }; |
| 256 | int ret; |
| 257 | |
| 258 | sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id], |
| 259 | lockdep_is_held(&mvm->mutex)); |
| 260 | |
| 261 | /* Note: internal stations are marked as error values */ |
| 262 | if (!sta) { |
| 263 | IWL_ERR(mvm, "Invalid station id\n"); |
| 264 | return -EINVAL; |
| 265 | } |
| 266 | |
| 267 | ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, CMD_SYNC, |
| 268 | sizeof(rm_sta_cmd), &rm_sta_cmd); |
| 269 | if (ret) { |
| 270 | IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id); |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | void iwl_mvm_sta_drained_wk(struct work_struct *wk) |
| 278 | { |
| 279 | struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk); |
| 280 | u8 sta_id; |
| 281 | |
| 282 | /* |
| 283 | * The mutex is needed because of the SYNC cmd, but not only: if the |
| 284 | * work would run concurrently with iwl_mvm_rm_sta, it would run before |
| 285 | * iwl_mvm_rm_sta sets the station as busy, and exit. Then |
| 286 | * iwl_mvm_rm_sta would set the station as busy, and nobody will clean |
| 287 | * that later. |
| 288 | */ |
| 289 | mutex_lock(&mvm->mutex); |
| 290 | |
| 291 | for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) { |
| 292 | int ret; |
| 293 | struct ieee80211_sta *sta = |
| 294 | rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id], |
| 295 | lockdep_is_held(&mvm->mutex)); |
| 296 | |
| 297 | /* This station is in use */ |
| 298 | if (!IS_ERR(sta)) |
| 299 | continue; |
| 300 | |
| 301 | if (PTR_ERR(sta) == -EINVAL) { |
| 302 | IWL_ERR(mvm, "Drained sta %d, but it is internal?\n", |
| 303 | sta_id); |
| 304 | continue; |
| 305 | } |
| 306 | |
| 307 | if (!sta) { |
| 308 | IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n", |
| 309 | sta_id); |
| 310 | continue; |
| 311 | } |
| 312 | |
| 313 | WARN_ON(PTR_ERR(sta) != -EBUSY); |
| 314 | /* This station was removed and we waited until it got drained, |
| 315 | * we can now proceed and remove it. |
| 316 | */ |
| 317 | ret = iwl_mvm_rm_sta_common(mvm, sta_id); |
| 318 | if (ret) { |
| 319 | IWL_ERR(mvm, |
| 320 | "Couldn't remove sta %d after it was drained\n", |
| 321 | sta_id); |
| 322 | continue; |
| 323 | } |
| 324 | rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL); |
| 325 | clear_bit(sta_id, mvm->sta_drained); |
| 326 | } |
| 327 | |
| 328 | mutex_unlock(&mvm->mutex); |
| 329 | } |
| 330 | |
| 331 | int iwl_mvm_rm_sta(struct iwl_mvm *mvm, |
| 332 | struct ieee80211_vif *vif, |
| 333 | struct ieee80211_sta *sta) |
| 334 | { |
| 335 | struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); |
| 336 | struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv; |
| 337 | int ret; |
| 338 | |
| 339 | lockdep_assert_held(&mvm->mutex); |
| 340 | |
| 341 | if (vif->type == NL80211_IFTYPE_STATION && |
| 342 | mvmvif->ap_sta_id == mvm_sta->sta_id) { |
Emmanuel Grumbach | 80d8565 | 2013-02-19 15:32:42 +0200 | [diff] [blame] | 343 | /* flush its queues here since we are freeing mvm_sta */ |
| 344 | ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true); |
| 345 | |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 346 | /* |
| 347 | * Put a non-NULL since the fw station isn't removed. |
| 348 | * It will be removed after the MAC will be set as |
| 349 | * unassoc. |
| 350 | */ |
| 351 | rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id], |
| 352 | ERR_PTR(-EINVAL)); |
| 353 | |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 354 | /* if we are associated - we can't remove the AP STA now */ |
| 355 | if (vif->bss_conf.assoc) |
| 356 | return ret; |
| 357 | |
| 358 | /* unassoc - go ahead - remove the AP STA now */ |
| 359 | mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * There are frames pending on the AC queues for this station. |
| 364 | * We need to wait until all the frames are drained... |
| 365 | */ |
| 366 | if (atomic_read(&mvm_sta->pending_frames)) { |
| 367 | ret = iwl_mvm_drain_sta(mvm, mvm_sta, true); |
| 368 | rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id], |
| 369 | ERR_PTR(-EBUSY)); |
| 370 | } else { |
| 371 | ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id); |
| 372 | rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL); |
| 373 | } |
| 374 | |
| 375 | return ret; |
| 376 | } |
| 377 | |
| 378 | int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm, |
| 379 | struct ieee80211_vif *vif, |
| 380 | u8 sta_id) |
| 381 | { |
| 382 | int ret = iwl_mvm_rm_sta_common(mvm, sta_id); |
| 383 | |
| 384 | lockdep_assert_held(&mvm->mutex); |
| 385 | |
| 386 | rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL); |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta, |
| 391 | u32 qmask) |
| 392 | { |
| 393 | if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) { |
| 394 | sta->sta_id = iwl_mvm_find_free_sta_id(mvm); |
| 395 | if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT)) |
| 396 | return -ENOSPC; |
| 397 | } |
| 398 | |
| 399 | sta->tfd_queue_msk = qmask; |
| 400 | |
| 401 | /* put a non-NULL value so iterating over the stations won't stop */ |
| 402 | rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL)); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta) |
| 407 | { |
| 408 | rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], NULL); |
| 409 | memset(sta, 0, sizeof(struct iwl_mvm_int_sta)); |
| 410 | sta->sta_id = IWL_MVM_STATION_COUNT; |
| 411 | } |
| 412 | |
| 413 | static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm, |
| 414 | struct iwl_mvm_int_sta *sta, |
| 415 | const u8 *addr, |
| 416 | u16 mac_id, u16 color) |
| 417 | { |
| 418 | struct iwl_mvm_add_sta_cmd cmd; |
| 419 | int ret; |
| 420 | u32 status; |
| 421 | |
| 422 | lockdep_assert_held(&mvm->mutex); |
| 423 | |
| 424 | memset(&cmd, 0, sizeof(struct iwl_mvm_add_sta_cmd)); |
| 425 | cmd.sta_id = sta->sta_id; |
| 426 | cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id, |
| 427 | color)); |
| 428 | |
| 429 | cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk); |
| 430 | |
| 431 | if (addr) |
| 432 | memcpy(cmd.addr, addr, ETH_ALEN); |
| 433 | |
| 434 | ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd), |
| 435 | &cmd, &status); |
| 436 | if (ret) |
| 437 | return ret; |
| 438 | |
| 439 | switch (status) { |
| 440 | case ADD_STA_SUCCESS: |
| 441 | IWL_DEBUG_INFO(mvm, "Internal station added.\n"); |
| 442 | return 0; |
| 443 | default: |
| 444 | ret = -EIO; |
| 445 | IWL_ERR(mvm, "Add internal station failed, status=0x%x\n", |
| 446 | status); |
| 447 | break; |
| 448 | } |
| 449 | return ret; |
| 450 | } |
| 451 | |
| 452 | int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm) |
| 453 | { |
| 454 | int ret; |
| 455 | |
| 456 | lockdep_assert_held(&mvm->mutex); |
| 457 | |
| 458 | /* Add the aux station, but without any queues */ |
| 459 | ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, 0); |
| 460 | if (ret) |
| 461 | return ret; |
| 462 | |
| 463 | ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL, |
| 464 | MAC_INDEX_AUX, 0); |
| 465 | |
| 466 | if (ret) |
| 467 | iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta); |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * Send the add station command for the vif's broadcast station. |
| 473 | * Assumes that the station was already allocated. |
| 474 | * |
| 475 | * @mvm: the mvm component |
| 476 | * @vif: the interface to which the broadcast station is added |
| 477 | * @bsta: the broadcast station to add. |
| 478 | */ |
| 479 | int iwl_mvm_send_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif, |
| 480 | struct iwl_mvm_int_sta *bsta) |
| 481 | { |
| 482 | struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); |
| 483 | static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; |
| 484 | |
| 485 | lockdep_assert_held(&mvm->mutex); |
| 486 | |
| 487 | if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT)) |
| 488 | return -ENOSPC; |
| 489 | |
| 490 | return iwl_mvm_add_int_sta_common(mvm, bsta, baddr, |
| 491 | mvmvif->id, mvmvif->color); |
| 492 | } |
| 493 | |
| 494 | /* Send the FW a request to remove the station from it's internal data |
| 495 | * structures, but DO NOT remove the entry from the local data structures. */ |
| 496 | int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, |
| 497 | struct iwl_mvm_int_sta *bsta) |
| 498 | { |
| 499 | int ret; |
| 500 | |
| 501 | lockdep_assert_held(&mvm->mutex); |
| 502 | |
| 503 | ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id); |
| 504 | if (ret) |
| 505 | IWL_WARN(mvm, "Failed sending remove station\n"); |
| 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | /* Allocate a new station entry for the broadcast station to the given vif, |
| 510 | * and send it to the FW. |
| 511 | * Note that each P2P mac should have its own broadcast station. |
| 512 | * |
| 513 | * @mvm: the mvm component |
| 514 | * @vif: the interface to which the broadcast station is added |
| 515 | * @bsta: the broadcast station to add. */ |
| 516 | int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif, |
| 517 | struct iwl_mvm_int_sta *bsta) |
| 518 | { |
| 519 | struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); |
| 520 | static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; |
| 521 | u32 qmask; |
| 522 | int ret; |
| 523 | |
| 524 | lockdep_assert_held(&mvm->mutex); |
| 525 | |
| 526 | qmask = iwl_mvm_mac_get_queues_mask(mvm, vif); |
| 527 | ret = iwl_mvm_allocate_int_sta(mvm, bsta, qmask); |
| 528 | if (ret) |
| 529 | return ret; |
| 530 | |
| 531 | ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr, |
| 532 | mvmvif->id, mvmvif->color); |
| 533 | |
| 534 | if (ret) |
| 535 | iwl_mvm_dealloc_int_sta(mvm, bsta); |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Send the FW a request to remove the station from it's internal data |
| 541 | * structures, and in addition remove it from the local data structure. |
| 542 | */ |
| 543 | int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *bsta) |
| 544 | { |
| 545 | int ret; |
| 546 | |
| 547 | lockdep_assert_held(&mvm->mutex); |
| 548 | |
| 549 | ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id); |
| 550 | if (ret) |
| 551 | return ret; |
| 552 | |
| 553 | iwl_mvm_dealloc_int_sta(mvm, bsta); |
| 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta, |
| 558 | int tid, u16 ssn, bool start) |
| 559 | { |
| 560 | struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv; |
| 561 | struct iwl_mvm_add_sta_cmd cmd = {}; |
| 562 | int ret; |
| 563 | u32 status; |
| 564 | |
| 565 | lockdep_assert_held(&mvm->mutex); |
| 566 | |
| 567 | cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color); |
| 568 | cmd.sta_id = mvm_sta->sta_id; |
| 569 | cmd.add_modify = STA_MODE_MODIFY; |
| 570 | cmd.add_immediate_ba_tid = (u8) tid; |
| 571 | cmd.add_immediate_ba_ssn = cpu_to_le16(ssn); |
| 572 | cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID : |
| 573 | STA_MODIFY_REMOVE_BA_TID; |
| 574 | |
| 575 | status = ADD_STA_SUCCESS; |
| 576 | ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd), |
| 577 | &cmd, &status); |
| 578 | if (ret) |
| 579 | return ret; |
| 580 | |
| 581 | switch (status) { |
| 582 | case ADD_STA_SUCCESS: |
| 583 | IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n", |
| 584 | start ? "start" : "stopp"); |
| 585 | break; |
| 586 | case ADD_STA_IMMEDIATE_BA_FAILURE: |
| 587 | IWL_WARN(mvm, "RX BA Session refused by fw\n"); |
| 588 | ret = -ENOSPC; |
| 589 | break; |
| 590 | default: |
| 591 | ret = -EIO; |
| 592 | IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n", |
| 593 | start ? "start" : "stopp", status); |
| 594 | break; |
| 595 | } |
| 596 | |
| 597 | return ret; |
| 598 | } |
| 599 | |
| 600 | static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta, |
| 601 | int tid, u8 queue, bool start) |
| 602 | { |
| 603 | struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv; |
| 604 | struct iwl_mvm_add_sta_cmd cmd = {}; |
| 605 | int ret; |
| 606 | u32 status; |
| 607 | |
| 608 | lockdep_assert_held(&mvm->mutex); |
| 609 | |
| 610 | if (start) { |
| 611 | mvm_sta->tfd_queue_msk |= BIT(queue); |
| 612 | mvm_sta->tid_disable_agg &= ~BIT(tid); |
| 613 | } else { |
| 614 | mvm_sta->tfd_queue_msk &= ~BIT(queue); |
| 615 | mvm_sta->tid_disable_agg |= BIT(tid); |
| 616 | } |
| 617 | |
| 618 | cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color); |
| 619 | cmd.sta_id = mvm_sta->sta_id; |
| 620 | cmd.add_modify = STA_MODE_MODIFY; |
| 621 | cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX; |
| 622 | cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk); |
| 623 | cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg); |
| 624 | |
| 625 | status = ADD_STA_SUCCESS; |
| 626 | ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd), |
| 627 | &cmd, &status); |
| 628 | if (ret) |
| 629 | return ret; |
| 630 | |
| 631 | switch (status) { |
| 632 | case ADD_STA_SUCCESS: |
| 633 | break; |
| 634 | default: |
| 635 | ret = -EIO; |
| 636 | IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n", |
| 637 | start ? "start" : "stopp", status); |
| 638 | break; |
| 639 | } |
| 640 | |
| 641 | return ret; |
| 642 | } |
| 643 | |
| 644 | static const u8 tid_to_ac[] = { |
| 645 | IEEE80211_AC_BE, |
| 646 | IEEE80211_AC_BK, |
| 647 | IEEE80211_AC_BK, |
| 648 | IEEE80211_AC_BE, |
| 649 | IEEE80211_AC_VI, |
| 650 | IEEE80211_AC_VI, |
| 651 | IEEE80211_AC_VO, |
| 652 | IEEE80211_AC_VO, |
| 653 | }; |
| 654 | |
| 655 | int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif, |
| 656 | struct ieee80211_sta *sta, u16 tid, u16 *ssn) |
| 657 | { |
| 658 | struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv; |
| 659 | struct iwl_mvm_tid_data *tid_data; |
| 660 | int txq_id; |
| 661 | |
| 662 | if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT)) |
| 663 | return -EINVAL; |
| 664 | |
| 665 | if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) { |
| 666 | IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n", |
| 667 | mvmsta->tid_data[tid].state); |
| 668 | return -ENXIO; |
| 669 | } |
| 670 | |
| 671 | lockdep_assert_held(&mvm->mutex); |
| 672 | |
| 673 | for (txq_id = IWL_MVM_FIRST_AGG_QUEUE; |
| 674 | txq_id <= IWL_MVM_LAST_AGG_QUEUE; txq_id++) |
| 675 | if (mvm->queue_to_mac80211[txq_id] == |
| 676 | IWL_INVALID_MAC80211_QUEUE) |
| 677 | break; |
| 678 | |
| 679 | if (txq_id > IWL_MVM_LAST_AGG_QUEUE) { |
| 680 | IWL_ERR(mvm, "Failed to allocate agg queue\n"); |
| 681 | return -EIO; |
| 682 | } |
| 683 | |
| 684 | /* the new tx queue is still connected to the same mac80211 queue */ |
| 685 | mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_ac[tid]]; |
| 686 | |
| 687 | spin_lock_bh(&mvmsta->lock); |
| 688 | tid_data = &mvmsta->tid_data[tid]; |
| 689 | tid_data->ssn = SEQ_TO_SN(tid_data->seq_number); |
| 690 | tid_data->txq_id = txq_id; |
| 691 | *ssn = tid_data->ssn; |
| 692 | |
| 693 | IWL_DEBUG_TX_QUEUES(mvm, |
| 694 | "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n", |
| 695 | mvmsta->sta_id, tid, txq_id, tid_data->ssn, |
| 696 | tid_data->next_reclaimed); |
| 697 | |
| 698 | if (tid_data->ssn == tid_data->next_reclaimed) { |
| 699 | tid_data->state = IWL_AGG_STARTING; |
| 700 | ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid); |
| 701 | } else { |
| 702 | tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA; |
| 703 | } |
| 704 | |
| 705 | spin_unlock_bh(&mvmsta->lock); |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif, |
| 711 | struct ieee80211_sta *sta, u16 tid, u8 buf_size) |
| 712 | { |
| 713 | struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv; |
| 714 | struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid]; |
| 715 | int queue, fifo, ret; |
| 716 | u16 ssn; |
| 717 | |
| 718 | buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF); |
| 719 | |
| 720 | spin_lock_bh(&mvmsta->lock); |
| 721 | ssn = tid_data->ssn; |
| 722 | queue = tid_data->txq_id; |
| 723 | tid_data->state = IWL_AGG_ON; |
| 724 | tid_data->ssn = 0xffff; |
| 725 | spin_unlock_bh(&mvmsta->lock); |
| 726 | |
| 727 | fifo = iwl_mvm_ac_to_tx_fifo[tid_to_ac[tid]]; |
| 728 | |
| 729 | ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true); |
| 730 | if (ret) |
| 731 | return -EIO; |
| 732 | |
| 733 | iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid, |
| 734 | buf_size, ssn); |
| 735 | |
| 736 | /* |
| 737 | * Even though in theory the peer could have different |
| 738 | * aggregation reorder buffer sizes for different sessions, |
| 739 | * our ucode doesn't allow for that and has a global limit |
| 740 | * for each station. Therefore, use the minimum of all the |
| 741 | * aggregation sessions and our default value. |
| 742 | */ |
| 743 | mvmsta->max_agg_bufsize = |
| 744 | min(mvmsta->max_agg_bufsize, buf_size); |
| 745 | mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize; |
| 746 | |
| 747 | if (mvm->cfg->ht_params->use_rts_for_aggregation) { |
| 748 | /* |
| 749 | * switch to RTS/CTS if it is the prefer protection |
| 750 | * method for HT traffic |
| 751 | */ |
| 752 | mvmsta->lq_sta.lq.flags |= LQ_FLAG_SET_STA_TLC_RTS_MSK; |
| 753 | /* |
| 754 | * TODO: remove the TLC_RTS flag when we tear down the last |
| 755 | * AGG session (agg_tids_count in DVM) |
| 756 | */ |
| 757 | } |
| 758 | |
| 759 | IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n", |
| 760 | sta->addr, tid); |
| 761 | |
| 762 | return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, CMD_ASYNC, false); |
| 763 | } |
| 764 | |
| 765 | int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif, |
| 766 | struct ieee80211_sta *sta, u16 tid) |
| 767 | { |
| 768 | struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv; |
| 769 | struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid]; |
| 770 | u16 txq_id; |
| 771 | int err; |
| 772 | |
Emmanuel Grumbach | f9aa8dd | 2013-03-04 09:11:08 +0200 | [diff] [blame] | 773 | |
| 774 | /* |
| 775 | * If mac80211 is cleaning its state, then say that we finished since |
| 776 | * our state has been cleared anyway. |
| 777 | */ |
| 778 | if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) { |
| 779 | ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); |
| 780 | return 0; |
| 781 | } |
| 782 | |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 783 | spin_lock_bh(&mvmsta->lock); |
| 784 | |
| 785 | txq_id = tid_data->txq_id; |
| 786 | |
| 787 | IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n", |
| 788 | mvmsta->sta_id, tid, txq_id, tid_data->state); |
| 789 | |
| 790 | switch (tid_data->state) { |
| 791 | case IWL_AGG_ON: |
| 792 | tid_data->ssn = SEQ_TO_SN(tid_data->seq_number); |
| 793 | |
| 794 | IWL_DEBUG_TX_QUEUES(mvm, |
| 795 | "ssn = %d, next_recl = %d\n", |
| 796 | tid_data->ssn, tid_data->next_reclaimed); |
| 797 | |
| 798 | /* There are still packets for this RA / TID in the HW */ |
| 799 | if (tid_data->ssn != tid_data->next_reclaimed) { |
| 800 | tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA; |
| 801 | err = 0; |
| 802 | break; |
| 803 | } |
| 804 | |
| 805 | tid_data->ssn = 0xffff; |
| 806 | iwl_trans_txq_disable(mvm->trans, txq_id); |
| 807 | /* fall through */ |
| 808 | case IWL_AGG_STARTING: |
| 809 | case IWL_EMPTYING_HW_QUEUE_ADDBA: |
| 810 | /* |
| 811 | * The agg session has been stopped before it was set up. This |
| 812 | * can happen when the AddBA timer times out for example. |
| 813 | */ |
| 814 | |
| 815 | /* No barriers since we are under mutex */ |
| 816 | lockdep_assert_held(&mvm->mutex); |
| 817 | mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE; |
| 818 | |
| 819 | ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); |
| 820 | tid_data->state = IWL_AGG_OFF; |
| 821 | err = 0; |
| 822 | break; |
| 823 | default: |
| 824 | IWL_ERR(mvm, |
| 825 | "Stopping AGG while state not ON or starting for %d on %d (%d)\n", |
| 826 | mvmsta->sta_id, tid, tid_data->state); |
| 827 | IWL_ERR(mvm, |
| 828 | "\ttid_data->txq_id = %d\n", tid_data->txq_id); |
| 829 | err = -EINVAL; |
| 830 | } |
| 831 | |
| 832 | spin_unlock_bh(&mvmsta->lock); |
| 833 | |
| 834 | return err; |
| 835 | } |
| 836 | |
Emmanuel Grumbach | e3d9e7c | 2013-02-19 16:13:53 +0200 | [diff] [blame^] | 837 | int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif, |
| 838 | struct ieee80211_sta *sta, u16 tid) |
| 839 | { |
| 840 | struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv; |
| 841 | struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid]; |
| 842 | u16 txq_id; |
| 843 | |
| 844 | /* |
| 845 | * First set the agg state to OFF to avoid calling |
| 846 | * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty. |
| 847 | */ |
| 848 | spin_lock_bh(&mvmsta->lock); |
| 849 | txq_id = tid_data->txq_id; |
| 850 | IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n", |
| 851 | mvmsta->sta_id, tid, txq_id, tid_data->state); |
| 852 | tid_data->state = IWL_AGG_OFF; |
| 853 | spin_unlock_bh(&mvmsta->lock); |
| 854 | |
| 855 | if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true)) |
| 856 | IWL_ERR(mvm, "Couldn't flush the AGG queue\n"); |
| 857 | |
| 858 | iwl_trans_txq_disable(mvm->trans, tid_data->txq_id); |
| 859 | mvm->queue_to_mac80211[tid_data->txq_id] = |
| 860 | IWL_INVALID_MAC80211_QUEUE; |
| 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 865 | static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm) |
| 866 | { |
| 867 | int i; |
| 868 | |
| 869 | lockdep_assert_held(&mvm->mutex); |
| 870 | |
| 871 | i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM); |
| 872 | |
| 873 | if (i == STA_KEY_MAX_NUM) |
| 874 | return STA_KEY_IDX_INVALID; |
| 875 | |
| 876 | __set_bit(i, mvm->fw_key_table); |
| 877 | |
| 878 | return i; |
| 879 | } |
| 880 | |
| 881 | static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif, |
| 882 | struct ieee80211_sta *sta) |
| 883 | { |
| 884 | struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv; |
| 885 | |
| 886 | if (sta) { |
| 887 | struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv; |
| 888 | |
| 889 | return mvm_sta->sta_id; |
| 890 | } |
| 891 | |
| 892 | /* |
| 893 | * The device expects GTKs for station interfaces to be |
| 894 | * installed as GTKs for the AP station. If we have no |
| 895 | * station ID, then use AP's station ID. |
| 896 | */ |
| 897 | if (vif->type == NL80211_IFTYPE_STATION && |
| 898 | mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) |
| 899 | return mvmvif->ap_sta_id; |
| 900 | |
| 901 | return IWL_INVALID_STATION; |
| 902 | } |
| 903 | |
| 904 | static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm, |
| 905 | struct iwl_mvm_sta *mvm_sta, |
| 906 | struct ieee80211_key_conf *keyconf, |
| 907 | u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k, |
| 908 | u32 cmd_flags) |
| 909 | { |
| 910 | __le16 key_flags; |
| 911 | struct iwl_mvm_add_sta_cmd cmd = {}; |
| 912 | int ret, status; |
| 913 | u16 keyidx; |
| 914 | int i; |
| 915 | |
| 916 | keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) & |
| 917 | STA_KEY_FLG_KEYID_MSK; |
| 918 | key_flags = cpu_to_le16(keyidx); |
| 919 | key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP); |
| 920 | |
| 921 | switch (keyconf->cipher) { |
| 922 | case WLAN_CIPHER_SUITE_TKIP: |
| 923 | key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP); |
| 924 | cmd.key.tkip_rx_tsc_byte2 = tkip_iv32; |
| 925 | for (i = 0; i < 5; i++) |
| 926 | cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]); |
| 927 | memcpy(cmd.key.key, keyconf->key, keyconf->keylen); |
| 928 | break; |
| 929 | case WLAN_CIPHER_SUITE_CCMP: |
| 930 | key_flags |= cpu_to_le16(STA_KEY_FLG_CCM); |
| 931 | memcpy(cmd.key.key, keyconf->key, keyconf->keylen); |
| 932 | break; |
| 933 | default: |
| 934 | WARN_ON(1); |
| 935 | return -EINVAL; |
| 936 | } |
| 937 | |
| 938 | if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) |
| 939 | key_flags |= cpu_to_le16(STA_KEY_MULTICAST); |
| 940 | |
| 941 | cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color); |
| 942 | cmd.key.key_offset = keyconf->hw_key_idx; |
| 943 | cmd.key.key_flags = key_flags; |
| 944 | cmd.add_modify = STA_MODE_MODIFY; |
| 945 | cmd.modify_mask = STA_MODIFY_KEY; |
| 946 | cmd.sta_id = sta_id; |
| 947 | |
| 948 | status = ADD_STA_SUCCESS; |
| 949 | if (cmd_flags == CMD_SYNC) |
| 950 | ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd), |
| 951 | &cmd, &status); |
| 952 | else |
| 953 | ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, |
| 954 | sizeof(cmd), &cmd); |
| 955 | |
| 956 | switch (status) { |
| 957 | case ADD_STA_SUCCESS: |
| 958 | IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n"); |
| 959 | break; |
| 960 | default: |
| 961 | ret = -EIO; |
| 962 | IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n"); |
| 963 | break; |
| 964 | } |
| 965 | |
| 966 | return ret; |
| 967 | } |
| 968 | |
| 969 | static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm, |
| 970 | struct ieee80211_key_conf *keyconf, |
| 971 | u8 sta_id, bool remove_key) |
| 972 | { |
| 973 | struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {}; |
| 974 | |
| 975 | /* verify the key details match the required command's expectations */ |
| 976 | if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) || |
| 977 | (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) || |
| 978 | (keyconf->keyidx != 4 && keyconf->keyidx != 5))) |
| 979 | return -EINVAL; |
| 980 | |
| 981 | igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx); |
| 982 | igtk_cmd.sta_id = cpu_to_le32(sta_id); |
| 983 | |
| 984 | if (remove_key) { |
| 985 | igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID); |
| 986 | } else { |
| 987 | struct ieee80211_key_seq seq; |
| 988 | const u8 *pn; |
| 989 | |
| 990 | memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen); |
| 991 | ieee80211_aes_cmac_calculate_k1_k2(keyconf, |
| 992 | igtk_cmd.K1, igtk_cmd.K2); |
| 993 | ieee80211_get_key_rx_seq(keyconf, 0, &seq); |
| 994 | pn = seq.aes_cmac.pn; |
| 995 | igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) | |
| 996 | ((u64) pn[4] << 8) | |
| 997 | ((u64) pn[3] << 16) | |
| 998 | ((u64) pn[2] << 24) | |
| 999 | ((u64) pn[1] << 32) | |
| 1000 | ((u64) pn[0] << 40)); |
| 1001 | } |
| 1002 | |
| 1003 | IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n", |
| 1004 | remove_key ? "removing" : "installing", |
| 1005 | igtk_cmd.sta_id); |
| 1006 | |
| 1007 | return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, CMD_SYNC, |
| 1008 | sizeof(igtk_cmd), &igtk_cmd); |
| 1009 | } |
| 1010 | |
| 1011 | |
| 1012 | static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm, |
| 1013 | struct ieee80211_vif *vif, |
| 1014 | struct ieee80211_sta *sta) |
| 1015 | { |
| 1016 | struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv; |
| 1017 | |
| 1018 | if (sta) |
| 1019 | return sta->addr; |
| 1020 | |
| 1021 | if (vif->type == NL80211_IFTYPE_STATION && |
| 1022 | mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) { |
| 1023 | u8 sta_id = mvmvif->ap_sta_id; |
| 1024 | sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id], |
| 1025 | lockdep_is_held(&mvm->mutex)); |
| 1026 | return sta->addr; |
| 1027 | } |
| 1028 | |
| 1029 | |
| 1030 | return NULL; |
| 1031 | } |
| 1032 | |
| 1033 | int iwl_mvm_set_sta_key(struct iwl_mvm *mvm, |
| 1034 | struct ieee80211_vif *vif, |
| 1035 | struct ieee80211_sta *sta, |
| 1036 | struct ieee80211_key_conf *keyconf, |
| 1037 | bool have_key_offset) |
| 1038 | { |
| 1039 | struct iwl_mvm_sta *mvm_sta; |
| 1040 | int ret; |
| 1041 | u8 *addr, sta_id; |
| 1042 | struct ieee80211_key_seq seq; |
| 1043 | u16 p1k[5]; |
| 1044 | |
| 1045 | lockdep_assert_held(&mvm->mutex); |
| 1046 | |
| 1047 | /* Get the station id from the mvm local station table */ |
| 1048 | sta_id = iwl_mvm_get_key_sta_id(vif, sta); |
| 1049 | if (sta_id == IWL_INVALID_STATION) { |
| 1050 | IWL_ERR(mvm, "Failed to find station id\n"); |
| 1051 | return -EINVAL; |
| 1052 | } |
| 1053 | |
| 1054 | if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) { |
| 1055 | ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false); |
| 1056 | goto end; |
| 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | * It is possible that the 'sta' parameter is NULL, and thus |
| 1061 | * there is a need to retrieve the sta from the local station table. |
| 1062 | */ |
| 1063 | if (!sta) { |
| 1064 | sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id], |
| 1065 | lockdep_is_held(&mvm->mutex)); |
| 1066 | if (IS_ERR_OR_NULL(sta)) { |
| 1067 | IWL_ERR(mvm, "Invalid station id\n"); |
| 1068 | return -EINVAL; |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv; |
| 1073 | if (WARN_ON_ONCE(mvm_sta->vif != vif)) |
| 1074 | return -EINVAL; |
| 1075 | |
| 1076 | if (!have_key_offset) { |
| 1077 | /* |
| 1078 | * The D3 firmware hardcodes the PTK offset to 0, so we have to |
| 1079 | * configure it there. As a result, this workaround exists to |
| 1080 | * let the caller set the key offset (hw_key_idx), see d3.c. |
| 1081 | */ |
| 1082 | keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm); |
| 1083 | if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID) |
| 1084 | return -ENOSPC; |
| 1085 | } |
| 1086 | |
| 1087 | switch (keyconf->cipher) { |
| 1088 | case WLAN_CIPHER_SUITE_TKIP: |
| 1089 | addr = iwl_mvm_get_mac_addr(mvm, vif, sta); |
| 1090 | /* get phase 1 key from mac80211 */ |
| 1091 | ieee80211_get_key_rx_seq(keyconf, 0, &seq); |
| 1092 | ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k); |
| 1093 | ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id, |
| 1094 | seq.tkip.iv32, p1k, CMD_SYNC); |
| 1095 | break; |
| 1096 | case WLAN_CIPHER_SUITE_CCMP: |
| 1097 | ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id, |
| 1098 | 0, NULL, CMD_SYNC); |
| 1099 | break; |
| 1100 | default: |
| 1101 | IWL_ERR(mvm, "Unknown cipher %x\n", keyconf->cipher); |
| 1102 | ret = -EINVAL; |
| 1103 | } |
| 1104 | |
| 1105 | if (ret) |
| 1106 | __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table); |
| 1107 | |
| 1108 | end: |
| 1109 | IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n", |
| 1110 | keyconf->cipher, keyconf->keylen, keyconf->keyidx, |
| 1111 | sta->addr, ret); |
| 1112 | return ret; |
| 1113 | } |
| 1114 | |
| 1115 | int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, |
| 1116 | struct ieee80211_vif *vif, |
| 1117 | struct ieee80211_sta *sta, |
| 1118 | struct ieee80211_key_conf *keyconf) |
| 1119 | { |
| 1120 | struct iwl_mvm_sta *mvm_sta; |
| 1121 | struct iwl_mvm_add_sta_cmd cmd = {}; |
| 1122 | __le16 key_flags; |
| 1123 | int ret, status; |
| 1124 | u8 sta_id; |
| 1125 | |
| 1126 | lockdep_assert_held(&mvm->mutex); |
| 1127 | |
| 1128 | /* Get the station id from the mvm local station table */ |
| 1129 | sta_id = iwl_mvm_get_key_sta_id(vif, sta); |
| 1130 | |
| 1131 | IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n", |
| 1132 | keyconf->keyidx, sta_id); |
| 1133 | |
| 1134 | if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) |
| 1135 | return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true); |
| 1136 | |
| 1137 | ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table); |
| 1138 | if (!ret) { |
| 1139 | IWL_ERR(mvm, "offset %d not used in fw key table.\n", |
| 1140 | keyconf->hw_key_idx); |
| 1141 | return -ENOENT; |
| 1142 | } |
| 1143 | |
| 1144 | if (sta_id == IWL_INVALID_STATION) { |
| 1145 | IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n"); |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | /* |
| 1150 | * It is possible that the 'sta' parameter is NULL, and thus |
| 1151 | * there is a need to retrieve the sta from the local station table, |
| 1152 | * for example when a GTK is removed (where the sta_id will then be |
| 1153 | * the AP ID, and no station was passed by mac80211.) |
| 1154 | */ |
| 1155 | if (!sta) { |
| 1156 | sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id], |
| 1157 | lockdep_is_held(&mvm->mutex)); |
| 1158 | if (!sta) { |
| 1159 | IWL_ERR(mvm, "Invalid station id\n"); |
| 1160 | return -EINVAL; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv; |
| 1165 | if (WARN_ON_ONCE(mvm_sta->vif != vif)) |
| 1166 | return -EINVAL; |
| 1167 | |
Emmanuel Grumbach | 8115efb | 2013-02-05 10:08:35 +0200 | [diff] [blame] | 1168 | key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) & |
| 1169 | STA_KEY_FLG_KEYID_MSK); |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1170 | key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP); |
| 1171 | key_flags |= cpu_to_le16(STA_KEY_NOT_VALID); |
| 1172 | |
| 1173 | if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) |
| 1174 | key_flags |= cpu_to_le16(STA_KEY_MULTICAST); |
| 1175 | |
| 1176 | cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color); |
| 1177 | cmd.key.key_flags = key_flags; |
| 1178 | cmd.key.key_offset = keyconf->hw_key_idx; |
| 1179 | cmd.sta_id = sta_id; |
| 1180 | |
| 1181 | cmd.modify_mask = STA_MODIFY_KEY; |
| 1182 | cmd.add_modify = STA_MODE_MODIFY; |
| 1183 | |
| 1184 | status = ADD_STA_SUCCESS; |
| 1185 | ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd), |
| 1186 | &cmd, &status); |
| 1187 | |
| 1188 | switch (status) { |
| 1189 | case ADD_STA_SUCCESS: |
| 1190 | IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n"); |
| 1191 | break; |
| 1192 | default: |
| 1193 | ret = -EIO; |
| 1194 | IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n"); |
| 1195 | break; |
| 1196 | } |
| 1197 | |
| 1198 | return ret; |
| 1199 | } |
| 1200 | |
| 1201 | void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm, |
| 1202 | struct ieee80211_vif *vif, |
| 1203 | struct ieee80211_key_conf *keyconf, |
| 1204 | struct ieee80211_sta *sta, u32 iv32, |
| 1205 | u16 *phase1key) |
| 1206 | { |
Beni Lev | c3eb536 | 2013-02-06 17:22:18 +0200 | [diff] [blame] | 1207 | struct iwl_mvm_sta *mvm_sta; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1208 | u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta); |
| 1209 | |
Beni Lev | c3eb536 | 2013-02-06 17:22:18 +0200 | [diff] [blame] | 1210 | if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION)) |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1211 | return; |
| 1212 | |
Beni Lev | c3eb536 | 2013-02-06 17:22:18 +0200 | [diff] [blame] | 1213 | rcu_read_lock(); |
| 1214 | |
| 1215 | if (!sta) { |
| 1216 | sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]); |
| 1217 | if (WARN_ON(IS_ERR_OR_NULL(sta))) { |
| 1218 | rcu_read_unlock(); |
| 1219 | return; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | mvm_sta = (void *)sta->drv_priv; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1224 | iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id, |
| 1225 | iv32, phase1key, CMD_ASYNC); |
Beni Lev | c3eb536 | 2013-02-06 17:22:18 +0200 | [diff] [blame] | 1226 | rcu_read_unlock(); |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1227 | } |
| 1228 | |
Johannes Berg | 9cc4071 | 2013-02-15 22:47:48 +0100 | [diff] [blame] | 1229 | void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm, |
| 1230 | struct ieee80211_sta *sta) |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1231 | { |
Johannes Berg | 9cc4071 | 2013-02-15 22:47:48 +0100 | [diff] [blame] | 1232 | struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1233 | struct iwl_mvm_add_sta_cmd cmd = { |
| 1234 | .add_modify = STA_MODE_MODIFY, |
Johannes Berg | 9cc4071 | 2013-02-15 22:47:48 +0100 | [diff] [blame] | 1235 | .sta_id = mvmsta->sta_id, |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1236 | .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT, |
| 1237 | .sleep_state_flags = cpu_to_le16(STA_SLEEP_STATE_AWAKE), |
Johannes Berg | 9cc4071 | 2013-02-15 22:47:48 +0100 | [diff] [blame] | 1238 | .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color), |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1239 | }; |
| 1240 | int ret; |
| 1241 | |
| 1242 | /* |
| 1243 | * Same modify mask for sleep_tx_count and sleep_state_flags but this |
| 1244 | * should be fine since if we set the STA as "awake", then |
| 1245 | * sleep_tx_count is not relevant. |
| 1246 | */ |
| 1247 | ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd); |
| 1248 | if (ret) |
| 1249 | IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret); |
| 1250 | } |
| 1251 | |
Johannes Berg | 9cc4071 | 2013-02-15 22:47:48 +0100 | [diff] [blame] | 1252 | void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm, |
| 1253 | struct ieee80211_sta *sta, |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1254 | enum ieee80211_frame_release_type reason, |
| 1255 | u16 cnt) |
| 1256 | { |
| 1257 | u16 sleep_state_flags = |
| 1258 | (reason == IEEE80211_FRAME_RELEASE_UAPSD) ? |
| 1259 | STA_SLEEP_STATE_UAPSD : STA_SLEEP_STATE_PS_POLL; |
Johannes Berg | 9cc4071 | 2013-02-15 22:47:48 +0100 | [diff] [blame] | 1260 | struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1261 | struct iwl_mvm_add_sta_cmd cmd = { |
| 1262 | .add_modify = STA_MODE_MODIFY, |
Johannes Berg | 9cc4071 | 2013-02-15 22:47:48 +0100 | [diff] [blame] | 1263 | .sta_id = mvmsta->sta_id, |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1264 | .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT, |
| 1265 | .sleep_tx_count = cpu_to_le16(cnt), |
Johannes Berg | 9cc4071 | 2013-02-15 22:47:48 +0100 | [diff] [blame] | 1266 | .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color), |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1267 | /* |
| 1268 | * Same modify mask for sleep_tx_count and sleep_state_flags so |
| 1269 | * we must set the sleep_state_flags too. |
| 1270 | */ |
| 1271 | .sleep_state_flags = cpu_to_le16(sleep_state_flags), |
| 1272 | }; |
| 1273 | int ret; |
| 1274 | |
| 1275 | /* TODO: somehow the fw doesn't seem to take PS_POLL into account */ |
| 1276 | ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd); |
| 1277 | if (ret) |
| 1278 | IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret); |
| 1279 | } |