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