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 ieee80211_sta *sta = control->sta;
27 	int ret, qsel;
28 
29 	ret = rtw89_core_tx_write(rtwdev, vif, sta, skb, &qsel);
30 	if (ret) {
31 		rtw89_err(rtwdev, "failed to transmit skb: %d\n", ret);
32 		ieee80211_free_txskb(hw, skb);
33 		return;
34 	}
35 	rtw89_core_tx_kick_off(rtwdev, qsel);
36 }
37 
38 static void rtw89_ops_wake_tx_queue(struct ieee80211_hw *hw,
39 				    struct ieee80211_txq *txq)
40 {
41 	struct rtw89_dev *rtwdev = hw->priv;
42 
43 	ieee80211_schedule_txq(hw, txq);
44 	queue_work(rtwdev->txq_wq, &rtwdev->txq_work);
45 }
46 
47 static int rtw89_ops_start(struct ieee80211_hw *hw)
48 {
49 	struct rtw89_dev *rtwdev = hw->priv;
50 	int ret;
51 
52 	mutex_lock(&rtwdev->mutex);
53 	ret = rtw89_core_start(rtwdev);
54 	mutex_unlock(&rtwdev->mutex);
55 
56 	return ret;
57 }
58 
59 static void rtw89_ops_stop(struct ieee80211_hw *hw)
60 {
61 	struct rtw89_dev *rtwdev = hw->priv;
62 
63 	mutex_lock(&rtwdev->mutex);
64 	rtw89_core_stop(rtwdev);
65 	mutex_unlock(&rtwdev->mutex);
66 }
67 
68 static int rtw89_ops_config(struct ieee80211_hw *hw, u32 changed)
69 {
70 	struct rtw89_dev *rtwdev = hw->priv;
71 
72 	/* let previous ips work finish to ensure we don't leave ips twice */
73 	cancel_work_sync(&rtwdev->ips_work);
74 
75 	mutex_lock(&rtwdev->mutex);
76 	rtw89_leave_ps_mode(rtwdev);
77 
78 	if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
79 	    !(hw->conf.flags & IEEE80211_CONF_IDLE))
80 		rtw89_leave_ips(rtwdev);
81 
82 	if (changed & IEEE80211_CONF_CHANGE_PS) {
83 		if (hw->conf.flags & IEEE80211_CONF_PS) {
84 			rtwdev->lps_enabled = true;
85 		} else {
86 			rtw89_leave_lps(rtwdev);
87 			rtwdev->lps_enabled = false;
88 		}
89 	}
90 
91 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
92 		rtw89_config_entity_chandef(rtwdev, RTW89_SUB_ENTITY_0,
93 					    &hw->conf.chandef);
94 		rtw89_set_channel(rtwdev);
95 	}
96 
97 	if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
98 	    (hw->conf.flags & IEEE80211_CONF_IDLE))
99 		rtw89_enter_ips(rtwdev);
100 
101 	mutex_unlock(&rtwdev->mutex);
102 
103 	return 0;
104 }
105 
106 static int rtw89_ops_add_interface(struct ieee80211_hw *hw,
107 				   struct ieee80211_vif *vif)
108 {
109 	struct rtw89_dev *rtwdev = hw->priv;
110 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
111 	int ret = 0;
112 
113 	rtw89_debug(rtwdev, RTW89_DBG_STATE, "add vif %pM type %d, p2p %d\n",
114 		    vif->addr, vif->type, vif->p2p);
115 
116 	mutex_lock(&rtwdev->mutex);
117 	rtwvif->rtwdev = rtwdev;
118 	list_add_tail(&rtwvif->list, &rtwdev->rtwvifs_list);
119 	INIT_WORK(&rtwvif->update_beacon_work, rtw89_core_update_beacon_work);
120 	rtw89_leave_ps_mode(rtwdev);
121 
122 	rtw89_traffic_stats_init(rtwdev, &rtwvif->stats);
123 	rtw89_vif_type_mapping(vif, false);
124 	rtwvif->port = rtw89_core_acquire_bit_map(rtwdev->hw_port,
125 						  RTW89_PORT_NUM);
126 	if (rtwvif->port == RTW89_PORT_NUM) {
127 		ret = -ENOSPC;
128 		list_del_init(&rtwvif->list);
129 		goto out;
130 	}
131 
132 	rtwvif->bcn_hit_cond = 0;
133 	rtwvif->mac_idx = RTW89_MAC_0;
134 	rtwvif->phy_idx = RTW89_PHY_0;
135 	rtwvif->sub_entity_idx = RTW89_SUB_ENTITY_0;
136 	rtwvif->hit_rule = 0;
137 	ether_addr_copy(rtwvif->mac_addr, vif->addr);
138 	INIT_LIST_HEAD(&rtwvif->general_pkt_list);
139 
140 	ret = rtw89_mac_add_vif(rtwdev, rtwvif);
141 	if (ret) {
142 		rtw89_core_release_bit_map(rtwdev->hw_port, rtwvif->port);
143 		list_del_init(&rtwvif->list);
144 		goto out;
145 	}
146 
147 	rtw89_core_txq_init(rtwdev, vif->txq);
148 
149 	rtw89_btc_ntfy_role_info(rtwdev, rtwvif, NULL, BTC_ROLE_START);
150 out:
151 	mutex_unlock(&rtwdev->mutex);
152 
153 	return ret;
154 }
155 
156 static void rtw89_ops_remove_interface(struct ieee80211_hw *hw,
157 				       struct ieee80211_vif *vif)
158 {
159 	struct rtw89_dev *rtwdev = hw->priv;
160 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
161 
162 	rtw89_debug(rtwdev, RTW89_DBG_STATE, "remove vif %pM type %d p2p %d\n",
163 		    vif->addr, vif->type, vif->p2p);
164 
165 	cancel_work_sync(&rtwvif->update_beacon_work);
166 
167 	mutex_lock(&rtwdev->mutex);
168 	rtw89_leave_ps_mode(rtwdev);
169 	rtw89_btc_ntfy_role_info(rtwdev, rtwvif, NULL, BTC_ROLE_STOP);
170 	rtw89_mac_remove_vif(rtwdev, rtwvif);
171 	rtw89_core_release_bit_map(rtwdev->hw_port, rtwvif->port);
172 	list_del_init(&rtwvif->list);
173 	mutex_unlock(&rtwdev->mutex);
174 }
175 
176 static int rtw89_ops_change_interface(struct ieee80211_hw *hw,
177 				      struct ieee80211_vif *vif,
178 				      enum nl80211_iftype type, bool p2p)
179 {
180 	struct rtw89_dev *rtwdev = hw->priv;
181 	int ret;
182 
183 	set_bit(RTW89_FLAG_CHANGING_INTERFACE, rtwdev->flags);
184 
185 	rtw89_debug(rtwdev, RTW89_DBG_STATE, "change vif %pM (%d)->(%d), p2p (%d)->(%d)\n",
186 		    vif->addr, vif->type, type, vif->p2p, p2p);
187 
188 	rtw89_ops_remove_interface(hw, vif);
189 
190 	vif->type = type;
191 	vif->p2p = p2p;
192 
193 	ret = rtw89_ops_add_interface(hw, vif);
194 	if (ret)
195 		rtw89_warn(rtwdev, "failed to change interface %d\n", ret);
196 
197 	clear_bit(RTW89_FLAG_CHANGING_INTERFACE, rtwdev->flags);
198 
199 	return ret;
200 }
201 
202 static void rtw89_ops_configure_filter(struct ieee80211_hw *hw,
203 				       unsigned int changed_flags,
204 				       unsigned int *new_flags,
205 				       u64 multicast)
206 {
207 	struct rtw89_dev *rtwdev = hw->priv;
208 
209 	mutex_lock(&rtwdev->mutex);
210 	rtw89_leave_ps_mode(rtwdev);
211 
212 	*new_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_FCSFAIL |
213 		      FIF_BCN_PRBRESP_PROMISC | FIF_PROBE_REQ;
214 
215 	if (changed_flags & FIF_ALLMULTI) {
216 		if (*new_flags & FIF_ALLMULTI)
217 			rtwdev->hal.rx_fltr &= ~B_AX_A_MC;
218 		else
219 			rtwdev->hal.rx_fltr |= B_AX_A_MC;
220 	}
221 	if (changed_flags & FIF_FCSFAIL) {
222 		if (*new_flags & FIF_FCSFAIL)
223 			rtwdev->hal.rx_fltr |= B_AX_A_CRC32_ERR;
224 		else
225 			rtwdev->hal.rx_fltr &= ~B_AX_A_CRC32_ERR;
226 	}
227 	if (changed_flags & FIF_OTHER_BSS) {
228 		if (*new_flags & FIF_OTHER_BSS)
229 			rtwdev->hal.rx_fltr &= ~B_AX_A_A1_MATCH;
230 		else
231 			rtwdev->hal.rx_fltr |= B_AX_A_A1_MATCH;
232 	}
233 	if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
234 		if (*new_flags & FIF_BCN_PRBRESP_PROMISC) {
235 			rtwdev->hal.rx_fltr &= ~B_AX_A_BCN_CHK_EN;
236 			rtwdev->hal.rx_fltr &= ~B_AX_A_BC;
237 			rtwdev->hal.rx_fltr &= ~B_AX_A_A1_MATCH;
238 		} else {
239 			rtwdev->hal.rx_fltr |= B_AX_A_BCN_CHK_EN;
240 			rtwdev->hal.rx_fltr |= B_AX_A_BC;
241 			rtwdev->hal.rx_fltr |= B_AX_A_A1_MATCH;
242 		}
243 	}
244 	if (changed_flags & FIF_PROBE_REQ) {
245 		if (*new_flags & FIF_PROBE_REQ) {
246 			rtwdev->hal.rx_fltr &= ~B_AX_A_BC_CAM_MATCH;
247 			rtwdev->hal.rx_fltr &= ~B_AX_A_UC_CAM_MATCH;
248 		} else {
249 			rtwdev->hal.rx_fltr |= B_AX_A_BC_CAM_MATCH;
250 			rtwdev->hal.rx_fltr |= B_AX_A_UC_CAM_MATCH;
251 		}
252 	}
253 
254 	rtw89_write32_mask(rtwdev,
255 			   rtw89_mac_reg_by_idx(R_AX_RX_FLTR_OPT, RTW89_MAC_0),
256 			   B_AX_RX_FLTR_CFG_MASK,
257 			   rtwdev->hal.rx_fltr);
258 	if (!rtwdev->dbcc_en)
259 		goto out;
260 	rtw89_write32_mask(rtwdev,
261 			   rtw89_mac_reg_by_idx(R_AX_RX_FLTR_OPT, RTW89_MAC_1),
262 			   B_AX_RX_FLTR_CFG_MASK,
263 			   rtwdev->hal.rx_fltr);
264 
265 out:
266 	mutex_unlock(&rtwdev->mutex);
267 }
268 
269 static const u8 ac_to_fw_idx[IEEE80211_NUM_ACS] = {
270 	[IEEE80211_AC_VO] = 3,
271 	[IEEE80211_AC_VI] = 2,
272 	[IEEE80211_AC_BE] = 0,
273 	[IEEE80211_AC_BK] = 1,
274 };
275 
276 static u8 rtw89_aifsn_to_aifs(struct rtw89_dev *rtwdev,
277 			      struct rtw89_vif *rtwvif, u8 aifsn)
278 {
279 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
280 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
281 	u8 slot_time;
282 	u8 sifs;
283 
284 	slot_time = vif->bss_conf.use_short_slot ? 9 : 20;
285 	sifs = chan->band_type == RTW89_BAND_5G ? 16 : 10;
286 
287 	return aifsn * slot_time + sifs;
288 }
289 
290 static void ____rtw89_conf_tx_edca(struct rtw89_dev *rtwdev,
291 				   struct rtw89_vif *rtwvif, u16 ac)
292 {
293 	struct ieee80211_tx_queue_params *params = &rtwvif->tx_params[ac];
294 	u32 val;
295 	u8 ecw_max, ecw_min;
296 	u8 aifs;
297 
298 	/* 2^ecw - 1 = cw; ecw = log2(cw + 1) */
299 	ecw_max = ilog2(params->cw_max + 1);
300 	ecw_min = ilog2(params->cw_min + 1);
301 	aifs = rtw89_aifsn_to_aifs(rtwdev, rtwvif, params->aifs);
302 	val = FIELD_PREP(FW_EDCA_PARAM_TXOPLMT_MSK, params->txop) |
303 	      FIELD_PREP(FW_EDCA_PARAM_CWMAX_MSK, ecw_max) |
304 	      FIELD_PREP(FW_EDCA_PARAM_CWMIN_MSK, ecw_min) |
305 	      FIELD_PREP(FW_EDCA_PARAM_AIFS_MSK, aifs);
306 	rtw89_fw_h2c_set_edca(rtwdev, rtwvif, ac_to_fw_idx[ac], val);
307 }
308 
309 static const u32 ac_to_mu_edca_param[IEEE80211_NUM_ACS] = {
310 	[IEEE80211_AC_VO] = R_AX_MUEDCA_VO_PARAM_0,
311 	[IEEE80211_AC_VI] = R_AX_MUEDCA_VI_PARAM_0,
312 	[IEEE80211_AC_BE] = R_AX_MUEDCA_BE_PARAM_0,
313 	[IEEE80211_AC_BK] = R_AX_MUEDCA_BK_PARAM_0,
314 };
315 
316 static void ____rtw89_conf_tx_mu_edca(struct rtw89_dev *rtwdev,
317 				      struct rtw89_vif *rtwvif, u16 ac)
318 {
319 	struct ieee80211_tx_queue_params *params = &rtwvif->tx_params[ac];
320 	struct ieee80211_he_mu_edca_param_ac_rec *mu_edca;
321 	u8 aifs, aifsn;
322 	u16 timer_32us;
323 	u32 reg;
324 	u32 val;
325 
326 	if (!params->mu_edca)
327 		return;
328 
329 	mu_edca = &params->mu_edca_param_rec;
330 	aifsn = FIELD_GET(GENMASK(3, 0), mu_edca->aifsn);
331 	aifs = aifsn ? rtw89_aifsn_to_aifs(rtwdev, rtwvif, aifsn) : 0;
332 	timer_32us = mu_edca->mu_edca_timer << 8;
333 
334 	val = FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_TIMER_MASK, timer_32us) |
335 	      FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_CW_MASK, mu_edca->ecw_min_max) |
336 	      FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_AIFS_MASK, aifs);
337 	reg = rtw89_mac_reg_by_idx(ac_to_mu_edca_param[ac], rtwvif->mac_idx);
338 	rtw89_write32(rtwdev, reg, val);
339 
340 	rtw89_mac_set_hw_muedca_ctrl(rtwdev, rtwvif, true);
341 }
342 
343 static void __rtw89_conf_tx(struct rtw89_dev *rtwdev,
344 			    struct rtw89_vif *rtwvif, u16 ac)
345 {
346 	____rtw89_conf_tx_edca(rtwdev, rtwvif, ac);
347 	____rtw89_conf_tx_mu_edca(rtwdev, rtwvif, ac);
348 }
349 
350 static void rtw89_conf_tx(struct rtw89_dev *rtwdev,
351 			  struct rtw89_vif *rtwvif)
352 {
353 	u16 ac;
354 
355 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
356 		__rtw89_conf_tx(rtwdev, rtwvif, ac);
357 }
358 
359 static void rtw89_station_mode_sta_assoc(struct rtw89_dev *rtwdev,
360 					 struct ieee80211_vif *vif,
361 					 struct ieee80211_bss_conf *conf)
362 {
363 	struct ieee80211_sta *sta;
364 
365 	if (vif->type != NL80211_IFTYPE_STATION)
366 		return;
367 
368 	sta = ieee80211_find_sta(vif, conf->bssid);
369 	if (!sta) {
370 		rtw89_err(rtwdev, "can't find sta to set sta_assoc state\n");
371 		return;
372 	}
373 
374 	rtw89_vif_type_mapping(vif, true);
375 
376 	rtw89_core_sta_assoc(rtwdev, vif, sta);
377 }
378 
379 static void rtw89_ops_bss_info_changed(struct ieee80211_hw *hw,
380 				       struct ieee80211_vif *vif,
381 				       struct ieee80211_bss_conf *conf,
382 				       u64 changed)
383 {
384 	struct rtw89_dev *rtwdev = hw->priv;
385 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
386 
387 	mutex_lock(&rtwdev->mutex);
388 	rtw89_leave_ps_mode(rtwdev);
389 
390 	if (changed & BSS_CHANGED_ASSOC) {
391 		if (vif->cfg.assoc) {
392 			rtw89_station_mode_sta_assoc(rtwdev, vif, conf);
393 			rtw89_phy_set_bss_color(rtwdev, vif);
394 			rtw89_chip_cfg_txpwr_ul_tb_offset(rtwdev, vif);
395 			rtw89_mac_port_update(rtwdev, rtwvif);
396 			rtw89_mac_set_he_obss_narrow_bw_ru(rtwdev, vif);
397 			rtw89_store_op_chan(rtwdev, true);
398 		} else {
399 			/* Abort ongoing scan if cancel_scan isn't issued
400 			 * when disconnected by peer
401 			 */
402 			if (rtwdev->scanning)
403 				rtw89_hw_scan_abort(rtwdev, vif);
404 		}
405 	}
406 
407 	if (changed & BSS_CHANGED_BSSID) {
408 		ether_addr_copy(rtwvif->bssid, conf->bssid);
409 		rtw89_cam_bssid_changed(rtwdev, rtwvif);
410 		rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL);
411 	}
412 
413 	if (changed & BSS_CHANGED_BEACON)
414 		rtw89_fw_h2c_update_beacon(rtwdev, rtwvif);
415 
416 	if (changed & BSS_CHANGED_ERP_SLOT)
417 		rtw89_conf_tx(rtwdev, rtwvif);
418 
419 	if (changed & BSS_CHANGED_HE_BSS_COLOR)
420 		rtw89_phy_set_bss_color(rtwdev, vif);
421 
422 	if (changed & BSS_CHANGED_MU_GROUPS)
423 		rtw89_mac_bf_set_gid_table(rtwdev, vif, conf);
424 
425 	if (changed & BSS_CHANGED_P2P_PS)
426 		rtw89_process_p2p_ps(rtwdev, vif);
427 
428 	mutex_unlock(&rtwdev->mutex);
429 }
430 
431 static int rtw89_ops_start_ap(struct ieee80211_hw *hw,
432 			      struct ieee80211_vif *vif,
433 			      struct ieee80211_bss_conf *link_conf)
434 {
435 	struct rtw89_dev *rtwdev = hw->priv;
436 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
437 
438 	mutex_lock(&rtwdev->mutex);
439 	ether_addr_copy(rtwvif->bssid, vif->bss_conf.bssid);
440 	rtw89_cam_bssid_changed(rtwdev, rtwvif);
441 	rtw89_mac_port_update(rtwdev, rtwvif);
442 	rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, NULL);
443 	rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, NULL, RTW89_ROLE_TYPE_CHANGE);
444 	rtw89_fw_h2c_join_info(rtwdev, rtwvif, NULL, true);
445 	rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL);
446 	rtw89_chip_rfk_channel(rtwdev);
447 	mutex_unlock(&rtwdev->mutex);
448 
449 	return 0;
450 }
451 
452 static
453 void rtw89_ops_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
454 		       struct ieee80211_bss_conf *link_conf)
455 {
456 	struct rtw89_dev *rtwdev = hw->priv;
457 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
458 
459 	mutex_lock(&rtwdev->mutex);
460 	rtw89_mac_stop_ap(rtwdev, rtwvif);
461 	rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, NULL);
462 	rtw89_fw_h2c_join_info(rtwdev, rtwvif, NULL, true);
463 	mutex_unlock(&rtwdev->mutex);
464 }
465 
466 static int rtw89_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
467 			     bool set)
468 {
469 	struct rtw89_dev *rtwdev = hw->priv;
470 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
471 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
472 
473 	ieee80211_queue_work(rtwdev->hw, &rtwvif->update_beacon_work);
474 
475 	return 0;
476 }
477 
478 static int rtw89_ops_conf_tx(struct ieee80211_hw *hw,
479 			     struct ieee80211_vif *vif,
480 			     unsigned int link_id, u16 ac,
481 			     const struct ieee80211_tx_queue_params *params)
482 {
483 	struct rtw89_dev *rtwdev = hw->priv;
484 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
485 
486 	mutex_lock(&rtwdev->mutex);
487 	rtw89_leave_ps_mode(rtwdev);
488 	rtwvif->tx_params[ac] = *params;
489 	__rtw89_conf_tx(rtwdev, rtwvif, ac);
490 	mutex_unlock(&rtwdev->mutex);
491 
492 	return 0;
493 }
494 
495 static int __rtw89_ops_sta_state(struct ieee80211_hw *hw,
496 				 struct ieee80211_vif *vif,
497 				 struct ieee80211_sta *sta,
498 				 enum ieee80211_sta_state old_state,
499 				 enum ieee80211_sta_state new_state)
500 {
501 	struct rtw89_dev *rtwdev = hw->priv;
502 
503 	if (old_state == IEEE80211_STA_NOTEXIST &&
504 	    new_state == IEEE80211_STA_NONE)
505 		return rtw89_core_sta_add(rtwdev, vif, sta);
506 
507 	if (old_state == IEEE80211_STA_AUTH &&
508 	    new_state == IEEE80211_STA_ASSOC) {
509 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
510 			return 0; /* defer to bss_info_changed to have vif info */
511 		return rtw89_core_sta_assoc(rtwdev, vif, sta);
512 	}
513 
514 	if (old_state == IEEE80211_STA_ASSOC &&
515 	    new_state == IEEE80211_STA_AUTH)
516 		return rtw89_core_sta_disassoc(rtwdev, vif, sta);
517 
518 	if (old_state == IEEE80211_STA_AUTH &&
519 	    new_state == IEEE80211_STA_NONE)
520 		return rtw89_core_sta_disconnect(rtwdev, vif, sta);
521 
522 	if (old_state == IEEE80211_STA_NONE &&
523 	    new_state == IEEE80211_STA_NOTEXIST)
524 		return rtw89_core_sta_remove(rtwdev, vif, sta);
525 
526 	return 0;
527 }
528 
529 static int rtw89_ops_sta_state(struct ieee80211_hw *hw,
530 			       struct ieee80211_vif *vif,
531 			       struct ieee80211_sta *sta,
532 			       enum ieee80211_sta_state old_state,
533 			       enum ieee80211_sta_state new_state)
534 {
535 	struct rtw89_dev *rtwdev = hw->priv;
536 	int ret;
537 
538 	mutex_lock(&rtwdev->mutex);
539 	rtw89_leave_ps_mode(rtwdev);
540 	ret = __rtw89_ops_sta_state(hw, vif, sta, old_state, new_state);
541 	mutex_unlock(&rtwdev->mutex);
542 
543 	return ret;
544 }
545 
546 static int rtw89_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
547 			     struct ieee80211_vif *vif,
548 			     struct ieee80211_sta *sta,
549 			     struct ieee80211_key_conf *key)
550 {
551 	struct rtw89_dev *rtwdev = hw->priv;
552 	int ret = 0;
553 
554 	mutex_lock(&rtwdev->mutex);
555 	rtw89_leave_ps_mode(rtwdev);
556 
557 	switch (cmd) {
558 	case SET_KEY:
559 		rtw89_btc_ntfy_specific_packet(rtwdev, PACKET_EAPOL_END);
560 		ret = rtw89_cam_sec_key_add(rtwdev, vif, sta, key);
561 		if (ret && ret != -EOPNOTSUPP) {
562 			rtw89_err(rtwdev, "failed to add key to sec cam\n");
563 			goto out;
564 		}
565 		break;
566 	case DISABLE_KEY:
567 		rtw89_hci_flush_queues(rtwdev, BIT(rtwdev->hw->queues) - 1,
568 				       false);
569 		rtw89_mac_flush_txq(rtwdev, BIT(rtwdev->hw->queues) - 1, false);
570 		ret = rtw89_cam_sec_key_del(rtwdev, vif, sta, key, true);
571 		if (ret) {
572 			rtw89_err(rtwdev, "failed to remove key from sec cam\n");
573 			goto out;
574 		}
575 		break;
576 	}
577 
578 out:
579 	mutex_unlock(&rtwdev->mutex);
580 
581 	return ret;
582 }
583 
584 static int rtw89_ops_ampdu_action(struct ieee80211_hw *hw,
585 				  struct ieee80211_vif *vif,
586 				  struct ieee80211_ampdu_params *params)
587 {
588 	struct rtw89_dev *rtwdev = hw->priv;
589 	struct ieee80211_sta *sta = params->sta;
590 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
591 	u16 tid = params->tid;
592 	struct ieee80211_txq *txq = sta->txq[tid];
593 	struct rtw89_txq *rtwtxq = (struct rtw89_txq *)txq->drv_priv;
594 
595 	switch (params->action) {
596 	case IEEE80211_AMPDU_TX_START:
597 		return IEEE80211_AMPDU_TX_START_IMMEDIATE;
598 	case IEEE80211_AMPDU_TX_STOP_CONT:
599 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
600 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
601 		mutex_lock(&rtwdev->mutex);
602 		clear_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags);
603 		mutex_unlock(&rtwdev->mutex);
604 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
605 		break;
606 	case IEEE80211_AMPDU_TX_OPERATIONAL:
607 		mutex_lock(&rtwdev->mutex);
608 		set_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags);
609 		rtwsta->ampdu_params[tid].agg_num = params->buf_size;
610 		rtwsta->ampdu_params[tid].amsdu = params->amsdu;
611 		rtw89_leave_ps_mode(rtwdev);
612 		mutex_unlock(&rtwdev->mutex);
613 		break;
614 	case IEEE80211_AMPDU_RX_START:
615 		mutex_lock(&rtwdev->mutex);
616 		rtw89_fw_h2c_ba_cam(rtwdev, rtwsta, true, params);
617 		mutex_unlock(&rtwdev->mutex);
618 		break;
619 	case IEEE80211_AMPDU_RX_STOP:
620 		mutex_lock(&rtwdev->mutex);
621 		rtw89_fw_h2c_ba_cam(rtwdev, rtwsta, false, params);
622 		mutex_unlock(&rtwdev->mutex);
623 		break;
624 	default:
625 		WARN_ON(1);
626 		return -ENOTSUPP;
627 	}
628 
629 	return 0;
630 }
631 
632 static int rtw89_ops_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
633 {
634 	struct rtw89_dev *rtwdev = hw->priv;
635 
636 	mutex_lock(&rtwdev->mutex);
637 	rtw89_leave_ps_mode(rtwdev);
638 	if (test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
639 		rtw89_mac_update_rts_threshold(rtwdev, RTW89_MAC_0);
640 	mutex_unlock(&rtwdev->mutex);
641 
642 	return 0;
643 }
644 
645 static void rtw89_ops_sta_statistics(struct ieee80211_hw *hw,
646 				     struct ieee80211_vif *vif,
647 				     struct ieee80211_sta *sta,
648 				     struct station_info *sinfo)
649 {
650 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
651 
652 	sinfo->txrate = rtwsta->ra_report.txrate;
653 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
654 }
655 
656 static
657 void __rtw89_drop_packets(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif)
658 {
659 	struct rtw89_vif *rtwvif;
660 
661 	if (vif) {
662 		rtwvif = (struct rtw89_vif *)vif->drv_priv;
663 		rtw89_mac_pkt_drop_vif(rtwdev, rtwvif);
664 	} else {
665 		rtw89_for_each_rtwvif(rtwdev, rtwvif)
666 			rtw89_mac_pkt_drop_vif(rtwdev, rtwvif);
667 	}
668 }
669 
670 static void rtw89_ops_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
671 			    u32 queues, bool drop)
672 {
673 	struct rtw89_dev *rtwdev = hw->priv;
674 
675 	mutex_lock(&rtwdev->mutex);
676 	rtw89_leave_lps(rtwdev);
677 	rtw89_hci_flush_queues(rtwdev, queues, drop);
678 
679 	if (drop && RTW89_CHK_FW_FEATURE(PACKET_DROP, &rtwdev->fw))
680 		__rtw89_drop_packets(rtwdev, vif);
681 	else
682 		rtw89_mac_flush_txq(rtwdev, queues, drop);
683 
684 	mutex_unlock(&rtwdev->mutex);
685 }
686 
687 struct rtw89_iter_bitrate_mask_data {
688 	struct rtw89_dev *rtwdev;
689 	struct ieee80211_vif *vif;
690 	const struct cfg80211_bitrate_mask *mask;
691 };
692 
693 static void rtw89_ra_mask_info_update_iter(void *data, struct ieee80211_sta *sta)
694 {
695 	struct rtw89_iter_bitrate_mask_data *br_data = data;
696 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
697 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwsta->rtwvif);
698 
699 	if (vif != br_data->vif || vif->p2p)
700 		return;
701 
702 	rtwsta->use_cfg_mask = true;
703 	rtwsta->mask = *br_data->mask;
704 	rtw89_phy_ra_updata_sta(br_data->rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED);
705 }
706 
707 static void rtw89_ra_mask_info_update(struct rtw89_dev *rtwdev,
708 				      struct ieee80211_vif *vif,
709 				      const struct cfg80211_bitrate_mask *mask)
710 {
711 	struct rtw89_iter_bitrate_mask_data br_data = { .rtwdev = rtwdev,
712 							.vif = vif,
713 							.mask = mask};
714 
715 	ieee80211_iterate_stations_atomic(rtwdev->hw, rtw89_ra_mask_info_update_iter,
716 					  &br_data);
717 }
718 
719 static int rtw89_ops_set_bitrate_mask(struct ieee80211_hw *hw,
720 				      struct ieee80211_vif *vif,
721 				      const struct cfg80211_bitrate_mask *mask)
722 {
723 	struct rtw89_dev *rtwdev = hw->priv;
724 
725 	mutex_lock(&rtwdev->mutex);
726 	rtw89_phy_rate_pattern_vif(rtwdev, vif, mask);
727 	rtw89_ra_mask_info_update(rtwdev, vif, mask);
728 	mutex_unlock(&rtwdev->mutex);
729 
730 	return 0;
731 }
732 
733 static
734 int rtw89_ops_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
735 {
736 	struct rtw89_dev *rtwdev = hw->priv;
737 	struct rtw89_hal *hal = &rtwdev->hal;
738 
739 	if (rx_ant != hw->wiphy->available_antennas_rx && rx_ant != hal->antenna_rx)
740 		return -EINVAL;
741 
742 	mutex_lock(&rtwdev->mutex);
743 	hal->antenna_tx = tx_ant;
744 	hal->antenna_rx = rx_ant;
745 	hal->tx_path_diversity = false;
746 	mutex_unlock(&rtwdev->mutex);
747 
748 	return 0;
749 }
750 
751 static
752 int rtw89_ops_get_antenna(struct ieee80211_hw *hw,  u32 *tx_ant, u32 *rx_ant)
753 {
754 	struct rtw89_dev *rtwdev = hw->priv;
755 	struct rtw89_hal *hal = &rtwdev->hal;
756 
757 	*tx_ant = hal->antenna_tx;
758 	*rx_ant = hal->antenna_rx;
759 
760 	return 0;
761 }
762 
763 static void rtw89_ops_sw_scan_start(struct ieee80211_hw *hw,
764 				    struct ieee80211_vif *vif,
765 				    const u8 *mac_addr)
766 {
767 	struct rtw89_dev *rtwdev = hw->priv;
768 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
769 
770 	mutex_lock(&rtwdev->mutex);
771 	rtw89_core_scan_start(rtwdev, rtwvif, mac_addr, false);
772 	mutex_unlock(&rtwdev->mutex);
773 }
774 
775 static void rtw89_ops_sw_scan_complete(struct ieee80211_hw *hw,
776 				       struct ieee80211_vif *vif)
777 {
778 	struct rtw89_dev *rtwdev = hw->priv;
779 
780 	mutex_lock(&rtwdev->mutex);
781 	rtw89_core_scan_complete(rtwdev, vif, false);
782 	mutex_unlock(&rtwdev->mutex);
783 }
784 
785 static void rtw89_ops_reconfig_complete(struct ieee80211_hw *hw,
786 					enum ieee80211_reconfig_type reconfig_type)
787 {
788 	struct rtw89_dev *rtwdev = hw->priv;
789 
790 	if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART)
791 		rtw89_ser_recfg_done(rtwdev);
792 }
793 
794 static int rtw89_ops_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
795 			     struct ieee80211_scan_request *req)
796 {
797 	struct rtw89_dev *rtwdev = hw->priv;
798 	int ret = 0;
799 
800 	if (!RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw))
801 		return 1;
802 
803 	if (rtwdev->scanning)
804 		return -EBUSY;
805 
806 	mutex_lock(&rtwdev->mutex);
807 	rtw89_hw_scan_start(rtwdev, vif, req);
808 	ret = rtw89_hw_scan_offload(rtwdev, vif, true);
809 	if (ret) {
810 		rtw89_hw_scan_abort(rtwdev, vif);
811 		rtw89_err(rtwdev, "HW scan failed with status: %d\n", ret);
812 	}
813 	mutex_unlock(&rtwdev->mutex);
814 
815 	return ret;
816 }
817 
818 static void rtw89_ops_cancel_hw_scan(struct ieee80211_hw *hw,
819 				     struct ieee80211_vif *vif)
820 {
821 	struct rtw89_dev *rtwdev = hw->priv;
822 
823 	if (!RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw))
824 		return;
825 
826 	if (!rtwdev->scanning)
827 		return;
828 
829 	mutex_lock(&rtwdev->mutex);
830 	rtw89_hw_scan_abort(rtwdev, vif);
831 	mutex_unlock(&rtwdev->mutex);
832 }
833 
834 static void rtw89_ops_sta_rc_update(struct ieee80211_hw *hw,
835 				    struct ieee80211_vif *vif,
836 				    struct ieee80211_sta *sta, u32 changed)
837 {
838 	struct rtw89_dev *rtwdev = hw->priv;
839 
840 	rtw89_phy_ra_updata_sta(rtwdev, sta, changed);
841 }
842 
843 static int rtw89_ops_add_chanctx(struct ieee80211_hw *hw,
844 				 struct ieee80211_chanctx_conf *ctx)
845 {
846 	struct rtw89_dev *rtwdev = hw->priv;
847 	int ret;
848 
849 	mutex_lock(&rtwdev->mutex);
850 	ret = rtw89_chanctx_ops_add(rtwdev, ctx);
851 	mutex_unlock(&rtwdev->mutex);
852 
853 	return ret;
854 }
855 
856 static void rtw89_ops_remove_chanctx(struct ieee80211_hw *hw,
857 				     struct ieee80211_chanctx_conf *ctx)
858 {
859 	struct rtw89_dev *rtwdev = hw->priv;
860 
861 	mutex_lock(&rtwdev->mutex);
862 	rtw89_chanctx_ops_remove(rtwdev, ctx);
863 	mutex_unlock(&rtwdev->mutex);
864 }
865 
866 static void rtw89_ops_change_chanctx(struct ieee80211_hw *hw,
867 				     struct ieee80211_chanctx_conf *ctx,
868 				     u32 changed)
869 {
870 	struct rtw89_dev *rtwdev = hw->priv;
871 
872 	mutex_lock(&rtwdev->mutex);
873 	rtw89_chanctx_ops_change(rtwdev, ctx, changed);
874 	mutex_unlock(&rtwdev->mutex);
875 }
876 
877 static int rtw89_ops_assign_vif_chanctx(struct ieee80211_hw *hw,
878 					struct ieee80211_vif *vif,
879 					struct ieee80211_bss_conf *link_conf,
880 					struct ieee80211_chanctx_conf *ctx)
881 {
882 	struct rtw89_dev *rtwdev = hw->priv;
883 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
884 	int ret;
885 
886 	mutex_lock(&rtwdev->mutex);
887 	ret = rtw89_chanctx_ops_assign_vif(rtwdev, rtwvif, ctx);
888 	mutex_unlock(&rtwdev->mutex);
889 
890 	return ret;
891 }
892 
893 static void rtw89_ops_unassign_vif_chanctx(struct ieee80211_hw *hw,
894 					   struct ieee80211_vif *vif,
895 					   struct ieee80211_bss_conf *link_conf,
896 					   struct ieee80211_chanctx_conf *ctx)
897 {
898 	struct rtw89_dev *rtwdev = hw->priv;
899 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
900 
901 	mutex_lock(&rtwdev->mutex);
902 	rtw89_chanctx_ops_unassign_vif(rtwdev, rtwvif, ctx);
903 	mutex_unlock(&rtwdev->mutex);
904 }
905 
906 static void rtw89_set_tid_config_iter(void *data, struct ieee80211_sta *sta)
907 {
908 	struct cfg80211_tid_config *tid_config = data;
909 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
910 	struct rtw89_dev *rtwdev = rtwsta->rtwvif->rtwdev;
911 
912 	rtw89_core_set_tid_config(rtwdev, sta, tid_config);
913 }
914 
915 static int rtw89_ops_set_tid_config(struct ieee80211_hw *hw,
916 				    struct ieee80211_vif *vif,
917 				    struct ieee80211_sta *sta,
918 				    struct cfg80211_tid_config *tid_config)
919 {
920 	struct rtw89_dev *rtwdev = hw->priv;
921 
922 	mutex_lock(&rtwdev->mutex);
923 	if (sta)
924 		rtw89_core_set_tid_config(rtwdev, sta, tid_config);
925 	else
926 		ieee80211_iterate_stations_atomic(rtwdev->hw,
927 						  rtw89_set_tid_config_iter,
928 						  tid_config);
929 	mutex_unlock(&rtwdev->mutex);
930 
931 	return 0;
932 }
933 
934 #ifdef CONFIG_PM
935 static int rtw89_ops_suspend(struct ieee80211_hw *hw,
936 			     struct cfg80211_wowlan *wowlan)
937 {
938 	struct rtw89_dev *rtwdev = hw->priv;
939 	int ret;
940 
941 	set_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags);
942 	cancel_delayed_work_sync(&rtwdev->track_work);
943 
944 	mutex_lock(&rtwdev->mutex);
945 	ret = rtw89_wow_suspend(rtwdev, wowlan);
946 	mutex_unlock(&rtwdev->mutex);
947 
948 	if (ret) {
949 		rtw89_warn(rtwdev, "failed to suspend for wow %d\n", ret);
950 		clear_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags);
951 		return 1;
952 	}
953 
954 	return 0;
955 }
956 
957 static int rtw89_ops_resume(struct ieee80211_hw *hw)
958 {
959 	struct rtw89_dev *rtwdev = hw->priv;
960 	int ret;
961 
962 	mutex_lock(&rtwdev->mutex);
963 	ret = rtw89_wow_resume(rtwdev);
964 	if (ret)
965 		rtw89_warn(rtwdev, "failed to resume for wow %d\n", ret);
966 	mutex_unlock(&rtwdev->mutex);
967 
968 	clear_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags);
969 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->track_work,
970 				     RTW89_TRACK_WORK_PERIOD);
971 
972 	return ret ? 1 : 0;
973 }
974 
975 static void rtw89_ops_set_wakeup(struct ieee80211_hw *hw, bool enabled)
976 {
977 	struct rtw89_dev *rtwdev = hw->priv;
978 
979 	device_set_wakeup_enable(rtwdev->dev, enabled);
980 }
981 #endif
982 
983 const struct ieee80211_ops rtw89_ops = {
984 	.tx			= rtw89_ops_tx,
985 	.wake_tx_queue		= rtw89_ops_wake_tx_queue,
986 	.start			= rtw89_ops_start,
987 	.stop			= rtw89_ops_stop,
988 	.config			= rtw89_ops_config,
989 	.add_interface		= rtw89_ops_add_interface,
990 	.change_interface       = rtw89_ops_change_interface,
991 	.remove_interface	= rtw89_ops_remove_interface,
992 	.configure_filter	= rtw89_ops_configure_filter,
993 	.bss_info_changed	= rtw89_ops_bss_info_changed,
994 	.start_ap		= rtw89_ops_start_ap,
995 	.stop_ap		= rtw89_ops_stop_ap,
996 	.set_tim		= rtw89_ops_set_tim,
997 	.conf_tx		= rtw89_ops_conf_tx,
998 	.sta_state		= rtw89_ops_sta_state,
999 	.set_key		= rtw89_ops_set_key,
1000 	.ampdu_action		= rtw89_ops_ampdu_action,
1001 	.set_rts_threshold	= rtw89_ops_set_rts_threshold,
1002 	.sta_statistics		= rtw89_ops_sta_statistics,
1003 	.flush			= rtw89_ops_flush,
1004 	.set_bitrate_mask	= rtw89_ops_set_bitrate_mask,
1005 	.set_antenna		= rtw89_ops_set_antenna,
1006 	.get_antenna		= rtw89_ops_get_antenna,
1007 	.sw_scan_start		= rtw89_ops_sw_scan_start,
1008 	.sw_scan_complete	= rtw89_ops_sw_scan_complete,
1009 	.reconfig_complete	= rtw89_ops_reconfig_complete,
1010 	.hw_scan		= rtw89_ops_hw_scan,
1011 	.cancel_hw_scan		= rtw89_ops_cancel_hw_scan,
1012 	.add_chanctx		= rtw89_ops_add_chanctx,
1013 	.remove_chanctx		= rtw89_ops_remove_chanctx,
1014 	.change_chanctx		= rtw89_ops_change_chanctx,
1015 	.assign_vif_chanctx	= rtw89_ops_assign_vif_chanctx,
1016 	.unassign_vif_chanctx	= rtw89_ops_unassign_vif_chanctx,
1017 	.set_sar_specs		= rtw89_ops_set_sar_specs,
1018 	.sta_rc_update		= rtw89_ops_sta_rc_update,
1019 	.set_tid_config		= rtw89_ops_set_tid_config,
1020 #ifdef CONFIG_PM
1021 	.suspend		= rtw89_ops_suspend,
1022 	.resume			= rtw89_ops_resume,
1023 	.set_wakeup		= rtw89_ops_set_wakeup,
1024 #endif
1025 };
1026 EXPORT_SYMBOL(rtw89_ops);
1027