blob: 6edd0341dfe2b1572ab0e888c482e3855e8ee8d0 [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;
Tomas Winklercf6da942010-08-23 14:00:54 -0700389 u32 rate_flags = 0;
390 __le32 rate_n_flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800391
Reinette Chatred2e210a2010-04-23 10:33:33 -0700392 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
393 if (!link_cmd) {
394 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
395 return NULL;
396 }
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800397 /* Set up the rate scaling to start at selected rate, fall back
398 * all the way down to 1M in IEEE order, and then spin on 1M */
Johannes Berg156b70d2010-04-28 14:44:45 -0700399 if (priv->band == IEEE80211_BAND_5GHZ)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800400 r = IWL_RATE_6M_INDEX;
401 else
402 r = IWL_RATE_1M_INDEX;
403
Tomas Winklercf6da942010-08-23 14:00:54 -0700404 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
405 rate_flags |= RATE_MCS_CCK_MSK;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800406
Tomas Winklercf6da942010-08-23 14:00:54 -0700407 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800408 RATE_MCS_ANT_POS;
Tomas Winklercf6da942010-08-23 14:00:54 -0700409 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
410 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
411 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800412
Reinette Chatred2e210a2010-04-23 10:33:33 -0700413 link_cmd->general_params.single_stream_ant_msk =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800414 first_antenna(priv->hw_params.valid_tx_ant);
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700415
Reinette Chatred2e210a2010-04-23 10:33:33 -0700416 link_cmd->general_params.dual_stream_ant_msk =
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700417 priv->hw_params.valid_tx_ant &
418 ~first_antenna(priv->hw_params.valid_tx_ant);
Reinette Chatred2e210a2010-04-23 10:33:33 -0700419 if (!link_cmd->general_params.dual_stream_ant_msk) {
420 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700421 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
Reinette Chatred2e210a2010-04-23 10:33:33 -0700422 link_cmd->general_params.dual_stream_ant_msk =
Wey-Yi Guy3a23d692010-04-03 16:44:39 -0700423 priv->hw_params.valid_tx_ant;
424 }
425
Reinette Chatred2e210a2010-04-23 10:33:33 -0700426 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
427 link_cmd->agg_params.agg_time_limit =
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800428 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
429
Johannes Berg156b70d2010-04-28 14:44:45 -0700430 link_cmd->sta_id = sta_id;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800431
Reinette Chatred2e210a2010-04-23 10:33:33 -0700432 return link_cmd;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800433}
434
435/*
Johannes Berg57f8db82010-04-30 11:30:49 -0700436 * iwl_add_bssid_station - Add the special IBSS BSSID station
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800437 *
438 * Function sleeps.
439 */
Johannes Berga194e322010-08-27 08:53:46 -0700440int iwl_add_bssid_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
441 const u8 *addr, bool init_rs, u8 *sta_id_r)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800442{
443 int ret;
444 u8 sta_id;
Reinette Chatred2e210a2010-04-23 10:33:33 -0700445 struct iwl_link_quality_cmd *link_cmd;
446 unsigned long flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800447
Dan Carpenter96900c72010-05-14 16:53:46 +0200448 if (sta_id_r)
Johannes Bergfd1af152010-04-30 11:30:43 -0700449 *sta_id_r = IWL_INVALID_STATION;
450
Johannes Berga194e322010-08-27 08:53:46 -0700451 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800452 if (ret) {
453 IWL_ERR(priv, "Unable to add station %pM\n", addr);
454 return ret;
455 }
456
Johannes Bergfd1af152010-04-30 11:30:43 -0700457 if (sta_id_r)
458 *sta_id_r = sta_id;
459
Reinette Chatred2e210a2010-04-23 10:33:33 -0700460 spin_lock_irqsave(&priv->sta_lock, flags);
461 priv->stations[sta_id].used |= IWL_STA_LOCAL;
462 spin_unlock_irqrestore(&priv->sta_lock, flags);
463
464 if (init_rs) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800465 /* Set up default rate scaling table in device's station table */
Johannes Berga6a03452010-04-29 10:41:07 -0700466 link_cmd = iwl_sta_alloc_lq(priv, sta_id);
Reinette Chatred2e210a2010-04-23 10:33:33 -0700467 if (!link_cmd) {
468 IWL_ERR(priv, "Unable to initialize rate scaling for station %pM.\n",
469 addr);
470 return -ENOMEM;
471 }
Johannes Berga6a03452010-04-29 10:41:07 -0700472
Johannes Berg7e6a5882010-08-23 10:46:46 +0200473 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
Johannes Berga6a03452010-04-29 10:41:07 -0700474 if (ret)
475 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
476
Reinette Chatred2e210a2010-04-23 10:33:33 -0700477 spin_lock_irqsave(&priv->sta_lock, flags);
478 priv->stations[sta_id].lq = link_cmd;
479 spin_unlock_irqrestore(&priv->sta_lock, flags);
480 }
481
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800482 return 0;
483}
Johannes Berg57f8db82010-04-30 11:30:49 -0700484EXPORT_SYMBOL(iwl_add_bssid_station);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800485
486/**
487 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
488 *
489 * priv->sta_lock must be held
490 */
491static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
492{
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800493 /* Ucode must be active and driver must be non active */
Reinette Chatred2e210a2010-04-23 10:33:33 -0700494 if ((priv->stations[sta_id].used &
495 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) != IWL_STA_UCODE_ACTIVE)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800496 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800497
498 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
499
500 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800501 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800502}
503
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800504static int iwl_send_remove_station(struct iwl_priv *priv,
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700505 const u8 *addr, int sta_id)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800506{
Zhu Yi2f301222009-10-09 17:19:45 +0800507 struct iwl_rx_packet *pkt;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800508 int ret;
509
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800510 unsigned long flags_spin;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800511 struct iwl_rem_sta_cmd rm_sta_cmd;
512
513 struct iwl_host_cmd cmd = {
514 .id = REPLY_REMOVE_STA,
515 .len = sizeof(struct iwl_rem_sta_cmd),
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800516 .flags = CMD_SYNC,
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800517 .data = &rm_sta_cmd,
518 };
519
520 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
521 rm_sta_cmd.num_sta = 1;
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700522 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800523
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800524 cmd.flags |= CMD_WANT_SKB;
525
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800526 ret = iwl_send_cmd(priv, &cmd);
527
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800528 if (ret)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800529 return ret;
530
Zhu Yi2f301222009-10-09 17:19:45 +0800531 pkt = (struct iwl_rx_packet *)cmd.reply_page;
532 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800533 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800534 pkt->hdr.flags);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800535 ret = -EIO;
536 }
537
538 if (!ret) {
Zhu Yi2f301222009-10-09 17:19:45 +0800539 switch (pkt->u.rem_sta.status) {
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800540 case REM_STA_SUCCESS_MSK:
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800541 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700542 iwl_sta_ucode_deactivate(priv, sta_id);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800543 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Tomas Winklere1623442009-01-27 14:27:56 -0800544 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800545 break;
546 default:
547 ret = -EIO;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800548 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800549 break;
550 }
551 }
Zhu Yi64a76b52009-12-10 14:37:21 -0800552 iwl_free_pages(priv, cmd.reply_page);
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800553
554 return ret;
555}
Emmanuel Grumbachbe1f3ab62008-06-12 09:47:18 +0800556
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800557/**
558 * iwl_remove_station - Remove driver's knowledge of station.
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800559 */
Johannes Bergfd1af152010-04-30 11:30:43 -0700560int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
561 const u8 *addr)
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800562{
Johannes Bergfd1af152010-04-30 11:30:43 -0700563 unsigned long flags;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800564
565 if (!iwl_is_ready(priv)) {
566 IWL_DEBUG_INFO(priv,
Frans Pop91dd6c22010-03-24 14:19:58 -0700567 "Unable to remove station %pM, device not ready.\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700568 addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800569 /*
570 * It is typical for stations to be removed when we are
571 * going down. Return success since device will be down
572 * soon anyway
573 */
574 return 0;
575 }
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800576
Tomas Winklere1623442009-01-27 14:27:56 -0800577 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
Johannes Bergfd1af152010-04-30 11:30:43 -0700578 sta_id, addr);
579
580 if (WARN_ON(sta_id == IWL_INVALID_STATION))
581 return -EINVAL;
582
583 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800584
585 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800586 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700587 addr);
Johannes Bergfd1af152010-04-30 11:30:43 -0700588 goto out_err;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800589 }
590
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800591 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800592 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
Johannes Bergc0222df2010-04-28 08:44:51 -0700593 addr);
Johannes Bergfd1af152010-04-30 11:30:43 -0700594 goto out_err;
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800595 }
596
Johannes Berg1fa61b22010-04-28 08:44:52 -0700597 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
598 kfree(priv->stations[sta_id].lq);
599 priv->stations[sta_id].lq = NULL;
600 }
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800601
602 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
603
604 priv->num_stations--;
605
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800606 BUG_ON(priv->num_stations < 0);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800607
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800608 spin_unlock_irqrestore(&priv->sta_lock, flags);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800609
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700610 return iwl_send_remove_station(priv, addr, sta_id);
Johannes Bergfd1af152010-04-30 11:30:43 -0700611out_err:
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800612 spin_unlock_irqrestore(&priv->sta_lock, flags);
Johannes Bergfd1af152010-04-30 11:30:43 -0700613 return -EINVAL;
Tomas Winkler7a999bf2008-05-29 16:35:02 +0800614}
Johannes Berg1fa61b22010-04-28 08:44:52 -0700615EXPORT_SYMBOL_GPL(iwl_remove_station);
Emmanuel Grumbach24e5c402008-06-30 17:23:03 +0800616
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800617/**
Johannes Berg2c810cc2010-04-29 00:53:29 -0700618 * iwl_clear_ucode_stations - clear ucode station table bits
619 *
620 * This function clears all the bits in the driver indicating
621 * which stations are active in the ucode. Call when something
622 * other than explicit station management would cause this in
623 * the ucode, e.g. unassociated RXON.
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800624 */
Johannes Bergdcef7322010-08-27 08:55:52 -0700625void iwl_clear_ucode_stations(struct iwl_priv *priv,
626 struct iwl_rxon_context *ctx)
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800627{
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700628 int i;
Reinette Chatre7e246192010-02-18 22:58:32 -0800629 unsigned long flags_spin;
630 bool cleared = false;
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800631
Johannes Berg2c810cc2010-04-29 00:53:29 -0700632 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
Mohamed Abbas48676eb2009-03-11 11:17:59 -0700633
Reinette Chatre7e246192010-02-18 22:58:32 -0800634 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Johannes Berg2c810cc2010-04-29 00:53:29 -0700635 for (i = 0; i < priv->hw_params.max_stations; i++) {
Johannes Bergdcef7322010-08-27 08:55:52 -0700636 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
637 continue;
638
Johannes Berg2c810cc2010-04-29 00:53:29 -0700639 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
640 IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d\n", i);
641 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
642 cleared = true;
Reinette Chatre7e246192010-02-18 22:58:32 -0800643 }
644 }
645 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
646
647 if (!cleared)
648 IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800649}
Reinette Chatre7e246192010-02-18 22:58:32 -0800650EXPORT_SYMBOL(iwl_clear_ucode_stations);
651
652/**
653 * iwl_restore_stations() - Restore driver known stations to device
654 *
655 * All stations considered active by driver, but not present in ucode, is
656 * restored.
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800657 *
658 * Function sleeps.
Reinette Chatre7e246192010-02-18 22:58:32 -0800659 */
Johannes Bergdcef7322010-08-27 08:55:52 -0700660void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
Reinette Chatre7e246192010-02-18 22:58:32 -0800661{
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700662 struct iwl_addsta_cmd sta_cmd;
663 struct iwl_link_quality_cmd lq;
Reinette Chatre7e246192010-02-18 22:58:32 -0800664 unsigned long flags_spin;
665 int i;
666 bool found = false;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800667 int ret;
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700668 bool send_lq;
Reinette Chatre7e246192010-02-18 22:58:32 -0800669
670 if (!iwl_is_ready(priv)) {
671 IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
672 return;
673 }
674
675 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
676 spin_lock_irqsave(&priv->sta_lock, flags_spin);
677 for (i = 0; i < priv->hw_params.max_stations; i++) {
Johannes Bergdcef7322010-08-27 08:55:52 -0700678 if (ctx->ctxid != priv->stations[i].ctxid)
679 continue;
Reinette Chatre7e246192010-02-18 22:58:32 -0800680 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
681 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
682 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
683 priv->stations[i].sta.sta.addr);
684 priv->stations[i].sta.mode = 0;
685 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
686 found = true;
687 }
688 }
689
690 for (i = 0; i < priv->hw_params.max_stations; i++) {
691 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700692 memcpy(&sta_cmd, &priv->stations[i].sta,
693 sizeof(struct iwl_addsta_cmd));
694 send_lq = false;
695 if (priv->stations[i].lq) {
696 memcpy(&lq, priv->stations[i].lq,
697 sizeof(struct iwl_link_quality_cmd));
698 send_lq = true;
699 }
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800700 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700701 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800702 if (ret) {
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800703 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700704 IWL_ERR(priv, "Adding station %pM failed.\n",
705 priv->stations[i].sta.sta.addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800706 priv->stations[i].used &= ~IWL_STA_DRIVER_ACTIVE;
707 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
708 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
709 }
710 /*
711 * Rate scaling has already been initialized, send
712 * current LQ command
713 */
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700714 if (send_lq)
Johannes Berg7e6a5882010-08-23 10:46:46 +0200715 iwl_send_lq_cmd(priv, ctx, &lq, CMD_SYNC, true);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800716 spin_lock_irqsave(&priv->sta_lock, flags_spin);
Reinette Chatre7e246192010-02-18 22:58:32 -0800717 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
718 }
719 }
720
721 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
722 if (!found)
723 IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
724 else
Reinette Chatrefe6b23d2010-02-22 16:24:47 -0800725 IWL_DEBUG_INFO(priv, "Restoring all known stations .... complete.\n");
Reinette Chatre7e246192010-02-18 22:58:32 -0800726}
727EXPORT_SYMBOL(iwl_restore_stations);
Winkler, Tomas83dde8c2008-11-19 15:32:23 -0800728
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800729int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700730{
731 int i;
732
Johannes Bergc10afb62010-08-23 10:46:43 +0200733 for (i = 0; i < priv->sta_key_max_num; i++)
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700734 if (!test_and_set_bit(i, &priv->ucode_key_table))
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700735 return i;
736
Tomas Winkler40a9a822008-11-25 23:29:03 +0200737 return WEP_INVALID_OFFSET;
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700738}
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800739EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700740
Johannes Bergc10afb62010-08-23 10:46:43 +0200741static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
742 struct iwl_rxon_context *ctx,
743 bool send_if_empty)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700744{
745 int i, not_empty = 0;
746 u8 buff[sizeof(struct iwl_wep_cmd) +
747 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
748 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
749 size_t cmd_size = sizeof(struct iwl_wep_cmd);
750 struct iwl_host_cmd cmd = {
Johannes Bergc10afb62010-08-23 10:46:43 +0200751 .id = ctx->wep_key_cmd,
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700752 .data = wep_cmd,
Johannes Berg72e15d72010-02-19 11:42:32 -0800753 .flags = CMD_SYNC,
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700754 };
755
Johannes Berg72e15d72010-02-19 11:42:32 -0800756 might_sleep();
757
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700758 memset(wep_cmd, 0, cmd_size +
759 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
760
761 for (i = 0; i < WEP_KEYS_MAX ; i++) {
762 wep_cmd->key[i].key_index = i;
Johannes Bergc10afb62010-08-23 10:46:43 +0200763 if (ctx->wep_keys[i].key_size) {
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700764 wep_cmd->key[i].key_offset = i;
765 not_empty = 1;
766 } else {
767 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
768 }
769
Johannes Bergc10afb62010-08-23 10:46:43 +0200770 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
771 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
772 ctx->wep_keys[i].key_size);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700773 }
774
775 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
776 wep_cmd->num_keys = WEP_KEYS_MAX;
777
778 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
779
780 cmd.len = cmd_size;
781
782 if (not_empty || send_if_empty)
783 return iwl_send_cmd(priv, &cmd);
784 else
785 return 0;
786}
Johannes Berg335348b2010-03-30 10:11:46 -0700787
Johannes Bergc10afb62010-08-23 10:46:43 +0200788int iwl_restore_default_wep_keys(struct iwl_priv *priv,
789 struct iwl_rxon_context *ctx)
Johannes Berg335348b2010-03-30 10:11:46 -0700790{
Johannes Berga24d52f2010-08-06 16:17:53 +0200791 lockdep_assert_held(&priv->mutex);
Johannes Berg335348b2010-03-30 10:11:46 -0700792
Johannes Bergc10afb62010-08-23 10:46:43 +0200793 return iwl_send_static_wepkey_cmd(priv, ctx, false);
Johannes Berg335348b2010-03-30 10:11:46 -0700794}
795EXPORT_SYMBOL(iwl_restore_default_wep_keys);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700796
797int iwl_remove_default_wep_key(struct iwl_priv *priv,
Johannes Bergc10afb62010-08-23 10:46:43 +0200798 struct iwl_rxon_context *ctx,
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700799 struct ieee80211_key_conf *keyconf)
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700800{
801 int ret;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700802
Johannes Berga24d52f2010-08-06 16:17:53 +0200803 lockdep_assert_held(&priv->mutex);
Johannes Berg72e15d72010-02-19 11:42:32 -0800804
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700805 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
806 keyconf->keyidx);
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700807
Johannes Bergc10afb62010-08-23 10:46:43 +0200808 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700809 if (iwl_is_rfkill(priv)) {
810 IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berg72e15d72010-02-19 11:42:32 -0800811 /* but keys in device are clear anyway so return success */
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -0700812 return 0;
813 }
Johannes Bergc10afb62010-08-23 10:46:43 +0200814 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
Tomas Winklere1623442009-01-27 14:27:56 -0800815 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800816 keyconf->keyidx, ret);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700817
818 return ret;
819}
Tomas Winkler27aaba02008-05-05 10:22:44 +0800820EXPORT_SYMBOL(iwl_remove_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700821
822int iwl_set_default_wep_key(struct iwl_priv *priv,
Johannes Berg2995baf2010-08-23 10:46:42 +0200823 struct iwl_rxon_context *ctx,
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700824 struct ieee80211_key_conf *keyconf)
825{
826 int ret;
Johannes Berg72e15d72010-02-19 11:42:32 -0800827
Johannes Berga24d52f2010-08-06 16:17:53 +0200828 lockdep_assert_held(&priv->mutex);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700829
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800830 if (keyconf->keylen != WEP_KEY_LEN_128 &&
831 keyconf->keylen != WEP_KEY_LEN_64) {
Tomas Winklere1623442009-01-27 14:27:56 -0800832 IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800833 return -EINVAL;
834 }
835
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700836 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +0800837 keyconf->hw_key_idx = HW_KEY_DEFAULT;
Johannes Berg2995baf2010-08-23 10:46:42 +0200838 priv->stations[ctx->ap_sta_id].keyinfo.cipher = keyconf->cipher;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700839
Johannes Bergc10afb62010-08-23 10:46:43 +0200840 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
841 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700842 keyconf->keylen);
843
Johannes Bergc10afb62010-08-23 10:46:43 +0200844 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
Tomas Winklere1623442009-01-27 14:27:56 -0800845 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +0800846 keyconf->keylen, 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_set_default_wep_key);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700851
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700852static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
Johannes Berga194e322010-08-27 08:53:46 -0700853 struct iwl_rxon_context *ctx,
854 struct ieee80211_key_conf *keyconf,
855 u8 sta_id)
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700856{
857 unsigned long flags;
858 __le16 key_flags = 0;
Johannes Berge6666742010-06-14 08:32:24 -0700859 struct iwl_addsta_cmd sta_cmd;
860
861 lockdep_assert_held(&priv->mutex);
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700862
863 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700864
865 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
866 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
867 key_flags &= ~STA_KEY_FLG_INVALID;
868
869 if (keyconf->keylen == WEP_KEY_LEN_128)
870 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
871
Johannes Berga194e322010-08-27 08:53:46 -0700872 if (sta_id == ctx->bcast_sta_id)
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700873 key_flags |= STA_KEY_MULTICAST_MSK;
874
875 spin_lock_irqsave(&priv->sta_lock, flags);
876
Johannes Berg97359d12010-08-10 09:46:38 +0200877 priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700878 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
879 priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
880
881 memcpy(priv->stations[sta_id].keyinfo.key,
882 keyconf->key, keyconf->keylen);
883
884 memcpy(&priv->stations[sta_id].sta.key.key[3],
885 keyconf->key, keyconf->keylen);
886
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700887 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
888 == STA_KEY_FLG_NO_ENC)
889 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach80fb47a2008-04-14 21:16:08 -0700890 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700891 /* else, we are overriding an existing key => no need to allocated room
892 * in uCode. */
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700893
Tomas Winkler40a9a822008-11-25 23:29:03 +0200894 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100895 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200896
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700897 priv->stations[sta_id].sta.key.key_flags = key_flags;
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700898 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
899 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
900
Johannes Berge6666742010-06-14 08:32:24 -0700901 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700902 spin_unlock_irqrestore(&priv->sta_lock, flags);
903
Johannes Berge6666742010-06-14 08:32:24 -0700904 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -0700905}
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700906
907static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
Johannes Berga194e322010-08-27 08:53:46 -0700908 struct iwl_rxon_context *ctx,
909 struct ieee80211_key_conf *keyconf,
910 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700911{
912 unsigned long flags;
913 __le16 key_flags = 0;
Johannes Berge6666742010-06-14 08:32:24 -0700914 struct iwl_addsta_cmd sta_cmd;
915
916 lockdep_assert_held(&priv->mutex);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700917
918 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
919 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
920 key_flags &= ~STA_KEY_FLG_INVALID;
921
Johannes Berga194e322010-08-27 08:53:46 -0700922 if (sta_id == ctx->bcast_sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700923 key_flags |= STA_KEY_MULTICAST_MSK;
924
925 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700926
927 spin_lock_irqsave(&priv->sta_lock, flags);
Johannes Berg97359d12010-08-10 09:46:38 +0200928 priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700929 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
930
931 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
932 keyconf->keylen);
933
934 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
935 keyconf->keylen);
936
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700937 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
938 == STA_KEY_FLG_NO_ENC)
939 priv->stations[sta_id].sta.key.key_offset =
940 iwl_get_free_ucode_key_index(priv);
941 /* else, we are overriding an existing key => no need to allocated room
942 * in uCode. */
943
Tomas Winkler40a9a822008-11-25 23:29:03 +0200944 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100945 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200946
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700947 priv->stations[sta_id].sta.key.key_flags = key_flags;
948 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
949 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
950
Johannes Berge6666742010-06-14 08:32:24 -0700951 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700952 spin_unlock_irqrestore(&priv->sta_lock, flags);
953
Johannes Berge6666742010-06-14 08:32:24 -0700954 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700955}
956
957static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
Johannes Berga194e322010-08-27 08:53:46 -0700958 struct iwl_rxon_context *ctx,
959 struct ieee80211_key_conf *keyconf,
960 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700961{
962 unsigned long flags;
963 int ret = 0;
Reinette Chatre299f5462009-04-30 13:56:31 -0700964 __le16 key_flags = 0;
965
966 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
967 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
968 key_flags &= ~STA_KEY_FLG_INVALID;
969
Johannes Berga194e322010-08-27 08:53:46 -0700970 if (sta_id == ctx->bcast_sta_id)
Reinette Chatre299f5462009-04-30 13:56:31 -0700971 key_flags |= STA_KEY_MULTICAST_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700972
973 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
974 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700975
976 spin_lock_irqsave(&priv->sta_lock, flags);
977
Johannes Berg97359d12010-08-10 09:46:38 +0200978 priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700979 priv->stations[sta_id].keyinfo.keylen = 16;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700980
981 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
982 == STA_KEY_FLG_NO_ENC)
983 priv->stations[sta_id].sta.key.key_offset =
Emmanuel Grumbach77bab602008-04-15 16:01:44 -0700984 iwl_get_free_ucode_key_index(priv);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -0700985 /* else, we are overriding an existing key => no need to allocated room
986 * in uCode. */
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700987
Tomas Winkler40a9a822008-11-25 23:29:03 +0200988 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Jiri Slabye724b8f2009-01-05 17:06:06 +0100989 "no space for a new key");
Tomas Winkler40a9a822008-11-25 23:29:03 +0200990
Reinette Chatre299f5462009-04-30 13:56:31 -0700991 priv->stations[sta_id].sta.key.key_flags = key_flags;
992
993
Emmanuel Grumbach74805132008-04-14 21:16:09 -0700994 /* This copy is acutally not needed: we get the key with each TX */
995 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
996
997 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
998
999 spin_unlock_irqrestore(&priv->sta_lock, flags);
1000
1001 return ret;
1002}
1003
Tomas Winkler9f586712008-11-12 13:14:05 -08001004void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berga194e322010-08-27 08:53:46 -07001005 struct iwl_rxon_context *ctx,
1006 struct ieee80211_key_conf *keyconf,
1007 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
Tomas Winkler9f586712008-11-12 13:14:05 -08001008{
Johannes Bergbdbb6122010-04-30 13:53:37 -07001009 u8 sta_id;
Tomas Winkler9f586712008-11-12 13:14:05 -08001010 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -08001011 int i;
Tomas Winkler9f586712008-11-12 13:14:05 -08001012
Tomas Winkler9f586712008-11-12 13:14:05 -08001013 if (iwl_scan_cancel(priv)) {
1014 /* cancel scan failed, just live w/ bad key and rely
1015 briefly on SW decryption */
1016 return;
1017 }
1018
Johannes Berga194e322010-08-27 08:53:46 -07001019 sta_id = iwl_sta_id_or_broadcast(priv, ctx, sta);
Johannes Berg0af8bca2010-04-30 14:08:00 -07001020 if (sta_id == IWL_INVALID_STATION)
1021 return;
1022
Tomas Winkler9f586712008-11-12 13:14:05 -08001023 spin_lock_irqsave(&priv->sta_lock, flags);
1024
Tomas Winkler9f586712008-11-12 13:14:05 -08001025 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
1026
1027 for (i = 0; i < 5; i++)
1028 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
1029 cpu_to_le16(phase1key[i]);
1030
1031 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1032 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1033
1034 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1035
1036 spin_unlock_irqrestore(&priv->sta_lock, flags);
1037
1038}
1039EXPORT_SYMBOL(iwl_update_tkip_key);
1040
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001041int iwl_remove_dynamic_key(struct iwl_priv *priv,
Johannes Bergc10afb62010-08-23 10:46:43 +02001042 struct iwl_rxon_context *ctx,
1043 struct ieee80211_key_conf *keyconf,
1044 u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001045{
1046 unsigned long flags;
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001047 u16 key_flags;
1048 u8 keyidx;
Johannes Berge6666742010-06-14 08:32:24 -07001049 struct iwl_addsta_cmd sta_cmd;
1050
1051 lockdep_assert_held(&priv->mutex);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001052
Johannes Bergc10afb62010-08-23 10:46:43 +02001053 ctx->key_mapping_keys--;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001054
1055 spin_lock_irqsave(&priv->sta_lock, flags);
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001056 key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
1057 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
1058
Tomas Winklere1623442009-01-27 14:27:56 -08001059 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +08001060 keyconf->keyidx, sta_id);
1061
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001062 if (keyconf->keyidx != keyidx) {
1063 /* We need to remove a key with index different that the one
1064 * in the uCode. This means that the key we need to remove has
1065 * been replaced by another one with different index.
1066 * Don't do anything and return ok
1067 */
1068 spin_unlock_irqrestore(&priv->sta_lock, flags);
1069 return 0;
1070 }
1071
Tomas Winkler40a9a822008-11-25 23:29:03 +02001072 if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001073 IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
Tomas Winkler40a9a822008-11-25 23:29:03 +02001074 keyconf->keyidx, key_flags);
1075 spin_unlock_irqrestore(&priv->sta_lock, flags);
1076 return 0;
1077 }
1078
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001079 if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
1080 &priv->ucode_key_table))
Winkler, Tomas15b16872008-12-19 10:37:33 +08001081 IWL_ERR(priv, "index %d not used in uCode key table.\n",
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001082 priv->stations[sta_id].sta.key.key_offset);
1083 memset(&priv->stations[sta_id].keyinfo, 0,
Tomas Winkler6def9762008-05-05 10:22:31 +08001084 sizeof(struct iwl_hw_key));
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001085 memset(&priv->stations[sta_id].sta.key, 0,
1086 sizeof(struct iwl4965_keyinfo));
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001087 priv->stations[sta_id].sta.key.key_flags =
1088 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
1089 priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001090 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1091 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001092
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -07001093 if (iwl_is_rfkill(priv)) {
Frans Pop91dd6c22010-03-24 14:19:58 -07001094 IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
Reinette Chatre2d1bb9e2009-07-17 09:30:18 -07001095 spin_unlock_irqrestore(&priv->sta_lock, flags);
1096 return 0;
1097 }
Johannes Berge6666742010-06-14 08:32:24 -07001098 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07001099 spin_unlock_irqrestore(&priv->sta_lock, flags);
Johannes Berge6666742010-06-14 08:32:24 -07001100
1101 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001102}
Tomas Winkler27aaba02008-05-05 10:22:44 +08001103EXPORT_SYMBOL(iwl_remove_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001104
Johannes Berga194e322010-08-27 08:53:46 -07001105int iwl_set_dynamic_key(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
1106 struct ieee80211_key_conf *keyconf, u8 sta_id)
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001107{
1108 int ret;
1109
Johannes Berge6666742010-06-14 08:32:24 -07001110 lockdep_assert_held(&priv->mutex);
1111
Johannes Bergc10afb62010-08-23 10:46:43 +02001112 ctx->key_mapping_keys++;
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08001113 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001114
Johannes Berg97359d12010-08-10 09:46:38 +02001115 switch (keyconf->cipher) {
1116 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berga194e322010-08-27 08:53:46 -07001117 ret = iwl_set_ccmp_dynamic_key_info(priv, ctx, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001118 break;
Johannes Berg97359d12010-08-10 09:46:38 +02001119 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berga194e322010-08-27 08:53:46 -07001120 ret = iwl_set_tkip_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_WEP40:
1123 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga194e322010-08-27 08:53:46 -07001124 ret = iwl_set_wep_dynamic_key_info(priv, ctx, keyconf, sta_id);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001125 break;
1126 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001127 IWL_ERR(priv,
Johannes Berg97359d12010-08-10 09:46:38 +02001128 "Unknown alg: %s cipher = %x\n", __func__,
1129 keyconf->cipher);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001130 ret = -EINVAL;
1131 }
1132
Johannes Berg97359d12010-08-10 09:46:38 +02001133 IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
1134 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Emmanuel Grumbach4564ce82008-06-12 09:47:09 +08001135 sta_id, ret);
1136
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001137 return ret;
1138}
Tomas Winkler27aaba02008-05-05 10:22:44 +08001139EXPORT_SYMBOL(iwl_set_dynamic_key);
Emmanuel Grumbach74805132008-04-14 21:16:09 -07001140
Tomas Winkler66c73db2008-04-15 16:01:40 -07001141#ifdef CONFIG_IWLWIFI_DEBUG
1142static void iwl_dump_lq_cmd(struct iwl_priv *priv,
1143 struct iwl_link_quality_cmd *lq)
1144{
1145 int i;
Tomas Winklere1623442009-01-27 14:27:56 -08001146 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
1147 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001148 lq->general_params.single_stream_ant_msk,
1149 lq->general_params.dual_stream_ant_msk);
1150
1151 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
Tomas Winklere1623442009-01-27 14:27:56 -08001152 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
Tomas Winkler66c73db2008-04-15 16:01:40 -07001153 i, lq->rs_table[i].rate_n_flags);
1154}
1155#else
1156static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
1157 struct iwl_link_quality_cmd *lq)
1158{
1159}
1160#endif
1161
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001162/**
Reinette Chatre3bce6062010-04-13 16:11:19 -07001163 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
1164 *
1165 * It sometimes happens when a HT rate has been in use and we
1166 * loose connectivity with AP then mac80211 will first tell us that the
1167 * current channel is not HT anymore before removing the station. In such a
1168 * scenario the RXON flags will be updated to indicate we are not
1169 * communicating HT anymore, but the LQ command may still contain HT rates.
1170 * Test for this to prevent driver from sending LQ command between the time
1171 * RXON flags are updated and when LQ command is updated.
1172 */
1173static bool is_lq_table_valid(struct iwl_priv *priv,
Johannes Berg7e6a5882010-08-23 10:46:46 +02001174 struct iwl_rxon_context *ctx,
Reinette Chatre3bce6062010-04-13 16:11:19 -07001175 struct iwl_link_quality_cmd *lq)
1176{
1177 int i;
Reinette Chatre3bce6062010-04-13 16:11:19 -07001178
Johannes Berg7e6a5882010-08-23 10:46:46 +02001179 if (ctx->ht.enabled)
Reinette Chatre3bce6062010-04-13 16:11:19 -07001180 return true;
1181
1182 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
Johannes Berg246ed352010-08-23 10:46:32 +02001183 ctx->active.channel);
Reinette Chatre3bce6062010-04-13 16:11:19 -07001184 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
1185 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) & RATE_MCS_HT_MSK) {
1186 IWL_DEBUG_INFO(priv,
1187 "index %d of LQ expects HT channel\n",
1188 i);
1189 return false;
1190 }
1191 }
1192 return true;
1193}
1194
1195/**
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001196 * iwl_send_lq_cmd() - Send link quality command
1197 * @init: This command is sent as part of station initialization right
1198 * after station has been added.
1199 *
1200 * The link quality command is sent as the last step of station creation.
1201 * This is the special case in which init is set and we call a callback in
1202 * this case to clear the state indicating that station creation is in
1203 * progress.
1204 */
Johannes Berg7e6a5882010-08-23 10:46:46 +02001205int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001206 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
Tomas Winkler66c73db2008-04-15 16:01:40 -07001207{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001208 int ret = 0;
1209 unsigned long flags_spin;
1210
Tomas Winkler66c73db2008-04-15 16:01:40 -07001211 struct iwl_host_cmd cmd = {
1212 .id = REPLY_TX_LINK_QUALITY_CMD,
1213 .len = sizeof(struct iwl_link_quality_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07001214 .flags = flags,
Tomas Winkler66c73db2008-04-15 16:01:40 -07001215 .data = lq,
1216 };
1217
Johannes Berg76c3c692010-03-30 02:46:29 -07001218 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
Tomas Winkler66c73db2008-04-15 16:01:40 -07001219 return -EINVAL;
1220
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001221 iwl_dump_lq_cmd(priv, lq);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001222 BUG_ON(init && (cmd.flags & CMD_ASYNC));
Tomas Winkler66c73db2008-04-15 16:01:40 -07001223
Johannes Berg7e6a5882010-08-23 10:46:46 +02001224 if (is_lq_table_valid(priv, ctx, lq))
Reinette Chatre3bce6062010-04-13 16:11:19 -07001225 ret = iwl_send_cmd(priv, &cmd);
1226 else
1227 ret = -EINVAL;
Reinette Chatred2e210a2010-04-23 10:33:33 -07001228
1229 if (cmd.flags & CMD_ASYNC)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001230 return ret;
Tomas Winkler66c73db2008-04-15 16:01:40 -07001231
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001232 if (init) {
Frans Pop91dd6c22010-03-24 14:19:58 -07001233 IWL_DEBUG_INFO(priv, "init LQ command complete, clearing sta addition status for sta %d\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001234 lq->sta_id);
1235 spin_lock_irqsave(&priv->sta_lock, flags_spin);
1236 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
1237 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
1238 }
Reinette Chatred2e210a2010-04-23 10:33:33 -07001239 return ret;
Tomas Winkler66c73db2008-04-15 16:01:40 -07001240}
1241EXPORT_SYMBOL(iwl_send_lq_cmd);
1242
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001243/**
Johannes Berg2c810cc2010-04-29 00:53:29 -07001244 * iwl_alloc_bcast_station - add broadcast station into driver's station table.
1245 *
1246 * This adds the broadcast station into the driver's station table
1247 * and marks it driver active, so that it will be restored to the
1248 * device at the next best time.
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001249 */
Johannes Berga194e322010-08-27 08:53:46 -07001250int iwl_alloc_bcast_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
1251 bool init_lq)
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001252{
Johannes Berg2c810cc2010-04-29 00:53:29 -07001253 struct iwl_link_quality_cmd *link_cmd;
1254 unsigned long flags;
1255 u8 sta_id;
Reinette Chatre9a9ca652009-10-30 14:36:12 -07001256
Johannes Berg2c810cc2010-04-29 00:53:29 -07001257 spin_lock_irqsave(&priv->sta_lock, flags);
Johannes Berga194e322010-08-27 08:53:46 -07001258 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
Johannes Berg2c810cc2010-04-29 00:53:29 -07001259 if (sta_id == IWL_INVALID_STATION) {
1260 IWL_ERR(priv, "Unable to prepare broadcast station\n");
1261 spin_unlock_irqrestore(&priv->sta_lock, flags);
1262
1263 return -EINVAL;
1264 }
1265
1266 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1267 priv->stations[sta_id].used |= IWL_STA_BCAST;
1268 spin_unlock_irqrestore(&priv->sta_lock, flags);
1269
1270 if (init_lq) {
1271 link_cmd = iwl_sta_alloc_lq(priv, sta_id);
1272 if (!link_cmd) {
1273 IWL_ERR(priv,
1274 "Unable to initialize rate scaling for bcast station.\n");
1275 return -ENOMEM;
1276 }
1277
1278 spin_lock_irqsave(&priv->sta_lock, flags);
1279 priv->stations[sta_id].lq = link_cmd;
1280 spin_unlock_irqrestore(&priv->sta_lock, flags);
1281 }
1282
1283 return 0;
1284}
1285EXPORT_SYMBOL_GPL(iwl_alloc_bcast_station);
1286
Daniel Halperin278c2f62010-06-14 13:10:29 -07001287/**
1288 * iwl_update_bcast_station - update broadcast station's LQ command
1289 *
1290 * Only used by iwlagn. Placed here to have all bcast station management
1291 * code together.
1292 */
Johannes Berga194e322010-08-27 08:53:46 -07001293static int iwl_update_bcast_station(struct iwl_priv *priv,
1294 struct iwl_rxon_context *ctx)
Daniel Halperin278c2f62010-06-14 13:10:29 -07001295{
1296 unsigned long flags;
1297 struct iwl_link_quality_cmd *link_cmd;
Johannes Berga194e322010-08-27 08:53:46 -07001298 u8 sta_id = ctx->bcast_sta_id;
Daniel Halperin278c2f62010-06-14 13:10:29 -07001299
1300 link_cmd = iwl_sta_alloc_lq(priv, sta_id);
1301 if (!link_cmd) {
1302 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1303 return -ENOMEM;
1304 }
1305
1306 spin_lock_irqsave(&priv->sta_lock, flags);
1307 if (priv->stations[sta_id].lq)
1308 kfree(priv->stations[sta_id].lq);
1309 else
1310 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1311 priv->stations[sta_id].lq = link_cmd;
1312 spin_unlock_irqrestore(&priv->sta_lock, flags);
1313
1314 return 0;
1315}
Daniel Halperin278c2f62010-06-14 13:10:29 -07001316
Johannes Berga194e322010-08-27 08:53:46 -07001317int iwl_update_bcast_stations(struct iwl_priv *priv)
1318{
1319 struct iwl_rxon_context *ctx;
1320 int ret = 0;
1321
1322 for_each_context(priv, ctx) {
1323 ret = iwl_update_bcast_station(priv, ctx);
1324 if (ret)
1325 break;
1326 }
1327
1328 return ret;
1329}
1330EXPORT_SYMBOL_GPL(iwl_update_bcast_stations);
1331
1332void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
Reinette Chatre3459ab52010-01-22 14:22:49 -08001333{
Johannes Berg2c810cc2010-04-29 00:53:29 -07001334 unsigned long flags;
1335 int i;
Reinette Chatred2e210a2010-04-23 10:33:33 -07001336
Johannes Berg2c810cc2010-04-29 00:53:29 -07001337 spin_lock_irqsave(&priv->sta_lock, flags);
1338 for (i = 0; i < priv->hw_params.max_stations; i++) {
1339 if (!(priv->stations[i].used & IWL_STA_BCAST))
1340 continue;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001341
Johannes Berg2c810cc2010-04-29 00:53:29 -07001342 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
1343 priv->num_stations--;
1344 BUG_ON(priv->num_stations < 0);
1345 kfree(priv->stations[i].lq);
1346 priv->stations[i].lq = NULL;
1347 }
1348 spin_unlock_irqrestore(&priv->sta_lock, flags);
Reinette Chatre3459ab52010-01-22 14:22:49 -08001349}
Johannes Berga194e322010-08-27 08:53:46 -07001350EXPORT_SYMBOL_GPL(iwl_dealloc_bcast_stations);
Reinette Chatre3459ab52010-01-22 14:22:49 -08001351
1352/**
Tomas Winkler9f586712008-11-12 13:14:05 -08001353 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
Tomas Winkler5083e562008-05-29 16:35:15 +08001354 */
Johannes Berg4620fef2010-06-16 03:30:27 -07001355int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
Tomas Winkler5083e562008-05-29 16:35:15 +08001356{
1357 unsigned long flags;
Johannes Berg4620fef2010-06-16 03:30:27 -07001358 struct iwl_addsta_cmd sta_cmd;
1359
1360 lockdep_assert_held(&priv->mutex);
Tomas Winkler5083e562008-05-29 16:35:15 +08001361
1362 /* Remove "disable" flag, to enable Tx for this TID */
1363 spin_lock_irqsave(&priv->sta_lock, flags);
1364 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1365 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1366 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berg4620fef2010-06-16 03:30:27 -07001367 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Reinette Chatre9c5ac092010-05-05 02:26:06 -07001368 spin_unlock_irqrestore(&priv->sta_lock, flags);
Johannes Berg4620fef2010-06-16 03:30:27 -07001369
1370 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Tomas Winkler5083e562008-05-29 16:35:15 +08001371}
Tomas Winkler9f586712008-11-12 13:14:05 -08001372EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
1373
Johannes Berg619753f2010-04-30 11:30:46 -07001374int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1375 int tid, u16 ssn)
Tomas Winkler9f586712008-11-12 13:14:05 -08001376{
1377 unsigned long flags;
1378 int sta_id;
Johannes Berg4620fef2010-06-16 03:30:27 -07001379 struct iwl_addsta_cmd sta_cmd;
1380
1381 lockdep_assert_held(&priv->mutex);
Tomas Winkler9f586712008-11-12 13:14:05 -08001382
Johannes Berg619753f2010-04-30 11:30:46 -07001383 sta_id = iwl_sta_id(sta);
Tomas Winkler9f586712008-11-12 13:14:05 -08001384 if (sta_id == IWL_INVALID_STATION)
1385 return -ENXIO;
1386
1387 spin_lock_irqsave(&priv->sta_lock, flags);
1388 priv->stations[sta_id].sta.station_flags_msk = 0;
1389 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1390 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1391 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1392 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berg4620fef2010-06-16 03:30:27 -07001393 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Tomas Winkler9f586712008-11-12 13:14:05 -08001394 spin_unlock_irqrestore(&priv->sta_lock, flags);
1395
Johannes Berg4620fef2010-06-16 03:30:27 -07001396 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001397}
1398EXPORT_SYMBOL(iwl_sta_rx_agg_start);
1399
Johannes Berg619753f2010-04-30 11:30:46 -07001400int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1401 int tid)
Tomas Winkler9f586712008-11-12 13:14:05 -08001402{
1403 unsigned long flags;
Johannes Berg4620fef2010-06-16 03:30:27 -07001404 int sta_id;
1405 struct iwl_addsta_cmd sta_cmd;
1406
1407 lockdep_assert_held(&priv->mutex);
Tomas Winkler9f586712008-11-12 13:14:05 -08001408
Johannes Berg619753f2010-04-30 11:30:46 -07001409 sta_id = iwl_sta_id(sta);
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001410 if (sta_id == IWL_INVALID_STATION) {
1411 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
Tomas Winkler9f586712008-11-12 13:14:05 -08001412 return -ENXIO;
Wey-Yi Guya2f1cbe2009-03-17 21:51:52 -07001413 }
Tomas Winkler9f586712008-11-12 13:14:05 -08001414
1415 spin_lock_irqsave(&priv->sta_lock, flags);
1416 priv->stations[sta_id].sta.station_flags_msk = 0;
1417 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1418 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1419 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berg4620fef2010-06-16 03:30:27 -07001420 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Tomas Winkler9f586712008-11-12 13:14:05 -08001421 spin_unlock_irqrestore(&priv->sta_lock, flags);
1422
Johannes Berg4620fef2010-06-16 03:30:27 -07001423 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001424}
1425EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
1426
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001427void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
Tomas Winkler9f586712008-11-12 13:14:05 -08001428{
1429 unsigned long flags;
1430
1431 spin_lock_irqsave(&priv->sta_lock, flags);
1432 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1433 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1434 priv->stations[sta_id].sta.sta.modify_mask = 0;
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001435 priv->stations[sta_id].sta.sleep_tx_count = 0;
Tomas Winkler9f586712008-11-12 13:14:05 -08001436 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Reinette Chatre9c5ac092010-05-05 02:26:06 -07001437 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Tomas Winkler9f586712008-11-12 13:14:05 -08001438 spin_unlock_irqrestore(&priv->sta_lock, flags);
1439
Tomas Winkler9f586712008-11-12 13:14:05 -08001440}
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001441EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
Tomas Winkler9f586712008-11-12 13:14:05 -08001442
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001443void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
Tomas Winkler9f586712008-11-12 13:14:05 -08001444{
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001445 unsigned long flags;
Tomas Winkler9f586712008-11-12 13:14:05 -08001446
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001447 spin_lock_irqsave(&priv->sta_lock, flags);
1448 priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
1449 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1450 priv->stations[sta_id].sta.sta.modify_mask =
1451 STA_MODIFY_SLEEP_TX_COUNT_MSK;
1452 priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
1453 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Reinette Chatre9c5ac092010-05-05 02:26:06 -07001454 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Johannes Berg6ab10ff2009-11-13 11:56:37 -08001455 spin_unlock_irqrestore(&priv->sta_lock, flags);
Tomas Winkler9f586712008-11-12 13:14:05 -08001456
Tomas Winkler9f586712008-11-12 13:14:05 -08001457}
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -07001458EXPORT_SYMBOL(iwl_sta_modify_sleep_tx_count);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001459
1460int iwl_mac_sta_remove(struct ieee80211_hw *hw,
Johannes Bergfd1af152010-04-30 11:30:43 -07001461 struct ieee80211_vif *vif,
1462 struct ieee80211_sta *sta)
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001463{
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001464 struct iwl_priv *priv = hw->priv;
Johannes Bergfd1af152010-04-30 11:30:43 -07001465 struct iwl_station_priv_common *sta_common = (void *)sta->drv_priv;
1466 int ret;
1467
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001468 IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
1469 sta->addr);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07001470 mutex_lock(&priv->mutex);
1471 IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n",
1472 sta->addr);
Johannes Bergfd1af152010-04-30 11:30:43 -07001473 ret = iwl_remove_station(priv, sta_common->sta_id, sta->addr);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001474 if (ret)
1475 IWL_ERR(priv, "Error removing station %pM\n",
1476 sta->addr);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07001477 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08001478 return ret;
1479}
1480EXPORT_SYMBOL(iwl_mac_sta_remove);