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