blob: 3c72bad0ae56fc3d1e443f2b08a70e337910e4f0 [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"
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +030069#include "iwl-debug.h"
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020070#include "iwl-trans.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"
Johannes Berg6238b002012-04-02 15:04:33 +020073#include "iwl-fw.h"
74#include "iwl-config.h"
Johannes Berg65de7e82012-04-17 07:36:30 -070075#include "iwl-modparams.h"
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020076
Johannes Berg0692fe42012-03-06 13:30:37 -080077/* private includes */
Johannes Berg3995dea2012-03-06 13:30:44 -080078#include "iwl-fw-file.h"
Johannes Berg0692fe42012-03-06 13:30:37 -080079
Johannes Berg965974a62012-03-06 13:30:38 -080080/**
81 * struct iwl_drv - drv common data
82 * @fw: the iwl_fw structure
Johannes Berg965974a62012-03-06 13:30:38 -080083 * @op_mode: the running op_mode
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +020084 * @trans: transport layer
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +020085 * @dev: for debug prints only
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +020086 * @cfg: configuration struct
Johannes Berg965974a62012-03-06 13:30:38 -080087 * @fw_index: firmware revision to try loading
88 * @firmware_name: composite filename of ucode file to load
89 * @request_firmware_complete: the firmware has been obtained from user space
90 */
91struct iwl_drv {
92 struct iwl_fw fw;
93
Johannes Berg965974a62012-03-06 13:30:38 -080094 struct iwl_op_mode *op_mode;
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -070095 struct iwl_trans *trans;
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +020096 struct device *dev;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +020097 const struct iwl_cfg *cfg;
Johannes Berg965974a62012-03-06 13:30:38 -080098
99 int fw_index; /* firmware we're trying to load */
100 char firmware_name[25]; /* name of firmware file to load */
101
102 struct completion request_firmware_complete;
103};
104
105
106
David Spinadel0cedacc2012-03-10 13:00:12 -0800107/*
108 * struct fw_sec: Just for the image parsing proccess.
109 * For the fw storage we are using struct fw_desc.
110 */
111struct fw_sec {
112 const void *data; /* the sec data */
113 size_t size; /* section size */
114 u32 offset; /* offset of writing in the device */
115};
116
Johannes Berg965974a62012-03-06 13:30:38 -0800117static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
Johannes Berg15854ef92012-03-05 11:24:50 -0800118{
119 if (desc->v_addr)
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200120 dma_free_coherent(drv->trans->dev, desc->len,
Johannes Berg15854ef92012-03-05 11:24:50 -0800121 desc->v_addr, desc->p_addr);
122 desc->v_addr = NULL;
123 desc->len = 0;
124}
125
Johannes Berg965974a62012-03-06 13:30:38 -0800126static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
Johannes Berg15854ef92012-03-05 11:24:50 -0800127{
David Spinadel6dfa8d02012-03-10 13:00:14 -0800128 int i;
129 for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
130 iwl_free_fw_desc(drv, &img->sec[i]);
Johannes Berg15854ef92012-03-05 11:24:50 -0800131}
132
Johannes Berg965974a62012-03-06 13:30:38 -0800133static void iwl_dealloc_ucode(struct iwl_drv *drv)
Johannes Berg15854ef92012-03-05 11:24:50 -0800134{
David Spinadel6dfa8d02012-03-10 13:00:14 -0800135 int i;
136 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
137 iwl_free_fw_img(drv, drv->fw.img + i);
Johannes Berg15854ef92012-03-05 11:24:50 -0800138}
139
Johannes Berg965974a62012-03-06 13:30:38 -0800140static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
David Spinadel0cedacc2012-03-10 13:00:12 -0800141 struct fw_sec *sec)
Johannes Berg15854ef92012-03-05 11:24:50 -0800142{
David Spinadel0cedacc2012-03-10 13:00:12 -0800143 if (!sec || !sec->size) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800144 desc->v_addr = NULL;
145 return -EINVAL;
146 }
147
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200148 desc->v_addr = dma_alloc_coherent(drv->trans->dev, sec->size,
Johannes Berg15854ef92012-03-05 11:24:50 -0800149 &desc->p_addr, GFP_KERNEL);
150 if (!desc->v_addr)
151 return -ENOMEM;
152
David Spinadel0cedacc2012-03-10 13:00:12 -0800153 desc->len = sec->size;
154 desc->offset = sec->offset;
155 memcpy(desc->v_addr, sec->data, sec->size);
Johannes Berg15854ef92012-03-05 11:24:50 -0800156 return 0;
157}
158
159static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
160
161#define UCODE_EXPERIMENTAL_INDEX 100
162#define UCODE_EXPERIMENTAL_TAG "exp"
163
Johannes Berg965974a62012-03-06 13:30:38 -0800164static int iwl_request_firmware(struct iwl_drv *drv, bool first)
Johannes Berg15854ef92012-03-05 11:24:50 -0800165{
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200166 const char *name_pre = drv->cfg->fw_name_pre;
Johannes Berg15854ef92012-03-05 11:24:50 -0800167 char tag[8];
168
169 if (first) {
170#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
Johannes Berg965974a62012-03-06 13:30:38 -0800171 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
Johannes Berg15854ef92012-03-05 11:24:50 -0800172 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
Johannes Berg965974a62012-03-06 13:30:38 -0800173 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800174#endif
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200175 drv->fw_index = drv->cfg->ucode_api_max;
Johannes Berg965974a62012-03-06 13:30:38 -0800176 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef92012-03-05 11:24:50 -0800177 } else {
Johannes Berg965974a62012-03-06 13:30:38 -0800178 drv->fw_index--;
179 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef92012-03-05 11:24:50 -0800180 }
181
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200182 if (drv->fw_index < drv->cfg->ucode_api_min) {
Johannes Berg965974a62012-03-06 13:30:38 -0800183 IWL_ERR(drv, "no suitable firmware found!\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800184 return -ENOENT;
185 }
186
Johannes Berg965974a62012-03-06 13:30:38 -0800187 sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Johannes Berg15854ef92012-03-05 11:24:50 -0800188
Johannes Berg965974a62012-03-06 13:30:38 -0800189 IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
190 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef92012-03-05 11:24:50 -0800191 ? "EXPERIMENTAL " : "",
Johannes Berg965974a62012-03-06 13:30:38 -0800192 drv->firmware_name);
Johannes Berg15854ef92012-03-05 11:24:50 -0800193
Johannes Berg965974a62012-03-06 13:30:38 -0800194 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200195 drv->trans->dev,
Johannes Berg965974a62012-03-06 13:30:38 -0800196 GFP_KERNEL, drv, iwl_ucode_callback);
Johannes Berg15854ef92012-03-05 11:24:50 -0800197}
198
David Spinadel0cedacc2012-03-10 13:00:12 -0800199struct fw_img_parsing {
David Spinadel6dfa8d02012-03-10 13:00:14 -0800200 struct fw_sec sec[IWL_UCODE_SECTION_MAX];
David Spinadel0cedacc2012-03-10 13:00:12 -0800201 int sec_counter;
202};
203
David Spinadeled8c8362012-03-10 13:00:13 -0800204/*
205 * struct fw_sec_parsing: to extract fw section and it's offset from tlv
206 */
207struct fw_sec_parsing {
208 __le32 offset;
209 const u8 data[];
210} __packed;
211
212/**
213 * struct iwl_tlv_calib_data - parse the default calib data from TLV
214 *
215 * @ucode_type: the uCode to which the following default calib relates.
216 * @calib: default calibrations.
217 */
218struct iwl_tlv_calib_data {
219 __le32 ucode_type;
220 __le64 calib;
221} __packed;
222
David Spinadel0cedacc2012-03-10 13:00:12 -0800223struct iwl_firmware_pieces {
224 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
Johannes Berg15854ef92012-03-05 11:24:50 -0800225
226 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
227 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
228};
229
David Spinadel0cedacc2012-03-10 13:00:12 -0800230/*
231 * These functions are just to extract uCode section data from the pieces
232 * structure.
233 */
234static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
235 enum iwl_ucode_type type,
236 int sec)
237{
238 return &pieces->img[type].sec[sec];
239}
240
241static void set_sec_data(struct iwl_firmware_pieces *pieces,
242 enum iwl_ucode_type type,
243 int sec,
244 const void *data)
245{
246 pieces->img[type].sec[sec].data = data;
247}
248
249static void set_sec_size(struct iwl_firmware_pieces *pieces,
250 enum iwl_ucode_type type,
251 int sec,
252 size_t size)
253{
254 pieces->img[type].sec[sec].size = size;
255}
256
257static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
258 enum iwl_ucode_type type,
259 int sec)
260{
261 return pieces->img[type].sec[sec].size;
262}
263
264static void set_sec_offset(struct iwl_firmware_pieces *pieces,
265 enum iwl_ucode_type type,
266 int sec,
267 u32 offset)
268{
269 pieces->img[type].sec[sec].offset = offset;
270}
271
David Spinadeled8c8362012-03-10 13:00:13 -0800272/*
273 * Gets uCode section from tlv.
274 */
275static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
276 const void *data, enum iwl_ucode_type type,
277 int size)
278{
279 struct fw_img_parsing *img;
280 struct fw_sec *sec;
281 struct fw_sec_parsing *sec_parse;
282
283 if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
284 return -1;
285
286 sec_parse = (struct fw_sec_parsing *)data;
287
288 img = &pieces->img[type];
289 sec = &img->sec[img->sec_counter];
290
291 sec->offset = le32_to_cpu(sec_parse->offset);
292 sec->data = sec_parse->data;
David Spinadel1176f432012-03-13 14:32:48 +0200293 sec->size = size - sizeof(sec_parse->offset);
David Spinadeled8c8362012-03-10 13:00:13 -0800294
295 ++img->sec_counter;
296
297 return 0;
298}
299
300static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
301{
302 struct iwl_tlv_calib_data *def_calib =
303 (struct iwl_tlv_calib_data *)data;
304 u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
305 if (ucode_type >= IWL_UCODE_TYPE_MAX) {
306 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
307 ucode_type);
308 return -EINVAL;
309 }
310 drv->fw.default_calib[ucode_type] = le64_to_cpu(def_calib->calib);
311 return 0;
312}
313
Johannes Berg965974a62012-03-06 13:30:38 -0800314static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
David Spinadel0cedacc2012-03-10 13:00:12 -0800315 const struct firmware *ucode_raw,
316 struct iwl_firmware_pieces *pieces)
Johannes Berg15854ef92012-03-05 11:24:50 -0800317{
318 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
319 u32 api_ver, hdr_size, build;
320 char buildstr[25];
321 const u8 *src;
322
Johannes Berg965974a62012-03-06 13:30:38 -0800323 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
324 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef92012-03-05 11:24:50 -0800325
326 switch (api_ver) {
327 default:
328 hdr_size = 28;
329 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a62012-03-06 13:30:38 -0800330 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800331 return -EINVAL;
332 }
333 build = le32_to_cpu(ucode->u.v2.build);
David Spinadel0cedacc2012-03-10 13:00:12 -0800334 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
335 le32_to_cpu(ucode->u.v2.inst_size));
336 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
337 le32_to_cpu(ucode->u.v2.data_size));
338 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
339 le32_to_cpu(ucode->u.v2.init_size));
340 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
341 le32_to_cpu(ucode->u.v2.init_data_size));
Johannes Berg15854ef92012-03-05 11:24:50 -0800342 src = ucode->u.v2.data;
343 break;
344 case 0:
345 case 1:
346 case 2:
347 hdr_size = 24;
348 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a62012-03-06 13:30:38 -0800349 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800350 return -EINVAL;
351 }
352 build = 0;
David Spinadel0cedacc2012-03-10 13:00:12 -0800353 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
354 le32_to_cpu(ucode->u.v1.inst_size));
355 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
356 le32_to_cpu(ucode->u.v1.data_size));
357 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
358 le32_to_cpu(ucode->u.v1.init_size));
359 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
360 le32_to_cpu(ucode->u.v1.init_data_size));
Johannes Berg15854ef92012-03-05 11:24:50 -0800361 src = ucode->u.v1.data;
362 break;
363 }
364
365 if (build)
366 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a62012-03-06 13:30:38 -0800367 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef92012-03-05 11:24:50 -0800368 ? " (EXP)" : "");
369 else
370 buildstr[0] = '\0';
371
Johannes Berg965974a62012-03-06 13:30:38 -0800372 snprintf(drv->fw.fw_version,
373 sizeof(drv->fw.fw_version),
Johannes Berg15854ef92012-03-05 11:24:50 -0800374 "%u.%u.%u.%u%s",
Johannes Berg965974a62012-03-06 13:30:38 -0800375 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
376 IWL_UCODE_MINOR(drv->fw.ucode_ver),
377 IWL_UCODE_API(drv->fw.ucode_ver),
378 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef92012-03-05 11:24:50 -0800379 buildstr);
380
381 /* Verify size of file vs. image size info in file's header */
David Spinadel0cedacc2012-03-10 13:00:12 -0800382
383 if (ucode_raw->size != hdr_size +
384 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
385 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
386 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
387 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800388
Johannes Berg965974a62012-03-06 13:30:38 -0800389 IWL_ERR(drv,
Johannes Berg15854ef92012-03-05 11:24:50 -0800390 "uCode file size %d does not match expected size\n",
391 (int)ucode_raw->size);
392 return -EINVAL;
393 }
394
Johannes Berg15854ef92012-03-05 11:24:50 -0800395
David Spinadel0cedacc2012-03-10 13:00:12 -0800396 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
397 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
398 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
399 IWLAGN_RTC_INST_LOWER_BOUND);
400 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
401 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
402 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
403 IWLAGN_RTC_DATA_LOWER_BOUND);
404 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
405 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
406 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
407 IWLAGN_RTC_INST_LOWER_BOUND);
408 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
409 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
410 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
411 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800412 return 0;
413}
414
Johannes Berg965974a62012-03-06 13:30:38 -0800415static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
Johannes Berg15854ef92012-03-05 11:24:50 -0800416 const struct firmware *ucode_raw,
David Spinadel0cedacc2012-03-10 13:00:12 -0800417 struct iwl_firmware_pieces *pieces,
Johannes Berg15854ef92012-03-05 11:24:50 -0800418 struct iwl_ucode_capabilities *capa)
419{
420 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
421 struct iwl_ucode_tlv *tlv;
422 size_t len = ucode_raw->size;
423 const u8 *data;
Johannes Berg15854ef92012-03-05 11:24:50 -0800424 u32 tlv_len;
425 enum iwl_ucode_tlv_type tlv_type;
426 const u8 *tlv_data;
427 char buildstr[25];
428 u32 build;
429
430 if (len < sizeof(*ucode)) {
Johannes Berg965974a62012-03-06 13:30:38 -0800431 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
Johannes Berg15854ef92012-03-05 11:24:50 -0800432 return -EINVAL;
433 }
434
435 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
Johannes Berg965974a62012-03-06 13:30:38 -0800436 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
Johannes Berg15854ef92012-03-05 11:24:50 -0800437 le32_to_cpu(ucode->magic));
438 return -EINVAL;
439 }
440
Johannes Berg965974a62012-03-06 13:30:38 -0800441 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
Johannes Berg15854ef92012-03-05 11:24:50 -0800442 build = le32_to_cpu(ucode->build);
443
444 if (build)
445 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a62012-03-06 13:30:38 -0800446 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef92012-03-05 11:24:50 -0800447 ? " (EXP)" : "");
448 else
449 buildstr[0] = '\0';
450
Johannes Berg965974a62012-03-06 13:30:38 -0800451 snprintf(drv->fw.fw_version,
452 sizeof(drv->fw.fw_version),
Johannes Berg15854ef92012-03-05 11:24:50 -0800453 "%u.%u.%u.%u%s",
Johannes Berg965974a62012-03-06 13:30:38 -0800454 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
455 IWL_UCODE_MINOR(drv->fw.ucode_ver),
456 IWL_UCODE_API(drv->fw.ucode_ver),
457 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef92012-03-05 11:24:50 -0800458 buildstr);
459
460 data = ucode->data;
461
462 len -= sizeof(*ucode);
463
464 while (len >= sizeof(*tlv)) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800465 len -= sizeof(*tlv);
466 tlv = (void *)data;
467
468 tlv_len = le32_to_cpu(tlv->length);
Johannes Berg0479c192012-03-09 09:16:35 +0100469 tlv_type = le32_to_cpu(tlv->type);
Johannes Berg15854ef92012-03-05 11:24:50 -0800470 tlv_data = tlv->data;
471
472 if (len < tlv_len) {
Johannes Berg965974a62012-03-06 13:30:38 -0800473 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
Johannes Berg15854ef92012-03-05 11:24:50 -0800474 len, tlv_len);
475 return -EINVAL;
476 }
477 len -= ALIGN(tlv_len, 4);
478 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
479
Johannes Berg15854ef92012-03-05 11:24:50 -0800480 switch (tlv_type) {
481 case IWL_UCODE_TLV_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800482 set_sec_data(pieces, IWL_UCODE_REGULAR,
483 IWL_UCODE_SECTION_INST, tlv_data);
484 set_sec_size(pieces, IWL_UCODE_REGULAR,
485 IWL_UCODE_SECTION_INST, tlv_len);
486 set_sec_offset(pieces, IWL_UCODE_REGULAR,
487 IWL_UCODE_SECTION_INST,
488 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800489 break;
490 case IWL_UCODE_TLV_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800491 set_sec_data(pieces, IWL_UCODE_REGULAR,
492 IWL_UCODE_SECTION_DATA, tlv_data);
493 set_sec_size(pieces, IWL_UCODE_REGULAR,
494 IWL_UCODE_SECTION_DATA, tlv_len);
495 set_sec_offset(pieces, IWL_UCODE_REGULAR,
496 IWL_UCODE_SECTION_DATA,
497 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800498 break;
499 case IWL_UCODE_TLV_INIT:
David Spinadel0cedacc2012-03-10 13:00:12 -0800500 set_sec_data(pieces, IWL_UCODE_INIT,
501 IWL_UCODE_SECTION_INST, tlv_data);
502 set_sec_size(pieces, IWL_UCODE_INIT,
503 IWL_UCODE_SECTION_INST, tlv_len);
504 set_sec_offset(pieces, IWL_UCODE_INIT,
505 IWL_UCODE_SECTION_INST,
506 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800507 break;
508 case IWL_UCODE_TLV_INIT_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800509 set_sec_data(pieces, IWL_UCODE_INIT,
510 IWL_UCODE_SECTION_DATA, tlv_data);
511 set_sec_size(pieces, IWL_UCODE_INIT,
512 IWL_UCODE_SECTION_DATA, tlv_len);
513 set_sec_offset(pieces, IWL_UCODE_INIT,
514 IWL_UCODE_SECTION_DATA,
515 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800516 break;
517 case IWL_UCODE_TLV_BOOT:
Johannes Berg965974a62012-03-06 13:30:38 -0800518 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800519 break;
520 case IWL_UCODE_TLV_PROBE_MAX_LEN:
521 if (tlv_len != sizeof(u32))
522 goto invalid_tlv_len;
523 capa->max_probe_length =
524 le32_to_cpup((__le32 *)tlv_data);
525 break;
526 case IWL_UCODE_TLV_PAN:
527 if (tlv_len)
528 goto invalid_tlv_len;
529 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
530 break;
531 case IWL_UCODE_TLV_FLAGS:
532 /* must be at least one u32 */
533 if (tlv_len < sizeof(u32))
534 goto invalid_tlv_len;
535 /* and a proper number of u32s */
536 if (tlv_len % sizeof(u32))
537 goto invalid_tlv_len;
538 /*
539 * This driver only reads the first u32 as
540 * right now no more features are defined,
541 * if that changes then either the driver
542 * will not work with the new firmware, or
543 * it'll not take advantage of new features.
544 */
545 capa->flags = le32_to_cpup((__le32 *)tlv_data);
546 break;
547 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
548 if (tlv_len != sizeof(u32))
549 goto invalid_tlv_len;
550 pieces->init_evtlog_ptr =
551 le32_to_cpup((__le32 *)tlv_data);
552 break;
553 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
554 if (tlv_len != sizeof(u32))
555 goto invalid_tlv_len;
556 pieces->init_evtlog_size =
557 le32_to_cpup((__le32 *)tlv_data);
558 break;
559 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
560 if (tlv_len != sizeof(u32))
561 goto invalid_tlv_len;
562 pieces->init_errlog_ptr =
563 le32_to_cpup((__le32 *)tlv_data);
564 break;
565 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
566 if (tlv_len != sizeof(u32))
567 goto invalid_tlv_len;
568 pieces->inst_evtlog_ptr =
569 le32_to_cpup((__le32 *)tlv_data);
570 break;
571 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
572 if (tlv_len != sizeof(u32))
573 goto invalid_tlv_len;
574 pieces->inst_evtlog_size =
575 le32_to_cpup((__le32 *)tlv_data);
576 break;
577 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
578 if (tlv_len != sizeof(u32))
579 goto invalid_tlv_len;
580 pieces->inst_errlog_ptr =
581 le32_to_cpup((__le32 *)tlv_data);
582 break;
583 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
584 if (tlv_len)
585 goto invalid_tlv_len;
Johannes Berg965974a62012-03-06 13:30:38 -0800586 drv->fw.enhance_sensitivity_table = true;
Johannes Berg15854ef92012-03-05 11:24:50 -0800587 break;
588 case IWL_UCODE_TLV_WOWLAN_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800589 set_sec_data(pieces, IWL_UCODE_WOWLAN,
590 IWL_UCODE_SECTION_INST, tlv_data);
591 set_sec_size(pieces, IWL_UCODE_WOWLAN,
592 IWL_UCODE_SECTION_INST, tlv_len);
593 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
594 IWL_UCODE_SECTION_INST,
595 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800596 break;
597 case IWL_UCODE_TLV_WOWLAN_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800598 set_sec_data(pieces, IWL_UCODE_WOWLAN,
599 IWL_UCODE_SECTION_DATA, tlv_data);
600 set_sec_size(pieces, IWL_UCODE_WOWLAN,
601 IWL_UCODE_SECTION_DATA, tlv_len);
602 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
603 IWL_UCODE_SECTION_DATA,
604 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800605 break;
606 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
607 if (tlv_len != sizeof(u32))
608 goto invalid_tlv_len;
609 capa->standard_phy_calibration_size =
610 le32_to_cpup((__le32 *)tlv_data);
611 break;
David Spinadeled8c8362012-03-10 13:00:13 -0800612 case IWL_UCODE_TLV_SEC_RT:
613 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
614 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800615 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800616 break;
617 case IWL_UCODE_TLV_SEC_INIT:
618 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
619 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800620 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800621 break;
622 case IWL_UCODE_TLV_SEC_WOWLAN:
623 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
624 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800625 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800626 break;
627 case IWL_UCODE_TLV_DEF_CALIB:
628 if (tlv_len != sizeof(struct iwl_tlv_calib_data))
629 goto invalid_tlv_len;
630 if (iwl_set_default_calib(drv, tlv_data))
631 goto tlv_error;
632 break;
633 case IWL_UCODE_TLV_PHY_SKU:
634 if (tlv_len != sizeof(u32))
635 goto invalid_tlv_len;
636 drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
637 break;
Johannes Berg15854ef92012-03-05 11:24:50 -0800638 default:
Johannes Berg965974a62012-03-06 13:30:38 -0800639 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
Johannes Berg15854ef92012-03-05 11:24:50 -0800640 break;
641 }
642 }
643
644 if (len) {
Johannes Berg965974a62012-03-06 13:30:38 -0800645 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
646 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
Johannes Berg15854ef92012-03-05 11:24:50 -0800647 return -EINVAL;
648 }
649
650 return 0;
651
652 invalid_tlv_len:
Johannes Berg965974a62012-03-06 13:30:38 -0800653 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
David Spinadeled8c8362012-03-10 13:00:13 -0800654 tlv_error:
Johannes Berg965974a62012-03-06 13:30:38 -0800655 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
Johannes Berg15854ef92012-03-05 11:24:50 -0800656
657 return -EINVAL;
658}
659
David Spinadel6dfa8d02012-03-10 13:00:14 -0800660static int alloc_pci_desc(struct iwl_drv *drv,
661 struct iwl_firmware_pieces *pieces,
662 enum iwl_ucode_type type)
663{
664 int i;
665 for (i = 0;
666 i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
667 i++)
668 if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
669 get_sec(pieces, type, i)))
670 return -1;
671 return 0;
672}
673
674static int validate_sec_sizes(struct iwl_drv *drv,
675 struct iwl_firmware_pieces *pieces,
676 const struct iwl_cfg *cfg)
677{
678 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
679 get_sec_size(pieces, IWL_UCODE_REGULAR,
680 IWL_UCODE_SECTION_INST));
681 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
682 get_sec_size(pieces, IWL_UCODE_REGULAR,
683 IWL_UCODE_SECTION_DATA));
684 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
685 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
686 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
687 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
688
689 /* Verify that uCode images will fit in card's SRAM. */
690 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
691 cfg->max_inst_size) {
692 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
693 get_sec_size(pieces, IWL_UCODE_REGULAR,
694 IWL_UCODE_SECTION_INST));
695 return -1;
696 }
697
698 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
699 cfg->max_data_size) {
700 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
701 get_sec_size(pieces, IWL_UCODE_REGULAR,
702 IWL_UCODE_SECTION_DATA));
703 return -1;
704 }
705
706 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
707 cfg->max_inst_size) {
708 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
709 get_sec_size(pieces, IWL_UCODE_INIT,
710 IWL_UCODE_SECTION_INST));
711 return -1;
712 }
713
714 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
715 cfg->max_data_size) {
716 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
717 get_sec_size(pieces, IWL_UCODE_REGULAR,
718 IWL_UCODE_SECTION_DATA));
719 return -1;
720 }
721 return 0;
722}
723
724
Johannes Berg15854ef92012-03-05 11:24:50 -0800725/**
726 * iwl_ucode_callback - callback when firmware was loaded
727 *
728 * If loaded successfully, copies the firmware into buffers
729 * for the card to fetch (via DMA).
730 */
731static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
732{
Johannes Berg965974a62012-03-06 13:30:38 -0800733 struct iwl_drv *drv = context;
Johannes Berg965974a62012-03-06 13:30:38 -0800734 struct iwl_fw *fw = &drv->fw;
Johannes Berg15854ef92012-03-05 11:24:50 -0800735 struct iwl_ucode_header *ucode;
736 int err;
David Spinadel0cedacc2012-03-10 13:00:12 -0800737 struct iwl_firmware_pieces pieces;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200738 const unsigned int api_max = drv->cfg->ucode_api_max;
739 unsigned int api_ok = drv->cfg->ucode_api_ok;
740 const unsigned int api_min = drv->cfg->ucode_api_min;
Johannes Berg15854ef92012-03-05 11:24:50 -0800741 u32 api_ver;
David Spinadel6dfa8d02012-03-10 13:00:14 -0800742 int i;
Johannes Berg15854ef92012-03-05 11:24:50 -0800743
744 fw->ucode_capa.max_probe_length = 200;
745 fw->ucode_capa.standard_phy_calibration_size =
746 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
747
748 if (!api_ok)
749 api_ok = api_max;
750
751 memset(&pieces, 0, sizeof(pieces));
752
753 if (!ucode_raw) {
Johannes Berg965974a62012-03-06 13:30:38 -0800754 if (drv->fw_index <= api_ok)
755 IWL_ERR(drv,
Johannes Berg15854ef92012-03-05 11:24:50 -0800756 "request for firmware file '%s' failed.\n",
Johannes Berg965974a62012-03-06 13:30:38 -0800757 drv->firmware_name);
Johannes Berg15854ef92012-03-05 11:24:50 -0800758 goto try_again;
759 }
760
Johannes Berg965974a62012-03-06 13:30:38 -0800761 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
762 drv->firmware_name, ucode_raw->size);
Johannes Berg15854ef92012-03-05 11:24:50 -0800763
764 /* Make sure that we got at least the API version number */
765 if (ucode_raw->size < 4) {
Johannes Berg965974a62012-03-06 13:30:38 -0800766 IWL_ERR(drv, "File size way too small!\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800767 goto try_again;
768 }
769
770 /* Data from ucode file: header followed by uCode images */
771 ucode = (struct iwl_ucode_header *)ucode_raw->data;
772
773 if (ucode->ver)
Johannes Berg965974a62012-03-06 13:30:38 -0800774 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
Johannes Berg15854ef92012-03-05 11:24:50 -0800775 else
Johannes Berg965974a62012-03-06 13:30:38 -0800776 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
Johannes Berg15854ef92012-03-05 11:24:50 -0800777 &fw->ucode_capa);
778
779 if (err)
780 goto try_again;
781
Johannes Berg965974a62012-03-06 13:30:38 -0800782 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef92012-03-05 11:24:50 -0800783
784 /*
785 * api_ver should match the api version forming part of the
786 * firmware filename ... but we don't check for that and only rely
787 * on the API version read from firmware header from here on forward
788 */
789 /* no api version check required for experimental uCode */
Johannes Berg965974a62012-03-06 13:30:38 -0800790 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800791 if (api_ver < api_min || api_ver > api_max) {
Johannes Berg965974a62012-03-06 13:30:38 -0800792 IWL_ERR(drv,
Johannes Berg15854ef92012-03-05 11:24:50 -0800793 "Driver unable to support your firmware API. "
794 "Driver supports v%u, firmware is v%u.\n",
795 api_max, api_ver);
796 goto try_again;
797 }
798
799 if (api_ver < api_ok) {
800 if (api_ok != api_max)
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 through v%u, got v%u.\n",
803 api_ok, api_max, api_ver);
804 else
Johannes Berg965974a62012-03-06 13:30:38 -0800805 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef92012-03-05 11:24:50 -0800806 "expected v%u, got v%u.\n",
807 api_max, api_ver);
Johannes Berg965974a62012-03-06 13:30:38 -0800808 IWL_ERR(drv, "New firmware can be obtained from "
Johannes Berg15854ef92012-03-05 11:24:50 -0800809 "http://www.intellinuxwireless.org/.\n");
810 }
811 }
812
Johannes Berg965974a62012-03-06 13:30:38 -0800813 IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
Johannes Berg15854ef92012-03-05 11:24:50 -0800814
815 /*
David Spinadel4db2c9a2012-03-10 13:00:15 -0800816 * In mvm uCode there is no difference between data and instructions
817 * sections.
818 */
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200819 if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
Johannes Berg15854ef92012-03-05 11:24:50 -0800820 goto try_again;
Johannes Berg15854ef92012-03-05 11:24:50 -0800821
822 /* Allocate ucode buffers for card's bus-master loading ... */
823
824 /* Runtime instructions and 2 copies of data:
825 * 1) unmodified from disk
826 * 2) backup cache for save/restore during power-downs */
David Spinadel6dfa8d02012-03-10 13:00:14 -0800827 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
828 if (alloc_pci_desc(drv, &pieces, i))
Johannes Berg15854ef92012-03-05 11:24:50 -0800829 goto err_pci_alloc;
Johannes Berg15854ef92012-03-05 11:24:50 -0800830
831 /* Now that we can no longer fail, copy information */
832
833 /*
834 * The (size - 16) / 12 formula is based on the information recorded
835 * for each event, which is of mode 1 (including timestamp) for all
836 * new microcodes that include this information.
837 */
Johannes Berg0692fe42012-03-06 13:30:37 -0800838 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
Johannes Berg15854ef92012-03-05 11:24:50 -0800839 if (pieces.init_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800840 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
Johannes Berg15854ef92012-03-05 11:24:50 -0800841 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800842 fw->init_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200843 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800844 fw->init_errlog_ptr = pieces.init_errlog_ptr;
845 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
Johannes Berg15854ef92012-03-05 11:24:50 -0800846 if (pieces.inst_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800847 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
Johannes Berg15854ef92012-03-05 11:24:50 -0800848 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800849 fw->inst_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200850 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800851 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
Johannes Berg15854ef92012-03-05 11:24:50 -0800852
853 /*
854 * figure out the offset of chain noise reset and gain commands
855 * base on the size of standard phy calibration commands table size
856 */
857 if (fw->ucode_capa.standard_phy_calibration_size >
858 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
859 fw->ucode_capa.standard_phy_calibration_size =
860 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
861
862 /* We have our copies now, allow OS release its copies */
863 release_firmware(ucode_raw);
Johannes Berg965974a62012-03-06 13:30:38 -0800864 complete(&drv->request_firmware_complete);
Johannes Berg15854ef92012-03-05 11:24:50 -0800865
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200866 drv->op_mode = iwl_dvm_ops.start(drv->trans, drv->cfg, &drv->fw);
Johannes Berg15854ef92012-03-05 11:24:50 -0800867
Johannes Berg965974a62012-03-06 13:30:38 -0800868 if (!drv->op_mode)
Johannes Berg15854ef92012-03-05 11:24:50 -0800869 goto out_unbind;
870
871 return;
872
873 try_again:
874 /* try next, if any */
875 release_firmware(ucode_raw);
Johannes Berg965974a62012-03-06 13:30:38 -0800876 if (iwl_request_firmware(drv, false))
Johannes Berg15854ef92012-03-05 11:24:50 -0800877 goto out_unbind;
878 return;
879
880 err_pci_alloc:
Johannes Berg965974a62012-03-06 13:30:38 -0800881 IWL_ERR(drv, "failed to allocate pci memory\n");
882 iwl_dealloc_ucode(drv);
Johannes Berg15854ef92012-03-05 11:24:50 -0800883 release_firmware(ucode_raw);
884 out_unbind:
Johannes Berg965974a62012-03-06 13:30:38 -0800885 complete(&drv->request_firmware_complete);
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200886 device_release_driver(drv->trans->dev);
Johannes Berg15854ef92012-03-05 11:24:50 -0800887}
888
Emmanuel Grumbach87ce05a2012-03-26 09:03:18 -0700889struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700890 const struct iwl_cfg *cfg)
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200891{
Johannes Berg965974a62012-03-06 13:30:38 -0800892 struct iwl_drv *drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200893 int ret;
894
Johannes Berg965974a62012-03-06 13:30:38 -0800895 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +0300896 if (!drv)
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700897 return NULL;
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +0300898
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700899 drv->trans = trans;
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +0200900 drv->dev = trans->dev;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200901 drv->cfg = cfg;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200902
Johannes Berg965974a62012-03-06 13:30:38 -0800903 init_completion(&drv->request_firmware_complete);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200904
Johannes Berg965974a62012-03-06 13:30:38 -0800905 ret = iwl_request_firmware(drv, true);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200906
907 if (ret) {
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +0300908 IWL_ERR(trans, "Couldn't request the fw\n");
Johannes Berg965974a62012-03-06 13:30:38 -0800909 kfree(drv);
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700910 drv = NULL;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200911 }
912
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700913 return drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200914}
915
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700916void iwl_drv_stop(struct iwl_drv *drv)
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200917{
Johannes Berg965974a62012-03-06 13:30:38 -0800918 wait_for_completion(&drv->request_firmware_complete);
Johannes Berg2e7eb112012-03-05 11:24:51 -0800919
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200920 /* op_mode can be NULL if its start failed */
Johannes Berg965974a62012-03-06 13:30:38 -0800921 if (drv->op_mode)
922 iwl_op_mode_stop(drv->op_mode);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200923
Johannes Berg965974a62012-03-06 13:30:38 -0800924 iwl_dealloc_ucode(drv);
Johannes Berg7db5b982012-03-05 11:24:48 -0800925
Johannes Berg965974a62012-03-06 13:30:38 -0800926 kfree(drv);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200927}
Johannes Berg65de7e82012-04-17 07:36:30 -0700928
929
930/* shared module parameters */
931struct iwl_mod_params iwlwifi_mod_params = {
932 .amsdu_size_8K = 1,
933 .restart_fw = 1,
934 .plcp_check = true,
935 .bt_coex_active = true,
936 .power_level = IWL_POWER_INDEX_1,
937 .bt_ch_announce = true,
938 .auto_agg = true,
939 /* the rest are 0 by default */
940};
941
942#ifdef CONFIG_IWLWIFI_DEBUG
943module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
944 S_IRUGO | S_IWUSR);
945MODULE_PARM_DESC(debug, "debug output mask");
946#endif
947
948module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
949MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
950module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
951MODULE_PARM_DESC(11n_disable,
952 "disable 11n functionality, bitmap: 1: full, 2: agg TX, 4: agg RX");
953module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
954 int, S_IRUGO);
955MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
956module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, int, S_IRUGO);
957MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");
958
959module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
960 int, S_IRUGO);
961MODULE_PARM_DESC(antenna_coupling,
962 "specify antenna coupling in dB (defualt: 0 dB)");
963
964module_param_named(bt_ch_inhibition, iwlwifi_mod_params.bt_ch_announce,
965 bool, S_IRUGO);
966MODULE_PARM_DESC(bt_ch_inhibition,
967 "Enable BT channel inhibition (default: enable)");
968
969module_param_named(plcp_check, iwlwifi_mod_params.plcp_check, bool, S_IRUGO);
970MODULE_PARM_DESC(plcp_check, "Check plcp health (default: 1 [enabled])");
971
972module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
973MODULE_PARM_DESC(wd_disable,
974 "Disable stuck queue watchdog timer 0=system default, "
975 "1=disable, 2=enable (default: 0)");
976
977/*
978 * set bt_coex_active to true, uCode will do kill/defer
979 * every time the priority line is asserted (BT is sending signals on the
980 * priority line in the PCIx).
981 * set bt_coex_active to false, uCode will ignore the BT activity and
982 * perform the normal operation
983 *
984 * User might experience transmit issue on some platform due to WiFi/BT
985 * co-exist problem. The possible behaviors are:
986 * Able to scan and finding all the available AP
987 * Not able to associate with any AP
988 * On those platforms, WiFi communication can be restored by set
989 * "bt_coex_active" module parameter to "false"
990 *
991 * default: bt_coex_active = true (BT_COEX_ENABLE)
992 */
993module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
994 bool, S_IRUGO);
995MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
996
997module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
998MODULE_PARM_DESC(led_mode, "0=system default, "
999 "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
1000
1001module_param_named(power_save, iwlwifi_mod_params.power_save,
1002 bool, S_IRUGO);
1003MODULE_PARM_DESC(power_save,
1004 "enable WiFi power management (default: disable)");
1005
1006module_param_named(power_level, iwlwifi_mod_params.power_level,
1007 int, S_IRUGO);
1008MODULE_PARM_DESC(power_level,
1009 "default power save level (range from 1 - 5, default: 1)");
1010
1011module_param_named(auto_agg, iwlwifi_mod_params.auto_agg,
1012 bool, S_IRUGO);
1013MODULE_PARM_DESC(auto_agg,
1014 "enable agg w/o check traffic load (default: enable)");
Stanislaw Gruszkaf6b11542012-04-19 09:51:24 -07001015
1016module_param_named(5ghz_disable, iwlwifi_mod_params.disable_5ghz,
1017 bool, S_IRUGO);
1018MODULE_PARM_DESC(5ghz_disable, "disable 5GHz band (default: 0 [enabled])");