blob: a175997e782965022703e7850a2600f62b5da9ac [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
197static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
198
199#define UCODE_EXPERIMENTAL_INDEX 100
200#define UCODE_EXPERIMENTAL_TAG "exp"
201
Johannes Berg965974a2012-03-06 13:30:38 -0800202static int iwl_request_firmware(struct iwl_drv *drv, bool first)
Johannes Berg15854ef2012-03-05 11:24:50 -0800203{
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200204 const char *name_pre = drv->cfg->fw_name_pre;
Johannes Berg15854ef2012-03-05 11:24:50 -0800205 char tag[8];
206
207 if (first) {
208#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
Johannes Berg965974a2012-03-06 13:30:38 -0800209 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
Johannes Berg15854ef2012-03-05 11:24:50 -0800210 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
Johannes Berg965974a2012-03-06 13:30:38 -0800211 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800212#endif
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200213 drv->fw_index = drv->cfg->ucode_api_max;
Johannes Berg965974a2012-03-06 13:30:38 -0800214 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800215 } else {
Johannes Berg965974a2012-03-06 13:30:38 -0800216 drv->fw_index--;
217 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800218 }
219
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200220 if (drv->fw_index < drv->cfg->ucode_api_min) {
Johannes Berg965974a2012-03-06 13:30:38 -0800221 IWL_ERR(drv, "no suitable firmware found!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800222 return -ENOENT;
223 }
224
Johannes Berg965974a2012-03-06 13:30:38 -0800225 sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Johannes Berg15854ef2012-03-05 11:24:50 -0800226
Johannes Berg965974a2012-03-06 13:30:38 -0800227 IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
228 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800229 ? "EXPERIMENTAL " : "",
Johannes Berg965974a2012-03-06 13:30:38 -0800230 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800231
Johannes Berg965974a2012-03-06 13:30:38 -0800232 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200233 drv->trans->dev,
Johannes Berg965974a2012-03-06 13:30:38 -0800234 GFP_KERNEL, drv, iwl_ucode_callback);
Johannes Berg15854ef2012-03-05 11:24:50 -0800235}
236
David Spinadel0cedacc2012-03-10 13:00:12 -0800237struct fw_img_parsing {
David Spinadel6dfa8d02012-03-10 13:00:14 -0800238 struct fw_sec sec[IWL_UCODE_SECTION_MAX];
David Spinadel0cedacc2012-03-10 13:00:12 -0800239 int sec_counter;
240};
241
David Spinadeled8c8362012-03-10 13:00:13 -0800242/*
243 * struct fw_sec_parsing: to extract fw section and it's offset from tlv
244 */
245struct fw_sec_parsing {
246 __le32 offset;
247 const u8 data[];
248} __packed;
249
250/**
251 * struct iwl_tlv_calib_data - parse the default calib data from TLV
252 *
253 * @ucode_type: the uCode to which the following default calib relates.
254 * @calib: default calibrations.
255 */
256struct iwl_tlv_calib_data {
257 __le32 ucode_type;
258 __le64 calib;
259} __packed;
260
David Spinadel0cedacc2012-03-10 13:00:12 -0800261struct iwl_firmware_pieces {
262 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
Johannes Berg15854ef2012-03-05 11:24:50 -0800263
264 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
265 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
266};
267
David Spinadel0cedacc2012-03-10 13:00:12 -0800268/*
269 * These functions are just to extract uCode section data from the pieces
270 * structure.
271 */
272static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
273 enum iwl_ucode_type type,
274 int sec)
275{
276 return &pieces->img[type].sec[sec];
277}
278
279static void set_sec_data(struct iwl_firmware_pieces *pieces,
280 enum iwl_ucode_type type,
281 int sec,
282 const void *data)
283{
284 pieces->img[type].sec[sec].data = data;
285}
286
287static void set_sec_size(struct iwl_firmware_pieces *pieces,
288 enum iwl_ucode_type type,
289 int sec,
290 size_t size)
291{
292 pieces->img[type].sec[sec].size = size;
293}
294
295static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
296 enum iwl_ucode_type type,
297 int sec)
298{
299 return pieces->img[type].sec[sec].size;
300}
301
302static void set_sec_offset(struct iwl_firmware_pieces *pieces,
303 enum iwl_ucode_type type,
304 int sec,
305 u32 offset)
306{
307 pieces->img[type].sec[sec].offset = offset;
308}
309
David Spinadeled8c8362012-03-10 13:00:13 -0800310/*
311 * Gets uCode section from tlv.
312 */
313static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
314 const void *data, enum iwl_ucode_type type,
315 int size)
316{
317 struct fw_img_parsing *img;
318 struct fw_sec *sec;
319 struct fw_sec_parsing *sec_parse;
320
321 if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
322 return -1;
323
324 sec_parse = (struct fw_sec_parsing *)data;
325
326 img = &pieces->img[type];
327 sec = &img->sec[img->sec_counter];
328
329 sec->offset = le32_to_cpu(sec_parse->offset);
330 sec->data = sec_parse->data;
David Spinadel1176f432012-03-13 14:32:48 +0200331 sec->size = size - sizeof(sec_parse->offset);
David Spinadeled8c8362012-03-10 13:00:13 -0800332
333 ++img->sec_counter;
334
335 return 0;
336}
337
338static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
339{
340 struct iwl_tlv_calib_data *def_calib =
341 (struct iwl_tlv_calib_data *)data;
342 u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
343 if (ucode_type >= IWL_UCODE_TYPE_MAX) {
344 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
345 ucode_type);
346 return -EINVAL;
347 }
348 drv->fw.default_calib[ucode_type] = le64_to_cpu(def_calib->calib);
349 return 0;
350}
351
Johannes Berg965974a2012-03-06 13:30:38 -0800352static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
David Spinadel0cedacc2012-03-10 13:00:12 -0800353 const struct firmware *ucode_raw,
354 struct iwl_firmware_pieces *pieces)
Johannes Berg15854ef2012-03-05 11:24:50 -0800355{
356 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
357 u32 api_ver, hdr_size, build;
358 char buildstr[25];
359 const u8 *src;
360
Johannes Berg965974a2012-03-06 13:30:38 -0800361 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
362 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800363
364 switch (api_ver) {
365 default:
366 hdr_size = 28;
367 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800368 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800369 return -EINVAL;
370 }
371 build = le32_to_cpu(ucode->u.v2.build);
David Spinadel0cedacc2012-03-10 13:00:12 -0800372 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
373 le32_to_cpu(ucode->u.v2.inst_size));
374 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
375 le32_to_cpu(ucode->u.v2.data_size));
376 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
377 le32_to_cpu(ucode->u.v2.init_size));
378 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
379 le32_to_cpu(ucode->u.v2.init_data_size));
Johannes Berg15854ef2012-03-05 11:24:50 -0800380 src = ucode->u.v2.data;
381 break;
382 case 0:
383 case 1:
384 case 2:
385 hdr_size = 24;
386 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800387 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800388 return -EINVAL;
389 }
390 build = 0;
David Spinadel0cedacc2012-03-10 13:00:12 -0800391 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
392 le32_to_cpu(ucode->u.v1.inst_size));
393 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
394 le32_to_cpu(ucode->u.v1.data_size));
395 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
396 le32_to_cpu(ucode->u.v1.init_size));
397 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
398 le32_to_cpu(ucode->u.v1.init_data_size));
Johannes Berg15854ef2012-03-05 11:24:50 -0800399 src = ucode->u.v1.data;
400 break;
401 }
402
403 if (build)
404 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800405 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800406 ? " (EXP)" : "");
407 else
408 buildstr[0] = '\0';
409
Johannes Berg965974a2012-03-06 13:30:38 -0800410 snprintf(drv->fw.fw_version,
411 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800412 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800413 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
414 IWL_UCODE_MINOR(drv->fw.ucode_ver),
415 IWL_UCODE_API(drv->fw.ucode_ver),
416 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef2012-03-05 11:24:50 -0800417 buildstr);
418
419 /* Verify size of file vs. image size info in file's header */
David Spinadel0cedacc2012-03-10 13:00:12 -0800420
421 if (ucode_raw->size != hdr_size +
422 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
423 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
424 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
425 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800426
Johannes Berg965974a2012-03-06 13:30:38 -0800427 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800428 "uCode file size %d does not match expected size\n",
429 (int)ucode_raw->size);
430 return -EINVAL;
431 }
432
Johannes Berg15854ef2012-03-05 11:24:50 -0800433
David Spinadel0cedacc2012-03-10 13:00:12 -0800434 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
435 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
436 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
437 IWLAGN_RTC_INST_LOWER_BOUND);
438 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
439 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
440 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
441 IWLAGN_RTC_DATA_LOWER_BOUND);
442 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
443 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
444 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
445 IWLAGN_RTC_INST_LOWER_BOUND);
446 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
447 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
448 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
449 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800450 return 0;
451}
452
Johannes Berg965974a2012-03-06 13:30:38 -0800453static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800454 const struct firmware *ucode_raw,
David Spinadel0cedacc2012-03-10 13:00:12 -0800455 struct iwl_firmware_pieces *pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -0800456 struct iwl_ucode_capabilities *capa)
457{
458 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
459 struct iwl_ucode_tlv *tlv;
460 size_t len = ucode_raw->size;
461 const u8 *data;
Johannes Berg15854ef2012-03-05 11:24:50 -0800462 u32 tlv_len;
463 enum iwl_ucode_tlv_type tlv_type;
464 const u8 *tlv_data;
465 char buildstr[25];
466 u32 build;
467
468 if (len < sizeof(*ucode)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800469 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800470 return -EINVAL;
471 }
472
473 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800474 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800475 le32_to_cpu(ucode->magic));
476 return -EINVAL;
477 }
478
Johannes Berg965974a2012-03-06 13:30:38 -0800479 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800480 build = le32_to_cpu(ucode->build);
481
482 if (build)
483 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800484 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800485 ? " (EXP)" : "");
486 else
487 buildstr[0] = '\0';
488
Johannes Berg965974a2012-03-06 13:30:38 -0800489 snprintf(drv->fw.fw_version,
490 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800491 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800492 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
493 IWL_UCODE_MINOR(drv->fw.ucode_ver),
494 IWL_UCODE_API(drv->fw.ucode_ver),
495 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef2012-03-05 11:24:50 -0800496 buildstr);
497
498 data = ucode->data;
499
500 len -= sizeof(*ucode);
501
502 while (len >= sizeof(*tlv)) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800503 len -= sizeof(*tlv);
504 tlv = (void *)data;
505
506 tlv_len = le32_to_cpu(tlv->length);
Johannes Berg0479c192012-03-09 09:16:35 +0100507 tlv_type = le32_to_cpu(tlv->type);
Johannes Berg15854ef2012-03-05 11:24:50 -0800508 tlv_data = tlv->data;
509
510 if (len < tlv_len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800511 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800512 len, tlv_len);
513 return -EINVAL;
514 }
515 len -= ALIGN(tlv_len, 4);
516 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
517
Johannes Berg15854ef2012-03-05 11:24:50 -0800518 switch (tlv_type) {
519 case IWL_UCODE_TLV_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800520 set_sec_data(pieces, IWL_UCODE_REGULAR,
521 IWL_UCODE_SECTION_INST, tlv_data);
522 set_sec_size(pieces, IWL_UCODE_REGULAR,
523 IWL_UCODE_SECTION_INST, tlv_len);
524 set_sec_offset(pieces, IWL_UCODE_REGULAR,
525 IWL_UCODE_SECTION_INST,
526 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800527 break;
528 case IWL_UCODE_TLV_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800529 set_sec_data(pieces, IWL_UCODE_REGULAR,
530 IWL_UCODE_SECTION_DATA, tlv_data);
531 set_sec_size(pieces, IWL_UCODE_REGULAR,
532 IWL_UCODE_SECTION_DATA, tlv_len);
533 set_sec_offset(pieces, IWL_UCODE_REGULAR,
534 IWL_UCODE_SECTION_DATA,
535 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800536 break;
537 case IWL_UCODE_TLV_INIT:
David Spinadel0cedacc2012-03-10 13:00:12 -0800538 set_sec_data(pieces, IWL_UCODE_INIT,
539 IWL_UCODE_SECTION_INST, tlv_data);
540 set_sec_size(pieces, IWL_UCODE_INIT,
541 IWL_UCODE_SECTION_INST, tlv_len);
542 set_sec_offset(pieces, IWL_UCODE_INIT,
543 IWL_UCODE_SECTION_INST,
544 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800545 break;
546 case IWL_UCODE_TLV_INIT_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800547 set_sec_data(pieces, IWL_UCODE_INIT,
548 IWL_UCODE_SECTION_DATA, tlv_data);
549 set_sec_size(pieces, IWL_UCODE_INIT,
550 IWL_UCODE_SECTION_DATA, tlv_len);
551 set_sec_offset(pieces, IWL_UCODE_INIT,
552 IWL_UCODE_SECTION_DATA,
553 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800554 break;
555 case IWL_UCODE_TLV_BOOT:
Johannes Berg965974a2012-03-06 13:30:38 -0800556 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800557 break;
558 case IWL_UCODE_TLV_PROBE_MAX_LEN:
559 if (tlv_len != sizeof(u32))
560 goto invalid_tlv_len;
561 capa->max_probe_length =
562 le32_to_cpup((__le32 *)tlv_data);
563 break;
564 case IWL_UCODE_TLV_PAN:
565 if (tlv_len)
566 goto invalid_tlv_len;
567 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
568 break;
569 case IWL_UCODE_TLV_FLAGS:
570 /* must be at least one u32 */
571 if (tlv_len < sizeof(u32))
572 goto invalid_tlv_len;
573 /* and a proper number of u32s */
574 if (tlv_len % sizeof(u32))
575 goto invalid_tlv_len;
576 /*
577 * This driver only reads the first u32 as
578 * right now no more features are defined,
579 * if that changes then either the driver
580 * will not work with the new firmware, or
581 * it'll not take advantage of new features.
582 */
583 capa->flags = le32_to_cpup((__le32 *)tlv_data);
584 break;
585 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
586 if (tlv_len != sizeof(u32))
587 goto invalid_tlv_len;
588 pieces->init_evtlog_ptr =
589 le32_to_cpup((__le32 *)tlv_data);
590 break;
591 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
592 if (tlv_len != sizeof(u32))
593 goto invalid_tlv_len;
594 pieces->init_evtlog_size =
595 le32_to_cpup((__le32 *)tlv_data);
596 break;
597 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
598 if (tlv_len != sizeof(u32))
599 goto invalid_tlv_len;
600 pieces->init_errlog_ptr =
601 le32_to_cpup((__le32 *)tlv_data);
602 break;
603 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
604 if (tlv_len != sizeof(u32))
605 goto invalid_tlv_len;
606 pieces->inst_evtlog_ptr =
607 le32_to_cpup((__le32 *)tlv_data);
608 break;
609 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
610 if (tlv_len != sizeof(u32))
611 goto invalid_tlv_len;
612 pieces->inst_evtlog_size =
613 le32_to_cpup((__le32 *)tlv_data);
614 break;
615 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
616 if (tlv_len != sizeof(u32))
617 goto invalid_tlv_len;
618 pieces->inst_errlog_ptr =
619 le32_to_cpup((__le32 *)tlv_data);
620 break;
621 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
622 if (tlv_len)
623 goto invalid_tlv_len;
Johannes Berg965974a2012-03-06 13:30:38 -0800624 drv->fw.enhance_sensitivity_table = true;
Johannes Berg15854ef2012-03-05 11:24:50 -0800625 break;
626 case IWL_UCODE_TLV_WOWLAN_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800627 set_sec_data(pieces, IWL_UCODE_WOWLAN,
628 IWL_UCODE_SECTION_INST, tlv_data);
629 set_sec_size(pieces, IWL_UCODE_WOWLAN,
630 IWL_UCODE_SECTION_INST, tlv_len);
631 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
632 IWL_UCODE_SECTION_INST,
633 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800634 break;
635 case IWL_UCODE_TLV_WOWLAN_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800636 set_sec_data(pieces, IWL_UCODE_WOWLAN,
637 IWL_UCODE_SECTION_DATA, tlv_data);
638 set_sec_size(pieces, IWL_UCODE_WOWLAN,
639 IWL_UCODE_SECTION_DATA, tlv_len);
640 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
641 IWL_UCODE_SECTION_DATA,
642 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800643 break;
644 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
645 if (tlv_len != sizeof(u32))
646 goto invalid_tlv_len;
647 capa->standard_phy_calibration_size =
648 le32_to_cpup((__le32 *)tlv_data);
649 break;
David Spinadeled8c8362012-03-10 13:00:13 -0800650 case IWL_UCODE_TLV_SEC_RT:
651 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
652 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800653 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800654 break;
655 case IWL_UCODE_TLV_SEC_INIT:
656 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
657 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800658 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800659 break;
660 case IWL_UCODE_TLV_SEC_WOWLAN:
661 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
662 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800663 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800664 break;
665 case IWL_UCODE_TLV_DEF_CALIB:
666 if (tlv_len != sizeof(struct iwl_tlv_calib_data))
667 goto invalid_tlv_len;
668 if (iwl_set_default_calib(drv, tlv_data))
669 goto tlv_error;
670 break;
671 case IWL_UCODE_TLV_PHY_SKU:
672 if (tlv_len != sizeof(u32))
673 goto invalid_tlv_len;
674 drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
675 break;
Johannes Berg15854ef2012-03-05 11:24:50 -0800676 default:
Johannes Berg965974a2012-03-06 13:30:38 -0800677 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
Johannes Berg15854ef2012-03-05 11:24:50 -0800678 break;
679 }
680 }
681
682 if (len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800683 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
684 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800685 return -EINVAL;
686 }
687
688 return 0;
689
690 invalid_tlv_len:
Johannes Berg965974a2012-03-06 13:30:38 -0800691 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
David Spinadeled8c8362012-03-10 13:00:13 -0800692 tlv_error:
Johannes Berg965974a2012-03-06 13:30:38 -0800693 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800694
695 return -EINVAL;
696}
697
Johannes Berg75813bd2012-05-16 22:49:49 +0200698static int iwl_alloc_ucode(struct iwl_drv *drv,
699 struct iwl_firmware_pieces *pieces,
700 enum iwl_ucode_type type)
David Spinadel6dfa8d02012-03-10 13:00:14 -0800701{
702 int i;
703 for (i = 0;
704 i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
705 i++)
706 if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
Johannes Berg75813bd2012-05-16 22:49:49 +0200707 get_sec(pieces, type, i)))
708 return -ENOMEM;
David Spinadel6dfa8d02012-03-10 13:00:14 -0800709 return 0;
710}
711
712static int validate_sec_sizes(struct iwl_drv *drv,
713 struct iwl_firmware_pieces *pieces,
714 const struct iwl_cfg *cfg)
715{
716 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
717 get_sec_size(pieces, IWL_UCODE_REGULAR,
718 IWL_UCODE_SECTION_INST));
719 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
720 get_sec_size(pieces, IWL_UCODE_REGULAR,
721 IWL_UCODE_SECTION_DATA));
722 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
723 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
724 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
725 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
726
727 /* Verify that uCode images will fit in card's SRAM. */
728 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
729 cfg->max_inst_size) {
730 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
731 get_sec_size(pieces, IWL_UCODE_REGULAR,
732 IWL_UCODE_SECTION_INST));
733 return -1;
734 }
735
736 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
737 cfg->max_data_size) {
738 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
739 get_sec_size(pieces, IWL_UCODE_REGULAR,
740 IWL_UCODE_SECTION_DATA));
741 return -1;
742 }
743
744 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
745 cfg->max_inst_size) {
746 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
747 get_sec_size(pieces, IWL_UCODE_INIT,
748 IWL_UCODE_SECTION_INST));
749 return -1;
750 }
751
752 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
753 cfg->max_data_size) {
754 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
755 get_sec_size(pieces, IWL_UCODE_REGULAR,
756 IWL_UCODE_SECTION_DATA));
757 return -1;
758 }
759 return 0;
760}
761
Johannes Berg15854ef2012-03-05 11:24:50 -0800762/**
763 * iwl_ucode_callback - callback when firmware was loaded
764 *
765 * If loaded successfully, copies the firmware into buffers
766 * for the card to fetch (via DMA).
767 */
768static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
769{
Johannes Berg965974a2012-03-06 13:30:38 -0800770 struct iwl_drv *drv = context;
Johannes Berg965974a2012-03-06 13:30:38 -0800771 struct iwl_fw *fw = &drv->fw;
Johannes Berg15854ef2012-03-05 11:24:50 -0800772 struct iwl_ucode_header *ucode;
Don Frycc5f7e32012-05-16 22:54:27 +0200773 struct iwlwifi_opmode_table *op;
Johannes Berg15854ef2012-03-05 11:24:50 -0800774 int err;
David Spinadel0cedacc2012-03-10 13:00:12 -0800775 struct iwl_firmware_pieces pieces;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200776 const unsigned int api_max = drv->cfg->ucode_api_max;
777 unsigned int api_ok = drv->cfg->ucode_api_ok;
778 const unsigned int api_min = drv->cfg->ucode_api_min;
Johannes Berg15854ef2012-03-05 11:24:50 -0800779 u32 api_ver;
David Spinadel6dfa8d02012-03-10 13:00:14 -0800780 int i;
Johannes Bergd4b10482012-06-12 19:50:43 +0200781 bool load_module = false;
Johannes Berg15854ef2012-03-05 11:24:50 -0800782
783 fw->ucode_capa.max_probe_length = 200;
784 fw->ucode_capa.standard_phy_calibration_size =
785 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
786
787 if (!api_ok)
788 api_ok = api_max;
789
790 memset(&pieces, 0, sizeof(pieces));
791
792 if (!ucode_raw) {
Johannes Berg965974a2012-03-06 13:30:38 -0800793 if (drv->fw_index <= api_ok)
794 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800795 "request for firmware file '%s' failed.\n",
Johannes Berg965974a2012-03-06 13:30:38 -0800796 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800797 goto try_again;
798 }
799
Johannes Berg965974a2012-03-06 13:30:38 -0800800 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
801 drv->firmware_name, ucode_raw->size);
Johannes Berg15854ef2012-03-05 11:24:50 -0800802
803 /* Make sure that we got at least the API version number */
804 if (ucode_raw->size < 4) {
Johannes Berg965974a2012-03-06 13:30:38 -0800805 IWL_ERR(drv, "File size way too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800806 goto try_again;
807 }
808
809 /* Data from ucode file: header followed by uCode images */
810 ucode = (struct iwl_ucode_header *)ucode_raw->data;
811
812 if (ucode->ver)
Johannes Berg965974a2012-03-06 13:30:38 -0800813 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
Johannes Berg15854ef2012-03-05 11:24:50 -0800814 else
Johannes Berg965974a2012-03-06 13:30:38 -0800815 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -0800816 &fw->ucode_capa);
817
818 if (err)
819 goto try_again;
820
Johannes Berg965974a2012-03-06 13:30:38 -0800821 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800822
823 /*
824 * api_ver should match the api version forming part of the
825 * firmware filename ... but we don't check for that and only rely
826 * on the API version read from firmware header from here on forward
827 */
828 /* no api version check required for experimental uCode */
Johannes Berg965974a2012-03-06 13:30:38 -0800829 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800830 if (api_ver < api_min || api_ver > api_max) {
Johannes Berg965974a2012-03-06 13:30:38 -0800831 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800832 "Driver unable to support your firmware API. "
833 "Driver supports v%u, firmware is v%u.\n",
834 api_max, api_ver);
835 goto try_again;
836 }
837
838 if (api_ver < api_ok) {
839 if (api_ok != api_max)
Johannes Berg965974a2012-03-06 13:30:38 -0800840 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -0800841 "expected v%u through v%u, got v%u.\n",
842 api_ok, api_max, api_ver);
843 else
Johannes Berg965974a2012-03-06 13:30:38 -0800844 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -0800845 "expected v%u, got v%u.\n",
846 api_max, api_ver);
Johannes Berg965974a2012-03-06 13:30:38 -0800847 IWL_ERR(drv, "New firmware can be obtained from "
Johannes Berg15854ef2012-03-05 11:24:50 -0800848 "http://www.intellinuxwireless.org/.\n");
849 }
850 }
851
Johannes Berg965974a2012-03-06 13:30:38 -0800852 IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
Johannes Berg15854ef2012-03-05 11:24:50 -0800853
854 /*
David Spinadel4db2c9a2012-03-10 13:00:15 -0800855 * In mvm uCode there is no difference between data and instructions
856 * sections.
857 */
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200858 if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
Johannes Berg15854ef2012-03-05 11:24:50 -0800859 goto try_again;
Johannes Berg15854ef2012-03-05 11:24:50 -0800860
861 /* Allocate ucode buffers for card's bus-master loading ... */
862
863 /* Runtime instructions and 2 copies of data:
864 * 1) unmodified from disk
865 * 2) backup cache for save/restore during power-downs */
David Spinadel6dfa8d02012-03-10 13:00:14 -0800866 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
Johannes Berg75813bd2012-05-16 22:49:49 +0200867 if (iwl_alloc_ucode(drv, &pieces, i))
868 goto out_free_fw;
Johannes Berg15854ef2012-03-05 11:24:50 -0800869
870 /* Now that we can no longer fail, copy information */
871
872 /*
873 * The (size - 16) / 12 formula is based on the information recorded
874 * for each event, which is of mode 1 (including timestamp) for all
875 * new microcodes that include this information.
876 */
Johannes Berg0692fe42012-03-06 13:30:37 -0800877 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800878 if (pieces.init_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800879 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -0800880 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800881 fw->init_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200882 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800883 fw->init_errlog_ptr = pieces.init_errlog_ptr;
884 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800885 if (pieces.inst_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800886 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -0800887 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800888 fw->inst_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200889 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800890 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800891
892 /*
893 * figure out the offset of chain noise reset and gain commands
894 * base on the size of standard phy calibration commands table size
895 */
896 if (fw->ucode_capa.standard_phy_calibration_size >
897 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
898 fw->ucode_capa.standard_phy_calibration_size =
899 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
900
901 /* We have our copies now, allow OS release its copies */
902 release_firmware(ucode_raw);
Johannes Berg15854ef2012-03-05 11:24:50 -0800903
Johannes Bergff1ffb82012-06-06 09:42:57 +0200904 mutex_lock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +0200905 op = &iwlwifi_opmode_table[DVM_OP_MODE];
Johannes Berg15854ef2012-03-05 11:24:50 -0800906
Don Frycc5f7e32012-05-16 22:54:27 +0200907 /* add this device to the list of devices using this op_mode */
908 list_add_tail(&drv->list, &op->drv);
909
910 if (op->ops) {
911 const struct iwl_op_mode_ops *ops = op->ops;
912 drv->op_mode = ops->start(drv->trans, drv->cfg, &drv->fw);
John W. Linville7c9c46c2012-06-06 14:40:06 -0400913
Dan Carpenterdaf67ce2012-06-14 21:35:26 +0300914 if (!drv->op_mode) {
915 mutex_unlock(&iwlwifi_opmode_table_mtx);
John W. Linville7c9c46c2012-06-06 14:40:06 -0400916 goto out_unbind;
Dan Carpenterdaf67ce2012-06-14 21:35:26 +0300917 }
Don Frycc5f7e32012-05-16 22:54:27 +0200918 } else {
Johannes Bergd4b10482012-06-12 19:50:43 +0200919 load_module = true;
Don Frycc5f7e32012-05-16 22:54:27 +0200920 }
Johannes Bergff1ffb82012-06-06 09:42:57 +0200921 mutex_unlock(&iwlwifi_opmode_table_mtx);
Johannes Berg15854ef2012-03-05 11:24:50 -0800922
Johannes Bergf69a23b2012-06-05 19:56:06 +0200923 /*
924 * Complete the firmware request last so that
925 * a driver unbind (stop) doesn't run while we
926 * are doing the start() above.
927 */
928 complete(&drv->request_firmware_complete);
Johannes Bergd4b10482012-06-12 19:50:43 +0200929
930 /*
931 * Load the module last so we don't block anything
932 * else from proceeding if the module fails to load
933 * or hangs loading.
934 */
935 if (load_module)
936 request_module("%s", op->name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800937 return;
938
939 try_again:
940 /* try next, if any */
941 release_firmware(ucode_raw);
Johannes Berg965974a2012-03-06 13:30:38 -0800942 if (iwl_request_firmware(drv, false))
Johannes Berg15854ef2012-03-05 11:24:50 -0800943 goto out_unbind;
944 return;
945
Johannes Berg75813bd2012-05-16 22:49:49 +0200946 out_free_fw:
Johannes Berg965974a2012-03-06 13:30:38 -0800947 IWL_ERR(drv, "failed to allocate pci memory\n");
948 iwl_dealloc_ucode(drv);
Johannes Berg15854ef2012-03-05 11:24:50 -0800949 release_firmware(ucode_raw);
950 out_unbind:
Johannes Berg965974a2012-03-06 13:30:38 -0800951 complete(&drv->request_firmware_complete);
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200952 device_release_driver(drv->trans->dev);
Johannes Berg15854ef2012-03-05 11:24:50 -0800953}
954
Emmanuel Grumbach87ce05a2012-03-26 09:03:18 -0700955struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700956 const struct iwl_cfg *cfg)
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200957{
Johannes Berg965974a2012-03-06 13:30:38 -0800958 struct iwl_drv *drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200959 int ret;
960
Johannes Berg965974a2012-03-06 13:30:38 -0800961 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +0300962 if (!drv)
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700963 return NULL;
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +0300964
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700965 drv->trans = trans;
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +0200966 drv->dev = trans->dev;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200967 drv->cfg = cfg;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200968
Johannes Berg965974a2012-03-06 13:30:38 -0800969 init_completion(&drv->request_firmware_complete);
Johannes Bergff1ffb82012-06-06 09:42:57 +0200970 INIT_LIST_HEAD(&drv->list);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200971
Johannes Berg965974a2012-03-06 13:30:38 -0800972 ret = iwl_request_firmware(drv, true);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200973
974 if (ret) {
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +0300975 IWL_ERR(trans, "Couldn't request the fw\n");
Johannes Berg965974a2012-03-06 13:30:38 -0800976 kfree(drv);
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700977 drv = NULL;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200978 }
979
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700980 return drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200981}
982
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700983void iwl_drv_stop(struct iwl_drv *drv)
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200984{
Johannes Berg965974a2012-03-06 13:30:38 -0800985 wait_for_completion(&drv->request_firmware_complete);
Johannes Berg2e7eb112012-03-05 11:24:51 -0800986
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200987 /* op_mode can be NULL if its start failed */
Johannes Berg965974a2012-03-06 13:30:38 -0800988 if (drv->op_mode)
989 iwl_op_mode_stop(drv->op_mode);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200990
Johannes Berg965974a2012-03-06 13:30:38 -0800991 iwl_dealloc_ucode(drv);
Johannes Berg7db5b982012-03-05 11:24:48 -0800992
Johannes Bergff1ffb82012-06-06 09:42:57 +0200993 mutex_lock(&iwlwifi_opmode_table_mtx);
994 /*
995 * List is empty (this item wasn't added)
996 * when firmware loading failed -- in that
997 * case we can't remove it from any list.
998 */
999 if (!list_empty(&drv->list))
1000 list_del(&drv->list);
1001 mutex_unlock(&iwlwifi_opmode_table_mtx);
1002
Johannes Berg965974a2012-03-06 13:30:38 -08001003 kfree(drv);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +02001004}
Johannes Berg65de7e82012-04-17 07:36:30 -07001005
1006
1007/* shared module parameters */
1008struct iwl_mod_params iwlwifi_mod_params = {
1009 .amsdu_size_8K = 1,
1010 .restart_fw = 1,
1011 .plcp_check = true,
1012 .bt_coex_active = true,
1013 .power_level = IWL_POWER_INDEX_1,
1014 .bt_ch_announce = true,
1015 .auto_agg = true,
1016 /* the rest are 0 by default */
1017};
Don Frycc5f7e32012-05-16 22:54:27 +02001018EXPORT_SYMBOL_GPL(iwlwifi_mod_params);
1019
1020int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
1021{
1022 int i;
1023 struct iwl_drv *drv;
1024
Johannes Bergff1ffb82012-06-06 09:42:57 +02001025 mutex_lock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001026 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1027 if (strcmp(iwlwifi_opmode_table[i].name, name))
1028 continue;
1029 iwlwifi_opmode_table[i].ops = ops;
1030 list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
1031 drv->op_mode = ops->start(drv->trans, drv->cfg,
1032 &drv->fw);
Johannes Bergff1ffb82012-06-06 09:42:57 +02001033 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001034 return 0;
1035 }
Johannes Bergff1ffb82012-06-06 09:42:57 +02001036 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001037 return -EIO;
1038}
1039EXPORT_SYMBOL_GPL(iwl_opmode_register);
1040
1041void iwl_opmode_deregister(const char *name)
1042{
1043 int i;
1044 struct iwl_drv *drv;
1045
Johannes Bergff1ffb82012-06-06 09:42:57 +02001046 mutex_lock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001047 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1048 if (strcmp(iwlwifi_opmode_table[i].name, name))
1049 continue;
1050 iwlwifi_opmode_table[i].ops = NULL;
1051
1052 /* call the stop routine for all devices */
1053 list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list) {
1054 if (drv->op_mode) {
1055 iwl_op_mode_stop(drv->op_mode);
1056 drv->op_mode = NULL;
1057 }
1058 }
Johannes Bergff1ffb82012-06-06 09:42:57 +02001059 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001060 return;
1061 }
Johannes Bergff1ffb82012-06-06 09:42:57 +02001062 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001063}
1064EXPORT_SYMBOL_GPL(iwl_opmode_deregister);
1065
1066static int __init iwl_drv_init(void)
1067{
1068 int i;
1069
Johannes Bergff1ffb82012-06-06 09:42:57 +02001070 mutex_init(&iwlwifi_opmode_table_mtx);
1071
Don Frycc5f7e32012-05-16 22:54:27 +02001072 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
1073 INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
1074
1075 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
1076 pr_info(DRV_COPYRIGHT "\n");
1077
1078 return iwl_pci_register_driver();
1079}
1080module_init(iwl_drv_init);
1081
1082static void __exit iwl_drv_exit(void)
1083{
1084 iwl_pci_unregister_driver();
1085}
1086module_exit(iwl_drv_exit);
Johannes Berg65de7e82012-04-17 07:36:30 -07001087
1088#ifdef CONFIG_IWLWIFI_DEBUG
1089module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
1090 S_IRUGO | S_IWUSR);
1091MODULE_PARM_DESC(debug, "debug output mask");
1092#endif
1093
1094module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
1095MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
1096module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
1097MODULE_PARM_DESC(11n_disable,
1098 "disable 11n functionality, bitmap: 1: full, 2: agg TX, 4: agg RX");
1099module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
1100 int, S_IRUGO);
1101MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
1102module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, int, S_IRUGO);
1103MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");
1104
1105module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
1106 int, S_IRUGO);
1107MODULE_PARM_DESC(antenna_coupling,
1108 "specify antenna coupling in dB (defualt: 0 dB)");
1109
1110module_param_named(bt_ch_inhibition, iwlwifi_mod_params.bt_ch_announce,
1111 bool, S_IRUGO);
1112MODULE_PARM_DESC(bt_ch_inhibition,
1113 "Enable BT channel inhibition (default: enable)");
1114
1115module_param_named(plcp_check, iwlwifi_mod_params.plcp_check, bool, S_IRUGO);
1116MODULE_PARM_DESC(plcp_check, "Check plcp health (default: 1 [enabled])");
1117
1118module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
1119MODULE_PARM_DESC(wd_disable,
1120 "Disable stuck queue watchdog timer 0=system default, "
1121 "1=disable, 2=enable (default: 0)");
1122
1123/*
1124 * set bt_coex_active to true, uCode will do kill/defer
1125 * every time the priority line is asserted (BT is sending signals on the
1126 * priority line in the PCIx).
1127 * set bt_coex_active to false, uCode will ignore the BT activity and
1128 * perform the normal operation
1129 *
1130 * User might experience transmit issue on some platform due to WiFi/BT
1131 * co-exist problem. The possible behaviors are:
1132 * Able to scan and finding all the available AP
1133 * Not able to associate with any AP
1134 * On those platforms, WiFi communication can be restored by set
1135 * "bt_coex_active" module parameter to "false"
1136 *
1137 * default: bt_coex_active = true (BT_COEX_ENABLE)
1138 */
1139module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
1140 bool, S_IRUGO);
1141MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
1142
1143module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
1144MODULE_PARM_DESC(led_mode, "0=system default, "
1145 "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
1146
1147module_param_named(power_save, iwlwifi_mod_params.power_save,
1148 bool, S_IRUGO);
1149MODULE_PARM_DESC(power_save,
1150 "enable WiFi power management (default: disable)");
1151
1152module_param_named(power_level, iwlwifi_mod_params.power_level,
1153 int, S_IRUGO);
1154MODULE_PARM_DESC(power_level,
1155 "default power save level (range from 1 - 5, default: 1)");
1156
1157module_param_named(auto_agg, iwlwifi_mod_params.auto_agg,
1158 bool, S_IRUGO);
1159MODULE_PARM_DESC(auto_agg,
1160 "enable agg w/o check traffic load (default: enable)");
Stanislaw Gruszkaf6b11542012-04-19 09:51:24 -07001161
1162module_param_named(5ghz_disable, iwlwifi_mod_params.disable_5ghz,
1163 bool, S_IRUGO);
1164MODULE_PARM_DESC(5ghz_disable, "disable 5GHz band (default: 0 [enabled])");