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