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 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
38 
39 	memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2));
40 	dev->mt76.region = request->dfs_region;
41 
42 	mt7921_mutex_acquire(dev);
43 	mt76_connac_mcu_set_channel_domain(hw->priv);
44 	mt76_connac_mcu_set_rate_txpower(phy->mt76);
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 = 64;
57 	hw->max_tx_aggregation_subframes = 128;
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 
101 	if (dev->pm.enable)
102 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
103 
104 	hw->max_tx_fragments = 4;
105 
106 	return 0;
107 }
108 
109 static void
110 mt7921_mac_init_band(struct mt7921_dev *dev, u8 band)
111 {
112 	mt76_rmw_field(dev, MT_TMAC_CTCR0(band),
113 		       MT_TMAC_CTCR0_INS_DDLMT_REFTIME, 0x3f);
114 	mt76_set(dev, MT_TMAC_CTCR0(band),
115 		 MT_TMAC_CTCR0_INS_DDLMT_VHT_SMPDU_EN |
116 		 MT_TMAC_CTCR0_INS_DDLMT_EN);
117 
118 	mt76_set(dev, MT_WF_RMAC_MIB_TIME0(band), MT_WF_RMAC_MIB_RXTIME_EN);
119 	mt76_set(dev, MT_WF_RMAC_MIB_AIRTIME0(band), MT_WF_RMAC_MIB_RXTIME_EN);
120 
121 	/* enable MIB tx-rx time reporting */
122 	mt76_set(dev, MT_MIB_SCR1(band), MT_MIB_TXDUR_EN);
123 	mt76_set(dev, MT_MIB_SCR1(band), MT_MIB_RXDUR_EN);
124 
125 	mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_MAX_RX_LEN, 1536);
126 	/* disable rx rate report by default due to hw issues */
127 	mt76_clear(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN);
128 }
129 
130 int mt7921_mac_init(struct mt7921_dev *dev)
131 {
132 	int i;
133 
134 	mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);
135 	/* enable hardware de-agg */
136 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);
137 	/* enable hardware rx header translation */
138 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN);
139 
140 	for (i = 0; i < MT7921_WTBL_SIZE; i++)
141 		mt7921_mac_wtbl_update(dev, i,
142 				       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
143 	for (i = 0; i < 2; i++)
144 		mt7921_mac_init_band(dev, i);
145 
146 	dev->mt76.rxfilter = mt76_rr(dev, MT_WF_RFCR(0));
147 
148 	return mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0);
149 }
150 EXPORT_SYMBOL_GPL(mt7921_mac_init);
151 
152 static int __mt7921_init_hardware(struct mt7921_dev *dev)
153 {
154 	int ret;
155 
156 	/* force firmware operation mode into normal state,
157 	 * which should be set before firmware download stage.
158 	 */
159 	mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE);
160 	ret = mt7921_mcu_init(dev);
161 	if (ret)
162 		goto out;
163 
164 	mt76_eeprom_override(&dev->mphy);
165 
166 	ret = mt7921_mcu_set_eeprom(dev);
167 	if (ret)
168 		goto out;
169 
170 	ret = mt7921_mac_init(dev);
171 out:
172 	return ret;
173 }
174 
175 static int mt7921_init_hardware(struct mt7921_dev *dev)
176 {
177 	int ret, i;
178 
179 	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
180 
181 	for (i = 0; i < MT7921_MCU_INIT_RETRY_COUNT; i++) {
182 		ret = __mt7921_init_hardware(dev);
183 		if (!ret)
184 			break;
185 
186 		mt7921_init_reset(dev);
187 	}
188 
189 	if (i == MT7921_MCU_INIT_RETRY_COUNT) {
190 		dev_err(dev->mt76.dev, "hardware init failed\n");
191 		return ret;
192 	}
193 
194 	return 0;
195 }
196 
197 static int mt7921_init_wcid(struct mt7921_dev *dev)
198 {
199 	int idx;
200 
201 	/* Beacon and mgmt frames should occupy wcid 0 */
202 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
203 	if (idx)
204 		return -ENOSPC;
205 
206 	dev->mt76.global_wcid.idx = idx;
207 	dev->mt76.global_wcid.hw_key_idx = -1;
208 	dev->mt76.global_wcid.tx_info |= MT_WCID_TX_INFO_SET;
209 	rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid);
210 
211 	return 0;
212 }
213 
214 static void mt7921_init_work(struct work_struct *work)
215 {
216 	struct mt7921_dev *dev = container_of(work, struct mt7921_dev,
217 					      init_work);
218 	int ret;
219 
220 	ret = mt7921_init_hardware(dev);
221 	if (ret)
222 		return;
223 
224 	mt76_set_stream_caps(&dev->mphy, true);
225 	mt7921_set_stream_he_caps(&dev->phy);
226 
227 	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
228 				   ARRAY_SIZE(mt76_rates));
229 	if (ret) {
230 		dev_err(dev->mt76.dev, "register device failed\n");
231 		return;
232 	}
233 
234 	ret = mt7921_init_debugfs(dev);
235 	if (ret) {
236 		dev_err(dev->mt76.dev, "register debugfs failed\n");
237 		return;
238 	}
239 
240 	/* we support chip reset now */
241 	dev->hw_init_done = true;
242 
243 	mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable);
244 }
245 
246 int mt7921_register_device(struct mt7921_dev *dev)
247 {
248 	struct ieee80211_hw *hw = mt76_hw(dev);
249 	int ret;
250 
251 	dev->phy.dev = dev;
252 	dev->phy.mt76 = &dev->mt76.phy;
253 	dev->mt76.phy.priv = &dev->phy;
254 	dev->mt76.tx_worker.fn = mt7921_tx_worker;
255 
256 	INIT_DELAYED_WORK(&dev->pm.ps_work, mt7921_pm_power_save_work);
257 	INIT_WORK(&dev->pm.wake_work, mt7921_pm_wake_work);
258 	spin_lock_init(&dev->pm.wake.lock);
259 	mutex_init(&dev->pm.mutex);
260 	init_waitqueue_head(&dev->pm.wait);
261 	if (mt76_is_sdio(&dev->mt76))
262 		init_waitqueue_head(&dev->mt76.sdio.wait);
263 	spin_lock_init(&dev->pm.txq_lock);
264 	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt7921_mac_work);
265 	INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work);
266 	INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work);
267 #if IS_ENABLED(CONFIG_IPV6)
268 	INIT_WORK(&dev->ipv6_ns_work, mt7921_set_ipv6_ns_work);
269 	skb_queue_head_init(&dev->ipv6_ns_list);
270 #endif
271 	skb_queue_head_init(&dev->phy.scan_event_list);
272 	skb_queue_head_init(&dev->coredump.msg_list);
273 	INIT_LIST_HEAD(&dev->sta_poll_list);
274 	spin_lock_init(&dev->sta_poll_lock);
275 
276 	INIT_WORK(&dev->reset_work, mt7921_mac_reset_work);
277 	INIT_WORK(&dev->init_work, mt7921_init_work);
278 
279 	dev->pm.idle_timeout = MT7921_PM_TIMEOUT;
280 	dev->pm.stats.last_wake_event = jiffies;
281 	dev->pm.stats.last_doze_event = jiffies;
282 	if (!mt76_is_usb(&dev->mt76)) {
283 		dev->pm.enable_user = true;
284 		dev->pm.enable = true;
285 		dev->pm.ds_enable_user = true;
286 		dev->pm.ds_enable = true;
287 	}
288 
289 	if (!mt76_is_mmio(&dev->mt76))
290 		hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;
291 
292 	ret = mt7921_init_wcid(dev);
293 	if (ret)
294 		return ret;
295 
296 	ret = mt7921_init_wiphy(hw);
297 	if (ret)
298 		return ret;
299 
300 	dev->mphy.sband_2g.sband.ht_cap.cap |=
301 			IEEE80211_HT_CAP_LDPC_CODING |
302 			IEEE80211_HT_CAP_MAX_AMSDU;
303 	dev->mphy.sband_5g.sband.ht_cap.cap |=
304 			IEEE80211_HT_CAP_LDPC_CODING |
305 			IEEE80211_HT_CAP_MAX_AMSDU;
306 	dev->mphy.sband_5g.sband.vht_cap.cap |=
307 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991 |
308 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
309 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
310 			IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
311 			(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
312 	if (is_mt7922(&dev->mt76))
313 		dev->mphy.sband_5g.sband.vht_cap.cap |=
314 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
315 			IEEE80211_VHT_CAP_SHORT_GI_160;
316 
317 	dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
318 	dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
319 
320 	queue_work(system_wq, &dev->init_work);
321 
322 	return 0;
323 }
324 EXPORT_SYMBOL_GPL(mt7921_register_device);
325