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