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