nvm.c 25.6 KB
Newer Older
J
Johannes Berg 已提交
1 2 3 4 5 6 7
/******************************************************************************
 *
 * This file is provided under a dual BSD/GPLv2 license.  When using or
 * redistributing this file, you may do so under either license.
 *
 * GPL LICENSE SUMMARY
 *
8
 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9
 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10
 * Copyright(c) 2016        Intel Deutschland GmbH
J
Johannes Berg 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
 * USA
 *
 * The full GNU General Public License is included in this distribution
27
 * in the file called COPYING.
J
Johannes Berg 已提交
28 29
 *
 * Contact Information:
30
 *  Intel Linux Wireless <linuxwifi@intel.com>
J
Johannes Berg 已提交
31 32 33 34
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 * BSD LICENSE
 *
35
 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36
 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37
 * Copyright(c) 2016        Intel Deutschland GmbH
J
Johannes Berg 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name Intel Corporation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/
67
#include <linux/firmware.h>
68
#include <linux/rtnetlink.h>
69 70
#include <linux/pci.h>
#include <linux/acpi.h>
J
Johannes Berg 已提交
71
#include "iwl-trans.h"
72
#include "iwl-csr.h"
J
Johannes Berg 已提交
73 74 75 76
#include "mvm.h"
#include "iwl-eeprom-parse.h"
#include "iwl-eeprom-read.h"
#include "iwl-nvm-parse.h"
77
#include "iwl-prph.h"
J
Johannes Berg 已提交
78

79
/* Default NVM size to read */
80
#define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024)
81
#define IWL_MAX_NVM_SECTION_SIZE	0x1b58
82
#define IWL_MAX_NVM_8000_SECTION_SIZE	0x1ffc
83

84 85 86
#define NVM_WRITE_OPCODE 1
#define NVM_READ_OPCODE 0

87 88 89 90 91 92
/* load nvm chunk response */
enum {
	READ_NVM_CHUNK_SUCCEED = 0,
	READ_NVM_CHUNK_NOT_VALID_ADDRESS = 1
};

93 94 95 96 97 98
/*
 * prepare the NVM host command w/ the pointers to the nvm buffer
 * and send it to fw
 */
static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section,
			       u16 offset, u16 length, const u8 *data)
J
Johannes Berg 已提交
99
{
100 101 102 103 104 105 106 107 108
	struct iwl_nvm_access_cmd nvm_access_cmd = {
		.offset = cpu_to_le16(offset),
		.length = cpu_to_le16(length),
		.type = cpu_to_le16(section),
		.op_code = NVM_WRITE_OPCODE,
	};
	struct iwl_host_cmd cmd = {
		.id = NVM_ACCESS_CMD,
		.len = { sizeof(struct iwl_nvm_access_cmd), length },
109
		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
110 111 112 113
		.data = { &nvm_access_cmd, data },
		/* data may come from vmalloc, so use _DUP */
		.dataflags = { 0, IWL_HCMD_DFL_DUP },
	};
114 115 116
	struct iwl_rx_packet *pkt;
	struct iwl_nvm_access_resp *nvm_resp;
	int ret;
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	ret = iwl_mvm_send_cmd(mvm, &cmd);
	if (ret)
		return ret;

	pkt = cmd.resp_pkt;
	if (!pkt) {
		IWL_ERR(mvm, "Error in NVM_ACCESS response\n");
		return -EINVAL;
	}
	/* Extract & check NVM write response */
	nvm_resp = (void *)pkt->data;
	if (le16_to_cpu(nvm_resp->status) != READ_NVM_CHUNK_SUCCEED) {
		IWL_ERR(mvm,
			"NVM access write command failed for section %u (status = 0x%x)\n",
			section, le16_to_cpu(nvm_resp->status));
		ret = -EIO;
	}

	iwl_free_resp(&cmd);
	return ret;
J
Johannes Berg 已提交
138 139 140 141 142
}

