xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/nvm.c (revision 6fa52430f0b3a45a31fb706084288884532e857c)
1e705c121SKalle Valo /******************************************************************************
2e705c121SKalle Valo  *
3e705c121SKalle Valo  * This file is provided under a dual BSD/GPLv2 license.  When using or
4e705c121SKalle Valo  * redistributing this file, you may do so under either license.
5e705c121SKalle Valo  *
6e705c121SKalle Valo  * GPL LICENSE SUMMARY
7e705c121SKalle Valo  *
8e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10*6fa52430SMatti Gottlieb  * Copyright(c) 2016        Intel Deutschland GmbH
11e705c121SKalle Valo  *
12e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
13e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
14e705c121SKalle Valo  * published by the Free Software Foundation.
15e705c121SKalle Valo  *
16e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
17e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
18e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19e705c121SKalle Valo  * General Public License for more details.
20e705c121SKalle Valo  *
21e705c121SKalle Valo  * You should have received a copy of the GNU General Public License
22e705c121SKalle Valo  * along with this program; if not, write to the Free Software
23e705c121SKalle Valo  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24e705c121SKalle Valo  * USA
25e705c121SKalle Valo  *
26e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
27e705c121SKalle Valo  * in the file called COPYING.
28e705c121SKalle Valo  *
29e705c121SKalle Valo  * Contact Information:
30cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
31e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32e705c121SKalle Valo  *
33e705c121SKalle Valo  * BSD LICENSE
34e705c121SKalle Valo  *
35e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37*6fa52430SMatti Gottlieb  * Copyright(c) 2016        Intel Deutschland GmbH
38e705c121SKalle Valo  * All rights reserved.
39e705c121SKalle Valo  *
40e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
41e705c121SKalle Valo  * modification, are permitted provided that the following conditions
42e705c121SKalle Valo  * are met:
43e705c121SKalle Valo  *
44e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
45e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
46e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
47e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
48e705c121SKalle Valo  *    the documentation and/or other materials provided with the
49e705c121SKalle Valo  *    distribution.
50e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
51e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
52e705c121SKalle Valo  *    from this software without specific prior written permission.
53e705c121SKalle Valo  *
54e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65e705c121SKalle Valo  *
66e705c121SKalle Valo  *****************************************************************************/
67e705c121SKalle Valo #include <linux/firmware.h>
68e705c121SKalle Valo #include <linux/rtnetlink.h>
69e705c121SKalle Valo #include <linux/pci.h>
70e705c121SKalle Valo #include <linux/acpi.h>
71e705c121SKalle Valo #include "iwl-trans.h"
72e705c121SKalle Valo #include "iwl-csr.h"
73e705c121SKalle Valo #include "mvm.h"
74e705c121SKalle Valo #include "iwl-eeprom-parse.h"
75e705c121SKalle Valo #include "iwl-eeprom-read.h"
76e705c121SKalle Valo #include "iwl-nvm-parse.h"
77e705c121SKalle Valo #include "iwl-prph.h"
78e705c121SKalle Valo 
79e705c121SKalle Valo /* Default NVM size to read */
80e705c121SKalle Valo #define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024)
81e705c121SKalle Valo #define IWL_MAX_NVM_SECTION_SIZE	0x1b58
82e705c121SKalle Valo #define IWL_MAX_NVM_8000_SECTION_SIZE	0x1ffc
83e705c121SKalle Valo 
84e705c121SKalle Valo #define NVM_WRITE_OPCODE 1
85e705c121SKalle Valo #define NVM_READ_OPCODE 0
86e705c121SKalle Valo 
87e705c121SKalle Valo /* load nvm chunk response */
88e705c121SKalle Valo enum {
89e705c121SKalle Valo 	READ_NVM_CHUNK_SUCCEED = 0,
90e705c121SKalle Valo 	READ_NVM_CHUNK_NOT_VALID_ADDRESS = 1
91e705c121SKalle Valo };
92e705c121SKalle Valo 
93e705c121SKalle Valo /*
94e705c121SKalle Valo  * prepare the NVM host command w/ the pointers to the nvm buffer
95e705c121SKalle Valo  * and send it to fw
96e705c121SKalle Valo  */
97e705c121SKalle Valo static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section,
98e705c121SKalle Valo 			       u16 offset, u16 length, const u8 *data)
99e705c121SKalle Valo {
100e705c121SKalle Valo 	struct iwl_nvm_access_cmd nvm_access_cmd = {
101e705c121SKalle Valo 		.offset = cpu_to_le16(offset),
102e705c121SKalle Valo 		.length = cpu_to_le16(length),
103e705c121SKalle Valo 		.type = cpu_to_le16(section),
104e705c121SKalle Valo 		.op_code = NVM_WRITE_OPCODE,
105e705c121SKalle Valo 	};
106e705c121SKalle Valo 	struct iwl_host_cmd cmd = {
107e705c121SKalle Valo 		.id = NVM_ACCESS_CMD,
108e705c121SKalle Valo 		.len = { sizeof(struct iwl_nvm_access_cmd), length },
1099a57f650SMatti Gottlieb 		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
110e705c121SKalle Valo 		.data = { &nvm_access_cmd, data },
111e705c121SKalle Valo 		/* data may come from vmalloc, so use _DUP */
112e705c121SKalle Valo 		.dataflags = { 0, IWL_HCMD_DFL_DUP },
113e705c121SKalle Valo 	};
1149a57f650SMatti Gottlieb 	struct iwl_rx_packet *pkt;
1159a57f650SMatti Gottlieb 	struct iwl_nvm_access_resp *nvm_resp;
1169a57f650SMatti Gottlieb 	int ret;
117e705c121SKalle Valo 
1189a57f650SMatti Gottlieb 	ret = iwl_mvm_send_cmd(mvm, &cmd);
1199a57f650SMatti Gottlieb 	if (ret)
1209a57f650SMatti Gottlieb 		return ret;
1219a57f650SMatti Gottlieb 
1229a57f650SMatti Gottlieb 	pkt = cmd.resp_pkt;
1239a57f650SMatti Gottlieb 	if (!pkt) {
1249a57f650SMatti Gottlieb 		IWL_ERR(mvm, "Error in NVM_ACCESS response\n");
1259a57f650SMatti Gottlieb 		return -EINVAL;
1269a57f650SMatti Gottlieb 	}
1279a57f650SMatti Gottlieb 	/* Extract & check NVM write response */
1289a57f650SMatti Gottlieb 	nvm_resp = (void *)pkt->data;
1299a57f650SMatti Gottlieb 	if (le16_to_cpu(nvm_resp->status) != READ_NVM_CHUNK_SUCCEED) {
1309a57f650SMatti Gottlieb 		IWL_ERR(mvm,
1319a57f650SMatti Gottlieb 			"NVM access write command failed for section %u (status = 0x%x)\n",
1329a57f650SMatti Gottlieb 			section, le16_to_cpu(nvm_resp->status));
1339a57f650SMatti Gottlieb 		ret = -EIO;
1349a57f650SMatti Gottlieb 	}
1359a57f650SMatti Gottlieb 
1369a57f650SMatti Gottlieb 	iwl_free_resp(&cmd);
1379a57f650SMatti Gottlieb 	return ret;
138e705c121SKalle Valo }
139e705c121SKalle Valo 
140e705c121SKalle Valo static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section,
141e705c121SKalle Valo 			      u16 offset, u16 length, u8 *data)
142e705c121SKalle Valo {
143e705c121SKalle Valo 	struct iwl_nvm_access_cmd nvm_access_cmd = {
144e705c121SKalle Valo 		.offset = cpu_to_le16(offset),
145e705c121SKalle Valo 		.length = cpu_to_le16(length),
146e705c121SKalle Valo 		.type = cpu_to_le16(section),
147e705c121SKalle Valo 		.op_code = NVM_READ_OPCODE,
148e705c121SKalle Valo 	};
149e705c121SKalle Valo 	struct iwl_nvm_access_resp *nvm_resp;
150e705c121SKalle Valo 	struct iwl_rx_packet *pkt;
151e705c121SKalle Valo 	struct iwl_host_cmd cmd = {
152e705c121SKalle Valo 		.id = NVM_ACCESS_CMD,
153e705c121SKalle Valo 		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
154e705c121SKalle Valo 		.data = { &nvm_access_cmd, },
155e705c121SKalle Valo 	};
156e705c121SKalle Valo 	int ret, bytes_read, offset_read;
157e705c121SKalle Valo 	u8 *resp_data;
158e705c121SKalle Valo 
159e705c121SKalle Valo 	cmd.len[0] = sizeof(struct iwl_nvm_access_cmd);
160e705c121SKalle Valo 
161e705c121SKalle Valo 	ret = iwl_mvm_send_cmd(mvm, &cmd);
162e705c121SKalle Valo 	if (ret)
163e705c121SKalle Valo 		return ret;
164e705c121SKalle Valo 
165e705c121SKalle Valo 	pkt = cmd.resp_pkt;
166e705c121SKalle Valo 
167e705c121SKalle Valo 	/* Extract NVM response */
168e705c121SKalle Valo 	nvm_resp = (void *)pkt->data;
169e705c121SKalle Valo 	ret = le16_to_cpu(nvm_resp->status);
170e705c121SKalle Valo 	bytes_read = le16_to_cpu(nvm_resp->length);
171e705c121SKalle Valo 	offset_read = le16_to_cpu(nvm_resp->offset);
172e705c121SKalle Valo 	resp_data = nvm_resp->data;
173e705c121SKalle Valo 	if (ret) {
174e705c121SKalle Valo 		if ((offset != 0) &&
175e705c121SKalle Valo 		    (ret == READ_NVM_CHUNK_NOT_VALID_ADDRESS)) {
176e705c121SKalle Valo 			/*
177e705c121SKalle Valo 			 * meaning of NOT_VALID_ADDRESS:
178e705c121SKalle Valo 			 * driver try to read chunk from address that is
179e705c121SKalle Valo 			 * multiple of 2K and got an error since addr is empty.
180e705c121SKalle Valo 			 * meaning of (offset != 0): driver already
181e705c121SKalle Valo 			 * read valid data from another chunk so this case
182e705c121SKalle Valo 			 * is not an error.
183e705c121SKalle Valo 			 */
184e705c121SKalle Valo 			IWL_DEBUG_EEPROM(mvm->trans->dev,
185e705c121SKalle Valo 					 "NVM access command failed on offset 0x%x since that section size is multiple 2K\n",
186e705c121SKalle Valo 					 offset);
187e705c121SKalle Valo 			ret = 0;
188e705c121SKalle Valo 		} else {
189e705c121SKalle Valo 			IWL_DEBUG_EEPROM(mvm->trans->dev,
190e705c121SKalle Valo 					 "NVM access command failed with status %d (device: %s)\n",
191e705c121SKalle Valo 					 ret, mvm->cfg->name);
192e705c121SKalle Valo 			ret = -EIO;
193e705c121SKalle Valo 		}
194e705c121SKalle Valo 		goto exit;
195e705c121SKalle Valo 	}
196e705c121SKalle Valo 
197e705c121SKalle Valo 	if (offset_read != offset) {
198e705c121SKalle Valo 		IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n",
199e705c121SKalle Valo 			offset_read);
200e705c121SKalle Valo 		ret = -EINVAL;
201e705c121SKalle Valo 		goto exit;
202e705c121SKalle Valo 	}
203e705c121SKalle Valo 
204e705c121SKalle Valo 	/* Write data to NVM */
205e705c121SKalle Valo 	memcpy(data + offset, resp_data, bytes_read);
206e705c121SKalle Valo 	ret = bytes_read;
207e705c121SKalle Valo 
208e705c121SKalle Valo exit:
209e705c121SKalle Valo 	iwl_free_resp(&cmd);
210e705c121SKalle Valo 	return ret;
211e705c121SKalle Valo }
212e705c121SKalle Valo 
213e705c121SKalle Valo static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section,
214e705c121SKalle Valo 				 const u8 *data, u16 length)
215e705c121SKalle Valo {
216e705c121SKalle Valo 	int offset = 0;
217e705c121SKalle Valo 
218e705c121SKalle Valo 	/* copy data in chunks of 2k (and remainder if any) */
219e705c121SKalle Valo 
220e705c121SKalle Valo 	while (offset < length) {
221e705c121SKalle Valo 		int chunk_size, ret;
222e705c121SKalle Valo 
223e705c121SKalle Valo 		chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE,
224e705c121SKalle Valo 				 length - offset);
225e705c121SKalle Valo 
226e705c121SKalle Valo 		ret = iwl_nvm_write_chunk(mvm, section, offset,
227e705c121SKalle Valo 					  chunk_size, data + offset);
228e705c121SKalle Valo 		if (ret < 0)
229e705c121SKalle Valo 			return ret;
230e705c121SKalle Valo 
231e705c121SKalle Valo 		offset += chunk_size;
232e705c121SKalle Valo 	}
233e705c121SKalle Valo 
234e705c121SKalle Valo 	return 0;
235e705c121SKalle Valo }
236e705c121SKalle Valo 
2377d162045SJohannes Berg static void iwl_mvm_nvm_fixups(struct iwl_mvm *mvm, unsigned int section,
2387d162045SJohannes Berg 			       u8 *data, unsigned int len)
2397d162045SJohannes Berg {
2407d162045SJohannes Berg #define IWL_4165_DEVICE_ID	0x5501
2417d162045SJohannes Berg #define NVM_SKU_CAP_MIMO_DISABLE BIT(5)
2427d162045SJohannes Berg 
2437d162045SJohannes Berg 	if (section == NVM_SECTION_TYPE_PHY_SKU &&
2447d162045SJohannes Berg 	    mvm->trans->hw_id == IWL_4165_DEVICE_ID && data && len >= 5 &&
2457d162045SJohannes Berg 	    (data[4] & NVM_SKU_CAP_MIMO_DISABLE))
2467d162045SJohannes Berg 		/* OTP 0x52 bug work around: it's a 1x1 device */
2477d162045SJohannes Berg 		data[3] = ANT_B | (ANT_B << 4);
2487d162045SJohannes Berg }
2497d162045SJohannes Berg 
250e705c121SKalle Valo /*
251e705c121SKalle Valo  * Reads an NVM section completely.
252e705c121SKalle Valo  * NICs prior to 7000 family doesn't have a real NVM, but just read
253e705c121SKalle Valo  * section 0 which is the EEPROM. Because the EEPROM reading is unlimited
254e705c121SKalle Valo  * by uCode, we need to manually check in this case that we don't
255e705c121SKalle Valo  * overflow and try to read more than the EEPROM size.
256e705c121SKalle Valo  * For 7000 family NICs, we supply the maximal size we can read, and
257e705c121SKalle Valo  * the uCode fills the response with as much data as we can,
258e705c121SKalle Valo  * without overflowing, so no check is needed.
259e705c121SKalle Valo  */
260e705c121SKalle Valo static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section,
261e705c121SKalle Valo 				u8 *data, u32 size_read)
262e705c121SKalle Valo {
263e705c121SKalle Valo 	u16 length, offset = 0;
264e705c121SKalle Valo 	int ret;
265e705c121SKalle Valo 
266e705c121SKalle Valo 	/* Set nvm section read length */
267e705c121SKalle Valo 	length = IWL_NVM_DEFAULT_CHUNK_SIZE;
268e705c121SKalle Valo 
269e705c121SKalle Valo 	ret = length;
270e705c121SKalle Valo 
271e705c121SKalle Valo 	/* Read the NVM until exhausted (reading less than requested) */
272e705c121SKalle Valo 	while (ret == length) {
273e705c121SKalle Valo 		/* Check no memory assumptions fail and cause an overflow */
274e705c121SKalle Valo 		if ((size_read + offset + length) >
275e705c121SKalle Valo 		    mvm->cfg->base_params->eeprom_size) {
276e705c121SKalle Valo 			IWL_ERR(mvm, "EEPROM size is too small for NVM\n");
277e705c121SKalle Valo 			return -ENOBUFS;
278e705c121SKalle Valo 		}
279e705c121SKalle Valo 
280e705c121SKalle Valo 		ret = iwl_nvm_read_chunk(mvm, section, offset, length, data);
281e705c121SKalle Valo 		if (ret < 0) {
282e705c121SKalle Valo 			IWL_DEBUG_EEPROM(mvm->trans->dev,
283e705c121SKalle Valo 					 "Cannot read NVM from section %d offset %d, length %d\n",
284e705c121SKalle Valo 					 section, offset, length);
285e705c121SKalle Valo 			return ret;
286e705c121SKalle Valo 		}
287e705c121SKalle Valo 		offset += ret;
288e705c121SKalle Valo 	}
289e705c121SKalle Valo 
2907d162045SJohannes Berg 	iwl_mvm_nvm_fixups(mvm, section, data, offset);
2917d162045SJohannes Berg 
292e705c121SKalle Valo 	IWL_DEBUG_EEPROM(mvm->trans->dev,
293e705c121SKalle Valo 			 "NVM section %d read completed\n", section);
294e705c121SKalle Valo 	return offset;
295e705c121SKalle Valo }
296e705c121SKalle Valo 
297e705c121SKalle Valo static struct iwl_nvm_data *
298e705c121SKalle Valo iwl_parse_nvm_sections(struct iwl_mvm *mvm)
299e705c121SKalle Valo {
300e705c121SKalle Valo 	struct iwl_nvm_section *sections = mvm->nvm_sections;
301e705c121SKalle Valo 	const __le16 *hw, *sw, *calib, *regulatory, *mac_override, *phy_sku;
302e705c121SKalle Valo 	bool lar_enabled;
303e705c121SKalle Valo 	u32 mac_addr0, mac_addr1;
304e705c121SKalle Valo 
305e705c121SKalle Valo 	/* Checking for required sections */
306e705c121SKalle Valo 	if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
307e705c121SKalle Valo 		if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
308e705c121SKalle Valo 		    !mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data) {
309e705c121SKalle Valo 			IWL_ERR(mvm, "Can't parse empty OTP/NVM sections\n");
310e705c121SKalle Valo 			return NULL;
311e705c121SKalle Valo 		}
312e705c121SKalle Valo 	} else {
313e705c121SKalle Valo 		/* SW and REGULATORY sections are mandatory */
314e705c121SKalle Valo 		if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
315e705c121SKalle Valo 		    !mvm->nvm_sections[NVM_SECTION_TYPE_REGULATORY].data) {
316e705c121SKalle Valo 			IWL_ERR(mvm,
317e705c121SKalle Valo 				"Can't parse empty family 8000 OTP/NVM sections\n");
318e705c121SKalle Valo 			return NULL;
319e705c121SKalle Valo 		}
320e705c121SKalle Valo 		/* MAC_OVERRIDE or at least HW section must exist */
321e705c121SKalle Valo 		if (!mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data &&
322e705c121SKalle Valo 		    !mvm->nvm_sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data) {
323e705c121SKalle Valo 			IWL_ERR(mvm,
324e705c121SKalle Valo 				"Can't parse mac_address, empty sections\n");
325e705c121SKalle Valo 			return NULL;
326e705c121SKalle Valo 		}
327e705c121SKalle Valo 
328e705c121SKalle Valo 		/* PHY_SKU section is mandatory in B0 */
329e705c121SKalle Valo 		if (!mvm->nvm_sections[NVM_SECTION_TYPE_PHY_SKU].data) {
330e705c121SKalle Valo 			IWL_ERR(mvm,
331e705c121SKalle Valo 				"Can't parse phy_sku in B0, empty sections\n");
332e705c121SKalle Valo 			return NULL;
333e705c121SKalle Valo 		}
334e705c121SKalle Valo 	}
335e705c121SKalle Valo 
336e705c121SKalle Valo 	if (WARN_ON(!mvm->cfg))
337e705c121SKalle Valo 		return NULL;
338e705c121SKalle Valo 
339e705c121SKalle Valo 	/* read the mac address from WFMP registers */
340e705c121SKalle Valo 	mac_addr0 = iwl_trans_read_prph(mvm->trans, WFMP_MAC_ADDR_0);
341e705c121SKalle Valo 	mac_addr1 = iwl_trans_read_prph(mvm->trans, WFMP_MAC_ADDR_1);
342e705c121SKalle Valo 
343e705c121SKalle Valo 	hw = (const __le16 *)sections[mvm->cfg->nvm_hw_section_num].data;
344e705c121SKalle Valo 	sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data;
345e705c121SKalle Valo 	calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data;
346e705c121SKalle Valo 	regulatory = (const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY].data;
347e705c121SKalle Valo 	mac_override =
348e705c121SKalle Valo 		(const __le16 *)sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data;
349e705c121SKalle Valo 	phy_sku = (const __le16 *)sections[NVM_SECTION_TYPE_PHY_SKU].data;
350e705c121SKalle Valo 
351e705c121SKalle Valo 	lar_enabled = !iwlwifi_mod_params.lar_disable &&
352e705c121SKalle Valo 		      fw_has_capa(&mvm->fw->ucode_capa,
353e705c121SKalle Valo 				  IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
354e705c121SKalle Valo 
355e705c121SKalle Valo 	return iwl_parse_nvm_data(mvm->trans->dev, mvm->cfg, hw, sw, calib,
356e705c121SKalle Valo 				  regulatory, mac_override, phy_sku,
357e705c121SKalle Valo 				  mvm->fw->valid_tx_ant, mvm->fw->valid_rx_ant,
3587d162045SJohannes Berg 				  lar_enabled, mac_addr0, mac_addr1);
359e705c121SKalle Valo }
360e705c121SKalle Valo 
361e705c121SKalle Valo #define MAX_NVM_FILE_LEN	16384
362e705c121SKalle Valo 
363e705c121SKalle Valo /*
364e705c121SKalle Valo  * Reads external NVM from a file into mvm->nvm_sections
365e705c121SKalle Valo  *
366e705c121SKalle Valo  * HOW TO CREATE THE NVM FILE FORMAT:
367e705c121SKalle Valo  * ------------------------------
368e705c121SKalle Valo  * 1. create hex file, format:
369e705c121SKalle Valo  *      3800 -> header
370e705c121SKalle Valo  *      0000 -> header
371e705c121SKalle Valo  *      5a40 -> data
372e705c121SKalle Valo  *
373e705c121SKalle Valo  *   rev - 6 bit (word1)
374e705c121SKalle Valo  *   len - 10 bit (word1)
375e705c121SKalle Valo  *   id - 4 bit (word2)
376e705c121SKalle Valo  *   rsv - 12 bit (word2)
377e705c121SKalle Valo  *
378e705c121SKalle Valo  * 2. flip 8bits with 8 bits per line to get the right NVM file format
379e705c121SKalle Valo  *
380e705c121SKalle Valo  * 3. create binary file from the hex file
381e705c121SKalle Valo  *
382e705c121SKalle Valo  * 4. save as "iNVM_xxx.bin" under /lib/firmware
383e705c121SKalle Valo  */
384e705c121SKalle Valo static int iwl_mvm_read_external_nvm(struct iwl_mvm *mvm)
385e705c121SKalle Valo {
386e705c121SKalle Valo 	int ret, section_size;
387e705c121SKalle Valo 	u16 section_id;
388e705c121SKalle Valo 	const struct firmware *fw_entry;
389e705c121SKalle Valo 	const struct {
390e705c121SKalle Valo 		__le16 word1;
391e705c121SKalle Valo 		__le16 word2;
392e705c121SKalle Valo 		u8 data[];
393e705c121SKalle Valo 	} *file_sec;
3947d162045SJohannes Berg 	const u8 *eof;
3957d162045SJohannes Berg 	u8 *temp;
396e705c121SKalle Valo 	int max_section_size;
397e705c121SKalle Valo 	const __le32 *dword_buff;
398e705c121SKalle Valo 
399e705c121SKalle Valo #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
400e705c121SKalle Valo #define NVM_WORD2_ID(x) (x >> 12)
401e705c121SKalle Valo #define NVM_WORD2_LEN_FAMILY_8000(x) (2 * ((x & 0xFF) << 8 | x >> 8))
402e705c121SKalle Valo #define NVM_WORD1_ID_FAMILY_8000(x) (x >> 4)
403e705c121SKalle Valo #define NVM_HEADER_0	(0x2A504C54)
404e705c121SKalle Valo #define NVM_HEADER_1	(0x4E564D2A)
405e705c121SKalle Valo #define NVM_HEADER_SIZE	(4 * sizeof(u32))
406e705c121SKalle Valo 
407e705c121SKalle Valo 	IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from external NVM\n");
408e705c121SKalle Valo 
409e705c121SKalle Valo 	/* Maximal size depends on HW family and step */
410e705c121SKalle Valo 	if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
411e705c121SKalle Valo 		max_section_size = IWL_MAX_NVM_SECTION_SIZE;
412e705c121SKalle Valo 	else
413e705c121SKalle Valo 		max_section_size = IWL_MAX_NVM_8000_SECTION_SIZE;
414e705c121SKalle Valo 
415e705c121SKalle Valo 	/*
416e705c121SKalle Valo 	 * Obtain NVM image via request_firmware. Since we already used
417e705c121SKalle Valo 	 * request_firmware_nowait() for the firmware binary load and only
418e705c121SKalle Valo 	 * get here after that we assume the NVM request can be satisfied
419e705c121SKalle Valo 	 * synchronously.
420e705c121SKalle Valo 	 */
421e705c121SKalle Valo 	ret = request_firmware(&fw_entry, mvm->nvm_file_name,
422e705c121SKalle Valo 			       mvm->trans->dev);
423e705c121SKalle Valo 	if (ret) {
424e705c121SKalle Valo 		IWL_ERR(mvm, "ERROR: %s isn't available %d\n",
425e705c121SKalle Valo 			mvm->nvm_file_name, ret);
426e705c121SKalle Valo 		return ret;
427e705c121SKalle Valo 	}
428e705c121SKalle Valo 
429e705c121SKalle Valo 	IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n",
430e705c121SKalle Valo 		 mvm->nvm_file_name, fw_entry->size);
431e705c121SKalle Valo 
432e705c121SKalle Valo 	if (fw_entry->size > MAX_NVM_FILE_LEN) {
433e705c121SKalle Valo 		IWL_ERR(mvm, "NVM file too large\n");
434e705c121SKalle Valo 		ret = -EINVAL;
435e705c121SKalle Valo 		goto out;
436e705c121SKalle Valo 	}
437e705c121SKalle Valo 
438e705c121SKalle Valo 	eof = fw_entry->data + fw_entry->size;
439e705c121SKalle Valo 	dword_buff = (__le32 *)fw_entry->data;
440e705c121SKalle Valo 
441e705c121SKalle Valo 	/* some NVM file will contain a header.
442e705c121SKalle Valo 	 * The header is identified by 2 dwords header as follow:
443e705c121SKalle Valo 	 * dword[0] = 0x2A504C54
444e705c121SKalle Valo 	 * dword[1] = 0x4E564D2A
445e705c121SKalle Valo 	 *
446e705c121SKalle Valo 	 * This header must be skipped when providing the NVM data to the FW.
447e705c121SKalle Valo 	 */
448e705c121SKalle Valo 	if (fw_entry->size > NVM_HEADER_SIZE &&
449e705c121SKalle Valo 	    dword_buff[0] == cpu_to_le32(NVM_HEADER_0) &&
450e705c121SKalle Valo 	    dword_buff[1] == cpu_to_le32(NVM_HEADER_1)) {
451e705c121SKalle Valo 		file_sec = (void *)(fw_entry->data + NVM_HEADER_SIZE);
452e705c121SKalle Valo 		IWL_INFO(mvm, "NVM Version %08X\n", le32_to_cpu(dword_buff[2]));
453e705c121SKalle Valo 		IWL_INFO(mvm, "NVM Manufacturing date %08X\n",
454e705c121SKalle Valo 			 le32_to_cpu(dword_buff[3]));
455e705c121SKalle Valo 
456e705c121SKalle Valo 		/* nvm file validation, dword_buff[2] holds the file version */
457e705c121SKalle Valo 		if ((CSR_HW_REV_STEP(mvm->trans->hw_rev) == SILICON_C_STEP &&
458e705c121SKalle Valo 		     le32_to_cpu(dword_buff[2]) < 0xE4A) ||
459e705c121SKalle Valo 		    (CSR_HW_REV_STEP(mvm->trans->hw_rev) == SILICON_B_STEP &&
460e705c121SKalle Valo 		     le32_to_cpu(dword_buff[2]) >= 0xE4A)) {
461e705c121SKalle Valo 			ret = -EFAULT;
462e705c121SKalle Valo 			goto out;
463e705c121SKalle Valo 		}
464e705c121SKalle Valo 	} else {
465e705c121SKalle Valo 		file_sec = (void *)fw_entry->data;
466e705c121SKalle Valo 	}
467e705c121SKalle Valo 
468e705c121SKalle Valo 	while (true) {
469e705c121SKalle Valo 		if (file_sec->data > eof) {
470e705c121SKalle Valo 			IWL_ERR(mvm,
471e705c121SKalle Valo 				"ERROR - NVM file too short for section header\n");
472e705c121SKalle Valo 			ret = -EINVAL;
473e705c121SKalle Valo 			break;
474e705c121SKalle Valo 		}
475e705c121SKalle Valo 
476e705c121SKalle Valo 		/* check for EOF marker */
477e705c121SKalle Valo 		if (!file_sec->word1 && !file_sec->word2) {
478e705c121SKalle Valo 			ret = 0;
479e705c121SKalle Valo 			break;
480e705c121SKalle Valo 		}
481e705c121SKalle Valo 
482e705c121SKalle Valo 		if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
483e705c121SKalle Valo 			section_size =
484e705c121SKalle Valo 				2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1));
485e705c121SKalle Valo 			section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2));
486e705c121SKalle Valo 		} else {
487e705c121SKalle Valo 			section_size = 2 * NVM_WORD2_LEN_FAMILY_8000(
488e705c121SKalle Valo 						le16_to_cpu(file_sec->word2));
489e705c121SKalle Valo 			section_id = NVM_WORD1_ID_FAMILY_8000(
490e705c121SKalle Valo 						le16_to_cpu(file_sec->word1));
491e705c121SKalle Valo 		}
492e705c121SKalle Valo 
493e705c121SKalle Valo 		if (section_size > max_section_size) {
494e705c121SKalle Valo 			IWL_ERR(mvm, "ERROR - section too large (%d)\n",
495e705c121SKalle Valo 				section_size);
496e705c121SKalle Valo 			ret = -EINVAL;
497e705c121SKalle Valo 			break;
498e705c121SKalle Valo 		}
499e705c121SKalle Valo 
500e705c121SKalle Valo 		if (!section_size) {
501e705c121SKalle Valo 			IWL_ERR(mvm, "ERROR - section empty\n");
502e705c121SKalle Valo 			ret = -EINVAL;
503e705c121SKalle Valo 			break;
504e705c121SKalle Valo 		}
505e705c121SKalle Valo 
506e705c121SKalle Valo 		if (file_sec->data + section_size > eof) {
507e705c121SKalle Valo 			IWL_ERR(mvm,
508e705c121SKalle Valo 				"ERROR - NVM file too short for section (%d bytes)\n",
509e705c121SKalle Valo 				section_size);
510e705c121SKalle Valo 			ret = -EINVAL;
511e705c121SKalle Valo 			break;
512e705c121SKalle Valo 		}
513e705c121SKalle Valo 
514e705c121SKalle Valo 		if (WARN(section_id >= NVM_MAX_NUM_SECTIONS,
515e705c121SKalle Valo 			 "Invalid NVM section ID %d\n", section_id)) {
516e705c121SKalle Valo 			ret = -EINVAL;
517e705c121SKalle Valo 			break;
518e705c121SKalle Valo 		}
519e705c121SKalle Valo 
520e705c121SKalle Valo 		temp = kmemdup(file_sec->data, section_size, GFP_KERNEL);
521e705c121SKalle Valo 		if (!temp) {
522e705c121SKalle Valo 			ret = -ENOMEM;
523e705c121SKalle Valo 			break;
524e705c121SKalle Valo 		}
5257d162045SJohannes Berg 
5267d162045SJohannes Berg 		iwl_mvm_nvm_fixups(mvm, section_id, temp, section_size);
5277d162045SJohannes Berg 
528e705c121SKalle Valo 		kfree(mvm->nvm_sections[section_id].data);
529e705c121SKalle Valo 		mvm->nvm_sections[section_id].data = temp;
530e705c121SKalle Valo 		mvm->nvm_sections[section_id].length = section_size;
531e705c121SKalle Valo 
532e705c121SKalle Valo 		/* advance to the next section */
533e705c121SKalle Valo 		file_sec = (void *)(file_sec->data + section_size);
534e705c121SKalle Valo 	}
535e705c121SKalle Valo out:
536e705c121SKalle Valo 	release_firmware(fw_entry);
537e705c121SKalle Valo 	return ret;
538e705c121SKalle Valo }
539e705c121SKalle Valo 
540e705c121SKalle Valo /* Loads the NVM data stored in mvm->nvm_sections into the NIC */
541e705c121SKalle Valo int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm)
542e705c121SKalle Valo {
543e705c121SKalle Valo 	int i, ret = 0;
544e705c121SKalle Valo 	struct iwl_nvm_section *sections = mvm->nvm_sections;
545e705c121SKalle Valo 
546e705c121SKalle Valo 	IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n");
547e705c121SKalle Valo 
548e705c121SKalle Valo 	for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) {
549e705c121SKalle Valo 		if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length)
550e705c121SKalle Valo 			continue;
551e705c121SKalle Valo 		ret = iwl_nvm_write_section(mvm, i, sections[i].data,
552e705c121SKalle Valo 					    sections[i].length);
553e705c121SKalle Valo 		if (ret < 0) {
554e705c121SKalle Valo 			IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);
555e705c121SKalle Valo 			break;
556e705c121SKalle Valo 		}
557e705c121SKalle Valo 	}
558e705c121SKalle Valo 	return ret;
559e705c121SKalle Valo }
560e705c121SKalle Valo 
561e705c121SKalle Valo int iwl_nvm_init(struct iwl_mvm *mvm, bool read_nvm_from_nic)
562e705c121SKalle Valo {
563e705c121SKalle Valo 	int ret, section;
564e705c121SKalle Valo 	u32 size_read = 0;
565e705c121SKalle Valo 	u8 *nvm_buffer, *temp;
566e705c121SKalle Valo 	const char *nvm_file_B = mvm->cfg->default_nvm_file_B_step;
567e705c121SKalle Valo 	const char *nvm_file_C = mvm->cfg->default_nvm_file_C_step;
568e705c121SKalle Valo 
569e705c121SKalle Valo 	if (WARN_ON_ONCE(mvm->cfg->nvm_hw_section_num >= NVM_MAX_NUM_SECTIONS))
570e705c121SKalle Valo 		return -EINVAL;
571e705c121SKalle Valo 
572e705c121SKalle Valo 	/* load NVM values from nic */
573e705c121SKalle Valo 	if (read_nvm_from_nic) {
574e705c121SKalle Valo 		/* Read From FW NVM */
575e705c121SKalle Valo 		IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");
576e705c121SKalle Valo 
577e705c121SKalle Valo 		nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size,
578e705c121SKalle Valo 				     GFP_KERNEL);
579e705c121SKalle Valo 		if (!nvm_buffer)
580e705c121SKalle Valo 			return -ENOMEM;
581e705c121SKalle Valo 		for (section = 0; section < NVM_MAX_NUM_SECTIONS; section++) {
582e705c121SKalle Valo 			/* we override the constness for initial read */
583e705c121SKalle Valo 			ret = iwl_nvm_read_section(mvm, section, nvm_buffer,
584e705c121SKalle Valo 						   size_read);
585e705c121SKalle Valo 			if (ret < 0)
586e705c121SKalle Valo 				continue;
587e705c121SKalle Valo 			size_read += ret;
588e705c121SKalle Valo 			temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
589e705c121SKalle Valo 			if (!temp) {
590e705c121SKalle Valo 				ret = -ENOMEM;
591e705c121SKalle Valo 				break;
592e705c121SKalle Valo 			}
5937d162045SJohannes Berg 
5947d162045SJohannes Berg 			iwl_mvm_nvm_fixups(mvm, section, temp, ret);
5957d162045SJohannes Berg 
596e705c121SKalle Valo 			mvm->nvm_sections[section].data = temp;
597e705c121SKalle Valo 			mvm->nvm_sections[section].length = ret;
598e705c121SKalle Valo 
599e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
600e705c121SKalle Valo 			switch (section) {
601e705c121SKalle Valo 			case NVM_SECTION_TYPE_SW:
602e705c121SKalle Valo 				mvm->nvm_sw_blob.data = temp;
603e705c121SKalle Valo 				mvm->nvm_sw_blob.size  = ret;
604e705c121SKalle Valo 				break;
605e705c121SKalle Valo 			case NVM_SECTION_TYPE_CALIBRATION:
606e705c121SKalle Valo 				mvm->nvm_calib_blob.data = temp;
607e705c121SKalle Valo 				mvm->nvm_calib_blob.size  = ret;
608e705c121SKalle Valo 				break;
609e705c121SKalle Valo 			case NVM_SECTION_TYPE_PRODUCTION:
610e705c121SKalle Valo 				mvm->nvm_prod_blob.data = temp;
611e705c121SKalle Valo 				mvm->nvm_prod_blob.size  = ret;
612e705c121SKalle Valo 				break;
613e705c121SKalle Valo 			case NVM_SECTION_TYPE_PHY_SKU:
614e705c121SKalle Valo 				mvm->nvm_phy_sku_blob.data = temp;
615e705c121SKalle Valo 				mvm->nvm_phy_sku_blob.size  = ret;
616e705c121SKalle Valo 				break;
617e705c121SKalle Valo 			default:
618e705c121SKalle Valo 				if (section == mvm->cfg->nvm_hw_section_num) {
619e705c121SKalle Valo 					mvm->nvm_hw_blob.data = temp;
620e705c121SKalle Valo 					mvm->nvm_hw_blob.size = ret;
621e705c121SKalle Valo 					break;
622e705c121SKalle Valo 				}
623e705c121SKalle Valo 			}
624e705c121SKalle Valo #endif
625e705c121SKalle Valo 		}
626e705c121SKalle Valo 		if (!size_read)
627e705c121SKalle Valo 			IWL_ERR(mvm, "OTP is blank\n");
628e705c121SKalle Valo 		kfree(nvm_buffer);
629e705c121SKalle Valo 	}
630e705c121SKalle Valo 
631e705c121SKalle Valo 	/* Only if PNVM selected in the mod param - load external NVM  */
632e705c121SKalle Valo 	if (mvm->nvm_file_name) {
633e705c121SKalle Valo 		/* read External NVM file from the mod param */
634e705c121SKalle Valo 		ret = iwl_mvm_read_external_nvm(mvm);
635e705c121SKalle Valo 		if (ret) {
636e705c121SKalle Valo 			/* choose the nvm_file name according to the
637e705c121SKalle Valo 			 * HW step
638e705c121SKalle Valo 			 */
639e705c121SKalle Valo 			if (CSR_HW_REV_STEP(mvm->trans->hw_rev) ==
640e705c121SKalle Valo 			    SILICON_B_STEP)
641e705c121SKalle Valo 				mvm->nvm_file_name = nvm_file_B;
642e705c121SKalle Valo 			else
643e705c121SKalle Valo 				mvm->nvm_file_name = nvm_file_C;
644e705c121SKalle Valo 
645e705c121SKalle Valo 			if (ret == -EFAULT && mvm->nvm_file_name) {
646e705c121SKalle Valo 				/* in case nvm file was failed try again */
647e705c121SKalle Valo 				ret = iwl_mvm_read_external_nvm(mvm);
648e705c121SKalle Valo 				if (ret)
649e705c121SKalle Valo 					return ret;
650e705c121SKalle Valo 			} else {
651e705c121SKalle Valo 				return ret;
652e705c121SKalle Valo 			}
653e705c121SKalle Valo 		}
654e705c121SKalle Valo 	}
655e705c121SKalle Valo 
656e705c121SKalle Valo 	/* parse the relevant nvm sections */
657e705c121SKalle Valo 	mvm->nvm_data = iwl_parse_nvm_sections(mvm);
658e705c121SKalle Valo 	if (!mvm->nvm_data)
659e705c121SKalle Valo 		return -ENODATA;
660e705c121SKalle Valo 	IWL_DEBUG_EEPROM(mvm->trans->dev, "nvm version = %x\n",
661e705c121SKalle Valo 			 mvm->nvm_data->nvm_version);
662e705c121SKalle Valo 
663e705c121SKalle Valo 	return 0;
664e705c121SKalle Valo }
665e705c121SKalle Valo 
666e705c121SKalle Valo struct iwl_mcc_update_resp *
667e705c121SKalle Valo iwl_mvm_update_mcc(struct iwl_mvm *mvm, const char *alpha2,
668e705c121SKalle Valo 		   enum iwl_mcc_source src_id)
669e705c121SKalle Valo {
670e705c121SKalle Valo 	struct iwl_mcc_update_cmd mcc_update_cmd = {
671e705c121SKalle Valo 		.mcc = cpu_to_le16(alpha2[0] << 8 | alpha2[1]),
672e705c121SKalle Valo 		.source_id = (u8)src_id,
673e705c121SKalle Valo 	};
674e705c121SKalle Valo 	struct iwl_mcc_update_resp *mcc_resp, *resp_cp = NULL;
675*6fa52430SMatti Gottlieb 	struct iwl_mcc_update_resp_v1 *mcc_resp_v1 = NULL;
676e705c121SKalle Valo 	struct iwl_rx_packet *pkt;
677e705c121SKalle Valo 	struct iwl_host_cmd cmd = {
678e705c121SKalle Valo 		.id = MCC_UPDATE_CMD,
679e705c121SKalle Valo 		.flags = CMD_WANT_SKB,
680e705c121SKalle Valo 		.data = { &mcc_update_cmd },
681e705c121SKalle Valo 	};
682e705c121SKalle Valo 
683e705c121SKalle Valo 	int ret;
684e705c121SKalle Valo 	u32 status;
685e705c121SKalle Valo 	int resp_len, n_channels;
686e705c121SKalle Valo 	u16 mcc;
687*6fa52430SMatti Gottlieb 	bool resp_v2 = fw_has_capa(&mvm->fw->ucode_capa,
688*6fa52430SMatti Gottlieb 				   IWL_UCODE_TLV_CAPA_LAR_SUPPORT_V2);
689e705c121SKalle Valo 
690e705c121SKalle Valo 	if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
691e705c121SKalle Valo 		return ERR_PTR(-EOPNOTSUPP);
692e705c121SKalle Valo 
693e705c121SKalle Valo 	cmd.len[0] = sizeof(struct iwl_mcc_update_cmd);
694*6fa52430SMatti Gottlieb 	if (!resp_v2)
695*6fa52430SMatti Gottlieb 		cmd.len[0] = sizeof(struct iwl_mcc_update_cmd_v1);
696e705c121SKalle Valo 
697e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "send MCC update to FW with '%c%c' src = %d\n",
698e705c121SKalle Valo 		      alpha2[0], alpha2[1], src_id);
699e705c121SKalle Valo 
700e705c121SKalle Valo 	ret = iwl_mvm_send_cmd(mvm, &cmd);
701e705c121SKalle Valo 	if (ret)
702e705c121SKalle Valo 		return ERR_PTR(ret);
703e705c121SKalle Valo 
704e705c121SKalle Valo 	pkt = cmd.resp_pkt;
705e705c121SKalle Valo 
706e705c121SKalle Valo 	/* Extract MCC response */
707*6fa52430SMatti Gottlieb 	if (resp_v2) {
708e705c121SKalle Valo 		mcc_resp = (void *)pkt->data;
709*6fa52430SMatti Gottlieb 		n_channels =  __le32_to_cpu(mcc_resp->n_channels);
710*6fa52430SMatti Gottlieb 	} else {
711*6fa52430SMatti Gottlieb 		mcc_resp_v1 = (void *)pkt->data;
712*6fa52430SMatti Gottlieb 		n_channels =  __le32_to_cpu(mcc_resp_v1->n_channels);
713e705c121SKalle Valo 	}
714e705c121SKalle Valo 
715*6fa52430SMatti Gottlieb 	resp_len = sizeof(struct iwl_mcc_update_resp) + n_channels *
716*6fa52430SMatti Gottlieb 		sizeof(__le32);
717e705c121SKalle Valo 
718*6fa52430SMatti Gottlieb 	resp_cp = kzalloc(resp_len, GFP_KERNEL);
719e705c121SKalle Valo 	if (!resp_cp) {
720e705c121SKalle Valo 		ret = -ENOMEM;
721e705c121SKalle Valo 		goto exit;
722e705c121SKalle Valo 	}
723e705c121SKalle Valo 
724*6fa52430SMatti Gottlieb 	if (resp_v2) {
725*6fa52430SMatti Gottlieb 		memcpy(resp_cp, mcc_resp, resp_len);
726*6fa52430SMatti Gottlieb 	} else {
727*6fa52430SMatti Gottlieb 		resp_cp->status = mcc_resp_v1->status;
728*6fa52430SMatti Gottlieb 		resp_cp->mcc = mcc_resp_v1->mcc;
729*6fa52430SMatti Gottlieb 		resp_cp->cap = mcc_resp_v1->cap;
730*6fa52430SMatti Gottlieb 		resp_cp->source_id = mcc_resp_v1->source_id;
731*6fa52430SMatti Gottlieb 		resp_cp->n_channels = mcc_resp_v1->n_channels;
732*6fa52430SMatti Gottlieb 		memcpy(resp_cp->channels, mcc_resp_v1->channels,
733*6fa52430SMatti Gottlieb 		       n_channels * sizeof(__le32));
734*6fa52430SMatti Gottlieb 	}
735*6fa52430SMatti Gottlieb 
736*6fa52430SMatti Gottlieb 	status = le32_to_cpu(resp_cp->status);
737*6fa52430SMatti Gottlieb 
738*6fa52430SMatti Gottlieb 	mcc = le16_to_cpu(resp_cp->mcc);
739*6fa52430SMatti Gottlieb 
740*6fa52430SMatti Gottlieb 	/* W/A for a FW/NVM issue - returns 0x00 for the world domain */
741*6fa52430SMatti Gottlieb 	if (mcc == 0) {
742*6fa52430SMatti Gottlieb 		mcc = 0x3030;  /* "00" - world */
743*6fa52430SMatti Gottlieb 		resp_cp->mcc = cpu_to_le16(mcc);
744*6fa52430SMatti Gottlieb 	}
745*6fa52430SMatti Gottlieb 
746*6fa52430SMatti Gottlieb 	IWL_DEBUG_LAR(mvm,
747*6fa52430SMatti Gottlieb 		      "MCC response status: 0x%x. new MCC: 0x%x ('%c%c') change: %d n_chans: %d\n",
748*6fa52430SMatti Gottlieb 		      status, mcc, mcc >> 8, mcc & 0xff,
749*6fa52430SMatti Gottlieb 		      !!(status == MCC_RESP_NEW_CHAN_PROFILE), n_channels);
750*6fa52430SMatti Gottlieb 
751e705c121SKalle Valo exit:
752e705c121SKalle Valo 	iwl_free_resp(&cmd);
753e705c121SKalle Valo 	if (ret)
754e705c121SKalle Valo 		return ERR_PTR(ret);
755e705c121SKalle Valo 	return resp_cp;
756e705c121SKalle Valo }
757e705c121SKalle Valo 
758e705c121SKalle Valo #ifdef CONFIG_ACPI
759e705c121SKalle Valo #define WRD_METHOD		"WRDD"
760e705c121SKalle Valo #define WRDD_WIFI		(0x07)
761e705c121SKalle Valo #define WRDD_WIGIG		(0x10)
762e705c121SKalle Valo 
763e705c121SKalle Valo static u32 iwl_mvm_wrdd_get_mcc(struct iwl_mvm *mvm, union acpi_object *wrdd)
764e705c121SKalle Valo {
765e705c121SKalle Valo 	union acpi_object *mcc_pkg, *domain_type, *mcc_value;
766e705c121SKalle Valo 	u32 i;
767e705c121SKalle Valo 
768e705c121SKalle Valo 	if (wrdd->type != ACPI_TYPE_PACKAGE ||
769e705c121SKalle Valo 	    wrdd->package.count < 2 ||
770e705c121SKalle Valo 	    wrdd->package.elements[0].type != ACPI_TYPE_INTEGER ||
771e705c121SKalle Valo 	    wrdd->package.elements[0].integer.value != 0) {
772e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Unsupported wrdd structure\n");
773e705c121SKalle Valo 		return 0;
774e705c121SKalle Valo 	}
775e705c121SKalle Valo 
776e705c121SKalle Valo 	for (i = 1 ; i < wrdd->package.count ; ++i) {
777e705c121SKalle Valo 		mcc_pkg = &wrdd->package.elements[i];
778e705c121SKalle Valo 
779e705c121SKalle Valo 		if (mcc_pkg->type != ACPI_TYPE_PACKAGE ||
780e705c121SKalle Valo 		    mcc_pkg->package.count < 2 ||
781e705c121SKalle Valo 		    mcc_pkg->package.elements[0].type != ACPI_TYPE_INTEGER ||
782e705c121SKalle Valo 		    mcc_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) {
783e705c121SKalle Valo 			mcc_pkg = NULL;
784e705c121SKalle Valo 			continue;
785e705c121SKalle Valo 		}
786e705c121SKalle Valo 
787e705c121SKalle Valo 		domain_type = &mcc_pkg->package.elements[0];
788e705c121SKalle Valo 		if (domain_type->integer.value == WRDD_WIFI)
789e705c121SKalle Valo 			break;
790e705c121SKalle Valo 
791e705c121SKalle Valo 		mcc_pkg = NULL;
792e705c121SKalle Valo 	}
793e705c121SKalle Valo 
794e705c121SKalle Valo 	if (mcc_pkg) {
795e705c121SKalle Valo 		mcc_value = &mcc_pkg->package.elements[1];
796e705c121SKalle Valo 		return mcc_value->integer.value;
797e705c121SKalle Valo 	}
798e705c121SKalle Valo 
799e705c121SKalle Valo 	return 0;
800e705c121SKalle Valo }
801e705c121SKalle Valo 
802e705c121SKalle Valo static int iwl_mvm_get_bios_mcc(struct iwl_mvm *mvm, char *mcc)
803e705c121SKalle Valo {
804e705c121SKalle Valo 	acpi_handle root_handle;
805e705c121SKalle Valo 	acpi_handle handle;
806e705c121SKalle Valo 	struct acpi_buffer wrdd = {ACPI_ALLOCATE_BUFFER, NULL};
807e705c121SKalle Valo 	acpi_status status;
808e705c121SKalle Valo 	u32 mcc_val;
809e705c121SKalle Valo 	struct pci_dev *pdev = to_pci_dev(mvm->dev);
810e705c121SKalle Valo 
811e705c121SKalle Valo 	root_handle = ACPI_HANDLE(&pdev->dev);
812e705c121SKalle Valo 	if (!root_handle) {
813e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm,
814e705c121SKalle Valo 			      "Could not retrieve root port ACPI handle\n");
815e705c121SKalle Valo 		return -ENOENT;
816e705c121SKalle Valo 	}
817e705c121SKalle Valo 
818e705c121SKalle Valo 	/* Get the method's handle */
819e705c121SKalle Valo 	status = acpi_get_handle(root_handle, (acpi_string)WRD_METHOD, &handle);
820e705c121SKalle Valo 	if (ACPI_FAILURE(status)) {
821e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "WRD method not found\n");
822e705c121SKalle Valo 		return -ENOENT;
823e705c121SKalle Valo 	}
824e705c121SKalle Valo 
825e705c121SKalle Valo 	/* Call WRDD with no arguments */
826e705c121SKalle Valo 	status = acpi_evaluate_object(handle, NULL, NULL, &wrdd);
827e705c121SKalle Valo 	if (ACPI_FAILURE(status)) {
828e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "WRDC invocation failed (0x%x)\n", status);
829e705c121SKalle Valo 		return -ENOENT;
830e705c121SKalle Valo 	}
831e705c121SKalle Valo 
832e705c121SKalle Valo 	mcc_val = iwl_mvm_wrdd_get_mcc(mvm, wrdd.pointer);
833e705c121SKalle Valo 	kfree(wrdd.pointer);
834e705c121SKalle Valo 	if (!mcc_val)
835e705c121SKalle Valo 		return -ENOENT;
836e705c121SKalle Valo 
837e705c121SKalle Valo 	mcc[0] = (mcc_val >> 8) & 0xff;
838e705c121SKalle Valo 	mcc[1] = mcc_val & 0xff;
839e705c121SKalle Valo 	mcc[2] = '\0';
840e705c121SKalle Valo 	return 0;
841e705c121SKalle Valo }
842e705c121SKalle Valo #else /* CONFIG_ACPI */
843e705c121SKalle Valo static int iwl_mvm_get_bios_mcc(struct iwl_mvm *mvm, char *mcc)
844e705c121SKalle Valo {
845e705c121SKalle Valo 	return -ENOENT;
846e705c121SKalle Valo }
847e705c121SKalle Valo #endif
848e705c121SKalle Valo 
849e705c121SKalle Valo int iwl_mvm_init_mcc(struct iwl_mvm *mvm)
850e705c121SKalle Valo {
851e705c121SKalle Valo 	bool tlv_lar;
852e705c121SKalle Valo 	bool nvm_lar;
853e705c121SKalle Valo 	int retval;
854e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
855e705c121SKalle Valo 	char mcc[3];
856e705c121SKalle Valo 
857e705c121SKalle Valo 	if (mvm->cfg->device_family == IWL_DEVICE_FAMILY_8000) {
858e705c121SKalle Valo 		tlv_lar = fw_has_capa(&mvm->fw->ucode_capa,
859e705c121SKalle Valo 				      IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
860e705c121SKalle Valo 		nvm_lar = mvm->nvm_data->lar_enabled;
861e705c121SKalle Valo 		if (tlv_lar != nvm_lar)
862e705c121SKalle Valo 			IWL_INFO(mvm,
863e705c121SKalle Valo 				 "Conflict between TLV & NVM regarding enabling LAR (TLV = %s NVM =%s)\n",
864e705c121SKalle Valo 				 tlv_lar ? "enabled" : "disabled",
865e705c121SKalle Valo 				 nvm_lar ? "enabled" : "disabled");
866e705c121SKalle Valo 	}
867e705c121SKalle Valo 
868e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
869e705c121SKalle Valo 		return 0;
870e705c121SKalle Valo 
871e705c121SKalle Valo 	/*
872e705c121SKalle Valo 	 * try to replay the last set MCC to FW. If it doesn't exist,
873e705c121SKalle Valo 	 * queue an update to cfg80211 to retrieve the default alpha2 from FW.
874e705c121SKalle Valo 	 */
875e705c121SKalle Valo 	retval = iwl_mvm_init_fw_regd(mvm);
876e705c121SKalle Valo 	if (retval != -ENOENT)
877e705c121SKalle Valo 		return retval;
878e705c121SKalle Valo 
879e705c121SKalle Valo 	/*
880e705c121SKalle Valo 	 * Driver regulatory hint for initial update, this also informs the
881e705c121SKalle Valo 	 * firmware we support wifi location updates.
882e705c121SKalle Valo 	 * Disallow scans that might crash the FW while the LAR regdomain
883e705c121SKalle Valo 	 * is not set.
884e705c121SKalle Valo 	 */
885e705c121SKalle Valo 	mvm->lar_regdom_set = false;
886e705c121SKalle Valo 
887e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, NULL);
888e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
889e705c121SKalle Valo 		return -EIO;
890e705c121SKalle Valo 
891e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm) &&
892e705c121SKalle Valo 	    !iwl_mvm_get_bios_mcc(mvm, mcc)) {
893e705c121SKalle Valo 		kfree(regd);
894e705c121SKalle Valo 		regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc,
895e705c121SKalle Valo 					     MCC_SOURCE_BIOS, NULL);
896e705c121SKalle Valo 		if (IS_ERR_OR_NULL(regd))
897e705c121SKalle Valo 			return -EIO;
898e705c121SKalle Valo 	}
899e705c121SKalle Valo 
900e705c121SKalle Valo 	retval = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd);
901e705c121SKalle Valo 	kfree(regd);
902e705c121SKalle Valo 	return retval;
903e705c121SKalle Valo }
904e705c121SKalle Valo 
905e705c121SKalle Valo void iwl_mvm_rx_chub_update_mcc(struct iwl_mvm *mvm,
906e705c121SKalle Valo 				struct iwl_rx_cmd_buffer *rxb)
907e705c121SKalle Valo {
908e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
909e705c121SKalle Valo 	struct iwl_mcc_chub_notif *notif = (void *)pkt->data;
910e705c121SKalle Valo 	enum iwl_mcc_source src;
911e705c121SKalle Valo 	char mcc[3];
912e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
913e705c121SKalle Valo 
914e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
915e705c121SKalle Valo 
916e705c121SKalle Valo 	if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
917e705c121SKalle Valo 		return;
918e705c121SKalle Valo 
919e705c121SKalle Valo 	mcc[0] = notif->mcc >> 8;
920e705c121SKalle Valo 	mcc[1] = notif->mcc & 0xff;
921e705c121SKalle Valo 	mcc[2] = '\0';
922e705c121SKalle Valo 	src = notif->source_id;
923e705c121SKalle Valo 
924e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm,
925e705c121SKalle Valo 		      "RX: received chub update mcc cmd (mcc '%s' src %d)\n",
926e705c121SKalle Valo 		      mcc, src);
927e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc, src, NULL);
928e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
929e705c121SKalle Valo 		return;
930e705c121SKalle Valo 
931e705c121SKalle Valo 	regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
932e705c121SKalle Valo 	kfree(regd);
933e705c121SKalle Valo }
934