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