static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section,
			      u16 offset, u16 length, u8 *data)
{
143 144 145 146 147 148
	struct iwl_nvm_access_cmd nvm_access_cmd = {
		.offset = cpu_to_le16(offset),
		.length = cpu_to_le16(length),
		.type = cpu_to_le16(section),
		.op_code = NVM_READ_OPCODE,
	};
149
	struct iwl_nvm_access_resp *nvm_resp;
J
Johannes Berg 已提交
150 151 152
	struct iwl_rx_packet *pkt;
	struct iwl_host_cmd cmd = {
		.id = NVM_ACCESS_CMD,
E
Emmanuel Grumbach 已提交
153
		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
J
Johannes Berg 已提交
154 155 156 157 158
		.data = { &nvm_access_cmd, },
	};
	int ret, bytes_read, offset_read;
	u8 *resp_data;

159
	cmd.len[0] = sizeof(struct iwl_nvm_access_cmd);
J
Johannes Berg 已提交
160 161 162 163 164 165 166 167 168

	ret = iwl_mvm_send_cmd(mvm, &cmd);
	if (ret)
		return ret;

	pkt = cmd.resp_pkt;

	/* Extract NVM response */
	nvm_resp = (void *)pkt->data;
169 170 171 172
	ret = le16_to_cpu(nvm_resp->status);
	bytes_read = le16_to_cpu(nvm_resp->length);
	offset_read = le16_to_cpu(nvm_resp->offset);
	resp_data = nvm_resp->data;
J
Johannes Berg 已提交
173
	if (ret) {
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
		if ((offset != 0) &&
		    (ret == READ_NVM_CHUNK_NOT_VALID_ADDRESS)) {
			/*
			 * meaning of NOT_VALID_ADDRESS:
			 * driver try to read chunk from address that is
			 * multiple of 2K and got an error since addr is empty.
			 * meaning of (offset != 0): driver already
			 * read valid data from another chunk so this case
			 * is not an error.
			 */
			IWL_DEBUG_EEPROM(mvm->trans->dev,
					 "NVM access command failed on offset 0x%x since that section size is multiple 2K\n",
					 offset);
			ret = 0;
		} else {
			IWL_DEBUG_EEPROM(mvm->trans->dev,
					 "NVM access command failed with status %d (device: %s)\n",
					 ret, mvm->cfg->name);
			ret = -EIO;
		}
J
Johannes Berg 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
		goto exit;
	}

	if (offset_read != offset) {
		IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n",
			offset_read);
		ret = -EINVAL;
		goto exit;
	}

	/* Write data to NVM */
	memcpy(data + offset, resp_data, bytes_read);
	ret = bytes_read;

exit:
	iwl_free_resp(&cmd);
	return ret;
}

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section,
				 const u8 *data, u16 length)
{
	int offset = 0;

	/* copy data in chunks of 2k (and remainder if any) */

	while (offset < length) {
		int chunk_size, ret;

		chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE,
				 length - offset);

		ret = iwl_nvm_write_chunk(mvm, section, offset,
					  chunk_size, data + offset);
		if (ret < 0)
			return ret;

		offset += chunk_size;
	}

	return 0;
}

237 238 239 240 241 242 243 244 245 246 247 248 249
static void iwl_mvm_nvm_fixups(struct iwl_mvm *mvm, unsigned int section,
			       u8 *data, unsigned int len)
{
#define IWL_4165_DEVICE_ID	0x5501
#define NVM_SKU_CAP_MIMO_DISABLE BIT(5)

	if (section == NVM_SECTION_TYPE_PHY_SKU &&
	    mvm->trans->hw_id == IWL_4165_DEVICE_ID && data && len >= 5 &&
	    (data[4] & NVM_SKU_CAP_MIMO_DISABLE))
		/* OTP 0x52 bug work around: it's a 1x1 device */
		data[3] = ANT_B | (ANT_B << 4);
}

J
Johannes Berg 已提交
250 251 252 253 254 255 256 257 258 259 260
/*
 * Reads an NVM section completely.
 * NICs prior to 7000 family doesn't have a real NVM, but just read
 * section 0 which is the EEPROM. Because the EEPROM reading is unlimited
 * by uCode, we need to manually check in this case that we don't
 * overflow and try to read more than the EEPROM size.
 * For 7000 family NICs, we supply the maximal size we can read, and
 * the uCode fills the response with as much data as we can,
 * without overflowing, so no check is needed.
 */
static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section,
261
				u8 *data, u32 size_read)
