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