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