nvm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24. * USA
  25. *
  26. * The full GNU General Public License is included in this distribution
  27. * in the file called COPYING.
  28. *
  29. * Contact Information:
  30. * Intel Linux Wireless <linuxwifi@intel.com>
  31. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32. *
  33. * BSD LICENSE
  34. *
  35. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  36. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  37. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  38. * All rights reserved.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * * Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in
  48. * the documentation and/or other materials provided with the
  49. * distribution.
  50. * * Neither the name Intel Corporation nor the names of its
  51. * contributors may be used to endorse or promote products derived
  52. * from this software without specific prior written permission.
  53. *
  54. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. *****************************************************************************/
  67. #include <linux/firmware.h>
  68. #include <linux/rtnetlink.h>
  69. #include "iwl-trans.h"
  70. #include "iwl-csr.h"
  71. #include "mvm.h"
  72. #include "iwl-eeprom-parse.h"
  73. #include "iwl-eeprom-read.h"
  74. #include "iwl-nvm-parse.h"
  75. #include "iwl-prph.h"
  76. #include "fw/acpi.h"
  77. /* Default NVM size to read */
  78. #define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024)
  79. #define IWL_MAX_NVM_SECTION_SIZE 0x1b58
  80. #define IWL_MAX_EXT_NVM_SECTION_SIZE 0x1ffc
  81. #define NVM_WRITE_OPCODE 1
  82. #define NVM_READ_OPCODE 0
  83. /* load nvm chunk response */
  84. enum {
  85. READ_NVM_CHUNK_SUCCEED = 0,
  86. READ_NVM_CHUNK_NOT_VALID_ADDRESS = 1
  87. };
  88. /*
  89. * prepare the NVM host command w/ the pointers to the nvm buffer
  90. * and send it to fw
  91. */
  92. static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section,
  93. u16 offset, u16 length, const u8 *data)
  94. {
  95. struct iwl_nvm_access_cmd nvm_access_cmd = {
  96. .offset = cpu_to_le16(offset),
  97. .length = cpu_to_le16(length),
  98. .type = cpu_to_le16(section),
  99. .op_code = NVM_WRITE_OPCODE,
  100. };
  101. struct iwl_host_cmd cmd = {
  102. .id = NVM_ACCESS_CMD,
  103. .len = { sizeof(struct iwl_nvm_access_cmd), length },
  104. .flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
  105. .data = { &nvm_access_cmd, data },
  106. /* data may come from vmalloc, so use _DUP */
  107. .dataflags = { 0, IWL_HCMD_DFL_DUP },
  108. };
  109. struct iwl_rx_packet *pkt;
  110. struct iwl_nvm_access_resp *nvm_resp;
  111. int ret;
  112. ret = iwl_mvm_send_cmd(mvm, &cmd);
  113. if (ret)
  114. return ret;
  115. pkt = cmd.resp_pkt;
  116. /* Extract & check NVM write response */
  117. nvm_resp = (void *)pkt->data;
  118. if (le16_to_cpu(nvm_resp->status) != READ_NVM_CHUNK_SUCCEED) {
  119. IWL_ERR(mvm,
  120. "NVM access write command failed for section %u (status = 0x%x)\n",
  121. section, le16_to_cpu(nvm_resp->status));
  122. ret = -EIO;
  123. }
  124. iwl_free_resp(&cmd);
  125. return ret;
  126. }
  127. static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section,
  128. u16 offset, u16 length, u8 *data)
  129. {
  130. struct iwl_nvm_access_cmd nvm_access_cmd = {
  131. .offset = cpu_to_le16(offset),
  132. .length = cpu_to_le16(length),
  133. .type = cpu_to_le16(section),
  134. .op_code = NVM_READ_OPCODE,
  135. };
  136. struct iwl_nvm_access_resp *nvm_resp;
  137. struct iwl_rx_packet *pkt;
  138. struct iwl_host_cmd cmd = {
  139. .id = NVM_ACCESS_CMD,
  140. .flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
  141. .data = { &nvm_access_cmd, },
  142. };
  143. int ret, bytes_read, offset_read;
  144. u8 *resp_data;
  145. cmd.len[0] = sizeof(struct iwl_nvm_access_cmd);
  146. ret = iwl_mvm_send_cmd(mvm, &cmd);
  147. if (ret)
  148. return ret;
  149. pkt = cmd.resp_pkt;
  150. /* Extract NVM response */
  151. nvm_resp = (void *)pkt->data;
  152. ret = le16_to_cpu(nvm_resp->status);
  153. bytes_read = le16_to_cpu(nvm_resp->length);
  154. offset_read = le16_to_cpu(nvm_resp->offset);
  155. resp_data = nvm_resp->data;
  156. if (ret) {
  157. if ((offset != 0) &&
  158. (ret == READ_NVM_CHUNK_NOT_VALID_ADDRESS)) {
  159. /*
  160. * meaning of NOT_VALID_ADDRESS:
  161. * driver try to read chunk from address that is
  162. * multiple of 2K and got an error since addr is empty.
  163. * meaning of (offset != 0): driver already
  164. * read valid data from another chunk so this case
  165. * is not an error.
  166. */
  167. IWL_DEBUG_EEPROM(mvm->trans->dev,
  168. "NVM access command failed on offset 0x%x since that section size is multiple 2K\n",
  169. offset);
  170. ret = 0;
  171. } else {
  172. IWL_DEBUG_EEPROM(mvm->trans->dev,
  173. "NVM access command failed with status %d (device: %s)\n",
  174. ret, mvm->cfg->name);
  175. ret = -EIO;
  176. }
  177. goto exit;
  178. }
  179. if (offset_read != offset) {
  180. IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n",
  181. offset_read);
  182. ret = -EINVAL;
  183. goto exit;
  184. }
  185. /* Write data to NVM */
  186. memcpy(data + offset, resp_data, bytes_read);
  187. ret = bytes_read;
  188. exit:
  189. iwl_free_resp(&cmd);
  190. return ret;
  191. }
  192. static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section,
  193. const u8 *data, u16 length)
  194. {
  195. int offset = 0;
  196. /* copy data in chunks of 2k (and remainder if any) */
  197. while (offset < length) {
  198. int chunk_size, ret;
  199. chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE,
  200. length - offset);
  201. ret = iwl_nvm_write_chunk(mvm, section, offset,
  202. chunk_size, data + offset);
  203. if (ret < 0)
  204. return ret;
  205. offset += chunk_size;
  206. }
  207. return 0;
  208. }
  209. static void iwl_mvm_nvm_fixups(struct iwl_mvm *mvm, unsigned int section,
  210. u8 *data, unsigned int len)
  211. {
  212. #define IWL_4165_DEVICE_ID 0x5501
  213. #define NVM_SKU_CAP_MIMO_DISABLE BIT(5)
  214. if (section == NVM_SECTION_TYPE_PHY_SKU &&
  215. mvm->trans->hw_id == IWL_4165_DEVICE_ID && data && len >= 5 &&
  216. (data[4] & NVM_SKU_CAP_MIMO_DISABLE))
  217. /* OTP 0x52 bug work around: it's a 1x1 device */
  218. data[3] = ANT_B | (ANT_B << 4);
  219. }
  220. /*
  221. * Reads an NVM section completely.
  222. * NICs prior to 7000 family doesn't have a real NVM, but just read
  223. * section 0 which is the EEPROM. Because the EEPROM reading is unlimited
  224. * by uCode, we need to manually check in this case that we don't
  225. * overflow and try to read more than the EEPROM size.
  226. * For 7000 family NICs, we supply the maximal size we can read, and
  227. * the uCode fills the response with as much data as we can,
  228. * without overflowing, so no check is needed.
  229. */
  230. static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section,
  231. u8 *data, u32 size_read)
  232. {
  233. u16 length, offset = 0;
  234. int ret;
  235. /* Set nvm section read length */
  236. length = IWL_NVM_DEFAULT_CHUNK_SIZE;
  237. ret = length;
  238. /* Read the NVM until exhausted (reading less than requested) */
  239. while (ret == length) {
  240. /* Check no memory assumptions fail and cause an overflow */
  241. if ((size_read + offset + length) >
  242. mvm->cfg->base_params->eeprom_size) {
  243. IWL_ERR(mvm, "EEPROM size is too small for NVM\n");
  244. return -ENOBUFS;
  245. }
  246. ret = iwl_nvm_read_chunk(mvm, section, offset, length, data);
  247. if (ret < 0) {
  248. IWL_DEBUG_EEPROM(mvm->trans->dev,
  249. "Cannot read NVM from section %d offset %d, length %d\n",
  250. section, offset, length);
  251. return ret;
  252. }
  253. offset += ret;
  254. }
  255. iwl_mvm_nvm_fixups(mvm, section, data, offset);
  256. IWL_DEBUG_EEPROM(mvm->trans->dev,
  257. "NVM section %d read completed\n", section);
  258. return offset;
  259. }
  260. static struct iwl_nvm_data *
  261. iwl_parse_nvm_sections(struct iwl_mvm *mvm)
  262. {
  263. struct iwl_nvm_section *sections = mvm->nvm_sections;
  264. const __be16 *hw;
  265. const __le16 *sw, *calib, *regulatory, *mac_override, *phy_sku;
  266. bool lar_enabled;
  267. int regulatory_type;
  268. /* Checking for required sections */
  269. if (mvm->trans->cfg->nvm_type != IWL_NVM_EXT) {
  270. if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
  271. !mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data) {
  272. IWL_ERR(mvm, "Can't parse empty OTP/NVM sections\n");
  273. return NULL;
  274. }
  275. } else {
  276. if (mvm->trans->cfg->nvm_type == IWL_NVM_SDP)
  277. regulatory_type = NVM_SECTION_TYPE_REGULATORY_SDP;
  278. else
  279. regulatory_type = NVM_SECTION_TYPE_REGULATORY;
  280. /* SW and REGULATORY sections are mandatory */
  281. if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
  282. !mvm->nvm_sections[regulatory_type].data) {
  283. IWL_ERR(mvm,
  284. "Can't parse empty family 8000 OTP/NVM sections\n");
  285. return NULL;
  286. }
  287. /* MAC_OVERRIDE or at least HW section must exist */
  288. if (!mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data &&
  289. !mvm->nvm_sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data) {
  290. IWL_ERR(mvm,
  291. "Can't parse mac_address, empty sections\n");
  292. return NULL;
  293. }
  294. /* PHY_SKU section is mandatory in B0 */
  295. if (!mvm->nvm_sections[NVM_SECTION_TYPE_PHY_SKU].data) {
  296. IWL_ERR(mvm,
  297. "Can't parse phy_sku in B0, empty sections\n");
  298. return NULL;
  299. }
  300. }
  301. hw = (const __be16 *)sections[mvm->cfg->nvm_hw_section_num].data;
  302. sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data;
  303. calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data;
  304. mac_override =
  305. (const __le16 *)sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data;
  306. phy_sku = (const __le16 *)sections[NVM_SECTION_TYPE_PHY_SKU].data;
  307. regulatory = mvm->trans->cfg->nvm_type == IWL_NVM_SDP ?
  308. (const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY_SDP].data :
  309. (const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY].data;
  310. lar_enabled = !iwlwifi_mod_params.lar_disable &&
  311. fw_has_capa(&mvm->fw->ucode_capa,
  312. IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
  313. return iwl_parse_nvm_data(mvm->trans, mvm->cfg, hw, sw, calib,
  314. regulatory, mac_override, phy_sku,
  315. mvm->fw->valid_tx_ant, mvm->fw->valid_rx_ant,
  316. lar_enabled);
  317. }
  318. #define MAX_NVM_FILE_LEN 16384
  319. /*
  320. * Reads external NVM from a file into mvm->nvm_sections
  321. *
  322. * HOW TO CREATE THE NVM FILE FORMAT:
  323. * ------------------------------
  324. * 1. create hex file, format:
  325. * 3800 -> header
  326. * 0000 -> header
  327. * 5a40 -> data
  328. *
  329. * rev - 6 bit (word1)
  330. * len - 10 bit (word1)
  331. * id - 4 bit (word2)
  332. * rsv - 12 bit (word2)
  333. *
  334. * 2. flip 8bits with 8 bits per line to get the right NVM file format
  335. *
  336. * 3. create binary file from the hex file
  337. *
  338. * 4. save as "iNVM_xxx.bin" under /lib/firmware
  339. */
  340. int iwl_mvm_read_external_nvm(struct iwl_mvm *mvm)
  341. {
  342. int ret, section_size;
  343. u16 section_id;
  344. const struct firmware *fw_entry;
  345. const struct {
  346. __le16 word1;
  347. __le16 word2;
  348. u8 data[];
  349. } *file_sec;
  350. const u8 *eof;
  351. u8 *temp;
  352. int max_section_size;
  353. const __le32 *dword_buff;
  354. #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
  355. #define NVM_WORD2_ID(x) (x >> 12)
  356. #define EXT_NVM_WORD2_LEN(x) (2 * (((x) & 0xFF) << 8 | (x) >> 8))
  357. #define EXT_NVM_WORD1_ID(x) ((x) >> 4)
  358. #define NVM_HEADER_0 (0x2A504C54)
  359. #define NVM_HEADER_1 (0x4E564D2A)
  360. #define NVM_HEADER_SIZE (4 * sizeof(u32))
  361. IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from external NVM\n");
  362. /* Maximal size depends on NVM version */
  363. if (mvm->trans->cfg->nvm_type != IWL_NVM_EXT)
  364. max_section_size = IWL_MAX_NVM_SECTION_SIZE;
  365. else
  366. max_section_size = IWL_MAX_EXT_NVM_SECTION_SIZE;
  367. /*
  368. * Obtain NVM image via request_firmware. Since we already used
  369. * request_firmware_nowait() for the firmware binary load and only
  370. * get here after that we assume the NVM request can be satisfied
  371. * synchronously.
  372. */
  373. ret = request_firmware(&fw_entry, mvm->nvm_file_name,
  374. mvm->trans->dev);
  375. if (ret) {
  376. IWL_ERR(mvm, "ERROR: %s isn't available %d\n",
  377. mvm->nvm_file_name, ret);
  378. return ret;
  379. }
  380. IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n",
  381. mvm->nvm_file_name, fw_entry->size);
  382. if (fw_entry->size > MAX_NVM_FILE_LEN) {
  383. IWL_ERR(mvm, "NVM file too large\n");
  384. ret = -EINVAL;
  385. goto out;
  386. }
  387. eof = fw_entry->data + fw_entry->size;
  388. dword_buff = (__le32 *)fw_entry->data;
  389. /* some NVM file will contain a header.
  390. * The header is identified by 2 dwords header as follow:
  391. * dword[0] = 0x2A504C54
  392. * dword[1] = 0x4E564D2A
  393. *
  394. * This header must be skipped when providing the NVM data to the FW.
  395. */
  396. if (fw_entry->size > NVM_HEADER_SIZE &&
  397. dword_buff[0] == cpu_to_le32(NVM_HEADER_0) &&
  398. dword_buff[1] == cpu_to_le32(NVM_HEADER_1)) {
  399. file_sec = (void *)(fw_entry->data + NVM_HEADER_SIZE);
  400. IWL_INFO(mvm, "NVM Version %08X\n", le32_to_cpu(dword_buff[2]));
  401. IWL_INFO(mvm, "NVM Manufacturing date %08X\n",
  402. le32_to_cpu(dword_buff[3]));
  403. /* nvm file validation, dword_buff[2] holds the file version */
  404. if (mvm->trans->cfg->device_family == IWL_DEVICE_FAMILY_8000 &&
  405. CSR_HW_REV_STEP(mvm->trans->hw_rev) == SILICON_C_STEP &&
  406. le32_to_cpu(dword_buff[2]) < 0xE4A) {
  407. ret = -EFAULT;
  408. goto out;
  409. }
  410. } else {
  411. file_sec = (void *)fw_entry->data;
  412. }
  413. while (true) {
  414. if (file_sec->data > eof) {
  415. IWL_ERR(mvm,
  416. "ERROR - NVM file too short for section header\n");
  417. ret = -EINVAL;
  418. break;
  419. }
  420. /* check for EOF marker */
  421. if (!file_sec->word1 && !file_sec->word2) {
  422. ret = 0;
  423. break;
  424. }
  425. if (mvm->trans->cfg->nvm_type != IWL_NVM_EXT) {
  426. section_size =
  427. 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1));
  428. section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2));
  429. } else {
  430. section_size = 2 * EXT_NVM_WORD2_LEN(
  431. le16_to_cpu(file_sec->word2));
  432. section_id = EXT_NVM_WORD1_ID(
  433. le16_to_cpu(file_sec->word1));
  434. }
  435. if (section_size > max_section_size) {
  436. IWL_ERR(mvm, "ERROR - section too large (%d)\n",
  437. section_size);
  438. ret = -EINVAL;
  439. break;
  440. }
  441. if (!section_size) {
  442. IWL_ERR(mvm, "ERROR - section empty\n");
  443. ret = -EINVAL;
  444. break;
  445. }
  446. if (file_sec->data + section_size > eof) {
  447. IWL_ERR(mvm,
  448. "ERROR - NVM file too short for section (%d bytes)\n",
  449. section_size);
  450. ret = -EINVAL;
  451. break;
  452. }
  453. if (WARN(section_id >= NVM_MAX_NUM_SECTIONS,
  454. "Invalid NVM section ID %d\n", section_id)) {
  455. ret = -EINVAL;
  456. break;
  457. }
  458. temp = kmemdup(file_sec->data, section_size, GFP_KERNEL);
  459. if (!temp) {
  460. ret = -ENOMEM;
  461. break;
  462. }
  463. iwl_mvm_nvm_fixups(mvm, section_id, temp, section_size);
  464. kfree(mvm->nvm_sections[section_id].data);
  465. mvm->nvm_sections[section_id].data = temp;
  466. mvm->nvm_sections[section_id].length = section_size;
  467. /* advance to the next section */
  468. file_sec = (void *)(file_sec->data + section_size);
  469. }
  470. out:
  471. release_firmware(fw_entry);
  472. return ret;
  473. }
  474. /* Loads the NVM data stored in mvm->nvm_sections into the NIC */
  475. int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm)
  476. {
  477. int i, ret = 0;
  478. struct iwl_nvm_section *sections = mvm->nvm_sections;
  479. IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n");
  480. for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) {
  481. if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length)
  482. continue;
  483. ret = iwl_nvm_write_section(mvm, i, sections[i].data,
  484. sections[i].length);
  485. if (ret < 0) {
  486. IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);
  487. break;
  488. }
  489. }
  490. return ret;
  491. }
  492. int iwl_nvm_init(struct iwl_mvm *mvm)
  493. {
  494. int ret, section;
  495. u32 size_read = 0;
  496. u8 *nvm_buffer, *temp;
  497. const char *nvm_file_C = mvm->cfg->default_nvm_file_C_step;
  498. if (WARN_ON_ONCE(mvm->cfg->nvm_hw_section_num >= NVM_MAX_NUM_SECTIONS))
  499. return -EINVAL;
  500. /* load NVM values from nic */
  501. /* Read From FW NVM */
  502. IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");
  503. nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size,
  504. GFP_KERNEL);
  505. if (!nvm_buffer)
  506. return -ENOMEM;
  507. for (section = 0; section < NVM_MAX_NUM_SECTIONS; section++) {
  508. /* we override the constness for initial read */
  509. ret = iwl_nvm_read_section(mvm, section, nvm_buffer,
  510. size_read);
  511. if (ret < 0)
  512. continue;
  513. size_read += ret;
  514. temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
  515. if (!temp) {
  516. ret = -ENOMEM;
  517. break;
  518. }
  519. iwl_mvm_nvm_fixups(mvm, section, temp, ret);
  520. mvm->nvm_sections[section].data = temp;
  521. mvm->nvm_sections[section].length = ret;
  522. #ifdef CONFIG_IWLWIFI_DEBUGFS
  523. switch (section) {
  524. case NVM_SECTION_TYPE_SW:
  525. mvm->nvm_sw_blob.data = temp;
  526. mvm->nvm_sw_blob.size = ret;
  527. break;
  528. case NVM_SECTION_TYPE_CALIBRATION:
  529. mvm->nvm_calib_blob.data = temp;
  530. mvm->nvm_calib_blob.size = ret;
  531. break;
  532. case NVM_SECTION_TYPE_PRODUCTION:
  533. mvm->nvm_prod_blob.data = temp;
  534. mvm->nvm_prod_blob.size = ret;
  535. break;
  536. case NVM_SECTION_TYPE_PHY_SKU:
  537. mvm->nvm_phy_sku_blob.data = temp;
  538. mvm->nvm_phy_sku_blob.size = ret;
  539. break;
  540. default:
  541. if (section == mvm->cfg->nvm_hw_section_num) {
  542. mvm->nvm_hw_blob.data = temp;
  543. mvm->nvm_hw_blob.size = ret;
  544. break;
  545. }
  546. }
  547. #endif
  548. }
  549. if (!size_read)
  550. IWL_ERR(mvm, "OTP is blank\n");
  551. kfree(nvm_buffer);
  552. /* Only if PNVM selected in the mod param - load external NVM */
  553. if (mvm->nvm_file_name) {
  554. /* read External NVM file from the mod param */
  555. ret = iwl_mvm_read_external_nvm(mvm);
  556. if (ret) {
  557. mvm->nvm_file_name = nvm_file_C;
  558. if ((ret == -EFAULT || ret == -ENOENT) &&
  559. mvm->nvm_file_name) {
  560. /* in case nvm file was failed try again */
  561. ret = iwl_mvm_read_external_nvm(mvm);
  562. if (ret)
  563. return ret;
  564. } else {
  565. return ret;
  566. }
  567. }
  568. }
  569. /* parse the relevant nvm sections */
  570. mvm->nvm_data = iwl_parse_nvm_sections(mvm);
  571. if (!mvm->nvm_data)
  572. return -ENODATA;
  573. IWL_DEBUG_EEPROM(mvm->trans->dev, "nvm version = %x\n",
  574. mvm->nvm_data->nvm_version);
  575. return 0;
  576. }
  577. struct iwl_mcc_update_resp *
  578. iwl_mvm_update_mcc(struct iwl_mvm *mvm, const char *alpha2,
  579. enum iwl_mcc_source src_id)
  580. {
  581. struct iwl_mcc_update_cmd mcc_update_cmd = {
  582. .mcc = cpu_to_le16(alpha2[0] << 8 | alpha2[1]),
  583. .source_id = (u8)src_id,
  584. };
  585. struct iwl_mcc_update_resp *resp_cp;
  586. struct iwl_rx_packet *pkt;
  587. struct iwl_host_cmd cmd = {
  588. .id = MCC_UPDATE_CMD,
  589. .flags = CMD_WANT_SKB,
  590. .data = { &mcc_update_cmd },
  591. };
  592. int ret;
  593. u32 status;
  594. int resp_len, n_channels;
  595. u16 mcc;
  596. bool resp_v2 = fw_has_capa(&mvm->fw->ucode_capa,
  597. IWL_UCODE_TLV_CAPA_LAR_SUPPORT_V2);
  598. if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
  599. return ERR_PTR(-EOPNOTSUPP);
  600. cmd.len[0] = sizeof(struct iwl_mcc_update_cmd);
  601. if (!resp_v2)
  602. cmd.len[0] = sizeof(struct iwl_mcc_update_cmd_v1);
  603. IWL_DEBUG_LAR(mvm, "send MCC update to FW with '%c%c' src = %d\n",
  604. alpha2[0], alpha2[1], src_id);
  605. ret = iwl_mvm_send_cmd(mvm, &cmd);
  606. if (ret)
  607. return ERR_PTR(ret);
  608. pkt = cmd.resp_pkt;
  609. /* Extract MCC response */
  610. if (resp_v2) {
  611. struct iwl_mcc_update_resp *mcc_resp = (void *)pkt->data;
  612. n_channels = __le32_to_cpu(mcc_resp->n_channels);
  613. resp_len = sizeof(struct iwl_mcc_update_resp) +
  614. n_channels * sizeof(__le32);
  615. resp_cp = kmemdup(mcc_resp, resp_len, GFP_KERNEL);
  616. if (!resp_cp) {
  617. resp_cp = ERR_PTR(-ENOMEM);
  618. goto exit;
  619. }
  620. } else {
  621. struct iwl_mcc_update_resp_v1 *mcc_resp_v1 = (void *)pkt->data;
  622. n_channels = __le32_to_cpu(mcc_resp_v1->n_channels);
  623. resp_len = sizeof(struct iwl_mcc_update_resp) +
  624. n_channels * sizeof(__le32);
  625. resp_cp = kzalloc(resp_len, GFP_KERNEL);
  626. if (!resp_cp) {
  627. resp_cp = ERR_PTR(-ENOMEM);
  628. goto exit;
  629. }
  630. resp_cp->status = mcc_resp_v1->status;
  631. resp_cp->mcc = mcc_resp_v1->mcc;
  632. resp_cp->cap = mcc_resp_v1->cap;
  633. resp_cp->source_id = mcc_resp_v1->source_id;
  634. resp_cp->n_channels = mcc_resp_v1->n_channels;
  635. memcpy(resp_cp->channels, mcc_resp_v1->channels,
  636. n_channels * sizeof(__le32));
  637. }
  638. status = le32_to_cpu(resp_cp->status);
  639. mcc = le16_to_cpu(resp_cp->mcc);
  640. /* W/A for a FW/NVM issue - returns 0x00 for the world domain */
  641. if (mcc == 0) {
  642. mcc = 0x3030; /* "00" - world */
  643. resp_cp->mcc = cpu_to_le16(mcc);
  644. }
  645. IWL_DEBUG_LAR(mvm,
  646. "MCC response status: 0x%x. new MCC: 0x%x ('%c%c') change: %d n_chans: %d\n",
  647. status, mcc, mcc >> 8, mcc & 0xff,
  648. !!(status == MCC_RESP_NEW_CHAN_PROFILE), n_channels);
  649. exit:
  650. iwl_free_resp(&cmd);
  651. return resp_cp;
  652. }
  653. int iwl_mvm_init_mcc(struct iwl_mvm *mvm)
  654. {
  655. bool tlv_lar;
  656. bool nvm_lar;
  657. int retval;
  658. struct ieee80211_regdomain *regd;
  659. char mcc[3];
  660. if (mvm->cfg->nvm_type == IWL_NVM_EXT) {
  661. tlv_lar = fw_has_capa(&mvm->fw->ucode_capa,
  662. IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
  663. nvm_lar = mvm->nvm_data->lar_enabled;
  664. if (tlv_lar != nvm_lar)
  665. IWL_INFO(mvm,
  666. "Conflict between TLV & NVM regarding enabling LAR (TLV = %s NVM =%s)\n",
  667. tlv_lar ? "enabled" : "disabled",
  668. nvm_lar ? "enabled" : "disabled");
  669. }
  670. if (!iwl_mvm_is_lar_supported(mvm))
  671. return 0;
  672. /*
  673. * try to replay the last set MCC to FW. If it doesn't exist,
  674. * queue an update to cfg80211 to retrieve the default alpha2 from FW.
  675. */
  676. retval = iwl_mvm_init_fw_regd(mvm);
  677. if (retval != -ENOENT)
  678. return retval;
  679. /*
  680. * Driver regulatory hint for initial update, this also informs the
  681. * firmware we support wifi location updates.
  682. * Disallow scans that might crash the FW while the LAR regdomain
  683. * is not set.
  684. */
  685. mvm->lar_regdom_set = false;
  686. regd = iwl_mvm_get_current_regdomain(mvm, NULL);
  687. if (IS_ERR_OR_NULL(regd))
  688. return -EIO;
  689. if (iwl_mvm_is_wifi_mcc_supported(mvm) &&
  690. !iwl_acpi_get_mcc(mvm->dev, mcc)) {
  691. kfree(regd);
  692. regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc,
  693. MCC_SOURCE_BIOS, NULL);
  694. if (IS_ERR_OR_NULL(regd))
  695. return -EIO;
  696. }
  697. retval = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd);
  698. kfree(regd);
  699. return retval;
  700. }
  701. void iwl_mvm_rx_chub_update_mcc(struct iwl_mvm *mvm,
  702. struct iwl_rx_cmd_buffer *rxb)
  703. {
  704. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  705. struct iwl_mcc_chub_notif *notif = (void *)pkt->data;
  706. enum iwl_mcc_source src;
  707. char mcc[3];
  708. struct ieee80211_regdomain *regd;
  709. lockdep_assert_held(&mvm->mutex);
  710. if (iwl_mvm_is_vif_assoc(mvm) && notif->source_id == MCC_SOURCE_WIFI) {
  711. IWL_DEBUG_LAR(mvm, "Ignore mcc update while associated\n");
  712. return;
  713. }
  714. if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
  715. return;
  716. mcc[0] = le16_to_cpu(notif->mcc) >> 8;
  717. mcc[1] = le16_to_cpu(notif->mcc) & 0xff;
  718. mcc[2] = '\0';
  719. src = notif->source_id;
  720. IWL_DEBUG_LAR(mvm,
  721. "RX: received chub update mcc cmd (mcc '%s' src %d)\n",
  722. mcc, src);
  723. regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc, src, NULL);
  724. if (IS_ERR_OR_NULL(regd))
  725. return;
  726. regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
  727. kfree(regd);
  728. }