blob: ccd09027c7cdd8f3a1b479e6fda1215c960bd102 [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>
Johannes Berg4620fef2010-06-16 03:30:27 -070033#include <linux/lockdep.h>
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070034
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070035#include "iwl-dev.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070036#include "iwl-core.h"
37#include "iwl-sta.h"
Tomas Winkler7a999bf2008-05-29 16:35:02 +080038
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080039/* priv->sta_lock must be held */
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080040static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
41{
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080042
43 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
44 IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u addr %pM\n",
45 sta_id, priv->stations[sta_id].sta.sta.addr);
46
47 if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
48 IWL_DEBUG_ASSOC(priv,
49 "STA id %u addr %pM already present in uCode (according to driver)\n",
50 sta_id, priv->stations[sta_id].sta.sta.addr);
51 } else {
52 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
53 IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
54 sta_id, priv->stations[sta_id].sta.sta.addr);
55 }
56}
57
Johannes Berg09034cb2010-06-14 08:32:38 -070058static int iwl_process_add_sta_resp(struct iwl_priv *priv,
59 struct iwl_addsta_cmd *addsta,
60 struct iwl_rx_packet *pkt,
61 bool sync)
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080062{
63 u8 sta_id = addsta->sta.sta_id;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080064 unsigned long flags;
Johannes Berg09034cb2010-06-14 08:32:38 -070065 int ret = -EIO;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080066
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080067 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
68 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
69 pkt->hdr.flags);
Johannes Berg09034cb2010-06-14 08:32:38 -070070 return ret;
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080071 }
72
73 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
74 sta_id);
75
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +080076 spin_lock_irqsave(&priv->sta_lock, flags);
77
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080078 switch (pkt->u.add_sta.status) {
79 case ADD_STA_SUCCESS_MSK:
80 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
81 iwl_sta_ucode_activate(priv, sta_id);
Johannes Berg09034cb2010-06-14 08:32:38 -070082 ret = 0;
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080083 break;
84 case ADD_STA_NO_ROOM_IN_TABLE:
85 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
Winkler, Tomas15b16872008-12-19 10:37:33 +080086 sta_id);
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080087 break;
88 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
89 IWL_ERR(priv, "Adding station %d failed, no block ack resource.\n",
90 sta_id);
91 break;
92 case ADD_STA_MODIFY_NON_EXIST_STA:
Frans Pop91dd6c22010-03-24 14:19:58 -070093 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
Reinette Chatre1fa97aa2010-01-22 14:22:48 -080094 sta_id);
95 break;
96 default:
97 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
98 pkt->u.add_sta.status);
99 break;
100 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800101
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800102 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
103 priv->stations[sta_id].sta.mode ==
104 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
105 sta_id, priv->stations[sta_id].sta.sta.addr);
106
107 /*
108 * XXX: The MAC address in the command buffer is often changed from
109 * the original sent to the device. That is, the MAC address
110 * written to the command buffer often is not the same MAC adress
111 * read from the command buffer when the command returns. This
112 * issue has not yet been resolved and this debugging is left to
113 * observe the problem.
114 */
115 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
116 priv->stations[sta_id].sta.mode ==
117 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
118 addsta->sta.addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800119 spin_unlock_irqrestore(&priv->sta_lock, flags);
Johannes Berg09034cb2010-06-14 08:32:38 -0700120
121 return ret;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800122}
123
Johannes Berg5696aea2009-07-24 11:13:06 -0700124static void iwl_add_sta_callback(struct iwl_priv *priv,
125 struct iwl_device_cmd *cmd,
Zhu Yi2f301222009-10-09 17:19:45 +0800126 struct iwl_rx_packet *pkt)
Tomas Winkler42132bc2008-05-29 16:35:03 +0800127{
Tomas Winkler3257e5d2008-10-14 12:32:43 -0700128 struct iwl_addsta_cmd *addsta =
129 (struct iwl_addsta_cmd *)cmd->cmd.payload;
Tomas Winkler42132bc2008-05-29 16:35:03 +0800130
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800131 iwl_process_add_sta_resp(priv, addsta, pkt, false);
Tomas Winkler42132bc2008-05-29 16:35:03 +0800132
Tomas Winkler42132bc2008-05-29 16:35:03 +0800133}
134
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800135int iwl_send_add_sta(struct iwl_priv *priv,
Tomas Winkler133636d2008-05-05 10:22:34 +0800136 struct iwl_addsta_cmd *sta, u8 flags)
137{
Zhu Yi2f301222009-10-09 17:19:45 +0800138 struct iwl_rx_packet *pkt = NULL;
Tomas Winkler133636d2008-05-05 10:22:34 +0800139 int ret = 0;
140 u8 data[sizeof(*sta)];
141 struct iwl_host_cmd cmd = {
142 .id = REPLY_ADD_STA,
Johannes Bergc2acea82009-07-24 11:13:05 -0700143 .flags = flags,
Tomas Winkler133636d2008-05-05 10:22:34 +0800144 .data = data,
145 };
Reinette Chatref875f512010-04-05 10:43:10 -0700146 u8 sta_id __maybe_unused = sta->sta.sta_id;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800147
148 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
149 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
Tomas Winkler133636d2008-05-05 10:22:34 +0800150
Tomas Winkler42132bc2008-05-29 16:35:03 +0800151 if (flags & CMD_ASYNC)
Johannes Bergc2acea82009-07-24 11:13:05 -0700152 cmd.callback = iwl_add_sta_callback;
Johannes Berg4620fef2010-06-16 03:30:27 -0700153 else {
Johannes Bergc2acea82009-07-24 11:13:05 -0700154 cmd.flags |= CMD_WANT_SKB;
Johannes Berg4620fef2010-06-16 03:30:27 -0700155 might_sleep();
156 }
Tomas Winkler133636d2008-05-05 10:22:34 +0800157
158 cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
159 ret = iwl_send_cmd(priv, &cmd);
160
161 if (ret || (flags & CMD_ASYNC))
162 return ret;
163
Tomas Winkler133636d2008-05-05 10:22:34 +0800164 if (ret == 0) {
Reinette Chatre1fa97aa2010-01-22 14:22:48 -0800165 pkt = (struct iwl_rx_packet *)cmd.reply_page;
Johannes Berg09034cb2010-06-14 08:32:38 -0700166 ret = iwl_process_add_sta_resp(priv, sta, pkt, true);
Tomas Winkler133636d2008-05-05 10:22:34 +0800167 }
Zhu Yi64a76b52009-12-10 14:37:21 -0800168 iwl_free_pages(priv, cmd.reply_page);
Tomas Winkler133636d2008-05-05 10:22:34 +0800169
170 return ret;
171}
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800172EXPORT_SYMBOL(iwl_send_add_sta);
Tomas Winkler947b13a2008-04-16 16:34:48 -0700173
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800174static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
Johannes Berg7e6a5882010-08-23 10:46:46 +0200175 struct ieee80211_sta *sta,
176 struct iwl_rxon_context *ctx)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800177{
Johannes Berg238d7812010-08-23 10:46:45 +0200178 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800179 __le32 sta_flags;
180 u8 mimo_ps_mode;
181
Johannes Berg238d7812010-08-23 10:46:45 +0200182 if (!sta || !sta_ht_inf->ht_supported)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800183 goto done;
184
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800185 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
Wey-Yi Guy3f3e0372009-10-30 14:36:17 -0700186 IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
187 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
188 "static" :
189 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
190 "dynamic" : "disabled");
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800191
192 sta_flags = priv->stations[index].sta.station_flags;
193
194 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
195
196 switch (mimo_ps_mode) {
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800197 case WLAN_HT_CAP_SM_PS_STATIC:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800198 sta_flags |= STA_FLG_MIMO_DIS_MSK;
199 break;
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800200 case WLAN_HT_CAP_SM_PS_DYNAMIC:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800201 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
202 break;
Tomas Winkler00c5ae22008-09-03 11:26:42 +0800203 case WLAN_HT_CAP_SM_PS_DISABLED:
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800204 break;
205 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800206 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800207 break;
208 }
209
210 sta_flags |= cpu_to_le32(
211 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
212
213 sta_flags |= cpu_to_le32(
214 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
215
Johannes Berg7e6a5882010-08-23 10:46:46 +0200216 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700217 sta_flags |= STA_FLG_HT40_EN_MSK;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800218 else
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700219 sta_flags &= ~STA_FLG_HT40_EN_MSK;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800220
221 priv->stations[index].sta.station_flags = sta_flags;
222 done:
223 return;
224}
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800225
226/**
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800227 * iwl_prep_station - Prepare station information for addition
228 *
229 * should be called with sta_lock held
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800230 */
Johannes Berga194e322010-08-27 08:53:46 -0700231static u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
232 const u8 *addr, bool is_ap,
Johannes Berg238d7812010-08-23 10:46:45 +0200233 struct ieee80211_sta *sta)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800234{
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800235 struct iwl_station_entry *station;
Tomas Winklerc587de02009-06-03 11:44:07 -0700236 int i;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800237 u8 sta_id = IWL_INVALID_STATION;
Tomas Winklerc587de02009-06-03 11:44:07 -0700238 u16 rate;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800239
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800240 if (is_ap)
Johannes Berg2995baf2010-08-23 10:46:42 +0200241 sta_id = ctx->ap_sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800242 else if (is_broadcast_ether_addr(addr))
Johannes Berga194e322010-08-27 08:53:46 -0700243 sta_id = ctx->bcast_sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800244 else
245 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
246 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
247 addr)) {
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800248 sta_id = i;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800249 break;
250 }
251
252 if (!priv->stations[i].used &&
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800253 sta_id == IWL_INVALID_STATION)
254 sta_id = i;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800255 }
256
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800257 /*
258 * These two conditions have the same outcome, but keep them
259 * separate
260 */
261 if (unlikely(sta_id == IWL_INVALID_STATION))
262 return sta_id;
263
264 /*
265 * uCode is not able to deal with multiple requests to add a
266 * station. Keep track if one is in progress so that we do not send
267 * another.
268 */
269 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
270 IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
271 sta_id);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800272 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800273 }
274
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800275 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
276 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800277 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800278 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
279 sta_id, addr);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800280 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800281 }
282
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800283 station = &priv->stations[sta_id];
284 station->used = IWL_STA_DRIVER_ACTIVE;
Tomas Winklere1623442009-01-27 14:27:56 -0800285 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700286 sta_id, addr);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800287 priv->num_stations++;
288
289 /* Set up the REPLY_ADD_STA command to send to device */
290 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
291 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
292 station->sta.mode = 0;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800293 station->sta.sta.sta_id = sta_id;
Johannes Berg770e13b2010-08-23 10:46:44 +0200294 station->sta.station_flags = ctx->station_flags;
Johannes Bergdcef7322010-08-27 08:55:52 -0700295 station->ctxid = ctx->ctxid;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800296
Johannes Berg238d7812010-08-23 10:46:45 +0200297 if (sta) {
298 struct iwl_station_priv_common *sta_priv;
299
300 sta_priv = (void *)sta->drv_priv;
301 sta_priv->ctx = ctx;
302 }
303
Johannes Berg63d41762010-04-30 11:30:50 -0700304 /*
305 * OK to call unconditionally, since local stations (IBSS BSSID
Johannes Berg238d7812010-08-23 10:46:45 +0200306 * STA and broadcast STA) pass in a NULL sta, and mac80211
Johannes Berg63d41762010-04-30 11:30:50 -0700307 * doesn't allow HT IBSS.
308 */
Johannes Berg7e6a5882010-08-23 10:46:46 +0200309 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800310
Tomas Winklerc587de02009-06-03 11:44:07 -0700311 /* 3945 only */
312 rate = (priv->band == IEEE80211_BAND_5GHZ) ?
313 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
314 /* Turn on both antennas for the station... */
315 station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
316
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800317 return sta_id;
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800318
319}
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800320
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800321#define STA_WAIT_TIMEOUT (HZ/2)
322
323/**
324 * iwl_add_station_common -
325 */
Johannes Berga194e322010-08-27 08:53:46 -0700326int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
327 const u8 *addr, bool is_ap,
Johannes Berg238d7812010-08-23 10:46:45 +0200328 struct ieee80211_sta *sta, u8 *sta_id_r)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800329{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800330 unsigned long flags_spin;
331 int ret = 0;
332 u8 sta_id;
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700333 struct iwl_addsta_cmd sta_cmd;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800334
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800335 *sta_id_r = 0;
336 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Johannes Berg238d7812010-08-23 10:46:45 +0200337 sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800338 if (sta_id == IWL_INVALID_STATION) {
339 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
340 addr);
341 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
342 return -EINVAL;
343 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800344
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800345 /*
346 * uCode is not able to deal with multiple requests to add a
347 * station. Keep track if one is in progress so that we do not send
348 * another.
349 */
350 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
351 IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
352 sta_id);
353 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
354 return -EEXIST;
355 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800356
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800357 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
358 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
359 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
360 sta_id, addr);
361 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
362 return -EEXIST;
363 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800364
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800365 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700366 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800367 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
368
369 /* Add station to device's station table */
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700370 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800371 if (ret) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800372 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700373 IWL_ERR(priv, "Adding station %pM failed.\n",
374 priv->stations[sta_id].sta.sta.addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800375 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
376 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
377 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
378 }
379 *sta_id_r = sta_id;
380 return ret;
381}
382EXPORT_SYMBOL(iwl_add_station_common);
383
Johannes Berga6a03452010-04-29 10:41:07 -0700384static struct iwl_link_quality_cmd *iwl_sta_alloc_lq(struct iwl_priv *priv,
385 u8 sta_id)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800386{
387 int i, r;
Reinette Chatred2e210a2010-04-23 10:33:33 -0700388 struct iwl_link_quality_cmd *link_cmd;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800389 u32 rate_flags;
390
Reinette Chatred2e210a2010-04-23 10:33:33 -0700391 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
392 if (!link_cmd) {
393 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
394 return NULL;
395 }
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800396 /* Set up the rate scaling to start at selected rate, fall back
397 * all the way down to 1M in IEEE order, and then spin on 1M */
Johannes Berg156b70d2010-04-28 14:44:45 -0700398 if (priv->band == IEEE80211_BAND_5GHZ)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800399 r = IWL_RATE_6M_INDEX;
400 else
401 r = IWL_RATE_1M_INDEX;
402
403 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
404 rate_flags = 0;
405 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
406 rate_flags |= RATE_MCS_CCK_MSK;
407
408 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
409 RATE_MCS_ANT_POS;
410
Reinette Chatred2e210a2010-04-23 10:33:33 -0700411 link_cmd->rs_table[i].rate_n_flags =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800412 iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
413 r = iwl_get_prev_ieee_rate(r);
414 }
415
Reinette Chatred2e210a2010-04-23 10:33:33 -0700416 link_cmd->general_params.single_stream_ant_msk =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800417 first_antenna(priv->hw_params.valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700418
Reinette Chatred2e210a2010-04-23 10:33:33 -0700419 link_cmd->general_params.dual_stream_ant_msk =
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700420 priv->hw_params.valid_tx_ant &
421 ~first_antenna(priv->hw_params.valid_tx_ant);
Reinette Chatred2e210a2010-04-23 10:33:33 -0700422 if (!link_cmd->general_params.dual_stream_ant_msk) {
423 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700424 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
Reinette Chatred2e210a2010-04-23 10:33:33 -0700425 link_cmd->general_params.dual_stream_ant_msk =
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700426 priv->hw_params.valid_tx_ant;
427 }
428
Reinette Chatred2e210a2010-04-23 10:33:33 -0700429 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
430 link_cmd->agg_params.agg_time_limit =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800431 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
432
Johannes Berg156b70d2010-04-28 14:44:45 -0700433 link_cmd->sta_id = sta_id;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800434
Reinette Chatred2e210a2010-04-23 10:33:33 -0700435 return link_cmd;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800436}
437
438/*
Johannes Berg57f8db82010-04-30 11:30:49 -0700439 * iwl_add_bssid_station - Add the special IBSS BSSID station
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800440 *
441 * Function sleeps.
442 */
Johannes Berga194e322010-08-27 08:53:46 -0700443int iwl_add_bssid_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
444 const u8 *addr, bool init_rs, u8 *sta_id_r)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800445{
446 int ret;
447 u8 sta_id;
Reinette Chatred2e210a2010-04-23 10:33:33 -0700448 struct iwl_link_quality_cmd *link_cmd;
449 unsigned long flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800450
Dan Carpenter96900c72010-05-14 16:53:46 +0200451 if (sta_id_r)
Johannes Bergfd1af152010-04-30 11:30:43 -0700452 *sta_id_r = IWL_INVALID_STATION;
453
Johannes Berga194e322010-08-27 08:53:46 -0700454 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800455 if (ret) {
456 IWL_ERR(priv, "Unable to add station %pM\n", addr);
457 return ret;
458 }
459
Johannes Bergfd1af152010-04-30 11:30:43 -0700460 if (sta_id_r)
461 *sta_id_r = sta_id;
462
Reinette Chatred2e210a2010-04-23 10:33:33 -0700463 spin_lock_irqsave(&priv->sta_lock, flags);
464 priv->stations[sta_id].used |= IWL_STA_LOCAL;
465 spin_unlock_irqrestore(&priv->sta_lock, flags);
466
467 if (init_rs) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800468 /* Set up default rate scaling table in device's station table */
Johannes Berga6a03452010-04-29 10:41:07 -0700469 link_cmd = iwl_sta_alloc_lq(priv, sta_id);
Reinette Chatred2e210a2010-04-23 10:33:33 -0700470 if (!link_cmd) {
471 IWL_ERR(priv, "Unable to initialize rate scaling for station %pM.\n",
472 addr);
473 return -ENOMEM;
474 }
Johannes Berga6a03452010-04-29 10:41:07 -0700475
Johannes Berg7e6a5882010-08-23 10:46:46 +0200476 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
Johannes Berga6a03452010-04-29 10:41:07 -0700477 if (ret)
478 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
479
Reinette Chatred2e210a2010-04-23 10:33:33 -0700480 spin_lock_irqsave(&priv->sta_lock, flags);
481 priv->stations[sta_id].lq = link_cmd;
482 spin_unlock_irqrestore(&priv->sta_lock, flags);
483 }
484
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800485 return 0;
486}
Johannes Berg57f8db82010-04-30 11:30:49 -0700487EXPORT_SYMBOL(iwl_add_bssid_station);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800488
489/**
490 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
491 *
492 * priv->sta_lock must be held
493 */
494static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
495{
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800496 /* Ucode must be active and driver must be non active */
Reinette Chatred2e210a2010-04-23 10:33:33 -0700497 if ((priv->stations[sta_id].used &
498 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) != IWL_STA_UCODE_ACTIVE)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800499 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800500
501 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
502
503 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800504 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800505}
506
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800507static int iwl_send_remove_station(struct iwl_priv *priv,
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700508 const u8 *addr, int sta_id)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800509{
Zhu Yi2f301222009-10-09 17:19:45 +0800510 struct iwl_rx_packet *pkt;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800511 int ret;
512
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800513 unsigned long flags_spin;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800514 struct iwl_rem_sta_cmd rm_sta_cmd;
515
516 struct iwl_host_cmd cmd = {
517 .id = REPLY_REMOVE_STA,
518 .len = sizeof(struct iwl_rem_sta_cmd),
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800519 .flags = CMD_SYNC,
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800520 .data = &rm_sta_cmd,
521 };
522
523 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
524 rm_sta_cmd.num_sta = 1;
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700525 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800526
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800527 cmd.flags |= CMD_WANT_SKB;
528
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800529 ret = iwl_send_cmd(priv, &cmd);
530
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800531 if (ret)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800532 return ret;
533
Zhu Yi2f301222009-10-09 17:19:45 +0800534 pkt = (struct iwl_rx_packet *)cmd.reply_page;
535 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800536 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800537 pkt->hdr.flags);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800538 ret = -EIO;
539 }
540
541 if (!ret) {
Zhu Yi2f301222009-10-09 17:19:45 +0800542 switch (pkt->u.rem_sta.status) {
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800543 case REM_STA_SUCCESS_MSK:
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800544 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700545 iwl_sta_ucode_deactivate(priv, sta_id);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800546 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Tomas Winklere1623442009-01-27 14:27:56 -0800547 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800548 break;
549 default:
550 ret = -EIO;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800551 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800552 break;
553 }
554 }
Zhu Yi64a76b52009-12-10 14:37:21 -0800555 iwl_free_pages(priv, cmd.reply_page);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800556
557 return ret;
558}
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +0800559
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800560/**
561 * iwl_remove_station - Remove driver's knowledge of station.
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800562 */
Johannes Bergfd1af152010-04-30 11:30:43 -0700563int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
564 const u8 *addr)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800565{
Johannes Bergfd1af152010-04-30 11:30:43 -0700566 unsigned long flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800567
568 if (!iwl_is_ready(priv)) {
569 IWL_DEBUG_INFO(priv,
Frans Pop91dd6c22010-03-24 14:19:58 -0700570 "Unable to remove station %pM, device not ready.\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700571 addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800572 /*
573 * It is typical for stations to be removed when we are
574 * going down. Return success since device will be down
575 * soon anyway
576 */
577 return 0;
578 }
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800579
Tomas Winklere1623442009-01-27 14:27:56 -0800580 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
Johannes Bergfd1af152010-04-30 11:30:43 -0700581 sta_id, addr);
582
583 if (WARN_ON(sta_id == IWL_INVALID_STATION))
584 return -EINVAL;
585
586 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800587
588 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800589 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700590 addr);
Johannes Bergfd1af152010-04-30 11:30:43 -0700591 goto out_err;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800592 }
593
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800594 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800595 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700596 addr);
Johannes Bergfd1af152010-04-30 11:30:43 -0700597 goto out_err;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800598 }
599
Johannes Berg1fa61b22010-04-28 08:44:52 -0700600 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
601 kfree(priv->stations[sta_id].lq);
602 priv->stations[sta_id].lq = NULL;
603 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800604
605 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
606
607 priv->num_stations--;
608
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800609 BUG_ON(priv->num_stations < 0);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800610
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800611 spin_unlock_irqrestore(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800612
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700613 return iwl_send_remove_station(priv, addr, sta_id);
Johannes Bergfd1af152010-04-30 11:30:43 -0700614out_err:
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800615 spin_unlock_irqrestore(&priv->sta_lock, flags);
Johannes Bergfd1af152010-04-30 11:30:43 -0700616 return -EINVAL;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800617}
Johannes Berg1fa61b22010-04-28 08:44:52 -0700618EXPORT_SYMBOL_GPL(iwl_remove_station);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800619
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800620/**
Johannes Berg2c810cc2010-04-29 00:53:29 -0700621 * iwl_clear_ucode_stations - clear ucode station table bits
622 *
623 * This function clears all the bits in the driver indicating
624 * which stations are active in the ucode. Call when something
625 * other than explicit station management would cause this in
626 * the ucode, e.g. unassociated RXON.
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800627 */
Johannes Bergdcef7322010-08-27 08:55:52 -0700628void iwl_clear_ucode_stations(struct iwl_priv *priv,
629 struct iwl_rxon_context *ctx)
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800630{
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700631 int i;
Reinette Chatre7e246192010-02-18 22:58:32 -0800632 unsigned long flags_spin;
633 bool cleared = false;
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800634
Johannes Berg2c810cc2010-04-29 00:53:29 -0700635 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700636
Reinette Chatre7e246192010-02-18 22:58:32 -0800637 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Johannes Berg2c810cc2010-04-29 00:53:29 -0700638 for (i = 0; i < priv->hw_params.max_stations; i++) {
Johannes Bergdcef7322010-08-27 08:55:52 -0700639 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
640 continue;
641
Johannes Berg2c810cc2010-04-29 00:53:29 -0700642 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
643 IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d\n", i);
644 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
645 cleared = true;
Reinette Chatre7e246192010-02-18 22:58:32 -0800646 }
647 }
648 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
649
650 if (!cleared)
651 IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800652}
Reinette Chatre7e246192010-02-18 22:58:32 -0800653EXPORT_SYMBOL(iwl_clear_ucode_stations);
654
655/**
656 * iwl_restore_stations() - Restore driver known stations to device
657 *
658 * All stations considered active by driver, but not present in ucode, is
659 * restored.
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800660 *
661 * Function sleeps.
Reinette Chatre7e246192010-02-18 22:58:32 -0800662 */
Johannes Bergdcef7322010-08-27 08:55:52 -0700663void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
Reinette Chatre7e246192010-02-18 22:58:32 -0800664{
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700665 struct iwl_addsta_cmd sta_cmd;
666 struct iwl_link_quality_cmd lq;
Reinette Chatre7e246192010-02-18 22:58:32 -0800667 unsigned long flags_spin;
668 int i;
669 bool found = false;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800670 int ret;
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700671 bool send_lq;
Reinette Chatre7e246192010-02-18 22:58:32 -0800672
673 if (!iwl_is_ready(priv)) {
674 IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
675 return;
676 }
677
678 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
679 spin_lock_irqsave(&priv->sta_lock, flags_spin);
680 for (i = 0; i < priv->hw_params.max_stations; i++) {
Johannes Bergdcef7322010-08-27 08:55:52 -0700681 if (ctx->ctxid != priv->stations[i].ctxid)
682 continue;
Reinette Chatre7e246192010-02-18 22:58:32 -0800683 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
684 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
685 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
686 priv->stations[i].sta.sta.addr);
687 priv->stations[i].sta.mode = 0;
688 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
689 found = true;
690 }
691 }
692
693 for (i = 0; i < priv->hw_params.max_stations; i++) {
694 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700695 memcpy(&sta_cmd, &priv->stations[i].sta,
696 sizeof(struct iwl_addsta_cmd));
697 send_lq = false;
698 if (priv->stations[i].lq) {
699 memcpy(&lq, priv->stations[i].lq,
700 sizeof(struct iwl_link_quality_cmd));
701 send_lq = true;
702 }
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800703 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700704 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800705 if (ret) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800706 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700707 IWL_ERR(priv, "Adding station %pM failed.\n",
708 priv->stations[i].sta.sta.addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800709 priv->stations[i].used &= ~IWL_STA_DRIVER_ACTIVE;
710 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
711 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
712 }
713 /*
714 * Rate scaling has already been initialized, send
715 * current LQ command
716 */
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700717 if (send_lq)
Johannes Berg7e6a5882010-08-23 10:46:46 +0200718 iwl_send_lq_cmd(priv, ctx, &lq, CMD_SYNC, true);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800719 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Reinette Chatre7e246192010-02-18 22:58:32 -0800720 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
721 }
722 }
723
724 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
725 if (!found)
726 IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
727 else
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800728 IWL_DEBUG_INFO(priv, "Restoring all known stations .... complete.\n");
Reinette Chatre7e246192010-02-18 22:58:32 -0800729}
730EXPORT_SYMBOL(iwl_restore_stations);
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800731
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800732int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700733{
734 int i;
735
Johannes Bergc10afb62010-08-23 10:46:43 +0200736 for (i = 0; i < priv->sta_key_max_num; i++)
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700737 if (!test_and_set_bit(i, &priv->ucode_key_table))
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700738 return i;
739
Tomas Winkler40a9a822008-11-25 23:29:03 +0200740 return WEP_INVALID_OFFSET;
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700741}
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800742EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700743
Johannes Bergc10afb62010-08-23 10:46:43 +0200744static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
745 struct iwl_rxon_context *ctx,
746 bool send_if_empty)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700747{
748 int i, not_empty = 0;
749 u8 buff[sizeof(struct iwl_wep_cmd) +
750 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
751 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
752 size_t cmd_size = sizeof(struct iwl_wep_cmd);
753 struct iwl_host_cmd cmd = {
Johannes Bergc10afb62010-08-23 10:46:43 +0200754 .id = ctx->wep_key_cmd,
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700755 .data = wep_cmd,
Johannes Berg72e15d72010-02-19 11:42:32 -0800756 .flags = CMD_SYNC,
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700757 };
758
Johannes Berg72e15d72010-02-19 11:42:32 -0800759 might_sleep();
760
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700761 memset(wep_cmd, 0, cmd_size +
762 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
763
764 for (i = 0; i < WEP_KEYS_MAX ; i++) {
765 wep_cmd->key[i].key_index = i;
Johannes Bergc10afb62010-08-23 10:46:43 +0200766 if (ctx->wep_keys[i].key_size) {
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700767 wep_cmd->key[i].key_offset = i;
768 not_empty = 1;
769 } else {
770 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
771 }
772
Johannes Bergc10afb62010-08-23 10:46:43 +0200773 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
774 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
775 ctx->wep_keys[i].key_size);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700776 }
777
778 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
779 wep_cmd->num_keys = WEP_KEYS_MAX;
780
781 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
782
783 cmd.len = cmd_size;
784
785 if (not_empty || send_if_empty)
786 return iwl_send_cmd(priv, &cmd);
787 else
788 return 0;
789}
Johannes Berg335348b2010-03-30 10:11:46 -0700790
Johannes Bergc10afb62010-08-23 10:46:43 +0200791int iwl_restore_default_wep_keys(struct iwl_priv *priv,
792 struct iwl_rxon_context *ctx)
Johannes Berg335348b2010-03-30 10:11:46 -0700793{
Johannes Berga24d52f2010-08-06 16:17:53 +0200794 lockdep_assert_held(&priv->mutex);
Johannes Berg335348b2010-03-30 10:11:46 -0700795
Johannes Bergc10afb62010-08-23 10:46:43 +0200796 return iwl_send_static_wepkey_cmd(priv, ctx, false);
Johannes Berg335348b2010-03-30 10:11:46 -0700797}
798EXPORT_SYMBOL(iwl_restore_default_wep_keys);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700799
800int iwl_remove_default_wep_key(struct iwl_priv *priv,
Johannes Bergc10afb62010-08-23 10:46:43 +0200801 struct iwl_rxon_context *ctx,
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700802 struct ieee80211_key_conf *keyconf)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700803{
804 int ret;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700805
Johannes Berga24d52f2010-08-06 16:17:53 +0200806 lockdep_assert_held(&priv->mutex);
Johannes Berg72e15d72010-02-19 11:42:32 -0800807
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700808 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
809 keyconf->keyidx);
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700810
Johannes Bergc10afb62010-08-23 10:46:43 +0200811 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700812 if (iwl_is_rfkill(priv)) {
813 IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berg72e15d72010-02-19 11:42:32 -0800814 /* but keys in device are clear anyway so return success */
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700815 return 0;
816 }
Johannes Bergc10afb62010-08-23 10:46:43 +0200817 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
Tomas Winklere1623442009-01-27 14:27:56 -0800818 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800819 keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700820
821 return ret;
822}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800823EXPORT_SYMBOL(iwl_remove_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700824
825int iwl_set_default_wep_key(struct iwl_priv *priv,
Johannes Berg2995baf2010-08-23 10:46:42 +0200826 struct iwl_rxon_context *ctx,
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700827 struct ieee80211_key_conf *keyconf)
828{
829 int ret;
Johannes Berg72e15d72010-02-19 11:42:32 -0800830
Johannes Berga24d52f2010-08-06 16:17:53 +0200831 lockdep_assert_held(&priv->mutex);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700832
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800833 if (keyconf->keylen != WEP_KEY_LEN_128 &&
834 keyconf->keylen != WEP_KEY_LEN_64) {
Tomas Winklere1623442009-01-27 14:27:56 -0800835 IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800836 return -EINVAL;
837 }
838
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700839 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800840 keyconf->hw_key_idx = HW_KEY_DEFAULT;
Johannes Berg2995baf2010-08-23 10:46:42 +0200841 priv->stations[ctx->ap_sta_id].keyinfo.cipher = keyconf->cipher;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700842
Johannes Bergc10afb62010-08-23 10:46:43 +0200843 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
844 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700845 keyconf->keylen);
846
Johannes Bergc10afb62010-08-23 10:46:43 +0200847 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
Tomas Winklere1623442009-01-27 14:27:56 -0800848 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800849 keyconf->keylen, keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700850
851 return ret;
852}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800853EXPORT_SYMBOL(iwl_set_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700854
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700855static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
Johannes Berga194e322010-08-27 08:53:46 -0700856 struct iwl_rxon_context *ctx,
857 struct ieee80211_key_conf *keyconf,
858 u8 sta_id)
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700859{
860 unsigned long flags;
861 __le16 key_flags = 0;
Johannes Berge6666742010-06-14 08:32:24 -0700862 struct iwl_addsta_cmd sta_cmd;
863
864 lockdep_assert_held(&priv->mutex);
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700865
866 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700867
868 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
869 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
870 key_flags &= ~STA_KEY_FLG_INVALID;
871
872 if (keyconf->keylen == WEP_KEY_LEN_128)
873 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
874
Johannes Berga194e322010-08-27 08:53:46 -0700875 if (sta_id == ctx->bcast_sta_id)
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700876 key_flags |= STA_KEY_MULTICAST_MSK;
877
878 spin_lock_irqsave(&priv->sta_lock, flags);
879
Johannes Berg97359d12010-08-10 09:46:38 +0200880 priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700881 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
882 priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
883
884 memcpy(priv->stations[sta_id].keyinfo.key,
885 keyconf->key, keyconf->keylen);
886
887 memcpy(&priv->stations[sta_id].sta.key.key[3],
888 keyconf->key, keyconf->keylen);
889
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700890 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
891 == STA_KEY_FLG_NO_ENC)
892 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700893 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700894 /* else, we are overriding an existing key => no need to allocated room
895 * in uCode. */
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700896
Tomas Winkler40a9a822008-11-25 23:29:03 +0200897 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100898 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200899
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700900 priv->stations[sta_id].sta.key.key_flags = key_flags;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700901 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
902 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
903
Johannes Berge6666742010-06-14 08:32:24 -0700904 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700905 spin_unlock_irqrestore(&priv->sta_lock, flags);
906
Johannes Berge6666742010-06-14 08:32:24 -0700907 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700908}
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700909
910static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
Johannes Berga194e322010-08-27 08:53:46 -0700911 struct iwl_rxon_context *ctx,
912 struct ieee80211_key_conf *keyconf,
913 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700914{
915 unsigned long flags;
916 __le16 key_flags = 0;
Johannes Berge6666742010-06-14 08:32:24 -0700917 struct iwl_addsta_cmd sta_cmd;
918
919 lockdep_assert_held(&priv->mutex);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700920
921 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
922 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
923 key_flags &= ~STA_KEY_FLG_INVALID;
924
Johannes Berga194e322010-08-27 08:53:46 -0700925 if (sta_id == ctx->bcast_sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700926 key_flags |= STA_KEY_MULTICAST_MSK;
927
928 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700929
930 spin_lock_irqsave(&priv->sta_lock, flags);
Johannes Berg97359d12010-08-10 09:46:38 +0200931 priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700932 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
933
934 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
935 keyconf->keylen);
936
937 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
938 keyconf->keylen);
939
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700940 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
941 == STA_KEY_FLG_NO_ENC)
942 priv->stations[sta_id].sta.key.key_offset =
943 iwl_get_free_ucode_key_index(priv);
944 /* else, we are overriding an existing key => no need to allocated room
945 * in uCode. */
946
Tomas Winkler40a9a822008-11-25 23:29:03 +0200947 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100948 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200949
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700950 priv->stations[sta_id].sta.key.key_flags = key_flags;
951 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
952 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
953
Johannes Berge6666742010-06-14 08:32:24 -0700954 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700955 spin_unlock_irqrestore(&priv->sta_lock, flags);
956
Johannes Berge6666742010-06-14 08:32:24 -0700957 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700958}
959
960static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
Johannes Berga194e322010-08-27 08:53:46 -0700961 struct iwl_rxon_context *ctx,
962 struct ieee80211_key_conf *keyconf,
963 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700964{
965 unsigned long flags;
966 int ret = 0;
Reinette Chatre299f5462009-04-30 13:56:31 -0700967 __le16 key_flags = 0;
968
969 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
970 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
971 key_flags &= ~STA_KEY_FLG_INVALID;
972
Johannes Berga194e322010-08-27 08:53:46 -0700973 if (sta_id == ctx->bcast_sta_id)
Reinette Chatre299f5462009-04-30 13:56:31 -0700974 key_flags |= STA_KEY_MULTICAST_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700975
976 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
977 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700978
979 spin_lock_irqsave(&priv->sta_lock, flags);
980
Johannes Berg97359d12010-08-10 09:46:38 +0200981 priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700982 priv->stations[sta_id].keyinfo.keylen = 16;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700983
984 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
985 == STA_KEY_FLG_NO_ENC)
986 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700987 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700988 /* else, we are overriding an existing key => no need to allocated room
989 * in uCode. */
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700990
Tomas Winkler40a9a822008-11-25 23:29:03 +0200991 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100992 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200993
Reinette Chatre299f5462009-04-30 13:56:31 -0700994 priv->stations[sta_id].sta.key.key_flags = key_flags;
995
996
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700997 /* This copy is acutally not needed: we get the key with each TX */
998 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
999
1000 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
1001
1002 spin_unlock_irqrestore(&priv->sta_lock, flags);
1003
1004 return ret;
1005}
1006
Tomas Winkler9f586712008-11-12 13:14:05 -08001007void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berga194e322010-08-27 08:53:46 -07001008 struct iwl_rxon_context *ctx,
1009 struct ieee80211_key_conf *keyconf,
1010 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
Tomas Winkler9f586712008-11-12 13:14:05 -08001011{
Johannes Bergbdbb6122010-04-30 13:53:37 -07001012 u8 sta_id;
Tomas Winkler9f586712008-11-12 13:14:05 -08001013 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -08001014 int i;
Tomas Winkler9f586712008-11-12 13:14:05 -08001015
Tomas Winkler9f586712008-11-12 13:14:05 -08001016 if (iwl_scan_cancel(priv)) {
1017 /* cancel scan failed, just live w/ bad key and rely
1018 briefly on SW decryption */
1019 return;
1020 }
1021
Johannes Berga194e322010-08-27 08:53:46 -07001022 sta_id = iwl_sta_id_or_broadcast(priv, ctx, sta);
Johannes Berg0af8bca2010-04-30 14:08:00 -07001023 if (sta_id == IWL_INVALID_STATION)
1024 return;
1025
Tomas Winkler9f586712008-11-12 13:14:05 -08001026 spin_lock_irqsave(&priv->sta_lock, flags);
1027
Tomas Winkler9f586712008-11-12 13:14:05 -08001028 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
1029
1030 for (i = 0; i < 5; i++)
1031 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
1032 cpu_to_le16(phase1key[i]);
1033
1034 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1035 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1036
1037 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1038
1039 spin_unlock_irqrestore(&priv->sta_lock, flags);
1040
1041}
1042EXPORT_SYMBOL(iwl_update_tkip_key);
1043
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001044int iwl_remove_dynamic_key(struct iwl_priv *priv,
Johannes Bergc10afb62010-08-23 10:46:43 +02001045 struct iwl_rxon_context *ctx,
1046 struct ieee80211_key_conf *keyconf,
1047 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001048{
1049 unsigned long flags;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001050 u16 key_flags;
1051 u8 keyidx;
Johannes Berge6666742010-06-14 08:32:24 -07001052 struct iwl_addsta_cmd sta_cmd;
1053
1054 lockdep_assert_held(&priv->mutex);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001055
Johannes Bergc10afb62010-08-23 10:46:43 +02001056 ctx->key_mapping_keys--;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001057
1058 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001059 key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
1060 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
1061
Tomas Winklere1623442009-01-27 14:27:56 -08001062 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +08001063 keyconf->keyidx, sta_id);
1064
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001065 if (keyconf->keyidx != keyidx) {
1066 /* We need to remove a key with index different that the one
1067 * in the uCode. This means that the key we need to remove has
1068 * been replaced by another one with different index.
1069 * Don't do anything and return ok
1070 */
1071 spin_unlock_irqrestore(&priv->sta_lock, flags);
1072 return 0;
1073 }
1074
Tomas Winkler40a9a822008-11-25 23:29:03 +02001075 if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001076 IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
Tomas Winkler40a9a822008-11-25 23:29:03 +02001077 keyconf->keyidx, key_flags);
1078 spin_unlock_irqrestore(&priv->sta_lock, flags);
1079 return 0;
1080 }
1081
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001082 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
1083 &priv->ucode_key_table))
Winkler, Tomas15b16872008-12-19 10:37:33 +08001084 IWL_ERR(priv, "index %d not used in uCode key table.\n",
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001085 priv->stations[sta_id].sta.key.key_offset);
1086 memset(&priv->stations[sta_id].keyinfo, 0,
Tomas Winkler6def9762008-05-05 10:22:31 +08001087 sizeof(struct iwl_hw_key));
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001088 memset(&priv->stations[sta_id].sta.key, 0,
1089 sizeof(struct iwl4965_keyinfo));
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001090 priv->stations[sta_id].sta.key.key_flags =
1091 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
1092 priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001093 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1094 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001095
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -07001096 if (iwl_is_rfkill(priv)) {
Frans Pop91dd6c22010-03-24 14:19:58 -07001097 IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -07001098 spin_unlock_irqrestore(&priv->sta_lock, flags);
1099 return 0;
1100 }
Johannes Berge6666742010-06-14 08:32:24 -07001101 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001102 spin_unlock_irqrestore(&priv->sta_lock, flags);
Johannes Berge6666742010-06-14 08:32:24 -07001103
1104 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001105}
Tomas Winkler27aaba02008-05-05 10:22:44 +08001106EXPORT_SYMBOL(iwl_remove_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001107
Johannes Berga194e322010-08-27 08:53:46 -07001108int iwl_set_dynamic_key(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
1109 struct ieee80211_key_conf *keyconf, u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001110{
1111 int ret;
1112
Johannes Berge6666742010-06-14 08:32:24 -07001113 lockdep_assert_held(&priv->mutex);
1114
Johannes Bergc10afb62010-08-23 10:46:43 +02001115 ctx->key_mapping_keys++;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001116 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001117
Johannes Berg97359d12010-08-10 09:46:38 +02001118 switch (keyconf->cipher) {
1119 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berga194e322010-08-27 08:53:46 -07001120 ret = iwl_set_ccmp_dynamic_key_info(priv, ctx, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001121 break;
Johannes Berg97359d12010-08-10 09:46:38 +02001122 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berga194e322010-08-27 08:53:46 -07001123 ret = iwl_set_tkip_dynamic_key_info(priv, ctx, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001124 break;
Johannes Berg97359d12010-08-10 09:46:38 +02001125 case WLAN_CIPHER_SUITE_WEP40:
1126 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga194e322010-08-27 08:53:46 -07001127 ret = iwl_set_wep_dynamic_key_info(priv, ctx, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001128 break;
1129 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001130 IWL_ERR(priv,
Johannes Berg97359d12010-08-10 09:46:38 +02001131 "Unknown alg: %s cipher = %x\n", __func__,
1132 keyconf->cipher);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001133 ret = -EINVAL;
1134 }
1135
Johannes Berg97359d12010-08-10 09:46:38 +02001136 IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
1137 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +08001138 sta_id, ret);
1139
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001140 return ret;
1141}
Tomas Winkler27aaba02008-05-05 10:22:44 +08001142EXPORT_SYMBOL(iwl_set_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001143
Tomas Winkler66c73db2008-04-15 16:01:40 -07001144#ifdef CONFIG_IWLWIFI_DEBUG
1145static void iwl_dump_lq_cmd(struct iwl_priv *priv,
1146 struct iwl_link_quality_cmd *lq)
1147{
1148 int i;
Tomas Winklere1623442009-01-27 14:27:56 -08001149 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
1150 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001151 lq->general_params.single_stream_ant_msk,
1152 lq->general_params.dual_stream_ant_msk);
1153
1154 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
Tomas Winklere1623442009-01-27 14:27:56 -08001155 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001156 i, lq->rs_table[i].rate_n_flags);
1157}
1158#else
1159static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
1160 struct iwl_link_quality_cmd *lq)
1161{
1162}
1163#endif
1164
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001165/**
Reinette Chatre3bce6062010-04-13 16:11:19 -07001166 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
1167 *
1168 * It sometimes happens when a HT rate has been in use and we
1169 * loose connectivity with AP then mac80211 will first tell us that the
1170 * current channel is not HT anymore before removing the station. In such a
1171 * scenario the RXON flags will be updated to indicate we are not
1172 * communicating HT anymore, but the LQ command may still contain HT rates.
1173 * Test for this to prevent driver from sending LQ command between the time
1174 * RXON flags are updated and when LQ command is updated.
1175 */
1176static bool is_lq_table_valid(struct iwl_priv *priv,
Johannes Berg7e6a5882010-08-23 10:46:46 +02001177 struct iwl_rxon_context *ctx,
Reinette Chatre3bce6062010-04-13 16:11:19 -07001178 struct iwl_link_quality_cmd *lq)
1179{
1180 int i;
Reinette Chatre3bce6062010-04-13 16:11:19 -07001181
Johannes Berg7e6a5882010-08-23 10:46:46 +02001182 if (ctx->ht.enabled)
Reinette Chatre3bce6062010-04-13 16:11:19 -07001183 return true;
1184
1185 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
Johannes Berg246ed352010-08-23 10:46:32 +02001186 ctx->active.channel);
Reinette Chatre3bce6062010-04-13 16:11:19 -07001187 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
1188 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) & RATE_MCS_HT_MSK) {
1189 IWL_DEBUG_INFO(priv,
1190 "index %d of LQ expects HT channel\n",
1191 i);
1192 return false;
1193 }
1194 }
1195 return true;
1196}
1197
1198/**
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001199 * iwl_send_lq_cmd() - Send link quality command
1200 * @init: This command is sent as part of station initialization right
1201 * after station has been added.
1202 *
1203 * The link quality command is sent as the last step of station creation.
1204 * This is the special case in which init is set and we call a callback in
1205 * this case to clear the state indicating that station creation is in
1206 * progress.
1207 */
Johannes Berg7e6a5882010-08-23 10:46:46 +02001208int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001209 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
Tomas Winkler66c73db2008-04-15 16:01:40 -07001210{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001211 int ret = 0;
1212 unsigned long flags_spin;
1213
Tomas Winkler66c73db2008-04-15 16:01:40 -07001214 struct iwl_host_cmd cmd = {
1215 .id = REPLY_TX_LINK_QUALITY_CMD,
1216 .len = sizeof(struct iwl_link_quality_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07001217 .flags = flags,
Tomas Winkler66c73db2008-04-15 16:01:40 -07001218 .data = lq,
1219 };
1220
Johannes Berg76c3c692010-03-30 02:46:29 -07001221 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
Tomas Winkler66c73db2008-04-15 16:01:40 -07001222 return -EINVAL;
1223
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001224 iwl_dump_lq_cmd(priv, lq);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001225 BUG_ON(init && (cmd.flags & CMD_ASYNC));
Tomas Winkler66c73db2008-04-15 16:01:40 -07001226
Johannes Berg7e6a5882010-08-23 10:46:46 +02001227 if (is_lq_table_valid(priv, ctx, lq))
Reinette Chatre3bce6062010-04-13 16:11:19 -07001228 ret = iwl_send_cmd(priv, &cmd);
1229 else
1230 ret = -EINVAL;
Reinette Chatred2e210a2010-04-23 10:33:33 -07001231
1232 if (cmd.flags & CMD_ASYNC)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001233 return ret;
Tomas Winkler66c73db2008-04-15 16:01:40 -07001234
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001235 if (init) {
Frans Pop91dd6c22010-03-24 14:19:58 -07001236 IWL_DEBUG_INFO(priv, "init LQ command complete, clearing sta addition status for sta %d\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001237 lq->sta_id);
1238 spin_lock_irqsave(&priv->sta_lock, flags_spin);
1239 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
1240 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
1241 }
Reinette Chatred2e210a2010-04-23 10:33:33 -07001242 return ret;
Tomas Winkler66c73db2008-04-15 16:01:40 -07001243}
1244EXPORT_SYMBOL(iwl_send_lq_cmd);
1245
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001246/**
Johannes Berg2c810cc2010-04-29 00:53:29 -07001247 * iwl_alloc_bcast_station - add broadcast station into driver's station table.
1248 *
1249 * This adds the broadcast station into the driver's station table
1250 * and marks it driver active, so that it will be restored to the
1251 * device at the next best time.
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001252 */
Johannes Berga194e322010-08-27 08:53:46 -07001253int iwl_alloc_bcast_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
1254 bool init_lq)
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001255{
Johannes Berg2c810cc2010-04-29 00:53:29 -07001256 struct iwl_link_quality_cmd *link_cmd;
1257 unsigned long flags;
1258 u8 sta_id;
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001259
Johannes Berg2c810cc2010-04-29 00:53:29 -07001260 spin_lock_irqsave(&priv->sta_lock, flags);
Johannes Berga194e322010-08-27 08:53:46 -07001261 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
Johannes Berg2c810cc2010-04-29 00:53:29 -07001262 if (sta_id == IWL_INVALID_STATION) {
1263 IWL_ERR(priv, "Unable to prepare broadcast station\n");
1264 spin_unlock_irqrestore(&priv->sta_lock, flags);
1265
1266 return -EINVAL;
1267 }
1268
1269 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1270 priv->stations[sta_id].used |= IWL_STA_BCAST;
1271 spin_unlock_irqrestore(&priv->sta_lock, flags);
1272
1273 if (init_lq) {
1274 link_cmd = iwl_sta_alloc_lq(priv, sta_id);
1275 if (!link_cmd) {
1276 IWL_ERR(priv,
1277 "Unable to initialize rate scaling for bcast station.\n");
1278 return -ENOMEM;
1279 }
1280
1281 spin_lock_irqsave(&priv->sta_lock, flags);
1282 priv->stations[sta_id].lq = link_cmd;
1283 spin_unlock_irqrestore(&priv->sta_lock, flags);
1284 }
1285
1286 return 0;
1287}
1288EXPORT_SYMBOL_GPL(iwl_alloc_bcast_station);
1289
Daniel Halperin278c2f62010-06-14 13:10:29 -07001290/**
1291 * iwl_update_bcast_station - update broadcast station's LQ command
1292 *
1293 * Only used by iwlagn. Placed here to have all bcast station management
1294 * code together.
1295 */
Johannes Berga194e322010-08-27 08:53:46 -07001296static int iwl_update_bcast_station(struct iwl_priv *priv,
1297 struct iwl_rxon_context *ctx)
Daniel Halperin278c2f62010-06-14 13:10:29 -07001298{
1299 unsigned long flags;
1300 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga194e322010-08-27 08:53:46 -07001301 u8 sta_id = ctx->bcast_sta_id;
Daniel Halperin278c2f62010-06-14 13:10:29 -07001302
1303 link_cmd = iwl_sta_alloc_lq(priv, sta_id);
1304 if (!link_cmd) {
1305 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1306 return -ENOMEM;
1307 }
1308
1309 spin_lock_irqsave(&priv->sta_lock, flags);
1310 if (priv->stations[sta_id].lq)
1311 kfree(priv->stations[sta_id].lq);
1312 else
1313 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1314 priv->stations[sta_id].lq = link_cmd;
1315 spin_unlock_irqrestore(&priv->sta_lock, flags);
1316
1317 return 0;
1318}
Daniel Halperin278c2f62010-06-14 13:10:29 -07001319
Johannes Berga194e322010-08-27 08:53:46 -07001320int iwl_update_bcast_stations(struct iwl_priv *priv)
1321{
1322 struct iwl_rxon_context *ctx;
1323 int ret = 0;
1324
1325 for_each_context(priv, ctx) {
1326 ret = iwl_update_bcast_station(priv, ctx);
1327 if (ret)
1328 break;
1329 }
1330
1331 return ret;
1332}
1333EXPORT_SYMBOL_GPL(iwl_update_bcast_stations);
1334
1335void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
Reinette Chatre3459ab52010-01-22 14:22:49 -08001336{
Johannes Berg2c810cc2010-04-29 00:53:29 -07001337 unsigned long flags;
1338 int i;
Reinette Chatred2e210a2010-04-23 10:33:33 -07001339
Johannes Berg2c810cc2010-04-29 00:53:29 -07001340 spin_lock_irqsave(&priv->sta_lock, flags);
1341 for (i = 0; i < priv->hw_params.max_stations; i++) {
1342 if (!(priv->stations[i].used & IWL_STA_BCAST))
1343 continue;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001344
Johannes Berg2c810cc2010-04-29 00:53:29 -07001345 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
1346 priv->num_stations--;
1347 BUG_ON(priv->num_stations < 0);
1348 kfree(priv->stations[i].lq);
1349 priv->stations[i].lq = NULL;
1350 }
1351 spin_unlock_irqrestore(&priv->sta_lock, flags);
Reinette Chatre3459ab52010-01-22 14:22:49 -08001352}
Johannes Berga194e322010-08-27 08:53:46 -07001353EXPORT_SYMBOL_GPL(iwl_dealloc_bcast_stations);
Reinette Chatre3459ab52010-01-22 14:22:49 -08001354
1355/**
Tomas Winkler9f586712008-11-12 13:14:05 -08001356 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
Tomas Winkler5083e562008-05-29 16:35:15 +08001357 */
Johannes Berg4620fef2010-06-16 03:30:27 -07001358int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
Tomas Winkler5083e562008-05-29 16:35:15 +08001359{
1360 unsigned long flags;
Johannes Berg4620fef2010-06-16 03:30:27 -07001361 struct iwl_addsta_cmd sta_cmd;
1362
1363 lockdep_assert_held(&priv->mutex);
Tomas Winkler5083e562008-05-29 16:35:15 +08001364
1365 /* Remove "disable" flag, to enable Tx for this TID */
1366 spin_lock_irqsave(&priv->sta_lock, flags);
1367 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1368 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1369 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berg4620fef2010-06-16 03:30:27 -07001370 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Reinette Chatre9c5ac092010-05-05 02:26:06 -07001371 spin_unlock_irqrestore(&priv->sta_lock, flags);
Johannes Berg4620fef2010-06-16 03:30:27 -07001372
1373 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Tomas Winkler5083e562008-05-29 16:35:15 +08001374}
Tomas Winkler9f586712008-11-12 13:14:05 -08001375EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
1376
Johannes Berg619753f2010-04-30 11:30:46 -07001377int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1378 int tid, u16 ssn)
Tomas Winkler9f586712008-11-12 13:14:05 -08001379{
1380 unsigned long flags;
1381 int sta_id;
Johannes Berg4620fef2010-06-16 03:30:27 -07001382 struct iwl_addsta_cmd sta_cmd;
1383
1384 lockdep_assert_held(&priv->mutex);
Tomas Winkler9f586712008-11-12 13:14:05 -08001385
Johannes Berg619753f2010-04-30 11:30:46 -07001386 sta_id = iwl_sta_id(sta);
Tomas Winkler9f586712008-11-12 13:14:05 -08001387 if (sta_id == IWL_INVALID_STATION)
1388 return -ENXIO;
1389
1390 spin_lock_irqsave(&priv->sta_lock, flags);
1391 priv->stations[sta_id].sta.station_flags_msk = 0;
1392 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1393 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1394 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1395 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berg4620fef2010-06-16 03:30:27 -07001396 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Tomas Winkler9f586712008-11-12 13:14:05 -08001397 spin_unlock_irqrestore(&priv->sta_lock, flags);
1398
Johannes Berg4620fef2010-06-16 03:30:27 -07001399 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001400}
1401EXPORT_SYMBOL(iwl_sta_rx_agg_start);
1402
Johannes Berg619753f2010-04-30 11:30:46 -07001403int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1404 int tid)
Tomas Winkler9f586712008-11-12 13:14:05 -08001405{
1406 unsigned long flags;
Johannes Berg4620fef2010-06-16 03:30:27 -07001407 int sta_id;
1408 struct iwl_addsta_cmd sta_cmd;
1409
1410 lockdep_assert_held(&priv->mutex);
Tomas Winkler9f586712008-11-12 13:14:05 -08001411
Johannes Berg619753f2010-04-30 11:30:46 -07001412 sta_id = iwl_sta_id(sta);
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001413 if (sta_id == IWL_INVALID_STATION) {
1414 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
Tomas Winkler9f586712008-11-12 13:14:05 -08001415 return -ENXIO;
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001416 }
Tomas Winkler9f586712008-11-12 13:14:05 -08001417
1418 spin_lock_irqsave(&priv->sta_lock, flags);
1419 priv->stations[sta_id].sta.station_flags_msk = 0;
1420 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1421 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1422 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berg4620fef2010-06-16 03:30:27 -07001423 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Tomas Winkler9f586712008-11-12 13:14:05 -08001424 spin_unlock_irqrestore(&priv->sta_lock, flags);
1425
Johannes Berg4620fef2010-06-16 03:30:27 -07001426 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001427}
1428EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
1429
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001430void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
Tomas Winkler9f586712008-11-12 13:14:05 -08001431{
1432 unsigned long flags;
1433
1434 spin_lock_irqsave(&priv->sta_lock, flags);
1435 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1436 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1437 priv->stations[sta_id].sta.sta.modify_mask = 0;
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001438 priv->stations[sta_id].sta.sleep_tx_count = 0;
Tomas Winkler9f586712008-11-12 13:14:05 -08001439 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Reinette Chatre9c5ac092010-05-05 02:26:06 -07001440 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001441 spin_unlock_irqrestore(&priv->sta_lock, flags);
1442
Tomas Winkler9f586712008-11-12 13:14:05 -08001443}
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001444EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
Tomas Winkler9f586712008-11-12 13:14:05 -08001445
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001446void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
Tomas Winkler9f586712008-11-12 13:14:05 -08001447{
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001448 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -08001449
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001450 spin_lock_irqsave(&priv->sta_lock, flags);
1451 priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
1452 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1453 priv->stations[sta_id].sta.sta.modify_mask =
1454 STA_MODIFY_SLEEP_TX_COUNT_MSK;
1455 priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
1456 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Reinette Chatre9c5ac092010-05-05 02:26:06 -07001457 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001458 spin_unlock_irqrestore(&priv->sta_lock, flags);
Tomas Winkler9f586712008-11-12 13:14:05 -08001459
Tomas Winkler9f586712008-11-12 13:14:05 -08001460}
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -07001461EXPORT_SYMBOL(iwl_sta_modify_sleep_tx_count);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001462
1463int iwl_mac_sta_remove(struct ieee80211_hw *hw,
Johannes Bergfd1af152010-04-30 11:30:43 -07001464 struct ieee80211_vif *vif,
1465 struct ieee80211_sta *sta)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001466{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001467 struct iwl_priv *priv = hw->priv;
Johannes Bergfd1af152010-04-30 11:30:43 -07001468 struct iwl_station_priv_common *sta_common = (void *)sta->drv_priv;
1469 int ret;
1470
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001471 IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
1472 sta->addr);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07001473 mutex_lock(&priv->mutex);
1474 IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n",
1475 sta->addr);
Johannes Bergfd1af152010-04-30 11:30:43 -07001476 ret = iwl_remove_station(priv, sta_common->sta_id, sta->addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001477 if (ret)
1478 IWL_ERR(priv, "Error removing station %pM\n",
1479 sta->addr);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07001480 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001481 return ret;
1482}
1483EXPORT_SYMBOL(iwl_mac_sta_remove);