xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/init.c (revision 975e122ddb7cd6f67bff974d2ea00c5568d2014c)
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 <linux/firmware.h>
9 #include "mt7921.h"
10 #include "../mt76_connac2_mac.h"
11 #include "mcu.h"
12 
13 static const struct ieee80211_iface_limit if_limits[] = {
14 	{
15 		.max = MT7921_MAX_INTERFACES,
16 		.types = BIT(NL80211_IFTYPE_STATION)
17 	},
18 	{
19 		.max = 1,
20 		.types = BIT(NL80211_IFTYPE_AP)
21 	}
22 };
23 
24 static const struct ieee80211_iface_combination if_comb[] = {
25 	{
26 		.limits = if_limits,
27 		.n_limits = ARRAY_SIZE(if_limits),
28 		.max_interfaces = MT7921_MAX_INTERFACES,
29 		.num_different_channels = 1,
30 		.beacon_int_infra_match = true,
31 	},
32 };
33 
34 static const struct ieee80211_iface_limit if_limits_chanctx[] = {
35 	{
36 		.max = 2,
37 		.types = BIT(NL80211_IFTYPE_STATION) |
38 			 BIT(NL80211_IFTYPE_P2P_CLIENT)
39 	},
40 	{
41 		.max = 1,
42 		.types = BIT(NL80211_IFTYPE_AP) |
43 			 BIT(NL80211_IFTYPE_P2P_GO)
44 	}
45 };
46 
47 static const struct ieee80211_iface_combination if_comb_chanctx[] = {
48 	{
49 		.limits = if_limits_chanctx,
50 		.n_limits = ARRAY_SIZE(if_limits_chanctx),
51 		.max_interfaces = 2,
52 		.num_different_channels = 2,
53 		.beacon_int_infra_match = false,
54 	}
55 };
56 
57 static ssize_t mt7921_thermal_temp_show(struct device *dev,
58 					struct device_attribute *attr,
59 					char *buf)
60 {
61 	switch (to_sensor_dev_attr(attr)->index) {
62 	case 0: {
63 		struct mt792x_phy *phy = dev_get_drvdata(dev);
64 		struct mt792x_dev *mdev = phy->dev;
65 		int temperature;
66 
67 		mt7921_mutex_acquire(mdev);
68 		temperature = mt7921_mcu_get_temperature(phy);
69 		mt7921_mutex_release(mdev);
70 
71 		if (temperature < 0)
72 			return temperature;
73 		/* display in millidegree Celsius */
74 		return sprintf(buf, "%u\n", temperature * 1000);
75 	}
76 	default:
77 		return -EINVAL;
78 	}
79 }
80 static SENSOR_DEVICE_ATTR_RO(temp1_input, mt7921_thermal_temp, 0);
81 
82 static struct attribute *mt7921_hwmon_attrs[] = {
83 	&sensor_dev_attr_temp1_input.dev_attr.attr,
84 	NULL,
85 };
86 ATTRIBUTE_GROUPS(mt7921_hwmon);
87 
88 static int mt7921_thermal_init(struct mt792x_phy *phy)
89 {
90 	struct wiphy *wiphy = phy->mt76->hw->wiphy;
91 	struct device *hwmon;
92 	const char *name;
93 
94 	if (!IS_REACHABLE(CONFIG_HWMON))
95 		return 0;
96 
97 	name = devm_kasprintf(&wiphy->dev, GFP_KERNEL, "mt7921_%s",
98 			      wiphy_name(wiphy));
99 
100 	hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy,
101 						       mt7921_hwmon_groups);
102 	if (IS_ERR(hwmon))
103 		return PTR_ERR(hwmon);
104 
105 	return 0;
106 }
107 
108 static void
109 mt7921_regd_notifier(struct wiphy *wiphy,
110 		     struct regulatory_request *request)
111 {
112 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
113 	struct mt792x_dev *dev = mt7921_hw_dev(hw);
114 
115 	memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2));
116 	dev->mt76.region = request->dfs_region;
117 	dev->country_ie_env = request->country_ie_env;
118 
119 	mt7921_mutex_acquire(dev);
120 	mt7921_mcu_set_clc(dev, request->alpha2, request->country_ie_env);
121 	mt76_connac_mcu_set_channel_domain(hw->priv);
122 	mt7921_set_tx_sar_pwr(hw, NULL);
123 	mt7921_mutex_release(dev);
124 }
125 
126 static int
127 mt7921_init_wiphy(struct ieee80211_hw *hw)
128 {
129 	struct mt792x_phy *phy = mt7921_hw_phy(hw);
130 	struct mt792x_dev *dev = phy->dev;
131 	struct wiphy *wiphy = hw->wiphy;
132 
133 	hw->queues = 4;
134 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
135 	hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
136 	hw->netdev_features = NETIF_F_RXCSUM;
137 
138 	hw->radiotap_timestamp.units_pos =
139 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US;
140 
141 	phy->slottime = 9;
142 
143 	hw->sta_data_size = sizeof(struct mt792x_sta);
144 	hw->vif_data_size = sizeof(struct mt792x_vif);
145 
146 	if (dev->fw_features & MT7921_FW_CAP_CNM) {
147 		wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
148 		wiphy->iface_combinations = if_comb_chanctx;
149 		wiphy->n_iface_combinations = ARRAY_SIZE(if_comb_chanctx);
150 	} else {
151 		wiphy->flags &= ~WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
152 		wiphy->iface_combinations = if_comb;
153 		wiphy->n_iface_combinations = ARRAY_SIZE(if_comb);
154 	}
155 	wiphy->flags &= ~(WIPHY_FLAG_IBSS_RSN | WIPHY_FLAG_4ADDR_AP |
156 			  WIPHY_FLAG_4ADDR_STATION);
157 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
158 				 BIT(NL80211_IFTYPE_AP) |
159 				 BIT(NL80211_IFTYPE_P2P_CLIENT) |
160 				 BIT(NL80211_IFTYPE_P2P_GO);
161 	wiphy->max_remain_on_channel_duration = 5000;
162 	wiphy->max_scan_ie_len = MT76_CONNAC_SCAN_IE_LEN;
163 	wiphy->max_scan_ssids = 4;
164 	wiphy->max_sched_scan_plan_interval =
165 		MT76_CONNAC_MAX_TIME_SCHED_SCAN_INTERVAL;
166 	wiphy->max_sched_scan_ie_len = IEEE80211_MAX_DATA_LEN;
167 	wiphy->max_sched_scan_ssids = MT76_CONNAC_MAX_SCHED_SCAN_SSID;
168 	wiphy->max_match_sets = MT76_CONNAC_MAX_SCAN_MATCH;
169 	wiphy->max_sched_scan_reqs = 1;
170 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH |
171 			WIPHY_FLAG_SPLIT_SCAN_6GHZ;
172 	wiphy->reg_notifier = mt7921_regd_notifier;
173 
174 	wiphy->features |= NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
175 			   NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
176 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_SET_SCAN_DWELL);
177 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
178 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_HT);
179 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_VHT);
180 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_HE);
181 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
182 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CAN_REPLACE_PTK0);
183 
184 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
185 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
186 	ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
187 	ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
188 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
189 	ieee80211_hw_set(hw, SUPPORTS_PS);
190 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
191 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
192 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
193 
194 	if (dev->pm.enable)
195 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
196 
197 	hw->max_tx_fragments = 4;
198 
199 	return 0;
200 }
201 
202 static void
203 mt7921_mac_init_band(struct mt792x_dev *dev, u8 band)
204 {
205 	u32 mask, set;
206 
207 	mt76_rmw_field(dev, MT_TMAC_CTCR0(band),
208 		       MT_TMAC_CTCR0_INS_DDLMT_REFTIME, 0x3f);
209 	mt76_set(dev, MT_TMAC_CTCR0(band),
210 		 MT_TMAC_CTCR0_INS_DDLMT_VHT_SMPDU_EN |
211 		 MT_TMAC_CTCR0_INS_DDLMT_EN);
212 
213 	mt76_set(dev, MT_WF_RMAC_MIB_TIME0(band), MT_WF_RMAC_MIB_RXTIME_EN);
214 	mt76_set(dev, MT_WF_RMAC_MIB_AIRTIME0(band), MT_WF_RMAC_MIB_RXTIME_EN);
215 
216 	/* enable MIB tx-rx time reporting */
217 	mt76_set(dev, MT_MIB_SCR1(band), MT_MIB_TXDUR_EN);
218 	mt76_set(dev, MT_MIB_SCR1(band), MT_MIB_RXDUR_EN);
219 
220 	mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_MAX_RX_LEN, 1536);
221 	/* disable rx rate report by default due to hw issues */
222 	mt76_clear(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN);
223 
224 	/* filter out non-resp frames and get instantaneous signal reporting */
225 	mask = MT_WTBLOFF_TOP_RSCR_RCPI_MODE | MT_WTBLOFF_TOP_RSCR_RCPI_PARAM;
226 	set = FIELD_PREP(MT_WTBLOFF_TOP_RSCR_RCPI_MODE, 0) |
227 	      FIELD_PREP(MT_WTBLOFF_TOP_RSCR_RCPI_PARAM, 0x3);
228 	mt76_rmw(dev, MT_WTBLOFF_TOP_RSCR(band), mask, set);
229 }
230 
231 static u8
232 mt7921_get_offload_capability(struct device *dev, const char *fw_wm)
233 {
234 	const struct mt76_connac2_fw_trailer *hdr;
235 	struct mt7921_realease_info *rel_info;
236 	const struct firmware *fw;
237 	int ret, i, offset = 0;
238 	const u8 *data, *end;
239 	u8 offload_caps = 0;
240 
241 	ret = request_firmware(&fw, fw_wm, dev);
242 	if (ret)
243 		return ret;
244 
245 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
246 		dev_err(dev, "Invalid firmware\n");
247 		goto out;
248 	}
249 
250 	data = fw->data;
251 	hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
252 
253 	for (i = 0; i < hdr->n_region; i++) {
254 		const struct mt76_connac2_fw_region *region;
255 
256 		region = (const void *)((const u8 *)hdr -
257 					(hdr->n_region - i) * sizeof(*region));
258 		offset += le32_to_cpu(region->len);
259 	}
260 
261 	data += offset + 16;
262 	rel_info = (struct mt7921_realease_info *)data;
263 	data += sizeof(*rel_info);
264 	end = data + le16_to_cpu(rel_info->len);
265 
266 	while (data < end) {
267 		rel_info = (struct mt7921_realease_info *)data;
268 		data += sizeof(*rel_info);
269 
270 		if (rel_info->tag == MT7921_FW_TAG_FEATURE) {
271 			struct mt7921_fw_features *features;
272 
273 			features = (struct mt7921_fw_features *)data;
274 			offload_caps = features->data;
275 			break;
276 		}
277 
278 		data += le16_to_cpu(rel_info->len) + rel_info->pad_len;
279 	}
280 
281 out:
282 	release_firmware(fw);
283 
284 	return offload_caps;
285 }
286 
287 struct ieee80211_ops *
288 mt7921_get_mac80211_ops(struct device *dev, void *drv_data, u8 *fw_features)
289 {
290 	struct ieee80211_ops *ops;
291 
292 	ops = devm_kmemdup(dev, &mt7921_ops, sizeof(mt7921_ops), GFP_KERNEL);
293 	if (!ops)
294 		return NULL;
295 
296 	*fw_features = mt7921_get_offload_capability(dev, drv_data);
297 	if (!(*fw_features & MT7921_FW_CAP_CNM)) {
298 		ops->remain_on_channel = NULL;
299 		ops->cancel_remain_on_channel = NULL;
300 		ops->add_chanctx = NULL;
301 		ops->remove_chanctx = NULL;
302 		ops->change_chanctx = NULL;
303 		ops->assign_vif_chanctx = NULL;
304 		ops->unassign_vif_chanctx = NULL;
305 		ops->mgd_prepare_tx = NULL;
306 		ops->mgd_complete_tx = NULL;
307 	}
308 	return ops;
309 }
310 EXPORT_SYMBOL_GPL(mt7921_get_mac80211_ops);
311 
312 int mt7921_mac_init(struct mt792x_dev *dev)
313 {
314 	int i;
315 
316 	mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);
317 	/* enable hardware de-agg */
318 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);
319 	/* enable hardware rx header translation */
320 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN);
321 
322 	for (i = 0; i < MT7921_WTBL_SIZE; i++)
323 		mt7921_mac_wtbl_update(dev, i,
324 				       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
325 	for (i = 0; i < 2; i++)
326 		mt7921_mac_init_band(dev, i);
327 
328 	return mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0);
329 }
330 EXPORT_SYMBOL_GPL(mt7921_mac_init);
331 
332 static int __mt7921_init_hardware(struct mt792x_dev *dev)
333 {
334 	int ret;
335 
336 	/* force firmware operation mode into normal state,
337 	 * which should be set before firmware download stage.
338 	 */
339 	mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE);
340 	ret = mt7921_mcu_init(dev);
341 	if (ret)
342 		goto out;
343 
344 	mt76_eeprom_override(&dev->mphy);
345 
346 	ret = mt7921_mcu_set_eeprom(dev);
347 	if (ret)
348 		goto out;
349 
350 	ret = mt7921_mac_init(dev);
351 out:
352 	return ret;
353 }
354 
355 static int mt7921_init_hardware(struct mt792x_dev *dev)
356 {
357 	int ret, i;
358 
359 	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
360 
361 	for (i = 0; i < MT7921_MCU_INIT_RETRY_COUNT; i++) {
362 		ret = __mt7921_init_hardware(dev);
363 		if (!ret)
364 			break;
365 
366 		mt7921_init_reset(dev);
367 	}
368 
369 	if (i == MT7921_MCU_INIT_RETRY_COUNT) {
370 		dev_err(dev->mt76.dev, "hardware init failed\n");
371 		return ret;
372 	}
373 
374 	return 0;
375 }
376 
377 static int mt7921_init_wcid(struct mt792x_dev *dev)
378 {
379 	int idx;
380 
381 	/* Beacon and mgmt frames should occupy wcid 0 */
382 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
383 	if (idx)
384 		return -ENOSPC;
385 
386 	dev->mt76.global_wcid.idx = idx;
387 	dev->mt76.global_wcid.hw_key_idx = -1;
388 	dev->mt76.global_wcid.tx_info |= MT_WCID_TX_INFO_SET;
389 	rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid);
390 
391 	return 0;
392 }
393 
394 static void mt7921_init_work(struct work_struct *work)
395 {
396 	struct mt792x_dev *dev = container_of(work, struct mt792x_dev,
397 					      init_work);
398 	int ret;
399 
400 	ret = mt7921_init_hardware(dev);
401 	if (ret)
402 		return;
403 
404 	mt76_set_stream_caps(&dev->mphy, true);
405 	mt7921_set_stream_he_caps(&dev->phy);
406 
407 	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
408 				   ARRAY_SIZE(mt76_rates));
409 	if (ret) {
410 		dev_err(dev->mt76.dev, "register device failed\n");
411 		return;
412 	}
413 
414 	ret = mt7921_init_debugfs(dev);
415 	if (ret) {
416 		dev_err(dev->mt76.dev, "register debugfs failed\n");
417 		return;
418 	}
419 
420 	ret = mt7921_thermal_init(&dev->phy);
421 	if (ret) {
422 		dev_err(dev->mt76.dev, "thermal init failed\n");
423 		return;
424 	}
425 
426 	/* we support chip reset now */
427 	dev->hw_init_done = true;
428 
429 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable);
430 }
431 
432 int mt7921_register_device(struct mt792x_dev *dev)
433 {
434 	struct ieee80211_hw *hw = mt76_hw(dev);
435 	int ret;
436 
437 	dev->phy.dev = dev;
438 	dev->phy.mt76 = &dev->mt76.phy;
439 	dev->mt76.phy.priv = &dev->phy;
440 	dev->mt76.tx_worker.fn = mt7921_tx_worker;
441 
442 	INIT_DELAYED_WORK(&dev->pm.ps_work, mt7921_pm_power_save_work);
443 	INIT_WORK(&dev->pm.wake_work, mt7921_pm_wake_work);
444 	spin_lock_init(&dev->pm.wake.lock);
445 	mutex_init(&dev->pm.mutex);
446 	init_waitqueue_head(&dev->pm.wait);
447 	if (mt76_is_sdio(&dev->mt76))
448 		init_waitqueue_head(&dev->mt76.sdio.wait);
449 	spin_lock_init(&dev->pm.txq_lock);
450 	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt7921_mac_work);
451 	INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work);
452 	INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work);
453 #if IS_ENABLED(CONFIG_IPV6)
454 	INIT_WORK(&dev->ipv6_ns_work, mt7921_set_ipv6_ns_work);
455 	skb_queue_head_init(&dev->ipv6_ns_list);
456 #endif
457 	skb_queue_head_init(&dev->phy.scan_event_list);
458 	skb_queue_head_init(&dev->coredump.msg_list);
459 
460 	INIT_WORK(&dev->reset_work, mt7921_mac_reset_work);
461 	INIT_WORK(&dev->init_work, mt7921_init_work);
462 
463 	INIT_WORK(&dev->phy.roc_work, mt7921_roc_work);
464 	timer_setup(&dev->phy.roc_timer, mt7921_roc_timer, 0);
465 	init_waitqueue_head(&dev->phy.roc_wait);
466 
467 	dev->pm.idle_timeout = MT7921_PM_TIMEOUT;
468 	dev->pm.stats.last_wake_event = jiffies;
469 	dev->pm.stats.last_doze_event = jiffies;
470 	if (!mt76_is_usb(&dev->mt76)) {
471 		dev->pm.enable_user = true;
472 		dev->pm.enable = true;
473 		dev->pm.ds_enable_user = true;
474 		dev->pm.ds_enable = true;
475 	}
476 
477 	if (!mt76_is_mmio(&dev->mt76))
478 		hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;
479 
480 	mt7921_init_acpi_sar(dev);
481 
482 	ret = mt7921_init_wcid(dev);
483 	if (ret)
484 		return ret;
485 
486 	ret = mt7921_init_wiphy(hw);
487 	if (ret)
488 		return ret;
489 
490 	dev->mphy.sband_2g.sband.ht_cap.cap |=
491 			IEEE80211_HT_CAP_LDPC_CODING |
492 			IEEE80211_HT_CAP_MAX_AMSDU;
493 	dev->mphy.sband_5g.sband.ht_cap.cap |=
494 			IEEE80211_HT_CAP_LDPC_CODING |
495 			IEEE80211_HT_CAP_MAX_AMSDU;
496 	dev->mphy.sband_5g.sband.vht_cap.cap |=
497 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
498 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
499 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
500 			IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
501 			(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
502 	if (is_mt7922(&dev->mt76))
503 		dev->mphy.sband_5g.sband.vht_cap.cap |=
504 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
505 			IEEE80211_VHT_CAP_SHORT_GI_160;
506 
507 	dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
508 	dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
509 
510 	queue_work(system_wq, &dev->init_work);
511 
512 	return 0;
513 }
514 EXPORT_SYMBOL_GPL(mt7921_register_device);
515