Emmanuel Grumbach | 5c58edc | 2012-02-07 14:18:40 +0200 | [diff] [blame] | 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) 2007 - 2012 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) 2005 - 2012 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/completion.h> |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 64 | #include <linux/dma-mapping.h> |
| 65 | #include <linux/firmware.h> |
| 66 | #include <linux/module.h> |
Emmanuel Grumbach | 5c58edc | 2012-02-07 14:18:40 +0200 | [diff] [blame] | 67 | |
| 68 | #include "iwl-drv.h" |
| 69 | #include "iwl-trans.h" |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 70 | #include "iwl-shared.h" |
Emmanuel Grumbach | d0f76d6 | 2012-02-09 16:08:15 +0200 | [diff] [blame] | 71 | #include "iwl-op-mode.h" |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 72 | #include "iwl-agn-hw.h" |
Emmanuel Grumbach | 5c58edc | 2012-02-07 14:18:40 +0200 | [diff] [blame] | 73 | |
Johannes Berg | 0692fe4 | 2012-03-06 13:30:37 -0800 | [diff] [blame] | 74 | /* private includes */ |
Johannes Berg | 3995dea | 2012-03-06 13:30:44 -0800 | [diff] [blame] | 75 | #include "iwl-fw-file.h" |
Johannes Berg | 0692fe4 | 2012-03-06 13:30:37 -0800 | [diff] [blame] | 76 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 77 | /** |
| 78 | * struct iwl_drv - drv common data |
| 79 | * @fw: the iwl_fw structure |
| 80 | * @shrd: pointer to common shared structure |
| 81 | * @op_mode: the running op_mode |
| 82 | * @fw_index: firmware revision to try loading |
| 83 | * @firmware_name: composite filename of ucode file to load |
| 84 | * @request_firmware_complete: the firmware has been obtained from user space |
| 85 | */ |
| 86 | struct iwl_drv { |
| 87 | struct iwl_fw fw; |
| 88 | |
| 89 | struct iwl_shared *shrd; |
| 90 | struct iwl_op_mode *op_mode; |
| 91 | |
| 92 | int fw_index; /* firmware we're trying to load */ |
| 93 | char firmware_name[25]; /* name of firmware file to load */ |
| 94 | |
| 95 | struct completion request_firmware_complete; |
| 96 | }; |
| 97 | |
| 98 | |
| 99 | |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 100 | /* |
| 101 | * struct fw_sec: Just for the image parsing proccess. |
| 102 | * For the fw storage we are using struct fw_desc. |
| 103 | */ |
| 104 | struct fw_sec { |
| 105 | const void *data; /* the sec data */ |
| 106 | size_t size; /* section size */ |
| 107 | u32 offset; /* offset of writing in the device */ |
| 108 | }; |
| 109 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 110 | static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 111 | { |
| 112 | if (desc->v_addr) |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 113 | dma_free_coherent(trans(drv)->dev, desc->len, |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 114 | desc->v_addr, desc->p_addr); |
| 115 | desc->v_addr = NULL; |
| 116 | desc->len = 0; |
| 117 | } |
| 118 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 119 | static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 120 | { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 121 | iwl_free_fw_desc(drv, &img->code); |
| 122 | iwl_free_fw_desc(drv, &img->data); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 125 | static void iwl_dealloc_ucode(struct iwl_drv *drv) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 126 | { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 127 | iwl_free_fw_img(drv, &drv->fw.ucode_rt); |
| 128 | iwl_free_fw_img(drv, &drv->fw.ucode_init); |
| 129 | iwl_free_fw_img(drv, &drv->fw.ucode_wowlan); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 132 | static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc, |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 133 | struct fw_sec *sec) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 134 | { |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 135 | if (!sec || !sec->size) { |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 136 | desc->v_addr = NULL; |
| 137 | return -EINVAL; |
| 138 | } |
| 139 | |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 140 | desc->v_addr = dma_alloc_coherent(trans(drv)->dev, sec->size, |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 141 | &desc->p_addr, GFP_KERNEL); |
| 142 | if (!desc->v_addr) |
| 143 | return -ENOMEM; |
| 144 | |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 145 | desc->len = sec->size; |
| 146 | desc->offset = sec->offset; |
| 147 | memcpy(desc->v_addr, sec->data, sec->size); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context); |
| 152 | |
| 153 | #define UCODE_EXPERIMENTAL_INDEX 100 |
| 154 | #define UCODE_EXPERIMENTAL_TAG "exp" |
| 155 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 156 | static int iwl_request_firmware(struct iwl_drv *drv, bool first) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 157 | { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 158 | const struct iwl_cfg *cfg = cfg(drv); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 159 | const char *name_pre = cfg->fw_name_pre; |
| 160 | char tag[8]; |
| 161 | |
| 162 | if (first) { |
| 163 | #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 164 | drv->fw_index = UCODE_EXPERIMENTAL_INDEX; |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 165 | strcpy(tag, UCODE_EXPERIMENTAL_TAG); |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 166 | } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) { |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 167 | #endif |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 168 | drv->fw_index = cfg->ucode_api_max; |
| 169 | sprintf(tag, "%d", drv->fw_index); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 170 | } else { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 171 | drv->fw_index--; |
| 172 | sprintf(tag, "%d", drv->fw_index); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 175 | if (drv->fw_index < cfg->ucode_api_min) { |
| 176 | IWL_ERR(drv, "no suitable firmware found!\n"); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 177 | return -ENOENT; |
| 178 | } |
| 179 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 180 | sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode"); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 181 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 182 | IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n", |
| 183 | (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 184 | ? "EXPERIMENTAL " : "", |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 185 | drv->firmware_name); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 186 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 187 | return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name, |
| 188 | trans(drv)->dev, |
| 189 | GFP_KERNEL, drv, iwl_ucode_callback); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 190 | } |
| 191 | |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 192 | /* |
| 193 | * enumeration of ucode section. |
| 194 | * This enumeration is used for legacy tlv style (before 16.0 uCode). |
| 195 | */ |
| 196 | enum iwl_ucode_sec { |
| 197 | IWL_UCODE_SECTION_INST, |
| 198 | IWL_UCODE_SECTION_DATA, |
| 199 | }; |
| 200 | /* |
| 201 | * For 16.0 uCode and above, there is no differentiation between section, |
| 202 | * just an offset to the HW address. |
| 203 | */ |
| 204 | #define UCODE_SECTION_MAX 4 |
| 205 | |
| 206 | struct fw_img_parsing { |
| 207 | struct fw_sec sec[UCODE_SECTION_MAX]; |
| 208 | int sec_counter; |
| 209 | }; |
| 210 | |
| 211 | struct iwl_firmware_pieces { |
| 212 | struct fw_img_parsing img[IWL_UCODE_TYPE_MAX]; |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 213 | |
| 214 | u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr; |
| 215 | u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr; |
| 216 | }; |
| 217 | |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 218 | /* |
| 219 | * These functions are just to extract uCode section data from the pieces |
| 220 | * structure. |
| 221 | */ |
| 222 | static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces, |
| 223 | enum iwl_ucode_type type, |
| 224 | int sec) |
| 225 | { |
| 226 | return &pieces->img[type].sec[sec]; |
| 227 | } |
| 228 | |
| 229 | static void set_sec_data(struct iwl_firmware_pieces *pieces, |
| 230 | enum iwl_ucode_type type, |
| 231 | int sec, |
| 232 | const void *data) |
| 233 | { |
| 234 | pieces->img[type].sec[sec].data = data; |
| 235 | } |
| 236 | |
| 237 | static void set_sec_size(struct iwl_firmware_pieces *pieces, |
| 238 | enum iwl_ucode_type type, |
| 239 | int sec, |
| 240 | size_t size) |
| 241 | { |
| 242 | pieces->img[type].sec[sec].size = size; |
| 243 | } |
| 244 | |
| 245 | static size_t get_sec_size(struct iwl_firmware_pieces *pieces, |
| 246 | enum iwl_ucode_type type, |
| 247 | int sec) |
| 248 | { |
| 249 | return pieces->img[type].sec[sec].size; |
| 250 | } |
| 251 | |
| 252 | static void set_sec_offset(struct iwl_firmware_pieces *pieces, |
| 253 | enum iwl_ucode_type type, |
| 254 | int sec, |
| 255 | u32 offset) |
| 256 | { |
| 257 | pieces->img[type].sec[sec].offset = offset; |
| 258 | } |
| 259 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 260 | static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv, |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 261 | const struct firmware *ucode_raw, |
| 262 | struct iwl_firmware_pieces *pieces) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 263 | { |
| 264 | struct iwl_ucode_header *ucode = (void *)ucode_raw->data; |
| 265 | u32 api_ver, hdr_size, build; |
| 266 | char buildstr[25]; |
| 267 | const u8 *src; |
| 268 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 269 | drv->fw.ucode_ver = le32_to_cpu(ucode->ver); |
| 270 | api_ver = IWL_UCODE_API(drv->fw.ucode_ver); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 271 | |
| 272 | switch (api_ver) { |
| 273 | default: |
| 274 | hdr_size = 28; |
| 275 | if (ucode_raw->size < hdr_size) { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 276 | IWL_ERR(drv, "File size too small!\n"); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 277 | return -EINVAL; |
| 278 | } |
| 279 | build = le32_to_cpu(ucode->u.v2.build); |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 280 | set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, |
| 281 | le32_to_cpu(ucode->u.v2.inst_size)); |
| 282 | set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, |
| 283 | le32_to_cpu(ucode->u.v2.data_size)); |
| 284 | set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, |
| 285 | le32_to_cpu(ucode->u.v2.init_size)); |
| 286 | set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, |
| 287 | le32_to_cpu(ucode->u.v2.init_data_size)); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 288 | src = ucode->u.v2.data; |
| 289 | break; |
| 290 | case 0: |
| 291 | case 1: |
| 292 | case 2: |
| 293 | hdr_size = 24; |
| 294 | if (ucode_raw->size < hdr_size) { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 295 | IWL_ERR(drv, "File size too small!\n"); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 296 | return -EINVAL; |
| 297 | } |
| 298 | build = 0; |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 299 | set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, |
| 300 | le32_to_cpu(ucode->u.v1.inst_size)); |
| 301 | set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, |
| 302 | le32_to_cpu(ucode->u.v1.data_size)); |
| 303 | set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, |
| 304 | le32_to_cpu(ucode->u.v1.init_size)); |
| 305 | set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, |
| 306 | le32_to_cpu(ucode->u.v1.init_data_size)); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 307 | src = ucode->u.v1.data; |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | if (build) |
| 312 | sprintf(buildstr, " build %u%s", build, |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 313 | (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 314 | ? " (EXP)" : ""); |
| 315 | else |
| 316 | buildstr[0] = '\0'; |
| 317 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 318 | snprintf(drv->fw.fw_version, |
| 319 | sizeof(drv->fw.fw_version), |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 320 | "%u.%u.%u.%u%s", |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 321 | IWL_UCODE_MAJOR(drv->fw.ucode_ver), |
| 322 | IWL_UCODE_MINOR(drv->fw.ucode_ver), |
| 323 | IWL_UCODE_API(drv->fw.ucode_ver), |
| 324 | IWL_UCODE_SERIAL(drv->fw.ucode_ver), |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 325 | buildstr); |
| 326 | |
| 327 | /* Verify size of file vs. image size info in file's header */ |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 328 | |
| 329 | if (ucode_raw->size != hdr_size + |
| 330 | get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) + |
| 331 | get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) + |
| 332 | get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) + |
| 333 | get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) { |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 334 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 335 | IWL_ERR(drv, |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 336 | "uCode file size %d does not match expected size\n", |
| 337 | (int)ucode_raw->size); |
| 338 | return -EINVAL; |
| 339 | } |
| 340 | |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 341 | |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 342 | set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src); |
| 343 | src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST); |
| 344 | set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, |
| 345 | IWLAGN_RTC_INST_LOWER_BOUND); |
| 346 | set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src); |
| 347 | src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA); |
| 348 | set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, |
| 349 | IWLAGN_RTC_DATA_LOWER_BOUND); |
| 350 | set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src); |
| 351 | src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST); |
| 352 | set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, |
| 353 | IWLAGN_RTC_INST_LOWER_BOUND); |
| 354 | set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src); |
| 355 | src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA); |
| 356 | set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, |
| 357 | IWLAGN_RTC_DATA_LOWER_BOUND); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 361 | static int iwl_parse_tlv_firmware(struct iwl_drv *drv, |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 362 | const struct firmware *ucode_raw, |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 363 | struct iwl_firmware_pieces *pieces, |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 364 | struct iwl_ucode_capabilities *capa) |
| 365 | { |
| 366 | struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data; |
| 367 | struct iwl_ucode_tlv *tlv; |
| 368 | size_t len = ucode_raw->size; |
| 369 | const u8 *data; |
| 370 | int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative; |
| 371 | int tmp; |
| 372 | u64 alternatives; |
| 373 | u32 tlv_len; |
| 374 | enum iwl_ucode_tlv_type tlv_type; |
| 375 | const u8 *tlv_data; |
| 376 | char buildstr[25]; |
| 377 | u32 build; |
| 378 | |
| 379 | if (len < sizeof(*ucode)) { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 380 | IWL_ERR(drv, "uCode has invalid length: %zd\n", len); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 381 | return -EINVAL; |
| 382 | } |
| 383 | |
| 384 | if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 385 | IWL_ERR(drv, "invalid uCode magic: 0X%x\n", |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 386 | le32_to_cpu(ucode->magic)); |
| 387 | return -EINVAL; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Check which alternatives are present, and "downgrade" |
| 392 | * when the chosen alternative is not present, warning |
| 393 | * the user when that happens. Some files may not have |
| 394 | * any alternatives, so don't warn in that case. |
| 395 | */ |
| 396 | alternatives = le64_to_cpu(ucode->alternatives); |
| 397 | tmp = wanted_alternative; |
| 398 | if (wanted_alternative > 63) |
| 399 | wanted_alternative = 63; |
| 400 | while (wanted_alternative && !(alternatives & BIT(wanted_alternative))) |
| 401 | wanted_alternative--; |
| 402 | if (wanted_alternative && wanted_alternative != tmp) |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 403 | IWL_WARN(drv, |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 404 | "uCode alternative %d not available, choosing %d\n", |
| 405 | tmp, wanted_alternative); |
| 406 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 407 | drv->fw.ucode_ver = le32_to_cpu(ucode->ver); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 408 | build = le32_to_cpu(ucode->build); |
| 409 | |
| 410 | if (build) |
| 411 | sprintf(buildstr, " build %u%s", build, |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 412 | (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 413 | ? " (EXP)" : ""); |
| 414 | else |
| 415 | buildstr[0] = '\0'; |
| 416 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 417 | snprintf(drv->fw.fw_version, |
| 418 | sizeof(drv->fw.fw_version), |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 419 | "%u.%u.%u.%u%s", |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 420 | IWL_UCODE_MAJOR(drv->fw.ucode_ver), |
| 421 | IWL_UCODE_MINOR(drv->fw.ucode_ver), |
| 422 | IWL_UCODE_API(drv->fw.ucode_ver), |
| 423 | IWL_UCODE_SERIAL(drv->fw.ucode_ver), |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 424 | buildstr); |
| 425 | |
| 426 | data = ucode->data; |
| 427 | |
| 428 | len -= sizeof(*ucode); |
| 429 | |
| 430 | while (len >= sizeof(*tlv)) { |
| 431 | u16 tlv_alt; |
| 432 | |
| 433 | len -= sizeof(*tlv); |
| 434 | tlv = (void *)data; |
| 435 | |
| 436 | tlv_len = le32_to_cpu(tlv->length); |
| 437 | tlv_type = le16_to_cpu(tlv->type); |
| 438 | tlv_alt = le16_to_cpu(tlv->alternative); |
| 439 | tlv_data = tlv->data; |
| 440 | |
| 441 | if (len < tlv_len) { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 442 | IWL_ERR(drv, "invalid TLV len: %zd/%u\n", |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 443 | len, tlv_len); |
| 444 | return -EINVAL; |
| 445 | } |
| 446 | len -= ALIGN(tlv_len, 4); |
| 447 | data += sizeof(*tlv) + ALIGN(tlv_len, 4); |
| 448 | |
| 449 | /* |
| 450 | * Alternative 0 is always valid. |
| 451 | * |
| 452 | * Skip alternative TLVs that are not selected. |
| 453 | */ |
| 454 | if (tlv_alt != 0 && tlv_alt != wanted_alternative) |
| 455 | continue; |
| 456 | |
| 457 | switch (tlv_type) { |
| 458 | case IWL_UCODE_TLV_INST: |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 459 | set_sec_data(pieces, IWL_UCODE_REGULAR, |
| 460 | IWL_UCODE_SECTION_INST, tlv_data); |
| 461 | set_sec_size(pieces, IWL_UCODE_REGULAR, |
| 462 | IWL_UCODE_SECTION_INST, tlv_len); |
| 463 | set_sec_offset(pieces, IWL_UCODE_REGULAR, |
| 464 | IWL_UCODE_SECTION_INST, |
| 465 | IWLAGN_RTC_INST_LOWER_BOUND); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 466 | break; |
| 467 | case IWL_UCODE_TLV_DATA: |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 468 | set_sec_data(pieces, IWL_UCODE_REGULAR, |
| 469 | IWL_UCODE_SECTION_DATA, tlv_data); |
| 470 | set_sec_size(pieces, IWL_UCODE_REGULAR, |
| 471 | IWL_UCODE_SECTION_DATA, tlv_len); |
| 472 | set_sec_offset(pieces, IWL_UCODE_REGULAR, |
| 473 | IWL_UCODE_SECTION_DATA, |
| 474 | IWLAGN_RTC_DATA_LOWER_BOUND); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 475 | break; |
| 476 | case IWL_UCODE_TLV_INIT: |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 477 | set_sec_data(pieces, IWL_UCODE_INIT, |
| 478 | IWL_UCODE_SECTION_INST, tlv_data); |
| 479 | set_sec_size(pieces, IWL_UCODE_INIT, |
| 480 | IWL_UCODE_SECTION_INST, tlv_len); |
| 481 | set_sec_offset(pieces, IWL_UCODE_INIT, |
| 482 | IWL_UCODE_SECTION_INST, |
| 483 | IWLAGN_RTC_INST_LOWER_BOUND); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 484 | break; |
| 485 | case IWL_UCODE_TLV_INIT_DATA: |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 486 | set_sec_data(pieces, IWL_UCODE_INIT, |
| 487 | IWL_UCODE_SECTION_DATA, tlv_data); |
| 488 | set_sec_size(pieces, IWL_UCODE_INIT, |
| 489 | IWL_UCODE_SECTION_DATA, tlv_len); |
| 490 | set_sec_offset(pieces, IWL_UCODE_INIT, |
| 491 | IWL_UCODE_SECTION_DATA, |
| 492 | IWLAGN_RTC_DATA_LOWER_BOUND); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 493 | break; |
| 494 | case IWL_UCODE_TLV_BOOT: |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 495 | IWL_ERR(drv, "Found unexpected BOOT ucode\n"); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 496 | break; |
| 497 | case IWL_UCODE_TLV_PROBE_MAX_LEN: |
| 498 | if (tlv_len != sizeof(u32)) |
| 499 | goto invalid_tlv_len; |
| 500 | capa->max_probe_length = |
| 501 | le32_to_cpup((__le32 *)tlv_data); |
| 502 | break; |
| 503 | case IWL_UCODE_TLV_PAN: |
| 504 | if (tlv_len) |
| 505 | goto invalid_tlv_len; |
| 506 | capa->flags |= IWL_UCODE_TLV_FLAGS_PAN; |
| 507 | break; |
| 508 | case IWL_UCODE_TLV_FLAGS: |
| 509 | /* must be at least one u32 */ |
| 510 | if (tlv_len < sizeof(u32)) |
| 511 | goto invalid_tlv_len; |
| 512 | /* and a proper number of u32s */ |
| 513 | if (tlv_len % sizeof(u32)) |
| 514 | goto invalid_tlv_len; |
| 515 | /* |
| 516 | * This driver only reads the first u32 as |
| 517 | * right now no more features are defined, |
| 518 | * if that changes then either the driver |
| 519 | * will not work with the new firmware, or |
| 520 | * it'll not take advantage of new features. |
| 521 | */ |
| 522 | capa->flags = le32_to_cpup((__le32 *)tlv_data); |
| 523 | break; |
| 524 | case IWL_UCODE_TLV_INIT_EVTLOG_PTR: |
| 525 | if (tlv_len != sizeof(u32)) |
| 526 | goto invalid_tlv_len; |
| 527 | pieces->init_evtlog_ptr = |
| 528 | le32_to_cpup((__le32 *)tlv_data); |
| 529 | break; |
| 530 | case IWL_UCODE_TLV_INIT_EVTLOG_SIZE: |
| 531 | if (tlv_len != sizeof(u32)) |
| 532 | goto invalid_tlv_len; |
| 533 | pieces->init_evtlog_size = |
| 534 | le32_to_cpup((__le32 *)tlv_data); |
| 535 | break; |
| 536 | case IWL_UCODE_TLV_INIT_ERRLOG_PTR: |
| 537 | if (tlv_len != sizeof(u32)) |
| 538 | goto invalid_tlv_len; |
| 539 | pieces->init_errlog_ptr = |
| 540 | le32_to_cpup((__le32 *)tlv_data); |
| 541 | break; |
| 542 | case IWL_UCODE_TLV_RUNT_EVTLOG_PTR: |
| 543 | if (tlv_len != sizeof(u32)) |
| 544 | goto invalid_tlv_len; |
| 545 | pieces->inst_evtlog_ptr = |
| 546 | le32_to_cpup((__le32 *)tlv_data); |
| 547 | break; |
| 548 | case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE: |
| 549 | if (tlv_len != sizeof(u32)) |
| 550 | goto invalid_tlv_len; |
| 551 | pieces->inst_evtlog_size = |
| 552 | le32_to_cpup((__le32 *)tlv_data); |
| 553 | break; |
| 554 | case IWL_UCODE_TLV_RUNT_ERRLOG_PTR: |
| 555 | if (tlv_len != sizeof(u32)) |
| 556 | goto invalid_tlv_len; |
| 557 | pieces->inst_errlog_ptr = |
| 558 | le32_to_cpup((__le32 *)tlv_data); |
| 559 | break; |
| 560 | case IWL_UCODE_TLV_ENHANCE_SENS_TBL: |
| 561 | if (tlv_len) |
| 562 | goto invalid_tlv_len; |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 563 | drv->fw.enhance_sensitivity_table = true; |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 564 | break; |
| 565 | case IWL_UCODE_TLV_WOWLAN_INST: |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 566 | set_sec_data(pieces, IWL_UCODE_WOWLAN, |
| 567 | IWL_UCODE_SECTION_INST, tlv_data); |
| 568 | set_sec_size(pieces, IWL_UCODE_WOWLAN, |
| 569 | IWL_UCODE_SECTION_INST, tlv_len); |
| 570 | set_sec_offset(pieces, IWL_UCODE_WOWLAN, |
| 571 | IWL_UCODE_SECTION_INST, |
| 572 | IWLAGN_RTC_INST_LOWER_BOUND); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 573 | break; |
| 574 | case IWL_UCODE_TLV_WOWLAN_DATA: |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 575 | set_sec_data(pieces, IWL_UCODE_WOWLAN, |
| 576 | IWL_UCODE_SECTION_DATA, tlv_data); |
| 577 | set_sec_size(pieces, IWL_UCODE_WOWLAN, |
| 578 | IWL_UCODE_SECTION_DATA, tlv_len); |
| 579 | set_sec_offset(pieces, IWL_UCODE_WOWLAN, |
| 580 | IWL_UCODE_SECTION_DATA, |
| 581 | IWLAGN_RTC_DATA_LOWER_BOUND); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 582 | break; |
| 583 | case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE: |
| 584 | if (tlv_len != sizeof(u32)) |
| 585 | goto invalid_tlv_len; |
| 586 | capa->standard_phy_calibration_size = |
| 587 | le32_to_cpup((__le32 *)tlv_data); |
| 588 | break; |
| 589 | default: |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 590 | IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | if (len) { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 596 | IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len); |
| 597 | iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 598 | return -EINVAL; |
| 599 | } |
| 600 | |
| 601 | return 0; |
| 602 | |
| 603 | invalid_tlv_len: |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 604 | IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len); |
| 605 | iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 606 | |
| 607 | return -EINVAL; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * iwl_ucode_callback - callback when firmware was loaded |
| 612 | * |
| 613 | * If loaded successfully, copies the firmware into buffers |
| 614 | * for the card to fetch (via DMA). |
| 615 | */ |
| 616 | static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context) |
| 617 | { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 618 | struct iwl_drv *drv = context; |
| 619 | const struct iwl_cfg *cfg = cfg(drv); |
| 620 | struct iwl_fw *fw = &drv->fw; |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 621 | struct iwl_ucode_header *ucode; |
| 622 | int err; |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 623 | struct iwl_firmware_pieces pieces; |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 624 | const unsigned int api_max = cfg->ucode_api_max; |
| 625 | unsigned int api_ok = cfg->ucode_api_ok; |
| 626 | const unsigned int api_min = cfg->ucode_api_min; |
| 627 | u32 api_ver; |
| 628 | |
| 629 | fw->ucode_capa.max_probe_length = 200; |
| 630 | fw->ucode_capa.standard_phy_calibration_size = |
| 631 | IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE; |
| 632 | |
| 633 | if (!api_ok) |
| 634 | api_ok = api_max; |
| 635 | |
| 636 | memset(&pieces, 0, sizeof(pieces)); |
| 637 | |
| 638 | if (!ucode_raw) { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 639 | if (drv->fw_index <= api_ok) |
| 640 | IWL_ERR(drv, |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 641 | "request for firmware file '%s' failed.\n", |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 642 | drv->firmware_name); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 643 | goto try_again; |
| 644 | } |
| 645 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 646 | IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n", |
| 647 | drv->firmware_name, ucode_raw->size); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 648 | |
| 649 | /* Make sure that we got at least the API version number */ |
| 650 | if (ucode_raw->size < 4) { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 651 | IWL_ERR(drv, "File size way too small!\n"); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 652 | goto try_again; |
| 653 | } |
| 654 | |
| 655 | /* Data from ucode file: header followed by uCode images */ |
| 656 | ucode = (struct iwl_ucode_header *)ucode_raw->data; |
| 657 | |
| 658 | if (ucode->ver) |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 659 | err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 660 | else |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 661 | err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces, |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 662 | &fw->ucode_capa); |
| 663 | |
| 664 | if (err) |
| 665 | goto try_again; |
| 666 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 667 | api_ver = IWL_UCODE_API(drv->fw.ucode_ver); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 668 | |
| 669 | /* |
| 670 | * api_ver should match the api version forming part of the |
| 671 | * firmware filename ... but we don't check for that and only rely |
| 672 | * on the API version read from firmware header from here on forward |
| 673 | */ |
| 674 | /* no api version check required for experimental uCode */ |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 675 | if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) { |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 676 | if (api_ver < api_min || api_ver > api_max) { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 677 | IWL_ERR(drv, |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 678 | "Driver unable to support your firmware API. " |
| 679 | "Driver supports v%u, firmware is v%u.\n", |
| 680 | api_max, api_ver); |
| 681 | goto try_again; |
| 682 | } |
| 683 | |
| 684 | if (api_ver < api_ok) { |
| 685 | if (api_ok != api_max) |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 686 | IWL_ERR(drv, "Firmware has old API version, " |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 687 | "expected v%u through v%u, got v%u.\n", |
| 688 | api_ok, api_max, api_ver); |
| 689 | else |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 690 | IWL_ERR(drv, "Firmware has old API version, " |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 691 | "expected v%u, got v%u.\n", |
| 692 | api_max, api_ver); |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 693 | IWL_ERR(drv, "New firmware can be obtained from " |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 694 | "http://www.intellinuxwireless.org/.\n"); |
| 695 | } |
| 696 | } |
| 697 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 698 | IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 699 | |
| 700 | /* |
| 701 | * For any of the failures below (before allocating pci memory) |
| 702 | * we will try to load a version with a smaller API -- maybe the |
| 703 | * user just got a corrupted version of the latest API. |
| 704 | */ |
| 705 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 706 | IWL_DEBUG_INFO(drv, "f/w package hdr ucode version raw = 0x%x\n", |
| 707 | drv->fw.ucode_ver); |
| 708 | IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n", |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 709 | get_sec_size(&pieces, IWL_UCODE_REGULAR, |
| 710 | IWL_UCODE_SECTION_INST)); |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 711 | IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n", |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 712 | get_sec_size(&pieces, IWL_UCODE_REGULAR, |
| 713 | IWL_UCODE_SECTION_DATA)); |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 714 | IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n", |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 715 | get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST)); |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 716 | IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n", |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 717 | get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 718 | |
| 719 | /* Verify that uCode images will fit in card's SRAM */ |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 720 | if (get_sec_size(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) > |
| 721 | cfg->max_inst_size) { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 722 | IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n", |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 723 | get_sec_size(&pieces, IWL_UCODE_REGULAR, |
| 724 | IWL_UCODE_SECTION_INST)); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 725 | goto try_again; |
| 726 | } |
| 727 | |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 728 | if (get_sec_size(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) > |
| 729 | cfg->max_data_size) { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 730 | IWL_ERR(drv, "uCode data len %Zd too large to fit in\n", |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 731 | get_sec_size(&pieces, IWL_UCODE_REGULAR, |
| 732 | IWL_UCODE_SECTION_DATA)); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 733 | goto try_again; |
| 734 | } |
| 735 | |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 736 | if (get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) > |
| 737 | cfg->max_inst_size) { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 738 | IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n", |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 739 | get_sec_size(&pieces, IWL_UCODE_INIT, |
| 740 | IWL_UCODE_SECTION_INST)); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 741 | goto try_again; |
| 742 | } |
| 743 | |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 744 | if (get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) > |
| 745 | cfg->max_data_size) { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 746 | IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n", |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 747 | get_sec_size(&pieces, IWL_UCODE_REGULAR, |
| 748 | IWL_UCODE_SECTION_DATA)); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 749 | goto try_again; |
| 750 | } |
| 751 | |
| 752 | /* Allocate ucode buffers for card's bus-master loading ... */ |
| 753 | |
| 754 | /* Runtime instructions and 2 copies of data: |
| 755 | * 1) unmodified from disk |
| 756 | * 2) backup cache for save/restore during power-downs */ |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 757 | if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_rt.code, |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 758 | get_sec(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST))) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 759 | goto err_pci_alloc; |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 760 | if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_rt.data, |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 761 | get_sec(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA))) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 762 | goto err_pci_alloc; |
| 763 | |
| 764 | /* Initialization instructions and data */ |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 765 | if (get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) && |
| 766 | get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) { |
| 767 | if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_init.code, |
| 768 | get_sec(&pieces, IWL_UCODE_INIT, |
| 769 | IWL_UCODE_SECTION_INST))) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 770 | goto err_pci_alloc; |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 771 | if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_init.data, |
| 772 | get_sec(&pieces, IWL_UCODE_INIT, |
| 773 | IWL_UCODE_SECTION_DATA))) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 774 | goto err_pci_alloc; |
| 775 | } |
| 776 | |
| 777 | /* WoWLAN instructions and data */ |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 778 | if (get_sec_size(&pieces, IWL_UCODE_WOWLAN, IWL_UCODE_SECTION_INST) && |
| 779 | get_sec_size(&pieces, IWL_UCODE_WOWLAN, IWL_UCODE_SECTION_DATA)) { |
| 780 | if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_wowlan.code, |
| 781 | get_sec(&pieces, IWL_UCODE_WOWLAN, |
| 782 | IWL_UCODE_SECTION_INST))) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 783 | goto err_pci_alloc; |
David Spinadel | 0cedacc | 2012-03-10 13:00:12 -0800 | [diff] [blame^] | 784 | if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_wowlan.data, |
| 785 | get_sec(&pieces, IWL_UCODE_WOWLAN, |
| 786 | IWL_UCODE_SECTION_DATA))) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 787 | goto err_pci_alloc; |
| 788 | } |
| 789 | |
| 790 | /* Now that we can no longer fail, copy information */ |
| 791 | |
| 792 | /* |
| 793 | * The (size - 16) / 12 formula is based on the information recorded |
| 794 | * for each event, which is of mode 1 (including timestamp) for all |
| 795 | * new microcodes that include this information. |
| 796 | */ |
Johannes Berg | 0692fe4 | 2012-03-06 13:30:37 -0800 | [diff] [blame] | 797 | fw->init_evtlog_ptr = pieces.init_evtlog_ptr; |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 798 | if (pieces.init_evtlog_size) |
Johannes Berg | 0692fe4 | 2012-03-06 13:30:37 -0800 | [diff] [blame] | 799 | fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12; |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 800 | else |
Johannes Berg | 0692fe4 | 2012-03-06 13:30:37 -0800 | [diff] [blame] | 801 | fw->init_evtlog_size = |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 802 | cfg->base_params->max_event_log_size; |
Johannes Berg | 0692fe4 | 2012-03-06 13:30:37 -0800 | [diff] [blame] | 803 | fw->init_errlog_ptr = pieces.init_errlog_ptr; |
| 804 | fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr; |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 805 | if (pieces.inst_evtlog_size) |
Johannes Berg | 0692fe4 | 2012-03-06 13:30:37 -0800 | [diff] [blame] | 806 | fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12; |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 807 | else |
Johannes Berg | 0692fe4 | 2012-03-06 13:30:37 -0800 | [diff] [blame] | 808 | fw->inst_evtlog_size = |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 809 | cfg->base_params->max_event_log_size; |
Johannes Berg | 0692fe4 | 2012-03-06 13:30:37 -0800 | [diff] [blame] | 810 | fw->inst_errlog_ptr = pieces.inst_errlog_ptr; |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 811 | |
| 812 | /* |
| 813 | * figure out the offset of chain noise reset and gain commands |
| 814 | * base on the size of standard phy calibration commands table size |
| 815 | */ |
| 816 | if (fw->ucode_capa.standard_phy_calibration_size > |
| 817 | IWL_MAX_PHY_CALIBRATE_TBL_SIZE) |
| 818 | fw->ucode_capa.standard_phy_calibration_size = |
| 819 | IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE; |
| 820 | |
| 821 | /* We have our copies now, allow OS release its copies */ |
| 822 | release_firmware(ucode_raw); |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 823 | complete(&drv->request_firmware_complete); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 824 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 825 | drv->op_mode = iwl_dvm_ops.start(drv->shrd->trans, &drv->fw); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 826 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 827 | if (!drv->op_mode) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 828 | goto out_unbind; |
| 829 | |
| 830 | return; |
| 831 | |
| 832 | try_again: |
| 833 | /* try next, if any */ |
| 834 | release_firmware(ucode_raw); |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 835 | if (iwl_request_firmware(drv, false)) |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 836 | goto out_unbind; |
| 837 | return; |
| 838 | |
| 839 | err_pci_alloc: |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 840 | IWL_ERR(drv, "failed to allocate pci memory\n"); |
| 841 | iwl_dealloc_ucode(drv); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 842 | release_firmware(ucode_raw); |
| 843 | out_unbind: |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 844 | complete(&drv->request_firmware_complete); |
| 845 | device_release_driver(trans(drv)->dev); |
Johannes Berg | 15854ef | 2012-03-05 11:24:50 -0800 | [diff] [blame] | 846 | } |
| 847 | |
Emmanuel Grumbach | 5c58edc | 2012-02-07 14:18:40 +0200 | [diff] [blame] | 848 | int iwl_drv_start(struct iwl_shared *shrd, |
Johannes Berg | 706c4ff | 2012-03-05 11:24:33 -0800 | [diff] [blame] | 849 | struct iwl_trans *trans, const struct iwl_cfg *cfg) |
Emmanuel Grumbach | 5c58edc | 2012-02-07 14:18:40 +0200 | [diff] [blame] | 850 | { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 851 | struct iwl_drv *drv; |
Emmanuel Grumbach | 5c58edc | 2012-02-07 14:18:40 +0200 | [diff] [blame] | 852 | int ret; |
| 853 | |
| 854 | shrd->cfg = cfg; |
| 855 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 856 | drv = kzalloc(sizeof(*drv), GFP_KERNEL); |
| 857 | if (!drv) { |
| 858 | dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_drv"); |
Emmanuel Grumbach | 5c58edc | 2012-02-07 14:18:40 +0200 | [diff] [blame] | 859 | return -ENOMEM; |
| 860 | } |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 861 | drv->shrd = shrd; |
| 862 | shrd->drv = drv; |
Emmanuel Grumbach | 5c58edc | 2012-02-07 14:18:40 +0200 | [diff] [blame] | 863 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 864 | init_completion(&drv->request_firmware_complete); |
Emmanuel Grumbach | 5c58edc | 2012-02-07 14:18:40 +0200 | [diff] [blame] | 865 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 866 | ret = iwl_request_firmware(drv, true); |
Emmanuel Grumbach | 5c58edc | 2012-02-07 14:18:40 +0200 | [diff] [blame] | 867 | |
| 868 | if (ret) { |
| 869 | dev_printk(KERN_ERR, trans->dev, "Couldn't request the fw"); |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 870 | kfree(drv); |
| 871 | shrd->drv = NULL; |
Emmanuel Grumbach | 5c58edc | 2012-02-07 14:18:40 +0200 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | return ret; |
| 875 | } |
| 876 | |
Emmanuel Grumbach | 07590f0 | 2012-02-07 14:27:31 +0200 | [diff] [blame] | 877 | void iwl_drv_stop(struct iwl_shared *shrd) |
| 878 | { |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 879 | struct iwl_drv *drv = shrd->drv; |
Johannes Berg | 0692fe4 | 2012-03-06 13:30:37 -0800 | [diff] [blame] | 880 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 881 | wait_for_completion(&drv->request_firmware_complete); |
Johannes Berg | 2e7eb11 | 2012-03-05 11:24:51 -0800 | [diff] [blame] | 882 | |
Emmanuel Grumbach | d0f76d6 | 2012-02-09 16:08:15 +0200 | [diff] [blame] | 883 | /* op_mode can be NULL if its start failed */ |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 884 | if (drv->op_mode) |
| 885 | iwl_op_mode_stop(drv->op_mode); |
Emmanuel Grumbach | 07590f0 | 2012-02-07 14:27:31 +0200 | [diff] [blame] | 886 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 887 | iwl_dealloc_ucode(drv); |
Johannes Berg | 7db5b98 | 2012-03-05 11:24:48 -0800 | [diff] [blame] | 888 | |
Johannes Berg | 965974a | 2012-03-06 13:30:38 -0800 | [diff] [blame] | 889 | kfree(drv); |
| 890 | shrd->drv = NULL; |
Emmanuel Grumbach | 07590f0 | 2012-02-07 14:27:31 +0200 | [diff] [blame] | 891 | } |