nvm.c 23 KB

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