1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2014 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  * The full GNU General Public License is included in this distribution in the
18  * file called LICENSE.
19  *
20  * Contact Information:
21  *  Intel Linux Wireless <linuxwifi@intel.com>
22  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
23  *
24  *****************************************************************************/
25 #include <linux/etherdevice.h>
26 #include <net/mac80211.h>
27 #include "iwl-trans.h"
28 #include "dev.h"
29 #include "agn.h"
30 
31 const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
32 
33 static int iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
34 {
35 	lockdep_assert_held(&priv->sta_lock);
36 
37 	if (sta_id >= IWLAGN_STATION_COUNT) {
38 		IWL_ERR(priv, "invalid sta_id %u\n", sta_id);
39 		return -EINVAL;
40 	}
41 	if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
42 		IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u "
43 			"addr %pM\n",
44 			sta_id, priv->stations[sta_id].sta.sta.addr);
45 
46 	if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
47 		IWL_DEBUG_ASSOC(priv,
48 				"STA id %u addr %pM already present in uCode "
49 				"(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 	return 0;
57 }
58 
59 static void iwl_process_add_sta_resp(struct iwl_priv *priv,
60 				     struct iwl_rx_packet *pkt)
61 {
62 	struct iwl_add_sta_resp *add_sta_resp = (void *)pkt->data;
63 
64 	IWL_DEBUG_INFO(priv, "Processing response for adding station\n");
65 
66 	spin_lock_bh(&priv->sta_lock);
67 
68 	switch (add_sta_resp->status) {
69 	case ADD_STA_SUCCESS_MSK:
70 		IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
71 		break;
72 	case ADD_STA_NO_ROOM_IN_TABLE:
73 		IWL_ERR(priv, "Adding station failed, no room in table.\n");
74 		break;
75 	case ADD_STA_NO_BLOCK_ACK_RESOURCE:
76 		IWL_ERR(priv,
77 			"Adding station failed, no block ack resource.\n");
78 		break;
79 	case ADD_STA_MODIFY_NON_EXIST_STA:
80 		IWL_ERR(priv, "Attempting to modify non-existing station\n");
81 		break;
82 	default:
83 		IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
84 				add_sta_resp->status);
85 		break;
86 	}
87 
88 	spin_unlock_bh(&priv->sta_lock);
89 }
90 
91 void iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb)
92 {
93 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
94 
95 	iwl_process_add_sta_resp(priv, pkt);
96 }
97 
98 int iwl_send_add_sta(struct iwl_priv *priv,
99 		     struct iwl_addsta_cmd *sta, u8 flags)
100 {
101 	int ret = 0;
102 	struct iwl_host_cmd cmd = {
103 		.id = REPLY_ADD_STA,
104 		.flags = flags,
105 		.data = { sta, },
106 		.len = { sizeof(*sta), },
107 	};
108 	u8 sta_id __maybe_unused = sta->sta.sta_id;
109 	struct iwl_rx_packet *pkt;
110 	struct iwl_add_sta_resp *add_sta_resp;
111 
112 	IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
113 		       sta_id, sta->sta.addr, flags & CMD_ASYNC ?  "a" : "");
114 
115 	if (!(flags & CMD_ASYNC)) {
116 		cmd.flags |= CMD_WANT_SKB;
117 		might_sleep();
118 	}
119 
120 	ret = iwl_dvm_send_cmd(priv, &cmd);
121 
122 	if (ret || (flags & CMD_ASYNC))
123 		return ret;
124 
125 	pkt = cmd.resp_pkt;
126 	add_sta_resp = (void *)pkt->data;
127 
128 	/* debug messages are printed in the handler */
129 	if (add_sta_resp->status == ADD_STA_SUCCESS_MSK) {
130 		spin_lock_bh(&priv->sta_lock);
131 		ret = iwl_sta_ucode_activate(priv, sta_id);
132 		spin_unlock_bh(&priv->sta_lock);
133 	} else {
134 		ret = -EIO;
135 	}
136 
137 	iwl_free_resp(&cmd);
138 
139 	return ret;
140 }
141 
142 bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
143 			    struct iwl_rxon_context *ctx,
144 			    struct ieee80211_sta *sta)
145 {
146 	if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
147 		return false;
148 
149 #ifdef CONFIG_IWLWIFI_DEBUGFS
150 	if (priv->disable_ht40)
151 		return false;
152 #endif
153 
154 	/* special case for RXON */
155 	if (!sta)
156 		return true;
157 
158 	return sta->bandwidth >= IEEE80211_STA_RX_BW_40;
159 }
160 
161 static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
162 				  struct ieee80211_sta *sta,
163 				  struct iwl_rxon_context *ctx,
164 				  __le32 *flags, __le32 *mask)
165 {
166 	struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
167 
168 	*mask = STA_FLG_RTS_MIMO_PROT_MSK |
169 		STA_FLG_MIMO_DIS_MSK |
170 		STA_FLG_HT40_EN_MSK |
171 		STA_FLG_MAX_AGG_SIZE_MSK |
172 		STA_FLG_AGG_MPDU_DENSITY_MSK;
173 	*flags = 0;
174 
175 	if (!sta || !sta_ht_inf->ht_supported)
176 		return;
177 
178 	IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",
179 			sta->addr,
180 			(sta->smps_mode == IEEE80211_SMPS_STATIC) ?
181 			"static" :
182 			(sta->smps_mode == IEEE80211_SMPS_DYNAMIC) ?
183 			"dynamic" : "disabled");
184 
185 	switch (sta->smps_mode) {
186 	case IEEE80211_SMPS_STATIC:
187 		*flags |= STA_FLG_MIMO_DIS_MSK;
188 		break;
189 	case IEEE80211_SMPS_DYNAMIC:
190 		*flags |= STA_FLG_RTS_MIMO_PROT_MSK;
191 		break;
192 	case IEEE80211_SMPS_OFF:
193 		break;
194 	default:
195 		IWL_WARN(priv, "Invalid MIMO PS mode %d\n", sta->smps_mode);
196 		break;
197 	}
198 
199 	*flags |= cpu_to_le32(
200 		(u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
201 
202 	*flags |= cpu_to_le32(
203 		(u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
204 
205 	if (iwl_is_ht40_tx_allowed(priv, ctx, sta))
206 		*flags |= STA_FLG_HT40_EN_MSK;
207 }
208 
209 int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
210 		      struct ieee80211_sta *sta)
211 {
212 	u8 sta_id = iwl_sta_id(sta);
213 	__le32 flags, mask;
214 	struct iwl_addsta_cmd cmd;
215 
216 	if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
217 		return -EINVAL;
218 
219 	iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
220 
221 	spin_lock_bh(&priv->sta_lock);
222 	priv->stations[sta_id].sta.station_flags &= ~mask;
223 	priv->stations[sta_id].sta.station_flags |= flags;
224 	spin_unlock_bh(&priv->sta_lock);
225 
226 	memset(&cmd, 0, sizeof(cmd));
227 	cmd.mode = STA_CONTROL_MODIFY_MSK;
228 	cmd.station_flags_msk = mask;
229 	cmd.station_flags = flags;
230 	cmd.sta.sta_id = sta_id;
231 
232 	return iwl_send_add_sta(priv, &cmd, 0);
233 }
234 
235 static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
236 				   struct ieee80211_sta *sta,
237 				   struct iwl_rxon_context *ctx)
238 {
239 	__le32 flags, mask;
240 
241 	iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
242 
243 	lockdep_assert_held(&priv->sta_lock);
244 	priv->stations[index].sta.station_flags &= ~mask;
245 	priv->stations[index].sta.station_flags |= flags;
246 }
247 
248 /**
249  * iwl_prep_station - Prepare station information for addition
250  *
251  * should be called with sta_lock held
252  */
253 u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
254 		    const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
255 {
256 	struct iwl_station_entry *station;
257 	int i;
258 	u8 sta_id = IWL_INVALID_STATION;
259 
260 	if (is_ap)
261 		sta_id = ctx->ap_sta_id;
262 	else if (is_broadcast_ether_addr(addr))
263 		sta_id = ctx->bcast_sta_id;
264 	else
265 		for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
266 			if (ether_addr_equal(priv->stations[i].sta.sta.addr,
267 					     addr)) {
268 				sta_id = i;
269 				break;
270 			}
271 
272 			if (!priv->stations[i].used &&
273 			    sta_id == IWL_INVALID_STATION)
274 				sta_id = i;
275 		}
276 
277 	/*
278 	 * These two conditions have the same outcome, but keep them
279 	 * separate
280 	 */
281 	if (unlikely(sta_id == IWL_INVALID_STATION))
282 		return sta_id;
283 
284 	/*
285 	 * uCode is not able to deal with multiple requests to add a
286 	 * station. Keep track if one is in progress so that we do not send
287 	 * another.
288 	 */
289 	if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
290 		IWL_DEBUG_INFO(priv, "STA %d already in process of being "
291 			       "added.\n", sta_id);
292 		return sta_id;
293 	}
294 
295 	if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
296 	    (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
297 	    ether_addr_equal(priv->stations[sta_id].sta.sta.addr, addr)) {
298 		IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
299 				"adding again.\n", sta_id, addr);
300 		return sta_id;
301 	}
302 
303 	station = &priv->stations[sta_id];
304 	station->used = IWL_STA_DRIVER_ACTIVE;
305 	IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
306 			sta_id, addr);
307 	priv->num_stations++;
308 
309 	/* Set up the REPLY_ADD_STA command to send to device */
310 	memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
311 	memcpy(station->sta.sta.addr, addr, ETH_ALEN);
312 	station->sta.mode = 0;
313 	station->sta.sta.sta_id = sta_id;
314 	station->sta.station_flags = ctx->station_flags;
315 	station->ctxid = ctx->ctxid;
316 
317 	if (sta) {
318 		struct iwl_station_priv *sta_priv;
319 
320 		sta_priv = (void *)sta->drv_priv;
321 		sta_priv->ctx = ctx;
322 	}
323 
324 	/*
325 	 * OK to call unconditionally, since local stations (IBSS BSSID
326 	 * STA and broadcast STA) pass in a NULL sta, and mac80211
327 	 * doesn't allow HT IBSS.
328 	 */
329 	iwl_set_ht_add_station(priv, sta_id, sta, ctx);
330 
331 	return sta_id;
332 
333 }
334 
335 #define STA_WAIT_TIMEOUT (HZ/2)
336 
337 /**
338  * iwl_add_station_common -
339  */
340 int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
341 			   const u8 *addr, bool is_ap,
342 			   struct ieee80211_sta *sta, u8 *sta_id_r)
343 {
344 	int ret = 0;
345 	u8 sta_id;
346 	struct iwl_addsta_cmd sta_cmd;
347 
348 	*sta_id_r = 0;
349 	spin_lock_bh(&priv->sta_lock);
350 	sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
351 	if (sta_id == IWL_INVALID_STATION) {
352 		IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
353 			addr);
354 		spin_unlock_bh(&priv->sta_lock);
355 		return -EINVAL;
356 	}
357 
358 	/*
359 	 * uCode is not able to deal with multiple requests to add a
360 	 * station. Keep track if one is in progress so that we do not send
361 	 * another.
362 	 */
363 	if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
364 		IWL_DEBUG_INFO(priv, "STA %d already in process of being "
365 			       "added.\n", sta_id);
366 		spin_unlock_bh(&priv->sta_lock);
367 		return -EEXIST;
368 	}
369 
370 	if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
371 	    (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
372 		IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
373 				"adding again.\n", sta_id, addr);
374 		spin_unlock_bh(&priv->sta_lock);
375 		return -EEXIST;
376 	}
377 
378 	priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
379 	memcpy(&sta_cmd, &priv->stations[sta_id].sta,
380 	       sizeof(struct iwl_addsta_cmd));
381 	spin_unlock_bh(&priv->sta_lock);
382 
383 	/* Add station to device's station table */
384 	ret = iwl_send_add_sta(priv, &sta_cmd, 0);
385 	if (ret) {
386 		spin_lock_bh(&priv->sta_lock);
387 		IWL_ERR(priv, "Adding station %pM failed.\n",
388 			priv->stations[sta_id].sta.sta.addr);
389 		priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
390 		priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
391 		spin_unlock_bh(&priv->sta_lock);
392 	}
393 	*sta_id_r = sta_id;
394 	return ret;
395 }
396 
397 /**
398  * iwl_sta_ucode_deactivate - deactivate ucode status for a station
399  */
400 static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
401 {
402 	lockdep_assert_held(&priv->sta_lock);
403 
404 	/* Ucode must be active and driver must be non active */
405 	if ((priv->stations[sta_id].used &
406 	     (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
407 	      IWL_STA_UCODE_ACTIVE)
408 		IWL_ERR(priv, "removed non active STA %u\n", sta_id);
409 
410 	priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
411 
412 	memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
413 	IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
414 }
415 
416 static int iwl_send_remove_station(struct iwl_priv *priv,
417 				   const u8 *addr, int sta_id,
418 				   bool temporary)
419 {
420 	struct iwl_rx_packet *pkt;
421 	int ret;
422 	struct iwl_rem_sta_cmd rm_sta_cmd;
423 	struct iwl_rem_sta_resp *rem_sta_resp;
424 
425 	struct iwl_host_cmd cmd = {
426 		.id = REPLY_REMOVE_STA,
427 		.len = { sizeof(struct iwl_rem_sta_cmd), },
428 		.data = { &rm_sta_cmd, },
429 	};
430 
431 	memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
432 	rm_sta_cmd.num_sta = 1;
433 	memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
434 
435 	cmd.flags |= CMD_WANT_SKB;
436 
437 	ret = iwl_dvm_send_cmd(priv, &cmd);
438 
439 	if (ret)
440 		return ret;
441 
442 	pkt = cmd.resp_pkt;
443 	rem_sta_resp = (void *)pkt->data;
444 
445 	switch (rem_sta_resp->status) {
446 	case REM_STA_SUCCESS_MSK:
447 		if (!temporary) {
448 			spin_lock_bh(&priv->sta_lock);
449 			iwl_sta_ucode_deactivate(priv, sta_id);
450 			spin_unlock_bh(&priv->sta_lock);
451 		}
452 		IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
453 		break;
454 	default:
455 		ret = -EIO;
456 		IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
457 		break;
458 	}
459 
460 	iwl_free_resp(&cmd);
461 
462 	return ret;
463 }
464 
465 /**
466  * iwl_remove_station - Remove driver's knowledge of station.
467  */
468 int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
469 		       const u8 *addr)
470 {
471 	u8 tid;
472 
473 	if (!iwl_is_ready(priv)) {
474 		IWL_DEBUG_INFO(priv,
475 			"Unable to remove station %pM, device not ready.\n",
476 			addr);
477 		/*
478 		 * It is typical for stations to be removed when we are
479 		 * going down. Return success since device will be down
480 		 * soon anyway
481 		 */
482 		return 0;
483 	}
484 
485 	IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d  %pM\n",
486 			sta_id, addr);
487 
488 	if (WARN_ON(sta_id == IWL_INVALID_STATION))
489 		return -EINVAL;
490 
491 	spin_lock_bh(&priv->sta_lock);
492 
493 	if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
494 		IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
495 				addr);
496 		goto out_err;
497 	}
498 
499 	if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
500 		IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
501 				addr);
502 		goto out_err;
503 	}
504 
505 	if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
506 		kfree(priv->stations[sta_id].lq);
507 		priv->stations[sta_id].lq = NULL;
508 	}
509 
510 	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
511 		memset(&priv->tid_data[sta_id][tid], 0,
512 			sizeof(priv->tid_data[sta_id][tid]));
513 
514 	priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
515 
516 	priv->num_stations--;
517 
518 	if (WARN_ON(priv->num_stations < 0))
519 		priv->num_stations = 0;
520 
521 	spin_unlock_bh(&priv->sta_lock);
522 
523 	return iwl_send_remove_station(priv, addr, sta_id, false);
524 out_err:
525 	spin_unlock_bh(&priv->sta_lock);
526 	return -EINVAL;
527 }
528 
529 void iwl_deactivate_station(struct iwl_priv *priv, const u8 sta_id,
530 			    const u8 *addr)
531 {
532 	u8 tid;
533 
534 	if (!iwl_is_ready(priv)) {
535 		IWL_DEBUG_INFO(priv,
536 			"Unable to remove station %pM, device not ready.\n",
537 			addr);
538 		return;
539 	}
540 
541 	IWL_DEBUG_ASSOC(priv, "Deactivating STA: %pM (%d)\n", addr, sta_id);
542 
543 	if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
544 		return;
545 
546 	spin_lock_bh(&priv->sta_lock);
547 
548 	WARN_ON_ONCE(!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE));
549 
550 	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
551 		memset(&priv->tid_data[sta_id][tid], 0,
552 			sizeof(priv->tid_data[sta_id][tid]));
553 
554 	priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
555 	priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
556 
557 	priv->num_stations--;
558 
559 	if (WARN_ON_ONCE(priv->num_stations < 0))
560 		priv->num_stations = 0;
561 
562 	spin_unlock_bh(&priv->sta_lock);
563 }
564 
565 static void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
566 			    u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
567 {
568 	int i, r;
569 	u32 rate_flags = 0;
570 	__le32 rate_n_flags;
571 
572 	lockdep_assert_held(&priv->mutex);
573 
574 	memset(link_cmd, 0, sizeof(*link_cmd));
575 
576 	/* Set up the rate scaling to start at selected rate, fall back
577 	 * all the way down to 1M in IEEE order, and then spin on 1M */
578 	if (priv->band == NL80211_BAND_5GHZ)
579 		r = IWL_RATE_6M_INDEX;
580 	else if (ctx && ctx->vif && ctx->vif->p2p)
581 		r = IWL_RATE_6M_INDEX;
582 	else
583 		r = IWL_RATE_1M_INDEX;
584 
585 	if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
586 		rate_flags |= RATE_MCS_CCK_MSK;
587 
588 	rate_flags |= first_antenna(priv->nvm_data->valid_tx_ant) <<
589 				RATE_MCS_ANT_POS;
590 	rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
591 	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
592 		link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
593 
594 	link_cmd->general_params.single_stream_ant_msk =
595 			first_antenna(priv->nvm_data->valid_tx_ant);
596 
597 	link_cmd->general_params.dual_stream_ant_msk =
598 		priv->nvm_data->valid_tx_ant &
599 		~first_antenna(priv->nvm_data->valid_tx_ant);
600 	if (!link_cmd->general_params.dual_stream_ant_msk) {
601 		link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
602 	} else if (num_of_ant(priv->nvm_data->valid_tx_ant) == 2) {
603 		link_cmd->general_params.dual_stream_ant_msk =
604 			priv->nvm_data->valid_tx_ant;
605 	}
606 
607 	link_cmd->agg_params.agg_dis_start_th =
608 		LINK_QUAL_AGG_DISABLE_START_DEF;
609 	link_cmd->agg_params.agg_time_limit =
610 		cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
611 
612 	link_cmd->sta_id = sta_id;
613 }
614 
615 /**
616  * iwl_clear_ucode_stations - clear ucode station table bits
617  *
618  * This function clears all the bits in the driver indicating
619  * which stations are active in the ucode. Call when something
620  * other than explicit station management would cause this in
621  * the ucode, e.g. unassociated RXON.
622  */
623 void iwl_clear_ucode_stations(struct iwl_priv *priv,
624 			      struct iwl_rxon_context *ctx)
625 {
626 	int i;
627 	bool cleared = false;
628 
629 	IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
630 
631 	spin_lock_bh(&priv->sta_lock);
632 	for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
633 		if (ctx && ctx->ctxid != priv->stations[i].ctxid)
634 			continue;
635 
636 		if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
637 			IWL_DEBUG_INFO(priv,
638 				"Clearing ucode active for station %d\n", i);
639 			priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
640 			cleared = true;
641 		}
642 	}
643 	spin_unlock_bh(&priv->sta_lock);
644 
645 	if (!cleared)
646 		IWL_DEBUG_INFO(priv,
647 			       "No active stations found to be cleared\n");
648 }
649 
650 /**
651  * iwl_restore_stations() - Restore driver known stations to device
652  *
653  * All stations considered active by driver, but not present in ucode, is
654  * restored.
655  *
656  * Function sleeps.
657  */
658 void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
659 {
660 	struct iwl_addsta_cmd sta_cmd;
661 	static const struct iwl_link_quality_cmd zero_lq = {};
662 	struct iwl_link_quality_cmd lq;
663 	int i;
664 	bool found = false;
665 	int ret;
666 	bool send_lq;
667 
668 	if (!iwl_is_ready(priv)) {
669 		IWL_DEBUG_INFO(priv,
670 			       "Not ready yet, not restoring any stations.\n");
671 		return;
672 	}
673 
674 	IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
675 	spin_lock_bh(&priv->sta_lock);
676 	for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
677 		if (ctx->ctxid != priv->stations[i].ctxid)
678 			continue;
679 		if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
680 			    !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
681 			IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
682 					priv->stations[i].sta.sta.addr);
683 			priv->stations[i].sta.mode = 0;
684 			priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
685 			found = true;
686 		}
687 	}
688 
689 	for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
690 		if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
691 			memcpy(&sta_cmd, &priv->stations[i].sta,
692 			       sizeof(struct iwl_addsta_cmd));
693 			send_lq = false;
694 			if (priv->stations[i].lq) {
695 				if (priv->wowlan)
696 					iwl_sta_fill_lq(priv, ctx, i, &lq);
697 				else
698 					memcpy(&lq, priv->stations[i].lq,
699 					       sizeof(struct iwl_link_quality_cmd));
700 
701 				if (memcmp(&lq, &zero_lq, sizeof(lq)))
702 					send_lq = true;
703 			}
704 			spin_unlock_bh(&priv->sta_lock);
705 			ret = iwl_send_add_sta(priv, &sta_cmd, 0);
706 			if (ret) {
707 				spin_lock_bh(&priv->sta_lock);
708 				IWL_ERR(priv, "Adding station %pM failed.\n",
709 					priv->stations[i].sta.sta.addr);
710 				priv->stations[i].used &=
711 						~IWL_STA_DRIVER_ACTIVE;
712 				priv->stations[i].used &=
713 						~IWL_STA_UCODE_INPROGRESS;
714 				continue;
715 			}
716 			/*
717 			 * Rate scaling has already been initialized, send
718 			 * current LQ command
719 			 */
720 			if (send_lq)
721 				iwl_send_lq_cmd(priv, ctx, &lq, 0, true);
722 			spin_lock_bh(&priv->sta_lock);
723 			priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
724 		}
725 	}
726 
727 	spin_unlock_bh(&priv->sta_lock);
728 	if (!found)
729 		IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
730 			"no stations to be restored.\n");
731 	else
732 		IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
733 			"complete.\n");
734 }
735 
736 int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
737 {
738 	int i;
739 
740 	for (i = 0; i < priv->sta_key_max_num; i++)
741 		if (!test_and_set_bit(i, &priv->ucode_key_table))
742 			return i;
743 
744 	return WEP_INVALID_OFFSET;
745 }
746 
747 void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
748 {
749 	int i;
750 
751 	spin_lock_bh(&priv->sta_lock);
752 	for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
753 		if (!(priv->stations[i].used & IWL_STA_BCAST))
754 			continue;
755 
756 		priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
757 		priv->num_stations--;
758 		if (WARN_ON(priv->num_stations < 0))
759 			priv->num_stations = 0;
760 		kfree(priv->stations[i].lq);
761 		priv->stations[i].lq = NULL;
762 	}
763 	spin_unlock_bh(&priv->sta_lock);
764 }
765 
766 #ifdef CONFIG_IWLWIFI_DEBUG
767 static void iwl_dump_lq_cmd(struct iwl_priv *priv,
768 			   struct iwl_link_quality_cmd *lq)
769 {
770 	int i;
771 	IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
772 	IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
773 		       lq->general_params.single_stream_ant_msk,
774 		       lq->general_params.dual_stream_ant_msk);
775 
776 	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
777 		IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
778 			       i, lq->rs_table[i].rate_n_flags);
779 }
780 #else
781 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
782 				   struct iwl_link_quality_cmd *lq)
783 {
784 }
785 #endif
786 
787 /**
788  * is_lq_table_valid() - Test one aspect of LQ cmd for validity
789  *
790  * It sometimes happens when a HT rate has been in use and we
791  * loose connectivity with AP then mac80211 will first tell us that the
792  * current channel is not HT anymore before removing the station. In such a
793  * scenario the RXON flags will be updated to indicate we are not
794  * communicating HT anymore, but the LQ command may still contain HT rates.
795  * Test for this to prevent driver from sending LQ command between the time
796  * RXON flags are updated and when LQ command is updated.
797  */
798 static bool is_lq_table_valid(struct iwl_priv *priv,
799 			      struct iwl_rxon_context *ctx,
800 			      struct iwl_link_quality_cmd *lq)
801 {
802 	int i;
803 
804 	if (ctx->ht.enabled)
805 		return true;
806 
807 	IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
808 		       ctx->active.channel);
809 	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
810 		if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
811 		    RATE_MCS_HT_MSK) {
812 			IWL_DEBUG_INFO(priv,
813 				       "index %d of LQ expects HT channel\n",
814 				       i);
815 			return false;
816 		}
817 	}
818 	return true;
819 }
820 
821 /**
822  * iwl_send_lq_cmd() - Send link quality command
823  * @init: This command is sent as part of station initialization right
824  *        after station has been added.
825  *
826  * The link quality command is sent as the last step of station creation.
827  * This is the special case in which init is set and we call a callback in
828  * this case to clear the state indicating that station creation is in
829  * progress.
830  */
831 int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
832 		    struct iwl_link_quality_cmd *lq, u8 flags, bool init)
833 {
834 	int ret = 0;
835 	struct iwl_host_cmd cmd = {
836 		.id = REPLY_TX_LINK_QUALITY_CMD,
837 		.len = { sizeof(struct iwl_link_quality_cmd), },
838 		.flags = flags,
839 		.data = { lq, },
840 	};
841 
842 	if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
843 		return -EINVAL;
844 
845 
846 	spin_lock_bh(&priv->sta_lock);
847 	if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
848 		spin_unlock_bh(&priv->sta_lock);
849 		return -EINVAL;
850 	}
851 	spin_unlock_bh(&priv->sta_lock);
852 
853 	iwl_dump_lq_cmd(priv, lq);
854 	if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
855 		return -EINVAL;
856 
857 	if (is_lq_table_valid(priv, ctx, lq))
858 		ret = iwl_dvm_send_cmd(priv, &cmd);
859 	else
860 		ret = -EINVAL;
861 
862 	if (cmd.flags & CMD_ASYNC)
863 		return ret;
864 
865 	if (init) {
866 		IWL_DEBUG_INFO(priv, "init LQ command complete, "
867 			       "clearing sta addition status for sta %d\n",
868 			       lq->sta_id);
869 		spin_lock_bh(&priv->sta_lock);
870 		priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
871 		spin_unlock_bh(&priv->sta_lock);
872 	}
873 	return ret;
874 }
875 
876 
877 static struct iwl_link_quality_cmd *
878 iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
879 		 u8 sta_id)
880 {
881 	struct iwl_link_quality_cmd *link_cmd;
882 
883 	link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
884 	if (!link_cmd) {
885 		IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
886 		return NULL;
887 	}
888 
889 	iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
890 
891 	return link_cmd;
892 }
893 
894 /*
895  * iwlagn_add_bssid_station - Add the special IBSS BSSID station
896  *
897  * Function sleeps.
898  */
899 int iwlagn_add_bssid_station(struct iwl_priv *priv,
900 			     struct iwl_rxon_context *ctx,
901 			     const u8 *addr, u8 *sta_id_r)
902 {
903 	int ret;
904 	u8 sta_id;
905 	struct iwl_link_quality_cmd *link_cmd;
906 
907 	if (sta_id_r)
908 		*sta_id_r = IWL_INVALID_STATION;
909 
910 	ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
911 	if (ret) {
912 		IWL_ERR(priv, "Unable to add station %pM\n", addr);
913 		return ret;
914 	}
915 
916 	if (sta_id_r)
917 		*sta_id_r = sta_id;
918 
919 	spin_lock_bh(&priv->sta_lock);
920 	priv->stations[sta_id].used |= IWL_STA_LOCAL;
921 	spin_unlock_bh(&priv->sta_lock);
922 
923 	/* Set up default rate scaling table in device's station table */
924 	link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
925 	if (!link_cmd) {
926 		IWL_ERR(priv,
927 			"Unable to initialize rate scaling for station %pM.\n",
928 			addr);
929 		return -ENOMEM;
930 	}
931 
932 	ret = iwl_send_lq_cmd(priv, ctx, link_cmd, 0, true);
933 	if (ret)
934 		IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
935 
936 	spin_lock_bh(&priv->sta_lock);
937 	priv->stations[sta_id].lq = link_cmd;
938 	spin_unlock_bh(&priv->sta_lock);
939 
940 	return 0;
941 }
942 
943 /*
944  * static WEP keys
945  *
946  * For each context, the device has a table of 4 static WEP keys
947  * (one for each key index) that is updated with the following
948  * commands.
949  */
950 
951 static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
952 				      struct iwl_rxon_context *ctx,
953 				      bool send_if_empty)
954 {
955 	int i, not_empty = 0;
956 	u8 buff[sizeof(struct iwl_wep_cmd) +
957 		sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
958 	struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
959 	size_t cmd_size  = sizeof(struct iwl_wep_cmd);
960 	struct iwl_host_cmd cmd = {
961 		.id = ctx->wep_key_cmd,
962 		.data = { wep_cmd, },
963 	};
964 
965 	might_sleep();
966 
967 	memset(wep_cmd, 0, cmd_size +
968 			(sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
969 
970 	for (i = 0; i < WEP_KEYS_MAX ; i++) {
971 		wep_cmd->key[i].key_index = i;
972 		if (ctx->wep_keys[i].key_size) {
973 			wep_cmd->key[i].key_offset = i;
974 			not_empty = 1;
975 		} else {
976 			wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
977 		}
978 
979 		wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
980 		memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
981 				ctx->wep_keys[i].key_size);
982 	}
983 
984 	wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
985 	wep_cmd->num_keys = WEP_KEYS_MAX;
986 
987 	cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
988 
989 	cmd.len[0] = cmd_size;
990 
991 	if (not_empty || send_if_empty)
992 		return iwl_dvm_send_cmd(priv, &cmd);
993 	else
994 		return 0;
995 }
996 
997 int iwl_restore_default_wep_keys(struct iwl_priv *priv,
998 				 struct iwl_rxon_context *ctx)
999 {
1000 	lockdep_assert_held(&priv->mutex);
1001 
1002 	return iwl_send_static_wepkey_cmd(priv, ctx, false);
1003 }
1004 
1005 int iwl_remove_default_wep_key(struct iwl_priv *priv,
1006 			       struct iwl_rxon_context *ctx,
1007 			       struct ieee80211_key_conf *keyconf)
1008 {
1009 	int ret;
1010 
1011 	lockdep_assert_held(&priv->mutex);
1012 
1013 	IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1014 		      keyconf->keyidx);
1015 
1016 	memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
1017 	if (iwl_is_rfkill(priv)) {
1018 		IWL_DEBUG_WEP(priv,
1019 			"Not sending REPLY_WEPKEY command due to RFKILL.\n");
1020 		/* but keys in device are clear anyway so return success */
1021 		return 0;
1022 	}
1023 	ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1024 	IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1025 		      keyconf->keyidx, ret);
1026 
1027 	return ret;
1028 }
1029 
1030 int iwl_set_default_wep_key(struct iwl_priv *priv,
1031 			    struct iwl_rxon_context *ctx,
1032 			    struct ieee80211_key_conf *keyconf)
1033 {
1034 	int ret;
1035 
1036 	lockdep_assert_held(&priv->mutex);
1037 
1038 	if (keyconf->keylen != WEP_KEY_LEN_128 &&
1039 	    keyconf->keylen != WEP_KEY_LEN_64) {
1040 		IWL_DEBUG_WEP(priv,
1041 			      "Bad WEP key length %d\n", keyconf->keylen);
1042 		return -EINVAL;
1043 	}
1044 
1045 	keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
1046 
1047 	ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1048 	memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1049 							keyconf->keylen);
1050 
1051 	ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1052 	IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1053 		keyconf->keylen, keyconf->keyidx, ret);
1054 
1055 	return ret;
1056 }
1057 
1058 /*
1059  * dynamic (per-station) keys
1060  *
1061  * The dynamic keys are a little more complicated. The device has
1062  * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1063  * These are linked to stations by a table that contains an index
1064  * into the key table for each station/key index/{mcast,unicast},
1065  * i.e. it's basically an array of pointers like this:
1066  *	key_offset_t key_mapping[NUM_STATIONS][4][2];
1067  * (it really works differently, but you can think of it as such)
1068  *
1069  * The key uploading and linking happens in the same command, the
1070  * add station command with STA_MODIFY_KEY_MASK.
1071  */
1072 
1073 static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1074 			    struct ieee80211_vif *vif,
1075 			    struct ieee80211_sta *sta)
1076 {
1077 	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1078 
1079 	if (sta)
1080 		return iwl_sta_id(sta);
1081 
1082 	/*
1083 	 * The device expects GTKs for station interfaces to be
1084 	 * installed as GTKs for the AP station. If we have no
1085 	 * station ID, then use the ap_sta_id in that case.
1086 	 */
1087 	if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1088 		return vif_priv->ctx->ap_sta_id;
1089 
1090 	return IWL_INVALID_STATION;
1091 }
1092 
1093 static int iwlagn_send_sta_key(struct iwl_priv *priv,
1094 			       struct ieee80211_key_conf *keyconf,
1095 			       u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1096 			       u32 cmd_flags)
1097 {
1098 	__le16 key_flags;
1099 	struct iwl_addsta_cmd sta_cmd;
1100 	int i;
1101 
1102 	spin_lock_bh(&priv->sta_lock);
1103 	memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1104 	spin_unlock_bh(&priv->sta_lock);
1105 
1106 	key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1107 	key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
1108 
1109 	switch (keyconf->cipher) {
1110 	case WLAN_CIPHER_SUITE_CCMP:
1111 		key_flags |= STA_KEY_FLG_CCMP;
1112 		memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1113 		break;
1114 	case WLAN_CIPHER_SUITE_TKIP:
1115 		key_flags |= STA_KEY_FLG_TKIP;
1116 		sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1117 		for (i = 0; i < 5; i++)
1118 			sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1119 		memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1120 		break;
1121 	case WLAN_CIPHER_SUITE_WEP104:
1122 		key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
1123 		/* fall through */
1124 	case WLAN_CIPHER_SUITE_WEP40:
1125 		key_flags |= STA_KEY_FLG_WEP;
1126 		memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1127 		break;
1128 	default:
1129 		WARN_ON(1);
1130 		return -EINVAL;
1131 	}
1132 
1133 	if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1134 		key_flags |= STA_KEY_MULTICAST_MSK;
1135 
1136 	/* key pointer (offset) */
1137 	sta_cmd.key.key_offset = keyconf->hw_key_idx;
1138 
1139 	sta_cmd.key.key_flags = key_flags;
1140 	sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1141 	sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1142 
1143 	return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
1144 }
1145 
1146 void iwl_update_tkip_key(struct iwl_priv *priv,
1147 			 struct ieee80211_vif *vif,
1148 			 struct ieee80211_key_conf *keyconf,
1149 			 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1150 {
1151 	u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1152 
1153 	if (sta_id == IWL_INVALID_STATION)
1154 		return;
1155 
1156 	if (iwl_scan_cancel(priv)) {
1157 		/* cancel scan failed, just live w/ bad key and rely
1158 		   briefly on SW decryption */
1159 		return;
1160 	}
1161 
1162 	iwlagn_send_sta_key(priv, keyconf, sta_id,
1163 			    iv32, phase1key, CMD_ASYNC);
1164 }
1165 
1166 int iwl_remove_dynamic_key(struct iwl_priv *priv,
1167 			   struct iwl_rxon_context *ctx,
1168 			   struct ieee80211_key_conf *keyconf,
1169 			   struct ieee80211_sta *sta)
1170 {
1171 	struct iwl_addsta_cmd sta_cmd;
1172 	u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1173 	__le16 key_flags;
1174 
1175 	/* if station isn't there, neither is the key */
1176 	if (sta_id == IWL_INVALID_STATION)
1177 		return -ENOENT;
1178 
1179 	spin_lock_bh(&priv->sta_lock);
1180 	memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1181 	if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1182 		sta_id = IWL_INVALID_STATION;
1183 	spin_unlock_bh(&priv->sta_lock);
1184 
1185 	if (sta_id == IWL_INVALID_STATION)
1186 		return 0;
1187 
1188 	lockdep_assert_held(&priv->mutex);
1189 
1190 	ctx->key_mapping_keys--;
1191 
1192 	IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1193 		      keyconf->keyidx, sta_id);
1194 
1195 	if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1196 		IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1197 			keyconf->hw_key_idx);
1198 
1199 	key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1200 	key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1201 		     STA_KEY_FLG_INVALID;
1202 
1203 	if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1204 		key_flags |= STA_KEY_MULTICAST_MSK;
1205 
1206 	sta_cmd.key.key_flags = key_flags;
1207 	sta_cmd.key.key_offset = keyconf->hw_key_idx;
1208 	sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1209 	sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1210 
1211 	return iwl_send_add_sta(priv, &sta_cmd, 0);
1212 }
1213 
1214 int iwl_set_dynamic_key(struct iwl_priv *priv,
1215 			struct iwl_rxon_context *ctx,
1216 			struct ieee80211_key_conf *keyconf,
1217 			struct ieee80211_sta *sta)
1218 {
1219 	struct ieee80211_key_seq seq;
1220 	u16 p1k[5];
1221 	int ret;
1222 	u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1223 	const u8 *addr;
1224 
1225 	if (sta_id == IWL_INVALID_STATION)
1226 		return -EINVAL;
1227 
1228 	lockdep_assert_held(&priv->mutex);
1229 
1230 	keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1231 	if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1232 		return -ENOSPC;
1233 
1234 	ctx->key_mapping_keys++;
1235 
1236 	switch (keyconf->cipher) {
1237 	case WLAN_CIPHER_SUITE_TKIP:
1238 		if (sta)
1239 			addr = sta->addr;
1240 		else /* station mode case only */
1241 			addr = ctx->active.bssid_addr;
1242 
1243 		/* pre-fill phase 1 key into device cache */
1244 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1245 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1246 		ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1247 					  seq.tkip.iv32, p1k, 0);
1248 		break;
1249 	case WLAN_CIPHER_SUITE_CCMP:
1250 	case WLAN_CIPHER_SUITE_WEP40:
1251 	case WLAN_CIPHER_SUITE_WEP104:
1252 		ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1253 					  0, NULL, 0);
1254 		break;
1255 	default:
1256 		IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
1257 		ret = -EINVAL;
1258 	}
1259 
1260 	if (ret) {
1261 		ctx->key_mapping_keys--;
1262 		clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1263 	}
1264 
1265 	IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1266 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1267 		      sta ? sta->addr : NULL, ret);
1268 
1269 	return ret;
1270 }
1271 
1272 /**
1273  * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1274  *
1275  * This adds the broadcast station into the driver's station table
1276  * and marks it driver active, so that it will be restored to the
1277  * device at the next best time.
1278  */
1279 int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1280 			       struct iwl_rxon_context *ctx)
1281 {
1282 	struct iwl_link_quality_cmd *link_cmd;
1283 	u8 sta_id;
1284 
1285 	spin_lock_bh(&priv->sta_lock);
1286 	sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1287 	if (sta_id == IWL_INVALID_STATION) {
1288 		IWL_ERR(priv, "Unable to prepare broadcast station\n");
1289 		spin_unlock_bh(&priv->sta_lock);
1290 
1291 		return -EINVAL;
1292 	}
1293 
1294 	priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1295 	priv->stations[sta_id].used |= IWL_STA_BCAST;
1296 	spin_unlock_bh(&priv->sta_lock);
1297 
1298 	link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
1299 	if (!link_cmd) {
1300 		IWL_ERR(priv,
1301 			"Unable to initialize rate scaling for bcast station.\n");
1302 		return -ENOMEM;
1303 	}
1304 
1305 	spin_lock_bh(&priv->sta_lock);
1306 	priv->stations[sta_id].lq = link_cmd;
1307 	spin_unlock_bh(&priv->sta_lock);
1308 
1309 	return 0;
1310 }
1311 
1312 /**
1313  * iwl_update_bcast_station - update broadcast station's LQ command
1314  *
1315  * Only used by iwlagn. Placed here to have all bcast station management
1316  * code together.
1317  */
1318 int iwl_update_bcast_station(struct iwl_priv *priv,
1319 			     struct iwl_rxon_context *ctx)
1320 {
1321 	struct iwl_link_quality_cmd *link_cmd;
1322 	u8 sta_id = ctx->bcast_sta_id;
1323 
1324 	link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
1325 	if (!link_cmd) {
1326 		IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1327 		return -ENOMEM;
1328 	}
1329 
1330 	spin_lock_bh(&priv->sta_lock);
1331 	if (priv->stations[sta_id].lq)
1332 		kfree(priv->stations[sta_id].lq);
1333 	else
1334 		IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1335 	priv->stations[sta_id].lq = link_cmd;
1336 	spin_unlock_bh(&priv->sta_lock);
1337 
1338 	return 0;
1339 }
1340 
1341 int iwl_update_bcast_stations(struct iwl_priv *priv)
1342 {
1343 	struct iwl_rxon_context *ctx;
1344 	int ret = 0;
1345 
1346 	for_each_context(priv, ctx) {
1347 		ret = iwl_update_bcast_station(priv, ctx);
1348 		if (ret)
1349 			break;
1350 	}
1351 
1352 	return ret;
1353 }
1354 
1355 /**
1356  * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1357  */
1358 int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1359 {
1360 	struct iwl_addsta_cmd sta_cmd;
1361 
1362 	lockdep_assert_held(&priv->mutex);
1363 
1364 	/* Remove "disable" flag, to enable Tx for this TID */
1365 	spin_lock_bh(&priv->sta_lock);
1366 	priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1367 	priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1368 	priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1369 	memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1370 	spin_unlock_bh(&priv->sta_lock);
1371 
1372 	return iwl_send_add_sta(priv, &sta_cmd, 0);
1373 }
1374 
1375 int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1376 			 int tid, u16 ssn)
1377 {
1378 	int sta_id;
1379 	struct iwl_addsta_cmd sta_cmd;
1380 
1381 	lockdep_assert_held(&priv->mutex);
1382 
1383 	sta_id = iwl_sta_id(sta);
1384 	if (sta_id == IWL_INVALID_STATION)
1385 		return -ENXIO;
1386 
1387 	spin_lock_bh(&priv->sta_lock);
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;
1393 	memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1394 	spin_unlock_bh(&priv->sta_lock);
1395 
1396 	return iwl_send_add_sta(priv, &sta_cmd, 0);
1397 }
1398 
1399 int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1400 			int tid)
1401 {
1402 	int sta_id;
1403 	struct iwl_addsta_cmd sta_cmd;
1404 
1405 	lockdep_assert_held(&priv->mutex);
1406 
1407 	sta_id = iwl_sta_id(sta);
1408 	if (sta_id == IWL_INVALID_STATION) {
1409 		IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1410 		return -ENXIO;
1411 	}
1412 
1413 	spin_lock_bh(&priv->sta_lock);
1414 	priv->stations[sta_id].sta.station_flags_msk = 0;
1415 	priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1416 	priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1417 	priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1418 	memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1419 	spin_unlock_bh(&priv->sta_lock);
1420 
1421 	return iwl_send_add_sta(priv, &sta_cmd, 0);
1422 }
1423 
1424 
1425 
1426 void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1427 {
1428 	struct iwl_addsta_cmd cmd = {
1429 		.mode = STA_CONTROL_MODIFY_MSK,
1430 		.station_flags = STA_FLG_PWR_SAVE_MSK,
1431 		.station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1432 		.sta.sta_id = sta_id,
1433 		.sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1434 		.sleep_tx_count = cpu_to_le16(cnt),
1435 	};
1436 
1437 	iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
1438 }
1439