blob: 8be535f8f6d4dad5538135f9d9077d287ce1e8be [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 Berge7a0d0c2012-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",
Meenakshi Venkataramana75b6122012-05-16 22:40:50 +0200194 sta->addr,
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700195 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
196 "static" :
197 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
198 "dynamic" : "disabled");
199
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700200 switch (mimo_ps_mode) {
201 case WLAN_HT_CAP_SM_PS_STATIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800202 *flags |= STA_FLG_MIMO_DIS_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700203 break;
204 case WLAN_HT_CAP_SM_PS_DYNAMIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800205 *flags |= STA_FLG_RTS_MIMO_PROT_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700206 break;
207 case WLAN_HT_CAP_SM_PS_DISABLED:
208 break;
209 default:
210 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
211 break;
212 }
213
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800214 *flags |= cpu_to_le32(
215 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700216
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800217 *flags |= cpu_to_le32(
218 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700219
220 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800221 *flags |= STA_FLG_HT40_EN_MSK;
222}
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700223
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800224int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
225 struct ieee80211_sta *sta)
226{
227 u8 sta_id = iwl_sta_id(sta);
228 __le32 flags, mask;
229 struct iwl_addsta_cmd cmd;
230
231 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
232 return -EINVAL;
233
234 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
235
236 spin_lock_bh(&priv->sta_lock);
237 priv->stations[sta_id].sta.station_flags &= ~mask;
238 priv->stations[sta_id].sta.station_flags |= flags;
239 spin_unlock_bh(&priv->sta_lock);
240
241 memset(&cmd, 0, sizeof(cmd));
242 cmd.mode = STA_CONTROL_MODIFY_MSK;
243 cmd.station_flags_msk = mask;
244 cmd.station_flags = flags;
245 cmd.sta.sta_id = sta_id;
246
247 return iwl_send_add_sta(priv, &cmd, CMD_SYNC);
248}
249
250static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
251 struct ieee80211_sta *sta,
252 struct iwl_rxon_context *ctx)
253{
254 __le32 flags, mask;
255
256 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
257
258 lockdep_assert_held(&priv->sta_lock);
259 priv->stations[index].sta.station_flags &= ~mask;
260 priv->stations[index].sta.station_flags |= flags;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700261}
262
263/**
264 * iwl_prep_station - Prepare station information for addition
265 *
266 * should be called with sta_lock held
267 */
268u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
269 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
270{
271 struct iwl_station_entry *station;
272 int i;
273 u8 sta_id = IWL_INVALID_STATION;
274
275 if (is_ap)
276 sta_id = ctx->ap_sta_id;
277 else if (is_broadcast_ether_addr(addr))
278 sta_id = ctx->bcast_sta_id;
279 else
280 for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
281 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
282 addr)) {
283 sta_id = i;
284 break;
285 }
286
287 if (!priv->stations[i].used &&
288 sta_id == IWL_INVALID_STATION)
289 sta_id = i;
290 }
291
292 /*
293 * These two conditions have the same outcome, but keep them
294 * separate
295 */
296 if (unlikely(sta_id == IWL_INVALID_STATION))
297 return sta_id;
298
299 /*
300 * uCode is not able to deal with multiple requests to add a
301 * station. Keep track if one is in progress so that we do not send
302 * another.
303 */
304 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
305 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
306 "added.\n", sta_id);
307 return sta_id;
308 }
309
310 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
311 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
312 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
313 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
314 "adding again.\n", sta_id, addr);
315 return sta_id;
316 }
317
318 station = &priv->stations[sta_id];
319 station->used = IWL_STA_DRIVER_ACTIVE;
320 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
321 sta_id, addr);
322 priv->num_stations++;
323
324 /* Set up the REPLY_ADD_STA command to send to device */
325 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
326 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
327 station->sta.mode = 0;
328 station->sta.sta.sta_id = sta_id;
329 station->sta.station_flags = ctx->station_flags;
330 station->ctxid = ctx->ctxid;
331
332 if (sta) {
333 struct iwl_station_priv *sta_priv;
334
335 sta_priv = (void *)sta->drv_priv;
336 sta_priv->ctx = ctx;
337 }
338
339 /*
340 * OK to call unconditionally, since local stations (IBSS BSSID
341 * STA and broadcast STA) pass in a NULL sta, and mac80211
342 * doesn't allow HT IBSS.
343 */
344 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
345
346 return sta_id;
347
348}
349
350#define STA_WAIT_TIMEOUT (HZ/2)
351
352/**
353 * iwl_add_station_common -
354 */
355int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
356 const u8 *addr, bool is_ap,
357 struct ieee80211_sta *sta, u8 *sta_id_r)
358{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700359 int ret = 0;
360 u8 sta_id;
361 struct iwl_addsta_cmd sta_cmd;
362
363 *sta_id_r = 0;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800364 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700365 sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
366 if (sta_id == IWL_INVALID_STATION) {
367 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
368 addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800369 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700370 return -EINVAL;
371 }
372
373 /*
374 * uCode is not able to deal with multiple requests to add a
375 * station. Keep track if one is in progress so that we do not send
376 * another.
377 */
378 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
379 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
380 "added.\n", sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800381 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700382 return -EEXIST;
383 }
384
385 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
386 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
387 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
388 "adding again.\n", sta_id, addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800389 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700390 return -EEXIST;
391 }
392
393 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
394 memcpy(&sta_cmd, &priv->stations[sta_id].sta,
395 sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -0800396 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700397
398 /* Add station to device's station table */
399 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
400 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800401 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700402 IWL_ERR(priv, "Adding station %pM failed.\n",
403 priv->stations[sta_id].sta.sta.addr);
404 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
405 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800406 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700407 }
408 *sta_id_r = sta_id;
409 return ret;
410}
411
412/**
413 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700414 */
415static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
416{
Johannes Bergfa23cb02012-03-05 11:24:25 -0800417 lockdep_assert_held(&priv->sta_lock);
418
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700419 /* Ucode must be active and driver must be non active */
420 if ((priv->stations[sta_id].used &
421 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
422 IWL_STA_UCODE_ACTIVE)
423 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
424
425 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
426
427 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
428 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
429}
430
431static int iwl_send_remove_station(struct iwl_priv *priv,
432 const u8 *addr, int sta_id,
433 bool temporary)
434{
435 struct iwl_rx_packet *pkt;
436 int ret;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700437 struct iwl_rem_sta_cmd rm_sta_cmd;
438
439 struct iwl_host_cmd cmd = {
440 .id = REPLY_REMOVE_STA,
441 .len = { sizeof(struct iwl_rem_sta_cmd), },
442 .flags = CMD_SYNC,
443 .data = { &rm_sta_cmd, },
444 };
445
446 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
447 rm_sta_cmd.num_sta = 1;
448 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
449
450 cmd.flags |= CMD_WANT_SKB;
451
Johannes Berge10a0532012-03-06 13:30:39 -0800452 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700453
454 if (ret)
455 return ret;
456
Johannes Berg65b94a42012-03-05 11:24:38 -0800457 pkt = cmd.resp_pkt;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700458 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
459 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
460 pkt->hdr.flags);
461 ret = -EIO;
462 }
463
464 if (!ret) {
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800465 struct iwl_rem_sta_resp *rem_sta_resp = (void *)pkt->data;
466 switch (rem_sta_resp->status) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700467 case REM_STA_SUCCESS_MSK:
468 if (!temporary) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800469 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700470 iwl_sta_ucode_deactivate(priv, sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800471 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700472 }
473 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
474 break;
475 default:
476 ret = -EIO;
477 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
478 break;
479 }
480 }
Johannes Berg65b94a42012-03-05 11:24:38 -0800481 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700482
483 return ret;
484}
485
486/**
487 * iwl_remove_station - Remove driver's knowledge of station.
488 */
489int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
490 const u8 *addr)
491{
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200492 u8 tid;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700493
Don Fry83626402012-03-07 09:52:37 -0800494 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700495 IWL_DEBUG_INFO(priv,
496 "Unable to remove station %pM, device not ready.\n",
497 addr);
498 /*
499 * It is typical for stations to be removed when we are
500 * going down. Return success since device will be down
501 * soon anyway
502 */
503 return 0;
504 }
505
506 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
507 sta_id, addr);
508
509 if (WARN_ON(sta_id == IWL_INVALID_STATION))
510 return -EINVAL;
511
Johannes Bergfa23cb02012-03-05 11:24:25 -0800512 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700513
514 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
515 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
516 addr);
517 goto out_err;
518 }
519
520 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
521 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
522 addr);
523 goto out_err;
524 }
525
526 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
527 kfree(priv->stations[sta_id].lq);
528 priv->stations[sta_id].lq = NULL;
529 }
530
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200531 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
532 memset(&priv->tid_data[sta_id][tid], 0,
533 sizeof(priv->tid_data[sta_id][tid]));
534
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700535 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
536
537 priv->num_stations--;
538
539 if (WARN_ON(priv->num_stations < 0))
540 priv->num_stations = 0;
541
Johannes Bergfa23cb02012-03-05 11:24:25 -0800542 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700543
544 return iwl_send_remove_station(priv, addr, sta_id, false);
545out_err:
Johannes Bergfa23cb02012-03-05 11:24:25 -0800546 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700547 return -EINVAL;
548}
549
Johannes Berg97420512012-03-07 09:52:42 -0800550void iwl_deactivate_station(struct iwl_priv *priv, const u8 sta_id,
551 const u8 *addr)
552{
553 u8 tid;
554
555 if (!iwl_is_ready(priv)) {
556 IWL_DEBUG_INFO(priv,
557 "Unable to remove station %pM, device not ready.\n",
558 addr);
559 return;
560 }
561
562 IWL_DEBUG_ASSOC(priv, "Deactivating STA: %pM (%d)\n", addr, sta_id);
563
564 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
565 return;
566
567 spin_lock_bh(&priv->sta_lock);
568
569 WARN_ON_ONCE(!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE));
570
571 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
572 memset(&priv->tid_data[sta_id][tid], 0,
573 sizeof(priv->tid_data[sta_id][tid]));
574
575 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
576
577 priv->num_stations--;
578
579 if (WARN_ON_ONCE(priv->num_stations < 0))
580 priv->num_stations = 0;
581
582 spin_unlock_bh(&priv->sta_lock);
583}
584
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700585/**
586 * iwl_clear_ucode_stations - clear ucode station table bits
587 *
588 * This function clears all the bits in the driver indicating
589 * which stations are active in the ucode. Call when something
590 * other than explicit station management would cause this in
591 * the ucode, e.g. unassociated RXON.
592 */
593void iwl_clear_ucode_stations(struct iwl_priv *priv,
594 struct iwl_rxon_context *ctx)
595{
596 int i;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700597 bool cleared = false;
598
599 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
600
Johannes Bergfa23cb02012-03-05 11:24:25 -0800601 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700602 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
603 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
604 continue;
605
606 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
607 IWL_DEBUG_INFO(priv,
608 "Clearing ucode active for station %d\n", i);
609 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
610 cleared = true;
611 }
612 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800613 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700614
615 if (!cleared)
616 IWL_DEBUG_INFO(priv,
617 "No active stations found to be cleared\n");
618}
619
620/**
621 * iwl_restore_stations() - Restore driver known stations to device
622 *
623 * All stations considered active by driver, but not present in ucode, is
624 * restored.
625 *
626 * Function sleeps.
627 */
628void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
629{
630 struct iwl_addsta_cmd sta_cmd;
631 struct iwl_link_quality_cmd lq;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700632 int i;
633 bool found = false;
634 int ret;
635 bool send_lq;
636
Don Fry83626402012-03-07 09:52:37 -0800637 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700638 IWL_DEBUG_INFO(priv,
639 "Not ready yet, not restoring any stations.\n");
640 return;
641 }
642
643 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -0800644 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700645 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
646 if (ctx->ctxid != priv->stations[i].ctxid)
647 continue;
648 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
649 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
650 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
651 priv->stations[i].sta.sta.addr);
652 priv->stations[i].sta.mode = 0;
653 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
654 found = true;
655 }
656 }
657
658 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
659 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
660 memcpy(&sta_cmd, &priv->stations[i].sta,
661 sizeof(struct iwl_addsta_cmd));
662 send_lq = false;
663 if (priv->stations[i].lq) {
Johannes Berg15b86bf2012-03-05 11:24:36 -0800664 if (priv->wowlan)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700665 iwl_sta_fill_lq(priv, ctx, i, &lq);
666 else
667 memcpy(&lq, priv->stations[i].lq,
668 sizeof(struct iwl_link_quality_cmd));
669 send_lq = true;
670 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800671 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700672 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
673 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800674 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700675 IWL_ERR(priv, "Adding station %pM failed.\n",
676 priv->stations[i].sta.sta.addr);
677 priv->stations[i].used &=
678 ~IWL_STA_DRIVER_ACTIVE;
679 priv->stations[i].used &=
680 ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800681 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700682 }
683 /*
684 * Rate scaling has already been initialized, send
685 * current LQ command
686 */
687 if (send_lq)
688 iwl_send_lq_cmd(priv, ctx, &lq,
689 CMD_SYNC, true);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800690 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700691 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
692 }
693 }
694
Johannes Bergfa23cb02012-03-05 11:24:25 -0800695 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700696 if (!found)
697 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
698 "no stations to be restored.\n");
699 else
700 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
701 "complete.\n");
702}
703
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700704int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
705{
706 int i;
707
708 for (i = 0; i < priv->sta_key_max_num; i++)
709 if (!test_and_set_bit(i, &priv->ucode_key_table))
710 return i;
711
712 return WEP_INVALID_OFFSET;
713}
714
715void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
716{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700717 int i;
718
Johannes Bergfa23cb02012-03-05 11:24:25 -0800719 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700720 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
721 if (!(priv->stations[i].used & IWL_STA_BCAST))
722 continue;
723
724 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
725 priv->num_stations--;
726 if (WARN_ON(priv->num_stations < 0))
727 priv->num_stations = 0;
728 kfree(priv->stations[i].lq);
729 priv->stations[i].lq = NULL;
730 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800731 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700732}
733
734#ifdef CONFIG_IWLWIFI_DEBUG
735static void iwl_dump_lq_cmd(struct iwl_priv *priv,
736 struct iwl_link_quality_cmd *lq)
737{
738 int i;
739 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
740 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
741 lq->general_params.single_stream_ant_msk,
742 lq->general_params.dual_stream_ant_msk);
743
744 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
745 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
746 i, lq->rs_table[i].rate_n_flags);
747}
748#else
749static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
750 struct iwl_link_quality_cmd *lq)
751{
752}
753#endif
754
755/**
756 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
757 *
758 * It sometimes happens when a HT rate has been in use and we
759 * loose connectivity with AP then mac80211 will first tell us that the
760 * current channel is not HT anymore before removing the station. In such a
761 * scenario the RXON flags will be updated to indicate we are not
762 * communicating HT anymore, but the LQ command may still contain HT rates.
763 * Test for this to prevent driver from sending LQ command between the time
764 * RXON flags are updated and when LQ command is updated.
765 */
766static bool is_lq_table_valid(struct iwl_priv *priv,
767 struct iwl_rxon_context *ctx,
768 struct iwl_link_quality_cmd *lq)
769{
770 int i;
771
772 if (ctx->ht.enabled)
773 return true;
774
775 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
776 ctx->active.channel);
777 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
778 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
779 RATE_MCS_HT_MSK) {
780 IWL_DEBUG_INFO(priv,
781 "index %d of LQ expects HT channel\n",
782 i);
783 return false;
784 }
785 }
786 return true;
787}
788
789/**
790 * iwl_send_lq_cmd() - Send link quality command
791 * @init: This command is sent as part of station initialization right
792 * after station has been added.
793 *
794 * The link quality command is sent as the last step of station creation.
795 * This is the special case in which init is set and we call a callback in
796 * this case to clear the state indicating that station creation is in
797 * progress.
798 */
799int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
800 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
801{
802 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700803 struct iwl_host_cmd cmd = {
804 .id = REPLY_TX_LINK_QUALITY_CMD,
805 .len = { sizeof(struct iwl_link_quality_cmd), },
806 .flags = flags,
807 .data = { lq, },
808 };
809
810 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
811 return -EINVAL;
812
813
Johannes Bergfa23cb02012-03-05 11:24:25 -0800814 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700815 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800816 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700817 return -EINVAL;
818 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800819 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700820
821 iwl_dump_lq_cmd(priv, lq);
822 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
823 return -EINVAL;
824
825 if (is_lq_table_valid(priv, ctx, lq))
Johannes Berge10a0532012-03-06 13:30:39 -0800826 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700827 else
828 ret = -EINVAL;
829
830 if (cmd.flags & CMD_ASYNC)
831 return ret;
832
833 if (init) {
834 IWL_DEBUG_INFO(priv, "init LQ command complete, "
835 "clearing sta addition status for sta %d\n",
836 lq->sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800837 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700838 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800839 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700840 }
841 return ret;
842}
843
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700844
Johannes Bergc0791662011-10-10 07:26:54 -0700845void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
846 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
Johannes Berga30e3112010-09-22 18:02:01 +0200847{
848 int i, r;
Johannes Berga30e3112010-09-22 18:02:01 +0200849 u32 rate_flags = 0;
850 __le32 rate_n_flags;
851
Johannes Bergb1eea292012-03-06 13:30:42 -0800852 lockdep_assert_held(&priv->mutex);
Johannes Bergf775aa02011-06-22 06:34:09 -0700853
Johannes Bergc0791662011-10-10 07:26:54 -0700854 memset(link_cmd, 0, sizeof(*link_cmd));
855
Johannes Berga30e3112010-09-22 18:02:01 +0200856 /* Set up the rate scaling to start at selected rate, fall back
857 * all the way down to 1M in IEEE order, and then spin on 1M */
858 if (priv->band == IEEE80211_BAND_5GHZ)
859 r = IWL_RATE_6M_INDEX;
Johannes Bergf775aa02011-06-22 06:34:09 -0700860 else if (ctx && ctx->vif && ctx->vif->p2p)
861 r = IWL_RATE_6M_INDEX;
Johannes Berga30e3112010-09-22 18:02:01 +0200862 else
863 r = IWL_RATE_1M_INDEX;
864
865 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
866 rate_flags |= RATE_MCS_CCK_MSK;
867
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700868 rate_flags |= first_antenna(hw_params(priv).valid_tx_ant) <<
Johannes Berga30e3112010-09-22 18:02:01 +0200869 RATE_MCS_ANT_POS;
870 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
871 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
872 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
873
874 link_cmd->general_params.single_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700875 first_antenna(hw_params(priv).valid_tx_ant);
Johannes Berga30e3112010-09-22 18:02:01 +0200876
877 link_cmd->general_params.dual_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700878 hw_params(priv).valid_tx_ant &
879 ~first_antenna(hw_params(priv).valid_tx_ant);
Johannes Berga30e3112010-09-22 18:02:01 +0200880 if (!link_cmd->general_params.dual_stream_ant_msk) {
881 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700882 } else if (num_of_ant(hw_params(priv).valid_tx_ant) == 2) {
Johannes Berga30e3112010-09-22 18:02:01 +0200883 link_cmd->general_params.dual_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700884 hw_params(priv).valid_tx_ant;
Johannes Berga30e3112010-09-22 18:02:01 +0200885 }
886
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700887 link_cmd->agg_params.agg_dis_start_th =
888 LINK_QUAL_AGG_DISABLE_START_DEF;
Johannes Berga30e3112010-09-22 18:02:01 +0200889 link_cmd->agg_params.agg_time_limit =
890 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
891
892 link_cmd->sta_id = sta_id;
Johannes Bergc0791662011-10-10 07:26:54 -0700893}
894
895static struct iwl_link_quality_cmd *
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700896iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
897 u8 sta_id)
Johannes Bergc0791662011-10-10 07:26:54 -0700898{
899 struct iwl_link_quality_cmd *link_cmd;
900
901 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
902 if (!link_cmd) {
903 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
904 return NULL;
905 }
906
907 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200908
909 return link_cmd;
910}
911
912/*
913 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
914 *
915 * Function sleeps.
916 */
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700917int iwlagn_add_bssid_station(struct iwl_priv *priv,
918 struct iwl_rxon_context *ctx,
Johannes Berga30e3112010-09-22 18:02:01 +0200919 const u8 *addr, u8 *sta_id_r)
920{
921 int ret;
922 u8 sta_id;
923 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +0200924
925 if (sta_id_r)
926 *sta_id_r = IWL_INVALID_STATION;
927
928 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
929 if (ret) {
930 IWL_ERR(priv, "Unable to add station %pM\n", addr);
931 return ret;
932 }
933
934 if (sta_id_r)
935 *sta_id_r = sta_id;
936
Johannes Bergfa23cb02012-03-05 11:24:25 -0800937 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200938 priv->stations[sta_id].used |= IWL_STA_LOCAL;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800939 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200940
941 /* Set up default rate scaling table in device's station table */
Johannes Bergf775aa02011-06-22 06:34:09 -0700942 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200943 if (!link_cmd) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700944 IWL_ERR(priv,
945 "Unable to initialize rate scaling for station %pM.\n",
Johannes Berga30e3112010-09-22 18:02:01 +0200946 addr);
947 return -ENOMEM;
948 }
949
950 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
951 if (ret)
952 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
953
Johannes Bergfa23cb02012-03-05 11:24:25 -0800954 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200955 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800956 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200957
958 return 0;
959}
960
Johannes Berg5a3d9882011-07-15 13:03:12 -0700961/*
962 * static WEP keys
963 *
964 * For each context, the device has a table of 4 static WEP keys
965 * (one for each key index) that is updated with the following
966 * commands.
967 */
968
Johannes Berga30e3112010-09-22 18:02:01 +0200969static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
970 struct iwl_rxon_context *ctx,
971 bool send_if_empty)
972{
973 int i, not_empty = 0;
974 u8 buff[sizeof(struct iwl_wep_cmd) +
975 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
976 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
977 size_t cmd_size = sizeof(struct iwl_wep_cmd);
978 struct iwl_host_cmd cmd = {
979 .id = ctx->wep_key_cmd,
Johannes Berg3fa50732011-05-04 07:50:38 -0700980 .data = { wep_cmd, },
Johannes Berga30e3112010-09-22 18:02:01 +0200981 .flags = CMD_SYNC,
982 };
983
984 might_sleep();
985
986 memset(wep_cmd, 0, cmd_size +
987 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
988
989 for (i = 0; i < WEP_KEYS_MAX ; i++) {
990 wep_cmd->key[i].key_index = i;
991 if (ctx->wep_keys[i].key_size) {
992 wep_cmd->key[i].key_offset = i;
993 not_empty = 1;
994 } else {
995 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
996 }
997
998 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
999 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
1000 ctx->wep_keys[i].key_size);
1001 }
1002
1003 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
1004 wep_cmd->num_keys = WEP_KEYS_MAX;
1005
1006 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
1007
Johannes Berg3fa50732011-05-04 07:50:38 -07001008 cmd.len[0] = cmd_size;
Johannes Berga30e3112010-09-22 18:02:01 +02001009
1010 if (not_empty || send_if_empty)
Johannes Berge10a0532012-03-06 13:30:39 -08001011 return iwl_dvm_send_cmd(priv, &cmd);
Johannes Berga30e3112010-09-22 18:02:01 +02001012 else
1013 return 0;
1014}
1015
1016int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1017 struct iwl_rxon_context *ctx)
1018{
Johannes Bergb1eea292012-03-06 13:30:42 -08001019 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001020
1021 return iwl_send_static_wepkey_cmd(priv, ctx, false);
1022}
1023
1024int iwl_remove_default_wep_key(struct iwl_priv *priv,
1025 struct iwl_rxon_context *ctx,
1026 struct ieee80211_key_conf *keyconf)
1027{
1028 int ret;
1029
Johannes Bergb1eea292012-03-06 13:30:42 -08001030 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001031
1032 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1033 keyconf->keyidx);
1034
1035 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
Don Fry83626402012-03-07 09:52:37 -08001036 if (iwl_is_rfkill(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001037 IWL_DEBUG_WEP(priv,
1038 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berga30e3112010-09-22 18:02:01 +02001039 /* but keys in device are clear anyway so return success */
1040 return 0;
1041 }
1042 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1043 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1044 keyconf->keyidx, ret);
1045
1046 return ret;
1047}
1048
1049int iwl_set_default_wep_key(struct iwl_priv *priv,
1050 struct iwl_rxon_context *ctx,
1051 struct ieee80211_key_conf *keyconf)
1052{
1053 int ret;
1054
Johannes Bergb1eea292012-03-06 13:30:42 -08001055 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001056
1057 if (keyconf->keylen != WEP_KEY_LEN_128 &&
1058 keyconf->keylen != WEP_KEY_LEN_64) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001059 IWL_DEBUG_WEP(priv,
1060 "Bad WEP key length %d\n", keyconf->keylen);
Johannes Berga30e3112010-09-22 18:02:01 +02001061 return -EINVAL;
1062 }
1063
Johannes Berg5a3d9882011-07-15 13:03:12 -07001064 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
Johannes Berga30e3112010-09-22 18:02:01 +02001065
1066 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1067 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1068 keyconf->keylen);
1069
1070 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1071 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1072 keyconf->keylen, keyconf->keyidx, ret);
1073
1074 return ret;
1075}
1076
Johannes Berg5a3d9882011-07-15 13:03:12 -07001077/*
1078 * dynamic (per-station) keys
1079 *
1080 * The dynamic keys are a little more complicated. The device has
1081 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1082 * These are linked to stations by a table that contains an index
1083 * into the key table for each station/key index/{mcast,unicast},
1084 * i.e. it's basically an array of pointers like this:
1085 * key_offset_t key_mapping[NUM_STATIONS][4][2];
1086 * (it really works differently, but you can think of it as such)
1087 *
1088 * The key uploading and linking happens in the same command, the
1089 * add station command with STA_MODIFY_KEY_MASK.
1090 */
1091
1092static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1093 struct ieee80211_vif *vif,
1094 struct ieee80211_sta *sta)
1095{
1096 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001097
1098 if (sta)
Johannes Berg40e4e682012-03-05 11:24:22 -08001099 return iwl_sta_id(sta);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001100
1101 /*
1102 * The device expects GTKs for station interfaces to be
1103 * installed as GTKs for the AP station. If we have no
1104 * station ID, then use the ap_sta_id in that case.
1105 */
Johannes Berg40e4e682012-03-05 11:24:22 -08001106 if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1107 return vif_priv->ctx->ap_sta_id;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001108
Johannes Berg40e4e682012-03-05 11:24:22 -08001109 return IWL_INVALID_STATION;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001110}
1111
Johannes Berge1b1c082011-07-17 01:44:11 -07001112static int iwlagn_send_sta_key(struct iwl_priv *priv,
1113 struct ieee80211_key_conf *keyconf,
1114 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1115 u32 cmd_flags)
Johannes Berga30e3112010-09-22 18:02:01 +02001116{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001117 __le16 key_flags;
Johannes Berga30e3112010-09-22 18:02:01 +02001118 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001119 int i;
Johannes Berge1b1c082011-07-17 01:44:11 -07001120
Johannes Bergfa23cb02012-03-05 11:24:25 -08001121 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001122 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001123 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001124
Johannes Berg5a3d9882011-07-15 13:03:12 -07001125 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1126 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001127
Johannes Berg5a3d9882011-07-15 13:03:12 -07001128 switch (keyconf->cipher) {
1129 case WLAN_CIPHER_SUITE_CCMP:
1130 key_flags |= STA_KEY_FLG_CCMP;
1131 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1132 break;
1133 case WLAN_CIPHER_SUITE_TKIP:
1134 key_flags |= STA_KEY_FLG_TKIP;
1135 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1136 for (i = 0; i < 5; i++)
1137 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1138 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1139 break;
1140 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga30e3112010-09-22 18:02:01 +02001141 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001142 /* fall through */
1143 case WLAN_CIPHER_SUITE_WEP40:
1144 key_flags |= STA_KEY_FLG_WEP;
1145 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1146 break;
1147 default:
1148 WARN_ON(1);
1149 return -EINVAL;
1150 }
Johannes Berga30e3112010-09-22 18:02:01 +02001151
Johannes Berg5a3d9882011-07-15 13:03:12 -07001152 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
Johannes Berga30e3112010-09-22 18:02:01 +02001153 key_flags |= STA_KEY_MULTICAST_MSK;
1154
Johannes Berg5a3d9882011-07-15 13:03:12 -07001155 /* key pointer (offset) */
1156 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berga30e3112010-09-22 18:02:01 +02001157
Johannes Berg5a3d9882011-07-15 13:03:12 -07001158 sta_cmd.key.key_flags = key_flags;
1159 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1160 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
Johannes Berga30e3112010-09-22 18:02:01 +02001161
Johannes Berg5a3d9882011-07-15 13:03:12 -07001162 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001163}
1164
1165void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001166 struct ieee80211_vif *vif,
Johannes Berga30e3112010-09-22 18:02:01 +02001167 struct ieee80211_key_conf *keyconf,
1168 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1169{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001170 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1171
1172 if (sta_id == IWL_INVALID_STATION)
1173 return;
Johannes Berga30e3112010-09-22 18:02:01 +02001174
1175 if (iwl_scan_cancel(priv)) {
1176 /* cancel scan failed, just live w/ bad key and rely
1177 briefly on SW decryption */
1178 return;
1179 }
1180
Johannes Berge1b1c082011-07-17 01:44:11 -07001181 iwlagn_send_sta_key(priv, keyconf, sta_id,
1182 iv32, phase1key, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001183}
1184
1185int iwl_remove_dynamic_key(struct iwl_priv *priv,
1186 struct iwl_rxon_context *ctx,
1187 struct ieee80211_key_conf *keyconf,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001188 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001189{
Johannes Berga30e3112010-09-22 18:02:01 +02001190 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001191 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
Johannes Berg5dcbf482012-02-17 09:47:14 -08001192 __le16 key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001193
1194 /* if station isn't there, neither is the key */
1195 if (sta_id == IWL_INVALID_STATION)
1196 return -ENOENT;
1197
Johannes Bergfa23cb02012-03-05 11:24:25 -08001198 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001199 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1200 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1201 sta_id = IWL_INVALID_STATION;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001202 spin_unlock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001203
1204 if (sta_id == IWL_INVALID_STATION)
1205 return 0;
Johannes Berga30e3112010-09-22 18:02:01 +02001206
Johannes Bergb1eea292012-03-06 13:30:42 -08001207 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001208
1209 ctx->key_mapping_keys--;
1210
Johannes Berga30e3112010-09-22 18:02:01 +02001211 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1212 keyconf->keyidx, sta_id);
1213
Johannes Berg5a3d9882011-07-15 13:03:12 -07001214 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1215 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1216 keyconf->hw_key_idx);
Johannes Berga30e3112010-09-22 18:02:01 +02001217
Johannes Berg5dcbf482012-02-17 09:47:14 -08001218 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1219 key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1220 STA_KEY_FLG_INVALID;
1221
1222 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1223 key_flags |= STA_KEY_MULTICAST_MSK;
1224
1225 sta_cmd.key.key_flags = key_flags;
Emmanuel Grumbach5ca732f2012-06-06 09:13:36 +02001226 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001227 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1228 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001229
1230 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1231}
1232
Johannes Berg5a3d9882011-07-15 13:03:12 -07001233int iwl_set_dynamic_key(struct iwl_priv *priv,
1234 struct iwl_rxon_context *ctx,
1235 struct ieee80211_key_conf *keyconf,
1236 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001237{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001238 struct ieee80211_key_seq seq;
1239 u16 p1k[5];
Johannes Berga30e3112010-09-22 18:02:01 +02001240 int ret;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001241 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1242 const u8 *addr;
1243
1244 if (sta_id == IWL_INVALID_STATION)
1245 return -EINVAL;
Johannes Berga30e3112010-09-22 18:02:01 +02001246
Johannes Bergb1eea292012-03-06 13:30:42 -08001247 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001248
Johannes Berg5a3d9882011-07-15 13:03:12 -07001249 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1250 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1251 return -ENOSPC;
1252
Johannes Berga30e3112010-09-22 18:02:01 +02001253 ctx->key_mapping_keys++;
Johannes Berga30e3112010-09-22 18:02:01 +02001254
1255 switch (keyconf->cipher) {
Johannes Berga30e3112010-09-22 18:02:01 +02001256 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001257 if (sta)
1258 addr = sta->addr;
1259 else /* station mode case only */
1260 addr = ctx->active.bssid_addr;
1261
1262 /* pre-fill phase 1 key into device cache */
1263 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1264 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Berge1b1c082011-07-17 01:44:11 -07001265 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1266 seq.tkip.iv32, p1k, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001267 break;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001268 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berga30e3112010-09-22 18:02:01 +02001269 case WLAN_CIPHER_SUITE_WEP40:
1270 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge1b1c082011-07-17 01:44:11 -07001271 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1272 0, NULL, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001273 break;
1274 default:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001275 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
Johannes Berga30e3112010-09-22 18:02:01 +02001276 ret = -EINVAL;
1277 }
1278
Johannes Berg5a3d9882011-07-15 13:03:12 -07001279 if (ret) {
1280 ctx->key_mapping_keys--;
1281 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1282 }
1283
1284 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 +02001285 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001286 sta ? sta->addr : NULL, ret);
Johannes Berga30e3112010-09-22 18:02:01 +02001287
1288 return ret;
1289}
1290
1291/**
1292 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1293 *
1294 * This adds the broadcast station into the driver's station table
1295 * and marks it driver active, so that it will be restored to the
1296 * device at the next best time.
1297 */
1298int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1299 struct iwl_rxon_context *ctx)
1300{
1301 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +02001302 u8 sta_id;
1303
Johannes Bergfa23cb02012-03-05 11:24:25 -08001304 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001305 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1306 if (sta_id == IWL_INVALID_STATION) {
1307 IWL_ERR(priv, "Unable to prepare broadcast station\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -08001308 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001309
1310 return -EINVAL;
1311 }
1312
1313 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1314 priv->stations[sta_id].used |= IWL_STA_BCAST;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001315 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001316
Johannes Bergf775aa02011-06-22 06:34:09 -07001317 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001318 if (!link_cmd) {
1319 IWL_ERR(priv,
1320 "Unable to initialize rate scaling for bcast station.\n");
1321 return -ENOMEM;
1322 }
1323
Johannes Bergfa23cb02012-03-05 11:24:25 -08001324 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001325 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001326 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001327
1328 return 0;
1329}
1330
1331/**
1332 * iwl_update_bcast_station - update broadcast station's LQ command
1333 *
1334 * Only used by iwlagn. Placed here to have all bcast station management
1335 * code together.
1336 */
Johannes Bergf775aa02011-06-22 06:34:09 -07001337int iwl_update_bcast_station(struct iwl_priv *priv,
1338 struct iwl_rxon_context *ctx)
Johannes Berga30e3112010-09-22 18:02:01 +02001339{
Johannes Berga30e3112010-09-22 18:02:01 +02001340 struct iwl_link_quality_cmd *link_cmd;
1341 u8 sta_id = ctx->bcast_sta_id;
1342
Johannes Bergf775aa02011-06-22 06:34:09 -07001343 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001344 if (!link_cmd) {
1345 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1346 return -ENOMEM;
1347 }
1348
Johannes Bergfa23cb02012-03-05 11:24:25 -08001349 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001350 if (priv->stations[sta_id].lq)
1351 kfree(priv->stations[sta_id].lq);
1352 else
1353 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1354 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001355 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001356
1357 return 0;
1358}
1359
1360int iwl_update_bcast_stations(struct iwl_priv *priv)
1361{
1362 struct iwl_rxon_context *ctx;
1363 int ret = 0;
1364
1365 for_each_context(priv, ctx) {
1366 ret = iwl_update_bcast_station(priv, ctx);
1367 if (ret)
1368 break;
1369 }
1370
1371 return ret;
1372}
1373
1374/**
1375 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1376 */
1377int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1378{
Johannes Berga30e3112010-09-22 18:02:01 +02001379 struct iwl_addsta_cmd sta_cmd;
1380
Johannes Bergb1eea292012-03-06 13:30:42 -08001381 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001382
1383 /* Remove "disable" flag, to enable Tx for this TID */
Johannes Bergfa23cb02012-03-05 11:24:25 -08001384 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001385 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1386 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1387 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1388 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001389 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001390
1391 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1392}
1393
1394int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1395 int tid, u16 ssn)
1396{
Johannes Berga30e3112010-09-22 18:02:01 +02001397 int sta_id;
1398 struct iwl_addsta_cmd sta_cmd;
1399
Johannes Bergb1eea292012-03-06 13:30:42 -08001400 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001401
1402 sta_id = iwl_sta_id(sta);
1403 if (sta_id == IWL_INVALID_STATION)
1404 return -ENXIO;
1405
Johannes Bergfa23cb02012-03-05 11:24:25 -08001406 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001407 priv->stations[sta_id].sta.station_flags_msk = 0;
1408 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1409 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1410 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1411 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1412 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001413 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001414
1415 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1416}
1417
1418int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1419 int tid)
1420{
Johannes Berga30e3112010-09-22 18:02:01 +02001421 int sta_id;
1422 struct iwl_addsta_cmd sta_cmd;
1423
Johannes Bergb1eea292012-03-06 13:30:42 -08001424 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001425
1426 sta_id = iwl_sta_id(sta);
1427 if (sta_id == IWL_INVALID_STATION) {
1428 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1429 return -ENXIO;
1430 }
1431
Johannes Bergfa23cb02012-03-05 11:24:25 -08001432 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001433 priv->stations[sta_id].sta.station_flags_msk = 0;
1434 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1435 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1436 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1437 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001438 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001439
1440 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1441}
1442
Johannes Berga30e3112010-09-22 18:02:01 +02001443
Johannes Berga30e3112010-09-22 18:02:01 +02001444
1445void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1446{
Johannes Berg9451ca12012-03-05 11:24:23 -08001447 struct iwl_addsta_cmd cmd = {
1448 .mode = STA_CONTROL_MODIFY_MSK,
1449 .station_flags = STA_FLG_PWR_SAVE_MSK,
1450 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1451 .sta.sta_id = sta_id,
1452 .sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1453 .sleep_tx_count = cpu_to_le16(cnt),
1454 };
Johannes Berga30e3112010-09-22 18:02:01 +02001455
Johannes Berg9451ca12012-03-05 11:24:23 -08001456 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001457}