blob: 19ccd36a55b3d441a64773ad09b18aab7eedcf87 [file] [log] [blame]
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2007 - 2012 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63#include <linux/completion.h>
Johannes Berg15854ef2012-03-05 11:24:50 -080064#include <linux/dma-mapping.h>
65#include <linux/firmware.h>
66#include <linux/module.h>
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020067
68#include "iwl-drv.h"
69#include "iwl-trans.h"
Johannes Berg15854ef2012-03-05 11:24:50 -080070#include "iwl-shared.h"
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +020071#include "iwl-op-mode.h"
David Spinadel0cedacc2012-03-10 13:00:12 -080072#include "iwl-agn-hw.h"
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020073
Johannes Berg0692fe42012-03-06 13:30:37 -080074/* private includes */
Johannes Berg3995dea2012-03-06 13:30:44 -080075#include "iwl-fw-file.h"
Johannes Berg0692fe42012-03-06 13:30:37 -080076
Johannes Berg965974a2012-03-06 13:30:38 -080077/**
78 * struct iwl_drv - drv common data
79 * @fw: the iwl_fw structure
80 * @shrd: pointer to common shared structure
81 * @op_mode: the running op_mode
82 * @fw_index: firmware revision to try loading
83 * @firmware_name: composite filename of ucode file to load
84 * @request_firmware_complete: the firmware has been obtained from user space
85 */
86struct iwl_drv {
87 struct iwl_fw fw;
88
89 struct iwl_shared *shrd;
90 struct iwl_op_mode *op_mode;
91
92 int fw_index; /* firmware we're trying to load */
93 char firmware_name[25]; /* name of firmware file to load */
94
95 struct completion request_firmware_complete;
96};
97
98
99
David Spinadel0cedacc2012-03-10 13:00:12 -0800100/*
101 * struct fw_sec: Just for the image parsing proccess.
102 * For the fw storage we are using struct fw_desc.
103 */
104struct fw_sec {
105 const void *data; /* the sec data */
106 size_t size; /* section size */
107 u32 offset; /* offset of writing in the device */
108};
109
Johannes Berg965974a2012-03-06 13:30:38 -0800110static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
Johannes Berg15854ef2012-03-05 11:24:50 -0800111{
112 if (desc->v_addr)
Johannes Berg965974a2012-03-06 13:30:38 -0800113 dma_free_coherent(trans(drv)->dev, desc->len,
Johannes Berg15854ef2012-03-05 11:24:50 -0800114 desc->v_addr, desc->p_addr);
115 desc->v_addr = NULL;
116 desc->len = 0;
117}
118
Johannes Berg965974a2012-03-06 13:30:38 -0800119static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
Johannes Berg15854ef2012-03-05 11:24:50 -0800120{
Johannes Berg965974a2012-03-06 13:30:38 -0800121 iwl_free_fw_desc(drv, &img->code);
122 iwl_free_fw_desc(drv, &img->data);
Johannes Berg15854ef2012-03-05 11:24:50 -0800123}
124
Johannes Berg965974a2012-03-06 13:30:38 -0800125static void iwl_dealloc_ucode(struct iwl_drv *drv)
Johannes Berg15854ef2012-03-05 11:24:50 -0800126{
Johannes Berg965974a2012-03-06 13:30:38 -0800127 iwl_free_fw_img(drv, &drv->fw.ucode_rt);
128 iwl_free_fw_img(drv, &drv->fw.ucode_init);
129 iwl_free_fw_img(drv, &drv->fw.ucode_wowlan);
Johannes Berg15854ef2012-03-05 11:24:50 -0800130}
131
Johannes Berg965974a2012-03-06 13:30:38 -0800132static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
David Spinadel0cedacc2012-03-10 13:00:12 -0800133 struct fw_sec *sec)
Johannes Berg15854ef2012-03-05 11:24:50 -0800134{
David Spinadel0cedacc2012-03-10 13:00:12 -0800135 if (!sec || !sec->size) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800136 desc->v_addr = NULL;
137 return -EINVAL;
138 }
139
David Spinadel0cedacc2012-03-10 13:00:12 -0800140 desc->v_addr = dma_alloc_coherent(trans(drv)->dev, sec->size,
Johannes Berg15854ef2012-03-05 11:24:50 -0800141 &desc->p_addr, GFP_KERNEL);
142 if (!desc->v_addr)
143 return -ENOMEM;
144
David Spinadel0cedacc2012-03-10 13:00:12 -0800145 desc->len = sec->size;
146 desc->offset = sec->offset;
147 memcpy(desc->v_addr, sec->data, sec->size);
Johannes Berg15854ef2012-03-05 11:24:50 -0800148 return 0;
149}
150
151static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
152
153#define UCODE_EXPERIMENTAL_INDEX 100
154#define UCODE_EXPERIMENTAL_TAG "exp"
155
Johannes Berg965974a2012-03-06 13:30:38 -0800156static int iwl_request_firmware(struct iwl_drv *drv, bool first)
Johannes Berg15854ef2012-03-05 11:24:50 -0800157{
Johannes Berg965974a2012-03-06 13:30:38 -0800158 const struct iwl_cfg *cfg = cfg(drv);
Johannes Berg15854ef2012-03-05 11:24:50 -0800159 const char *name_pre = cfg->fw_name_pre;
160 char tag[8];
161
162 if (first) {
163#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
Johannes Berg965974a2012-03-06 13:30:38 -0800164 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
Johannes Berg15854ef2012-03-05 11:24:50 -0800165 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
Johannes Berg965974a2012-03-06 13:30:38 -0800166 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800167#endif
Johannes Berg965974a2012-03-06 13:30:38 -0800168 drv->fw_index = cfg->ucode_api_max;
169 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800170 } else {
Johannes Berg965974a2012-03-06 13:30:38 -0800171 drv->fw_index--;
172 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800173 }
174
Johannes Berg965974a2012-03-06 13:30:38 -0800175 if (drv->fw_index < cfg->ucode_api_min) {
176 IWL_ERR(drv, "no suitable firmware found!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800177 return -ENOENT;
178 }
179
Johannes Berg965974a2012-03-06 13:30:38 -0800180 sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Johannes Berg15854ef2012-03-05 11:24:50 -0800181
Johannes Berg965974a2012-03-06 13:30:38 -0800182 IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
183 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800184 ? "EXPERIMENTAL " : "",
Johannes Berg965974a2012-03-06 13:30:38 -0800185 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800186
Johannes Berg965974a2012-03-06 13:30:38 -0800187 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
188 trans(drv)->dev,
189 GFP_KERNEL, drv, iwl_ucode_callback);
Johannes Berg15854ef2012-03-05 11:24:50 -0800190}
191
David Spinadel0cedacc2012-03-10 13:00:12 -0800192/*
193 * enumeration of ucode section.
194 * This enumeration is used for legacy tlv style (before 16.0 uCode).
195 */
196enum iwl_ucode_sec {
197 IWL_UCODE_SECTION_INST,
198 IWL_UCODE_SECTION_DATA,
199};
200/*
201 * For 16.0 uCode and above, there is no differentiation between section,
202 * just an offset to the HW address.
203 */
204#define UCODE_SECTION_MAX 4
205
206struct fw_img_parsing {
207 struct fw_sec sec[UCODE_SECTION_MAX];
208 int sec_counter;
209};
210
David Spinadeled8c8362012-03-10 13:00:13 -0800211/*
212 * struct fw_sec_parsing: to extract fw section and it's offset from tlv
213 */
214struct fw_sec_parsing {
215 __le32 offset;
216 const u8 data[];
217} __packed;
218
219/**
220 * struct iwl_tlv_calib_data - parse the default calib data from TLV
221 *
222 * @ucode_type: the uCode to which the following default calib relates.
223 * @calib: default calibrations.
224 */
225struct iwl_tlv_calib_data {
226 __le32 ucode_type;
227 __le64 calib;
228} __packed;
229
David Spinadel0cedacc2012-03-10 13:00:12 -0800230struct iwl_firmware_pieces {
231 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
Johannes Berg15854ef2012-03-05 11:24:50 -0800232
233 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
234 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
235};
236
David Spinadel0cedacc2012-03-10 13:00:12 -0800237/*
238 * These functions are just to extract uCode section data from the pieces
239 * structure.
240 */
241static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
242 enum iwl_ucode_type type,
243 int sec)
244{
245 return &pieces->img[type].sec[sec];
246}
247
248static void set_sec_data(struct iwl_firmware_pieces *pieces,
249 enum iwl_ucode_type type,
250 int sec,
251 const void *data)
252{
253 pieces->img[type].sec[sec].data = data;
254}
255
256static void set_sec_size(struct iwl_firmware_pieces *pieces,
257 enum iwl_ucode_type type,
258 int sec,
259 size_t size)
260{
261 pieces->img[type].sec[sec].size = size;
262}
263
264static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
265 enum iwl_ucode_type type,
266 int sec)
267{
268 return pieces->img[type].sec[sec].size;
269}
270
271static void set_sec_offset(struct iwl_firmware_pieces *pieces,
272 enum iwl_ucode_type type,
273 int sec,
274 u32 offset)
275{
276 pieces->img[type].sec[sec].offset = offset;
277}
278
David Spinadeled8c8362012-03-10 13:00:13 -0800279/*
280 * Gets uCode section from tlv.
281 */
282static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
283 const void *data, enum iwl_ucode_type type,
284 int size)
285{
286 struct fw_img_parsing *img;
287 struct fw_sec *sec;
288 struct fw_sec_parsing *sec_parse;
289
290 if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
291 return -1;
292
293 sec_parse = (struct fw_sec_parsing *)data;
294
295 img = &pieces->img[type];
296 sec = &img->sec[img->sec_counter];
297
298 sec->offset = le32_to_cpu(sec_parse->offset);
299 sec->data = sec_parse->data;
300
301 ++img->sec_counter;
302
303 return 0;
304}
305
306static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
307{
308 struct iwl_tlv_calib_data *def_calib =
309 (struct iwl_tlv_calib_data *)data;
310 u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
311 if (ucode_type >= IWL_UCODE_TYPE_MAX) {
312 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
313 ucode_type);
314 return -EINVAL;
315 }
316 drv->fw.default_calib[ucode_type] = le64_to_cpu(def_calib->calib);
317 return 0;
318}
319
Johannes Berg965974a2012-03-06 13:30:38 -0800320static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
David Spinadel0cedacc2012-03-10 13:00:12 -0800321 const struct firmware *ucode_raw,
322 struct iwl_firmware_pieces *pieces)
Johannes Berg15854ef2012-03-05 11:24:50 -0800323{
324 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
325 u32 api_ver, hdr_size, build;
326 char buildstr[25];
327 const u8 *src;
328
Johannes Berg965974a2012-03-06 13:30:38 -0800329 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
330 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800331
332 switch (api_ver) {
333 default:
334 hdr_size = 28;
335 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800336 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800337 return -EINVAL;
338 }
339 build = le32_to_cpu(ucode->u.v2.build);
David Spinadel0cedacc2012-03-10 13:00:12 -0800340 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
341 le32_to_cpu(ucode->u.v2.inst_size));
342 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
343 le32_to_cpu(ucode->u.v2.data_size));
344 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
345 le32_to_cpu(ucode->u.v2.init_size));
346 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
347 le32_to_cpu(ucode->u.v2.init_data_size));
Johannes Berg15854ef2012-03-05 11:24:50 -0800348 src = ucode->u.v2.data;
349 break;
350 case 0:
351 case 1:
352 case 2:
353 hdr_size = 24;
354 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800355 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800356 return -EINVAL;
357 }
358 build = 0;
David Spinadel0cedacc2012-03-10 13:00:12 -0800359 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
360 le32_to_cpu(ucode->u.v1.inst_size));
361 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
362 le32_to_cpu(ucode->u.v1.data_size));
363 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
364 le32_to_cpu(ucode->u.v1.init_size));
365 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
366 le32_to_cpu(ucode->u.v1.init_data_size));
Johannes Berg15854ef2012-03-05 11:24:50 -0800367 src = ucode->u.v1.data;
368 break;
369 }
370
371 if (build)
372 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800373 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800374 ? " (EXP)" : "");
375 else
376 buildstr[0] = '\0';
377
Johannes Berg965974a2012-03-06 13:30:38 -0800378 snprintf(drv->fw.fw_version,
379 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800380 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800381 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
382 IWL_UCODE_MINOR(drv->fw.ucode_ver),
383 IWL_UCODE_API(drv->fw.ucode_ver),
384 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef2012-03-05 11:24:50 -0800385 buildstr);
386
387 /* Verify size of file vs. image size info in file's header */
David Spinadel0cedacc2012-03-10 13:00:12 -0800388
389 if (ucode_raw->size != hdr_size +
390 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
391 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
392 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
393 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800394
Johannes Berg965974a2012-03-06 13:30:38 -0800395 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800396 "uCode file size %d does not match expected size\n",
397 (int)ucode_raw->size);
398 return -EINVAL;
399 }
400
Johannes Berg15854ef2012-03-05 11:24:50 -0800401
David Spinadel0cedacc2012-03-10 13:00:12 -0800402 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
403 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
404 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
405 IWLAGN_RTC_INST_LOWER_BOUND);
406 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
407 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
408 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
409 IWLAGN_RTC_DATA_LOWER_BOUND);
410 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
411 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
412 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
413 IWLAGN_RTC_INST_LOWER_BOUND);
414 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
415 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
416 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
417 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800418 return 0;
419}
420
Johannes Berg965974a2012-03-06 13:30:38 -0800421static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800422 const struct firmware *ucode_raw,
David Spinadel0cedacc2012-03-10 13:00:12 -0800423 struct iwl_firmware_pieces *pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -0800424 struct iwl_ucode_capabilities *capa)
425{
426 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
427 struct iwl_ucode_tlv *tlv;
428 size_t len = ucode_raw->size;
429 const u8 *data;
430 int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
431 int tmp;
432 u64 alternatives;
433 u32 tlv_len;
434 enum iwl_ucode_tlv_type tlv_type;
435 const u8 *tlv_data;
436 char buildstr[25];
437 u32 build;
438
439 if (len < sizeof(*ucode)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800440 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800441 return -EINVAL;
442 }
443
444 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800445 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800446 le32_to_cpu(ucode->magic));
447 return -EINVAL;
448 }
449
450 /*
451 * Check which alternatives are present, and "downgrade"
452 * when the chosen alternative is not present, warning
453 * the user when that happens. Some files may not have
454 * any alternatives, so don't warn in that case.
455 */
456 alternatives = le64_to_cpu(ucode->alternatives);
457 tmp = wanted_alternative;
458 if (wanted_alternative > 63)
459 wanted_alternative = 63;
460 while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
461 wanted_alternative--;
462 if (wanted_alternative && wanted_alternative != tmp)
Johannes Berg965974a2012-03-06 13:30:38 -0800463 IWL_WARN(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800464 "uCode alternative %d not available, choosing %d\n",
465 tmp, wanted_alternative);
466
Johannes Berg965974a2012-03-06 13:30:38 -0800467 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800468 build = le32_to_cpu(ucode->build);
469
470 if (build)
471 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800472 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800473 ? " (EXP)" : "");
474 else
475 buildstr[0] = '\0';
476
Johannes Berg965974a2012-03-06 13:30:38 -0800477 snprintf(drv->fw.fw_version,
478 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800479 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800480 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
481 IWL_UCODE_MINOR(drv->fw.ucode_ver),
482 IWL_UCODE_API(drv->fw.ucode_ver),
483 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef2012-03-05 11:24:50 -0800484 buildstr);
485
486 data = ucode->data;
487
488 len -= sizeof(*ucode);
489
490 while (len >= sizeof(*tlv)) {
491 u16 tlv_alt;
492
493 len -= sizeof(*tlv);
494 tlv = (void *)data;
495
496 tlv_len = le32_to_cpu(tlv->length);
497 tlv_type = le16_to_cpu(tlv->type);
498 tlv_alt = le16_to_cpu(tlv->alternative);
499 tlv_data = tlv->data;
500
501 if (len < tlv_len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800502 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800503 len, tlv_len);
504 return -EINVAL;
505 }
506 len -= ALIGN(tlv_len, 4);
507 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
508
509 /*
510 * Alternative 0 is always valid.
511 *
512 * Skip alternative TLVs that are not selected.
513 */
514 if (tlv_alt != 0 && tlv_alt != wanted_alternative)
515 continue;
516
517 switch (tlv_type) {
518 case IWL_UCODE_TLV_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800519 set_sec_data(pieces, IWL_UCODE_REGULAR,
520 IWL_UCODE_SECTION_INST, tlv_data);
521 set_sec_size(pieces, IWL_UCODE_REGULAR,
522 IWL_UCODE_SECTION_INST, tlv_len);
523 set_sec_offset(pieces, IWL_UCODE_REGULAR,
524 IWL_UCODE_SECTION_INST,
525 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800526 break;
527 case IWL_UCODE_TLV_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800528 set_sec_data(pieces, IWL_UCODE_REGULAR,
529 IWL_UCODE_SECTION_DATA, tlv_data);
530 set_sec_size(pieces, IWL_UCODE_REGULAR,
531 IWL_UCODE_SECTION_DATA, tlv_len);
532 set_sec_offset(pieces, IWL_UCODE_REGULAR,
533 IWL_UCODE_SECTION_DATA,
534 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800535 break;
536 case IWL_UCODE_TLV_INIT:
David Spinadel0cedacc2012-03-10 13:00:12 -0800537 set_sec_data(pieces, IWL_UCODE_INIT,
538 IWL_UCODE_SECTION_INST, tlv_data);
539 set_sec_size(pieces, IWL_UCODE_INIT,
540 IWL_UCODE_SECTION_INST, tlv_len);
541 set_sec_offset(pieces, IWL_UCODE_INIT,
542 IWL_UCODE_SECTION_INST,
543 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800544 break;
545 case IWL_UCODE_TLV_INIT_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800546 set_sec_data(pieces, IWL_UCODE_INIT,
547 IWL_UCODE_SECTION_DATA, tlv_data);
548 set_sec_size(pieces, IWL_UCODE_INIT,
549 IWL_UCODE_SECTION_DATA, tlv_len);
550 set_sec_offset(pieces, IWL_UCODE_INIT,
551 IWL_UCODE_SECTION_DATA,
552 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800553 break;
554 case IWL_UCODE_TLV_BOOT:
Johannes Berg965974a2012-03-06 13:30:38 -0800555 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800556 break;
557 case IWL_UCODE_TLV_PROBE_MAX_LEN:
558 if (tlv_len != sizeof(u32))
559 goto invalid_tlv_len;
560 capa->max_probe_length =
561 le32_to_cpup((__le32 *)tlv_data);
562 break;
563 case IWL_UCODE_TLV_PAN:
564 if (tlv_len)
565 goto invalid_tlv_len;
566 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
567 break;
568 case IWL_UCODE_TLV_FLAGS:
569 /* must be at least one u32 */
570 if (tlv_len < sizeof(u32))
571 goto invalid_tlv_len;
572 /* and a proper number of u32s */
573 if (tlv_len % sizeof(u32))
574 goto invalid_tlv_len;
575 /*
576 * This driver only reads the first u32 as
577 * right now no more features are defined,
578 * if that changes then either the driver
579 * will not work with the new firmware, or
580 * it'll not take advantage of new features.
581 */
582 capa->flags = le32_to_cpup((__le32 *)tlv_data);
583 break;
584 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
585 if (tlv_len != sizeof(u32))
586 goto invalid_tlv_len;
587 pieces->init_evtlog_ptr =
588 le32_to_cpup((__le32 *)tlv_data);
589 break;
590 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
591 if (tlv_len != sizeof(u32))
592 goto invalid_tlv_len;
593 pieces->init_evtlog_size =
594 le32_to_cpup((__le32 *)tlv_data);
595 break;
596 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
597 if (tlv_len != sizeof(u32))
598 goto invalid_tlv_len;
599 pieces->init_errlog_ptr =
600 le32_to_cpup((__le32 *)tlv_data);
601 break;
602 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
603 if (tlv_len != sizeof(u32))
604 goto invalid_tlv_len;
605 pieces->inst_evtlog_ptr =
606 le32_to_cpup((__le32 *)tlv_data);
607 break;
608 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
609 if (tlv_len != sizeof(u32))
610 goto invalid_tlv_len;
611 pieces->inst_evtlog_size =
612 le32_to_cpup((__le32 *)tlv_data);
613 break;
614 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
615 if (tlv_len != sizeof(u32))
616 goto invalid_tlv_len;
617 pieces->inst_errlog_ptr =
618 le32_to_cpup((__le32 *)tlv_data);
619 break;
620 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
621 if (tlv_len)
622 goto invalid_tlv_len;
Johannes Berg965974a2012-03-06 13:30:38 -0800623 drv->fw.enhance_sensitivity_table = true;
Johannes Berg15854ef2012-03-05 11:24:50 -0800624 break;
625 case IWL_UCODE_TLV_WOWLAN_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800626 set_sec_data(pieces, IWL_UCODE_WOWLAN,
627 IWL_UCODE_SECTION_INST, tlv_data);
628 set_sec_size(pieces, IWL_UCODE_WOWLAN,
629 IWL_UCODE_SECTION_INST, tlv_len);
630 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
631 IWL_UCODE_SECTION_INST,
632 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800633 break;
634 case IWL_UCODE_TLV_WOWLAN_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800635 set_sec_data(pieces, IWL_UCODE_WOWLAN,
636 IWL_UCODE_SECTION_DATA, tlv_data);
637 set_sec_size(pieces, IWL_UCODE_WOWLAN,
638 IWL_UCODE_SECTION_DATA, tlv_len);
639 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
640 IWL_UCODE_SECTION_DATA,
641 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800642 break;
643 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
644 if (tlv_len != sizeof(u32))
645 goto invalid_tlv_len;
646 capa->standard_phy_calibration_size =
647 le32_to_cpup((__le32 *)tlv_data);
648 break;
David Spinadeled8c8362012-03-10 13:00:13 -0800649 case IWL_UCODE_TLV_SEC_RT:
650 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
651 tlv_len);
652 break;
653 case IWL_UCODE_TLV_SEC_INIT:
654 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
655 tlv_len);
656 break;
657 case IWL_UCODE_TLV_SEC_WOWLAN:
658 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
659 tlv_len);
660 break;
661 case IWL_UCODE_TLV_DEF_CALIB:
662 if (tlv_len != sizeof(struct iwl_tlv_calib_data))
663 goto invalid_tlv_len;
664 if (iwl_set_default_calib(drv, tlv_data))
665 goto tlv_error;
666 break;
667 case IWL_UCODE_TLV_PHY_SKU:
668 if (tlv_len != sizeof(u32))
669 goto invalid_tlv_len;
670 drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
671 break;
Johannes Berg15854ef2012-03-05 11:24:50 -0800672 default:
Johannes Berg965974a2012-03-06 13:30:38 -0800673 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
Johannes Berg15854ef2012-03-05 11:24:50 -0800674 break;
675 }
676 }
677
678 if (len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800679 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
680 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800681 return -EINVAL;
682 }
683
684 return 0;
685
686 invalid_tlv_len:
Johannes Berg965974a2012-03-06 13:30:38 -0800687 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
David Spinadeled8c8362012-03-10 13:00:13 -0800688 tlv_error:
Johannes Berg965974a2012-03-06 13:30:38 -0800689 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800690
691 return -EINVAL;
692}
693
694/**
695 * iwl_ucode_callback - callback when firmware was loaded
696 *
697 * If loaded successfully, copies the firmware into buffers
698 * for the card to fetch (via DMA).
699 */
700static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
701{
Johannes Berg965974a2012-03-06 13:30:38 -0800702 struct iwl_drv *drv = context;
703 const struct iwl_cfg *cfg = cfg(drv);
704 struct iwl_fw *fw = &drv->fw;
Johannes Berg15854ef2012-03-05 11:24:50 -0800705 struct iwl_ucode_header *ucode;
706 int err;
David Spinadel0cedacc2012-03-10 13:00:12 -0800707 struct iwl_firmware_pieces pieces;
Johannes Berg15854ef2012-03-05 11:24:50 -0800708 const unsigned int api_max = cfg->ucode_api_max;
709 unsigned int api_ok = cfg->ucode_api_ok;
710 const unsigned int api_min = cfg->ucode_api_min;
711 u32 api_ver;
712
713 fw->ucode_capa.max_probe_length = 200;
714 fw->ucode_capa.standard_phy_calibration_size =
715 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
716
717 if (!api_ok)
718 api_ok = api_max;
719
720 memset(&pieces, 0, sizeof(pieces));
721
722 if (!ucode_raw) {
Johannes Berg965974a2012-03-06 13:30:38 -0800723 if (drv->fw_index <= api_ok)
724 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800725 "request for firmware file '%s' failed.\n",
Johannes Berg965974a2012-03-06 13:30:38 -0800726 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800727 goto try_again;
728 }
729
Johannes Berg965974a2012-03-06 13:30:38 -0800730 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
731 drv->firmware_name, ucode_raw->size);
Johannes Berg15854ef2012-03-05 11:24:50 -0800732
733 /* Make sure that we got at least the API version number */
734 if (ucode_raw->size < 4) {
Johannes Berg965974a2012-03-06 13:30:38 -0800735 IWL_ERR(drv, "File size way too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800736 goto try_again;
737 }
738
739 /* Data from ucode file: header followed by uCode images */
740 ucode = (struct iwl_ucode_header *)ucode_raw->data;
741
742 if (ucode->ver)
Johannes Berg965974a2012-03-06 13:30:38 -0800743 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
Johannes Berg15854ef2012-03-05 11:24:50 -0800744 else
Johannes Berg965974a2012-03-06 13:30:38 -0800745 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -0800746 &fw->ucode_capa);
747
748 if (err)
749 goto try_again;
750
Johannes Berg965974a2012-03-06 13:30:38 -0800751 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800752
753 /*
754 * api_ver should match the api version forming part of the
755 * firmware filename ... but we don't check for that and only rely
756 * on the API version read from firmware header from here on forward
757 */
758 /* no api version check required for experimental uCode */
Johannes Berg965974a2012-03-06 13:30:38 -0800759 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800760 if (api_ver < api_min || api_ver > api_max) {
Johannes Berg965974a2012-03-06 13:30:38 -0800761 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800762 "Driver unable to support your firmware API. "
763 "Driver supports v%u, firmware is v%u.\n",
764 api_max, api_ver);
765 goto try_again;
766 }
767
768 if (api_ver < api_ok) {
769 if (api_ok != api_max)
Johannes Berg965974a2012-03-06 13:30:38 -0800770 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -0800771 "expected v%u through v%u, got v%u.\n",
772 api_ok, api_max, api_ver);
773 else
Johannes Berg965974a2012-03-06 13:30:38 -0800774 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -0800775 "expected v%u, got v%u.\n",
776 api_max, api_ver);
Johannes Berg965974a2012-03-06 13:30:38 -0800777 IWL_ERR(drv, "New firmware can be obtained from "
Johannes Berg15854ef2012-03-05 11:24:50 -0800778 "http://www.intellinuxwireless.org/.\n");
779 }
780 }
781
Johannes Berg965974a2012-03-06 13:30:38 -0800782 IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
Johannes Berg15854ef2012-03-05 11:24:50 -0800783
784 /*
785 * For any of the failures below (before allocating pci memory)
786 * we will try to load a version with a smaller API -- maybe the
787 * user just got a corrupted version of the latest API.
788 */
789
Johannes Berg965974a2012-03-06 13:30:38 -0800790 IWL_DEBUG_INFO(drv, "f/w package hdr ucode version raw = 0x%x\n",
791 drv->fw.ucode_ver);
792 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800793 get_sec_size(&pieces, IWL_UCODE_REGULAR,
794 IWL_UCODE_SECTION_INST));
Johannes Berg965974a2012-03-06 13:30:38 -0800795 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800796 get_sec_size(&pieces, IWL_UCODE_REGULAR,
797 IWL_UCODE_SECTION_DATA));
Johannes Berg965974a2012-03-06 13:30:38 -0800798 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800799 get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
Johannes Berg965974a2012-03-06 13:30:38 -0800800 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800801 get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
Johannes Berg15854ef2012-03-05 11:24:50 -0800802
803 /* Verify that uCode images will fit in card's SRAM */
David Spinadel0cedacc2012-03-10 13:00:12 -0800804 if (get_sec_size(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
805 cfg->max_inst_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800806 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800807 get_sec_size(&pieces, IWL_UCODE_REGULAR,
808 IWL_UCODE_SECTION_INST));
Johannes Berg15854ef2012-03-05 11:24:50 -0800809 goto try_again;
810 }
811
David Spinadel0cedacc2012-03-10 13:00:12 -0800812 if (get_sec_size(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
813 cfg->max_data_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800814 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800815 get_sec_size(&pieces, IWL_UCODE_REGULAR,
816 IWL_UCODE_SECTION_DATA));
Johannes Berg15854ef2012-03-05 11:24:50 -0800817 goto try_again;
818 }
819
David Spinadel0cedacc2012-03-10 13:00:12 -0800820 if (get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
821 cfg->max_inst_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800822 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800823 get_sec_size(&pieces, IWL_UCODE_INIT,
824 IWL_UCODE_SECTION_INST));
Johannes Berg15854ef2012-03-05 11:24:50 -0800825 goto try_again;
826 }
827
David Spinadel0cedacc2012-03-10 13:00:12 -0800828 if (get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
829 cfg->max_data_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800830 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
David Spinadel0cedacc2012-03-10 13:00:12 -0800831 get_sec_size(&pieces, IWL_UCODE_REGULAR,
832 IWL_UCODE_SECTION_DATA));
Johannes Berg15854ef2012-03-05 11:24:50 -0800833 goto try_again;
834 }
835
836 /* Allocate ucode buffers for card's bus-master loading ... */
837
838 /* Runtime instructions and 2 copies of data:
839 * 1) unmodified from disk
840 * 2) backup cache for save/restore during power-downs */
Johannes Berg965974a2012-03-06 13:30:38 -0800841 if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_rt.code,
David Spinadel0cedacc2012-03-10 13:00:12 -0800842 get_sec(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST)))
Johannes Berg15854ef2012-03-05 11:24:50 -0800843 goto err_pci_alloc;
Johannes Berg965974a2012-03-06 13:30:38 -0800844 if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_rt.data,
David Spinadel0cedacc2012-03-10 13:00:12 -0800845 get_sec(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA)))
Johannes Berg15854ef2012-03-05 11:24:50 -0800846 goto err_pci_alloc;
847
848 /* Initialization instructions and data */
David Spinadel0cedacc2012-03-10 13:00:12 -0800849 if (get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) &&
850 get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
851 if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_init.code,
852 get_sec(&pieces, IWL_UCODE_INIT,
853 IWL_UCODE_SECTION_INST)))
Johannes Berg15854ef2012-03-05 11:24:50 -0800854 goto err_pci_alloc;
David Spinadel0cedacc2012-03-10 13:00:12 -0800855 if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_init.data,
856 get_sec(&pieces, IWL_UCODE_INIT,
857 IWL_UCODE_SECTION_DATA)))
Johannes Berg15854ef2012-03-05 11:24:50 -0800858 goto err_pci_alloc;
859 }
860
861 /* WoWLAN instructions and data */
David Spinadel0cedacc2012-03-10 13:00:12 -0800862 if (get_sec_size(&pieces, IWL_UCODE_WOWLAN, IWL_UCODE_SECTION_INST) &&
863 get_sec_size(&pieces, IWL_UCODE_WOWLAN, IWL_UCODE_SECTION_DATA)) {
864 if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_wowlan.code,
865 get_sec(&pieces, IWL_UCODE_WOWLAN,
866 IWL_UCODE_SECTION_INST)))
Johannes Berg15854ef2012-03-05 11:24:50 -0800867 goto err_pci_alloc;
David Spinadel0cedacc2012-03-10 13:00:12 -0800868 if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_wowlan.data,
869 get_sec(&pieces, IWL_UCODE_WOWLAN,
870 IWL_UCODE_SECTION_DATA)))
Johannes Berg15854ef2012-03-05 11:24:50 -0800871 goto err_pci_alloc;
872 }
873
874 /* Now that we can no longer fail, copy information */
875
876 /*
877 * The (size - 16) / 12 formula is based on the information recorded
878 * for each event, which is of mode 1 (including timestamp) for all
879 * new microcodes that include this information.
880 */
Johannes Berg0692fe42012-03-06 13:30:37 -0800881 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800882 if (pieces.init_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800883 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -0800884 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800885 fw->init_evtlog_size =
Johannes Berg15854ef2012-03-05 11:24:50 -0800886 cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800887 fw->init_errlog_ptr = pieces.init_errlog_ptr;
888 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800889 if (pieces.inst_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800890 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -0800891 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800892 fw->inst_evtlog_size =
Johannes Berg15854ef2012-03-05 11:24:50 -0800893 cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800894 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800895
896 /*
897 * figure out the offset of chain noise reset and gain commands
898 * base on the size of standard phy calibration commands table size
899 */
900 if (fw->ucode_capa.standard_phy_calibration_size >
901 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
902 fw->ucode_capa.standard_phy_calibration_size =
903 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
904
905 /* We have our copies now, allow OS release its copies */
906 release_firmware(ucode_raw);
Johannes Berg965974a2012-03-06 13:30:38 -0800907 complete(&drv->request_firmware_complete);
Johannes Berg15854ef2012-03-05 11:24:50 -0800908
Johannes Berg965974a2012-03-06 13:30:38 -0800909 drv->op_mode = iwl_dvm_ops.start(drv->shrd->trans, &drv->fw);
Johannes Berg15854ef2012-03-05 11:24:50 -0800910
Johannes Berg965974a2012-03-06 13:30:38 -0800911 if (!drv->op_mode)
Johannes Berg15854ef2012-03-05 11:24:50 -0800912 goto out_unbind;
913
914 return;
915
916 try_again:
917 /* try next, if any */
918 release_firmware(ucode_raw);
Johannes Berg965974a2012-03-06 13:30:38 -0800919 if (iwl_request_firmware(drv, false))
Johannes Berg15854ef2012-03-05 11:24:50 -0800920 goto out_unbind;
921 return;
922
923 err_pci_alloc:
Johannes Berg965974a2012-03-06 13:30:38 -0800924 IWL_ERR(drv, "failed to allocate pci memory\n");
925 iwl_dealloc_ucode(drv);
Johannes Berg15854ef2012-03-05 11:24:50 -0800926 release_firmware(ucode_raw);
927 out_unbind:
Johannes Berg965974a2012-03-06 13:30:38 -0800928 complete(&drv->request_firmware_complete);
929 device_release_driver(trans(drv)->dev);
Johannes Berg15854ef2012-03-05 11:24:50 -0800930}
931
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200932int iwl_drv_start(struct iwl_shared *shrd,
Johannes Berg706c4ff2012-03-05 11:24:33 -0800933 struct iwl_trans *trans, const struct iwl_cfg *cfg)
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200934{
Johannes Berg965974a2012-03-06 13:30:38 -0800935 struct iwl_drv *drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200936 int ret;
937
938 shrd->cfg = cfg;
939
Johannes Berg965974a2012-03-06 13:30:38 -0800940 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
941 if (!drv) {
942 dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_drv");
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200943 return -ENOMEM;
944 }
Johannes Berg965974a2012-03-06 13:30:38 -0800945 drv->shrd = shrd;
946 shrd->drv = drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200947
Johannes Berg965974a2012-03-06 13:30:38 -0800948 init_completion(&drv->request_firmware_complete);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200949
Johannes Berg965974a2012-03-06 13:30:38 -0800950 ret = iwl_request_firmware(drv, true);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200951
952 if (ret) {
953 dev_printk(KERN_ERR, trans->dev, "Couldn't request the fw");
Johannes Berg965974a2012-03-06 13:30:38 -0800954 kfree(drv);
955 shrd->drv = NULL;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200956 }
957
958 return ret;
959}
960
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200961void iwl_drv_stop(struct iwl_shared *shrd)
962{
Johannes Berg965974a2012-03-06 13:30:38 -0800963 struct iwl_drv *drv = shrd->drv;
Johannes Berg0692fe42012-03-06 13:30:37 -0800964
Johannes Berg965974a2012-03-06 13:30:38 -0800965 wait_for_completion(&drv->request_firmware_complete);
Johannes Berg2e7eb112012-03-05 11:24:51 -0800966
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200967 /* op_mode can be NULL if its start failed */
Johannes Berg965974a2012-03-06 13:30:38 -0800968 if (drv->op_mode)
969 iwl_op_mode_stop(drv->op_mode);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200970
Johannes Berg965974a2012-03-06 13:30:38 -0800971 iwl_dealloc_ucode(drv);
Johannes Berg7db5b982012-03-05 11:24:48 -0800972
Johannes Berg965974a2012-03-06 13:30:38 -0800973 kfree(drv);
974 shrd->drv = NULL;
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200975}