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 				       u64 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 (vif->cfg.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, true);
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,
385 			      struct ieee80211_vif *vif,
386 			      struct ieee80211_bss_conf *link_conf)
387 {
388 	struct rtw89_dev *rtwdev = hw->priv;
389 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
390 
391 	mutex_lock(&rtwdev->mutex);
392 	ether_addr_copy(rtwvif->bssid, vif->bss_conf.bssid);
393 	rtw89_cam_bssid_changed(rtwdev, rtwvif);
394 	rtw89_mac_port_update(rtwdev, rtwvif);
395 	rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, NULL);
396 	rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, NULL, RTW89_ROLE_TYPE_CHANGE);
397 	rtw89_fw_h2c_join_info(rtwdev, rtwvif, NULL, true);
398 	rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL);
399 	rtw89_chip_rfk_channel(rtwdev);
400 	mutex_unlock(&rtwdev->mutex);
401 
402 	return 0;
403 }
404 
405 static
406 void rtw89_ops_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
407 		       struct ieee80211_bss_conf *link_conf)
408 {
409 	struct rtw89_dev *rtwdev = hw->priv;
410 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
411 
412 	mutex_lock(&rtwdev->mutex);
413 	rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, NULL);
414 	rtw89_fw_h2c_join_info(rtwdev, rtwvif, NULL, true);
415 	mutex_unlock(&rtwdev->mutex);
416 }
417 
418 static int rtw89_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
419 			     bool set)
420 {
421 	struct rtw89_dev *rtwdev = hw->priv;
422 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
423 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
424 
425 	ieee80211_queue_work(rtwdev->hw, &rtwvif->update_beacon_work);
426 
427 	return 0;
428 }
429 
430 static int rtw89_ops_conf_tx(struct ieee80211_hw *hw,
431 			     struct ieee80211_vif *vif,
432 			     unsigned int link_id, u16 ac,
433 			     const struct ieee80211_tx_queue_params *params)
434 {
435 	struct rtw89_dev *rtwdev = hw->priv;
436 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
437 
438 	mutex_lock(&rtwdev->mutex);
439 	rtw89_leave_ps_mode(rtwdev);
440 	rtwvif->tx_params[ac] = *params;
441 	__rtw89_conf_tx(rtwdev, rtwvif, ac);
442 	mutex_unlock(&rtwdev->mutex);
443 
444 	return 0;
445 }
446 
447 static int __rtw89_ops_sta_state(struct ieee80211_hw *hw,
448 				 struct ieee80211_vif *vif,
449 				 struct ieee80211_sta *sta,
450 				 enum ieee80211_sta_state old_state,
451 				 enum ieee80211_sta_state new_state)
452 {
453 	struct rtw89_dev *rtwdev = hw->priv;
454 
455 	if (old_state == IEEE80211_STA_NOTEXIST &&
456 	    new_state == IEEE80211_STA_NONE)
457 		return rtw89_core_sta_add(rtwdev, vif, sta);
458 
459 	if (old_state == IEEE80211_STA_AUTH &&
460 	    new_state == IEEE80211_STA_ASSOC) {
461 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
462 			return 0; /* defer to bss_info_changed to have vif info */
463 		return rtw89_core_sta_assoc(rtwdev, vif, sta);
464 	}
465 
466 	if (old_state == IEEE80211_STA_ASSOC &&
467 	    new_state == IEEE80211_STA_AUTH)
468 		return rtw89_core_sta_disassoc(rtwdev, vif, sta);
469 
470 	if (old_state == IEEE80211_STA_AUTH &&
471 	    new_state == IEEE80211_STA_NONE)
472 		return rtw89_core_sta_disconnect(rtwdev, vif, sta);
473 
474 	if (old_state == IEEE80211_STA_NONE &&
475 	    new_state == IEEE80211_STA_NOTEXIST)
476 		return rtw89_core_sta_remove(rtwdev, vif, sta);
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 	int ret;
489 
490 	mutex_lock(&rtwdev->mutex);
491 	rtw89_leave_ps_mode(rtwdev);
492 	ret = __rtw89_ops_sta_state(hw, vif, sta, old_state, new_state);
493 	mutex_unlock(&rtwdev->mutex);
494 
495 	return ret;
496 }
497 
498 static int rtw89_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
499 			     struct ieee80211_vif *vif,
500 			     struct ieee80211_sta *sta,
501 			     struct ieee80211_key_conf *key)
502 {
503 	struct rtw89_dev *rtwdev = hw->priv;
504 	int ret = 0;
505 
506 	mutex_lock(&rtwdev->mutex);
507 	rtw89_leave_ps_mode(rtwdev);
508 
509 	switch (cmd) {
510 	case SET_KEY:
511 		rtw89_btc_ntfy_specific_packet(rtwdev, PACKET_EAPOL_END);
512 		ret = rtw89_cam_sec_key_add(rtwdev, vif, sta, key);
513 		if (ret && ret != -EOPNOTSUPP) {
514 			rtw89_err(rtwdev, "failed to add key to sec cam\n");
515 			goto out;
516 		}
517 		break;
518 	case DISABLE_KEY:
519 		rtw89_hci_flush_queues(rtwdev, BIT(rtwdev->hw->queues) - 1,
520 				       false);
521 		rtw89_mac_flush_txq(rtwdev, BIT(rtwdev->hw->queues) - 1, false);
522 		ret = rtw89_cam_sec_key_del(rtwdev, vif, sta, key, true);
523 		if (ret) {
524 			rtw89_err(rtwdev, "failed to remove key from sec cam\n");
525 			goto out;
526 		}
527 		break;
528 	}
529 
530 out:
531 	mutex_unlock(&rtwdev->mutex);
532 
533 	return ret;
534 }
535 
536 static int rtw89_ops_ampdu_action(struct ieee80211_hw *hw,
537 				  struct ieee80211_vif *vif,
538 				  struct ieee80211_ampdu_params *params)
539 {
540 	struct rtw89_dev *rtwdev = hw->priv;
541 	struct ieee80211_sta *sta = params->sta;
542 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
543 	u16 tid = params->tid;
544 	struct ieee80211_txq *txq = sta->txq[tid];
545 	struct rtw89_txq *rtwtxq = (struct rtw89_txq *)txq->drv_priv;
546 
547 	switch (params->action) {
548 	case IEEE80211_AMPDU_TX_START:
549 		return IEEE80211_AMPDU_TX_START_IMMEDIATE;
550 	case IEEE80211_AMPDU_TX_STOP_CONT:
551 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
552 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
553 		mutex_lock(&rtwdev->mutex);
554 		clear_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags);
555 		mutex_unlock(&rtwdev->mutex);
556 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
557 		break;
558 	case IEEE80211_AMPDU_TX_OPERATIONAL:
559 		mutex_lock(&rtwdev->mutex);
560 		set_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags);
561 		rtwsta->ampdu_params[tid].agg_num = params->buf_size;
562 		rtwsta->ampdu_params[tid].amsdu = params->amsdu;
563 		rtw89_leave_ps_mode(rtwdev);
564 		mutex_unlock(&rtwdev->mutex);
565 		break;
566 	case IEEE80211_AMPDU_RX_START:
567 		mutex_lock(&rtwdev->mutex);
568 		rtw89_fw_h2c_ba_cam(rtwdev, rtwsta, true, params);
569 		mutex_unlock(&rtwdev->mutex);
570 		break;
571 	case IEEE80211_AMPDU_RX_STOP:
572 		mutex_lock(&rtwdev->mutex);
573 		rtw89_fw_h2c_ba_cam(rtwdev, rtwsta, false, params);
574 		mutex_unlock(&rtwdev->mutex);
575 		break;
576 	default:
577 		WARN_ON(1);
578 		return -ENOTSUPP;
579 	}
580 
581 	return 0;
582 }
583 
584 static int rtw89_ops_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
585 {
586 	struct rtw89_dev *rtwdev = hw->priv;
587 
588 	mutex_lock(&rtwdev->mutex);
589 	rtw89_leave_ps_mode(rtwdev);
590 	if (test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
591 		rtw89_mac_update_rts_threshold(rtwdev, RTW89_MAC_0);
592 	mutex_unlock(&rtwdev->mutex);
593 
594 	return 0;
595 }
596 
597 static void rtw89_ops_sta_statistics(struct ieee80211_hw *hw,
598 				     struct ieee80211_vif *vif,
599 				     struct ieee80211_sta *sta,
600 				     struct station_info *sinfo)
601 {
602 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
603 
604 	sinfo->txrate = rtwsta->ra_report.txrate;
605 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
606 }
607 
608 static void rtw89_ops_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
609 			    u32 queues, bool drop)
610 {
611 	struct rtw89_dev *rtwdev = hw->priv;
612 
613 	mutex_lock(&rtwdev->mutex);
614 	rtw89_leave_lps(rtwdev);
615 	rtw89_hci_flush_queues(rtwdev, queues, drop);
616 	rtw89_mac_flush_txq(rtwdev, queues, drop);
617 	mutex_unlock(&rtwdev->mutex);
618 }
619 
620 struct rtw89_iter_bitrate_mask_data {
621 	struct rtw89_dev *rtwdev;
622 	struct ieee80211_vif *vif;
623 	const struct cfg80211_bitrate_mask *mask;
624 };
625 
626 static void rtw89_ra_mask_info_update_iter(void *data, struct ieee80211_sta *sta)
627 {
628 	struct rtw89_iter_bitrate_mask_data *br_data = data;
629 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
630 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwsta->rtwvif);
631 
632 	if (vif != br_data->vif)
633 		return;
634 
635 	rtwsta->use_cfg_mask = true;
636 	rtwsta->mask = *br_data->mask;
637 	rtw89_phy_ra_updata_sta(br_data->rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED);
638 }
639 
640 static void rtw89_ra_mask_info_update(struct rtw89_dev *rtwdev,
641 				      struct ieee80211_vif *vif,
642 				      const struct cfg80211_bitrate_mask *mask)
643 {
644 	struct rtw89_iter_bitrate_mask_data br_data = { .rtwdev = rtwdev,
645 							.vif = vif,
646 							.mask = mask};
647 
648 	ieee80211_iterate_stations_atomic(rtwdev->hw, rtw89_ra_mask_info_update_iter,
649 					  &br_data);
650 }
651 
652 static int rtw89_ops_set_bitrate_mask(struct ieee80211_hw *hw,
653 				      struct ieee80211_vif *vif,
654 				      const struct cfg80211_bitrate_mask *mask)
655 {
656 	struct rtw89_dev *rtwdev = hw->priv;
657 
658 	mutex_lock(&rtwdev->mutex);
659 	rtw89_phy_rate_pattern_vif(rtwdev, vif, mask);
660 	rtw89_ra_mask_info_update(rtwdev, vif, mask);
661 	mutex_unlock(&rtwdev->mutex);
662 
663 	return 0;
664 }
665 
666 static
667 int rtw89_ops_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
668 {
669 	struct rtw89_dev *rtwdev = hw->priv;
670 	struct rtw89_hal *hal = &rtwdev->hal;
671 
672 	if (rx_ant != hw->wiphy->available_antennas_rx)
673 		return -EINVAL;
674 
675 	mutex_lock(&rtwdev->mutex);
676 	hal->antenna_tx = tx_ant;
677 	hal->antenna_rx = rx_ant;
678 	mutex_unlock(&rtwdev->mutex);
679 
680 	return 0;
681 }
682 
683 static
684 int rtw89_ops_get_antenna(struct ieee80211_hw *hw,  u32 *tx_ant, u32 *rx_ant)
685 {
686 	struct rtw89_dev *rtwdev = hw->priv;
687 	struct rtw89_hal *hal = &rtwdev->hal;
688 
689 	*tx_ant = hal->antenna_tx;
690 	*rx_ant = hal->antenna_rx;
691 
692 	return 0;
693 }
694 
695 static void rtw89_ops_sw_scan_start(struct ieee80211_hw *hw,
696 				    struct ieee80211_vif *vif,
697 				    const u8 *mac_addr)
698 {
699 	struct rtw89_dev *rtwdev = hw->priv;
700 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
701 
702 	mutex_lock(&rtwdev->mutex);
703 	rtw89_core_scan_start(rtwdev, rtwvif, mac_addr, false);
704 	mutex_unlock(&rtwdev->mutex);
705 }
706 
707 static void rtw89_ops_sw_scan_complete(struct ieee80211_hw *hw,
708 				       struct ieee80211_vif *vif)
709 {
710 	struct rtw89_dev *rtwdev = hw->priv;
711 
712 	mutex_lock(&rtwdev->mutex);
713 	rtw89_core_scan_complete(rtwdev, vif, false);
714 	mutex_unlock(&rtwdev->mutex);
715 }
716 
717 static void rtw89_ops_reconfig_complete(struct ieee80211_hw *hw,
718 					enum ieee80211_reconfig_type reconfig_type)
719 {
720 	struct rtw89_dev *rtwdev = hw->priv;
721 
722 	if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART)
723 		rtw89_ser_recfg_done(rtwdev);
724 }
725 
726 static int rtw89_ops_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
727 			     struct ieee80211_scan_request *req)
728 {
729 	struct rtw89_dev *rtwdev = hw->priv;
730 	int ret = 0;
731 
732 	if (!RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw))
733 		return 1;
734 
735 	if (rtwdev->scanning)
736 		return -EBUSY;
737 
738 	mutex_lock(&rtwdev->mutex);
739 	rtw89_hw_scan_start(rtwdev, vif, req);
740 	ret = rtw89_hw_scan_offload(rtwdev, vif, true);
741 	if (ret) {
742 		rtw89_hw_scan_abort(rtwdev, vif);
743 		rtw89_err(rtwdev, "HW scan failed with status: %d\n", ret);
744 	}
745 	mutex_unlock(&rtwdev->mutex);
746 
747 	return ret;
748 }
749 
750 static void rtw89_ops_cancel_hw_scan(struct ieee80211_hw *hw,
751 				     struct ieee80211_vif *vif)
752 {
753 	struct rtw89_dev *rtwdev = hw->priv;
754 
755 	if (!RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw))
756 		return;
757 
758 	if (!rtwdev->scanning)
759 		return;
760 
761 	mutex_lock(&rtwdev->mutex);
762 	rtw89_hw_scan_abort(rtwdev, vif);
763 	mutex_unlock(&rtwdev->mutex);
764 }
765 
766 static void rtw89_ops_sta_rc_update(struct ieee80211_hw *hw,
767 				    struct ieee80211_vif *vif,
768 				    struct ieee80211_sta *sta, u32 changed)
769 {
770 	struct rtw89_dev *rtwdev = hw->priv;
771 
772 	rtw89_phy_ra_updata_sta(rtwdev, sta, changed);
773 }
774 
775 const struct ieee80211_ops rtw89_ops = {
776 	.tx			= rtw89_ops_tx,
777 	.wake_tx_queue		= rtw89_ops_wake_tx_queue,
778 	.start			= rtw89_ops_start,
779 	.stop			= rtw89_ops_stop,
780 	.config			= rtw89_ops_config,
781 	.add_interface		= rtw89_ops_add_interface,
782 	.remove_interface	= rtw89_ops_remove_interface,
783 	.configure_filter	= rtw89_ops_configure_filter,
784 	.bss_info_changed	= rtw89_ops_bss_info_changed,
785 	.start_ap		= rtw89_ops_start_ap,
786 	.stop_ap		= rtw89_ops_stop_ap,
787 	.set_tim		= rtw89_ops_set_tim,
788 	.conf_tx		= rtw89_ops_conf_tx,
789 	.sta_state		= rtw89_ops_sta_state,
790 	.set_key		= rtw89_ops_set_key,
791 	.ampdu_action		= rtw89_ops_ampdu_action,
792 	.set_rts_threshold	= rtw89_ops_set_rts_threshold,
793 	.sta_statistics		= rtw89_ops_sta_statistics,
794 	.flush			= rtw89_ops_flush,
795 	.set_bitrate_mask	= rtw89_ops_set_bitrate_mask,
796 	.set_antenna		= rtw89_ops_set_antenna,
797 	.get_antenna		= rtw89_ops_get_antenna,
798 	.sw_scan_start		= rtw89_ops_sw_scan_start,
799 	.sw_scan_complete	= rtw89_ops_sw_scan_complete,
800 	.reconfig_complete	= rtw89_ops_reconfig_complete,
801 	.hw_scan		= rtw89_ops_hw_scan,
802 	.cancel_hw_scan		= rtw89_ops_cancel_hw_scan,
803 	.set_sar_specs		= rtw89_ops_set_sar_specs,
804 	.sta_rc_update		= rtw89_ops_sta_rc_update,
805 };
806 EXPORT_SYMBOL(rtw89_ops);
807