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 "regd.h"
7 #include "fw.h"
8 #include "ps.h"
9 #include "sec.h"
10 #include "mac.h"
11 #include "coex.h"
12 #include "phy.h"
13 #include "reg.h"
14 #include "efuse.h"
15 #include "tx.h"
16 #include "debug.h"
17 #include "bf.h"
18 
19 unsigned int rtw_fw_lps_deep_mode;
20 EXPORT_SYMBOL(rtw_fw_lps_deep_mode);
21 bool rtw_bf_support = true;
22 unsigned int rtw_debug_mask;
23 EXPORT_SYMBOL(rtw_debug_mask);
24 
25 module_param_named(lps_deep_mode, rtw_fw_lps_deep_mode, uint, 0644);
26 module_param_named(support_bf, rtw_bf_support, bool, 0644);
27 module_param_named(debug_mask, rtw_debug_mask, uint, 0644);
28 
29 MODULE_PARM_DESC(lps_deep_mode, "Deeper PS mode. If 0, deep PS is disabled");
30 MODULE_PARM_DESC(support_bf, "Set Y to enable beamformee support");
31 MODULE_PARM_DESC(debug_mask, "Debugging mask");
32 
33 static struct ieee80211_channel rtw_channeltable_2g[] = {
34 	{.center_freq = 2412, .hw_value = 1,},
35 	{.center_freq = 2417, .hw_value = 2,},
36 	{.center_freq = 2422, .hw_value = 3,},
37 	{.center_freq = 2427, .hw_value = 4,},
38 	{.center_freq = 2432, .hw_value = 5,},
39 	{.center_freq = 2437, .hw_value = 6,},
40 	{.center_freq = 2442, .hw_value = 7,},
41 	{.center_freq = 2447, .hw_value = 8,},
42 	{.center_freq = 2452, .hw_value = 9,},
43 	{.center_freq = 2457, .hw_value = 10,},
44 	{.center_freq = 2462, .hw_value = 11,},
45 	{.center_freq = 2467, .hw_value = 12,},
46 	{.center_freq = 2472, .hw_value = 13,},
47 	{.center_freq = 2484, .hw_value = 14,},
48 };
49 
50 static struct ieee80211_channel rtw_channeltable_5g[] = {
51 	{.center_freq = 5180, .hw_value = 36,},
52 	{.center_freq = 5200, .hw_value = 40,},
53 	{.center_freq = 5220, .hw_value = 44,},
54 	{.center_freq = 5240, .hw_value = 48,},
55 	{.center_freq = 5260, .hw_value = 52,},
56 	{.center_freq = 5280, .hw_value = 56,},
57 	{.center_freq = 5300, .hw_value = 60,},
58 	{.center_freq = 5320, .hw_value = 64,},
59 	{.center_freq = 5500, .hw_value = 100,},
60 	{.center_freq = 5520, .hw_value = 104,},
61 	{.center_freq = 5540, .hw_value = 108,},
62 	{.center_freq = 5560, .hw_value = 112,},
63 	{.center_freq = 5580, .hw_value = 116,},
64 	{.center_freq = 5600, .hw_value = 120,},
65 	{.center_freq = 5620, .hw_value = 124,},
66 	{.center_freq = 5640, .hw_value = 128,},
67 	{.center_freq = 5660, .hw_value = 132,},
68 	{.center_freq = 5680, .hw_value = 136,},
69 	{.center_freq = 5700, .hw_value = 140,},
70 	{.center_freq = 5745, .hw_value = 149,},
71 	{.center_freq = 5765, .hw_value = 153,},
72 	{.center_freq = 5785, .hw_value = 157,},
73 	{.center_freq = 5805, .hw_value = 161,},
74 	{.center_freq = 5825, .hw_value = 165,
75 	 .flags = IEEE80211_CHAN_NO_HT40MINUS},
76 };
77 
78 static struct ieee80211_rate rtw_ratetable[] = {
79 	{.bitrate = 10, .hw_value = 0x00,},
80 	{.bitrate = 20, .hw_value = 0x01,},
81 	{.bitrate = 55, .hw_value = 0x02,},
82 	{.bitrate = 110, .hw_value = 0x03,},
83 	{.bitrate = 60, .hw_value = 0x04,},
84 	{.bitrate = 90, .hw_value = 0x05,},
85 	{.bitrate = 120, .hw_value = 0x06,},
86 	{.bitrate = 180, .hw_value = 0x07,},
87 	{.bitrate = 240, .hw_value = 0x08,},
88 	{.bitrate = 360, .hw_value = 0x09,},
89 	{.bitrate = 480, .hw_value = 0x0a,},
90 	{.bitrate = 540, .hw_value = 0x0b,},
91 };
92 
93 u16 rtw_desc_to_bitrate(u8 desc_rate)
94 {
95 	struct ieee80211_rate rate;
96 
97 	if (WARN(desc_rate >= ARRAY_SIZE(rtw_ratetable), "invalid desc rate\n"))
98 		return 0;
99 
100 	rate = rtw_ratetable[desc_rate];
101 
102 	return rate.bitrate;
103 }
104 
105 static struct ieee80211_supported_band rtw_band_2ghz = {
106 	.band = NL80211_BAND_2GHZ,
107 
108 	.channels = rtw_channeltable_2g,
109 	.n_channels = ARRAY_SIZE(rtw_channeltable_2g),
110 
111 	.bitrates = rtw_ratetable,
112 	.n_bitrates = ARRAY_SIZE(rtw_ratetable),
113 
114 	.ht_cap = {0},
115 	.vht_cap = {0},
116 };
117 
118 static struct ieee80211_supported_band rtw_band_5ghz = {
119 	.band = NL80211_BAND_5GHZ,
120 
121 	.channels = rtw_channeltable_5g,
122 	.n_channels = ARRAY_SIZE(rtw_channeltable_5g),
123 
124 	/* 5G has no CCK rates */
125 	.bitrates = rtw_ratetable + 4,
126 	.n_bitrates = ARRAY_SIZE(rtw_ratetable) - 4,
127 
128 	.ht_cap = {0},
129 	.vht_cap = {0},
130 };
131 
132 struct rtw_watch_dog_iter_data {
133 	struct rtw_dev *rtwdev;
134 	struct rtw_vif *rtwvif;
135 };
136 
137 static void rtw_dynamic_csi_rate(struct rtw_dev *rtwdev, struct rtw_vif *rtwvif)
138 {
139 	struct rtw_bf_info *bf_info = &rtwdev->bf_info;
140 	u8 fix_rate_enable = 0;
141 	u8 new_csi_rate_idx;
142 
143 	if (rtwvif->bfee.role != RTW_BFEE_SU &&
144 	    rtwvif->bfee.role != RTW_BFEE_MU)
145 		return;
146 
147 	rtw_chip_cfg_csi_rate(rtwdev, rtwdev->dm_info.min_rssi,
148 			      bf_info->cur_csi_rpt_rate,
149 			      fix_rate_enable, &new_csi_rate_idx);
150 
151 	if (new_csi_rate_idx != bf_info->cur_csi_rpt_rate)
152 		bf_info->cur_csi_rpt_rate = new_csi_rate_idx;
153 }
154 
155 static void rtw_vif_watch_dog_iter(void *data, u8 *mac,
156 				   struct ieee80211_vif *vif)
157 {
158 	struct rtw_watch_dog_iter_data *iter_data = data;
159 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
160 
161 	if (vif->type == NL80211_IFTYPE_STATION)
162 		if (vif->bss_conf.assoc)
163 			iter_data->rtwvif = rtwvif;
164 
165 	rtw_dynamic_csi_rate(iter_data->rtwdev, rtwvif);
166 
167 	rtwvif->stats.tx_unicast = 0;
168 	rtwvif->stats.rx_unicast = 0;
169 	rtwvif->stats.tx_cnt = 0;
170 	rtwvif->stats.rx_cnt = 0;
171 }
172 
173 /* process TX/RX statistics periodically for hardware,
174  * the information helps hardware to enhance performance
175  */
176 static void rtw_watch_dog_work(struct work_struct *work)
177 {
178 	struct rtw_dev *rtwdev = container_of(work, struct rtw_dev,
179 					      watch_dog_work.work);
180 	struct rtw_traffic_stats *stats = &rtwdev->stats;
181 	struct rtw_watch_dog_iter_data data = {};
182 	bool busy_traffic = test_bit(RTW_FLAG_BUSY_TRAFFIC, rtwdev->flags);
183 	bool ps_active;
184 
185 	mutex_lock(&rtwdev->mutex);
186 
187 	if (!test_bit(RTW_FLAG_RUNNING, rtwdev->flags))
188 		goto unlock;
189 
190 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->watch_dog_work,
191 				     RTW_WATCH_DOG_DELAY_TIME);
192 
193 	if (rtwdev->stats.tx_cnt > 100 || rtwdev->stats.rx_cnt > 100)
194 		set_bit(RTW_FLAG_BUSY_TRAFFIC, rtwdev->flags);
195 	else
196 		clear_bit(RTW_FLAG_BUSY_TRAFFIC, rtwdev->flags);
197 
198 	if (busy_traffic != test_bit(RTW_FLAG_BUSY_TRAFFIC, rtwdev->flags))
199 		rtw_coex_wl_status_change_notify(rtwdev);
200 
201 	if (stats->tx_cnt > RTW_LPS_THRESHOLD ||
202 	    stats->rx_cnt > RTW_LPS_THRESHOLD)
203 		ps_active = true;
204 	else
205 		ps_active = false;
206 
207 	ewma_tp_add(&stats->tx_ewma_tp,
208 		    (u32)(stats->tx_unicast >> RTW_TP_SHIFT));
209 	ewma_tp_add(&stats->rx_ewma_tp,
210 		    (u32)(stats->rx_unicast >> RTW_TP_SHIFT));
211 	stats->tx_throughput = ewma_tp_read(&stats->tx_ewma_tp);
212 	stats->rx_throughput = ewma_tp_read(&stats->rx_ewma_tp);
213 
214 	/* reset tx/rx statictics */
215 	stats->tx_unicast = 0;
216 	stats->rx_unicast = 0;
217 	stats->tx_cnt = 0;
218 	stats->rx_cnt = 0;
219 
220 	if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
221 		goto unlock;
222 
223 	/* make sure BB/RF is working for dynamic mech */
224 	rtw_leave_lps(rtwdev);
225 
226 	rtw_phy_dynamic_mechanism(rtwdev);
227 
228 	data.rtwdev = rtwdev;
229 	/* use atomic version to avoid taking local->iflist_mtx mutex */
230 	rtw_iterate_vifs_atomic(rtwdev, rtw_vif_watch_dog_iter, &data);
231 
232 	/* fw supports only one station associated to enter lps, if there are
233 	 * more than two stations associated to the AP, then we can not enter
234 	 * lps, because fw does not handle the overlapped beacon interval
235 	 *
236 	 * mac80211 should iterate vifs and determine if driver can enter
237 	 * ps by passing IEEE80211_CONF_PS to us, all we need to do is to
238 	 * get that vif and check if device is having traffic more than the
239 	 * threshold.
240 	 */
241 	if (rtwdev->ps_enabled && data.rtwvif && !ps_active)
242 		rtw_enter_lps(rtwdev, data.rtwvif->port);
243 
244 	rtwdev->watch_dog_cnt++;
245 
246 unlock:
247 	mutex_unlock(&rtwdev->mutex);
248 }
249 
250 static void rtw_c2h_work(struct work_struct *work)
251 {
252 	struct rtw_dev *rtwdev = container_of(work, struct rtw_dev, c2h_work);
253 	struct sk_buff *skb, *tmp;
254 
255 	skb_queue_walk_safe(&rtwdev->c2h_queue, skb, tmp) {
256 		skb_unlink(skb, &rtwdev->c2h_queue);
257 		rtw_fw_c2h_cmd_handle(rtwdev, skb);
258 		dev_kfree_skb_any(skb);
259 	}
260 }
261 
262 struct rtw_txq_ba_iter_data {
263 };
264 
265 static void rtw_txq_ba_iter(void *data, struct ieee80211_sta *sta)
266 {
267 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
268 	int ret;
269 	u8 tid;
270 
271 	tid = find_first_bit(si->tid_ba, IEEE80211_NUM_TIDS);
272 	while (tid != IEEE80211_NUM_TIDS) {
273 		clear_bit(tid, si->tid_ba);
274 		ret = ieee80211_start_tx_ba_session(sta, tid, 0);
275 		if (ret == -EINVAL) {
276 			struct ieee80211_txq *txq;
277 			struct rtw_txq *rtwtxq;
278 
279 			txq = sta->txq[tid];
280 			rtwtxq = (struct rtw_txq *)txq->drv_priv;
281 			set_bit(RTW_TXQ_BLOCK_BA, &rtwtxq->flags);
282 		}
283 
284 		tid = find_first_bit(si->tid_ba, IEEE80211_NUM_TIDS);
285 	}
286 }
287 
288 static void rtw_txq_ba_work(struct work_struct *work)
289 {
290 	struct rtw_dev *rtwdev = container_of(work, struct rtw_dev, ba_work);
291 	struct rtw_txq_ba_iter_data data;
292 
293 	rtw_iterate_stas_atomic(rtwdev, rtw_txq_ba_iter, &data);
294 }
295 
296 void rtw_get_channel_params(struct cfg80211_chan_def *chandef,
297 			    struct rtw_channel_params *chan_params)
298 {
299 	struct ieee80211_channel *channel = chandef->chan;
300 	enum nl80211_chan_width width = chandef->width;
301 	u8 *cch_by_bw = chan_params->cch_by_bw;
302 	u32 primary_freq, center_freq;
303 	u8 center_chan;
304 	u8 bandwidth = RTW_CHANNEL_WIDTH_20;
305 	u8 primary_chan_idx = 0;
306 	u8 i;
307 
308 	center_chan = channel->hw_value;
309 	primary_freq = channel->center_freq;
310 	center_freq = chandef->center_freq1;
311 
312 	/* assign the center channel used while 20M bw is selected */
313 	cch_by_bw[RTW_CHANNEL_WIDTH_20] = channel->hw_value;
314 
315 	switch (width) {
316 	case NL80211_CHAN_WIDTH_20_NOHT:
317 	case NL80211_CHAN_WIDTH_20:
318 		bandwidth = RTW_CHANNEL_WIDTH_20;
319 		primary_chan_idx = RTW_SC_DONT_CARE;
320 		break;
321 	case NL80211_CHAN_WIDTH_40:
322 		bandwidth = RTW_CHANNEL_WIDTH_40;
323 		if (primary_freq > center_freq) {
324 			primary_chan_idx = RTW_SC_20_UPPER;
325 			center_chan -= 2;
326 		} else {
327 			primary_chan_idx = RTW_SC_20_LOWER;
328 			center_chan += 2;
329 		}
330 		break;
331 	case NL80211_CHAN_WIDTH_80:
332 		bandwidth = RTW_CHANNEL_WIDTH_80;
333 		if (primary_freq > center_freq) {
334 			if (primary_freq - center_freq == 10) {
335 				primary_chan_idx = RTW_SC_20_UPPER;
336 				center_chan -= 2;
337 			} else {
338 				primary_chan_idx = RTW_SC_20_UPMOST;
339 				center_chan -= 6;
340 			}
341 			/* assign the center channel used
342 			 * while 40M bw is selected
343 			 */
344 			cch_by_bw[RTW_CHANNEL_WIDTH_40] = center_chan + 4;
345 		} else {
346 			if (center_freq - primary_freq == 10) {
347 				primary_chan_idx = RTW_SC_20_LOWER;
348 				center_chan += 2;
349 			} else {
350 				primary_chan_idx = RTW_SC_20_LOWEST;
351 				center_chan += 6;
352 			}
353 			/* assign the center channel used
354 			 * while 40M bw is selected
355 			 */
356 			cch_by_bw[RTW_CHANNEL_WIDTH_40] = center_chan - 4;
357 		}
358 		break;
359 	default:
360 		center_chan = 0;
361 		break;
362 	}
363 
364 	chan_params->center_chan = center_chan;
365 	chan_params->bandwidth = bandwidth;
366 	chan_params->primary_chan_idx = primary_chan_idx;
367 
368 	/* assign the center channel used while current bw is selected */
369 	cch_by_bw[bandwidth] = center_chan;
370 
371 	for (i = bandwidth + 1; i <= RTW_MAX_CHANNEL_WIDTH; i++)
372 		cch_by_bw[i] = 0;
373 }
374 
375 void rtw_set_channel(struct rtw_dev *rtwdev)
376 {
377 	struct ieee80211_hw *hw = rtwdev->hw;
378 	struct rtw_hal *hal = &rtwdev->hal;
379 	struct rtw_chip_info *chip = rtwdev->chip;
380 	struct rtw_channel_params ch_param;
381 	u8 center_chan, bandwidth, primary_chan_idx;
382 	u8 i;
383 
384 	rtw_get_channel_params(&hw->conf.chandef, &ch_param);
385 	if (WARN(ch_param.center_chan == 0, "Invalid channel\n"))
386 		return;
387 
388 	center_chan = ch_param.center_chan;
389 	bandwidth = ch_param.bandwidth;
390 	primary_chan_idx = ch_param.primary_chan_idx;
391 
392 	hal->current_band_width = bandwidth;
393 	hal->current_channel = center_chan;
394 	hal->current_band_type = center_chan > 14 ? RTW_BAND_5G : RTW_BAND_2G;
395 
396 	for (i = RTW_CHANNEL_WIDTH_20; i <= RTW_MAX_CHANNEL_WIDTH; i++)
397 		hal->cch_by_bw[i] = ch_param.cch_by_bw[i];
398 
399 	chip->ops->set_channel(rtwdev, center_chan, bandwidth, primary_chan_idx);
400 
401 	if (hal->current_band_type == RTW_BAND_5G) {
402 		rtw_coex_switchband_notify(rtwdev, COEX_SWITCH_TO_5G);
403 	} else {
404 		if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
405 			rtw_coex_switchband_notify(rtwdev, COEX_SWITCH_TO_24G);
406 		else
407 			rtw_coex_switchband_notify(rtwdev, COEX_SWITCH_TO_24G_NOFORSCAN);
408 	}
409 
410 	rtw_phy_set_tx_power_level(rtwdev, center_chan);
411 
412 	/* if the channel isn't set for scanning, we will do RF calibration
413 	 * in ieee80211_ops::mgd_prepare_tx(). Performing the calibration
414 	 * during scanning on each channel takes too long.
415 	 */
416 	if (!test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
417 		rtwdev->need_rfk = true;
418 }
419 
420 void rtw_chip_prepare_tx(struct rtw_dev *rtwdev)
421 {
422 	struct rtw_chip_info *chip = rtwdev->chip;
423 
424 	if (rtwdev->need_rfk) {
425 		rtwdev->need_rfk = false;
426 		chip->ops->phy_calibration(rtwdev);
427 	}
428 }
429 
430 static void rtw_vif_write_addr(struct rtw_dev *rtwdev, u32 start, u8 *addr)
431 {
432 	int i;
433 
434 	for (i = 0; i < ETH_ALEN; i++)
435 		rtw_write8(rtwdev, start + i, addr[i]);
436 }
437 
438 void rtw_vif_port_config(struct rtw_dev *rtwdev,
439 			 struct rtw_vif *rtwvif,
440 			 u32 config)
441 {
442 	u32 addr, mask;
443 
444 	if (config & PORT_SET_MAC_ADDR) {
445 		addr = rtwvif->conf->mac_addr.addr;
446 		rtw_vif_write_addr(rtwdev, addr, rtwvif->mac_addr);
447 	}
448 	if (config & PORT_SET_BSSID) {
449 		addr = rtwvif->conf->bssid.addr;
450 		rtw_vif_write_addr(rtwdev, addr, rtwvif->bssid);
451 	}
452 	if (config & PORT_SET_NET_TYPE) {
453 		addr = rtwvif->conf->net_type.addr;
454 		mask = rtwvif->conf->net_type.mask;
455 		rtw_write32_mask(rtwdev, addr, mask, rtwvif->net_type);
456 	}
457 	if (config & PORT_SET_AID) {
458 		addr = rtwvif->conf->aid.addr;
459 		mask = rtwvif->conf->aid.mask;
460 		rtw_write32_mask(rtwdev, addr, mask, rtwvif->aid);
461 	}
462 	if (config & PORT_SET_BCN_CTRL) {
463 		addr = rtwvif->conf->bcn_ctrl.addr;
464 		mask = rtwvif->conf->bcn_ctrl.mask;
465 		rtw_write8_mask(rtwdev, addr, mask, rtwvif->bcn_ctrl);
466 	}
467 }
468 
469 static u8 hw_bw_cap_to_bitamp(u8 bw_cap)
470 {
471 	u8 bw = 0;
472 
473 	switch (bw_cap) {
474 	case EFUSE_HW_CAP_IGNORE:
475 	case EFUSE_HW_CAP_SUPP_BW80:
476 		bw |= BIT(RTW_CHANNEL_WIDTH_80);
477 		/* fall through */
478 	case EFUSE_HW_CAP_SUPP_BW40:
479 		bw |= BIT(RTW_CHANNEL_WIDTH_40);
480 		/* fall through */
481 	default:
482 		bw |= BIT(RTW_CHANNEL_WIDTH_20);
483 		break;
484 	}
485 
486 	return bw;
487 }
488 
489 static void rtw_hw_config_rf_ant_num(struct rtw_dev *rtwdev, u8 hw_ant_num)
490 {
491 	struct rtw_hal *hal = &rtwdev->hal;
492 	struct rtw_chip_info *chip = rtwdev->chip;
493 
494 	if (hw_ant_num == EFUSE_HW_CAP_IGNORE ||
495 	    hw_ant_num >= hal->rf_path_num)
496 		return;
497 
498 	switch (hw_ant_num) {
499 	case 1:
500 		hal->rf_type = RF_1T1R;
501 		hal->rf_path_num = 1;
502 		if (!chip->fix_rf_phy_num)
503 			hal->rf_phy_num = hal->rf_path_num;
504 		hal->antenna_tx = BB_PATH_A;
505 		hal->antenna_rx = BB_PATH_A;
506 		break;
507 	default:
508 		WARN(1, "invalid hw configuration from efuse\n");
509 		break;
510 	}
511 }
512 
513 static u64 get_vht_ra_mask(struct ieee80211_sta *sta)
514 {
515 	u64 ra_mask = 0;
516 	u16 mcs_map = le16_to_cpu(sta->vht_cap.vht_mcs.rx_mcs_map);
517 	u8 vht_mcs_cap;
518 	int i, nss;
519 
520 	/* 4SS, every two bits for MCS7/8/9 */
521 	for (i = 0, nss = 12; i < 4; i++, mcs_map >>= 2, nss += 10) {
522 		vht_mcs_cap = mcs_map & 0x3;
523 		switch (vht_mcs_cap) {
524 		case 2: /* MCS9 */
525 			ra_mask |= 0x3ffULL << nss;
526 			break;
527 		case 1: /* MCS8 */
528 			ra_mask |= 0x1ffULL << nss;
529 			break;
530 		case 0: /* MCS7 */
531 			ra_mask |= 0x0ffULL << nss;
532 			break;
533 		default:
534 			break;
535 		}
536 	}
537 
538 	return ra_mask;
539 }
540 
541 static u8 get_rate_id(u8 wireless_set, enum rtw_bandwidth bw_mode, u8 tx_num)
542 {
543 	u8 rate_id = 0;
544 
545 	switch (wireless_set) {
546 	case WIRELESS_CCK:
547 		rate_id = RTW_RATEID_B_20M;
548 		break;
549 	case WIRELESS_OFDM:
550 		rate_id = RTW_RATEID_G;
551 		break;
552 	case WIRELESS_CCK | WIRELESS_OFDM:
553 		rate_id = RTW_RATEID_BG;
554 		break;
555 	case WIRELESS_OFDM | WIRELESS_HT:
556 		if (tx_num == 1)
557 			rate_id = RTW_RATEID_GN_N1SS;
558 		else if (tx_num == 2)
559 			rate_id = RTW_RATEID_GN_N2SS;
560 		else if (tx_num == 3)
561 			rate_id = RTW_RATEID_ARFR5_N_3SS;
562 		break;
563 	case WIRELESS_CCK | WIRELESS_OFDM | WIRELESS_HT:
564 		if (bw_mode == RTW_CHANNEL_WIDTH_40) {
565 			if (tx_num == 1)
566 				rate_id = RTW_RATEID_BGN_40M_1SS;
567 			else if (tx_num == 2)
568 				rate_id = RTW_RATEID_BGN_40M_2SS;
569 			else if (tx_num == 3)
570 				rate_id = RTW_RATEID_ARFR5_N_3SS;
571 			else if (tx_num == 4)
572 				rate_id = RTW_RATEID_ARFR7_N_4SS;
573 		} else {
574 			if (tx_num == 1)
575 				rate_id = RTW_RATEID_BGN_20M_1SS;
576 			else if (tx_num == 2)
577 				rate_id = RTW_RATEID_BGN_20M_2SS;
578 			else if (tx_num == 3)
579 				rate_id = RTW_RATEID_ARFR5_N_3SS;
580 			else if (tx_num == 4)
581 				rate_id = RTW_RATEID_ARFR7_N_4SS;
582 		}
583 		break;
584 	case WIRELESS_OFDM | WIRELESS_VHT:
585 		if (tx_num == 1)
586 			rate_id = RTW_RATEID_ARFR1_AC_1SS;
587 		else if (tx_num == 2)
588 			rate_id = RTW_RATEID_ARFR0_AC_2SS;
589 		else if (tx_num == 3)
590 			rate_id = RTW_RATEID_ARFR4_AC_3SS;
591 		else if (tx_num == 4)
592 			rate_id = RTW_RATEID_ARFR6_AC_4SS;
593 		break;
594 	case WIRELESS_CCK | WIRELESS_OFDM | WIRELESS_VHT:
595 		if (bw_mode >= RTW_CHANNEL_WIDTH_80) {
596 			if (tx_num == 1)
597 				rate_id = RTW_RATEID_ARFR1_AC_1SS;
598 			else if (tx_num == 2)
599 				rate_id = RTW_RATEID_ARFR0_AC_2SS;
600 			else if (tx_num == 3)
601 				rate_id = RTW_RATEID_ARFR4_AC_3SS;
602 			else if (tx_num == 4)
603 				rate_id = RTW_RATEID_ARFR6_AC_4SS;
604 		} else {
605 			if (tx_num == 1)
606 				rate_id = RTW_RATEID_ARFR2_AC_2G_1SS;
607 			else if (tx_num == 2)
608 				rate_id = RTW_RATEID_ARFR3_AC_2G_2SS;
609 			else if (tx_num == 3)
610 				rate_id = RTW_RATEID_ARFR4_AC_3SS;
611 			else if (tx_num == 4)
612 				rate_id = RTW_RATEID_ARFR6_AC_4SS;
613 		}
614 		break;
615 	default:
616 		break;
617 	}
618 
619 	return rate_id;
620 }
621 
622 #define RA_MASK_CCK_RATES	0x0000f
623 #define RA_MASK_OFDM_RATES	0x00ff0
624 #define RA_MASK_HT_RATES_1SS	(0xff000ULL << 0)
625 #define RA_MASK_HT_RATES_2SS	(0xff000ULL << 8)
626 #define RA_MASK_HT_RATES_3SS	(0xff000ULL << 16)
627 #define RA_MASK_HT_RATES	(RA_MASK_HT_RATES_1SS | \
628 				 RA_MASK_HT_RATES_2SS | \
629 				 RA_MASK_HT_RATES_3SS)
630 #define RA_MASK_VHT_RATES_1SS	(0x3ff000ULL << 0)
631 #define RA_MASK_VHT_RATES_2SS	(0x3ff000ULL << 10)
632 #define RA_MASK_VHT_RATES_3SS	(0x3ff000ULL << 20)
633 #define RA_MASK_VHT_RATES	(RA_MASK_VHT_RATES_1SS | \
634 				 RA_MASK_VHT_RATES_2SS | \
635 				 RA_MASK_VHT_RATES_3SS)
636 #define RA_MASK_CCK_IN_HT	0x00005
637 #define RA_MASK_CCK_IN_VHT	0x00005
638 #define RA_MASK_OFDM_IN_VHT	0x00010
639 #define RA_MASK_OFDM_IN_HT_2G	0x00010
640 #define RA_MASK_OFDM_IN_HT_5G	0x00030
641 
642 static u64 rtw_update_rate_mask(struct rtw_dev *rtwdev,
643 				struct rtw_sta_info *si,
644 				u64 ra_mask, bool is_vht_enable,
645 				u8 wireless_set)
646 {
647 	struct rtw_hal *hal = &rtwdev->hal;
648 	const struct cfg80211_bitrate_mask *mask = si->mask;
649 	u64 cfg_mask = GENMASK_ULL(63, 0);
650 	u8 rssi_level, band;
651 
652 	if (wireless_set != WIRELESS_CCK) {
653 		rssi_level = si->rssi_level;
654 		if (rssi_level == 0)
655 			ra_mask &= 0xffffffffffffffffULL;
656 		else if (rssi_level == 1)
657 			ra_mask &= 0xfffffffffffffff0ULL;
658 		else if (rssi_level == 2)
659 			ra_mask &= 0xffffffffffffefe0ULL;
660 		else if (rssi_level == 3)
661 			ra_mask &= 0xffffffffffffcfc0ULL;
662 		else if (rssi_level == 4)
663 			ra_mask &= 0xffffffffffff8f80ULL;
664 		else if (rssi_level >= 5)
665 			ra_mask &= 0xffffffffffff0f00ULL;
666 	}
667 
668 	if (!si->use_cfg_mask)
669 		return ra_mask;
670 
671 	band = hal->current_band_type;
672 	if (band == RTW_BAND_2G) {
673 		band = NL80211_BAND_2GHZ;
674 		cfg_mask = mask->control[band].legacy;
675 	} else if (band == RTW_BAND_5G) {
676 		band = NL80211_BAND_5GHZ;
677 		cfg_mask = u64_encode_bits(mask->control[band].legacy,
678 					   RA_MASK_OFDM_RATES);
679 	}
680 
681 	if (!is_vht_enable) {
682 		if (ra_mask & RA_MASK_HT_RATES_1SS)
683 			cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[0],
684 						    RA_MASK_HT_RATES_1SS);
685 		if (ra_mask & RA_MASK_HT_RATES_2SS)
686 			cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[1],
687 						    RA_MASK_HT_RATES_2SS);
688 	} else {
689 		if (ra_mask & RA_MASK_VHT_RATES_1SS)
690 			cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[0],
691 						    RA_MASK_VHT_RATES_1SS);
692 		if (ra_mask & RA_MASK_VHT_RATES_2SS)
693 			cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[1],
694 						    RA_MASK_VHT_RATES_2SS);
695 	}
696 
697 	ra_mask &= cfg_mask;
698 
699 	return ra_mask;
700 }
701 
702 void rtw_update_sta_info(struct rtw_dev *rtwdev, struct rtw_sta_info *si)
703 {
704 	struct ieee80211_sta *sta = si->sta;
705 	struct rtw_efuse *efuse = &rtwdev->efuse;
706 	struct rtw_hal *hal = &rtwdev->hal;
707 	u8 wireless_set;
708 	u8 bw_mode;
709 	u8 rate_id;
710 	u8 rf_type = RF_1T1R;
711 	u8 stbc_en = 0;
712 	u8 ldpc_en = 0;
713 	u8 tx_num = 1;
714 	u64 ra_mask = 0;
715 	bool is_vht_enable = false;
716 	bool is_support_sgi = false;
717 
718 	if (sta->vht_cap.vht_supported) {
719 		is_vht_enable = true;
720 		ra_mask |= get_vht_ra_mask(sta);
721 		if (sta->vht_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK)
722 			stbc_en = VHT_STBC_EN;
723 		if (sta->vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC)
724 			ldpc_en = VHT_LDPC_EN;
725 	} else if (sta->ht_cap.ht_supported) {
726 		ra_mask |= (sta->ht_cap.mcs.rx_mask[1] << 20) |
727 			   (sta->ht_cap.mcs.rx_mask[0] << 12);
728 		if (sta->ht_cap.cap & IEEE80211_HT_CAP_RX_STBC)
729 			stbc_en = HT_STBC_EN;
730 		if (sta->ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING)
731 			ldpc_en = HT_LDPC_EN;
732 	}
733 
734 	if (efuse->hw_cap.nss == 1)
735 		ra_mask &= RA_MASK_VHT_RATES_1SS | RA_MASK_HT_RATES_1SS;
736 
737 	if (hal->current_band_type == RTW_BAND_5G) {
738 		ra_mask |= (u64)sta->supp_rates[NL80211_BAND_5GHZ] << 4;
739 		if (sta->vht_cap.vht_supported) {
740 			ra_mask &= RA_MASK_VHT_RATES | RA_MASK_OFDM_IN_VHT;
741 			wireless_set = WIRELESS_OFDM | WIRELESS_VHT;
742 		} else if (sta->ht_cap.ht_supported) {
743 			ra_mask &= RA_MASK_HT_RATES | RA_MASK_OFDM_IN_HT_5G;
744 			wireless_set = WIRELESS_OFDM | WIRELESS_HT;
745 		} else {
746 			wireless_set = WIRELESS_OFDM;
747 		}
748 	} else if (hal->current_band_type == RTW_BAND_2G) {
749 		ra_mask |= sta->supp_rates[NL80211_BAND_2GHZ];
750 		if (sta->vht_cap.vht_supported) {
751 			ra_mask &= RA_MASK_VHT_RATES | RA_MASK_CCK_IN_VHT |
752 				   RA_MASK_OFDM_IN_VHT;
753 			wireless_set = WIRELESS_CCK | WIRELESS_OFDM |
754 				       WIRELESS_HT | WIRELESS_VHT;
755 		} else if (sta->ht_cap.ht_supported) {
756 			ra_mask &= RA_MASK_HT_RATES | RA_MASK_CCK_IN_HT |
757 				   RA_MASK_OFDM_IN_HT_2G;
758 			wireless_set = WIRELESS_CCK | WIRELESS_OFDM |
759 				       WIRELESS_HT;
760 		} else if (sta->supp_rates[0] <= 0xf) {
761 			wireless_set = WIRELESS_CCK;
762 		} else {
763 			wireless_set = WIRELESS_CCK | WIRELESS_OFDM;
764 		}
765 	} else {
766 		rtw_err(rtwdev, "Unknown band type\n");
767 		wireless_set = 0;
768 	}
769 
770 	switch (sta->bandwidth) {
771 	case IEEE80211_STA_RX_BW_80:
772 		bw_mode = RTW_CHANNEL_WIDTH_80;
773 		is_support_sgi = sta->vht_cap.vht_supported &&
774 				 (sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80);
775 		break;
776 	case IEEE80211_STA_RX_BW_40:
777 		bw_mode = RTW_CHANNEL_WIDTH_40;
778 		is_support_sgi = sta->ht_cap.ht_supported &&
779 				 (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40);
780 		break;
781 	default:
782 		bw_mode = RTW_CHANNEL_WIDTH_20;
783 		is_support_sgi = sta->ht_cap.ht_supported &&
784 				 (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20);
785 		break;
786 	}
787 
788 	if (sta->vht_cap.vht_supported && ra_mask & 0xffc00000) {
789 		tx_num = 2;
790 		rf_type = RF_2T2R;
791 	} else if (sta->ht_cap.ht_supported && ra_mask & 0xfff00000) {
792 		tx_num = 2;
793 		rf_type = RF_2T2R;
794 	}
795 
796 	rate_id = get_rate_id(wireless_set, bw_mode, tx_num);
797 
798 	ra_mask = rtw_update_rate_mask(rtwdev, si, ra_mask, is_vht_enable,
799 				       wireless_set);
800 
801 	si->bw_mode = bw_mode;
802 	si->stbc_en = stbc_en;
803 	si->ldpc_en = ldpc_en;
804 	si->rf_type = rf_type;
805 	si->wireless_set = wireless_set;
806 	si->sgi_enable = is_support_sgi;
807 	si->vht_enable = is_vht_enable;
808 	si->ra_mask = ra_mask;
809 	si->rate_id = rate_id;
810 
811 	rtw_fw_send_ra_info(rtwdev, si);
812 }
813 
814 static int rtw_wait_firmware_completion(struct rtw_dev *rtwdev)
815 {
816 	struct rtw_chip_info *chip = rtwdev->chip;
817 	struct rtw_fw_state *fw;
818 
819 	fw = &rtwdev->fw;
820 	wait_for_completion(&fw->completion);
821 	if (!fw->firmware)
822 		return -EINVAL;
823 
824 	if (chip->wow_fw_name) {
825 		fw = &rtwdev->wow_fw;
826 		wait_for_completion(&fw->completion);
827 		if (!fw->firmware)
828 			return -EINVAL;
829 	}
830 
831 	return 0;
832 }
833 
834 static int rtw_power_on(struct rtw_dev *rtwdev)
835 {
836 	struct rtw_chip_info *chip = rtwdev->chip;
837 	struct rtw_fw_state *fw = &rtwdev->fw;
838 	bool wifi_only;
839 	int ret;
840 
841 	ret = rtw_hci_setup(rtwdev);
842 	if (ret) {
843 		rtw_err(rtwdev, "failed to setup hci\n");
844 		goto err;
845 	}
846 
847 	/* power on MAC before firmware downloaded */
848 	ret = rtw_mac_power_on(rtwdev);
849 	if (ret) {
850 		rtw_err(rtwdev, "failed to power on mac\n");
851 		goto err;
852 	}
853 
854 	ret = rtw_wait_firmware_completion(rtwdev);
855 	if (ret) {
856 		rtw_err(rtwdev, "failed to wait firmware completion\n");
857 		goto err_off;
858 	}
859 
860 	ret = rtw_download_firmware(rtwdev, fw);
861 	if (ret) {
862 		rtw_err(rtwdev, "failed to download firmware\n");
863 		goto err_off;
864 	}
865 
866 	/* config mac after firmware downloaded */
867 	ret = rtw_mac_init(rtwdev);
868 	if (ret) {
869 		rtw_err(rtwdev, "failed to configure mac\n");
870 		goto err_off;
871 	}
872 
873 	chip->ops->phy_set_param(rtwdev);
874 
875 	ret = rtw_hci_start(rtwdev);
876 	if (ret) {
877 		rtw_err(rtwdev, "failed to start hci\n");
878 		goto err_off;
879 	}
880 
881 	/* send H2C after HCI has started */
882 	rtw_fw_send_general_info(rtwdev);
883 	rtw_fw_send_phydm_info(rtwdev);
884 
885 	wifi_only = !rtwdev->efuse.btcoex;
886 	rtw_coex_power_on_setting(rtwdev);
887 	rtw_coex_init_hw_config(rtwdev, wifi_only);
888 
889 	return 0;
890 
891 err_off:
892 	rtw_mac_power_off(rtwdev);
893 
894 err:
895 	return ret;
896 }
897 
898 int rtw_core_start(struct rtw_dev *rtwdev)
899 {
900 	int ret;
901 
902 	ret = rtw_power_on(rtwdev);
903 	if (ret)
904 		return ret;
905 
906 	rtw_sec_enable_sec_engine(rtwdev);
907 
908 	/* rcr reset after powered on */
909 	rtw_write32(rtwdev, REG_RCR, rtwdev->hal.rcr);
910 
911 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->watch_dog_work,
912 				     RTW_WATCH_DOG_DELAY_TIME);
913 
914 	set_bit(RTW_FLAG_RUNNING, rtwdev->flags);
915 
916 	return 0;
917 }
918 
919 static void rtw_power_off(struct rtw_dev *rtwdev)
920 {
921 	rtw_hci_stop(rtwdev);
922 	rtw_mac_power_off(rtwdev);
923 }
924 
925 void rtw_core_stop(struct rtw_dev *rtwdev)
926 {
927 	struct rtw_coex *coex = &rtwdev->coex;
928 
929 	clear_bit(RTW_FLAG_RUNNING, rtwdev->flags);
930 	clear_bit(RTW_FLAG_FW_RUNNING, rtwdev->flags);
931 
932 	mutex_unlock(&rtwdev->mutex);
933 
934 	cancel_work_sync(&rtwdev->c2h_work);
935 	cancel_delayed_work_sync(&rtwdev->watch_dog_work);
936 	cancel_delayed_work_sync(&coex->bt_relink_work);
937 	cancel_delayed_work_sync(&coex->bt_reenable_work);
938 	cancel_delayed_work_sync(&coex->defreeze_work);
939 	cancel_delayed_work_sync(&coex->wl_remain_work);
940 	cancel_delayed_work_sync(&coex->bt_remain_work);
941 
942 	mutex_lock(&rtwdev->mutex);
943 
944 	rtw_power_off(rtwdev);
945 }
946 
947 static void rtw_init_ht_cap(struct rtw_dev *rtwdev,
948 			    struct ieee80211_sta_ht_cap *ht_cap)
949 {
950 	struct rtw_efuse *efuse = &rtwdev->efuse;
951 
952 	ht_cap->ht_supported = true;
953 	ht_cap->cap = 0;
954 	ht_cap->cap |= IEEE80211_HT_CAP_SGI_20 |
955 			IEEE80211_HT_CAP_MAX_AMSDU |
956 			(1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
957 
958 	if (rtw_chip_has_rx_ldpc(rtwdev))
959 		ht_cap->cap |= IEEE80211_HT_CAP_LDPC_CODING;
960 
961 	if (efuse->hw_cap.bw & BIT(RTW_CHANNEL_WIDTH_40))
962 		ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
963 				IEEE80211_HT_CAP_DSSSCCK40 |
964 				IEEE80211_HT_CAP_SGI_40;
965 	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
966 	ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
967 	ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
968 	if (efuse->hw_cap.nss > 1) {
969 		ht_cap->mcs.rx_mask[0] = 0xFF;
970 		ht_cap->mcs.rx_mask[1] = 0xFF;
971 		ht_cap->mcs.rx_mask[4] = 0x01;
972 		ht_cap->mcs.rx_highest = cpu_to_le16(300);
973 	} else {
974 		ht_cap->mcs.rx_mask[0] = 0xFF;
975 		ht_cap->mcs.rx_mask[1] = 0x00;
976 		ht_cap->mcs.rx_mask[4] = 0x01;
977 		ht_cap->mcs.rx_highest = cpu_to_le16(150);
978 	}
979 }
980 
981 static void rtw_init_vht_cap(struct rtw_dev *rtwdev,
982 			     struct ieee80211_sta_vht_cap *vht_cap)
983 {
984 	struct rtw_efuse *efuse = &rtwdev->efuse;
985 	u16 mcs_map;
986 	__le16 highest;
987 
988 	if (efuse->hw_cap.ptcl != EFUSE_HW_CAP_IGNORE &&
989 	    efuse->hw_cap.ptcl != EFUSE_HW_CAP_PTCL_VHT)
990 		return;
991 
992 	vht_cap->vht_supported = true;
993 	vht_cap->cap = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
994 		       IEEE80211_VHT_CAP_SHORT_GI_80 |
995 		       IEEE80211_VHT_CAP_RXSTBC_1 |
996 		       IEEE80211_VHT_CAP_HTC_VHT |
997 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
998 		       0;
999 	if (rtwdev->hal.rf_path_num > 1)
1000 		vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
1001 	vht_cap->cap |= IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
1002 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
1003 	vht_cap->cap |= (rtwdev->hal.bfee_sts_cap <<
1004 			IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
1005 
1006 	if (rtw_chip_has_rx_ldpc(rtwdev))
1007 		vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC;
1008 
1009 	mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
1010 		  IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
1011 		  IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
1012 		  IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
1013 		  IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
1014 		  IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
1015 		  IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
1016 	if (efuse->hw_cap.nss > 1) {
1017 		highest = cpu_to_le16(780);
1018 		mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << 2;
1019 	} else {
1020 		highest = cpu_to_le16(390);
1021 		mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << 2;
1022 	}
1023 
1024 	vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
1025 	vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
1026 	vht_cap->vht_mcs.rx_highest = highest;
1027 	vht_cap->vht_mcs.tx_highest = highest;
1028 }
1029 
1030 static void rtw_set_supported_band(struct ieee80211_hw *hw,
1031 				   struct rtw_chip_info *chip)
1032 {
1033 	struct rtw_dev *rtwdev = hw->priv;
1034 	struct ieee80211_supported_band *sband;
1035 
1036 	if (chip->band & RTW_BAND_2G) {
1037 		sband = kmemdup(&rtw_band_2ghz, sizeof(*sband), GFP_KERNEL);
1038 		if (!sband)
1039 			goto err_out;
1040 		if (chip->ht_supported)
1041 			rtw_init_ht_cap(rtwdev, &sband->ht_cap);
1042 		hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
1043 	}
1044 
1045 	if (chip->band & RTW_BAND_5G) {
1046 		sband = kmemdup(&rtw_band_5ghz, sizeof(*sband), GFP_KERNEL);
1047 		if (!sband)
1048 			goto err_out;
1049 		if (chip->ht_supported)
1050 			rtw_init_ht_cap(rtwdev, &sband->ht_cap);
1051 		if (chip->vht_supported)
1052 			rtw_init_vht_cap(rtwdev, &sband->vht_cap);
1053 		hw->wiphy->bands[NL80211_BAND_5GHZ] = sband;
1054 	}
1055 
1056 	return;
1057 
1058 err_out:
1059 	rtw_err(rtwdev, "failed to set supported band\n");
1060 	kfree(sband);
1061 }
1062 
1063 static void rtw_unset_supported_band(struct ieee80211_hw *hw,
1064 				     struct rtw_chip_info *chip)
1065 {
1066 	kfree(hw->wiphy->bands[NL80211_BAND_2GHZ]);
1067 	kfree(hw->wiphy->bands[NL80211_BAND_5GHZ]);
1068 }
1069 
1070 static void __update_firmware_info(struct rtw_dev *rtwdev,
1071 				   struct rtw_fw_state *fw)
1072 {
1073 	const struct rtw_fw_hdr *fw_hdr =
1074 				(const struct rtw_fw_hdr *)fw->firmware->data;
1075 
1076 	fw->h2c_version = le16_to_cpu(fw_hdr->h2c_fmt_ver);
1077 	fw->version = le16_to_cpu(fw_hdr->version);
1078 	fw->sub_version = fw_hdr->subversion;
1079 	fw->sub_index = fw_hdr->subindex;
1080 }
1081 
1082 static void __update_firmware_info_legacy(struct rtw_dev *rtwdev,
1083 					  struct rtw_fw_state *fw)
1084 {
1085 	struct rtw_fw_hdr_legacy *legacy =
1086 				(struct rtw_fw_hdr_legacy *)fw->firmware->data;
1087 
1088 	fw->h2c_version = 0;
1089 	fw->version = le16_to_cpu(legacy->version);
1090 	fw->sub_version = legacy->subversion1;
1091 	fw->sub_index = legacy->subversion2;
1092 }
1093 
1094 static void update_firmware_info(struct rtw_dev *rtwdev,
1095 				 struct rtw_fw_state *fw)
1096 {
1097 	if (rtw_chip_wcpu_11n(rtwdev))
1098 		__update_firmware_info_legacy(rtwdev, fw);
1099 	else
1100 		__update_firmware_info(rtwdev, fw);
1101 }
1102 
1103 static void rtw_load_firmware_cb(const struct firmware *firmware, void *context)
1104 {
1105 	struct rtw_fw_state *fw = context;
1106 	struct rtw_dev *rtwdev = fw->rtwdev;
1107 
1108 	if (!firmware || !firmware->data) {
1109 		rtw_err(rtwdev, "failed to request firmware\n");
1110 		complete_all(&fw->completion);
1111 		return;
1112 	}
1113 
1114 	fw->firmware = firmware;
1115 	update_firmware_info(rtwdev, fw);
1116 	complete_all(&fw->completion);
1117 
1118 	rtw_info(rtwdev, "Firmware version %u.%u.%u, H2C version %u\n",
1119 		 fw->version, fw->sub_version, fw->sub_index, fw->h2c_version);
1120 }
1121 
1122 static int rtw_load_firmware(struct rtw_dev *rtwdev, enum rtw_fw_type type)
1123 {
1124 	const char *fw_name;
1125 	struct rtw_fw_state *fw;
1126 	int ret;
1127 
1128 	switch (type) {
1129 	case RTW_WOWLAN_FW:
1130 		fw = &rtwdev->wow_fw;
1131 		fw_name = rtwdev->chip->wow_fw_name;
1132 		break;
1133 
1134 	case RTW_NORMAL_FW:
1135 		fw = &rtwdev->fw;
1136 		fw_name = rtwdev->chip->fw_name;
1137 		break;
1138 
1139 	default:
1140 		rtw_warn(rtwdev, "unsupported firmware type\n");
1141 		return -ENOENT;
1142 	}
1143 
1144 	fw->rtwdev = rtwdev;
1145 	init_completion(&fw->completion);
1146 
1147 	ret = request_firmware_nowait(THIS_MODULE, true, fw_name, rtwdev->dev,
1148 				      GFP_KERNEL, fw, rtw_load_firmware_cb);
1149 	if (ret) {
1150 		rtw_err(rtwdev, "failed to async firmware request\n");
1151 		return ret;
1152 	}
1153 
1154 	return 0;
1155 }
1156 
1157 static int rtw_chip_parameter_setup(struct rtw_dev *rtwdev)
1158 {
1159 	struct rtw_chip_info *chip = rtwdev->chip;
1160 	struct rtw_hal *hal = &rtwdev->hal;
1161 	struct rtw_efuse *efuse = &rtwdev->efuse;
1162 	int ret = 0;
1163 
1164 	switch (rtw_hci_type(rtwdev)) {
1165 	case RTW_HCI_TYPE_PCIE:
1166 		rtwdev->hci.rpwm_addr = 0x03d9;
1167 		rtwdev->hci.cpwm_addr = 0x03da;
1168 		break;
1169 	default:
1170 		rtw_err(rtwdev, "unsupported hci type\n");
1171 		return -EINVAL;
1172 	}
1173 
1174 	hal->chip_version = rtw_read32(rtwdev, REG_SYS_CFG1);
1175 	hal->cut_version = BIT_GET_CHIP_VER(hal->chip_version);
1176 	hal->mp_chip = (hal->chip_version & BIT_RTL_ID) ? 0 : 1;
1177 	if (hal->chip_version & BIT_RF_TYPE_ID) {
1178 		hal->rf_type = RF_2T2R;
1179 		hal->rf_path_num = 2;
1180 		hal->antenna_tx = BB_PATH_AB;
1181 		hal->antenna_rx = BB_PATH_AB;
1182 	} else {
1183 		hal->rf_type = RF_1T1R;
1184 		hal->rf_path_num = 1;
1185 		hal->antenna_tx = BB_PATH_A;
1186 		hal->antenna_rx = BB_PATH_A;
1187 	}
1188 	hal->rf_phy_num = chip->fix_rf_phy_num ? chip->fix_rf_phy_num :
1189 			  hal->rf_path_num;
1190 
1191 	efuse->physical_size = chip->phy_efuse_size;
1192 	efuse->logical_size = chip->log_efuse_size;
1193 	efuse->protect_size = chip->ptct_efuse_size;
1194 
1195 	/* default use ack */
1196 	rtwdev->hal.rcr |= BIT_VHT_DACK;
1197 
1198 	hal->bfee_sts_cap = 3;
1199 
1200 	return ret;
1201 }
1202 
1203 static int rtw_chip_efuse_enable(struct rtw_dev *rtwdev)
1204 {
1205 	struct rtw_fw_state *fw = &rtwdev->fw;
1206 	int ret;
1207 
1208 	ret = rtw_hci_setup(rtwdev);
1209 	if (ret) {
1210 		rtw_err(rtwdev, "failed to setup hci\n");
1211 		goto err;
1212 	}
1213 
1214 	ret = rtw_mac_power_on(rtwdev);
1215 	if (ret) {
1216 		rtw_err(rtwdev, "failed to power on mac\n");
1217 		goto err;
1218 	}
1219 
1220 	rtw_write8(rtwdev, REG_C2HEVT, C2H_HW_FEATURE_DUMP);
1221 
1222 	wait_for_completion(&fw->completion);
1223 	if (!fw->firmware) {
1224 		ret = -EINVAL;
1225 		rtw_err(rtwdev, "failed to load firmware\n");
1226 		goto err;
1227 	}
1228 
1229 	ret = rtw_download_firmware(rtwdev, fw);
1230 	if (ret) {
1231 		rtw_err(rtwdev, "failed to download firmware\n");
1232 		goto err_off;
1233 	}
1234 
1235 	return 0;
1236 
1237 err_off:
1238 	rtw_mac_power_off(rtwdev);
1239 
1240 err:
1241 	return ret;
1242 }
1243 
1244 static int rtw_dump_hw_feature(struct rtw_dev *rtwdev)
1245 {
1246 	struct rtw_efuse *efuse = &rtwdev->efuse;
1247 	u8 hw_feature[HW_FEATURE_LEN];
1248 	u8 id;
1249 	u8 bw;
1250 	int i;
1251 
1252 	id = rtw_read8(rtwdev, REG_C2HEVT);
1253 	if (id != C2H_HW_FEATURE_REPORT) {
1254 		rtw_err(rtwdev, "failed to read hw feature report\n");
1255 		return -EBUSY;
1256 	}
1257 
1258 	for (i = 0; i < HW_FEATURE_LEN; i++)
1259 		hw_feature[i] = rtw_read8(rtwdev, REG_C2HEVT + 2 + i);
1260 
1261 	rtw_write8(rtwdev, REG_C2HEVT, 0);
1262 
1263 	bw = GET_EFUSE_HW_CAP_BW(hw_feature);
1264 	efuse->hw_cap.bw = hw_bw_cap_to_bitamp(bw);
1265 	efuse->hw_cap.hci = GET_EFUSE_HW_CAP_HCI(hw_feature);
1266 	efuse->hw_cap.nss = GET_EFUSE_HW_CAP_NSS(hw_feature);
1267 	efuse->hw_cap.ptcl = GET_EFUSE_HW_CAP_PTCL(hw_feature);
1268 	efuse->hw_cap.ant_num = GET_EFUSE_HW_CAP_ANT_NUM(hw_feature);
1269 
1270 	rtw_hw_config_rf_ant_num(rtwdev, efuse->hw_cap.ant_num);
1271 
1272 	if (efuse->hw_cap.nss == EFUSE_HW_CAP_IGNORE ||
1273 	    efuse->hw_cap.nss > rtwdev->hal.rf_path_num)
1274 		efuse->hw_cap.nss = rtwdev->hal.rf_path_num;
1275 
1276 	rtw_dbg(rtwdev, RTW_DBG_EFUSE,
1277 		"hw cap: hci=0x%02x, bw=0x%02x, ptcl=0x%02x, ant_num=%d, nss=%d\n",
1278 		efuse->hw_cap.hci, efuse->hw_cap.bw, efuse->hw_cap.ptcl,
1279 		efuse->hw_cap.ant_num, efuse->hw_cap.nss);
1280 
1281 	return 0;
1282 }
1283 
1284 static void rtw_chip_efuse_disable(struct rtw_dev *rtwdev)
1285 {
1286 	rtw_hci_stop(rtwdev);
1287 	rtw_mac_power_off(rtwdev);
1288 }
1289 
1290 static int rtw_chip_efuse_info_setup(struct rtw_dev *rtwdev)
1291 {
1292 	struct rtw_efuse *efuse = &rtwdev->efuse;
1293 	int ret;
1294 
1295 	mutex_lock(&rtwdev->mutex);
1296 
1297 	/* power on mac to read efuse */
1298 	ret = rtw_chip_efuse_enable(rtwdev);
1299 	if (ret)
1300 		goto out_unlock;
1301 
1302 	ret = rtw_parse_efuse_map(rtwdev);
1303 	if (ret)
1304 		goto out_disable;
1305 
1306 	ret = rtw_dump_hw_feature(rtwdev);
1307 	if (ret)
1308 		goto out_disable;
1309 
1310 	ret = rtw_check_supported_rfe(rtwdev);
1311 	if (ret)
1312 		goto out_disable;
1313 
1314 	if (efuse->crystal_cap == 0xff)
1315 		efuse->crystal_cap = 0;
1316 	if (efuse->pa_type_2g == 0xff)
1317 		efuse->pa_type_2g = 0;
1318 	if (efuse->pa_type_5g == 0xff)
1319 		efuse->pa_type_5g = 0;
1320 	if (efuse->lna_type_2g == 0xff)
1321 		efuse->lna_type_2g = 0;
1322 	if (efuse->lna_type_5g == 0xff)
1323 		efuse->lna_type_5g = 0;
1324 	if (efuse->channel_plan == 0xff)
1325 		efuse->channel_plan = 0x7f;
1326 	if (efuse->rf_board_option == 0xff)
1327 		efuse->rf_board_option = 0;
1328 	if (efuse->bt_setting & BIT(0))
1329 		efuse->share_ant = true;
1330 	if (efuse->regd == 0xff)
1331 		efuse->regd = 0;
1332 	if (efuse->tx_bb_swing_setting_2g == 0xff)
1333 		efuse->tx_bb_swing_setting_2g = 0;
1334 	if (efuse->tx_bb_swing_setting_5g == 0xff)
1335 		efuse->tx_bb_swing_setting_5g = 0;
1336 
1337 	efuse->btcoex = (efuse->rf_board_option & 0xe0) == 0x20;
1338 	efuse->ext_pa_2g = efuse->pa_type_2g & BIT(4) ? 1 : 0;
1339 	efuse->ext_lna_2g = efuse->lna_type_2g & BIT(3) ? 1 : 0;
1340 	efuse->ext_pa_5g = efuse->pa_type_5g & BIT(0) ? 1 : 0;
1341 	efuse->ext_lna_2g = efuse->lna_type_5g & BIT(3) ? 1 : 0;
1342 
1343 out_disable:
1344 	rtw_chip_efuse_disable(rtwdev);
1345 
1346 out_unlock:
1347 	mutex_unlock(&rtwdev->mutex);
1348 	return ret;
1349 }
1350 
1351 static int rtw_chip_board_info_setup(struct rtw_dev *rtwdev)
1352 {
1353 	struct rtw_hal *hal = &rtwdev->hal;
1354 	const struct rtw_rfe_def *rfe_def = rtw_get_rfe_def(rtwdev);
1355 
1356 	if (!rfe_def)
1357 		return -ENODEV;
1358 
1359 	rtw_phy_setup_phy_cond(rtwdev, 0);
1360 
1361 	rtw_phy_init_tx_power(rtwdev);
1362 	rtw_load_table(rtwdev, rfe_def->phy_pg_tbl);
1363 	rtw_load_table(rtwdev, rfe_def->txpwr_lmt_tbl);
1364 	rtw_phy_tx_power_by_rate_config(hal);
1365 	rtw_phy_tx_power_limit_config(hal);
1366 
1367 	return 0;
1368 }
1369 
1370 int rtw_chip_info_setup(struct rtw_dev *rtwdev)
1371 {
1372 	int ret;
1373 
1374 	ret = rtw_chip_parameter_setup(rtwdev);
1375 	if (ret) {
1376 		rtw_err(rtwdev, "failed to setup chip parameters\n");
1377 		goto err_out;
1378 	}
1379 
1380 	ret = rtw_chip_efuse_info_setup(rtwdev);
1381 	if (ret) {
1382 		rtw_err(rtwdev, "failed to setup chip efuse info\n");
1383 		goto err_out;
1384 	}
1385 
1386 	ret = rtw_chip_board_info_setup(rtwdev);
1387 	if (ret) {
1388 		rtw_err(rtwdev, "failed to setup chip board info\n");
1389 		goto err_out;
1390 	}
1391 
1392 	return 0;
1393 
1394 err_out:
1395 	return ret;
1396 }
1397 EXPORT_SYMBOL(rtw_chip_info_setup);
1398 
1399 static void rtw_stats_init(struct rtw_dev *rtwdev)
1400 {
1401 	struct rtw_traffic_stats *stats = &rtwdev->stats;
1402 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
1403 	int i;
1404 
1405 	ewma_tp_init(&stats->tx_ewma_tp);
1406 	ewma_tp_init(&stats->rx_ewma_tp);
1407 
1408 	for (i = 0; i < RTW_EVM_NUM; i++)
1409 		ewma_evm_init(&dm_info->ewma_evm[i]);
1410 	for (i = 0; i < RTW_SNR_NUM; i++)
1411 		ewma_snr_init(&dm_info->ewma_snr[i]);
1412 }
1413 
1414 int rtw_core_init(struct rtw_dev *rtwdev)
1415 {
1416 	struct rtw_chip_info *chip = rtwdev->chip;
1417 	struct rtw_coex *coex = &rtwdev->coex;
1418 	int ret;
1419 
1420 	INIT_LIST_HEAD(&rtwdev->rsvd_page_list);
1421 	INIT_LIST_HEAD(&rtwdev->txqs);
1422 
1423 	timer_setup(&rtwdev->tx_report.purge_timer,
1424 		    rtw_tx_report_purge_timer, 0);
1425 	tasklet_init(&rtwdev->tx_tasklet, rtw_tx_tasklet,
1426 		     (unsigned long)rtwdev);
1427 
1428 	INIT_DELAYED_WORK(&rtwdev->watch_dog_work, rtw_watch_dog_work);
1429 	INIT_DELAYED_WORK(&coex->bt_relink_work, rtw_coex_bt_relink_work);
1430 	INIT_DELAYED_WORK(&coex->bt_reenable_work, rtw_coex_bt_reenable_work);
1431 	INIT_DELAYED_WORK(&coex->defreeze_work, rtw_coex_defreeze_work);
1432 	INIT_DELAYED_WORK(&coex->wl_remain_work, rtw_coex_wl_remain_work);
1433 	INIT_DELAYED_WORK(&coex->bt_remain_work, rtw_coex_bt_remain_work);
1434 	INIT_WORK(&rtwdev->c2h_work, rtw_c2h_work);
1435 	INIT_WORK(&rtwdev->ba_work, rtw_txq_ba_work);
1436 	skb_queue_head_init(&rtwdev->c2h_queue);
1437 	skb_queue_head_init(&rtwdev->coex.queue);
1438 	skb_queue_head_init(&rtwdev->tx_report.queue);
1439 
1440 	spin_lock_init(&rtwdev->rf_lock);
1441 	spin_lock_init(&rtwdev->h2c.lock);
1442 	spin_lock_init(&rtwdev->txq_lock);
1443 	spin_lock_init(&rtwdev->tx_report.q_lock);
1444 
1445 	mutex_init(&rtwdev->mutex);
1446 	mutex_init(&rtwdev->coex.mutex);
1447 	mutex_init(&rtwdev->hal.tx_power_mutex);
1448 
1449 	init_waitqueue_head(&rtwdev->coex.wait);
1450 
1451 	rtwdev->sec.total_cam_num = 32;
1452 	rtwdev->hal.current_channel = 1;
1453 	set_bit(RTW_BC_MC_MACID, rtwdev->mac_id_map);
1454 	if (!(BIT(rtw_fw_lps_deep_mode) & chip->lps_deep_mode_supported))
1455 		rtwdev->lps_conf.deep_mode = LPS_DEEP_MODE_NONE;
1456 	else
1457 		rtwdev->lps_conf.deep_mode = rtw_fw_lps_deep_mode;
1458 
1459 	rtw_stats_init(rtwdev);
1460 
1461 	/* default rx filter setting */
1462 	rtwdev->hal.rcr = BIT_APP_FCS | BIT_APP_MIC | BIT_APP_ICV |
1463 			  BIT_HTC_LOC_CTRL | BIT_APP_PHYSTS |
1464 			  BIT_AB | BIT_AM | BIT_APM;
1465 
1466 	ret = rtw_load_firmware(rtwdev, RTW_NORMAL_FW);
1467 	if (ret) {
1468 		rtw_warn(rtwdev, "no firmware loaded\n");
1469 		return ret;
1470 	}
1471 
1472 	if (chip->wow_fw_name) {
1473 		ret = rtw_load_firmware(rtwdev, RTW_WOWLAN_FW);
1474 		if (ret) {
1475 			rtw_warn(rtwdev, "no wow firmware loaded\n");
1476 			return ret;
1477 		}
1478 	}
1479 	return 0;
1480 }
1481 EXPORT_SYMBOL(rtw_core_init);
1482 
1483 void rtw_core_deinit(struct rtw_dev *rtwdev)
1484 {
1485 	struct rtw_fw_state *fw = &rtwdev->fw;
1486 	struct rtw_fw_state *wow_fw = &rtwdev->wow_fw;
1487 	struct rtw_rsvd_page *rsvd_pkt, *tmp;
1488 	unsigned long flags;
1489 
1490 	if (fw->firmware)
1491 		release_firmware(fw->firmware);
1492 
1493 	if (wow_fw->firmware)
1494 		release_firmware(wow_fw->firmware);
1495 
1496 	tasklet_kill(&rtwdev->tx_tasklet);
1497 	spin_lock_irqsave(&rtwdev->tx_report.q_lock, flags);
1498 	skb_queue_purge(&rtwdev->tx_report.queue);
1499 	spin_unlock_irqrestore(&rtwdev->tx_report.q_lock, flags);
1500 
1501 	list_for_each_entry_safe(rsvd_pkt, tmp, &rtwdev->rsvd_page_list,
1502 				 build_list) {
1503 		list_del(&rsvd_pkt->build_list);
1504 		kfree(rsvd_pkt);
1505 	}
1506 
1507 	mutex_destroy(&rtwdev->mutex);
1508 	mutex_destroy(&rtwdev->coex.mutex);
1509 	mutex_destroy(&rtwdev->hal.tx_power_mutex);
1510 }
1511 EXPORT_SYMBOL(rtw_core_deinit);
1512 
1513 int rtw_register_hw(struct rtw_dev *rtwdev, struct ieee80211_hw *hw)
1514 {
1515 	struct rtw_hal *hal = &rtwdev->hal;
1516 	int max_tx_headroom = 0;
1517 	int ret;
1518 
1519 	/* TODO: USB & SDIO may need extra room? */
1520 	max_tx_headroom = rtwdev->chip->tx_pkt_desc_sz;
1521 
1522 	hw->extra_tx_headroom = max_tx_headroom;
1523 	hw->queues = IEEE80211_NUM_ACS;
1524 	hw->txq_data_size = sizeof(struct rtw_txq);
1525 	hw->sta_data_size = sizeof(struct rtw_sta_info);
1526 	hw->vif_data_size = sizeof(struct rtw_vif);
1527 
1528 	ieee80211_hw_set(hw, SIGNAL_DBM);
1529 	ieee80211_hw_set(hw, RX_INCLUDES_FCS);
1530 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
1531 	ieee80211_hw_set(hw, MFP_CAPABLE);
1532 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
1533 	ieee80211_hw_set(hw, SUPPORTS_PS);
1534 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
1535 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
1536 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
1537 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
1538 	ieee80211_hw_set(hw, TX_AMSDU);
1539 
1540 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1541 				     BIT(NL80211_IFTYPE_AP) |
1542 				     BIT(NL80211_IFTYPE_ADHOC) |
1543 				     BIT(NL80211_IFTYPE_MESH_POINT);
1544 	hw->wiphy->available_antennas_tx = hal->antenna_tx;
1545 	hw->wiphy->available_antennas_rx = hal->antenna_rx;
1546 
1547 	hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
1548 			    WIPHY_FLAG_TDLS_EXTERNAL_SETUP;
1549 
1550 	hw->wiphy->features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
1551 
1552 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CAN_REPLACE_PTK0);
1553 
1554 #ifdef CONFIG_PM
1555 	hw->wiphy->wowlan = rtwdev->chip->wowlan_stub;
1556 	hw->wiphy->max_sched_scan_ssids = rtwdev->chip->max_sched_scan_ssids;
1557 #endif
1558 	rtw_set_supported_band(hw, rtwdev->chip);
1559 	SET_IEEE80211_PERM_ADDR(hw, rtwdev->efuse.addr);
1560 
1561 	rtw_regd_init(rtwdev, rtw_regd_notifier);
1562 
1563 	ret = ieee80211_register_hw(hw);
1564 	if (ret) {
1565 		rtw_err(rtwdev, "failed to register hw\n");
1566 		return ret;
1567 	}
1568 
1569 	if (regulatory_hint(hw->wiphy, rtwdev->regd.alpha2))
1570 		rtw_err(rtwdev, "regulatory_hint fail\n");
1571 
1572 	rtw_debugfs_init(rtwdev);
1573 
1574 	rtwdev->bf_info.bfer_mu_cnt = 0;
1575 	rtwdev->bf_info.bfer_su_cnt = 0;
1576 
1577 	return 0;
1578 }
1579 EXPORT_SYMBOL(rtw_register_hw);
1580 
1581 void rtw_unregister_hw(struct rtw_dev *rtwdev, struct ieee80211_hw *hw)
1582 {
1583 	struct rtw_chip_info *chip = rtwdev->chip;
1584 
1585 	ieee80211_unregister_hw(hw);
1586 	rtw_unset_supported_band(hw, chip);
1587 }
1588 EXPORT_SYMBOL(rtw_unregister_hw);
1589 
1590 MODULE_AUTHOR("Realtek Corporation");
1591 MODULE_DESCRIPTION("Realtek 802.11ac wireless core module");
1592 MODULE_LICENSE("Dual BSD/GPL");
1593