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