Merge branch 'for_next' into for_linus
[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
774         /*
775          * If mac80211 is cleaning its state, then say that we finished since
776          * our state has been cleared anyway.
777          */
778         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
779                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
780                 return 0;
781         }
782
783         spin_lock_bh(&mvmsta->lock);
784
785         txq_id = tid_data->txq_id;
786
787         IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
788                             mvmsta->sta_id, tid, txq_id, tid_data->state);
789
790         switch (tid_data->state) {
791         case IWL_AGG_ON:
792                 tid_data->ssn = SEQ_TO_SN(tid_data->seq_number);
793
794                 IWL_DEBUG_TX_QUEUES(mvm,
795                                     "ssn = %d, next_recl = %d\n",
796                                     tid_data->ssn, tid_data->next_reclaimed);
797
798                 /* There are still packets for this RA / TID in the HW */
799                 if (tid_data->ssn != tid_data->next_reclaimed) {
800                         tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
801                         err = 0;
802                         break;
803                 }
804
805                 tid_data->ssn = 0xffff;
806                 iwl_trans_txq_disable(mvm->trans, txq_id);
807                 /* fall through */
808         case IWL_AGG_STARTING:
809         case IWL_EMPTYING_HW_QUEUE_ADDBA:
810                 /*
811                  * The agg session has been stopped before it was set up. This
812                  * can happen when the AddBA timer times out for example.
813                  */
814
815                 /* No barriers since we are under mutex */
816                 lockdep_assert_held(&mvm->mutex);
817                 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
818
819                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
820                 tid_data->state = IWL_AGG_OFF;
821                 err = 0;
822                 break;
823         default:
824                 IWL_ERR(mvm,
825                         "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
826                         mvmsta->sta_id, tid, tid_data->state);
827                 IWL_ERR(mvm,
828                         "\ttid_data->txq_id = %d\n", tid_data->txq_id);
829                 err = -EINVAL;
830         }
831
832         spin_unlock_bh(&mvmsta->lock);
833
834         return err;
835 }
836
837 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
838 {
839         int i;
840
841         lockdep_assert_held(&mvm->mutex);
842
843         i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
844
845         if (i == STA_KEY_MAX_NUM)
846                 return STA_KEY_IDX_INVALID;
847
848         __set_bit(i, mvm->fw_key_table);
849
850         return i;
851 }
852
853 static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
854                                  struct ieee80211_sta *sta)
855 {
856         struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
857
858         if (sta) {
859                 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
860
861                 return mvm_sta->sta_id;
862         }
863
864         /*
865          * The device expects GTKs for station interfaces to be
866          * installed as GTKs for the AP station. If we have no
867          * station ID, then use AP's station ID.
868          */
869         if (vif->type == NL80211_IFTYPE_STATION &&
870             mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
871                 return mvmvif->ap_sta_id;
872
873         return IWL_INVALID_STATION;
874 }
875
876 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
877                                 struct iwl_mvm_sta *mvm_sta,
878                                 struct ieee80211_key_conf *keyconf,
879                                 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
880                                 u32 cmd_flags)
881 {
882         __le16 key_flags;
883         struct iwl_mvm_add_sta_cmd cmd = {};
884         int ret, status;
885         u16 keyidx;
886         int i;
887
888         keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
889                  STA_KEY_FLG_KEYID_MSK;
890         key_flags = cpu_to_le16(keyidx);
891         key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
892
893         switch (keyconf->cipher) {
894         case WLAN_CIPHER_SUITE_TKIP:
895                 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
896                 cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
897                 for (i = 0; i < 5; i++)
898                         cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
899                 memcpy(cmd.key.key, keyconf->key, keyconf->keylen);
900                 break;
901         case WLAN_CIPHER_SUITE_CCMP:
902                 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
903                 memcpy(cmd.key.key, keyconf->key, keyconf->keylen);
904                 break;
905         default:
906                 WARN_ON(1);
907                 return -EINVAL;
908         }
909
910         if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
911                 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
912
913         cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
914         cmd.key.key_offset = keyconf->hw_key_idx;
915         cmd.key.key_flags = key_flags;
916         cmd.add_modify = STA_MODE_MODIFY;
917         cmd.modify_mask = STA_MODIFY_KEY;
918         cmd.sta_id = sta_id;
919
920         status = ADD_STA_SUCCESS;
921         if (cmd_flags == CMD_SYNC)
922                 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
923                                                   &cmd, &status);
924         else
925                 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
926                                            sizeof(cmd), &cmd);
927
928         switch (status) {
929         case ADD_STA_SUCCESS:
930                 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
931                 break;
932         default:
933                 ret = -EIO;
934                 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
935                 break;
936         }
937
938         return ret;
939 }
940
941 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
942                                  struct ieee80211_key_conf *keyconf,
943                                  u8 sta_id, bool remove_key)
944 {
945         struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
946
947         /* verify the key details match the required command's expectations */
948         if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
949                     (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
950                     (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
951                 return -EINVAL;
952
953         igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
954         igtk_cmd.sta_id = cpu_to_le32(sta_id);
955
956         if (remove_key) {
957                 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
958         } else {
959                 struct ieee80211_key_seq seq;
960                 const u8 *pn;
961
962                 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
963                 ieee80211_aes_cmac_calculate_k1_k2(keyconf,
964                                                    igtk_cmd.K1, igtk_cmd.K2);
965                 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
966                 pn = seq.aes_cmac.pn;
967                 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
968                                                        ((u64) pn[4] << 8) |
969                                                        ((u64) pn[3] << 16) |
970                                                        ((u64) pn[2] << 24) |
971                                                        ((u64) pn[1] << 32) |
972                                                        ((u64) pn[0] << 40));
973         }
974
975         IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
976                        remove_key ? "removing" : "installing",
977                        igtk_cmd.sta_id);
978
979         return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, CMD_SYNC,
980                                     sizeof(igtk_cmd), &igtk_cmd);
981 }
982
983
984 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
985                                        struct ieee80211_vif *vif,
986                                        struct ieee80211_sta *sta)
987 {
988         struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
989
990         if (sta)
991                 return sta->addr;
992
993         if (vif->type == NL80211_IFTYPE_STATION &&
994             mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
995                 u8 sta_id = mvmvif->ap_sta_id;
996                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
997                                                 lockdep_is_held(&mvm->mutex));
998                 return sta->addr;
999         }
1000
1001
1002         return NULL;
1003 }
1004
1005 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1006                         struct ieee80211_vif *vif,
1007                         struct ieee80211_sta *sta,
1008                         struct ieee80211_key_conf *keyconf,
1009                         bool have_key_offset)
1010 {
1011         struct iwl_mvm_sta *mvm_sta;
1012         int ret;
1013         u8 *addr, sta_id;
1014         struct ieee80211_key_seq seq;
1015         u16 p1k[5];
1016
1017         lockdep_assert_held(&mvm->mutex);
1018
1019         /* Get the station id from the mvm local station table */
1020         sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1021         if (sta_id == IWL_INVALID_STATION) {
1022                 IWL_ERR(mvm, "Failed to find station id\n");
1023                 return -EINVAL;
1024         }
1025
1026         if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1027                 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1028                 goto end;
1029         }
1030
1031         /*
1032          * It is possible that the 'sta' parameter is NULL, and thus
1033          * there is a need to retrieve  the sta from the local station table.
1034          */
1035         if (!sta) {
1036                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1037                                                 lockdep_is_held(&mvm->mutex));
1038                 if (IS_ERR_OR_NULL(sta)) {
1039                         IWL_ERR(mvm, "Invalid station id\n");
1040                         return -EINVAL;
1041                 }
1042         }
1043
1044         mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1045         if (WARN_ON_ONCE(mvm_sta->vif != vif))
1046                 return -EINVAL;
1047
1048         if (!have_key_offset) {
1049                 /*
1050                  * The D3 firmware hardcodes the PTK offset to 0, so we have to
1051                  * configure it there. As a result, this workaround exists to
1052                  * let the caller set the key offset (hw_key_idx), see d3.c.
1053                  */
1054                 keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1055                 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1056                         return -ENOSPC;
1057         }
1058
1059         switch (keyconf->cipher) {
1060         case WLAN_CIPHER_SUITE_TKIP:
1061                 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1062                 /* get phase 1 key from mac80211 */
1063                 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1064                 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1065                 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1066                                            seq.tkip.iv32, p1k, CMD_SYNC);
1067                 break;
1068         case WLAN_CIPHER_SUITE_CCMP:
1069                 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1070                                            0, NULL, CMD_SYNC);
1071                 break;
1072         default:
1073                 IWL_ERR(mvm, "Unknown cipher %x\n", keyconf->cipher);
1074                 ret = -EINVAL;
1075         }
1076
1077         if (ret)
1078                 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1079
1080 end:
1081         IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1082                       keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1083                       sta->addr, ret);
1084         return ret;
1085 }
1086
1087 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1088                            struct ieee80211_vif *vif,
1089                            struct ieee80211_sta *sta,
1090                            struct ieee80211_key_conf *keyconf)
1091 {
1092         struct iwl_mvm_sta *mvm_sta;
1093         struct iwl_mvm_add_sta_cmd cmd = {};
1094         __le16 key_flags;
1095         int ret, status;
1096         u8 sta_id;
1097
1098         lockdep_assert_held(&mvm->mutex);
1099
1100         /* Get the station id from the mvm local station table */
1101         sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1102
1103         IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1104                       keyconf->keyidx, sta_id);
1105
1106         if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1107                 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1108
1109         ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1110         if (!ret) {
1111                 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1112                         keyconf->hw_key_idx);
1113                 return -ENOENT;
1114         }
1115
1116         if (sta_id == IWL_INVALID_STATION) {
1117                 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1118                 return 0;
1119         }
1120
1121         /*
1122          * It is possible that the 'sta' parameter is NULL, and thus
1123          * there is a need to retrieve the sta from the local station table,
1124          * for example when a GTK is removed (where the sta_id will then be
1125          * the AP ID, and no station was passed by mac80211.)
1126          */
1127         if (!sta) {
1128                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1129                                                 lockdep_is_held(&mvm->mutex));
1130                 if (!sta) {
1131                         IWL_ERR(mvm, "Invalid station id\n");
1132                         return -EINVAL;
1133                 }
1134         }
1135
1136         mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1137         if (WARN_ON_ONCE(mvm_sta->vif != vif))
1138                 return -EINVAL;
1139
1140         key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1141                                  STA_KEY_FLG_KEYID_MSK);
1142         key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1143         key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1144
1145         if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1146                 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1147
1148         cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1149         cmd.key.key_flags = key_flags;
1150         cmd.key.key_offset = keyconf->hw_key_idx;
1151         cmd.sta_id = sta_id;
1152
1153         cmd.modify_mask = STA_MODIFY_KEY;
1154         cmd.add_modify = STA_MODE_MODIFY;
1155
1156         status = ADD_STA_SUCCESS;
1157         ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
1158                                           &cmd, &status);
1159
1160         switch (status) {
1161         case ADD_STA_SUCCESS:
1162                 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1163                 break;
1164         default:
1165                 ret = -EIO;
1166                 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1167                 break;
1168         }
1169
1170         return ret;
1171 }
1172
1173 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1174                              struct ieee80211_vif *vif,
1175                              struct ieee80211_key_conf *keyconf,
1176                              struct ieee80211_sta *sta, u32 iv32,
1177                              u16 *phase1key)
1178 {
1179         struct iwl_mvm_sta *mvm_sta;
1180         u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1181
1182         if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
1183                 return;
1184
1185         rcu_read_lock();
1186
1187         if (!sta) {
1188                 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1189                 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1190                         rcu_read_unlock();
1191                         return;
1192                 }
1193         }
1194
1195         mvm_sta = (void *)sta->drv_priv;
1196         iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1197                              iv32, phase1key, CMD_ASYNC);
1198         rcu_read_unlock();
1199 }
1200
1201 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1202                                 struct ieee80211_sta *sta)
1203 {
1204         struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
1205         struct iwl_mvm_add_sta_cmd cmd = {
1206                 .add_modify = STA_MODE_MODIFY,
1207                 .sta_id = mvmsta->sta_id,
1208                 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1209                 .sleep_state_flags = cpu_to_le16(STA_SLEEP_STATE_AWAKE),
1210                 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1211         };
1212         int ret;
1213
1214         /*
1215          * Same modify mask for sleep_tx_count and sleep_state_flags but this
1216          * should be fine since if we set the STA as "awake", then
1217          * sleep_tx_count is not relevant.
1218          */
1219         ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1220         if (ret)
1221                 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1222 }
1223
1224 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1225                                        struct ieee80211_sta *sta,
1226                                        enum ieee80211_frame_release_type reason,
1227                                        u16 cnt)
1228 {
1229         u16 sleep_state_flags =
1230                 (reason == IEEE80211_FRAME_RELEASE_UAPSD) ?
1231                         STA_SLEEP_STATE_UAPSD : STA_SLEEP_STATE_PS_POLL;
1232         struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
1233         struct iwl_mvm_add_sta_cmd cmd = {
1234                 .add_modify = STA_MODE_MODIFY,
1235                 .sta_id = mvmsta->sta_id,
1236                 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1237                 .sleep_tx_count = cpu_to_le16(cnt),
1238                 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1239                 /*
1240                  * Same modify mask for sleep_tx_count and sleep_state_flags so
1241                  * we must set the sleep_state_flags too.
1242                  */
1243                 .sleep_state_flags = cpu_to_le16(sleep_state_flags),
1244         };
1245         int ret;
1246
1247         /* TODO: somehow the fw doesn't seem to take PS_POLL into account */
1248         ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1249         if (ret)
1250                 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1251 }