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