blob: c18aa3e950d5b5881730d5919cf5c5529ed92e49 [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
Johannes Berg73e686f2015-06-23 21:22:09 +020063static void iwl_process_add_sta_resp(struct iwl_priv *priv,
64 struct iwl_rx_packet *pkt)
Wey-Yi Guyc745f552011-10-10 07:27:12 -070065{
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 Berg73e686f2015-06-23 21:22:09 +020071 return;
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}
100
Johannes Berg73e686f2015-06-23 21:22:09 +0200101void iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700102{
103 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700104
Johannes Berg73e686f2015-06-23 21:22:09 +0200105 iwl_process_add_sta_resp(priv, pkt);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700106}
107
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700108int iwl_send_add_sta(struct iwl_priv *priv,
109 struct iwl_addsta_cmd *sta, u8 flags)
110{
111 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700112 struct iwl_host_cmd cmd = {
113 .id = REPLY_ADD_STA,
114 .flags = flags,
Johannes Berg69b172f2011-12-12 04:17:44 -0800115 .data = { sta, },
116 .len = { sizeof(*sta), },
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700117 };
118 u8 sta_id __maybe_unused = sta->sta.sta_id;
Johannes Berg4492bb62015-06-23 20:56:45 +0200119 struct iwl_rx_packet *pkt;
120 struct iwl_add_sta_resp *add_sta_resp;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700121
122 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
123 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
124
125 if (!(flags & CMD_ASYNC)) {
Johannes Berg38c0f3342013-02-27 13:18:50 +0100126 cmd.flags |= CMD_WANT_SKB;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700127 might_sleep();
128 }
129
Johannes Berge10a0532012-03-06 13:30:39 -0800130 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700131
132 if (ret || (flags & CMD_ASYNC))
133 return ret;
Johannes Berg4492bb62015-06-23 20:56:45 +0200134
135 pkt = cmd.resp_pkt;
136 add_sta_resp = (void *)pkt->data;
137
138 /* debug messages are printed in the handler */
139 if (!(pkt->hdr.flags & IWL_CMD_FAILED_MSK) &&
140 add_sta_resp->status == ADD_STA_SUCCESS_MSK) {
141 spin_lock_bh(&priv->sta_lock);
142 ret = iwl_sta_ucode_activate(priv, sta_id);
143 spin_unlock_bh(&priv->sta_lock);
144 } else {
145 ret = -EIO;
146 }
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700147
Johannes Berg65b94a42012-03-05 11:24:38 -0800148 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700149
Johannes Berg4492bb62015-06-23 20:56:45 +0200150 return ret;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700151}
152
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700153bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
154 struct iwl_rxon_context *ctx,
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100155 struct ieee80211_sta *sta)
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700156{
157 if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
158 return false;
159
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700160#ifdef CONFIG_IWLWIFI_DEBUGFS
161 if (priv->disable_ht40)
162 return false;
163#endif
164
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100165 /* special case for RXON */
166 if (!sta)
Johannes Berg26a7ca92012-05-21 11:55:54 +0200167 return true;
168
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100169 return sta->bandwidth >= IEEE80211_STA_RX_BW_40;
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700170}
171
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800172static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
173 struct ieee80211_sta *sta,
174 struct iwl_rxon_context *ctx,
175 __le32 *flags, __le32 *mask)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700176{
177 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700178
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800179 *mask = STA_FLG_RTS_MIMO_PROT_MSK |
180 STA_FLG_MIMO_DIS_MSK |
181 STA_FLG_HT40_EN_MSK |
182 STA_FLG_MAX_AGG_SIZE_MSK |
183 STA_FLG_AGG_MPDU_DENSITY_MSK;
184 *flags = 0;
185
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700186 if (!sta || !sta_ht_inf->ht_supported)
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800187 return;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700188
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800189 IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",
Meenakshi Venkataramana35e2702012-05-16 22:40:50 +0200190 sta->addr,
Johannes Bergaf0ed692013-02-12 14:21:00 +0100191 (sta->smps_mode == IEEE80211_SMPS_STATIC) ?
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700192 "static" :
Johannes Bergaf0ed692013-02-12 14:21:00 +0100193 (sta->smps_mode == IEEE80211_SMPS_DYNAMIC) ?
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700194 "dynamic" : "disabled");
195
Johannes Bergaf0ed692013-02-12 14:21:00 +0100196 switch (sta->smps_mode) {
197 case IEEE80211_SMPS_STATIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800198 *flags |= STA_FLG_MIMO_DIS_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700199 break;
Johannes Bergaf0ed692013-02-12 14:21:00 +0100200 case IEEE80211_SMPS_DYNAMIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800201 *flags |= STA_FLG_RTS_MIMO_PROT_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700202 break;
Johannes Bergaf0ed692013-02-12 14:21:00 +0100203 case IEEE80211_SMPS_OFF:
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700204 break;
205 default:
Johannes Bergaf0ed692013-02-12 14:21:00 +0100206 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", sta->smps_mode);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700207 break;
208 }
209
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800210 *flags |= cpu_to_le32(
211 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700212
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800213 *flags |= cpu_to_le32(
214 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700215
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100216 if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800217 *flags |= STA_FLG_HT40_EN_MSK;
218}
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700219
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800220int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
221 struct ieee80211_sta *sta)
222{
223 u8 sta_id = iwl_sta_id(sta);
224 __le32 flags, mask;
225 struct iwl_addsta_cmd cmd;
226
227 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
228 return -EINVAL;
229
230 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
231
232 spin_lock_bh(&priv->sta_lock);
233 priv->stations[sta_id].sta.station_flags &= ~mask;
234 priv->stations[sta_id].sta.station_flags |= flags;
235 spin_unlock_bh(&priv->sta_lock);
236
237 memset(&cmd, 0, sizeof(cmd));
238 cmd.mode = STA_CONTROL_MODIFY_MSK;
239 cmd.station_flags_msk = mask;
240 cmd.station_flags = flags;
241 cmd.sta.sta_id = sta_id;
242
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300243 return iwl_send_add_sta(priv, &cmd, 0);
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800244}
245
246static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
247 struct ieee80211_sta *sta,
248 struct iwl_rxon_context *ctx)
249{
250 __le32 flags, mask;
251
252 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
253
254 lockdep_assert_held(&priv->sta_lock);
255 priv->stations[index].sta.station_flags &= ~mask;
256 priv->stations[index].sta.station_flags |= flags;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700257}
258
259/**
260 * iwl_prep_station - Prepare station information for addition
261 *
262 * should be called with sta_lock held
263 */
264u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
265 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
266{
267 struct iwl_station_entry *station;
268 int i;
269 u8 sta_id = IWL_INVALID_STATION;
270
271 if (is_ap)
272 sta_id = ctx->ap_sta_id;
273 else if (is_broadcast_ether_addr(addr))
274 sta_id = ctx->bcast_sta_id;
275 else
276 for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
Joe Perches2e42e472012-05-09 17:17:46 +0000277 if (ether_addr_equal(priv->stations[i].sta.sta.addr,
278 addr)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700279 sta_id = i;
280 break;
281 }
282
283 if (!priv->stations[i].used &&
284 sta_id == IWL_INVALID_STATION)
285 sta_id = i;
286 }
287
288 /*
289 * These two conditions have the same outcome, but keep them
290 * separate
291 */
292 if (unlikely(sta_id == IWL_INVALID_STATION))
293 return sta_id;
294
295 /*
296 * uCode is not able to deal with multiple requests to add a
297 * station. Keep track if one is in progress so that we do not send
298 * another.
299 */
300 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
301 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
302 "added.\n", sta_id);
303 return sta_id;
304 }
305
306 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
307 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
Joe Perches2e42e472012-05-09 17:17:46 +0000308 ether_addr_equal(priv->stations[sta_id].sta.sta.addr, addr)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700309 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
310 "adding again.\n", sta_id, addr);
311 return sta_id;
312 }
313
314 station = &priv->stations[sta_id];
315 station->used = IWL_STA_DRIVER_ACTIVE;
316 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
317 sta_id, addr);
318 priv->num_stations++;
319
320 /* Set up the REPLY_ADD_STA command to send to device */
321 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
322 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
323 station->sta.mode = 0;
324 station->sta.sta.sta_id = sta_id;
325 station->sta.station_flags = ctx->station_flags;
326 station->ctxid = ctx->ctxid;
327
328 if (sta) {
329 struct iwl_station_priv *sta_priv;
330
331 sta_priv = (void *)sta->drv_priv;
332 sta_priv->ctx = ctx;
333 }
334
335 /*
336 * OK to call unconditionally, since local stations (IBSS BSSID
337 * STA and broadcast STA) pass in a NULL sta, and mac80211
338 * doesn't allow HT IBSS.
339 */
340 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
341
342 return sta_id;
343
344}
345
346#define STA_WAIT_TIMEOUT (HZ/2)
347
348/**
349 * iwl_add_station_common -
350 */
351int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
352 const u8 *addr, bool is_ap,
353 struct ieee80211_sta *sta, u8 *sta_id_r)
354{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700355 int ret = 0;
356 u8 sta_id;
357 struct iwl_addsta_cmd sta_cmd;
358
359 *sta_id_r = 0;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800360 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700361 sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
362 if (sta_id == IWL_INVALID_STATION) {
363 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
364 addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800365 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700366 return -EINVAL;
367 }
368
369 /*
370 * uCode is not able to deal with multiple requests to add a
371 * station. Keep track if one is in progress so that we do not send
372 * another.
373 */
374 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
375 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
376 "added.\n", sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800377 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700378 return -EEXIST;
379 }
380
381 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
382 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
383 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
384 "adding again.\n", sta_id, addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800385 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700386 return -EEXIST;
387 }
388
389 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
390 memcpy(&sta_cmd, &priv->stations[sta_id].sta,
391 sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -0800392 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700393
394 /* Add station to device's station table */
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300395 ret = iwl_send_add_sta(priv, &sta_cmd, 0);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700396 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800397 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700398 IWL_ERR(priv, "Adding station %pM failed.\n",
399 priv->stations[sta_id].sta.sta.addr);
400 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
401 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800402 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700403 }
404 *sta_id_r = sta_id;
405 return ret;
406}
407
408/**
409 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700410 */
411static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
412{
Johannes Bergfa23cb02012-03-05 11:24:25 -0800413 lockdep_assert_held(&priv->sta_lock);
414
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700415 /* Ucode must be active and driver must be non active */
416 if ((priv->stations[sta_id].used &
417 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
418 IWL_STA_UCODE_ACTIVE)
419 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
420
421 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
422
423 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
424 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
425}
426
427static int iwl_send_remove_station(struct iwl_priv *priv,
428 const u8 *addr, int sta_id,
429 bool temporary)
430{
431 struct iwl_rx_packet *pkt;
432 int ret;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700433 struct iwl_rem_sta_cmd rm_sta_cmd;
434
435 struct iwl_host_cmd cmd = {
436 .id = REPLY_REMOVE_STA,
437 .len = { sizeof(struct iwl_rem_sta_cmd), },
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700438 .data = { &rm_sta_cmd, },
439 };
440
441 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
442 rm_sta_cmd.num_sta = 1;
443 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
444
445 cmd.flags |= CMD_WANT_SKB;
446
Johannes Berge10a0532012-03-06 13:30:39 -0800447 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700448
449 if (ret)
450 return ret;
451
Johannes Berg65b94a42012-03-05 11:24:38 -0800452 pkt = cmd.resp_pkt;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700453 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
454 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
455 pkt->hdr.flags);
456 ret = -EIO;
457 }
458
459 if (!ret) {
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800460 struct iwl_rem_sta_resp *rem_sta_resp = (void *)pkt->data;
461 switch (rem_sta_resp->status) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700462 case REM_STA_SUCCESS_MSK:
463 if (!temporary) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800464 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700465 iwl_sta_ucode_deactivate(priv, sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800466 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700467 }
468 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
469 break;
470 default:
471 ret = -EIO;
472 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
473 break;
474 }
475 }
Johannes Berg65b94a42012-03-05 11:24:38 -0800476 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700477
478 return ret;
479}
480
481/**
482 * iwl_remove_station - Remove driver's knowledge of station.
483 */
484int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
485 const u8 *addr)
486{
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200487 u8 tid;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700488
Don Fry83626402012-03-07 09:52:37 -0800489 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700490 IWL_DEBUG_INFO(priv,
491 "Unable to remove station %pM, device not ready.\n",
492 addr);
493 /*
494 * It is typical for stations to be removed when we are
495 * going down. Return success since device will be down
496 * soon anyway
497 */
498 return 0;
499 }
500
501 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
502 sta_id, addr);
503
504 if (WARN_ON(sta_id == IWL_INVALID_STATION))
505 return -EINVAL;
506
Johannes Bergfa23cb02012-03-05 11:24:25 -0800507 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700508
509 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
510 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
511 addr);
512 goto out_err;
513 }
514
515 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
516 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
517 addr);
518 goto out_err;
519 }
520
521 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
522 kfree(priv->stations[sta_id].lq);
523 priv->stations[sta_id].lq = NULL;
524 }
525
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200526 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
527 memset(&priv->tid_data[sta_id][tid], 0,
528 sizeof(priv->tid_data[sta_id][tid]));
529
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700530 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
531
532 priv->num_stations--;
533
534 if (WARN_ON(priv->num_stations < 0))
535 priv->num_stations = 0;
536
Johannes Bergfa23cb02012-03-05 11:24:25 -0800537 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700538
539 return iwl_send_remove_station(priv, addr, sta_id, false);
540out_err:
Johannes Bergfa23cb02012-03-05 11:24:25 -0800541 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700542 return -EINVAL;
543}
544
Johannes Berg97420512012-03-07 09:52:42 -0800545void iwl_deactivate_station(struct iwl_priv *priv, const u8 sta_id,
546 const u8 *addr)
547{
548 u8 tid;
549
550 if (!iwl_is_ready(priv)) {
551 IWL_DEBUG_INFO(priv,
552 "Unable to remove station %pM, device not ready.\n",
553 addr);
554 return;
555 }
556
557 IWL_DEBUG_ASSOC(priv, "Deactivating STA: %pM (%d)\n", addr, sta_id);
558
559 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
560 return;
561
562 spin_lock_bh(&priv->sta_lock);
563
564 WARN_ON_ONCE(!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE));
565
566 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
567 memset(&priv->tid_data[sta_id][tid], 0,
568 sizeof(priv->tid_data[sta_id][tid]));
569
570 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
Emmanuel Grumbachec6f6782014-02-18 10:30:18 +0200571 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Berg97420512012-03-07 09:52:42 -0800572
573 priv->num_stations--;
574
575 if (WARN_ON_ONCE(priv->num_stations < 0))
576 priv->num_stations = 0;
577
578 spin_unlock_bh(&priv->sta_lock);
579}
580
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100581static void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
582 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
583{
584 int i, r;
585 u32 rate_flags = 0;
586 __le32 rate_n_flags;
587
588 lockdep_assert_held(&priv->mutex);
589
590 memset(link_cmd, 0, sizeof(*link_cmd));
591
592 /* Set up the rate scaling to start at selected rate, fall back
593 * all the way down to 1M in IEEE order, and then spin on 1M */
594 if (priv->band == IEEE80211_BAND_5GHZ)
595 r = IWL_RATE_6M_INDEX;
596 else if (ctx && ctx->vif && ctx->vif->p2p)
597 r = IWL_RATE_6M_INDEX;
598 else
599 r = IWL_RATE_1M_INDEX;
600
601 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
602 rate_flags |= RATE_MCS_CCK_MSK;
603
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200604 rate_flags |= first_antenna(priv->nvm_data->valid_tx_ant) <<
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100605 RATE_MCS_ANT_POS;
606 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
607 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
608 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
609
610 link_cmd->general_params.single_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200611 first_antenna(priv->nvm_data->valid_tx_ant);
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100612
613 link_cmd->general_params.dual_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200614 priv->nvm_data->valid_tx_ant &
615 ~first_antenna(priv->nvm_data->valid_tx_ant);
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100616 if (!link_cmd->general_params.dual_stream_ant_msk) {
617 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200618 } else if (num_of_ant(priv->nvm_data->valid_tx_ant) == 2) {
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100619 link_cmd->general_params.dual_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200620 priv->nvm_data->valid_tx_ant;
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100621 }
622
623 link_cmd->agg_params.agg_dis_start_th =
624 LINK_QUAL_AGG_DISABLE_START_DEF;
625 link_cmd->agg_params.agg_time_limit =
626 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
627
628 link_cmd->sta_id = sta_id;
629}
630
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700631/**
632 * iwl_clear_ucode_stations - clear ucode station table bits
633 *
634 * This function clears all the bits in the driver indicating
635 * which stations are active in the ucode. Call when something
636 * other than explicit station management would cause this in
637 * the ucode, e.g. unassociated RXON.
638 */
639void iwl_clear_ucode_stations(struct iwl_priv *priv,
640 struct iwl_rxon_context *ctx)
641{
642 int i;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700643 bool cleared = false;
644
645 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
646
Johannes Bergfa23cb02012-03-05 11:24:25 -0800647 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700648 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
649 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
650 continue;
651
652 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
653 IWL_DEBUG_INFO(priv,
654 "Clearing ucode active for station %d\n", i);
655 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
656 cleared = true;
657 }
658 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800659 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700660
661 if (!cleared)
662 IWL_DEBUG_INFO(priv,
663 "No active stations found to be cleared\n");
664}
665
666/**
667 * iwl_restore_stations() - Restore driver known stations to device
668 *
669 * All stations considered active by driver, but not present in ucode, is
670 * restored.
671 *
672 * Function sleeps.
673 */
674void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
675{
676 struct iwl_addsta_cmd sta_cmd;
Emmanuel Grumbach63b77bf2013-04-17 09:47:00 +0300677 static const struct iwl_link_quality_cmd zero_lq = {};
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700678 struct iwl_link_quality_cmd lq;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700679 int i;
680 bool found = false;
681 int ret;
682 bool send_lq;
683
Don Fry83626402012-03-07 09:52:37 -0800684 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700685 IWL_DEBUG_INFO(priv,
686 "Not ready yet, not restoring any stations.\n");
687 return;
688 }
689
690 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -0800691 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700692 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
693 if (ctx->ctxid != priv->stations[i].ctxid)
694 continue;
695 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
696 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
697 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
698 priv->stations[i].sta.sta.addr);
699 priv->stations[i].sta.mode = 0;
700 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
701 found = true;
702 }
703 }
704
705 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
706 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
707 memcpy(&sta_cmd, &priv->stations[i].sta,
708 sizeof(struct iwl_addsta_cmd));
709 send_lq = false;
710 if (priv->stations[i].lq) {
Johannes Berg15b86bf2012-03-05 11:24:36 -0800711 if (priv->wowlan)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700712 iwl_sta_fill_lq(priv, ctx, i, &lq);
713 else
714 memcpy(&lq, priv->stations[i].lq,
715 sizeof(struct iwl_link_quality_cmd));
Emmanuel Grumbach63b77bf2013-04-17 09:47:00 +0300716
Emmanuel Grumbacha8778362013-05-22 10:48:10 +0300717 if (memcmp(&lq, &zero_lq, sizeof(lq)))
Emmanuel Grumbach63b77bf2013-04-17 09:47:00 +0300718 send_lq = true;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700719 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800720 spin_unlock_bh(&priv->sta_lock);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300721 ret = iwl_send_add_sta(priv, &sta_cmd, 0);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700722 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800723 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700724 IWL_ERR(priv, "Adding station %pM failed.\n",
725 priv->stations[i].sta.sta.addr);
726 priv->stations[i].used &=
727 ~IWL_STA_DRIVER_ACTIVE;
728 priv->stations[i].used &=
729 ~IWL_STA_UCODE_INPROGRESS;
Meenakshi Venkataramanf4c37172012-05-16 22:49:48 +0200730 continue;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700731 }
732 /*
733 * Rate scaling has already been initialized, send
734 * current LQ command
735 */
736 if (send_lq)
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300737 iwl_send_lq_cmd(priv, ctx, &lq, 0, true);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800738 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700739 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
740 }
741 }
742
Johannes Bergfa23cb02012-03-05 11:24:25 -0800743 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700744 if (!found)
745 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
746 "no stations to be restored.\n");
747 else
748 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
749 "complete.\n");
750}
751
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700752int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
753{
754 int i;
755
756 for (i = 0; i < priv->sta_key_max_num; i++)
757 if (!test_and_set_bit(i, &priv->ucode_key_table))
758 return i;
759
760 return WEP_INVALID_OFFSET;
761}
762
763void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
764{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700765 int i;
766
Johannes Bergfa23cb02012-03-05 11:24:25 -0800767 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700768 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
769 if (!(priv->stations[i].used & IWL_STA_BCAST))
770 continue;
771
772 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
773 priv->num_stations--;
774 if (WARN_ON(priv->num_stations < 0))
775 priv->num_stations = 0;
776 kfree(priv->stations[i].lq);
777 priv->stations[i].lq = NULL;
778 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800779 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700780}
781
782#ifdef CONFIG_IWLWIFI_DEBUG
783static void iwl_dump_lq_cmd(struct iwl_priv *priv,
784 struct iwl_link_quality_cmd *lq)
785{
786 int i;
787 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
788 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
789 lq->general_params.single_stream_ant_msk,
790 lq->general_params.dual_stream_ant_msk);
791
792 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
793 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
794 i, lq->rs_table[i].rate_n_flags);
795}
796#else
797static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
798 struct iwl_link_quality_cmd *lq)
799{
800}
801#endif
802
803/**
804 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
805 *
806 * It sometimes happens when a HT rate has been in use and we
807 * loose connectivity with AP then mac80211 will first tell us that the
808 * current channel is not HT anymore before removing the station. In such a
809 * scenario the RXON flags will be updated to indicate we are not
810 * communicating HT anymore, but the LQ command may still contain HT rates.
811 * Test for this to prevent driver from sending LQ command between the time
812 * RXON flags are updated and when LQ command is updated.
813 */
814static bool is_lq_table_valid(struct iwl_priv *priv,
815 struct iwl_rxon_context *ctx,
816 struct iwl_link_quality_cmd *lq)
817{
818 int i;
819
820 if (ctx->ht.enabled)
821 return true;
822
823 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
824 ctx->active.channel);
825 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
826 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
827 RATE_MCS_HT_MSK) {
828 IWL_DEBUG_INFO(priv,
829 "index %d of LQ expects HT channel\n",
830 i);
831 return false;
832 }
833 }
834 return true;
835}
836
837/**
838 * iwl_send_lq_cmd() - Send link quality command
839 * @init: This command is sent as part of station initialization right
840 * after station has been added.
841 *
842 * The link quality command is sent as the last step of station creation.
843 * This is the special case in which init is set and we call a callback in
844 * this case to clear the state indicating that station creation is in
845 * progress.
846 */
847int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
848 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
849{
850 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700851 struct iwl_host_cmd cmd = {
852 .id = REPLY_TX_LINK_QUALITY_CMD,
853 .len = { sizeof(struct iwl_link_quality_cmd), },
854 .flags = flags,
855 .data = { lq, },
856 };
857
858 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
859 return -EINVAL;
860
861
Johannes Bergfa23cb02012-03-05 11:24:25 -0800862 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700863 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800864 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700865 return -EINVAL;
866 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800867 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700868
869 iwl_dump_lq_cmd(priv, lq);
870 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
871 return -EINVAL;
872
873 if (is_lq_table_valid(priv, ctx, lq))
Johannes Berge10a0532012-03-06 13:30:39 -0800874 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700875 else
876 ret = -EINVAL;
877
878 if (cmd.flags & CMD_ASYNC)
879 return ret;
880
881 if (init) {
882 IWL_DEBUG_INFO(priv, "init LQ command complete, "
883 "clearing sta addition status for sta %d\n",
884 lq->sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800885 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700886 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800887 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700888 }
889 return ret;
890}
891
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700892
Johannes Bergc079166e2011-10-10 07:26:54 -0700893static struct iwl_link_quality_cmd *
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700894iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
895 u8 sta_id)
Johannes Bergc079166e2011-10-10 07:26:54 -0700896{
897 struct iwl_link_quality_cmd *link_cmd;
898
899 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
900 if (!link_cmd) {
901 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
902 return NULL;
903 }
904
905 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200906
907 return link_cmd;
908}
909
910/*
911 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
912 *
913 * Function sleeps.
914 */
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700915int iwlagn_add_bssid_station(struct iwl_priv *priv,
916 struct iwl_rxon_context *ctx,
Johannes Berga30e3112010-09-22 18:02:01 +0200917 const u8 *addr, u8 *sta_id_r)
918{
919 int ret;
920 u8 sta_id;
921 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +0200922
923 if (sta_id_r)
924 *sta_id_r = IWL_INVALID_STATION;
925
926 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
927 if (ret) {
928 IWL_ERR(priv, "Unable to add station %pM\n", addr);
929 return ret;
930 }
931
932 if (sta_id_r)
933 *sta_id_r = sta_id;
934
Johannes Bergfa23cb02012-03-05 11:24:25 -0800935 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200936 priv->stations[sta_id].used |= IWL_STA_LOCAL;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800937 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200938
939 /* Set up default rate scaling table in device's station table */
Johannes Bergf775aa06d2011-06-22 06:34:09 -0700940 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200941 if (!link_cmd) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700942 IWL_ERR(priv,
943 "Unable to initialize rate scaling for station %pM.\n",
Johannes Berga30e3112010-09-22 18:02:01 +0200944 addr);
945 return -ENOMEM;
946 }
947
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300948 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, 0, true);
Johannes Berga30e3112010-09-22 18:02:01 +0200949 if (ret)
950 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
951
Johannes Bergfa23cb02012-03-05 11:24:25 -0800952 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200953 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800954 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200955
956 return 0;
957}
958
Johannes Berg5a3d9882011-07-15 13:03:12 -0700959/*
960 * static WEP keys
961 *
962 * For each context, the device has a table of 4 static WEP keys
963 * (one for each key index) that is updated with the following
964 * commands.
965 */
966
Johannes Berga30e3112010-09-22 18:02:01 +0200967static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
968 struct iwl_rxon_context *ctx,
969 bool send_if_empty)
970{
971 int i, not_empty = 0;
972 u8 buff[sizeof(struct iwl_wep_cmd) +
973 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
974 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
975 size_t cmd_size = sizeof(struct iwl_wep_cmd);
976 struct iwl_host_cmd cmd = {
977 .id = ctx->wep_key_cmd,
Johannes Berg3fa50732011-05-04 07:50:38 -0700978 .data = { wep_cmd, },
Johannes Berga30e3112010-09-22 18:02:01 +0200979 };
980
981 might_sleep();
982
983 memset(wep_cmd, 0, cmd_size +
984 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
985
986 for (i = 0; i < WEP_KEYS_MAX ; i++) {
987 wep_cmd->key[i].key_index = i;
988 if (ctx->wep_keys[i].key_size) {
989 wep_cmd->key[i].key_offset = i;
990 not_empty = 1;
991 } else {
992 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
993 }
994
995 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
996 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
997 ctx->wep_keys[i].key_size);
998 }
999
1000 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
1001 wep_cmd->num_keys = WEP_KEYS_MAX;
1002
1003 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
1004
Johannes Berg3fa50732011-05-04 07:50:38 -07001005 cmd.len[0] = cmd_size;
Johannes Berga30e3112010-09-22 18:02:01 +02001006
1007 if (not_empty || send_if_empty)
Johannes Berge10a0532012-03-06 13:30:39 -08001008 return iwl_dvm_send_cmd(priv, &cmd);
Johannes Berga30e3112010-09-22 18:02:01 +02001009 else
1010 return 0;
1011}
1012
1013int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1014 struct iwl_rxon_context *ctx)
1015{
Johannes Bergb1eea292012-03-06 13:30:42 -08001016 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001017
1018 return iwl_send_static_wepkey_cmd(priv, ctx, false);
1019}
1020
1021int iwl_remove_default_wep_key(struct iwl_priv *priv,
1022 struct iwl_rxon_context *ctx,
1023 struct ieee80211_key_conf *keyconf)
1024{
1025 int ret;
1026
Johannes Bergb1eea292012-03-06 13:30:42 -08001027 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001028
1029 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1030 keyconf->keyidx);
1031
1032 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
Don Fry83626402012-03-07 09:52:37 -08001033 if (iwl_is_rfkill(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001034 IWL_DEBUG_WEP(priv,
1035 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berga30e3112010-09-22 18:02:01 +02001036 /* but keys in device are clear anyway so return success */
1037 return 0;
1038 }
1039 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1040 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1041 keyconf->keyidx, ret);
1042
1043 return ret;
1044}
1045
1046int iwl_set_default_wep_key(struct iwl_priv *priv,
1047 struct iwl_rxon_context *ctx,
1048 struct ieee80211_key_conf *keyconf)
1049{
1050 int ret;
1051
Johannes Bergb1eea292012-03-06 13:30:42 -08001052 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001053
1054 if (keyconf->keylen != WEP_KEY_LEN_128 &&
1055 keyconf->keylen != WEP_KEY_LEN_64) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001056 IWL_DEBUG_WEP(priv,
1057 "Bad WEP key length %d\n", keyconf->keylen);
Johannes Berga30e3112010-09-22 18:02:01 +02001058 return -EINVAL;
1059 }
1060
Johannes Berg5a3d9882011-07-15 13:03:12 -07001061 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
Johannes Berga30e3112010-09-22 18:02:01 +02001062
1063 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1064 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1065 keyconf->keylen);
1066
1067 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1068 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1069 keyconf->keylen, keyconf->keyidx, ret);
1070
1071 return ret;
1072}
1073
Johannes Berg5a3d9882011-07-15 13:03:12 -07001074/*
1075 * dynamic (per-station) keys
1076 *
1077 * The dynamic keys are a little more complicated. The device has
1078 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1079 * These are linked to stations by a table that contains an index
1080 * into the key table for each station/key index/{mcast,unicast},
1081 * i.e. it's basically an array of pointers like this:
1082 * key_offset_t key_mapping[NUM_STATIONS][4][2];
1083 * (it really works differently, but you can think of it as such)
1084 *
1085 * The key uploading and linking happens in the same command, the
1086 * add station command with STA_MODIFY_KEY_MASK.
1087 */
1088
1089static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1090 struct ieee80211_vif *vif,
1091 struct ieee80211_sta *sta)
1092{
1093 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001094
1095 if (sta)
Johannes Berg40e4e682012-03-05 11:24:22 -08001096 return iwl_sta_id(sta);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001097
1098 /*
1099 * The device expects GTKs for station interfaces to be
1100 * installed as GTKs for the AP station. If we have no
1101 * station ID, then use the ap_sta_id in that case.
1102 */
Johannes Berg40e4e682012-03-05 11:24:22 -08001103 if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1104 return vif_priv->ctx->ap_sta_id;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001105
Johannes Berg40e4e682012-03-05 11:24:22 -08001106 return IWL_INVALID_STATION;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001107}
1108
Johannes Berge1b1c082011-07-17 01:44:11 -07001109static int iwlagn_send_sta_key(struct iwl_priv *priv,
1110 struct ieee80211_key_conf *keyconf,
1111 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1112 u32 cmd_flags)
Johannes Berga30e3112010-09-22 18:02:01 +02001113{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001114 __le16 key_flags;
Johannes Berga30e3112010-09-22 18:02:01 +02001115 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001116 int i;
Johannes Berge1b1c082011-07-17 01:44:11 -07001117
Johannes Bergfa23cb02012-03-05 11:24:25 -08001118 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001119 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001120 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001121
Johannes Berg5a3d9882011-07-15 13:03:12 -07001122 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1123 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001124
Johannes Berg5a3d9882011-07-15 13:03:12 -07001125 switch (keyconf->cipher) {
1126 case WLAN_CIPHER_SUITE_CCMP:
1127 key_flags |= STA_KEY_FLG_CCMP;
1128 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1129 break;
1130 case WLAN_CIPHER_SUITE_TKIP:
1131 key_flags |= STA_KEY_FLG_TKIP;
1132 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1133 for (i = 0; i < 5; i++)
1134 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1135 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1136 break;
1137 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga30e3112010-09-22 18:02:01 +02001138 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001139 /* fall through */
1140 case WLAN_CIPHER_SUITE_WEP40:
1141 key_flags |= STA_KEY_FLG_WEP;
1142 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1143 break;
1144 default:
1145 WARN_ON(1);
1146 return -EINVAL;
1147 }
Johannes Berga30e3112010-09-22 18:02:01 +02001148
Johannes Berg5a3d9882011-07-15 13:03:12 -07001149 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
Johannes Berga30e3112010-09-22 18:02:01 +02001150 key_flags |= STA_KEY_MULTICAST_MSK;
1151
Johannes Berg5a3d9882011-07-15 13:03:12 -07001152 /* key pointer (offset) */
1153 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berga30e3112010-09-22 18:02:01 +02001154
Johannes Berg5a3d9882011-07-15 13:03:12 -07001155 sta_cmd.key.key_flags = key_flags;
1156 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1157 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
Johannes Berga30e3112010-09-22 18:02:01 +02001158
Johannes Berg5a3d9882011-07-15 13:03:12 -07001159 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001160}
1161
1162void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001163 struct ieee80211_vif *vif,
Johannes Berga30e3112010-09-22 18:02:01 +02001164 struct ieee80211_key_conf *keyconf,
1165 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1166{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001167 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1168
1169 if (sta_id == IWL_INVALID_STATION)
1170 return;
Johannes Berga30e3112010-09-22 18:02:01 +02001171
1172 if (iwl_scan_cancel(priv)) {
1173 /* cancel scan failed, just live w/ bad key and rely
1174 briefly on SW decryption */
1175 return;
1176 }
1177
Johannes Berge1b1c082011-07-17 01:44:11 -07001178 iwlagn_send_sta_key(priv, keyconf, sta_id,
1179 iv32, phase1key, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001180}
1181
1182int iwl_remove_dynamic_key(struct iwl_priv *priv,
1183 struct iwl_rxon_context *ctx,
1184 struct ieee80211_key_conf *keyconf,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001185 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001186{
Johannes Berga30e3112010-09-22 18:02:01 +02001187 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001188 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
Johannes Berg5dcbf482012-02-17 09:47:14 -08001189 __le16 key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001190
1191 /* if station isn't there, neither is the key */
1192 if (sta_id == IWL_INVALID_STATION)
1193 return -ENOENT;
1194
Johannes Bergfa23cb02012-03-05 11:24:25 -08001195 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001196 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1197 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1198 sta_id = IWL_INVALID_STATION;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001199 spin_unlock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001200
1201 if (sta_id == IWL_INVALID_STATION)
1202 return 0;
Johannes Berga30e3112010-09-22 18:02:01 +02001203
Johannes Bergb1eea292012-03-06 13:30:42 -08001204 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001205
1206 ctx->key_mapping_keys--;
1207
Johannes Berga30e3112010-09-22 18:02:01 +02001208 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1209 keyconf->keyidx, sta_id);
1210
Johannes Berg5a3d9882011-07-15 13:03:12 -07001211 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1212 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1213 keyconf->hw_key_idx);
Johannes Berga30e3112010-09-22 18:02:01 +02001214
Johannes Berg5dcbf482012-02-17 09:47:14 -08001215 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1216 key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1217 STA_KEY_FLG_INVALID;
1218
1219 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1220 key_flags |= STA_KEY_MULTICAST_MSK;
1221
1222 sta_cmd.key.key_flags = key_flags;
Emmanuel Grumbachd6ee27eb2012-06-06 09:13:36 +02001223 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001224 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1225 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001226
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001227 return iwl_send_add_sta(priv, &sta_cmd, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001228}
1229
Johannes Berg5a3d9882011-07-15 13:03:12 -07001230int iwl_set_dynamic_key(struct iwl_priv *priv,
1231 struct iwl_rxon_context *ctx,
1232 struct ieee80211_key_conf *keyconf,
1233 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001234{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001235 struct ieee80211_key_seq seq;
1236 u16 p1k[5];
Johannes Berga30e3112010-09-22 18:02:01 +02001237 int ret;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001238 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1239 const u8 *addr;
1240
1241 if (sta_id == IWL_INVALID_STATION)
1242 return -EINVAL;
Johannes Berga30e3112010-09-22 18:02:01 +02001243
Johannes Bergb1eea292012-03-06 13:30:42 -08001244 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001245
Johannes Berg5a3d9882011-07-15 13:03:12 -07001246 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1247 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1248 return -ENOSPC;
1249
Johannes Berga30e3112010-09-22 18:02:01 +02001250 ctx->key_mapping_keys++;
Johannes Berga30e3112010-09-22 18:02:01 +02001251
1252 switch (keyconf->cipher) {
Johannes Berga30e3112010-09-22 18:02:01 +02001253 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001254 if (sta)
1255 addr = sta->addr;
1256 else /* station mode case only */
1257 addr = ctx->active.bssid_addr;
1258
1259 /* pre-fill phase 1 key into device cache */
1260 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1261 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Berge1b1c082011-07-17 01:44:11 -07001262 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001263 seq.tkip.iv32, p1k, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001264 break;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001265 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berga30e3112010-09-22 18:02:01 +02001266 case WLAN_CIPHER_SUITE_WEP40:
1267 case WLAN_CIPHER_SUITE_WEP104:
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 0, NULL, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001270 break;
1271 default:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001272 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
Johannes Berga30e3112010-09-22 18:02:01 +02001273 ret = -EINVAL;
1274 }
1275
Johannes Berg5a3d9882011-07-15 13:03:12 -07001276 if (ret) {
1277 ctx->key_mapping_keys--;
1278 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1279 }
1280
1281 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 +02001282 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001283 sta ? sta->addr : NULL, ret);
Johannes Berga30e3112010-09-22 18:02:01 +02001284
1285 return ret;
1286}
1287
1288/**
1289 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1290 *
1291 * This adds the broadcast station into the driver's station table
1292 * and marks it driver active, so that it will be restored to the
1293 * device at the next best time.
1294 */
1295int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1296 struct iwl_rxon_context *ctx)
1297{
1298 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +02001299 u8 sta_id;
1300
Johannes Bergfa23cb02012-03-05 11:24:25 -08001301 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001302 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1303 if (sta_id == IWL_INVALID_STATION) {
1304 IWL_ERR(priv, "Unable to prepare broadcast station\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -08001305 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001306
1307 return -EINVAL;
1308 }
1309
1310 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1311 priv->stations[sta_id].used |= IWL_STA_BCAST;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001312 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001313
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001314 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001315 if (!link_cmd) {
1316 IWL_ERR(priv,
1317 "Unable to initialize rate scaling for bcast station.\n");
1318 return -ENOMEM;
1319 }
1320
Johannes Bergfa23cb02012-03-05 11:24:25 -08001321 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001322 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001323 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001324
1325 return 0;
1326}
1327
1328/**
1329 * iwl_update_bcast_station - update broadcast station's LQ command
1330 *
1331 * Only used by iwlagn. Placed here to have all bcast station management
1332 * code together.
1333 */
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001334int iwl_update_bcast_station(struct iwl_priv *priv,
1335 struct iwl_rxon_context *ctx)
Johannes Berga30e3112010-09-22 18:02:01 +02001336{
Johannes Berga30e3112010-09-22 18:02:01 +02001337 struct iwl_link_quality_cmd *link_cmd;
1338 u8 sta_id = ctx->bcast_sta_id;
1339
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001340 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001341 if (!link_cmd) {
1342 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1343 return -ENOMEM;
1344 }
1345
Johannes Bergfa23cb02012-03-05 11:24:25 -08001346 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001347 if (priv->stations[sta_id].lq)
1348 kfree(priv->stations[sta_id].lq);
1349 else
1350 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1351 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001352 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001353
1354 return 0;
1355}
1356
1357int iwl_update_bcast_stations(struct iwl_priv *priv)
1358{
1359 struct iwl_rxon_context *ctx;
1360 int ret = 0;
1361
1362 for_each_context(priv, ctx) {
1363 ret = iwl_update_bcast_station(priv, ctx);
1364 if (ret)
1365 break;
1366 }
1367
1368 return ret;
1369}
1370
1371/**
1372 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1373 */
1374int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1375{
Johannes Berga30e3112010-09-22 18:02:01 +02001376 struct iwl_addsta_cmd sta_cmd;
1377
Johannes Bergb1eea292012-03-06 13:30:42 -08001378 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001379
1380 /* Remove "disable" flag, to enable Tx for this TID */
Johannes Bergfa23cb02012-03-05 11:24:25 -08001381 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001382 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1383 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1384 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1385 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001386 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001387
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001388 return iwl_send_add_sta(priv, &sta_cmd, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001389}
1390
1391int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1392 int tid, u16 ssn)
1393{
Johannes Berga30e3112010-09-22 18:02:01 +02001394 int sta_id;
1395 struct iwl_addsta_cmd sta_cmd;
1396
Johannes Bergb1eea292012-03-06 13:30:42 -08001397 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001398
1399 sta_id = iwl_sta_id(sta);
1400 if (sta_id == IWL_INVALID_STATION)
1401 return -ENXIO;
1402
Johannes Bergfa23cb02012-03-05 11:24:25 -08001403 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001404 priv->stations[sta_id].sta.station_flags_msk = 0;
1405 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1406 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1407 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1408 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1409 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001410 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001411
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001412 return iwl_send_add_sta(priv, &sta_cmd, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001413}
1414
1415int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1416 int tid)
1417{
Johannes Berga30e3112010-09-22 18:02:01 +02001418 int sta_id;
1419 struct iwl_addsta_cmd sta_cmd;
1420
Johannes Bergb1eea292012-03-06 13:30:42 -08001421 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001422
1423 sta_id = iwl_sta_id(sta);
1424 if (sta_id == IWL_INVALID_STATION) {
1425 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1426 return -ENXIO;
1427 }
1428
Johannes Bergfa23cb02012-03-05 11:24:25 -08001429 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001430 priv->stations[sta_id].sta.station_flags_msk = 0;
1431 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1432 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1433 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1434 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001435 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001436
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001437 return iwl_send_add_sta(priv, &sta_cmd, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001438}
1439
Johannes Berga30e3112010-09-22 18:02:01 +02001440
Johannes Berga30e3112010-09-22 18:02:01 +02001441
1442void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1443{
Johannes Berg9451ca12012-03-05 11:24:23 -08001444 struct iwl_addsta_cmd cmd = {
1445 .mode = STA_CONTROL_MODIFY_MSK,
1446 .station_flags = STA_FLG_PWR_SAVE_MSK,
1447 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1448 .sta.sta_id = sta_id,
1449 .sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1450 .sleep_tx_count = cpu_to_le16(cnt),
1451 };
Johannes Berga30e3112010-09-22 18:02:01 +02001452
Johannes Berg9451ca12012-03-05 11:24:23 -08001453 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001454}