nvm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <linuxwifi@intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include <linux/firmware.h>
  66. #include <linux/rtnetlink.h>
  67. #include <linux/pci.h>
  68. #include <linux/acpi.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. u32 mac_addr0, mac_addr1;
  270. /* Checking for required sections */
  271. if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
  272. if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
  273. !mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data) {
  274. IWL_ERR(mvm, "Can't parse empty OTP/NVM sections\n");
  275. return NULL;
  276. }
  277. } else {
  278. /* SW and REGULATORY sections are mandatory */
  279. if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
  280. !mvm->nvm_sections[NVM_SECTION_TYPE_REGULATORY].data) {
  281. IWL_ERR(mvm,
  282. "Can't parse empty family 8000 OTP/NVM sections\n");
  283. return NULL;
  284. }
  285. /* MAC_OVERRIDE or at least HW section must exist */
  286. if (!mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data &&
  287. !mvm->nvm_sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data) {
  288. IWL_ERR(mvm,
  289. "Can't parse mac_address, empty sections\n");
  290. return NULL;
  291. }
  292. /* PHY_SKU section is mandatory in B0 */
  293. if (!mvm->nvm_sections[NVM_SECTION_TYPE_PHY_SKU].data) {
  294. IWL_ERR(mvm,
  295. "Can't parse phy_sku in B0, empty sections\n");
  296. return NULL;
  297. }
  298. }
  299. if (WARN_ON(!mvm->cfg))
  300. return NULL;
  301. /* read the mac address from WFMP registers */
  302. mac_addr0 = iwl_trans_read_prph(mvm->trans, WFMP_MAC_ADDR_0);
  303. mac_addr1 = iwl_trans_read_prph(mvm->trans, WFMP_MAC_ADDR_1);
  304. hw = (const __le16 *)sections[mvm->cfg->nvm_hw_section_num].data;
  305. sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data;
  306. calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data;
  307. regulatory = (const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY].data;
  308. mac_override =
  309. (const __le16 *)sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data;
  310. phy_sku = (const __le16 *)sections[NVM_SECTION_TYPE_PHY_SKU].data;
  311. lar_enabled = !iwlwifi_mod_params.lar_disable &&
  312. fw_has_capa(&mvm->fw->ucode_capa,
  313. IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
  314. return iwl_parse_nvm_data(mvm->trans->dev, mvm->cfg, hw, sw, calib,
  315. regulatory, mac_override, phy_sku,
  316. mvm->fw->valid_tx_ant, mvm->fw->valid_rx_ant,
  317. lar_enabled, mac_addr0, mac_addr1);
  318. }
  319. #define MAX_NVM_FILE_LEN 16384
  320. /*
  321. * Reads external NVM from a file into mvm->nvm_sections
  322. *
  323. * HOW TO CREATE THE NVM FILE FORMAT:
  324. * ------------------------------
  325. * 1. create hex file, format:
  326. * 3800 -> header
  327. * 0000 -> header
  328. * 5a40 -> data
  329. *
  330. * rev - 6 bit (word1)
  331. * len - 10 bit (word1)
  332. * id - 4 bit (word2)
  333. * rsv - 12 bit (word2)
  334. *
  335. * 2. flip 8bits with 8 bits per line to get the right NVM file format
  336. *
  337. * 3. create binary file from the hex file
  338. *
  339. * 4. save as "iNVM_xxx.bin" under /lib/firmware
  340. */
  341. static int iwl_mvm_read_external_nvm(struct iwl_mvm *mvm)
  342. {
  343. int ret, section_size;
  344. u16 section_id;
  345. const struct firmware *fw_entry;
  346. const struct {
  347. __le16 word1;
  348. __le16 word2;
  349. u8 data[];
  350. } *file_sec;
  351. const u8 *eof;
  352. u8 *temp;
  353. int max_section_size;
  354. const __le32 *dword_buff;
  355. #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
  356. #define NVM_WORD2_ID(x) (x >> 12)
  357. #define NVM_WORD2_LEN_FAMILY_8000(x) (2 * ((x & 0xFF) << 8 | x >> 8))
  358. #define NVM_WORD1_ID_FAMILY_8000(x) (x >> 4)
  359. #define NVM_HEADER_0 (0x2A504C54)
  360. #define NVM_HEADER_1 (0x4E564D2A)
  361. #define NVM_HEADER_SIZE (4 * sizeof(u32))
  362. IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from external NVM\n");
  363. /* Maximal size depends on HW family and step */
  364. if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
  365. max_section_size = IWL_MAX_NVM_SECTION_SIZE;
  366. else
  367. max_section_size = IWL_MAX_NVM_8000_SECTION_SIZE;
  368. /*
  369. * Obtain NVM image via request_firmware. Since we already used
  370. * request_firmware_nowait() for the firmware binary load and only
  371. * get here after that we assume the NVM request can be satisfied
  372. * synchronously.
  373. */
  374. ret = request_firmware(&fw_entry, mvm->nvm_file_name,
  375. mvm->trans->dev);
  376. if (ret) {
  377. IWL_ERR(mvm, "ERROR: %s isn't available %d\n",
  378. mvm->nvm_file_name, ret);
  379. return ret;
  380. }
  381. IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n",
  382. mvm->nvm_file_name, fw_entry->size);
  383. if (fw_entry->size > MAX_NVM_FILE_LEN) {
  384. IWL_ERR(mvm, "NVM file too large\n");
  385. ret = -EINVAL;
  386. goto out;
  387. }
  388. eof = fw_entry->data + fw_entry->size;
  389. dword_buff = (__le32 *)fw_entry->data;
  390. /* some NVM file will contain a header.
  391. * The header is identified by 2 dwords header as follow:
  392. * dword[0] = 0x2A504C54
  393. * dword[1] = 0x4E564D2A
  394. *
  395. * This header must be skipped when providing the NVM data to the FW.
  396. */
  397. if (fw_entry->size > NVM_HEADER_SIZE &&
  398. dword_buff[0] == cpu_to_le32(NVM_HEADER_0) &&
  399. dword_buff[1] == cpu_to_le32(NVM_HEADER_1)) {
  400. file_sec = (void *)(fw_entry->data + NVM_HEADER_SIZE);
  401. IWL_INFO(mvm, "NVM Version %08X\n", le32_to_cpu(dword_buff[2]));
  402. IWL_INFO(mvm, "NVM Manufacturing date %08X\n",
  403. le32_to_cpu(dword_buff[3]));
  404. /* nvm file validation, dword_buff[2] holds the file version */
  405. if ((CSR_HW_REV_STEP(mvm->trans->hw_rev) == SILICON_C_STEP &&
  406. le32_to_cpu(dword_buff[2]) < 0xE4A) ||
  407. (CSR_HW_REV_STEP(mvm->trans->hw_rev) == SILICON_B_STEP &&
  408. le32_to_cpu(dword_buff[2]) >= 0xE4A)) {
  409. ret = -EFAULT;
  410. goto out;
  411. }
  412. } else {
  413. file_sec = (void *)fw_entry->data;
  414. }
  415. while (true) {
  416. if (file_sec->data > eof) {
  417. IWL_ERR(mvm,
  418. "ERROR - NVM file too short for section header\n");
  419. ret = -EINVAL;
  420. break;
  421. }
  422. /* check for EOF marker */
  423. if (!file_sec->word1 && !file_sec->word2) {
  424. ret = 0;
  425. break;
  426. }
  427. if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
  428. section_size =
  429. 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1));
  430. section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2));
  431. } else {
  432. section_size = 2 * NVM_WORD2_LEN_FAMILY_8000(
  433. le16_to_cpu(file_sec->word2));
  434. section_id = NVM_WORD1_ID_FAMILY_8000(
  435. le16_to_cpu(file_sec->word1));
  436. }
  437. if (section_size > max_section_size) {
  438. IWL_ERR(mvm, "ERROR - section too large (%d)\n",
  439. section_size);
  440. ret = -EINVAL;
  441. break;
  442. }
  443. if (!section_size) {
  444. IWL_ERR(mvm, "ERROR - section empty\n");
  445. ret = -EINVAL;
  446. break;
  447. }
  448. if (file_sec->data + section_size > eof) {
  449. IWL_ERR(mvm,
  450. "ERROR - NVM file too short for section (%d bytes)\n",
  451. section_size);
  452. ret = -EINVAL;
  453. break;
  454. }
  455. if (WARN(section_id >= NVM_MAX_NUM_SECTIONS,
  456. "Invalid NVM section ID %d\n", section_id)) {
  457. ret = -EINVAL;
  458. break;
  459. }
  460. temp = kmemdup(file_sec->data, section_size, GFP_KERNEL);
  461. if (!temp) {
  462. ret = -ENOMEM;
  463. break;
  464. }
  465. iwl_mvm_nvm_fixups(mvm, section_id, temp, section_size);
  466. kfree(mvm->nvm_sections[section_id].data);
  467. mvm->nvm_sections[section_id].data = temp;
  468. mvm->nvm_sections[section_id].length = section_size;
  469. /* advance to the next section */
  470. file_sec = (void *)(file_sec->data + section_size);
  471. }
  472. out:
  473. release_firmware(fw_entry);
  474. return ret;
  475. }
  476. /* Loads the NVM data stored in mvm->nvm_sections into the NIC */
  477. int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm)
  478. {
  479. int i, ret = 0;
  480. struct iwl_nvm_section *sections = mvm->nvm_sections;
  481. IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n");
  482. for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) {
  483. if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length)
  484. continue;
  485. ret = iwl_nvm_write_section(mvm, i, sections[i].data,
  486. sections[i].length);
  487. if (ret < 0) {
  488. IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);
  489. break;
  490. }
  491. }
  492. return ret;
  493. }
  494. int iwl_nvm_init(struct iwl_mvm *mvm, bool read_nvm_from_nic)
  495. {
  496. int ret, section;
  497. u32 size_read = 0;
  498. u8 *nvm_buffer, *temp;
  499. const char *nvm_file_B = mvm->cfg->default_nvm_file_B_step;
  500. const char *nvm_file_C = mvm->cfg->default_nvm_file_C_step;
  501. if (WARN_ON_ONCE(mvm->cfg->nvm_hw_section_num >= NVM_MAX_NUM_SECTIONS))
  502. return -EINVAL;
  503. /* load NVM values from nic */
  504. if (read_nvm_from_nic) {
  505. /* Read From FW NVM */
  506. IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");
  507. nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size,
  508. GFP_KERNEL);
  509. if (!nvm_buffer)
  510. return -ENOMEM;
  511. for (section = 0; section < NVM_MAX_NUM_SECTIONS; section++) {
  512. /* we override the constness for initial read */
  513. ret = iwl_nvm_read_section(mvm, section, nvm_buffer,
  514. size_read);
  515. if (ret < 0)
  516. continue;
  517. size_read += ret;
  518. temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
  519. if (!temp) {
  520. ret = -ENOMEM;
  521. break;
  522. }
  523. iwl_mvm_nvm_fixups(mvm, section, temp, ret);
  524. mvm->nvm_sections[section].data = temp;
  525. mvm->nvm_sections[section].length = ret;
  526. #ifdef CONFIG_IWLWIFI_DEBUGFS
  527. switch (section) {
  528. case NVM_SECTION_TYPE_SW:
  529. mvm->nvm_sw_blob.data = temp;
  530. mvm->nvm_sw_blob.size = ret;
  531. break;
  532. case NVM_SECTION_TYPE_CALIBRATION:
  533. mvm->nvm_calib_blob.data = temp;
  534. mvm->nvm_calib_blob.size = ret;
  535. break;
  536. case NVM_SECTION_TYPE_PRODUCTION:
  537. mvm->nvm_prod_blob.data = temp;
  538. mvm->nvm_prod_blob.size = ret;
  539. break;
  540. case NVM_SECTION_TYPE_PHY_SKU:
  541. mvm->nvm_phy_sku_blob.data = temp;
  542. mvm->nvm_phy_sku_blob.size = ret;
  543. break;
  544. default:
  545. if (section == mvm->cfg->nvm_hw_section_num) {
  546. mvm->nvm_hw_blob.data = temp;
  547. mvm->nvm_hw_blob.size = ret;
  548. break;
  549. }
  550. }
  551. #endif
  552. }
  553. if (!size_read)
  554. IWL_ERR(mvm, "OTP is blank\n");
  555. kfree(nvm_buffer);
  556. }
  557. /* Only if PNVM selected in the mod param - load external NVM */
  558. if (mvm->nvm_file_name) {
  559. /* read External NVM file from the mod param */
  560. ret = iwl_mvm_read_external_nvm(mvm);
  561. if (ret) {
  562. /* choose the nvm_file name according to the
  563. * HW step
  564. */
  565. if (CSR_HW_REV_STEP(mvm->trans->hw_rev) ==
  566. SILICON_B_STEP)
  567. mvm->nvm_file_name = nvm_file_B;
  568. else
  569. mvm->nvm_file_name = nvm_file_C;
  570. if (ret == -EFAULT && mvm->nvm_file_name) {
  571. /* in case nvm file was failed try again */
  572. ret = iwl_mvm_read_external_nvm(mvm);
  573. if (ret)
  574. return ret;
  575. } else {
  576. return ret;
  577. }
  578. }
  579. }
  580. /* parse the relevant nvm sections */
  581. mvm->nvm_data = iwl_parse_nvm_sections(mvm);
  582. if (!mvm->nvm_data)
  583. return -ENODATA;
  584. IWL_DEBUG_EEPROM(mvm->trans->dev, "nvm version = %x\n",
  585. mvm->nvm_data->nvm_version);
  586. return 0;
  587. }
  588. struct iwl_mcc_update_resp *
  589. iwl_mvm_update_mcc(struct iwl_mvm *mvm, const char *alpha2,
  590. enum iwl_mcc_source src_id)
  591. {
  592. struct iwl_mcc_update_cmd mcc_update_cmd = {
  593. .mcc = cpu_to_le16(alpha2[0] << 8 | alpha2[1]),
  594. .source_id = (u8)src_id,
  595. };
  596. struct iwl_mcc_update_resp *mcc_resp, *resp_cp = NULL;
  597. struct iwl_rx_packet *pkt;
  598. struct iwl_host_cmd cmd = {
  599. .id = MCC_UPDATE_CMD,
  600. .flags = CMD_WANT_SKB,
  601. .data = { &mcc_update_cmd },
  602. };
  603. int ret;
  604. u32 status;
  605. int resp_len, n_channels;
  606. u16 mcc;
  607. if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
  608. return ERR_PTR(-EOPNOTSUPP);
  609. cmd.len[0] = sizeof(struct iwl_mcc_update_cmd);
  610. IWL_DEBUG_LAR(mvm, "send MCC update to FW with '%c%c' src = %d\n",
  611. alpha2[0], alpha2[1], src_id);
  612. ret = iwl_mvm_send_cmd(mvm, &cmd);
  613. if (ret)
  614. return ERR_PTR(ret);
  615. pkt = cmd.resp_pkt;
  616. /* Extract MCC response */
  617. mcc_resp = (void *)pkt->data;
  618. status = le32_to_cpu(mcc_resp->status);
  619. mcc = le16_to_cpu(mcc_resp->mcc);
  620. /* W/A for a FW/NVM issue - returns 0x00 for the world domain */
  621. if (mcc == 0) {
  622. mcc = 0x3030; /* "00" - world */
  623. mcc_resp->mcc = cpu_to_le16(mcc);
  624. }
  625. n_channels = __le32_to_cpu(mcc_resp->n_channels);
  626. IWL_DEBUG_LAR(mvm,
  627. "MCC response status: 0x%x. new MCC: 0x%x ('%c%c') change: %d n_chans: %d\n",
  628. status, mcc, mcc >> 8, mcc & 0xff,
  629. !!(status == MCC_RESP_NEW_CHAN_PROFILE), n_channels);
  630. resp_len = sizeof(*mcc_resp) + n_channels * sizeof(__le32);
  631. resp_cp = kmemdup(mcc_resp, resp_len, GFP_KERNEL);
  632. if (!resp_cp) {
  633. ret = -ENOMEM;
  634. goto exit;
  635. }
  636. ret = 0;
  637. exit:
  638. iwl_free_resp(&cmd);
  639. if (ret)
  640. return ERR_PTR(ret);
  641. return resp_cp;
  642. }
  643. #ifdef CONFIG_ACPI
  644. #define WRD_METHOD "WRDD"
  645. #define WRDD_WIFI (0x07)
  646. #define WRDD_WIGIG (0x10)
  647. static u32 iwl_mvm_wrdd_get_mcc(struct iwl_mvm *mvm, union acpi_object *wrdd)
  648. {
  649. union acpi_object *mcc_pkg, *domain_type, *mcc_value;
  650. u32 i;
  651. if (wrdd->type != ACPI_TYPE_PACKAGE ||
  652. wrdd->package.count < 2 ||
  653. wrdd->package.elements[0].type != ACPI_TYPE_INTEGER ||
  654. wrdd->package.elements[0].integer.value != 0) {
  655. IWL_DEBUG_LAR(mvm, "Unsupported wrdd structure\n");
  656. return 0;
  657. }
  658. for (i = 1 ; i < wrdd->package.count ; ++i) {
  659. mcc_pkg = &wrdd->package.elements[i];
  660. if (mcc_pkg->type != ACPI_TYPE_PACKAGE ||
  661. mcc_pkg->package.count < 2 ||
  662. mcc_pkg->package.elements[0].type != ACPI_TYPE_INTEGER ||
  663. mcc_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) {
  664. mcc_pkg = NULL;
  665. continue;
  666. }
  667. domain_type = &mcc_pkg->package.elements[0];
  668. if (domain_type->integer.value == WRDD_WIFI)
  669. break;
  670. mcc_pkg = NULL;
  671. }
  672. if (mcc_pkg) {
  673. mcc_value = &mcc_pkg->package.elements[1];
  674. return mcc_value->integer.value;
  675. }
  676. return 0;
  677. }
  678. static int iwl_mvm_get_bios_mcc(struct iwl_mvm *mvm, char *mcc)
  679. {
  680. acpi_handle root_handle;
  681. acpi_handle handle;
  682. struct acpi_buffer wrdd = {ACPI_ALLOCATE_BUFFER, NULL};
  683. acpi_status status;
  684. u32 mcc_val;
  685. struct pci_dev *pdev = to_pci_dev(mvm->dev);
  686. root_handle = ACPI_HANDLE(&pdev->dev);
  687. if (!root_handle) {
  688. IWL_DEBUG_LAR(mvm,
  689. "Could not retrieve root port ACPI handle\n");
  690. return -ENOENT;
  691. }
  692. /* Get the method's handle */
  693. status = acpi_get_handle(root_handle, (acpi_string)WRD_METHOD, &handle);
  694. if (ACPI_FAILURE(status)) {
  695. IWL_DEBUG_LAR(mvm, "WRD method not found\n");
  696. return -ENOENT;
  697. }
  698. /* Call WRDD with no arguments */
  699. status = acpi_evaluate_object(handle, NULL, NULL, &wrdd);
  700. if (ACPI_FAILURE(status)) {
  701. IWL_DEBUG_LAR(mvm, "WRDC invocation failed (0x%x)\n", status);
  702. return -ENOENT;
  703. }
  704. mcc_val = iwl_mvm_wrdd_get_mcc(mvm, wrdd.pointer);
  705. kfree(wrdd.pointer);
  706. if (!mcc_val)
  707. return -ENOENT;
  708. mcc[0] = (mcc_val >> 8) & 0xff;
  709. mcc[1] = mcc_val & 0xff;
  710. mcc[2] = '\0';
  711. return 0;
  712. }
  713. #else /* CONFIG_ACPI */
  714. static int iwl_mvm_get_bios_mcc(struct iwl_mvm *mvm, char *mcc)
  715. {
  716. return -ENOENT;
  717. }
  718. #endif
  719. int iwl_mvm_init_mcc(struct iwl_mvm *mvm)
  720. {
  721. bool tlv_lar;
  722. bool nvm_lar;
  723. int retval;
  724. struct ieee80211_regdomain *regd;
  725. char mcc[3];
  726. if (mvm->cfg->device_family == IWL_DEVICE_FAMILY_8000) {
  727. tlv_lar = fw_has_capa(&mvm->fw->ucode_capa,
  728. IWL_UCODE_TLV_CAPA_LAR_SUPPORT);
  729. nvm_lar = mvm->nvm_data->lar_enabled;
  730. if (tlv_lar != nvm_lar)
  731. IWL_INFO(mvm,
  732. "Conflict between TLV & NVM regarding enabling LAR (TLV = %s NVM =%s)\n",
  733. tlv_lar ? "enabled" : "disabled",
  734. nvm_lar ? "enabled" : "disabled");
  735. }
  736. if (!iwl_mvm_is_lar_supported(mvm))
  737. return 0;
  738. /*
  739. * try to replay the last set MCC to FW. If it doesn't exist,
  740. * queue an update to cfg80211 to retrieve the default alpha2 from FW.
  741. */
  742. retval = iwl_mvm_init_fw_regd(mvm);
  743. if (retval != -ENOENT)
  744. return retval;
  745. /*
  746. * Driver regulatory hint for initial update, this also informs the
  747. * firmware we support wifi location updates.
  748. * Disallow scans that might crash the FW while the LAR regdomain
  749. * is not set.
  750. */
  751. mvm->lar_regdom_set = false;
  752. regd = iwl_mvm_get_current_regdomain(mvm, NULL);
  753. if (IS_ERR_OR_NULL(regd))
  754. return -EIO;
  755. if (iwl_mvm_is_wifi_mcc_supported(mvm) &&
  756. !iwl_mvm_get_bios_mcc(mvm, mcc)) {
  757. kfree(regd);
  758. regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc,
  759. MCC_SOURCE_BIOS, NULL);
  760. if (IS_ERR_OR_NULL(regd))
  761. return -EIO;
  762. }
  763. retval = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd);
  764. kfree(regd);
  765. return retval;
  766. }
  767. void iwl_mvm_rx_chub_update_mcc(struct iwl_mvm *mvm,
  768. struct iwl_rx_cmd_buffer *rxb)
  769. {
  770. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  771. struct iwl_mcc_chub_notif *notif = (void *)pkt->data;
  772. enum iwl_mcc_source src;
  773. char mcc[3];
  774. struct ieee80211_regdomain *regd;
  775. lockdep_assert_held(&mvm->mutex);
  776. if (WARN_ON_ONCE(!iwl_mvm_is_lar_supported(mvm)))
  777. return;
  778. mcc[0] = notif->mcc >> 8;
  779. mcc[1] = notif->mcc & 0xff;
  780. mcc[2] = '\0';
  781. src = notif->source_id;
  782. IWL_DEBUG_LAR(mvm,
  783. "RX: received chub update mcc cmd (mcc '%s' src %d)\n",
  784. mcc, src);
  785. regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, mcc, src, NULL);
  786. if (IS_ERR_OR_NULL(regd))
  787. return;
  788. regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
  789. kfree(regd);
  790. }