• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
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.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 2015, 2018 - 2020 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * The full GNU General Public License is included in this distribution
22  * in the file called COPYING.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <linuxwifi@intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  * BSD LICENSE
29  *
30  * Copyright(c) 2012 - 2015, 2018 - 2020 Intel Corporation. All rights reserved.
31  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
32  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  *
39  *  * Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  *  * Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in
43  *    the documentation and/or other materials provided with the
44  *    distribution.
45  *  * Neither the name Intel Corporation nor the names of its
46  *    contributors may be used to endorse or promote products derived
47  *    from this software without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
52  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
53  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60  *
61  *****************************************************************************/
62 #include <net/mac80211.h>
63 
64 #include "mvm.h"
65 #include "sta.h"
66 #include "rs.h"
67 
68 /*
69  * New version of ADD_STA_sta command added new fields at the end of the
70  * structure, so sending the size of the relevant API's structure is enough to
71  * support both API versions.
72  */
iwl_mvm_add_sta_cmd_size(struct iwl_mvm * mvm)73 static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
74 {
75 	if (iwl_mvm_has_new_rx_api(mvm) ||
76 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
77 		return sizeof(struct iwl_mvm_add_sta_cmd);
78 	else
79 		return sizeof(struct iwl_mvm_add_sta_cmd_v7);
80 }
81 
iwl_mvm_find_free_sta_id(struct iwl_mvm * mvm,enum nl80211_iftype iftype)82 static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
83 				    enum nl80211_iftype iftype)
84 {
85 	int sta_id;
86 	u32 reserved_ids = 0;
87 
88 	BUILD_BUG_ON(IWL_MVM_STATION_COUNT_MAX > 32);
89 	WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
90 
91 	lockdep_assert_held(&mvm->mutex);
92 
93 	/* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
94 	if (iftype != NL80211_IFTYPE_STATION)
95 		reserved_ids = BIT(0);
96 
97 	/* Don't take rcu_read_lock() since we are protected by mvm->mutex */
98 	for (sta_id = 0; sta_id < mvm->fw->ucode_capa.num_stations; sta_id++) {
99 		if (BIT(sta_id) & reserved_ids)
100 			continue;
101 
102 		if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
103 					       lockdep_is_held(&mvm->mutex)))
104 			return sta_id;
105 	}
106 	return IWL_MVM_INVALID_STA;
107 }
108 
109 /* send station add/update command to firmware */
iwl_mvm_sta_send_to_fw(struct iwl_mvm * mvm,struct ieee80211_sta * sta,bool update,unsigned int flags)110 int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
111 			   bool update, unsigned int flags)
112 {
113 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
114 	struct iwl_mvm_add_sta_cmd add_sta_cmd = {
115 		.sta_id = mvm_sta->sta_id,
116 		.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
117 		.add_modify = update ? 1 : 0,
118 		.station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
119 						 STA_FLG_MIMO_EN_MSK |
120 						 STA_FLG_RTS_MIMO_PROT),
121 		.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
122 	};
123 	int ret;
124 	u32 status;
125 	u32 agg_size = 0, mpdu_dens = 0;
126 
127 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
128 		add_sta_cmd.station_type = mvm_sta->sta_type;
129 
130 	if (!update || (flags & STA_MODIFY_QUEUES)) {
131 		memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
132 
133 		if (!iwl_mvm_has_new_tx_api(mvm)) {
134 			add_sta_cmd.tfd_queue_msk =
135 				cpu_to_le32(mvm_sta->tfd_queue_msk);
136 
137 			if (flags & STA_MODIFY_QUEUES)
138 				add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
139 		} else {
140 			WARN_ON(flags & STA_MODIFY_QUEUES);
141 		}
142 	}
143 
144 	switch (sta->bandwidth) {
145 	case IEEE80211_STA_RX_BW_160:
146 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
147 		/* fall through */
148 	case IEEE80211_STA_RX_BW_80:
149 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
150 		/* fall through */
151 	case IEEE80211_STA_RX_BW_40:
152 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
153 		/* fall through */
154 	case IEEE80211_STA_RX_BW_20:
155 		if (sta->ht_cap.ht_supported)
156 			add_sta_cmd.station_flags |=
157 				cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
158 		break;
159 	}
160 
161 	switch (sta->rx_nss) {
162 	case 1:
163 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
164 		break;
165 	case 2:
166 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
167 		break;
168 	case 3 ... 8:
169 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
170 		break;
171 	}
172 
173 	switch (sta->smps_mode) {
174 	case IEEE80211_SMPS_AUTOMATIC:
175 	case IEEE80211_SMPS_NUM_MODES:
176 		WARN_ON(1);
177 		break;
178 	case IEEE80211_SMPS_STATIC:
179 		/* override NSS */
180 		add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
181 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
182 		break;
183 	case IEEE80211_SMPS_DYNAMIC:
184 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
185 		break;
186 	case IEEE80211_SMPS_OFF:
187 		/* nothing */
188 		break;
189 	}
190 
191 	if (sta->ht_cap.ht_supported) {
192 		add_sta_cmd.station_flags_msk |=
193 			cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
194 				    STA_FLG_AGG_MPDU_DENS_MSK);
195 
196 		mpdu_dens = sta->ht_cap.ampdu_density;
197 	}
198 
199 
200 	if (sta->vht_cap.vht_supported) {
201 		agg_size = sta->vht_cap.cap &
202 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
203 		agg_size >>=
204 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
205 	} else if (sta->ht_cap.ht_supported) {
206 		agg_size = sta->ht_cap.ampdu_factor;
207 	}
208 
209 	/* D6.0 10.12.2 A-MPDU length limit rules
210 	 * A STA indicates the maximum length of the A-MPDU preEOF padding
211 	 * that it can receive in an HE PPDU in the Maximum A-MPDU Length
212 	 * Exponent field in its HT Capabilities, VHT Capabilities,
213 	 * and HE 6 GHz Band Capabilities elements (if present) and the
214 	 * Maximum AMPDU Length Exponent Extension field in its HE
215 	 * Capabilities element
216 	 */
217 	if (sta->he_cap.has_he)
218 		agg_size += u8_get_bits(sta->he_cap.he_cap_elem.mac_cap_info[3],
219 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
220 
221 	/* Limit to max A-MPDU supported by FW */
222 	if (agg_size > (STA_FLG_MAX_AGG_SIZE_4M >> STA_FLG_MAX_AGG_SIZE_SHIFT))
223 		agg_size = (STA_FLG_MAX_AGG_SIZE_4M >>
224 			    STA_FLG_MAX_AGG_SIZE_SHIFT);
225 
226 	add_sta_cmd.station_flags |=
227 		cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
228 	add_sta_cmd.station_flags |=
229 		cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
230 	if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)
231 		add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
232 
233 	if (sta->wme) {
234 		add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
235 
236 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
237 			add_sta_cmd.uapsd_acs |= BIT(AC_BK);
238 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
239 			add_sta_cmd.uapsd_acs |= BIT(AC_BE);
240 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
241 			add_sta_cmd.uapsd_acs |= BIT(AC_VI);
242 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
243 			add_sta_cmd.uapsd_acs |= BIT(AC_VO);
244 		add_sta_cmd.uapsd_acs |= add_sta_cmd.uapsd_acs << 4;
245 		add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
246 	}
247 
248 	status = ADD_STA_SUCCESS;
249 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
250 					  iwl_mvm_add_sta_cmd_size(mvm),
251 					  &add_sta_cmd, &status);
252 	if (ret)
253 		return ret;
254 
255 	switch (status & IWL_ADD_STA_STATUS_MASK) {
256 	case ADD_STA_SUCCESS:
257 		IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
258 		break;
259 	default:
260 		ret = -EIO;
261 		IWL_ERR(mvm, "ADD_STA failed\n");
262 		break;
263 	}
264 
265 	return ret;
266 }
267 
iwl_mvm_rx_agg_session_expired(struct timer_list * t)268 static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
269 {
270 	struct iwl_mvm_baid_data *data =
271 		from_timer(data, t, session_timer);
272 	struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr;
273 	struct iwl_mvm_baid_data *ba_data;
274 	struct ieee80211_sta *sta;
275 	struct iwl_mvm_sta *mvm_sta;
276 	unsigned long timeout;
277 
278 	rcu_read_lock();
279 
280 	ba_data = rcu_dereference(*rcu_ptr);
281 
282 	if (WARN_ON(!ba_data))
283 		goto unlock;
284 
285 	if (!ba_data->timeout)
286 		goto unlock;
287 
288 	timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
289 	if (time_is_after_jiffies(timeout)) {
290 		mod_timer(&ba_data->session_timer, timeout);
291 		goto unlock;
292 	}
293 
294 	/* Timer expired */
295 	sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
296 
297 	/*
298 	 * sta should be valid unless the following happens:
299 	 * The firmware asserts which triggers a reconfig flow, but
300 	 * the reconfig fails before we set the pointer to sta into
301 	 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
302 	 * A-MDPU and hence the timer continues to run. Then, the
303 	 * timer expires and sta is NULL.
304 	 */
305 	if (!sta)
306 		goto unlock;
307 
308 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
309 	ieee80211_rx_ba_timer_expired(mvm_sta->vif,
310 				      sta->addr, ba_data->tid);
311 unlock:
312 	rcu_read_unlock();
313 }
314 
315 /* Disable aggregations for a bitmap of TIDs for a given station */
iwl_mvm_invalidate_sta_queue(struct iwl_mvm * mvm,int queue,unsigned long disable_agg_tids,bool remove_queue)316 static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
317 					unsigned long disable_agg_tids,
318 					bool remove_queue)
319 {
320 	struct iwl_mvm_add_sta_cmd cmd = {};
321 	struct ieee80211_sta *sta;
322 	struct iwl_mvm_sta *mvmsta;
323 	u32 status;
324 	u8 sta_id;
325 
326 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
327 		return -EINVAL;
328 
329 	sta_id = mvm->queue_info[queue].ra_sta_id;
330 
331 	rcu_read_lock();
332 
333 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
334 
335 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
336 		rcu_read_unlock();
337 		return -EINVAL;
338 	}
339 
340 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
341 
342 	mvmsta->tid_disable_agg |= disable_agg_tids;
343 
344 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
345 	cmd.sta_id = mvmsta->sta_id;
346 	cmd.add_modify = STA_MODE_MODIFY;
347 	cmd.modify_mask = STA_MODIFY_QUEUES;
348 	if (disable_agg_tids)
349 		cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
350 	if (remove_queue)
351 		cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
352 	cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
353 	cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
354 
355 	rcu_read_unlock();
356 
357 	/* Notify FW of queue removal from the STA queues */
358 	status = ADD_STA_SUCCESS;
359 	return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
360 					   iwl_mvm_add_sta_cmd_size(mvm),
361 					   &cmd, &status);
362 }
363 
iwl_mvm_disable_txq(struct iwl_mvm * mvm,struct ieee80211_sta * sta,u16 * queueptr,u8 tid,u8 flags)364 static int iwl_mvm_disable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
365 			       u16 *queueptr, u8 tid, u8 flags)
366 {
367 	int queue = *queueptr;
368 	struct iwl_scd_txq_cfg_cmd cmd = {
369 		.scd_queue = queue,
370 		.action = SCD_CFG_DISABLE_QUEUE,
371 	};
372 	int ret;
373 
374 	if (iwl_mvm_has_new_tx_api(mvm)) {
375 		iwl_trans_txq_free(mvm->trans, queue);
376 		*queueptr = IWL_MVM_INVALID_QUEUE;
377 
378 		return 0;
379 	}
380 
381 	if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0))
382 		return 0;
383 
384 	mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
385 
386 	cmd.action = mvm->queue_info[queue].tid_bitmap ?
387 		SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
388 	if (cmd.action == SCD_CFG_DISABLE_QUEUE)
389 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
390 
391 	IWL_DEBUG_TX_QUEUES(mvm,
392 			    "Disabling TXQ #%d tids=0x%x\n",
393 			    queue,
394 			    mvm->queue_info[queue].tid_bitmap);
395 
396 	/* If the queue is still enabled - nothing left to do in this func */
397 	if (cmd.action == SCD_CFG_ENABLE_QUEUE)
398 		return 0;
399 
400 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
401 	cmd.tid = mvm->queue_info[queue].txq_tid;
402 
403 	/* Make sure queue info is correct even though we overwrite it */
404 	WARN(mvm->queue_info[queue].tid_bitmap,
405 	     "TXQ #%d info out-of-sync - tids=0x%x\n",
406 	     queue, mvm->queue_info[queue].tid_bitmap);
407 
408 	/* If we are here - the queue is freed and we can zero out these vals */
409 	mvm->queue_info[queue].tid_bitmap = 0;
410 
411 	if (sta) {
412 		struct iwl_mvm_txq *mvmtxq =
413 			iwl_mvm_txq_from_tid(sta, tid);
414 
415 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
416 	}
417 
418 	/* Regardless if this is a reserved TXQ for a STA - mark it as false */
419 	mvm->queue_info[queue].reserved = false;
420 
421 	iwl_trans_txq_disable(mvm->trans, queue, false);
422 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, flags,
423 				   sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
424 
425 	if (ret)
426 		IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
427 			queue, ret);
428 	return ret;
429 }
430 
iwl_mvm_get_queue_agg_tids(struct iwl_mvm * mvm,int queue)431 static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
432 {
433 	struct ieee80211_sta *sta;
434 	struct iwl_mvm_sta *mvmsta;
435 	unsigned long tid_bitmap;
436 	unsigned long agg_tids = 0;
437 	u8 sta_id;
438 	int tid;
439 
440 	lockdep_assert_held(&mvm->mutex);
441 
442 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
443 		return -EINVAL;
444 
445 	sta_id = mvm->queue_info[queue].ra_sta_id;
446 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
447 
448 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
449 					lockdep_is_held(&mvm->mutex));
450 
451 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
452 		return -EINVAL;
453 
454 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
455 
456 	spin_lock_bh(&mvmsta->lock);
457 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
458 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
459 			agg_tids |= BIT(tid);
460 	}
461 	spin_unlock_bh(&mvmsta->lock);
462 
463 	return agg_tids;
464 }
465 
466 /*
467  * Remove a queue from a station's resources.
468  * Note that this only marks as free. It DOESN'T delete a BA agreement, and
469  * doesn't disable the queue
470  */
iwl_mvm_remove_sta_queue_marking(struct iwl_mvm * mvm,int queue)471 static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
472 {
473 	struct ieee80211_sta *sta;
474 	struct iwl_mvm_sta *mvmsta;
475 	unsigned long tid_bitmap;
476 	unsigned long disable_agg_tids = 0;
477 	u8 sta_id;
478 	int tid;
479 
480 	lockdep_assert_held(&mvm->mutex);
481 
482 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
483 		return -EINVAL;
484 
485 	sta_id = mvm->queue_info[queue].ra_sta_id;
486 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
487 
488 	rcu_read_lock();
489 
490 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
491 
492 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
493 		rcu_read_unlock();
494 		return 0;
495 	}
496 
497 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
498 
499 	spin_lock_bh(&mvmsta->lock);
500 	/* Unmap MAC queues and TIDs from this queue */
501 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
502 		struct iwl_mvm_txq *mvmtxq =
503 			iwl_mvm_txq_from_tid(sta, tid);
504 
505 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
506 			disable_agg_tids |= BIT(tid);
507 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
508 
509 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
510 	}
511 
512 	mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
513 	spin_unlock_bh(&mvmsta->lock);
514 
515 	rcu_read_unlock();
516 
517 	/*
518 	 * The TX path may have been using this TXQ_ID from the tid_data,
519 	 * so make sure it's no longer running so that we can safely reuse
520 	 * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE
521 	 * above, but nothing guarantees we've stopped using them. Thus,
522 	 * without this, we could get to iwl_mvm_disable_txq() and remove
523 	 * the queue while still sending frames to it.
524 	 */
525 	synchronize_net();
526 
527 	return disable_agg_tids;
528 }
529 
iwl_mvm_free_inactive_queue(struct iwl_mvm * mvm,int queue,struct ieee80211_sta * old_sta,u8 new_sta_id)530 static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
531 				       struct ieee80211_sta *old_sta,
532 				       u8 new_sta_id)
533 {
534 	struct iwl_mvm_sta *mvmsta;
535 	u8 sta_id, tid;
536 	unsigned long disable_agg_tids = 0;
537 	bool same_sta;
538 	u16 queue_tmp = queue;
539 	int ret;
540 
541 	lockdep_assert_held(&mvm->mutex);
542 
543 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
544 		return -EINVAL;
545 
546 	sta_id = mvm->queue_info[queue].ra_sta_id;
547 	tid = mvm->queue_info[queue].txq_tid;
548 
549 	same_sta = sta_id == new_sta_id;
550 
551 	mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
552 	if (WARN_ON(!mvmsta))
553 		return -EINVAL;
554 
555 	disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
556 	/* Disable the queue */
557 	if (disable_agg_tids)
558 		iwl_mvm_invalidate_sta_queue(mvm, queue,
559 					     disable_agg_tids, false);
560 
561 	ret = iwl_mvm_disable_txq(mvm, old_sta, &queue_tmp, tid, 0);
562 	if (ret) {
563 		IWL_ERR(mvm,
564 			"Failed to free inactive queue %d (ret=%d)\n",
565 			queue, ret);
566 
567 		return ret;
568 	}
569 
570 	/* If TXQ is allocated to another STA, update removal in FW */
571 	if (!same_sta)
572 		iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
573 
574 	return 0;
575 }
576 
iwl_mvm_get_shared_queue(struct iwl_mvm * mvm,unsigned long tfd_queue_mask,u8 ac)577 static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
578 				    unsigned long tfd_queue_mask, u8 ac)
579 {
580 	int queue = 0;
581 	u8 ac_to_queue[IEEE80211_NUM_ACS];
582 	int i;
583 
584 	/*
585 	 * This protects us against grabbing a queue that's being reconfigured
586 	 * by the inactivity checker.
587 	 */
588 	lockdep_assert_held(&mvm->mutex);
589 
590 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
591 		return -EINVAL;
592 
593 	memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
594 
595 	/* See what ACs the existing queues for this STA have */
596 	for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
597 		/* Only DATA queues can be shared */
598 		if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
599 		    i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
600 			continue;
601 
602 		ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
603 	}
604 
605 	/*
606 	 * The queue to share is chosen only from DATA queues as follows (in
607 	 * descending priority):
608 	 * 1. An AC_BE queue
609 	 * 2. Same AC queue
610 	 * 3. Highest AC queue that is lower than new AC
611 	 * 4. Any existing AC (there always is at least 1 DATA queue)
612 	 */
613 
614 	/* Priority 1: An AC_BE queue */
615 	if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
616 		queue = ac_to_queue[IEEE80211_AC_BE];
617 	/* Priority 2: Same AC queue */
618 	else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
619 		queue = ac_to_queue[ac];
620 	/* Priority 3a: If new AC is VO and VI exists - use VI */
621 	else if (ac == IEEE80211_AC_VO &&
622 		 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
623 		queue = ac_to_queue[IEEE80211_AC_VI];
624 	/* Priority 3b: No BE so only AC less than the new one is BK */
625 	else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
626 		queue = ac_to_queue[IEEE80211_AC_BK];
627 	/* Priority 4a: No BE nor BK - use VI if exists */
628 	else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
629 		queue = ac_to_queue[IEEE80211_AC_VI];
630 	/* Priority 4b: No BE, BK nor VI - use VO if exists */
631 	else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
632 		queue = ac_to_queue[IEEE80211_AC_VO];
633 
634 	/* Make sure queue found (or not) is legal */
635 	if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
636 	    !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
637 	    (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
638 		IWL_ERR(mvm, "No DATA queues available to share\n");
639 		return -ENOSPC;
640 	}
641 
642 	return queue;
643 }
644 
645 /*
646  * If a given queue has a higher AC than the TID stream that is being compared
647  * to, the queue needs to be redirected to the lower AC. This function does that
648  * in such a case, otherwise - if no redirection required - it does nothing,
649  * unless the %force param is true.
650  */
iwl_mvm_redirect_queue(struct iwl_mvm * mvm,int queue,int tid,int ac,int ssn,unsigned int wdg_timeout,bool force,struct iwl_mvm_txq * txq)651 static int iwl_mvm_redirect_queue(struct iwl_mvm *mvm, int queue, int tid,
652 				  int ac, int ssn, unsigned int wdg_timeout,
653 				  bool force, struct iwl_mvm_txq *txq)
654 {
655 	struct iwl_scd_txq_cfg_cmd cmd = {
656 		.scd_queue = queue,
657 		.action = SCD_CFG_DISABLE_QUEUE,
658 	};
659 	bool shared_queue;
660 	int ret;
661 
662 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
663 		return -EINVAL;
664 
665 	/*
666 	 * If the AC is lower than current one - FIFO needs to be redirected to
667 	 * the lowest one of the streams in the queue. Check if this is needed
668 	 * here.
669 	 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
670 	 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
671 	 * we need to check if the numerical value of X is LARGER than of Y.
672 	 */
673 	if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
674 		IWL_DEBUG_TX_QUEUES(mvm,
675 				    "No redirection needed on TXQ #%d\n",
676 				    queue);
677 		return 0;
678 	}
679 
680 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
681 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
682 	cmd.tid = mvm->queue_info[queue].txq_tid;
683 	shared_queue = hweight16(mvm->queue_info[queue].tid_bitmap) > 1;
684 
685 	IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
686 			    queue, iwl_mvm_ac_to_tx_fifo[ac]);
687 
688 	/* Stop the queue and wait for it to empty */
689 	txq->stopped = true;
690 
691 	ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
692 	if (ret) {
693 		IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
694 			queue);
695 		ret = -EIO;
696 		goto out;
697 	}
698 
699 	/* Before redirecting the queue we need to de-activate it */
700 	iwl_trans_txq_disable(mvm->trans, queue, false);
701 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
702 	if (ret)
703 		IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
704 			ret);
705 
706 	/* Make sure the SCD wrptr is correctly set before reconfiguring */
707 	iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
708 
709 	/* Update the TID "owner" of the queue */
710 	mvm->queue_info[queue].txq_tid = tid;
711 
712 	/* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
713 
714 	/* Redirect to lower AC */
715 	iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
716 			     cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
717 
718 	/* Update AC marking of the queue */
719 	mvm->queue_info[queue].mac80211_ac = ac;
720 
721 	/*
722 	 * Mark queue as shared in transport if shared
723 	 * Note this has to be done after queue enablement because enablement
724 	 * can also set this value, and there is no indication there to shared
725 	 * queues
726 	 */
727 	if (shared_queue)
728 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
729 
730 out:
731 	/* Continue using the queue */
732 	txq->stopped = false;
733 
734 	return ret;
735 }
736 
iwl_mvm_find_free_queue(struct iwl_mvm * mvm,u8 sta_id,u8 minq,u8 maxq)737 static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id,
738 				   u8 minq, u8 maxq)
739 {
740 	int i;
741 
742 	lockdep_assert_held(&mvm->mutex);
743 
744 	if (WARN(maxq >= mvm->trans->trans_cfg->base_params->num_of_queues,
745 		 "max queue %d >= num_of_queues (%d)", maxq,
746 		 mvm->trans->trans_cfg->base_params->num_of_queues))
747 		maxq = mvm->trans->trans_cfg->base_params->num_of_queues - 1;
748 
749 	/* This should not be hit with new TX path */
750 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
751 		return -ENOSPC;
752 
753 	/* Start by looking for a free queue */
754 	for (i = minq; i <= maxq; i++)
755 		if (mvm->queue_info[i].tid_bitmap == 0 &&
756 		    mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
757 			return i;
758 
759 	return -ENOSPC;
760 }
761 
iwl_mvm_tvqm_enable_txq(struct iwl_mvm * mvm,u8 sta_id,u8 tid,unsigned int timeout)762 static int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
763 				   u8 sta_id, u8 tid, unsigned int timeout)
764 {
765 	int queue, size = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
766 				mvm->trans->cfg->min_256_ba_txq_size);
767 
768 	if (tid == IWL_MAX_TID_COUNT) {
769 		tid = IWL_MGMT_TID;
770 		size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
771 			     mvm->trans->cfg->min_txq_size);
772 	}
773 
774 	do {
775 		__le16 enable = cpu_to_le16(TX_QUEUE_CFG_ENABLE_QUEUE);
776 
777 		queue = iwl_trans_txq_alloc(mvm->trans, enable,
778 					    sta_id, tid, SCD_QUEUE_CFG,
779 					    size, timeout);
780 
781 		if (queue < 0)
782 			IWL_DEBUG_TX_QUEUES(mvm,
783 					    "Failed allocating TXQ of size %d for sta %d tid %d, ret: %d\n",
784 					    size, sta_id, tid, queue);
785 		size /= 2;
786 	} while (queue < 0 && size >= 16);
787 
788 	if (queue < 0)
789 		return queue;
790 
791 	IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
792 			    queue, sta_id, tid);
793 
794 	return queue;
795 }
796 
iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm * mvm,struct ieee80211_sta * sta,u8 ac,int tid)797 static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
798 					struct ieee80211_sta *sta, u8 ac,
799 					int tid)
800 {
801 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
802 	struct iwl_mvm_txq *mvmtxq =
803 		iwl_mvm_txq_from_tid(sta, tid);
804 	unsigned int wdg_timeout =
805 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
806 	int queue = -1;
807 
808 	lockdep_assert_held(&mvm->mutex);
809 
810 	IWL_DEBUG_TX_QUEUES(mvm,
811 			    "Allocating queue for sta %d on tid %d\n",
812 			    mvmsta->sta_id, tid);
813 	queue = iwl_mvm_tvqm_enable_txq(mvm, mvmsta->sta_id, tid, wdg_timeout);
814 	if (queue < 0)
815 		return queue;
816 
817 	mvmtxq->txq_id = queue;
818 	mvm->tvqm_info[queue].txq_tid = tid;
819 	mvm->tvqm_info[queue].sta_id = mvmsta->sta_id;
820 
821 	IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
822 
823 	spin_lock_bh(&mvmsta->lock);
824 	mvmsta->tid_data[tid].txq_id = queue;
825 	spin_unlock_bh(&mvmsta->lock);
826 
827 	return 0;
828 }
829 
iwl_mvm_update_txq_mapping(struct iwl_mvm * mvm,struct ieee80211_sta * sta,int queue,u8 sta_id,u8 tid)830 static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
831 				       struct ieee80211_sta *sta,
832 				       int queue, u8 sta_id, u8 tid)
833 {
834 	bool enable_queue = true;
835 
836 	/* Make sure this TID isn't already enabled */
837 	if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
838 		IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
839 			queue, tid);
840 		return false;
841 	}
842 
843 	/* Update mappings and refcounts */
844 	if (mvm->queue_info[queue].tid_bitmap)
845 		enable_queue = false;
846 
847 	mvm->queue_info[queue].tid_bitmap |= BIT(tid);
848 	mvm->queue_info[queue].ra_sta_id = sta_id;
849 
850 	if (enable_queue) {
851 		if (tid != IWL_MAX_TID_COUNT)
852 			mvm->queue_info[queue].mac80211_ac =
853 				tid_to_mac80211_ac[tid];
854 		else
855 			mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
856 
857 		mvm->queue_info[queue].txq_tid = tid;
858 	}
859 
860 	if (sta) {
861 		struct iwl_mvm_txq *mvmtxq =
862 			iwl_mvm_txq_from_tid(sta, tid);
863 
864 		mvmtxq->txq_id = queue;
865 	}
866 
867 	IWL_DEBUG_TX_QUEUES(mvm,
868 			    "Enabling TXQ #%d tids=0x%x\n",
869 			    queue, mvm->queue_info[queue].tid_bitmap);
870 
871 	return enable_queue;
872 }
873 
iwl_mvm_enable_txq(struct iwl_mvm * mvm,struct ieee80211_sta * sta,int queue,u16 ssn,const struct iwl_trans_txq_scd_cfg * cfg,unsigned int wdg_timeout)874 static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
875 			       int queue, u16 ssn,
876 			       const struct iwl_trans_txq_scd_cfg *cfg,
877 			       unsigned int wdg_timeout)
878 {
879 	struct iwl_scd_txq_cfg_cmd cmd = {
880 		.scd_queue = queue,
881 		.action = SCD_CFG_ENABLE_QUEUE,
882 		.window = cfg->frame_limit,
883 		.sta_id = cfg->sta_id,
884 		.ssn = cpu_to_le16(ssn),
885 		.tx_fifo = cfg->fifo,
886 		.aggregate = cfg->aggregate,
887 		.tid = cfg->tid,
888 	};
889 	bool inc_ssn;
890 
891 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
892 		return false;
893 
894 	/* Send the enabling command if we need to */
895 	if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
896 		return false;
897 
898 	inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
899 					   NULL, wdg_timeout);
900 	if (inc_ssn)
901 		le16_add_cpu(&cmd.ssn, 1);
902 
903 	WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
904 	     "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
905 
906 	return inc_ssn;
907 }
908 
iwl_mvm_change_queue_tid(struct iwl_mvm * mvm,int queue)909 static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
910 {
911 	struct iwl_scd_txq_cfg_cmd cmd = {
912 		.scd_queue = queue,
913 		.action = SCD_CFG_UPDATE_QUEUE_TID,
914 	};
915 	int tid;
916 	unsigned long tid_bitmap;
917 	int ret;
918 
919 	lockdep_assert_held(&mvm->mutex);
920 
921 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
922 		return;
923 
924 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
925 
926 	if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
927 		return;
928 
929 	/* Find any TID for queue */
930 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
931 	cmd.tid = tid;
932 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
933 
934 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
935 	if (ret) {
936 		IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
937 			queue, ret);
938 		return;
939 	}
940 
941 	mvm->queue_info[queue].txq_tid = tid;
942 	IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
943 			    queue, tid);
944 }
945 
iwl_mvm_unshare_queue(struct iwl_mvm * mvm,int queue)946 static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
947 {
948 	struct ieee80211_sta *sta;
949 	struct iwl_mvm_sta *mvmsta;
950 	u8 sta_id;
951 	int tid = -1;
952 	unsigned long tid_bitmap;
953 	unsigned int wdg_timeout;
954 	int ssn;
955 	int ret = true;
956 
957 	/* queue sharing is disabled on new TX path */
958 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
959 		return;
960 
961 	lockdep_assert_held(&mvm->mutex);
962 
963 	sta_id = mvm->queue_info[queue].ra_sta_id;
964 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
965 
966 	/* Find TID for queue, and make sure it is the only one on the queue */
967 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
968 	if (tid_bitmap != BIT(tid)) {
969 		IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
970 			queue, tid_bitmap);
971 		return;
972 	}
973 
974 	IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
975 			    tid);
976 
977 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
978 					lockdep_is_held(&mvm->mutex));
979 
980 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
981 		return;
982 
983 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
984 	wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
985 
986 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
987 
988 	ret = iwl_mvm_redirect_queue(mvm, queue, tid,
989 				     tid_to_mac80211_ac[tid], ssn,
990 				     wdg_timeout, true,
991 				     iwl_mvm_txq_from_tid(sta, tid));
992 	if (ret) {
993 		IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
994 		return;
995 	}
996 
997 	/* If aggs should be turned back on - do it */
998 	if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
999 		struct iwl_mvm_add_sta_cmd cmd = {0};
1000 
1001 		mvmsta->tid_disable_agg &= ~BIT(tid);
1002 
1003 		cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1004 		cmd.sta_id = mvmsta->sta_id;
1005 		cmd.add_modify = STA_MODE_MODIFY;
1006 		cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1007 		cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
1008 		cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
1009 
1010 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1011 					   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
1012 		if (!ret) {
1013 			IWL_DEBUG_TX_QUEUES(mvm,
1014 					    "TXQ #%d is now aggregated again\n",
1015 					    queue);
1016 
1017 			/* Mark queue intenally as aggregating again */
1018 			iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
1019 		}
1020 	}
1021 
1022 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1023 }
1024 
1025 /*
1026  * Remove inactive TIDs of a given queue.
1027  * If all queue TIDs are inactive - mark the queue as inactive
1028  * If only some the queue TIDs are inactive - unmap them from the queue
1029  *
1030  * Returns %true if all TIDs were removed and the queue could be reused.
1031  */
iwl_mvm_remove_inactive_tids(struct iwl_mvm * mvm,struct iwl_mvm_sta * mvmsta,int queue,unsigned long tid_bitmap,unsigned long * unshare_queues,unsigned long * changetid_queues)1032 static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
1033 					 struct iwl_mvm_sta *mvmsta, int queue,
1034 					 unsigned long tid_bitmap,
1035 					 unsigned long *unshare_queues,
1036 					 unsigned long *changetid_queues)
1037 {
1038 	int tid;
1039 
1040 	lockdep_assert_held(&mvmsta->lock);
1041 	lockdep_assert_held(&mvm->mutex);
1042 
1043 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1044 		return false;
1045 
1046 	/* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
1047 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1048 		/* If some TFDs are still queued - don't mark TID as inactive */
1049 		if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
1050 			tid_bitmap &= ~BIT(tid);
1051 
1052 		/* Don't mark as inactive any TID that has an active BA */
1053 		if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
1054 			tid_bitmap &= ~BIT(tid);
1055 	}
1056 
1057 	/* If all TIDs in the queue are inactive - return it can be reused */
1058 	if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1059 		IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1060 		return true;
1061 	}
1062 
1063 	/*
1064 	 * If we are here, this is a shared queue and not all TIDs timed-out.
1065 	 * Remove the ones that did.
1066 	 */
1067 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1068 		u16 tid_bitmap;
1069 
1070 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
1071 		mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
1072 
1073 		tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1074 
1075 		/*
1076 		 * We need to take into account a situation in which a TXQ was
1077 		 * allocated to TID x, and then turned shared by adding TIDs y
1078 		 * and z. If TID x becomes inactive and is removed from the TXQ,
1079 		 * ownership must be given to one of the remaining TIDs.
1080 		 * This is mainly because if TID x continues - a new queue can't
1081 		 * be allocated for it as long as it is an owner of another TXQ.
1082 		 *
1083 		 * Mark this queue in the right bitmap, we'll send the command
1084 		 * to the firmware later.
1085 		 */
1086 		if (!(tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1087 			set_bit(queue, changetid_queues);
1088 
1089 		IWL_DEBUG_TX_QUEUES(mvm,
1090 				    "Removing inactive TID %d from shared Q:%d\n",
1091 				    tid, queue);
1092 	}
1093 
1094 	IWL_DEBUG_TX_QUEUES(mvm,
1095 			    "TXQ #%d left with tid bitmap 0x%x\n", queue,
1096 			    mvm->queue_info[queue].tid_bitmap);
1097 
1098 	/*
1099 	 * There may be different TIDs with the same mac queues, so make
1100 	 * sure all TIDs have existing corresponding mac queues enabled
1101 	 */
1102 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1103 
1104 	/* If the queue is marked as shared - "unshare" it */
1105 	if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
1106 	    mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
1107 		IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
1108 				    queue);
1109 		set_bit(queue, unshare_queues);
1110 	}
1111 
1112 	return false;
1113 }
1114 
1115 /*
1116  * Check for inactivity - this includes checking if any queue
1117  * can be unshared and finding one (and only one) that can be
1118  * reused.
1119  * This function is also invoked as a sort of clean-up task,
1120  * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1121  *
1122  * Returns the queue number, or -ENOSPC.
1123  */
iwl_mvm_inactivity_check(struct iwl_mvm * mvm,u8 alloc_for_sta)1124 static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
1125 {
1126 	unsigned long now = jiffies;
1127 	unsigned long unshare_queues = 0;
1128 	unsigned long changetid_queues = 0;
1129 	int i, ret, free_queue = -ENOSPC;
1130 	struct ieee80211_sta *queue_owner  = NULL;
1131 
1132 	lockdep_assert_held(&mvm->mutex);
1133 
1134 	if (iwl_mvm_has_new_tx_api(mvm))
1135 		return -ENOSPC;
1136 
1137 	rcu_read_lock();
1138 
1139 	/* we skip the CMD queue below by starting at 1 */
1140 	BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1141 
1142 	for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
1143 		struct ieee80211_sta *sta;
1144 		struct iwl_mvm_sta *mvmsta;
1145 		u8 sta_id;
1146 		int tid;
1147 		unsigned long inactive_tid_bitmap = 0;
1148 		unsigned long queue_tid_bitmap;
1149 
1150 		queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1151 		if (!queue_tid_bitmap)
1152 			continue;
1153 
1154 		/* If TXQ isn't in active use anyway - nothing to do here... */
1155 		if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1156 		    mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
1157 			continue;
1158 
1159 		/* Check to see if there are inactive TIDs on this queue */
1160 		for_each_set_bit(tid, &queue_tid_bitmap,
1161 				 IWL_MAX_TID_COUNT + 1) {
1162 			if (time_after(mvm->queue_info[i].last_frame_time[tid] +
1163 				       IWL_MVM_DQA_QUEUE_TIMEOUT, now))
1164 				continue;
1165 
1166 			inactive_tid_bitmap |= BIT(tid);
1167 		}
1168 
1169 		/* If all TIDs are active - finish check on this queue */
1170 		if (!inactive_tid_bitmap)
1171 			continue;
1172 
1173 		/*
1174 		 * If we are here - the queue hadn't been served recently and is
1175 		 * in use
1176 		 */
1177 
1178 		sta_id = mvm->queue_info[i].ra_sta_id;
1179 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1180 
1181 		/*
1182 		 * If the STA doesn't exist anymore, it isn't an error. It could
1183 		 * be that it was removed since getting the queues, and in this
1184 		 * case it should've inactivated its queues anyway.
1185 		 */
1186 		if (IS_ERR_OR_NULL(sta))
1187 			continue;
1188 
1189 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
1190 
1191 		spin_lock_bh(&mvmsta->lock);
1192 		ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
1193 						   inactive_tid_bitmap,
1194 						   &unshare_queues,
1195 						   &changetid_queues);
1196 		if (ret && free_queue < 0) {
1197 			queue_owner = sta;
1198 			free_queue = i;
1199 		}
1200 		/* only unlock sta lock - we still need the queue info lock */
1201 		spin_unlock_bh(&mvmsta->lock);
1202 	}
1203 
1204 
1205 	/* Reconfigure queues requiring reconfiguation */
1206 	for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
1207 		iwl_mvm_unshare_queue(mvm, i);
1208 	for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1209 		iwl_mvm_change_queue_tid(mvm, i);
1210 
1211 	rcu_read_unlock();
1212 
1213 	if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1214 		ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1215 						  alloc_for_sta);
1216 		if (ret)
1217 			return ret;
1218 	}
1219 
1220 	return free_queue;
1221 }
1222 
iwl_mvm_sta_alloc_queue(struct iwl_mvm * mvm,struct ieee80211_sta * sta,u8 ac,int tid)1223 static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1224 				   struct ieee80211_sta *sta, u8 ac, int tid)
1225 {
1226 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1227 	struct iwl_trans_txq_scd_cfg cfg = {
1228 		.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1229 		.sta_id = mvmsta->sta_id,
1230 		.tid = tid,
1231 		.frame_limit = IWL_FRAME_LIMIT,
1232 	};
1233 	unsigned int wdg_timeout =
1234 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1235 	int queue = -1;
1236 	u16 queue_tmp;
1237 	unsigned long disable_agg_tids = 0;
1238 	enum iwl_mvm_agg_state queue_state;
1239 	bool shared_queue = false, inc_ssn;
1240 	int ssn;
1241 	unsigned long tfd_queue_mask;
1242 	int ret;
1243 
1244 	lockdep_assert_held(&mvm->mutex);
1245 
1246 	if (iwl_mvm_has_new_tx_api(mvm))
1247 		return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1248 
1249 	spin_lock_bh(&mvmsta->lock);
1250 	tfd_queue_mask = mvmsta->tfd_queue_msk;
1251 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1252 	spin_unlock_bh(&mvmsta->lock);
1253 
1254 	if (tid == IWL_MAX_TID_COUNT) {
1255 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1256 						IWL_MVM_DQA_MIN_MGMT_QUEUE,
1257 						IWL_MVM_DQA_MAX_MGMT_QUEUE);
1258 		if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1259 			IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1260 					    queue);
1261 
1262 		/* If no such queue is found, we'll use a DATA queue instead */
1263 	}
1264 
1265 	if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1266 	    (mvm->queue_info[mvmsta->reserved_queue].status ==
1267 			IWL_MVM_QUEUE_RESERVED)) {
1268 		queue = mvmsta->reserved_queue;
1269 		mvm->queue_info[queue].reserved = true;
1270 		IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1271 	}
1272 
1273 	if (queue < 0)
1274 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1275 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1276 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1277 	if (queue < 0) {
1278 		/* try harder - perhaps kill an inactive queue */
1279 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1280 	}
1281 
1282 	/* No free queue - we'll have to share */
1283 	if (queue <= 0) {
1284 		queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1285 		if (queue > 0) {
1286 			shared_queue = true;
1287 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1288 		}
1289 	}
1290 
1291 	/*
1292 	 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1293 	 * to make sure no one else takes it.
1294 	 * This will allow avoiding re-acquiring the lock at the end of the
1295 	 * configuration. On error we'll mark it back as free.
1296 	 */
1297 	if (queue > 0 && !shared_queue)
1298 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1299 
1300 	/* This shouldn't happen - out of queues */
1301 	if (WARN_ON(queue <= 0)) {
1302 		IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1303 			tid, cfg.sta_id);
1304 		return queue;
1305 	}
1306 
1307 	/*
1308 	 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1309 	 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1310 	 * as aggregatable.
1311 	 * Mark all DATA queues as allowing to be aggregated at some point
1312 	 */
1313 	cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1314 			 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1315 
1316 	IWL_DEBUG_TX_QUEUES(mvm,
1317 			    "Allocating %squeue #%d to sta %d on tid %d\n",
1318 			    shared_queue ? "shared " : "", queue,
1319 			    mvmsta->sta_id, tid);
1320 
1321 	if (shared_queue) {
1322 		/* Disable any open aggs on this queue */
1323 		disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1324 
1325 		if (disable_agg_tids) {
1326 			IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1327 					    queue);
1328 			iwl_mvm_invalidate_sta_queue(mvm, queue,
1329 						     disable_agg_tids, false);
1330 		}
1331 	}
1332 
1333 	inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1334 
1335 	/*
1336 	 * Mark queue as shared in transport if shared
1337 	 * Note this has to be done after queue enablement because enablement
1338 	 * can also set this value, and there is no indication there to shared
1339 	 * queues
1340 	 */
1341 	if (shared_queue)
1342 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1343 
1344 	spin_lock_bh(&mvmsta->lock);
1345 	/*
1346 	 * This looks racy, but it is not. We have only one packet for
1347 	 * this ra/tid in our Tx path since we stop the Qdisc when we
1348 	 * need to allocate a new TFD queue.
1349 	 */
1350 	if (inc_ssn) {
1351 		mvmsta->tid_data[tid].seq_number += 0x10;
1352 		ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
1353 	}
1354 	mvmsta->tid_data[tid].txq_id = queue;
1355 	mvmsta->tfd_queue_msk |= BIT(queue);
1356 	queue_state = mvmsta->tid_data[tid].state;
1357 
1358 	if (mvmsta->reserved_queue == queue)
1359 		mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1360 	spin_unlock_bh(&mvmsta->lock);
1361 
1362 	if (!shared_queue) {
1363 		ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1364 		if (ret)
1365 			goto out_err;
1366 
1367 		/* If we need to re-enable aggregations... */
1368 		if (queue_state == IWL_AGG_ON) {
1369 			ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1370 			if (ret)
1371 				goto out_err;
1372 		}
1373 	} else {
1374 		/* Redirect queue, if needed */
1375 		ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1376 					     wdg_timeout, false,
1377 					     iwl_mvm_txq_from_tid(sta, tid));
1378 		if (ret)
1379 			goto out_err;
1380 	}
1381 
1382 	return 0;
1383 
1384 out_err:
1385 	queue_tmp = queue;
1386 	iwl_mvm_disable_txq(mvm, sta, &queue_tmp, tid, 0);
1387 
1388 	return ret;
1389 }
1390 
iwl_mvm_add_new_dqa_stream_wk(struct work_struct * wk)1391 void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1392 {
1393 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1394 					   add_stream_wk);
1395 
1396 	mutex_lock(&mvm->mutex);
1397 
1398 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1399 
1400 	while (!list_empty(&mvm->add_stream_txqs)) {
1401 		struct iwl_mvm_txq *mvmtxq;
1402 		struct ieee80211_txq *txq;
1403 		u8 tid;
1404 
1405 		mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1406 					  struct iwl_mvm_txq, list);
1407 
1408 		txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1409 				   drv_priv);
1410 		tid = txq->tid;
1411 		if (tid == IEEE80211_NUM_TIDS)
1412 			tid = IWL_MAX_TID_COUNT;
1413 
1414 		/*
1415 		 * We can't really do much here, but if this fails we can't
1416 		 * transmit anyway - so just don't transmit the frame etc.
1417 		 * and let them back up ... we've tried our best to allocate
1418 		 * a queue in the function itself.
1419 		 */
1420 		if (iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid)) {
1421 			list_del_init(&mvmtxq->list);
1422 			continue;
1423 		}
1424 
1425 		list_del_init(&mvmtxq->list);
1426 		local_bh_disable();
1427 		iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1428 		local_bh_enable();
1429 	}
1430 
1431 	mutex_unlock(&mvm->mutex);
1432 }
1433 
iwl_mvm_reserve_sta_stream(struct iwl_mvm * mvm,struct ieee80211_sta * sta,enum nl80211_iftype vif_type)1434 static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1435 				      struct ieee80211_sta *sta,
1436 				      enum nl80211_iftype vif_type)
1437 {
1438 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1439 	int queue;
1440 
1441 	/* queue reserving is disabled on new TX path */
1442 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1443 		return 0;
1444 
1445 	/* run the general cleanup/unsharing of queues */
1446 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1447 
1448 	/* Make sure we have free resources for this STA */
1449 	if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1450 	    !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1451 	    (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1452 	     IWL_MVM_QUEUE_FREE))
1453 		queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1454 	else
1455 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1456 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1457 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1458 	if (queue < 0) {
1459 		/* try again - this time kick out a queue if needed */
1460 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1461 		if (queue < 0) {
1462 			IWL_ERR(mvm, "No available queues for new station\n");
1463 			return -ENOSPC;
1464 		}
1465 	}
1466 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
1467 
1468 	mvmsta->reserved_queue = queue;
1469 
1470 	IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1471 			    queue, mvmsta->sta_id);
1472 
1473 	return 0;
1474 }
1475 
1476 /*
1477  * In DQA mode, after a HW restart the queues should be allocated as before, in
1478  * order to avoid race conditions when there are shared queues. This function
1479  * does the re-mapping and queue allocation.
1480  *
1481  * Note that re-enabling aggregations isn't done in this function.
1482  */
iwl_mvm_realloc_queues_after_restart(struct iwl_mvm * mvm,struct ieee80211_sta * sta)1483 static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1484 						 struct ieee80211_sta *sta)
1485 {
1486 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1487 	unsigned int wdg =
1488 		iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1489 	int i;
1490 	struct iwl_trans_txq_scd_cfg cfg = {
1491 		.sta_id = mvm_sta->sta_id,
1492 		.frame_limit = IWL_FRAME_LIMIT,
1493 	};
1494 
1495 	/* Make sure reserved queue is still marked as such (if allocated) */
1496 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1497 		mvm->queue_info[mvm_sta->reserved_queue].status =
1498 			IWL_MVM_QUEUE_RESERVED;
1499 
1500 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1501 		struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1502 		int txq_id = tid_data->txq_id;
1503 		int ac;
1504 
1505 		if (txq_id == IWL_MVM_INVALID_QUEUE)
1506 			continue;
1507 
1508 		ac = tid_to_mac80211_ac[i];
1509 
1510 		if (iwl_mvm_has_new_tx_api(mvm)) {
1511 			IWL_DEBUG_TX_QUEUES(mvm,
1512 					    "Re-mapping sta %d tid %d\n",
1513 					    mvm_sta->sta_id, i);
1514 			txq_id = iwl_mvm_tvqm_enable_txq(mvm, mvm_sta->sta_id,
1515 							 i, wdg);
1516 			/*
1517 			 * on failures, just set it to IWL_MVM_INVALID_QUEUE
1518 			 * to try again later, we have no other good way of
1519 			 * failing here
1520 			 */
1521 			if (txq_id < 0)
1522 				txq_id = IWL_MVM_INVALID_QUEUE;
1523 			tid_data->txq_id = txq_id;
1524 
1525 			/*
1526 			 * Since we don't set the seq number after reset, and HW
1527 			 * sets it now, FW reset will cause the seq num to start
1528 			 * at 0 again, so driver will need to update it
1529 			 * internally as well, so it keeps in sync with real val
1530 			 */
1531 			tid_data->seq_number = 0;
1532 		} else {
1533 			u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1534 
1535 			cfg.tid = i;
1536 			cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
1537 			cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1538 					 txq_id ==
1539 					 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1540 
1541 			IWL_DEBUG_TX_QUEUES(mvm,
1542 					    "Re-mapping sta %d tid %d to queue %d\n",
1543 					    mvm_sta->sta_id, i, txq_id);
1544 
1545 			iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
1546 			mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1547 		}
1548 	}
1549 }
1550 
iwl_mvm_add_int_sta_common(struct iwl_mvm * mvm,struct iwl_mvm_int_sta * sta,const u8 * addr,u16 mac_id,u16 color)1551 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1552 				      struct iwl_mvm_int_sta *sta,
1553 				      const u8 *addr,
1554 				      u16 mac_id, u16 color)
1555 {
1556 	struct iwl_mvm_add_sta_cmd cmd;
1557 	int ret;
1558 	u32 status = ADD_STA_SUCCESS;
1559 
1560 	lockdep_assert_held(&mvm->mutex);
1561 
1562 	memset(&cmd, 0, sizeof(cmd));
1563 	cmd.sta_id = sta->sta_id;
1564 
1565 	if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP, ADD_STA,
1566 				  0) >= 12 &&
1567 	    sta->type == IWL_STA_AUX_ACTIVITY)
1568 		cmd.mac_id_n_color = cpu_to_le32(mac_id);
1569 	else
1570 		cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1571 								     color));
1572 
1573 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1574 		cmd.station_type = sta->type;
1575 
1576 	if (!iwl_mvm_has_new_tx_api(mvm))
1577 		cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1578 	cmd.tid_disable_tx = cpu_to_le16(0xffff);
1579 
1580 	if (addr)
1581 		memcpy(cmd.addr, addr, ETH_ALEN);
1582 
1583 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1584 					  iwl_mvm_add_sta_cmd_size(mvm),
1585 					  &cmd, &status);
1586 	if (ret)
1587 		return ret;
1588 
1589 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1590 	case ADD_STA_SUCCESS:
1591 		IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1592 		return 0;
1593 	default:
1594 		ret = -EIO;
1595 		IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1596 			status);
1597 		break;
1598 	}
1599 	return ret;
1600 }
1601 
iwl_mvm_add_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1602 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1603 		    struct ieee80211_vif *vif,
1604 		    struct ieee80211_sta *sta)
1605 {
1606 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1607 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1608 	struct iwl_mvm_rxq_dup_data *dup_data;
1609 	int i, ret, sta_id;
1610 	bool sta_update = false;
1611 	unsigned int sta_flags = 0;
1612 
1613 	lockdep_assert_held(&mvm->mutex);
1614 
1615 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1616 		sta_id = iwl_mvm_find_free_sta_id(mvm,
1617 						  ieee80211_vif_type_p2p(vif));
1618 	else
1619 		sta_id = mvm_sta->sta_id;
1620 
1621 	if (sta_id == IWL_MVM_INVALID_STA)
1622 		return -ENOSPC;
1623 
1624 	spin_lock_init(&mvm_sta->lock);
1625 
1626 	/* if this is a HW restart re-alloc existing queues */
1627 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1628 		struct iwl_mvm_int_sta tmp_sta = {
1629 			.sta_id = sta_id,
1630 			.type = mvm_sta->sta_type,
1631 		};
1632 
1633 		/*
1634 		 * First add an empty station since allocating
1635 		 * a queue requires a valid station
1636 		 */
1637 		ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1638 						 mvmvif->id, mvmvif->color);
1639 		if (ret)
1640 			goto err;
1641 
1642 		iwl_mvm_realloc_queues_after_restart(mvm, sta);
1643 		sta_update = true;
1644 		sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
1645 		goto update_fw;
1646 	}
1647 
1648 	mvm_sta->sta_id = sta_id;
1649 	mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1650 						      mvmvif->color);
1651 	mvm_sta->vif = vif;
1652 	if (!mvm->trans->trans_cfg->gen2)
1653 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1654 	else
1655 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1656 	mvm_sta->tx_protection = 0;
1657 	mvm_sta->tt_tx_protection = false;
1658 	mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1659 
1660 	/* HW restart, don't assume the memory has been zeroed */
1661 	mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1662 	mvm_sta->tfd_queue_msk = 0;
1663 
1664 	/* for HW restart - reset everything but the sequence number */
1665 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1666 		u16 seq = mvm_sta->tid_data[i].seq_number;
1667 		memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1668 		mvm_sta->tid_data[i].seq_number = seq;
1669 
1670 		/*
1671 		 * Mark all queues for this STA as unallocated and defer TX
1672 		 * frames until the queue is allocated
1673 		 */
1674 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1675 	}
1676 
1677 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1678 		struct iwl_mvm_txq *mvmtxq =
1679 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1680 
1681 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1682 		INIT_LIST_HEAD(&mvmtxq->list);
1683 		atomic_set(&mvmtxq->tx_request, 0);
1684 	}
1685 
1686 	mvm_sta->agg_tids = 0;
1687 
1688 	if (iwl_mvm_has_new_rx_api(mvm) &&
1689 	    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1690 		int q;
1691 
1692 		dup_data = kcalloc(mvm->trans->num_rx_queues,
1693 				   sizeof(*dup_data), GFP_KERNEL);
1694 		if (!dup_data)
1695 			return -ENOMEM;
1696 		/*
1697 		 * Initialize all the last_seq values to 0xffff which can never
1698 		 * compare equal to the frame's seq_ctrl in the check in
1699 		 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1700 		 * number and fragmented packets don't reach that function.
1701 		 *
1702 		 * This thus allows receiving a packet with seqno 0 and the
1703 		 * retry bit set as the very first packet on a new TID.
1704 		 */
1705 		for (q = 0; q < mvm->trans->num_rx_queues; q++)
1706 			memset(dup_data[q].last_seq, 0xff,
1707 			       sizeof(dup_data[q].last_seq));
1708 		mvm_sta->dup_data = dup_data;
1709 	}
1710 
1711 	if (!iwl_mvm_has_new_tx_api(mvm)) {
1712 		ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1713 						 ieee80211_vif_type_p2p(vif));
1714 		if (ret)
1715 			goto err;
1716 	}
1717 
1718 	/*
1719 	 * if rs is registered with mac80211, then "add station" will be handled
1720 	 * via the corresponding ops, otherwise need to notify rate scaling here
1721 	 */
1722 	if (iwl_mvm_has_tlc_offload(mvm))
1723 		iwl_mvm_rs_add_sta(mvm, mvm_sta);
1724 	else
1725 		spin_lock_init(&mvm_sta->lq_sta.rs_drv.pers.lock);
1726 
1727 	iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
1728 
1729 update_fw:
1730 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1731 	if (ret)
1732 		goto err;
1733 
1734 	if (vif->type == NL80211_IFTYPE_STATION) {
1735 		if (!sta->tdls) {
1736 			WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1737 			mvmvif->ap_sta_id = sta_id;
1738 		} else {
1739 			WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1740 		}
1741 	}
1742 
1743 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1744 
1745 	return 0;
1746 
1747 err:
1748 	return ret;
1749 }
1750 
iwl_mvm_drain_sta(struct iwl_mvm * mvm,struct iwl_mvm_sta * mvmsta,bool drain)1751 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1752 		      bool drain)
1753 {
1754 	struct iwl_mvm_add_sta_cmd cmd = {};
1755 	int ret;
1756 	u32 status;
1757 
1758 	lockdep_assert_held(&mvm->mutex);
1759 
1760 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1761 	cmd.sta_id = mvmsta->sta_id;
1762 	cmd.add_modify = STA_MODE_MODIFY;
1763 	cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1764 	cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1765 
1766 	status = ADD_STA_SUCCESS;
1767 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1768 					  iwl_mvm_add_sta_cmd_size(mvm),
1769 					  &cmd, &status);
1770 	if (ret)
1771 		return ret;
1772 
1773 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1774 	case ADD_STA_SUCCESS:
1775 		IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1776 			       mvmsta->sta_id);
1777 		break;
1778 	default:
1779 		ret = -EIO;
1780 		IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1781 			mvmsta->sta_id);
1782 		break;
1783 	}
1784 
1785 	return ret;
1786 }
1787 
1788 /*
1789  * Remove a station from the FW table. Before sending the command to remove
1790  * the station validate that the station is indeed known to the driver (sanity
1791  * only).
1792  */
iwl_mvm_rm_sta_common(struct iwl_mvm * mvm,u8 sta_id)1793 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1794 {
1795 	struct ieee80211_sta *sta;
1796 	struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1797 		.sta_id = sta_id,
1798 	};
1799 	int ret;
1800 
1801 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1802 					lockdep_is_held(&mvm->mutex));
1803 
1804 	/* Note: internal stations are marked as error values */
1805 	if (!sta) {
1806 		IWL_ERR(mvm, "Invalid station id\n");
1807 		return -EINVAL;
1808 	}
1809 
1810 	ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1811 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
1812 	if (ret) {
1813 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1814 		return ret;
1815 	}
1816 
1817 	return 0;
1818 }
1819 
iwl_mvm_disable_sta_queues(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1820 static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1821 				       struct ieee80211_vif *vif,
1822 				       struct ieee80211_sta *sta)
1823 {
1824 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1825 	int i;
1826 
1827 	lockdep_assert_held(&mvm->mutex);
1828 
1829 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1830 		if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
1831 			continue;
1832 
1833 		iwl_mvm_disable_txq(mvm, sta, &mvm_sta->tid_data[i].txq_id, i,
1834 				    0);
1835 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1836 	}
1837 
1838 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1839 		struct iwl_mvm_txq *mvmtxq =
1840 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1841 
1842 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1843 	}
1844 }
1845 
iwl_mvm_wait_sta_queues_empty(struct iwl_mvm * mvm,struct iwl_mvm_sta * mvm_sta)1846 int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1847 				  struct iwl_mvm_sta *mvm_sta)
1848 {
1849 	int i;
1850 
1851 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1852 		u16 txq_id;
1853 		int ret;
1854 
1855 		spin_lock_bh(&mvm_sta->lock);
1856 		txq_id = mvm_sta->tid_data[i].txq_id;
1857 		spin_unlock_bh(&mvm_sta->lock);
1858 
1859 		if (txq_id == IWL_MVM_INVALID_QUEUE)
1860 			continue;
1861 
1862 		ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1863 		if (ret)
1864 			return ret;
1865 	}
1866 
1867 	return 0;
1868 }
1869 
iwl_mvm_rm_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1870 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1871 		   struct ieee80211_vif *vif,
1872 		   struct ieee80211_sta *sta)
1873 {
1874 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1875 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1876 	u8 sta_id = mvm_sta->sta_id;
1877 	int ret;
1878 
1879 	lockdep_assert_held(&mvm->mutex);
1880 
1881 	if (iwl_mvm_has_new_rx_api(mvm))
1882 		kfree(mvm_sta->dup_data);
1883 
1884 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1885 	if (ret)
1886 		return ret;
1887 
1888 	/* flush its queues here since we are freeing mvm_sta */
1889 	ret = iwl_mvm_flush_sta(mvm, mvm_sta, false);
1890 	if (ret)
1891 		return ret;
1892 	if (iwl_mvm_has_new_tx_api(mvm)) {
1893 		ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1894 	} else {
1895 		u32 q_mask = mvm_sta->tfd_queue_msk;
1896 
1897 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1898 						     q_mask);
1899 	}
1900 	if (ret)
1901 		return ret;
1902 
1903 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1904 
1905 	iwl_mvm_disable_sta_queues(mvm, vif, sta);
1906 
1907 	/* If there is a TXQ still marked as reserved - free it */
1908 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1909 		u8 reserved_txq = mvm_sta->reserved_queue;
1910 		enum iwl_mvm_queue_status *status;
1911 
1912 		/*
1913 		 * If no traffic has gone through the reserved TXQ - it
1914 		 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1915 		 * should be manually marked as free again
1916 		 */
1917 		status = &mvm->queue_info[reserved_txq].status;
1918 		if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1919 			 (*status != IWL_MVM_QUEUE_FREE),
1920 			 "sta_id %d reserved txq %d status %d",
1921 			 sta_id, reserved_txq, *status))
1922 			return -EINVAL;
1923 
1924 		*status = IWL_MVM_QUEUE_FREE;
1925 	}
1926 
1927 	if (vif->type == NL80211_IFTYPE_STATION &&
1928 	    mvmvif->ap_sta_id == sta_id) {
1929 		/* if associated - we can't remove the AP STA now */
1930 		if (vif->bss_conf.assoc)
1931 			return ret;
1932 
1933 		/* unassoc - go ahead - remove the AP STA now */
1934 		mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1935 	}
1936 
1937 	/*
1938 	 * This shouldn't happen - the TDLS channel switch should be canceled
1939 	 * before the STA is removed.
1940 	 */
1941 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
1942 		mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1943 		cancel_delayed_work(&mvm->tdls_cs.dwork);
1944 	}
1945 
1946 	/*
1947 	 * Make sure that the tx response code sees the station as -EBUSY and
1948 	 * calls the drain worker.
1949 	 */
1950 	spin_lock_bh(&mvm_sta->lock);
1951 	spin_unlock_bh(&mvm_sta->lock);
1952 
1953 	ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1954 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1955 
1956 	return ret;
1957 }
1958 
iwl_mvm_rm_sta_id(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u8 sta_id)1959 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1960 		      struct ieee80211_vif *vif,
1961 		      u8 sta_id)
1962 {
1963 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1964 
1965 	lockdep_assert_held(&mvm->mutex);
1966 
1967 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1968 	return ret;
1969 }
1970 
iwl_mvm_allocate_int_sta(struct iwl_mvm * mvm,struct iwl_mvm_int_sta * sta,u32 qmask,enum nl80211_iftype iftype,enum iwl_sta_type type)1971 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1972 			     struct iwl_mvm_int_sta *sta,
1973 			     u32 qmask, enum nl80211_iftype iftype,
1974 			     enum iwl_sta_type type)
1975 {
1976 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1977 	    sta->sta_id == IWL_MVM_INVALID_STA) {
1978 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
1979 		if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
1980 			return -ENOSPC;
1981 	}
1982 
1983 	sta->tfd_queue_msk = qmask;
1984 	sta->type = type;
1985 
1986 	/* put a non-NULL value so iterating over the stations won't stop */
1987 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1988 	return 0;
1989 }
1990 
iwl_mvm_dealloc_int_sta(struct iwl_mvm * mvm,struct iwl_mvm_int_sta * sta)1991 void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
1992 {
1993 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1994 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1995 	sta->sta_id = IWL_MVM_INVALID_STA;
1996 }
1997 
iwl_mvm_enable_aux_snif_queue(struct iwl_mvm * mvm,u16 queue,u8 sta_id,u8 fifo)1998 static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue,
1999 					  u8 sta_id, u8 fifo)
2000 {
2001 	unsigned int wdg_timeout =
2002 		mvm->trans->trans_cfg->base_params->wd_timeout;
2003 	struct iwl_trans_txq_scd_cfg cfg = {
2004 		.fifo = fifo,
2005 		.sta_id = sta_id,
2006 		.tid = IWL_MAX_TID_COUNT,
2007 		.aggregate = false,
2008 		.frame_limit = IWL_FRAME_LIMIT,
2009 	};
2010 
2011 	WARN_ON(iwl_mvm_has_new_tx_api(mvm));
2012 
2013 	iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2014 }
2015 
iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm * mvm,u8 sta_id)2016 static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id)
2017 {
2018 	unsigned int wdg_timeout =
2019 		mvm->trans->trans_cfg->base_params->wd_timeout;
2020 
2021 	WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
2022 
2023 	return iwl_mvm_tvqm_enable_txq(mvm, sta_id, IWL_MAX_TID_COUNT,
2024 				       wdg_timeout);
2025 }
2026 
iwl_mvm_add_int_sta_with_queue(struct iwl_mvm * mvm,int macidx,int maccolor,u8 * addr,struct iwl_mvm_int_sta * sta,u16 * queue,int fifo)2027 static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx,
2028 					  int maccolor, u8 *addr,
2029 					  struct iwl_mvm_int_sta *sta,
2030 					  u16 *queue, int fifo)
2031 {
2032 	int ret;
2033 
2034 	/* Map queue to fifo - needs to happen before adding station */
2035 	if (!iwl_mvm_has_new_tx_api(mvm))
2036 		iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo);
2037 
2038 	ret = iwl_mvm_add_int_sta_common(mvm, sta, addr, macidx, maccolor);
2039 	if (ret) {
2040 		if (!iwl_mvm_has_new_tx_api(mvm))
2041 			iwl_mvm_disable_txq(mvm, NULL, queue,
2042 					    IWL_MAX_TID_COUNT, 0);
2043 		return ret;
2044 	}
2045 
2046 	/*
2047 	 * For 22000 firmware and on we cannot add queue to a station unknown
2048 	 * to firmware so enable queue here - after the station was added
2049 	 */
2050 	if (iwl_mvm_has_new_tx_api(mvm)) {
2051 		int txq;
2052 
2053 		txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id);
2054 		if (txq < 0) {
2055 			iwl_mvm_rm_sta_common(mvm, sta->sta_id);
2056 			return txq;
2057 		}
2058 
2059 		*queue = txq;
2060 	}
2061 
2062 	return 0;
2063 }
2064 
iwl_mvm_add_aux_sta(struct iwl_mvm * mvm,u32 lmac_id)2065 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm, u32 lmac_id)
2066 {
2067 	int ret;
2068 
2069 	lockdep_assert_held(&mvm->mutex);
2070 
2071 	/* Allocate aux station and assign to it the aux queue */
2072 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
2073 				       NL80211_IFTYPE_UNSPECIFIED,
2074 				       IWL_STA_AUX_ACTIVITY);
2075 	if (ret)
2076 		return ret;
2077 
2078 	/*
2079 	 * In CDB NICs we need to specify which lmac to use for aux activity
2080 	 * using the mac_id argument place to send lmac_id to the function
2081 	 */
2082 	ret = iwl_mvm_add_int_sta_with_queue(mvm, lmac_id, 0, NULL,
2083 					     &mvm->aux_sta, &mvm->aux_queue,
2084 					     IWL_MVM_TX_FIFO_MCAST);
2085 	if (ret) {
2086 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2087 		return ret;
2088 	}
2089 
2090 	return 0;
2091 }
2092 
iwl_mvm_add_snif_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2093 int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2094 {
2095 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2096 
2097 	lockdep_assert_held(&mvm->mutex);
2098 
2099 	return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
2100 					      NULL, &mvm->snif_sta,
2101 					      &mvm->snif_queue,
2102 					      IWL_MVM_TX_FIFO_BE);
2103 }
2104 
iwl_mvm_rm_snif_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2105 int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2106 {
2107 	int ret;
2108 
2109 	lockdep_assert_held(&mvm->mutex);
2110 
2111 	if (WARN_ON_ONCE(mvm->snif_sta.sta_id == IWL_MVM_INVALID_STA))
2112 		return -EINVAL;
2113 
2114 	iwl_mvm_disable_txq(mvm, NULL, &mvm->snif_queue, IWL_MAX_TID_COUNT, 0);
2115 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
2116 	if (ret)
2117 		IWL_WARN(mvm, "Failed sending remove station\n");
2118 
2119 	return ret;
2120 }
2121 
iwl_mvm_rm_aux_sta(struct iwl_mvm * mvm)2122 int iwl_mvm_rm_aux_sta(struct iwl_mvm *mvm)
2123 {
2124 	int ret;
2125 
2126 	lockdep_assert_held(&mvm->mutex);
2127 
2128 	if (WARN_ON_ONCE(mvm->aux_sta.sta_id == IWL_MVM_INVALID_STA))
2129 		return -EINVAL;
2130 
2131 	iwl_mvm_disable_txq(mvm, NULL, &mvm->aux_queue, IWL_MAX_TID_COUNT, 0);
2132 	ret = iwl_mvm_rm_sta_common(mvm, mvm->aux_sta.sta_id);
2133 	if (ret)
2134 		IWL_WARN(mvm, "Failed sending remove station\n");
2135 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2136 
2137 	return ret;
2138 }
2139 
iwl_mvm_dealloc_snif_sta(struct iwl_mvm * mvm)2140 void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
2141 {
2142 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
2143 }
2144 
2145 /*
2146  * Send the add station command for the vif's broadcast station.
2147  * Assumes that the station was already allocated.
2148  *
2149  * @mvm: the mvm component
2150  * @vif: the interface to which the broadcast station is added
2151  * @bsta: the broadcast station to add.
2152  */
iwl_mvm_send_add_bcast_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2153 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2154 {
2155 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2156 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2157 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2158 	const u8 *baddr = _baddr;
2159 	int queue;
2160 	int ret;
2161 	unsigned int wdg_timeout =
2162 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2163 	struct iwl_trans_txq_scd_cfg cfg = {
2164 		.fifo = IWL_MVM_TX_FIFO_VO,
2165 		.sta_id = mvmvif->bcast_sta.sta_id,
2166 		.tid = IWL_MAX_TID_COUNT,
2167 		.aggregate = false,
2168 		.frame_limit = IWL_FRAME_LIMIT,
2169 	};
2170 
2171 	lockdep_assert_held(&mvm->mutex);
2172 
2173 	if (!iwl_mvm_has_new_tx_api(mvm)) {
2174 		if (vif->type == NL80211_IFTYPE_AP ||
2175 		    vif->type == NL80211_IFTYPE_ADHOC) {
2176 			queue = mvm->probe_queue;
2177 		} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2178 			queue = mvm->p2p_dev_queue;
2179 		} else {
2180 			WARN(1, "Missing required TXQ for adding bcast STA\n");
2181 			return -EINVAL;
2182 		}
2183 
2184 		bsta->tfd_queue_msk |= BIT(queue);
2185 
2186 		iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2187 	}
2188 
2189 	if (vif->type == NL80211_IFTYPE_ADHOC)
2190 		baddr = vif->bss_conf.bssid;
2191 
2192 	if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2193 		return -ENOSPC;
2194 
2195 	ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2196 					 mvmvif->id, mvmvif->color);
2197 	if (ret)
2198 		return ret;
2199 
2200 	/*
2201 	 * For 22000 firmware and on we cannot add queue to a station unknown
2202 	 * to firmware so enable queue here - after the station was added
2203 	 */
2204 	if (iwl_mvm_has_new_tx_api(mvm)) {
2205 		queue = iwl_mvm_tvqm_enable_txq(mvm, bsta->sta_id,
2206 						IWL_MAX_TID_COUNT,
2207 						wdg_timeout);
2208 		if (queue < 0) {
2209 			iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
2210 			return queue;
2211 		}
2212 
2213 		if (vif->type == NL80211_IFTYPE_AP ||
2214 		    vif->type == NL80211_IFTYPE_ADHOC)
2215 			mvm->probe_queue = queue;
2216 		else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2217 			mvm->p2p_dev_queue = queue;
2218 	}
2219 
2220 	return 0;
2221 }
2222 
iwl_mvm_free_bcast_sta_queues(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2223 static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2224 					  struct ieee80211_vif *vif)
2225 {
2226 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2227 	u16 *queueptr, queue;
2228 
2229 	lockdep_assert_held(&mvm->mutex);
2230 
2231 	iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true);
2232 
2233 	switch (vif->type) {
2234 	case NL80211_IFTYPE_AP:
2235 	case NL80211_IFTYPE_ADHOC:
2236 		queueptr = &mvm->probe_queue;
2237 		break;
2238 	case NL80211_IFTYPE_P2P_DEVICE:
2239 		queueptr = &mvm->p2p_dev_queue;
2240 		break;
2241 	default:
2242 		WARN(1, "Can't free bcast queue on vif type %d\n",
2243 		     vif->type);
2244 		return;
2245 	}
2246 
2247 	queue = *queueptr;
2248 	iwl_mvm_disable_txq(mvm, NULL, queueptr, IWL_MAX_TID_COUNT, 0);
2249 	if (iwl_mvm_has_new_tx_api(mvm))
2250 		return;
2251 
2252 	WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2253 	mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2254 }
2255 
2256 /* Send the FW a request to remove the station from it's internal data
2257  * structures, but DO NOT remove the entry from the local data structures. */
iwl_mvm_send_rm_bcast_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2258 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2259 {
2260 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2261 	int ret;
2262 
2263 	lockdep_assert_held(&mvm->mutex);
2264 
2265 	iwl_mvm_free_bcast_sta_queues(mvm, vif);
2266 
2267 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2268 	if (ret)
2269 		IWL_WARN(mvm, "Failed sending remove station\n");
2270 	return ret;
2271 }
2272 
iwl_mvm_alloc_bcast_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2273 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2274 {
2275 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2276 
2277 	lockdep_assert_held(&mvm->mutex);
2278 
2279 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2280 					ieee80211_vif_type_p2p(vif),
2281 					IWL_STA_GENERAL_PURPOSE);
2282 }
2283 
2284 /* Allocate a new station entry for the broadcast station to the given vif,
2285  * and send it to the FW.
2286  * Note that each P2P mac should have its own broadcast station.
2287  *
2288  * @mvm: the mvm component
2289  * @vif: the interface to which the broadcast station is added
2290  * @bsta: the broadcast station to add. */
iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2291 int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2292 {
2293 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2294 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2295 	int ret;
2296 
2297 	lockdep_assert_held(&mvm->mutex);
2298 
2299 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2300 	if (ret)
2301 		return ret;
2302 
2303 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2304 
2305 	if (ret)
2306 		iwl_mvm_dealloc_int_sta(mvm, bsta);
2307 
2308 	return ret;
2309 }
2310 
iwl_mvm_dealloc_bcast_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2311 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2312 {
2313 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2314 
2315 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2316 }
2317 
2318 /*
2319  * Send the FW a request to remove the station from it's internal data
2320  * structures, and in addition remove it from the local data structure.
2321  */
iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2322 int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2323 {
2324 	int ret;
2325 
2326 	lockdep_assert_held(&mvm->mutex);
2327 
2328 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2329 
2330 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
2331 
2332 	return ret;
2333 }
2334 
2335 /*
2336  * Allocate a new station entry for the multicast station to the given vif,
2337  * and send it to the FW.
2338  * Note that each AP/GO mac should have its own multicast station.
2339  *
2340  * @mvm: the mvm component
2341  * @vif: the interface to which the multicast station is added
2342  */
iwl_mvm_add_mcast_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2343 int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2344 {
2345 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2346 	struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
2347 	static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2348 	const u8 *maddr = _maddr;
2349 	struct iwl_trans_txq_scd_cfg cfg = {
2350 		.fifo = vif->type == NL80211_IFTYPE_AP ?
2351 			IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
2352 		.sta_id = msta->sta_id,
2353 		.tid = 0,
2354 		.aggregate = false,
2355 		.frame_limit = IWL_FRAME_LIMIT,
2356 	};
2357 	unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2358 	int ret;
2359 
2360 	lockdep_assert_held(&mvm->mutex);
2361 
2362 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2363 		    vif->type != NL80211_IFTYPE_ADHOC))
2364 		return -ENOTSUPP;
2365 
2366 	/*
2367 	 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2368 	 * invalid, so make sure we use the queue we want.
2369 	 * Note that this is done here as we want to avoid making DQA
2370 	 * changes in mac80211 layer.
2371 	 */
2372 	if (vif->type == NL80211_IFTYPE_ADHOC)
2373 		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2374 
2375 	/*
2376 	 * While in previous FWs we had to exclude cab queue from TFD queue
2377 	 * mask, now it is needed as any other queue.
2378 	 */
2379 	if (!iwl_mvm_has_new_tx_api(mvm) &&
2380 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2381 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2382 				   timeout);
2383 		msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2384 	}
2385 	ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2386 					 mvmvif->id, mvmvif->color);
2387 	if (ret)
2388 		goto err;
2389 
2390 	/*
2391 	 * Enable cab queue after the ADD_STA command is sent.
2392 	 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2393 	 * command with unknown station id, and for FW that doesn't support
2394 	 * station API since the cab queue is not included in the
2395 	 * tfd_queue_mask.
2396 	 */
2397 	if (iwl_mvm_has_new_tx_api(mvm)) {
2398 		int queue = iwl_mvm_tvqm_enable_txq(mvm, msta->sta_id,
2399 						    0,
2400 						    timeout);
2401 		if (queue < 0) {
2402 			ret = queue;
2403 			goto err;
2404 		}
2405 		mvmvif->cab_queue = queue;
2406 	} else if (!fw_has_api(&mvm->fw->ucode_capa,
2407 			       IWL_UCODE_TLV_API_STA_TYPE))
2408 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2409 				   timeout);
2410 
2411 	return 0;
2412 err:
2413 	iwl_mvm_dealloc_int_sta(mvm, msta);
2414 	return ret;
2415 }
2416 
__iwl_mvm_remove_sta_key(struct iwl_mvm * mvm,u8 sta_id,struct ieee80211_key_conf * keyconf,bool mcast)2417 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2418 				    struct ieee80211_key_conf *keyconf,
2419 				    bool mcast)
2420 {
2421 	union {
2422 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2423 		struct iwl_mvm_add_sta_key_cmd cmd;
2424 	} u = {};
2425 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2426 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
2427 	__le16 key_flags;
2428 	int ret, size;
2429 	u32 status;
2430 
2431 	/* This is a valid situation for GTK removal */
2432 	if (sta_id == IWL_MVM_INVALID_STA)
2433 		return 0;
2434 
2435 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2436 				 STA_KEY_FLG_KEYID_MSK);
2437 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2438 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2439 
2440 	if (mcast)
2441 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2442 
2443 	/*
2444 	 * The fields assigned here are in the same location at the start
2445 	 * of the command, so we can do this union trick.
2446 	 */
2447 	u.cmd.common.key_flags = key_flags;
2448 	u.cmd.common.key_offset = keyconf->hw_key_idx;
2449 	u.cmd.common.sta_id = sta_id;
2450 
2451 	size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
2452 
2453 	status = ADD_STA_SUCCESS;
2454 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
2455 					  &status);
2456 
2457 	switch (status) {
2458 	case ADD_STA_SUCCESS:
2459 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2460 		break;
2461 	default:
2462 		ret = -EIO;
2463 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2464 		break;
2465 	}
2466 
2467 	return ret;
2468 }
2469 
2470 /*
2471  * Send the FW a request to remove the station from it's internal data
2472  * structures, and in addition remove it from the local data structure.
2473  */
iwl_mvm_rm_mcast_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2474 int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2475 {
2476 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2477 	int ret;
2478 
2479 	lockdep_assert_held(&mvm->mutex);
2480 
2481 	iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true);
2482 
2483 	iwl_mvm_disable_txq(mvm, NULL, &mvmvif->cab_queue, 0, 0);
2484 
2485 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2486 	if (ret)
2487 		IWL_WARN(mvm, "Failed sending remove station\n");
2488 
2489 	return ret;
2490 }
2491 
2492 #define IWL_MAX_RX_BA_SESSIONS 16
2493 
iwl_mvm_sync_rxq_del_ba(struct iwl_mvm * mvm,u8 baid)2494 static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
2495 {
2496 	struct iwl_mvm_rss_sync_notif notif = {
2497 		.metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2498 		.metadata.sync = 1,
2499 		.delba.baid = baid,
2500 	};
2501 	iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
2502 };
2503 
iwl_mvm_free_reorder(struct iwl_mvm * mvm,struct iwl_mvm_baid_data * data)2504 static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2505 				 struct iwl_mvm_baid_data *data)
2506 {
2507 	int i;
2508 
2509 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2510 
2511 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2512 		int j;
2513 		struct iwl_mvm_reorder_buffer *reorder_buf =
2514 			&data->reorder_buf[i];
2515 		struct iwl_mvm_reorder_buf_entry *entries =
2516 			&data->entries[i * data->entries_per_queue];
2517 
2518 		spin_lock_bh(&reorder_buf->lock);
2519 		if (likely(!reorder_buf->num_stored)) {
2520 			spin_unlock_bh(&reorder_buf->lock);
2521 			continue;
2522 		}
2523 
2524 		/*
2525 		 * This shouldn't happen in regular DELBA since the internal
2526 		 * delBA notification should trigger a release of all frames in
2527 		 * the reorder buffer.
2528 		 */
2529 		WARN_ON(1);
2530 
2531 		for (j = 0; j < reorder_buf->buf_size; j++)
2532 			__skb_queue_purge(&entries[j].e.frames);
2533 		/*
2534 		 * Prevent timer re-arm. This prevents a very far fetched case
2535 		 * where we timed out on the notification. There may be prior
2536 		 * RX frames pending in the RX queue before the notification
2537 		 * that might get processed between now and the actual deletion
2538 		 * and we would re-arm the timer although we are deleting the
2539 		 * reorder buffer.
2540 		 */
2541 		reorder_buf->removed = true;
2542 		spin_unlock_bh(&reorder_buf->lock);
2543 		del_timer_sync(&reorder_buf->reorder_timer);
2544 	}
2545 }
2546 
iwl_mvm_init_reorder_buffer(struct iwl_mvm * mvm,struct iwl_mvm_baid_data * data,u16 ssn,u16 buf_size)2547 static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2548 					struct iwl_mvm_baid_data *data,
2549 					u16 ssn, u16 buf_size)
2550 {
2551 	int i;
2552 
2553 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2554 		struct iwl_mvm_reorder_buffer *reorder_buf =
2555 			&data->reorder_buf[i];
2556 		struct iwl_mvm_reorder_buf_entry *entries =
2557 			&data->entries[i * data->entries_per_queue];
2558 		int j;
2559 
2560 		reorder_buf->num_stored = 0;
2561 		reorder_buf->head_sn = ssn;
2562 		reorder_buf->buf_size = buf_size;
2563 		/* rx reorder timer */
2564 		timer_setup(&reorder_buf->reorder_timer,
2565 			    iwl_mvm_reorder_timer_expired, 0);
2566 		spin_lock_init(&reorder_buf->lock);
2567 		reorder_buf->mvm = mvm;
2568 		reorder_buf->queue = i;
2569 		reorder_buf->valid = false;
2570 		for (j = 0; j < reorder_buf->buf_size; j++)
2571 			__skb_queue_head_init(&entries[j].e.frames);
2572 	}
2573 }
2574 
iwl_mvm_sta_rx_agg(struct iwl_mvm * mvm,struct ieee80211_sta * sta,int tid,u16 ssn,bool start,u16 buf_size,u16 timeout)2575 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2576 		       int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2577 {
2578 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2579 	struct iwl_mvm_add_sta_cmd cmd = {};
2580 	struct iwl_mvm_baid_data *baid_data = NULL;
2581 	int ret;
2582 	u32 status;
2583 
2584 	lockdep_assert_held(&mvm->mutex);
2585 
2586 	if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2587 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2588 		return -ENOSPC;
2589 	}
2590 
2591 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
2592 		u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2593 
2594 		/* sparse doesn't like the __align() so don't check */
2595 #ifndef __CHECKER__
2596 		/*
2597 		 * The division below will be OK if either the cache line size
2598 		 * can be divided by the entry size (ALIGN will round up) or if
2599 		 * if the entry size can be divided by the cache line size, in
2600 		 * which case the ALIGN() will do nothing.
2601 		 */
2602 		BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2603 			     sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2604 #endif
2605 
2606 		/*
2607 		 * Upward align the reorder buffer size to fill an entire cache
2608 		 * line for each queue, to avoid sharing cache lines between
2609 		 * different queues.
2610 		 */
2611 		reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2612 
2613 		/*
2614 		 * Allocate here so if allocation fails we can bail out early
2615 		 * before starting the BA session in the firmware
2616 		 */
2617 		baid_data = kzalloc(sizeof(*baid_data) +
2618 				    mvm->trans->num_rx_queues *
2619 				    reorder_buf_size,
2620 				    GFP_KERNEL);
2621 		if (!baid_data)
2622 			return -ENOMEM;
2623 
2624 		/*
2625 		 * This division is why we need the above BUILD_BUG_ON(),
2626 		 * if that doesn't hold then this will not be right.
2627 		 */
2628 		baid_data->entries_per_queue =
2629 			reorder_buf_size / sizeof(baid_data->entries[0]);
2630 	}
2631 
2632 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2633 	cmd.sta_id = mvm_sta->sta_id;
2634 	cmd.add_modify = STA_MODE_MODIFY;
2635 	if (start) {
2636 		cmd.add_immediate_ba_tid = (u8) tid;
2637 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2638 		cmd.rx_ba_window = cpu_to_le16(buf_size);
2639 	} else {
2640 		cmd.remove_immediate_ba_tid = (u8) tid;
2641 	}
2642 	cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2643 				  STA_MODIFY_REMOVE_BA_TID;
2644 
2645 	status = ADD_STA_SUCCESS;
2646 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2647 					  iwl_mvm_add_sta_cmd_size(mvm),
2648 					  &cmd, &status);
2649 	if (ret)
2650 		goto out_free;
2651 
2652 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2653 	case ADD_STA_SUCCESS:
2654 		IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2655 			     start ? "start" : "stopp");
2656 		break;
2657 	case ADD_STA_IMMEDIATE_BA_FAILURE:
2658 		IWL_WARN(mvm, "RX BA Session refused by fw\n");
2659 		ret = -ENOSPC;
2660 		break;
2661 	default:
2662 		ret = -EIO;
2663 		IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2664 			start ? "start" : "stopp", status);
2665 		break;
2666 	}
2667 
2668 	if (ret)
2669 		goto out_free;
2670 
2671 	if (start) {
2672 		u8 baid;
2673 
2674 		mvm->rx_ba_sessions++;
2675 
2676 		if (!iwl_mvm_has_new_rx_api(mvm))
2677 			return 0;
2678 
2679 		if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2680 			ret = -EINVAL;
2681 			goto out_free;
2682 		}
2683 		baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2684 			    IWL_ADD_STA_BAID_SHIFT);
2685 		baid_data->baid = baid;
2686 		baid_data->timeout = timeout;
2687 		baid_data->last_rx = jiffies;
2688 		baid_data->rcu_ptr = &mvm->baid_map[baid];
2689 		timer_setup(&baid_data->session_timer,
2690 			    iwl_mvm_rx_agg_session_expired, 0);
2691 		baid_data->mvm = mvm;
2692 		baid_data->tid = tid;
2693 		baid_data->sta_id = mvm_sta->sta_id;
2694 
2695 		mvm_sta->tid_to_baid[tid] = baid;
2696 		if (timeout)
2697 			mod_timer(&baid_data->session_timer,
2698 				  TU_TO_EXP_TIME(timeout * 2));
2699 
2700 		iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
2701 		/*
2702 		 * protect the BA data with RCU to cover a case where our
2703 		 * internal RX sync mechanism will timeout (not that it's
2704 		 * supposed to happen) and we will free the session data while
2705 		 * RX is being processed in parallel
2706 		 */
2707 		IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2708 			     mvm_sta->sta_id, tid, baid);
2709 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2710 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2711 	} else  {
2712 		u8 baid = mvm_sta->tid_to_baid[tid];
2713 
2714 		if (mvm->rx_ba_sessions > 0)
2715 			/* check that restart flow didn't zero the counter */
2716 			mvm->rx_ba_sessions--;
2717 		if (!iwl_mvm_has_new_rx_api(mvm))
2718 			return 0;
2719 
2720 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2721 			return -EINVAL;
2722 
2723 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2724 		if (WARN_ON(!baid_data))
2725 			return -EINVAL;
2726 
2727 		/* synchronize all rx queues so we can safely delete */
2728 		iwl_mvm_free_reorder(mvm, baid_data);
2729 		del_timer_sync(&baid_data->session_timer);
2730 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2731 		kfree_rcu(baid_data, rcu_head);
2732 		IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
2733 	}
2734 	return 0;
2735 
2736 out_free:
2737 	kfree(baid_data);
2738 	return ret;
2739 }
2740 
iwl_mvm_sta_tx_agg(struct iwl_mvm * mvm,struct ieee80211_sta * sta,int tid,u8 queue,bool start)2741 int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2742 		       int tid, u8 queue, bool start)
2743 {
2744 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2745 	struct iwl_mvm_add_sta_cmd cmd = {};
2746 	int ret;
2747 	u32 status;
2748 
2749 	lockdep_assert_held(&mvm->mutex);
2750 
2751 	if (start) {
2752 		mvm_sta->tfd_queue_msk |= BIT(queue);
2753 		mvm_sta->tid_disable_agg &= ~BIT(tid);
2754 	} else {
2755 		/* In DQA-mode the queue isn't removed on agg termination */
2756 		mvm_sta->tid_disable_agg |= BIT(tid);
2757 	}
2758 
2759 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2760 	cmd.sta_id = mvm_sta->sta_id;
2761 	cmd.add_modify = STA_MODE_MODIFY;
2762 	if (!iwl_mvm_has_new_tx_api(mvm))
2763 		cmd.modify_mask = STA_MODIFY_QUEUES;
2764 	cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2765 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2766 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2767 
2768 	status = ADD_STA_SUCCESS;
2769 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2770 					  iwl_mvm_add_sta_cmd_size(mvm),
2771 					  &cmd, &status);
2772 	if (ret)
2773 		return ret;
2774 
2775 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2776 	case ADD_STA_SUCCESS:
2777 		break;
2778 	default:
2779 		ret = -EIO;
2780 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2781 			start ? "start" : "stopp", status);
2782 		break;
2783 	}
2784 
2785 	return ret;
2786 }
2787 
2788 const u8 tid_to_mac80211_ac[] = {
2789 	IEEE80211_AC_BE,
2790 	IEEE80211_AC_BK,
2791 	IEEE80211_AC_BK,
2792 	IEEE80211_AC_BE,
2793 	IEEE80211_AC_VI,
2794 	IEEE80211_AC_VI,
2795 	IEEE80211_AC_VO,
2796 	IEEE80211_AC_VO,
2797 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2798 };
2799 
2800 static const u8 tid_to_ucode_ac[] = {
2801 	AC_BE,
2802 	AC_BK,
2803 	AC_BK,
2804 	AC_BE,
2805 	AC_VI,
2806 	AC_VI,
2807 	AC_VO,
2808 	AC_VO,
2809 };
2810 
iwl_mvm_sta_tx_agg_start(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 tid,u16 * ssn)2811 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2812 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2813 {
2814 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2815 	struct iwl_mvm_tid_data *tid_data;
2816 	u16 normalized_ssn;
2817 	u16 txq_id;
2818 	int ret;
2819 
2820 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2821 		return -EINVAL;
2822 
2823 	if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2824 	    mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2825 		IWL_ERR(mvm,
2826 			"Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2827 			mvmsta->tid_data[tid].state);
2828 		return -ENXIO;
2829 	}
2830 
2831 	lockdep_assert_held(&mvm->mutex);
2832 
2833 	if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2834 	    iwl_mvm_has_new_tx_api(mvm)) {
2835 		u8 ac = tid_to_mac80211_ac[tid];
2836 
2837 		ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2838 		if (ret)
2839 			return ret;
2840 	}
2841 
2842 	spin_lock_bh(&mvmsta->lock);
2843 
2844 	/*
2845 	 * Note the possible cases:
2846 	 *  1. An enabled TXQ - TXQ needs to become agg'ed
2847 	 *  2. The TXQ hasn't yet been enabled, so find a free one and mark
2848 	 *	it as reserved
2849 	 */
2850 	txq_id = mvmsta->tid_data[tid].txq_id;
2851 	if (txq_id == IWL_MVM_INVALID_QUEUE) {
2852 		ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2853 					      IWL_MVM_DQA_MIN_DATA_QUEUE,
2854 					      IWL_MVM_DQA_MAX_DATA_QUEUE);
2855 		if (ret < 0) {
2856 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
2857 			goto out;
2858 		}
2859 
2860 		txq_id = ret;
2861 
2862 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
2863 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2864 	} else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2865 		ret = -ENXIO;
2866 		IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2867 			tid, IWL_MAX_HW_QUEUES - 1);
2868 		goto out;
2869 
2870 	} else if (unlikely(mvm->queue_info[txq_id].status ==
2871 			    IWL_MVM_QUEUE_SHARED)) {
2872 		ret = -ENXIO;
2873 		IWL_DEBUG_TX_QUEUES(mvm,
2874 				    "Can't start tid %d agg on shared queue!\n",
2875 				    tid);
2876 		goto out;
2877 	}
2878 
2879 	IWL_DEBUG_TX_QUEUES(mvm,
2880 			    "AGG for tid %d will be on queue #%d\n",
2881 			    tid, txq_id);
2882 
2883 	tid_data = &mvmsta->tid_data[tid];
2884 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2885 	tid_data->txq_id = txq_id;
2886 	*ssn = tid_data->ssn;
2887 
2888 	IWL_DEBUG_TX_QUEUES(mvm,
2889 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2890 			    mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2891 			    tid_data->next_reclaimed);
2892 
2893 	/*
2894 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
2895 	 * to align the wrap around of ssn so we compare relevant values.
2896 	 */
2897 	normalized_ssn = tid_data->ssn;
2898 	if (mvm->trans->trans_cfg->gen2)
2899 		normalized_ssn &= 0xff;
2900 
2901 	if (normalized_ssn == tid_data->next_reclaimed) {
2902 		tid_data->state = IWL_AGG_STARTING;
2903 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
2904 	} else {
2905 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2906 		ret = IEEE80211_AMPDU_TX_START_DELAY_ADDBA;
2907 	}
2908 
2909 out:
2910 	spin_unlock_bh(&mvmsta->lock);
2911 
2912 	return ret;
2913 }
2914 
iwl_mvm_sta_tx_agg_oper(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 tid,u16 buf_size,bool amsdu)2915 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2916 			    struct ieee80211_sta *sta, u16 tid, u16 buf_size,
2917 			    bool amsdu)
2918 {
2919 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2920 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2921 	unsigned int wdg_timeout =
2922 		iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2923 	int queue, ret;
2924 	bool alloc_queue = true;
2925 	enum iwl_mvm_queue_status queue_status;
2926 	u16 ssn;
2927 
2928 	struct iwl_trans_txq_scd_cfg cfg = {
2929 		.sta_id = mvmsta->sta_id,
2930 		.tid = tid,
2931 		.frame_limit = buf_size,
2932 		.aggregate = true,
2933 	};
2934 
2935 	/*
2936 	 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
2937 	 * manager, so this function should never be called in this case.
2938 	 */
2939 	if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
2940 		return -EINVAL;
2941 
2942 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2943 		     != IWL_MAX_TID_COUNT);
2944 
2945 	spin_lock_bh(&mvmsta->lock);
2946 	ssn = tid_data->ssn;
2947 	queue = tid_data->txq_id;
2948 	tid_data->state = IWL_AGG_ON;
2949 	mvmsta->agg_tids |= BIT(tid);
2950 	tid_data->ssn = 0xffff;
2951 	tid_data->amsdu_in_ampdu_allowed = amsdu;
2952 	spin_unlock_bh(&mvmsta->lock);
2953 
2954 	if (iwl_mvm_has_new_tx_api(mvm)) {
2955 		/*
2956 		 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
2957 		 * would have failed, so if we are here there is no need to
2958 		 * allocate a queue.
2959 		 * However, if aggregation size is different than the default
2960 		 * size, the scheduler should be reconfigured.
2961 		 * We cannot do this with the new TX API, so return unsupported
2962 		 * for now, until it will be offloaded to firmware..
2963 		 * Note that if SCD default value changes - this condition
2964 		 * should be updated as well.
2965 		 */
2966 		if (buf_size < IWL_FRAME_LIMIT)
2967 			return -ENOTSUPP;
2968 
2969 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2970 		if (ret)
2971 			return -EIO;
2972 		goto out;
2973 	}
2974 
2975 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2976 
2977 	queue_status = mvm->queue_info[queue].status;
2978 
2979 	/* Maybe there is no need to even alloc a queue... */
2980 	if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2981 		alloc_queue = false;
2982 
2983 	/*
2984 	 * Only reconfig the SCD for the queue if the window size has
2985 	 * changed from current (become smaller)
2986 	 */
2987 	if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
2988 		/*
2989 		 * If reconfiguring an existing queue, it first must be
2990 		 * drained
2991 		 */
2992 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2993 						     BIT(queue));
2994 		if (ret) {
2995 			IWL_ERR(mvm,
2996 				"Error draining queue before reconfig\n");
2997 			return ret;
2998 		}
2999 
3000 		ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
3001 					   mvmsta->sta_id, tid,
3002 					   buf_size, ssn);
3003 		if (ret) {
3004 			IWL_ERR(mvm,
3005 				"Error reconfiguring TXQ #%d\n", queue);
3006 			return ret;
3007 		}
3008 	}
3009 
3010 	if (alloc_queue)
3011 		iwl_mvm_enable_txq(mvm, sta, queue, ssn,
3012 				   &cfg, wdg_timeout);
3013 
3014 	/* Send ADD_STA command to enable aggs only if the queue isn't shared */
3015 	if (queue_status != IWL_MVM_QUEUE_SHARED) {
3016 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
3017 		if (ret)
3018 			return -EIO;
3019 	}
3020 
3021 	/* No need to mark as reserved */
3022 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
3023 
3024 out:
3025 	/*
3026 	 * Even though in theory the peer could have different
3027 	 * aggregation reorder buffer sizes for different sessions,
3028 	 * our ucode doesn't allow for that and has a global limit
3029 	 * for each station. Therefore, use the minimum of all the
3030 	 * aggregation sessions and our default value.
3031 	 */
3032 	mvmsta->max_agg_bufsize =
3033 		min(mvmsta->max_agg_bufsize, buf_size);
3034 	mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
3035 
3036 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
3037 		     sta->addr, tid);
3038 
3039 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq);
3040 }
3041 
iwl_mvm_unreserve_agg_queue(struct iwl_mvm * mvm,struct iwl_mvm_sta * mvmsta,struct iwl_mvm_tid_data * tid_data)3042 static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
3043 					struct iwl_mvm_sta *mvmsta,
3044 					struct iwl_mvm_tid_data *tid_data)
3045 {
3046 	u16 txq_id = tid_data->txq_id;
3047 
3048 	lockdep_assert_held(&mvm->mutex);
3049 
3050 	if (iwl_mvm_has_new_tx_api(mvm))
3051 		return;
3052 
3053 	/*
3054 	 * The TXQ is marked as reserved only if no traffic came through yet
3055 	 * This means no traffic has been sent on this TID (agg'd or not), so
3056 	 * we no longer have use for the queue. Since it hasn't even been
3057 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
3058 	 * free.
3059 	 */
3060 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
3061 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
3062 		tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
3063 	}
3064 }
3065 
iwl_mvm_sta_tx_agg_stop(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 tid)3066 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3067 			    struct ieee80211_sta *sta, u16 tid)
3068 {
3069 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3070 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3071 	u16 txq_id;
3072 	int err;
3073 
3074 	/*
3075 	 * If mac80211 is cleaning its state, then say that we finished since
3076 	 * our state has been cleared anyway.
3077 	 */
3078 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3079 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3080 		return 0;
3081 	}
3082 
3083 	spin_lock_bh(&mvmsta->lock);
3084 
3085 	txq_id = tid_data->txq_id;
3086 
3087 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3088 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3089 
3090 	mvmsta->agg_tids &= ~BIT(tid);
3091 
3092 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3093 
3094 	switch (tid_data->state) {
3095 	case IWL_AGG_ON:
3096 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3097 
3098 		IWL_DEBUG_TX_QUEUES(mvm,
3099 				    "ssn = %d, next_recl = %d\n",
3100 				    tid_data->ssn, tid_data->next_reclaimed);
3101 
3102 		tid_data->ssn = 0xffff;
3103 		tid_data->state = IWL_AGG_OFF;
3104 		spin_unlock_bh(&mvmsta->lock);
3105 
3106 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3107 
3108 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3109 		return 0;
3110 	case IWL_AGG_STARTING:
3111 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
3112 		/*
3113 		 * The agg session has been stopped before it was set up. This
3114 		 * can happen when the AddBA timer times out for example.
3115 		 */
3116 
3117 		/* No barriers since we are under mutex */
3118 		lockdep_assert_held(&mvm->mutex);
3119 
3120 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3121 		tid_data->state = IWL_AGG_OFF;
3122 		err = 0;
3123 		break;
3124 	default:
3125 		IWL_ERR(mvm,
3126 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3127 			mvmsta->sta_id, tid, tid_data->state);
3128 		IWL_ERR(mvm,
3129 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
3130 		err = -EINVAL;
3131 	}
3132 
3133 	spin_unlock_bh(&mvmsta->lock);
3134 
3135 	return err;
3136 }
3137 
iwl_mvm_sta_tx_agg_flush(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 tid)3138 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3139 			    struct ieee80211_sta *sta, u16 tid)
3140 {
3141 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3142 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3143 	u16 txq_id;
3144 	enum iwl_mvm_agg_state old_state;
3145 
3146 	/*
3147 	 * First set the agg state to OFF to avoid calling
3148 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3149 	 */
3150 	spin_lock_bh(&mvmsta->lock);
3151 	txq_id = tid_data->txq_id;
3152 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3153 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3154 	old_state = tid_data->state;
3155 	tid_data->state = IWL_AGG_OFF;
3156 	mvmsta->agg_tids &= ~BIT(tid);
3157 	spin_unlock_bh(&mvmsta->lock);
3158 
3159 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3160 
3161 	if (old_state >= IWL_AGG_ON) {
3162 		iwl_mvm_drain_sta(mvm, mvmsta, true);
3163 
3164 		if (iwl_mvm_has_new_tx_api(mvm)) {
3165 			if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3166 						   BIT(tid), 0))
3167 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3168 			iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3169 		} else {
3170 			if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
3171 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3172 			iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3173 		}
3174 
3175 		iwl_mvm_drain_sta(mvm, mvmsta, false);
3176 
3177 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3178 	}
3179 
3180 	return 0;
3181 }
3182 
iwl_mvm_set_fw_key_idx(struct iwl_mvm * mvm)3183 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3184 {
3185 	int i, max = -1, max_offs = -1;
3186 
3187 	lockdep_assert_held(&mvm->mutex);
3188 
3189 	/* Pick the unused key offset with the highest 'deleted'
3190 	 * counter. Every time a key is deleted, all the counters
3191 	 * are incremented and the one that was just deleted is
3192 	 * reset to zero. Thus, the highest counter is the one
3193 	 * that was deleted longest ago. Pick that one.
3194 	 */
3195 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3196 		if (test_bit(i, mvm->fw_key_table))
3197 			continue;
3198 		if (mvm->fw_key_deleted[i] > max) {
3199 			max = mvm->fw_key_deleted[i];
3200 			max_offs = i;
3201 		}
3202 	}
3203 
3204 	if (max_offs < 0)
3205 		return STA_KEY_IDX_INVALID;
3206 
3207 	return max_offs;
3208 }
3209 
iwl_mvm_get_key_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3210 static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
3211 					       struct ieee80211_vif *vif,
3212 					       struct ieee80211_sta *sta)
3213 {
3214 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3215 
3216 	if (sta)
3217 		return iwl_mvm_sta_from_mac80211(sta);
3218 
3219 	/*
3220 	 * The device expects GTKs for station interfaces to be
3221 	 * installed as GTKs for the AP station. If we have no
3222 	 * station ID, then use AP's station ID.
3223 	 */
3224 	if (vif->type == NL80211_IFTYPE_STATION &&
3225 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3226 		u8 sta_id = mvmvif->ap_sta_id;
3227 
3228 		sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
3229 					    lockdep_is_held(&mvm->mutex));
3230 
3231 		/*
3232 		 * It is possible that the 'sta' parameter is NULL,
3233 		 * for example when a GTK is removed - the sta_id will then
3234 		 * be the AP ID, and no station was passed by mac80211.
3235 		 */
3236 		if (IS_ERR_OR_NULL(sta))
3237 			return NULL;
3238 
3239 		return iwl_mvm_sta_from_mac80211(sta);
3240 	}
3241 
3242 	return NULL;
3243 }
3244 
iwl_mvm_send_sta_key(struct iwl_mvm * mvm,u32 sta_id,struct ieee80211_key_conf * key,bool mcast,u32 tkip_iv32,u16 * tkip_p1k,u32 cmd_flags,u8 key_offset,bool mfp)3245 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
3246 				u32 sta_id,
3247 				struct ieee80211_key_conf *key, bool mcast,
3248 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
3249 				u8 key_offset, bool mfp)
3250 {
3251 	union {
3252 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3253 		struct iwl_mvm_add_sta_key_cmd cmd;
3254 	} u = {};
3255 	__le16 key_flags;
3256 	int ret;
3257 	u32 status;
3258 	u16 keyidx;
3259 	u64 pn = 0;
3260 	int i, size;
3261 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3262 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3263 
3264 	if (sta_id == IWL_MVM_INVALID_STA)
3265 		return -EINVAL;
3266 
3267 	keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3268 		 STA_KEY_FLG_KEYID_MSK;
3269 	key_flags = cpu_to_le16(keyidx);
3270 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3271 
3272 	switch (key->cipher) {
3273 	case WLAN_CIPHER_SUITE_TKIP:
3274 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3275 		if (new_api) {
3276 			memcpy((void *)&u.cmd.tx_mic_key,
3277 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
3278 			       IWL_MIC_KEY_SIZE);
3279 
3280 			memcpy((void *)&u.cmd.rx_mic_key,
3281 			       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3282 			       IWL_MIC_KEY_SIZE);
3283 			pn = atomic64_read(&key->tx_pn);
3284 
3285 		} else {
3286 			u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3287 			for (i = 0; i < 5; i++)
3288 				u.cmd_v1.tkip_rx_ttak[i] =
3289 					cpu_to_le16(tkip_p1k[i]);
3290 		}
3291 		memcpy(u.cmd.common.key, key->key, key->keylen);
3292 		break;
3293 	case WLAN_CIPHER_SUITE_CCMP:
3294 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
3295 		memcpy(u.cmd.common.key, key->key, key->keylen);
3296 		if (new_api)
3297 			pn = atomic64_read(&key->tx_pn);
3298 		break;
3299 	case WLAN_CIPHER_SUITE_WEP104:
3300 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3301 		/* fall through */
3302 	case WLAN_CIPHER_SUITE_WEP40:
3303 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
3304 		memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3305 		break;
3306 	case WLAN_CIPHER_SUITE_GCMP_256:
3307 		key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3308 		/* fall through */
3309 	case WLAN_CIPHER_SUITE_GCMP:
3310 		key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
3311 		memcpy(u.cmd.common.key, key->key, key->keylen);
3312 		if (new_api)
3313 			pn = atomic64_read(&key->tx_pn);
3314 		break;
3315 	default:
3316 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
3317 		memcpy(u.cmd.common.key, key->key, key->keylen);
3318 	}
3319 
3320 	if (mcast)
3321 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3322 	if (mfp)
3323 		key_flags |= cpu_to_le16(STA_KEY_MFP);
3324 
3325 	u.cmd.common.key_offset = key_offset;
3326 	u.cmd.common.key_flags = key_flags;
3327 	u.cmd.common.sta_id = sta_id;
3328 
3329 	if (new_api) {
3330 		u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3331 		size = sizeof(u.cmd);
3332 	} else {
3333 		size = sizeof(u.cmd_v1);
3334 	}
3335 
3336 	status = ADD_STA_SUCCESS;
3337 	if (cmd_flags & CMD_ASYNC)
3338 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3339 					   &u.cmd);
3340 	else
3341 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3342 						  &u.cmd, &status);
3343 
3344 	switch (status) {
3345 	case ADD_STA_SUCCESS:
3346 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3347 		break;
3348 	default:
3349 		ret = -EIO;
3350 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3351 		break;
3352 	}
3353 
3354 	return ret;
3355 }
3356 
iwl_mvm_send_sta_igtk(struct iwl_mvm * mvm,struct ieee80211_key_conf * keyconf,u8 sta_id,bool remove_key)3357 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3358 				 struct ieee80211_key_conf *keyconf,
3359 				 u8 sta_id, bool remove_key)
3360 {
3361 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3362 
3363 	/* verify the key details match the required command's expectations */
3364 	if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3365 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
3366 		    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3367 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3368 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3369 		return -EINVAL;
3370 
3371 	if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3372 		    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3373 		return -EINVAL;
3374 
3375 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3376 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
3377 
3378 	if (remove_key) {
3379 		/* This is a valid situation for IGTK */
3380 		if (sta_id == IWL_MVM_INVALID_STA)
3381 			return 0;
3382 
3383 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3384 	} else {
3385 		struct ieee80211_key_seq seq;
3386 		const u8 *pn;
3387 
3388 		switch (keyconf->cipher) {
3389 		case WLAN_CIPHER_SUITE_AES_CMAC:
3390 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3391 			break;
3392 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3393 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3394 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3395 			break;
3396 		default:
3397 			return -EINVAL;
3398 		}
3399 
3400 		memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3401 		if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3402 			igtk_cmd.ctrl_flags |=
3403 				cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3404 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3405 		pn = seq.aes_cmac.pn;
3406 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3407 						       ((u64) pn[4] << 8) |
3408 						       ((u64) pn[3] << 16) |
3409 						       ((u64) pn[2] << 24) |
3410 						       ((u64) pn[1] << 32) |
3411 						       ((u64) pn[0] << 40));
3412 	}
3413 
3414 	IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3415 		       remove_key ? "removing" : "installing",
3416 		       igtk_cmd.sta_id);
3417 
3418 	if (!iwl_mvm_has_new_rx_api(mvm)) {
3419 		struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3420 			.ctrl_flags = igtk_cmd.ctrl_flags,
3421 			.key_id = igtk_cmd.key_id,
3422 			.sta_id = igtk_cmd.sta_id,
3423 			.receive_seq_cnt = igtk_cmd.receive_seq_cnt
3424 		};
3425 
3426 		memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3427 		       ARRAY_SIZE(igtk_cmd_v1.igtk));
3428 		return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3429 					    sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3430 	}
3431 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3432 				    sizeof(igtk_cmd), &igtk_cmd);
3433 }
3434 
3435 
iwl_mvm_get_mac_addr(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3436 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3437 				       struct ieee80211_vif *vif,
3438 				       struct ieee80211_sta *sta)
3439 {
3440 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3441 
3442 	if (sta)
3443 		return sta->addr;
3444 
3445 	if (vif->type == NL80211_IFTYPE_STATION &&
3446 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3447 		u8 sta_id = mvmvif->ap_sta_id;
3448 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3449 						lockdep_is_held(&mvm->mutex));
3450 		return sta->addr;
3451 	}
3452 
3453 
3454 	return NULL;
3455 }
3456 
__iwl_mvm_set_sta_key(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * keyconf,u8 key_offset,bool mcast)3457 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3458 				 struct ieee80211_vif *vif,
3459 				 struct ieee80211_sta *sta,
3460 				 struct ieee80211_key_conf *keyconf,
3461 				 u8 key_offset,
3462 				 bool mcast)
3463 {
3464 	int ret;
3465 	const u8 *addr;
3466 	struct ieee80211_key_seq seq;
3467 	u16 p1k[5];
3468 	u32 sta_id;
3469 	bool mfp = false;
3470 
3471 	if (sta) {
3472 		struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3473 
3474 		sta_id = mvm_sta->sta_id;
3475 		mfp = sta->mfp;
3476 	} else if (vif->type == NL80211_IFTYPE_AP &&
3477 		   !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3478 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3479 
3480 		sta_id = mvmvif->mcast_sta.sta_id;
3481 	} else {
3482 		IWL_ERR(mvm, "Failed to find station id\n");
3483 		return -EINVAL;
3484 	}
3485 
3486 	switch (keyconf->cipher) {
3487 	case WLAN_CIPHER_SUITE_TKIP:
3488 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3489 		/* get phase 1 key from mac80211 */
3490 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3491 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
3492 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3493 					   seq.tkip.iv32, p1k, 0, key_offset,
3494 					   mfp);
3495 		break;
3496 	case WLAN_CIPHER_SUITE_CCMP:
3497 	case WLAN_CIPHER_SUITE_WEP40:
3498 	case WLAN_CIPHER_SUITE_WEP104:
3499 	case WLAN_CIPHER_SUITE_GCMP:
3500 	case WLAN_CIPHER_SUITE_GCMP_256:
3501 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3502 					   0, NULL, 0, key_offset, mfp);
3503 		break;
3504 	default:
3505 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3506 					   0, NULL, 0, key_offset, mfp);
3507 	}
3508 
3509 	return ret;
3510 }
3511 
iwl_mvm_set_sta_key(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * keyconf,u8 key_offset)3512 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3513 			struct ieee80211_vif *vif,
3514 			struct ieee80211_sta *sta,
3515 			struct ieee80211_key_conf *keyconf,
3516 			u8 key_offset)
3517 {
3518 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3519 	struct iwl_mvm_sta *mvm_sta;
3520 	u8 sta_id = IWL_MVM_INVALID_STA;
3521 	int ret;
3522 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3523 
3524 	lockdep_assert_held(&mvm->mutex);
3525 
3526 	if (vif->type != NL80211_IFTYPE_AP ||
3527 	    keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3528 		/* Get the station id from the mvm local station table */
3529 		mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3530 		if (!mvm_sta) {
3531 			IWL_ERR(mvm, "Failed to find station\n");
3532 			return -EINVAL;
3533 		}
3534 		sta_id = mvm_sta->sta_id;
3535 
3536 		/*
3537 		 * It is possible that the 'sta' parameter is NULL, and thus
3538 		 * there is a need to retrieve the sta from the local station
3539 		 * table.
3540 		 */
3541 		if (!sta) {
3542 			sta = rcu_dereference_protected(
3543 				mvm->fw_id_to_mac_id[sta_id],
3544 				lockdep_is_held(&mvm->mutex));
3545 			if (IS_ERR_OR_NULL(sta)) {
3546 				IWL_ERR(mvm, "Invalid station id\n");
3547 				return -EINVAL;
3548 			}
3549 		}
3550 
3551 		if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3552 			return -EINVAL;
3553 	} else {
3554 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3555 
3556 		sta_id = mvmvif->mcast_sta.sta_id;
3557 	}
3558 
3559 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3560 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3561 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3562 		ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3563 		goto end;
3564 	}
3565 
3566 	/* If the key_offset is not pre-assigned, we need to find a
3567 	 * new offset to use.  In normal cases, the offset is not
3568 	 * pre-assigned, but during HW_RESTART we want to reuse the
3569 	 * same indices, so we pass them when this function is called.
3570 	 *
3571 	 * In D3 entry, we need to hardcoded the indices (because the
3572 	 * firmware hardcodes the PTK offset to 0).  In this case, we
3573 	 * need to make sure we don't overwrite the hw_key_idx in the
3574 	 * keyconf structure, because otherwise we cannot configure
3575 	 * the original ones back when resuming.
3576 	 */
3577 	if (key_offset == STA_KEY_IDX_INVALID) {
3578 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
3579 		if (key_offset == STA_KEY_IDX_INVALID)
3580 			return -ENOSPC;
3581 		keyconf->hw_key_idx = key_offset;
3582 	}
3583 
3584 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
3585 	if (ret)
3586 		goto end;
3587 
3588 	/*
3589 	 * For WEP, the same key is used for multicast and unicast. Upload it
3590 	 * again, using the same key offset, and now pointing the other one
3591 	 * to the same key slot (offset).
3592 	 * If this fails, remove the original as well.
3593 	 */
3594 	if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3595 	     keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3596 	    sta) {
3597 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3598 					    key_offset, !mcast);
3599 		if (ret) {
3600 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3601 			goto end;
3602 		}
3603 	}
3604 
3605 	__set_bit(key_offset, mvm->fw_key_table);
3606 
3607 end:
3608 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3609 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3610 		      sta ? sta->addr : zero_addr, ret);
3611 	return ret;
3612 }
3613 
iwl_mvm_remove_sta_key(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * keyconf)3614 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3615 			   struct ieee80211_vif *vif,
3616 			   struct ieee80211_sta *sta,
3617 			   struct ieee80211_key_conf *keyconf)
3618 {
3619 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3620 	struct iwl_mvm_sta *mvm_sta;
3621 	u8 sta_id = IWL_MVM_INVALID_STA;
3622 	int ret, i;
3623 
3624 	lockdep_assert_held(&mvm->mutex);
3625 
3626 	/* Get the station from the mvm local station table */
3627 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3628 	if (mvm_sta)
3629 		sta_id = mvm_sta->sta_id;
3630 	else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3631 		sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3632 
3633 
3634 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3635 		      keyconf->keyidx, sta_id);
3636 
3637 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3638 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3639 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3640 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3641 
3642 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3643 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3644 			keyconf->hw_key_idx);
3645 		return -ENOENT;
3646 	}
3647 
3648 	/* track which key was deleted last */
3649 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3650 		if (mvm->fw_key_deleted[i] < U8_MAX)
3651 			mvm->fw_key_deleted[i]++;
3652 	}
3653 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3654 
3655 	if (sta && !mvm_sta) {
3656 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3657 		return 0;
3658 	}
3659 
3660 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3661 	if (ret)
3662 		return ret;
3663 
3664 	/* delete WEP key twice to get rid of (now useless) offset */
3665 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3666 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3667 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3668 
3669 	return ret;
3670 }
3671 
iwl_mvm_update_tkip_key(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_key_conf * keyconf,struct ieee80211_sta * sta,u32 iv32,u16 * phase1key)3672 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3673 			     struct ieee80211_vif *vif,
3674 			     struct ieee80211_key_conf *keyconf,
3675 			     struct ieee80211_sta *sta, u32 iv32,
3676 			     u16 *phase1key)
3677 {
3678 	struct iwl_mvm_sta *mvm_sta;
3679 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3680 	bool mfp = sta ? sta->mfp : false;
3681 
3682 	rcu_read_lock();
3683 
3684 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3685 	if (WARN_ON_ONCE(!mvm_sta))
3686 		goto unlock;
3687 	iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
3688 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
3689 			     mfp);
3690 
3691  unlock:
3692 	rcu_read_unlock();
3693 }
3694 
iwl_mvm_sta_modify_ps_wake(struct iwl_mvm * mvm,struct ieee80211_sta * sta)3695 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3696 				struct ieee80211_sta *sta)
3697 {
3698 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3699 	struct iwl_mvm_add_sta_cmd cmd = {
3700 		.add_modify = STA_MODE_MODIFY,
3701 		.sta_id = mvmsta->sta_id,
3702 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
3703 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3704 	};
3705 	int ret;
3706 
3707 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3708 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3709 	if (ret)
3710 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3711 }
3712 
iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm * mvm,struct ieee80211_sta * sta,enum ieee80211_frame_release_type reason,u16 cnt,u16 tids,bool more_data,bool single_sta_queue)3713 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3714 				       struct ieee80211_sta *sta,
3715 				       enum ieee80211_frame_release_type reason,
3716 				       u16 cnt, u16 tids, bool more_data,
3717 				       bool single_sta_queue)
3718 {
3719 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3720 	struct iwl_mvm_add_sta_cmd cmd = {
3721 		.add_modify = STA_MODE_MODIFY,
3722 		.sta_id = mvmsta->sta_id,
3723 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3724 		.sleep_tx_count = cpu_to_le16(cnt),
3725 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3726 	};
3727 	int tid, ret;
3728 	unsigned long _tids = tids;
3729 
3730 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
3731 	 * Note that this field is reserved and unused by firmware not
3732 	 * supporting GO uAPSD, so it's safe to always do this.
3733 	 */
3734 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3735 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3736 
3737 	/* If we're releasing frames from aggregation or dqa queues then check
3738 	 * if all the queues that we're releasing frames from, combined, have:
3739 	 *  - more frames than the service period, in which case more_data
3740 	 *    needs to be set
3741 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
3742 	 *    firmware command (but do that unconditionally)
3743 	 */
3744 	if (single_sta_queue) {
3745 		int remaining = cnt;
3746 		int sleep_tx_count;
3747 
3748 		spin_lock_bh(&mvmsta->lock);
3749 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3750 			struct iwl_mvm_tid_data *tid_data;
3751 			u16 n_queued;
3752 
3753 			tid_data = &mvmsta->tid_data[tid];
3754 
3755 			n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3756 			if (n_queued > remaining) {
3757 				more_data = true;
3758 				remaining = 0;
3759 				break;
3760 			}
3761 			remaining -= n_queued;
3762 		}
3763 		sleep_tx_count = cnt - remaining;
3764 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3765 			mvmsta->sleep_tx_count = sleep_tx_count;
3766 		spin_unlock_bh(&mvmsta->lock);
3767 
3768 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3769 		if (WARN_ON(cnt - remaining == 0)) {
3770 			ieee80211_sta_eosp(sta);
3771 			return;
3772 		}
3773 	}
3774 
3775 	/* Note: this is ignored by firmware not supporting GO uAPSD */
3776 	if (more_data)
3777 		cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3778 
3779 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3780 		mvmsta->next_status_eosp = true;
3781 		cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3782 	} else {
3783 		cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3784 	}
3785 
3786 	/* block the Tx queues until the FW updated the sleep Tx count */
3787 	iwl_trans_block_txq_ptrs(mvm->trans, true);
3788 
3789 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3790 				   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3791 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3792 	if (ret)
3793 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3794 }
3795 
iwl_mvm_rx_eosp_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)3796 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3797 			   struct iwl_rx_cmd_buffer *rxb)
3798 {
3799 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3800 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3801 	struct ieee80211_sta *sta;
3802 	u32 sta_id = le32_to_cpu(notif->sta_id);
3803 
3804 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
3805 		return;
3806 
3807 	rcu_read_lock();
3808 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3809 	if (!IS_ERR_OR_NULL(sta))
3810 		ieee80211_sta_eosp(sta);
3811 	rcu_read_unlock();
3812 }
3813 
iwl_mvm_sta_modify_disable_tx(struct iwl_mvm * mvm,struct iwl_mvm_sta * mvmsta,bool disable)3814 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3815 				   struct iwl_mvm_sta *mvmsta, bool disable)
3816 {
3817 	struct iwl_mvm_add_sta_cmd cmd = {
3818 		.add_modify = STA_MODE_MODIFY,
3819 		.sta_id = mvmsta->sta_id,
3820 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3821 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3822 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3823 	};
3824 	int ret;
3825 
3826 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3827 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3828 	if (ret)
3829 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3830 }
3831 
iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm * mvm,struct ieee80211_sta * sta,bool disable)3832 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3833 				      struct ieee80211_sta *sta,
3834 				      bool disable)
3835 {
3836 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3837 
3838 	spin_lock_bh(&mvm_sta->lock);
3839 
3840 	if (mvm_sta->disable_tx == disable) {
3841 		spin_unlock_bh(&mvm_sta->lock);
3842 		return;
3843 	}
3844 
3845 	mvm_sta->disable_tx = disable;
3846 
3847 	/* Tell mac80211 to start/stop queuing tx for this station */
3848 	ieee80211_sta_block_awake(mvm->hw, sta, disable);
3849 
3850 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3851 
3852 	spin_unlock_bh(&mvm_sta->lock);
3853 }
3854 
iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm * mvm,struct iwl_mvm_vif * mvmvif,struct iwl_mvm_int_sta * sta,bool disable)3855 static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3856 					      struct iwl_mvm_vif *mvmvif,
3857 					      struct iwl_mvm_int_sta *sta,
3858 					      bool disable)
3859 {
3860 	u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3861 	struct iwl_mvm_add_sta_cmd cmd = {
3862 		.add_modify = STA_MODE_MODIFY,
3863 		.sta_id = sta->sta_id,
3864 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3865 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3866 		.mac_id_n_color = cpu_to_le32(id),
3867 	};
3868 	int ret;
3869 
3870 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3871 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3872 	if (ret)
3873 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3874 }
3875 
iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm * mvm,struct iwl_mvm_vif * mvmvif,bool disable)3876 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3877 				       struct iwl_mvm_vif *mvmvif,
3878 				       bool disable)
3879 {
3880 	struct ieee80211_sta *sta;
3881 	struct iwl_mvm_sta *mvm_sta;
3882 	int i;
3883 
3884 	lockdep_assert_held(&mvm->mutex);
3885 
3886 	/* Block/unblock all the stations of the given mvmvif */
3887 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
3888 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3889 						lockdep_is_held(&mvm->mutex));
3890 		if (IS_ERR_OR_NULL(sta))
3891 			continue;
3892 
3893 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3894 		if (mvm_sta->mac_id_n_color !=
3895 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3896 			continue;
3897 
3898 		iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3899 	}
3900 
3901 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3902 		return;
3903 
3904 	/* Need to block/unblock also multicast station */
3905 	if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3906 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3907 						  &mvmvif->mcast_sta, disable);
3908 
3909 	/*
3910 	 * Only unblock the broadcast station (FW blocks it for immediate
3911 	 * quiet, not the driver)
3912 	 */
3913 	if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3914 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3915 						  &mvmvif->bcast_sta, disable);
3916 }
3917 
iwl_mvm_csa_client_absent(struct iwl_mvm * mvm,struct ieee80211_vif * vif)3918 void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3919 {
3920 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3921 	struct iwl_mvm_sta *mvmsta;
3922 
3923 	rcu_read_lock();
3924 
3925 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3926 
3927 	if (!WARN_ON(!mvmsta))
3928 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3929 
3930 	rcu_read_unlock();
3931 }
3932 
iwl_mvm_tid_queued(struct iwl_mvm * mvm,struct iwl_mvm_tid_data * tid_data)3933 u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3934 {
3935 	u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3936 
3937 	/*
3938 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3939 	 * to align the wrap around of ssn so we compare relevant values.
3940 	 */
3941 	if (mvm->trans->trans_cfg->gen2)
3942 		sn &= 0xff;
3943 
3944 	return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
3945 }
3946 
iwl_mvm_add_pasn_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mvm_int_sta * sta,u8 * addr,u32 cipher,u8 * key,u32 key_len)3947 int iwl_mvm_add_pasn_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3948 			 struct iwl_mvm_int_sta *sta, u8 *addr, u32 cipher,
3949 			 u8 *key, u32 key_len)
3950 {
3951 	int ret;
3952 	u16 queue;
3953 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3954 	struct ieee80211_key_conf *keyconf;
3955 
3956 	ret = iwl_mvm_allocate_int_sta(mvm, sta, 0,
3957 				       NL80211_IFTYPE_UNSPECIFIED,
3958 				       IWL_STA_LINK);
3959 	if (ret)
3960 		return ret;
3961 
3962 	ret = iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
3963 					     addr, sta, &queue,
3964 					     IWL_MVM_TX_FIFO_BE);
3965 	if (ret)
3966 		goto out;
3967 
3968 	keyconf = kzalloc(sizeof(*keyconf) + key_len, GFP_KERNEL);
3969 	if (!keyconf) {
3970 		ret = -ENOBUFS;
3971 		goto out;
3972 	}
3973 
3974 	keyconf->cipher = cipher;
3975 	memcpy(keyconf->key, key, key_len);
3976 	keyconf->keylen = key_len;
3977 
3978 	ret = iwl_mvm_send_sta_key(mvm, sta->sta_id, keyconf, false,
3979 				   0, NULL, 0, 0, true);
3980 	kfree(keyconf);
3981 	return 0;
3982 out:
3983 	iwl_mvm_dealloc_int_sta(mvm, sta);
3984 	return ret;
3985 }
3986