xref: /linux/drivers/net/wireless/realtek/rtw89/mac80211.c (revision fcc79e1714e8c2b8e216dc3149812edd37884eef)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2019-2020  Realtek Corporation
3  */
4 
5 #include "cam.h"
6 #include "chan.h"
7 #include "coex.h"
8 #include "debug.h"
9 #include "fw.h"
10 #include "mac.h"
11 #include "phy.h"
12 #include "ps.h"
13 #include "reg.h"
14 #include "sar.h"
15 #include "ser.h"
16 #include "util.h"
17 #include "wow.h"
18 
19 static void rtw89_ops_tx(struct ieee80211_hw *hw,
20 			 struct ieee80211_tx_control *control,
21 			 struct sk_buff *skb)
22 {
23 	struct rtw89_dev *rtwdev = hw->priv;
24 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
25 	struct ieee80211_vif *vif = info->control.vif;
26 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
27 	struct ieee80211_sta *sta = control->sta;
28 	u32 flags = IEEE80211_SKB_CB(skb)->flags;
29 	int ret, qsel;
30 
31 	if (rtwvif->offchan && !(flags & IEEE80211_TX_CTL_TX_OFFCHAN) && sta) {
32 		struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
33 
34 		rtw89_debug(rtwdev, RTW89_DBG_TXRX, "ops_tx during offchan\n");
35 		skb_queue_tail(&rtwsta->roc_queue, skb);
36 		return;
37 	}
38 
39 	ret = rtw89_core_tx_write(rtwdev, vif, sta, skb, &qsel);
40 	if (ret) {
41 		rtw89_err(rtwdev, "failed to transmit skb: %d\n", ret);
42 		ieee80211_free_txskb(hw, skb);
43 		return;
44 	}
45 	rtw89_core_tx_kick_off(rtwdev, qsel);
46 }
47 
48 static void rtw89_ops_wake_tx_queue(struct ieee80211_hw *hw,
49 				    struct ieee80211_txq *txq)
50 {
51 	struct rtw89_dev *rtwdev = hw->priv;
52 
53 	ieee80211_schedule_txq(hw, txq);
54 	queue_work(rtwdev->txq_wq, &rtwdev->txq_work);
55 }
56 
57 static int rtw89_ops_start(struct ieee80211_hw *hw)
58 {
59 	struct rtw89_dev *rtwdev = hw->priv;
60 	int ret;
61 
62 	mutex_lock(&rtwdev->mutex);
63 	ret = rtw89_core_start(rtwdev);
64 	mutex_unlock(&rtwdev->mutex);
65 
66 	return ret;
67 }
68 
69 static void rtw89_ops_stop(struct ieee80211_hw *hw, bool suspend)
70 {
71 	struct rtw89_dev *rtwdev = hw->priv;
72 
73 	mutex_lock(&rtwdev->mutex);
74 	rtw89_core_stop(rtwdev);
75 	mutex_unlock(&rtwdev->mutex);
76 }
77 
78 static int rtw89_ops_config(struct ieee80211_hw *hw, u32 changed)
79 {
80 	struct rtw89_dev *rtwdev = hw->priv;
81 
82 	/* let previous ips work finish to ensure we don't leave ips twice */
83 	cancel_work_sync(&rtwdev->ips_work);
84 
85 	mutex_lock(&rtwdev->mutex);
86 	rtw89_leave_ps_mode(rtwdev);
87 
88 	if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
89 	    !(hw->conf.flags & IEEE80211_CONF_IDLE))
90 		rtw89_leave_ips(rtwdev);
91 
92 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
93 		rtw89_config_entity_chandef(rtwdev, RTW89_CHANCTX_0,
94 					    &hw->conf.chandef);
95 		rtw89_set_channel(rtwdev);
96 	}
97 
98 	if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
99 	    (hw->conf.flags & IEEE80211_CONF_IDLE) &&
100 	    !rtwdev->scanning)
101 		rtw89_enter_ips(rtwdev);
102 
103 	mutex_unlock(&rtwdev->mutex);
104 
105 	return 0;
106 }
107 
108 static int __rtw89_ops_add_iface_link(struct rtw89_dev *rtwdev,
109 				      struct rtw89_vif_link *rtwvif_link)
110 {
111 	struct ieee80211_bss_conf *bss_conf;
112 	int ret;
113 
114 	rtw89_leave_ps_mode(rtwdev);
115 
116 	rtw89_vif_type_mapping(rtwvif_link, false);
117 
118 	INIT_WORK(&rtwvif_link->update_beacon_work, rtw89_core_update_beacon_work);
119 	INIT_LIST_HEAD(&rtwvif_link->general_pkt_list);
120 
121 	rtwvif_link->hit_rule = 0;
122 	rtwvif_link->bcn_hit_cond = 0;
123 	rtwvif_link->chanctx_assigned = false;
124 	rtwvif_link->chanctx_idx = RTW89_CHANCTX_0;
125 	rtwvif_link->reg_6ghz_power = RTW89_REG_6GHZ_POWER_DFLT;
126 
127 	rcu_read_lock();
128 
129 	bss_conf = rtw89_vif_rcu_dereference_link(rtwvif_link, true);
130 	ether_addr_copy(rtwvif_link->mac_addr, bss_conf->addr);
131 
132 	rcu_read_unlock();
133 
134 	ret = rtw89_mac_add_vif(rtwdev, rtwvif_link);
135 	if (ret)
136 		return ret;
137 
138 	rtw89_btc_ntfy_role_info(rtwdev, rtwvif_link, NULL, BTC_ROLE_START);
139 	return 0;
140 }
141 
142 static void __rtw89_ops_remove_iface_link(struct rtw89_dev *rtwdev,
143 					  struct rtw89_vif_link *rtwvif_link)
144 {
145 	mutex_unlock(&rtwdev->mutex);
146 	cancel_work_sync(&rtwvif_link->update_beacon_work);
147 	mutex_lock(&rtwdev->mutex);
148 
149 	rtw89_leave_ps_mode(rtwdev);
150 
151 	rtw89_btc_ntfy_role_info(rtwdev, rtwvif_link, NULL, BTC_ROLE_STOP);
152 
153 	rtw89_mac_remove_vif(rtwdev, rtwvif_link);
154 }
155 
156 static int rtw89_ops_add_interface(struct ieee80211_hw *hw,
157 				   struct ieee80211_vif *vif)
158 {
159 	struct rtw89_dev *rtwdev = hw->priv;
160 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
161 	struct rtw89_vif_link *rtwvif_link;
162 	u8 mac_id, port;
163 	int ret = 0;
164 
165 	rtw89_debug(rtwdev, RTW89_DBG_STATE, "add vif %pM type %d, p2p %d\n",
166 		    vif->addr, vif->type, vif->p2p);
167 
168 	mutex_lock(&rtwdev->mutex);
169 
170 	rtw89_leave_ips_by_hwflags(rtwdev);
171 
172 	if (RTW89_CHK_FW_FEATURE(BEACON_FILTER, &rtwdev->fw))
173 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
174 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
175 
176 	mac_id = rtw89_acquire_mac_id(rtwdev);
177 	if (mac_id == RTW89_MAX_MAC_ID_NUM) {
178 		ret = -ENOSPC;
179 		goto err;
180 	}
181 
182 	port = rtw89_core_acquire_bit_map(rtwdev->hw_port, RTW89_PORT_NUM);
183 	if (port == RTW89_PORT_NUM) {
184 		ret = -ENOSPC;
185 		goto release_macid;
186 	}
187 
188 	rtw89_init_vif(rtwdev, rtwvif, mac_id, port);
189 
190 	rtw89_core_txq_init(rtwdev, vif->txq);
191 
192 	if (!rtw89_rtwvif_in_list(rtwdev, rtwvif))
193 		list_add_tail(&rtwvif->list, &rtwdev->rtwvifs_list);
194 
195 	INIT_LIST_HEAD(&rtwvif->mgnt_entry);
196 
197 	ether_addr_copy(rtwvif->mac_addr, vif->addr);
198 
199 	rtwvif->offchan = false;
200 	rtwvif->roc.state = RTW89_ROC_IDLE;
201 	INIT_DELAYED_WORK(&rtwvif->roc.roc_work, rtw89_roc_work);
202 
203 	rtw89_traffic_stats_init(rtwdev, &rtwvif->stats);
204 
205 	rtwvif_link = rtw89_vif_set_link(rtwvif, 0);
206 	if (!rtwvif_link) {
207 		ret = -EINVAL;
208 		goto release_port;
209 	}
210 
211 	ret = __rtw89_ops_add_iface_link(rtwdev, rtwvif_link);
212 	if (ret)
213 		goto unset_link;
214 
215 	rtw89_recalc_lps(rtwdev);
216 
217 	mutex_unlock(&rtwdev->mutex);
218 	return 0;
219 
220 unset_link:
221 	rtw89_vif_unset_link(rtwvif, 0);
222 release_port:
223 	list_del_init(&rtwvif->list);
224 	rtw89_core_release_bit_map(rtwdev->hw_port, port);
225 release_macid:
226 	rtw89_release_mac_id(rtwdev, mac_id);
227 err:
228 	mutex_unlock(&rtwdev->mutex);
229 
230 	return ret;
231 }
232 
233 static void rtw89_ops_remove_interface(struct ieee80211_hw *hw,
234 				       struct ieee80211_vif *vif)
235 {
236 	struct rtw89_dev *rtwdev = hw->priv;
237 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
238 	u8 macid = rtw89_vif_get_main_macid(rtwvif);
239 	u8 port = rtw89_vif_get_main_port(rtwvif);
240 	struct rtw89_vif_link *rtwvif_link;
241 
242 	rtw89_debug(rtwdev, RTW89_DBG_STATE, "remove vif %pM type %d p2p %d\n",
243 		    vif->addr, vif->type, vif->p2p);
244 
245 	cancel_delayed_work_sync(&rtwvif->roc.roc_work);
246 
247 	mutex_lock(&rtwdev->mutex);
248 
249 	rtwvif_link = rtwvif->links[0];
250 	if (unlikely(!rtwvif_link)) {
251 		rtw89_err(rtwdev,
252 			  "%s: rtwvif link (link_id %u) is not active\n",
253 			  __func__, 0);
254 		goto bottom;
255 	}
256 
257 	__rtw89_ops_remove_iface_link(rtwdev, rtwvif_link);
258 
259 	rtw89_vif_unset_link(rtwvif, 0);
260 
261 bottom:
262 	list_del_init(&rtwvif->list);
263 	rtw89_core_release_bit_map(rtwdev->hw_port, port);
264 	rtw89_release_mac_id(rtwdev, macid);
265 
266 	rtw89_recalc_lps(rtwdev);
267 	rtw89_enter_ips_by_hwflags(rtwdev);
268 
269 	mutex_unlock(&rtwdev->mutex);
270 }
271 
272 static int rtw89_ops_change_interface(struct ieee80211_hw *hw,
273 				      struct ieee80211_vif *vif,
274 				      enum nl80211_iftype type, bool p2p)
275 {
276 	struct rtw89_dev *rtwdev = hw->priv;
277 	int ret;
278 
279 	set_bit(RTW89_FLAG_CHANGING_INTERFACE, rtwdev->flags);
280 
281 	rtw89_debug(rtwdev, RTW89_DBG_STATE, "change vif %pM (%d)->(%d), p2p (%d)->(%d)\n",
282 		    vif->addr, vif->type, type, vif->p2p, p2p);
283 
284 	rtw89_ops_remove_interface(hw, vif);
285 
286 	vif->type = type;
287 	vif->p2p = p2p;
288 
289 	ret = rtw89_ops_add_interface(hw, vif);
290 	if (ret)
291 		rtw89_warn(rtwdev, "failed to change interface %d\n", ret);
292 
293 	clear_bit(RTW89_FLAG_CHANGING_INTERFACE, rtwdev->flags);
294 
295 	return ret;
296 }
297 
298 static void rtw89_ops_configure_filter(struct ieee80211_hw *hw,
299 				       unsigned int changed_flags,
300 				       unsigned int *new_flags,
301 				       u64 multicast)
302 {
303 	struct rtw89_dev *rtwdev = hw->priv;
304 	const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
305 	u32 rx_fltr;
306 
307 	mutex_lock(&rtwdev->mutex);
308 	rtw89_leave_ps_mode(rtwdev);
309 
310 	*new_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_FCSFAIL |
311 		      FIF_BCN_PRBRESP_PROMISC | FIF_PROBE_REQ;
312 
313 	if (changed_flags & FIF_ALLMULTI) {
314 		if (*new_flags & FIF_ALLMULTI)
315 			rtwdev->hal.rx_fltr &= ~B_AX_A_MC;
316 		else
317 			rtwdev->hal.rx_fltr |= B_AX_A_MC;
318 	}
319 	if (changed_flags & FIF_FCSFAIL) {
320 		if (*new_flags & FIF_FCSFAIL)
321 			rtwdev->hal.rx_fltr |= B_AX_A_CRC32_ERR;
322 		else
323 			rtwdev->hal.rx_fltr &= ~B_AX_A_CRC32_ERR;
324 	}
325 	if (changed_flags & FIF_OTHER_BSS) {
326 		if (*new_flags & FIF_OTHER_BSS)
327 			rtwdev->hal.rx_fltr &= ~B_AX_A_A1_MATCH;
328 		else
329 			rtwdev->hal.rx_fltr |= B_AX_A_A1_MATCH;
330 	}
331 	if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
332 		if (*new_flags & FIF_BCN_PRBRESP_PROMISC) {
333 			rtwdev->hal.rx_fltr &= ~B_AX_A_BCN_CHK_EN;
334 			rtwdev->hal.rx_fltr &= ~B_AX_A_BC;
335 			rtwdev->hal.rx_fltr &= ~B_AX_A_A1_MATCH;
336 		} else {
337 			rtwdev->hal.rx_fltr |= B_AX_A_BCN_CHK_EN;
338 			rtwdev->hal.rx_fltr |= B_AX_A_BC;
339 			rtwdev->hal.rx_fltr |= B_AX_A_A1_MATCH;
340 		}
341 	}
342 	if (changed_flags & FIF_PROBE_REQ) {
343 		if (*new_flags & FIF_PROBE_REQ) {
344 			rtwdev->hal.rx_fltr &= ~B_AX_A_BC_CAM_MATCH;
345 			rtwdev->hal.rx_fltr &= ~B_AX_A_UC_CAM_MATCH;
346 		} else {
347 			rtwdev->hal.rx_fltr |= B_AX_A_BC_CAM_MATCH;
348 			rtwdev->hal.rx_fltr |= B_AX_A_UC_CAM_MATCH;
349 		}
350 	}
351 
352 	rx_fltr = rtwdev->hal.rx_fltr;
353 
354 	/* mac80211 doesn't configure filter when HW scan, driver need to
355 	 * set by itself. However, during P2P scan might have configure
356 	 * filter to overwrite filter that HW scan needed, so we need to
357 	 * check scan and append related filter
358 	 */
359 	if (rtwdev->scanning) {
360 		rx_fltr &= ~B_AX_A_BCN_CHK_EN;
361 		rx_fltr &= ~B_AX_A_BC;
362 		rx_fltr &= ~B_AX_A_A1_MATCH;
363 	}
364 
365 	rtw89_write32_mask(rtwdev,
366 			   rtw89_mac_reg_by_idx(rtwdev, mac->rx_fltr, RTW89_MAC_0),
367 			   B_AX_RX_FLTR_CFG_MASK,
368 			   rx_fltr);
369 	if (!rtwdev->dbcc_en)
370 		goto out;
371 	rtw89_write32_mask(rtwdev,
372 			   rtw89_mac_reg_by_idx(rtwdev, mac->rx_fltr, RTW89_MAC_1),
373 			   B_AX_RX_FLTR_CFG_MASK,
374 			   rx_fltr);
375 
376 out:
377 	mutex_unlock(&rtwdev->mutex);
378 }
379 
380 static const u8 ac_to_fw_idx[IEEE80211_NUM_ACS] = {
381 	[IEEE80211_AC_VO] = 3,
382 	[IEEE80211_AC_VI] = 2,
383 	[IEEE80211_AC_BE] = 0,
384 	[IEEE80211_AC_BK] = 1,
385 };
386 
387 static u8 rtw89_aifsn_to_aifs(struct rtw89_dev *rtwdev,
388 			      struct rtw89_vif_link *rtwvif_link, u8 aifsn)
389 {
390 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev,
391 						       rtwvif_link->chanctx_idx);
392 	struct ieee80211_bss_conf *bss_conf;
393 	u8 slot_time;
394 	u8 sifs;
395 
396 	rcu_read_lock();
397 
398 	bss_conf = rtw89_vif_rcu_dereference_link(rtwvif_link, true);
399 	slot_time = bss_conf->use_short_slot ? 9 : 20;
400 
401 	rcu_read_unlock();
402 
403 	sifs = chan->band_type == RTW89_BAND_2G ? 10 : 16;
404 
405 	return aifsn * slot_time + sifs;
406 }
407 
408 static void ____rtw89_conf_tx_edca(struct rtw89_dev *rtwdev,
409 				   struct rtw89_vif_link *rtwvif_link, u16 ac)
410 {
411 	struct ieee80211_tx_queue_params *params = &rtwvif_link->tx_params[ac];
412 	u32 val;
413 	u8 ecw_max, ecw_min;
414 	u8 aifs;
415 
416 	/* 2^ecw - 1 = cw; ecw = log2(cw + 1) */
417 	ecw_max = ilog2(params->cw_max + 1);
418 	ecw_min = ilog2(params->cw_min + 1);
419 	aifs = rtw89_aifsn_to_aifs(rtwdev, rtwvif_link, params->aifs);
420 	val = FIELD_PREP(FW_EDCA_PARAM_TXOPLMT_MSK, params->txop) |
421 	      FIELD_PREP(FW_EDCA_PARAM_CWMAX_MSK, ecw_max) |
422 	      FIELD_PREP(FW_EDCA_PARAM_CWMIN_MSK, ecw_min) |
423 	      FIELD_PREP(FW_EDCA_PARAM_AIFS_MSK, aifs);
424 	rtw89_fw_h2c_set_edca(rtwdev, rtwvif_link, ac_to_fw_idx[ac], val);
425 }
426 
427 #define R_MUEDCA_ACS_PARAM(acs) {R_AX_MUEDCA_ ## acs ## _PARAM_0, \
428 				 R_BE_MUEDCA_ ## acs ## _PARAM_0}
429 
430 static const u32 ac_to_mu_edca_param[IEEE80211_NUM_ACS][RTW89_CHIP_GEN_NUM] = {
431 	[IEEE80211_AC_VO] = R_MUEDCA_ACS_PARAM(VO),
432 	[IEEE80211_AC_VI] = R_MUEDCA_ACS_PARAM(VI),
433 	[IEEE80211_AC_BE] = R_MUEDCA_ACS_PARAM(BE),
434 	[IEEE80211_AC_BK] = R_MUEDCA_ACS_PARAM(BK),
435 };
436 
437 static void ____rtw89_conf_tx_mu_edca(struct rtw89_dev *rtwdev,
438 				      struct rtw89_vif_link *rtwvif_link, u16 ac)
439 {
440 	struct ieee80211_tx_queue_params *params = &rtwvif_link->tx_params[ac];
441 	struct ieee80211_he_mu_edca_param_ac_rec *mu_edca;
442 	int gen = rtwdev->chip->chip_gen;
443 	u8 aifs, aifsn;
444 	u16 timer_32us;
445 	u32 reg;
446 	u32 val;
447 
448 	if (!params->mu_edca)
449 		return;
450 
451 	mu_edca = &params->mu_edca_param_rec;
452 	aifsn = FIELD_GET(GENMASK(3, 0), mu_edca->aifsn);
453 	aifs = aifsn ? rtw89_aifsn_to_aifs(rtwdev, rtwvif_link, aifsn) : 0;
454 	timer_32us = mu_edca->mu_edca_timer << 8;
455 
456 	val = FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_TIMER_MASK, timer_32us) |
457 	      FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_CW_MASK, mu_edca->ecw_min_max) |
458 	      FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_AIFS_MASK, aifs);
459 	reg = rtw89_mac_reg_by_idx(rtwdev, ac_to_mu_edca_param[ac][gen],
460 				   rtwvif_link->mac_idx);
461 	rtw89_write32(rtwdev, reg, val);
462 
463 	rtw89_mac_set_hw_muedca_ctrl(rtwdev, rtwvif_link, true);
464 }
465 
466 static void __rtw89_conf_tx(struct rtw89_dev *rtwdev,
467 			    struct rtw89_vif_link *rtwvif_link, u16 ac)
468 {
469 	____rtw89_conf_tx_edca(rtwdev, rtwvif_link, ac);
470 	____rtw89_conf_tx_mu_edca(rtwdev, rtwvif_link, ac);
471 }
472 
473 static void rtw89_conf_tx(struct rtw89_dev *rtwdev,
474 			  struct rtw89_vif_link *rtwvif_link)
475 {
476 	u16 ac;
477 
478 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
479 		__rtw89_conf_tx(rtwdev, rtwvif_link, ac);
480 }
481 
482 static int __rtw89_ops_sta_add(struct rtw89_dev *rtwdev,
483 			       struct ieee80211_vif *vif,
484 			       struct ieee80211_sta *sta)
485 {
486 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
487 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
488 	struct rtw89_vif_link *rtwvif_link;
489 	struct rtw89_sta_link *rtwsta_link;
490 	bool acquire_macid = false;
491 	u8 macid;
492 	int ret;
493 	int i;
494 
495 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
496 		/* for station mode, assign the mac_id from itself */
497 		macid = rtw89_vif_get_main_macid(rtwvif);
498 	} else {
499 		macid = rtw89_acquire_mac_id(rtwdev);
500 		if (macid == RTW89_MAX_MAC_ID_NUM)
501 			return -ENOSPC;
502 
503 		acquire_macid = true;
504 	}
505 
506 	rtw89_init_sta(rtwdev, rtwvif, rtwsta, macid);
507 
508 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
509 		rtw89_core_txq_init(rtwdev, sta->txq[i]);
510 
511 	skb_queue_head_init(&rtwsta->roc_queue);
512 
513 	rtwsta_link = rtw89_sta_set_link(rtwsta, sta->deflink.link_id);
514 	if (!rtwsta_link) {
515 		ret = -EINVAL;
516 		goto err;
517 	}
518 
519 	rtwvif_link = rtwsta_link->rtwvif_link;
520 
521 	ret = rtw89_core_sta_link_add(rtwdev, rtwvif_link, rtwsta_link);
522 	if (ret)
523 		goto unset_link;
524 
525 	if (vif->type == NL80211_IFTYPE_AP || sta->tdls)
526 		rtw89_queue_chanctx_change(rtwdev, RTW89_CHANCTX_REMOTE_STA_CHANGE);
527 
528 	return 0;
529 
530 unset_link:
531 	rtw89_sta_unset_link(rtwsta, sta->deflink.link_id);
532 err:
533 	if (acquire_macid)
534 		rtw89_release_mac_id(rtwdev, macid);
535 
536 	return ret;
537 }
538 
539 static int __rtw89_ops_sta_assoc(struct rtw89_dev *rtwdev,
540 				 struct ieee80211_vif *vif,
541 				 struct ieee80211_sta *sta,
542 				 bool station_mode)
543 {
544 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
545 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
546 	struct rtw89_vif_link *rtwvif_link;
547 	struct rtw89_sta_link *rtwsta_link;
548 	unsigned int link_id;
549 	int ret;
550 
551 	rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) {
552 		rtwvif_link = rtwsta_link->rtwvif_link;
553 
554 		if (station_mode)
555 			rtw89_vif_type_mapping(rtwvif_link, true);
556 
557 		ret = rtw89_core_sta_link_assoc(rtwdev, rtwvif_link, rtwsta_link);
558 		if (ret)
559 			return ret;
560 	}
561 
562 	rtwdev->total_sta_assoc++;
563 	if (sta->tdls)
564 		rtwvif->tdls_peer++;
565 
566 	return 0;
567 }
568 
569 static int __rtw89_ops_sta_disassoc(struct rtw89_dev *rtwdev,
570 				    struct ieee80211_vif *vif,
571 				    struct ieee80211_sta *sta)
572 {
573 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
574 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
575 	struct rtw89_vif_link *rtwvif_link;
576 	struct rtw89_sta_link *rtwsta_link;
577 	unsigned int link_id;
578 	int ret;
579 
580 	rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) {
581 		rtwvif_link = rtwsta_link->rtwvif_link;
582 		ret = rtw89_core_sta_link_disassoc(rtwdev, rtwvif_link, rtwsta_link);
583 		if (ret)
584 			return ret;
585 	}
586 
587 	rtwsta->disassoc = true;
588 
589 	rtwdev->total_sta_assoc--;
590 	if (sta->tdls)
591 		rtwvif->tdls_peer--;
592 
593 	return 0;
594 }
595 
596 static int __rtw89_ops_sta_disconnect(struct rtw89_dev *rtwdev,
597 				      struct ieee80211_vif *vif,
598 				      struct ieee80211_sta *sta)
599 {
600 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
601 	struct rtw89_vif_link *rtwvif_link;
602 	struct rtw89_sta_link *rtwsta_link;
603 	unsigned int link_id;
604 	int ret;
605 
606 	rtw89_core_free_sta_pending_ba(rtwdev, sta);
607 	rtw89_core_free_sta_pending_forbid_ba(rtwdev, sta);
608 	rtw89_core_free_sta_pending_roc_tx(rtwdev, sta);
609 
610 	rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) {
611 		rtwvif_link = rtwsta_link->rtwvif_link;
612 		ret = rtw89_core_sta_link_disconnect(rtwdev, rtwvif_link, rtwsta_link);
613 		if (ret)
614 			return ret;
615 	}
616 
617 	return 0;
618 }
619 
620 static int __rtw89_ops_sta_remove(struct rtw89_dev *rtwdev,
621 				  struct ieee80211_vif *vif,
622 				  struct ieee80211_sta *sta)
623 {
624 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
625 	u8 macid = rtw89_sta_get_main_macid(rtwsta);
626 	struct rtw89_vif_link *rtwvif_link;
627 	struct rtw89_sta_link *rtwsta_link;
628 	unsigned int link_id;
629 	int ret;
630 
631 	rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) {
632 		rtwvif_link = rtwsta_link->rtwvif_link;
633 		ret = rtw89_core_sta_link_remove(rtwdev, rtwvif_link, rtwsta_link);
634 		if (ret)
635 			return ret;
636 
637 		rtw89_sta_unset_link(rtwsta, link_id);
638 	}
639 
640 	if (vif->type == NL80211_IFTYPE_AP || sta->tdls) {
641 		rtw89_release_mac_id(rtwdev, macid);
642 		rtw89_queue_chanctx_change(rtwdev, RTW89_CHANCTX_REMOTE_STA_CHANGE);
643 	}
644 
645 	return 0;
646 }
647 
648 static void rtw89_station_mode_sta_assoc(struct rtw89_dev *rtwdev,
649 					 struct ieee80211_vif *vif)
650 {
651 	struct ieee80211_sta *sta;
652 
653 	if (vif->type != NL80211_IFTYPE_STATION)
654 		return;
655 
656 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
657 	if (!sta) {
658 		rtw89_err(rtwdev, "can't find sta to set sta_assoc state\n");
659 		return;
660 	}
661 
662 	__rtw89_ops_sta_assoc(rtwdev, vif, sta, true);
663 }
664 
665 static void __rtw89_ops_bss_link_assoc(struct rtw89_dev *rtwdev,
666 				       struct rtw89_vif_link *rtwvif_link)
667 {
668 	rtw89_phy_set_bss_color(rtwdev, rtwvif_link);
669 	rtw89_chip_cfg_txpwr_ul_tb_offset(rtwdev, rtwvif_link);
670 	rtw89_mac_port_update(rtwdev, rtwvif_link);
671 	rtw89_mac_set_he_obss_narrow_bw_ru(rtwdev, rtwvif_link);
672 }
673 
674 static void __rtw89_ops_bss_assoc(struct rtw89_dev *rtwdev,
675 				  struct ieee80211_vif *vif)
676 {
677 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
678 	struct rtw89_vif_link *rtwvif_link;
679 	unsigned int link_id;
680 
681 	rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id)
682 		__rtw89_ops_bss_link_assoc(rtwdev, rtwvif_link);
683 }
684 
685 static void rtw89_ops_vif_cfg_changed(struct ieee80211_hw *hw,
686 				      struct ieee80211_vif *vif, u64 changed)
687 {
688 	struct rtw89_dev *rtwdev = hw->priv;
689 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
690 
691 	mutex_lock(&rtwdev->mutex);
692 	rtw89_leave_ps_mode(rtwdev);
693 
694 	if (changed & BSS_CHANGED_ASSOC) {
695 		if (vif->cfg.assoc) {
696 			rtw89_station_mode_sta_assoc(rtwdev, vif);
697 			__rtw89_ops_bss_assoc(rtwdev, vif);
698 
699 			rtw89_queue_chanctx_work(rtwdev);
700 		} else {
701 			/* Abort ongoing scan if cancel_scan isn't issued
702 			 * when disconnected by peer
703 			 */
704 			if (rtwdev->scanning)
705 				rtw89_hw_scan_abort(rtwdev, rtwdev->scan_info.scanning_vif);
706 		}
707 	}
708 
709 	if (changed & BSS_CHANGED_PS)
710 		rtw89_recalc_lps(rtwdev);
711 
712 	if (changed & BSS_CHANGED_ARP_FILTER)
713 		rtwvif->ip_addr = vif->cfg.arp_addr_list[0];
714 
715 	mutex_unlock(&rtwdev->mutex);
716 }
717 
718 static void rtw89_ops_link_info_changed(struct ieee80211_hw *hw,
719 					struct ieee80211_vif *vif,
720 					struct ieee80211_bss_conf *conf,
721 					u64 changed)
722 {
723 	struct rtw89_dev *rtwdev = hw->priv;
724 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
725 	struct rtw89_vif_link *rtwvif_link;
726 
727 	mutex_lock(&rtwdev->mutex);
728 	rtw89_leave_ps_mode(rtwdev);
729 
730 	rtwvif_link = rtwvif->links[conf->link_id];
731 	if (unlikely(!rtwvif_link)) {
732 		rtw89_err(rtwdev,
733 			  "%s: rtwvif link (link_id %u) is not active\n",
734 			  __func__, conf->link_id);
735 		goto out;
736 	}
737 
738 	if (changed & BSS_CHANGED_BSSID) {
739 		ether_addr_copy(rtwvif_link->bssid, conf->bssid);
740 		rtw89_cam_bssid_changed(rtwdev, rtwvif_link);
741 		rtw89_fw_h2c_cam(rtwdev, rtwvif_link, NULL, NULL);
742 		WRITE_ONCE(rtwvif_link->sync_bcn_tsf, 0);
743 	}
744 
745 	if (changed & BSS_CHANGED_BEACON)
746 		rtw89_chip_h2c_update_beacon(rtwdev, rtwvif_link);
747 
748 	if (changed & BSS_CHANGED_ERP_SLOT)
749 		rtw89_conf_tx(rtwdev, rtwvif_link);
750 
751 	if (changed & BSS_CHANGED_HE_BSS_COLOR)
752 		rtw89_phy_set_bss_color(rtwdev, rtwvif_link);
753 
754 	if (changed & BSS_CHANGED_MU_GROUPS)
755 		rtw89_mac_bf_set_gid_table(rtwdev, vif, conf);
756 
757 	if (changed & BSS_CHANGED_P2P_PS)
758 		rtw89_core_update_p2p_ps(rtwdev, rtwvif_link, conf);
759 
760 	if (changed & BSS_CHANGED_CQM)
761 		rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, rtwvif_link, true);
762 
763 	if (changed & BSS_CHANGED_TPE)
764 		rtw89_reg_6ghz_recalc(rtwdev, rtwvif_link, true);
765 
766 out:
767 	mutex_unlock(&rtwdev->mutex);
768 }
769 
770 static int rtw89_ops_start_ap(struct ieee80211_hw *hw,
771 			      struct ieee80211_vif *vif,
772 			      struct ieee80211_bss_conf *link_conf)
773 {
774 	struct rtw89_dev *rtwdev = hw->priv;
775 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
776 	struct rtw89_vif_link *rtwvif_link;
777 	const struct rtw89_chan *chan;
778 
779 	mutex_lock(&rtwdev->mutex);
780 
781 	rtwvif_link = rtwvif->links[link_conf->link_id];
782 	if (unlikely(!rtwvif_link)) {
783 		rtw89_err(rtwdev,
784 			  "%s: rtwvif link (link_id %u) is not active\n",
785 			  __func__, link_conf->link_id);
786 		goto out;
787 	}
788 
789 	chan = rtw89_chan_get(rtwdev, rtwvif_link->chanctx_idx);
790 	if (chan->band_type == RTW89_BAND_6G) {
791 		mutex_unlock(&rtwdev->mutex);
792 		return -EOPNOTSUPP;
793 	}
794 
795 	if (rtwdev->scanning)
796 		rtw89_hw_scan_abort(rtwdev, rtwdev->scan_info.scanning_vif);
797 
798 	ether_addr_copy(rtwvif_link->bssid, link_conf->bssid);
799 	rtw89_cam_bssid_changed(rtwdev, rtwvif_link);
800 	rtw89_mac_port_update(rtwdev, rtwvif_link);
801 	rtw89_chip_h2c_assoc_cmac_tbl(rtwdev, rtwvif_link, NULL);
802 	rtw89_fw_h2c_role_maintain(rtwdev, rtwvif_link, NULL, RTW89_ROLE_TYPE_CHANGE);
803 	rtw89_fw_h2c_join_info(rtwdev, rtwvif_link, NULL, true);
804 	rtw89_fw_h2c_cam(rtwdev, rtwvif_link, NULL, NULL);
805 	rtw89_chip_rfk_channel(rtwdev, rtwvif_link);
806 
807 	rtw89_queue_chanctx_work(rtwdev);
808 
809 out:
810 	mutex_unlock(&rtwdev->mutex);
811 
812 	return 0;
813 }
814 
815 static
816 void rtw89_ops_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
817 		       struct ieee80211_bss_conf *link_conf)
818 {
819 	struct rtw89_dev *rtwdev = hw->priv;
820 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
821 	struct rtw89_vif_link *rtwvif_link;
822 
823 	mutex_lock(&rtwdev->mutex);
824 
825 	rtwvif_link = rtwvif->links[link_conf->link_id];
826 	if (unlikely(!rtwvif_link)) {
827 		rtw89_err(rtwdev,
828 			  "%s: rtwvif link (link_id %u) is not active\n",
829 			  __func__, link_conf->link_id);
830 		goto out;
831 	}
832 
833 	rtw89_mac_stop_ap(rtwdev, rtwvif_link);
834 	rtw89_chip_h2c_assoc_cmac_tbl(rtwdev, rtwvif_link, NULL);
835 	rtw89_fw_h2c_join_info(rtwdev, rtwvif_link, NULL, true);
836 
837 out:
838 	mutex_unlock(&rtwdev->mutex);
839 }
840 
841 static int rtw89_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
842 			     bool set)
843 {
844 	struct rtw89_dev *rtwdev = hw->priv;
845 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
846 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
847 	struct rtw89_vif_link *rtwvif_link;
848 	unsigned int link_id;
849 
850 	rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id)
851 		ieee80211_queue_work(rtwdev->hw, &rtwvif_link->update_beacon_work);
852 
853 	return 0;
854 }
855 
856 static int rtw89_ops_conf_tx(struct ieee80211_hw *hw,
857 			     struct ieee80211_vif *vif,
858 			     unsigned int link_id, u16 ac,
859 			     const struct ieee80211_tx_queue_params *params)
860 {
861 	struct rtw89_dev *rtwdev = hw->priv;
862 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
863 	struct rtw89_vif_link *rtwvif_link;
864 	int ret = 0;
865 
866 	mutex_lock(&rtwdev->mutex);
867 	rtw89_leave_ps_mode(rtwdev);
868 
869 	rtwvif_link = rtwvif->links[link_id];
870 	if (unlikely(!rtwvif_link)) {
871 		rtw89_err(rtwdev,
872 			  "%s: rtwvif link (link_id %u) is not active\n",
873 			  __func__, link_id);
874 		ret = -ENOLINK;
875 		goto out;
876 	}
877 
878 	rtwvif_link->tx_params[ac] = *params;
879 	__rtw89_conf_tx(rtwdev, rtwvif_link, ac);
880 
881 out:
882 	mutex_unlock(&rtwdev->mutex);
883 
884 	return ret;
885 }
886 
887 static int __rtw89_ops_sta_state(struct ieee80211_hw *hw,
888 				 struct ieee80211_vif *vif,
889 				 struct ieee80211_sta *sta,
890 				 enum ieee80211_sta_state old_state,
891 				 enum ieee80211_sta_state new_state)
892 {
893 	struct rtw89_dev *rtwdev = hw->priv;
894 
895 	if (old_state == IEEE80211_STA_NOTEXIST &&
896 	    new_state == IEEE80211_STA_NONE)
897 		return __rtw89_ops_sta_add(rtwdev, vif, sta);
898 
899 	if (old_state == IEEE80211_STA_AUTH &&
900 	    new_state == IEEE80211_STA_ASSOC) {
901 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
902 			return 0; /* defer to bss_info_changed to have vif info */
903 		return __rtw89_ops_sta_assoc(rtwdev, vif, sta, false);
904 	}
905 
906 	if (old_state == IEEE80211_STA_ASSOC &&
907 	    new_state == IEEE80211_STA_AUTH)
908 		return __rtw89_ops_sta_disassoc(rtwdev, vif, sta);
909 
910 	if (old_state == IEEE80211_STA_AUTH &&
911 	    new_state == IEEE80211_STA_NONE)
912 		return __rtw89_ops_sta_disconnect(rtwdev, vif, sta);
913 
914 	if (old_state == IEEE80211_STA_NONE &&
915 	    new_state == IEEE80211_STA_NOTEXIST)
916 		return __rtw89_ops_sta_remove(rtwdev, vif, sta);
917 
918 	return 0;
919 }
920 
921 static int rtw89_ops_sta_state(struct ieee80211_hw *hw,
922 			       struct ieee80211_vif *vif,
923 			       struct ieee80211_sta *sta,
924 			       enum ieee80211_sta_state old_state,
925 			       enum ieee80211_sta_state new_state)
926 {
927 	struct rtw89_dev *rtwdev = hw->priv;
928 	int ret;
929 
930 	mutex_lock(&rtwdev->mutex);
931 	rtw89_leave_ps_mode(rtwdev);
932 	ret = __rtw89_ops_sta_state(hw, vif, sta, old_state, new_state);
933 	mutex_unlock(&rtwdev->mutex);
934 
935 	return ret;
936 }
937 
938 static int rtw89_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
939 			     struct ieee80211_vif *vif,
940 			     struct ieee80211_sta *sta,
941 			     struct ieee80211_key_conf *key)
942 {
943 	struct rtw89_dev *rtwdev = hw->priv;
944 	int ret = 0;
945 
946 	mutex_lock(&rtwdev->mutex);
947 	rtw89_leave_ps_mode(rtwdev);
948 
949 	switch (cmd) {
950 	case SET_KEY:
951 		rtw89_btc_ntfy_specific_packet(rtwdev, PACKET_EAPOL_END);
952 		ret = rtw89_cam_sec_key_add(rtwdev, vif, sta, key);
953 		if (ret && ret != -EOPNOTSUPP) {
954 			rtw89_err(rtwdev, "failed to add key to sec cam\n");
955 			goto out;
956 		}
957 		break;
958 	case DISABLE_KEY:
959 		rtw89_hci_flush_queues(rtwdev, BIT(rtwdev->hw->queues) - 1,
960 				       false);
961 		rtw89_mac_flush_txq(rtwdev, BIT(rtwdev->hw->queues) - 1, false);
962 		ret = rtw89_cam_sec_key_del(rtwdev, vif, sta, key, true);
963 		if (ret) {
964 			rtw89_err(rtwdev, "failed to remove key from sec cam\n");
965 			goto out;
966 		}
967 		break;
968 	}
969 
970 out:
971 	mutex_unlock(&rtwdev->mutex);
972 
973 	return ret;
974 }
975 
976 static int rtw89_ops_ampdu_action(struct ieee80211_hw *hw,
977 				  struct ieee80211_vif *vif,
978 				  struct ieee80211_ampdu_params *params)
979 {
980 	struct rtw89_dev *rtwdev = hw->priv;
981 	struct ieee80211_sta *sta = params->sta;
982 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
983 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
984 	u16 tid = params->tid;
985 	struct ieee80211_txq *txq = sta->txq[tid];
986 	struct rtw89_txq *rtwtxq = (struct rtw89_txq *)txq->drv_priv;
987 
988 	switch (params->action) {
989 	case IEEE80211_AMPDU_TX_START:
990 		return IEEE80211_AMPDU_TX_START_IMMEDIATE;
991 	case IEEE80211_AMPDU_TX_STOP_CONT:
992 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
993 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
994 		mutex_lock(&rtwdev->mutex);
995 		clear_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags);
996 		clear_bit(tid, rtwsta->ampdu_map);
997 		rtw89_chip_h2c_ampdu_cmac_tbl(rtwdev, rtwvif, rtwsta);
998 		mutex_unlock(&rtwdev->mutex);
999 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1000 		break;
1001 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1002 		mutex_lock(&rtwdev->mutex);
1003 		set_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags);
1004 		rtwsta->ampdu_params[tid].agg_num = params->buf_size;
1005 		rtwsta->ampdu_params[tid].amsdu = params->amsdu;
1006 		set_bit(tid, rtwsta->ampdu_map);
1007 		rtw89_leave_ps_mode(rtwdev);
1008 		rtw89_chip_h2c_ampdu_cmac_tbl(rtwdev, rtwvif, rtwsta);
1009 		mutex_unlock(&rtwdev->mutex);
1010 		break;
1011 	case IEEE80211_AMPDU_RX_START:
1012 		mutex_lock(&rtwdev->mutex);
1013 		rtw89_chip_h2c_ba_cam(rtwdev, rtwsta, true, params);
1014 		mutex_unlock(&rtwdev->mutex);
1015 		break;
1016 	case IEEE80211_AMPDU_RX_STOP:
1017 		mutex_lock(&rtwdev->mutex);
1018 		rtw89_chip_h2c_ba_cam(rtwdev, rtwsta, false, params);
1019 		mutex_unlock(&rtwdev->mutex);
1020 		break;
1021 	default:
1022 		WARN_ON(1);
1023 		return -ENOTSUPP;
1024 	}
1025 
1026 	return 0;
1027 }
1028 
1029 static int rtw89_ops_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1030 {
1031 	struct rtw89_dev *rtwdev = hw->priv;
1032 
1033 	mutex_lock(&rtwdev->mutex);
1034 	rtw89_leave_ps_mode(rtwdev);
1035 	if (test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
1036 		rtw89_mac_update_rts_threshold(rtwdev);
1037 	mutex_unlock(&rtwdev->mutex);
1038 
1039 	return 0;
1040 }
1041 
1042 static void rtw89_ops_sta_statistics(struct ieee80211_hw *hw,
1043 				     struct ieee80211_vif *vif,
1044 				     struct ieee80211_sta *sta,
1045 				     struct station_info *sinfo)
1046 {
1047 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
1048 	struct rtw89_sta_link *rtwsta_link;
1049 
1050 	rtwsta_link = rtw89_sta_get_link_inst(rtwsta, 0);
1051 	if (unlikely(!rtwsta_link))
1052 		return;
1053 
1054 	sinfo->txrate = rtwsta_link->ra_report.txrate;
1055 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1056 }
1057 
1058 static
1059 void __rtw89_drop_packets(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif)
1060 {
1061 	struct rtw89_vif *rtwvif;
1062 
1063 	if (vif) {
1064 		rtwvif = vif_to_rtwvif(vif);
1065 		rtw89_mac_pkt_drop_vif(rtwdev, rtwvif);
1066 	} else {
1067 		rtw89_for_each_rtwvif(rtwdev, rtwvif)
1068 			rtw89_mac_pkt_drop_vif(rtwdev, rtwvif);
1069 	}
1070 }
1071 
1072 static void rtw89_ops_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1073 			    u32 queues, bool drop)
1074 {
1075 	struct rtw89_dev *rtwdev = hw->priv;
1076 
1077 	mutex_lock(&rtwdev->mutex);
1078 	rtw89_leave_lps(rtwdev);
1079 	rtw89_hci_flush_queues(rtwdev, queues, drop);
1080 
1081 	if (drop && !RTW89_CHK_FW_FEATURE(NO_PACKET_DROP, &rtwdev->fw))
1082 		__rtw89_drop_packets(rtwdev, vif);
1083 	else
1084 		rtw89_mac_flush_txq(rtwdev, queues, drop);
1085 
1086 	mutex_unlock(&rtwdev->mutex);
1087 }
1088 
1089 struct rtw89_iter_bitrate_mask_data {
1090 	struct rtw89_dev *rtwdev;
1091 	struct ieee80211_vif *vif;
1092 	const struct cfg80211_bitrate_mask *mask;
1093 };
1094 
1095 static void rtw89_ra_mask_info_update_iter(void *data, struct ieee80211_sta *sta)
1096 {
1097 	struct rtw89_iter_bitrate_mask_data *br_data = data;
1098 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
1099 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
1100 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1101 	struct rtw89_sta_link *rtwsta_link;
1102 	unsigned int link_id;
1103 
1104 	if (vif != br_data->vif || vif->p2p)
1105 		return;
1106 
1107 	rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) {
1108 		rtwsta_link->use_cfg_mask = true;
1109 		rtwsta_link->mask = *br_data->mask;
1110 	}
1111 
1112 	rtw89_phy_ra_update_sta(br_data->rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED);
1113 }
1114 
1115 static void rtw89_ra_mask_info_update(struct rtw89_dev *rtwdev,
1116 				      struct ieee80211_vif *vif,
1117 				      const struct cfg80211_bitrate_mask *mask)
1118 {
1119 	struct rtw89_iter_bitrate_mask_data br_data = { .rtwdev = rtwdev,
1120 							.vif = vif,
1121 							.mask = mask};
1122 
1123 	ieee80211_iterate_stations_atomic(rtwdev->hw, rtw89_ra_mask_info_update_iter,
1124 					  &br_data);
1125 }
1126 
1127 static int rtw89_ops_set_bitrate_mask(struct ieee80211_hw *hw,
1128 				      struct ieee80211_vif *vif,
1129 				      const struct cfg80211_bitrate_mask *mask)
1130 {
1131 	struct rtw89_dev *rtwdev = hw->priv;
1132 
1133 	mutex_lock(&rtwdev->mutex);
1134 	rtw89_phy_rate_pattern_vif(rtwdev, vif, mask);
1135 	rtw89_ra_mask_info_update(rtwdev, vif, mask);
1136 	mutex_unlock(&rtwdev->mutex);
1137 
1138 	return 0;
1139 }
1140 
1141 static
1142 int rtw89_ops_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1143 {
1144 	struct rtw89_dev *rtwdev = hw->priv;
1145 	struct rtw89_hal *hal = &rtwdev->hal;
1146 
1147 	if (hal->ant_diversity) {
1148 		if (tx_ant != rx_ant || hweight32(tx_ant) != 1)
1149 			return -EINVAL;
1150 	} else if (rx_ant != hw->wiphy->available_antennas_rx && rx_ant != hal->antenna_rx) {
1151 		return -EINVAL;
1152 	}
1153 
1154 	mutex_lock(&rtwdev->mutex);
1155 	hal->antenna_tx = tx_ant;
1156 	hal->antenna_rx = rx_ant;
1157 	hal->tx_path_diversity = false;
1158 	hal->ant_diversity_fixed = true;
1159 	mutex_unlock(&rtwdev->mutex);
1160 
1161 	return 0;
1162 }
1163 
1164 static
1165 int rtw89_ops_get_antenna(struct ieee80211_hw *hw,  u32 *tx_ant, u32 *rx_ant)
1166 {
1167 	struct rtw89_dev *rtwdev = hw->priv;
1168 	struct rtw89_hal *hal = &rtwdev->hal;
1169 
1170 	*tx_ant = hal->antenna_tx;
1171 	*rx_ant = hal->antenna_rx;
1172 
1173 	return 0;
1174 }
1175 
1176 static void rtw89_ops_sw_scan_start(struct ieee80211_hw *hw,
1177 				    struct ieee80211_vif *vif,
1178 				    const u8 *mac_addr)
1179 {
1180 	struct rtw89_dev *rtwdev = hw->priv;
1181 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1182 	struct rtw89_vif_link *rtwvif_link;
1183 
1184 	mutex_lock(&rtwdev->mutex);
1185 
1186 	rtwvif_link = rtw89_vif_get_link_inst(rtwvif, 0);
1187 	if (unlikely(!rtwvif_link)) {
1188 		rtw89_err(rtwdev, "sw scan start: find no link on HW-0\n");
1189 		goto out;
1190 	}
1191 
1192 	rtw89_core_scan_start(rtwdev, rtwvif_link, mac_addr, false);
1193 
1194 out:
1195 	mutex_unlock(&rtwdev->mutex);
1196 }
1197 
1198 static void rtw89_ops_sw_scan_complete(struct ieee80211_hw *hw,
1199 				       struct ieee80211_vif *vif)
1200 {
1201 	struct rtw89_dev *rtwdev = hw->priv;
1202 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1203 	struct rtw89_vif_link *rtwvif_link;
1204 
1205 	mutex_lock(&rtwdev->mutex);
1206 
1207 	rtwvif_link = rtw89_vif_get_link_inst(rtwvif, 0);
1208 	if (unlikely(!rtwvif_link)) {
1209 		rtw89_err(rtwdev, "sw scan complete: find no link on HW-0\n");
1210 		goto out;
1211 	}
1212 
1213 	rtw89_core_scan_complete(rtwdev, rtwvif_link, false);
1214 
1215 out:
1216 	mutex_unlock(&rtwdev->mutex);
1217 }
1218 
1219 static void rtw89_ops_reconfig_complete(struct ieee80211_hw *hw,
1220 					enum ieee80211_reconfig_type reconfig_type)
1221 {
1222 	struct rtw89_dev *rtwdev = hw->priv;
1223 
1224 	if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART)
1225 		rtw89_ser_recfg_done(rtwdev);
1226 }
1227 
1228 static int rtw89_ops_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1229 			     struct ieee80211_scan_request *req)
1230 {
1231 	struct rtw89_dev *rtwdev = hw->priv;
1232 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1233 	struct rtw89_vif_link *rtwvif_link;
1234 	int ret;
1235 
1236 	if (!RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw))
1237 		return 1;
1238 
1239 	mutex_lock(&rtwdev->mutex);
1240 
1241 	if (rtwdev->scanning || rtwvif->offchan) {
1242 		ret = -EBUSY;
1243 		goto out;
1244 	}
1245 
1246 	rtwvif_link = rtw89_vif_get_link_inst(rtwvif, 0);
1247 	if (unlikely(!rtwvif_link)) {
1248 		rtw89_err(rtwdev, "hw scan: find no link on HW-0\n");
1249 		ret = -ENOLINK;
1250 		goto out;
1251 	}
1252 
1253 	rtw89_hw_scan_start(rtwdev, rtwvif_link, req);
1254 	ret = rtw89_hw_scan_offload(rtwdev, rtwvif_link, true);
1255 	if (ret) {
1256 		rtw89_hw_scan_abort(rtwdev, rtwvif_link);
1257 		rtw89_err(rtwdev, "HW scan failed with status: %d\n", ret);
1258 	}
1259 
1260 out:
1261 	mutex_unlock(&rtwdev->mutex);
1262 
1263 	return ret;
1264 }
1265 
1266 static void rtw89_ops_cancel_hw_scan(struct ieee80211_hw *hw,
1267 				     struct ieee80211_vif *vif)
1268 {
1269 	struct rtw89_dev *rtwdev = hw->priv;
1270 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1271 	struct rtw89_vif_link *rtwvif_link;
1272 
1273 	if (!RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw))
1274 		return;
1275 
1276 	if (!rtwdev->scanning)
1277 		return;
1278 
1279 	mutex_lock(&rtwdev->mutex);
1280 
1281 	rtwvif_link = rtw89_vif_get_link_inst(rtwvif, 0);
1282 	if (unlikely(!rtwvif_link)) {
1283 		rtw89_err(rtwdev, "cancel hw scan: find no link on HW-0\n");
1284 		goto out;
1285 	}
1286 
1287 	rtw89_hw_scan_abort(rtwdev, rtwvif_link);
1288 
1289 out:
1290 	mutex_unlock(&rtwdev->mutex);
1291 }
1292 
1293 static void rtw89_ops_sta_rc_update(struct ieee80211_hw *hw,
1294 				    struct ieee80211_vif *vif,
1295 				    struct ieee80211_link_sta *link_sta,
1296 				    u32 changed)
1297 {
1298 	struct ieee80211_sta *sta = link_sta->sta;
1299 	struct rtw89_dev *rtwdev = hw->priv;
1300 
1301 	rtw89_phy_ra_update_sta(rtwdev, sta, changed);
1302 }
1303 
1304 static int rtw89_ops_add_chanctx(struct ieee80211_hw *hw,
1305 				 struct ieee80211_chanctx_conf *ctx)
1306 {
1307 	struct rtw89_dev *rtwdev = hw->priv;
1308 	int ret;
1309 
1310 	mutex_lock(&rtwdev->mutex);
1311 	ret = rtw89_chanctx_ops_add(rtwdev, ctx);
1312 	mutex_unlock(&rtwdev->mutex);
1313 
1314 	return ret;
1315 }
1316 
1317 static void rtw89_ops_remove_chanctx(struct ieee80211_hw *hw,
1318 				     struct ieee80211_chanctx_conf *ctx)
1319 {
1320 	struct rtw89_dev *rtwdev = hw->priv;
1321 
1322 	mutex_lock(&rtwdev->mutex);
1323 	rtw89_chanctx_ops_remove(rtwdev, ctx);
1324 	mutex_unlock(&rtwdev->mutex);
1325 }
1326 
1327 static void rtw89_ops_change_chanctx(struct ieee80211_hw *hw,
1328 				     struct ieee80211_chanctx_conf *ctx,
1329 				     u32 changed)
1330 {
1331 	struct rtw89_dev *rtwdev = hw->priv;
1332 
1333 	mutex_lock(&rtwdev->mutex);
1334 	rtw89_chanctx_ops_change(rtwdev, ctx, changed);
1335 	mutex_unlock(&rtwdev->mutex);
1336 }
1337 
1338 static int rtw89_ops_assign_vif_chanctx(struct ieee80211_hw *hw,
1339 					struct ieee80211_vif *vif,
1340 					struct ieee80211_bss_conf *link_conf,
1341 					struct ieee80211_chanctx_conf *ctx)
1342 {
1343 	struct rtw89_dev *rtwdev = hw->priv;
1344 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1345 	struct rtw89_vif_link *rtwvif_link;
1346 	int ret;
1347 
1348 	mutex_lock(&rtwdev->mutex);
1349 
1350 	rtwvif_link = rtwvif->links[link_conf->link_id];
1351 	if (unlikely(!rtwvif_link)) {
1352 		rtw89_err(rtwdev,
1353 			  "%s: rtwvif link (link_id %u) is not active\n",
1354 			  __func__, link_conf->link_id);
1355 		ret = -ENOLINK;
1356 		goto out;
1357 	}
1358 
1359 	ret = rtw89_chanctx_ops_assign_vif(rtwdev, rtwvif_link, ctx);
1360 
1361 out:
1362 	mutex_unlock(&rtwdev->mutex);
1363 
1364 	return ret;
1365 }
1366 
1367 static void rtw89_ops_unassign_vif_chanctx(struct ieee80211_hw *hw,
1368 					   struct ieee80211_vif *vif,
1369 					   struct ieee80211_bss_conf *link_conf,
1370 					   struct ieee80211_chanctx_conf *ctx)
1371 {
1372 	struct rtw89_dev *rtwdev = hw->priv;
1373 	struct rtw89_vif *rtwvif = vif_to_rtwvif(vif);
1374 	struct rtw89_vif_link *rtwvif_link;
1375 
1376 	mutex_lock(&rtwdev->mutex);
1377 
1378 	rtwvif_link = rtwvif->links[link_conf->link_id];
1379 	if (unlikely(!rtwvif_link)) {
1380 		mutex_unlock(&rtwdev->mutex);
1381 		rtw89_err(rtwdev,
1382 			  "%s: rtwvif link (link_id %u) is not active\n",
1383 			  __func__, link_conf->link_id);
1384 		return;
1385 	}
1386 
1387 	rtw89_chanctx_ops_unassign_vif(rtwdev, rtwvif_link, ctx);
1388 	mutex_unlock(&rtwdev->mutex);
1389 }
1390 
1391 static int rtw89_ops_remain_on_channel(struct ieee80211_hw *hw,
1392 				       struct ieee80211_vif *vif,
1393 				       struct ieee80211_channel *chan,
1394 				       int duration,
1395 				       enum ieee80211_roc_type type)
1396 {
1397 	struct rtw89_dev *rtwdev = hw->priv;
1398 	struct rtw89_vif *rtwvif = vif_to_rtwvif_safe(vif);
1399 	struct rtw89_roc *roc = &rtwvif->roc;
1400 
1401 	if (!rtwvif)
1402 		return -EINVAL;
1403 
1404 	mutex_lock(&rtwdev->mutex);
1405 
1406 	if (roc->state != RTW89_ROC_IDLE) {
1407 		mutex_unlock(&rtwdev->mutex);
1408 		return -EBUSY;
1409 	}
1410 
1411 	if (rtwdev->scanning)
1412 		rtw89_hw_scan_abort(rtwdev, rtwdev->scan_info.scanning_vif);
1413 
1414 	if (type == IEEE80211_ROC_TYPE_MGMT_TX)
1415 		roc->state = RTW89_ROC_MGMT;
1416 	else
1417 		roc->state = RTW89_ROC_NORMAL;
1418 
1419 	roc->duration = duration;
1420 	roc->chan = *chan;
1421 	roc->type = type;
1422 
1423 	rtw89_roc_start(rtwdev, rtwvif);
1424 
1425 	mutex_unlock(&rtwdev->mutex);
1426 
1427 	return 0;
1428 }
1429 
1430 static int rtw89_ops_cancel_remain_on_channel(struct ieee80211_hw *hw,
1431 					      struct ieee80211_vif *vif)
1432 {
1433 	struct rtw89_dev *rtwdev = hw->priv;
1434 	struct rtw89_vif *rtwvif = vif_to_rtwvif_safe(vif);
1435 
1436 	if (!rtwvif)
1437 		return -EINVAL;
1438 
1439 	cancel_delayed_work_sync(&rtwvif->roc.roc_work);
1440 
1441 	mutex_lock(&rtwdev->mutex);
1442 	rtw89_roc_end(rtwdev, rtwvif);
1443 	mutex_unlock(&rtwdev->mutex);
1444 
1445 	return 0;
1446 }
1447 
1448 static void rtw89_set_tid_config_iter(void *data, struct ieee80211_sta *sta)
1449 {
1450 	struct cfg80211_tid_config *tid_config = data;
1451 	struct rtw89_sta *rtwsta = sta_to_rtwsta(sta);
1452 	struct rtw89_dev *rtwdev = rtwsta->rtwdev;
1453 
1454 	rtw89_core_set_tid_config(rtwdev, sta, tid_config);
1455 }
1456 
1457 static int rtw89_ops_set_tid_config(struct ieee80211_hw *hw,
1458 				    struct ieee80211_vif *vif,
1459 				    struct ieee80211_sta *sta,
1460 				    struct cfg80211_tid_config *tid_config)
1461 {
1462 	struct rtw89_dev *rtwdev = hw->priv;
1463 
1464 	mutex_lock(&rtwdev->mutex);
1465 	if (sta)
1466 		rtw89_core_set_tid_config(rtwdev, sta, tid_config);
1467 	else
1468 		ieee80211_iterate_stations_atomic(rtwdev->hw,
1469 						  rtw89_set_tid_config_iter,
1470 						  tid_config);
1471 	mutex_unlock(&rtwdev->mutex);
1472 
1473 	return 0;
1474 }
1475 
1476 #ifdef CONFIG_PM
1477 static int rtw89_ops_suspend(struct ieee80211_hw *hw,
1478 			     struct cfg80211_wowlan *wowlan)
1479 {
1480 	struct rtw89_dev *rtwdev = hw->priv;
1481 	int ret;
1482 
1483 	set_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags);
1484 	cancel_delayed_work_sync(&rtwdev->track_work);
1485 
1486 	mutex_lock(&rtwdev->mutex);
1487 	ret = rtw89_wow_suspend(rtwdev, wowlan);
1488 	mutex_unlock(&rtwdev->mutex);
1489 
1490 	if (ret) {
1491 		rtw89_warn(rtwdev, "failed to suspend for wow %d\n", ret);
1492 		clear_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags);
1493 		return 1;
1494 	}
1495 
1496 	return 0;
1497 }
1498 
1499 static int rtw89_ops_resume(struct ieee80211_hw *hw)
1500 {
1501 	struct rtw89_dev *rtwdev = hw->priv;
1502 	int ret;
1503 
1504 	mutex_lock(&rtwdev->mutex);
1505 	ret = rtw89_wow_resume(rtwdev);
1506 	if (ret)
1507 		rtw89_warn(rtwdev, "failed to resume for wow %d\n", ret);
1508 	mutex_unlock(&rtwdev->mutex);
1509 
1510 	clear_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags);
1511 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->track_work,
1512 				     RTW89_TRACK_WORK_PERIOD);
1513 
1514 	return ret ? 1 : 0;
1515 }
1516 
1517 static void rtw89_ops_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1518 {
1519 	struct rtw89_dev *rtwdev = hw->priv;
1520 
1521 	device_set_wakeup_enable(rtwdev->dev, enabled);
1522 }
1523 
1524 static void rtw89_set_rekey_data(struct ieee80211_hw *hw,
1525 				 struct ieee80211_vif *vif,
1526 				 struct cfg80211_gtk_rekey_data *data)
1527 {
1528 	struct rtw89_dev *rtwdev = hw->priv;
1529 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1530 	struct rtw89_wow_gtk_info *gtk_info = &rtw_wow->gtk_info;
1531 
1532 	if (data->kek_len > sizeof(gtk_info->kek) ||
1533 	    data->kck_len > sizeof(gtk_info->kck)) {
1534 		rtw89_warn(rtwdev, "kek or kck length over fw limit\n");
1535 		return;
1536 	}
1537 
1538 	mutex_lock(&rtwdev->mutex);
1539 
1540 	memcpy(gtk_info->kek, data->kek, data->kek_len);
1541 	memcpy(gtk_info->kck, data->kck, data->kck_len);
1542 
1543 	mutex_unlock(&rtwdev->mutex);
1544 }
1545 #endif
1546 
1547 static void rtw89_ops_rfkill_poll(struct ieee80211_hw *hw)
1548 {
1549 	struct rtw89_dev *rtwdev = hw->priv;
1550 
1551 	mutex_lock(&rtwdev->mutex);
1552 
1553 	/* wl_disable GPIO get floating when entering LPS */
1554 	if (test_bit(RTW89_FLAG_RUNNING, rtwdev->flags))
1555 		goto out;
1556 
1557 	rtw89_core_rfkill_poll(rtwdev, false);
1558 
1559 out:
1560 	mutex_unlock(&rtwdev->mutex);
1561 }
1562 
1563 const struct ieee80211_ops rtw89_ops = {
1564 	.tx			= rtw89_ops_tx,
1565 	.wake_tx_queue		= rtw89_ops_wake_tx_queue,
1566 	.start			= rtw89_ops_start,
1567 	.stop			= rtw89_ops_stop,
1568 	.config			= rtw89_ops_config,
1569 	.add_interface		= rtw89_ops_add_interface,
1570 	.change_interface       = rtw89_ops_change_interface,
1571 	.remove_interface	= rtw89_ops_remove_interface,
1572 	.configure_filter	= rtw89_ops_configure_filter,
1573 	.vif_cfg_changed	= rtw89_ops_vif_cfg_changed,
1574 	.link_info_changed	= rtw89_ops_link_info_changed,
1575 	.start_ap		= rtw89_ops_start_ap,
1576 	.stop_ap		= rtw89_ops_stop_ap,
1577 	.set_tim		= rtw89_ops_set_tim,
1578 	.conf_tx		= rtw89_ops_conf_tx,
1579 	.sta_state		= rtw89_ops_sta_state,
1580 	.set_key		= rtw89_ops_set_key,
1581 	.ampdu_action		= rtw89_ops_ampdu_action,
1582 	.set_rts_threshold	= rtw89_ops_set_rts_threshold,
1583 	.sta_statistics		= rtw89_ops_sta_statistics,
1584 	.flush			= rtw89_ops_flush,
1585 	.set_bitrate_mask	= rtw89_ops_set_bitrate_mask,
1586 	.set_antenna		= rtw89_ops_set_antenna,
1587 	.get_antenna		= rtw89_ops_get_antenna,
1588 	.sw_scan_start		= rtw89_ops_sw_scan_start,
1589 	.sw_scan_complete	= rtw89_ops_sw_scan_complete,
1590 	.reconfig_complete	= rtw89_ops_reconfig_complete,
1591 	.hw_scan		= rtw89_ops_hw_scan,
1592 	.cancel_hw_scan		= rtw89_ops_cancel_hw_scan,
1593 	.add_chanctx		= rtw89_ops_add_chanctx,
1594 	.remove_chanctx		= rtw89_ops_remove_chanctx,
1595 	.change_chanctx		= rtw89_ops_change_chanctx,
1596 	.assign_vif_chanctx	= rtw89_ops_assign_vif_chanctx,
1597 	.unassign_vif_chanctx	= rtw89_ops_unassign_vif_chanctx,
1598 	.remain_on_channel		= rtw89_ops_remain_on_channel,
1599 	.cancel_remain_on_channel	= rtw89_ops_cancel_remain_on_channel,
1600 	.set_sar_specs		= rtw89_ops_set_sar_specs,
1601 	.link_sta_rc_update	= rtw89_ops_sta_rc_update,
1602 	.set_tid_config		= rtw89_ops_set_tid_config,
1603 #ifdef CONFIG_PM
1604 	.suspend		= rtw89_ops_suspend,
1605 	.resume			= rtw89_ops_resume,
1606 	.set_wakeup		= rtw89_ops_set_wakeup,
1607 	.set_rekey_data		= rtw89_set_rekey_data,
1608 #endif
1609 	.rfkill_poll		= rtw89_ops_rfkill_poll,
1610 };
1611 EXPORT_SYMBOL(rtw89_ops);
1612