blob: ba36df5e2c787c2f1aba6a64bfd8fc2d9222f031 [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
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080038/* priv->sta_lock must be held */
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080039static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
40{
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080041
42 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
43 IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u addr %pM\n",
44 sta_id, priv->stations[sta_id].sta.sta.addr);
45
46 if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
47 IWL_DEBUG_ASSOC(priv,
48 "STA id %u addr %pM already present in uCode (according to driver)\n",
49 sta_id, priv->stations[sta_id].sta.sta.addr);
50 } else {
51 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
52 IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
53 sta_id, priv->stations[sta_id].sta.sta.addr);
54 }
55}
56
57static void iwl_process_add_sta_resp(struct iwl_priv *priv,
58 struct iwl_addsta_cmd *addsta,
59 struct iwl_rx_packet *pkt,
60 bool sync)
61{
62 u8 sta_id = addsta->sta.sta_id;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080063 unsigned long flags;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080064
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080065 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
66 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
67 pkt->hdr.flags);
68 return;
69 }
70
71 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
72 sta_id);
73
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080074 spin_lock_irqsave(&priv->sta_lock, flags);
75
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080076 switch (pkt->u.add_sta.status) {
77 case ADD_STA_SUCCESS_MSK:
78 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
79 iwl_sta_ucode_activate(priv, sta_id);
80 break;
81 case ADD_STA_NO_ROOM_IN_TABLE:
82 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
Winkler, Tomas15b16872008-12-19 10:37:33 +080083 sta_id);
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080084 break;
85 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
86 IWL_ERR(priv, "Adding station %d failed, no block ack resource.\n",
87 sta_id);
88 break;
89 case ADD_STA_MODIFY_NON_EXIST_STA:
Frans Pop91dd6c22010-03-24 14:19:58 -070090 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080091 sta_id);
92 break;
93 default:
94 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
95 pkt->u.add_sta.status);
96 break;
97 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080098
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080099 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
100 priv->stations[sta_id].sta.mode ==
101 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
102 sta_id, priv->stations[sta_id].sta.sta.addr);
103
104 /*
105 * XXX: The MAC address in the command buffer is often changed from
106 * the original sent to the device. That is, the MAC address
107 * written to the command buffer often is not the same MAC adress
108 * read from the command buffer when the command returns. This
109 * issue has not yet been resolved and this debugging is left to
110 * observe the problem.
111 */
112 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
113 priv->stations[sta_id].sta.mode ==
114 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
115 addsta->sta.addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800116 spin_unlock_irqrestore(&priv->sta_lock, flags);
117}
118
Johannes Berg5696aea2009-07-24 11:13:06 -0700119static void iwl_add_sta_callback(struct iwl_priv *priv,
120 struct iwl_device_cmd *cmd,
Zhu Yi2f301222009-10-09 17:19:45 +0800121 struct iwl_rx_packet *pkt)
Tomas Winkler42132bc2008-05-29 16:35:03 +0800122{
Tomas Winkler3257e5d2008-10-14 12:32:43 -0700123 struct iwl_addsta_cmd *addsta =
124 (struct iwl_addsta_cmd *)cmd->cmd.payload;
Tomas Winkler42132bc2008-05-29 16:35:03 +0800125
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800126 iwl_process_add_sta_resp(priv, addsta, pkt, false);
Tomas Winkler42132bc2008-05-29 16:35:03 +0800127
Tomas Winkler42132bc2008-05-29 16:35:03 +0800128}
129
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800130int iwl_send_add_sta(struct iwl_priv *priv,
Tomas Winkler133636d2008-05-05 10:22:34 +0800131 struct iwl_addsta_cmd *sta, u8 flags)
132{
Zhu Yi2f301222009-10-09 17:19:45 +0800133 struct iwl_rx_packet *pkt = NULL;
Tomas Winkler133636d2008-05-05 10:22:34 +0800134 int ret = 0;
135 u8 data[sizeof(*sta)];
136 struct iwl_host_cmd cmd = {
137 .id = REPLY_ADD_STA,
Johannes Bergc2acea82009-07-24 11:13:05 -0700138 .flags = flags,
Tomas Winkler133636d2008-05-05 10:22:34 +0800139 .data = data,
140 };
Reinette Chatref875f512010-04-05 10:43:10 -0700141 u8 sta_id __maybe_unused = sta->sta.sta_id;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800142
143 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
144 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
Tomas Winkler133636d2008-05-05 10:22:34 +0800145
Tomas Winkler42132bc2008-05-29 16:35:03 +0800146 if (flags & CMD_ASYNC)
Johannes Bergc2acea82009-07-24 11:13:05 -0700147 cmd.callback = iwl_add_sta_callback;
Tomas Winkler42132bc2008-05-29 16:35:03 +0800148 else
Johannes Bergc2acea82009-07-24 11:13:05 -0700149 cmd.flags |= CMD_WANT_SKB;
Tomas Winkler133636d2008-05-05 10:22:34 +0800150
151 cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
152 ret = iwl_send_cmd(priv, &cmd);
153
154 if (ret || (flags & CMD_ASYNC))
155 return ret;
156
Tomas Winkler133636d2008-05-05 10:22:34 +0800157 if (ret == 0) {
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800158 pkt = (struct iwl_rx_packet *)cmd.reply_page;
159 iwl_process_add_sta_resp(priv, sta, pkt, true);
Tomas Winkler133636d2008-05-05 10:22:34 +0800160 }
Zhu Yi64a76b52009-12-10 14:37:21 -0800161 iwl_free_pages(priv, cmd.reply_page);
Tomas Winkler133636d2008-05-05 10:22:34 +0800162
163 return ret;
164}
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800165EXPORT_SYMBOL(iwl_send_add_sta);
Tomas Winkler947b13a2008-04-16 16:34:48 -0700166
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800167static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200168 struct ieee80211_sta_ht_cap *sta_ht_inf)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800169{
170 __le32 sta_flags;
171 u8 mimo_ps_mode;
172
173 if (!sta_ht_inf || !sta_ht_inf->ht_supported)
174 goto done;
175
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800176 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
Wey-Yi Guy3f3e0372009-10-30 14:36:17 -0700177 IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
178 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
179 "static" :
180 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
181 "dynamic" : "disabled");
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800182
183 sta_flags = priv->stations[index].sta.station_flags;
184
185 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
186
187 switch (mimo_ps_mode) {
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800188 case WLAN_HT_CAP_SM_PS_STATIC:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800189 sta_flags |= STA_FLG_MIMO_DIS_MSK;
190 break;
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800191 case WLAN_HT_CAP_SM_PS_DYNAMIC:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800192 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
193 break;
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800194 case WLAN_HT_CAP_SM_PS_DISABLED:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800195 break;
196 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800197 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800198 break;
199 }
200
201 sta_flags |= cpu_to_le32(
202 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
203
204 sta_flags |= cpu_to_le32(
205 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
206
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700207 if (iwl_is_ht40_tx_allowed(priv, sta_ht_inf))
208 sta_flags |= STA_FLG_HT40_EN_MSK;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800209 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700210 sta_flags &= ~STA_FLG_HT40_EN_MSK;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800211
212 priv->stations[index].sta.station_flags = sta_flags;
213 done:
214 return;
215}
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800216
217/**
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800218 * iwl_prep_station - Prepare station information for addition
219 *
220 * should be called with sta_lock held
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800221 */
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800222static u8 iwl_prep_station(struct iwl_priv *priv, const u8 *addr,
223 bool is_ap,
224 struct ieee80211_sta_ht_cap *ht_info)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800225{
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800226 struct iwl_station_entry *station;
Tomas Winklerc587de02009-06-03 11:44:07 -0700227 int i;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800228 u8 sta_id = IWL_INVALID_STATION;
Tomas Winklerc587de02009-06-03 11:44:07 -0700229 u16 rate;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800230
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800231 if (is_ap)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800232 sta_id = IWL_AP_ID;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800233 else if (is_broadcast_ether_addr(addr))
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800234 sta_id = priv->hw_params.bcast_sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800235 else
236 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
237 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
238 addr)) {
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800239 sta_id = i;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800240 break;
241 }
242
243 if (!priv->stations[i].used &&
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800244 sta_id == IWL_INVALID_STATION)
245 sta_id = i;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800246 }
247
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800248 /*
249 * These two conditions have the same outcome, but keep them
250 * separate
251 */
252 if (unlikely(sta_id == IWL_INVALID_STATION))
253 return sta_id;
254
255 /*
256 * uCode is not able to deal with multiple requests to add a
257 * station. Keep track if one is in progress so that we do not send
258 * another.
259 */
260 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
261 IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
262 sta_id);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800263 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800264 }
265
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800266 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
267 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800268 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800269 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
270 sta_id, addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800271 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800272 }
273
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800274 station = &priv->stations[sta_id];
275 station->used = IWL_STA_DRIVER_ACTIVE;
Tomas Winklere1623442009-01-27 14:27:56 -0800276 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700277 sta_id, addr);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800278 priv->num_stations++;
279
280 /* Set up the REPLY_ADD_STA command to send to device */
281 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
282 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
283 station->sta.mode = 0;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800284 station->sta.sta.sta_id = sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800285 station->sta.station_flags = 0;
286
287 /* BCAST station and IBSS stations do not work in HT mode */
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800288 if (sta_id != priv->hw_params.bcast_sta_id &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200289 priv->iw_mode != NL80211_IFTYPE_ADHOC)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800290 iwl_set_ht_add_station(priv, sta_id, ht_info);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800291
Tomas Winklerc587de02009-06-03 11:44:07 -0700292 /* 3945 only */
293 rate = (priv->band == IEEE80211_BAND_5GHZ) ?
294 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
295 /* Turn on both antennas for the station... */
296 station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
297
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800298 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800299
300}
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800301
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800302#define STA_WAIT_TIMEOUT (HZ/2)
303
304/**
305 * iwl_add_station_common -
306 */
307int iwl_add_station_common(struct iwl_priv *priv, const u8 *addr,
308 bool is_ap,
309 struct ieee80211_sta_ht_cap *ht_info,
310 u8 *sta_id_r)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800311{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800312 struct iwl_station_entry *station;
313 unsigned long flags_spin;
314 int ret = 0;
315 u8 sta_id;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800316
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800317 *sta_id_r = 0;
318 spin_lock_irqsave(&priv->sta_lock, flags_spin);
319 sta_id = iwl_prep_station(priv, addr, is_ap, ht_info);
320 if (sta_id == IWL_INVALID_STATION) {
321 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
322 addr);
323 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
324 return -EINVAL;
325 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800326
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800327 /*
328 * uCode is not able to deal with multiple requests to add a
329 * station. Keep track if one is in progress so that we do not send
330 * another.
331 */
332 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
333 IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
334 sta_id);
335 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
336 return -EEXIST;
337 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800338
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800339 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
340 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
341 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
342 sta_id, addr);
343 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
344 return -EEXIST;
345 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800346
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800347 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
348 station = &priv->stations[sta_id];
349 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
350
351 /* Add station to device's station table */
352 ret = iwl_send_add_sta(priv, &station->sta, CMD_SYNC);
353 if (ret) {
354 IWL_ERR(priv, "Adding station %pM failed.\n", station->sta.sta.addr);
355 spin_lock_irqsave(&priv->sta_lock, flags_spin);
356 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
357 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
358 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
359 }
360 *sta_id_r = sta_id;
361 return ret;
362}
363EXPORT_SYMBOL(iwl_add_station_common);
364
Johannes Berga6a03452010-04-29 10:41:07 -0700365static struct iwl_link_quality_cmd *iwl_sta_alloc_lq(struct iwl_priv *priv,
366 u8 sta_id)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800367{
368 int i, r;
Reinette Chatred2e210a2010-04-23 10:33:33 -0700369 struct iwl_link_quality_cmd *link_cmd;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800370 u32 rate_flags;
371
Reinette Chatred2e210a2010-04-23 10:33:33 -0700372 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
373 if (!link_cmd) {
374 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
375 return NULL;
376 }
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800377 /* Set up the rate scaling to start at selected rate, fall back
378 * all the way down to 1M in IEEE order, and then spin on 1M */
Johannes Berg156b70d2010-04-28 14:44:45 -0700379 if (priv->band == IEEE80211_BAND_5GHZ)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800380 r = IWL_RATE_6M_INDEX;
381 else
382 r = IWL_RATE_1M_INDEX;
383
384 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
385 rate_flags = 0;
386 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
387 rate_flags |= RATE_MCS_CCK_MSK;
388
389 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
390 RATE_MCS_ANT_POS;
391
Reinette Chatred2e210a2010-04-23 10:33:33 -0700392 link_cmd->rs_table[i].rate_n_flags =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800393 iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
394 r = iwl_get_prev_ieee_rate(r);
395 }
396
Reinette Chatred2e210a2010-04-23 10:33:33 -0700397 link_cmd->general_params.single_stream_ant_msk =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800398 first_antenna(priv->hw_params.valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700399
Reinette Chatred2e210a2010-04-23 10:33:33 -0700400 link_cmd->general_params.dual_stream_ant_msk =
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700401 priv->hw_params.valid_tx_ant &
402 ~first_antenna(priv->hw_params.valid_tx_ant);
Reinette Chatred2e210a2010-04-23 10:33:33 -0700403 if (!link_cmd->general_params.dual_stream_ant_msk) {
404 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700405 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
Reinette Chatred2e210a2010-04-23 10:33:33 -0700406 link_cmd->general_params.dual_stream_ant_msk =
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700407 priv->hw_params.valid_tx_ant;
408 }
409
Reinette Chatred2e210a2010-04-23 10:33:33 -0700410 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
411 link_cmd->agg_params.agg_time_limit =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800412 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
413
Johannes Berg156b70d2010-04-28 14:44:45 -0700414 link_cmd->sta_id = sta_id;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800415
Reinette Chatred2e210a2010-04-23 10:33:33 -0700416 return link_cmd;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800417}
418
419/*
Johannes Bergfd1af152010-04-30 11:30:43 -0700420 * iwl_add_local_station - Add stations not requested by mac80211
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800421 *
422 * This will be either the broadcast station or the bssid station needed by
423 * ad-hoc.
424 *
425 * Function sleeps.
426 */
Johannes Bergfd1af152010-04-30 11:30:43 -0700427int iwl_add_local_station(struct iwl_priv *priv, const u8 *addr, bool init_rs,
428 u8 *sta_id_r)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800429{
430 int ret;
431 u8 sta_id;
Reinette Chatred2e210a2010-04-23 10:33:33 -0700432 struct iwl_link_quality_cmd *link_cmd;
433 unsigned long flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800434
Johannes Bergfd1af152010-04-30 11:30:43 -0700435 if (*sta_id_r)
436 *sta_id_r = IWL_INVALID_STATION;
437
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800438 ret = iwl_add_station_common(priv, addr, 0, NULL, &sta_id);
439 if (ret) {
440 IWL_ERR(priv, "Unable to add station %pM\n", addr);
441 return ret;
442 }
443
Johannes Bergfd1af152010-04-30 11:30:43 -0700444 if (sta_id_r)
445 *sta_id_r = sta_id;
446
Reinette Chatred2e210a2010-04-23 10:33:33 -0700447 spin_lock_irqsave(&priv->sta_lock, flags);
448 priv->stations[sta_id].used |= IWL_STA_LOCAL;
449 spin_unlock_irqrestore(&priv->sta_lock, flags);
450
451 if (init_rs) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800452 /* Set up default rate scaling table in device's station table */
Johannes Berga6a03452010-04-29 10:41:07 -0700453 link_cmd = iwl_sta_alloc_lq(priv, sta_id);
Reinette Chatred2e210a2010-04-23 10:33:33 -0700454 if (!link_cmd) {
455 IWL_ERR(priv, "Unable to initialize rate scaling for station %pM.\n",
456 addr);
457 return -ENOMEM;
458 }
Johannes Berga6a03452010-04-29 10:41:07 -0700459
460 ret = iwl_send_lq_cmd(priv, link_cmd, CMD_SYNC, true);
461 if (ret)
462 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
463
Reinette Chatred2e210a2010-04-23 10:33:33 -0700464 spin_lock_irqsave(&priv->sta_lock, flags);
465 priv->stations[sta_id].lq = link_cmd;
466 spin_unlock_irqrestore(&priv->sta_lock, flags);
467 }
468
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800469 return 0;
470}
471EXPORT_SYMBOL(iwl_add_local_station);
472
473/**
474 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
475 *
476 * priv->sta_lock must be held
477 */
478static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
479{
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800480 /* Ucode must be active and driver must be non active */
Reinette Chatred2e210a2010-04-23 10:33:33 -0700481 if ((priv->stations[sta_id].used &
482 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) != IWL_STA_UCODE_ACTIVE)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800483 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800484
485 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
486
487 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800488 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800489}
490
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800491static int iwl_send_remove_station(struct iwl_priv *priv,
492 struct iwl_station_entry *station)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800493{
Zhu Yi2f301222009-10-09 17:19:45 +0800494 struct iwl_rx_packet *pkt;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800495 int ret;
496
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800497 unsigned long flags_spin;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800498 struct iwl_rem_sta_cmd rm_sta_cmd;
499
500 struct iwl_host_cmd cmd = {
501 .id = REPLY_REMOVE_STA,
502 .len = sizeof(struct iwl_rem_sta_cmd),
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800503 .flags = CMD_SYNC,
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800504 .data = &rm_sta_cmd,
505 };
506
507 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
508 rm_sta_cmd.num_sta = 1;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800509 memcpy(&rm_sta_cmd.addr, &station->sta.sta.addr , ETH_ALEN);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800510
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800511 cmd.flags |= CMD_WANT_SKB;
512
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800513 ret = iwl_send_cmd(priv, &cmd);
514
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800515 if (ret)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800516 return ret;
517
Zhu Yi2f301222009-10-09 17:19:45 +0800518 pkt = (struct iwl_rx_packet *)cmd.reply_page;
519 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800520 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800521 pkt->hdr.flags);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800522 ret = -EIO;
523 }
524
525 if (!ret) {
Zhu Yi2f301222009-10-09 17:19:45 +0800526 switch (pkt->u.rem_sta.status) {
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800527 case REM_STA_SUCCESS_MSK:
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800528 spin_lock_irqsave(&priv->sta_lock, flags_spin);
529 iwl_sta_ucode_deactivate(priv, station->sta.sta.sta_id);
530 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Tomas Winklere1623442009-01-27 14:27:56 -0800531 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800532 break;
533 default:
534 ret = -EIO;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800535 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800536 break;
537 }
538 }
Zhu Yi64a76b52009-12-10 14:37:21 -0800539 iwl_free_pages(priv, cmd.reply_page);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800540
541 return ret;
542}
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +0800543
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800544/**
545 * iwl_remove_station - Remove driver's knowledge of station.
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800546 */
Johannes Bergfd1af152010-04-30 11:30:43 -0700547int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
548 const u8 *addr)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800549{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800550 struct iwl_station_entry *station;
Johannes Bergfd1af152010-04-30 11:30:43 -0700551 unsigned long flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800552
553 if (!iwl_is_ready(priv)) {
554 IWL_DEBUG_INFO(priv,
Frans Pop91dd6c22010-03-24 14:19:58 -0700555 "Unable to remove station %pM, device not ready.\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700556 addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800557 /*
558 * It is typical for stations to be removed when we are
559 * going down. Return success since device will be down
560 * soon anyway
561 */
562 return 0;
563 }
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800564
Tomas Winklere1623442009-01-27 14:27:56 -0800565 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
Johannes Bergfd1af152010-04-30 11:30:43 -0700566 sta_id, addr);
567
568 if (WARN_ON(sta_id == IWL_INVALID_STATION))
569 return -EINVAL;
570
571 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800572
573 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800574 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700575 addr);
Johannes Bergfd1af152010-04-30 11:30:43 -0700576 goto out_err;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800577 }
578
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800579 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800580 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700581 addr);
Johannes Bergfd1af152010-04-30 11:30:43 -0700582 goto out_err;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800583 }
584
Johannes Berg1fa61b22010-04-28 08:44:52 -0700585 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
586 kfree(priv->stations[sta_id].lq);
587 priv->stations[sta_id].lq = NULL;
588 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800589
590 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
591
592 priv->num_stations--;
593
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800594 BUG_ON(priv->num_stations < 0);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800595
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800596 station = &priv->stations[sta_id];
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800597 spin_unlock_irqrestore(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800598
Johannes Bergfd1af152010-04-30 11:30:43 -0700599 return iwl_send_remove_station(priv, station);
600out_err:
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800601 spin_unlock_irqrestore(&priv->sta_lock, flags);
Johannes Bergfd1af152010-04-30 11:30:43 -0700602 return -EINVAL;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800603}
Johannes Berg1fa61b22010-04-28 08:44:52 -0700604EXPORT_SYMBOL_GPL(iwl_remove_station);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800605
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800606/**
Johannes Berg2c810cc2010-04-29 00:53:29 -0700607 * iwl_clear_ucode_stations - clear ucode station table bits
608 *
609 * This function clears all the bits in the driver indicating
610 * which stations are active in the ucode. Call when something
611 * other than explicit station management would cause this in
612 * the ucode, e.g. unassociated RXON.
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800613 */
Johannes Berg2c810cc2010-04-29 00:53:29 -0700614void iwl_clear_ucode_stations(struct iwl_priv *priv)
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800615{
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700616 int i;
Reinette Chatre7e246192010-02-18 22:58:32 -0800617 unsigned long flags_spin;
618 bool cleared = false;
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800619
Johannes Berg2c810cc2010-04-29 00:53:29 -0700620 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700621
Reinette Chatre7e246192010-02-18 22:58:32 -0800622 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Johannes Berg2c810cc2010-04-29 00:53:29 -0700623 for (i = 0; i < priv->hw_params.max_stations; i++) {
624 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
625 IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d\n", i);
626 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
627 cleared = true;
Reinette Chatre7e246192010-02-18 22:58:32 -0800628 }
629 }
630 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
631
632 if (!cleared)
633 IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800634}
Reinette Chatre7e246192010-02-18 22:58:32 -0800635EXPORT_SYMBOL(iwl_clear_ucode_stations);
636
637/**
638 * iwl_restore_stations() - Restore driver known stations to device
639 *
640 * All stations considered active by driver, but not present in ucode, is
641 * restored.
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800642 *
643 * Function sleeps.
Reinette Chatre7e246192010-02-18 22:58:32 -0800644 */
645void iwl_restore_stations(struct iwl_priv *priv)
646{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800647 struct iwl_station_entry *station;
Reinette Chatre7e246192010-02-18 22:58:32 -0800648 unsigned long flags_spin;
649 int i;
650 bool found = false;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800651 int ret;
Reinette Chatre7e246192010-02-18 22:58:32 -0800652
653 if (!iwl_is_ready(priv)) {
654 IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
655 return;
656 }
657
658 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
659 spin_lock_irqsave(&priv->sta_lock, flags_spin);
660 for (i = 0; i < priv->hw_params.max_stations; i++) {
661 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
662 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
663 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
664 priv->stations[i].sta.sta.addr);
665 priv->stations[i].sta.mode = 0;
666 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
667 found = true;
668 }
669 }
670
671 for (i = 0; i < priv->hw_params.max_stations; i++) {
672 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800673 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
674 station = &priv->stations[i];
675 ret = iwl_send_add_sta(priv, &priv->stations[i].sta, CMD_SYNC);
676 if (ret) {
677 IWL_ERR(priv, "Adding station %pM failed.\n",
678 station->sta.sta.addr);
679 spin_lock_irqsave(&priv->sta_lock, flags_spin);
680 priv->stations[i].used &= ~IWL_STA_DRIVER_ACTIVE;
681 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
682 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
683 }
684 /*
685 * Rate scaling has already been initialized, send
686 * current LQ command
687 */
688 if (station->lq)
689 iwl_send_lq_cmd(priv, station->lq, CMD_SYNC, true);
690 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Reinette Chatre7e246192010-02-18 22:58:32 -0800691 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
692 }
693 }
694
695 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
696 if (!found)
697 IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
698 else
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800699 IWL_DEBUG_INFO(priv, "Restoring all known stations .... complete.\n");
Reinette Chatre7e246192010-02-18 22:58:32 -0800700}
701EXPORT_SYMBOL(iwl_restore_stations);
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800702
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800703int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700704{
705 int i;
706
707 for (i = 0; i < STA_KEY_MAX_NUM; i++)
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700708 if (!test_and_set_bit(i, &priv->ucode_key_table))
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700709 return i;
710
Tomas Winkler40a9a822008-11-25 23:29:03 +0200711 return WEP_INVALID_OFFSET;
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700712}
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800713EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700714
Johannes Berg335348b2010-03-30 10:11:46 -0700715static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700716{
717 int i, not_empty = 0;
718 u8 buff[sizeof(struct iwl_wep_cmd) +
719 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
720 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
721 size_t cmd_size = sizeof(struct iwl_wep_cmd);
722 struct iwl_host_cmd cmd = {
723 .id = REPLY_WEPKEY,
724 .data = wep_cmd,
Johannes Berg72e15d72010-02-19 11:42:32 -0800725 .flags = CMD_SYNC,
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700726 };
727
Johannes Berg72e15d72010-02-19 11:42:32 -0800728 might_sleep();
729
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700730 memset(wep_cmd, 0, cmd_size +
731 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
732
733 for (i = 0; i < WEP_KEYS_MAX ; i++) {
734 wep_cmd->key[i].key_index = i;
735 if (priv->wep_keys[i].key_size) {
736 wep_cmd->key[i].key_offset = i;
737 not_empty = 1;
738 } else {
739 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
740 }
741
742 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
743 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
744 priv->wep_keys[i].key_size);
745 }
746
747 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
748 wep_cmd->num_keys = WEP_KEYS_MAX;
749
750 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
751
752 cmd.len = cmd_size;
753
754 if (not_empty || send_if_empty)
755 return iwl_send_cmd(priv, &cmd);
756 else
757 return 0;
758}
Johannes Berg335348b2010-03-30 10:11:46 -0700759
760int iwl_restore_default_wep_keys(struct iwl_priv *priv)
761{
762 WARN_ON(!mutex_is_locked(&priv->mutex));
763
764 return iwl_send_static_wepkey_cmd(priv, 0);
765}
766EXPORT_SYMBOL(iwl_restore_default_wep_keys);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700767
768int iwl_remove_default_wep_key(struct iwl_priv *priv,
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700769 struct ieee80211_key_conf *keyconf)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700770{
771 int ret;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700772
Johannes Berg72e15d72010-02-19 11:42:32 -0800773 WARN_ON(!mutex_is_locked(&priv->mutex));
774
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700775 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
776 keyconf->keyidx);
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700777
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700778 memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700779 if (iwl_is_rfkill(priv)) {
780 IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berg72e15d72010-02-19 11:42:32 -0800781 /* but keys in device are clear anyway so return success */
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700782 return 0;
783 }
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700784 ret = iwl_send_static_wepkey_cmd(priv, 1);
Tomas Winklere1623442009-01-27 14:27:56 -0800785 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800786 keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700787
788 return ret;
789}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800790EXPORT_SYMBOL(iwl_remove_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700791
792int iwl_set_default_wep_key(struct iwl_priv *priv,
793 struct ieee80211_key_conf *keyconf)
794{
795 int ret;
Johannes Berg72e15d72010-02-19 11:42:32 -0800796
797 WARN_ON(!mutex_is_locked(&priv->mutex));
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700798
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800799 if (keyconf->keylen != WEP_KEY_LEN_128 &&
800 keyconf->keylen != WEP_KEY_LEN_64) {
Tomas Winklere1623442009-01-27 14:27:56 -0800801 IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800802 return -EINVAL;
803 }
804
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700805 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800806 keyconf->hw_key_idx = HW_KEY_DEFAULT;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700807 priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
808
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700809 priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
810 memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
811 keyconf->keylen);
812
813 ret = iwl_send_static_wepkey_cmd(priv, 0);
Tomas Winklere1623442009-01-27 14:27:56 -0800814 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800815 keyconf->keylen, keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700816
817 return ret;
818}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800819EXPORT_SYMBOL(iwl_set_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700820
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700821static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700822 struct ieee80211_key_conf *keyconf,
823 u8 sta_id)
824{
825 unsigned long flags;
826 __le16 key_flags = 0;
827 int ret;
828
829 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700830
831 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
832 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
833 key_flags &= ~STA_KEY_FLG_INVALID;
834
835 if (keyconf->keylen == WEP_KEY_LEN_128)
836 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
837
Tomas Winkler5425e492008-04-15 16:01:38 -0700838 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700839 key_flags |= STA_KEY_MULTICAST_MSK;
840
841 spin_lock_irqsave(&priv->sta_lock, flags);
842
843 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
844 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
845 priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
846
847 memcpy(priv->stations[sta_id].keyinfo.key,
848 keyconf->key, keyconf->keylen);
849
850 memcpy(&priv->stations[sta_id].sta.key.key[3],
851 keyconf->key, keyconf->keylen);
852
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700853 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
854 == STA_KEY_FLG_NO_ENC)
855 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700856 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700857 /* else, we are overriding an existing key => no need to allocated room
858 * in uCode. */
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700859
Tomas Winkler40a9a822008-11-25 23:29:03 +0200860 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100861 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200862
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700863 priv->stations[sta_id].sta.key.key_flags = key_flags;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700864 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
865 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
866
Tomas Winkler133636d2008-05-05 10:22:34 +0800867 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700868
869 spin_unlock_irqrestore(&priv->sta_lock, flags);
870
871 return ret;
872}
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700873
874static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
875 struct ieee80211_key_conf *keyconf,
876 u8 sta_id)
877{
878 unsigned long flags;
879 __le16 key_flags = 0;
Tomas Winkler40a9a822008-11-25 23:29:03 +0200880 int ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700881
882 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
883 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
884 key_flags &= ~STA_KEY_FLG_INVALID;
885
Tomas Winkler5425e492008-04-15 16:01:38 -0700886 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700887 key_flags |= STA_KEY_MULTICAST_MSK;
888
889 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700890
891 spin_lock_irqsave(&priv->sta_lock, flags);
892 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
893 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
894
895 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
896 keyconf->keylen);
897
898 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
899 keyconf->keylen);
900
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700901 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
902 == STA_KEY_FLG_NO_ENC)
903 priv->stations[sta_id].sta.key.key_offset =
904 iwl_get_free_ucode_key_index(priv);
905 /* else, we are overriding an existing key => no need to allocated room
906 * in uCode. */
907
Tomas Winkler40a9a822008-11-25 23:29:03 +0200908 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100909 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200910
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700911 priv->stations[sta_id].sta.key.key_flags = key_flags;
912 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
913 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
914
Tomas Winkler40a9a822008-11-25 23:29:03 +0200915 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
916
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700917 spin_unlock_irqrestore(&priv->sta_lock, flags);
918
Tomas Winkler40a9a822008-11-25 23:29:03 +0200919 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700920}
921
922static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
923 struct ieee80211_key_conf *keyconf,
924 u8 sta_id)
925{
926 unsigned long flags;
927 int ret = 0;
Reinette Chatre299f5462009-04-30 13:56:31 -0700928 __le16 key_flags = 0;
929
930 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
931 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
932 key_flags &= ~STA_KEY_FLG_INVALID;
933
934 if (sta_id == priv->hw_params.bcast_sta_id)
935 key_flags |= STA_KEY_MULTICAST_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700936
937 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
938 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700939
940 spin_lock_irqsave(&priv->sta_lock, flags);
941
942 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700943 priv->stations[sta_id].keyinfo.keylen = 16;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700944
945 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
946 == STA_KEY_FLG_NO_ENC)
947 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700948 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700949 /* else, we are overriding an existing key => no need to allocated room
950 * in uCode. */
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700951
Tomas Winkler40a9a822008-11-25 23:29:03 +0200952 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100953 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200954
Reinette Chatre299f5462009-04-30 13:56:31 -0700955 priv->stations[sta_id].sta.key.key_flags = key_flags;
956
957
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700958 /* This copy is acutally not needed: we get the key with each TX */
959 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
960
961 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
962
963 spin_unlock_irqrestore(&priv->sta_lock, flags);
964
965 return ret;
966}
967
Tomas Winkler9f586712008-11-12 13:14:05 -0800968void iwl_update_tkip_key(struct iwl_priv *priv,
969 struct ieee80211_key_conf *keyconf,
Johannes Bergbdbb6122010-04-30 13:53:37 -0700970 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
Tomas Winkler9f586712008-11-12 13:14:05 -0800971{
Johannes Bergbdbb6122010-04-30 13:53:37 -0700972 u8 sta_id;
Tomas Winkler9f586712008-11-12 13:14:05 -0800973 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -0800974 int i;
Tomas Winkler9f586712008-11-12 13:14:05 -0800975
Johannes Bergbdbb6122010-04-30 13:53:37 -0700976 if (sta) {
977 sta_id = iwl_sta_id(sta);
978
979 if (sta_id == IWL_INVALID_STATION) {
980 IWL_DEBUG_MAC80211(priv, "leave - %pM not initialised.\n",
981 sta->addr);
982 return;
983 }
984 } else
985 sta_id = priv->hw_params.bcast_sta_id;
986
Tomas Winkler9f586712008-11-12 13:14:05 -0800987
988 if (iwl_scan_cancel(priv)) {
989 /* cancel scan failed, just live w/ bad key and rely
990 briefly on SW decryption */
991 return;
992 }
993
Tomas Winkler9f586712008-11-12 13:14:05 -0800994 spin_lock_irqsave(&priv->sta_lock, flags);
995
Tomas Winkler9f586712008-11-12 13:14:05 -0800996 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
997
998 for (i = 0; i < 5; i++)
999 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
1000 cpu_to_le16(phase1key[i]);
1001
1002 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1003 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1004
1005 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1006
1007 spin_unlock_irqrestore(&priv->sta_lock, flags);
1008
1009}
1010EXPORT_SYMBOL(iwl_update_tkip_key);
1011
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001012int iwl_remove_dynamic_key(struct iwl_priv *priv,
1013 struct ieee80211_key_conf *keyconf,
1014 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001015{
1016 unsigned long flags;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001017 int ret = 0;
1018 u16 key_flags;
1019 u8 keyidx;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001020
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001021 priv->key_mapping_key--;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001022
1023 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001024 key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
1025 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
1026
Tomas Winklere1623442009-01-27 14:27:56 -08001027 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +08001028 keyconf->keyidx, sta_id);
1029
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001030 if (keyconf->keyidx != keyidx) {
1031 /* We need to remove a key with index different that the one
1032 * in the uCode. This means that the key we need to remove has
1033 * been replaced by another one with different index.
1034 * Don't do anything and return ok
1035 */
1036 spin_unlock_irqrestore(&priv->sta_lock, flags);
1037 return 0;
1038 }
1039
Tomas Winkler40a9a822008-11-25 23:29:03 +02001040 if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001041 IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
Tomas Winkler40a9a822008-11-25 23:29:03 +02001042 keyconf->keyidx, key_flags);
1043 spin_unlock_irqrestore(&priv->sta_lock, flags);
1044 return 0;
1045 }
1046
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001047 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
1048 &priv->ucode_key_table))
Winkler, Tomas15b16872008-12-19 10:37:33 +08001049 IWL_ERR(priv, "index %d not used in uCode key table.\n",
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001050 priv->stations[sta_id].sta.key.key_offset);
1051 memset(&priv->stations[sta_id].keyinfo, 0,
Tomas Winkler6def9762008-05-05 10:22:31 +08001052 sizeof(struct iwl_hw_key));
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001053 memset(&priv->stations[sta_id].sta.key, 0,
1054 sizeof(struct iwl4965_keyinfo));
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001055 priv->stations[sta_id].sta.key.key_flags =
1056 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
1057 priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001058 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1059 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001060
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -07001061 if (iwl_is_rfkill(priv)) {
Frans Pop91dd6c22010-03-24 14:19:58 -07001062 IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -07001063 spin_unlock_irqrestore(&priv->sta_lock, flags);
1064 return 0;
1065 }
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001066 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001067 spin_unlock_irqrestore(&priv->sta_lock, flags);
1068 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001069}
Tomas Winkler27aaba02008-05-05 10:22:44 +08001070EXPORT_SYMBOL(iwl_remove_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001071
1072int iwl_set_dynamic_key(struct iwl_priv *priv,
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001073 struct ieee80211_key_conf *keyconf, u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001074{
1075 int ret;
1076
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001077 priv->key_mapping_key++;
1078 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001079
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001080 switch (keyconf->alg) {
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001081 case ALG_CCMP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001082 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001083 break;
1084 case ALG_TKIP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001085 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001086 break;
1087 case ALG_WEP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001088 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001089 break;
1090 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001091 IWL_ERR(priv,
1092 "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001093 ret = -EINVAL;
1094 }
1095
Tomas Winklere1623442009-01-27 14:27:56 -08001096 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 +08001097 keyconf->alg, keyconf->keylen, keyconf->keyidx,
1098 sta_id, ret);
1099
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001100 return ret;
1101}
Tomas Winkler27aaba02008-05-05 10:22:44 +08001102EXPORT_SYMBOL(iwl_set_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001103
Tomas Winkler66c73db2008-04-15 16:01:40 -07001104#ifdef CONFIG_IWLWIFI_DEBUG
1105static void iwl_dump_lq_cmd(struct iwl_priv *priv,
1106 struct iwl_link_quality_cmd *lq)
1107{
1108 int i;
Tomas Winklere1623442009-01-27 14:27:56 -08001109 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
1110 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001111 lq->general_params.single_stream_ant_msk,
1112 lq->general_params.dual_stream_ant_msk);
1113
1114 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
Tomas Winklere1623442009-01-27 14:27:56 -08001115 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001116 i, lq->rs_table[i].rate_n_flags);
1117}
1118#else
1119static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
1120 struct iwl_link_quality_cmd *lq)
1121{
1122}
1123#endif
1124
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001125/**
Reinette Chatre3bce6062010-04-13 16:11:19 -07001126 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
1127 *
1128 * It sometimes happens when a HT rate has been in use and we
1129 * loose connectivity with AP then mac80211 will first tell us that the
1130 * current channel is not HT anymore before removing the station. In such a
1131 * scenario the RXON flags will be updated to indicate we are not
1132 * communicating HT anymore, but the LQ command may still contain HT rates.
1133 * Test for this to prevent driver from sending LQ command between the time
1134 * RXON flags are updated and when LQ command is updated.
1135 */
1136static bool is_lq_table_valid(struct iwl_priv *priv,
1137 struct iwl_link_quality_cmd *lq)
1138{
1139 int i;
1140 struct iwl_ht_config *ht_conf = &priv->current_ht_config;
1141
1142 if (ht_conf->is_ht)
1143 return true;
1144
1145 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
1146 priv->active_rxon.channel);
1147 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
1148 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) & RATE_MCS_HT_MSK) {
1149 IWL_DEBUG_INFO(priv,
1150 "index %d of LQ expects HT channel\n",
1151 i);
1152 return false;
1153 }
1154 }
1155 return true;
1156}
1157
1158/**
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001159 * iwl_send_lq_cmd() - Send link quality command
1160 * @init: This command is sent as part of station initialization right
1161 * after station has been added.
1162 *
1163 * The link quality command is sent as the last step of station creation.
1164 * This is the special case in which init is set and we call a callback in
1165 * this case to clear the state indicating that station creation is in
1166 * progress.
1167 */
Tomas Winkler66c73db2008-04-15 16:01:40 -07001168int iwl_send_lq_cmd(struct iwl_priv *priv,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001169 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
Tomas Winkler66c73db2008-04-15 16:01:40 -07001170{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001171 int ret = 0;
1172 unsigned long flags_spin;
1173
Tomas Winkler66c73db2008-04-15 16:01:40 -07001174 struct iwl_host_cmd cmd = {
1175 .id = REPLY_TX_LINK_QUALITY_CMD,
1176 .len = sizeof(struct iwl_link_quality_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07001177 .flags = flags,
Tomas Winkler66c73db2008-04-15 16:01:40 -07001178 .data = lq,
1179 };
1180
Johannes Berg76c3c692010-03-30 02:46:29 -07001181 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
Tomas Winkler66c73db2008-04-15 16:01:40 -07001182 return -EINVAL;
1183
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001184 iwl_dump_lq_cmd(priv, lq);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001185 BUG_ON(init && (cmd.flags & CMD_ASYNC));
Tomas Winkler66c73db2008-04-15 16:01:40 -07001186
Reinette Chatre3bce6062010-04-13 16:11:19 -07001187 if (is_lq_table_valid(priv, lq))
1188 ret = iwl_send_cmd(priv, &cmd);
1189 else
1190 ret = -EINVAL;
Reinette Chatred2e210a2010-04-23 10:33:33 -07001191
1192 if (cmd.flags & CMD_ASYNC)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001193 return ret;
Tomas Winkler66c73db2008-04-15 16:01:40 -07001194
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001195 if (init) {
Frans Pop91dd6c22010-03-24 14:19:58 -07001196 IWL_DEBUG_INFO(priv, "init LQ command complete, clearing sta addition status for sta %d\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001197 lq->sta_id);
1198 spin_lock_irqsave(&priv->sta_lock, flags_spin);
1199 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
1200 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
1201 }
Reinette Chatred2e210a2010-04-23 10:33:33 -07001202 return ret;
Tomas Winkler66c73db2008-04-15 16:01:40 -07001203}
1204EXPORT_SYMBOL(iwl_send_lq_cmd);
1205
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001206/**
Johannes Berg2c810cc2010-04-29 00:53:29 -07001207 * iwl_alloc_bcast_station - add broadcast station into driver's station table.
1208 *
1209 * This adds the broadcast station into the driver's station table
1210 * and marks it driver active, so that it will be restored to the
1211 * device at the next best time.
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001212 */
Johannes Berg2c810cc2010-04-29 00:53:29 -07001213int iwl_alloc_bcast_station(struct iwl_priv *priv, bool init_lq)
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001214{
Johannes Berg2c810cc2010-04-29 00:53:29 -07001215 struct iwl_link_quality_cmd *link_cmd;
1216 unsigned long flags;
1217 u8 sta_id;
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001218
Johannes Berg2c810cc2010-04-29 00:53:29 -07001219 spin_lock_irqsave(&priv->sta_lock, flags);
1220 sta_id = iwl_prep_station(priv, iwl_bcast_addr, false, NULL);
1221 if (sta_id == IWL_INVALID_STATION) {
1222 IWL_ERR(priv, "Unable to prepare broadcast station\n");
1223 spin_unlock_irqrestore(&priv->sta_lock, flags);
1224
1225 return -EINVAL;
1226 }
1227
1228 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1229 priv->stations[sta_id].used |= IWL_STA_BCAST;
1230 spin_unlock_irqrestore(&priv->sta_lock, flags);
1231
1232 if (init_lq) {
1233 link_cmd = iwl_sta_alloc_lq(priv, sta_id);
1234 if (!link_cmd) {
1235 IWL_ERR(priv,
1236 "Unable to initialize rate scaling for bcast station.\n");
1237 return -ENOMEM;
1238 }
1239
1240 spin_lock_irqsave(&priv->sta_lock, flags);
1241 priv->stations[sta_id].lq = link_cmd;
1242 spin_unlock_irqrestore(&priv->sta_lock, flags);
1243 }
1244
1245 return 0;
1246}
1247EXPORT_SYMBOL_GPL(iwl_alloc_bcast_station);
1248
1249void iwl_dealloc_bcast_station(struct iwl_priv *priv)
Reinette Chatre3459ab52010-01-22 14:22:49 -08001250{
Johannes Berg2c810cc2010-04-29 00:53:29 -07001251 unsigned long flags;
1252 int i;
Reinette Chatred2e210a2010-04-23 10:33:33 -07001253
Johannes Berg2c810cc2010-04-29 00:53:29 -07001254 spin_lock_irqsave(&priv->sta_lock, flags);
1255 for (i = 0; i < priv->hw_params.max_stations; i++) {
1256 if (!(priv->stations[i].used & IWL_STA_BCAST))
1257 continue;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001258
Johannes Berg2c810cc2010-04-29 00:53:29 -07001259 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
1260 priv->num_stations--;
1261 BUG_ON(priv->num_stations < 0);
1262 kfree(priv->stations[i].lq);
1263 priv->stations[i].lq = NULL;
1264 }
1265 spin_unlock_irqrestore(&priv->sta_lock, flags);
Reinette Chatre3459ab52010-01-22 14:22:49 -08001266}
Johannes Berg2c810cc2010-04-29 00:53:29 -07001267EXPORT_SYMBOL_GPL(iwl_dealloc_bcast_station);
Reinette Chatre3459ab52010-01-22 14:22:49 -08001268
1269/**
Tomas Winkler9f586712008-11-12 13:14:05 -08001270 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
Tomas Winkler5083e562008-05-29 16:35:15 +08001271 */
Tomas Winkler9f586712008-11-12 13:14:05 -08001272void iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
Tomas Winkler5083e562008-05-29 16:35:15 +08001273{
1274 unsigned long flags;
1275
1276 /* Remove "disable" flag, to enable Tx for this TID */
1277 spin_lock_irqsave(&priv->sta_lock, flags);
1278 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1279 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1280 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1281 spin_unlock_irqrestore(&priv->sta_lock, flags);
1282
1283 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1284}
Tomas Winkler9f586712008-11-12 13:14:05 -08001285EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
1286
Johannes Berg619753f2010-04-30 11:30:46 -07001287int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1288 int tid, u16 ssn)
Tomas Winkler9f586712008-11-12 13:14:05 -08001289{
1290 unsigned long flags;
1291 int sta_id;
1292
Johannes Berg619753f2010-04-30 11:30:46 -07001293 sta_id = iwl_sta_id(sta);
Tomas Winkler9f586712008-11-12 13:14:05 -08001294 if (sta_id == IWL_INVALID_STATION)
1295 return -ENXIO;
1296
1297 spin_lock_irqsave(&priv->sta_lock, flags);
1298 priv->stations[sta_id].sta.station_flags_msk = 0;
1299 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1300 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1301 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1302 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1303 spin_unlock_irqrestore(&priv->sta_lock, flags);
1304
1305 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
Johannes Berg619753f2010-04-30 11:30:46 -07001306 CMD_ASYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001307}
1308EXPORT_SYMBOL(iwl_sta_rx_agg_start);
1309
Johannes Berg619753f2010-04-30 11:30:46 -07001310int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1311 int tid)
Tomas Winkler9f586712008-11-12 13:14:05 -08001312{
1313 unsigned long flags;
1314 int sta_id;
1315
Johannes Berg619753f2010-04-30 11:30:46 -07001316 sta_id = iwl_sta_id(sta);
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001317 if (sta_id == IWL_INVALID_STATION) {
1318 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
Tomas Winkler9f586712008-11-12 13:14:05 -08001319 return -ENXIO;
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001320 }
Tomas Winkler9f586712008-11-12 13:14:05 -08001321
1322 spin_lock_irqsave(&priv->sta_lock, flags);
1323 priv->stations[sta_id].sta.station_flags_msk = 0;
1324 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1325 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1326 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1327 spin_unlock_irqrestore(&priv->sta_lock, flags);
1328
1329 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1330 CMD_ASYNC);
1331}
1332EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
1333
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001334void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
Tomas Winkler9f586712008-11-12 13:14:05 -08001335{
1336 unsigned long flags;
1337
1338 spin_lock_irqsave(&priv->sta_lock, flags);
1339 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1340 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1341 priv->stations[sta_id].sta.sta.modify_mask = 0;
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001342 priv->stations[sta_id].sta.sleep_tx_count = 0;
Tomas Winkler9f586712008-11-12 13:14:05 -08001343 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1344 spin_unlock_irqrestore(&priv->sta_lock, flags);
1345
1346 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1347}
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001348EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
Tomas Winkler9f586712008-11-12 13:14:05 -08001349
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001350void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
Tomas Winkler9f586712008-11-12 13:14:05 -08001351{
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001352 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -08001353
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001354 spin_lock_irqsave(&priv->sta_lock, flags);
1355 priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
1356 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1357 priv->stations[sta_id].sta.sta.modify_mask =
1358 STA_MODIFY_SLEEP_TX_COUNT_MSK;
1359 priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
1360 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1361 spin_unlock_irqrestore(&priv->sta_lock, flags);
Tomas Winkler9f586712008-11-12 13:14:05 -08001362
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001363 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001364}
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -07001365EXPORT_SYMBOL(iwl_sta_modify_sleep_tx_count);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001366
1367int iwl_mac_sta_remove(struct ieee80211_hw *hw,
Johannes Bergfd1af152010-04-30 11:30:43 -07001368 struct ieee80211_vif *vif,
1369 struct ieee80211_sta *sta)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001370{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001371 struct iwl_priv *priv = hw->priv;
Johannes Bergfd1af152010-04-30 11:30:43 -07001372 struct iwl_station_priv_common *sta_common = (void *)sta->drv_priv;
1373 int ret;
1374
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001375 IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
1376 sta->addr);
Johannes Bergfd1af152010-04-30 11:30:43 -07001377 ret = iwl_remove_station(priv, sta_common->sta_id, sta->addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001378 if (ret)
1379 IWL_ERR(priv, "Error removing station %pM\n",
1380 sta->addr);
1381 return ret;
1382}
1383EXPORT_SYMBOL(iwl_mac_sta_remove);