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