blob: 4bd50daeac1a845d02f7ba488b24deb57e3dbd9b [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
126int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb,
127 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
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700156 ret = iwl_trans_send_cmd(trans(priv), &cmd);
157
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
163 iwl_free_pages(priv->shrd, cmd.reply_page);
164
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
172static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
173 struct ieee80211_sta *sta,
174 struct iwl_rxon_context *ctx)
175{
176 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
177 __le32 sta_flags;
178 u8 mimo_ps_mode;
179
180 if (!sta || !sta_ht_inf->ht_supported)
181 goto done;
182
183 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
184 IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
185 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
186 "static" :
187 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
188 "dynamic" : "disabled");
189
190 sta_flags = priv->stations[index].sta.station_flags;
191
192 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
193
194 switch (mimo_ps_mode) {
195 case WLAN_HT_CAP_SM_PS_STATIC:
196 sta_flags |= STA_FLG_MIMO_DIS_MSK;
197 break;
198 case WLAN_HT_CAP_SM_PS_DYNAMIC:
199 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
200 break;
201 case WLAN_HT_CAP_SM_PS_DISABLED:
202 break;
203 default:
204 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
205 break;
206 }
207
208 sta_flags |= cpu_to_le32(
209 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
210
211 sta_flags |= cpu_to_le32(
212 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
213
214 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
215 sta_flags |= STA_FLG_HT40_EN_MSK;
216 else
217 sta_flags &= ~STA_FLG_HT40_EN_MSK;
218
219 priv->stations[index].sta.station_flags = sta_flags;
220 done:
221 return;
222}
223
224/**
225 * iwl_prep_station - Prepare station information for addition
226 *
227 * should be called with sta_lock held
228 */
229u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
230 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
231{
232 struct iwl_station_entry *station;
233 int i;
234 u8 sta_id = IWL_INVALID_STATION;
235
236 if (is_ap)
237 sta_id = ctx->ap_sta_id;
238 else if (is_broadcast_ether_addr(addr))
239 sta_id = ctx->bcast_sta_id;
240 else
241 for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
242 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
243 addr)) {
244 sta_id = i;
245 break;
246 }
247
248 if (!priv->stations[i].used &&
249 sta_id == IWL_INVALID_STATION)
250 sta_id = i;
251 }
252
253 /*
254 * These two conditions have the same outcome, but keep them
255 * separate
256 */
257 if (unlikely(sta_id == IWL_INVALID_STATION))
258 return sta_id;
259
260 /*
261 * uCode is not able to deal with multiple requests to add a
262 * station. Keep track if one is in progress so that we do not send
263 * another.
264 */
265 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
266 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
267 "added.\n", sta_id);
268 return sta_id;
269 }
270
271 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
272 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
273 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
274 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
275 "adding again.\n", sta_id, addr);
276 return sta_id;
277 }
278
279 station = &priv->stations[sta_id];
280 station->used = IWL_STA_DRIVER_ACTIVE;
281 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
282 sta_id, addr);
283 priv->num_stations++;
284
285 /* Set up the REPLY_ADD_STA command to send to device */
286 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
287 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
288 station->sta.mode = 0;
289 station->sta.sta.sta_id = sta_id;
290 station->sta.station_flags = ctx->station_flags;
291 station->ctxid = ctx->ctxid;
292
293 if (sta) {
294 struct iwl_station_priv *sta_priv;
295
296 sta_priv = (void *)sta->drv_priv;
297 sta_priv->ctx = ctx;
298 }
299
300 /*
301 * OK to call unconditionally, since local stations (IBSS BSSID
302 * STA and broadcast STA) pass in a NULL sta, and mac80211
303 * doesn't allow HT IBSS.
304 */
305 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
306
307 return sta_id;
308
309}
310
311#define STA_WAIT_TIMEOUT (HZ/2)
312
313/**
314 * iwl_add_station_common -
315 */
316int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
317 const u8 *addr, bool is_ap,
318 struct ieee80211_sta *sta, u8 *sta_id_r)
319{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700320 int ret = 0;
321 u8 sta_id;
322 struct iwl_addsta_cmd sta_cmd;
323
324 *sta_id_r = 0;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800325 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700326 sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
327 if (sta_id == IWL_INVALID_STATION) {
328 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
329 addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800330 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700331 return -EINVAL;
332 }
333
334 /*
335 * uCode is not able to deal with multiple requests to add a
336 * station. Keep track if one is in progress so that we do not send
337 * another.
338 */
339 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
340 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
341 "added.\n", sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800342 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700343 return -EEXIST;
344 }
345
346 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
347 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
348 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
349 "adding again.\n", sta_id, addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800350 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700351 return -EEXIST;
352 }
353
354 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
355 memcpy(&sta_cmd, &priv->stations[sta_id].sta,
356 sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -0800357 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700358
359 /* Add station to device's station table */
360 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
361 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800362 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700363 IWL_ERR(priv, "Adding station %pM failed.\n",
364 priv->stations[sta_id].sta.sta.addr);
365 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
366 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800367 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700368 }
369 *sta_id_r = sta_id;
370 return ret;
371}
372
373/**
374 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700375 */
376static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
377{
Johannes Bergfa23cb02012-03-05 11:24:25 -0800378 lockdep_assert_held(&priv->sta_lock);
379
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700380 /* Ucode must be active and driver must be non active */
381 if ((priv->stations[sta_id].used &
382 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
383 IWL_STA_UCODE_ACTIVE)
384 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
385
386 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
387
388 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
389 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
390}
391
392static int iwl_send_remove_station(struct iwl_priv *priv,
393 const u8 *addr, int sta_id,
394 bool temporary)
395{
396 struct iwl_rx_packet *pkt;
397 int ret;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700398 struct iwl_rem_sta_cmd rm_sta_cmd;
399
400 struct iwl_host_cmd cmd = {
401 .id = REPLY_REMOVE_STA,
402 .len = { sizeof(struct iwl_rem_sta_cmd), },
403 .flags = CMD_SYNC,
404 .data = { &rm_sta_cmd, },
405 };
406
407 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
408 rm_sta_cmd.num_sta = 1;
409 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
410
411 cmd.flags |= CMD_WANT_SKB;
412
413 ret = iwl_trans_send_cmd(trans(priv), &cmd);
414
415 if (ret)
416 return ret;
417
418 pkt = (struct iwl_rx_packet *)cmd.reply_page;
419 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
420 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
421 pkt->hdr.flags);
422 ret = -EIO;
423 }
424
425 if (!ret) {
426 switch (pkt->u.rem_sta.status) {
427 case REM_STA_SUCCESS_MSK:
428 if (!temporary) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800429 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700430 iwl_sta_ucode_deactivate(priv, sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800431 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700432 }
433 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
434 break;
435 default:
436 ret = -EIO;
437 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
438 break;
439 }
440 }
441 iwl_free_pages(priv->shrd, cmd.reply_page);
442
443 return ret;
444}
445
446/**
447 * iwl_remove_station - Remove driver's knowledge of station.
448 */
449int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
450 const u8 *addr)
451{
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200452 u8 tid;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700453
454 if (!iwl_is_ready(priv->shrd)) {
455 IWL_DEBUG_INFO(priv,
456 "Unable to remove station %pM, device not ready.\n",
457 addr);
458 /*
459 * It is typical for stations to be removed when we are
460 * going down. Return success since device will be down
461 * soon anyway
462 */
463 return 0;
464 }
465
466 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
467 sta_id, addr);
468
469 if (WARN_ON(sta_id == IWL_INVALID_STATION))
470 return -EINVAL;
471
Johannes Bergfa23cb02012-03-05 11:24:25 -0800472 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700473
474 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
475 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
476 addr);
477 goto out_err;
478 }
479
480 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
481 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
482 addr);
483 goto out_err;
484 }
485
486 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
487 kfree(priv->stations[sta_id].lq);
488 priv->stations[sta_id].lq = NULL;
489 }
490
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200491 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
492 memset(&priv->tid_data[sta_id][tid], 0,
493 sizeof(priv->tid_data[sta_id][tid]));
494
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700495 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
496
497 priv->num_stations--;
498
499 if (WARN_ON(priv->num_stations < 0))
500 priv->num_stations = 0;
501
Johannes Bergfa23cb02012-03-05 11:24:25 -0800502 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700503
504 return iwl_send_remove_station(priv, addr, sta_id, false);
505out_err:
Johannes Bergfa23cb02012-03-05 11:24:25 -0800506 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700507 return -EINVAL;
508}
509
510/**
511 * iwl_clear_ucode_stations - clear ucode station table bits
512 *
513 * This function clears all the bits in the driver indicating
514 * which stations are active in the ucode. Call when something
515 * other than explicit station management would cause this in
516 * the ucode, e.g. unassociated RXON.
517 */
518void iwl_clear_ucode_stations(struct iwl_priv *priv,
519 struct iwl_rxon_context *ctx)
520{
521 int i;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700522 bool cleared = false;
523
524 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
525
Johannes Bergfa23cb02012-03-05 11:24:25 -0800526 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700527 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
528 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
529 continue;
530
531 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
532 IWL_DEBUG_INFO(priv,
533 "Clearing ucode active for station %d\n", i);
534 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
535 cleared = true;
536 }
537 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800538 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700539
540 if (!cleared)
541 IWL_DEBUG_INFO(priv,
542 "No active stations found to be cleared\n");
543}
544
545/**
546 * iwl_restore_stations() - Restore driver known stations to device
547 *
548 * All stations considered active by driver, but not present in ucode, is
549 * restored.
550 *
551 * Function sleeps.
552 */
553void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
554{
555 struct iwl_addsta_cmd sta_cmd;
556 struct iwl_link_quality_cmd lq;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700557 int i;
558 bool found = false;
559 int ret;
560 bool send_lq;
561
562 if (!iwl_is_ready(priv->shrd)) {
563 IWL_DEBUG_INFO(priv,
564 "Not ready yet, not restoring any stations.\n");
565 return;
566 }
567
568 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -0800569 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700570 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
571 if (ctx->ctxid != priv->stations[i].ctxid)
572 continue;
573 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
574 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
575 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
576 priv->stations[i].sta.sta.addr);
577 priv->stations[i].sta.mode = 0;
578 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
579 found = true;
580 }
581 }
582
583 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
584 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
585 memcpy(&sta_cmd, &priv->stations[i].sta,
586 sizeof(struct iwl_addsta_cmd));
587 send_lq = false;
588 if (priv->stations[i].lq) {
589 if (priv->shrd->wowlan)
590 iwl_sta_fill_lq(priv, ctx, i, &lq);
591 else
592 memcpy(&lq, priv->stations[i].lq,
593 sizeof(struct iwl_link_quality_cmd));
594 send_lq = true;
595 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800596 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700597 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
598 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800599 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700600 IWL_ERR(priv, "Adding station %pM failed.\n",
601 priv->stations[i].sta.sta.addr);
602 priv->stations[i].used &=
603 ~IWL_STA_DRIVER_ACTIVE;
604 priv->stations[i].used &=
605 ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800606 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700607 }
608 /*
609 * Rate scaling has already been initialized, send
610 * current LQ command
611 */
612 if (send_lq)
613 iwl_send_lq_cmd(priv, ctx, &lq,
614 CMD_SYNC, true);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800615 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700616 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
617 }
618 }
619
Johannes Bergfa23cb02012-03-05 11:24:25 -0800620 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700621 if (!found)
622 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
623 "no stations to be restored.\n");
624 else
625 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
626 "complete.\n");
627}
628
629void iwl_reprogram_ap_sta(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
630{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700631 int sta_id = ctx->ap_sta_id;
632 int ret;
633 struct iwl_addsta_cmd sta_cmd;
634 struct iwl_link_quality_cmd lq;
Johannes Bergaed0fd42011-11-10 06:55:05 -0800635 bool active, have_lq = false;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700636
Johannes Bergfa23cb02012-03-05 11:24:25 -0800637 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700638 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800639 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700640 return;
641 }
642
643 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
644 sta_cmd.mode = 0;
Johannes Bergaed0fd42011-11-10 06:55:05 -0800645 if (priv->stations[sta_id].lq) {
646 memcpy(&lq, priv->stations[sta_id].lq, sizeof(lq));
647 have_lq = true;
648 }
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700649
650 active = priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE;
651 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800652 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700653
654 if (active) {
655 ret = iwl_send_remove_station(
656 priv, priv->stations[sta_id].sta.sta.addr,
657 sta_id, true);
658 if (ret)
659 IWL_ERR(priv, "failed to remove STA %pM (%d)\n",
660 priv->stations[sta_id].sta.sta.addr, ret);
661 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800662 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700663 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800664 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700665
666 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
667 if (ret)
668 IWL_ERR(priv, "failed to re-add STA %pM (%d)\n",
669 priv->stations[sta_id].sta.sta.addr, ret);
Johannes Bergaed0fd42011-11-10 06:55:05 -0800670 if (have_lq)
671 iwl_send_lq_cmd(priv, ctx, &lq, CMD_SYNC, true);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700672}
673
674int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
675{
676 int i;
677
678 for (i = 0; i < priv->sta_key_max_num; i++)
679 if (!test_and_set_bit(i, &priv->ucode_key_table))
680 return i;
681
682 return WEP_INVALID_OFFSET;
683}
684
685void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
686{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700687 int i;
688
Johannes Bergfa23cb02012-03-05 11:24:25 -0800689 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700690 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
691 if (!(priv->stations[i].used & IWL_STA_BCAST))
692 continue;
693
694 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
695 priv->num_stations--;
696 if (WARN_ON(priv->num_stations < 0))
697 priv->num_stations = 0;
698 kfree(priv->stations[i].lq);
699 priv->stations[i].lq = NULL;
700 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800701 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700702}
703
704#ifdef CONFIG_IWLWIFI_DEBUG
705static void iwl_dump_lq_cmd(struct iwl_priv *priv,
706 struct iwl_link_quality_cmd *lq)
707{
708 int i;
709 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
710 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
711 lq->general_params.single_stream_ant_msk,
712 lq->general_params.dual_stream_ant_msk);
713
714 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
715 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
716 i, lq->rs_table[i].rate_n_flags);
717}
718#else
719static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
720 struct iwl_link_quality_cmd *lq)
721{
722}
723#endif
724
725/**
726 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
727 *
728 * It sometimes happens when a HT rate has been in use and we
729 * loose connectivity with AP then mac80211 will first tell us that the
730 * current channel is not HT anymore before removing the station. In such a
731 * scenario the RXON flags will be updated to indicate we are not
732 * communicating HT anymore, but the LQ command may still contain HT rates.
733 * Test for this to prevent driver from sending LQ command between the time
734 * RXON flags are updated and when LQ command is updated.
735 */
736static bool is_lq_table_valid(struct iwl_priv *priv,
737 struct iwl_rxon_context *ctx,
738 struct iwl_link_quality_cmd *lq)
739{
740 int i;
741
742 if (ctx->ht.enabled)
743 return true;
744
745 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
746 ctx->active.channel);
747 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
748 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
749 RATE_MCS_HT_MSK) {
750 IWL_DEBUG_INFO(priv,
751 "index %d of LQ expects HT channel\n",
752 i);
753 return false;
754 }
755 }
756 return true;
757}
758
759/**
760 * iwl_send_lq_cmd() - Send link quality command
761 * @init: This command is sent as part of station initialization right
762 * after station has been added.
763 *
764 * The link quality command is sent as the last step of station creation.
765 * This is the special case in which init is set and we call a callback in
766 * this case to clear the state indicating that station creation is in
767 * progress.
768 */
769int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
770 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
771{
772 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700773 struct iwl_host_cmd cmd = {
774 .id = REPLY_TX_LINK_QUALITY_CMD,
775 .len = { sizeof(struct iwl_link_quality_cmd), },
776 .flags = flags,
777 .data = { lq, },
778 };
779
780 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
781 return -EINVAL;
782
783
Johannes Bergfa23cb02012-03-05 11:24:25 -0800784 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700785 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800786 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700787 return -EINVAL;
788 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800789 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700790
791 iwl_dump_lq_cmd(priv, lq);
792 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
793 return -EINVAL;
794
795 if (is_lq_table_valid(priv, ctx, lq))
796 ret = iwl_trans_send_cmd(trans(priv), &cmd);
797 else
798 ret = -EINVAL;
799
800 if (cmd.flags & CMD_ASYNC)
801 return ret;
802
803 if (init) {
804 IWL_DEBUG_INFO(priv, "init LQ command complete, "
805 "clearing sta addition status for sta %d\n",
806 lq->sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800807 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700808 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800809 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700810 }
811 return ret;
812}
813
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700814
Johannes Bergc079166e2011-10-10 07:26:54 -0700815void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
816 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
Johannes Berga30e3112010-09-22 18:02:01 +0200817{
818 int i, r;
Johannes Berga30e3112010-09-22 18:02:01 +0200819 u32 rate_flags = 0;
820 __le32 rate_n_flags;
821
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700822 lockdep_assert_held(&priv->shrd->mutex);
Johannes Bergf775aa02011-06-22 06:34:09 -0700823
Johannes Bergc079166e2011-10-10 07:26:54 -0700824 memset(link_cmd, 0, sizeof(*link_cmd));
825
Johannes Berga30e3112010-09-22 18:02:01 +0200826 /* Set up the rate scaling to start at selected rate, fall back
827 * all the way down to 1M in IEEE order, and then spin on 1M */
828 if (priv->band == IEEE80211_BAND_5GHZ)
829 r = IWL_RATE_6M_INDEX;
Johannes Bergf775aa02011-06-22 06:34:09 -0700830 else if (ctx && ctx->vif && ctx->vif->p2p)
831 r = IWL_RATE_6M_INDEX;
Johannes Berga30e3112010-09-22 18:02:01 +0200832 else
833 r = IWL_RATE_1M_INDEX;
834
835 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
836 rate_flags |= RATE_MCS_CCK_MSK;
837
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700838 rate_flags |= first_antenna(hw_params(priv).valid_tx_ant) <<
Johannes Berga30e3112010-09-22 18:02:01 +0200839 RATE_MCS_ANT_POS;
840 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
841 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
842 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
843
844 link_cmd->general_params.single_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700845 first_antenna(hw_params(priv).valid_tx_ant);
Johannes Berga30e3112010-09-22 18:02:01 +0200846
847 link_cmd->general_params.dual_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700848 hw_params(priv).valid_tx_ant &
849 ~first_antenna(hw_params(priv).valid_tx_ant);
Johannes Berga30e3112010-09-22 18:02:01 +0200850 if (!link_cmd->general_params.dual_stream_ant_msk) {
851 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700852 } else if (num_of_ant(hw_params(priv).valid_tx_ant) == 2) {
Johannes Berga30e3112010-09-22 18:02:01 +0200853 link_cmd->general_params.dual_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700854 hw_params(priv).valid_tx_ant;
Johannes Berga30e3112010-09-22 18:02:01 +0200855 }
856
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700857 link_cmd->agg_params.agg_dis_start_th =
858 LINK_QUAL_AGG_DISABLE_START_DEF;
Johannes Berga30e3112010-09-22 18:02:01 +0200859 link_cmd->agg_params.agg_time_limit =
860 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
861
862 link_cmd->sta_id = sta_id;
Johannes Bergc079166e2011-10-10 07:26:54 -0700863}
864
865static struct iwl_link_quality_cmd *
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700866iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
867 u8 sta_id)
Johannes Bergc079166e2011-10-10 07:26:54 -0700868{
869 struct iwl_link_quality_cmd *link_cmd;
870
871 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
872 if (!link_cmd) {
873 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
874 return NULL;
875 }
876
877 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200878
879 return link_cmd;
880}
881
882/*
883 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
884 *
885 * Function sleeps.
886 */
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700887int iwlagn_add_bssid_station(struct iwl_priv *priv,
888 struct iwl_rxon_context *ctx,
Johannes Berga30e3112010-09-22 18:02:01 +0200889 const u8 *addr, u8 *sta_id_r)
890{
891 int ret;
892 u8 sta_id;
893 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +0200894
895 if (sta_id_r)
896 *sta_id_r = IWL_INVALID_STATION;
897
898 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
899 if (ret) {
900 IWL_ERR(priv, "Unable to add station %pM\n", addr);
901 return ret;
902 }
903
904 if (sta_id_r)
905 *sta_id_r = sta_id;
906
Johannes Bergfa23cb02012-03-05 11:24:25 -0800907 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200908 priv->stations[sta_id].used |= IWL_STA_LOCAL;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800909 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200910
911 /* Set up default rate scaling table in device's station table */
Johannes Bergf775aa02011-06-22 06:34:09 -0700912 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200913 if (!link_cmd) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700914 IWL_ERR(priv,
915 "Unable to initialize rate scaling for station %pM.\n",
Johannes Berga30e3112010-09-22 18:02:01 +0200916 addr);
917 return -ENOMEM;
918 }
919
920 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
921 if (ret)
922 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
923
Johannes Bergfa23cb02012-03-05 11:24:25 -0800924 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200925 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800926 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200927
928 return 0;
929}
930
Johannes Berg5a3d9882011-07-15 13:03:12 -0700931/*
932 * static WEP keys
933 *
934 * For each context, the device has a table of 4 static WEP keys
935 * (one for each key index) that is updated with the following
936 * commands.
937 */
938
Johannes Berga30e3112010-09-22 18:02:01 +0200939static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
940 struct iwl_rxon_context *ctx,
941 bool send_if_empty)
942{
943 int i, not_empty = 0;
944 u8 buff[sizeof(struct iwl_wep_cmd) +
945 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
946 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
947 size_t cmd_size = sizeof(struct iwl_wep_cmd);
948 struct iwl_host_cmd cmd = {
949 .id = ctx->wep_key_cmd,
Johannes Berg3fa50732011-05-04 07:50:38 -0700950 .data = { wep_cmd, },
Johannes Berga30e3112010-09-22 18:02:01 +0200951 .flags = CMD_SYNC,
952 };
953
954 might_sleep();
955
956 memset(wep_cmd, 0, cmd_size +
957 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
958
959 for (i = 0; i < WEP_KEYS_MAX ; i++) {
960 wep_cmd->key[i].key_index = i;
961 if (ctx->wep_keys[i].key_size) {
962 wep_cmd->key[i].key_offset = i;
963 not_empty = 1;
964 } else {
965 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
966 }
967
968 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
969 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
970 ctx->wep_keys[i].key_size);
971 }
972
973 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
974 wep_cmd->num_keys = WEP_KEYS_MAX;
975
976 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
977
Johannes Berg3fa50732011-05-04 07:50:38 -0700978 cmd.len[0] = cmd_size;
Johannes Berga30e3112010-09-22 18:02:01 +0200979
980 if (not_empty || send_if_empty)
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -0700981 return iwl_trans_send_cmd(trans(priv), &cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200982 else
983 return 0;
984}
985
986int iwl_restore_default_wep_keys(struct iwl_priv *priv,
987 struct iwl_rxon_context *ctx)
988{
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700989 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +0200990
991 return iwl_send_static_wepkey_cmd(priv, ctx, false);
992}
993
994int iwl_remove_default_wep_key(struct iwl_priv *priv,
995 struct iwl_rxon_context *ctx,
996 struct ieee80211_key_conf *keyconf)
997{
998 int ret;
999
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001000 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001001
1002 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1003 keyconf->keyidx);
1004
1005 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
Emmanuel Grumbach845a9c02011-08-25 23:11:04 -07001006 if (iwl_is_rfkill(priv->shrd)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001007 IWL_DEBUG_WEP(priv,
1008 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berga30e3112010-09-22 18:02:01 +02001009 /* but keys in device are clear anyway so return success */
1010 return 0;
1011 }
1012 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1013 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1014 keyconf->keyidx, ret);
1015
1016 return ret;
1017}
1018
1019int iwl_set_default_wep_key(struct iwl_priv *priv,
1020 struct iwl_rxon_context *ctx,
1021 struct ieee80211_key_conf *keyconf)
1022{
1023 int ret;
1024
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001025 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001026
1027 if (keyconf->keylen != WEP_KEY_LEN_128 &&
1028 keyconf->keylen != WEP_KEY_LEN_64) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001029 IWL_DEBUG_WEP(priv,
1030 "Bad WEP key length %d\n", keyconf->keylen);
Johannes Berga30e3112010-09-22 18:02:01 +02001031 return -EINVAL;
1032 }
1033
Johannes Berg5a3d9882011-07-15 13:03:12 -07001034 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
Johannes Berga30e3112010-09-22 18:02:01 +02001035
1036 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1037 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1038 keyconf->keylen);
1039
1040 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1041 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1042 keyconf->keylen, keyconf->keyidx, ret);
1043
1044 return ret;
1045}
1046
Johannes Berg5a3d9882011-07-15 13:03:12 -07001047/*
1048 * dynamic (per-station) keys
1049 *
1050 * The dynamic keys are a little more complicated. The device has
1051 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1052 * These are linked to stations by a table that contains an index
1053 * into the key table for each station/key index/{mcast,unicast},
1054 * i.e. it's basically an array of pointers like this:
1055 * key_offset_t key_mapping[NUM_STATIONS][4][2];
1056 * (it really works differently, but you can think of it as such)
1057 *
1058 * The key uploading and linking happens in the same command, the
1059 * add station command with STA_MODIFY_KEY_MASK.
1060 */
1061
1062static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1063 struct ieee80211_vif *vif,
1064 struct ieee80211_sta *sta)
1065{
1066 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001067
1068 if (sta)
Johannes Berg40e4e682012-03-05 11:24:22 -08001069 return iwl_sta_id(sta);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001070
1071 /*
1072 * The device expects GTKs for station interfaces to be
1073 * installed as GTKs for the AP station. If we have no
1074 * station ID, then use the ap_sta_id in that case.
1075 */
Johannes Berg40e4e682012-03-05 11:24:22 -08001076 if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1077 return vif_priv->ctx->ap_sta_id;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001078
Johannes Berg40e4e682012-03-05 11:24:22 -08001079 return IWL_INVALID_STATION;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001080}
1081
Johannes Berge1b1c082011-07-17 01:44:11 -07001082static int iwlagn_send_sta_key(struct iwl_priv *priv,
1083 struct ieee80211_key_conf *keyconf,
1084 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1085 u32 cmd_flags)
Johannes Berga30e3112010-09-22 18:02:01 +02001086{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001087 __le16 key_flags;
Johannes Berga30e3112010-09-22 18:02:01 +02001088 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001089 int i;
Johannes Berge1b1c082011-07-17 01:44:11 -07001090
Johannes Bergfa23cb02012-03-05 11:24:25 -08001091 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001092 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001093 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001094
Johannes Berg5a3d9882011-07-15 13:03:12 -07001095 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1096 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001097
Johannes Berg5a3d9882011-07-15 13:03:12 -07001098 switch (keyconf->cipher) {
1099 case WLAN_CIPHER_SUITE_CCMP:
1100 key_flags |= STA_KEY_FLG_CCMP;
1101 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1102 break;
1103 case WLAN_CIPHER_SUITE_TKIP:
1104 key_flags |= STA_KEY_FLG_TKIP;
1105 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1106 for (i = 0; i < 5; i++)
1107 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1108 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1109 break;
1110 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga30e3112010-09-22 18:02:01 +02001111 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001112 /* fall through */
1113 case WLAN_CIPHER_SUITE_WEP40:
1114 key_flags |= STA_KEY_FLG_WEP;
1115 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1116 break;
1117 default:
1118 WARN_ON(1);
1119 return -EINVAL;
1120 }
Johannes Berga30e3112010-09-22 18:02:01 +02001121
Johannes Berg5a3d9882011-07-15 13:03:12 -07001122 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
Johannes Berga30e3112010-09-22 18:02:01 +02001123 key_flags |= STA_KEY_MULTICAST_MSK;
1124
Johannes Berg5a3d9882011-07-15 13:03:12 -07001125 /* key pointer (offset) */
1126 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berga30e3112010-09-22 18:02:01 +02001127
Johannes Berg5a3d9882011-07-15 13:03:12 -07001128 sta_cmd.key.key_flags = key_flags;
1129 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1130 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
Johannes Berga30e3112010-09-22 18:02:01 +02001131
Johannes Berg5a3d9882011-07-15 13:03:12 -07001132 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001133}
1134
1135void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001136 struct ieee80211_vif *vif,
Johannes Berga30e3112010-09-22 18:02:01 +02001137 struct ieee80211_key_conf *keyconf,
1138 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1139{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001140 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1141
1142 if (sta_id == IWL_INVALID_STATION)
1143 return;
Johannes Berga30e3112010-09-22 18:02:01 +02001144
1145 if (iwl_scan_cancel(priv)) {
1146 /* cancel scan failed, just live w/ bad key and rely
1147 briefly on SW decryption */
1148 return;
1149 }
1150
Johannes Berge1b1c082011-07-17 01:44:11 -07001151 iwlagn_send_sta_key(priv, keyconf, sta_id,
1152 iv32, phase1key, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001153}
1154
1155int iwl_remove_dynamic_key(struct iwl_priv *priv,
1156 struct iwl_rxon_context *ctx,
1157 struct ieee80211_key_conf *keyconf,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001158 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001159{
Johannes Berga30e3112010-09-22 18:02:01 +02001160 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001161 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
Johannes Berg5dcbf482012-02-17 09:47:14 -08001162 __le16 key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001163
1164 /* if station isn't there, neither is the key */
1165 if (sta_id == IWL_INVALID_STATION)
1166 return -ENOENT;
1167
Johannes Bergfa23cb02012-03-05 11:24:25 -08001168 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001169 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1170 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1171 sta_id = IWL_INVALID_STATION;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001172 spin_unlock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001173
1174 if (sta_id == IWL_INVALID_STATION)
1175 return 0;
Johannes Berga30e3112010-09-22 18:02:01 +02001176
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001177 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001178
1179 ctx->key_mapping_keys--;
1180
Johannes Berga30e3112010-09-22 18:02:01 +02001181 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1182 keyconf->keyidx, sta_id);
1183
Johannes Berg5a3d9882011-07-15 13:03:12 -07001184 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1185 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1186 keyconf->hw_key_idx);
Johannes Berga30e3112010-09-22 18:02:01 +02001187
Johannes Berg5dcbf482012-02-17 09:47:14 -08001188 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1189 key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1190 STA_KEY_FLG_INVALID;
1191
1192 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1193 key_flags |= STA_KEY_MULTICAST_MSK;
1194
1195 sta_cmd.key.key_flags = key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001196 sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
1197 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1198 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001199
1200 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1201}
1202
Johannes Berg5a3d9882011-07-15 13:03:12 -07001203int iwl_set_dynamic_key(struct iwl_priv *priv,
1204 struct iwl_rxon_context *ctx,
1205 struct ieee80211_key_conf *keyconf,
1206 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001207{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001208 struct ieee80211_key_seq seq;
1209 u16 p1k[5];
Johannes Berga30e3112010-09-22 18:02:01 +02001210 int ret;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001211 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1212 const u8 *addr;
1213
1214 if (sta_id == IWL_INVALID_STATION)
1215 return -EINVAL;
Johannes Berga30e3112010-09-22 18:02:01 +02001216
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001217 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001218
Johannes Berg5a3d9882011-07-15 13:03:12 -07001219 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1220 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1221 return -ENOSPC;
1222
Johannes Berga30e3112010-09-22 18:02:01 +02001223 ctx->key_mapping_keys++;
Johannes Berga30e3112010-09-22 18:02:01 +02001224
1225 switch (keyconf->cipher) {
Johannes Berga30e3112010-09-22 18:02:01 +02001226 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001227 if (sta)
1228 addr = sta->addr;
1229 else /* station mode case only */
1230 addr = ctx->active.bssid_addr;
1231
1232 /* pre-fill phase 1 key into device cache */
1233 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1234 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Berge1b1c082011-07-17 01:44:11 -07001235 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1236 seq.tkip.iv32, p1k, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001237 break;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001238 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berga30e3112010-09-22 18:02:01 +02001239 case WLAN_CIPHER_SUITE_WEP40:
1240 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge1b1c082011-07-17 01:44:11 -07001241 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1242 0, NULL, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001243 break;
1244 default:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001245 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
Johannes Berga30e3112010-09-22 18:02:01 +02001246 ret = -EINVAL;
1247 }
1248
Johannes Berg5a3d9882011-07-15 13:03:12 -07001249 if (ret) {
1250 ctx->key_mapping_keys--;
1251 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1252 }
1253
1254 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 +02001255 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001256 sta ? sta->addr : NULL, ret);
Johannes Berga30e3112010-09-22 18:02:01 +02001257
1258 return ret;
1259}
1260
1261/**
1262 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1263 *
1264 * This adds the broadcast station into the driver's station table
1265 * and marks it driver active, so that it will be restored to the
1266 * device at the next best time.
1267 */
1268int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1269 struct iwl_rxon_context *ctx)
1270{
1271 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +02001272 u8 sta_id;
1273
Johannes Bergfa23cb02012-03-05 11:24:25 -08001274 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001275 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1276 if (sta_id == IWL_INVALID_STATION) {
1277 IWL_ERR(priv, "Unable to prepare broadcast station\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -08001278 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001279
1280 return -EINVAL;
1281 }
1282
1283 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1284 priv->stations[sta_id].used |= IWL_STA_BCAST;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001285 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001286
Johannes Bergf775aa02011-06-22 06:34:09 -07001287 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001288 if (!link_cmd) {
1289 IWL_ERR(priv,
1290 "Unable to initialize rate scaling for bcast station.\n");
1291 return -ENOMEM;
1292 }
1293
Johannes Bergfa23cb02012-03-05 11:24:25 -08001294 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001295 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001296 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001297
1298 return 0;
1299}
1300
1301/**
1302 * iwl_update_bcast_station - update broadcast station's LQ command
1303 *
1304 * Only used by iwlagn. Placed here to have all bcast station management
1305 * code together.
1306 */
Johannes Bergf775aa02011-06-22 06:34:09 -07001307int iwl_update_bcast_station(struct iwl_priv *priv,
1308 struct iwl_rxon_context *ctx)
Johannes Berga30e3112010-09-22 18:02:01 +02001309{
Johannes Berga30e3112010-09-22 18:02:01 +02001310 struct iwl_link_quality_cmd *link_cmd;
1311 u8 sta_id = ctx->bcast_sta_id;
1312
Johannes Bergf775aa02011-06-22 06:34:09 -07001313 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001314 if (!link_cmd) {
1315 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1316 return -ENOMEM;
1317 }
1318
Johannes Bergfa23cb02012-03-05 11:24:25 -08001319 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001320 if (priv->stations[sta_id].lq)
1321 kfree(priv->stations[sta_id].lq);
1322 else
1323 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1324 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001325 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001326
1327 return 0;
1328}
1329
1330int iwl_update_bcast_stations(struct iwl_priv *priv)
1331{
1332 struct iwl_rxon_context *ctx;
1333 int ret = 0;
1334
1335 for_each_context(priv, ctx) {
1336 ret = iwl_update_bcast_station(priv, ctx);
1337 if (ret)
1338 break;
1339 }
1340
1341 return ret;
1342}
1343
1344/**
1345 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1346 */
1347int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1348{
Johannes Berga30e3112010-09-22 18:02:01 +02001349 struct iwl_addsta_cmd sta_cmd;
1350
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001351 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001352
1353 /* Remove "disable" flag, to enable Tx for this TID */
Johannes Bergfa23cb02012-03-05 11:24:25 -08001354 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001355 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1356 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1357 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1358 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001359 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001360
1361 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1362}
1363
1364int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1365 int tid, u16 ssn)
1366{
Johannes Berga30e3112010-09-22 18:02:01 +02001367 int sta_id;
1368 struct iwl_addsta_cmd sta_cmd;
1369
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001370 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001371
1372 sta_id = iwl_sta_id(sta);
1373 if (sta_id == IWL_INVALID_STATION)
1374 return -ENXIO;
1375
Johannes Bergfa23cb02012-03-05 11:24:25 -08001376 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001377 priv->stations[sta_id].sta.station_flags_msk = 0;
1378 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1379 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1380 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1381 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1382 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001383 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001384
1385 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1386}
1387
1388int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1389 int tid)
1390{
Johannes Berga30e3112010-09-22 18:02:01 +02001391 int sta_id;
1392 struct iwl_addsta_cmd sta_cmd;
1393
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001394 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001395
1396 sta_id = iwl_sta_id(sta);
1397 if (sta_id == IWL_INVALID_STATION) {
1398 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1399 return -ENXIO;
1400 }
1401
Johannes Bergfa23cb02012-03-05 11:24:25 -08001402 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001403 priv->stations[sta_id].sta.station_flags_msk = 0;
1404 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1405 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1406 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1407 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001408 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001409
1410 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1411}
1412
Johannes Berga30e3112010-09-22 18:02:01 +02001413
Johannes Berga30e3112010-09-22 18:02:01 +02001414
1415void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1416{
Johannes Berg9451ca12012-03-05 11:24:23 -08001417 struct iwl_addsta_cmd cmd = {
1418 .mode = STA_CONTROL_MODIFY_MSK,
1419 .station_flags = STA_FLG_PWR_SAVE_MSK,
1420 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1421 .sta.sta_id = sta_id,
1422 .sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1423 .sleep_tx_count = cpu_to_le16(cnt),
1424 };
Johannes Berga30e3112010-09-22 18:02:01 +02001425
Johannes Berg9451ca12012-03-05 11:24:23 -08001426 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001427}