blob: 35a9d65664e935514f1d616a448defdd8d417186 [file] [log] [blame]
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2007 - 2012 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63#include <linux/completion.h>
Johannes Berg15854ef2012-03-05 11:24:50 -080064#include <linux/dma-mapping.h>
65#include <linux/firmware.h>
66#include <linux/module.h>
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020067
68#include "iwl-drv.h"
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 Berg965974a2012-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 Berg965974a2012-03-06 13:30:38 -0800107 * @fw: the iwl_fw structure
Johannes Berg965974a2012-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 Berg965974a2012-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 Berg965974a2012-03-06 13:30:38 -0800118 struct iwl_fw fw;
119
Johannes Berg965974a2012-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 Berg965974a2012-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 Berg965974a2012-03-06 13:30:38 -0800133
Johannes Bergff1ffb82012-06-06 09:42:57 +0200134/* Protects the table contents, i.e. the ops pointer & drv list */
135static struct mutex iwlwifi_opmode_table_mtx;
Don Frycc5f7e32012-05-16 22:54:27 +0200136static struct iwlwifi_opmode_table {
137 const char *name; /* name: iwldvm, iwlmvm, etc */
138 const struct iwl_op_mode_ops *ops; /* pointer to op_mode ops */
139 struct list_head drv; /* list of devices using this op_mode */
140} iwlwifi_opmode_table[] = { /* ops set when driver is initialized */
141 { .name = "iwldvm", .ops = NULL },
142 { .name = "iwlmvm", .ops = NULL },
143};
Johannes Berg965974a2012-03-06 13:30:38 -0800144
David Spinadel0cedacc2012-03-10 13:00:12 -0800145/*
146 * struct fw_sec: Just for the image parsing proccess.
147 * For the fw storage we are using struct fw_desc.
148 */
149struct fw_sec {
150 const void *data; /* the sec data */
151 size_t size; /* section size */
152 u32 offset; /* offset of writing in the device */
153};
154
Johannes Berg965974a2012-03-06 13:30:38 -0800155static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
Johannes Berg15854ef2012-03-05 11:24:50 -0800156{
157 if (desc->v_addr)
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200158 dma_free_coherent(drv->trans->dev, desc->len,
Johannes Berg15854ef2012-03-05 11:24:50 -0800159 desc->v_addr, desc->p_addr);
160 desc->v_addr = NULL;
161 desc->len = 0;
162}
163
Johannes Berg965974a2012-03-06 13:30:38 -0800164static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
Johannes Berg15854ef2012-03-05 11:24:50 -0800165{
David Spinadel6dfa8d02012-03-10 13:00:14 -0800166 int i;
167 for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
168 iwl_free_fw_desc(drv, &img->sec[i]);
Johannes Berg15854ef2012-03-05 11:24:50 -0800169}
170
Johannes Berg965974a2012-03-06 13:30:38 -0800171static void iwl_dealloc_ucode(struct iwl_drv *drv)
Johannes Berg15854ef2012-03-05 11:24:50 -0800172{
David Spinadel6dfa8d02012-03-10 13:00:14 -0800173 int i;
174 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
175 iwl_free_fw_img(drv, drv->fw.img + i);
Johannes Berg15854ef2012-03-05 11:24:50 -0800176}
177
Johannes Berg965974a2012-03-06 13:30:38 -0800178static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
David Spinadel0cedacc2012-03-10 13:00:12 -0800179 struct fw_sec *sec)
Johannes Berg15854ef2012-03-05 11:24:50 -0800180{
David Spinadel0cedacc2012-03-10 13:00:12 -0800181 if (!sec || !sec->size) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800182 desc->v_addr = NULL;
183 return -EINVAL;
184 }
185
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200186 desc->v_addr = dma_alloc_coherent(drv->trans->dev, sec->size,
Johannes Berg15854ef2012-03-05 11:24:50 -0800187 &desc->p_addr, GFP_KERNEL);
188 if (!desc->v_addr)
189 return -ENOMEM;
190
David Spinadel0cedacc2012-03-10 13:00:12 -0800191 desc->len = sec->size;
192 desc->offset = sec->offset;
193 memcpy(desc->v_addr, sec->data, sec->size);
Johannes Berg15854ef2012-03-05 11:24:50 -0800194 return 0;
195}
196
Emmanuel Grumbach1c8e11e2012-07-16 12:31:28 +0300197static void iwl_req_fw_callback(const struct firmware *ucode_raw,
198 void *context);
Johannes Berg15854ef2012-03-05 11:24:50 -0800199
200#define UCODE_EXPERIMENTAL_INDEX 100
201#define UCODE_EXPERIMENTAL_TAG "exp"
202
Johannes Berg965974a2012-03-06 13:30:38 -0800203static int iwl_request_firmware(struct iwl_drv *drv, bool first)
Johannes Berg15854ef2012-03-05 11:24:50 -0800204{
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200205 const char *name_pre = drv->cfg->fw_name_pre;
Johannes Berg15854ef2012-03-05 11:24:50 -0800206 char tag[8];
207
208 if (first) {
209#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
Johannes Berg965974a2012-03-06 13:30:38 -0800210 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
Johannes Berg15854ef2012-03-05 11:24:50 -0800211 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
Johannes Berg965974a2012-03-06 13:30:38 -0800212 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800213#endif
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200214 drv->fw_index = drv->cfg->ucode_api_max;
Johannes Berg965974a2012-03-06 13:30:38 -0800215 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800216 } else {
Johannes Berg965974a2012-03-06 13:30:38 -0800217 drv->fw_index--;
218 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800219 }
220
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200221 if (drv->fw_index < drv->cfg->ucode_api_min) {
Johannes Berg965974a2012-03-06 13:30:38 -0800222 IWL_ERR(drv, "no suitable firmware found!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800223 return -ENOENT;
224 }
225
Johannes Berg965974a2012-03-06 13:30:38 -0800226 sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Johannes Berg15854ef2012-03-05 11:24:50 -0800227
Johannes Berg965974a2012-03-06 13:30:38 -0800228 IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
229 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800230 ? "EXPERIMENTAL " : "",
Johannes Berg965974a2012-03-06 13:30:38 -0800231 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800232
Johannes Berg965974a2012-03-06 13:30:38 -0800233 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200234 drv->trans->dev,
Emmanuel Grumbach1c8e11e2012-07-16 12:31:28 +0300235 GFP_KERNEL, drv, iwl_req_fw_callback);
Johannes Berg15854ef2012-03-05 11:24:50 -0800236}
237
David Spinadel0cedacc2012-03-10 13:00:12 -0800238struct fw_img_parsing {
David Spinadel6dfa8d02012-03-10 13:00:14 -0800239 struct fw_sec sec[IWL_UCODE_SECTION_MAX];
David Spinadel0cedacc2012-03-10 13:00:12 -0800240 int sec_counter;
241};
242
David Spinadeled8c8362012-03-10 13:00:13 -0800243/*
244 * struct fw_sec_parsing: to extract fw section and it's offset from tlv
245 */
246struct fw_sec_parsing {
247 __le32 offset;
248 const u8 data[];
249} __packed;
250
251/**
252 * struct iwl_tlv_calib_data - parse the default calib data from TLV
253 *
254 * @ucode_type: the uCode to which the following default calib relates.
255 * @calib: default calibrations.
256 */
257struct iwl_tlv_calib_data {
258 __le32 ucode_type;
259 __le64 calib;
260} __packed;
261
David Spinadel0cedacc2012-03-10 13:00:12 -0800262struct iwl_firmware_pieces {
263 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
Johannes Berg15854ef2012-03-05 11:24:50 -0800264
265 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
266 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
267};
268
David Spinadel0cedacc2012-03-10 13:00:12 -0800269/*
270 * These functions are just to extract uCode section data from the pieces
271 * structure.
272 */
273static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
274 enum iwl_ucode_type type,
275 int sec)
276{
277 return &pieces->img[type].sec[sec];
278}
279
280static void set_sec_data(struct iwl_firmware_pieces *pieces,
281 enum iwl_ucode_type type,
282 int sec,
283 const void *data)
284{
285 pieces->img[type].sec[sec].data = data;
286}
287
288static void set_sec_size(struct iwl_firmware_pieces *pieces,
289 enum iwl_ucode_type type,
290 int sec,
291 size_t size)
292{
293 pieces->img[type].sec[sec].size = size;
294}
295
296static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
297 enum iwl_ucode_type type,
298 int sec)
299{
300 return pieces->img[type].sec[sec].size;
301}
302
303static void set_sec_offset(struct iwl_firmware_pieces *pieces,
304 enum iwl_ucode_type type,
305 int sec,
306 u32 offset)
307{
308 pieces->img[type].sec[sec].offset = offset;
309}
310
David Spinadeled8c8362012-03-10 13:00:13 -0800311/*
312 * Gets uCode section from tlv.
313 */
314static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
315 const void *data, enum iwl_ucode_type type,
316 int size)
317{
318 struct fw_img_parsing *img;
319 struct fw_sec *sec;
320 struct fw_sec_parsing *sec_parse;
321
322 if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
323 return -1;
324
325 sec_parse = (struct fw_sec_parsing *)data;
326
327 img = &pieces->img[type];
328 sec = &img->sec[img->sec_counter];
329
330 sec->offset = le32_to_cpu(sec_parse->offset);
331 sec->data = sec_parse->data;
David Spinadel1176f432012-03-13 14:32:48 +0200332 sec->size = size - sizeof(sec_parse->offset);
David Spinadeled8c8362012-03-10 13:00:13 -0800333
334 ++img->sec_counter;
335
336 return 0;
337}
338
339static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
340{
341 struct iwl_tlv_calib_data *def_calib =
342 (struct iwl_tlv_calib_data *)data;
343 u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
344 if (ucode_type >= IWL_UCODE_TYPE_MAX) {
345 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
346 ucode_type);
347 return -EINVAL;
348 }
349 drv->fw.default_calib[ucode_type] = le64_to_cpu(def_calib->calib);
350 return 0;
351}
352
Johannes Berg965974a2012-03-06 13:30:38 -0800353static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
David Spinadel0cedacc2012-03-10 13:00:12 -0800354 const struct firmware *ucode_raw,
355 struct iwl_firmware_pieces *pieces)
Johannes Berg15854ef2012-03-05 11:24:50 -0800356{
357 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
358 u32 api_ver, hdr_size, build;
359 char buildstr[25];
360 const u8 *src;
361
Johannes Berg965974a2012-03-06 13:30:38 -0800362 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
363 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800364
365 switch (api_ver) {
366 default:
367 hdr_size = 28;
368 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800369 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800370 return -EINVAL;
371 }
372 build = le32_to_cpu(ucode->u.v2.build);
David Spinadel0cedacc2012-03-10 13:00:12 -0800373 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
374 le32_to_cpu(ucode->u.v2.inst_size));
375 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
376 le32_to_cpu(ucode->u.v2.data_size));
377 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
378 le32_to_cpu(ucode->u.v2.init_size));
379 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
380 le32_to_cpu(ucode->u.v2.init_data_size));
Johannes Berg15854ef2012-03-05 11:24:50 -0800381 src = ucode->u.v2.data;
382 break;
383 case 0:
384 case 1:
385 case 2:
386 hdr_size = 24;
387 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800388 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800389 return -EINVAL;
390 }
391 build = 0;
David Spinadel0cedacc2012-03-10 13:00:12 -0800392 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
393 le32_to_cpu(ucode->u.v1.inst_size));
394 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
395 le32_to_cpu(ucode->u.v1.data_size));
396 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
397 le32_to_cpu(ucode->u.v1.init_size));
398 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
399 le32_to_cpu(ucode->u.v1.init_data_size));
Johannes Berg15854ef2012-03-05 11:24:50 -0800400 src = ucode->u.v1.data;
401 break;
402 }
403
404 if (build)
405 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800406 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800407 ? " (EXP)" : "");
408 else
409 buildstr[0] = '\0';
410
Johannes Berg965974a2012-03-06 13:30:38 -0800411 snprintf(drv->fw.fw_version,
412 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800413 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800414 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
415 IWL_UCODE_MINOR(drv->fw.ucode_ver),
416 IWL_UCODE_API(drv->fw.ucode_ver),
417 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef2012-03-05 11:24:50 -0800418 buildstr);
419
420 /* Verify size of file vs. image size info in file's header */
David Spinadel0cedacc2012-03-10 13:00:12 -0800421
422 if (ucode_raw->size != hdr_size +
423 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
424 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
425 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
426 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800427
Johannes Berg965974a2012-03-06 13:30:38 -0800428 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800429 "uCode file size %d does not match expected size\n",
430 (int)ucode_raw->size);
431 return -EINVAL;
432 }
433
Johannes Berg15854ef2012-03-05 11:24:50 -0800434
David Spinadel0cedacc2012-03-10 13:00:12 -0800435 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
436 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
437 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
438 IWLAGN_RTC_INST_LOWER_BOUND);
439 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
440 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
441 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
442 IWLAGN_RTC_DATA_LOWER_BOUND);
443 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
444 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
445 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
446 IWLAGN_RTC_INST_LOWER_BOUND);
447 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
448 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
449 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
450 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800451 return 0;
452}
453
Johannes Berg965974a2012-03-06 13:30:38 -0800454static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800455 const struct firmware *ucode_raw,
David Spinadel0cedacc2012-03-10 13:00:12 -0800456 struct iwl_firmware_pieces *pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -0800457 struct iwl_ucode_capabilities *capa)
458{
459 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
460 struct iwl_ucode_tlv *tlv;
461 size_t len = ucode_raw->size;
462 const u8 *data;
Johannes Berg15854ef2012-03-05 11:24:50 -0800463 u32 tlv_len;
464 enum iwl_ucode_tlv_type tlv_type;
465 const u8 *tlv_data;
466 char buildstr[25];
467 u32 build;
468
469 if (len < sizeof(*ucode)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800470 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800471 return -EINVAL;
472 }
473
474 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800475 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800476 le32_to_cpu(ucode->magic));
477 return -EINVAL;
478 }
479
Johannes Berg965974a2012-03-06 13:30:38 -0800480 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800481 build = le32_to_cpu(ucode->build);
482
483 if (build)
484 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800485 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800486 ? " (EXP)" : "");
487 else
488 buildstr[0] = '\0';
489
Johannes Berg965974a2012-03-06 13:30:38 -0800490 snprintf(drv->fw.fw_version,
491 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800492 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800493 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
494 IWL_UCODE_MINOR(drv->fw.ucode_ver),
495 IWL_UCODE_API(drv->fw.ucode_ver),
496 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef2012-03-05 11:24:50 -0800497 buildstr);
498
499 data = ucode->data;
500
501 len -= sizeof(*ucode);
502
503 while (len >= sizeof(*tlv)) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800504 len -= sizeof(*tlv);
505 tlv = (void *)data;
506
507 tlv_len = le32_to_cpu(tlv->length);
Johannes Berg0479c192012-03-09 09:16:35 +0100508 tlv_type = le32_to_cpu(tlv->type);
Johannes Berg15854ef2012-03-05 11:24:50 -0800509 tlv_data = tlv->data;
510
511 if (len < tlv_len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800512 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800513 len, tlv_len);
514 return -EINVAL;
515 }
516 len -= ALIGN(tlv_len, 4);
517 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
518
Johannes Berg15854ef2012-03-05 11:24:50 -0800519 switch (tlv_type) {
520 case IWL_UCODE_TLV_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800521 set_sec_data(pieces, IWL_UCODE_REGULAR,
522 IWL_UCODE_SECTION_INST, tlv_data);
523 set_sec_size(pieces, IWL_UCODE_REGULAR,
524 IWL_UCODE_SECTION_INST, tlv_len);
525 set_sec_offset(pieces, IWL_UCODE_REGULAR,
526 IWL_UCODE_SECTION_INST,
527 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800528 break;
529 case IWL_UCODE_TLV_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800530 set_sec_data(pieces, IWL_UCODE_REGULAR,
531 IWL_UCODE_SECTION_DATA, tlv_data);
532 set_sec_size(pieces, IWL_UCODE_REGULAR,
533 IWL_UCODE_SECTION_DATA, tlv_len);
534 set_sec_offset(pieces, IWL_UCODE_REGULAR,
535 IWL_UCODE_SECTION_DATA,
536 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800537 break;
538 case IWL_UCODE_TLV_INIT:
David Spinadel0cedacc2012-03-10 13:00:12 -0800539 set_sec_data(pieces, IWL_UCODE_INIT,
540 IWL_UCODE_SECTION_INST, tlv_data);
541 set_sec_size(pieces, IWL_UCODE_INIT,
542 IWL_UCODE_SECTION_INST, tlv_len);
543 set_sec_offset(pieces, IWL_UCODE_INIT,
544 IWL_UCODE_SECTION_INST,
545 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800546 break;
547 case IWL_UCODE_TLV_INIT_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800548 set_sec_data(pieces, IWL_UCODE_INIT,
549 IWL_UCODE_SECTION_DATA, tlv_data);
550 set_sec_size(pieces, IWL_UCODE_INIT,
551 IWL_UCODE_SECTION_DATA, tlv_len);
552 set_sec_offset(pieces, IWL_UCODE_INIT,
553 IWL_UCODE_SECTION_DATA,
554 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800555 break;
556 case IWL_UCODE_TLV_BOOT:
Johannes Berg965974a2012-03-06 13:30:38 -0800557 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800558 break;
559 case IWL_UCODE_TLV_PROBE_MAX_LEN:
560 if (tlv_len != sizeof(u32))
561 goto invalid_tlv_len;
562 capa->max_probe_length =
563 le32_to_cpup((__le32 *)tlv_data);
564 break;
565 case IWL_UCODE_TLV_PAN:
566 if (tlv_len)
567 goto invalid_tlv_len;
568 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
569 break;
570 case IWL_UCODE_TLV_FLAGS:
571 /* must be at least one u32 */
572 if (tlv_len < sizeof(u32))
573 goto invalid_tlv_len;
574 /* and a proper number of u32s */
575 if (tlv_len % sizeof(u32))
576 goto invalid_tlv_len;
577 /*
578 * This driver only reads the first u32 as
579 * right now no more features are defined,
580 * if that changes then either the driver
581 * will not work with the new firmware, or
582 * it'll not take advantage of new features.
583 */
584 capa->flags = le32_to_cpup((__le32 *)tlv_data);
585 break;
586 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
587 if (tlv_len != sizeof(u32))
588 goto invalid_tlv_len;
589 pieces->init_evtlog_ptr =
590 le32_to_cpup((__le32 *)tlv_data);
591 break;
592 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
593 if (tlv_len != sizeof(u32))
594 goto invalid_tlv_len;
595 pieces->init_evtlog_size =
596 le32_to_cpup((__le32 *)tlv_data);
597 break;
598 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
599 if (tlv_len != sizeof(u32))
600 goto invalid_tlv_len;
601 pieces->init_errlog_ptr =
602 le32_to_cpup((__le32 *)tlv_data);
603 break;
604 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
605 if (tlv_len != sizeof(u32))
606 goto invalid_tlv_len;
607 pieces->inst_evtlog_ptr =
608 le32_to_cpup((__le32 *)tlv_data);
609 break;
610 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
611 if (tlv_len != sizeof(u32))
612 goto invalid_tlv_len;
613 pieces->inst_evtlog_size =
614 le32_to_cpup((__le32 *)tlv_data);
615 break;
616 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
617 if (tlv_len != sizeof(u32))
618 goto invalid_tlv_len;
619 pieces->inst_errlog_ptr =
620 le32_to_cpup((__le32 *)tlv_data);
621 break;
622 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
623 if (tlv_len)
624 goto invalid_tlv_len;
Johannes Berg965974a2012-03-06 13:30:38 -0800625 drv->fw.enhance_sensitivity_table = true;
Johannes Berg15854ef2012-03-05 11:24:50 -0800626 break;
627 case IWL_UCODE_TLV_WOWLAN_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800628 set_sec_data(pieces, IWL_UCODE_WOWLAN,
629 IWL_UCODE_SECTION_INST, tlv_data);
630 set_sec_size(pieces, IWL_UCODE_WOWLAN,
631 IWL_UCODE_SECTION_INST, tlv_len);
632 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
633 IWL_UCODE_SECTION_INST,
634 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800635 break;
636 case IWL_UCODE_TLV_WOWLAN_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800637 set_sec_data(pieces, IWL_UCODE_WOWLAN,
638 IWL_UCODE_SECTION_DATA, tlv_data);
639 set_sec_size(pieces, IWL_UCODE_WOWLAN,
640 IWL_UCODE_SECTION_DATA, tlv_len);
641 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
642 IWL_UCODE_SECTION_DATA,
643 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800644 break;
645 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
646 if (tlv_len != sizeof(u32))
647 goto invalid_tlv_len;
648 capa->standard_phy_calibration_size =
649 le32_to_cpup((__le32 *)tlv_data);
650 break;
David Spinadeled8c8362012-03-10 13:00:13 -0800651 case IWL_UCODE_TLV_SEC_RT:
652 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
653 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800654 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800655 break;
656 case IWL_UCODE_TLV_SEC_INIT:
657 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
658 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800659 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800660 break;
661 case IWL_UCODE_TLV_SEC_WOWLAN:
662 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
663 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800664 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800665 break;
666 case IWL_UCODE_TLV_DEF_CALIB:
667 if (tlv_len != sizeof(struct iwl_tlv_calib_data))
668 goto invalid_tlv_len;
669 if (iwl_set_default_calib(drv, tlv_data))
670 goto tlv_error;
671 break;
672 case IWL_UCODE_TLV_PHY_SKU:
673 if (tlv_len != sizeof(u32))
674 goto invalid_tlv_len;
675 drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
676 break;
Johannes Berg15854ef2012-03-05 11:24:50 -0800677 default:
Johannes Berg965974a2012-03-06 13:30:38 -0800678 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
Johannes Berg15854ef2012-03-05 11:24:50 -0800679 break;
680 }
681 }
682
683 if (len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800684 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
685 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800686 return -EINVAL;
687 }
688
689 return 0;
690
691 invalid_tlv_len:
Johannes Berg965974a2012-03-06 13:30:38 -0800692 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
David Spinadeled8c8362012-03-10 13:00:13 -0800693 tlv_error:
Johannes Berg965974a2012-03-06 13:30:38 -0800694 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800695
696 return -EINVAL;
697}
698
Johannes Berg75813bd2012-05-16 22:49:49 +0200699static int iwl_alloc_ucode(struct iwl_drv *drv,
700 struct iwl_firmware_pieces *pieces,
701 enum iwl_ucode_type type)
David Spinadel6dfa8d02012-03-10 13:00:14 -0800702{
703 int i;
704 for (i = 0;
705 i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
706 i++)
707 if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
Johannes Berg75813bd2012-05-16 22:49:49 +0200708 get_sec(pieces, type, i)))
709 return -ENOMEM;
David Spinadel6dfa8d02012-03-10 13:00:14 -0800710 return 0;
711}
712
713static int validate_sec_sizes(struct iwl_drv *drv,
714 struct iwl_firmware_pieces *pieces,
715 const struct iwl_cfg *cfg)
716{
717 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
718 get_sec_size(pieces, IWL_UCODE_REGULAR,
719 IWL_UCODE_SECTION_INST));
720 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
721 get_sec_size(pieces, IWL_UCODE_REGULAR,
722 IWL_UCODE_SECTION_DATA));
723 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
724 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
725 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
726 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
727
728 /* Verify that uCode images will fit in card's SRAM. */
729 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
730 cfg->max_inst_size) {
731 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
732 get_sec_size(pieces, IWL_UCODE_REGULAR,
733 IWL_UCODE_SECTION_INST));
734 return -1;
735 }
736
737 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
738 cfg->max_data_size) {
739 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
740 get_sec_size(pieces, IWL_UCODE_REGULAR,
741 IWL_UCODE_SECTION_DATA));
742 return -1;
743 }
744
745 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
746 cfg->max_inst_size) {
747 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
748 get_sec_size(pieces, IWL_UCODE_INIT,
749 IWL_UCODE_SECTION_INST));
750 return -1;
751 }
752
753 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
754 cfg->max_data_size) {
755 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
756 get_sec_size(pieces, IWL_UCODE_REGULAR,
757 IWL_UCODE_SECTION_DATA));
758 return -1;
759 }
760 return 0;
761}
762
Johannes Berg15854ef2012-03-05 11:24:50 -0800763/**
Emmanuel Grumbach1c8e11e2012-07-16 12:31:28 +0300764 * iwl_req_fw_callback - callback when firmware was loaded
Johannes Berg15854ef2012-03-05 11:24:50 -0800765 *
766 * If loaded successfully, copies the firmware into buffers
767 * for the card to fetch (via DMA).
768 */
Emmanuel Grumbach1c8e11e2012-07-16 12:31:28 +0300769static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context)
Johannes Berg15854ef2012-03-05 11:24:50 -0800770{
Johannes Berg965974a2012-03-06 13:30:38 -0800771 struct iwl_drv *drv = context;
Johannes Berg965974a2012-03-06 13:30:38 -0800772 struct iwl_fw *fw = &drv->fw;
Johannes Berg15854ef2012-03-05 11:24:50 -0800773 struct iwl_ucode_header *ucode;
Don Frycc5f7e32012-05-16 22:54:27 +0200774 struct iwlwifi_opmode_table *op;
Johannes Berg15854ef2012-03-05 11:24:50 -0800775 int err;
David Spinadel0cedacc2012-03-10 13:00:12 -0800776 struct iwl_firmware_pieces pieces;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200777 const unsigned int api_max = drv->cfg->ucode_api_max;
778 unsigned int api_ok = drv->cfg->ucode_api_ok;
779 const unsigned int api_min = drv->cfg->ucode_api_min;
Johannes Berg15854ef2012-03-05 11:24:50 -0800780 u32 api_ver;
David Spinadel6dfa8d02012-03-10 13:00:14 -0800781 int i;
Johannes Bergd4b10482012-06-12 19:50:43 +0200782 bool load_module = false;
Johannes Berg15854ef2012-03-05 11:24:50 -0800783
784 fw->ucode_capa.max_probe_length = 200;
785 fw->ucode_capa.standard_phy_calibration_size =
786 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
787
788 if (!api_ok)
789 api_ok = api_max;
790
791 memset(&pieces, 0, sizeof(pieces));
792
793 if (!ucode_raw) {
Johannes Berg965974a2012-03-06 13:30:38 -0800794 if (drv->fw_index <= api_ok)
795 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800796 "request for firmware file '%s' failed.\n",
Johannes Berg965974a2012-03-06 13:30:38 -0800797 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800798 goto try_again;
799 }
800
Johannes Berg965974a2012-03-06 13:30:38 -0800801 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
802 drv->firmware_name, ucode_raw->size);
Johannes Berg15854ef2012-03-05 11:24:50 -0800803
804 /* Make sure that we got at least the API version number */
805 if (ucode_raw->size < 4) {
Johannes Berg965974a2012-03-06 13:30:38 -0800806 IWL_ERR(drv, "File size way too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800807 goto try_again;
808 }
809
810 /* Data from ucode file: header followed by uCode images */
811 ucode = (struct iwl_ucode_header *)ucode_raw->data;
812
813 if (ucode->ver)
Johannes Berg965974a2012-03-06 13:30:38 -0800814 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
Johannes Berg15854ef2012-03-05 11:24:50 -0800815 else
Johannes Berg965974a2012-03-06 13:30:38 -0800816 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -0800817 &fw->ucode_capa);
818
819 if (err)
820 goto try_again;
821
Johannes Berg965974a2012-03-06 13:30:38 -0800822 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800823
824 /*
825 * api_ver should match the api version forming part of the
826 * firmware filename ... but we don't check for that and only rely
827 * on the API version read from firmware header from here on forward
828 */
829 /* no api version check required for experimental uCode */
Johannes Berg965974a2012-03-06 13:30:38 -0800830 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800831 if (api_ver < api_min || api_ver > api_max) {
Johannes Berg965974a2012-03-06 13:30:38 -0800832 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800833 "Driver unable to support your firmware API. "
834 "Driver supports v%u, firmware is v%u.\n",
835 api_max, api_ver);
836 goto try_again;
837 }
838
839 if (api_ver < api_ok) {
840 if (api_ok != api_max)
Johannes Berg965974a2012-03-06 13:30:38 -0800841 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -0800842 "expected v%u through v%u, got v%u.\n",
843 api_ok, api_max, api_ver);
844 else
Johannes Berg965974a2012-03-06 13:30:38 -0800845 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -0800846 "expected v%u, got v%u.\n",
847 api_max, api_ver);
Johannes Berg965974a2012-03-06 13:30:38 -0800848 IWL_ERR(drv, "New firmware can be obtained from "
Johannes Berg15854ef2012-03-05 11:24:50 -0800849 "http://www.intellinuxwireless.org/.\n");
850 }
851 }
852
Johannes Berg965974a2012-03-06 13:30:38 -0800853 IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
Johannes Berg15854ef2012-03-05 11:24:50 -0800854
855 /*
David Spinadel4db2c9a2012-03-10 13:00:15 -0800856 * In mvm uCode there is no difference between data and instructions
857 * sections.
858 */
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200859 if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
Johannes Berg15854ef2012-03-05 11:24:50 -0800860 goto try_again;
Johannes Berg15854ef2012-03-05 11:24:50 -0800861
862 /* Allocate ucode buffers for card's bus-master loading ... */
863
864 /* Runtime instructions and 2 copies of data:
865 * 1) unmodified from disk
866 * 2) backup cache for save/restore during power-downs */
David Spinadel6dfa8d02012-03-10 13:00:14 -0800867 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
Johannes Berg75813bd2012-05-16 22:49:49 +0200868 if (iwl_alloc_ucode(drv, &pieces, i))
869 goto out_free_fw;
Johannes Berg15854ef2012-03-05 11:24:50 -0800870
871 /* Now that we can no longer fail, copy information */
872
873 /*
874 * The (size - 16) / 12 formula is based on the information recorded
875 * for each event, which is of mode 1 (including timestamp) for all
876 * new microcodes that include this information.
877 */
Johannes Berg0692fe42012-03-06 13:30:37 -0800878 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800879 if (pieces.init_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800880 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -0800881 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800882 fw->init_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200883 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800884 fw->init_errlog_ptr = pieces.init_errlog_ptr;
885 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800886 if (pieces.inst_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800887 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -0800888 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800889 fw->inst_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200890 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800891 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800892
893 /*
894 * figure out the offset of chain noise reset and gain commands
895 * base on the size of standard phy calibration commands table size
896 */
897 if (fw->ucode_capa.standard_phy_calibration_size >
898 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
899 fw->ucode_capa.standard_phy_calibration_size =
900 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
901
902 /* We have our copies now, allow OS release its copies */
903 release_firmware(ucode_raw);
Johannes Berg15854ef2012-03-05 11:24:50 -0800904
Johannes Bergff1ffb82012-06-06 09:42:57 +0200905 mutex_lock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +0200906 op = &iwlwifi_opmode_table[DVM_OP_MODE];
Johannes Berg15854ef2012-03-05 11:24:50 -0800907
Don Frycc5f7e32012-05-16 22:54:27 +0200908 /* add this device to the list of devices using this op_mode */
909 list_add_tail(&drv->list, &op->drv);
910
911 if (op->ops) {
912 const struct iwl_op_mode_ops *ops = op->ops;
913 drv->op_mode = ops->start(drv->trans, drv->cfg, &drv->fw);
John W. Linville7c9c46c2012-06-06 14:40:06 -0400914
Dan Carpenterdaf67ce2012-06-14 21:35:26 +0300915 if (!drv->op_mode) {
916 mutex_unlock(&iwlwifi_opmode_table_mtx);
John W. Linville7c9c46c2012-06-06 14:40:06 -0400917 goto out_unbind;
Dan Carpenterdaf67ce2012-06-14 21:35:26 +0300918 }
Don Frycc5f7e32012-05-16 22:54:27 +0200919 } else {
Johannes Bergd4b10482012-06-12 19:50:43 +0200920 load_module = true;
Don Frycc5f7e32012-05-16 22:54:27 +0200921 }
Johannes Bergff1ffb82012-06-06 09:42:57 +0200922 mutex_unlock(&iwlwifi_opmode_table_mtx);
Johannes Berg15854ef2012-03-05 11:24:50 -0800923
Johannes Bergf69a23b2012-06-05 19:56:06 +0200924 /*
925 * Complete the firmware request last so that
926 * a driver unbind (stop) doesn't run while we
927 * are doing the start() above.
928 */
929 complete(&drv->request_firmware_complete);
Johannes Bergd4b10482012-06-12 19:50:43 +0200930
931 /*
932 * Load the module last so we don't block anything
933 * else from proceeding if the module fails to load
934 * or hangs loading.
935 */
936 if (load_module)
937 request_module("%s", op->name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800938 return;
939
940 try_again:
941 /* try next, if any */
942 release_firmware(ucode_raw);
Johannes Berg965974a2012-03-06 13:30:38 -0800943 if (iwl_request_firmware(drv, false))
Johannes Berg15854ef2012-03-05 11:24:50 -0800944 goto out_unbind;
945 return;
946
Johannes Berg75813bd2012-05-16 22:49:49 +0200947 out_free_fw:
Johannes Berg965974a2012-03-06 13:30:38 -0800948 IWL_ERR(drv, "failed to allocate pci memory\n");
949 iwl_dealloc_ucode(drv);
Johannes Berg15854ef2012-03-05 11:24:50 -0800950 release_firmware(ucode_raw);
951 out_unbind:
Johannes Berg965974a2012-03-06 13:30:38 -0800952 complete(&drv->request_firmware_complete);
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200953 device_release_driver(drv->trans->dev);
Johannes Berg15854ef2012-03-05 11:24:50 -0800954}
955
Emmanuel Grumbach87ce05a2012-03-26 09:03:18 -0700956struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700957 const struct iwl_cfg *cfg)
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200958{
Johannes Berg965974a2012-03-06 13:30:38 -0800959 struct iwl_drv *drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200960 int ret;
961
Johannes Berg965974a2012-03-06 13:30:38 -0800962 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +0300963 if (!drv)
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700964 return NULL;
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +0300965
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700966 drv->trans = trans;
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +0200967 drv->dev = trans->dev;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200968 drv->cfg = cfg;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200969
Johannes Berg965974a2012-03-06 13:30:38 -0800970 init_completion(&drv->request_firmware_complete);
Johannes Bergff1ffb82012-06-06 09:42:57 +0200971 INIT_LIST_HEAD(&drv->list);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200972
Johannes Berg965974a2012-03-06 13:30:38 -0800973 ret = iwl_request_firmware(drv, true);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200974
975 if (ret) {
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +0300976 IWL_ERR(trans, "Couldn't request the fw\n");
Johannes Berg965974a2012-03-06 13:30:38 -0800977 kfree(drv);
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700978 drv = NULL;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200979 }
980
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700981 return drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200982}
983
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700984void iwl_drv_stop(struct iwl_drv *drv)
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200985{
Johannes Berg965974a2012-03-06 13:30:38 -0800986 wait_for_completion(&drv->request_firmware_complete);
Johannes Berg2e7eb112012-03-05 11:24:51 -0800987
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200988 /* op_mode can be NULL if its start failed */
Johannes Berg965974a2012-03-06 13:30:38 -0800989 if (drv->op_mode)
990 iwl_op_mode_stop(drv->op_mode);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200991
Johannes Berg965974a2012-03-06 13:30:38 -0800992 iwl_dealloc_ucode(drv);
Johannes Berg7db5b982012-03-05 11:24:48 -0800993
Johannes Bergff1ffb82012-06-06 09:42:57 +0200994 mutex_lock(&iwlwifi_opmode_table_mtx);
995 /*
996 * List is empty (this item wasn't added)
997 * when firmware loading failed -- in that
998 * case we can't remove it from any list.
999 */
1000 if (!list_empty(&drv->list))
1001 list_del(&drv->list);
1002 mutex_unlock(&iwlwifi_opmode_table_mtx);
1003
Johannes Berg965974a2012-03-06 13:30:38 -08001004 kfree(drv);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +02001005}
Johannes Berg65de7e82012-04-17 07:36:30 -07001006
1007
1008/* shared module parameters */
1009struct iwl_mod_params iwlwifi_mod_params = {
1010 .amsdu_size_8K = 1,
1011 .restart_fw = 1,
1012 .plcp_check = true,
1013 .bt_coex_active = true,
1014 .power_level = IWL_POWER_INDEX_1,
1015 .bt_ch_announce = true,
1016 .auto_agg = true,
Emmanuel Grumbach020c8762012-06-24 15:51:59 +03001017 .wd_disable = true,
Johannes Berg65de7e82012-04-17 07:36:30 -07001018 /* the rest are 0 by default */
1019};
Don Frycc5f7e32012-05-16 22:54:27 +02001020EXPORT_SYMBOL_GPL(iwlwifi_mod_params);
1021
1022int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
1023{
1024 int i;
1025 struct iwl_drv *drv;
1026
Johannes Bergff1ffb82012-06-06 09:42:57 +02001027 mutex_lock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001028 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1029 if (strcmp(iwlwifi_opmode_table[i].name, name))
1030 continue;
1031 iwlwifi_opmode_table[i].ops = ops;
1032 list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
1033 drv->op_mode = ops->start(drv->trans, drv->cfg,
1034 &drv->fw);
Johannes Bergff1ffb82012-06-06 09:42:57 +02001035 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001036 return 0;
1037 }
Johannes Bergff1ffb82012-06-06 09:42:57 +02001038 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001039 return -EIO;
1040}
1041EXPORT_SYMBOL_GPL(iwl_opmode_register);
1042
1043void iwl_opmode_deregister(const char *name)
1044{
1045 int i;
1046 struct iwl_drv *drv;
1047
Johannes Bergff1ffb82012-06-06 09:42:57 +02001048 mutex_lock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001049 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1050 if (strcmp(iwlwifi_opmode_table[i].name, name))
1051 continue;
1052 iwlwifi_opmode_table[i].ops = NULL;
1053
1054 /* call the stop routine for all devices */
1055 list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list) {
1056 if (drv->op_mode) {
1057 iwl_op_mode_stop(drv->op_mode);
1058 drv->op_mode = NULL;
1059 }
1060 }
Johannes Bergff1ffb82012-06-06 09:42:57 +02001061 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001062 return;
1063 }
Johannes Bergff1ffb82012-06-06 09:42:57 +02001064 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001065}
1066EXPORT_SYMBOL_GPL(iwl_opmode_deregister);
1067
1068static int __init iwl_drv_init(void)
1069{
1070 int i;
1071
Johannes Bergff1ffb82012-06-06 09:42:57 +02001072 mutex_init(&iwlwifi_opmode_table_mtx);
1073
Don Frycc5f7e32012-05-16 22:54:27 +02001074 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
1075 INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
1076
1077 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
1078 pr_info(DRV_COPYRIGHT "\n");
1079
1080 return iwl_pci_register_driver();
1081}
1082module_init(iwl_drv_init);
1083
1084static void __exit iwl_drv_exit(void)
1085{
1086 iwl_pci_unregister_driver();
1087}
1088module_exit(iwl_drv_exit);
Johannes Berg65de7e82012-04-17 07:36:30 -07001089
1090#ifdef CONFIG_IWLWIFI_DEBUG
1091module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
1092 S_IRUGO | S_IWUSR);
1093MODULE_PARM_DESC(debug, "debug output mask");
1094#endif
1095
1096module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
1097MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
1098module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
1099MODULE_PARM_DESC(11n_disable,
1100 "disable 11n functionality, bitmap: 1: full, 2: agg TX, 4: agg RX");
1101module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
1102 int, S_IRUGO);
1103MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
1104module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, int, S_IRUGO);
1105MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");
1106
1107module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
1108 int, S_IRUGO);
1109MODULE_PARM_DESC(antenna_coupling,
1110 "specify antenna coupling in dB (defualt: 0 dB)");
1111
1112module_param_named(bt_ch_inhibition, iwlwifi_mod_params.bt_ch_announce,
1113 bool, S_IRUGO);
1114MODULE_PARM_DESC(bt_ch_inhibition,
1115 "Enable BT channel inhibition (default: enable)");
1116
1117module_param_named(plcp_check, iwlwifi_mod_params.plcp_check, bool, S_IRUGO);
1118MODULE_PARM_DESC(plcp_check, "Check plcp health (default: 1 [enabled])");
1119
1120module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
1121MODULE_PARM_DESC(wd_disable,
1122 "Disable stuck queue watchdog timer 0=system default, "
1123 "1=disable, 2=enable (default: 0)");
1124
1125/*
1126 * set bt_coex_active to true, uCode will do kill/defer
1127 * every time the priority line is asserted (BT is sending signals on the
1128 * priority line in the PCIx).
1129 * set bt_coex_active to false, uCode will ignore the BT activity and
1130 * perform the normal operation
1131 *
1132 * User might experience transmit issue on some platform due to WiFi/BT
1133 * co-exist problem. The possible behaviors are:
1134 * Able to scan and finding all the available AP
1135 * Not able to associate with any AP
1136 * On those platforms, WiFi communication can be restored by set
1137 * "bt_coex_active" module parameter to "false"
1138 *
1139 * default: bt_coex_active = true (BT_COEX_ENABLE)
1140 */
1141module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
1142 bool, S_IRUGO);
1143MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
1144
1145module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
1146MODULE_PARM_DESC(led_mode, "0=system default, "
1147 "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
1148
1149module_param_named(power_save, iwlwifi_mod_params.power_save,
1150 bool, S_IRUGO);
1151MODULE_PARM_DESC(power_save,
1152 "enable WiFi power management (default: disable)");
1153
1154module_param_named(power_level, iwlwifi_mod_params.power_level,
1155 int, S_IRUGO);
1156MODULE_PARM_DESC(power_level,
1157 "default power save level (range from 1 - 5, default: 1)");
1158
1159module_param_named(auto_agg, iwlwifi_mod_params.auto_agg,
1160 bool, S_IRUGO);
1161MODULE_PARM_DESC(auto_agg,
1162 "enable agg w/o check traffic load (default: enable)");
Stanislaw Gruszkaf6b11542012-04-19 09:51:24 -07001163
1164module_param_named(5ghz_disable, iwlwifi_mod_params.disable_5ghz,
1165 bool, S_IRUGO);
1166MODULE_PARM_DESC(5ghz_disable, "disable 5GHz band (default: 0 [enabled])");