blob: 6deab38c7aee6a79dd9f64e36b5a15aad055efc7 [file] [log] [blame]
Johannes Berga30e3112010-09-22 18:02:01 +02001/******************************************************************************
2 *
Johannes Berg128e63e2013-01-21 21:39:26 +01003 * Copyright(c) 2003 - 2013 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);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700131
Johannes Berg513aa3b2012-09-20 16:06:14 +0200132 if (!cmd)
133 return 0;
134
135 return iwl_process_add_sta_resp(priv, (void *)cmd->payload, pkt);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700136}
137
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700138int iwl_send_add_sta(struct iwl_priv *priv,
139 struct iwl_addsta_cmd *sta, u8 flags)
140{
141 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700142 struct iwl_host_cmd cmd = {
143 .id = REPLY_ADD_STA,
144 .flags = flags,
Johannes Berg69b172f2011-12-12 04:17:44 -0800145 .data = { sta, },
146 .len = { sizeof(*sta), },
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700147 };
148 u8 sta_id __maybe_unused = sta->sta.sta_id;
149
150 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
151 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
152
153 if (!(flags & CMD_ASYNC)) {
Emmanuel Grumbach96791422012-07-24 01:58:32 +0300154 cmd.flags |= CMD_WANT_SKB | CMD_WANT_HCMD;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700155 might_sleep();
156 }
157
Johannes Berge10a0532012-03-06 13:30:39 -0800158 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700159
160 if (ret || (flags & CMD_ASYNC))
161 return ret;
162 /*else the command was successfully sent in SYNC mode, need to free
163 * the reply page */
164
Johannes Berg65b94a42012-03-05 11:24:38 -0800165 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700166
167 if (cmd.handler_status)
168 IWL_ERR(priv, "%s - error in the CMD response %d", __func__,
169 cmd.handler_status);
170
171 return cmd.handler_status;
172}
173
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700174bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
175 struct iwl_rxon_context *ctx,
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100176 struct ieee80211_sta *sta)
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700177{
178 if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
179 return false;
180
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700181#ifdef CONFIG_IWLWIFI_DEBUGFS
182 if (priv->disable_ht40)
183 return false;
184#endif
185
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100186 /* special case for RXON */
187 if (!sta)
Johannes Berg26a7ca92012-05-21 11:55:54 +0200188 return true;
189
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100190 return sta->bandwidth >= IEEE80211_STA_RX_BW_40;
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700191}
192
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800193static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
194 struct ieee80211_sta *sta,
195 struct iwl_rxon_context *ctx,
196 __le32 *flags, __le32 *mask)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700197{
198 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700199 u8 mimo_ps_mode;
200
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800201 *mask = STA_FLG_RTS_MIMO_PROT_MSK |
202 STA_FLG_MIMO_DIS_MSK |
203 STA_FLG_HT40_EN_MSK |
204 STA_FLG_MAX_AGG_SIZE_MSK |
205 STA_FLG_AGG_MPDU_DENSITY_MSK;
206 *flags = 0;
207
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700208 if (!sta || !sta_ht_inf->ht_supported)
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800209 return;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700210
211 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800212
213 IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",
Meenakshi Venkataramana35e2702012-05-16 22:40:50 +0200214 sta->addr,
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700215 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
216 "static" :
217 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
218 "dynamic" : "disabled");
219
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700220 switch (mimo_ps_mode) {
221 case WLAN_HT_CAP_SM_PS_STATIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800222 *flags |= STA_FLG_MIMO_DIS_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700223 break;
224 case WLAN_HT_CAP_SM_PS_DYNAMIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800225 *flags |= STA_FLG_RTS_MIMO_PROT_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700226 break;
227 case WLAN_HT_CAP_SM_PS_DISABLED:
228 break;
229 default:
230 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
231 break;
232 }
233
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800234 *flags |= cpu_to_le32(
235 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700236
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800237 *flags |= cpu_to_le32(
238 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700239
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100240 if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800241 *flags |= STA_FLG_HT40_EN_MSK;
242}
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700243
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800244int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
245 struct ieee80211_sta *sta)
246{
247 u8 sta_id = iwl_sta_id(sta);
248 __le32 flags, mask;
249 struct iwl_addsta_cmd cmd;
250
251 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
252 return -EINVAL;
253
254 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
255
256 spin_lock_bh(&priv->sta_lock);
257 priv->stations[sta_id].sta.station_flags &= ~mask;
258 priv->stations[sta_id].sta.station_flags |= flags;
259 spin_unlock_bh(&priv->sta_lock);
260
261 memset(&cmd, 0, sizeof(cmd));
262 cmd.mode = STA_CONTROL_MODIFY_MSK;
263 cmd.station_flags_msk = mask;
264 cmd.station_flags = flags;
265 cmd.sta.sta_id = sta_id;
266
267 return iwl_send_add_sta(priv, &cmd, CMD_SYNC);
268}
269
270static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
271 struct ieee80211_sta *sta,
272 struct iwl_rxon_context *ctx)
273{
274 __le32 flags, mask;
275
276 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
277
278 lockdep_assert_held(&priv->sta_lock);
279 priv->stations[index].sta.station_flags &= ~mask;
280 priv->stations[index].sta.station_flags |= flags;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700281}
282
283/**
284 * iwl_prep_station - Prepare station information for addition
285 *
286 * should be called with sta_lock held
287 */
288u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
289 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
290{
291 struct iwl_station_entry *station;
292 int i;
293 u8 sta_id = IWL_INVALID_STATION;
294
295 if (is_ap)
296 sta_id = ctx->ap_sta_id;
297 else if (is_broadcast_ether_addr(addr))
298 sta_id = ctx->bcast_sta_id;
299 else
300 for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
Joe Perches2e42e472012-05-09 17:17:46 +0000301 if (ether_addr_equal(priv->stations[i].sta.sta.addr,
302 addr)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700303 sta_id = i;
304 break;
305 }
306
307 if (!priv->stations[i].used &&
308 sta_id == IWL_INVALID_STATION)
309 sta_id = i;
310 }
311
312 /*
313 * These two conditions have the same outcome, but keep them
314 * separate
315 */
316 if (unlikely(sta_id == IWL_INVALID_STATION))
317 return sta_id;
318
319 /*
320 * uCode is not able to deal with multiple requests to add a
321 * station. Keep track if one is in progress so that we do not send
322 * another.
323 */
324 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
325 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
326 "added.\n", sta_id);
327 return sta_id;
328 }
329
330 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
331 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
Joe Perches2e42e472012-05-09 17:17:46 +0000332 ether_addr_equal(priv->stations[sta_id].sta.sta.addr, addr)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700333 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
334 "adding again.\n", sta_id, addr);
335 return sta_id;
336 }
337
338 station = &priv->stations[sta_id];
339 station->used = IWL_STA_DRIVER_ACTIVE;
340 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
341 sta_id, addr);
342 priv->num_stations++;
343
344 /* Set up the REPLY_ADD_STA command to send to device */
345 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
346 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
347 station->sta.mode = 0;
348 station->sta.sta.sta_id = sta_id;
349 station->sta.station_flags = ctx->station_flags;
350 station->ctxid = ctx->ctxid;
351
352 if (sta) {
353 struct iwl_station_priv *sta_priv;
354
355 sta_priv = (void *)sta->drv_priv;
356 sta_priv->ctx = ctx;
357 }
358
359 /*
360 * OK to call unconditionally, since local stations (IBSS BSSID
361 * STA and broadcast STA) pass in a NULL sta, and mac80211
362 * doesn't allow HT IBSS.
363 */
364 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
365
366 return sta_id;
367
368}
369
370#define STA_WAIT_TIMEOUT (HZ/2)
371
372/**
373 * iwl_add_station_common -
374 */
375int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
376 const u8 *addr, bool is_ap,
377 struct ieee80211_sta *sta, u8 *sta_id_r)
378{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700379 int ret = 0;
380 u8 sta_id;
381 struct iwl_addsta_cmd sta_cmd;
382
383 *sta_id_r = 0;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800384 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700385 sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
386 if (sta_id == IWL_INVALID_STATION) {
387 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
388 addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800389 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700390 return -EINVAL;
391 }
392
393 /*
394 * uCode is not able to deal with multiple requests to add a
395 * station. Keep track if one is in progress so that we do not send
396 * another.
397 */
398 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
399 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
400 "added.\n", sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800401 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700402 return -EEXIST;
403 }
404
405 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
406 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
407 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
408 "adding again.\n", sta_id, addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800409 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700410 return -EEXIST;
411 }
412
413 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
414 memcpy(&sta_cmd, &priv->stations[sta_id].sta,
415 sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -0800416 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700417
418 /* Add station to device's station table */
419 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
420 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800421 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700422 IWL_ERR(priv, "Adding station %pM failed.\n",
423 priv->stations[sta_id].sta.sta.addr);
424 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
425 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800426 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700427 }
428 *sta_id_r = sta_id;
429 return ret;
430}
431
432/**
433 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700434 */
435static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
436{
Johannes Bergfa23cb02012-03-05 11:24:25 -0800437 lockdep_assert_held(&priv->sta_lock);
438
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700439 /* Ucode must be active and driver must be non active */
440 if ((priv->stations[sta_id].used &
441 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
442 IWL_STA_UCODE_ACTIVE)
443 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
444
445 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
446
447 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
448 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
449}
450
451static int iwl_send_remove_station(struct iwl_priv *priv,
452 const u8 *addr, int sta_id,
453 bool temporary)
454{
455 struct iwl_rx_packet *pkt;
456 int ret;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700457 struct iwl_rem_sta_cmd rm_sta_cmd;
458
459 struct iwl_host_cmd cmd = {
460 .id = REPLY_REMOVE_STA,
461 .len = { sizeof(struct iwl_rem_sta_cmd), },
462 .flags = CMD_SYNC,
463 .data = { &rm_sta_cmd, },
464 };
465
466 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
467 rm_sta_cmd.num_sta = 1;
468 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
469
470 cmd.flags |= CMD_WANT_SKB;
471
Johannes Berge10a0532012-03-06 13:30:39 -0800472 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700473
474 if (ret)
475 return ret;
476
Johannes Berg65b94a42012-03-05 11:24:38 -0800477 pkt = cmd.resp_pkt;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700478 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
479 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
480 pkt->hdr.flags);
481 ret = -EIO;
482 }
483
484 if (!ret) {
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800485 struct iwl_rem_sta_resp *rem_sta_resp = (void *)pkt->data;
486 switch (rem_sta_resp->status) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700487 case REM_STA_SUCCESS_MSK:
488 if (!temporary) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800489 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700490 iwl_sta_ucode_deactivate(priv, sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800491 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700492 }
493 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
494 break;
495 default:
496 ret = -EIO;
497 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
498 break;
499 }
500 }
Johannes Berg65b94a42012-03-05 11:24:38 -0800501 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700502
503 return ret;
504}
505
506/**
507 * iwl_remove_station - Remove driver's knowledge of station.
508 */
509int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
510 const u8 *addr)
511{
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200512 u8 tid;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700513
Don Fry83626402012-03-07 09:52:37 -0800514 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700515 IWL_DEBUG_INFO(priv,
516 "Unable to remove station %pM, device not ready.\n",
517 addr);
518 /*
519 * It is typical for stations to be removed when we are
520 * going down. Return success since device will be down
521 * soon anyway
522 */
523 return 0;
524 }
525
526 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
527 sta_id, addr);
528
529 if (WARN_ON(sta_id == IWL_INVALID_STATION))
530 return -EINVAL;
531
Johannes Bergfa23cb02012-03-05 11:24:25 -0800532 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700533
534 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
535 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
536 addr);
537 goto out_err;
538 }
539
540 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
541 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
542 addr);
543 goto out_err;
544 }
545
546 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
547 kfree(priv->stations[sta_id].lq);
548 priv->stations[sta_id].lq = NULL;
549 }
550
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200551 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
552 memset(&priv->tid_data[sta_id][tid], 0,
553 sizeof(priv->tid_data[sta_id][tid]));
554
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700555 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
556
557 priv->num_stations--;
558
559 if (WARN_ON(priv->num_stations < 0))
560 priv->num_stations = 0;
561
Johannes Bergfa23cb02012-03-05 11:24:25 -0800562 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700563
564 return iwl_send_remove_station(priv, addr, sta_id, false);
565out_err:
Johannes Bergfa23cb02012-03-05 11:24:25 -0800566 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700567 return -EINVAL;
568}
569
Johannes Berg97420512012-03-07 09:52:42 -0800570void iwl_deactivate_station(struct iwl_priv *priv, const u8 sta_id,
571 const u8 *addr)
572{
573 u8 tid;
574
575 if (!iwl_is_ready(priv)) {
576 IWL_DEBUG_INFO(priv,
577 "Unable to remove station %pM, device not ready.\n",
578 addr);
579 return;
580 }
581
582 IWL_DEBUG_ASSOC(priv, "Deactivating STA: %pM (%d)\n", addr, sta_id);
583
584 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
585 return;
586
587 spin_lock_bh(&priv->sta_lock);
588
589 WARN_ON_ONCE(!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE));
590
591 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
592 memset(&priv->tid_data[sta_id][tid], 0,
593 sizeof(priv->tid_data[sta_id][tid]));
594
595 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
596
597 priv->num_stations--;
598
599 if (WARN_ON_ONCE(priv->num_stations < 0))
600 priv->num_stations = 0;
601
602 spin_unlock_bh(&priv->sta_lock);
603}
604
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100605static void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
606 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
607{
608 int i, r;
609 u32 rate_flags = 0;
610 __le32 rate_n_flags;
611
612 lockdep_assert_held(&priv->mutex);
613
614 memset(link_cmd, 0, sizeof(*link_cmd));
615
616 /* Set up the rate scaling to start at selected rate, fall back
617 * all the way down to 1M in IEEE order, and then spin on 1M */
618 if (priv->band == IEEE80211_BAND_5GHZ)
619 r = IWL_RATE_6M_INDEX;
620 else if (ctx && ctx->vif && ctx->vif->p2p)
621 r = IWL_RATE_6M_INDEX;
622 else
623 r = IWL_RATE_1M_INDEX;
624
625 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
626 rate_flags |= RATE_MCS_CCK_MSK;
627
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200628 rate_flags |= first_antenna(priv->nvm_data->valid_tx_ant) <<
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100629 RATE_MCS_ANT_POS;
630 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
631 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
632 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
633
634 link_cmd->general_params.single_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200635 first_antenna(priv->nvm_data->valid_tx_ant);
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100636
637 link_cmd->general_params.dual_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200638 priv->nvm_data->valid_tx_ant &
639 ~first_antenna(priv->nvm_data->valid_tx_ant);
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100640 if (!link_cmd->general_params.dual_stream_ant_msk) {
641 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200642 } else if (num_of_ant(priv->nvm_data->valid_tx_ant) == 2) {
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100643 link_cmd->general_params.dual_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200644 priv->nvm_data->valid_tx_ant;
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100645 }
646
647 link_cmd->agg_params.agg_dis_start_th =
648 LINK_QUAL_AGG_DISABLE_START_DEF;
649 link_cmd->agg_params.agg_time_limit =
650 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
651
652 link_cmd->sta_id = sta_id;
653}
654
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700655/**
656 * iwl_clear_ucode_stations - clear ucode station table bits
657 *
658 * This function clears all the bits in the driver indicating
659 * which stations are active in the ucode. Call when something
660 * other than explicit station management would cause this in
661 * the ucode, e.g. unassociated RXON.
662 */
663void iwl_clear_ucode_stations(struct iwl_priv *priv,
664 struct iwl_rxon_context *ctx)
665{
666 int i;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700667 bool cleared = false;
668
669 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
670
Johannes Bergfa23cb02012-03-05 11:24:25 -0800671 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700672 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
673 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
674 continue;
675
676 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
677 IWL_DEBUG_INFO(priv,
678 "Clearing ucode active for station %d\n", i);
679 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
680 cleared = true;
681 }
682 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800683 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700684
685 if (!cleared)
686 IWL_DEBUG_INFO(priv,
687 "No active stations found to be cleared\n");
688}
689
690/**
691 * iwl_restore_stations() - Restore driver known stations to device
692 *
693 * All stations considered active by driver, but not present in ucode, is
694 * restored.
695 *
696 * Function sleeps.
697 */
698void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
699{
700 struct iwl_addsta_cmd sta_cmd;
701 struct iwl_link_quality_cmd lq;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700702 int i;
703 bool found = false;
704 int ret;
705 bool send_lq;
706
Don Fry83626402012-03-07 09:52:37 -0800707 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700708 IWL_DEBUG_INFO(priv,
709 "Not ready yet, not restoring any stations.\n");
710 return;
711 }
712
713 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -0800714 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700715 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
716 if (ctx->ctxid != priv->stations[i].ctxid)
717 continue;
718 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
719 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
720 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
721 priv->stations[i].sta.sta.addr);
722 priv->stations[i].sta.mode = 0;
723 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
724 found = true;
725 }
726 }
727
728 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
729 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
730 memcpy(&sta_cmd, &priv->stations[i].sta,
731 sizeof(struct iwl_addsta_cmd));
732 send_lq = false;
733 if (priv->stations[i].lq) {
Johannes Berg15b86bf2012-03-05 11:24:36 -0800734 if (priv->wowlan)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700735 iwl_sta_fill_lq(priv, ctx, i, &lq);
736 else
737 memcpy(&lq, priv->stations[i].lq,
738 sizeof(struct iwl_link_quality_cmd));
739 send_lq = true;
740 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800741 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700742 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
743 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800744 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700745 IWL_ERR(priv, "Adding station %pM failed.\n",
746 priv->stations[i].sta.sta.addr);
747 priv->stations[i].used &=
748 ~IWL_STA_DRIVER_ACTIVE;
749 priv->stations[i].used &=
750 ~IWL_STA_UCODE_INPROGRESS;
Meenakshi Venkataramanf4c37172012-05-16 22:49:48 +0200751 continue;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700752 }
753 /*
754 * Rate scaling has already been initialized, send
755 * current LQ command
756 */
757 if (send_lq)
758 iwl_send_lq_cmd(priv, ctx, &lq,
759 CMD_SYNC, true);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800760 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700761 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
762 }
763 }
764
Johannes Bergfa23cb02012-03-05 11:24:25 -0800765 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700766 if (!found)
767 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
768 "no stations to be restored.\n");
769 else
770 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
771 "complete.\n");
772}
773
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700774int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
775{
776 int i;
777
778 for (i = 0; i < priv->sta_key_max_num; i++)
779 if (!test_and_set_bit(i, &priv->ucode_key_table))
780 return i;
781
782 return WEP_INVALID_OFFSET;
783}
784
785void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
786{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700787 int i;
788
Johannes Bergfa23cb02012-03-05 11:24:25 -0800789 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700790 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
791 if (!(priv->stations[i].used & IWL_STA_BCAST))
792 continue;
793
794 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
795 priv->num_stations--;
796 if (WARN_ON(priv->num_stations < 0))
797 priv->num_stations = 0;
798 kfree(priv->stations[i].lq);
799 priv->stations[i].lq = NULL;
800 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800801 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700802}
803
804#ifdef CONFIG_IWLWIFI_DEBUG
805static void iwl_dump_lq_cmd(struct iwl_priv *priv,
806 struct iwl_link_quality_cmd *lq)
807{
808 int i;
809 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
810 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
811 lq->general_params.single_stream_ant_msk,
812 lq->general_params.dual_stream_ant_msk);
813
814 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
815 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
816 i, lq->rs_table[i].rate_n_flags);
817}
818#else
819static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
820 struct iwl_link_quality_cmd *lq)
821{
822}
823#endif
824
825/**
826 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
827 *
828 * It sometimes happens when a HT rate has been in use and we
829 * loose connectivity with AP then mac80211 will first tell us that the
830 * current channel is not HT anymore before removing the station. In such a
831 * scenario the RXON flags will be updated to indicate we are not
832 * communicating HT anymore, but the LQ command may still contain HT rates.
833 * Test for this to prevent driver from sending LQ command between the time
834 * RXON flags are updated and when LQ command is updated.
835 */
836static bool is_lq_table_valid(struct iwl_priv *priv,
837 struct iwl_rxon_context *ctx,
838 struct iwl_link_quality_cmd *lq)
839{
840 int i;
841
842 if (ctx->ht.enabled)
843 return true;
844
845 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
846 ctx->active.channel);
847 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
848 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
849 RATE_MCS_HT_MSK) {
850 IWL_DEBUG_INFO(priv,
851 "index %d of LQ expects HT channel\n",
852 i);
853 return false;
854 }
855 }
856 return true;
857}
858
859/**
860 * iwl_send_lq_cmd() - Send link quality command
861 * @init: This command is sent as part of station initialization right
862 * after station has been added.
863 *
864 * The link quality command is sent as the last step of station creation.
865 * This is the special case in which init is set and we call a callback in
866 * this case to clear the state indicating that station creation is in
867 * progress.
868 */
869int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
870 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
871{
872 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700873 struct iwl_host_cmd cmd = {
874 .id = REPLY_TX_LINK_QUALITY_CMD,
875 .len = { sizeof(struct iwl_link_quality_cmd), },
876 .flags = flags,
877 .data = { lq, },
878 };
879
880 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
881 return -EINVAL;
882
883
Johannes Bergfa23cb02012-03-05 11:24:25 -0800884 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700885 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800886 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700887 return -EINVAL;
888 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800889 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700890
891 iwl_dump_lq_cmd(priv, lq);
892 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
893 return -EINVAL;
894
895 if (is_lq_table_valid(priv, ctx, lq))
Johannes Berge10a0532012-03-06 13:30:39 -0800896 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700897 else
898 ret = -EINVAL;
899
900 if (cmd.flags & CMD_ASYNC)
901 return ret;
902
903 if (init) {
904 IWL_DEBUG_INFO(priv, "init LQ command complete, "
905 "clearing sta addition status for sta %d\n",
906 lq->sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800907 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700908 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800909 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700910 }
911 return ret;
912}
913
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700914
Johannes Bergc079166e2011-10-10 07:26:54 -0700915static struct iwl_link_quality_cmd *
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700916iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
917 u8 sta_id)
Johannes Bergc079166e2011-10-10 07:26:54 -0700918{
919 struct iwl_link_quality_cmd *link_cmd;
920
921 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
922 if (!link_cmd) {
923 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
924 return NULL;
925 }
926
927 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200928
929 return link_cmd;
930}
931
932/*
933 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
934 *
935 * Function sleeps.
936 */
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700937int iwlagn_add_bssid_station(struct iwl_priv *priv,
938 struct iwl_rxon_context *ctx,
Johannes Berga30e3112010-09-22 18:02:01 +0200939 const u8 *addr, u8 *sta_id_r)
940{
941 int ret;
942 u8 sta_id;
943 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +0200944
945 if (sta_id_r)
946 *sta_id_r = IWL_INVALID_STATION;
947
948 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
949 if (ret) {
950 IWL_ERR(priv, "Unable to add station %pM\n", addr);
951 return ret;
952 }
953
954 if (sta_id_r)
955 *sta_id_r = sta_id;
956
Johannes Bergfa23cb02012-03-05 11:24:25 -0800957 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200958 priv->stations[sta_id].used |= IWL_STA_LOCAL;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800959 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200960
961 /* Set up default rate scaling table in device's station table */
Johannes Bergf775aa06d2011-06-22 06:34:09 -0700962 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200963 if (!link_cmd) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700964 IWL_ERR(priv,
965 "Unable to initialize rate scaling for station %pM.\n",
Johannes Berga30e3112010-09-22 18:02:01 +0200966 addr);
967 return -ENOMEM;
968 }
969
970 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
971 if (ret)
972 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
973
Johannes Bergfa23cb02012-03-05 11:24:25 -0800974 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200975 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800976 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200977
978 return 0;
979}
980
Johannes Berg5a3d9882011-07-15 13:03:12 -0700981/*
982 * static WEP keys
983 *
984 * For each context, the device has a table of 4 static WEP keys
985 * (one for each key index) that is updated with the following
986 * commands.
987 */
988
Johannes Berga30e3112010-09-22 18:02:01 +0200989static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
990 struct iwl_rxon_context *ctx,
991 bool send_if_empty)
992{
993 int i, not_empty = 0;
994 u8 buff[sizeof(struct iwl_wep_cmd) +
995 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
996 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
997 size_t cmd_size = sizeof(struct iwl_wep_cmd);
998 struct iwl_host_cmd cmd = {
999 .id = ctx->wep_key_cmd,
Johannes Berg3fa50732011-05-04 07:50:38 -07001000 .data = { wep_cmd, },
Johannes Berga30e3112010-09-22 18:02:01 +02001001 .flags = CMD_SYNC,
1002 };
1003
1004 might_sleep();
1005
1006 memset(wep_cmd, 0, cmd_size +
1007 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
1008
1009 for (i = 0; i < WEP_KEYS_MAX ; i++) {
1010 wep_cmd->key[i].key_index = i;
1011 if (ctx->wep_keys[i].key_size) {
1012 wep_cmd->key[i].key_offset = i;
1013 not_empty = 1;
1014 } else {
1015 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
1016 }
1017
1018 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
1019 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
1020 ctx->wep_keys[i].key_size);
1021 }
1022
1023 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
1024 wep_cmd->num_keys = WEP_KEYS_MAX;
1025
1026 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
1027
Johannes Berg3fa50732011-05-04 07:50:38 -07001028 cmd.len[0] = cmd_size;
Johannes Berga30e3112010-09-22 18:02:01 +02001029
1030 if (not_empty || send_if_empty)
Johannes Berge10a0532012-03-06 13:30:39 -08001031 return iwl_dvm_send_cmd(priv, &cmd);
Johannes Berga30e3112010-09-22 18:02:01 +02001032 else
1033 return 0;
1034}
1035
1036int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1037 struct iwl_rxon_context *ctx)
1038{
Johannes Bergb1eea292012-03-06 13:30:42 -08001039 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001040
1041 return iwl_send_static_wepkey_cmd(priv, ctx, false);
1042}
1043
1044int iwl_remove_default_wep_key(struct iwl_priv *priv,
1045 struct iwl_rxon_context *ctx,
1046 struct ieee80211_key_conf *keyconf)
1047{
1048 int ret;
1049
Johannes Bergb1eea292012-03-06 13:30:42 -08001050 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001051
1052 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1053 keyconf->keyidx);
1054
1055 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
Don Fry83626402012-03-07 09:52:37 -08001056 if (iwl_is_rfkill(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001057 IWL_DEBUG_WEP(priv,
1058 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berga30e3112010-09-22 18:02:01 +02001059 /* but keys in device are clear anyway so return success */
1060 return 0;
1061 }
1062 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1063 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1064 keyconf->keyidx, ret);
1065
1066 return ret;
1067}
1068
1069int iwl_set_default_wep_key(struct iwl_priv *priv,
1070 struct iwl_rxon_context *ctx,
1071 struct ieee80211_key_conf *keyconf)
1072{
1073 int ret;
1074
Johannes Bergb1eea292012-03-06 13:30:42 -08001075 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001076
1077 if (keyconf->keylen != WEP_KEY_LEN_128 &&
1078 keyconf->keylen != WEP_KEY_LEN_64) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001079 IWL_DEBUG_WEP(priv,
1080 "Bad WEP key length %d\n", keyconf->keylen);
Johannes Berga30e3112010-09-22 18:02:01 +02001081 return -EINVAL;
1082 }
1083
Johannes Berg5a3d9882011-07-15 13:03:12 -07001084 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
Johannes Berga30e3112010-09-22 18:02:01 +02001085
1086 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1087 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1088 keyconf->keylen);
1089
1090 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1091 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1092 keyconf->keylen, keyconf->keyidx, ret);
1093
1094 return ret;
1095}
1096
Johannes Berg5a3d9882011-07-15 13:03:12 -07001097/*
1098 * dynamic (per-station) keys
1099 *
1100 * The dynamic keys are a little more complicated. The device has
1101 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1102 * These are linked to stations by a table that contains an index
1103 * into the key table for each station/key index/{mcast,unicast},
1104 * i.e. it's basically an array of pointers like this:
1105 * key_offset_t key_mapping[NUM_STATIONS][4][2];
1106 * (it really works differently, but you can think of it as such)
1107 *
1108 * The key uploading and linking happens in the same command, the
1109 * add station command with STA_MODIFY_KEY_MASK.
1110 */
1111
1112static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1113 struct ieee80211_vif *vif,
1114 struct ieee80211_sta *sta)
1115{
1116 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001117
1118 if (sta)
Johannes Berg40e4e682012-03-05 11:24:22 -08001119 return iwl_sta_id(sta);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001120
1121 /*
1122 * The device expects GTKs for station interfaces to be
1123 * installed as GTKs for the AP station. If we have no
1124 * station ID, then use the ap_sta_id in that case.
1125 */
Johannes Berg40e4e682012-03-05 11:24:22 -08001126 if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1127 return vif_priv->ctx->ap_sta_id;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001128
Johannes Berg40e4e682012-03-05 11:24:22 -08001129 return IWL_INVALID_STATION;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001130}
1131
Johannes Berge1b1c082011-07-17 01:44:11 -07001132static int iwlagn_send_sta_key(struct iwl_priv *priv,
1133 struct ieee80211_key_conf *keyconf,
1134 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1135 u32 cmd_flags)
Johannes Berga30e3112010-09-22 18:02:01 +02001136{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001137 __le16 key_flags;
Johannes Berga30e3112010-09-22 18:02:01 +02001138 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001139 int i;
Johannes Berge1b1c082011-07-17 01:44:11 -07001140
Johannes Bergfa23cb02012-03-05 11:24:25 -08001141 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001142 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001143 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001144
Johannes Berg5a3d9882011-07-15 13:03:12 -07001145 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1146 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001147
Johannes Berg5a3d9882011-07-15 13:03:12 -07001148 switch (keyconf->cipher) {
1149 case WLAN_CIPHER_SUITE_CCMP:
1150 key_flags |= STA_KEY_FLG_CCMP;
1151 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1152 break;
1153 case WLAN_CIPHER_SUITE_TKIP:
1154 key_flags |= STA_KEY_FLG_TKIP;
1155 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1156 for (i = 0; i < 5; i++)
1157 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1158 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1159 break;
1160 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga30e3112010-09-22 18:02:01 +02001161 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001162 /* fall through */
1163 case WLAN_CIPHER_SUITE_WEP40:
1164 key_flags |= STA_KEY_FLG_WEP;
1165 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1166 break;
1167 default:
1168 WARN_ON(1);
1169 return -EINVAL;
1170 }
Johannes Berga30e3112010-09-22 18:02:01 +02001171
Johannes Berg5a3d9882011-07-15 13:03:12 -07001172 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
Johannes Berga30e3112010-09-22 18:02:01 +02001173 key_flags |= STA_KEY_MULTICAST_MSK;
1174
Johannes Berg5a3d9882011-07-15 13:03:12 -07001175 /* key pointer (offset) */
1176 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berga30e3112010-09-22 18:02:01 +02001177
Johannes Berg5a3d9882011-07-15 13:03:12 -07001178 sta_cmd.key.key_flags = key_flags;
1179 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1180 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
Johannes Berga30e3112010-09-22 18:02:01 +02001181
Johannes Berg5a3d9882011-07-15 13:03:12 -07001182 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001183}
1184
1185void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001186 struct ieee80211_vif *vif,
Johannes Berga30e3112010-09-22 18:02:01 +02001187 struct ieee80211_key_conf *keyconf,
1188 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1189{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001190 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1191
1192 if (sta_id == IWL_INVALID_STATION)
1193 return;
Johannes Berga30e3112010-09-22 18:02:01 +02001194
1195 if (iwl_scan_cancel(priv)) {
1196 /* cancel scan failed, just live w/ bad key and rely
1197 briefly on SW decryption */
1198 return;
1199 }
1200
Johannes Berge1b1c082011-07-17 01:44:11 -07001201 iwlagn_send_sta_key(priv, keyconf, sta_id,
1202 iv32, phase1key, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001203}
1204
1205int iwl_remove_dynamic_key(struct iwl_priv *priv,
1206 struct iwl_rxon_context *ctx,
1207 struct ieee80211_key_conf *keyconf,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001208 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001209{
Johannes Berga30e3112010-09-22 18:02:01 +02001210 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001211 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
Johannes Berg5dcbf482012-02-17 09:47:14 -08001212 __le16 key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001213
1214 /* if station isn't there, neither is the key */
1215 if (sta_id == IWL_INVALID_STATION)
1216 return -ENOENT;
1217
Johannes Bergfa23cb02012-03-05 11:24:25 -08001218 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001219 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1220 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1221 sta_id = IWL_INVALID_STATION;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001222 spin_unlock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001223
1224 if (sta_id == IWL_INVALID_STATION)
1225 return 0;
Johannes Berga30e3112010-09-22 18:02:01 +02001226
Johannes Bergb1eea292012-03-06 13:30:42 -08001227 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001228
1229 ctx->key_mapping_keys--;
1230
Johannes Berga30e3112010-09-22 18:02:01 +02001231 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1232 keyconf->keyidx, sta_id);
1233
Johannes Berg5a3d9882011-07-15 13:03:12 -07001234 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1235 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1236 keyconf->hw_key_idx);
Johannes Berga30e3112010-09-22 18:02:01 +02001237
Johannes Berg5dcbf482012-02-17 09:47:14 -08001238 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1239 key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1240 STA_KEY_FLG_INVALID;
1241
1242 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1243 key_flags |= STA_KEY_MULTICAST_MSK;
1244
1245 sta_cmd.key.key_flags = key_flags;
Emmanuel Grumbachd6ee27eb2012-06-06 09:13:36 +02001246 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001247 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1248 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001249
1250 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1251}
1252
Johannes Berg5a3d9882011-07-15 13:03:12 -07001253int iwl_set_dynamic_key(struct iwl_priv *priv,
1254 struct iwl_rxon_context *ctx,
1255 struct ieee80211_key_conf *keyconf,
1256 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001257{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001258 struct ieee80211_key_seq seq;
1259 u16 p1k[5];
Johannes Berga30e3112010-09-22 18:02:01 +02001260 int ret;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001261 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1262 const u8 *addr;
1263
1264 if (sta_id == IWL_INVALID_STATION)
1265 return -EINVAL;
Johannes Berga30e3112010-09-22 18:02:01 +02001266
Johannes Bergb1eea292012-03-06 13:30:42 -08001267 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001268
Johannes Berg5a3d9882011-07-15 13:03:12 -07001269 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1270 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1271 return -ENOSPC;
1272
Johannes Berga30e3112010-09-22 18:02:01 +02001273 ctx->key_mapping_keys++;
Johannes Berga30e3112010-09-22 18:02:01 +02001274
1275 switch (keyconf->cipher) {
Johannes Berga30e3112010-09-22 18:02:01 +02001276 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001277 if (sta)
1278 addr = sta->addr;
1279 else /* station mode case only */
1280 addr = ctx->active.bssid_addr;
1281
1282 /* pre-fill phase 1 key into device cache */
1283 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1284 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Berge1b1c082011-07-17 01:44:11 -07001285 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1286 seq.tkip.iv32, p1k, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001287 break;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001288 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berga30e3112010-09-22 18:02:01 +02001289 case WLAN_CIPHER_SUITE_WEP40:
1290 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge1b1c082011-07-17 01:44:11 -07001291 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1292 0, NULL, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001293 break;
1294 default:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001295 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
Johannes Berga30e3112010-09-22 18:02:01 +02001296 ret = -EINVAL;
1297 }
1298
Johannes Berg5a3d9882011-07-15 13:03:12 -07001299 if (ret) {
1300 ctx->key_mapping_keys--;
1301 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1302 }
1303
1304 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 +02001305 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001306 sta ? sta->addr : NULL, ret);
Johannes Berga30e3112010-09-22 18:02:01 +02001307
1308 return ret;
1309}
1310
1311/**
1312 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1313 *
1314 * This adds the broadcast station into the driver's station table
1315 * and marks it driver active, so that it will be restored to the
1316 * device at the next best time.
1317 */
1318int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1319 struct iwl_rxon_context *ctx)
1320{
1321 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +02001322 u8 sta_id;
1323
Johannes Bergfa23cb02012-03-05 11:24:25 -08001324 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001325 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1326 if (sta_id == IWL_INVALID_STATION) {
1327 IWL_ERR(priv, "Unable to prepare broadcast station\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -08001328 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001329
1330 return -EINVAL;
1331 }
1332
1333 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1334 priv->stations[sta_id].used |= IWL_STA_BCAST;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001335 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001336
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001337 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001338 if (!link_cmd) {
1339 IWL_ERR(priv,
1340 "Unable to initialize rate scaling for bcast station.\n");
1341 return -ENOMEM;
1342 }
1343
Johannes Bergfa23cb02012-03-05 11:24:25 -08001344 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001345 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001346 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001347
1348 return 0;
1349}
1350
1351/**
1352 * iwl_update_bcast_station - update broadcast station's LQ command
1353 *
1354 * Only used by iwlagn. Placed here to have all bcast station management
1355 * code together.
1356 */
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001357int iwl_update_bcast_station(struct iwl_priv *priv,
1358 struct iwl_rxon_context *ctx)
Johannes Berga30e3112010-09-22 18:02:01 +02001359{
Johannes Berga30e3112010-09-22 18:02:01 +02001360 struct iwl_link_quality_cmd *link_cmd;
1361 u8 sta_id = ctx->bcast_sta_id;
1362
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001363 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001364 if (!link_cmd) {
1365 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1366 return -ENOMEM;
1367 }
1368
Johannes Bergfa23cb02012-03-05 11:24:25 -08001369 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001370 if (priv->stations[sta_id].lq)
1371 kfree(priv->stations[sta_id].lq);
1372 else
1373 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1374 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001375 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001376
1377 return 0;
1378}
1379
1380int iwl_update_bcast_stations(struct iwl_priv *priv)
1381{
1382 struct iwl_rxon_context *ctx;
1383 int ret = 0;
1384
1385 for_each_context(priv, ctx) {
1386 ret = iwl_update_bcast_station(priv, ctx);
1387 if (ret)
1388 break;
1389 }
1390
1391 return ret;
1392}
1393
1394/**
1395 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1396 */
1397int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1398{
Johannes Berga30e3112010-09-22 18:02:01 +02001399 struct iwl_addsta_cmd sta_cmd;
1400
Johannes Bergb1eea292012-03-06 13:30:42 -08001401 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001402
1403 /* Remove "disable" flag, to enable Tx for this TID */
Johannes Bergfa23cb02012-03-05 11:24:25 -08001404 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001405 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1406 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1407 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1408 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001409 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001410
1411 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1412}
1413
1414int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1415 int tid, u16 ssn)
1416{
Johannes Berga30e3112010-09-22 18:02:01 +02001417 int sta_id;
1418 struct iwl_addsta_cmd sta_cmd;
1419
Johannes Bergb1eea292012-03-06 13:30:42 -08001420 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001421
1422 sta_id = iwl_sta_id(sta);
1423 if (sta_id == IWL_INVALID_STATION)
1424 return -ENXIO;
1425
Johannes Bergfa23cb02012-03-05 11:24:25 -08001426 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001427 priv->stations[sta_id].sta.station_flags_msk = 0;
1428 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1429 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1430 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1431 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1432 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001433 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001434
1435 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1436}
1437
1438int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1439 int tid)
1440{
Johannes Berga30e3112010-09-22 18:02:01 +02001441 int sta_id;
1442 struct iwl_addsta_cmd sta_cmd;
1443
Johannes Bergb1eea292012-03-06 13:30:42 -08001444 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001445
1446 sta_id = iwl_sta_id(sta);
1447 if (sta_id == IWL_INVALID_STATION) {
1448 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1449 return -ENXIO;
1450 }
1451
Johannes Bergfa23cb02012-03-05 11:24:25 -08001452 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001453 priv->stations[sta_id].sta.station_flags_msk = 0;
1454 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1455 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1456 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1457 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001458 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001459
1460 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1461}
1462
Johannes Berga30e3112010-09-22 18:02:01 +02001463
Johannes Berga30e3112010-09-22 18:02:01 +02001464
1465void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1466{
Johannes Berg9451ca12012-03-05 11:24:23 -08001467 struct iwl_addsta_cmd cmd = {
1468 .mode = STA_CONTROL_MODIFY_MSK,
1469 .station_flags = STA_FLG_PWR_SAVE_MSK,
1470 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1471 .sta.sta_id = sta_id,
1472 .sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1473 .sleep_tx_count = cpu_to_le16(cnt),
1474 };
Johannes Berga30e3112010-09-22 18:02:01 +02001475
Johannes Berg9451ca12012-03-05 11:24:23 -08001476 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001477}