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