Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: ISC
  2/* Copyright (C) 2020 MediaTek Inc. */
  3
  4#include <linux/etherdevice.h>
  5#include <linux/hwmon.h>
  6#include <linux/hwmon-sysfs.h>
  7#include <linux/thermal.h>
  8#include <linux/firmware.h>
  9#include "mt7921.h"
 10#include "../mt76_connac2_mac.h"
 11#include "mcu.h"
 
 12
 13static ssize_t mt7921_thermal_temp_show(struct device *dev,
 14					struct device_attribute *attr,
 15					char *buf)
 16{
 17	switch (to_sensor_dev_attr(attr)->index) {
 18	case 0: {
 19		struct mt792x_phy *phy = dev_get_drvdata(dev);
 20		struct mt792x_dev *mdev = phy->dev;
 21		int temperature;
 22
 23		mt792x_mutex_acquire(mdev);
 24		temperature = mt7921_mcu_get_temperature(phy);
 25		mt792x_mutex_release(mdev);
 26
 27		if (temperature < 0)
 28			return temperature;
 29		/* display in millidegree Celsius */
 30		return sprintf(buf, "%u\n", temperature * 1000);
 31	}
 32	default:
 33		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 34	}
 35}
 36static SENSOR_DEVICE_ATTR_RO(temp1_input, mt7921_thermal_temp, 0);
 37
 38static struct attribute *mt7921_hwmon_attrs[] = {
 39	&sensor_dev_attr_temp1_input.dev_attr.attr,
 40	NULL,
 
 
 
 
 
 41};
 42ATTRIBUTE_GROUPS(mt7921_hwmon);
 43
 44static int mt7921_thermal_init(struct mt792x_phy *phy)
 
 
 45{
 46	struct wiphy *wiphy = phy->mt76->hw->wiphy;
 47	struct device *hwmon;
 48	const char *name;
 49
 50	if (!IS_REACHABLE(CONFIG_HWMON))
 51		return 0;
 52
 53	name = devm_kasprintf(&wiphy->dev, GFP_KERNEL, "mt7921_%s",
 54			      wiphy_name(wiphy));
 55	if (!name)
 56		return -ENOMEM;
 57
 58	hwmon = devm_hwmon_device_register_with_groups(&wiphy->dev, name, phy,
 59						       mt7921_hwmon_groups);
 60	return PTR_ERR_OR_ZERO(hwmon);
 61}
 62
 63static void
 64mt7921_regd_channel_update(struct wiphy *wiphy, struct mt792x_dev *dev)
 65{
 66#define IS_UNII_INVALID(idx, sfreq, efreq) \
 67	(!(dev->phy.clc_chan_conf & BIT(idx)) && (cfreq) >= (sfreq) && (cfreq) <= (efreq))
 68	struct ieee80211_supported_band *sband;
 69	struct mt76_dev *mdev = &dev->mt76;
 70	struct device_node *np, *band_np;
 71	struct ieee80211_channel *ch;
 72	int i, cfreq;
 73
 74	np = mt76_find_power_limits_node(mdev);
 75
 76	sband = wiphy->bands[NL80211_BAND_5GHZ];
 77	band_np = np ? of_get_child_by_name(np, "txpower-5g") : NULL;
 78	for (i = 0; i < sband->n_channels; i++) {
 79		ch = &sband->channels[i];
 80		cfreq = ch->center_freq;
 81
 82		if (np && (!band_np || !mt76_find_channel_node(band_np, ch))) {
 83			ch->flags |= IEEE80211_CHAN_DISABLED;
 84			continue;
 85		}
 86
 87		/* UNII-4 */
 88		if (IS_UNII_INVALID(0, 5845, 5925))
 89			ch->flags |= IEEE80211_CHAN_DISABLED;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 90	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 91
 92	sband = wiphy->bands[NL80211_BAND_6GHZ];
 93	if (!sband)
 94		return;
 95
 96	band_np = np ? of_get_child_by_name(np, "txpower-6g") : NULL;
 97	for (i = 0; i < sband->n_channels; i++) {
 98		ch = &sband->channels[i];
 99		cfreq = ch->center_freq;
100
101		if (np && (!band_np || !mt76_find_channel_node(band_np, ch))) {
102			ch->flags |= IEEE80211_CHAN_DISABLED;
103			continue;
104		}
105
106		/* UNII-5/6/7/8 */
107		if (IS_UNII_INVALID(1, 5925, 6425) ||
108		    IS_UNII_INVALID(2, 6425, 6525) ||
109		    IS_UNII_INVALID(3, 6525, 6875) ||
110		    IS_UNII_INVALID(4, 6875, 7125))
111			ch->flags |= IEEE80211_CHAN_DISABLED;
112	}
113}
114
115void mt7921_regd_update(struct mt792x_dev *dev)
 
116{
117	struct mt76_dev *mdev = &dev->mt76;
118	struct ieee80211_hw *hw = mdev->hw;
119	struct wiphy *wiphy = hw->wiphy;
120
121	mt7921_mcu_set_clc(dev, mdev->alpha2, dev->country_ie_env);
122	mt7921_regd_channel_update(wiphy, dev);
123	mt76_connac_mcu_set_channel_domain(hw->priv);
124	mt7921_set_tx_sar_pwr(hw, NULL);
 
 
 
 
 
 
 
 
125}
126EXPORT_SYMBOL_GPL(mt7921_regd_update);
127
128static void
129mt7921_regd_notifier(struct wiphy *wiphy,
130		     struct regulatory_request *request)
131{
132	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
133	struct mt792x_dev *dev = mt792x_hw_dev(hw);
134	struct mt76_connac_pm *pm = &dev->pm;
 
 
 
135
136	memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2));
137	dev->mt76.region = request->dfs_region;
138	dev->country_ie_env = request->country_ie_env;
139
140	if (pm->suspended)
141		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
142
143	dev->regd_in_progress = true;
 
 
 
 
 
 
 
