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