blob: 83a26361a9b56b35d4364c93b9f9df32905c8cc6 [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
Johannes Berg63d41762010-04-30 11:30:50 -0700287 /*
288 * OK to call unconditionally, since local stations (IBSS BSSID
289 * STA and broadcast STA) pass in a NULL ht_info, and mac80211
290 * doesn't allow HT IBSS.
291 */
292 iwl_set_ht_add_station(priv, sta_id, ht_info);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800293
Tomas Winklerc587de02009-06-03 11:44:07 -0700294 /* 3945 only */
295 rate = (priv->band == IEEE80211_BAND_5GHZ) ?
296 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
297 /* Turn on both antennas for the station... */
298 station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
299
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800300 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800301
302}
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800303
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800304#define STA_WAIT_TIMEOUT (HZ/2)
305
306/**
307 * iwl_add_station_common -
308 */
309int iwl_add_station_common(struct iwl_priv *priv, const u8 *addr,
310 bool is_ap,
311 struct ieee80211_sta_ht_cap *ht_info,
312 u8 *sta_id_r)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800313{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800314 struct iwl_station_entry *station;
315 unsigned long flags_spin;
316 int ret = 0;
317 u8 sta_id;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800318
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800319 *sta_id_r = 0;
320 spin_lock_irqsave(&priv->sta_lock, flags_spin);
321 sta_id = iwl_prep_station(priv, addr, is_ap, ht_info);
322 if (sta_id == IWL_INVALID_STATION) {
323 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
324 addr);
325 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
326 return -EINVAL;
327 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800328
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800329 /*
330 * uCode is not able to deal with multiple requests to add a
331 * station. Keep track if one is in progress so that we do not send
332 * another.
333 */
334 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
335 IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
336 sta_id);
337 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
338 return -EEXIST;
339 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800340
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800341 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
342 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
343 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
344 sta_id, addr);
345 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
346 return -EEXIST;
347 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800348
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800349 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
350 station = &priv->stations[sta_id];
351 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
352
353 /* Add station to device's station table */
354 ret = iwl_send_add_sta(priv, &station->sta, CMD_SYNC);
355 if (ret) {
356 IWL_ERR(priv, "Adding station %pM failed.\n", station->sta.sta.addr);
357 spin_lock_irqsave(&priv->sta_lock, flags_spin);
358 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
359 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
360 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
361 }
362 *sta_id_r = sta_id;
363 return ret;
364}
365EXPORT_SYMBOL(iwl_add_station_common);
366
Johannes Berga6a03452010-04-29 10:41:07 -0700367static struct iwl_link_quality_cmd *iwl_sta_alloc_lq(struct iwl_priv *priv,
368 u8 sta_id)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800369{
370 int i, r;
Reinette Chatred2e210a2010-04-23 10:33:33 -0700371 struct iwl_link_quality_cmd *link_cmd;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800372 u32 rate_flags;
373
Reinette Chatred2e210a2010-04-23 10:33:33 -0700374 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
375 if (!link_cmd) {
376 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
377 return NULL;
378 }
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800379 /* Set up the rate scaling to start at selected rate, fall back
380 * all the way down to 1M in IEEE order, and then spin on 1M */
Johannes Berg156b70d2010-04-28 14:44:45 -0700381 if (priv->band == IEEE80211_BAND_5GHZ)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800382 r = IWL_RATE_6M_INDEX;
383 else
384 r = IWL_RATE_1M_INDEX;
385
386 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
387 rate_flags = 0;
388 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
389 rate_flags |= RATE_MCS_CCK_MSK;
390
391 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
392 RATE_MCS_ANT_POS;
393
Reinette Chatred2e210a2010-04-23 10:33:33 -0700394 link_cmd->rs_table[i].rate_n_flags =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800395 iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
396 r = iwl_get_prev_ieee_rate(r);
397 }
398
Reinette Chatred2e210a2010-04-23 10:33:33 -0700399 link_cmd->general_params.single_stream_ant_msk =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800400 first_antenna(priv->hw_params.valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700401
Reinette Chatred2e210a2010-04-23 10:33:33 -0700402 link_cmd->general_params.dual_stream_ant_msk =
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700403 priv->hw_params.valid_tx_ant &
404 ~first_antenna(priv->hw_params.valid_tx_ant);
Reinette Chatred2e210a2010-04-23 10:33:33 -0700405 if (!link_cmd->general_params.dual_stream_ant_msk) {
406 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700407 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
Reinette Chatred2e210a2010-04-23 10:33:33 -0700408 link_cmd->general_params.dual_stream_ant_msk =
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700409 priv->hw_params.valid_tx_ant;
410 }
411
Reinette Chatred2e210a2010-04-23 10:33:33 -0700412 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
413 link_cmd->agg_params.agg_time_limit =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800414 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
415
Johannes Berg156b70d2010-04-28 14:44:45 -0700416 link_cmd->sta_id = sta_id;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800417
Reinette Chatred2e210a2010-04-23 10:33:33 -0700418 return link_cmd;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800419}
420
421/*
Johannes Berg57f8db82010-04-30 11:30:49 -0700422 * iwl_add_bssid_station - Add the special IBSS BSSID station
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800423 *
424 * Function sleeps.
425 */
Johannes Berg57f8db82010-04-30 11:30:49 -0700426int iwl_add_bssid_station(struct iwl_priv *priv, const u8 *addr, bool init_rs,
Johannes Bergfd1af152010-04-30 11:30:43 -0700427 u8 *sta_id_r)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800428{
429 int ret;
430 u8 sta_id;
Reinette Chatred2e210a2010-04-23 10:33:33 -0700431 struct iwl_link_quality_cmd *link_cmd;
432 unsigned long flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800433
Dan Carpenter96900c72010-05-14 16:53:46 +0200434 if (sta_id_r)
Johannes Bergfd1af152010-04-30 11:30:43 -0700435 *sta_id_r = IWL_INVALID_STATION;
436
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800437 ret = iwl_add_station_common(priv, addr, 0, NULL, &sta_id);
438 if (ret) {
439 IWL_ERR(priv, "Unable to add station %pM\n", addr);
440 return ret;
441 }
442
Johannes Bergfd1af152010-04-30 11:30:43 -0700443 if (sta_id_r)
444 *sta_id_r = sta_id;
445
Reinette Chatred2e210a2010-04-23 10:33:33 -0700446 spin_lock_irqsave(&priv->sta_lock, flags);
447 priv->stations[sta_id].used |= IWL_STA_LOCAL;
448 spin_unlock_irqrestore(&priv->sta_lock, flags);
449
450 if (init_rs) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800451 /* Set up default rate scaling table in device's station table */
Johannes Berga6a03452010-04-29 10:41:07 -0700452 link_cmd = iwl_sta_alloc_lq(priv, sta_id);
Reinette Chatred2e210a2010-04-23 10:33:33 -0700453 if (!link_cmd) {
454 IWL_ERR(priv, "Unable to initialize rate scaling for station %pM.\n",
455 addr);
456 return -ENOMEM;
457 }
Johannes Berga6a03452010-04-29 10:41:07 -0700458
459 ret = iwl_send_lq_cmd(priv, link_cmd, CMD_SYNC, true);
460 if (ret)
461 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
462
Reinette Chatred2e210a2010-04-23 10:33:33 -0700463 spin_lock_irqsave(&priv->sta_lock, flags);
464 priv->stations[sta_id].lq = link_cmd;
465 spin_unlock_irqrestore(&priv->sta_lock, flags);
466 }
467
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800468 return 0;
469}
Johannes Berg57f8db82010-04-30 11:30:49 -0700470EXPORT_SYMBOL(iwl_add_bssid_station);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800471
472/**
473 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
474 *
475 * priv->sta_lock must be held
476 */
477static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
478{
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800479 /* Ucode must be active and driver must be non active */
Reinette Chatred2e210a2010-04-23 10:33:33 -0700480 if ((priv->stations[sta_id].used &
481 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) != IWL_STA_UCODE_ACTIVE)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800482 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800483
484 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
485
486 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800487 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800488}
489
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800490static int iwl_send_remove_station(struct iwl_priv *priv,
491 struct iwl_station_entry *station)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800492{
Zhu Yi2f301222009-10-09 17:19:45 +0800493 struct iwl_rx_packet *pkt;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800494 int ret;
495
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800496 unsigned long flags_spin;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800497 struct iwl_rem_sta_cmd rm_sta_cmd;
498
499 struct iwl_host_cmd cmd = {
500 .id = REPLY_REMOVE_STA,
501 .len = sizeof(struct iwl_rem_sta_cmd),
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800502 .flags = CMD_SYNC,
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800503 .data = &rm_sta_cmd,
504 };
505
506 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
507 rm_sta_cmd.num_sta = 1;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800508 memcpy(&rm_sta_cmd.addr, &station->sta.sta.addr , ETH_ALEN);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800509
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800510 cmd.flags |= CMD_WANT_SKB;
511
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800512 ret = iwl_send_cmd(priv, &cmd);
513
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800514 if (ret)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800515 return ret;
516
Zhu Yi2f301222009-10-09 17:19:45 +0800517 pkt = (struct iwl_rx_packet *)cmd.reply_page;
518 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800519 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800520 pkt->hdr.flags);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800521 ret = -EIO;
522 }
523
524 if (!ret) {
Zhu Yi2f301222009-10-09 17:19:45 +0800525 switch (pkt->u.rem_sta.status) {
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800526 case REM_STA_SUCCESS_MSK:
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800527 spin_lock_irqsave(&priv->sta_lock, flags_spin);
528 iwl_sta_ucode_deactivate(priv, station->sta.sta.sta_id);
529 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Tomas Winklere1623442009-01-27 14:27:56 -0800530 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800531 break;
532 default:
533 ret = -EIO;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800534 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800535 break;
536 }
537 }
Zhu Yi64a76b52009-12-10 14:37:21 -0800538 iwl_free_pages(priv, cmd.reply_page);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800539
540 return ret;
541}
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +0800542
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800543/**
544 * iwl_remove_station - Remove driver's knowledge of station.
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800545 */
Johannes Bergfd1af152010-04-30 11:30:43 -0700546int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
547 const u8 *addr)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800548{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800549 struct iwl_station_entry *station;
Johannes Bergfd1af152010-04-30 11:30:43 -0700550 unsigned long flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800551
552 if (!iwl_is_ready(priv)) {
553 IWL_DEBUG_INFO(priv,
Frans Pop91dd6c22010-03-24 14:19:58 -0700554 "Unable to remove station %pM, device not ready.\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700555 addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800556 /*
557 * It is typical for stations to be removed when we are
558 * going down. Return success since device will be down
559 * soon anyway
560 */
561 return 0;
562 }
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800563
Tomas Winklere1623442009-01-27 14:27:56 -0800564 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
Johannes Bergfd1af152010-04-30 11:30:43 -0700565 sta_id, addr);
566
567 if (WARN_ON(sta_id == IWL_INVALID_STATION))
568 return -EINVAL;
569
570 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800571
572 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800573 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700574 addr);
Johannes Bergfd1af152010-04-30 11:30:43 -0700575 goto out_err;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800576 }
577
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800578 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800579 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700580 addr);
Johannes Bergfd1af152010-04-30 11:30:43 -0700581 goto out_err;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800582 }
583
Johannes Berg1fa61b22010-04-28 08:44:52 -0700584 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
585 kfree(priv->stations[sta_id].lq);
586 priv->stations[sta_id].lq = NULL;
587 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800588
589 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
590
591 priv->num_stations--;
592
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800593 BUG_ON(priv->num_stations < 0);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800594
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800595 station = &priv->stations[sta_id];
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800596 spin_unlock_irqrestore(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800597
Johannes Bergfd1af152010-04-30 11:30:43 -0700598 return iwl_send_remove_station(priv, station);
599out_err:
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800600 spin_unlock_irqrestore(&priv->sta_lock, flags);
Johannes Bergfd1af152010-04-30 11:30:43 -0700601 return -EINVAL;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800602}
Johannes Berg1fa61b22010-04-28 08:44:52 -0700603EXPORT_SYMBOL_GPL(iwl_remove_station);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800604
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800605/**
Johannes Berg2c810cc2010-04-29 00:53:29 -0700606 * iwl_clear_ucode_stations - clear ucode station table bits
607 *
608 * This function clears all the bits in the driver indicating
609 * which stations are active in the ucode. Call when something
610 * other than explicit station management would cause this in
611 * the ucode, e.g. unassociated RXON.
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800612 */
Johannes Berg2c810cc2010-04-29 00:53:29 -0700613void iwl_clear_ucode_stations(struct iwl_priv *priv)
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800614{
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700615 int i;
Reinette Chatre7e246192010-02-18 22:58:32 -0800616 unsigned long flags_spin;
617 bool cleared = false;
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800618
Johannes Berg2c810cc2010-04-29 00:53:29 -0700619 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700620
Reinette Chatre7e246192010-02-18 22:58:32 -0800621 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Johannes Berg2c810cc2010-04-29 00:53:29 -0700622 for (i = 0; i < priv->hw_params.max_stations; i++) {
623 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
624 IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d\n", i);
625 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
626 cleared = true;
Reinette Chatre7e246192010-02-18 22:58:32 -0800627 }
628 }
629 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
630
631 if (!cleared)
632 IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800633}
Reinette Chatre7e246192010-02-18 22:58:32 -0800634EXPORT_SYMBOL(iwl_clear_ucode_stations);
635
636/**
637 * iwl_restore_stations() - Restore driver known stations to device
638 *
639 * All stations considered active by driver, but not present in ucode, is
640 * restored.
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800641 *
642 * Function sleeps.
Reinette Chatre7e246192010-02-18 22:58:32 -0800643 */
644void iwl_restore_stations(struct iwl_priv *priv)
645{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800646 struct iwl_station_entry *station;
Reinette Chatre7e246192010-02-18 22:58:32 -0800647 unsigned long flags_spin;
648 int i;
649 bool found = false;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800650 int ret;
Reinette Chatre7e246192010-02-18 22:58:32 -0800651
652 if (!iwl_is_ready(priv)) {
653 IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
654 return;
655 }
656
657 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
658 spin_lock_irqsave(&priv->sta_lock, flags_spin);
659 for (i = 0; i < priv->hw_params.max_stations; i++) {
660 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
661 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
662 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
663 priv->stations[i].sta.sta.addr);
664 priv->stations[i].sta.mode = 0;
665 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
666 found = true;
667 }
668 }
669
670 for (i = 0; i < priv->hw_params.max_stations; i++) {
671 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800672 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
673 station = &priv->stations[i];
674 ret = iwl_send_add_sta(priv, &priv->stations[i].sta, CMD_SYNC);
675 if (ret) {
676 IWL_ERR(priv, "Adding station %pM failed.\n",
677 station->sta.sta.addr);
678 spin_lock_irqsave(&priv->sta_lock, flags_spin);
679 priv->stations[i].used &= ~IWL_STA_DRIVER_ACTIVE;
680 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
681 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
682 }
683 /*
684 * Rate scaling has already been initialized, send
685 * current LQ command
686 */
687 if (station->lq)
688 iwl_send_lq_cmd(priv, station->lq, CMD_SYNC, true);
689 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Reinette Chatre7e246192010-02-18 22:58:32 -0800690 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
691 }
692 }
693
694 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
695 if (!found)
696 IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
697 else
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800698 IWL_DEBUG_INFO(priv, "Restoring all known stations .... complete.\n");
Reinette Chatre7e246192010-02-18 22:58:32 -0800699}
700EXPORT_SYMBOL(iwl_restore_stations);
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800701
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800702int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700703{
704 int i;
705
706 for (i = 0; i < STA_KEY_MAX_NUM; i++)
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700707 if (!test_and_set_bit(i, &priv->ucode_key_table))
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700708 return i;
709
Tomas Winkler40a9a822008-11-25 23:29:03 +0200710 return WEP_INVALID_OFFSET;
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700711}
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800712EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700713
Johannes Berg335348b2010-03-30 10:11:46 -0700714static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700715{
716 int i, not_empty = 0;
717 u8 buff[sizeof(struct iwl_wep_cmd) +
718 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
719 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
720 size_t cmd_size = sizeof(struct iwl_wep_cmd);
721 struct iwl_host_cmd cmd = {
722 .id = REPLY_WEPKEY,
723 .data = wep_cmd,
Johannes Berg72e15d72010-02-19 11:42:32 -0800724 .flags = CMD_SYNC,
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700725 };
726
Johannes Berg72e15d72010-02-19 11:42:32 -0800727 might_sleep();
728
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700729 memset(wep_cmd, 0, cmd_size +
730 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
731
732 for (i = 0; i < WEP_KEYS_MAX ; i++) {
733 wep_cmd->key[i].key_index = i;
734 if (priv->wep_keys[i].key_size) {
735 wep_cmd->key[i].key_offset = i;
736 not_empty = 1;
737 } else {
738 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
739 }
740
741 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
742 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
743 priv->wep_keys[i].key_size);
744 }
745
746 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
747 wep_cmd->num_keys = WEP_KEYS_MAX;
748
749 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
750
751 cmd.len = cmd_size;
752
753 if (not_empty || send_if_empty)
754 return iwl_send_cmd(priv, &cmd);
755 else
756 return 0;
757}
Johannes Berg335348b2010-03-30 10:11:46 -0700758
759int iwl_restore_default_wep_keys(struct iwl_priv *priv)
760{
761 WARN_ON(!mutex_is_locked(&priv->mutex));
762
763 return iwl_send_static_wepkey_cmd(priv, 0);
764}
765EXPORT_SYMBOL(iwl_restore_default_wep_keys);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700766
767int iwl_remove_default_wep_key(struct iwl_priv *priv,
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700768 struct ieee80211_key_conf *keyconf)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700769{
770 int ret;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700771
Johannes Berg72e15d72010-02-19 11:42:32 -0800772 WARN_ON(!mutex_is_locked(&priv->mutex));
773
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700774 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
775 keyconf->keyidx);
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700776
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700777 memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700778 if (iwl_is_rfkill(priv)) {
779 IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berg72e15d72010-02-19 11:42:32 -0800780 /* but keys in device are clear anyway so return success */
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700781 return 0;
782 }
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700783 ret = iwl_send_static_wepkey_cmd(priv, 1);
Tomas Winklere1623442009-01-27 14:27:56 -0800784 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800785 keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700786
787 return ret;
788}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800789EXPORT_SYMBOL(iwl_remove_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700790
791int iwl_set_default_wep_key(struct iwl_priv *priv,
792 struct ieee80211_key_conf *keyconf)
793{
794 int ret;
Johannes Berg72e15d72010-02-19 11:42:32 -0800795
796 WARN_ON(!mutex_is_locked(&priv->mutex));
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700797
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800798 if (keyconf->keylen != WEP_KEY_LEN_128 &&
799 keyconf->keylen != WEP_KEY_LEN_64) {
Tomas Winklere1623442009-01-27 14:27:56 -0800800 IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800801 return -EINVAL;
802 }
803
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700804 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800805 keyconf->hw_key_idx = HW_KEY_DEFAULT;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700806 priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
807
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700808 priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
809 memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
810 keyconf->keylen);
811
812 ret = iwl_send_static_wepkey_cmd(priv, 0);
Tomas Winklere1623442009-01-27 14:27:56 -0800813 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800814 keyconf->keylen, keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700815
816 return ret;
817}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800818EXPORT_SYMBOL(iwl_set_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700819
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700820static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700821 struct ieee80211_key_conf *keyconf,
822 u8 sta_id)
823{
824 unsigned long flags;
825 __le16 key_flags = 0;
826 int ret;
827
828 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700829
830 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
831 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
832 key_flags &= ~STA_KEY_FLG_INVALID;
833
834 if (keyconf->keylen == WEP_KEY_LEN_128)
835 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
836
Tomas Winkler5425e492008-04-15 16:01:38 -0700837 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700838 key_flags |= STA_KEY_MULTICAST_MSK;
839
840 spin_lock_irqsave(&priv->sta_lock, flags);
841
842 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
843 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
844 priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
845
846 memcpy(priv->stations[sta_id].keyinfo.key,
847 keyconf->key, keyconf->keylen);
848
849 memcpy(&priv->stations[sta_id].sta.key.key[3],
850 keyconf->key, keyconf->keylen);
851
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700852 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
853 == STA_KEY_FLG_NO_ENC)
854 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700855 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700856 /* else, we are overriding an existing key => no need to allocated room
857 * in uCode. */
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700858
Tomas Winkler40a9a822008-11-25 23:29:03 +0200859 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100860 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200861
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700862 priv->stations[sta_id].sta.key.key_flags = key_flags;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700863 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
864 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
865
Tomas Winkler133636d2008-05-05 10:22:34 +0800866 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700867
868 spin_unlock_irqrestore(&priv->sta_lock, flags);
869
870 return ret;
871}
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700872
873static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
874 struct ieee80211_key_conf *keyconf,
875 u8 sta_id)
876{
877 unsigned long flags;
878 __le16 key_flags = 0;
Tomas Winkler40a9a822008-11-25 23:29:03 +0200879 int ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700880
881 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
882 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
883 key_flags &= ~STA_KEY_FLG_INVALID;
884
Tomas Winkler5425e492008-04-15 16:01:38 -0700885 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700886 key_flags |= STA_KEY_MULTICAST_MSK;
887
888 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700889
890 spin_lock_irqsave(&priv->sta_lock, flags);
891 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
892 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
893
894 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
895 keyconf->keylen);
896
897 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
898 keyconf->keylen);
899
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700900 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
901 == STA_KEY_FLG_NO_ENC)
902 priv->stations[sta_id].sta.key.key_offset =
903 iwl_get_free_ucode_key_index(priv);
904 /* else, we are overriding an existing key => no need to allocated room
905 * in uCode. */
906
Tomas Winkler40a9a822008-11-25 23:29:03 +0200907 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100908 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200909
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700910 priv->stations[sta_id].sta.key.key_flags = key_flags;
911 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
912 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
913
Tomas Winkler40a9a822008-11-25 23:29:03 +0200914 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
915
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700916 spin_unlock_irqrestore(&priv->sta_lock, flags);
917
Tomas Winkler40a9a822008-11-25 23:29:03 +0200918 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700919}
920
921static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
922 struct ieee80211_key_conf *keyconf,
923 u8 sta_id)
924{
925 unsigned long flags;
926 int ret = 0;
Reinette Chatre299f5462009-04-30 13:56:31 -0700927 __le16 key_flags = 0;
928
929 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
930 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
931 key_flags &= ~STA_KEY_FLG_INVALID;
932
933 if (sta_id == priv->hw_params.bcast_sta_id)
934 key_flags |= STA_KEY_MULTICAST_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700935
936 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
937 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700938
939 spin_lock_irqsave(&priv->sta_lock, flags);
940
941 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700942 priv->stations[sta_id].keyinfo.keylen = 16;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700943
944 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
945 == STA_KEY_FLG_NO_ENC)
946 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700947 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700948 /* else, we are overriding an existing key => no need to allocated room
949 * in uCode. */
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700950
Tomas Winkler40a9a822008-11-25 23:29:03 +0200951 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100952 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200953
Reinette Chatre299f5462009-04-30 13:56:31 -0700954 priv->stations[sta_id].sta.key.key_flags = key_flags;
955
956
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700957 /* This copy is acutally not needed: we get the key with each TX */
958 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
959
960 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
961
962 spin_unlock_irqrestore(&priv->sta_lock, flags);
963
964 return ret;
965}
966
Tomas Winkler9f586712008-11-12 13:14:05 -0800967void iwl_update_tkip_key(struct iwl_priv *priv,
968 struct ieee80211_key_conf *keyconf,
Johannes Bergbdbb6122010-04-30 13:53:37 -0700969 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
Tomas Winkler9f586712008-11-12 13:14:05 -0800970{
Johannes Bergbdbb6122010-04-30 13:53:37 -0700971 u8 sta_id;
Tomas Winkler9f586712008-11-12 13:14:05 -0800972 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -0800973 int i;
Tomas Winkler9f586712008-11-12 13:14:05 -0800974
Johannes Bergbdbb6122010-04-30 13:53:37 -0700975 if (sta) {
976 sta_id = iwl_sta_id(sta);
977
978 if (sta_id == IWL_INVALID_STATION) {
979 IWL_DEBUG_MAC80211(priv, "leave - %pM not initialised.\n",
980 sta->addr);
981 return;
982 }
983 } else
984 sta_id = priv->hw_params.bcast_sta_id;
985
Tomas Winkler9f586712008-11-12 13:14:05 -0800986
987 if (iwl_scan_cancel(priv)) {
988 /* cancel scan failed, just live w/ bad key and rely
989 briefly on SW decryption */
990 return;
991 }
992
Tomas Winkler9f586712008-11-12 13:14:05 -0800993 spin_lock_irqsave(&priv->sta_lock, flags);
994
Tomas Winkler9f586712008-11-12 13:14:05 -0800995 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
996
997 for (i = 0; i < 5; i++)
998 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
999 cpu_to_le16(phase1key[i]);
1000
1001 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1002 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1003
1004 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1005
1006 spin_unlock_irqrestore(&priv->sta_lock, flags);
1007
1008}
1009EXPORT_SYMBOL(iwl_update_tkip_key);
1010
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001011int iwl_remove_dynamic_key(struct iwl_priv *priv,
1012 struct ieee80211_key_conf *keyconf,
1013 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001014{
1015 unsigned long flags;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001016 int ret = 0;
1017 u16 key_flags;
1018 u8 keyidx;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001019
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001020 priv->key_mapping_key--;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001021
1022 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001023 key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
1024 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
1025
Tomas Winklere1623442009-01-27 14:27:56 -08001026 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +08001027 keyconf->keyidx, sta_id);
1028
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001029 if (keyconf->keyidx != keyidx) {
1030 /* We need to remove a key with index different that the one
1031 * in the uCode. This means that the key we need to remove has
1032 * been replaced by another one with different index.
1033 * Don't do anything and return ok
1034 */
1035 spin_unlock_irqrestore(&priv->sta_lock, flags);
1036 return 0;
1037 }
1038
Tomas Winkler40a9a822008-11-25 23:29:03 +02001039 if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001040 IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
Tomas Winkler40a9a822008-11-25 23:29:03 +02001041 keyconf->keyidx, key_flags);
1042 spin_unlock_irqrestore(&priv->sta_lock, flags);
1043 return 0;
1044 }
1045
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001046 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
1047 &priv->ucode_key_table))
Winkler, Tomas15b16872008-12-19 10:37:33 +08001048 IWL_ERR(priv, "index %d not used in uCode key table.\n",
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001049 priv->stations[sta_id].sta.key.key_offset);
1050 memset(&priv->stations[sta_id].keyinfo, 0,
Tomas Winkler6def9762008-05-05 10:22:31 +08001051 sizeof(struct iwl_hw_key));
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001052 memset(&priv->stations[sta_id].sta.key, 0,
1053 sizeof(struct iwl4965_keyinfo));
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001054 priv->stations[sta_id].sta.key.key_flags =
1055 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
1056 priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001057 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1058 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001059
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -07001060 if (iwl_is_rfkill(priv)) {
Frans Pop91dd6c22010-03-24 14:19:58 -07001061 IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -07001062 spin_unlock_irqrestore(&priv->sta_lock, flags);
1063 return 0;
1064 }
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001065 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001066 spin_unlock_irqrestore(&priv->sta_lock, flags);
1067 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001068}
Tomas Winkler27aaba02008-05-05 10:22:44 +08001069EXPORT_SYMBOL(iwl_remove_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001070
1071int iwl_set_dynamic_key(struct iwl_priv *priv,
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001072 struct ieee80211_key_conf *keyconf, u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001073{
1074 int ret;
1075
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001076 priv->key_mapping_key++;
1077 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001078
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001079 switch (keyconf->alg) {
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001080 case ALG_CCMP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001081 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001082 break;
1083 case ALG_TKIP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001084 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001085 break;
1086 case ALG_WEP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001087 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001088 break;
1089 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001090 IWL_ERR(priv,
1091 "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001092 ret = -EINVAL;
1093 }
1094
Tomas Winklere1623442009-01-27 14:27:56 -08001095 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 +08001096 keyconf->alg, keyconf->keylen, keyconf->keyidx,
1097 sta_id, ret);
1098
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001099 return ret;
1100}
Tomas Winkler27aaba02008-05-05 10:22:44 +08001101EXPORT_SYMBOL(iwl_set_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001102
Tomas Winkler66c73db2008-04-15 16:01:40 -07001103#ifdef CONFIG_IWLWIFI_DEBUG
1104static void iwl_dump_lq_cmd(struct iwl_priv *priv,
1105 struct iwl_link_quality_cmd *lq)
1106{
1107 int i;
Tomas Winklere1623442009-01-27 14:27:56 -08001108 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
1109 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001110 lq->general_params.single_stream_ant_msk,
1111 lq->general_params.dual_stream_ant_msk);
1112
1113 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
Tomas Winklere1623442009-01-27 14:27:56 -08001114 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001115 i, lq->rs_table[i].rate_n_flags);
1116}
1117#else
1118static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
1119 struct iwl_link_quality_cmd *lq)
1120{
1121}
1122#endif
1123
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001124/**
Reinette Chatre3bce6062010-04-13 16:11:19 -07001125 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
1126 *
1127 * It sometimes happens when a HT rate has been in use and we
1128 * loose connectivity with AP then mac80211 will first tell us that the
1129 * current channel is not HT anymore before removing the station. In such a
1130 * scenario the RXON flags will be updated to indicate we are not
1131 * communicating HT anymore, but the LQ command may still contain HT rates.
1132 * Test for this to prevent driver from sending LQ command between the time
1133 * RXON flags are updated and when LQ command is updated.
1134 */
1135static bool is_lq_table_valid(struct iwl_priv *priv,
1136 struct iwl_link_quality_cmd *lq)
1137{
1138 int i;
1139 struct iwl_ht_config *ht_conf = &priv->current_ht_config;
1140
1141 if (ht_conf->is_ht)
1142 return true;
1143
1144 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
1145 priv->active_rxon.channel);
1146 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
1147 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) & RATE_MCS_HT_MSK) {
1148 IWL_DEBUG_INFO(priv,
1149 "index %d of LQ expects HT channel\n",
1150 i);
1151 return false;
1152 }
1153 }
1154 return true;
1155}
1156
1157/**
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001158 * iwl_send_lq_cmd() - Send link quality command
1159 * @init: This command is sent as part of station initialization right
1160 * after station has been added.
1161 *
1162 * The link quality command is sent as the last step of station creation.
1163 * This is the special case in which init is set and we call a callback in
1164 * this case to clear the state indicating that station creation is in
1165 * progress.
1166 */
Tomas Winkler66c73db2008-04-15 16:01:40 -07001167int iwl_send_lq_cmd(struct iwl_priv *priv,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001168 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
Tomas Winkler66c73db2008-04-15 16:01:40 -07001169{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001170 int ret = 0;
1171 unsigned long flags_spin;
1172
Tomas Winkler66c73db2008-04-15 16:01:40 -07001173 struct iwl_host_cmd cmd = {
1174 .id = REPLY_TX_LINK_QUALITY_CMD,
1175 .len = sizeof(struct iwl_link_quality_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07001176 .flags = flags,
Tomas Winkler66c73db2008-04-15 16:01:40 -07001177 .data = lq,
1178 };
1179
Johannes Berg76c3c692010-03-30 02:46:29 -07001180 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
Tomas Winkler66c73db2008-04-15 16:01:40 -07001181 return -EINVAL;
1182
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001183 iwl_dump_lq_cmd(priv, lq);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001184 BUG_ON(init && (cmd.flags & CMD_ASYNC));
Tomas Winkler66c73db2008-04-15 16:01:40 -07001185
Reinette Chatre3bce6062010-04-13 16:11:19 -07001186 if (is_lq_table_valid(priv, lq))
1187 ret = iwl_send_cmd(priv, &cmd);
1188 else
1189 ret = -EINVAL;
Reinette Chatred2e210a2010-04-23 10:33:33 -07001190
1191 if (cmd.flags & CMD_ASYNC)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001192 return ret;
Tomas Winkler66c73db2008-04-15 16:01:40 -07001193
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001194 if (init) {
Frans Pop91dd6c22010-03-24 14:19:58 -07001195 IWL_DEBUG_INFO(priv, "init LQ command complete, clearing sta addition status for sta %d\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001196 lq->sta_id);
1197 spin_lock_irqsave(&priv->sta_lock, flags_spin);
1198 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
1199 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
1200 }
Reinette Chatred2e210a2010-04-23 10:33:33 -07001201 return ret;
Tomas Winkler66c73db2008-04-15 16:01:40 -07001202}
1203EXPORT_SYMBOL(iwl_send_lq_cmd);
1204
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001205/**
Johannes Berg2c810cc2010-04-29 00:53:29 -07001206 * iwl_alloc_bcast_station - add broadcast station into driver's station table.
1207 *
1208 * This adds the broadcast station into the driver's station table
1209 * and marks it driver active, so that it will be restored to the
1210 * device at the next best time.
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001211 */
Johannes Berg2c810cc2010-04-29 00:53:29 -07001212int iwl_alloc_bcast_station(struct iwl_priv *priv, bool init_lq)
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001213{
Johannes Berg2c810cc2010-04-29 00:53:29 -07001214 struct iwl_link_quality_cmd *link_cmd;
1215 unsigned long flags;
1216 u8 sta_id;
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001217
Johannes Berg2c810cc2010-04-29 00:53:29 -07001218 spin_lock_irqsave(&priv->sta_lock, flags);
1219 sta_id = iwl_prep_station(priv, iwl_bcast_addr, false, NULL);
1220 if (sta_id == IWL_INVALID_STATION) {
1221 IWL_ERR(priv, "Unable to prepare broadcast station\n");
1222 spin_unlock_irqrestore(&priv->sta_lock, flags);
1223
1224 return -EINVAL;
1225 }
1226
1227 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1228 priv->stations[sta_id].used |= IWL_STA_BCAST;
1229 spin_unlock_irqrestore(&priv->sta_lock, flags);
1230
1231 if (init_lq) {
1232 link_cmd = iwl_sta_alloc_lq(priv, sta_id);
1233 if (!link_cmd) {
1234 IWL_ERR(priv,
1235 "Unable to initialize rate scaling for bcast station.\n");
1236 return -ENOMEM;
1237 }
1238
1239 spin_lock_irqsave(&priv->sta_lock, flags);
1240 priv->stations[sta_id].lq = link_cmd;
1241 spin_unlock_irqrestore(&priv->sta_lock, flags);
1242 }
1243
1244 return 0;
1245}
1246EXPORT_SYMBOL_GPL(iwl_alloc_bcast_station);
1247
1248void iwl_dealloc_bcast_station(struct iwl_priv *priv)
Reinette Chatre3459ab52010-01-22 14:22:49 -08001249{
Johannes Berg2c810cc2010-04-29 00:53:29 -07001250 unsigned long flags;
1251 int i;
Reinette Chatred2e210a2010-04-23 10:33:33 -07001252
Johannes Berg2c810cc2010-04-29 00:53:29 -07001253 spin_lock_irqsave(&priv->sta_lock, flags);
1254 for (i = 0; i < priv->hw_params.max_stations; i++) {
1255 if (!(priv->stations[i].used & IWL_STA_BCAST))
1256 continue;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001257
Johannes Berg2c810cc2010-04-29 00:53:29 -07001258 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
1259 priv->num_stations--;
1260 BUG_ON(priv->num_stations < 0);
1261 kfree(priv->stations[i].lq);
1262 priv->stations[i].lq = NULL;
1263 }
1264 spin_unlock_irqrestore(&priv->sta_lock, flags);
Reinette Chatre3459ab52010-01-22 14:22:49 -08001265}
Johannes Berg2c810cc2010-04-29 00:53:29 -07001266EXPORT_SYMBOL_GPL(iwl_dealloc_bcast_station);
Reinette Chatre3459ab52010-01-22 14:22:49 -08001267
1268/**
Tomas Winkler9f586712008-11-12 13:14:05 -08001269 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
Tomas Winkler5083e562008-05-29 16:35:15 +08001270 */
Tomas Winkler9f586712008-11-12 13:14:05 -08001271void iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
Tomas Winkler5083e562008-05-29 16:35:15 +08001272{
1273 unsigned long flags;
1274
1275 /* Remove "disable" flag, to enable Tx for this TID */
1276 spin_lock_irqsave(&priv->sta_lock, flags);
1277 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1278 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1279 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1280 spin_unlock_irqrestore(&priv->sta_lock, flags);
1281
1282 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1283}
Tomas Winkler9f586712008-11-12 13:14:05 -08001284EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
1285
Johannes Berg619753f2010-04-30 11:30:46 -07001286int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1287 int tid, u16 ssn)
Tomas Winkler9f586712008-11-12 13:14:05 -08001288{
1289 unsigned long flags;
1290 int sta_id;
1291
Johannes Berg619753f2010-04-30 11:30:46 -07001292 sta_id = iwl_sta_id(sta);
Tomas Winkler9f586712008-11-12 13:14:05 -08001293 if (sta_id == IWL_INVALID_STATION)
1294 return -ENXIO;
1295
1296 spin_lock_irqsave(&priv->sta_lock, flags);
1297 priv->stations[sta_id].sta.station_flags_msk = 0;
1298 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1299 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1300 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1301 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1302 spin_unlock_irqrestore(&priv->sta_lock, flags);
1303
1304 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
Johannes Berg619753f2010-04-30 11:30:46 -07001305 CMD_ASYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001306}
1307EXPORT_SYMBOL(iwl_sta_rx_agg_start);
1308
Johannes Berg619753f2010-04-30 11:30:46 -07001309int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1310 int tid)
Tomas Winkler9f586712008-11-12 13:14:05 -08001311{
1312 unsigned long flags;
1313 int sta_id;
1314
Johannes Berg619753f2010-04-30 11:30:46 -07001315 sta_id = iwl_sta_id(sta);
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001316 if (sta_id == IWL_INVALID_STATION) {
1317 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
Tomas Winkler9f586712008-11-12 13:14:05 -08001318 return -ENXIO;
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001319 }
Tomas Winkler9f586712008-11-12 13:14:05 -08001320
1321 spin_lock_irqsave(&priv->sta_lock, flags);
1322 priv->stations[sta_id].sta.station_flags_msk = 0;
1323 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1324 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1325 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1326 spin_unlock_irqrestore(&priv->sta_lock, flags);
1327
1328 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1329 CMD_ASYNC);
1330}
1331EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
1332
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001333void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
Tomas Winkler9f586712008-11-12 13:14:05 -08001334{
1335 unsigned long flags;
1336
1337 spin_lock_irqsave(&priv->sta_lock, flags);
1338 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1339 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1340 priv->stations[sta_id].sta.sta.modify_mask = 0;
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001341 priv->stations[sta_id].sta.sleep_tx_count = 0;
Tomas Winkler9f586712008-11-12 13:14:05 -08001342 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1343 spin_unlock_irqrestore(&priv->sta_lock, flags);
1344
1345 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1346}
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001347EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
Tomas Winkler9f586712008-11-12 13:14:05 -08001348
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001349void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
Tomas Winkler9f586712008-11-12 13:14:05 -08001350{
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001351 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -08001352
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001353 spin_lock_irqsave(&priv->sta_lock, flags);
1354 priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
1355 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1356 priv->stations[sta_id].sta.sta.modify_mask =
1357 STA_MODIFY_SLEEP_TX_COUNT_MSK;
1358 priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
1359 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1360 spin_unlock_irqrestore(&priv->sta_lock, flags);
Tomas Winkler9f586712008-11-12 13:14:05 -08001361
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001362 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001363}
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -07001364EXPORT_SYMBOL(iwl_sta_modify_sleep_tx_count);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001365
1366int iwl_mac_sta_remove(struct ieee80211_hw *hw,
Johannes Bergfd1af152010-04-30 11:30:43 -07001367 struct ieee80211_vif *vif,
1368 struct ieee80211_sta *sta)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001369{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001370 struct iwl_priv *priv = hw->priv;
Johannes Bergfd1af152010-04-30 11:30:43 -07001371 struct iwl_station_priv_common *sta_common = (void *)sta->drv_priv;
1372 int ret;
1373
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001374 IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
1375 sta->addr);
Johannes Bergfd1af152010-04-30 11:30:43 -07001376 ret = iwl_remove_station(priv, sta_common->sta_id, sta->addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001377 if (ret)
1378 IWL_ERR(priv, "Error removing station %pM\n",
1379 sta->addr);
1380 return ret;
1381}
1382EXPORT_SYMBOL(iwl_mac_sta_remove);