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