J
Johannes Berg 已提交
262 263 264 265
{
	u16 length, offset = 0;
	int ret;

266 267 268
	/* Set nvm section read length */
	length = IWL_NVM_DEFAULT_CHUNK_SIZE;

J
Johannes Berg 已提交
269 270 271 272
	ret = length;

	/* Read the NVM until exhausted (reading less than requested) */
	while (ret == length) {
273 274 275 276 277 278 279
		/* Check no memory assumptions fail and cause an overflow */
		if ((size_read + offset + length) >
		    mvm->cfg->base_params->eeprom_size) {
			IWL_ERR(mvm, "EEPROM size is too small for NVM\n");
			return -ENOBUFS;
		}

J
Johannes Berg 已提交
280 281
		ret = iwl_nvm_read_chunk(mvm, section, offset, length, data);
		if (ret < 0) {
282 283 284
			IWL_DEBUG_EEPROM(mvm->trans->dev,
					 "Cannot read NVM from section %d offset %d, length %d\n",
					 section, offset, length);
J
Johannes Berg 已提交
285 286 287 288 289
			return ret;
		}
		offset += ret;
	}

290 291
	iwl_mvm_nvm_fixups(mvm, section, data, offset);

292 293
	IWL_DEBUG_EEPROM(mvm->trans->dev,
			 "NVM section %d read completed\n", section);
J
Johannes Berg 已提交
294 295 296 297 298 299 300
	return offset;
}

static struct iwl_nvm_data *
iwl_parse_nvm_sections(struct iwl_mvm *mvm)
{
	struct iwl_nvm_section *sections = mvm->nvm_sections;
301
	const __le16 *hw, *sw, *calib, *regulatory, *mac_override, *phy_sku;
302
	bool lar_enabled;
303
	u32 mac_addr0, mac_addr1;
J
Johannes Berg 已提交
304 305

	/* Checking for required sections */
306 307 308
	if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
		if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
		    !mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data) {
309
			IWL_ERR(mvm, "Can't parse empty OTP/NVM sections\n");
310 311 312
			return NULL;
		}
	} else {
313
		/* SW and REGULATORY sections are mandatory */
314 315 316
		if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
		    !mvm->nvm_sections[NVM_SECTION_TYPE_REGULATORY].data) {
			IWL_ERR(mvm,
317
				"Can't parse empty family 8000 OTP/NVM sections\n");
318 319
			return NULL;
		}
320
		/* MAC_OVERRIDE or at least HW section must exist */
321
		if (!mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data &&
322 323 324 325 326
		    !mvm->nvm_sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data) {
			IWL_ERR(mvm,
				"Can't parse mac_address, empty sections\n");
			return NULL;
		}
327 328

		/* PHY_SKU section is mandatory in B0 */
329
		if (!mvm->nvm_sections[NVM_SECTION_TYPE_PHY_SKU].data) {
330 331 332 333
			IWL_ERR(mvm,
				"Can't parse phy_sku in B0, empty sections\n");
			return NULL;
		}
J
Johannes Berg 已提交
334 335 336 337 338
	}

	if (WARN_ON(!mvm->cfg))
		return NULL;

339 340 341 342
	/* read the mac address from WFMP registers */
	mac_addr0 = iwl_trans_read_prph(mvm->trans, WFMP_MAC_ADDR_0);
	mac_addr1 = iwl_trans_read_prph(mvm->trans, WFMP_MAC_ADDR_1);

343
	hw = (const __le16 *)sections[mvm->cfg->nvm_hw_section_num].data;
J
Johannes Berg 已提交
344 345
	sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data;
	calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data;
346 347 348
	regulatory = (const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY].data;
	mac_override =
		(const __le16 *)sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data;
349
	phy_sku = (const __le16 *)sections[NVM_SECTION_TYPE_PHY_SKU].data;
350

351
	lar_enabled = !iwlwifi_mod_params.lar_disable &&
352 353
		      fw_has_capa(&mvm->fw->ucode_capa,
				  IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
354

355
	return iwl_parse_nvm_data(mvm->trans->dev, mvm->cfg, hw, sw, calib,
356
				  regulatory, mac_override, phy_sku,
357
				  mvm->fw->valid_tx_ant, mvm->fw->valid_rx_ant,
358
				  lar_enabled, mac_addr0, mac_addr1);
J
Johannes Berg 已提交
359 360
}

361 362 363
#define MAX_NVM_FILE_LEN	16384

