nvm.c 23 KB

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