1 /******************************************************************************
3 * Copyright(c) 2003 - 2012 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
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>
35 #include "iwl-trans.h"
37 static int iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
39 lockdep_assert_held(&priv->sta_lock);
41 if (sta_id >= IWLAGN_STATION_COUNT) {
42 IWL_ERR(priv, "invalid sta_id %u", sta_id);
45 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
46 IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u "
48 sta_id, priv->stations[sta_id].sta.sta.addr);
50 if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
52 "STA id %u addr %pM already present in uCode "
53 "(according to driver)\n",
54 sta_id, priv->stations[sta_id].sta.sta.addr);
56 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
57 IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
58 sta_id, priv->stations[sta_id].sta.sta.addr);
63 static int iwl_process_add_sta_resp(struct iwl_priv *priv,
64 struct iwl_addsta_cmd *addsta,
65 struct iwl_rx_packet *pkt)
67 u8 sta_id = addsta->sta.sta_id;
70 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
71 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
76 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
79 spin_lock(&priv->sta_lock);
81 switch (pkt->u.add_sta.status) {
82 case ADD_STA_SUCCESS_MSK:
83 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
84 ret = iwl_sta_ucode_activate(priv, sta_id);
86 case ADD_STA_NO_ROOM_IN_TABLE:
87 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
90 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
91 IWL_ERR(priv, "Adding station %d failed, no block ack "
92 "resource.\n", sta_id);
94 case ADD_STA_MODIFY_NON_EXIST_STA:
95 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
99 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
100 pkt->u.add_sta.status);
104 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
105 priv->stations[sta_id].sta.mode ==
106 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
107 sta_id, priv->stations[sta_id].sta.sta.addr);
110 * XXX: The MAC address in the command buffer is often changed from
111 * the original sent to the device. That is, the MAC address
112 * written to the command buffer often is not the same MAC address
113 * read from the command buffer when the command returns. This
114 * issue has not yet been resolved and this debugging is left to
115 * observe the problem.
117 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
118 priv->stations[sta_id].sta.mode ==
119 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
121 spin_unlock(&priv->sta_lock);
126 int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb,
127 struct iwl_device_cmd *cmd)
129 struct iwl_rx_packet *pkt = rxb_addr(rxb);
130 struct iwl_addsta_cmd *addsta =
131 (struct iwl_addsta_cmd *) cmd->payload;
133 return iwl_process_add_sta_resp(priv, addsta, pkt);
136 int iwl_send_add_sta(struct iwl_priv *priv,
137 struct iwl_addsta_cmd *sta, u8 flags)
140 struct iwl_host_cmd cmd = {
144 .len = { sizeof(*sta), },
146 u8 sta_id __maybe_unused = sta->sta.sta_id;
148 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
149 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
151 if (!(flags & CMD_ASYNC)) {
152 cmd.flags |= CMD_WANT_SKB;
156 ret = iwl_trans_send_cmd(trans(priv), &cmd);
158 if (ret || (flags & CMD_ASYNC))
160 /*else the command was successfully sent in SYNC mode, need to free
163 iwl_free_pages(priv->shrd, cmd.reply_page);
165 if (cmd.handler_status)
166 IWL_ERR(priv, "%s - error in the CMD response %d", __func__,
169 return cmd.handler_status;
172 static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
173 struct ieee80211_sta *sta,
174 struct iwl_rxon_context *ctx)
176 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
180 if (!sta || !sta_ht_inf->ht_supported)
183 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
184 IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
185 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
187 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
188 "dynamic" : "disabled");
190 sta_flags = priv->stations[index].sta.station_flags;
192 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
194 switch (mimo_ps_mode) {
195 case WLAN_HT_CAP_SM_PS_STATIC:
196 sta_flags |= STA_FLG_MIMO_DIS_MSK;
198 case WLAN_HT_CAP_SM_PS_DYNAMIC:
199 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
201 case WLAN_HT_CAP_SM_PS_DISABLED:
204 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
208 sta_flags |= cpu_to_le32(
209 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
211 sta_flags |= cpu_to_le32(
212 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
214 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
215 sta_flags |= STA_FLG_HT40_EN_MSK;
217 sta_flags &= ~STA_FLG_HT40_EN_MSK;
219 priv->stations[index].sta.station_flags = sta_flags;
225 * iwl_prep_station - Prepare station information for addition
227 * should be called with sta_lock held
229 u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
230 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
232 struct iwl_station_entry *station;
234 u8 sta_id = IWL_INVALID_STATION;
237 sta_id = ctx->ap_sta_id;
238 else if (is_broadcast_ether_addr(addr))
239 sta_id = ctx->bcast_sta_id;
241 for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
242 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
248 if (!priv->stations[i].used &&
249 sta_id == IWL_INVALID_STATION)
254 * These two conditions have the same outcome, but keep them
257 if (unlikely(sta_id == IWL_INVALID_STATION))
261 * uCode is not able to deal with multiple requests to add a
262 * station. Keep track if one is in progress so that we do not send
265 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
266 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
271 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
272 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
273 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
274 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
275 "adding again.\n", sta_id, addr);
279 station = &priv->stations[sta_id];
280 station->used = IWL_STA_DRIVER_ACTIVE;
281 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
283 priv->num_stations++;
285 /* Set up the REPLY_ADD_STA command to send to device */
286 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
287 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
288 station->sta.mode = 0;
289 station->sta.sta.sta_id = sta_id;
290 station->sta.station_flags = ctx->station_flags;
291 station->ctxid = ctx->ctxid;
294 struct iwl_station_priv *sta_priv;
296 sta_priv = (void *)sta->drv_priv;
301 * OK to call unconditionally, since local stations (IBSS BSSID
302 * STA and broadcast STA) pass in a NULL sta, and mac80211
303 * doesn't allow HT IBSS.
305 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
311 #define STA_WAIT_TIMEOUT (HZ/2)
314 * iwl_add_station_common -
316 int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
317 const u8 *addr, bool is_ap,
318 struct ieee80211_sta *sta, u8 *sta_id_r)
322 struct iwl_addsta_cmd sta_cmd;
325 spin_lock_bh(&priv->sta_lock);
326 sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
327 if (sta_id == IWL_INVALID_STATION) {
328 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
330 spin_unlock_bh(&priv->sta_lock);
335 * uCode is not able to deal with multiple requests to add a
336 * station. Keep track if one is in progress so that we do not send
339 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
340 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
342 spin_unlock_bh(&priv->sta_lock);
346 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
347 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
348 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
349 "adding again.\n", sta_id, addr);
350 spin_unlock_bh(&priv->sta_lock);
354 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
355 memcpy(&sta_cmd, &priv->stations[sta_id].sta,
356 sizeof(struct iwl_addsta_cmd));
357 spin_unlock_bh(&priv->sta_lock);
359 /* Add station to device's station table */
360 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
362 spin_lock_bh(&priv->sta_lock);
363 IWL_ERR(priv, "Adding station %pM failed.\n",
364 priv->stations[sta_id].sta.sta.addr);
365 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
366 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
367 spin_unlock_bh(&priv->sta_lock);
374 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
376 static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
378 lockdep_assert_held(&priv->sta_lock);
380 /* Ucode must be active and driver must be non active */
381 if ((priv->stations[sta_id].used &
382 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
383 IWL_STA_UCODE_ACTIVE)
384 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
386 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
388 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
389 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
392 static int iwl_send_remove_station(struct iwl_priv *priv,
393 const u8 *addr, int sta_id,
396 struct iwl_rx_packet *pkt;
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), },
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);
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",
426 switch (pkt->u.rem_sta.status) {
427 case REM_STA_SUCCESS_MSK:
429 spin_lock_bh(&priv->sta_lock);
430 iwl_sta_ucode_deactivate(priv, sta_id);
431 spin_unlock_bh(&priv->sta_lock);
433 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
437 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
441 iwl_free_pages(priv->shrd, cmd.reply_page);
447 * iwl_remove_station - Remove driver's knowledge of station.
449 int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
454 if (!iwl_is_ready(priv->shrd)) {
456 "Unable to remove station %pM, device not ready.\n",
459 * It is typical for stations to be removed when we are
460 * going down. Return success since device will be down
466 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
469 if (WARN_ON(sta_id == IWL_INVALID_STATION))
472 spin_lock_bh(&priv->sta_lock);
474 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
475 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
480 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
481 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
486 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
487 kfree(priv->stations[sta_id].lq);
488 priv->stations[sta_id].lq = NULL;
491 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
492 memset(&priv->tid_data[sta_id][tid], 0,
493 sizeof(priv->tid_data[sta_id][tid]));
495 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
497 priv->num_stations--;
499 if (WARN_ON(priv->num_stations < 0))
500 priv->num_stations = 0;
502 spin_unlock_bh(&priv->sta_lock);
504 return iwl_send_remove_station(priv, addr, sta_id, false);
506 spin_unlock_bh(&priv->sta_lock);
511 * iwl_clear_ucode_stations - clear ucode station table bits
513 * This function clears all the bits in the driver indicating
514 * which stations are active in the ucode. Call when something
515 * other than explicit station management would cause this in
516 * the ucode, e.g. unassociated RXON.
518 void iwl_clear_ucode_stations(struct iwl_priv *priv,
519 struct iwl_rxon_context *ctx)
522 bool cleared = false;
524 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
526 spin_lock_bh(&priv->sta_lock);
527 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
528 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
531 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
533 "Clearing ucode active for station %d\n", i);
534 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
538 spin_unlock_bh(&priv->sta_lock);
542 "No active stations found to be cleared\n");
546 * iwl_restore_stations() - Restore driver known stations to device
548 * All stations considered active by driver, but not present in ucode, is
553 void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
555 struct iwl_addsta_cmd sta_cmd;
556 struct iwl_link_quality_cmd lq;
562 if (!iwl_is_ready(priv->shrd)) {
564 "Not ready yet, not restoring any stations.\n");
568 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
569 spin_lock_bh(&priv->sta_lock);
570 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
571 if (ctx->ctxid != priv->stations[i].ctxid)
573 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
574 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
575 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
576 priv->stations[i].sta.sta.addr);
577 priv->stations[i].sta.mode = 0;
578 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
583 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
584 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
585 memcpy(&sta_cmd, &priv->stations[i].sta,
586 sizeof(struct iwl_addsta_cmd));
588 if (priv->stations[i].lq) {
589 if (priv->shrd->wowlan)
590 iwl_sta_fill_lq(priv, ctx, i, &lq);
592 memcpy(&lq, priv->stations[i].lq,
593 sizeof(struct iwl_link_quality_cmd));
596 spin_unlock_bh(&priv->sta_lock);
597 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
599 spin_lock_bh(&priv->sta_lock);
600 IWL_ERR(priv, "Adding station %pM failed.\n",
601 priv->stations[i].sta.sta.addr);
602 priv->stations[i].used &=
603 ~IWL_STA_DRIVER_ACTIVE;
604 priv->stations[i].used &=
605 ~IWL_STA_UCODE_INPROGRESS;
606 spin_unlock_bh(&priv->sta_lock);
609 * Rate scaling has already been initialized, send
613 iwl_send_lq_cmd(priv, ctx, &lq,
615 spin_lock_bh(&priv->sta_lock);
616 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
620 spin_unlock_bh(&priv->sta_lock);
622 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
623 "no stations to be restored.\n");
625 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
629 int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
633 for (i = 0; i < priv->sta_key_max_num; i++)
634 if (!test_and_set_bit(i, &priv->ucode_key_table))
637 return WEP_INVALID_OFFSET;
640 void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
644 spin_lock_bh(&priv->sta_lock);
645 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
646 if (!(priv->stations[i].used & IWL_STA_BCAST))
649 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
650 priv->num_stations--;
651 if (WARN_ON(priv->num_stations < 0))
652 priv->num_stations = 0;
653 kfree(priv->stations[i].lq);
654 priv->stations[i].lq = NULL;
656 spin_unlock_bh(&priv->sta_lock);
659 #ifdef CONFIG_IWLWIFI_DEBUG
660 static void iwl_dump_lq_cmd(struct iwl_priv *priv,
661 struct iwl_link_quality_cmd *lq)
664 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
665 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
666 lq->general_params.single_stream_ant_msk,
667 lq->general_params.dual_stream_ant_msk);
669 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
670 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
671 i, lq->rs_table[i].rate_n_flags);
674 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
675 struct iwl_link_quality_cmd *lq)
681 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
683 * It sometimes happens when a HT rate has been in use and we
684 * loose connectivity with AP then mac80211 will first tell us that the
685 * current channel is not HT anymore before removing the station. In such a
686 * scenario the RXON flags will be updated to indicate we are not
687 * communicating HT anymore, but the LQ command may still contain HT rates.
688 * Test for this to prevent driver from sending LQ command between the time
689 * RXON flags are updated and when LQ command is updated.
691 static bool is_lq_table_valid(struct iwl_priv *priv,
692 struct iwl_rxon_context *ctx,
693 struct iwl_link_quality_cmd *lq)
700 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
701 ctx->active.channel);
702 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
703 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
706 "index %d of LQ expects HT channel\n",
715 * iwl_send_lq_cmd() - Send link quality command
716 * @init: This command is sent as part of station initialization right
717 * after station has been added.
719 * The link quality command is sent as the last step of station creation.
720 * This is the special case in which init is set and we call a callback in
721 * this case to clear the state indicating that station creation is in
724 int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
725 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
728 struct iwl_host_cmd cmd = {
729 .id = REPLY_TX_LINK_QUALITY_CMD,
730 .len = { sizeof(struct iwl_link_quality_cmd), },
735 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
739 spin_lock_bh(&priv->sta_lock);
740 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
741 spin_unlock_bh(&priv->sta_lock);
744 spin_unlock_bh(&priv->sta_lock);
746 iwl_dump_lq_cmd(priv, lq);
747 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
750 if (is_lq_table_valid(priv, ctx, lq))
751 ret = iwl_trans_send_cmd(trans(priv), &cmd);
755 if (cmd.flags & CMD_ASYNC)
759 IWL_DEBUG_INFO(priv, "init LQ command complete, "
760 "clearing sta addition status for sta %d\n",
762 spin_lock_bh(&priv->sta_lock);
763 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
764 spin_unlock_bh(&priv->sta_lock);
770 void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
771 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
777 lockdep_assert_held(&priv->shrd->mutex);
779 memset(link_cmd, 0, sizeof(*link_cmd));
781 /* Set up the rate scaling to start at selected rate, fall back
782 * all the way down to 1M in IEEE order, and then spin on 1M */
783 if (priv->band == IEEE80211_BAND_5GHZ)
784 r = IWL_RATE_6M_INDEX;
785 else if (ctx && ctx->vif && ctx->vif->p2p)
786 r = IWL_RATE_6M_INDEX;
788 r = IWL_RATE_1M_INDEX;
790 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
791 rate_flags |= RATE_MCS_CCK_MSK;
793 rate_flags |= first_antenna(hw_params(priv).valid_tx_ant) <<
795 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
796 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
797 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
799 link_cmd->general_params.single_stream_ant_msk =
800 first_antenna(hw_params(priv).valid_tx_ant);
802 link_cmd->general_params.dual_stream_ant_msk =
803 hw_params(priv).valid_tx_ant &
804 ~first_antenna(hw_params(priv).valid_tx_ant);
805 if (!link_cmd->general_params.dual_stream_ant_msk) {
806 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
807 } else if (num_of_ant(hw_params(priv).valid_tx_ant) == 2) {
808 link_cmd->general_params.dual_stream_ant_msk =
809 hw_params(priv).valid_tx_ant;
812 link_cmd->agg_params.agg_dis_start_th =
813 LINK_QUAL_AGG_DISABLE_START_DEF;
814 link_cmd->agg_params.agg_time_limit =
815 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
817 link_cmd->sta_id = sta_id;
820 static struct iwl_link_quality_cmd *
821 iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
824 struct iwl_link_quality_cmd *link_cmd;
826 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
828 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
832 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
838 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
842 int iwlagn_add_bssid_station(struct iwl_priv *priv,
843 struct iwl_rxon_context *ctx,
844 const u8 *addr, u8 *sta_id_r)
848 struct iwl_link_quality_cmd *link_cmd;
851 *sta_id_r = IWL_INVALID_STATION;
853 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
855 IWL_ERR(priv, "Unable to add station %pM\n", addr);
862 spin_lock_bh(&priv->sta_lock);
863 priv->stations[sta_id].used |= IWL_STA_LOCAL;
864 spin_unlock_bh(&priv->sta_lock);
866 /* Set up default rate scaling table in device's station table */
867 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
870 "Unable to initialize rate scaling for station %pM.\n",
875 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
877 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
879 spin_lock_bh(&priv->sta_lock);
880 priv->stations[sta_id].lq = link_cmd;
881 spin_unlock_bh(&priv->sta_lock);
889 * For each context, the device has a table of 4 static WEP keys
890 * (one for each key index) that is updated with the following
894 static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
895 struct iwl_rxon_context *ctx,
898 int i, not_empty = 0;
899 u8 buff[sizeof(struct iwl_wep_cmd) +
900 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
901 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
902 size_t cmd_size = sizeof(struct iwl_wep_cmd);
903 struct iwl_host_cmd cmd = {
904 .id = ctx->wep_key_cmd,
905 .data = { wep_cmd, },
911 memset(wep_cmd, 0, cmd_size +
912 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
914 for (i = 0; i < WEP_KEYS_MAX ; i++) {
915 wep_cmd->key[i].key_index = i;
916 if (ctx->wep_keys[i].key_size) {
917 wep_cmd->key[i].key_offset = i;
920 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
923 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
924 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
925 ctx->wep_keys[i].key_size);
928 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
929 wep_cmd->num_keys = WEP_KEYS_MAX;
931 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
933 cmd.len[0] = cmd_size;
935 if (not_empty || send_if_empty)
936 return iwl_trans_send_cmd(trans(priv), &cmd);
941 int iwl_restore_default_wep_keys(struct iwl_priv *priv,
942 struct iwl_rxon_context *ctx)
944 lockdep_assert_held(&priv->shrd->mutex);
946 return iwl_send_static_wepkey_cmd(priv, ctx, false);
949 int iwl_remove_default_wep_key(struct iwl_priv *priv,
950 struct iwl_rxon_context *ctx,
951 struct ieee80211_key_conf *keyconf)
955 lockdep_assert_held(&priv->shrd->mutex);
957 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
960 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
961 if (iwl_is_rfkill(priv->shrd)) {
963 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
964 /* but keys in device are clear anyway so return success */
967 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
968 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
969 keyconf->keyidx, ret);
974 int iwl_set_default_wep_key(struct iwl_priv *priv,
975 struct iwl_rxon_context *ctx,
976 struct ieee80211_key_conf *keyconf)
980 lockdep_assert_held(&priv->shrd->mutex);
982 if (keyconf->keylen != WEP_KEY_LEN_128 &&
983 keyconf->keylen != WEP_KEY_LEN_64) {
985 "Bad WEP key length %d\n", keyconf->keylen);
989 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
991 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
992 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
995 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
996 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
997 keyconf->keylen, keyconf->keyidx, ret);
1003 * dynamic (per-station) keys
1005 * The dynamic keys are a little more complicated. The device has
1006 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1007 * These are linked to stations by a table that contains an index
1008 * into the key table for each station/key index/{mcast,unicast},
1009 * i.e. it's basically an array of pointers like this:
1010 * key_offset_t key_mapping[NUM_STATIONS][4][2];
1011 * (it really works differently, but you can think of it as such)
1013 * The key uploading and linking happens in the same command, the
1014 * add station command with STA_MODIFY_KEY_MASK.
1017 static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1018 struct ieee80211_vif *vif,
1019 struct ieee80211_sta *sta)
1021 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1024 return iwl_sta_id(sta);
1027 * The device expects GTKs for station interfaces to be
1028 * installed as GTKs for the AP station. If we have no
1029 * station ID, then use the ap_sta_id in that case.
1031 if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1032 return vif_priv->ctx->ap_sta_id;
1034 return IWL_INVALID_STATION;
1037 static int iwlagn_send_sta_key(struct iwl_priv *priv,
1038 struct ieee80211_key_conf *keyconf,
1039 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1043 struct iwl_addsta_cmd sta_cmd;
1046 spin_lock_bh(&priv->sta_lock);
1047 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1048 spin_unlock_bh(&priv->sta_lock);
1050 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1051 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
1053 switch (keyconf->cipher) {
1054 case WLAN_CIPHER_SUITE_CCMP:
1055 key_flags |= STA_KEY_FLG_CCMP;
1056 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1058 case WLAN_CIPHER_SUITE_TKIP:
1059 key_flags |= STA_KEY_FLG_TKIP;
1060 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1061 for (i = 0; i < 5; i++)
1062 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1063 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1065 case WLAN_CIPHER_SUITE_WEP104:
1066 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
1068 case WLAN_CIPHER_SUITE_WEP40:
1069 key_flags |= STA_KEY_FLG_WEP;
1070 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1077 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1078 key_flags |= STA_KEY_MULTICAST_MSK;
1080 /* key pointer (offset) */
1081 sta_cmd.key.key_offset = keyconf->hw_key_idx;
1083 sta_cmd.key.key_flags = key_flags;
1084 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1085 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1087 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
1090 void iwl_update_tkip_key(struct iwl_priv *priv,
1091 struct ieee80211_vif *vif,
1092 struct ieee80211_key_conf *keyconf,
1093 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1095 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1097 if (sta_id == IWL_INVALID_STATION)
1100 if (iwl_scan_cancel(priv)) {
1101 /* cancel scan failed, just live w/ bad key and rely
1102 briefly on SW decryption */
1106 iwlagn_send_sta_key(priv, keyconf, sta_id,
1107 iv32, phase1key, CMD_ASYNC);
1110 int iwl_remove_dynamic_key(struct iwl_priv *priv,
1111 struct iwl_rxon_context *ctx,
1112 struct ieee80211_key_conf *keyconf,
1113 struct ieee80211_sta *sta)
1115 struct iwl_addsta_cmd sta_cmd;
1116 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1119 /* if station isn't there, neither is the key */
1120 if (sta_id == IWL_INVALID_STATION)
1123 spin_lock_bh(&priv->sta_lock);
1124 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1125 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1126 sta_id = IWL_INVALID_STATION;
1127 spin_unlock_bh(&priv->sta_lock);
1129 if (sta_id == IWL_INVALID_STATION)
1132 lockdep_assert_held(&priv->shrd->mutex);
1134 ctx->key_mapping_keys--;
1136 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1137 keyconf->keyidx, sta_id);
1139 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1140 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1141 keyconf->hw_key_idx);
1143 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1144 key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1145 STA_KEY_FLG_INVALID;
1147 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1148 key_flags |= STA_KEY_MULTICAST_MSK;
1150 sta_cmd.key.key_flags = key_flags;
1151 sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
1152 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1153 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1155 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1158 int iwl_set_dynamic_key(struct iwl_priv *priv,
1159 struct iwl_rxon_context *ctx,
1160 struct ieee80211_key_conf *keyconf,
1161 struct ieee80211_sta *sta)
1163 struct ieee80211_key_seq seq;
1166 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1169 if (sta_id == IWL_INVALID_STATION)
1172 lockdep_assert_held(&priv->shrd->mutex);
1174 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1175 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1178 ctx->key_mapping_keys++;
1180 switch (keyconf->cipher) {
1181 case WLAN_CIPHER_SUITE_TKIP:
1184 else /* station mode case only */
1185 addr = ctx->active.bssid_addr;
1187 /* pre-fill phase 1 key into device cache */
1188 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1189 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1190 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1191 seq.tkip.iv32, p1k, CMD_SYNC);
1193 case WLAN_CIPHER_SUITE_CCMP:
1194 case WLAN_CIPHER_SUITE_WEP40:
1195 case WLAN_CIPHER_SUITE_WEP104:
1196 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1200 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
1205 ctx->key_mapping_keys--;
1206 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1209 IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1210 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1211 sta ? sta->addr : NULL, ret);
1217 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1219 * This adds the broadcast station into the driver's station table
1220 * and marks it driver active, so that it will be restored to the
1221 * device at the next best time.
1223 int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1224 struct iwl_rxon_context *ctx)
1226 struct iwl_link_quality_cmd *link_cmd;
1229 spin_lock_bh(&priv->sta_lock);
1230 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1231 if (sta_id == IWL_INVALID_STATION) {
1232 IWL_ERR(priv, "Unable to prepare broadcast station\n");
1233 spin_unlock_bh(&priv->sta_lock);
1238 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1239 priv->stations[sta_id].used |= IWL_STA_BCAST;
1240 spin_unlock_bh(&priv->sta_lock);
1242 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
1245 "Unable to initialize rate scaling for bcast station.\n");
1249 spin_lock_bh(&priv->sta_lock);
1250 priv->stations[sta_id].lq = link_cmd;
1251 spin_unlock_bh(&priv->sta_lock);
1257 * iwl_update_bcast_station - update broadcast station's LQ command
1259 * Only used by iwlagn. Placed here to have all bcast station management
1262 int iwl_update_bcast_station(struct iwl_priv *priv,
1263 struct iwl_rxon_context *ctx)
1265 struct iwl_link_quality_cmd *link_cmd;
1266 u8 sta_id = ctx->bcast_sta_id;
1268 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
1270 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1274 spin_lock_bh(&priv->sta_lock);
1275 if (priv->stations[sta_id].lq)
1276 kfree(priv->stations[sta_id].lq);
1278 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1279 priv->stations[sta_id].lq = link_cmd;
1280 spin_unlock_bh(&priv->sta_lock);
1285 int iwl_update_bcast_stations(struct iwl_priv *priv)
1287 struct iwl_rxon_context *ctx;
1290 for_each_context(priv, ctx) {
1291 ret = iwl_update_bcast_station(priv, ctx);
1300 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1302 int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1304 struct iwl_addsta_cmd sta_cmd;
1306 lockdep_assert_held(&priv->shrd->mutex);
1308 /* Remove "disable" flag, to enable Tx for this TID */
1309 spin_lock_bh(&priv->sta_lock);
1310 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1311 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1312 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1313 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1314 spin_unlock_bh(&priv->sta_lock);
1316 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1319 int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1323 struct iwl_addsta_cmd sta_cmd;
1325 lockdep_assert_held(&priv->shrd->mutex);
1327 sta_id = iwl_sta_id(sta);
1328 if (sta_id == IWL_INVALID_STATION)
1331 spin_lock_bh(&priv->sta_lock);
1332 priv->stations[sta_id].sta.station_flags_msk = 0;
1333 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1334 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1335 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1336 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1337 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1338 spin_unlock_bh(&priv->sta_lock);
1340 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1343 int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1347 struct iwl_addsta_cmd sta_cmd;
1349 lockdep_assert_held(&priv->shrd->mutex);
1351 sta_id = iwl_sta_id(sta);
1352 if (sta_id == IWL_INVALID_STATION) {
1353 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1357 spin_lock_bh(&priv->sta_lock);
1358 priv->stations[sta_id].sta.station_flags_msk = 0;
1359 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1360 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1361 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1362 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1363 spin_unlock_bh(&priv->sta_lock);
1365 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1370 void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1372 struct iwl_addsta_cmd cmd = {
1373 .mode = STA_CONTROL_MODIFY_MSK,
1374 .station_flags = STA_FLG_PWR_SAVE_MSK,
1375 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1376 .sta.sta_id = sta_id,
1377 .sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1378 .sleep_tx_count = cpu_to_le16(cnt),
1381 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);