blob: a21af3df6c8ac3a442a66981a565af5567c4827b [file] [log] [blame]
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001/******************************************************************************
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 Berg15854ef2012-03-05 11:24:50 -080064#include <linux/dma-mapping.h>
65#include <linux/firmware.h>
66#include <linux/module.h>
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020067
68#include "iwl-drv.h"
69#include "iwl-trans.h"
Johannes Berg15854ef2012-03-05 11:24:50 -080070#include "iwl-shared.h"
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +020071#include "iwl-op-mode.h"
David Spinadel0cedacc2012-03-10 13:00:12 -080072#include "iwl-agn-hw.h"
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020073
Johannes Berg0692fe42012-03-06 13:30:37 -080074/* private includes */
Johannes Berg3995dea2012-03-06 13:30:44 -080075#include "iwl-fw-file.h"
Johannes Berg0692fe42012-03-06 13:30:37 -080076
Johannes Berg965974a2012-03-06 13:30:38 -080077/**
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 */
86struct 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 Spinadel0cedacc2012-03-10 13:00:12 -0800100/*
101 * struct fw_sec: Just for the image parsing proccess.
102 * For the fw storage we are using struct fw_desc.
103 */
104struct 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 Berg965974a2012-03-06 13:30:38 -0800110static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
Johannes Berg15854ef2012-03-05 11:24:50 -0800111{
112 if (desc->v_addr)
Johannes Berg965974a2012-03-06 13:30:38 -0800113 dma_free_coherent(trans(drv)->dev, desc->len,
Johannes Berg15854ef2012-03-05 11:24:50 -0800114 desc->v_addr, desc->p_addr);
115 desc->v_addr = NULL;
116 desc->len = 0;
117}
118
Johannes Berg965974a2012-03-06 13:30:38 -0800119static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
Johannes Berg15854ef2012-03-05 11:24:50 -0800120{
Johannes Berg965974a2012-03-06 13:30:38 -0800121 iwl_free_fw_desc(drv, &img->code);
122 iwl_free_fw_desc(drv, &img->data);
Johannes Berg15854ef2012-03-05 11:24:50 -0800123}
124
Johannes Berg965974a2012-03-06 13:30:38 -0800125static void iwl_dealloc_ucode(struct iwl_drv *drv)
Johannes Berg15854ef2012-03-05 11:24:50 -0800126{
Johannes Berg965974a2012-03-06 13:30:38 -0800127 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 Berg15854ef2012-03-05 11:24:50 -0800130}
131
Johannes Berg965974a2012-03-06 13:30:38 -0800132static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
David Spinadel0cedacc2012-03-10 13:00:12 -0800133 struct fw_sec *sec)
Johannes Berg15854ef2012-03-05 11:24:50 -0800134{
David Spinadel0cedacc2012-03-10 13:00:12 -0800135 if (!sec || !sec->size) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800136 desc->v_addr = NULL;
137 return -EINVAL;
138 }
139
David Spinadel0cedacc2012-03-10 13:00:12 -0800140 desc->v_addr = dma_alloc_coherent(trans(drv)->dev, sec->size,
Johannes Berg15854ef2012-03-05 11:24:50 -0800141 &desc->p_addr, GFP_KERNEL);
142 if (!desc->v_addr)
143 return -ENOMEM;
144
David Spinadel0cedacc2012-03-10 13:00:12 -0800145 desc->len = sec->size;
146 desc->offset = sec->offset;
147 memcpy(desc->v_addr, sec->data, sec->size);
Johannes Berg15854ef2012-03-05 11:24:50 -0800148 return 0;
149}
150
151static 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 Berg965974a2012-03-06 13:30:38 -0800156static int iwl_request_firmware(struct iwl_drv *drv, bool first)
Johannes Berg15854ef2012-03-05 11:24:50 -0800157{
Johannes Berg965974a2012-03-06 13:30:38 -0800158 const struct iwl_cfg *cfg = cfg(drv);
Johannes Berg15854ef2012-03-05 11:24:50 -0800159 const char *name_pre = cfg->fw_name_pre;
160 char tag[8];
161
162 if (first) {
163#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
Johannes Berg965974a2012-03-06 13:30:38 -0800164 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
Johannes Berg15854ef2012-03-05 11:24:50 -0800165 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
Johannes Berg965974a2012-03-06 13:30:38 -0800166 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800167#endif
Johannes Berg965974a2012-03-06 13:30:38 -0800168 drv->fw_index = cfg->ucode_api_max;
169 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800170 } else {
Johannes Berg965974a2012-03-06 13:30:38 -0800171 drv->fw_index--;
172 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800173 }
174
Johannes Berg965974a2012-03-06 13:30:38 -0800175 if (drv->fw_index < cfg->ucode_api_min) {
176 IWL_ERR(drv, "no suitable firmware found!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800177 return -ENOENT;
178 }
179
Johannes Berg965974a2012-03-06 13:30:38 -0800180 sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Johannes Berg15854ef2012-03-05 11:24:50 -0800181
Johannes Berg965974a2012-03-06 13:30:38 -0800182 IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
183 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800184 ? "EXPERIMENTAL " : "",
Johannes Berg965974a2012-03-06 13:30:38 -0800185 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800186
Johannes Berg965974a2012-03-06 13:30:38 -0800187 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
188 trans(drv)->dev,
189 GFP_KERNEL, drv, iwl_ucode_callback);
Johannes Berg15854ef2012-03-05 11:24:50 -0800190}
191
David Spinadel0cedacc2012-03-10 13:00:12 -0800192/*
193 * enumeration of ucode section.
194 * This enumeration is used for legacy tlv style (before 16.0 uCode).
195 */
196enum 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
206struct fw_img_parsing {
207 struct fw_sec sec[UCODE_SECTION_MAX];
208 int sec_counter;
209};
210
211struct iwl_firmware_pieces {
212 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
Johannes Berg15854ef2012-03-05 11:24:50 -0800213
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 Spinadel0cedacc2012-03-10 13:00:12 -0800218/*
219 * These functions are just to extract uCode section data from the pieces
220 * structure.
221 */
222static 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
229static 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
237static 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
245static 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
252static 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 Berg965974a2012-03-06 13:30:38 -0800260static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
David Spinadel0cedacc2012-03-10 13:00:12 -0800261 const struct firmware *ucode_raw,
262 struct iwl_firmware_pieces *pieces)
Johannes Berg15854ef2012-03-05 11:24:50 -0800263{
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 Berg965974a2012-03-06 13:30:38 -0800269 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
270 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800271
272 switch (api_ver) {
273 default:
274 hdr_size = 28;
275 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800276 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800277 return -EINVAL;
278 }
279 build = le32_to_cpu(ucode->u.v2.build);
David Spinadel0cedacc2012-03-10 13:00:12 -0800280 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 Berg15854ef2012-03-05 11:24:50 -0800288 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 Berg965974a2012-03-06 13:30:38 -0800295 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800296 return -EINVAL;
297 }
298 build = 0;
David Spinadel0cedacc2012-03-10 13:00:12 -0800299 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 Berg15854ef2012-03-05 11:24:50 -0800307 src = ucode->u.v1.data;
308 break;
309 }
310
311 if (build)
312 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800313 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800314 ? " (EXP)" : "");
315 else
316 buildstr[0] = '\0';
317
Johannes Berg965974a2012-03-06 13:30:38 -0800318 snprintf(drv->fw.fw_version,
319 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800320 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800321 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 Berg15854ef2012-03-05 11:24:50 -0800325 buildstr);
326
327 /* Verify size of file vs. image size info in file's header */
David Spinadel0cedacc2012-03-10 13:00:12 -0800328
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 Berg15854ef2012-03-05 11:24:50 -0800334
Johannes Berg965974a2012-03-06 13:30:38 -0800335 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800336 "uCode file size %d does not match expected size\n",
337 (int)ucode_raw->size);
338 return -EINVAL;
339 }
340
Johannes Berg15854ef2012-03-05 11:24:50 -0800341
David Spinadel0cedacc2012-03-10 13:00:12 -0800342 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 Berg15854ef2012-03-05 11:24:50 -0800358 return 0;
359}
360
Johannes Berg965974a2012-03-06 13:30:38 -0800361static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800362 const struct firmware *ucode_raw,
David Spinadel0cedacc2012-03-10 13:00:12 -0800363 struct iwl_firmware_pieces *pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -0800364 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 Berg965974a2012-03-06 13:30:38 -0800380 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800381 return -EINVAL;
382 }
383
384 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800385 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800386 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 Berg965974a2012-03-06 13:30:38 -0800403 IWL_WARN(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800404 "uCode alternative %d not available, choosing %d\n",
405 tmp, wanted_alternative);
406
Johannes Berg965974a2012-03-06 13:30:38 -0800407 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800408 build = le32_to_cpu(ucode->build);
409
410 if (build)
411 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800412 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800413 ? " (EXP)" : "");
414 else
415 buildstr[0] = '\0';
416
Johannes Berg965974a2012-03-06 13:30:38 -0800417 snprintf(drv->fw.fw_version,
418 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800419 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800420 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 Berg15854ef2012-03-05 11:24:50 -0800424 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 Berg965974a2012-03-06 13:30:38 -0800442 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800443 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 Spinadel0cedacc2012-03-10 13:00:12 -0800459 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 Berg15854ef2012-03-05 11:24:50 -0800466 break;
467 case IWL_UCODE_TLV_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800468 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 Berg15854ef2012-03-05 11:24:50 -0800475 break;
476 case IWL_UCODE_TLV_INIT:
David Spinadel0cedacc2012-03-10 13:00:12 -0800477 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 Berg15854ef2012-03-05 11:24:50 -0800484 break;
485 case IWL_UCODE_TLV_INIT_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800486 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 Berg15854ef2012-03-05 11:24:50 -0800493 break;
494 case IWL_UCODE_TLV_BOOT:
Johannes Berg965974a2012-03-06 13:30:38 -0800495 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800496 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 Berg965974a2012-03-06 13:30:38 -0800563 drv->fw.enhance_sensitivity_table = true;
Johannes Berg15854ef2012-03-05 11:24:50 -0800564 break;
565 case IWL_UCODE_TLV_WOWLAN_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800566 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 Berg15854ef2012-03-05 11:24:50 -0800573 break;
574 case IWL_UCODE_TLV_WOWLAN_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800575 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 Berg15854ef2012-03-05 11:24:50 -0800582 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 Berg965974a2012-03-06 13:30:38 -0800590 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
Johannes Berg15854ef2012-03-05 11:24:50 -0800591 break;
592 }
593 }
594
595 if (len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800596 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
597 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800598 return -EINVAL;
599 }
600
601 return 0;
602
603 invalid_tlv_len:
Johannes Berg965974a2012-03-06 13:30:38 -0800604 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 Berg15854ef2012-03-05 11:24:50 -0800606
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 */
616static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
617{
Johannes Berg965974a2012-03-06 13:30:38 -0800618 struct iwl_drv *drv = context;
619 const struct iwl_cfg *cfg = cfg(drv);
620 struct iwl_fw *fw = &drv->fw;
Johannes Berg15854ef2012-03-05 11:24:50 -0800621 struct iwl_ucode_header *ucode;
622 int err;
David Spinadel0cedacc2012-03-10 13:00:12 -0800623 struct iwl_firmware_pieces pieces;
Johannes Berg15854ef2012-03-05 11:24:50 -0800624 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 Berg965974a2012-03-06 13:30:38 -0800639 if (drv->fw_index <= api_ok)
640 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800641 "request for firmware file '%s' failed.\n",
Johannes Berg965974a2012-03-06 13:30:38 -0800642 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800643 goto try_again;
644 }
645
Johannes Berg965974a2012-03-06 13:30:38 -0800646 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
647 drv->firmware_name, ucode_raw->size);
Johannes Berg15854ef2012-03-05 11:24:50 -0800648
649 /* Make sure that we got at least the API version number */
650 if (ucode_raw->size < 4) {
Johannes Berg965974a2012-03-06 13:30:38 -0800651 IWL_ERR(drv, "File size way too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800652 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 Berg965974a2012-03-06 13:30:38 -0800659 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
Johannes Berg15854ef2012-03-05 11:24:50 -0800660 else
Johannes Berg965974a2012-03-06 13:30:38 -0800661 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -0800662 &fw->ucode_capa);
663
664 if (err)
665 goto try_again;
666
Johannes Berg965974a2012-03-06 13:30:38 -0800667 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800668
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 Berg965974a2012-03-06 13:30:38 -0800675 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800676 if (api_ver < api_min || api_ver > api_max) {
Johannes Berg965974a2012-03-06 13:30:38 -0800677 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800678 "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 Berg965974a2012-03-06 13:30:38 -0800686 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -0800687 "expected v%u through v%u, got v%u.\n",
688 api_ok, api_max, api_ver);
689 else
Johannes Berg965974a2012-03-06 13:30:38 -0800690 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -0800691 "expected v%u, got v%u.\n",
692 api_max, api_ver);
Johannes Berg965974a2012-03-06 13:30:38 -0800693 IWL_ERR(drv, "New firmware can be obtained from "
Johannes Berg15854ef2012-03-05 11:24:50 -0800694 "http://www.intellinuxwireless.org/.\n");
695 }
696 }
697
Johannes Berg965974a2012-03-06 13:30:38 -0800698 IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
Johannes Berg15854ef2012-03-05 11:24:50 -0800699
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 Berg965974a2012-03-06 13:30:38 -0800706 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 Spinadel0cedacc2012-03-10 13:00:12 -0800709 get_sec_size(&pieces, IWL_UCODE_REGULAR,
710 IWL_UCODE_SECTION_INST));
Johannes Berg965974a2012-03-06 13:30:38 -0800711 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800712 get_sec_size(&pieces, IWL_UCODE_REGULAR,
713 IWL_UCODE_SECTION_DATA));
Johannes Berg965974a2012-03-06 13:30:38 -0800714 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800715 get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
Johannes Berg965974a2012-03-06 13:30:38 -0800716 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800717 get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
Johannes Berg15854ef2012-03-05 11:24:50 -0800718
719 /* Verify that uCode images will fit in card's SRAM */
David Spinadel0cedacc2012-03-10 13:00:12 -0800720 if (get_sec_size(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
721 cfg->max_inst_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800722 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800723 get_sec_size(&pieces, IWL_UCODE_REGULAR,
724 IWL_UCODE_SECTION_INST));
Johannes Berg15854ef2012-03-05 11:24:50 -0800725 goto try_again;
726 }
727
David Spinadel0cedacc2012-03-10 13:00:12 -0800728 if (get_sec_size(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
729 cfg->max_data_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800730 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800731 get_sec_size(&pieces, IWL_UCODE_REGULAR,
732 IWL_UCODE_SECTION_DATA));
Johannes Berg15854ef2012-03-05 11:24:50 -0800733 goto try_again;
734 }
735
David Spinadel0cedacc2012-03-10 13:00:12 -0800736 if (get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
737 cfg->max_inst_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800738 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800739 get_sec_size(&pieces, IWL_UCODE_INIT,
740 IWL_UCODE_SECTION_INST));
Johannes Berg15854ef2012-03-05 11:24:50 -0800741 goto try_again;
742 }
743
David Spinadel0cedacc2012-03-10 13:00:12 -0800744 if (get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
745 cfg->max_data_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800746 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800747 get_sec_size(&pieces, IWL_UCODE_REGULAR,
748 IWL_UCODE_SECTION_DATA));
Johannes Berg15854ef2012-03-05 11:24:50 -0800749 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 Berg965974a2012-03-06 13:30:38 -0800757 if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_rt.code,
David Spinadel0cedacc2012-03-10 13:00:12 -0800758 get_sec(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST)))
Johannes Berg15854ef2012-03-05 11:24:50 -0800759 goto err_pci_alloc;
Johannes Berg965974a2012-03-06 13:30:38 -0800760 if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_rt.data,
David Spinadel0cedacc2012-03-10 13:00:12 -0800761 get_sec(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA)))
Johannes Berg15854ef2012-03-05 11:24:50 -0800762 goto err_pci_alloc;
763
764 /* Initialization instructions and data */
David Spinadel0cedacc2012-03-10 13:00:12 -0800765 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 Berg15854ef2012-03-05 11:24:50 -0800770 goto err_pci_alloc;
David Spinadel0cedacc2012-03-10 13:00:12 -0800771 if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_init.data,
772 get_sec(&pieces, IWL_UCODE_INIT,
773 IWL_UCODE_SECTION_DATA)))
Johannes Berg15854ef2012-03-05 11:24:50 -0800774 goto err_pci_alloc;
775 }
776
777 /* WoWLAN instructions and data */
David Spinadel0cedacc2012-03-10 13:00:12 -0800778 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 Berg15854ef2012-03-05 11:24:50 -0800783 goto err_pci_alloc;
David Spinadel0cedacc2012-03-10 13:00:12 -0800784 if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_wowlan.data,
785 get_sec(&pieces, IWL_UCODE_WOWLAN,
786 IWL_UCODE_SECTION_DATA)))
Johannes Berg15854ef2012-03-05 11:24:50 -0800787 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 Berg0692fe42012-03-06 13:30:37 -0800797 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800798 if (pieces.init_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800799 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -0800800 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800801 fw->init_evtlog_size =
Johannes Berg15854ef2012-03-05 11:24:50 -0800802 cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800803 fw->init_errlog_ptr = pieces.init_errlog_ptr;
804 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800805 if (pieces.inst_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800806 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -0800807 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800808 fw->inst_evtlog_size =
Johannes Berg15854ef2012-03-05 11:24:50 -0800809 cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800810 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800811
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 Berg965974a2012-03-06 13:30:38 -0800823 complete(&drv->request_firmware_complete);
Johannes Berg15854ef2012-03-05 11:24:50 -0800824
Johannes Berg965974a2012-03-06 13:30:38 -0800825 drv->op_mode = iwl_dvm_ops.start(drv->shrd->trans, &drv->fw);
Johannes Berg15854ef2012-03-05 11:24:50 -0800826
Johannes Berg965974a2012-03-06 13:30:38 -0800827 if (!drv->op_mode)
Johannes Berg15854ef2012-03-05 11:24:50 -0800828 goto out_unbind;
829
830 return;
831
832 try_again:
833 /* try next, if any */
834 release_firmware(ucode_raw);
Johannes Berg965974a2012-03-06 13:30:38 -0800835 if (iwl_request_firmware(drv, false))
Johannes Berg15854ef2012-03-05 11:24:50 -0800836 goto out_unbind;
837 return;
838
839 err_pci_alloc:
Johannes Berg965974a2012-03-06 13:30:38 -0800840 IWL_ERR(drv, "failed to allocate pci memory\n");
841 iwl_dealloc_ucode(drv);
Johannes Berg15854ef2012-03-05 11:24:50 -0800842 release_firmware(ucode_raw);
843 out_unbind:
Johannes Berg965974a2012-03-06 13:30:38 -0800844 complete(&drv->request_firmware_complete);
845 device_release_driver(trans(drv)->dev);
Johannes Berg15854ef2012-03-05 11:24:50 -0800846}
847
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200848int iwl_drv_start(struct iwl_shared *shrd,
Johannes Berg706c4ff2012-03-05 11:24:33 -0800849 struct iwl_trans *trans, const struct iwl_cfg *cfg)
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200850{
Johannes Berg965974a2012-03-06 13:30:38 -0800851 struct iwl_drv *drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200852 int ret;
853
854 shrd->cfg = cfg;
855
Johannes Berg965974a2012-03-06 13:30:38 -0800856 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
857 if (!drv) {
858 dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_drv");
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200859 return -ENOMEM;
860 }
Johannes Berg965974a2012-03-06 13:30:38 -0800861 drv->shrd = shrd;
862 shrd->drv = drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200863
Johannes Berg965974a2012-03-06 13:30:38 -0800864 init_completion(&drv->request_firmware_complete);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200865
Johannes Berg965974a2012-03-06 13:30:38 -0800866 ret = iwl_request_firmware(drv, true);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200867
868 if (ret) {
869 dev_printk(KERN_ERR, trans->dev, "Couldn't request the fw");
Johannes Berg965974a2012-03-06 13:30:38 -0800870 kfree(drv);
871 shrd->drv = NULL;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200872 }
873
874 return ret;
875}
876
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200877void iwl_drv_stop(struct iwl_shared *shrd)
878{
Johannes Berg965974a2012-03-06 13:30:38 -0800879 struct iwl_drv *drv = shrd->drv;
Johannes Berg0692fe42012-03-06 13:30:37 -0800880
Johannes Berg965974a2012-03-06 13:30:38 -0800881 wait_for_completion(&drv->request_firmware_complete);
Johannes Berg2e7eb112012-03-05 11:24:51 -0800882
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200883 /* op_mode can be NULL if its start failed */
Johannes Berg965974a2012-03-06 13:30:38 -0800884 if (drv->op_mode)
885 iwl_op_mode_stop(drv->op_mode);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200886
Johannes Berg965974a2012-03-06 13:30:38 -0800887 iwl_dealloc_ucode(drv);
Johannes Berg7db5b982012-03-05 11:24:48 -0800888
Johannes Berg965974a2012-03-06 13:30:38 -0800889 kfree(drv);
890 shrd->drv = NULL;
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200891}