blob: 14fbdecccd268276b2763d61470ab51834dd087b [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 *****************************************************************************/
29
30#include <net/mac80211.h>
31
32#include "iwl-dev.h"
33#include "iwl-core.h"
Johannes Berga30e3112010-09-22 18:02:01 +020034#include "iwl-agn.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070035#include "iwl-trans.h"
Johannes Berga30e3112010-09-22 18:02:01 +020036
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{
67 u8 sta_id = addsta->sta.sta_id;
Wey-Yi Guyc745f552011-10-10 07:27:12 -070068 int ret = -EIO;
69
70 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
71 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
72 pkt->hdr.flags);
73 return ret;
74 }
75
76 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
77 sta_id);
78
Johannes Bergfa23cb02012-03-05 11:24:25 -080079 spin_lock(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -070080
81 switch (pkt->u.add_sta.status) {
82 case ADD_STA_SUCCESS_MSK:
83 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
Wey-Yi Guy2da424b2012-01-06 13:16:28 -080084 ret = iwl_sta_ucode_activate(priv, sta_id);
Wey-Yi Guyc745f552011-10-10 07:27:12 -070085 break;
86 case ADD_STA_NO_ROOM_IN_TABLE:
87 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
88 sta_id);
89 break;
90 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
91 IWL_ERR(priv, "Adding station %d failed, no block ack "
92 "resource.\n", sta_id);
93 break;
94 case ADD_STA_MODIFY_NON_EXIST_STA:
95 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
96 sta_id);
97 break;
98 default:
99 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
100 pkt->u.add_sta.status);
101 break;
102 }
103
104 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
105 priv->stations[sta_id].sta.mode ==
106 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
107 sta_id, priv->stations[sta_id].sta.sta.addr);
108
109 /*
110 * XXX: The MAC address in the command buffer is often changed from
111 * the original sent to the device. That is, the MAC address
112 * written to the command buffer often is not the same MAC address
113 * read from the command buffer when the command returns. This
114 * issue has not yet been resolved and this debugging is left to
115 * observe the problem.
116 */
117 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
118 priv->stations[sta_id].sta.mode ==
119 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
120 addsta->sta.addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800121 spin_unlock(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700122
123 return ret;
124}
125
Johannes Berg48a2d662012-03-05 11:24:39 -0800126int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700127 struct iwl_device_cmd *cmd)
128{
129 struct iwl_rx_packet *pkt = rxb_addr(rxb);
130 struct iwl_addsta_cmd *addsta =
131 (struct iwl_addsta_cmd *) cmd->payload;
132
133 return iwl_process_add_sta_resp(priv, addsta, pkt);
134}
135
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700136int iwl_send_add_sta(struct iwl_priv *priv,
137 struct iwl_addsta_cmd *sta, u8 flags)
138{
139 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700140 struct iwl_host_cmd cmd = {
141 .id = REPLY_ADD_STA,
142 .flags = flags,
Johannes Berg69b172f2011-12-12 04:17:44 -0800143 .data = { sta, },
144 .len = { sizeof(*sta), },
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700145 };
146 u8 sta_id __maybe_unused = sta->sta.sta_id;
147
148 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
149 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
150
151 if (!(flags & CMD_ASYNC)) {
152 cmd.flags |= CMD_WANT_SKB;
153 might_sleep();
154 }
155
Johannes Berge10a0532012-03-06 13:30:39 -0800156 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700157
158 if (ret || (flags & CMD_ASYNC))
159 return ret;
160 /*else the command was successfully sent in SYNC mode, need to free
161 * the reply page */
162
Johannes Berg65b94a42012-03-05 11:24:38 -0800163 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700164
165 if (cmd.handler_status)
166 IWL_ERR(priv, "%s - error in the CMD response %d", __func__,
167 cmd.handler_status);
168
169 return cmd.handler_status;
170}
171
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800172static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
173 struct ieee80211_sta *sta,
174 struct iwl_rxon_context *ctx,
175 __le32 *flags, __le32 *mask)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700176{
177 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700178 u8 mimo_ps_mode;
179
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800180 *mask = STA_FLG_RTS_MIMO_PROT_MSK |
181 STA_FLG_MIMO_DIS_MSK |
182 STA_FLG_HT40_EN_MSK |
183 STA_FLG_MAX_AGG_SIZE_MSK |
184 STA_FLG_AGG_MPDU_DENSITY_MSK;
185 *flags = 0;
186
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700187 if (!sta || !sta_ht_inf->ht_supported)
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800188 return;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700189
190 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800191
192 IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700193 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
194 "static" :
195 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
196 "dynamic" : "disabled");
197
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700198 switch (mimo_ps_mode) {
199 case WLAN_HT_CAP_SM_PS_STATIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800200 *flags |= STA_FLG_MIMO_DIS_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700201 break;
202 case WLAN_HT_CAP_SM_PS_DYNAMIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800203 *flags |= STA_FLG_RTS_MIMO_PROT_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700204 break;
205 case WLAN_HT_CAP_SM_PS_DISABLED:
206 break;
207 default:
208 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
209 break;
210 }
211
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800212 *flags |= cpu_to_le32(
213 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700214
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800215 *flags |= cpu_to_le32(
216 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700217
218 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800219 *flags |= STA_FLG_HT40_EN_MSK;
220}
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700221
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800222int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
223 struct ieee80211_sta *sta)
224{
225 u8 sta_id = iwl_sta_id(sta);
226 __le32 flags, mask;
227 struct iwl_addsta_cmd cmd;
228
229 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
230 return -EINVAL;
231
232 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
233
234 spin_lock_bh(&priv->sta_lock);
235 priv->stations[sta_id].sta.station_flags &= ~mask;
236 priv->stations[sta_id].sta.station_flags |= flags;
237 spin_unlock_bh(&priv->sta_lock);
238
239 memset(&cmd, 0, sizeof(cmd));
240 cmd.mode = STA_CONTROL_MODIFY_MSK;
241 cmd.station_flags_msk = mask;
242 cmd.station_flags = flags;
243 cmd.sta.sta_id = sta_id;
244
245 return iwl_send_add_sta(priv, &cmd, CMD_SYNC);
246}
247
248static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
249 struct ieee80211_sta *sta,
250 struct iwl_rxon_context *ctx)
251{
252 __le32 flags, mask;
253
254 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
255
256 lockdep_assert_held(&priv->sta_lock);
257 priv->stations[index].sta.station_flags &= ~mask;
258 priv->stations[index].sta.station_flags |= flags;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700259}
260
261/**
262 * iwl_prep_station - Prepare station information for addition
263 *
264 * should be called with sta_lock held
265 */
266u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
267 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
268{
269 struct iwl_station_entry *station;
270 int i;
271 u8 sta_id = IWL_INVALID_STATION;
272
273 if (is_ap)
274 sta_id = ctx->ap_sta_id;
275 else if (is_broadcast_ether_addr(addr))
276 sta_id = ctx->bcast_sta_id;
277 else
278 for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
279 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
280 addr)) {
281 sta_id = i;
282 break;
283 }
284
285 if (!priv->stations[i].used &&
286 sta_id == IWL_INVALID_STATION)
287 sta_id = i;
288 }
289
290 /*
291 * These two conditions have the same outcome, but keep them
292 * separate
293 */
294 if (unlikely(sta_id == IWL_INVALID_STATION))
295 return sta_id;
296
297 /*
298 * uCode is not able to deal with multiple requests to add a
299 * station. Keep track if one is in progress so that we do not send
300 * another.
301 */
302 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
303 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
304 "added.\n", sta_id);
305 return sta_id;
306 }
307
308 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
309 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
310 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
311 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
312 "adding again.\n", sta_id, addr);
313 return sta_id;
314 }
315
316 station = &priv->stations[sta_id];
317 station->used = IWL_STA_DRIVER_ACTIVE;
318 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
319 sta_id, addr);
320 priv->num_stations++;
321
322 /* Set up the REPLY_ADD_STA command to send to device */
323 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
324 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
325 station->sta.mode = 0;
326 station->sta.sta.sta_id = sta_id;
327 station->sta.station_flags = ctx->station_flags;
328 station->ctxid = ctx->ctxid;
329
330 if (sta) {
331 struct iwl_station_priv *sta_priv;
332
333 sta_priv = (void *)sta->drv_priv;
334 sta_priv->ctx = ctx;
335 }
336
337 /*
338 * OK to call unconditionally, since local stations (IBSS BSSID
339 * STA and broadcast STA) pass in a NULL sta, and mac80211
340 * doesn't allow HT IBSS.
341 */
342 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
343
344 return sta_id;
345
346}
347
348#define STA_WAIT_TIMEOUT (HZ/2)
349
350/**
351 * iwl_add_station_common -
352 */
353int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
354 const u8 *addr, bool is_ap,
355 struct ieee80211_sta *sta, u8 *sta_id_r)
356{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700357 int ret = 0;
358 u8 sta_id;
359 struct iwl_addsta_cmd sta_cmd;
360
361 *sta_id_r = 0;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800362 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700363 sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
364 if (sta_id == IWL_INVALID_STATION) {
365 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
366 addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800367 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700368 return -EINVAL;
369 }
370
371 /*
372 * uCode is not able to deal with multiple requests to add a
373 * station. Keep track if one is in progress so that we do not send
374 * another.
375 */
376 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
377 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
378 "added.\n", sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800379 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700380 return -EEXIST;
381 }
382
383 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
384 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
385 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
386 "adding again.\n", sta_id, addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800387 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700388 return -EEXIST;
389 }
390
391 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
392 memcpy(&sta_cmd, &priv->stations[sta_id].sta,
393 sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -0800394 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700395
396 /* Add station to device's station table */
397 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
398 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800399 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700400 IWL_ERR(priv, "Adding station %pM failed.\n",
401 priv->stations[sta_id].sta.sta.addr);
402 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
403 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800404 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700405 }
406 *sta_id_r = sta_id;
407 return ret;
408}
409
410/**
411 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700412 */
413static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
414{
Johannes Bergfa23cb02012-03-05 11:24:25 -0800415 lockdep_assert_held(&priv->sta_lock);
416
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700417 /* Ucode must be active and driver must be non active */
418 if ((priv->stations[sta_id].used &
419 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
420 IWL_STA_UCODE_ACTIVE)
421 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
422
423 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
424
425 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
426 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
427}
428
429static int iwl_send_remove_station(struct iwl_priv *priv,
430 const u8 *addr, int sta_id,
431 bool temporary)
432{
433 struct iwl_rx_packet *pkt;
434 int ret;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700435 struct iwl_rem_sta_cmd rm_sta_cmd;
436
437 struct iwl_host_cmd cmd = {
438 .id = REPLY_REMOVE_STA,
439 .len = { sizeof(struct iwl_rem_sta_cmd), },
440 .flags = CMD_SYNC,
441 .data = { &rm_sta_cmd, },
442 };
443
444 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
445 rm_sta_cmd.num_sta = 1;
446 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
447
448 cmd.flags |= CMD_WANT_SKB;
449
Johannes Berge10a0532012-03-06 13:30:39 -0800450 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700451
452 if (ret)
453 return ret;
454
Johannes Berg65b94a42012-03-05 11:24:38 -0800455 pkt = cmd.resp_pkt;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700456 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
457 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
458 pkt->hdr.flags);
459 ret = -EIO;
460 }
461
462 if (!ret) {
463 switch (pkt->u.rem_sta.status) {
464 case REM_STA_SUCCESS_MSK:
465 if (!temporary) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800466 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700467 iwl_sta_ucode_deactivate(priv, sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800468 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700469 }
470 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
471 break;
472 default:
473 ret = -EIO;
474 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
475 break;
476 }
477 }
Johannes Berg65b94a42012-03-05 11:24:38 -0800478 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700479
480 return ret;
481}
482
483/**
484 * iwl_remove_station - Remove driver's knowledge of station.
485 */
486int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
487 const u8 *addr)
488{
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200489 u8 tid;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700490
491 if (!iwl_is_ready(priv->shrd)) {
492 IWL_DEBUG_INFO(priv,
493 "Unable to remove station %pM, device not ready.\n",
494 addr);
495 /*
496 * It is typical for stations to be removed when we are
497 * going down. Return success since device will be down
498 * soon anyway
499 */
500 return 0;
501 }
502
503 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
504 sta_id, addr);
505
506 if (WARN_ON(sta_id == IWL_INVALID_STATION))
507 return -EINVAL;
508
Johannes Bergfa23cb02012-03-05 11:24:25 -0800509 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700510
511 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
512 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
513 addr);
514 goto out_err;
515 }
516
517 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
518 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
519 addr);
520 goto out_err;
521 }
522
523 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
524 kfree(priv->stations[sta_id].lq);
525 priv->stations[sta_id].lq = NULL;
526 }
527
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200528 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
529 memset(&priv->tid_data[sta_id][tid], 0,
530 sizeof(priv->tid_data[sta_id][tid]));
531
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700532 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
533
534 priv->num_stations--;
535
536 if (WARN_ON(priv->num_stations < 0))
537 priv->num_stations = 0;
538
Johannes Bergfa23cb02012-03-05 11:24:25 -0800539 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700540
541 return iwl_send_remove_station(priv, addr, sta_id, false);
542out_err:
Johannes Bergfa23cb02012-03-05 11:24:25 -0800543 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700544 return -EINVAL;
545}
546
547/**
548 * iwl_clear_ucode_stations - clear ucode station table bits
549 *
550 * This function clears all the bits in the driver indicating
551 * which stations are active in the ucode. Call when something
552 * other than explicit station management would cause this in
553 * the ucode, e.g. unassociated RXON.
554 */
555void iwl_clear_ucode_stations(struct iwl_priv *priv,
556 struct iwl_rxon_context *ctx)
557{
558 int i;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700559 bool cleared = false;
560
561 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
562
Johannes Bergfa23cb02012-03-05 11:24:25 -0800563 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700564 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
565 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
566 continue;
567
568 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
569 IWL_DEBUG_INFO(priv,
570 "Clearing ucode active for station %d\n", i);
571 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
572 cleared = true;
573 }
574 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800575 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700576
577 if (!cleared)
578 IWL_DEBUG_INFO(priv,
579 "No active stations found to be cleared\n");
580}
581
582/**
583 * iwl_restore_stations() - Restore driver known stations to device
584 *
585 * All stations considered active by driver, but not present in ucode, is
586 * restored.
587 *
588 * Function sleeps.
589 */
590void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
591{
592 struct iwl_addsta_cmd sta_cmd;
593 struct iwl_link_quality_cmd lq;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700594 int i;
595 bool found = false;
596 int ret;
597 bool send_lq;
598
599 if (!iwl_is_ready(priv->shrd)) {
600 IWL_DEBUG_INFO(priv,
601 "Not ready yet, not restoring any stations.\n");
602 return;
603 }
604
605 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -0800606 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700607 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
608 if (ctx->ctxid != priv->stations[i].ctxid)
609 continue;
610 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
611 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
612 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
613 priv->stations[i].sta.sta.addr);
614 priv->stations[i].sta.mode = 0;
615 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
616 found = true;
617 }
618 }
619
620 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
621 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
622 memcpy(&sta_cmd, &priv->stations[i].sta,
623 sizeof(struct iwl_addsta_cmd));
624 send_lq = false;
625 if (priv->stations[i].lq) {
Johannes Berg15b86bf2012-03-05 11:24:36 -0800626 if (priv->wowlan)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700627 iwl_sta_fill_lq(priv, ctx, i, &lq);
628 else
629 memcpy(&lq, priv->stations[i].lq,
630 sizeof(struct iwl_link_quality_cmd));
631 send_lq = true;
632 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800633 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700634 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
635 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800636 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700637 IWL_ERR(priv, "Adding station %pM failed.\n",
638 priv->stations[i].sta.sta.addr);
639 priv->stations[i].used &=
640 ~IWL_STA_DRIVER_ACTIVE;
641 priv->stations[i].used &=
642 ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800643 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700644 }
645 /*
646 * Rate scaling has already been initialized, send
647 * current LQ command
648 */
649 if (send_lq)
650 iwl_send_lq_cmd(priv, ctx, &lq,
651 CMD_SYNC, true);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800652 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700653 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
654 }
655 }
656
Johannes Bergfa23cb02012-03-05 11:24:25 -0800657 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700658 if (!found)
659 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
660 "no stations to be restored.\n");
661 else
662 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
663 "complete.\n");
664}
665
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700666int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
667{
668 int i;
669
670 for (i = 0; i < priv->sta_key_max_num; i++)
671 if (!test_and_set_bit(i, &priv->ucode_key_table))
672 return i;
673
674 return WEP_INVALID_OFFSET;
675}
676
677void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
678{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700679 int i;
680
Johannes Bergfa23cb02012-03-05 11:24:25 -0800681 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700682 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
683 if (!(priv->stations[i].used & IWL_STA_BCAST))
684 continue;
685
686 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
687 priv->num_stations--;
688 if (WARN_ON(priv->num_stations < 0))
689 priv->num_stations = 0;
690 kfree(priv->stations[i].lq);
691 priv->stations[i].lq = NULL;
692 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800693 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700694}
695
696#ifdef CONFIG_IWLWIFI_DEBUG
697static void iwl_dump_lq_cmd(struct iwl_priv *priv,
698 struct iwl_link_quality_cmd *lq)
699{
700 int i;
701 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
702 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
703 lq->general_params.single_stream_ant_msk,
704 lq->general_params.dual_stream_ant_msk);
705
706 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
707 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
708 i, lq->rs_table[i].rate_n_flags);
709}
710#else
711static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
712 struct iwl_link_quality_cmd *lq)
713{
714}
715#endif
716
717/**
718 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
719 *
720 * It sometimes happens when a HT rate has been in use and we
721 * loose connectivity with AP then mac80211 will first tell us that the
722 * current channel is not HT anymore before removing the station. In such a
723 * scenario the RXON flags will be updated to indicate we are not
724 * communicating HT anymore, but the LQ command may still contain HT rates.
725 * Test for this to prevent driver from sending LQ command between the time
726 * RXON flags are updated and when LQ command is updated.
727 */
728static bool is_lq_table_valid(struct iwl_priv *priv,
729 struct iwl_rxon_context *ctx,
730 struct iwl_link_quality_cmd *lq)
731{
732 int i;
733
734 if (ctx->ht.enabled)
735 return true;
736
737 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
738 ctx->active.channel);
739 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
740 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
741 RATE_MCS_HT_MSK) {
742 IWL_DEBUG_INFO(priv,
743 "index %d of LQ expects HT channel\n",
744 i);
745 return false;
746 }
747 }
748 return true;
749}
750
751/**
752 * iwl_send_lq_cmd() - Send link quality command
753 * @init: This command is sent as part of station initialization right
754 * after station has been added.
755 *
756 * The link quality command is sent as the last step of station creation.
757 * This is the special case in which init is set and we call a callback in
758 * this case to clear the state indicating that station creation is in
759 * progress.
760 */
761int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
762 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
763{
764 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700765 struct iwl_host_cmd cmd = {
766 .id = REPLY_TX_LINK_QUALITY_CMD,
767 .len = { sizeof(struct iwl_link_quality_cmd), },
768 .flags = flags,
769 .data = { lq, },
770 };
771
772 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
773 return -EINVAL;
774
775
Johannes Bergfa23cb02012-03-05 11:24:25 -0800776 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700777 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800778 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700779 return -EINVAL;
780 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800781 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700782
783 iwl_dump_lq_cmd(priv, lq);
784 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
785 return -EINVAL;
786
787 if (is_lq_table_valid(priv, ctx, lq))
Johannes Berge10a0532012-03-06 13:30:39 -0800788 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700789 else
790 ret = -EINVAL;
791
792 if (cmd.flags & CMD_ASYNC)
793 return ret;
794
795 if (init) {
796 IWL_DEBUG_INFO(priv, "init LQ command complete, "
797 "clearing sta addition status for sta %d\n",
798 lq->sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800799 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700800 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800801 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700802 }
803 return ret;
804}
805
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700806
Johannes Bergc079166e2011-10-10 07:26:54 -0700807void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
808 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
Johannes Berga30e3112010-09-22 18:02:01 +0200809{
810 int i, r;
Johannes Berga30e3112010-09-22 18:02:01 +0200811 u32 rate_flags = 0;
812 __le32 rate_n_flags;
813
Johannes Bergb1eea292012-03-06 13:30:42 -0800814 lockdep_assert_held(&priv->mutex);
Johannes Bergf775aa06d2011-06-22 06:34:09 -0700815
Johannes Bergc079166e2011-10-10 07:26:54 -0700816 memset(link_cmd, 0, sizeof(*link_cmd));
817
Johannes Berga30e3112010-09-22 18:02:01 +0200818 /* Set up the rate scaling to start at selected rate, fall back
819 * all the way down to 1M in IEEE order, and then spin on 1M */
820 if (priv->band == IEEE80211_BAND_5GHZ)
821 r = IWL_RATE_6M_INDEX;
Johannes Bergf775aa06d2011-06-22 06:34:09 -0700822 else if (ctx && ctx->vif && ctx->vif->p2p)
823 r = IWL_RATE_6M_INDEX;
Johannes Berga30e3112010-09-22 18:02:01 +0200824 else
825 r = IWL_RATE_1M_INDEX;
826
827 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
828 rate_flags |= RATE_MCS_CCK_MSK;
829
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700830 rate_flags |= first_antenna(hw_params(priv).valid_tx_ant) <<
Johannes Berga30e3112010-09-22 18:02:01 +0200831 RATE_MCS_ANT_POS;
832 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
833 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
834 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
835
836 link_cmd->general_params.single_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700837 first_antenna(hw_params(priv).valid_tx_ant);
Johannes Berga30e3112010-09-22 18:02:01 +0200838
839 link_cmd->general_params.dual_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700840 hw_params(priv).valid_tx_ant &
841 ~first_antenna(hw_params(priv).valid_tx_ant);
Johannes Berga30e3112010-09-22 18:02:01 +0200842 if (!link_cmd->general_params.dual_stream_ant_msk) {
843 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700844 } else if (num_of_ant(hw_params(priv).valid_tx_ant) == 2) {
Johannes Berga30e3112010-09-22 18:02:01 +0200845 link_cmd->general_params.dual_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700846 hw_params(priv).valid_tx_ant;
Johannes Berga30e3112010-09-22 18:02:01 +0200847 }
848
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700849 link_cmd->agg_params.agg_dis_start_th =
850 LINK_QUAL_AGG_DISABLE_START_DEF;
Johannes Berga30e3112010-09-22 18:02:01 +0200851 link_cmd->agg_params.agg_time_limit =
852 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
853
854 link_cmd->sta_id = sta_id;
Johannes Bergc079166e2011-10-10 07:26:54 -0700855}
856
857static struct iwl_link_quality_cmd *
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700858iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
859 u8 sta_id)
Johannes Bergc079166e2011-10-10 07:26:54 -0700860{
861 struct iwl_link_quality_cmd *link_cmd;
862
863 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
864 if (!link_cmd) {
865 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
866 return NULL;
867 }
868
869 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200870
871 return link_cmd;
872}
873
874/*
875 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
876 *
877 * Function sleeps.
878 */
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700879int iwlagn_add_bssid_station(struct iwl_priv *priv,
880 struct iwl_rxon_context *ctx,
Johannes Berga30e3112010-09-22 18:02:01 +0200881 const u8 *addr, u8 *sta_id_r)
882{
883 int ret;
884 u8 sta_id;
885 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +0200886
887 if (sta_id_r)
888 *sta_id_r = IWL_INVALID_STATION;
889
890 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
891 if (ret) {
892 IWL_ERR(priv, "Unable to add station %pM\n", addr);
893 return ret;
894 }
895
896 if (sta_id_r)
897 *sta_id_r = sta_id;
898
Johannes Bergfa23cb02012-03-05 11:24:25 -0800899 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200900 priv->stations[sta_id].used |= IWL_STA_LOCAL;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800901 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200902
903 /* Set up default rate scaling table in device's station table */
Johannes Bergf775aa06d2011-06-22 06:34:09 -0700904 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200905 if (!link_cmd) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700906 IWL_ERR(priv,
907 "Unable to initialize rate scaling for station %pM.\n",
Johannes Berga30e3112010-09-22 18:02:01 +0200908 addr);
909 return -ENOMEM;
910 }
911
912 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
913 if (ret)
914 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
915
Johannes Bergfa23cb02012-03-05 11:24:25 -0800916 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200917 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800918 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200919
920 return 0;
921}
922
Johannes Berg5a3d9882011-07-15 13:03:12 -0700923/*
924 * static WEP keys
925 *
926 * For each context, the device has a table of 4 static WEP keys
927 * (one for each key index) that is updated with the following
928 * commands.
929 */
930
Johannes Berga30e3112010-09-22 18:02:01 +0200931static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
932 struct iwl_rxon_context *ctx,
933 bool send_if_empty)
934{
935 int i, not_empty = 0;
936 u8 buff[sizeof(struct iwl_wep_cmd) +
937 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
938 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
939 size_t cmd_size = sizeof(struct iwl_wep_cmd);
940 struct iwl_host_cmd cmd = {
941 .id = ctx->wep_key_cmd,
Johannes Berg3fa50732011-05-04 07:50:38 -0700942 .data = { wep_cmd, },
Johannes Berga30e3112010-09-22 18:02:01 +0200943 .flags = CMD_SYNC,
944 };
945
946 might_sleep();
947
948 memset(wep_cmd, 0, cmd_size +
949 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
950
951 for (i = 0; i < WEP_KEYS_MAX ; i++) {
952 wep_cmd->key[i].key_index = i;
953 if (ctx->wep_keys[i].key_size) {
954 wep_cmd->key[i].key_offset = i;
955 not_empty = 1;
956 } else {
957 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
958 }
959
960 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
961 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
962 ctx->wep_keys[i].key_size);
963 }
964
965 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
966 wep_cmd->num_keys = WEP_KEYS_MAX;
967
968 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
969
Johannes Berg3fa50732011-05-04 07:50:38 -0700970 cmd.len[0] = cmd_size;
Johannes Berga30e3112010-09-22 18:02:01 +0200971
972 if (not_empty || send_if_empty)
Johannes Berge10a0532012-03-06 13:30:39 -0800973 return iwl_dvm_send_cmd(priv, &cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200974 else
975 return 0;
976}
977
978int iwl_restore_default_wep_keys(struct iwl_priv *priv,
979 struct iwl_rxon_context *ctx)
980{
Johannes Bergb1eea292012-03-06 13:30:42 -0800981 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +0200982
983 return iwl_send_static_wepkey_cmd(priv, ctx, false);
984}
985
986int iwl_remove_default_wep_key(struct iwl_priv *priv,
987 struct iwl_rxon_context *ctx,
988 struct ieee80211_key_conf *keyconf)
989{
990 int ret;
991
Johannes Bergb1eea292012-03-06 13:30:42 -0800992 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +0200993
994 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
995 keyconf->keyidx);
996
997 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
Emmanuel Grumbach845a9c02011-08-25 23:11:04 -0700998 if (iwl_is_rfkill(priv->shrd)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700999 IWL_DEBUG_WEP(priv,
1000 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berga30e3112010-09-22 18:02:01 +02001001 /* but keys in device are clear anyway so return success */
1002 return 0;
1003 }
1004 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1005 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1006 keyconf->keyidx, ret);
1007
1008 return ret;
1009}
1010
1011int iwl_set_default_wep_key(struct iwl_priv *priv,
1012 struct iwl_rxon_context *ctx,
1013 struct ieee80211_key_conf *keyconf)
1014{
1015 int ret;
1016
Johannes Bergb1eea292012-03-06 13:30:42 -08001017 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001018
1019 if (keyconf->keylen != WEP_KEY_LEN_128 &&
1020 keyconf->keylen != WEP_KEY_LEN_64) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001021 IWL_DEBUG_WEP(priv,
1022 "Bad WEP key length %d\n", keyconf->keylen);
Johannes Berga30e3112010-09-22 18:02:01 +02001023 return -EINVAL;
1024 }
1025
Johannes Berg5a3d9882011-07-15 13:03:12 -07001026 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
Johannes Berga30e3112010-09-22 18:02:01 +02001027
1028 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1029 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1030 keyconf->keylen);
1031
1032 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1033 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1034 keyconf->keylen, keyconf->keyidx, ret);
1035
1036 return ret;
1037}
1038
Johannes Berg5a3d9882011-07-15 13:03:12 -07001039/*
1040 * dynamic (per-station) keys
1041 *
1042 * The dynamic keys are a little more complicated. The device has
1043 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1044 * These are linked to stations by a table that contains an index
1045 * into the key table for each station/key index/{mcast,unicast},
1046 * i.e. it's basically an array of pointers like this:
1047 * key_offset_t key_mapping[NUM_STATIONS][4][2];
1048 * (it really works differently, but you can think of it as such)
1049 *
1050 * The key uploading and linking happens in the same command, the
1051 * add station command with STA_MODIFY_KEY_MASK.
1052 */
1053
1054static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1055 struct ieee80211_vif *vif,
1056 struct ieee80211_sta *sta)
1057{
1058 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001059
1060 if (sta)
Johannes Berg40e4e682012-03-05 11:24:22 -08001061 return iwl_sta_id(sta);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001062
1063 /*
1064 * The device expects GTKs for station interfaces to be
1065 * installed as GTKs for the AP station. If we have no
1066 * station ID, then use the ap_sta_id in that case.
1067 */
Johannes Berg40e4e682012-03-05 11:24:22 -08001068 if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1069 return vif_priv->ctx->ap_sta_id;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001070
Johannes Berg40e4e682012-03-05 11:24:22 -08001071 return IWL_INVALID_STATION;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001072}
1073
Johannes Berge1b1c082011-07-17 01:44:11 -07001074static int iwlagn_send_sta_key(struct iwl_priv *priv,
1075 struct ieee80211_key_conf *keyconf,
1076 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1077 u32 cmd_flags)
Johannes Berga30e3112010-09-22 18:02:01 +02001078{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001079 __le16 key_flags;
Johannes Berga30e3112010-09-22 18:02:01 +02001080 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001081 int i;
Johannes Berge1b1c082011-07-17 01:44:11 -07001082
Johannes Bergfa23cb02012-03-05 11:24:25 -08001083 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001084 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001085 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001086
Johannes Berg5a3d9882011-07-15 13:03:12 -07001087 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1088 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001089
Johannes Berg5a3d9882011-07-15 13:03:12 -07001090 switch (keyconf->cipher) {
1091 case WLAN_CIPHER_SUITE_CCMP:
1092 key_flags |= STA_KEY_FLG_CCMP;
1093 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1094 break;
1095 case WLAN_CIPHER_SUITE_TKIP:
1096 key_flags |= STA_KEY_FLG_TKIP;
1097 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1098 for (i = 0; i < 5; i++)
1099 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1100 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1101 break;
1102 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga30e3112010-09-22 18:02:01 +02001103 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001104 /* fall through */
1105 case WLAN_CIPHER_SUITE_WEP40:
1106 key_flags |= STA_KEY_FLG_WEP;
1107 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1108 break;
1109 default:
1110 WARN_ON(1);
1111 return -EINVAL;
1112 }
Johannes Berga30e3112010-09-22 18:02:01 +02001113
Johannes Berg5a3d9882011-07-15 13:03:12 -07001114 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
Johannes Berga30e3112010-09-22 18:02:01 +02001115 key_flags |= STA_KEY_MULTICAST_MSK;
1116
Johannes Berg5a3d9882011-07-15 13:03:12 -07001117 /* key pointer (offset) */
1118 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berga30e3112010-09-22 18:02:01 +02001119
Johannes Berg5a3d9882011-07-15 13:03:12 -07001120 sta_cmd.key.key_flags = key_flags;
1121 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1122 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
Johannes Berga30e3112010-09-22 18:02:01 +02001123
Johannes Berg5a3d9882011-07-15 13:03:12 -07001124 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001125}
1126
1127void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001128 struct ieee80211_vif *vif,
Johannes Berga30e3112010-09-22 18:02:01 +02001129 struct ieee80211_key_conf *keyconf,
1130 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1131{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001132 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1133
1134 if (sta_id == IWL_INVALID_STATION)
1135 return;
Johannes Berga30e3112010-09-22 18:02:01 +02001136
1137 if (iwl_scan_cancel(priv)) {
1138 /* cancel scan failed, just live w/ bad key and rely
1139 briefly on SW decryption */
1140 return;
1141 }
1142
Johannes Berge1b1c082011-07-17 01:44:11 -07001143 iwlagn_send_sta_key(priv, keyconf, sta_id,
1144 iv32, phase1key, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001145}
1146
1147int iwl_remove_dynamic_key(struct iwl_priv *priv,
1148 struct iwl_rxon_context *ctx,
1149 struct ieee80211_key_conf *keyconf,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001150 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001151{
Johannes Berga30e3112010-09-22 18:02:01 +02001152 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001153 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
Johannes Berg5dcbf482012-02-17 09:47:14 -08001154 __le16 key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001155
1156 /* if station isn't there, neither is the key */
1157 if (sta_id == IWL_INVALID_STATION)
1158 return -ENOENT;
1159
Johannes Bergfa23cb02012-03-05 11:24:25 -08001160 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001161 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1162 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1163 sta_id = IWL_INVALID_STATION;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001164 spin_unlock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001165
1166 if (sta_id == IWL_INVALID_STATION)
1167 return 0;
Johannes Berga30e3112010-09-22 18:02:01 +02001168
Johannes Bergb1eea292012-03-06 13:30:42 -08001169 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001170
1171 ctx->key_mapping_keys--;
1172
Johannes Berga30e3112010-09-22 18:02:01 +02001173 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1174 keyconf->keyidx, sta_id);
1175
Johannes Berg5a3d9882011-07-15 13:03:12 -07001176 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1177 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1178 keyconf->hw_key_idx);
Johannes Berga30e3112010-09-22 18:02:01 +02001179
Johannes Berg5dcbf482012-02-17 09:47:14 -08001180 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1181 key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1182 STA_KEY_FLG_INVALID;
1183
1184 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1185 key_flags |= STA_KEY_MULTICAST_MSK;
1186
1187 sta_cmd.key.key_flags = key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001188 sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
1189 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1190 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001191
1192 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1193}
1194
Johannes Berg5a3d9882011-07-15 13:03:12 -07001195int iwl_set_dynamic_key(struct iwl_priv *priv,
1196 struct iwl_rxon_context *ctx,
1197 struct ieee80211_key_conf *keyconf,
1198 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001199{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001200 struct ieee80211_key_seq seq;
1201 u16 p1k[5];
Johannes Berga30e3112010-09-22 18:02:01 +02001202 int ret;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001203 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1204 const u8 *addr;
1205
1206 if (sta_id == IWL_INVALID_STATION)
1207 return -EINVAL;
Johannes Berga30e3112010-09-22 18:02:01 +02001208
Johannes Bergb1eea292012-03-06 13:30:42 -08001209 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001210
Johannes Berg5a3d9882011-07-15 13:03:12 -07001211 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1212 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1213 return -ENOSPC;
1214
Johannes Berga30e3112010-09-22 18:02:01 +02001215 ctx->key_mapping_keys++;
Johannes Berga30e3112010-09-22 18:02:01 +02001216
1217 switch (keyconf->cipher) {
Johannes Berga30e3112010-09-22 18:02:01 +02001218 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001219 if (sta)
1220 addr = sta->addr;
1221 else /* station mode case only */
1222 addr = ctx->active.bssid_addr;
1223
1224 /* pre-fill phase 1 key into device cache */
1225 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1226 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Berge1b1c082011-07-17 01:44:11 -07001227 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1228 seq.tkip.iv32, p1k, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001229 break;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001230 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berga30e3112010-09-22 18:02:01 +02001231 case WLAN_CIPHER_SUITE_WEP40:
1232 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge1b1c082011-07-17 01:44:11 -07001233 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1234 0, NULL, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001235 break;
1236 default:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001237 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
Johannes Berga30e3112010-09-22 18:02:01 +02001238 ret = -EINVAL;
1239 }
1240
Johannes Berg5a3d9882011-07-15 13:03:12 -07001241 if (ret) {
1242 ctx->key_mapping_keys--;
1243 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1244 }
1245
1246 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 +02001247 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001248 sta ? sta->addr : NULL, ret);
Johannes Berga30e3112010-09-22 18:02:01 +02001249
1250 return ret;
1251}
1252
1253/**
1254 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1255 *
1256 * This adds the broadcast station into the driver's station table
1257 * and marks it driver active, so that it will be restored to the
1258 * device at the next best time.
1259 */
1260int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1261 struct iwl_rxon_context *ctx)
1262{
1263 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +02001264 u8 sta_id;
1265
Johannes Bergfa23cb02012-03-05 11:24:25 -08001266 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001267 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1268 if (sta_id == IWL_INVALID_STATION) {
1269 IWL_ERR(priv, "Unable to prepare broadcast station\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -08001270 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001271
1272 return -EINVAL;
1273 }
1274
1275 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1276 priv->stations[sta_id].used |= IWL_STA_BCAST;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001277 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001278
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001279 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001280 if (!link_cmd) {
1281 IWL_ERR(priv,
1282 "Unable to initialize rate scaling for bcast station.\n");
1283 return -ENOMEM;
1284 }
1285
Johannes Bergfa23cb02012-03-05 11:24:25 -08001286 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001287 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001288 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001289
1290 return 0;
1291}
1292
1293/**
1294 * iwl_update_bcast_station - update broadcast station's LQ command
1295 *
1296 * Only used by iwlagn. Placed here to have all bcast station management
1297 * code together.
1298 */
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001299int iwl_update_bcast_station(struct iwl_priv *priv,
1300 struct iwl_rxon_context *ctx)
Johannes Berga30e3112010-09-22 18:02:01 +02001301{
Johannes Berga30e3112010-09-22 18:02:01 +02001302 struct iwl_link_quality_cmd *link_cmd;
1303 u8 sta_id = ctx->bcast_sta_id;
1304
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001305 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001306 if (!link_cmd) {
1307 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1308 return -ENOMEM;
1309 }
1310
Johannes Bergfa23cb02012-03-05 11:24:25 -08001311 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001312 if (priv->stations[sta_id].lq)
1313 kfree(priv->stations[sta_id].lq);
1314 else
1315 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1316 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001317 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001318
1319 return 0;
1320}
1321
1322int iwl_update_bcast_stations(struct iwl_priv *priv)
1323{
1324 struct iwl_rxon_context *ctx;
1325 int ret = 0;
1326
1327 for_each_context(priv, ctx) {
1328 ret = iwl_update_bcast_station(priv, ctx);
1329 if (ret)
1330 break;
1331 }
1332
1333 return ret;
1334}
1335
1336/**
1337 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1338 */
1339int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1340{
Johannes Berga30e3112010-09-22 18:02:01 +02001341 struct iwl_addsta_cmd sta_cmd;
1342
Johannes Bergb1eea292012-03-06 13:30:42 -08001343 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001344
1345 /* Remove "disable" flag, to enable Tx for this TID */
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].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1348 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1349 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1350 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001351 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001352
1353 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1354}
1355
1356int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1357 int tid, u16 ssn)
1358{
Johannes Berga30e3112010-09-22 18:02:01 +02001359 int sta_id;
1360 struct iwl_addsta_cmd sta_cmd;
1361
Johannes Bergb1eea292012-03-06 13:30:42 -08001362 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001363
1364 sta_id = iwl_sta_id(sta);
1365 if (sta_id == IWL_INVALID_STATION)
1366 return -ENXIO;
1367
Johannes Bergfa23cb02012-03-05 11:24:25 -08001368 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001369 priv->stations[sta_id].sta.station_flags_msk = 0;
1370 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1371 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1372 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1373 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1374 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_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 iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1378}
1379
1380int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1381 int tid)
1382{
Johannes Berga30e3112010-09-22 18:02:01 +02001383 int sta_id;
1384 struct iwl_addsta_cmd sta_cmd;
1385
Johannes Bergb1eea292012-03-06 13:30:42 -08001386 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001387
1388 sta_id = iwl_sta_id(sta);
1389 if (sta_id == IWL_INVALID_STATION) {
1390 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1391 return -ENXIO;
1392 }
1393
Johannes Bergfa23cb02012-03-05 11:24:25 -08001394 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001395 priv->stations[sta_id].sta.station_flags_msk = 0;
1396 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1397 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1398 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1399 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001400 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001401
1402 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1403}
1404
Johannes Berga30e3112010-09-22 18:02:01 +02001405
Johannes Berga30e3112010-09-22 18:02:01 +02001406
1407void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1408{
Johannes Berg9451ca12012-03-05 11:24:23 -08001409 struct iwl_addsta_cmd cmd = {
1410 .mode = STA_CONTROL_MODIFY_MSK,
1411 .station_flags = STA_FLG_PWR_SAVE_MSK,
1412 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1413 .sta.sta_id = sta_id,
1414 .sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1415 .sleep_tx_count = cpu_to_le16(cnt),
1416 };
Johannes Berga30e3112010-09-22 18:02:01 +02001417
Johannes Berg9451ca12012-03-05 11:24:23 -08001418 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001419}