1 /******************************************************************************
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
33 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *****************************************************************************/
63 #include <net/mac80211.h>
68 static int iwl_mvm_find_free_sta_id(struct iwl_mvm
*mvm
)
72 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART
, &mvm
->status
));
74 lockdep_assert_held(&mvm
->mutex
);
76 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
77 for (sta_id
= 0; sta_id
< IWL_MVM_STATION_COUNT
; sta_id
++)
78 if (!rcu_dereference_protected(mvm
->fw_id_to_mac_id
[sta_id
],
79 lockdep_is_held(&mvm
->mutex
)))
81 return IWL_MVM_STATION_COUNT
;
84 /* send station add/update command to firmware */
85 int iwl_mvm_sta_send_to_fw(struct iwl_mvm
*mvm
, struct ieee80211_sta
*sta
,
88 struct iwl_mvm_sta
*mvm_sta
= (void *)sta
->drv_priv
;
89 struct iwl_mvm_add_sta_cmd add_sta_cmd
;
92 u32 agg_size
= 0, mpdu_dens
= 0;
94 memset(&add_sta_cmd
, 0, sizeof(add_sta_cmd
));
96 add_sta_cmd
.sta_id
= mvm_sta
->sta_id
;
97 add_sta_cmd
.mac_id_n_color
= cpu_to_le32(mvm_sta
->mac_id_n_color
);
99 add_sta_cmd
.tfd_queue_msk
= cpu_to_le32(mvm_sta
->tfd_queue_msk
);
100 memcpy(&add_sta_cmd
.addr
, sta
->addr
, ETH_ALEN
);
102 add_sta_cmd
.add_modify
= update
? 1 : 0;
104 /* STA_FLG_FAT_EN_MSK ? */
105 /* STA_FLG_MIMO_EN_MSK ? */
107 if (sta
->ht_cap
.ht_supported
) {
108 add_sta_cmd
.station_flags_msk
|=
109 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK
|
110 STA_FLG_AGG_MPDU_DENS_MSK
);
112 mpdu_dens
= sta
->ht_cap
.ampdu_density
;
115 if (sta
->vht_cap
.vht_supported
) {
116 agg_size
= sta
->vht_cap
.cap
&
117 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK
;
119 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT
;
120 } else if (sta
->ht_cap
.ht_supported
) {
121 agg_size
= sta
->ht_cap
.ampdu_factor
;
124 add_sta_cmd
.station_flags
|=
125 cpu_to_le32(agg_size
<< STA_FLG_MAX_AGG_SIZE_SHIFT
);
126 add_sta_cmd
.station_flags
|=
127 cpu_to_le32(mpdu_dens
<< STA_FLG_AGG_MPDU_DENS_SHIFT
);
129 status
= ADD_STA_SUCCESS
;
130 ret
= iwl_mvm_send_cmd_pdu_status(mvm
, ADD_STA
, sizeof(add_sta_cmd
),
131 &add_sta_cmd
, &status
);
136 case ADD_STA_SUCCESS
:
137 IWL_DEBUG_ASSOC(mvm
, "ADD_STA PASSED\n");
141 IWL_ERR(mvm
, "ADD_STA failed\n");
148 int iwl_mvm_add_sta(struct iwl_mvm
*mvm
,
149 struct ieee80211_vif
*vif
,
150 struct ieee80211_sta
*sta
)
152 struct iwl_mvm_vif
*mvmvif
= iwl_mvm_vif_from_mac80211(vif
);
153 struct iwl_mvm_sta
*mvm_sta
= (void *)sta
->drv_priv
;
156 lockdep_assert_held(&mvm
->mutex
);
158 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART
, &mvm
->status
))
159 sta_id
= iwl_mvm_find_free_sta_id(mvm
);
161 sta_id
= mvm_sta
->sta_id
;
163 if (WARN_ON_ONCE(sta_id
== IWL_MVM_STATION_COUNT
))
166 spin_lock_init(&mvm_sta
->lock
);
168 mvm_sta
->sta_id
= sta_id
;
169 mvm_sta
->mac_id_n_color
= FW_CMD_ID_AND_COLOR(mvmvif
->id
,
172 mvm_sta
->max_agg_bufsize
= LINK_QUAL_AGG_FRAME_LIMIT_DEF
;
174 /* HW restart, don't assume the memory has been zeroed */
175 atomic_set(&mvm_sta
->pending_frames
, 0);
176 mvm_sta
->tid_disable_agg
= 0;
177 mvm_sta
->tfd_queue_msk
= 0;
178 for (i
= 0; i
< IEEE80211_NUM_ACS
; i
++)
179 if (vif
->hw_queue
[i
] != IEEE80211_INVAL_HW_QUEUE
)
180 mvm_sta
->tfd_queue_msk
|= BIT(vif
->hw_queue
[i
]);
182 if (vif
->cab_queue
!= IEEE80211_INVAL_HW_QUEUE
)
183 mvm_sta
->tfd_queue_msk
|= BIT(vif
->cab_queue
);
185 /* for HW restart - need to reset the seq_number etc... */
186 memset(mvm_sta
->tid_data
, 0, sizeof(mvm_sta
->tid_data
));
188 ret
= iwl_mvm_sta_send_to_fw(mvm
, sta
, false);
192 /* The first station added is the AP, the others are TDLS STAs */
193 if (vif
->type
== NL80211_IFTYPE_STATION
&&
194 mvmvif
->ap_sta_id
== IWL_MVM_STATION_COUNT
)
195 mvmvif
->ap_sta_id
= sta_id
;
197 rcu_assign_pointer(mvm
->fw_id_to_mac_id
[sta_id
], sta
);
202 int iwl_mvm_update_sta(struct iwl_mvm
*mvm
,
203 struct ieee80211_vif
*vif
,
204 struct ieee80211_sta
*sta
)
206 return iwl_mvm_sta_send_to_fw(mvm
, sta
, true);
209 int iwl_mvm_drain_sta(struct iwl_mvm
*mvm
, struct iwl_mvm_sta
*mvmsta
,
212 struct iwl_mvm_add_sta_cmd cmd
= {};
216 lockdep_assert_held(&mvm
->mutex
);
218 cmd
.mac_id_n_color
= cpu_to_le32(mvmsta
->mac_id_n_color
);
219 cmd
.sta_id
= mvmsta
->sta_id
;
220 cmd
.add_modify
= STA_MODE_MODIFY
;
221 cmd
.station_flags
= drain
? cpu_to_le32(STA_FLG_DRAIN_FLOW
) : 0;
222 cmd
.station_flags_msk
= cpu_to_le32(STA_FLG_DRAIN_FLOW
);
224 status
= ADD_STA_SUCCESS
;
225 ret
= iwl_mvm_send_cmd_pdu_status(mvm
, ADD_STA
, sizeof(cmd
),
231 case ADD_STA_SUCCESS
:
232 IWL_DEBUG_INFO(mvm
, "Frames for staid %d will drained in fw\n",
237 IWL_ERR(mvm
, "Couldn't drain frames for staid %d\n",
246 * Remove a station from the FW table. Before sending the command to remove
247 * the station validate that the station is indeed known to the driver (sanity
250 static int iwl_mvm_rm_sta_common(struct iwl_mvm
*mvm
, u8 sta_id
)
252 struct ieee80211_sta
*sta
;
253 struct iwl_mvm_rm_sta_cmd rm_sta_cmd
= {
258 sta
= rcu_dereference_protected(mvm
->fw_id_to_mac_id
[sta_id
],
259 lockdep_is_held(&mvm
->mutex
));
261 /* Note: internal stations are marked as error values */
263 IWL_ERR(mvm
, "Invalid station id\n");
267 ret
= iwl_mvm_send_cmd_pdu(mvm
, REMOVE_STA
, CMD_SYNC
,
268 sizeof(rm_sta_cmd
), &rm_sta_cmd
);
270 IWL_ERR(mvm
, "Failed to remove station. Id=%d\n", sta_id
);
277 void iwl_mvm_sta_drained_wk(struct work_struct
*wk
)
279 struct iwl_mvm
*mvm
= container_of(wk
, struct iwl_mvm
, sta_drained_wk
);
283 * The mutex is needed because of the SYNC cmd, but not only: if the
284 * work would run concurrently with iwl_mvm_rm_sta, it would run before
285 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
286 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
289 mutex_lock(&mvm
->mutex
);
291 for_each_set_bit(sta_id
, mvm
->sta_drained
, IWL_MVM_STATION_COUNT
) {
293 struct ieee80211_sta
*sta
=
294 rcu_dereference_protected(mvm
->fw_id_to_mac_id
[sta_id
],
295 lockdep_is_held(&mvm
->mutex
));
297 /* This station is in use */
301 if (PTR_ERR(sta
) == -EINVAL
) {
302 IWL_ERR(mvm
, "Drained sta %d, but it is internal?\n",
308 IWL_ERR(mvm
, "Drained sta %d, but it was NULL?\n",
313 WARN_ON(PTR_ERR(sta
) != -EBUSY
);
314 /* This station was removed and we waited until it got drained,
315 * we can now proceed and remove it.
317 ret
= iwl_mvm_rm_sta_common(mvm
, sta_id
);
320 "Couldn't remove sta %d after it was drained\n",
324 rcu_assign_pointer(mvm
->fw_id_to_mac_id
[sta_id
], NULL
);
325 clear_bit(sta_id
, mvm
->sta_drained
);
328 mutex_unlock(&mvm
->mutex
);
331 int iwl_mvm_rm_sta(struct iwl_mvm
*mvm
,
332 struct ieee80211_vif
*vif
,
333 struct ieee80211_sta
*sta
)
335 struct iwl_mvm_vif
*mvmvif
= iwl_mvm_vif_from_mac80211(vif
);
336 struct iwl_mvm_sta
*mvm_sta
= (void *)sta
->drv_priv
;
339 lockdep_assert_held(&mvm
->mutex
);
341 if (vif
->type
== NL80211_IFTYPE_STATION
&&
342 mvmvif
->ap_sta_id
== mvm_sta
->sta_id
) {
344 * Put a non-NULL since the fw station isn't removed.
345 * It will be removed after the MAC will be set as
348 rcu_assign_pointer(mvm
->fw_id_to_mac_id
[mvm_sta
->sta_id
],
351 /* flush its queues here since we are freeing mvm_sta */
352 ret
= iwl_mvm_flush_tx_path(mvm
, mvm_sta
->tfd_queue_msk
, true);
354 /* if we are associated - we can't remove the AP STA now */
355 if (vif
->bss_conf
.assoc
)
358 /* unassoc - go ahead - remove the AP STA now */
359 mvmvif
->ap_sta_id
= IWL_MVM_STATION_COUNT
;
363 * There are frames pending on the AC queues for this station.
364 * We need to wait until all the frames are drained...
366 if (atomic_read(&mvm_sta
->pending_frames
)) {
367 ret
= iwl_mvm_drain_sta(mvm
, mvm_sta
, true);
368 rcu_assign_pointer(mvm
->fw_id_to_mac_id
[mvm_sta
->sta_id
],
371 ret
= iwl_mvm_rm_sta_common(mvm
, mvm_sta
->sta_id
);
372 rcu_assign_pointer(mvm
->fw_id_to_mac_id
[mvm_sta
->sta_id
], NULL
);
378 int iwl_mvm_rm_sta_id(struct iwl_mvm
*mvm
,
379 struct ieee80211_vif
*vif
,
382 int ret
= iwl_mvm_rm_sta_common(mvm
, sta_id
);
384 lockdep_assert_held(&mvm
->mutex
);
386 rcu_assign_pointer(mvm
->fw_id_to_mac_id
[sta_id
], NULL
);
390 int iwl_mvm_allocate_int_sta(struct iwl_mvm
*mvm
, struct iwl_mvm_int_sta
*sta
,
393 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART
, &mvm
->status
)) {
394 sta
->sta_id
= iwl_mvm_find_free_sta_id(mvm
);
395 if (WARN_ON_ONCE(sta
->sta_id
== IWL_MVM_STATION_COUNT
))
399 sta
->tfd_queue_msk
= qmask
;
401 /* put a non-NULL value so iterating over the stations won't stop */
402 rcu_assign_pointer(mvm
->fw_id_to_mac_id
[sta
->sta_id
], ERR_PTR(-EINVAL
));
406 void iwl_mvm_dealloc_int_sta(struct iwl_mvm
*mvm
, struct iwl_mvm_int_sta
*sta
)
408 rcu_assign_pointer(mvm
->fw_id_to_mac_id
[sta
->sta_id
], NULL
);
409 memset(sta
, 0, sizeof(struct iwl_mvm_int_sta
));
410 sta
->sta_id
= IWL_MVM_STATION_COUNT
;
413 static int iwl_mvm_add_int_sta_common(struct iwl_mvm
*mvm
,
414 struct iwl_mvm_int_sta
*sta
,
416 u16 mac_id
, u16 color
)
418 struct iwl_mvm_add_sta_cmd cmd
;
422 lockdep_assert_held(&mvm
->mutex
);
424 memset(&cmd
, 0, sizeof(struct iwl_mvm_add_sta_cmd
));
425 cmd
.sta_id
= sta
->sta_id
;
426 cmd
.mac_id_n_color
= cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id
,
429 cmd
.tfd_queue_msk
= cpu_to_le32(sta
->tfd_queue_msk
);
432 memcpy(cmd
.addr
, addr
, ETH_ALEN
);
434 ret
= iwl_mvm_send_cmd_pdu_status(mvm
, ADD_STA
, sizeof(cmd
),
440 case ADD_STA_SUCCESS
:
441 IWL_DEBUG_INFO(mvm
, "Internal station added.\n");
445 IWL_ERR(mvm
, "Add internal station failed, status=0x%x\n",
452 int iwl_mvm_add_aux_sta(struct iwl_mvm
*mvm
)
456 lockdep_assert_held(&mvm
->mutex
);
458 /* Add the aux station, but without any queues */
459 ret
= iwl_mvm_allocate_int_sta(mvm
, &mvm
->aux_sta
, 0);
463 ret
= iwl_mvm_add_int_sta_common(mvm
, &mvm
->aux_sta
, NULL
,
467 iwl_mvm_dealloc_int_sta(mvm
, &mvm
->aux_sta
);
472 * Send the add station command for the vif's broadcast station.
473 * Assumes that the station was already allocated.
475 * @mvm: the mvm component
476 * @vif: the interface to which the broadcast station is added
477 * @bsta: the broadcast station to add.
479 int iwl_mvm_send_bcast_sta(struct iwl_mvm
*mvm
, struct ieee80211_vif
*vif
,
480 struct iwl_mvm_int_sta
*bsta
)
482 struct iwl_mvm_vif
*mvmvif
= iwl_mvm_vif_from_mac80211(vif
);
483 static const u8 baddr
[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
485 lockdep_assert_held(&mvm
->mutex
);
487 if (WARN_ON_ONCE(bsta
->sta_id
== IWL_MVM_STATION_COUNT
))
490 return iwl_mvm_add_int_sta_common(mvm
, bsta
, baddr
,
491 mvmvif
->id
, mvmvif
->color
);
494 /* Send the FW a request to remove the station from it's internal data
495 * structures, but DO NOT remove the entry from the local data structures. */
496 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm
*mvm
,
497 struct iwl_mvm_int_sta
*bsta
)
501 lockdep_assert_held(&mvm
->mutex
);
503 ret
= iwl_mvm_rm_sta_common(mvm
, bsta
->sta_id
);
505 IWL_WARN(mvm
, "Failed sending remove station\n");
509 /* Allocate a new station entry for the broadcast station to the given vif,
510 * and send it to the FW.
511 * Note that each P2P mac should have its own broadcast station.
513 * @mvm: the mvm component
514 * @vif: the interface to which the broadcast station is added
515 * @bsta: the broadcast station to add. */
516 int iwl_mvm_add_bcast_sta(struct iwl_mvm
*mvm
, struct ieee80211_vif
*vif
,
517 struct iwl_mvm_int_sta
*bsta
)
519 struct iwl_mvm_vif
*mvmvif
= iwl_mvm_vif_from_mac80211(vif
);
520 static const u8 baddr
[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
524 lockdep_assert_held(&mvm
->mutex
);
526 qmask
= iwl_mvm_mac_get_queues_mask(mvm
, vif
);
527 ret
= iwl_mvm_allocate_int_sta(mvm
, bsta
, qmask
);
531 ret
= iwl_mvm_add_int_sta_common(mvm
, bsta
, baddr
,
532 mvmvif
->id
, mvmvif
->color
);
535 iwl_mvm_dealloc_int_sta(mvm
, bsta
);
540 * Send the FW a request to remove the station from it's internal data
541 * structures, and in addition remove it from the local data structure.
543 int iwl_mvm_rm_bcast_sta(struct iwl_mvm
*mvm
, struct iwl_mvm_int_sta
*bsta
)
547 lockdep_assert_held(&mvm
->mutex
);
549 ret
= iwl_mvm_rm_sta_common(mvm
, bsta
->sta_id
);
553 iwl_mvm_dealloc_int_sta(mvm
, bsta
);
557 int iwl_mvm_sta_rx_agg(struct iwl_mvm
*mvm
, struct ieee80211_sta
*sta
,
558 int tid
, u16 ssn
, bool start
)
560 struct iwl_mvm_sta
*mvm_sta
= (void *)sta
->drv_priv
;
561 struct iwl_mvm_add_sta_cmd cmd
= {};
565 lockdep_assert_held(&mvm
->mutex
);
567 cmd
.mac_id_n_color
= cpu_to_le32(mvm_sta
->mac_id_n_color
);
568 cmd
.sta_id
= mvm_sta
->sta_id
;
569 cmd
.add_modify
= STA_MODE_MODIFY
;
570 cmd
.add_immediate_ba_tid
= (u8
) tid
;
571 cmd
.add_immediate_ba_ssn
= cpu_to_le16(ssn
);
572 cmd
.modify_mask
= start
? STA_MODIFY_ADD_BA_TID
:
573 STA_MODIFY_REMOVE_BA_TID
;
575 status
= ADD_STA_SUCCESS
;
576 ret
= iwl_mvm_send_cmd_pdu_status(mvm
, ADD_STA
, sizeof(cmd
),
582 case ADD_STA_SUCCESS
:
583 IWL_DEBUG_INFO(mvm
, "RX BA Session %sed in fw\n",
584 start
? "start" : "stopp");
586 case ADD_STA_IMMEDIATE_BA_FAILURE
:
587 IWL_WARN(mvm
, "RX BA Session refused by fw\n");
592 IWL_ERR(mvm
, "RX BA Session failed %sing, status 0x%x\n",
593 start
? "start" : "stopp", status
);
600 static int iwl_mvm_sta_tx_agg(struct iwl_mvm
*mvm
, struct ieee80211_sta
*sta
,
601 int tid
, u8 queue
, bool start
)
603 struct iwl_mvm_sta
*mvm_sta
= (void *)sta
->drv_priv
;
604 struct iwl_mvm_add_sta_cmd cmd
= {};
608 lockdep_assert_held(&mvm
->mutex
);
611 mvm_sta
->tfd_queue_msk
|= BIT(queue
);
612 mvm_sta
->tid_disable_agg
&= ~BIT(tid
);
614 mvm_sta
->tfd_queue_msk
&= ~BIT(queue
);
615 mvm_sta
->tid_disable_agg
|= BIT(tid
);
618 cmd
.mac_id_n_color
= cpu_to_le32(mvm_sta
->mac_id_n_color
);
619 cmd
.sta_id
= mvm_sta
->sta_id
;
620 cmd
.add_modify
= STA_MODE_MODIFY
;
621 cmd
.modify_mask
= STA_MODIFY_QUEUES
| STA_MODIFY_TID_DISABLE_TX
;
622 cmd
.tfd_queue_msk
= cpu_to_le32(mvm_sta
->tfd_queue_msk
);
623 cmd
.tid_disable_tx
= cpu_to_le16(mvm_sta
->tid_disable_agg
);
625 status
= ADD_STA_SUCCESS
;
626 ret
= iwl_mvm_send_cmd_pdu_status(mvm
, ADD_STA
, sizeof(cmd
),
632 case ADD_STA_SUCCESS
:
636 IWL_ERR(mvm
, "TX BA Session failed %sing, status 0x%x\n",
637 start
? "start" : "stopp", status
);
644 static const u8 tid_to_ac
[] = {
655 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm
*mvm
, struct ieee80211_vif
*vif
,
656 struct ieee80211_sta
*sta
, u16 tid
, u16
*ssn
)
658 struct iwl_mvm_sta
*mvmsta
= (void *)sta
->drv_priv
;
659 struct iwl_mvm_tid_data
*tid_data
;
662 if (WARN_ON_ONCE(tid
>= IWL_MAX_TID_COUNT
))
665 if (mvmsta
->tid_data
[tid
].state
!= IWL_AGG_OFF
) {
666 IWL_ERR(mvm
, "Start AGG when state is not IWL_AGG_OFF %d!\n",
667 mvmsta
->tid_data
[tid
].state
);
671 lockdep_assert_held(&mvm
->mutex
);
673 for (txq_id
= IWL_MVM_FIRST_AGG_QUEUE
;
674 txq_id
<= IWL_MVM_LAST_AGG_QUEUE
; txq_id
++)
675 if (mvm
->queue_to_mac80211
[txq_id
] ==
676 IWL_INVALID_MAC80211_QUEUE
)
679 if (txq_id
> IWL_MVM_LAST_AGG_QUEUE
) {
680 IWL_ERR(mvm
, "Failed to allocate agg queue\n");
684 /* the new tx queue is still connected to the same mac80211 queue */
685 mvm
->queue_to_mac80211
[txq_id
] = vif
->hw_queue
[tid_to_ac
[tid
]];
687 spin_lock_bh(&mvmsta
->lock
);
688 tid_data
= &mvmsta
->tid_data
[tid
];
689 tid_data
->ssn
= SEQ_TO_SN(tid_data
->seq_number
);
690 tid_data
->txq_id
= txq_id
;
691 *ssn
= tid_data
->ssn
;
693 IWL_DEBUG_TX_QUEUES(mvm
,
694 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
695 mvmsta
->sta_id
, tid
, txq_id
, tid_data
->ssn
,
696 tid_data
->next_reclaimed
);
698 if (tid_data
->ssn
== tid_data
->next_reclaimed
) {
699 tid_data
->state
= IWL_AGG_STARTING
;
700 ieee80211_start_tx_ba_cb_irqsafe(vif
, sta
->addr
, tid
);
702 tid_data
->state
= IWL_EMPTYING_HW_QUEUE_ADDBA
;
705 spin_unlock_bh(&mvmsta
->lock
);
710 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm
*mvm
, struct ieee80211_vif
*vif
,
711 struct ieee80211_sta
*sta
, u16 tid
, u8 buf_size
)
713 struct iwl_mvm_sta
*mvmsta
= (void *)sta
->drv_priv
;
714 struct iwl_mvm_tid_data
*tid_data
= &mvmsta
->tid_data
[tid
];
715 int queue
, fifo
, ret
;
718 buf_size
= min_t(int, buf_size
, LINK_QUAL_AGG_FRAME_LIMIT_DEF
);
720 spin_lock_bh(&mvmsta
->lock
);
722 queue
= tid_data
->txq_id
;
723 tid_data
->state
= IWL_AGG_ON
;
724 tid_data
->ssn
= 0xffff;
725 spin_unlock_bh(&mvmsta
->lock
);
727 fifo
= iwl_mvm_ac_to_tx_fifo
[tid_to_ac
[tid
]];
729 ret
= iwl_mvm_sta_tx_agg(mvm
, sta
, tid
, queue
, true);
733 iwl_trans_txq_enable(mvm
->trans
, queue
, fifo
, mvmsta
->sta_id
, tid
,
737 * Even though in theory the peer could have different
738 * aggregation reorder buffer sizes for different sessions,
739 * our ucode doesn't allow for that and has a global limit
740 * for each station. Therefore, use the minimum of all the
741 * aggregation sessions and our default value.
743 mvmsta
->max_agg_bufsize
=
744 min(mvmsta
->max_agg_bufsize
, buf_size
);
745 mvmsta
->lq_sta
.lq
.agg_frame_cnt_limit
= mvmsta
->max_agg_bufsize
;
747 if (mvm
->cfg
->ht_params
->use_rts_for_aggregation
) {
749 * switch to RTS/CTS if it is the prefer protection
750 * method for HT traffic
752 mvmsta
->lq_sta
.lq
.flags
|= LQ_FLAG_SET_STA_TLC_RTS_MSK
;
754 * TODO: remove the TLC_RTS flag when we tear down the last
755 * AGG session (agg_tids_count in DVM)
759 IWL_DEBUG_HT(mvm
, "Tx aggregation enabled on ra = %pM tid = %d\n",
762 return iwl_mvm_send_lq_cmd(mvm
, &mvmsta
->lq_sta
.lq
, CMD_ASYNC
, false);
765 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm
*mvm
, struct ieee80211_vif
*vif
,
766 struct ieee80211_sta
*sta
, u16 tid
)
768 struct iwl_mvm_sta
*mvmsta
= (void *)sta
->drv_priv
;
769 struct iwl_mvm_tid_data
*tid_data
= &mvmsta
->tid_data
[tid
];
773 spin_lock_bh(&mvmsta
->lock
);
775 txq_id
= tid_data
->txq_id
;
777 IWL_DEBUG_TX_QUEUES(mvm
, "Stop AGG: sta %d tid %d q %d state %d\n",
778 mvmsta
->sta_id
, tid
, txq_id
, tid_data
->state
);
780 switch (tid_data
->state
) {
782 tid_data
->ssn
= SEQ_TO_SN(tid_data
->seq_number
);
784 IWL_DEBUG_TX_QUEUES(mvm
,
785 "ssn = %d, next_recl = %d\n",
786 tid_data
->ssn
, tid_data
->next_reclaimed
);
788 /* There are still packets for this RA / TID in the HW */
789 if (tid_data
->ssn
!= tid_data
->next_reclaimed
) {
790 tid_data
->state
= IWL_EMPTYING_HW_QUEUE_DELBA
;
795 tid_data
->ssn
= 0xffff;
796 iwl_trans_txq_disable(mvm
->trans
, txq_id
);
798 case IWL_AGG_STARTING
:
799 case IWL_EMPTYING_HW_QUEUE_ADDBA
:
801 * The agg session has been stopped before it was set up. This
802 * can happen when the AddBA timer times out for example.
805 /* No barriers since we are under mutex */
806 lockdep_assert_held(&mvm
->mutex
);
807 mvm
->queue_to_mac80211
[txq_id
] = IWL_INVALID_MAC80211_QUEUE
;
809 ieee80211_stop_tx_ba_cb_irqsafe(vif
, sta
->addr
, tid
);
810 tid_data
->state
= IWL_AGG_OFF
;
815 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
816 mvmsta
->sta_id
, tid
, tid_data
->state
);
818 "\ttid_data->txq_id = %d\n", tid_data
->txq_id
);
822 spin_unlock_bh(&mvmsta
->lock
);
827 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm
*mvm
)
831 lockdep_assert_held(&mvm
->mutex
);
833 i
= find_first_zero_bit(mvm
->fw_key_table
, STA_KEY_MAX_NUM
);
835 if (i
== STA_KEY_MAX_NUM
)
836 return STA_KEY_IDX_INVALID
;
838 __set_bit(i
, mvm
->fw_key_table
);
843 static u8
iwl_mvm_get_key_sta_id(struct ieee80211_vif
*vif
,
844 struct ieee80211_sta
*sta
)
846 struct iwl_mvm_vif
*mvmvif
= (void *)vif
->drv_priv
;
849 struct iwl_mvm_sta
*mvm_sta
= (void *)sta
->drv_priv
;
851 return mvm_sta
->sta_id
;
855 * The device expects GTKs for station interfaces to be
856 * installed as GTKs for the AP station. If we have no
857 * station ID, then use AP's station ID.
859 if (vif
->type
== NL80211_IFTYPE_STATION
&&
860 mvmvif
->ap_sta_id
!= IWL_MVM_STATION_COUNT
)
861 return mvmvif
->ap_sta_id
;
863 return IWL_INVALID_STATION
;
866 static int iwl_mvm_send_sta_key(struct iwl_mvm
*mvm
,
867 struct iwl_mvm_sta
*mvm_sta
,
868 struct ieee80211_key_conf
*keyconf
,
869 u8 sta_id
, u32 tkip_iv32
, u16
*tkip_p1k
,
873 struct iwl_mvm_add_sta_cmd cmd
= {};
878 keyidx
= (keyconf
->keyidx
<< STA_KEY_FLG_KEYID_POS
) &
879 STA_KEY_FLG_KEYID_MSK
;
880 key_flags
= cpu_to_le16(keyidx
);
881 key_flags
|= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP
);
883 switch (keyconf
->cipher
) {
884 case WLAN_CIPHER_SUITE_TKIP
:
885 key_flags
|= cpu_to_le16(STA_KEY_FLG_TKIP
);
886 cmd
.key
.tkip_rx_tsc_byte2
= tkip_iv32
;
887 for (i
= 0; i
< 5; i
++)
888 cmd
.key
.tkip_rx_ttak
[i
] = cpu_to_le16(tkip_p1k
[i
]);
889 memcpy(cmd
.key
.key
, keyconf
->key
, keyconf
->keylen
);
891 case WLAN_CIPHER_SUITE_CCMP
:
892 key_flags
|= cpu_to_le16(STA_KEY_FLG_CCM
);
893 memcpy(cmd
.key
.key
, keyconf
->key
, keyconf
->keylen
);
900 if (!(keyconf
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
))
901 key_flags
|= cpu_to_le16(STA_KEY_MULTICAST
);
903 cmd
.mac_id_n_color
= cpu_to_le32(mvm_sta
->mac_id_n_color
);
904 cmd
.key
.key_offset
= keyconf
->hw_key_idx
;
905 cmd
.key
.key_flags
= key_flags
;
906 cmd
.add_modify
= STA_MODE_MODIFY
;
907 cmd
.modify_mask
= STA_MODIFY_KEY
;
910 status
= ADD_STA_SUCCESS
;
911 if (cmd_flags
== CMD_SYNC
)
912 ret
= iwl_mvm_send_cmd_pdu_status(mvm
, ADD_STA
, sizeof(cmd
),
915 ret
= iwl_mvm_send_cmd_pdu(mvm
, ADD_STA
, CMD_ASYNC
,
919 case ADD_STA_SUCCESS
:
920 IWL_DEBUG_WEP(mvm
, "MODIFY_STA: set dynamic key passed\n");
924 IWL_ERR(mvm
, "MODIFY_STA: set dynamic key failed\n");
931 static int iwl_mvm_send_sta_igtk(struct iwl_mvm
*mvm
,
932 struct ieee80211_key_conf
*keyconf
,
933 u8 sta_id
, bool remove_key
)
935 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd
= {};
937 /* verify the key details match the required command's expectations */
938 if (WARN_ON((keyconf
->cipher
!= WLAN_CIPHER_SUITE_AES_CMAC
) ||
939 (keyconf
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
) ||
940 (keyconf
->keyidx
!= 4 && keyconf
->keyidx
!= 5)))
943 igtk_cmd
.key_id
= cpu_to_le32(keyconf
->keyidx
);
944 igtk_cmd
.sta_id
= cpu_to_le32(sta_id
);
947 igtk_cmd
.ctrl_flags
|= cpu_to_le32(STA_KEY_NOT_VALID
);
949 struct ieee80211_key_seq seq
;
952 memcpy(igtk_cmd
.IGTK
, keyconf
->key
, keyconf
->keylen
);
953 ieee80211_aes_cmac_calculate_k1_k2(keyconf
,
954 igtk_cmd
.K1
, igtk_cmd
.K2
);
955 ieee80211_get_key_rx_seq(keyconf
, 0, &seq
);
956 pn
= seq
.aes_cmac
.pn
;
957 igtk_cmd
.receive_seq_cnt
= cpu_to_le64(((u64
) pn
[5] << 0) |
959 ((u64
) pn
[3] << 16) |
960 ((u64
) pn
[2] << 24) |
961 ((u64
) pn
[1] << 32) |
962 ((u64
) pn
[0] << 40));
965 IWL_DEBUG_INFO(mvm
, "%s igtk for sta %u\n",
966 remove_key
? "removing" : "installing",
969 return iwl_mvm_send_cmd_pdu(mvm
, MGMT_MCAST_KEY
, CMD_SYNC
,
970 sizeof(igtk_cmd
), &igtk_cmd
);
974 static inline u8
*iwl_mvm_get_mac_addr(struct iwl_mvm
*mvm
,
975 struct ieee80211_vif
*vif
,
976 struct ieee80211_sta
*sta
)
978 struct iwl_mvm_vif
*mvmvif
= (void *)vif
->drv_priv
;
983 if (vif
->type
== NL80211_IFTYPE_STATION
&&
984 mvmvif
->ap_sta_id
!= IWL_MVM_STATION_COUNT
) {
985 u8 sta_id
= mvmvif
->ap_sta_id
;
986 sta
= rcu_dereference_protected(mvm
->fw_id_to_mac_id
[sta_id
],
987 lockdep_is_held(&mvm
->mutex
));
995 int iwl_mvm_set_sta_key(struct iwl_mvm
*mvm
,
996 struct ieee80211_vif
*vif
,
997 struct ieee80211_sta
*sta
,
998 struct ieee80211_key_conf
*keyconf
,
999 bool have_key_offset
)
1001 struct iwl_mvm_sta
*mvm_sta
;
1004 struct ieee80211_key_seq seq
;
1007 lockdep_assert_held(&mvm
->mutex
);
1009 /* Get the station id from the mvm local station table */
1010 sta_id
= iwl_mvm_get_key_sta_id(vif
, sta
);
1011 if (sta_id
== IWL_INVALID_STATION
) {
1012 IWL_ERR(mvm
, "Failed to find station id\n");
1016 if (keyconf
->cipher
== WLAN_CIPHER_SUITE_AES_CMAC
) {
1017 ret
= iwl_mvm_send_sta_igtk(mvm
, keyconf
, sta_id
, false);
1022 * It is possible that the 'sta' parameter is NULL, and thus
1023 * there is a need to retrieve the sta from the local station table.
1026 sta
= rcu_dereference_protected(mvm
->fw_id_to_mac_id
[sta_id
],
1027 lockdep_is_held(&mvm
->mutex
));
1028 if (IS_ERR_OR_NULL(sta
)) {
1029 IWL_ERR(mvm
, "Invalid station id\n");
1034 mvm_sta
= (struct iwl_mvm_sta
*)sta
->drv_priv
;
1035 if (WARN_ON_ONCE(mvm_sta
->vif
!= vif
))
1038 if (!have_key_offset
) {
1040 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1041 * configure it there. As a result, this workaround exists to
1042 * let the caller set the key offset (hw_key_idx), see d3.c.
1044 keyconf
->hw_key_idx
= iwl_mvm_set_fw_key_idx(mvm
);
1045 if (keyconf
->hw_key_idx
== STA_KEY_IDX_INVALID
)
1049 switch (keyconf
->cipher
) {
1050 case WLAN_CIPHER_SUITE_TKIP
:
1051 addr
= iwl_mvm_get_mac_addr(mvm
, vif
, sta
);
1052 /* get phase 1 key from mac80211 */
1053 ieee80211_get_key_rx_seq(keyconf
, 0, &seq
);
1054 ieee80211_get_tkip_rx_p1k(keyconf
, addr
, seq
.tkip
.iv32
, p1k
);
1055 ret
= iwl_mvm_send_sta_key(mvm
, mvm_sta
, keyconf
, sta_id
,
1056 seq
.tkip
.iv32
, p1k
, CMD_SYNC
);
1058 case WLAN_CIPHER_SUITE_CCMP
:
1059 ret
= iwl_mvm_send_sta_key(mvm
, mvm_sta
, keyconf
, sta_id
,
1063 IWL_ERR(mvm
, "Unknown cipher %x\n", keyconf
->cipher
);
1068 __clear_bit(keyconf
->hw_key_idx
, mvm
->fw_key_table
);
1071 IWL_DEBUG_WEP(mvm
, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1072 keyconf
->cipher
, keyconf
->keylen
, keyconf
->keyidx
,
1077 int iwl_mvm_remove_sta_key(struct iwl_mvm
*mvm
,
1078 struct ieee80211_vif
*vif
,
1079 struct ieee80211_sta
*sta
,
1080 struct ieee80211_key_conf
*keyconf
)
1082 struct iwl_mvm_sta
*mvm_sta
;
1083 struct iwl_mvm_add_sta_cmd cmd
= {};
1088 lockdep_assert_held(&mvm
->mutex
);
1090 /* Get the station id from the mvm local station table */
1091 sta_id
= iwl_mvm_get_key_sta_id(vif
, sta
);
1093 IWL_DEBUG_WEP(mvm
, "mvm remove dynamic key: idx=%d sta=%d\n",
1094 keyconf
->keyidx
, sta_id
);
1096 if (keyconf
->cipher
== WLAN_CIPHER_SUITE_AES_CMAC
)
1097 return iwl_mvm_send_sta_igtk(mvm
, keyconf
, sta_id
, true);
1099 ret
= __test_and_clear_bit(keyconf
->hw_key_idx
, mvm
->fw_key_table
);
1101 IWL_ERR(mvm
, "offset %d not used in fw key table.\n",
1102 keyconf
->hw_key_idx
);
1106 if (sta_id
== IWL_INVALID_STATION
) {
1107 IWL_DEBUG_WEP(mvm
, "station non-existent, early return.\n");
1112 * It is possible that the 'sta' parameter is NULL, and thus
1113 * there is a need to retrieve the sta from the local station table,
1114 * for example when a GTK is removed (where the sta_id will then be
1115 * the AP ID, and no station was passed by mac80211.)
1118 sta
= rcu_dereference_protected(mvm
->fw_id_to_mac_id
[sta_id
],
1119 lockdep_is_held(&mvm
->mutex
));
1121 IWL_ERR(mvm
, "Invalid station id\n");
1126 mvm_sta
= (struct iwl_mvm_sta
*)sta
->drv_priv
;
1127 if (WARN_ON_ONCE(mvm_sta
->vif
!= vif
))
1130 key_flags
= cpu_to_le16((keyconf
->keyidx
<< STA_KEY_FLG_KEYID_POS
) &
1131 STA_KEY_FLG_KEYID_MSK
);
1132 key_flags
|= cpu_to_le16(STA_KEY_FLG_NO_ENC
| STA_KEY_FLG_WEP_KEY_MAP
);
1133 key_flags
|= cpu_to_le16(STA_KEY_NOT_VALID
);
1135 if (!(keyconf
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
))
1136 key_flags
|= cpu_to_le16(STA_KEY_MULTICAST
);
1138 cmd
.mac_id_n_color
= cpu_to_le32(mvm_sta
->mac_id_n_color
);
1139 cmd
.key
.key_flags
= key_flags
;
1140 cmd
.key
.key_offset
= keyconf
->hw_key_idx
;
1141 cmd
.sta_id
= sta_id
;
1143 cmd
.modify_mask
= STA_MODIFY_KEY
;
1144 cmd
.add_modify
= STA_MODE_MODIFY
;
1146 status
= ADD_STA_SUCCESS
;
1147 ret
= iwl_mvm_send_cmd_pdu_status(mvm
, ADD_STA
, sizeof(cmd
),
1151 case ADD_STA_SUCCESS
:
1152 IWL_DEBUG_WEP(mvm
, "MODIFY_STA: remove sta key passed\n");
1156 IWL_ERR(mvm
, "MODIFY_STA: remove sta key failed\n");
1163 void iwl_mvm_update_tkip_key(struct iwl_mvm
*mvm
,
1164 struct ieee80211_vif
*vif
,
1165 struct ieee80211_key_conf
*keyconf
,
1166 struct ieee80211_sta
*sta
, u32 iv32
,
1169 struct iwl_mvm_sta
*mvm_sta
;
1170 u8 sta_id
= iwl_mvm_get_key_sta_id(vif
, sta
);
1172 if (WARN_ON_ONCE(sta_id
== IWL_INVALID_STATION
))
1178 sta
= rcu_dereference(mvm
->fw_id_to_mac_id
[sta_id
]);
1179 if (WARN_ON(IS_ERR_OR_NULL(sta
))) {
1185 mvm_sta
= (void *)sta
->drv_priv
;
1186 iwl_mvm_send_sta_key(mvm
, mvm_sta
, keyconf
, sta_id
,
1187 iv32
, phase1key
, CMD_ASYNC
);
1191 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm
*mvm
,
1192 struct ieee80211_sta
*sta
)
1194 struct iwl_mvm_sta
*mvmsta
= (void *)sta
->drv_priv
;
1195 struct iwl_mvm_add_sta_cmd cmd
= {
1196 .add_modify
= STA_MODE_MODIFY
,
1197 .sta_id
= mvmsta
->sta_id
,
1198 .modify_mask
= STA_MODIFY_SLEEPING_STA_TX_COUNT
,
1199 .sleep_state_flags
= cpu_to_le16(STA_SLEEP_STATE_AWAKE
),
1200 .mac_id_n_color
= cpu_to_le32(mvmsta
->mac_id_n_color
),
1205 * Same modify mask for sleep_tx_count and sleep_state_flags but this
1206 * should be fine since if we set the STA as "awake", then
1207 * sleep_tx_count is not relevant.
1209 ret
= iwl_mvm_send_cmd_pdu(mvm
, ADD_STA
, CMD_ASYNC
, sizeof(cmd
), &cmd
);
1211 IWL_ERR(mvm
, "Failed to send ADD_STA command (%d)\n", ret
);
1214 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm
*mvm
,
1215 struct ieee80211_sta
*sta
,
1216 enum ieee80211_frame_release_type reason
,
1219 u16 sleep_state_flags
=
1220 (reason
== IEEE80211_FRAME_RELEASE_UAPSD
) ?
1221 STA_SLEEP_STATE_UAPSD
: STA_SLEEP_STATE_PS_POLL
;
1222 struct iwl_mvm_sta
*mvmsta
= (void *)sta
->drv_priv
;
1223 struct iwl_mvm_add_sta_cmd cmd
= {
1224 .add_modify
= STA_MODE_MODIFY
,
1225 .sta_id
= mvmsta
->sta_id
,
1226 .modify_mask
= STA_MODIFY_SLEEPING_STA_TX_COUNT
,
1227 .sleep_tx_count
= cpu_to_le16(cnt
),
1228 .mac_id_n_color
= cpu_to_le32(mvmsta
->mac_id_n_color
),
1230 * Same modify mask for sleep_tx_count and sleep_state_flags so
1231 * we must set the sleep_state_flags too.
1233 .sleep_state_flags
= cpu_to_le16(sleep_state_flags
),
1237 /* TODO: somehow the fw doesn't seem to take PS_POLL into account */
1238 ret
= iwl_mvm_send_cmd_pdu(mvm
, ADD_STA
, CMD_ASYNC
, sizeof(cmd
), &cmd
);
1240 IWL_ERR(mvm
, "Failed to send ADD_STA command (%d)\n", ret
);