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