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