xref: /linux/drivers/net/wireless/mediatek/mt76/mt792x_core.c (revision c7979c3917fa1326dae3607e1c6a04c12057b194)
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,s16 coverage_class)604 void mt792x_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
605 {
606 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
607 	struct mt792x_dev *dev = phy->dev;
608 
609 	mt792x_mutex_acquire(dev);
610 
611 	phy->coverage_class = max_t(s16, coverage_class, 0);
612 	mt792x_mac_set_timeing(phy);
613 
614 	mt792x_mutex_release(dev);
615 }
616 EXPORT_SYMBOL_GPL(mt792x_set_coverage_class);
617 
mt792x_init_wiphy(struct ieee80211_hw * hw)618 int mt792x_init_wiphy(struct ieee80211_hw *hw)
619 {
620 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
621 	struct mt792x_dev *dev = phy->dev;
622 	struct wiphy *wiphy = hw->wiphy;
623 
624 	hw->queues = 4;
625 	if (dev->has_eht) {
626 		hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
627 		hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
628 	} else {
629 		hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
630 		hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
631 	}
632 	hw->netdev_features = NETIF_F_RXCSUM;
633 
634 	hw->radiotap_timestamp.units_pos =
635 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US;
636 
637 	phy->slottime = 9;
638 
639 	hw->sta_data_size = sizeof(struct mt792x_sta);
640 	hw->vif_data_size = sizeof(struct mt792x_vif);
641 	hw->chanctx_data_size = sizeof(struct mt792x_chanctx);
642 
643 	if (dev->fw_features & MT792x_FW_CAP_CNM) {
644 		wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
645 		wiphy->iface_combinations = if_comb_chanctx;
646 		wiphy->n_iface_combinations = ARRAY_SIZE(if_comb_chanctx);
647 	} else {
648 		wiphy->flags &= ~WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
649 		wiphy->iface_combinations = if_comb;
650 		wiphy->n_iface_combinations = ARRAY_SIZE(if_comb);
651 	}
652 	wiphy->flags &= ~(WIPHY_FLAG_IBSS_RSN | WIPHY_FLAG_4ADDR_AP |
653 			  WIPHY_FLAG_4ADDR_STATION);
654 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
655 				 BIT(NL80211_IFTYPE_AP) |
656 				 BIT(NL80211_IFTYPE_P2P_CLIENT) |
657 				 BIT(NL80211_IFTYPE_P2P_GO) |
658 				 BIT(NL80211_IFTYPE_P2P_DEVICE);
659 	wiphy->max_remain_on_channel_duration = 5000;
660 	wiphy->max_scan_ie_len = MT76_CONNAC_SCAN_IE_LEN;
661 	wiphy->max_scan_ssids = 4;
662 	wiphy->max_sched_scan_plan_interval =
663 		MT76_CONNAC_MAX_TIME_SCHED_SCAN_INTERVAL;
664 	wiphy->max_sched_scan_ie_len = IEEE80211_MAX_DATA_LEN;
665 	wiphy->max_sched_scan_ssids = MT76_CONNAC_MAX_SCHED_SCAN_SSID;
666 	wiphy->max_match_sets = MT76_CONNAC_MAX_SCAN_MATCH;
667 	wiphy->max_sched_scan_reqs = 1;
668 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH |
669 			WIPHY_FLAG_SPLIT_SCAN_6GHZ;
670 
671 	wiphy->features |= NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
672 			   NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
673 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_SET_SCAN_DWELL);
674 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
675 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_HT);
676 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_VHT);
677 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_HE);
678 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
679 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CAN_REPLACE_PTK0);
680 
681 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
682 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
683 	ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
684 	ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
685 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
686 	ieee80211_hw_set(hw, SUPPORTS_PS);
687 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
688 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
689 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
690 	if (is_mt7921(&dev->mt76))
691 		ieee80211_hw_set(hw, CHANCTX_STA_CSA);
692 
693 	if (dev->pm.enable)
694 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
695 
696 	hw->max_tx_fragments = 4;
697 
698 	return 0;
699 }
700 EXPORT_SYMBOL_GPL(mt792x_init_wiphy);
701 
702 static u8
mt792x_get_offload_capability(struct device * dev,const char * fw_wm)703 mt792x_get_offload_capability(struct device *dev, const char *fw_wm)
704 {
705 	const struct mt76_connac2_fw_trailer *hdr;
706 	struct mt792x_realease_info *rel_info;
707 	const struct firmware *fw;
708 	int ret, i, offset = 0;
709 	const u8 *data, *end;
710 	u8 offload_caps = 0;
711 
712 	ret = request_firmware(&fw, fw_wm, dev);
713 	if (ret)
714 		return ret;
715 
716 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
717 		dev_err(dev, "Invalid firmware\n");
718 		goto out;
719 	}
720 
721 	data = fw->data;
722 	hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
723 
724 	for (i = 0; i < hdr->n_region; i++) {
725 		const struct mt76_connac2_fw_region *region;
726 
727 		region = (const void *)((const u8 *)hdr -
728 					(hdr->n_region - i) * sizeof(*region));
729 		offset += le32_to_cpu(region->len);
730 	}
731 
732 	data += offset + 16;
733 	rel_info = (struct mt792x_realease_info *)data;
734 	data += sizeof(*rel_info);
735 	end = data + le16_to_cpu(rel_info->len);
736 
737 	while (data < end) {
738 		rel_info = (struct mt792x_realease_info *)data;
739 		data += sizeof(*rel_info);
740 
741 		if (rel_info->tag == MT792x_FW_TAG_FEATURE) {
742 			struct mt792x_fw_features *features;
743 
744 			features = (struct mt792x_fw_features *)data;
745 			offload_caps = features->data;
746 			break;
747 		}
748 
749 		data += le16_to_cpu(rel_info->len) + rel_info->pad_len;
750 	}
751 
752 out:
753 	release_firmware(fw);
754 
755 	return offload_caps;
756 }
757 
758 struct ieee80211_ops *
mt792x_get_mac80211_ops(struct device * dev,const struct ieee80211_ops * mac80211_ops,void * drv_data,u8 * fw_features)759 mt792x_get_mac80211_ops(struct device *dev,
760 			const struct ieee80211_ops *mac80211_ops,
761 			void *drv_data, u8 *fw_features)
762 {
763 	struct ieee80211_ops *ops;
764 
765 	ops = devm_kmemdup(dev, mac80211_ops, sizeof(struct ieee80211_ops),
766 			   GFP_KERNEL);
767 	if (!ops)
768 		return NULL;
769 
770 	*fw_features = mt792x_get_offload_capability(dev, drv_data);
771 	if (!(*fw_features & MT792x_FW_CAP_CNM)) {
772 		ops->remain_on_channel = NULL;
773 		ops->cancel_remain_on_channel = NULL;
774 		ops->add_chanctx = ieee80211_emulate_add_chanctx;
775 		ops->remove_chanctx = ieee80211_emulate_remove_chanctx;
776 		ops->change_chanctx = ieee80211_emulate_change_chanctx;
777 		ops->switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx;
778 		ops->assign_vif_chanctx = NULL;
779 		ops->unassign_vif_chanctx = NULL;
780 		ops->mgd_prepare_tx = NULL;
781 		ops->mgd_complete_tx = NULL;
782 	}
783 	return ops;
784 }
785 EXPORT_SYMBOL_GPL(mt792x_get_mac80211_ops);
786 
mt792x_init_wcid(struct mt792x_dev * dev)787 int mt792x_init_wcid(struct mt792x_dev *dev)
788 {
789 	int idx;
790 
791 	/* Beacon and mgmt frames should occupy wcid 0 */
792 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT792x_WTBL_STA - 1);
793 	if (idx)
794 		return -ENOSPC;
795 
796 	dev->mt76.global_wcid.idx = idx;
797 	dev->mt76.global_wcid.hw_key_idx = -1;
798 	dev->mt76.global_wcid.tx_info |= MT_WCID_TX_INFO_SET;
799 	rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid);
800 
801 	return 0;
802 }
803 EXPORT_SYMBOL_GPL(mt792x_init_wcid);
804 
mt792x_mcu_drv_pmctrl(struct mt792x_dev * dev)805 int mt792x_mcu_drv_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 (!test_bit(MT76_STATE_PM, &mphy->state))
814 		goto out;
815 
816 	err = __mt792x_mcu_drv_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_drv_pmctrl);
826 
mt792x_mcu_fw_pmctrl(struct mt792x_dev * dev)827 int mt792x_mcu_fw_pmctrl(struct mt792x_dev *dev)
828 {
829 	struct mt76_phy *mphy = &dev->mt76.phy;
830 	struct mt76_connac_pm *pm = &dev->pm;
831 	int err = 0;
832 
833 	mutex_lock(&pm->mutex);
834 
835 	if (mt76_connac_skip_fw_pmctrl(mphy, pm))
836 		goto out;
837 
838 	err = __mt792x_mcu_fw_pmctrl(dev);
839 out:
840 	mutex_unlock(&pm->mutex);
841 
842 	if (err)
843 		mt792x_reset(&dev->mt76);
844 
845 	return err;
846 }
847 EXPORT_SYMBOL_GPL(mt792x_mcu_fw_pmctrl);
848 
__mt792xe_mcu_drv_pmctrl(struct mt792x_dev * dev)849 int __mt792xe_mcu_drv_pmctrl(struct mt792x_dev *dev)
850 {
851 	int i, err = 0;
852 
853 	for (i = 0; i < MT792x_DRV_OWN_RETRY_COUNT; i++) {
854 		mt76_wr(dev, MT_CONN_ON_LPCTL, PCIE_LPCR_HOST_CLR_OWN);
855 
856 		if (dev->aspm_supported)
857 			usleep_range(2000, 3000);
858 
859 		if (mt76_poll_msec_tick(dev, MT_CONN_ON_LPCTL,
860 					PCIE_LPCR_HOST_OWN_SYNC, 0, 50, 1))
861 			break;
862 	}
863 
864 	if (i == MT792x_DRV_OWN_RETRY_COUNT) {
865 		dev_err(dev->mt76.dev, "driver own failed\n");
866 		err = -EIO;
867 	}
868 
869 	return err;
870 }
871 EXPORT_SYMBOL_GPL(__mt792xe_mcu_drv_pmctrl);
872 
mt792xe_mcu_drv_pmctrl(struct mt792x_dev * dev)873 int mt792xe_mcu_drv_pmctrl(struct mt792x_dev *dev)
874 {
875 	struct mt76_phy *mphy = &dev->mt76.phy;
876 	struct mt76_connac_pm *pm = &dev->pm;
877 	int err;
878 
879 	err = __mt792xe_mcu_drv_pmctrl(dev);
880 	if (err < 0)
881 		goto out;
882 
883 	mt792x_wpdma_reinit_cond(dev);
884 	clear_bit(MT76_STATE_PM, &mphy->state);
885 
886 	pm->stats.last_wake_event = jiffies;
887 	pm->stats.doze_time += pm->stats.last_wake_event -
888 			       pm->stats.last_doze_event;
889 out:
890 	return err;
891 }
892 EXPORT_SYMBOL_GPL(mt792xe_mcu_drv_pmctrl);
893 
mt792xe_mcu_fw_pmctrl(struct mt792x_dev * dev)894 int mt792xe_mcu_fw_pmctrl(struct mt792x_dev *dev)
895 {
896 	struct mt76_phy *mphy = &dev->mt76.phy;
897 	struct mt76_connac_pm *pm = &dev->pm;
898 	int i;
899 
900 	for (i = 0; i < MT792x_DRV_OWN_RETRY_COUNT; i++) {
901 		mt76_wr(dev, MT_CONN_ON_LPCTL, PCIE_LPCR_HOST_SET_OWN);
902 		if (mt76_poll_msec_tick(dev, MT_CONN_ON_LPCTL,
903 					PCIE_LPCR_HOST_OWN_SYNC, 4, 50, 1))
904 			break;
905 	}
906 
907 	if (i == MT792x_DRV_OWN_RETRY_COUNT) {
908 		dev_err(dev->mt76.dev, "firmware own failed\n");
909 		clear_bit(MT76_STATE_PM, &mphy->state);
910 		return -EIO;
911 	}
912 
913 	pm->stats.last_doze_event = jiffies;
914 	pm->stats.awake_time += pm->stats.last_doze_event -
915 				pm->stats.last_wake_event;
916 
917 	return 0;
918 }
919 EXPORT_SYMBOL_GPL(mt792xe_mcu_fw_pmctrl);
920 
mt792x_load_firmware(struct mt792x_dev * dev)921 int mt792x_load_firmware(struct mt792x_dev *dev)
922 {
923 	int ret;
924 
925 	ret = mt76_connac2_load_patch(&dev->mt76, mt792x_patch_name(dev));
926 	if (ret)
927 		return ret;
928 
929 	if (mt76_is_sdio(&dev->mt76)) {
930 		/* activate again */
931 		ret = __mt792x_mcu_fw_pmctrl(dev);
932 		if (!ret)
933 			ret = __mt792x_mcu_drv_pmctrl(dev);
934 	}
935 
936 	ret = mt76_connac2_load_ram(&dev->mt76, mt792x_ram_name(dev), NULL);
937 	if (ret)
938 		return ret;
939 
940 	if (!mt76_poll_msec(dev, MT_CONN_ON_MISC, MT_TOP_MISC2_FW_N9_RDY,
941 			    MT_TOP_MISC2_FW_N9_RDY, 1500)) {
942 		dev_err(dev->mt76.dev, "Timeout for initializing firmware\n");
943 
944 		return -EIO;
945 	}
946 
947 #ifdef CONFIG_PM
948 	dev->mt76.hw->wiphy->wowlan = &mt76_connac_wowlan_support;
949 #endif /* CONFIG_PM */
950 
951 	dev_dbg(dev->mt76.dev, "Firmware init done\n");
952 
953 	return 0;
954 }
955 EXPORT_SYMBOL_GPL(mt792x_load_firmware);
956 
mt792x_config_mac_addr_list(struct mt792x_dev * dev)957 void mt792x_config_mac_addr_list(struct mt792x_dev *dev)
958 {
959 	struct ieee80211_hw *hw = mt76_hw(dev);
960 	struct wiphy *wiphy = hw->wiphy;
961 	int i;
962 
963 	for (i = 0; i < ARRAY_SIZE(dev->macaddr_list); i++) {
964 		u8 *addr = dev->macaddr_list[i].addr;
965 
966 		memcpy(addr, dev->mphy.macaddr, ETH_ALEN);
967 
968 		if (!i)
969 			continue;
970 
971 		addr[0] |= BIT(1);
972 		addr[0] ^= ((i - 1) << 2);
973 	}
974 	wiphy->addresses = dev->macaddr_list;
975 	wiphy->n_addresses = ARRAY_SIZE(dev->macaddr_list);
976 }
977 EXPORT_SYMBOL_GPL(mt792x_config_mac_addr_list);
978 
979 MODULE_DESCRIPTION("MediaTek MT792x core driver");
980 MODULE_LICENSE("Dual BSD/GPL");
981 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
982