1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include <linux/etherdevice.h>
5 #include "mt7915.h"
6 #include "mac.h"
7 #include "mcu.h"
8 #include "eeprom.h"
9 
10 #define CCK_RATE(_idx, _rate) {						\
11 	.bitrate = _rate,						\
12 	.flags = IEEE80211_RATE_SHORT_PREAMBLE,				\
13 	.hw_value = (MT_PHY_TYPE_CCK << 8) | (_idx),			\
14 	.hw_value_short = (MT_PHY_TYPE_CCK << 8) | (4 + (_idx)),	\
15 }
16 
17 #define OFDM_RATE(_idx, _rate) {					\
18 	.bitrate = _rate,						\
19 	.hw_value = (MT_PHY_TYPE_OFDM << 8) | (_idx),			\
20 	.hw_value_short = (MT_PHY_TYPE_OFDM << 8) | (_idx),		\
21 }
22 
23 static struct ieee80211_rate mt7915_rates[] = {
24 	CCK_RATE(0, 10),
25 	CCK_RATE(1, 20),
26 	CCK_RATE(2, 55),
27 	CCK_RATE(3, 110),
28 	OFDM_RATE(11, 60),
29 	OFDM_RATE(15, 90),
30 	OFDM_RATE(10, 120),
31 	OFDM_RATE(14, 180),
32 	OFDM_RATE(9,  240),
33 	OFDM_RATE(13, 360),
34 	OFDM_RATE(8,  480),
35 	OFDM_RATE(12, 540),
36 };
37 
38 static const struct ieee80211_iface_limit if_limits[] = {
39 	{
40 		.max = 1,
41 		.types = BIT(NL80211_IFTYPE_ADHOC)
42 	}, {
43 		.max = 16,
44 		.types = BIT(NL80211_IFTYPE_AP)
45 #ifdef CONFIG_MAC80211_MESH
46 			 | BIT(NL80211_IFTYPE_MESH_POINT)
47 #endif
48 	}, {
49 		.max = MT7915_MAX_INTERFACES,
50 		.types = BIT(NL80211_IFTYPE_STATION)
51 	}
52 };
53 
54 static const struct ieee80211_iface_combination if_comb[] = {
55 	{
56 		.limits = if_limits,
57 		.n_limits = ARRAY_SIZE(if_limits),
58 		.max_interfaces = MT7915_MAX_INTERFACES,
59 		.num_different_channels = 1,
60 		.beacon_int_infra_match = true,
61 		.radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
62 				       BIT(NL80211_CHAN_WIDTH_20) |
63 				       BIT(NL80211_CHAN_WIDTH_40) |
64 				       BIT(NL80211_CHAN_WIDTH_80) |
65 				       BIT(NL80211_CHAN_WIDTH_160) |
66 				       BIT(NL80211_CHAN_WIDTH_80P80),
67 	}
68 };
69 
70 static void
71 mt7915_init_txpower(struct mt7915_dev *dev,
72 		    struct ieee80211_supported_band *sband)
73 {
74 	int i, n_chains = hweight8(dev->mphy.antenna_mask);
75 	int nss_delta = mt76_tx_power_nss_delta(n_chains);
76 	int pwr_delta = mt7915_eeprom_get_power_delta(dev, sband->band);
77 	struct mt76_power_limits limits;
78 
79 	for (i = 0; i < sband->n_channels; i++) {
80 		struct ieee80211_channel *chan = &sband->channels[i];
81 		u32 target_power = 0;
82 		int j;
83 
84 		for (j = 0; j < n_chains; j++) {
85 			u32 val;
86 
87 			val = mt7915_eeprom_get_target_power(dev, chan, j);
88 			target_power = max(target_power, val);
89 		}
90 
91 		target_power += pwr_delta;
92 		target_power = mt76_get_rate_power_limits(&dev->mphy, chan,
93 							  &limits,
94 							  target_power);
95 		target_power += nss_delta;
96 		target_power = DIV_ROUND_UP(target_power, 2);
97 		chan->max_power = min_t(int, chan->max_reg_power,
98 					target_power);
99 		chan->orig_mpwr = target_power;
100 	}
101 }
102 
103 static void
104 mt7915_regd_notifier(struct wiphy *wiphy,
105 		     struct regulatory_request *request)
106 {
107 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
108 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
109 	struct mt76_phy *mphy = hw->priv;
110 	struct mt7915_phy *phy = mphy->priv;
111 	struct cfg80211_chan_def *chandef = &mphy->chandef;
112 
113 	memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2));
114 	dev->mt76.region = request->dfs_region;
115 
116 	mt7915_init_txpower(dev, &mphy->sband_2g.sband);
117 	mt7915_init_txpower(dev, &mphy->sband_5g.sband);
118 
119 	if (!(chandef->chan->flags & IEEE80211_CHAN_RADAR))
120 		return;
121 
122 	mt7915_dfs_init_radar_detector(phy);
123 }
124 
125 static void
126 mt7915_init_wiphy(struct ieee80211_hw *hw)
127 {
128 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
129 	struct wiphy *wiphy = hw->wiphy;
130 
131 	hw->queues = 4;
132 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
133 	hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
134 	hw->netdev_features = NETIF_F_RXCSUM;
135 
136 	hw->radiotap_timestamp.units_pos =
137 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US;
138 
139 	phy->slottime = 9;
140 
141 	hw->sta_data_size = sizeof(struct mt7915_sta);
142 	hw->vif_data_size = sizeof(struct mt7915_vif);
143 
144 	wiphy->iface_combinations = if_comb;
145 	wiphy->n_iface_combinations = ARRAY_SIZE(if_comb);
146 	wiphy->reg_notifier = mt7915_regd_notifier;
147 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
148 
149 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
150 
151 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
152 	ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
153 	ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
154 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
155 
156 	hw->max_tx_fragments = 4;
157 
158 	if (phy->mt76->cap.has_2ghz)
159 		phy->mt76->sband_2g.sband.ht_cap.cap |=
160 			IEEE80211_HT_CAP_LDPC_CODING |
161 			IEEE80211_HT_CAP_MAX_AMSDU;
162 
163 	if (phy->mt76->cap.has_5ghz) {
164 		phy->mt76->sband_5g.sband.ht_cap.cap |=
165 			IEEE80211_HT_CAP_LDPC_CODING |
166 			IEEE80211_HT_CAP_MAX_AMSDU;
167 		phy->mt76->sband_5g.sband.vht_cap.cap |=
168 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991 |
169 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
170 	}
171 
172 	mt76_set_stream_caps(phy->mt76, true);
173 	mt7915_set_stream_vht_txbf_caps(phy);
174 	mt7915_set_stream_he_caps(phy);
175 }
176 
177 static void
178 mt7915_mac_init_band(struct mt7915_dev *dev, u8 band)
179 {
180 	u32 mask, set;
181 
182 	mt76_rmw_field(dev, MT_TMAC_CTCR0(band),
183 		       MT_TMAC_CTCR0_INS_DDLMT_REFTIME, 0x3f);
184 	mt76_set(dev, MT_TMAC_CTCR0(band),
185 		 MT_TMAC_CTCR0_INS_DDLMT_VHT_SMPDU_EN |
186 		 MT_TMAC_CTCR0_INS_DDLMT_EN);
187 
188 	mask = MT_MDP_RCFR0_MCU_RX_MGMT |
189 	       MT_MDP_RCFR0_MCU_RX_CTL_NON_BAR |
190 	       MT_MDP_RCFR0_MCU_RX_CTL_BAR;
191 	set = FIELD_PREP(MT_MDP_RCFR0_MCU_RX_MGMT, MT_MDP_TO_HIF) |
192 	      FIELD_PREP(MT_MDP_RCFR0_MCU_RX_CTL_NON_BAR, MT_MDP_TO_HIF) |
193 	      FIELD_PREP(MT_MDP_RCFR0_MCU_RX_CTL_BAR, MT_MDP_TO_HIF);
194 	mt76_rmw(dev, MT_MDP_BNRCFR0(band), mask, set);
195 
196 	mask = MT_MDP_RCFR1_MCU_RX_BYPASS |
197 	       MT_MDP_RCFR1_RX_DROPPED_UCAST |
198 	       MT_MDP_RCFR1_RX_DROPPED_MCAST;
199 	set = FIELD_PREP(MT_MDP_RCFR1_MCU_RX_BYPASS, MT_MDP_TO_HIF) |
200 	      FIELD_PREP(MT_MDP_RCFR1_RX_DROPPED_UCAST, MT_MDP_TO_HIF) |
201 	      FIELD_PREP(MT_MDP_RCFR1_RX_DROPPED_MCAST, MT_MDP_TO_HIF);
202 	mt76_rmw(dev, MT_MDP_BNRCFR1(band), mask, set);
203 
204 	mt76_set(dev, MT_WF_RMAC_MIB_TIME0(band), MT_WF_RMAC_MIB_RXTIME_EN);
205 	mt76_set(dev, MT_WF_RMAC_MIB_AIRTIME0(band), MT_WF_RMAC_MIB_RXTIME_EN);
206 
207 	mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_MAX_RX_LEN, 1536);
208 	/* disable rx rate report by default due to hw issues */
209 	mt76_clear(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN);
210 }
211 
212 static void mt7915_mac_init(struct mt7915_dev *dev)
213 {
214 	int i;
215 
216 	mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);
217 	/* enable hardware de-agg */
218 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);
219 
220 	for (i = 0; i < MT7915_WTBL_SIZE; i++)
221 		mt7915_mac_wtbl_update(dev, i,
222 				       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
223 	for (i = 0; i < 2; i++)
224 		mt7915_mac_init_band(dev, i);
225 }
226 
227 static int mt7915_txbf_init(struct mt7915_dev *dev)
228 {
229 	int ret;
230 
231 
232 	if (dev->dbdc_support) {
233 		ret = mt7915_mcu_set_txbf_module(dev);
234 		if (ret)
235 			return ret;
236 	}
237 
238 	/* trigger sounding packets */
239 	ret = mt7915_mcu_set_txbf_sounding(dev);
240 	if (ret)
241 		return ret;
242 
243 	/* enable eBF */
244 	return mt7915_mcu_set_txbf_type(dev);
245 }
246 
247 static int mt7915_register_ext_phy(struct mt7915_dev *dev)
248 {
249 	struct mt7915_phy *phy = mt7915_ext_phy(dev);
250 	struct mt76_phy *mphy;
251 	int ret;
252 
253 	if (!dev->dbdc_support)
254 		return 0;
255 
256 	if (phy)
257 		return 0;
258 
259 	mphy = mt76_alloc_phy(&dev->mt76, sizeof(*phy), &mt7915_ops);
260 	if (!mphy)
261 		return -ENOMEM;
262 
263 	phy = mphy->priv;
264 	phy->dev = dev;
265 	phy->mt76 = mphy;
266 	mphy->chainmask = dev->chainmask & ~dev->mphy.chainmask;
267 	mphy->antenna_mask = BIT(hweight8(mphy->chainmask)) - 1;
268 
269 	INIT_LIST_HEAD(&phy->stats_list);
270 	INIT_DELAYED_WORK(&mphy->mac_work, mt7915_mac_work);
271 
272 	mt7915_eeprom_parse_band_config(phy);
273 	mt7915_init_wiphy(mphy->hw);
274 
275 	memcpy(mphy->macaddr, dev->mt76.eeprom.data + MT_EE_MAC_ADDR2,
276 	       ETH_ALEN);
277 	mt76_eeprom_override(mphy);
278 
279 	ret = mt7915_init_tx_queues(phy, MT7915_TXQ_BAND1,
280 				    MT7915_TX_RING_SIZE);
281 	if (ret)
282 		goto error;
283 
284 	ret = mt76_register_phy(mphy, true, mt7915_rates,
285 				ARRAY_SIZE(mt7915_rates));
286 	if (ret)
287 		goto error;
288 
289 	return 0;
290 
291 error:
292 	ieee80211_free_hw(mphy->hw);
293 	return ret;
294 }
295 
296 static void mt7915_init_work(struct work_struct *work)
297 {
298 	struct mt7915_dev *dev = container_of(work, struct mt7915_dev,
299 				 init_work);
300 
301 	mt7915_mcu_set_eeprom(dev);
302 	mt7915_mac_init(dev);
303 	mt7915_init_txpower(dev, &dev->mphy.sband_2g.sband);
304 	mt7915_init_txpower(dev, &dev->mphy.sband_5g.sband);
305 	mt7915_txbf_init(dev);
306 }
307 
308 static void mt7915_wfsys_reset(struct mt7915_dev *dev)
309 {
310 	u32 val = MT_TOP_PWR_KEY | MT_TOP_PWR_SW_PWR_ON | MT_TOP_PWR_PWR_ON;
311 
312 #define MT_MCU_DUMMY_RANDOM	GENMASK(15, 0)
313 #define MT_MCU_DUMMY_DEFAULT	GENMASK(31, 16)
314 
315 	mt76_wr(dev, MT_MCU_WFDMA0_DUMMY_CR, MT_MCU_DUMMY_RANDOM);
316 
317 	/* change to software control */
318 	val |= MT_TOP_PWR_SW_RST;
319 	mt76_wr(dev, MT_TOP_PWR_CTRL, val);
320 
321 	/* reset wfsys */
322 	val &= ~MT_TOP_PWR_SW_RST;
323 	mt76_wr(dev, MT_TOP_PWR_CTRL, val);
324 
325 	/* release wfsys then mcu re-excutes romcode */
326 	val |= MT_TOP_PWR_SW_RST;
327 	mt76_wr(dev, MT_TOP_PWR_CTRL, val);
328 
329 	/* switch to hw control */
330 	val &= ~MT_TOP_PWR_SW_RST;
331 	val |= MT_TOP_PWR_HW_CTRL;
332 	mt76_wr(dev, MT_TOP_PWR_CTRL, val);
333 
334 	/* check whether mcu resets to default */
335 	if (!mt76_poll_msec(dev, MT_MCU_WFDMA0_DUMMY_CR, MT_MCU_DUMMY_DEFAULT,
336 			    MT_MCU_DUMMY_DEFAULT, 1000)) {
337 		dev_err(dev->mt76.dev, "wifi subsystem reset failure\n");
338 		return;
339 	}
340 
341 	/* wfsys reset won't clear host registers */
342 	mt76_clear(dev, MT_TOP_MISC, MT_TOP_MISC_FW_STATE);
343 
344 	msleep(100);
345 }
346 
347 static int mt7915_init_hardware(struct mt7915_dev *dev)
348 {
349 	int ret, idx;
350 
351 	mt76_wr(dev, MT_INT_SOURCE_CSR, ~0);
352 
353 	INIT_WORK(&dev->init_work, mt7915_init_work);
354 	dev->dbdc_support = !!(mt76_rr(dev, MT_HW_BOUND) & BIT(5));
355 
356 	/* If MCU was already running, it is likely in a bad state */
357 	if (mt76_get_field(dev, MT_TOP_MISC, MT_TOP_MISC_FW_STATE) >
358 	    FW_STATE_FW_DOWNLOAD)
359 		mt7915_wfsys_reset(dev);
360 
361 	ret = mt7915_dma_init(dev);
362 	if (ret)
363 		return ret;
364 
365 	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
366 
367 	/*
368 	 * force firmware operation mode into normal state,
369 	 * which should be set before firmware download stage.
370 	 */
371 	mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE);
372 
373 	ret = mt7915_mcu_init(dev);
374 	if (ret) {
375 		/* Reset and try again */
376 		mt7915_wfsys_reset(dev);
377 
378 		ret = mt7915_mcu_init(dev);
379 		if (ret)
380 			return ret;
381 	}
382 
383 	ret = mt7915_eeprom_init(dev);
384 	if (ret < 0)
385 		return ret;
386 
387 
388 	if (dev->flash_mode) {
389 		ret = mt7915_mcu_apply_group_cal(dev);
390 		if (ret)
391 			return ret;
392 	}
393 
394 	/* Beacon and mgmt frames should occupy wcid 0 */
395 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA - 1);
396 	if (idx)
397 		return -ENOSPC;
398 
399 	dev->mt76.global_wcid.idx = idx;
400 	dev->mt76.global_wcid.hw_key_idx = -1;
401 	dev->mt76.global_wcid.tx_info |= MT_WCID_TX_INFO_SET;
402 	rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid);
403 
404 	return 0;
405 }
406 
407 void mt7915_set_stream_vht_txbf_caps(struct mt7915_phy *phy)
408 {
409 	int nss;
410 	u32 *cap;
411 
412 	if (!phy->mt76->cap.has_5ghz)
413 		return;
414 
415 	nss = hweight8(phy->mt76->chainmask);
416 	cap = &phy->mt76->sband_5g.sband.vht_cap.cap;
417 
418 	*cap |= IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
419 		IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
420 		(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
421 
422 	*cap &= ~(IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK |
423 		  IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
424 		  IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
425 
426 	if (nss < 2)
427 		return;
428 
429 	*cap |= IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
430 		IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE |
431 		FIELD_PREP(IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK,
432 			   nss - 1);
433 }
434 
435 static void
436 mt7915_set_stream_he_txbf_caps(struct ieee80211_sta_he_cap *he_cap,
437 			       int vif, int nss)
438 {
439 	struct ieee80211_he_cap_elem *elem = &he_cap->he_cap_elem;
440 	struct ieee80211_he_mcs_nss_supp *mcs = &he_cap->he_mcs_nss_supp;
441 	u8 c;
442 
443 #ifdef CONFIG_MAC80211_MESH
444 	if (vif == NL80211_IFTYPE_MESH_POINT)
445 		return;
446 #endif
447 
448 	elem->phy_cap_info[3] &= ~IEEE80211_HE_PHY_CAP3_SU_BEAMFORMER;
449 	elem->phy_cap_info[4] &= ~IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
450 
451 	c = IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK |
452 	    IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_ABOVE_80MHZ_MASK;
453 	elem->phy_cap_info[5] &= ~c;
454 
455 	c = IEEE80211_HE_PHY_CAP6_TRIG_SU_BEAMFORMING_FB |
456 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB;
457 	elem->phy_cap_info[6] &= ~c;
458 
459 	elem->phy_cap_info[7] &= ~IEEE80211_HE_PHY_CAP7_MAX_NC_MASK;
460 
461 	c = IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
462 	    IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
463 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
464 	elem->phy_cap_info[2] |= c;
465 
466 	c = IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE |
467 	    IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4 |
468 	    IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_4;
469 	elem->phy_cap_info[4] |= c;
470 
471 	/* do not support NG16 due to spec D4.0 changes subcarrier idx */
472 	c = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU |
473 	    IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU;
474 
475 	if (vif == NL80211_IFTYPE_STATION)
476 		c |= IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
477 
478 	elem->phy_cap_info[6] |= c;
479 
480 	if (nss < 2)
481 		return;
482 
483 	if (vif != NL80211_IFTYPE_AP)
484 		return;
485 
486 	elem->phy_cap_info[3] |= IEEE80211_HE_PHY_CAP3_SU_BEAMFORMER;
487 	elem->phy_cap_info[4] |= IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
488 
489 	/* num_snd_dim */
490 	c = (nss - 1) | (max_t(int, le16_to_cpu(mcs->tx_mcs_160), 1) << 3);
491 	elem->phy_cap_info[5] |= c;
492 
493 	c = IEEE80211_HE_PHY_CAP6_TRIG_SU_BEAMFORMING_FB |
494 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB;
495 	elem->phy_cap_info[6] |= c;
496 
497 	/* the maximum cap is 4 x 3, (Nr, Nc) = (3, 2) */
498 	elem->phy_cap_info[7] |= min_t(int, nss - 1, 2) << 3;
499 }
500 
501 static void
502 mt7915_gen_ppe_thresh(u8 *he_ppet, int nss)
503 {
504 	u8 i, ppet_bits, ppet_size, ru_bit_mask = 0x7; /* HE80 */
505 	u8 ppet16_ppet8_ru3_ru0[] = {0x1c, 0xc7, 0x71};
506 
507 	he_ppet[0] = FIELD_PREP(IEEE80211_PPE_THRES_NSS_MASK, nss - 1) |
508 		     FIELD_PREP(IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK,
509 				ru_bit_mask);
510 
511 	ppet_bits = IEEE80211_PPE_THRES_INFO_PPET_SIZE *
512 		    nss * hweight8(ru_bit_mask) * 2;
513 	ppet_size = DIV_ROUND_UP(ppet_bits, 8);
514 
515 	for (i = 0; i < ppet_size - 1; i++)
516 		he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3];
517 
518 	he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3] &
519 			 (0xff >> (8 - (ppet_bits - 1) % 8));
520 }
521 
522 static int
523 mt7915_init_he_caps(struct mt7915_phy *phy, enum nl80211_band band,
524 		    struct ieee80211_sband_iftype_data *data)
525 {
526 	int i, idx = 0, nss = hweight8(phy->mt76->chainmask);
527 	u16 mcs_map = 0;
528 
529 	for (i = 0; i < 8; i++) {
530 		if (i < nss)
531 			mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2));
532 		else
533 			mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2));
534 	}
535 
536 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
537 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
538 		struct ieee80211_he_cap_elem *he_cap_elem =
539 				&he_cap->he_cap_elem;
540 		struct ieee80211_he_mcs_nss_supp *he_mcs =
541 				&he_cap->he_mcs_nss_supp;
542 
543 		switch (i) {
544 		case NL80211_IFTYPE_STATION:
545 		case NL80211_IFTYPE_AP:
546 #ifdef CONFIG_MAC80211_MESH
547 		case NL80211_IFTYPE_MESH_POINT:
548 #endif
549 			break;
550 		default:
551 			continue;
552 		}
553 
554 		data[idx].types_mask = BIT(i);
555 		he_cap->has_he = true;
556 
557 		he_cap_elem->mac_cap_info[0] =
558 			IEEE80211_HE_MAC_CAP0_HTC_HE;
559 		he_cap_elem->mac_cap_info[3] =
560 			IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
561 			IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3;
562 		he_cap_elem->mac_cap_info[4] =
563 			IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
564 
565 		if (band == NL80211_BAND_2GHZ)
566 			he_cap_elem->phy_cap_info[0] =
567 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
568 		else if (band == NL80211_BAND_5GHZ)
569 			he_cap_elem->phy_cap_info[0] =
570 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
571 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
572 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
573 
574 		he_cap_elem->phy_cap_info[1] =
575 			IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD;
576 		he_cap_elem->phy_cap_info[2] =
577 			IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
578 			IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ;
579 
580 		switch (i) {
581 		case NL80211_IFTYPE_AP:
582 			he_cap_elem->mac_cap_info[0] |=
583 				IEEE80211_HE_MAC_CAP0_TWT_RES;
584 			he_cap_elem->mac_cap_info[2] |=
585 				IEEE80211_HE_MAC_CAP2_BSR;
586 			he_cap_elem->mac_cap_info[4] |=
587 				IEEE80211_HE_MAC_CAP4_BQR;
588 			he_cap_elem->mac_cap_info[5] |=
589 				IEEE80211_HE_MAC_CAP5_OM_CTRL_UL_MU_DATA_DIS_RX;
590 			he_cap_elem->phy_cap_info[3] |=
591 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
592 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
593 			he_cap_elem->phy_cap_info[6] |=
594 				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
595 			break;
596 		case NL80211_IFTYPE_STATION:
597 			he_cap_elem->mac_cap_info[0] |=
598 				IEEE80211_HE_MAC_CAP0_TWT_REQ;
599 			he_cap_elem->mac_cap_info[1] |=
600 				IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
601 
602 			if (band == NL80211_BAND_2GHZ)
603 				he_cap_elem->phy_cap_info[0] |=
604 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G;
605 			else if (band == NL80211_BAND_5GHZ)
606 				he_cap_elem->phy_cap_info[0] |=
607 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G;
608 
609 			he_cap_elem->phy_cap_info[1] |=
610 				IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
611 				IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
612 			he_cap_elem->phy_cap_info[3] |=
613 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
614 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
615 			he_cap_elem->phy_cap_info[6] |=
616 				IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
617 				IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
618 				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
619 			he_cap_elem->phy_cap_info[7] |=
620 				IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
621 				IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI;
622 			he_cap_elem->phy_cap_info[8] |=
623 				IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
624 				IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
625 				IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU |
626 				IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484;
627 			he_cap_elem->phy_cap_info[9] |=
628 				IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
629 				IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
630 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
631 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
632 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
633 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
634 			break;
635 		}
636 
637 		he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map);
638 		he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map);
639 		he_mcs->rx_mcs_160 = cpu_to_le16(mcs_map);
640 		he_mcs->tx_mcs_160 = cpu_to_le16(mcs_map);
641 		he_mcs->rx_mcs_80p80 = cpu_to_le16(mcs_map);
642 		he_mcs->tx_mcs_80p80 = cpu_to_le16(mcs_map);
643 
644 		mt7915_set_stream_he_txbf_caps(he_cap, i, nss);
645 
646 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
647 		if (he_cap_elem->phy_cap_info[6] &
648 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
649 			mt7915_gen_ppe_thresh(he_cap->ppe_thres, nss);
650 		} else {
651 			he_cap_elem->phy_cap_info[9] |=
652 				IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US;
653 		}
654 		idx++;
655 	}
656 
657 	return idx;
658 }
659 
660 void mt7915_set_stream_he_caps(struct mt7915_phy *phy)
661 {
662 	struct ieee80211_sband_iftype_data *data;
663 	struct ieee80211_supported_band *band;
664 	int n;
665 
666 	if (phy->mt76->cap.has_2ghz) {
667 		data = phy->iftype[NL80211_BAND_2GHZ];
668 		n = mt7915_init_he_caps(phy, NL80211_BAND_2GHZ, data);
669 
670 		band = &phy->mt76->sband_2g.sband;
671 		band->iftype_data = data;
672 		band->n_iftype_data = n;
673 	}
674 
675 	if (phy->mt76->cap.has_5ghz) {
676 		data = phy->iftype[NL80211_BAND_5GHZ];
677 		n = mt7915_init_he_caps(phy, NL80211_BAND_5GHZ, data);
678 
679 		band = &phy->mt76->sband_5g.sband;
680 		band->iftype_data = data;
681 		band->n_iftype_data = n;
682 	}
683 }
684 
685 static void mt7915_unregister_ext_phy(struct mt7915_dev *dev)
686 {
687 	struct mt7915_phy *phy = mt7915_ext_phy(dev);
688 	struct mt76_phy *mphy = dev->mt76.phy2;
689 
690 	if (!phy)
691 		return;
692 
693 	mt76_unregister_phy(mphy);
694 	ieee80211_free_hw(mphy->hw);
695 }
696 
697 int mt7915_register_device(struct mt7915_dev *dev)
698 {
699 	struct ieee80211_hw *hw = mt76_hw(dev);
700 	int ret;
701 
702 	dev->phy.dev = dev;
703 	dev->phy.mt76 = &dev->mt76.phy;
704 	dev->mt76.phy.priv = &dev->phy;
705 	INIT_LIST_HEAD(&dev->phy.stats_list);
706 	INIT_WORK(&dev->rc_work, mt7915_mac_sta_rc_work);
707 	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt7915_mac_work);
708 	INIT_LIST_HEAD(&dev->sta_rc_list);
709 	INIT_LIST_HEAD(&dev->sta_poll_list);
710 	spin_lock_init(&dev->sta_poll_lock);
711 
712 	init_waitqueue_head(&dev->reset_wait);
713 	INIT_WORK(&dev->reset_work, mt7915_mac_reset_work);
714 
715 	ret = mt7915_init_hardware(dev);
716 	if (ret)
717 		return ret;
718 
719 	mt7915_init_wiphy(hw);
720 
721 	if (!dev->dbdc_support)
722 		dev->mphy.sband_5g.sband.vht_cap.cap |=
723 			IEEE80211_VHT_CAP_SHORT_GI_160 |
724 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
725 
726 	dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
727 	dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
728 	dev->phy.dfs_state = -1;
729 
730 #ifdef CONFIG_NL80211_TESTMODE
731 	dev->mt76.test_ops = &mt7915_testmode_ops;
732 #endif
733 
734 	ret = mt76_register_device(&dev->mt76, true, mt7915_rates,
735 				   ARRAY_SIZE(mt7915_rates));
736 	if (ret)
737 		return ret;
738 
739 	ieee80211_queue_work(mt76_hw(dev), &dev->init_work);
740 
741 	ret = mt7915_register_ext_phy(dev);
742 	if (ret)
743 		return ret;
744 
745 	return mt7915_init_debugfs(dev);
746 }
747 
748 void mt7915_unregister_device(struct mt7915_dev *dev)
749 {
750 	mt7915_unregister_ext_phy(dev);
751 	mt76_unregister_device(&dev->mt76);
752 	mt7915_mcu_exit(dev);
753 	mt7915_tx_token_put(dev);
754 	mt7915_dma_cleanup(dev);
755 
756 	mt76_free_device(&dev->mt76);
757 }
758