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