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 COPYING.
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
];
775 * If mac80211 is cleaning its state, then say that we finished since
776 * our state has been cleared anyway.
778 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART
, &mvm
->status
)) {
779 ieee80211_stop_tx_ba_cb_irqsafe(vif
, sta
->addr
, tid
);
783 spin_lock_bh(&mvmsta
->lock
);
785 txq_id
= tid_data
->txq_id
;
787 IWL_DEBUG_TX_QUEUES(mvm
, "Stop AGG: sta %d tid %d q %d state %d\n",
788 mvmsta
->sta_id
, tid
, txq_id
, tid_data
->state
);
790 switch (tid_data
->state
) {
792 tid_data
->ssn
= SEQ_TO_SN(tid_data
->seq_number
);
794 IWL_DEBUG_TX_QUEUES(mvm
,
795 "ssn = %d, next_recl = %d\n",
796 tid_data
->ssn
, tid_data
->next_reclaimed
);
798 /* There are still packets for this RA / TID in the HW */
799 if (tid_data
->ssn
!= tid_data
->next_reclaimed
) {
800 tid_data
->state
= IWL_EMPTYING_HW_QUEUE_DELBA
;
805 tid_data
->ssn
= 0xffff;
806 iwl_trans_txq_disable(mvm
->trans
, txq_id
);
808 case IWL_AGG_STARTING
:
809 case IWL_EMPTYING_HW_QUEUE_ADDBA
:
811 * The agg session has been stopped before it was set up. This
812 * can happen when the AddBA timer times out for example.
815 /* No barriers since we are under mutex */
816 lockdep_assert_held(&mvm
->mutex
);
817 mvm
->queue_to_mac80211
[txq_id
] = IWL_INVALID_MAC80211_QUEUE
;
819 ieee80211_stop_tx_ba_cb_irqsafe(vif
, sta
->addr
, tid
);
820 tid_data
->state
= IWL_AGG_OFF
;
825 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
826 mvmsta
->sta_id
, tid
, tid_data
->state
);
828 "\ttid_data->txq_id = %d\n", tid_data
->txq_id
);
832 spin_unlock_bh(&mvmsta
->lock
);
837 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm
*mvm
)
841 lockdep_assert_held(&mvm
->mutex
);
843 i
= find_first_zero_bit(mvm
->fw_key_table
, STA_KEY_MAX_NUM
);
845 if (i
== STA_KEY_MAX_NUM
)
846 return STA_KEY_IDX_INVALID
;
848 __set_bit(i
, mvm
->fw_key_table
);
853 static u8
iwl_mvm_get_key_sta_id(struct ieee80211_vif
*vif
,
854 struct ieee80211_sta
*sta
)
856 struct iwl_mvm_vif
*mvmvif
= (void *)vif
->drv_priv
;
859 struct iwl_mvm_sta
*mvm_sta
= (void *)sta
->drv_priv
;
861 return mvm_sta
->sta_id
;
865 * The device expects GTKs for station interfaces to be
866 * installed as GTKs for the AP station. If we have no
867 * station ID, then use AP's station ID.
869 if (vif
->type
== NL80211_IFTYPE_STATION
&&
870 mvmvif
->ap_sta_id
!= IWL_MVM_STATION_COUNT
)
871 return mvmvif
->ap_sta_id
;
873 return IWL_INVALID_STATION
;
876 static int iwl_mvm_send_sta_key(struct iwl_mvm
*mvm
,
877 struct iwl_mvm_sta
*mvm_sta
,
878 struct ieee80211_key_conf
*keyconf
,
879 u8 sta_id
, u32 tkip_iv32
, u16
*tkip_p1k
,
883 struct iwl_mvm_add_sta_cmd cmd
= {};
888 keyidx
= (keyconf
->keyidx
<< STA_KEY_FLG_KEYID_POS
) &
889 STA_KEY_FLG_KEYID_MSK
;
890 key_flags
= cpu_to_le16(keyidx
);
891 key_flags
|= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP
);
893 switch (keyconf
->cipher
) {
894 case WLAN_CIPHER_SUITE_TKIP
:
895 key_flags
|= cpu_to_le16(STA_KEY_FLG_TKIP
);
896 cmd
.key
.tkip_rx_tsc_byte2
= tkip_iv32
;
897 for (i
= 0; i
< 5; i
++)
898 cmd
.key
.tkip_rx_ttak
[i
] = cpu_to_le16(tkip_p1k
[i
]);
899 memcpy(cmd
.key
.key
, keyconf
->key
, keyconf
->keylen
);
901 case WLAN_CIPHER_SUITE_CCMP
:
902 key_flags
|= cpu_to_le16(STA_KEY_FLG_CCM
);
903 memcpy(cmd
.key
.key
, keyconf
->key
, keyconf
->keylen
);
910 if (!(keyconf
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
))
911 key_flags
|= cpu_to_le16(STA_KEY_MULTICAST
);
913 cmd
.mac_id_n_color
= cpu_to_le32(mvm_sta
->mac_id_n_color
);
914 cmd
.key
.key_offset
= keyconf
->hw_key_idx
;
915 cmd
.key
.key_flags
= key_flags
;
916 cmd
.add_modify
= STA_MODE_MODIFY
;
917 cmd
.modify_mask
= STA_MODIFY_KEY
;
920 status
= ADD_STA_SUCCESS
;
921 if (cmd_flags
== CMD_SYNC
)
922 ret
= iwl_mvm_send_cmd_pdu_status(mvm
, ADD_STA
, sizeof(cmd
),
925 ret
= iwl_mvm_send_cmd_pdu(mvm
, ADD_STA
, CMD_ASYNC
,
929 case ADD_STA_SUCCESS
:
930 IWL_DEBUG_WEP(mvm
, "MODIFY_STA: set dynamic key passed\n");
934 IWL_ERR(mvm
, "MODIFY_STA: set dynamic key failed\n");
941 static int iwl_mvm_send_sta_igtk(struct iwl_mvm
*mvm
,
942 struct ieee80211_key_conf
*keyconf
,
943 u8 sta_id
, bool remove_key
)
945 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd
= {};
947 /* verify the key details match the required command's expectations */
948 if (WARN_ON((keyconf
->cipher
!= WLAN_CIPHER_SUITE_AES_CMAC
) ||
949 (keyconf
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
) ||
950 (keyconf
->keyidx
!= 4 && keyconf
->keyidx
!= 5)))
953 igtk_cmd
.key_id
= cpu_to_le32(keyconf
->keyidx
);
954 igtk_cmd
.sta_id
= cpu_to_le32(sta_id
);
957 igtk_cmd
.ctrl_flags
|= cpu_to_le32(STA_KEY_NOT_VALID
);
959 struct ieee80211_key_seq seq
;
962 memcpy(igtk_cmd
.IGTK
, keyconf
->key
, keyconf
->keylen
);
963 ieee80211_aes_cmac_calculate_k1_k2(keyconf
,
964 igtk_cmd
.K1
, igtk_cmd
.K2
);
965 ieee80211_get_key_rx_seq(keyconf
, 0, &seq
);
966 pn
= seq
.aes_cmac
.pn
;
967 igtk_cmd
.receive_seq_cnt
= cpu_to_le64(((u64
) pn
[5] << 0) |
969 ((u64
) pn
[3] << 16) |
970 ((u64
) pn
[2] << 24) |
971 ((u64
) pn
[1] << 32) |
972 ((u64
) pn
[0] << 40));
975 IWL_DEBUG_INFO(mvm
, "%s igtk for sta %u\n",
976 remove_key
? "removing" : "installing",
979 return iwl_mvm_send_cmd_pdu(mvm
, MGMT_MCAST_KEY
, CMD_SYNC
,
980 sizeof(igtk_cmd
), &igtk_cmd
);
984 static inline u8
*iwl_mvm_get_mac_addr(struct iwl_mvm
*mvm
,
985 struct ieee80211_vif
*vif
,
986 struct ieee80211_sta
*sta
)
988 struct iwl_mvm_vif
*mvmvif
= (void *)vif
->drv_priv
;
993 if (vif
->type
== NL80211_IFTYPE_STATION
&&
994 mvmvif
->ap_sta_id
!= IWL_MVM_STATION_COUNT
) {
995 u8 sta_id
= mvmvif
->ap_sta_id
;
996 sta
= rcu_dereference_protected(mvm
->fw_id_to_mac_id
[sta_id
],
997 lockdep_is_held(&mvm
->mutex
));
1005 int iwl_mvm_set_sta_key(struct iwl_mvm
*mvm
,
1006 struct ieee80211_vif
*vif
,
1007 struct ieee80211_sta
*sta
,
1008 struct ieee80211_key_conf
*keyconf
,
1009 bool have_key_offset
)
1011 struct iwl_mvm_sta
*mvm_sta
;
1014 struct ieee80211_key_seq seq
;
1017 lockdep_assert_held(&mvm
->mutex
);
1019 /* Get the station id from the mvm local station table */
1020 sta_id
= iwl_mvm_get_key_sta_id(vif
, sta
);
1021 if (sta_id
== IWL_INVALID_STATION
) {
1022 IWL_ERR(mvm
, "Failed to find station id\n");
1026 if (keyconf
->cipher
== WLAN_CIPHER_SUITE_AES_CMAC
) {
1027 ret
= iwl_mvm_send_sta_igtk(mvm
, keyconf
, sta_id
, false);
1032 * It is possible that the 'sta' parameter is NULL, and thus
1033 * there is a need to retrieve the sta from the local station table.
1036 sta
= rcu_dereference_protected(mvm
->fw_id_to_mac_id
[sta_id
],
1037 lockdep_is_held(&mvm
->mutex
));
1038 if (IS_ERR_OR_NULL(sta
)) {
1039 IWL_ERR(mvm
, "Invalid station id\n");
1044 mvm_sta
= (struct iwl_mvm_sta
*)sta
->drv_priv
;
1045 if (WARN_ON_ONCE(mvm_sta
->vif
!= vif
))
1048 if (!have_key_offset
) {
1050 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1051 * configure it there. As a result, this workaround exists to
1052 * let the caller set the key offset (hw_key_idx), see d3.c.
1054 keyconf
->hw_key_idx
= iwl_mvm_set_fw_key_idx(mvm
);
1055 if (keyconf
->hw_key_idx
== STA_KEY_IDX_INVALID
)
1059 switch (keyconf
->cipher
) {
1060 case WLAN_CIPHER_SUITE_TKIP
:
1061 addr
= iwl_mvm_get_mac_addr(mvm
, vif
, sta
);
1062 /* get phase 1 key from mac80211 */
1063 ieee80211_get_key_rx_seq(keyconf
, 0, &seq
);
1064 ieee80211_get_tkip_rx_p1k(keyconf
, addr
, seq
.tkip
.iv32
, p1k
);
1065 ret
= iwl_mvm_send_sta_key(mvm
, mvm_sta
, keyconf
, sta_id
,
1066 seq
.tkip
.iv32
, p1k
, CMD_SYNC
);
1068 case WLAN_CIPHER_SUITE_CCMP
:
1069 ret
= iwl_mvm_send_sta_key(mvm
, mvm_sta
, keyconf
, sta_id
,
1073 IWL_ERR(mvm
, "Unknown cipher %x\n", keyconf
->cipher
);
1078 __clear_bit(keyconf
->hw_key_idx
, mvm
->fw_key_table
);
1081 IWL_DEBUG_WEP(mvm
, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1082 keyconf
->cipher
, keyconf
->keylen
, keyconf
->keyidx
,
1087 int iwl_mvm_remove_sta_key(struct iwl_mvm
*mvm
,
1088 struct ieee80211_vif
*vif
,
1089 struct ieee80211_sta
*sta
,
1090 struct ieee80211_key_conf
*keyconf
)
1092 struct iwl_mvm_sta
*mvm_sta
;
1093 struct iwl_mvm_add_sta_cmd cmd
= {};
1098 lockdep_assert_held(&mvm
->mutex
);
1100 /* Get the station id from the mvm local station table */
1101 sta_id
= iwl_mvm_get_key_sta_id(vif
, sta
);
1103 IWL_DEBUG_WEP(mvm
, "mvm remove dynamic key: idx=%d sta=%d\n",
1104 keyconf
->keyidx
, sta_id
);
1106 if (keyconf
->cipher
== WLAN_CIPHER_SUITE_AES_CMAC
)
1107 return iwl_mvm_send_sta_igtk(mvm
, keyconf
, sta_id
, true);
1109 ret
= __test_and_clear_bit(keyconf
->hw_key_idx
, mvm
->fw_key_table
);
1111 IWL_ERR(mvm
, "offset %d not used in fw key table.\n",
1112 keyconf
->hw_key_idx
);
1116 if (sta_id
== IWL_INVALID_STATION
) {
1117 IWL_DEBUG_WEP(mvm
, "station non-existent, early return.\n");
1122 * It is possible that the 'sta' parameter is NULL, and thus
1123 * there is a need to retrieve the sta from the local station table,
1124 * for example when a GTK is removed (where the sta_id will then be
1125 * the AP ID, and no station was passed by mac80211.)
1128 sta
= rcu_dereference_protected(mvm
->fw_id_to_mac_id
[sta_id
],
1129 lockdep_is_held(&mvm
->mutex
));
1131 IWL_ERR(mvm
, "Invalid station id\n");
1136 mvm_sta
= (struct iwl_mvm_sta
*)sta
->drv_priv
;
1137 if (WARN_ON_ONCE(mvm_sta
->vif
!= vif
))
1140 key_flags
= cpu_to_le16((keyconf
->keyidx
<< STA_KEY_FLG_KEYID_POS
) &
1141 STA_KEY_FLG_KEYID_MSK
);
1142 key_flags
|= cpu_to_le16(STA_KEY_FLG_NO_ENC
| STA_KEY_FLG_WEP_KEY_MAP
);
1143 key_flags
|= cpu_to_le16(STA_KEY_NOT_VALID
);
1145 if (!(keyconf
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
))
1146 key_flags
|= cpu_to_le16(STA_KEY_MULTICAST
);
1148 cmd
.mac_id_n_color
= cpu_to_le32(mvm_sta
->mac_id_n_color
);
1149 cmd
.key
.key_flags
= key_flags
;
1150 cmd
.key
.key_offset
= keyconf
->hw_key_idx
;
1151 cmd
.sta_id
= sta_id
;
1153 cmd
.modify_mask
= STA_MODIFY_KEY
;
1154 cmd
.add_modify
= STA_MODE_MODIFY
;
1156 status
= ADD_STA_SUCCESS
;
1157 ret
= iwl_mvm_send_cmd_pdu_status(mvm
, ADD_STA
, sizeof(cmd
),
1161 case ADD_STA_SUCCESS
:
1162 IWL_DEBUG_WEP(mvm
, "MODIFY_STA: remove sta key passed\n");
1166 IWL_ERR(mvm
, "MODIFY_STA: remove sta key failed\n");
1173 void iwl_mvm_update_tkip_key(struct iwl_mvm
*mvm
,
1174 struct ieee80211_vif
*vif
,
1175 struct ieee80211_key_conf
*keyconf
,
1176 struct ieee80211_sta
*sta
, u32 iv32
,
1179 struct iwl_mvm_sta
*mvm_sta
;
1180 u8 sta_id
= iwl_mvm_get_key_sta_id(vif
, sta
);
1182 if (WARN_ON_ONCE(sta_id
== IWL_INVALID_STATION
))
1188 sta
= rcu_dereference(mvm
->fw_id_to_mac_id
[sta_id
]);
1189 if (WARN_ON(IS_ERR_OR_NULL(sta
))) {
1195 mvm_sta
= (void *)sta
->drv_priv
;
1196 iwl_mvm_send_sta_key(mvm
, mvm_sta
, keyconf
, sta_id
,
1197 iv32
, phase1key
, CMD_ASYNC
);
1201 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm
*mvm
,
1202 struct ieee80211_sta
*sta
)
1204 struct iwl_mvm_sta
*mvmsta
= (void *)sta
->drv_priv
;
1205 struct iwl_mvm_add_sta_cmd cmd
= {
1206 .add_modify
= STA_MODE_MODIFY
,
1207 .sta_id
= mvmsta
->sta_id
,
1208 .modify_mask
= STA_MODIFY_SLEEPING_STA_TX_COUNT
,
1209 .sleep_state_flags
= cpu_to_le16(STA_SLEEP_STATE_AWAKE
),
1210 .mac_id_n_color
= cpu_to_le32(mvmsta
->mac_id_n_color
),
1215 * Same modify mask for sleep_tx_count and sleep_state_flags but this
1216 * should be fine since if we set the STA as "awake", then
1217 * sleep_tx_count is not relevant.
1219 ret
= iwl_mvm_send_cmd_pdu(mvm
, ADD_STA
, CMD_ASYNC
, sizeof(cmd
), &cmd
);
1221 IWL_ERR(mvm
, "Failed to send ADD_STA command (%d)\n", ret
);
1224 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm
*mvm
,
1225 struct ieee80211_sta
*sta
,
1226 enum ieee80211_frame_release_type reason
,
1229 u16 sleep_state_flags
=
1230 (reason
== IEEE80211_FRAME_RELEASE_UAPSD
) ?
1231 STA_SLEEP_STATE_UAPSD
: STA_SLEEP_STATE_PS_POLL
;
1232 struct iwl_mvm_sta
*mvmsta
= (void *)sta
->drv_priv
;
1233 struct iwl_mvm_add_sta_cmd cmd
= {
1234 .add_modify
= STA_MODE_MODIFY
,
1235 .sta_id
= mvmsta
->sta_id
,
1236 .modify_mask
= STA_MODIFY_SLEEPING_STA_TX_COUNT
,
1237 .sleep_tx_count
= cpu_to_le16(cnt
),
1238 .mac_id_n_color
= cpu_to_le32(mvmsta
->mac_id_n_color
),
1240 * Same modify mask for sleep_tx_count and sleep_state_flags so
1241 * we must set the sleep_state_flags too.
1243 .sleep_state_flags
= cpu_to_le16(sleep_state_flags
),
1247 /* TODO: somehow the fw doesn't seem to take PS_POLL into account */
1248 ret
= iwl_mvm_send_cmd_pdu(mvm
, ADD_STA
, CMD_ASYNC
, sizeof(cmd
), &cmd
);
1250 IWL_ERR(mvm
, "Failed to send ADD_STA command (%d)\n", ret
);