blob: 09d9c4d564508fc4143cd75ad9a305e429eb044f [file] [log] [blame]
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001/******************************************************************************
2 *
Reinette Chatre1f447802010-01-15 13:43:41 -08003 * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070026 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
30#include <net/mac80211.h>
Tomas Winkler947b13a2008-04-16 16:34:48 -070031#include <linux/etherdevice.h>
Reinette Chatrefe6b23d2010-02-22 16:24:47 -080032#include <linux/sched.h>
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070033
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070034#include "iwl-dev.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070035#include "iwl-core.h"
36#include "iwl-sta.h"
Tomas Winkler7a999bf2008-05-29 16:35:02 +080037
Tomas Winkler947b13a2008-04-16 16:34:48 -070038u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
39{
40 int i;
41 int start = 0;
42 int ret = IWL_INVALID_STATION;
43 unsigned long flags;
Tomas Winkler947b13a2008-04-16 16:34:48 -070044
Johannes Berg05c914f2008-09-11 00:01:58 +020045 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) ||
46 (priv->iw_mode == NL80211_IFTYPE_AP))
Tomas Winkler947b13a2008-04-16 16:34:48 -070047 start = IWL_STA_ID;
48
49 if (is_broadcast_ether_addr(addr))
50 return priv->hw_params.bcast_sta_id;
51
52 spin_lock_irqsave(&priv->sta_lock, flags);
53 for (i = start; i < priv->hw_params.max_stations; i++)
54 if (priv->stations[i].used &&
55 (!compare_ether_addr(priv->stations[i].sta.sta.addr,
56 addr))) {
57 ret = i;
58 goto out;
59 }
60
Tomas Winklere1623442009-01-27 14:27:56 -080061 IWL_DEBUG_ASSOC_LIMIT(priv, "can not find STA %pM total %d\n",
Johannes Berge1749612008-10-27 15:59:26 -070062 addr, priv->num_stations);
Tomas Winkler947b13a2008-04-16 16:34:48 -070063
64 out:
Reinette Chatrefe6b23d2010-02-22 16:24:47 -080065 /*
66 * It may be possible that more commands interacting with stations
67 * arrive before we completed processing the adding of
68 * station
69 */
70 if (ret != IWL_INVALID_STATION &&
71 (!(priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) ||
72 ((priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) &&
73 (priv->stations[ret].used & IWL_STA_UCODE_INPROGRESS)))) {
Frans Pop91dd6c22010-03-24 14:19:58 -070074 IWL_ERR(priv, "Requested station info for sta %d before ready.\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -080075 ret);
76 ret = IWL_INVALID_STATION;
77 }
Tomas Winkler947b13a2008-04-16 16:34:48 -070078 spin_unlock_irqrestore(&priv->sta_lock, flags);
79 return ret;
80}
81EXPORT_SYMBOL(iwl_find_station);
82
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080083int iwl_get_ra_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
84{
Johannes Berg05c914f2008-09-11 00:01:58 +020085 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080086 return IWL_AP_ID;
87 } else {
88 u8 *da = ieee80211_get_DA(hdr);
Tomas Winklerc587de02009-06-03 11:44:07 -070089 return iwl_find_station(priv, da);
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +080090 }
91}
92EXPORT_SYMBOL(iwl_get_ra_sta_id);
93
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080094/* priv->sta_lock must be held */
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080095static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
96{
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080097
98 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
99 IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u addr %pM\n",
100 sta_id, priv->stations[sta_id].sta.sta.addr);
101
102 if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
103 IWL_DEBUG_ASSOC(priv,
104 "STA id %u addr %pM already present in uCode (according to driver)\n",
105 sta_id, priv->stations[sta_id].sta.sta.addr);
106 } else {
107 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
108 IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
109 sta_id, priv->stations[sta_id].sta.sta.addr);
110 }
111}
112
113static void iwl_process_add_sta_resp(struct iwl_priv *priv,
114 struct iwl_addsta_cmd *addsta,
115 struct iwl_rx_packet *pkt,
116 bool sync)
117{
118 u8 sta_id = addsta->sta.sta_id;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800119 unsigned long flags;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800120
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800121 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
122 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
123 pkt->hdr.flags);
124 return;
125 }
126
127 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
128 sta_id);
129
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800130 spin_lock_irqsave(&priv->sta_lock, flags);
131
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800132 switch (pkt->u.add_sta.status) {
133 case ADD_STA_SUCCESS_MSK:
134 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
135 iwl_sta_ucode_activate(priv, sta_id);
136 break;
137 case ADD_STA_NO_ROOM_IN_TABLE:
138 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
Winkler, Tomas15b16872008-12-19 10:37:33 +0800139 sta_id);
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800140 break;
141 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
142 IWL_ERR(priv, "Adding station %d failed, no block ack resource.\n",
143 sta_id);
144 break;
145 case ADD_STA_MODIFY_NON_EXIST_STA:
Frans Pop91dd6c22010-03-24 14:19:58 -0700146 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800147 sta_id);
148 break;
149 default:
150 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
151 pkt->u.add_sta.status);
152 break;
153 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800154
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800155 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
156 priv->stations[sta_id].sta.mode ==
157 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
158 sta_id, priv->stations[sta_id].sta.sta.addr);
159
160 /*
161 * XXX: The MAC address in the command buffer is often changed from
162 * the original sent to the device. That is, the MAC address
163 * written to the command buffer often is not the same MAC adress
164 * read from the command buffer when the command returns. This
165 * issue has not yet been resolved and this debugging is left to
166 * observe the problem.
167 */
168 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
169 priv->stations[sta_id].sta.mode ==
170 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
171 addsta->sta.addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800172 spin_unlock_irqrestore(&priv->sta_lock, flags);
173}
174
Johannes Berg5696aea2009-07-24 11:13:06 -0700175static void iwl_add_sta_callback(struct iwl_priv *priv,
176 struct iwl_device_cmd *cmd,
Zhu Yi2f301222009-10-09 17:19:45 +0800177 struct iwl_rx_packet *pkt)
Tomas Winkler42132bc2008-05-29 16:35:03 +0800178{
Tomas Winkler3257e5d2008-10-14 12:32:43 -0700179 struct iwl_addsta_cmd *addsta =
180 (struct iwl_addsta_cmd *)cmd->cmd.payload;
Tomas Winkler42132bc2008-05-29 16:35:03 +0800181
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800182 iwl_process_add_sta_resp(priv, addsta, pkt, false);
Tomas Winkler42132bc2008-05-29 16:35:03 +0800183
Tomas Winkler42132bc2008-05-29 16:35:03 +0800184}
185
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800186int iwl_send_add_sta(struct iwl_priv *priv,
Tomas Winkler133636d2008-05-05 10:22:34 +0800187 struct iwl_addsta_cmd *sta, u8 flags)
188{
Zhu Yi2f301222009-10-09 17:19:45 +0800189 struct iwl_rx_packet *pkt = NULL;
Tomas Winkler133636d2008-05-05 10:22:34 +0800190 int ret = 0;
191 u8 data[sizeof(*sta)];
192 struct iwl_host_cmd cmd = {
193 .id = REPLY_ADD_STA,
Johannes Bergc2acea82009-07-24 11:13:05 -0700194 .flags = flags,
Tomas Winkler133636d2008-05-05 10:22:34 +0800195 .data = data,
196 };
Reinette Chatref875f512010-04-05 10:43:10 -0700197 u8 sta_id __maybe_unused = sta->sta.sta_id;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800198
199 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
200 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
Tomas Winkler133636d2008-05-05 10:22:34 +0800201
Tomas Winkler42132bc2008-05-29 16:35:03 +0800202 if (flags & CMD_ASYNC)
Johannes Bergc2acea82009-07-24 11:13:05 -0700203 cmd.callback = iwl_add_sta_callback;
Tomas Winkler42132bc2008-05-29 16:35:03 +0800204 else
Johannes Bergc2acea82009-07-24 11:13:05 -0700205 cmd.flags |= CMD_WANT_SKB;
Tomas Winkler133636d2008-05-05 10:22:34 +0800206
207 cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
208 ret = iwl_send_cmd(priv, &cmd);
209
210 if (ret || (flags & CMD_ASYNC))
211 return ret;
212
Tomas Winkler133636d2008-05-05 10:22:34 +0800213 if (ret == 0) {
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800214 pkt = (struct iwl_rx_packet *)cmd.reply_page;
215 iwl_process_add_sta_resp(priv, sta, pkt, true);
Tomas Winkler133636d2008-05-05 10:22:34 +0800216 }
Zhu Yi64a76b52009-12-10 14:37:21 -0800217 iwl_free_pages(priv, cmd.reply_page);
Tomas Winkler133636d2008-05-05 10:22:34 +0800218
219 return ret;
220}
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800221EXPORT_SYMBOL(iwl_send_add_sta);
Tomas Winkler947b13a2008-04-16 16:34:48 -0700222
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800223static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200224 struct ieee80211_sta_ht_cap *sta_ht_inf)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800225{
226 __le32 sta_flags;
227 u8 mimo_ps_mode;
228
229 if (!sta_ht_inf || !sta_ht_inf->ht_supported)
230 goto done;
231
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800232 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
Wey-Yi Guy3f3e0372009-10-30 14:36:17 -0700233 IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
234 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
235 "static" :
236 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
237 "dynamic" : "disabled");
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800238
239 sta_flags = priv->stations[index].sta.station_flags;
240
241 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
242
243 switch (mimo_ps_mode) {
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800244 case WLAN_HT_CAP_SM_PS_STATIC:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800245 sta_flags |= STA_FLG_MIMO_DIS_MSK;
246 break;
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800247 case WLAN_HT_CAP_SM_PS_DYNAMIC:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800248 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
249 break;
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800250 case WLAN_HT_CAP_SM_PS_DISABLED:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800251 break;
252 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800253 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800254 break;
255 }
256
257 sta_flags |= cpu_to_le32(
258 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
259
260 sta_flags |= cpu_to_le32(
261 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
262
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700263 if (iwl_is_ht40_tx_allowed(priv, sta_ht_inf))
264 sta_flags |= STA_FLG_HT40_EN_MSK;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800265 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700266 sta_flags &= ~STA_FLG_HT40_EN_MSK;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800267
268 priv->stations[index].sta.station_flags = sta_flags;
269 done:
270 return;
271}
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800272
273/**
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800274 * iwl_prep_station - Prepare station information for addition
275 *
276 * should be called with sta_lock held
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800277 */
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800278static u8 iwl_prep_station(struct iwl_priv *priv, const u8 *addr,
279 bool is_ap,
280 struct ieee80211_sta_ht_cap *ht_info)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800281{
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800282 struct iwl_station_entry *station;
Tomas Winklerc587de02009-06-03 11:44:07 -0700283 int i;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800284 u8 sta_id = IWL_INVALID_STATION;
Tomas Winklerc587de02009-06-03 11:44:07 -0700285 u16 rate;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800286
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800287 if (is_ap)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800288 sta_id = IWL_AP_ID;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800289 else if (is_broadcast_ether_addr(addr))
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800290 sta_id = priv->hw_params.bcast_sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800291 else
292 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
293 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
294 addr)) {
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800295 sta_id = i;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800296 break;
297 }
298
299 if (!priv->stations[i].used &&
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800300 sta_id == IWL_INVALID_STATION)
301 sta_id = i;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800302 }
303
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800304 /*
305 * These two conditions have the same outcome, but keep them
306 * separate
307 */
308 if (unlikely(sta_id == IWL_INVALID_STATION))
309 return sta_id;
310
311 /*
312 * uCode is not able to deal with multiple requests to add a
313 * station. Keep track if one is in progress so that we do not send
314 * another.
315 */
316 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
317 IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
318 sta_id);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800319 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800320 }
321
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800322 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
323 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800324 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800325 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
326 sta_id, addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800327 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800328 }
329
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800330 station = &priv->stations[sta_id];
331 station->used = IWL_STA_DRIVER_ACTIVE;
Tomas Winklere1623442009-01-27 14:27:56 -0800332 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700333 sta_id, addr);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800334 priv->num_stations++;
335
336 /* Set up the REPLY_ADD_STA command to send to device */
337 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
338 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
339 station->sta.mode = 0;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800340 station->sta.sta.sta_id = sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800341 station->sta.station_flags = 0;
342
343 /* BCAST station and IBSS stations do not work in HT mode */
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800344 if (sta_id != priv->hw_params.bcast_sta_id &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200345 priv->iw_mode != NL80211_IFTYPE_ADHOC)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800346 iwl_set_ht_add_station(priv, sta_id, ht_info);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800347
Tomas Winklerc587de02009-06-03 11:44:07 -0700348 /* 3945 only */
349 rate = (priv->band == IEEE80211_BAND_5GHZ) ?
350 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
351 /* Turn on both antennas for the station... */
352 station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
353
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800354 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800355
356}
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800357
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800358#define STA_WAIT_TIMEOUT (HZ/2)
359
360/**
361 * iwl_add_station_common -
362 */
363int iwl_add_station_common(struct iwl_priv *priv, const u8 *addr,
364 bool is_ap,
365 struct ieee80211_sta_ht_cap *ht_info,
366 u8 *sta_id_r)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800367{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800368 struct iwl_station_entry *station;
369 unsigned long flags_spin;
370 int ret = 0;
371 u8 sta_id;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800372
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800373 *sta_id_r = 0;
374 spin_lock_irqsave(&priv->sta_lock, flags_spin);
375 sta_id = iwl_prep_station(priv, addr, is_ap, ht_info);
376 if (sta_id == IWL_INVALID_STATION) {
377 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
378 addr);
379 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
380 return -EINVAL;
381 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800382
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800383 /*
384 * uCode is not able to deal with multiple requests to add a
385 * station. Keep track if one is in progress so that we do not send
386 * another.
387 */
388 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
389 IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
390 sta_id);
391 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
392 return -EEXIST;
393 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800394
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800395 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
396 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
397 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
398 sta_id, addr);
399 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
400 return -EEXIST;
401 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800402
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800403 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
404 station = &priv->stations[sta_id];
405 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
406
407 /* Add station to device's station table */
408 ret = iwl_send_add_sta(priv, &station->sta, CMD_SYNC);
409 if (ret) {
410 IWL_ERR(priv, "Adding station %pM failed.\n", station->sta.sta.addr);
411 spin_lock_irqsave(&priv->sta_lock, flags_spin);
412 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
413 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
414 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
415 }
416 *sta_id_r = sta_id;
417 return ret;
418}
419EXPORT_SYMBOL(iwl_add_station_common);
420
421static void iwl_sta_init_lq(struct iwl_priv *priv, const u8 *addr, bool is_ap)
422{
423 int i, r;
424 struct iwl_link_quality_cmd link_cmd = {
425 .reserved1 = 0,
426 };
427 u32 rate_flags;
Wey-Yi Guy57e40d32010-03-09 15:36:10 -0800428 int ret = 0;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800429
430 /* Set up the rate scaling to start at selected rate, fall back
431 * all the way down to 1M in IEEE order, and then spin on 1M */
432 if (is_ap)
433 r = IWL_RATE_54M_INDEX;
434 else if (priv->band == IEEE80211_BAND_5GHZ)
435 r = IWL_RATE_6M_INDEX;
436 else
437 r = IWL_RATE_1M_INDEX;
438
439 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
440 rate_flags = 0;
441 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
442 rate_flags |= RATE_MCS_CCK_MSK;
443
444 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
445 RATE_MCS_ANT_POS;
446
447 link_cmd.rs_table[i].rate_n_flags =
448 iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
449 r = iwl_get_prev_ieee_rate(r);
450 }
451
452 link_cmd.general_params.single_stream_ant_msk =
453 first_antenna(priv->hw_params.valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700454
455 link_cmd.general_params.dual_stream_ant_msk =
456 priv->hw_params.valid_tx_ant &
457 ~first_antenna(priv->hw_params.valid_tx_ant);
458 if (!link_cmd.general_params.dual_stream_ant_msk) {
459 link_cmd.general_params.dual_stream_ant_msk = ANT_AB;
460 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
461 link_cmd.general_params.dual_stream_ant_msk =
462 priv->hw_params.valid_tx_ant;
463 }
464
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800465 link_cmd.agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
466 link_cmd.agg_params.agg_time_limit =
467 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
468
469 /* Update the rate scaling for control frame Tx to AP */
470 link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
471
Wey-Yi Guy57e40d32010-03-09 15:36:10 -0800472 ret = iwl_send_cmd_pdu(priv, REPLY_TX_LINK_QUALITY_CMD,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800473 sizeof(link_cmd), &link_cmd);
Wey-Yi Guy57e40d32010-03-09 15:36:10 -0800474 if (ret)
475 IWL_ERR(priv, "REPLY_TX_LINK_QUALITY_CMD failed (%d)\n", ret);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800476}
477
478/*
479 * iwl_add_local_stations - Add stations not requested by mac80211
480 *
481 * This will be either the broadcast station or the bssid station needed by
482 * ad-hoc.
483 *
484 * Function sleeps.
485 */
486int iwl_add_local_station(struct iwl_priv *priv, const u8 *addr, bool init_rs)
487{
488 int ret;
489 u8 sta_id;
490
491 ret = iwl_add_station_common(priv, addr, 0, NULL, &sta_id);
492 if (ret) {
493 IWL_ERR(priv, "Unable to add station %pM\n", addr);
494 return ret;
495 }
496
497 if (init_rs)
498 /* Set up default rate scaling table in device's station table */
499 iwl_sta_init_lq(priv, addr, false);
500 return 0;
501}
502EXPORT_SYMBOL(iwl_add_local_station);
503
504/**
505 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
506 *
507 * priv->sta_lock must be held
508 */
509static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
510{
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800511 /* Ucode must be active and driver must be non active */
512 if (priv->stations[sta_id].used != IWL_STA_UCODE_ACTIVE)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800513 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800514
515 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
516
517 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800518 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800519}
520
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800521static int iwl_send_remove_station(struct iwl_priv *priv,
522 struct iwl_station_entry *station)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800523{
Zhu Yi2f301222009-10-09 17:19:45 +0800524 struct iwl_rx_packet *pkt;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800525 int ret;
526
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800527 unsigned long flags_spin;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800528 struct iwl_rem_sta_cmd rm_sta_cmd;
529
530 struct iwl_host_cmd cmd = {
531 .id = REPLY_REMOVE_STA,
532 .len = sizeof(struct iwl_rem_sta_cmd),
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800533 .flags = CMD_SYNC,
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800534 .data = &rm_sta_cmd,
535 };
536
537 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
538 rm_sta_cmd.num_sta = 1;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800539 memcpy(&rm_sta_cmd.addr, &station->sta.sta.addr , ETH_ALEN);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800540
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800541 cmd.flags |= CMD_WANT_SKB;
542
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800543 ret = iwl_send_cmd(priv, &cmd);
544
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800545 if (ret)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800546 return ret;
547
Zhu Yi2f301222009-10-09 17:19:45 +0800548 pkt = (struct iwl_rx_packet *)cmd.reply_page;
549 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800550 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800551 pkt->hdr.flags);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800552 ret = -EIO;
553 }
554
555 if (!ret) {
Zhu Yi2f301222009-10-09 17:19:45 +0800556 switch (pkt->u.rem_sta.status) {
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800557 case REM_STA_SUCCESS_MSK:
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800558 spin_lock_irqsave(&priv->sta_lock, flags_spin);
559 iwl_sta_ucode_deactivate(priv, station->sta.sta.sta_id);
560 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Tomas Winklere1623442009-01-27 14:27:56 -0800561 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800562 break;
563 default:
564 ret = -EIO;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800565 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800566 break;
567 }
568 }
Zhu Yi64a76b52009-12-10 14:37:21 -0800569 iwl_free_pages(priv, cmd.reply_page);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800570
571 return ret;
572}
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +0800573
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800574/**
575 * iwl_remove_station - Remove driver's knowledge of station.
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800576 */
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800577static int iwl_remove_station(struct iwl_priv *priv, struct ieee80211_sta *sta)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800578{
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800579 int sta_id = IWL_INVALID_STATION;
580 int i, ret = -EINVAL;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800581 unsigned long flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800582 bool is_ap = priv->iw_mode == NL80211_IFTYPE_STATION;
583 struct iwl_station_entry *station;
584
585 if (!iwl_is_ready(priv)) {
586 IWL_DEBUG_INFO(priv,
Frans Pop91dd6c22010-03-24 14:19:58 -0700587 "Unable to remove station %pM, device not ready.\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800588 sta->addr);
589 /*
590 * It is typical for stations to be removed when we are
591 * going down. Return success since device will be down
592 * soon anyway
593 */
594 return 0;
595 }
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800596
597 spin_lock_irqsave(&priv->sta_lock, flags);
598
599 if (is_ap)
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800600 sta_id = IWL_AP_ID;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800601 else
602 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
603 if (priv->stations[i].used &&
604 !compare_ether_addr(priv->stations[i].sta.sta.addr,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800605 sta->addr)) {
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800606 sta_id = i;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800607 break;
608 }
609
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800610 if (unlikely(sta_id == IWL_INVALID_STATION))
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800611 goto out;
612
Tomas Winklere1623442009-01-27 14:27:56 -0800613 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800614 sta_id, sta->addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800615
616 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800617 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
618 sta->addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800619 goto out;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800620 }
621
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800622 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800623 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
624 sta->addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800625 goto out;
626 }
627
628
629 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
630
631 priv->num_stations--;
632
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800633 BUG_ON(priv->num_stations < 0);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800634
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800635 station = &priv->stations[sta_id];
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800636 spin_unlock_irqrestore(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800637
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800638 ret = iwl_send_remove_station(priv, station);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800639 return ret;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800640out:
641 spin_unlock_irqrestore(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800642 return ret;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800643}
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800644
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800645/**
Reinette Chatre7e246192010-02-18 22:58:32 -0800646 * iwl_clear_ucode_stations() - clear entire station table driver and/or ucode
647 * @priv:
648 * @force: If set then the uCode station table needs to be cleared here. If
649 * not set then the uCode station table has already been cleared,
650 * for example after sending it a RXON command without ASSOC bit
651 * set, and we just need to change driver state here.
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800652 */
Reinette Chatre7e246192010-02-18 22:58:32 -0800653void iwl_clear_ucode_stations(struct iwl_priv *priv, bool force)
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800654{
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700655 int i;
Reinette Chatre7e246192010-02-18 22:58:32 -0800656 unsigned long flags_spin;
657 bool cleared = false;
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800658
Reinette Chatre7e246192010-02-18 22:58:32 -0800659 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver%s\n",
660 force ? " and ucode" : "");
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800661
Reinette Chatre7e246192010-02-18 22:58:32 -0800662 if (force) {
663 if (!iwl_is_ready(priv)) {
664 /*
665 * If device is not ready at this point the station
666 * table is likely already empty (uCode not ready
667 * to receive station requests) or will soon be
668 * due to interface going down.
669 */
670 IWL_DEBUG_INFO(priv, "Unable to remove stations from device - device not ready\n");
671 } else {
672 iwl_send_cmd_pdu_async(priv, REPLY_REMOVE_ALL_STA, 0, NULL, NULL);
673 }
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700674 }
675
Reinette Chatre7e246192010-02-18 22:58:32 -0800676 spin_lock_irqsave(&priv->sta_lock, flags_spin);
677 if (force) {
678 IWL_DEBUG_INFO(priv, "Clearing all station information in driver\n");
679 priv->num_stations = 0;
680 memset(priv->stations, 0, sizeof(priv->stations));
681 } else {
682 for (i = 0; i < priv->hw_params.max_stations; i++) {
683 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
Frans Pop91dd6c22010-03-24 14:19:58 -0700684 IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d\n", i);
Reinette Chatre7e246192010-02-18 22:58:32 -0800685 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
686 cleared = true;
687 }
688 }
689 }
690 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
691
692 if (!cleared)
693 IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800694}
Reinette Chatre7e246192010-02-18 22:58:32 -0800695EXPORT_SYMBOL(iwl_clear_ucode_stations);
696
697/**
698 * iwl_restore_stations() - Restore driver known stations to device
699 *
700 * All stations considered active by driver, but not present in ucode, is
701 * restored.
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800702 *
703 * Function sleeps.
Reinette Chatre7e246192010-02-18 22:58:32 -0800704 */
705void iwl_restore_stations(struct iwl_priv *priv)
706{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800707 struct iwl_station_entry *station;
Reinette Chatre7e246192010-02-18 22:58:32 -0800708 unsigned long flags_spin;
709 int i;
710 bool found = false;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800711 int ret;
Reinette Chatre7e246192010-02-18 22:58:32 -0800712
713 if (!iwl_is_ready(priv)) {
714 IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
715 return;
716 }
717
718 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
719 spin_lock_irqsave(&priv->sta_lock, flags_spin);
720 for (i = 0; i < priv->hw_params.max_stations; i++) {
721 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
722 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
723 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
724 priv->stations[i].sta.sta.addr);
725 priv->stations[i].sta.mode = 0;
726 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
727 found = true;
728 }
729 }
730
731 for (i = 0; i < priv->hw_params.max_stations; i++) {
732 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800733 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
734 station = &priv->stations[i];
735 ret = iwl_send_add_sta(priv, &priv->stations[i].sta, CMD_SYNC);
736 if (ret) {
737 IWL_ERR(priv, "Adding station %pM failed.\n",
738 station->sta.sta.addr);
739 spin_lock_irqsave(&priv->sta_lock, flags_spin);
740 priv->stations[i].used &= ~IWL_STA_DRIVER_ACTIVE;
741 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
742 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
743 }
744 /*
745 * Rate scaling has already been initialized, send
746 * current LQ command
747 */
748 if (station->lq)
749 iwl_send_lq_cmd(priv, station->lq, CMD_SYNC, true);
750 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Reinette Chatre7e246192010-02-18 22:58:32 -0800751 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
752 }
753 }
754
755 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
756 if (!found)
757 IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
758 else
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800759 IWL_DEBUG_INFO(priv, "Restoring all known stations .... complete.\n");
Reinette Chatre7e246192010-02-18 22:58:32 -0800760}
761EXPORT_SYMBOL(iwl_restore_stations);
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800762
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800763int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700764{
765 int i;
766
767 for (i = 0; i < STA_KEY_MAX_NUM; i++)
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700768 if (!test_and_set_bit(i, &priv->ucode_key_table))
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700769 return i;
770
Tomas Winkler40a9a822008-11-25 23:29:03 +0200771 return WEP_INVALID_OFFSET;
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700772}
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800773EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700774
Johannes Berg335348b2010-03-30 10:11:46 -0700775static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700776{
777 int i, not_empty = 0;
778 u8 buff[sizeof(struct iwl_wep_cmd) +
779 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
780 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
781 size_t cmd_size = sizeof(struct iwl_wep_cmd);
782 struct iwl_host_cmd cmd = {
783 .id = REPLY_WEPKEY,
784 .data = wep_cmd,
Johannes Berg72e15d72010-02-19 11:42:32 -0800785 .flags = CMD_SYNC,
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700786 };
787
Johannes Berg72e15d72010-02-19 11:42:32 -0800788 might_sleep();
789
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700790 memset(wep_cmd, 0, cmd_size +
791 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
792
793 for (i = 0; i < WEP_KEYS_MAX ; i++) {
794 wep_cmd->key[i].key_index = i;
795 if (priv->wep_keys[i].key_size) {
796 wep_cmd->key[i].key_offset = i;
797 not_empty = 1;
798 } else {
799 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
800 }
801
802 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
803 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
804 priv->wep_keys[i].key_size);
805 }
806
807 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
808 wep_cmd->num_keys = WEP_KEYS_MAX;
809
810 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
811
812 cmd.len = cmd_size;
813
814 if (not_empty || send_if_empty)
815 return iwl_send_cmd(priv, &cmd);
816 else
817 return 0;
818}
Johannes Berg335348b2010-03-30 10:11:46 -0700819
820int iwl_restore_default_wep_keys(struct iwl_priv *priv)
821{
822 WARN_ON(!mutex_is_locked(&priv->mutex));
823
824 return iwl_send_static_wepkey_cmd(priv, 0);
825}
826EXPORT_SYMBOL(iwl_restore_default_wep_keys);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700827
828int iwl_remove_default_wep_key(struct iwl_priv *priv,
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700829 struct ieee80211_key_conf *keyconf)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700830{
831 int ret;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700832
Johannes Berg72e15d72010-02-19 11:42:32 -0800833 WARN_ON(!mutex_is_locked(&priv->mutex));
834
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700835 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
836 keyconf->keyidx);
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700837
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700838 memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700839 if (iwl_is_rfkill(priv)) {
840 IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berg72e15d72010-02-19 11:42:32 -0800841 /* but keys in device are clear anyway so return success */
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700842 return 0;
843 }
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700844 ret = iwl_send_static_wepkey_cmd(priv, 1);
Tomas Winklere1623442009-01-27 14:27:56 -0800845 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800846 keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700847
848 return ret;
849}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800850EXPORT_SYMBOL(iwl_remove_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700851
852int iwl_set_default_wep_key(struct iwl_priv *priv,
853 struct ieee80211_key_conf *keyconf)
854{
855 int ret;
Johannes Berg72e15d72010-02-19 11:42:32 -0800856
857 WARN_ON(!mutex_is_locked(&priv->mutex));
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700858
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800859 if (keyconf->keylen != WEP_KEY_LEN_128 &&
860 keyconf->keylen != WEP_KEY_LEN_64) {
Tomas Winklere1623442009-01-27 14:27:56 -0800861 IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800862 return -EINVAL;
863 }
864
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700865 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800866 keyconf->hw_key_idx = HW_KEY_DEFAULT;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700867 priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
868
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700869 priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
870 memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
871 keyconf->keylen);
872
873 ret = iwl_send_static_wepkey_cmd(priv, 0);
Tomas Winklere1623442009-01-27 14:27:56 -0800874 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800875 keyconf->keylen, keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700876
877 return ret;
878}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800879EXPORT_SYMBOL(iwl_set_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700880
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700881static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700882 struct ieee80211_key_conf *keyconf,
883 u8 sta_id)
884{
885 unsigned long flags;
886 __le16 key_flags = 0;
887 int ret;
888
889 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700890
891 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
892 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
893 key_flags &= ~STA_KEY_FLG_INVALID;
894
895 if (keyconf->keylen == WEP_KEY_LEN_128)
896 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
897
Tomas Winkler5425e492008-04-15 16:01:38 -0700898 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700899 key_flags |= STA_KEY_MULTICAST_MSK;
900
901 spin_lock_irqsave(&priv->sta_lock, flags);
902
903 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
904 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
905 priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
906
907 memcpy(priv->stations[sta_id].keyinfo.key,
908 keyconf->key, keyconf->keylen);
909
910 memcpy(&priv->stations[sta_id].sta.key.key[3],
911 keyconf->key, keyconf->keylen);
912
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700913 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
914 == STA_KEY_FLG_NO_ENC)
915 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700916 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700917 /* else, we are overriding an existing key => no need to allocated room
918 * in uCode. */
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700919
Tomas Winkler40a9a822008-11-25 23:29:03 +0200920 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100921 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200922
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700923 priv->stations[sta_id].sta.key.key_flags = key_flags;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700924 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
925 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
926
Tomas Winkler133636d2008-05-05 10:22:34 +0800927 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700928
929 spin_unlock_irqrestore(&priv->sta_lock, flags);
930
931 return ret;
932}
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700933
934static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
935 struct ieee80211_key_conf *keyconf,
936 u8 sta_id)
937{
938 unsigned long flags;
939 __le16 key_flags = 0;
Tomas Winkler40a9a822008-11-25 23:29:03 +0200940 int ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700941
942 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
943 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
944 key_flags &= ~STA_KEY_FLG_INVALID;
945
Tomas Winkler5425e492008-04-15 16:01:38 -0700946 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700947 key_flags |= STA_KEY_MULTICAST_MSK;
948
949 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700950
951 spin_lock_irqsave(&priv->sta_lock, flags);
952 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
953 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
954
955 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
956 keyconf->keylen);
957
958 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
959 keyconf->keylen);
960
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700961 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
962 == STA_KEY_FLG_NO_ENC)
963 priv->stations[sta_id].sta.key.key_offset =
964 iwl_get_free_ucode_key_index(priv);
965 /* else, we are overriding an existing key => no need to allocated room
966 * in uCode. */
967
Tomas Winkler40a9a822008-11-25 23:29:03 +0200968 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100969 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200970
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700971 priv->stations[sta_id].sta.key.key_flags = key_flags;
972 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
973 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
974
Tomas Winkler40a9a822008-11-25 23:29:03 +0200975 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
976
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700977 spin_unlock_irqrestore(&priv->sta_lock, flags);
978
Tomas Winkler40a9a822008-11-25 23:29:03 +0200979 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700980}
981
982static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
983 struct ieee80211_key_conf *keyconf,
984 u8 sta_id)
985{
986 unsigned long flags;
987 int ret = 0;
Reinette Chatre299f5462009-04-30 13:56:31 -0700988 __le16 key_flags = 0;
989
990 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
991 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
992 key_flags &= ~STA_KEY_FLG_INVALID;
993
994 if (sta_id == priv->hw_params.bcast_sta_id)
995 key_flags |= STA_KEY_MULTICAST_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700996
997 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
998 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700999
1000 spin_lock_irqsave(&priv->sta_lock, flags);
1001
1002 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001003 priv->stations[sta_id].keyinfo.keylen = 16;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001004
1005 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
1006 == STA_KEY_FLG_NO_ENC)
1007 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach77bab602008-04-15 16:01:44 -07001008 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001009 /* else, we are overriding an existing key => no need to allocated room
1010 * in uCode. */
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001011
Tomas Winkler40a9a822008-11-25 23:29:03 +02001012 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +01001013 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +02001014
Reinette Chatre299f5462009-04-30 13:56:31 -07001015 priv->stations[sta_id].sta.key.key_flags = key_flags;
1016
1017
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001018 /* This copy is acutally not needed: we get the key with each TX */
1019 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
1020
1021 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
1022
1023 spin_unlock_irqrestore(&priv->sta_lock, flags);
1024
1025 return ret;
1026}
1027
Tomas Winkler9f586712008-11-12 13:14:05 -08001028void iwl_update_tkip_key(struct iwl_priv *priv,
1029 struct ieee80211_key_conf *keyconf,
1030 const u8 *addr, u32 iv32, u16 *phase1key)
1031{
1032 u8 sta_id = IWL_INVALID_STATION;
1033 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -08001034 int i;
Tomas Winkler9f586712008-11-12 13:14:05 -08001035
Tomas Winklerc587de02009-06-03 11:44:07 -07001036 sta_id = iwl_find_station(priv, addr);
Tomas Winkler9f586712008-11-12 13:14:05 -08001037 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08001038 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
Tomas Winkler9f586712008-11-12 13:14:05 -08001039 addr);
1040 return;
1041 }
1042
1043 if (iwl_scan_cancel(priv)) {
1044 /* cancel scan failed, just live w/ bad key and rely
1045 briefly on SW decryption */
1046 return;
1047 }
1048
Tomas Winkler9f586712008-11-12 13:14:05 -08001049 spin_lock_irqsave(&priv->sta_lock, flags);
1050
Tomas Winkler9f586712008-11-12 13:14:05 -08001051 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
1052
1053 for (i = 0; i < 5; i++)
1054 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
1055 cpu_to_le16(phase1key[i]);
1056
1057 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1058 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1059
1060 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1061
1062 spin_unlock_irqrestore(&priv->sta_lock, flags);
1063
1064}
1065EXPORT_SYMBOL(iwl_update_tkip_key);
1066
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001067int iwl_remove_dynamic_key(struct iwl_priv *priv,
1068 struct ieee80211_key_conf *keyconf,
1069 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001070{
1071 unsigned long flags;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001072 int ret = 0;
1073 u16 key_flags;
1074 u8 keyidx;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001075
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001076 priv->key_mapping_key--;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001077
1078 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001079 key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
1080 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
1081
Tomas Winklere1623442009-01-27 14:27:56 -08001082 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +08001083 keyconf->keyidx, sta_id);
1084
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001085 if (keyconf->keyidx != keyidx) {
1086 /* We need to remove a key with index different that the one
1087 * in the uCode. This means that the key we need to remove has
1088 * been replaced by another one with different index.
1089 * Don't do anything and return ok
1090 */
1091 spin_unlock_irqrestore(&priv->sta_lock, flags);
1092 return 0;
1093 }
1094
Tomas Winkler40a9a822008-11-25 23:29:03 +02001095 if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001096 IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
Tomas Winkler40a9a822008-11-25 23:29:03 +02001097 keyconf->keyidx, key_flags);
1098 spin_unlock_irqrestore(&priv->sta_lock, flags);
1099 return 0;
1100 }
1101
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001102 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
1103 &priv->ucode_key_table))
Winkler, Tomas15b16872008-12-19 10:37:33 +08001104 IWL_ERR(priv, "index %d not used in uCode key table.\n",
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001105 priv->stations[sta_id].sta.key.key_offset);
1106 memset(&priv->stations[sta_id].keyinfo, 0,
Tomas Winkler6def9762008-05-05 10:22:31 +08001107 sizeof(struct iwl_hw_key));
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001108 memset(&priv->stations[sta_id].sta.key, 0,
1109 sizeof(struct iwl4965_keyinfo));
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001110 priv->stations[sta_id].sta.key.key_flags =
1111 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
1112 priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001113 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1114 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001115
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -07001116 if (iwl_is_rfkill(priv)) {
Frans Pop91dd6c22010-03-24 14:19:58 -07001117 IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -07001118 spin_unlock_irqrestore(&priv->sta_lock, flags);
1119 return 0;
1120 }
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001121 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001122 spin_unlock_irqrestore(&priv->sta_lock, flags);
1123 return ret;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001124}
Tomas Winkler27aaba02008-05-05 10:22:44 +08001125EXPORT_SYMBOL(iwl_remove_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001126
1127int iwl_set_dynamic_key(struct iwl_priv *priv,
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001128 struct ieee80211_key_conf *keyconf, u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001129{
1130 int ret;
1131
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001132 priv->key_mapping_key++;
1133 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001134
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001135 switch (keyconf->alg) {
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001136 case ALG_CCMP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001137 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001138 break;
1139 case ALG_TKIP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001140 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001141 break;
1142 case ALG_WEP:
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001143 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001144 break;
1145 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001146 IWL_ERR(priv,
1147 "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001148 ret = -EINVAL;
1149 }
1150
Tomas Winklere1623442009-01-27 14:27:56 -08001151 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 +08001152 keyconf->alg, keyconf->keylen, keyconf->keyidx,
1153 sta_id, ret);
1154
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001155 return ret;
1156}
Tomas Winkler27aaba02008-05-05 10:22:44 +08001157EXPORT_SYMBOL(iwl_set_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001158
Tomas Winkler66c73db2008-04-15 16:01:40 -07001159#ifdef CONFIG_IWLWIFI_DEBUG
1160static void iwl_dump_lq_cmd(struct iwl_priv *priv,
1161 struct iwl_link_quality_cmd *lq)
1162{
1163 int i;
Tomas Winklere1623442009-01-27 14:27:56 -08001164 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
1165 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001166 lq->general_params.single_stream_ant_msk,
1167 lq->general_params.dual_stream_ant_msk);
1168
1169 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
Tomas Winklere1623442009-01-27 14:27:56 -08001170 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001171 i, lq->rs_table[i].rate_n_flags);
1172}
1173#else
1174static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
1175 struct iwl_link_quality_cmd *lq)
1176{
1177}
1178#endif
1179
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001180/**
1181 * iwl_send_lq_cmd() - Send link quality command
1182 * @init: This command is sent as part of station initialization right
1183 * after station has been added.
1184 *
1185 * The link quality command is sent as the last step of station creation.
1186 * This is the special case in which init is set and we call a callback in
1187 * this case to clear the state indicating that station creation is in
1188 * progress.
1189 */
Tomas Winkler66c73db2008-04-15 16:01:40 -07001190int iwl_send_lq_cmd(struct iwl_priv *priv,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001191 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
Tomas Winkler66c73db2008-04-15 16:01:40 -07001192{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001193 int ret = 0;
1194 unsigned long flags_spin;
1195
Tomas Winkler66c73db2008-04-15 16:01:40 -07001196 struct iwl_host_cmd cmd = {
1197 .id = REPLY_TX_LINK_QUALITY_CMD,
1198 .len = sizeof(struct iwl_link_quality_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07001199 .flags = flags,
Tomas Winkler66c73db2008-04-15 16:01:40 -07001200 .data = lq,
1201 };
1202
Johannes Berg76c3c692010-03-30 02:46:29 -07001203 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
Tomas Winkler66c73db2008-04-15 16:01:40 -07001204 return -EINVAL;
1205
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001206 iwl_dump_lq_cmd(priv, lq);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001207 BUG_ON(init && (cmd.flags & CMD_ASYNC));
Tomas Winkler66c73db2008-04-15 16:01:40 -07001208
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001209 iwl_dump_lq_cmd(priv, lq);
1210 ret = iwl_send_cmd(priv, &cmd);
1211 if (ret || (cmd.flags & CMD_ASYNC))
1212 return ret;
Tomas Winkler66c73db2008-04-15 16:01:40 -07001213
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001214 if (init) {
Frans Pop91dd6c22010-03-24 14:19:58 -07001215 IWL_DEBUG_INFO(priv, "init LQ command complete, clearing sta addition status for sta %d\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001216 lq->sta_id);
1217 spin_lock_irqsave(&priv->sta_lock, flags_spin);
1218 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
1219 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
1220 }
Tomas Winkler66c73db2008-04-15 16:01:40 -07001221 return 0;
1222}
1223EXPORT_SYMBOL(iwl_send_lq_cmd);
1224
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001225/**
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001226 * iwl_add_bcast_station - add broadcast station into station table.
1227 */
1228void iwl_add_bcast_station(struct iwl_priv *priv)
1229{
Reinette Chatre3459ab52010-01-22 14:22:49 -08001230 IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001231 iwl_add_local_station(priv, iwl_bcast_addr, true);
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001232}
1233EXPORT_SYMBOL(iwl_add_bcast_station);
1234
1235/**
Reinette Chatre3459ab52010-01-22 14:22:49 -08001236 * iwl3945_add_bcast_station - add broadcast station into station table.
1237 */
1238void iwl3945_add_bcast_station(struct iwl_priv *priv)
1239{
1240 IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001241 iwl_add_local_station(priv, iwl_bcast_addr, false);
1242 /*
1243 * It is assumed that when station is added more initialization
1244 * needs to be done, but for 3945 it is not the case and we can
1245 * just release station table access right here.
1246 */
1247 priv->stations[priv->hw_params.bcast_sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
1248
Reinette Chatre3459ab52010-01-22 14:22:49 -08001249}
1250EXPORT_SYMBOL(iwl3945_add_bcast_station);
1251
1252/**
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001253 * iwl_get_sta_id - Find station's index within station table
1254 *
1255 * If new IBSS station, create new entry in station table
1256 */
1257int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
1258{
1259 int sta_id;
Luis R. Rodriguez943ab702009-07-14 20:14:07 -04001260 __le16 fc = hdr->frame_control;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001261
1262 /* If this frame is broadcast or management, use broadcast station id */
Luis R. Rodriguez943ab702009-07-14 20:14:07 -04001263 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001264 return priv->hw_params.bcast_sta_id;
1265
1266 switch (priv->iw_mode) {
1267
1268 /* If we are a client station in a BSS network, use the special
1269 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02001270 case NL80211_IFTYPE_STATION:
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001271 /*
1272 * If addition of station not complete yet, which means
1273 * that rate scaling has not been initialized, then return
1274 * the broadcast station.
1275 */
1276 if (!(priv->stations[IWL_AP_ID].used & IWL_STA_UCODE_ACTIVE))
1277 return priv->hw_params.bcast_sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001278 return IWL_AP_ID;
1279
1280 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02001281 case NL80211_IFTYPE_AP:
Tomas Winklerc587de02009-06-03 11:44:07 -07001282 sta_id = iwl_find_station(priv, hdr->addr1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001283 if (sta_id != IWL_INVALID_STATION)
1284 return sta_id;
1285 return priv->hw_params.bcast_sta_id;
1286
1287 /* If this frame is going out to an IBSS network, find the station,
1288 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02001289 case NL80211_IFTYPE_ADHOC:
Tomas Winklerc587de02009-06-03 11:44:07 -07001290 sta_id = iwl_find_station(priv, hdr->addr1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001291 if (sta_id != IWL_INVALID_STATION)
1292 return sta_id;
1293
Tomas Winklere1623442009-01-27 14:27:56 -08001294 IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001295 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07001296 hdr->addr1);
Reinette Chatre3d816c72009-08-07 15:41:37 -07001297 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001298 return priv->hw_params.bcast_sta_id;
1299
1300 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001301 IWL_WARN(priv, "Unknown mode of operation: %d\n",
1302 priv->iw_mode);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001303 return priv->hw_params.bcast_sta_id;
1304 }
1305}
1306EXPORT_SYMBOL(iwl_get_sta_id);
1307
Tomas Winkler5083e562008-05-29 16:35:15 +08001308/**
Tomas Winkler9f586712008-11-12 13:14:05 -08001309 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
Tomas Winkler5083e562008-05-29 16:35:15 +08001310 */
Tomas Winkler9f586712008-11-12 13:14:05 -08001311void iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
Tomas Winkler5083e562008-05-29 16:35:15 +08001312{
1313 unsigned long flags;
1314
1315 /* Remove "disable" flag, to enable Tx for this TID */
1316 spin_lock_irqsave(&priv->sta_lock, flags);
1317 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1318 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1319 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1320 spin_unlock_irqrestore(&priv->sta_lock, flags);
1321
1322 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1323}
Tomas Winkler9f586712008-11-12 13:14:05 -08001324EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
1325
1326int iwl_sta_rx_agg_start(struct iwl_priv *priv,
1327 const u8 *addr, int tid, u16 ssn)
1328{
1329 unsigned long flags;
1330 int sta_id;
1331
Tomas Winklerc587de02009-06-03 11:44:07 -07001332 sta_id = iwl_find_station(priv, addr);
Tomas Winkler9f586712008-11-12 13:14:05 -08001333 if (sta_id == IWL_INVALID_STATION)
1334 return -ENXIO;
1335
1336 spin_lock_irqsave(&priv->sta_lock, flags);
1337 priv->stations[sta_id].sta.station_flags_msk = 0;
1338 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1339 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1340 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1341 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1342 spin_unlock_irqrestore(&priv->sta_lock, flags);
1343
1344 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1345 CMD_ASYNC);
1346}
1347EXPORT_SYMBOL(iwl_sta_rx_agg_start);
1348
1349int iwl_sta_rx_agg_stop(struct iwl_priv *priv, const u8 *addr, int tid)
1350{
1351 unsigned long flags;
1352 int sta_id;
1353
Tomas Winklerc587de02009-06-03 11:44:07 -07001354 sta_id = iwl_find_station(priv, addr);
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001355 if (sta_id == IWL_INVALID_STATION) {
1356 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
Tomas Winkler9f586712008-11-12 13:14:05 -08001357 return -ENXIO;
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001358 }
Tomas Winkler9f586712008-11-12 13:14:05 -08001359
1360 spin_lock_irqsave(&priv->sta_lock, flags);
1361 priv->stations[sta_id].sta.station_flags_msk = 0;
1362 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1363 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1364 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1365 spin_unlock_irqrestore(&priv->sta_lock, flags);
1366
1367 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1368 CMD_ASYNC);
1369}
1370EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
1371
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001372void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
Tomas Winkler9f586712008-11-12 13:14:05 -08001373{
1374 unsigned long flags;
1375
1376 spin_lock_irqsave(&priv->sta_lock, flags);
1377 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1378 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1379 priv->stations[sta_id].sta.sta.modify_mask = 0;
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001380 priv->stations[sta_id].sta.sleep_tx_count = 0;
Tomas Winkler9f586712008-11-12 13:14:05 -08001381 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1382 spin_unlock_irqrestore(&priv->sta_lock, flags);
1383
1384 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1385}
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001386EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
Tomas Winkler9f586712008-11-12 13:14:05 -08001387
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001388void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
Tomas Winkler9f586712008-11-12 13:14:05 -08001389{
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001390 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -08001391
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001392 spin_lock_irqsave(&priv->sta_lock, flags);
1393 priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
1394 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1395 priv->stations[sta_id].sta.sta.modify_mask =
1396 STA_MODIFY_SLEEP_TX_COUNT_MSK;
1397 priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
1398 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1399 spin_unlock_irqrestore(&priv->sta_lock, flags);
Tomas Winkler9f586712008-11-12 13:14:05 -08001400
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001401 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001402}
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -07001403EXPORT_SYMBOL(iwl_sta_modify_sleep_tx_count);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001404
1405int iwl_mac_sta_remove(struct ieee80211_hw *hw,
1406 struct ieee80211_vif *vif,
1407 struct ieee80211_sta *sta)
1408{
1409 int ret;
1410 struct iwl_priv *priv = hw->priv;
1411 IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
1412 sta->addr);
1413 ret = iwl_remove_station(priv, sta);
1414 if (ret)
1415 IWL_ERR(priv, "Error removing station %pM\n",
1416 sta->addr);
1417 return ret;
1418}
1419EXPORT_SYMBOL(iwl_mac_sta_remove);