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