/*
364 365
 * Reads external NVM from a file into mvm->nvm_sections
 *
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
 * HOW TO CREATE THE NVM FILE FORMAT:
 * ------------------------------
 * 1. create hex file, format:
 *      3800 -> header
 *      0000 -> header
 *      5a40 -> data
 *
 *   rev - 6 bit (word1)
 *   len - 10 bit (word1)
 *   id - 4 bit (word2)
 *   rsv - 12 bit (word2)
 *
 * 2. flip 8bits with 8 bits per line to get the right NVM file format
 *
 * 3. create binary file from the hex file
 *
 * 4. save as "iNVM_xxx.bin" under /lib/firmware
 */
384
static int iwl_mvm_read_external_nvm(struct iwl_mvm *mvm)
385
{
386 387
	int ret, section_size;
	u16 section_id;
388 389 390 391 392 393
	const struct firmware *fw_entry;
	const struct {
		__le16 word1;
		__le16 word2;
		u8 data[];
	} *file_sec;
394 395
	const u8 *eof;
	u8 *temp;
396
	int max_section_size;
397
	const __le32 *dword_buff;
398 399 400

#define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
#define NVM_WORD2_ID(x) (x >> 12)
401 402
#define NVM_WORD2_LEN_FAMILY_8000(x) (2 * ((x & 0xFF) << 8 | x >> 8))
#define NVM_WORD1_ID_FAMILY_8000(x) (x >> 4)
403 404 405
#define NVM_HEADER_0	(0x2A504C54)
#define NVM_HEADER_1	(0x4E564D2A)
#define NVM_HEADER_SIZE	(4 * sizeof(u32))
406

407 408
	IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from external NVM\n");

409 410 411
	/* Maximal size depends on HW family and step */
	if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
		max_section_size = IWL_MAX_NVM_SECTION_SIZE;
412 413
	else
		max_section_size = IWL_MAX_NVM_8000_SECTION_SIZE;
414

415 416 417 418 419 420
	/*
	 * Obtain NVM image via request_firmware. Since we already used
	 * request_firmware_nowait() for the firmware binary load and only
	 * get here after that we assume the NVM request can be satisfied
	 * synchronously.
	 */
421
	ret = request_firmware(&fw_entry, mvm->nvm_file_name,
422 423 424
			       mvm->trans->dev);
	if (ret) {
		IWL_ERR(mvm, "ERROR: %s isn't available %d\n",
425
			mvm->nvm_file_name, ret);
426 427 428 429
		return ret;
	}

	IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n",
430
		 mvm->nvm_file_name, fw_entry->size);
431 432 433 434 435 436 437 438

	if (fw_entry->size > MAX_NVM_FILE_LEN) {
		IWL_ERR(mvm, "NVM file too large\n");
		ret = -EINVAL;
		goto out;
	}

	eof = fw_entry->data + fw_entry->size;
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	dword_buff = (__le32 *)fw_entry->data;

	/* some NVM file will contain a header.
	 * The header is identified by 2 dwords header as follow:
	 * dword[0] = 0x2A504C54
	 * dword[1] = 0x4E564D2A
	 *
	 * This header must be skipped when providing the NVM data to the FW.
	 */
	if (fw_entry->size > NVM_HEADER_SIZE &&
	    dword_buff[0] == cpu_to_le32(NVM_HEADER_0) &&
	    dword_buff[1] == cpu_to_le32(NVM_HEADER_1)) {
		file_sec = (void *)(fw_entry->data + NVM_HEADER_SIZE);
		IWL_INFO(mvm, "NVM Version %08X\n", le32_to_cpu(dword_buff[2]));
		IWL_INFO(mvm, "NVM Manufacturing date %08X\n",
			 le32_to_cpu(dword_buff[3]));
455 456 457 458 459 460 461 462 463

		/* nvm file validation, dword_buff[2] holds the file version */
		if ((CSR_HW_REV_STEP(mvm->trans->hw_rev) == SILICON_C_STEP &&
		     le32_to_cpu(dword_buff[2]) < 0xE4A) ||
		    (CSR_HW_REV_STEP(mvm->trans->hw_rev) == SILICON_B_STEP &&
		     le32_to_cpu(dword_buff[2]) >= 0xE4A)) {
			ret = -EFAULT;
			goto out;
		}
464 465 466
	} else {
		file_sec = (void *)fw_entry->data;
	}
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

	while (true) {
		if (file_sec->data > eof) {
			IWL_ERR(mvm,
				"ERROR - NVM file too short for section header\n");
			ret = -EINVAL;
			break;
		}

		/* check for EOF marker */
		if (!file_sec->word1 && !file_sec->word2) {
			ret = 0;
			break;
		}

482 483 484 485 486 487 488 489 490 491
		if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
			section_size =
				2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1));
			section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2));
		} else {
			section_size = 2 * NVM_WORD2_LEN_FAMILY_8000(
						le16_to_cpu(file_sec->word2));
			section_id = NVM_WORD1_ID_FAMILY_8000(
						le16_to_cpu(file_sec->word1));
		}
