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