OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / drivers / net / wireless / iwlwifi / iwl-agn-sta.c
blobe483cfa8d14e175aac3432f77951d3641989f116
1 /******************************************************************************
3 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
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.
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.
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
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *****************************************************************************/
30 #include <net/mac80211.h>
32 #include "iwl-dev.h"
33 #include "iwl-core.h"
34 #include "iwl-agn.h"
35 #include "iwl-trans.h"
37 /* priv->shrd->sta_lock must be held */
38 static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
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);
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);
58 static int iwl_process_add_sta_resp(struct iwl_priv *priv,
59 struct iwl_addsta_cmd *addsta,
60 struct iwl_rx_packet *pkt)
62 u8 sta_id = addsta->sta.sta_id;
63 unsigned long flags;
64 int ret = -EIO;
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;
72 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
73 sta_id);
75 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
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;
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);
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.
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);
120 return ret;
123 int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb,
124 struct iwl_device_cmd *cmd)
126 struct iwl_rx_packet *pkt = rxb_addr(rxb);
127 struct iwl_addsta_cmd *addsta =
128 (struct iwl_addsta_cmd *) cmd->payload;
130 return iwl_process_add_sta_resp(priv, addsta, pkt);
133 int iwl_send_add_sta(struct iwl_priv *priv,
134 struct iwl_addsta_cmd *sta, u8 flags)
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), },
143 u8 sta_id __maybe_unused = sta->sta.sta_id;
145 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
146 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
148 if (!(flags & CMD_ASYNC)) {
149 cmd.flags |= CMD_WANT_SKB;
150 might_sleep();
153 ret = iwl_trans_send_cmd(trans(priv), &cmd);
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 */
160 iwl_free_pages(priv->shrd, cmd.reply_page);
162 if (cmd.handler_status)
163 IWL_ERR(priv, "%s - error in the CMD response %d", __func__,
164 cmd.handler_status);
166 return cmd.handler_status;
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)
173 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
174 __le32 sta_flags;
175 u8 mimo_ps_mode;
177 if (!sta || !sta_ht_inf->ht_supported)
178 goto done;
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");
187 sta_flags = priv->stations[index].sta.station_flags;
189 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
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;
205 sta_flags |= cpu_to_le32(
206 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
208 sta_flags |= cpu_to_le32(
209 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
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;
216 priv->stations[index].sta.station_flags = sta_flags;
217 done:
218 return;
222 * iwl_prep_station - Prepare station information for addition
224 * should be called with sta_lock held
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)
229 struct iwl_station_entry *station;
230 int i;
231 u8 sta_id = IWL_INVALID_STATION;
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;
245 if (!priv->stations[i].used &&
246 sta_id == IWL_INVALID_STATION)
247 sta_id = i;
251 * These two conditions have the same outcome, but keep them
252 * separate
254 if (unlikely(sta_id == IWL_INVALID_STATION))
255 return sta_id;
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.
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;
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;
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++;
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;
290 if (sta) {
291 struct iwl_station_priv *sta_priv;
293 sta_priv = (void *)sta->drv_priv;
294 sta_priv->ctx = ctx;
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.
302 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
304 return sta_id;
308 #define STA_WAIT_TIMEOUT (HZ/2)
311 * iwl_add_station_common -
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)
317 unsigned long flags_spin;
318 int ret = 0;
319 u8 sta_id;
320 struct iwl_addsta_cmd sta_cmd;
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;
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.
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;
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;
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);
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);
367 *sta_id_r = sta_id;
368 return ret;
372 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
374 * priv->shrd->sta_lock must be held
376 static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
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);
384 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
386 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
387 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
390 static int iwl_send_remove_station(struct iwl_priv *priv,
391 const u8 *addr, int sta_id,
392 bool temporary)
394 struct iwl_rx_packet *pkt;
395 int ret;
397 unsigned long flags_spin;
398 struct iwl_rem_sta_cmd rm_sta_cmd;
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, },
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);
411 cmd.flags |= CMD_WANT_SKB;
413 ret = iwl_trans_send_cmd(trans(priv), &cmd);
415 if (ret)
416 return ret;
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;
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);
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;
443 iwl_free_pages(priv->shrd, cmd.reply_page);
445 return ret;
449 * iwl_remove_station - Remove driver's knowledge of station.
451 int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
452 const u8 *addr)
454 unsigned long flags;
455 u8 tid;
457 if (!iwl_is_ready(priv->shrd)) {
458 IWL_DEBUG_INFO(priv,
459 "Unable to remove station %pM, device not ready.\n",
460 addr);
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
466 return 0;
469 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
470 sta_id, addr);
472 if (WARN_ON(sta_id == IWL_INVALID_STATION))
473 return -EINVAL;
475 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
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;
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;
489 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
490 kfree(priv->stations[sta_id].lq);
491 priv->stations[sta_id].lq = NULL;
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]));
498 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
500 priv->num_stations--;
502 if (WARN_ON(priv->num_stations < 0))
503 priv->num_stations = 0;
505 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
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;
514 * iwl_clear_ucode_stations - clear ucode station table bits
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.
521 void iwl_clear_ucode_stations(struct iwl_priv *priv,
522 struct iwl_rxon_context *ctx)
524 int i;
525 unsigned long flags_spin;
526 bool cleared = false;
528 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
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;
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;
542 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
544 if (!cleared)
545 IWL_DEBUG_INFO(priv,
546 "No active stations found to be cleared\n");
550 * iwl_restore_stations() - Restore driver known stations to device
552 * All stations considered active by driver, but not present in ucode, is
553 * restored.
555 * Function sleeps.
557 void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
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;
567 if (!iwl_is_ready(priv->shrd)) {
568 IWL_DEBUG_INFO(priv,
569 "Not ready yet, not restoring any stations.\n");
570 return;
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;
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;
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);
617 * Rate scaling has already been initialized, send
618 * current LQ command
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;
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");
637 void iwl_reprogram_ap_sta(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
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;
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;
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;
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);
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);
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);
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);
683 int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
685 int i;
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;
691 return WEP_INVALID_OFFSET;
694 void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
696 unsigned long flags;
697 int i;
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;
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;
711 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
714 #ifdef CONFIG_IWLWIFI_DEBUG
715 static void iwl_dump_lq_cmd(struct iwl_priv *priv,
716 struct iwl_link_quality_cmd *lq)
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);
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);
728 #else
729 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
730 struct iwl_link_quality_cmd *lq)
733 #endif
736 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
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.
746 static bool is_lq_table_valid(struct iwl_priv *priv,
747 struct iwl_rxon_context *ctx,
748 struct iwl_link_quality_cmd *lq)
750 int i;
752 if (ctx->ht.enabled)
753 return true;
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",
763 return false;
766 return true;
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.
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.
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)
782 int ret = 0;
783 unsigned long flags_spin;
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, },
792 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
793 return -EINVAL;
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;
801 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
803 iwl_dump_lq_cmd(priv, lq);
804 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
805 return -EINVAL;
807 if (is_lq_table_valid(priv, ctx, lq))
808 ret = iwl_trans_send_cmd(trans(priv), &cmd);
809 else
810 ret = -EINVAL;
812 if (cmd.flags & CMD_ASYNC)
813 return ret;
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);
823 return ret;
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)
830 int i, r;
831 u32 rate_flags = 0;
832 __le32 rate_n_flags;
834 lockdep_assert_held(&priv->shrd->mutex);
836 memset(link_cmd, 0, sizeof(*link_cmd));
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;
847 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
848 rate_flags |= RATE_MCS_CCK_MSK;
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;
856 link_cmd->general_params.single_stream_ant_msk =
857 first_antenna(hw_params(priv).valid_tx_ant);
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;
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);
874 link_cmd->sta_id = sta_id;
877 static struct iwl_link_quality_cmd *
878 iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
879 u8 sta_id)
881 struct iwl_link_quality_cmd *link_cmd;
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;
889 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
891 return link_cmd;
895 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
897 * Function sleeps.
899 int iwlagn_add_bssid_station(struct iwl_priv *priv,
900 struct iwl_rxon_context *ctx,
901 const u8 *addr, u8 *sta_id_r)
903 int ret;
904 u8 sta_id;
905 struct iwl_link_quality_cmd *link_cmd;
906 unsigned long flags;
908 if (sta_id_r)
909 *sta_id_r = IWL_INVALID_STATION;
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;
917 if (sta_id_r)
918 *sta_id_r = sta_id;
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);
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;
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);
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);
941 return 0;
945 * static WEP keys
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.
952 static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
953 struct iwl_rxon_context *ctx,
954 bool send_if_empty)
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,
967 might_sleep();
969 memset(wep_cmd, 0, cmd_size +
970 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
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;
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);
986 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
987 wep_cmd->num_keys = WEP_KEYS_MAX;
989 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
991 cmd.len[0] = cmd_size;
993 if (not_empty || send_if_empty)
994 return iwl_trans_send_cmd(trans(priv), &cmd);
995 else
996 return 0;
999 int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1000 struct iwl_rxon_context *ctx)
1002 lockdep_assert_held(&priv->shrd->mutex);
1004 return iwl_send_static_wepkey_cmd(priv, ctx, false);
1007 int iwl_remove_default_wep_key(struct iwl_priv *priv,
1008 struct iwl_rxon_context *ctx,
1009 struct ieee80211_key_conf *keyconf)
1011 int ret;
1013 lockdep_assert_held(&priv->shrd->mutex);
1015 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1016 keyconf->keyidx);
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;
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);
1029 return ret;
1032 int iwl_set_default_wep_key(struct iwl_priv *priv,
1033 struct iwl_rxon_context *ctx,
1034 struct ieee80211_key_conf *keyconf)
1036 int ret;
1038 lockdep_assert_held(&priv->shrd->mutex);
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;
1047 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
1049 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1050 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1051 keyconf->keylen);
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);
1057 return ret;
1061 * dynamic (per-station) keys
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)
1071 * The key uploading and linking happens in the same command, the
1072 * add station command with STA_MODIFY_KEY_MASK.
1075 static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1076 struct ieee80211_vif *vif,
1077 struct ieee80211_sta *sta)
1079 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1080 u8 sta_id = IWL_INVALID_STATION;
1082 if (sta)
1083 sta_id = iwl_sta_id(sta);
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.
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:
1097 * In all other cases, the key will be
1098 * used either for TX only or is bound
1099 * to a station already.
1101 break;
1105 return sta_id;
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)
1113 unsigned long flags;
1114 __le16 key_flags;
1115 struct iwl_addsta_cmd sta_cmd;
1116 int i;
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);
1122 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1123 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
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;
1149 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1150 key_flags |= STA_KEY_MULTICAST_MSK;
1152 /* key pointer (offset) */
1153 sta_cmd.key.key_offset = keyconf->hw_key_idx;
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;
1159 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
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)
1167 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1169 if (sta_id == IWL_INVALID_STATION)
1170 return;
1172 if (iwl_scan_cancel(priv)) {
1173 /* cancel scan failed, just live w/ bad key and rely
1174 briefly on SW decryption */
1175 return;
1178 iwlagn_send_sta_key(priv, keyconf, sta_id,
1179 iv32, phase1key, CMD_ASYNC);
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)
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;
1192 /* if station isn't there, neither is the key */
1193 if (sta_id == IWL_INVALID_STATION)
1194 return -ENOENT;
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);
1202 if (sta_id == IWL_INVALID_STATION)
1203 return 0;
1205 lockdep_assert_held(&priv->shrd->mutex);
1207 ctx->key_mapping_keys--;
1209 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1210 keyconf->keyidx, sta_id);
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);
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;
1220 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1221 key_flags |= STA_KEY_MULTICAST_MSK;
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;
1228 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
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)
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;
1242 if (sta_id == IWL_INVALID_STATION)
1243 return -EINVAL;
1245 lockdep_assert_held(&priv->shrd->mutex);
1247 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1248 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1249 return -ENOSPC;
1251 ctx->key_mapping_keys++;
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;
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;
1277 if (ret) {
1278 ctx->key_mapping_keys--;
1279 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
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);
1286 return ret;
1290 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
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.
1296 int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1297 struct iwl_rxon_context *ctx)
1299 struct iwl_link_quality_cmd *link_cmd;
1300 unsigned long flags;
1301 u8 sta_id;
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);
1309 return -EINVAL;
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);
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;
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);
1327 return 0;
1331 * iwl_update_bcast_station - update broadcast station's LQ command
1333 * Only used by iwlagn. Placed here to have all bcast station management
1334 * code together.
1336 int iwl_update_bcast_station(struct iwl_priv *priv,
1337 struct iwl_rxon_context *ctx)
1339 unsigned long flags;
1340 struct iwl_link_quality_cmd *link_cmd;
1341 u8 sta_id = ctx->bcast_sta_id;
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;
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);
1357 return 0;
1360 int iwl_update_bcast_stations(struct iwl_priv *priv)
1362 struct iwl_rxon_context *ctx;
1363 int ret = 0;
1365 for_each_context(priv, ctx) {
1366 ret = iwl_update_bcast_station(priv, ctx);
1367 if (ret)
1368 break;
1371 return ret;
1375 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1377 int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1379 unsigned long flags;
1380 struct iwl_addsta_cmd sta_cmd;
1382 lockdep_assert_held(&priv->shrd->mutex);
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);
1392 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1395 int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1396 int tid, u16 ssn)
1398 unsigned long flags;
1399 int sta_id;
1400 struct iwl_addsta_cmd sta_cmd;
1402 lockdep_assert_held(&priv->shrd->mutex);
1404 sta_id = iwl_sta_id(sta);
1405 if (sta_id == IWL_INVALID_STATION)
1406 return -ENXIO;
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);
1417 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1420 int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1421 int tid)
1423 unsigned long flags;
1424 int sta_id;
1425 struct iwl_addsta_cmd sta_cmd;
1427 lockdep_assert_held(&priv->shrd->mutex);
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;
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);
1443 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1448 void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1450 unsigned long flags;
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);