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
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>
31 #include <linux/etherdevice.h>
32 #include <linux/sched.h>
33 #include <linux/lockdep.h>
34 #include <linux/export.h>
40 /* il->sta_lock must be held */
41 static void il_sta_ucode_activate(struct il_priv *il, u8 sta_id)
44 if (!(il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE))
46 "ACTIVATE a non DRIVER active station id %u addr %pM\n",
47 sta_id, il->stations[sta_id].sta.sta.addr);
49 if (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE) {
51 "STA id %u addr %pM already present"
52 " in uCode (according to driver)\n",
53 sta_id, il->stations[sta_id].sta.sta.addr);
55 il->stations[sta_id].used |= IL_STA_UCODE_ACTIVE;
56 D_ASSOC("Added STA id %u addr %pM to uCode\n",
57 sta_id, il->stations[sta_id].sta.sta.addr);
61 static int il_process_add_sta_resp(struct il_priv *il,
62 struct il_addsta_cmd *addsta,
63 struct il_rx_pkt *pkt,
66 u8 sta_id = addsta->sta.sta_id;
70 if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
71 IL_ERR("Bad return from C_ADD_STA (0x%08X)\n",
76 D_INFO("Processing response for adding station %u\n",
79 spin_lock_irqsave(&il->sta_lock, flags);
81 switch (pkt->u.add_sta.status) {
82 case ADD_STA_SUCCESS_MSK:
83 D_INFO("C_ADD_STA PASSED\n");
84 il_sta_ucode_activate(il, sta_id);
87 case ADD_STA_NO_ROOM_IN_TBL:
88 IL_ERR("Adding station %d failed, no room in table.\n",
91 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
93 "Adding station %d failed, no block ack resource.\n",
96 case ADD_STA_MODIFY_NON_EXIST_STA:
97 IL_ERR("Attempting to modify non-existing station %d\n",
101 D_ASSOC("Received C_ADD_STA:(0x%08X)\n",
102 pkt->u.add_sta.status);
106 D_INFO("%s station id %u addr %pM\n",
107 il->stations[sta_id].sta.mode ==
108 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
109 sta_id, il->stations[sta_id].sta.sta.addr);
112 * XXX: The MAC address in the command buffer is often changed from
113 * the original sent to the device. That is, the MAC address
114 * written to the command buffer often is not the same MAC address
115 * read from the command buffer when the command returns. This
116 * issue has not yet been resolved and this debugging is left to
117 * observe the problem.
119 D_INFO("%s station according to cmd buffer %pM\n",
120 il->stations[sta_id].sta.mode ==
121 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
123 spin_unlock_irqrestore(&il->sta_lock, flags);
128 static void il_add_sta_callback(struct il_priv *il,
129 struct il_device_cmd *cmd,
130 struct il_rx_pkt *pkt)
132 struct il_addsta_cmd *addsta =
133 (struct il_addsta_cmd *)cmd->cmd.payload;
135 il_process_add_sta_resp(il, addsta, pkt, false);
139 int il_send_add_sta(struct il_priv *il,
140 struct il_addsta_cmd *sta, u8 flags)
142 struct il_rx_pkt *pkt = NULL;
144 u8 data[sizeof(*sta)];
145 struct il_host_cmd cmd = {
150 u8 sta_id __maybe_unused = sta->sta.sta_id;
152 D_INFO("Adding sta %u (%pM) %ssynchronously\n",
153 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
155 if (flags & CMD_ASYNC)
156 cmd.callback = il_add_sta_callback;
158 cmd.flags |= CMD_WANT_SKB;
162 cmd.len = il->cfg->ops->utils->build_addsta_hcmd(sta, data);
163 ret = il_send_cmd(il, &cmd);
165 if (ret || (flags & CMD_ASYNC))
169 pkt = (struct il_rx_pkt *)cmd.reply_page;
170 ret = il_process_add_sta_resp(il, sta, pkt, true);
172 il_free_pages(il, cmd.reply_page);
176 EXPORT_SYMBOL(il_send_add_sta);
178 static void il_set_ht_add_station(struct il_priv *il, u8 idx,
179 struct ieee80211_sta *sta,
180 struct il_rxon_context *ctx)
182 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
186 if (!sta || !sta_ht_inf->ht_supported)
189 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
190 D_ASSOC("spatial multiplexing power save mode: %s\n",
191 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
193 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
194 "dynamic" : "disabled");
196 sta_flags = il->stations[idx].sta.station_flags;
198 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
200 switch (mimo_ps_mode) {
201 case WLAN_HT_CAP_SM_PS_STATIC:
202 sta_flags |= STA_FLG_MIMO_DIS_MSK;
204 case WLAN_HT_CAP_SM_PS_DYNAMIC:
205 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
207 case WLAN_HT_CAP_SM_PS_DISABLED:
210 IL_WARN("Invalid MIMO PS mode %d\n", mimo_ps_mode);
214 sta_flags |= cpu_to_le32(
215 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
217 sta_flags |= cpu_to_le32(
218 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
220 if (il_is_ht40_tx_allowed(il, ctx, &sta->ht_cap))
221 sta_flags |= STA_FLG_HT40_EN_MSK;
223 sta_flags &= ~STA_FLG_HT40_EN_MSK;
225 il->stations[idx].sta.station_flags = sta_flags;
231 * il_prep_station - Prepare station information for addition
233 * should be called with sta_lock held
235 u8 il_prep_station(struct il_priv *il, struct il_rxon_context *ctx,
236 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
238 struct il_station_entry *station;
240 u8 sta_id = IL_INVALID_STATION;
244 sta_id = ctx->ap_sta_id;
245 else if (is_broadcast_ether_addr(addr))
246 sta_id = ctx->bcast_sta_id;
248 for (i = IL_STA_ID; i < il->hw_params.max_stations; i++) {
249 if (!compare_ether_addr(il->stations[i].sta.sta.addr,
255 if (!il->stations[i].used &&
256 sta_id == IL_INVALID_STATION)
261 * These two conditions have the same outcome, but keep them
264 if (unlikely(sta_id == IL_INVALID_STATION))
268 * uCode is not able to deal with multiple requests to add a
269 * station. Keep track if one is in progress so that we do not send
272 if (il->stations[sta_id].used & IL_STA_UCODE_INPROGRESS) {
274 "STA %d already in process of being added.\n",
279 if ((il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE) &&
280 (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE) &&
281 !compare_ether_addr(il->stations[sta_id].sta.sta.addr, addr)) {
283 "STA %d (%pM) already added, not adding again.\n",
288 station = &il->stations[sta_id];
289 station->used = IL_STA_DRIVER_ACTIVE;
290 D_ASSOC("Add STA to driver ID %d: %pM\n",
294 /* Set up the C_ADD_STA command to send to device */
295 memset(&station->sta, 0, sizeof(struct il_addsta_cmd));
296 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
297 station->sta.mode = 0;
298 station->sta.sta.sta_id = sta_id;
299 station->sta.station_flags = ctx->station_flags;
300 station->ctxid = ctx->ctxid;
303 struct il_station_priv_common *sta_priv;
305 sta_priv = (void *)sta->drv_priv;
310 * OK to call unconditionally, since local stations (IBSS BSSID
311 * STA and broadcast STA) pass in a NULL sta, and mac80211
312 * doesn't allow HT IBSS.
314 il_set_ht_add_station(il, sta_id, sta, ctx);
317 rate = (il->band == IEEE80211_BAND_5GHZ) ?
318 RATE_6M_PLCP : RATE_1M_PLCP;
319 /* Turn on both antennas for the station... */
320 station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
325 EXPORT_SYMBOL_GPL(il_prep_station);
327 #define STA_WAIT_TIMEOUT (HZ/2)
330 * il_add_station_common -
333 il_add_station_common(struct il_priv *il,
334 struct il_rxon_context *ctx,
335 const u8 *addr, bool is_ap,
336 struct ieee80211_sta *sta, u8 *sta_id_r)
338 unsigned long flags_spin;
341 struct il_addsta_cmd sta_cmd;
344 spin_lock_irqsave(&il->sta_lock, flags_spin);
345 sta_id = il_prep_station(il, ctx, addr, is_ap, sta);
346 if (sta_id == IL_INVALID_STATION) {
347 IL_ERR("Unable to prepare station %pM for addition\n",
349 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
354 * uCode is not able to deal with multiple requests to add a
355 * station. Keep track if one is in progress so that we do not send
358 if (il->stations[sta_id].used & IL_STA_UCODE_INPROGRESS) {
360 "STA %d already in process of being added.\n",
362 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
366 if ((il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE) &&
367 (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE)) {
369 "STA %d (%pM) already added, not adding again.\n",
371 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
375 il->stations[sta_id].used |= IL_STA_UCODE_INPROGRESS;
376 memcpy(&sta_cmd, &il->stations[sta_id].sta,
377 sizeof(struct il_addsta_cmd));
378 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
380 /* Add station to device's station table */
381 ret = il_send_add_sta(il, &sta_cmd, CMD_SYNC);
383 spin_lock_irqsave(&il->sta_lock, flags_spin);
384 IL_ERR("Adding station %pM failed.\n",
385 il->stations[sta_id].sta.sta.addr);
386 il->stations[sta_id].used &= ~IL_STA_DRIVER_ACTIVE;
387 il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
388 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
393 EXPORT_SYMBOL(il_add_station_common);
396 * il_sta_ucode_deactivate - deactivate ucode status for a station
398 * il->sta_lock must be held
400 static void il_sta_ucode_deactivate(struct il_priv *il, u8 sta_id)
402 /* Ucode must be active and driver must be non active */
403 if ((il->stations[sta_id].used &
404 (IL_STA_UCODE_ACTIVE | IL_STA_DRIVER_ACTIVE)) !=
406 IL_ERR("removed non active STA %u\n", sta_id);
408 il->stations[sta_id].used &= ~IL_STA_UCODE_ACTIVE;
410 memset(&il->stations[sta_id], 0, sizeof(struct il_station_entry));
411 D_ASSOC("Removed STA %u\n", sta_id);
414 static int il_send_remove_station(struct il_priv *il,
415 const u8 *addr, int sta_id,
418 struct il_rx_pkt *pkt;
421 unsigned long flags_spin;
422 struct il_rem_sta_cmd rm_sta_cmd;
424 struct il_host_cmd cmd = {
426 .len = sizeof(struct il_rem_sta_cmd),
431 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
432 rm_sta_cmd.num_sta = 1;
433 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
435 cmd.flags |= CMD_WANT_SKB;
437 ret = il_send_cmd(il, &cmd);
442 pkt = (struct il_rx_pkt *)cmd.reply_page;
443 if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
444 IL_ERR("Bad return from C_REM_STA (0x%08X)\n",
450 switch (pkt->u.rem_sta.status) {
451 case REM_STA_SUCCESS_MSK:
453 spin_lock_irqsave(&il->sta_lock, flags_spin);
454 il_sta_ucode_deactivate(il, sta_id);
455 spin_unlock_irqrestore(&il->sta_lock,
458 D_ASSOC("C_REM_STA PASSED\n");
462 IL_ERR("C_REM_STA failed\n");
466 il_free_pages(il, cmd.reply_page);
472 * il_remove_station - Remove driver's knowledge of station.
474 int il_remove_station(struct il_priv *il, const u8 sta_id,
479 if (!il_is_ready(il)) {
481 "Unable to remove station %pM, device not ready.\n",
484 * It is typical for stations to be removed when we are
485 * going down. Return success since device will be down
491 D_ASSOC("Removing STA from driver:%d %pM\n",
494 if (WARN_ON(sta_id == IL_INVALID_STATION))
497 spin_lock_irqsave(&il->sta_lock, flags);
499 if (!(il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE)) {
500 D_INFO("Removing %pM but non DRIVER active\n",
505 if (!(il->stations[sta_id].used & IL_STA_UCODE_ACTIVE)) {
506 D_INFO("Removing %pM but non UCODE active\n",
511 if (il->stations[sta_id].used & IL_STA_LOCAL) {
512 kfree(il->stations[sta_id].lq);
513 il->stations[sta_id].lq = NULL;
516 il->stations[sta_id].used &= ~IL_STA_DRIVER_ACTIVE;
520 BUG_ON(il->num_stations < 0);
522 spin_unlock_irqrestore(&il->sta_lock, flags);
524 return il_send_remove_station(il, addr, sta_id, false);
526 spin_unlock_irqrestore(&il->sta_lock, flags);
529 EXPORT_SYMBOL_GPL(il_remove_station);
532 * il_clear_ucode_stations - clear ucode station table bits
534 * This function clears all the bits in the driver indicating
535 * which stations are active in the ucode. Call when something
536 * other than explicit station management would cause this in
537 * the ucode, e.g. unassociated RXON.
539 void il_clear_ucode_stations(struct il_priv *il,
540 struct il_rxon_context *ctx)
543 unsigned long flags_spin;
544 bool cleared = false;
546 D_INFO("Clearing ucode stations in driver\n");
548 spin_lock_irqsave(&il->sta_lock, flags_spin);
549 for (i = 0; i < il->hw_params.max_stations; i++) {
550 if (ctx && ctx->ctxid != il->stations[i].ctxid)
553 if (il->stations[i].used & IL_STA_UCODE_ACTIVE) {
555 "Clearing ucode active for station %d\n", i);
556 il->stations[i].used &= ~IL_STA_UCODE_ACTIVE;
560 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
564 "No active stations found to be cleared\n");
566 EXPORT_SYMBOL(il_clear_ucode_stations);
569 * il_restore_stations() - Restore driver known stations to device
571 * All stations considered active by driver, but not present in ucode, is
577 il_restore_stations(struct il_priv *il, struct il_rxon_context *ctx)
579 struct il_addsta_cmd sta_cmd;
580 struct il_link_quality_cmd lq;
581 unsigned long flags_spin;
587 if (!il_is_ready(il)) {
589 "Not ready yet, not restoring any stations.\n");
593 D_ASSOC("Restoring all known stations ... start.\n");
594 spin_lock_irqsave(&il->sta_lock, flags_spin);
595 for (i = 0; i < il->hw_params.max_stations; i++) {
596 if (ctx->ctxid != il->stations[i].ctxid)
598 if ((il->stations[i].used & IL_STA_DRIVER_ACTIVE) &&
599 !(il->stations[i].used & IL_STA_UCODE_ACTIVE)) {
600 D_ASSOC("Restoring sta %pM\n",
601 il->stations[i].sta.sta.addr);
602 il->stations[i].sta.mode = 0;
603 il->stations[i].used |= IL_STA_UCODE_INPROGRESS;
608 for (i = 0; i < il->hw_params.max_stations; i++) {
609 if ((il->stations[i].used & IL_STA_UCODE_INPROGRESS)) {
610 memcpy(&sta_cmd, &il->stations[i].sta,
611 sizeof(struct il_addsta_cmd));
613 if (il->stations[i].lq) {
614 memcpy(&lq, il->stations[i].lq,
615 sizeof(struct il_link_quality_cmd));
618 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
619 ret = il_send_add_sta(il, &sta_cmd, CMD_SYNC);
621 spin_lock_irqsave(&il->sta_lock, flags_spin);
622 IL_ERR("Adding station %pM failed.\n",
623 il->stations[i].sta.sta.addr);
624 il->stations[i].used &=
625 ~IL_STA_DRIVER_ACTIVE;
626 il->stations[i].used &=
627 ~IL_STA_UCODE_INPROGRESS;
628 spin_unlock_irqrestore(&il->sta_lock,
632 * Rate scaling has already been initialized, send
636 il_send_lq_cmd(il, ctx, &lq,
638 spin_lock_irqsave(&il->sta_lock, flags_spin);
639 il->stations[i].used &= ~IL_STA_UCODE_INPROGRESS;
643 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
645 D_INFO("Restoring all known stations"
646 " .... no stations to be restored.\n");
648 D_INFO("Restoring all known stations"
649 " .... complete.\n");
651 EXPORT_SYMBOL(il_restore_stations);
653 int il_get_free_ucode_key_idx(struct il_priv *il)
657 for (i = 0; i < il->sta_key_max_num; i++)
658 if (!test_and_set_bit(i, &il->ucode_key_table))
661 return WEP_INVALID_OFFSET;
663 EXPORT_SYMBOL(il_get_free_ucode_key_idx);
665 void il_dealloc_bcast_stations(struct il_priv *il)
670 spin_lock_irqsave(&il->sta_lock, flags);
671 for (i = 0; i < il->hw_params.max_stations; i++) {
672 if (!(il->stations[i].used & IL_STA_BCAST))
675 il->stations[i].used &= ~IL_STA_UCODE_ACTIVE;
677 BUG_ON(il->num_stations < 0);
678 kfree(il->stations[i].lq);
679 il->stations[i].lq = NULL;
681 spin_unlock_irqrestore(&il->sta_lock, flags);
683 EXPORT_SYMBOL_GPL(il_dealloc_bcast_stations);
685 #ifdef CONFIG_IWLEGACY_DEBUG
686 static void il_dump_lq_cmd(struct il_priv *il,
687 struct il_link_quality_cmd *lq)
690 D_RATE("lq station id 0x%x\n", lq->sta_id);
691 D_RATE("lq ant 0x%X 0x%X\n",
692 lq->general_params.single_stream_ant_msk,
693 lq->general_params.dual_stream_ant_msk);
695 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
696 D_RATE("lq idx %d 0x%X\n",
697 i, lq->rs_table[i].rate_n_flags);
700 static inline void il_dump_lq_cmd(struct il_priv *il,
701 struct il_link_quality_cmd *lq)
707 * il_is_lq_table_valid() - Test one aspect of LQ cmd for validity
709 * It sometimes happens when a HT rate has been in use and we
710 * loose connectivity with AP then mac80211 will first tell us that the
711 * current channel is not HT anymore before removing the station. In such a
712 * scenario the RXON flags will be updated to indicate we are not
713 * communicating HT anymore, but the LQ command may still contain HT rates.
714 * Test for this to prevent driver from sending LQ command between the time
715 * RXON flags are updated and when LQ command is updated.
717 static bool il_is_lq_table_valid(struct il_priv *il,
718 struct il_rxon_context *ctx,
719 struct il_link_quality_cmd *lq)
726 D_INFO("Channel %u is not an HT channel\n",
727 ctx->active.channel);
728 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
729 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
732 "idx %d of LQ expects HT channel\n",
741 * il_send_lq_cmd() - Send link quality command
742 * @init: This command is sent as part of station initialization right
743 * after station has been added.
745 * The link quality command is sent as the last step of station creation.
746 * This is the special case in which init is set and we call a callback in
747 * this case to clear the state indicating that station creation is in
750 int il_send_lq_cmd(struct il_priv *il, struct il_rxon_context *ctx,
751 struct il_link_quality_cmd *lq, u8 flags, bool init)
754 unsigned long flags_spin;
756 struct il_host_cmd cmd = {
757 .id = C_TX_LINK_QUALITY_CMD,
758 .len = sizeof(struct il_link_quality_cmd),
763 if (WARN_ON(lq->sta_id == IL_INVALID_STATION))
767 spin_lock_irqsave(&il->sta_lock, flags_spin);
768 if (!(il->stations[lq->sta_id].used & IL_STA_DRIVER_ACTIVE)) {
769 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
772 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
774 il_dump_lq_cmd(il, lq);
775 BUG_ON(init && (cmd.flags & CMD_ASYNC));
777 if (il_is_lq_table_valid(il, ctx, lq))
778 ret = il_send_cmd(il, &cmd);
782 if (cmd.flags & CMD_ASYNC)
786 D_INFO("init LQ command complete,"
787 " clearing sta addition status for sta %d\n",
789 spin_lock_irqsave(&il->sta_lock, flags_spin);
790 il->stations[lq->sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
791 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
795 EXPORT_SYMBOL(il_send_lq_cmd);
797 int il_mac_sta_remove(struct ieee80211_hw *hw,
798 struct ieee80211_vif *vif,
799 struct ieee80211_sta *sta)
801 struct il_priv *il = hw->priv;
802 struct il_station_priv_common *sta_common = (void *)sta->drv_priv;
805 D_INFO("received request to remove station %pM\n",
807 mutex_lock(&il->mutex);
808 D_INFO("proceeding to remove station %pM\n",
810 ret = il_remove_station(il, sta_common->sta_id, sta->addr);
812 IL_ERR("Error removing station %pM\n",
814 mutex_unlock(&il->mutex);
817 EXPORT_SYMBOL(il_mac_sta_remove);