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