492

493
		if (section_size > max_section_size) {
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
			IWL_ERR(mvm, "ERROR - section too large (%d)\n",
				section_size);
			ret = -EINVAL;
			break;
		}

		if (!section_size) {
			IWL_ERR(mvm, "ERROR - section empty\n");
			ret = -EINVAL;
			break;
		}

		if (file_sec->data + section_size > eof) {
			IWL_ERR(mvm,
				"ERROR - NVM file too short for section (%d bytes)\n",
				section_size);
			ret = -EINVAL;
			break;
		}

514
		if (WARN(section_id >= NVM_MAX_NUM_SECTIONS,
515 516 517 518 519
			 "Invalid NVM section ID %d\n", section_id)) {
			ret = -EINVAL;
			break;
		}

520 521 522 523 524
		temp = kmemdup(file_sec->data, section_size, GFP_KERNEL);
		if (!temp) {
			ret = -ENOMEM;
			break;
		}
525 526 527

		iwl_mvm_nvm_fixups(mvm, section_id, temp, section_size);

528
		kfree(mvm->nvm_sections[section_id].data);
529 530
		mvm->nvm_sections[section_id].data = temp;
		mvm->nvm_sections[section_id].length = section_size;
531 532 533 534 535 536 537 538 539

		/* advance to the next section */
		file_sec = (void *)(file_sec->data + section_size);
	}
out:
	release_firmware(fw_entry);
	return ret;
}

540 541 542
/* Loads the NVM data stored in mvm->nvm_sections into the NIC */
int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm)
{
543
	int i, ret = 0;
544 545 546 547
	struct iwl_nvm_section *sections = mvm->nvm_sections;

	IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n");

548 549 550 551 552
	for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) {
		if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length)
			continue;
		ret = iwl_nvm_write_section(mvm, i, sections[i].data,
					    sections[i].length);
553 554 555 556 557 558 559 560
		if (ret < 0) {
			IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);
			break;
		}
	}
	return ret;
}

561
int iwl_nvm_init(struct iwl_mvm *mvm, bool read_nvm_from_nic)
J
Johannes Berg 已提交
562
{
563
	int ret, section;
564
	u32 size_read = 0;
J
Johannes Berg 已提交
565
	u8 *nvm_buffer, *temp;
566 567
	const char *nvm_file_B = mvm->cfg->default_nvm_file_B_step;
	const char *nvm_file_C = mvm->cfg->default_nvm_file_C_step;
J
Johannes Berg 已提交
568

569 570 571
	if (WARN_ON_ONCE(mvm->cfg->nvm_hw_section_num >= NVM_MAX_NUM_SECTIONS))
		return -EINVAL;

572
	/* load NVM values from nic */
573
	if (read_nvm_from_nic) {
574 575 576 577 578 579 580
		/* Read From FW NVM */
		IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");

		nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size,
				     GFP_KERNEL);
		if (!nvm_buffer)
			return -ENOMEM;
581
		for (section = 0; section < NVM_MAX_NUM_SECTIONS; section++) {
582
			/* we override the constness for initial read */
583 584
			ret = iwl_nvm_read_section(mvm, section, nvm_buffer,
						   size_read);
585
			if (ret < 0)
586
				continue;
587
			size_read += ret;
588 589 590 591 592
			temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
			if (!temp) {
				ret = -ENOMEM;
				break;
			}
593 594 595

			iwl_mvm_nvm_fixups(mvm, section, temp, ret);

596 597
			mvm->nvm_sections[section].data = temp;
			mvm->nvm_sections[section].length = ret;
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

#ifdef CONFIG_IWLWIFI_DEBUGFS
			switch (section) {
			case NVM_SECTION_TYPE_SW:
				mvm->nvm_sw_blob.data = temp;
				mvm->nvm_sw_blob.size  = ret;
				break;
			case NVM_SECTION_TYPE_CALIBRATION:
				mvm->nvm_calib_blob.data = temp;
				mvm->nvm_calib_blob.size  = ret;
				break;
			case NVM_SECTION_TYPE_PRODUCTION:
				mvm->nvm_prod_blob.data = temp;
				mvm->nvm_prod_blob.size  = ret;
				break;
613 614 615 616
			case NVM_SECTION_TYPE_PHY_SKU:
				mvm->nvm_phy_sku_blob.data = temp;
				mvm->nvm_phy_sku_blob.size  = ret;
				break;
617
			default:
618 619 620 621 622
				if (section == mvm->cfg->nvm_hw_section_num) {
					mvm->nvm_hw_blob.data = temp;
					mvm->nvm_hw_blob.size = ret;
					break;
				}
623 624
			}
#endif
J
Johannes Berg 已提交
625
		}
E
Eran Harary 已提交
626 627
		if (!size_read)
			IWL_ERR(mvm, "OTP is blank\n");
628
		kfree(nvm_buffer);
J
Johannes Berg 已提交
629 630
	}

