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