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