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