nvm.c 24 KB

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