Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / net / wireless / iwlwifi / iwl-agn-sta.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2011 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-sta.h"
35 #include "iwl-agn.h"
36
37 static struct iwl_link_quality_cmd *
38 iwl_sta_alloc_lq(struct iwl_priv *priv, u8 sta_id)
39 {
40         int i, r;
41         struct iwl_link_quality_cmd *link_cmd;
42         u32 rate_flags = 0;
43         __le32 rate_n_flags;
44
45         link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
46         if (!link_cmd) {
47                 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
48                 return NULL;
49         }
50         /* Set up the rate scaling to start at selected rate, fall back
51          * all the way down to 1M in IEEE order, and then spin on 1M */
52         if (priv->band == IEEE80211_BAND_5GHZ)
53                 r = IWL_RATE_6M_INDEX;
54         else
55                 r = IWL_RATE_1M_INDEX;
56
57         if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
58                 rate_flags |= RATE_MCS_CCK_MSK;
59
60         rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
61                                 RATE_MCS_ANT_POS;
62         rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
63         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
64                 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
65
66         link_cmd->general_params.single_stream_ant_msk =
67                                 first_antenna(priv->hw_params.valid_tx_ant);
68
69         link_cmd->general_params.dual_stream_ant_msk =
70                 priv->hw_params.valid_tx_ant &
71                 ~first_antenna(priv->hw_params.valid_tx_ant);
72         if (!link_cmd->general_params.dual_stream_ant_msk) {
73                 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
74         } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
75                 link_cmd->general_params.dual_stream_ant_msk =
76                         priv->hw_params.valid_tx_ant;
77         }
78
79         link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
80         link_cmd->agg_params.agg_time_limit =
81                 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
82
83         link_cmd->sta_id = sta_id;
84
85         return link_cmd;
86 }
87
88 /*
89  * iwlagn_add_bssid_station - Add the special IBSS BSSID station
90  *
91  * Function sleeps.
92  */
93 int iwlagn_add_bssid_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
94                              const u8 *addr, u8 *sta_id_r)
95 {
96         int ret;
97         u8 sta_id;
98         struct iwl_link_quality_cmd *link_cmd;
99         unsigned long flags;
100
101         if (sta_id_r)
102                 *sta_id_r = IWL_INVALID_STATION;
103
104         ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
105         if (ret) {
106                 IWL_ERR(priv, "Unable to add station %pM\n", addr);
107                 return ret;
108         }
109
110         if (sta_id_r)
111                 *sta_id_r = sta_id;
112
113         spin_lock_irqsave(&priv->sta_lock, flags);
114         priv->stations[sta_id].used |= IWL_STA_LOCAL;
115         spin_unlock_irqrestore(&priv->sta_lock, flags);
116
117         /* Set up default rate scaling table in device's station table */
118         link_cmd = iwl_sta_alloc_lq(priv, sta_id);
119         if (!link_cmd) {
120                 IWL_ERR(priv, "Unable to initialize rate scaling for station %pM.\n",
121                         addr);
122                 return -ENOMEM;
123         }
124
125         ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
126         if (ret)
127                 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
128
129         spin_lock_irqsave(&priv->sta_lock, flags);
130         priv->stations[sta_id].lq = link_cmd;
131         spin_unlock_irqrestore(&priv->sta_lock, flags);
132
133         return 0;
134 }
135
136 static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
137                                       struct iwl_rxon_context *ctx,
138                                       bool send_if_empty)
139 {
140         int i, not_empty = 0;
141         u8 buff[sizeof(struct iwl_wep_cmd) +
142                 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
143         struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
144         size_t cmd_size  = sizeof(struct iwl_wep_cmd);
145         struct iwl_host_cmd cmd = {
146                 .id = ctx->wep_key_cmd,
147                 .data = wep_cmd,
148                 .flags = CMD_SYNC,
149         };
150
151         might_sleep();
152
153         memset(wep_cmd, 0, cmd_size +
154                         (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
155
156         for (i = 0; i < WEP_KEYS_MAX ; i++) {
157                 wep_cmd->key[i].key_index = i;
158                 if (ctx->wep_keys[i].key_size) {
159                         wep_cmd->key[i].key_offset = i;
160                         not_empty = 1;
161                 } else {
162                         wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
163                 }
164
165                 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
166                 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
167                                 ctx->wep_keys[i].key_size);
168         }
169
170         wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
171         wep_cmd->num_keys = WEP_KEYS_MAX;
172
173         cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
174
175         cmd.len = cmd_size;
176
177         if (not_empty || send_if_empty)
178                 return iwl_send_cmd(priv, &cmd);
179         else
180                 return 0;
181 }
182
183 int iwl_restore_default_wep_keys(struct iwl_priv *priv,
184                                  struct iwl_rxon_context *ctx)
185 {
186         lockdep_assert_held(&priv->mutex);
187
188         return iwl_send_static_wepkey_cmd(priv, ctx, false);
189 }
190
191 int iwl_remove_default_wep_key(struct iwl_priv *priv,
192                                struct iwl_rxon_context *ctx,
193                                struct ieee80211_key_conf *keyconf)
194 {
195         int ret;
196
197         lockdep_assert_held(&priv->mutex);
198
199         IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
200                       keyconf->keyidx);
201
202         memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
203         if (iwl_is_rfkill(priv)) {
204                 IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
205                 /* but keys in device are clear anyway so return success */
206                 return 0;
207         }
208         ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
209         IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
210                       keyconf->keyidx, ret);
211
212         return ret;
213 }
214
215 int iwl_set_default_wep_key(struct iwl_priv *priv,
216                             struct iwl_rxon_context *ctx,
217                             struct ieee80211_key_conf *keyconf)
218 {
219         int ret;
220
221         lockdep_assert_held(&priv->mutex);
222
223         if (keyconf->keylen != WEP_KEY_LEN_128 &&
224             keyconf->keylen != WEP_KEY_LEN_64) {
225                 IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
226                 return -EINVAL;
227         }
228
229         keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
230         keyconf->hw_key_idx = HW_KEY_DEFAULT;
231         priv->stations[ctx->ap_sta_id].keyinfo.cipher = keyconf->cipher;
232
233         ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
234         memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
235                                                         keyconf->keylen);
236
237         ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
238         IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
239                 keyconf->keylen, keyconf->keyidx, ret);
240
241         return ret;
242 }
243
244 static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
245                                         struct iwl_rxon_context *ctx,
246                                         struct ieee80211_key_conf *keyconf,
247                                         u8 sta_id)
248 {
249         unsigned long flags;
250         __le16 key_flags = 0;
251         struct iwl_addsta_cmd sta_cmd;
252
253         lockdep_assert_held(&priv->mutex);
254
255         keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
256
257         key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
258         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
259         key_flags &= ~STA_KEY_FLG_INVALID;
260
261         if (keyconf->keylen == WEP_KEY_LEN_128)
262                 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
263
264         if (sta_id == ctx->bcast_sta_id)
265                 key_flags |= STA_KEY_MULTICAST_MSK;
266
267         spin_lock_irqsave(&priv->sta_lock, flags);
268
269         priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
270         priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
271         priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
272
273         memcpy(priv->stations[sta_id].keyinfo.key,
274                                 keyconf->key, keyconf->keylen);
275
276         memcpy(&priv->stations[sta_id].sta.key.key[3],
277                                 keyconf->key, keyconf->keylen);
278
279         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
280                         == STA_KEY_FLG_NO_ENC)
281                 priv->stations[sta_id].sta.key.key_offset =
282                                  iwl_get_free_ucode_key_index(priv);
283         /* else, we are overriding an existing key => no need to allocated room
284          * in uCode. */
285
286         WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
287                 "no space for a new key");
288
289         priv->stations[sta_id].sta.key.key_flags = key_flags;
290         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
291         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
292
293         memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
294         spin_unlock_irqrestore(&priv->sta_lock, flags);
295
296         return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
297 }
298
299 static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
300                                          struct iwl_rxon_context *ctx,
301                                          struct ieee80211_key_conf *keyconf,
302                                          u8 sta_id)
303 {
304         unsigned long flags;
305         __le16 key_flags = 0;
306         struct iwl_addsta_cmd sta_cmd;
307
308         lockdep_assert_held(&priv->mutex);
309
310         key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
311         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
312         key_flags &= ~STA_KEY_FLG_INVALID;
313
314         if (sta_id == ctx->bcast_sta_id)
315                 key_flags |= STA_KEY_MULTICAST_MSK;
316
317         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
318
319         spin_lock_irqsave(&priv->sta_lock, flags);
320         priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
321         priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
322
323         memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
324                keyconf->keylen);
325
326         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
327                keyconf->keylen);
328
329         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
330                         == STA_KEY_FLG_NO_ENC)
331                 priv->stations[sta_id].sta.key.key_offset =
332                                  iwl_get_free_ucode_key_index(priv);
333         /* else, we are overriding an existing key => no need to allocated room
334          * in uCode. */
335
336         WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
337                 "no space for a new key");
338
339         priv->stations[sta_id].sta.key.key_flags = key_flags;
340         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
341         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
342
343         memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
344         spin_unlock_irqrestore(&priv->sta_lock, flags);
345
346         return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
347 }
348
349 static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
350                                          struct iwl_rxon_context *ctx,
351                                          struct ieee80211_key_conf *keyconf,
352                                          u8 sta_id)
353 {
354         unsigned long flags;
355         int ret = 0;
356         __le16 key_flags = 0;
357
358         key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
359         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
360         key_flags &= ~STA_KEY_FLG_INVALID;
361
362         if (sta_id == ctx->bcast_sta_id)
363                 key_flags |= STA_KEY_MULTICAST_MSK;
364
365         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
366         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
367
368         spin_lock_irqsave(&priv->sta_lock, flags);
369
370         priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
371         priv->stations[sta_id].keyinfo.keylen = 16;
372
373         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
374                         == STA_KEY_FLG_NO_ENC)
375                 priv->stations[sta_id].sta.key.key_offset =
376                                  iwl_get_free_ucode_key_index(priv);
377         /* else, we are overriding an existing key => no need to allocated room
378          * in uCode. */
379
380         WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
381                 "no space for a new key");
382
383         priv->stations[sta_id].sta.key.key_flags = key_flags;
384
385
386         /* This copy is acutally not needed: we get the key with each TX */
387         memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
388
389         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
390
391         spin_unlock_irqrestore(&priv->sta_lock, flags);
392
393         return ret;
394 }
395
396 void iwl_update_tkip_key(struct iwl_priv *priv,
397                          struct iwl_rxon_context *ctx,
398                          struct ieee80211_key_conf *keyconf,
399                          struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
400 {
401         u8 sta_id;
402         unsigned long flags;
403         int i;
404
405         if (iwl_scan_cancel(priv)) {
406                 /* cancel scan failed, just live w/ bad key and rely
407                    briefly on SW decryption */
408                 return;
409         }
410
411         sta_id = iwl_sta_id_or_broadcast(priv, ctx, sta);
412         if (sta_id == IWL_INVALID_STATION)
413                 return;
414
415         spin_lock_irqsave(&priv->sta_lock, flags);
416
417         priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
418
419         for (i = 0; i < 5; i++)
420                 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
421                         cpu_to_le16(phase1key[i]);
422
423         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
424         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
425
426         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
427
428         spin_unlock_irqrestore(&priv->sta_lock, flags);
429
430 }
431
432 int iwl_remove_dynamic_key(struct iwl_priv *priv,
433                            struct iwl_rxon_context *ctx,
434                            struct ieee80211_key_conf *keyconf,
435                            u8 sta_id)
436 {
437         unsigned long flags;
438         u16 key_flags;
439         u8 keyidx;
440         struct iwl_addsta_cmd sta_cmd;
441
442         lockdep_assert_held(&priv->mutex);
443
444         ctx->key_mapping_keys--;
445
446         spin_lock_irqsave(&priv->sta_lock, flags);
447         key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
448         keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
449
450         IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
451                       keyconf->keyidx, sta_id);
452
453         if (keyconf->keyidx != keyidx) {
454                 /* We need to remove a key with index different that the one
455                  * in the uCode. This means that the key we need to remove has
456                  * been replaced by another one with different index.
457                  * Don't do anything and return ok
458                  */
459                 spin_unlock_irqrestore(&priv->sta_lock, flags);
460                 return 0;
461         }
462
463         if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
464                 IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
465                             keyconf->keyidx, key_flags);
466                 spin_unlock_irqrestore(&priv->sta_lock, flags);
467                 return 0;
468         }
469
470         if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
471                 &priv->ucode_key_table))
472                 IWL_ERR(priv, "index %d not used in uCode key table.\n",
473                         priv->stations[sta_id].sta.key.key_offset);
474         memset(&priv->stations[sta_id].keyinfo, 0,
475                                         sizeof(struct iwl_hw_key));
476         memset(&priv->stations[sta_id].sta.key, 0,
477                                         sizeof(struct iwl_keyinfo));
478         priv->stations[sta_id].sta.key.key_flags =
479                         STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
480         priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
481         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
482         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
483
484         if (iwl_is_rfkill(priv)) {
485                 IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
486                 spin_unlock_irqrestore(&priv->sta_lock, flags);
487                 return 0;
488         }
489         memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
490         spin_unlock_irqrestore(&priv->sta_lock, flags);
491
492         return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
493 }
494
495 int iwl_set_dynamic_key(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
496                         struct ieee80211_key_conf *keyconf, u8 sta_id)
497 {
498         int ret;
499
500         lockdep_assert_held(&priv->mutex);
501
502         ctx->key_mapping_keys++;
503         keyconf->hw_key_idx = HW_KEY_DYNAMIC;
504
505         switch (keyconf->cipher) {
506         case WLAN_CIPHER_SUITE_CCMP:
507                 ret = iwl_set_ccmp_dynamic_key_info(priv, ctx, keyconf, sta_id);
508                 break;
509         case WLAN_CIPHER_SUITE_TKIP:
510                 ret = iwl_set_tkip_dynamic_key_info(priv, ctx, keyconf, sta_id);
511                 break;
512         case WLAN_CIPHER_SUITE_WEP40:
513         case WLAN_CIPHER_SUITE_WEP104:
514                 ret = iwl_set_wep_dynamic_key_info(priv, ctx, keyconf, sta_id);
515                 break;
516         default:
517                 IWL_ERR(priv,
518                         "Unknown alg: %s cipher = %x\n", __func__,
519                         keyconf->cipher);
520                 ret = -EINVAL;
521         }
522
523         IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
524                       keyconf->cipher, keyconf->keylen, keyconf->keyidx,
525                       sta_id, ret);
526
527         return ret;
528 }
529
530 /**
531  * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
532  *
533  * This adds the broadcast station into the driver's station table
534  * and marks it driver active, so that it will be restored to the
535  * device at the next best time.
536  */
537 int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
538                                struct iwl_rxon_context *ctx)
539 {
540         struct iwl_link_quality_cmd *link_cmd;
541         unsigned long flags;
542         u8 sta_id;
543
544         spin_lock_irqsave(&priv->sta_lock, flags);
545         sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
546         if (sta_id == IWL_INVALID_STATION) {
547                 IWL_ERR(priv, "Unable to prepare broadcast station\n");
548                 spin_unlock_irqrestore(&priv->sta_lock, flags);
549
550                 return -EINVAL;
551         }
552
553         priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
554         priv->stations[sta_id].used |= IWL_STA_BCAST;
555         spin_unlock_irqrestore(&priv->sta_lock, flags);
556
557         link_cmd = iwl_sta_alloc_lq(priv, sta_id);
558         if (!link_cmd) {
559                 IWL_ERR(priv,
560                         "Unable to initialize rate scaling for bcast station.\n");
561                 return -ENOMEM;
562         }
563
564         spin_lock_irqsave(&priv->sta_lock, flags);
565         priv->stations[sta_id].lq = link_cmd;
566         spin_unlock_irqrestore(&priv->sta_lock, flags);
567
568         return 0;
569 }
570
571 /**
572  * iwl_update_bcast_station - update broadcast station's LQ command
573  *
574  * Only used by iwlagn. Placed here to have all bcast station management
575  * code together.
576  */
577 static int iwl_update_bcast_station(struct iwl_priv *priv,
578                                     struct iwl_rxon_context *ctx)
579 {
580         unsigned long flags;
581         struct iwl_link_quality_cmd *link_cmd;
582         u8 sta_id = ctx->bcast_sta_id;
583
584         link_cmd = iwl_sta_alloc_lq(priv, sta_id);
585         if (!link_cmd) {
586                 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
587                 return -ENOMEM;
588         }
589
590         spin_lock_irqsave(&priv->sta_lock, flags);
591         if (priv->stations[sta_id].lq)
592                 kfree(priv->stations[sta_id].lq);
593         else
594                 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
595         priv->stations[sta_id].lq = link_cmd;
596         spin_unlock_irqrestore(&priv->sta_lock, flags);
597
598         return 0;
599 }
600
601 int iwl_update_bcast_stations(struct iwl_priv *priv)
602 {
603         struct iwl_rxon_context *ctx;
604         int ret = 0;
605
606         for_each_context(priv, ctx) {
607                 ret = iwl_update_bcast_station(priv, ctx);
608                 if (ret)
609                         break;
610         }
611
612         return ret;
613 }
614
615 /**
616  * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
617  */
618 int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
619 {
620         unsigned long flags;
621         struct iwl_addsta_cmd sta_cmd;
622
623         lockdep_assert_held(&priv->mutex);
624
625         /* Remove "disable" flag, to enable Tx for this TID */
626         spin_lock_irqsave(&priv->sta_lock, flags);
627         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
628         priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
629         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
630         memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
631         spin_unlock_irqrestore(&priv->sta_lock, flags);
632
633         return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
634 }
635
636 int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
637                          int tid, u16 ssn)
638 {
639         unsigned long flags;
640         int sta_id;
641         struct iwl_addsta_cmd sta_cmd;
642
643         lockdep_assert_held(&priv->mutex);
644
645         sta_id = iwl_sta_id(sta);
646         if (sta_id == IWL_INVALID_STATION)
647                 return -ENXIO;
648
649         spin_lock_irqsave(&priv->sta_lock, flags);
650         priv->stations[sta_id].sta.station_flags_msk = 0;
651         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
652         priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
653         priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
654         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
655         memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
656         spin_unlock_irqrestore(&priv->sta_lock, flags);
657
658         return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
659 }
660
661 int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
662                         int tid)
663 {
664         unsigned long flags;
665         int sta_id;
666         struct iwl_addsta_cmd sta_cmd;
667
668         lockdep_assert_held(&priv->mutex);
669
670         sta_id = iwl_sta_id(sta);
671         if (sta_id == IWL_INVALID_STATION) {
672                 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
673                 return -ENXIO;
674         }
675
676         spin_lock_irqsave(&priv->sta_lock, flags);
677         priv->stations[sta_id].sta.station_flags_msk = 0;
678         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
679         priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
680         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
681         memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
682         spin_unlock_irqrestore(&priv->sta_lock, flags);
683
684         return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
685 }
686
687 static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
688 {
689         unsigned long flags;
690
691         spin_lock_irqsave(&priv->sta_lock, flags);
692         priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
693         priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
694         priv->stations[sta_id].sta.sta.modify_mask = 0;
695         priv->stations[sta_id].sta.sleep_tx_count = 0;
696         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
697         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
698         spin_unlock_irqrestore(&priv->sta_lock, flags);
699
700 }
701
702 void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
703 {
704         unsigned long flags;
705
706         spin_lock_irqsave(&priv->sta_lock, flags);
707         priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
708         priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
709         priv->stations[sta_id].sta.sta.modify_mask =
710                                         STA_MODIFY_SLEEP_TX_COUNT_MSK;
711         priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
712         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
713         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
714         spin_unlock_irqrestore(&priv->sta_lock, flags);
715
716 }
717
718 void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
719                            struct ieee80211_vif *vif,
720                            enum sta_notify_cmd cmd,
721                            struct ieee80211_sta *sta)
722 {
723         struct iwl_priv *priv = hw->priv;
724         struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
725         int sta_id;
726
727         switch (cmd) {
728         case STA_NOTIFY_SLEEP:
729                 WARN_ON(!sta_priv->client);
730                 sta_priv->asleep = true;
731                 if (atomic_read(&sta_priv->pending_frames) > 0)
732                         ieee80211_sta_block_awake(hw, sta, true);
733                 break;
734         case STA_NOTIFY_AWAKE:
735                 WARN_ON(!sta_priv->client);
736                 if (!sta_priv->asleep)
737                         break;
738                 sta_priv->asleep = false;
739                 sta_id = iwl_sta_id(sta);
740                 if (sta_id != IWL_INVALID_STATION)
741                         iwl_sta_modify_ps_wake(priv, sta_id);
742                 break;
743         default:
744                 break;
745         }
746 }