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