]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/iwlwifi/mvm/sta.c
Merge remote-tracking branch 'wireless-next/master'
[karo-tx-linux.git] / drivers / net / wireless / iwlwifi / mvm / sta.c
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
25  * in the file called COPYING.
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 #include "rs.h"
68
69 static void iwl_mvm_add_sta_cmd_v6_to_v5(struct iwl_mvm_add_sta_cmd_v6 *cmd_v6,
70                                          struct iwl_mvm_add_sta_cmd_v5 *cmd_v5)
71 {
72         memset(cmd_v5, 0, sizeof(*cmd_v5));
73
74         cmd_v5->add_modify = cmd_v6->add_modify;
75         cmd_v5->tid_disable_tx = cmd_v6->tid_disable_tx;
76         cmd_v5->mac_id_n_color = cmd_v6->mac_id_n_color;
77         memcpy(cmd_v5->addr, cmd_v6->addr, ETH_ALEN);
78         cmd_v5->sta_id = cmd_v6->sta_id;
79         cmd_v5->modify_mask = cmd_v6->modify_mask;
80         cmd_v5->station_flags = cmd_v6->station_flags;
81         cmd_v5->station_flags_msk = cmd_v6->station_flags_msk;
82         cmd_v5->add_immediate_ba_tid = cmd_v6->add_immediate_ba_tid;
83         cmd_v5->remove_immediate_ba_tid = cmd_v6->remove_immediate_ba_tid;
84         cmd_v5->add_immediate_ba_ssn = cmd_v6->add_immediate_ba_ssn;
85         cmd_v5->sleep_tx_count = cmd_v6->sleep_tx_count;
86         cmd_v5->sleep_state_flags = cmd_v6->sleep_state_flags;
87         cmd_v5->assoc_id = cmd_v6->assoc_id;
88         cmd_v5->beamform_flags = cmd_v6->beamform_flags;
89         cmd_v5->tfd_queue_msk = cmd_v6->tfd_queue_msk;
90 }
91
92 static void
93 iwl_mvm_add_sta_key_to_add_sta_cmd_v5(struct iwl_mvm_add_sta_key_cmd *key_cmd,
94                                       struct iwl_mvm_add_sta_cmd_v5 *sta_cmd,
95                                       u32 mac_id_n_color)
96 {
97         memset(sta_cmd, 0, sizeof(*sta_cmd));
98
99         sta_cmd->sta_id = key_cmd->sta_id;
100         sta_cmd->add_modify = STA_MODE_MODIFY;
101         sta_cmd->modify_mask = STA_MODIFY_KEY;
102         sta_cmd->mac_id_n_color = cpu_to_le32(mac_id_n_color);
103
104         sta_cmd->key.key_offset = key_cmd->key_offset;
105         sta_cmd->key.key_flags = key_cmd->key_flags;
106         memcpy(sta_cmd->key.key, key_cmd->key, sizeof(sta_cmd->key.key));
107         sta_cmd->key.tkip_rx_tsc_byte2 = key_cmd->tkip_rx_tsc_byte2;
108         memcpy(sta_cmd->key.tkip_rx_ttak, key_cmd->tkip_rx_ttak,
109                sizeof(sta_cmd->key.tkip_rx_ttak));
110 }
111
112 static int iwl_mvm_send_add_sta_cmd_status(struct iwl_mvm *mvm,
113                                            struct iwl_mvm_add_sta_cmd_v6 *cmd,
114                                            int *status)
115 {
116         struct iwl_mvm_add_sta_cmd_v5 cmd_v5;
117
118         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_STA_KEY_CMD)
119                 return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(*cmd),
120                                                    cmd, status);
121
122         iwl_mvm_add_sta_cmd_v6_to_v5(cmd, &cmd_v5);
123
124         return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd_v5),
125                                            &cmd_v5, status);
126 }
127
128 static int iwl_mvm_send_add_sta_cmd(struct iwl_mvm *mvm, u32 flags,
129                                     struct iwl_mvm_add_sta_cmd_v6 *cmd)
130 {
131         struct iwl_mvm_add_sta_cmd_v5 cmd_v5;
132
133         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_STA_KEY_CMD)
134                 return iwl_mvm_send_cmd_pdu(mvm, ADD_STA, flags,
135                                             sizeof(*cmd), cmd);
136
137         iwl_mvm_add_sta_cmd_v6_to_v5(cmd, &cmd_v5);
138
139         return iwl_mvm_send_cmd_pdu(mvm, ADD_STA, flags, sizeof(cmd_v5),
140                                     &cmd_v5);
141 }
142
143 static int
144 iwl_mvm_send_add_sta_key_cmd_status(struct iwl_mvm *mvm,
145                                     struct iwl_mvm_add_sta_key_cmd *cmd,
146                                     u32 mac_id_n_color,
147                                     int *status)
148 {
149         struct iwl_mvm_add_sta_cmd_v5 sta_cmd;
150
151         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_STA_KEY_CMD)
152                 return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY,
153                                                    sizeof(*cmd), cmd, status);
154
155         iwl_mvm_add_sta_key_to_add_sta_cmd_v5(cmd, &sta_cmd, mac_id_n_color);
156
157         return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(sta_cmd),
158                                            &sta_cmd, status);
159 }
160
161 static int iwl_mvm_send_add_sta_key_cmd(struct iwl_mvm *mvm,
162                                         u32 flags,
163                                         struct iwl_mvm_add_sta_key_cmd *cmd,
164                                         u32 mac_id_n_color)
165 {
166         struct iwl_mvm_add_sta_cmd_v5 sta_cmd;
167
168         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_STA_KEY_CMD)
169                 return iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, flags,
170                                             sizeof(*cmd), cmd);
171
172         iwl_mvm_add_sta_key_to_add_sta_cmd_v5(cmd, &sta_cmd, mac_id_n_color);
173
174         return iwl_mvm_send_cmd_pdu(mvm, ADD_STA, flags, sizeof(sta_cmd),
175                                     &sta_cmd);
176 }
177
178 static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm)
179 {
180         int sta_id;
181
182         WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
183
184         lockdep_assert_held(&mvm->mutex);
185
186         /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
187         for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++)
188                 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
189                                                lockdep_is_held(&mvm->mutex)))
190                         return sta_id;
191         return IWL_MVM_STATION_COUNT;
192 }
193
194 /* send station add/update command to firmware */
195 int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
196                            bool update)
197 {
198         struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
199         struct iwl_mvm_add_sta_cmd_v6 add_sta_cmd;
200         int ret;
201         u32 status;
202         u32 agg_size = 0, mpdu_dens = 0;
203
204         memset(&add_sta_cmd, 0, sizeof(add_sta_cmd));
205
206         add_sta_cmd.sta_id = mvm_sta->sta_id;
207         add_sta_cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
208         if (!update) {
209                 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
210                 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
211         }
212         add_sta_cmd.add_modify = update ? 1 : 0;
213
214         add_sta_cmd.station_flags_msk |= cpu_to_le32(STA_FLG_FAT_EN_MSK |
215                                                      STA_FLG_MIMO_EN_MSK);
216
217         switch (sta->bandwidth) {
218         case IEEE80211_STA_RX_BW_160:
219                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
220                 /* fall through */
221         case IEEE80211_STA_RX_BW_80:
222                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
223                 /* fall through */
224         case IEEE80211_STA_RX_BW_40:
225                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
226                 /* fall through */
227         case IEEE80211_STA_RX_BW_20:
228                 if (sta->ht_cap.ht_supported)
229                         add_sta_cmd.station_flags |=
230                                 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
231                 break;
232         }
233
234         switch (sta->rx_nss) {
235         case 1:
236                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
237                 break;
238         case 2:
239                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
240                 break;
241         case 3 ... 8:
242                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
243                 break;
244         }
245
246         switch (sta->smps_mode) {
247         case IEEE80211_SMPS_AUTOMATIC:
248         case IEEE80211_SMPS_NUM_MODES:
249                 WARN_ON(1);
250                 break;
251         case IEEE80211_SMPS_STATIC:
252                 /* override NSS */
253                 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
254                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
255                 break;
256         case IEEE80211_SMPS_DYNAMIC:
257                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
258                 break;
259         case IEEE80211_SMPS_OFF:
260                 /* nothing */
261                 break;
262         }
263
264         if (sta->ht_cap.ht_supported) {
265                 add_sta_cmd.station_flags_msk |=
266                         cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
267                                     STA_FLG_AGG_MPDU_DENS_MSK);
268
269                 mpdu_dens = sta->ht_cap.ampdu_density;
270         }
271
272         if (sta->vht_cap.vht_supported) {
273                 agg_size = sta->vht_cap.cap &
274                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
275                 agg_size >>=
276                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
277         } else if (sta->ht_cap.ht_supported) {
278                 agg_size = sta->ht_cap.ampdu_factor;
279         }
280
281         add_sta_cmd.station_flags |=
282                 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
283         add_sta_cmd.station_flags |=
284                 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
285
286         status = ADD_STA_SUCCESS;
287         ret = iwl_mvm_send_add_sta_cmd_status(mvm, &add_sta_cmd, &status);
288         if (ret)
289                 return ret;
290
291         switch (status) {
292         case ADD_STA_SUCCESS:
293                 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
294                 break;
295         default:
296                 ret = -EIO;
297                 IWL_ERR(mvm, "ADD_STA failed\n");
298                 break;
299         }
300
301         return ret;
302 }
303
304 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
305                     struct ieee80211_vif *vif,
306                     struct ieee80211_sta *sta)
307 {
308         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
309         struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
310         int i, ret, sta_id;
311
312         lockdep_assert_held(&mvm->mutex);
313
314         if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
315                 sta_id = iwl_mvm_find_free_sta_id(mvm);
316         else
317                 sta_id = mvm_sta->sta_id;
318
319         if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
320                 return -ENOSPC;
321
322         spin_lock_init(&mvm_sta->lock);
323
324         mvm_sta->sta_id = sta_id;
325         mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
326                                                       mvmvif->color);
327         mvm_sta->vif = vif;
328         mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
329         mvm_sta->tx_protection = 0;
330         mvm_sta->tt_tx_protection = false;
331
332         /* HW restart, don't assume the memory has been zeroed */
333         atomic_set(&mvm->pending_frames[sta_id], 0);
334         mvm_sta->tid_disable_agg = 0;
335         mvm_sta->tfd_queue_msk = 0;
336         for (i = 0; i < IEEE80211_NUM_ACS; i++)
337                 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
338                         mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
339
340         /* for HW restart - reset everything but the sequence number */
341         for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
342                 u16 seq = mvm_sta->tid_data[i].seq_number;
343                 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
344                 mvm_sta->tid_data[i].seq_number = seq;
345         }
346
347         ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
348         if (ret)
349                 return ret;
350
351         /* The first station added is the AP, the others are TDLS STAs */
352         if (vif->type == NL80211_IFTYPE_STATION &&
353             mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
354                 mvmvif->ap_sta_id = sta_id;
355
356         rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
357
358         return 0;
359 }
360
361 int iwl_mvm_update_sta(struct iwl_mvm *mvm,
362                        struct ieee80211_vif *vif,
363                        struct ieee80211_sta *sta)
364 {
365         return iwl_mvm_sta_send_to_fw(mvm, sta, true);
366 }
367
368 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
369                       bool drain)
370 {
371         struct iwl_mvm_add_sta_cmd_v6 cmd = {};
372         int ret;
373         u32 status;
374
375         lockdep_assert_held(&mvm->mutex);
376
377         cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
378         cmd.sta_id = mvmsta->sta_id;
379         cmd.add_modify = STA_MODE_MODIFY;
380         cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
381         cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
382
383         status = ADD_STA_SUCCESS;
384         ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
385         if (ret)
386                 return ret;
387
388         switch (status) {
389         case ADD_STA_SUCCESS:
390                 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
391                                mvmsta->sta_id);
392                 break;
393         default:
394                 ret = -EIO;
395                 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
396                         mvmsta->sta_id);
397                 break;
398         }
399
400         return ret;
401 }
402
403 /*
404  * Remove a station from the FW table. Before sending the command to remove
405  * the station validate that the station is indeed known to the driver (sanity
406  * only).
407  */
408 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
409 {
410         struct ieee80211_sta *sta;
411         struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
412                 .sta_id = sta_id,
413         };
414         int ret;
415
416         sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
417                                         lockdep_is_held(&mvm->mutex));
418
419         /* Note: internal stations are marked as error values */
420         if (!sta) {
421                 IWL_ERR(mvm, "Invalid station id\n");
422                 return -EINVAL;
423         }
424
425         ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, CMD_SYNC,
426                                    sizeof(rm_sta_cmd), &rm_sta_cmd);
427         if (ret) {
428                 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
429                 return ret;
430         }
431
432         return 0;
433 }
434
435 void iwl_mvm_sta_drained_wk(struct work_struct *wk)
436 {
437         struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
438         u8 sta_id;
439
440         /*
441          * The mutex is needed because of the SYNC cmd, but not only: if the
442          * work would run concurrently with iwl_mvm_rm_sta, it would run before
443          * iwl_mvm_rm_sta sets the station as busy, and exit. Then
444          * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
445          * that later.
446          */
447         mutex_lock(&mvm->mutex);
448
449         for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
450                 int ret;
451                 struct ieee80211_sta *sta =
452                         rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
453                                                   lockdep_is_held(&mvm->mutex));
454
455                 /* This station is in use */
456                 if (!IS_ERR(sta))
457                         continue;
458
459                 if (PTR_ERR(sta) == -EINVAL) {
460                         IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
461                                 sta_id);
462                         continue;
463                 }
464
465                 if (!sta) {
466                         IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
467                                 sta_id);
468                         continue;
469                 }
470
471                 WARN_ON(PTR_ERR(sta) != -EBUSY);
472                 /* This station was removed and we waited until it got drained,
473                  * we can now proceed and remove it.
474                  */
475                 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
476                 if (ret) {
477                         IWL_ERR(mvm,
478                                 "Couldn't remove sta %d after it was drained\n",
479                                 sta_id);
480                         continue;
481                 }
482                 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
483                 clear_bit(sta_id, mvm->sta_drained);
484         }
485
486         mutex_unlock(&mvm->mutex);
487 }
488
489 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
490                    struct ieee80211_vif *vif,
491                    struct ieee80211_sta *sta)
492 {
493         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
494         struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
495         int ret;
496
497         lockdep_assert_held(&mvm->mutex);
498
499         if (vif->type == NL80211_IFTYPE_STATION &&
500             mvmvif->ap_sta_id == mvm_sta->sta_id) {
501                 /* flush its queues here since we are freeing mvm_sta */
502                 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
503
504                 /*
505                  * Put a non-NULL since the fw station isn't removed.
506                  * It will be removed after the MAC will be set as
507                  * unassoc.
508                  */
509                 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
510                                    ERR_PTR(-EINVAL));
511
512                 /* if we are associated - we can't remove the AP STA now */
513                 if (vif->bss_conf.assoc)
514                         return ret;
515
516                 /* unassoc - go ahead - remove the AP STA now */
517                 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
518         }
519
520         /*
521          * Make sure that the tx response code sees the station as -EBUSY and
522          * calls the drain worker.
523          */
524         spin_lock_bh(&mvm_sta->lock);
525         /*
526          * There are frames pending on the AC queues for this station.
527          * We need to wait until all the frames are drained...
528          */
529         if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
530                 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
531                                    ERR_PTR(-EBUSY));
532                 spin_unlock_bh(&mvm_sta->lock);
533                 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
534         } else {
535                 spin_unlock_bh(&mvm_sta->lock);
536                 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
537                 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
538         }
539
540         return ret;
541 }
542
543 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
544                       struct ieee80211_vif *vif,
545                       u8 sta_id)
546 {
547         int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
548
549         lockdep_assert_held(&mvm->mutex);
550
551         rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
552         return ret;
553 }
554
555 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta,
556                              u32 qmask)
557 {
558         if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
559                 sta->sta_id = iwl_mvm_find_free_sta_id(mvm);
560                 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
561                         return -ENOSPC;
562         }
563
564         sta->tfd_queue_msk = qmask;
565
566         /* put a non-NULL value so iterating over the stations won't stop */
567         rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
568         return 0;
569 }
570
571 void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
572 {
573         rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
574         memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
575         sta->sta_id = IWL_MVM_STATION_COUNT;
576 }
577
578 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
579                                       struct iwl_mvm_int_sta *sta,
580                                       const u8 *addr,
581                                       u16 mac_id, u16 color)
582 {
583         struct iwl_mvm_add_sta_cmd_v6 cmd;
584         int ret;
585         u32 status;
586
587         lockdep_assert_held(&mvm->mutex);
588
589         memset(&cmd, 0, sizeof(struct iwl_mvm_add_sta_cmd_v6));
590         cmd.sta_id = sta->sta_id;
591         cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
592                                                              color));
593
594         cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
595
596         if (addr)
597                 memcpy(cmd.addr, addr, ETH_ALEN);
598
599         ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
600         if (ret)
601                 return ret;
602
603         switch (status) {
604         case ADD_STA_SUCCESS:
605                 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
606                 return 0;
607         default:
608                 ret = -EIO;
609                 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
610                         status);
611                 break;
612         }
613         return ret;
614 }
615
616 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
617 {
618         int ret;
619
620         lockdep_assert_held(&mvm->mutex);
621
622         /* Add the aux station, but without any queues */
623         ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, 0);
624         if (ret)
625                 return ret;
626
627         ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
628                                          MAC_INDEX_AUX, 0);
629
630         if (ret)
631                 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
632         return ret;
633 }
634
635 /*
636  * Send the add station command for the vif's broadcast station.
637  * Assumes that the station was already allocated.
638  *
639  * @mvm: the mvm component
640  * @vif: the interface to which the broadcast station is added
641  * @bsta: the broadcast station to add.
642  */
643 int iwl_mvm_send_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
644                            struct iwl_mvm_int_sta *bsta)
645 {
646         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
647         static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
648         static const u8 *baddr = _baddr;
649
650         lockdep_assert_held(&mvm->mutex);
651
652         if (vif->type == NL80211_IFTYPE_ADHOC)
653                 baddr = vif->bss_conf.bssid;
654
655         if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
656                 return -ENOSPC;
657
658         return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
659                                           mvmvif->id, mvmvif->color);
660 }
661
662 /* Send the FW a request to remove the station from it's internal data
663  * structures, but DO NOT remove the entry from the local data structures. */
664 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm,
665                               struct iwl_mvm_int_sta *bsta)
666 {
667         int ret;
668
669         lockdep_assert_held(&mvm->mutex);
670
671         ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
672         if (ret)
673                 IWL_WARN(mvm, "Failed sending remove station\n");
674         return ret;
675 }
676
677 /* Allocate a new station entry for the broadcast station to the given vif,
678  * and send it to the FW.
679  * Note that each P2P mac should have its own broadcast station.
680  *
681  * @mvm: the mvm component
682  * @vif: the interface to which the broadcast station is added
683  * @bsta: the broadcast station to add. */
684 int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
685                           struct iwl_mvm_int_sta *bsta)
686 {
687         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
688         static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
689         u32 qmask;
690         int ret;
691
692         lockdep_assert_held(&mvm->mutex);
693
694         qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
695         ret = iwl_mvm_allocate_int_sta(mvm, bsta, qmask);
696         if (ret)
697                 return ret;
698
699         ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
700                                          mvmvif->id, mvmvif->color);
701
702         if (ret)
703                 iwl_mvm_dealloc_int_sta(mvm, bsta);
704         return ret;
705 }
706
707 /*
708  * Send the FW a request to remove the station from it's internal data
709  * structures, and in addition remove it from the local data structure.
710  */
711 int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *bsta)
712 {
713         int ret;
714
715         lockdep_assert_held(&mvm->mutex);
716
717         ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
718         if (ret)
719                 return ret;
720
721         iwl_mvm_dealloc_int_sta(mvm, bsta);
722         return ret;
723 }
724
725 #define IWL_MAX_RX_BA_SESSIONS 16
726
727 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
728                        int tid, u16 ssn, bool start)
729 {
730         struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
731         struct iwl_mvm_add_sta_cmd_v6 cmd = {};
732         int ret;
733         u32 status;
734
735         lockdep_assert_held(&mvm->mutex);
736
737         if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
738                 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
739                 return -ENOSPC;
740         }
741
742         cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
743         cmd.sta_id = mvm_sta->sta_id;
744         cmd.add_modify = STA_MODE_MODIFY;
745         if (start) {
746                 cmd.add_immediate_ba_tid = (u8) tid;
747                 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
748         } else {
749                 cmd.remove_immediate_ba_tid = (u8) tid;
750         }
751         cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
752                                   STA_MODIFY_REMOVE_BA_TID;
753
754         status = ADD_STA_SUCCESS;
755         ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
756         if (ret)
757                 return ret;
758
759         switch (status) {
760         case ADD_STA_SUCCESS:
761                 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
762                                start ? "start" : "stopp");
763                 break;
764         case ADD_STA_IMMEDIATE_BA_FAILURE:
765                 IWL_WARN(mvm, "RX BA Session refused by fw\n");
766                 ret = -ENOSPC;
767                 break;
768         default:
769                 ret = -EIO;
770                 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
771                         start ? "start" : "stopp", status);
772                 break;
773         }
774
775         if (!ret) {
776                 if (start)
777                         mvm->rx_ba_sessions++;
778                 else if (mvm->rx_ba_sessions > 0)
779                         /* check that restart flow didn't zero the counter */
780                         mvm->rx_ba_sessions--;
781         }
782
783         return ret;
784 }
785
786 static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
787                               int tid, u8 queue, bool start)
788 {
789         struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
790         struct iwl_mvm_add_sta_cmd_v6 cmd = {};
791         int ret;
792         u32 status;
793
794         lockdep_assert_held(&mvm->mutex);
795
796         if (start) {
797                 mvm_sta->tfd_queue_msk |= BIT(queue);
798                 mvm_sta->tid_disable_agg &= ~BIT(tid);
799         } else {
800                 mvm_sta->tfd_queue_msk &= ~BIT(queue);
801                 mvm_sta->tid_disable_agg |= BIT(tid);
802         }
803
804         cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
805         cmd.sta_id = mvm_sta->sta_id;
806         cmd.add_modify = STA_MODE_MODIFY;
807         cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
808         cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
809         cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
810
811         status = ADD_STA_SUCCESS;
812         ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
813         if (ret)
814                 return ret;
815
816         switch (status) {
817         case ADD_STA_SUCCESS:
818                 break;
819         default:
820                 ret = -EIO;
821                 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
822                         start ? "start" : "stopp", status);
823                 break;
824         }
825
826         return ret;
827 }
828
829 static const u8 tid_to_ac[] = {
830         IEEE80211_AC_BE,
831         IEEE80211_AC_BK,
832         IEEE80211_AC_BK,
833         IEEE80211_AC_BE,
834         IEEE80211_AC_VI,
835         IEEE80211_AC_VI,
836         IEEE80211_AC_VO,
837         IEEE80211_AC_VO,
838 };
839
840 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
841                              struct ieee80211_sta *sta, u16 tid, u16 *ssn)
842 {
843         struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
844         struct iwl_mvm_tid_data *tid_data;
845         int txq_id;
846
847         if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
848                 return -EINVAL;
849
850         if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
851                 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
852                         mvmsta->tid_data[tid].state);
853                 return -ENXIO;
854         }
855
856         lockdep_assert_held(&mvm->mutex);
857
858         for (txq_id = mvm->first_agg_queue;
859              txq_id <= mvm->last_agg_queue; txq_id++)
860                 if (mvm->queue_to_mac80211[txq_id] ==
861                     IWL_INVALID_MAC80211_QUEUE)
862                         break;
863
864         if (txq_id > mvm->last_agg_queue) {
865                 IWL_ERR(mvm, "Failed to allocate agg queue\n");
866                 return -EIO;
867         }
868
869         /* the new tx queue is still connected to the same mac80211 queue */
870         mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_ac[tid]];
871
872         spin_lock_bh(&mvmsta->lock);
873         tid_data = &mvmsta->tid_data[tid];
874         tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
875         tid_data->txq_id = txq_id;
876         *ssn = tid_data->ssn;
877
878         IWL_DEBUG_TX_QUEUES(mvm,
879                             "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
880                             mvmsta->sta_id, tid, txq_id, tid_data->ssn,
881                             tid_data->next_reclaimed);
882
883         if (tid_data->ssn == tid_data->next_reclaimed) {
884                 tid_data->state = IWL_AGG_STARTING;
885                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
886         } else {
887                 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
888         }
889
890         spin_unlock_bh(&mvmsta->lock);
891
892         return 0;
893 }
894
895 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
896                             struct ieee80211_sta *sta, u16 tid, u8 buf_size)
897 {
898         struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
899         struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
900         int queue, fifo, ret;
901         u16 ssn;
902
903         buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
904
905         spin_lock_bh(&mvmsta->lock);
906         ssn = tid_data->ssn;
907         queue = tid_data->txq_id;
908         tid_data->state = IWL_AGG_ON;
909         tid_data->ssn = 0xffff;
910         spin_unlock_bh(&mvmsta->lock);
911
912         fifo = iwl_mvm_ac_to_tx_fifo[tid_to_ac[tid]];
913
914         ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
915         if (ret)
916                 return -EIO;
917
918         iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid,
919                              buf_size, ssn);
920
921         /*
922          * Even though in theory the peer could have different
923          * aggregation reorder buffer sizes for different sessions,
924          * our ucode doesn't allow for that and has a global limit
925          * for each station. Therefore, use the minimum of all the
926          * aggregation sessions and our default value.
927          */
928         mvmsta->max_agg_bufsize =
929                 min(mvmsta->max_agg_bufsize, buf_size);
930         mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
931
932         IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
933                      sta->addr, tid);
934
935         if (mvm->cfg->ht_params->use_rts_for_aggregation) {
936                 /*
937                  * switch to RTS/CTS if it is the prefer protection
938                  * method for HT traffic
939                  * this function also sends the LQ command
940                  */
941                 return iwl_mvm_tx_protection(mvm, mvmsta, true);
942                 /*
943                  * TODO: remove the TLC_RTS flag when we tear down the last
944                  * AGG session (agg_tids_count in DVM)
945                  */
946         }
947
948         return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, CMD_ASYNC, false);
949 }
950
951 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
952                             struct ieee80211_sta *sta, u16 tid)
953 {
954         struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
955         struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
956         u16 txq_id;
957         int err;
958
959
960         /*
961          * If mac80211 is cleaning its state, then say that we finished since
962          * our state has been cleared anyway.
963          */
964         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
965                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
966                 return 0;
967         }
968
969         spin_lock_bh(&mvmsta->lock);
970
971         txq_id = tid_data->txq_id;
972
973         IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
974                             mvmsta->sta_id, tid, txq_id, tid_data->state);
975
976         switch (tid_data->state) {
977         case IWL_AGG_ON:
978                 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
979
980                 IWL_DEBUG_TX_QUEUES(mvm,
981                                     "ssn = %d, next_recl = %d\n",
982                                     tid_data->ssn, tid_data->next_reclaimed);
983
984                 /* There are still packets for this RA / TID in the HW */
985                 if (tid_data->ssn != tid_data->next_reclaimed) {
986                         tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
987                         err = 0;
988                         break;
989                 }
990
991                 tid_data->ssn = 0xffff;
992                 iwl_trans_txq_disable(mvm->trans, txq_id);
993                 /* fall through */
994         case IWL_AGG_STARTING:
995         case IWL_EMPTYING_HW_QUEUE_ADDBA:
996                 /*
997                  * The agg session has been stopped before it was set up. This
998                  * can happen when the AddBA timer times out for example.
999                  */
1000
1001                 /* No barriers since we are under mutex */
1002                 lockdep_assert_held(&mvm->mutex);
1003                 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
1004
1005                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1006                 tid_data->state = IWL_AGG_OFF;
1007                 err = 0;
1008                 break;
1009         default:
1010                 IWL_ERR(mvm,
1011                         "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1012                         mvmsta->sta_id, tid, tid_data->state);
1013                 IWL_ERR(mvm,
1014                         "\ttid_data->txq_id = %d\n", tid_data->txq_id);
1015                 err = -EINVAL;
1016         }
1017
1018         spin_unlock_bh(&mvmsta->lock);
1019
1020         return err;
1021 }
1022
1023 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1024                             struct ieee80211_sta *sta, u16 tid)
1025 {
1026         struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
1027         struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1028         u16 txq_id;
1029         enum iwl_mvm_agg_state old_state;
1030
1031         /*
1032          * First set the agg state to OFF to avoid calling
1033          * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1034          */
1035         spin_lock_bh(&mvmsta->lock);
1036         txq_id = tid_data->txq_id;
1037         IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1038                             mvmsta->sta_id, tid, txq_id, tid_data->state);
1039         old_state = tid_data->state;
1040         tid_data->state = IWL_AGG_OFF;
1041         spin_unlock_bh(&mvmsta->lock);
1042
1043         if (old_state >= IWL_AGG_ON) {
1044                 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
1045                         IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
1046
1047                 iwl_trans_txq_disable(mvm->trans, tid_data->txq_id);
1048         }
1049
1050         mvm->queue_to_mac80211[tid_data->txq_id] =
1051                                 IWL_INVALID_MAC80211_QUEUE;
1052
1053         return 0;
1054 }
1055
1056 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1057 {
1058         int i;
1059
1060         lockdep_assert_held(&mvm->mutex);
1061
1062         i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
1063
1064         if (i == STA_KEY_MAX_NUM)
1065                 return STA_KEY_IDX_INVALID;
1066
1067         __set_bit(i, mvm->fw_key_table);
1068
1069         return i;
1070 }
1071
1072 static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
1073                                  struct ieee80211_sta *sta)
1074 {
1075         struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1076
1077         if (sta) {
1078                 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1079
1080                 return mvm_sta->sta_id;
1081         }
1082
1083         /*
1084          * The device expects GTKs for station interfaces to be
1085          * installed as GTKs for the AP station. If we have no
1086          * station ID, then use AP's station ID.
1087          */
1088         if (vif->type == NL80211_IFTYPE_STATION &&
1089             mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
1090                 return mvmvif->ap_sta_id;
1091
1092         return IWL_MVM_STATION_COUNT;
1093 }
1094
1095 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1096                                 struct iwl_mvm_sta *mvm_sta,
1097                                 struct ieee80211_key_conf *keyconf,
1098                                 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1099                                 u32 cmd_flags)
1100 {
1101         __le16 key_flags;
1102         struct iwl_mvm_add_sta_key_cmd cmd = {};
1103         int ret, status;
1104         u16 keyidx;
1105         int i;
1106         u32 mac_id_n_color = mvm_sta->mac_id_n_color;
1107
1108         keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1109                  STA_KEY_FLG_KEYID_MSK;
1110         key_flags = cpu_to_le16(keyidx);
1111         key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1112
1113         switch (keyconf->cipher) {
1114         case WLAN_CIPHER_SUITE_TKIP:
1115                 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
1116                 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
1117                 for (i = 0; i < 5; i++)
1118                         cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1119                 memcpy(cmd.key, keyconf->key, keyconf->keylen);
1120                 break;
1121         case WLAN_CIPHER_SUITE_CCMP:
1122                 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
1123                 memcpy(cmd.key, keyconf->key, keyconf->keylen);
1124                 break;
1125         default:
1126                 WARN_ON(1);
1127                 return -EINVAL;
1128         }
1129
1130         if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1131                 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1132
1133         cmd.key_offset = keyconf->hw_key_idx;
1134         cmd.key_flags = key_flags;
1135         cmd.sta_id = sta_id;
1136
1137         status = ADD_STA_SUCCESS;
1138         if (cmd_flags == CMD_SYNC)
1139                 ret = iwl_mvm_send_add_sta_key_cmd_status(mvm, &cmd,
1140                                                           mac_id_n_color,
1141                                                           &status);
1142         else
1143                 ret = iwl_mvm_send_add_sta_key_cmd(mvm, CMD_ASYNC, &cmd,
1144                                                    mac_id_n_color);
1145
1146         switch (status) {
1147         case ADD_STA_SUCCESS:
1148                 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1149                 break;
1150         default:
1151                 ret = -EIO;
1152                 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1153                 break;
1154         }
1155
1156         return ret;
1157 }
1158
1159 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1160                                  struct ieee80211_key_conf *keyconf,
1161                                  u8 sta_id, bool remove_key)
1162 {
1163         struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1164
1165         /* verify the key details match the required command's expectations */
1166         if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1167                     (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1168                     (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1169                 return -EINVAL;
1170
1171         igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1172         igtk_cmd.sta_id = cpu_to_le32(sta_id);
1173
1174         if (remove_key) {
1175                 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1176         } else {
1177                 struct ieee80211_key_seq seq;
1178                 const u8 *pn;
1179
1180                 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
1181                 ieee80211_aes_cmac_calculate_k1_k2(keyconf,
1182                                                    igtk_cmd.K1, igtk_cmd.K2);
1183                 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1184                 pn = seq.aes_cmac.pn;
1185                 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1186                                                        ((u64) pn[4] << 8) |
1187                                                        ((u64) pn[3] << 16) |
1188                                                        ((u64) pn[2] << 24) |
1189                                                        ((u64) pn[1] << 32) |
1190                                                        ((u64) pn[0] << 40));
1191         }
1192
1193         IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1194                        remove_key ? "removing" : "installing",
1195                        igtk_cmd.sta_id);
1196
1197         return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, CMD_SYNC,
1198                                     sizeof(igtk_cmd), &igtk_cmd);
1199 }
1200
1201
1202 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1203                                        struct ieee80211_vif *vif,
1204                                        struct ieee80211_sta *sta)
1205 {
1206         struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1207
1208         if (sta)
1209                 return sta->addr;
1210
1211         if (vif->type == NL80211_IFTYPE_STATION &&
1212             mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1213                 u8 sta_id = mvmvif->ap_sta_id;
1214                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1215                                                 lockdep_is_held(&mvm->mutex));
1216                 return sta->addr;
1217         }
1218
1219
1220         return NULL;
1221 }
1222
1223 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1224                         struct ieee80211_vif *vif,
1225                         struct ieee80211_sta *sta,
1226                         struct ieee80211_key_conf *keyconf,
1227                         bool have_key_offset)
1228 {
1229         struct iwl_mvm_sta *mvm_sta;
1230         int ret;
1231         u8 *addr, sta_id;
1232         struct ieee80211_key_seq seq;
1233         u16 p1k[5];
1234
1235         lockdep_assert_held(&mvm->mutex);
1236
1237         /* Get the station id from the mvm local station table */
1238         sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1239         if (sta_id == IWL_MVM_STATION_COUNT) {
1240                 IWL_ERR(mvm, "Failed to find station id\n");
1241                 return -EINVAL;
1242         }
1243
1244         if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1245                 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1246                 goto end;
1247         }
1248
1249         /*
1250          * It is possible that the 'sta' parameter is NULL, and thus
1251          * there is a need to retrieve  the sta from the local station table.
1252          */
1253         if (!sta) {
1254                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1255                                                 lockdep_is_held(&mvm->mutex));
1256                 if (IS_ERR_OR_NULL(sta)) {
1257                         IWL_ERR(mvm, "Invalid station id\n");
1258                         return -EINVAL;
1259                 }
1260         }
1261
1262         mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1263         if (WARN_ON_ONCE(mvm_sta->vif != vif))
1264                 return -EINVAL;
1265
1266         if (!have_key_offset) {
1267                 /*
1268                  * The D3 firmware hardcodes the PTK offset to 0, so we have to
1269                  * configure it there. As a result, this workaround exists to
1270                  * let the caller set the key offset (hw_key_idx), see d3.c.
1271                  */
1272                 keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1273                 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1274                         return -ENOSPC;
1275         }
1276
1277         switch (keyconf->cipher) {
1278         case WLAN_CIPHER_SUITE_TKIP:
1279                 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1280                 /* get phase 1 key from mac80211 */
1281                 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1282                 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1283                 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1284                                            seq.tkip.iv32, p1k, CMD_SYNC);
1285                 break;
1286         case WLAN_CIPHER_SUITE_CCMP:
1287                 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1288                                            0, NULL, CMD_SYNC);
1289                 break;
1290         default:
1291                 IWL_ERR(mvm, "Unknown cipher %x\n", keyconf->cipher);
1292                 ret = -EINVAL;
1293         }
1294
1295         if (ret)
1296                 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1297
1298 end:
1299         IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1300                       keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1301                       sta->addr, ret);
1302         return ret;
1303 }
1304
1305 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1306                            struct ieee80211_vif *vif,
1307                            struct ieee80211_sta *sta,
1308                            struct ieee80211_key_conf *keyconf)
1309 {
1310         struct iwl_mvm_sta *mvm_sta;
1311         struct iwl_mvm_add_sta_key_cmd cmd = {};
1312         __le16 key_flags;
1313         int ret, status;
1314         u8 sta_id;
1315
1316         lockdep_assert_held(&mvm->mutex);
1317
1318         /* Get the station id from the mvm local station table */
1319         sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1320
1321         IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1322                       keyconf->keyidx, sta_id);
1323
1324         if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1325                 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1326
1327         ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1328         if (!ret) {
1329                 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1330                         keyconf->hw_key_idx);
1331                 return -ENOENT;
1332         }
1333
1334         if (sta_id == IWL_MVM_STATION_COUNT) {
1335                 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1336                 return 0;
1337         }
1338
1339         /*
1340          * It is possible that the 'sta' parameter is NULL, and thus
1341          * there is a need to retrieve the sta from the local station table,
1342          * for example when a GTK is removed (where the sta_id will then be
1343          * the AP ID, and no station was passed by mac80211.)
1344          */
1345         if (!sta) {
1346                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1347                                                 lockdep_is_held(&mvm->mutex));
1348                 if (!sta) {
1349                         IWL_ERR(mvm, "Invalid station id\n");
1350                         return -EINVAL;
1351                 }
1352         }
1353
1354         mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1355         if (WARN_ON_ONCE(mvm_sta->vif != vif))
1356                 return -EINVAL;
1357
1358         key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1359                                  STA_KEY_FLG_KEYID_MSK);
1360         key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1361         key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1362
1363         if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1364                 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1365
1366         cmd.key_flags = key_flags;
1367         cmd.key_offset = keyconf->hw_key_idx;
1368         cmd.sta_id = sta_id;
1369
1370         status = ADD_STA_SUCCESS;
1371         ret = iwl_mvm_send_add_sta_key_cmd_status(mvm, &cmd,
1372                                                   mvm_sta->mac_id_n_color,
1373                                                   &status);
1374
1375         switch (status) {
1376         case ADD_STA_SUCCESS:
1377                 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1378                 break;
1379         default:
1380                 ret = -EIO;
1381                 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1382                 break;
1383         }
1384
1385         return ret;
1386 }
1387
1388 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1389                              struct ieee80211_vif *vif,
1390                              struct ieee80211_key_conf *keyconf,
1391                              struct ieee80211_sta *sta, u32 iv32,
1392                              u16 *phase1key)
1393 {
1394         struct iwl_mvm_sta *mvm_sta;
1395         u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1396
1397         if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
1398                 return;
1399
1400         rcu_read_lock();
1401
1402         if (!sta) {
1403                 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1404                 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1405                         rcu_read_unlock();
1406                         return;
1407                 }
1408         }
1409
1410         mvm_sta = (void *)sta->drv_priv;
1411         iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1412                              iv32, phase1key, CMD_ASYNC);
1413         rcu_read_unlock();
1414 }
1415
1416 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1417                                 struct ieee80211_sta *sta)
1418 {
1419         struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
1420         struct iwl_mvm_add_sta_cmd_v6 cmd = {
1421                 .add_modify = STA_MODE_MODIFY,
1422                 .sta_id = mvmsta->sta_id,
1423                 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
1424                 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1425         };
1426         int ret;
1427
1428         ret = iwl_mvm_send_add_sta_cmd(mvm, CMD_ASYNC, &cmd);
1429         if (ret)
1430                 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1431 }
1432
1433 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1434                                        struct ieee80211_sta *sta,
1435                                        enum ieee80211_frame_release_type reason,
1436                                        u16 cnt)
1437 {
1438         u16 sleep_state_flags =
1439                 (reason == IEEE80211_FRAME_RELEASE_UAPSD) ?
1440                         STA_SLEEP_STATE_UAPSD : STA_SLEEP_STATE_PS_POLL;
1441         struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
1442         struct iwl_mvm_add_sta_cmd_v6 cmd = {
1443                 .add_modify = STA_MODE_MODIFY,
1444                 .sta_id = mvmsta->sta_id,
1445                 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1446                 .sleep_tx_count = cpu_to_le16(cnt),
1447                 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1448                 /*
1449                  * Same modify mask for sleep_tx_count and sleep_state_flags so
1450                  * we must set the sleep_state_flags too.
1451                  */
1452                 .sleep_state_flags = cpu_to_le16(sleep_state_flags),
1453         };
1454         int ret;
1455
1456         /* TODO: somehow the fw doesn't seem to take PS_POLL into account */
1457         ret = iwl_mvm_send_add_sta_cmd(mvm, CMD_ASYNC, &cmd);
1458         if (ret)
1459                 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1460 }