eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

band.c (42062B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2021  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #include <stdbool.h>
     24 #include <stdint.h>
     25 #include <errno.h>
     26 
     27 #include <ell/ell.h>
     28 
     29 #include "ell/useful.h"
     30 
     31 #include "src/band.h"
     32 #include "src/netdev.h"
     33 
     34 void band_free(struct band *band)
     35 {
     36 	if (band->he_capabilities)
     37 		l_queue_destroy(band->he_capabilities, l_free);
     38 
     39 	l_free(band->freq_attrs);
     40 
     41 	l_free(band);
     42 }
     43 
     44 /*
     45  * Rates are stored as they are encoded in the Supported Rates IE.
     46  * This data was taken from 802.11 Section 17.3.10.2 Table 17-18 and
     47  * Table 17-4. Together we have minimum RSSI required for a given data rate.
     48  */
     49 static const struct {
     50 	int32_t rssi;
     51 	uint8_t rate;
     52 } rate_rssi_map[] = {
     53 	{ -90, 2 },  /* Make something up for 11b rates */
     54 	{ -88, 4 },
     55 	{ -86, 11 },
     56 	{ -84, 22 },
     57 	{ -82, 12 },
     58 	{ -81, 18 },
     59 	{ -79, 24 },
     60 	{ -77, 36 },
     61 	{ -74, 48 },
     62 	{ -70, 72 },
     63 	{ -66, 96 },
     64 	{ -65, 108 },
     65 };
     66 
     67 static bool peer_supports_rate(const uint8_t *rates, uint8_t rate)
     68 {
     69 	int i;
     70 
     71 	if (rates && rates[1]) {
     72 		for (i = 0; i < rates[1]; i++) {
     73 			uint8_t r = rates[i + 2] & 0x7f;
     74 
     75 			if (r == rate)
     76 				return true;
     77 		}
     78 	}
     79 
     80 	return false;
     81 }
     82 
     83 int band_estimate_nonht_rate(const struct band *band,
     84 				const uint8_t *supported_rates,
     85 				const uint8_t *ext_supported_rates,
     86 				int32_t rssi, uint64_t *out_data_rate)
     87 {
     88 	int nrates = L_ARRAY_SIZE(rate_rssi_map);
     89 	uint8_t max_rate = 0;
     90 	int i;
     91 
     92 	if (!supported_rates && !ext_supported_rates)
     93 		return -ENOTSUP;
     94 
     95 	/*
     96 	 * Start at the back of the array.  Rates are generally given in
     97 	 * ascending order, starting at 11b rates, then 11g rates.  More often
     98 	 * than not we'll pick the highest rate and avoid unneeded processing
     99 	 */
    100 	for (i = band->supported_rates_len - 1; i >= 0; i--) {
    101 		uint8_t rate = band->supported_rates[i];
    102 		int j;
    103 
    104 		if (max_rate >= rate)
    105 			continue;
    106 
    107 		/* Can this rate be used at the peer's RSSI? */
    108 		for (j = 0; j < nrates; j++)
    109 			if (rate_rssi_map[j].rate == rate)
    110 				break;
    111 
    112 		if (j == nrates)
    113 			continue;
    114 
    115 		if (rssi < rate_rssi_map[j].rssi)
    116 			continue;
    117 
    118 		if (peer_supports_rate(supported_rates, rate) ||
    119 				peer_supports_rate(ext_supported_rates, rate))
    120 			max_rate = rate;
    121 	}
    122 
    123 	if (!max_rate)
    124 		return -ENETUNREACH;
    125 
    126 	*out_data_rate = max_rate * 500000;
    127 	return 0;
    128 }
    129 
    130 /*
    131  * Base RSSI values for 20MHz (HT, VHT and HE) channel. These values can be
    132  * used to calculate the minimum RSSI values for all other channel widths. HT
    133  * MCS indexes are grouped into ranges of 8 (per spatial stream), VHT in groups
    134  * of 10 and HE in groups of 12. This just means HT will not use the last four
    135  * index's of this array, and VHT won't use the last two.
    136  *
    137  * Note: The values here are not based on anything from 802.11 but data
    138  *       found elsewhere online (presumably from testing, we hope). The two
    139  *       indexes for HE (MCS 11/12) are not based on any data, but just
    140  *       increased by 3dB compared to the previous value. We consider this good
    141  *       enough for its purpose to estimate the date rate for network/BSS
    142  *       preference.
    143  */
    144 static const int32_t ht_vht_he_base_rssi[] = {
    145 	-82, -79, -77, -74, -70, -66, -65, -64, -59, -57, -54, -51
    146 };
    147 
    148 /*
    149  * Data Rate for HT/VHT is obtained according to this formula:
    150  * Nsd * Nbpscs * R * Nss / (Tdft + Tgi)
    151  *
    152  * Where Nsd is [52, 108, 234, 468] for 20/40/80/160 Mhz respectively
    153  * Nbpscs is [1, 2, 4, 6, 8] for BPSK/QPSK/16QAM/64QAM/256QAM
    154  * R is [1/2, 2/3, 3/4, 5/6] depending on the MCS index
    155  * Nss is the number of spatial streams
    156  * Tdft = 3.2 us
    157  * Tgi = Long/Short GI of 0.8/0.4 us
    158  *
    159  * Short GI rate can be easily obtained by multiplying by (10 / 9)
    160  *
    161  * The table was pre-computed using the following python snippet:
    162  * rfactors = [ 1/2, 1/2, 3/4, 1/2, 3/4, 2/3, 3/4, 5/6, 3/4, 5/6 ]
    163  * nbpscs = [1, 2, 2, 4, 4, 6, 6, 6, 8, 8 ]
    164  * nsds = [52, 108, 234, 468]
    165  *
    166  * for nsd in nsds:
    167  *	rates = []
    168  *	for i in xrange(0, 10):
    169  *		data_rate = (nsd * rfactors[i] * nbpscs[i]) / 0.004
    170  *		rates.append(int(data_rate) * 1000)
    171  *	print('rates for nsd: ' + nsd + ': ' + rates)
    172  */
    173 
    174 static const uint64_t ht_vht_rates[4][10] = {
    175 	[OFDM_CHANNEL_WIDTH_20MHZ] = {
    176 		6500000ULL, 13000000ULL, 19500000ULL, 26000000ULL,
    177 		39000000ULL, 52000000ULL, 58500000ULL, 65000000ULL,
    178 		78000000ULL, 86666000ULL },
    179 	[OFDM_CHANNEL_WIDTH_40MHZ] = {
    180 		13500000ULL, 27000000ULL, 40500000ULL, 54000000ULL,
    181 		81000000ULL, 108000000ULL, 121500000ULL, 135000000ULL,
    182 		162000000ULL, 180000000ULL, },
    183 	[OFDM_CHANNEL_WIDTH_80MHZ] = {
    184 		29250000ULL, 58500000ULL, 87750000ULL, 117000000ULL,
    185 		175500000ULL, 234000000ULL, 263250000ULL, 292500000ULL,
    186 		351000000ULL, 390000000ULL, },
    187 	[OFDM_CHANNEL_WIDTH_160MHZ] = {
    188 		58500000ULL, 117000000ULL, 175500000ULL, 234000000ULL,
    189 		351000000ULL, 468000000ULL, 526500000ULL, 585000000ULL,
    190 		702000000ULL, 780000000ULL,
    191 	}
    192 };
    193 
    194 /*
    195  * Both HT and VHT rates are calculated in the same fashion. The only difference
    196  * is a relative MCS index is used for HT since, for each NSS, the formula
    197  * is the same with relative index's. This is why this is called with index % 8
    198  * for HT, but not VHT.
    199  */
    200 bool band_ofdm_rate(uint8_t index, enum ofdm_channel_width width,
    201 			int32_t rssi, uint8_t nss, bool sgi,
    202 			uint64_t *data_rate)
    203 {
    204 	uint64_t rate;
    205 	int32_t width_adjust = width * 3;
    206 
    207 	if (rssi < ht_vht_he_base_rssi[index] + width_adjust)
    208 		return false;
    209 
    210 	rate = ht_vht_rates[width][index];
    211 
    212 	if (sgi)
    213 		rate = rate / 9 * 10;
    214 
    215 	rate *= nss;
    216 
    217 	*data_rate = rate;
    218 	return true;
    219 }
    220 
    221 static bool find_best_mcs_ht(const struct band *band,
    222 				const uint8_t *tx_mcs_set,
    223 				uint8_t max_mcs, enum ofdm_channel_width width,
    224 				int32_t rssi, bool sgi,
    225 				uint64_t *out_data_rate)
    226 {
    227 	int i;
    228 
    229 	/*
    230 	 * TODO: Support MCS values 32 - 76
    231 	 *
    232 	 * The MCS values > 31 use an unequal modulation, and the number of
    233 	 * supported MCS indexes per NSS differs.  We do not consider them
    234 	 * here for now to keep things simple(r).
    235 	 */
    236 	for (i = max_mcs; i >= 0; i--) {
    237 		if (!test_bit(band->ht_mcs_set, i))
    238 			continue;
    239 
    240 		if (!test_bit(tx_mcs_set, i))
    241 			continue;
    242 
    243 		if (band_ofdm_rate(i % 8, width, rssi,
    244 					(i / 8) + 1, sgi, out_data_rate))
    245 			return true;
    246 	}
    247 
    248 	return false;
    249 }
    250 
    251 int band_estimate_ht_rx_rate(const struct band *band,
    252 				const uint8_t *htc, const uint8_t *hto,
    253 				int32_t rssi, uint64_t *out_data_rate)
    254 {
    255 	uint8_t channel_offset;
    256 	int max_mcs = 31;
    257 	bool sgi;
    258 	uint8_t unequal_tx_mcs_set[16];
    259 	const uint8_t *tx_mcs_set;
    260 
    261 	if (!band->ht_supported)
    262 		return -ENOTSUP;
    263 
    264 	if (!htc || !hto)
    265 		return -ENOTSUP;
    266 
    267 	memset(unequal_tx_mcs_set, 0, sizeof(unequal_tx_mcs_set));
    268 
    269 	tx_mcs_set = htc + 5;
    270 
    271 	/*
    272 	 * Check 'Tx MCS Set Defined' at bit 96 and 'Tx MCS Set Unequal' at
    273 	 * bit 97 of the Supported MCS Set field.  Also extract 'Tx Maximum
    274 	 * Number of Spatial Streams Supported' field at bits 98 and 99.
    275 	 *
    276 	 * Note 44 on page 1662 of 802.11-2016 states:
    277 	 * "How a non-AP STA determines an AP's HT MCS transmission support,
    278 	 * if the Tx MCS Set subfield in the HT Capabilities element
    279 	 * advertised by the AP is equal to 0 or if he Tx Rx MCS Set Not Equal
    280 	 * subfield in that element is equal to 1, is implementation dependent.
    281 	 * The non-AP STA might conservatively use the basic HT-MCS set, or it
    282 	 * might use knowledge of past transmissions by the AP, or it might
    283 	 * use other means.
    284 	 */
    285 	if (test_bit(tx_mcs_set, 96)) {
    286 		if (test_bit(tx_mcs_set, 97)) {
    287 			uint8_t max_nss = bit_field(tx_mcs_set[12], 2, 2);
    288 
    289 			max_mcs = max_nss * 4 + 7;
    290 
    291 			/*
    292 			 * For purposes of finding the best MCS below, assume
    293 			 * the AP can send any MCS up to max_nss (i.e 0-7 for
    294 			 * 1 nss, 0-15 for 2 nss, 0-23 for 3 nss, 0-31 for 4
    295 			 */
    296 			memset(unequal_tx_mcs_set, 0xff, max_nss + 1);
    297 			tx_mcs_set = unequal_tx_mcs_set;
    298 		}
    299 	} else
    300 		max_mcs = 7;
    301 
    302 	/* Test for 40 Mhz operation */
    303 	channel_offset = bit_field(hto[3], 0, 2);
    304 	if (test_bit(hto + 3, 2) &&
    305 			(channel_offset == 1 || channel_offset == 3)) {
    306 		sgi = test_bit(band->ht_capabilities, 6) &&
    307 						test_bit(htc + 2, 6);
    308 
    309 		if (find_best_mcs_ht(band, tx_mcs_set, max_mcs,
    310 					OFDM_CHANNEL_WIDTH_40MHZ,
    311 					rssi, sgi, out_data_rate))
    312 			return 0;
    313 	}
    314 
    315 	sgi = test_bit(band->ht_capabilities, 5) && test_bit(htc + 2, 5);
    316 
    317 	if (find_best_mcs_ht(band, tx_mcs_set, max_mcs,
    318 				OFDM_CHANNEL_WIDTH_20MHZ,
    319 				rssi, sgi, out_data_rate))
    320 		return 0;
    321 
    322 	return -ENETUNREACH;
    323 }
    324 
    325 static bool find_best_mcs_vht(uint8_t max_index, enum ofdm_channel_width width,
    326 				int32_t rssi, uint8_t nss, bool sgi,
    327 				uint64_t *out_data_rate)
    328 {
    329 	int i;
    330 
    331 	/*
    332 	 * Iterate over all available MCS indexes to find the best one
    333 	 * we can use.  Note that band_ofdm_rate() will return false if a
    334 	 * given combination cannot be used due to rssi being too low.
    335 	 *
    336 	 * Also, Certain MCS/Width/NSS combinations are not valid,
    337 	 * refer to IEEE 802.11-2016 Section 21.5 for more details
    338 	 */
    339 
    340 	for (i = max_index; i >= 0; i--)
    341 		if (band_ofdm_rate(i, width, rssi, nss, sgi, out_data_rate))
    342 			return true;
    343 
    344 	return false;
    345 }
    346 
    347 static bool find_best_mcs_nss(const uint8_t *rx_map, const uint8_t *tx_map,
    348 				uint8_t value0, uint8_t value1, uint8_t value2,
    349 				uint32_t *mcs_out, uint32_t *nss_out)
    350 {
    351 	uint32_t nss = 0;
    352 	uint32_t max_mcs = 0;
    353 	int bitoffset;
    354 
    355 	for (bitoffset = 14; bitoffset >= 0; bitoffset -= 2) {
    356 		uint8_t rx_val = bit_field(rx_map[bitoffset / 8],
    357 							bitoffset % 8, 2);
    358 		uint8_t tx_val = bit_field(tx_map[bitoffset / 8],
    359 							bitoffset % 8, 2);
    360 
    361 		/*
    362 		 * 0 indicates support for MCS 0 - value0
    363 		 * 1 indicates support for MCS 0 - value1
    364 		 * 2 indicates support for MCS 0 - value2
    365 		 * 3 indicates no support
    366 		 */
    367 
    368 		if (rx_val == 3 || tx_val == 3)
    369 			continue;
    370 
    371 		/* rx_val/tx_val tells us which value# to use */
    372 		max_mcs = minsize(rx_val, tx_val);
    373 		switch (max_mcs) {
    374 		case 0:
    375 			max_mcs = value0;
    376 			break;
    377 		case 1:
    378 			max_mcs = value1;
    379 			break;
    380 		case 2:
    381 			max_mcs = value2;
    382 			break;
    383 		}
    384 
    385 		nss = bitoffset / 2 + 1;
    386 		break;
    387 	}
    388 
    389 	if (!nss)
    390 		return false;
    391 
    392 	*nss_out = nss;
    393 	*mcs_out = max_mcs;
    394 
    395 	return true;
    396 }
    397 /*
    398  * IEEE 802.11 - Table 9-250
    399  *
    400  * For simplicity, we are ignoring the Extended BSS BW support, per NOTE 11:
    401  *
    402  * NOTE 11-A receiving STA in which dot11VHTExtendedNSSCapable is false will
    403  * ignore the Extended NSS BW Support subfield and effectively evaluate this
    404  * table only at the entries where Extended NSS BW Support is 0.
    405  *
    406  * This also allows us to group the 160/80+80 widths together, since they are
    407  * the same when Extended NSS BW is zero.
    408  */
    409 int band_estimate_vht_rx_rate(const struct band *band,
    410 				const uint8_t *vhtc, const uint8_t *vhto,
    411 				const uint8_t *htc, const uint8_t *hto,
    412 				int32_t rssi, uint64_t *out_data_rate)
    413 {
    414 	uint32_t nss = 0;
    415 	uint32_t max_mcs = 7; /* MCS 0-7 for NSS:1 is always supported */
    416 	const uint8_t *rx_mcs_map;
    417 	const uint8_t *tx_mcs_map;
    418 	uint8_t chan_width;
    419 	uint8_t channel_offset;
    420 	bool sgi;
    421 
    422 	if (!band->vht_supported || !band->ht_supported)
    423 		return -ENOTSUP;
    424 
    425 	if (!vhtc || !vhto || !htc || !hto)
    426 		return -ENOTSUP;
    427 
    428 	if (vhto[2] > 3)
    429 		return -EBADMSG;
    430 
    431 	/*
    432 	 * Find the highest NSS/MCS index combination.  Since this is used by
    433 	 * STAs, we try to estimate our 'download' speed from the AP/peer.
    434 	 * Hence we look at the TX MCS map of the peer and our own RX MCS map
    435 	 * to find an overlapping combination that works
    436 	 */
    437 	rx_mcs_map = band->vht_mcs_set;
    438 	tx_mcs_map = vhtc + 2 + 8;
    439 
    440 	if (!find_best_mcs_nss(rx_mcs_map, tx_mcs_map, 7, 8, 9, &max_mcs, &nss))
    441 		return -EBADMSG;
    442 
    443 	/*
    444 	 * There is no way to know whether a peer would send us packets using
    445 	 * the short guard interval (SGI.)  SGI capability is only used to
    446 	 * indicate whether the peer can accept packets that we send this way.
    447 	 * Here we make the assumption that if the peer has the capability to
    448 	 * accept packets using SGI and we have the capability to do so, then
    449 	 * SGI will be used
    450 	 *
    451 	 * Also, we assume that the highest bandwidth will result in the
    452 	 * highest rate for any given rssi.  Even accounting for invalid
    453 	 * MCS/Width/NSS combinations, the higher channel width results
    454 	 * in better data rate at [mcs index - 2] compared to [mcs index] of
    455 	 * a next lower bandwidth.
    456 	 */
    457 
    458 	/* See if 160 Mhz operation is available */
    459 	chan_width = bit_field(band->vht_capabilities[0], 2, 2);
    460 	if (chan_width != 1 && chan_width != 2)
    461 		goto try_vht80;
    462 
    463 	/*
    464 	 * Channel Width is set to 2 or 3, or 1 and
    465 	 * channel center frequency segment 1 is non-zero
    466 	 */
    467 	if (vhto[2] == 2 || vhto[2] == 3 || (vhto[2] == 1 && vhto[4])) {
    468 		sgi = test_bit(band->vht_capabilities, 6) &&
    469 						test_bit(vhtc + 2, 6);
    470 
    471 		if (find_best_mcs_vht(max_mcs, OFDM_CHANNEL_WIDTH_160MHZ,
    472 					rssi, nss, sgi, out_data_rate))
    473 			return 0;
    474 	}
    475 
    476 try_vht80:
    477 	if (vhto[2] == 1) {
    478 		sgi = test_bit(band->vht_capabilities, 5) &&
    479 						test_bit(vhtc + 2, 5);
    480 
    481 		if (find_best_mcs_vht(max_mcs, OFDM_CHANNEL_WIDTH_80MHZ,
    482 					rssi, nss, sgi, out_data_rate))
    483 			return 0;
    484 	} /* Otherwise, assume 20/40 Operation */
    485 
    486 	channel_offset = bit_field(hto[3], 0, 2);
    487 
    488 	/* Test for 40 Mhz operation */
    489 	if (test_bit(hto + 3, 2) &&
    490 			(channel_offset == 1 || channel_offset == 3)) {
    491 		sgi = test_bit(band->ht_capabilities, 6) &&
    492 						test_bit(htc + 2, 6);
    493 
    494 		if (find_best_mcs_vht(max_mcs, OFDM_CHANNEL_WIDTH_40MHZ,
    495 					rssi, nss, sgi, out_data_rate))
    496 			return 0;
    497 	}
    498 
    499 	sgi = test_bit(band->ht_capabilities, 5) && test_bit(htc + 2, 5);
    500 
    501 	if (find_best_mcs_vht(max_mcs, OFDM_CHANNEL_WIDTH_20MHZ,
    502 				rssi, nss, sgi, out_data_rate))
    503 		return 0;
    504 
    505 	return -ENETUNREACH;
    506 }
    507 
    508 /*
    509  * Data Rate for HE is much the same as HT/VHT but some additional MCS indexes
    510  * were added. This mean rfactors, and nbpscs will contain two additional
    511  * values:
    512  *
    513  * rfactors.extend([3/4, 5/6])
    514  * nbpscs.extend([10, 10])
    515  *
    516  * The guard interval also differs:
    517  *
    518  * Tdft = 12.8us
    519  * Tgi = 0.8, 1.6 or 2.3us
    520  *
    521  * The Nsd values for HE are:
    522  *
    523  * Nsd = [234, 468, 980, 1960]
    524  *
    525  * The formula is identical to HT/VHT:
    526  *
    527  * Nsd * Nbpscs * R * Nss / (Tdft + Tgi)
    528  *
    529  * Note: The table below assumes a 0.8us GI. There isn't any way to know what
    530  *       GI will be used for an actual connection, so assume the best.
    531  */
    532 static uint64_t he_rates[4][12] = {
    533 	[OFDM_CHANNEL_WIDTH_20MHZ] = {
    534 		8600000ULL, 17200000ULL, 25800000ULL, 34400000ULL,
    535 		51600000ULL, 68800000ULL, 77400000ULL, 86000000ULL,
    536 		103200000ULL, 114700000ULL, 129000000ULL, 143300000ULL,
    537 	},
    538 	[OFDM_CHANNEL_WIDTH_40MHZ] = {
    539 		17200000ULL, 34400000ULL, 51600000ULL, 68800000ULL,
    540 		103200000ULL, 137600000ULL, 154900000ULL, 172000000ULL,
    541 		206500000ULL, 229400000ULL, 258000000ULL, 286800000ULL,
    542 	},
    543 	[OFDM_CHANNEL_WIDTH_80MHZ] = {
    544 		36000000ULL, 72000000ULL, 108000000ULL, 144100000ULL,
    545 		216200000ULL, 288200000ULL, 324300000ULL, 360300000ULL,
    546 		432400000ULL, 480400000ULL, 540400000ULL, 600500000ULL,
    547 	},
    548 	[OFDM_CHANNEL_WIDTH_160MHZ] = {
    549 		72000000ULL, 144100000ULL, 216200000ULL, 288200000ULL,
    550 		432400000ULL, 576500000ULL, 648500000ULL, 720600000ULL,
    551 		864700000ULL, 960800000ULL, 1080900000ULL, 1201000000ULL,
    552 	},
    553 };
    554 
    555 static bool band_he_rate(uint8_t index, enum ofdm_channel_width width,
    556 			int32_t rssi, uint8_t nss, uint64_t *data_rate)
    557 {
    558 	uint64_t rate;
    559 	int32_t width_adjust;
    560 
    561 	width_adjust = width * 3;
    562 
    563 	if (rssi < ht_vht_he_base_rssi[index] + width_adjust)
    564 		return false;
    565 
    566 	rate = he_rates[width][index];
    567 
    568 	rate *= nss;
    569 
    570 	*data_rate = rate;
    571 	return true;
    572 }
    573 
    574 static bool find_rate_he(const uint8_t *rx_map, const uint8_t *tx_map,
    575 				enum ofdm_channel_width width, int32_t rssi,
    576 				uint64_t *out_data_rate)
    577 {
    578 	uint32_t nss;
    579 	uint32_t max_mcs;
    580 	int i;
    581 
    582 	if (!find_best_mcs_nss(rx_map, tx_map, 7, 9, 11,
    583 				&max_mcs, &nss))
    584 		return false;
    585 
    586 	for (i = max_mcs; i >= 0; i--)
    587 		if (band_he_rate(i, width, rssi, nss, out_data_rate))
    588 			return true;
    589 
    590 	return false;
    591 }
    592 
    593 /*
    594  * HE data rate is calculated based on 802.11ax - Section 27.5
    595  */
    596 int band_estimate_he_rx_rate(const struct band *band, const uint8_t *hec,
    597 				int32_t rssi, uint64_t *out_data_rate)
    598 {
    599 	enum ofdm_channel_width width = OFDM_CHANNEL_WIDTH_20MHZ;
    600 	int i;
    601 	const struct band_he_capabilities *he_cap = NULL;
    602 	const struct l_queue_entry *entry;
    603 	const uint8_t *rx_map;
    604 	const uint8_t *tx_map;
    605 	uint64_t rate = 0;
    606 	uint64_t new_rate = 0;
    607 	uint8_t width_set;
    608 
    609 	if (!hec || !band->he_capabilities)
    610 		return -ENOTSUP;
    611 
    612 	for (entry = l_queue_get_entries(band->he_capabilities);
    613 						entry; entry = entry->next) {
    614 		const struct band_he_capabilities *cap = entry->data;
    615 
    616 		/*
    617 		 * TODO: Station type is assumed here since it is the only
    618 		 *       consumer of these data rate estimation APIs. If this
    619 		 *       changes the iftype would need to be passed in.
    620 		 */
    621 		if (cap->iftypes & (1 << NETDEV_IFTYPE_STATION)) {
    622 			he_cap = cap;
    623 			break;
    624 		}
    625 	}
    626 
    627 	if (!he_cap)
    628 		return -ENOTSUP;
    629 
    630 	/* AND the width sets, giving the widths supported by both */
    631 	width_set = bit_field(he_cap->he_phy_capa[0], 1, 7) &
    632 				bit_field((hec + 6)[0], 1, 7);
    633 
    634 	/*
    635 	 * The HE-MCS maps are 17 bytes into the HE Capabilities IE, and
    636 	 * alternate RX/TX every 2 bytes. Start the TX map 17 + 2 bytes
    637 	 * into the MCS set. For each MCS set find the best data rate.
    638 	 */
    639 	rx_map = he_cap->he_mcs_set;
    640 	tx_map = hec + 19;
    641 
    642 	/*
    643 	 * 802.11ax Table 9-322b
    644 	 *
    645 	 * B3 indicates support for 80+80MHz MCS set
    646 	 */
    647 	if (test_bit(&width_set, 3)) {
    648 		if (find_rate_he(rx_map + 8, tx_map + 8,
    649 					OFDM_CHANNEL_WIDTH_160MHZ,
    650 					rssi, &new_rate))
    651 			rate = new_rate;
    652 	}
    653 
    654 	/* B2 indicates support for 160MHz MCS set */
    655 	if (test_bit(&width_set, 2)) {
    656 		if (find_rate_he(rx_map + 4, tx_map + 4,
    657 					OFDM_CHANNEL_WIDTH_160MHZ,
    658 					rssi, &new_rate) && new_rate > rate)
    659 			rate = new_rate;
    660 	}
    661 
    662 	/* B1 indicates support for 80MHz */
    663 	if (test_bit(&width_set, 1))
    664 		width = OFDM_CHANNEL_WIDTH_80MHZ;
    665 
    666 	/* B0 indicates support for 40MHz */
    667 	if (test_bit(&width_set, 0))
    668 		width = OFDM_CHANNEL_WIDTH_40MHZ;
    669 
    670 	/* <= 80MHz MCS set */
    671 	for (i = width; i >= OFDM_CHANNEL_WIDTH_20MHZ; i--) {
    672 		if (find_rate_he(rx_map, tx_map, i, rssi, &new_rate)) {
    673 			if (new_rate > rate)
    674 				rate = new_rate;
    675 
    676 			break;
    677 		}
    678 	}
    679 
    680 	if (!rate)
    681 		return -ENETUNREACH;
    682 
    683 	*out_data_rate = rate;
    684 
    685 	return 0;
    686 }
    687 
    688 static int band_channel_info_get_bandwidth(const struct band_chandef *info)
    689 {
    690 	switch (info->channel_width) {
    691 	case BAND_CHANDEF_WIDTH_20NOHT:
    692 	case BAND_CHANDEF_WIDTH_20:
    693 		return 20;
    694 	case BAND_CHANDEF_WIDTH_40:
    695 		return 40;
    696 	case BAND_CHANDEF_WIDTH_80:
    697 		return 80;
    698 	case BAND_CHANDEF_WIDTH_80P80:
    699 	case BAND_CHANDEF_WIDTH_160:
    700 		return 160;
    701 	default:
    702 		break;
    703 	}
    704 
    705 	return -ENOTSUP;
    706 }
    707 
    708 struct operating_class_info {
    709 	uint32_t starting_frequency;
    710 	uint32_t flags;
    711 	uint8_t channel_set[60];
    712 	uint8_t center_frequencies[30];
    713 	uint16_t channel_spacing;
    714 	uint8_t operating_class;
    715 };
    716 
    717 enum operating_class_flags {
    718 	PRIMARY_CHANNEL_UPPER = 0x1,
    719 	PRIMARY_CHANNEL_LOWER = 0x2,
    720 	PLUS80 = 0x4,
    721 };
    722 
    723 static const struct operating_class_info e4_operating_classes[] = {
    724 	{
    725 		.operating_class = 81,
    726 		.starting_frequency = 2407,
    727 		.channel_set = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 },
    728 		.channel_spacing = 20,
    729 	},
    730 	{
    731 		.operating_class = 82,
    732 		.starting_frequency = 2414,
    733 		.channel_set = { 14 },
    734 		.channel_spacing = 20,
    735 	},
    736 	{
    737 		.operating_class = 83,
    738 		.starting_frequency = 2407,
    739 		.channel_set = { 1, 2, 3, 4, 5, 6, 7, 8, 9 },
    740 		.channel_spacing = 40,
    741 		.flags = PRIMARY_CHANNEL_LOWER,
    742 	},
    743 	{
    744 		.operating_class = 84,
    745 		.starting_frequency = 2407,
    746 		.channel_set = { 5, 6, 7, 8, 9, 10, 11, 12, 13 },
    747 		.channel_spacing = 40,
    748 		.flags = PRIMARY_CHANNEL_UPPER,
    749 	},
    750 	{
    751 		.operating_class = 115,
    752 		.starting_frequency = 5000,
    753 		.channel_set = { 36, 40, 44, 48},
    754 		.channel_spacing = 20,
    755 	},
    756 	{
    757 		.operating_class = 116,
    758 		.starting_frequency = 5000,
    759 		.channel_set = { 36, 44 },
    760 		.channel_spacing = 40,
    761 		.flags = PRIMARY_CHANNEL_LOWER,
    762 	},
    763 	{
    764 		.operating_class = 117,
    765 		.starting_frequency = 5000,
    766 		.channel_set = { 40, 48 },
    767 		.channel_spacing = 40,
    768 		.flags = PRIMARY_CHANNEL_UPPER,
    769 	},
    770 	{
    771 		.operating_class = 118,
    772 		.starting_frequency = 5000,
    773 		.channel_set = { 52, 56, 60, 64},
    774 		.channel_spacing = 20,
    775 	},
    776 	{
    777 		.operating_class = 119,
    778 		.starting_frequency = 5000,
    779 		.channel_set = { 52, 60 },
    780 		.channel_spacing = 40,
    781 		.flags = PRIMARY_CHANNEL_LOWER,
    782 	},
    783 	{
    784 		.operating_class = 120,
    785 		.starting_frequency = 5000,
    786 		.channel_set = { 56, 64 },
    787 		.channel_spacing = 40,
    788 		.flags = PRIMARY_CHANNEL_UPPER,
    789 	},
    790 	{
    791 		.operating_class = 121,
    792 		.starting_frequency = 5000,
    793 		.channel_set = { 100, 104, 108, 112, 116, 120,
    794 					124, 128, 132, 136, 140, 144 },
    795 		.channel_spacing = 20,
    796 	},
    797 	{
    798 		.operating_class = 122,
    799 		.starting_frequency = 5000,
    800 		.channel_set = { 100, 108, 116, 124, 132, 140},
    801 		.channel_spacing = 40,
    802 		.flags = PRIMARY_CHANNEL_LOWER,
    803 	},
    804 	{
    805 		.operating_class = 123,
    806 		.starting_frequency = 5000,
    807 		.channel_set = { 104, 112, 120, 128, 136, 144 },
    808 		.channel_spacing = 40,
    809 		.flags = PRIMARY_CHANNEL_UPPER,
    810 	},
    811 	{
    812 		.operating_class = 124,
    813 		.starting_frequency = 5000,
    814 		.channel_set = { 149, 153, 157, 161 },
    815 		.channel_spacing = 20,
    816 	},
    817 	{
    818 		.operating_class = 125,
    819 		.starting_frequency = 5000,
    820 		.channel_set = { 149, 153, 157, 161, 165, 169, 173, 177 },
    821 		.channel_spacing = 20,
    822 	},
    823 	{
    824 		.operating_class = 126,
    825 		.starting_frequency = 5000,
    826 		.channel_set = { 149, 157, 165, 173 },
    827 		.channel_spacing = 40,
    828 		.flags = PRIMARY_CHANNEL_LOWER,
    829 	},
    830 	{
    831 		.operating_class = 127,
    832 		.starting_frequency = 5000,
    833 		.channel_set = { 153, 161, 169, 177 },
    834 		.channel_spacing = 40,
    835 		.flags = PRIMARY_CHANNEL_UPPER,
    836 	},
    837 	{
    838 		.operating_class = 128,
    839 		.starting_frequency = 5000,
    840 		.channel_spacing = 80,
    841 		.center_frequencies = { 42, 58, 106, 122, 138, 155, 171 },
    842 	},
    843 	{
    844 		.operating_class = 129,
    845 		.starting_frequency = 5000,
    846 		.channel_spacing = 160,
    847 		.center_frequencies = { 50, 114, 163 },
    848 	},
    849 	{
    850 		.operating_class = 130,
    851 		.starting_frequency = 5000,
    852 		.channel_spacing = 80,
    853 		.center_frequencies = { 42, 58, 106, 122, 138, 155, 171 },
    854 		.flags = PLUS80,
    855 	},
    856 	{
    857 		.operating_class = 131,
    858 		.starting_frequency = 5950,
    859 		.channel_spacing = 20,
    860 		.channel_set = { 1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45,
    861 				49, 53, 57, 61, 65, 69, 73, 77, 81, 85, 89, 93,
    862 				97, 101, 105, 109, 113, 117, 121, 125, 129, 133,
    863 				137, 141, 145, 149, 153, 157, 161, 165, 169,
    864 				173, 177, 181, 185, 189, 193, 197, 201, 205,
    865 				209, 213, 217, 221, 225, 229, 233 },
    866 	},
    867 	{
    868 		.operating_class = 132,
    869 		.starting_frequency = 5950,
    870 		.channel_spacing = 40,
    871 		.center_frequencies = { 3, 11, 19, 27, 35, 43, 51, 59, 67, 75,
    872 					83, 91, 99, 107, 115, 123, 131, 139,
    873 					147, 155, 163, 171, 179, 187, 195, 203,
    874 					211, 219, 227 },
    875 	},
    876 	{
    877 		.operating_class = 133,
    878 		.starting_frequency = 5950,
    879 		.channel_spacing = 80,
    880 		.center_frequencies = { 7, 23, 39, 55, 71, 87, 103, 119, 135,
    881 					151, 167, 183, 199, 215 },
    882 	},
    883 	{
    884 		.operating_class = 134,
    885 		.starting_frequency = 5950,
    886 		.channel_spacing = 160,
    887 		.center_frequencies = { 15, 47, 79, 111, 143, 175, 207 },
    888 	},
    889 	{
    890 		.operating_class = 135,
    891 		.starting_frequency = 5950,
    892 		.channel_spacing = 80,
    893 		.center_frequencies = { 7, 23, 39, 55, 71, 87, 102, 119, 135,
    894 					151, 167, 183, 199, 215 },
    895 		.flags = PLUS80,
    896 	},
    897 	{
    898 		.operating_class = 136,
    899 		.starting_frequency = 5925,
    900 		.channel_spacing = 20,
    901 		.center_frequencies = { 2 },
    902 	}
    903 };
    904 
    905 static const struct operating_class_info *e4_find_opclass(uint32_t opclass)
    906 {
    907 	unsigned int i;
    908 
    909 	for (i = 0; i < L_ARRAY_SIZE(e4_operating_classes); i++) {
    910 		if (e4_operating_classes[i].operating_class != opclass)
    911 			continue;
    912 
    913 		return &e4_operating_classes[i];
    914 	}
    915 
    916 	return NULL;
    917 }
    918 
    919 static int e4_channel_to_frequency(const struct operating_class_info *info,
    920 					uint32_t channel)
    921 {
    922 	unsigned int i;
    923 	unsigned int offset;
    924 
    925 	for (i = 0; info->channel_set[i] &&
    926 				i < L_ARRAY_SIZE(info->channel_set); i++) {
    927 		if (info->channel_set[i] != channel)
    928 			continue;
    929 
    930 		return channel * 5 + info->starting_frequency;
    931 	}
    932 
    933 	/*
    934 	 * Only classes in Table E4 with center frequencies are 128-130,
    935 	 * which use 20 Mhz wide channels.  Since E4 gives a center frequency,
    936 	 * calculate the channel offset based on channel spacing.
    937 	 *
    938 	 * Typically +/- 6 for 80 Mhz channels and +/- 14 for 160 Mhz channels
    939 	 */
    940 	offset = (info->channel_spacing - 20) / 5 / 2;
    941 
    942 	/*
    943 	 * Check that the channel is in the frequency range given by one of
    944 	 * the center frequencies listed for the operating class.  The channel
    945 	 * must be a valid channel for a lower operating class and spaced
    946 	 * 20 mhz apart
    947 	 */
    948 	for (i = 0; info->center_frequencies[i] &&
    949 			i < L_ARRAY_SIZE(info->center_frequencies); i++) {
    950 
    951 		unsigned int upper = info->center_frequencies[i] + offset;
    952 		unsigned int j = info->center_frequencies[i] - offset;
    953 
    954 		while (j <= upper && channel >= j) {
    955 			if (channel == j)
    956 				return channel * 5 + info->starting_frequency;
    957 
    958 			j += 4;
    959 		}
    960 	}
    961 
    962 	return -EINVAL;
    963 }
    964 
    965 static int e4_frequency_to_channel(const struct operating_class_info *info,
    966 					uint32_t frequency)
    967 {
    968 	return (frequency - info->starting_frequency) / 5;
    969 }
    970 
    971 static int e4_has_frequency(const struct operating_class_info *info,
    972 				uint32_t frequency)
    973 {
    974 	unsigned int i;
    975 	unsigned int channel = e4_frequency_to_channel(info, frequency);
    976 
    977 	for (i = 0; info->channel_set[i] &&
    978 				i < L_ARRAY_SIZE(info->channel_set); i++) {
    979 		if (info->channel_set[i] != channel)
    980 			continue;
    981 
    982 		return 0;
    983 	}
    984 
    985 	return -ENOENT;
    986 }
    987 
    988 static int e4_has_ccfi(const struct operating_class_info *info,
    989 				uint32_t center_frequency)
    990 {
    991 	unsigned int i;
    992 	unsigned int ccfi = e4_frequency_to_channel(info, center_frequency);
    993 
    994 	for (i = 0; info->center_frequencies[i] &&
    995 			i < L_ARRAY_SIZE(info->center_frequencies); i++) {
    996 		if (info->center_frequencies[i] != ccfi)
    997 			continue;
    998 
    999 		return 0;
   1000 	}
   1001 
   1002 	return -ENOENT;
   1003 }
   1004 
   1005 static int e4_class_matches(const struct operating_class_info *info,
   1006 					const struct band_chandef *chandef)
   1007 {
   1008 	int own_bandwidth = band_channel_info_get_bandwidth(chandef);
   1009 	int r;
   1010 
   1011 	if (own_bandwidth < 0)
   1012 		return own_bandwidth;
   1013 
   1014 	switch (chandef->channel_width) {
   1015 	case BAND_CHANDEF_WIDTH_20NOHT:
   1016 	case BAND_CHANDEF_WIDTH_20:
   1017 	case BAND_CHANDEF_WIDTH_40:
   1018 		if (own_bandwidth != info->channel_spacing)
   1019 			return -ENOENT;
   1020 
   1021 		if (own_bandwidth == 40) {
   1022 			uint32_t behavior;
   1023 
   1024 			if (chandef->center1_frequency > chandef->frequency)
   1025 				behavior = PRIMARY_CHANNEL_LOWER;
   1026 			else
   1027 				behavior = PRIMARY_CHANNEL_UPPER;
   1028 
   1029 			if ((info->flags & behavior) != behavior)
   1030 				return -ENOENT;
   1031 		}
   1032 
   1033 		return e4_has_frequency(info, chandef->frequency);
   1034 	case BAND_CHANDEF_WIDTH_80:
   1035 	case BAND_CHANDEF_WIDTH_160:
   1036 		if (info->flags & PLUS80)
   1037 			return -ENOENT;
   1038 
   1039 		if (own_bandwidth != info->channel_spacing)
   1040 			return -ENOENT;
   1041 
   1042 		return e4_has_ccfi(info, chandef->center1_frequency);
   1043 	case BAND_CHANDEF_WIDTH_80P80:
   1044 		if (!(info->flags & PLUS80))
   1045 			return -ENOENT;
   1046 
   1047 		r = e4_has_ccfi(info, chandef->center1_frequency);
   1048 		if (r < 0)
   1049 			return r;
   1050 
   1051 		return e4_has_ccfi(info, chandef->center2_frequency);
   1052 	default:
   1053 		break;
   1054 	}
   1055 
   1056 	return -ENOTSUP;
   1057 }
   1058 
   1059 int oci_to_frequency(uint32_t operating_class, uint32_t channel)
   1060 {
   1061 	const struct operating_class_info *info;
   1062 
   1063 	info = e4_find_opclass(operating_class);
   1064 	if (!info)
   1065 		return -ENOENT;
   1066 
   1067 	return e4_channel_to_frequency(info, channel);
   1068 }
   1069 
   1070 int oci_verify(const uint8_t oci[static 3], const struct band_chandef *own)
   1071 {
   1072 	const struct operating_class_info *info;
   1073 	int oci_frequency;
   1074 	int own_bandwidth;
   1075 	int oci_bandwidth;
   1076 
   1077 	info = e4_find_opclass(oci[0]);
   1078 	if (!info)
   1079 		return -ENOENT;
   1080 
   1081 	/*
   1082 	 * 802.11-2020, 12.2.9:
   1083 	 * Verifying that the maximum bandwidth used by the STA to transmit or
   1084 	 * receive PPDUs to/from the peer STA from which the OCI was received
   1085 	 * is no greater than the bandwidth of the operating class specified
   1086 	 * in the Operating Class field of the received OCI
   1087 	 */
   1088 	own_bandwidth = band_channel_info_get_bandwidth(own);
   1089 	if (own_bandwidth < 0)
   1090 		return own_bandwidth;
   1091 
   1092 	oci_bandwidth = info->channel_spacing;
   1093 	if (info->flags & PLUS80)
   1094 		oci_bandwidth *= 2;
   1095 
   1096 	if (own_bandwidth > oci_bandwidth)
   1097 		return -EPERM;
   1098 
   1099 	/*
   1100 	 * 802.11-2020, 12.2.9:
   1101 	 * Verifying that the primary channel used by the STA to transmit or
   1102 	 * receive PPDUs to/from the peer STA from which the OCI was received
   1103 	 * is equal to the Primary Channel Number field (for the corresponding
   1104 	 * operating class)
   1105 	 */
   1106 	oci_frequency = e4_channel_to_frequency(info, oci[1]);
   1107 	if (oci_frequency < 0)
   1108 		return oci_frequency;
   1109 
   1110 	if (oci_frequency != (int) own->frequency)
   1111 		return -EPERM;
   1112 
   1113 	/*
   1114 	 * 802.11-2020, 12.2.9:
   1115 	 * Verifying that, when 40 MHz bandwidth is used by the STA to transmit
   1116 	 * or receive PPDUs to/from the peer STA from which the OCI was
   1117 	 * received, the nonprimary 20 MHz used matches the operating class
   1118 	 * (i.e., upper/lower behavior) specified in the Operating Class field
   1119 	 * of the received OCI
   1120 	 *
   1121 	 * NOTE: For now we only check this if the STA and peer are operating
   1122 	 * on 40 Mhz channels.  If the STA is operating on 40 Mhz while the
   1123 	 * peer is operating on 80 or 160 Mhz wide channels, then only the
   1124 	 * primary channel validation is performed
   1125 	 *
   1126 	 * With 6GHz operating classes there is no concept of upper/lower 40mhz
   1127 	 * channels, therefore this special handling list not needed.
   1128 	 */
   1129 	if (own_bandwidth == 40 && oci_bandwidth == 40 &&
   1130 						info->operating_class < 131) {
   1131 		uint32_t behavior;
   1132 
   1133 		/*
   1134 		 * - Primary Channel Upper Behavior -> Secondary channel below
   1135 		 *   primary channel.  Or HT40MINUS.
   1136 		 * - Primary Channel Lower Behavior -> Secondary channel above
   1137 		 *   primary channel.  Or HT40PLUS.
   1138 		 */
   1139 		if (own->center1_frequency > own->frequency)
   1140 			behavior = PRIMARY_CHANNEL_LOWER;
   1141 		else
   1142 			behavior = PRIMARY_CHANNEL_UPPER;
   1143 
   1144 		if ((info->flags & behavior) != behavior)
   1145 			return -EPERM;
   1146 	}
   1147 
   1148 	/*
   1149 	 * 802.11-2020, 12.2.9:
   1150 	 * Verifying that, if operating an 80+80 MHz operating class, the
   1151 	 * frequency segment 1 channel number used by the STA to transmit or
   1152 	 * receive PPDUs to/from the peer STA from which the OCI was received
   1153 	 * is equal to the Frequency Segment 1 Channel Number field of the OCI.
   1154 	 */
   1155 	if (own->channel_width == BAND_CHANDEF_WIDTH_80P80) {
   1156 		uint32_t freq_segment1_chan_num;
   1157 
   1158 		if (!(info->flags & PLUS80))
   1159 			return -EPERM;
   1160 
   1161 		freq_segment1_chan_num =
   1162 			e4_frequency_to_channel(info, own->center2_frequency);
   1163 
   1164 		if (freq_segment1_chan_num != oci[2])
   1165 			return -EPERM;
   1166 	}
   1167 
   1168 	return 0;
   1169 }
   1170 
   1171 int oci_from_chandef(const struct band_chandef *own, uint8_t oci[static 3])
   1172 {
   1173 	unsigned int i;
   1174 
   1175 	for (i = 0; i < L_ARRAY_SIZE(e4_operating_classes); i++) {
   1176 		const struct operating_class_info *info =
   1177 						&e4_operating_classes[i];
   1178 
   1179 		if (e4_class_matches(info, own) < 0)
   1180 			continue;
   1181 
   1182 		oci[0] = info->operating_class;
   1183 		oci[1] = e4_frequency_to_channel(info, own->frequency);
   1184 
   1185 		if (own->center2_frequency)
   1186 			oci[2] = e4_frequency_to_channel(info,
   1187 							own->center2_frequency);
   1188 		else
   1189 			oci[2] = 0;
   1190 
   1191 		return 0;
   1192 	}
   1193 
   1194 	return -ENOENT;
   1195 }
   1196 
   1197 /* Find an HT chandef for the frequency */
   1198 int band_freq_to_ht_chandef(uint32_t freq, const struct band_freq_attrs *attr,
   1199 				struct band_chandef *chandef)
   1200 {
   1201 	enum band_freq band;
   1202 	enum band_chandef_width width;
   1203 	unsigned int i;
   1204 	const struct operating_class_info *best = NULL;
   1205 
   1206 	if (attr->disabled || !attr->supported)
   1207 		return -EINVAL;
   1208 
   1209 	if (!band_freq_to_channel(freq, &band))
   1210 		return -EINVAL;
   1211 
   1212 	for (i = 0; i < L_ARRAY_SIZE(e4_operating_classes); i++) {
   1213 		const struct operating_class_info *info =
   1214 						&e4_operating_classes[i];
   1215 		enum band_chandef_width w;
   1216 
   1217 		if (e4_has_frequency(info, freq) < 0)
   1218 			continue;
   1219 
   1220 		/* Any restrictions for this channel width? */
   1221 		switch (info->channel_spacing) {
   1222 		case 20:
   1223 			w = BAND_CHANDEF_WIDTH_20;
   1224 			break;
   1225 		case 40:
   1226 			w = BAND_CHANDEF_WIDTH_40;
   1227 
   1228 			/* 6GHz remove the upper/lower 40mhz channel concept */
   1229 			if (band == BAND_FREQ_6_GHZ)
   1230 				break;
   1231 
   1232 			if (info->flags & PRIMARY_CHANNEL_LOWER &&
   1233 						attr->no_ht40_plus)
   1234 				continue;
   1235 
   1236 			if (info->flags & PRIMARY_CHANNEL_UPPER &&
   1237 						attr->no_ht40_minus)
   1238 				continue;
   1239 
   1240 			break;
   1241 		default:
   1242 			continue;
   1243 		}
   1244 
   1245 		if (!best || best->channel_spacing < info->channel_spacing) {
   1246 			best = info;
   1247 			width = w;
   1248 		}
   1249 	}
   1250 
   1251 	if (!best)
   1252 		return -ENOENT;
   1253 
   1254 	chandef->frequency = freq;
   1255 	chandef->channel_width = width;
   1256 
   1257 	/*
   1258 	 * Choose a secondary channel frequency:
   1259 	 * - 20mhz no secondary
   1260 	 * - 40mhz we can base the selection off the channel flags, either
   1261 	 *   higher or lower.
   1262 	 */
   1263 _Pragma("GCC diagnostic push")
   1264 _Pragma("GCC diagnostic ignored \"-Wmaybe-uninitialized\"")
   1265 	switch (width) {
   1266 _Pragma("GCC diagnostic pop")
   1267 	case BAND_CHANDEF_WIDTH_20:
   1268 		return 0;
   1269 	case BAND_CHANDEF_WIDTH_40:
   1270 		if (band == BAND_FREQ_6_GHZ)
   1271 			return 0;
   1272 
   1273 		if (best->flags & PRIMARY_CHANNEL_UPPER)
   1274 			chandef->center1_frequency = freq - 10;
   1275 		else
   1276 			chandef->center1_frequency = freq + 10;
   1277 
   1278 		return 0;
   1279 	default:
   1280 		/* Should never happen */
   1281 		return -EINVAL;
   1282 	}
   1283 
   1284 	return 0;
   1285 }
   1286 
   1287 uint8_t band_freq_to_channel(uint32_t freq, enum band_freq *out_band)
   1288 {
   1289 	unsigned int i;
   1290 	enum band_freq band = 0;
   1291 	uint32_t channel = 0;
   1292 
   1293 	if (freq >= 2412 && freq <= 2484) {
   1294 		if (freq == 2484)
   1295 			channel = 14;
   1296 		else {
   1297 			channel = freq - 2407;
   1298 
   1299 			if (channel % 5)
   1300 				return 0;
   1301 
   1302 			channel /= 5;
   1303 		}
   1304 
   1305 		band = BAND_FREQ_2_4_GHZ;
   1306 		goto check_e4;
   1307 	}
   1308 
   1309 	if (freq >= 5005 && freq < 5900) {
   1310 		if (freq % 5)
   1311 			return 0;
   1312 
   1313 		channel = (freq - 5000) / 5;
   1314 
   1315 		band = BAND_FREQ_5_GHZ;
   1316 
   1317 		goto check_e4;
   1318 	}
   1319 
   1320 	if (freq >= 4905 && freq < 5000) {
   1321 		if (freq % 5)
   1322 			return 0;
   1323 
   1324 		channel = (freq - 4000) / 5;
   1325 
   1326 		band = BAND_FREQ_5_GHZ;
   1327 
   1328 		goto check_e4;
   1329 	}
   1330 
   1331 	if (freq > 5950 && freq <= 7115) {
   1332 		if (freq % 5)
   1333 			return 0;
   1334 
   1335 		channel = (freq - 5950) / 5;
   1336 
   1337 		band = BAND_FREQ_6_GHZ;
   1338 
   1339 		goto check_e4;
   1340 	}
   1341 
   1342 	if (freq == 5935) {
   1343 		band = BAND_FREQ_6_GHZ;
   1344 		channel = 2;
   1345 	}
   1346 
   1347 	if (!band || !channel)
   1348 		return 0;
   1349 
   1350 check_e4:
   1351 	for (i = 0; i < L_ARRAY_SIZE(e4_operating_classes); i++) {
   1352 		const struct operating_class_info *info =
   1353 						&e4_operating_classes[i];
   1354 
   1355 		if (band != band_oper_class_to_band(NULL,
   1356 							info->operating_class))
   1357 			continue;
   1358 
   1359 		if (e4_has_frequency(info, freq) == 0 ||
   1360 					e4_has_ccfi(info, freq) == 0) {
   1361 			if (out_band)
   1362 				*out_band = band;
   1363 
   1364 			return channel;
   1365 		}
   1366 	}
   1367 
   1368 	return 0;
   1369 }
   1370 
   1371 uint32_t band_channel_to_freq(uint8_t channel, enum band_freq band)
   1372 {
   1373 	unsigned int i;
   1374 	uint32_t freq = 0;
   1375 
   1376 	if (band == BAND_FREQ_2_4_GHZ) {
   1377 		if (channel >= 1 && channel <= 13)
   1378 			freq = 2407 + 5 * channel;
   1379 		else if (channel == 14)
   1380 			freq = 2484;
   1381 
   1382 		goto check_e4;
   1383 	}
   1384 
   1385 	if (band == BAND_FREQ_5_GHZ) {
   1386 		if (channel >= 1 && channel <= 179)
   1387 			freq = 5000 + 5 * channel;
   1388 		else if (channel >= 181 && channel <= 199)
   1389 			freq = 4000 + 5 * channel;
   1390 
   1391 		goto check_e4;
   1392 	}
   1393 
   1394 	if (band == BAND_FREQ_6_GHZ) {
   1395 		/* operating class 136 */
   1396 		if (channel == 2) {
   1397 			freq = 5935;
   1398 			goto check_e4;
   1399 		}
   1400 
   1401 		/* Channels increment by 4, starting with 1 */
   1402 		if (channel % 4 != 1)
   1403 			return 0;
   1404 
   1405 		if (channel < 1 || channel > 233)
   1406 			return 0;
   1407 
   1408 		/* operating classes 131, 132, 133, 134, 135 */
   1409 		freq = 5950 + 5 * channel;
   1410 	}
   1411 
   1412 check_e4:
   1413 	for (i = 0; i < L_ARRAY_SIZE(e4_operating_classes); i++) {
   1414 		const struct operating_class_info *info =
   1415 						&e4_operating_classes[i];
   1416 
   1417 		if (e4_has_frequency(info, freq) == 0 ||
   1418 					e4_has_ccfi(info, freq) == 0)
   1419 			return freq;
   1420 	}
   1421 
   1422 	return 0;
   1423 }
   1424 
   1425 static const char *const oper_class_us_codes[] = {
   1426 	"US", "CA"
   1427 };
   1428 
   1429 static const char *const oper_class_eu_codes[] = {
   1430 	"AL", "AM", "AT", "AZ", "BA", "BE", "BG", "BY", "CH", "CY", "CZ", "DE",
   1431 	"DK", "EE", "EL", "ES", "FI", "FR", "GE", "HR", "HU", "IE", "IS", "IT",
   1432 	"LI", "LT", "LU", "LV", "MD", "ME", "MK", "MT", "NL", "NO", "PL", "PT",
   1433 	"RO", "RS", "RU", "SE", "SI", "SK", "TR", "UA", "UK", "GB"
   1434 };
   1435 
   1436 /* Annex E, table E-1 */
   1437 static const uint8_t oper_class_us_to_global[] = {
   1438 	[1]  = 115, [2]  = 118, [3]  = 124, [4]  = 121,
   1439 	[5]  = 125, [6]  = 103, [7]  = 103, [8]  = 102,
   1440 	[9]  = 102, [10] = 101, [11] = 101, [12] = 81,
   1441 	[13] = 94,  [14] = 95,  [15] = 96,  [22] = 116,
   1442 	[23] = 119, [24] = 122, [25] = 126, [26] = 126,
   1443 	[27] = 117, [28] = 120, [29] = 123, [30] = 127,
   1444 	[31] = 127, [32] = 83,  [33] = 84,  [34] = 180,
   1445 	/* 128 - 130 is a 1 to 1 mapping */
   1446 };
   1447 
   1448 /* Annex E, table E-2 */
   1449 static const uint8_t oper_class_eu_to_global[] = {
   1450 	[1]  = 115, [2]  = 118, [3]  = 121, [4]  = 81,
   1451 	[5]  = 116, [6]  = 119, [7]  = 122, [8]  = 117,
   1452 	[9]  = 120, [10] = 123, [11] = 83,  [12] = 84,
   1453 	[17] = 125, [18] = 130,
   1454 	/* 128 - 130 is a 1 to 1 mapping */
   1455 };
   1456 
   1457 /* Annex E, table E-3 */
   1458 static const uint8_t oper_class_jp_to_global[] = {
   1459 	[1]  = 115, [2]  = 112, [3]  = 112, [4]  = 112,
   1460 	[5]  = 112, [6]  = 112, [7]  = 109, [8]  = 109,
   1461 	[9]  = 109, [10] = 109, [11] = 109, [12] = 113,
   1462 	[13] = 113, [14] = 113, [15] = 113, [16] = 110,
   1463 	[17] = 110, [18] = 110, [19] = 110, [20] = 110,
   1464 	[21] = 114, [22] = 114, [23] = 114, [24] = 114,
   1465 	[25] = 111, [26] = 111, [27] = 111, [28] = 111,
   1466 	[29] = 111, [30] = 81,  [31] = 82,  [32] = 118,
   1467 	[33] = 118, [34] = 121, [35] = 121, [36] = 116,
   1468 	[37] = 119, [38] = 119, [39] = 122, [40] = 122,
   1469 	[41] = 117, [42] = 120, [43] = 120, [44] = 123,
   1470 	[45] = 123, [46] = 104, [47] = 104, [48] = 104,
   1471 	[49] = 104, [50] = 104, [51] = 105, [52] = 105,
   1472 	[53] = 105, [54] = 105, [55] = 105, [56] = 83,
   1473 	[57] = 84,  [58] = 121, [59] = 180,
   1474 	/* 128 - 130 is a 1 to 1 mapping */
   1475 };
   1476 
   1477 /* Annex E, table E-4 (only 2.4GHz, 4.9 / 5GHz, and 6GHz bands) */
   1478 static const enum band_freq oper_class_to_band_global[] = {
   1479 	[81 ... 84]   = BAND_FREQ_2_4_GHZ,
   1480 	[104 ... 130] = BAND_FREQ_5_GHZ,
   1481 	[131 ... 136] = BAND_FREQ_6_GHZ,
   1482 };
   1483 
   1484 /* Annex E, table E-5 */
   1485 static const uint8_t oper_class_cn_to_global[] = {
   1486 	[1]  = 115, [2]  = 118, [3]  = 125, [4]  = 116,
   1487 	[5]  = 119, [6]  = 126, [7]  = 81,  [8]  = 83,
   1488 	[9]  = 84,
   1489 	/* 128 - 130 is a 1 to 1 mapping */
   1490 };
   1491 
   1492 /*
   1493  * Annex C describes the country string encoding.
   1494  *
   1495  * If it is a country, the first two octets of this string is the two character
   1496  * country code as described in document ISO 3166-1. The third octet is one of
   1497  * the following:
   1498  *  1. an ASCII space character, if the regulations under which the station is
   1499  *     operating encompass all environments for the current frequency band in
   1500  *     the country,
   1501  *  2. an ASCII 'O' character, if the regulations under which the station is
   1502  *     operating are for an outdoor environment only, or
   1503  *  3. an ASCII 'I' character, if the regulations under which the station is
   1504  *     operating are for an indoor environment only.
   1505  *  4. an ASCII 'X' character, if the station is operating under a noncountry
   1506  *     entity. The first two octets of the noncountry entity is two ASCII 'XX'
   1507  *     characters.
   1508  *  5. the hexadecimal representation of the Operating Class table number
   1509  *     currently in use, from the set of tables defined in Annex E, e.g.,
   1510  *     Table E-1 is represented as x'01'.
   1511  */
   1512 static enum band_freq oper_class_to_band(const uint8_t *country,
   1513 					uint8_t oper_class,
   1514 					bool ignore_country3)
   1515 {
   1516 	unsigned int i;
   1517 	int table = 0;
   1518 
   1519 	/*
   1520 	 * If a country is set, and the 3rd byte maps to some E-* table in the
   1521 	 * spec use that (case 5). Only caveat here is some APs erroneously set
   1522 	 * this 3rd byte. To work around we can fall back to case 1, where only
   1523 	 * the first two characters are used to lookup the table.
   1524 	 */
   1525 	if (!ignore_country3 && country && country[2] >= 1 && country[2] <= 5)
   1526 		table = country[2];
   1527 	else if (country) {
   1528 		/*
   1529 		 * Assuming case 1, although its unlikely you would handle
   1530 		 * cases 2 (O) or 3 (I) any differently. Case 4 (X) is unlikely
   1531 		 * and we really wouldn't have enough information to correctly
   1532 		 * determine the band in some obscure non-country domain.
   1533 		 */
   1534 		for (i = 0; i < L_ARRAY_SIZE(oper_class_us_codes); i++)
   1535 			if (!memcmp(oper_class_us_codes[i], country, 2)) {
   1536 				/* Use table E-1 */
   1537 				table = 1;
   1538 				break;
   1539 			}
   1540 
   1541 		for (i = 0; i < L_ARRAY_SIZE(oper_class_eu_codes); i++)
   1542 			if (!memcmp(oper_class_eu_codes[i], country, 2)) {
   1543 				/* Use table E-2 */
   1544 				table = 2;
   1545 				break;
   1546 			}
   1547 
   1548 		if (!memcmp("JP", country, 2))
   1549 			/* Use table E-3 */
   1550 			table = 3;
   1551 
   1552 		if (!memcmp("CN", country, 2))
   1553 			/* Use table E-5 */
   1554 			table = 5;
   1555 	}
   1556 
   1557 	switch (table) {
   1558 	case 1:
   1559 		if (oper_class < L_ARRAY_SIZE(oper_class_us_to_global))
   1560 			oper_class = oper_class_us_to_global[oper_class];
   1561 		break;
   1562 	case 2:
   1563 		if (oper_class < L_ARRAY_SIZE(oper_class_eu_to_global))
   1564 			oper_class = oper_class_eu_to_global[oper_class];
   1565 		break;
   1566 	case 3:
   1567 		if (oper_class < L_ARRAY_SIZE(oper_class_jp_to_global))
   1568 			oper_class = oper_class_jp_to_global[oper_class];
   1569 		break;
   1570 	case 5:
   1571 		if (oper_class < L_ARRAY_SIZE(oper_class_cn_to_global))
   1572 			oper_class = oper_class_cn_to_global[oper_class];
   1573 		break;
   1574 	}
   1575 
   1576 	if (oper_class < L_ARRAY_SIZE(oper_class_to_band_global))
   1577 		return oper_class_to_band_global[oper_class];
   1578 	else
   1579 		return 0;
   1580 }
   1581 
   1582 enum band_freq band_oper_class_to_band(const uint8_t *country,
   1583 					uint8_t oper_class)
   1584 {
   1585 	enum band_freq band = oper_class_to_band(country, oper_class, false);
   1586 	if (!band) {
   1587 		/* Fallback with no country string won't change anything */
   1588 		if (!country)
   1589 			return 0;
   1590 
   1591 		l_warn("Failed to find band with country string '%c%c %u' and "
   1592 			"oper class %u, trying fallback",
   1593 			country[0], country[1], country[2], oper_class);
   1594 
   1595 		return oper_class_to_band(country, oper_class, true);
   1596 	}
   1597 
   1598 	return band;
   1599 }
   1600 
   1601 const char *band_chandef_width_to_string(enum band_chandef_width width)
   1602 {
   1603 	switch (width) {
   1604 	case BAND_CHANDEF_WIDTH_20NOHT:
   1605 		return "20MHz (no-HT)";
   1606 	case BAND_CHANDEF_WIDTH_20:
   1607 		return "20MHz";
   1608 	case BAND_CHANDEF_WIDTH_40:
   1609 		return "40MHz";
   1610 	case BAND_CHANDEF_WIDTH_80:
   1611 		return "80MHz";
   1612 	case BAND_CHANDEF_WIDTH_80P80:
   1613 		return "80+80MHz";
   1614 	case BAND_CHANDEF_WIDTH_160:
   1615 		return "160MHz";
   1616 	}
   1617 
   1618 	return NULL;
   1619 }