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