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