vht.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * VHT handling
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/ieee80211.h>
  9. #include <linux/export.h>
  10. #include <net/mac80211.h>
  11. #include "ieee80211_i.h"
  12. #include "rate.h"
  13. static void __check_vhtcap_disable(struct ieee80211_sub_if_data *sdata,
  14. struct ieee80211_sta_vht_cap *vht_cap,
  15. u32 flag)
  16. {
  17. __le32 le_flag = cpu_to_le32(flag);
  18. if (sdata->u.mgd.vht_capa_mask.vht_cap_info & le_flag &&
  19. !(sdata->u.mgd.vht_capa.vht_cap_info & le_flag))
  20. vht_cap->cap &= ~flag;
  21. }
  22. void ieee80211_apply_vhtcap_overrides(struct ieee80211_sub_if_data *sdata,
  23. struct ieee80211_sta_vht_cap *vht_cap)
  24. {
  25. int i;
  26. u16 rxmcs_mask, rxmcs_cap, rxmcs_n, txmcs_mask, txmcs_cap, txmcs_n;
  27. if (!vht_cap->vht_supported)
  28. return;
  29. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  30. return;
  31. __check_vhtcap_disable(sdata, vht_cap,
  32. IEEE80211_VHT_CAP_RXLDPC);
  33. __check_vhtcap_disable(sdata, vht_cap,
  34. IEEE80211_VHT_CAP_SHORT_GI_80);
  35. __check_vhtcap_disable(sdata, vht_cap,
  36. IEEE80211_VHT_CAP_SHORT_GI_160);
  37. __check_vhtcap_disable(sdata, vht_cap,
  38. IEEE80211_VHT_CAP_TXSTBC);
  39. __check_vhtcap_disable(sdata, vht_cap,
  40. IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
  41. __check_vhtcap_disable(sdata, vht_cap,
  42. IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE);
  43. __check_vhtcap_disable(sdata, vht_cap,
  44. IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN);
  45. __check_vhtcap_disable(sdata, vht_cap,
  46. IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN);
  47. /* Allow user to decrease AMPDU length exponent */
  48. if (sdata->u.mgd.vht_capa_mask.vht_cap_info &
  49. cpu_to_le32(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK)) {
  50. u32 cap, n;
  51. n = le32_to_cpu(sdata->u.mgd.vht_capa.vht_cap_info) &
  52. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  53. n >>= IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  54. cap = vht_cap->cap & IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  55. cap >>= IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  56. if (n < cap) {
  57. vht_cap->cap &=
  58. ~IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  59. vht_cap->cap |=
  60. n << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  61. }
  62. }
  63. /* Allow the user to decrease MCSes */
  64. rxmcs_mask =
  65. le16_to_cpu(sdata->u.mgd.vht_capa_mask.supp_mcs.rx_mcs_map);
  66. rxmcs_n = le16_to_cpu(sdata->u.mgd.vht_capa.supp_mcs.rx_mcs_map);
  67. rxmcs_n &= rxmcs_mask;
  68. rxmcs_cap = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
  69. txmcs_mask =
  70. le16_to_cpu(sdata->u.mgd.vht_capa_mask.supp_mcs.tx_mcs_map);
  71. txmcs_n = le16_to_cpu(sdata->u.mgd.vht_capa.supp_mcs.tx_mcs_map);
  72. txmcs_n &= txmcs_mask;
  73. txmcs_cap = le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
  74. for (i = 0; i < 8; i++) {
  75. u8 m, n, c;
  76. m = (rxmcs_mask >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
  77. n = (rxmcs_n >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
  78. c = (rxmcs_cap >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
  79. if (m && ((c != IEEE80211_VHT_MCS_NOT_SUPPORTED && n < c) ||
  80. n == IEEE80211_VHT_MCS_NOT_SUPPORTED)) {
  81. rxmcs_cap &= ~(3 << 2*i);
  82. rxmcs_cap |= (rxmcs_n & (3 << 2*i));
  83. }
  84. m = (txmcs_mask >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
  85. n = (txmcs_n >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
  86. c = (txmcs_cap >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
  87. if (m && ((c != IEEE80211_VHT_MCS_NOT_SUPPORTED && n < c) ||
  88. n == IEEE80211_VHT_MCS_NOT_SUPPORTED)) {
  89. txmcs_cap &= ~(3 << 2*i);
  90. txmcs_cap |= (txmcs_n & (3 << 2*i));
  91. }
  92. }
  93. vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_cap);
  94. vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_cap);
  95. }
  96. void
  97. ieee80211_vht_cap_ie_to_sta_vht_cap(struct ieee80211_sub_if_data *sdata,
  98. struct ieee80211_supported_band *sband,
  99. const struct ieee80211_vht_cap *vht_cap_ie,
  100. struct sta_info *sta)
  101. {
  102. struct ieee80211_sta_vht_cap *vht_cap = &sta->sta.vht_cap;
  103. struct ieee80211_sta_vht_cap own_cap;
  104. u32 cap_info, i;
  105. bool have_80mhz;
  106. memset(vht_cap, 0, sizeof(*vht_cap));
  107. if (!sta->sta.ht_cap.ht_supported)
  108. return;
  109. if (!vht_cap_ie || !sband->vht_cap.vht_supported)
  110. return;
  111. /* Allow VHT if at least one channel on the sband supports 80 MHz */
  112. have_80mhz = false;
  113. for (i = 0; i < sband->n_channels; i++) {
  114. if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
  115. IEEE80211_CHAN_NO_80MHZ))
  116. continue;
  117. have_80mhz = true;
  118. break;
  119. }
  120. if (!have_80mhz)
  121. return;
  122. /*
  123. * A VHT STA must support 40 MHz, but if we verify that here
  124. * then we break a few things - some APs (e.g. Netgear R6300v2
  125. * and others based on the BCM4360 chipset) will unset this
  126. * capability bit when operating in 20 MHz.
  127. */
  128. vht_cap->vht_supported = true;
  129. own_cap = sband->vht_cap;
  130. /*
  131. * If user has specified capability overrides, take care
  132. * of that if the station we're setting up is the AP that
  133. * we advertised a restricted capability set to. Override
  134. * our own capabilities and then use those below.
  135. */
  136. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  137. !test_sta_flag(sta, WLAN_STA_TDLS_PEER))
  138. ieee80211_apply_vhtcap_overrides(sdata, &own_cap);
  139. /* take some capabilities as-is */
  140. cap_info = le32_to_cpu(vht_cap_ie->vht_cap_info);
  141. vht_cap->cap = cap_info;
  142. vht_cap->cap &= IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895 |
  143. IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991 |
  144. IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
  145. IEEE80211_VHT_CAP_RXLDPC |
  146. IEEE80211_VHT_CAP_VHT_TXOP_PS |
  147. IEEE80211_VHT_CAP_HTC_VHT |
  148. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
  149. IEEE80211_VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB |
  150. IEEE80211_VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB |
  151. IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
  152. IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN;
  153. /* and some based on our own capabilities */
  154. switch (own_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
  155. case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
  156. vht_cap->cap |= cap_info &
  157. IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
  158. break;
  159. case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
  160. vht_cap->cap |= cap_info &
  161. IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
  162. break;
  163. default:
  164. /* nothing */
  165. break;
  166. }
  167. /* symmetric capabilities */
  168. vht_cap->cap |= cap_info & own_cap.cap &
  169. (IEEE80211_VHT_CAP_SHORT_GI_80 |
  170. IEEE80211_VHT_CAP_SHORT_GI_160);
  171. /* remaining ones */
  172. if (own_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
  173. vht_cap->cap |= cap_info &
  174. (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
  175. IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
  176. if (own_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
  177. vht_cap->cap |= cap_info &
  178. (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
  179. IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
  180. if (own_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
  181. vht_cap->cap |= cap_info &
  182. IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
  183. if (own_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
  184. vht_cap->cap |= cap_info &
  185. IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE;
  186. if (own_cap.cap & IEEE80211_VHT_CAP_TXSTBC)
  187. vht_cap->cap |= cap_info & IEEE80211_VHT_CAP_RXSTBC_MASK;
  188. if (own_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK)
  189. vht_cap->cap |= cap_info & IEEE80211_VHT_CAP_TXSTBC;
  190. /* Copy peer MCS info, the driver might need them. */
  191. memcpy(&vht_cap->vht_mcs, &vht_cap_ie->supp_mcs,
  192. sizeof(struct ieee80211_vht_mcs_info));
  193. /* but also restrict MCSes */
  194. for (i = 0; i < 8; i++) {
  195. u16 own_rx, own_tx, peer_rx, peer_tx;
  196. own_rx = le16_to_cpu(own_cap.vht_mcs.rx_mcs_map);
  197. own_rx = (own_rx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
  198. own_tx = le16_to_cpu(own_cap.vht_mcs.tx_mcs_map);
  199. own_tx = (own_tx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
  200. peer_rx = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
  201. peer_rx = (peer_rx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
  202. peer_tx = le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
  203. peer_tx = (peer_tx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
  204. if (peer_tx != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
  205. if (own_rx == IEEE80211_VHT_MCS_NOT_SUPPORTED)
  206. peer_tx = IEEE80211_VHT_MCS_NOT_SUPPORTED;
  207. else if (own_rx < peer_tx)
  208. peer_tx = own_rx;
  209. }
  210. if (peer_rx != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
  211. if (own_tx == IEEE80211_VHT_MCS_NOT_SUPPORTED)
  212. peer_rx = IEEE80211_VHT_MCS_NOT_SUPPORTED;
  213. else if (own_tx < peer_rx)
  214. peer_rx = own_tx;
  215. }
  216. vht_cap->vht_mcs.rx_mcs_map &=
  217. ~cpu_to_le16(IEEE80211_VHT_MCS_NOT_SUPPORTED << i * 2);
  218. vht_cap->vht_mcs.rx_mcs_map |= cpu_to_le16(peer_rx << i * 2);
  219. vht_cap->vht_mcs.tx_mcs_map &=
  220. ~cpu_to_le16(IEEE80211_VHT_MCS_NOT_SUPPORTED << i * 2);
  221. vht_cap->vht_mcs.tx_mcs_map |= cpu_to_le16(peer_tx << i * 2);
  222. }
  223. /* finally set up the bandwidth */
  224. switch (vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
  225. case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
  226. case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
  227. sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_160;
  228. break;
  229. default:
  230. sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_80;
  231. }
  232. sta->sta.bandwidth = ieee80211_sta_cur_vht_bw(sta);
  233. }
  234. enum ieee80211_sta_rx_bandwidth ieee80211_sta_cap_rx_bw(struct sta_info *sta)
  235. {
  236. struct ieee80211_sta_vht_cap *vht_cap = &sta->sta.vht_cap;
  237. u32 cap_width;
  238. if (!vht_cap->vht_supported)
  239. return sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 ?
  240. IEEE80211_STA_RX_BW_40 :
  241. IEEE80211_STA_RX_BW_20;
  242. cap_width = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
  243. if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
  244. cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
  245. return IEEE80211_STA_RX_BW_160;
  246. return IEEE80211_STA_RX_BW_80;
  247. }
  248. static enum ieee80211_sta_rx_bandwidth
  249. ieee80211_chan_width_to_rx_bw(enum nl80211_chan_width width)
  250. {
  251. switch (width) {
  252. case NL80211_CHAN_WIDTH_20_NOHT:
  253. case NL80211_CHAN_WIDTH_20:
  254. return IEEE80211_STA_RX_BW_20;
  255. case NL80211_CHAN_WIDTH_40:
  256. return IEEE80211_STA_RX_BW_40;
  257. case NL80211_CHAN_WIDTH_80:
  258. return IEEE80211_STA_RX_BW_80;
  259. case NL80211_CHAN_WIDTH_160:
  260. case NL80211_CHAN_WIDTH_80P80:
  261. return IEEE80211_STA_RX_BW_160;
  262. default:
  263. WARN_ON_ONCE(1);
  264. return IEEE80211_STA_RX_BW_20;
  265. }
  266. }
  267. enum ieee80211_sta_rx_bandwidth ieee80211_sta_cur_vht_bw(struct sta_info *sta)
  268. {
  269. struct ieee80211_sub_if_data *sdata = sta->sdata;
  270. enum ieee80211_sta_rx_bandwidth bw;
  271. enum nl80211_chan_width bss_width = sdata->vif.bss_conf.chandef.width;
  272. bw = ieee80211_sta_cap_rx_bw(sta);
  273. bw = min(bw, sta->cur_max_bandwidth);
  274. /* do not cap the BW of TDLS WIDER_BW peers by the bss */
  275. if (!test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW))
  276. bw = min(bw, ieee80211_chan_width_to_rx_bw(bss_width));
  277. return bw;
  278. }
  279. void ieee80211_sta_set_rx_nss(struct sta_info *sta)
  280. {
  281. u8 ht_rx_nss = 0, vht_rx_nss = 0;
  282. /* if we received a notification already don't overwrite it */
  283. if (sta->sta.rx_nss)
  284. return;
  285. if (sta->sta.ht_cap.ht_supported) {
  286. if (sta->sta.ht_cap.mcs.rx_mask[0])
  287. ht_rx_nss++;
  288. if (sta->sta.ht_cap.mcs.rx_mask[1])
  289. ht_rx_nss++;
  290. if (sta->sta.ht_cap.mcs.rx_mask[2])
  291. ht_rx_nss++;
  292. if (sta->sta.ht_cap.mcs.rx_mask[3])
  293. ht_rx_nss++;
  294. /* FIXME: consider rx_highest? */
  295. }
  296. if (sta->sta.vht_cap.vht_supported) {
  297. int i;
  298. u16 rx_mcs_map;
  299. rx_mcs_map = le16_to_cpu(sta->sta.vht_cap.vht_mcs.rx_mcs_map);
  300. for (i = 7; i >= 0; i--) {
  301. u8 mcs = (rx_mcs_map >> (2 * i)) & 3;
  302. if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
  303. vht_rx_nss = i + 1;
  304. break;
  305. }
  306. }
  307. /* FIXME: consider rx_highest? */
  308. }
  309. ht_rx_nss = max(ht_rx_nss, vht_rx_nss);
  310. sta->sta.rx_nss = max_t(u8, 1, ht_rx_nss);
  311. }
  312. u32 __ieee80211_vht_handle_opmode(struct ieee80211_sub_if_data *sdata,
  313. struct sta_info *sta, u8 opmode,
  314. enum ieee80211_band band)
  315. {
  316. struct ieee80211_local *local = sdata->local;
  317. struct ieee80211_supported_band *sband;
  318. enum ieee80211_sta_rx_bandwidth new_bw;
  319. u32 changed = 0;
  320. u8 nss;
  321. sband = local->hw.wiphy->bands[band];
  322. /* ignore - no support for BF yet */
  323. if (opmode & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)
  324. return 0;
  325. nss = opmode & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
  326. nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
  327. nss += 1;
  328. if (sta->sta.rx_nss != nss) {
  329. sta->sta.rx_nss = nss;
  330. changed |= IEEE80211_RC_NSS_CHANGED;
  331. }
  332. switch (opmode & IEEE80211_OPMODE_NOTIF_CHANWIDTH_MASK) {
  333. case IEEE80211_OPMODE_NOTIF_CHANWIDTH_20MHZ:
  334. sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_20;
  335. break;
  336. case IEEE80211_OPMODE_NOTIF_CHANWIDTH_40MHZ:
  337. sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_40;
  338. break;
  339. case IEEE80211_OPMODE_NOTIF_CHANWIDTH_80MHZ:
  340. sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_80;
  341. break;
  342. case IEEE80211_OPMODE_NOTIF_CHANWIDTH_160MHZ:
  343. sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_160;
  344. break;
  345. }
  346. new_bw = ieee80211_sta_cur_vht_bw(sta);
  347. if (new_bw != sta->sta.bandwidth) {
  348. sta->sta.bandwidth = new_bw;
  349. changed |= IEEE80211_RC_BW_CHANGED;
  350. }
  351. return changed;
  352. }
  353. void ieee80211_vht_handle_opmode(struct ieee80211_sub_if_data *sdata,
  354. struct sta_info *sta, u8 opmode,
  355. enum ieee80211_band band)
  356. {
  357. struct ieee80211_local *local = sdata->local;
  358. struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
  359. u32 changed = __ieee80211_vht_handle_opmode(sdata, sta, opmode, band);
  360. if (changed > 0)
  361. rate_control_rate_update(local, sband, sta, changed);
  362. }
  363. void ieee80211_get_vht_mask_from_cap(__le16 vht_cap,
  364. u16 vht_mask[NL80211_VHT_NSS_MAX])
  365. {
  366. int i;
  367. u16 mask, cap = le16_to_cpu(vht_cap);
  368. for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
  369. mask = (cap >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
  370. switch (mask) {
  371. case IEEE80211_VHT_MCS_SUPPORT_0_7:
  372. vht_mask[i] = 0x00FF;
  373. break;
  374. case IEEE80211_VHT_MCS_SUPPORT_0_8:
  375. vht_mask[i] = 0x01FF;
  376. break;
  377. case IEEE80211_VHT_MCS_SUPPORT_0_9:
  378. vht_mask[i] = 0x03FF;
  379. break;
  380. case IEEE80211_VHT_MCS_NOT_SUPPORTED:
  381. default:
  382. vht_mask[i] = 0;
  383. break;
  384. }
  385. }
  386. }