xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/nvm.c (revision 0b295a1eb81f37dc7d4f4f2ee9ef375fb36ab5d8)
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
10d4a7e708SHaim Dreyfuss  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
119c4f7d51SShaul Triebitz  * Copyright(c) 2018        Intel Corporation
12e705c121SKalle Valo  *
13e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
14e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
15e705c121SKalle Valo  * published by the Free Software Foundation.
16e705c121SKalle Valo  *
17e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
18e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
19e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20e705c121SKalle Valo  * General Public License for more details.
21e705c121SKalle Valo  *
22e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
23e705c121SKalle Valo  * in the file called COPYING.
24e705c121SKalle Valo  *
25e705c121SKalle Valo  * Contact Information:
26cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
27e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28e705c121SKalle Valo  *
29e705c121SKalle Valo  * BSD LICENSE
30e705c121SKalle Valo  *
31e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33d4a7e708SHaim Dreyfuss  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
349c4f7d51SShaul Triebitz  * Copyright(c) 2018        Intel Corporation
35e705c121SKalle Valo  * All rights reserved.
36e705c121SKalle Valo  *
37e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
38e705c121SKalle Valo  * modification, are permitted provided that the following conditions
39e705c121SKalle Valo  * are met:
40e705c121SKalle Valo  *
41e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
42e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
43e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
44e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
45e705c121SKalle Valo  *    the documentation and/or other materials provided with the
46e705c121SKalle Valo  *    distribution.
47e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
48e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
49e705c121SKalle Valo  *    from this software without specific prior written permission.
50e705c121SKalle Valo  *
51e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62e705c121SKalle Valo  *
63e705c121SKalle Valo  *****************************************************************************/
64e705c121SKalle Valo #include <linux/firmware.h>
65e705c121SKalle Valo #include <linux/rtnetlink.h>
66e705c121SKalle Valo #include "iwl-trans.h"
67e705c121SKalle Valo #include "iwl-csr.h"
68e705c121SKalle Valo #include "mvm.h"
69e705c121SKalle Valo #include "iwl-eeprom-parse.h"
70e705c121SKalle Valo #include "iwl-eeprom-read.h"
71e705c121SKalle Valo #include "iwl-nvm-parse.h"
72e705c121SKalle Valo #include "iwl-prph.h"
7345f65569SLuca Coelho #include "fw/acpi.h"
74e705c121SKalle Valo 
75e705c121SKalle Valo /* Default NVM size to read */
76e705c121SKalle Valo #define IWL_NVM_DEFAULT_CHUNK_SIZE (2 * 1024)
77e705c121SKalle Valo 
78e705c121SKalle Valo #define NVM_WRITE_OPCODE 1
79e705c121SKalle Valo #define NVM_READ_OPCODE 0
80e705c121SKalle Valo 
81e705c121SKalle Valo /* load nvm chunk response */
82e705c121SKalle Valo enum {
83e705c121SKalle Valo 	READ_NVM_CHUNK_SUCCEED = 0,
84e705c121SKalle Valo 	READ_NVM_CHUNK_NOT_VALID_ADDRESS = 1
85e705c121SKalle Valo };
86e705c121SKalle Valo 
87e705c121SKalle Valo /*
88e705c121SKalle Valo  * prepare the NVM host command w/ the pointers to the nvm buffer
89e705c121SKalle Valo  * and send it to fw
90e705c121SKalle Valo  */
91e705c121SKalle Valo static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section,
92e705c121SKalle Valo 			       u16 offset, u16 length, const u8 *data)
93e705c121SKalle Valo {
94e705c121SKalle Valo 	struct iwl_nvm_access_cmd nvm_access_cmd = {
95e705c121SKalle Valo 		.offset = cpu_to_le16(offset),
96e705c121SKalle Valo 		.length = cpu_to_le16(length),
97e705c121SKalle Valo 		.type = cpu_to_le16(section),
98e705c121SKalle Valo 		.op_code = NVM_WRITE_OPCODE,
99e705c121SKalle Valo 	};
100e705c121SKalle Valo 	struct iwl_host_cmd cmd = {
101e705c121SKalle Valo 		.id = NVM_ACCESS_CMD,
102e705c121SKalle Valo 		.len = { sizeof(struct iwl_nvm_access_cmd), length },
1039a57f650SMatti Gottlieb 		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
104e705c121SKalle Valo 		.data = { &nvm_access_cmd, data },
105e705c121SKalle Valo 		/* data may come from vmalloc, so use _DUP */
106e705c121SKalle Valo 		.dataflags = { 0, IWL_HCMD_DFL_DUP },
107e705c121SKalle Valo 	};
1089a57f650SMatti Gottlieb 	struct iwl_rx_packet *pkt;
1099a57f650SMatti Gottlieb 	struct iwl_nvm_access_resp *nvm_resp;
1109a57f650SMatti Gottlieb 	int ret;
111e705c121SKalle Valo 
1129a57f650SMatti Gottlieb 	ret = iwl_mvm_send_cmd(mvm, &cmd);
1139a57f650SMatti Gottlieb 	if (ret)
1149a57f650SMatti Gottlieb 		return ret;
1159a57f650SMatti Gottlieb 
1169a57f650SMatti Gottlieb 	pkt = cmd.resp_pkt;
1179a57f650SMatti Gottlieb 	/* Extract & check NVM write response */
1189a57f650SMatti Gottlieb 	nvm_resp = (void *)pkt->data;
1199a57f650SMatti Gottlieb 	if (le16_to_cpu(nvm_resp->status) != READ_NVM_CHUNK_SUCCEED) {
1209a57f650SMatti Gottlieb 		IWL_ERR(mvm,
1219a57f650SMatti Gottlieb 			"NVM access write command failed for section %u (status = 0x%x)\n",
1229a57f650SMatti Gottlieb 			section, le16_to_cpu(nvm_resp->status));
1239a57f650SMatti Gottlieb 		ret = -EIO;
1249a57f650SMatti Gottlieb 	}
1259a57f650SMatti Gottlieb 
1269a57f650SMatti Gottlieb 	iwl_free_resp(&cmd);
1279a57f650SMatti Gottlieb 	return ret;
128e705c121SKalle Valo }
129e705c121SKalle Valo 
130e705c121SKalle Valo static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section,
131e705c121SKalle Valo 			      u16 offset, u16 length, u8 *data)
132e705c121SKalle Valo {
133e705c121SKalle Valo 	struct iwl_nvm_access_cmd nvm_access_cmd = {
134e705c121SKalle Valo 		.offset = cpu_to_le16(offset),
135e705c121SKalle Valo 		.length = cpu_to_le16(length),
136e705c121SKalle Valo 		.type = cpu_to_le16(section),
137e705c121SKalle Valo 		.op_code = NVM_READ_OPCODE,
138e705c121SKalle Valo 	};
139e705c121SKalle Valo 	struct iwl_nvm_access_resp *nvm_resp;
140e705c121SKalle Valo 	struct iwl_rx_packet *pkt;
141e705c121SKalle Valo 	struct iwl_host_cmd cmd = {
142e705c121SKalle Valo 		.id = NVM_ACCESS_CMD,
143e705c121SKalle Valo 		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
144e705c121SKalle Valo 		.data = { &nvm_access_cmd, },
145e705c121SKalle Valo 	};
146e705c121SKalle Valo 	int ret, bytes_read, offset_read;
147e705c121SKalle Valo 	u8 *resp_data;
148e705c121SKalle Valo 
149e705c121SKalle Valo 	cmd.len[0] = sizeof(struct iwl_nvm_access_cmd);
150e705c121SKalle Valo 
151e705c121SKalle Valo 	ret = iwl_mvm_send_cmd(mvm, &cmd);
152e705c121SKalle Valo 	if (ret)
153e705c121SKalle Valo 		return ret;
154e705c121SKalle Valo 
155e705c121SKalle Valo 	pkt = cmd.resp_pkt;
156e705c121SKalle Valo 
157e705c121SKalle Valo 	/* Extract NVM response */
158e705c121SKalle Valo 	nvm_resp = (void *)pkt->data;
159e705c121SKalle Valo 	ret = le16_to_cpu(nvm_resp->status);
160e705c121SKalle Valo 	bytes_read = le16_to_cpu(nvm_resp->length);
161e705c121SKalle Valo 	offset_read = le16_to_cpu(nvm_resp->offset);
162e705c121SKalle Valo 	resp_data = nvm_resp->data;
163e705c121SKalle Valo 	if (ret) {
164e705c121SKalle Valo 		if ((offset != 0) &&
165e705c121SKalle Valo 		    (ret == READ_NVM_CHUNK_NOT_VALID_ADDRESS)) {
166e705c121SKalle Valo 			/*
167e705c121SKalle Valo 			 * meaning of NOT_VALID_ADDRESS:
168e705c121SKalle Valo 			 * driver try to read chunk from address that is
169e705c121SKalle Valo 			 * multiple of 2K and got an error since addr is empty.
170e705c121SKalle Valo 			 * meaning of (offset != 0): driver already
171e705c121SKalle Valo 			 * read valid data from another chunk so this case
172e705c121SKalle Valo 			 * is not an error.
173e705c121SKalle Valo 			 */
174e705c121SKalle Valo 			IWL_DEBUG_EEPROM(mvm->trans->dev,
175e705c121SKalle Valo 					 "NVM access command failed on offset 0x%x since that section size is multiple 2K\n",
176e705c121SKalle Valo 					 offset);
177e705c121SKalle Valo 			ret = 0;
178e705c121SKalle Valo 		} else {
179e705c121SKalle Valo 			IWL_DEBUG_EEPROM(mvm->trans->dev,
180e705c121SKalle Valo 					 "NVM access command failed with status %d (device: %s)\n",
181*0b295a1eSLuca Coelho 					 ret, mvm->trans->name);
182c281f137SJohannes Berg 			ret = -ENODATA;
183e705c121SKalle Valo 		}
184e705c121SKalle Valo 		goto exit;
185e705c121SKalle Valo 	}
186e705c121SKalle Valo 
187e705c121SKalle Valo 	if (offset_read != offset) {
188e705c121SKalle Valo 		IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n",
189e705c121SKalle Valo 			offset_read);
190e705c121SKalle Valo 		ret = -EINVAL;
191e705c121SKalle Valo 		goto exit;
192e705c121SKalle Valo 	}
193e705c121SKalle Valo 
194e705c121SKalle Valo 	/* Write data to NVM */
195e705c121SKalle Valo 	memcpy(data + offset, resp_data, bytes_read);
196e705c121SKalle Valo 	ret = bytes_read;
197e705c121SKalle Valo 
198e705c121SKalle Valo exit:
199e705c121SKalle Valo 	iwl_free_resp(&cmd);
200e705c121SKalle Valo 	return ret;
201e705c121SKalle Valo }
202e705c121SKalle Valo 
203e705c121SKalle Valo static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section,
204e705c121SKalle Valo 				 const u8 *data, u16 length)
205e705c121SKalle Valo {
206e705c121SKalle Valo 	int offset = 0;
207e705c121SKalle Valo 
208e705c121SKalle Valo 	/* copy data in chunks of 2k (and remainder if any) */
209e705c121SKalle Valo 
210e705c121SKalle Valo 	while (offset < length) {
211e705c121SKalle Valo 		int chunk_size, ret;
212e705c121SKalle Valo 
213e705c121SKalle Valo 		chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE,
214e705c121SKalle Valo 				 length - offset);
215e705c121SKalle Valo 
216e705c121SKalle Valo 		ret = iwl_nvm_write_chunk(mvm, section, offset,
217e705c121SKalle Valo 					  chunk_size, data + offset);
218e705c121SKalle Valo 		if (ret < 0)
219e705c121SKalle Valo 			return ret;
220e705c121SKalle Valo 
221e705c121SKalle Valo 		offset += chunk_size;
222e705c121SKalle Valo 	}
223e705c121SKalle Valo 
224e705c121SKalle Valo 	return 0;
225e705c121SKalle Valo }
226e705c121SKalle Valo 
227e705c121SKalle Valo /*
228e705c121SKalle Valo  * Reads an NVM section completely.
229e705c121SKalle Valo  * NICs prior to 7000 family doesn't have a real NVM, but just read
230e705c121SKalle Valo  * section 0 which is the EEPROM. Because the EEPROM reading is unlimited
231e705c121SKalle Valo  * by uCode, we need to manually check in this case that we don't
232e705c121SKalle Valo  * overflow and try to read more than the EEPROM size.
233e705c121SKalle Valo  * For 7000 family NICs, we supply the maximal size we can read, and
234e705c121SKalle Valo  * the uCode fills the response with as much data as we can,
235e705c121SKalle Valo  * without overflowing, so no check is needed.
236e705c121SKalle Valo  */
237e705c121SKalle Valo static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section,
238e705c121SKalle Valo 				u8 *data, u32 size_read)
239e705c121SKalle Valo {
240e705c121SKalle Valo 	u16 length, offset = 0;
241e705c121SKalle Valo 	int ret;
242e705c121SKalle Valo 
243e705c121SKalle Valo 	/* Set nvm section read length */
244e705c121SKalle Valo 	length = IWL_NVM_DEFAULT_CHUNK_SIZE;
245e705c121SKalle Valo 
246e705c121SKalle Valo 	ret = length;
247e705c121SKalle Valo 
248e705c121SKalle Valo 	/* Read the NVM until exhausted (reading less than requested) */
249e705c121SKalle Valo 	while (ret == length) {
250e705c121SKalle Valo 		/* Check no memory assumptions fail and cause an overflow */
251e705c121SKalle Valo 		if ((size_read + offset + length) >
2527d34a7d7SLuca Coelho 		    mvm->trans->trans_cfg->base_params->eeprom_size) {
253e705c121SKalle Valo 			IWL_ERR(mvm, "EEPROM size is too small for NVM\n");
254e705c121SKalle Valo 			return -ENOBUFS;
255e705c121SKalle Valo 		}
256e705c121SKalle Valo 
257e705c121SKalle Valo 		ret = iwl_nvm_read_chunk(mvm, section, offset, length, data);
258e705c121SKalle Valo 		if (ret < 0) {
259e705c121SKalle Valo 			IWL_DEBUG_EEPROM(mvm->trans->dev,
260e705c121SKalle Valo 					 "Cannot read NVM from section %d offset %d, length %d\n",
261e705c121SKalle Valo 					 section, offset, length);
262e705c121SKalle Valo 			return ret;
263e705c121SKalle Valo 		}
264e705c121SKalle Valo 		offset += ret;
265e705c121SKalle Valo 	}
266e705c121SKalle Valo 
2679c4f7d51SShaul Triebitz 	iwl_nvm_fixups(mvm->trans->hw_id, section, data, offset);
2687d162045SJohannes Berg 
269e705c121SKalle Valo 	IWL_DEBUG_EEPROM(mvm->trans->dev,
270e705c121SKalle Valo 			 "NVM section %d read completed\n", section);
271e705c121SKalle Valo 	return offset;
272e705c121SKalle Valo }
273e705c121SKalle Valo 
274e705c121SKalle Valo static struct iwl_nvm_data *
275e705c121SKalle Valo iwl_parse_nvm_sections(struct iwl_mvm *mvm)
276e705c121SKalle Valo {
277e705c121SKalle Valo 	struct iwl_nvm_section *sections = mvm->nvm_sections;
2788fe34b06SLuca Coelho 	const __be16 *hw;
2798fe34b06SLuca Coelho 	const __le16 *sw, *calib, *regulatory, *mac_override, *phy_sku;
280e705c121SKalle Valo 	bool lar_enabled;
28144fd09daSChaya Rachel Ivgi 	int regulatory_type;
282e705c121SKalle Valo 
283e705c121SKalle Valo 	/* Checking for required sections */
28444fd09daSChaya Rachel Ivgi 	if (mvm->trans->cfg->nvm_type != IWL_NVM_EXT) {
285e705c121SKalle Valo 		if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
286e705c121SKalle Valo 		    !mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data) {
287e705c121SKalle Valo 			IWL_ERR(mvm, "Can't parse empty OTP/NVM sections\n");
288e705c121SKalle Valo 			return NULL;
289e705c121SKalle Valo 		}
290e705c121SKalle Valo 	} else {
29144fd09daSChaya Rachel Ivgi 		if (mvm->trans->cfg->nvm_type == IWL_NVM_SDP)
29244fd09daSChaya Rachel Ivgi 			regulatory_type = NVM_SECTION_TYPE_REGULATORY_SDP;
29344fd09daSChaya Rachel Ivgi 		else
29444fd09daSChaya Rachel Ivgi 			regulatory_type = NVM_SECTION_TYPE_REGULATORY;
29544fd09daSChaya Rachel Ivgi 
296e705c121SKalle Valo 		/* SW and REGULATORY sections are mandatory */
297e705c121SKalle Valo 		if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
29844fd09daSChaya Rachel Ivgi 		    !mvm->nvm_sections[regulatory_type].data) {
299e705c121SKalle Valo 			IWL_ERR(mvm,
300e705c121SKalle Valo 				"Can't parse empty family 8000 OTP/NVM sections\n");
301e705c121SKalle Valo 			return NULL;
302e705c121SKalle Valo 		}
303e705c121SKalle Valo 		/* MAC_OVERRIDE or at least HW section must exist */
304e705c121SKalle Valo 		if (!mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data &&
305e705c121SKalle Valo 		    !mvm->nvm_sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data) {
306e705c121SKalle Valo 			IWL_ERR(mvm,
307e705c121SKalle Valo 				"Can't parse mac_address, empty sections\n");
308e705c121SKalle Valo 			return NULL;
309e705c121SKalle Valo 		}
310e705c121SKalle Valo 
311e705c121SKalle Valo 		/* PHY_SKU section is mandatory in B0 */
312e705c121SKalle Valo 		if (!mvm->nvm_sections[NVM_SECTION_TYPE_PHY_SKU].data) {
313e705c121SKalle Valo 			IWL_ERR(mvm,
314e705c121SKalle Valo 				"Can't parse phy_sku in B0, empty sections\n");
315e705c121SKalle Valo 			return NULL;
316e705c121SKalle Valo 		}
317e705c121SKalle Valo 	}
318e705c121SKalle Valo 
3198fe34b06SLuca Coelho 	hw = (const __be16 *)sections[mvm->cfg->nvm_hw_section_num].data;
320e705c121SKalle Valo 	sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data;
321e705c121SKalle Valo 	calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data;
322e705c121SKalle Valo 	mac_override =
323e705c121SKalle Valo 		(const __le16 *)sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data;
324e705c121SKalle Valo 	phy_sku = (const __le16 *)sections[NVM_SECTION_TYPE_PHY_SKU].data;
325e705c121SKalle Valo 
32644fd09daSChaya Rachel Ivgi 	regulatory = mvm->trans->cfg->nvm_type == IWL_NVM_SDP ?
32744fd09daSChaya Rachel Ivgi 		(const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY_SDP].data :
32844fd09daSChaya Rachel Ivgi 		(const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY].data;
32944fd09daSChaya Rachel Ivgi 
330e705c121SKalle Valo 	lar_enabled = !iwlwifi_mod_params.lar_disable &&
331e705c121SKalle Valo 		      fw_has_capa(&mvm->fw->ucode_capa,
332e705c121SKalle Valo 				  IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
333e705c121SKalle Valo 
334afd5b170SSara Sharon 	return iwl_parse_nvm_data(mvm->trans, mvm->cfg, hw, sw, calib,
335e705c121SKalle Valo 				  regulatory, mac_override, phy_sku,
336e705c121SKalle Valo 				  mvm->fw->valid_tx_ant, mvm->fw->valid_rx_ant,
337afd5b170SSara Sharon 				  lar_enabled);
338e705c121SKalle Valo }
339e705c121SKalle Valo 
340e705c121SKalle Valo /* Loads the NVM data stored in mvm->nvm_sections into the NIC */
341e705c121SKalle Valo int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm)
342e705c121SKalle Valo {
343e705c121SKalle Valo 	int i, ret = 0;
344e705c121SKalle Valo 	struct iwl_nvm_section *sections = mvm->nvm_sections;
345e705c121SKalle Valo 
346e705c121SKalle Valo 	IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n");
347e705c121SKalle Valo 
348e705c121SKalle Valo 	for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) {
349e705c121SKalle Valo 		if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length)
350e705c121SKalle Valo 			continue;
351e705c121SKalle Valo 		ret = iwl_nvm_write_section(mvm, i, sections[i].data,
352e705c121SKalle Valo 					    sections[i].length);
353e705c121SKalle Valo 		if (ret < 0) {
354e705c121SKalle Valo 			IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);
355e705c121SKalle Valo 			break;
356e705c121SKalle Valo 		}
357e705c121SKalle Valo 	}
358e705c121SKalle Valo 	return ret;
359e705c121SKalle Valo }
360e705c121SKalle Valo 
3615bd1d2c1SLuca Coelho int iwl_nvm_init(struct iwl_mvm *mvm)
362e705c121SKalle Valo {
363e705c121SKalle Valo 	int ret, section;
364e705c121SKalle Valo 	u32 size_read = 0;
365e705c121SKalle Valo 	u8 *nvm_buffer, *temp;
366e705c121SKalle Valo 	const char *nvm_file_C = mvm->cfg->default_nvm_file_C_step;
367e705c121SKalle Valo 
368e705c121SKalle Valo 	if (WARN_ON_ONCE(mvm->cfg->nvm_hw_section_num >= NVM_MAX_NUM_SECTIONS))
369e705c121SKalle Valo 		return -EINVAL;
370e705c121SKalle Valo 
371e705c121SKalle Valo 	/* load NVM values from nic */
372e705c121SKalle Valo 	/* Read From FW NVM */
373e705c121SKalle Valo 	IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");
374e705c121SKalle Valo 
3757d34a7d7SLuca Coelho 	nvm_buffer = kmalloc(mvm->trans->trans_cfg->base_params->eeprom_size,
376e705c121SKalle Valo 			     GFP_KERNEL);
377e705c121SKalle Valo 	if (!nvm_buffer)
378e705c121SKalle Valo 		return -ENOMEM;
379e705c121SKalle Valo 	for (section = 0; section < NVM_MAX_NUM_SECTIONS; section++) {
380e705c121SKalle Valo 		/* we override the constness for initial read */
381e705c121SKalle Valo 		ret = iwl_nvm_read_section(mvm, section, nvm_buffer,
382e705c121SKalle Valo 					   size_read);
383c281f137SJohannes Berg 		if (ret == -ENODATA) {
384c281f137SJohannes Berg 			ret = 0;
385e705c121SKalle Valo 			continue;
386c281f137SJohannes Berg 		}
387c281f137SJohannes Berg 		if (ret < 0)
388c281f137SJohannes Berg 			break;
389e705c121SKalle Valo 		size_read += ret;
390e705c121SKalle Valo 		temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
391e705c121SKalle Valo 		if (!temp) {
392e705c121SKalle Valo 			ret = -ENOMEM;
393e705c121SKalle Valo 			break;
394e705c121SKalle Valo 		}
3957d162045SJohannes Berg 
3969c4f7d51SShaul Triebitz 		iwl_nvm_fixups(mvm->trans->hw_id, section, temp, ret);
3977d162045SJohannes Berg 
398e705c121SKalle Valo 		mvm->nvm_sections[section].data = temp;
399e705c121SKalle Valo 		mvm->nvm_sections[section].length = ret;
400e705c121SKalle Valo 
401e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
402e705c121SKalle Valo 		switch (section) {
403e705c121SKalle Valo 		case NVM_SECTION_TYPE_SW:
404e705c121SKalle Valo 			mvm->nvm_sw_blob.data = temp;
405e705c121SKalle Valo 			mvm->nvm_sw_blob.size  = ret;
406e705c121SKalle Valo 			break;
407e705c121SKalle Valo 		case NVM_SECTION_TYPE_CALIBRATION:
408e705c121SKalle Valo 			mvm->nvm_calib_blob.data = temp;
409e705c121SKalle Valo 			mvm->nvm_calib_blob.size  = ret;
410e705c121SKalle Valo 			break;
411e705c121SKalle Valo 		case NVM_SECTION_TYPE_PRODUCTION:
412e705c121SKalle Valo 			mvm->nvm_prod_blob.data = temp;
413e705c121SKalle Valo 			mvm->nvm_prod_blob.size  = ret;
414e705c121SKalle Valo 			break;
415e705c121SKalle Valo 		case NVM_SECTION_TYPE_PHY_SKU:
416e705c121SKalle Valo 			mvm->nvm_phy_sku_blob.data = temp;
417e705c121SKalle Valo 			mvm->nvm_phy_sku_blob.size  = ret;
418e705c121SKalle Valo 			break;
4190f8bf03cSLuca Coelho 		case NVM_SECTION_TYPE_REGULATORY_SDP:
4200f8bf03cSLuca Coelho 		case NVM_SECTION_TYPE_REGULATORY:
4210f8bf03cSLuca Coelho 			mvm->nvm_reg_blob.data = temp;
4220f8bf03cSLuca Coelho 			mvm->nvm_reg_blob.size  = ret;
4230f8bf03cSLuca Coelho 			break;
424e705c121SKalle Valo 		default:
425e705c121SKalle Valo 			if (section == mvm->cfg->nvm_hw_section_num) {
426e705c121SKalle Valo 				mvm->nvm_hw_blob.data = temp;
427e705c121SKalle Valo 				mvm->nvm_hw_blob.size = ret;
428e705c121SKalle Valo 				break;
429e705c121SKalle Valo 			}
430e705c121SKalle Valo 		}
431e705c121SKalle Valo #endif
432e705c121SKalle Valo 	}
433e705c121SKalle Valo 	if (!size_read)
434e705c121SKalle Valo 		IWL_ERR(mvm, "OTP is blank\n");
435e705c121SKalle Valo 	kfree(nvm_buffer);
436e705c121SKalle Valo 
437e705c121SKalle Valo 	/* Only if PNVM selected in the mod param - load external NVM  */
438e705c121SKalle Valo 	if (mvm->nvm_file_name) {
439e705c121SKalle Valo 		/* read External NVM file from the mod param */
4409c4f7d51SShaul Triebitz 		ret = iwl_read_external_nvm(mvm->trans, mvm->nvm_file_name,
4419c4f7d51SShaul Triebitz 					    mvm->nvm_sections);
442e705c121SKalle Valo 		if (ret) {
443e705c121SKalle Valo 			mvm->nvm_file_name = nvm_file_C;
444e705c121SKalle Valo 
445488c28e1SOren Givon 			if ((ret == -EFAULT || ret == -ENOENT) &&
446488c28e1SOren Givon 			    mvm->nvm_file_name) {
447e705c121SKalle Valo 				/* in case nvm file was failed try again */
4489c4f7d51SShaul Triebitz 				ret = iwl_read_external_nvm(mvm->trans,
4499c4f7d51SShaul Triebitz 							    mvm->nvm_file_name,
4509c4f7d51SShaul Triebitz 							    mvm->nvm_sections);
451e705c121SKalle Valo 				if (ret)
452e705c121SKalle Valo 					return ret;
453e705c121SKalle Valo 			} else {
454e705c121SKalle Valo 				return ret;
455e705c121SKalle Valo 			}
456e705c121SKalle Valo 		}
457e705c121SKalle Valo 	}
458e705c121SKalle Valo 
459e705c121SKalle Valo 	/* parse the relevant nvm sections */
460e705c121SKalle Valo 	mvm->nvm_data = iwl_parse_nvm_sections(mvm);
461e705c121SKalle Valo 	if (!mvm->nvm_data)
462e705c121SKalle Valo 		return -ENODATA;
463e705c121SKalle Valo 	IWL_DEBUG_EEPROM(mvm->trans->dev, "nvm version = %x\n",
464e705c121SKalle Valo 			 mvm->nvm_data->nvm_version);
465e705c121SKalle Valo 
466c281f137SJohannes Berg 	return ret < 0 ? ret : 0;
467e705c121SKalle Valo }
468e705c121SKalle Valo 
469e705c121SKalle Valo struct iwl_mcc_update_resp *
470e705c121SKalle Valo iwl_mvm_update_mcc(struct iwl_mvm *mvm, const char *alpha2,
471e705c121SKalle Valo 		   enum iwl_mcc_source src_id)
472e705c121SKalle Valo {
473e705c121SKalle Valo 	struct iwl_mcc_update_cmd mcc_update_cmd = {
474e705c121SKalle Valo 		.mcc = cpu_to_le16(alpha2[0] << 8 | alpha2[1]),
475e705c121SKalle Valo 		.source_id = (u8)src_id,
476e705c121SKalle Valo 	};
4775a7d87daSArnd Bergmann 	struct iwl_mcc_update_resp *resp_cp;
478e705c121SKalle Valo 	struct iwl_rx_packet *pkt;
479e705c121SKalle Valo 	struct iwl_host_cmd cmd = {
480e705c121SKalle Valo 		.id = MCC_UPDATE_CMD,
481e705c121SKalle Valo 		.flags = CMD_WANT_SKB,
482e705c121SKalle Valo 		.data = { &mcc_update_cmd },
483e705c121SKalle Valo 	};
484e705c121SKalle Valo 
485e705c121SKalle Valo 	int ret;
486e705c121SKalle Valo 	u32 status;
487e705c121SKalle Valo 	int resp_len, n_channels;
488e705c121SKalle Valo 	u16 mcc;
489e705c121SKalle Valo 
490e705c121SKalle Valo 	if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
491e705c121SKalle Valo 		return ERR_PTR(-EOPNOTSUPP);
492e705c121SKalle Valo 
493e705c121SKalle Valo 	cmd.len[0] = sizeof(struct iwl_mcc_update_cmd);
494e705c121SKalle Valo 
495e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "send MCC update to FW with '%c%c' src = %d\n",
496e705c121SKalle Valo 		      alpha2[0], alpha2[1], src_id);
497e705c121SKalle Valo 
498e705c121SKalle Valo 	ret = iwl_mvm_send_cmd(mvm, &cmd);
499e705c121SKalle Valo 	if (ret)
500e705c121SKalle Valo 		return ERR_PTR(ret);
501e705c121SKalle Valo 
502e705c121SKalle Valo 	pkt = cmd.resp_pkt;
503e705c121SKalle Valo 
504e705c121SKalle Valo 	/* Extract MCC response */
50547fe2f8eSHaim Dreyfuss 	if (fw_has_capa(&mvm->fw->ucode_capa,
50647fe2f8eSHaim Dreyfuss 			IWL_UCODE_TLV_CAPA_MCC_UPDATE_11AX_SUPPORT)) {
5075a7d87daSArnd Bergmann 		struct iwl_mcc_update_resp *mcc_resp = (void *)pkt->data;
5085a7d87daSArnd Bergmann 
5096fa52430SMatti Gottlieb 		n_channels =  __le32_to_cpu(mcc_resp->n_channels);
5105a7d87daSArnd Bergmann 		resp_len = sizeof(struct iwl_mcc_update_resp) +
5115a7d87daSArnd Bergmann 			   n_channels * sizeof(__le32);
5125a7d87daSArnd Bergmann 		resp_cp = kmemdup(mcc_resp, resp_len, GFP_KERNEL);
51328269897SJohannes Berg 		if (!resp_cp) {
51428269897SJohannes Berg 			resp_cp = ERR_PTR(-ENOMEM);
51528269897SJohannes Berg 			goto exit;
51628269897SJohannes Berg 		}
5176fa52430SMatti Gottlieb 	} else {
51847fe2f8eSHaim Dreyfuss 		struct iwl_mcc_update_resp_v3 *mcc_resp_v3 = (void *)pkt->data;
5195a7d87daSArnd Bergmann 
52047fe2f8eSHaim Dreyfuss 		n_channels =  __le32_to_cpu(mcc_resp_v3->n_channels);
5215a7d87daSArnd Bergmann 		resp_len = sizeof(struct iwl_mcc_update_resp) +
5225a7d87daSArnd Bergmann 			   n_channels * sizeof(__le32);
5236fa52430SMatti Gottlieb 		resp_cp = kzalloc(resp_len, GFP_KERNEL);
52428269897SJohannes Berg 		if (!resp_cp) {
52528269897SJohannes Berg 			resp_cp = ERR_PTR(-ENOMEM);
52628269897SJohannes Berg 			goto exit;
52728269897SJohannes Berg 		}
528e705c121SKalle Valo 
52947fe2f8eSHaim Dreyfuss 		resp_cp->status = mcc_resp_v3->status;
53047fe2f8eSHaim Dreyfuss 		resp_cp->mcc = mcc_resp_v3->mcc;
53147fe2f8eSHaim Dreyfuss 		resp_cp->cap = cpu_to_le16(mcc_resp_v3->cap);
53247fe2f8eSHaim Dreyfuss 		resp_cp->source_id = mcc_resp_v3->source_id;
53347fe2f8eSHaim Dreyfuss 		resp_cp->time = mcc_resp_v3->time;
53447fe2f8eSHaim Dreyfuss 		resp_cp->geo_info = mcc_resp_v3->geo_info;
53547fe2f8eSHaim Dreyfuss 		resp_cp->n_channels = mcc_resp_v3->n_channels;
53647fe2f8eSHaim Dreyfuss 		memcpy(resp_cp->channels, mcc_resp_v3->channels,
5376fa52430SMatti Gottlieb 		       n_channels * sizeof(__le32));
5386fa52430SMatti Gottlieb 	}
5396fa52430SMatti Gottlieb 
5406fa52430SMatti Gottlieb 	status = le32_to_cpu(resp_cp->status);
5416fa52430SMatti Gottlieb 
5426fa52430SMatti Gottlieb 	mcc = le16_to_cpu(resp_cp->mcc);
5436fa52430SMatti Gottlieb 
5446fa52430SMatti Gottlieb 	/* W/A for a FW/NVM issue - returns 0x00 for the world domain */
5456fa52430SMatti Gottlieb 	if (mcc == 0) {
5466fa52430SMatti Gottlieb 		mcc = 0x3030;  /* "00" - world */
5476fa52430SMatti Gottlieb 		resp_cp->mcc = cpu_to_le16(mcc);
5486fa52430SMatti Gottlieb 	}
5496fa52430SMatti Gottlieb 
5506fa52430SMatti Gottlieb 	IWL_DEBUG_LAR(mvm,
55182715ac7SEmmanuel Grumbach 		      "MCC response status: 0x%x. new MCC: 0x%x ('%c%c') n_chans: %d\n",
55282715ac7SEmmanuel Grumbach 		      status, mcc, mcc >> 8, mcc & 0xff, n_channels);
5536fa52430SMatti Gottlieb 
554e705c121SKalle Valo exit:
555e705c121SKalle Valo 	iwl_free_resp(&cmd);
556e705c121SKalle Valo 	return resp_cp;
557e705c121SKalle Valo }
558e705c121SKalle Valo 
559e705c121SKalle Valo int iwl_mvm_init_mcc(struct iwl_mvm *mvm)
560e705c121SKalle Valo {
561e705c121SKalle Valo 	bool tlv_lar;
562e705c121SKalle Valo 	bool nvm_lar;
563e705c121SKalle Valo 	int retval;
564e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
565e705c121SKalle Valo 	char mcc[3];
566e705c121SKalle Valo 
56744fd09daSChaya Rachel Ivgi 	if (mvm->cfg->nvm_type == IWL_NVM_EXT) {
568e705c121SKalle Valo 		tlv_lar = fw_has_capa(&mvm->fw->ucode_capa,
569e705c121SKalle Valo 				      IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
570e705c121SKalle Valo 		nvm_lar = mvm->nvm_data->lar_enabled;
571e705c121SKalle Valo 		if (tlv_lar != nvm_lar)
572e705c121SKalle Valo 			IWL_INFO(mvm,
573e705c121SKalle Valo 				 "Conflict between TLV & NVM regarding enabling LAR (TLV = %s NVM =%s)\n",
574e705c121SKalle Valo 				 tlv_lar ? "enabled" : "disabled",
575e705c121SKalle Valo 				 nvm_lar ? "enabled" : "disabled");
576e705c121SKalle Valo 	}
577e705c121SKalle Valo 
578e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
579e705c121SKalle Valo 		return 0;
580e705c121SKalle Valo 
581e705c121SKalle Valo 	/*
582e705c121SKalle Valo 	 * try to replay the last set MCC to FW. If it doesn't exist,
583e705c121SKalle Valo 	 * queue an update to cfg80211 to retrieve the default alpha2 from FW.
584e705c121SKalle Valo 	 */
585e705c121SKalle Valo 	retval = iwl_mvm_init_fw_regd(mvm);
586e705c121SKalle Valo 	if (retval != -ENOENT)
587e705c121SKalle Valo 		return retval;
588e705c121SKalle Valo 
589e705c121SKalle Valo 	/*
590e705c121SKalle Valo 	 * Driver regulatory hint for initial update, this also informs the
591e705c121SKalle Valo 	 * firmware we support wifi location updates.
592e705c121SKalle Valo 	 * Disallow scans that might crash the FW while the LAR regdomain
593e705c121SKalle Valo 	 * is not set.
594e705c121SKalle Valo 	 */
595e705c121SKalle Valo 	mvm->lar_regdom_set = false;
596e705c121SKalle Valo 
597e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, NULL);
598e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
599e705c121SKalle Valo 		return -EIO;
600e705c121SKalle Valo 
601e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm) &&
60245f65569SLuca Coelho 	    !iwl_acpi_get_mcc(mvm->dev, mcc)) {
603e705c121SKalle Valo 		kfree(regd);
604e705c121SKalle Valo 		regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc,
605e705c121SKalle Valo 					     MCC_SOURCE_BIOS, NULL);
606e705c121SKalle Valo 		if (IS_ERR_OR_NULL(regd))
607e705c121SKalle Valo 			return -EIO;
608e705c121SKalle Valo 	}
609e705c121SKalle Valo 
610e705c121SKalle Valo 	retval = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd);
611e705c121SKalle Valo 	kfree(regd);
612e705c121SKalle Valo 	return retval;
613e705c121SKalle Valo }
614e705c121SKalle Valo 
615e705c121SKalle Valo void iwl_mvm_rx_chub_update_mcc(struct iwl_mvm *mvm,
616e705c121SKalle Valo 				struct iwl_rx_cmd_buffer *rxb)
617e705c121SKalle Valo {
618e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
619e705c121SKalle Valo 	struct iwl_mcc_chub_notif *notif = (void *)pkt->data;
620e705c121SKalle Valo 	enum iwl_mcc_source src;
621e705c121SKalle Valo 	char mcc[3];
622e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
623b6cf2879SColin Ian King 	int wgds_tbl_idx;
624e705c121SKalle Valo 
625e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
626e705c121SKalle Valo 
627d4a7e708SHaim Dreyfuss 	if (iwl_mvm_is_vif_assoc(mvm) && notif->source_id == MCC_SOURCE_WIFI) {
628d4a7e708SHaim Dreyfuss 		IWL_DEBUG_LAR(mvm, "Ignore mcc update while associated\n");
629d4a7e708SHaim Dreyfuss 		return;
630d4a7e708SHaim Dreyfuss 	}
631d4a7e708SHaim Dreyfuss 
632e705c121SKalle Valo 	if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
633e705c121SKalle Valo 		return;
634e705c121SKalle Valo 
63556c1f3c4SJohannes Berg 	mcc[0] = le16_to_cpu(notif->mcc) >> 8;
63656c1f3c4SJohannes Berg 	mcc[1] = le16_to_cpu(notif->mcc) & 0xff;
637e705c121SKalle Valo 	mcc[2] = '\0';
638e705c121SKalle Valo 	src = notif->source_id;
639e705c121SKalle Valo 
640e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm,
641e705c121SKalle Valo 		      "RX: received chub update mcc cmd (mcc '%s' src %d)\n",
642e705c121SKalle Valo 		      mcc, src);
643e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc, src, NULL);
644e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
645e705c121SKalle Valo 		return;
646e705c121SKalle Valo 
6474fd445a2SHaim Dreyfuss 	wgds_tbl_idx = iwl_mvm_get_sar_geo_profile(mvm);
6484fd445a2SHaim Dreyfuss 	if (wgds_tbl_idx < 0)
6494fd445a2SHaim Dreyfuss 		IWL_DEBUG_INFO(mvm, "SAR WGDS is disabled (%d)\n",
6504fd445a2SHaim Dreyfuss 			       wgds_tbl_idx);
6514fd445a2SHaim Dreyfuss 	else
6524fd445a2SHaim Dreyfuss 		IWL_DEBUG_INFO(mvm, "SAR WGDS: geo profile %d is configured\n",
6534fd445a2SHaim Dreyfuss 			       wgds_tbl_idx);
6544fd445a2SHaim Dreyfuss 
655e705c121SKalle Valo 	regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
656e705c121SKalle Valo 	kfree(regd);
657e705c121SKalle Valo }
658