xref: /linux/drivers/net/wireless/realtek/rtw88/mac80211.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
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 | FIF_CONTROL;
285 
286 	mutex_lock(&rtwdev->mutex);
287 
288 	rtw_leave_lps_deep(rtwdev);
289 
290 	if (changed_flags & FIF_CONTROL) {
291 		if (*new_flags & FIF_CONTROL)
292 			rtw_write16(rtwdev, REG_RXFLTMAP1, 0xffff);
293 		else
294 			rtw_write16(rtwdev, REG_RXFLTMAP1, rtwdev->hal.rxfltmap1);
295 	}
296 	if (changed_flags & FIF_ALLMULTI) {
297 		if (*new_flags & FIF_ALLMULTI)
298 			rtwdev->hal.rcr |= BIT_AM;
299 		else
300 			rtwdev->hal.rcr &= ~(BIT_AM);
301 	}
302 	if (changed_flags & FIF_FCSFAIL) {
303 		if (*new_flags & FIF_FCSFAIL)
304 			rtwdev->hal.rcr |= BIT_ACRC32;
305 		else
306 			rtwdev->hal.rcr &= ~(BIT_ACRC32);
307 	}
308 	if (changed_flags & FIF_OTHER_BSS) {
309 		if (*new_flags & FIF_OTHER_BSS)
310 			rtwdev->hal.rcr |= BIT_AAP;
311 		else
312 			rtwdev->hal.rcr &= ~(BIT_AAP);
313 	}
314 	if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
315 		if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
316 			rtwdev->hal.rcr &= ~(BIT_CBSSID_BCN | BIT_CBSSID_DATA);
317 		else
318 			rtwdev->hal.rcr |= BIT_CBSSID_BCN;
319 	}
320 
321 	rtw_dbg(rtwdev, RTW_DBG_RX,
322 		"config rx filter, changed=0x%08x, new=0x%08x, rcr=0x%08x\n",
323 		changed_flags, *new_flags, rtwdev->hal.rcr);
324 
325 	rtw_write32(rtwdev, REG_RCR, rtwdev->hal.rcr);
326 
327 	mutex_unlock(&rtwdev->mutex);
328 }
329 
330 /* Only have one group of EDCA parameters now */
331 static const u32 ac_to_edca_param[IEEE80211_NUM_ACS] = {
332 	[IEEE80211_AC_VO] = REG_EDCA_VO_PARAM,
333 	[IEEE80211_AC_VI] = REG_EDCA_VI_PARAM,
334 	[IEEE80211_AC_BE] = REG_EDCA_BE_PARAM,
335 	[IEEE80211_AC_BK] = REG_EDCA_BK_PARAM,
336 };
337 
rtw_aifsn_to_aifs(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif,u8 aifsn)338 static u8 rtw_aifsn_to_aifs(struct rtw_dev *rtwdev,
339 			    struct rtw_vif *rtwvif, u8 aifsn)
340 {
341 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
342 	u8 slot_time;
343 	u8 sifs;
344 
345 	slot_time = vif->bss_conf.use_short_slot ? 9 : 20;
346 	sifs = rtwdev->hal.current_band_type == RTW_BAND_5G ? 16 : 10;
347 
348 	return aifsn * slot_time + sifs;
349 }
350 
__rtw_conf_tx(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif,u16 ac)351 static void __rtw_conf_tx(struct rtw_dev *rtwdev,
352 			  struct rtw_vif *rtwvif, u16 ac)
353 {
354 	struct ieee80211_tx_queue_params *params = &rtwvif->tx_params[ac];
355 	u32 edca_param = ac_to_edca_param[ac];
356 	u8 ecw_max, ecw_min;
357 	u8 aifs;
358 
359 	/* 2^ecw - 1 = cw; ecw = log2(cw + 1) */
360 	ecw_max = ilog2(params->cw_max + 1);
361 	ecw_min = ilog2(params->cw_min + 1);
362 	aifs = rtw_aifsn_to_aifs(rtwdev, rtwvif, params->aifs);
363 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_TXOP_LMT, params->txop);
364 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_CWMAX, ecw_max);
365 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_CWMIN, ecw_min);
366 	rtw_write32_mask(rtwdev, edca_param, BIT_MASK_AIFS, aifs);
367 }
368 
rtw_conf_tx(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif)369 static void rtw_conf_tx(struct rtw_dev *rtwdev,
370 			struct rtw_vif *rtwvif)
371 {
372 	u16 ac;
373 
374 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
375 		__rtw_conf_tx(rtwdev, rtwvif, ac);
376 }
377 
rtw_ops_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * conf,u64 changed)378 static void rtw_ops_bss_info_changed(struct ieee80211_hw *hw,
379 				     struct ieee80211_vif *vif,
380 				     struct ieee80211_bss_conf *conf,
381 				     u64 changed)
382 {
383 	struct rtw_dev *rtwdev = hw->priv;
384 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
385 	struct rtw_coex *coex = &rtwdev->coex;
386 	struct rtw_coex_stat *coex_stat = &coex->stat;
387 	u32 config = 0;
388 
389 	mutex_lock(&rtwdev->mutex);
390 
391 	rtw_leave_lps_deep(rtwdev);
392 
393 	if (changed & BSS_CHANGED_ASSOC) {
394 		rtw_vif_assoc_changed(rtwvif, conf);
395 		if (vif->cfg.assoc) {
396 			rtw_coex_connect_notify(rtwdev, COEX_ASSOCIATE_FINISH);
397 
398 			rtw_fw_download_rsvd_page(rtwdev);
399 			rtw_send_rsvd_page_h2c(rtwdev);
400 			rtw_fw_default_port(rtwdev, rtwvif);
401 			rtw_coex_media_status_notify(rtwdev, vif->cfg.assoc);
402 			if (rtw_bf_support)
403 				rtw_bf_assoc(rtwdev, vif, conf);
404 
405 			rtw_set_ampdu_factor(rtwdev, vif, conf);
406 
407 			rtw_fw_beacon_filter_config(rtwdev, true, vif);
408 		} else {
409 			rtw_leave_lps(rtwdev);
410 			rtw_bf_disassoc(rtwdev, vif, conf);
411 			/* Abort ongoing scan if cancel_scan isn't issued
412 			 * when disconnected by peer
413 			 */
414 			if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
415 				rtw_hw_scan_abort(rtwdev);
416 
417 		}
418 
419 		config |= PORT_SET_NET_TYPE;
420 		config |= PORT_SET_AID;
421 	}
422 
423 	if (changed & BSS_CHANGED_BSSID) {
424 		ether_addr_copy(rtwvif->bssid, conf->bssid);
425 		config |= PORT_SET_BSSID;
426 		if (!rtw_core_check_sta_active(rtwdev))
427 			rtw_clear_op_chan(rtwdev);
428 		else
429 			rtw_store_op_chan(rtwdev, true);
430 	}
431 
432 	if (changed & BSS_CHANGED_BEACON_INT) {
433 		if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_STATION)
434 			coex_stat->wl_beacon_interval = conf->beacon_int;
435 	}
436 
437 	if (changed & BSS_CHANGED_BEACON) {
438 		rtw_set_dtim_period(rtwdev, conf->dtim_period);
439 		rtw_fw_download_rsvd_page(rtwdev);
440 		rtw_send_rsvd_page_h2c(rtwdev);
441 	}
442 
443 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
444 		if (conf->enable_beacon)
445 			rtw_write32_set(rtwdev, REG_FWHW_TXQ_CTRL,
446 					BIT_EN_BCNQ_DL);
447 		else
448 			rtw_write32_clr(rtwdev, REG_FWHW_TXQ_CTRL,
449 					BIT_EN_BCNQ_DL);
450 	}
451 	if (changed & BSS_CHANGED_CQM)
452 		rtw_fw_beacon_filter_config(rtwdev, true, vif);
453 
454 	if (changed & BSS_CHANGED_MU_GROUPS)
455 		rtw_chip_set_gid_table(rtwdev, vif, conf);
456 
457 	if (changed & BSS_CHANGED_ERP_SLOT)
458 		rtw_conf_tx(rtwdev, rtwvif);
459 
460 	if (changed & BSS_CHANGED_PS)
461 		rtw_recalc_lps(rtwdev, NULL);
462 
463 	rtw_vif_port_config(rtwdev, rtwvif, config);
464 
465 	mutex_unlock(&rtwdev->mutex);
466 }
467 
rtw_ops_start_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)468 static int rtw_ops_start_ap(struct ieee80211_hw *hw,
469 			    struct ieee80211_vif *vif,
470 			    struct ieee80211_bss_conf *link_conf)
471 {
472 	struct rtw_dev *rtwdev = hw->priv;
473 	const struct rtw_chip_info *chip = rtwdev->chip;
474 
475 	mutex_lock(&rtwdev->mutex);
476 	rtw_write32_set(rtwdev, REG_TCR, BIT_TCR_UPDATE_HGQMD);
477 	rtwdev->ap_active = true;
478 	rtw_store_op_chan(rtwdev, true);
479 	chip->ops->phy_calibration(rtwdev);
480 	mutex_unlock(&rtwdev->mutex);
481 
482 	return 0;
483 }
484 
rtw_ops_stop_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)485 static void rtw_ops_stop_ap(struct ieee80211_hw *hw,
486 			    struct ieee80211_vif *vif,
487 			    struct ieee80211_bss_conf *link_conf)
488 {
489 	struct rtw_dev *rtwdev = hw->priv;
490 
491 	mutex_lock(&rtwdev->mutex);
492 	rtw_write32_clr(rtwdev, REG_TCR, BIT_TCR_UPDATE_HGQMD);
493 	rtwdev->ap_active = false;
494 	if (!rtw_core_check_sta_active(rtwdev))
495 		rtw_clear_op_chan(rtwdev);
496 	mutex_unlock(&rtwdev->mutex);
497 }
498 
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)499 static int rtw_ops_conf_tx(struct ieee80211_hw *hw,
500 			   struct ieee80211_vif *vif,
501 			   unsigned int link_id, u16 ac,
502 			   const struct ieee80211_tx_queue_params *params)
503 {
504 	struct rtw_dev *rtwdev = hw->priv;
505 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
506 
507 	mutex_lock(&rtwdev->mutex);
508 
509 	rtw_leave_lps_deep(rtwdev);
510 
511 	rtwvif->tx_params[ac] = *params;
512 	__rtw_conf_tx(rtwdev, rtwvif, ac);
513 
514 	mutex_unlock(&rtwdev->mutex);
515 
516 	return 0;
517 }
518 
rtw_ops_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)519 static int rtw_ops_sta_add(struct ieee80211_hw *hw,
520 			   struct ieee80211_vif *vif,
521 			   struct ieee80211_sta *sta)
522 {
523 	struct rtw_dev *rtwdev = hw->priv;
524 	int ret = 0;
525 
526 	mutex_lock(&rtwdev->mutex);
527 	ret = rtw_sta_add(rtwdev, sta, vif);
528 	mutex_unlock(&rtwdev->mutex);
529 
530 	return ret;
531 }
532 
rtw_ops_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)533 static int rtw_ops_sta_remove(struct ieee80211_hw *hw,
534 			      struct ieee80211_vif *vif,
535 			      struct ieee80211_sta *sta)
536 {
537 	struct rtw_dev *rtwdev = hw->priv;
538 
539 	mutex_lock(&rtwdev->mutex);
540 	rtw_fw_beacon_filter_config(rtwdev, false, vif);
541 	rtw_sta_remove(rtwdev, sta, true);
542 	mutex_unlock(&rtwdev->mutex);
543 
544 	return 0;
545 }
546 
rtw_ops_set_tim(struct ieee80211_hw * hw,struct ieee80211_sta * sta,bool set)547 static int rtw_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
548 			   bool set)
549 {
550 	struct rtw_dev *rtwdev = hw->priv;
551 
552 	ieee80211_queue_work(hw, &rtwdev->update_beacon_work);
553 
554 	return 0;
555 }
556 
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)557 static int rtw_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
558 			   struct ieee80211_vif *vif, struct ieee80211_sta *sta,
559 			   struct ieee80211_key_conf *key)
560 {
561 	struct rtw_dev *rtwdev = hw->priv;
562 	struct rtw_sec_desc *sec = &rtwdev->sec;
563 	u8 hw_key_type;
564 	u8 hw_key_idx;
565 	int ret = 0;
566 
567 	switch (key->cipher) {
568 	case WLAN_CIPHER_SUITE_WEP40:
569 		hw_key_type = RTW_CAM_WEP40;
570 		break;
571 	case WLAN_CIPHER_SUITE_WEP104:
572 		hw_key_type = RTW_CAM_WEP104;
573 		break;
574 	case WLAN_CIPHER_SUITE_TKIP:
575 		hw_key_type = RTW_CAM_TKIP;
576 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
577 		break;
578 	case WLAN_CIPHER_SUITE_CCMP:
579 		hw_key_type = RTW_CAM_AES;
580 		key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
581 		break;
582 	case WLAN_CIPHER_SUITE_AES_CMAC:
583 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
584 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
585 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
586 	case WLAN_CIPHER_SUITE_CCMP_256:
587 	case WLAN_CIPHER_SUITE_GCMP:
588 	case WLAN_CIPHER_SUITE_GCMP_256:
589 		/* suppress error messages */
590 		return -EOPNOTSUPP;
591 	default:
592 		return -ENOTSUPP;
593 	}
594 
595 	mutex_lock(&rtwdev->mutex);
596 
597 	rtw_leave_lps_deep(rtwdev);
598 
599 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
600 		hw_key_idx = rtw_sec_get_free_cam(sec);
601 	} else {
602 		/* multiple interfaces? */
603 		hw_key_idx = key->keyidx;
604 	}
605 
606 	if (hw_key_idx > sec->total_cam_num) {
607 		ret = -ENOSPC;
608 		goto out;
609 	}
610 
611 	switch (cmd) {
612 	case SET_KEY:
613 		/* need sw generated IV */
614 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
615 		key->hw_key_idx = hw_key_idx;
616 		rtw_sec_write_cam(rtwdev, sec, sta, key,
617 				  hw_key_type, hw_key_idx);
618 		break;
619 	case DISABLE_KEY:
620 		rtw_hci_flush_all_queues(rtwdev, false);
621 		rtw_mac_flush_all_queues(rtwdev, false);
622 		rtw_sec_clear_cam(rtwdev, sec, key->hw_key_idx);
623 		break;
624 	}
625 
626 	/* download new cam settings for PG to backup */
627 	if (rtw_get_lps_deep_mode(rtwdev) == LPS_DEEP_MODE_PG)
628 		rtw_fw_download_rsvd_page(rtwdev);
629 
630 out:
631 	mutex_unlock(&rtwdev->mutex);
632 
633 	return ret;
634 }
635 
rtw_ops_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)636 static int rtw_ops_ampdu_action(struct ieee80211_hw *hw,
637 				struct ieee80211_vif *vif,
638 				struct ieee80211_ampdu_params *params)
639 {
640 	struct ieee80211_sta *sta = params->sta;
641 	u16 tid = params->tid;
642 	struct ieee80211_txq *txq = sta->txq[tid];
643 	struct rtw_txq *rtwtxq = (struct rtw_txq *)txq->drv_priv;
644 
645 	switch (params->action) {
646 	case IEEE80211_AMPDU_TX_START:
647 		return IEEE80211_AMPDU_TX_START_IMMEDIATE;
648 	case IEEE80211_AMPDU_TX_STOP_CONT:
649 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
650 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
651 		clear_bit(RTW_TXQ_AMPDU, &rtwtxq->flags);
652 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
653 		break;
654 	case IEEE80211_AMPDU_TX_OPERATIONAL:
655 		set_bit(RTW_TXQ_AMPDU, &rtwtxq->flags);
656 		break;
657 	case IEEE80211_AMPDU_RX_START:
658 	case IEEE80211_AMPDU_RX_STOP:
659 		break;
660 	default:
661 		WARN_ON(1);
662 		return -ENOTSUPP;
663 	}
664 
665 	return 0;
666 }
667 
rtw_ops_can_aggregate_in_amsdu(struct ieee80211_hw * hw,struct sk_buff * head,struct sk_buff * skb)668 static bool rtw_ops_can_aggregate_in_amsdu(struct ieee80211_hw *hw,
669 					   struct sk_buff *head,
670 					   struct sk_buff *skb)
671 {
672 	struct rtw_dev *rtwdev = hw->priv;
673 	struct rtw_hal *hal = &rtwdev->hal;
674 
675 	/* we don't want to enable TX AMSDU on 2.4G */
676 	if (hal->current_band_type == RTW_BAND_2G)
677 		return false;
678 
679 	return true;
680 }
681 
rtw_ops_sw_scan_start(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const u8 * mac_addr)682 static void rtw_ops_sw_scan_start(struct ieee80211_hw *hw,
683 				  struct ieee80211_vif *vif,
684 				  const u8 *mac_addr)
685 {
686 	struct rtw_dev *rtwdev = hw->priv;
687 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
688 
689 	mutex_lock(&rtwdev->mutex);
690 	rtw_core_scan_start(rtwdev, rtwvif, mac_addr, false);
691 	mutex_unlock(&rtwdev->mutex);
692 }
693 
rtw_ops_sw_scan_complete(struct ieee80211_hw * hw,struct ieee80211_vif * vif)694 static void rtw_ops_sw_scan_complete(struct ieee80211_hw *hw,
695 				     struct ieee80211_vif *vif)
696 {
697 	struct rtw_dev *rtwdev = hw->priv;
698 
699 	mutex_lock(&rtwdev->mutex);
700 	rtw_core_scan_complete(rtwdev, vif, false);
701 	mutex_unlock(&rtwdev->mutex);
702 }
703 
rtw_ops_mgd_prepare_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_prep_tx_info * info)704 static void rtw_ops_mgd_prepare_tx(struct ieee80211_hw *hw,
705 				   struct ieee80211_vif *vif,
706 				   struct ieee80211_prep_tx_info *info)
707 {
708 	struct rtw_dev *rtwdev = hw->priv;
709 
710 	mutex_lock(&rtwdev->mutex);
711 	rtw_leave_lps_deep(rtwdev);
712 	rtw_coex_connect_notify(rtwdev, COEX_ASSOCIATE_START);
713 	rtw_chip_prepare_tx(rtwdev);
714 	mutex_unlock(&rtwdev->mutex);
715 }
716 
rtw_ops_set_rts_threshold(struct ieee80211_hw * hw,int radio_idx,u32 value)717 static int rtw_ops_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
718 				     u32 value)
719 {
720 	struct rtw_dev *rtwdev = hw->priv;
721 
722 	mutex_lock(&rtwdev->mutex);
723 	rtwdev->rts_threshold = value;
724 	mutex_unlock(&rtwdev->mutex);
725 
726 	return 0;
727 }
728 
rtw_ops_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)729 static void rtw_ops_sta_statistics(struct ieee80211_hw *hw,
730 				   struct ieee80211_vif *vif,
731 				   struct ieee80211_sta *sta,
732 				   struct station_info *sinfo)
733 {
734 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
735 
736 	sinfo->txrate = si->ra_report.txrate;
737 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
738 }
739 
rtw_ops_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)740 static void rtw_ops_flush(struct ieee80211_hw *hw,
741 			  struct ieee80211_vif *vif,
742 			  u32 queues, bool drop)
743 {
744 	struct rtw_dev *rtwdev = hw->priv;
745 
746 	mutex_lock(&rtwdev->mutex);
747 	rtw_leave_lps_deep(rtwdev);
748 
749 	rtw_hci_flush_queues(rtwdev, queues, drop);
750 	rtw_mac_flush_queues(rtwdev, queues, drop);
751 	mutex_unlock(&rtwdev->mutex);
752 }
753 
754 struct rtw_iter_bitrate_mask_data {
755 	struct rtw_dev *rtwdev;
756 	struct ieee80211_vif *vif;
757 	const struct cfg80211_bitrate_mask *mask;
758 };
759 
rtw_ra_mask_info_update_iter(void * data,struct ieee80211_sta * sta)760 static void rtw_ra_mask_info_update_iter(void *data, struct ieee80211_sta *sta)
761 {
762 	struct rtw_iter_bitrate_mask_data *br_data = data;
763 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
764 
765 	if (si->vif != br_data->vif)
766 		return;
767 
768 	/* free previous mask setting */
769 	kfree(si->mask);
770 	si->mask = kmemdup(br_data->mask, sizeof(struct cfg80211_bitrate_mask),
771 			   GFP_ATOMIC);
772 	if (!si->mask) {
773 		si->use_cfg_mask = false;
774 		return;
775 	}
776 
777 	si->use_cfg_mask = true;
778 	rtw_update_sta_info(br_data->rtwdev, si, true);
779 }
780 
rtw_ra_mask_info_update(struct rtw_dev * rtwdev,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)781 static void rtw_ra_mask_info_update(struct rtw_dev *rtwdev,
782 				    struct ieee80211_vif *vif,
783 				    const struct cfg80211_bitrate_mask *mask)
784 {
785 	struct rtw_iter_bitrate_mask_data br_data;
786 
787 	br_data.rtwdev = rtwdev;
788 	br_data.vif = vif;
789 	br_data.mask = mask;
790 	rtw_iterate_stas(rtwdev, rtw_ra_mask_info_update_iter, &br_data);
791 }
792 
rtw_ops_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)793 static int rtw_ops_set_bitrate_mask(struct ieee80211_hw *hw,
794 				    struct ieee80211_vif *vif,
795 				    const struct cfg80211_bitrate_mask *mask)
796 {
797 	struct rtw_dev *rtwdev = hw->priv;
798 
799 	mutex_lock(&rtwdev->mutex);
800 	rtw_ra_mask_info_update(rtwdev, vif, mask);
801 	mutex_unlock(&rtwdev->mutex);
802 
803 	return 0;
804 }
805 
rtw_ops_set_antenna(struct ieee80211_hw * hw,int radio_idx,u32 tx_antenna,u32 rx_antenna)806 static int rtw_ops_set_antenna(struct ieee80211_hw *hw,
807 			       int radio_idx,
808 			       u32 tx_antenna,
809 			       u32 rx_antenna)
810 {
811 	struct rtw_dev *rtwdev = hw->priv;
812 	const struct rtw_chip_info *chip = rtwdev->chip;
813 	int ret;
814 
815 	if (!chip->ops->set_antenna)
816 		return -EOPNOTSUPP;
817 
818 	mutex_lock(&rtwdev->mutex);
819 	ret = chip->ops->set_antenna(rtwdev, -1, tx_antenna, rx_antenna);
820 	mutex_unlock(&rtwdev->mutex);
821 
822 	return ret;
823 }
824 
rtw_ops_get_antenna(struct ieee80211_hw * hw,int radio_idx,u32 * tx_antenna,u32 * rx_antenna)825 static int rtw_ops_get_antenna(struct ieee80211_hw *hw,
826 			       int radio_idx,
827 			       u32 *tx_antenna,
828 			       u32 *rx_antenna)
829 {
830 	struct rtw_dev *rtwdev = hw->priv;
831 	struct rtw_hal *hal = &rtwdev->hal;
832 
833 	*tx_antenna = hal->antenna_tx;
834 	*rx_antenna = hal->antenna_rx;
835 
836 	return 0;
837 }
838 
839 #ifdef CONFIG_PM
rtw_ops_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)840 static int rtw_ops_suspend(struct ieee80211_hw *hw,
841 			   struct cfg80211_wowlan *wowlan)
842 {
843 	struct rtw_dev *rtwdev = hw->priv;
844 	int ret;
845 
846 	mutex_lock(&rtwdev->mutex);
847 	ret = rtw_wow_suspend(rtwdev, wowlan);
848 	if (ret)
849 		rtw_err(rtwdev, "failed to suspend for wow %d\n", ret);
850 	mutex_unlock(&rtwdev->mutex);
851 
852 	return ret ? 1 : 0;
853 }
854 
rtw_ops_resume(struct ieee80211_hw * hw)855 static int rtw_ops_resume(struct ieee80211_hw *hw)
856 {
857 	struct rtw_dev *rtwdev = hw->priv;
858 	int ret;
859 
860 	mutex_lock(&rtwdev->mutex);
861 	ret = rtw_wow_resume(rtwdev);
862 	if (ret)
863 		rtw_err(rtwdev, "failed to resume for wow %d\n", ret);
864 	mutex_unlock(&rtwdev->mutex);
865 
866 	return ret ? 1 : 0;
867 }
868 
rtw_ops_set_wakeup(struct ieee80211_hw * hw,bool enabled)869 static void rtw_ops_set_wakeup(struct ieee80211_hw *hw, bool enabled)
870 {
871 	struct rtw_dev *rtwdev = hw->priv;
872 
873 	device_set_wakeup_enable(rtwdev->dev, enabled);
874 }
875 #endif
876 
rtw_reconfig_complete(struct ieee80211_hw * hw,enum ieee80211_reconfig_type reconfig_type)877 static void rtw_reconfig_complete(struct ieee80211_hw *hw,
878 				  enum ieee80211_reconfig_type reconfig_type)
879 {
880 	struct rtw_dev *rtwdev = hw->priv;
881 
882 	mutex_lock(&rtwdev->mutex);
883 	if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART)
884 		clear_bit(RTW_FLAG_RESTARTING, rtwdev->flags);
885 	mutex_unlock(&rtwdev->mutex);
886 }
887 
rtw_ops_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * req)888 static int rtw_ops_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
889 			   struct ieee80211_scan_request *req)
890 {
891 	struct rtw_dev *rtwdev = hw->priv;
892 	int ret;
893 
894 	if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_SCAN_OFFLOAD))
895 		return 1;
896 
897 	if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
898 		return -EBUSY;
899 
900 	mutex_lock(&rtwdev->mutex);
901 	rtw_hw_scan_start(rtwdev, vif, req);
902 	ret = rtw_hw_scan_offload(rtwdev, vif, true);
903 	if (ret) {
904 		rtw_hw_scan_abort(rtwdev);
905 		rtw_err(rtwdev, "HW scan failed with status: %d\n", ret);
906 	}
907 	mutex_unlock(&rtwdev->mutex);
908 
909 	return ret;
910 }
911 
rtw_ops_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)912 static void rtw_ops_cancel_hw_scan(struct ieee80211_hw *hw,
913 				   struct ieee80211_vif *vif)
914 {
915 	struct rtw_dev *rtwdev = hw->priv;
916 
917 	if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_SCAN_OFFLOAD))
918 		return;
919 
920 	if (!test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
921 		return;
922 
923 	mutex_lock(&rtwdev->mutex);
924 	rtw_hw_scan_abort(rtwdev);
925 	mutex_unlock(&rtwdev->mutex);
926 }
927 
rtw_ops_set_sar_specs(struct ieee80211_hw * hw,const struct cfg80211_sar_specs * sar)928 static int rtw_ops_set_sar_specs(struct ieee80211_hw *hw,
929 				 const struct cfg80211_sar_specs *sar)
930 {
931 	struct rtw_dev *rtwdev = hw->priv;
932 
933 	mutex_lock(&rtwdev->mutex);
934 	rtw_set_sar_specs(rtwdev, sar);
935 	mutex_unlock(&rtwdev->mutex);
936 
937 	return 0;
938 }
939 
rtw_ops_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta,u32 changed)940 static void rtw_ops_sta_rc_update(struct ieee80211_hw *hw,
941 				  struct ieee80211_vif *vif,
942 				  struct ieee80211_link_sta *link_sta,
943 				  u32 changed)
944 {
945 	struct ieee80211_sta *sta = link_sta->sta;
946 	struct rtw_dev *rtwdev = hw->priv;
947 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
948 
949 	if (changed & IEEE80211_RC_BW_CHANGED)
950 		ieee80211_queue_work(rtwdev->hw, &si->rc_work);
951 }
952 
953 const struct ieee80211_ops rtw_ops = {
954 	.add_chanctx = ieee80211_emulate_add_chanctx,
955 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
956 	.change_chanctx = ieee80211_emulate_change_chanctx,
957 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
958 	.tx			= rtw_ops_tx,
959 	.wake_tx_queue		= rtw_ops_wake_tx_queue,
960 	.start			= rtw_ops_start,
961 	.stop			= rtw_ops_stop,
962 	.config			= rtw_ops_config,
963 	.add_interface		= rtw_ops_add_interface,
964 	.remove_interface	= rtw_ops_remove_interface,
965 	.change_interface	= rtw_ops_change_interface,
966 	.configure_filter	= rtw_ops_configure_filter,
967 	.bss_info_changed	= rtw_ops_bss_info_changed,
968 	.start_ap		= rtw_ops_start_ap,
969 	.stop_ap		= rtw_ops_stop_ap,
970 	.conf_tx		= rtw_ops_conf_tx,
971 	.sta_add		= rtw_ops_sta_add,
972 	.sta_remove		= rtw_ops_sta_remove,
973 	.set_tim		= rtw_ops_set_tim,
974 	.set_key		= rtw_ops_set_key,
975 	.ampdu_action		= rtw_ops_ampdu_action,
976 	.can_aggregate_in_amsdu	= rtw_ops_can_aggregate_in_amsdu,
977 	.sw_scan_start		= rtw_ops_sw_scan_start,
978 	.sw_scan_complete	= rtw_ops_sw_scan_complete,
979 	.mgd_prepare_tx		= rtw_ops_mgd_prepare_tx,
980 	.set_rts_threshold	= rtw_ops_set_rts_threshold,
981 	.sta_statistics		= rtw_ops_sta_statistics,
982 	.flush			= rtw_ops_flush,
983 	.set_bitrate_mask	= rtw_ops_set_bitrate_mask,
984 	.set_antenna		= rtw_ops_set_antenna,
985 	.get_antenna		= rtw_ops_get_antenna,
986 	.reconfig_complete	= rtw_reconfig_complete,
987 	.hw_scan		= rtw_ops_hw_scan,
988 	.cancel_hw_scan		= rtw_ops_cancel_hw_scan,
989 	.link_sta_rc_update	= rtw_ops_sta_rc_update,
990 	.set_sar_specs          = rtw_ops_set_sar_specs,
991 #ifdef CONFIG_PM
992 	.suspend		= rtw_ops_suspend,
993 	.resume			= rtw_ops_resume,
994 	.set_wakeup		= rtw_ops_set_wakeup,
995 #endif
996 };
997 EXPORT_SYMBOL(rtw_ops);
998