1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3
4 #include <linux/firmware.h>
5 #include <linux/fs.h>
6 #include "mt7915.h"
7 #include "mcu.h"
8 #include "mac.h"
9 #include "eeprom.h"
10
11 struct mt7915_patch_hdr {
12 char build_date[16];
13 char platform[4];
14 __be32 hw_sw_ver;
15 __be32 patch_ver;
16 __be16 checksum;
17 u16 reserved;
18 struct {
19 __be32 patch_ver;
20 __be32 subsys;
21 __be32 feature;
22 __be32 n_region;
23 __be32 crc;
24 u32 reserved[11];
25 } desc;
26 } __packed;
27
28 struct mt7915_patch_sec {
29 __be32 type;
30 __be32 offs;
31 __be32 size;
32 union {
33 __be32 spec[13];
34 struct {
35 __be32 addr;
36 __be32 len;
37 __be32 sec_key_idx;
38 __be32 align_len;
39 u32 reserved[9];
40 } info;
41 };
42 } __packed;
43
44 struct mt7915_fw_trailer {
45 u8 chip_id;
46 u8 eco_code;
47 u8 n_region;
48 u8 format_ver;
49 u8 format_flag;
50 u8 reserved[2];
51 char fw_ver[10];
52 char build_date[15];
53 u32 crc;
54 } __packed;
55
56 struct mt7915_fw_region {
57 __le32 decomp_crc;
58 __le32 decomp_len;
59 __le32 decomp_blk_sz;
60 u8 reserved[4];
61 __le32 addr;
62 __le32 len;
63 u8 feature_set;
64 u8 reserved1[15];
65 } __packed;
66
67 #define MCU_PATCH_ADDRESS 0x200000
68
69 #define MT_STA_BFER BIT(0)
70 #define MT_STA_BFEE BIT(1)
71
72 #define FW_FEATURE_SET_ENCRYPT BIT(0)
73 #define FW_FEATURE_SET_KEY_IDX GENMASK(2, 1)
74 #define FW_FEATURE_OVERRIDE_ADDR BIT(5)
75
76 #define DL_MODE_ENCRYPT BIT(0)
77 #define DL_MODE_KEY_IDX GENMASK(2, 1)
78 #define DL_MODE_RESET_SEC_IV BIT(3)
79 #define DL_MODE_WORKING_PDA_CR4 BIT(4)
80 #define DL_MODE_NEED_RSP BIT(31)
81
82 #define FW_START_OVERRIDE BIT(0)
83 #define FW_START_WORKING_PDA_CR4 BIT(2)
84
85 #define PATCH_SEC_TYPE_MASK GENMASK(15, 0)
86 #define PATCH_SEC_TYPE_INFO 0x2
87
88 #define to_wcid_lo(id) FIELD_GET(GENMASK(7, 0), (u16)id)
89 #define to_wcid_hi(id) FIELD_GET(GENMASK(9, 8), (u16)id)
90
91 #define HE_PHY(p, c) u8_get_bits(c, IEEE80211_HE_PHY_##p)
92 #define HE_MAC(m, c) u8_get_bits(c, IEEE80211_HE_MAC_##m)
93
94 static enum mt7915_cipher_type
mt7915_mcu_get_cipher(int cipher)95 mt7915_mcu_get_cipher(int cipher)
96 {
97 switch (cipher) {
98 case WLAN_CIPHER_SUITE_WEP40:
99 return MT_CIPHER_WEP40;
100 case WLAN_CIPHER_SUITE_WEP104:
101 return MT_CIPHER_WEP104;
102 case WLAN_CIPHER_SUITE_TKIP:
103 return MT_CIPHER_TKIP;
104 case WLAN_CIPHER_SUITE_AES_CMAC:
105 return MT_CIPHER_BIP_CMAC_128;
106 case WLAN_CIPHER_SUITE_CCMP:
107 return MT_CIPHER_AES_CCMP;
108 case WLAN_CIPHER_SUITE_CCMP_256:
109 return MT_CIPHER_CCMP_256;
110 case WLAN_CIPHER_SUITE_GCMP:
111 return MT_CIPHER_GCMP;
112 case WLAN_CIPHER_SUITE_GCMP_256:
113 return MT_CIPHER_GCMP_256;
114 case WLAN_CIPHER_SUITE_SMS4:
115 return MT_CIPHER_WAPI;
116 default:
117 return MT_CIPHER_NONE;
118 }
119 }
120
mt7915_mcu_chan_bw(struct cfg80211_chan_def * chandef)121 static u8 mt7915_mcu_chan_bw(struct cfg80211_chan_def *chandef)
122 {
123 static const u8 width_to_bw[] = {
124 [NL80211_CHAN_WIDTH_40] = CMD_CBW_40MHZ,
125 [NL80211_CHAN_WIDTH_80] = CMD_CBW_80MHZ,
126 [NL80211_CHAN_WIDTH_80P80] = CMD_CBW_8080MHZ,
127 [NL80211_CHAN_WIDTH_160] = CMD_CBW_160MHZ,
128 [NL80211_CHAN_WIDTH_5] = CMD_CBW_5MHZ,
129 [NL80211_CHAN_WIDTH_10] = CMD_CBW_10MHZ,
130 [NL80211_CHAN_WIDTH_20] = CMD_CBW_20MHZ,
131 [NL80211_CHAN_WIDTH_20_NOHT] = CMD_CBW_20MHZ,
132 };
133
134 if (chandef->width >= ARRAY_SIZE(width_to_bw))
135 return 0;
136
137 return width_to_bw[chandef->width];
138 }
139
140 static const struct ieee80211_sta_he_cap *
mt7915_get_he_phy_cap(struct mt7915_phy * phy,struct ieee80211_vif * vif)141 mt7915_get_he_phy_cap(struct mt7915_phy *phy, struct ieee80211_vif *vif)
142 {
143 struct ieee80211_supported_band *sband;
144 enum nl80211_band band;
145
146 band = phy->mt76->chandef.chan->band;
147 sband = phy->mt76->hw->wiphy->bands[band];
148
149 return ieee80211_get_he_iftype_cap(sband, vif->type);
150 }
151
152 static u8
mt7915_get_phy_mode(struct mt7915_dev * dev,struct ieee80211_vif * vif,enum nl80211_band band,struct ieee80211_sta * sta)153 mt7915_get_phy_mode(struct mt7915_dev *dev, struct ieee80211_vif *vif,
154 enum nl80211_band band, struct ieee80211_sta *sta)
155 {
156 struct ieee80211_sta_ht_cap *ht_cap;
157 struct ieee80211_sta_vht_cap *vht_cap;
158 const struct ieee80211_sta_he_cap *he_cap;
159 u8 mode = 0;
160
161 if (sta) {
162 ht_cap = &sta->ht_cap;
163 vht_cap = &sta->vht_cap;
164 he_cap = &sta->he_cap;
165 } else {
166 struct ieee80211_supported_band *sband;
167 struct mt7915_phy *phy;
168 struct mt7915_vif *mvif;
169
170 mvif = (struct mt7915_vif *)vif->drv_priv;
171 phy = mvif->band_idx ? mt7915_ext_phy(dev) : &dev->phy;
172 sband = phy->mt76->hw->wiphy->bands[band];
173
174 ht_cap = &sband->ht_cap;
175 vht_cap = &sband->vht_cap;
176 he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
177 }
178
179 if (band == NL80211_BAND_2GHZ) {
180 mode |= PHY_MODE_B | PHY_MODE_G;
181
182 if (ht_cap->ht_supported)
183 mode |= PHY_MODE_GN;
184
185 if (he_cap && he_cap->has_he)
186 mode |= PHY_MODE_AX_24G;
187 } else if (band == NL80211_BAND_5GHZ) {
188 mode |= PHY_MODE_A;
189
190 if (ht_cap->ht_supported)
191 mode |= PHY_MODE_AN;
192
193 if (vht_cap->vht_supported)
194 mode |= PHY_MODE_AC;
195
196 if (he_cap && he_cap->has_he)
197 mode |= PHY_MODE_AX_5G;
198 }
199
200 return mode;
201 }
202
203 static u8
mt7915_mcu_get_sta_nss(u16 mcs_map)204 mt7915_mcu_get_sta_nss(u16 mcs_map)
205 {
206 u8 nss;
207
208 for (nss = 8; nss > 0; nss--) {
209 u8 nss_mcs = (mcs_map >> (2 * (nss - 1))) & 3;
210
211 if (nss_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED)
212 break;
213 }
214
215 return nss - 1;
216 }
217
__mt7915_mcu_msg_send(struct mt7915_dev * dev,struct sk_buff * skb,int cmd,int * wait_seq)218 static int __mt7915_mcu_msg_send(struct mt7915_dev *dev, struct sk_buff *skb,
219 int cmd, int *wait_seq)
220 {
221 struct mt7915_mcu_txd *mcu_txd;
222 u8 seq, pkt_fmt, qidx;
223 enum mt76_txq_id txq;
224 __le32 *txd;
225 u32 val;
226
227 seq = ++dev->mt76.mcu.msg_seq & 0xf;
228 if (!seq)
229 seq = ++dev->mt76.mcu.msg_seq & 0xf;
230
231 if (cmd == -MCU_CMD_FW_SCATTER) {
232 txq = MT_TXQ_FWDL;
233 goto exit;
234 }
235
236 mcu_txd = (struct mt7915_mcu_txd *)skb_push(skb, sizeof(*mcu_txd));
237
238 if (test_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state)) {
239 txq = MT_TXQ_MCU_WA;
240 qidx = MT_TX_MCU_PORT_RX_Q0;
241 pkt_fmt = MT_TX_TYPE_CMD;
242 } else {
243 txq = MT_TXQ_MCU;
244 qidx = MT_TX_MCU_PORT_RX_Q0;
245 pkt_fmt = MT_TX_TYPE_CMD;
246 }
247
248 txd = mcu_txd->txd;
249
250 val = FIELD_PREP(MT_TXD0_TX_BYTES, skb->len) |
251 FIELD_PREP(MT_TXD0_PKT_FMT, pkt_fmt) |
252 FIELD_PREP(MT_TXD0_Q_IDX, qidx);
253 txd[0] = cpu_to_le32(val);
254
255 val = MT_TXD1_LONG_FORMAT |
256 FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_CMD);
257 txd[1] = cpu_to_le32(val);
258
259 mcu_txd->len = cpu_to_le16(skb->len - sizeof(mcu_txd->txd));
260 mcu_txd->pq_id = cpu_to_le16(MCU_PQ_ID(MT_TX_PORT_IDX_MCU, qidx));
261 mcu_txd->pkt_type = MCU_PKT_ID;
262 mcu_txd->seq = seq;
263
264 if (cmd < 0) {
265 mcu_txd->set_query = MCU_Q_NA;
266 mcu_txd->cid = -cmd;
267 } else {
268 mcu_txd->cid = MCU_CMD_EXT_CID;
269 mcu_txd->ext_cid = cmd;
270 mcu_txd->ext_cid_ack = 1;
271
272 /* do not use Q_SET for efuse */
273 if (cmd == MCU_EXT_CMD_EFUSE_ACCESS)
274 mcu_txd->set_query = MCU_Q_QUERY;
275 else
276 mcu_txd->set_query = MCU_Q_SET;
277 }
278
279 mcu_txd->s2d_index = MCU_S2D_H2N;
280 WARN_ON(cmd == MCU_EXT_CMD_EFUSE_ACCESS &&
281 mcu_txd->set_query != MCU_Q_QUERY);
282
283 exit:
284 if (wait_seq)
285 *wait_seq = seq;
286
287 return mt76_tx_queue_skb_raw(dev, txq, skb, 0);
288 }
289
290 static int
mt7915_mcu_parse_eeprom(struct mt7915_dev * dev,struct sk_buff * skb)291 mt7915_mcu_parse_eeprom(struct mt7915_dev *dev, struct sk_buff *skb)
292 {
293 struct mt7915_mcu_eeprom_info *res;
294 u8 *buf;
295
296 if (!skb)
297 return -EINVAL;
298
299 skb_pull(skb, sizeof(struct mt7915_mcu_rxd));
300
301 res = (struct mt7915_mcu_eeprom_info *)skb->data;
302 buf = dev->mt76.eeprom.data + le32_to_cpu(res->addr);
303 memcpy(buf, res->data, 16);
304
305 return 0;
306 }
307
308 static int
mt7915_mcu_parse_response(struct mt7915_dev * dev,int cmd,struct sk_buff * skb,int seq)309 mt7915_mcu_parse_response(struct mt7915_dev *dev, int cmd,
310 struct sk_buff *skb, int seq)
311 {
312 struct mt7915_mcu_rxd *rxd = (struct mt7915_mcu_rxd *)skb->data;
313 int ret = 0;
314
315 if (seq != rxd->seq) {
316 ret = -EAGAIN;
317 goto out;
318 }
319
320 switch (cmd) {
321 case -MCU_CMD_PATCH_SEM_CONTROL:
322 skb_pull(skb, sizeof(*rxd) - 4);
323 ret = *skb->data;
324 break;
325 case MCU_EXT_CMD_THERMAL_CTRL:
326 skb_pull(skb, sizeof(*rxd) + 4);
327 ret = le32_to_cpu(*(__le32 *)skb->data);
328 break;
329 case MCU_EXT_CMD_EFUSE_ACCESS:
330 ret = mt7915_mcu_parse_eeprom(dev, skb);
331 break;
332 default:
333 break;
334 }
335 out:
336 dev_kfree_skb(skb);
337
338 return ret;
339 }
340
341 static int
mt7915_mcu_wait_response(struct mt7915_dev * dev,int cmd,int seq)342 mt7915_mcu_wait_response(struct mt7915_dev *dev, int cmd, int seq)
343 {
344 unsigned long expires = jiffies + 20 * HZ;
345 struct sk_buff *skb;
346 int ret = 0;
347
348 while (true) {
349 skb = mt76_mcu_get_response(&dev->mt76, expires);
350 if (!skb) {
351 dev_err(dev->mt76.dev, "Message %d (seq %d) timeout\n",
352 cmd, seq);
353 return -ETIMEDOUT;
354 }
355
356 ret = mt7915_mcu_parse_response(dev, cmd, skb, seq);
357 if (ret != -EAGAIN)
358 break;
359 }
360
361 return ret;
362 }
363
364 static int
mt7915_mcu_send_message(struct mt76_dev * mdev,struct sk_buff * skb,int cmd,bool wait_resp)365 mt7915_mcu_send_message(struct mt76_dev *mdev, struct sk_buff *skb,
366 int cmd, bool wait_resp)
367 {
368 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
369 int ret, seq;
370
371 mutex_lock(&mdev->mcu.mutex);
372
373 ret = __mt7915_mcu_msg_send(dev, skb, cmd, &seq);
374 if (ret)
375 goto out;
376
377 if (wait_resp)
378 ret = mt7915_mcu_wait_response(dev, cmd, seq);
379
380 out:
381 mutex_unlock(&mdev->mcu.mutex);
382
383 return ret;
384 }
385
386 static int
mt7915_mcu_msg_send(struct mt76_dev * mdev,int cmd,const void * data,int len,bool wait_resp)387 mt7915_mcu_msg_send(struct mt76_dev *mdev, int cmd, const void *data,
388 int len, bool wait_resp)
389 {
390 struct sk_buff *skb;
391
392 skb = mt76_mcu_msg_alloc(mdev, data, len);
393 if (!skb)
394 return -ENOMEM;
395
396 return __mt76_mcu_skb_send_msg(mdev, skb, cmd, wait_resp);
397 }
398
399 static void
mt7915_mcu_csa_finish(void * priv,u8 * mac,struct ieee80211_vif * vif)400 mt7915_mcu_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif)
401 {
402 if (vif->csa_active)
403 ieee80211_csa_finish(vif);
404 }
405
406 static void
mt7915_mcu_rx_radar_detected(struct mt7915_dev * dev,struct sk_buff * skb)407 mt7915_mcu_rx_radar_detected(struct mt7915_dev *dev, struct sk_buff *skb)
408 {
409 struct mt76_phy *mphy = &dev->mt76.phy;
410 struct mt7915_mcu_rdd_report *r;
411
412 r = (struct mt7915_mcu_rdd_report *)skb->data;
413
414 if (r->idx && dev->mt76.phy2)
415 mphy = dev->mt76.phy2;
416
417 ieee80211_radar_detected(mphy->hw);
418 dev->hw_pattern++;
419 }
420
421 static void
mt7915_mcu_tx_rate_cal(struct mt76_phy * mphy,struct mt7915_mcu_ra_info * ra,struct rate_info * rate,u16 r)422 mt7915_mcu_tx_rate_cal(struct mt76_phy *mphy, struct mt7915_mcu_ra_info *ra,
423 struct rate_info *rate, u16 r)
424 {
425 struct ieee80211_supported_band *sband;
426 u16 ru_idx = le16_to_cpu(ra->ru_idx);
427 u16 flags = 0;
428
429 rate->mcs = FIELD_GET(MT_RA_RATE_MCS, r);
430 rate->nss = FIELD_GET(MT_RA_RATE_NSS, r) + 1;
431
432 switch (FIELD_GET(MT_RA_RATE_TX_MODE, r)) {
433 case MT_PHY_TYPE_CCK:
434 case MT_PHY_TYPE_OFDM:
435 if (mphy->chandef.chan->band == NL80211_BAND_5GHZ)
436 sband = &mphy->sband_5g.sband;
437 else
438 sband = &mphy->sband_2g.sband;
439
440 rate->legacy = sband->bitrates[rate->mcs].bitrate;
441 break;
442 case MT_PHY_TYPE_HT:
443 case MT_PHY_TYPE_HT_GF:
444 rate->mcs += (rate->nss - 1) * 8;
445 flags |= RATE_INFO_FLAGS_MCS;
446
447 if (ra->gi)
448 flags |= RATE_INFO_FLAGS_SHORT_GI;
449 break;
450 case MT_PHY_TYPE_VHT:
451 flags |= RATE_INFO_FLAGS_VHT_MCS;
452
453 if (ra->gi)
454 flags |= RATE_INFO_FLAGS_SHORT_GI;
455 break;
456 case MT_PHY_TYPE_HE_SU:
457 case MT_PHY_TYPE_HE_EXT_SU:
458 case MT_PHY_TYPE_HE_TB:
459 case MT_PHY_TYPE_HE_MU:
460 rate->he_gi = ra->gi;
461 rate->he_dcm = FIELD_GET(MT_RA_RATE_DCM_EN, r);
462
463 flags |= RATE_INFO_FLAGS_HE_MCS;
464 break;
465 default:
466 break;
467 }
468 rate->flags = flags;
469
470 if (ru_idx) {
471 switch (ru_idx) {
472 case 1 ... 2:
473 rate->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_996;
474 break;
475 case 3 ... 6:
476 rate->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_484;
477 break;
478 case 7 ... 14:
479 rate->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_242;
480 break;
481 default:
482 rate->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
483 break;
484 }
485 rate->bw = RATE_INFO_BW_HE_RU;
486 } else {
487 u8 bw = mt7915_mcu_chan_bw(&mphy->chandef) -
488 FIELD_GET(MT_RA_RATE_BW, r);
489
490 switch (bw) {
491 case IEEE80211_STA_RX_BW_160:
492 rate->bw = RATE_INFO_BW_160;
493 break;
494 case IEEE80211_STA_RX_BW_80:
495 rate->bw = RATE_INFO_BW_80;
496 break;
497 case IEEE80211_STA_RX_BW_40:
498 rate->bw = RATE_INFO_BW_40;
499 break;
500 default:
501 rate->bw = RATE_INFO_BW_20;
502 break;
503 }
504 }
505 }
506
507 static void
mt7915_mcu_tx_rate_report(struct mt7915_dev * dev,struct sk_buff * skb)508 mt7915_mcu_tx_rate_report(struct mt7915_dev *dev, struct sk_buff *skb)
509 {
510 struct mt7915_mcu_ra_info *ra = (struct mt7915_mcu_ra_info *)skb->data;
511 struct rate_info rate = {}, prob_rate = {};
512 u16 probe = le16_to_cpu(ra->prob_up_rate);
513 u16 attempts = le16_to_cpu(ra->attempts);
514 u16 curr = le16_to_cpu(ra->curr_rate);
515 u16 wcidx = le16_to_cpu(ra->wlan_idx);
516 struct mt76_phy *mphy = &dev->mphy;
517 struct mt7915_sta_stats *stats;
518 struct mt7915_sta *msta;
519 struct mt76_wcid *wcid;
520
521 if (wcidx >= MT76_N_WCIDS)
522 return;
523
524 wcid = rcu_dereference(dev->mt76.wcid[wcidx]);
525 if (!wcid)
526 return;
527
528 msta = container_of(wcid, struct mt7915_sta, wcid);
529 stats = &msta->stats;
530
531 if (msta->wcid.ext_phy && dev->mt76.phy2)
532 mphy = dev->mt76.phy2;
533
534 /* current rate */
535 mt7915_mcu_tx_rate_cal(mphy, ra, &rate, curr);
536 stats->tx_rate = rate;
537
538 /* probing rate */
539 mt7915_mcu_tx_rate_cal(mphy, ra, &prob_rate, probe);
540 stats->prob_rate = prob_rate;
541
542 if (attempts) {
543 u16 success = le16_to_cpu(ra->success);
544
545 stats->per = 1000 * (attempts - success) / attempts;
546 }
547 }
548
549 static void
mt7915_mcu_rx_log_message(struct mt7915_dev * dev,struct sk_buff * skb)550 mt7915_mcu_rx_log_message(struct mt7915_dev *dev, struct sk_buff *skb)
551 {
552 struct mt7915_mcu_rxd *rxd = (struct mt7915_mcu_rxd *)skb->data;
553 const char *data = (char *)&rxd[1];
554 const char *type;
555
556 switch (rxd->s2d_index) {
557 case 0:
558 type = "WM";
559 break;
560 case 2:
561 type = "WA";
562 break;
563 default:
564 type = "unknown";
565 break;
566 }
567
568 wiphy_info(mt76_hw(dev)->wiphy, "%s: %s", type, data);
569 }
570
571 static void
mt7915_mcu_rx_ext_event(struct mt7915_dev * dev,struct sk_buff * skb)572 mt7915_mcu_rx_ext_event(struct mt7915_dev *dev, struct sk_buff *skb)
573 {
574 struct mt7915_mcu_rxd *rxd = (struct mt7915_mcu_rxd *)skb->data;
575
576 switch (rxd->ext_eid) {
577 case MCU_EXT_EVENT_RDD_REPORT:
578 mt7915_mcu_rx_radar_detected(dev, skb);
579 break;
580 case MCU_EXT_EVENT_CSA_NOTIFY:
581 ieee80211_iterate_active_interfaces_atomic(dev->mt76.hw,
582 IEEE80211_IFACE_ITER_RESUME_ALL,
583 mt7915_mcu_csa_finish, dev);
584 break;
585 case MCU_EXT_EVENT_RATE_REPORT:
586 mt7915_mcu_tx_rate_report(dev, skb);
587 break;
588 case MCU_EXT_EVENT_FW_LOG_2_HOST:
589 mt7915_mcu_rx_log_message(dev, skb);
590 break;
591 default:
592 break;
593 }
594 }
595
596 static void
mt7915_mcu_rx_unsolicited_event(struct mt7915_dev * dev,struct sk_buff * skb)597 mt7915_mcu_rx_unsolicited_event(struct mt7915_dev *dev, struct sk_buff *skb)
598 {
599 struct mt7915_mcu_rxd *rxd = (struct mt7915_mcu_rxd *)skb->data;
600
601 switch (rxd->eid) {
602 case MCU_EVENT_EXT:
603 mt7915_mcu_rx_ext_event(dev, skb);
604 break;
605 default:
606 break;
607 }
608 dev_kfree_skb(skb);
609 }
610
mt7915_mcu_rx_event(struct mt7915_dev * dev,struct sk_buff * skb)611 void mt7915_mcu_rx_event(struct mt7915_dev *dev, struct sk_buff *skb)
612 {
613 struct mt7915_mcu_rxd *rxd = (struct mt7915_mcu_rxd *)skb->data;
614
615 if (rxd->ext_eid == MCU_EXT_EVENT_THERMAL_PROTECT ||
616 rxd->ext_eid == MCU_EXT_EVENT_FW_LOG_2_HOST ||
617 rxd->ext_eid == MCU_EXT_EVENT_ASSERT_DUMP ||
618 rxd->ext_eid == MCU_EXT_EVENT_PS_SYNC ||
619 rxd->ext_eid == MCU_EXT_EVENT_RATE_REPORT ||
620 !rxd->seq)
621 mt7915_mcu_rx_unsolicited_event(dev, skb);
622 else
623 mt76_mcu_rx_event(&dev->mt76, skb);
624 }
625
626 static struct sk_buff *
mt7915_mcu_alloc_sta_req(struct mt7915_dev * dev,struct mt7915_vif * mvif,struct mt7915_sta * msta,int len)627 mt7915_mcu_alloc_sta_req(struct mt7915_dev *dev, struct mt7915_vif *mvif,
628 struct mt7915_sta *msta, int len)
629 {
630 struct sta_req_hdr hdr = {
631 .bss_idx = mvif->idx,
632 .wlan_idx_lo = msta ? to_wcid_lo(msta->wcid.idx) : 0,
633 .wlan_idx_hi = msta ? to_wcid_hi(msta->wcid.idx) : 0,
634 .muar_idx = msta && msta->wcid.sta ? mvif->omac_idx : 0xe,
635 .is_tlv_append = 1,
636 };
637 struct sk_buff *skb;
638
639 skb = mt76_mcu_msg_alloc(&dev->mt76, NULL, len);
640 if (!skb)
641 return ERR_PTR(-ENOMEM);
642
643 skb_put_data(skb, &hdr, sizeof(hdr));
644
645 return skb;
646 }
647
648 static struct wtbl_req_hdr *
mt7915_mcu_alloc_wtbl_req(struct mt7915_dev * dev,struct mt7915_sta * msta,int cmd,void * sta_wtbl,struct sk_buff ** skb)649 mt7915_mcu_alloc_wtbl_req(struct mt7915_dev *dev, struct mt7915_sta *msta,
650 int cmd, void *sta_wtbl, struct sk_buff **skb)
651 {
652 struct tlv *sta_hdr = sta_wtbl;
653 struct wtbl_req_hdr hdr = {
654 .wlan_idx_lo = to_wcid_lo(msta->wcid.idx),
655 .wlan_idx_hi = to_wcid_hi(msta->wcid.idx),
656 .operation = cmd,
657 };
658 struct sk_buff *nskb = *skb;
659
660 if (!nskb) {
661 nskb = mt76_mcu_msg_alloc(&dev->mt76, NULL,
662 MT7915_WTBL_UPDATE_BA_SIZE);
663 if (!nskb)
664 return ERR_PTR(-ENOMEM);
665
666 *skb = nskb;
667 }
668
669 if (sta_hdr)
670 le16_add_cpu(&sta_hdr->len, sizeof(hdr));
671
672 return skb_put_data(nskb, &hdr, sizeof(hdr));
673 }
674
675 static struct tlv *
mt7915_mcu_add_nested_tlv(struct sk_buff * skb,int tag,int len,void * sta_ntlv,void * sta_wtbl)676 mt7915_mcu_add_nested_tlv(struct sk_buff *skb, int tag, int len,
677 void *sta_ntlv, void *sta_wtbl)
678 {
679 struct sta_ntlv_hdr *ntlv_hdr = sta_ntlv;
680 struct tlv *sta_hdr = sta_wtbl;
681 struct tlv *ptlv, tlv = {
682 .tag = cpu_to_le16(tag),
683 .len = cpu_to_le16(len),
684 };
685 u16 ntlv;
686
687 ptlv = skb_put(skb, len);
688 memcpy(ptlv, &tlv, sizeof(tlv));
689
690 ntlv = le16_to_cpu(ntlv_hdr->tlv_num);
691 ntlv_hdr->tlv_num = cpu_to_le16(ntlv + 1);
692
693 if (sta_hdr) {
694 u16 size = le16_to_cpu(sta_hdr->len);
695
696 sta_hdr->len = cpu_to_le16(size + len);
697 }
698
699 return ptlv;
700 }
701
702 static struct tlv *
mt7915_mcu_add_tlv(struct sk_buff * skb,int tag,int len)703 mt7915_mcu_add_tlv(struct sk_buff *skb, int tag, int len)
704 {
705 return mt7915_mcu_add_nested_tlv(skb, tag, len, skb->data, NULL);
706 }
707
708 static struct tlv *
mt7915_mcu_add_nested_subtlv(struct sk_buff * skb,int sub_tag,int sub_len,__le16 * sub_ntlv,__le16 * len)709 mt7915_mcu_add_nested_subtlv(struct sk_buff *skb, int sub_tag, int sub_len,
710 __le16 *sub_ntlv, __le16 *len)
711 {
712 struct tlv *ptlv, tlv = {
713 .tag = cpu_to_le16(sub_tag),
714 .len = cpu_to_le16(sub_len),
715 };
716
717 ptlv = skb_put(skb, sub_len);
718 memcpy(ptlv, &tlv, sizeof(tlv));
719
720 le16_add_cpu(sub_ntlv, 1);
721 le16_add_cpu(len, sub_len);
722
723 return ptlv;
724 }
725
726 /** bss info **/
727 static int
mt7915_mcu_bss_basic_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct mt7915_phy * phy,bool enable)728 mt7915_mcu_bss_basic_tlv(struct sk_buff *skb, struct ieee80211_vif *vif,
729 struct mt7915_phy *phy, bool enable)
730 {
731 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
732 struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
733 enum nl80211_band band = chandef->chan->band;
734 struct bss_info_basic *bss;
735 u16 wlan_idx = mvif->sta.wcid.idx;
736 u32 type = NETWORK_INFRA;
737 struct tlv *tlv;
738
739 tlv = mt7915_mcu_add_tlv(skb, BSS_INFO_BASIC, sizeof(*bss));
740
741 switch (vif->type) {
742 case NL80211_IFTYPE_MESH_POINT:
743 case NL80211_IFTYPE_AP:
744 break;
745 case NL80211_IFTYPE_STATION:
746 /* TODO: enable BSS_INFO_UAPSD & BSS_INFO_PM */
747 if (enable) {
748 struct ieee80211_sta *sta;
749 struct mt7915_sta *msta;
750
751 rcu_read_lock();
752 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
753 if (!sta) {
754 rcu_read_unlock();
755 return -EINVAL;
756 }
757
758 msta = (struct mt7915_sta *)sta->drv_priv;
759 wlan_idx = msta->wcid.idx;
760 rcu_read_unlock();
761 }
762 break;
763 case NL80211_IFTYPE_ADHOC:
764 type = NETWORK_IBSS;
765 break;
766 default:
767 WARN_ON(1);
768 break;
769 }
770
771 bss = (struct bss_info_basic *)tlv;
772 memcpy(bss->bssid, vif->bss_conf.bssid, ETH_ALEN);
773 bss->bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int);
774 bss->network_type = cpu_to_le32(type);
775 bss->dtim_period = vif->bss_conf.dtim_period;
776 bss->bmc_wcid_lo = to_wcid_lo(wlan_idx);
777 bss->bmc_wcid_hi = to_wcid_hi(wlan_idx);
778 bss->phy_mode = mt7915_get_phy_mode(phy->dev, vif, band, NULL);
779 bss->wmm_idx = mvif->wmm_idx;
780 bss->active = enable;
781
782 return 0;
783 }
784
785 static void
mt7915_mcu_bss_omac_tlv(struct sk_buff * skb,struct ieee80211_vif * vif)786 mt7915_mcu_bss_omac_tlv(struct sk_buff *skb, struct ieee80211_vif *vif)
787 {
788 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
789 struct bss_info_omac *omac;
790 struct tlv *tlv;
791 u32 type = 0;
792 u8 idx;
793
794 tlv = mt7915_mcu_add_tlv(skb, BSS_INFO_OMAC, sizeof(*omac));
795
796 switch (vif->type) {
797 case NL80211_IFTYPE_MESH_POINT:
798 case NL80211_IFTYPE_AP:
799 type = CONNECTION_INFRA_AP;
800 break;
801 case NL80211_IFTYPE_STATION:
802 type = CONNECTION_INFRA_STA;
803 break;
804 case NL80211_IFTYPE_ADHOC:
805 type = CONNECTION_IBSS_ADHOC;
806 break;
807 default:
808 WARN_ON(1);
809 break;
810 }
811
812 omac = (struct bss_info_omac *)tlv;
813 idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
814 omac->conn_type = cpu_to_le32(type);
815 omac->omac_idx = mvif->omac_idx;
816 omac->band_idx = mvif->band_idx;
817 omac->hw_bss_idx = idx;
818 }
819
820 struct mt7915_he_obss_narrow_bw_ru_data {
821 bool tolerated;
822 };
823
mt7915_check_he_obss_narrow_bw_ru_iter(struct wiphy * wiphy,struct cfg80211_bss * bss,void * _data)824 static void mt7915_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
825 struct cfg80211_bss *bss,
826 void *_data)
827 {
828 struct mt7915_he_obss_narrow_bw_ru_data *data = _data;
829 const struct element *elem;
830
831 elem = ieee80211_bss_get_elem(bss, WLAN_EID_EXT_CAPABILITY);
832
833 if (!elem || elem->datalen <= 10 ||
834 !(elem->data[10] &
835 WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT))
836 data->tolerated = false;
837 }
838
mt7915_check_he_obss_narrow_bw_ru(struct ieee80211_hw * hw,struct ieee80211_vif * vif)839 static bool mt7915_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
840 struct ieee80211_vif *vif)
841 {
842 struct mt7915_he_obss_narrow_bw_ru_data iter_data = {
843 .tolerated = true,
844 };
845
846 if (!(vif->bss_conf.chandef.chan->flags & IEEE80211_CHAN_RADAR))
847 return false;
848
849 cfg80211_bss_iter(hw->wiphy, &vif->bss_conf.chandef,
850 mt7915_check_he_obss_narrow_bw_ru_iter,
851 &iter_data);
852
853 /*
854 * If there is at least one AP on radar channel that cannot
855 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
856 */
857 return !iter_data.tolerated;
858 }
859
860 static void
mt7915_mcu_bss_rfch_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct mt7915_phy * phy)861 mt7915_mcu_bss_rfch_tlv(struct sk_buff *skb, struct ieee80211_vif *vif,
862 struct mt7915_phy *phy)
863 {
864 struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
865 struct bss_info_rf_ch *ch;
866 struct tlv *tlv;
867 int freq1 = chandef->center_freq1;
868
869 tlv = mt7915_mcu_add_tlv(skb, BSS_INFO_RF_CH, sizeof(*ch));
870
871 ch = (struct bss_info_rf_ch *)tlv;
872 ch->pri_ch = chandef->chan->hw_value;
873 ch->center_ch0 = ieee80211_frequency_to_channel(freq1);
874 ch->bw = mt7915_mcu_chan_bw(chandef);
875
876 if (chandef->width == NL80211_CHAN_WIDTH_80P80) {
877 int freq2 = chandef->center_freq2;
878
879 ch->center_ch1 = ieee80211_frequency_to_channel(freq2);
880 }
881
882 if (vif->bss_conf.he_support && vif->type == NL80211_IFTYPE_STATION) {
883 struct mt7915_dev *dev = phy->dev;
884 struct mt76_phy *mphy = &dev->mt76.phy;
885 bool ext_phy = phy != &dev->phy;
886
887 if (ext_phy && dev->mt76.phy2)
888 mphy = dev->mt76.phy2;
889
890 ch->he_ru26_block =
891 mt7915_check_he_obss_narrow_bw_ru(mphy->hw, vif);
892 ch->he_all_disable = false;
893 } else {
894 ch->he_all_disable = true;
895 }
896 }
897
898 static void
mt7915_mcu_bss_ra_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct mt7915_phy * phy)899 mt7915_mcu_bss_ra_tlv(struct sk_buff *skb, struct ieee80211_vif *vif,
900 struct mt7915_phy *phy)
901 {
902 struct bss_info_ra *ra;
903 struct tlv *tlv;
904 int max_nss = hweight8(phy->chainmask);
905
906 tlv = mt7915_mcu_add_tlv(skb, BSS_INFO_RA, sizeof(*ra));
907
908 ra = (struct bss_info_ra *)tlv;
909 ra->op_mode = vif->type == NL80211_IFTYPE_AP;
910 ra->adhoc_en = vif->type == NL80211_IFTYPE_ADHOC;
911 ra->short_preamble = true;
912 ra->tx_streams = max_nss;
913 ra->rx_streams = max_nss;
914 ra->algo = 4;
915 ra->train_up_rule = 2;
916 ra->train_up_high_thres = 110;
917 ra->train_up_rule_rssi = -70;
918 ra->low_traffic_thres = 2;
919 ra->phy_cap = cpu_to_le32(0xfdf);
920 ra->interval = cpu_to_le32(500);
921 ra->fast_interval = cpu_to_le32(100);
922 }
923
924 static void
mt7915_mcu_bss_he_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct mt7915_phy * phy)925 mt7915_mcu_bss_he_tlv(struct sk_buff *skb, struct ieee80211_vif *vif,
926 struct mt7915_phy *phy)
927 {
928 #define DEFAULT_HE_PE_DURATION 4
929 #define DEFAULT_HE_DURATION_RTS_THRES 1023
930 const struct ieee80211_sta_he_cap *cap;
931 struct bss_info_he *he;
932 struct tlv *tlv;
933
934 cap = mt7915_get_he_phy_cap(phy, vif);
935
936 tlv = mt7915_mcu_add_tlv(skb, BSS_INFO_HE_BASIC, sizeof(*he));
937
938 he = (struct bss_info_he *)tlv;
939 he->he_pe_duration = vif->bss_conf.htc_trig_based_pkt_ext;
940 if (!he->he_pe_duration)
941 he->he_pe_duration = DEFAULT_HE_PE_DURATION;
942
943 he->he_rts_thres = cpu_to_le16(vif->bss_conf.frame_time_rts_th);
944 if (!he->he_rts_thres)
945 he->he_rts_thres = cpu_to_le16(DEFAULT_HE_DURATION_RTS_THRES);
946
947 he->max_nss_mcs[CMD_HE_MCS_BW80] = cap->he_mcs_nss_supp.tx_mcs_80;
948 he->max_nss_mcs[CMD_HE_MCS_BW160] = cap->he_mcs_nss_supp.tx_mcs_160;
949 he->max_nss_mcs[CMD_HE_MCS_BW8080] = cap->he_mcs_nss_supp.tx_mcs_80p80;
950 }
951
952 static void
mt7915_mcu_bss_hw_amsdu_tlv(struct sk_buff * skb)953 mt7915_mcu_bss_hw_amsdu_tlv(struct sk_buff *skb)
954 {
955 #define TXD_CMP_MAP1 GENMASK(15, 0)
956 #define TXD_CMP_MAP2 (GENMASK(31, 0) & ~BIT(23))
957 struct bss_info_hw_amsdu *amsdu;
958 struct tlv *tlv;
959
960 tlv = mt7915_mcu_add_tlv(skb, BSS_INFO_HW_AMSDU, sizeof(*amsdu));
961
962 amsdu = (struct bss_info_hw_amsdu *)tlv;
963 amsdu->cmp_bitmap_0 = cpu_to_le32(TXD_CMP_MAP1);
964 amsdu->cmp_bitmap_1 = cpu_to_le32(TXD_CMP_MAP2);
965 amsdu->trig_thres = cpu_to_le16(2);
966 amsdu->enable = true;
967 }
968
969 static void
mt7915_mcu_bss_ext_tlv(struct sk_buff * skb,struct mt7915_vif * mvif)970 mt7915_mcu_bss_ext_tlv(struct sk_buff *skb, struct mt7915_vif *mvif)
971 {
972 /* SIFS 20us + 512 byte beacon tranmitted by 1Mbps (3906us) */
973 #define BCN_TX_ESTIMATE_TIME (4096 + 20)
974 struct bss_info_ext_bss *ext;
975 int ext_bss_idx, tsf_offset;
976 struct tlv *tlv;
977
978 ext_bss_idx = mvif->omac_idx - EXT_BSSID_START;
979 if (ext_bss_idx < 0)
980 return;
981
982 tlv = mt7915_mcu_add_tlv(skb, BSS_INFO_EXT_BSS, sizeof(*ext));
983
984 ext = (struct bss_info_ext_bss *)tlv;
985 tsf_offset = ext_bss_idx * BCN_TX_ESTIMATE_TIME;
986 ext->mbss_tsf_offset = cpu_to_le32(tsf_offset);
987 }
988
989 static void
mt7915_mcu_bss_bmc_tlv(struct sk_buff * skb,struct mt7915_phy * phy)990 mt7915_mcu_bss_bmc_tlv(struct sk_buff *skb, struct mt7915_phy *phy)
991 {
992 struct bss_info_bmc_rate *bmc;
993 struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
994 enum nl80211_band band = chandef->chan->band;
995 struct tlv *tlv;
996
997 tlv = mt7915_mcu_add_tlv(skb, BSS_INFO_BMC_RATE, sizeof(*bmc));
998
999 bmc = (struct bss_info_bmc_rate *)tlv;
1000 if (band == NL80211_BAND_2GHZ) {
1001 bmc->short_preamble = true;
1002 } else {
1003 bmc->bc_trans = cpu_to_le16(0x2000);
1004 bmc->mc_trans = cpu_to_le16(0x2080);
1005 }
1006 }
1007
1008 static void
mt7915_mcu_bss_sync_tlv(struct sk_buff * skb,struct ieee80211_vif * vif)1009 mt7915_mcu_bss_sync_tlv(struct sk_buff *skb, struct ieee80211_vif *vif)
1010 {
1011 struct bss_info_sync_mode *sync;
1012 struct tlv *tlv;
1013
1014 tlv = mt7915_mcu_add_tlv(skb, BSS_INFO_SYNC_MODE, sizeof(*sync));
1015
1016 sync = (struct bss_info_sync_mode *)tlv;
1017 sync->bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int);
1018 sync->dtim_period = vif->bss_conf.dtim_period;
1019 sync->enable = true;
1020 }
1021
mt7915_mcu_add_bss_info(struct mt7915_phy * phy,struct ieee80211_vif * vif,int enable)1022 int mt7915_mcu_add_bss_info(struct mt7915_phy *phy,
1023 struct ieee80211_vif *vif, int enable)
1024 {
1025 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1026 struct sk_buff *skb;
1027
1028 skb = mt7915_mcu_alloc_sta_req(phy->dev, mvif, NULL,
1029 MT7915_BSS_UPDATE_MAX_SIZE);
1030 if (IS_ERR(skb))
1031 return PTR_ERR(skb);
1032
1033 /* bss_omac must be first */
1034 if (enable)
1035 mt7915_mcu_bss_omac_tlv(skb, vif);
1036
1037 mt7915_mcu_bss_basic_tlv(skb, vif, phy, enable);
1038
1039 if (enable) {
1040 mt7915_mcu_bss_rfch_tlv(skb, vif, phy);
1041 mt7915_mcu_bss_bmc_tlv(skb, phy);
1042 mt7915_mcu_bss_ra_tlv(skb, vif, phy);
1043 mt7915_mcu_bss_hw_amsdu_tlv(skb);
1044
1045 if (vif->bss_conf.he_support)
1046 mt7915_mcu_bss_he_tlv(skb, vif, phy);
1047
1048 if (mvif->omac_idx > HW_BSSID_MAX)
1049 mt7915_mcu_bss_ext_tlv(skb, mvif);
1050 else
1051 mt7915_mcu_bss_sync_tlv(skb, vif);
1052 }
1053
1054 return __mt76_mcu_skb_send_msg(&phy->dev->mt76, skb,
1055 MCU_EXT_CMD_BSS_INFO_UPDATE, true);
1056 }
1057
1058 /** starec & wtbl **/
1059 static int
mt7915_mcu_sta_key_tlv(struct sk_buff * skb,struct ieee80211_key_conf * key,enum set_key_cmd cmd)1060 mt7915_mcu_sta_key_tlv(struct sk_buff *skb, struct ieee80211_key_conf *key,
1061 enum set_key_cmd cmd)
1062 {
1063 struct sta_rec_sec *sec;
1064 struct tlv *tlv;
1065 u32 len = sizeof(*sec);
1066
1067 tlv = mt7915_mcu_add_tlv(skb, STA_REC_KEY_V2, sizeof(*sec));
1068
1069 sec = (struct sta_rec_sec *)tlv;
1070 sec->add = cmd;
1071
1072 if (cmd == SET_KEY) {
1073 struct sec_key *sec_key;
1074 u8 cipher;
1075
1076 cipher = mt7915_mcu_get_cipher(key->cipher);
1077 if (cipher == MT_CIPHER_NONE)
1078 return -EOPNOTSUPP;
1079
1080 sec_key = &sec->key[0];
1081 sec_key->cipher_len = sizeof(*sec_key);
1082 sec_key->key_id = key->keyidx;
1083
1084 if (cipher == MT_CIPHER_BIP_CMAC_128) {
1085 sec_key->cipher_id = MT_CIPHER_AES_CCMP;
1086 sec_key->key_len = 16;
1087 memcpy(sec_key->key, key->key, 16);
1088
1089 sec_key = &sec->key[1];
1090 sec_key->cipher_id = MT_CIPHER_BIP_CMAC_128;
1091 sec_key->cipher_len = sizeof(*sec_key);
1092 sec_key->key_len = 16;
1093 memcpy(sec_key->key, key->key + 16, 16);
1094
1095 sec->n_cipher = 2;
1096 } else {
1097 sec_key->cipher_id = cipher;
1098 sec_key->key_len = key->keylen;
1099 memcpy(sec_key->key, key->key, key->keylen);
1100
1101 if (cipher == MT_CIPHER_TKIP) {
1102 /* Rx/Tx MIC keys are swapped */
1103 memcpy(sec_key->key + 16, key->key + 24, 8);
1104 memcpy(sec_key->key + 24, key->key + 16, 8);
1105 }
1106
1107 len -= sizeof(*sec_key);
1108 sec->n_cipher = 1;
1109 }
1110 } else {
1111 len -= sizeof(sec->key);
1112 sec->n_cipher = 0;
1113 }
1114 sec->len = cpu_to_le16(len);
1115
1116 return 0;
1117 }
1118
mt7915_mcu_add_key(struct mt7915_dev * dev,struct ieee80211_vif * vif,struct mt7915_sta * msta,struct ieee80211_key_conf * key,enum set_key_cmd cmd)1119 int mt7915_mcu_add_key(struct mt7915_dev *dev, struct ieee80211_vif *vif,
1120 struct mt7915_sta *msta, struct ieee80211_key_conf *key,
1121 enum set_key_cmd cmd)
1122 {
1123 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1124 struct sk_buff *skb;
1125 int len = sizeof(struct sta_req_hdr) + sizeof(struct sta_rec_sec);
1126 int ret;
1127
1128 skb = mt7915_mcu_alloc_sta_req(dev, mvif, msta, len);
1129 if (IS_ERR(skb))
1130 return PTR_ERR(skb);
1131
1132 ret = mt7915_mcu_sta_key_tlv(skb, key, cmd);
1133 if (ret)
1134 return ret;
1135
1136 return __mt76_mcu_skb_send_msg(&dev->mt76, skb,
1137 MCU_EXT_CMD_STA_REC_UPDATE, true);
1138 }
1139
1140 static void
mt7915_mcu_sta_ba_tlv(struct sk_buff * skb,struct ieee80211_ampdu_params * params,bool enable,bool tx)1141 mt7915_mcu_sta_ba_tlv(struct sk_buff *skb,
1142 struct ieee80211_ampdu_params *params,
1143 bool enable, bool tx)
1144 {
1145 struct sta_rec_ba *ba;
1146 struct tlv *tlv;
1147
1148 tlv = mt7915_mcu_add_tlv(skb, STA_REC_BA, sizeof(*ba));
1149
1150 ba = (struct sta_rec_ba *)tlv;
1151 ba->ba_type = tx ? MT_BA_TYPE_ORIGINATOR : MT_BA_TYPE_RECIPIENT,
1152 ba->winsize = cpu_to_le16(params->buf_size);
1153 ba->ssn = cpu_to_le16(params->ssn);
1154 ba->ba_en = enable << params->tid;
1155 ba->amsdu = params->amsdu;
1156 ba->tid = params->tid;
1157 }
1158
1159 static void
mt7915_mcu_wtbl_ba_tlv(struct sk_buff * skb,struct ieee80211_ampdu_params * params,bool enable,bool tx,void * sta_wtbl,void * wtbl_tlv)1160 mt7915_mcu_wtbl_ba_tlv(struct sk_buff *skb,
1161 struct ieee80211_ampdu_params *params,
1162 bool enable, bool tx, void *sta_wtbl,
1163 void *wtbl_tlv)
1164 {
1165 struct wtbl_ba *ba;
1166 struct tlv *tlv;
1167
1168 tlv = mt7915_mcu_add_nested_tlv(skb, WTBL_BA, sizeof(*ba),
1169 wtbl_tlv, sta_wtbl);
1170
1171 ba = (struct wtbl_ba *)tlv;
1172 ba->tid = params->tid;
1173
1174 if (tx) {
1175 ba->ba_type = MT_BA_TYPE_ORIGINATOR;
1176 ba->sn = enable ? cpu_to_le16(params->ssn) : 0;
1177 ba->ba_en = enable;
1178 } else {
1179 memcpy(ba->peer_addr, params->sta->addr, ETH_ALEN);
1180 ba->ba_type = MT_BA_TYPE_RECIPIENT;
1181 ba->rst_ba_tid = params->tid;
1182 ba->rst_ba_sel = RST_BA_MAC_TID_MATCH;
1183 ba->rst_ba_sb = 1;
1184 }
1185
1186 if (enable && tx)
1187 ba->ba_winsize = cpu_to_le16(params->buf_size);
1188 }
1189
1190 static int
mt7915_mcu_sta_ba(struct mt7915_dev * dev,struct ieee80211_ampdu_params * params,bool enable,bool tx)1191 mt7915_mcu_sta_ba(struct mt7915_dev *dev,
1192 struct ieee80211_ampdu_params *params,
1193 bool enable, bool tx)
1194 {
1195 struct mt7915_sta *msta = (struct mt7915_sta *)params->sta->drv_priv;
1196 struct mt7915_vif *mvif = msta->vif;
1197 struct wtbl_req_hdr *wtbl_hdr;
1198 struct tlv *sta_wtbl;
1199 struct sk_buff *skb;
1200 int ret;
1201
1202 if (enable && tx && !params->amsdu)
1203 msta->wcid.amsdu = false;
1204
1205 skb = mt7915_mcu_alloc_sta_req(dev, mvif, msta,
1206 MT7915_STA_UPDATE_MAX_SIZE);
1207 if (IS_ERR(skb))
1208 return PTR_ERR(skb);
1209
1210 sta_wtbl = mt7915_mcu_add_tlv(skb, STA_REC_WTBL, sizeof(struct tlv));
1211
1212 wtbl_hdr = mt7915_mcu_alloc_wtbl_req(dev, msta, WTBL_SET, sta_wtbl,
1213 &skb);
1214 mt7915_mcu_wtbl_ba_tlv(skb, params, enable, tx, sta_wtbl, wtbl_hdr);
1215
1216 ret = __mt76_mcu_skb_send_msg(&dev->mt76, skb,
1217 MCU_EXT_CMD_STA_REC_UPDATE, true);
1218 if (ret)
1219 return ret;
1220
1221 skb = mt7915_mcu_alloc_sta_req(dev, mvif, msta,
1222 MT7915_STA_UPDATE_MAX_SIZE);
1223 if (IS_ERR(skb))
1224 return PTR_ERR(skb);
1225
1226 mt7915_mcu_sta_ba_tlv(skb, params, enable, tx);
1227
1228 return __mt76_mcu_skb_send_msg(&dev->mt76, skb,
1229 MCU_EXT_CMD_STA_REC_UPDATE, true);
1230 }
1231
mt7915_mcu_add_tx_ba(struct mt7915_dev * dev,struct ieee80211_ampdu_params * params,bool enable)1232 int mt7915_mcu_add_tx_ba(struct mt7915_dev *dev,
1233 struct ieee80211_ampdu_params *params,
1234 bool enable)
1235 {
1236 return mt7915_mcu_sta_ba(dev, params, enable, true);
1237 }
1238
mt7915_mcu_add_rx_ba(struct mt7915_dev * dev,struct ieee80211_ampdu_params * params,bool enable)1239 int mt7915_mcu_add_rx_ba(struct mt7915_dev *dev,
1240 struct ieee80211_ampdu_params *params,
1241 bool enable)
1242 {
1243 return mt7915_mcu_sta_ba(dev, params, enable, false);
1244 }
1245
1246 static void
mt7915_mcu_wtbl_generic_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct ieee80211_sta * sta,void * sta_wtbl,void * wtbl_tlv)1247 mt7915_mcu_wtbl_generic_tlv(struct sk_buff *skb, struct ieee80211_vif *vif,
1248 struct ieee80211_sta *sta, void *sta_wtbl,
1249 void *wtbl_tlv)
1250 {
1251 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1252 struct wtbl_generic *generic;
1253 struct wtbl_rx *rx;
1254 struct tlv *tlv;
1255
1256 tlv = mt7915_mcu_add_nested_tlv(skb, WTBL_GENERIC, sizeof(*generic),
1257 wtbl_tlv, sta_wtbl);
1258
1259 generic = (struct wtbl_generic *)tlv;
1260
1261 if (sta) {
1262 if (vif->type == NL80211_IFTYPE_STATION)
1263 generic->partial_aid = cpu_to_le16(vif->bss_conf.aid);
1264 else
1265 generic->partial_aid = cpu_to_le16(sta->aid);
1266 memcpy(generic->peer_addr, sta->addr, ETH_ALEN);
1267 generic->muar_idx = mvif->omac_idx;
1268 generic->qos = sta->wme;
1269 } else {
1270 /* use BSSID in station mode */
1271 if (vif->type == NL80211_IFTYPE_STATION)
1272 memcpy(generic->peer_addr, vif->bss_conf.bssid,
1273 ETH_ALEN);
1274 else
1275 eth_broadcast_addr(generic->peer_addr);
1276
1277 generic->muar_idx = 0xe;
1278 }
1279
1280 tlv = mt7915_mcu_add_nested_tlv(skb, WTBL_RX, sizeof(*rx),
1281 wtbl_tlv, sta_wtbl);
1282
1283 rx = (struct wtbl_rx *)tlv;
1284 rx->rca1 = sta ? vif->type != NL80211_IFTYPE_AP : 1;
1285 rx->rca2 = 1;
1286 rx->rv = 1;
1287 }
1288
1289 static void
mt7915_mcu_sta_basic_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enable)1290 mt7915_mcu_sta_basic_tlv(struct sk_buff *skb, struct ieee80211_vif *vif,
1291 struct ieee80211_sta *sta, bool enable)
1292 {
1293 #define EXTRA_INFO_VER BIT(0)
1294 #define EXTRA_INFO_NEW BIT(1)
1295 struct sta_rec_basic *basic;
1296 struct tlv *tlv;
1297
1298 tlv = mt7915_mcu_add_tlv(skb, STA_REC_BASIC, sizeof(*basic));
1299
1300 basic = (struct sta_rec_basic *)tlv;
1301 basic->extra_info = cpu_to_le16(EXTRA_INFO_VER);
1302
1303 if (enable) {
1304 basic->extra_info |= cpu_to_le16(EXTRA_INFO_NEW);
1305 basic->conn_state = CONN_STATE_PORT_SECURE;
1306 } else {
1307 basic->conn_state = CONN_STATE_DISCONNECT;
1308 }
1309
1310 if (!sta) {
1311 basic->conn_type = cpu_to_le32(CONNECTION_INFRA_BC);
1312 eth_broadcast_addr(basic->peer_addr);
1313 return;
1314 }
1315
1316 switch (vif->type) {
1317 case NL80211_IFTYPE_MESH_POINT:
1318 case NL80211_IFTYPE_AP:
1319 basic->conn_type = cpu_to_le32(CONNECTION_INFRA_STA);
1320 basic->aid = cpu_to_le16(sta->aid);
1321 break;
1322 case NL80211_IFTYPE_STATION:
1323 basic->conn_type = cpu_to_le32(CONNECTION_INFRA_AP);
1324 basic->aid = cpu_to_le16(vif->bss_conf.aid);
1325 break;
1326 case NL80211_IFTYPE_ADHOC:
1327 basic->conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
1328 basic->aid = cpu_to_le16(sta->aid);
1329 break;
1330 default:
1331 WARN_ON(1);
1332 break;
1333 }
1334
1335 memcpy(basic->peer_addr, sta->addr, ETH_ALEN);
1336 basic->qos = sta->wme;
1337 }
1338
1339 static void
mt7915_mcu_sta_he_tlv(struct sk_buff * skb,struct ieee80211_sta * sta)1340 mt7915_mcu_sta_he_tlv(struct sk_buff *skb, struct ieee80211_sta *sta)
1341 {
1342 struct ieee80211_sta_he_cap *he_cap = &sta->he_cap;
1343 struct ieee80211_he_cap_elem *elem = &he_cap->he_cap_elem;
1344 struct sta_rec_he *he;
1345 struct tlv *tlv;
1346 u32 cap = 0;
1347
1348 tlv = mt7915_mcu_add_tlv(skb, STA_REC_HE, sizeof(*he));
1349
1350 he = (struct sta_rec_he *)tlv;
1351
1352 if (elem->mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
1353 cap |= STA_REC_HE_CAP_HTC;
1354
1355 if (elem->mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
1356 cap |= STA_REC_HE_CAP_BSR;
1357
1358 if (elem->mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
1359 cap |= STA_REC_HE_CAP_OM;
1360
1361 if (elem->mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU)
1362 cap |= STA_REC_HE_CAP_AMSDU_IN_AMPDU;
1363
1364 if (elem->mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
1365 cap |= STA_REC_HE_CAP_BQR;
1366
1367 if (elem->phy_cap_info[0] &
1368 (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G |
1369 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G))
1370 cap |= STA_REC_HE_CAP_BW20_RU242_SUPPORT;
1371
1372 if (elem->phy_cap_info[1] &
1373 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD)
1374 cap |= STA_REC_HE_CAP_LDPC;
1375
1376 if (elem->phy_cap_info[1] &
1377 IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US)
1378 cap |= STA_REC_HE_CAP_SU_PPDU_1LTF_8US_GI;
1379
1380 if (elem->phy_cap_info[2] &
1381 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US)
1382 cap |= STA_REC_HE_CAP_NDP_4LTF_3DOT2MS_GI;
1383
1384 if (elem->phy_cap_info[2] &
1385 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ)
1386 cap |= STA_REC_HE_CAP_LE_EQ_80M_TX_STBC;
1387
1388 if (elem->phy_cap_info[2] &
1389 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ)
1390 cap |= STA_REC_HE_CAP_LE_EQ_80M_RX_STBC;
1391
1392 if (elem->phy_cap_info[6] &
1393 IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE)
1394 cap |= STA_REC_HE_CAP_PARTIAL_BW_EXT_RANGE;
1395
1396 if (elem->phy_cap_info[7] &
1397 IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI)
1398 cap |= STA_REC_HE_CAP_SU_MU_PPDU_4LTF_8US_GI;
1399
1400 if (elem->phy_cap_info[7] &
1401 IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ)
1402 cap |= STA_REC_HE_CAP_GT_80M_TX_STBC;
1403
1404 if (elem->phy_cap_info[7] &
1405 IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ)
1406 cap |= STA_REC_HE_CAP_GT_80M_RX_STBC;
1407
1408 if (elem->phy_cap_info[8] &
1409 IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI)
1410 cap |= STA_REC_HE_CAP_ER_SU_PPDU_4LTF_8US_GI;
1411
1412 if (elem->phy_cap_info[8] &
1413 IEEE80211_HE_PHY_CAP8_HE_ER_SU_1XLTF_AND_08_US_GI)
1414 cap |= STA_REC_HE_CAP_ER_SU_PPDU_1LTF_8US_GI;
1415
1416 if (elem->phy_cap_info[9] &
1417 IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK)
1418 cap |= STA_REC_HE_CAP_TRIG_CQI_FK;
1419
1420 if (elem->phy_cap_info[9] &
1421 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU)
1422 cap |= STA_REC_HE_CAP_TX_1024QAM_UNDER_RU242;
1423
1424 if (elem->phy_cap_info[9] &
1425 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU)
1426 cap |= STA_REC_HE_CAP_RX_1024QAM_UNDER_RU242;
1427
1428 he->he_cap = cpu_to_le32(cap);
1429
1430 switch (sta->bandwidth) {
1431 case IEEE80211_STA_RX_BW_160:
1432 if (elem->phy_cap_info[0] &
1433 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
1434 he->max_nss_mcs[CMD_HE_MCS_BW8080] =
1435 he_cap->he_mcs_nss_supp.rx_mcs_80p80;
1436
1437 he->max_nss_mcs[CMD_HE_MCS_BW160] =
1438 he_cap->he_mcs_nss_supp.rx_mcs_160;
1439 fallthrough;
1440 default:
1441 he->max_nss_mcs[CMD_HE_MCS_BW80] =
1442 he_cap->he_mcs_nss_supp.rx_mcs_80;
1443 break;
1444 }
1445
1446 he->t_frame_dur =
1447 HE_MAC(CAP1_TF_MAC_PAD_DUR_MASK, elem->mac_cap_info[1]);
1448 he->max_ampdu_exp =
1449 HE_MAC(CAP3_MAX_AMPDU_LEN_EXP_MASK, elem->mac_cap_info[3]);
1450
1451 he->bw_set =
1452 HE_PHY(CAP0_CHANNEL_WIDTH_SET_MASK, elem->phy_cap_info[0]);
1453 he->device_class =
1454 HE_PHY(CAP1_DEVICE_CLASS_A, elem->phy_cap_info[1]);
1455 he->punc_pream_rx =
1456 HE_PHY(CAP1_PREAMBLE_PUNC_RX_MASK, elem->phy_cap_info[1]);
1457
1458 he->dcm_tx_mode =
1459 HE_PHY(CAP3_DCM_MAX_CONST_TX_MASK, elem->phy_cap_info[3]);
1460 he->dcm_tx_max_nss =
1461 HE_PHY(CAP3_DCM_MAX_TX_NSS_2, elem->phy_cap_info[3]);
1462 he->dcm_rx_mode =
1463 HE_PHY(CAP3_DCM_MAX_CONST_RX_MASK, elem->phy_cap_info[3]);
1464 he->dcm_rx_max_nss =
1465 HE_PHY(CAP3_DCM_MAX_RX_NSS_2, elem->phy_cap_info[3]);
1466 he->dcm_rx_max_nss =
1467 HE_PHY(CAP8_DCM_MAX_RU_MASK, elem->phy_cap_info[8]);
1468
1469 he->pkt_ext = 2;
1470 }
1471
1472 static void
mt7915_mcu_sta_uapsd_tlv(struct sk_buff * skb,struct ieee80211_sta * sta,struct ieee80211_vif * vif)1473 mt7915_mcu_sta_uapsd_tlv(struct sk_buff *skb, struct ieee80211_sta *sta,
1474 struct ieee80211_vif *vif)
1475 {
1476 struct sta_rec_uapsd *uapsd;
1477 struct tlv *tlv;
1478
1479 if (vif->type != NL80211_IFTYPE_AP || !sta->wme)
1480 return;
1481
1482 tlv = mt7915_mcu_add_tlv(skb, STA_REC_APPS, sizeof(*uapsd));
1483 uapsd = (struct sta_rec_uapsd *)tlv;
1484
1485 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) {
1486 uapsd->dac_map |= BIT(3);
1487 uapsd->tac_map |= BIT(3);
1488 }
1489 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI) {
1490 uapsd->dac_map |= BIT(2);
1491 uapsd->tac_map |= BIT(2);
1492 }
1493 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE) {
1494 uapsd->dac_map |= BIT(1);
1495 uapsd->tac_map |= BIT(1);
1496 }
1497 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK) {
1498 uapsd->dac_map |= BIT(0);
1499 uapsd->tac_map |= BIT(0);
1500 }
1501 uapsd->max_sp = sta->max_sp;
1502 }
1503
1504 static void
mt7915_mcu_sta_muru_tlv(struct sk_buff * skb,struct ieee80211_sta * sta)1505 mt7915_mcu_sta_muru_tlv(struct sk_buff *skb, struct ieee80211_sta *sta)
1506 {
1507 struct ieee80211_sta_he_cap *he_cap = &sta->he_cap;
1508 struct ieee80211_he_cap_elem *elem = &he_cap->he_cap_elem;
1509 struct sta_rec_muru *muru;
1510 struct tlv *tlv;
1511
1512 tlv = mt7915_mcu_add_tlv(skb, STA_REC_MURU, sizeof(*muru));
1513
1514 muru = (struct sta_rec_muru *)tlv;
1515 muru->cfg.ofdma_dl_en = true;
1516 muru->cfg.ofdma_ul_en = true;
1517 muru->cfg.mimo_dl_en = true;
1518 muru->cfg.mimo_ul_en = true;
1519
1520 muru->ofdma_dl.punc_pream_rx =
1521 HE_PHY(CAP1_PREAMBLE_PUNC_RX_MASK, elem->phy_cap_info[1]);
1522 muru->ofdma_dl.he_20m_in_40m_2g =
1523 HE_PHY(CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G, elem->phy_cap_info[8]);
1524 muru->ofdma_dl.he_20m_in_160m =
1525 HE_PHY(CAP8_20MHZ_IN_160MHZ_HE_PPDU, elem->phy_cap_info[8]);
1526 muru->ofdma_dl.he_80m_in_160m =
1527 HE_PHY(CAP8_80MHZ_IN_160MHZ_HE_PPDU, elem->phy_cap_info[8]);
1528 muru->ofdma_dl.lt16_sigb = 0;
1529 muru->ofdma_dl.rx_su_comp_sigb = 0;
1530 muru->ofdma_dl.rx_su_non_comp_sigb = 0;
1531
1532 muru->ofdma_ul.t_frame_dur =
1533 HE_MAC(CAP1_TF_MAC_PAD_DUR_MASK, elem->mac_cap_info[1]);
1534 muru->ofdma_ul.mu_cascading =
1535 HE_MAC(CAP2_MU_CASCADING, elem->mac_cap_info[2]);
1536 muru->ofdma_ul.uo_ra =
1537 HE_MAC(CAP3_OFDMA_RA, elem->mac_cap_info[3]);
1538 muru->ofdma_ul.he_2x996_tone = 0;
1539 muru->ofdma_ul.rx_t_frame_11ac = 0;
1540
1541 muru->mimo_dl.vht_mu_bfee =
1542 !!(sta->vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
1543 muru->mimo_dl.partial_bw_dl_mimo =
1544 HE_PHY(CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO, elem->phy_cap_info[6]);
1545
1546 muru->mimo_ul.full_ul_mimo =
1547 HE_PHY(CAP2_UL_MU_FULL_MU_MIMO, elem->phy_cap_info[2]);
1548 muru->mimo_ul.partial_ul_mimo =
1549 HE_PHY(CAP2_UL_MU_PARTIAL_MU_MIMO, elem->phy_cap_info[2]);
1550 }
1551
1552 static int
mt7915_mcu_add_mu(struct mt7915_dev * dev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1553 mt7915_mcu_add_mu(struct mt7915_dev *dev, struct ieee80211_vif *vif,
1554 struct ieee80211_sta *sta)
1555 {
1556 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1557 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1558 struct sk_buff *skb;
1559 int len = sizeof(struct sta_req_hdr) + sizeof(struct sta_rec_muru);
1560
1561 if (!sta->vht_cap.vht_supported && !sta->he_cap.has_he)
1562 return 0;
1563
1564 skb = mt7915_mcu_alloc_sta_req(dev, mvif, msta, len);
1565 if (IS_ERR(skb))
1566 return PTR_ERR(skb);
1567
1568 /* starec muru */
1569 mt7915_mcu_sta_muru_tlv(skb, sta);
1570
1571 return __mt76_mcu_skb_send_msg(&dev->mt76, skb,
1572 MCU_EXT_CMD_STA_REC_UPDATE, true);
1573 }
1574
1575 static void
mt7915_mcu_sta_amsdu_tlv(struct sk_buff * skb,struct ieee80211_sta * sta)1576 mt7915_mcu_sta_amsdu_tlv(struct sk_buff *skb, struct ieee80211_sta *sta)
1577 {
1578 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1579 struct sta_rec_amsdu *amsdu;
1580 struct tlv *tlv;
1581
1582 if (!sta->max_amsdu_len)
1583 return;
1584
1585 tlv = mt7915_mcu_add_tlv(skb, STA_REC_HW_AMSDU, sizeof(*amsdu));
1586 amsdu = (struct sta_rec_amsdu *)tlv;
1587 amsdu->max_amsdu_num = 8;
1588 amsdu->amsdu_en = true;
1589 amsdu->max_mpdu_size = sta->max_amsdu_len >=
1590 IEEE80211_MAX_MPDU_LEN_VHT_7991;
1591 msta->wcid.amsdu = true;
1592 }
1593
1594 static bool
mt7915_hw_amsdu_supported(struct ieee80211_vif * vif)1595 mt7915_hw_amsdu_supported(struct ieee80211_vif *vif)
1596 {
1597 switch (vif->type) {
1598 case NL80211_IFTYPE_AP:
1599 case NL80211_IFTYPE_STATION:
1600 return true;
1601 default:
1602 return false;
1603 }
1604 }
1605
1606 static void
mt7915_mcu_sta_tlv(struct mt7915_dev * dev,struct sk_buff * skb,struct ieee80211_sta * sta,struct ieee80211_vif * vif)1607 mt7915_mcu_sta_tlv(struct mt7915_dev *dev, struct sk_buff *skb,
1608 struct ieee80211_sta *sta, struct ieee80211_vif *vif)
1609 {
1610 struct tlv *tlv;
1611
1612 /* starec ht */
1613 if (sta->ht_cap.ht_supported) {
1614 struct sta_rec_ht *ht;
1615
1616 tlv = mt7915_mcu_add_tlv(skb, STA_REC_HT, sizeof(*ht));
1617 ht = (struct sta_rec_ht *)tlv;
1618 ht->ht_cap = cpu_to_le16(sta->ht_cap.cap);
1619
1620 if (mt7915_hw_amsdu_supported(vif))
1621 mt7915_mcu_sta_amsdu_tlv(skb, sta);
1622 }
1623
1624 /* starec vht */
1625 if (sta->vht_cap.vht_supported) {
1626 struct sta_rec_vht *vht;
1627
1628 tlv = mt7915_mcu_add_tlv(skb, STA_REC_VHT, sizeof(*vht));
1629 vht = (struct sta_rec_vht *)tlv;
1630 vht->vht_cap = cpu_to_le32(sta->vht_cap.cap);
1631 vht->vht_rx_mcs_map = sta->vht_cap.vht_mcs.rx_mcs_map;
1632 vht->vht_tx_mcs_map = sta->vht_cap.vht_mcs.tx_mcs_map;
1633 }
1634
1635 /* starec he */
1636 if (sta->he_cap.has_he)
1637 mt7915_mcu_sta_he_tlv(skb, sta);
1638
1639 /* starec uapsd */
1640 mt7915_mcu_sta_uapsd_tlv(skb, sta, vif);
1641 }
1642
1643 static void
mt7915_mcu_wtbl_smps_tlv(struct sk_buff * skb,struct ieee80211_sta * sta,void * sta_wtbl,void * wtbl_tlv)1644 mt7915_mcu_wtbl_smps_tlv(struct sk_buff *skb, struct ieee80211_sta *sta,
1645 void *sta_wtbl, void *wtbl_tlv)
1646 {
1647 struct wtbl_smps *smps;
1648 struct tlv *tlv;
1649
1650 tlv = mt7915_mcu_add_nested_tlv(skb, WTBL_SMPS, sizeof(*smps),
1651 wtbl_tlv, sta_wtbl);
1652 smps = (struct wtbl_smps *)tlv;
1653
1654 if (sta->smps_mode == IEEE80211_SMPS_DYNAMIC)
1655 smps->smps = true;
1656 }
1657
1658 static void
mt7915_mcu_wtbl_ht_tlv(struct sk_buff * skb,struct ieee80211_sta * sta,void * sta_wtbl,void * wtbl_tlv)1659 mt7915_mcu_wtbl_ht_tlv(struct sk_buff *skb, struct ieee80211_sta *sta,
1660 void *sta_wtbl, void *wtbl_tlv)
1661 {
1662 struct wtbl_ht *ht = NULL;
1663 struct tlv *tlv;
1664
1665 /* wtbl ht */
1666 if (sta->ht_cap.ht_supported) {
1667 tlv = mt7915_mcu_add_nested_tlv(skb, WTBL_HT, sizeof(*ht),
1668 wtbl_tlv, sta_wtbl);
1669 ht = (struct wtbl_ht *)tlv;
1670 ht->ldpc = sta->ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING;
1671 ht->af = sta->ht_cap.ampdu_factor;
1672 ht->mm = sta->ht_cap.ampdu_density;
1673 ht->ht = true;
1674 }
1675
1676 /* wtbl vht */
1677 if (sta->vht_cap.vht_supported) {
1678 struct wtbl_vht *vht;
1679 u8 af;
1680
1681 tlv = mt7915_mcu_add_nested_tlv(skb, WTBL_VHT, sizeof(*vht),
1682 wtbl_tlv, sta_wtbl);
1683 vht = (struct wtbl_vht *)tlv;
1684 vht->ldpc = sta->vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC,
1685 vht->vht = true;
1686
1687 af = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
1688 sta->vht_cap.cap);
1689 if (ht)
1690 ht->af = max_t(u8, ht->af, af);
1691 }
1692
1693 mt7915_mcu_wtbl_smps_tlv(skb, sta, sta_wtbl, wtbl_tlv);
1694 }
1695
mt7915_mcu_add_smps(struct mt7915_dev * dev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1696 int mt7915_mcu_add_smps(struct mt7915_dev *dev, struct ieee80211_vif *vif,
1697 struct ieee80211_sta *sta)
1698 {
1699 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1700 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1701 struct wtbl_req_hdr *wtbl_hdr;
1702 struct tlv *sta_wtbl;
1703 struct sk_buff *skb;
1704
1705 skb = mt7915_mcu_alloc_sta_req(dev, mvif, msta,
1706 MT7915_STA_UPDATE_MAX_SIZE);
1707 if (IS_ERR(skb))
1708 return PTR_ERR(skb);
1709
1710 sta_wtbl = mt7915_mcu_add_tlv(skb, STA_REC_WTBL, sizeof(struct tlv));
1711
1712 wtbl_hdr = mt7915_mcu_alloc_wtbl_req(dev, msta, WTBL_SET, sta_wtbl,
1713 &skb);
1714 mt7915_mcu_wtbl_smps_tlv(skb, sta, sta_wtbl, wtbl_hdr);
1715
1716 return __mt76_mcu_skb_send_msg(&dev->mt76, skb,
1717 MCU_EXT_CMD_STA_REC_UPDATE, true);
1718 }
1719
1720 static void
mt7915_mcu_sta_sounding_rate(struct sta_rec_bf * bf)1721 mt7915_mcu_sta_sounding_rate(struct sta_rec_bf *bf)
1722 {
1723 bf->sounding_phy = MT_PHY_TYPE_OFDM;
1724 bf->ndp_rate = 0; /* mcs0 */
1725 bf->ndpa_rate = MT7915_CFEND_RATE_DEFAULT; /* ofdm 24m */
1726 bf->rept_poll_rate = MT7915_CFEND_RATE_DEFAULT; /* ofdm 24m */
1727 }
1728
1729 static void
mt7915_mcu_sta_bfer_ht(struct ieee80211_sta * sta,struct sta_rec_bf * bf)1730 mt7915_mcu_sta_bfer_ht(struct ieee80211_sta *sta, struct sta_rec_bf *bf)
1731 {
1732 struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
1733 u8 n = 0;
1734
1735 bf->tx_mode = MT_PHY_TYPE_HT;
1736 bf->bf_cap |= MT_IBF;
1737
1738 if (mcs->tx_params & IEEE80211_HT_MCS_TX_RX_DIFF &&
1739 (mcs->tx_params & IEEE80211_HT_MCS_TX_DEFINED))
1740 n = FIELD_GET(IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK,
1741 mcs->tx_params);
1742 else if (mcs->rx_mask[3])
1743 n = 3;
1744 else if (mcs->rx_mask[2])
1745 n = 2;
1746 else if (mcs->rx_mask[1])
1747 n = 1;
1748
1749 bf->nc = min_t(u8, bf->nr, n);
1750 bf->ibf_ncol = bf->nc;
1751
1752 if (sta->bandwidth <= IEEE80211_STA_RX_BW_40 && !bf->nc)
1753 bf->ibf_timeout = 0x48;
1754 }
1755
1756 static void
mt7915_mcu_sta_bfer_vht(struct ieee80211_sta * sta,struct mt7915_phy * phy,struct sta_rec_bf * bf)1757 mt7915_mcu_sta_bfer_vht(struct ieee80211_sta *sta, struct mt7915_phy *phy,
1758 struct sta_rec_bf *bf)
1759 {
1760 struct ieee80211_sta_vht_cap *pc = &sta->vht_cap;
1761 struct ieee80211_sta_vht_cap *vc = &phy->mt76->sband_5g.sband.vht_cap;
1762 u8 bfee_nr, bfer_nr, n, tx_ant = hweight8(phy->chainmask) - 1;
1763 u16 mcs_map;
1764
1765 bf->tx_mode = MT_PHY_TYPE_VHT;
1766 bf->bf_cap |= MT_EBF;
1767
1768 mt7915_mcu_sta_sounding_rate(bf);
1769
1770 bfee_nr = FIELD_GET(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK,
1771 pc->cap);
1772 bfer_nr = FIELD_GET(IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK,
1773 vc->cap);
1774 mcs_map = le16_to_cpu(pc->vht_mcs.rx_mcs_map);
1775
1776 n = min_t(u8, bfer_nr, bfee_nr);
1777 bf->nr = min_t(u8, n, tx_ant);
1778 n = mt7915_mcu_get_sta_nss(mcs_map);
1779
1780 bf->nc = min_t(u8, n, bf->nr);
1781 bf->ibf_ncol = bf->nc;
1782
1783 /* force nr from 4 to 2 */
1784 if (sta->bandwidth == IEEE80211_STA_RX_BW_160)
1785 bf->nr = 1;
1786 }
1787
1788 static void
mt7915_mcu_sta_bfer_he(struct ieee80211_sta * sta,struct ieee80211_vif * vif,struct mt7915_phy * phy,struct sta_rec_bf * bf)1789 mt7915_mcu_sta_bfer_he(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
1790 struct mt7915_phy *phy, struct sta_rec_bf *bf)
1791 {
1792 struct ieee80211_sta_he_cap *pc = &sta->he_cap;
1793 struct ieee80211_he_cap_elem *pe = &pc->he_cap_elem;
1794 const struct ieee80211_he_cap_elem *ve;
1795 const struct ieee80211_sta_he_cap *vc;
1796 u8 bfee_nr, bfer_nr, nss_mcs;
1797 u16 mcs_map;
1798
1799 vc = mt7915_get_he_phy_cap(phy, vif);
1800 ve = &vc->he_cap_elem;
1801
1802 bf->tx_mode = MT_PHY_TYPE_HE_SU;
1803 bf->bf_cap |= MT_EBF;
1804
1805 mt7915_mcu_sta_sounding_rate(bf);
1806
1807 bf->trigger_su = HE_PHY(CAP6_TRIG_SU_BEAMFORMER_FB,
1808 pe->phy_cap_info[6]);
1809 bf->trigger_mu = HE_PHY(CAP6_TRIG_MU_BEAMFORMER_FB,
1810 pe->phy_cap_info[6]);
1811 bfer_nr = HE_PHY(CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK,
1812 ve->phy_cap_info[5]);
1813 bfee_nr = HE_PHY(CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_MASK,
1814 pe->phy_cap_info[4]);
1815
1816 mcs_map = le16_to_cpu(pc->he_mcs_nss_supp.tx_mcs_80);
1817 nss_mcs = mt7915_mcu_get_sta_nss(mcs_map);
1818
1819 bf->nr = min_t(u8, bfer_nr, bfee_nr);
1820 bf->nc = min_t(u8, nss_mcs, bf->nr);
1821 bf->ibf_ncol = bf->nc;
1822
1823 if (sta->bandwidth != IEEE80211_STA_RX_BW_160)
1824 return;
1825
1826 /* go over for 160MHz and 80p80 */
1827 if (pe->phy_cap_info[0] &
1828 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G) {
1829 mcs_map = le16_to_cpu(pc->he_mcs_nss_supp.rx_mcs_160);
1830 nss_mcs = mt7915_mcu_get_sta_nss(mcs_map);
1831
1832 bf->nc_bw160 = nss_mcs;
1833 }
1834
1835 if (pe->phy_cap_info[0] &
1836 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) {
1837 mcs_map = le16_to_cpu(pc->he_mcs_nss_supp.rx_mcs_80p80);
1838 nss_mcs = mt7915_mcu_get_sta_nss(mcs_map);
1839
1840 if (bf->nc_bw160)
1841 bf->nc_bw160 = min_t(u8, bf->nc_bw160, nss_mcs);
1842 else
1843 bf->nc_bw160 = nss_mcs;
1844 }
1845
1846 bfer_nr = HE_PHY(CAP5_BEAMFORMEE_NUM_SND_DIM_ABOVE_80MHZ_MASK,
1847 ve->phy_cap_info[5]);
1848 bfee_nr = HE_PHY(CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_MASK,
1849 pe->phy_cap_info[4]);
1850
1851 bf->nr_bw160 = min_t(int, bfer_nr, bfee_nr);
1852 }
1853
1854 static void
mt7915_mcu_sta_bfer_tlv(struct sk_buff * skb,struct ieee80211_sta * sta,struct ieee80211_vif * vif,struct mt7915_phy * phy,bool enable)1855 mt7915_mcu_sta_bfer_tlv(struct sk_buff *skb, struct ieee80211_sta *sta,
1856 struct ieee80211_vif *vif, struct mt7915_phy *phy,
1857 bool enable)
1858 {
1859 struct sta_rec_bf *bf;
1860 struct tlv *tlv;
1861 int tx_ant = hweight8(phy->chainmask) - 1;
1862 const u8 matrix[4][4] = {
1863 {0, 0, 0, 0},
1864 {1, 1, 0, 0}, /* 2x1, 2x2, 2x3, 2x4 */
1865 {2, 4, 4, 0}, /* 3x1, 3x2, 3x3, 3x4 */
1866 {3, 5, 6, 0} /* 4x1, 4x2, 4x3, 4x4 */
1867 };
1868
1869 #define MT_BFER_FREE cpu_to_le16(GENMASK(15, 0))
1870
1871 tlv = mt7915_mcu_add_tlv(skb, STA_REC_BF, sizeof(*bf));
1872 bf = (struct sta_rec_bf *)tlv;
1873
1874 if (!enable) {
1875 bf->pfmu = MT_BFER_FREE;
1876 return;
1877 }
1878
1879 bf->bw = sta->bandwidth;
1880 bf->ibf_dbw = sta->bandwidth;
1881 bf->ibf_nrow = tx_ant;
1882 bf->ibf_timeout = 0x18;
1883
1884 if (sta->he_cap.has_he)
1885 mt7915_mcu_sta_bfer_he(sta, vif, phy, bf);
1886 else if (sta->vht_cap.vht_supported)
1887 mt7915_mcu_sta_bfer_vht(sta, phy, bf);
1888 else if (sta->ht_cap.ht_supported)
1889 mt7915_mcu_sta_bfer_ht(sta, bf);
1890
1891 if (bf->bf_cap & MT_EBF && bf->nr != tx_ant)
1892 bf->mem_20m = matrix[tx_ant][bf->nc];
1893 else
1894 bf->mem_20m = matrix[bf->nr][bf->nc];
1895
1896 switch (sta->bandwidth) {
1897 case IEEE80211_STA_RX_BW_160:
1898 case IEEE80211_STA_RX_BW_80:
1899 bf->mem_total = bf->mem_20m * 2;
1900 break;
1901 case IEEE80211_STA_RX_BW_40:
1902 bf->mem_total = bf->mem_20m;
1903 break;
1904 case IEEE80211_STA_RX_BW_20:
1905 default:
1906 break;
1907 }
1908 }
1909
1910 static void
mt7915_mcu_sta_bfee_tlv(struct sk_buff * skb,struct ieee80211_sta * sta,struct mt7915_phy * phy)1911 mt7915_mcu_sta_bfee_tlv(struct sk_buff *skb, struct ieee80211_sta *sta,
1912 struct mt7915_phy *phy)
1913 {
1914 struct sta_rec_bfee *bfee;
1915 struct tlv *tlv;
1916 int tx_ant = hweight8(phy->chainmask) - 1;
1917 u8 nr = 0;
1918
1919 tlv = mt7915_mcu_add_tlv(skb, STA_REC_BFEE, sizeof(*bfee));
1920 bfee = (struct sta_rec_bfee *)tlv;
1921
1922 if (sta->he_cap.has_he) {
1923 struct ieee80211_he_cap_elem *pe = &sta->he_cap.he_cap_elem;
1924
1925 nr = HE_PHY(CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK,
1926 pe->phy_cap_info[5]);
1927 } else if (sta->vht_cap.vht_supported) {
1928 struct ieee80211_sta_vht_cap *pc = &sta->vht_cap;
1929
1930 nr = FIELD_GET(IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK,
1931 pc->cap);
1932 }
1933
1934 /* reply with identity matrix to avoid 2x2 BF negative gain */
1935 if (nr == 1 && tx_ant == 2)
1936 bfee->fb_identity_matrix = true;
1937 }
1938
1939 static u8
mt7915_mcu_sta_txbf_type(struct mt7915_phy * phy,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1940 mt7915_mcu_sta_txbf_type(struct mt7915_phy *phy, struct ieee80211_vif *vif,
1941 struct ieee80211_sta *sta)
1942 {
1943 u8 type = 0;
1944
1945 if (vif->type != NL80211_IFTYPE_STATION &&
1946 vif->type != NL80211_IFTYPE_AP)
1947 return 0;
1948
1949 if (sta->he_cap.has_he) {
1950 struct ieee80211_he_cap_elem *pe;
1951 const struct ieee80211_he_cap_elem *ve;
1952 const struct ieee80211_sta_he_cap *vc;
1953
1954 pe = &sta->he_cap.he_cap_elem;
1955 vc = mt7915_get_he_phy_cap(phy, vif);
1956 ve = &vc->he_cap_elem;
1957
1958 if ((HE_PHY(CAP3_SU_BEAMFORMER, pe->phy_cap_info[3]) ||
1959 HE_PHY(CAP4_MU_BEAMFORMER, pe->phy_cap_info[4])) &&
1960 HE_PHY(CAP4_SU_BEAMFORMEE, ve->phy_cap_info[4]))
1961 type |= MT_STA_BFEE;
1962
1963 if ((HE_PHY(CAP3_SU_BEAMFORMER, ve->phy_cap_info[3]) ||
1964 HE_PHY(CAP4_MU_BEAMFORMER, ve->phy_cap_info[4])) &&
1965 HE_PHY(CAP4_SU_BEAMFORMEE, pe->phy_cap_info[4]))
1966 type |= MT_STA_BFER;
1967 } else if (sta->vht_cap.vht_supported) {
1968 struct ieee80211_sta_vht_cap *pc;
1969 struct ieee80211_sta_vht_cap *vc;
1970 u32 cr, ce;
1971
1972 pc = &sta->vht_cap;
1973 vc = &phy->mt76->sband_5g.sband.vht_cap;
1974 cr = IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
1975 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE;
1976 ce = IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
1977 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
1978
1979 if ((pc->cap & cr) && (vc->cap & ce))
1980 type |= MT_STA_BFEE;
1981
1982 if ((vc->cap & cr) && (pc->cap & ce))
1983 type |= MT_STA_BFER;
1984 } else if (sta->ht_cap.ht_supported) {
1985 /* TODO: iBF */
1986 }
1987
1988 return type;
1989 }
1990
1991 static int
mt7915_mcu_add_txbf(struct mt7915_dev * dev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enable)1992 mt7915_mcu_add_txbf(struct mt7915_dev *dev, struct ieee80211_vif *vif,
1993 struct ieee80211_sta *sta, bool enable)
1994 {
1995 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1996 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1997 struct mt7915_phy *phy;
1998 struct sk_buff *skb;
1999 int r, len;
2000 u8 type;
2001
2002 phy = mvif->band_idx ? mt7915_ext_phy(dev) : &dev->phy;
2003
2004 type = mt7915_mcu_sta_txbf_type(phy, vif, sta);
2005
2006 /* must keep each tag independent */
2007
2008 /* starec bf */
2009 if (type & MT_STA_BFER) {
2010 len = sizeof(struct sta_req_hdr) + sizeof(struct sta_rec_bf);
2011
2012 skb = mt7915_mcu_alloc_sta_req(dev, mvif, msta, len);
2013 if (IS_ERR(skb))
2014 return PTR_ERR(skb);
2015
2016 mt7915_mcu_sta_bfer_tlv(skb, sta, vif, phy, enable);
2017
2018 r = __mt76_mcu_skb_send_msg(&dev->mt76, skb,
2019 MCU_EXT_CMD_STA_REC_UPDATE, true);
2020 if (r)
2021 return r;
2022 }
2023
2024 /* starec bfee */
2025 if (type & MT_STA_BFEE) {
2026 len = sizeof(struct sta_req_hdr) + sizeof(struct sta_rec_bfee);
2027
2028 skb = mt7915_mcu_alloc_sta_req(dev, mvif, msta, len);
2029 if (IS_ERR(skb))
2030 return PTR_ERR(skb);
2031
2032 mt7915_mcu_sta_bfee_tlv(skb, sta, phy);
2033
2034 r = __mt76_mcu_skb_send_msg(&dev->mt76, skb,
2035 MCU_EXT_CMD_STA_REC_UPDATE, true);
2036 if (r)
2037 return r;
2038 }
2039
2040 return 0;
2041 }
2042
2043 static void
mt7915_mcu_sta_rate_ctrl_tlv(struct sk_buff * skb,struct mt7915_dev * dev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)2044 mt7915_mcu_sta_rate_ctrl_tlv(struct sk_buff *skb, struct mt7915_dev *dev,
2045 struct ieee80211_vif *vif,
2046 struct ieee80211_sta *sta)
2047 {
2048 struct cfg80211_chan_def *chandef = &dev->mphy.chandef;
2049 struct sta_rec_ra *ra;
2050 struct tlv *tlv;
2051 enum nl80211_band band = chandef->chan->band;
2052 u32 supp_rate = sta->supp_rates[band];
2053 int n_rates = hweight32(supp_rate);
2054 u32 cap = sta->wme ? STA_CAP_WMM : 0;
2055 u8 i, nss = sta->rx_nss, mcs = 0;
2056
2057 tlv = mt7915_mcu_add_tlv(skb, STA_REC_RA, sizeof(*ra));
2058
2059 ra = (struct sta_rec_ra *)tlv;
2060 ra->valid = true;
2061 ra->auto_rate = true;
2062 ra->phy_mode = mt7915_get_phy_mode(dev, vif, band, sta);
2063 ra->channel = chandef->chan->hw_value;
2064 ra->bw = sta->bandwidth;
2065 ra->rate_len = n_rates;
2066 ra->phy.bw = sta->bandwidth;
2067
2068 if (n_rates) {
2069 if (band == NL80211_BAND_2GHZ) {
2070 ra->supp_mode = MODE_CCK;
2071 ra->supp_cck_rate = supp_rate & GENMASK(3, 0);
2072 ra->phy.type = MT_PHY_TYPE_CCK;
2073
2074 if (n_rates > 4) {
2075 ra->supp_mode |= MODE_OFDM;
2076 ra->supp_ofdm_rate = supp_rate >> 4;
2077 ra->phy.type = MT_PHY_TYPE_OFDM;
2078 }
2079 } else {
2080 ra->supp_mode = MODE_OFDM;
2081 ra->supp_ofdm_rate = supp_rate;
2082 ra->phy.type = MT_PHY_TYPE_OFDM;
2083 }
2084 }
2085
2086 if (sta->ht_cap.ht_supported) {
2087 for (i = 0; i < nss; i++)
2088 ra->ht_mcs[i] = sta->ht_cap.mcs.rx_mask[i];
2089
2090 ra->supp_ht_mcs = *(__le32 *)ra->ht_mcs;
2091 ra->supp_mode |= MODE_HT;
2092 mcs = hweight32(le32_to_cpu(ra->supp_ht_mcs)) - 1;
2093 ra->af = sta->ht_cap.ampdu_factor;
2094 ra->ht_gf = !!(sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD);
2095
2096 cap |= STA_CAP_HT;
2097 if (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20)
2098 cap |= STA_CAP_SGI_20;
2099 if (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40)
2100 cap |= STA_CAP_SGI_40;
2101 if (sta->ht_cap.cap & IEEE80211_HT_CAP_TX_STBC)
2102 cap |= STA_CAP_TX_STBC;
2103 if (sta->ht_cap.cap & IEEE80211_HT_CAP_RX_STBC)
2104 cap |= STA_CAP_RX_STBC;
2105 if (sta->ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING)
2106 cap |= STA_CAP_LDPC;
2107 }
2108
2109 if (sta->vht_cap.vht_supported) {
2110 u16 mcs_map = le16_to_cpu(sta->vht_cap.vht_mcs.rx_mcs_map);
2111 u16 vht_mcs;
2112 u8 af, mcs_prev;
2113
2114 af = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
2115 sta->vht_cap.cap);
2116 ra->af = max_t(u8, ra->af, af);
2117
2118 cap |= STA_CAP_VHT;
2119 if (sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80)
2120 cap |= STA_CAP_VHT_SGI_80;
2121 if (sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160)
2122 cap |= STA_CAP_VHT_SGI_160;
2123 if (sta->vht_cap.cap & IEEE80211_VHT_CAP_TXSTBC)
2124 cap |= STA_CAP_VHT_TX_STBC;
2125 if (sta->vht_cap.cap & IEEE80211_VHT_CAP_RXSTBC_1)
2126 cap |= STA_CAP_VHT_RX_STBC;
2127 if (sta->vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC)
2128 cap |= STA_CAP_VHT_LDPC;
2129
2130 ra->supp_mode |= MODE_VHT;
2131 for (mcs = 0, i = 0; i < nss; i++, mcs_map >>= 2) {
2132 switch (mcs_map & 0x3) {
2133 case IEEE80211_VHT_MCS_SUPPORT_0_9:
2134 vht_mcs = GENMASK(9, 0);
2135 break;
2136 case IEEE80211_VHT_MCS_SUPPORT_0_8:
2137 vht_mcs = GENMASK(8, 0);
2138 break;
2139 case IEEE80211_VHT_MCS_SUPPORT_0_7:
2140 vht_mcs = GENMASK(7, 0);
2141 break;
2142 default:
2143 vht_mcs = 0;
2144 }
2145
2146 ra->supp_vht_mcs[i] = cpu_to_le16(vht_mcs);
2147
2148 mcs_prev = hweight16(vht_mcs) - 1;
2149 if (mcs_prev > mcs)
2150 mcs = mcs_prev;
2151
2152 /* only support 2ss on 160MHz */
2153 if (i > 1 && (ra->bw == CMD_CBW_160MHZ ||
2154 ra->bw == CMD_CBW_8080MHZ))
2155 break;
2156 }
2157 }
2158
2159 if (sta->he_cap.has_he) {
2160 ra->supp_mode |= MODE_HE;
2161 cap |= STA_CAP_HE;
2162 }
2163
2164 ra->sta_status = cpu_to_le32(cap);
2165
2166 switch (BIT(fls(ra->supp_mode) - 1)) {
2167 case MODE_VHT:
2168 ra->phy.type = MT_PHY_TYPE_VHT;
2169 ra->phy.mcs = mcs;
2170 ra->phy.nss = nss;
2171 ra->phy.stbc = !!(sta->vht_cap.cap & IEEE80211_VHT_CAP_TXSTBC);
2172 ra->phy.ldpc = !!(sta->vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC);
2173 ra->phy.sgi =
2174 !!(sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80);
2175 break;
2176 case MODE_HT:
2177 ra->phy.type = MT_PHY_TYPE_HT;
2178 ra->phy.mcs = mcs;
2179 ra->phy.ldpc = sta->ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING;
2180 ra->phy.stbc = !!(sta->ht_cap.cap & IEEE80211_HT_CAP_TX_STBC);
2181 ra->phy.sgi = !!(sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20);
2182 break;
2183 default:
2184 break;
2185 }
2186 }
2187
mt7915_mcu_add_rate_ctrl(struct mt7915_dev * dev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)2188 int mt7915_mcu_add_rate_ctrl(struct mt7915_dev *dev, struct ieee80211_vif *vif,
2189 struct ieee80211_sta *sta)
2190 {
2191 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
2192 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
2193 struct sk_buff *skb;
2194 int len = sizeof(struct sta_req_hdr) + sizeof(struct sta_rec_ra);
2195
2196 skb = mt7915_mcu_alloc_sta_req(dev, mvif, msta, len);
2197 if (IS_ERR(skb))
2198 return PTR_ERR(skb);
2199
2200 mt7915_mcu_sta_rate_ctrl_tlv(skb, dev, vif, sta);
2201
2202 return __mt76_mcu_skb_send_msg(&dev->mt76, skb,
2203 MCU_EXT_CMD_STA_REC_UPDATE, true);
2204 }
2205
2206 static int
mt7915_mcu_add_group(struct mt7915_dev * dev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)2207 mt7915_mcu_add_group(struct mt7915_dev *dev, struct ieee80211_vif *vif,
2208 struct ieee80211_sta *sta)
2209 {
2210 #define MT_STA_BSS_GROUP 1
2211 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
2212 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
2213 struct {
2214 __le32 action;
2215 u8 wlan_idx_lo;
2216 u8 status;
2217 u8 wlan_idx_hi;
2218 u8 rsv0[5];
2219 __le32 val;
2220 u8 rsv1[8];
2221 } __packed req = {
2222 .action = cpu_to_le32(MT_STA_BSS_GROUP),
2223 .wlan_idx_lo = to_wcid_lo(msta->wcid.idx),
2224 .wlan_idx_hi = to_wcid_hi(msta->wcid.idx),
2225 .val = cpu_to_le32(mvif->idx),
2226 };
2227
2228 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_SET_DRR_CTRL,
2229 &req, sizeof(req), true);
2230 }
2231
mt7915_mcu_add_sta_adv(struct mt7915_dev * dev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enable)2232 int mt7915_mcu_add_sta_adv(struct mt7915_dev *dev, struct ieee80211_vif *vif,
2233 struct ieee80211_sta *sta, bool enable)
2234 {
2235 int ret;
2236
2237 if (!sta)
2238 return 0;
2239
2240 /* must keep the order */
2241 ret = mt7915_mcu_add_group(dev, vif, sta);
2242 if (ret)
2243 return ret;
2244
2245 ret = mt7915_mcu_add_txbf(dev, vif, sta, enable);
2246 if (ret)
2247 return ret;
2248
2249 ret = mt7915_mcu_add_mu(dev, vif, sta);
2250 if (ret)
2251 return ret;
2252
2253 if (enable)
2254 return mt7915_mcu_add_rate_ctrl(dev, vif, sta);
2255
2256 return 0;
2257 }
2258
mt7915_mcu_add_sta(struct mt7915_dev * dev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enable)2259 int mt7915_mcu_add_sta(struct mt7915_dev *dev, struct ieee80211_vif *vif,
2260 struct ieee80211_sta *sta, bool enable)
2261 {
2262 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
2263 struct wtbl_req_hdr *wtbl_hdr;
2264 struct mt7915_sta *msta;
2265 struct tlv *sta_wtbl;
2266 struct sk_buff *skb;
2267
2268 msta = sta ? (struct mt7915_sta *)sta->drv_priv : &mvif->sta;
2269
2270 skb = mt7915_mcu_alloc_sta_req(dev, mvif, msta,
2271 MT7915_STA_UPDATE_MAX_SIZE);
2272 if (IS_ERR(skb))
2273 return PTR_ERR(skb);
2274
2275 mt7915_mcu_sta_basic_tlv(skb, vif, sta, enable);
2276 if (enable && sta)
2277 mt7915_mcu_sta_tlv(dev, skb, sta, vif);
2278
2279 sta_wtbl = mt7915_mcu_add_tlv(skb, STA_REC_WTBL, sizeof(struct tlv));
2280
2281 wtbl_hdr = mt7915_mcu_alloc_wtbl_req(dev, msta, WTBL_RESET_AND_SET,
2282 sta_wtbl, &skb);
2283 if (enable) {
2284 mt7915_mcu_wtbl_generic_tlv(skb, vif, sta, sta_wtbl, wtbl_hdr);
2285 if (sta)
2286 mt7915_mcu_wtbl_ht_tlv(skb, sta, sta_wtbl, wtbl_hdr);
2287 }
2288
2289 return __mt76_mcu_skb_send_msg(&dev->mt76, skb,
2290 MCU_EXT_CMD_STA_REC_UPDATE, true);
2291 }
2292
mt7915_mcu_set_fixed_rate(struct mt7915_dev * dev,struct ieee80211_sta * sta,u32 rate)2293 int mt7915_mcu_set_fixed_rate(struct mt7915_dev *dev,
2294 struct ieee80211_sta *sta, u32 rate)
2295 {
2296 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
2297 struct mt7915_vif *mvif = msta->vif;
2298 struct sta_rec_ra_fixed *ra;
2299 struct sk_buff *skb;
2300 struct tlv *tlv;
2301 int len = sizeof(struct sta_req_hdr) + sizeof(*ra);
2302
2303 skb = mt7915_mcu_alloc_sta_req(dev, mvif, msta, len);
2304 if (IS_ERR(skb))
2305 return PTR_ERR(skb);
2306
2307 tlv = mt7915_mcu_add_tlv(skb, STA_REC_RA_UPDATE, sizeof(*ra));
2308 ra = (struct sta_rec_ra_fixed *)tlv;
2309
2310 if (!rate) {
2311 ra->field = cpu_to_le32(RATE_PARAM_AUTO);
2312 goto out;
2313 } else {
2314 ra->field = cpu_to_le32(RATE_PARAM_FIXED);
2315 }
2316
2317 ra->phy.type = FIELD_GET(RATE_CFG_PHY_TYPE, rate);
2318 ra->phy.bw = FIELD_GET(RATE_CFG_BW, rate);
2319 ra->phy.nss = FIELD_GET(RATE_CFG_NSS, rate);
2320 ra->phy.mcs = FIELD_GET(RATE_CFG_MCS, rate);
2321 ra->phy.stbc = FIELD_GET(RATE_CFG_STBC, rate);
2322
2323 if (ra->phy.bw)
2324 ra->phy.ldpc = 7;
2325 else
2326 ra->phy.ldpc = FIELD_GET(RATE_CFG_LDPC, rate) * 7;
2327
2328 /* HT/VHT - SGI: 1, LGI: 0; HE - SGI: 0, MGI: 1, LGI: 2 */
2329 if (ra->phy.type > MT_PHY_TYPE_VHT)
2330 ra->phy.sgi = ra->phy.mcs * 85;
2331 else
2332 ra->phy.sgi = ra->phy.mcs * 15;
2333
2334 out:
2335 return __mt76_mcu_skb_send_msg(&dev->mt76, skb,
2336 MCU_EXT_CMD_STA_REC_UPDATE, true);
2337 }
2338
mt7915_mcu_add_dev_info(struct mt7915_dev * dev,struct ieee80211_vif * vif,bool enable)2339 int mt7915_mcu_add_dev_info(struct mt7915_dev *dev,
2340 struct ieee80211_vif *vif, bool enable)
2341 {
2342 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
2343 struct {
2344 struct req_hdr {
2345 u8 omac_idx;
2346 u8 dbdc_idx;
2347 __le16 tlv_num;
2348 u8 is_tlv_append;
2349 u8 rsv[3];
2350 } __packed hdr;
2351 struct req_tlv {
2352 __le16 tag;
2353 __le16 len;
2354 u8 active;
2355 u8 dbdc_idx;
2356 u8 omac_addr[ETH_ALEN];
2357 } __packed tlv;
2358 } data = {
2359 .hdr = {
2360 .omac_idx = mvif->omac_idx,
2361 .dbdc_idx = mvif->band_idx,
2362 .tlv_num = cpu_to_le16(1),
2363 .is_tlv_append = 1,
2364 },
2365 .tlv = {
2366 .tag = cpu_to_le16(DEV_INFO_ACTIVE),
2367 .len = cpu_to_le16(sizeof(struct req_tlv)),
2368 .active = enable,
2369 .dbdc_idx = mvif->band_idx,
2370 },
2371 };
2372
2373 memcpy(data.tlv.omac_addr, vif->addr, ETH_ALEN);
2374 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_DEV_INFO_UPDATE,
2375 &data, sizeof(data), true);
2376 }
2377
2378 static void
mt7915_mcu_beacon_csa(struct sk_buff * rskb,struct sk_buff * skb,struct bss_info_bcn * bcn,struct ieee80211_mutable_offsets * offs)2379 mt7915_mcu_beacon_csa(struct sk_buff *rskb, struct sk_buff *skb,
2380 struct bss_info_bcn *bcn,
2381 struct ieee80211_mutable_offsets *offs)
2382 {
2383 if (offs->cntdwn_counter_offs[0]) {
2384 struct tlv *tlv;
2385 struct bss_info_bcn_csa *csa;
2386
2387 tlv = mt7915_mcu_add_nested_subtlv(rskb, BSS_INFO_BCN_CSA,
2388 sizeof(*csa), &bcn->sub_ntlv,
2389 &bcn->len);
2390 csa = (struct bss_info_bcn_csa *)tlv;
2391 csa->cnt = skb->data[offs->cntdwn_counter_offs[0]];
2392 }
2393 }
2394
2395 static void
mt7915_mcu_beacon_cont(struct mt7915_dev * dev,struct sk_buff * rskb,struct sk_buff * skb,struct bss_info_bcn * bcn,struct ieee80211_mutable_offsets * offs)2396 mt7915_mcu_beacon_cont(struct mt7915_dev *dev, struct sk_buff *rskb,
2397 struct sk_buff *skb, struct bss_info_bcn *bcn,
2398 struct ieee80211_mutable_offsets *offs)
2399 {
2400 struct mt76_wcid *wcid = &dev->mt76.global_wcid;
2401 struct bss_info_bcn_cont *cont;
2402 struct tlv *tlv;
2403 u8 *buf;
2404 int len = sizeof(*cont) + MT_TXD_SIZE + skb->len;
2405
2406 tlv = mt7915_mcu_add_nested_subtlv(rskb, BSS_INFO_BCN_CONTENT,
2407 len, &bcn->sub_ntlv, &bcn->len);
2408
2409 cont = (struct bss_info_bcn_cont *)tlv;
2410 cont->pkt_len = cpu_to_le16(MT_TXD_SIZE + skb->len);
2411 cont->tim_ofs = cpu_to_le16(offs->tim_offset);
2412
2413 if (offs->cntdwn_counter_offs[0])
2414 cont->csa_ofs = cpu_to_le16(offs->cntdwn_counter_offs[0] - 4);
2415
2416 buf = (u8 *)tlv + sizeof(*cont);
2417 mt7915_mac_write_txwi(dev, (__le32 *)buf, skb, wcid, NULL,
2418 true);
2419 memcpy(buf + MT_TXD_SIZE, skb->data, skb->len);
2420 }
2421
mt7915_mcu_add_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int en)2422 int mt7915_mcu_add_beacon(struct ieee80211_hw *hw,
2423 struct ieee80211_vif *vif, int en)
2424 {
2425 #define MAX_BEACON_SIZE 512
2426 struct mt7915_dev *dev = mt7915_hw_dev(hw);
2427 struct mt7915_phy *phy = mt7915_hw_phy(hw);
2428 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
2429 struct ieee80211_mutable_offsets offs;
2430 struct ieee80211_tx_info *info;
2431 struct sk_buff *skb, *rskb;
2432 struct tlv *tlv;
2433 struct bss_info_bcn *bcn;
2434 int len = MT7915_BEACON_UPDATE_SIZE + MAX_BEACON_SIZE;
2435
2436 skb = ieee80211_beacon_get_template(hw, vif, &offs);
2437 if (!skb)
2438 return -EINVAL;
2439
2440 if (skb->len > MAX_BEACON_SIZE - MT_TXD_SIZE) {
2441 dev_err(dev->mt76.dev, "Bcn size limit exceed\n");
2442 dev_kfree_skb(skb);
2443 return -EINVAL;
2444 }
2445
2446 rskb = mt7915_mcu_alloc_sta_req(dev, mvif, NULL, len);
2447 if (IS_ERR(rskb)) {
2448 dev_kfree_skb(skb);
2449 return PTR_ERR(rskb);
2450 }
2451
2452 tlv = mt7915_mcu_add_tlv(rskb, BSS_INFO_OFFLOAD, sizeof(*bcn));
2453 bcn = (struct bss_info_bcn *)tlv;
2454 bcn->enable = en;
2455
2456 if (mvif->band_idx) {
2457 info = IEEE80211_SKB_CB(skb);
2458 info->hw_queue |= MT_TX_HW_QUEUE_EXT_PHY;
2459 }
2460
2461 /* TODO: subtag - bss color count & 11v MBSSID */
2462 mt7915_mcu_beacon_csa(rskb, skb, bcn, &offs);
2463 mt7915_mcu_beacon_cont(dev, rskb, skb, bcn, &offs);
2464 dev_kfree_skb(skb);
2465
2466 return __mt76_mcu_skb_send_msg(&phy->dev->mt76, rskb,
2467 MCU_EXT_CMD_BSS_INFO_UPDATE, true);
2468 }
2469
mt7915_mcu_send_firmware(struct mt7915_dev * dev,const void * data,int len)2470 static int mt7915_mcu_send_firmware(struct mt7915_dev *dev, const void *data,
2471 int len)
2472 {
2473 int ret = 0, cur_len;
2474
2475 while (len > 0) {
2476 cur_len = min_t(int, 4096 - sizeof(struct mt7915_mcu_txd),
2477 len);
2478
2479 ret = __mt76_mcu_send_msg(&dev->mt76, -MCU_CMD_FW_SCATTER,
2480 data, cur_len, false);
2481 if (ret)
2482 break;
2483
2484 data += cur_len;
2485 len -= cur_len;
2486 mt76_queue_tx_cleanup(dev, MT_TXQ_FWDL, false);
2487 }
2488
2489 return ret;
2490 }
2491
mt7915_mcu_start_firmware(struct mt7915_dev * dev,u32 addr,u32 option)2492 static int mt7915_mcu_start_firmware(struct mt7915_dev *dev, u32 addr,
2493 u32 option)
2494 {
2495 struct {
2496 __le32 option;
2497 __le32 addr;
2498 } req = {
2499 .option = cpu_to_le32(option),
2500 .addr = cpu_to_le32(addr),
2501 };
2502
2503 return __mt76_mcu_send_msg(&dev->mt76, -MCU_CMD_FW_START_REQ,
2504 &req, sizeof(req), true);
2505 }
2506
mt7915_mcu_restart(struct mt76_dev * dev)2507 static int mt7915_mcu_restart(struct mt76_dev *dev)
2508 {
2509 struct {
2510 u8 power_mode;
2511 u8 rsv[3];
2512 } req = {
2513 .power_mode = 1,
2514 };
2515
2516 return __mt76_mcu_send_msg(dev, -MCU_CMD_NIC_POWER_CTRL, &req,
2517 sizeof(req), false);
2518 }
2519
mt7915_mcu_patch_sem_ctrl(struct mt7915_dev * dev,bool get)2520 static int mt7915_mcu_patch_sem_ctrl(struct mt7915_dev *dev, bool get)
2521 {
2522 struct {
2523 __le32 op;
2524 } req = {
2525 .op = cpu_to_le32(get ? PATCH_SEM_GET : PATCH_SEM_RELEASE),
2526 };
2527
2528 return __mt76_mcu_send_msg(&dev->mt76, -MCU_CMD_PATCH_SEM_CONTROL,
2529 &req, sizeof(req), true);
2530 }
2531
mt7915_mcu_start_patch(struct mt7915_dev * dev)2532 static int mt7915_mcu_start_patch(struct mt7915_dev *dev)
2533 {
2534 struct {
2535 u8 check_crc;
2536 u8 reserved[3];
2537 } req = {
2538 .check_crc = 0,
2539 };
2540
2541 return __mt76_mcu_send_msg(&dev->mt76, -MCU_CMD_PATCH_FINISH_REQ,
2542 &req, sizeof(req), true);
2543 }
2544
mt7915_driver_own(struct mt7915_dev * dev)2545 static int mt7915_driver_own(struct mt7915_dev *dev)
2546 {
2547 u32 reg = mt7915_reg_map_l1(dev, MT_TOP_LPCR_HOST_BAND0);
2548
2549 mt76_wr(dev, reg, MT_TOP_LPCR_HOST_DRV_OWN);
2550 if (!mt76_poll_msec(dev, reg, MT_TOP_LPCR_HOST_FW_OWN,
2551 0, 500)) {
2552 dev_err(dev->mt76.dev, "Timeout for driver own\n");
2553 return -EIO;
2554 }
2555
2556 return 0;
2557 }
2558
mt7915_mcu_init_download(struct mt7915_dev * dev,u32 addr,u32 len,u32 mode)2559 static int mt7915_mcu_init_download(struct mt7915_dev *dev, u32 addr,
2560 u32 len, u32 mode)
2561 {
2562 struct {
2563 __le32 addr;
2564 __le32 len;
2565 __le32 mode;
2566 } req = {
2567 .addr = cpu_to_le32(addr),
2568 .len = cpu_to_le32(len),
2569 .mode = cpu_to_le32(mode),
2570 };
2571 int attr;
2572
2573 if (req.addr == cpu_to_le32(MCU_PATCH_ADDRESS))
2574 attr = -MCU_CMD_PATCH_START_REQ;
2575 else
2576 attr = -MCU_CMD_TARGET_ADDRESS_LEN_REQ;
2577
2578 return __mt76_mcu_send_msg(&dev->mt76, attr, &req, sizeof(req), true);
2579 }
2580
mt7915_load_patch(struct mt7915_dev * dev)2581 static int mt7915_load_patch(struct mt7915_dev *dev)
2582 {
2583 const struct mt7915_patch_hdr *hdr;
2584 const struct firmware *fw = NULL;
2585 int i, ret, sem;
2586
2587 sem = mt7915_mcu_patch_sem_ctrl(dev, 1);
2588 switch (sem) {
2589 case PATCH_IS_DL:
2590 return 0;
2591 case PATCH_NOT_DL_SEM_SUCCESS:
2592 break;
2593 default:
2594 dev_err(dev->mt76.dev, "Failed to get patch semaphore\n");
2595 return -EAGAIN;
2596 }
2597
2598 ret = request_firmware(&fw, MT7915_ROM_PATCH, dev->mt76.dev);
2599 if (ret)
2600 goto out;
2601
2602 if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
2603 dev_err(dev->mt76.dev, "Invalid firmware\n");
2604 ret = -EINVAL;
2605 goto out;
2606 }
2607
2608 hdr = (const struct mt7915_patch_hdr *)(fw->data);
2609
2610 dev_info(dev->mt76.dev, "HW/SW Version: 0x%x, Build Time: %.16s\n",
2611 be32_to_cpu(hdr->hw_sw_ver), hdr->build_date);
2612
2613 for (i = 0; i < be32_to_cpu(hdr->desc.n_region); i++) {
2614 struct mt7915_patch_sec *sec;
2615 const u8 *dl;
2616 u32 len, addr;
2617
2618 sec = (struct mt7915_patch_sec *)(fw->data + sizeof(*hdr) +
2619 i * sizeof(*sec));
2620 if ((be32_to_cpu(sec->type) & PATCH_SEC_TYPE_MASK) !=
2621 PATCH_SEC_TYPE_INFO) {
2622 ret = -EINVAL;
2623 goto out;
2624 }
2625
2626 addr = be32_to_cpu(sec->info.addr);
2627 len = be32_to_cpu(sec->info.len);
2628 dl = fw->data + be32_to_cpu(sec->offs);
2629
2630 ret = mt7915_mcu_init_download(dev, addr, len,
2631 DL_MODE_NEED_RSP);
2632 if (ret) {
2633 dev_err(dev->mt76.dev, "Download request failed\n");
2634 goto out;
2635 }
2636
2637 ret = mt7915_mcu_send_firmware(dev, dl, len);
2638 if (ret) {
2639 dev_err(dev->mt76.dev, "Failed to send patch\n");
2640 goto out;
2641 }
2642 }
2643
2644 ret = mt7915_mcu_start_patch(dev);
2645 if (ret)
2646 dev_err(dev->mt76.dev, "Failed to start patch\n");
2647
2648 out:
2649 sem = mt7915_mcu_patch_sem_ctrl(dev, 0);
2650 switch (sem) {
2651 case PATCH_REL_SEM_SUCCESS:
2652 break;
2653 default:
2654 ret = -EAGAIN;
2655 dev_err(dev->mt76.dev, "Failed to release patch semaphore\n");
2656 break;
2657 }
2658 release_firmware(fw);
2659
2660 return ret;
2661 }
2662
mt7915_mcu_gen_dl_mode(u8 feature_set,bool is_wa)2663 static u32 mt7915_mcu_gen_dl_mode(u8 feature_set, bool is_wa)
2664 {
2665 u32 ret = 0;
2666
2667 ret |= (feature_set & FW_FEATURE_SET_ENCRYPT) ?
2668 (DL_MODE_ENCRYPT | DL_MODE_RESET_SEC_IV) : 0;
2669 ret |= FIELD_PREP(DL_MODE_KEY_IDX,
2670 FIELD_GET(FW_FEATURE_SET_KEY_IDX, feature_set));
2671 ret |= DL_MODE_NEED_RSP;
2672 ret |= is_wa ? DL_MODE_WORKING_PDA_CR4 : 0;
2673
2674 return ret;
2675 }
2676
2677 static int
mt7915_mcu_send_ram_firmware(struct mt7915_dev * dev,const struct mt7915_fw_trailer * hdr,const u8 * data,bool is_wa)2678 mt7915_mcu_send_ram_firmware(struct mt7915_dev *dev,
2679 const struct mt7915_fw_trailer *hdr,
2680 const u8 *data, bool is_wa)
2681 {
2682 int i, offset = 0;
2683 u32 override = 0, option = 0;
2684
2685 for (i = 0; i < hdr->n_region; i++) {
2686 const struct mt7915_fw_region *region;
2687 int err;
2688 u32 len, addr, mode;
2689
2690 region = (const struct mt7915_fw_region *)((const u8 *)hdr -
2691 (hdr->n_region - i) * sizeof(*region));
2692 mode = mt7915_mcu_gen_dl_mode(region->feature_set, is_wa);
2693 len = le32_to_cpu(region->len);
2694 addr = le32_to_cpu(region->addr);
2695
2696 if (region->feature_set & FW_FEATURE_OVERRIDE_ADDR)
2697 override = addr;
2698
2699 err = mt7915_mcu_init_download(dev, addr, len, mode);
2700 if (err) {
2701 dev_err(dev->mt76.dev, "Download request failed\n");
2702 return err;
2703 }
2704
2705 err = mt7915_mcu_send_firmware(dev, data + offset, len);
2706 if (err) {
2707 dev_err(dev->mt76.dev, "Failed to send firmware.\n");
2708 return err;
2709 }
2710
2711 offset += len;
2712 }
2713
2714 if (override)
2715 option |= FW_START_OVERRIDE;
2716
2717 if (is_wa)
2718 option |= FW_START_WORKING_PDA_CR4;
2719
2720 return mt7915_mcu_start_firmware(dev, override, option);
2721 }
2722
mt7915_load_ram(struct mt7915_dev * dev)2723 static int mt7915_load_ram(struct mt7915_dev *dev)
2724 {
2725 const struct mt7915_fw_trailer *hdr;
2726 const struct firmware *fw;
2727 int ret;
2728
2729 ret = request_firmware(&fw, MT7915_FIRMWARE_WM, dev->mt76.dev);
2730 if (ret)
2731 return ret;
2732
2733 if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
2734 dev_err(dev->mt76.dev, "Invalid firmware\n");
2735 ret = -EINVAL;
2736 goto out;
2737 }
2738
2739 hdr = (const struct mt7915_fw_trailer *)(fw->data + fw->size -
2740 sizeof(*hdr));
2741
2742 dev_info(dev->mt76.dev, "WM Firmware Version: %.10s, Build Time: %.15s\n",
2743 hdr->fw_ver, hdr->build_date);
2744
2745 ret = mt7915_mcu_send_ram_firmware(dev, hdr, fw->data, false);
2746 if (ret) {
2747 dev_err(dev->mt76.dev, "Failed to start WM firmware\n");
2748 goto out;
2749 }
2750
2751 release_firmware(fw);
2752
2753 ret = request_firmware(&fw, MT7915_FIRMWARE_WA, dev->mt76.dev);
2754 if (ret)
2755 return ret;
2756
2757 if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
2758 dev_err(dev->mt76.dev, "Invalid firmware\n");
2759 ret = -EINVAL;
2760 goto out;
2761 }
2762
2763 hdr = (const struct mt7915_fw_trailer *)(fw->data + fw->size -
2764 sizeof(*hdr));
2765
2766 dev_info(dev->mt76.dev, "WA Firmware Version: %.10s, Build Time: %.15s\n",
2767 hdr->fw_ver, hdr->build_date);
2768
2769 ret = mt7915_mcu_send_ram_firmware(dev, hdr, fw->data, true);
2770 if (ret) {
2771 dev_err(dev->mt76.dev, "Failed to start WA firmware\n");
2772 goto out;
2773 }
2774
2775 snprintf(dev->mt76.hw->wiphy->fw_version,
2776 sizeof(dev->mt76.hw->wiphy->fw_version),
2777 "%.10s-%.15s", hdr->fw_ver, hdr->build_date);
2778
2779 out:
2780 release_firmware(fw);
2781
2782 return ret;
2783 }
2784
mt7915_load_firmware(struct mt7915_dev * dev)2785 static int mt7915_load_firmware(struct mt7915_dev *dev)
2786 {
2787 int ret;
2788 u32 val, reg = mt7915_reg_map_l1(dev, MT_TOP_MISC);
2789
2790 val = FIELD_PREP(MT_TOP_MISC_FW_STATE, FW_STATE_FW_DOWNLOAD);
2791
2792 if (!mt76_poll_msec(dev, reg, MT_TOP_MISC_FW_STATE, val, 1000)) {
2793 /* restart firmware once */
2794 __mt76_mcu_restart(&dev->mt76);
2795 if (!mt76_poll_msec(dev, reg, MT_TOP_MISC_FW_STATE,
2796 val, 1000)) {
2797 dev_err(dev->mt76.dev,
2798 "Firmware is not ready for download\n");
2799 return -EIO;
2800 }
2801 }
2802
2803 ret = mt7915_load_patch(dev);
2804 if (ret)
2805 return ret;
2806
2807 ret = mt7915_load_ram(dev);
2808 if (ret)
2809 return ret;
2810
2811 if (!mt76_poll_msec(dev, reg, MT_TOP_MISC_FW_STATE,
2812 FIELD_PREP(MT_TOP_MISC_FW_STATE,
2813 FW_STATE_WACPU_RDY), 1000)) {
2814 dev_err(dev->mt76.dev, "Timeout for initializing firmware\n");
2815 return -EIO;
2816 }
2817
2818 mt76_queue_tx_cleanup(dev, MT_TXQ_FWDL, false);
2819
2820 dev_dbg(dev->mt76.dev, "Firmware init done\n");
2821
2822 return 0;
2823 }
2824
mt7915_mcu_fw_log_2_host(struct mt7915_dev * dev,u8 ctrl)2825 int mt7915_mcu_fw_log_2_host(struct mt7915_dev *dev, u8 ctrl)
2826 {
2827 struct {
2828 u8 ctrl_val;
2829 u8 pad[3];
2830 } data = {
2831 .ctrl_val = ctrl
2832 };
2833
2834 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_FW_LOG_2_HOST,
2835 &data, sizeof(data), true);
2836 }
2837
mt7915_mcu_fw_dbg_ctrl(struct mt7915_dev * dev,u32 module,u8 level)2838 int mt7915_mcu_fw_dbg_ctrl(struct mt7915_dev *dev, u32 module, u8 level)
2839 {
2840 struct {
2841 u8 ver;
2842 u8 pad;
2843 __le16 len;
2844 u8 level;
2845 u8 rsv[3];
2846 __le32 module_idx;
2847 } data = {
2848 .module_idx = cpu_to_le32(module),
2849 .level = level,
2850 };
2851
2852 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_FW_DBG_CTRL,
2853 &data, sizeof(data), false);
2854 }
2855
mt7915_mcu_init(struct mt7915_dev * dev)2856 int mt7915_mcu_init(struct mt7915_dev *dev)
2857 {
2858 static const struct mt76_mcu_ops mt7915_mcu_ops = {
2859 .headroom = sizeof(struct mt7915_mcu_txd),
2860 .mcu_skb_send_msg = mt7915_mcu_send_message,
2861 .mcu_send_msg = mt7915_mcu_msg_send,
2862 .mcu_restart = mt7915_mcu_restart,
2863 };
2864 int ret;
2865
2866 dev->mt76.mcu_ops = &mt7915_mcu_ops,
2867
2868 ret = mt7915_driver_own(dev);
2869 if (ret)
2870 return ret;
2871
2872 ret = mt7915_load_firmware(dev);
2873 if (ret)
2874 return ret;
2875
2876 set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
2877 mt7915_mcu_fw_log_2_host(dev, 0);
2878
2879 return 0;
2880 }
2881
mt7915_mcu_exit(struct mt7915_dev * dev)2882 void mt7915_mcu_exit(struct mt7915_dev *dev)
2883 {
2884 u32 reg = mt7915_reg_map_l1(dev, MT_TOP_MISC);
2885
2886 __mt76_mcu_restart(&dev->mt76);
2887 if (!mt76_poll_msec(dev, reg, MT_TOP_MISC_FW_STATE,
2888 FIELD_PREP(MT_TOP_MISC_FW_STATE,
2889 FW_STATE_FW_DOWNLOAD), 1000)) {
2890 dev_err(dev->mt76.dev, "Failed to exit mcu\n");
2891 return;
2892 }
2893
2894 reg = mt7915_reg_map_l1(dev, MT_TOP_LPCR_HOST_BAND0);
2895 mt76_wr(dev, reg, MT_TOP_LPCR_HOST_FW_OWN);
2896 skb_queue_purge(&dev->mt76.mcu.res_q);
2897 }
2898
mt7915_mcu_set_mac(struct mt7915_dev * dev,int band,bool enable,bool hdr_trans)2899 int mt7915_mcu_set_mac(struct mt7915_dev *dev, int band,
2900 bool enable, bool hdr_trans)
2901 {
2902 struct {
2903 u8 operation;
2904 u8 enable;
2905 u8 check_bssid;
2906 u8 insert_vlan;
2907 u8 remove_vlan;
2908 u8 tid;
2909 u8 mode;
2910 u8 rsv;
2911 } __packed req_trans = {
2912 .enable = hdr_trans,
2913 };
2914 struct {
2915 u8 enable;
2916 u8 band;
2917 u8 rsv[2];
2918 } __packed req_mac = {
2919 .enable = enable,
2920 .band = band,
2921 };
2922 int ret;
2923
2924 ret = __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_RX_HDR_TRANS,
2925 &req_trans, sizeof(req_trans), false);
2926 if (ret)
2927 return ret;
2928
2929 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_MAC_INIT_CTRL,
2930 &req_mac, sizeof(req_mac), true);
2931 }
2932
mt7915_mcu_set_scs(struct mt7915_dev * dev,u8 band,bool enable)2933 int mt7915_mcu_set_scs(struct mt7915_dev *dev, u8 band, bool enable)
2934 {
2935 struct {
2936 __le32 cmd;
2937 u8 band;
2938 u8 enable;
2939 } __packed req = {
2940 .cmd = cpu_to_le32(SCS_ENABLE),
2941 .band = band,
2942 .enable = enable + 1,
2943 };
2944
2945 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_SCS_CTRL, &req,
2946 sizeof(req), false);
2947 }
2948
mt7915_mcu_set_rts_thresh(struct mt7915_phy * phy,u32 val)2949 int mt7915_mcu_set_rts_thresh(struct mt7915_phy *phy, u32 val)
2950 {
2951 struct mt7915_dev *dev = phy->dev;
2952 struct {
2953 u8 prot_idx;
2954 u8 band;
2955 u8 rsv[2];
2956 __le32 len_thresh;
2957 __le32 pkt_thresh;
2958 } __packed req = {
2959 .prot_idx = 1,
2960 .band = phy != &dev->phy,
2961 .len_thresh = cpu_to_le32(val),
2962 .pkt_thresh = cpu_to_le32(0x2),
2963 };
2964
2965 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_PROTECT_CTRL,
2966 &req, sizeof(req), true);
2967 }
2968
mt7915_mcu_set_tx(struct mt7915_dev * dev,struct ieee80211_vif * vif)2969 int mt7915_mcu_set_tx(struct mt7915_dev *dev, struct ieee80211_vif *vif)
2970 {
2971 #define WMM_AIFS_SET BIT(0)
2972 #define WMM_CW_MIN_SET BIT(1)
2973 #define WMM_CW_MAX_SET BIT(2)
2974 #define WMM_TXOP_SET BIT(3)
2975 #define WMM_PARAM_SET GENMASK(3, 0)
2976 #define TX_CMD_MODE 1
2977 struct edca {
2978 u8 queue;
2979 u8 set;
2980 u8 aifs;
2981 u8 cw_min;
2982 __le16 cw_max;
2983 __le16 txop;
2984 };
2985 struct mt7915_mcu_tx {
2986 u8 total;
2987 u8 action;
2988 u8 valid;
2989 u8 mode;
2990
2991 struct edca edca[IEEE80211_NUM_ACS];
2992 } __packed req = {
2993 .valid = true,
2994 .mode = TX_CMD_MODE,
2995 .total = IEEE80211_NUM_ACS,
2996 };
2997 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
2998 int ac;
2999
3000 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
3001 struct ieee80211_tx_queue_params *q = &mvif->queue_params[ac];
3002 struct edca *e = &req.edca[ac];
3003
3004 e->set = WMM_PARAM_SET;
3005 e->queue = ac + mvif->wmm_idx * MT7915_MAX_WMM_SETS;
3006 e->aifs = q->aifs;
3007 e->txop = cpu_to_le16(q->txop);
3008
3009 if (q->cw_min)
3010 e->cw_min = fls(q->cw_min);
3011 else
3012 e->cw_min = 5;
3013
3014 if (q->cw_max)
3015 e->cw_max = cpu_to_le16(fls(q->cw_max));
3016 else
3017 e->cw_max = cpu_to_le16(10);
3018 }
3019 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_EDCA_UPDATE,
3020 &req, sizeof(req), true);
3021 }
3022
mt7915_mcu_set_pm(struct mt7915_dev * dev,int band,int enter)3023 int mt7915_mcu_set_pm(struct mt7915_dev *dev, int band, int enter)
3024 {
3025 #define ENTER_PM_STATE 1
3026 #define EXIT_PM_STATE 2
3027 struct {
3028 u8 pm_number;
3029 u8 pm_state;
3030 u8 bssid[ETH_ALEN];
3031 u8 dtim_period;
3032 u8 wlan_idx_lo;
3033 __le16 bcn_interval;
3034 __le32 aid;
3035 __le32 rx_filter;
3036 u8 band_idx;
3037 u8 wlan_idx_hi;
3038 u8 rsv[2];
3039 __le32 feature;
3040 u8 omac_idx;
3041 u8 wmm_idx;
3042 u8 bcn_loss_cnt;
3043 u8 bcn_sp_duration;
3044 } __packed req = {
3045 .pm_number = 5,
3046 .pm_state = (enter) ? ENTER_PM_STATE : EXIT_PM_STATE,
3047 .band_idx = band,
3048 };
3049
3050 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_PM_STATE_CTRL,
3051 &req, sizeof(req), true);
3052 }
3053
mt7915_mcu_rdd_cmd(struct mt7915_dev * dev,enum mt7915_rdd_cmd cmd,u8 index,u8 rx_sel,u8 val)3054 int mt7915_mcu_rdd_cmd(struct mt7915_dev *dev,
3055 enum mt7915_rdd_cmd cmd, u8 index,
3056 u8 rx_sel, u8 val)
3057 {
3058 struct {
3059 u8 ctrl;
3060 u8 rdd_idx;
3061 u8 rdd_rx_sel;
3062 u8 val;
3063 u8 rsv[4];
3064 } __packed req = {
3065 .ctrl = cmd,
3066 .rdd_idx = index,
3067 .rdd_rx_sel = rx_sel,
3068 .val = val,
3069 };
3070
3071 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_SET_RDD_CTRL,
3072 &req, sizeof(req), true);
3073 }
3074
mt7915_mcu_set_fcc5_lpn(struct mt7915_dev * dev,int val)3075 int mt7915_mcu_set_fcc5_lpn(struct mt7915_dev *dev, int val)
3076 {
3077 struct {
3078 __le32 tag;
3079 __le16 min_lpn;
3080 u8 rsv[2];
3081 } __packed req = {
3082 .tag = cpu_to_le32(0x1),
3083 .min_lpn = cpu_to_le16(val),
3084 };
3085
3086 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_SET_RDD_TH,
3087 &req, sizeof(req), true);
3088 }
3089
mt7915_mcu_set_pulse_th(struct mt7915_dev * dev,const struct mt7915_dfs_pulse * pulse)3090 int mt7915_mcu_set_pulse_th(struct mt7915_dev *dev,
3091 const struct mt7915_dfs_pulse *pulse)
3092 {
3093 struct {
3094 __le32 tag;
3095
3096 __le32 max_width; /* us */
3097 __le32 max_pwr; /* dbm */
3098 __le32 min_pwr; /* dbm */
3099 __le32 min_stgr_pri; /* us */
3100 __le32 max_stgr_pri; /* us */
3101 __le32 min_cr_pri; /* us */
3102 __le32 max_cr_pri; /* us */
3103 } __packed req = {
3104 .tag = cpu_to_le32(0x3),
3105
3106 #define __req_field(field) .field = cpu_to_le32(pulse->field)
3107 __req_field(max_width),
3108 __req_field(max_pwr),
3109 __req_field(min_pwr),
3110 __req_field(min_stgr_pri),
3111 __req_field(max_stgr_pri),
3112 __req_field(min_cr_pri),
3113 __req_field(max_cr_pri),
3114 #undef __req_field
3115 };
3116
3117 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_SET_RDD_TH,
3118 &req, sizeof(req), true);
3119 }
3120
mt7915_mcu_set_radar_th(struct mt7915_dev * dev,int index,const struct mt7915_dfs_pattern * pattern)3121 int mt7915_mcu_set_radar_th(struct mt7915_dev *dev, int index,
3122 const struct mt7915_dfs_pattern *pattern)
3123 {
3124 struct {
3125 __le32 tag;
3126 __le16 radar_type;
3127
3128 u8 enb;
3129 u8 stgr;
3130 u8 min_crpn;
3131 u8 max_crpn;
3132 u8 min_crpr;
3133 u8 min_pw;
3134 u32 min_pri;
3135 u32 max_pri;
3136 u8 max_pw;
3137 u8 min_crbn;
3138 u8 max_crbn;
3139 u8 min_stgpn;
3140 u8 max_stgpn;
3141 u8 min_stgpr;
3142 u8 rsv[2];
3143 u32 min_stgpr_diff;
3144 } __packed req = {
3145 .tag = cpu_to_le32(0x2),
3146 .radar_type = cpu_to_le16(index),
3147
3148 #define __req_field_u8(field) .field = pattern->field
3149 #define __req_field_u32(field) .field = cpu_to_le32(pattern->field)
3150 __req_field_u8(enb),
3151 __req_field_u8(stgr),
3152 __req_field_u8(min_crpn),
3153 __req_field_u8(max_crpn),
3154 __req_field_u8(min_crpr),
3155 __req_field_u8(min_pw),
3156 __req_field_u32(min_pri),
3157 __req_field_u32(max_pri),
3158 __req_field_u8(max_pw),
3159 __req_field_u8(min_crbn),
3160 __req_field_u8(max_crbn),
3161 __req_field_u8(min_stgpn),
3162 __req_field_u8(max_stgpn),
3163 __req_field_u8(min_stgpr),
3164 __req_field_u32(min_stgpr_diff),
3165 #undef __req_field_u8
3166 #undef __req_field_u32
3167 };
3168
3169 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_SET_RDD_TH,
3170 &req, sizeof(req), true);
3171 }
3172
mt7915_mcu_set_chan_info(struct mt7915_phy * phy,int cmd)3173 int mt7915_mcu_set_chan_info(struct mt7915_phy *phy, int cmd)
3174 {
3175 struct mt7915_dev *dev = phy->dev;
3176 struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
3177 int freq1 = chandef->center_freq1;
3178 struct {
3179 u8 control_ch;
3180 u8 center_ch;
3181 u8 bw;
3182 u8 tx_streams_num;
3183 u8 rx_streams; /* mask or num */
3184 u8 switch_reason;
3185 u8 band_idx;
3186 u8 center_ch2; /* for 80+80 only */
3187 __le16 cac_case;
3188 u8 channel_band;
3189 u8 rsv0;
3190 __le32 outband_freq;
3191 u8 txpower_drop;
3192 u8 ap_bw;
3193 u8 ap_center_ch;
3194 u8 rsv1[57];
3195 } __packed req = {
3196 .control_ch = chandef->chan->hw_value,
3197 .center_ch = ieee80211_frequency_to_channel(freq1),
3198 .bw = mt7915_mcu_chan_bw(chandef),
3199 .tx_streams_num = hweight8(phy->mt76->antenna_mask),
3200 .rx_streams = phy->chainmask,
3201 .band_idx = phy != &dev->phy,
3202 .channel_band = chandef->chan->band,
3203 };
3204
3205 if (dev->mt76.hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
3206 req.switch_reason = CH_SWITCH_SCAN_BYPASS_DPD;
3207 else if ((chandef->chan->flags & IEEE80211_CHAN_RADAR) &&
3208 chandef->chan->dfs_state != NL80211_DFS_AVAILABLE)
3209 req.switch_reason = CH_SWITCH_DFS;
3210 else
3211 req.switch_reason = CH_SWITCH_NORMAL;
3212
3213 if (cmd == MCU_EXT_CMD_CHANNEL_SWITCH)
3214 req.rx_streams = hweight8(req.rx_streams);
3215
3216 if (chandef->width == NL80211_CHAN_WIDTH_80P80) {
3217 int freq2 = chandef->center_freq2;
3218
3219 req.center_ch2 = ieee80211_frequency_to_channel(freq2);
3220 }
3221
3222 return __mt76_mcu_send_msg(&dev->mt76, cmd, &req, sizeof(req), true);
3223 }
3224
mt7915_mcu_set_eeprom(struct mt7915_dev * dev)3225 int mt7915_mcu_set_eeprom(struct mt7915_dev *dev)
3226 {
3227 struct req_hdr {
3228 u8 buffer_mode;
3229 u8 format;
3230 __le16 len;
3231 } __packed req = {
3232 .buffer_mode = EE_MODE_EFUSE,
3233 .format = EE_FORMAT_WHOLE,
3234 };
3235
3236 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_EFUSE_BUFFER_MODE,
3237 &req, sizeof(req), true);
3238 }
3239
mt7915_mcu_get_eeprom(struct mt7915_dev * dev,u32 offset)3240 int mt7915_mcu_get_eeprom(struct mt7915_dev *dev, u32 offset)
3241 {
3242 struct mt7915_mcu_eeprom_info req = {
3243 .addr = cpu_to_le32(round_down(offset, 16)),
3244 };
3245
3246 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_EFUSE_ACCESS, &req,
3247 sizeof(req), true);
3248 }
3249
mt7915_mcu_get_temperature(struct mt7915_dev * dev,int index)3250 int mt7915_mcu_get_temperature(struct mt7915_dev *dev, int index)
3251 {
3252 struct {
3253 u8 ctrl_id;
3254 u8 action;
3255 u8 band;
3256 u8 rsv[5];
3257 } req = {
3258 .ctrl_id = THERMAL_SENSOR_TEMP_QUERY,
3259 .action = index,
3260 };
3261
3262 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_THERMAL_CTRL, &req,
3263 sizeof(req), true);
3264 }
3265
mt7915_mcu_get_rate_info(struct mt7915_dev * dev,u32 cmd,u16 wlan_idx)3266 int mt7915_mcu_get_rate_info(struct mt7915_dev *dev, u32 cmd, u16 wlan_idx)
3267 {
3268 struct {
3269 __le32 cmd;
3270 __le16 wlan_idx;
3271 __le16 ru_idx;
3272 __le16 direction;
3273 __le16 dump_group;
3274 } req = {
3275 .cmd = cpu_to_le32(cmd),
3276 .wlan_idx = cpu_to_le16(wlan_idx),
3277 .dump_group = cpu_to_le16(1),
3278 };
3279
3280 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_RATE_CTRL, &req,
3281 sizeof(req), false);
3282 }
3283
mt7915_mcu_set_sku(struct mt7915_phy * phy)3284 int mt7915_mcu_set_sku(struct mt7915_phy *phy)
3285 {
3286 struct mt7915_dev *dev = phy->dev;
3287 struct mt76_phy *mphy = phy->mt76;
3288 struct ieee80211_hw *hw = mphy->hw;
3289 struct mt7915_sku_val {
3290 u8 format_id;
3291 u8 limit_type;
3292 u8 dbdc_idx;
3293 s8 val[MT7915_SKU_RATE_NUM];
3294 } __packed req = {
3295 .format_id = 4,
3296 .dbdc_idx = phy != &dev->phy,
3297 };
3298 int i;
3299 s8 *delta;
3300
3301 delta = dev->rate_power[mphy->chandef.chan->band];
3302 mphy->txpower_cur = hw->conf.power_level * 2 +
3303 delta[MT7915_SKU_MAX_DELTA_IDX];
3304
3305 for (i = 0; i < MT7915_SKU_RATE_NUM; i++)
3306 req.val[i] = hw->conf.power_level * 2 + delta[i];
3307
3308 return __mt76_mcu_send_msg(&dev->mt76,
3309 MCU_EXT_CMD_TX_POWER_FEATURE_CTRL,
3310 &req, sizeof(req), true);
3311 }
3312
mt7915_mcu_set_sku_en(struct mt7915_phy * phy,bool enable)3313 int mt7915_mcu_set_sku_en(struct mt7915_phy *phy, bool enable)
3314 {
3315 struct mt7915_dev *dev = phy->dev;
3316 struct mt7915_sku {
3317 u8 format_id;
3318 u8 sku_enable;
3319 u8 dbdc_idx;
3320 u8 rsv;
3321 } __packed req = {
3322 .format_id = 0,
3323 .dbdc_idx = phy != &dev->phy,
3324 .sku_enable = enable,
3325 };
3326
3327 return __mt76_mcu_send_msg(&dev->mt76,
3328 MCU_EXT_CMD_TX_POWER_FEATURE_CTRL,
3329 &req, sizeof(req), true);
3330 }
3331
mt7915_mcu_set_ser(struct mt7915_dev * dev,u8 action,u8 set,u8 band)3332 int mt7915_mcu_set_ser(struct mt7915_dev *dev, u8 action, u8 set, u8 band)
3333 {
3334 struct {
3335 u8 action;
3336 u8 set;
3337 u8 band;
3338 u8 rsv;
3339 } req = {
3340 .action = action,
3341 .set = set,
3342 .band = band,
3343 };
3344
3345 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_SET_SER_TRIGGER,
3346 &req, sizeof(req), false);
3347 }
3348
mt7915_mcu_set_txbf_type(struct mt7915_dev * dev)3349 int mt7915_mcu_set_txbf_type(struct mt7915_dev *dev)
3350 {
3351 #define MT_BF_TYPE_UPDATE 20
3352 struct {
3353 u8 action;
3354 bool ebf;
3355 bool ibf;
3356 u8 rsv;
3357 } __packed req = {
3358 .action = MT_BF_TYPE_UPDATE,
3359 .ebf = true,
3360 .ibf = false,
3361 };
3362
3363 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_TXBF_ACTION,
3364 &req, sizeof(req), true);
3365 }
3366
mt7915_mcu_set_txbf_sounding(struct mt7915_dev * dev)3367 int mt7915_mcu_set_txbf_sounding(struct mt7915_dev *dev)
3368 {
3369 #define MT_BF_PROCESSING 4
3370 struct {
3371 u8 action;
3372 u8 snd_mode;
3373 u8 sta_num;
3374 u8 rsv;
3375 u8 wlan_idx[4];
3376 __le32 snd_period; /* ms */
3377 } __packed req = {
3378 .action = true,
3379 .snd_mode = MT_BF_PROCESSING,
3380 };
3381
3382 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_TXBF_ACTION,
3383 &req, sizeof(req), true);
3384 }
3385
mt7915_mcu_add_obss_spr(struct mt7915_dev * dev,struct ieee80211_vif * vif,bool enable)3386 int mt7915_mcu_add_obss_spr(struct mt7915_dev *dev, struct ieee80211_vif *vif,
3387 bool enable)
3388 {
3389 #define MT_SPR_ENABLE 1
3390 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
3391 struct {
3392 u8 action;
3393 u8 arg_num;
3394 u8 band_idx;
3395 u8 status;
3396 u8 drop_tx_idx;
3397 u8 sta_idx; /* 256 sta */
3398 u8 rsv[2];
3399 __le32 val;
3400 } __packed req = {
3401 .action = MT_SPR_ENABLE,
3402 .arg_num = 1,
3403 .band_idx = mvif->band_idx,
3404 .val = cpu_to_le32(enable),
3405 };
3406
3407 return __mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD_SET_SPR,
3408 &req, sizeof(req), true);
3409 }
3410