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