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