144
145	mt792x_mutex_acquire(dev);
146	mt7921_regd_update(dev);
147	mt792x_mutex_release(dev);
 
 
 
 
148
149	dev->regd_in_progress = false;
150	wake_up(&dev->wait);
 
151}
 
152
153int mt7921_mac_init(struct mt792x_dev *dev)
154{
155	int i;
156
157	mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536);
158	/* enable hardware de-agg */
159	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN);
160	/* enable hardware rx header translation */
161	mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN);
162
163	for (i = 0; i < MT792x_WTBL_SIZE; i++)
164		mt7921_mac_wtbl_update(dev, i,
165				       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
166	for (i = 0; i < 2; i++)
167		mt792x_mac_init_band(dev, i);
 
 
168
169	return mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0);
170}
171EXPORT_SYMBOL_GPL(mt7921_mac_init);
172
173static int __mt7921_init_hardware(struct mt792x_dev *dev)
174{
175	int ret;
176
177	/* force firmware operation mode into normal state,
178	 * which should be set before firmware download stage.
179	 */
180	mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE);
181	ret = mt792x_mcu_init(dev);
182	if (ret)
183		goto out;
184
185	mt76_eeprom_override(&dev->mphy);
186
187	ret = mt7921_mcu_set_eeprom(dev);
188	if (ret)
189		goto out;
190
191	ret = mt7921_mac_init(dev);
192out:
193	return ret;
194}
195
196static int mt7921_init_hardware(struct mt792x_dev *dev)
197{
198	int ret, i;
199
200	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
201
202	for (i = 0; i < MT792x_MCU_INIT_RETRY_COUNT; i++) {
203		ret = __mt7921_init_hardware(dev);
204		if (!ret)
205			break;
206
207		mt792x_init_reset(dev);
208	}
209
210	if (i == MT792x_MCU_INIT_RETRY_COUNT) {
211		dev_err(dev->mt76.dev, "hardware init failed\n");
212		return ret;
213	}
214
215	return 0;
216}
217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218static void mt7921_init_work(struct work_struct *work)
219{
220	struct mt792x_dev *dev = container_of(work, struct mt792x_dev,
221					      init_work);
222	int ret;
223
224	ret = mt7921_init_hardware(dev);
225	if (ret)
226		return;
227
228	mt76_set_stream_caps(&dev->mphy, true);
229	mt7921_set_stream_he_caps(&dev->phy);
230
231	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
232				   ARRAY_SIZE(mt76_rates));
233	if (ret) {
234		dev_err(dev->mt76.dev, "register device failed\n");
235		return;
236	}
237
238	ret = mt7921_init_debugfs(dev);
239	if (ret) {
240		dev_err(dev->mt76.dev, "register debugfs failed\n");
241		return;
242	}
243
244	ret = mt7921_thermal_init(&dev->phy);
245	if (ret) {
246		dev_err(dev->mt76.dev, "thermal init failed\n");
247		return;
248	}
249
250	/* we support chip reset now */
251	dev->hw_init_done = true;
252
253	mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable);
254}
255
256int mt7921_register_device(struct mt792x_dev *dev)
257{
258	struct ieee80211_hw *hw = mt76_hw(dev);
259	int ret;
260
261	dev->phy.dev = dev;
262	dev->phy.mt76 = &dev->mt76.phy;
263	dev->mt76.phy.priv = &dev->phy;
264	dev->mt76.tx_worker.fn = mt792x_tx_worker;
265
266	INIT_DELAYED_WORK(&dev->pm.ps_work, mt792x_pm_power_save_work);
267	INIT_WORK(&dev->pm.wake_work, mt792x_pm_wake_work);
268	spin_lock_init(&dev->pm.wake.lock);
269	mutex_init(&dev->pm.mutex);
270	init_waitqueue_head(&dev->pm.wait);
271	init_waitqueue_head(&dev->wait);
272	if (mt76_is_sdio(&dev->mt76))
273		init_waitqueue_head(&dev->mt76.sdio.wait);
274	spin_lock_init(&dev->pm.txq_lock);
275	INIT_DELAYED_WORK(&dev->mphy.mac_work, mt792x_mac_work);
276	INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work);
277	INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work);
278#if IS_ENABLED(CONFIG_IPV6)
279	INIT_WORK(&dev->ipv6_ns_work, mt7921_set_ipv6_ns_work);
280	skb_queue_head_init(&dev->ipv6_ns_list);
281#endif
282	skb_queue_head_init(&dev->phy.scan_event_list);
283	skb_queue_head_init(&dev->coredump.msg_list);
 
 
284
285	INIT_WORK(&dev->reset_work, mt7921_mac_reset_work);
286	INIT_WORK(&dev->init_work, mt7921_init_work);
287
288	INIT_WORK(&dev->phy.roc_work, mt7921_roc_work);
289	timer_setup(&dev->phy.roc_timer, mt792x_roc_timer, 0);
290	init_waitqueue_head(&dev->phy.roc_wait);
291
292	dev->pm.idle_timeout = MT792x_PM_TIMEOUT;
293	dev->pm.stats.last_wake_event = jiffies;
294	dev->pm.stats.last_doze_event = jiffies;
295	if (!mt76_is_usb(&dev->mt76)) {
296		dev->pm.enable_user = true;
297		dev->pm.enable = true;
298		dev->pm.ds_enable_user = true;
299		dev->pm.ds_enable = true;
300	}
301
302	if (!mt76_is_mmio(&dev->mt76))
303		hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE;
304
305	mt792x_init_acpi_sar(dev);
306
307	ret = mt792x_init_wcid(dev);
308	if (ret)
309		return ret;
310
311	ret = mt792x_init_wiphy(hw);
312	if (ret)
313		return ret;
314
315	hw->wiphy->reg_notifier = mt7921_regd_notifier;
316	dev->mphy.sband_2g.sband.ht_cap.cap |=
317			IEEE80211_HT_CAP_LDPC_CODING |
318			IEEE80211_HT_CAP_MAX_AMSDU;
319	dev->mphy.sband_5g.sband.ht_cap.cap |=
320			IEEE80211_HT_CAP_LDPC_CODING |
321			IEEE80211_HT_CAP_MAX_AMSDU;
322	dev->mphy.sband_5g.sband.vht_cap.cap |=
323			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
324			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
325			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
326			IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
327			(3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
328	if (is_mt7922(&dev->mt76))
329		dev->mphy.sband_5g.sband.vht_cap.cap |=
330			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
331			IEEE80211_VHT_CAP_SHORT_GI_160;
332
333	dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask;
334	dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask;
335
336	queue_work(system_wq, &dev->init_work);
337
338	return 0;
339}
340EXPORT_SYMBOL_GPL(mt7921_register_device);
v6.2
  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
 11static 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
 22static 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
 32static 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
 43static 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
 53static void
 54mt7921_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
 71static int
 72mt7921_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
142static void
143mt7921_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
163u8 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}
213EXPORT_SYMBOL_GPL(mt7921_check_offload_capability);
214
215int 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}
235EXPORT_SYMBOL_GPL(mt7921_mac_init);
236
237static 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);
256out:
257	return ret;
258}
259
260static 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
282static 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
299static 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
331int 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}
415EXPORT_SYMBOL_GPL(mt7921_register_device);