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