nvm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called COPYING.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/firmware.h>
  64. #include "iwl-trans.h"
  65. #include "mvm.h"
  66. #include "iwl-eeprom-parse.h"
  67. #include "iwl-eeprom-read.h"
  68. #include "iwl-nvm-parse.h"
  69. /* Default NVM size to read */
  70. #define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024)
  71. #define IWL_MAX_NVM_SECTION_SIZE 7000
  72. #define NVM_WRITE_OPCODE 1
  73. #define NVM_READ_OPCODE 0
  74. /* load nvm chunk response */
  75. enum {
  76. READ_NVM_CHUNK_SUCCEED = 0,
  77. READ_NVM_CHUNK_NOT_VALID_ADDRESS = 1
  78. };
  79. /*
  80. * prepare the NVM host command w/ the pointers to the nvm buffer
  81. * and send it to fw
  82. */
  83. static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section,
  84. u16 offset, u16 length, const u8 *data)
  85. {
  86. struct iwl_nvm_access_cmd nvm_access_cmd = {
  87. .offset = cpu_to_le16(offset),
  88. .length = cpu_to_le16(length),
  89. .type = cpu_to_le16(section),
  90. .op_code = NVM_WRITE_OPCODE,
  91. };
  92. struct iwl_host_cmd cmd = {
  93. .id = NVM_ACCESS_CMD,
  94. .len = { sizeof(struct iwl_nvm_access_cmd), length },
  95. .flags = CMD_SEND_IN_RFKILL,
  96. .data = { &nvm_access_cmd, data },
  97. /* data may come from vmalloc, so use _DUP */
  98. .dataflags = { 0, IWL_HCMD_DFL_DUP },
  99. };
  100. return iwl_mvm_send_cmd(mvm, &cmd);
  101. }
  102. static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section,
  103. u16 offset, u16 length, u8 *data)
  104. {
  105. struct iwl_nvm_access_cmd nvm_access_cmd = {
  106. .offset = cpu_to_le16(offset),
  107. .length = cpu_to_le16(length),
  108. .type = cpu_to_le16(section),
  109. .op_code = NVM_READ_OPCODE,
  110. };
  111. struct iwl_nvm_access_resp *nvm_resp;
  112. struct iwl_rx_packet *pkt;
  113. struct iwl_host_cmd cmd = {
  114. .id = NVM_ACCESS_CMD,
  115. .flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
  116. .data = { &nvm_access_cmd, },
  117. };
  118. int ret, bytes_read, offset_read;
  119. u8 *resp_data;
  120. cmd.len[0] = sizeof(struct iwl_nvm_access_cmd);
  121. ret = iwl_mvm_send_cmd(mvm, &cmd);
  122. if (ret)
  123. return ret;
  124. pkt = cmd.resp_pkt;
  125. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  126. IWL_ERR(mvm, "Bad return from NVM_ACCES_COMMAND (0x%08X)\n",
  127. pkt->hdr.flags);
  128. ret = -EIO;
  129. goto exit;
  130. }
  131. /* Extract NVM response */
  132. nvm_resp = (void *)pkt->data;
  133. ret = le16_to_cpu(nvm_resp->status);
  134. bytes_read = le16_to_cpu(nvm_resp->length);
  135. offset_read = le16_to_cpu(nvm_resp->offset);
  136. resp_data = nvm_resp->data;
  137. if (ret) {
  138. if ((offset != 0) &&
  139. (ret == READ_NVM_CHUNK_NOT_VALID_ADDRESS)) {
  140. /*
  141. * meaning of NOT_VALID_ADDRESS:
  142. * driver try to read chunk from address that is
  143. * multiple of 2K and got an error since addr is empty.
  144. * meaning of (offset != 0): driver already
  145. * read valid data from another chunk so this case
  146. * is not an error.
  147. */
  148. IWL_DEBUG_EEPROM(mvm->trans->dev,
  149. "NVM access command failed on offset 0x%x since that section size is multiple 2K\n",
  150. offset);
  151. ret = 0;
  152. } else {
  153. IWL_DEBUG_EEPROM(mvm->trans->dev,
  154. "NVM access command failed with status %d (device: %s)\n",
  155. ret, mvm->cfg->name);
  156. ret = -EIO;
  157. }
  158. goto exit;
  159. }
  160. if (offset_read != offset) {
  161. IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n",
  162. offset_read);
  163. ret = -EINVAL;
  164. goto exit;
  165. }
  166. /* Write data to NVM */
  167. memcpy(data + offset, resp_data, bytes_read);
  168. ret = bytes_read;
  169. exit:
  170. iwl_free_resp(&cmd);
  171. return ret;
  172. }
  173. static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section,
  174. const u8 *data, u16 length)
  175. {
  176. int offset = 0;
  177. /* copy data in chunks of 2k (and remainder if any) */
  178. while (offset < length) {
  179. int chunk_size, ret;
  180. chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE,
  181. length - offset);
  182. ret = iwl_nvm_write_chunk(mvm, section, offset,
  183. chunk_size, data + offset);
  184. if (ret < 0)
  185. return ret;
  186. offset += chunk_size;
  187. }
  188. return 0;
  189. }
  190. /*
  191. * Reads an NVM section completely.
  192. * NICs prior to 7000 family doesn't have a real NVM, but just read
  193. * section 0 which is the EEPROM. Because the EEPROM reading is unlimited
  194. * by uCode, we need to manually check in this case that we don't
  195. * overflow and try to read more than the EEPROM size.
  196. * For 7000 family NICs, we supply the maximal size we can read, and
  197. * the uCode fills the response with as much data as we can,
  198. * without overflowing, so no check is needed.
  199. */
  200. static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section,
  201. u8 *data)
  202. {
  203. u16 length, offset = 0;
  204. int ret;
  205. /* Set nvm section read length */
  206. length = IWL_NVM_DEFAULT_CHUNK_SIZE;
  207. ret = length;
  208. /* Read the NVM until exhausted (reading less than requested) */
  209. while (ret == length) {
  210. ret = iwl_nvm_read_chunk(mvm, section, offset, length, data);
  211. if (ret < 0) {
  212. IWL_DEBUG_EEPROM(mvm->trans->dev,
  213. "Cannot read NVM from section %d offset %d, length %d\n",
  214. section, offset, length);
  215. return ret;
  216. }
  217. offset += ret;
  218. }
  219. IWL_DEBUG_EEPROM(mvm->trans->dev,
  220. "NVM section %d read completed\n", section);
  221. return offset;
  222. }
  223. static struct iwl_nvm_data *
  224. iwl_parse_nvm_sections(struct iwl_mvm *mvm)
  225. {
  226. struct iwl_nvm_section *sections = mvm->nvm_sections;
  227. const __le16 *hw, *sw, *calib, *regulatory, *mac_override;
  228. /* Checking for required sections */
  229. if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
  230. if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
  231. !mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data) {
  232. IWL_ERR(mvm, "Can't parse empty NVM sections\n");
  233. return NULL;
  234. }
  235. } else {
  236. /* SW and REGULATORY sections are mandatory */
  237. if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
  238. !mvm->nvm_sections[NVM_SECTION_TYPE_REGULATORY].data) {
  239. IWL_ERR(mvm,
  240. "Can't parse empty family 8000 NVM sections\n");
  241. return NULL;
  242. }
  243. /* MAC_OVERRIDE or at least HW section must exist */
  244. if (!mvm->nvm_sections[mvm->cfg->nvm_hw_section_num].data &&
  245. !mvm->nvm_sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data) {
  246. IWL_ERR(mvm,
  247. "Can't parse mac_address, empty sections\n");
  248. return NULL;
  249. }
  250. }
  251. if (WARN_ON(!mvm->cfg))
  252. return NULL;
  253. hw = (const __le16 *)sections[mvm->cfg->nvm_hw_section_num].data;
  254. sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data;
  255. calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data;
  256. regulatory = (const __le16 *)sections[NVM_SECTION_TYPE_REGULATORY].data;
  257. mac_override =
  258. (const __le16 *)sections[NVM_SECTION_TYPE_MAC_OVERRIDE].data;
  259. return iwl_parse_nvm_data(mvm->trans->dev, mvm->cfg, hw, sw, calib,
  260. regulatory, mac_override,
  261. mvm->fw->valid_tx_ant,
  262. mvm->fw->valid_rx_ant);
  263. }
  264. #define MAX_NVM_FILE_LEN 16384
  265. /*
  266. * Reads external NVM from a file into mvm->nvm_sections
  267. *
  268. * HOW TO CREATE THE NVM FILE FORMAT:
  269. * ------------------------------
  270. * 1. create hex file, format:
  271. * 3800 -> header
  272. * 0000 -> header
  273. * 5a40 -> data
  274. *
  275. * rev - 6 bit (word1)
  276. * len - 10 bit (word1)
  277. * id - 4 bit (word2)
  278. * rsv - 12 bit (word2)
  279. *
  280. * 2. flip 8bits with 8 bits per line to get the right NVM file format
  281. *
  282. * 3. create binary file from the hex file
  283. *
  284. * 4. save as "iNVM_xxx.bin" under /lib/firmware
  285. */
  286. static int iwl_mvm_read_external_nvm(struct iwl_mvm *mvm)
  287. {
  288. int ret, section_size;
  289. u16 section_id;
  290. const struct firmware *fw_entry;
  291. const struct {
  292. __le16 word1;
  293. __le16 word2;
  294. u8 data[];
  295. } *file_sec;
  296. const u8 *eof, *temp;
  297. #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
  298. #define NVM_WORD2_ID(x) (x >> 12)
  299. #define NVM_WORD2_LEN_FAMILY_8000(x) (2 * ((x & 0xFF) << 8 | x >> 8))
  300. #define NVM_WORD1_ID_FAMILY_8000(x) (x >> 4)
  301. IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from external NVM\n");
  302. /*
  303. * Obtain NVM image via request_firmware. Since we already used
  304. * request_firmware_nowait() for the firmware binary load and only
  305. * get here after that we assume the NVM request can be satisfied
  306. * synchronously.
  307. */
  308. ret = request_firmware(&fw_entry, mvm->nvm_file_name,
  309. mvm->trans->dev);
  310. if (ret) {
  311. IWL_ERR(mvm, "ERROR: %s isn't available %d\n",
  312. mvm->nvm_file_name, ret);
  313. return ret;
  314. }
  315. IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n",
  316. mvm->nvm_file_name, fw_entry->size);
  317. if (fw_entry->size < sizeof(*file_sec)) {
  318. IWL_ERR(mvm, "NVM file too small\n");
  319. ret = -EINVAL;
  320. goto out;
  321. }
  322. if (fw_entry->size > MAX_NVM_FILE_LEN) {
  323. IWL_ERR(mvm, "NVM file too large\n");
  324. ret = -EINVAL;
  325. goto out;
  326. }
  327. eof = fw_entry->data + fw_entry->size;
  328. file_sec = (void *)fw_entry->data;
  329. while (true) {
  330. if (file_sec->data > eof) {
  331. IWL_ERR(mvm,
  332. "ERROR - NVM file too short for section header\n");
  333. ret = -EINVAL;
  334. break;
  335. }
  336. /* check for EOF marker */
  337. if (!file_sec->word1 && !file_sec->word2) {
  338. ret = 0;
  339. break;
  340. }
  341. if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
  342. section_size =
  343. 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1));
  344. section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2));
  345. } else {
  346. section_size = 2 * NVM_WORD2_LEN_FAMILY_8000(
  347. le16_to_cpu(file_sec->word2));
  348. section_id = NVM_WORD1_ID_FAMILY_8000(
  349. le16_to_cpu(file_sec->word1));
  350. }
  351. if (section_size > IWL_MAX_NVM_SECTION_SIZE) {
  352. IWL_ERR(mvm, "ERROR - section too large (%d)\n",
  353. section_size);
  354. ret = -EINVAL;
  355. break;
  356. }
  357. if (!section_size) {
  358. IWL_ERR(mvm, "ERROR - section empty\n");
  359. ret = -EINVAL;
  360. break;
  361. }
  362. if (file_sec->data + section_size > eof) {
  363. IWL_ERR(mvm,
  364. "ERROR - NVM file too short for section (%d bytes)\n",
  365. section_size);
  366. ret = -EINVAL;
  367. break;
  368. }
  369. if (WARN(section_id >= NVM_MAX_NUM_SECTIONS,
  370. "Invalid NVM section ID %d\n", section_id)) {
  371. ret = -EINVAL;
  372. break;
  373. }
  374. temp = kmemdup(file_sec->data, section_size, GFP_KERNEL);
  375. if (!temp) {
  376. ret = -ENOMEM;
  377. break;
  378. }
  379. mvm->nvm_sections[section_id].data = temp;
  380. mvm->nvm_sections[section_id].length = section_size;
  381. /* advance to the next section */
  382. file_sec = (void *)(file_sec->data + section_size);
  383. }
  384. out:
  385. release_firmware(fw_entry);
  386. return ret;
  387. }
  388. /* Loads the NVM data stored in mvm->nvm_sections into the NIC */
  389. int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm)
  390. {
  391. int i, ret = 0;
  392. struct iwl_nvm_section *sections = mvm->nvm_sections;
  393. IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n");
  394. for (i = 0; i < ARRAY_SIZE(mvm->nvm_sections); i++) {
  395. if (!mvm->nvm_sections[i].data || !mvm->nvm_sections[i].length)
  396. continue;
  397. ret = iwl_nvm_write_section(mvm, i, sections[i].data,
  398. sections[i].length);
  399. if (ret < 0) {
  400. IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);
  401. break;
  402. }
  403. }
  404. return ret;
  405. }
  406. int iwl_nvm_init(struct iwl_mvm *mvm, bool read_nvm_from_nic)
  407. {
  408. int ret, section;
  409. u8 *nvm_buffer, *temp;
  410. if (WARN_ON_ONCE(mvm->cfg->nvm_hw_section_num >= NVM_MAX_NUM_SECTIONS))
  411. return -EINVAL;
  412. /* load NVM values from nic */
  413. if (read_nvm_from_nic) {
  414. /* Read From FW NVM */
  415. IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");
  416. nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size,
  417. GFP_KERNEL);
  418. if (!nvm_buffer)
  419. return -ENOMEM;
  420. for (section = 0; section < NVM_MAX_NUM_SECTIONS; section++) {
  421. /* we override the constness for initial read */
  422. ret = iwl_nvm_read_section(mvm, section, nvm_buffer);
  423. if (ret < 0)
  424. continue;
  425. temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
  426. if (!temp) {
  427. ret = -ENOMEM;
  428. break;
  429. }
  430. mvm->nvm_sections[section].data = temp;
  431. mvm->nvm_sections[section].length = ret;
  432. #ifdef CONFIG_IWLWIFI_DEBUGFS
  433. switch (section) {
  434. case NVM_SECTION_TYPE_SW:
  435. mvm->nvm_sw_blob.data = temp;
  436. mvm->nvm_sw_blob.size = ret;
  437. break;
  438. case NVM_SECTION_TYPE_CALIBRATION:
  439. mvm->nvm_calib_blob.data = temp;
  440. mvm->nvm_calib_blob.size = ret;
  441. break;
  442. case NVM_SECTION_TYPE_PRODUCTION:
  443. mvm->nvm_prod_blob.data = temp;
  444. mvm->nvm_prod_blob.size = ret;
  445. break;
  446. default:
  447. if (section == mvm->cfg->nvm_hw_section_num) {
  448. mvm->nvm_hw_blob.data = temp;
  449. mvm->nvm_hw_blob.size = ret;
  450. break;
  451. }
  452. }
  453. #endif
  454. }
  455. kfree(nvm_buffer);
  456. }
  457. /* load external NVM if configured */
  458. if (mvm->nvm_file_name) {
  459. /* move to External NVM flow */
  460. ret = iwl_mvm_read_external_nvm(mvm);
  461. if (ret)
  462. return ret;
  463. }
  464. /* parse the relevant nvm sections */
  465. mvm->nvm_data = iwl_parse_nvm_sections(mvm);
  466. if (!mvm->nvm_data)
  467. return -ENODATA;
  468. return 0;
  469. }