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 "coredump.h"
12 #include "eeprom.h"
13 
14 static const struct ieee80211_iface_limit if_limits[] = {
15 	{
16 		.max = 1,
17 		.types = BIT(NL80211_IFTYPE_ADHOC)
18 	}, {
19 		.max = 16,
20 		.types = BIT(NL80211_IFTYPE_AP)
21 #ifdef CONFIG_MAC80211_MESH
22 			 | BIT(NL80211_IFTYPE_MESH_POINT)
23 #endif
24 	}, {
25 		.max = MT7915_MAX_INTERFACES,
26 		.types = BIT(NL80211_IFTYPE_STATION)
27 	}
28 };
29 
30 static const struct ieee80211_iface_combination if_comb[] = {
31 	{
32 		.limits = if_limits,
33 		.n_limits = ARRAY_SIZE(if_limits),
34 		.max_interfaces = MT7915_MAX_INTERFACES,
35 		.num_different_channels = 1,
36 		.beacon_int_infra_match = true,
37 		.radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
38 				       BIT(NL80211_CHAN_WIDTH_20) |
39 				       BIT(NL80211_CHAN_WIDTH_40) |
40 				       BIT(NL80211_CHAN_WIDTH_80) |
41 				       BIT(NL80211_CHAN_WIDTH_160) |
42 				       BIT(NL80211_CHAN_WIDTH_80P80),
43 	}
44 };
45 
46 static ssize_t mt7915_thermal_temp_show(struct device *dev,
47 					struct device_attribute *attr,
48 					char *buf)
49 {
50 	struct mt7915_phy *phy = dev_get_drvdata(dev);
51 	int i = to_sensor_dev_attr(attr)->index;
52 	int temperature;
53 
54 	switch (i) {
55 	case 0:
56 		temperature = mt7915_mcu_get_temperature(phy);
57 		if (temperature < 0)
58 			return temperature;
59 		/* display in millidegree celcius */
60 		return sprintf(buf, "%u\n", temperature * 1000);
61 	case 1:
62 	case 2:
63 		return sprintf(buf, "%u\n",
64 			       phy->throttle_temp[i - 1] * 1000);
65 	case 3:
66 		return sprintf(buf, "%hhu\n", phy->throttle_state);
67 	default:
68 		return -EINVAL;
69 	}
70 }
71 
72 static ssize_t mt7915_thermal_temp_store(struct device *dev,
73 					 struct device_attribute *attr,
74 					 const char *buf, size_t count)
75 {
76 	struct mt7915_phy *phy = dev_get_drvdata(dev);
77 	int ret, i = to_sensor_dev_attr(attr)->index;
78 	long val;
79 
80 	ret = kstrtol(buf, 10, &val);
81 	if (ret < 0)
82 		return ret;
83 
84 	mutex_lock(&phy->dev->mt76.mutex);
85 	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 60, 130);
86 	phy->throttle_temp[i - 1] = val;
87 	mutex_unlock(&phy->dev->mt76.mutex);
88 
89 	return count;
90 }
91 
92 static SENSOR_DEVICE_ATTR_RO(temp1_input, mt7915_thermal_temp, 0);
93 static SENSOR_DEVICE_ATTR_RW(temp1_crit, mt7915_thermal_temp, 1);
94 static SENSOR_DEVICE_ATTR_RW(temp1_max, mt7915_thermal_temp, 2);
95 static SENSOR_DEVICE_ATTR_RO(throttle1, mt7915_thermal_temp, 3);
96 
97 static struct attribute *mt7915_hwmon_attrs[] = {
98 	&sensor_dev_attr_temp1_input.dev_attr.attr,
99 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
100 	&sensor_dev_attr_temp1_max.dev_attr.attr,
101 	&sensor_dev_attr_throttle1.dev_attr.attr,
102 	NULL,
103 };
104 ATTRIBUTE_GROUPS(mt7915_hwmon);
105 
106 static int
107 mt7915_thermal_get_max_throttle_state(struct thermal_cooling_device *cdev,
108 				      unsigned long *state)
109 {
110 	*state = MT7915_CDEV_THROTTLE_MAX;
111 
112 	return 0;
113 }
114 
115 static int
116 mt7915_thermal_get_cur_throttle_state(struct thermal_cooling_device *cdev,
117 				      unsigned long *state)
118 {
119 	struct mt7915_phy *phy = cdev->devdata;
120 
121 	*state = phy->cdev_state;
122 
123 	return 0;
124 }
125 
126 static int
127 mt7915_thermal_set_cur_throttle_state(struct thermal_cooling_device *cdev,
128 				      unsigned long state)
129 {
130 	struct mt7915_phy *phy = cdev->devdata;
131 	u8 throttling = MT7915_THERMAL_THROTTLE_MAX - state;
132 	int ret;
133 
134 	if (state > MT7915_CDEV_THROTTLE_MAX)
135 		return -EINVAL;
136 
137 	if (phy->throttle_temp[0] > phy->throttle_temp[1])
138 		return 0;
139 
140 	if (state == phy->cdev_state)
141 		return 0;
142 
143 	/*
144 	 * cooling_device convention: 0 = no cooling, more = more cooling
145 	 * mcu convention: 1 = max cooling, more = less cooling
146 	 */
147 	ret = mt7915_mcu_set_thermal_throttling(phy, throttling);
148 	if (ret)
149 		return ret;
150 
151 	phy->cdev_state = state;
152 
153 	return 0;
154 }
155 
156 static const struct thermal_cooling_device_ops mt7915_thermal_ops = {
157 	.get_max_state = mt7915_thermal_get_max_throttle_state,
158 	.get_cur_state = mt7915_thermal_get_cur_throttle_state,
159 	.set_cur_state = mt7915_thermal_set_cur_throttle_state,
160 };
161 
162 static void mt7915_unregister_thermal(struct mt7915_phy *phy)
163 {
164 	struct wiphy *wiphy = phy->mt76->hw->wiphy;
165 
166 	if (!phy->cdev)
167 	    return;
168 
169 	sysfs_remove_link(&wiphy->dev.kobj, "cooling_device");
170 	thermal_cooling_device_unregister(phy->cdev);
171 }
172 
173 static int mt7915_thermal_init(struct mt7915_phy *phy)
174 {
175 	struct wiphy *wiphy = phy->mt76->hw->wiphy;
176 	struct thermal_cooling_device *cdev;
177 	struct device *hwmon;
178 	const char *name;
179 
180 	name = devm_kasprintf(&wiphy->dev, GFP_KERNEL, "mt7915_%s",
181 			      wiphy_name(wiphy));
182 
183 	cdev = thermal_cooling_device_register(name, phy, &mt7915_thermal_ops);
184 	if (!IS_ERR(cdev)) {
185 		if (sysfs_create_link(&wiphy->dev.kobj, &cdev->device.kobj,
186 				      "cooling_device") < 0)
187 			thermal_cooling_device_unregister(cdev);
188 		else
189 			phy->cdev = cdev;
190 	}
191 
192 	if (!IS_REACHABLE(CONFIG_HWMON))
193 		return 0;
194 
195 	hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy,
196 						       mt7915_hwmon_groups);
197 	if (IS_ERR(hwmon))
198 		return PTR_ERR(hwmon);
199 
200 	/* initialize critical/maximum high temperature */
201 	phy->throttle_temp[0] = 110;
202 	phy->throttle_temp[1] = 120;
203 
204 	return mt7915_mcu_set_thermal_throttling(phy,
205 						 MT7915_THERMAL_THROTTLE_MAX);
206 }
207 
208 static void mt7915_led_set_config(struct led_classdev *led_cdev,
209 				  u8 delay_on, u8 delay_off)
210 {
211 	struct mt7915_dev *dev;
212 	struct mt76_dev *mt76;
213 	u32 val;
214 
215 	mt76 = container_of(led_cdev, struct mt76_dev, led_cdev);
216 	dev = container_of(mt76, struct mt7915_dev, mt76);
217 
218 	/* select TX blink mode, 2: only data frames */
219 	mt76_rmw_field(dev, MT_TMAC_TCR0(0), MT_TMAC_TCR0_TX_BLINK, 2);
220 
221 	/* enable LED */
222 	mt76_wr(dev, MT_LED_EN(0), 1);
223 
224 	/* set LED Tx blink on/off time */
225 	val = FIELD_PREP(MT_LED_TX_BLINK_ON_MASK, delay_on) |
226 	      FIELD_PREP(MT_LED_TX_BLINK_OFF_MASK, delay_off);
227 	mt76_wr(dev, MT_LED_TX_BLINK(0), val);
228 
229 	/* control LED */
230 	val = MT_LED_CTRL_BLINK_MODE | MT_LED_CTRL_KICK;
231 	if (dev->mt76.led_al)
232 		val |= MT_LED_CTRL_POLARITY;
233 
234 	mt76_wr(dev, MT_LED_CTRL(0), val);
235 	mt76_clear(dev, MT_LED_CTRL(0), MT_LED_CTRL_KICK);
236 }
237 
238 static int mt7915_led_set_blink(struct led_classdev *led_cdev,
239 				unsigned long *delay_on,
240 				unsigned long *delay_off)
241 {
242 	u16 delta_on = 0, delta_off = 0;
243 
244 #define HW_TICK		10
245 #define TO_HW_TICK(_t)	(((_t) > HW_TICK) ? ((_t) / HW_TICK) : HW_TICK)
246 
247 	if (*delay_on)
248 		delta_on = TO_HW_TICK(*delay_on);
249 	if (*delay_off)
250 		delta_off = TO_HW_TICK(*delay_off);
251 
252 	mt7915_led_set_config(led_cdev, delta_on, delta_off);
253 
254 	return 0;
255 }
256 
257 static void mt7915_led_set_brightness(struct led_classdev *led_cdev,
258 				      enum led_brightness brightness)
259 {
260 	if (!brightness)
261 		mt7915_led_set_config(led_cdev, 0, 0xff);
262 	else
263 		mt7915_led_set_config(led_cdev, 0xff, 0);
264 }
265 
266 void mt7915_init_txpower(struct mt7915_dev *dev,
267 			 struct ieee80211_supported_band *sband)
268 {
269 	int i, n_chains = hweight8(dev->mphy.antenna_mask);
270 	int nss_delta = mt76_tx_power_nss_delta(n_chains);
271 	int pwr_delta = mt7915_eeprom_get_power_delta(dev, sband->band);
272 	struct mt76_power_limits limits;
273 
274 	for (i = 0; i < sband->n_channels; i++) {
275 		struct ieee80211_channel *chan = &sband->channels[i];
276 		u32 target_power = 0;
277 		int j;
278 
279 		for (j = 0; j < n_chains; j++) {
280 			u32 val;
281 
282 			val = mt7915_eeprom_get_target_power(dev, chan, j);
283 			target_power = max(target_power, val);
284 		}
285 
286 		target_power += pwr_delta;
287 		target_power = mt76_get_rate_power_limits(&dev->mphy, chan,
288 							  &limits,
289 							  target_power);
290 		target_power += nss_delta;
291 		target_power = DIV_ROUND_UP(target_power, 2);
292 		chan->max_power = min_t(int, chan->max_reg_power,
293 					target_power);
294 		chan->orig_mpwr = target_power;
295 	}
296 }
297 
298 static void
299 mt7915_regd_notifier(struct wiphy *wiphy,
300 		     struct regulatory_request *request)
301 {
302 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
303 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
304 	struct mt76_phy *mphy = hw->priv;
305 	struct mt7915_phy *phy = mphy->priv;
306 
307 	memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2));
308 	dev->mt76.region = request->dfs_region;
309 
310 	if (dev->mt76.region == NL80211_DFS_UNSET)
311 		mt7915_mcu_rdd_background_enable(phy, NULL);
312 
313 	mt7915_init_txpower(dev, &mphy->sband_2g.sband);
314 	mt7915_init_txpower(dev, &mphy->sband_5g.sband);
315 	mt7915_init_txpower(dev, &mphy->sband_6g.sband);
316 
317 	mphy->dfs_state = MT_DFS_STATE_UNKNOWN;
318 	mt7915_dfs_init_radar_detector(phy);
319 }
320 
321 static void
322 mt7915_init_wiphy(struct ieee80211_hw *hw)
323 {
324 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
325 	struct mt76_dev *mdev = &phy->dev->mt76;
326 	struct wiphy *wiphy = hw->wiphy;
327 	struct mt7915_dev *dev = phy->dev;
328 
329 	hw->queues = 4;
330 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
331 	hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
332 	hw->netdev_features = NETIF_F_RXCSUM;
333 
334 	hw->radiotap_timestamp.units_pos =
335 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US;
336 
337 	phy->slottime = 9;
338 
339 	hw->sta_data_size = sizeof(struct mt7915_sta);
340 	hw->vif_data_size = sizeof(struct mt7915_vif);
341 
342 	wiphy->iface_combinations = if_comb;
343 	wiphy->n_iface_combinations = ARRAY_SIZE(if_comb);
344 	wiphy->reg_notifier = mt7915_regd_notifier;
345 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
346 	wiphy->mbssid_max_interfaces = 16;
347 
348 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
349 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
350 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
351 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_HT);
352 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_VHT);
353 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_HE);
354 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
355 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_FILS_DISCOVERY);
356 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
357 
358 	if (!is_mt7915(&dev->mt76))
359 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
360 
361 	if (!mdev->dev->of_node ||
362 	    !of_property_read_bool(mdev->dev->of_node,
363 				   "mediatek,disable-radar-background"))
364 		wiphy_ext_feature_set(wiphy,
365 				      NL80211_EXT_FEATURE_RADAR_BACKGROUND);
366 
367 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
368 	ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
369 	ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
370 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
371 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
372 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
373 
374 	hw->max_tx_fragments = 4;
375 
376 	if (phy->mt76->cap.has_2ghz) {
377 		phy->mt76->sband_2g.sband.ht_cap.cap |=
378 			IEEE80211_HT_CAP_LDPC_CODING |
379 			IEEE80211_HT_CAP_MAX_AMSDU;
380 		phy->mt76->sband_2g.sband.ht_cap.ampdu_density =
381 			IEEE80211_HT_MPDU_DENSITY_4;
382 	}
383 
384 	if (phy->mt76->cap.has_5ghz) {
385 		phy->mt76->sband_5g.sband.ht_cap.cap |=
386 			IEEE80211_HT_CAP_LDPC_CODING |
387 			IEEE80211_HT_CAP_MAX_AMSDU;
388 		phy->mt76->sband_5g.sband.ht_cap.ampdu_density =
389 			IEEE80211_HT_MPDU_DENSITY_4;
390 
391 		if (is_mt7915(&dev->mt76)) {
392 			phy->mt76->sband_5g.sband.vht_cap.cap |=
393 				IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991 |
394 				IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
395 
396 			if (!dev->dbdc_support)
397 				phy->mt76->sband_5g.sband.vht_cap.cap |=
398 					IEEE80211_VHT_CAP_SHORT_GI_160 |
399 					IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
400 		} else {
401 			phy->mt76->sband_5g.sband.vht_cap.cap |=
402 				IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
403 				IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
404 
405 			/* mt7916 dbdc with 2g 2x2 bw40 and 5g 2x2 bw160c */
406 			phy->mt76->sband_5g.sband.vht_cap.cap |=
407 				IEEE80211_VHT_CAP_SHORT_GI_160 |
408 				IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
409 		}
410 	}
411 
412 	mt76_set_stream_caps(phy->mt76, true);
413 	mt7915_set_stream_vht_txbf_caps(phy);
414 	mt7915_set_stream_he_caps(phy);
415 
416 	wiphy->available_antennas_rx = phy->mt76->antenna_mask;
417 	wiphy->available_antennas_tx = phy->mt76->antenna_mask;
418 }
419 
420 static void
421 mt7915_mac_init_band(struct mt7915_dev *dev, u8 band)
422 {
423 	u32 mask, set;
424 
425 	mt76_rmw_field(dev, MT_TMAC_CTCR0(band),
426 		       MT_TMAC_CTCR0_INS_DDLMT_REFTIME, 0x3f);
427 	mt76_set(dev, MT_TMAC_CTCR0(band),
428 		 MT_TMAC_CTCR0_INS_DDLMT_VHT_SMPDU_EN |
429 		 MT_TMAC_CTCR0_INS_DDLMT_EN);
430 
431 	mask = MT_MDP_RCFR0_MCU_RX_MGMT |
432 	       MT_MDP_RCFR0_MCU_RX_CTL_NON_BAR |
433 	       MT_MDP_RCFR0_MCU_RX_CTL_BAR;
434 	set = FIELD_PREP(MT_MDP_RCFR0_MCU_RX_MGMT, MT_MDP_TO_HIF) |
435 	      FIELD_PREP(MT_MDP_RCFR0_MCU_RX_CTL_NON_BAR, MT_MDP_TO_HIF) |
436 	      FIELD_PREP(MT_MDP_RCFR0_MCU_RX_CTL_BAR, MT_MDP_TO_HIF);
437 	mt76_rmw(dev, MT_MDP_BNRCFR0(band), mask, set);
438 
439 	mask = MT_MDP_RCFR1_MCU_RX_BYPASS |
440 	       MT_MDP_RCFR1_RX_DROPPED_UCAST |
441 	       MT_MDP_RCFR1_RX_DROPPED_MCAST;
442 	set = FIELD_PREP(MT_MDP_RCFR1_MCU_RX_BYPASS, MT_MDP_TO_HIF) |
443 	      FIELD_PREP(MT_MDP_RCFR1_RX_DROPPED_UCAST, MT_MDP_TO_HIF) |
444 	      FIELD_PREP(MT_MDP_RCFR1_RX_DROPPED_MCAST, MT_MDP_TO_HIF);
445 	mt76_rmw(dev, MT_MDP_BNRCFR1(band), mask, set);
446 
447 	mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_MAX_RX_LEN, 0x680);
448 
449 	/* mt7915: disable rx rate report by default due to hw issues */
450 	mt76_clear(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN);
451 
452 	/* clear estimated value of EIFS for Rx duration & OBSS time */
453 	mt76_wr(dev, MT_WF_RMAC_RSVD0(band), MT_WF_RMAC_RSVD0_EIFS_CLR);
454 
455 	/* clear backoff time for Rx duration  */
456 	mt76_clear(dev, MT_WF_RMAC_MIB_AIRTIME1(band),
457 		   MT_WF_RMAC_MIB_NONQOSD_BACKOFF);
458 	mt76_clear(dev, MT_WF_RMAC_MIB_AIRTIME3(band),
459 		   MT_WF_RMAC_MIB_QOS01_BACKOFF);
460 	mt76_clear(dev, MT_WF_RMAC_MIB_AIRTIME4(band),
461 		   MT_WF_RMAC_MIB_QOS23_BACKOFF);
462 
463 	/* clear backoff time and set software compensation for OBSS time */
464 	mask = MT_WF_RMAC_MIB_OBSS_BACKOFF | MT_WF_RMAC_MIB_ED_OFFSET;
465 	set = FIELD_PREP(MT_WF_RMAC_MIB_OBSS_BACKOFF, 0) |
466 	      FIELD_PREP(MT_WF_RMAC_MIB_ED_OFFSET, 4);
467 	mt76_rmw(dev, MT_WF_RMAC_MIB_AIRTIME0(band), mask, set);
468 
469 	/* filter out non-resp frames and get instanstaeous signal reporting */
470 	mask = MT_WTBLOFF_TOP_RSCR_RCPI_MODE | MT_WTBLOFF_TOP_RSCR_RCPI_PARAM;
471 	set = FIELD_PREP(MT_WTBLOFF_TOP_RSCR_RCPI_MODE, 0) |
472 	      FIELD_PREP(MT_WTBLOFF_TOP_RSCR_RCPI_PARAM, 0x3);
473 	mt76_rmw(dev, MT_WTBLOFF_TOP_RSCR(band), mask, set);
474 }
475 
476 void mt7915_mac_init(struct mt7915_dev *dev)
477 {
478 	int i;
479 	u32 rx_len = is_mt7915(&dev->mt76) ? 0x400 : 0x680;
480 
481 	/* config pse qid6 wfdma port selection */
482 	if (!is_mt7915(&dev->mt76) && dev->hif2)
483 		mt76_rmw(dev, MT_WF_PP_TOP_RXQ_WFDMA_CF_5, 0,
484 			 MT_WF_PP_TOP_RXQ_QID6_WFDMA_HIF_SEL_MASK);
485 
486 	mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, rx_len);
487 
488 	if (!is_mt7915(&dev->mt76))
489 		mt76_clear(dev, MT_MDP_DCR2, MT_MDP_DCR2_RX_TRANS_SHORT);
490 
491 	/* enable hardware de-agg */
492 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);
493 
494 	for (i = 0; i < mt7915_wtbl_size(dev); i++)
495 		mt7915_mac_wtbl_update(dev, i,
496 				       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
497 	for (i = 0; i < 2; i++)
498 		mt7915_mac_init_band(dev, i);
499 
500 	if (IS_ENABLED(CONFIG_MT76_LEDS)) {
501 		i = dev->mt76.led_pin ? MT_LED_GPIO_MUX3 : MT_LED_GPIO_MUX2;
502 		mt76_rmw_field(dev, i, MT_LED_GPIO_SEL_MASK, 4);
503 	}
504 }
505 
506 int mt7915_txbf_init(struct mt7915_dev *dev)
507 {
508 	int ret;
509 
510 	if (dev->dbdc_support) {
511 		ret = mt7915_mcu_set_txbf(dev, MT_BF_MODULE_UPDATE);
512 		if (ret)
513 			return ret;
514 	}
515 
516 	/* trigger sounding packets */
517 	ret = mt7915_mcu_set_txbf(dev, MT_BF_SOUNDING_ON);
518 	if (ret)
519 		return ret;
520 
521 	/* enable eBF */
522 	return mt7915_mcu_set_txbf(dev, MT_BF_TYPE_UPDATE);
523 }
524 
525 static struct mt7915_phy *
526 mt7915_alloc_ext_phy(struct mt7915_dev *dev)
527 {
528 	struct mt7915_phy *phy;
529 	struct mt76_phy *mphy;
530 
531 	if (!dev->dbdc_support)
532 		return NULL;
533 
534 	mphy = mt76_alloc_phy(&dev->mt76, sizeof(*phy), &mt7915_ops, MT_BAND1);
535 	if (!mphy)
536 		return ERR_PTR(-ENOMEM);
537 
538 	phy = mphy->priv;
539 	phy->dev = dev;
540 	phy->mt76 = mphy;
541 
542 	/* Bind main phy to band0 and ext_phy to band1 for dbdc case */
543 	phy->mt76->band_idx = 1;
544 
545 	return phy;
546 }
547 
548 static int
549 mt7915_register_ext_phy(struct mt7915_dev *dev, struct mt7915_phy *phy)
550 {
551 	struct mt76_phy *mphy = phy->mt76;
552 	int ret;
553 
554 	INIT_DELAYED_WORK(&mphy->mac_work, mt7915_mac_work);
555 
556 	mt7915_eeprom_parse_hw_cap(dev, phy);
557 
558 	memcpy(mphy->macaddr, dev->mt76.eeprom.data + MT_EE_MAC_ADDR2,
559 	       ETH_ALEN);
560 	/* Make the secondary PHY MAC address local without overlapping with
561 	 * the usual MAC address allocation scheme on multiple virtual interfaces
562 	 */
563 	if (!is_valid_ether_addr(mphy->macaddr)) {
564 		memcpy(mphy->macaddr, dev->mt76.eeprom.data + MT_EE_MAC_ADDR,
565 		       ETH_ALEN);
566 		mphy->macaddr[0] |= 2;
567 		mphy->macaddr[0] ^= BIT(7);
568 	}
569 	mt76_eeprom_override(mphy);
570 
571 	/* init wiphy according to mphy and phy */
572 	mt7915_init_wiphy(mphy->hw);
573 
574 	ret = mt76_register_phy(mphy, true, mt76_rates,
575 				ARRAY_SIZE(mt76_rates));
576 	if (ret)
577 		return ret;
578 
579 	ret = mt7915_thermal_init(phy);
580 	if (ret)
581 		goto unreg;
582 
583 	mt7915_init_debugfs(phy);
584 
585 	return 0;
586 
587 unreg:
588 	mt76_unregister_phy(mphy);
589 	return ret;
590 }
591 
592 static void mt7915_init_work(struct work_struct *work)
593 {
594 	struct mt7915_dev *dev = container_of(work, struct mt7915_dev,
595 				 init_work);
596 
597 	mt7915_mcu_set_eeprom(dev);
598 	mt7915_mac_init(dev);
599 	mt7915_init_txpower(dev, &dev->mphy.sband_2g.sband);
600 	mt7915_init_txpower(dev, &dev->mphy.sband_5g.sband);
601 	mt7915_init_txpower(dev, &dev->mphy.sband_6g.sband);
602 	mt7915_txbf_init(dev);
603 }
604 
605 void mt7915_wfsys_reset(struct mt7915_dev *dev)
606 {
607 #define MT_MCU_DUMMY_RANDOM	GENMASK(15, 0)
608 #define MT_MCU_DUMMY_DEFAULT	GENMASK(31, 16)
609 
610 	if (is_mt7915(&dev->mt76)) {
611 		u32 val = MT_TOP_PWR_KEY | MT_TOP_PWR_SW_PWR_ON | MT_TOP_PWR_PWR_ON;
612 
613 		mt76_wr(dev, MT_MCU_WFDMA0_DUMMY_CR, MT_MCU_DUMMY_RANDOM);
614 
615 		/* change to software control */
616 		val |= MT_TOP_PWR_SW_RST;
617 		mt76_wr(dev, MT_TOP_PWR_CTRL, val);
618 
619 		/* reset wfsys */
620 		val &= ~MT_TOP_PWR_SW_RST;
621 		mt76_wr(dev, MT_TOP_PWR_CTRL, val);
622 
623 		/* release wfsys then mcu re-executes romcode */
624 		val |= MT_TOP_PWR_SW_RST;
625 		mt76_wr(dev, MT_TOP_PWR_CTRL, val);
626 
627 		/* switch to hw control */
628 		val &= ~MT_TOP_PWR_SW_RST;
629 		val |= MT_TOP_PWR_HW_CTRL;
630 		mt76_wr(dev, MT_TOP_PWR_CTRL, val);
631 
632 		/* check whether mcu resets to default */
633 		if (!mt76_poll_msec(dev, MT_MCU_WFDMA0_DUMMY_CR,
634 				    MT_MCU_DUMMY_DEFAULT, MT_MCU_DUMMY_DEFAULT,
635 				    1000)) {
636 			dev_err(dev->mt76.dev, "wifi subsystem reset failure\n");
637 			return;
638 		}
639 
640 		/* wfsys reset won't clear host registers */
641 		mt76_clear(dev, MT_TOP_MISC, MT_TOP_MISC_FW_STATE);
642 
643 		msleep(100);
644 	} else if (is_mt7986(&dev->mt76)) {
645 		mt7986_wmac_disable(dev);
646 		msleep(20);
647 
648 		mt7986_wmac_enable(dev);
649 		msleep(20);
650 	} else {
651 		mt76_set(dev, MT_WF_SUBSYS_RST, 0x1);
652 		msleep(20);
653 
654 		mt76_clear(dev, MT_WF_SUBSYS_RST, 0x1);
655 		msleep(20);
656 	}
657 }
658 
659 static bool mt7915_band_config(struct mt7915_dev *dev)
660 {
661 	bool ret = true;
662 
663 	dev->phy.mt76->band_idx = 0;
664 
665 	if (is_mt7986(&dev->mt76)) {
666 		u32 sku = mt7915_check_adie(dev, true);
667 
668 		/*
669 		 * for mt7986, dbdc support is determined by the number
670 		 * of adie chips and the main phy is bound to band1 when
671 		 * dbdc is disabled.
672 		 */
673 		if (sku == MT7975_ONE_ADIE || sku == MT7976_ONE_ADIE) {
674 			dev->phy.mt76->band_idx = 1;
675 			ret = false;
676 		}
677 	} else {
678 		ret = is_mt7915(&dev->mt76) ?
679 		      !!(mt76_rr(dev, MT_HW_BOUND) & BIT(5)) : true;
680 	}
681 
682 	return ret;
683 }
684 
685 static int
686 mt7915_init_hardware(struct mt7915_dev *dev, struct mt7915_phy *phy2)
687 {
688 	int ret, idx;
689 
690 	mt76_wr(dev, MT_INT_MASK_CSR, 0);
691 	mt76_wr(dev, MT_INT_SOURCE_CSR, ~0);
692 
693 	INIT_WORK(&dev->init_work, mt7915_init_work);
694 
695 	ret = mt7915_dma_init(dev, phy2);
696 	if (ret)
697 		return ret;
698 
699 	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
700 
701 	ret = mt7915_mcu_init(dev);
702 	if (ret)
703 		return ret;
704 
705 	ret = mt7915_eeprom_init(dev);
706 	if (ret < 0)
707 		return ret;
708 
709 	if (dev->flash_mode) {
710 		ret = mt7915_mcu_apply_group_cal(dev);
711 		if (ret)
712 			return ret;
713 	}
714 
715 	/* Beacon and mgmt frames should occupy wcid 0 */
716 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA);
717 	if (idx)
718 		return -ENOSPC;
719 
720 	dev->mt76.global_wcid.idx = idx;
721 	dev->mt76.global_wcid.hw_key_idx = -1;
722 	dev->mt76.global_wcid.tx_info |= MT_WCID_TX_INFO_SET;
723 	rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid);
724 
725 	return 0;
726 }
727 
728 void mt7915_set_stream_vht_txbf_caps(struct mt7915_phy *phy)
729 {
730 	int sts;
731 	u32 *cap;
732 
733 	if (!phy->mt76->cap.has_5ghz)
734 		return;
735 
736 	sts = hweight8(phy->mt76->chainmask);
737 	cap = &phy->mt76->sband_5g.sband.vht_cap.cap;
738 
739 	*cap |= IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
740 		IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
741 		FIELD_PREP(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK,
742 			   sts - 1);
743 
744 	*cap &= ~(IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK |
745 		  IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
746 		  IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
747 
748 	if (sts < 2)
749 		return;
750 
751 	*cap |= IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
752 		IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE |
753 		FIELD_PREP(IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK,
754 			   sts - 1);
755 }
756 
757 static void
758 mt7915_set_stream_he_txbf_caps(struct mt7915_phy *phy,
759 			       struct ieee80211_sta_he_cap *he_cap, int vif)
760 {
761 	struct mt7915_dev *dev = phy->dev;
762 	struct ieee80211_he_cap_elem *elem = &he_cap->he_cap_elem;
763 	int sts = hweight8(phy->mt76->chainmask);
764 	u8 c, sts_160 = sts;
765 
766 	/* Can do 1/2 of STS in 160Mhz mode for mt7915 */
767 	if (is_mt7915(&dev->mt76)) {
768 		if (!dev->dbdc_support)
769 			sts_160 /= 2;
770 		else
771 			sts_160 = 0;
772 	}
773 
774 #ifdef CONFIG_MAC80211_MESH
775 	if (vif == NL80211_IFTYPE_MESH_POINT)
776 		return;
777 #endif
778 
779 	elem->phy_cap_info[3] &= ~IEEE80211_HE_PHY_CAP3_SU_BEAMFORMER;
780 	elem->phy_cap_info[4] &= ~IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
781 
782 	c = IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
783 	if (sts_160)
784 		c |= IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_ABOVE_80MHZ_MASK;
785 	elem->phy_cap_info[5] &= ~c;
786 
787 	c = IEEE80211_HE_PHY_CAP6_TRIG_SU_BEAMFORMING_FB |
788 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB;
789 	elem->phy_cap_info[6] &= ~c;
790 
791 	elem->phy_cap_info[7] &= ~IEEE80211_HE_PHY_CAP7_MAX_NC_MASK;
792 
793 	c = IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US;
794 	if (!is_mt7915(&dev->mt76))
795 		c |= IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
796 		     IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
797 	elem->phy_cap_info[2] |= c;
798 
799 	c = IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE |
800 	    IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4;
801 	if (sts_160)
802 		c |= IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_4;
803 	elem->phy_cap_info[4] |= c;
804 
805 	/* do not support NG16 due to spec D4.0 changes subcarrier idx */
806 	c = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU |
807 	    IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU;
808 
809 	if (vif == NL80211_IFTYPE_STATION)
810 		c |= IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
811 
812 	elem->phy_cap_info[6] |= c;
813 
814 	if (sts < 2)
815 		return;
816 
817 	/* the maximum cap is 4 x 3, (Nr, Nc) = (3, 2) */
818 	elem->phy_cap_info[7] |= min_t(int, sts - 1, 2) << 3;
819 
820 	if (vif != NL80211_IFTYPE_AP)
821 		return;
822 
823 	elem->phy_cap_info[3] |= IEEE80211_HE_PHY_CAP3_SU_BEAMFORMER;
824 	elem->phy_cap_info[4] |= IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
825 
826 	/* num_snd_dim
827 	 * for mt7915, max supported sts is 2 for bw > 80MHz and 0 if dbdc
828 	 */
829 	c = FIELD_PREP(IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK,
830 		       sts - 1);
831 	if (sts_160)
832 		c |= FIELD_PREP(IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_ABOVE_80MHZ_MASK,
833 				sts_160 - 1);
834 	elem->phy_cap_info[5] |= c;
835 
836 	c = IEEE80211_HE_PHY_CAP6_TRIG_SU_BEAMFORMING_FB |
837 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB;
838 	elem->phy_cap_info[6] |= c;
839 
840 	if (!is_mt7915(&dev->mt76)) {
841 		c = IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
842 		    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
843 		elem->phy_cap_info[7] |= c;
844 	}
845 }
846 
847 static void
848 mt7915_gen_ppe_thresh(u8 *he_ppet, int nss)
849 {
850 	u8 i, ppet_bits, ppet_size, ru_bit_mask = 0x7; /* HE80 */
851 	static const u8 ppet16_ppet8_ru3_ru0[] = {0x1c, 0xc7, 0x71};
852 
853 	he_ppet[0] = FIELD_PREP(IEEE80211_PPE_THRES_NSS_MASK, nss - 1) |
854 		     FIELD_PREP(IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK,
855 				ru_bit_mask);
856 
857 	ppet_bits = IEEE80211_PPE_THRES_INFO_PPET_SIZE *
858 		    nss * hweight8(ru_bit_mask) * 2;
859 	ppet_size = DIV_ROUND_UP(ppet_bits, 8);
860 
861 	for (i = 0; i < ppet_size - 1; i++)
862 		he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3];
863 
864 	he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3] &
865 			 (0xff >> (8 - (ppet_bits - 1) % 8));
866 }
867 
868 static int
869 mt7915_init_he_caps(struct mt7915_phy *phy, enum nl80211_band band,
870 		    struct ieee80211_sband_iftype_data *data)
871 {
872 	struct mt7915_dev *dev = phy->dev;
873 	int i, idx = 0, nss = hweight8(phy->mt76->antenna_mask);
874 	u16 mcs_map = 0;
875 	u16 mcs_map_160 = 0;
876 	u8 nss_160;
877 
878 	if (!is_mt7915(&dev->mt76))
879 		nss_160 = nss;
880 	else if (!dev->dbdc_support)
881 		/* Can do 1/2 of NSS streams in 160Mhz mode for mt7915 */
882 		nss_160 = nss / 2;
883 	else
884 		/* Can't do 160MHz with mt7915 dbdc */
885 		nss_160 = 0;
886 
887 	for (i = 0; i < 8; i++) {
888 		if (i < nss)
889 			mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2));
890 		else
891 			mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2));
892 
893 		if (i < nss_160)
894 			mcs_map_160 |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2));
895 		else
896 			mcs_map_160 |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2));
897 	}
898 
899 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
900 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
901 		struct ieee80211_he_cap_elem *he_cap_elem =
902 				&he_cap->he_cap_elem;
903 		struct ieee80211_he_mcs_nss_supp *he_mcs =
904 				&he_cap->he_mcs_nss_supp;
905 
906 		switch (i) {
907 		case NL80211_IFTYPE_STATION:
908 		case NL80211_IFTYPE_AP:
909 #ifdef CONFIG_MAC80211_MESH
910 		case NL80211_IFTYPE_MESH_POINT:
911 #endif
912 			break;
913 		default:
914 			continue;
915 		}
916 
917 		data[idx].types_mask = BIT(i);
918 		he_cap->has_he = true;
919 
920 		he_cap_elem->mac_cap_info[0] =
921 			IEEE80211_HE_MAC_CAP0_HTC_HE;
922 		he_cap_elem->mac_cap_info[3] =
923 			IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
924 			IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3;
925 		he_cap_elem->mac_cap_info[4] =
926 			IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
927 
928 		if (band == NL80211_BAND_2GHZ)
929 			he_cap_elem->phy_cap_info[0] =
930 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
931 		else if (nss_160)
932 			he_cap_elem->phy_cap_info[0] =
933 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
934 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
935 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
936 		else
937 			he_cap_elem->phy_cap_info[0] =
938 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G;
939 
940 		he_cap_elem->phy_cap_info[1] =
941 			IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD;
942 		he_cap_elem->phy_cap_info[2] =
943 			IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
944 			IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ;
945 
946 		switch (i) {
947 		case NL80211_IFTYPE_AP:
948 			he_cap_elem->mac_cap_info[0] |=
949 				IEEE80211_HE_MAC_CAP0_TWT_RES;
950 			he_cap_elem->mac_cap_info[2] |=
951 				IEEE80211_HE_MAC_CAP2_BSR;
952 			he_cap_elem->mac_cap_info[4] |=
953 				IEEE80211_HE_MAC_CAP4_BQR;
954 			he_cap_elem->mac_cap_info[5] |=
955 				IEEE80211_HE_MAC_CAP5_OM_CTRL_UL_MU_DATA_DIS_RX;
956 			he_cap_elem->phy_cap_info[3] |=
957 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
958 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
959 			he_cap_elem->phy_cap_info[6] |=
960 				IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
961 				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
962 			he_cap_elem->phy_cap_info[9] |=
963 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
964 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
965 			break;
966 		case NL80211_IFTYPE_STATION:
967 			he_cap_elem->mac_cap_info[1] |=
968 				IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
969 
970 			if (band == NL80211_BAND_2GHZ)
971 				he_cap_elem->phy_cap_info[0] |=
972 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G;
973 			else
974 				he_cap_elem->phy_cap_info[0] |=
975 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G;
976 
977 			he_cap_elem->phy_cap_info[1] |=
978 				IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
979 				IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
980 			he_cap_elem->phy_cap_info[3] |=
981 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
982 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
983 			he_cap_elem->phy_cap_info[6] |=
984 				IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
985 				IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
986 				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
987 			he_cap_elem->phy_cap_info[7] |=
988 				IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
989 				IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI;
990 			he_cap_elem->phy_cap_info[8] |=
991 				IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
992 				IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484;
993 			if (nss_160)
994 				he_cap_elem->phy_cap_info[8] |=
995 					IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
996 					IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
997 			he_cap_elem->phy_cap_info[9] |=
998 				IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
999 				IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
1000 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
1001 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
1002 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
1003 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
1004 			break;
1005 		}
1006 
1007 		he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map);
1008 		he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map);
1009 		he_mcs->rx_mcs_160 = cpu_to_le16(mcs_map_160);
1010 		he_mcs->tx_mcs_160 = cpu_to_le16(mcs_map_160);
1011 		he_mcs->rx_mcs_80p80 = cpu_to_le16(mcs_map_160);
1012 		he_mcs->tx_mcs_80p80 = cpu_to_le16(mcs_map_160);
1013 
1014 		mt7915_set_stream_he_txbf_caps(phy, he_cap, i);
1015 
1016 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
1017 		if (he_cap_elem->phy_cap_info[6] &
1018 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
1019 			mt7915_gen_ppe_thresh(he_cap->ppe_thres, nss);
1020 		} else {
1021 			he_cap_elem->phy_cap_info[9] |=
1022 				u8_encode_bits(IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US,
1023 					       IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
1024 		}
1025 
1026 		if (band == NL80211_BAND_6GHZ) {
1027 			u16 cap = IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
1028 				  IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
1029 
1030 			cap |= u16_encode_bits(IEEE80211_HT_MPDU_DENSITY_2,
1031 					       IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START) |
1032 			       u16_encode_bits(IEEE80211_VHT_MAX_AMPDU_1024K,
1033 					       IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP) |
1034 			       u16_encode_bits(IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454,
1035 					       IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
1036 
1037 			data[idx].he_6ghz_capa.capa = cpu_to_le16(cap);
1038 		}
1039 
1040 		idx++;
1041 	}
1042 
1043 	return idx;
1044 }
1045 
1046 void mt7915_set_stream_he_caps(struct mt7915_phy *phy)
1047 {
1048 	struct ieee80211_sband_iftype_data *data;
1049 	struct ieee80211_supported_band *band;
1050 	int n;
1051 
1052 	if (phy->mt76->cap.has_2ghz) {
1053 		data = phy->iftype[NL80211_BAND_2GHZ];
1054 		n = mt7915_init_he_caps(phy, NL80211_BAND_2GHZ, data);
1055 
1056 		band = &phy->mt76->sband_2g.sband;
1057 		band->iftype_data = data;
1058 		band->n_iftype_data = n;
1059 	}
1060 
1061 	if (phy->mt76->cap.has_5ghz) {
1062 		data = phy->iftype[NL80211_BAND_5GHZ];
1063 		n = mt7915_init_he_caps(phy, NL80211_BAND_5GHZ, data);
1064 
1065 		band = &phy->mt76->sband_5g.sband;
1066 		band->iftype_data = data;
1067 		band->n_iftype_data = n;
1068 	}
1069 
1070 	if (phy->mt76->cap.has_6ghz) {
1071 		data = phy->iftype[NL80211_BAND_6GHZ];
1072 		n = mt7915_init_he_caps(phy, NL80211_BAND_6GHZ, data);
1073 
1074 		band = &phy->mt76->sband_6g.sband;
1075 		band->iftype_data = data;
1076 		band->n_iftype_data = n;
1077 	}
1078 }
1079 
1080 static void mt7915_unregister_ext_phy(struct mt7915_dev *dev)
1081 {
1082 	struct mt7915_phy *phy = mt7915_ext_phy(dev);
1083 	struct mt76_phy *mphy = dev->mt76.phys[MT_BAND1];
1084 
1085 	if (!phy)
1086 		return;
1087 
1088 	mt7915_unregister_thermal(phy);
1089 	mt76_unregister_phy(mphy);
1090 	ieee80211_free_hw(mphy->hw);
1091 }
1092 
1093 static void mt7915_stop_hardware(struct mt7915_dev *dev)
1094 {
1095 	mt7915_mcu_exit(dev);
1096 	mt7915_tx_token_put(dev);
1097 	mt7915_dma_cleanup(dev);
1098 	tasklet_disable(&dev->irq_tasklet);
1099 
1100 	if (is_mt7986(&dev->mt76))
1101 		mt7986_wmac_disable(dev);
1102 }
1103 
1104 
1105 int mt7915_register_device(struct mt7915_dev *dev)
1106 {
1107 	struct ieee80211_hw *hw = mt76_hw(dev);
1108 	struct mt7915_phy *phy2;
1109 	int ret;
1110 
1111 	dev->phy.dev = dev;
1112 	dev->phy.mt76 = &dev->mt76.phy;
1113 	dev->mt76.phy.priv = &dev->phy;
1114 	INIT_WORK(&dev->rc_work, mt7915_mac_sta_rc_work);
1115 	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt7915_mac_work);
1116 	INIT_LIST_HEAD(&dev->sta_rc_list);
1117 	INIT_LIST_HEAD(&dev->sta_poll_list);
1118 	INIT_LIST_HEAD(&dev->twt_list);
1119 	spin_lock_init(&dev->sta_poll_lock);
1120 
1121 	init_waitqueue_head(&dev->reset_wait);
1122 	INIT_WORK(&dev->reset_work, mt7915_mac_reset_work);
1123 	INIT_WORK(&dev->dump_work, mt7915_mac_dump_work);
1124 	mutex_init(&dev->dump_mutex);
1125 
1126 	dev->dbdc_support = mt7915_band_config(dev);
1127 
1128 	phy2 = mt7915_alloc_ext_phy(dev);
1129 	if (IS_ERR(phy2))
1130 		return PTR_ERR(phy2);
1131 
1132 	ret = mt7915_init_hardware(dev, phy2);
1133 	if (ret)
1134 		goto free_phy2;
1135 
1136 	mt7915_init_wiphy(hw);
1137 
1138 #ifdef CONFIG_NL80211_TESTMODE
1139 	dev->mt76.test_ops = &mt7915_testmode_ops;
1140 #endif
1141 
1142 	/* init led callbacks */
1143 	if (IS_ENABLED(CONFIG_MT76_LEDS)) {
1144 		dev->mt76.led_cdev.brightness_set = mt7915_led_set_brightness;
1145 		dev->mt76.led_cdev.blink_set = mt7915_led_set_blink;
1146 	}
1147 
1148 	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
1149 				   ARRAY_SIZE(mt76_rates));
1150 	if (ret)
1151 		goto stop_hw;
1152 
1153 	ret = mt7915_thermal_init(&dev->phy);
1154 	if (ret)
1155 		goto unreg_dev;
1156 
1157 	ieee80211_queue_work(mt76_hw(dev), &dev->init_work);
1158 
1159 	if (phy2) {
1160 		ret = mt7915_register_ext_phy(dev, phy2);
1161 		if (ret)
1162 			goto unreg_thermal;
1163 	}
1164 
1165 	dev->recovery.hw_init_done = true;
1166 
1167 	ret = mt7915_init_debugfs(&dev->phy);
1168 	if (ret)
1169 		goto unreg_thermal;
1170 
1171 	ret = mt7915_coredump_register(dev);
1172 	if (ret)
1173 		goto unreg_thermal;
1174 
1175 	return 0;
1176 
1177 unreg_thermal:
1178 	mt7915_unregister_thermal(&dev->phy);
1179 unreg_dev:
1180 	mt76_unregister_device(&dev->mt76);
1181 stop_hw:
1182 	mt7915_stop_hardware(dev);
1183 free_phy2:
1184 	if (phy2)
1185 		ieee80211_free_hw(phy2->mt76->hw);
1186 	return ret;
1187 }
1188 
1189 void mt7915_unregister_device(struct mt7915_dev *dev)
1190 {
1191 	mt7915_unregister_ext_phy(dev);
1192 	mt7915_coredump_unregister(dev);
1193 	mt7915_unregister_thermal(&dev->phy);
1194 	mt76_unregister_device(&dev->mt76);
1195 	mt7915_stop_hardware(dev);
1196 
1197 	mt76_free_device(&dev->mt76);
1198 }
1199