1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
4  *
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:
25  *  Intel Linux Wireless <ilw@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  *****************************************************************************/
29 
30 #include <net/mac80211.h>
31 
32 #include "iwl-dev.h"
33 #include "iwl-core.h"
34 #include "iwl-sta.h"
35 #include "iwl-4965.h"
36 
37 static struct iwl_link_quality_cmd *
iwl4965_sta_alloc_lq(struct iwl_priv * priv,u8 sta_id)38 iwl4965_sta_alloc_lq(struct iwl_priv *priv, u8 sta_id)
39 {
40 	int i, r;
41 	struct iwl_link_quality_cmd *link_cmd;
42 	u32 rate_flags = 0;
43 	__le32 rate_n_flags;
44 
45 	link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
46 	if (!link_cmd) {
47 		IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
48 		return NULL;
49 	}
50 	/* Set up the rate scaling to start at selected rate, fall back
51 	 * all the way down to 1M in IEEE order, and then spin on 1M */
52 	if (priv->band == IEEE80211_BAND_5GHZ)
53 		r = IWL_RATE_6M_INDEX;
54 	else
55 		r = IWL_RATE_1M_INDEX;
56 
57 	if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
58 		rate_flags |= RATE_MCS_CCK_MSK;
59 
60 	rate_flags |= iwl4965_first_antenna(priv->hw_params.valid_tx_ant) <<
61 				RATE_MCS_ANT_POS;
62 	rate_n_flags = iwl4965_hw_set_rate_n_flags(iwlegacy_rates[r].plcp,
63 						   rate_flags);
64 	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
65 		link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
66 
67 	link_cmd->general_params.single_stream_ant_msk =
68 				iwl4965_first_antenna(priv->hw_params.valid_tx_ant);
69 
70 	link_cmd->general_params.dual_stream_ant_msk =
71 		priv->hw_params.valid_tx_ant &
72 		~iwl4965_first_antenna(priv->hw_params.valid_tx_ant);
73 	if (!link_cmd->general_params.dual_stream_ant_msk) {
74 		link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
75 	} else if (iwl4965_num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
76 		link_cmd->general_params.dual_stream_ant_msk =
77 			priv->hw_params.valid_tx_ant;
78 	}
79 
80 	link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
81 	link_cmd->agg_params.agg_time_limit =
82 		cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
83 
84 	link_cmd->sta_id = sta_id;
85 
86 	return link_cmd;
87 }
88 
89 /*
90  * iwl4965_add_bssid_station - Add the special IBSS BSSID station
91  *
92  * Function sleeps.
93  */
94 int
iwl4965_add_bssid_station(struct iwl_priv * priv,struct iwl_rxon_context * ctx,const u8 * addr,u8 * sta_id_r)95 iwl4965_add_bssid_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
96 			     const u8 *addr, u8 *sta_id_r)
97 {
98 	int ret;
99 	u8 sta_id;
100 	struct iwl_link_quality_cmd *link_cmd;
101 	unsigned long flags;
102 
103 	if (sta_id_r)
104 		*sta_id_r = IWL_INVALID_STATION;
105 
106 	ret = iwl_legacy_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
107 	if (ret) {
108 		IWL_ERR(priv, "Unable to add station %pM\n", addr);
109 		return ret;
110 	}
111 
112 	if (sta_id_r)
113 		*sta_id_r = sta_id;
114 
115 	spin_lock_irqsave(&priv->sta_lock, flags);
116 	priv->stations[sta_id].used |= IWL_STA_LOCAL;
117 	spin_unlock_irqrestore(&priv->sta_lock, flags);
118 
119 	/* Set up default rate scaling table in device's station table */
120 	link_cmd = iwl4965_sta_alloc_lq(priv, sta_id);
121 	if (!link_cmd) {
122 		IWL_ERR(priv,
123 			"Unable to initialize rate scaling for station %pM.\n",
124 			addr);
125 		return -ENOMEM;
126 	}
127 
128 	ret = iwl_legacy_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
129 	if (ret)
130 		IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
131 
132 	spin_lock_irqsave(&priv->sta_lock, flags);
133 	priv->stations[sta_id].lq = link_cmd;
134 	spin_unlock_irqrestore(&priv->sta_lock, flags);
135 
136 	return 0;
137 }
138 
iwl4965_static_wepkey_cmd(struct iwl_priv * priv,struct iwl_rxon_context * ctx,bool send_if_empty)139 static int iwl4965_static_wepkey_cmd(struct iwl_priv *priv,
140 				      struct iwl_rxon_context *ctx,
141 				      bool send_if_empty)
142 {
143 	int i, not_empty = 0;
144 	u8 buff[sizeof(struct iwl_wep_cmd) +
145 		sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
146 	struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
147 	size_t cmd_size  = sizeof(struct iwl_wep_cmd);
148 	struct iwl_host_cmd cmd = {
149 		.id = ctx->wep_key_cmd,
150 		.data = wep_cmd,
151 		.flags = CMD_SYNC,
152 	};
153 
154 	might_sleep();
155 
156 	memset(wep_cmd, 0, cmd_size +
157 			(sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
158 
159 	for (i = 0; i < WEP_KEYS_MAX ; i++) {
160 		wep_cmd->key[i].key_index = i;
161 		if (ctx->wep_keys[i].key_size) {
162 			wep_cmd->key[i].key_offset = i;
163 			not_empty = 1;
164 		} else {
165 			wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
166 		}
167 
168 		wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
169 		memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
170 				ctx->wep_keys[i].key_size);
171 	}
172 
173 	wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
174 	wep_cmd->num_keys = WEP_KEYS_MAX;
175 
176 	cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
177 
178 	cmd.len = cmd_size;
179 
180 	if (not_empty || send_if_empty)
181 		return iwl_legacy_send_cmd(priv, &cmd);
182 	else
183 		return 0;
184 }
185 
iwl4965_restore_default_wep_keys(struct iwl_priv * priv,struct iwl_rxon_context * ctx)186 int iwl4965_restore_default_wep_keys(struct iwl_priv *priv,
187 				 struct iwl_rxon_context *ctx)
188 {
189 	lockdep_assert_held(&priv->mutex);
190 
191 	return iwl4965_static_wepkey_cmd(priv, ctx, false);
192 }
193 
iwl4965_remove_default_wep_key(struct iwl_priv * priv,struct iwl_rxon_context * ctx,struct ieee80211_key_conf * keyconf)194 int iwl4965_remove_default_wep_key(struct iwl_priv *priv,
195 			       struct iwl_rxon_context *ctx,
196 			       struct ieee80211_key_conf *keyconf)
197 {
198 	int ret;
199 
200 	lockdep_assert_held(&priv->mutex);
201 
202 	IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
203 		      keyconf->keyidx);
204 
205 	memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
206 	if (iwl_legacy_is_rfkill(priv)) {
207 		IWL_DEBUG_WEP(priv,
208 		"Not sending REPLY_WEPKEY command due to RFKILL.\n");
209 		/* but keys in device are clear anyway so return success */
210 		return 0;
211 	}
212 	ret = iwl4965_static_wepkey_cmd(priv, ctx, 1);
213 	IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
214 		      keyconf->keyidx, ret);
215 
216 	return ret;
217 }
218 
iwl4965_set_default_wep_key(struct iwl_priv * priv,struct iwl_rxon_context * ctx,struct ieee80211_key_conf * keyconf)219 int iwl4965_set_default_wep_key(struct iwl_priv *priv,
220 			    struct iwl_rxon_context *ctx,
221 			    struct ieee80211_key_conf *keyconf)
222 {
223 	int ret;
224 
225 	lockdep_assert_held(&priv->mutex);
226 
227 	if (keyconf->keylen != WEP_KEY_LEN_128 &&
228 	    keyconf->keylen != WEP_KEY_LEN_64) {
229 		IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
230 		return -EINVAL;
231 	}
232 
233 	keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
234 	keyconf->hw_key_idx = HW_KEY_DEFAULT;
235 	priv->stations[ctx->ap_sta_id].keyinfo.cipher = keyconf->cipher;
236 
237 	ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
238 	memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
239 							keyconf->keylen);
240 
241 	ret = iwl4965_static_wepkey_cmd(priv, ctx, false);
242 	IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
243 		keyconf->keylen, keyconf->keyidx, ret);
244 
245 	return ret;
246 }
247 
iwl4965_set_wep_dynamic_key_info(struct iwl_priv * priv,struct iwl_rxon_context * ctx,struct ieee80211_key_conf * keyconf,u8 sta_id)248 static int iwl4965_set_wep_dynamic_key_info(struct iwl_priv *priv,
249 					struct iwl_rxon_context *ctx,
250 					struct ieee80211_key_conf *keyconf,
251 					u8 sta_id)
252 {
253 	unsigned long flags;
254 	__le16 key_flags = 0;
255 	struct iwl_legacy_addsta_cmd sta_cmd;
256 
257 	lockdep_assert_held(&priv->mutex);
258 
259 	keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
260 
261 	key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
262 	key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
263 	key_flags &= ~STA_KEY_FLG_INVALID;
264 
265 	if (keyconf->keylen == WEP_KEY_LEN_128)
266 		key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
267 
268 	if (sta_id == ctx->bcast_sta_id)
269 		key_flags |= STA_KEY_MULTICAST_MSK;
270 
271 	spin_lock_irqsave(&priv->sta_lock, flags);
272 
273 	priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
274 	priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
275 	priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
276 
277 	memcpy(priv->stations[sta_id].keyinfo.key,
278 				keyconf->key, keyconf->keylen);
279 
280 	memcpy(&priv->stations[sta_id].sta.key.key[3],
281 				keyconf->key, keyconf->keylen);
282 
283 	if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
284 			== STA_KEY_FLG_NO_ENC)
285 		priv->stations[sta_id].sta.key.key_offset =
286 				 iwl_legacy_get_free_ucode_key_index(priv);
287 	/* else, we are overriding an existing key => no need to allocated room
288 	 * in uCode. */
289 
290 	WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
291 		"no space for a new key");
292 
293 	priv->stations[sta_id].sta.key.key_flags = key_flags;
294 	priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
295 	priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
296 
297 	memcpy(&sta_cmd, &priv->stations[sta_id].sta,
298 			sizeof(struct iwl_legacy_addsta_cmd));
299 	spin_unlock_irqrestore(&priv->sta_lock, flags);
300 
301 	return iwl_legacy_send_add_sta(priv, &sta_cmd, CMD_SYNC);
302 }
303 
iwl4965_set_ccmp_dynamic_key_info(struct iwl_priv * priv,struct iwl_rxon_context * ctx,struct ieee80211_key_conf * keyconf,u8 sta_id)304 static int iwl4965_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
305 					 struct iwl_rxon_context *ctx,
306 					 struct ieee80211_key_conf *keyconf,
307 					 u8 sta_id)
308 {
309 	unsigned long flags;
310 	__le16 key_flags = 0;
311 	struct iwl_legacy_addsta_cmd sta_cmd;
312 
313 	lockdep_assert_held(&priv->mutex);
314 
315 	key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
316 	key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
317 	key_flags &= ~STA_KEY_FLG_INVALID;
318 
319 	if (sta_id == ctx->bcast_sta_id)
320 		key_flags |= STA_KEY_MULTICAST_MSK;
321 
322 	keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
323 
324 	spin_lock_irqsave(&priv->sta_lock, flags);
325 	priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
326 	priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
327 
328 	memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
329 	       keyconf->keylen);
330 
331 	memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
332 	       keyconf->keylen);
333 
334 	if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
335 			== STA_KEY_FLG_NO_ENC)
336 		priv->stations[sta_id].sta.key.key_offset =
337 				 iwl_legacy_get_free_ucode_key_index(priv);
338 	/* else, we are overriding an existing key => no need to allocated room
339 	 * in uCode. */
340 
341 	WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
342 		"no space for a new key");
343 
344 	priv->stations[sta_id].sta.key.key_flags = key_flags;
345 	priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
346 	priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
347 
348 	memcpy(&sta_cmd, &priv->stations[sta_id].sta,
349 			 sizeof(struct iwl_legacy_addsta_cmd));
350 	spin_unlock_irqrestore(&priv->sta_lock, flags);
351 
352 	return iwl_legacy_send_add_sta(priv, &sta_cmd, CMD_SYNC);
353 }
354 
iwl4965_set_tkip_dynamic_key_info(struct iwl_priv * priv,struct iwl_rxon_context * ctx,struct ieee80211_key_conf * keyconf,u8 sta_id)355 static int iwl4965_set_tkip_dynamic_key_info(struct iwl_priv *priv,
356 					 struct iwl_rxon_context *ctx,
357 					 struct ieee80211_key_conf *keyconf,
358 					 u8 sta_id)
359 {
360 	unsigned long flags;
361 	int ret = 0;
362 	__le16 key_flags = 0;
363 
364 	key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
365 	key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
366 	key_flags &= ~STA_KEY_FLG_INVALID;
367 
368 	if (sta_id == ctx->bcast_sta_id)
369 		key_flags |= STA_KEY_MULTICAST_MSK;
370 
371 	keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
372 	keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
373 
374 	spin_lock_irqsave(&priv->sta_lock, flags);
375 
376 	priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
377 	priv->stations[sta_id].keyinfo.keylen = 16;
378 
379 	if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
380 			== STA_KEY_FLG_NO_ENC)
381 		priv->stations[sta_id].sta.key.key_offset =
382 				 iwl_legacy_get_free_ucode_key_index(priv);
383 	/* else, we are overriding an existing key => no need to allocated room
384 	 * in uCode. */
385 
386 	WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
387 		"no space for a new key");
388 
389 	priv->stations[sta_id].sta.key.key_flags = key_flags;
390 
391 
392 	/* This copy is acutally not needed: we get the key with each TX */
393 	memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
394 
395 	memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
396 
397 	spin_unlock_irqrestore(&priv->sta_lock, flags);
398 
399 	return ret;
400 }
401 
iwl4965_update_tkip_key(struct iwl_priv * priv,struct iwl_rxon_context * ctx,struct ieee80211_key_conf * keyconf,struct ieee80211_sta * sta,u32 iv32,u16 * phase1key)402 void iwl4965_update_tkip_key(struct iwl_priv *priv,
403 			 struct iwl_rxon_context *ctx,
404 			 struct ieee80211_key_conf *keyconf,
405 			 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
406 {
407 	u8 sta_id;
408 	unsigned long flags;
409 	int i;
410 
411 	if (iwl_legacy_scan_cancel(priv)) {
412 		/* cancel scan failed, just live w/ bad key and rely
413 		   briefly on SW decryption */
414 		return;
415 	}
416 
417 	sta_id = iwl_legacy_sta_id_or_broadcast(priv, ctx, sta);
418 	if (sta_id == IWL_INVALID_STATION)
419 		return;
420 
421 	spin_lock_irqsave(&priv->sta_lock, flags);
422 
423 	priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
424 
425 	for (i = 0; i < 5; i++)
426 		priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
427 			cpu_to_le16(phase1key[i]);
428 
429 	priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
430 	priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
431 
432 	iwl_legacy_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
433 
434 	spin_unlock_irqrestore(&priv->sta_lock, flags);
435 
436 }
437 
iwl4965_remove_dynamic_key(struct iwl_priv * priv,struct iwl_rxon_context * ctx,struct ieee80211_key_conf * keyconf,u8 sta_id)438 int iwl4965_remove_dynamic_key(struct iwl_priv *priv,
439 			   struct iwl_rxon_context *ctx,
440 			   struct ieee80211_key_conf *keyconf,
441 			   u8 sta_id)
442 {
443 	unsigned long flags;
444 	u16 key_flags;
445 	u8 keyidx;
446 	struct iwl_legacy_addsta_cmd sta_cmd;
447 
448 	lockdep_assert_held(&priv->mutex);
449 
450 	ctx->key_mapping_keys--;
451 
452 	spin_lock_irqsave(&priv->sta_lock, flags);
453 	key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
454 	keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
455 
456 	IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
457 		      keyconf->keyidx, sta_id);
458 
459 	if (keyconf->keyidx != keyidx) {
460 		/* We need to remove a key with index different that the one
461 		 * in the uCode. This means that the key we need to remove has
462 		 * been replaced by another one with different index.
463 		 * Don't do anything and return ok
464 		 */
465 		spin_unlock_irqrestore(&priv->sta_lock, flags);
466 		return 0;
467 	}
468 
469 	if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
470 		IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
471 			    keyconf->keyidx, key_flags);
472 		spin_unlock_irqrestore(&priv->sta_lock, flags);
473 		return 0;
474 	}
475 
476 	if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
477 		&priv->ucode_key_table))
478 		IWL_ERR(priv, "index %d not used in uCode key table.\n",
479 			priv->stations[sta_id].sta.key.key_offset);
480 	memset(&priv->stations[sta_id].keyinfo, 0,
481 					sizeof(struct iwl_hw_key));
482 	memset(&priv->stations[sta_id].sta.key, 0,
483 					sizeof(struct iwl4965_keyinfo));
484 	priv->stations[sta_id].sta.key.key_flags =
485 			STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
486 	priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
487 	priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
488 	priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
489 
490 	if (iwl_legacy_is_rfkill(priv)) {
491 		IWL_DEBUG_WEP(priv,
492 		 "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
493 		spin_unlock_irqrestore(&priv->sta_lock, flags);
494 		return 0;
495 	}
496 	memcpy(&sta_cmd, &priv->stations[sta_id].sta,
497 			sizeof(struct iwl_legacy_addsta_cmd));
498 	spin_unlock_irqrestore(&priv->sta_lock, flags);
499 
500 	return iwl_legacy_send_add_sta(priv, &sta_cmd, CMD_SYNC);
501 }
502 
iwl4965_set_dynamic_key(struct iwl_priv * priv,struct iwl_rxon_context * ctx,struct ieee80211_key_conf * keyconf,u8 sta_id)503 int iwl4965_set_dynamic_key(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
504 			struct ieee80211_key_conf *keyconf, u8 sta_id)
505 {
506 	int ret;
507 
508 	lockdep_assert_held(&priv->mutex);
509 
510 	ctx->key_mapping_keys++;
511 	keyconf->hw_key_idx = HW_KEY_DYNAMIC;
512 
513 	switch (keyconf->cipher) {
514 	case WLAN_CIPHER_SUITE_CCMP:
515 		ret = iwl4965_set_ccmp_dynamic_key_info(priv, ctx,
516 							keyconf, sta_id);
517 		break;
518 	case WLAN_CIPHER_SUITE_TKIP:
519 		ret = iwl4965_set_tkip_dynamic_key_info(priv, ctx,
520 							keyconf, sta_id);
521 		break;
522 	case WLAN_CIPHER_SUITE_WEP40:
523 	case WLAN_CIPHER_SUITE_WEP104:
524 		ret = iwl4965_set_wep_dynamic_key_info(priv, ctx,
525 							keyconf, sta_id);
526 		break;
527 	default:
528 		IWL_ERR(priv,
529 			"Unknown alg: %s cipher = %x\n", __func__,
530 			keyconf->cipher);
531 		ret = -EINVAL;
532 	}
533 
534 	IWL_DEBUG_WEP(priv,
535 		"Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
536 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
537 		      sta_id, ret);
538 
539 	return ret;
540 }
541 
542 /**
543  * iwl4965_alloc_bcast_station - add broadcast station into driver's station table.
544  *
545  * This adds the broadcast station into the driver's station table
546  * and marks it driver active, so that it will be restored to the
547  * device at the next best time.
548  */
iwl4965_alloc_bcast_station(struct iwl_priv * priv,struct iwl_rxon_context * ctx)549 int iwl4965_alloc_bcast_station(struct iwl_priv *priv,
550 			       struct iwl_rxon_context *ctx)
551 {
552 	struct iwl_link_quality_cmd *link_cmd;
553 	unsigned long flags;
554 	u8 sta_id;
555 
556 	spin_lock_irqsave(&priv->sta_lock, flags);
557 	sta_id = iwl_legacy_prep_station(priv, ctx, iwlegacy_bcast_addr,
558 								false, NULL);
559 	if (sta_id == IWL_INVALID_STATION) {
560 		IWL_ERR(priv, "Unable to prepare broadcast station\n");
561 		spin_unlock_irqrestore(&priv->sta_lock, flags);
562 
563 		return -EINVAL;
564 	}
565 
566 	priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
567 	priv->stations[sta_id].used |= IWL_STA_BCAST;
568 	spin_unlock_irqrestore(&priv->sta_lock, flags);
569 
570 	link_cmd = iwl4965_sta_alloc_lq(priv, sta_id);
571 	if (!link_cmd) {
572 		IWL_ERR(priv,
573 			"Unable to initialize rate scaling for bcast station.\n");
574 		return -ENOMEM;
575 	}
576 
577 	spin_lock_irqsave(&priv->sta_lock, flags);
578 	priv->stations[sta_id].lq = link_cmd;
579 	spin_unlock_irqrestore(&priv->sta_lock, flags);
580 
581 	return 0;
582 }
583 
584 /**
585  * iwl4965_update_bcast_station - update broadcast station's LQ command
586  *
587  * Only used by iwl4965. Placed here to have all bcast station management
588  * code together.
589  */
iwl4965_update_bcast_station(struct iwl_priv * priv,struct iwl_rxon_context * ctx)590 static int iwl4965_update_bcast_station(struct iwl_priv *priv,
591 				    struct iwl_rxon_context *ctx)
592 {
593 	unsigned long flags;
594 	struct iwl_link_quality_cmd *link_cmd;
595 	u8 sta_id = ctx->bcast_sta_id;
596 
597 	link_cmd = iwl4965_sta_alloc_lq(priv, sta_id);
598 	if (!link_cmd) {
599 		IWL_ERR(priv,
600 		"Unable to initialize rate scaling for bcast station.\n");
601 		return -ENOMEM;
602 	}
603 
604 	spin_lock_irqsave(&priv->sta_lock, flags);
605 	if (priv->stations[sta_id].lq)
606 		kfree(priv->stations[sta_id].lq);
607 	else
608 		IWL_DEBUG_INFO(priv,
609 		"Bcast station rate scaling has not been initialized yet.\n");
610 	priv->stations[sta_id].lq = link_cmd;
611 	spin_unlock_irqrestore(&priv->sta_lock, flags);
612 
613 	return 0;
614 }
615 
iwl4965_update_bcast_stations(struct iwl_priv * priv)616 int iwl4965_update_bcast_stations(struct iwl_priv *priv)
617 {
618 	struct iwl_rxon_context *ctx;
619 	int ret = 0;
620 
621 	for_each_context(priv, ctx) {
622 		ret = iwl4965_update_bcast_station(priv, ctx);
623 		if (ret)
624 			break;
625 	}
626 
627 	return ret;
628 }
629 
630 /**
631  * iwl4965_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
632  */
iwl4965_sta_tx_modify_enable_tid(struct iwl_priv * priv,int sta_id,int tid)633 int iwl4965_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
634 {
635 	unsigned long flags;
636 	struct iwl_legacy_addsta_cmd sta_cmd;
637 
638 	lockdep_assert_held(&priv->mutex);
639 
640 	/* Remove "disable" flag, to enable Tx for this TID */
641 	spin_lock_irqsave(&priv->sta_lock, flags);
642 	priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
643 	priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
644 	priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
645 	memcpy(&sta_cmd, &priv->stations[sta_id].sta,
646 					sizeof(struct iwl_legacy_addsta_cmd));
647 	spin_unlock_irqrestore(&priv->sta_lock, flags);
648 
649 	return iwl_legacy_send_add_sta(priv, &sta_cmd, CMD_SYNC);
650 }
651 
iwl4965_sta_rx_agg_start(struct iwl_priv * priv,struct ieee80211_sta * sta,int tid,u16 ssn)652 int iwl4965_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
653 			 int tid, u16 ssn)
654 {
655 	unsigned long flags;
656 	int sta_id;
657 	struct iwl_legacy_addsta_cmd sta_cmd;
658 
659 	lockdep_assert_held(&priv->mutex);
660 
661 	sta_id = iwl_legacy_sta_id(sta);
662 	if (sta_id == IWL_INVALID_STATION)
663 		return -ENXIO;
664 
665 	spin_lock_irqsave(&priv->sta_lock, flags);
666 	priv->stations[sta_id].sta.station_flags_msk = 0;
667 	priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
668 	priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
669 	priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
670 	priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
671 	memcpy(&sta_cmd, &priv->stations[sta_id].sta,
672 					sizeof(struct iwl_legacy_addsta_cmd));
673 	spin_unlock_irqrestore(&priv->sta_lock, flags);
674 
675 	return iwl_legacy_send_add_sta(priv, &sta_cmd, CMD_SYNC);
676 }
677 
iwl4965_sta_rx_agg_stop(struct iwl_priv * priv,struct ieee80211_sta * sta,int tid)678 int iwl4965_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
679 			int tid)
680 {
681 	unsigned long flags;
682 	int sta_id;
683 	struct iwl_legacy_addsta_cmd sta_cmd;
684 
685 	lockdep_assert_held(&priv->mutex);
686 
687 	sta_id = iwl_legacy_sta_id(sta);
688 	if (sta_id == IWL_INVALID_STATION) {
689 		IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
690 		return -ENXIO;
691 	}
692 
693 	spin_lock_irqsave(&priv->sta_lock, flags);
694 	priv->stations[sta_id].sta.station_flags_msk = 0;
695 	priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
696 	priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
697 	priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
698 	memcpy(&sta_cmd, &priv->stations[sta_id].sta,
699 				sizeof(struct iwl_legacy_addsta_cmd));
700 	spin_unlock_irqrestore(&priv->sta_lock, flags);
701 
702 	return iwl_legacy_send_add_sta(priv, &sta_cmd, CMD_SYNC);
703 }
704 
705 void
iwl4965_sta_modify_sleep_tx_count(struct iwl_priv * priv,int sta_id,int cnt)706 iwl4965_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
707 {
708 	unsigned long flags;
709 
710 	spin_lock_irqsave(&priv->sta_lock, flags);
711 	priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
712 	priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
713 	priv->stations[sta_id].sta.sta.modify_mask =
714 					STA_MODIFY_SLEEP_TX_COUNT_MSK;
715 	priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
716 	priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
717 	iwl_legacy_send_add_sta(priv,
718 				&priv->stations[sta_id].sta, CMD_ASYNC);
719 	spin_unlock_irqrestore(&priv->sta_lock, flags);
720 
721 }
722