631
	/* Only if PNVM selected in the mod param - load external NVM  */
632
	if (mvm->nvm_file_name) {
633
		/* read External NVM file from the mod param */
634
		ret = iwl_mvm_read_external_nvm(mvm);
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
		if (ret) {
			/* choose the nvm_file name according to the
			 * HW step
			 */
			if (CSR_HW_REV_STEP(mvm->trans->hw_rev) ==
			    SILICON_B_STEP)
				mvm->nvm_file_name = nvm_file_B;
			else
				mvm->nvm_file_name = nvm_file_C;

			if (ret == -EFAULT && mvm->nvm_file_name) {
				/* in case nvm file was failed try again */
				ret = iwl_mvm_read_external_nvm(mvm);
				if (ret)
					return ret;
			} else {
				return ret;
			}
		}
654 655 656
	}

	/* parse the relevant nvm sections */
657
	mvm->nvm_data = iwl_parse_nvm_sections(mvm);
658 659
	if (!mvm->nvm_data)
		return -ENODATA;
660 661
	IWL_DEBUG_EEPROM(mvm->trans->dev, "nvm version = %x\n",
			 mvm->nvm_data->nvm_version);
J
Johannes Berg 已提交
662

663
	return 0;
J
Johannes Berg 已提交
664
}
665 666

struct iwl_mcc_update_resp *
667 668
iwl_mvm_update_mcc(struct iwl_mvm *mvm, const char *alpha2,
		   enum iwl_mcc_source src_id)
