blob: e4cc5f56e9df3d9f71e8733ed1823b3293d983d1 [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 Berg15854ef92012-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"
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +020070#include "iwl-op-mode.h"
David Spinadel0cedacc2012-03-10 13:00:12 -080071#include "iwl-agn-hw.h"
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020072
Johannes Berg0692fe42012-03-06 13:30:37 -080073/* private includes */
Johannes Berg3995dea2012-03-06 13:30:44 -080074#include "iwl-fw-file.h"
Johannes Berg0692fe42012-03-06 13:30:37 -080075
Johannes Berg965974a62012-03-06 13:30:38 -080076/**
77 * struct iwl_drv - drv common data
78 * @fw: the iwl_fw structure
Johannes Berg965974a62012-03-06 13:30:38 -080079 * @op_mode: the running op_mode
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +020080 * @trans: transport layer
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +020081 * @dev: for debug prints only
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +020082 * @cfg: configuration struct
Johannes Berg965974a62012-03-06 13:30:38 -080083 * @fw_index: firmware revision to try loading
84 * @firmware_name: composite filename of ucode file to load
85 * @request_firmware_complete: the firmware has been obtained from user space
86 */
87struct iwl_drv {
88 struct iwl_fw fw;
89
Johannes Berg965974a62012-03-06 13:30:38 -080090 struct iwl_op_mode *op_mode;
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -070091 struct iwl_trans *trans;
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +020092 struct device *dev;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +020093 const struct iwl_cfg *cfg;
Johannes Berg965974a62012-03-06 13:30:38 -080094
95 int fw_index; /* firmware we're trying to load */
96 char firmware_name[25]; /* name of firmware file to load */
97
98 struct completion request_firmware_complete;
99};
100
101
102
David Spinadel0cedacc2012-03-10 13:00:12 -0800103/*
104 * struct fw_sec: Just for the image parsing proccess.
105 * For the fw storage we are using struct fw_desc.
106 */
107struct fw_sec {
108 const void *data; /* the sec data */
109 size_t size; /* section size */
110 u32 offset; /* offset of writing in the device */
111};
112
Johannes Berg965974a62012-03-06 13:30:38 -0800113static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
Johannes Berg15854ef92012-03-05 11:24:50 -0800114{
115 if (desc->v_addr)
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200116 dma_free_coherent(drv->trans->dev, desc->len,
Johannes Berg15854ef92012-03-05 11:24:50 -0800117 desc->v_addr, desc->p_addr);
118 desc->v_addr = NULL;
119 desc->len = 0;
120}
121
Johannes Berg965974a62012-03-06 13:30:38 -0800122static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
Johannes Berg15854ef92012-03-05 11:24:50 -0800123{
David Spinadel6dfa8d02012-03-10 13:00:14 -0800124 int i;
125 for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
126 iwl_free_fw_desc(drv, &img->sec[i]);
Johannes Berg15854ef92012-03-05 11:24:50 -0800127}
128
Johannes Berg965974a62012-03-06 13:30:38 -0800129static void iwl_dealloc_ucode(struct iwl_drv *drv)
Johannes Berg15854ef92012-03-05 11:24:50 -0800130{
David Spinadel6dfa8d02012-03-10 13:00:14 -0800131 int i;
132 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
133 iwl_free_fw_img(drv, drv->fw.img + i);
Johannes Berg15854ef92012-03-05 11:24:50 -0800134}
135
Johannes Berg965974a62012-03-06 13:30:38 -0800136static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
David Spinadel0cedacc2012-03-10 13:00:12 -0800137 struct fw_sec *sec)
Johannes Berg15854ef92012-03-05 11:24:50 -0800138{
David Spinadel0cedacc2012-03-10 13:00:12 -0800139 if (!sec || !sec->size) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800140 desc->v_addr = NULL;
141 return -EINVAL;
142 }
143
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200144 desc->v_addr = dma_alloc_coherent(drv->trans->dev, sec->size,
Johannes Berg15854ef92012-03-05 11:24:50 -0800145 &desc->p_addr, GFP_KERNEL);
146 if (!desc->v_addr)
147 return -ENOMEM;
148
David Spinadel0cedacc2012-03-10 13:00:12 -0800149 desc->len = sec->size;
150 desc->offset = sec->offset;
151 memcpy(desc->v_addr, sec->data, sec->size);
Johannes Berg15854ef92012-03-05 11:24:50 -0800152 return 0;
153}
154
155static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
156
157#define UCODE_EXPERIMENTAL_INDEX 100
158#define UCODE_EXPERIMENTAL_TAG "exp"
159
Johannes Berg965974a62012-03-06 13:30:38 -0800160static int iwl_request_firmware(struct iwl_drv *drv, bool first)
Johannes Berg15854ef92012-03-05 11:24:50 -0800161{
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200162 const char *name_pre = drv->cfg->fw_name_pre;
Johannes Berg15854ef92012-03-05 11:24:50 -0800163 char tag[8];
164
165 if (first) {
166#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
Johannes Berg965974a62012-03-06 13:30:38 -0800167 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
Johannes Berg15854ef92012-03-05 11:24:50 -0800168 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
Johannes Berg965974a62012-03-06 13:30:38 -0800169 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800170#endif
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200171 drv->fw_index = drv->cfg->ucode_api_max;
Johannes Berg965974a62012-03-06 13:30:38 -0800172 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef92012-03-05 11:24:50 -0800173 } else {
Johannes Berg965974a62012-03-06 13:30:38 -0800174 drv->fw_index--;
175 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef92012-03-05 11:24:50 -0800176 }
177
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200178 if (drv->fw_index < drv->cfg->ucode_api_min) {
Johannes Berg965974a62012-03-06 13:30:38 -0800179 IWL_ERR(drv, "no suitable firmware found!\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800180 return -ENOENT;
181 }
182
Johannes Berg965974a62012-03-06 13:30:38 -0800183 sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Johannes Berg15854ef92012-03-05 11:24:50 -0800184
Johannes Berg965974a62012-03-06 13:30:38 -0800185 IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
186 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef92012-03-05 11:24:50 -0800187 ? "EXPERIMENTAL " : "",
Johannes Berg965974a62012-03-06 13:30:38 -0800188 drv->firmware_name);
Johannes Berg15854ef92012-03-05 11:24:50 -0800189
Johannes Berg965974a62012-03-06 13:30:38 -0800190 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200191 drv->trans->dev,
Johannes Berg965974a62012-03-06 13:30:38 -0800192 GFP_KERNEL, drv, iwl_ucode_callback);
Johannes Berg15854ef92012-03-05 11:24:50 -0800193}
194
David Spinadel0cedacc2012-03-10 13:00:12 -0800195struct fw_img_parsing {
David Spinadel6dfa8d02012-03-10 13:00:14 -0800196 struct fw_sec sec[IWL_UCODE_SECTION_MAX];
David Spinadel0cedacc2012-03-10 13:00:12 -0800197 int sec_counter;
198};
199
David Spinadeled8c8362012-03-10 13:00:13 -0800200/*
201 * struct fw_sec_parsing: to extract fw section and it's offset from tlv
202 */
203struct fw_sec_parsing {
204 __le32 offset;
205 const u8 data[];
206} __packed;
207
208/**
209 * struct iwl_tlv_calib_data - parse the default calib data from TLV
210 *
211 * @ucode_type: the uCode to which the following default calib relates.
212 * @calib: default calibrations.
213 */
214struct iwl_tlv_calib_data {
215 __le32 ucode_type;
216 __le64 calib;
217} __packed;
218
David Spinadel0cedacc2012-03-10 13:00:12 -0800219struct iwl_firmware_pieces {
220 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
Johannes Berg15854ef92012-03-05 11:24:50 -0800221
222 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
223 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
224};
225
David Spinadel0cedacc2012-03-10 13:00:12 -0800226/*
227 * These functions are just to extract uCode section data from the pieces
228 * structure.
229 */
230static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
231 enum iwl_ucode_type type,
232 int sec)
233{
234 return &pieces->img[type].sec[sec];
235}
236
237static void set_sec_data(struct iwl_firmware_pieces *pieces,
238 enum iwl_ucode_type type,
239 int sec,
240 const void *data)
241{
242 pieces->img[type].sec[sec].data = data;
243}
244
245static void set_sec_size(struct iwl_firmware_pieces *pieces,
246 enum iwl_ucode_type type,
247 int sec,
248 size_t size)
249{
250 pieces->img[type].sec[sec].size = size;
251}
252
253static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
254 enum iwl_ucode_type type,
255 int sec)
256{
257 return pieces->img[type].sec[sec].size;
258}
259
260static void set_sec_offset(struct iwl_firmware_pieces *pieces,
261 enum iwl_ucode_type type,
262 int sec,
263 u32 offset)
264{
265 pieces->img[type].sec[sec].offset = offset;
266}
267
David Spinadeled8c8362012-03-10 13:00:13 -0800268/*
269 * Gets uCode section from tlv.
270 */
271static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
272 const void *data, enum iwl_ucode_type type,
273 int size)
274{
275 struct fw_img_parsing *img;
276 struct fw_sec *sec;
277 struct fw_sec_parsing *sec_parse;
278
279 if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
280 return -1;
281
282 sec_parse = (struct fw_sec_parsing *)data;
283
284 img = &pieces->img[type];
285 sec = &img->sec[img->sec_counter];
286
287 sec->offset = le32_to_cpu(sec_parse->offset);
288 sec->data = sec_parse->data;
David Spinadel1176f432012-03-13 14:32:48 +0200289 sec->size = size - sizeof(sec_parse->offset);
David Spinadeled8c8362012-03-10 13:00:13 -0800290
291 ++img->sec_counter;
292
293 return 0;
294}
295
296static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
297{
298 struct iwl_tlv_calib_data *def_calib =
299 (struct iwl_tlv_calib_data *)data;
300 u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
301 if (ucode_type >= IWL_UCODE_TYPE_MAX) {
302 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
303 ucode_type);
304 return -EINVAL;
305 }
306 drv->fw.default_calib[ucode_type] = le64_to_cpu(def_calib->calib);
307 return 0;
308}
309
Johannes Berg965974a62012-03-06 13:30:38 -0800310static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
David Spinadel0cedacc2012-03-10 13:00:12 -0800311 const struct firmware *ucode_raw,
312 struct iwl_firmware_pieces *pieces)
Johannes Berg15854ef92012-03-05 11:24:50 -0800313{
314 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
315 u32 api_ver, hdr_size, build;
316 char buildstr[25];
317 const u8 *src;
318
Johannes Berg965974a62012-03-06 13:30:38 -0800319 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
320 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef92012-03-05 11:24:50 -0800321
322 switch (api_ver) {
323 default:
324 hdr_size = 28;
325 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a62012-03-06 13:30:38 -0800326 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800327 return -EINVAL;
328 }
329 build = le32_to_cpu(ucode->u.v2.build);
David Spinadel0cedacc2012-03-10 13:00:12 -0800330 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
331 le32_to_cpu(ucode->u.v2.inst_size));
332 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
333 le32_to_cpu(ucode->u.v2.data_size));
334 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
335 le32_to_cpu(ucode->u.v2.init_size));
336 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
337 le32_to_cpu(ucode->u.v2.init_data_size));
Johannes Berg15854ef92012-03-05 11:24:50 -0800338 src = ucode->u.v2.data;
339 break;
340 case 0:
341 case 1:
342 case 2:
343 hdr_size = 24;
344 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a62012-03-06 13:30:38 -0800345 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800346 return -EINVAL;
347 }
348 build = 0;
David Spinadel0cedacc2012-03-10 13:00:12 -0800349 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
350 le32_to_cpu(ucode->u.v1.inst_size));
351 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
352 le32_to_cpu(ucode->u.v1.data_size));
353 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
354 le32_to_cpu(ucode->u.v1.init_size));
355 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
356 le32_to_cpu(ucode->u.v1.init_data_size));
Johannes Berg15854ef92012-03-05 11:24:50 -0800357 src = ucode->u.v1.data;
358 break;
359 }
360
361 if (build)
362 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a62012-03-06 13:30:38 -0800363 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef92012-03-05 11:24:50 -0800364 ? " (EXP)" : "");
365 else
366 buildstr[0] = '\0';
367
Johannes Berg965974a62012-03-06 13:30:38 -0800368 snprintf(drv->fw.fw_version,
369 sizeof(drv->fw.fw_version),
Johannes Berg15854ef92012-03-05 11:24:50 -0800370 "%u.%u.%u.%u%s",
Johannes Berg965974a62012-03-06 13:30:38 -0800371 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
372 IWL_UCODE_MINOR(drv->fw.ucode_ver),
373 IWL_UCODE_API(drv->fw.ucode_ver),
374 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef92012-03-05 11:24:50 -0800375 buildstr);
376
377 /* Verify size of file vs. image size info in file's header */
David Spinadel0cedacc2012-03-10 13:00:12 -0800378
379 if (ucode_raw->size != hdr_size +
380 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
381 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
382 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
383 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800384
Johannes Berg965974a62012-03-06 13:30:38 -0800385 IWL_ERR(drv,
Johannes Berg15854ef92012-03-05 11:24:50 -0800386 "uCode file size %d does not match expected size\n",
387 (int)ucode_raw->size);
388 return -EINVAL;
389 }
390
Johannes Berg15854ef92012-03-05 11:24:50 -0800391
David Spinadel0cedacc2012-03-10 13:00:12 -0800392 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
393 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
394 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
395 IWLAGN_RTC_INST_LOWER_BOUND);
396 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
397 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
398 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
399 IWLAGN_RTC_DATA_LOWER_BOUND);
400 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
401 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
402 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
403 IWLAGN_RTC_INST_LOWER_BOUND);
404 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
405 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
406 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
407 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800408 return 0;
409}
410
Johannes Berg965974a62012-03-06 13:30:38 -0800411static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
Johannes Berg15854ef92012-03-05 11:24:50 -0800412 const struct firmware *ucode_raw,
David Spinadel0cedacc2012-03-10 13:00:12 -0800413 struct iwl_firmware_pieces *pieces,
Johannes Berg15854ef92012-03-05 11:24:50 -0800414 struct iwl_ucode_capabilities *capa)
415{
416 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
417 struct iwl_ucode_tlv *tlv;
418 size_t len = ucode_raw->size;
419 const u8 *data;
Johannes Berg15854ef92012-03-05 11:24:50 -0800420 u32 tlv_len;
421 enum iwl_ucode_tlv_type tlv_type;
422 const u8 *tlv_data;
423 char buildstr[25];
424 u32 build;
425
426 if (len < sizeof(*ucode)) {
Johannes Berg965974a62012-03-06 13:30:38 -0800427 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
Johannes Berg15854ef92012-03-05 11:24:50 -0800428 return -EINVAL;
429 }
430
431 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
Johannes Berg965974a62012-03-06 13:30:38 -0800432 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
Johannes Berg15854ef92012-03-05 11:24:50 -0800433 le32_to_cpu(ucode->magic));
434 return -EINVAL;
435 }
436
Johannes Berg965974a62012-03-06 13:30:38 -0800437 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
Johannes Berg15854ef92012-03-05 11:24:50 -0800438 build = le32_to_cpu(ucode->build);
439
440 if (build)
441 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a62012-03-06 13:30:38 -0800442 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef92012-03-05 11:24:50 -0800443 ? " (EXP)" : "");
444 else
445 buildstr[0] = '\0';
446
Johannes Berg965974a62012-03-06 13:30:38 -0800447 snprintf(drv->fw.fw_version,
448 sizeof(drv->fw.fw_version),
Johannes Berg15854ef92012-03-05 11:24:50 -0800449 "%u.%u.%u.%u%s",
Johannes Berg965974a62012-03-06 13:30:38 -0800450 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
451 IWL_UCODE_MINOR(drv->fw.ucode_ver),
452 IWL_UCODE_API(drv->fw.ucode_ver),
453 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef92012-03-05 11:24:50 -0800454 buildstr);
455
456 data = ucode->data;
457
458 len -= sizeof(*ucode);
459
460 while (len >= sizeof(*tlv)) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800461 len -= sizeof(*tlv);
462 tlv = (void *)data;
463
464 tlv_len = le32_to_cpu(tlv->length);
Johannes Berg0479c192012-03-09 09:16:35 +0100465 tlv_type = le32_to_cpu(tlv->type);
Johannes Berg15854ef92012-03-05 11:24:50 -0800466 tlv_data = tlv->data;
467
468 if (len < tlv_len) {
Johannes Berg965974a62012-03-06 13:30:38 -0800469 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
Johannes Berg15854ef92012-03-05 11:24:50 -0800470 len, tlv_len);
471 return -EINVAL;
472 }
473 len -= ALIGN(tlv_len, 4);
474 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
475
Johannes Berg15854ef92012-03-05 11:24:50 -0800476 switch (tlv_type) {
477 case IWL_UCODE_TLV_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800478 set_sec_data(pieces, IWL_UCODE_REGULAR,
479 IWL_UCODE_SECTION_INST, tlv_data);
480 set_sec_size(pieces, IWL_UCODE_REGULAR,
481 IWL_UCODE_SECTION_INST, tlv_len);
482 set_sec_offset(pieces, IWL_UCODE_REGULAR,
483 IWL_UCODE_SECTION_INST,
484 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800485 break;
486 case IWL_UCODE_TLV_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800487 set_sec_data(pieces, IWL_UCODE_REGULAR,
488 IWL_UCODE_SECTION_DATA, tlv_data);
489 set_sec_size(pieces, IWL_UCODE_REGULAR,
490 IWL_UCODE_SECTION_DATA, tlv_len);
491 set_sec_offset(pieces, IWL_UCODE_REGULAR,
492 IWL_UCODE_SECTION_DATA,
493 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800494 break;
495 case IWL_UCODE_TLV_INIT:
David Spinadel0cedacc2012-03-10 13:00:12 -0800496 set_sec_data(pieces, IWL_UCODE_INIT,
497 IWL_UCODE_SECTION_INST, tlv_data);
498 set_sec_size(pieces, IWL_UCODE_INIT,
499 IWL_UCODE_SECTION_INST, tlv_len);
500 set_sec_offset(pieces, IWL_UCODE_INIT,
501 IWL_UCODE_SECTION_INST,
502 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800503 break;
504 case IWL_UCODE_TLV_INIT_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800505 set_sec_data(pieces, IWL_UCODE_INIT,
506 IWL_UCODE_SECTION_DATA, tlv_data);
507 set_sec_size(pieces, IWL_UCODE_INIT,
508 IWL_UCODE_SECTION_DATA, tlv_len);
509 set_sec_offset(pieces, IWL_UCODE_INIT,
510 IWL_UCODE_SECTION_DATA,
511 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800512 break;
513 case IWL_UCODE_TLV_BOOT:
Johannes Berg965974a62012-03-06 13:30:38 -0800514 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800515 break;
516 case IWL_UCODE_TLV_PROBE_MAX_LEN:
517 if (tlv_len != sizeof(u32))
518 goto invalid_tlv_len;
519 capa->max_probe_length =
520 le32_to_cpup((__le32 *)tlv_data);
521 break;
522 case IWL_UCODE_TLV_PAN:
523 if (tlv_len)
524 goto invalid_tlv_len;
525 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
526 break;
527 case IWL_UCODE_TLV_FLAGS:
528 /* must be at least one u32 */
529 if (tlv_len < sizeof(u32))
530 goto invalid_tlv_len;
531 /* and a proper number of u32s */
532 if (tlv_len % sizeof(u32))
533 goto invalid_tlv_len;
534 /*
535 * This driver only reads the first u32 as
536 * right now no more features are defined,
537 * if that changes then either the driver
538 * will not work with the new firmware, or
539 * it'll not take advantage of new features.
540 */
541 capa->flags = le32_to_cpup((__le32 *)tlv_data);
542 break;
543 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
544 if (tlv_len != sizeof(u32))
545 goto invalid_tlv_len;
546 pieces->init_evtlog_ptr =
547 le32_to_cpup((__le32 *)tlv_data);
548 break;
549 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
550 if (tlv_len != sizeof(u32))
551 goto invalid_tlv_len;
552 pieces->init_evtlog_size =
553 le32_to_cpup((__le32 *)tlv_data);
554 break;
555 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
556 if (tlv_len != sizeof(u32))
557 goto invalid_tlv_len;
558 pieces->init_errlog_ptr =
559 le32_to_cpup((__le32 *)tlv_data);
560 break;
561 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
562 if (tlv_len != sizeof(u32))
563 goto invalid_tlv_len;
564 pieces->inst_evtlog_ptr =
565 le32_to_cpup((__le32 *)tlv_data);
566 break;
567 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
568 if (tlv_len != sizeof(u32))
569 goto invalid_tlv_len;
570 pieces->inst_evtlog_size =
571 le32_to_cpup((__le32 *)tlv_data);
572 break;
573 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
574 if (tlv_len != sizeof(u32))
575 goto invalid_tlv_len;
576 pieces->inst_errlog_ptr =
577 le32_to_cpup((__le32 *)tlv_data);
578 break;
579 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
580 if (tlv_len)
581 goto invalid_tlv_len;
Johannes Berg965974a62012-03-06 13:30:38 -0800582 drv->fw.enhance_sensitivity_table = true;
Johannes Berg15854ef92012-03-05 11:24:50 -0800583 break;
584 case IWL_UCODE_TLV_WOWLAN_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800585 set_sec_data(pieces, IWL_UCODE_WOWLAN,
586 IWL_UCODE_SECTION_INST, tlv_data);
587 set_sec_size(pieces, IWL_UCODE_WOWLAN,
588 IWL_UCODE_SECTION_INST, tlv_len);
589 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
590 IWL_UCODE_SECTION_INST,
591 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800592 break;
593 case IWL_UCODE_TLV_WOWLAN_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800594 set_sec_data(pieces, IWL_UCODE_WOWLAN,
595 IWL_UCODE_SECTION_DATA, tlv_data);
596 set_sec_size(pieces, IWL_UCODE_WOWLAN,
597 IWL_UCODE_SECTION_DATA, tlv_len);
598 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
599 IWL_UCODE_SECTION_DATA,
600 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800601 break;
602 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
603 if (tlv_len != sizeof(u32))
604 goto invalid_tlv_len;
605 capa->standard_phy_calibration_size =
606 le32_to_cpup((__le32 *)tlv_data);
607 break;
David Spinadeled8c8362012-03-10 13:00:13 -0800608 case IWL_UCODE_TLV_SEC_RT:
609 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
610 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800611 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800612 break;
613 case IWL_UCODE_TLV_SEC_INIT:
614 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
615 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800616 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800617 break;
618 case IWL_UCODE_TLV_SEC_WOWLAN:
619 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
620 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800621 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800622 break;
623 case IWL_UCODE_TLV_DEF_CALIB:
624 if (tlv_len != sizeof(struct iwl_tlv_calib_data))
625 goto invalid_tlv_len;
626 if (iwl_set_default_calib(drv, tlv_data))
627 goto tlv_error;
628 break;
629 case IWL_UCODE_TLV_PHY_SKU:
630 if (tlv_len != sizeof(u32))
631 goto invalid_tlv_len;
632 drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
633 break;
Johannes Berg15854ef92012-03-05 11:24:50 -0800634 default:
Johannes Berg965974a62012-03-06 13:30:38 -0800635 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
Johannes Berg15854ef92012-03-05 11:24:50 -0800636 break;
637 }
638 }
639
640 if (len) {
Johannes Berg965974a62012-03-06 13:30:38 -0800641 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
642 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
Johannes Berg15854ef92012-03-05 11:24:50 -0800643 return -EINVAL;
644 }
645
646 return 0;
647
648 invalid_tlv_len:
Johannes Berg965974a62012-03-06 13:30:38 -0800649 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
David Spinadeled8c8362012-03-10 13:00:13 -0800650 tlv_error:
Johannes Berg965974a62012-03-06 13:30:38 -0800651 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
Johannes Berg15854ef92012-03-05 11:24:50 -0800652
653 return -EINVAL;
654}
655
David Spinadel6dfa8d02012-03-10 13:00:14 -0800656static int alloc_pci_desc(struct iwl_drv *drv,
657 struct iwl_firmware_pieces *pieces,
658 enum iwl_ucode_type type)
659{
660 int i;
661 for (i = 0;
662 i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
663 i++)
664 if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
665 get_sec(pieces, type, i)))
666 return -1;
667 return 0;
668}
669
670static int validate_sec_sizes(struct iwl_drv *drv,
671 struct iwl_firmware_pieces *pieces,
672 const struct iwl_cfg *cfg)
673{
674 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
675 get_sec_size(pieces, IWL_UCODE_REGULAR,
676 IWL_UCODE_SECTION_INST));
677 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
678 get_sec_size(pieces, IWL_UCODE_REGULAR,
679 IWL_UCODE_SECTION_DATA));
680 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
681 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
682 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
683 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
684
685 /* Verify that uCode images will fit in card's SRAM. */
686 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
687 cfg->max_inst_size) {
688 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
689 get_sec_size(pieces, IWL_UCODE_REGULAR,
690 IWL_UCODE_SECTION_INST));
691 return -1;
692 }
693
694 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
695 cfg->max_data_size) {
696 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
697 get_sec_size(pieces, IWL_UCODE_REGULAR,
698 IWL_UCODE_SECTION_DATA));
699 return -1;
700 }
701
702 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
703 cfg->max_inst_size) {
704 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
705 get_sec_size(pieces, IWL_UCODE_INIT,
706 IWL_UCODE_SECTION_INST));
707 return -1;
708 }
709
710 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
711 cfg->max_data_size) {
712 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
713 get_sec_size(pieces, IWL_UCODE_REGULAR,
714 IWL_UCODE_SECTION_DATA));
715 return -1;
716 }
717 return 0;
718}
719
720
Johannes Berg15854ef92012-03-05 11:24:50 -0800721/**
722 * iwl_ucode_callback - callback when firmware was loaded
723 *
724 * If loaded successfully, copies the firmware into buffers
725 * for the card to fetch (via DMA).
726 */
727static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
728{
Johannes Berg965974a62012-03-06 13:30:38 -0800729 struct iwl_drv *drv = context;
Johannes Berg965974a62012-03-06 13:30:38 -0800730 struct iwl_fw *fw = &drv->fw;
Johannes Berg15854ef92012-03-05 11:24:50 -0800731 struct iwl_ucode_header *ucode;
732 int err;
David Spinadel0cedacc2012-03-10 13:00:12 -0800733 struct iwl_firmware_pieces pieces;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200734 const unsigned int api_max = drv->cfg->ucode_api_max;
735 unsigned int api_ok = drv->cfg->ucode_api_ok;
736 const unsigned int api_min = drv->cfg->ucode_api_min;
Johannes Berg15854ef92012-03-05 11:24:50 -0800737 u32 api_ver;
David Spinadel6dfa8d02012-03-10 13:00:14 -0800738 int i;
Johannes Berg15854ef92012-03-05 11:24:50 -0800739
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 Berg965974a62012-03-06 13:30:38 -0800750 if (drv->fw_index <= api_ok)
751 IWL_ERR(drv,
Johannes Berg15854ef92012-03-05 11:24:50 -0800752 "request for firmware file '%s' failed.\n",
Johannes Berg965974a62012-03-06 13:30:38 -0800753 drv->firmware_name);
Johannes Berg15854ef92012-03-05 11:24:50 -0800754 goto try_again;
755 }
756
Johannes Berg965974a62012-03-06 13:30:38 -0800757 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
758 drv->firmware_name, ucode_raw->size);
Johannes Berg15854ef92012-03-05 11:24:50 -0800759
760 /* Make sure that we got at least the API version number */
761 if (ucode_raw->size < 4) {
Johannes Berg965974a62012-03-06 13:30:38 -0800762 IWL_ERR(drv, "File size way too small!\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800763 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 Berg965974a62012-03-06 13:30:38 -0800770 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
Johannes Berg15854ef92012-03-05 11:24:50 -0800771 else
Johannes Berg965974a62012-03-06 13:30:38 -0800772 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
Johannes Berg15854ef92012-03-05 11:24:50 -0800773 &fw->ucode_capa);
774
775 if (err)
776 goto try_again;
777
Johannes Berg965974a62012-03-06 13:30:38 -0800778 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef92012-03-05 11:24:50 -0800779
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 Berg965974a62012-03-06 13:30:38 -0800786 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800787 if (api_ver < api_min || api_ver > api_max) {
Johannes Berg965974a62012-03-06 13:30:38 -0800788 IWL_ERR(drv,
Johannes Berg15854ef92012-03-05 11:24:50 -0800789 "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 Berg965974a62012-03-06 13:30:38 -0800797 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef92012-03-05 11:24:50 -0800798 "expected v%u through v%u, got v%u.\n",
799 api_ok, api_max, api_ver);
800 else
Johannes Berg965974a62012-03-06 13:30:38 -0800801 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef92012-03-05 11:24:50 -0800802 "expected v%u, got v%u.\n",
803 api_max, api_ver);
Johannes Berg965974a62012-03-06 13:30:38 -0800804 IWL_ERR(drv, "New firmware can be obtained from "
Johannes Berg15854ef92012-03-05 11:24:50 -0800805 "http://www.intellinuxwireless.org/.\n");
806 }
807 }
808
Johannes Berg965974a62012-03-06 13:30:38 -0800809 IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
Johannes Berg15854ef92012-03-05 11:24:50 -0800810
811 /*
David Spinadel4db2c9a2012-03-10 13:00:15 -0800812 * In mvm uCode there is no difference between data and instructions
813 * sections.
814 */
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200815 if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
Johannes Berg15854ef92012-03-05 11:24:50 -0800816 goto try_again;
Johannes Berg15854ef92012-03-05 11:24:50 -0800817
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 Spinadel6dfa8d02012-03-10 13:00:14 -0800823 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
824 if (alloc_pci_desc(drv, &pieces, i))
Johannes Berg15854ef92012-03-05 11:24:50 -0800825 goto err_pci_alloc;
Johannes Berg15854ef92012-03-05 11:24:50 -0800826
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 Berg0692fe42012-03-06 13:30:37 -0800834 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
Johannes Berg15854ef92012-03-05 11:24:50 -0800835 if (pieces.init_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800836 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
Johannes Berg15854ef92012-03-05 11:24:50 -0800837 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800838 fw->init_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200839 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800840 fw->init_errlog_ptr = pieces.init_errlog_ptr;
841 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
Johannes Berg15854ef92012-03-05 11:24:50 -0800842 if (pieces.inst_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800843 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
Johannes Berg15854ef92012-03-05 11:24:50 -0800844 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800845 fw->inst_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200846 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800847 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
Johannes Berg15854ef92012-03-05 11:24:50 -0800848
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 Berg965974a62012-03-06 13:30:38 -0800860 complete(&drv->request_firmware_complete);
Johannes Berg15854ef92012-03-05 11:24:50 -0800861
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200862 drv->op_mode = iwl_dvm_ops.start(drv->trans, drv->cfg, &drv->fw);
Johannes Berg15854ef92012-03-05 11:24:50 -0800863
Johannes Berg965974a62012-03-06 13:30:38 -0800864 if (!drv->op_mode)
Johannes Berg15854ef92012-03-05 11:24:50 -0800865 goto out_unbind;
866
867 return;
868
869 try_again:
870 /* try next, if any */
871 release_firmware(ucode_raw);
Johannes Berg965974a62012-03-06 13:30:38 -0800872 if (iwl_request_firmware(drv, false))
Johannes Berg15854ef92012-03-05 11:24:50 -0800873 goto out_unbind;
874 return;
875
876 err_pci_alloc:
Johannes Berg965974a62012-03-06 13:30:38 -0800877 IWL_ERR(drv, "failed to allocate pci memory\n");
878 iwl_dealloc_ucode(drv);
Johannes Berg15854ef92012-03-05 11:24:50 -0800879 release_firmware(ucode_raw);
880 out_unbind:
Johannes Berg965974a62012-03-06 13:30:38 -0800881 complete(&drv->request_firmware_complete);
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200882 device_release_driver(drv->trans->dev);
Johannes Berg15854ef92012-03-05 11:24:50 -0800883}
884
Emmanuel Grumbach87ce05a2012-03-26 09:03:18 -0700885struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700886 const struct iwl_cfg *cfg)
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200887{
Johannes Berg965974a62012-03-06 13:30:38 -0800888 struct iwl_drv *drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200889 int ret;
890
Johannes Berg965974a62012-03-06 13:30:38 -0800891 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
892 if (!drv) {
893 dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_drv");
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700894 return NULL;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200895 }
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700896 drv->trans = trans;
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +0200897 drv->dev = trans->dev;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200898 drv->cfg = cfg;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200899
Johannes Berg965974a62012-03-06 13:30:38 -0800900 init_completion(&drv->request_firmware_complete);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200901
Johannes Berg965974a62012-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 Berg965974a62012-03-06 13:30:38 -0800906 kfree(drv);
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700907 drv = NULL;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200908 }
909
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700910 return drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200911}
912
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700913void iwl_drv_stop(struct iwl_drv *drv)
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200914{
Johannes Berg965974a62012-03-06 13:30:38 -0800915 wait_for_completion(&drv->request_firmware_complete);
Johannes Berg2e7eb112012-03-05 11:24:51 -0800916
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200917 /* op_mode can be NULL if its start failed */
Johannes Berg965974a62012-03-06 13:30:38 -0800918 if (drv->op_mode)
919 iwl_op_mode_stop(drv->op_mode);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200920
Johannes Berg965974a62012-03-06 13:30:38 -0800921 iwl_dealloc_ucode(drv);
Johannes Berg7db5b982012-03-05 11:24:48 -0800922
Johannes Berg965974a62012-03-06 13:30:38 -0800923 kfree(drv);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200924}