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