blob: c6b343fdf5f931488815f7415e1427e31bc3659b [file] [log] [blame]
Johannes Berga30e3112010-09-22 18:02:01 +02001/******************************************************************************
2 *
Wey-Yi Guyfb4961d2012-01-06 13:16:33 -08003 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
Johannes Berga30e3112010-09-22 18:02:01 +02004 *
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 *****************************************************************************/
Johannes Berge7a0d0c42012-03-06 13:31:07 -080029#include <linux/etherdevice.h>
Johannes Berga30e3112010-09-22 18:02:01 +020030#include <net/mac80211.h>
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070031#include "iwl-trans.h"
Johannes Berg1023fdc2012-05-15 12:16:34 +020032#include "dev.h"
33#include "agn.h"
Johannes Berga30e3112010-09-22 18:02:01 +020034
Meenakshi Venkataramanf32cadf2012-03-14 15:59:15 -070035const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
36
Wey-Yi Guy2da424b2012-01-06 13:16:28 -080037static int iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
Wey-Yi Guyc745f552011-10-10 07:27:12 -070038{
Johannes Bergfa23cb02012-03-05 11:24:25 -080039 lockdep_assert_held(&priv->sta_lock);
40
Wey-Yi Guy2da424b2012-01-06 13:16:28 -080041 if (sta_id >= IWLAGN_STATION_COUNT) {
42 IWL_ERR(priv, "invalid sta_id %u", sta_id);
43 return -EINVAL;
44 }
Wey-Yi Guyc745f552011-10-10 07:27:12 -070045 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 }
Wey-Yi Guy2da424b2012-01-06 13:16:28 -080060 return 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -070061}
62
63static int iwl_process_add_sta_resp(struct iwl_priv *priv,
64 struct iwl_addsta_cmd *addsta,
65 struct iwl_rx_packet *pkt)
66{
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080067 struct iwl_add_sta_resp *add_sta_resp = (void *)pkt->data;
Wey-Yi Guyc745f552011-10-10 07:27:12 -070068 u8 sta_id = addsta->sta.sta_id;
Wey-Yi Guyc745f552011-10-10 07:27:12 -070069 int ret = -EIO;
70
71 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
72 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
73 pkt->hdr.flags);
74 return ret;
75 }
76
77 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
78 sta_id);
79
Johannes Bergfa23cb02012-03-05 11:24:25 -080080 spin_lock(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -070081
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080082 switch (add_sta_resp->status) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -070083 case ADD_STA_SUCCESS_MSK:
84 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
Wey-Yi Guy2da424b2012-01-06 13:16:28 -080085 ret = iwl_sta_ucode_activate(priv, sta_id);
Wey-Yi Guyc745f552011-10-10 07:27:12 -070086 break;
87 case ADD_STA_NO_ROOM_IN_TABLE:
88 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
89 sta_id);
90 break;
91 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
92 IWL_ERR(priv, "Adding station %d failed, no block ack "
93 "resource.\n", sta_id);
94 break;
95 case ADD_STA_MODIFY_NON_EXIST_STA:
96 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
97 sta_id);
98 break;
99 default:
100 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800101 add_sta_resp->status);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700102 break;
103 }
104
105 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
106 priv->stations[sta_id].sta.mode ==
107 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
108 sta_id, priv->stations[sta_id].sta.sta.addr);
109
110 /*
111 * XXX: The MAC address in the command buffer is often changed from
112 * the original sent to the device. That is, the MAC address
113 * written to the command buffer often is not the same MAC address
114 * read from the command buffer when the command returns. This
115 * issue has not yet been resolved and this debugging is left to
116 * observe the problem.
117 */
118 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
119 priv->stations[sta_id].sta.mode ==
120 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
121 addsta->sta.addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800122 spin_unlock(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700123
124 return ret;
125}
126
Johannes Berg48a2d662012-03-05 11:24:39 -0800127int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700128 struct iwl_device_cmd *cmd)
129{
130 struct iwl_rx_packet *pkt = rxb_addr(rxb);
131 struct iwl_addsta_cmd *addsta =
132 (struct iwl_addsta_cmd *) cmd->payload;
133
134 return iwl_process_add_sta_resp(priv, addsta, pkt);
135}
136
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700137int iwl_send_add_sta(struct iwl_priv *priv,
138 struct iwl_addsta_cmd *sta, u8 flags)
139{
140 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700141 struct iwl_host_cmd cmd = {
142 .id = REPLY_ADD_STA,
143 .flags = flags,
Johannes Berg69b172f2011-12-12 04:17:44 -0800144 .data = { sta, },
145 .len = { sizeof(*sta), },
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700146 };
147 u8 sta_id __maybe_unused = sta->sta.sta_id;
148
149 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
150 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
151
152 if (!(flags & CMD_ASYNC)) {
153 cmd.flags |= CMD_WANT_SKB;
154 might_sleep();
155 }
156
Johannes Berge10a0532012-03-06 13:30:39 -0800157 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700158
159 if (ret || (flags & CMD_ASYNC))
160 return ret;
161 /*else the command was successfully sent in SYNC mode, need to free
162 * the reply page */
163
Johannes Berg65b94a42012-03-05 11:24:38 -0800164 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700165
166 if (cmd.handler_status)
167 IWL_ERR(priv, "%s - error in the CMD response %d", __func__,
168 cmd.handler_status);
169
170 return cmd.handler_status;
171}
172
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700173bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
174 struct iwl_rxon_context *ctx,
175 struct ieee80211_sta_ht_cap *ht_cap)
176{
177 if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
178 return false;
179
180 /*
181 * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
182 * the bit will not set if it is pure 40MHz case
183 */
184 if (ht_cap && !ht_cap->ht_supported)
185 return false;
186
187#ifdef CONFIG_IWLWIFI_DEBUGFS
188 if (priv->disable_ht40)
189 return false;
190#endif
191
Johannes Berg20041ea2012-05-21 12:56:31 +0200192 return true;
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700193}
194
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800195static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
196 struct ieee80211_sta *sta,
197 struct iwl_rxon_context *ctx,
198 __le32 *flags, __le32 *mask)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700199{
200 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700201 u8 mimo_ps_mode;
202
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800203 *mask = STA_FLG_RTS_MIMO_PROT_MSK |
204 STA_FLG_MIMO_DIS_MSK |
205 STA_FLG_HT40_EN_MSK |
206 STA_FLG_MAX_AGG_SIZE_MSK |
207 STA_FLG_AGG_MPDU_DENSITY_MSK;
208 *flags = 0;
209
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700210 if (!sta || !sta_ht_inf->ht_supported)
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800211 return;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700212
213 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800214
215 IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",
Meenakshi Venkataramana35e2702012-05-16 22:40:50 +0200216 sta->addr,
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700217 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
218 "static" :
219 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
220 "dynamic" : "disabled");
221
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700222 switch (mimo_ps_mode) {
223 case WLAN_HT_CAP_SM_PS_STATIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800224 *flags |= STA_FLG_MIMO_DIS_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700225 break;
226 case WLAN_HT_CAP_SM_PS_DYNAMIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800227 *flags |= STA_FLG_RTS_MIMO_PROT_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700228 break;
229 case WLAN_HT_CAP_SM_PS_DISABLED:
230 break;
231 default:
232 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
233 break;
234 }
235
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800236 *flags |= cpu_to_le32(
237 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700238
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800239 *flags |= cpu_to_le32(
240 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700241
242 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800243 *flags |= STA_FLG_HT40_EN_MSK;
244}
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700245
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800246int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
247 struct ieee80211_sta *sta)
248{
249 u8 sta_id = iwl_sta_id(sta);
250 __le32 flags, mask;
251 struct iwl_addsta_cmd cmd;
252
253 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
254 return -EINVAL;
255
256 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
257
258 spin_lock_bh(&priv->sta_lock);
259 priv->stations[sta_id].sta.station_flags &= ~mask;
260 priv->stations[sta_id].sta.station_flags |= flags;
261 spin_unlock_bh(&priv->sta_lock);
262
263 memset(&cmd, 0, sizeof(cmd));
264 cmd.mode = STA_CONTROL_MODIFY_MSK;
265 cmd.station_flags_msk = mask;
266 cmd.station_flags = flags;
267 cmd.sta.sta_id = sta_id;
268
269 return iwl_send_add_sta(priv, &cmd, CMD_SYNC);
270}
271
272static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
273 struct ieee80211_sta *sta,
274 struct iwl_rxon_context *ctx)
275{
276 __le32 flags, mask;
277
278 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
279
280 lockdep_assert_held(&priv->sta_lock);
281 priv->stations[index].sta.station_flags &= ~mask;
282 priv->stations[index].sta.station_flags |= flags;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700283}
284
285/**
286 * iwl_prep_station - Prepare station information for addition
287 *
288 * should be called with sta_lock held
289 */
290u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
291 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
292{
293 struct iwl_station_entry *station;
294 int i;
295 u8 sta_id = IWL_INVALID_STATION;
296
297 if (is_ap)
298 sta_id = ctx->ap_sta_id;
299 else if (is_broadcast_ether_addr(addr))
300 sta_id = ctx->bcast_sta_id;
301 else
302 for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
Joe Perches2e42e472012-05-09 17:17:46 +0000303 if (ether_addr_equal(priv->stations[i].sta.sta.addr,
304 addr)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700305 sta_id = i;
306 break;
307 }
308
309 if (!priv->stations[i].used &&
310 sta_id == IWL_INVALID_STATION)
311 sta_id = i;
312 }
313
314 /*
315 * These two conditions have the same outcome, but keep them
316 * separate
317 */
318 if (unlikely(sta_id == IWL_INVALID_STATION))
319 return sta_id;
320
321 /*
322 * uCode is not able to deal with multiple requests to add a
323 * station. Keep track if one is in progress so that we do not send
324 * another.
325 */
326 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
327 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
328 "added.\n", sta_id);
329 return sta_id;
330 }
331
332 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
333 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
Joe Perches2e42e472012-05-09 17:17:46 +0000334 ether_addr_equal(priv->stations[sta_id].sta.sta.addr, addr)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700335 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
336 "adding again.\n", sta_id, addr);
337 return sta_id;
338 }
339
340 station = &priv->stations[sta_id];
341 station->used = IWL_STA_DRIVER_ACTIVE;
342 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
343 sta_id, addr);
344 priv->num_stations++;
345
346 /* Set up the REPLY_ADD_STA command to send to device */
347 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
348 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
349 station->sta.mode = 0;
350 station->sta.sta.sta_id = sta_id;
351 station->sta.station_flags = ctx->station_flags;
352 station->ctxid = ctx->ctxid;
353
354 if (sta) {
355 struct iwl_station_priv *sta_priv;
356
357 sta_priv = (void *)sta->drv_priv;
358 sta_priv->ctx = ctx;
359 }
360
361 /*
362 * OK to call unconditionally, since local stations (IBSS BSSID
363 * STA and broadcast STA) pass in a NULL sta, and mac80211
364 * doesn't allow HT IBSS.
365 */
366 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
367
368 return sta_id;
369
370}
371
372#define STA_WAIT_TIMEOUT (HZ/2)
373
374/**
375 * iwl_add_station_common -
376 */
377int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
378 const u8 *addr, bool is_ap,
379 struct ieee80211_sta *sta, u8 *sta_id_r)
380{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700381 int ret = 0;
382 u8 sta_id;
383 struct iwl_addsta_cmd sta_cmd;
384
385 *sta_id_r = 0;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800386 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700387 sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
388 if (sta_id == IWL_INVALID_STATION) {
389 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
390 addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800391 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700392 return -EINVAL;
393 }
394
395 /*
396 * uCode is not able to deal with multiple requests to add a
397 * station. Keep track if one is in progress so that we do not send
398 * another.
399 */
400 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
401 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
402 "added.\n", sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800403 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700404 return -EEXIST;
405 }
406
407 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
408 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
409 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
410 "adding again.\n", sta_id, addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800411 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700412 return -EEXIST;
413 }
414
415 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
416 memcpy(&sta_cmd, &priv->stations[sta_id].sta,
417 sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -0800418 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700419
420 /* Add station to device's station table */
421 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
422 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800423 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700424 IWL_ERR(priv, "Adding station %pM failed.\n",
425 priv->stations[sta_id].sta.sta.addr);
426 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
427 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800428 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700429 }
430 *sta_id_r = sta_id;
431 return ret;
432}
433
434/**
435 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700436 */
437static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
438{
Johannes Bergfa23cb02012-03-05 11:24:25 -0800439 lockdep_assert_held(&priv->sta_lock);
440
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700441 /* Ucode must be active and driver must be non active */
442 if ((priv->stations[sta_id].used &
443 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
444 IWL_STA_UCODE_ACTIVE)
445 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
446
447 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
448
449 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
450 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
451}
452
453static int iwl_send_remove_station(struct iwl_priv *priv,
454 const u8 *addr, int sta_id,
455 bool temporary)
456{
457 struct iwl_rx_packet *pkt;
458 int ret;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700459 struct iwl_rem_sta_cmd rm_sta_cmd;
460
461 struct iwl_host_cmd cmd = {
462 .id = REPLY_REMOVE_STA,
463 .len = { sizeof(struct iwl_rem_sta_cmd), },
464 .flags = CMD_SYNC,
465 .data = { &rm_sta_cmd, },
466 };
467
468 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
469 rm_sta_cmd.num_sta = 1;
470 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
471
472 cmd.flags |= CMD_WANT_SKB;
473
Johannes Berge10a0532012-03-06 13:30:39 -0800474 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700475
476 if (ret)
477 return ret;
478
Johannes Berg65b94a42012-03-05 11:24:38 -0800479 pkt = cmd.resp_pkt;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700480 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
481 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
482 pkt->hdr.flags);
483 ret = -EIO;
484 }
485
486 if (!ret) {
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800487 struct iwl_rem_sta_resp *rem_sta_resp = (void *)pkt->data;
488 switch (rem_sta_resp->status) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700489 case REM_STA_SUCCESS_MSK:
490 if (!temporary) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800491 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700492 iwl_sta_ucode_deactivate(priv, sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800493 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700494 }
495 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
496 break;
497 default:
498 ret = -EIO;
499 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
500 break;
501 }
502 }
Johannes Berg65b94a42012-03-05 11:24:38 -0800503 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700504
505 return ret;
506}
507
508/**
509 * iwl_remove_station - Remove driver's knowledge of station.
510 */
511int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
512 const u8 *addr)
513{
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200514 u8 tid;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700515
Don Fry83626402012-03-07 09:52:37 -0800516 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700517 IWL_DEBUG_INFO(priv,
518 "Unable to remove station %pM, device not ready.\n",
519 addr);
520 /*
521 * It is typical for stations to be removed when we are
522 * going down. Return success since device will be down
523 * soon anyway
524 */
525 return 0;
526 }
527
528 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
529 sta_id, addr);
530
531 if (WARN_ON(sta_id == IWL_INVALID_STATION))
532 return -EINVAL;
533
Johannes Bergfa23cb02012-03-05 11:24:25 -0800534 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700535
536 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
537 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
538 addr);
539 goto out_err;
540 }
541
542 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
543 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
544 addr);
545 goto out_err;
546 }
547
548 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
549 kfree(priv->stations[sta_id].lq);
550 priv->stations[sta_id].lq = NULL;
551 }
552
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200553 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
554 memset(&priv->tid_data[sta_id][tid], 0,
555 sizeof(priv->tid_data[sta_id][tid]));
556
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700557 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
558
559 priv->num_stations--;
560
561 if (WARN_ON(priv->num_stations < 0))
562 priv->num_stations = 0;
563
Johannes Bergfa23cb02012-03-05 11:24:25 -0800564 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700565
566 return iwl_send_remove_station(priv, addr, sta_id, false);
567out_err:
Johannes Bergfa23cb02012-03-05 11:24:25 -0800568 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700569 return -EINVAL;
570}
571
Johannes Berg97420512012-03-07 09:52:42 -0800572void iwl_deactivate_station(struct iwl_priv *priv, const u8 sta_id,
573 const u8 *addr)
574{
575 u8 tid;
576
577 if (!iwl_is_ready(priv)) {
578 IWL_DEBUG_INFO(priv,
579 "Unable to remove station %pM, device not ready.\n",
580 addr);
581 return;
582 }
583
584 IWL_DEBUG_ASSOC(priv, "Deactivating STA: %pM (%d)\n", addr, sta_id);
585
586 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
587 return;
588
589 spin_lock_bh(&priv->sta_lock);
590
591 WARN_ON_ONCE(!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE));
592
593 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
594 memset(&priv->tid_data[sta_id][tid], 0,
595 sizeof(priv->tid_data[sta_id][tid]));
596
597 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
598
599 priv->num_stations--;
600
601 if (WARN_ON_ONCE(priv->num_stations < 0))
602 priv->num_stations = 0;
603
604 spin_unlock_bh(&priv->sta_lock);
605}
606
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100607static void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
608 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
609{
610 int i, r;
611 u32 rate_flags = 0;
612 __le32 rate_n_flags;
613
614 lockdep_assert_held(&priv->mutex);
615
616 memset(link_cmd, 0, sizeof(*link_cmd));
617
618 /* Set up the rate scaling to start at selected rate, fall back
619 * all the way down to 1M in IEEE order, and then spin on 1M */
620 if (priv->band == IEEE80211_BAND_5GHZ)
621 r = IWL_RATE_6M_INDEX;
622 else if (ctx && ctx->vif && ctx->vif->p2p)
623 r = IWL_RATE_6M_INDEX;
624 else
625 r = IWL_RATE_1M_INDEX;
626
627 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
628 rate_flags |= RATE_MCS_CCK_MSK;
629
630 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
631 RATE_MCS_ANT_POS;
632 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
633 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
634 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
635
636 link_cmd->general_params.single_stream_ant_msk =
637 first_antenna(priv->hw_params.valid_tx_ant);
638
639 link_cmd->general_params.dual_stream_ant_msk =
640 priv->hw_params.valid_tx_ant &
641 ~first_antenna(priv->hw_params.valid_tx_ant);
642 if (!link_cmd->general_params.dual_stream_ant_msk) {
643 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
644 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
645 link_cmd->general_params.dual_stream_ant_msk =
646 priv->hw_params.valid_tx_ant;
647 }
648
649 link_cmd->agg_params.agg_dis_start_th =
650 LINK_QUAL_AGG_DISABLE_START_DEF;
651 link_cmd->agg_params.agg_time_limit =
652 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
653
654 link_cmd->sta_id = sta_id;
655}
656
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700657/**
658 * iwl_clear_ucode_stations - clear ucode station table bits
659 *
660 * This function clears all the bits in the driver indicating
661 * which stations are active in the ucode. Call when something
662 * other than explicit station management would cause this in
663 * the ucode, e.g. unassociated RXON.
664 */
665void iwl_clear_ucode_stations(struct iwl_priv *priv,
666 struct iwl_rxon_context *ctx)
667{
668 int i;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700669 bool cleared = false;
670
671 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
672
Johannes Bergfa23cb02012-03-05 11:24:25 -0800673 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700674 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
675 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
676 continue;
677
678 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
679 IWL_DEBUG_INFO(priv,
680 "Clearing ucode active for station %d\n", i);
681 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
682 cleared = true;
683 }
684 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800685 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700686
687 if (!cleared)
688 IWL_DEBUG_INFO(priv,
689 "No active stations found to be cleared\n");
690}
691
692/**
693 * iwl_restore_stations() - Restore driver known stations to device
694 *
695 * All stations considered active by driver, but not present in ucode, is
696 * restored.
697 *
698 * Function sleeps.
699 */
700void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
701{
702 struct iwl_addsta_cmd sta_cmd;
703 struct iwl_link_quality_cmd lq;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700704 int i;
705 bool found = false;
706 int ret;
707 bool send_lq;
708
Don Fry83626402012-03-07 09:52:37 -0800709 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700710 IWL_DEBUG_INFO(priv,
711 "Not ready yet, not restoring any stations.\n");
712 return;
713 }
714
715 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -0800716 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700717 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
718 if (ctx->ctxid != priv->stations[i].ctxid)
719 continue;
720 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
721 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
722 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
723 priv->stations[i].sta.sta.addr);
724 priv->stations[i].sta.mode = 0;
725 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
726 found = true;
727 }
728 }
729
730 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
731 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
732 memcpy(&sta_cmd, &priv->stations[i].sta,
733 sizeof(struct iwl_addsta_cmd));
734 send_lq = false;
735 if (priv->stations[i].lq) {
Johannes Berg15b86bf2012-03-05 11:24:36 -0800736 if (priv->wowlan)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700737 iwl_sta_fill_lq(priv, ctx, i, &lq);
738 else
739 memcpy(&lq, priv->stations[i].lq,
740 sizeof(struct iwl_link_quality_cmd));
741 send_lq = true;
742 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800743 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700744 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
745 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800746 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700747 IWL_ERR(priv, "Adding station %pM failed.\n",
748 priv->stations[i].sta.sta.addr);
749 priv->stations[i].used &=
750 ~IWL_STA_DRIVER_ACTIVE;
751 priv->stations[i].used &=
752 ~IWL_STA_UCODE_INPROGRESS;
Meenakshi Venkataramanf4c37172012-05-16 22:49:48 +0200753 continue;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700754 }
755 /*
756 * Rate scaling has already been initialized, send
757 * current LQ command
758 */
759 if (send_lq)
760 iwl_send_lq_cmd(priv, ctx, &lq,
761 CMD_SYNC, true);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800762 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700763 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
764 }
765 }
766
Johannes Bergfa23cb02012-03-05 11:24:25 -0800767 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700768 if (!found)
769 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
770 "no stations to be restored.\n");
771 else
772 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
773 "complete.\n");
774}
775
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700776int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
777{
778 int i;
779
780 for (i = 0; i < priv->sta_key_max_num; i++)
781 if (!test_and_set_bit(i, &priv->ucode_key_table))
782 return i;
783
784 return WEP_INVALID_OFFSET;
785}
786
787void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
788{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700789 int i;
790
Johannes Bergfa23cb02012-03-05 11:24:25 -0800791 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700792 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
793 if (!(priv->stations[i].used & IWL_STA_BCAST))
794 continue;
795
796 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
797 priv->num_stations--;
798 if (WARN_ON(priv->num_stations < 0))
799 priv->num_stations = 0;
800 kfree(priv->stations[i].lq);
801 priv->stations[i].lq = NULL;
802 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800803 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700804}
805
806#ifdef CONFIG_IWLWIFI_DEBUG
807static void iwl_dump_lq_cmd(struct iwl_priv *priv,
808 struct iwl_link_quality_cmd *lq)
809{
810 int i;
811 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
812 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
813 lq->general_params.single_stream_ant_msk,
814 lq->general_params.dual_stream_ant_msk);
815
816 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
817 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
818 i, lq->rs_table[i].rate_n_flags);
819}
820#else
821static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
822 struct iwl_link_quality_cmd *lq)
823{
824}
825#endif
826
827/**
828 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
829 *
830 * It sometimes happens when a HT rate has been in use and we
831 * loose connectivity with AP then mac80211 will first tell us that the
832 * current channel is not HT anymore before removing the station. In such a
833 * scenario the RXON flags will be updated to indicate we are not
834 * communicating HT anymore, but the LQ command may still contain HT rates.
835 * Test for this to prevent driver from sending LQ command between the time
836 * RXON flags are updated and when LQ command is updated.
837 */
838static bool is_lq_table_valid(struct iwl_priv *priv,
839 struct iwl_rxon_context *ctx,
840 struct iwl_link_quality_cmd *lq)
841{
842 int i;
843
844 if (ctx->ht.enabled)
845 return true;
846
847 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
848 ctx->active.channel);
849 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
850 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
851 RATE_MCS_HT_MSK) {
852 IWL_DEBUG_INFO(priv,
853 "index %d of LQ expects HT channel\n",
854 i);
855 return false;
856 }
857 }
858 return true;
859}
860
861/**
862 * iwl_send_lq_cmd() - Send link quality command
863 * @init: This command is sent as part of station initialization right
864 * after station has been added.
865 *
866 * The link quality command is sent as the last step of station creation.
867 * This is the special case in which init is set and we call a callback in
868 * this case to clear the state indicating that station creation is in
869 * progress.
870 */
871int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
872 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
873{
874 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700875 struct iwl_host_cmd cmd = {
876 .id = REPLY_TX_LINK_QUALITY_CMD,
877 .len = { sizeof(struct iwl_link_quality_cmd), },
878 .flags = flags,
879 .data = { lq, },
880 };
881
882 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
883 return -EINVAL;
884
885
Johannes Bergfa23cb02012-03-05 11:24:25 -0800886 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700887 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800888 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700889 return -EINVAL;
890 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800891 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700892
893 iwl_dump_lq_cmd(priv, lq);
894 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
895 return -EINVAL;
896
897 if (is_lq_table_valid(priv, ctx, lq))
Johannes Berge10a0532012-03-06 13:30:39 -0800898 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700899 else
900 ret = -EINVAL;
901
902 if (cmd.flags & CMD_ASYNC)
903 return ret;
904
905 if (init) {
906 IWL_DEBUG_INFO(priv, "init LQ command complete, "
907 "clearing sta addition status for sta %d\n",
908 lq->sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800909 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700910 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800911 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700912 }
913 return ret;
914}
915
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700916
Johannes Bergc079166e2011-10-10 07:26:54 -0700917static struct iwl_link_quality_cmd *
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700918iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
919 u8 sta_id)
Johannes Bergc079166e2011-10-10 07:26:54 -0700920{
921 struct iwl_link_quality_cmd *link_cmd;
922
923 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
924 if (!link_cmd) {
925 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
926 return NULL;
927 }
928
929 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200930
931 return link_cmd;
932}
933
934/*
935 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
936 *
937 * Function sleeps.
938 */
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700939int iwlagn_add_bssid_station(struct iwl_priv *priv,
940 struct iwl_rxon_context *ctx,
Johannes Berga30e3112010-09-22 18:02:01 +0200941 const u8 *addr, u8 *sta_id_r)
942{
943 int ret;
944 u8 sta_id;
945 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +0200946
947 if (sta_id_r)
948 *sta_id_r = IWL_INVALID_STATION;
949
950 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
951 if (ret) {
952 IWL_ERR(priv, "Unable to add station %pM\n", addr);
953 return ret;
954 }
955
956 if (sta_id_r)
957 *sta_id_r = sta_id;
958
Johannes Bergfa23cb02012-03-05 11:24:25 -0800959 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200960 priv->stations[sta_id].used |= IWL_STA_LOCAL;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800961 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200962
963 /* Set up default rate scaling table in device's station table */
Johannes Bergf775aa06d2011-06-22 06:34:09 -0700964 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200965 if (!link_cmd) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700966 IWL_ERR(priv,
967 "Unable to initialize rate scaling for station %pM.\n",
Johannes Berga30e3112010-09-22 18:02:01 +0200968 addr);
969 return -ENOMEM;
970 }
971
972 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
973 if (ret)
974 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
975
Johannes Bergfa23cb02012-03-05 11:24:25 -0800976 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200977 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800978 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200979
980 return 0;
981}
982
Johannes Berg5a3d9882011-07-15 13:03:12 -0700983/*
984 * static WEP keys
985 *
986 * For each context, the device has a table of 4 static WEP keys
987 * (one for each key index) that is updated with the following
988 * commands.
989 */
990
Johannes Berga30e3112010-09-22 18:02:01 +0200991static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
992 struct iwl_rxon_context *ctx,
993 bool send_if_empty)
994{
995 int i, not_empty = 0;
996 u8 buff[sizeof(struct iwl_wep_cmd) +
997 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
998 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
999 size_t cmd_size = sizeof(struct iwl_wep_cmd);
1000 struct iwl_host_cmd cmd = {
1001 .id = ctx->wep_key_cmd,
Johannes Berg3fa50732011-05-04 07:50:38 -07001002 .data = { wep_cmd, },
Johannes Berga30e3112010-09-22 18:02:01 +02001003 .flags = CMD_SYNC,
1004 };
1005
1006 might_sleep();
1007
1008 memset(wep_cmd, 0, cmd_size +
1009 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
1010
1011 for (i = 0; i < WEP_KEYS_MAX ; i++) {
1012 wep_cmd->key[i].key_index = i;
1013 if (ctx->wep_keys[i].key_size) {
1014 wep_cmd->key[i].key_offset = i;
1015 not_empty = 1;
1016 } else {
1017 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
1018 }
1019
1020 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
1021 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
1022 ctx->wep_keys[i].key_size);
1023 }
1024
1025 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
1026 wep_cmd->num_keys = WEP_KEYS_MAX;
1027
1028 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
1029
Johannes Berg3fa50732011-05-04 07:50:38 -07001030 cmd.len[0] = cmd_size;
Johannes Berga30e3112010-09-22 18:02:01 +02001031
1032 if (not_empty || send_if_empty)
Johannes Berge10a0532012-03-06 13:30:39 -08001033 return iwl_dvm_send_cmd(priv, &cmd);
Johannes Berga30e3112010-09-22 18:02:01 +02001034 else
1035 return 0;
1036}
1037
1038int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1039 struct iwl_rxon_context *ctx)
1040{
Johannes Bergb1eea292012-03-06 13:30:42 -08001041 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001042
1043 return iwl_send_static_wepkey_cmd(priv, ctx, false);
1044}
1045
1046int iwl_remove_default_wep_key(struct iwl_priv *priv,
1047 struct iwl_rxon_context *ctx,
1048 struct ieee80211_key_conf *keyconf)
1049{
1050 int ret;
1051
Johannes Bergb1eea292012-03-06 13:30:42 -08001052 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001053
1054 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1055 keyconf->keyidx);
1056
1057 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
Don Fry83626402012-03-07 09:52:37 -08001058 if (iwl_is_rfkill(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001059 IWL_DEBUG_WEP(priv,
1060 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berga30e3112010-09-22 18:02:01 +02001061 /* but keys in device are clear anyway so return success */
1062 return 0;
1063 }
1064 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1065 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1066 keyconf->keyidx, ret);
1067
1068 return ret;
1069}
1070
1071int iwl_set_default_wep_key(struct iwl_priv *priv,
1072 struct iwl_rxon_context *ctx,
1073 struct ieee80211_key_conf *keyconf)
1074{
1075 int ret;
1076
Johannes Bergb1eea292012-03-06 13:30:42 -08001077 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001078
1079 if (keyconf->keylen != WEP_KEY_LEN_128 &&
1080 keyconf->keylen != WEP_KEY_LEN_64) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001081 IWL_DEBUG_WEP(priv,
1082 "Bad WEP key length %d\n", keyconf->keylen);
Johannes Berga30e3112010-09-22 18:02:01 +02001083 return -EINVAL;
1084 }
1085
Johannes Berg5a3d9882011-07-15 13:03:12 -07001086 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
Johannes Berga30e3112010-09-22 18:02:01 +02001087
1088 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1089 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1090 keyconf->keylen);
1091
1092 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1093 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1094 keyconf->keylen, keyconf->keyidx, ret);
1095
1096 return ret;
1097}
1098
Johannes Berg5a3d9882011-07-15 13:03:12 -07001099/*
1100 * dynamic (per-station) keys
1101 *
1102 * The dynamic keys are a little more complicated. The device has
1103 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1104 * These are linked to stations by a table that contains an index
1105 * into the key table for each station/key index/{mcast,unicast},
1106 * i.e. it's basically an array of pointers like this:
1107 * key_offset_t key_mapping[NUM_STATIONS][4][2];
1108 * (it really works differently, but you can think of it as such)
1109 *
1110 * The key uploading and linking happens in the same command, the
1111 * add station command with STA_MODIFY_KEY_MASK.
1112 */
1113
1114static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1115 struct ieee80211_vif *vif,
1116 struct ieee80211_sta *sta)
1117{
1118 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001119
1120 if (sta)
Johannes Berg40e4e682012-03-05 11:24:22 -08001121 return iwl_sta_id(sta);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001122
1123 /*
1124 * The device expects GTKs for station interfaces to be
1125 * installed as GTKs for the AP station. If we have no
1126 * station ID, then use the ap_sta_id in that case.
1127 */
Johannes Berg40e4e682012-03-05 11:24:22 -08001128 if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1129 return vif_priv->ctx->ap_sta_id;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001130
Johannes Berg40e4e682012-03-05 11:24:22 -08001131 return IWL_INVALID_STATION;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001132}
1133
Johannes Berge1b1c082011-07-17 01:44:11 -07001134static int iwlagn_send_sta_key(struct iwl_priv *priv,
1135 struct ieee80211_key_conf *keyconf,
1136 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1137 u32 cmd_flags)
Johannes Berga30e3112010-09-22 18:02:01 +02001138{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001139 __le16 key_flags;
Johannes Berga30e3112010-09-22 18:02:01 +02001140 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001141 int i;
Johannes Berge1b1c082011-07-17 01:44:11 -07001142
Johannes Bergfa23cb02012-03-05 11:24:25 -08001143 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001144 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001145 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001146
Johannes Berg5a3d9882011-07-15 13:03:12 -07001147 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1148 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001149
Johannes Berg5a3d9882011-07-15 13:03:12 -07001150 switch (keyconf->cipher) {
1151 case WLAN_CIPHER_SUITE_CCMP:
1152 key_flags |= STA_KEY_FLG_CCMP;
1153 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1154 break;
1155 case WLAN_CIPHER_SUITE_TKIP:
1156 key_flags |= STA_KEY_FLG_TKIP;
1157 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1158 for (i = 0; i < 5; i++)
1159 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1160 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1161 break;
1162 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga30e3112010-09-22 18:02:01 +02001163 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001164 /* fall through */
1165 case WLAN_CIPHER_SUITE_WEP40:
1166 key_flags |= STA_KEY_FLG_WEP;
1167 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1168 break;
1169 default:
1170 WARN_ON(1);
1171 return -EINVAL;
1172 }
Johannes Berga30e3112010-09-22 18:02:01 +02001173
Johannes Berg5a3d9882011-07-15 13:03:12 -07001174 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
Johannes Berga30e3112010-09-22 18:02:01 +02001175 key_flags |= STA_KEY_MULTICAST_MSK;
1176
Johannes Berg5a3d9882011-07-15 13:03:12 -07001177 /* key pointer (offset) */
1178 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berga30e3112010-09-22 18:02:01 +02001179
Johannes Berg5a3d9882011-07-15 13:03:12 -07001180 sta_cmd.key.key_flags = key_flags;
1181 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1182 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
Johannes Berga30e3112010-09-22 18:02:01 +02001183
Johannes Berg5a3d9882011-07-15 13:03:12 -07001184 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001185}
1186
1187void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001188 struct ieee80211_vif *vif,
Johannes Berga30e3112010-09-22 18:02:01 +02001189 struct ieee80211_key_conf *keyconf,
1190 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1191{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001192 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1193
1194 if (sta_id == IWL_INVALID_STATION)
1195 return;
Johannes Berga30e3112010-09-22 18:02:01 +02001196
1197 if (iwl_scan_cancel(priv)) {
1198 /* cancel scan failed, just live w/ bad key and rely
1199 briefly on SW decryption */
1200 return;
1201 }
1202
Johannes Berge1b1c082011-07-17 01:44:11 -07001203 iwlagn_send_sta_key(priv, keyconf, sta_id,
1204 iv32, phase1key, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001205}
1206
1207int iwl_remove_dynamic_key(struct iwl_priv *priv,
1208 struct iwl_rxon_context *ctx,
1209 struct ieee80211_key_conf *keyconf,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001210 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001211{
Johannes Berga30e3112010-09-22 18:02:01 +02001212 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001213 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
Johannes Berg5dcbf482012-02-17 09:47:14 -08001214 __le16 key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001215
1216 /* if station isn't there, neither is the key */
1217 if (sta_id == IWL_INVALID_STATION)
1218 return -ENOENT;
1219
Johannes Bergfa23cb02012-03-05 11:24:25 -08001220 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001221 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1222 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1223 sta_id = IWL_INVALID_STATION;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001224 spin_unlock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001225
1226 if (sta_id == IWL_INVALID_STATION)
1227 return 0;
Johannes Berga30e3112010-09-22 18:02:01 +02001228
Johannes Bergb1eea292012-03-06 13:30:42 -08001229 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001230
1231 ctx->key_mapping_keys--;
1232
Johannes Berga30e3112010-09-22 18:02:01 +02001233 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1234 keyconf->keyidx, sta_id);
1235
Johannes Berg5a3d9882011-07-15 13:03:12 -07001236 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1237 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1238 keyconf->hw_key_idx);
Johannes Berga30e3112010-09-22 18:02:01 +02001239
Johannes Berg5dcbf482012-02-17 09:47:14 -08001240 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1241 key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1242 STA_KEY_FLG_INVALID;
1243
1244 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1245 key_flags |= STA_KEY_MULTICAST_MSK;
1246
1247 sta_cmd.key.key_flags = key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001248 sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
1249 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1250 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001251
1252 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1253}
1254
Johannes Berg5a3d9882011-07-15 13:03:12 -07001255int iwl_set_dynamic_key(struct iwl_priv *priv,
1256 struct iwl_rxon_context *ctx,
1257 struct ieee80211_key_conf *keyconf,
1258 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001259{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001260 struct ieee80211_key_seq seq;
1261 u16 p1k[5];
Johannes Berga30e3112010-09-22 18:02:01 +02001262 int ret;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001263 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1264 const u8 *addr;
1265
1266 if (sta_id == IWL_INVALID_STATION)
1267 return -EINVAL;
Johannes Berga30e3112010-09-22 18:02:01 +02001268
Johannes Bergb1eea292012-03-06 13:30:42 -08001269 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001270
Johannes Berg5a3d9882011-07-15 13:03:12 -07001271 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1272 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1273 return -ENOSPC;
1274
Johannes Berga30e3112010-09-22 18:02:01 +02001275 ctx->key_mapping_keys++;
Johannes Berga30e3112010-09-22 18:02:01 +02001276
1277 switch (keyconf->cipher) {
Johannes Berga30e3112010-09-22 18:02:01 +02001278 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001279 if (sta)
1280 addr = sta->addr;
1281 else /* station mode case only */
1282 addr = ctx->active.bssid_addr;
1283
1284 /* pre-fill phase 1 key into device cache */
1285 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1286 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Berge1b1c082011-07-17 01:44:11 -07001287 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1288 seq.tkip.iv32, p1k, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001289 break;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001290 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berga30e3112010-09-22 18:02:01 +02001291 case WLAN_CIPHER_SUITE_WEP40:
1292 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge1b1c082011-07-17 01:44:11 -07001293 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1294 0, NULL, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001295 break;
1296 default:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001297 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
Johannes Berga30e3112010-09-22 18:02:01 +02001298 ret = -EINVAL;
1299 }
1300
Johannes Berg5a3d9882011-07-15 13:03:12 -07001301 if (ret) {
1302 ctx->key_mapping_keys--;
1303 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1304 }
1305
1306 IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
Johannes Berga30e3112010-09-22 18:02:01 +02001307 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001308 sta ? sta->addr : NULL, ret);
Johannes Berga30e3112010-09-22 18:02:01 +02001309
1310 return ret;
1311}
1312
1313/**
1314 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1315 *
1316 * This adds the broadcast station into the driver's station table
1317 * and marks it driver active, so that it will be restored to the
1318 * device at the next best time.
1319 */
1320int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1321 struct iwl_rxon_context *ctx)
1322{
1323 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +02001324 u8 sta_id;
1325
Johannes Bergfa23cb02012-03-05 11:24:25 -08001326 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001327 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1328 if (sta_id == IWL_INVALID_STATION) {
1329 IWL_ERR(priv, "Unable to prepare broadcast station\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -08001330 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001331
1332 return -EINVAL;
1333 }
1334
1335 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1336 priv->stations[sta_id].used |= IWL_STA_BCAST;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001337 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001338
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001339 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001340 if (!link_cmd) {
1341 IWL_ERR(priv,
1342 "Unable to initialize rate scaling for bcast station.\n");
1343 return -ENOMEM;
1344 }
1345
Johannes Bergfa23cb02012-03-05 11:24:25 -08001346 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001347 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001348 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001349
1350 return 0;
1351}
1352
1353/**
1354 * iwl_update_bcast_station - update broadcast station's LQ command
1355 *
1356 * Only used by iwlagn. Placed here to have all bcast station management
1357 * code together.
1358 */
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001359int iwl_update_bcast_station(struct iwl_priv *priv,
1360 struct iwl_rxon_context *ctx)
Johannes Berga30e3112010-09-22 18:02:01 +02001361{
Johannes Berga30e3112010-09-22 18:02:01 +02001362 struct iwl_link_quality_cmd *link_cmd;
1363 u8 sta_id = ctx->bcast_sta_id;
1364
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001365 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001366 if (!link_cmd) {
1367 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1368 return -ENOMEM;
1369 }
1370
Johannes Bergfa23cb02012-03-05 11:24:25 -08001371 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001372 if (priv->stations[sta_id].lq)
1373 kfree(priv->stations[sta_id].lq);
1374 else
1375 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1376 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001377 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001378
1379 return 0;
1380}
1381
1382int iwl_update_bcast_stations(struct iwl_priv *priv)
1383{
1384 struct iwl_rxon_context *ctx;
1385 int ret = 0;
1386
1387 for_each_context(priv, ctx) {
1388 ret = iwl_update_bcast_station(priv, ctx);
1389 if (ret)
1390 break;
1391 }
1392
1393 return ret;
1394}
1395
1396/**
1397 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1398 */
1399int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1400{
Johannes Berga30e3112010-09-22 18:02:01 +02001401 struct iwl_addsta_cmd sta_cmd;
1402
Johannes Bergb1eea292012-03-06 13:30:42 -08001403 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001404
1405 /* Remove "disable" flag, to enable Tx for this TID */
Johannes Bergfa23cb02012-03-05 11:24:25 -08001406 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001407 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1408 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1409 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1410 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001411 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001412
1413 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1414}
1415
1416int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1417 int tid, u16 ssn)
1418{
Johannes Berga30e3112010-09-22 18:02:01 +02001419 int sta_id;
1420 struct iwl_addsta_cmd sta_cmd;
1421
Johannes Bergb1eea292012-03-06 13:30:42 -08001422 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001423
1424 sta_id = iwl_sta_id(sta);
1425 if (sta_id == IWL_INVALID_STATION)
1426 return -ENXIO;
1427
Johannes Bergfa23cb02012-03-05 11:24:25 -08001428 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001429 priv->stations[sta_id].sta.station_flags_msk = 0;
1430 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1431 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1432 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1433 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1434 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001435 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001436
1437 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1438}
1439
1440int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1441 int tid)
1442{
Johannes Berga30e3112010-09-22 18:02:01 +02001443 int sta_id;
1444 struct iwl_addsta_cmd sta_cmd;
1445
Johannes Bergb1eea292012-03-06 13:30:42 -08001446 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001447
1448 sta_id = iwl_sta_id(sta);
1449 if (sta_id == IWL_INVALID_STATION) {
1450 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1451 return -ENXIO;
1452 }
1453
Johannes Bergfa23cb02012-03-05 11:24:25 -08001454 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001455 priv->stations[sta_id].sta.station_flags_msk = 0;
1456 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1457 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1458 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1459 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001460 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001461
1462 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1463}
1464
Johannes Berga30e3112010-09-22 18:02:01 +02001465
Johannes Berga30e3112010-09-22 18:02:01 +02001466
1467void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1468{
Johannes Berg9451ca12012-03-05 11:24:23 -08001469 struct iwl_addsta_cmd cmd = {
1470 .mode = STA_CONTROL_MODIFY_MSK,
1471 .station_flags = STA_FLG_PWR_SAVE_MSK,
1472 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1473 .sta.sta_id = sta_id,
1474 .sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1475 .sleep_tx_count = cpu_to_le16(cnt),
1476 };
Johannes Berga30e3112010-09-22 18:02:01 +02001477
Johannes Berg9451ca12012-03-05 11:24:23 -08001478 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001479}