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