Merge branch 'for-3.5-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / iwlwifi / iwl-agn-sta.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
4  *
5  * Portions of this file are derived from the ipw3945 project, as well
6  * as portions of the ieee80211 subsystem header files.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of version 2 of the GNU General Public License as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20  *
21  * The full GNU General Public License is included in this distribution in the
22  * file called LICENSE.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <ilw@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  *****************************************************************************/
29 #include <linux/etherdevice.h>
30 #include <net/mac80211.h>
31
32 #include "iwl-dev.h"
33 #include "iwl-agn.h"
34 #include "iwl-trans.h"
35
36 const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
37
38 static int iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
39 {
40         lockdep_assert_held(&priv->sta_lock);
41
42         if (sta_id >= IWLAGN_STATION_COUNT) {
43                 IWL_ERR(priv, "invalid sta_id %u", sta_id);
44                 return -EINVAL;
45         }
46         if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
47                 IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u "
48                         "addr %pM\n",
49                         sta_id, priv->stations[sta_id].sta.sta.addr);
50
51         if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
52                 IWL_DEBUG_ASSOC(priv,
53                                 "STA id %u addr %pM already present in uCode "
54                                 "(according to driver)\n",
55                                 sta_id, priv->stations[sta_id].sta.sta.addr);
56         } else {
57                 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
58                 IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
59                                 sta_id, priv->stations[sta_id].sta.sta.addr);
60         }
61         return 0;
62 }
63
64 static int iwl_process_add_sta_resp(struct iwl_priv *priv,
65                                     struct iwl_addsta_cmd *addsta,
66                                     struct iwl_rx_packet *pkt)
67 {
68         struct iwl_add_sta_resp *add_sta_resp = (void *)pkt->data;
69         u8 sta_id = addsta->sta.sta_id;
70         int ret = -EIO;
71
72         if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
73                 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
74                         pkt->hdr.flags);
75                 return ret;
76         }
77
78         IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
79                        sta_id);
80
81         spin_lock(&priv->sta_lock);
82
83         switch (add_sta_resp->status) {
84         case ADD_STA_SUCCESS_MSK:
85                 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
86                 ret = iwl_sta_ucode_activate(priv, sta_id);
87                 break;
88         case ADD_STA_NO_ROOM_IN_TABLE:
89                 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
90                         sta_id);
91                 break;
92         case ADD_STA_NO_BLOCK_ACK_RESOURCE:
93                 IWL_ERR(priv, "Adding station %d failed, no block ack "
94                         "resource.\n", sta_id);
95                 break;
96         case ADD_STA_MODIFY_NON_EXIST_STA:
97                 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
98                         sta_id);
99                 break;
100         default:
101                 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
102                                 add_sta_resp->status);
103                 break;
104         }
105
106         IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
107                        priv->stations[sta_id].sta.mode ==
108                        STA_CONTROL_MODIFY_MSK ?  "Modified" : "Added",
109                        sta_id, priv->stations[sta_id].sta.sta.addr);
110
111         /*
112          * XXX: The MAC address in the command buffer is often changed from
113          * the original sent to the device. That is, the MAC address
114          * written to the command buffer often is not the same MAC address
115          * read from the command buffer when the command returns. This
116          * issue has not yet been resolved and this debugging is left to
117          * observe the problem.
118          */
119         IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
120                        priv->stations[sta_id].sta.mode ==
121                        STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
122                        addsta->sta.addr);
123         spin_unlock(&priv->sta_lock);
124
125         return ret;
126 }
127
128 int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
129                                struct iwl_device_cmd *cmd)
130 {
131         struct iwl_rx_packet *pkt = rxb_addr(rxb);
132         struct iwl_addsta_cmd *addsta =
133                 (struct iwl_addsta_cmd *) cmd->payload;
134
135         return iwl_process_add_sta_resp(priv, addsta, pkt);
136 }
137
138 int iwl_send_add_sta(struct iwl_priv *priv,
139                      struct iwl_addsta_cmd *sta, u8 flags)
140 {
141         int ret = 0;
142         struct iwl_host_cmd cmd = {
143                 .id = REPLY_ADD_STA,
144                 .flags = flags,
145                 .data = { sta, },
146                 .len = { sizeof(*sta), },
147         };
148         u8 sta_id __maybe_unused = sta->sta.sta_id;
149
150         IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
151                        sta_id, sta->sta.addr, flags & CMD_ASYNC ?  "a" : "");
152
153         if (!(flags & CMD_ASYNC)) {
154                 cmd.flags |= CMD_WANT_SKB;
155                 might_sleep();
156         }
157
158         ret = iwl_dvm_send_cmd(priv, &cmd);
159
160         if (ret || (flags & CMD_ASYNC))
161                 return ret;
162         /*else the command was successfully sent in SYNC mode, need to free
163          * the reply page */
164
165         iwl_free_resp(&cmd);
166
167         if (cmd.handler_status)
168                 IWL_ERR(priv, "%s - error in the CMD response %d", __func__,
169                         cmd.handler_status);
170
171         return cmd.handler_status;
172 }
173
174 static bool iwl_is_channel_extension(struct iwl_priv *priv,
175                                      enum ieee80211_band band,
176                                      u16 channel, u8 extension_chan_offset)
177 {
178         const struct iwl_channel_info *ch_info;
179
180         ch_info = iwl_get_channel_info(priv, band, channel);
181         if (!is_channel_valid(ch_info))
182                 return false;
183
184         if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
185                 return !(ch_info->ht40_extension_channel &
186                                         IEEE80211_CHAN_NO_HT40PLUS);
187         else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
188                 return !(ch_info->ht40_extension_channel &
189                                         IEEE80211_CHAN_NO_HT40MINUS);
190
191         return false;
192 }
193
194 bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
195                             struct iwl_rxon_context *ctx,
196                             struct ieee80211_sta_ht_cap *ht_cap)
197 {
198         if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
199                 return false;
200
201         /*
202          * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
203          * the bit will not set if it is pure 40MHz case
204          */
205         if (ht_cap && !ht_cap->ht_supported)
206                 return false;
207
208 #ifdef CONFIG_IWLWIFI_DEBUGFS
209         if (priv->disable_ht40)
210                 return false;
211 #endif
212
213         return iwl_is_channel_extension(priv, priv->band,
214                         le16_to_cpu(ctx->staging.channel),
215                         ctx->ht.extension_chan_offset);
216 }
217
218 static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
219                                   struct ieee80211_sta *sta,
220                                   struct iwl_rxon_context *ctx,
221                                   __le32 *flags, __le32 *mask)
222 {
223         struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
224         u8 mimo_ps_mode;
225
226         *mask = STA_FLG_RTS_MIMO_PROT_MSK |
227                 STA_FLG_MIMO_DIS_MSK |
228                 STA_FLG_HT40_EN_MSK |
229                 STA_FLG_MAX_AGG_SIZE_MSK |
230                 STA_FLG_AGG_MPDU_DENSITY_MSK;
231         *flags = 0;
232
233         if (!sta || !sta_ht_inf->ht_supported)
234                 return;
235
236         mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
237
238         IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",
239                         (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
240                         "static" :
241                         (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
242                         "dynamic" : "disabled");
243
244         switch (mimo_ps_mode) {
245         case WLAN_HT_CAP_SM_PS_STATIC:
246                 *flags |= STA_FLG_MIMO_DIS_MSK;
247                 break;
248         case WLAN_HT_CAP_SM_PS_DYNAMIC:
249                 *flags |= STA_FLG_RTS_MIMO_PROT_MSK;
250                 break;
251         case WLAN_HT_CAP_SM_PS_DISABLED:
252                 break;
253         default:
254                 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
255                 break;
256         }
257
258         *flags |= cpu_to_le32(
259                 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
260
261         *flags |= cpu_to_le32(
262                 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
263
264         if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
265                 *flags |= STA_FLG_HT40_EN_MSK;
266 }
267
268 int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
269                       struct ieee80211_sta *sta)
270 {
271         u8 sta_id = iwl_sta_id(sta);
272         __le32 flags, mask;
273         struct iwl_addsta_cmd cmd;
274
275         if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
276                 return -EINVAL;
277
278         iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
279
280         spin_lock_bh(&priv->sta_lock);
281         priv->stations[sta_id].sta.station_flags &= ~mask;
282         priv->stations[sta_id].sta.station_flags |= flags;
283         spin_unlock_bh(&priv->sta_lock);
284
285         memset(&cmd, 0, sizeof(cmd));
286         cmd.mode = STA_CONTROL_MODIFY_MSK;
287         cmd.station_flags_msk = mask;
288         cmd.station_flags = flags;
289         cmd.sta.sta_id = sta_id;
290
291         return iwl_send_add_sta(priv, &cmd, CMD_SYNC);
292 }
293
294 static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
295                                    struct ieee80211_sta *sta,
296                                    struct iwl_rxon_context *ctx)
297 {
298         __le32 flags, mask;
299
300         iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
301
302         lockdep_assert_held(&priv->sta_lock);
303         priv->stations[index].sta.station_flags &= ~mask;
304         priv->stations[index].sta.station_flags |= flags;
305 }
306
307 /**
308  * iwl_prep_station - Prepare station information for addition
309  *
310  * should be called with sta_lock held
311  */
312 u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
313                     const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
314 {
315         struct iwl_station_entry *station;
316         int i;
317         u8 sta_id = IWL_INVALID_STATION;
318
319         if (is_ap)
320                 sta_id = ctx->ap_sta_id;
321         else if (is_broadcast_ether_addr(addr))
322                 sta_id = ctx->bcast_sta_id;
323         else
324                 for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
325                         if (ether_addr_equal(priv->stations[i].sta.sta.addr,
326                                              addr)) {
327                                 sta_id = i;
328                                 break;
329                         }
330
331                         if (!priv->stations[i].used &&
332                             sta_id == IWL_INVALID_STATION)
333                                 sta_id = i;
334                 }
335
336         /*
337          * These two conditions have the same outcome, but keep them
338          * separate
339          */
340         if (unlikely(sta_id == IWL_INVALID_STATION))
341                 return sta_id;
342
343         /*
344          * uCode is not able to deal with multiple requests to add a
345          * station. Keep track if one is in progress so that we do not send
346          * another.
347          */
348         if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
349                 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
350                                "added.\n", sta_id);
351                 return sta_id;
352         }
353
354         if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
355             (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
356             ether_addr_equal(priv->stations[sta_id].sta.sta.addr, addr)) {
357                 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
358                                 "adding again.\n", sta_id, addr);
359                 return sta_id;
360         }
361
362         station = &priv->stations[sta_id];
363         station->used = IWL_STA_DRIVER_ACTIVE;
364         IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
365                         sta_id, addr);
366         priv->num_stations++;
367
368         /* Set up the REPLY_ADD_STA command to send to device */
369         memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
370         memcpy(station->sta.sta.addr, addr, ETH_ALEN);
371         station->sta.mode = 0;
372         station->sta.sta.sta_id = sta_id;
373         station->sta.station_flags = ctx->station_flags;
374         station->ctxid = ctx->ctxid;
375
376         if (sta) {
377                 struct iwl_station_priv *sta_priv;
378
379                 sta_priv = (void *)sta->drv_priv;
380                 sta_priv->ctx = ctx;
381         }
382
383         /*
384          * OK to call unconditionally, since local stations (IBSS BSSID
385          * STA and broadcast STA) pass in a NULL sta, and mac80211
386          * doesn't allow HT IBSS.
387          */
388         iwl_set_ht_add_station(priv, sta_id, sta, ctx);
389
390         return sta_id;
391
392 }
393
394 #define STA_WAIT_TIMEOUT (HZ/2)
395
396 /**
397  * iwl_add_station_common -
398  */
399 int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
400                            const u8 *addr, bool is_ap,
401                            struct ieee80211_sta *sta, u8 *sta_id_r)
402 {
403         int ret = 0;
404         u8 sta_id;
405         struct iwl_addsta_cmd sta_cmd;
406
407         *sta_id_r = 0;
408         spin_lock_bh(&priv->sta_lock);
409         sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
410         if (sta_id == IWL_INVALID_STATION) {
411                 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
412                         addr);
413                 spin_unlock_bh(&priv->sta_lock);
414                 return -EINVAL;
415         }
416
417         /*
418          * uCode is not able to deal with multiple requests to add a
419          * station. Keep track if one is in progress so that we do not send
420          * another.
421          */
422         if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
423                 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
424                                "added.\n", sta_id);
425                 spin_unlock_bh(&priv->sta_lock);
426                 return -EEXIST;
427         }
428
429         if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
430             (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
431                 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
432                                 "adding again.\n", sta_id, addr);
433                 spin_unlock_bh(&priv->sta_lock);
434                 return -EEXIST;
435         }
436
437         priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
438         memcpy(&sta_cmd, &priv->stations[sta_id].sta,
439                sizeof(struct iwl_addsta_cmd));
440         spin_unlock_bh(&priv->sta_lock);
441
442         /* Add station to device's station table */
443         ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
444         if (ret) {
445                 spin_lock_bh(&priv->sta_lock);
446                 IWL_ERR(priv, "Adding station %pM failed.\n",
447                         priv->stations[sta_id].sta.sta.addr);
448                 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
449                 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
450                 spin_unlock_bh(&priv->sta_lock);
451         }
452         *sta_id_r = sta_id;
453         return ret;
454 }
455
456 /**
457  * iwl_sta_ucode_deactivate - deactivate ucode status for a station
458  */
459 static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
460 {
461         lockdep_assert_held(&priv->sta_lock);
462
463         /* Ucode must be active and driver must be non active */
464         if ((priv->stations[sta_id].used &
465              (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
466               IWL_STA_UCODE_ACTIVE)
467                 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
468
469         priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
470
471         memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
472         IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
473 }
474
475 static int iwl_send_remove_station(struct iwl_priv *priv,
476                                    const u8 *addr, int sta_id,
477                                    bool temporary)
478 {
479         struct iwl_rx_packet *pkt;
480         int ret;
481         struct iwl_rem_sta_cmd rm_sta_cmd;
482
483         struct iwl_host_cmd cmd = {
484                 .id = REPLY_REMOVE_STA,
485                 .len = { sizeof(struct iwl_rem_sta_cmd), },
486                 .flags = CMD_SYNC,
487                 .data = { &rm_sta_cmd, },
488         };
489
490         memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
491         rm_sta_cmd.num_sta = 1;
492         memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
493
494         cmd.flags |= CMD_WANT_SKB;
495
496         ret = iwl_dvm_send_cmd(priv, &cmd);
497
498         if (ret)
499                 return ret;
500
501         pkt = cmd.resp_pkt;
502         if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
503                 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
504                           pkt->hdr.flags);
505                 ret = -EIO;
506         }
507
508         if (!ret) {
509                 struct iwl_rem_sta_resp *rem_sta_resp = (void *)pkt->data;
510                 switch (rem_sta_resp->status) {
511                 case REM_STA_SUCCESS_MSK:
512                         if (!temporary) {
513                                 spin_lock_bh(&priv->sta_lock);
514                                 iwl_sta_ucode_deactivate(priv, sta_id);
515                                 spin_unlock_bh(&priv->sta_lock);
516                         }
517                         IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
518                         break;
519                 default:
520                         ret = -EIO;
521                         IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
522                         break;
523                 }
524         }
525         iwl_free_resp(&cmd);
526
527         return ret;
528 }
529
530 /**
531  * iwl_remove_station - Remove driver's knowledge of station.
532  */
533 int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
534                        const u8 *addr)
535 {
536         u8 tid;
537
538         if (!iwl_is_ready(priv)) {
539                 IWL_DEBUG_INFO(priv,
540                         "Unable to remove station %pM, device not ready.\n",
541                         addr);
542                 /*
543                  * It is typical for stations to be removed when we are
544                  * going down. Return success since device will be down
545                  * soon anyway
546                  */
547                 return 0;
548         }
549
550         IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d  %pM\n",
551                         sta_id, addr);
552
553         if (WARN_ON(sta_id == IWL_INVALID_STATION))
554                 return -EINVAL;
555
556         spin_lock_bh(&priv->sta_lock);
557
558         if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
559                 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
560                                 addr);
561                 goto out_err;
562         }
563
564         if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
565                 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
566                                 addr);
567                 goto out_err;
568         }
569
570         if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
571                 kfree(priv->stations[sta_id].lq);
572                 priv->stations[sta_id].lq = NULL;
573         }
574
575         for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
576                 memset(&priv->tid_data[sta_id][tid], 0,
577                         sizeof(priv->tid_data[sta_id][tid]));
578
579         priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
580
581         priv->num_stations--;
582
583         if (WARN_ON(priv->num_stations < 0))
584                 priv->num_stations = 0;
585
586         spin_unlock_bh(&priv->sta_lock);
587
588         return iwl_send_remove_station(priv, addr, sta_id, false);
589 out_err:
590         spin_unlock_bh(&priv->sta_lock);
591         return -EINVAL;
592 }
593
594 void iwl_deactivate_station(struct iwl_priv *priv, const u8 sta_id,
595                             const u8 *addr)
596 {
597         u8 tid;
598
599         if (!iwl_is_ready(priv)) {
600                 IWL_DEBUG_INFO(priv,
601                         "Unable to remove station %pM, device not ready.\n",
602                         addr);
603                 return;
604         }
605
606         IWL_DEBUG_ASSOC(priv, "Deactivating STA: %pM (%d)\n", addr, sta_id);
607
608         if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
609                 return;
610
611         spin_lock_bh(&priv->sta_lock);
612
613         WARN_ON_ONCE(!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE));
614
615         for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
616                 memset(&priv->tid_data[sta_id][tid], 0,
617                         sizeof(priv->tid_data[sta_id][tid]));
618
619         priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
620
621         priv->num_stations--;
622
623         if (WARN_ON_ONCE(priv->num_stations < 0))
624                 priv->num_stations = 0;
625
626         spin_unlock_bh(&priv->sta_lock);
627 }
628
629 static void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
630                             u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
631 {
632         int i, r;
633         u32 rate_flags = 0;
634         __le32 rate_n_flags;
635
636         lockdep_assert_held(&priv->mutex);
637
638         memset(link_cmd, 0, sizeof(*link_cmd));
639
640         /* Set up the rate scaling to start at selected rate, fall back
641          * all the way down to 1M in IEEE order, and then spin on 1M */
642         if (priv->band == IEEE80211_BAND_5GHZ)
643                 r = IWL_RATE_6M_INDEX;
644         else if (ctx && ctx->vif && ctx->vif->p2p)
645                 r = IWL_RATE_6M_INDEX;
646         else
647                 r = IWL_RATE_1M_INDEX;
648
649         if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
650                 rate_flags |= RATE_MCS_CCK_MSK;
651
652         rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
653                                 RATE_MCS_ANT_POS;
654         rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
655         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
656                 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
657
658         link_cmd->general_params.single_stream_ant_msk =
659                         first_antenna(priv->hw_params.valid_tx_ant);
660
661         link_cmd->general_params.dual_stream_ant_msk =
662                 priv->hw_params.valid_tx_ant &
663                 ~first_antenna(priv->hw_params.valid_tx_ant);
664         if (!link_cmd->general_params.dual_stream_ant_msk) {
665                 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
666         } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
667                 link_cmd->general_params.dual_stream_ant_msk =
668                         priv->hw_params.valid_tx_ant;
669         }
670
671         link_cmd->agg_params.agg_dis_start_th =
672                 LINK_QUAL_AGG_DISABLE_START_DEF;
673         link_cmd->agg_params.agg_time_limit =
674                 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
675
676         link_cmd->sta_id = sta_id;
677 }
678
679 /**
680  * iwl_clear_ucode_stations - clear ucode station table bits
681  *
682  * This function clears all the bits in the driver indicating
683  * which stations are active in the ucode. Call when something
684  * other than explicit station management would cause this in
685  * the ucode, e.g. unassociated RXON.
686  */
687 void iwl_clear_ucode_stations(struct iwl_priv *priv,
688                               struct iwl_rxon_context *ctx)
689 {
690         int i;
691         bool cleared = false;
692
693         IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
694
695         spin_lock_bh(&priv->sta_lock);
696         for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
697                 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
698                         continue;
699
700                 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
701                         IWL_DEBUG_INFO(priv,
702                                 "Clearing ucode active for station %d\n", i);
703                         priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
704                         cleared = true;
705                 }
706         }
707         spin_unlock_bh(&priv->sta_lock);
708
709         if (!cleared)
710                 IWL_DEBUG_INFO(priv,
711                                "No active stations found to be cleared\n");
712 }
713
714 /**
715  * iwl_restore_stations() - Restore driver known stations to device
716  *
717  * All stations considered active by driver, but not present in ucode, is
718  * restored.
719  *
720  * Function sleeps.
721  */
722 void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
723 {
724         struct iwl_addsta_cmd sta_cmd;
725         struct iwl_link_quality_cmd lq;
726         int i;
727         bool found = false;
728         int ret;
729         bool send_lq;
730
731         if (!iwl_is_ready(priv)) {
732                 IWL_DEBUG_INFO(priv,
733                                "Not ready yet, not restoring any stations.\n");
734                 return;
735         }
736
737         IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
738         spin_lock_bh(&priv->sta_lock);
739         for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
740                 if (ctx->ctxid != priv->stations[i].ctxid)
741                         continue;
742                 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
743                             !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
744                         IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
745                                         priv->stations[i].sta.sta.addr);
746                         priv->stations[i].sta.mode = 0;
747                         priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
748                         found = true;
749                 }
750         }
751
752         for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
753                 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
754                         memcpy(&sta_cmd, &priv->stations[i].sta,
755                                sizeof(struct iwl_addsta_cmd));
756                         send_lq = false;
757                         if (priv->stations[i].lq) {
758                                 if (priv->wowlan)
759                                         iwl_sta_fill_lq(priv, ctx, i, &lq);
760                                 else
761                                         memcpy(&lq, priv->stations[i].lq,
762                                                sizeof(struct iwl_link_quality_cmd));
763                                 send_lq = true;
764                         }
765                         spin_unlock_bh(&priv->sta_lock);
766                         ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
767                         if (ret) {
768                                 spin_lock_bh(&priv->sta_lock);
769                                 IWL_ERR(priv, "Adding station %pM failed.\n",
770                                         priv->stations[i].sta.sta.addr);
771                                 priv->stations[i].used &=
772                                                 ~IWL_STA_DRIVER_ACTIVE;
773                                 priv->stations[i].used &=
774                                                 ~IWL_STA_UCODE_INPROGRESS;
775                                 continue;
776                         }
777                         /*
778                          * Rate scaling has already been initialized, send
779                          * current LQ command
780                          */
781                         if (send_lq)
782                                 iwl_send_lq_cmd(priv, ctx, &lq,
783                                                 CMD_SYNC, true);
784                         spin_lock_bh(&priv->sta_lock);
785                         priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
786                 }
787         }
788
789         spin_unlock_bh(&priv->sta_lock);
790         if (!found)
791                 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
792                         "no stations to be restored.\n");
793         else
794                 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
795                         "complete.\n");
796 }
797
798 int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
799 {
800         int i;
801
802         for (i = 0; i < priv->sta_key_max_num; i++)
803                 if (!test_and_set_bit(i, &priv->ucode_key_table))
804                         return i;
805
806         return WEP_INVALID_OFFSET;
807 }
808
809 void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
810 {
811         int i;
812
813         spin_lock_bh(&priv->sta_lock);
814         for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
815                 if (!(priv->stations[i].used & IWL_STA_BCAST))
816                         continue;
817
818                 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
819                 priv->num_stations--;
820                 if (WARN_ON(priv->num_stations < 0))
821                         priv->num_stations = 0;
822                 kfree(priv->stations[i].lq);
823                 priv->stations[i].lq = NULL;
824         }
825         spin_unlock_bh(&priv->sta_lock);
826 }
827
828 #ifdef CONFIG_IWLWIFI_DEBUG
829 static void iwl_dump_lq_cmd(struct iwl_priv *priv,
830                            struct iwl_link_quality_cmd *lq)
831 {
832         int i;
833         IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
834         IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
835                        lq->general_params.single_stream_ant_msk,
836                        lq->general_params.dual_stream_ant_msk);
837
838         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
839                 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
840                                i, lq->rs_table[i].rate_n_flags);
841 }
842 #else
843 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
844                                    struct iwl_link_quality_cmd *lq)
845 {
846 }
847 #endif
848
849 /**
850  * is_lq_table_valid() - Test one aspect of LQ cmd for validity
851  *
852  * It sometimes happens when a HT rate has been in use and we
853  * loose connectivity with AP then mac80211 will first tell us that the
854  * current channel is not HT anymore before removing the station. In such a
855  * scenario the RXON flags will be updated to indicate we are not
856  * communicating HT anymore, but the LQ command may still contain HT rates.
857  * Test for this to prevent driver from sending LQ command between the time
858  * RXON flags are updated and when LQ command is updated.
859  */
860 static bool is_lq_table_valid(struct iwl_priv *priv,
861                               struct iwl_rxon_context *ctx,
862                               struct iwl_link_quality_cmd *lq)
863 {
864         int i;
865
866         if (ctx->ht.enabled)
867                 return true;
868
869         IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
870                        ctx->active.channel);
871         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
872                 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
873                     RATE_MCS_HT_MSK) {
874                         IWL_DEBUG_INFO(priv,
875                                        "index %d of LQ expects HT channel\n",
876                                        i);
877                         return false;
878                 }
879         }
880         return true;
881 }
882
883 /**
884  * iwl_send_lq_cmd() - Send link quality command
885  * @init: This command is sent as part of station initialization right
886  *        after station has been added.
887  *
888  * The link quality command is sent as the last step of station creation.
889  * This is the special case in which init is set and we call a callback in
890  * this case to clear the state indicating that station creation is in
891  * progress.
892  */
893 int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
894                     struct iwl_link_quality_cmd *lq, u8 flags, bool init)
895 {
896         int ret = 0;
897         struct iwl_host_cmd cmd = {
898                 .id = REPLY_TX_LINK_QUALITY_CMD,
899                 .len = { sizeof(struct iwl_link_quality_cmd), },
900                 .flags = flags,
901                 .data = { lq, },
902         };
903
904         if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
905                 return -EINVAL;
906
907
908         spin_lock_bh(&priv->sta_lock);
909         if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
910                 spin_unlock_bh(&priv->sta_lock);
911                 return -EINVAL;
912         }
913         spin_unlock_bh(&priv->sta_lock);
914
915         iwl_dump_lq_cmd(priv, lq);
916         if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
917                 return -EINVAL;
918
919         if (is_lq_table_valid(priv, ctx, lq))
920                 ret = iwl_dvm_send_cmd(priv, &cmd);
921         else
922                 ret = -EINVAL;
923
924         if (cmd.flags & CMD_ASYNC)
925                 return ret;
926
927         if (init) {
928                 IWL_DEBUG_INFO(priv, "init LQ command complete, "
929                                "clearing sta addition status for sta %d\n",
930                                lq->sta_id);
931                 spin_lock_bh(&priv->sta_lock);
932                 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
933                 spin_unlock_bh(&priv->sta_lock);
934         }
935         return ret;
936 }
937
938
939 static struct iwl_link_quality_cmd *
940 iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
941                  u8 sta_id)
942 {
943         struct iwl_link_quality_cmd *link_cmd;
944
945         link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
946         if (!link_cmd) {
947                 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
948                 return NULL;
949         }
950
951         iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
952
953         return link_cmd;
954 }
955
956 /*
957  * iwlagn_add_bssid_station - Add the special IBSS BSSID station
958  *
959  * Function sleeps.
960  */
961 int iwlagn_add_bssid_station(struct iwl_priv *priv,
962                              struct iwl_rxon_context *ctx,
963                              const u8 *addr, u8 *sta_id_r)
964 {
965         int ret;
966         u8 sta_id;
967         struct iwl_link_quality_cmd *link_cmd;
968
969         if (sta_id_r)
970                 *sta_id_r = IWL_INVALID_STATION;
971
972         ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
973         if (ret) {
974                 IWL_ERR(priv, "Unable to add station %pM\n", addr);
975                 return ret;
976         }
977
978         if (sta_id_r)
979                 *sta_id_r = sta_id;
980
981         spin_lock_bh(&priv->sta_lock);
982         priv->stations[sta_id].used |= IWL_STA_LOCAL;
983         spin_unlock_bh(&priv->sta_lock);
984
985         /* Set up default rate scaling table in device's station table */
986         link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
987         if (!link_cmd) {
988                 IWL_ERR(priv,
989                         "Unable to initialize rate scaling for station %pM.\n",
990                         addr);
991                 return -ENOMEM;
992         }
993
994         ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
995         if (ret)
996                 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
997
998         spin_lock_bh(&priv->sta_lock);
999         priv->stations[sta_id].lq = link_cmd;
1000         spin_unlock_bh(&priv->sta_lock);
1001
1002         return 0;
1003 }
1004
1005 /*
1006  * static WEP keys
1007  *
1008  * For each context, the device has a table of 4 static WEP keys
1009  * (one for each key index) that is updated with the following
1010  * commands.
1011  */
1012
1013 static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
1014                                       struct iwl_rxon_context *ctx,
1015                                       bool send_if_empty)
1016 {
1017         int i, not_empty = 0;
1018         u8 buff[sizeof(struct iwl_wep_cmd) +
1019                 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
1020         struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
1021         size_t cmd_size  = sizeof(struct iwl_wep_cmd);
1022         struct iwl_host_cmd cmd = {
1023                 .id = ctx->wep_key_cmd,
1024                 .data = { wep_cmd, },
1025                 .flags = CMD_SYNC,
1026         };
1027
1028         might_sleep();
1029
1030         memset(wep_cmd, 0, cmd_size +
1031                         (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
1032
1033         for (i = 0; i < WEP_KEYS_MAX ; i++) {
1034                 wep_cmd->key[i].key_index = i;
1035                 if (ctx->wep_keys[i].key_size) {
1036                         wep_cmd->key[i].key_offset = i;
1037                         not_empty = 1;
1038                 } else {
1039                         wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
1040                 }
1041
1042                 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
1043                 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
1044                                 ctx->wep_keys[i].key_size);
1045         }
1046
1047         wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
1048         wep_cmd->num_keys = WEP_KEYS_MAX;
1049
1050         cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
1051
1052         cmd.len[0] = cmd_size;
1053
1054         if (not_empty || send_if_empty)
1055                 return iwl_dvm_send_cmd(priv, &cmd);
1056         else
1057                 return 0;
1058 }
1059
1060 int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1061                                  struct iwl_rxon_context *ctx)
1062 {
1063         lockdep_assert_held(&priv->mutex);
1064
1065         return iwl_send_static_wepkey_cmd(priv, ctx, false);
1066 }
1067
1068 int iwl_remove_default_wep_key(struct iwl_priv *priv,
1069                                struct iwl_rxon_context *ctx,
1070                                struct ieee80211_key_conf *keyconf)
1071 {
1072         int ret;
1073
1074         lockdep_assert_held(&priv->mutex);
1075
1076         IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1077                       keyconf->keyidx);
1078
1079         memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
1080         if (iwl_is_rfkill(priv)) {
1081                 IWL_DEBUG_WEP(priv,
1082                         "Not sending REPLY_WEPKEY command due to RFKILL.\n");
1083                 /* but keys in device are clear anyway so return success */
1084                 return 0;
1085         }
1086         ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1087         IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1088                       keyconf->keyidx, ret);
1089
1090         return ret;
1091 }
1092
1093 int iwl_set_default_wep_key(struct iwl_priv *priv,
1094                             struct iwl_rxon_context *ctx,
1095                             struct ieee80211_key_conf *keyconf)
1096 {
1097         int ret;
1098
1099         lockdep_assert_held(&priv->mutex);
1100
1101         if (keyconf->keylen != WEP_KEY_LEN_128 &&
1102             keyconf->keylen != WEP_KEY_LEN_64) {
1103                 IWL_DEBUG_WEP(priv,
1104                               "Bad WEP key length %d\n", keyconf->keylen);
1105                 return -EINVAL;
1106         }
1107
1108         keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
1109
1110         ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1111         memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1112                                                         keyconf->keylen);
1113
1114         ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1115         IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1116                 keyconf->keylen, keyconf->keyidx, ret);
1117
1118         return ret;
1119 }
1120
1121 /*
1122  * dynamic (per-station) keys
1123  *
1124  * The dynamic keys are a little more complicated. The device has
1125  * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1126  * These are linked to stations by a table that contains an index
1127  * into the key table for each station/key index/{mcast,unicast},
1128  * i.e. it's basically an array of pointers like this:
1129  *      key_offset_t key_mapping[NUM_STATIONS][4][2];
1130  * (it really works differently, but you can think of it as such)
1131  *
1132  * The key uploading and linking happens in the same command, the
1133  * add station command with STA_MODIFY_KEY_MASK.
1134  */
1135
1136 static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1137                             struct ieee80211_vif *vif,
1138                             struct ieee80211_sta *sta)
1139 {
1140         struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1141
1142         if (sta)
1143                 return iwl_sta_id(sta);
1144
1145         /*
1146          * The device expects GTKs for station interfaces to be
1147          * installed as GTKs for the AP station. If we have no
1148          * station ID, then use the ap_sta_id in that case.
1149          */
1150         if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1151                 return vif_priv->ctx->ap_sta_id;
1152
1153         return IWL_INVALID_STATION;
1154 }
1155
1156 static int iwlagn_send_sta_key(struct iwl_priv *priv,
1157                                struct ieee80211_key_conf *keyconf,
1158                                u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1159                                u32 cmd_flags)
1160 {
1161         __le16 key_flags;
1162         struct iwl_addsta_cmd sta_cmd;
1163         int i;
1164
1165         spin_lock_bh(&priv->sta_lock);
1166         memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1167         spin_unlock_bh(&priv->sta_lock);
1168
1169         key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1170         key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
1171
1172         switch (keyconf->cipher) {
1173         case WLAN_CIPHER_SUITE_CCMP:
1174                 key_flags |= STA_KEY_FLG_CCMP;
1175                 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1176                 break;
1177         case WLAN_CIPHER_SUITE_TKIP:
1178                 key_flags |= STA_KEY_FLG_TKIP;
1179                 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1180                 for (i = 0; i < 5; i++)
1181                         sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1182                 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1183                 break;
1184         case WLAN_CIPHER_SUITE_WEP104:
1185                 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
1186                 /* fall through */
1187         case WLAN_CIPHER_SUITE_WEP40:
1188                 key_flags |= STA_KEY_FLG_WEP;
1189                 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1190                 break;
1191         default:
1192                 WARN_ON(1);
1193                 return -EINVAL;
1194         }
1195
1196         if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1197                 key_flags |= STA_KEY_MULTICAST_MSK;
1198
1199         /* key pointer (offset) */
1200         sta_cmd.key.key_offset = keyconf->hw_key_idx;
1201
1202         sta_cmd.key.key_flags = key_flags;
1203         sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1204         sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1205
1206         return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
1207 }
1208
1209 void iwl_update_tkip_key(struct iwl_priv *priv,
1210                          struct ieee80211_vif *vif,
1211                          struct ieee80211_key_conf *keyconf,
1212                          struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1213 {
1214         u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1215
1216         if (sta_id == IWL_INVALID_STATION)
1217                 return;
1218
1219         if (iwl_scan_cancel(priv)) {
1220                 /* cancel scan failed, just live w/ bad key and rely
1221                    briefly on SW decryption */
1222                 return;
1223         }
1224
1225         iwlagn_send_sta_key(priv, keyconf, sta_id,
1226                             iv32, phase1key, CMD_ASYNC);
1227 }
1228
1229 int iwl_remove_dynamic_key(struct iwl_priv *priv,
1230                            struct iwl_rxon_context *ctx,
1231                            struct ieee80211_key_conf *keyconf,
1232                            struct ieee80211_sta *sta)
1233 {
1234         struct iwl_addsta_cmd sta_cmd;
1235         u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1236         __le16 key_flags;
1237
1238         /* if station isn't there, neither is the key */
1239         if (sta_id == IWL_INVALID_STATION)
1240                 return -ENOENT;
1241
1242         spin_lock_bh(&priv->sta_lock);
1243         memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1244         if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1245                 sta_id = IWL_INVALID_STATION;
1246         spin_unlock_bh(&priv->sta_lock);
1247
1248         if (sta_id == IWL_INVALID_STATION)
1249                 return 0;
1250
1251         lockdep_assert_held(&priv->mutex);
1252
1253         ctx->key_mapping_keys--;
1254
1255         IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1256                       keyconf->keyidx, sta_id);
1257
1258         if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1259                 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1260                         keyconf->hw_key_idx);
1261
1262         key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1263         key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1264                      STA_KEY_FLG_INVALID;
1265
1266         if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1267                 key_flags |= STA_KEY_MULTICAST_MSK;
1268
1269         sta_cmd.key.key_flags = key_flags;
1270         sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
1271         sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1272         sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1273
1274         return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1275 }
1276
1277 int iwl_set_dynamic_key(struct iwl_priv *priv,
1278                         struct iwl_rxon_context *ctx,
1279                         struct ieee80211_key_conf *keyconf,
1280                         struct ieee80211_sta *sta)
1281 {
1282         struct ieee80211_key_seq seq;
1283         u16 p1k[5];
1284         int ret;
1285         u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1286         const u8 *addr;
1287
1288         if (sta_id == IWL_INVALID_STATION)
1289                 return -EINVAL;
1290
1291         lockdep_assert_held(&priv->mutex);
1292
1293         keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1294         if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1295                 return -ENOSPC;
1296
1297         ctx->key_mapping_keys++;
1298
1299         switch (keyconf->cipher) {
1300         case WLAN_CIPHER_SUITE_TKIP:
1301                 if (sta)
1302                         addr = sta->addr;
1303                 else /* station mode case only */
1304                         addr = ctx->active.bssid_addr;
1305
1306                 /* pre-fill phase 1 key into device cache */
1307                 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1308                 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1309                 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1310                                           seq.tkip.iv32, p1k, CMD_SYNC);
1311                 break;
1312         case WLAN_CIPHER_SUITE_CCMP:
1313         case WLAN_CIPHER_SUITE_WEP40:
1314         case WLAN_CIPHER_SUITE_WEP104:
1315                 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1316                                           0, NULL, CMD_SYNC);
1317                 break;
1318         default:
1319                 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
1320                 ret = -EINVAL;
1321         }
1322
1323         if (ret) {
1324                 ctx->key_mapping_keys--;
1325                 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1326         }
1327
1328         IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1329                       keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1330                       sta ? sta->addr : NULL, ret);
1331
1332         return ret;
1333 }
1334
1335 /**
1336  * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1337  *
1338  * This adds the broadcast station into the driver's station table
1339  * and marks it driver active, so that it will be restored to the
1340  * device at the next best time.
1341  */
1342 int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1343                                struct iwl_rxon_context *ctx)
1344 {
1345         struct iwl_link_quality_cmd *link_cmd;
1346         u8 sta_id;
1347
1348         spin_lock_bh(&priv->sta_lock);
1349         sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1350         if (sta_id == IWL_INVALID_STATION) {
1351                 IWL_ERR(priv, "Unable to prepare broadcast station\n");
1352                 spin_unlock_bh(&priv->sta_lock);
1353
1354                 return -EINVAL;
1355         }
1356
1357         priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1358         priv->stations[sta_id].used |= IWL_STA_BCAST;
1359         spin_unlock_bh(&priv->sta_lock);
1360
1361         link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
1362         if (!link_cmd) {
1363                 IWL_ERR(priv,
1364                         "Unable to initialize rate scaling for bcast station.\n");
1365                 return -ENOMEM;
1366         }
1367
1368         spin_lock_bh(&priv->sta_lock);
1369         priv->stations[sta_id].lq = link_cmd;
1370         spin_unlock_bh(&priv->sta_lock);
1371
1372         return 0;
1373 }
1374
1375 /**
1376  * iwl_update_bcast_station - update broadcast station's LQ command
1377  *
1378  * Only used by iwlagn. Placed here to have all bcast station management
1379  * code together.
1380  */
1381 int iwl_update_bcast_station(struct iwl_priv *priv,
1382                              struct iwl_rxon_context *ctx)
1383 {
1384         struct iwl_link_quality_cmd *link_cmd;
1385         u8 sta_id = ctx->bcast_sta_id;
1386
1387         link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
1388         if (!link_cmd) {
1389                 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1390                 return -ENOMEM;
1391         }
1392
1393         spin_lock_bh(&priv->sta_lock);
1394         if (priv->stations[sta_id].lq)
1395                 kfree(priv->stations[sta_id].lq);
1396         else
1397                 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1398         priv->stations[sta_id].lq = link_cmd;
1399         spin_unlock_bh(&priv->sta_lock);
1400
1401         return 0;
1402 }
1403
1404 int iwl_update_bcast_stations(struct iwl_priv *priv)
1405 {
1406         struct iwl_rxon_context *ctx;
1407         int ret = 0;
1408
1409         for_each_context(priv, ctx) {
1410                 ret = iwl_update_bcast_station(priv, ctx);
1411                 if (ret)
1412                         break;
1413         }
1414
1415         return ret;
1416 }
1417
1418 /**
1419  * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1420  */
1421 int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1422 {
1423         struct iwl_addsta_cmd sta_cmd;
1424
1425         lockdep_assert_held(&priv->mutex);
1426
1427         /* Remove "disable" flag, to enable Tx for this TID */
1428         spin_lock_bh(&priv->sta_lock);
1429         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1430         priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1431         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1432         memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1433         spin_unlock_bh(&priv->sta_lock);
1434
1435         return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1436 }
1437
1438 int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1439                          int tid, u16 ssn)
1440 {
1441         int sta_id;
1442         struct iwl_addsta_cmd sta_cmd;
1443
1444         lockdep_assert_held(&priv->mutex);
1445
1446         sta_id = iwl_sta_id(sta);
1447         if (sta_id == IWL_INVALID_STATION)
1448                 return -ENXIO;
1449
1450         spin_lock_bh(&priv->sta_lock);
1451         priv->stations[sta_id].sta.station_flags_msk = 0;
1452         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1453         priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1454         priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1455         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1456         memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1457         spin_unlock_bh(&priv->sta_lock);
1458
1459         return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1460 }
1461
1462 int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1463                         int tid)
1464 {
1465         int sta_id;
1466         struct iwl_addsta_cmd sta_cmd;
1467
1468         lockdep_assert_held(&priv->mutex);
1469
1470         sta_id = iwl_sta_id(sta);
1471         if (sta_id == IWL_INVALID_STATION) {
1472                 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1473                 return -ENXIO;
1474         }
1475
1476         spin_lock_bh(&priv->sta_lock);
1477         priv->stations[sta_id].sta.station_flags_msk = 0;
1478         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1479         priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1480         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1481         memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1482         spin_unlock_bh(&priv->sta_lock);
1483
1484         return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1485 }
1486
1487
1488
1489 void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1490 {
1491         struct iwl_addsta_cmd cmd = {
1492                 .mode = STA_CONTROL_MODIFY_MSK,
1493                 .station_flags = STA_FLG_PWR_SAVE_MSK,
1494                 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1495                 .sta.sta_id = sta_id,
1496                 .sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1497                 .sleep_tx_count = cpu_to_le16(cnt),
1498         };
1499
1500         iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
1501 }