blob: 79cbaab79dddb010a673053ab6ca1f2967ba4a8a [file] [log] [blame]
Johannes Berga30e3112010-09-22 18:02:01 +02001/******************************************************************************
2 *
Wey-Yi Guyfb4961d2012-01-06 13:16:33 -08003 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
Johannes Berga30e3112010-09-22 18:02:01 +02004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
Johannes Berge7a0d0c42012-03-06 13:31:07 -080029#include <linux/etherdevice.h>
Johannes Berga30e3112010-09-22 18:02:01 +020030#include <net/mac80211.h>
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{
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080067 struct iwl_add_sta_resp *add_sta_resp = (void *)pkt->data;
Wey-Yi Guyc745f552011-10-10 07:27:12 -070068 u8 sta_id = addsta->sta.sta_id;
Wey-Yi Guyc745f552011-10-10 07:27:12 -070069 int ret = -EIO;
70
71 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
72 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
73 pkt->hdr.flags);
74 return ret;
75 }
76
77 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
78 sta_id);
79
Johannes Bergfa23cb02012-03-05 11:24:25 -080080 spin_lock(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -070081
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080082 switch (add_sta_resp->status) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -070083 case ADD_STA_SUCCESS_MSK:
84 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
Wey-Yi Guy2da424b2012-01-06 13:16:28 -080085 ret = iwl_sta_ucode_activate(priv, sta_id);
Wey-Yi Guyc745f552011-10-10 07:27:12 -070086 break;
87 case ADD_STA_NO_ROOM_IN_TABLE:
88 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
89 sta_id);
90 break;
91 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
92 IWL_ERR(priv, "Adding station %d failed, no block ack "
93 "resource.\n", sta_id);
94 break;
95 case ADD_STA_MODIFY_NON_EXIST_STA:
96 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
97 sta_id);
98 break;
99 default:
100 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800101 add_sta_resp->status);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700102 break;
103 }
104
105 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
106 priv->stations[sta_id].sta.mode ==
107 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
108 sta_id, priv->stations[sta_id].sta.sta.addr);
109
110 /*
111 * XXX: The MAC address in the command buffer is often changed from
112 * the original sent to the device. That is, the MAC address
113 * written to the command buffer often is not the same MAC address
114 * read from the command buffer when the command returns. This
115 * issue has not yet been resolved and this debugging is left to
116 * observe the problem.
117 */
118 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
119 priv->stations[sta_id].sta.mode ==
120 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
121 addsta->sta.addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800122 spin_unlock(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700123
124 return ret;
125}
126
Johannes Berg48a2d662012-03-05 11:24:39 -0800127int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700128 struct iwl_device_cmd *cmd)
129{
130 struct iwl_rx_packet *pkt = rxb_addr(rxb);
131 struct iwl_addsta_cmd *addsta =
132 (struct iwl_addsta_cmd *) cmd->payload;
133
134 return iwl_process_add_sta_resp(priv, addsta, pkt);
135}
136
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700137int iwl_send_add_sta(struct iwl_priv *priv,
138 struct iwl_addsta_cmd *sta, u8 flags)
139{
140 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700141 struct iwl_host_cmd cmd = {
142 .id = REPLY_ADD_STA,
143 .flags = flags,
Johannes Berg69b172f2011-12-12 04:17:44 -0800144 .data = { sta, },
145 .len = { sizeof(*sta), },
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700146 };
147 u8 sta_id __maybe_unused = sta->sta.sta_id;
148
149 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
150 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
151
152 if (!(flags & CMD_ASYNC)) {
153 cmd.flags |= CMD_WANT_SKB;
154 might_sleep();
155 }
156
Johannes Berge10a0532012-03-06 13:30:39 -0800157 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700158
159 if (ret || (flags & CMD_ASYNC))
160 return ret;
161 /*else the command was successfully sent in SYNC mode, need to free
162 * the reply page */
163
Johannes Berg65b94a42012-03-05 11:24:38 -0800164 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700165
166 if (cmd.handler_status)
167 IWL_ERR(priv, "%s - error in the CMD response %d", __func__,
168 cmd.handler_status);
169
170 return cmd.handler_status;
171}
172
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800173static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
174 struct ieee80211_sta *sta,
175 struct iwl_rxon_context *ctx,
176 __le32 *flags, __le32 *mask)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700177{
178 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700179 u8 mimo_ps_mode;
180
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800181 *mask = STA_FLG_RTS_MIMO_PROT_MSK |
182 STA_FLG_MIMO_DIS_MSK |
183 STA_FLG_HT40_EN_MSK |
184 STA_FLG_MAX_AGG_SIZE_MSK |
185 STA_FLG_AGG_MPDU_DENSITY_MSK;
186 *flags = 0;
187
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700188 if (!sta || !sta_ht_inf->ht_supported)
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800189 return;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700190
191 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800192
193 IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700194 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
195 "static" :
196 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
197 "dynamic" : "disabled");
198
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700199 switch (mimo_ps_mode) {
200 case WLAN_HT_CAP_SM_PS_STATIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800201 *flags |= STA_FLG_MIMO_DIS_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700202 break;
203 case WLAN_HT_CAP_SM_PS_DYNAMIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800204 *flags |= STA_FLG_RTS_MIMO_PROT_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700205 break;
206 case WLAN_HT_CAP_SM_PS_DISABLED:
207 break;
208 default:
209 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
210 break;
211 }
212
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800213 *flags |= cpu_to_le32(
214 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700215
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800216 *flags |= cpu_to_le32(
217 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700218
219 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800220 *flags |= STA_FLG_HT40_EN_MSK;
221}
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700222
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800223int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
224 struct ieee80211_sta *sta)
225{
226 u8 sta_id = iwl_sta_id(sta);
227 __le32 flags, mask;
228 struct iwl_addsta_cmd cmd;
229
230 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
231 return -EINVAL;
232
233 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
234
235 spin_lock_bh(&priv->sta_lock);
236 priv->stations[sta_id].sta.station_flags &= ~mask;
237 priv->stations[sta_id].sta.station_flags |= flags;
238 spin_unlock_bh(&priv->sta_lock);
239
240 memset(&cmd, 0, sizeof(cmd));
241 cmd.mode = STA_CONTROL_MODIFY_MSK;
242 cmd.station_flags_msk = mask;
243 cmd.station_flags = flags;
244 cmd.sta.sta_id = sta_id;
245
246 return iwl_send_add_sta(priv, &cmd, CMD_SYNC);
247}
248
249static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
250 struct ieee80211_sta *sta,
251 struct iwl_rxon_context *ctx)
252{
253 __le32 flags, mask;
254
255 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
256
257 lockdep_assert_held(&priv->sta_lock);
258 priv->stations[index].sta.station_flags &= ~mask;
259 priv->stations[index].sta.station_flags |= flags;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700260}
261
262/**
263 * iwl_prep_station - Prepare station information for addition
264 *
265 * should be called with sta_lock held
266 */
267u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
268 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
269{
270 struct iwl_station_entry *station;
271 int i;
272 u8 sta_id = IWL_INVALID_STATION;
273
274 if (is_ap)
275 sta_id = ctx->ap_sta_id;
276 else if (is_broadcast_ether_addr(addr))
277 sta_id = ctx->bcast_sta_id;
278 else
279 for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
280 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
281 addr)) {
282 sta_id = i;
283 break;
284 }
285
286 if (!priv->stations[i].used &&
287 sta_id == IWL_INVALID_STATION)
288 sta_id = i;
289 }
290
291 /*
292 * These two conditions have the same outcome, but keep them
293 * separate
294 */
295 if (unlikely(sta_id == IWL_INVALID_STATION))
296 return sta_id;
297
298 /*
299 * uCode is not able to deal with multiple requests to add a
300 * station. Keep track if one is in progress so that we do not send
301 * another.
302 */
303 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
304 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
305 "added.\n", sta_id);
306 return sta_id;
307 }
308
309 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
310 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
311 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
312 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
313 "adding again.\n", sta_id, addr);
314 return sta_id;
315 }
316
317 station = &priv->stations[sta_id];
318 station->used = IWL_STA_DRIVER_ACTIVE;
319 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
320 sta_id, addr);
321 priv->num_stations++;
322
323 /* Set up the REPLY_ADD_STA command to send to device */
324 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
325 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
326 station->sta.mode = 0;
327 station->sta.sta.sta_id = sta_id;
328 station->sta.station_flags = ctx->station_flags;
329 station->ctxid = ctx->ctxid;
330
331 if (sta) {
332 struct iwl_station_priv *sta_priv;
333
334 sta_priv = (void *)sta->drv_priv;
335 sta_priv->ctx = ctx;
336 }
337
338 /*
339 * OK to call unconditionally, since local stations (IBSS BSSID
340 * STA and broadcast STA) pass in a NULL sta, and mac80211
341 * doesn't allow HT IBSS.
342 */
343 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
344
345 return sta_id;
346
347}
348
349#define STA_WAIT_TIMEOUT (HZ/2)
350
351/**
352 * iwl_add_station_common -
353 */
354int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
355 const u8 *addr, bool is_ap,
356 struct ieee80211_sta *sta, u8 *sta_id_r)
357{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700358 int ret = 0;
359 u8 sta_id;
360 struct iwl_addsta_cmd sta_cmd;
361
362 *sta_id_r = 0;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800363 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700364 sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
365 if (sta_id == IWL_INVALID_STATION) {
366 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
367 addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800368 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700369 return -EINVAL;
370 }
371
372 /*
373 * uCode is not able to deal with multiple requests to add a
374 * station. Keep track if one is in progress so that we do not send
375 * another.
376 */
377 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
378 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
379 "added.\n", sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800380 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700381 return -EEXIST;
382 }
383
384 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
385 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
386 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
387 "adding again.\n", sta_id, addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800388 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700389 return -EEXIST;
390 }
391
392 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
393 memcpy(&sta_cmd, &priv->stations[sta_id].sta,
394 sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -0800395 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700396
397 /* Add station to device's station table */
398 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
399 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800400 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700401 IWL_ERR(priv, "Adding station %pM failed.\n",
402 priv->stations[sta_id].sta.sta.addr);
403 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
404 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800405 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700406 }
407 *sta_id_r = sta_id;
408 return ret;
409}
410
411/**
412 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700413 */
414static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
415{
Johannes Bergfa23cb02012-03-05 11:24:25 -0800416 lockdep_assert_held(&priv->sta_lock);
417
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700418 /* Ucode must be active and driver must be non active */
419 if ((priv->stations[sta_id].used &
420 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
421 IWL_STA_UCODE_ACTIVE)
422 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
423
424 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
425
426 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
427 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
428}
429
430static int iwl_send_remove_station(struct iwl_priv *priv,
431 const u8 *addr, int sta_id,
432 bool temporary)
433{
434 struct iwl_rx_packet *pkt;
435 int ret;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700436 struct iwl_rem_sta_cmd rm_sta_cmd;
437
438 struct iwl_host_cmd cmd = {
439 .id = REPLY_REMOVE_STA,
440 .len = { sizeof(struct iwl_rem_sta_cmd), },
441 .flags = CMD_SYNC,
442 .data = { &rm_sta_cmd, },
443 };
444
445 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
446 rm_sta_cmd.num_sta = 1;
447 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
448
449 cmd.flags |= CMD_WANT_SKB;
450
Johannes Berge10a0532012-03-06 13:30:39 -0800451 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700452
453 if (ret)
454 return ret;
455
Johannes Berg65b94a42012-03-05 11:24:38 -0800456 pkt = cmd.resp_pkt;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700457 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
458 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
459 pkt->hdr.flags);
460 ret = -EIO;
461 }
462
463 if (!ret) {
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800464 struct iwl_rem_sta_resp *rem_sta_resp = (void *)pkt->data;
465 switch (rem_sta_resp->status) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700466 case REM_STA_SUCCESS_MSK:
467 if (!temporary) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800468 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700469 iwl_sta_ucode_deactivate(priv, sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800470 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700471 }
472 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
473 break;
474 default:
475 ret = -EIO;
476 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
477 break;
478 }
479 }
Johannes Berg65b94a42012-03-05 11:24:38 -0800480 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700481
482 return ret;
483}
484
485/**
486 * iwl_remove_station - Remove driver's knowledge of station.
487 */
488int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
489 const u8 *addr)
490{
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200491 u8 tid;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700492
Don Fry83626402012-03-07 09:52:37 -0800493 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700494 IWL_DEBUG_INFO(priv,
495 "Unable to remove station %pM, device not ready.\n",
496 addr);
497 /*
498 * It is typical for stations to be removed when we are
499 * going down. Return success since device will be down
500 * soon anyway
501 */
502 return 0;
503 }
504
505 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
506 sta_id, addr);
507
508 if (WARN_ON(sta_id == IWL_INVALID_STATION))
509 return -EINVAL;
510
Johannes Bergfa23cb02012-03-05 11:24:25 -0800511 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700512
513 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
514 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
515 addr);
516 goto out_err;
517 }
518
519 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
520 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
521 addr);
522 goto out_err;
523 }
524
525 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
526 kfree(priv->stations[sta_id].lq);
527 priv->stations[sta_id].lq = NULL;
528 }
529
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200530 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
531 memset(&priv->tid_data[sta_id][tid], 0,
532 sizeof(priv->tid_data[sta_id][tid]));
533
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700534 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
535
536 priv->num_stations--;
537
538 if (WARN_ON(priv->num_stations < 0))
539 priv->num_stations = 0;
540
Johannes Bergfa23cb02012-03-05 11:24:25 -0800541 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700542
543 return iwl_send_remove_station(priv, addr, sta_id, false);
544out_err:
Johannes Bergfa23cb02012-03-05 11:24:25 -0800545 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700546 return -EINVAL;
547}
548
Johannes Berg97420512012-03-07 09:52:42 -0800549void iwl_deactivate_station(struct iwl_priv *priv, const u8 sta_id,
550 const u8 *addr)
551{
552 u8 tid;
553
554 if (!iwl_is_ready(priv)) {
555 IWL_DEBUG_INFO(priv,
556 "Unable to remove station %pM, device not ready.\n",
557 addr);
558 return;
559 }
560
561 IWL_DEBUG_ASSOC(priv, "Deactivating STA: %pM (%d)\n", addr, sta_id);
562
563 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
564 return;
565
566 spin_lock_bh(&priv->sta_lock);
567
568 WARN_ON_ONCE(!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE));
569
570 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
571 memset(&priv->tid_data[sta_id][tid], 0,
572 sizeof(priv->tid_data[sta_id][tid]));
573
574 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
575
576 priv->num_stations--;
577
578 if (WARN_ON_ONCE(priv->num_stations < 0))
579 priv->num_stations = 0;
580
581 spin_unlock_bh(&priv->sta_lock);
582}
583
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100584static void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
585 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
586{
587 int i, r;
588 u32 rate_flags = 0;
589 __le32 rate_n_flags;
590
591 lockdep_assert_held(&priv->mutex);
592
593 memset(link_cmd, 0, sizeof(*link_cmd));
594
595 /* Set up the rate scaling to start at selected rate, fall back
596 * all the way down to 1M in IEEE order, and then spin on 1M */
597 if (priv->band == IEEE80211_BAND_5GHZ)
598 r = IWL_RATE_6M_INDEX;
599 else if (ctx && ctx->vif && ctx->vif->p2p)
600 r = IWL_RATE_6M_INDEX;
601 else
602 r = IWL_RATE_1M_INDEX;
603
604 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
605 rate_flags |= RATE_MCS_CCK_MSK;
606
607 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
608 RATE_MCS_ANT_POS;
609 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
610 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
611 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
612
613 link_cmd->general_params.single_stream_ant_msk =
614 first_antenna(priv->hw_params.valid_tx_ant);
615
616 link_cmd->general_params.dual_stream_ant_msk =
617 priv->hw_params.valid_tx_ant &
618 ~first_antenna(priv->hw_params.valid_tx_ant);
619 if (!link_cmd->general_params.dual_stream_ant_msk) {
620 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
621 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
622 link_cmd->general_params.dual_stream_ant_msk =
623 priv->hw_params.valid_tx_ant;
624 }
625
626 link_cmd->agg_params.agg_dis_start_th =
627 LINK_QUAL_AGG_DISABLE_START_DEF;
628 link_cmd->agg_params.agg_time_limit =
629 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
630
631 link_cmd->sta_id = sta_id;
632}
633
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700634/**
635 * iwl_clear_ucode_stations - clear ucode station table bits
636 *
637 * This function clears all the bits in the driver indicating
638 * which stations are active in the ucode. Call when something
639 * other than explicit station management would cause this in
640 * the ucode, e.g. unassociated RXON.
641 */
642void iwl_clear_ucode_stations(struct iwl_priv *priv,
643 struct iwl_rxon_context *ctx)
644{
645 int i;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700646 bool cleared = false;
647
648 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
649
Johannes Bergfa23cb02012-03-05 11:24:25 -0800650 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700651 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
652 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
653 continue;
654
655 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
656 IWL_DEBUG_INFO(priv,
657 "Clearing ucode active for station %d\n", i);
658 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
659 cleared = true;
660 }
661 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800662 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700663
664 if (!cleared)
665 IWL_DEBUG_INFO(priv,
666 "No active stations found to be cleared\n");
667}
668
669/**
670 * iwl_restore_stations() - Restore driver known stations to device
671 *
672 * All stations considered active by driver, but not present in ucode, is
673 * restored.
674 *
675 * Function sleeps.
676 */
677void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
678{
679 struct iwl_addsta_cmd sta_cmd;
680 struct iwl_link_quality_cmd lq;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700681 int i;
682 bool found = false;
683 int ret;
684 bool send_lq;
685
Don Fry83626402012-03-07 09:52:37 -0800686 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700687 IWL_DEBUG_INFO(priv,
688 "Not ready yet, not restoring any stations.\n");
689 return;
690 }
691
692 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -0800693 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700694 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
695 if (ctx->ctxid != priv->stations[i].ctxid)
696 continue;
697 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
698 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
699 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
700 priv->stations[i].sta.sta.addr);
701 priv->stations[i].sta.mode = 0;
702 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
703 found = true;
704 }
705 }
706
707 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
708 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
709 memcpy(&sta_cmd, &priv->stations[i].sta,
710 sizeof(struct iwl_addsta_cmd));
711 send_lq = false;
712 if (priv->stations[i].lq) {
Johannes Berg15b86bf2012-03-05 11:24:36 -0800713 if (priv->wowlan)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700714 iwl_sta_fill_lq(priv, ctx, i, &lq);
715 else
716 memcpy(&lq, priv->stations[i].lq,
717 sizeof(struct iwl_link_quality_cmd));
718 send_lq = true;
719 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800720 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700721 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
722 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800723 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700724 IWL_ERR(priv, "Adding station %pM failed.\n",
725 priv->stations[i].sta.sta.addr);
726 priv->stations[i].used &=
727 ~IWL_STA_DRIVER_ACTIVE;
728 priv->stations[i].used &=
729 ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800730 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700731 }
732 /*
733 * Rate scaling has already been initialized, send
734 * current LQ command
735 */
736 if (send_lq)
737 iwl_send_lq_cmd(priv, ctx, &lq,
738 CMD_SYNC, true);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800739 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700740 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
741 }
742 }
743
Johannes Bergfa23cb02012-03-05 11:24:25 -0800744 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700745 if (!found)
746 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
747 "no stations to be restored.\n");
748 else
749 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
750 "complete.\n");
751}
752
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700753int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
754{
755 int i;
756
757 for (i = 0; i < priv->sta_key_max_num; i++)
758 if (!test_and_set_bit(i, &priv->ucode_key_table))
759 return i;
760
761 return WEP_INVALID_OFFSET;
762}
763
764void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
765{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700766 int i;
767
Johannes Bergfa23cb02012-03-05 11:24:25 -0800768 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700769 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
770 if (!(priv->stations[i].used & IWL_STA_BCAST))
771 continue;
772
773 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
774 priv->num_stations--;
775 if (WARN_ON(priv->num_stations < 0))
776 priv->num_stations = 0;
777 kfree(priv->stations[i].lq);
778 priv->stations[i].lq = NULL;
779 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800780 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700781}
782
783#ifdef CONFIG_IWLWIFI_DEBUG
784static void iwl_dump_lq_cmd(struct iwl_priv *priv,
785 struct iwl_link_quality_cmd *lq)
786{
787 int i;
788 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
789 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
790 lq->general_params.single_stream_ant_msk,
791 lq->general_params.dual_stream_ant_msk);
792
793 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
794 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
795 i, lq->rs_table[i].rate_n_flags);
796}
797#else
798static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
799 struct iwl_link_quality_cmd *lq)
800{
801}
802#endif
803
804/**
805 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
806 *
807 * It sometimes happens when a HT rate has been in use and we
808 * loose connectivity with AP then mac80211 will first tell us that the
809 * current channel is not HT anymore before removing the station. In such a
810 * scenario the RXON flags will be updated to indicate we are not
811 * communicating HT anymore, but the LQ command may still contain HT rates.
812 * Test for this to prevent driver from sending LQ command between the time
813 * RXON flags are updated and when LQ command is updated.
814 */
815static bool is_lq_table_valid(struct iwl_priv *priv,
816 struct iwl_rxon_context *ctx,
817 struct iwl_link_quality_cmd *lq)
818{
819 int i;
820
821 if (ctx->ht.enabled)
822 return true;
823
824 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
825 ctx->active.channel);
826 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
827 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
828 RATE_MCS_HT_MSK) {
829 IWL_DEBUG_INFO(priv,
830 "index %d of LQ expects HT channel\n",
831 i);
832 return false;
833 }
834 }
835 return true;
836}
837
838/**
839 * iwl_send_lq_cmd() - Send link quality command
840 * @init: This command is sent as part of station initialization right
841 * after station has been added.
842 *
843 * The link quality command is sent as the last step of station creation.
844 * This is the special case in which init is set and we call a callback in
845 * this case to clear the state indicating that station creation is in
846 * progress.
847 */
848int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
849 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
850{
851 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700852 struct iwl_host_cmd cmd = {
853 .id = REPLY_TX_LINK_QUALITY_CMD,
854 .len = { sizeof(struct iwl_link_quality_cmd), },
855 .flags = flags,
856 .data = { lq, },
857 };
858
859 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
860 return -EINVAL;
861
862
Johannes Bergfa23cb02012-03-05 11:24:25 -0800863 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700864 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800865 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700866 return -EINVAL;
867 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800868 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700869
870 iwl_dump_lq_cmd(priv, lq);
871 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
872 return -EINVAL;
873
874 if (is_lq_table_valid(priv, ctx, lq))
Johannes Berge10a0532012-03-06 13:30:39 -0800875 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700876 else
877 ret = -EINVAL;
878
879 if (cmd.flags & CMD_ASYNC)
880 return ret;
881
882 if (init) {
883 IWL_DEBUG_INFO(priv, "init LQ command complete, "
884 "clearing sta addition status for sta %d\n",
885 lq->sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800886 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700887 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800888 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700889 }
890 return ret;
891}
892
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700893
Johannes Bergc079166e2011-10-10 07:26:54 -0700894static struct iwl_link_quality_cmd *
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700895iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
896 u8 sta_id)
Johannes Bergc079166e2011-10-10 07:26:54 -0700897{
898 struct iwl_link_quality_cmd *link_cmd;
899
900 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
901 if (!link_cmd) {
902 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
903 return NULL;
904 }
905
906 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200907
908 return link_cmd;
909}
910
911/*
912 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
913 *
914 * Function sleeps.
915 */
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700916int iwlagn_add_bssid_station(struct iwl_priv *priv,
917 struct iwl_rxon_context *ctx,
Johannes Berga30e3112010-09-22 18:02:01 +0200918 const u8 *addr, u8 *sta_id_r)
919{
920 int ret;
921 u8 sta_id;
922 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +0200923
924 if (sta_id_r)
925 *sta_id_r = IWL_INVALID_STATION;
926
927 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
928 if (ret) {
929 IWL_ERR(priv, "Unable to add station %pM\n", addr);
930 return ret;
931 }
932
933 if (sta_id_r)
934 *sta_id_r = sta_id;
935
Johannes Bergfa23cb02012-03-05 11:24:25 -0800936 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200937 priv->stations[sta_id].used |= IWL_STA_LOCAL;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800938 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200939
940 /* Set up default rate scaling table in device's station table */
Johannes Bergf775aa06d2011-06-22 06:34:09 -0700941 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200942 if (!link_cmd) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700943 IWL_ERR(priv,
944 "Unable to initialize rate scaling for station %pM.\n",
Johannes Berga30e3112010-09-22 18:02:01 +0200945 addr);
946 return -ENOMEM;
947 }
948
949 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
950 if (ret)
951 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
952
Johannes Bergfa23cb02012-03-05 11:24:25 -0800953 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200954 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800955 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200956
957 return 0;
958}
959
Johannes Berg5a3d9882011-07-15 13:03:12 -0700960/*
961 * static WEP keys
962 *
963 * For each context, the device has a table of 4 static WEP keys
964 * (one for each key index) that is updated with the following
965 * commands.
966 */
967
Johannes Berga30e3112010-09-22 18:02:01 +0200968static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
969 struct iwl_rxon_context *ctx,
970 bool send_if_empty)
971{
972 int i, not_empty = 0;
973 u8 buff[sizeof(struct iwl_wep_cmd) +
974 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
975 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
976 size_t cmd_size = sizeof(struct iwl_wep_cmd);
977 struct iwl_host_cmd cmd = {
978 .id = ctx->wep_key_cmd,
Johannes Berg3fa50732011-05-04 07:50:38 -0700979 .data = { wep_cmd, },
Johannes Berga30e3112010-09-22 18:02:01 +0200980 .flags = CMD_SYNC,
981 };
982
983 might_sleep();
984
985 memset(wep_cmd, 0, cmd_size +
986 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
987
988 for (i = 0; i < WEP_KEYS_MAX ; i++) {
989 wep_cmd->key[i].key_index = i;
990 if (ctx->wep_keys[i].key_size) {
991 wep_cmd->key[i].key_offset = i;
992 not_empty = 1;
993 } else {
994 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
995 }
996
997 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
998 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
999 ctx->wep_keys[i].key_size);
1000 }
1001
1002 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
1003 wep_cmd->num_keys = WEP_KEYS_MAX;
1004
1005 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
1006
Johannes Berg3fa50732011-05-04 07:50:38 -07001007 cmd.len[0] = cmd_size;
Johannes Berga30e3112010-09-22 18:02:01 +02001008
1009 if (not_empty || send_if_empty)
Johannes Berge10a0532012-03-06 13:30:39 -08001010 return iwl_dvm_send_cmd(priv, &cmd);
Johannes Berga30e3112010-09-22 18:02:01 +02001011 else
1012 return 0;
1013}
1014
1015int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1016 struct iwl_rxon_context *ctx)
1017{
Johannes Bergb1eea292012-03-06 13:30:42 -08001018 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001019
1020 return iwl_send_static_wepkey_cmd(priv, ctx, false);
1021}
1022
1023int iwl_remove_default_wep_key(struct iwl_priv *priv,
1024 struct iwl_rxon_context *ctx,
1025 struct ieee80211_key_conf *keyconf)
1026{
1027 int ret;
1028
Johannes Bergb1eea292012-03-06 13:30:42 -08001029 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001030
1031 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1032 keyconf->keyidx);
1033
1034 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
Don Fry83626402012-03-07 09:52:37 -08001035 if (iwl_is_rfkill(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001036 IWL_DEBUG_WEP(priv,
1037 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berga30e3112010-09-22 18:02:01 +02001038 /* but keys in device are clear anyway so return success */
1039 return 0;
1040 }
1041 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1042 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1043 keyconf->keyidx, ret);
1044
1045 return ret;
1046}
1047
1048int iwl_set_default_wep_key(struct iwl_priv *priv,
1049 struct iwl_rxon_context *ctx,
1050 struct ieee80211_key_conf *keyconf)
1051{
1052 int ret;
1053
Johannes Bergb1eea292012-03-06 13:30:42 -08001054 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001055
1056 if (keyconf->keylen != WEP_KEY_LEN_128 &&
1057 keyconf->keylen != WEP_KEY_LEN_64) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001058 IWL_DEBUG_WEP(priv,
1059 "Bad WEP key length %d\n", keyconf->keylen);
Johannes Berga30e3112010-09-22 18:02:01 +02001060 return -EINVAL;
1061 }
1062
Johannes Berg5a3d9882011-07-15 13:03:12 -07001063 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
Johannes Berga30e3112010-09-22 18:02:01 +02001064
1065 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1066 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1067 keyconf->keylen);
1068
1069 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1070 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1071 keyconf->keylen, keyconf->keyidx, ret);
1072
1073 return ret;
1074}
1075
Johannes Berg5a3d9882011-07-15 13:03:12 -07001076/*
1077 * dynamic (per-station) keys
1078 *
1079 * The dynamic keys are a little more complicated. The device has
1080 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1081 * These are linked to stations by a table that contains an index
1082 * into the key table for each station/key index/{mcast,unicast},
1083 * i.e. it's basically an array of pointers like this:
1084 * key_offset_t key_mapping[NUM_STATIONS][4][2];
1085 * (it really works differently, but you can think of it as such)
1086 *
1087 * The key uploading and linking happens in the same command, the
1088 * add station command with STA_MODIFY_KEY_MASK.
1089 */
1090
1091static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1092 struct ieee80211_vif *vif,
1093 struct ieee80211_sta *sta)
1094{
1095 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001096
1097 if (sta)
Johannes Berg40e4e682012-03-05 11:24:22 -08001098 return iwl_sta_id(sta);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001099
1100 /*
1101 * The device expects GTKs for station interfaces to be
1102 * installed as GTKs for the AP station. If we have no
1103 * station ID, then use the ap_sta_id in that case.
1104 */
Johannes Berg40e4e682012-03-05 11:24:22 -08001105 if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1106 return vif_priv->ctx->ap_sta_id;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001107
Johannes Berg40e4e682012-03-05 11:24:22 -08001108 return IWL_INVALID_STATION;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001109}
1110
Johannes Berge1b1c082011-07-17 01:44:11 -07001111static int iwlagn_send_sta_key(struct iwl_priv *priv,
1112 struct ieee80211_key_conf *keyconf,
1113 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1114 u32 cmd_flags)
Johannes Berga30e3112010-09-22 18:02:01 +02001115{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001116 __le16 key_flags;
Johannes Berga30e3112010-09-22 18:02:01 +02001117 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001118 int i;
Johannes Berge1b1c082011-07-17 01:44:11 -07001119
Johannes Bergfa23cb02012-03-05 11:24:25 -08001120 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001121 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001122 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001123
Johannes Berg5a3d9882011-07-15 13:03:12 -07001124 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1125 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001126
Johannes Berg5a3d9882011-07-15 13:03:12 -07001127 switch (keyconf->cipher) {
1128 case WLAN_CIPHER_SUITE_CCMP:
1129 key_flags |= STA_KEY_FLG_CCMP;
1130 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1131 break;
1132 case WLAN_CIPHER_SUITE_TKIP:
1133 key_flags |= STA_KEY_FLG_TKIP;
1134 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1135 for (i = 0; i < 5; i++)
1136 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1137 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1138 break;
1139 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga30e3112010-09-22 18:02:01 +02001140 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001141 /* fall through */
1142 case WLAN_CIPHER_SUITE_WEP40:
1143 key_flags |= STA_KEY_FLG_WEP;
1144 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1145 break;
1146 default:
1147 WARN_ON(1);
1148 return -EINVAL;
1149 }
Johannes Berga30e3112010-09-22 18:02:01 +02001150
Johannes Berg5a3d9882011-07-15 13:03:12 -07001151 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
Johannes Berga30e3112010-09-22 18:02:01 +02001152 key_flags |= STA_KEY_MULTICAST_MSK;
1153
Johannes Berg5a3d9882011-07-15 13:03:12 -07001154 /* key pointer (offset) */
1155 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berga30e3112010-09-22 18:02:01 +02001156
Johannes Berg5a3d9882011-07-15 13:03:12 -07001157 sta_cmd.key.key_flags = key_flags;
1158 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1159 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
Johannes Berga30e3112010-09-22 18:02:01 +02001160
Johannes Berg5a3d9882011-07-15 13:03:12 -07001161 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001162}
1163
1164void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001165 struct ieee80211_vif *vif,
Johannes Berga30e3112010-09-22 18:02:01 +02001166 struct ieee80211_key_conf *keyconf,
1167 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1168{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001169 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1170
1171 if (sta_id == IWL_INVALID_STATION)
1172 return;
Johannes Berga30e3112010-09-22 18:02:01 +02001173
1174 if (iwl_scan_cancel(priv)) {
1175 /* cancel scan failed, just live w/ bad key and rely
1176 briefly on SW decryption */
1177 return;
1178 }
1179
Johannes Berge1b1c082011-07-17 01:44:11 -07001180 iwlagn_send_sta_key(priv, keyconf, sta_id,
1181 iv32, phase1key, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001182}
1183
1184int iwl_remove_dynamic_key(struct iwl_priv *priv,
1185 struct iwl_rxon_context *ctx,
1186 struct ieee80211_key_conf *keyconf,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001187 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001188{
Johannes Berga30e3112010-09-22 18:02:01 +02001189 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001190 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
Johannes Berg5dcbf482012-02-17 09:47:14 -08001191 __le16 key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001192
1193 /* if station isn't there, neither is the key */
1194 if (sta_id == IWL_INVALID_STATION)
1195 return -ENOENT;
1196
Johannes Bergfa23cb02012-03-05 11:24:25 -08001197 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001198 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1199 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1200 sta_id = IWL_INVALID_STATION;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001201 spin_unlock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001202
1203 if (sta_id == IWL_INVALID_STATION)
1204 return 0;
Johannes Berga30e3112010-09-22 18:02:01 +02001205
Johannes Bergb1eea292012-03-06 13:30:42 -08001206 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001207
1208 ctx->key_mapping_keys--;
1209
Johannes Berga30e3112010-09-22 18:02:01 +02001210 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1211 keyconf->keyidx, sta_id);
1212
Johannes Berg5a3d9882011-07-15 13:03:12 -07001213 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1214 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1215 keyconf->hw_key_idx);
Johannes Berga30e3112010-09-22 18:02:01 +02001216
Johannes Berg5dcbf482012-02-17 09:47:14 -08001217 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1218 key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1219 STA_KEY_FLG_INVALID;
1220
1221 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1222 key_flags |= STA_KEY_MULTICAST_MSK;
1223
1224 sta_cmd.key.key_flags = key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001225 sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
1226 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1227 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001228
1229 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1230}
1231
Johannes Berg5a3d9882011-07-15 13:03:12 -07001232int iwl_set_dynamic_key(struct iwl_priv *priv,
1233 struct iwl_rxon_context *ctx,
1234 struct ieee80211_key_conf *keyconf,
1235 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001236{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001237 struct ieee80211_key_seq seq;
1238 u16 p1k[5];
Johannes Berga30e3112010-09-22 18:02:01 +02001239 int ret;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001240 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1241 const u8 *addr;
1242
1243 if (sta_id == IWL_INVALID_STATION)
1244 return -EINVAL;
Johannes Berga30e3112010-09-22 18:02:01 +02001245
Johannes Bergb1eea292012-03-06 13:30:42 -08001246 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001247
Johannes Berg5a3d9882011-07-15 13:03:12 -07001248 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1249 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1250 return -ENOSPC;
1251
Johannes Berga30e3112010-09-22 18:02:01 +02001252 ctx->key_mapping_keys++;
Johannes Berga30e3112010-09-22 18:02:01 +02001253
1254 switch (keyconf->cipher) {
Johannes Berga30e3112010-09-22 18:02:01 +02001255 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001256 if (sta)
1257 addr = sta->addr;
1258 else /* station mode case only */
1259 addr = ctx->active.bssid_addr;
1260
1261 /* pre-fill phase 1 key into device cache */
1262 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1263 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Berge1b1c082011-07-17 01:44:11 -07001264 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1265 seq.tkip.iv32, p1k, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001266 break;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001267 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berga30e3112010-09-22 18:02:01 +02001268 case WLAN_CIPHER_SUITE_WEP40:
1269 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge1b1c082011-07-17 01:44:11 -07001270 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1271 0, NULL, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001272 break;
1273 default:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001274 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
Johannes Berga30e3112010-09-22 18:02:01 +02001275 ret = -EINVAL;
1276 }
1277
Johannes Berg5a3d9882011-07-15 13:03:12 -07001278 if (ret) {
1279 ctx->key_mapping_keys--;
1280 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1281 }
1282
1283 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 +02001284 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001285 sta ? sta->addr : NULL, ret);
Johannes Berga30e3112010-09-22 18:02:01 +02001286
1287 return ret;
1288}
1289
1290/**
1291 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1292 *
1293 * This adds the broadcast station into the driver's station table
1294 * and marks it driver active, so that it will be restored to the
1295 * device at the next best time.
1296 */
1297int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1298 struct iwl_rxon_context *ctx)
1299{
1300 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +02001301 u8 sta_id;
1302
Johannes Bergfa23cb02012-03-05 11:24:25 -08001303 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001304 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1305 if (sta_id == IWL_INVALID_STATION) {
1306 IWL_ERR(priv, "Unable to prepare broadcast station\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -08001307 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001308
1309 return -EINVAL;
1310 }
1311
1312 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1313 priv->stations[sta_id].used |= IWL_STA_BCAST;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001314 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001315
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001316 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001317 if (!link_cmd) {
1318 IWL_ERR(priv,
1319 "Unable to initialize rate scaling for bcast station.\n");
1320 return -ENOMEM;
1321 }
1322
Johannes Bergfa23cb02012-03-05 11:24:25 -08001323 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001324 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
1330/**
1331 * iwl_update_bcast_station - update broadcast station's LQ command
1332 *
1333 * Only used by iwlagn. Placed here to have all bcast station management
1334 * code together.
1335 */
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001336int iwl_update_bcast_station(struct iwl_priv *priv,
1337 struct iwl_rxon_context *ctx)
Johannes Berga30e3112010-09-22 18:02:01 +02001338{
Johannes Berga30e3112010-09-22 18:02:01 +02001339 struct iwl_link_quality_cmd *link_cmd;
1340 u8 sta_id = ctx->bcast_sta_id;
1341
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001342 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001343 if (!link_cmd) {
1344 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1345 return -ENOMEM;
1346 }
1347
Johannes Bergfa23cb02012-03-05 11:24:25 -08001348 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001349 if (priv->stations[sta_id].lq)
1350 kfree(priv->stations[sta_id].lq);
1351 else
1352 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1353 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001354 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001355
1356 return 0;
1357}
1358
1359int iwl_update_bcast_stations(struct iwl_priv *priv)
1360{
1361 struct iwl_rxon_context *ctx;
1362 int ret = 0;
1363
1364 for_each_context(priv, ctx) {
1365 ret = iwl_update_bcast_station(priv, ctx);
1366 if (ret)
1367 break;
1368 }
1369
1370 return ret;
1371}
1372
1373/**
1374 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1375 */
1376int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1377{
Johannes Berga30e3112010-09-22 18:02:01 +02001378 struct iwl_addsta_cmd sta_cmd;
1379
Johannes Bergb1eea292012-03-06 13:30:42 -08001380 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001381
1382 /* Remove "disable" flag, to enable Tx for this TID */
Johannes Bergfa23cb02012-03-05 11:24:25 -08001383 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001384 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1385 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1386 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1387 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001388 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001389
1390 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1391}
1392
1393int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1394 int tid, u16 ssn)
1395{
Johannes Berga30e3112010-09-22 18:02:01 +02001396 int sta_id;
1397 struct iwl_addsta_cmd sta_cmd;
1398
Johannes Bergb1eea292012-03-06 13:30:42 -08001399 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001400
1401 sta_id = iwl_sta_id(sta);
1402 if (sta_id == IWL_INVALID_STATION)
1403 return -ENXIO;
1404
Johannes Bergfa23cb02012-03-05 11:24:25 -08001405 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001406 priv->stations[sta_id].sta.station_flags_msk = 0;
1407 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1408 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1409 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1410 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1411 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001412 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001413
1414 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1415}
1416
1417int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1418 int tid)
1419{
Johannes Berga30e3112010-09-22 18:02:01 +02001420 int sta_id;
1421 struct iwl_addsta_cmd sta_cmd;
1422
Johannes Bergb1eea292012-03-06 13:30:42 -08001423 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001424
1425 sta_id = iwl_sta_id(sta);
1426 if (sta_id == IWL_INVALID_STATION) {
1427 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1428 return -ENXIO;
1429 }
1430
Johannes Bergfa23cb02012-03-05 11:24:25 -08001431 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001432 priv->stations[sta_id].sta.station_flags_msk = 0;
1433 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1434 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1435 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1436 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001437 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001438
1439 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1440}
1441
Johannes Berga30e3112010-09-22 18:02:01 +02001442
Johannes Berga30e3112010-09-22 18:02:01 +02001443
1444void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1445{
Johannes Berg9451ca12012-03-05 11:24:23 -08001446 struct iwl_addsta_cmd cmd = {
1447 .mode = STA_CONTROL_MODIFY_MSK,
1448 .station_flags = STA_FLG_PWR_SAVE_MSK,
1449 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1450 .sta.sta_id = sta_id,
1451 .sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1452 .sleep_tx_count = cpu_to_le16(cnt),
1453 };
Johannes Berga30e3112010-09-22 18:02:01 +02001454
Johannes Berg9451ca12012-03-05 11:24:23 -08001455 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001456}