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