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