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