xref: /linux/drivers/net/wireless/mediatek/mt76/mt792x_core.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /* Copyright (C) 2023 MediaTek Inc. */
3 
4 #include <linux/module.h>
5 #include <linux/firmware.h>
6 #include <linux/slab.h>
7 
8 #include "mt792x.h"
9 #include "dma.h"
10 
11 static const struct ieee80211_iface_limit if_limits[] = {
12 	{
13 		.max = MT792x_MAX_INTERFACES,
14 		.types = BIT(NL80211_IFTYPE_STATION)
15 	},
16 	{
17 		.max = 1,
18 		.types = BIT(NL80211_IFTYPE_AP)
19 	}
20 };
21 
22 static const struct ieee80211_iface_combination if_comb[] = {
23 	{
24 		.limits = if_limits,
25 		.n_limits = ARRAY_SIZE(if_limits),
26 		.max_interfaces = MT792x_MAX_INTERFACES,
27 		.num_different_channels = 1,
28 		.beacon_int_infra_match = true,
29 	},
30 };
31 
32 static const struct ieee80211_iface_limit if_limits_chanctx_mcc[] = {
33 	{
34 		.max = 2,
35 		.types = BIT(NL80211_IFTYPE_STATION) |
36 			 BIT(NL80211_IFTYPE_P2P_CLIENT)
37 	},
38 	{
39 		.max = 1,
40 		.types = BIT(NL80211_IFTYPE_P2P_GO)
41 	},
42 	{
43 		.max = 1,
44 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE)
45 	}
46 };
47 
48 static const struct ieee80211_iface_limit if_limits_chanctx_scc[] = {
49 	{
50 		.max = 2,
51 		.types = BIT(NL80211_IFTYPE_STATION) |
52 			 BIT(NL80211_IFTYPE_P2P_CLIENT)
53 	},
54 	{
55 		.max = 1,
56 		.types = BIT(NL80211_IFTYPE_AP)
57 	},
58 	{
59 		.max = 1,
60 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE)
61 	}
62 };
63 
64 static const struct ieee80211_iface_limit if_limits_nan_mcc[] = {
65 	{
66 		.max = 2,
67 		.types = BIT(NL80211_IFTYPE_STATION),
68 	},
69 	{
70 		.max = 1,
71 		.types = BIT(NL80211_IFTYPE_NAN),
72 	},
73 	{
74 		.max = 2,
75 		.types = BIT(NL80211_IFTYPE_NAN_DATA),
76 	},
77 };
78 
79 static const struct ieee80211_iface_limit if_limits_nan_scc[] = {
80 	{
81 		.max = 2,
82 		.types = BIT(NL80211_IFTYPE_STATION),
83 	},
84 	{
85 		.max = 1,
86 		.types = BIT(NL80211_IFTYPE_NAN),
87 	},
88 	{
89 		.max = 2,
90 		.types = BIT(NL80211_IFTYPE_NAN_DATA),
91 	},
92 	{
93 		.max = 1,
94 		.types = BIT(NL80211_IFTYPE_AP),
95 	},
96 };
97 
98 static const struct ieee80211_iface_combination if_comb_chanctx_base[] = {
99 	{
100 		.limits = if_limits_chanctx_mcc,
101 		.n_limits = ARRAY_SIZE(if_limits_chanctx_mcc),
102 		.max_interfaces = 3,
103 		.num_different_channels = 2,
104 		.beacon_int_infra_match = false,
105 	},
106 	{
107 		.limits = if_limits_chanctx_scc,
108 		.n_limits = ARRAY_SIZE(if_limits_chanctx_scc),
109 		.max_interfaces = 3,
110 		.num_different_channels = 1,
111 		.beacon_int_infra_match = false,
112 	}
113 };
114 
115 static const struct ieee80211_iface_combination if_comb_chanctx_nan[] = {
116 	{
117 		.limits = if_limits_nan_mcc,
118 		.n_limits = ARRAY_SIZE(if_limits_nan_mcc),
119 		.max_interfaces = MT792x_MAX_INTERFACES,
120 		.num_different_channels = 2,
121 		.beacon_int_infra_match = false,
122 	},
123 	{
124 		.limits = if_limits_nan_scc,
125 		.n_limits = ARRAY_SIZE(if_limits_nan_scc),
126 		.max_interfaces = MT792x_MAX_INTERFACES,
127 		.num_different_channels = 1,
128 		.beacon_int_infra_match = false,
129 	},
130 };
131 
mt792x_setup_iface_combinations(struct mt792x_dev * dev,struct wiphy * wiphy)132 static int mt792x_setup_iface_combinations(struct mt792x_dev *dev,
133 					   struct wiphy *wiphy)
134 {
135 	const bool cnm = !!(dev->fw_features & MT792x_FW_CAP_CNM);
136 	const bool nan = !!(dev->fw_features & MT792x_FW_CAP_NAN);
137 	const int n_base = ARRAY_SIZE(if_comb_chanctx_base);
138 	const int n_nan = ARRAY_SIZE(if_comb_chanctx_nan);
139 	struct ieee80211_iface_combination *comb;
140 
141 	if (!cnm) {
142 		dev->iface_combinations = if_comb;
143 		dev->n_iface_combinations = ARRAY_SIZE(if_comb);
144 		return 0;
145 	}
146 
147 	/* CNM enabled, NAN optional */
148 	if (!nan) {
149 		dev->iface_combinations = if_comb_chanctx_base;
150 		dev->n_iface_combinations = ARRAY_SIZE(if_comb_chanctx_base);
151 		return 0;
152 	}
153 
154 	/* CNM + NAN: dynamically build base + nan list */
155 	comb = devm_kcalloc(&wiphy->dev, n_base + n_nan, sizeof(*comb),
156 			    GFP_KERNEL);
157 	if (!comb)
158 		return -ENOMEM;
159 
160 	memcpy(comb, if_comb_chanctx_base, sizeof(if_comb_chanctx_base));
161 	memcpy(comb + n_base, if_comb_chanctx_nan, sizeof(if_comb_chanctx_nan));
162 
163 	dev->iface_combinations = comb;
164 	dev->n_iface_combinations = n_base + n_nan;
165 
166 	return 0;
167 }
168 
mt792x_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)169 void mt792x_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
170 	       struct sk_buff *skb)
171 {
172 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
173 	struct mt76_phy *mphy = hw->priv;
174 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
175 	struct ieee80211_vif *vif = info->control.vif;
176 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
177 	u8 link_id;
178 	int qid;
179 
180 	if (control->sta) {
181 		struct mt792x_link_sta *mlink;
182 		struct mt792x_sta *sta;
183 		link_id = u32_get_bits(info->control.flags,
184 				       IEEE80211_TX_CTRL_MLO_LINK);
185 		sta = (struct mt792x_sta *)control->sta->drv_priv;
186 		mlink = mt792x_sta_to_link(sta, link_id);
187 		wcid = &mlink->wcid;
188 	}
189 
190 	if (vif && !control->sta) {
191 		struct mt792x_vif *mvif;
192 
193 		mvif = (struct mt792x_vif *)vif->drv_priv;
194 		wcid = &mvif->sta.deflink.wcid;
195 	}
196 
197 	if (vif && control->sta && ieee80211_vif_is_mld(vif) &&
198 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
199 		struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
200 		struct ieee80211_link_sta *link_sta;
201 		struct ieee80211_bss_conf *conf;
202 
203 		link_id = wcid->link_id;
204 		rcu_read_lock();
205 		conf = rcu_dereference(vif->link_conf[link_id]);
206 		memcpy(hdr->addr2, conf->addr, ETH_ALEN);
207 
208 		link_sta = rcu_dereference(control->sta->link[link_id]);
209 		memcpy(hdr->addr1, link_sta->addr, ETH_ALEN);
210 
211 		if (vif->type == NL80211_IFTYPE_STATION)
212 			memcpy(hdr->addr3, conf->bssid, ETH_ALEN);
213 		rcu_read_unlock();
214 	}
215 
216 	if (mt76_connac_pm_ref(mphy, &dev->pm)) {
217 		mt76_tx(mphy, control->sta, wcid, skb);
218 		mt76_connac_pm_unref(mphy, &dev->pm);
219 		return;
220 	}
221 
222 	qid = skb_get_queue_mapping(skb);
223 	if (qid >= MT_TXQ_PSD) {
224 		qid = IEEE80211_AC_BE;
225 		skb_set_queue_mapping(skb, qid);
226 	}
227 
228 	mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
229 }
230 EXPORT_SYMBOL_GPL(mt792x_tx);
231 
mt792x_stop(struct ieee80211_hw * hw,bool suspend)232 void mt792x_stop(struct ieee80211_hw *hw, bool suspend)
233 {
234 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
235 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
236 
237 	cancel_delayed_work_sync(&phy->mt76->mac_work);
238 
239 	cancel_delayed_work_sync(&dev->pm.ps_work);
240 	cancel_work_sync(&dev->pm.wake_work);
241 	cancel_work_sync(&dev->reset_work);
242 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
243 
244 	if (is_connac2(&dev->mt76)) {
245 		mt792x_mutex_acquire(dev);
246 		mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
247 		mt792x_mutex_release(dev);
248 	}
249 
250 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
251 }
252 EXPORT_SYMBOL_GPL(mt792x_stop);
253 
mt792x_mac_link_bss_remove(struct mt792x_dev * dev,struct mt792x_bss_conf * mconf,struct mt792x_link_sta * mlink)254 void mt792x_mac_link_bss_remove(struct mt792x_dev *dev,
255 				struct mt792x_bss_conf *mconf,
256 				struct mt792x_link_sta *mlink)
257 {
258 	struct ieee80211_vif *vif = container_of((void *)mconf->vif,
259 						 struct ieee80211_vif, drv_priv);
260 	struct ieee80211_bss_conf *link_conf;
261 	int idx = mlink->wcid.idx;
262 
263 	link_conf = mt792x_vif_to_bss_conf(vif, mconf->link_id);
264 
265 	mt76_connac_free_pending_tx_skbs(&dev->pm, &mlink->wcid);
266 	mt76_connac_mcu_uni_add_dev(&dev->mphy, link_conf, &mconf->mt76,
267 				    &mlink->wcid, false);
268 
269 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
270 
271 	dev->mt76.vif_mask &= ~BIT_ULL(mconf->mt76.idx);
272 	mconf->vif->phy->omac_mask &= ~BIT_ULL(mconf->mt76.omac_idx);
273 
274 	spin_lock_bh(&dev->mt76.sta_poll_lock);
275 	if (!list_empty(&mlink->wcid.poll_list))
276 		list_del_init(&mlink->wcid.poll_list);
277 	spin_unlock_bh(&dev->mt76.sta_poll_lock);
278 
279 	mt76_wcid_cleanup(&dev->mt76, &mlink->wcid);
280 }
281 EXPORT_SYMBOL_GPL(mt792x_mac_link_bss_remove);
282 
mt792x_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)283 void mt792x_remove_interface(struct ieee80211_hw *hw,
284 			     struct ieee80211_vif *vif)
285 {
286 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
287 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
288 	struct mt792x_bss_conf *mconf;
289 
290 	mt792x_mutex_acquire(dev);
291 
292 	mconf = mt792x_link_conf_to_mconf(&vif->bss_conf);
293 	mt792x_mac_link_bss_remove(dev, mconf, &mvif->sta.deflink);
294 
295 	mt792x_mutex_release(dev);
296 }
297 EXPORT_SYMBOL_GPL(mt792x_remove_interface);
298 
mt792x_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,u16 queue,const struct ieee80211_tx_queue_params * params)299 int mt792x_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
300 		   unsigned int link_id, u16 queue,
301 		   const struct ieee80211_tx_queue_params *params)
302 {
303 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
304 
305 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
306 	queue = mt76_connac_lmac_mapping(queue);
307 	mvif->bss_conf.queue_params[queue] = *params;
308 
309 	return 0;
310 }
311 EXPORT_SYMBOL_GPL(mt792x_conf_tx);
312 
mt792x_get_stats(struct ieee80211_hw * hw,struct ieee80211_low_level_stats * stats)313 int mt792x_get_stats(struct ieee80211_hw *hw,
314 		     struct ieee80211_low_level_stats *stats)
315 {
316 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
317 	struct mt76_mib_stats *mib = &phy->mib;
318 
319 	mt792x_mutex_acquire(phy->dev);
320 
321 	stats->dot11RTSSuccessCount = mib->rts_cnt;
322 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
323 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
324 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
325 
326 	mt792x_mutex_release(phy->dev);
327 
328 	return 0;
329 }
330 EXPORT_SYMBOL_GPL(mt792x_get_stats);
331 
mt792x_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)332 u64 mt792x_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
333 {
334 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
335 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
336 	u8 omac_idx = mvif->bss_conf.mt76.omac_idx;
337 	union {
338 		u64 t64;
339 		u32 t32[2];
340 	} tsf;
341 	u16 n;
342 
343 	mt792x_mutex_acquire(dev);
344 
345 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
346 	/* TSF software read */
347 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_MODE);
348 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(0));
349 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(0));
350 
351 	mt792x_mutex_release(dev);
352 
353 	return tsf.t64;
354 }
355 EXPORT_SYMBOL_GPL(mt792x_get_tsf);
356 
mt792x_set_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 timestamp)357 void mt792x_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
358 		    u64 timestamp)
359 {
360 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
361 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
362 	u8 omac_idx = mvif->bss_conf.mt76.omac_idx;
363 	union {
364 		u64 t64;
365 		u32 t32[2];
366 	} tsf = { .t64 = timestamp, };
367 	u16 n;
368 
369 	mt792x_mutex_acquire(dev);
370 
371 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
372 	mt76_wr(dev, MT_LPON_UTTR0(0), tsf.t32[0]);
373 	mt76_wr(dev, MT_LPON_UTTR1(0), tsf.t32[1]);
374 	/* TSF software overwrite */
375 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_WRITE);
376 
377 	mt792x_mutex_release(dev);
378 }
379 EXPORT_SYMBOL_GPL(mt792x_set_tsf);
380 
mt792x_tx_worker(struct mt76_worker * w)381 void mt792x_tx_worker(struct mt76_worker *w)
382 {
383 	struct mt792x_dev *dev = container_of(w, struct mt792x_dev,
384 					      mt76.tx_worker);
385 
386 	if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
387 		queue_work(dev->mt76.wq, &dev->pm.wake_work);
388 		return;
389 	}
390 
391 	mt76_txq_schedule_all(&dev->mphy);
392 	mt76_connac_pm_unref(&dev->mphy, &dev->pm);
393 }
394 EXPORT_SYMBOL_GPL(mt792x_tx_worker);
395 
mt792x_roc_timer(struct timer_list * timer)396 void mt792x_roc_timer(struct timer_list *timer)
397 {
398 	struct mt792x_phy *phy = timer_container_of(phy, timer, roc_timer);
399 
400 	ieee80211_queue_work(phy->mt76->hw, &phy->roc_work);
401 }
402 EXPORT_SYMBOL_GPL(mt792x_roc_timer);
403 
mt792x_csa_timer(struct timer_list * timer)404 void mt792x_csa_timer(struct timer_list *timer)
405 {
406 	struct mt792x_vif *mvif = timer_container_of(mvif, timer, csa_timer);
407 
408 	ieee80211_queue_work(mvif->phy->mt76->hw, &mvif->csa_work);
409 }
410 EXPORT_SYMBOL_GPL(mt792x_csa_timer);
411 
mt792x_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)412 void mt792x_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
413 		  u32 queues, bool drop)
414 {
415 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
416 
417 	wait_event_timeout(dev->mt76.tx_wait,
418 			   !mt76_has_tx_pending(&dev->mphy), HZ / 2);
419 }
420 EXPORT_SYMBOL_GPL(mt792x_flush);
421 
mt792x_get_txpower(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,int * dbm)422 int mt792x_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
423 		       unsigned int link_id, int *dbm)
424 {
425 	struct mt76_power_limits limits = {};
426 	struct ieee80211_bss_conf *link_conf;
427 	struct ieee80211_channel *chan;
428 	struct mt792x_bss_conf *mconf;
429 	struct mt792x_vif *mvif;
430 	struct mt76_phy *phy;
431 	s8 max_power;
432 
433 	if (!vif)
434 		return mt76_get_txpower(hw, vif, link_id, dbm);
435 
436 	mvif = (struct mt792x_vif *)vif->drv_priv;
437 	phy = mvif->phy->mt76;
438 
439 	mt792x_mutex_acquire(mvif->phy->dev);
440 
441 	link_conf = mt792x_vif_to_bss_conf(vif, link_id);
442 	mconf = link_conf ? mt792x_link_conf_to_mconf(link_conf) : NULL;
443 	if (mconf && mconf->mt76.ctx && mconf->mt76.ctx->def.chan)
444 		chan = mconf->mt76.ctx->def.chan;
445 	else
446 		/* Fall back to the current PHY chandef if the requested link
447 		 * does not have a valid channel context.
448 		 */
449 		chan = phy->chandef.chan;
450 
451 	mt792x_mutex_release(mvif->phy->dev);
452 
453 	if (!chan)
454 		return -EINVAL;
455 
456 	max_power = mt76_connac_get_rate_power_limit(phy, chan, &limits);
457 	*dbm = DIV_ROUND_UP(max_power, 2);
458 
459 	return 0;
460 }
461 EXPORT_SYMBOL_GPL(mt792x_get_txpower);
462 
mt792x_assign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)463 int mt792x_assign_vif_chanctx(struct ieee80211_hw *hw,
464 			      struct ieee80211_vif *vif,
465 			      struct ieee80211_bss_conf *link_conf,
466 			      struct ieee80211_chanctx_conf *ctx)
467 {
468 	struct mt792x_chanctx *mctx = (struct mt792x_chanctx *)ctx->drv_priv;
469 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
470 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
471 
472 	mutex_lock(&dev->mt76.mutex);
473 	mvif->bss_conf.mt76.ctx = ctx;
474 	mctx->bss_conf = &mvif->bss_conf;
475 	mutex_unlock(&dev->mt76.mutex);
476 
477 	return 0;
478 }
479 EXPORT_SYMBOL_GPL(mt792x_assign_vif_chanctx);
480 
mt792x_unassign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)481 void mt792x_unassign_vif_chanctx(struct ieee80211_hw *hw,
482 				 struct ieee80211_vif *vif,
483 				 struct ieee80211_bss_conf *link_conf,
484 				 struct ieee80211_chanctx_conf *ctx)
485 {
486 	struct mt792x_chanctx *mctx = (struct mt792x_chanctx *)ctx->drv_priv;
487 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
488 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
489 
490 	mutex_lock(&dev->mt76.mutex);
491 	mctx->bss_conf = NULL;
492 	mvif->bss_conf.mt76.ctx = NULL;
493 	mutex_unlock(&dev->mt76.mutex);
494 
495 	if (vif->bss_conf.csa_active) {
496 		timer_delete_sync(&mvif->csa_timer);
497 		cancel_work_sync(&mvif->csa_work);
498 	}
499 }
500 EXPORT_SYMBOL_GPL(mt792x_unassign_vif_chanctx);
501 
mt792x_set_wakeup(struct ieee80211_hw * hw,bool enabled)502 void mt792x_set_wakeup(struct ieee80211_hw *hw, bool enabled)
503 {
504 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
505 	struct mt76_dev *mdev = &dev->mt76;
506 
507 	device_set_wakeup_enable(mdev->dev, enabled);
508 }
509 EXPORT_SYMBOL_GPL(mt792x_set_wakeup);
510 
511 static const char mt792x_gstrings_stats[][ETH_GSTRING_LEN] = {
512 	/* tx counters */
513 	"tx_ampdu_cnt",
514 	"tx_mpdu_attempts",
515 	"tx_mpdu_success",
516 	"tx_pkt_ebf_cnt",
517 	"tx_pkt_ibf_cnt",
518 	"tx_ampdu_len:0-1",
519 	"tx_ampdu_len:2-10",
520 	"tx_ampdu_len:11-19",
521 	"tx_ampdu_len:20-28",
522 	"tx_ampdu_len:29-37",
523 	"tx_ampdu_len:38-46",
524 	"tx_ampdu_len:47-55",
525 	"tx_ampdu_len:56-79",
526 	"tx_ampdu_len:80-103",
527 	"tx_ampdu_len:104-127",
528 	"tx_ampdu_len:128-151",
529 	"tx_ampdu_len:152-175",
530 	"tx_ampdu_len:176-199",
531 	"tx_ampdu_len:200-223",
532 	"tx_ampdu_len:224-247",
533 	"ba_miss_count",
534 	"tx_beamformer_ppdu_iBF",
535 	"tx_beamformer_ppdu_eBF",
536 	"tx_beamformer_rx_feedback_all",
537 	"tx_beamformer_rx_feedback_he",
538 	"tx_beamformer_rx_feedback_vht",
539 	"tx_beamformer_rx_feedback_ht",
540 	"tx_msdu_pack_1",
541 	"tx_msdu_pack_2",
542 	"tx_msdu_pack_3",
543 	"tx_msdu_pack_4",
544 	"tx_msdu_pack_5",
545 	"tx_msdu_pack_6",
546 	"tx_msdu_pack_7",
547 	"tx_msdu_pack_8",
548 	/* rx counters */
549 	"rx_mpdu_cnt",
550 	"rx_ampdu_cnt",
551 	"rx_ampdu_bytes_cnt",
552 	"rx_ba_cnt",
553 	/* per vif counters */
554 	"v_tx_mode_cck",
555 	"v_tx_mode_ofdm",
556 	"v_tx_mode_ht",
557 	"v_tx_mode_ht_gf",
558 	"v_tx_mode_vht",
559 	"v_tx_mode_he_su",
560 	"v_tx_mode_he_ext_su",
561 	"v_tx_mode_he_tb",
562 	"v_tx_mode_he_mu",
563 	"v_tx_mode_eht_su",
564 	"v_tx_mode_eht_trig",
565 	"v_tx_mode_eht_mu",
566 	"v_tx_bw_20",
567 	"v_tx_bw_40",
568 	"v_tx_bw_80",
569 	"v_tx_bw_160",
570 	"v_tx_bw_320",
571 	"v_tx_mcs_0",
572 	"v_tx_mcs_1",
573 	"v_tx_mcs_2",
574 	"v_tx_mcs_3",
575 	"v_tx_mcs_4",
576 	"v_tx_mcs_5",
577 	"v_tx_mcs_6",
578 	"v_tx_mcs_7",
579 	"v_tx_mcs_8",
580 	"v_tx_mcs_9",
581 	"v_tx_mcs_10",
582 	"v_tx_mcs_11",
583 	"v_tx_mcs_12",
584 	"v_tx_mcs_13",
585 	"v_tx_nss_1",
586 	"v_tx_nss_2",
587 	"v_tx_nss_3",
588 	"v_tx_nss_4",
589 };
590 
mt792x_get_et_strings(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 sset,u8 * data)591 void mt792x_get_et_strings(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
592 			   u32 sset, u8 *data)
593 {
594 	if (sset != ETH_SS_STATS)
595 		return;
596 
597 	memcpy(data, mt792x_gstrings_stats, sizeof(mt792x_gstrings_stats));
598 
599 	data += sizeof(mt792x_gstrings_stats);
600 	page_pool_ethtool_stats_get_strings(data);
601 }
602 EXPORT_SYMBOL_GPL(mt792x_get_et_strings);
603 
mt792x_get_et_sset_count(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int sset)604 int mt792x_get_et_sset_count(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
605 			     int sset)
606 {
607 	if (sset != ETH_SS_STATS)
608 		return 0;
609 
610 	return ARRAY_SIZE(mt792x_gstrings_stats) +
611 	       page_pool_ethtool_stats_get_count();
612 }
613 EXPORT_SYMBOL_GPL(mt792x_get_et_sset_count);
614 
615 static void
mt792x_ethtool_worker(void * wi_data,struct ieee80211_sta * sta)616 mt792x_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
617 {
618 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
619 	struct mt76_ethtool_worker_info *wi = wi_data;
620 
621 	if (msta->vif->bss_conf.mt76.idx != wi->idx)
622 		return;
623 
624 	mt76_ethtool_worker(wi, &msta->deflink.wcid.stats, true);
625 }
626 
mt792x_get_et_stats(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ethtool_stats * stats,u64 * data)627 void mt792x_get_et_stats(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
628 			 struct ethtool_stats *stats, u64 *data)
629 {
630 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
631 	int stats_size = ARRAY_SIZE(mt792x_gstrings_stats);
632 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
633 	struct mt792x_dev *dev = phy->dev;
634 	struct mt76_mib_stats *mib = &phy->mib;
635 	struct mt76_ethtool_worker_info wi = {
636 		.data = data,
637 		.idx = mvif->bss_conf.mt76.idx,
638 	};
639 	int i, ei = 0;
640 
641 	mt792x_mutex_acquire(dev);
642 
643 	mt792x_mac_update_mib_stats(phy);
644 
645 	data[ei++] = mib->tx_ampdu_cnt;
646 	data[ei++] = mib->tx_mpdu_attempts_cnt;
647 	data[ei++] = mib->tx_mpdu_success_cnt;
648 	data[ei++] = mib->tx_pkt_ebf_cnt;
649 	data[ei++] = mib->tx_pkt_ibf_cnt;
650 
651 	/* Tx ampdu stat */
652 	for (i = 0; i < 15; i++)
653 		data[ei++] = phy->mt76->aggr_stats[i];
654 
655 	data[ei++] = phy->mib.ba_miss_cnt;
656 
657 	/* Tx Beamformer monitor */
658 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
659 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
660 
661 	/* Tx Beamformer Rx feedback monitor */
662 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
663 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
664 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
665 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
666 
667 	/* Tx amsdu info (pack-count histogram) */
668 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
669 		data[ei++] = mib->tx_amsdu[i];
670 
671 	/* rx counters */
672 	data[ei++] = mib->rx_mpdu_cnt;
673 	data[ei++] = mib->rx_ampdu_cnt;
674 	data[ei++] = mib->rx_ampdu_bytes_cnt;
675 	data[ei++] = mib->rx_ba_cnt;
676 
677 	/* Add values for all stations owned by this vif */
678 	wi.initial_stat_idx = ei;
679 	ieee80211_iterate_stations_atomic(hw, mt792x_ethtool_worker, &wi);
680 
681 	mt792x_mutex_release(dev);
682 
683 	if (!wi.sta_count)
684 		return;
685 
686 	ei += wi.worker_stat_count;
687 
688 	mt76_ethtool_page_pool_stats(&dev->mt76, &data[ei], &ei);
689 	stats_size += page_pool_ethtool_stats_get_count();
690 
691 	if (ei != stats_size)
692 		dev_err(dev->mt76.dev, "ei: %d  SSTATS_LEN: %d", ei,
693 			stats_size);
694 }
695 EXPORT_SYMBOL_GPL(mt792x_get_et_stats);
696 
mt792x_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)697 void mt792x_sta_statistics(struct ieee80211_hw *hw,
698 			   struct ieee80211_vif *vif,
699 			   struct ieee80211_sta *sta,
700 			   struct station_info *sinfo)
701 {
702 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
703 	struct rate_info *txrate = &msta->deflink.wcid.rate;
704 
705 	if (!txrate->legacy && !txrate->flags)
706 		return;
707 
708 	if (txrate->legacy) {
709 		sinfo->txrate.legacy = txrate->legacy;
710 	} else {
711 		sinfo->txrate.mcs = txrate->mcs;
712 		sinfo->txrate.nss = txrate->nss;
713 		sinfo->txrate.bw = txrate->bw;
714 		sinfo->txrate.he_gi = txrate->he_gi;
715 		sinfo->txrate.he_dcm = txrate->he_dcm;
716 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
717 	}
718 	sinfo->tx_failed = msta->deflink.wcid.stats.tx_failed;
719 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
720 
721 	sinfo->tx_retries = msta->deflink.wcid.stats.tx_retries;
722 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
723 
724 	sinfo->txrate.flags = txrate->flags;
725 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
726 
727 	sinfo->ack_signal = (s8)msta->deflink.ack_signal;
728 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
729 
730 	sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->deflink.avg_ack_signal);
731 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
732 }
733 EXPORT_SYMBOL_GPL(mt792x_sta_statistics);
734 
mt792x_set_coverage_class(struct ieee80211_hw * hw,int radio_idx,s16 coverage_class)735 void mt792x_set_coverage_class(struct ieee80211_hw *hw, int radio_idx,
736 			       s16 coverage_class)
737 {
738 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
739 	struct mt792x_dev *dev = phy->dev;
740 
741 	mt792x_mutex_acquire(dev);
742 
743 	phy->coverage_class = max_t(s16, coverage_class, 0);
744 	mt792x_mac_set_timeing(phy);
745 
746 	mt792x_mutex_release(dev);
747 }
748 EXPORT_SYMBOL_GPL(mt792x_set_coverage_class);
749 
mt792x_init_wiphy(struct ieee80211_hw * hw)750 int mt792x_init_wiphy(struct ieee80211_hw *hw)
751 {
752 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
753 	struct mt792x_dev *dev = phy->dev;
754 	struct wiphy *wiphy = hw->wiphy;
755 	int err;
756 
757 	hw->queues = 4;
758 	if (dev->has_eht) {
759 		hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
760 		hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
761 	} else {
762 		hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
763 		hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
764 	}
765 	hw->netdev_features = NETIF_F_RXCSUM;
766 
767 	hw->radiotap_timestamp.units_pos =
768 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US;
769 
770 	phy->slottime = 9;
771 
772 	hw->sta_data_size = sizeof(struct mt792x_sta);
773 	hw->vif_data_size = sizeof(struct mt792x_vif);
774 	hw->chanctx_data_size = sizeof(struct mt792x_chanctx);
775 
776 	if (dev->fw_features & MT792x_FW_CAP_CNM)
777 		wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
778 	else
779 		wiphy->flags &= ~WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
780 
781 	err = mt792x_setup_iface_combinations(dev, wiphy);
782 	if (err)
783 		return err;
784 
785 	wiphy->iface_combinations = dev->iface_combinations;
786 	wiphy->n_iface_combinations = dev->n_iface_combinations;
787 	wiphy->flags &= ~(WIPHY_FLAG_IBSS_RSN | WIPHY_FLAG_4ADDR_AP |
788 			  WIPHY_FLAG_4ADDR_STATION);
789 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
790 				 BIT(NL80211_IFTYPE_AP) |
791 				 BIT(NL80211_IFTYPE_P2P_CLIENT) |
792 				 BIT(NL80211_IFTYPE_P2P_GO) |
793 				 BIT(NL80211_IFTYPE_P2P_DEVICE);
794 
795 	if ((dev->fw_features & MT792x_FW_CAP_CNM) &&
796 	    (dev->fw_features & MT792x_FW_CAP_NAN)) {
797 		wiphy->interface_modes |= BIT(NL80211_IFTYPE_NAN) |
798 					  BIT(NL80211_IFTYPE_NAN_DATA);
799 		wiphy->nan_supported_bands = BIT(NL80211_BAND_2GHZ) |
800 					      BIT(NL80211_BAND_5GHZ);
801 		wiphy->nan_capa.flags = WIPHY_NAN_FLAGS_CONFIGURABLE_SYNC |
802 					WIPHY_NAN_FLAGS_USERSPACE_DE;
803 		wiphy->nan_capa.op_mode = NAN_OP_MODE_PHY_MODE_MASK;
804 		wiphy->nan_capa.n_antennas = 0x22;
805 		wiphy->nan_capa.max_channel_switch_time = 12;
806 		wiphy->nan_capa.dev_capabilities = NAN_DEV_CAPA_EXT_KEY_ID_SUPPORTED;
807 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_SECURE_NAN);
808 	}
809 
810 	wiphy->max_scan_ie_len = MT76_CONNAC_SCAN_IE_LEN;
811 	wiphy->max_scan_ssids = 4;
812 	wiphy->max_sched_scan_plan_interval =
813 		MT76_CONNAC_MAX_TIME_SCHED_SCAN_INTERVAL;
814 	wiphy->max_sched_scan_ie_len = IEEE80211_MAX_DATA_LEN;
815 	wiphy->max_sched_scan_ssids = MT76_CONNAC_MAX_SCHED_SCAN_SSID;
816 	wiphy->max_match_sets = MT76_CONNAC_MAX_SCAN_MATCH;
817 	wiphy->max_sched_scan_reqs = 1;
818 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH |
819 			WIPHY_FLAG_SPLIT_SCAN_6GHZ;
820 
821 	wiphy->features |= NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
822 			   NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
823 	phy->mt76->no_active_monitor = true;
824 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_SET_SCAN_DWELL);
825 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
826 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_HT);
827 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_VHT);
828 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_HE);
829 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
830 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CAN_REPLACE_PTK0);
831 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS);
832 
833 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
834 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
835 	ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
836 	ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
837 	if (is_mt7927(&dev->mt76))
838 		ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
839 	else
840 		ieee80211_hw_set(hw, WANT_MONITOR_VIF);
841 	ieee80211_hw_set(hw, SUPPORTS_PS);
842 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
843 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
844 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
845 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
846 	ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
847 
848 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
849 
850 
851 	if (dev->pm.enable)
852 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
853 
854 	hw->max_tx_fragments = 4;
855 
856 	return 0;
857 }
858 EXPORT_SYMBOL_GPL(mt792x_init_wiphy);
859 
860 static u8
mt792x_get_offload_capability(struct device * dev,const char * fw_wm)861 mt792x_get_offload_capability(struct device *dev, const char *fw_wm)
862 {
863 	const struct mt76_connac2_fw_trailer *hdr;
864 	struct mt792x_realease_info *rel_info;
865 	const struct firmware *fw;
866 	int ret, i, offset = 0;
867 	const u8 *data, *end;
868 	u8 offload_caps = 0;
869 
870 	ret = request_firmware(&fw, fw_wm, dev);
871 	if (ret)
872 		return ret;
873 
874 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
875 		dev_err(dev, "Invalid firmware\n");
876 		goto out;
877 	}
878 
879 	data = fw->data;
880 	hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
881 
882 	for (i = 0; i < hdr->n_region; i++) {
883 		const struct mt76_connac2_fw_region *region;
884 
885 		region = (const void *)((const u8 *)hdr -
886 					(hdr->n_region - i) * sizeof(*region));
887 		offset += le32_to_cpu(region->len);
888 	}
889 
890 	data += offset + 16;
891 	rel_info = (struct mt792x_realease_info *)data;
892 	data += sizeof(*rel_info);
893 	end = data + le16_to_cpu(rel_info->len);
894 
895 	while (data < end) {
896 		rel_info = (struct mt792x_realease_info *)data;
897 		data += sizeof(*rel_info);
898 
899 		if (rel_info->tag == MT792x_FW_TAG_FEATURE) {
900 			struct mt792x_fw_features *features;
901 
902 			features = (struct mt792x_fw_features *)data;
903 			offload_caps = features->data;
904 			break;
905 		}
906 
907 		data += le16_to_cpu(rel_info->len) + rel_info->pad_len;
908 	}
909 
910 out:
911 	release_firmware(fw);
912 
913 	return offload_caps;
914 }
915 
mt792x_needs_cnm_runtime(const void * drv_data)916 static bool mt792x_needs_cnm_runtime(const void *drv_data)
917 {
918 	const char *fw_wm = drv_data;
919 
920 	return fw_wm && !strcmp(fw_wm, MT7927_FIRMWARE_WM);
921 }
922 
923 struct ieee80211_ops *
mt792x_get_mac80211_ops(struct device * dev,const struct ieee80211_ops * mac80211_ops,void * drv_data,u8 * fw_features)924 mt792x_get_mac80211_ops(struct device *dev,
925 			const struct ieee80211_ops *mac80211_ops,
926 			void *drv_data, u8 *fw_features)
927 {
928 	struct ieee80211_ops *ops;
929 
930 	ops = devm_kmemdup(dev, mac80211_ops, sizeof(struct ieee80211_ops),
931 			   GFP_KERNEL);
932 	if (!ops)
933 		return NULL;
934 
935 	*fw_features = mt792x_get_offload_capability(dev, drv_data);
936 
937 	if (mt792x_needs_cnm_runtime(drv_data))
938 		*fw_features |= MT792x_FW_CAP_CNM;
939 
940 	if (!(*fw_features & MT792x_FW_CAP_CNM)) {
941 		ops->remain_on_channel = NULL;
942 		ops->cancel_remain_on_channel = NULL;
943 		ops->add_chanctx = ieee80211_emulate_add_chanctx;
944 		ops->remove_chanctx = ieee80211_emulate_remove_chanctx;
945 		ops->change_chanctx = ieee80211_emulate_change_chanctx;
946 		ops->switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx;
947 		ops->assign_vif_chanctx = NULL;
948 		ops->unassign_vif_chanctx = NULL;
949 		ops->mgd_prepare_tx = NULL;
950 		ops->mgd_complete_tx = NULL;
951 	}
952 	return ops;
953 }
954 EXPORT_SYMBOL_GPL(mt792x_get_mac80211_ops);
955 
mt792x_init_wcid(struct mt792x_dev * dev)956 int mt792x_init_wcid(struct mt792x_dev *dev)
957 {
958 	int idx;
959 
960 	/* Beacon and mgmt frames should occupy wcid 0 */
961 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT792x_WTBL_STA - 1);
962 	if (idx)
963 		return -ENOSPC;
964 
965 	dev->mt76.global_wcid.idx = idx;
966 	dev->mt76.global_wcid.hw_key_idx = -1;
967 	dev->mt76.global_wcid.tx_info |= MT_WCID_TX_INFO_SET;
968 	rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid);
969 
970 	return 0;
971 }
972 EXPORT_SYMBOL_GPL(mt792x_init_wcid);
973 
mt792x_mcu_drv_pmctrl(struct mt792x_dev * dev)974 int mt792x_mcu_drv_pmctrl(struct mt792x_dev *dev)
975 {
976 	struct mt76_phy *mphy = &dev->mt76.phy;
977 	struct mt76_connac_pm *pm = &dev->pm;
978 	int err = 0;
979 
980 	mutex_lock(&pm->mutex);
981 
982 	if (!test_bit(MT76_STATE_PM, &mphy->state))
983 		goto out;
984 
985 	err = __mt792x_mcu_drv_pmctrl(dev);
986 out:
987 	mutex_unlock(&pm->mutex);
988 
989 	if (err)
990 		mt792x_reset(&dev->mt76);
991 
992 	return err;
993 }
994 EXPORT_SYMBOL_GPL(mt792x_mcu_drv_pmctrl);
995 
mt792x_mcu_fw_pmctrl(struct mt792x_dev * dev)996 int mt792x_mcu_fw_pmctrl(struct mt792x_dev *dev)
997 {
998 	struct mt76_phy *mphy = &dev->mt76.phy;
999 	struct mt76_connac_pm *pm = &dev->pm;
1000 	int err = 0;
1001 
1002 	mutex_lock(&pm->mutex);
1003 
1004 	if (mt76_connac_skip_fw_pmctrl(mphy, pm))
1005 		goto out;
1006 
1007 	err = __mt792x_mcu_fw_pmctrl(dev);
1008 out:
1009 	mutex_unlock(&pm->mutex);
1010 
1011 	if (err)
1012 		mt792x_reset(&dev->mt76);
1013 
1014 	return err;
1015 }
1016 EXPORT_SYMBOL_GPL(mt792x_mcu_fw_pmctrl);
1017 
__mt792xe_mcu_drv_pmctrl(struct mt792x_dev * dev)1018 int __mt792xe_mcu_drv_pmctrl(struct mt792x_dev *dev)
1019 {
1020 	int i, err = 0;
1021 
1022 	for (i = 0; i < MT792x_DRV_OWN_RETRY_COUNT; i++) {
1023 		mt76_wr(dev, MT_CONN_ON_LPCTL, PCIE_LPCR_HOST_CLR_OWN);
1024 
1025 		if (dev->aspm_supported)
1026 			usleep_range(2000, 3000);
1027 
1028 		if (mt76_poll_msec_tick(dev, MT_CONN_ON_LPCTL,
1029 					PCIE_LPCR_HOST_OWN_SYNC, 0, 50, 1))
1030 			break;
1031 	}
1032 
1033 	if (i == MT792x_DRV_OWN_RETRY_COUNT) {
1034 		dev_err(dev->mt76.dev, "driver own failed\n");
1035 		err = -EIO;
1036 	}
1037 
1038 	return err;
1039 }
1040 EXPORT_SYMBOL_GPL(__mt792xe_mcu_drv_pmctrl);
1041 
mt792xe_mcu_drv_pmctrl(struct mt792x_dev * dev)1042 int mt792xe_mcu_drv_pmctrl(struct mt792x_dev *dev)
1043 {
1044 	struct mt76_phy *mphy = &dev->mt76.phy;
1045 	struct mt76_connac_pm *pm = &dev->pm;
1046 	int err;
1047 
1048 	err = __mt792xe_mcu_drv_pmctrl(dev);
1049 	if (err < 0)
1050 		goto out;
1051 
1052 	mt792x_wpdma_reinit_cond(dev);
1053 	clear_bit(MT76_STATE_PM, &mphy->state);
1054 
1055 	pm->stats.last_wake_event = jiffies;
1056 	pm->stats.doze_time += pm->stats.last_wake_event -
1057 			       pm->stats.last_doze_event;
1058 out:
1059 	return err;
1060 }
1061 EXPORT_SYMBOL_GPL(mt792xe_mcu_drv_pmctrl);
1062 
mt792xe_mcu_fw_pmctrl(struct mt792x_dev * dev)1063 int mt792xe_mcu_fw_pmctrl(struct mt792x_dev *dev)
1064 {
1065 	struct mt76_phy *mphy = &dev->mt76.phy;
1066 	struct mt76_connac_pm *pm = &dev->pm;
1067 	int i;
1068 
1069 	for (i = 0; i < MT792x_DRV_OWN_RETRY_COUNT; i++) {
1070 		mt76_wr(dev, MT_CONN_ON_LPCTL, PCIE_LPCR_HOST_SET_OWN);
1071 		if (mt76_poll_msec_tick(dev, MT_CONN_ON_LPCTL,
1072 					PCIE_LPCR_HOST_OWN_SYNC, 4, 50, 1))
1073 			break;
1074 	}
1075 
1076 	if (i == MT792x_DRV_OWN_RETRY_COUNT) {
1077 		dev_err(dev->mt76.dev, "firmware own failed\n");
1078 		clear_bit(MT76_STATE_PM, &mphy->state);
1079 		return -EIO;
1080 	}
1081 
1082 	pm->stats.last_doze_event = jiffies;
1083 	pm->stats.awake_time += pm->stats.last_doze_event -
1084 				pm->stats.last_wake_event;
1085 
1086 	return 0;
1087 }
1088 EXPORT_SYMBOL_GPL(mt792xe_mcu_fw_pmctrl);
1089 
mt792x_load_firmware(struct mt792x_dev * dev)1090 int mt792x_load_firmware(struct mt792x_dev *dev)
1091 {
1092 	int ret;
1093 
1094 	mt76_connac_mcu_restart(&dev->mt76);
1095 
1096 	if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC_FW_STATE,
1097 			    MT_TOP_MISC2_FW_PWR_ON, 1000))
1098 		dev_warn(dev->mt76.dev,
1099 			 "MCU is not ready for firmware download\n");
1100 
1101 	if (is_mt7928(&dev->mt76)) {
1102 		dev_info(dev->mt76.dev, "Loading CB firmware patch: %s\n",
1103 			 mt792x_cb_patch_name(dev));
1104 		ret = mt76_connac3_load_cb_patch(&dev->mt76, mt792x_cb_patch_name(dev));
1105 		if (ret)
1106 			return ret;
1107 	}
1108 
1109 	dev_info(dev->mt76.dev, "Loading firmware patch: %s\n", mt792x_patch_name(dev));
1110 	ret = mt76_connac2_load_patch(&dev->mt76, mt792x_patch_name(dev));
1111 	if (ret)
1112 		return ret;
1113 
1114 	if (mt76_is_sdio(&dev->mt76)) {
1115 		/* activate again */
1116 		ret = __mt792x_mcu_fw_pmctrl(dev);
1117 		if (!ret)
1118 			ret = __mt792x_mcu_drv_pmctrl(dev);
1119 	}
1120 
1121 	if (is_mt7928(&dev->mt76)) {
1122 		ret = mt76_connac3_load_phy_ram(&dev->mt76, mt792x_phy_ram_name(dev));
1123 		if (ret)
1124 			return ret;
1125 	}
1126 
1127 	ret = mt76_connac2_load_ram(&dev->mt76, mt792x_ram_name(dev), NULL);
1128 	if (ret)
1129 		return ret;
1130 
1131 	if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY,
1132 			    MT_TOP_MISC2_FW_N9_RDY, 1500)) {
1133 		dev_err(dev->mt76.dev, "Timeout for initializing firmware\n");
1134 
1135 		return -EIO;
1136 	}
1137 
1138 #ifdef CONFIG_PM
1139 	dev->mt76.hw->wiphy->wowlan = &mt76_connac_wowlan_support;
1140 #endif /* CONFIG_PM */
1141 
1142 	dev_dbg(dev->mt76.dev, "Firmware init done\n");
1143 
1144 	return 0;
1145 }
1146 EXPORT_SYMBOL_GPL(mt792x_load_firmware);
1147 
mt792x_config_mac_addr_list(struct mt792x_dev * dev)1148 void mt792x_config_mac_addr_list(struct mt792x_dev *dev)
1149 {
1150 	struct ieee80211_hw *hw = mt76_hw(dev);
1151 	struct wiphy *wiphy = hw->wiphy;
1152 	int i;
1153 
1154 	for (i = 0; i < ARRAY_SIZE(dev->macaddr_list); i++) {
1155 		u8 *addr = dev->macaddr_list[i].addr;
1156 
1157 		memcpy(addr, dev->mphy.macaddr, ETH_ALEN);
1158 
1159 		if (!i)
1160 			continue;
1161 
1162 		addr[0] |= BIT(1);
1163 		addr[0] ^= ((i - 1) << 2);
1164 	}
1165 	wiphy->addresses = dev->macaddr_list;
1166 	wiphy->n_addresses = ARRAY_SIZE(dev->macaddr_list);
1167 }
1168 EXPORT_SYMBOL_GPL(mt792x_config_mac_addr_list);
1169 
1170 MODULE_DESCRIPTION("MediaTek MT792x core driver");
1171 MODULE_LICENSE("Dual BSD/GPL");
1172 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
1173