blob: 8fec0263f01673020416a4fed2856d365bd5fd8c [file] [log] [blame]
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001/******************************************************************************
2 *
Reinette Chatre1f447802010-01-15 13:43:41 -08003 * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004 *
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:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070026 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
30#include <net/mac80211.h>
Tomas Winkler947b13a2008-04-16 16:34:48 -070031#include <linux/etherdevice.h>
Reinette Chatrefe6b23d2010-02-22 16:24:47 -080032#include <linux/sched.h>
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070033
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070034#include "iwl-dev.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070035#include "iwl-core.h"
36#include "iwl-sta.h"
Tomas Winkler7a999bf2008-05-29 16:35:02 +080037
Tomas Winkler947b13a2008-04-16 16:34:48 -070038u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
39{
40 int i;
41 int start = 0;
42 int ret = IWL_INVALID_STATION;
43 unsigned long flags;
Tomas Winkler947b13a2008-04-16 16:34:48 -070044
Johannes Berg05c914f2008-09-11 00:01:58 +020045 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) ||
46 (priv->iw_mode == NL80211_IFTYPE_AP))
Tomas Winkler947b13a2008-04-16 16:34:48 -070047 start = IWL_STA_ID;
48
49 if (is_broadcast_ether_addr(addr))
50 return priv->hw_params.bcast_sta_id;
51
52 spin_lock_irqsave(&priv->sta_lock, flags);
53 for (i = start; i < priv->hw_params.max_stations; i++)
54 if (priv->stations[i].used &&
55 (!compare_ether_addr(priv->stations[i].sta.sta.addr,
56 addr))) {
57 ret = i;
58 goto out;
59 }
60
Tomas Winklere1623442009-01-27 14:27:56 -080061 IWL_DEBUG_ASSOC_LIMIT(priv, "can not find STA %pM total %d\n",
Johannes Berge1749612008-10-27 15:59:26 -070062 addr, priv->num_stations);
Tomas Winkler947b13a2008-04-16 16:34:48 -070063
64 out:
Reinette Chatrefe6b23d2010-02-22 16:24:47 -080065 /*
66 * It may be possible that more commands interacting with stations
67 * arrive before we completed processing the adding of
68 * station
69 */
70 if (ret != IWL_INVALID_STATION &&
71 (!(priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) ||
72 ((priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) &&
73 (priv->stations[ret].used & IWL_STA_UCODE_INPROGRESS)))) {
Frans Pop91dd6c22010-03-24 14:19:58 -070074 IWL_ERR(priv, "Requested station info for sta %d before ready.\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -080075 ret);
76 ret = IWL_INVALID_STATION;
77 }
Tomas Winkler947b13a2008-04-16 16:34:48 -070078 spin_unlock_irqrestore(&priv->sta_lock, flags);
79 return ret;
80}
81EXPORT_SYMBOL(iwl_find_station);
82
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080083/* priv->sta_lock must be held */
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080084static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
85{
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080086
87 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
88 IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u addr %pM\n",
89 sta_id, priv->stations[sta_id].sta.sta.addr);
90
91 if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
92 IWL_DEBUG_ASSOC(priv,
93 "STA id %u addr %pM already present in uCode (according to driver)\n",
94 sta_id, priv->stations[sta_id].sta.sta.addr);
95 } else {
96 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
97 IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
98 sta_id, priv->stations[sta_id].sta.sta.addr);
99 }
100}
101
102static void iwl_process_add_sta_resp(struct iwl_priv *priv,
103 struct iwl_addsta_cmd *addsta,
104 struct iwl_rx_packet *pkt,
105 bool sync)
106{
107 u8 sta_id = addsta->sta.sta_id;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800108 unsigned long flags;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800109
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800110 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
111 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
112 pkt->hdr.flags);
113 return;
114 }
115
116 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
117 sta_id);
118
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800119 spin_lock_irqsave(&priv->sta_lock, flags);
120
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800121 switch (pkt->u.add_sta.status) {
122 case ADD_STA_SUCCESS_MSK:
123 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
124 iwl_sta_ucode_activate(priv, sta_id);
125 break;
126 case ADD_STA_NO_ROOM_IN_TABLE:
127 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
Winkler, Tomas15b16872008-12-19 10:37:33 +0800128 sta_id);
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800129 break;
130 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
131 IWL_ERR(priv, "Adding station %d failed, no block ack resource.\n",
132 sta_id);
133 break;
134 case ADD_STA_MODIFY_NON_EXIST_STA:
Frans Pop91dd6c22010-03-24 14:19:58 -0700135 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800136 sta_id);
137 break;
138 default:
139 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
140 pkt->u.add_sta.status);
141 break;
142 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800143
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800144 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
145 priv->stations[sta_id].sta.mode ==
146 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
147 sta_id, priv->stations[sta_id].sta.sta.addr);
148
149 /*
150 * XXX: The MAC address in the command buffer is often changed from
151 * the original sent to the device. That is, the MAC address
152 * written to the command buffer often is not the same MAC adress
153 * read from the command buffer when the command returns. This
154 * issue has not yet been resolved and this debugging is left to
155 * observe the problem.
156 */
157 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
158 priv->stations[sta_id].sta.mode ==
159 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
160 addsta->sta.addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800161 spin_unlock_irqrestore(&priv->sta_lock, flags);
162}
163
Johannes Berg5696aea2009-07-24 11:13:06 -0700164static void iwl_add_sta_callback(struct iwl_priv *priv,
165 struct iwl_device_cmd *cmd,
Zhu Yi2f301222009-10-09 17:19:45 +0800166 struct iwl_rx_packet *pkt)
Tomas Winkler42132bc2008-05-29 16:35:03 +0800167{
Tomas Winkler3257e5d2008-10-14 12:32:43 -0700168 struct iwl_addsta_cmd *addsta =
169 (struct iwl_addsta_cmd *)cmd->cmd.payload;
Tomas Winkler42132bc2008-05-29 16:35:03 +0800170
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800171 iwl_process_add_sta_resp(priv, addsta, pkt, false);
Tomas Winkler42132bc2008-05-29 16:35:03 +0800172
Tomas Winkler42132bc2008-05-29 16:35:03 +0800173}
174
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800175int iwl_send_add_sta(struct iwl_priv *priv,
Tomas Winkler133636d2008-05-05 10:22:34 +0800176 struct iwl_addsta_cmd *sta, u8 flags)
177{
Zhu Yi2f301222009-10-09 17:19:45 +0800178 struct iwl_rx_packet *pkt = NULL;
Tomas Winkler133636d2008-05-05 10:22:34 +0800179 int ret = 0;
180 u8 data[sizeof(*sta)];
181 struct iwl_host_cmd cmd = {
182 .id = REPLY_ADD_STA,
Johannes Bergc2acea82009-07-24 11:13:05 -0700183 .flags = flags,
Tomas Winkler133636d2008-05-05 10:22:34 +0800184 .data = data,
185 };
Reinette Chatref875f512010-04-05 10:43:10 -0700186 u8 sta_id __maybe_unused = sta->sta.sta_id;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800187
188 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
189 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
Tomas Winkler133636d2008-05-05 10:22:34 +0800190
Tomas Winkler42132bc2008-05-29 16:35:03 +0800191 if (flags & CMD_ASYNC)
Johannes Bergc2acea82009-07-24 11:13:05 -0700192 cmd.callback = iwl_add_sta_callback;
Tomas Winkler42132bc2008-05-29 16:35:03 +0800193 else
Johannes Bergc2acea82009-07-24 11:13:05 -0700194 cmd.flags |= CMD_WANT_SKB;
Tomas Winkler133636d2008-05-05 10:22:34 +0800195
196 cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
197 ret = iwl_send_cmd(priv, &cmd);
198
199 if (ret || (flags & CMD_ASYNC))
200 return ret;
201
Tomas Winkler133636d2008-05-05 10:22:34 +0800202 if (ret == 0) {
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800203 pkt = (struct iwl_rx_packet *)cmd.reply_page;
204 iwl_process_add_sta_resp(priv, sta, pkt, true);
Tomas Winkler133636d2008-05-05 10:22:34 +0800205 }
Zhu Yi64a76b52009-12-10 14:37:21 -0800206 iwl_free_pages(priv, cmd.reply_page);
Tomas Winkler133636d2008-05-05 10:22:34 +0800207
208 return ret;
209}
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800210EXPORT_SYMBOL(iwl_send_add_sta);
Tomas Winkler947b13a2008-04-16 16:34:48 -0700211
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800212static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200213 struct ieee80211_sta_ht_cap *sta_ht_inf)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800214{
215 __le32 sta_flags;
216 u8 mimo_ps_mode;
217
218 if (!sta_ht_inf || !sta_ht_inf->ht_supported)
219 goto done;
220
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800221 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
Wey-Yi Guy3f3e0372009-10-30 14:36:17 -0700222 IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
223 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
224 "static" :
225 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
226 "dynamic" : "disabled");
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800227
228 sta_flags = priv->stations[index].sta.station_flags;
229
230 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
231
232 switch (mimo_ps_mode) {
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800233 case WLAN_HT_CAP_SM_PS_STATIC:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800234 sta_flags |= STA_FLG_MIMO_DIS_MSK;
235 break;
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800236 case WLAN_HT_CAP_SM_PS_DYNAMIC:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800237 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
238 break;
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800239 case WLAN_HT_CAP_SM_PS_DISABLED:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800240 break;
241 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800242 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800243 break;
244 }
245
246 sta_flags |= cpu_to_le32(
247 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
248
249 sta_flags |= cpu_to_le32(
250 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
251
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700252 if (iwl_is_ht40_tx_allowed(priv, sta_ht_inf))
253 sta_flags |= STA_FLG_HT40_EN_MSK;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800254 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700255 sta_flags &= ~STA_FLG_HT40_EN_MSK;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800256
257 priv->stations[index].sta.station_flags = sta_flags;
258 done:
259 return;
260}
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800261
262/**
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800263 * iwl_prep_station - Prepare station information for addition
264 *
265 * should be called with sta_lock held
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800266 */
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800267static u8 iwl_prep_station(struct iwl_priv *priv, const u8 *addr,
268 bool is_ap,
269 struct ieee80211_sta_ht_cap *ht_info)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800270{
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800271 struct iwl_station_entry *station;
Tomas Winklerc587de02009-06-03 11:44:07 -0700272 int i;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800273 u8 sta_id = IWL_INVALID_STATION;
Tomas Winklerc587de02009-06-03 11:44:07 -0700274 u16 rate;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800275
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800276 if (is_ap)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800277 sta_id = IWL_AP_ID;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800278 else if (is_broadcast_ether_addr(addr))
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800279 sta_id = priv->hw_params.bcast_sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800280 else
281 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
282 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
283 addr)) {
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800284 sta_id = i;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800285 break;
286 }
287
288 if (!priv->stations[i].used &&
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800289 sta_id == IWL_INVALID_STATION)
290 sta_id = i;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800291 }
292
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800293 /*
294 * These two conditions have the same outcome, but keep them
295 * separate
296 */
297 if (unlikely(sta_id == IWL_INVALID_STATION))
298 return sta_id;
299
300 /*
301 * uCode is not able to deal with multiple requests to add a
302 * station. Keep track if one is in progress so that we do not send
303 * another.
304 */
305 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
306 IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
307 sta_id);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800308 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800309 }
310
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800311 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
312 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800313 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800314 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
315 sta_id, addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800316 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800317 }
318
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800319 station = &priv->stations[sta_id];
320 station->used = IWL_STA_DRIVER_ACTIVE;
Tomas Winklere1623442009-01-27 14:27:56 -0800321 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700322 sta_id, addr);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800323 priv->num_stations++;
324
325 /* Set up the REPLY_ADD_STA command to send to device */
326 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
327 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
328 station->sta.mode = 0;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800329 station->sta.sta.sta_id = sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800330 station->sta.station_flags = 0;
331
332 /* BCAST station and IBSS stations do not work in HT mode */
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800333 if (sta_id != priv->hw_params.bcast_sta_id &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200334 priv->iw_mode != NL80211_IFTYPE_ADHOC)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800335 iwl_set_ht_add_station(priv, sta_id, ht_info);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800336
Tomas Winklerc587de02009-06-03 11:44:07 -0700337 /* 3945 only */
338 rate = (priv->band == IEEE80211_BAND_5GHZ) ?
339 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
340 /* Turn on both antennas for the station... */
341 station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
342
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800343 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800344
345}
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800346
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800347#define STA_WAIT_TIMEOUT (HZ/2)
348
349/**
350 * iwl_add_station_common -
351 */
352int iwl_add_station_common(struct iwl_priv *priv, const u8 *addr,
353 bool is_ap,
354 struct ieee80211_sta_ht_cap *ht_info,
355 u8 *sta_id_r)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800356{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800357 struct iwl_station_entry *station;
358 unsigned long flags_spin;
359 int ret = 0;
360 u8 sta_id;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800361
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800362 *sta_id_r = 0;
363 spin_lock_irqsave(&priv->sta_lock, flags_spin);
364 sta_id = iwl_prep_station(priv, addr, is_ap, ht_info);
365 if (sta_id == IWL_INVALID_STATION) {
366 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
367 addr);
368 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
369 return -EINVAL;
370 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800371
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800372 /*
373 * uCode is not able to deal with multiple requests to add a
374 * station. Keep track if one is in progress so that we do not send
375 * another.
376 */
377 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
378 IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
379 sta_id);
380 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
381 return -EEXIST;
382 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800383
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800384 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
385 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
386 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
387 sta_id, addr);
388 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
389 return -EEXIST;
390 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800391
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800392 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
393 station = &priv->stations[sta_id];
394 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
395
396 /* Add station to device's station table */
397 ret = iwl_send_add_sta(priv, &station->sta, CMD_SYNC);
398 if (ret) {
399 IWL_ERR(priv, "Adding station %pM failed.\n", station->sta.sta.addr);
400 spin_lock_irqsave(&priv->sta_lock, flags_spin);
401 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
402 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
403 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
404 }
405 *sta_id_r = sta_id;
406 return ret;
407}
408EXPORT_SYMBOL(iwl_add_station_common);
409
Johannes Berga6a03452010-04-29 10:41:07 -0700410static struct iwl_link_quality_cmd *iwl_sta_alloc_lq(struct iwl_priv *priv,
411 u8 sta_id)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800412{
413 int i, r;
Reinette Chatred2e210a2010-04-23 10:33:33 -0700414 struct iwl_link_quality_cmd *link_cmd;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800415 u32 rate_flags;
416
Reinette Chatred2e210a2010-04-23 10:33:33 -0700417 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
418 if (!link_cmd) {
419 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
420 return NULL;
421 }
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800422 /* Set up the rate scaling to start at selected rate, fall back
423 * all the way down to 1M in IEEE order, and then spin on 1M */
Johannes Berg156b70d2010-04-28 14:44:45 -0700424 if (priv->band == IEEE80211_BAND_5GHZ)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800425 r = IWL_RATE_6M_INDEX;
426 else
427 r = IWL_RATE_1M_INDEX;
428
429 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
430 rate_flags = 0;
431 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
432 rate_flags |= RATE_MCS_CCK_MSK;
433
434 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
435 RATE_MCS_ANT_POS;
436
Reinette Chatred2e210a2010-04-23 10:33:33 -0700437 link_cmd->rs_table[i].rate_n_flags =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800438 iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
439 r = iwl_get_prev_ieee_rate(r);
440 }
441
Reinette Chatred2e210a2010-04-23 10:33:33 -0700442 link_cmd->general_params.single_stream_ant_msk =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800443 first_antenna(priv->hw_params.valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700444
Reinette Chatred2e210a2010-04-23 10:33:33 -0700445 link_cmd->general_params.dual_stream_ant_msk =
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700446 priv->hw_params.valid_tx_ant &
447 ~first_antenna(priv->hw_params.valid_tx_ant);
Reinette Chatred2e210a2010-04-23 10:33:33 -0700448 if (!link_cmd->general_params.dual_stream_ant_msk) {
449 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700450 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
Reinette Chatred2e210a2010-04-23 10:33:33 -0700451 link_cmd->general_params.dual_stream_ant_msk =
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700452 priv->hw_params.valid_tx_ant;
453 }
454
Reinette Chatred2e210a2010-04-23 10:33:33 -0700455 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
456 link_cmd->agg_params.agg_time_limit =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800457 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
458
Johannes Berg156b70d2010-04-28 14:44:45 -0700459 link_cmd->sta_id = sta_id;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800460
Reinette Chatred2e210a2010-04-23 10:33:33 -0700461 return link_cmd;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800462}
463
464/*
Johannes Bergfd1af152010-04-30 11:30:43 -0700465 * iwl_add_local_station - Add stations not requested by mac80211
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800466 *
467 * This will be either the broadcast station or the bssid station needed by
468 * ad-hoc.
469 *
470 * Function sleeps.
471 */
Johannes Bergfd1af152010-04-30 11:30:43 -0700472int iwl_add_local_station(struct iwl_priv *priv, const u8 *addr, bool init_rs,
473 u8 *sta_id_r)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800474{
475 int ret;
476 u8 sta_id;
Reinette Chatred2e210a2010-04-23 10:33:33 -0700477 struct iwl_link_quality_cmd *link_cmd;
478 unsigned long flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800479
Johannes Bergfd1af152010-04-30 11:30:43 -0700480 if (*sta_id_r)
481 *sta_id_r = IWL_INVALID_STATION;
482
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800483 ret = iwl_add_station_common(priv, addr, 0, NULL, &sta_id);
484 if (ret) {
485 IWL_ERR(priv, "Unable to add station %pM\n", addr);
486 return ret;
487 }
488
Johannes Bergfd1af152010-04-30 11:30:43 -0700489 if (sta_id_r)
490 *sta_id_r = sta_id;
491
Reinette Chatred2e210a2010-04-23 10:33:33 -0700492 spin_lock_irqsave(&priv->sta_lock, flags);
493 priv->stations[sta_id].used |= IWL_STA_LOCAL;
494 spin_unlock_irqrestore(&priv->sta_lock, flags);
495
496 if (init_rs) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800497 /* Set up default rate scaling table in device's station table */
Johannes Berga6a03452010-04-29 10:41:07 -0700498 link_cmd = iwl_sta_alloc_lq(priv, sta_id);
Reinette Chatred2e210a2010-04-23 10:33:33 -0700499 if (!link_cmd) {
500 IWL_ERR(priv, "Unable to initialize rate scaling for station %pM.\n",
501 addr);
502 return -ENOMEM;
503 }
Johannes Berga6a03452010-04-29 10:41:07 -0700504
505 ret = iwl_send_lq_cmd(priv, link_cmd, CMD_SYNC, true);
506 if (ret)
507 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
508
Reinette Chatred2e210a2010-04-23 10:33:33 -0700509 spin_lock_irqsave(&priv->sta_lock, flags);
510 priv->stations[sta_id].lq = link_cmd;
511 spin_unlock_irqrestore(&priv->sta_lock, flags);
512 }
513
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800514 return 0;
515}
516EXPORT_SYMBOL(iwl_add_local_station);
517
518/**
519 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
520 *
521 * priv->sta_lock must be held
522 */
523static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
524{
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800525 /* Ucode must be active and driver must be non active */
Reinette Chatred2e210a2010-04-23 10:33:33 -0700526 if ((priv->stations[sta_id].used &
527 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) != IWL_STA_UCODE_ACTIVE)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800528 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800529
530 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
531
532 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800533 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800534}
535
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800536static int iwl_send_remove_station(struct iwl_priv *priv,
537 struct iwl_station_entry *station)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800538{
Zhu Yi2f301222009-10-09 17:19:45 +0800539 struct iwl_rx_packet *pkt;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800540 int ret;
541
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800542 unsigned long flags_spin;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800543 struct iwl_rem_sta_cmd rm_sta_cmd;
544
545 struct iwl_host_cmd cmd = {
546 .id = REPLY_REMOVE_STA,
547 .len = sizeof(struct iwl_rem_sta_cmd),
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800548 .flags = CMD_SYNC,
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800549 .data = &rm_sta_cmd,
550 };
551
552 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
553 rm_sta_cmd.num_sta = 1;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800554 memcpy(&rm_sta_cmd.addr, &station->sta.sta.addr , ETH_ALEN);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800555
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800556 cmd.flags |= CMD_WANT_SKB;
557
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800558 ret = iwl_send_cmd(priv, &cmd);
559
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800560 if (ret)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800561 return ret;
562
Zhu Yi2f301222009-10-09 17:19:45 +0800563 pkt = (struct iwl_rx_packet *)cmd.reply_page;
564 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800565 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800566 pkt->hdr.flags);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800567 ret = -EIO;
568 }
569
570 if (!ret) {
Zhu Yi2f301222009-10-09 17:19:45 +0800571 switch (pkt->u.rem_sta.status) {
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800572 case REM_STA_SUCCESS_MSK:
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800573 spin_lock_irqsave(&priv->sta_lock, flags_spin);
574 iwl_sta_ucode_deactivate(priv, station->sta.sta.sta_id);
575 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Tomas Winklere1623442009-01-27 14:27:56 -0800576 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800577 break;
578 default:
579 ret = -EIO;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800580 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800581 break;
582 }
583 }
Zhu Yi64a76b52009-12-10 14:37:21 -0800584 iwl_free_pages(priv, cmd.reply_page);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800585
586 return ret;
587}
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +0800588
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800589/**
590 * iwl_remove_station - Remove driver's knowledge of station.
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800591 */
Johannes Bergfd1af152010-04-30 11:30:43 -0700592int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
593 const u8 *addr)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800594{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800595 struct iwl_station_entry *station;
Johannes Bergfd1af152010-04-30 11:30:43 -0700596 unsigned long flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800597
598 if (!iwl_is_ready(priv)) {
599 IWL_DEBUG_INFO(priv,
Frans Pop91dd6c22010-03-24 14:19:58 -0700600 "Unable to remove station %pM, device not ready.\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700601 addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800602 /*
603 * It is typical for stations to be removed when we are
604 * going down. Return success since device will be down
605 * soon anyway
606 */
607 return 0;
608 }
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800609
Tomas Winklere1623442009-01-27 14:27:56 -0800610 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
Johannes Bergfd1af152010-04-30 11:30:43 -0700611 sta_id, addr);
612
613 if (WARN_ON(sta_id == IWL_INVALID_STATION))
614 return -EINVAL;
615
616 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800617
618 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800619 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700620 addr);
Johannes Bergfd1af152010-04-30 11:30:43 -0700621 goto out_err;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800622 }
623
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800624 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800625 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700626 addr);
Johannes Bergfd1af152010-04-30 11:30:43 -0700627 goto out_err;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800628 }
629
Johannes Berg1fa61b22010-04-28 08:44:52 -0700630 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
631 kfree(priv->stations[sta_id].lq);
632 priv->stations[sta_id].lq = NULL;
633 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800634
635 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
636
637 priv->num_stations--;
638
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800639 BUG_ON(priv->num_stations < 0);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800640
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800641 station = &priv->stations[sta_id];
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800642 spin_unlock_irqrestore(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800643
Johannes Bergfd1af152010-04-30 11:30:43 -0700644 return iwl_send_remove_station(priv, station);
645out_err:
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800646 spin_unlock_irqrestore(&priv->sta_lock, flags);
Johannes Bergfd1af152010-04-30 11:30:43 -0700647 return -EINVAL;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800648}
Johannes Berg1fa61b22010-04-28 08:44:52 -0700649EXPORT_SYMBOL_GPL(iwl_remove_station);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800650
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800651/**
Johannes Berg2c810cc2010-04-29 00:53:29 -0700652 * iwl_clear_ucode_stations - clear ucode station table bits
653 *
654 * This function clears all the bits in the driver indicating
655 * which stations are active in the ucode. Call when something
656 * other than explicit station management would cause this in
657 * the ucode, e.g. unassociated RXON.
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800658 */
Johannes Berg2c810cc2010-04-29 00:53:29 -0700659void iwl_clear_ucode_stations(struct iwl_priv *priv)
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800660{
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700661 int i;
Reinette Chatre7e246192010-02-18 22:58:32 -0800662 unsigned long flags_spin;
663 bool cleared = false;
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800664
Johannes Berg2c810cc2010-04-29 00:53:29 -0700665 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700666
Reinette Chatre7e246192010-02-18 22:58:32 -0800667 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Johannes Berg2c810cc2010-04-29 00:53:29 -0700668 for (i = 0; i < priv->hw_params.max_stations; i++) {
669 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
670 IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d\n", i);
671 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
672 cleared = true;
Reinette Chatre7e246192010-02-18 22:58:32 -0800673 }
674 }
675 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
676
677 if (!cleared)
678 IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800679}
Reinette Chatre7e246192010-02-18 22:58:32 -0800680EXPORT_SYMBOL(iwl_clear_ucode_stations);
681
682/**
683 * iwl_restore_stations() - Restore driver known stations to device
684 *
685 * All stations considered active by driver, but not present in ucode, is
686 * restored.
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800687 *
688 * Function sleeps.
Reinette Chatre7e246192010-02-18 22:58:32 -0800689 */
690void iwl_restore_stations(struct iwl_priv *priv)
691{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800692 struct iwl_station_entry *station;
Reinette Chatre7e246192010-02-18 22:58:32 -0800693 unsigned long flags_spin;
694 int i;
695 bool found = false;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800696 int ret;
Reinette Chatre7e246192010-02-18 22:58:32 -0800697
698 if (!iwl_is_ready(priv)) {
699 IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
700 return;
701 }
702
703 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
704 spin_lock_irqsave(&priv->sta_lock, flags_spin);
705 for (i = 0; i < priv->hw_params.max_stations; i++) {
706 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
707 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
708 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
709 priv->stations[i].sta.sta.addr);
710 priv->stations[i].sta.mode = 0;
711 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
712 found = true;
713 }
714 }
715
716 for (i = 0; i < priv->hw_params.max_stations; i++) {
717 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800718 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
719 station = &priv->stations[i];
720 ret = iwl_send_add_sta(priv, &priv->stations[i].sta, CMD_SYNC);
721 if (ret) {
722 IWL_ERR(priv, "Adding station %pM failed.\n",
723 station->sta.sta.addr);
724 spin_lock_irqsave(&priv->sta_lock, flags_spin);
725 priv->stations[i].used &= ~IWL_STA_DRIVER_ACTIVE;
726 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
727 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
728 }
729 /*
730 * Rate scaling has already been initialized, send
731 * current LQ command
732 */
733 if (station->lq)
734 iwl_send_lq_cmd(priv, station->lq, CMD_SYNC, true);
735 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Reinette Chatre7e246192010-02-18 22:58:32 -0800736 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
737 }
738 }
739
740 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
741 if (!found)
742 IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
743 else
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800744 IWL_DEBUG_INFO(priv, "Restoring all known stations .... complete.\n");
Reinette Chatre7e246192010-02-18 22:58:32 -0800745}
746EXPORT_SYMBOL(iwl_restore_stations);
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800747
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800748int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700749{
750 int i;
751
752 for (i = 0; i < STA_KEY_MAX_NUM; i++)
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700753 if (!test_and_set_bit(i, &priv->ucode_key_table))
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700754 return i;
755
Tomas Winkler40a9a822008-11-25 23:29:03 +0200756 return WEP_INVALID_OFFSET;
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700757}
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800758EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700759
Johannes Berg335348b2010-03-30 10:11:46 -0700760static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700761{
762 int i, not_empty = 0;
763 u8 buff[sizeof(struct iwl_wep_cmd) +
764 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
765 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
766 size_t cmd_size = sizeof(struct iwl_wep_cmd);
767 struct iwl_host_cmd cmd = {
768 .id = REPLY_WEPKEY,
769 .data = wep_cmd,
Johannes Berg72e15d72010-02-19 11:42:32 -0800770 .flags = CMD_SYNC,
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700771 };
772
Johannes Berg72e15d72010-02-19 11:42:32 -0800773 might_sleep();
774
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700775 memset(wep_cmd, 0, cmd_size +
776 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
777
778 for (i = 0; i < WEP_KEYS_MAX ; i++) {
779 wep_cmd->key[i].key_index = i;
780 if (priv->wep_keys[i].key_size) {
781 wep_cmd->key[i].key_offset = i;
782 not_empty = 1;
783 } else {
784 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
785 }
786
787 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
788 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
789 priv->wep_keys[i].key_size);
790 }
791
792 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
793 wep_cmd->num_keys = WEP_KEYS_MAX;
794
795 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
796
797 cmd.len = cmd_size;
798
799 if (not_empty || send_if_empty)
800 return iwl_send_cmd(priv, &cmd);
801 else
802 return 0;
803}
Johannes Berg335348b2010-03-30 10:11:46 -0700804
805int iwl_restore_default_wep_keys(struct iwl_priv *priv)
806{
807 WARN_ON(!mutex_is_locked(&priv->mutex));
808
809 return iwl_send_static_wepkey_cmd(priv, 0);
810}
811EXPORT_SYMBOL(iwl_restore_default_wep_keys);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700812
813int iwl_remove_default_wep_key(struct iwl_priv *priv,
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700814 struct ieee80211_key_conf *keyconf)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700815{
816 int ret;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700817
Johannes Berg72e15d72010-02-19 11:42:32 -0800818 WARN_ON(!mutex_is_locked(&priv->mutex));
819
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700820 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
821 keyconf->keyidx);
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700822
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700823 memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700824 if (iwl_is_rfkill(priv)) {
825 IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berg72e15d72010-02-19 11:42:32 -0800826 /* but keys in device are clear anyway so return success */
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700827 return 0;
828 }
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700829 ret = iwl_send_static_wepkey_cmd(priv, 1);
Tomas Winklere1623442009-01-27 14:27:56 -0800830 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800831 keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700832
833 return ret;
834}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800835EXPORT_SYMBOL(iwl_remove_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700836
837int iwl_set_default_wep_key(struct iwl_priv *priv,
838 struct ieee80211_key_conf *keyconf)
839{
840 int ret;
Johannes Berg72e15d72010-02-19 11:42:32 -0800841
842 WARN_ON(!mutex_is_locked(&priv->mutex));
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700843
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800844 if (keyconf->keylen != WEP_KEY_LEN_128 &&
845 keyconf->keylen != WEP_KEY_LEN_64) {
Tomas Winklere1623442009-01-27 14:27:56 -0800846 IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800847 return -EINVAL;
848 }
849
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700850 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800851 keyconf->hw_key_idx = HW_KEY_DEFAULT;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700852 priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
853
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700854 priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
855 memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
856 keyconf->keylen);
857
858 ret = iwl_send_static_wepkey_cmd(priv, 0);
Tomas Winklere1623442009-01-27 14:27:56 -0800859 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800860 keyconf->keylen, keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700861
862 return ret;
863}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800864EXPORT_SYMBOL(iwl_set_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700865
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700866static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700867 struct ieee80211_key_conf *keyconf,
868 u8 sta_id)
869{
870 unsigned long flags;
871 __le16 key_flags = 0;
872 int ret;
873
874 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700875
876 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
877 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
878 key_flags &= ~STA_KEY_FLG_INVALID;
879
880 if (keyconf->keylen == WEP_KEY_LEN_128)
881 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
882
Tomas Winkler5425e492008-04-15 16:01:38 -0700883 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700884 key_flags |= STA_KEY_MULTICAST_MSK;
885
886 spin_lock_irqsave(&priv->sta_lock, flags);
887
888 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
889 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
890 priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
891
892 memcpy(priv->stations[sta_id].keyinfo.key,
893 keyconf->key, keyconf->keylen);
894
895 memcpy(&priv->stations[sta_id].sta.key.key[3],
896 keyconf->key, keyconf->keylen);
897
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700898 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
899 == STA_KEY_FLG_NO_ENC)
900 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700901 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700902 /* else, we are overriding an existing key => no need to allocated room
903 * in uCode. */
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700904
Tomas Winkler40a9a822008-11-25 23:29:03 +0200905 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100906 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200907
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700908 priv->stations[sta_id].sta.key.key_flags = key_flags;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700909 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
910 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
911
Tomas Winkler133636d2008-05-05 10:22:34 +0800912 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700913
914 spin_unlock_irqrestore(&priv->sta_lock, flags);
915
916 return ret;
917}
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700918
919static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
920 struct ieee80211_key_conf *keyconf,
921 u8 sta_id)
922{
923 unsigned long flags;
924 __le16 key_flags = 0;
Tomas Winkler40a9a822008-11-25 23:29:03 +0200925 int ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700926
927 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
928 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
929 key_flags &= ~STA_KEY_FLG_INVALID;
930
Tomas Winkler5425e492008-04-15 16:01:38 -0700931 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700932 key_flags |= STA_KEY_MULTICAST_MSK;
933
934 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700935
936 spin_lock_irqsave(&priv->sta_lock, flags);
937 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
938 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
939
940 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
941 keyconf->keylen);
942
943 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
944 keyconf->keylen);
945
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700946 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
947 == STA_KEY_FLG_NO_ENC)
948 priv->stations[sta_id].sta.key.key_offset =
949 iwl_get_free_ucode_key_index(priv);
950 /* else, we are overriding an existing key => no need to allocated room
951 * in uCode. */
952
Tomas Winkler40a9a822008-11-25 23:29:03 +0200953 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100954 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200955
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700956 priv->stations[sta_id].sta.key.key_flags = key_flags;
957 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
958 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
959
Tomas Winkler40a9a822008-11-25 23:29:03 +0200960 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
961
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700962 spin_unlock_irqrestore(&priv->sta_lock, flags);
963
Tomas Winkler40a9a822008-11-25 23:29:03 +0200964 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700965}
966
967static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
968 struct ieee80211_key_conf *keyconf,
969 u8 sta_id)
970{
971 unsigned long flags;
972 int ret = 0;
Reinette Chatre299f5462009-04-30 13:56:31 -0700973 __le16 key_flags = 0;
974
975 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
976 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
977 key_flags &= ~STA_KEY_FLG_INVALID;
978
979 if (sta_id == priv->hw_params.bcast_sta_id)
980 key_flags |= STA_KEY_MULTICAST_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700981
982 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
983 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700984
985 spin_lock_irqsave(&priv->sta_lock, flags);
986
987 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700988 priv->stations[sta_id].keyinfo.keylen = 16;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700989
990 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
991 == STA_KEY_FLG_NO_ENC)
992 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700993 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700994 /* else, we are overriding an existing key => no need to allocated room
995 * in uCode. */
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700996
Tomas Winkler40a9a822008-11-25 23:29:03 +0200997 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100998 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200999
Reinette Chatre299f5462009-04-30 13:56:31 -07001000 priv->stations[sta_id].sta.key.key_flags = key_flags;
1001
1002
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001003 /* This copy is acutally not needed: we get the key with each TX */
1004 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
1005
1006 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
1007
1008 spin_unlock_irqrestore(&priv->sta_lock, flags);
1009
1010 return ret;
1011}
1012
Tomas Winkler9f586712008-11-12 13:14:05 -08001013void iwl_update_tkip_key(struct iwl_priv *priv,
1014 struct ieee80211_key_conf *keyconf,
1015 const u8 *addr, u32 iv32, u16 *phase1key)
1016{
1017 u8 sta_id = IWL_INVALID_STATION;
1018 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -08001019 int i;
Tomas Winkler9f586712008-11-12 13:14:05 -08001020
Tomas Winklerc587de02009-06-03 11:44:07 -07001021 sta_id = iwl_find_station(priv, addr);
Tomas Winkler9f586712008-11-12 13:14:05 -08001022 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08001023 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
Tomas Winkler9f586712008-11-12 13:14:05 -08001024 addr);
1025 return;
1026 }
1027
1028 if (iwl_scan_cancel(priv)) {
1029 /* cancel scan failed, just live w/ bad key and rely
1030 briefly on SW decryption */
1031 return;
1032 }
1033
Tomas Winkler9f586712008-11-12 13:14:05 -08001034 spin_lock_irqsave(&priv->sta_lock, flags);
1035
Tomas Winkler9f586712008-11-12 13:14:05 -08001036 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
1037
1038 for (i = 0; i < 5; i++)
1039 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
1040 cpu_to_le16(phase1key[i]);
1041
1042 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1043 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1044
1045 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1046
1047 spin_unlock_irqrestore(&priv->sta_lock, flags);
1048
1049}
1050EXPORT_SYMBOL(iwl_update_tkip_key);
1051
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001052int iwl_remove_dynamic_key(struct iwl_priv *priv,
1053 struct ieee80211_key_conf *keyconf,
1054 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001055{
1056 unsigned long flags;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001057 int ret = 0;
1058 u16 key_flags;
1059 u8 keyidx;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001060
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001061 priv->key_mapping_key--;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001062
1063 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001064 key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
1065 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
1066
Tomas Winklere1623442009-01-27 14:27:56 -08001067 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +08001068 keyconf->keyidx, sta_id);
1069
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001070 if (keyconf->keyidx != keyidx) {
1071 /* We need to remove a key with index different that the one
1072 * in the uCode. This means that the key we need to remove has
1073 * been replaced by another one with different index.
1074 * Don't do anything and return ok
1075 */
1076 spin_unlock_irqrestore(&priv->sta_lock, flags);
1077 return 0;
1078 }
1079
Tomas Winkler40a9a822008-11-25 23:29:03 +02001080 if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001081 IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
Tomas Winkler40a9a822008-11-25 23:29:03 +02001082 keyconf->keyidx, key_flags);
1083 spin_unlock_irqrestore(&priv->sta_lock, flags);
1084 return 0;
1085 }
1086
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001087 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
1088 &priv->ucode_key_table))
Winkler, Tomas15b16872008-12-19 10:37:33 +08001089 IWL_ERR(priv, "index %d not used in uCode key table.\n",
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001090 priv->stations[sta_id].sta.key.key_offset);
1091 memset(&priv->stations[sta_id].keyinfo, 0,
Tomas Winkler6def9762008-05-05 10:22:31 +08001092 sizeof(struct iwl_hw_key));
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001093 memset(&priv->stations[sta_id].sta.key, 0,
1094 sizeof(struct iwl4965_keyinfo));
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001095 priv->stations[sta_id].sta.key.key_flags =
1096 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
1097 priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001098 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1099 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001100
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -07001101 if (iwl_is_rfkill(priv)) {
Frans Pop91dd6c22010-03-24 14:19:58 -07001102 IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -07001103 spin_unlock_irqrestore(&priv->sta_lock, flags);
1104 return 0;
1105 }
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001106 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001107 spin_unlock_irqrestore(&priv->sta_lock, flags);
1108 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001109}
Tomas Winkler27aaba02008-05-05 10:22:44 +08001110EXPORT_SYMBOL(iwl_remove_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001111
1112int iwl_set_dynamic_key(struct iwl_priv *priv,
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001113 struct ieee80211_key_conf *keyconf, u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001114{
1115 int ret;
1116
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001117 priv->key_mapping_key++;
1118 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001119
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001120 switch (keyconf->alg) {
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001121 case ALG_CCMP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001122 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001123 break;
1124 case ALG_TKIP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001125 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001126 break;
1127 case ALG_WEP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001128 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001129 break;
1130 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001131 IWL_ERR(priv,
1132 "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001133 ret = -EINVAL;
1134 }
1135
Tomas Winklere1623442009-01-27 14:27:56 -08001136 IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +08001137 keyconf->alg, keyconf->keylen, keyconf->keyidx,
1138 sta_id, ret);
1139
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001140 return ret;
1141}
Tomas Winkler27aaba02008-05-05 10:22:44 +08001142EXPORT_SYMBOL(iwl_set_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001143
Tomas Winkler66c73db2008-04-15 16:01:40 -07001144#ifdef CONFIG_IWLWIFI_DEBUG
1145static void iwl_dump_lq_cmd(struct iwl_priv *priv,
1146 struct iwl_link_quality_cmd *lq)
1147{
1148 int i;
Tomas Winklere1623442009-01-27 14:27:56 -08001149 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
1150 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001151 lq->general_params.single_stream_ant_msk,
1152 lq->general_params.dual_stream_ant_msk);
1153
1154 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
Tomas Winklere1623442009-01-27 14:27:56 -08001155 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001156 i, lq->rs_table[i].rate_n_flags);
1157}
1158#else
1159static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
1160 struct iwl_link_quality_cmd *lq)
1161{
1162}
1163#endif
1164
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001165/**
Reinette Chatre3bce6062010-04-13 16:11:19 -07001166 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
1167 *
1168 * It sometimes happens when a HT rate has been in use and we
1169 * loose connectivity with AP then mac80211 will first tell us that the
1170 * current channel is not HT anymore before removing the station. In such a
1171 * scenario the RXON flags will be updated to indicate we are not
1172 * communicating HT anymore, but the LQ command may still contain HT rates.
1173 * Test for this to prevent driver from sending LQ command between the time
1174 * RXON flags are updated and when LQ command is updated.
1175 */
1176static bool is_lq_table_valid(struct iwl_priv *priv,
1177 struct iwl_link_quality_cmd *lq)
1178{
1179 int i;
1180 struct iwl_ht_config *ht_conf = &priv->current_ht_config;
1181
1182 if (ht_conf->is_ht)
1183 return true;
1184
1185 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
1186 priv->active_rxon.channel);
1187 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
1188 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) & RATE_MCS_HT_MSK) {
1189 IWL_DEBUG_INFO(priv,
1190 "index %d of LQ expects HT channel\n",
1191 i);
1192 return false;
1193 }
1194 }
1195 return true;
1196}
1197
1198/**
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001199 * iwl_send_lq_cmd() - Send link quality command
1200 * @init: This command is sent as part of station initialization right
1201 * after station has been added.
1202 *
1203 * The link quality command is sent as the last step of station creation.
1204 * This is the special case in which init is set and we call a callback in
1205 * this case to clear the state indicating that station creation is in
1206 * progress.
1207 */
Tomas Winkler66c73db2008-04-15 16:01:40 -07001208int iwl_send_lq_cmd(struct iwl_priv *priv,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001209 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
Tomas Winkler66c73db2008-04-15 16:01:40 -07001210{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001211 int ret = 0;
1212 unsigned long flags_spin;
1213
Tomas Winkler66c73db2008-04-15 16:01:40 -07001214 struct iwl_host_cmd cmd = {
1215 .id = REPLY_TX_LINK_QUALITY_CMD,
1216 .len = sizeof(struct iwl_link_quality_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07001217 .flags = flags,
Tomas Winkler66c73db2008-04-15 16:01:40 -07001218 .data = lq,
1219 };
1220
Johannes Berg76c3c692010-03-30 02:46:29 -07001221 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
Tomas Winkler66c73db2008-04-15 16:01:40 -07001222 return -EINVAL;
1223
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001224 iwl_dump_lq_cmd(priv, lq);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001225 BUG_ON(init && (cmd.flags & CMD_ASYNC));
Tomas Winkler66c73db2008-04-15 16:01:40 -07001226
Reinette Chatre3bce6062010-04-13 16:11:19 -07001227 if (is_lq_table_valid(priv, lq))
1228 ret = iwl_send_cmd(priv, &cmd);
1229 else
1230 ret = -EINVAL;
Reinette Chatred2e210a2010-04-23 10:33:33 -07001231
1232 if (cmd.flags & CMD_ASYNC)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001233 return ret;
Tomas Winkler66c73db2008-04-15 16:01:40 -07001234
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001235 if (init) {
Frans Pop91dd6c22010-03-24 14:19:58 -07001236 IWL_DEBUG_INFO(priv, "init LQ command complete, clearing sta addition status for sta %d\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001237 lq->sta_id);
1238 spin_lock_irqsave(&priv->sta_lock, flags_spin);
1239 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
1240 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
1241 }
Reinette Chatred2e210a2010-04-23 10:33:33 -07001242 return ret;
Tomas Winkler66c73db2008-04-15 16:01:40 -07001243}
1244EXPORT_SYMBOL(iwl_send_lq_cmd);
1245
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001246/**
Johannes Berg2c810cc2010-04-29 00:53:29 -07001247 * iwl_alloc_bcast_station - add broadcast station into driver's station table.
1248 *
1249 * This adds the broadcast station into the driver's station table
1250 * and marks it driver active, so that it will be restored to the
1251 * device at the next best time.
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001252 */
Johannes Berg2c810cc2010-04-29 00:53:29 -07001253int iwl_alloc_bcast_station(struct iwl_priv *priv, bool init_lq)
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001254{
Johannes Berg2c810cc2010-04-29 00:53:29 -07001255 struct iwl_link_quality_cmd *link_cmd;
1256 unsigned long flags;
1257 u8 sta_id;
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001258
Johannes Berg2c810cc2010-04-29 00:53:29 -07001259 spin_lock_irqsave(&priv->sta_lock, flags);
1260 sta_id = iwl_prep_station(priv, iwl_bcast_addr, false, NULL);
1261 if (sta_id == IWL_INVALID_STATION) {
1262 IWL_ERR(priv, "Unable to prepare broadcast station\n");
1263 spin_unlock_irqrestore(&priv->sta_lock, flags);
1264
1265 return -EINVAL;
1266 }
1267
1268 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1269 priv->stations[sta_id].used |= IWL_STA_BCAST;
1270 spin_unlock_irqrestore(&priv->sta_lock, flags);
1271
1272 if (init_lq) {
1273 link_cmd = iwl_sta_alloc_lq(priv, sta_id);
1274 if (!link_cmd) {
1275 IWL_ERR(priv,
1276 "Unable to initialize rate scaling for bcast station.\n");
1277 return -ENOMEM;
1278 }
1279
1280 spin_lock_irqsave(&priv->sta_lock, flags);
1281 priv->stations[sta_id].lq = link_cmd;
1282 spin_unlock_irqrestore(&priv->sta_lock, flags);
1283 }
1284
1285 return 0;
1286}
1287EXPORT_SYMBOL_GPL(iwl_alloc_bcast_station);
1288
1289void iwl_dealloc_bcast_station(struct iwl_priv *priv)
Reinette Chatre3459ab52010-01-22 14:22:49 -08001290{
Johannes Berg2c810cc2010-04-29 00:53:29 -07001291 unsigned long flags;
1292 int i;
Reinette Chatred2e210a2010-04-23 10:33:33 -07001293
Johannes Berg2c810cc2010-04-29 00:53:29 -07001294 spin_lock_irqsave(&priv->sta_lock, flags);
1295 for (i = 0; i < priv->hw_params.max_stations; i++) {
1296 if (!(priv->stations[i].used & IWL_STA_BCAST))
1297 continue;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001298
Johannes Berg2c810cc2010-04-29 00:53:29 -07001299 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
1300 priv->num_stations--;
1301 BUG_ON(priv->num_stations < 0);
1302 kfree(priv->stations[i].lq);
1303 priv->stations[i].lq = NULL;
1304 }
1305 spin_unlock_irqrestore(&priv->sta_lock, flags);
Reinette Chatre3459ab52010-01-22 14:22:49 -08001306}
Johannes Berg2c810cc2010-04-29 00:53:29 -07001307EXPORT_SYMBOL_GPL(iwl_dealloc_bcast_station);
Reinette Chatre3459ab52010-01-22 14:22:49 -08001308
1309/**
Tomas Winkler9f586712008-11-12 13:14:05 -08001310 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
Tomas Winkler5083e562008-05-29 16:35:15 +08001311 */
Tomas Winkler9f586712008-11-12 13:14:05 -08001312void iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
Tomas Winkler5083e562008-05-29 16:35:15 +08001313{
1314 unsigned long flags;
1315
1316 /* Remove "disable" flag, to enable Tx for this TID */
1317 spin_lock_irqsave(&priv->sta_lock, flags);
1318 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1319 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1320 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1321 spin_unlock_irqrestore(&priv->sta_lock, flags);
1322
1323 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1324}
Tomas Winkler9f586712008-11-12 13:14:05 -08001325EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
1326
Johannes Berg619753f2010-04-30 11:30:46 -07001327int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1328 int tid, u16 ssn)
Tomas Winkler9f586712008-11-12 13:14:05 -08001329{
1330 unsigned long flags;
1331 int sta_id;
1332
Johannes Berg619753f2010-04-30 11:30:46 -07001333 sta_id = iwl_sta_id(sta);
Tomas Winkler9f586712008-11-12 13:14:05 -08001334 if (sta_id == IWL_INVALID_STATION)
1335 return -ENXIO;
1336
1337 spin_lock_irqsave(&priv->sta_lock, flags);
1338 priv->stations[sta_id].sta.station_flags_msk = 0;
1339 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1340 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1341 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1342 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1343 spin_unlock_irqrestore(&priv->sta_lock, flags);
1344
1345 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
Johannes Berg619753f2010-04-30 11:30:46 -07001346 CMD_ASYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001347}
1348EXPORT_SYMBOL(iwl_sta_rx_agg_start);
1349
Johannes Berg619753f2010-04-30 11:30:46 -07001350int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1351 int tid)
Tomas Winkler9f586712008-11-12 13:14:05 -08001352{
1353 unsigned long flags;
1354 int sta_id;
1355
Johannes Berg619753f2010-04-30 11:30:46 -07001356 sta_id = iwl_sta_id(sta);
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001357 if (sta_id == IWL_INVALID_STATION) {
1358 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
Tomas Winkler9f586712008-11-12 13:14:05 -08001359 return -ENXIO;
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001360 }
Tomas Winkler9f586712008-11-12 13:14:05 -08001361
1362 spin_lock_irqsave(&priv->sta_lock, flags);
1363 priv->stations[sta_id].sta.station_flags_msk = 0;
1364 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1365 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1366 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1367 spin_unlock_irqrestore(&priv->sta_lock, flags);
1368
1369 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1370 CMD_ASYNC);
1371}
1372EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
1373
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001374void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
Tomas Winkler9f586712008-11-12 13:14:05 -08001375{
1376 unsigned long flags;
1377
1378 spin_lock_irqsave(&priv->sta_lock, flags);
1379 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1380 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1381 priv->stations[sta_id].sta.sta.modify_mask = 0;
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001382 priv->stations[sta_id].sta.sleep_tx_count = 0;
Tomas Winkler9f586712008-11-12 13:14:05 -08001383 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1384 spin_unlock_irqrestore(&priv->sta_lock, flags);
1385
1386 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1387}
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001388EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
Tomas Winkler9f586712008-11-12 13:14:05 -08001389
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001390void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
Tomas Winkler9f586712008-11-12 13:14:05 -08001391{
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001392 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -08001393
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001394 spin_lock_irqsave(&priv->sta_lock, flags);
1395 priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
1396 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1397 priv->stations[sta_id].sta.sta.modify_mask =
1398 STA_MODIFY_SLEEP_TX_COUNT_MSK;
1399 priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
1400 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1401 spin_unlock_irqrestore(&priv->sta_lock, flags);
Tomas Winkler9f586712008-11-12 13:14:05 -08001402
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001403 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001404}
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -07001405EXPORT_SYMBOL(iwl_sta_modify_sleep_tx_count);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001406
1407int iwl_mac_sta_remove(struct ieee80211_hw *hw,
Johannes Bergfd1af152010-04-30 11:30:43 -07001408 struct ieee80211_vif *vif,
1409 struct ieee80211_sta *sta)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001410{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001411 struct iwl_priv *priv = hw->priv;
Johannes Bergfd1af152010-04-30 11:30:43 -07001412 struct iwl_station_priv_common *sta_common = (void *)sta->drv_priv;
1413 int ret;
1414
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001415 IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
1416 sta->addr);
Johannes Bergfd1af152010-04-30 11:30:43 -07001417 ret = iwl_remove_station(priv, sta_common->sta_id, sta->addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001418 if (ret)
1419 IWL_ERR(priv, "Error removing station %pM\n",
1420 sta->addr);
1421 return ret;
1422}
1423EXPORT_SYMBOL(iwl_mac_sta_remove);