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