mt76x02_mac.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. // SPDX-License-Identifier: BSD-3-Clause-Clear
  2. /*
  3. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  4. * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
  5. */
  6. #include "mt76x02.h"
  7. #include "mt76x02_trace.h"
  8. #include "trace.h"
  9. void mt76x02_mac_reset_counters(struct mt76x02_dev *dev)
  10. {
  11. int i;
  12. mt76_rr(dev, MT_RX_STAT_0);
  13. mt76_rr(dev, MT_RX_STAT_1);
  14. mt76_rr(dev, MT_RX_STAT_2);
  15. mt76_rr(dev, MT_TX_STA_0);
  16. mt76_rr(dev, MT_TX_STA_1);
  17. mt76_rr(dev, MT_TX_STA_2);
  18. for (i = 0; i < 16; i++)
  19. mt76_rr(dev, MT_TX_AGG_CNT(i));
  20. for (i = 0; i < 16; i++)
  21. mt76_rr(dev, MT_TX_STAT_FIFO);
  22. memset(dev->mphy.aggr_stats, 0, sizeof(dev->mphy.aggr_stats));
  23. }
  24. EXPORT_SYMBOL_GPL(mt76x02_mac_reset_counters);
  25. static enum mt76x02_cipher_type
  26. mt76x02_mac_get_key_info(struct ieee80211_key_conf *key, u8 *key_data)
  27. {
  28. memset(key_data, 0, 32);
  29. if (!key)
  30. return MT76X02_CIPHER_NONE;
  31. if (key->keylen > 32)
  32. return MT76X02_CIPHER_NONE;
  33. memcpy(key_data, key->key, key->keylen);
  34. switch (key->cipher) {
  35. case WLAN_CIPHER_SUITE_WEP40:
  36. return MT76X02_CIPHER_WEP40;
  37. case WLAN_CIPHER_SUITE_WEP104:
  38. return MT76X02_CIPHER_WEP104;
  39. case WLAN_CIPHER_SUITE_TKIP:
  40. return MT76X02_CIPHER_TKIP;
  41. case WLAN_CIPHER_SUITE_CCMP:
  42. return MT76X02_CIPHER_AES_CCMP;
  43. default:
  44. return MT76X02_CIPHER_NONE;
  45. }
  46. }
  47. int mt76x02_mac_shared_key_setup(struct mt76x02_dev *dev, u8 vif_idx,
  48. u8 key_idx, struct ieee80211_key_conf *key)
  49. {
  50. enum mt76x02_cipher_type cipher;
  51. u8 key_data[32];
  52. u32 val;
  53. cipher = mt76x02_mac_get_key_info(key, key_data);
  54. if (cipher == MT76X02_CIPHER_NONE && key)
  55. return -EOPNOTSUPP;
  56. val = mt76_rr(dev, MT_SKEY_MODE(vif_idx));
  57. val &= ~(MT_SKEY_MODE_MASK << MT_SKEY_MODE_SHIFT(vif_idx, key_idx));
  58. val |= cipher << MT_SKEY_MODE_SHIFT(vif_idx, key_idx);
  59. mt76_wr(dev, MT_SKEY_MODE(vif_idx), val);
  60. mt76_wr_copy(dev, MT_SKEY(vif_idx, key_idx), key_data,
  61. sizeof(key_data));
  62. return 0;
  63. }
  64. EXPORT_SYMBOL_GPL(mt76x02_mac_shared_key_setup);
  65. void mt76x02_mac_wcid_sync_pn(struct mt76x02_dev *dev, u8 idx,
  66. struct ieee80211_key_conf *key)
  67. {
  68. enum mt76x02_cipher_type cipher;
  69. u8 key_data[32];
  70. u32 iv, eiv;
  71. u64 pn;
  72. cipher = mt76x02_mac_get_key_info(key, key_data);
  73. iv = mt76_rr(dev, MT_WCID_IV(idx));
  74. eiv = mt76_rr(dev, MT_WCID_IV(idx) + 4);
  75. pn = (u64)eiv << 16;
  76. if (cipher == MT76X02_CIPHER_TKIP) {
  77. pn |= (iv >> 16) & 0xff;
  78. pn |= (iv & 0xff) << 8;
  79. } else if (cipher >= MT76X02_CIPHER_AES_CCMP) {
  80. pn |= iv & 0xffff;
  81. } else {
  82. return;
  83. }
  84. atomic64_set(&key->tx_pn, pn);
  85. }
  86. int mt76x02_mac_wcid_set_key(struct mt76x02_dev *dev, u8 idx,
  87. struct ieee80211_key_conf *key)
  88. {
  89. enum mt76x02_cipher_type cipher;
  90. u8 key_data[32];
  91. u8 iv_data[8];
  92. u64 pn;
  93. cipher = mt76x02_mac_get_key_info(key, key_data);
  94. if (cipher == MT76X02_CIPHER_NONE && key)
  95. return -EOPNOTSUPP;
  96. mt76_wr_copy(dev, MT_WCID_KEY(idx), key_data, sizeof(key_data));
  97. mt76_rmw_field(dev, MT_WCID_ATTR(idx), MT_WCID_ATTR_PKEY_MODE, cipher);
  98. memset(iv_data, 0, sizeof(iv_data));
  99. if (key) {
  100. mt76_rmw_field(dev, MT_WCID_ATTR(idx), MT_WCID_ATTR_PAIRWISE,
  101. !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
  102. pn = atomic64_read(&key->tx_pn);
  103. iv_data[3] = key->keyidx << 6;
  104. if (cipher >= MT76X02_CIPHER_TKIP) {
  105. iv_data[3] |= 0x20;
  106. put_unaligned_le32(pn >> 16, &iv_data[4]);
  107. }
  108. if (cipher == MT76X02_CIPHER_TKIP) {
  109. iv_data[0] = (pn >> 8) & 0xff;
  110. iv_data[1] = (iv_data[0] | 0x20) & 0x7f;
  111. iv_data[2] = pn & 0xff;
  112. } else if (cipher >= MT76X02_CIPHER_AES_CCMP) {
  113. put_unaligned_le16((pn & 0xffff), &iv_data[0]);
  114. }
  115. }
  116. mt76_wr_copy(dev, MT_WCID_IV(idx), iv_data, sizeof(iv_data));
  117. return 0;
  118. }
  119. void mt76x02_mac_wcid_setup(struct mt76x02_dev *dev, u8 idx,
  120. u8 vif_idx, u8 *mac)
  121. {
  122. struct mt76_wcid_addr addr = {};
  123. u32 attr;
  124. attr = FIELD_PREP(MT_WCID_ATTR_BSS_IDX, vif_idx & 7) |
  125. FIELD_PREP(MT_WCID_ATTR_BSS_IDX_EXT, !!(vif_idx & 8));
  126. mt76_wr(dev, MT_WCID_ATTR(idx), attr);
  127. if (idx >= 128)
  128. return;
  129. if (mac)
  130. memcpy(addr.macaddr, mac, ETH_ALEN);
  131. mt76_wr_copy(dev, MT_WCID_ADDR(idx), &addr, sizeof(addr));
  132. }
  133. EXPORT_SYMBOL_GPL(mt76x02_mac_wcid_setup);
  134. void mt76x02_mac_wcid_set_drop(struct mt76x02_dev *dev, u8 idx, bool drop)
  135. {
  136. u32 val = mt76_rr(dev, MT_WCID_DROP(idx));
  137. u32 bit = MT_WCID_DROP_MASK(idx);
  138. /* prevent unnecessary writes */
  139. if ((val & bit) != (bit * drop))
  140. mt76_wr(dev, MT_WCID_DROP(idx), (val & ~bit) | (bit * drop));
  141. }
  142. static u16
  143. mt76x02_mac_tx_rate_val(struct mt76x02_dev *dev,
  144. const struct ieee80211_tx_rate *rate, u8 *nss_val)
  145. {
  146. u8 phy, rate_idx, nss, bw = 0;
  147. u16 rateval;
  148. if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
  149. rate_idx = rate->idx;
  150. nss = 1 + (rate->idx >> 4);
  151. phy = MT_PHY_TYPE_VHT;
  152. if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  153. bw = 2;
  154. else if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  155. bw = 1;
  156. } else if (rate->flags & IEEE80211_TX_RC_MCS) {
  157. rate_idx = rate->idx;
  158. nss = 1 + (rate->idx >> 3);
  159. phy = MT_PHY_TYPE_HT;
  160. if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  161. phy = MT_PHY_TYPE_HT_GF;
  162. if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  163. bw = 1;
  164. } else {
  165. const struct ieee80211_rate *r;
  166. int band = dev->mphy.chandef.chan->band;
  167. u16 val;
  168. r = &dev->mt76.hw->wiphy->bands[band]->bitrates[rate->idx];
  169. if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  170. val = r->hw_value_short;
  171. else
  172. val = r->hw_value;
  173. phy = val >> 8;
  174. rate_idx = val & 0xff;
  175. nss = 1;
  176. }
  177. rateval = FIELD_PREP(MT_RXWI_RATE_INDEX, rate_idx);
  178. rateval |= FIELD_PREP(MT_RXWI_RATE_PHY, phy);
  179. rateval |= FIELD_PREP(MT_RXWI_RATE_BW, bw);
  180. if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
  181. rateval |= MT_RXWI_RATE_SGI;
  182. *nss_val = nss;
  183. return rateval;
  184. }
  185. void mt76x02_mac_wcid_set_rate(struct mt76x02_dev *dev, struct mt76_wcid *wcid,
  186. const struct ieee80211_tx_rate *rate)
  187. {
  188. s8 max_txpwr_adj = mt76x02_tx_get_max_txpwr_adj(dev, rate);
  189. u16 rateval;
  190. u32 tx_info;
  191. s8 nss;
  192. rateval = mt76x02_mac_tx_rate_val(dev, rate, &nss);
  193. tx_info = FIELD_PREP(MT_WCID_TX_INFO_RATE, rateval) |
  194. FIELD_PREP(MT_WCID_TX_INFO_NSS, nss) |
  195. FIELD_PREP(MT_WCID_TX_INFO_TXPWR_ADJ, max_txpwr_adj) |
  196. MT_WCID_TX_INFO_SET;
  197. wcid->tx_info = tx_info;
  198. }
  199. void mt76x02_mac_set_short_preamble(struct mt76x02_dev *dev, bool enable)
  200. {
  201. if (enable)
  202. mt76_set(dev, MT_AUTO_RSP_CFG, MT_AUTO_RSP_PREAMB_SHORT);
  203. else
  204. mt76_clear(dev, MT_AUTO_RSP_CFG, MT_AUTO_RSP_PREAMB_SHORT);
  205. }
  206. bool mt76x02_mac_load_tx_status(struct mt76x02_dev *dev,
  207. struct mt76x02_tx_status *stat)
  208. {
  209. u32 stat1, stat2;
  210. stat2 = mt76_rr(dev, MT_TX_STAT_FIFO_EXT);
  211. stat1 = mt76_rr(dev, MT_TX_STAT_FIFO);
  212. stat->valid = !!(stat1 & MT_TX_STAT_FIFO_VALID);
  213. if (!stat->valid)
  214. return false;
  215. stat->success = !!(stat1 & MT_TX_STAT_FIFO_SUCCESS);
  216. stat->aggr = !!(stat1 & MT_TX_STAT_FIFO_AGGR);
  217. stat->ack_req = !!(stat1 & MT_TX_STAT_FIFO_ACKREQ);
  218. stat->wcid = FIELD_GET(MT_TX_STAT_FIFO_WCID, stat1);
  219. stat->rate = FIELD_GET(MT_TX_STAT_FIFO_RATE, stat1);
  220. stat->retry = FIELD_GET(MT_TX_STAT_FIFO_EXT_RETRY, stat2);
  221. stat->pktid = FIELD_GET(MT_TX_STAT_FIFO_EXT_PKTID, stat2);
  222. trace_mac_txstat_fetch(dev, stat);
  223. return true;
  224. }
  225. static int
  226. mt76x02_mac_process_tx_rate(struct ieee80211_tx_rate *txrate, u16 rate,
  227. enum nl80211_band band)
  228. {
  229. u8 idx = FIELD_GET(MT_RXWI_RATE_INDEX, rate);
  230. txrate->idx = 0;
  231. txrate->flags = 0;
  232. txrate->count = 1;
  233. switch (FIELD_GET(MT_RXWI_RATE_PHY, rate)) {
  234. case MT_PHY_TYPE_OFDM:
  235. if (band == NL80211_BAND_2GHZ)
  236. idx += 4;
  237. txrate->idx = idx;
  238. return 0;
  239. case MT_PHY_TYPE_CCK:
  240. if (idx >= 8)
  241. idx -= 8;
  242. txrate->idx = idx;
  243. return 0;
  244. case MT_PHY_TYPE_HT_GF:
  245. txrate->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  246. fallthrough;
  247. case MT_PHY_TYPE_HT:
  248. txrate->flags |= IEEE80211_TX_RC_MCS;
  249. txrate->idx = idx;
  250. break;
  251. case MT_PHY_TYPE_VHT:
  252. txrate->flags |= IEEE80211_TX_RC_VHT_MCS;
  253. txrate->idx = idx;
  254. break;
  255. default:
  256. return -EINVAL;
  257. }
  258. switch (FIELD_GET(MT_RXWI_RATE_BW, rate)) {
  259. case MT_PHY_BW_20:
  260. break;
  261. case MT_PHY_BW_40:
  262. txrate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  263. break;
  264. case MT_PHY_BW_80:
  265. txrate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
  266. break;
  267. default:
  268. return -EINVAL;
  269. }
  270. if (rate & MT_RXWI_RATE_SGI)
  271. txrate->flags |= IEEE80211_TX_RC_SHORT_GI;
  272. return 0;
  273. }
  274. void mt76x02_mac_write_txwi(struct mt76x02_dev *dev, struct mt76x02_txwi *txwi,
  275. struct sk_buff *skb, struct mt76_wcid *wcid,
  276. struct ieee80211_sta *sta, int len)
  277. {
  278. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  279. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  280. struct ieee80211_tx_rate *rate = &info->control.rates[0];
  281. struct ieee80211_key_conf *key = info->control.hw_key;
  282. u32 wcid_tx_info;
  283. u16 rate_ht_mask = FIELD_PREP(MT_RXWI_RATE_PHY, BIT(1) | BIT(2));
  284. u16 txwi_flags = 0, rateval;
  285. u8 nss;
  286. s8 txpwr_adj, max_txpwr_adj;
  287. u8 ccmp_pn[8], nstreams = dev->mphy.chainmask & 0xf;
  288. memset(txwi, 0, sizeof(*txwi));
  289. mt76_tx_check_agg_ssn(sta, skb);
  290. if (!info->control.hw_key && wcid && wcid->hw_key_idx != 0xff &&
  291. ieee80211_has_protected(hdr->frame_control)) {
  292. wcid = NULL;
  293. ieee80211_get_tx_rates(info->control.vif, sta, skb,
  294. info->control.rates, 1);
  295. }
  296. if (wcid)
  297. txwi->wcid = wcid->idx;
  298. else
  299. txwi->wcid = 0xff;
  300. if (wcid && wcid->sw_iv && key) {
  301. u64 pn = atomic64_inc_return(&key->tx_pn);
  302. ccmp_pn[0] = pn;
  303. ccmp_pn[1] = pn >> 8;
  304. ccmp_pn[2] = 0;
  305. ccmp_pn[3] = 0x20 | (key->keyidx << 6);
  306. ccmp_pn[4] = pn >> 16;
  307. ccmp_pn[5] = pn >> 24;
  308. ccmp_pn[6] = pn >> 32;
  309. ccmp_pn[7] = pn >> 40;
  310. txwi->iv = *((__le32 *)&ccmp_pn[0]);
  311. txwi->eiv = *((__le32 *)&ccmp_pn[4]);
  312. }
  313. if (wcid && (rate->idx < 0 || !rate->count)) {
  314. wcid_tx_info = wcid->tx_info;
  315. rateval = FIELD_GET(MT_WCID_TX_INFO_RATE, wcid_tx_info);
  316. max_txpwr_adj = FIELD_GET(MT_WCID_TX_INFO_TXPWR_ADJ,
  317. wcid_tx_info);
  318. nss = FIELD_GET(MT_WCID_TX_INFO_NSS, wcid_tx_info);
  319. } else {
  320. rateval = mt76x02_mac_tx_rate_val(dev, rate, &nss);
  321. max_txpwr_adj = mt76x02_tx_get_max_txpwr_adj(dev, rate);
  322. }
  323. txwi->rate = cpu_to_le16(rateval);
  324. txpwr_adj = mt76x02_tx_get_txpwr_adj(dev, dev->txpower_conf,
  325. max_txpwr_adj);
  326. txwi->ctl2 = FIELD_PREP(MT_TX_PWR_ADJ, txpwr_adj);
  327. if (nstreams > 1 && mt76_rev(&dev->mt76) >= MT76XX_REV_E4)
  328. txwi->txstream = 0x13;
  329. else if (nstreams > 1 && mt76_rev(&dev->mt76) >= MT76XX_REV_E3 &&
  330. !(txwi->rate & cpu_to_le16(rate_ht_mask)))
  331. txwi->txstream = 0x93;
  332. if (is_mt76x2(dev) && (info->flags & IEEE80211_TX_CTL_LDPC))
  333. txwi->rate |= cpu_to_le16(MT_RXWI_RATE_LDPC);
  334. if ((info->flags & IEEE80211_TX_CTL_STBC) && nss == 1)
  335. txwi->rate |= cpu_to_le16(MT_RXWI_RATE_STBC);
  336. if (nss > 1 && sta && sta->deflink.smps_mode == IEEE80211_SMPS_DYNAMIC)
  337. txwi_flags |= MT_TXWI_FLAGS_MMPS;
  338. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  339. txwi->ack_ctl |= MT_TXWI_ACK_CTL_REQ;
  340. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  341. txwi->ack_ctl |= MT_TXWI_ACK_CTL_NSEQ;
  342. if ((info->flags & IEEE80211_TX_CTL_AMPDU) && sta) {
  343. u8 ba_size = IEEE80211_MIN_AMPDU_BUF;
  344. u8 ampdu_density = sta->deflink.ht_cap.ampdu_density;
  345. ba_size <<= sta->deflink.ht_cap.ampdu_factor;
  346. ba_size = min_t(int, 63, ba_size - 1);
  347. if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
  348. ba_size = 0;
  349. txwi->ack_ctl |= FIELD_PREP(MT_TXWI_ACK_CTL_BA_WINDOW, ba_size);
  350. if (ampdu_density < IEEE80211_HT_MPDU_DENSITY_4)
  351. ampdu_density = IEEE80211_HT_MPDU_DENSITY_4;
  352. txwi_flags |= MT_TXWI_FLAGS_AMPDU |
  353. FIELD_PREP(MT_TXWI_FLAGS_MPDU_DENSITY, ampdu_density);
  354. }
  355. if (ieee80211_is_probe_resp(hdr->frame_control) ||
  356. ieee80211_is_beacon(hdr->frame_control))
  357. txwi_flags |= MT_TXWI_FLAGS_TS;
  358. txwi->flags |= cpu_to_le16(txwi_flags);
  359. txwi->len_ctl = cpu_to_le16(len);
  360. }
  361. EXPORT_SYMBOL_GPL(mt76x02_mac_write_txwi);
  362. static void
  363. mt76x02_tx_rate_fallback(struct ieee80211_tx_rate *rates, int idx, int phy)
  364. {
  365. u8 mcs, nss;
  366. if (!idx)
  367. return;
  368. rates += idx - 1;
  369. rates[1] = rates[0];
  370. switch (phy) {
  371. case MT_PHY_TYPE_VHT:
  372. mcs = ieee80211_rate_get_vht_mcs(rates);
  373. nss = ieee80211_rate_get_vht_nss(rates);
  374. if (mcs == 0)
  375. nss = max_t(int, nss - 1, 1);
  376. else
  377. mcs--;
  378. ieee80211_rate_set_vht(rates + 1, mcs, nss);
  379. break;
  380. case MT_PHY_TYPE_HT_GF:
  381. case MT_PHY_TYPE_HT:
  382. /* MCS 8 falls back to MCS 0 */
  383. if (rates[0].idx == 8) {
  384. rates[1].idx = 0;
  385. break;
  386. }
  387. fallthrough;
  388. default:
  389. rates[1].idx = max_t(int, rates[0].idx - 1, 0);
  390. break;
  391. }
  392. }
  393. static void
  394. mt76x02_mac_fill_tx_status(struct mt76x02_dev *dev, struct mt76x02_sta *msta,
  395. struct ieee80211_tx_info *info,
  396. struct mt76x02_tx_status *st, int n_frames)
  397. {
  398. struct ieee80211_tx_rate *rate = info->status.rates;
  399. struct ieee80211_tx_rate last_rate;
  400. u16 first_rate;
  401. int retry = st->retry;
  402. int phy;
  403. int i;
  404. if (!n_frames)
  405. return;
  406. phy = FIELD_GET(MT_RXWI_RATE_PHY, st->rate);
  407. if (st->pktid & MT_PACKET_ID_HAS_RATE) {
  408. first_rate = st->rate & ~MT_PKTID_RATE;
  409. first_rate |= st->pktid & MT_PKTID_RATE;
  410. mt76x02_mac_process_tx_rate(&rate[0], first_rate,
  411. dev->mphy.chandef.chan->band);
  412. } else if (rate[0].idx < 0) {
  413. if (!msta)
  414. return;
  415. mt76x02_mac_process_tx_rate(&rate[0], msta->wcid.tx_info,
  416. dev->mphy.chandef.chan->band);
  417. }
  418. mt76x02_mac_process_tx_rate(&last_rate, st->rate,
  419. dev->mphy.chandef.chan->band);
  420. for (i = 0; i < ARRAY_SIZE(info->status.rates); i++) {
  421. retry--;
  422. if (i + 1 == ARRAY_SIZE(info->status.rates)) {
  423. info->status.rates[i] = last_rate;
  424. info->status.rates[i].count = max_t(int, retry, 1);
  425. break;
  426. }
  427. mt76x02_tx_rate_fallback(info->status.rates, i, phy);
  428. if (info->status.rates[i].idx == last_rate.idx)
  429. break;
  430. }
  431. if (i + 1 < ARRAY_SIZE(info->status.rates)) {
  432. info->status.rates[i + 1].idx = -1;
  433. info->status.rates[i + 1].count = 0;
  434. }
  435. info->status.ampdu_len = n_frames;
  436. info->status.ampdu_ack_len = st->success ? n_frames : 0;
  437. if (st->aggr)
  438. info->flags |= IEEE80211_TX_CTL_AMPDU |
  439. IEEE80211_TX_STAT_AMPDU;
  440. if (!st->ack_req)
  441. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  442. else if (st->success)
  443. info->flags |= IEEE80211_TX_STAT_ACK;
  444. }
  445. void mt76x02_send_tx_status(struct mt76x02_dev *dev,
  446. struct mt76x02_tx_status *stat, u8 *update)
  447. {
  448. struct ieee80211_tx_info info = {};
  449. struct ieee80211_tx_status status = {
  450. .info = &info
  451. };
  452. static const u8 ac_to_tid[4] = {
  453. [IEEE80211_AC_BE] = 0,
  454. [IEEE80211_AC_BK] = 1,
  455. [IEEE80211_AC_VI] = 4,
  456. [IEEE80211_AC_VO] = 6
  457. };
  458. struct mt76_wcid *wcid = NULL;
  459. struct mt76x02_sta *msta = NULL;
  460. struct mt76_dev *mdev = &dev->mt76;
  461. struct sk_buff_head list;
  462. u32 duration = 0;
  463. u8 cur_pktid;
  464. u32 ac = 0;
  465. int len = 0;
  466. if (stat->pktid == MT_PACKET_ID_NO_ACK)
  467. return;
  468. rcu_read_lock();
  469. wcid = mt76_wcid_ptr(dev, stat->wcid);
  470. if (wcid && wcid->sta) {
  471. void *priv;
  472. priv = msta = container_of(wcid, struct mt76x02_sta, wcid);
  473. status.sta = container_of(priv, struct ieee80211_sta,
  474. drv_priv);
  475. }
  476. mt76_tx_status_lock(mdev, &list);
  477. if (wcid) {
  478. if (mt76_is_skb_pktid(stat->pktid))
  479. status.skb = mt76_tx_status_skb_get(mdev, wcid,
  480. stat->pktid, &list);
  481. if (status.skb)
  482. status.info = IEEE80211_SKB_CB(status.skb);
  483. }
  484. if (!status.skb && !(stat->pktid & MT_PACKET_ID_HAS_RATE)) {
  485. mt76_tx_status_unlock(mdev, &list);
  486. goto out;
  487. }
  488. if (msta && stat->aggr && !status.skb) {
  489. u32 stat_val, stat_cache;
  490. stat_val = stat->rate;
  491. stat_val |= ((u32)stat->retry) << 16;
  492. stat_cache = msta->status.rate;
  493. stat_cache |= ((u32)msta->status.retry) << 16;
  494. if (*update == 0 && stat_val == stat_cache &&
  495. stat->wcid == msta->status.wcid && msta->n_frames < 32) {
  496. msta->n_frames++;
  497. mt76_tx_status_unlock(mdev, &list);
  498. goto out;
  499. }
  500. cur_pktid = msta->status.pktid;
  501. mt76x02_mac_fill_tx_status(dev, msta, status.info,
  502. &msta->status, msta->n_frames);
  503. msta->status = *stat;
  504. msta->n_frames = 1;
  505. *update = 0;
  506. } else {
  507. cur_pktid = stat->pktid;
  508. mt76x02_mac_fill_tx_status(dev, msta, status.info, stat, 1);
  509. *update = 1;
  510. }
  511. if (status.skb) {
  512. info = *status.info;
  513. len = status.skb->len;
  514. ac = skb_get_queue_mapping(status.skb);
  515. mt76_tx_status_skb_done(mdev, status.skb, &list);
  516. } else if (msta) {
  517. len = status.info->status.ampdu_len * ewma_pktlen_read(&msta->pktlen);
  518. ac = FIELD_GET(MT_PKTID_AC, cur_pktid);
  519. }
  520. mt76_tx_status_unlock(mdev, &list);
  521. if (!status.skb) {
  522. spin_lock_bh(&dev->mt76.rx_lock);
  523. ieee80211_tx_status_ext(mt76_hw(dev), &status);
  524. spin_unlock_bh(&dev->mt76.rx_lock);
  525. }
  526. if (!len)
  527. goto out;
  528. duration = ieee80211_calc_tx_airtime(mt76_hw(dev), &info, len);
  529. spin_lock_bh(&dev->mt76.cc_lock);
  530. dev->tx_airtime += duration;
  531. spin_unlock_bh(&dev->mt76.cc_lock);
  532. if (msta)
  533. ieee80211_sta_register_airtime(status.sta, ac_to_tid[ac], duration, 0);
  534. out:
  535. rcu_read_unlock();
  536. }
  537. static int
  538. mt76x02_mac_process_rate(struct mt76x02_dev *dev,
  539. struct mt76_rx_status *status,
  540. u16 rate)
  541. {
  542. u8 idx = FIELD_GET(MT_RXWI_RATE_INDEX, rate);
  543. switch (FIELD_GET(MT_RXWI_RATE_PHY, rate)) {
  544. case MT_PHY_TYPE_OFDM:
  545. if (idx >= 8)
  546. idx = 0;
  547. if (status->band == NL80211_BAND_2GHZ)
  548. idx += 4;
  549. status->rate_idx = idx;
  550. return 0;
  551. case MT_PHY_TYPE_CCK:
  552. if (idx >= 8) {
  553. idx -= 8;
  554. status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
  555. }
  556. if (idx >= 4)
  557. idx = 0;
  558. status->rate_idx = idx;
  559. return 0;
  560. case MT_PHY_TYPE_HT_GF:
  561. status->enc_flags |= RX_ENC_FLAG_HT_GF;
  562. fallthrough;
  563. case MT_PHY_TYPE_HT:
  564. status->encoding = RX_ENC_HT;
  565. status->rate_idx = idx;
  566. break;
  567. case MT_PHY_TYPE_VHT: {
  568. u8 n_rxstream = dev->mphy.chainmask & 0xf;
  569. status->encoding = RX_ENC_VHT;
  570. status->rate_idx = FIELD_GET(MT_RATE_INDEX_VHT_IDX, idx);
  571. status->nss = min_t(u8, n_rxstream,
  572. FIELD_GET(MT_RATE_INDEX_VHT_NSS, idx) + 1);
  573. break;
  574. }
  575. default:
  576. return -EINVAL;
  577. }
  578. if (rate & MT_RXWI_RATE_LDPC)
  579. status->enc_flags |= RX_ENC_FLAG_LDPC;
  580. if (rate & MT_RXWI_RATE_SGI)
  581. status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
  582. if (rate & MT_RXWI_RATE_STBC)
  583. status->enc_flags |= 1 << RX_ENC_FLAG_STBC_SHIFT;
  584. switch (FIELD_GET(MT_RXWI_RATE_BW, rate)) {
  585. case MT_PHY_BW_20:
  586. break;
  587. case MT_PHY_BW_40:
  588. status->bw = RATE_INFO_BW_40;
  589. break;
  590. case MT_PHY_BW_80:
  591. status->bw = RATE_INFO_BW_80;
  592. break;
  593. default:
  594. break;
  595. }
  596. return 0;
  597. }
  598. void mt76x02_mac_setaddr(struct mt76x02_dev *dev, const u8 *addr)
  599. {
  600. static const u8 null_addr[ETH_ALEN] = {};
  601. int i;
  602. ether_addr_copy(dev->mphy.macaddr, addr);
  603. if (!is_valid_ether_addr(dev->mphy.macaddr)) {
  604. eth_random_addr(dev->mphy.macaddr);
  605. dev_info(dev->mt76.dev,
  606. "Invalid MAC address, using random address %pM\n",
  607. dev->mphy.macaddr);
  608. }
  609. mt76_wr(dev, MT_MAC_ADDR_DW0, get_unaligned_le32(dev->mphy.macaddr));
  610. mt76_wr(dev, MT_MAC_ADDR_DW1,
  611. get_unaligned_le16(dev->mphy.macaddr + 4) |
  612. FIELD_PREP(MT_MAC_ADDR_DW1_U2ME_MASK, 0xff));
  613. mt76_wr(dev, MT_MAC_BSSID_DW0,
  614. get_unaligned_le32(dev->mphy.macaddr));
  615. mt76_wr(dev, MT_MAC_BSSID_DW1,
  616. get_unaligned_le16(dev->mphy.macaddr + 4) |
  617. FIELD_PREP(MT_MAC_BSSID_DW1_MBSS_MODE, 3) | /* 8 APs + 8 STAs */
  618. MT_MAC_BSSID_DW1_MBSS_LOCAL_BIT);
  619. /* enable 7 additional beacon slots and control them with bypass mask */
  620. mt76_rmw_field(dev, MT_MAC_BSSID_DW1, MT_MAC_BSSID_DW1_MBEACON_N, 7);
  621. for (i = 0; i < 16; i++)
  622. mt76x02_mac_set_bssid(dev, i, null_addr);
  623. }
  624. EXPORT_SYMBOL_GPL(mt76x02_mac_setaddr);
  625. static int
  626. mt76x02_mac_get_rssi(struct mt76x02_dev *dev, s8 rssi, int chain)
  627. {
  628. struct mt76x02_rx_freq_cal *cal = &dev->cal.rx;
  629. rssi += cal->rssi_offset[chain];
  630. rssi -= cal->lna_gain;
  631. return rssi;
  632. }
  633. int mt76x02_mac_process_rx(struct mt76x02_dev *dev, struct sk_buff *skb,
  634. void *rxi)
  635. {
  636. struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
  637. struct ieee80211_hdr *hdr;
  638. struct mt76x02_rxwi *rxwi = rxi;
  639. struct mt76x02_sta *sta;
  640. u32 rxinfo = le32_to_cpu(rxwi->rxinfo);
  641. u32 ctl = le32_to_cpu(rxwi->ctl);
  642. u16 rate = le16_to_cpu(rxwi->rate);
  643. u16 tid_sn = le16_to_cpu(rxwi->tid_sn);
  644. bool unicast = rxwi->rxinfo & cpu_to_le32(MT_RXINFO_UNICAST);
  645. int pad_len = 0, nstreams = dev->mphy.chainmask & 0xf;
  646. s8 signal;
  647. u8 pn_len;
  648. u8 wcid;
  649. int len;
  650. if (!test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
  651. return -EINVAL;
  652. if (rxinfo & MT_RXINFO_L2PAD)
  653. pad_len += 2;
  654. if (rxinfo & MT_RXINFO_DECRYPT) {
  655. status->flag |= RX_FLAG_DECRYPTED;
  656. status->flag |= RX_FLAG_MMIC_STRIPPED;
  657. status->flag |= RX_FLAG_MIC_STRIPPED;
  658. status->flag |= RX_FLAG_IV_STRIPPED;
  659. }
  660. wcid = FIELD_GET(MT_RXWI_CTL_WCID, ctl);
  661. sta = mt76x02_rx_get_sta(&dev->mt76, wcid);
  662. status->wcid = mt76x02_rx_get_sta_wcid(sta, unicast);
  663. len = FIELD_GET(MT_RXWI_CTL_MPDU_LEN, ctl);
  664. pn_len = FIELD_GET(MT_RXINFO_PN_LEN, rxinfo);
  665. if (pn_len) {
  666. int offset = ieee80211_get_hdrlen_from_skb(skb) + pad_len;
  667. u8 *data = skb->data + offset;
  668. status->iv[0] = data[7];
  669. status->iv[1] = data[6];
  670. status->iv[2] = data[5];
  671. status->iv[3] = data[4];
  672. status->iv[4] = data[1];
  673. status->iv[5] = data[0];
  674. /*
  675. * Driver CCMP validation can't deal with fragments.
  676. * Let mac80211 take care of it.
  677. */
  678. if (rxinfo & MT_RXINFO_FRAG) {
  679. status->flag &= ~RX_FLAG_IV_STRIPPED;
  680. } else {
  681. pad_len += pn_len << 2;
  682. len -= pn_len << 2;
  683. }
  684. }
  685. mt76x02_remove_hdr_pad(skb, pad_len);
  686. if ((rxinfo & MT_RXINFO_BA) && !(rxinfo & MT_RXINFO_NULL))
  687. status->aggr = true;
  688. if (rxinfo & MT_RXINFO_AMPDU) {
  689. status->flag |= RX_FLAG_AMPDU_DETAILS;
  690. status->ampdu_ref = dev->ampdu_ref;
  691. /*
  692. * When receiving an A-MPDU subframe and RSSI info is not valid,
  693. * we can assume that more subframes belonging to the same A-MPDU
  694. * are coming. The last one will have valid RSSI info
  695. */
  696. if (rxinfo & MT_RXINFO_RSSI) {
  697. if (!++dev->ampdu_ref)
  698. dev->ampdu_ref++;
  699. }
  700. }
  701. if (WARN_ON_ONCE(len > skb->len))
  702. return -EINVAL;
  703. if (pskb_trim(skb, len))
  704. return -EINVAL;
  705. status->chains = BIT(0);
  706. signal = mt76x02_mac_get_rssi(dev, rxwi->rssi[0], 0);
  707. status->chain_signal[0] = signal;
  708. if (nstreams > 1) {
  709. status->chains |= BIT(1);
  710. status->chain_signal[1] = mt76x02_mac_get_rssi(dev,
  711. rxwi->rssi[1],
  712. 1);
  713. }
  714. status->freq = dev->mphy.chandef.chan->center_freq;
  715. status->band = dev->mphy.chandef.chan->band;
  716. hdr = (struct ieee80211_hdr *)skb->data;
  717. status->qos_ctl = *ieee80211_get_qos_ctl(hdr);
  718. status->seqno = FIELD_GET(MT_RXWI_SN, tid_sn);
  719. return mt76x02_mac_process_rate(dev, status, rate);
  720. }
  721. void mt76x02_mac_poll_tx_status(struct mt76x02_dev *dev, bool irq)
  722. {
  723. struct mt76x02_tx_status stat = {};
  724. u8 update = 1;
  725. bool ret;
  726. if (!test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
  727. return;
  728. trace_mac_txstat_poll(dev);
  729. while (!irq || !kfifo_is_full(&dev->txstatus_fifo)) {
  730. if (!spin_trylock(&dev->txstatus_fifo_lock))
  731. break;
  732. ret = mt76x02_mac_load_tx_status(dev, &stat);
  733. spin_unlock(&dev->txstatus_fifo_lock);
  734. if (!ret)
  735. break;
  736. if (!irq) {
  737. mt76x02_send_tx_status(dev, &stat, &update);
  738. continue;
  739. }
  740. kfifo_put(&dev->txstatus_fifo, stat);
  741. }
  742. }
  743. void mt76x02_tx_complete_skb(struct mt76_dev *mdev, struct mt76_queue_entry *e)
  744. {
  745. struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
  746. struct mt76x02_txwi *txwi;
  747. u8 *txwi_ptr;
  748. if (!e->txwi) {
  749. dev_kfree_skb_any(e->skb);
  750. return;
  751. }
  752. mt76x02_mac_poll_tx_status(dev, false);
  753. txwi_ptr = mt76_get_txwi_ptr(mdev, e->txwi);
  754. txwi = (struct mt76x02_txwi *)txwi_ptr;
  755. trace_mac_txdone(mdev, txwi->wcid, txwi->pktid);
  756. mt76_tx_complete_skb(mdev, e->wcid, e->skb);
  757. }
  758. EXPORT_SYMBOL_GPL(mt76x02_tx_complete_skb);
  759. void mt76x02_mac_set_rts_thresh(struct mt76x02_dev *dev, u32 val)
  760. {
  761. u32 data = 0;
  762. if (val != ~0)
  763. data = FIELD_PREP(MT_PROT_CFG_CTRL, 1) |
  764. MT_PROT_CFG_RTS_THRESH;
  765. mt76_rmw_field(dev, MT_TX_RTS_CFG, MT_TX_RTS_CFG_THRESH, val);
  766. mt76_rmw(dev, MT_CCK_PROT_CFG,
  767. MT_PROT_CFG_CTRL | MT_PROT_CFG_RTS_THRESH, data);
  768. mt76_rmw(dev, MT_OFDM_PROT_CFG,
  769. MT_PROT_CFG_CTRL | MT_PROT_CFG_RTS_THRESH, data);
  770. }
  771. void mt76x02_mac_set_tx_protection(struct mt76x02_dev *dev, bool legacy_prot,
  772. int ht_mode)
  773. {
  774. int mode = ht_mode & IEEE80211_HT_OP_MODE_PROTECTION;
  775. bool non_gf = !!(ht_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
  776. u32 prot[6];
  777. u32 vht_prot[3];
  778. int i;
  779. u16 rts_thr;
  780. for (i = 0; i < ARRAY_SIZE(prot); i++) {
  781. prot[i] = mt76_rr(dev, MT_CCK_PROT_CFG + i * 4);
  782. prot[i] &= ~MT_PROT_CFG_CTRL;
  783. if (i >= 2)
  784. prot[i] &= ~MT_PROT_CFG_RATE;
  785. }
  786. for (i = 0; i < ARRAY_SIZE(vht_prot); i++) {
  787. vht_prot[i] = mt76_rr(dev, MT_TX_PROT_CFG6 + i * 4);
  788. vht_prot[i] &= ~(MT_PROT_CFG_CTRL | MT_PROT_CFG_RATE);
  789. }
  790. rts_thr = mt76_get_field(dev, MT_TX_RTS_CFG, MT_TX_RTS_CFG_THRESH);
  791. if (rts_thr != 0xffff)
  792. prot[0] |= MT_PROT_CTRL_RTS_CTS;
  793. if (legacy_prot) {
  794. prot[1] |= MT_PROT_CTRL_CTS2SELF;
  795. prot[2] |= MT_PROT_RATE_CCK_11;
  796. prot[3] |= MT_PROT_RATE_CCK_11;
  797. prot[4] |= MT_PROT_RATE_CCK_11;
  798. prot[5] |= MT_PROT_RATE_CCK_11;
  799. vht_prot[0] |= MT_PROT_RATE_CCK_11;
  800. vht_prot[1] |= MT_PROT_RATE_CCK_11;
  801. vht_prot[2] |= MT_PROT_RATE_CCK_11;
  802. } else {
  803. if (rts_thr != 0xffff)
  804. prot[1] |= MT_PROT_CTRL_RTS_CTS;
  805. prot[2] |= MT_PROT_RATE_OFDM_24;
  806. prot[3] |= MT_PROT_RATE_DUP_OFDM_24;
  807. prot[4] |= MT_PROT_RATE_OFDM_24;
  808. prot[5] |= MT_PROT_RATE_DUP_OFDM_24;
  809. vht_prot[0] |= MT_PROT_RATE_OFDM_24;
  810. vht_prot[1] |= MT_PROT_RATE_DUP_OFDM_24;
  811. vht_prot[2] |= MT_PROT_RATE_SGI_OFDM_24;
  812. }
  813. switch (mode) {
  814. case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
  815. case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
  816. prot[2] |= MT_PROT_CTRL_RTS_CTS;
  817. prot[3] |= MT_PROT_CTRL_RTS_CTS;
  818. prot[4] |= MT_PROT_CTRL_RTS_CTS;
  819. prot[5] |= MT_PROT_CTRL_RTS_CTS;
  820. vht_prot[0] |= MT_PROT_CTRL_RTS_CTS;
  821. vht_prot[1] |= MT_PROT_CTRL_RTS_CTS;
  822. vht_prot[2] |= MT_PROT_CTRL_RTS_CTS;
  823. break;
  824. case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
  825. prot[3] |= MT_PROT_CTRL_RTS_CTS;
  826. prot[5] |= MT_PROT_CTRL_RTS_CTS;
  827. vht_prot[1] |= MT_PROT_CTRL_RTS_CTS;
  828. vht_prot[2] |= MT_PROT_CTRL_RTS_CTS;
  829. break;
  830. }
  831. if (non_gf) {
  832. prot[4] |= MT_PROT_CTRL_RTS_CTS;
  833. prot[5] |= MT_PROT_CTRL_RTS_CTS;
  834. }
  835. for (i = 0; i < ARRAY_SIZE(prot); i++)
  836. mt76_wr(dev, MT_CCK_PROT_CFG + i * 4, prot[i]);
  837. for (i = 0; i < ARRAY_SIZE(vht_prot); i++)
  838. mt76_wr(dev, MT_TX_PROT_CFG6 + i * 4, vht_prot[i]);
  839. }
  840. void mt76x02_update_channel(struct mt76_phy *mphy)
  841. {
  842. struct mt76x02_dev *dev = container_of(mphy->dev, struct mt76x02_dev, mt76);
  843. struct mt76_channel_state *state;
  844. state = mphy->chan_state;
  845. state->cc_busy += mt76_rr(dev, MT_CH_BUSY);
  846. spin_lock_bh(&dev->mt76.cc_lock);
  847. state->cc_tx += dev->tx_airtime;
  848. dev->tx_airtime = 0;
  849. spin_unlock_bh(&dev->mt76.cc_lock);
  850. }
  851. EXPORT_SYMBOL_GPL(mt76x02_update_channel);
  852. static void mt76x02_check_mac_err(struct mt76x02_dev *dev)
  853. {
  854. if (dev->mt76.beacon_mask) {
  855. if (mt76_rr(dev, MT_TX_STA_0) & MT_TX_STA_0_BEACONS) {
  856. dev->beacon_hang_check = 0;
  857. return;
  858. }
  859. if (dev->beacon_hang_check < 10)
  860. return;
  861. } else {
  862. u32 val = mt76_rr(dev, 0x10f4);
  863. if (!(val & BIT(29)) || !(val & (BIT(7) | BIT(5))))
  864. return;
  865. }
  866. dev_err(dev->mt76.dev, "MAC error detected\n");
  867. mt76_wr(dev, MT_MAC_SYS_CTRL, 0);
  868. if (!mt76x02_wait_for_txrx_idle(&dev->mt76)) {
  869. dev_err(dev->mt76.dev, "MAC stop failed\n");
  870. goto out;
  871. }
  872. dev->beacon_hang_check = 0;
  873. mt76_set(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_RESET_CSR);
  874. udelay(10);
  875. out:
  876. mt76_wr(dev, MT_MAC_SYS_CTRL,
  877. MT_MAC_SYS_CTRL_ENABLE_TX | MT_MAC_SYS_CTRL_ENABLE_RX);
  878. }
  879. static void
  880. mt76x02_edcca_tx_enable(struct mt76x02_dev *dev, bool enable)
  881. {
  882. if (enable) {
  883. u32 data;
  884. mt76_set(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_ENABLE_TX);
  885. mt76_set(dev, MT_AUTO_RSP_CFG, MT_AUTO_RSP_EN);
  886. /* enable pa-lna */
  887. data = mt76_rr(dev, MT_TX_PIN_CFG);
  888. data |= MT_TX_PIN_CFG_TXANT |
  889. MT_TX_PIN_CFG_RXANT |
  890. MT_TX_PIN_RFTR_EN |
  891. MT_TX_PIN_TRSW_EN;
  892. mt76_wr(dev, MT_TX_PIN_CFG, data);
  893. } else {
  894. mt76_clear(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_ENABLE_TX);
  895. mt76_clear(dev, MT_AUTO_RSP_CFG, MT_AUTO_RSP_EN);
  896. /* disable pa-lna */
  897. mt76_clear(dev, MT_TX_PIN_CFG, MT_TX_PIN_CFG_TXANT);
  898. mt76_clear(dev, MT_TX_PIN_CFG, MT_TX_PIN_CFG_RXANT);
  899. }
  900. dev->ed_tx_blocked = !enable;
  901. }
  902. void mt76x02_edcca_init(struct mt76x02_dev *dev)
  903. {
  904. dev->ed_trigger = 0;
  905. dev->ed_silent = 0;
  906. if (dev->ed_monitor) {
  907. struct ieee80211_channel *chan = dev->mphy.chandef.chan;
  908. u8 ed_th = chan->band == NL80211_BAND_5GHZ ? 0x0e : 0x20;
  909. mt76_clear(dev, MT_TX_LINK_CFG, MT_TX_CFACK_EN);
  910. mt76_set(dev, MT_TXOP_CTRL_CFG, MT_TXOP_ED_CCA_EN);
  911. mt76_rmw(dev, MT_BBP(AGC, 2), GENMASK(15, 0),
  912. ed_th << 8 | ed_th);
  913. mt76_set(dev, MT_TXOP_HLDR_ET, MT_TXOP_HLDR_TX40M_BLK_EN);
  914. } else {
  915. mt76_set(dev, MT_TX_LINK_CFG, MT_TX_CFACK_EN);
  916. mt76_clear(dev, MT_TXOP_CTRL_CFG, MT_TXOP_ED_CCA_EN);
  917. if (is_mt76x2(dev)) {
  918. mt76_wr(dev, MT_BBP(AGC, 2), 0x00007070);
  919. mt76_set(dev, MT_TXOP_HLDR_ET,
  920. MT_TXOP_HLDR_TX40M_BLK_EN);
  921. } else {
  922. mt76_wr(dev, MT_BBP(AGC, 2), 0x003a6464);
  923. mt76_clear(dev, MT_TXOP_HLDR_ET,
  924. MT_TXOP_HLDR_TX40M_BLK_EN);
  925. }
  926. }
  927. mt76x02_edcca_tx_enable(dev, true);
  928. dev->ed_monitor_learning = true;
  929. /* clear previous CCA timer value */
  930. mt76_rr(dev, MT_ED_CCA_TIMER);
  931. dev->ed_time = ktime_get_boottime();
  932. }
  933. EXPORT_SYMBOL_GPL(mt76x02_edcca_init);
  934. #define MT_EDCCA_TH 92
  935. #define MT_EDCCA_BLOCK_TH 2
  936. #define MT_EDCCA_LEARN_TH 50
  937. #define MT_EDCCA_LEARN_CCA 180
  938. #define MT_EDCCA_LEARN_TIMEOUT (20 * HZ)
  939. static void mt76x02_edcca_check(struct mt76x02_dev *dev)
  940. {
  941. ktime_t cur_time;
  942. u32 active, val, busy;
  943. cur_time = ktime_get_boottime();
  944. val = mt76_rr(dev, MT_ED_CCA_TIMER);
  945. active = ktime_to_us(ktime_sub(cur_time, dev->ed_time));
  946. dev->ed_time = cur_time;
  947. busy = (val * 100) / active;
  948. busy = min_t(u32, busy, 100);
  949. if (busy > MT_EDCCA_TH) {
  950. dev->ed_trigger++;
  951. dev->ed_silent = 0;
  952. } else {
  953. dev->ed_silent++;
  954. dev->ed_trigger = 0;
  955. }
  956. if (dev->cal.agc_lowest_gain &&
  957. dev->cal.false_cca > MT_EDCCA_LEARN_CCA &&
  958. dev->ed_trigger > MT_EDCCA_LEARN_TH) {
  959. dev->ed_monitor_learning = false;
  960. dev->ed_trigger_timeout = jiffies + 20 * HZ;
  961. } else if (!dev->ed_monitor_learning &&
  962. time_is_after_jiffies(dev->ed_trigger_timeout)) {
  963. dev->ed_monitor_learning = true;
  964. mt76x02_edcca_tx_enable(dev, true);
  965. }
  966. if (dev->ed_monitor_learning)
  967. return;
  968. if (dev->ed_trigger > MT_EDCCA_BLOCK_TH && !dev->ed_tx_blocked)
  969. mt76x02_edcca_tx_enable(dev, false);
  970. else if (dev->ed_silent > MT_EDCCA_BLOCK_TH && dev->ed_tx_blocked)
  971. mt76x02_edcca_tx_enable(dev, true);
  972. }
  973. void mt76x02_mac_work(struct work_struct *work)
  974. {
  975. struct mt76x02_dev *dev = container_of(work, struct mt76x02_dev,
  976. mphy.mac_work.work);
  977. int i, idx;
  978. mutex_lock(&dev->mt76.mutex);
  979. mt76_update_survey(&dev->mphy);
  980. for (i = 0, idx = 0; i < 16; i++) {
  981. u32 val = mt76_rr(dev, MT_TX_AGG_CNT(i));
  982. dev->mphy.aggr_stats[idx++] += val & 0xffff;
  983. dev->mphy.aggr_stats[idx++] += val >> 16;
  984. }
  985. mt76x02_check_mac_err(dev);
  986. if (dev->ed_monitor)
  987. mt76x02_edcca_check(dev);
  988. mutex_unlock(&dev->mt76.mutex);
  989. mt76_tx_status_check(&dev->mt76, false);
  990. ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mphy.mac_work,
  991. MT_MAC_WORK_INTERVAL);
  992. }
  993. void mt76x02_mac_cc_reset(struct mt76x02_dev *dev)
  994. {
  995. dev->mphy.survey_time = ktime_get_boottime();
  996. mt76_wr(dev, MT_CH_TIME_CFG,
  997. MT_CH_TIME_CFG_TIMER_EN |
  998. MT_CH_TIME_CFG_TX_AS_BUSY |
  999. MT_CH_TIME_CFG_RX_AS_BUSY |
  1000. MT_CH_TIME_CFG_NAV_AS_BUSY |
  1001. MT_CH_TIME_CFG_EIFS_AS_BUSY |
  1002. MT_CH_CCA_RC_EN |
  1003. FIELD_PREP(MT_CH_TIME_CFG_CH_TIMER_CLR, 1));
  1004. /* channel cycle counters read-and-clear */
  1005. mt76_rr(dev, MT_CH_BUSY);
  1006. mt76_rr(dev, MT_CH_IDLE);
  1007. }
  1008. EXPORT_SYMBOL_GPL(mt76x02_mac_cc_reset);
  1009. void mt76x02_mac_set_bssid(struct mt76x02_dev *dev, u8 idx, const u8 *addr)
  1010. {
  1011. idx &= 7;
  1012. mt76_wr(dev, MT_MAC_APC_BSSID_L(idx), get_unaligned_le32(addr));
  1013. mt76_rmw_field(dev, MT_MAC_APC_BSSID_H(idx), MT_MAC_APC_BSSID_H_ADDR,
  1014. get_unaligned_le16(addr + 4));
  1015. }