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