blob: cdfdfaec395e092a47fae6a10c9725b454b06d1f [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
Don Frycc5f7e32012-05-16 22:54:27 +020080/******************************************************************************
81 *
82 * module boiler plate
83 *
84 ******************************************************************************/
85
86/*
87 * module name, copyright, version, etc.
88 */
89#define DRV_DESCRIPTION "Intel(R) Wireless WiFi driver for Linux"
90
91#ifdef CONFIG_IWLWIFI_DEBUG
92#define VD "d"
93#else
94#define VD
95#endif
96
97#define DRV_VERSION IWLWIFI_VERSION VD
98
99MODULE_DESCRIPTION(DRV_DESCRIPTION);
100MODULE_VERSION(DRV_VERSION);
101MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
102MODULE_LICENSE("GPL");
103
Johannes Berg965974a62012-03-06 13:30:38 -0800104/**
105 * struct iwl_drv - drv common data
Don Frycc5f7e32012-05-16 22:54:27 +0200106 * @list: list of drv structures using this opmode
Johannes Berg965974a62012-03-06 13:30:38 -0800107 * @fw: the iwl_fw structure
Johannes Berg965974a62012-03-06 13:30:38 -0800108 * @op_mode: the running op_mode
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200109 * @trans: transport layer
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +0200110 * @dev: for debug prints only
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200111 * @cfg: configuration struct
Johannes Berg965974a62012-03-06 13:30:38 -0800112 * @fw_index: firmware revision to try loading
113 * @firmware_name: composite filename of ucode file to load
114 * @request_firmware_complete: the firmware has been obtained from user space
115 */
116struct iwl_drv {
Don Frycc5f7e32012-05-16 22:54:27 +0200117 struct list_head list;
Johannes Berg965974a62012-03-06 13:30:38 -0800118 struct iwl_fw fw;
119
Johannes Berg965974a62012-03-06 13:30:38 -0800120 struct iwl_op_mode *op_mode;
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700121 struct iwl_trans *trans;
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +0200122 struct device *dev;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200123 const struct iwl_cfg *cfg;
Johannes Berg965974a62012-03-06 13:30:38 -0800124
125 int fw_index; /* firmware we're trying to load */
126 char firmware_name[25]; /* name of firmware file to load */
127
128 struct completion request_firmware_complete;
129};
130
Don Frycc5f7e32012-05-16 22:54:27 +0200131#define DVM_OP_MODE 0
132#define MVM_OP_MODE 1
Johannes Berg965974a62012-03-06 13:30:38 -0800133
Don Frycc5f7e32012-05-16 22:54:27 +0200134static struct iwlwifi_opmode_table {
135 const char *name; /* name: iwldvm, iwlmvm, etc */
136 const struct iwl_op_mode_ops *ops; /* pointer to op_mode ops */
137 struct list_head drv; /* list of devices using this op_mode */
138} iwlwifi_opmode_table[] = { /* ops set when driver is initialized */
139 { .name = "iwldvm", .ops = NULL },
140 { .name = "iwlmvm", .ops = NULL },
141};
Johannes Berg965974a62012-03-06 13:30:38 -0800142
David Spinadel0cedacc2012-03-10 13:00:12 -0800143/*
144 * struct fw_sec: Just for the image parsing proccess.
145 * For the fw storage we are using struct fw_desc.
146 */
147struct fw_sec {
148 const void *data; /* the sec data */
149 size_t size; /* section size */
150 u32 offset; /* offset of writing in the device */
151};
152
Johannes Berg965974a62012-03-06 13:30:38 -0800153static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
Johannes Berg15854ef92012-03-05 11:24:50 -0800154{
155 if (desc->v_addr)
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200156 dma_free_coherent(drv->trans->dev, desc->len,
Johannes Berg15854ef92012-03-05 11:24:50 -0800157 desc->v_addr, desc->p_addr);
158 desc->v_addr = NULL;
159 desc->len = 0;
160}
161
Johannes Berg965974a62012-03-06 13:30:38 -0800162static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
Johannes Berg15854ef92012-03-05 11:24:50 -0800163{
David Spinadel6dfa8d02012-03-10 13:00:14 -0800164 int i;
165 for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
166 iwl_free_fw_desc(drv, &img->sec[i]);
Johannes Berg15854ef92012-03-05 11:24:50 -0800167}
168
Johannes Berg965974a62012-03-06 13:30:38 -0800169static void iwl_dealloc_ucode(struct iwl_drv *drv)
Johannes Berg15854ef92012-03-05 11:24:50 -0800170{
David Spinadel6dfa8d02012-03-10 13:00:14 -0800171 int i;
172 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
173 iwl_free_fw_img(drv, drv->fw.img + i);
Johannes Berg15854ef92012-03-05 11:24:50 -0800174}
175
Johannes Berg965974a62012-03-06 13:30:38 -0800176static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
David Spinadel0cedacc2012-03-10 13:00:12 -0800177 struct fw_sec *sec)
Johannes Berg15854ef92012-03-05 11:24:50 -0800178{
David Spinadel0cedacc2012-03-10 13:00:12 -0800179 if (!sec || !sec->size) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800180 desc->v_addr = NULL;
181 return -EINVAL;
182 }
183
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200184 desc->v_addr = dma_alloc_coherent(drv->trans->dev, sec->size,
Johannes Berg15854ef92012-03-05 11:24:50 -0800185 &desc->p_addr, GFP_KERNEL);
186 if (!desc->v_addr)
187 return -ENOMEM;
188
David Spinadel0cedacc2012-03-10 13:00:12 -0800189 desc->len = sec->size;
190 desc->offset = sec->offset;
191 memcpy(desc->v_addr, sec->data, sec->size);
Johannes Berg15854ef92012-03-05 11:24:50 -0800192 return 0;
193}
194
195static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
196
197#define UCODE_EXPERIMENTAL_INDEX 100
198#define UCODE_EXPERIMENTAL_TAG "exp"
199
Johannes Berg965974a62012-03-06 13:30:38 -0800200static int iwl_request_firmware(struct iwl_drv *drv, bool first)
Johannes Berg15854ef92012-03-05 11:24:50 -0800201{
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200202 const char *name_pre = drv->cfg->fw_name_pre;
Johannes Berg15854ef92012-03-05 11:24:50 -0800203 char tag[8];
204
205 if (first) {
206#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
Johannes Berg965974a62012-03-06 13:30:38 -0800207 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
Johannes Berg15854ef92012-03-05 11:24:50 -0800208 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
Johannes Berg965974a62012-03-06 13:30:38 -0800209 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800210#endif
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200211 drv->fw_index = drv->cfg->ucode_api_max;
Johannes Berg965974a62012-03-06 13:30:38 -0800212 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef92012-03-05 11:24:50 -0800213 } else {
Johannes Berg965974a62012-03-06 13:30:38 -0800214 drv->fw_index--;
215 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef92012-03-05 11:24:50 -0800216 }
217
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200218 if (drv->fw_index < drv->cfg->ucode_api_min) {
Johannes Berg965974a62012-03-06 13:30:38 -0800219 IWL_ERR(drv, "no suitable firmware found!\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800220 return -ENOENT;
221 }
222
Johannes Berg965974a62012-03-06 13:30:38 -0800223 sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Johannes Berg15854ef92012-03-05 11:24:50 -0800224
Johannes Berg965974a62012-03-06 13:30:38 -0800225 IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
226 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef92012-03-05 11:24:50 -0800227 ? "EXPERIMENTAL " : "",
Johannes Berg965974a62012-03-06 13:30:38 -0800228 drv->firmware_name);
Johannes Berg15854ef92012-03-05 11:24:50 -0800229
Johannes Berg965974a62012-03-06 13:30:38 -0800230 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200231 drv->trans->dev,
Johannes Berg965974a62012-03-06 13:30:38 -0800232 GFP_KERNEL, drv, iwl_ucode_callback);
Johannes Berg15854ef92012-03-05 11:24:50 -0800233}
234
David Spinadel0cedacc2012-03-10 13:00:12 -0800235struct fw_img_parsing {
David Spinadel6dfa8d02012-03-10 13:00:14 -0800236 struct fw_sec sec[IWL_UCODE_SECTION_MAX];
David Spinadel0cedacc2012-03-10 13:00:12 -0800237 int sec_counter;
238};
239
David Spinadeled8c8362012-03-10 13:00:13 -0800240/*
241 * struct fw_sec_parsing: to extract fw section and it's offset from tlv
242 */
243struct fw_sec_parsing {
244 __le32 offset;
245 const u8 data[];
246} __packed;
247
248/**
249 * struct iwl_tlv_calib_data - parse the default calib data from TLV
250 *
251 * @ucode_type: the uCode to which the following default calib relates.
252 * @calib: default calibrations.
253 */
254struct iwl_tlv_calib_data {
255 __le32 ucode_type;
256 __le64 calib;
257} __packed;
258
David Spinadel0cedacc2012-03-10 13:00:12 -0800259struct iwl_firmware_pieces {
260 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
Johannes Berg15854ef92012-03-05 11:24:50 -0800261
262 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
263 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
264};
265
David Spinadel0cedacc2012-03-10 13:00:12 -0800266/*
267 * These functions are just to extract uCode section data from the pieces
268 * structure.
269 */
270static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
271 enum iwl_ucode_type type,
272 int sec)
273{
274 return &pieces->img[type].sec[sec];
275}
276
277static void set_sec_data(struct iwl_firmware_pieces *pieces,
278 enum iwl_ucode_type type,
279 int sec,
280 const void *data)
281{
282 pieces->img[type].sec[sec].data = data;
283}
284
285static void set_sec_size(struct iwl_firmware_pieces *pieces,
286 enum iwl_ucode_type type,
287 int sec,
288 size_t size)
289{
290 pieces->img[type].sec[sec].size = size;
291}
292
293static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
294 enum iwl_ucode_type type,
295 int sec)
296{
297 return pieces->img[type].sec[sec].size;
298}
299
300static void set_sec_offset(struct iwl_firmware_pieces *pieces,
301 enum iwl_ucode_type type,
302 int sec,
303 u32 offset)
304{
305 pieces->img[type].sec[sec].offset = offset;
306}
307
David Spinadeled8c8362012-03-10 13:00:13 -0800308/*
309 * Gets uCode section from tlv.
310 */
311static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
312 const void *data, enum iwl_ucode_type type,
313 int size)
314{
315 struct fw_img_parsing *img;
316 struct fw_sec *sec;
317 struct fw_sec_parsing *sec_parse;
318
319 if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
320 return -1;
321
322 sec_parse = (struct fw_sec_parsing *)data;
323
324 img = &pieces->img[type];
325 sec = &img->sec[img->sec_counter];
326
327 sec->offset = le32_to_cpu(sec_parse->offset);
328 sec->data = sec_parse->data;
David Spinadel1176f432012-03-13 14:32:48 +0200329 sec->size = size - sizeof(sec_parse->offset);
David Spinadeled8c8362012-03-10 13:00:13 -0800330
331 ++img->sec_counter;
332
333 return 0;
334}
335
336static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
337{
338 struct iwl_tlv_calib_data *def_calib =
339 (struct iwl_tlv_calib_data *)data;
340 u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
341 if (ucode_type >= IWL_UCODE_TYPE_MAX) {
342 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
343 ucode_type);
344 return -EINVAL;
345 }
346 drv->fw.default_calib[ucode_type] = le64_to_cpu(def_calib->calib);
347 return 0;
348}
349
Johannes Berg965974a62012-03-06 13:30:38 -0800350static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
David Spinadel0cedacc2012-03-10 13:00:12 -0800351 const struct firmware *ucode_raw,
352 struct iwl_firmware_pieces *pieces)
Johannes Berg15854ef92012-03-05 11:24:50 -0800353{
354 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
355 u32 api_ver, hdr_size, build;
356 char buildstr[25];
357 const u8 *src;
358
Johannes Berg965974a62012-03-06 13:30:38 -0800359 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
360 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef92012-03-05 11:24:50 -0800361
362 switch (api_ver) {
363 default:
364 hdr_size = 28;
365 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a62012-03-06 13:30:38 -0800366 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800367 return -EINVAL;
368 }
369 build = le32_to_cpu(ucode->u.v2.build);
David Spinadel0cedacc2012-03-10 13:00:12 -0800370 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
371 le32_to_cpu(ucode->u.v2.inst_size));
372 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
373 le32_to_cpu(ucode->u.v2.data_size));
374 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
375 le32_to_cpu(ucode->u.v2.init_size));
376 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
377 le32_to_cpu(ucode->u.v2.init_data_size));
Johannes Berg15854ef92012-03-05 11:24:50 -0800378 src = ucode->u.v2.data;
379 break;
380 case 0:
381 case 1:
382 case 2:
383 hdr_size = 24;
384 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a62012-03-06 13:30:38 -0800385 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800386 return -EINVAL;
387 }
388 build = 0;
David Spinadel0cedacc2012-03-10 13:00:12 -0800389 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
390 le32_to_cpu(ucode->u.v1.inst_size));
391 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
392 le32_to_cpu(ucode->u.v1.data_size));
393 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
394 le32_to_cpu(ucode->u.v1.init_size));
395 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
396 le32_to_cpu(ucode->u.v1.init_data_size));
Johannes Berg15854ef92012-03-05 11:24:50 -0800397 src = ucode->u.v1.data;
398 break;
399 }
400
401 if (build)
402 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a62012-03-06 13:30:38 -0800403 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef92012-03-05 11:24:50 -0800404 ? " (EXP)" : "");
405 else
406 buildstr[0] = '\0';
407
Johannes Berg965974a62012-03-06 13:30:38 -0800408 snprintf(drv->fw.fw_version,
409 sizeof(drv->fw.fw_version),
Johannes Berg15854ef92012-03-05 11:24:50 -0800410 "%u.%u.%u.%u%s",
Johannes Berg965974a62012-03-06 13:30:38 -0800411 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
412 IWL_UCODE_MINOR(drv->fw.ucode_ver),
413 IWL_UCODE_API(drv->fw.ucode_ver),
414 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef92012-03-05 11:24:50 -0800415 buildstr);
416
417 /* Verify size of file vs. image size info in file's header */
David Spinadel0cedacc2012-03-10 13:00:12 -0800418
419 if (ucode_raw->size != hdr_size +
420 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
421 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
422 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
423 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800424
Johannes Berg965974a62012-03-06 13:30:38 -0800425 IWL_ERR(drv,
Johannes Berg15854ef92012-03-05 11:24:50 -0800426 "uCode file size %d does not match expected size\n",
427 (int)ucode_raw->size);
428 return -EINVAL;
429 }
430
Johannes Berg15854ef92012-03-05 11:24:50 -0800431
David Spinadel0cedacc2012-03-10 13:00:12 -0800432 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
433 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
434 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
435 IWLAGN_RTC_INST_LOWER_BOUND);
436 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
437 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
438 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
439 IWLAGN_RTC_DATA_LOWER_BOUND);
440 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
441 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
442 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
443 IWLAGN_RTC_INST_LOWER_BOUND);
444 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
445 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
446 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
447 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800448 return 0;
449}
450
Johannes Berg965974a62012-03-06 13:30:38 -0800451static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
Johannes Berg15854ef92012-03-05 11:24:50 -0800452 const struct firmware *ucode_raw,
David Spinadel0cedacc2012-03-10 13:00:12 -0800453 struct iwl_firmware_pieces *pieces,
Johannes Berg15854ef92012-03-05 11:24:50 -0800454 struct iwl_ucode_capabilities *capa)
455{
456 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
457 struct iwl_ucode_tlv *tlv;
458 size_t len = ucode_raw->size;
459 const u8 *data;
Johannes Berg15854ef92012-03-05 11:24:50 -0800460 u32 tlv_len;
461 enum iwl_ucode_tlv_type tlv_type;
462 const u8 *tlv_data;
463 char buildstr[25];
464 u32 build;
465
466 if (len < sizeof(*ucode)) {
Johannes Berg965974a62012-03-06 13:30:38 -0800467 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
Johannes Berg15854ef92012-03-05 11:24:50 -0800468 return -EINVAL;
469 }
470
471 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
Johannes Berg965974a62012-03-06 13:30:38 -0800472 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
Johannes Berg15854ef92012-03-05 11:24:50 -0800473 le32_to_cpu(ucode->magic));
474 return -EINVAL;
475 }
476
Johannes Berg965974a62012-03-06 13:30:38 -0800477 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
Johannes Berg15854ef92012-03-05 11:24:50 -0800478 build = le32_to_cpu(ucode->build);
479
480 if (build)
481 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a62012-03-06 13:30:38 -0800482 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef92012-03-05 11:24:50 -0800483 ? " (EXP)" : "");
484 else
485 buildstr[0] = '\0';
486
Johannes Berg965974a62012-03-06 13:30:38 -0800487 snprintf(drv->fw.fw_version,
488 sizeof(drv->fw.fw_version),
Johannes Berg15854ef92012-03-05 11:24:50 -0800489 "%u.%u.%u.%u%s",
Johannes Berg965974a62012-03-06 13:30:38 -0800490 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
491 IWL_UCODE_MINOR(drv->fw.ucode_ver),
492 IWL_UCODE_API(drv->fw.ucode_ver),
493 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef92012-03-05 11:24:50 -0800494 buildstr);
495
496 data = ucode->data;
497
498 len -= sizeof(*ucode);
499
500 while (len >= sizeof(*tlv)) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800501 len -= sizeof(*tlv);
502 tlv = (void *)data;
503
504 tlv_len = le32_to_cpu(tlv->length);
Johannes Berg0479c192012-03-09 09:16:35 +0100505 tlv_type = le32_to_cpu(tlv->type);
Johannes Berg15854ef92012-03-05 11:24:50 -0800506 tlv_data = tlv->data;
507
508 if (len < tlv_len) {
Johannes Berg965974a62012-03-06 13:30:38 -0800509 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
Johannes Berg15854ef92012-03-05 11:24:50 -0800510 len, tlv_len);
511 return -EINVAL;
512 }
513 len -= ALIGN(tlv_len, 4);
514 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
515
Johannes Berg15854ef92012-03-05 11:24:50 -0800516 switch (tlv_type) {
517 case IWL_UCODE_TLV_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800518 set_sec_data(pieces, IWL_UCODE_REGULAR,
519 IWL_UCODE_SECTION_INST, tlv_data);
520 set_sec_size(pieces, IWL_UCODE_REGULAR,
521 IWL_UCODE_SECTION_INST, tlv_len);
522 set_sec_offset(pieces, IWL_UCODE_REGULAR,
523 IWL_UCODE_SECTION_INST,
524 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800525 break;
526 case IWL_UCODE_TLV_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800527 set_sec_data(pieces, IWL_UCODE_REGULAR,
528 IWL_UCODE_SECTION_DATA, tlv_data);
529 set_sec_size(pieces, IWL_UCODE_REGULAR,
530 IWL_UCODE_SECTION_DATA, tlv_len);
531 set_sec_offset(pieces, IWL_UCODE_REGULAR,
532 IWL_UCODE_SECTION_DATA,
533 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800534 break;
535 case IWL_UCODE_TLV_INIT:
David Spinadel0cedacc2012-03-10 13:00:12 -0800536 set_sec_data(pieces, IWL_UCODE_INIT,
537 IWL_UCODE_SECTION_INST, tlv_data);
538 set_sec_size(pieces, IWL_UCODE_INIT,
539 IWL_UCODE_SECTION_INST, tlv_len);
540 set_sec_offset(pieces, IWL_UCODE_INIT,
541 IWL_UCODE_SECTION_INST,
542 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800543 break;
544 case IWL_UCODE_TLV_INIT_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800545 set_sec_data(pieces, IWL_UCODE_INIT,
546 IWL_UCODE_SECTION_DATA, tlv_data);
547 set_sec_size(pieces, IWL_UCODE_INIT,
548 IWL_UCODE_SECTION_DATA, tlv_len);
549 set_sec_offset(pieces, IWL_UCODE_INIT,
550 IWL_UCODE_SECTION_DATA,
551 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800552 break;
553 case IWL_UCODE_TLV_BOOT:
Johannes Berg965974a62012-03-06 13:30:38 -0800554 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800555 break;
556 case IWL_UCODE_TLV_PROBE_MAX_LEN:
557 if (tlv_len != sizeof(u32))
558 goto invalid_tlv_len;
559 capa->max_probe_length =
560 le32_to_cpup((__le32 *)tlv_data);
561 break;
562 case IWL_UCODE_TLV_PAN:
563 if (tlv_len)
564 goto invalid_tlv_len;
565 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
566 break;
567 case IWL_UCODE_TLV_FLAGS:
568 /* must be at least one u32 */
569 if (tlv_len < sizeof(u32))
570 goto invalid_tlv_len;
571 /* and a proper number of u32s */
572 if (tlv_len % sizeof(u32))
573 goto invalid_tlv_len;
574 /*
575 * This driver only reads the first u32 as
576 * right now no more features are defined,
577 * if that changes then either the driver
578 * will not work with the new firmware, or
579 * it'll not take advantage of new features.
580 */
581 capa->flags = le32_to_cpup((__le32 *)tlv_data);
582 break;
583 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
584 if (tlv_len != sizeof(u32))
585 goto invalid_tlv_len;
586 pieces->init_evtlog_ptr =
587 le32_to_cpup((__le32 *)tlv_data);
588 break;
589 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
590 if (tlv_len != sizeof(u32))
591 goto invalid_tlv_len;
592 pieces->init_evtlog_size =
593 le32_to_cpup((__le32 *)tlv_data);
594 break;
595 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
596 if (tlv_len != sizeof(u32))
597 goto invalid_tlv_len;
598 pieces->init_errlog_ptr =
599 le32_to_cpup((__le32 *)tlv_data);
600 break;
601 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
602 if (tlv_len != sizeof(u32))
603 goto invalid_tlv_len;
604 pieces->inst_evtlog_ptr =
605 le32_to_cpup((__le32 *)tlv_data);
606 break;
607 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
608 if (tlv_len != sizeof(u32))
609 goto invalid_tlv_len;
610 pieces->inst_evtlog_size =
611 le32_to_cpup((__le32 *)tlv_data);
612 break;
613 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
614 if (tlv_len != sizeof(u32))
615 goto invalid_tlv_len;
616 pieces->inst_errlog_ptr =
617 le32_to_cpup((__le32 *)tlv_data);
618 break;
619 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
620 if (tlv_len)
621 goto invalid_tlv_len;
Johannes Berg965974a62012-03-06 13:30:38 -0800622 drv->fw.enhance_sensitivity_table = true;
Johannes Berg15854ef92012-03-05 11:24:50 -0800623 break;
624 case IWL_UCODE_TLV_WOWLAN_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800625 set_sec_data(pieces, IWL_UCODE_WOWLAN,
626 IWL_UCODE_SECTION_INST, tlv_data);
627 set_sec_size(pieces, IWL_UCODE_WOWLAN,
628 IWL_UCODE_SECTION_INST, tlv_len);
629 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
630 IWL_UCODE_SECTION_INST,
631 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800632 break;
633 case IWL_UCODE_TLV_WOWLAN_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800634 set_sec_data(pieces, IWL_UCODE_WOWLAN,
635 IWL_UCODE_SECTION_DATA, tlv_data);
636 set_sec_size(pieces, IWL_UCODE_WOWLAN,
637 IWL_UCODE_SECTION_DATA, tlv_len);
638 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
639 IWL_UCODE_SECTION_DATA,
640 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800641 break;
642 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
643 if (tlv_len != sizeof(u32))
644 goto invalid_tlv_len;
645 capa->standard_phy_calibration_size =
646 le32_to_cpup((__le32 *)tlv_data);
647 break;
David Spinadeled8c8362012-03-10 13:00:13 -0800648 case IWL_UCODE_TLV_SEC_RT:
649 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
650 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800651 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800652 break;
653 case IWL_UCODE_TLV_SEC_INIT:
654 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
655 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800656 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800657 break;
658 case IWL_UCODE_TLV_SEC_WOWLAN:
659 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
660 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800661 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800662 break;
663 case IWL_UCODE_TLV_DEF_CALIB:
664 if (tlv_len != sizeof(struct iwl_tlv_calib_data))
665 goto invalid_tlv_len;
666 if (iwl_set_default_calib(drv, tlv_data))
667 goto tlv_error;
668 break;
669 case IWL_UCODE_TLV_PHY_SKU:
670 if (tlv_len != sizeof(u32))
671 goto invalid_tlv_len;
672 drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
673 break;
Johannes Berg15854ef92012-03-05 11:24:50 -0800674 default:
Johannes Berg965974a62012-03-06 13:30:38 -0800675 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
Johannes Berg15854ef92012-03-05 11:24:50 -0800676 break;
677 }
678 }
679
680 if (len) {
Johannes Berg965974a62012-03-06 13:30:38 -0800681 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
682 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
Johannes Berg15854ef92012-03-05 11:24:50 -0800683 return -EINVAL;
684 }
685
686 return 0;
687
688 invalid_tlv_len:
Johannes Berg965974a62012-03-06 13:30:38 -0800689 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
David Spinadeled8c8362012-03-10 13:00:13 -0800690 tlv_error:
Johannes Berg965974a62012-03-06 13:30:38 -0800691 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
Johannes Berg15854ef92012-03-05 11:24:50 -0800692
693 return -EINVAL;
694}
695
Johannes Berg75813bd2012-05-16 22:49:49 +0200696static int iwl_alloc_ucode(struct iwl_drv *drv,
697 struct iwl_firmware_pieces *pieces,
698 enum iwl_ucode_type type)
David Spinadel6dfa8d02012-03-10 13:00:14 -0800699{
700 int i;
701 for (i = 0;
702 i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
703 i++)
704 if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
Johannes Berg75813bd2012-05-16 22:49:49 +0200705 get_sec(pieces, type, i)))
706 return -ENOMEM;
David Spinadel6dfa8d02012-03-10 13:00:14 -0800707 return 0;
708}
709
710static int validate_sec_sizes(struct iwl_drv *drv,
711 struct iwl_firmware_pieces *pieces,
712 const struct iwl_cfg *cfg)
713{
714 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
715 get_sec_size(pieces, IWL_UCODE_REGULAR,
716 IWL_UCODE_SECTION_INST));
717 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
718 get_sec_size(pieces, IWL_UCODE_REGULAR,
719 IWL_UCODE_SECTION_DATA));
720 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
721 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
722 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
723 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
724
725 /* Verify that uCode images will fit in card's SRAM. */
726 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
727 cfg->max_inst_size) {
728 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
729 get_sec_size(pieces, IWL_UCODE_REGULAR,
730 IWL_UCODE_SECTION_INST));
731 return -1;
732 }
733
734 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
735 cfg->max_data_size) {
736 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
737 get_sec_size(pieces, IWL_UCODE_REGULAR,
738 IWL_UCODE_SECTION_DATA));
739 return -1;
740 }
741
742 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
743 cfg->max_inst_size) {
744 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
745 get_sec_size(pieces, IWL_UCODE_INIT,
746 IWL_UCODE_SECTION_INST));
747 return -1;
748 }
749
750 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
751 cfg->max_data_size) {
752 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
753 get_sec_size(pieces, IWL_UCODE_REGULAR,
754 IWL_UCODE_SECTION_DATA));
755 return -1;
756 }
757 return 0;
758}
759
Johannes Berg15854ef92012-03-05 11:24:50 -0800760/**
761 * iwl_ucode_callback - callback when firmware was loaded
762 *
763 * If loaded successfully, copies the firmware into buffers
764 * for the card to fetch (via DMA).
765 */
766static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
767{
Johannes Berg965974a62012-03-06 13:30:38 -0800768 struct iwl_drv *drv = context;
Johannes Berg965974a62012-03-06 13:30:38 -0800769 struct iwl_fw *fw = &drv->fw;
Johannes Berg15854ef92012-03-05 11:24:50 -0800770 struct iwl_ucode_header *ucode;
Don Frycc5f7e32012-05-16 22:54:27 +0200771 struct iwlwifi_opmode_table *op;
Johannes Berg15854ef92012-03-05 11:24:50 -0800772 int err;
David Spinadel0cedacc2012-03-10 13:00:12 -0800773 struct iwl_firmware_pieces pieces;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200774 const unsigned int api_max = drv->cfg->ucode_api_max;
775 unsigned int api_ok = drv->cfg->ucode_api_ok;
776 const unsigned int api_min = drv->cfg->ucode_api_min;
Johannes Berg15854ef92012-03-05 11:24:50 -0800777 u32 api_ver;
David Spinadel6dfa8d02012-03-10 13:00:14 -0800778 int i;
Johannes Berg15854ef92012-03-05 11:24:50 -0800779
780 fw->ucode_capa.max_probe_length = 200;
781 fw->ucode_capa.standard_phy_calibration_size =
782 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
783
784 if (!api_ok)
785 api_ok = api_max;
786
787 memset(&pieces, 0, sizeof(pieces));
788
789 if (!ucode_raw) {
Johannes Berg965974a62012-03-06 13:30:38 -0800790 if (drv->fw_index <= api_ok)
791 IWL_ERR(drv,
Johannes Berg15854ef92012-03-05 11:24:50 -0800792 "request for firmware file '%s' failed.\n",
Johannes Berg965974a62012-03-06 13:30:38 -0800793 drv->firmware_name);
Johannes Berg15854ef92012-03-05 11:24:50 -0800794 goto try_again;
795 }
796
Johannes Berg965974a62012-03-06 13:30:38 -0800797 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
798 drv->firmware_name, ucode_raw->size);
Johannes Berg15854ef92012-03-05 11:24:50 -0800799
800 /* Make sure that we got at least the API version number */
801 if (ucode_raw->size < 4) {
Johannes Berg965974a62012-03-06 13:30:38 -0800802 IWL_ERR(drv, "File size way too small!\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800803 goto try_again;
804 }
805
806 /* Data from ucode file: header followed by uCode images */
807 ucode = (struct iwl_ucode_header *)ucode_raw->data;
808
809 if (ucode->ver)
Johannes Berg965974a62012-03-06 13:30:38 -0800810 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
Johannes Berg15854ef92012-03-05 11:24:50 -0800811 else
Johannes Berg965974a62012-03-06 13:30:38 -0800812 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
Johannes Berg15854ef92012-03-05 11:24:50 -0800813 &fw->ucode_capa);
814
815 if (err)
816 goto try_again;
817
Johannes Berg965974a62012-03-06 13:30:38 -0800818 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef92012-03-05 11:24:50 -0800819
820 /*
821 * api_ver should match the api version forming part of the
822 * firmware filename ... but we don't check for that and only rely
823 * on the API version read from firmware header from here on forward
824 */
825 /* no api version check required for experimental uCode */
Johannes Berg965974a62012-03-06 13:30:38 -0800826 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800827 if (api_ver < api_min || api_ver > api_max) {
Johannes Berg965974a62012-03-06 13:30:38 -0800828 IWL_ERR(drv,
Johannes Berg15854ef92012-03-05 11:24:50 -0800829 "Driver unable to support your firmware API. "
830 "Driver supports v%u, firmware is v%u.\n",
831 api_max, api_ver);
832 goto try_again;
833 }
834
835 if (api_ver < api_ok) {
836 if (api_ok != api_max)
Johannes Berg965974a62012-03-06 13:30:38 -0800837 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef92012-03-05 11:24:50 -0800838 "expected v%u through v%u, got v%u.\n",
839 api_ok, api_max, api_ver);
840 else
Johannes Berg965974a62012-03-06 13:30:38 -0800841 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef92012-03-05 11:24:50 -0800842 "expected v%u, got v%u.\n",
843 api_max, api_ver);
Johannes Berg965974a62012-03-06 13:30:38 -0800844 IWL_ERR(drv, "New firmware can be obtained from "
Johannes Berg15854ef92012-03-05 11:24:50 -0800845 "http://www.intellinuxwireless.org/.\n");
846 }
847 }
848
Johannes Berg965974a62012-03-06 13:30:38 -0800849 IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
Johannes Berg15854ef92012-03-05 11:24:50 -0800850
851 /*
David Spinadel4db2c9a2012-03-10 13:00:15 -0800852 * In mvm uCode there is no difference between data and instructions
853 * sections.
854 */
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200855 if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
Johannes Berg15854ef92012-03-05 11:24:50 -0800856 goto try_again;
Johannes Berg15854ef92012-03-05 11:24:50 -0800857
858 /* Allocate ucode buffers for card's bus-master loading ... */
859
860 /* Runtime instructions and 2 copies of data:
861 * 1) unmodified from disk
862 * 2) backup cache for save/restore during power-downs */
David Spinadel6dfa8d02012-03-10 13:00:14 -0800863 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
Johannes Berg75813bd2012-05-16 22:49:49 +0200864 if (iwl_alloc_ucode(drv, &pieces, i))
865 goto out_free_fw;
Johannes Berg15854ef92012-03-05 11:24:50 -0800866
867 /* Now that we can no longer fail, copy information */
868
869 /*
870 * The (size - 16) / 12 formula is based on the information recorded
871 * for each event, which is of mode 1 (including timestamp) for all
872 * new microcodes that include this information.
873 */
Johannes Berg0692fe42012-03-06 13:30:37 -0800874 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
Johannes Berg15854ef92012-03-05 11:24:50 -0800875 if (pieces.init_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800876 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
Johannes Berg15854ef92012-03-05 11:24:50 -0800877 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800878 fw->init_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200879 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800880 fw->init_errlog_ptr = pieces.init_errlog_ptr;
881 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
Johannes Berg15854ef92012-03-05 11:24:50 -0800882 if (pieces.inst_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800883 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
Johannes Berg15854ef92012-03-05 11:24:50 -0800884 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800885 fw->inst_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200886 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800887 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
Johannes Berg15854ef92012-03-05 11:24:50 -0800888
889 /*
890 * figure out the offset of chain noise reset and gain commands
891 * base on the size of standard phy calibration commands table size
892 */
893 if (fw->ucode_capa.standard_phy_calibration_size >
894 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
895 fw->ucode_capa.standard_phy_calibration_size =
896 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
897
898 /* We have our copies now, allow OS release its copies */
899 release_firmware(ucode_raw);
Johannes Berg965974a62012-03-06 13:30:38 -0800900 complete(&drv->request_firmware_complete);
Johannes Berg15854ef92012-03-05 11:24:50 -0800901
Don Frycc5f7e32012-05-16 22:54:27 +0200902 op = &iwlwifi_opmode_table[DVM_OP_MODE];
Johannes Berg15854ef92012-03-05 11:24:50 -0800903
Don Frycc5f7e32012-05-16 22:54:27 +0200904 /* add this device to the list of devices using this op_mode */
905 list_add_tail(&drv->list, &op->drv);
906
907 if (op->ops) {
908 const struct iwl_op_mode_ops *ops = op->ops;
909 drv->op_mode = ops->start(drv->trans, drv->cfg, &drv->fw);
910 } else {
911 request_module_nowait("%s", op->name);
912 }
Johannes Berg15854ef92012-03-05 11:24:50 -0800913
914 return;
915
916 try_again:
917 /* try next, if any */
918 release_firmware(ucode_raw);
Johannes Berg965974a62012-03-06 13:30:38 -0800919 if (iwl_request_firmware(drv, false))
Johannes Berg15854ef92012-03-05 11:24:50 -0800920 goto out_unbind;
921 return;
922
Johannes Berg75813bd2012-05-16 22:49:49 +0200923 out_free_fw:
Johannes Berg965974a62012-03-06 13:30:38 -0800924 IWL_ERR(drv, "failed to allocate pci memory\n");
925 iwl_dealloc_ucode(drv);
Johannes Berg15854ef92012-03-05 11:24:50 -0800926 release_firmware(ucode_raw);
927 out_unbind:
Johannes Berg965974a62012-03-06 13:30:38 -0800928 complete(&drv->request_firmware_complete);
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200929 device_release_driver(drv->trans->dev);
Johannes Berg15854ef92012-03-05 11:24:50 -0800930}
931
Emmanuel Grumbach87ce05a2012-03-26 09:03:18 -0700932struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700933 const struct iwl_cfg *cfg)
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200934{
Johannes Berg965974a62012-03-06 13:30:38 -0800935 struct iwl_drv *drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200936 int ret;
937
Johannes Berg965974a62012-03-06 13:30:38 -0800938 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +0300939 if (!drv)
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700940 return NULL;
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +0300941
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700942 drv->trans = trans;
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +0200943 drv->dev = trans->dev;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200944 drv->cfg = cfg;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200945
Johannes Berg965974a62012-03-06 13:30:38 -0800946 init_completion(&drv->request_firmware_complete);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200947
Johannes Berg965974a62012-03-06 13:30:38 -0800948 ret = iwl_request_firmware(drv, true);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200949
950 if (ret) {
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +0300951 IWL_ERR(trans, "Couldn't request the fw\n");
Johannes Berg965974a62012-03-06 13:30:38 -0800952 kfree(drv);
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700953 drv = NULL;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200954 }
955
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700956 return drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200957}
958
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700959void iwl_drv_stop(struct iwl_drv *drv)
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200960{
Johannes Berg965974a62012-03-06 13:30:38 -0800961 wait_for_completion(&drv->request_firmware_complete);
Johannes Berg2e7eb112012-03-05 11:24:51 -0800962
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200963 /* op_mode can be NULL if its start failed */
Johannes Berg965974a62012-03-06 13:30:38 -0800964 if (drv->op_mode)
965 iwl_op_mode_stop(drv->op_mode);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200966
Johannes Berg965974a62012-03-06 13:30:38 -0800967 iwl_dealloc_ucode(drv);
Johannes Berg7db5b982012-03-05 11:24:48 -0800968
Johannes Berg965974a62012-03-06 13:30:38 -0800969 kfree(drv);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200970}
Johannes Berg65de7e82012-04-17 07:36:30 -0700971
972
973/* shared module parameters */
974struct iwl_mod_params iwlwifi_mod_params = {
975 .amsdu_size_8K = 1,
976 .restart_fw = 1,
977 .plcp_check = true,
978 .bt_coex_active = true,
979 .power_level = IWL_POWER_INDEX_1,
980 .bt_ch_announce = true,
981 .auto_agg = true,
982 /* the rest are 0 by default */
983};
Don Frycc5f7e32012-05-16 22:54:27 +0200984EXPORT_SYMBOL_GPL(iwlwifi_mod_params);
985
986int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
987{
988 int i;
989 struct iwl_drv *drv;
990
991 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
992 if (strcmp(iwlwifi_opmode_table[i].name, name))
993 continue;
994 iwlwifi_opmode_table[i].ops = ops;
995 list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
996 drv->op_mode = ops->start(drv->trans, drv->cfg,
997 &drv->fw);
998 return 0;
999 }
1000 return -EIO;
1001}
1002EXPORT_SYMBOL_GPL(iwl_opmode_register);
1003
1004void iwl_opmode_deregister(const char *name)
1005{
1006 int i;
1007 struct iwl_drv *drv;
1008
1009 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1010 if (strcmp(iwlwifi_opmode_table[i].name, name))
1011 continue;
1012 iwlwifi_opmode_table[i].ops = NULL;
1013
1014 /* call the stop routine for all devices */
1015 list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list) {
1016 if (drv->op_mode) {
1017 iwl_op_mode_stop(drv->op_mode);
1018 drv->op_mode = NULL;
1019 }
1020 }
1021 return;
1022 }
1023}
1024EXPORT_SYMBOL_GPL(iwl_opmode_deregister);
1025
1026static int __init iwl_drv_init(void)
1027{
1028 int i;
1029
1030 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
1031 INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
1032
1033 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
1034 pr_info(DRV_COPYRIGHT "\n");
1035
1036 return iwl_pci_register_driver();
1037}
1038module_init(iwl_drv_init);
1039
1040static void __exit iwl_drv_exit(void)
1041{
1042 iwl_pci_unregister_driver();
1043}
1044module_exit(iwl_drv_exit);
Johannes Berg65de7e82012-04-17 07:36:30 -07001045
1046#ifdef CONFIG_IWLWIFI_DEBUG
1047module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
1048 S_IRUGO | S_IWUSR);
1049MODULE_PARM_DESC(debug, "debug output mask");
1050#endif
1051
1052module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
1053MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
1054module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
1055MODULE_PARM_DESC(11n_disable,
1056 "disable 11n functionality, bitmap: 1: full, 2: agg TX, 4: agg RX");
1057module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
1058 int, S_IRUGO);
1059MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
1060module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, int, S_IRUGO);
1061MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");
1062
1063module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
1064 int, S_IRUGO);
1065MODULE_PARM_DESC(antenna_coupling,
1066 "specify antenna coupling in dB (defualt: 0 dB)");
1067
1068module_param_named(bt_ch_inhibition, iwlwifi_mod_params.bt_ch_announce,
1069 bool, S_IRUGO);
1070MODULE_PARM_DESC(bt_ch_inhibition,
1071 "Enable BT channel inhibition (default: enable)");
1072
1073module_param_named(plcp_check, iwlwifi_mod_params.plcp_check, bool, S_IRUGO);
1074MODULE_PARM_DESC(plcp_check, "Check plcp health (default: 1 [enabled])");
1075
1076module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
1077MODULE_PARM_DESC(wd_disable,
1078 "Disable stuck queue watchdog timer 0=system default, "
1079 "1=disable, 2=enable (default: 0)");
1080
1081/*
1082 * set bt_coex_active to true, uCode will do kill/defer
1083 * every time the priority line is asserted (BT is sending signals on the
1084 * priority line in the PCIx).
1085 * set bt_coex_active to false, uCode will ignore the BT activity and
1086 * perform the normal operation
1087 *
1088 * User might experience transmit issue on some platform due to WiFi/BT
1089 * co-exist problem. The possible behaviors are:
1090 * Able to scan and finding all the available AP
1091 * Not able to associate with any AP
1092 * On those platforms, WiFi communication can be restored by set
1093 * "bt_coex_active" module parameter to "false"
1094 *
1095 * default: bt_coex_active = true (BT_COEX_ENABLE)
1096 */
1097module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
1098 bool, S_IRUGO);
1099MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
1100
1101module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
1102MODULE_PARM_DESC(led_mode, "0=system default, "
1103 "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
1104
1105module_param_named(power_save, iwlwifi_mod_params.power_save,
1106 bool, S_IRUGO);
1107MODULE_PARM_DESC(power_save,
1108 "enable WiFi power management (default: disable)");
1109
1110module_param_named(power_level, iwlwifi_mod_params.power_level,
1111 int, S_IRUGO);
1112MODULE_PARM_DESC(power_level,
1113 "default power save level (range from 1 - 5, default: 1)");
1114
1115module_param_named(auto_agg, iwlwifi_mod_params.auto_agg,
1116 bool, S_IRUGO);
1117MODULE_PARM_DESC(auto_agg,
1118 "enable agg w/o check traffic load (default: enable)");
Stanislaw Gruszkaf6b11542012-04-19 09:51:24 -07001119
1120module_param_named(5ghz_disable, iwlwifi_mod_params.disable_5ghz,
1121 bool, S_IRUGO);
1122MODULE_PARM_DESC(5ghz_disable, "disable 5GHz band (default: 0 [enabled])");