mt76x02_util.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. // SPDX-License-Identifier: ISC
  2. /*
  3. * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
  4. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  5. */
  6. #include <linux/module.h>
  7. #include "mt76x02.h"
  8. #define MT76x02_CCK_RATE(_idx, _rate) { \
  9. .bitrate = _rate, \
  10. .flags = IEEE80211_RATE_SHORT_PREAMBLE, \
  11. .hw_value = (MT_PHY_TYPE_CCK << 8) | (_idx), \
  12. .hw_value_short = (MT_PHY_TYPE_CCK << 8) | (8 + (_idx)), \
  13. }
  14. struct ieee80211_rate mt76x02_rates[] = {
  15. MT76x02_CCK_RATE(0, 10),
  16. MT76x02_CCK_RATE(1, 20),
  17. MT76x02_CCK_RATE(2, 55),
  18. MT76x02_CCK_RATE(3, 110),
  19. OFDM_RATE(0, 60),
  20. OFDM_RATE(1, 90),
  21. OFDM_RATE(2, 120),
  22. OFDM_RATE(3, 180),
  23. OFDM_RATE(4, 240),
  24. OFDM_RATE(5, 360),
  25. OFDM_RATE(6, 480),
  26. OFDM_RATE(7, 540),
  27. };
  28. EXPORT_SYMBOL_GPL(mt76x02_rates);
  29. static const struct ieee80211_iface_limit mt76x02_if_limits[] = {
  30. {
  31. .max = 1,
  32. .types = BIT(NL80211_IFTYPE_ADHOC)
  33. }, {
  34. .max = 8,
  35. .types = BIT(NL80211_IFTYPE_STATION) |
  36. #ifdef CONFIG_MAC80211_MESH
  37. BIT(NL80211_IFTYPE_MESH_POINT) |
  38. #endif
  39. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  40. BIT(NL80211_IFTYPE_P2P_GO) |
  41. BIT(NL80211_IFTYPE_AP)
  42. },
  43. };
  44. static const struct ieee80211_iface_limit mt76x02u_if_limits[] = {
  45. {
  46. .max = 1,
  47. .types = BIT(NL80211_IFTYPE_ADHOC)
  48. }, {
  49. .max = 2,
  50. .types = BIT(NL80211_IFTYPE_STATION) |
  51. #ifdef CONFIG_MAC80211_MESH
  52. BIT(NL80211_IFTYPE_MESH_POINT) |
  53. #endif
  54. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  55. BIT(NL80211_IFTYPE_P2P_GO) |
  56. BIT(NL80211_IFTYPE_AP)
  57. },
  58. };
  59. static const struct ieee80211_iface_combination mt76x02_if_comb[] = {
  60. {
  61. .limits = mt76x02_if_limits,
  62. .n_limits = ARRAY_SIZE(mt76x02_if_limits),
  63. .max_interfaces = 8,
  64. .num_different_channels = 1,
  65. .beacon_int_infra_match = true,
  66. .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
  67. BIT(NL80211_CHAN_WIDTH_20) |
  68. BIT(NL80211_CHAN_WIDTH_40) |
  69. BIT(NL80211_CHAN_WIDTH_80),
  70. }
  71. };
  72. static const struct ieee80211_iface_combination mt76x02u_if_comb[] = {
  73. {
  74. .limits = mt76x02u_if_limits,
  75. .n_limits = ARRAY_SIZE(mt76x02u_if_limits),
  76. .max_interfaces = 2,
  77. .num_different_channels = 1,
  78. .beacon_int_infra_match = true,
  79. }
  80. };
  81. static void
  82. mt76x02_led_set_config(struct mt76_phy *mphy, u8 delay_on, u8 delay_off)
  83. {
  84. struct mt76x02_dev *dev = container_of(mphy->dev, struct mt76x02_dev,
  85. mt76);
  86. u32 val;
  87. val = FIELD_PREP(MT_LED_STATUS_DURATION, 0xff) |
  88. FIELD_PREP(MT_LED_STATUS_OFF, delay_off) |
  89. FIELD_PREP(MT_LED_STATUS_ON, delay_on);
  90. mt76_wr(dev, MT_LED_S0(mphy->leds.pin), val);
  91. mt76_wr(dev, MT_LED_S1(mphy->leds.pin), val);
  92. val = MT_LED_CTRL_REPLAY(mphy->leds.pin) |
  93. MT_LED_CTRL_KICK(mphy->leds.pin);
  94. if (mphy->leds.al)
  95. val |= MT_LED_CTRL_POLARITY(mphy->leds.pin);
  96. mt76_wr(dev, MT_LED_CTRL, val);
  97. }
  98. static int
  99. mt76x02_led_set_blink(struct led_classdev *led_cdev,
  100. unsigned long *delay_on,
  101. unsigned long *delay_off)
  102. {
  103. struct mt76_phy *mphy = container_of(led_cdev, struct mt76_phy,
  104. leds.cdev);
  105. u8 delta_on, delta_off;
  106. delta_off = max_t(u8, *delay_off / 10, 1);
  107. delta_on = max_t(u8, *delay_on / 10, 1);
  108. mt76x02_led_set_config(mphy, delta_on, delta_off);
  109. return 0;
  110. }
  111. static void
  112. mt76x02_led_set_brightness(struct led_classdev *led_cdev,
  113. enum led_brightness brightness)
  114. {
  115. struct mt76_phy *mphy = container_of(led_cdev, struct mt76_phy,
  116. leds.cdev);
  117. if (!brightness)
  118. mt76x02_led_set_config(mphy, 0, 0xff);
  119. else
  120. mt76x02_led_set_config(mphy, 0xff, 0);
  121. }
  122. int mt76x02_init_device(struct mt76x02_dev *dev)
  123. {
  124. struct ieee80211_hw *hw = mt76_hw(dev);
  125. struct wiphy *wiphy = hw->wiphy;
  126. INIT_DELAYED_WORK(&dev->mphy.mac_work, mt76x02_mac_work);
  127. hw->queues = 4;
  128. hw->max_rates = 1;
  129. hw->max_report_rates = 7;
  130. hw->max_rate_tries = 1;
  131. hw->extra_tx_headroom = 2;
  132. if (mt76_is_usb(&dev->mt76)) {
  133. hw->extra_tx_headroom += sizeof(struct mt76x02_txwi) +
  134. MT_DMA_HDR_LEN;
  135. wiphy->iface_combinations = mt76x02u_if_comb;
  136. wiphy->n_iface_combinations = ARRAY_SIZE(mt76x02u_if_comb);
  137. } else {
  138. INIT_DELAYED_WORK(&dev->wdt_work, mt76x02_wdt_work);
  139. mt76x02_dfs_init_detector(dev);
  140. wiphy->reg_notifier = mt76x02_regd_notifier;
  141. wiphy->iface_combinations = mt76x02_if_comb;
  142. wiphy->n_iface_combinations = ARRAY_SIZE(mt76x02_if_comb);
  143. /* init led callbacks */
  144. if (IS_ENABLED(CONFIG_MT76_LEDS)) {
  145. dev->mphy.leds.cdev.brightness_set =
  146. mt76x02_led_set_brightness;
  147. dev->mphy.leds.cdev.blink_set = mt76x02_led_set_blink;
  148. }
  149. }
  150. wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
  151. hw->sta_data_size = sizeof(struct mt76x02_sta);
  152. hw->vif_data_size = sizeof(struct mt76x02_vif);
  153. ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
  154. ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
  155. ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
  156. dev->mt76.global_wcid.idx = 255;
  157. dev->mt76.global_wcid.hw_key_idx = -1;
  158. dev->slottime = 9;
  159. if (is_mt76x2(dev)) {
  160. dev->mphy.sband_2g.sband.ht_cap.cap |=
  161. IEEE80211_HT_CAP_LDPC_CODING;
  162. dev->mphy.sband_5g.sband.ht_cap.cap |=
  163. IEEE80211_HT_CAP_LDPC_CODING;
  164. dev->mphy.chainmask = 0x202;
  165. dev->mphy.antenna_mask = 3;
  166. } else {
  167. dev->mphy.chainmask = 0x101;
  168. dev->mphy.antenna_mask = 1;
  169. }
  170. return 0;
  171. }
  172. EXPORT_SYMBOL_GPL(mt76x02_init_device);
  173. void mt76x02_configure_filter(struct ieee80211_hw *hw,
  174. unsigned int changed_flags,
  175. unsigned int *total_flags, u64 multicast)
  176. {
  177. struct mt76x02_dev *dev = hw->priv;
  178. u32 flags = 0;
  179. #define MT76_FILTER(_flag, _hw) do { \
  180. flags |= *total_flags & FIF_##_flag; \
  181. dev->mt76.rxfilter &= ~(_hw); \
  182. dev->mt76.rxfilter |= !(flags & FIF_##_flag) * (_hw); \
  183. } while (0)
  184. mutex_lock(&dev->mt76.mutex);
  185. dev->mt76.rxfilter &= ~MT_RX_FILTR_CFG_OTHER_BSS;
  186. MT76_FILTER(FCSFAIL, MT_RX_FILTR_CFG_CRC_ERR);
  187. MT76_FILTER(PLCPFAIL, MT_RX_FILTR_CFG_PHY_ERR);
  188. MT76_FILTER(CONTROL, MT_RX_FILTR_CFG_ACK |
  189. MT_RX_FILTR_CFG_CTS |
  190. MT_RX_FILTR_CFG_CFEND |
  191. MT_RX_FILTR_CFG_CFACK |
  192. MT_RX_FILTR_CFG_BA |
  193. MT_RX_FILTR_CFG_CTRL_RSV);
  194. MT76_FILTER(PSPOLL, MT_RX_FILTR_CFG_PSPOLL);
  195. *total_flags = flags;
  196. mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
  197. mutex_unlock(&dev->mt76.mutex);
  198. }
  199. EXPORT_SYMBOL_GPL(mt76x02_configure_filter);
  200. int mt76x02_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
  201. struct ieee80211_sta *sta)
  202. {
  203. struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
  204. struct mt76x02_sta *msta = (struct mt76x02_sta *)sta->drv_priv;
  205. struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
  206. int idx = 0;
  207. memset(msta, 0, sizeof(*msta));
  208. idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT76x02_N_WCIDS);
  209. if (idx < 0)
  210. return -ENOSPC;
  211. msta->vif = mvif;
  212. msta->wcid.sta = 1;
  213. msta->wcid.idx = idx;
  214. msta->wcid.hw_key_idx = -1;
  215. mt76x02_mac_wcid_setup(dev, idx, mvif->idx, sta->addr);
  216. mt76x02_mac_wcid_set_drop(dev, idx, false);
  217. ewma_pktlen_init(&msta->pktlen);
  218. if (vif->type == NL80211_IFTYPE_AP)
  219. set_bit(MT_WCID_FLAG_CHECK_PS, &msta->wcid.flags);
  220. return 0;
  221. }
  222. EXPORT_SYMBOL_GPL(mt76x02_sta_add);
  223. void mt76x02_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
  224. struct ieee80211_sta *sta)
  225. {
  226. struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
  227. struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
  228. int idx = wcid->idx;
  229. mt76x02_mac_wcid_set_drop(dev, idx, true);
  230. mt76x02_mac_wcid_setup(dev, idx, 0, NULL);
  231. }
  232. EXPORT_SYMBOL_GPL(mt76x02_sta_remove);
  233. static void
  234. mt76x02_vif_init(struct mt76x02_dev *dev, struct ieee80211_vif *vif,
  235. unsigned int idx)
  236. {
  237. struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
  238. struct mt76_txq *mtxq;
  239. memset(mvif, 0, sizeof(*mvif));
  240. mvif->idx = idx;
  241. mvif->group_wcid.idx = MT_VIF_WCID(idx);
  242. mvif->group_wcid.hw_key_idx = -1;
  243. mt76_wcid_init(&mvif->group_wcid);
  244. mtxq = (struct mt76_txq *)vif->txq->drv_priv;
  245. rcu_assign_pointer(dev->mt76.wcid[MT_VIF_WCID(idx)], &mvif->group_wcid);
  246. mtxq->wcid = MT_VIF_WCID(idx);
  247. }
  248. int
  249. mt76x02_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  250. {
  251. struct mt76x02_dev *dev = hw->priv;
  252. unsigned int idx = 0;
  253. /* Allow to change address in HW if we create first interface. */
  254. if (!dev->mt76.vif_mask &&
  255. (((vif->addr[0] ^ dev->mphy.macaddr[0]) & ~GENMASK(4, 1)) ||
  256. memcmp(vif->addr + 1, dev->mphy.macaddr + 1, ETH_ALEN - 1)))
  257. mt76x02_mac_setaddr(dev, vif->addr);
  258. if (vif->addr[0] & BIT(1))
  259. idx = 1 + (((dev->mphy.macaddr[0] ^ vif->addr[0]) >> 2) & 7);
  260. /*
  261. * Client mode typically only has one configurable BSSID register,
  262. * which is used for bssidx=0. This is linked to the MAC address.
  263. * Since mac80211 allows changing interface types, and we cannot
  264. * force the use of the primary MAC address for a station mode
  265. * interface, we need some other way of configuring a per-interface
  266. * remote BSSID.
  267. * The hardware provides an AP-Client feature, where bssidx 0-7 are
  268. * used for AP mode and bssidx 8-15 for client mode.
  269. * We shift the station interface bss index by 8 to force the
  270. * hardware to recognize the BSSID.
  271. * The resulting bssidx mismatch for unicast frames is ignored by hw.
  272. */
  273. if (vif->type == NL80211_IFTYPE_STATION)
  274. idx += 8;
  275. /* vif is already set or idx is 8 for AP/Mesh/... */
  276. if (dev->mt76.vif_mask & BIT_ULL(idx) ||
  277. (vif->type != NL80211_IFTYPE_STATION && idx > 7))
  278. return -EBUSY;
  279. dev->mt76.vif_mask |= BIT_ULL(idx);
  280. mt76x02_vif_init(dev, vif, idx);
  281. return 0;
  282. }
  283. EXPORT_SYMBOL_GPL(mt76x02_add_interface);
  284. void mt76x02_remove_interface(struct ieee80211_hw *hw,
  285. struct ieee80211_vif *vif)
  286. {
  287. struct mt76x02_dev *dev = hw->priv;
  288. struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
  289. dev->mt76.vif_mask &= ~BIT_ULL(mvif->idx);
  290. rcu_assign_pointer(dev->mt76.wcid[mvif->group_wcid.idx], NULL);
  291. mt76_wcid_cleanup(&dev->mt76, &mvif->group_wcid);
  292. }
  293. EXPORT_SYMBOL_GPL(mt76x02_remove_interface);
  294. int mt76x02_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  295. struct ieee80211_ampdu_params *params)
  296. {
  297. enum ieee80211_ampdu_mlme_action action = params->action;
  298. struct ieee80211_sta *sta = params->sta;
  299. struct mt76x02_dev *dev = hw->priv;
  300. struct mt76x02_sta *msta = (struct mt76x02_sta *)sta->drv_priv;
  301. struct ieee80211_txq *txq = sta->txq[params->tid];
  302. u16 tid = params->tid;
  303. u16 ssn = params->ssn;
  304. struct mt76_txq *mtxq;
  305. int ret = 0;
  306. if (!txq)
  307. return -EINVAL;
  308. mtxq = (struct mt76_txq *)txq->drv_priv;
  309. mutex_lock(&dev->mt76.mutex);
  310. switch (action) {
  311. case IEEE80211_AMPDU_RX_START:
  312. mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid,
  313. ssn, params->buf_size);
  314. mt76_set(dev, MT_WCID_ADDR(msta->wcid.idx) + 4, BIT(16 + tid));
  315. break;
  316. case IEEE80211_AMPDU_RX_STOP:
  317. mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
  318. mt76_clear(dev, MT_WCID_ADDR(msta->wcid.idx) + 4,
  319. BIT(16 + tid));
  320. break;
  321. case IEEE80211_AMPDU_TX_OPERATIONAL:
  322. mtxq->aggr = true;
  323. mtxq->send_bar = false;
  324. ieee80211_send_bar(vif, sta->addr, tid, mtxq->agg_ssn);
  325. break;
  326. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  327. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  328. mtxq->aggr = false;
  329. break;
  330. case IEEE80211_AMPDU_TX_START:
  331. mtxq->agg_ssn = IEEE80211_SN_TO_SEQ(ssn);
  332. ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
  333. break;
  334. case IEEE80211_AMPDU_TX_STOP_CONT:
  335. mtxq->aggr = false;
  336. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  337. break;
  338. }
  339. mutex_unlock(&dev->mt76.mutex);
  340. return ret;
  341. }
  342. EXPORT_SYMBOL_GPL(mt76x02_ampdu_action);
  343. int mt76x02_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  344. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  345. struct ieee80211_key_conf *key)
  346. {
  347. struct mt76x02_dev *dev = hw->priv;
  348. struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
  349. struct mt76x02_sta *msta;
  350. struct mt76_wcid *wcid;
  351. int idx = key->keyidx;
  352. int ret;
  353. /* fall back to sw encryption for unsupported ciphers */
  354. switch (key->cipher) {
  355. case WLAN_CIPHER_SUITE_WEP40:
  356. case WLAN_CIPHER_SUITE_WEP104:
  357. case WLAN_CIPHER_SUITE_TKIP:
  358. case WLAN_CIPHER_SUITE_CCMP:
  359. break;
  360. default:
  361. return -EOPNOTSUPP;
  362. }
  363. /*
  364. * The hardware does not support per-STA RX GTK, fall back
  365. * to software mode for these.
  366. */
  367. if ((vif->type == NL80211_IFTYPE_ADHOC ||
  368. vif->type == NL80211_IFTYPE_MESH_POINT) &&
  369. (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
  370. key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
  371. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  372. return -EOPNOTSUPP;
  373. /*
  374. * In USB AP mode, broadcast/multicast frames are setup in beacon
  375. * data registers and sent via HW beacons engine, they require to
  376. * be already encrypted.
  377. */
  378. if (mt76_is_usb(&dev->mt76) &&
  379. vif->type == NL80211_IFTYPE_AP &&
  380. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  381. return -EOPNOTSUPP;
  382. /* MT76x0 GTK offloading does not work with more than one VIF */
  383. if (is_mt76x0(dev) && !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  384. return -EOPNOTSUPP;
  385. msta = sta ? (struct mt76x02_sta *)sta->drv_priv : NULL;
  386. wcid = msta ? &msta->wcid : &mvif->group_wcid;
  387. if (cmd != SET_KEY) {
  388. if (idx == wcid->hw_key_idx) {
  389. wcid->hw_key_idx = -1;
  390. wcid->sw_iv = false;
  391. }
  392. return 0;
  393. }
  394. key->hw_key_idx = wcid->idx;
  395. wcid->hw_key_idx = idx;
  396. if (key->flags & IEEE80211_KEY_FLAG_RX_MGMT) {
  397. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
  398. wcid->sw_iv = true;
  399. }
  400. mt76_wcid_key_setup(&dev->mt76, wcid, key);
  401. if (!msta) {
  402. if (key || wcid->hw_key_idx == idx) {
  403. ret = mt76x02_mac_wcid_set_key(dev, wcid->idx, key);
  404. if (ret)
  405. return ret;
  406. }
  407. return mt76x02_mac_shared_key_setup(dev, mvif->idx, idx, key);
  408. }
  409. return mt76x02_mac_wcid_set_key(dev, msta->wcid.idx, key);
  410. }
  411. EXPORT_SYMBOL_GPL(mt76x02_set_key);
  412. int mt76x02_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  413. unsigned int link_id, u16 queue,
  414. const struct ieee80211_tx_queue_params *params)
  415. {
  416. struct mt76x02_dev *dev = hw->priv;
  417. u8 cw_min = 5, cw_max = 10, qid;
  418. u32 val;
  419. qid = dev->mphy.q_tx[queue]->hw_idx;
  420. if (params->cw_min)
  421. cw_min = fls(params->cw_min);
  422. if (params->cw_max)
  423. cw_max = fls(params->cw_max);
  424. val = FIELD_PREP(MT_EDCA_CFG_TXOP, params->txop) |
  425. FIELD_PREP(MT_EDCA_CFG_AIFSN, params->aifs) |
  426. FIELD_PREP(MT_EDCA_CFG_CWMIN, cw_min) |
  427. FIELD_PREP(MT_EDCA_CFG_CWMAX, cw_max);
  428. mt76_wr(dev, MT_EDCA_CFG_AC(qid), val);
  429. val = mt76_rr(dev, MT_WMM_TXOP(qid));
  430. val &= ~(MT_WMM_TXOP_MASK << MT_WMM_TXOP_SHIFT(qid));
  431. val |= params->txop << MT_WMM_TXOP_SHIFT(qid);
  432. mt76_wr(dev, MT_WMM_TXOP(qid), val);
  433. val = mt76_rr(dev, MT_WMM_AIFSN);
  434. val &= ~(MT_WMM_AIFSN_MASK << MT_WMM_AIFSN_SHIFT(qid));
  435. val |= params->aifs << MT_WMM_AIFSN_SHIFT(qid);
  436. mt76_wr(dev, MT_WMM_AIFSN, val);
  437. val = mt76_rr(dev, MT_WMM_CWMIN);
  438. val &= ~(MT_WMM_CWMIN_MASK << MT_WMM_CWMIN_SHIFT(qid));
  439. val |= cw_min << MT_WMM_CWMIN_SHIFT(qid);
  440. mt76_wr(dev, MT_WMM_CWMIN, val);
  441. val = mt76_rr(dev, MT_WMM_CWMAX);
  442. val &= ~(MT_WMM_CWMAX_MASK << MT_WMM_CWMAX_SHIFT(qid));
  443. val |= cw_max << MT_WMM_CWMAX_SHIFT(qid);
  444. mt76_wr(dev, MT_WMM_CWMAX, val);
  445. return 0;
  446. }
  447. EXPORT_SYMBOL_GPL(mt76x02_conf_tx);
  448. void mt76x02_set_tx_ackto(struct mt76x02_dev *dev)
  449. {
  450. u8 ackto, sifs, slottime = dev->slottime;
  451. /* As defined by IEEE 802.11-2007 17.3.8.6 */
  452. slottime += 3 * dev->coverage_class;
  453. mt76_rmw_field(dev, MT_BKOFF_SLOT_CFG,
  454. MT_BKOFF_SLOT_CFG_SLOTTIME, slottime);
  455. sifs = mt76_get_field(dev, MT_XIFS_TIME_CFG,
  456. MT_XIFS_TIME_CFG_OFDM_SIFS);
  457. ackto = slottime + sifs;
  458. mt76_rmw_field(dev, MT_TX_TIMEOUT_CFG,
  459. MT_TX_TIMEOUT_CFG_ACKTO, ackto);
  460. }
  461. EXPORT_SYMBOL_GPL(mt76x02_set_tx_ackto);
  462. void mt76x02_set_coverage_class(struct ieee80211_hw *hw,
  463. s16 coverage_class)
  464. {
  465. struct mt76x02_dev *dev = hw->priv;
  466. mutex_lock(&dev->mt76.mutex);
  467. dev->coverage_class = max_t(s16, coverage_class, 0);
  468. mt76x02_set_tx_ackto(dev);
  469. mutex_unlock(&dev->mt76.mutex);
  470. }
  471. EXPORT_SYMBOL_GPL(mt76x02_set_coverage_class);
  472. int mt76x02_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
  473. {
  474. struct mt76x02_dev *dev = hw->priv;
  475. if (val != ~0 && val > 0xffff)
  476. return -EINVAL;
  477. mutex_lock(&dev->mt76.mutex);
  478. mt76x02_mac_set_rts_thresh(dev, val);
  479. mutex_unlock(&dev->mt76.mutex);
  480. return 0;
  481. }
  482. EXPORT_SYMBOL_GPL(mt76x02_set_rts_threshold);
  483. void mt76x02_sta_rate_tbl_update(struct ieee80211_hw *hw,
  484. struct ieee80211_vif *vif,
  485. struct ieee80211_sta *sta)
  486. {
  487. struct mt76x02_dev *dev = hw->priv;
  488. struct mt76x02_sta *msta = (struct mt76x02_sta *)sta->drv_priv;
  489. struct ieee80211_sta_rates *rates = rcu_dereference(sta->rates);
  490. struct ieee80211_tx_rate rate = {};
  491. if (!rates)
  492. return;
  493. rate.idx = rates->rate[0].idx;
  494. rate.flags = rates->rate[0].flags;
  495. mt76x02_mac_wcid_set_rate(dev, &msta->wcid, &rate);
  496. }
  497. EXPORT_SYMBOL_GPL(mt76x02_sta_rate_tbl_update);
  498. void mt76x02_remove_hdr_pad(struct sk_buff *skb, int len)
  499. {
  500. int hdrlen;
  501. if (!len)
  502. return;
  503. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  504. memmove(skb->data + len, skb->data, hdrlen);
  505. skb_pull(skb, len);
  506. }
  507. EXPORT_SYMBOL_GPL(mt76x02_remove_hdr_pad);
  508. void mt76x02_sw_scan_complete(struct ieee80211_hw *hw,
  509. struct ieee80211_vif *vif)
  510. {
  511. struct mt76x02_dev *dev = hw->priv;
  512. clear_bit(MT76_SCANNING, &dev->mphy.state);
  513. if (dev->cal.gain_init_done) {
  514. /* Restore AGC gain and resume calibration after scanning. */
  515. dev->cal.low_gain = -1;
  516. ieee80211_queue_delayed_work(hw, &dev->cal_work, 0);
  517. }
  518. }
  519. EXPORT_SYMBOL_GPL(mt76x02_sw_scan_complete);
  520. void mt76x02_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta,
  521. bool ps)
  522. {
  523. struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
  524. struct mt76x02_sta *msta = (struct mt76x02_sta *)sta->drv_priv;
  525. int idx = msta->wcid.idx;
  526. mt76_stop_tx_queues(&dev->mphy, sta, true);
  527. if (mt76_is_mmio(mdev))
  528. mt76x02_mac_wcid_set_drop(dev, idx, ps);
  529. }
  530. EXPORT_SYMBOL_GPL(mt76x02_sta_ps);
  531. void mt76x02_bss_info_changed(struct ieee80211_hw *hw,
  532. struct ieee80211_vif *vif,
  533. struct ieee80211_bss_conf *info,
  534. u64 changed)
  535. {
  536. struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
  537. struct mt76x02_dev *dev = hw->priv;
  538. mutex_lock(&dev->mt76.mutex);
  539. if (changed & BSS_CHANGED_BSSID)
  540. mt76x02_mac_set_bssid(dev, mvif->idx, info->bssid);
  541. if (changed & BSS_CHANGED_HT || changed & BSS_CHANGED_ERP_CTS_PROT)
  542. mt76x02_mac_set_tx_protection(dev, info->use_cts_prot,
  543. info->ht_operation_mode);
  544. if (changed & BSS_CHANGED_BEACON_INT) {
  545. mt76_rmw_field(dev, MT_BEACON_TIME_CFG,
  546. MT_BEACON_TIME_CFG_INTVAL,
  547. info->beacon_int << 4);
  548. dev->mt76.beacon_int = info->beacon_int;
  549. }
  550. if (changed & BSS_CHANGED_BEACON_ENABLED)
  551. mt76x02_mac_set_beacon_enable(dev, vif, info->enable_beacon);
  552. if (changed & BSS_CHANGED_ERP_PREAMBLE)
  553. mt76x02_mac_set_short_preamble(dev, info->use_short_preamble);
  554. if (changed & BSS_CHANGED_ERP_SLOT) {
  555. int slottime = info->use_short_slot ? 9 : 20;
  556. dev->slottime = slottime;
  557. mt76x02_set_tx_ackto(dev);
  558. }
  559. mutex_unlock(&dev->mt76.mutex);
  560. }
  561. EXPORT_SYMBOL_GPL(mt76x02_bss_info_changed);
  562. void mt76x02_config_mac_addr_list(struct mt76x02_dev *dev)
  563. {
  564. struct ieee80211_hw *hw = mt76_hw(dev);
  565. struct wiphy *wiphy = hw->wiphy;
  566. int i;
  567. for (i = 0; i < ARRAY_SIZE(dev->macaddr_list); i++) {
  568. u8 *addr = dev->macaddr_list[i].addr;
  569. memcpy(addr, dev->mphy.macaddr, ETH_ALEN);
  570. if (!i)
  571. continue;
  572. addr[0] |= BIT(1);
  573. addr[0] ^= ((i - 1) << 2);
  574. }
  575. wiphy->addresses = dev->macaddr_list;
  576. wiphy->n_addresses = ARRAY_SIZE(dev->macaddr_list);
  577. }
  578. EXPORT_SYMBOL_GPL(mt76x02_config_mac_addr_list);
  579. MODULE_DESCRIPTION("MediaTek MT76x02 helpers");
  580. MODULE_LICENSE("Dual BSD/GPL");