nvm.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 LICENSE.GPL.
  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 "iwl-trans.h"
  64. #include "mvm.h"
  65. #include "iwl-eeprom-parse.h"
  66. #include "iwl-eeprom-read.h"
  67. #include "iwl-nvm-parse.h"
  68. /* list of NVM sections we are allowed/need to read */
  69. static const int nvm_to_read[] = {
  70. NVM_SECTION_TYPE_HW,
  71. NVM_SECTION_TYPE_SW,
  72. NVM_SECTION_TYPE_CALIBRATION,
  73. NVM_SECTION_TYPE_PRODUCTION,
  74. };
  75. /* used to simplify the shared operations on NCM_ACCESS_CMD versions */
  76. union iwl_nvm_access_cmd {
  77. struct iwl_nvm_access_cmd_ver1 ver1;
  78. struct iwl_nvm_access_cmd_ver2 ver2;
  79. };
  80. union iwl_nvm_access_resp {
  81. struct iwl_nvm_access_resp_ver1 ver1;
  82. struct iwl_nvm_access_resp_ver2 ver2;
  83. };
  84. static inline void iwl_nvm_fill_read_ver1(struct iwl_nvm_access_cmd_ver1 *cmd,
  85. u16 offset, u16 length)
  86. {
  87. cmd->offset = cpu_to_le16(offset);
  88. cmd->length = cpu_to_le16(length);
  89. cmd->cache_refresh = 1;
  90. }
  91. static inline void iwl_nvm_fill_read_ver2(struct iwl_nvm_access_cmd_ver2 *cmd,
  92. u16 offset, u16 length, u16 section)
  93. {
  94. cmd->offset = cpu_to_le16(offset);
  95. cmd->length = cpu_to_le16(length);
  96. cmd->type = cpu_to_le16(section);
  97. }
  98. static int iwl_nvm_read_chunk(struct iwl_mvm *mvm, u16 section,
  99. u16 offset, u16 length, u8 *data)
  100. {
  101. union iwl_nvm_access_cmd nvm_access_cmd;
  102. union iwl_nvm_access_resp *nvm_resp;
  103. struct iwl_rx_packet *pkt;
  104. struct iwl_host_cmd cmd = {
  105. .id = NVM_ACCESS_CMD,
  106. .flags = CMD_SYNC | CMD_WANT_SKB,
  107. .data = { &nvm_access_cmd, },
  108. };
  109. int ret, bytes_read, offset_read;
  110. u8 *resp_data;
  111. memset(&nvm_access_cmd, 0, sizeof(nvm_access_cmd));
  112. /* TODO: not sure family should be the decider, maybe FW version? */
  113. if (mvm->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
  114. iwl_nvm_fill_read_ver2(&(nvm_access_cmd.ver2),
  115. offset, length, section);
  116. cmd.len[0] = sizeof(struct iwl_nvm_access_cmd_ver2);
  117. } else {
  118. iwl_nvm_fill_read_ver1(&(nvm_access_cmd.ver1),
  119. offset, length);
  120. cmd.len[0] = sizeof(struct iwl_nvm_access_cmd_ver1);
  121. }
  122. ret = iwl_mvm_send_cmd(mvm, &cmd);
  123. if (ret)
  124. return ret;
  125. pkt = cmd.resp_pkt;
  126. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  127. IWL_ERR(mvm, "Bad return from NVM_ACCES_COMMAND (0x%08X)\n",
  128. pkt->hdr.flags);
  129. ret = -EIO;
  130. goto exit;
  131. }
  132. /* Extract NVM response */
  133. nvm_resp = (void *)pkt->data;
  134. if (mvm->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
  135. ret = le16_to_cpu(nvm_resp->ver2.status);
  136. bytes_read = le16_to_cpu(nvm_resp->ver2.length);
  137. offset_read = le16_to_cpu(nvm_resp->ver2.offset);
  138. resp_data = nvm_resp->ver2.data;
  139. } else {
  140. ret = le16_to_cpu(nvm_resp->ver1.length) <= 0;
  141. bytes_read = le16_to_cpu(nvm_resp->ver1.length);
  142. offset_read = le16_to_cpu(nvm_resp->ver1.offset);
  143. resp_data = nvm_resp->ver1.data;
  144. }
  145. if (ret) {
  146. IWL_ERR(mvm,
  147. "NVM access command failed with status %d (device: %s)\n",
  148. ret, mvm->cfg->name);
  149. ret = -EINVAL;
  150. goto exit;
  151. }
  152. if (offset_read != offset) {
  153. IWL_ERR(mvm, "NVM ACCESS response with invalid offset %d\n",
  154. offset_read);
  155. ret = -EINVAL;
  156. goto exit;
  157. }
  158. /* Write data to NVM */
  159. memcpy(data + offset, resp_data, bytes_read);
  160. ret = bytes_read;
  161. exit:
  162. iwl_free_resp(&cmd);
  163. return ret;
  164. }
  165. /*
  166. * Reads an NVM section completely.
  167. * NICs prior to 7000 family doesn't have a real NVM, but just read
  168. * section 0 which is the EEPROM. Because the EEPROM reading is unlimited
  169. * by uCode, we need to manually check in this case that we don't
  170. * overflow and try to read more than the EEPROM size.
  171. * For 7000 family NICs, we supply the maximal size we can read, and
  172. * the uCode fills the response with as much data as we can,
  173. * without overflowing, so no check is needed.
  174. */
  175. static int iwl_nvm_read_section(struct iwl_mvm *mvm, u16 section,
  176. u8 *data)
  177. {
  178. u16 length, offset = 0;
  179. int ret;
  180. bool old_eeprom = mvm->cfg->device_family != IWL_DEVICE_FAMILY_7000;
  181. length = (iwlwifi_mod_params.amsdu_size_8K ? (8 * 1024) : (4 * 1024))
  182. - sizeof(union iwl_nvm_access_cmd)
  183. - sizeof(struct iwl_rx_packet);
  184. /*
  185. * if length is greater than EEPROM size, truncate it because uCode
  186. * doesn't check it by itself, and exit the loop when reached.
  187. */
  188. if (old_eeprom && length > mvm->cfg->base_params->eeprom_size)
  189. length = mvm->cfg->base_params->eeprom_size;
  190. ret = length;
  191. /* Read the NVM until exhausted (reading less than requested) */
  192. while (ret == length) {
  193. ret = iwl_nvm_read_chunk(mvm, section, offset, length, data);
  194. if (ret < 0) {
  195. IWL_ERR(mvm,
  196. "Cannot read NVM from section %d offset %d, length %d\n",
  197. section, offset, length);
  198. return ret;
  199. }
  200. offset += ret;
  201. if (old_eeprom && offset == mvm->cfg->base_params->eeprom_size)
  202. break;
  203. }
  204. IWL_INFO(mvm, "NVM section %d read completed\n", section);
  205. return offset;
  206. }
  207. static struct iwl_nvm_data *
  208. iwl_parse_nvm_sections(struct iwl_mvm *mvm)
  209. {
  210. struct iwl_nvm_section *sections = mvm->nvm_sections;
  211. const __le16 *hw, *sw, *calib;
  212. /* Checking for required sections */
  213. if (!mvm->nvm_sections[NVM_SECTION_TYPE_SW].data ||
  214. !mvm->nvm_sections[NVM_SECTION_TYPE_HW].data) {
  215. IWL_ERR(mvm, "Can't parse empty NVM sections\n");
  216. return NULL;
  217. }
  218. if (WARN_ON(!mvm->cfg))
  219. return NULL;
  220. hw = (const __le16 *)sections[NVM_SECTION_TYPE_HW].data;
  221. sw = (const __le16 *)sections[NVM_SECTION_TYPE_SW].data;
  222. calib = (const __le16 *)sections[NVM_SECTION_TYPE_CALIBRATION].data;
  223. return iwl_parse_nvm_data(mvm->trans->dev, mvm->cfg, hw, sw, calib);
  224. }
  225. int iwl_nvm_init(struct iwl_mvm *mvm)
  226. {
  227. int ret, i, section;
  228. u8 *nvm_buffer, *temp;
  229. if (mvm->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
  230. /* TODO: find correct NVM max size for a section */
  231. nvm_buffer = kmalloc(mvm->cfg->base_params->eeprom_size,
  232. GFP_KERNEL);
  233. if (!nvm_buffer)
  234. return -ENOMEM;
  235. for (i = 0; i < ARRAY_SIZE(nvm_to_read); i++) {
  236. section = nvm_to_read[i];
  237. /* we override the constness for initial read */
  238. ret = iwl_nvm_read_section(mvm, section, nvm_buffer);
  239. if (ret < 0)
  240. break;
  241. temp = kmemdup(nvm_buffer, ret, GFP_KERNEL);
  242. if (!temp) {
  243. ret = -ENOMEM;
  244. break;
  245. }
  246. mvm->nvm_sections[section].data = temp;
  247. mvm->nvm_sections[section].length = ret;
  248. }
  249. kfree(nvm_buffer);
  250. if (ret < 0)
  251. return ret;
  252. } else {
  253. /* allocate eeprom */
  254. mvm->eeprom_blob_size = mvm->cfg->base_params->eeprom_size;
  255. IWL_DEBUG_EEPROM(mvm->trans->dev, "NVM size = %zd\n",
  256. mvm->eeprom_blob_size);
  257. mvm->eeprom_blob = kzalloc(mvm->eeprom_blob_size, GFP_KERNEL);
  258. if (!mvm->eeprom_blob)
  259. return -ENOMEM;
  260. ret = iwl_nvm_read_section(mvm, 0, mvm->eeprom_blob);
  261. if (ret != mvm->eeprom_blob_size) {
  262. IWL_ERR(mvm, "Read partial NVM %d/%zd\n",
  263. ret, mvm->eeprom_blob_size);
  264. kfree(mvm->eeprom_blob);
  265. mvm->eeprom_blob = NULL;
  266. return -EINVAL;
  267. }
  268. }
  269. ret = 0;
  270. if (mvm->cfg->device_family == IWL_DEVICE_FAMILY_7000)
  271. mvm->nvm_data = iwl_parse_nvm_sections(mvm);
  272. else
  273. mvm->nvm_data =
  274. iwl_parse_eeprom_data(mvm->trans->dev,
  275. mvm->cfg,
  276. mvm->eeprom_blob,
  277. mvm->eeprom_blob_size);
  278. if (!mvm->nvm_data) {
  279. kfree(mvm->eeprom_blob);
  280. mvm->eeprom_blob = NULL;
  281. ret = -ENOMEM;
  282. }
  283. return ret;
  284. }