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