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