blob: 42af9f94d7d98aba646e69cafc06c69317635b51 [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
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700173static bool iwl_is_channel_extension(struct iwl_priv *priv,
174 enum ieee80211_band band,
175 u16 channel, u8 extension_chan_offset)
176{
177 const struct iwl_channel_info *ch_info;
178
179 ch_info = iwl_get_channel_info(priv, band, channel);
180 if (!is_channel_valid(ch_info))
181 return false;
182
183 if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
184 return !(ch_info->ht40_extension_channel &
185 IEEE80211_CHAN_NO_HT40PLUS);
186 else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
187 return !(ch_info->ht40_extension_channel &
188 IEEE80211_CHAN_NO_HT40MINUS);
189
190 return false;
191}
192
193bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
194 struct iwl_rxon_context *ctx,
195 struct ieee80211_sta_ht_cap *ht_cap)
196{
197 if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
198 return false;
199
200 /*
201 * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
202 * the bit will not set if it is pure 40MHz case
203 */
204 if (ht_cap && !ht_cap->ht_supported)
205 return false;
206
207#ifdef CONFIG_IWLWIFI_DEBUGFS
208 if (priv->disable_ht40)
209 return false;
210#endif
211
212 return iwl_is_channel_extension(priv, priv->band,
213 le16_to_cpu(ctx->staging.channel),
214 ctx->ht.extension_chan_offset);
215}
216
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800217static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
218 struct ieee80211_sta *sta,
219 struct iwl_rxon_context *ctx,
220 __le32 *flags, __le32 *mask)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700221{
222 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700223 u8 mimo_ps_mode;
224
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800225 *mask = STA_FLG_RTS_MIMO_PROT_MSK |
226 STA_FLG_MIMO_DIS_MSK |
227 STA_FLG_HT40_EN_MSK |
228 STA_FLG_MAX_AGG_SIZE_MSK |
229 STA_FLG_AGG_MPDU_DENSITY_MSK;
230 *flags = 0;
231
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700232 if (!sta || !sta_ht_inf->ht_supported)
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800233 return;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700234
235 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800236
237 IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700238 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
239 "static" :
240 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
241 "dynamic" : "disabled");
242
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700243 switch (mimo_ps_mode) {
244 case WLAN_HT_CAP_SM_PS_STATIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800245 *flags |= STA_FLG_MIMO_DIS_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700246 break;
247 case WLAN_HT_CAP_SM_PS_DYNAMIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800248 *flags |= STA_FLG_RTS_MIMO_PROT_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700249 break;
250 case WLAN_HT_CAP_SM_PS_DISABLED:
251 break;
252 default:
253 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
254 break;
255 }
256
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800257 *flags |= cpu_to_le32(
258 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700259
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800260 *flags |= cpu_to_le32(
261 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700262
263 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800264 *flags |= STA_FLG_HT40_EN_MSK;
265}
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700266
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800267int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
268 struct ieee80211_sta *sta)
269{
270 u8 sta_id = iwl_sta_id(sta);
271 __le32 flags, mask;
272 struct iwl_addsta_cmd cmd;
273
274 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
275 return -EINVAL;
276
277 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
278
279 spin_lock_bh(&priv->sta_lock);
280 priv->stations[sta_id].sta.station_flags &= ~mask;
281 priv->stations[sta_id].sta.station_flags |= flags;
282 spin_unlock_bh(&priv->sta_lock);
283
284 memset(&cmd, 0, sizeof(cmd));
285 cmd.mode = STA_CONTROL_MODIFY_MSK;
286 cmd.station_flags_msk = mask;
287 cmd.station_flags = flags;
288 cmd.sta.sta_id = sta_id;
289
290 return iwl_send_add_sta(priv, &cmd, CMD_SYNC);
291}
292
293static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
294 struct ieee80211_sta *sta,
295 struct iwl_rxon_context *ctx)
296{
297 __le32 flags, mask;
298
299 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
300
301 lockdep_assert_held(&priv->sta_lock);
302 priv->stations[index].sta.station_flags &= ~mask;
303 priv->stations[index].sta.station_flags |= flags;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700304}
305
306/**
307 * iwl_prep_station - Prepare station information for addition
308 *
309 * should be called with sta_lock held
310 */
311u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
312 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
313{
314 struct iwl_station_entry *station;
315 int i;
316 u8 sta_id = IWL_INVALID_STATION;
317
318 if (is_ap)
319 sta_id = ctx->ap_sta_id;
320 else if (is_broadcast_ether_addr(addr))
321 sta_id = ctx->bcast_sta_id;
322 else
323 for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
324 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
325 addr)) {
326 sta_id = i;
327 break;
328 }
329
330 if (!priv->stations[i].used &&
331 sta_id == IWL_INVALID_STATION)
332 sta_id = i;
333 }
334
335 /*
336 * These two conditions have the same outcome, but keep them
337 * separate
338 */
339 if (unlikely(sta_id == IWL_INVALID_STATION))
340 return sta_id;
341
342 /*
343 * uCode is not able to deal with multiple requests to add a
344 * station. Keep track if one is in progress so that we do not send
345 * another.
346 */
347 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
348 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
349 "added.\n", sta_id);
350 return sta_id;
351 }
352
353 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
354 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
355 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
356 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
357 "adding again.\n", sta_id, addr);
358 return sta_id;
359 }
360
361 station = &priv->stations[sta_id];
362 station->used = IWL_STA_DRIVER_ACTIVE;
363 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
364 sta_id, addr);
365 priv->num_stations++;
366
367 /* Set up the REPLY_ADD_STA command to send to device */
368 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
369 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
370 station->sta.mode = 0;
371 station->sta.sta.sta_id = sta_id;
372 station->sta.station_flags = ctx->station_flags;
373 station->ctxid = ctx->ctxid;
374
375 if (sta) {
376 struct iwl_station_priv *sta_priv;
377
378 sta_priv = (void *)sta->drv_priv;
379 sta_priv->ctx = ctx;
380 }
381
382 /*
383 * OK to call unconditionally, since local stations (IBSS BSSID
384 * STA and broadcast STA) pass in a NULL sta, and mac80211
385 * doesn't allow HT IBSS.
386 */
387 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
388
389 return sta_id;
390
391}
392
393#define STA_WAIT_TIMEOUT (HZ/2)
394
395/**
396 * iwl_add_station_common -
397 */
398int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
399 const u8 *addr, bool is_ap,
400 struct ieee80211_sta *sta, u8 *sta_id_r)
401{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700402 int ret = 0;
403 u8 sta_id;
404 struct iwl_addsta_cmd sta_cmd;
405
406 *sta_id_r = 0;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800407 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700408 sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
409 if (sta_id == IWL_INVALID_STATION) {
410 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
411 addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800412 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700413 return -EINVAL;
414 }
415
416 /*
417 * uCode is not able to deal with multiple requests to add a
418 * station. Keep track if one is in progress so that we do not send
419 * another.
420 */
421 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
422 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
423 "added.\n", sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800424 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700425 return -EEXIST;
426 }
427
428 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
429 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
430 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
431 "adding again.\n", sta_id, addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800432 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700433 return -EEXIST;
434 }
435
436 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
437 memcpy(&sta_cmd, &priv->stations[sta_id].sta,
438 sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -0800439 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700440
441 /* Add station to device's station table */
442 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
443 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800444 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700445 IWL_ERR(priv, "Adding station %pM failed.\n",
446 priv->stations[sta_id].sta.sta.addr);
447 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
448 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800449 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700450 }
451 *sta_id_r = sta_id;
452 return ret;
453}
454
455/**
456 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700457 */
458static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
459{
Johannes Bergfa23cb02012-03-05 11:24:25 -0800460 lockdep_assert_held(&priv->sta_lock);
461
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700462 /* Ucode must be active and driver must be non active */
463 if ((priv->stations[sta_id].used &
464 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
465 IWL_STA_UCODE_ACTIVE)
466 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
467
468 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
469
470 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
471 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
472}
473
474static int iwl_send_remove_station(struct iwl_priv *priv,
475 const u8 *addr, int sta_id,
476 bool temporary)
477{
478 struct iwl_rx_packet *pkt;
479 int ret;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700480 struct iwl_rem_sta_cmd rm_sta_cmd;
481
482 struct iwl_host_cmd cmd = {
483 .id = REPLY_REMOVE_STA,
484 .len = { sizeof(struct iwl_rem_sta_cmd), },
485 .flags = CMD_SYNC,
486 .data = { &rm_sta_cmd, },
487 };
488
489 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
490 rm_sta_cmd.num_sta = 1;
491 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
492
493 cmd.flags |= CMD_WANT_SKB;
494
Johannes Berge10a0532012-03-06 13:30:39 -0800495 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700496
497 if (ret)
498 return ret;
499
Johannes Berg65b94a42012-03-05 11:24:38 -0800500 pkt = cmd.resp_pkt;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700501 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
502 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
503 pkt->hdr.flags);
504 ret = -EIO;
505 }
506
507 if (!ret) {
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800508 struct iwl_rem_sta_resp *rem_sta_resp = (void *)pkt->data;
509 switch (rem_sta_resp->status) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700510 case REM_STA_SUCCESS_MSK:
511 if (!temporary) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800512 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700513 iwl_sta_ucode_deactivate(priv, sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800514 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700515 }
516 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
517 break;
518 default:
519 ret = -EIO;
520 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
521 break;
522 }
523 }
Johannes Berg65b94a42012-03-05 11:24:38 -0800524 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700525
526 return ret;
527}
528
529/**
530 * iwl_remove_station - Remove driver's knowledge of station.
531 */
532int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
533 const u8 *addr)
534{
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200535 u8 tid;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700536
Don Fry83626402012-03-07 09:52:37 -0800537 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700538 IWL_DEBUG_INFO(priv,
539 "Unable to remove station %pM, device not ready.\n",
540 addr);
541 /*
542 * It is typical for stations to be removed when we are
543 * going down. Return success since device will be down
544 * soon anyway
545 */
546 return 0;
547 }
548
549 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
550 sta_id, addr);
551
552 if (WARN_ON(sta_id == IWL_INVALID_STATION))
553 return -EINVAL;
554
Johannes Bergfa23cb02012-03-05 11:24:25 -0800555 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700556
557 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
558 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
559 addr);
560 goto out_err;
561 }
562
563 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
564 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
565 addr);
566 goto out_err;
567 }
568
569 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
570 kfree(priv->stations[sta_id].lq);
571 priv->stations[sta_id].lq = NULL;
572 }
573
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200574 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
575 memset(&priv->tid_data[sta_id][tid], 0,
576 sizeof(priv->tid_data[sta_id][tid]));
577
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700578 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
579
580 priv->num_stations--;
581
582 if (WARN_ON(priv->num_stations < 0))
583 priv->num_stations = 0;
584
Johannes Bergfa23cb02012-03-05 11:24:25 -0800585 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700586
587 return iwl_send_remove_station(priv, addr, sta_id, false);
588out_err:
Johannes Bergfa23cb02012-03-05 11:24:25 -0800589 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700590 return -EINVAL;
591}
592
Johannes Berg97420512012-03-07 09:52:42 -0800593void iwl_deactivate_station(struct iwl_priv *priv, const u8 sta_id,
594 const u8 *addr)
595{
596 u8 tid;
597
598 if (!iwl_is_ready(priv)) {
599 IWL_DEBUG_INFO(priv,
600 "Unable to remove station %pM, device not ready.\n",
601 addr);
602 return;
603 }
604
605 IWL_DEBUG_ASSOC(priv, "Deactivating STA: %pM (%d)\n", addr, sta_id);
606
607 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
608 return;
609
610 spin_lock_bh(&priv->sta_lock);
611
612 WARN_ON_ONCE(!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE));
613
614 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
615 memset(&priv->tid_data[sta_id][tid], 0,
616 sizeof(priv->tid_data[sta_id][tid]));
617
618 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
619
620 priv->num_stations--;
621
622 if (WARN_ON_ONCE(priv->num_stations < 0))
623 priv->num_stations = 0;
624
625 spin_unlock_bh(&priv->sta_lock);
626}
627
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100628static void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
629 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
630{
631 int i, r;
632 u32 rate_flags = 0;
633 __le32 rate_n_flags;
634
635 lockdep_assert_held(&priv->mutex);
636
637 memset(link_cmd, 0, sizeof(*link_cmd));
638
639 /* Set up the rate scaling to start at selected rate, fall back
640 * all the way down to 1M in IEEE order, and then spin on 1M */
641 if (priv->band == IEEE80211_BAND_5GHZ)
642 r = IWL_RATE_6M_INDEX;
643 else if (ctx && ctx->vif && ctx->vif->p2p)
644 r = IWL_RATE_6M_INDEX;
645 else
646 r = IWL_RATE_1M_INDEX;
647
648 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
649 rate_flags |= RATE_MCS_CCK_MSK;
650
651 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
652 RATE_MCS_ANT_POS;
653 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
654 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
655 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
656
657 link_cmd->general_params.single_stream_ant_msk =
658 first_antenna(priv->hw_params.valid_tx_ant);
659
660 link_cmd->general_params.dual_stream_ant_msk =
661 priv->hw_params.valid_tx_ant &
662 ~first_antenna(priv->hw_params.valid_tx_ant);
663 if (!link_cmd->general_params.dual_stream_ant_msk) {
664 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
665 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
666 link_cmd->general_params.dual_stream_ant_msk =
667 priv->hw_params.valid_tx_ant;
668 }
669
670 link_cmd->agg_params.agg_dis_start_th =
671 LINK_QUAL_AGG_DISABLE_START_DEF;
672 link_cmd->agg_params.agg_time_limit =
673 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
674
675 link_cmd->sta_id = sta_id;
676}
677
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700678/**
679 * iwl_clear_ucode_stations - clear ucode station table bits
680 *
681 * This function clears all the bits in the driver indicating
682 * which stations are active in the ucode. Call when something
683 * other than explicit station management would cause this in
684 * the ucode, e.g. unassociated RXON.
685 */
686void iwl_clear_ucode_stations(struct iwl_priv *priv,
687 struct iwl_rxon_context *ctx)
688{
689 int i;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700690 bool cleared = false;
691
692 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
693
Johannes Bergfa23cb02012-03-05 11:24:25 -0800694 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700695 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
696 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
697 continue;
698
699 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
700 IWL_DEBUG_INFO(priv,
701 "Clearing ucode active for station %d\n", i);
702 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
703 cleared = true;
704 }
705 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800706 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700707
708 if (!cleared)
709 IWL_DEBUG_INFO(priv,
710 "No active stations found to be cleared\n");
711}
712
713/**
714 * iwl_restore_stations() - Restore driver known stations to device
715 *
716 * All stations considered active by driver, but not present in ucode, is
717 * restored.
718 *
719 * Function sleeps.
720 */
721void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
722{
723 struct iwl_addsta_cmd sta_cmd;
724 struct iwl_link_quality_cmd lq;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700725 int i;
726 bool found = false;
727 int ret;
728 bool send_lq;
729
Don Fry83626402012-03-07 09:52:37 -0800730 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700731 IWL_DEBUG_INFO(priv,
732 "Not ready yet, not restoring any stations.\n");
733 return;
734 }
735
736 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -0800737 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700738 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
739 if (ctx->ctxid != priv->stations[i].ctxid)
740 continue;
741 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
742 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
743 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
744 priv->stations[i].sta.sta.addr);
745 priv->stations[i].sta.mode = 0;
746 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
747 found = true;
748 }
749 }
750
751 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
752 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
753 memcpy(&sta_cmd, &priv->stations[i].sta,
754 sizeof(struct iwl_addsta_cmd));
755 send_lq = false;
756 if (priv->stations[i].lq) {
Johannes Berg15b86bf2012-03-05 11:24:36 -0800757 if (priv->wowlan)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700758 iwl_sta_fill_lq(priv, ctx, i, &lq);
759 else
760 memcpy(&lq, priv->stations[i].lq,
761 sizeof(struct iwl_link_quality_cmd));
762 send_lq = true;
763 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800764 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700765 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
766 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800767 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700768 IWL_ERR(priv, "Adding station %pM failed.\n",
769 priv->stations[i].sta.sta.addr);
770 priv->stations[i].used &=
771 ~IWL_STA_DRIVER_ACTIVE;
772 priv->stations[i].used &=
773 ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800774 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700775 }
776 /*
777 * Rate scaling has already been initialized, send
778 * current LQ command
779 */
780 if (send_lq)
781 iwl_send_lq_cmd(priv, ctx, &lq,
782 CMD_SYNC, true);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800783 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700784 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
785 }
786 }
787
Johannes Bergfa23cb02012-03-05 11:24:25 -0800788 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700789 if (!found)
790 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
791 "no stations to be restored.\n");
792 else
793 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
794 "complete.\n");
795}
796
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700797int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
798{
799 int i;
800
801 for (i = 0; i < priv->sta_key_max_num; i++)
802 if (!test_and_set_bit(i, &priv->ucode_key_table))
803 return i;
804
805 return WEP_INVALID_OFFSET;
806}
807
808void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
809{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700810 int i;
811
Johannes Bergfa23cb02012-03-05 11:24:25 -0800812 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700813 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
814 if (!(priv->stations[i].used & IWL_STA_BCAST))
815 continue;
816
817 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
818 priv->num_stations--;
819 if (WARN_ON(priv->num_stations < 0))
820 priv->num_stations = 0;
821 kfree(priv->stations[i].lq);
822 priv->stations[i].lq = NULL;
823 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800824 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700825}
826
827#ifdef CONFIG_IWLWIFI_DEBUG
828static void iwl_dump_lq_cmd(struct iwl_priv *priv,
829 struct iwl_link_quality_cmd *lq)
830{
831 int i;
832 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
833 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
834 lq->general_params.single_stream_ant_msk,
835 lq->general_params.dual_stream_ant_msk);
836
837 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
838 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
839 i, lq->rs_table[i].rate_n_flags);
840}
841#else
842static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
843 struct iwl_link_quality_cmd *lq)
844{
845}
846#endif
847
848/**
849 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
850 *
851 * It sometimes happens when a HT rate has been in use and we
852 * loose connectivity with AP then mac80211 will first tell us that the
853 * current channel is not HT anymore before removing the station. In such a
854 * scenario the RXON flags will be updated to indicate we are not
855 * communicating HT anymore, but the LQ command may still contain HT rates.
856 * Test for this to prevent driver from sending LQ command between the time
857 * RXON flags are updated and when LQ command is updated.
858 */
859static bool is_lq_table_valid(struct iwl_priv *priv,
860 struct iwl_rxon_context *ctx,
861 struct iwl_link_quality_cmd *lq)
862{
863 int i;
864
865 if (ctx->ht.enabled)
866 return true;
867
868 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
869 ctx->active.channel);
870 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
871 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
872 RATE_MCS_HT_MSK) {
873 IWL_DEBUG_INFO(priv,
874 "index %d of LQ expects HT channel\n",
875 i);
876 return false;
877 }
878 }
879 return true;
880}
881
882/**
883 * iwl_send_lq_cmd() - Send link quality command
884 * @init: This command is sent as part of station initialization right
885 * after station has been added.
886 *
887 * The link quality command is sent as the last step of station creation.
888 * This is the special case in which init is set and we call a callback in
889 * this case to clear the state indicating that station creation is in
890 * progress.
891 */
892int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
893 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
894{
895 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700896 struct iwl_host_cmd cmd = {
897 .id = REPLY_TX_LINK_QUALITY_CMD,
898 .len = { sizeof(struct iwl_link_quality_cmd), },
899 .flags = flags,
900 .data = { lq, },
901 };
902
903 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
904 return -EINVAL;
905
906
Johannes Bergfa23cb02012-03-05 11:24:25 -0800907 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700908 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800909 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700910 return -EINVAL;
911 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800912 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700913
914 iwl_dump_lq_cmd(priv, lq);
915 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
916 return -EINVAL;
917
918 if (is_lq_table_valid(priv, ctx, lq))
Johannes Berge10a0532012-03-06 13:30:39 -0800919 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700920 else
921 ret = -EINVAL;
922
923 if (cmd.flags & CMD_ASYNC)
924 return ret;
925
926 if (init) {
927 IWL_DEBUG_INFO(priv, "init LQ command complete, "
928 "clearing sta addition status for sta %d\n",
929 lq->sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800930 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700931 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800932 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700933 }
934 return ret;
935}
936
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700937
Johannes Bergc079166e2011-10-10 07:26:54 -0700938static struct iwl_link_quality_cmd *
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700939iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
940 u8 sta_id)
Johannes Bergc079166e2011-10-10 07:26:54 -0700941{
942 struct iwl_link_quality_cmd *link_cmd;
943
944 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
945 if (!link_cmd) {
946 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
947 return NULL;
948 }
949
950 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200951
952 return link_cmd;
953}
954
955/*
956 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
957 *
958 * Function sleeps.
959 */
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700960int iwlagn_add_bssid_station(struct iwl_priv *priv,
961 struct iwl_rxon_context *ctx,
Johannes Berga30e3112010-09-22 18:02:01 +0200962 const u8 *addr, u8 *sta_id_r)
963{
964 int ret;
965 u8 sta_id;
966 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +0200967
968 if (sta_id_r)
969 *sta_id_r = IWL_INVALID_STATION;
970
971 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
972 if (ret) {
973 IWL_ERR(priv, "Unable to add station %pM\n", addr);
974 return ret;
975 }
976
977 if (sta_id_r)
978 *sta_id_r = sta_id;
979
Johannes Bergfa23cb02012-03-05 11:24:25 -0800980 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200981 priv->stations[sta_id].used |= IWL_STA_LOCAL;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800982 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200983
984 /* Set up default rate scaling table in device's station table */
Johannes Bergf775aa06d2011-06-22 06:34:09 -0700985 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200986 if (!link_cmd) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700987 IWL_ERR(priv,
988 "Unable to initialize rate scaling for station %pM.\n",
Johannes Berga30e3112010-09-22 18:02:01 +0200989 addr);
990 return -ENOMEM;
991 }
992
993 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
994 if (ret)
995 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
996
Johannes Bergfa23cb02012-03-05 11:24:25 -0800997 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200998 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800999 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001000
1001 return 0;
1002}
1003
Johannes Berg5a3d9882011-07-15 13:03:12 -07001004/*
1005 * static WEP keys
1006 *
1007 * For each context, the device has a table of 4 static WEP keys
1008 * (one for each key index) that is updated with the following
1009 * commands.
1010 */
1011
Johannes Berga30e3112010-09-22 18:02:01 +02001012static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
1013 struct iwl_rxon_context *ctx,
1014 bool send_if_empty)
1015{
1016 int i, not_empty = 0;
1017 u8 buff[sizeof(struct iwl_wep_cmd) +
1018 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
1019 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
1020 size_t cmd_size = sizeof(struct iwl_wep_cmd);
1021 struct iwl_host_cmd cmd = {
1022 .id = ctx->wep_key_cmd,
Johannes Berg3fa50732011-05-04 07:50:38 -07001023 .data = { wep_cmd, },
Johannes Berga30e3112010-09-22 18:02:01 +02001024 .flags = CMD_SYNC,
1025 };
1026
1027 might_sleep();
1028
1029 memset(wep_cmd, 0, cmd_size +
1030 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
1031
1032 for (i = 0; i < WEP_KEYS_MAX ; i++) {
1033 wep_cmd->key[i].key_index = i;
1034 if (ctx->wep_keys[i].key_size) {
1035 wep_cmd->key[i].key_offset = i;
1036 not_empty = 1;
1037 } else {
1038 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
1039 }
1040
1041 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
1042 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
1043 ctx->wep_keys[i].key_size);
1044 }
1045
1046 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
1047 wep_cmd->num_keys = WEP_KEYS_MAX;
1048
1049 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
1050
Johannes Berg3fa50732011-05-04 07:50:38 -07001051 cmd.len[0] = cmd_size;
Johannes Berga30e3112010-09-22 18:02:01 +02001052
1053 if (not_empty || send_if_empty)
Johannes Berge10a0532012-03-06 13:30:39 -08001054 return iwl_dvm_send_cmd(priv, &cmd);
Johannes Berga30e3112010-09-22 18:02:01 +02001055 else
1056 return 0;
1057}
1058
1059int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1060 struct iwl_rxon_context *ctx)
1061{
Johannes Bergb1eea292012-03-06 13:30:42 -08001062 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001063
1064 return iwl_send_static_wepkey_cmd(priv, ctx, false);
1065}
1066
1067int iwl_remove_default_wep_key(struct iwl_priv *priv,
1068 struct iwl_rxon_context *ctx,
1069 struct ieee80211_key_conf *keyconf)
1070{
1071 int ret;
1072
Johannes Bergb1eea292012-03-06 13:30:42 -08001073 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001074
1075 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1076 keyconf->keyidx);
1077
1078 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
Don Fry83626402012-03-07 09:52:37 -08001079 if (iwl_is_rfkill(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001080 IWL_DEBUG_WEP(priv,
1081 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berga30e3112010-09-22 18:02:01 +02001082 /* but keys in device are clear anyway so return success */
1083 return 0;
1084 }
1085 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1086 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1087 keyconf->keyidx, ret);
1088
1089 return ret;
1090}
1091
1092int iwl_set_default_wep_key(struct iwl_priv *priv,
1093 struct iwl_rxon_context *ctx,
1094 struct ieee80211_key_conf *keyconf)
1095{
1096 int ret;
1097
Johannes Bergb1eea292012-03-06 13:30:42 -08001098 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001099
1100 if (keyconf->keylen != WEP_KEY_LEN_128 &&
1101 keyconf->keylen != WEP_KEY_LEN_64) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001102 IWL_DEBUG_WEP(priv,
1103 "Bad WEP key length %d\n", keyconf->keylen);
Johannes Berga30e3112010-09-22 18:02:01 +02001104 return -EINVAL;
1105 }
1106
Johannes Berg5a3d9882011-07-15 13:03:12 -07001107 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
Johannes Berga30e3112010-09-22 18:02:01 +02001108
1109 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1110 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1111 keyconf->keylen);
1112
1113 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1114 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1115 keyconf->keylen, keyconf->keyidx, ret);
1116
1117 return ret;
1118}
1119
Johannes Berg5a3d9882011-07-15 13:03:12 -07001120/*
1121 * dynamic (per-station) keys
1122 *
1123 * The dynamic keys are a little more complicated. The device has
1124 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1125 * These are linked to stations by a table that contains an index
1126 * into the key table for each station/key index/{mcast,unicast},
1127 * i.e. it's basically an array of pointers like this:
1128 * key_offset_t key_mapping[NUM_STATIONS][4][2];
1129 * (it really works differently, but you can think of it as such)
1130 *
1131 * The key uploading and linking happens in the same command, the
1132 * add station command with STA_MODIFY_KEY_MASK.
1133 */
1134
1135static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1136 struct ieee80211_vif *vif,
1137 struct ieee80211_sta *sta)
1138{
1139 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001140
1141 if (sta)
Johannes Berg40e4e682012-03-05 11:24:22 -08001142 return iwl_sta_id(sta);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001143
1144 /*
1145 * The device expects GTKs for station interfaces to be
1146 * installed as GTKs for the AP station. If we have no
1147 * station ID, then use the ap_sta_id in that case.
1148 */
Johannes Berg40e4e682012-03-05 11:24:22 -08001149 if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1150 return vif_priv->ctx->ap_sta_id;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001151
Johannes Berg40e4e682012-03-05 11:24:22 -08001152 return IWL_INVALID_STATION;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001153}
1154
Johannes Berge1b1c082011-07-17 01:44:11 -07001155static int iwlagn_send_sta_key(struct iwl_priv *priv,
1156 struct ieee80211_key_conf *keyconf,
1157 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1158 u32 cmd_flags)
Johannes Berga30e3112010-09-22 18:02:01 +02001159{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001160 __le16 key_flags;
Johannes Berga30e3112010-09-22 18:02:01 +02001161 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001162 int i;
Johannes Berge1b1c082011-07-17 01:44:11 -07001163
Johannes Bergfa23cb02012-03-05 11:24:25 -08001164 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001165 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001166 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001167
Johannes Berg5a3d9882011-07-15 13:03:12 -07001168 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1169 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001170
Johannes Berg5a3d9882011-07-15 13:03:12 -07001171 switch (keyconf->cipher) {
1172 case WLAN_CIPHER_SUITE_CCMP:
1173 key_flags |= STA_KEY_FLG_CCMP;
1174 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1175 break;
1176 case WLAN_CIPHER_SUITE_TKIP:
1177 key_flags |= STA_KEY_FLG_TKIP;
1178 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1179 for (i = 0; i < 5; i++)
1180 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1181 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1182 break;
1183 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga30e3112010-09-22 18:02:01 +02001184 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001185 /* fall through */
1186 case WLAN_CIPHER_SUITE_WEP40:
1187 key_flags |= STA_KEY_FLG_WEP;
1188 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1189 break;
1190 default:
1191 WARN_ON(1);
1192 return -EINVAL;
1193 }
Johannes Berga30e3112010-09-22 18:02:01 +02001194
Johannes Berg5a3d9882011-07-15 13:03:12 -07001195 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
Johannes Berga30e3112010-09-22 18:02:01 +02001196 key_flags |= STA_KEY_MULTICAST_MSK;
1197
Johannes Berg5a3d9882011-07-15 13:03:12 -07001198 /* key pointer (offset) */
1199 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berga30e3112010-09-22 18:02:01 +02001200
Johannes Berg5a3d9882011-07-15 13:03:12 -07001201 sta_cmd.key.key_flags = key_flags;
1202 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1203 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
Johannes Berga30e3112010-09-22 18:02:01 +02001204
Johannes Berg5a3d9882011-07-15 13:03:12 -07001205 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001206}
1207
1208void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001209 struct ieee80211_vif *vif,
Johannes Berga30e3112010-09-22 18:02:01 +02001210 struct ieee80211_key_conf *keyconf,
1211 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1212{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001213 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1214
1215 if (sta_id == IWL_INVALID_STATION)
1216 return;
Johannes Berga30e3112010-09-22 18:02:01 +02001217
1218 if (iwl_scan_cancel(priv)) {
1219 /* cancel scan failed, just live w/ bad key and rely
1220 briefly on SW decryption */
1221 return;
1222 }
1223
Johannes Berge1b1c082011-07-17 01:44:11 -07001224 iwlagn_send_sta_key(priv, keyconf, sta_id,
1225 iv32, phase1key, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001226}
1227
1228int iwl_remove_dynamic_key(struct iwl_priv *priv,
1229 struct iwl_rxon_context *ctx,
1230 struct ieee80211_key_conf *keyconf,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001231 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001232{
Johannes Berga30e3112010-09-22 18:02:01 +02001233 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001234 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
Johannes Berg5dcbf482012-02-17 09:47:14 -08001235 __le16 key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001236
1237 /* if station isn't there, neither is the key */
1238 if (sta_id == IWL_INVALID_STATION)
1239 return -ENOENT;
1240
Johannes Bergfa23cb02012-03-05 11:24:25 -08001241 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001242 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1243 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1244 sta_id = IWL_INVALID_STATION;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001245 spin_unlock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001246
1247 if (sta_id == IWL_INVALID_STATION)
1248 return 0;
Johannes Berga30e3112010-09-22 18:02:01 +02001249
Johannes Bergb1eea292012-03-06 13:30:42 -08001250 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001251
1252 ctx->key_mapping_keys--;
1253
Johannes Berga30e3112010-09-22 18:02:01 +02001254 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1255 keyconf->keyidx, sta_id);
1256
Johannes Berg5a3d9882011-07-15 13:03:12 -07001257 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1258 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1259 keyconf->hw_key_idx);
Johannes Berga30e3112010-09-22 18:02:01 +02001260
Johannes Berg5dcbf482012-02-17 09:47:14 -08001261 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1262 key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1263 STA_KEY_FLG_INVALID;
1264
1265 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1266 key_flags |= STA_KEY_MULTICAST_MSK;
1267
1268 sta_cmd.key.key_flags = key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001269 sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
1270 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1271 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001272
1273 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1274}
1275
Johannes Berg5a3d9882011-07-15 13:03:12 -07001276int iwl_set_dynamic_key(struct iwl_priv *priv,
1277 struct iwl_rxon_context *ctx,
1278 struct ieee80211_key_conf *keyconf,
1279 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001280{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001281 struct ieee80211_key_seq seq;
1282 u16 p1k[5];
Johannes Berga30e3112010-09-22 18:02:01 +02001283 int ret;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001284 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1285 const u8 *addr;
1286
1287 if (sta_id == IWL_INVALID_STATION)
1288 return -EINVAL;
Johannes Berga30e3112010-09-22 18:02:01 +02001289
Johannes Bergb1eea292012-03-06 13:30:42 -08001290 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001291
Johannes Berg5a3d9882011-07-15 13:03:12 -07001292 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1293 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1294 return -ENOSPC;
1295
Johannes Berga30e3112010-09-22 18:02:01 +02001296 ctx->key_mapping_keys++;
Johannes Berga30e3112010-09-22 18:02:01 +02001297
1298 switch (keyconf->cipher) {
Johannes Berga30e3112010-09-22 18:02:01 +02001299 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001300 if (sta)
1301 addr = sta->addr;
1302 else /* station mode case only */
1303 addr = ctx->active.bssid_addr;
1304
1305 /* pre-fill phase 1 key into device cache */
1306 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1307 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Berge1b1c082011-07-17 01:44:11 -07001308 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1309 seq.tkip.iv32, p1k, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001310 break;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001311 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berga30e3112010-09-22 18:02:01 +02001312 case WLAN_CIPHER_SUITE_WEP40:
1313 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge1b1c082011-07-17 01:44:11 -07001314 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1315 0, NULL, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001316 break;
1317 default:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001318 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
Johannes Berga30e3112010-09-22 18:02:01 +02001319 ret = -EINVAL;
1320 }
1321
Johannes Berg5a3d9882011-07-15 13:03:12 -07001322 if (ret) {
1323 ctx->key_mapping_keys--;
1324 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1325 }
1326
1327 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 +02001328 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001329 sta ? sta->addr : NULL, ret);
Johannes Berga30e3112010-09-22 18:02:01 +02001330
1331 return ret;
1332}
1333
1334/**
1335 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1336 *
1337 * This adds the broadcast station into the driver's station table
1338 * and marks it driver active, so that it will be restored to the
1339 * device at the next best time.
1340 */
1341int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1342 struct iwl_rxon_context *ctx)
1343{
1344 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +02001345 u8 sta_id;
1346
Johannes Bergfa23cb02012-03-05 11:24:25 -08001347 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001348 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1349 if (sta_id == IWL_INVALID_STATION) {
1350 IWL_ERR(priv, "Unable to prepare broadcast station\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -08001351 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001352
1353 return -EINVAL;
1354 }
1355
1356 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1357 priv->stations[sta_id].used |= IWL_STA_BCAST;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001358 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001359
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001360 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001361 if (!link_cmd) {
1362 IWL_ERR(priv,
1363 "Unable to initialize rate scaling for bcast station.\n");
1364 return -ENOMEM;
1365 }
1366
Johannes Bergfa23cb02012-03-05 11:24:25 -08001367 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001368 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001369 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001370
1371 return 0;
1372}
1373
1374/**
1375 * iwl_update_bcast_station - update broadcast station's LQ command
1376 *
1377 * Only used by iwlagn. Placed here to have all bcast station management
1378 * code together.
1379 */
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001380int iwl_update_bcast_station(struct iwl_priv *priv,
1381 struct iwl_rxon_context *ctx)
Johannes Berga30e3112010-09-22 18:02:01 +02001382{
Johannes Berga30e3112010-09-22 18:02:01 +02001383 struct iwl_link_quality_cmd *link_cmd;
1384 u8 sta_id = ctx->bcast_sta_id;
1385
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001386 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001387 if (!link_cmd) {
1388 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1389 return -ENOMEM;
1390 }
1391
Johannes Bergfa23cb02012-03-05 11:24:25 -08001392 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001393 if (priv->stations[sta_id].lq)
1394 kfree(priv->stations[sta_id].lq);
1395 else
1396 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1397 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001398 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001399
1400 return 0;
1401}
1402
1403int iwl_update_bcast_stations(struct iwl_priv *priv)
1404{
1405 struct iwl_rxon_context *ctx;
1406 int ret = 0;
1407
1408 for_each_context(priv, ctx) {
1409 ret = iwl_update_bcast_station(priv, ctx);
1410 if (ret)
1411 break;
1412 }
1413
1414 return ret;
1415}
1416
1417/**
1418 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1419 */
1420int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1421{
Johannes Berga30e3112010-09-22 18:02:01 +02001422 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 /* Remove "disable" flag, to enable Tx for this TID */
Johannes Bergfa23cb02012-03-05 11:24:25 -08001427 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001428 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1429 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1430 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1431 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001432 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001433
1434 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1435}
1436
1437int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1438 int tid, u16 ssn)
1439{
Johannes Berga30e3112010-09-22 18:02:01 +02001440 int sta_id;
1441 struct iwl_addsta_cmd sta_cmd;
1442
Johannes Bergb1eea292012-03-06 13:30:42 -08001443 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001444
1445 sta_id = iwl_sta_id(sta);
1446 if (sta_id == IWL_INVALID_STATION)
1447 return -ENXIO;
1448
Johannes Bergfa23cb02012-03-05 11:24:25 -08001449 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001450 priv->stations[sta_id].sta.station_flags_msk = 0;
1451 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1452 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1453 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1454 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1455 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001456 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001457
1458 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1459}
1460
1461int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1462 int tid)
1463{
Johannes Berga30e3112010-09-22 18:02:01 +02001464 int sta_id;
1465 struct iwl_addsta_cmd sta_cmd;
1466
Johannes Bergb1eea292012-03-06 13:30:42 -08001467 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001468
1469 sta_id = iwl_sta_id(sta);
1470 if (sta_id == IWL_INVALID_STATION) {
1471 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1472 return -ENXIO;
1473 }
1474
Johannes Bergfa23cb02012-03-05 11:24:25 -08001475 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001476 priv->stations[sta_id].sta.station_flags_msk = 0;
1477 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1478 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1479 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1480 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001481 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001482
1483 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1484}
1485
Johannes Berga30e3112010-09-22 18:02:01 +02001486
Johannes Berga30e3112010-09-22 18:02:01 +02001487
1488void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1489{
Johannes Berg9451ca12012-03-05 11:24:23 -08001490 struct iwl_addsta_cmd cmd = {
1491 .mode = STA_CONTROL_MODIFY_MSK,
1492 .station_flags = STA_FLG_PWR_SAVE_MSK,
1493 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1494 .sta.sta_id = sta_id,
1495 .sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1496 .sleep_tx_count = cpu_to_le16(cnt),
1497 };
Johannes Berga30e3112010-09-22 18:02:01 +02001498
Johannes Berg9451ca12012-03-05 11:24:23 -08001499 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001500}