669 670 671
{
	struct iwl_mcc_update_cmd mcc_update_cmd = {
		.mcc = cpu_to_le16(alpha2[0] << 8 | alpha2[1]),
672
		.source_id = (u8)src_id,
673 674
	};
	struct iwl_mcc_update_resp *mcc_resp, *resp_cp = NULL;
675
	struct iwl_mcc_update_resp_v1 *mcc_resp_v1 = NULL;
676 677 678 679 680 681 682 683 684 685 686
	struct iwl_rx_packet *pkt;
	struct iwl_host_cmd cmd = {
		.id = MCC_UPDATE_CMD,
		.flags = CMD_WANT_SKB,
		.data = { &mcc_update_cmd },
	};

	int ret;
	u32 status;
	int resp_len, n_channels;
	u16 mcc;
687 688
	bool resp_v2 = fw_has_capa(&mvm->fw->ucode_capa,
				   IWL_UCODE_TLV_CAPA_LAR_SUPPORT_V2);
689 690 691 692 693

	if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
		return ERR_PTR(-EOPNOTSUPP);

	cmd.len[0] = sizeof(struct iwl_mcc_update_cmd);
694 695
	if (!resp_v2)
		cmd.len[0] = sizeof(struct iwl_mcc_update_cmd_v1);
696

697 698
	IWL_DEBUG_LAR(mvm, "send MCC update to FW with '%c%c' src = %d\n",
		      alpha2[0], alpha2[1], src_id);
699 700 701 702 703 704 705 706

	ret = iwl_mvm_send_cmd(mvm, &cmd);
	if (ret)
		return ERR_PTR(ret);

	pkt = cmd.resp_pkt;

	/* Extract MCC response */
707 708 709 710 711 712 713
	if (resp_v2) {
		mcc_resp = (void *)pkt->data;
		n_channels =  __le32_to_cpu(mcc_resp->n_channels);
	} else {
		mcc_resp_v1 = (void *)pkt->data;
		n_channels =  __le32_to_cpu(mcc_resp_v1->n_channels);
	}
714

715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
	resp_len = sizeof(struct iwl_mcc_update_resp) + n_channels *
		sizeof(__le32);

	resp_cp = kzalloc(resp_len, GFP_KERNEL);
	if (!resp_cp) {
		ret = -ENOMEM;
		goto exit;
	}

	if (resp_v2) {
		memcpy(resp_cp, mcc_resp, resp_len);
	} else {
		resp_cp->status = mcc_resp_v1->status;
		resp_cp->mcc = mcc_resp_v1->mcc;
		resp_cp->cap = mcc_resp_v1->cap;
		resp_cp->source_id = mcc_resp_v1->source_id;
		resp_cp->n_channels = mcc_resp_v1->n_channels;
		memcpy(resp_cp->channels, mcc_resp_v1->channels,
		       n_channels * sizeof(__le32));
	}

	status = le32_to_cpu(resp_cp->status);

	mcc = le16_to_cpu(resp_cp->mcc);
739 740 741 742

	/* W/A for a FW/NVM issue - returns 0x00 for the world domain */
	if (mcc == 0) {
		mcc = 0x3030;  /* "00" - world */
743
		resp_cp->mcc = cpu_to_le16(mcc);
744 745 746 747 748
	}

	IWL_DEBUG_LAR(mvm,
		      "MCC response status: 0x%x. new MCC: 0x%x ('%c%c') change: %d n_chans: %d\n",
		      status, mcc, mcc >> 8, mcc & 0xff,
749
		      !!(status == MCC_RESP_NEW_CHAN_PROFILE), n_channels);
750 751 752 753 754 755 756

exit:
	iwl_free_resp(&cmd);
	if (ret)
		return ERR_PTR(ret);
	return resp_cp;
}
757

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
#ifdef CONFIG_ACPI
#define WRD_METHOD		"WRDD"
#define WRDD_WIFI		(0x07)
#define WRDD_WIGIG		(0x10)

static u32 iwl_mvm_wrdd_get_mcc(struct iwl_mvm *mvm, union acpi_object *wrdd)
{
	union acpi_object *mcc_pkg, *domain_type, *mcc_value;
	u32 i;

	if (wrdd->type != ACPI_TYPE_PACKAGE ||
	    wrdd->package.count < 2 ||
	    wrdd->package.elements[0].type != ACPI_TYPE_INTEGER ||
	    wrdd->package.elements[0].integer.value != 0) {
		IWL_DEBUG_LAR(mvm, "Unsupported wrdd structure\n");
		return 0;
	}

	for (i = 1 ; i < wrdd->package.count ; ++i) {
		mcc_pkg = &wrdd->package.elements[i];

		if (mcc_pkg->type != ACPI_TYPE_PACKAGE ||
		    mcc_pkg->package.count < 2 ||
		    mcc_pkg->package.elements[0].type != ACPI_TYPE_INTEGER ||
		    mcc_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) {
			mcc_pkg = NULL;
			continue;
		}

		domain_type = &mcc_pkg->package.elements[0];
		if (domain_type->integer.value == WRDD_WIFI)
			break;

		mcc_pkg = NULL;
	}

	if (mcc_pkg) {
		mcc_value = &mcc_pkg->package.elements[1];
		return mcc_value->integer.value;
	}

	return 0;
}

