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