nvm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. int regulatory_type;
  267. /* Checking for required sections */
  268. if (mvm->trans->cfg->nvm_type != IWL_NVM_EXT) {
  269. if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
  270. !mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data) {
  271. IWL_ERR(mvm, "Can't parse empty OTP/NVM sections\n");
  272. return NULL;
  273. }
  274. } else {
  275. if (mvm->trans->cfg->nvm_type == IWL_NVM_SDP)
  276. regulatory_type = NVM_SECTION_TYPE_REGULATORY_SDP;
  277. else
  278. regulatory_type = NVM_SECTION_TYPE_REGULATORY;
  279. /* SW and REGULATORY sections are mandatory */
  280. if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
  281. !mvm->nvm_sections[regulatory_type].data) {
  282. IWL_ERR(mvm,
  283. "Can't parse empty family 8000 OTP/NVM sections\n");
  284. return NULL;
  285. }
  286. /* MAC_OVERRIDE or at least HW section must exist */
  287. if (!mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data &&
  288. !mvm->nvm_sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data) {
  289. IWL_ERR(mvm,
  290. "Can't parse mac_address, empty sections\n");
  291. return NULL;
  292. }
  293. /* PHY_SKU section is mandatory in B0 */
  294. if (!mvm->nvm_sections[NVM_SECTION_TYPE_PHY_SKU].data) {
  295. IWL_ERR(mvm,
  296. "Can't parse phy_sku in B0, empty sections\n");
  297. return NULL;
  298. }
  299. }
  300. hw = (const __be16 *)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. mac_override =
  304. (const __le16 *)sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data;
  305. phy_sku = (const __le16 *)sections[NVM_SECTION_TYPE_PHY_SKU].data;
  306. regulatory = mvm->trans->cfg->nvm_type == IWL_NVM_SDP ?
  307. (const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY_SDP].data :
  308. (const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY].data;
  309. lar_enabled = !iwlwifi_mod_params.lar_disable &&
  310. fw_has_capa(&mvm->fw->ucode_capa,
  311. IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
  312. return iwl_parse_nvm_data(mvm->trans, mvm->cfg, hw, sw, calib,
  313. regulatory, mac_override, phy_sku,
  314. mvm->fw->valid_tx_ant, mvm->fw->valid_rx_ant,
  315. lar_enabled);
  316. }
  317. #define MAX_NVM_FILE_LEN 16384
  318. /*
  319. * Reads external NVM from a file into mvm->nvm_sections
  320. *
  321. * HOW TO CREATE THE NVM FILE FORMAT:
  322. * ------------------------------
  323. * 1. create hex file, format:
  324. * 3800 -> header
  325. * 0000 -> header
  326. * 5a40 -> data
  327. *
  328. * rev - 6 bit (word1)
  329. * len - 10 bit (word1)
  330. * id - 4 bit (word2)
  331. * rsv - 12 bit (word2)
  332. *
  333. * 2. flip 8bits with 8 bits per line to get the right NVM file format
  334. *
  335. * 3. create binary file from the hex file
  336. *
  337. * 4. save as "iNVM_xxx.bin" under /lib/firmware
  338. */
  339. int iwl_mvm_read_external_nvm(struct iwl_mvm *mvm)
  340. {
  341. int ret, section_size;
  342. u16 section_id;
  343. const struct firmware *fw_entry;
  344. const struct {
  345. __le16 word1;
  346. __le16 word2;
  347. u8 data[];
  348. } *file_sec;
  349. const u8 *eof;
  350. u8 *temp;
  351. int max_section_size;
  352. const __le32 *dword_buff;
  353. #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
  354. #define NVM_WORD2_ID(x) (x >> 12)
  355. #define EXT_NVM_WORD2_LEN(x) (2 * (((x) & 0xFF) << 8 | (x) >> 8))
  356. #define EXT_NVM_WORD1_ID(x) ((x) >> 4)
  357. #define NVM_HEADER_0 (0x2A504C54)
  358. #define NVM_HEADER_1 (0x4E564D2A)
  359. #define NVM_HEADER_SIZE (4 * sizeof(u32))
  360. IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from external NVM\n");
  361. /* Maximal size depends on NVM version */
  362. if (mvm->trans->cfg->nvm_type != IWL_NVM_EXT)
  363. max_section_size = IWL_MAX_NVM_SECTION_SIZE;
  364. else
  365. max_section_size = IWL_MAX_EXT_NVM_SECTION_SIZE;
  366. /*
  367. * Obtain NVM image via request_firmware. Since we already used
  368. * request_firmware_nowait() for the firmware binary load and only
  369. * get here after that we assume the NVM request can be satisfied
  370. * synchronously.
  371. */
  372. ret = request_firmware(&fw_entry, mvm->nvm_file_name,
  373. mvm->trans->dev);
  374. if (ret) {
  375. IWL_ERR(mvm, "ERROR: %s isn't available %d\n",
  376. mvm->nvm_file_name, ret);
  377. return ret;
  378. }
  379. IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n",
  380. mvm->nvm_file_name, fw_entry->size);
  381. if (fw_entry->size > MAX_NVM_FILE_LEN) {
  382. IWL_ERR(mvm, "NVM file too large\n");
  383. ret = -EINVAL;
  384. goto out;
  385. }
  386. eof = fw_entry->data + fw_entry->size;
  387. dword_buff = (__le32 *)fw_entry->data;
  388. /* some NVM file will contain a header.
  389. * The header is identified by 2 dwords header as follow:
  390. * dword[0] = 0x2A504C54
  391. * dword[1] = 0x4E564D2A
  392. *
  393. * This header must be skipped when providing the NVM data to the FW.
  394. */
  395. if (fw_entry->size > NVM_HEADER_SIZE &&
  396. dword_buff[0] == cpu_to_le32(NVM_HEADER_0) &&
  397. dword_buff[1] == cpu_to_le32(NVM_HEADER_1)) {
  398. file_sec = (void *)(fw_entry->data + NVM_HEADER_SIZE);
  399. IWL_INFO(mvm, "NVM Version %08X\n", le32_to_cpu(dword_buff[2]));
  400. IWL_INFO(mvm, "NVM Manufacturing date %08X\n",
  401. le32_to_cpu(dword_buff[3]));
  402. /* nvm file validation, dword_buff[2] holds the file version */
  403. if (mvm->trans->cfg->device_family == IWL_DEVICE_FAMILY_8000 &&
  404. CSR_HW_REV_STEP(mvm->trans->hw_rev) == SILICON_C_STEP &&
  405. le32_to_cpu(dword_buff[2]) < 0xE4A) {
  406. ret = -EFAULT;
  407. goto out;
  408. }
  409. } else {
  410. file_sec = (void *)fw_entry->data;
  411. }
  412. while (true) {
  413. if (file_sec->data > eof) {
  414. IWL_ERR(mvm,
  415. "ERROR - NVM file too short for section header\n");
  416. ret = -EINVAL;
  417. break;
  418. }
  419. /* check for EOF marker */
  420. if (!file_sec->word1 && !file_sec->word2) {
  421. ret = 0;
  422. break;
  423. }
  424. if (mvm->trans->cfg->nvm_type != IWL_NVM_EXT) {
  425. section_size =
  426. 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1));
  427. section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2));
  428. } else {
  429. section_size = 2 * EXT_NVM_WORD2_LEN(
  430. le16_to_cpu(file_sec->word2));
  431. section_id = EXT_NVM_WORD1_ID(
  432. le16_to_cpu(file_sec->word1));
  433. }
  434. if (section_size > max_section_size) {
  435. IWL_ERR(mvm, "ERROR - section too large (%d)\n",
  436. section_size);
  437. ret = -EINVAL;
  438. break;
  439. }
  440. if (!section_size) {
  441. IWL_ERR(mvm, "ERROR - section empty\n");
  442. ret = -EINVAL;
  443. break;
  444. }
  445. if (file_sec->data + section_size > eof) {
  446. IWL_ERR(mvm,
  447. "ERROR - NVM file too short for section (%d bytes)\n",
  448. section_size);
  449. ret = -EINVAL;
  450. break;
  451. }
  452. if (WARN(section_id >= NVM_MAX_NUM_SECTIONS,
  453. "Invalid NVM section ID %d\n", section_id)) {
  454. ret = -EINVAL;
  455. break;
  456. }
  457. temp = kmemdup(file_sec->data, section_size, GFP_KERNEL);
  458. if (!temp) {
  459. ret = -ENOMEM;
  460. break;
  461. }
  462. iwl_mvm_nvm_fixups(mvm, section_id, temp, section_size);
  463. kfree(mvm->nvm_sections[section_id].data);
  464. mvm->nvm_sections[section_id].data = temp;
  465. mvm->nvm_sections[section_id].length = section_size;
  466. /* advance to the next section */
  467. file_sec = (void *)(file_sec->data + section_size);
  468. }
  469. out:
  470. release_firmware(fw_entry);
  471. return ret;
  472. }
  473. /* Loads the NVM data stored in mvm->nvm_sections into the NIC */
  474. int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm)
  475. {
  476. int i, ret = 0;
  477. struct iwl_nvm_section *sections = mvm->nvm_sections;
  478. IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n");
  479. for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) {
  480. if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length)
  481. continue;
  482. ret = iwl_nvm_write_section(mvm, i, sections[i].data,
  483. sections[i].length);
  484. if (ret < 0) {
  485. IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);
  486. break;
  487. }
  488. }
  489. return ret;
  490. }
  491. int iwl_nvm_init(struct iwl_mvm *mvm)
  492. {
  493. int ret, section;
  494. u32 size_read = 0;
  495. u8 *nvm_buffer, *temp;
  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. /* Read From FW NVM */
  501. IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");
  502. nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size,
  503. GFP_KERNEL);
  504. if (!nvm_buffer)
  505. return -ENOMEM;
  506. for (section = 0; section < NVM_MAX_NUM_SECTIONS; section++) {
  507. /* we override the constness for initial read */
  508. ret = iwl_nvm_read_section(mvm, section, nvm_buffer,
  509. size_read);
  510. if (ret < 0)
  511. continue;
  512. size_read += ret;
  513. temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
  514. if (!temp) {
  515. ret = -ENOMEM;
  516. break;
  517. }
  518. iwl_mvm_nvm_fixups(mvm, section, temp, ret);
  519. mvm->nvm_sections[section].data = temp;
  520. mvm->nvm_sections[section].length = ret;
  521. #ifdef CONFIG_IWLWIFI_DEBUGFS
  522. switch (section) {
  523. case NVM_SECTION_TYPE_SW:
  524. mvm->nvm_sw_blob.data = temp;
  525. mvm->nvm_sw_blob.size = ret;
  526. break;
  527. case NVM_SECTION_TYPE_CALIBRATION:
  528. mvm->nvm_calib_blob.data = temp;
  529. mvm->nvm_calib_blob.size = ret;
  530. break;
  531. case NVM_SECTION_TYPE_PRODUCTION:
  532. mvm->nvm_prod_blob.data = temp;
  533. mvm->nvm_prod_blob.size = ret;
  534. break;
  535. case NVM_SECTION_TYPE_PHY_SKU:
  536. mvm->nvm_phy_sku_blob.data = temp;
  537. mvm->nvm_phy_sku_blob.size = ret;
  538. break;
  539. default:
  540. if (section == mvm->cfg->nvm_hw_section_num) {
  541. mvm->nvm_hw_blob.data = temp;
  542. mvm->nvm_hw_blob.size = ret;
  543. break;
  544. }
  545. }
  546. #endif
  547. }
  548. if (!size_read)
  549. IWL_ERR(mvm, "OTP is blank\n");
  550. kfree(nvm_buffer);
  551. /* Only if PNVM selected in the mod param - load external NVM */
  552. if (mvm->nvm_file_name) {
  553. /* read External NVM file from the mod param */
  554. ret = iwl_mvm_read_external_nvm(mvm);
  555. if (ret) {
  556. mvm->nvm_file_name = nvm_file_C;
  557. if ((ret == -EFAULT || ret == -ENOENT) &&
  558. mvm->nvm_file_name) {
  559. /* in case nvm file was failed try again */
  560. ret = iwl_mvm_read_external_nvm(mvm);
  561. if (ret)
  562. return ret;
  563. } else {
  564. return ret;
  565. }
  566. }
  567. }
  568. /* parse the relevant nvm sections */
  569. mvm->nvm_data = iwl_parse_nvm_sections(mvm);
  570. if (!mvm->nvm_data)
  571. return -ENODATA;
  572. IWL_DEBUG_EEPROM(mvm->trans->dev, "nvm version = %x\n",
  573. mvm->nvm_data->nvm_version);
  574. return 0;
  575. }
  576. struct iwl_mcc_update_resp *
  577. iwl_mvm_update_mcc(struct iwl_mvm *mvm, const char *alpha2,
  578. enum iwl_mcc_source src_id)
  579. {
  580. struct iwl_mcc_update_cmd mcc_update_cmd = {
  581. .mcc = cpu_to_le16(alpha2[0] << 8 | alpha2[1]),
  582. .source_id = (u8)src_id,
  583. };
  584. struct iwl_mcc_update_resp *resp_cp;
  585. struct iwl_rx_packet *pkt;
  586. struct iwl_host_cmd cmd = {
  587. .id = MCC_UPDATE_CMD,
  588. .flags = CMD_WANT_SKB,
  589. .data = { &mcc_update_cmd },
  590. };
  591. int ret;
  592. u32 status;
  593. int resp_len, n_channels;
  594. u16 mcc;
  595. bool resp_v2 = fw_has_capa(&mvm->fw->ucode_capa,
  596. IWL_UCODE_TLV_CAPA_LAR_SUPPORT_V2);
  597. if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
  598. return ERR_PTR(-EOPNOTSUPP);
  599. cmd.len[0] = sizeof(struct iwl_mcc_update_cmd);
  600. if (!resp_v2)
  601. cmd.len[0] = sizeof(struct iwl_mcc_update_cmd_v1);
  602. IWL_DEBUG_LAR(mvm, "send MCC update to FW with '%c%c' src = %d\n",
  603. alpha2[0], alpha2[1], src_id);
  604. ret = iwl_mvm_send_cmd(mvm, &cmd);
  605. if (ret)
  606. return ERR_PTR(ret);
  607. pkt = cmd.resp_pkt;
  608. /* Extract MCC response */
  609. if (resp_v2) {
  610. struct iwl_mcc_update_resp *mcc_resp = (void *)pkt->data;
  611. n_channels = __le32_to_cpu(mcc_resp->n_channels);
  612. resp_len = sizeof(struct iwl_mcc_update_resp) +
  613. n_channels * sizeof(__le32);
  614. resp_cp = kmemdup(mcc_resp, resp_len, GFP_KERNEL);
  615. if (!resp_cp) {
  616. resp_cp = ERR_PTR(-ENOMEM);
  617. goto exit;
  618. }
  619. } else {
  620. struct iwl_mcc_update_resp_v1 *mcc_resp_v1 = (void *)pkt->data;
  621. n_channels = __le32_to_cpu(mcc_resp_v1->n_channels);
  622. resp_len = sizeof(struct iwl_mcc_update_resp) +
  623. n_channels * sizeof(__le32);
  624. resp_cp = kzalloc(resp_len, GFP_KERNEL);
  625. if (!resp_cp) {
  626. resp_cp = ERR_PTR(-ENOMEM);
  627. goto exit;
  628. }
  629. resp_cp->status = mcc_resp_v1->status;
  630. resp_cp->mcc = mcc_resp_v1->mcc;
  631. resp_cp->cap = mcc_resp_v1->cap;
  632. resp_cp->source_id = mcc_resp_v1->source_id;
  633. resp_cp->n_channels = mcc_resp_v1->n_channels;
  634. memcpy(resp_cp->channels, mcc_resp_v1->channels,
  635. n_channels * sizeof(__le32));
  636. }
  637. status = le32_to_cpu(resp_cp->status);
  638. mcc = le16_to_cpu(resp_cp->mcc);
  639. /* W/A for a FW/NVM issue - returns 0x00 for the world domain */
  640. if (mcc == 0) {
  641. mcc = 0x3030; /* "00" - world */
  642. resp_cp->mcc = cpu_to_le16(mcc);
  643. }
  644. IWL_DEBUG_LAR(mvm,
  645. "MCC response status: 0x%x. new MCC: 0x%x ('%c%c') change: %d n_chans: %d\n",
  646. status, mcc, mcc >> 8, mcc & 0xff,
  647. !!(status == MCC_RESP_NEW_CHAN_PROFILE), n_channels);
  648. exit:
  649. iwl_free_resp(&cmd);
  650. return resp_cp;
  651. }
  652. int iwl_mvm_init_mcc(struct iwl_mvm *mvm)
  653. {
  654. bool tlv_lar;
  655. bool nvm_lar;
  656. int retval;
  657. struct ieee80211_regdomain *regd;
  658. char mcc[3];
  659. if (mvm->cfg->nvm_type == IWL_NVM_EXT) {
  660. tlv_lar = fw_has_capa(&mvm->fw->ucode_capa,
  661. IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
  662. nvm_lar = mvm->nvm_data->lar_enabled;
  663. if (tlv_lar != nvm_lar)
  664. IWL_INFO(mvm,
  665. "Conflict between TLV & NVM regarding enabling LAR (TLV = %s NVM =%s)\n",
  666. tlv_lar ? "enabled" : "disabled",
  667. nvm_lar ? "enabled" : "disabled");
  668. }
  669. if (!iwl_mvm_is_lar_supported(mvm))
  670. return 0;
  671. /*
  672. * try to replay the last set MCC to FW. If it doesn't exist,
  673. * queue an update to cfg80211 to retrieve the default alpha2 from FW.
  674. */
  675. retval = iwl_mvm_init_fw_regd(mvm);
  676. if (retval != -ENOENT)
  677. return retval;
  678. /*
  679. * Driver regulatory hint for initial update, this also informs the
  680. * firmware we support wifi location updates.
  681. * Disallow scans that might crash the FW while the LAR regdomain
  682. * is not set.
  683. */
  684. mvm->lar_regdom_set = false;
  685. regd = iwl_mvm_get_current_regdomain(mvm, NULL);
  686. if (IS_ERR_OR_NULL(regd))
  687. return -EIO;
  688. if (iwl_mvm_is_wifi_mcc_supported(mvm) &&
  689. !iwl_get_bios_mcc(mvm->dev, mcc)) {
  690. kfree(regd);
  691. regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc,
  692. MCC_SOURCE_BIOS, NULL);
  693. if (IS_ERR_OR_NULL(regd))
  694. return -EIO;
  695. }
  696. retval = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd);
  697. kfree(regd);
  698. return retval;
  699. }
  700. void iwl_mvm_rx_chub_update_mcc(struct iwl_mvm *mvm,
  701. struct iwl_rx_cmd_buffer *rxb)
  702. {
  703. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  704. struct iwl_mcc_chub_notif *notif = (void *)pkt->data;
  705. enum iwl_mcc_source src;
  706. char mcc[3];
  707. struct ieee80211_regdomain *regd;
  708. lockdep_assert_held(&mvm->mutex);
  709. if (iwl_mvm_is_vif_assoc(mvm) && notif->source_id == MCC_SOURCE_WIFI) {
  710. IWL_DEBUG_LAR(mvm, "Ignore mcc update while associated\n");
  711. return;
  712. }
  713. if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
  714. return;
  715. mcc[0] = le16_to_cpu(notif->mcc) >> 8;
  716. mcc[1] = le16_to_cpu(notif->mcc) & 0xff;
  717. mcc[2] = '\0';
  718. src = notif->source_id;
  719. IWL_DEBUG_LAR(mvm,
  720. "RX: received chub update mcc cmd (mcc '%s' src %d)\n",
  721. mcc, src);
  722. regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc, src, NULL);
  723. if (IS_ERR_OR_NULL(regd))
  724. return;
  725. regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
  726. kfree(regd);
  727. }