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