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