blob: 346077fad14ade2fdda3acad34aaa88074a0fa4e [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,
64 struct iwl_addsta_cmd *addsta,
65 struct iwl_rx_packet *pkt)
66{
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080067 struct iwl_add_sta_resp *add_sta_resp = (void *)pkt->data;
Wey-Yi Guyc745f552011-10-10 07:27:12 -070068 u8 sta_id = addsta->sta.sta_id;
Wey-Yi Guyc745f552011-10-10 07:27:12 -070069
70 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
71 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
72 pkt->hdr.flags);
Johannes Berg4492bb62015-06-23 20:56:45 +020073 return 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -070074 }
75
76 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
77 sta_id);
78
Johannes Berg2bfb5092012-12-27 21:43:48 +010079 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -070080
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080081 switch (add_sta_resp->status) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -070082 case ADD_STA_SUCCESS_MSK:
83 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
Wey-Yi Guyc745f552011-10-10 07:27:12 -070084 break;
85 case ADD_STA_NO_ROOM_IN_TABLE:
86 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
87 sta_id);
88 break;
89 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
90 IWL_ERR(priv, "Adding station %d failed, no block ack "
91 "resource.\n", sta_id);
92 break;
93 case ADD_STA_MODIFY_NON_EXIST_STA:
94 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
95 sta_id);
96 break;
97 default:
98 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080099 add_sta_resp->status);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700100 break;
101 }
102
103 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
104 priv->stations[sta_id].sta.mode ==
105 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
106 sta_id, priv->stations[sta_id].sta.sta.addr);
107
108 /*
109 * XXX: The MAC address in the command buffer is often changed from
110 * the original sent to the device. That is, the MAC address
111 * written to the command buffer often is not the same MAC address
112 * read from the command buffer when the command returns. This
113 * issue has not yet been resolved and this debugging is left to
114 * observe the problem.
115 */
116 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
117 priv->stations[sta_id].sta.mode ==
118 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
119 addsta->sta.addr);
Johannes Berg2bfb5092012-12-27 21:43:48 +0100120 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700121
Johannes Berg4492bb62015-06-23 20:56:45 +0200122 return 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700123}
124
Johannes Berg48a2d662012-03-05 11:24:39 -0800125int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700126 struct iwl_device_cmd *cmd)
127{
128 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700129
Johannes Berg513aa3b2012-09-20 16:06:14 +0200130 if (!cmd)
131 return 0;
132
133 return iwl_process_add_sta_resp(priv, (void *)cmd->payload, pkt);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700134}
135
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700136int iwl_send_add_sta(struct iwl_priv *priv,
137 struct iwl_addsta_cmd *sta, u8 flags)
138{
139 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700140 struct iwl_host_cmd cmd = {
141 .id = REPLY_ADD_STA,
142 .flags = flags,
Johannes Berg69b172f2011-12-12 04:17:44 -0800143 .data = { sta, },
144 .len = { sizeof(*sta), },
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700145 };
146 u8 sta_id __maybe_unused = sta->sta.sta_id;
Johannes Berg4492bb62015-06-23 20:56:45 +0200147 struct iwl_rx_packet *pkt;
148 struct iwl_add_sta_resp *add_sta_resp;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700149
150 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
151 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
152
153 if (!(flags & CMD_ASYNC)) {
Johannes Berg38c0f3342013-02-27 13:18:50 +0100154 cmd.flags |= CMD_WANT_SKB;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700155 might_sleep();
156 }
157
Johannes Berge10a0532012-03-06 13:30:39 -0800158 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700159
160 if (ret || (flags & CMD_ASYNC))
161 return ret;
Johannes Berg4492bb62015-06-23 20:56:45 +0200162
163 pkt = cmd.resp_pkt;
164 add_sta_resp = (void *)pkt->data;
165
166 /* debug messages are printed in the handler */
167 if (!(pkt->hdr.flags & IWL_CMD_FAILED_MSK) &&
168 add_sta_resp->status == ADD_STA_SUCCESS_MSK) {
169 spin_lock_bh(&priv->sta_lock);
170 ret = iwl_sta_ucode_activate(priv, sta_id);
171 spin_unlock_bh(&priv->sta_lock);
172 } else {
173 ret = -EIO;
174 }
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700175
Johannes Berg65b94a42012-03-05 11:24:38 -0800176 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700177
Johannes Berg4492bb62015-06-23 20:56:45 +0200178 return ret;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700179}
180
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700181bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
182 struct iwl_rxon_context *ctx,
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100183 struct ieee80211_sta *sta)
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700184{
185 if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
186 return false;
187
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700188#ifdef CONFIG_IWLWIFI_DEBUGFS
189 if (priv->disable_ht40)
190 return false;
191#endif
192
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100193 /* special case for RXON */
194 if (!sta)
Johannes Berg26a7ca92012-05-21 11:55:54 +0200195 return true;
196
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100197 return sta->bandwidth >= IEEE80211_STA_RX_BW_40;
Meenakshi Venkataraman5edc565d62012-03-13 16:15:09 -0700198}
199
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800200static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
201 struct ieee80211_sta *sta,
202 struct iwl_rxon_context *ctx,
203 __le32 *flags, __le32 *mask)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700204{
205 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700206
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800207 *mask = STA_FLG_RTS_MIMO_PROT_MSK |
208 STA_FLG_MIMO_DIS_MSK |
209 STA_FLG_HT40_EN_MSK |
210 STA_FLG_MAX_AGG_SIZE_MSK |
211 STA_FLG_AGG_MPDU_DENSITY_MSK;
212 *flags = 0;
213
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700214 if (!sta || !sta_ht_inf->ht_supported)
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800215 return;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700216
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800217 IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",
Meenakshi Venkataramana35e2702012-05-16 22:40:50 +0200218 sta->addr,
Johannes Bergaf0ed692013-02-12 14:21:00 +0100219 (sta->smps_mode == IEEE80211_SMPS_STATIC) ?
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700220 "static" :
Johannes Bergaf0ed692013-02-12 14:21:00 +0100221 (sta->smps_mode == IEEE80211_SMPS_DYNAMIC) ?
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700222 "dynamic" : "disabled");
223
Johannes Bergaf0ed692013-02-12 14:21:00 +0100224 switch (sta->smps_mode) {
225 case IEEE80211_SMPS_STATIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800226 *flags |= STA_FLG_MIMO_DIS_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700227 break;
Johannes Bergaf0ed692013-02-12 14:21:00 +0100228 case IEEE80211_SMPS_DYNAMIC:
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800229 *flags |= STA_FLG_RTS_MIMO_PROT_MSK;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700230 break;
Johannes Bergaf0ed692013-02-12 14:21:00 +0100231 case IEEE80211_SMPS_OFF:
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700232 break;
233 default:
Johannes Bergaf0ed692013-02-12 14:21:00 +0100234 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", sta->smps_mode);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700235 break;
236 }
237
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800238 *flags |= cpu_to_le32(
239 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700240
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800241 *flags |= cpu_to_le32(
242 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700243
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100244 if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800245 *flags |= STA_FLG_HT40_EN_MSK;
246}
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700247
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800248int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
249 struct ieee80211_sta *sta)
250{
251 u8 sta_id = iwl_sta_id(sta);
252 __le32 flags, mask;
253 struct iwl_addsta_cmd cmd;
254
255 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
256 return -EINVAL;
257
258 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
259
260 spin_lock_bh(&priv->sta_lock);
261 priv->stations[sta_id].sta.station_flags &= ~mask;
262 priv->stations[sta_id].sta.station_flags |= flags;
263 spin_unlock_bh(&priv->sta_lock);
264
265 memset(&cmd, 0, sizeof(cmd));
266 cmd.mode = STA_CONTROL_MODIFY_MSK;
267 cmd.station_flags_msk = mask;
268 cmd.station_flags = flags;
269 cmd.sta.sta_id = sta_id;
270
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300271 return iwl_send_add_sta(priv, &cmd, 0);
Johannes Bergab0bd5b2012-03-05 11:24:47 -0800272}
273
274static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
275 struct ieee80211_sta *sta,
276 struct iwl_rxon_context *ctx)
277{
278 __le32 flags, mask;
279
280 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
281
282 lockdep_assert_held(&priv->sta_lock);
283 priv->stations[index].sta.station_flags &= ~mask;
284 priv->stations[index].sta.station_flags |= flags;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700285}
286
287/**
288 * iwl_prep_station - Prepare station information for addition
289 *
290 * should be called with sta_lock held
291 */
292u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
293 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
294{
295 struct iwl_station_entry *station;
296 int i;
297 u8 sta_id = IWL_INVALID_STATION;
298
299 if (is_ap)
300 sta_id = ctx->ap_sta_id;
301 else if (is_broadcast_ether_addr(addr))
302 sta_id = ctx->bcast_sta_id;
303 else
304 for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
Joe Perches2e42e472012-05-09 17:17:46 +0000305 if (ether_addr_equal(priv->stations[i].sta.sta.addr,
306 addr)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700307 sta_id = i;
308 break;
309 }
310
311 if (!priv->stations[i].used &&
312 sta_id == IWL_INVALID_STATION)
313 sta_id = i;
314 }
315
316 /*
317 * These two conditions have the same outcome, but keep them
318 * separate
319 */
320 if (unlikely(sta_id == IWL_INVALID_STATION))
321 return sta_id;
322
323 /*
324 * uCode is not able to deal with multiple requests to add a
325 * station. Keep track if one is in progress so that we do not send
326 * another.
327 */
328 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
329 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
330 "added.\n", sta_id);
331 return sta_id;
332 }
333
334 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
335 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
Joe Perches2e42e472012-05-09 17:17:46 +0000336 ether_addr_equal(priv->stations[sta_id].sta.sta.addr, addr)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700337 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
338 "adding again.\n", sta_id, addr);
339 return sta_id;
340 }
341
342 station = &priv->stations[sta_id];
343 station->used = IWL_STA_DRIVER_ACTIVE;
344 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
345 sta_id, addr);
346 priv->num_stations++;
347
348 /* Set up the REPLY_ADD_STA command to send to device */
349 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
350 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
351 station->sta.mode = 0;
352 station->sta.sta.sta_id = sta_id;
353 station->sta.station_flags = ctx->station_flags;
354 station->ctxid = ctx->ctxid;
355
356 if (sta) {
357 struct iwl_station_priv *sta_priv;
358
359 sta_priv = (void *)sta->drv_priv;
360 sta_priv->ctx = ctx;
361 }
362
363 /*
364 * OK to call unconditionally, since local stations (IBSS BSSID
365 * STA and broadcast STA) pass in a NULL sta, and mac80211
366 * doesn't allow HT IBSS.
367 */
368 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
369
370 return sta_id;
371
372}
373
374#define STA_WAIT_TIMEOUT (HZ/2)
375
376/**
377 * iwl_add_station_common -
378 */
379int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
380 const u8 *addr, bool is_ap,
381 struct ieee80211_sta *sta, u8 *sta_id_r)
382{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700383 int ret = 0;
384 u8 sta_id;
385 struct iwl_addsta_cmd sta_cmd;
386
387 *sta_id_r = 0;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800388 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700389 sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
390 if (sta_id == IWL_INVALID_STATION) {
391 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
392 addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800393 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700394 return -EINVAL;
395 }
396
397 /*
398 * uCode is not able to deal with multiple requests to add a
399 * station. Keep track if one is in progress so that we do not send
400 * another.
401 */
402 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
403 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
404 "added.\n", sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800405 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700406 return -EEXIST;
407 }
408
409 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
410 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
411 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
412 "adding again.\n", sta_id, addr);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800413 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700414 return -EEXIST;
415 }
416
417 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
418 memcpy(&sta_cmd, &priv->stations[sta_id].sta,
419 sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -0800420 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700421
422 /* Add station to device's station table */
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300423 ret = iwl_send_add_sta(priv, &sta_cmd, 0);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700424 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800425 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700426 IWL_ERR(priv, "Adding station %pM failed.\n",
427 priv->stations[sta_id].sta.sta.addr);
428 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
429 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800430 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700431 }
432 *sta_id_r = sta_id;
433 return ret;
434}
435
436/**
437 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700438 */
439static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
440{
Johannes Bergfa23cb02012-03-05 11:24:25 -0800441 lockdep_assert_held(&priv->sta_lock);
442
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700443 /* Ucode must be active and driver must be non active */
444 if ((priv->stations[sta_id].used &
445 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
446 IWL_STA_UCODE_ACTIVE)
447 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
448
449 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
450
451 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
452 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
453}
454
455static int iwl_send_remove_station(struct iwl_priv *priv,
456 const u8 *addr, int sta_id,
457 bool temporary)
458{
459 struct iwl_rx_packet *pkt;
460 int ret;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700461 struct iwl_rem_sta_cmd rm_sta_cmd;
462
463 struct iwl_host_cmd cmd = {
464 .id = REPLY_REMOVE_STA,
465 .len = { sizeof(struct iwl_rem_sta_cmd), },
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700466 .data = { &rm_sta_cmd, },
467 };
468
469 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
470 rm_sta_cmd.num_sta = 1;
471 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
472
473 cmd.flags |= CMD_WANT_SKB;
474
Johannes Berge10a0532012-03-06 13:30:39 -0800475 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700476
477 if (ret)
478 return ret;
479
Johannes Berg65b94a42012-03-05 11:24:38 -0800480 pkt = cmd.resp_pkt;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700481 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
482 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
483 pkt->hdr.flags);
484 ret = -EIO;
485 }
486
487 if (!ret) {
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800488 struct iwl_rem_sta_resp *rem_sta_resp = (void *)pkt->data;
489 switch (rem_sta_resp->status) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700490 case REM_STA_SUCCESS_MSK:
491 if (!temporary) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800492 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700493 iwl_sta_ucode_deactivate(priv, sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800494 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700495 }
496 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
497 break;
498 default:
499 ret = -EIO;
500 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
501 break;
502 }
503 }
Johannes Berg65b94a42012-03-05 11:24:38 -0800504 iwl_free_resp(&cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700505
506 return ret;
507}
508
509/**
510 * iwl_remove_station - Remove driver's knowledge of station.
511 */
512int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
513 const u8 *addr)
514{
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200515 u8 tid;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700516
Don Fry83626402012-03-07 09:52:37 -0800517 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700518 IWL_DEBUG_INFO(priv,
519 "Unable to remove station %pM, device not ready.\n",
520 addr);
521 /*
522 * It is typical for stations to be removed when we are
523 * going down. Return success since device will be down
524 * soon anyway
525 */
526 return 0;
527 }
528
529 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
530 sta_id, addr);
531
532 if (WARN_ON(sta_id == IWL_INVALID_STATION))
533 return -EINVAL;
534
Johannes Bergfa23cb02012-03-05 11:24:25 -0800535 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700536
537 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
538 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
539 addr);
540 goto out_err;
541 }
542
543 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
544 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
545 addr);
546 goto out_err;
547 }
548
549 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
550 kfree(priv->stations[sta_id].lq);
551 priv->stations[sta_id].lq = NULL;
552 }
553
Emmanuel Grumbach855c2ee2011-11-23 11:37:27 +0200554 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
555 memset(&priv->tid_data[sta_id][tid], 0,
556 sizeof(priv->tid_data[sta_id][tid]));
557
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700558 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
559
560 priv->num_stations--;
561
562 if (WARN_ON(priv->num_stations < 0))
563 priv->num_stations = 0;
564
Johannes Bergfa23cb02012-03-05 11:24:25 -0800565 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700566
567 return iwl_send_remove_station(priv, addr, sta_id, false);
568out_err:
Johannes Bergfa23cb02012-03-05 11:24:25 -0800569 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700570 return -EINVAL;
571}
572
Johannes Berg97420512012-03-07 09:52:42 -0800573void iwl_deactivate_station(struct iwl_priv *priv, const u8 sta_id,
574 const u8 *addr)
575{
576 u8 tid;
577
578 if (!iwl_is_ready(priv)) {
579 IWL_DEBUG_INFO(priv,
580 "Unable to remove station %pM, device not ready.\n",
581 addr);
582 return;
583 }
584
585 IWL_DEBUG_ASSOC(priv, "Deactivating STA: %pM (%d)\n", addr, sta_id);
586
587 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
588 return;
589
590 spin_lock_bh(&priv->sta_lock);
591
592 WARN_ON_ONCE(!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE));
593
594 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
595 memset(&priv->tid_data[sta_id][tid], 0,
596 sizeof(priv->tid_data[sta_id][tid]));
597
598 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
Emmanuel Grumbachec6f6782014-02-18 10:30:18 +0200599 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Berg97420512012-03-07 09:52:42 -0800600
601 priv->num_stations--;
602
603 if (WARN_ON_ONCE(priv->num_stations < 0))
604 priv->num_stations = 0;
605
606 spin_unlock_bh(&priv->sta_lock);
607}
608
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100609static void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
610 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
611{
612 int i, r;
613 u32 rate_flags = 0;
614 __le32 rate_n_flags;
615
616 lockdep_assert_held(&priv->mutex);
617
618 memset(link_cmd, 0, sizeof(*link_cmd));
619
620 /* Set up the rate scaling to start at selected rate, fall back
621 * all the way down to 1M in IEEE order, and then spin on 1M */
622 if (priv->band == IEEE80211_BAND_5GHZ)
623 r = IWL_RATE_6M_INDEX;
624 else if (ctx && ctx->vif && ctx->vif->p2p)
625 r = IWL_RATE_6M_INDEX;
626 else
627 r = IWL_RATE_1M_INDEX;
628
629 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
630 rate_flags |= RATE_MCS_CCK_MSK;
631
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200632 rate_flags |= first_antenna(priv->nvm_data->valid_tx_ant) <<
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100633 RATE_MCS_ANT_POS;
634 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
635 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
636 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
637
638 link_cmd->general_params.single_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200639 first_antenna(priv->nvm_data->valid_tx_ant);
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100640
641 link_cmd->general_params.dual_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200642 priv->nvm_data->valid_tx_ant &
643 ~first_antenna(priv->nvm_data->valid_tx_ant);
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100644 if (!link_cmd->general_params.dual_stream_ant_msk) {
645 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200646 } else if (num_of_ant(priv->nvm_data->valid_tx_ant) == 2) {
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100647 link_cmd->general_params.dual_stream_ant_msk =
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200648 priv->nvm_data->valid_tx_ant;
Johannes Berg4dcba6d2012-03-08 10:19:55 +0100649 }
650
651 link_cmd->agg_params.agg_dis_start_th =
652 LINK_QUAL_AGG_DISABLE_START_DEF;
653 link_cmd->agg_params.agg_time_limit =
654 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
655
656 link_cmd->sta_id = sta_id;
657}
658
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700659/**
660 * iwl_clear_ucode_stations - clear ucode station table bits
661 *
662 * This function clears all the bits in the driver indicating
663 * which stations are active in the ucode. Call when something
664 * other than explicit station management would cause this in
665 * the ucode, e.g. unassociated RXON.
666 */
667void iwl_clear_ucode_stations(struct iwl_priv *priv,
668 struct iwl_rxon_context *ctx)
669{
670 int i;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700671 bool cleared = false;
672
673 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
674
Johannes Bergfa23cb02012-03-05 11:24:25 -0800675 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700676 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
677 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
678 continue;
679
680 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
681 IWL_DEBUG_INFO(priv,
682 "Clearing ucode active for station %d\n", i);
683 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
684 cleared = true;
685 }
686 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800687 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700688
689 if (!cleared)
690 IWL_DEBUG_INFO(priv,
691 "No active stations found to be cleared\n");
692}
693
694/**
695 * iwl_restore_stations() - Restore driver known stations to device
696 *
697 * All stations considered active by driver, but not present in ucode, is
698 * restored.
699 *
700 * Function sleeps.
701 */
702void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
703{
704 struct iwl_addsta_cmd sta_cmd;
Emmanuel Grumbach63b77bf2013-04-17 09:47:00 +0300705 static const struct iwl_link_quality_cmd zero_lq = {};
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700706 struct iwl_link_quality_cmd lq;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700707 int i;
708 bool found = false;
709 int ret;
710 bool send_lq;
711
Don Fry83626402012-03-07 09:52:37 -0800712 if (!iwl_is_ready(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700713 IWL_DEBUG_INFO(priv,
714 "Not ready yet, not restoring any stations.\n");
715 return;
716 }
717
718 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -0800719 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700720 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
721 if (ctx->ctxid != priv->stations[i].ctxid)
722 continue;
723 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
724 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
725 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
726 priv->stations[i].sta.sta.addr);
727 priv->stations[i].sta.mode = 0;
728 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
729 found = true;
730 }
731 }
732
733 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
734 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
735 memcpy(&sta_cmd, &priv->stations[i].sta,
736 sizeof(struct iwl_addsta_cmd));
737 send_lq = false;
738 if (priv->stations[i].lq) {
Johannes Berg15b86bf2012-03-05 11:24:36 -0800739 if (priv->wowlan)
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700740 iwl_sta_fill_lq(priv, ctx, i, &lq);
741 else
742 memcpy(&lq, priv->stations[i].lq,
743 sizeof(struct iwl_link_quality_cmd));
Emmanuel Grumbach63b77bf2013-04-17 09:47:00 +0300744
Emmanuel Grumbacha8778362013-05-22 10:48:10 +0300745 if (memcmp(&lq, &zero_lq, sizeof(lq)))
Emmanuel Grumbach63b77bf2013-04-17 09:47:00 +0300746 send_lq = true;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700747 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800748 spin_unlock_bh(&priv->sta_lock);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300749 ret = iwl_send_add_sta(priv, &sta_cmd, 0);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700750 if (ret) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800751 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700752 IWL_ERR(priv, "Adding station %pM failed.\n",
753 priv->stations[i].sta.sta.addr);
754 priv->stations[i].used &=
755 ~IWL_STA_DRIVER_ACTIVE;
756 priv->stations[i].used &=
757 ~IWL_STA_UCODE_INPROGRESS;
Meenakshi Venkataramanf4c37172012-05-16 22:49:48 +0200758 continue;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700759 }
760 /*
761 * Rate scaling has already been initialized, send
762 * current LQ command
763 */
764 if (send_lq)
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300765 iwl_send_lq_cmd(priv, ctx, &lq, 0, true);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800766 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700767 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
768 }
769 }
770
Johannes Bergfa23cb02012-03-05 11:24:25 -0800771 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700772 if (!found)
773 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
774 "no stations to be restored.\n");
775 else
776 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
777 "complete.\n");
778}
779
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700780int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
781{
782 int i;
783
784 for (i = 0; i < priv->sta_key_max_num; i++)
785 if (!test_and_set_bit(i, &priv->ucode_key_table))
786 return i;
787
788 return WEP_INVALID_OFFSET;
789}
790
791void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
792{
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700793 int i;
794
Johannes Bergfa23cb02012-03-05 11:24:25 -0800795 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700796 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
797 if (!(priv->stations[i].used & IWL_STA_BCAST))
798 continue;
799
800 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
801 priv->num_stations--;
802 if (WARN_ON(priv->num_stations < 0))
803 priv->num_stations = 0;
804 kfree(priv->stations[i].lq);
805 priv->stations[i].lq = NULL;
806 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800807 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700808}
809
810#ifdef CONFIG_IWLWIFI_DEBUG
811static void iwl_dump_lq_cmd(struct iwl_priv *priv,
812 struct iwl_link_quality_cmd *lq)
813{
814 int i;
815 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
816 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
817 lq->general_params.single_stream_ant_msk,
818 lq->general_params.dual_stream_ant_msk);
819
820 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
821 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
822 i, lq->rs_table[i].rate_n_flags);
823}
824#else
825static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
826 struct iwl_link_quality_cmd *lq)
827{
828}
829#endif
830
831/**
832 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
833 *
834 * It sometimes happens when a HT rate has been in use and we
835 * loose connectivity with AP then mac80211 will first tell us that the
836 * current channel is not HT anymore before removing the station. In such a
837 * scenario the RXON flags will be updated to indicate we are not
838 * communicating HT anymore, but the LQ command may still contain HT rates.
839 * Test for this to prevent driver from sending LQ command between the time
840 * RXON flags are updated and when LQ command is updated.
841 */
842static bool is_lq_table_valid(struct iwl_priv *priv,
843 struct iwl_rxon_context *ctx,
844 struct iwl_link_quality_cmd *lq)
845{
846 int i;
847
848 if (ctx->ht.enabled)
849 return true;
850
851 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
852 ctx->active.channel);
853 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
854 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
855 RATE_MCS_HT_MSK) {
856 IWL_DEBUG_INFO(priv,
857 "index %d of LQ expects HT channel\n",
858 i);
859 return false;
860 }
861 }
862 return true;
863}
864
865/**
866 * iwl_send_lq_cmd() - Send link quality command
867 * @init: This command is sent as part of station initialization right
868 * after station has been added.
869 *
870 * The link quality command is sent as the last step of station creation.
871 * This is the special case in which init is set and we call a callback in
872 * this case to clear the state indicating that station creation is in
873 * progress.
874 */
875int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
876 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
877{
878 int ret = 0;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700879 struct iwl_host_cmd cmd = {
880 .id = REPLY_TX_LINK_QUALITY_CMD,
881 .len = { sizeof(struct iwl_link_quality_cmd), },
882 .flags = flags,
883 .data = { lq, },
884 };
885
886 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
887 return -EINVAL;
888
889
Johannes Bergfa23cb02012-03-05 11:24:25 -0800890 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700891 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Johannes Bergfa23cb02012-03-05 11:24:25 -0800892 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700893 return -EINVAL;
894 }
Johannes Bergfa23cb02012-03-05 11:24:25 -0800895 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700896
897 iwl_dump_lq_cmd(priv, lq);
898 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
899 return -EINVAL;
900
901 if (is_lq_table_valid(priv, ctx, lq))
Johannes Berge10a0532012-03-06 13:30:39 -0800902 ret = iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700903 else
904 ret = -EINVAL;
905
906 if (cmd.flags & CMD_ASYNC)
907 return ret;
908
909 if (init) {
910 IWL_DEBUG_INFO(priv, "init LQ command complete, "
911 "clearing sta addition status for sta %d\n",
912 lq->sta_id);
Johannes Bergfa23cb02012-03-05 11:24:25 -0800913 spin_lock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700914 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800915 spin_unlock_bh(&priv->sta_lock);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700916 }
917 return ret;
918}
919
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700920
Johannes Bergc079166e2011-10-10 07:26:54 -0700921static struct iwl_link_quality_cmd *
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700922iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
923 u8 sta_id)
Johannes Bergc079166e2011-10-10 07:26:54 -0700924{
925 struct iwl_link_quality_cmd *link_cmd;
926
927 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
928 if (!link_cmd) {
929 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
930 return NULL;
931 }
932
933 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200934
935 return link_cmd;
936}
937
938/*
939 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
940 *
941 * Function sleeps.
942 */
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700943int iwlagn_add_bssid_station(struct iwl_priv *priv,
944 struct iwl_rxon_context *ctx,
Johannes Berga30e3112010-09-22 18:02:01 +0200945 const u8 *addr, u8 *sta_id_r)
946{
947 int ret;
948 u8 sta_id;
949 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +0200950
951 if (sta_id_r)
952 *sta_id_r = IWL_INVALID_STATION;
953
954 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
955 if (ret) {
956 IWL_ERR(priv, "Unable to add station %pM\n", addr);
957 return ret;
958 }
959
960 if (sta_id_r)
961 *sta_id_r = sta_id;
962
Johannes Bergfa23cb02012-03-05 11:24:25 -0800963 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200964 priv->stations[sta_id].used |= IWL_STA_LOCAL;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800965 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200966
967 /* Set up default rate scaling table in device's station table */
Johannes Bergf775aa06d2011-06-22 06:34:09 -0700968 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200969 if (!link_cmd) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700970 IWL_ERR(priv,
971 "Unable to initialize rate scaling for station %pM.\n",
Johannes Berga30e3112010-09-22 18:02:01 +0200972 addr);
973 return -ENOMEM;
974 }
975
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300976 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, 0, true);
Johannes Berga30e3112010-09-22 18:02:01 +0200977 if (ret)
978 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
979
Johannes Bergfa23cb02012-03-05 11:24:25 -0800980 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200981 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -0800982 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +0200983
984 return 0;
985}
986
Johannes Berg5a3d9882011-07-15 13:03:12 -0700987/*
988 * static WEP keys
989 *
990 * For each context, the device has a table of 4 static WEP keys
991 * (one for each key index) that is updated with the following
992 * commands.
993 */
994
Johannes Berga30e3112010-09-22 18:02:01 +0200995static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
996 struct iwl_rxon_context *ctx,
997 bool send_if_empty)
998{
999 int i, not_empty = 0;
1000 u8 buff[sizeof(struct iwl_wep_cmd) +
1001 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
1002 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
1003 size_t cmd_size = sizeof(struct iwl_wep_cmd);
1004 struct iwl_host_cmd cmd = {
1005 .id = ctx->wep_key_cmd,
Johannes Berg3fa50732011-05-04 07:50:38 -07001006 .data = { wep_cmd, },
Johannes Berga30e3112010-09-22 18:02:01 +02001007 };
1008
1009 might_sleep();
1010
1011 memset(wep_cmd, 0, cmd_size +
1012 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
1013
1014 for (i = 0; i < WEP_KEYS_MAX ; i++) {
1015 wep_cmd->key[i].key_index = i;
1016 if (ctx->wep_keys[i].key_size) {
1017 wep_cmd->key[i].key_offset = i;
1018 not_empty = 1;
1019 } else {
1020 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
1021 }
1022
1023 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
1024 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
1025 ctx->wep_keys[i].key_size);
1026 }
1027
1028 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
1029 wep_cmd->num_keys = WEP_KEYS_MAX;
1030
1031 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
1032
Johannes Berg3fa50732011-05-04 07:50:38 -07001033 cmd.len[0] = cmd_size;
Johannes Berga30e3112010-09-22 18:02:01 +02001034
1035 if (not_empty || send_if_empty)
Johannes Berge10a0532012-03-06 13:30:39 -08001036 return iwl_dvm_send_cmd(priv, &cmd);
Johannes Berga30e3112010-09-22 18:02:01 +02001037 else
1038 return 0;
1039}
1040
1041int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1042 struct iwl_rxon_context *ctx)
1043{
Johannes Bergb1eea292012-03-06 13:30:42 -08001044 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001045
1046 return iwl_send_static_wepkey_cmd(priv, ctx, false);
1047}
1048
1049int iwl_remove_default_wep_key(struct iwl_priv *priv,
1050 struct iwl_rxon_context *ctx,
1051 struct ieee80211_key_conf *keyconf)
1052{
1053 int ret;
1054
Johannes Bergb1eea292012-03-06 13:30:42 -08001055 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001056
1057 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1058 keyconf->keyidx);
1059
1060 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
Don Fry83626402012-03-07 09:52:37 -08001061 if (iwl_is_rfkill(priv)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001062 IWL_DEBUG_WEP(priv,
1063 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berga30e3112010-09-22 18:02:01 +02001064 /* but keys in device are clear anyway so return success */
1065 return 0;
1066 }
1067 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1068 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1069 keyconf->keyidx, ret);
1070
1071 return ret;
1072}
1073
1074int iwl_set_default_wep_key(struct iwl_priv *priv,
1075 struct iwl_rxon_context *ctx,
1076 struct ieee80211_key_conf *keyconf)
1077{
1078 int ret;
1079
Johannes Bergb1eea292012-03-06 13:30:42 -08001080 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001081
1082 if (keyconf->keylen != WEP_KEY_LEN_128 &&
1083 keyconf->keylen != WEP_KEY_LEN_64) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001084 IWL_DEBUG_WEP(priv,
1085 "Bad WEP key length %d\n", keyconf->keylen);
Johannes Berga30e3112010-09-22 18:02:01 +02001086 return -EINVAL;
1087 }
1088
Johannes Berg5a3d9882011-07-15 13:03:12 -07001089 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
Johannes Berga30e3112010-09-22 18:02:01 +02001090
1091 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1092 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1093 keyconf->keylen);
1094
1095 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1096 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1097 keyconf->keylen, keyconf->keyidx, ret);
1098
1099 return ret;
1100}
1101
Johannes Berg5a3d9882011-07-15 13:03:12 -07001102/*
1103 * dynamic (per-station) keys
1104 *
1105 * The dynamic keys are a little more complicated. The device has
1106 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1107 * These are linked to stations by a table that contains an index
1108 * into the key table for each station/key index/{mcast,unicast},
1109 * i.e. it's basically an array of pointers like this:
1110 * key_offset_t key_mapping[NUM_STATIONS][4][2];
1111 * (it really works differently, but you can think of it as such)
1112 *
1113 * The key uploading and linking happens in the same command, the
1114 * add station command with STA_MODIFY_KEY_MASK.
1115 */
1116
1117static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1118 struct ieee80211_vif *vif,
1119 struct ieee80211_sta *sta)
1120{
1121 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001122
1123 if (sta)
Johannes Berg40e4e682012-03-05 11:24:22 -08001124 return iwl_sta_id(sta);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001125
1126 /*
1127 * The device expects GTKs for station interfaces to be
1128 * installed as GTKs for the AP station. If we have no
1129 * station ID, then use the ap_sta_id in that case.
1130 */
Johannes Berg40e4e682012-03-05 11:24:22 -08001131 if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1132 return vif_priv->ctx->ap_sta_id;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001133
Johannes Berg40e4e682012-03-05 11:24:22 -08001134 return IWL_INVALID_STATION;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001135}
1136
Johannes Berge1b1c082011-07-17 01:44:11 -07001137static int iwlagn_send_sta_key(struct iwl_priv *priv,
1138 struct ieee80211_key_conf *keyconf,
1139 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1140 u32 cmd_flags)
Johannes Berga30e3112010-09-22 18:02:01 +02001141{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001142 __le16 key_flags;
Johannes Berga30e3112010-09-22 18:02:01 +02001143 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001144 int i;
Johannes Berge1b1c082011-07-17 01:44:11 -07001145
Johannes Bergfa23cb02012-03-05 11:24:25 -08001146 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001147 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001148 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001149
Johannes Berg5a3d9882011-07-15 13:03:12 -07001150 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1151 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001152
Johannes Berg5a3d9882011-07-15 13:03:12 -07001153 switch (keyconf->cipher) {
1154 case WLAN_CIPHER_SUITE_CCMP:
1155 key_flags |= STA_KEY_FLG_CCMP;
1156 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1157 break;
1158 case WLAN_CIPHER_SUITE_TKIP:
1159 key_flags |= STA_KEY_FLG_TKIP;
1160 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1161 for (i = 0; i < 5; i++)
1162 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1163 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1164 break;
1165 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga30e3112010-09-22 18:02:01 +02001166 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001167 /* fall through */
1168 case WLAN_CIPHER_SUITE_WEP40:
1169 key_flags |= STA_KEY_FLG_WEP;
1170 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1171 break;
1172 default:
1173 WARN_ON(1);
1174 return -EINVAL;
1175 }
Johannes Berga30e3112010-09-22 18:02:01 +02001176
Johannes Berg5a3d9882011-07-15 13:03:12 -07001177 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
Johannes Berga30e3112010-09-22 18:02:01 +02001178 key_flags |= STA_KEY_MULTICAST_MSK;
1179
Johannes Berg5a3d9882011-07-15 13:03:12 -07001180 /* key pointer (offset) */
1181 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berga30e3112010-09-22 18:02:01 +02001182
Johannes Berg5a3d9882011-07-15 13:03:12 -07001183 sta_cmd.key.key_flags = key_flags;
1184 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1185 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
Johannes Berga30e3112010-09-22 18:02:01 +02001186
Johannes Berg5a3d9882011-07-15 13:03:12 -07001187 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001188}
1189
1190void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001191 struct ieee80211_vif *vif,
Johannes Berga30e3112010-09-22 18:02:01 +02001192 struct ieee80211_key_conf *keyconf,
1193 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1194{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001195 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1196
1197 if (sta_id == IWL_INVALID_STATION)
1198 return;
Johannes Berga30e3112010-09-22 18:02:01 +02001199
1200 if (iwl_scan_cancel(priv)) {
1201 /* cancel scan failed, just live w/ bad key and rely
1202 briefly on SW decryption */
1203 return;
1204 }
1205
Johannes Berge1b1c082011-07-17 01:44:11 -07001206 iwlagn_send_sta_key(priv, keyconf, sta_id,
1207 iv32, phase1key, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001208}
1209
1210int iwl_remove_dynamic_key(struct iwl_priv *priv,
1211 struct iwl_rxon_context *ctx,
1212 struct ieee80211_key_conf *keyconf,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001213 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001214{
Johannes Berga30e3112010-09-22 18:02:01 +02001215 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001216 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
Johannes Berg5dcbf482012-02-17 09:47:14 -08001217 __le16 key_flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001218
1219 /* if station isn't there, neither is the key */
1220 if (sta_id == IWL_INVALID_STATION)
1221 return -ENOENT;
1222
Johannes Bergfa23cb02012-03-05 11:24:25 -08001223 spin_lock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001224 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1225 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1226 sta_id = IWL_INVALID_STATION;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001227 spin_unlock_bh(&priv->sta_lock);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001228
1229 if (sta_id == IWL_INVALID_STATION)
1230 return 0;
Johannes Berga30e3112010-09-22 18:02:01 +02001231
Johannes Bergb1eea292012-03-06 13:30:42 -08001232 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001233
1234 ctx->key_mapping_keys--;
1235
Johannes Berga30e3112010-09-22 18:02:01 +02001236 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1237 keyconf->keyidx, sta_id);
1238
Johannes Berg5a3d9882011-07-15 13:03:12 -07001239 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1240 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1241 keyconf->hw_key_idx);
Johannes Berga30e3112010-09-22 18:02:01 +02001242
Johannes Berg5dcbf482012-02-17 09:47:14 -08001243 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1244 key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1245 STA_KEY_FLG_INVALID;
1246
1247 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1248 key_flags |= STA_KEY_MULTICAST_MSK;
1249
1250 sta_cmd.key.key_flags = key_flags;
Emmanuel Grumbachd6ee27eb2012-06-06 09:13:36 +02001251 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001252 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1253 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001254
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001255 return iwl_send_add_sta(priv, &sta_cmd, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001256}
1257
Johannes Berg5a3d9882011-07-15 13:03:12 -07001258int iwl_set_dynamic_key(struct iwl_priv *priv,
1259 struct iwl_rxon_context *ctx,
1260 struct ieee80211_key_conf *keyconf,
1261 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001262{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001263 struct ieee80211_key_seq seq;
1264 u16 p1k[5];
Johannes Berga30e3112010-09-22 18:02:01 +02001265 int ret;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001266 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1267 const u8 *addr;
1268
1269 if (sta_id == IWL_INVALID_STATION)
1270 return -EINVAL;
Johannes Berga30e3112010-09-22 18:02:01 +02001271
Johannes Bergb1eea292012-03-06 13:30:42 -08001272 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001273
Johannes Berg5a3d9882011-07-15 13:03:12 -07001274 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1275 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1276 return -ENOSPC;
1277
Johannes Berga30e3112010-09-22 18:02:01 +02001278 ctx->key_mapping_keys++;
Johannes Berga30e3112010-09-22 18:02:01 +02001279
1280 switch (keyconf->cipher) {
Johannes Berga30e3112010-09-22 18:02:01 +02001281 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001282 if (sta)
1283 addr = sta->addr;
1284 else /* station mode case only */
1285 addr = ctx->active.bssid_addr;
1286
1287 /* pre-fill phase 1 key into device cache */
1288 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1289 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Berge1b1c082011-07-17 01:44:11 -07001290 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001291 seq.tkip.iv32, p1k, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001292 break;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001293 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berga30e3112010-09-22 18:02:01 +02001294 case WLAN_CIPHER_SUITE_WEP40:
1295 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge1b1c082011-07-17 01:44:11 -07001296 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001297 0, NULL, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001298 break;
1299 default:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001300 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
Johannes Berga30e3112010-09-22 18:02:01 +02001301 ret = -EINVAL;
1302 }
1303
Johannes Berg5a3d9882011-07-15 13:03:12 -07001304 if (ret) {
1305 ctx->key_mapping_keys--;
1306 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1307 }
1308
1309 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 +02001310 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001311 sta ? sta->addr : NULL, ret);
Johannes Berga30e3112010-09-22 18:02:01 +02001312
1313 return ret;
1314}
1315
1316/**
1317 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1318 *
1319 * This adds the broadcast station into the driver's station table
1320 * and marks it driver active, so that it will be restored to the
1321 * device at the next best time.
1322 */
1323int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1324 struct iwl_rxon_context *ctx)
1325{
1326 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga30e3112010-09-22 18:02:01 +02001327 u8 sta_id;
1328
Johannes Bergfa23cb02012-03-05 11:24:25 -08001329 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001330 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1331 if (sta_id == IWL_INVALID_STATION) {
1332 IWL_ERR(priv, "Unable to prepare broadcast station\n");
Johannes Bergfa23cb02012-03-05 11:24:25 -08001333 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001334
1335 return -EINVAL;
1336 }
1337
1338 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1339 priv->stations[sta_id].used |= IWL_STA_BCAST;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001340 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001341
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001342 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001343 if (!link_cmd) {
1344 IWL_ERR(priv,
1345 "Unable to initialize rate scaling for bcast station.\n");
1346 return -ENOMEM;
1347 }
1348
Johannes Bergfa23cb02012-03-05 11:24:25 -08001349 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001350 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001351 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001352
1353 return 0;
1354}
1355
1356/**
1357 * iwl_update_bcast_station - update broadcast station's LQ command
1358 *
1359 * Only used by iwlagn. Placed here to have all bcast station management
1360 * code together.
1361 */
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001362int iwl_update_bcast_station(struct iwl_priv *priv,
1363 struct iwl_rxon_context *ctx)
Johannes Berga30e3112010-09-22 18:02:01 +02001364{
Johannes Berga30e3112010-09-22 18:02:01 +02001365 struct iwl_link_quality_cmd *link_cmd;
1366 u8 sta_id = ctx->bcast_sta_id;
1367
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001368 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001369 if (!link_cmd) {
1370 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1371 return -ENOMEM;
1372 }
1373
Johannes Bergfa23cb02012-03-05 11:24:25 -08001374 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001375 if (priv->stations[sta_id].lq)
1376 kfree(priv->stations[sta_id].lq);
1377 else
1378 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1379 priv->stations[sta_id].lq = link_cmd;
Johannes Bergfa23cb02012-03-05 11:24:25 -08001380 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001381
1382 return 0;
1383}
1384
1385int iwl_update_bcast_stations(struct iwl_priv *priv)
1386{
1387 struct iwl_rxon_context *ctx;
1388 int ret = 0;
1389
1390 for_each_context(priv, ctx) {
1391 ret = iwl_update_bcast_station(priv, ctx);
1392 if (ret)
1393 break;
1394 }
1395
1396 return ret;
1397}
1398
1399/**
1400 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1401 */
1402int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1403{
Johannes Berga30e3112010-09-22 18:02:01 +02001404 struct iwl_addsta_cmd sta_cmd;
1405
Johannes Bergb1eea292012-03-06 13:30:42 -08001406 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001407
1408 /* Remove "disable" flag, to enable Tx for this TID */
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.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1411 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1412 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1413 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001414 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001415
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001416 return iwl_send_add_sta(priv, &sta_cmd, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001417}
1418
1419int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1420 int tid, u16 ssn)
1421{
Johannes Berga30e3112010-09-22 18:02:01 +02001422 int sta_id;
1423 struct iwl_addsta_cmd sta_cmd;
1424
Johannes Bergb1eea292012-03-06 13:30:42 -08001425 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001426
1427 sta_id = iwl_sta_id(sta);
1428 if (sta_id == IWL_INVALID_STATION)
1429 return -ENXIO;
1430
Johannes Bergfa23cb02012-03-05 11:24:25 -08001431 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001432 priv->stations[sta_id].sta.station_flags_msk = 0;
1433 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1434 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1435 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1436 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1437 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001438 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001439
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001440 return iwl_send_add_sta(priv, &sta_cmd, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001441}
1442
1443int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1444 int tid)
1445{
Johannes Berga30e3112010-09-22 18:02:01 +02001446 int sta_id;
1447 struct iwl_addsta_cmd sta_cmd;
1448
Johannes Bergb1eea292012-03-06 13:30:42 -08001449 lockdep_assert_held(&priv->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001450
1451 sta_id = iwl_sta_id(sta);
1452 if (sta_id == IWL_INVALID_STATION) {
1453 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1454 return -ENXIO;
1455 }
1456
Johannes Bergfa23cb02012-03-05 11:24:25 -08001457 spin_lock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001458 priv->stations[sta_id].sta.station_flags_msk = 0;
1459 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1460 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1461 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1462 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Johannes Bergfa23cb02012-03-05 11:24:25 -08001463 spin_unlock_bh(&priv->sta_lock);
Johannes Berga30e3112010-09-22 18:02:01 +02001464
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001465 return iwl_send_add_sta(priv, &sta_cmd, 0);
Johannes Berga30e3112010-09-22 18:02:01 +02001466}
1467
Johannes Berga30e3112010-09-22 18:02:01 +02001468
Johannes Berga30e3112010-09-22 18:02:01 +02001469
1470void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1471{
Johannes Berg9451ca12012-03-05 11:24:23 -08001472 struct iwl_addsta_cmd cmd = {
1473 .mode = STA_CONTROL_MODIFY_MSK,
1474 .station_flags = STA_FLG_PWR_SAVE_MSK,
1475 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1476 .sta.sta_id = sta_id,
1477 .sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1478 .sleep_tx_count = cpu_to_le16(cnt),
1479 };
Johannes Berga30e3112010-09-22 18:02:01 +02001480
Johannes Berg9451ca12012-03-05 11:24:23 -08001481 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001482}