mt76x02_util.c 19 KB

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