nvm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 - 2013 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 - 2013 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. /* list of NVM sections we are allowed/need to read */
  70. static const int nvm_to_read[] = {
  71. NVM_SECTION_TYPE_HW,
  72. NVM_SECTION_TYPE_SW,
  73. NVM_SECTION_TYPE_CALIBRATION,
  74. NVM_SECTION_TYPE_PRODUCTION,
  75. };
  76. /* Default NVM size to read */
  77. #define IWL_NVM_DEFAULT_CHUNK_SIZE (2*1024)
  78. #define IWL_MAX_NVM_SECTION_SIZE 7000
  79. #define NVM_WRITE_OPCODE 1
  80. #define NVM_READ_OPCODE 0
  81. /*
  82. * prepare the NVM host command w/ the pointers to the nvm buffer
  83. * and send it to fw
  84. */
  85. static int iwl_nvm_write_chunk(struct iwl_mvm *mvm, u16 section,
  86. u16 offset, u16 length, const u8 *data)
  87. {
  88. struct iwl_nvm_access_cmd nvm_access_cmd = {
  89. .offset = cpu_to_le16(offset),
  90. .length = cpu_to_le16(length),
  91. .type = cpu_to_le16(section),
  92. .op_code = NVM_WRITE_OPCODE,
  93. };
  94. struct iwl_host_cmd cmd = {
  95. .id = NVM_ACCESS_CMD,
  96. .len = { sizeof(struct iwl_nvm_access_cmd), length },
  97. .flags = CMD_SYNC | CMD_SEND_IN_RFKILL,
  98. .data = { &nvm_access_cmd, data },
  99. /* data may come from vmalloc, so use _DUP */
  100. .dataflags = { 0, IWL_HCMD_DFL_DUP },
  101. };
  102. return iwl_mvm_send_cmd(mvm, &cmd);
  103. }
  104. static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section,
  105. u16 offset, u16 length, u8 *data)
  106. {
  107. struct iwl_nvm_access_cmd nvm_access_cmd = {
  108. .offset = cpu_to_le16(offset),
  109. .length = cpu_to_le16(length),
  110. .type = cpu_to_le16(section),
  111. .op_code = NVM_READ_OPCODE,
  112. };
  113. struct iwl_nvm_access_resp *nvm_resp;
  114. struct iwl_rx_packet *pkt;
  115. struct iwl_host_cmd cmd = {
  116. .id = NVM_ACCESS_CMD,
  117. .flags = CMD_SYNC | CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
  118. .data = { &nvm_access_cmd, },
  119. };
  120. int ret, bytes_read, offset_read;
  121. u8 *resp_data;
  122. cmd.len[0] = sizeof(struct iwl_nvm_access_cmd);
  123. ret = iwl_mvm_send_cmd(mvm, &cmd);
  124. if (ret)
  125. return ret;
  126. pkt = cmd.resp_pkt;
  127. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  128. IWL_ERR(mvm, "Bad return from NVM_ACCES_COMMAND (0x%08X)\n",
  129. pkt->hdr.flags);
  130. ret = -EIO;
  131. goto exit;
  132. }
  133. /* Extract NVM response */
  134. nvm_resp = (void *)pkt->data;
  135. ret = le16_to_cpu(nvm_resp->status);
  136. bytes_read = le16_to_cpu(nvm_resp->length);
  137. offset_read = le16_to_cpu(nvm_resp->offset);
  138. resp_data = nvm_resp->data;
  139. if (ret) {
  140. IWL_ERR(mvm,
  141. "NVM access command failed with status %d (device: %s)\n",
  142. ret, mvm->cfg->name);
  143. ret = -EINVAL;
  144. goto exit;
  145. }
  146. if (offset_read != offset) {
  147. IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n",
  148. offset_read);
  149. ret = -EINVAL;
  150. goto exit;
  151. }
  152. /* Write data to NVM */
  153. memcpy(data + offset, resp_data, bytes_read);
  154. ret = bytes_read;
  155. exit:
  156. iwl_free_resp(&cmd);
  157. return ret;
  158. }
  159. static int iwl_nvm_write_section(struct iwl_mvm *mvm, u16 section,
  160. const u8 *data, u16 length)
  161. {
  162. int offset = 0;
  163. /* copy data in chunks of 2k (and remainder if any) */
  164. while (offset < length) {
  165. int chunk_size, ret;
  166. chunk_size = min(IWL_NVM_DEFAULT_CHUNK_SIZE,
  167. length - offset);
  168. ret = iwl_nvm_write_chunk(mvm, section, offset,
  169. chunk_size, data + offset);
  170. if (ret < 0)
  171. return ret;
  172. offset += chunk_size;
  173. }
  174. return 0;
  175. }
  176. /*
  177. * Reads an NVM section completely.
  178. * NICs prior to 7000 family doesn't have a real NVM, but just read
  179. * section 0 which is the EEPROM. Because the EEPROM reading is unlimited
  180. * by uCode, we need to manually check in this case that we don't
  181. * overflow and try to read more than the EEPROM size.
  182. * For 7000 family NICs, we supply the maximal size we can read, and
  183. * the uCode fills the response with as much data as we can,
  184. * without overflowing, so no check is needed.
  185. */
  186. static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section,
  187. u8 *data)
  188. {
  189. u16 length, offset = 0;
  190. int ret;
  191. /* Set nvm section read length */
  192. length = IWL_NVM_DEFAULT_CHUNK_SIZE;
  193. ret = length;
  194. /* Read the NVM until exhausted (reading less than requested) */
  195. while (ret == length) {
  196. ret = iwl_nvm_read_chunk(mvm, section, offset, length, data);
  197. if (ret < 0) {
  198. IWL_ERR(mvm,
  199. "Cannot read NVM from section %d offset %d, length %d\n",
  200. section, offset, length);
  201. return ret;
  202. }
  203. offset += ret;
  204. }
  205. IWL_DEBUG_EEPROM(mvm->trans->dev,
  206. "NVM section %d read completed\n", section);
  207. return offset;
  208. }
  209. static struct iwl_nvm_data *
  210. iwl_parse_nvm_sections(struct iwl_mvm *mvm)
  211. {
  212. struct iwl_nvm_section *sections = mvm->nvm_sections;
  213. const __le16 *hw, *sw, *calib;
  214. /* Checking for required sections */
  215. if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
  216. !mvm->nvm_sections[NVM_SECTION_TYPE_HW].data) {
  217. IWL_ERR(mvm, "Can't parse empty NVM sections\n");
  218. return NULL;
  219. }
  220. if (WARN_ON(!mvm->cfg))
  221. return NULL;
  222. hw = (const __le16 *)sections[NVM_SECTION_TYPE_HW].data;
  223. sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data;
  224. calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data;
  225. return iwl_parse_nvm_data(mvm->trans->dev, mvm->cfg, hw, sw, calib,
  226. iwl_fw_valid_tx_ant(mvm->fw),
  227. iwl_fw_valid_rx_ant(mvm->fw));
  228. }
  229. #define MAX_NVM_FILE_LEN 16384
  230. /*
  231. * Reads external NVM from a file into mvm->nvm_sections
  232. *
  233. * HOW TO CREATE THE NVM FILE FORMAT:
  234. * ------------------------------
  235. * 1. create hex file, format:
  236. * 3800 -> header
  237. * 0000 -> header
  238. * 5a40 -> data
  239. *
  240. * rev - 6 bit (word1)
  241. * len - 10 bit (word1)
  242. * id - 4 bit (word2)
  243. * rsv - 12 bit (word2)
  244. *
  245. * 2. flip 8bits with 8 bits per line to get the right NVM file format
  246. *
  247. * 3. create binary file from the hex file
  248. *
  249. * 4. save as "iNVM_xxx.bin" under /lib/firmware
  250. */
  251. static int iwl_mvm_read_external_nvm(struct iwl_mvm *mvm)
  252. {
  253. int ret, section_size;
  254. u16 section_id;
  255. const struct firmware *fw_entry;
  256. const struct {
  257. __le16 word1;
  258. __le16 word2;
  259. u8 data[];
  260. } *file_sec;
  261. const u8 *eof, *temp;
  262. #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
  263. #define NVM_WORD2_ID(x) (x >> 12)
  264. IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from external NVM\n");
  265. /*
  266. * Obtain NVM image via request_firmware. Since we already used
  267. * request_firmware_nowait() for the firmware binary load and only
  268. * get here after that we assume the NVM request can be satisfied
  269. * synchronously.
  270. */
  271. ret = request_firmware(&fw_entry, iwlwifi_mod_params.nvm_file,
  272. mvm->trans->dev);
  273. if (ret) {
  274. IWL_ERR(mvm, "ERROR: %s isn't available %d\n",
  275. iwlwifi_mod_params.nvm_file, ret);
  276. return ret;
  277. }
  278. IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n",
  279. iwlwifi_mod_params.nvm_file, fw_entry->size);
  280. if (fw_entry->size < sizeof(*file_sec)) {
  281. IWL_ERR(mvm, "NVM file too small\n");
  282. ret = -EINVAL;
  283. goto out;
  284. }
  285. if (fw_entry->size > MAX_NVM_FILE_LEN) {
  286. IWL_ERR(mvm, "NVM file too large\n");
  287. ret = -EINVAL;
  288. goto out;
  289. }
  290. eof = fw_entry->data + fw_entry->size;
  291. file_sec = (void *)fw_entry->data;
  292. while (true) {
  293. if (file_sec->data > eof) {
  294. IWL_ERR(mvm,
  295. "ERROR - NVM file too short for section header\n");
  296. ret = -EINVAL;
  297. break;
  298. }
  299. /* check for EOF marker */
  300. if (!file_sec->word1 && !file_sec->word2) {
  301. ret = 0;
  302. break;
  303. }
  304. section_size = 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1));
  305. section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2));
  306. if (section_size > IWL_MAX_NVM_SECTION_SIZE) {
  307. IWL_ERR(mvm, "ERROR - section too large (%d)\n",
  308. section_size);
  309. ret = -EINVAL;
  310. break;
  311. }
  312. if (!section_size) {
  313. IWL_ERR(mvm, "ERROR - section empty\n");
  314. ret = -EINVAL;
  315. break;
  316. }
  317. if (file_sec->data + section_size > eof) {
  318. IWL_ERR(mvm,
  319. "ERROR - NVM file too short for section (%d bytes)\n",
  320. section_size);
  321. ret = -EINVAL;
  322. break;
  323. }
  324. temp = kmemdup(file_sec->data, section_size, GFP_KERNEL);
  325. if (!temp) {
  326. ret = -ENOMEM;
  327. break;
  328. }
  329. if (WARN_ON(section_id >= NVM_NUM_OF_SECTIONS)) {
  330. IWL_ERR(mvm, "Invalid NVM section ID\n");
  331. ret = -EINVAL;
  332. break;
  333. }
  334. mvm->nvm_sections[section_id].data = temp;
  335. mvm->nvm_sections[section_id].length = section_size;
  336. /* advance to the next section */
  337. file_sec = (void *)(file_sec->data + section_size);
  338. }
  339. out:
  340. release_firmware(fw_entry);
  341. return ret;
  342. }
  343. /* Loads the NVM data stored in mvm->nvm_sections into the NIC */
  344. int iwl_mvm_load_nvm_to_nic(struct iwl_mvm *mvm)
  345. {
  346. int i, ret;
  347. u16 section_id;
  348. struct iwl_nvm_section *sections = mvm->nvm_sections;
  349. IWL_DEBUG_EEPROM(mvm->trans->dev, "'Write to NVM\n");
  350. for (i = 0; i < ARRAY_SIZE(nvm_to_read); i++) {
  351. section_id = nvm_to_read[i];
  352. ret = iwl_nvm_write_section(mvm, section_id,
  353. sections[section_id].data,
  354. sections[section_id].length);
  355. if (ret < 0) {
  356. IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);
  357. break;
  358. }
  359. }
  360. return ret;
  361. }
  362. int iwl_nvm_init(struct iwl_mvm *mvm)
  363. {
  364. int ret, i, section;
  365. u8 *nvm_buffer, *temp;
  366. /* load external NVM if configured */
  367. if (iwlwifi_mod_params.nvm_file) {
  368. /* move to External NVM flow */
  369. ret = iwl_mvm_read_external_nvm(mvm);
  370. if (ret)
  371. return ret;
  372. } else {
  373. /* Read From FW NVM */
  374. IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");
  375. /* TODO: find correct NVM max size for a section */
  376. nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size,
  377. GFP_KERNEL);
  378. if (!nvm_buffer)
  379. return -ENOMEM;
  380. for (i = 0; i < ARRAY_SIZE(nvm_to_read); i++) {
  381. section = nvm_to_read[i];
  382. /* we override the constness for initial read */
  383. ret = iwl_nvm_read_section(mvm, section, nvm_buffer);
  384. if (ret < 0)
  385. break;
  386. temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
  387. if (!temp) {
  388. ret = -ENOMEM;
  389. break;
  390. }
  391. mvm->nvm_sections[section].data = temp;
  392. mvm->nvm_sections[section].length = ret;
  393. }
  394. kfree(nvm_buffer);
  395. if (ret < 0)
  396. return ret;
  397. }
  398. mvm->nvm_data = iwl_parse_nvm_sections(mvm);
  399. if (!mvm->nvm_data)
  400. return -ENODATA;
  401. return 0;
  402. }