static int iwl_mvm_get_bios_mcc(struct iwl_mvm *mvm, char *mcc)
{
	acpi_handle root_handle;
	acpi_handle handle;
	struct acpi_buffer wrdd = {ACPI_ALLOCATE_BUFFER, NULL};
	acpi_status status;
	u32 mcc_val;
	struct pci_dev *pdev = to_pci_dev(mvm->dev);

	root_handle = ACPI_HANDLE(&pdev->dev);
	if (!root_handle) {
		IWL_DEBUG_LAR(mvm,
			      "Could not retrieve root port ACPI handle\n");
		return -ENOENT;
	}

	/* Get the method's handle */
	status = acpi_get_handle(root_handle, (acpi_string)WRD_METHOD, &handle);
	if (ACPI_FAILURE(status)) {
		IWL_DEBUG_LAR(mvm, "WRD method not found\n");
		return -ENOENT;
	}

	/* Call WRDD with no arguments */
	status = acpi_evaluate_object(handle, NULL, NULL, &wrdd);
	if (ACPI_FAILURE(status)) {
		IWL_DEBUG_LAR(mvm, "WRDC invocation failed (0x%x)\n", status);
		return -ENOENT;
	}

	mcc_val = iwl_mvm_wrdd_get_mcc(mvm, wrdd.pointer);
	kfree(wrdd.pointer);
	if (!mcc_val)
		return -ENOENT;

	mcc[0] = (mcc_val >> 8) & 0xff;
	mcc[1] = mcc_val & 0xff;
	mcc[2] = '\0';
	return 0;
}
#else /* CONFIG_ACPI */
static int iwl_mvm_get_bios_mcc(struct iwl_mvm *mvm, char *mcc)
{
	return -ENOENT;
}
#endif

849 850
int iwl_mvm_init_mcc(struct iwl_mvm *mvm)
{
851 852
	bool tlv_lar;
	bool nvm_lar;
853 854
	int retval;
	struct ieee80211_regdomain *regd;
855
	char mcc[3];
856 857

	if (mvm->cfg->device_family == IWL_DEVICE_FAMILY_8000) {
858 859
		tlv_lar = fw_has_capa(&mvm->fw->ucode_capa,
				      IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
860 861 862 863 864 865 866 867
		nvm_lar = mvm->nvm_data->lar_enabled;
		if (tlv_lar != nvm_lar)
			IWL_INFO(mvm,
				 "Conflict between TLV & NVM regarding enabling LAR (TLV = %s NVM =%s)\n",
				 tlv_lar ? "enabled" : "disabled",
				 nvm_lar ? "enabled" : "disabled");
	}

868 869 870 871
	if (!iwl_mvm_is_lar_supported(mvm))
		return 0;

	/*
872
	 * try to replay the last set MCC to FW. If it doesn't exist,
873 874
	 * queue an update to cfg80211 to retrieve the default alpha2 from FW.
	 */
875 876 877
	retval = iwl_mvm_init_fw_regd(mvm);
	if (retval != -ENOENT)
		return retval;
878 879

	/*
880 881
	 * Driver regulatory hint for initial update, this also informs the
	 * firmware we support wifi location updates.
882 883
	 * Disallow scans that might crash the FW while the LAR regdomain
	 * is not set.
884
	 */
885
	mvm->lar_regdom_set = false;
886

887
	regd = iwl_mvm_get_current_regdomain(mvm, NULL);
888 889 890
	if (IS_ERR_OR_NULL(regd))
		return -EIO;

891 892 893 894
	if (iwl_mvm_is_wifi_mcc_supported(mvm) &&
	    !iwl_mvm_get_bios_mcc(mvm, mcc)) {
		kfree(regd);
		regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc,
895
					     MCC_SOURCE_BIOS, NULL);
896 897 898 899
		if (IS_ERR_OR_NULL(regd))
			return -EIO;
	}

900 901 902
	retval = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd);
	kfree(regd);
	return retval;
903 904
}

905 906
void iwl_mvm_rx_chub_update_mcc(struct iwl_mvm *mvm,
				struct iwl_rx_cmd_buffer *rxb)
907 908 909
{
	struct iwl_rx_packet *pkt = rxb_addr(rxb);
	struct iwl_mcc_chub_notif *notif = (void *)pkt->data;
910
	enum iwl_mcc_source src;
911
	char mcc[3];
912 913 914
	struct ieee80211_regdomain *regd;

	lockdep_assert_held(&mvm->mutex);
915 916

	if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
917
		return;
918 919 920 921

	mcc[0] = notif->mcc >> 8;
	mcc[1] = notif->mcc & 0xff;
	mcc[2] = '\0';
922
	src = notif->source_id;
923 924

	IWL_DEBUG_LAR(mvm,
925 926
		      "RX: received chub update mcc cmd (mcc '%s' src %d)\n",
		      mcc, src);
927
	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc, src, NULL);
928
	if (IS_ERR_OR_NULL(regd))
929
		return;
930 931 932

	regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
	kfree(regd);
933
}