GNU Linux-libre 4.19.264-gnu1
[releases.git] / net / mac80211 / vht.c
1 /*
2  * VHT handling
3  *
4  * Portions of this file
5  * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/ieee80211.h>
13 #include <linux/export.h>
14 #include <net/mac80211.h>
15 #include "ieee80211_i.h"
16 #include "rate.h"
17
18
19 static void __check_vhtcap_disable(struct ieee80211_sub_if_data *sdata,
20                                    struct ieee80211_sta_vht_cap *vht_cap,
21                                    u32 flag)
22 {
23         __le32 le_flag = cpu_to_le32(flag);
24
25         if (sdata->u.mgd.vht_capa_mask.vht_cap_info & le_flag &&
26             !(sdata->u.mgd.vht_capa.vht_cap_info & le_flag))
27                 vht_cap->cap &= ~flag;
28 }
29
30 void ieee80211_apply_vhtcap_overrides(struct ieee80211_sub_if_data *sdata,
31                                       struct ieee80211_sta_vht_cap *vht_cap)
32 {
33         int i;
34         u16 rxmcs_mask, rxmcs_cap, rxmcs_n, txmcs_mask, txmcs_cap, txmcs_n;
35
36         if (!vht_cap->vht_supported)
37                 return;
38
39         if (sdata->vif.type != NL80211_IFTYPE_STATION)
40                 return;
41
42         __check_vhtcap_disable(sdata, vht_cap,
43                                IEEE80211_VHT_CAP_RXLDPC);
44         __check_vhtcap_disable(sdata, vht_cap,
45                                IEEE80211_VHT_CAP_SHORT_GI_80);
46         __check_vhtcap_disable(sdata, vht_cap,
47                                IEEE80211_VHT_CAP_SHORT_GI_160);
48         __check_vhtcap_disable(sdata, vht_cap,
49                                IEEE80211_VHT_CAP_TXSTBC);
50         __check_vhtcap_disable(sdata, vht_cap,
51                                IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
52         __check_vhtcap_disable(sdata, vht_cap,
53                                IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE);
54         __check_vhtcap_disable(sdata, vht_cap,
55                                IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN);
56         __check_vhtcap_disable(sdata, vht_cap,
57                                IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN);
58
59         /* Allow user to decrease AMPDU length exponent */
60         if (sdata->u.mgd.vht_capa_mask.vht_cap_info &
61             cpu_to_le32(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK)) {
62                 u32 cap, n;
63
64                 n = le32_to_cpu(sdata->u.mgd.vht_capa.vht_cap_info) &
65                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
66                 n >>= IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
67                 cap = vht_cap->cap & IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
68                 cap >>= IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
69
70                 if (n < cap) {
71                         vht_cap->cap &=
72                                 ~IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
73                         vht_cap->cap |=
74                                 n << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
75                 }
76         }
77
78         /* Allow the user to decrease MCSes */
79         rxmcs_mask =
80                 le16_to_cpu(sdata->u.mgd.vht_capa_mask.supp_mcs.rx_mcs_map);
81         rxmcs_n = le16_to_cpu(sdata->u.mgd.vht_capa.supp_mcs.rx_mcs_map);
82         rxmcs_n &= rxmcs_mask;
83         rxmcs_cap = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
84
85         txmcs_mask =
86                 le16_to_cpu(sdata->u.mgd.vht_capa_mask.supp_mcs.tx_mcs_map);
87         txmcs_n = le16_to_cpu(sdata->u.mgd.vht_capa.supp_mcs.tx_mcs_map);
88         txmcs_n &= txmcs_mask;
89         txmcs_cap = le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
90         for (i = 0; i < 8; i++) {
91                 u8 m, n, c;
92
93                 m = (rxmcs_mask >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
94                 n = (rxmcs_n >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
95                 c = (rxmcs_cap >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
96
97                 if (m && ((c != IEEE80211_VHT_MCS_NOT_SUPPORTED && n < c) ||
98                           n == IEEE80211_VHT_MCS_NOT_SUPPORTED)) {
99                         rxmcs_cap &= ~(3 << 2*i);
100                         rxmcs_cap |= (rxmcs_n & (3 << 2*i));
101                 }
102
103                 m = (txmcs_mask >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
104                 n = (txmcs_n >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
105                 c = (txmcs_cap >> 2*i) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
106
107                 if (m && ((c != IEEE80211_VHT_MCS_NOT_SUPPORTED && n < c) ||
108                           n == IEEE80211_VHT_MCS_NOT_SUPPORTED)) {
109                         txmcs_cap &= ~(3 << 2*i);
110                         txmcs_cap |= (txmcs_n & (3 << 2*i));
111                 }
112         }
113         vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_cap);
114         vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_cap);
115 }
116
117 void
118 ieee80211_vht_cap_ie_to_sta_vht_cap(struct ieee80211_sub_if_data *sdata,
119                                     struct ieee80211_supported_band *sband,
120                                     const struct ieee80211_vht_cap *vht_cap_ie,
121                                     struct sta_info *sta)
122 {
123         struct ieee80211_sta_vht_cap *vht_cap = &sta->sta.vht_cap;
124         struct ieee80211_sta_vht_cap own_cap;
125         u32 cap_info, i;
126         bool have_80mhz;
127
128         memset(vht_cap, 0, sizeof(*vht_cap));
129
130         if (!sta->sta.ht_cap.ht_supported)
131                 return;
132
133         if (!vht_cap_ie || !sband->vht_cap.vht_supported)
134                 return;
135
136         /* Allow VHT if at least one channel on the sband supports 80 MHz */
137         have_80mhz = false;
138         for (i = 0; i < sband->n_channels; i++) {
139                 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
140                                                 IEEE80211_CHAN_NO_80MHZ))
141                         continue;
142
143                 have_80mhz = true;
144                 break;
145         }
146
147         if (!have_80mhz)
148                 return;
149
150         /*
151          * A VHT STA must support 40 MHz, but if we verify that here
152          * then we break a few things - some APs (e.g. Netgear R6300v2
153          * and others based on the BCM4360 chipset) will unset this
154          * capability bit when operating in 20 MHz.
155          */
156
157         vht_cap->vht_supported = true;
158
159         own_cap = sband->vht_cap;
160         /*
161          * If user has specified capability overrides, take care
162          * of that if the station we're setting up is the AP that
163          * we advertised a restricted capability set to. Override
164          * our own capabilities and then use those below.
165          */
166         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
167             !test_sta_flag(sta, WLAN_STA_TDLS_PEER))
168                 ieee80211_apply_vhtcap_overrides(sdata, &own_cap);
169
170         /* take some capabilities as-is */
171         cap_info = le32_to_cpu(vht_cap_ie->vht_cap_info);
172         vht_cap->cap = cap_info;
173         vht_cap->cap &= IEEE80211_VHT_CAP_RXLDPC |
174                         IEEE80211_VHT_CAP_VHT_TXOP_PS |
175                         IEEE80211_VHT_CAP_HTC_VHT |
176                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
177                         IEEE80211_VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB |
178                         IEEE80211_VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB |
179                         IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
180                         IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN;
181
182         vht_cap->cap |= min_t(u32, cap_info & IEEE80211_VHT_CAP_MAX_MPDU_MASK,
183                               own_cap.cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK);
184
185         /* and some based on our own capabilities */
186         switch (own_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
187         case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
188                 vht_cap->cap |= cap_info &
189                                 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
190                 break;
191         case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
192                 vht_cap->cap |= cap_info &
193                                 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
194                 break;
195         default:
196                 /* nothing */
197                 break;
198         }
199
200         /* symmetric capabilities */
201         vht_cap->cap |= cap_info & own_cap.cap &
202                         (IEEE80211_VHT_CAP_SHORT_GI_80 |
203                          IEEE80211_VHT_CAP_SHORT_GI_160);
204
205         /* remaining ones */
206         if (own_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
207                 vht_cap->cap |= cap_info &
208                                 (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
209                                  IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
210
211         if (own_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
212                 vht_cap->cap |= cap_info &
213                                 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
214                                  IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
215
216         if (own_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
217                 vht_cap->cap |= cap_info &
218                                 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
219
220         if (own_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
221                 vht_cap->cap |= cap_info &
222                                 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE;
223
224         if (own_cap.cap & IEEE80211_VHT_CAP_TXSTBC)
225                 vht_cap->cap |= cap_info & IEEE80211_VHT_CAP_RXSTBC_MASK;
226
227         if (own_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK)
228                 vht_cap->cap |= cap_info & IEEE80211_VHT_CAP_TXSTBC;
229
230         /* Copy peer MCS info, the driver might need them. */
231         memcpy(&vht_cap->vht_mcs, &vht_cap_ie->supp_mcs,
232                sizeof(struct ieee80211_vht_mcs_info));
233
234         /* but also restrict MCSes */
235         for (i = 0; i < 8; i++) {
236                 u16 own_rx, own_tx, peer_rx, peer_tx;
237
238                 own_rx = le16_to_cpu(own_cap.vht_mcs.rx_mcs_map);
239                 own_rx = (own_rx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
240
241                 own_tx = le16_to_cpu(own_cap.vht_mcs.tx_mcs_map);
242                 own_tx = (own_tx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
243
244                 peer_rx = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
245                 peer_rx = (peer_rx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
246
247                 peer_tx = le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
248                 peer_tx = (peer_tx >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
249
250                 if (peer_tx != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
251                         if (own_rx == IEEE80211_VHT_MCS_NOT_SUPPORTED)
252                                 peer_tx = IEEE80211_VHT_MCS_NOT_SUPPORTED;
253                         else if (own_rx < peer_tx)
254                                 peer_tx = own_rx;
255                 }
256
257                 if (peer_rx != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
258                         if (own_tx == IEEE80211_VHT_MCS_NOT_SUPPORTED)
259                                 peer_rx = IEEE80211_VHT_MCS_NOT_SUPPORTED;
260                         else if (own_tx < peer_rx)
261                                 peer_rx = own_tx;
262                 }
263
264                 vht_cap->vht_mcs.rx_mcs_map &=
265                         ~cpu_to_le16(IEEE80211_VHT_MCS_NOT_SUPPORTED << i * 2);
266                 vht_cap->vht_mcs.rx_mcs_map |= cpu_to_le16(peer_rx << i * 2);
267
268                 vht_cap->vht_mcs.tx_mcs_map &=
269                         ~cpu_to_le16(IEEE80211_VHT_MCS_NOT_SUPPORTED << i * 2);
270                 vht_cap->vht_mcs.tx_mcs_map |= cpu_to_le16(peer_tx << i * 2);
271         }
272
273         /*
274          * This is a workaround for VHT-enabled STAs which break the spec
275          * and have the VHT-MCS Rx map filled in with value 3 for all eight
276          * spacial streams, an example is AR9462.
277          *
278          * As per spec, in section 22.1.1 Introduction to the VHT PHY
279          * A VHT STA shall support at least single spactial stream VHT-MCSs
280          * 0 to 7 (transmit and receive) in all supported channel widths.
281          */
282         if (vht_cap->vht_mcs.rx_mcs_map == cpu_to_le16(0xFFFF)) {
283                 vht_cap->vht_supported = false;
284                 sdata_info(sdata, "Ignoring VHT IE from %pM due to invalid rx_mcs_map\n",
285                            sta->addr);
286                 return;
287         }
288
289         /* finally set up the bandwidth */
290         switch (vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
291         case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
292         case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
293                 sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_160;
294                 break;
295         default:
296                 sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_80;
297         }
298
299         sta->sta.bandwidth = ieee80211_sta_cur_vht_bw(sta);
300
301         /* If HT IE reported 3839 bytes only, stay with that size. */
302         if (sta->sta.max_amsdu_len == IEEE80211_MAX_MPDU_LEN_HT_3839)
303                 return;
304
305         switch (vht_cap->cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) {
306         case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
307                 sta->sta.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_11454;
308                 break;
309         case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
310                 sta->sta.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_7991;
311                 break;
312         case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895:
313         default:
314                 sta->sta.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_3895;
315                 break;
316         }
317 }
318
319 enum ieee80211_sta_rx_bandwidth ieee80211_sta_cap_rx_bw(struct sta_info *sta)
320 {
321         struct ieee80211_sta_vht_cap *vht_cap = &sta->sta.vht_cap;
322         u32 cap_width;
323
324         if (!vht_cap->vht_supported)
325                 return sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 ?
326                                 IEEE80211_STA_RX_BW_40 :
327                                 IEEE80211_STA_RX_BW_20;
328
329         cap_width = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
330
331         if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
332             cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
333                 return IEEE80211_STA_RX_BW_160;
334
335         return IEEE80211_STA_RX_BW_80;
336 }
337
338 enum nl80211_chan_width ieee80211_sta_cap_chan_bw(struct sta_info *sta)
339 {
340         struct ieee80211_sta_vht_cap *vht_cap = &sta->sta.vht_cap;
341         u32 cap_width;
342
343         if (!vht_cap->vht_supported) {
344                 if (!sta->sta.ht_cap.ht_supported)
345                         return NL80211_CHAN_WIDTH_20_NOHT;
346
347                 return sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 ?
348                                 NL80211_CHAN_WIDTH_40 : NL80211_CHAN_WIDTH_20;
349         }
350
351         cap_width = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
352
353         if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ)
354                 return NL80211_CHAN_WIDTH_160;
355         else if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
356                 return NL80211_CHAN_WIDTH_80P80;
357
358         return NL80211_CHAN_WIDTH_80;
359 }
360
361 enum nl80211_chan_width
362 ieee80211_sta_rx_bw_to_chan_width(struct sta_info *sta)
363 {
364         enum ieee80211_sta_rx_bandwidth cur_bw = sta->sta.bandwidth;
365         struct ieee80211_sta_vht_cap *vht_cap = &sta->sta.vht_cap;
366         u32 cap_width;
367
368         switch (cur_bw) {
369         case IEEE80211_STA_RX_BW_20:
370                 if (!sta->sta.ht_cap.ht_supported)
371                         return NL80211_CHAN_WIDTH_20_NOHT;
372                 else
373                         return NL80211_CHAN_WIDTH_20;
374         case IEEE80211_STA_RX_BW_40:
375                 return NL80211_CHAN_WIDTH_40;
376         case IEEE80211_STA_RX_BW_80:
377                 return NL80211_CHAN_WIDTH_80;
378         case IEEE80211_STA_RX_BW_160:
379                 cap_width =
380                         vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
381
382                 if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ)
383                         return NL80211_CHAN_WIDTH_160;
384
385                 return NL80211_CHAN_WIDTH_80P80;
386         default:
387                 return NL80211_CHAN_WIDTH_20;
388         }
389 }
390
391 enum ieee80211_sta_rx_bandwidth
392 ieee80211_chan_width_to_rx_bw(enum nl80211_chan_width width)
393 {
394         switch (width) {
395         case NL80211_CHAN_WIDTH_20_NOHT:
396         case NL80211_CHAN_WIDTH_20:
397                 return IEEE80211_STA_RX_BW_20;
398         case NL80211_CHAN_WIDTH_40:
399                 return IEEE80211_STA_RX_BW_40;
400         case NL80211_CHAN_WIDTH_80:
401                 return IEEE80211_STA_RX_BW_80;
402         case NL80211_CHAN_WIDTH_160:
403         case NL80211_CHAN_WIDTH_80P80:
404                 return IEEE80211_STA_RX_BW_160;
405         default:
406                 WARN_ON_ONCE(1);
407                 return IEEE80211_STA_RX_BW_20;
408         }
409 }
410
411 enum ieee80211_sta_rx_bandwidth ieee80211_sta_cur_vht_bw(struct sta_info *sta)
412 {
413         struct ieee80211_sub_if_data *sdata = sta->sdata;
414         enum ieee80211_sta_rx_bandwidth bw;
415         enum nl80211_chan_width bss_width = sdata->vif.bss_conf.chandef.width;
416
417         bw = ieee80211_sta_cap_rx_bw(sta);
418         bw = min(bw, sta->cur_max_bandwidth);
419
420         /* Don't consider AP's bandwidth for TDLS peers, section 11.23.1 of
421          * IEEE80211-2016 specification makes higher bandwidth operation
422          * possible on the TDLS link if the peers have wider bandwidth
423          * capability.
424          *
425          * However, in this case, and only if the TDLS peer is authorized,
426          * limit to the tdls_chandef so that the configuration here isn't
427          * wider than what's actually requested on the channel context.
428          */
429         if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
430             test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) &&
431             test_sta_flag(sta, WLAN_STA_AUTHORIZED) &&
432             sta->tdls_chandef.chan)
433                 bw = min(bw, ieee80211_chan_width_to_rx_bw(sta->tdls_chandef.width));
434         else
435                 bw = min(bw, ieee80211_chan_width_to_rx_bw(bss_width));
436
437         return bw;
438 }
439
440 void ieee80211_sta_set_rx_nss(struct sta_info *sta)
441 {
442         u8 ht_rx_nss = 0, vht_rx_nss = 0;
443
444         /* if we received a notification already don't overwrite it */
445         if (sta->sta.rx_nss)
446                 return;
447
448         if (sta->sta.ht_cap.ht_supported) {
449                 if (sta->sta.ht_cap.mcs.rx_mask[0])
450                         ht_rx_nss++;
451                 if (sta->sta.ht_cap.mcs.rx_mask[1])
452                         ht_rx_nss++;
453                 if (sta->sta.ht_cap.mcs.rx_mask[2])
454                         ht_rx_nss++;
455                 if (sta->sta.ht_cap.mcs.rx_mask[3])
456                         ht_rx_nss++;
457                 /* FIXME: consider rx_highest? */
458         }
459
460         if (sta->sta.vht_cap.vht_supported) {
461                 int i;
462                 u16 rx_mcs_map;
463
464                 rx_mcs_map = le16_to_cpu(sta->sta.vht_cap.vht_mcs.rx_mcs_map);
465
466                 for (i = 7; i >= 0; i--) {
467                         u8 mcs = (rx_mcs_map >> (2 * i)) & 3;
468
469                         if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
470                                 vht_rx_nss = i + 1;
471                                 break;
472                         }
473                 }
474                 /* FIXME: consider rx_highest? */
475         }
476
477         ht_rx_nss = max(ht_rx_nss, vht_rx_nss);
478         sta->sta.rx_nss = max_t(u8, 1, ht_rx_nss);
479 }
480
481 u32 __ieee80211_vht_handle_opmode(struct ieee80211_sub_if_data *sdata,
482                                   struct sta_info *sta, u8 opmode,
483                                   enum nl80211_band band)
484 {
485         enum ieee80211_sta_rx_bandwidth new_bw;
486         struct sta_opmode_info sta_opmode = {};
487         u32 changed = 0;
488         u8 nss;
489
490         /* ignore - no support for BF yet */
491         if (opmode & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)
492                 return 0;
493
494         nss = opmode & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
495         nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
496         nss += 1;
497
498         if (sta->sta.rx_nss != nss) {
499                 sta->sta.rx_nss = nss;
500                 sta_opmode.rx_nss = nss;
501                 changed |= IEEE80211_RC_NSS_CHANGED;
502                 sta_opmode.changed |= STA_OPMODE_N_SS_CHANGED;
503         }
504
505         switch (opmode & IEEE80211_OPMODE_NOTIF_CHANWIDTH_MASK) {
506         case IEEE80211_OPMODE_NOTIF_CHANWIDTH_20MHZ:
507                 sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_20;
508                 break;
509         case IEEE80211_OPMODE_NOTIF_CHANWIDTH_40MHZ:
510                 sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_40;
511                 break;
512         case IEEE80211_OPMODE_NOTIF_CHANWIDTH_80MHZ:
513                 sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_80;
514                 break;
515         case IEEE80211_OPMODE_NOTIF_CHANWIDTH_160MHZ:
516                 sta->cur_max_bandwidth = IEEE80211_STA_RX_BW_160;
517                 break;
518         }
519
520         new_bw = ieee80211_sta_cur_vht_bw(sta);
521         if (new_bw != sta->sta.bandwidth) {
522                 sta->sta.bandwidth = new_bw;
523                 sta_opmode.bw = ieee80211_sta_rx_bw_to_chan_width(sta);
524                 changed |= IEEE80211_RC_BW_CHANGED;
525                 sta_opmode.changed |= STA_OPMODE_MAX_BW_CHANGED;
526         }
527
528         if (sta_opmode.changed)
529                 cfg80211_sta_opmode_change_notify(sdata->dev, sta->addr,
530                                                   &sta_opmode, GFP_KERNEL);
531
532         return changed;
533 }
534
535 void ieee80211_process_mu_groups(struct ieee80211_sub_if_data *sdata,
536                                  struct ieee80211_mgmt *mgmt)
537 {
538         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
539
540         if (!sdata->vif.mu_mimo_owner)
541                 return;
542
543         if (!memcmp(mgmt->u.action.u.vht_group_notif.position,
544                     bss_conf->mu_group.position, WLAN_USER_POSITION_LEN) &&
545             !memcmp(mgmt->u.action.u.vht_group_notif.membership,
546                     bss_conf->mu_group.membership, WLAN_MEMBERSHIP_LEN))
547                 return;
548
549         memcpy(bss_conf->mu_group.membership,
550                mgmt->u.action.u.vht_group_notif.membership,
551                WLAN_MEMBERSHIP_LEN);
552         memcpy(bss_conf->mu_group.position,
553                mgmt->u.action.u.vht_group_notif.position,
554                WLAN_USER_POSITION_LEN);
555
556         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_MU_GROUPS);
557 }
558
559 void ieee80211_update_mu_groups(struct ieee80211_vif *vif,
560                                 const u8 *membership, const u8 *position)
561 {
562         struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
563
564         if (WARN_ON_ONCE(!vif->mu_mimo_owner))
565                 return;
566
567         memcpy(bss_conf->mu_group.membership, membership, WLAN_MEMBERSHIP_LEN);
568         memcpy(bss_conf->mu_group.position, position, WLAN_USER_POSITION_LEN);
569 }
570 EXPORT_SYMBOL_GPL(ieee80211_update_mu_groups);
571
572 void ieee80211_vht_handle_opmode(struct ieee80211_sub_if_data *sdata,
573                                  struct sta_info *sta, u8 opmode,
574                                  enum nl80211_band band)
575 {
576         struct ieee80211_local *local = sdata->local;
577         struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
578
579         u32 changed = __ieee80211_vht_handle_opmode(sdata, sta, opmode, band);
580
581         if (changed > 0) {
582                 ieee80211_recalc_min_chandef(sdata);
583                 rate_control_rate_update(local, sband, sta, changed);
584         }
585 }
586
587 void ieee80211_get_vht_mask_from_cap(__le16 vht_cap,
588                                      u16 vht_mask[NL80211_VHT_NSS_MAX])
589 {
590         int i;
591         u16 mask, cap = le16_to_cpu(vht_cap);
592
593         for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
594                 mask = (cap >> i * 2) & IEEE80211_VHT_MCS_NOT_SUPPORTED;
595                 switch (mask) {
596                 case IEEE80211_VHT_MCS_SUPPORT_0_7:
597                         vht_mask[i] = 0x00FF;
598                         break;
599                 case IEEE80211_VHT_MCS_SUPPORT_0_8:
600                         vht_mask[i] = 0x01FF;
601                         break;
602                 case IEEE80211_VHT_MCS_SUPPORT_0_9:
603                         vht_mask[i] = 0x03FF;
604                         break;
605                 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
606                 default:
607                         vht_mask[i] = 0;
608                         break;
609                 }
610         }
611 }