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