1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24  * USA
25  *
26  * The full GNU General Public License is included in this distribution
27  * in the file called COPYING.
28  *
29  * Contact Information:
30  *  Intel Linux Wireless <linuxwifi@intel.com>
31  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32  *
33  * BSD LICENSE
34  *
35  * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
36  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
38  * All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  *
44  *  * Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  *  * Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in
48  *    the documentation and/or other materials provided with the
49  *    distribution.
50  *  * Neither the name Intel Corporation nor the names of its
51  *    contributors may be used to endorse or promote products derived
52  *    from this software without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65  *****************************************************************************/
66 #include <linux/types.h>
67 #include <linux/slab.h>
68 #include <linux/export.h>
69 #include <linux/etherdevice.h>
70 #include <linux/pci.h>
71 #include <linux/acpi.h>
72 #include "iwl-drv.h"
73 #include "iwl-modparams.h"
74 #include "iwl-nvm-parse.h"
75 #include "iwl-prph.h"
76 #include "iwl-io.h"
77 #include "iwl-csr.h"
78 
79 /* NVM offsets (in words) definitions */
80 enum wkp_nvm_offsets {
81 	/* NVM HW-Section offset (in words) definitions */
82 	HW_ADDR = 0x15,
83 
84 	/* NVM SW-Section offset (in words) definitions */
85 	NVM_SW_SECTION = 0x1C0,
86 	NVM_VERSION = 0,
87 	RADIO_CFG = 1,
88 	SKU = 2,
89 	N_HW_ADDRS = 3,
90 	NVM_CHANNELS = 0x1E0 - NVM_SW_SECTION,
91 
92 	/* NVM calibration section offset (in words) definitions */
93 	NVM_CALIB_SECTION = 0x2B8,
94 	XTAL_CALIB = 0x316 - NVM_CALIB_SECTION
95 };
96 
97 enum ext_nvm_offsets {
98 	/* NVM HW-Section offset (in words) definitions */
99 	MAC_ADDRESS_OVERRIDE_EXT_NVM = 1,
100 
101 	/* NVM SW-Section offset (in words) definitions */
102 	NVM_VERSION_EXT_NVM = 0,
103 	RADIO_CFG_FAMILY_EXT_NVM = 0,
104 	SKU_FAMILY_8000 = 2,
105 	N_HW_ADDRS_FAMILY_8000 = 3,
106 
107 	/* NVM REGULATORY -Section offset (in words) definitions */
108 	NVM_CHANNELS_EXTENDED = 0,
109 	NVM_LAR_OFFSET_OLD = 0x4C7,
110 	NVM_LAR_OFFSET = 0x507,
111 	NVM_LAR_ENABLED = 0x7,
112 };
113 
114 /* SKU Capabilities (actual values from NVM definition) */
115 enum nvm_sku_bits {
116 	NVM_SKU_CAP_BAND_24GHZ		= BIT(0),
117 	NVM_SKU_CAP_BAND_52GHZ		= BIT(1),
118 	NVM_SKU_CAP_11N_ENABLE		= BIT(2),
119 	NVM_SKU_CAP_11AC_ENABLE		= BIT(3),
120 	NVM_SKU_CAP_MIMO_DISABLE	= BIT(5),
121 };
122 
123 /*
124  * These are the channel numbers in the order that they are stored in the NVM
125  */
126 static const u8 iwl_nvm_channels[] = {
127 	/* 2.4 GHz */
128 	1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
129 	/* 5 GHz */
130 	36, 40, 44 , 48, 52, 56, 60, 64,
131 	100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144,
132 	149, 153, 157, 161, 165
133 };
134 
135 static const u8 iwl_ext_nvm_channels[] = {
136 	/* 2.4 GHz */
137 	1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
138 	/* 5 GHz */
139 	36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92,
140 	96, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144,
141 	149, 153, 157, 161, 165, 169, 173, 177, 181
142 };
143 
144 #define IWL_NUM_CHANNELS		ARRAY_SIZE(iwl_nvm_channels)
145 #define IWL_NUM_CHANNELS_EXT	ARRAY_SIZE(iwl_ext_nvm_channels)
146 #define NUM_2GHZ_CHANNELS		14
147 #define NUM_2GHZ_CHANNELS_EXT	14
148 #define FIRST_2GHZ_HT_MINUS		5
149 #define LAST_2GHZ_HT_PLUS		9
150 #define LAST_5GHZ_HT			165
151 #define LAST_5GHZ_HT_FAMILY_8000	181
152 #define N_HW_ADDR_MASK			0xF
153 
154 /* rate data (static) */
155 static struct ieee80211_rate iwl_cfg80211_rates[] = {
156 	{ .bitrate = 1 * 10, .hw_value = 0, .hw_value_short = 0, },
157 	{ .bitrate = 2 * 10, .hw_value = 1, .hw_value_short = 1,
158 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE, },
159 	{ .bitrate = 5.5 * 10, .hw_value = 2, .hw_value_short = 2,
160 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE, },
161 	{ .bitrate = 11 * 10, .hw_value = 3, .hw_value_short = 3,
162 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE, },
163 	{ .bitrate = 6 * 10, .hw_value = 4, .hw_value_short = 4, },
164 	{ .bitrate = 9 * 10, .hw_value = 5, .hw_value_short = 5, },
165 	{ .bitrate = 12 * 10, .hw_value = 6, .hw_value_short = 6, },
166 	{ .bitrate = 18 * 10, .hw_value = 7, .hw_value_short = 7, },
167 	{ .bitrate = 24 * 10, .hw_value = 8, .hw_value_short = 8, },
168 	{ .bitrate = 36 * 10, .hw_value = 9, .hw_value_short = 9, },
169 	{ .bitrate = 48 * 10, .hw_value = 10, .hw_value_short = 10, },
170 	{ .bitrate = 54 * 10, .hw_value = 11, .hw_value_short = 11, },
171 };
172 #define RATES_24_OFFS	0
173 #define N_RATES_24	ARRAY_SIZE(iwl_cfg80211_rates)
174 #define RATES_52_OFFS	4
175 #define N_RATES_52	(N_RATES_24 - RATES_52_OFFS)
176 
177 /**
178  * enum iwl_nvm_channel_flags - channel flags in NVM
179  * @NVM_CHANNEL_VALID: channel is usable for this SKU/geo
180  * @NVM_CHANNEL_IBSS: usable as an IBSS channel
181  * @NVM_CHANNEL_ACTIVE: active scanning allowed
182  * @NVM_CHANNEL_RADAR: radar detection required
183  * @NVM_CHANNEL_INDOOR_ONLY: only indoor use is allowed
184  * @NVM_CHANNEL_GO_CONCURRENT: GO operation is allowed when connected to BSS
185  *	on same channel on 2.4 or same UNII band on 5.2
186  * @NVM_CHANNEL_WIDE: 20 MHz channel okay (?)
187  * @NVM_CHANNEL_40MHZ: 40 MHz channel okay (?)
188  * @NVM_CHANNEL_80MHZ: 80 MHz channel okay (?)
189  * @NVM_CHANNEL_160MHZ: 160 MHz channel okay (?)
190  */
191 enum iwl_nvm_channel_flags {
192 	NVM_CHANNEL_VALID = BIT(0),
193 	NVM_CHANNEL_IBSS = BIT(1),
194 	NVM_CHANNEL_ACTIVE = BIT(3),
195 	NVM_CHANNEL_RADAR = BIT(4),
196 	NVM_CHANNEL_INDOOR_ONLY = BIT(5),
197 	NVM_CHANNEL_GO_CONCURRENT = BIT(6),
198 	NVM_CHANNEL_WIDE = BIT(8),
199 	NVM_CHANNEL_40MHZ = BIT(9),
200 	NVM_CHANNEL_80MHZ = BIT(10),
201 	NVM_CHANNEL_160MHZ = BIT(11),
202 };
203 
204 #define CHECK_AND_PRINT_I(x)	\
205 	((ch_flags & NVM_CHANNEL_##x) ? # x " " : "")
206 
207 static u32 iwl_get_channel_flags(u8 ch_num, int ch_idx, bool is_5ghz,
208 				 u16 nvm_flags, const struct iwl_cfg *cfg)
209 {
210 	u32 flags = IEEE80211_CHAN_NO_HT40;
211 	u32 last_5ghz_ht = LAST_5GHZ_HT;
212 
213 	if (cfg->ext_nvm)
214 		last_5ghz_ht = LAST_5GHZ_HT_FAMILY_8000;
215 
216 	if (!is_5ghz && (nvm_flags & NVM_CHANNEL_40MHZ)) {
217 		if (ch_num <= LAST_2GHZ_HT_PLUS)
218 			flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
219 		if (ch_num >= FIRST_2GHZ_HT_MINUS)
220 			flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
221 	} else if (ch_num <= last_5ghz_ht && (nvm_flags & NVM_CHANNEL_40MHZ)) {
222 		if ((ch_idx - NUM_2GHZ_CHANNELS) % 2 == 0)
223 			flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
224 		else
225 			flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
226 	}
227 	if (!(nvm_flags & NVM_CHANNEL_80MHZ))
228 		flags |= IEEE80211_CHAN_NO_80MHZ;
229 	if (!(nvm_flags & NVM_CHANNEL_160MHZ))
230 		flags |= IEEE80211_CHAN_NO_160MHZ;
231 
232 	if (!(nvm_flags & NVM_CHANNEL_IBSS))
233 		flags |= IEEE80211_CHAN_NO_IR;
234 
235 	if (!(nvm_flags & NVM_CHANNEL_ACTIVE))
236 		flags |= IEEE80211_CHAN_NO_IR;
237 
238 	if (nvm_flags & NVM_CHANNEL_RADAR)
239 		flags |= IEEE80211_CHAN_RADAR;
240 
241 	if (nvm_flags & NVM_CHANNEL_INDOOR_ONLY)
242 		flags |= IEEE80211_CHAN_INDOOR_ONLY;
243 
244 	/* Set the GO concurrent flag only in case that NO_IR is set.
245 	 * Otherwise it is meaningless
246 	 */
247 	if ((nvm_flags & NVM_CHANNEL_GO_CONCURRENT) &&
248 	    (flags & IEEE80211_CHAN_NO_IR))
249 		flags |= IEEE80211_CHAN_IR_CONCURRENT;
250 
251 	return flags;
252 }
253 
254 static int iwl_init_channel_map(struct device *dev, const struct iwl_cfg *cfg,
255 				struct iwl_nvm_data *data,
256 				const __le16 * const nvm_ch_flags,
257 				bool lar_supported)
258 {
259 	int ch_idx;
260 	int n_channels = 0;
261 	struct ieee80211_channel *channel;
262 	u16 ch_flags;
263 	bool is_5ghz;
264 	int num_of_ch, num_2ghz_channels;
265 	const u8 *nvm_chan;
266 
267 	if (!cfg->ext_nvm) {
268 		num_of_ch = IWL_NUM_CHANNELS;
269 		nvm_chan = &iwl_nvm_channels[0];
270 		num_2ghz_channels = NUM_2GHZ_CHANNELS;
271 	} else {
272 		num_of_ch = IWL_NUM_CHANNELS_EXT;
273 		nvm_chan = &iwl_ext_nvm_channels[0];
274 		num_2ghz_channels = NUM_2GHZ_CHANNELS_EXT;
275 	}
276 
277 	for (ch_idx = 0; ch_idx < num_of_ch; ch_idx++) {
278 		ch_flags = __le16_to_cpup(nvm_ch_flags + ch_idx);
279 
280 		if (ch_idx >= num_2ghz_channels &&
281 		    !data->sku_cap_band_52GHz_enable)
282 			continue;
283 
284 		if (ch_flags & NVM_CHANNEL_160MHZ)
285 			data->vht160_supported = true;
286 
287 		if (!lar_supported && !(ch_flags & NVM_CHANNEL_VALID)) {
288 			/*
289 			 * Channels might become valid later if lar is
290 			 * supported, hence we still want to add them to
291 			 * the list of supported channels to cfg80211.
292 			 */
293 			IWL_DEBUG_EEPROM(dev,
294 					 "Ch. %d Flags %x [%sGHz] - No traffic\n",
295 					 nvm_chan[ch_idx],
296 					 ch_flags,
297 					 (ch_idx >= num_2ghz_channels) ?
298 					 "5.2" : "2.4");
299 			continue;
300 		}
301 
302 		channel = &data->channels[n_channels];
303 		n_channels++;
304 
305 		channel->hw_value = nvm_chan[ch_idx];
306 		channel->band = (ch_idx < num_2ghz_channels) ?
307 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
308 		channel->center_freq =
309 			ieee80211_channel_to_frequency(
310 				channel->hw_value, channel->band);
311 
312 		/* Initialize regulatory-based run-time data */
313 
314 		/*
315 		 * Default value - highest tx power value.  max_power
316 		 * is not used in mvm, and is used for backwards compatibility
317 		 */
318 		channel->max_power = IWL_DEFAULT_MAX_TX_POWER;
319 		is_5ghz = channel->band == NL80211_BAND_5GHZ;
320 
321 		/* don't put limitations in case we're using LAR */
322 		if (!lar_supported)
323 			channel->flags = iwl_get_channel_flags(nvm_chan[ch_idx],
324 							       ch_idx, is_5ghz,
325 							       ch_flags, cfg);
326 		else
327 			channel->flags = 0;
328 
329 		IWL_DEBUG_EEPROM(dev,
330 				 "Ch. %d [%sGHz] flags 0x%x %s%s%s%s%s%s%s%s%s%s(%ddBm): Ad-Hoc %ssupported\n",
331 				 channel->hw_value,
332 				 is_5ghz ? "5.2" : "2.4",
333 				 ch_flags,
334 				 CHECK_AND_PRINT_I(VALID),
335 				 CHECK_AND_PRINT_I(IBSS),
336 				 CHECK_AND_PRINT_I(ACTIVE),
337 				 CHECK_AND_PRINT_I(RADAR),
338 				 CHECK_AND_PRINT_I(INDOOR_ONLY),
339 				 CHECK_AND_PRINT_I(GO_CONCURRENT),
340 				 CHECK_AND_PRINT_I(WIDE),
341 				 CHECK_AND_PRINT_I(40MHZ),
342 				 CHECK_AND_PRINT_I(80MHZ),
343 				 CHECK_AND_PRINT_I(160MHZ),
344 				 channel->max_power,
345 				 ((ch_flags & NVM_CHANNEL_IBSS) &&
346 				  !(ch_flags & NVM_CHANNEL_RADAR))
347 					? "" : "not ");
348 	}
349 
350 	return n_channels;
351 }
352 
353 static void iwl_init_vht_hw_capab(const struct iwl_cfg *cfg,
354 				  struct iwl_nvm_data *data,
355 				  struct ieee80211_sta_vht_cap *vht_cap,
356 				  u8 tx_chains, u8 rx_chains)
357 {
358 	int num_rx_ants = num_of_ant(rx_chains);
359 	int num_tx_ants = num_of_ant(tx_chains);
360 	unsigned int max_ampdu_exponent = (cfg->max_vht_ampdu_exponent ?:
361 					   IEEE80211_VHT_MAX_AMPDU_1024K);
362 
363 	vht_cap->vht_supported = true;
364 
365 	vht_cap->cap = IEEE80211_VHT_CAP_SHORT_GI_80 |
366 		       IEEE80211_VHT_CAP_RXSTBC_1 |
367 		       IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
368 		       3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT |
369 		       max_ampdu_exponent <<
370 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
371 
372 	if (data->vht160_supported)
373 		vht_cap->cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
374 				IEEE80211_VHT_CAP_SHORT_GI_160;
375 
376 	if (cfg->vht_mu_mimo_supported)
377 		vht_cap->cap |= IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
378 
379 	if (cfg->ht_params->ldpc)
380 		vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC;
381 
382 	if (data->sku_cap_mimo_disabled) {
383 		num_rx_ants = 1;
384 		num_tx_ants = 1;
385 	}
386 
387 	if (num_tx_ants > 1)
388 		vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
389 	else
390 		vht_cap->cap |= IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN;
391 
392 	switch (iwlwifi_mod_params.amsdu_size) {
393 	case IWL_AMSDU_DEF:
394 		if (cfg->mq_rx_supported)
395 			vht_cap->cap |=
396 				IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454;
397 		else
398 			vht_cap->cap |= IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895;
399 		break;
400 	case IWL_AMSDU_4K:
401 		vht_cap->cap |= IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895;
402 		break;
403 	case IWL_AMSDU_8K:
404 		vht_cap->cap |= IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991;
405 		break;
406 	case IWL_AMSDU_12K:
407 		vht_cap->cap |= IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454;
408 		break;
409 	default:
410 		break;
411 	}
412 
413 	vht_cap->vht_mcs.rx_mcs_map =
414 		cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
415 			    IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
416 			    IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
417 			    IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
418 			    IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
419 			    IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
420 			    IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
421 			    IEEE80211_VHT_MCS_NOT_SUPPORTED << 14);
422 
423 	if (num_rx_ants == 1 || cfg->rx_with_siso_diversity) {
424 		vht_cap->cap |= IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN;
425 		/* this works because NOT_SUPPORTED == 3 */
426 		vht_cap->vht_mcs.rx_mcs_map |=
427 			cpu_to_le16(IEEE80211_VHT_MCS_NOT_SUPPORTED << 2);
428 	}
429 
430 	vht_cap->vht_mcs.tx_mcs_map = vht_cap->vht_mcs.rx_mcs_map;
431 }
432 
433 void iwl_init_sbands(struct device *dev, const struct iwl_cfg *cfg,
434 		     struct iwl_nvm_data *data, const __le16 *nvm_ch_flags,
435 		     u8 tx_chains, u8 rx_chains, bool lar_supported)
436 {
437 	int n_channels;
438 	int n_used = 0;
439 	struct ieee80211_supported_band *sband;
440 
441 	n_channels = iwl_init_channel_map(dev, cfg, data, nvm_ch_flags,
442 					  lar_supported);
443 	sband = &data->bands[NL80211_BAND_2GHZ];
444 	sband->band = NL80211_BAND_2GHZ;
445 	sband->bitrates = &iwl_cfg80211_rates[RATES_24_OFFS];
446 	sband->n_bitrates = N_RATES_24;
447 	n_used += iwl_init_sband_channels(data, sband, n_channels,
448 					  NL80211_BAND_2GHZ);
449 	iwl_init_ht_hw_capab(cfg, data, &sband->ht_cap, NL80211_BAND_2GHZ,
450 			     tx_chains, rx_chains);
451 
452 	sband = &data->bands[NL80211_BAND_5GHZ];
453 	sband->band = NL80211_BAND_5GHZ;
454 	sband->bitrates = &iwl_cfg80211_rates[RATES_52_OFFS];
455 	sband->n_bitrates = N_RATES_52;
456 	n_used += iwl_init_sband_channels(data, sband, n_channels,
457 					  NL80211_BAND_5GHZ);
458 	iwl_init_ht_hw_capab(cfg, data, &sband->ht_cap, NL80211_BAND_5GHZ,
459 			     tx_chains, rx_chains);
460 	if (data->sku_cap_11ac_enable && !iwlwifi_mod_params.disable_11ac)
461 		iwl_init_vht_hw_capab(cfg, data, &sband->vht_cap,
462 				      tx_chains, rx_chains);
463 
464 	if (n_channels != n_used)
465 		IWL_ERR_DEV(dev, "NVM: used only %d of %d channels\n",
466 			    n_used, n_channels);
467 }
468 IWL_EXPORT_SYMBOL(iwl_init_sbands);
469 
470 static int iwl_get_sku(const struct iwl_cfg *cfg, const __le16 *nvm_sw,
471 		       const __le16 *phy_sku)
472 {
473 	if (!cfg->ext_nvm)
474 		return le16_to_cpup(nvm_sw + SKU);
475 
476 	return le32_to_cpup((__le32 *)(phy_sku + SKU_FAMILY_8000));
477 }
478 
479 static int iwl_get_nvm_version(const struct iwl_cfg *cfg, const __le16 *nvm_sw)
480 {
481 	if (!cfg->ext_nvm)
482 		return le16_to_cpup(nvm_sw + NVM_VERSION);
483 	else
484 		return le32_to_cpup((__le32 *)(nvm_sw +
485 					       NVM_VERSION_EXT_NVM));
486 }
487 
488 static int iwl_get_radio_cfg(const struct iwl_cfg *cfg, const __le16 *nvm_sw,
489 			     const __le16 *phy_sku)
490 {
491 	if (!cfg->ext_nvm)
492 		return le16_to_cpup(nvm_sw + RADIO_CFG);
493 
494 	return le32_to_cpup((__le32 *)(phy_sku + RADIO_CFG_FAMILY_EXT_NVM));
495 
496 }
497 
498 static int iwl_get_n_hw_addrs(const struct iwl_cfg *cfg, const __le16 *nvm_sw)
499 {
500 	int n_hw_addr;
501 
502 	if (!cfg->ext_nvm)
503 		return le16_to_cpup(nvm_sw + N_HW_ADDRS);
504 
505 	n_hw_addr = le32_to_cpup((__le32 *)(nvm_sw + N_HW_ADDRS_FAMILY_8000));
506 
507 	return n_hw_addr & N_HW_ADDR_MASK;
508 }
509 
510 static void iwl_set_radio_cfg(const struct iwl_cfg *cfg,
511 			      struct iwl_nvm_data *data,
512 			      u32 radio_cfg)
513 {
514 	if (!cfg->ext_nvm) {
515 		data->radio_cfg_type = NVM_RF_CFG_TYPE_MSK(radio_cfg);
516 		data->radio_cfg_step = NVM_RF_CFG_STEP_MSK(radio_cfg);
517 		data->radio_cfg_dash = NVM_RF_CFG_DASH_MSK(radio_cfg);
518 		data->radio_cfg_pnum = NVM_RF_CFG_PNUM_MSK(radio_cfg);
519 		return;
520 	}
521 
522 	/* set the radio configuration for family 8000 */
523 	data->radio_cfg_type = EXT_NVM_RF_CFG_TYPE_MSK(radio_cfg);
524 	data->radio_cfg_step = EXT_NVM_RF_CFG_STEP_MSK(radio_cfg);
525 	data->radio_cfg_dash = EXT_NVM_RF_CFG_DASH_MSK(radio_cfg);
526 	data->radio_cfg_pnum = EXT_NVM_RF_CFG_FLAVOR_MSK(radio_cfg);
527 	data->valid_tx_ant = EXT_NVM_RF_CFG_TX_ANT_MSK(radio_cfg);
528 	data->valid_rx_ant = EXT_NVM_RF_CFG_RX_ANT_MSK(radio_cfg);
529 }
530 
531 static void iwl_flip_hw_address(__le32 mac_addr0, __le32 mac_addr1, u8 *dest)
532 {
533 	const u8 *hw_addr;
534 
535 	hw_addr = (const u8 *)&mac_addr0;
536 	dest[0] = hw_addr[3];
537 	dest[1] = hw_addr[2];
538 	dest[2] = hw_addr[1];
539 	dest[3] = hw_addr[0];
540 
541 	hw_addr = (const u8 *)&mac_addr1;
542 	dest[4] = hw_addr[1];
543 	dest[5] = hw_addr[0];
544 }
545 
546 void iwl_set_hw_address_from_csr(struct iwl_trans *trans,
547 				 struct iwl_nvm_data *data)
548 {
549 	__le32 mac_addr0 = cpu_to_le32(iwl_read32(trans, CSR_MAC_ADDR0_STRAP));
550 	__le32 mac_addr1 = cpu_to_le32(iwl_read32(trans, CSR_MAC_ADDR1_STRAP));
551 
552 	iwl_flip_hw_address(mac_addr0, mac_addr1, data->hw_addr);
553 	/*
554 	 * If the OEM fused a valid address, use it instead of the one in the
555 	 * OTP
556 	 */
557 	if (is_valid_ether_addr(data->hw_addr))
558 		return;
559 
560 	mac_addr0 = cpu_to_le32(iwl_read32(trans, CSR_MAC_ADDR0_OTP));
561 	mac_addr1 = cpu_to_le32(iwl_read32(trans, CSR_MAC_ADDR1_OTP));
562 
563 	iwl_flip_hw_address(mac_addr0, mac_addr1, data->hw_addr);
564 }
565 IWL_EXPORT_SYMBOL(iwl_set_hw_address_from_csr);
566 
567 static void iwl_set_hw_address_family_8000(struct iwl_trans *trans,
568 					   const struct iwl_cfg *cfg,
569 					   struct iwl_nvm_data *data,
570 					   const __le16 *mac_override,
571 					   const __le16 *nvm_hw)
572 {
573 	const u8 *hw_addr;
574 
575 	if (mac_override) {
576 		static const u8 reserved_mac[] = {
577 			0x02, 0xcc, 0xaa, 0xff, 0xee, 0x00
578 		};
579 
580 		hw_addr = (const u8 *)(mac_override +
581 				 MAC_ADDRESS_OVERRIDE_EXT_NVM);
582 
583 		/*
584 		 * Store the MAC address from MAO section.
585 		 * No byte swapping is required in MAO section
586 		 */
587 		memcpy(data->hw_addr, hw_addr, ETH_ALEN);
588 
589 		/*
590 		 * Force the use of the OTP MAC address in case of reserved MAC
591 		 * address in the NVM, or if address is given but invalid.
592 		 */
593 		if (is_valid_ether_addr(data->hw_addr) &&
594 		    memcmp(reserved_mac, hw_addr, ETH_ALEN) != 0)
595 			return;
596 
597 		IWL_ERR(trans,
598 			"mac address from nvm override section is not valid\n");
599 	}
600 
601 	if (nvm_hw) {
602 		/* read the mac address from WFMP registers */
603 		__le32 mac_addr0 = cpu_to_le32(iwl_trans_read_prph(trans,
604 						WFMP_MAC_ADDR_0));
605 		__le32 mac_addr1 = cpu_to_le32(iwl_trans_read_prph(trans,
606 						WFMP_MAC_ADDR_1));
607 
608 		iwl_flip_hw_address(mac_addr0, mac_addr1, data->hw_addr);
609 
610 		return;
611 	}
612 
613 	IWL_ERR(trans, "mac address is not found\n");
614 }
615 
616 static int iwl_set_hw_address(struct iwl_trans *trans,
617 			      const struct iwl_cfg *cfg,
618 			      struct iwl_nvm_data *data, const __le16 *nvm_hw,
619 			      const __le16 *mac_override)
620 {
621 	if (cfg->mac_addr_from_csr) {
622 		iwl_set_hw_address_from_csr(trans, data);
623 	} else if (!cfg->ext_nvm) {
624 		const u8 *hw_addr = (const u8 *)(nvm_hw + HW_ADDR);
625 
626 		/* The byte order is little endian 16 bit, meaning 214365 */
627 		data->hw_addr[0] = hw_addr[1];
628 		data->hw_addr[1] = hw_addr[0];
629 		data->hw_addr[2] = hw_addr[3];
630 		data->hw_addr[3] = hw_addr[2];
631 		data->hw_addr[4] = hw_addr[5];
632 		data->hw_addr[5] = hw_addr[4];
633 	} else {
634 		iwl_set_hw_address_family_8000(trans, cfg, data,
635 					       mac_override, nvm_hw);
636 	}
637 
638 	if (!is_valid_ether_addr(data->hw_addr)) {
639 		IWL_ERR(trans, "no valid mac address was found\n");
640 		return -EINVAL;
641 	}
642 
643 	IWL_INFO(trans, "base HW address: %pM\n", data->hw_addr);
644 
645 	return 0;
646 }
647 
648 struct iwl_nvm_data *
649 iwl_parse_nvm_data(struct iwl_trans *trans, const struct iwl_cfg *cfg,
650 		   const __le16 *nvm_hw, const __le16 *nvm_sw,
651 		   const __le16 *nvm_calib, const __le16 *regulatory,
652 		   const __le16 *mac_override, const __le16 *phy_sku,
653 		   u8 tx_chains, u8 rx_chains, bool lar_fw_supported)
654 {
655 	struct device *dev = trans->dev;
656 	struct iwl_nvm_data *data;
657 	bool lar_enabled;
658 	u32 sku, radio_cfg;
659 	u16 lar_config;
660 	const __le16 *ch_section;
661 
662 	if (!cfg->ext_nvm)
663 		data = kzalloc(sizeof(*data) +
664 			       sizeof(struct ieee80211_channel) *
665 			       IWL_NUM_CHANNELS,
666 			       GFP_KERNEL);
667 	else
668 		data = kzalloc(sizeof(*data) +
669 			       sizeof(struct ieee80211_channel) *
670 			       IWL_NUM_CHANNELS_EXT,
671 			       GFP_KERNEL);
672 	if (!data)
673 		return NULL;
674 
675 	data->nvm_version = iwl_get_nvm_version(cfg, nvm_sw);
676 
677 	radio_cfg = iwl_get_radio_cfg(cfg, nvm_sw, phy_sku);
678 	iwl_set_radio_cfg(cfg, data, radio_cfg);
679 	if (data->valid_tx_ant)
680 		tx_chains &= data->valid_tx_ant;
681 	if (data->valid_rx_ant)
682 		rx_chains &= data->valid_rx_ant;
683 
684 	sku = iwl_get_sku(cfg, nvm_sw, phy_sku);
685 	data->sku_cap_band_24GHz_enable = sku & NVM_SKU_CAP_BAND_24GHZ;
686 	data->sku_cap_band_52GHz_enable = sku & NVM_SKU_CAP_BAND_52GHZ;
687 	data->sku_cap_11n_enable = sku & NVM_SKU_CAP_11N_ENABLE;
688 	if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_ALL)
689 		data->sku_cap_11n_enable = false;
690 	data->sku_cap_11ac_enable = data->sku_cap_11n_enable &&
691 				    (sku & NVM_SKU_CAP_11AC_ENABLE);
692 	data->sku_cap_mimo_disabled = sku & NVM_SKU_CAP_MIMO_DISABLE;
693 
694 	data->n_hw_addrs = iwl_get_n_hw_addrs(cfg, nvm_sw);
695 
696 	if (!cfg->ext_nvm) {
697 		/* Checking for required sections */
698 		if (!nvm_calib) {
699 			IWL_ERR(trans,
700 				"Can't parse empty Calib NVM sections\n");
701 			kfree(data);
702 			return NULL;
703 		}
704 		/* in family 8000 Xtal calibration values moved to OTP */
705 		data->xtal_calib[0] = *(nvm_calib + XTAL_CALIB);
706 		data->xtal_calib[1] = *(nvm_calib + XTAL_CALIB + 1);
707 		lar_enabled = true;
708 		ch_section = &nvm_sw[NVM_CHANNELS];
709 	} else {
710 		u16 lar_offset = data->nvm_version < 0xE39 ?
711 				 NVM_LAR_OFFSET_OLD :
712 				 NVM_LAR_OFFSET;
713 
714 		lar_config = le16_to_cpup(regulatory + lar_offset);
715 		data->lar_enabled = !!(lar_config &
716 				       NVM_LAR_ENABLED);
717 		lar_enabled = data->lar_enabled;
718 		ch_section = &regulatory[NVM_CHANNELS_EXTENDED];
719 	}
720 
721 	/* If no valid mac address was found - bail out */
722 	if (iwl_set_hw_address(trans, cfg, data, nvm_hw, mac_override)) {
723 		kfree(data);
724 		return NULL;
725 	}
726 
727 	iwl_init_sbands(dev, cfg, data, ch_section, tx_chains, rx_chains,
728 			lar_fw_supported && lar_enabled);
729 	data->calib_version = 255;
730 
731 	return data;
732 }
733 IWL_EXPORT_SYMBOL(iwl_parse_nvm_data);
734 
735 static u32 iwl_nvm_get_regdom_bw_flags(const u8 *nvm_chan,
736 				       int ch_idx, u16 nvm_flags,
737 				       const struct iwl_cfg *cfg)
738 {
739 	u32 flags = NL80211_RRF_NO_HT40;
740 	u32 last_5ghz_ht = LAST_5GHZ_HT;
741 
742 	if (cfg->ext_nvm)
743 		last_5ghz_ht = LAST_5GHZ_HT_FAMILY_8000;
744 
745 	if (ch_idx < NUM_2GHZ_CHANNELS &&
746 	    (nvm_flags & NVM_CHANNEL_40MHZ)) {
747 		if (nvm_chan[ch_idx] <= LAST_2GHZ_HT_PLUS)
748 			flags &= ~NL80211_RRF_NO_HT40PLUS;
749 		if (nvm_chan[ch_idx] >= FIRST_2GHZ_HT_MINUS)
750 			flags &= ~NL80211_RRF_NO_HT40MINUS;
751 	} else if (nvm_chan[ch_idx] <= last_5ghz_ht &&
752 		   (nvm_flags & NVM_CHANNEL_40MHZ)) {
753 		if ((ch_idx - NUM_2GHZ_CHANNELS) % 2 == 0)
754 			flags &= ~NL80211_RRF_NO_HT40PLUS;
755 		else
756 			flags &= ~NL80211_RRF_NO_HT40MINUS;
757 	}
758 
759 	if (!(nvm_flags & NVM_CHANNEL_80MHZ))
760 		flags |= NL80211_RRF_NO_80MHZ;
761 	if (!(nvm_flags & NVM_CHANNEL_160MHZ))
762 		flags |= NL80211_RRF_NO_160MHZ;
763 
764 	if (!(nvm_flags & NVM_CHANNEL_ACTIVE))
765 		flags |= NL80211_RRF_NO_IR;
766 
767 	if (nvm_flags & NVM_CHANNEL_RADAR)
768 		flags |= NL80211_RRF_DFS;
769 
770 	if (nvm_flags & NVM_CHANNEL_INDOOR_ONLY)
771 		flags |= NL80211_RRF_NO_OUTDOOR;
772 
773 	/* Set the GO concurrent flag only in case that NO_IR is set.
774 	 * Otherwise it is meaningless
775 	 */
776 	if ((nvm_flags & NVM_CHANNEL_GO_CONCURRENT) &&
777 	    (flags & NL80211_RRF_NO_IR))
778 		flags |= NL80211_RRF_GO_CONCURRENT;
779 
780 	return flags;
781 }
782 
783 struct ieee80211_regdomain *
784 iwl_parse_nvm_mcc_info(struct device *dev, const struct iwl_cfg *cfg,
785 		       int num_of_ch, __le32 *channels, u16 fw_mcc)
786 {
787 	int ch_idx;
788 	u16 ch_flags, prev_ch_flags = 0;
789 	const u8 *nvm_chan = cfg->ext_nvm ?
790 			     iwl_ext_nvm_channels : iwl_nvm_channels;
791 	struct ieee80211_regdomain *regd;
792 	int size_of_regd;
793 	struct ieee80211_reg_rule *rule;
794 	enum nl80211_band band;
795 	int center_freq, prev_center_freq = 0;
796 	int valid_rules = 0;
797 	bool new_rule;
798 	int max_num_ch = cfg->ext_nvm ?
799 			 IWL_NUM_CHANNELS_EXT : IWL_NUM_CHANNELS;
800 
801 	if (WARN_ON_ONCE(num_of_ch > NL80211_MAX_SUPP_REG_RULES))
802 		return ERR_PTR(-EINVAL);
803 
804 	if (WARN_ON(num_of_ch > max_num_ch))
805 		num_of_ch = max_num_ch;
806 
807 	IWL_DEBUG_DEV(dev, IWL_DL_LAR, "building regdom for %d channels\n",
808 		      num_of_ch);
809 
810 	/* build a regdomain rule for every valid channel */
811 	size_of_regd =
812 		sizeof(struct ieee80211_regdomain) +
813 		num_of_ch * sizeof(struct ieee80211_reg_rule);
814 
815 	regd = kzalloc(size_of_regd, GFP_KERNEL);
816 	if (!regd)
817 		return ERR_PTR(-ENOMEM);
818 
819 	for (ch_idx = 0; ch_idx < num_of_ch; ch_idx++) {
820 		ch_flags = (u16)__le32_to_cpup(channels + ch_idx);
821 		band = (ch_idx < NUM_2GHZ_CHANNELS) ?
822 		       NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
823 		center_freq = ieee80211_channel_to_frequency(nvm_chan[ch_idx],
824 							     band);
825 		new_rule = false;
826 
827 		if (!(ch_flags & NVM_CHANNEL_VALID)) {
828 			IWL_DEBUG_DEV(dev, IWL_DL_LAR,
829 				      "Ch. %d Flags %x [%sGHz] - No traffic\n",
830 				      nvm_chan[ch_idx],
831 				      ch_flags,
832 				      (ch_idx >= NUM_2GHZ_CHANNELS) ?
833 				      "5.2" : "2.4");
834 			continue;
835 		}
836 
837 		/* we can't continue the same rule */
838 		if (ch_idx == 0 || prev_ch_flags != ch_flags ||
839 		    center_freq - prev_center_freq > 20) {
840 			valid_rules++;
841 			new_rule = true;
842 		}
843 
844 		rule = &regd->reg_rules[valid_rules - 1];
845 
846 		if (new_rule)
847 			rule->freq_range.start_freq_khz =
848 						MHZ_TO_KHZ(center_freq - 10);
849 
850 		rule->freq_range.end_freq_khz = MHZ_TO_KHZ(center_freq + 10);
851 
852 		/* this doesn't matter - not used by FW */
853 		rule->power_rule.max_antenna_gain = DBI_TO_MBI(6);
854 		rule->power_rule.max_eirp =
855 			DBM_TO_MBM(IWL_DEFAULT_MAX_TX_POWER);
856 
857 		rule->flags = iwl_nvm_get_regdom_bw_flags(nvm_chan, ch_idx,
858 							  ch_flags, cfg);
859 
860 		/* rely on auto-calculation to merge BW of contiguous chans */
861 		rule->flags |= NL80211_RRF_AUTO_BW;
862 		rule->freq_range.max_bandwidth_khz = 0;
863 
864 		prev_ch_flags = ch_flags;
865 		prev_center_freq = center_freq;
866 
867 		IWL_DEBUG_DEV(dev, IWL_DL_LAR,
868 			      "Ch. %d [%sGHz] %s%s%s%s%s%s%s%s%s(0x%02x): Ad-Hoc %ssupported\n",
869 			      center_freq,
870 			      band == NL80211_BAND_5GHZ ? "5.2" : "2.4",
871 			      CHECK_AND_PRINT_I(VALID),
872 			      CHECK_AND_PRINT_I(ACTIVE),
873 			      CHECK_AND_PRINT_I(RADAR),
874 			      CHECK_AND_PRINT_I(WIDE),
875 			      CHECK_AND_PRINT_I(40MHZ),
876 			      CHECK_AND_PRINT_I(80MHZ),
877 			      CHECK_AND_PRINT_I(160MHZ),
878 			      CHECK_AND_PRINT_I(INDOOR_ONLY),
879 			      CHECK_AND_PRINT_I(GO_CONCURRENT),
880 			      ch_flags,
881 			      ((ch_flags & NVM_CHANNEL_ACTIVE) &&
882 			       !(ch_flags & NVM_CHANNEL_RADAR))
883 					 ? "" : "not ");
884 	}
885 
886 	regd->n_reg_rules = valid_rules;
887 
888 	/* set alpha2 from FW. */
889 	regd->alpha2[0] = fw_mcc >> 8;
890 	regd->alpha2[1] = fw_mcc & 0xff;
891 
892 	return regd;
893 }
894 IWL_EXPORT_SYMBOL(iwl_parse_nvm_mcc_info);
895 
896 #ifdef CONFIG_ACPI
897 #define WRDD_METHOD		"WRDD"
898 #define WRDD_WIFI		(0x07)
899 #define WRDD_WIGIG		(0x10)
900 
901 static u32 iwl_wrdd_get_mcc(struct device *dev, union acpi_object *wrdd)
902 {
903 	union acpi_object *mcc_pkg, *domain_type, *mcc_value;
904 	u32 i;
905 
906 	if (wrdd->type != ACPI_TYPE_PACKAGE ||
907 	    wrdd->package.count < 2 ||
908 	    wrdd->package.elements[0].type != ACPI_TYPE_INTEGER ||
909 	    wrdd->package.elements[0].integer.value != 0) {
910 		IWL_DEBUG_EEPROM(dev, "Unsupported wrdd structure\n");
911 		return 0;
912 	}
913 
914 	for (i = 1 ; i < wrdd->package.count ; ++i) {
915 		mcc_pkg = &wrdd->package.elements[i];
916 
917 		if (mcc_pkg->type != ACPI_TYPE_PACKAGE ||
918 		    mcc_pkg->package.count < 2 ||
919 		    mcc_pkg->package.elements[0].type != ACPI_TYPE_INTEGER ||
920 		    mcc_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) {
921 			mcc_pkg = NULL;
922 			continue;
923 		}
924 
925 		domain_type = &mcc_pkg->package.elements[0];
926 		if (domain_type->integer.value == WRDD_WIFI)
927 			break;
928 
929 		mcc_pkg = NULL;
930 	}
931 
932 	if (mcc_pkg) {
933 		mcc_value = &mcc_pkg->package.elements[1];
934 		return mcc_value->integer.value;
935 	}
936 
937 	return 0;
938 }
939 
940 int iwl_get_bios_mcc(struct device *dev, char *mcc)
941 {
942 	acpi_handle root_handle;
943 	acpi_handle handle;
944 	struct acpi_buffer wrdd = {ACPI_ALLOCATE_BUFFER, NULL};
945 	acpi_status status;
946 	u32 mcc_val;
947 
948 	root_handle = ACPI_HANDLE(dev);
949 	if (!root_handle) {
950 		IWL_DEBUG_EEPROM(dev,
951 				 "Could not retrieve root port ACPI handle\n");
952 		return -ENOENT;
953 	}
954 
955 	/* Get the method's handle */
956 	status = acpi_get_handle(root_handle, (acpi_string)WRDD_METHOD,
957 				 &handle);
958 	if (ACPI_FAILURE(status)) {
959 		IWL_DEBUG_EEPROM(dev, "WRD method not found\n");
960 		return -ENOENT;
961 	}
962 
963 	/* Call WRDD with no arguments */
964 	status = acpi_evaluate_object(handle, NULL, NULL, &wrdd);
965 	if (ACPI_FAILURE(status)) {
966 		IWL_DEBUG_EEPROM(dev, "WRDC invocation failed (0x%x)\n",
967 				 status);
968 		return -ENOENT;
969 	}
970 
971 	mcc_val = iwl_wrdd_get_mcc(dev, wrdd.pointer);
972 	kfree(wrdd.pointer);
973 	if (!mcc_val)
974 		return -ENOENT;
975 
976 	mcc[0] = (mcc_val >> 8) & 0xff;
977 	mcc[1] = mcc_val & 0xff;
978 	mcc[2] = '\0';
979 	return 0;
980 }
981 IWL_EXPORT_SYMBOL(iwl_get_bios_mcc);
982 #endif
983