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 
17 static const struct ieee80211_iface_combination if_comb[] = {
18 	{
19 		.limits = if_limits,
20 		.n_limits = ARRAY_SIZE(if_limits),
21 		.max_interfaces = MT7921_MAX_INTERFACES,
22 		.num_different_channels = 1,
23 		.beacon_int_infra_match = true,
24 	}
25 };
26 
27 static void
28 mt7921_regd_notifier(struct wiphy *wiphy,
29 		     struct regulatory_request *request)
30 {
31 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
32 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
33 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
34 
35 	memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2));
36 	dev->mt76.region = request->dfs_region;
37 
38 	mt7921_mutex_acquire(dev);
39 	mt76_connac_mcu_set_channel_domain(hw->priv);
40 	mt76_connac_mcu_set_rate_txpower(phy->mt76);
41 	mt7921_mutex_release(dev);
42 }
43 
44 static int
45 mt7921_init_wiphy(struct ieee80211_hw *hw)
46 {
47 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
48 	struct mt7921_dev *dev = phy->dev;
49 	struct wiphy *wiphy = hw->wiphy;
50 
51 	hw->queues = 4;
52 	hw->max_rx_aggregation_subframes = 64;
53 	hw->max_tx_aggregation_subframes = 128;
54 	hw->netdev_features = NETIF_F_RXCSUM;
55 
56 	hw->radiotap_timestamp.units_pos =
57 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US;
58 
59 	phy->slottime = 9;
60 
61 	hw->sta_data_size = sizeof(struct mt7921_sta);
62 	hw->vif_data_size = sizeof(struct mt7921_vif);
63 
64 	wiphy->iface_combinations = if_comb;
65 	wiphy->flags &= ~(WIPHY_FLAG_IBSS_RSN | WIPHY_FLAG_4ADDR_AP |
66 			  WIPHY_FLAG_4ADDR_STATION);
67 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
68 	wiphy->n_iface_combinations = ARRAY_SIZE(if_comb);
69 	wiphy->max_scan_ie_len = MT76_CONNAC_SCAN_IE_LEN;
70 	wiphy->max_scan_ssids = 4;
71 	wiphy->max_sched_scan_plan_interval =
72 		MT76_CONNAC_MAX_TIME_SCHED_SCAN_INTERVAL;
73 	wiphy->max_sched_scan_ie_len = IEEE80211_MAX_DATA_LEN;
74 	wiphy->max_sched_scan_ssids = MT76_CONNAC_MAX_SCHED_SCAN_SSID;
75 	wiphy->max_match_sets = MT76_CONNAC_MAX_SCAN_MATCH;
76 	wiphy->max_sched_scan_reqs = 1;
77 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
78 	wiphy->reg_notifier = mt7921_regd_notifier;
79 	wiphy->sar_capa = &mt76_sar_capa;
80 
81 	phy->mt76->frp = devm_kcalloc(dev->mt76.dev,
82 				      wiphy->sar_capa->num_freq_ranges,
83 				      sizeof(struct mt76_freq_range_power),
84 				      GFP_KERNEL);
85 	if (!phy->mt76->frp)
86 		return -ENOMEM;
87 
88 	wiphy->features |= NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
89 			   NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
90 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_SET_SCAN_DWELL);
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 
100 	if (dev->pm.enable)
101 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
102 
103 	hw->max_tx_fragments = 4;
104 
105 	return 0;
106 }
107 
108 static void
109 mt7921_mac_init_band(struct mt7921_dev *dev, u8 band)
110 {
111 	mt76_rmw_field(dev, MT_TMAC_CTCR0(band),
112 		       MT_TMAC_CTCR0_INS_DDLMT_REFTIME, 0x3f);
113 	mt76_set(dev, MT_TMAC_CTCR0(band),
114 		 MT_TMAC_CTCR0_INS_DDLMT_VHT_SMPDU_EN |
115 		 MT_TMAC_CTCR0_INS_DDLMT_EN);
116 
117 	mt76_set(dev, MT_WF_RMAC_MIB_TIME0(band), MT_WF_RMAC_MIB_RXTIME_EN);
118 	mt76_set(dev, MT_WF_RMAC_MIB_AIRTIME0(band), MT_WF_RMAC_MIB_RXTIME_EN);
119 
120 	/* enable MIB tx-rx time reporting */
121 	mt76_set(dev, MT_MIB_SCR1(band), MT_MIB_TXDUR_EN);
122 	mt76_set(dev, MT_MIB_SCR1(band), MT_MIB_RXDUR_EN);
123 
124 	mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_MAX_RX_LEN, 1536);
125 	/* disable rx rate report by default due to hw issues */
126 	mt76_clear(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN);
127 }
128 
129 int mt7921_mac_init(struct mt7921_dev *dev)
130 {
131 	int i;
132 
133 	mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);
134 	/* enable hardware de-agg */
135 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);
136 	/* enable hardware rx header translation */
137 	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN);
138 
139 	for (i = 0; i < MT7921_WTBL_SIZE; i++)
140 		mt7921_mac_wtbl_update(dev, i,
141 				       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
142 	for (i = 0; i < 2; i++)
143 		mt7921_mac_init_band(dev, i);
144 
145 	dev->mt76.rxfilter = mt76_rr(dev, MT_WF_RFCR(0));
146 
147 	return mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0);
148 }
149 EXPORT_SYMBOL_GPL(mt7921_mac_init);
150 
151 static int __mt7921_init_hardware(struct mt7921_dev *dev)
152 {
153 	int ret;
154 
155 	/* force firmware operation mode into normal state,
156 	 * which should be set before firmware download stage.
157 	 */
158 	mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE);
159 	ret = mt7921_mcu_init(dev);
160 	if (ret)
161 		goto out;
162 
163 	mt76_eeprom_override(&dev->mphy);
164 
165 	ret = mt7921_mcu_set_eeprom(dev);
166 	if (ret)
167 		goto out;
168 
169 	ret = mt7921_mac_init(dev);
170 out:
171 	return ret;
172 }
173 
174 static int mt7921_init_hardware(struct mt7921_dev *dev)
175 {
176 	int ret, idx, i;
177 
178 	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
179 
180 	for (i = 0; i < MT7921_MCU_INIT_RETRY_COUNT; i++) {
181 		ret = __mt7921_init_hardware(dev);
182 		if (!ret)
183 			break;
184 
185 		mt7921_init_reset(dev);
186 	}
187 
188 	if (i == MT7921_MCU_INIT_RETRY_COUNT) {
189 		dev_err(dev->mt76.dev, "hardware init failed\n");
190 		return ret;
191 	}
192 
193 	/* Beacon and mgmt frames should occupy wcid 0 */
194 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
195 	if (idx)
196 		return -ENOSPC;
197 
198 	dev->mt76.global_wcid.idx = idx;
199 	dev->mt76.global_wcid.hw_key_idx = -1;
200 	dev->mt76.global_wcid.tx_info |= MT_WCID_TX_INFO_SET;
201 	rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid);
202 
203 	return 0;
204 }
205 
206 int mt7921_register_device(struct mt7921_dev *dev)
207 {
208 	struct ieee80211_hw *hw = mt76_hw(dev);
209 	int ret;
210 
211 	dev->phy.dev = dev;
212 	dev->phy.mt76 = &dev->mt76.phy;
213 	dev->mt76.phy.priv = &dev->phy;
214 	dev->mt76.tx_worker.fn = mt7921_tx_worker;
215 
216 	INIT_DELAYED_WORK(&dev->pm.ps_work, mt7921_pm_power_save_work);
217 	INIT_WORK(&dev->pm.wake_work, mt7921_pm_wake_work);
218 	spin_lock_init(&dev->pm.wake.lock);
219 	mutex_init(&dev->pm.mutex);
220 	init_waitqueue_head(&dev->pm.wait);
221 	if (mt76_is_sdio(&dev->mt76))
222 		init_waitqueue_head(&dev->mt76.sdio.wait);
223 	spin_lock_init(&dev->pm.txq_lock);
224 	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt7921_mac_work);
225 	INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work);
226 	INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work);
227 	skb_queue_head_init(&dev->phy.scan_event_list);
228 	skb_queue_head_init(&dev->coredump.msg_list);
229 	INIT_LIST_HEAD(&dev->sta_poll_list);
230 	spin_lock_init(&dev->sta_poll_lock);
231 
232 	INIT_WORK(&dev->reset_work, mt7921_mac_reset_work);
233 
234 	dev->pm.idle_timeout = MT7921_PM_TIMEOUT;
235 	dev->pm.stats.last_wake_event = jiffies;
236 	dev->pm.stats.last_doze_event = jiffies;
237 
238 	/* TODO: mt7921s run sleep mode on default  */
239 	if (mt76_is_mmio(&dev->mt76)) {
240 		dev->pm.enable = true;
241 		dev->pm.ds_enable = true;
242 	}
243 
244 	if (mt76_is_sdio(&dev->mt76))
245 		hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;
246 
247 	ret = mt7921_init_hardware(dev);
248 	if (ret)
249 		return ret;
250 
251 	ret = mt7921_init_wiphy(hw);
252 	if (ret)
253 		return ret;
254 
255 	dev->mphy.sband_2g.sband.ht_cap.cap |=
256 			IEEE80211_HT_CAP_LDPC_CODING |
257 			IEEE80211_HT_CAP_MAX_AMSDU;
258 	dev->mphy.sband_5g.sband.ht_cap.cap |=
259 			IEEE80211_HT_CAP_LDPC_CODING |
260 			IEEE80211_HT_CAP_MAX_AMSDU;
261 	dev->mphy.sband_5g.sband.vht_cap.cap |=
262 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991 |
263 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
264 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
265 			IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
266 			(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
267 
268 	dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
269 	dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
270 
271 	mt76_set_stream_caps(&dev->mphy, true);
272 	mt7921_set_stream_he_caps(&dev->phy);
273 
274 	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
275 				   ARRAY_SIZE(mt76_rates));
276 	if (ret)
277 		return ret;
278 
279 	ret = mt7921_init_debugfs(dev);
280 	if (ret)
281 		return ret;
282 
283 	ret = mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable);
284 	if (ret)
285 		return ret;
286 
287 	dev->hw_init_done = true;
288 
289 	return 0;
290 }
291 EXPORT_SYMBOL_GPL(mt7921_register_device);
292