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