xref: /linux/drivers/net/wireless/realtek/rtw88/mac80211.c (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019  Realtek Corporation
3  */
4 
5 #include "main.h"
6 #include "sec.h"
7 #include "tx.h"
8 #include "fw.h"
9 #include "mac.h"
10 #include "coex.h"
11 #include "ps.h"
12 #include "reg.h"
13 #include "bf.h"
14 #include "debug.h"
15 #include "wow.h"
16 #include "sar.h"
17 
rtw_ops_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)18 static void rtw_ops_tx(struct ieee80211_hw *hw,
19 		       struct ieee80211_tx_control *control,
20 		       struct sk_buff *skb)
21 {
22 	struct rtw_dev *rtwdev = hw->priv;
23 
24 	if (!test_bit(RTW_FLAG_RUNNING, rtwdev->flags)) {
25 		ieee80211_free_txskb(hw, skb);
26 		return;
27 	}
28 
29 	rtw_tx(rtwdev, control, skb);
30 }
31 
rtw_ops_wake_tx_queue(struct ieee80211_hw * hw,struct ieee80211_txq * txq)32 static void rtw_ops_wake_tx_queue(struct ieee80211_hw *hw,
33 				  struct ieee80211_txq *txq)
34 {
35 	struct rtw_dev *rtwdev = hw->priv;
36 	struct rtw_txq *rtwtxq = (struct rtw_txq *)txq->drv_priv;
37 
38 	if (!test_bit(RTW_FLAG_RUNNING, rtwdev->flags))
39 		return;
40 
41 	spin_lock_bh(&rtwdev->txq_lock);
42 	if (list_empty(&rtwtxq->list))
43 		list_add_tail(&rtwtxq->list, &rtwdev->txqs);
44 	spin_unlock_bh(&rtwdev->txq_lock);
45 
46 	/* ensure to dequeue EAPOL (4/4) at the right time */
47 	if (txq->ac == IEEE80211_AC_VO)
48 		__rtw_tx_work(rtwdev);
49 	else
50 		queue_work(rtwdev->tx_wq, &rtwdev->tx_work);
51 }
52 
rtw_ops_start(struct ieee80211_hw * hw)53 static int rtw_ops_start(struct ieee80211_hw *hw)
54 {
55 	struct rtw_dev *rtwdev = hw->priv;
56 	int ret;
57 
58 	mutex_lock(&rtwdev->mutex);
59 	ret = rtw_core_start(rtwdev);
60 	mutex_unlock(&rtwdev->mutex);
61 
62 	return ret;
63 }
64 
rtw_ops_stop(struct ieee80211_hw * hw,bool suspend)65 static void rtw_ops_stop(struct ieee80211_hw *hw, bool suspend)
66 {
67 	struct rtw_dev *rtwdev = hw->priv;
68 
69 	mutex_lock(&rtwdev->mutex);
70 	rtw_core_stop(rtwdev);
71 	mutex_unlock(&rtwdev->mutex);
72 }
73 
rtw_ops_config(struct ieee80211_hw * hw,int radio_idx,u32 changed)74 static int rtw_ops_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
75 {
76 	struct rtw_dev *rtwdev = hw->priv;
77 	int ret = 0;
78 
79 	/* let previous ips work finish to ensure we don't leave ips twice */
80 	cancel_work_sync(&rtwdev->ips_work);
81 
82 	mutex_lock(&rtwdev->mutex);
83 
84 	rtw_leave_lps_deep(rtwdev);
85 
86 	if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
87 	    !(hw->conf.flags & IEEE80211_CONF_IDLE)) {
88 		ret = rtw_leave_ips(rtwdev);
89 		if (ret) {
90 			rtw_err(rtwdev, "failed to leave idle state\n");
91 			goto out;
92 		}
93 	}
94 
95 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL)
96 		rtw_set_channel(rtwdev);
97 
98 	if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
99 	    (hw->conf.flags & IEEE80211_CONF_IDLE) &&
100 	    !test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
101 		rtw_enter_ips(rtwdev);
102 
103 out:
104 	mutex_unlock(&rtwdev->mutex);
105 	return ret;
106 }
107 
108 static const struct rtw_vif_port rtw_vif_port[] = {
109 	[0] = {
110 		.mac_addr	= {.addr = 0x0610},
111 		.bssid		= {.addr = 0x0618},
112 		.net_type	= {.addr = 0x0100, .mask = 0x30000},
113 		.aid		= {.addr = 0x06a8, .mask = 0x7ff},
114 		.bcn_ctrl	= {.addr = 0x0550, .mask = 0xff},
115 	},
116 	[1] = {
117 		.mac_addr	= {.addr = 0x0700},
118 		.bssid		= {.addr = 0x0708},
119 		.net_type	= {.addr = 0x0100, .mask = 0xc0000},
120 		.aid		= {.addr = 0x0710, .mask = 0x7ff},
121 		.bcn_ctrl	= {.addr = 0x0551, .mask = 0xff},
122 	},
123 	[2] = {
124 		.mac_addr	= {.addr = 0x1620},
125 		.bssid		= {.addr = 0x1628},
126 		.net_type	= {.addr = 0x1100, .mask = 0x3},
127 		.aid		= {.addr = 0x1600, .mask = 0x7ff},
128 		.bcn_ctrl	= {.addr = 0x0578, .mask = 0xff},
129 	},
130 	[3] = {
131 		.mac_addr	= {.addr = 0x1630},
132 		.bssid		= {.addr = 0x1638},
133 		.net_type	= {.addr = 0x1100, .mask = 0xc},
134 		.aid		= {.addr = 0x1604, .mask = 0x7ff},
135 		.bcn_ctrl	= {.addr = 0x0579, .mask = 0xff},
136 	},
137 	[4] = {
138 		.mac_addr	= {.addr = 0x1640},
139 		.bssid		= {.addr = 0x1648},
140 		.net_type	= {.addr = 0x1100, .mask = 0x30},
141 		.aid		= {.addr = 0x1608, .mask = 0x7ff},
142 		.bcn_ctrl	= {.addr = 0x057a, .mask = 0xff},
143 	},
144 };
145 
rtw_ops_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)146 static int rtw_ops_add_interface(struct ieee80211_hw *hw,
147 				 struct ieee80211_vif *vif)
148 {
149 	struct rtw_dev *rtwdev = hw->priv;
150 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
151 	enum rtw_net_type net_type;
152 	u32 config = 0;
153 	u8 port;
154 	u8 bcn_ctrl = 0;
155 
156 	if (rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_BCN_FILTER))
157 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
158 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
159 	rtwvif->stats.tx_unicast = 0;
160 	rtwvif->stats.rx_unicast = 0;
161 	rtwvif->stats.tx_cnt = 0;
162 	rtwvif->stats.rx_cnt = 0;
163 	rtwvif->scan_req = NULL;
164 	memset(&rtwvif->bfee, 0, sizeof(struct rtw_bfee));
165 	rtw_txq_init(rtwdev, vif->txq);
166 	INIT_LIST_HEAD(&rtwvif->rsvd_page_list);
167 
168 	mutex_lock(&rtwdev->mutex);
169 
170 	rtwvif->mac_id = rtw_acquire_macid(rtwdev);
171 	if (rtwvif->mac_id >= RTW_MAX_MAC_ID_NUM) {
172 		mutex_unlock(&rtwdev->mutex);
173 		return -ENOSPC;
174 	}
175 
176 	port = find_first_zero_bit(rtwdev->hw_port, RTW_PORT_NUM);
177 	if (port >= RTW_PORT_NUM) {
178 		mutex_unlock(&rtwdev->mutex);
179 		return -EINVAL;
180 	}
181 	set_bit(port, rtwdev->hw_port);
182 
183 	rtwvif->port = port;
184 	rtwvif->conf = &rtw_vif_port[port];
185 	rtw_leave_lps_deep(rtwdev);
186 
187 	switch (vif->type) {
188 	case NL80211_IFTYPE_AP:
189 	case NL80211_IFTYPE_MESH_POINT:
190 		rtw_add_rsvd_page_bcn(rtwdev, rtwvif);
191 		net_type = RTW_NET_AP_MODE;
192 		bcn_ctrl = BIT_EN_BCN_FUNCTION | BIT_DIS_TSF_UDT;
193 		break;
194 	case NL80211_IFTYPE_ADHOC:
195 		rtw_add_rsvd_page_bcn(rtwdev, rtwvif);
196 		net_type = RTW_NET_AD_HOC;
197 		bcn_ctrl = BIT_EN_BCN_FUNCTION | BIT_DIS_TSF_UDT;
198 		break;
199 	case NL80211_IFTYPE_STATION:
200 		rtw_add_rsvd_page_sta(rtwdev, rtwvif);
201 		net_type = RTW_NET_NO_LINK;
202 		bcn_ctrl = BIT_EN_BCN_FUNCTION;
203 		break;
204 	default:
205 		WARN_ON(1);
206 		clear_bit(rtwvif->port, rtwdev->hw_port);
207 		mutex_unlock(&rtwdev->mutex);
208 		return -EINVAL;
209 	}
210 
211 	ether_addr_copy(rtwvif->mac_addr, vif->addr);
212 	config |= PORT_SET_MAC_ADDR;
213 	rtwvif->net_type = net_type;
214 	config |= PORT_SET_NET_TYPE;
215 	rtwvif->bcn_ctrl = bcn_ctrl;
216 	config |= PORT_SET_BCN_CTRL;
217 	rtw_vif_port_config(rtwdev, rtwvif, config);
218 	rtw_core_port_switch(rtwdev, vif);
219 	rtw_recalc_lps(rtwdev, vif);
220 
221 	mutex_unlock(&rtwdev->mutex);
222 
223 	rtw_dbg(rtwdev, RTW_DBG_STATE, "start vif %pM mac_id %d on port %d\n",
224 		vif->addr, rtwvif->mac_id, rtwvif->port);
225 	return 0;
226 }
227 
rtw_ops_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)228 static void rtw_ops_remove_interface(struct ieee80211_hw *hw,
229 				     struct ieee80211_vif *vif)
230 {
231 	struct rtw_dev *rtwdev = hw->priv;
232 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
233 	u32 config = 0;
234 
235 	rtw_dbg(rtwdev, RTW_DBG_STATE, "stop vif %pM mac_id %d on port %d\n",
236 		vif->addr, rtwvif->mac_id, rtwvif->port);
237 
238 	mutex_lock(&rtwdev->mutex);
239 
240 	rtw_leave_lps_deep(rtwdev);
241 
242 	rtw_txq_cleanup(rtwdev, vif->txq);
243 	rtw_remove_rsvd_page(rtwdev, rtwvif);
244 
245 	eth_zero_addr(rtwvif->mac_addr);
246 	config |= PORT_SET_MAC_ADDR;
247 	rtwvif->net_type = RTW_NET_NO_LINK;
248 	config |= PORT_SET_NET_TYPE;
249 	rtwvif->bcn_ctrl = 0;
250 	config |= PORT_SET_BCN_CTRL;
251 	rtw_vif_port_config(rtwdev, rtwvif, config);
252 	clear_bit(rtwvif->port, rtwdev->hw_port);
253 	rtw_release_macid(rtwdev, rtwvif->mac_id);
254 	rtw_recalc_lps(rtwdev, NULL);
255 
256 	mutex_unlock(&rtwdev->mutex);
257 }
258 
rtw_ops_change_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif,enum nl80211_iftype type,bool p2p)259 static int rtw_ops_change_interface(struct ieee80211_hw *hw,
260 				    struct ieee80211_vif *vif,
261 				    enum nl80211_iftype type, bool p2p)
262 {
263 	struct rtw_dev *rtwdev = hw->priv;
264 
265 	rtw_dbg(rtwdev, RTW_DBG_STATE, "change vif %pM (%d)->(%d), p2p (%d)->(%d)\n",
266 		vif->addr, vif->type, type, vif->p2p, p2p);
267 
268 	rtw_ops_remove_interface(hw, vif);
269 
270 	vif->type = type;
271 	vif->p2p = p2p;
272 
273 	return rtw_ops_add_interface(hw, vif);
274 }
275 
rtw_ops_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * new_flags,u64 multicast)276 static void rtw_ops_configure_filter(struct ieee80211_hw *hw,
277 				     unsigned int changed_flags,
278 				     unsigned int *new_flags,
279 				     u64 multicast)
280 {
281 	struct rtw_dev *rtwdev = hw->priv;
282 
283 	*new_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_FCSFAIL |
284 		      FIF_BCN_PRBRESP_PROMISC;
285 
286 	mutex_lock(&rtwdev->mutex);
287 
288 	rtw_leave_lps_deep(rtwdev);
289 
290 	if (changed_flags & FIF_ALLMULTI) {
291 		if (*new_flags & FIF_ALLMULTI)
292 			rtwdev->hal.rcr |= BIT_AM;
293 		else
294 			rtwdev->hal.rcr &= ~(BIT_AM);
295 	}
296 	if (changed_flags & FIF_FCSFAIL) {
297 		if (*new_flags & FIF_FCSFAIL)
298 			rtwdev->hal.rcr |= BIT_ACRC32;
299 		else
300 			rtwdev->hal.rcr &= ~(BIT_ACRC32);
301 	}
302 	if (changed_flags & FIF_OTHER_BSS) {
303 		if (*new_flags & FIF_OTHER_BSS)
304 			rtwdev->hal.rcr |= BIT_AAP;
305 		else
306 			rtwdev->hal.rcr &= ~(BIT_AAP);
307 	}
308 	if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
309 		if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
310 			rtwdev->hal.rcr &= ~(BIT_CBSSID_BCN | BIT_CBSSID_DATA);
311 		else
312 			rtwdev->hal.rcr |= BIT_CBSSID_BCN;
313 	}
314 
315 	rtw_dbg(rtwdev, RTW_DBG_RX,
316 		"config rx filter, changed=0x%08x, new=0x%08x, rcr=0x%08x\n",
317 		changed_flags, *new_flags, rtwdev->hal.rcr);
318 
319 	rtw_write32(rtwdev, REG_RCR, rtwdev->hal.rcr);
320 
321 	mutex_unlock(&rtwdev->mutex);
322 }
323 
324 /* Only have one group of EDCA parameters now */
325 static const u32 ac_to_edca_param[IEEE80211_NUM_ACS] = {
326 	[IEEE80211_AC_VO] = REG_EDCA_VO_PARAM,
327 	[IEEE80211_AC_VI] = REG_EDCA_VI_PARAM,
328 	[IEEE80211_AC_BE] = REG_EDCA_BE_PARAM,
329 	[IEEE80211_AC_BK] = REG_EDCA_BK_PARAM,
330 };
331 
rtw_aifsn_to_aifs(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif,u8 aifsn)332 static u8 rtw_aifsn_to_aifs(struct rtw_dev *rtwdev,
333 			    struct rtw_vif *rtwvif, u8 aifsn)
334 {
335 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
336 	u8 slot_time;
337 	u8 sifs;
338 
339 	slot_time = vif->bss_conf.use_short_slot ? 9 : 20;
340 	sifs = rtwdev->hal.current_band_type == RTW_BAND_5G ? 16 : 10;
341 
342 	return aifsn * slot_time + sifs;
343 }
344 
__rtw_conf_tx(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif,u16 ac)345 static void __rtw_conf_tx(struct rtw_dev *rtwdev,
346 			  struct rtw_vif *rtwvif, u16 ac)
347 {
348 	struct ieee80211_tx_queue_params *params = &rtwvif->tx_params[ac];
349 	u32 edca_param = ac_to_edca_param[ac];
350 	u8 ecw_max, ecw_min;
351 	u8 aifs;
352 
353 	/* 2^ecw - 1 = cw; ecw = log2(cw + 1) */
354 	ecw_max = ilog2(params->cw_max + 1);
355 	ecw_min = ilog2(params->cw_min + 1);
356 	aifs = rtw_aifsn_to_aifs(rtwdev, rtwvif, params->aifs);
357 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_TXOP_LMT, params->txop);
358 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_CWMAX, ecw_max);
359 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_CWMIN, ecw_min);
360 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_AIFS, aifs);
361 }
362 
rtw_conf_tx(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif)363 static void rtw_conf_tx(struct rtw_dev *rtwdev,
364 			struct rtw_vif *rtwvif)
365 {
366 	u16 ac;
367 
368 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
369 		__rtw_conf_tx(rtwdev, rtwvif, ac);
370 }
371 
rtw_ops_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * conf,u64 changed)372 static void rtw_ops_bss_info_changed(struct ieee80211_hw *hw,
373 				     struct ieee80211_vif *vif,
374 				     struct ieee80211_bss_conf *conf,
375 				     u64 changed)
376 {
377 	struct rtw_dev *rtwdev = hw->priv;
378 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
379 	struct rtw_coex *coex = &rtwdev->coex;
380 	struct rtw_coex_stat *coex_stat = &coex->stat;
381 	u32 config = 0;
382 
383 	mutex_lock(&rtwdev->mutex);
384 
385 	rtw_leave_lps_deep(rtwdev);
386 
387 	if (changed & BSS_CHANGED_ASSOC) {
388 		rtw_vif_assoc_changed(rtwvif, conf);
389 		if (vif->cfg.assoc) {
390 			rtw_coex_connect_notify(rtwdev, COEX_ASSOCIATE_FINISH);
391 
392 			rtw_fw_download_rsvd_page(rtwdev);
393 			rtw_send_rsvd_page_h2c(rtwdev);
394 			rtw_fw_default_port(rtwdev, rtwvif);
395 			rtw_coex_media_status_notify(rtwdev, vif->cfg.assoc);
396 			if (rtw_bf_support)
397 				rtw_bf_assoc(rtwdev, vif, conf);
398 
399 			rtw_set_ampdu_factor(rtwdev, vif, conf);
400 
401 			rtw_fw_beacon_filter_config(rtwdev, true, vif);
402 		} else {
403 			rtw_leave_lps(rtwdev);
404 			rtw_bf_disassoc(rtwdev, vif, conf);
405 			/* Abort ongoing scan if cancel_scan isn't issued
406 			 * when disconnected by peer
407 			 */
408 			if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
409 				rtw_hw_scan_abort(rtwdev);
410 
411 		}
412 
413 		config |= PORT_SET_NET_TYPE;
414 		config |= PORT_SET_AID;
415 	}
416 
417 	if (changed & BSS_CHANGED_BSSID) {
418 		ether_addr_copy(rtwvif->bssid, conf->bssid);
419 		config |= PORT_SET_BSSID;
420 		if (!rtw_core_check_sta_active(rtwdev))
421 			rtw_clear_op_chan(rtwdev);
422 		else
423 			rtw_store_op_chan(rtwdev, true);
424 	}
425 
426 	if (changed & BSS_CHANGED_BEACON_INT) {
427 		if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_STATION)
428 			coex_stat->wl_beacon_interval = conf->beacon_int;
429 	}
430 
431 	if (changed & BSS_CHANGED_BEACON) {
432 		rtw_set_dtim_period(rtwdev, conf->dtim_period);
433 		rtw_fw_download_rsvd_page(rtwdev);
434 		rtw_send_rsvd_page_h2c(rtwdev);
435 	}
436 
437 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
438 		if (conf->enable_beacon)
439 			rtw_write32_set(rtwdev, REG_FWHW_TXQ_CTRL,
440 					BIT_EN_BCNQ_DL);
441 		else
442 			rtw_write32_clr(rtwdev, REG_FWHW_TXQ_CTRL,
443 					BIT_EN_BCNQ_DL);
444 	}
445 	if (changed & BSS_CHANGED_CQM)
446 		rtw_fw_beacon_filter_config(rtwdev, true, vif);
447 
448 	if (changed & BSS_CHANGED_MU_GROUPS)
449 		rtw_chip_set_gid_table(rtwdev, vif, conf);
450 
451 	if (changed & BSS_CHANGED_ERP_SLOT)
452 		rtw_conf_tx(rtwdev, rtwvif);
453 
454 	if (changed & BSS_CHANGED_PS)
455 		rtw_recalc_lps(rtwdev, NULL);
456 
457 	rtw_vif_port_config(rtwdev, rtwvif, config);
458 
459 	mutex_unlock(&rtwdev->mutex);
460 }
461 
rtw_ops_start_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)462 static int rtw_ops_start_ap(struct ieee80211_hw *hw,
463 			    struct ieee80211_vif *vif,
464 			    struct ieee80211_bss_conf *link_conf)
465 {
466 	struct rtw_dev *rtwdev = hw->priv;
467 	const struct rtw_chip_info *chip = rtwdev->chip;
468 
469 	mutex_lock(&rtwdev->mutex);
470 	rtw_write32_set(rtwdev, REG_TCR, BIT_TCR_UPDATE_HGQMD);
471 	rtwdev->ap_active = true;
472 	rtw_store_op_chan(rtwdev, true);
473 	chip->ops->phy_calibration(rtwdev);
474 	mutex_unlock(&rtwdev->mutex);
475 
476 	return 0;
477 }
478 
rtw_ops_stop_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)479 static void rtw_ops_stop_ap(struct ieee80211_hw *hw,
480 			    struct ieee80211_vif *vif,
481 			    struct ieee80211_bss_conf *link_conf)
482 {
483 	struct rtw_dev *rtwdev = hw->priv;
484 
485 	mutex_lock(&rtwdev->mutex);
486 	rtw_write32_clr(rtwdev, REG_TCR, BIT_TCR_UPDATE_HGQMD);
487 	rtwdev->ap_active = false;
488 	if (!rtw_core_check_sta_active(rtwdev))
489 		rtw_clear_op_chan(rtwdev);
490 	mutex_unlock(&rtwdev->mutex);
491 }
492 
rtw_ops_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,u16 ac,const struct ieee80211_tx_queue_params * params)493 static int rtw_ops_conf_tx(struct ieee80211_hw *hw,
494 			   struct ieee80211_vif *vif,
495 			   unsigned int link_id, u16 ac,
496 			   const struct ieee80211_tx_queue_params *params)
497 {
498 	struct rtw_dev *rtwdev = hw->priv;
499 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
500 
501 	mutex_lock(&rtwdev->mutex);
502 
503 	rtw_leave_lps_deep(rtwdev);
504 
505 	rtwvif->tx_params[ac] = *params;
506 	__rtw_conf_tx(rtwdev, rtwvif, ac);
507 
508 	mutex_unlock(&rtwdev->mutex);
509 
510 	return 0;
511 }
512 
rtw_ops_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)513 static int rtw_ops_sta_add(struct ieee80211_hw *hw,
514 			   struct ieee80211_vif *vif,
515 			   struct ieee80211_sta *sta)
516 {
517 	struct rtw_dev *rtwdev = hw->priv;
518 	int ret = 0;
519 
520 	mutex_lock(&rtwdev->mutex);
521 	ret = rtw_sta_add(rtwdev, sta, vif);
522 	mutex_unlock(&rtwdev->mutex);
523 
524 	return ret;
525 }
526 
rtw_ops_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)527 static int rtw_ops_sta_remove(struct ieee80211_hw *hw,
528 			      struct ieee80211_vif *vif,
529 			      struct ieee80211_sta *sta)
530 {
531 	struct rtw_dev *rtwdev = hw->priv;
532 
533 	mutex_lock(&rtwdev->mutex);
534 	rtw_fw_beacon_filter_config(rtwdev, false, vif);
535 	rtw_sta_remove(rtwdev, sta, true);
536 	mutex_unlock(&rtwdev->mutex);
537 
538 	return 0;
539 }
540 
rtw_ops_set_tim(struct ieee80211_hw * hw,struct ieee80211_sta * sta,bool set)541 static int rtw_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
542 			   bool set)
543 {
544 	struct rtw_dev *rtwdev = hw->priv;
545 
546 	ieee80211_queue_work(hw, &rtwdev->update_beacon_work);
547 
548 	return 0;
549 }
550 
rtw_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)551 static int rtw_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
552 			   struct ieee80211_vif *vif, struct ieee80211_sta *sta,
553 			   struct ieee80211_key_conf *key)
554 {
555 	struct rtw_dev *rtwdev = hw->priv;
556 	struct rtw_sec_desc *sec = &rtwdev->sec;
557 	u8 hw_key_type;
558 	u8 hw_key_idx;
559 	int ret = 0;
560 
561 	switch (key->cipher) {
562 	case WLAN_CIPHER_SUITE_WEP40:
563 		hw_key_type = RTW_CAM_WEP40;
564 		break;
565 	case WLAN_CIPHER_SUITE_WEP104:
566 		hw_key_type = RTW_CAM_WEP104;
567 		break;
568 	case WLAN_CIPHER_SUITE_TKIP:
569 		hw_key_type = RTW_CAM_TKIP;
570 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
571 		break;
572 	case WLAN_CIPHER_SUITE_CCMP:
573 		hw_key_type = RTW_CAM_AES;
574 		key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
575 		break;
576 	case WLAN_CIPHER_SUITE_AES_CMAC:
577 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
578 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
579 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
580 	case WLAN_CIPHER_SUITE_CCMP_256:
581 	case WLAN_CIPHER_SUITE_GCMP:
582 	case WLAN_CIPHER_SUITE_GCMP_256:
583 		/* suppress error messages */
584 		return -EOPNOTSUPP;
585 	default:
586 		return -ENOTSUPP;
587 	}
588 
589 	mutex_lock(&rtwdev->mutex);
590 
591 	rtw_leave_lps_deep(rtwdev);
592 
593 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
594 		hw_key_idx = rtw_sec_get_free_cam(sec);
595 	} else {
596 		/* multiple interfaces? */
597 		hw_key_idx = key->keyidx;
598 	}
599 
600 	if (hw_key_idx > sec->total_cam_num) {
601 		ret = -ENOSPC;
602 		goto out;
603 	}
604 
605 	switch (cmd) {
606 	case SET_KEY:
607 		/* need sw generated IV */
608 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
609 		key->hw_key_idx = hw_key_idx;
610 		rtw_sec_write_cam(rtwdev, sec, sta, key,
611 				  hw_key_type, hw_key_idx);
612 		break;
613 	case DISABLE_KEY:
614 		rtw_hci_flush_all_queues(rtwdev, false);
615 		rtw_mac_flush_all_queues(rtwdev, false);
616 		rtw_sec_clear_cam(rtwdev, sec, key->hw_key_idx);
617 		break;
618 	}
619 
620 	/* download new cam settings for PG to backup */
621 	if (rtw_get_lps_deep_mode(rtwdev) == LPS_DEEP_MODE_PG)
622 		rtw_fw_download_rsvd_page(rtwdev);
623 
624 out:
625 	mutex_unlock(&rtwdev->mutex);
626 
627 	return ret;
628 }
629 
rtw_ops_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)630 static int rtw_ops_ampdu_action(struct ieee80211_hw *hw,
631 				struct ieee80211_vif *vif,
632 				struct ieee80211_ampdu_params *params)
633 {
634 	struct ieee80211_sta *sta = params->sta;
635 	u16 tid = params->tid;
636 	struct ieee80211_txq *txq = sta->txq[tid];
637 	struct rtw_txq *rtwtxq = (struct rtw_txq *)txq->drv_priv;
638 
639 	switch (params->action) {
640 	case IEEE80211_AMPDU_TX_START:
641 		return IEEE80211_AMPDU_TX_START_IMMEDIATE;
642 	case IEEE80211_AMPDU_TX_STOP_CONT:
643 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
644 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
645 		clear_bit(RTW_TXQ_AMPDU, &rtwtxq->flags);
646 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
647 		break;
648 	case IEEE80211_AMPDU_TX_OPERATIONAL:
649 		set_bit(RTW_TXQ_AMPDU, &rtwtxq->flags);
650 		break;
651 	case IEEE80211_AMPDU_RX_START:
652 	case IEEE80211_AMPDU_RX_STOP:
653 		break;
654 	default:
655 		WARN_ON(1);
656 		return -ENOTSUPP;
657 	}
658 
659 	return 0;
660 }
661 
rtw_ops_can_aggregate_in_amsdu(struct ieee80211_hw * hw,struct sk_buff * head,struct sk_buff * skb)662 static bool rtw_ops_can_aggregate_in_amsdu(struct ieee80211_hw *hw,
663 					   struct sk_buff *head,
664 					   struct sk_buff *skb)
665 {
666 	struct rtw_dev *rtwdev = hw->priv;
667 	struct rtw_hal *hal = &rtwdev->hal;
668 
669 	/* we don't want to enable TX AMSDU on 2.4G */
670 	if (hal->current_band_type == RTW_BAND_2G)
671 		return false;
672 
673 	return true;
674 }
675 
rtw_ops_sw_scan_start(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const u8 * mac_addr)676 static void rtw_ops_sw_scan_start(struct ieee80211_hw *hw,
677 				  struct ieee80211_vif *vif,
678 				  const u8 *mac_addr)
679 {
680 	struct rtw_dev *rtwdev = hw->priv;
681 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
682 
683 	mutex_lock(&rtwdev->mutex);
684 	rtw_core_scan_start(rtwdev, rtwvif, mac_addr, false);
685 	mutex_unlock(&rtwdev->mutex);
686 }
687 
rtw_ops_sw_scan_complete(struct ieee80211_hw * hw,struct ieee80211_vif * vif)688 static void rtw_ops_sw_scan_complete(struct ieee80211_hw *hw,
689 				     struct ieee80211_vif *vif)
690 {
691 	struct rtw_dev *rtwdev = hw->priv;
692 
693 	mutex_lock(&rtwdev->mutex);
694 	rtw_core_scan_complete(rtwdev, vif, false);
695 	mutex_unlock(&rtwdev->mutex);
696 }
697 
rtw_ops_mgd_prepare_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_prep_tx_info * info)698 static void rtw_ops_mgd_prepare_tx(struct ieee80211_hw *hw,
699 				   struct ieee80211_vif *vif,
700 				   struct ieee80211_prep_tx_info *info)
701 {
702 	struct rtw_dev *rtwdev = hw->priv;
703 
704 	mutex_lock(&rtwdev->mutex);
705 	rtw_leave_lps_deep(rtwdev);
706 	rtw_coex_connect_notify(rtwdev, COEX_ASSOCIATE_START);
707 	rtw_chip_prepare_tx(rtwdev);
708 	mutex_unlock(&rtwdev->mutex);
709 }
710 
rtw_ops_set_rts_threshold(struct ieee80211_hw * hw,int radio_idx,u32 value)711 static int rtw_ops_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
712 				     u32 value)
713 {
714 	struct rtw_dev *rtwdev = hw->priv;
715 
716 	mutex_lock(&rtwdev->mutex);
717 	rtwdev->rts_threshold = value;
718 	mutex_unlock(&rtwdev->mutex);
719 
720 	return 0;
721 }
722 
rtw_ops_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)723 static void rtw_ops_sta_statistics(struct ieee80211_hw *hw,
724 				   struct ieee80211_vif *vif,
725 				   struct ieee80211_sta *sta,
726 				   struct station_info *sinfo)
727 {
728 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
729 
730 	sinfo->txrate = si->ra_report.txrate;
731 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
732 }
733 
rtw_ops_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)734 static void rtw_ops_flush(struct ieee80211_hw *hw,
735 			  struct ieee80211_vif *vif,
736 			  u32 queues, bool drop)
737 {
738 	struct rtw_dev *rtwdev = hw->priv;
739 
740 	mutex_lock(&rtwdev->mutex);
741 	rtw_leave_lps_deep(rtwdev);
742 
743 	rtw_hci_flush_queues(rtwdev, queues, drop);
744 	rtw_mac_flush_queues(rtwdev, queues, drop);
745 	mutex_unlock(&rtwdev->mutex);
746 }
747 
748 struct rtw_iter_bitrate_mask_data {
749 	struct rtw_dev *rtwdev;
750 	struct ieee80211_vif *vif;
751 	const struct cfg80211_bitrate_mask *mask;
752 };
753 
rtw_ra_mask_info_update_iter(void * data,struct ieee80211_sta * sta)754 static void rtw_ra_mask_info_update_iter(void *data, struct ieee80211_sta *sta)
755 {
756 	struct rtw_iter_bitrate_mask_data *br_data = data;
757 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
758 
759 	if (si->vif != br_data->vif)
760 		return;
761 
762 	/* free previous mask setting */
763 	kfree(si->mask);
764 	si->mask = kmemdup(br_data->mask, sizeof(struct cfg80211_bitrate_mask),
765 			   GFP_ATOMIC);
766 	if (!si->mask) {
767 		si->use_cfg_mask = false;
768 		return;
769 	}
770 
771 	si->use_cfg_mask = true;
772 	rtw_update_sta_info(br_data->rtwdev, si, true);
773 }
774 
rtw_ra_mask_info_update(struct rtw_dev * rtwdev,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)775 static void rtw_ra_mask_info_update(struct rtw_dev *rtwdev,
776 				    struct ieee80211_vif *vif,
777 				    const struct cfg80211_bitrate_mask *mask)
778 {
779 	struct rtw_iter_bitrate_mask_data br_data;
780 
781 	br_data.rtwdev = rtwdev;
782 	br_data.vif = vif;
783 	br_data.mask = mask;
784 	rtw_iterate_stas(rtwdev, rtw_ra_mask_info_update_iter, &br_data);
785 }
786 
rtw_ops_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)787 static int rtw_ops_set_bitrate_mask(struct ieee80211_hw *hw,
788 				    struct ieee80211_vif *vif,
789 				    const struct cfg80211_bitrate_mask *mask)
790 {
791 	struct rtw_dev *rtwdev = hw->priv;
792 
793 	mutex_lock(&rtwdev->mutex);
794 	rtw_ra_mask_info_update(rtwdev, vif, mask);
795 	mutex_unlock(&rtwdev->mutex);
796 
797 	return 0;
798 }
799 
rtw_ops_set_antenna(struct ieee80211_hw * hw,int radio_idx,u32 tx_antenna,u32 rx_antenna)800 static int rtw_ops_set_antenna(struct ieee80211_hw *hw,
801 			       int radio_idx,
802 			       u32 tx_antenna,
803 			       u32 rx_antenna)
804 {
805 	struct rtw_dev *rtwdev = hw->priv;
806 	const struct rtw_chip_info *chip = rtwdev->chip;
807 	int ret;
808 
809 	if (!chip->ops->set_antenna)
810 		return -EOPNOTSUPP;
811 
812 	mutex_lock(&rtwdev->mutex);
813 	ret = chip->ops->set_antenna(rtwdev, -1, tx_antenna, rx_antenna);
814 	mutex_unlock(&rtwdev->mutex);
815 
816 	return ret;
817 }
818 
rtw_ops_get_antenna(struct ieee80211_hw * hw,int radio_idx,u32 * tx_antenna,u32 * rx_antenna)819 static int rtw_ops_get_antenna(struct ieee80211_hw *hw,
820 			       int radio_idx,
821 			       u32 *tx_antenna,
822 			       u32 *rx_antenna)
823 {
824 	struct rtw_dev *rtwdev = hw->priv;
825 	struct rtw_hal *hal = &rtwdev->hal;
826 
827 	*tx_antenna = hal->antenna_tx;
828 	*rx_antenna = hal->antenna_rx;
829 
830 	return 0;
831 }
832 
833 #ifdef CONFIG_PM
rtw_ops_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)834 static int rtw_ops_suspend(struct ieee80211_hw *hw,
835 			   struct cfg80211_wowlan *wowlan)
836 {
837 	struct rtw_dev *rtwdev = hw->priv;
838 	int ret;
839 
840 	mutex_lock(&rtwdev->mutex);
841 	ret = rtw_wow_suspend(rtwdev, wowlan);
842 	if (ret)
843 		rtw_err(rtwdev, "failed to suspend for wow %d\n", ret);
844 	mutex_unlock(&rtwdev->mutex);
845 
846 	return ret ? 1 : 0;
847 }
848 
rtw_ops_resume(struct ieee80211_hw * hw)849 static int rtw_ops_resume(struct ieee80211_hw *hw)
850 {
851 	struct rtw_dev *rtwdev = hw->priv;
852 	int ret;
853 
854 	mutex_lock(&rtwdev->mutex);
855 	ret = rtw_wow_resume(rtwdev);
856 	if (ret)
857 		rtw_err(rtwdev, "failed to resume for wow %d\n", ret);
858 	mutex_unlock(&rtwdev->mutex);
859 
860 	return ret ? 1 : 0;
861 }
862 
rtw_ops_set_wakeup(struct ieee80211_hw * hw,bool enabled)863 static void rtw_ops_set_wakeup(struct ieee80211_hw *hw, bool enabled)
864 {
865 	struct rtw_dev *rtwdev = hw->priv;
866 
867 	device_set_wakeup_enable(rtwdev->dev, enabled);
868 }
869 #endif
870 
rtw_reconfig_complete(struct ieee80211_hw * hw,enum ieee80211_reconfig_type reconfig_type)871 static void rtw_reconfig_complete(struct ieee80211_hw *hw,
872 				  enum ieee80211_reconfig_type reconfig_type)
873 {
874 	struct rtw_dev *rtwdev = hw->priv;
875 
876 	mutex_lock(&rtwdev->mutex);
877 	if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART)
878 		clear_bit(RTW_FLAG_RESTARTING, rtwdev->flags);
879 	mutex_unlock(&rtwdev->mutex);
880 }
881 
rtw_ops_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * req)882 static int rtw_ops_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
883 			   struct ieee80211_scan_request *req)
884 {
885 	struct rtw_dev *rtwdev = hw->priv;
886 	int ret;
887 
888 	if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_SCAN_OFFLOAD))
889 		return 1;
890 
891 	if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
892 		return -EBUSY;
893 
894 	mutex_lock(&rtwdev->mutex);
895 	rtw_hw_scan_start(rtwdev, vif, req);
896 	ret = rtw_hw_scan_offload(rtwdev, vif, true);
897 	if (ret) {
898 		rtw_hw_scan_abort(rtwdev);
899 		rtw_err(rtwdev, "HW scan failed with status: %d\n", ret);
900 	}
901 	mutex_unlock(&rtwdev->mutex);
902 
903 	return ret;
904 }
905 
rtw_ops_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)906 static void rtw_ops_cancel_hw_scan(struct ieee80211_hw *hw,
907 				   struct ieee80211_vif *vif)
908 {
909 	struct rtw_dev *rtwdev = hw->priv;
910 
911 	if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_SCAN_OFFLOAD))
912 		return;
913 
914 	if (!test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
915 		return;
916 
917 	mutex_lock(&rtwdev->mutex);
918 	rtw_hw_scan_abort(rtwdev);
919 	mutex_unlock(&rtwdev->mutex);
920 }
921 
rtw_ops_set_sar_specs(struct ieee80211_hw * hw,const struct cfg80211_sar_specs * sar)922 static int rtw_ops_set_sar_specs(struct ieee80211_hw *hw,
923 				 const struct cfg80211_sar_specs *sar)
924 {
925 	struct rtw_dev *rtwdev = hw->priv;
926 
927 	mutex_lock(&rtwdev->mutex);
928 	rtw_set_sar_specs(rtwdev, sar);
929 	mutex_unlock(&rtwdev->mutex);
930 
931 	return 0;
932 }
933 
rtw_ops_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta,u32 changed)934 static void rtw_ops_sta_rc_update(struct ieee80211_hw *hw,
935 				  struct ieee80211_vif *vif,
936 				  struct ieee80211_link_sta *link_sta,
937 				  u32 changed)
938 {
939 	struct ieee80211_sta *sta = link_sta->sta;
940 	struct rtw_dev *rtwdev = hw->priv;
941 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
942 
943 	if (changed & IEEE80211_RC_BW_CHANGED)
944 		ieee80211_queue_work(rtwdev->hw, &si->rc_work);
945 }
946 
947 const struct ieee80211_ops rtw_ops = {
948 	.add_chanctx = ieee80211_emulate_add_chanctx,
949 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
950 	.change_chanctx = ieee80211_emulate_change_chanctx,
951 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
952 	.tx			= rtw_ops_tx,
953 	.wake_tx_queue		= rtw_ops_wake_tx_queue,
954 	.start			= rtw_ops_start,
955 	.stop			= rtw_ops_stop,
956 	.config			= rtw_ops_config,
957 	.add_interface		= rtw_ops_add_interface,
958 	.remove_interface	= rtw_ops_remove_interface,
959 	.change_interface	= rtw_ops_change_interface,
960 	.configure_filter	= rtw_ops_configure_filter,
961 	.bss_info_changed	= rtw_ops_bss_info_changed,
962 	.start_ap		= rtw_ops_start_ap,
963 	.stop_ap		= rtw_ops_stop_ap,
964 	.conf_tx		= rtw_ops_conf_tx,
965 	.sta_add		= rtw_ops_sta_add,
966 	.sta_remove		= rtw_ops_sta_remove,
967 	.set_tim		= rtw_ops_set_tim,
968 	.set_key		= rtw_ops_set_key,
969 	.ampdu_action		= rtw_ops_ampdu_action,
970 	.can_aggregate_in_amsdu	= rtw_ops_can_aggregate_in_amsdu,
971 	.sw_scan_start		= rtw_ops_sw_scan_start,
972 	.sw_scan_complete	= rtw_ops_sw_scan_complete,
973 	.mgd_prepare_tx		= rtw_ops_mgd_prepare_tx,
974 	.set_rts_threshold	= rtw_ops_set_rts_threshold,
975 	.sta_statistics		= rtw_ops_sta_statistics,
976 	.flush			= rtw_ops_flush,
977 	.set_bitrate_mask	= rtw_ops_set_bitrate_mask,
978 	.set_antenna		= rtw_ops_set_antenna,
979 	.get_antenna		= rtw_ops_get_antenna,
980 	.reconfig_complete	= rtw_reconfig_complete,
981 	.hw_scan		= rtw_ops_hw_scan,
982 	.cancel_hw_scan		= rtw_ops_cancel_hw_scan,
983 	.link_sta_rc_update	= rtw_ops_sta_rc_update,
984 	.set_sar_specs          = rtw_ops_set_sar_specs,
985 #ifdef CONFIG_PM
986 	.suspend		= rtw_ops_suspend,
987 	.resume			= rtw_ops_resume,
988 	.set_wakeup		= rtw_ops_set_wakeup,
989 #endif
990 };
991 EXPORT_SYMBOL(rtw_ops);
992