xref: /linux/drivers/net/wireless/realtek/rtw89/mac80211.c (revision 06b9cce42634a50f2840777a66553b02320db5ef)
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 "coex.h"
7 #include "debug.h"
8 #include "fw.h"
9 #include "mac.h"
10 #include "phy.h"
11 #include "ps.h"
12 #include "reg.h"
13 #include "sar.h"
14 #include "ser.h"
15 
16 static void rtw89_ops_tx(struct ieee80211_hw *hw,
17 			 struct ieee80211_tx_control *control,
18 			 struct sk_buff *skb)
19 {
20 	struct rtw89_dev *rtwdev = hw->priv;
21 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
22 	struct ieee80211_vif *vif = info->control.vif;
23 	struct ieee80211_sta *sta = control->sta;
24 	int ret, qsel;
25 
26 	ret = rtw89_core_tx_write(rtwdev, vif, sta, skb, &qsel);
27 	if (ret) {
28 		rtw89_err(rtwdev, "failed to transmit skb: %d\n", ret);
29 		ieee80211_free_txskb(hw, skb);
30 		return;
31 	}
32 	rtw89_core_tx_kick_off(rtwdev, qsel);
33 }
34 
35 static void rtw89_ops_wake_tx_queue(struct ieee80211_hw *hw,
36 				    struct ieee80211_txq *txq)
37 {
38 	struct rtw89_dev *rtwdev = hw->priv;
39 
40 	ieee80211_schedule_txq(hw, txq);
41 	queue_work(rtwdev->txq_wq, &rtwdev->txq_work);
42 }
43 
44 static int rtw89_ops_start(struct ieee80211_hw *hw)
45 {
46 	struct rtw89_dev *rtwdev = hw->priv;
47 	int ret;
48 
49 	mutex_lock(&rtwdev->mutex);
50 	ret = rtw89_core_start(rtwdev);
51 	mutex_unlock(&rtwdev->mutex);
52 
53 	return ret;
54 }
55 
56 static void rtw89_ops_stop(struct ieee80211_hw *hw)
57 {
58 	struct rtw89_dev *rtwdev = hw->priv;
59 
60 	mutex_lock(&rtwdev->mutex);
61 	rtw89_core_stop(rtwdev);
62 	mutex_unlock(&rtwdev->mutex);
63 }
64 
65 static int rtw89_ops_config(struct ieee80211_hw *hw, u32 changed)
66 {
67 	struct rtw89_dev *rtwdev = hw->priv;
68 
69 	mutex_lock(&rtwdev->mutex);
70 	rtw89_leave_ps_mode(rtwdev);
71 
72 	if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
73 	    !(hw->conf.flags & IEEE80211_CONF_IDLE))
74 		rtw89_leave_ips(rtwdev);
75 
76 	if (changed & IEEE80211_CONF_CHANGE_PS) {
77 		if (hw->conf.flags & IEEE80211_CONF_PS) {
78 			rtwdev->lps_enabled = true;
79 		} else {
80 			rtw89_leave_lps(rtwdev);
81 			rtwdev->lps_enabled = false;
82 		}
83 	}
84 
85 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL)
86 		rtw89_set_channel(rtwdev);
87 
88 	if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
89 	    (hw->conf.flags & IEEE80211_CONF_IDLE))
90 		rtw89_enter_ips(rtwdev);
91 
92 	mutex_unlock(&rtwdev->mutex);
93 
94 	return 0;
95 }
96 
97 static int rtw89_ops_add_interface(struct ieee80211_hw *hw,
98 				   struct ieee80211_vif *vif)
99 {
100 	struct rtw89_dev *rtwdev = hw->priv;
101 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
102 	int ret = 0;
103 
104 	mutex_lock(&rtwdev->mutex);
105 	rtwvif->rtwdev = rtwdev;
106 	list_add_tail(&rtwvif->list, &rtwdev->rtwvifs_list);
107 	INIT_WORK(&rtwvif->update_beacon_work, rtw89_core_update_beacon_work);
108 	rtw89_leave_ps_mode(rtwdev);
109 
110 	rtw89_traffic_stats_init(rtwdev, &rtwvif->stats);
111 	rtw89_vif_type_mapping(vif, false);
112 	rtwvif->port = rtw89_core_acquire_bit_map(rtwdev->hw_port,
113 						  RTW89_PORT_NUM);
114 	if (rtwvif->port == RTW89_PORT_NUM) {
115 		ret = -ENOSPC;
116 		goto out;
117 	}
118 
119 	rtwvif->bcn_hit_cond = 0;
120 	rtwvif->mac_idx = RTW89_MAC_0;
121 	rtwvif->phy_idx = RTW89_PHY_0;
122 	rtwvif->hit_rule = 0;
123 	ether_addr_copy(rtwvif->mac_addr, vif->addr);
124 
125 	ret = rtw89_mac_add_vif(rtwdev, rtwvif);
126 	if (ret) {
127 		rtw89_core_release_bit_map(rtwdev->hw_port, rtwvif->port);
128 		goto out;
129 	}
130 
131 	rtw89_core_txq_init(rtwdev, vif->txq);
132 
133 	rtw89_btc_ntfy_role_info(rtwdev, rtwvif, NULL, BTC_ROLE_START);
134 out:
135 	mutex_unlock(&rtwdev->mutex);
136 
137 	return ret;
138 }
139 
140 static void rtw89_ops_remove_interface(struct ieee80211_hw *hw,
141 				       struct ieee80211_vif *vif)
142 {
143 	struct rtw89_dev *rtwdev = hw->priv;
144 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
145 
146 	cancel_work_sync(&rtwvif->update_beacon_work);
147 
148 	mutex_lock(&rtwdev->mutex);
149 	rtw89_leave_ps_mode(rtwdev);
150 	rtw89_btc_ntfy_role_info(rtwdev, rtwvif, NULL, BTC_ROLE_STOP);
151 	rtw89_mac_remove_vif(rtwdev, rtwvif);
152 	rtw89_core_release_bit_map(rtwdev->hw_port, rtwvif->port);
153 	list_del_init(&rtwvif->list);
154 	mutex_unlock(&rtwdev->mutex);
155 }
156 
157 static void rtw89_ops_configure_filter(struct ieee80211_hw *hw,
158 				       unsigned int changed_flags,
159 				       unsigned int *new_flags,
160 				       u64 multicast)
161 {
162 	struct rtw89_dev *rtwdev = hw->priv;
163 
164 	mutex_lock(&rtwdev->mutex);
165 	rtw89_leave_ps_mode(rtwdev);
166 
167 	*new_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_FCSFAIL |
168 		      FIF_BCN_PRBRESP_PROMISC | FIF_PROBE_REQ;
169 
170 	if (changed_flags & FIF_ALLMULTI) {
171 		if (*new_flags & FIF_ALLMULTI)
172 			rtwdev->hal.rx_fltr &= ~B_AX_A_MC;
173 		else
174 			rtwdev->hal.rx_fltr |= B_AX_A_MC;
175 	}
176 	if (changed_flags & FIF_FCSFAIL) {
177 		if (*new_flags & FIF_FCSFAIL)
178 			rtwdev->hal.rx_fltr |= B_AX_A_CRC32_ERR;
179 		else
180 			rtwdev->hal.rx_fltr &= ~B_AX_A_CRC32_ERR;
181 	}
182 	if (changed_flags & FIF_OTHER_BSS) {
183 		if (*new_flags & FIF_OTHER_BSS)
184 			rtwdev->hal.rx_fltr &= ~B_AX_A_A1_MATCH;
185 		else
186 			rtwdev->hal.rx_fltr |= B_AX_A_A1_MATCH;
187 	}
188 	if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
189 		if (*new_flags & FIF_BCN_PRBRESP_PROMISC) {
190 			rtwdev->hal.rx_fltr &= ~B_AX_A_BCN_CHK_EN;
191 			rtwdev->hal.rx_fltr &= ~B_AX_A_BC;
192 			rtwdev->hal.rx_fltr &= ~B_AX_A_A1_MATCH;
193 		} else {
194 			rtwdev->hal.rx_fltr |= B_AX_A_BCN_CHK_EN;
195 			rtwdev->hal.rx_fltr |= B_AX_A_BC;
196 			rtwdev->hal.rx_fltr |= B_AX_A_A1_MATCH;
197 		}
198 	}
199 	if (changed_flags & FIF_PROBE_REQ) {
200 		if (*new_flags & FIF_PROBE_REQ) {
201 			rtwdev->hal.rx_fltr &= ~B_AX_A_BC_CAM_MATCH;
202 			rtwdev->hal.rx_fltr &= ~B_AX_A_UC_CAM_MATCH;
203 		} else {
204 			rtwdev->hal.rx_fltr |= B_AX_A_BC_CAM_MATCH;
205 			rtwdev->hal.rx_fltr |= B_AX_A_UC_CAM_MATCH;
206 		}
207 	}
208 
209 	rtw89_write32_mask(rtwdev,
210 			   rtw89_mac_reg_by_idx(R_AX_RX_FLTR_OPT, RTW89_MAC_0),
211 			   B_AX_RX_FLTR_CFG_MASK,
212 			   rtwdev->hal.rx_fltr);
213 	if (!rtwdev->dbcc_en)
214 		goto out;
215 	rtw89_write32_mask(rtwdev,
216 			   rtw89_mac_reg_by_idx(R_AX_RX_FLTR_OPT, RTW89_MAC_1),
217 			   B_AX_RX_FLTR_CFG_MASK,
218 			   rtwdev->hal.rx_fltr);
219 
220 out:
221 	mutex_unlock(&rtwdev->mutex);
222 }
223 
224 static const u8 ac_to_fw_idx[IEEE80211_NUM_ACS] = {
225 	[IEEE80211_AC_VO] = 3,
226 	[IEEE80211_AC_VI] = 2,
227 	[IEEE80211_AC_BE] = 0,
228 	[IEEE80211_AC_BK] = 1,
229 };
230 
231 static u8 rtw89_aifsn_to_aifs(struct rtw89_dev *rtwdev,
232 			      struct rtw89_vif *rtwvif, u8 aifsn)
233 {
234 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
235 	u8 slot_time;
236 	u8 sifs;
237 
238 	slot_time = vif->bss_conf.use_short_slot ? 9 : 20;
239 	sifs = rtwdev->hal.current_band_type == RTW89_BAND_5G ? 16 : 10;
240 
241 	return aifsn * slot_time + sifs;
242 }
243 
244 static void ____rtw89_conf_tx_edca(struct rtw89_dev *rtwdev,
245 				   struct rtw89_vif *rtwvif, u16 ac)
246 {
247 	struct ieee80211_tx_queue_params *params = &rtwvif->tx_params[ac];
248 	u32 val;
249 	u8 ecw_max, ecw_min;
250 	u8 aifs;
251 
252 	/* 2^ecw - 1 = cw; ecw = log2(cw + 1) */
253 	ecw_max = ilog2(params->cw_max + 1);
254 	ecw_min = ilog2(params->cw_min + 1);
255 	aifs = rtw89_aifsn_to_aifs(rtwdev, rtwvif, params->aifs);
256 	val = FIELD_PREP(FW_EDCA_PARAM_TXOPLMT_MSK, params->txop) |
257 	      FIELD_PREP(FW_EDCA_PARAM_CWMAX_MSK, ecw_max) |
258 	      FIELD_PREP(FW_EDCA_PARAM_CWMIN_MSK, ecw_min) |
259 	      FIELD_PREP(FW_EDCA_PARAM_AIFS_MSK, aifs);
260 	rtw89_fw_h2c_set_edca(rtwdev, rtwvif, ac_to_fw_idx[ac], val);
261 }
262 
263 static const u32 ac_to_mu_edca_param[IEEE80211_NUM_ACS] = {
264 	[IEEE80211_AC_VO] = R_AX_MUEDCA_VO_PARAM_0,
265 	[IEEE80211_AC_VI] = R_AX_MUEDCA_VI_PARAM_0,
266 	[IEEE80211_AC_BE] = R_AX_MUEDCA_BE_PARAM_0,
267 	[IEEE80211_AC_BK] = R_AX_MUEDCA_BK_PARAM_0,
268 };
269 
270 static void ____rtw89_conf_tx_mu_edca(struct rtw89_dev *rtwdev,
271 				      struct rtw89_vif *rtwvif, u16 ac)
272 {
273 	struct ieee80211_tx_queue_params *params = &rtwvif->tx_params[ac];
274 	struct ieee80211_he_mu_edca_param_ac_rec *mu_edca;
275 	u8 aifs, aifsn;
276 	u16 timer_32us;
277 	u32 reg;
278 	u32 val;
279 
280 	if (!params->mu_edca)
281 		return;
282 
283 	mu_edca = &params->mu_edca_param_rec;
284 	aifsn = FIELD_GET(GENMASK(3, 0), mu_edca->aifsn);
285 	aifs = aifsn ? rtw89_aifsn_to_aifs(rtwdev, rtwvif, aifsn) : 0;
286 	timer_32us = mu_edca->mu_edca_timer << 8;
287 
288 	val = FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_TIMER_MASK, timer_32us) |
289 	      FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_CW_MASK, mu_edca->ecw_min_max) |
290 	      FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_AIFS_MASK, aifs);
291 	reg = rtw89_mac_reg_by_idx(ac_to_mu_edca_param[ac], rtwvif->mac_idx);
292 	rtw89_write32(rtwdev, reg, val);
293 
294 	rtw89_mac_set_hw_muedca_ctrl(rtwdev, rtwvif, true);
295 }
296 
297 static void __rtw89_conf_tx(struct rtw89_dev *rtwdev,
298 			    struct rtw89_vif *rtwvif, u16 ac)
299 {
300 	____rtw89_conf_tx_edca(rtwdev, rtwvif, ac);
301 	____rtw89_conf_tx_mu_edca(rtwdev, rtwvif, ac);
302 }
303 
304 static void rtw89_conf_tx(struct rtw89_dev *rtwdev,
305 			  struct rtw89_vif *rtwvif)
306 {
307 	u16 ac;
308 
309 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
310 		__rtw89_conf_tx(rtwdev, rtwvif, ac);
311 }
312 
313 static void rtw89_station_mode_sta_assoc(struct rtw89_dev *rtwdev,
314 					 struct ieee80211_vif *vif,
315 					 struct ieee80211_bss_conf *conf)
316 {
317 	struct ieee80211_sta *sta;
318 
319 	if (vif->type != NL80211_IFTYPE_STATION)
320 		return;
321 
322 	sta = ieee80211_find_sta(vif, conf->bssid);
323 	if (!sta) {
324 		rtw89_err(rtwdev, "can't find sta to set sta_assoc state\n");
325 		return;
326 	}
327 
328 	rtw89_vif_type_mapping(vif, true);
329 
330 	rtw89_core_sta_assoc(rtwdev, vif, sta);
331 }
332 
333 static void rtw89_ops_bss_info_changed(struct ieee80211_hw *hw,
334 				       struct ieee80211_vif *vif,
335 				       struct ieee80211_bss_conf *conf,
336 				       u32 changed)
337 {
338 	struct rtw89_dev *rtwdev = hw->priv;
339 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
340 
341 	mutex_lock(&rtwdev->mutex);
342 	rtw89_leave_ps_mode(rtwdev);
343 
344 	if (changed & BSS_CHANGED_ASSOC) {
345 		if (conf->assoc) {
346 			rtw89_station_mode_sta_assoc(rtwdev, vif, conf);
347 			rtw89_phy_set_bss_color(rtwdev, vif);
348 			rtw89_chip_cfg_txpwr_ul_tb_offset(rtwdev, vif);
349 			rtw89_mac_port_update(rtwdev, rtwvif);
350 		}
351 	}
352 
353 	if (changed & BSS_CHANGED_BSSID) {
354 		ether_addr_copy(rtwvif->bssid, conf->bssid);
355 		rtw89_cam_bssid_changed(rtwdev, rtwvif);
356 		rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL);
357 	}
358 
359 	if (changed & BSS_CHANGED_BEACON)
360 		rtw89_fw_h2c_update_beacon(rtwdev, rtwvif);
361 
362 	if (changed & BSS_CHANGED_ERP_SLOT)
363 		rtw89_conf_tx(rtwdev, rtwvif);
364 
365 	if (changed & BSS_CHANGED_HE_BSS_COLOR)
366 		rtw89_phy_set_bss_color(rtwdev, vif);
367 
368 	if (changed & BSS_CHANGED_MU_GROUPS)
369 		rtw89_mac_bf_set_gid_table(rtwdev, vif, conf);
370 
371 	mutex_unlock(&rtwdev->mutex);
372 }
373 
374 static int rtw89_ops_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
375 {
376 	struct rtw89_dev *rtwdev = hw->priv;
377 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
378 
379 	mutex_lock(&rtwdev->mutex);
380 	ether_addr_copy(rtwvif->bssid, vif->bss_conf.bssid);
381 	rtw89_cam_bssid_changed(rtwdev, rtwvif);
382 	rtw89_mac_port_update(rtwdev, rtwvif);
383 	rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, NULL);
384 	rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, NULL, RTW89_ROLE_TYPE_CHANGE);
385 	rtw89_fw_h2c_join_info(rtwdev, rtwvif, NULL, true);
386 	rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL);
387 	rtw89_chip_rfk_channel(rtwdev);
388 	mutex_unlock(&rtwdev->mutex);
389 
390 	return 0;
391 }
392 
393 static
394 void rtw89_ops_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
395 {
396 	struct rtw89_dev *rtwdev = hw->priv;
397 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
398 
399 	mutex_lock(&rtwdev->mutex);
400 	rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, NULL);
401 	rtw89_fw_h2c_join_info(rtwdev, rtwvif, NULL, true);
402 	mutex_unlock(&rtwdev->mutex);
403 }
404 
405 static int rtw89_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
406 			     bool set)
407 {
408 	struct rtw89_dev *rtwdev = hw->priv;
409 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
410 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
411 
412 	ieee80211_queue_work(rtwdev->hw, &rtwvif->update_beacon_work);
413 
414 	return 0;
415 }
416 
417 static int rtw89_ops_conf_tx(struct ieee80211_hw *hw,
418 			     struct ieee80211_vif *vif, u16 ac,
419 			     const struct ieee80211_tx_queue_params *params)
420 {
421 	struct rtw89_dev *rtwdev = hw->priv;
422 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
423 
424 	mutex_lock(&rtwdev->mutex);
425 	rtw89_leave_ps_mode(rtwdev);
426 	rtwvif->tx_params[ac] = *params;
427 	__rtw89_conf_tx(rtwdev, rtwvif, ac);
428 	mutex_unlock(&rtwdev->mutex);
429 
430 	return 0;
431 }
432 
433 static int __rtw89_ops_sta_state(struct ieee80211_hw *hw,
434 				 struct ieee80211_vif *vif,
435 				 struct ieee80211_sta *sta,
436 				 enum ieee80211_sta_state old_state,
437 				 enum ieee80211_sta_state new_state)
438 {
439 	struct rtw89_dev *rtwdev = hw->priv;
440 
441 	if (old_state == IEEE80211_STA_NOTEXIST &&
442 	    new_state == IEEE80211_STA_NONE)
443 		return rtw89_core_sta_add(rtwdev, vif, sta);
444 
445 	if (old_state == IEEE80211_STA_AUTH &&
446 	    new_state == IEEE80211_STA_ASSOC) {
447 		if (vif->type == NL80211_IFTYPE_STATION)
448 			return 0; /* defer to bss_info_changed to have vif info */
449 		return rtw89_core_sta_assoc(rtwdev, vif, sta);
450 	}
451 
452 	if (old_state == IEEE80211_STA_ASSOC &&
453 	    new_state == IEEE80211_STA_AUTH)
454 		return rtw89_core_sta_disassoc(rtwdev, vif, sta);
455 
456 	if (old_state == IEEE80211_STA_AUTH &&
457 	    new_state == IEEE80211_STA_NONE)
458 		return rtw89_core_sta_disconnect(rtwdev, vif, sta);
459 
460 	if (old_state == IEEE80211_STA_NONE &&
461 	    new_state == IEEE80211_STA_NOTEXIST)
462 		return rtw89_core_sta_remove(rtwdev, vif, sta);
463 
464 	return 0;
465 }
466 
467 static int rtw89_ops_sta_state(struct ieee80211_hw *hw,
468 			       struct ieee80211_vif *vif,
469 			       struct ieee80211_sta *sta,
470 			       enum ieee80211_sta_state old_state,
471 			       enum ieee80211_sta_state new_state)
472 {
473 	struct rtw89_dev *rtwdev = hw->priv;
474 	int ret;
475 
476 	mutex_lock(&rtwdev->mutex);
477 	rtw89_leave_ps_mode(rtwdev);
478 	ret = __rtw89_ops_sta_state(hw, vif, sta, old_state, new_state);
479 	mutex_unlock(&rtwdev->mutex);
480 
481 	return ret;
482 }
483 
484 static int rtw89_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
485 			     struct ieee80211_vif *vif,
486 			     struct ieee80211_sta *sta,
487 			     struct ieee80211_key_conf *key)
488 {
489 	struct rtw89_dev *rtwdev = hw->priv;
490 	int ret = 0;
491 
492 	mutex_lock(&rtwdev->mutex);
493 	rtw89_leave_ps_mode(rtwdev);
494 
495 	switch (cmd) {
496 	case SET_KEY:
497 		rtw89_btc_ntfy_specific_packet(rtwdev, PACKET_EAPOL_END);
498 		ret = rtw89_cam_sec_key_add(rtwdev, vif, sta, key);
499 		if (ret && ret != -EOPNOTSUPP) {
500 			rtw89_err(rtwdev, "failed to add key to sec cam\n");
501 			goto out;
502 		}
503 		break;
504 	case DISABLE_KEY:
505 		rtw89_hci_flush_queues(rtwdev, BIT(rtwdev->hw->queues) - 1,
506 				       false);
507 		rtw89_mac_flush_txq(rtwdev, BIT(rtwdev->hw->queues) - 1, false);
508 		ret = rtw89_cam_sec_key_del(rtwdev, vif, sta, key, true);
509 		if (ret) {
510 			rtw89_err(rtwdev, "failed to remove key from sec cam\n");
511 			goto out;
512 		}
513 		break;
514 	}
515 
516 out:
517 	mutex_unlock(&rtwdev->mutex);
518 
519 	return ret;
520 }
521 
522 static int rtw89_ops_ampdu_action(struct ieee80211_hw *hw,
523 				  struct ieee80211_vif *vif,
524 				  struct ieee80211_ampdu_params *params)
525 {
526 	struct rtw89_dev *rtwdev = hw->priv;
527 	struct ieee80211_sta *sta = params->sta;
528 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
529 	u16 tid = params->tid;
530 	struct ieee80211_txq *txq = sta->txq[tid];
531 	struct rtw89_txq *rtwtxq = (struct rtw89_txq *)txq->drv_priv;
532 
533 	switch (params->action) {
534 	case IEEE80211_AMPDU_TX_START:
535 		return IEEE80211_AMPDU_TX_START_IMMEDIATE;
536 	case IEEE80211_AMPDU_TX_STOP_CONT:
537 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
538 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
539 		mutex_lock(&rtwdev->mutex);
540 		clear_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags);
541 		mutex_unlock(&rtwdev->mutex);
542 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
543 		break;
544 	case IEEE80211_AMPDU_TX_OPERATIONAL:
545 		mutex_lock(&rtwdev->mutex);
546 		set_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags);
547 		rtwsta->ampdu_params[tid].agg_num = params->buf_size;
548 		rtwsta->ampdu_params[tid].amsdu = params->amsdu;
549 		rtw89_leave_ps_mode(rtwdev);
550 		mutex_unlock(&rtwdev->mutex);
551 		break;
552 	case IEEE80211_AMPDU_RX_START:
553 		mutex_lock(&rtwdev->mutex);
554 		rtw89_fw_h2c_ba_cam(rtwdev, rtwsta, true, params);
555 		mutex_unlock(&rtwdev->mutex);
556 		break;
557 	case IEEE80211_AMPDU_RX_STOP:
558 		mutex_lock(&rtwdev->mutex);
559 		rtw89_fw_h2c_ba_cam(rtwdev, rtwsta, false, params);
560 		mutex_unlock(&rtwdev->mutex);
561 		break;
562 	default:
563 		WARN_ON(1);
564 		return -ENOTSUPP;
565 	}
566 
567 	return 0;
568 }
569 
570 static int rtw89_ops_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
571 {
572 	struct rtw89_dev *rtwdev = hw->priv;
573 
574 	mutex_lock(&rtwdev->mutex);
575 	rtw89_leave_ps_mode(rtwdev);
576 	if (test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
577 		rtw89_mac_update_rts_threshold(rtwdev, RTW89_MAC_0);
578 	mutex_unlock(&rtwdev->mutex);
579 
580 	return 0;
581 }
582 
583 static void rtw89_ops_sta_statistics(struct ieee80211_hw *hw,
584 				     struct ieee80211_vif *vif,
585 				     struct ieee80211_sta *sta,
586 				     struct station_info *sinfo)
587 {
588 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
589 
590 	sinfo->txrate = rtwsta->ra_report.txrate;
591 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
592 }
593 
594 static void rtw89_ops_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
595 			    u32 queues, bool drop)
596 {
597 	struct rtw89_dev *rtwdev = hw->priv;
598 
599 	mutex_lock(&rtwdev->mutex);
600 	rtw89_leave_lps(rtwdev);
601 	rtw89_hci_flush_queues(rtwdev, queues, drop);
602 	rtw89_mac_flush_txq(rtwdev, queues, drop);
603 	mutex_unlock(&rtwdev->mutex);
604 }
605 
606 struct rtw89_iter_bitrate_mask_data {
607 	struct rtw89_dev *rtwdev;
608 	struct ieee80211_vif *vif;
609 	const struct cfg80211_bitrate_mask *mask;
610 };
611 
612 static void rtw89_ra_mask_info_update_iter(void *data, struct ieee80211_sta *sta)
613 {
614 	struct rtw89_iter_bitrate_mask_data *br_data = data;
615 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
616 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwsta->rtwvif);
617 
618 	if (vif != br_data->vif)
619 		return;
620 
621 	rtwsta->use_cfg_mask = true;
622 	rtwsta->mask = *br_data->mask;
623 	rtw89_phy_ra_updata_sta(br_data->rtwdev, sta);
624 }
625 
626 static void rtw89_ra_mask_info_update(struct rtw89_dev *rtwdev,
627 				      struct ieee80211_vif *vif,
628 				      const struct cfg80211_bitrate_mask *mask)
629 {
630 	struct rtw89_iter_bitrate_mask_data br_data = { .rtwdev = rtwdev,
631 							.vif = vif,
632 							.mask = mask};
633 
634 	ieee80211_iterate_stations_atomic(rtwdev->hw, rtw89_ra_mask_info_update_iter,
635 					  &br_data);
636 }
637 
638 static int rtw89_ops_set_bitrate_mask(struct ieee80211_hw *hw,
639 				      struct ieee80211_vif *vif,
640 				      const struct cfg80211_bitrate_mask *mask)
641 {
642 	struct rtw89_dev *rtwdev = hw->priv;
643 
644 	mutex_lock(&rtwdev->mutex);
645 	rtw89_phy_rate_pattern_vif(rtwdev, vif, mask);
646 	rtw89_ra_mask_info_update(rtwdev, vif, mask);
647 	mutex_unlock(&rtwdev->mutex);
648 
649 	return 0;
650 }
651 
652 static
653 int rtw89_ops_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
654 {
655 	struct rtw89_dev *rtwdev = hw->priv;
656 	struct rtw89_hal *hal = &rtwdev->hal;
657 
658 	if (rx_ant != hw->wiphy->available_antennas_rx)
659 		return -EINVAL;
660 
661 	mutex_lock(&rtwdev->mutex);
662 	hal->antenna_tx = tx_ant;
663 	hal->antenna_rx = rx_ant;
664 	mutex_unlock(&rtwdev->mutex);
665 
666 	return 0;
667 }
668 
669 static
670 int rtw89_ops_get_antenna(struct ieee80211_hw *hw,  u32 *tx_ant, u32 *rx_ant)
671 {
672 	struct rtw89_dev *rtwdev = hw->priv;
673 	struct rtw89_hal *hal = &rtwdev->hal;
674 
675 	*tx_ant = hal->antenna_tx;
676 	*rx_ant = hal->antenna_rx;
677 
678 	return 0;
679 }
680 
681 static void rtw89_ops_sw_scan_start(struct ieee80211_hw *hw,
682 				    struct ieee80211_vif *vif,
683 				    const u8 *mac_addr)
684 {
685 	struct rtw89_dev *rtwdev = hw->priv;
686 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
687 	struct rtw89_hal *hal = &rtwdev->hal;
688 
689 	mutex_lock(&rtwdev->mutex);
690 	rtwdev->scanning = true;
691 	rtw89_leave_lps(rtwdev);
692 	rtw89_btc_ntfy_scan_start(rtwdev, RTW89_PHY_0, hal->current_band_type);
693 	rtw89_chip_rfk_scan(rtwdev, true);
694 	rtw89_hci_recalc_int_mit(rtwdev);
695 	rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, mac_addr);
696 	mutex_unlock(&rtwdev->mutex);
697 }
698 
699 static void rtw89_ops_sw_scan_complete(struct ieee80211_hw *hw,
700 				       struct ieee80211_vif *vif)
701 {
702 	struct rtw89_dev *rtwdev = hw->priv;
703 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
704 
705 	mutex_lock(&rtwdev->mutex);
706 	rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL);
707 	rtw89_chip_rfk_scan(rtwdev, false);
708 	rtw89_btc_ntfy_scan_finish(rtwdev, RTW89_PHY_0);
709 	rtwdev->scanning = false;
710 	rtwdev->dig.bypass_dig = true;
711 	mutex_unlock(&rtwdev->mutex);
712 }
713 
714 static void rtw89_ops_reconfig_complete(struct ieee80211_hw *hw,
715 					enum ieee80211_reconfig_type reconfig_type)
716 {
717 	struct rtw89_dev *rtwdev = hw->priv;
718 
719 	if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART)
720 		rtw89_ser_recfg_done(rtwdev);
721 }
722 
723 const struct ieee80211_ops rtw89_ops = {
724 	.tx			= rtw89_ops_tx,
725 	.wake_tx_queue		= rtw89_ops_wake_tx_queue,
726 	.start			= rtw89_ops_start,
727 	.stop			= rtw89_ops_stop,
728 	.config			= rtw89_ops_config,
729 	.add_interface		= rtw89_ops_add_interface,
730 	.remove_interface	= rtw89_ops_remove_interface,
731 	.configure_filter	= rtw89_ops_configure_filter,
732 	.bss_info_changed	= rtw89_ops_bss_info_changed,
733 	.start_ap		= rtw89_ops_start_ap,
734 	.stop_ap		= rtw89_ops_stop_ap,
735 	.set_tim		= rtw89_ops_set_tim,
736 	.conf_tx		= rtw89_ops_conf_tx,
737 	.sta_state		= rtw89_ops_sta_state,
738 	.set_key		= rtw89_ops_set_key,
739 	.ampdu_action		= rtw89_ops_ampdu_action,
740 	.set_rts_threshold	= rtw89_ops_set_rts_threshold,
741 	.sta_statistics		= rtw89_ops_sta_statistics,
742 	.flush			= rtw89_ops_flush,
743 	.set_bitrate_mask	= rtw89_ops_set_bitrate_mask,
744 	.set_antenna		= rtw89_ops_set_antenna,
745 	.get_antenna		= rtw89_ops_get_antenna,
746 	.sw_scan_start		= rtw89_ops_sw_scan_start,
747 	.sw_scan_complete	= rtw89_ops_sw_scan_complete,
748 	.reconfig_complete	= rtw89_ops_reconfig_complete,
749 	.set_sar_specs		= rtw89_ops_set_sar_specs,
750 };
751 EXPORT_SYMBOL(rtw89_ops);
752