mac.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include "mt7601u.h"
  15. #include "trace.h"
  16. #include <linux/etherdevice.h>
  17. static void
  18. mt76_mac_process_tx_rate(struct ieee80211_tx_rate *txrate, u16 rate)
  19. {
  20. u8 idx = MT76_GET(MT_TXWI_RATE_MCS, rate);
  21. txrate->idx = 0;
  22. txrate->flags = 0;
  23. txrate->count = 1;
  24. switch (MT76_GET(MT_TXWI_RATE_PHY_MODE, rate)) {
  25. case MT_PHY_TYPE_OFDM:
  26. txrate->idx = idx + 4;
  27. return;
  28. case MT_PHY_TYPE_CCK:
  29. if (idx >= 8)
  30. idx -= 8;
  31. txrate->idx = idx;
  32. return;
  33. case MT_PHY_TYPE_HT_GF:
  34. txrate->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  35. /* fall through */
  36. case MT_PHY_TYPE_HT:
  37. txrate->flags |= IEEE80211_TX_RC_MCS;
  38. txrate->idx = idx;
  39. break;
  40. default:
  41. WARN_ON(1);
  42. return;
  43. }
  44. if (MT76_GET(MT_TXWI_RATE_BW, rate) == MT_PHY_BW_40)
  45. txrate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  46. if (rate & MT_TXWI_RATE_SGI)
  47. txrate->flags |= IEEE80211_TX_RC_SHORT_GI;
  48. }
  49. static void
  50. mt76_mac_fill_tx_status(struct mt7601u_dev *dev, struct ieee80211_tx_info *info,
  51. struct mt76_tx_status *st)
  52. {
  53. struct ieee80211_tx_rate *rate = info->status.rates;
  54. int cur_idx, last_rate;
  55. int i;
  56. last_rate = min_t(int, st->retry, IEEE80211_TX_MAX_RATES - 1);
  57. mt76_mac_process_tx_rate(&rate[last_rate], st->rate);
  58. if (last_rate < IEEE80211_TX_MAX_RATES - 1)
  59. rate[last_rate + 1].idx = -1;
  60. cur_idx = rate[last_rate].idx + st->retry;
  61. for (i = 0; i <= last_rate; i++) {
  62. rate[i].flags = rate[last_rate].flags;
  63. rate[i].idx = max_t(int, 0, cur_idx - i);
  64. rate[i].count = 1;
  65. }
  66. if (last_rate > 0)
  67. rate[last_rate - 1].count = st->retry + 1 - last_rate;
  68. info->status.ampdu_len = 1;
  69. info->status.ampdu_ack_len = st->success;
  70. if (st->is_probe)
  71. info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  72. if (st->aggr)
  73. info->flags |= IEEE80211_TX_CTL_AMPDU |
  74. IEEE80211_TX_STAT_AMPDU;
  75. if (!st->ack_req)
  76. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  77. else if (st->success)
  78. info->flags |= IEEE80211_TX_STAT_ACK;
  79. }
  80. u16 mt76_mac_tx_rate_val(struct mt7601u_dev *dev,
  81. const struct ieee80211_tx_rate *rate, u8 *nss_val)
  82. {
  83. u16 rateval;
  84. u8 phy, rate_idx;
  85. u8 nss = 1;
  86. u8 bw = 0;
  87. if (rate->flags & IEEE80211_TX_RC_MCS) {
  88. rate_idx = rate->idx;
  89. nss = 1 + (rate->idx >> 3);
  90. phy = MT_PHY_TYPE_HT;
  91. if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  92. phy = MT_PHY_TYPE_HT_GF;
  93. if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  94. bw = 1;
  95. } else {
  96. const struct ieee80211_rate *r;
  97. int band = dev->chandef.chan->band;
  98. u16 val;
  99. r = &dev->hw->wiphy->bands[band]->bitrates[rate->idx];
  100. if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  101. val = r->hw_value_short;
  102. else
  103. val = r->hw_value;
  104. phy = val >> 8;
  105. rate_idx = val & 0xff;
  106. bw = 0;
  107. }
  108. rateval = MT76_SET(MT_RXWI_RATE_MCS, rate_idx);
  109. rateval |= MT76_SET(MT_RXWI_RATE_PHY, phy);
  110. rateval |= MT76_SET(MT_RXWI_RATE_BW, bw);
  111. if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
  112. rateval |= MT_RXWI_RATE_SGI;
  113. *nss_val = nss;
  114. return rateval;
  115. }
  116. void mt76_mac_wcid_set_rate(struct mt7601u_dev *dev, struct mt76_wcid *wcid,
  117. const struct ieee80211_tx_rate *rate)
  118. {
  119. unsigned long flags;
  120. spin_lock_irqsave(&dev->lock, flags);
  121. wcid->tx_rate = mt76_mac_tx_rate_val(dev, rate, &wcid->tx_rate_nss);
  122. wcid->tx_rate_set = true;
  123. spin_unlock_irqrestore(&dev->lock, flags);
  124. }
  125. struct mt76_tx_status mt7601u_mac_fetch_tx_status(struct mt7601u_dev *dev)
  126. {
  127. struct mt76_tx_status stat = {};
  128. u32 val;
  129. val = mt7601u_rr(dev, MT_TX_STAT_FIFO);
  130. stat.valid = !!(val & MT_TX_STAT_FIFO_VALID);
  131. stat.success = !!(val & MT_TX_STAT_FIFO_SUCCESS);
  132. stat.aggr = !!(val & MT_TX_STAT_FIFO_AGGR);
  133. stat.ack_req = !!(val & MT_TX_STAT_FIFO_ACKREQ);
  134. stat.pktid = MT76_GET(MT_TX_STAT_FIFO_PID_TYPE, val);
  135. stat.wcid = MT76_GET(MT_TX_STAT_FIFO_WCID, val);
  136. stat.rate = MT76_GET(MT_TX_STAT_FIFO_RATE, val);
  137. return stat;
  138. }
  139. void mt76_send_tx_status(struct mt7601u_dev *dev, struct mt76_tx_status *stat)
  140. {
  141. struct ieee80211_tx_info info = {};
  142. struct ieee80211_sta *sta = NULL;
  143. struct mt76_wcid *wcid = NULL;
  144. void *msta;
  145. rcu_read_lock();
  146. if (stat->wcid < ARRAY_SIZE(dev->wcid))
  147. wcid = rcu_dereference(dev->wcid[stat->wcid]);
  148. if (wcid) {
  149. msta = container_of(wcid, struct mt76_sta, wcid);
  150. sta = container_of(msta, struct ieee80211_sta,
  151. drv_priv);
  152. }
  153. mt76_mac_fill_tx_status(dev, &info, stat);
  154. spin_lock_bh(&dev->mac_lock);
  155. ieee80211_tx_status_noskb(dev->hw, sta, &info);
  156. spin_unlock_bh(&dev->mac_lock);
  157. rcu_read_unlock();
  158. }
  159. void mt7601u_mac_set_protection(struct mt7601u_dev *dev, bool legacy_prot,
  160. int ht_mode)
  161. {
  162. int mode = ht_mode & IEEE80211_HT_OP_MODE_PROTECTION;
  163. bool non_gf = !!(ht_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
  164. u32 prot[6];
  165. bool ht_rts[4] = {};
  166. int i;
  167. prot[0] = MT_PROT_NAV_SHORT |
  168. MT_PROT_TXOP_ALLOW_ALL |
  169. MT_PROT_RTS_THR_EN;
  170. prot[1] = prot[0];
  171. if (legacy_prot)
  172. prot[1] |= MT_PROT_CTRL_CTS2SELF;
  173. prot[2] = prot[4] = MT_PROT_NAV_SHORT | MT_PROT_TXOP_ALLOW_BW20;
  174. prot[3] = prot[5] = MT_PROT_NAV_SHORT | MT_PROT_TXOP_ALLOW_ALL;
  175. if (legacy_prot) {
  176. prot[2] |= MT_PROT_RATE_CCK_11;
  177. prot[3] |= MT_PROT_RATE_CCK_11;
  178. prot[4] |= MT_PROT_RATE_CCK_11;
  179. prot[5] |= MT_PROT_RATE_CCK_11;
  180. } else {
  181. prot[2] |= MT_PROT_RATE_OFDM_24;
  182. prot[3] |= MT_PROT_RATE_DUP_OFDM_24;
  183. prot[4] |= MT_PROT_RATE_OFDM_24;
  184. prot[5] |= MT_PROT_RATE_DUP_OFDM_24;
  185. }
  186. switch (mode) {
  187. case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
  188. break;
  189. case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
  190. ht_rts[0] = ht_rts[1] = ht_rts[2] = ht_rts[3] = true;
  191. break;
  192. case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
  193. ht_rts[1] = ht_rts[3] = true;
  194. break;
  195. case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
  196. ht_rts[0] = ht_rts[1] = ht_rts[2] = ht_rts[3] = true;
  197. break;
  198. }
  199. if (non_gf)
  200. ht_rts[2] = ht_rts[3] = true;
  201. for (i = 0; i < 4; i++)
  202. if (ht_rts[i])
  203. prot[i + 2] |= MT_PROT_CTRL_RTS_CTS;
  204. for (i = 0; i < 6; i++)
  205. mt7601u_wr(dev, MT_CCK_PROT_CFG + i * 4, prot[i]);
  206. }
  207. void mt7601u_mac_set_short_preamble(struct mt7601u_dev *dev, bool short_preamb)
  208. {
  209. if (short_preamb)
  210. mt76_set(dev, MT_AUTO_RSP_CFG, MT_AUTO_RSP_PREAMB_SHORT);
  211. else
  212. mt76_clear(dev, MT_AUTO_RSP_CFG, MT_AUTO_RSP_PREAMB_SHORT);
  213. }
  214. void mt7601u_mac_config_tsf(struct mt7601u_dev *dev, bool enable, int interval)
  215. {
  216. u32 val = mt7601u_rr(dev, MT_BEACON_TIME_CFG);
  217. val &= ~(MT_BEACON_TIME_CFG_TIMER_EN |
  218. MT_BEACON_TIME_CFG_SYNC_MODE |
  219. MT_BEACON_TIME_CFG_TBTT_EN);
  220. if (!enable) {
  221. mt7601u_wr(dev, MT_BEACON_TIME_CFG, val);
  222. return;
  223. }
  224. val &= ~MT_BEACON_TIME_CFG_INTVAL;
  225. val |= MT76_SET(MT_BEACON_TIME_CFG_INTVAL, interval << 4) |
  226. MT_BEACON_TIME_CFG_TIMER_EN |
  227. MT_BEACON_TIME_CFG_SYNC_MODE |
  228. MT_BEACON_TIME_CFG_TBTT_EN;
  229. }
  230. static void mt7601u_check_mac_err(struct mt7601u_dev *dev)
  231. {
  232. u32 val = mt7601u_rr(dev, 0x10f4);
  233. if (!(val & BIT(29)) || !(val & (BIT(7) | BIT(5))))
  234. return;
  235. dev_err(dev->dev, "Error: MAC specific condition occurred\n");
  236. mt76_set(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_RESET_CSR);
  237. udelay(10);
  238. mt76_clear(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_RESET_CSR);
  239. }
  240. void mt7601u_mac_work(struct work_struct *work)
  241. {
  242. struct mt7601u_dev *dev = container_of(work, struct mt7601u_dev,
  243. mac_work.work);
  244. struct {
  245. u32 addr_base;
  246. u32 span;
  247. u64 *stat_base;
  248. } spans[] = {
  249. { MT_RX_STA_CNT0, 3, dev->stats.rx_stat },
  250. { MT_TX_STA_CNT0, 3, dev->stats.tx_stat },
  251. { MT_TX_AGG_STAT, 1, dev->stats.aggr_stat },
  252. { MT_MPDU_DENSITY_CNT, 1, dev->stats.zero_len_del },
  253. { MT_TX_AGG_CNT_BASE0, 8, &dev->stats.aggr_n[0] },
  254. { MT_TX_AGG_CNT_BASE1, 8, &dev->stats.aggr_n[16] },
  255. };
  256. u32 sum, n;
  257. int i, j, k;
  258. /* Note: using MCU_RANDOM_READ is actually slower then reading all the
  259. * registers by hand. MCU takes ca. 20ms to complete read of 24
  260. * registers while reading them one by one will takes roughly
  261. * 24*200us =~ 5ms.
  262. */
  263. k = 0;
  264. n = 0;
  265. sum = 0;
  266. for (i = 0; i < ARRAY_SIZE(spans); i++)
  267. for (j = 0; j < spans[i].span; j++) {
  268. u32 val = mt7601u_rr(dev, spans[i].addr_base + j * 4);
  269. spans[i].stat_base[j * 2] += val & 0xffff;
  270. spans[i].stat_base[j * 2 + 1] += val >> 16;
  271. /* Calculate average AMPDU length */
  272. if (spans[i].addr_base != MT_TX_AGG_CNT_BASE0 &&
  273. spans[i].addr_base != MT_TX_AGG_CNT_BASE1)
  274. continue;
  275. n += (val >> 16) + (val & 0xffff);
  276. sum += (val & 0xffff) * (1 + k * 2) +
  277. (val >> 16) * (2 + k * 2);
  278. k++;
  279. }
  280. atomic_set(&dev->avg_ampdu_len, n ? DIV_ROUND_CLOSEST(sum, n) : 1);
  281. mt7601u_check_mac_err(dev);
  282. ieee80211_queue_delayed_work(dev->hw, &dev->mac_work, 10 * HZ);
  283. }
  284. void
  285. mt7601u_mac_wcid_setup(struct mt7601u_dev *dev, u8 idx, u8 vif_idx, u8 *mac)
  286. {
  287. u8 zmac[ETH_ALEN] = {};
  288. u32 attr;
  289. attr = MT76_SET(MT_WCID_ATTR_BSS_IDX, vif_idx & 7) |
  290. MT76_SET(MT_WCID_ATTR_BSS_IDX_EXT, !!(vif_idx & 8));
  291. mt76_wr(dev, MT_WCID_ATTR(idx), attr);
  292. if (mac)
  293. memcpy(zmac, mac, sizeof(zmac));
  294. mt7601u_addr_wr(dev, MT_WCID_ADDR(idx), zmac);
  295. }
  296. void mt7601u_mac_set_ampdu_factor(struct mt7601u_dev *dev)
  297. {
  298. struct ieee80211_sta *sta;
  299. struct mt76_wcid *wcid;
  300. void *msta;
  301. u8 min_factor = 3;
  302. int i;
  303. rcu_read_lock();
  304. for (i = 0; i < ARRAY_SIZE(dev->wcid); i++) {
  305. wcid = rcu_dereference(dev->wcid[i]);
  306. if (!wcid)
  307. continue;
  308. msta = container_of(wcid, struct mt76_sta, wcid);
  309. sta = container_of(msta, struct ieee80211_sta, drv_priv);
  310. min_factor = min(min_factor, sta->ht_cap.ampdu_factor);
  311. }
  312. rcu_read_unlock();
  313. mt7601u_wr(dev, MT_MAX_LEN_CFG, 0xa0fff |
  314. MT76_SET(MT_MAX_LEN_CFG_AMPDU, min_factor));
  315. }
  316. static void
  317. mt76_mac_process_rate(struct ieee80211_rx_status *status, u16 rate)
  318. {
  319. u8 idx = MT76_GET(MT_RXWI_RATE_MCS, rate);
  320. switch (MT76_GET(MT_RXWI_RATE_PHY, rate)) {
  321. case MT_PHY_TYPE_OFDM:
  322. if (WARN_ON(idx >= 8))
  323. idx = 0;
  324. idx += 4;
  325. status->rate_idx = idx;
  326. return;
  327. case MT_PHY_TYPE_CCK:
  328. if (idx >= 8) {
  329. idx -= 8;
  330. status->flag |= RX_FLAG_SHORTPRE;
  331. }
  332. if (WARN_ON(idx >= 4))
  333. idx = 0;
  334. status->rate_idx = idx;
  335. return;
  336. case MT_PHY_TYPE_HT_GF:
  337. status->flag |= RX_FLAG_HT_GF;
  338. /* fall through */
  339. case MT_PHY_TYPE_HT:
  340. status->flag |= RX_FLAG_HT;
  341. status->rate_idx = idx;
  342. break;
  343. default:
  344. WARN_ON(1);
  345. return;
  346. }
  347. if (rate & MT_RXWI_RATE_SGI)
  348. status->flag |= RX_FLAG_SHORT_GI;
  349. if (rate & MT_RXWI_RATE_STBC)
  350. status->flag |= 1 << RX_FLAG_STBC_SHIFT;
  351. if (rate & MT_RXWI_RATE_BW)
  352. status->flag |= RX_FLAG_40MHZ;
  353. }
  354. static void
  355. mt7601u_rx_monitor_beacon(struct mt7601u_dev *dev, struct mt7601u_rxwi *rxwi,
  356. u16 rate, int rssi)
  357. {
  358. dev->bcn_freq_off = rxwi->freq_off;
  359. dev->bcn_phy_mode = MT76_GET(MT_RXWI_RATE_PHY, rate);
  360. dev->avg_rssi = (dev->avg_rssi * 15) / 16 + (rssi << 8);
  361. }
  362. static int
  363. mt7601u_rx_is_our_beacon(struct mt7601u_dev *dev, u8 *data)
  364. {
  365. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
  366. return ieee80211_is_beacon(hdr->frame_control) &&
  367. ether_addr_equal(hdr->addr2, dev->ap_bssid);
  368. }
  369. u32 mt76_mac_process_rx(struct mt7601u_dev *dev, struct sk_buff *skb,
  370. u8 *data, void *rxi)
  371. {
  372. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  373. struct mt7601u_rxwi *rxwi = rxi;
  374. u32 len, ctl = le32_to_cpu(rxwi->ctl);
  375. u16 rate = le16_to_cpu(rxwi->rate);
  376. int rssi;
  377. len = MT76_GET(MT_RXWI_CTL_MPDU_LEN, ctl);
  378. if (len < 10)
  379. return 0;
  380. if (rxwi->rxinfo & cpu_to_le32(MT_RXINFO_DECRYPT)) {
  381. status->flag |= RX_FLAG_DECRYPTED;
  382. status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED;
  383. }
  384. status->chains = BIT(0);
  385. rssi = mt7601u_phy_get_rssi(dev, rxwi, rate);
  386. status->chain_signal[0] = status->signal = rssi;
  387. status->freq = dev->chandef.chan->center_freq;
  388. status->band = dev->chandef.chan->band;
  389. mt76_mac_process_rate(status, rate);
  390. spin_lock_bh(&dev->con_mon_lock);
  391. if (mt7601u_rx_is_our_beacon(dev, data))
  392. mt7601u_rx_monitor_beacon(dev, rxwi, rate, rssi);
  393. else if (rxwi->rxinfo & cpu_to_le32(MT_RXINFO_U2M))
  394. dev->avg_rssi = (dev->avg_rssi * 15) / 16 + (rssi << 8);
  395. spin_unlock_bh(&dev->con_mon_lock);
  396. return len;
  397. }
  398. static enum mt76_cipher_type
  399. mt76_mac_get_key_info(struct ieee80211_key_conf *key, u8 *key_data)
  400. {
  401. memset(key_data, 0, 32);
  402. if (!key)
  403. return MT_CIPHER_NONE;
  404. if (key->keylen > 32)
  405. return MT_CIPHER_NONE;
  406. memcpy(key_data, key->key, key->keylen);
  407. switch (key->cipher) {
  408. case WLAN_CIPHER_SUITE_WEP40:
  409. return MT_CIPHER_WEP40;
  410. case WLAN_CIPHER_SUITE_WEP104:
  411. return MT_CIPHER_WEP104;
  412. case WLAN_CIPHER_SUITE_TKIP:
  413. return MT_CIPHER_TKIP;
  414. case WLAN_CIPHER_SUITE_CCMP:
  415. return MT_CIPHER_AES_CCMP;
  416. default:
  417. return MT_CIPHER_NONE;
  418. }
  419. }
  420. int mt76_mac_wcid_set_key(struct mt7601u_dev *dev, u8 idx,
  421. struct ieee80211_key_conf *key)
  422. {
  423. enum mt76_cipher_type cipher;
  424. u8 key_data[32];
  425. u8 iv_data[8];
  426. u32 val;
  427. cipher = mt76_mac_get_key_info(key, key_data);
  428. if (cipher == MT_CIPHER_NONE && key)
  429. return -EINVAL;
  430. trace_set_key(dev, idx);
  431. mt7601u_wr_copy(dev, MT_WCID_KEY(idx), key_data, sizeof(key_data));
  432. memset(iv_data, 0, sizeof(iv_data));
  433. if (key) {
  434. iv_data[3] = key->keyidx << 6;
  435. if (cipher >= MT_CIPHER_TKIP) {
  436. /* Note: start with 1 to comply with spec,
  437. * (see comment on common/cmm_wpa.c:4291).
  438. */
  439. iv_data[0] |= 1;
  440. iv_data[3] |= 0x20;
  441. }
  442. }
  443. mt7601u_wr_copy(dev, MT_WCID_IV(idx), iv_data, sizeof(iv_data));
  444. val = mt7601u_rr(dev, MT_WCID_ATTR(idx));
  445. val &= ~MT_WCID_ATTR_PKEY_MODE & ~MT_WCID_ATTR_PKEY_MODE_EXT;
  446. val |= MT76_SET(MT_WCID_ATTR_PKEY_MODE, cipher & 7) |
  447. MT76_SET(MT_WCID_ATTR_PKEY_MODE_EXT, cipher >> 3);
  448. val &= ~MT_WCID_ATTR_PAIRWISE;
  449. val |= MT_WCID_ATTR_PAIRWISE *
  450. !!(key && key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  451. mt7601u_wr(dev, MT_WCID_ATTR(idx), val);
  452. return 0;
  453. }
  454. int mt76_mac_shared_key_setup(struct mt7601u_dev *dev, u8 vif_idx, u8 key_idx,
  455. struct ieee80211_key_conf *key)
  456. {
  457. enum mt76_cipher_type cipher;
  458. u8 key_data[32];
  459. u32 val;
  460. cipher = mt76_mac_get_key_info(key, key_data);
  461. if (cipher == MT_CIPHER_NONE && key)
  462. return -EINVAL;
  463. trace_set_shared_key(dev, vif_idx, key_idx);
  464. mt7601u_wr_copy(dev, MT_SKEY(vif_idx, key_idx),
  465. key_data, sizeof(key_data));
  466. val = mt76_rr(dev, MT_SKEY_MODE(vif_idx));
  467. val &= ~(MT_SKEY_MODE_MASK << MT_SKEY_MODE_SHIFT(vif_idx, key_idx));
  468. val |= cipher << MT_SKEY_MODE_SHIFT(vif_idx, key_idx);
  469. mt76_wr(dev, MT_SKEY_MODE(vif_idx), val);
  470. return 0;
  471. }