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