blob: 6e1e23eff0dbf812910176bc0c7cf017da00f891 [file] [log] [blame]
Johannes Berga30e3112010-09-22 18:02:01 +02001/******************************************************************************
2 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02003 * Copyright(c) 2003 - 2014 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>
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070031#include "iwl-trans.h"
Johannes Berg1023fdc2012-05-15 12:16:34 +020032#include "dev.h"
33#include "agn.h"
Johannes Berga30e3112010-09-22 18:02:01 +020034
Meenakshi Venkataramanf32cadf2012-03-14 15:59:15 -070035const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
36
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) {
Johannes Berg3c6acb62014-05-07 11:47:53 +020042 IWL_ERR(priv, "invalid sta_id %u\n", sta_id);
Wey-Yi Guy2da424b2012-01-06 13:16:28 -080043 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,
Wey-Yi Guyc745f552011-10-10 07:27:12 -070064 struct iwl_rx_packet *pkt)
65{
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080066 struct iwl_add_sta_resp *add_sta_resp = (void *)pkt->data;
Wey-Yi Guyc745f552011-10-10 07:27:12 -070067
68 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
69 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
70 pkt->hdr.flags);
Johannes Berg4492bb62015-06-23 20:56:45 +020071 return 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -070072 }
73
Johannes Berg121b8002015-06-23 21:01:44 +020074 IWL_DEBUG_INFO(priv, "Processing response for adding station\n");
Wey-Yi Guyc745f552011-10-10 07:27:12 -070075
Johannes Berg2bfb5092012-12-27 21:43:48 +010076 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -070077
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080078 switch (add_sta_resp->status) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -070079 case ADD_STA_SUCCESS_MSK:
80 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
Wey-Yi Guyc745f552011-10-10 07:27:12 -070081 break;
82 case ADD_STA_NO_ROOM_IN_TABLE:
Johannes Berg121b8002015-06-23 21:01:44 +020083 IWL_ERR(priv, "Adding station failed, no room in table.\n");
Wey-Yi Guyc745f552011-10-10 07:27:12 -070084 break;
85 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
Johannes Berg121b8002015-06-23 21:01:44 +020086 IWL_ERR(priv,
87 "Adding station failed, no block ack resource.\n");
Wey-Yi Guyc745f552011-10-10 07:27:12 -070088 break;
89 case ADD_STA_MODIFY_NON_EXIST_STA:
Johannes Berg121b8002015-06-23 21:01:44 +020090 IWL_ERR(priv, "Attempting to modify non-existing station\n");
Wey-Yi Guyc745f552011-10-10 07:27:12 -070091 break;
92 default:
93 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080094 add_sta_resp->status);
Wey-Yi Guyc745f552011-10-10 07:27:12 -070095 break;
96 }
97
Johannes Berg2bfb5092012-12-27 21:43:48 +010098 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -070099
Johannes Berg4492bb62015-06-23 20:56:45 +0200100 return 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700101}
102
Johannes Berg48a2d662012-03-05 11:24:39 -0800103int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700104 struct iwl_device_cmd *cmd)
105{
106 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700107
Johannes Berg513aa3b2012-09-20 16:06:14 +0200108 if (!cmd)
109 return 0;
110
Johannes Berg121b8002015-06-23 21:01:44 +0200111 return iwl_process_add_sta_resp(priv, pkt);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700112}
113
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700114int iwl_send_add_sta(struct iwl_priv *priv,
115 struct iwl_addsta_cmd *sta, u8 flags)
116{
117 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700118 struct iwl_host_cmd cmd = {
119 .id = REPLY_ADD_STA,
120 .flags = flags,
Johannes Berg69b172f2011-12-12 04:17:44 -0800121 .data = { sta, },
122 .len = { sizeof(*sta), },
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700123 };
124 u8 sta_id __maybe_unused = sta->sta.sta_id;
Johannes Berg4492bb62015-06-23 20:56:45 +0200125 struct iwl_rx_packet *pkt;
126 struct iwl_add_sta_resp *add_sta_resp;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700127
128 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
129 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
130
131 if (!(flags & CMD_ASYNC)) {
Johannes Berg38c0f3342013-02-27 13:18:50 +0100132 cmd.flags |= CMD_WANT_SKB;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700133 might_sleep();
134 }
135
Johannes Berge10a0532012-03-06 13:30:39 -0800136 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700137
138 if (ret || (flags & CMD_ASYNC))
139 return ret;
Johannes Berg4492bb62015-06-23 20:56:45 +0200140
141 pkt = cmd.resp_pkt;
142 add_sta_resp = (void *)pkt->data;
143
144 /* debug messages are printed in the handler */
145 if (!(pkt->hdr.flags & IWL_CMD_FAILED_MSK) &&
146 add_sta_resp->status == ADD_STA_SUCCESS_MSK) {
147 spin_lock_bh(&priv->sta_lock);
148 ret = iwl_sta_ucode_activate(priv, sta_id);
149 spin_unlock_bh(&priv->sta_lock);
150 } else {
151 ret = -EIO;
152 }
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700153
Johannes Berg65b94a42012-03-05 11:24:38 -0800154 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700155
Johannes Berg4492bb62015-06-23 20:56:45 +0200156 return ret;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700157}
158
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700159bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
160 struct iwl_rxon_context *ctx,
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100161 struct ieee80211_sta *sta)
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700162{
163 if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
164 return false;
165
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700166#ifdef CONFIG_IWLWIFI_DEBUGFS
167 if (priv->disable_ht40)
168 return false;
169#endif
170
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100171 /* special case for RXON */
172 if (!sta)
Johannes Berg26a7ca92012-05-21 11:55:54 +0200173 return true;
174
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100175 return sta->bandwidth >= IEEE80211_STA_RX_BW_40;
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700176}
177
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800178static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
179 struct ieee80211_sta *sta,
180 struct iwl_rxon_context *ctx,
181 __le32 *flags, __le32 *mask)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700182{
183 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700184
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800185 *mask = STA_FLG_RTS_MIMO_PROT_MSK |
186 STA_FLG_MIMO_DIS_MSK |
187 STA_FLG_HT40_EN_MSK |
188 STA_FLG_MAX_AGG_SIZE_MSK |
189 STA_FLG_AGG_MPDU_DENSITY_MSK;
190 *flags = 0;
191
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700192 if (!sta || !sta_ht_inf->ht_supported)
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800193 return;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700194
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800195 IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",
Meenakshi Venkataramana35e2702012-05-16 22:40:50 +0200196 sta->addr,
Johannes Bergaf0ed692013-02-12 14:21:00 +0100197 (sta->smps_mode == IEEE80211_SMPS_STATIC) ?
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700198 "static" :
Johannes Bergaf0ed692013-02-12 14:21:00 +0100199 (sta->smps_mode == IEEE80211_SMPS_DYNAMIC) ?
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700200 "dynamic" : "disabled");
201
Johannes Bergaf0ed692013-02-12 14:21:00 +0100202 switch (sta->smps_mode) {
203 case IEEE80211_SMPS_STATIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800204 *flags |= STA_FLG_MIMO_DIS_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700205 break;
Johannes Bergaf0ed692013-02-12 14:21:00 +0100206 case IEEE80211_SMPS_DYNAMIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800207 *flags |= STA_FLG_RTS_MIMO_PROT_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700208 break;
Johannes Bergaf0ed692013-02-12 14:21:00 +0100209 case IEEE80211_SMPS_OFF:
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700210 break;
211 default:
Johannes Bergaf0ed692013-02-12 14:21:00 +0100212 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", sta->smps_mode);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700213 break;
214 }
215
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800216 *flags |= cpu_to_le32(
217 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700218
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800219 *flags |= cpu_to_le32(
220 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700221
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100222 if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800223 *flags |= STA_FLG_HT40_EN_MSK;
224}
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700225
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800226int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
227 struct ieee80211_sta *sta)
228{
229 u8 sta_id = iwl_sta_id(sta);
230 __le32 flags, mask;
231 struct iwl_addsta_cmd cmd;
232
233 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
234 return -EINVAL;
235
236 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
237
238 spin_lock_bh(&priv->sta_lock);
239 priv->stations[sta_id].sta.station_flags &= ~mask;
240 priv->stations[sta_id].sta.station_flags |= flags;
241 spin_unlock_bh(&priv->sta_lock);
242
243 memset(&cmd, 0, sizeof(cmd));
244 cmd.mode = STA_CONTROL_MODIFY_MSK;
245 cmd.station_flags_msk = mask;
246 cmd.station_flags = flags;
247 cmd.sta.sta_id = sta_id;
248
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300249 return iwl_send_add_sta(priv, &cmd, 0);
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800250}
251
252static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
253 struct ieee80211_sta *sta,
254 struct iwl_rxon_context *ctx)
255{
256 __le32 flags, mask;
257
258 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
259
260 lockdep_assert_held(&priv->sta_lock);
261 priv->stations[index].sta.station_flags &= ~mask;
262 priv->stations[index].sta.station_flags |= flags;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700263}
264
265/**
266 * iwl_prep_station - Prepare station information for addition
267 *
268 * should be called with sta_lock held
269 */
270u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
271 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
272{
273 struct iwl_station_entry *station;
274 int i;
275 u8 sta_id = IWL_INVALID_STATION;
276
277 if (is_ap)
278 sta_id = ctx->ap_sta_id;
279 else if (is_broadcast_ether_addr(addr))
280 sta_id = ctx->bcast_sta_id;
281 else
282 for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
Joe Perches2e42e472012-05-09 17:17:46 +0000283 if (ether_addr_equal(priv->stations[i].sta.sta.addr,
284 addr)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700285 sta_id = i;
286 break;
287 }
288
289 if (!priv->stations[i].used &&
290 sta_id == IWL_INVALID_STATION)
291 sta_id = i;
292 }
293
294 /*
295 * These two conditions have the same outcome, but keep them
296 * separate
297 */
298 if (unlikely(sta_id == IWL_INVALID_STATION))
299 return sta_id;
300
301 /*
302 * uCode is not able to deal with multiple requests to add a
303 * station. Keep track if one is in progress so that we do not send
304 * another.
305 */
306 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
307 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
308 "added.\n", sta_id);
309 return sta_id;
310 }
311
312 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
313 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
Joe Perches2e42e472012-05-09 17:17:46 +0000314 ether_addr_equal(priv->stations[sta_id].sta.sta.addr, addr)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700315 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
316 "adding again.\n", sta_id, addr);
317 return sta_id;
318 }
319
320 station = &priv->stations[sta_id];
321 station->used = IWL_STA_DRIVER_ACTIVE;
322 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
323 sta_id, addr);
324 priv->num_stations++;
325
326 /* Set up the REPLY_ADD_STA command to send to device */
327 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
328 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
329 station->sta.mode = 0;
330 station->sta.sta.sta_id = sta_id;
331 station->sta.station_flags = ctx->station_flags;
332 station->ctxid = ctx->ctxid;
333
334 if (sta) {
335 struct iwl_station_priv *sta_priv;
336
337 sta_priv = (void *)sta->drv_priv;
338 sta_priv->ctx = ctx;
339 }
340
341 /*
342 * OK to call unconditionally, since local stations (IBSS BSSID
343 * STA and broadcast STA) pass in a NULL sta, and mac80211
344 * doesn't allow HT IBSS.
345 */
346 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
347
348 return sta_id;
349
350}
351
352#define STA_WAIT_TIMEOUT (HZ/2)
353
354/**
355 * iwl_add_station_common -
356 */
357int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
358 const u8 *addr, bool is_ap,
359 struct ieee80211_sta *sta, u8 *sta_id_r)
360{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700361 int ret = 0;
362 u8 sta_id;
363 struct iwl_addsta_cmd sta_cmd;
364
365 *sta_id_r = 0;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800366 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700367 sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
368 if (sta_id == IWL_INVALID_STATION) {
369 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
370 addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800371 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700372 return -EINVAL;
373 }
374
375 /*
376 * uCode is not able to deal with multiple requests to add a
377 * station. Keep track if one is in progress so that we do not send
378 * another.
379 */
380 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
381 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
382 "added.\n", sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800383 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700384 return -EEXIST;
385 }
386
387 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
388 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
389 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
390 "adding again.\n", sta_id, addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800391 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700392 return -EEXIST;
393 }
394
395 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
396 memcpy(&sta_cmd, &priv->stations[sta_id].sta,
397 sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -0800398 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700399
400 /* Add station to device's station table */
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300401 ret = iwl_send_add_sta(priv, &sta_cmd, 0);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700402 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800403 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700404 IWL_ERR(priv, "Adding station %pM failed.\n",
405 priv->stations[sta_id].sta.sta.addr);
406 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
407 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800408 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700409 }
410 *sta_id_r = sta_id;
411 return ret;
412}
413
414/**
415 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700416 */
417static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
418{
Johannes Bergfa23cb02012-03-05 11:24:25 -0800419 lockdep_assert_held(&priv->sta_lock);
420
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700421 /* Ucode must be active and driver must be non active */
422 if ((priv->stations[sta_id].used &
423 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
424 IWL_STA_UCODE_ACTIVE)
425 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
426
427 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
428
429 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
430 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
431}
432
433static int iwl_send_remove_station(struct iwl_priv *priv,
434 const u8 *addr, int sta_id,
435 bool temporary)
436{
437 struct iwl_rx_packet *pkt;
438 int ret;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700439 struct iwl_rem_sta_cmd rm_sta_cmd;
440
441 struct iwl_host_cmd cmd = {
442 .id = REPLY_REMOVE_STA,
443 .len = { sizeof(struct iwl_rem_sta_cmd), },
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700444 .data = { &rm_sta_cmd, },
445 };
446
447 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
448 rm_sta_cmd.num_sta = 1;
449 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
450
451 cmd.flags |= CMD_WANT_SKB;
452
Johannes Berge10a0532012-03-06 13:30:39 -0800453 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700454
455 if (ret)
456 return ret;
457
Johannes Berg65b94a42012-03-05 11:24:38 -0800458 pkt = cmd.resp_pkt;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700459 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
460 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
461 pkt->hdr.flags);
462 ret = -EIO;
463 }
464
465 if (!ret) {
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800466 struct iwl_rem_sta_resp *rem_sta_resp = (void *)pkt->data;
467 switch (rem_sta_resp->status) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700468 case REM_STA_SUCCESS_MSK:
469 if (!temporary) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800470 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700471 iwl_sta_ucode_deactivate(priv, sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800472 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700473 }
474 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
475 break;
476 default:
477 ret = -EIO;
478 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
479 break;
480 }
481 }
Johannes Berg65b94a42012-03-05 11:24:38 -0800482 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700483
484 return ret;
485}
486
487/**
488 * iwl_remove_station - Remove driver's knowledge of station.
489 */
490int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
491 const u8 *addr)
492{
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200493 u8 tid;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700494
Don Fry83626402012-03-07 09:52:37 -0800495 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700496 IWL_DEBUG_INFO(priv,
497 "Unable to remove station %pM, device not ready.\n",
498 addr);
499 /*
500 * It is typical for stations to be removed when we are
501 * going down. Return success since device will be down
502 * soon anyway
503 */
504 return 0;
505 }
506
507 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
508 sta_id, addr);
509
510 if (WARN_ON(sta_id == IWL_INVALID_STATION))
511 return -EINVAL;
512
Johannes Bergfa23cb02012-03-05 11:24:25 -0800513 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700514
515 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
516 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
517 addr);
518 goto out_err;
519 }
520
521 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
522 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
523 addr);
524 goto out_err;
525 }
526
527 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
528 kfree(priv->stations[sta_id].lq);
529 priv->stations[sta_id].lq = NULL;
530 }
531
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200532 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
533 memset(&priv->tid_data[sta_id][tid], 0,
534 sizeof(priv->tid_data[sta_id][tid]));
535
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700536 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
537
538 priv->num_stations--;
539
540 if (WARN_ON(priv->num_stations < 0))
541 priv->num_stations = 0;
542
Johannes Bergfa23cb02012-03-05 11:24:25 -0800543 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700544
545 return iwl_send_remove_station(priv, addr, sta_id, false);
546out_err:
Johannes Bergfa23cb02012-03-05 11:24:25 -0800547 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700548 return -EINVAL;
549}
550
Johannes Berg97420512012-03-07 09:52:42 -0800551void iwl_deactivate_station(struct iwl_priv *priv, const u8 sta_id,
552 const u8 *addr)
553{
554 u8 tid;
555
556 if (!iwl_is_ready(priv)) {
557 IWL_DEBUG_INFO(priv,
558 "Unable to remove station %pM, device not ready.\n",
559 addr);
560 return;
561 }
562
563 IWL_DEBUG_ASSOC(priv, "Deactivating STA: %pM (%d)\n", addr, sta_id);
564
565 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
566 return;
567
568 spin_lock_bh(&priv->sta_lock);
569
570 WARN_ON_ONCE(!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE));
571
572 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
573 memset(&priv->tid_data[sta_id][tid], 0,
574 sizeof(priv->tid_data[sta_id][tid]));
575
576 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
Emmanuel Grumbachec6f6782014-02-18 10:30:18 +0200577 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Berg97420512012-03-07 09:52:42 -0800578
579 priv->num_stations--;
580
581 if (WARN_ON_ONCE(priv->num_stations < 0))
582 priv->num_stations = 0;
583
584 spin_unlock_bh(&priv->sta_lock);
585}
586
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100587static void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
588 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
589{
590 int i, r;
591 u32 rate_flags = 0;
592 __le32 rate_n_flags;
593
594 lockdep_assert_held(&priv->mutex);
595
596 memset(link_cmd, 0, sizeof(*link_cmd));
597
598 /* Set up the rate scaling to start at selected rate, fall back
599 * all the way down to 1M in IEEE order, and then spin on 1M */
600 if (priv->band == IEEE80211_BAND_5GHZ)
601 r = IWL_RATE_6M_INDEX;
602 else if (ctx && ctx->vif && ctx->vif->p2p)
603 r = IWL_RATE_6M_INDEX;
604 else
605 r = IWL_RATE_1M_INDEX;
606
607 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
608 rate_flags |= RATE_MCS_CCK_MSK;
609
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200610 rate_flags |= first_antenna(priv->nvm_data->valid_tx_ant) <<
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100611 RATE_MCS_ANT_POS;
612 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
613 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
614 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
615
616 link_cmd->general_params.single_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200617 first_antenna(priv->nvm_data->valid_tx_ant);
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100618
619 link_cmd->general_params.dual_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200620 priv->nvm_data->valid_tx_ant &
621 ~first_antenna(priv->nvm_data->valid_tx_ant);
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100622 if (!link_cmd->general_params.dual_stream_ant_msk) {
623 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200624 } else if (num_of_ant(priv->nvm_data->valid_tx_ant) == 2) {
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100625 link_cmd->general_params.dual_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200626 priv->nvm_data->valid_tx_ant;
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100627 }
628
629 link_cmd->agg_params.agg_dis_start_th =
630 LINK_QUAL_AGG_DISABLE_START_DEF;
631 link_cmd->agg_params.agg_time_limit =
632 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
633
634 link_cmd->sta_id = sta_id;
635}
636
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700637/**
638 * iwl_clear_ucode_stations - clear ucode station table bits
639 *
640 * This function clears all the bits in the driver indicating
641 * which stations are active in the ucode. Call when something
642 * other than explicit station management would cause this in
643 * the ucode, e.g. unassociated RXON.
644 */
645void iwl_clear_ucode_stations(struct iwl_priv *priv,
646 struct iwl_rxon_context *ctx)
647{
648 int i;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700649 bool cleared = false;
650
651 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
652
Johannes Bergfa23cb02012-03-05 11:24:25 -0800653 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700654 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
655 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
656 continue;
657
658 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
659 IWL_DEBUG_INFO(priv,
660 "Clearing ucode active for station %d\n", i);
661 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
662 cleared = true;
663 }
664 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800665 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700666
667 if (!cleared)
668 IWL_DEBUG_INFO(priv,
669 "No active stations found to be cleared\n");
670}
671
672/**
673 * iwl_restore_stations() - Restore driver known stations to device
674 *
675 * All stations considered active by driver, but not present in ucode, is
676 * restored.
677 *
678 * Function sleeps.
679 */
680void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
681{
682 struct iwl_addsta_cmd sta_cmd;
Emmanuel Grumbach63b77bf2013-04-17 09:47:00 +0300683 static const struct iwl_link_quality_cmd zero_lq = {};
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700684 struct iwl_link_quality_cmd lq;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700685 int i;
686 bool found = false;
687 int ret;
688 bool send_lq;
689
Don Fry83626402012-03-07 09:52:37 -0800690 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700691 IWL_DEBUG_INFO(priv,
692 "Not ready yet, not restoring any stations.\n");
693 return;
694 }
695
696 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -0800697 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700698 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
699 if (ctx->ctxid != priv->stations[i].ctxid)
700 continue;
701 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
702 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
703 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
704 priv->stations[i].sta.sta.addr);
705 priv->stations[i].sta.mode = 0;
706 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
707 found = true;
708 }
709 }
710
711 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
712 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
713 memcpy(&sta_cmd, &priv->stations[i].sta,
714 sizeof(struct iwl_addsta_cmd));
715 send_lq = false;
716 if (priv->stations[i].lq) {
Johannes Berg15b86bf2012-03-05 11:24:36 -0800717 if (priv->wowlan)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700718 iwl_sta_fill_lq(priv, ctx, i, &lq);
719 else
720 memcpy(&lq, priv->stations[i].lq,
721 sizeof(struct iwl_link_quality_cmd));
Emmanuel Grumbach63b77bf2013-04-17 09:47:00 +0300722
Emmanuel Grumbacha8778362013-05-22 10:48:10 +0300723 if (memcmp(&lq, &zero_lq, sizeof(lq)))
Emmanuel Grumbach63b77bf2013-04-17 09:47:00 +0300724 send_lq = true;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700725 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800726 spin_unlock_bh(&priv->sta_lock);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300727 ret = iwl_send_add_sta(priv, &sta_cmd, 0);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700728 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800729 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700730 IWL_ERR(priv, "Adding station %pM failed.\n",
731 priv->stations[i].sta.sta.addr);
732 priv->stations[i].used &=
733 ~IWL_STA_DRIVER_ACTIVE;
734 priv->stations[i].used &=
735 ~IWL_STA_UCODE_INPROGRESS;
Meenakshi Venkataramanf4c37172012-05-16 22:49:48 +0200736 continue;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700737 }
738 /*
739 * Rate scaling has already been initialized, send
740 * current LQ command
741 */
742 if (send_lq)
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300743 iwl_send_lq_cmd(priv, ctx, &lq, 0, true);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800744 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700745 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
746 }
747 }
748
Johannes Bergfa23cb02012-03-05 11:24:25 -0800749 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700750 if (!found)
751 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
752 "no stations to be restored.\n");
753 else
754 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
755 "complete.\n");
756}
757
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700758int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
759{
760 int i;
761
762 for (i = 0; i < priv->sta_key_max_num; i++)
763 if (!test_and_set_bit(i, &priv->ucode_key_table))
764 return i;
765
766 return WEP_INVALID_OFFSET;
767}
768
769void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
770{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700771 int i;
772
Johannes Bergfa23cb02012-03-05 11:24:25 -0800773 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700774 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
775 if (!(priv->stations[i].used & IWL_STA_BCAST))
776 continue;
777
778 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
779 priv->num_stations--;
780 if (WARN_ON(priv->num_stations < 0))
781 priv->num_stations = 0;
782 kfree(priv->stations[i].lq);
783 priv->stations[i].lq = NULL;
784 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800785 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700786}
787
788#ifdef CONFIG_IWLWIFI_DEBUG
789static void iwl_dump_lq_cmd(struct iwl_priv *priv,
790 struct iwl_link_quality_cmd *lq)
791{
792 int i;
793 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
794 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
795 lq->general_params.single_stream_ant_msk,
796 lq->general_params.dual_stream_ant_msk);
797
798 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
799 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
800 i, lq->rs_table[i].rate_n_flags);
801}
802#else
803static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
804 struct iwl_link_quality_cmd *lq)
805{
806}
807#endif
808
809/**
810 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
811 *
812 * It sometimes happens when a HT rate has been in use and we
813 * loose connectivity with AP then mac80211 will first tell us that the
814 * current channel is not HT anymore before removing the station. In such a
815 * scenario the RXON flags will be updated to indicate we are not
816 * communicating HT anymore, but the LQ command may still contain HT rates.
817 * Test for this to prevent driver from sending LQ command between the time
818 * RXON flags are updated and when LQ command is updated.
819 */
820static bool is_lq_table_valid(struct iwl_priv *priv,
821 struct iwl_rxon_context *ctx,
822 struct iwl_link_quality_cmd *lq)
823{
824 int i;
825
826 if (ctx->ht.enabled)
827 return true;
828
829 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
830 ctx->active.channel);
831 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
832 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
833 RATE_MCS_HT_MSK) {
834 IWL_DEBUG_INFO(priv,
835 "index %d of LQ expects HT channel\n",
836 i);
837 return false;
838 }
839 }
840 return true;
841}
842
843/**
844 * iwl_send_lq_cmd() - Send link quality command
845 * @init: This command is sent as part of station initialization right
846 * after station has been added.
847 *
848 * The link quality command is sent as the last step of station creation.
849 * This is the special case in which init is set and we call a callback in
850 * this case to clear the state indicating that station creation is in
851 * progress.
852 */
853int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
854 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
855{
856 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700857 struct iwl_host_cmd cmd = {
858 .id = REPLY_TX_LINK_QUALITY_CMD,
859 .len = { sizeof(struct iwl_link_quality_cmd), },
860 .flags = flags,
861 .data = { lq, },
862 };
863
864 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
865 return -EINVAL;
866
867
Johannes Bergfa23cb02012-03-05 11:24:25 -0800868 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700869 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800870 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700871 return -EINVAL;
872 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800873 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700874
875 iwl_dump_lq_cmd(priv, lq);
876 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
877 return -EINVAL;
878
879 if (is_lq_table_valid(priv, ctx, lq))
Johannes Berge10a0532012-03-06 13:30:39 -0800880 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700881 else
882 ret = -EINVAL;
883
884 if (cmd.flags & CMD_ASYNC)
885 return ret;
886
887 if (init) {
888 IWL_DEBUG_INFO(priv, "init LQ command complete, "
889 "clearing sta addition status for sta %d\n",
890 lq->sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800891 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700892 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800893 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700894 }
895 return ret;
896}
897
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700898
Johannes Bergc079166e2011-10-10 07:26:54 -0700899static struct iwl_link_quality_cmd *
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700900iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
901 u8 sta_id)
Johannes Bergc079166e2011-10-10 07:26:54 -0700902{
903 struct iwl_link_quality_cmd *link_cmd;
904
905 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
906 if (!link_cmd) {
907 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
908 return NULL;
909 }
910
911 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200912
913 return link_cmd;
914}
915
916/*
917 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
918 *
919 * Function sleeps.
920 */
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700921int iwlagn_add_bssid_station(struct iwl_priv *priv,
922 struct iwl_rxon_context *ctx,
Johannes Berga30e3112010-09-22 18:02:01 +0200923 const u8 *addr, u8 *sta_id_r)
924{
925 int ret;
926 u8 sta_id;
927 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +0200928
929 if (sta_id_r)
930 *sta_id_r = IWL_INVALID_STATION;
931
932 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
933 if (ret) {
934 IWL_ERR(priv, "Unable to add station %pM\n", addr);
935 return ret;
936 }
937
938 if (sta_id_r)
939 *sta_id_r = sta_id;
940
Johannes Bergfa23cb02012-03-05 11:24:25 -0800941 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200942 priv->stations[sta_id].used |= IWL_STA_LOCAL;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800943 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200944
945 /* Set up default rate scaling table in device's station table */
Johannes Bergf775aa06d2011-06-22 06:34:09 -0700946 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200947 if (!link_cmd) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700948 IWL_ERR(priv,
949 "Unable to initialize rate scaling for station %pM.\n",
Johannes Berga30e3112010-09-22 18:02:01 +0200950 addr);
951 return -ENOMEM;
952 }
953
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300954 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, 0, true);
Johannes Berga30e3112010-09-22 18:02:01 +0200955 if (ret)
956 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
957
Johannes Bergfa23cb02012-03-05 11:24:25 -0800958 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200959 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800960 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200961
962 return 0;
963}
964
Johannes Berg5a3d9882011-07-15 13:03:12 -0700965/*
966 * static WEP keys
967 *
968 * For each context, the device has a table of 4 static WEP keys
969 * (one for each key index) that is updated with the following
970 * commands.
971 */
972
Johannes Berga30e3112010-09-22 18:02:01 +0200973static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
974 struct iwl_rxon_context *ctx,
975 bool send_if_empty)
976{
977 int i, not_empty = 0;
978 u8 buff[sizeof(struct iwl_wep_cmd) +
979 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
980 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
981 size_t cmd_size = sizeof(struct iwl_wep_cmd);
982 struct iwl_host_cmd cmd = {
983 .id = ctx->wep_key_cmd,
Johannes Berg3fa50732011-05-04 07:50:38 -0700984 .data = { wep_cmd, },
Johannes Berga30e3112010-09-22 18:02:01 +0200985 };
986
987 might_sleep();
988
989 memset(wep_cmd, 0, cmd_size +
990 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
991
992 for (i = 0; i < WEP_KEYS_MAX ; i++) {
993 wep_cmd->key[i].key_index = i;
994 if (ctx->wep_keys[i].key_size) {
995 wep_cmd->key[i].key_offset = i;
996 not_empty = 1;
997 } else {
998 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
999 }
1000
1001 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
1002 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
1003 ctx->wep_keys[i].key_size);
1004 }
1005
1006 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
1007 wep_cmd->num_keys = WEP_KEYS_MAX;
1008
1009 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
1010
Johannes Berg3fa50732011-05-04 07:50:38 -07001011 cmd.len[0] = cmd_size;
Johannes Berga30e3112010-09-22 18:02:01 +02001012
1013 if (not_empty || send_if_empty)
Johannes Berge10a0532012-03-06 13:30:39 -08001014 return iwl_dvm_send_cmd(priv, &cmd);
Johannes Berga30e3112010-09-22 18:02:01 +02001015 else
1016 return 0;
1017}
1018
1019int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1020 struct iwl_rxon_context *ctx)
1021{
Johannes Bergb1eea292012-03-06 13:30:42 -08001022 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001023
1024 return iwl_send_static_wepkey_cmd(priv, ctx, false);
1025}
1026
1027int iwl_remove_default_wep_key(struct iwl_priv *priv,
1028 struct iwl_rxon_context *ctx,
1029 struct ieee80211_key_conf *keyconf)
1030{
1031 int ret;
1032
Johannes Bergb1eea292012-03-06 13:30:42 -08001033 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001034
1035 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1036 keyconf->keyidx);
1037
1038 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
Don Fry83626402012-03-07 09:52:37 -08001039 if (iwl_is_rfkill(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001040 IWL_DEBUG_WEP(priv,
1041 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berga30e3112010-09-22 18:02:01 +02001042 /* but keys in device are clear anyway so return success */
1043 return 0;
1044 }
1045 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1046 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1047 keyconf->keyidx, ret);
1048
1049 return ret;
1050}
1051
1052int iwl_set_default_wep_key(struct iwl_priv *priv,
1053 struct iwl_rxon_context *ctx,
1054 struct ieee80211_key_conf *keyconf)
1055{
1056 int ret;
1057
Johannes Bergb1eea292012-03-06 13:30:42 -08001058 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001059
1060 if (keyconf->keylen != WEP_KEY_LEN_128 &&
1061 keyconf->keylen != WEP_KEY_LEN_64) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001062 IWL_DEBUG_WEP(priv,
1063 "Bad WEP key length %d\n", keyconf->keylen);
Johannes Berga30e3112010-09-22 18:02:01 +02001064 return -EINVAL;
1065 }
1066
Johannes Berg5a3d9882011-07-15 13:03:12 -07001067 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
Johannes Berga30e3112010-09-22 18:02:01 +02001068
1069 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1070 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1071 keyconf->keylen);
1072
1073 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1074 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1075 keyconf->keylen, keyconf->keyidx, ret);
1076
1077 return ret;
1078}
1079
Johannes Berg5a3d9882011-07-15 13:03:12 -07001080/*
1081 * dynamic (per-station) keys
1082 *
1083 * The dynamic keys are a little more complicated. The device has
1084 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1085 * These are linked to stations by a table that contains an index
1086 * into the key table for each station/key index/{mcast,unicast},
1087 * i.e. it's basically an array of pointers like this:
1088 * key_offset_t key_mapping[NUM_STATIONS][4][2];
1089 * (it really works differently, but you can think of it as such)
1090 *
1091 * The key uploading and linking happens in the same command, the
1092 * add station command with STA_MODIFY_KEY_MASK.
1093 */
1094
1095static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1096 struct ieee80211_vif *vif,
1097 struct ieee80211_sta *sta)
1098{
1099 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001100
1101 if (sta)
Johannes Berg40e4e682012-03-05 11:24:22 -08001102 return iwl_sta_id(sta);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001103
1104 /*
1105 * The device expects GTKs for station interfaces to be
1106 * installed as GTKs for the AP station. If we have no
1107 * station ID, then use the ap_sta_id in that case.
1108 */
Johannes Berg40e4e682012-03-05 11:24:22 -08001109 if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1110 return vif_priv->ctx->ap_sta_id;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001111
Johannes Berg40e4e682012-03-05 11:24:22 -08001112 return IWL_INVALID_STATION;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001113}
1114
Johannes Berge1b1c082011-07-17 01:44:11 -07001115static int iwlagn_send_sta_key(struct iwl_priv *priv,
1116 struct ieee80211_key_conf *keyconf,
1117 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1118 u32 cmd_flags)
Johannes Berga30e3112010-09-22 18:02:01 +02001119{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001120 __le16 key_flags;
Johannes Berga30e3112010-09-22 18:02:01 +02001121 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001122 int i;
Johannes Berge1b1c082011-07-17 01:44:11 -07001123
Johannes Bergfa23cb02012-03-05 11:24:25 -08001124 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001125 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001126 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001127
Johannes Berg5a3d9882011-07-15 13:03:12 -07001128 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1129 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001130
Johannes Berg5a3d9882011-07-15 13:03:12 -07001131 switch (keyconf->cipher) {
1132 case WLAN_CIPHER_SUITE_CCMP:
1133 key_flags |= STA_KEY_FLG_CCMP;
1134 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1135 break;
1136 case WLAN_CIPHER_SUITE_TKIP:
1137 key_flags |= STA_KEY_FLG_TKIP;
1138 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1139 for (i = 0; i < 5; i++)
1140 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1141 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1142 break;
1143 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga30e3112010-09-22 18:02:01 +02001144 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001145 /* fall through */
1146 case WLAN_CIPHER_SUITE_WEP40:
1147 key_flags |= STA_KEY_FLG_WEP;
1148 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1149 break;
1150 default:
1151 WARN_ON(1);
1152 return -EINVAL;
1153 }
Johannes Berga30e3112010-09-22 18:02:01 +02001154
Johannes Berg5a3d9882011-07-15 13:03:12 -07001155 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
Johannes Berga30e3112010-09-22 18:02:01 +02001156 key_flags |= STA_KEY_MULTICAST_MSK;
1157
Johannes Berg5a3d9882011-07-15 13:03:12 -07001158 /* key pointer (offset) */
1159 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berga30e3112010-09-22 18:02:01 +02001160
Johannes Berg5a3d9882011-07-15 13:03:12 -07001161 sta_cmd.key.key_flags = key_flags;
1162 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1163 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
Johannes Berga30e3112010-09-22 18:02:01 +02001164
Johannes Berg5a3d9882011-07-15 13:03:12 -07001165 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001166}
1167
1168void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001169 struct ieee80211_vif *vif,
Johannes Berga30e3112010-09-22 18:02:01 +02001170 struct ieee80211_key_conf *keyconf,
1171 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1172{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001173 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1174
1175 if (sta_id == IWL_INVALID_STATION)
1176 return;
Johannes Berga30e3112010-09-22 18:02:01 +02001177
1178 if (iwl_scan_cancel(priv)) {
1179 /* cancel scan failed, just live w/ bad key and rely
1180 briefly on SW decryption */
1181 return;
1182 }
1183
Johannes Berge1b1c082011-07-17 01:44:11 -07001184 iwlagn_send_sta_key(priv, keyconf, sta_id,
1185 iv32, phase1key, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001186}
1187
1188int iwl_remove_dynamic_key(struct iwl_priv *priv,
1189 struct iwl_rxon_context *ctx,
1190 struct ieee80211_key_conf *keyconf,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001191 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001192{
Johannes Berga30e3112010-09-22 18:02:01 +02001193 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001194 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
Johannes Berg5dcbf482012-02-17 09:47:14 -08001195 __le16 key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001196
1197 /* if station isn't there, neither is the key */
1198 if (sta_id == IWL_INVALID_STATION)
1199 return -ENOENT;
1200
Johannes Bergfa23cb02012-03-05 11:24:25 -08001201 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001202 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1203 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1204 sta_id = IWL_INVALID_STATION;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001205 spin_unlock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001206
1207 if (sta_id == IWL_INVALID_STATION)
1208 return 0;
Johannes Berga30e3112010-09-22 18:02:01 +02001209
Johannes Bergb1eea292012-03-06 13:30:42 -08001210 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001211
1212 ctx->key_mapping_keys--;
1213
Johannes Berga30e3112010-09-22 18:02:01 +02001214 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1215 keyconf->keyidx, sta_id);
1216
Johannes Berg5a3d9882011-07-15 13:03:12 -07001217 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1218 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1219 keyconf->hw_key_idx);
Johannes Berga30e3112010-09-22 18:02:01 +02001220
Johannes Berg5dcbf482012-02-17 09:47:14 -08001221 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1222 key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1223 STA_KEY_FLG_INVALID;
1224
1225 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1226 key_flags |= STA_KEY_MULTICAST_MSK;
1227
1228 sta_cmd.key.key_flags = key_flags;
Emmanuel Grumbachd6ee27eb2012-06-06 09:13:36 +02001229 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001230 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1231 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001232
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001233 return iwl_send_add_sta(priv, &sta_cmd, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001234}
1235
Johannes Berg5a3d9882011-07-15 13:03:12 -07001236int iwl_set_dynamic_key(struct iwl_priv *priv,
1237 struct iwl_rxon_context *ctx,
1238 struct ieee80211_key_conf *keyconf,
1239 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001240{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001241 struct ieee80211_key_seq seq;
1242 u16 p1k[5];
Johannes Berga30e3112010-09-22 18:02:01 +02001243 int ret;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001244 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1245 const u8 *addr;
1246
1247 if (sta_id == IWL_INVALID_STATION)
1248 return -EINVAL;
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
Johannes Berg5a3d9882011-07-15 13:03:12 -07001252 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1253 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1254 return -ENOSPC;
1255
Johannes Berga30e3112010-09-22 18:02:01 +02001256 ctx->key_mapping_keys++;
Johannes Berga30e3112010-09-22 18:02:01 +02001257
1258 switch (keyconf->cipher) {
Johannes Berga30e3112010-09-22 18:02:01 +02001259 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001260 if (sta)
1261 addr = sta->addr;
1262 else /* station mode case only */
1263 addr = ctx->active.bssid_addr;
1264
1265 /* pre-fill phase 1 key into device cache */
1266 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1267 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Berge1b1c082011-07-17 01:44:11 -07001268 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001269 seq.tkip.iv32, p1k, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001270 break;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001271 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berga30e3112010-09-22 18:02:01 +02001272 case WLAN_CIPHER_SUITE_WEP40:
1273 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge1b1c082011-07-17 01:44:11 -07001274 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001275 0, NULL, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001276 break;
1277 default:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001278 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
Johannes Berga30e3112010-09-22 18:02:01 +02001279 ret = -EINVAL;
1280 }
1281
Johannes Berg5a3d9882011-07-15 13:03:12 -07001282 if (ret) {
1283 ctx->key_mapping_keys--;
1284 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1285 }
1286
1287 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 +02001288 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001289 sta ? sta->addr : NULL, ret);
Johannes Berga30e3112010-09-22 18:02:01 +02001290
1291 return ret;
1292}
1293
1294/**
1295 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1296 *
1297 * This adds the broadcast station into the driver's station table
1298 * and marks it driver active, so that it will be restored to the
1299 * device at the next best time.
1300 */
1301int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1302 struct iwl_rxon_context *ctx)
1303{
1304 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +02001305 u8 sta_id;
1306
Johannes Bergfa23cb02012-03-05 11:24:25 -08001307 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001308 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1309 if (sta_id == IWL_INVALID_STATION) {
1310 IWL_ERR(priv, "Unable to prepare broadcast station\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -08001311 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001312
1313 return -EINVAL;
1314 }
1315
1316 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1317 priv->stations[sta_id].used |= IWL_STA_BCAST;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001318 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001319
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001320 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001321 if (!link_cmd) {
1322 IWL_ERR(priv,
1323 "Unable to initialize rate scaling for bcast station.\n");
1324 return -ENOMEM;
1325 }
1326
Johannes Bergfa23cb02012-03-05 11:24:25 -08001327 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001328 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001329 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001330
1331 return 0;
1332}
1333
1334/**
1335 * iwl_update_bcast_station - update broadcast station's LQ command
1336 *
1337 * Only used by iwlagn. Placed here to have all bcast station management
1338 * code together.
1339 */
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001340int iwl_update_bcast_station(struct iwl_priv *priv,
1341 struct iwl_rxon_context *ctx)
Johannes Berga30e3112010-09-22 18:02:01 +02001342{
Johannes Berga30e3112010-09-22 18:02:01 +02001343 struct iwl_link_quality_cmd *link_cmd;
1344 u8 sta_id = ctx->bcast_sta_id;
1345
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001346 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001347 if (!link_cmd) {
1348 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1349 return -ENOMEM;
1350 }
1351
Johannes Bergfa23cb02012-03-05 11:24:25 -08001352 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001353 if (priv->stations[sta_id].lq)
1354 kfree(priv->stations[sta_id].lq);
1355 else
1356 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1357 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001358 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001359
1360 return 0;
1361}
1362
1363int iwl_update_bcast_stations(struct iwl_priv *priv)
1364{
1365 struct iwl_rxon_context *ctx;
1366 int ret = 0;
1367
1368 for_each_context(priv, ctx) {
1369 ret = iwl_update_bcast_station(priv, ctx);
1370 if (ret)
1371 break;
1372 }
1373
1374 return ret;
1375}
1376
1377/**
1378 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1379 */
1380int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1381{
Johannes Berga30e3112010-09-22 18:02:01 +02001382 struct iwl_addsta_cmd sta_cmd;
1383
Johannes Bergb1eea292012-03-06 13:30:42 -08001384 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001385
1386 /* Remove "disable" flag, to enable Tx for this TID */
Johannes Bergfa23cb02012-03-05 11:24:25 -08001387 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001388 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1389 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1390 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1391 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001392 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001393
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001394 return iwl_send_add_sta(priv, &sta_cmd, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001395}
1396
1397int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1398 int tid, u16 ssn)
1399{
Johannes Berga30e3112010-09-22 18:02:01 +02001400 int sta_id;
1401 struct iwl_addsta_cmd sta_cmd;
1402
Johannes Bergb1eea292012-03-06 13:30:42 -08001403 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001404
1405 sta_id = iwl_sta_id(sta);
1406 if (sta_id == IWL_INVALID_STATION)
1407 return -ENXIO;
1408
Johannes Bergfa23cb02012-03-05 11:24:25 -08001409 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001410 priv->stations[sta_id].sta.station_flags_msk = 0;
1411 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1412 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1413 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1414 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1415 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001416 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001417
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001418 return iwl_send_add_sta(priv, &sta_cmd, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001419}
1420
1421int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1422 int tid)
1423{
Johannes Berga30e3112010-09-22 18:02:01 +02001424 int sta_id;
1425 struct iwl_addsta_cmd sta_cmd;
1426
Johannes Bergb1eea292012-03-06 13:30:42 -08001427 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001428
1429 sta_id = iwl_sta_id(sta);
1430 if (sta_id == IWL_INVALID_STATION) {
1431 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1432 return -ENXIO;
1433 }
1434
Johannes Bergfa23cb02012-03-05 11:24:25 -08001435 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001436 priv->stations[sta_id].sta.station_flags_msk = 0;
1437 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1438 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1439 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1440 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001441 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001442
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001443 return iwl_send_add_sta(priv, &sta_cmd, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001444}
1445
Johannes Berga30e3112010-09-22 18:02:01 +02001446
Johannes Berga30e3112010-09-22 18:02:01 +02001447
1448void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1449{
Johannes Berg9451ca12012-03-05 11:24:23 -08001450 struct iwl_addsta_cmd cmd = {
1451 .mode = STA_CONTROL_MODIFY_MSK,
1452 .station_flags = STA_FLG_PWR_SAVE_MSK,
1453 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1454 .sta.sta_id = sta_id,
1455 .sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1456 .sleep_tx_count = cpu_to_le16(cnt),
1457 };
Johannes Berga30e3112010-09-22 18:02:01 +02001458
Johannes Berg9451ca12012-03-05 11:24:23 -08001459 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001460}