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