blob: 28d81a793641bae963370c722c6c7e3a81a7c3e6 [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"
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"
Johannes Berg6238b002012-04-02 15:04:33 +020072#include "iwl-fw.h"
73#include "iwl-config.h"
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020074
Johannes Berg0692fe42012-03-06 13:30:37 -080075/* private includes */
Johannes Berg3995dea2012-03-06 13:30:44 -080076#include "iwl-fw-file.h"
Johannes Berg0692fe42012-03-06 13:30:37 -080077
Johannes Berg965974a2012-03-06 13:30:38 -080078/**
79 * struct iwl_drv - drv common data
80 * @fw: the iwl_fw structure
Johannes Berg965974a2012-03-06 13:30:38 -080081 * @op_mode: the running op_mode
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +020082 * @trans: transport layer
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +020083 * @dev: for debug prints only
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +020084 * @cfg: configuration struct
Johannes Berg965974a2012-03-06 13:30:38 -080085 * @fw_index: firmware revision to try loading
86 * @firmware_name: composite filename of ucode file to load
87 * @request_firmware_complete: the firmware has been obtained from user space
88 */
89struct iwl_drv {
90 struct iwl_fw fw;
91
Johannes Berg965974a2012-03-06 13:30:38 -080092 struct iwl_op_mode *op_mode;
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -070093 struct iwl_trans *trans;
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +020094 struct device *dev;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +020095 const struct iwl_cfg *cfg;
Johannes Berg965974a2012-03-06 13:30:38 -080096
97 int fw_index; /* firmware we're trying to load */
98 char firmware_name[25]; /* name of firmware file to load */
99
100 struct completion request_firmware_complete;
101};
102
103
104
David Spinadel0cedacc2012-03-10 13:00:12 -0800105/*
106 * struct fw_sec: Just for the image parsing proccess.
107 * For the fw storage we are using struct fw_desc.
108 */
109struct fw_sec {
110 const void *data; /* the sec data */
111 size_t size; /* section size */
112 u32 offset; /* offset of writing in the device */
113};
114
Johannes Berg965974a2012-03-06 13:30:38 -0800115static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
Johannes Berg15854ef2012-03-05 11:24:50 -0800116{
117 if (desc->v_addr)
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200118 dma_free_coherent(drv->trans->dev, desc->len,
Johannes Berg15854ef2012-03-05 11:24:50 -0800119 desc->v_addr, desc->p_addr);
120 desc->v_addr = NULL;
121 desc->len = 0;
122}
123
Johannes Berg965974a2012-03-06 13:30:38 -0800124static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
Johannes Berg15854ef2012-03-05 11:24:50 -0800125{
David Spinadel6dfa8d02012-03-10 13:00:14 -0800126 int i;
127 for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
128 iwl_free_fw_desc(drv, &img->sec[i]);
Johannes Berg15854ef2012-03-05 11:24:50 -0800129}
130
Johannes Berg965974a2012-03-06 13:30:38 -0800131static void iwl_dealloc_ucode(struct iwl_drv *drv)
Johannes Berg15854ef2012-03-05 11:24:50 -0800132{
David Spinadel6dfa8d02012-03-10 13:00:14 -0800133 int i;
134 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
135 iwl_free_fw_img(drv, drv->fw.img + i);
Johannes Berg15854ef2012-03-05 11:24:50 -0800136}
137
Johannes Berg965974a2012-03-06 13:30:38 -0800138static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
David Spinadel0cedacc2012-03-10 13:00:12 -0800139 struct fw_sec *sec)
Johannes Berg15854ef2012-03-05 11:24:50 -0800140{
David Spinadel0cedacc2012-03-10 13:00:12 -0800141 if (!sec || !sec->size) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800142 desc->v_addr = NULL;
143 return -EINVAL;
144 }
145
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200146 desc->v_addr = dma_alloc_coherent(drv->trans->dev, sec->size,
Johannes Berg15854ef2012-03-05 11:24:50 -0800147 &desc->p_addr, GFP_KERNEL);
148 if (!desc->v_addr)
149 return -ENOMEM;
150
David Spinadel0cedacc2012-03-10 13:00:12 -0800151 desc->len = sec->size;
152 desc->offset = sec->offset;
153 memcpy(desc->v_addr, sec->data, sec->size);
Johannes Berg15854ef2012-03-05 11:24:50 -0800154 return 0;
155}
156
157static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
158
159#define UCODE_EXPERIMENTAL_INDEX 100
160#define UCODE_EXPERIMENTAL_TAG "exp"
161
Johannes Berg965974a2012-03-06 13:30:38 -0800162static int iwl_request_firmware(struct iwl_drv *drv, bool first)
Johannes Berg15854ef2012-03-05 11:24:50 -0800163{
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200164 const char *name_pre = drv->cfg->fw_name_pre;
Johannes Berg15854ef2012-03-05 11:24:50 -0800165 char tag[8];
166
167 if (first) {
168#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
Johannes Berg965974a2012-03-06 13:30:38 -0800169 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
Johannes Berg15854ef2012-03-05 11:24:50 -0800170 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
Johannes Berg965974a2012-03-06 13:30:38 -0800171 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800172#endif
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200173 drv->fw_index = drv->cfg->ucode_api_max;
Johannes Berg965974a2012-03-06 13:30:38 -0800174 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800175 } else {
Johannes Berg965974a2012-03-06 13:30:38 -0800176 drv->fw_index--;
177 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800178 }
179
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200180 if (drv->fw_index < drv->cfg->ucode_api_min) {
Johannes Berg965974a2012-03-06 13:30:38 -0800181 IWL_ERR(drv, "no suitable firmware found!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800182 return -ENOENT;
183 }
184
Johannes Berg965974a2012-03-06 13:30:38 -0800185 sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Johannes Berg15854ef2012-03-05 11:24:50 -0800186
Johannes Berg965974a2012-03-06 13:30:38 -0800187 IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
188 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800189 ? "EXPERIMENTAL " : "",
Johannes Berg965974a2012-03-06 13:30:38 -0800190 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800191
Johannes Berg965974a2012-03-06 13:30:38 -0800192 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200193 drv->trans->dev,
Johannes Berg965974a2012-03-06 13:30:38 -0800194 GFP_KERNEL, drv, iwl_ucode_callback);
Johannes Berg15854ef2012-03-05 11:24:50 -0800195}
196
David Spinadel0cedacc2012-03-10 13:00:12 -0800197struct fw_img_parsing {
David Spinadel6dfa8d02012-03-10 13:00:14 -0800198 struct fw_sec sec[IWL_UCODE_SECTION_MAX];
David Spinadel0cedacc2012-03-10 13:00:12 -0800199 int sec_counter;
200};
201
David Spinadeled8c8362012-03-10 13:00:13 -0800202/*
203 * struct fw_sec_parsing: to extract fw section and it's offset from tlv
204 */
205struct fw_sec_parsing {
206 __le32 offset;
207 const u8 data[];
208} __packed;
209
210/**
211 * struct iwl_tlv_calib_data - parse the default calib data from TLV
212 *
213 * @ucode_type: the uCode to which the following default calib relates.
214 * @calib: default calibrations.
215 */
216struct iwl_tlv_calib_data {
217 __le32 ucode_type;
218 __le64 calib;
219} __packed;
220
David Spinadel0cedacc2012-03-10 13:00:12 -0800221struct iwl_firmware_pieces {
222 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
Johannes Berg15854ef2012-03-05 11:24:50 -0800223
224 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
225 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
226};
227
David Spinadel0cedacc2012-03-10 13:00:12 -0800228/*
229 * These functions are just to extract uCode section data from the pieces
230 * structure.
231 */
232static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
233 enum iwl_ucode_type type,
234 int sec)
235{
236 return &pieces->img[type].sec[sec];
237}
238
239static void set_sec_data(struct iwl_firmware_pieces *pieces,
240 enum iwl_ucode_type type,
241 int sec,
242 const void *data)
243{
244 pieces->img[type].sec[sec].data = data;
245}
246
247static void set_sec_size(struct iwl_firmware_pieces *pieces,
248 enum iwl_ucode_type type,
249 int sec,
250 size_t size)
251{
252 pieces->img[type].sec[sec].size = size;
253}
254
255static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
256 enum iwl_ucode_type type,
257 int sec)
258{
259 return pieces->img[type].sec[sec].size;
260}
261
262static void set_sec_offset(struct iwl_firmware_pieces *pieces,
263 enum iwl_ucode_type type,
264 int sec,
265 u32 offset)
266{
267 pieces->img[type].sec[sec].offset = offset;
268}
269
David Spinadeled8c8362012-03-10 13:00:13 -0800270/*
271 * Gets uCode section from tlv.
272 */
273static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
274 const void *data, enum iwl_ucode_type type,
275 int size)
276{
277 struct fw_img_parsing *img;
278 struct fw_sec *sec;
279 struct fw_sec_parsing *sec_parse;
280
281 if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
282 return -1;
283
284 sec_parse = (struct fw_sec_parsing *)data;
285
286 img = &pieces->img[type];
287 sec = &img->sec[img->sec_counter];
288
289 sec->offset = le32_to_cpu(sec_parse->offset);
290 sec->data = sec_parse->data;
David Spinadel1176f432012-03-13 14:32:48 +0200291 sec->size = size - sizeof(sec_parse->offset);
David Spinadeled8c8362012-03-10 13:00:13 -0800292
293 ++img->sec_counter;
294
295 return 0;
296}
297
298static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
299{
300 struct iwl_tlv_calib_data *def_calib =
301 (struct iwl_tlv_calib_data *)data;
302 u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
303 if (ucode_type >= IWL_UCODE_TYPE_MAX) {
304 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
305 ucode_type);
306 return -EINVAL;
307 }
308 drv->fw.default_calib[ucode_type] = le64_to_cpu(def_calib->calib);
309 return 0;
310}
311
Johannes Berg965974a2012-03-06 13:30:38 -0800312static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
David Spinadel0cedacc2012-03-10 13:00:12 -0800313 const struct firmware *ucode_raw,
314 struct iwl_firmware_pieces *pieces)
Johannes Berg15854ef2012-03-05 11:24:50 -0800315{
316 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
317 u32 api_ver, hdr_size, build;
318 char buildstr[25];
319 const u8 *src;
320
Johannes Berg965974a2012-03-06 13:30:38 -0800321 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
322 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800323
324 switch (api_ver) {
325 default:
326 hdr_size = 28;
327 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800328 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800329 return -EINVAL;
330 }
331 build = le32_to_cpu(ucode->u.v2.build);
David Spinadel0cedacc2012-03-10 13:00:12 -0800332 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
333 le32_to_cpu(ucode->u.v2.inst_size));
334 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
335 le32_to_cpu(ucode->u.v2.data_size));
336 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
337 le32_to_cpu(ucode->u.v2.init_size));
338 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
339 le32_to_cpu(ucode->u.v2.init_data_size));
Johannes Berg15854ef2012-03-05 11:24:50 -0800340 src = ucode->u.v2.data;
341 break;
342 case 0:
343 case 1:
344 case 2:
345 hdr_size = 24;
346 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800347 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800348 return -EINVAL;
349 }
350 build = 0;
David Spinadel0cedacc2012-03-10 13:00:12 -0800351 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
352 le32_to_cpu(ucode->u.v1.inst_size));
353 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
354 le32_to_cpu(ucode->u.v1.data_size));
355 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
356 le32_to_cpu(ucode->u.v1.init_size));
357 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
358 le32_to_cpu(ucode->u.v1.init_data_size));
Johannes Berg15854ef2012-03-05 11:24:50 -0800359 src = ucode->u.v1.data;
360 break;
361 }
362
363 if (build)
364 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800365 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800366 ? " (EXP)" : "");
367 else
368 buildstr[0] = '\0';
369
Johannes Berg965974a2012-03-06 13:30:38 -0800370 snprintf(drv->fw.fw_version,
371 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800372 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800373 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
374 IWL_UCODE_MINOR(drv->fw.ucode_ver),
375 IWL_UCODE_API(drv->fw.ucode_ver),
376 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef2012-03-05 11:24:50 -0800377 buildstr);
378
379 /* Verify size of file vs. image size info in file's header */
David Spinadel0cedacc2012-03-10 13:00:12 -0800380
381 if (ucode_raw->size != hdr_size +
382 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
383 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
384 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
385 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800386
Johannes Berg965974a2012-03-06 13:30:38 -0800387 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800388 "uCode file size %d does not match expected size\n",
389 (int)ucode_raw->size);
390 return -EINVAL;
391 }
392
Johannes Berg15854ef2012-03-05 11:24:50 -0800393
David Spinadel0cedacc2012-03-10 13:00:12 -0800394 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
395 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
396 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
397 IWLAGN_RTC_INST_LOWER_BOUND);
398 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
399 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
400 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
401 IWLAGN_RTC_DATA_LOWER_BOUND);
402 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
403 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
404 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
405 IWLAGN_RTC_INST_LOWER_BOUND);
406 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
407 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
408 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
409 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800410 return 0;
411}
412
Johannes Berg965974a2012-03-06 13:30:38 -0800413static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800414 const struct firmware *ucode_raw,
David Spinadel0cedacc2012-03-10 13:00:12 -0800415 struct iwl_firmware_pieces *pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -0800416 struct iwl_ucode_capabilities *capa)
417{
418 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
419 struct iwl_ucode_tlv *tlv;
420 size_t len = ucode_raw->size;
421 const u8 *data;
Johannes Berg15854ef2012-03-05 11:24:50 -0800422 u32 tlv_len;
423 enum iwl_ucode_tlv_type tlv_type;
424 const u8 *tlv_data;
425 char buildstr[25];
426 u32 build;
427
428 if (len < sizeof(*ucode)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800429 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800430 return -EINVAL;
431 }
432
433 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800434 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800435 le32_to_cpu(ucode->magic));
436 return -EINVAL;
437 }
438
Johannes Berg965974a2012-03-06 13:30:38 -0800439 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800440 build = le32_to_cpu(ucode->build);
441
442 if (build)
443 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800444 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800445 ? " (EXP)" : "");
446 else
447 buildstr[0] = '\0';
448
Johannes Berg965974a2012-03-06 13:30:38 -0800449 snprintf(drv->fw.fw_version,
450 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800451 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800452 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
453 IWL_UCODE_MINOR(drv->fw.ucode_ver),
454 IWL_UCODE_API(drv->fw.ucode_ver),
455 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef2012-03-05 11:24:50 -0800456 buildstr);
457
458 data = ucode->data;
459
460 len -= sizeof(*ucode);
461
462 while (len >= sizeof(*tlv)) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800463 len -= sizeof(*tlv);
464 tlv = (void *)data;
465
466 tlv_len = le32_to_cpu(tlv->length);
Johannes Berg0479c192012-03-09 09:16:35 +0100467 tlv_type = le32_to_cpu(tlv->type);
Johannes Berg15854ef2012-03-05 11:24:50 -0800468 tlv_data = tlv->data;
469
470 if (len < tlv_len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800471 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800472 len, tlv_len);
473 return -EINVAL;
474 }
475 len -= ALIGN(tlv_len, 4);
476 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
477
Johannes Berg15854ef2012-03-05 11:24:50 -0800478 switch (tlv_type) {
479 case IWL_UCODE_TLV_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800480 set_sec_data(pieces, IWL_UCODE_REGULAR,
481 IWL_UCODE_SECTION_INST, tlv_data);
482 set_sec_size(pieces, IWL_UCODE_REGULAR,
483 IWL_UCODE_SECTION_INST, tlv_len);
484 set_sec_offset(pieces, IWL_UCODE_REGULAR,
485 IWL_UCODE_SECTION_INST,
486 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800487 break;
488 case IWL_UCODE_TLV_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800489 set_sec_data(pieces, IWL_UCODE_REGULAR,
490 IWL_UCODE_SECTION_DATA, tlv_data);
491 set_sec_size(pieces, IWL_UCODE_REGULAR,
492 IWL_UCODE_SECTION_DATA, tlv_len);
493 set_sec_offset(pieces, IWL_UCODE_REGULAR,
494 IWL_UCODE_SECTION_DATA,
495 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800496 break;
497 case IWL_UCODE_TLV_INIT:
David Spinadel0cedacc2012-03-10 13:00:12 -0800498 set_sec_data(pieces, IWL_UCODE_INIT,
499 IWL_UCODE_SECTION_INST, tlv_data);
500 set_sec_size(pieces, IWL_UCODE_INIT,
501 IWL_UCODE_SECTION_INST, tlv_len);
502 set_sec_offset(pieces, IWL_UCODE_INIT,
503 IWL_UCODE_SECTION_INST,
504 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800505 break;
506 case IWL_UCODE_TLV_INIT_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800507 set_sec_data(pieces, IWL_UCODE_INIT,
508 IWL_UCODE_SECTION_DATA, tlv_data);
509 set_sec_size(pieces, IWL_UCODE_INIT,
510 IWL_UCODE_SECTION_DATA, tlv_len);
511 set_sec_offset(pieces, IWL_UCODE_INIT,
512 IWL_UCODE_SECTION_DATA,
513 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800514 break;
515 case IWL_UCODE_TLV_BOOT:
Johannes Berg965974a2012-03-06 13:30:38 -0800516 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800517 break;
518 case IWL_UCODE_TLV_PROBE_MAX_LEN:
519 if (tlv_len != sizeof(u32))
520 goto invalid_tlv_len;
521 capa->max_probe_length =
522 le32_to_cpup((__le32 *)tlv_data);
523 break;
524 case IWL_UCODE_TLV_PAN:
525 if (tlv_len)
526 goto invalid_tlv_len;
527 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
528 break;
529 case IWL_UCODE_TLV_FLAGS:
530 /* must be at least one u32 */
531 if (tlv_len < sizeof(u32))
532 goto invalid_tlv_len;
533 /* and a proper number of u32s */
534 if (tlv_len % sizeof(u32))
535 goto invalid_tlv_len;
536 /*
537 * This driver only reads the first u32 as
538 * right now no more features are defined,
539 * if that changes then either the driver
540 * will not work with the new firmware, or
541 * it'll not take advantage of new features.
542 */
543 capa->flags = le32_to_cpup((__le32 *)tlv_data);
544 break;
545 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
546 if (tlv_len != sizeof(u32))
547 goto invalid_tlv_len;
548 pieces->init_evtlog_ptr =
549 le32_to_cpup((__le32 *)tlv_data);
550 break;
551 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
552 if (tlv_len != sizeof(u32))
553 goto invalid_tlv_len;
554 pieces->init_evtlog_size =
555 le32_to_cpup((__le32 *)tlv_data);
556 break;
557 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
558 if (tlv_len != sizeof(u32))
559 goto invalid_tlv_len;
560 pieces->init_errlog_ptr =
561 le32_to_cpup((__le32 *)tlv_data);
562 break;
563 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
564 if (tlv_len != sizeof(u32))
565 goto invalid_tlv_len;
566 pieces->inst_evtlog_ptr =
567 le32_to_cpup((__le32 *)tlv_data);
568 break;
569 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
570 if (tlv_len != sizeof(u32))
571 goto invalid_tlv_len;
572 pieces->inst_evtlog_size =
573 le32_to_cpup((__le32 *)tlv_data);
574 break;
575 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
576 if (tlv_len != sizeof(u32))
577 goto invalid_tlv_len;
578 pieces->inst_errlog_ptr =
579 le32_to_cpup((__le32 *)tlv_data);
580 break;
581 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
582 if (tlv_len)
583 goto invalid_tlv_len;
Johannes Berg965974a2012-03-06 13:30:38 -0800584 drv->fw.enhance_sensitivity_table = true;
Johannes Berg15854ef2012-03-05 11:24:50 -0800585 break;
586 case IWL_UCODE_TLV_WOWLAN_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800587 set_sec_data(pieces, IWL_UCODE_WOWLAN,
588 IWL_UCODE_SECTION_INST, tlv_data);
589 set_sec_size(pieces, IWL_UCODE_WOWLAN,
590 IWL_UCODE_SECTION_INST, tlv_len);
591 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
592 IWL_UCODE_SECTION_INST,
593 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800594 break;
595 case IWL_UCODE_TLV_WOWLAN_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800596 set_sec_data(pieces, IWL_UCODE_WOWLAN,
597 IWL_UCODE_SECTION_DATA, tlv_data);
598 set_sec_size(pieces, IWL_UCODE_WOWLAN,
599 IWL_UCODE_SECTION_DATA, tlv_len);
600 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
601 IWL_UCODE_SECTION_DATA,
602 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800603 break;
604 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
605 if (tlv_len != sizeof(u32))
606 goto invalid_tlv_len;
607 capa->standard_phy_calibration_size =
608 le32_to_cpup((__le32 *)tlv_data);
609 break;
David Spinadeled8c8362012-03-10 13:00:13 -0800610 case IWL_UCODE_TLV_SEC_RT:
611 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
612 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800613 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800614 break;
615 case IWL_UCODE_TLV_SEC_INIT:
616 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
617 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800618 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800619 break;
620 case IWL_UCODE_TLV_SEC_WOWLAN:
621 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
622 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800623 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800624 break;
625 case IWL_UCODE_TLV_DEF_CALIB:
626 if (tlv_len != sizeof(struct iwl_tlv_calib_data))
627 goto invalid_tlv_len;
628 if (iwl_set_default_calib(drv, tlv_data))
629 goto tlv_error;
630 break;
631 case IWL_UCODE_TLV_PHY_SKU:
632 if (tlv_len != sizeof(u32))
633 goto invalid_tlv_len;
634 drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
635 break;
Johannes Berg15854ef2012-03-05 11:24:50 -0800636 default:
Johannes Berg965974a2012-03-06 13:30:38 -0800637 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
Johannes Berg15854ef2012-03-05 11:24:50 -0800638 break;
639 }
640 }
641
642 if (len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800643 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
644 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800645 return -EINVAL;
646 }
647
648 return 0;
649
650 invalid_tlv_len:
Johannes Berg965974a2012-03-06 13:30:38 -0800651 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
David Spinadeled8c8362012-03-10 13:00:13 -0800652 tlv_error:
Johannes Berg965974a2012-03-06 13:30:38 -0800653 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800654
655 return -EINVAL;
656}
657
David Spinadel6dfa8d02012-03-10 13:00:14 -0800658static int alloc_pci_desc(struct iwl_drv *drv,
659 struct iwl_firmware_pieces *pieces,
660 enum iwl_ucode_type type)
661{
662 int i;
663 for (i = 0;
664 i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
665 i++)
666 if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
667 get_sec(pieces, type, i)))
668 return -1;
669 return 0;
670}
671
672static int validate_sec_sizes(struct iwl_drv *drv,
673 struct iwl_firmware_pieces *pieces,
674 const struct iwl_cfg *cfg)
675{
676 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
677 get_sec_size(pieces, IWL_UCODE_REGULAR,
678 IWL_UCODE_SECTION_INST));
679 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
680 get_sec_size(pieces, IWL_UCODE_REGULAR,
681 IWL_UCODE_SECTION_DATA));
682 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
683 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
684 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
685 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
686
687 /* Verify that uCode images will fit in card's SRAM. */
688 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
689 cfg->max_inst_size) {
690 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
691 get_sec_size(pieces, IWL_UCODE_REGULAR,
692 IWL_UCODE_SECTION_INST));
693 return -1;
694 }
695
696 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
697 cfg->max_data_size) {
698 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
699 get_sec_size(pieces, IWL_UCODE_REGULAR,
700 IWL_UCODE_SECTION_DATA));
701 return -1;
702 }
703
704 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
705 cfg->max_inst_size) {
706 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
707 get_sec_size(pieces, IWL_UCODE_INIT,
708 IWL_UCODE_SECTION_INST));
709 return -1;
710 }
711
712 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
713 cfg->max_data_size) {
714 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
715 get_sec_size(pieces, IWL_UCODE_REGULAR,
716 IWL_UCODE_SECTION_DATA));
717 return -1;
718 }
719 return 0;
720}
721
722
Johannes Berg15854ef2012-03-05 11:24:50 -0800723/**
724 * iwl_ucode_callback - callback when firmware was loaded
725 *
726 * If loaded successfully, copies the firmware into buffers
727 * for the card to fetch (via DMA).
728 */
729static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
730{
Johannes Berg965974a2012-03-06 13:30:38 -0800731 struct iwl_drv *drv = context;
Johannes Berg965974a2012-03-06 13:30:38 -0800732 struct iwl_fw *fw = &drv->fw;
Johannes Berg15854ef2012-03-05 11:24:50 -0800733 struct iwl_ucode_header *ucode;
734 int err;
David Spinadel0cedacc2012-03-10 13:00:12 -0800735 struct iwl_firmware_pieces pieces;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200736 const unsigned int api_max = drv->cfg->ucode_api_max;
737 unsigned int api_ok = drv->cfg->ucode_api_ok;
738 const unsigned int api_min = drv->cfg->ucode_api_min;
Johannes Berg15854ef2012-03-05 11:24:50 -0800739 u32 api_ver;
David Spinadel6dfa8d02012-03-10 13:00:14 -0800740 int i;
Johannes Berg15854ef2012-03-05 11:24:50 -0800741
742 fw->ucode_capa.max_probe_length = 200;
743 fw->ucode_capa.standard_phy_calibration_size =
744 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
745
746 if (!api_ok)
747 api_ok = api_max;
748
749 memset(&pieces, 0, sizeof(pieces));
750
751 if (!ucode_raw) {
Johannes Berg965974a2012-03-06 13:30:38 -0800752 if (drv->fw_index <= api_ok)
753 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800754 "request for firmware file '%s' failed.\n",
Johannes Berg965974a2012-03-06 13:30:38 -0800755 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800756 goto try_again;
757 }
758
Johannes Berg965974a2012-03-06 13:30:38 -0800759 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
760 drv->firmware_name, ucode_raw->size);
Johannes Berg15854ef2012-03-05 11:24:50 -0800761
762 /* Make sure that we got at least the API version number */
763 if (ucode_raw->size < 4) {
Johannes Berg965974a2012-03-06 13:30:38 -0800764 IWL_ERR(drv, "File size way too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800765 goto try_again;
766 }
767
768 /* Data from ucode file: header followed by uCode images */
769 ucode = (struct iwl_ucode_header *)ucode_raw->data;
770
771 if (ucode->ver)
Johannes Berg965974a2012-03-06 13:30:38 -0800772 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
Johannes Berg15854ef2012-03-05 11:24:50 -0800773 else
Johannes Berg965974a2012-03-06 13:30:38 -0800774 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -0800775 &fw->ucode_capa);
776
777 if (err)
778 goto try_again;
779
Johannes Berg965974a2012-03-06 13:30:38 -0800780 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800781
782 /*
783 * api_ver should match the api version forming part of the
784 * firmware filename ... but we don't check for that and only rely
785 * on the API version read from firmware header from here on forward
786 */
787 /* no api version check required for experimental uCode */
Johannes Berg965974a2012-03-06 13:30:38 -0800788 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800789 if (api_ver < api_min || api_ver > api_max) {
Johannes Berg965974a2012-03-06 13:30:38 -0800790 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800791 "Driver unable to support your firmware API. "
792 "Driver supports v%u, firmware is v%u.\n",
793 api_max, api_ver);
794 goto try_again;
795 }
796
797 if (api_ver < api_ok) {
798 if (api_ok != api_max)
Johannes Berg965974a2012-03-06 13:30:38 -0800799 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -0800800 "expected v%u through v%u, got v%u.\n",
801 api_ok, api_max, api_ver);
802 else
Johannes Berg965974a2012-03-06 13:30:38 -0800803 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -0800804 "expected v%u, got v%u.\n",
805 api_max, api_ver);
Johannes Berg965974a2012-03-06 13:30:38 -0800806 IWL_ERR(drv, "New firmware can be obtained from "
Johannes Berg15854ef2012-03-05 11:24:50 -0800807 "http://www.intellinuxwireless.org/.\n");
808 }
809 }
810
Johannes Berg965974a2012-03-06 13:30:38 -0800811 IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
Johannes Berg15854ef2012-03-05 11:24:50 -0800812
813 /*
David Spinadel4db2c9a2012-03-10 13:00:15 -0800814 * In mvm uCode there is no difference between data and instructions
815 * sections.
816 */
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200817 if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
Johannes Berg15854ef2012-03-05 11:24:50 -0800818 goto try_again;
Johannes Berg15854ef2012-03-05 11:24:50 -0800819
820 /* Allocate ucode buffers for card's bus-master loading ... */
821
822 /* Runtime instructions and 2 copies of data:
823 * 1) unmodified from disk
824 * 2) backup cache for save/restore during power-downs */
David Spinadel6dfa8d02012-03-10 13:00:14 -0800825 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
826 if (alloc_pci_desc(drv, &pieces, i))
Johannes Berg15854ef2012-03-05 11:24:50 -0800827 goto err_pci_alloc;
Johannes Berg15854ef2012-03-05 11:24:50 -0800828
829 /* Now that we can no longer fail, copy information */
830
831 /*
832 * The (size - 16) / 12 formula is based on the information recorded
833 * for each event, which is of mode 1 (including timestamp) for all
834 * new microcodes that include this information.
835 */
Johannes Berg0692fe42012-03-06 13:30:37 -0800836 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800837 if (pieces.init_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800838 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -0800839 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800840 fw->init_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200841 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800842 fw->init_errlog_ptr = pieces.init_errlog_ptr;
843 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800844 if (pieces.inst_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800845 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -0800846 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800847 fw->inst_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200848 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800849 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800850
851 /*
852 * figure out the offset of chain noise reset and gain commands
853 * base on the size of standard phy calibration commands table size
854 */
855 if (fw->ucode_capa.standard_phy_calibration_size >
856 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
857 fw->ucode_capa.standard_phy_calibration_size =
858 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
859
860 /* We have our copies now, allow OS release its copies */
861 release_firmware(ucode_raw);
Johannes Berg965974a2012-03-06 13:30:38 -0800862 complete(&drv->request_firmware_complete);
Johannes Berg15854ef2012-03-05 11:24:50 -0800863
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200864 drv->op_mode = iwl_dvm_ops.start(drv->trans, drv->cfg, &drv->fw);
Johannes Berg15854ef2012-03-05 11:24:50 -0800865
Johannes Berg965974a2012-03-06 13:30:38 -0800866 if (!drv->op_mode)
Johannes Berg15854ef2012-03-05 11:24:50 -0800867 goto out_unbind;
868
869 return;
870
871 try_again:
872 /* try next, if any */
873 release_firmware(ucode_raw);
Johannes Berg965974a2012-03-06 13:30:38 -0800874 if (iwl_request_firmware(drv, false))
Johannes Berg15854ef2012-03-05 11:24:50 -0800875 goto out_unbind;
876 return;
877
878 err_pci_alloc:
Johannes Berg965974a2012-03-06 13:30:38 -0800879 IWL_ERR(drv, "failed to allocate pci memory\n");
880 iwl_dealloc_ucode(drv);
Johannes Berg15854ef2012-03-05 11:24:50 -0800881 release_firmware(ucode_raw);
882 out_unbind:
Johannes Berg965974a2012-03-06 13:30:38 -0800883 complete(&drv->request_firmware_complete);
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200884 device_release_driver(drv->trans->dev);
Johannes Berg15854ef2012-03-05 11:24:50 -0800885}
886
Emmanuel Grumbach87ce05a2012-03-26 09:03:18 -0700887struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700888 const struct iwl_cfg *cfg)
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200889{
Johannes Berg965974a2012-03-06 13:30:38 -0800890 struct iwl_drv *drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200891 int ret;
892
Johannes Berg965974a2012-03-06 13:30:38 -0800893 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
894 if (!drv) {
895 dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_drv");
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700896 return NULL;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200897 }
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700898 drv->trans = trans;
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +0200899 drv->dev = trans->dev;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200900 drv->cfg = cfg;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200901
Johannes Berg965974a2012-03-06 13:30:38 -0800902 init_completion(&drv->request_firmware_complete);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200903
Johannes Berg965974a2012-03-06 13:30:38 -0800904 ret = iwl_request_firmware(drv, true);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200905
906 if (ret) {
907 dev_printk(KERN_ERR, trans->dev, "Couldn't request the fw");
Johannes Berg965974a2012-03-06 13:30:38 -0800908 kfree(drv);
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700909 drv = NULL;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200910 }
911
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700912 return drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200913}
914
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700915void iwl_drv_stop(struct iwl_drv *drv)
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200916{
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);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200926}