mt76x02_util.c 19 KB

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