nvm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 6000
  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. * HOW TO CREATE THE NVM FILE FORMAT:
  232. * ------------------------------
  233. * 1. create hex file, format:
  234. * 3800 -> header
  235. * 0000 -> header
  236. * 5a40 -> data
  237. *
  238. * rev - 6 bit (word1)
  239. * len - 10 bit (word1)
  240. * id - 4 bit (word2)
  241. * rsv - 12 bit (word2)
  242. *
  243. * 2. flip 8bits with 8 bits per line to get the right NVM file format
  244. *
  245. * 3. create binary file from the hex file
  246. *
  247. * 4. save as "iNVM_xxx.bin" under /lib/firmware
  248. */
  249. static int iwl_mvm_load_external_nvm(struct iwl_mvm *mvm)
  250. {
  251. int ret, section_id, section_size;
  252. const struct firmware *fw_entry;
  253. const struct {
  254. __le16 word1;
  255. __le16 word2;
  256. u8 data[];
  257. } *file_sec;
  258. const u8 *eof;
  259. #define NVM_WORD1_LEN(x) (8 * (x & 0x03FF))
  260. #define NVM_WORD2_ID(x) (x >> 12)
  261. /*
  262. * Obtain NVM image via request_firmware. Since we already used
  263. * request_firmware_nowait() for the firmware binary load and only
  264. * get here after that we assume the NVM request can be satisfied
  265. * synchronously.
  266. */
  267. ret = request_firmware(&fw_entry, iwlwifi_mod_params.nvm_file,
  268. mvm->trans->dev);
  269. if (ret) {
  270. IWL_ERR(mvm, "ERROR: %s isn't available %d\n",
  271. iwlwifi_mod_params.nvm_file, ret);
  272. return ret;
  273. }
  274. IWL_INFO(mvm, "Loaded NVM file %s (%zu bytes)\n",
  275. iwlwifi_mod_params.nvm_file, fw_entry->size);
  276. if (fw_entry->size < sizeof(*file_sec)) {
  277. IWL_ERR(mvm, "NVM file too small\n");
  278. ret = -EINVAL;
  279. goto out;
  280. }
  281. if (fw_entry->size > MAX_NVM_FILE_LEN) {
  282. IWL_ERR(mvm, "NVM file too large\n");
  283. ret = -EINVAL;
  284. goto out;
  285. }
  286. eof = fw_entry->data + fw_entry->size;
  287. file_sec = (void *)fw_entry->data;
  288. while (true) {
  289. if (file_sec->data > eof) {
  290. IWL_ERR(mvm,
  291. "ERROR - NVM file too short for section header\n");
  292. ret = -EINVAL;
  293. break;
  294. }
  295. /* check for EOF marker */
  296. if (!file_sec->word1 && !file_sec->word2) {
  297. ret = 0;
  298. break;
  299. }
  300. section_size = 2 * NVM_WORD1_LEN(le16_to_cpu(file_sec->word1));
  301. section_id = NVM_WORD2_ID(le16_to_cpu(file_sec->word2));
  302. if (section_size > IWL_MAX_NVM_SECTION_SIZE) {
  303. IWL_ERR(mvm, "ERROR - section too large (%d)\n",
  304. section_size);
  305. ret = -EINVAL;
  306. break;
  307. }
  308. if (!section_size) {
  309. IWL_ERR(mvm, "ERROR - section empty\n");
  310. ret = -EINVAL;
  311. break;
  312. }
  313. if (file_sec->data + section_size > eof) {
  314. IWL_ERR(mvm,
  315. "ERROR - NVM file too short for section (%d bytes)\n",
  316. section_size);
  317. ret = -EINVAL;
  318. break;
  319. }
  320. ret = iwl_nvm_write_section(mvm, section_id, file_sec->data,
  321. section_size);
  322. if (ret < 0) {
  323. IWL_ERR(mvm, "iwl_mvm_send_cmd failed: %d\n", ret);
  324. break;
  325. }
  326. /* advance to the next section */
  327. file_sec = (void *)(file_sec->data + section_size);
  328. }
  329. out:
  330. release_firmware(fw_entry);
  331. return ret;
  332. }
  333. int iwl_nvm_init(struct iwl_mvm *mvm)
  334. {
  335. int ret, i, section;
  336. u8 *nvm_buffer, *temp;
  337. /* load external NVM if configured */
  338. if (iwlwifi_mod_params.nvm_file) {
  339. /* move to External NVM flow */
  340. ret = iwl_mvm_load_external_nvm(mvm);
  341. if (ret)
  342. return ret;
  343. }
  344. /* Read From FW NVM */
  345. IWL_DEBUG_EEPROM(mvm->trans->dev, "Read from NVM\n");
  346. /* TODO: find correct NVM max size for a section */
  347. nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size,
  348. GFP_KERNEL);
  349. if (!nvm_buffer)
  350. return -ENOMEM;
  351. for (i = 0; i < ARRAY_SIZE(nvm_to_read); i++) {
  352. section = nvm_to_read[i];
  353. /* we override the constness for initial read */
  354. ret = iwl_nvm_read_section(mvm, section, nvm_buffer);
  355. if (ret < 0)
  356. break;
  357. temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
  358. if (!temp) {
  359. ret = -ENOMEM;
  360. break;
  361. }
  362. mvm->nvm_sections[section].data = temp;
  363. mvm->nvm_sections[section].length = ret;
  364. }
  365. kfree(nvm_buffer);
  366. if (ret < 0)
  367. return ret;
  368. mvm->nvm_data = iwl_parse_nvm_sections(mvm);
  369. if (!mvm->nvm_data)
  370. return -ENODATA;
  371. return 0;
  372. }