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