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