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 
124 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
125 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
126 	ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
127 	ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
128 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
129 	ieee80211_hw_set(hw, SUPPORTS_PS);
130 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
131 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
132 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
133 
134 	if (dev->pm.enable)
135 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
136 
137 	hw->max_tx_fragments = 4;
138 
139 	return 0;
140 }
141 
142 static void
143 mt7921_mac_init_band(struct mt7921_dev *dev, u8 band)
144 {
145 	mt76_rmw_field(dev, MT_TMAC_CTCR0(band),
146 		       MT_TMAC_CTCR0_INS_DDLMT_REFTIME, 0x3f);
147 	mt76_set(dev, MT_TMAC_CTCR0(band),
148 		 MT_TMAC_CTCR0_INS_DDLMT_VHT_SMPDU_EN |
149 		 MT_TMAC_CTCR0_INS_DDLMT_EN);
150 
151 	mt76_set(dev, MT_WF_RMAC_MIB_TIME0(band), MT_WF_RMAC_MIB_RXTIME_EN);
152 	mt76_set(dev, MT_WF_RMAC_MIB_AIRTIME0(band), MT_WF_RMAC_MIB_RXTIME_EN);
153 
154 	/* enable MIB tx-rx time reporting */
155 	mt76_set(dev, MT_MIB_SCR1(band), MT_MIB_TXDUR_EN);
156 	mt76_set(dev, MT_MIB_SCR1(band), MT_MIB_RXDUR_EN);
157 
158 	mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_MAX_RX_LEN, 1536);
159 	/* disable rx rate report by default due to hw issues */
160 	mt76_clear(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN);
161 }
162 
163 u8 mt7921_check_offload_capability(struct device *dev, const char *fw_wm)
164 {
165 	struct mt7921_fw_features *features = NULL;
166 	const struct mt76_connac2_fw_trailer *hdr;
167 	struct mt7921_realease_info *rel_info;
168 	const struct firmware *fw;
169 	int ret, i, offset = 0;
170 	const u8 *data, *end;
171 
172 	ret = request_firmware(&fw, fw_wm, dev);
173 	if (ret)
174 		return ret;
175 
176 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
177 		dev_err(dev, "Invalid firmware\n");
178 		return -EINVAL;
179 	}
180 
181 	data = fw->data;
182 	hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
183 
184 	for (i = 0; i < hdr->n_region; i++) {
185 		const struct mt76_connac2_fw_region *region;
186 
187 		region = (const void *)((const u8 *)hdr -
188 					(hdr->n_region - i) * sizeof(*region));
189 		offset += le32_to_cpu(region->len);
190 	}
191 
192 	data += offset + 16;
193 	rel_info = (struct mt7921_realease_info *)data;
194 	data += sizeof(*rel_info);
195 	end = data + le16_to_cpu(rel_info->len);
196 
197 	while (data < end) {
198 		rel_info = (struct mt7921_realease_info *)data;
199 		data += sizeof(*rel_info);
200 
201 		if (rel_info->tag == MT7921_FW_TAG_FEATURE) {
202 			features = (struct mt7921_fw_features *)data;
203 			break;
204 		}
205 
206 		data += le16_to_cpu(rel_info->len) + rel_info->pad_len;
207 	}
208 
209 	release_firmware(fw);
210 
211 	return features ? features->data : 0;
212 }
213 EXPORT_SYMBOL_GPL(mt7921_check_offload_capability);
214 
215 int mt7921_mac_init(struct mt7921_dev *dev)
216 {
217 	int i;
218 
219 	mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);
220 	/* enable hardware de-agg */
221 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);
222 	/* enable hardware rx header translation */
223 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN);
224 
225 	for (i = 0; i < MT7921_WTBL_SIZE; i++)
226 		mt7921_mac_wtbl_update(dev, i,
227 				       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
228 	for (i = 0; i < 2; i++)
229 		mt7921_mac_init_band(dev, i);
230 
231 	dev->mt76.rxfilter = mt76_rr(dev, MT_WF_RFCR(0));
232 
233 	return mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0);
234 }
235 EXPORT_SYMBOL_GPL(mt7921_mac_init);
236 
237 static int __mt7921_init_hardware(struct mt7921_dev *dev)
238 {
239 	int ret;
240 
241 	/* force firmware operation mode into normal state,
242 	 * which should be set before firmware download stage.
243 	 */
244 	mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE);
245 	ret = mt7921_mcu_init(dev);
246 	if (ret)
247 		goto out;
248 
249 	mt76_eeprom_override(&dev->mphy);
250 
251 	ret = mt7921_mcu_set_eeprom(dev);
252 	if (ret)
253 		goto out;
254 
255 	ret = mt7921_mac_init(dev);
256 out:
257 	return ret;
258 }
259 
260 static int mt7921_init_hardware(struct mt7921_dev *dev)
261 {
262 	int ret, i;
263 
264 	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
265 
266 	for (i = 0; i < MT7921_MCU_INIT_RETRY_COUNT; i++) {
267 		ret = __mt7921_init_hardware(dev);
268 		if (!ret)
269 			break;
270 
271 		mt7921_init_reset(dev);
272 	}
273 
274 	if (i == MT7921_MCU_INIT_RETRY_COUNT) {
275 		dev_err(dev->mt76.dev, "hardware init failed\n");
276 		return ret;
277 	}
278 
279 	return 0;
280 }
281 
282 static int mt7921_init_wcid(struct mt7921_dev *dev)
283 {
284 	int idx;
285 
286 	/* Beacon and mgmt frames should occupy wcid 0 */
287 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
288 	if (idx)
289 		return -ENOSPC;
290 
291 	dev->mt76.global_wcid.idx = idx;
292 	dev->mt76.global_wcid.hw_key_idx = -1;
293 	dev->mt76.global_wcid.tx_info |= MT_WCID_TX_INFO_SET;
294 	rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid);
295 
296 	return 0;
297 }
298 
299 static void mt7921_init_work(struct work_struct *work)
300 {
301 	struct mt7921_dev *dev = container_of(work, struct mt7921_dev,
302 					      init_work);
303 	int ret;
304 
305 	ret = mt7921_init_hardware(dev);
306 	if (ret)
307 		return;
308 
309 	mt76_set_stream_caps(&dev->mphy, true);
310 	mt7921_set_stream_he_caps(&dev->phy);
311 
312 	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
313 				   ARRAY_SIZE(mt76_rates));
314 	if (ret) {
315 		dev_err(dev->mt76.dev, "register device failed\n");
316 		return;
317 	}
318 
319 	ret = mt7921_init_debugfs(dev);
320 	if (ret) {
321 		dev_err(dev->mt76.dev, "register debugfs failed\n");
322 		return;
323 	}
324 
325 	/* we support chip reset now */
326 	dev->hw_init_done = true;
327 
328 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable);
329 }
330 
331 int mt7921_register_device(struct mt7921_dev *dev)
332 {
333 	struct ieee80211_hw *hw = mt76_hw(dev);
334 	int ret;
335 
336 	dev->phy.dev = dev;
337 	dev->phy.mt76 = &dev->mt76.phy;
338 	dev->mt76.phy.priv = &dev->phy;
339 	dev->mt76.tx_worker.fn = mt7921_tx_worker;
340 
341 	INIT_DELAYED_WORK(&dev->pm.ps_work, mt7921_pm_power_save_work);
342 	INIT_WORK(&dev->pm.wake_work, mt7921_pm_wake_work);
343 	spin_lock_init(&dev->pm.wake.lock);
344 	mutex_init(&dev->pm.mutex);
345 	init_waitqueue_head(&dev->pm.wait);
346 	if (mt76_is_sdio(&dev->mt76))
347 		init_waitqueue_head(&dev->mt76.sdio.wait);
348 	spin_lock_init(&dev->pm.txq_lock);
349 	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt7921_mac_work);
350 	INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work);
351 	INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work);
352 #if IS_ENABLED(CONFIG_IPV6)
353 	INIT_WORK(&dev->ipv6_ns_work, mt7921_set_ipv6_ns_work);
354 	skb_queue_head_init(&dev->ipv6_ns_list);
355 #endif
356 	skb_queue_head_init(&dev->phy.scan_event_list);
357 	skb_queue_head_init(&dev->coredump.msg_list);
358 	INIT_LIST_HEAD(&dev->sta_poll_list);
359 	spin_lock_init(&dev->sta_poll_lock);
360 
361 	INIT_WORK(&dev->reset_work, mt7921_mac_reset_work);
362 	INIT_WORK(&dev->init_work, mt7921_init_work);
363 
364 	INIT_WORK(&dev->phy.roc_work, mt7921_roc_work);
365 	timer_setup(&dev->phy.roc_timer, mt7921_roc_timer, 0);
366 	init_waitqueue_head(&dev->phy.roc_wait);
367 
368 	dev->pm.idle_timeout = MT7921_PM_TIMEOUT;
369 	dev->pm.stats.last_wake_event = jiffies;
370 	dev->pm.stats.last_doze_event = jiffies;
371 	if (!mt76_is_usb(&dev->mt76)) {
372 		dev->pm.enable_user = true;
373 		dev->pm.enable = true;
374 		dev->pm.ds_enable_user = true;
375 		dev->pm.ds_enable = true;
376 	}
377 
378 	if (!mt76_is_mmio(&dev->mt76))
379 		hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;
380 
381 	mt7921_init_acpi_sar(dev);
382 
383 	ret = mt7921_init_wcid(dev);
384 	if (ret)
385 		return ret;
386 
387 	ret = mt7921_init_wiphy(hw);
388 	if (ret)
389 		return ret;
390 
391 	dev->mphy.sband_2g.sband.ht_cap.cap |=
392 			IEEE80211_HT_CAP_LDPC_CODING |
393 			IEEE80211_HT_CAP_MAX_AMSDU;
394 	dev->mphy.sband_5g.sband.ht_cap.cap |=
395 			IEEE80211_HT_CAP_LDPC_CODING |
396 			IEEE80211_HT_CAP_MAX_AMSDU;
397 	dev->mphy.sband_5g.sband.vht_cap.cap |=
398 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
399 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
400 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
401 			IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
402 			(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
403 	if (is_mt7922(&dev->mt76))
404 		dev->mphy.sband_5g.sband.vht_cap.cap |=
405 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
406 			IEEE80211_VHT_CAP_SHORT_GI_160;
407 
408 	dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
409 	dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
410 
411 	queue_work(system_wq, &dev->init_work);
412 
413 	return 0;
414 }
415 EXPORT_SYMBOL_GPL(mt7921_register_device);
416