[go: nahoru, domu]

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