blob: 17485e715424d9392577d337d675b369072bbad7 [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{
David Spinadel6dfa8d02012-03-10 13:00:14 -0800121 int i;
122 for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
123 iwl_free_fw_desc(drv, &img->sec[i]);
Johannes Berg15854ef2012-03-05 11:24:50 -0800124}
125
Johannes Berg965974a2012-03-06 13:30:38 -0800126static void iwl_dealloc_ucode(struct iwl_drv *drv)
Johannes Berg15854ef2012-03-05 11:24:50 -0800127{
David Spinadel6dfa8d02012-03-10 13:00:14 -0800128 int i;
129 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
130 iwl_free_fw_img(drv, drv->fw.img + i);
Johannes Berg15854ef2012-03-05 11:24:50 -0800131}
132
Johannes Berg965974a2012-03-06 13:30:38 -0800133static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
David Spinadel0cedacc2012-03-10 13:00:12 -0800134 struct fw_sec *sec)
Johannes Berg15854ef2012-03-05 11:24:50 -0800135{
David Spinadel0cedacc2012-03-10 13:00:12 -0800136 if (!sec || !sec->size) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800137 desc->v_addr = NULL;
138 return -EINVAL;
139 }
140
David Spinadel0cedacc2012-03-10 13:00:12 -0800141 desc->v_addr = dma_alloc_coherent(trans(drv)->dev, sec->size,
Johannes Berg15854ef2012-03-05 11:24:50 -0800142 &desc->p_addr, GFP_KERNEL);
143 if (!desc->v_addr)
144 return -ENOMEM;
145
David Spinadel0cedacc2012-03-10 13:00:12 -0800146 desc->len = sec->size;
147 desc->offset = sec->offset;
148 memcpy(desc->v_addr, sec->data, sec->size);
Johannes Berg15854ef2012-03-05 11:24:50 -0800149 return 0;
150}
151
152static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
153
154#define UCODE_EXPERIMENTAL_INDEX 100
155#define UCODE_EXPERIMENTAL_TAG "exp"
156
Johannes Berg965974a2012-03-06 13:30:38 -0800157static int iwl_request_firmware(struct iwl_drv *drv, bool first)
Johannes Berg15854ef2012-03-05 11:24:50 -0800158{
Johannes Berg965974a2012-03-06 13:30:38 -0800159 const struct iwl_cfg *cfg = cfg(drv);
Johannes Berg15854ef2012-03-05 11:24:50 -0800160 const char *name_pre = cfg->fw_name_pre;
161 char tag[8];
162
163 if (first) {
164#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
Johannes Berg965974a2012-03-06 13:30:38 -0800165 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
Johannes Berg15854ef2012-03-05 11:24:50 -0800166 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
Johannes Berg965974a2012-03-06 13:30:38 -0800167 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800168#endif
Johannes Berg965974a2012-03-06 13:30:38 -0800169 drv->fw_index = cfg->ucode_api_max;
170 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800171 } else {
Johannes Berg965974a2012-03-06 13:30:38 -0800172 drv->fw_index--;
173 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800174 }
175
Johannes Berg965974a2012-03-06 13:30:38 -0800176 if (drv->fw_index < cfg->ucode_api_min) {
177 IWL_ERR(drv, "no suitable firmware found!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800178 return -ENOENT;
179 }
180
Johannes Berg965974a2012-03-06 13:30:38 -0800181 sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Johannes Berg15854ef2012-03-05 11:24:50 -0800182
Johannes Berg965974a2012-03-06 13:30:38 -0800183 IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
184 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800185 ? "EXPERIMENTAL " : "",
Johannes Berg965974a2012-03-06 13:30:38 -0800186 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800187
Johannes Berg965974a2012-03-06 13:30:38 -0800188 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
189 trans(drv)->dev,
190 GFP_KERNEL, drv, iwl_ucode_callback);
Johannes Berg15854ef2012-03-05 11:24:50 -0800191}
192
David Spinadel0cedacc2012-03-10 13:00:12 -0800193struct fw_img_parsing {
David Spinadel6dfa8d02012-03-10 13:00:14 -0800194 struct fw_sec sec[IWL_UCODE_SECTION_MAX];
David Spinadel0cedacc2012-03-10 13:00:12 -0800195 int sec_counter;
196};
197
David Spinadeled8c8362012-03-10 13:00:13 -0800198/*
199 * struct fw_sec_parsing: to extract fw section and it's offset from tlv
200 */
201struct fw_sec_parsing {
202 __le32 offset;
203 const u8 data[];
204} __packed;
205
206/**
207 * struct iwl_tlv_calib_data - parse the default calib data from TLV
208 *
209 * @ucode_type: the uCode to which the following default calib relates.
210 * @calib: default calibrations.
211 */
212struct iwl_tlv_calib_data {
213 __le32 ucode_type;
214 __le64 calib;
215} __packed;
216
David Spinadel0cedacc2012-03-10 13:00:12 -0800217struct iwl_firmware_pieces {
218 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
Johannes Berg15854ef2012-03-05 11:24:50 -0800219
220 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
221 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
222};
223
David Spinadel0cedacc2012-03-10 13:00:12 -0800224/*
225 * These functions are just to extract uCode section data from the pieces
226 * structure.
227 */
228static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
229 enum iwl_ucode_type type,
230 int sec)
231{
232 return &pieces->img[type].sec[sec];
233}
234
235static void set_sec_data(struct iwl_firmware_pieces *pieces,
236 enum iwl_ucode_type type,
237 int sec,
238 const void *data)
239{
240 pieces->img[type].sec[sec].data = data;
241}
242
243static void set_sec_size(struct iwl_firmware_pieces *pieces,
244 enum iwl_ucode_type type,
245 int sec,
246 size_t size)
247{
248 pieces->img[type].sec[sec].size = size;
249}
250
251static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
252 enum iwl_ucode_type type,
253 int sec)
254{
255 return pieces->img[type].sec[sec].size;
256}
257
258static void set_sec_offset(struct iwl_firmware_pieces *pieces,
259 enum iwl_ucode_type type,
260 int sec,
261 u32 offset)
262{
263 pieces->img[type].sec[sec].offset = offset;
264}
265
David Spinadeled8c8362012-03-10 13:00:13 -0800266/*
267 * Gets uCode section from tlv.
268 */
269static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
270 const void *data, enum iwl_ucode_type type,
271 int size)
272{
273 struct fw_img_parsing *img;
274 struct fw_sec *sec;
275 struct fw_sec_parsing *sec_parse;
276
277 if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
278 return -1;
279
280 sec_parse = (struct fw_sec_parsing *)data;
281
282 img = &pieces->img[type];
283 sec = &img->sec[img->sec_counter];
284
285 sec->offset = le32_to_cpu(sec_parse->offset);
286 sec->data = sec_parse->data;
David Spinadel1176f432012-03-13 14:32:48 +0200287 sec->size = size - sizeof(sec_parse->offset);
David Spinadeled8c8362012-03-10 13:00:13 -0800288
289 ++img->sec_counter;
290
291 return 0;
292}
293
294static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
295{
296 struct iwl_tlv_calib_data *def_calib =
297 (struct iwl_tlv_calib_data *)data;
298 u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
299 if (ucode_type >= IWL_UCODE_TYPE_MAX) {
300 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
301 ucode_type);
302 return -EINVAL;
303 }
304 drv->fw.default_calib[ucode_type] = le64_to_cpu(def_calib->calib);
305 return 0;
306}
307
Johannes Berg965974a2012-03-06 13:30:38 -0800308static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
David Spinadel0cedacc2012-03-10 13:00:12 -0800309 const struct firmware *ucode_raw,
310 struct iwl_firmware_pieces *pieces)
Johannes Berg15854ef2012-03-05 11:24:50 -0800311{
312 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
313 u32 api_ver, hdr_size, build;
314 char buildstr[25];
315 const u8 *src;
316
Johannes Berg965974a2012-03-06 13:30:38 -0800317 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
318 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800319
320 switch (api_ver) {
321 default:
322 hdr_size = 28;
323 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800324 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800325 return -EINVAL;
326 }
327 build = le32_to_cpu(ucode->u.v2.build);
David Spinadel0cedacc2012-03-10 13:00:12 -0800328 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
329 le32_to_cpu(ucode->u.v2.inst_size));
330 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
331 le32_to_cpu(ucode->u.v2.data_size));
332 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
333 le32_to_cpu(ucode->u.v2.init_size));
334 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
335 le32_to_cpu(ucode->u.v2.init_data_size));
Johannes Berg15854ef2012-03-05 11:24:50 -0800336 src = ucode->u.v2.data;
337 break;
338 case 0:
339 case 1:
340 case 2:
341 hdr_size = 24;
342 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800343 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800344 return -EINVAL;
345 }
346 build = 0;
David Spinadel0cedacc2012-03-10 13:00:12 -0800347 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
348 le32_to_cpu(ucode->u.v1.inst_size));
349 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
350 le32_to_cpu(ucode->u.v1.data_size));
351 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
352 le32_to_cpu(ucode->u.v1.init_size));
353 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
354 le32_to_cpu(ucode->u.v1.init_data_size));
Johannes Berg15854ef2012-03-05 11:24:50 -0800355 src = ucode->u.v1.data;
356 break;
357 }
358
359 if (build)
360 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800361 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800362 ? " (EXP)" : "");
363 else
364 buildstr[0] = '\0';
365
Johannes Berg965974a2012-03-06 13:30:38 -0800366 snprintf(drv->fw.fw_version,
367 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800368 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800369 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
370 IWL_UCODE_MINOR(drv->fw.ucode_ver),
371 IWL_UCODE_API(drv->fw.ucode_ver),
372 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef2012-03-05 11:24:50 -0800373 buildstr);
374
375 /* Verify size of file vs. image size info in file's header */
David Spinadel0cedacc2012-03-10 13:00:12 -0800376
377 if (ucode_raw->size != hdr_size +
378 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
379 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
380 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
381 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800382
Johannes Berg965974a2012-03-06 13:30:38 -0800383 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800384 "uCode file size %d does not match expected size\n",
385 (int)ucode_raw->size);
386 return -EINVAL;
387 }
388
Johannes Berg15854ef2012-03-05 11:24:50 -0800389
David Spinadel0cedacc2012-03-10 13:00:12 -0800390 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
391 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
392 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
393 IWLAGN_RTC_INST_LOWER_BOUND);
394 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
395 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
396 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
397 IWLAGN_RTC_DATA_LOWER_BOUND);
398 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
399 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
400 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
401 IWLAGN_RTC_INST_LOWER_BOUND);
402 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
403 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
404 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
405 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800406 return 0;
407}
408
Johannes Berg965974a2012-03-06 13:30:38 -0800409static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800410 const struct firmware *ucode_raw,
David Spinadel0cedacc2012-03-10 13:00:12 -0800411 struct iwl_firmware_pieces *pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -0800412 struct iwl_ucode_capabilities *capa)
413{
414 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
415 struct iwl_ucode_tlv *tlv;
416 size_t len = ucode_raw->size;
417 const u8 *data;
Johannes Berg15854ef2012-03-05 11:24:50 -0800418 u32 tlv_len;
419 enum iwl_ucode_tlv_type tlv_type;
420 const u8 *tlv_data;
421 char buildstr[25];
422 u32 build;
423
424 if (len < sizeof(*ucode)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800425 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800426 return -EINVAL;
427 }
428
429 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800430 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800431 le32_to_cpu(ucode->magic));
432 return -EINVAL;
433 }
434
Johannes Berg965974a2012-03-06 13:30:38 -0800435 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800436 build = le32_to_cpu(ucode->build);
437
438 if (build)
439 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800440 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800441 ? " (EXP)" : "");
442 else
443 buildstr[0] = '\0';
444
Johannes Berg965974a2012-03-06 13:30:38 -0800445 snprintf(drv->fw.fw_version,
446 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800447 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800448 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
449 IWL_UCODE_MINOR(drv->fw.ucode_ver),
450 IWL_UCODE_API(drv->fw.ucode_ver),
451 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef2012-03-05 11:24:50 -0800452 buildstr);
453
454 data = ucode->data;
455
456 len -= sizeof(*ucode);
457
458 while (len >= sizeof(*tlv)) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800459 len -= sizeof(*tlv);
460 tlv = (void *)data;
461
462 tlv_len = le32_to_cpu(tlv->length);
Johannes Berg0479c192012-03-09 09:16:35 +0100463 tlv_type = le32_to_cpu(tlv->type);
Johannes Berg15854ef2012-03-05 11:24:50 -0800464 tlv_data = tlv->data;
465
466 if (len < tlv_len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800467 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800468 len, tlv_len);
469 return -EINVAL;
470 }
471 len -= ALIGN(tlv_len, 4);
472 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
473
Johannes Berg15854ef2012-03-05 11:24:50 -0800474 switch (tlv_type) {
475 case IWL_UCODE_TLV_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800476 set_sec_data(pieces, IWL_UCODE_REGULAR,
477 IWL_UCODE_SECTION_INST, tlv_data);
478 set_sec_size(pieces, IWL_UCODE_REGULAR,
479 IWL_UCODE_SECTION_INST, tlv_len);
480 set_sec_offset(pieces, IWL_UCODE_REGULAR,
481 IWL_UCODE_SECTION_INST,
482 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800483 break;
484 case IWL_UCODE_TLV_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800485 set_sec_data(pieces, IWL_UCODE_REGULAR,
486 IWL_UCODE_SECTION_DATA, tlv_data);
487 set_sec_size(pieces, IWL_UCODE_REGULAR,
488 IWL_UCODE_SECTION_DATA, tlv_len);
489 set_sec_offset(pieces, IWL_UCODE_REGULAR,
490 IWL_UCODE_SECTION_DATA,
491 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800492 break;
493 case IWL_UCODE_TLV_INIT:
David Spinadel0cedacc2012-03-10 13:00:12 -0800494 set_sec_data(pieces, IWL_UCODE_INIT,
495 IWL_UCODE_SECTION_INST, tlv_data);
496 set_sec_size(pieces, IWL_UCODE_INIT,
497 IWL_UCODE_SECTION_INST, tlv_len);
498 set_sec_offset(pieces, IWL_UCODE_INIT,
499 IWL_UCODE_SECTION_INST,
500 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800501 break;
502 case IWL_UCODE_TLV_INIT_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800503 set_sec_data(pieces, IWL_UCODE_INIT,
504 IWL_UCODE_SECTION_DATA, tlv_data);
505 set_sec_size(pieces, IWL_UCODE_INIT,
506 IWL_UCODE_SECTION_DATA, tlv_len);
507 set_sec_offset(pieces, IWL_UCODE_INIT,
508 IWL_UCODE_SECTION_DATA,
509 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800510 break;
511 case IWL_UCODE_TLV_BOOT:
Johannes Berg965974a2012-03-06 13:30:38 -0800512 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800513 break;
514 case IWL_UCODE_TLV_PROBE_MAX_LEN:
515 if (tlv_len != sizeof(u32))
516 goto invalid_tlv_len;
517 capa->max_probe_length =
518 le32_to_cpup((__le32 *)tlv_data);
519 break;
520 case IWL_UCODE_TLV_PAN:
521 if (tlv_len)
522 goto invalid_tlv_len;
523 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
524 break;
525 case IWL_UCODE_TLV_FLAGS:
526 /* must be at least one u32 */
527 if (tlv_len < sizeof(u32))
528 goto invalid_tlv_len;
529 /* and a proper number of u32s */
530 if (tlv_len % sizeof(u32))
531 goto invalid_tlv_len;
532 /*
533 * This driver only reads the first u32 as
534 * right now no more features are defined,
535 * if that changes then either the driver
536 * will not work with the new firmware, or
537 * it'll not take advantage of new features.
538 */
539 capa->flags = le32_to_cpup((__le32 *)tlv_data);
540 break;
541 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
542 if (tlv_len != sizeof(u32))
543 goto invalid_tlv_len;
544 pieces->init_evtlog_ptr =
545 le32_to_cpup((__le32 *)tlv_data);
546 break;
547 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
548 if (tlv_len != sizeof(u32))
549 goto invalid_tlv_len;
550 pieces->init_evtlog_size =
551 le32_to_cpup((__le32 *)tlv_data);
552 break;
553 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
554 if (tlv_len != sizeof(u32))
555 goto invalid_tlv_len;
556 pieces->init_errlog_ptr =
557 le32_to_cpup((__le32 *)tlv_data);
558 break;
559 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
560 if (tlv_len != sizeof(u32))
561 goto invalid_tlv_len;
562 pieces->inst_evtlog_ptr =
563 le32_to_cpup((__le32 *)tlv_data);
564 break;
565 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
566 if (tlv_len != sizeof(u32))
567 goto invalid_tlv_len;
568 pieces->inst_evtlog_size =
569 le32_to_cpup((__le32 *)tlv_data);
570 break;
571 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
572 if (tlv_len != sizeof(u32))
573 goto invalid_tlv_len;
574 pieces->inst_errlog_ptr =
575 le32_to_cpup((__le32 *)tlv_data);
576 break;
577 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
578 if (tlv_len)
579 goto invalid_tlv_len;
Johannes Berg965974a2012-03-06 13:30:38 -0800580 drv->fw.enhance_sensitivity_table = true;
Johannes Berg15854ef2012-03-05 11:24:50 -0800581 break;
582 case IWL_UCODE_TLV_WOWLAN_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800583 set_sec_data(pieces, IWL_UCODE_WOWLAN,
584 IWL_UCODE_SECTION_INST, tlv_data);
585 set_sec_size(pieces, IWL_UCODE_WOWLAN,
586 IWL_UCODE_SECTION_INST, tlv_len);
587 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
588 IWL_UCODE_SECTION_INST,
589 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800590 break;
591 case IWL_UCODE_TLV_WOWLAN_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800592 set_sec_data(pieces, IWL_UCODE_WOWLAN,
593 IWL_UCODE_SECTION_DATA, tlv_data);
594 set_sec_size(pieces, IWL_UCODE_WOWLAN,
595 IWL_UCODE_SECTION_DATA, tlv_len);
596 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
597 IWL_UCODE_SECTION_DATA,
598 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800599 break;
600 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
601 if (tlv_len != sizeof(u32))
602 goto invalid_tlv_len;
603 capa->standard_phy_calibration_size =
604 le32_to_cpup((__le32 *)tlv_data);
605 break;
David Spinadeled8c8362012-03-10 13:00:13 -0800606 case IWL_UCODE_TLV_SEC_RT:
607 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
608 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800609 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800610 break;
611 case IWL_UCODE_TLV_SEC_INIT:
612 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
613 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800614 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800615 break;
616 case IWL_UCODE_TLV_SEC_WOWLAN:
617 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
618 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800619 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800620 break;
621 case IWL_UCODE_TLV_DEF_CALIB:
622 if (tlv_len != sizeof(struct iwl_tlv_calib_data))
623 goto invalid_tlv_len;
624 if (iwl_set_default_calib(drv, tlv_data))
625 goto tlv_error;
626 break;
627 case IWL_UCODE_TLV_PHY_SKU:
628 if (tlv_len != sizeof(u32))
629 goto invalid_tlv_len;
630 drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
631 break;
Johannes Berg15854ef2012-03-05 11:24:50 -0800632 default:
Johannes Berg965974a2012-03-06 13:30:38 -0800633 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
Johannes Berg15854ef2012-03-05 11:24:50 -0800634 break;
635 }
636 }
637
638 if (len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800639 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
640 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800641 return -EINVAL;
642 }
643
644 return 0;
645
646 invalid_tlv_len:
Johannes Berg965974a2012-03-06 13:30:38 -0800647 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
David Spinadeled8c8362012-03-10 13:00:13 -0800648 tlv_error:
Johannes Berg965974a2012-03-06 13:30:38 -0800649 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800650
651 return -EINVAL;
652}
653
David Spinadel6dfa8d02012-03-10 13:00:14 -0800654static int alloc_pci_desc(struct iwl_drv *drv,
655 struct iwl_firmware_pieces *pieces,
656 enum iwl_ucode_type type)
657{
658 int i;
659 for (i = 0;
660 i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
661 i++)
662 if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
663 get_sec(pieces, type, i)))
664 return -1;
665 return 0;
666}
667
668static int validate_sec_sizes(struct iwl_drv *drv,
669 struct iwl_firmware_pieces *pieces,
670 const struct iwl_cfg *cfg)
671{
672 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
673 get_sec_size(pieces, IWL_UCODE_REGULAR,
674 IWL_UCODE_SECTION_INST));
675 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
676 get_sec_size(pieces, IWL_UCODE_REGULAR,
677 IWL_UCODE_SECTION_DATA));
678 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
679 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
680 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
681 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
682
683 /* Verify that uCode images will fit in card's SRAM. */
684 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
685 cfg->max_inst_size) {
686 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
687 get_sec_size(pieces, IWL_UCODE_REGULAR,
688 IWL_UCODE_SECTION_INST));
689 return -1;
690 }
691
692 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
693 cfg->max_data_size) {
694 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
695 get_sec_size(pieces, IWL_UCODE_REGULAR,
696 IWL_UCODE_SECTION_DATA));
697 return -1;
698 }
699
700 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
701 cfg->max_inst_size) {
702 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
703 get_sec_size(pieces, IWL_UCODE_INIT,
704 IWL_UCODE_SECTION_INST));
705 return -1;
706 }
707
708 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
709 cfg->max_data_size) {
710 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
711 get_sec_size(pieces, IWL_UCODE_REGULAR,
712 IWL_UCODE_SECTION_DATA));
713 return -1;
714 }
715 return 0;
716}
717
718
Johannes Berg15854ef2012-03-05 11:24:50 -0800719/**
720 * iwl_ucode_callback - callback when firmware was loaded
721 *
722 * If loaded successfully, copies the firmware into buffers
723 * for the card to fetch (via DMA).
724 */
725static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
726{
Johannes Berg965974a2012-03-06 13:30:38 -0800727 struct iwl_drv *drv = context;
728 const struct iwl_cfg *cfg = cfg(drv);
729 struct iwl_fw *fw = &drv->fw;
Johannes Berg15854ef2012-03-05 11:24:50 -0800730 struct iwl_ucode_header *ucode;
731 int err;
David Spinadel0cedacc2012-03-10 13:00:12 -0800732 struct iwl_firmware_pieces pieces;
Johannes Berg15854ef2012-03-05 11:24:50 -0800733 const unsigned int api_max = cfg->ucode_api_max;
734 unsigned int api_ok = cfg->ucode_api_ok;
735 const unsigned int api_min = cfg->ucode_api_min;
736 u32 api_ver;
David Spinadel6dfa8d02012-03-10 13:00:14 -0800737 int i;
Johannes Berg15854ef2012-03-05 11:24:50 -0800738
739 fw->ucode_capa.max_probe_length = 200;
740 fw->ucode_capa.standard_phy_calibration_size =
741 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
742
743 if (!api_ok)
744 api_ok = api_max;
745
746 memset(&pieces, 0, sizeof(pieces));
747
748 if (!ucode_raw) {
Johannes Berg965974a2012-03-06 13:30:38 -0800749 if (drv->fw_index <= api_ok)
750 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800751 "request for firmware file '%s' failed.\n",
Johannes Berg965974a2012-03-06 13:30:38 -0800752 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800753 goto try_again;
754 }
755
Johannes Berg965974a2012-03-06 13:30:38 -0800756 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
757 drv->firmware_name, ucode_raw->size);
Johannes Berg15854ef2012-03-05 11:24:50 -0800758
759 /* Make sure that we got at least the API version number */
760 if (ucode_raw->size < 4) {
Johannes Berg965974a2012-03-06 13:30:38 -0800761 IWL_ERR(drv, "File size way too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800762 goto try_again;
763 }
764
765 /* Data from ucode file: header followed by uCode images */
766 ucode = (struct iwl_ucode_header *)ucode_raw->data;
767
768 if (ucode->ver)
Johannes Berg965974a2012-03-06 13:30:38 -0800769 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
Johannes Berg15854ef2012-03-05 11:24:50 -0800770 else
Johannes Berg965974a2012-03-06 13:30:38 -0800771 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -0800772 &fw->ucode_capa);
773
774 if (err)
775 goto try_again;
776
Johannes Berg965974a2012-03-06 13:30:38 -0800777 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800778
779 /*
780 * api_ver should match the api version forming part of the
781 * firmware filename ... but we don't check for that and only rely
782 * on the API version read from firmware header from here on forward
783 */
784 /* no api version check required for experimental uCode */
Johannes Berg965974a2012-03-06 13:30:38 -0800785 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800786 if (api_ver < api_min || api_ver > api_max) {
Johannes Berg965974a2012-03-06 13:30:38 -0800787 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800788 "Driver unable to support your firmware API. "
789 "Driver supports v%u, firmware is v%u.\n",
790 api_max, api_ver);
791 goto try_again;
792 }
793
794 if (api_ver < api_ok) {
795 if (api_ok != api_max)
Johannes Berg965974a2012-03-06 13:30:38 -0800796 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -0800797 "expected v%u through v%u, got v%u.\n",
798 api_ok, api_max, api_ver);
799 else
Johannes Berg965974a2012-03-06 13:30:38 -0800800 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -0800801 "expected v%u, got v%u.\n",
802 api_max, api_ver);
Johannes Berg965974a2012-03-06 13:30:38 -0800803 IWL_ERR(drv, "New firmware can be obtained from "
Johannes Berg15854ef2012-03-05 11:24:50 -0800804 "http://www.intellinuxwireless.org/.\n");
805 }
806 }
807
Johannes Berg965974a2012-03-06 13:30:38 -0800808 IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
Johannes Berg15854ef2012-03-05 11:24:50 -0800809
810 /*
David Spinadel4db2c9a2012-03-10 13:00:15 -0800811 * In mvm uCode there is no difference between data and instructions
812 * sections.
813 */
814 if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, cfg))
Johannes Berg15854ef2012-03-05 11:24:50 -0800815 goto try_again;
Johannes Berg15854ef2012-03-05 11:24:50 -0800816
817 /* Allocate ucode buffers for card's bus-master loading ... */
818
819 /* Runtime instructions and 2 copies of data:
820 * 1) unmodified from disk
821 * 2) backup cache for save/restore during power-downs */
David Spinadel6dfa8d02012-03-10 13:00:14 -0800822 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
823 if (alloc_pci_desc(drv, &pieces, i))
Johannes Berg15854ef2012-03-05 11:24:50 -0800824 goto err_pci_alloc;
Johannes Berg15854ef2012-03-05 11:24:50 -0800825
826 /* Now that we can no longer fail, copy information */
827
828 /*
829 * The (size - 16) / 12 formula is based on the information recorded
830 * for each event, which is of mode 1 (including timestamp) for all
831 * new microcodes that include this information.
832 */
Johannes Berg0692fe42012-03-06 13:30:37 -0800833 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800834 if (pieces.init_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800835 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -0800836 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800837 fw->init_evtlog_size =
Johannes Berg15854ef2012-03-05 11:24:50 -0800838 cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800839 fw->init_errlog_ptr = pieces.init_errlog_ptr;
840 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800841 if (pieces.inst_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800842 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -0800843 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800844 fw->inst_evtlog_size =
Johannes Berg15854ef2012-03-05 11:24:50 -0800845 cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800846 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800847
848 /*
849 * figure out the offset of chain noise reset and gain commands
850 * base on the size of standard phy calibration commands table size
851 */
852 if (fw->ucode_capa.standard_phy_calibration_size >
853 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
854 fw->ucode_capa.standard_phy_calibration_size =
855 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
856
857 /* We have our copies now, allow OS release its copies */
858 release_firmware(ucode_raw);
Johannes Berg965974a2012-03-06 13:30:38 -0800859 complete(&drv->request_firmware_complete);
Johannes Berg15854ef2012-03-05 11:24:50 -0800860
Johannes Berg965974a2012-03-06 13:30:38 -0800861 drv->op_mode = iwl_dvm_ops.start(drv->shrd->trans, &drv->fw);
Johannes Berg15854ef2012-03-05 11:24:50 -0800862
Johannes Berg965974a2012-03-06 13:30:38 -0800863 if (!drv->op_mode)
Johannes Berg15854ef2012-03-05 11:24:50 -0800864 goto out_unbind;
865
866 return;
867
868 try_again:
869 /* try next, if any */
870 release_firmware(ucode_raw);
Johannes Berg965974a2012-03-06 13:30:38 -0800871 if (iwl_request_firmware(drv, false))
Johannes Berg15854ef2012-03-05 11:24:50 -0800872 goto out_unbind;
873 return;
874
875 err_pci_alloc:
Johannes Berg965974a2012-03-06 13:30:38 -0800876 IWL_ERR(drv, "failed to allocate pci memory\n");
877 iwl_dealloc_ucode(drv);
Johannes Berg15854ef2012-03-05 11:24:50 -0800878 release_firmware(ucode_raw);
879 out_unbind:
Johannes Berg965974a2012-03-06 13:30:38 -0800880 complete(&drv->request_firmware_complete);
881 device_release_driver(trans(drv)->dev);
Johannes Berg15854ef2012-03-05 11:24:50 -0800882}
883
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200884int iwl_drv_start(struct iwl_shared *shrd,
Johannes Berg706c4ff2012-03-05 11:24:33 -0800885 struct iwl_trans *trans, const struct iwl_cfg *cfg)
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200886{
Johannes Berg965974a2012-03-06 13:30:38 -0800887 struct iwl_drv *drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200888 int ret;
889
890 shrd->cfg = cfg;
891
Johannes Berg965974a2012-03-06 13:30:38 -0800892 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
893 if (!drv) {
894 dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_drv");
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200895 return -ENOMEM;
896 }
Johannes Berg965974a2012-03-06 13:30:38 -0800897 drv->shrd = shrd;
898 shrd->drv = drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200899
Johannes Berg965974a2012-03-06 13:30:38 -0800900 init_completion(&drv->request_firmware_complete);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200901
Johannes Berg965974a2012-03-06 13:30:38 -0800902 ret = iwl_request_firmware(drv, true);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200903
904 if (ret) {
905 dev_printk(KERN_ERR, trans->dev, "Couldn't request the fw");
Johannes Berg965974a2012-03-06 13:30:38 -0800906 kfree(drv);
907 shrd->drv = NULL;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200908 }
909
910 return ret;
911}
912
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200913void iwl_drv_stop(struct iwl_shared *shrd)
914{
Johannes Berg965974a2012-03-06 13:30:38 -0800915 struct iwl_drv *drv = shrd->drv;
Johannes Berg0692fe42012-03-06 13:30:37 -0800916
Johannes Berg965974a2012-03-06 13:30:38 -0800917 wait_for_completion(&drv->request_firmware_complete);
Johannes Berg2e7eb112012-03-05 11:24:51 -0800918
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200919 /* op_mode can be NULL if its start failed */
Johannes Berg965974a2012-03-06 13:30:38 -0800920 if (drv->op_mode)
921 iwl_op_mode_stop(drv->op_mode);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200922
Johannes Berg965974a2012-03-06 13:30:38 -0800923 iwl_dealloc_ucode(drv);
Johannes Berg7db5b982012-03-05 11:24:48 -0800924
Johannes Berg965974a2012-03-06 13:30:38 -0800925 kfree(drv);
926 shrd->drv = NULL;
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200927}