xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/sta.c (revision 586c8e32330591693c5fca4e089d90340b313f5d)
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, status %#x\n",
1738 			mvmsta->sta_id, status);
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 		mvm_sta->dup_data = NULL;
1841 	}
1842 
1843 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1844 	if (ret)
1845 		return ret;
1846 
1847 	/* flush its queues here since we are freeing mvm_sta */
1848 	ret = iwl_mvm_flush_sta(mvm, mvm_sta, false);
1849 	if (ret)
1850 		return ret;
1851 	if (iwl_mvm_has_new_tx_api(mvm)) {
1852 		ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1853 	} else {
1854 		u32 q_mask = mvm_sta->tfd_queue_msk;
1855 
1856 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1857 						     q_mask);
1858 	}
1859 	if (ret)
1860 		return ret;
1861 
1862 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1863 
1864 	iwl_mvm_disable_sta_queues(mvm, vif, sta);
1865 
1866 	/* If there is a TXQ still marked as reserved - free it */
1867 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1868 		u8 reserved_txq = mvm_sta->reserved_queue;
1869 		enum iwl_mvm_queue_status *status;
1870 
1871 		/*
1872 		 * If no traffic has gone through the reserved TXQ - it
1873 		 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1874 		 * should be manually marked as free again
1875 		 */
1876 		status = &mvm->queue_info[reserved_txq].status;
1877 		if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1878 			 (*status != IWL_MVM_QUEUE_FREE),
1879 			 "sta_id %d reserved txq %d status %d",
1880 			 sta_id, reserved_txq, *status))
1881 			return -EINVAL;
1882 
1883 		*status = IWL_MVM_QUEUE_FREE;
1884 	}
1885 
1886 	if (vif->type == NL80211_IFTYPE_STATION &&
1887 	    mvmvif->ap_sta_id == sta_id) {
1888 		/* if associated - we can't remove the AP STA now */
1889 		if (vif->bss_conf.assoc)
1890 			return ret;
1891 
1892 		/* unassoc - go ahead - remove the AP STA now */
1893 		mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1894 	}
1895 
1896 	/*
1897 	 * This shouldn't happen - the TDLS channel switch should be canceled
1898 	 * before the STA is removed.
1899 	 */
1900 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
1901 		mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1902 		cancel_delayed_work(&mvm->tdls_cs.dwork);
1903 	}
1904 
1905 	/*
1906 	 * Make sure that the tx response code sees the station as -EBUSY and
1907 	 * calls the drain worker.
1908 	 */
1909 	spin_lock_bh(&mvm_sta->lock);
1910 	spin_unlock_bh(&mvm_sta->lock);
1911 
1912 	ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1913 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1914 
1915 	return ret;
1916 }
1917 
1918 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1919 		      struct ieee80211_vif *vif,
1920 		      u8 sta_id)
1921 {
1922 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1923 
1924 	lockdep_assert_held(&mvm->mutex);
1925 
1926 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1927 	return ret;
1928 }
1929 
1930 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1931 			     struct iwl_mvm_int_sta *sta,
1932 			     u32 qmask, enum nl80211_iftype iftype,
1933 			     enum iwl_sta_type type)
1934 {
1935 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1936 	    sta->sta_id == IWL_MVM_INVALID_STA) {
1937 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
1938 		if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
1939 			return -ENOSPC;
1940 	}
1941 
1942 	sta->tfd_queue_msk = qmask;
1943 	sta->type = type;
1944 
1945 	/* put a non-NULL value so iterating over the stations won't stop */
1946 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1947 	return 0;
1948 }
1949 
1950 void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
1951 {
1952 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1953 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1954 	sta->sta_id = IWL_MVM_INVALID_STA;
1955 }
1956 
1957 static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue,
1958 					  u8 sta_id, u8 fifo)
1959 {
1960 	unsigned int wdg_timeout =
1961 		mvm->trans->trans_cfg->base_params->wd_timeout;
1962 	struct iwl_trans_txq_scd_cfg cfg = {
1963 		.fifo = fifo,
1964 		.sta_id = sta_id,
1965 		.tid = IWL_MAX_TID_COUNT,
1966 		.aggregate = false,
1967 		.frame_limit = IWL_FRAME_LIMIT,
1968 	};
1969 
1970 	WARN_ON(iwl_mvm_has_new_tx_api(mvm));
1971 
1972 	iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
1973 }
1974 
1975 static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id)
1976 {
1977 	unsigned int wdg_timeout =
1978 		mvm->trans->trans_cfg->base_params->wd_timeout;
1979 
1980 	WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
1981 
1982 	return iwl_mvm_tvqm_enable_txq(mvm, sta_id, IWL_MAX_TID_COUNT,
1983 				       wdg_timeout);
1984 }
1985 
1986 static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx,
1987 					  int maccolor, u8 *addr,
1988 					  struct iwl_mvm_int_sta *sta,
1989 					  u16 *queue, int fifo)
1990 {
1991 	int ret;
1992 
1993 	/* Map queue to fifo - needs to happen before adding station */
1994 	if (!iwl_mvm_has_new_tx_api(mvm))
1995 		iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo);
1996 
1997 	ret = iwl_mvm_add_int_sta_common(mvm, sta, addr, macidx, maccolor);
1998 	if (ret) {
1999 		if (!iwl_mvm_has_new_tx_api(mvm))
2000 			iwl_mvm_disable_txq(mvm, NULL, queue,
2001 					    IWL_MAX_TID_COUNT, 0);
2002 		return ret;
2003 	}
2004 
2005 	/*
2006 	 * For 22000 firmware and on we cannot add queue to a station unknown
2007 	 * to firmware so enable queue here - after the station was added
2008 	 */
2009 	if (iwl_mvm_has_new_tx_api(mvm)) {
2010 		int txq;
2011 
2012 		txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id);
2013 		if (txq < 0) {
2014 			iwl_mvm_rm_sta_common(mvm, sta->sta_id);
2015 			return txq;
2016 		}
2017 
2018 		*queue = txq;
2019 	}
2020 
2021 	return 0;
2022 }
2023 
2024 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm, u32 lmac_id)
2025 {
2026 	int ret;
2027 
2028 	lockdep_assert_held(&mvm->mutex);
2029 
2030 	/* Allocate aux station and assign to it the aux queue */
2031 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
2032 				       NL80211_IFTYPE_UNSPECIFIED,
2033 				       IWL_STA_AUX_ACTIVITY);
2034 	if (ret)
2035 		return ret;
2036 
2037 	/*
2038 	 * In CDB NICs we need to specify which lmac to use for aux activity
2039 	 * using the mac_id argument place to send lmac_id to the function
2040 	 */
2041 	ret = iwl_mvm_add_int_sta_with_queue(mvm, lmac_id, 0, NULL,
2042 					     &mvm->aux_sta, &mvm->aux_queue,
2043 					     IWL_MVM_TX_FIFO_MCAST);
2044 	if (ret) {
2045 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2046 		return ret;
2047 	}
2048 
2049 	return 0;
2050 }
2051 
2052 int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2053 {
2054 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2055 
2056 	lockdep_assert_held(&mvm->mutex);
2057 
2058 	return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
2059 					      NULL, &mvm->snif_sta,
2060 					      &mvm->snif_queue,
2061 					      IWL_MVM_TX_FIFO_BE);
2062 }
2063 
2064 int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2065 {
2066 	int ret;
2067 
2068 	lockdep_assert_held(&mvm->mutex);
2069 
2070 	if (WARN_ON_ONCE(mvm->snif_sta.sta_id == IWL_MVM_INVALID_STA))
2071 		return -EINVAL;
2072 
2073 	iwl_mvm_disable_txq(mvm, NULL, &mvm->snif_queue, IWL_MAX_TID_COUNT, 0);
2074 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
2075 	if (ret)
2076 		IWL_WARN(mvm, "Failed sending remove station\n");
2077 
2078 	return ret;
2079 }
2080 
2081 int iwl_mvm_rm_aux_sta(struct iwl_mvm *mvm)
2082 {
2083 	int ret;
2084 
2085 	lockdep_assert_held(&mvm->mutex);
2086 
2087 	if (WARN_ON_ONCE(mvm->aux_sta.sta_id == IWL_MVM_INVALID_STA))
2088 		return -EINVAL;
2089 
2090 	iwl_mvm_disable_txq(mvm, NULL, &mvm->aux_queue, IWL_MAX_TID_COUNT, 0);
2091 	ret = iwl_mvm_rm_sta_common(mvm, mvm->aux_sta.sta_id);
2092 	if (ret)
2093 		IWL_WARN(mvm, "Failed sending remove station\n");
2094 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2095 
2096 	return ret;
2097 }
2098 
2099 void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
2100 {
2101 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
2102 }
2103 
2104 /*
2105  * Send the add station command for the vif's broadcast station.
2106  * Assumes that the station was already allocated.
2107  *
2108  * @mvm: the mvm component
2109  * @vif: the interface to which the broadcast station is added
2110  * @bsta: the broadcast station to add.
2111  */
2112 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2113 {
2114 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2115 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2116 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2117 	const u8 *baddr = _baddr;
2118 	int queue;
2119 	int ret;
2120 	unsigned int wdg_timeout =
2121 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2122 	struct iwl_trans_txq_scd_cfg cfg = {
2123 		.fifo = IWL_MVM_TX_FIFO_VO,
2124 		.sta_id = mvmvif->bcast_sta.sta_id,
2125 		.tid = IWL_MAX_TID_COUNT,
2126 		.aggregate = false,
2127 		.frame_limit = IWL_FRAME_LIMIT,
2128 	};
2129 
2130 	lockdep_assert_held(&mvm->mutex);
2131 
2132 	if (!iwl_mvm_has_new_tx_api(mvm)) {
2133 		if (vif->type == NL80211_IFTYPE_AP ||
2134 		    vif->type == NL80211_IFTYPE_ADHOC) {
2135 			queue = mvm->probe_queue;
2136 		} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2137 			queue = mvm->p2p_dev_queue;
2138 		} else {
2139 			WARN(1, "Missing required TXQ for adding bcast STA\n");
2140 			return -EINVAL;
2141 		}
2142 
2143 		bsta->tfd_queue_msk |= BIT(queue);
2144 
2145 		iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2146 	}
2147 
2148 	if (vif->type == NL80211_IFTYPE_ADHOC)
2149 		baddr = vif->bss_conf.bssid;
2150 
2151 	if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2152 		return -ENOSPC;
2153 
2154 	ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2155 					 mvmvif->id, mvmvif->color);
2156 	if (ret)
2157 		return ret;
2158 
2159 	/*
2160 	 * For 22000 firmware and on we cannot add queue to a station unknown
2161 	 * to firmware so enable queue here - after the station was added
2162 	 */
2163 	if (iwl_mvm_has_new_tx_api(mvm)) {
2164 		queue = iwl_mvm_tvqm_enable_txq(mvm, bsta->sta_id,
2165 						IWL_MAX_TID_COUNT,
2166 						wdg_timeout);
2167 		if (queue < 0) {
2168 			iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
2169 			return queue;
2170 		}
2171 
2172 		if (vif->type == NL80211_IFTYPE_AP ||
2173 		    vif->type == NL80211_IFTYPE_ADHOC)
2174 			mvm->probe_queue = queue;
2175 		else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2176 			mvm->p2p_dev_queue = queue;
2177 	}
2178 
2179 	return 0;
2180 }
2181 
2182 static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2183 					  struct ieee80211_vif *vif)
2184 {
2185 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2186 	u16 *queueptr, queue;
2187 
2188 	lockdep_assert_held(&mvm->mutex);
2189 
2190 	iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true);
2191 
2192 	switch (vif->type) {
2193 	case NL80211_IFTYPE_AP:
2194 	case NL80211_IFTYPE_ADHOC:
2195 		queueptr = &mvm->probe_queue;
2196 		break;
2197 	case NL80211_IFTYPE_P2P_DEVICE:
2198 		queueptr = &mvm->p2p_dev_queue;
2199 		break;
2200 	default:
2201 		WARN(1, "Can't free bcast queue on vif type %d\n",
2202 		     vif->type);
2203 		return;
2204 	}
2205 
2206 	queue = *queueptr;
2207 	iwl_mvm_disable_txq(mvm, NULL, queueptr, IWL_MAX_TID_COUNT, 0);
2208 	if (iwl_mvm_has_new_tx_api(mvm))
2209 		return;
2210 
2211 	WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2212 	mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2213 }
2214 
2215 /* Send the FW a request to remove the station from it's internal data
2216  * structures, but DO NOT remove the entry from the local data structures. */
2217 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2218 {
2219 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2220 	int ret;
2221 
2222 	lockdep_assert_held(&mvm->mutex);
2223 
2224 	iwl_mvm_free_bcast_sta_queues(mvm, vif);
2225 
2226 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2227 	if (ret)
2228 		IWL_WARN(mvm, "Failed sending remove station\n");
2229 	return ret;
2230 }
2231 
2232 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2233 {
2234 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2235 
2236 	lockdep_assert_held(&mvm->mutex);
2237 
2238 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2239 					ieee80211_vif_type_p2p(vif),
2240 					IWL_STA_GENERAL_PURPOSE);
2241 }
2242 
2243 /* Allocate a new station entry for the broadcast station to the given vif,
2244  * and send it to the FW.
2245  * Note that each P2P mac should have its own broadcast station.
2246  *
2247  * @mvm: the mvm component
2248  * @vif: the interface to which the broadcast station is added
2249  * @bsta: the broadcast station to add. */
2250 int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2251 {
2252 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2253 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2254 	int ret;
2255 
2256 	lockdep_assert_held(&mvm->mutex);
2257 
2258 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2259 	if (ret)
2260 		return ret;
2261 
2262 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2263 
2264 	if (ret)
2265 		iwl_mvm_dealloc_int_sta(mvm, bsta);
2266 
2267 	return ret;
2268 }
2269 
2270 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2271 {
2272 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2273 
2274 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2275 }
2276 
2277 /*
2278  * Send the FW a request to remove the station from it's internal data
2279  * structures, and in addition remove it from the local data structure.
2280  */
2281 int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2282 {
2283 	int ret;
2284 
2285 	lockdep_assert_held(&mvm->mutex);
2286 
2287 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2288 
2289 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
2290 
2291 	return ret;
2292 }
2293 
2294 /*
2295  * Allocate a new station entry for the multicast station to the given vif,
2296  * and send it to the FW.
2297  * Note that each AP/GO mac should have its own multicast station.
2298  *
2299  * @mvm: the mvm component
2300  * @vif: the interface to which the multicast station is added
2301  */
2302 int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2303 {
2304 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2305 	struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
2306 	static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2307 	const u8 *maddr = _maddr;
2308 	struct iwl_trans_txq_scd_cfg cfg = {
2309 		.fifo = vif->type == NL80211_IFTYPE_AP ?
2310 			IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
2311 		.sta_id = msta->sta_id,
2312 		.tid = 0,
2313 		.aggregate = false,
2314 		.frame_limit = IWL_FRAME_LIMIT,
2315 	};
2316 	unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2317 	int ret;
2318 
2319 	lockdep_assert_held(&mvm->mutex);
2320 
2321 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2322 		    vif->type != NL80211_IFTYPE_ADHOC))
2323 		return -ENOTSUPP;
2324 
2325 	/*
2326 	 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2327 	 * invalid, so make sure we use the queue we want.
2328 	 * Note that this is done here as we want to avoid making DQA
2329 	 * changes in mac80211 layer.
2330 	 */
2331 	if (vif->type == NL80211_IFTYPE_ADHOC)
2332 		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2333 
2334 	/*
2335 	 * While in previous FWs we had to exclude cab queue from TFD queue
2336 	 * mask, now it is needed as any other queue.
2337 	 */
2338 	if (!iwl_mvm_has_new_tx_api(mvm) &&
2339 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2340 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2341 				   timeout);
2342 		msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2343 	}
2344 	ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2345 					 mvmvif->id, mvmvif->color);
2346 	if (ret)
2347 		goto err;
2348 
2349 	/*
2350 	 * Enable cab queue after the ADD_STA command is sent.
2351 	 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2352 	 * command with unknown station id, and for FW that doesn't support
2353 	 * station API since the cab queue is not included in the
2354 	 * tfd_queue_mask.
2355 	 */
2356 	if (iwl_mvm_has_new_tx_api(mvm)) {
2357 		int queue = iwl_mvm_tvqm_enable_txq(mvm, msta->sta_id,
2358 						    0,
2359 						    timeout);
2360 		if (queue < 0) {
2361 			ret = queue;
2362 			goto err;
2363 		}
2364 		mvmvif->cab_queue = queue;
2365 	} else if (!fw_has_api(&mvm->fw->ucode_capa,
2366 			       IWL_UCODE_TLV_API_STA_TYPE))
2367 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2368 				   timeout);
2369 
2370 	return 0;
2371 err:
2372 	iwl_mvm_dealloc_int_sta(mvm, msta);
2373 	return ret;
2374 }
2375 
2376 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2377 				    struct ieee80211_key_conf *keyconf,
2378 				    bool mcast)
2379 {
2380 	union {
2381 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2382 		struct iwl_mvm_add_sta_key_cmd cmd;
2383 	} u = {};
2384 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2385 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
2386 	__le16 key_flags;
2387 	int ret, size;
2388 	u32 status;
2389 
2390 	/* This is a valid situation for GTK removal */
2391 	if (sta_id == IWL_MVM_INVALID_STA)
2392 		return 0;
2393 
2394 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2395 				 STA_KEY_FLG_KEYID_MSK);
2396 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2397 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2398 
2399 	if (mcast)
2400 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2401 
2402 	/*
2403 	 * The fields assigned here are in the same location at the start
2404 	 * of the command, so we can do this union trick.
2405 	 */
2406 	u.cmd.common.key_flags = key_flags;
2407 	u.cmd.common.key_offset = keyconf->hw_key_idx;
2408 	u.cmd.common.sta_id = sta_id;
2409 
2410 	size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
2411 
2412 	status = ADD_STA_SUCCESS;
2413 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
2414 					  &status);
2415 
2416 	switch (status) {
2417 	case ADD_STA_SUCCESS:
2418 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2419 		break;
2420 	default:
2421 		ret = -EIO;
2422 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2423 		break;
2424 	}
2425 
2426 	return ret;
2427 }
2428 
2429 /*
2430  * Send the FW a request to remove the station from it's internal data
2431  * structures, and in addition remove it from the local data structure.
2432  */
2433 int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2434 {
2435 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2436 	int ret;
2437 
2438 	lockdep_assert_held(&mvm->mutex);
2439 
2440 	iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true);
2441 
2442 	iwl_mvm_disable_txq(mvm, NULL, &mvmvif->cab_queue, 0, 0);
2443 
2444 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2445 	if (ret)
2446 		IWL_WARN(mvm, "Failed sending remove station\n");
2447 
2448 	return ret;
2449 }
2450 
2451 #define IWL_MAX_RX_BA_SESSIONS 16
2452 
2453 static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
2454 {
2455 	struct iwl_mvm_delba_data notif = {
2456 		.baid = baid,
2457 	};
2458 
2459 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_NOTIF_DEL_BA, true,
2460 					&notif, sizeof(notif));
2461 };
2462 
2463 static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2464 				 struct iwl_mvm_baid_data *data)
2465 {
2466 	int i;
2467 
2468 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2469 
2470 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2471 		int j;
2472 		struct iwl_mvm_reorder_buffer *reorder_buf =
2473 			&data->reorder_buf[i];
2474 		struct iwl_mvm_reorder_buf_entry *entries =
2475 			&data->entries[i * data->entries_per_queue];
2476 
2477 		spin_lock_bh(&reorder_buf->lock);
2478 		if (likely(!reorder_buf->num_stored)) {
2479 			spin_unlock_bh(&reorder_buf->lock);
2480 			continue;
2481 		}
2482 
2483 		/*
2484 		 * This shouldn't happen in regular DELBA since the internal
2485 		 * delBA notification should trigger a release of all frames in
2486 		 * the reorder buffer.
2487 		 */
2488 		WARN_ON(1);
2489 
2490 		for (j = 0; j < reorder_buf->buf_size; j++)
2491 			__skb_queue_purge(&entries[j].e.frames);
2492 		/*
2493 		 * Prevent timer re-arm. This prevents a very far fetched case
2494 		 * where we timed out on the notification. There may be prior
2495 		 * RX frames pending in the RX queue before the notification
2496 		 * that might get processed between now and the actual deletion
2497 		 * and we would re-arm the timer although we are deleting the
2498 		 * reorder buffer.
2499 		 */
2500 		reorder_buf->removed = true;
2501 		spin_unlock_bh(&reorder_buf->lock);
2502 		del_timer_sync(&reorder_buf->reorder_timer);
2503 	}
2504 }
2505 
2506 static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2507 					struct iwl_mvm_baid_data *data,
2508 					u16 ssn, u16 buf_size)
2509 {
2510 	int i;
2511 
2512 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2513 		struct iwl_mvm_reorder_buffer *reorder_buf =
2514 			&data->reorder_buf[i];
2515 		struct iwl_mvm_reorder_buf_entry *entries =
2516 			&data->entries[i * data->entries_per_queue];
2517 		int j;
2518 
2519 		reorder_buf->num_stored = 0;
2520 		reorder_buf->head_sn = ssn;
2521 		reorder_buf->buf_size = buf_size;
2522 		/* rx reorder timer */
2523 		timer_setup(&reorder_buf->reorder_timer,
2524 			    iwl_mvm_reorder_timer_expired, 0);
2525 		spin_lock_init(&reorder_buf->lock);
2526 		reorder_buf->mvm = mvm;
2527 		reorder_buf->queue = i;
2528 		reorder_buf->valid = false;
2529 		for (j = 0; j < reorder_buf->buf_size; j++)
2530 			__skb_queue_head_init(&entries[j].e.frames);
2531 	}
2532 }
2533 
2534 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2535 		       int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2536 {
2537 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2538 	struct iwl_mvm_add_sta_cmd cmd = {};
2539 	struct iwl_mvm_baid_data *baid_data = NULL;
2540 	int ret;
2541 	u32 status;
2542 
2543 	lockdep_assert_held(&mvm->mutex);
2544 
2545 	if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2546 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2547 		return -ENOSPC;
2548 	}
2549 
2550 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
2551 		u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2552 
2553 		/* sparse doesn't like the __align() so don't check */
2554 #ifndef __CHECKER__
2555 		/*
2556 		 * The division below will be OK if either the cache line size
2557 		 * can be divided by the entry size (ALIGN will round up) or if
2558 		 * if the entry size can be divided by the cache line size, in
2559 		 * which case the ALIGN() will do nothing.
2560 		 */
2561 		BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2562 			     sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2563 #endif
2564 
2565 		/*
2566 		 * Upward align the reorder buffer size to fill an entire cache
2567 		 * line for each queue, to avoid sharing cache lines between
2568 		 * different queues.
2569 		 */
2570 		reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2571 
2572 		/*
2573 		 * Allocate here so if allocation fails we can bail out early
2574 		 * before starting the BA session in the firmware
2575 		 */
2576 		baid_data = kzalloc(sizeof(*baid_data) +
2577 				    mvm->trans->num_rx_queues *
2578 				    reorder_buf_size,
2579 				    GFP_KERNEL);
2580 		if (!baid_data)
2581 			return -ENOMEM;
2582 
2583 		/*
2584 		 * This division is why we need the above BUILD_BUG_ON(),
2585 		 * if that doesn't hold then this will not be right.
2586 		 */
2587 		baid_data->entries_per_queue =
2588 			reorder_buf_size / sizeof(baid_data->entries[0]);
2589 	}
2590 
2591 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2592 	cmd.sta_id = mvm_sta->sta_id;
2593 	cmd.add_modify = STA_MODE_MODIFY;
2594 	if (start) {
2595 		cmd.add_immediate_ba_tid = (u8) tid;
2596 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2597 		cmd.rx_ba_window = cpu_to_le16(buf_size);
2598 	} else {
2599 		cmd.remove_immediate_ba_tid = (u8) tid;
2600 	}
2601 	cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2602 				  STA_MODIFY_REMOVE_BA_TID;
2603 
2604 	status = ADD_STA_SUCCESS;
2605 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2606 					  iwl_mvm_add_sta_cmd_size(mvm),
2607 					  &cmd, &status);
2608 	if (ret)
2609 		goto out_free;
2610 
2611 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2612 	case ADD_STA_SUCCESS:
2613 		IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2614 			     start ? "start" : "stopp");
2615 		break;
2616 	case ADD_STA_IMMEDIATE_BA_FAILURE:
2617 		IWL_WARN(mvm, "RX BA Session refused by fw\n");
2618 		ret = -ENOSPC;
2619 		break;
2620 	default:
2621 		ret = -EIO;
2622 		IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2623 			start ? "start" : "stopp", status);
2624 		break;
2625 	}
2626 
2627 	if (ret)
2628 		goto out_free;
2629 
2630 	if (start) {
2631 		u8 baid;
2632 
2633 		mvm->rx_ba_sessions++;
2634 
2635 		if (!iwl_mvm_has_new_rx_api(mvm))
2636 			return 0;
2637 
2638 		if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2639 			ret = -EINVAL;
2640 			goto out_free;
2641 		}
2642 		baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2643 			    IWL_ADD_STA_BAID_SHIFT);
2644 		baid_data->baid = baid;
2645 		baid_data->timeout = timeout;
2646 		baid_data->last_rx = jiffies;
2647 		baid_data->rcu_ptr = &mvm->baid_map[baid];
2648 		timer_setup(&baid_data->session_timer,
2649 			    iwl_mvm_rx_agg_session_expired, 0);
2650 		baid_data->mvm = mvm;
2651 		baid_data->tid = tid;
2652 		baid_data->sta_id = mvm_sta->sta_id;
2653 
2654 		mvm_sta->tid_to_baid[tid] = baid;
2655 		if (timeout)
2656 			mod_timer(&baid_data->session_timer,
2657 				  TU_TO_EXP_TIME(timeout * 2));
2658 
2659 		iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
2660 		/*
2661 		 * protect the BA data with RCU to cover a case where our
2662 		 * internal RX sync mechanism will timeout (not that it's
2663 		 * supposed to happen) and we will free the session data while
2664 		 * RX is being processed in parallel
2665 		 */
2666 		IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2667 			     mvm_sta->sta_id, tid, baid);
2668 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2669 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2670 	} else  {
2671 		u8 baid = mvm_sta->tid_to_baid[tid];
2672 
2673 		if (mvm->rx_ba_sessions > 0)
2674 			/* check that restart flow didn't zero the counter */
2675 			mvm->rx_ba_sessions--;
2676 		if (!iwl_mvm_has_new_rx_api(mvm))
2677 			return 0;
2678 
2679 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2680 			return -EINVAL;
2681 
2682 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2683 		if (WARN_ON(!baid_data))
2684 			return -EINVAL;
2685 
2686 		/* synchronize all rx queues so we can safely delete */
2687 		iwl_mvm_free_reorder(mvm, baid_data);
2688 		del_timer_sync(&baid_data->session_timer);
2689 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2690 		kfree_rcu(baid_data, rcu_head);
2691 		IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
2692 	}
2693 	return 0;
2694 
2695 out_free:
2696 	kfree(baid_data);
2697 	return ret;
2698 }
2699 
2700 int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2701 		       int tid, u8 queue, bool start)
2702 {
2703 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2704 	struct iwl_mvm_add_sta_cmd cmd = {};
2705 	int ret;
2706 	u32 status;
2707 
2708 	lockdep_assert_held(&mvm->mutex);
2709 
2710 	if (start) {
2711 		mvm_sta->tfd_queue_msk |= BIT(queue);
2712 		mvm_sta->tid_disable_agg &= ~BIT(tid);
2713 	} else {
2714 		/* In DQA-mode the queue isn't removed on agg termination */
2715 		mvm_sta->tid_disable_agg |= BIT(tid);
2716 	}
2717 
2718 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2719 	cmd.sta_id = mvm_sta->sta_id;
2720 	cmd.add_modify = STA_MODE_MODIFY;
2721 	if (!iwl_mvm_has_new_tx_api(mvm))
2722 		cmd.modify_mask = STA_MODIFY_QUEUES;
2723 	cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2724 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2725 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2726 
2727 	status = ADD_STA_SUCCESS;
2728 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2729 					  iwl_mvm_add_sta_cmd_size(mvm),
2730 					  &cmd, &status);
2731 	if (ret)
2732 		return ret;
2733 
2734 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2735 	case ADD_STA_SUCCESS:
2736 		break;
2737 	default:
2738 		ret = -EIO;
2739 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2740 			start ? "start" : "stopp", status);
2741 		break;
2742 	}
2743 
2744 	return ret;
2745 }
2746 
2747 const u8 tid_to_mac80211_ac[] = {
2748 	IEEE80211_AC_BE,
2749 	IEEE80211_AC_BK,
2750 	IEEE80211_AC_BK,
2751 	IEEE80211_AC_BE,
2752 	IEEE80211_AC_VI,
2753 	IEEE80211_AC_VI,
2754 	IEEE80211_AC_VO,
2755 	IEEE80211_AC_VO,
2756 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2757 };
2758 
2759 static const u8 tid_to_ucode_ac[] = {
2760 	AC_BE,
2761 	AC_BK,
2762 	AC_BK,
2763 	AC_BE,
2764 	AC_VI,
2765 	AC_VI,
2766 	AC_VO,
2767 	AC_VO,
2768 };
2769 
2770 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2771 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2772 {
2773 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2774 	struct iwl_mvm_tid_data *tid_data;
2775 	u16 normalized_ssn;
2776 	u16 txq_id;
2777 	int ret;
2778 
2779 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2780 		return -EINVAL;
2781 
2782 	if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2783 	    mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2784 		IWL_ERR(mvm,
2785 			"Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2786 			mvmsta->tid_data[tid].state);
2787 		return -ENXIO;
2788 	}
2789 
2790 	lockdep_assert_held(&mvm->mutex);
2791 
2792 	if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2793 	    iwl_mvm_has_new_tx_api(mvm)) {
2794 		u8 ac = tid_to_mac80211_ac[tid];
2795 
2796 		ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2797 		if (ret)
2798 			return ret;
2799 	}
2800 
2801 	spin_lock_bh(&mvmsta->lock);
2802 
2803 	/*
2804 	 * Note the possible cases:
2805 	 *  1. An enabled TXQ - TXQ needs to become agg'ed
2806 	 *  2. The TXQ hasn't yet been enabled, so find a free one and mark
2807 	 *	it as reserved
2808 	 */
2809 	txq_id = mvmsta->tid_data[tid].txq_id;
2810 	if (txq_id == IWL_MVM_INVALID_QUEUE) {
2811 		ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2812 					      IWL_MVM_DQA_MIN_DATA_QUEUE,
2813 					      IWL_MVM_DQA_MAX_DATA_QUEUE);
2814 		if (ret < 0) {
2815 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
2816 			goto out;
2817 		}
2818 
2819 		txq_id = ret;
2820 
2821 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
2822 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2823 	} else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2824 		ret = -ENXIO;
2825 		IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2826 			tid, IWL_MAX_HW_QUEUES - 1);
2827 		goto out;
2828 
2829 	} else if (unlikely(mvm->queue_info[txq_id].status ==
2830 			    IWL_MVM_QUEUE_SHARED)) {
2831 		ret = -ENXIO;
2832 		IWL_DEBUG_TX_QUEUES(mvm,
2833 				    "Can't start tid %d agg on shared queue!\n",
2834 				    tid);
2835 		goto out;
2836 	}
2837 
2838 	IWL_DEBUG_TX_QUEUES(mvm,
2839 			    "AGG for tid %d will be on queue #%d\n",
2840 			    tid, txq_id);
2841 
2842 	tid_data = &mvmsta->tid_data[tid];
2843 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2844 	tid_data->txq_id = txq_id;
2845 	*ssn = tid_data->ssn;
2846 
2847 	IWL_DEBUG_TX_QUEUES(mvm,
2848 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2849 			    mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2850 			    tid_data->next_reclaimed);
2851 
2852 	/*
2853 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
2854 	 * to align the wrap around of ssn so we compare relevant values.
2855 	 */
2856 	normalized_ssn = tid_data->ssn;
2857 	if (mvm->trans->trans_cfg->gen2)
2858 		normalized_ssn &= 0xff;
2859 
2860 	if (normalized_ssn == tid_data->next_reclaimed) {
2861 		tid_data->state = IWL_AGG_STARTING;
2862 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
2863 	} else {
2864 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2865 		ret = IEEE80211_AMPDU_TX_START_DELAY_ADDBA;
2866 	}
2867 
2868 out:
2869 	spin_unlock_bh(&mvmsta->lock);
2870 
2871 	return ret;
2872 }
2873 
2874 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2875 			    struct ieee80211_sta *sta, u16 tid, u16 buf_size,
2876 			    bool amsdu)
2877 {
2878 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2879 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2880 	unsigned int wdg_timeout =
2881 		iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2882 	int queue, ret;
2883 	bool alloc_queue = true;
2884 	enum iwl_mvm_queue_status queue_status;
2885 	u16 ssn;
2886 
2887 	struct iwl_trans_txq_scd_cfg cfg = {
2888 		.sta_id = mvmsta->sta_id,
2889 		.tid = tid,
2890 		.frame_limit = buf_size,
2891 		.aggregate = true,
2892 	};
2893 
2894 	/*
2895 	 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
2896 	 * manager, so this function should never be called in this case.
2897 	 */
2898 	if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
2899 		return -EINVAL;
2900 
2901 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2902 		     != IWL_MAX_TID_COUNT);
2903 
2904 	spin_lock_bh(&mvmsta->lock);
2905 	ssn = tid_data->ssn;
2906 	queue = tid_data->txq_id;
2907 	tid_data->state = IWL_AGG_ON;
2908 	mvmsta->agg_tids |= BIT(tid);
2909 	tid_data->ssn = 0xffff;
2910 	tid_data->amsdu_in_ampdu_allowed = amsdu;
2911 	spin_unlock_bh(&mvmsta->lock);
2912 
2913 	if (iwl_mvm_has_new_tx_api(mvm)) {
2914 		/*
2915 		 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
2916 		 * would have failed, so if we are here there is no need to
2917 		 * allocate a queue.
2918 		 * However, if aggregation size is different than the default
2919 		 * size, the scheduler should be reconfigured.
2920 		 * We cannot do this with the new TX API, so return unsupported
2921 		 * for now, until it will be offloaded to firmware..
2922 		 * Note that if SCD default value changes - this condition
2923 		 * should be updated as well.
2924 		 */
2925 		if (buf_size < IWL_FRAME_LIMIT)
2926 			return -ENOTSUPP;
2927 
2928 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2929 		if (ret)
2930 			return -EIO;
2931 		goto out;
2932 	}
2933 
2934 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2935 
2936 	queue_status = mvm->queue_info[queue].status;
2937 
2938 	/* Maybe there is no need to even alloc a queue... */
2939 	if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2940 		alloc_queue = false;
2941 
2942 	/*
2943 	 * Only reconfig the SCD for the queue if the window size has
2944 	 * changed from current (become smaller)
2945 	 */
2946 	if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
2947 		/*
2948 		 * If reconfiguring an existing queue, it first must be
2949 		 * drained
2950 		 */
2951 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2952 						     BIT(queue));
2953 		if (ret) {
2954 			IWL_ERR(mvm,
2955 				"Error draining queue before reconfig\n");
2956 			return ret;
2957 		}
2958 
2959 		ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2960 					   mvmsta->sta_id, tid,
2961 					   buf_size, ssn);
2962 		if (ret) {
2963 			IWL_ERR(mvm,
2964 				"Error reconfiguring TXQ #%d\n", queue);
2965 			return ret;
2966 		}
2967 	}
2968 
2969 	if (alloc_queue)
2970 		iwl_mvm_enable_txq(mvm, sta, queue, ssn,
2971 				   &cfg, wdg_timeout);
2972 
2973 	/* Send ADD_STA command to enable aggs only if the queue isn't shared */
2974 	if (queue_status != IWL_MVM_QUEUE_SHARED) {
2975 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2976 		if (ret)
2977 			return -EIO;
2978 	}
2979 
2980 	/* No need to mark as reserved */
2981 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2982 
2983 out:
2984 	/*
2985 	 * Even though in theory the peer could have different
2986 	 * aggregation reorder buffer sizes for different sessions,
2987 	 * our ucode doesn't allow for that and has a global limit
2988 	 * for each station. Therefore, use the minimum of all the
2989 	 * aggregation sessions and our default value.
2990 	 */
2991 	mvmsta->max_agg_bufsize =
2992 		min(mvmsta->max_agg_bufsize, buf_size);
2993 	mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2994 
2995 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2996 		     sta->addr, tid);
2997 
2998 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq);
2999 }
3000 
3001 static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
3002 					struct iwl_mvm_sta *mvmsta,
3003 					struct iwl_mvm_tid_data *tid_data)
3004 {
3005 	u16 txq_id = tid_data->txq_id;
3006 
3007 	lockdep_assert_held(&mvm->mutex);
3008 
3009 	if (iwl_mvm_has_new_tx_api(mvm))
3010 		return;
3011 
3012 	/*
3013 	 * The TXQ is marked as reserved only if no traffic came through yet
3014 	 * This means no traffic has been sent on this TID (agg'd or not), so
3015 	 * we no longer have use for the queue. Since it hasn't even been
3016 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
3017 	 * free.
3018 	 */
3019 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
3020 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
3021 		tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
3022 	}
3023 }
3024 
3025 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3026 			    struct ieee80211_sta *sta, u16 tid)
3027 {
3028 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3029 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3030 	u16 txq_id;
3031 	int err;
3032 
3033 	/*
3034 	 * If mac80211 is cleaning its state, then say that we finished since
3035 	 * our state has been cleared anyway.
3036 	 */
3037 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3038 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3039 		return 0;
3040 	}
3041 
3042 	spin_lock_bh(&mvmsta->lock);
3043 
3044 	txq_id = tid_data->txq_id;
3045 
3046 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3047 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3048 
3049 	mvmsta->agg_tids &= ~BIT(tid);
3050 
3051 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3052 
3053 	switch (tid_data->state) {
3054 	case IWL_AGG_ON:
3055 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3056 
3057 		IWL_DEBUG_TX_QUEUES(mvm,
3058 				    "ssn = %d, next_recl = %d\n",
3059 				    tid_data->ssn, tid_data->next_reclaimed);
3060 
3061 		tid_data->ssn = 0xffff;
3062 		tid_data->state = IWL_AGG_OFF;
3063 		spin_unlock_bh(&mvmsta->lock);
3064 
3065 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3066 
3067 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3068 		return 0;
3069 	case IWL_AGG_STARTING:
3070 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
3071 		/*
3072 		 * The agg session has been stopped before it was set up. This
3073 		 * can happen when the AddBA timer times out for example.
3074 		 */
3075 
3076 		/* No barriers since we are under mutex */
3077 		lockdep_assert_held(&mvm->mutex);
3078 
3079 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3080 		tid_data->state = IWL_AGG_OFF;
3081 		err = 0;
3082 		break;
3083 	default:
3084 		IWL_ERR(mvm,
3085 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3086 			mvmsta->sta_id, tid, tid_data->state);
3087 		IWL_ERR(mvm,
3088 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
3089 		err = -EINVAL;
3090 	}
3091 
3092 	spin_unlock_bh(&mvmsta->lock);
3093 
3094 	return err;
3095 }
3096 
3097 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3098 			    struct ieee80211_sta *sta, u16 tid)
3099 {
3100 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3101 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3102 	u16 txq_id;
3103 	enum iwl_mvm_agg_state old_state;
3104 
3105 	/*
3106 	 * First set the agg state to OFF to avoid calling
3107 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3108 	 */
3109 	spin_lock_bh(&mvmsta->lock);
3110 	txq_id = tid_data->txq_id;
3111 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3112 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3113 	old_state = tid_data->state;
3114 	tid_data->state = IWL_AGG_OFF;
3115 	mvmsta->agg_tids &= ~BIT(tid);
3116 	spin_unlock_bh(&mvmsta->lock);
3117 
3118 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3119 
3120 	if (old_state >= IWL_AGG_ON) {
3121 		iwl_mvm_drain_sta(mvm, mvmsta, true);
3122 
3123 		if (iwl_mvm_has_new_tx_api(mvm)) {
3124 			if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3125 						   BIT(tid)))
3126 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3127 			iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3128 		} else {
3129 			if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id)))
3130 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3131 			iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3132 		}
3133 
3134 		iwl_mvm_drain_sta(mvm, mvmsta, false);
3135 
3136 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3137 	}
3138 
3139 	return 0;
3140 }
3141 
3142 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3143 {
3144 	int i, max = -1, max_offs = -1;
3145 
3146 	lockdep_assert_held(&mvm->mutex);
3147 
3148 	/* Pick the unused key offset with the highest 'deleted'
3149 	 * counter. Every time a key is deleted, all the counters
3150 	 * are incremented and the one that was just deleted is
3151 	 * reset to zero. Thus, the highest counter is the one
3152 	 * that was deleted longest ago. Pick that one.
3153 	 */
3154 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3155 		if (test_bit(i, mvm->fw_key_table))
3156 			continue;
3157 		if (mvm->fw_key_deleted[i] > max) {
3158 			max = mvm->fw_key_deleted[i];
3159 			max_offs = i;
3160 		}
3161 	}
3162 
3163 	if (max_offs < 0)
3164 		return STA_KEY_IDX_INVALID;
3165 
3166 	return max_offs;
3167 }
3168 
3169 static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
3170 					       struct ieee80211_vif *vif,
3171 					       struct ieee80211_sta *sta)
3172 {
3173 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3174 
3175 	if (sta)
3176 		return iwl_mvm_sta_from_mac80211(sta);
3177 
3178 	/*
3179 	 * The device expects GTKs for station interfaces to be
3180 	 * installed as GTKs for the AP station. If we have no
3181 	 * station ID, then use AP's station ID.
3182 	 */
3183 	if (vif->type == NL80211_IFTYPE_STATION &&
3184 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3185 		u8 sta_id = mvmvif->ap_sta_id;
3186 
3187 		sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
3188 					    lockdep_is_held(&mvm->mutex));
3189 
3190 		/*
3191 		 * It is possible that the 'sta' parameter is NULL,
3192 		 * for example when a GTK is removed - the sta_id will then
3193 		 * be the AP ID, and no station was passed by mac80211.
3194 		 */
3195 		if (IS_ERR_OR_NULL(sta))
3196 			return NULL;
3197 
3198 		return iwl_mvm_sta_from_mac80211(sta);
3199 	}
3200 
3201 	return NULL;
3202 }
3203 
3204 static int iwl_mvm_pn_cmp(const u8 *pn1, const u8 *pn2, int len)
3205 {
3206 	int i;
3207 
3208 	for (i = len - 1; i >= 0; i--) {
3209 		if (pn1[i] > pn2[i])
3210 			return 1;
3211 		if (pn1[i] < pn2[i])
3212 			return -1;
3213 	}
3214 
3215 	return 0;
3216 }
3217 
3218 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
3219 				u32 sta_id,
3220 				struct ieee80211_key_conf *key, bool mcast,
3221 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
3222 				u8 key_offset, bool mfp)
3223 {
3224 	union {
3225 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3226 		struct iwl_mvm_add_sta_key_cmd cmd;
3227 	} u = {};
3228 	__le16 key_flags;
3229 	int ret;
3230 	u32 status;
3231 	u16 keyidx;
3232 	u64 pn = 0;
3233 	int i, size;
3234 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3235 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3236 	int api_ver = iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
3237 					    ADD_STA_KEY,
3238 					    new_api ? 2 : 1);
3239 
3240 	if (sta_id == IWL_MVM_INVALID_STA)
3241 		return -EINVAL;
3242 
3243 	keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3244 		 STA_KEY_FLG_KEYID_MSK;
3245 	key_flags = cpu_to_le16(keyidx);
3246 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3247 
3248 	switch (key->cipher) {
3249 	case WLAN_CIPHER_SUITE_TKIP:
3250 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3251 		if (api_ver >= 2) {
3252 			memcpy((void *)&u.cmd.tx_mic_key,
3253 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
3254 			       IWL_MIC_KEY_SIZE);
3255 
3256 			memcpy((void *)&u.cmd.rx_mic_key,
3257 			       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3258 			       IWL_MIC_KEY_SIZE);
3259 			pn = atomic64_read(&key->tx_pn);
3260 
3261 		} else {
3262 			u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3263 			for (i = 0; i < 5; i++)
3264 				u.cmd_v1.tkip_rx_ttak[i] =
3265 					cpu_to_le16(tkip_p1k[i]);
3266 		}
3267 		memcpy(u.cmd.common.key, key->key, key->keylen);
3268 		break;
3269 	case WLAN_CIPHER_SUITE_CCMP:
3270 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
3271 		memcpy(u.cmd.common.key, key->key, key->keylen);
3272 		if (api_ver >= 2)
3273 			pn = atomic64_read(&key->tx_pn);
3274 		break;
3275 	case WLAN_CIPHER_SUITE_WEP104:
3276 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3277 		fallthrough;
3278 	case WLAN_CIPHER_SUITE_WEP40:
3279 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
3280 		memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3281 		break;
3282 	case WLAN_CIPHER_SUITE_GCMP_256:
3283 		key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3284 		fallthrough;
3285 	case WLAN_CIPHER_SUITE_GCMP:
3286 		key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
3287 		memcpy(u.cmd.common.key, key->key, key->keylen);
3288 		if (api_ver >= 2)
3289 			pn = atomic64_read(&key->tx_pn);
3290 		break;
3291 	default:
3292 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
3293 		memcpy(u.cmd.common.key, key->key, key->keylen);
3294 	}
3295 
3296 	if (mcast)
3297 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3298 	if (mfp)
3299 		key_flags |= cpu_to_le16(STA_KEY_MFP);
3300 
3301 	u.cmd.common.key_offset = key_offset;
3302 	u.cmd.common.key_flags = key_flags;
3303 	u.cmd.common.sta_id = sta_id;
3304 
3305 	if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
3306 		i = 0;
3307 	else
3308 		i = -1;
3309 
3310 	for (; i < IEEE80211_NUM_TIDS; i++) {
3311 		struct ieee80211_key_seq seq = {};
3312 		u8 _rx_pn[IEEE80211_MAX_PN_LEN] = {}, *rx_pn = _rx_pn;
3313 		int rx_pn_len = 8;
3314 		/* there's a hole at 2/3 in FW format depending on version */
3315 		int hole = api_ver >= 3 ? 0 : 2;
3316 
3317 		ieee80211_get_key_rx_seq(key, i, &seq);
3318 
3319 		if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
3320 			rx_pn[0] = seq.tkip.iv16;
3321 			rx_pn[1] = seq.tkip.iv16 >> 8;
3322 			rx_pn[2 + hole] = seq.tkip.iv32;
3323 			rx_pn[3 + hole] = seq.tkip.iv32 >> 8;
3324 			rx_pn[4 + hole] = seq.tkip.iv32 >> 16;
3325 			rx_pn[5 + hole] = seq.tkip.iv32 >> 24;
3326 		} else if (key_flags & cpu_to_le16(STA_KEY_FLG_EXT)) {
3327 			rx_pn = seq.hw.seq;
3328 			rx_pn_len = seq.hw.seq_len;
3329 		} else {
3330 			rx_pn[0] = seq.ccmp.pn[0];
3331 			rx_pn[1] = seq.ccmp.pn[1];
3332 			rx_pn[2 + hole] = seq.ccmp.pn[2];
3333 			rx_pn[3 + hole] = seq.ccmp.pn[3];
3334 			rx_pn[4 + hole] = seq.ccmp.pn[4];
3335 			rx_pn[5 + hole] = seq.ccmp.pn[5];
3336 		}
3337 
3338 		if (iwl_mvm_pn_cmp(rx_pn, (u8 *)&u.cmd.common.rx_secur_seq_cnt,
3339 				   rx_pn_len) > 0)
3340 			memcpy(&u.cmd.common.rx_secur_seq_cnt, rx_pn,
3341 			       rx_pn_len);
3342 	}
3343 
3344 	if (api_ver >= 2) {
3345 		u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3346 		size = sizeof(u.cmd);
3347 	} else {
3348 		size = sizeof(u.cmd_v1);
3349 	}
3350 
3351 	status = ADD_STA_SUCCESS;
3352 	if (cmd_flags & CMD_ASYNC)
3353 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3354 					   &u.cmd);
3355 	else
3356 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3357 						  &u.cmd, &status);
3358 
3359 	switch (status) {
3360 	case ADD_STA_SUCCESS:
3361 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3362 		break;
3363 	default:
3364 		ret = -EIO;
3365 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3366 		break;
3367 	}
3368 
3369 	return ret;
3370 }
3371 
3372 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3373 				 struct ieee80211_key_conf *keyconf,
3374 				 u8 sta_id, bool remove_key)
3375 {
3376 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3377 
3378 	/* verify the key details match the required command's expectations */
3379 	if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3380 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5 &&
3381 		     keyconf->keyidx != 6 && keyconf->keyidx != 7) ||
3382 		    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3383 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3384 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3385 		return -EINVAL;
3386 
3387 	if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3388 		    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3389 		return -EINVAL;
3390 
3391 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3392 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
3393 
3394 	if (remove_key) {
3395 		/* This is a valid situation for IGTK */
3396 		if (sta_id == IWL_MVM_INVALID_STA)
3397 			return 0;
3398 
3399 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3400 	} else {
3401 		struct ieee80211_key_seq seq;
3402 		const u8 *pn;
3403 
3404 		switch (keyconf->cipher) {
3405 		case WLAN_CIPHER_SUITE_AES_CMAC:
3406 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3407 			break;
3408 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3409 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3410 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3411 			break;
3412 		default:
3413 			return -EINVAL;
3414 		}
3415 
3416 		memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3417 		if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3418 			igtk_cmd.ctrl_flags |=
3419 				cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3420 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3421 		pn = seq.aes_cmac.pn;
3422 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3423 						       ((u64) pn[4] << 8) |
3424 						       ((u64) pn[3] << 16) |
3425 						       ((u64) pn[2] << 24) |
3426 						       ((u64) pn[1] << 32) |
3427 						       ((u64) pn[0] << 40));
3428 	}
3429 
3430 	IWL_DEBUG_INFO(mvm, "%s %sIGTK (%d) for sta %u\n",
3431 		       remove_key ? "removing" : "installing",
3432 		       keyconf->keyidx >= 6 ? "B" : "",
3433 		       keyconf->keyidx, igtk_cmd.sta_id);
3434 
3435 	if (!iwl_mvm_has_new_rx_api(mvm)) {
3436 		struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3437 			.ctrl_flags = igtk_cmd.ctrl_flags,
3438 			.key_id = igtk_cmd.key_id,
3439 			.sta_id = igtk_cmd.sta_id,
3440 			.receive_seq_cnt = igtk_cmd.receive_seq_cnt
3441 		};
3442 
3443 		memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3444 		       ARRAY_SIZE(igtk_cmd_v1.igtk));
3445 		return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3446 					    sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3447 	}
3448 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3449 				    sizeof(igtk_cmd), &igtk_cmd);
3450 }
3451 
3452 
3453 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3454 				       struct ieee80211_vif *vif,
3455 				       struct ieee80211_sta *sta)
3456 {
3457 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3458 
3459 	if (sta)
3460 		return sta->addr;
3461 
3462 	if (vif->type == NL80211_IFTYPE_STATION &&
3463 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3464 		u8 sta_id = mvmvif->ap_sta_id;
3465 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3466 						lockdep_is_held(&mvm->mutex));
3467 		return sta->addr;
3468 	}
3469 
3470 
3471 	return NULL;
3472 }
3473 
3474 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3475 				 struct ieee80211_vif *vif,
3476 				 struct ieee80211_sta *sta,
3477 				 struct ieee80211_key_conf *keyconf,
3478 				 u8 key_offset,
3479 				 bool mcast)
3480 {
3481 	const u8 *addr;
3482 	struct ieee80211_key_seq seq;
3483 	u16 p1k[5];
3484 	u32 sta_id;
3485 	bool mfp = false;
3486 
3487 	if (sta) {
3488 		struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3489 
3490 		sta_id = mvm_sta->sta_id;
3491 		mfp = sta->mfp;
3492 	} else if (vif->type == NL80211_IFTYPE_AP &&
3493 		   !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3494 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3495 
3496 		sta_id = mvmvif->mcast_sta.sta_id;
3497 	} else {
3498 		IWL_ERR(mvm, "Failed to find station id\n");
3499 		return -EINVAL;
3500 	}
3501 
3502 	if (keyconf->cipher == WLAN_CIPHER_SUITE_TKIP) {
3503 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3504 		/* get phase 1 key from mac80211 */
3505 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3506 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
3507 
3508 		return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3509 					    seq.tkip.iv32, p1k, 0, key_offset,
3510 					    mfp);
3511 	}
3512 
3513 	return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3514 				    0, NULL, 0, key_offset, mfp);
3515 }
3516 
3517 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3518 			struct ieee80211_vif *vif,
3519 			struct ieee80211_sta *sta,
3520 			struct ieee80211_key_conf *keyconf,
3521 			u8 key_offset)
3522 {
3523 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3524 	struct iwl_mvm_sta *mvm_sta;
3525 	u8 sta_id = IWL_MVM_INVALID_STA;
3526 	int ret;
3527 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3528 
3529 	lockdep_assert_held(&mvm->mutex);
3530 
3531 	if (vif->type != NL80211_IFTYPE_AP ||
3532 	    keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3533 		/* Get the station id from the mvm local station table */
3534 		mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3535 		if (!mvm_sta) {
3536 			IWL_ERR(mvm, "Failed to find station\n");
3537 			return -EINVAL;
3538 		}
3539 		sta_id = mvm_sta->sta_id;
3540 
3541 		/*
3542 		 * It is possible that the 'sta' parameter is NULL, and thus
3543 		 * there is a need to retrieve the sta from the local station
3544 		 * table.
3545 		 */
3546 		if (!sta) {
3547 			sta = rcu_dereference_protected(
3548 				mvm->fw_id_to_mac_id[sta_id],
3549 				lockdep_is_held(&mvm->mutex));
3550 			if (IS_ERR_OR_NULL(sta)) {
3551 				IWL_ERR(mvm, "Invalid station id\n");
3552 				return -EINVAL;
3553 			}
3554 		}
3555 
3556 		if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3557 			return -EINVAL;
3558 	} else {
3559 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3560 
3561 		sta_id = mvmvif->mcast_sta.sta_id;
3562 	}
3563 
3564 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3565 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3566 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3567 		ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3568 		goto end;
3569 	}
3570 
3571 	/* If the key_offset is not pre-assigned, we need to find a
3572 	 * new offset to use.  In normal cases, the offset is not
3573 	 * pre-assigned, but during HW_RESTART we want to reuse the
3574 	 * same indices, so we pass them when this function is called.
3575 	 *
3576 	 * In D3 entry, we need to hardcoded the indices (because the
3577 	 * firmware hardcodes the PTK offset to 0).  In this case, we
3578 	 * need to make sure we don't overwrite the hw_key_idx in the
3579 	 * keyconf structure, because otherwise we cannot configure
3580 	 * the original ones back when resuming.
3581 	 */
3582 	if (key_offset == STA_KEY_IDX_INVALID) {
3583 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
3584 		if (key_offset == STA_KEY_IDX_INVALID)
3585 			return -ENOSPC;
3586 		keyconf->hw_key_idx = key_offset;
3587 	}
3588 
3589 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
3590 	if (ret)
3591 		goto end;
3592 
3593 	/*
3594 	 * For WEP, the same key is used for multicast and unicast. Upload it
3595 	 * again, using the same key offset, and now pointing the other one
3596 	 * to the same key slot (offset).
3597 	 * If this fails, remove the original as well.
3598 	 */
3599 	if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3600 	     keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3601 	    sta) {
3602 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3603 					    key_offset, !mcast);
3604 		if (ret) {
3605 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3606 			goto end;
3607 		}
3608 	}
3609 
3610 	__set_bit(key_offset, mvm->fw_key_table);
3611 
3612 end:
3613 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3614 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3615 		      sta ? sta->addr : zero_addr, ret);
3616 	return ret;
3617 }
3618 
3619 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3620 			   struct ieee80211_vif *vif,
3621 			   struct ieee80211_sta *sta,
3622 			   struct ieee80211_key_conf *keyconf)
3623 {
3624 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3625 	struct iwl_mvm_sta *mvm_sta;
3626 	u8 sta_id = IWL_MVM_INVALID_STA;
3627 	int ret, i;
3628 
3629 	lockdep_assert_held(&mvm->mutex);
3630 
3631 	/* Get the station from the mvm local station table */
3632 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3633 	if (mvm_sta)
3634 		sta_id = mvm_sta->sta_id;
3635 	else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3636 		sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3637 
3638 
3639 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3640 		      keyconf->keyidx, sta_id);
3641 
3642 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3643 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3644 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3645 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3646 
3647 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3648 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3649 			keyconf->hw_key_idx);
3650 		return -ENOENT;
3651 	}
3652 
3653 	/* track which key was deleted last */
3654 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3655 		if (mvm->fw_key_deleted[i] < U8_MAX)
3656 			mvm->fw_key_deleted[i]++;
3657 	}
3658 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3659 
3660 	if (sta && !mvm_sta) {
3661 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3662 		return 0;
3663 	}
3664 
3665 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3666 	if (ret)
3667 		return ret;
3668 
3669 	/* delete WEP key twice to get rid of (now useless) offset */
3670 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3671 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3672 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3673 
3674 	return ret;
3675 }
3676 
3677 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3678 			     struct ieee80211_vif *vif,
3679 			     struct ieee80211_key_conf *keyconf,
3680 			     struct ieee80211_sta *sta, u32 iv32,
3681 			     u16 *phase1key)
3682 {
3683 	struct iwl_mvm_sta *mvm_sta;
3684 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3685 	bool mfp = sta ? sta->mfp : false;
3686 
3687 	rcu_read_lock();
3688 
3689 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3690 	if (WARN_ON_ONCE(!mvm_sta))
3691 		goto unlock;
3692 	iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
3693 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
3694 			     mfp);
3695 
3696  unlock:
3697 	rcu_read_unlock();
3698 }
3699 
3700 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3701 				struct ieee80211_sta *sta)
3702 {
3703 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3704 	struct iwl_mvm_add_sta_cmd cmd = {
3705 		.add_modify = STA_MODE_MODIFY,
3706 		.sta_id = mvmsta->sta_id,
3707 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
3708 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3709 	};
3710 	int ret;
3711 
3712 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3713 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3714 	if (ret)
3715 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3716 }
3717 
3718 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3719 				       struct ieee80211_sta *sta,
3720 				       enum ieee80211_frame_release_type reason,
3721 				       u16 cnt, u16 tids, bool more_data,
3722 				       bool single_sta_queue)
3723 {
3724 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3725 	struct iwl_mvm_add_sta_cmd cmd = {
3726 		.add_modify = STA_MODE_MODIFY,
3727 		.sta_id = mvmsta->sta_id,
3728 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3729 		.sleep_tx_count = cpu_to_le16(cnt),
3730 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3731 	};
3732 	int tid, ret;
3733 	unsigned long _tids = tids;
3734 
3735 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
3736 	 * Note that this field is reserved and unused by firmware not
3737 	 * supporting GO uAPSD, so it's safe to always do this.
3738 	 */
3739 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3740 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3741 
3742 	/* If we're releasing frames from aggregation or dqa queues then check
3743 	 * if all the queues that we're releasing frames from, combined, have:
3744 	 *  - more frames than the service period, in which case more_data
3745 	 *    needs to be set
3746 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
3747 	 *    firmware command (but do that unconditionally)
3748 	 */
3749 	if (single_sta_queue) {
3750 		int remaining = cnt;
3751 		int sleep_tx_count;
3752 
3753 		spin_lock_bh(&mvmsta->lock);
3754 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3755 			struct iwl_mvm_tid_data *tid_data;
3756 			u16 n_queued;
3757 
3758 			tid_data = &mvmsta->tid_data[tid];
3759 
3760 			n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3761 			if (n_queued > remaining) {
3762 				more_data = true;
3763 				remaining = 0;
3764 				break;
3765 			}
3766 			remaining -= n_queued;
3767 		}
3768 		sleep_tx_count = cnt - remaining;
3769 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3770 			mvmsta->sleep_tx_count = sleep_tx_count;
3771 		spin_unlock_bh(&mvmsta->lock);
3772 
3773 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3774 		if (WARN_ON(cnt - remaining == 0)) {
3775 			ieee80211_sta_eosp(sta);
3776 			return;
3777 		}
3778 	}
3779 
3780 	/* Note: this is ignored by firmware not supporting GO uAPSD */
3781 	if (more_data)
3782 		cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3783 
3784 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3785 		mvmsta->next_status_eosp = true;
3786 		cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3787 	} else {
3788 		cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3789 	}
3790 
3791 	/* block the Tx queues until the FW updated the sleep Tx count */
3792 	iwl_trans_block_txq_ptrs(mvm->trans, true);
3793 
3794 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3795 				   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3796 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3797 	if (ret)
3798 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3799 }
3800 
3801 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3802 			   struct iwl_rx_cmd_buffer *rxb)
3803 {
3804 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3805 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3806 	struct ieee80211_sta *sta;
3807 	u32 sta_id = le32_to_cpu(notif->sta_id);
3808 
3809 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
3810 		return;
3811 
3812 	rcu_read_lock();
3813 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3814 	if (!IS_ERR_OR_NULL(sta))
3815 		ieee80211_sta_eosp(sta);
3816 	rcu_read_unlock();
3817 }
3818 
3819 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3820 				   struct iwl_mvm_sta *mvmsta, bool disable)
3821 {
3822 	struct iwl_mvm_add_sta_cmd cmd = {
3823 		.add_modify = STA_MODE_MODIFY,
3824 		.sta_id = mvmsta->sta_id,
3825 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3826 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3827 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3828 	};
3829 	int ret;
3830 
3831 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3832 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3833 	if (ret)
3834 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3835 }
3836 
3837 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3838 				      struct ieee80211_sta *sta,
3839 				      bool disable)
3840 {
3841 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3842 
3843 	spin_lock_bh(&mvm_sta->lock);
3844 
3845 	if (mvm_sta->disable_tx == disable) {
3846 		spin_unlock_bh(&mvm_sta->lock);
3847 		return;
3848 	}
3849 
3850 	mvm_sta->disable_tx = disable;
3851 
3852 	/*
3853 	 * If sta PS state is handled by mac80211, tell it to start/stop
3854 	 * queuing tx for this station.
3855 	 */
3856 	if (!ieee80211_hw_check(mvm->hw, AP_LINK_PS))
3857 		ieee80211_sta_block_awake(mvm->hw, sta, disable);
3858 
3859 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3860 
3861 	spin_unlock_bh(&mvm_sta->lock);
3862 }
3863 
3864 static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3865 					      struct iwl_mvm_vif *mvmvif,
3866 					      struct iwl_mvm_int_sta *sta,
3867 					      bool disable)
3868 {
3869 	u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3870 	struct iwl_mvm_add_sta_cmd cmd = {
3871 		.add_modify = STA_MODE_MODIFY,
3872 		.sta_id = sta->sta_id,
3873 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3874 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3875 		.mac_id_n_color = cpu_to_le32(id),
3876 	};
3877 	int ret;
3878 
3879 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3880 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3881 	if (ret)
3882 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3883 }
3884 
3885 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3886 				       struct iwl_mvm_vif *mvmvif,
3887 				       bool disable)
3888 {
3889 	struct ieee80211_sta *sta;
3890 	struct iwl_mvm_sta *mvm_sta;
3891 	int i;
3892 
3893 	rcu_read_lock();
3894 
3895 	/* Block/unblock all the stations of the given mvmvif */
3896 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
3897 		sta = rcu_dereference(mvm->fw_id_to_mac_id[i]);
3898 		if (IS_ERR_OR_NULL(sta))
3899 			continue;
3900 
3901 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3902 		if (mvm_sta->mac_id_n_color !=
3903 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3904 			continue;
3905 
3906 		iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3907 	}
3908 
3909 	rcu_read_unlock();
3910 
3911 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3912 		return;
3913 
3914 	/* Need to block/unblock also multicast station */
3915 	if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3916 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3917 						  &mvmvif->mcast_sta, disable);
3918 
3919 	/*
3920 	 * Only unblock the broadcast station (FW blocks it for immediate
3921 	 * quiet, not the driver)
3922 	 */
3923 	if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3924 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3925 						  &mvmvif->bcast_sta, disable);
3926 }
3927 
3928 void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3929 {
3930 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3931 	struct iwl_mvm_sta *mvmsta;
3932 
3933 	rcu_read_lock();
3934 
3935 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3936 
3937 	if (!WARN_ON(!mvmsta))
3938 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3939 
3940 	rcu_read_unlock();
3941 }
3942 
3943 u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3944 {
3945 	u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3946 
3947 	/*
3948 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3949 	 * to align the wrap around of ssn so we compare relevant values.
3950 	 */
3951 	if (mvm->trans->trans_cfg->gen2)
3952 		sn &= 0xff;
3953 
3954 	return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
3955 }
3956 
3957 int iwl_mvm_add_pasn_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3958 			 struct iwl_mvm_int_sta *sta, u8 *addr, u32 cipher,
3959 			 u8 *key, u32 key_len)
3960 {
3961 	int ret;
3962 	u16 queue;
3963 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3964 	struct ieee80211_key_conf *keyconf;
3965 
3966 	ret = iwl_mvm_allocate_int_sta(mvm, sta, 0,
3967 				       NL80211_IFTYPE_UNSPECIFIED,
3968 				       IWL_STA_LINK);
3969 	if (ret)
3970 		return ret;
3971 
3972 	ret = iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
3973 					     addr, sta, &queue,
3974 					     IWL_MVM_TX_FIFO_BE);
3975 	if (ret)
3976 		goto out;
3977 
3978 	keyconf = kzalloc(sizeof(*keyconf) + key_len, GFP_KERNEL);
3979 	if (!keyconf) {
3980 		ret = -ENOBUFS;
3981 		goto out;
3982 	}
3983 
3984 	keyconf->cipher = cipher;
3985 	memcpy(keyconf->key, key, key_len);
3986 	keyconf->keylen = key_len;
3987 
3988 	ret = iwl_mvm_send_sta_key(mvm, sta->sta_id, keyconf, false,
3989 				   0, NULL, 0, 0, true);
3990 	kfree(keyconf);
3991 	return 0;
3992 out:
3993 	iwl_mvm_dealloc_int_sta(mvm, sta);
3994 	return ret;
3995 }
3996