blob: 283aee0e28a4ddc61f7a877c1fa50823322ffb25 [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 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02008 * Copyright(c) 2007 - 2014 Intel Corporation. All rights reserved.
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02009 *
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
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020025 * in the file called COPYING.
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020026 *
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 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020033 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020034 * 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>
Johannes Berg83f84d72012-09-10 11:50:18 +020067#include <linux/vmalloc.h>
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020068
69#include "iwl-drv.h"
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +030070#include "iwl-debug.h"
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020071#include "iwl-trans.h"
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +020072#include "iwl-op-mode.h"
David Spinadel0cedacc2012-03-10 13:00:12 -080073#include "iwl-agn-hw.h"
Johannes Berg6238b002012-04-02 15:04:33 +020074#include "iwl-fw.h"
75#include "iwl-config.h"
Johannes Berg65de7e82012-04-17 07:36:30 -070076#include "iwl-modparams.h"
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020077
Johannes Berg0692fe42012-03-06 13:30:37 -080078/* private includes */
Johannes Berg3995dea2012-03-06 13:30:44 -080079#include "iwl-fw-file.h"
Johannes Berg0692fe42012-03-06 13:30:37 -080080
Don Frycc5f7e32012-05-16 22:54:27 +020081/******************************************************************************
82 *
83 * module boiler plate
84 *
85 ******************************************************************************/
86
87/*
88 * module name, copyright, version, etc.
89 */
90#define DRV_DESCRIPTION "Intel(R) Wireless WiFi driver for Linux"
91
92#ifdef CONFIG_IWLWIFI_DEBUG
93#define VD "d"
94#else
95#define VD
96#endif
97
98#define DRV_VERSION IWLWIFI_VERSION VD
99
100MODULE_DESCRIPTION(DRV_DESCRIPTION);
101MODULE_VERSION(DRV_VERSION);
102MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
103MODULE_LICENSE("GPL");
104
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -0700105#ifdef CONFIG_IWLWIFI_DEBUGFS
106static struct dentry *iwl_dbgfs_root;
107#endif
108
Johannes Berg965974a2012-03-06 13:30:38 -0800109/**
110 * struct iwl_drv - drv common data
Don Frycc5f7e32012-05-16 22:54:27 +0200111 * @list: list of drv structures using this opmode
Johannes Berg965974a2012-03-06 13:30:38 -0800112 * @fw: the iwl_fw structure
Johannes Berg965974a2012-03-06 13:30:38 -0800113 * @op_mode: the running op_mode
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200114 * @trans: transport layer
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +0200115 * @dev: for debug prints only
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200116 * @cfg: configuration struct
Johannes Berg965974a2012-03-06 13:30:38 -0800117 * @fw_index: firmware revision to try loading
118 * @firmware_name: composite filename of ucode file to load
119 * @request_firmware_complete: the firmware has been obtained from user space
120 */
121struct iwl_drv {
Don Frycc5f7e32012-05-16 22:54:27 +0200122 struct list_head list;
Johannes Berg965974a2012-03-06 13:30:38 -0800123 struct iwl_fw fw;
124
Johannes Berg965974a2012-03-06 13:30:38 -0800125 struct iwl_op_mode *op_mode;
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -0700126 struct iwl_trans *trans;
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +0200127 struct device *dev;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200128 const struct iwl_cfg *cfg;
Johannes Berg965974a2012-03-06 13:30:38 -0800129
130 int fw_index; /* firmware we're trying to load */
Liad Kaufman84b03122014-01-27 16:34:23 +0200131 char firmware_name[32]; /* name of firmware file to load */
Johannes Berg965974a2012-03-06 13:30:38 -0800132
133 struct completion request_firmware_complete;
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -0700134
135#ifdef CONFIG_IWLWIFI_DEBUGFS
136 struct dentry *dbgfs_drv;
137 struct dentry *dbgfs_trans;
138 struct dentry *dbgfs_op_mode;
139#endif
Johannes Berg965974a2012-03-06 13:30:38 -0800140};
141
Johannes Berg8ca151b2013-01-24 14:25:36 +0100142enum {
143 DVM_OP_MODE = 0,
144 MVM_OP_MODE = 1,
145};
Johannes Berg965974a2012-03-06 13:30:38 -0800146
Johannes Bergff1ffb82012-06-06 09:42:57 +0200147/* Protects the table contents, i.e. the ops pointer & drv list */
148static struct mutex iwlwifi_opmode_table_mtx;
Don Frycc5f7e32012-05-16 22:54:27 +0200149static struct iwlwifi_opmode_table {
150 const char *name; /* name: iwldvm, iwlmvm, etc */
151 const struct iwl_op_mode_ops *ops; /* pointer to op_mode ops */
152 struct list_head drv; /* list of devices using this op_mode */
153} iwlwifi_opmode_table[] = { /* ops set when driver is initialized */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100154 [DVM_OP_MODE] = { .name = "iwldvm", .ops = NULL },
155 [MVM_OP_MODE] = { .name = "iwlmvm", .ops = NULL },
Don Frycc5f7e32012-05-16 22:54:27 +0200156};
Johannes Berg965974a2012-03-06 13:30:38 -0800157
David Spinadel762533b2014-06-05 11:20:43 +0300158#define IWL_DEFAULT_SCAN_CHANNELS 40
159
David Spinadel0cedacc2012-03-10 13:00:12 -0800160/*
161 * struct fw_sec: Just for the image parsing proccess.
162 * For the fw storage we are using struct fw_desc.
163 */
164struct fw_sec {
165 const void *data; /* the sec data */
166 size_t size; /* section size */
167 u32 offset; /* offset of writing in the device */
168};
169
Johannes Berg965974a2012-03-06 13:30:38 -0800170static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
Johannes Berg15854ef2012-03-05 11:24:50 -0800171{
Johannes Berg83f84d72012-09-10 11:50:18 +0200172 vfree(desc->data);
173 desc->data = NULL;
Johannes Berg15854ef2012-03-05 11:24:50 -0800174 desc->len = 0;
175}
176
Johannes Berg965974a2012-03-06 13:30:38 -0800177static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
Johannes Berg15854ef2012-03-05 11:24:50 -0800178{
David Spinadel6dfa8d02012-03-10 13:00:14 -0800179 int i;
180 for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
181 iwl_free_fw_desc(drv, &img->sec[i]);
Johannes Berg15854ef2012-03-05 11:24:50 -0800182}
183
Johannes Berg965974a2012-03-06 13:30:38 -0800184static void iwl_dealloc_ucode(struct iwl_drv *drv)
Johannes Berg15854ef2012-03-05 11:24:50 -0800185{
David Spinadel6dfa8d02012-03-10 13:00:14 -0800186 int i;
187 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
188 iwl_free_fw_img(drv, drv->fw.img + i);
Johannes Berg15854ef2012-03-05 11:24:50 -0800189}
190
Johannes Berg965974a2012-03-06 13:30:38 -0800191static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
Johannes Berg83f84d72012-09-10 11:50:18 +0200192 struct fw_sec *sec)
Johannes Berg15854ef2012-03-05 11:24:50 -0800193{
Johannes Berg83f84d72012-09-10 11:50:18 +0200194 void *data;
Johannes Berg15854ef2012-03-05 11:24:50 -0800195
Johannes Berg83f84d72012-09-10 11:50:18 +0200196 desc->data = NULL;
197
198 if (!sec || !sec->size)
199 return -EINVAL;
200
201 data = vmalloc(sec->size);
202 if (!data)
Johannes Berg15854ef2012-03-05 11:24:50 -0800203 return -ENOMEM;
204
David Spinadel0cedacc2012-03-10 13:00:12 -0800205 desc->len = sec->size;
206 desc->offset = sec->offset;
Johannes Berg83f84d72012-09-10 11:50:18 +0200207 memcpy(data, sec->data, desc->len);
208 desc->data = data;
209
Johannes Berg15854ef2012-03-05 11:24:50 -0800210 return 0;
211}
212
Emmanuel Grumbach1c8e11e2012-07-16 12:31:28 +0300213static void iwl_req_fw_callback(const struct firmware *ucode_raw,
214 void *context);
Johannes Berg15854ef2012-03-05 11:24:50 -0800215
216#define UCODE_EXPERIMENTAL_INDEX 100
217#define UCODE_EXPERIMENTAL_TAG "exp"
218
Johannes Berg965974a2012-03-06 13:30:38 -0800219static int iwl_request_firmware(struct iwl_drv *drv, bool first)
Johannes Berg15854ef2012-03-05 11:24:50 -0800220{
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200221 const char *name_pre = drv->cfg->fw_name_pre;
Johannes Berg15854ef2012-03-05 11:24:50 -0800222 char tag[8];
223
224 if (first) {
225#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
Johannes Berg965974a2012-03-06 13:30:38 -0800226 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
Johannes Berg15854ef2012-03-05 11:24:50 -0800227 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
Johannes Berg965974a2012-03-06 13:30:38 -0800228 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800229#endif
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200230 drv->fw_index = drv->cfg->ucode_api_max;
Johannes Berg965974a2012-03-06 13:30:38 -0800231 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800232 } else {
Johannes Berg965974a2012-03-06 13:30:38 -0800233 drv->fw_index--;
234 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800235 }
236
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200237 if (drv->fw_index < drv->cfg->ucode_api_min) {
Johannes Berg965974a2012-03-06 13:30:38 -0800238 IWL_ERR(drv, "no suitable firmware found!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800239 return -ENOENT;
240 }
241
Liad Kaufman84b03122014-01-27 16:34:23 +0200242 snprintf(drv->firmware_name, sizeof(drv->firmware_name), "%s%s.ucode",
243 name_pre, tag);
Johannes Berg15854ef2012-03-05 11:24:50 -0800244
Johannes Berg965974a2012-03-06 13:30:38 -0800245 IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
246 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800247 ? "EXPERIMENTAL " : "",
Johannes Berg965974a2012-03-06 13:30:38 -0800248 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800249
Johannes Berg965974a2012-03-06 13:30:38 -0800250 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +0200251 drv->trans->dev,
Emmanuel Grumbach1c8e11e2012-07-16 12:31:28 +0300252 GFP_KERNEL, drv, iwl_req_fw_callback);
Johannes Berg15854ef2012-03-05 11:24:50 -0800253}
254
David Spinadel0cedacc2012-03-10 13:00:12 -0800255struct fw_img_parsing {
David Spinadel6dfa8d02012-03-10 13:00:14 -0800256 struct fw_sec sec[IWL_UCODE_SECTION_MAX];
David Spinadel0cedacc2012-03-10 13:00:12 -0800257 int sec_counter;
258};
259
David Spinadeled8c8362012-03-10 13:00:13 -0800260/*
261 * struct fw_sec_parsing: to extract fw section and it's offset from tlv
262 */
263struct fw_sec_parsing {
264 __le32 offset;
265 const u8 data[];
266} __packed;
267
268/**
269 * struct iwl_tlv_calib_data - parse the default calib data from TLV
270 *
271 * @ucode_type: the uCode to which the following default calib relates.
272 * @calib: default calibrations.
273 */
274struct iwl_tlv_calib_data {
275 __le32 ucode_type;
Johannes Bergaa2b1772013-01-24 14:12:07 +0100276 struct iwl_tlv_calib_ctrl calib;
David Spinadeled8c8362012-03-10 13:00:13 -0800277} __packed;
278
David Spinadel0cedacc2012-03-10 13:00:12 -0800279struct iwl_firmware_pieces {
280 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
Johannes Berg15854ef2012-03-05 11:24:50 -0800281
282 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
283 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
284};
285
David Spinadel0cedacc2012-03-10 13:00:12 -0800286/*
287 * These functions are just to extract uCode section data from the pieces
288 * structure.
289 */
290static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
291 enum iwl_ucode_type type,
292 int sec)
293{
294 return &pieces->img[type].sec[sec];
295}
296
297static void set_sec_data(struct iwl_firmware_pieces *pieces,
298 enum iwl_ucode_type type,
299 int sec,
300 const void *data)
301{
302 pieces->img[type].sec[sec].data = data;
303}
304
305static void set_sec_size(struct iwl_firmware_pieces *pieces,
306 enum iwl_ucode_type type,
307 int sec,
308 size_t size)
309{
310 pieces->img[type].sec[sec].size = size;
311}
312
313static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
314 enum iwl_ucode_type type,
315 int sec)
316{
317 return pieces->img[type].sec[sec].size;
318}
319
320static void set_sec_offset(struct iwl_firmware_pieces *pieces,
321 enum iwl_ucode_type type,
322 int sec,
323 u32 offset)
324{
325 pieces->img[type].sec[sec].offset = offset;
326}
327
Max Stepanove36e5432013-08-27 19:56:13 +0300328static int iwl_store_cscheme(struct iwl_fw *fw, const u8 *data, const u32 len)
329{
330 int i, j;
331 struct iwl_fw_cscheme_list *l = (struct iwl_fw_cscheme_list *)data;
332 struct iwl_fw_cipher_scheme *fwcs;
333 struct ieee80211_cipher_scheme *cs;
334 u32 cipher;
335
336 if (len < sizeof(*l) ||
337 len < sizeof(l->size) + l->size * sizeof(l->cs[0]))
338 return -EINVAL;
339
340 for (i = 0, j = 0; i < IWL_UCODE_MAX_CS && i < l->size; i++) {
341 fwcs = &l->cs[j];
342 cipher = le32_to_cpu(fwcs->cipher);
343
344 /* we skip schemes with zero cipher suite selector */
345 if (!cipher)
346 continue;
347
348 cs = &fw->cs[j++];
349 cs->cipher = cipher;
350 cs->iftype = BIT(NL80211_IFTYPE_STATION);
351 cs->hdr_len = fwcs->hdr_len;
352 cs->pn_len = fwcs->pn_len;
353 cs->pn_off = fwcs->pn_off;
354 cs->key_idx_off = fwcs->key_idx_off;
355 cs->key_idx_mask = fwcs->key_idx_mask;
356 cs->key_idx_shift = fwcs->key_idx_shift;
357 cs->mic_len = fwcs->mic_len;
358 }
359
360 return 0;
361}
362
David Spinadeled8c8362012-03-10 13:00:13 -0800363/*
364 * Gets uCode section from tlv.
365 */
366static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
367 const void *data, enum iwl_ucode_type type,
368 int size)
369{
370 struct fw_img_parsing *img;
371 struct fw_sec *sec;
372 struct fw_sec_parsing *sec_parse;
373
374 if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
375 return -1;
376
377 sec_parse = (struct fw_sec_parsing *)data;
378
379 img = &pieces->img[type];
380 sec = &img->sec[img->sec_counter];
381
382 sec->offset = le32_to_cpu(sec_parse->offset);
383 sec->data = sec_parse->data;
David Spinadel1176f432012-03-13 14:32:48 +0200384 sec->size = size - sizeof(sec_parse->offset);
David Spinadeled8c8362012-03-10 13:00:13 -0800385
386 ++img->sec_counter;
387
388 return 0;
389}
390
391static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
392{
393 struct iwl_tlv_calib_data *def_calib =
394 (struct iwl_tlv_calib_data *)data;
395 u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
396 if (ucode_type >= IWL_UCODE_TYPE_MAX) {
397 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
398 ucode_type);
399 return -EINVAL;
400 }
Johannes Bergaa2b1772013-01-24 14:12:07 +0100401 drv->fw.default_calib[ucode_type].flow_trigger =
402 def_calib->calib.flow_trigger;
403 drv->fw.default_calib[ucode_type].event_trigger =
404 def_calib->calib.event_trigger;
405
David Spinadeled8c8362012-03-10 13:00:13 -0800406 return 0;
407}
408
Eran Hararya2978b12014-02-20 11:00:01 +0200409static int iwl_set_ucode_api_flags(struct iwl_drv *drv, const u8 *data,
410 struct iwl_ucode_capabilities *capa)
411{
412 const struct iwl_ucode_api *ucode_api = (void *)data;
413 u32 api_index = le32_to_cpu(ucode_api->api_index);
414
415 if (api_index >= IWL_API_ARRAY_SIZE) {
416 IWL_ERR(drv, "api_index larger than supported by driver\n");
417 return -EINVAL;
418 }
419
420 capa->api[api_index] = le32_to_cpu(ucode_api->api_flags);
421
422 return 0;
423}
424
425static int iwl_set_ucode_capabilities(struct iwl_drv *drv, const u8 *data,
426 struct iwl_ucode_capabilities *capa)
427{
428 const struct iwl_ucode_capa *ucode_capa = (void *)data;
429 u32 api_index = le32_to_cpu(ucode_capa->api_index);
430
431 if (api_index >= IWL_CAPABILITIES_ARRAY_SIZE) {
432 IWL_ERR(drv, "api_index larger than supported by driver\n");
433 return -EINVAL;
434 }
435
436 capa->capa[api_index] = le32_to_cpu(ucode_capa->api_capa);
437
438 return 0;
439}
440
Johannes Berg965974a2012-03-06 13:30:38 -0800441static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
David Spinadel0cedacc2012-03-10 13:00:12 -0800442 const struct firmware *ucode_raw,
443 struct iwl_firmware_pieces *pieces)
Johannes Berg15854ef2012-03-05 11:24:50 -0800444{
445 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
446 u32 api_ver, hdr_size, build;
447 char buildstr[25];
448 const u8 *src;
449
Johannes Berg965974a2012-03-06 13:30:38 -0800450 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
451 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800452
453 switch (api_ver) {
454 default:
455 hdr_size = 28;
456 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800457 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800458 return -EINVAL;
459 }
460 build = le32_to_cpu(ucode->u.v2.build);
David Spinadel0cedacc2012-03-10 13:00:12 -0800461 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
462 le32_to_cpu(ucode->u.v2.inst_size));
463 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
464 le32_to_cpu(ucode->u.v2.data_size));
465 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
466 le32_to_cpu(ucode->u.v2.init_size));
467 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
468 le32_to_cpu(ucode->u.v2.init_data_size));
Johannes Berg15854ef2012-03-05 11:24:50 -0800469 src = ucode->u.v2.data;
470 break;
471 case 0:
472 case 1:
473 case 2:
474 hdr_size = 24;
475 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800476 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800477 return -EINVAL;
478 }
479 build = 0;
David Spinadel0cedacc2012-03-10 13:00:12 -0800480 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
481 le32_to_cpu(ucode->u.v1.inst_size));
482 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
483 le32_to_cpu(ucode->u.v1.data_size));
484 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
485 le32_to_cpu(ucode->u.v1.init_size));
486 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
487 le32_to_cpu(ucode->u.v1.init_data_size));
Johannes Berg15854ef2012-03-05 11:24:50 -0800488 src = ucode->u.v1.data;
489 break;
490 }
491
492 if (build)
493 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800494 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800495 ? " (EXP)" : "");
496 else
497 buildstr[0] = '\0';
498
Johannes Berg965974a2012-03-06 13:30:38 -0800499 snprintf(drv->fw.fw_version,
500 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800501 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800502 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
503 IWL_UCODE_MINOR(drv->fw.ucode_ver),
504 IWL_UCODE_API(drv->fw.ucode_ver),
505 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef2012-03-05 11:24:50 -0800506 buildstr);
507
508 /* Verify size of file vs. image size info in file's header */
David Spinadel0cedacc2012-03-10 13:00:12 -0800509
510 if (ucode_raw->size != hdr_size +
511 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
512 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
513 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
514 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800515
Johannes Berg965974a2012-03-06 13:30:38 -0800516 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800517 "uCode file size %d does not match expected size\n",
518 (int)ucode_raw->size);
519 return -EINVAL;
520 }
521
Johannes Berg15854ef2012-03-05 11:24:50 -0800522
David Spinadel0cedacc2012-03-10 13:00:12 -0800523 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
524 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
525 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
526 IWLAGN_RTC_INST_LOWER_BOUND);
527 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
528 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
529 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
530 IWLAGN_RTC_DATA_LOWER_BOUND);
531 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
532 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
533 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
534 IWLAGN_RTC_INST_LOWER_BOUND);
535 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
536 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
537 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
538 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800539 return 0;
540}
541
Johannes Berg965974a2012-03-06 13:30:38 -0800542static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800543 const struct firmware *ucode_raw,
David Spinadel0cedacc2012-03-10 13:00:12 -0800544 struct iwl_firmware_pieces *pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -0800545 struct iwl_ucode_capabilities *capa)
546{
547 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
548 struct iwl_ucode_tlv *tlv;
549 size_t len = ucode_raw->size;
550 const u8 *data;
Johannes Berg15854ef2012-03-05 11:24:50 -0800551 u32 tlv_len;
552 enum iwl_ucode_tlv_type tlv_type;
553 const u8 *tlv_data;
554 char buildstr[25];
555 u32 build;
Eran Hararye2d6f4e2013-10-02 13:53:40 +0300556 int num_of_cpus;
Johannes Berg15854ef2012-03-05 11:24:50 -0800557
558 if (len < sizeof(*ucode)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800559 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800560 return -EINVAL;
561 }
562
563 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800564 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800565 le32_to_cpu(ucode->magic));
566 return -EINVAL;
567 }
568
Johannes Berg965974a2012-03-06 13:30:38 -0800569 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
Emmanuel Grumbach06ddbf52014-06-02 08:34:53 +0300570 memcpy(drv->fw.human_readable, ucode->human_readable,
571 sizeof(drv->fw.human_readable));
Johannes Berg15854ef2012-03-05 11:24:50 -0800572 build = le32_to_cpu(ucode->build);
573
574 if (build)
575 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800576 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800577 ? " (EXP)" : "");
578 else
579 buildstr[0] = '\0';
580
Johannes Berg965974a2012-03-06 13:30:38 -0800581 snprintf(drv->fw.fw_version,
582 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800583 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800584 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
585 IWL_UCODE_MINOR(drv->fw.ucode_ver),
586 IWL_UCODE_API(drv->fw.ucode_ver),
587 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef2012-03-05 11:24:50 -0800588 buildstr);
589
590 data = ucode->data;
591
592 len -= sizeof(*ucode);
593
594 while (len >= sizeof(*tlv)) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800595 len -= sizeof(*tlv);
596 tlv = (void *)data;
597
598 tlv_len = le32_to_cpu(tlv->length);
Johannes Berg0479c192012-03-09 09:16:35 +0100599 tlv_type = le32_to_cpu(tlv->type);
Johannes Berg15854ef2012-03-05 11:24:50 -0800600 tlv_data = tlv->data;
601
602 if (len < tlv_len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800603 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800604 len, tlv_len);
605 return -EINVAL;
606 }
607 len -= ALIGN(tlv_len, 4);
608 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
609
Johannes Berg15854ef2012-03-05 11:24:50 -0800610 switch (tlv_type) {
611 case IWL_UCODE_TLV_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800612 set_sec_data(pieces, IWL_UCODE_REGULAR,
613 IWL_UCODE_SECTION_INST, tlv_data);
614 set_sec_size(pieces, IWL_UCODE_REGULAR,
615 IWL_UCODE_SECTION_INST, tlv_len);
616 set_sec_offset(pieces, IWL_UCODE_REGULAR,
617 IWL_UCODE_SECTION_INST,
618 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800619 break;
620 case IWL_UCODE_TLV_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800621 set_sec_data(pieces, IWL_UCODE_REGULAR,
622 IWL_UCODE_SECTION_DATA, tlv_data);
623 set_sec_size(pieces, IWL_UCODE_REGULAR,
624 IWL_UCODE_SECTION_DATA, tlv_len);
625 set_sec_offset(pieces, IWL_UCODE_REGULAR,
626 IWL_UCODE_SECTION_DATA,
627 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800628 break;
629 case IWL_UCODE_TLV_INIT:
David Spinadel0cedacc2012-03-10 13:00:12 -0800630 set_sec_data(pieces, IWL_UCODE_INIT,
631 IWL_UCODE_SECTION_INST, tlv_data);
632 set_sec_size(pieces, IWL_UCODE_INIT,
633 IWL_UCODE_SECTION_INST, tlv_len);
634 set_sec_offset(pieces, IWL_UCODE_INIT,
635 IWL_UCODE_SECTION_INST,
636 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800637 break;
638 case IWL_UCODE_TLV_INIT_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800639 set_sec_data(pieces, IWL_UCODE_INIT,
640 IWL_UCODE_SECTION_DATA, tlv_data);
641 set_sec_size(pieces, IWL_UCODE_INIT,
642 IWL_UCODE_SECTION_DATA, tlv_len);
643 set_sec_offset(pieces, IWL_UCODE_INIT,
644 IWL_UCODE_SECTION_DATA,
645 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800646 break;
647 case IWL_UCODE_TLV_BOOT:
Johannes Berg965974a2012-03-06 13:30:38 -0800648 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800649 break;
650 case IWL_UCODE_TLV_PROBE_MAX_LEN:
651 if (tlv_len != sizeof(u32))
652 goto invalid_tlv_len;
653 capa->max_probe_length =
654 le32_to_cpup((__le32 *)tlv_data);
655 break;
656 case IWL_UCODE_TLV_PAN:
657 if (tlv_len)
658 goto invalid_tlv_len;
659 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
660 break;
661 case IWL_UCODE_TLV_FLAGS:
662 /* must be at least one u32 */
663 if (tlv_len < sizeof(u32))
664 goto invalid_tlv_len;
665 /* and a proper number of u32s */
666 if (tlv_len % sizeof(u32))
667 goto invalid_tlv_len;
668 /*
669 * This driver only reads the first u32 as
670 * right now no more features are defined,
671 * if that changes then either the driver
672 * will not work with the new firmware, or
673 * it'll not take advantage of new features.
674 */
675 capa->flags = le32_to_cpup((__le32 *)tlv_data);
676 break;
Eran Hararya2978b12014-02-20 11:00:01 +0200677 case IWL_UCODE_TLV_API_CHANGES_SET:
678 if (tlv_len != sizeof(struct iwl_ucode_api))
679 goto invalid_tlv_len;
680 if (iwl_set_ucode_api_flags(drv, tlv_data, capa))
681 goto tlv_error;
682 break;
683 case IWL_UCODE_TLV_ENABLED_CAPABILITIES:
684 if (tlv_len != sizeof(struct iwl_ucode_capa))
685 goto invalid_tlv_len;
686 if (iwl_set_ucode_capabilities(drv, tlv_data, capa))
687 goto tlv_error;
688 break;
Johannes Berg15854ef2012-03-05 11:24:50 -0800689 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
690 if (tlv_len != sizeof(u32))
691 goto invalid_tlv_len;
692 pieces->init_evtlog_ptr =
693 le32_to_cpup((__le32 *)tlv_data);
694 break;
695 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
696 if (tlv_len != sizeof(u32))
697 goto invalid_tlv_len;
698 pieces->init_evtlog_size =
699 le32_to_cpup((__le32 *)tlv_data);
700 break;
701 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
702 if (tlv_len != sizeof(u32))
703 goto invalid_tlv_len;
704 pieces->init_errlog_ptr =
705 le32_to_cpup((__le32 *)tlv_data);
706 break;
707 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
708 if (tlv_len != sizeof(u32))
709 goto invalid_tlv_len;
710 pieces->inst_evtlog_ptr =
711 le32_to_cpup((__le32 *)tlv_data);
712 break;
713 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
714 if (tlv_len != sizeof(u32))
715 goto invalid_tlv_len;
716 pieces->inst_evtlog_size =
717 le32_to_cpup((__le32 *)tlv_data);
718 break;
719 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
720 if (tlv_len != sizeof(u32))
721 goto invalid_tlv_len;
722 pieces->inst_errlog_ptr =
723 le32_to_cpup((__le32 *)tlv_data);
724 break;
725 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
726 if (tlv_len)
727 goto invalid_tlv_len;
Johannes Berg965974a2012-03-06 13:30:38 -0800728 drv->fw.enhance_sensitivity_table = true;
Johannes Berg15854ef2012-03-05 11:24:50 -0800729 break;
730 case IWL_UCODE_TLV_WOWLAN_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800731 set_sec_data(pieces, IWL_UCODE_WOWLAN,
732 IWL_UCODE_SECTION_INST, tlv_data);
733 set_sec_size(pieces, IWL_UCODE_WOWLAN,
734 IWL_UCODE_SECTION_INST, tlv_len);
735 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
736 IWL_UCODE_SECTION_INST,
737 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800738 break;
739 case IWL_UCODE_TLV_WOWLAN_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800740 set_sec_data(pieces, IWL_UCODE_WOWLAN,
741 IWL_UCODE_SECTION_DATA, tlv_data);
742 set_sec_size(pieces, IWL_UCODE_WOWLAN,
743 IWL_UCODE_SECTION_DATA, tlv_len);
744 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
745 IWL_UCODE_SECTION_DATA,
746 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef2012-03-05 11:24:50 -0800747 break;
748 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
749 if (tlv_len != sizeof(u32))
750 goto invalid_tlv_len;
751 capa->standard_phy_calibration_size =
752 le32_to_cpup((__le32 *)tlv_data);
753 break;
David Spinadeled8c8362012-03-10 13:00:13 -0800754 case IWL_UCODE_TLV_SEC_RT:
755 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
756 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800757 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800758 break;
759 case IWL_UCODE_TLV_SEC_INIT:
760 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
761 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800762 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800763 break;
764 case IWL_UCODE_TLV_SEC_WOWLAN:
765 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
766 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800767 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800768 break;
769 case IWL_UCODE_TLV_DEF_CALIB:
770 if (tlv_len != sizeof(struct iwl_tlv_calib_data))
771 goto invalid_tlv_len;
772 if (iwl_set_default_calib(drv, tlv_data))
773 goto tlv_error;
774 break;
775 case IWL_UCODE_TLV_PHY_SKU:
776 if (tlv_len != sizeof(u32))
777 goto invalid_tlv_len;
778 drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
Eran Harary77db0a32014-02-04 14:21:38 +0200779 drv->fw.valid_tx_ant = (drv->fw.phy_config &
780 FW_PHY_CFG_TX_CHAIN) >>
781 FW_PHY_CFG_TX_CHAIN_POS;
782 drv->fw.valid_rx_ant = (drv->fw.phy_config &
783 FW_PHY_CFG_RX_CHAIN) >>
784 FW_PHY_CFG_RX_CHAIN_POS;
David Spinadeled8c8362012-03-10 13:00:13 -0800785 break;
Eran Hararye2d6f4e2013-10-02 13:53:40 +0300786 case IWL_UCODE_TLV_SECURE_SEC_RT:
787 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
788 tlv_len);
789 drv->fw.mvm_fw = true;
790 drv->fw.img[IWL_UCODE_REGULAR].is_secure = true;
791 break;
792 case IWL_UCODE_TLV_SECURE_SEC_INIT:
793 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
794 tlv_len);
795 drv->fw.mvm_fw = true;
796 drv->fw.img[IWL_UCODE_INIT].is_secure = true;
797 break;
798 case IWL_UCODE_TLV_SECURE_SEC_WOWLAN:
799 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
800 tlv_len);
801 drv->fw.mvm_fw = true;
802 drv->fw.img[IWL_UCODE_WOWLAN].is_secure = true;
803 break;
804 case IWL_UCODE_TLV_NUM_OF_CPU:
805 if (tlv_len != sizeof(u32))
806 goto invalid_tlv_len;
807 num_of_cpus =
808 le32_to_cpup((__le32 *)tlv_data);
809
810 if (num_of_cpus == 2) {
811 drv->fw.img[IWL_UCODE_REGULAR].is_dual_cpus =
812 true;
813 drv->fw.img[IWL_UCODE_INIT].is_dual_cpus =
814 true;
815 drv->fw.img[IWL_UCODE_WOWLAN].is_dual_cpus =
816 true;
817 } else if ((num_of_cpus > 2) || (num_of_cpus < 1)) {
818 IWL_ERR(drv, "Driver support upto 2 CPUs\n");
819 return -EINVAL;
820 }
821 break;
Max Stepanove36e5432013-08-27 19:56:13 +0300822 case IWL_UCODE_TLV_CSCHEME:
823 if (iwl_store_cscheme(&drv->fw, tlv_data, tlv_len))
824 goto invalid_tlv_len;
825 break;
David Spinadel762533b2014-06-05 11:20:43 +0300826 case IWL_UCODE_TLV_N_SCAN_CHANNELS:
827 if (tlv_len != sizeof(u32))
828 goto invalid_tlv_len;
829 capa->n_scan_channels =
830 le32_to_cpup((__le32 *)tlv_data);
831 break;
Johannes Berg15854ef2012-03-05 11:24:50 -0800832 default:
Johannes Berg965974a2012-03-06 13:30:38 -0800833 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
Johannes Berg15854ef2012-03-05 11:24:50 -0800834 break;
835 }
836 }
837
838 if (len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800839 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
840 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800841 return -EINVAL;
842 }
843
844 return 0;
845
846 invalid_tlv_len:
Johannes Berg965974a2012-03-06 13:30:38 -0800847 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
David Spinadeled8c8362012-03-10 13:00:13 -0800848 tlv_error:
Johannes Berg965974a2012-03-06 13:30:38 -0800849 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800850
851 return -EINVAL;
852}
853
Johannes Berg75813bd2012-05-16 22:49:49 +0200854static int iwl_alloc_ucode(struct iwl_drv *drv,
855 struct iwl_firmware_pieces *pieces,
856 enum iwl_ucode_type type)
David Spinadel6dfa8d02012-03-10 13:00:14 -0800857{
858 int i;
859 for (i = 0;
860 i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
861 i++)
862 if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
Johannes Berg75813bd2012-05-16 22:49:49 +0200863 get_sec(pieces, type, i)))
864 return -ENOMEM;
David Spinadel6dfa8d02012-03-10 13:00:14 -0800865 return 0;
866}
867
868static int validate_sec_sizes(struct iwl_drv *drv,
869 struct iwl_firmware_pieces *pieces,
870 const struct iwl_cfg *cfg)
871{
872 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
873 get_sec_size(pieces, IWL_UCODE_REGULAR,
874 IWL_UCODE_SECTION_INST));
875 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
876 get_sec_size(pieces, IWL_UCODE_REGULAR,
877 IWL_UCODE_SECTION_DATA));
878 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
879 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
880 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
881 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
882
883 /* Verify that uCode images will fit in card's SRAM. */
884 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
885 cfg->max_inst_size) {
886 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
887 get_sec_size(pieces, IWL_UCODE_REGULAR,
888 IWL_UCODE_SECTION_INST));
889 return -1;
890 }
891
892 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
893 cfg->max_data_size) {
894 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
895 get_sec_size(pieces, IWL_UCODE_REGULAR,
896 IWL_UCODE_SECTION_DATA));
897 return -1;
898 }
899
900 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
901 cfg->max_inst_size) {
902 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
903 get_sec_size(pieces, IWL_UCODE_INIT,
904 IWL_UCODE_SECTION_INST));
905 return -1;
906 }
907
908 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
909 cfg->max_data_size) {
910 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
911 get_sec_size(pieces, IWL_UCODE_REGULAR,
912 IWL_UCODE_SECTION_DATA));
913 return -1;
914 }
915 return 0;
916}
917
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -0700918static struct iwl_op_mode *
919_iwl_op_mode_start(struct iwl_drv *drv, struct iwlwifi_opmode_table *op)
920{
921 const struct iwl_op_mode_ops *ops = op->ops;
922 struct dentry *dbgfs_dir = NULL;
923 struct iwl_op_mode *op_mode = NULL;
924
925#ifdef CONFIG_IWLWIFI_DEBUGFS
926 drv->dbgfs_op_mode = debugfs_create_dir(op->name,
927 drv->dbgfs_drv);
928 if (!drv->dbgfs_op_mode) {
929 IWL_ERR(drv,
930 "failed to create opmode debugfs directory\n");
931 return op_mode;
932 }
933 dbgfs_dir = drv->dbgfs_op_mode;
934#endif
935
936 op_mode = ops->start(drv->trans, drv->cfg, &drv->fw, dbgfs_dir);
937
938#ifdef CONFIG_IWLWIFI_DEBUGFS
939 if (!op_mode) {
940 debugfs_remove_recursive(drv->dbgfs_op_mode);
941 drv->dbgfs_op_mode = NULL;
942 }
943#endif
944
945 return op_mode;
946}
947
948static void _iwl_op_mode_stop(struct iwl_drv *drv)
949{
950 /* op_mode can be NULL if its start failed */
951 if (drv->op_mode) {
952 iwl_op_mode_stop(drv->op_mode);
953 drv->op_mode = NULL;
954
955#ifdef CONFIG_IWLWIFI_DEBUGFS
956 debugfs_remove_recursive(drv->dbgfs_op_mode);
957 drv->dbgfs_op_mode = NULL;
958#endif
959 }
960}
961
Johannes Berg15854ef2012-03-05 11:24:50 -0800962/**
Emmanuel Grumbach1c8e11e2012-07-16 12:31:28 +0300963 * iwl_req_fw_callback - callback when firmware was loaded
Johannes Berg15854ef2012-03-05 11:24:50 -0800964 *
965 * If loaded successfully, copies the firmware into buffers
966 * for the card to fetch (via DMA).
967 */
Emmanuel Grumbach1c8e11e2012-07-16 12:31:28 +0300968static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context)
Johannes Berg15854ef2012-03-05 11:24:50 -0800969{
Johannes Berg965974a2012-03-06 13:30:38 -0800970 struct iwl_drv *drv = context;
Johannes Berg965974a2012-03-06 13:30:38 -0800971 struct iwl_fw *fw = &drv->fw;
Johannes Berg15854ef2012-03-05 11:24:50 -0800972 struct iwl_ucode_header *ucode;
Don Frycc5f7e32012-05-16 22:54:27 +0200973 struct iwlwifi_opmode_table *op;
Johannes Berg15854ef2012-03-05 11:24:50 -0800974 int err;
David Spinadel0cedacc2012-03-10 13:00:12 -0800975 struct iwl_firmware_pieces pieces;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200976 const unsigned int api_max = drv->cfg->ucode_api_max;
977 unsigned int api_ok = drv->cfg->ucode_api_ok;
978 const unsigned int api_min = drv->cfg->ucode_api_min;
Johannes Berg15854ef2012-03-05 11:24:50 -0800979 u32 api_ver;
David Spinadel6dfa8d02012-03-10 13:00:14 -0800980 int i;
Johannes Bergd4b10482012-06-12 19:50:43 +0200981 bool load_module = false;
Johannes Berg15854ef2012-03-05 11:24:50 -0800982
Luciano Coelho66140ad2013-08-12 19:30:21 +0300983 fw->ucode_capa.max_probe_length = IWL_DEFAULT_MAX_PROBE_LENGTH;
Johannes Berg15854ef2012-03-05 11:24:50 -0800984 fw->ucode_capa.standard_phy_calibration_size =
985 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
David Spinadel762533b2014-06-05 11:20:43 +0300986 fw->ucode_capa.n_scan_channels = IWL_DEFAULT_SCAN_CHANNELS;
Johannes Berg15854ef2012-03-05 11:24:50 -0800987
988 if (!api_ok)
989 api_ok = api_max;
990
991 memset(&pieces, 0, sizeof(pieces));
992
993 if (!ucode_raw) {
Johannes Berg965974a2012-03-06 13:30:38 -0800994 if (drv->fw_index <= api_ok)
995 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800996 "request for firmware file '%s' failed.\n",
Johannes Berg965974a2012-03-06 13:30:38 -0800997 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800998 goto try_again;
999 }
1000
Johannes Berg965974a2012-03-06 13:30:38 -08001001 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
1002 drv->firmware_name, ucode_raw->size);
Johannes Berg15854ef2012-03-05 11:24:50 -08001003
1004 /* Make sure that we got at least the API version number */
1005 if (ucode_raw->size < 4) {
Johannes Berg965974a2012-03-06 13:30:38 -08001006 IWL_ERR(drv, "File size way too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -08001007 goto try_again;
1008 }
1009
1010 /* Data from ucode file: header followed by uCode images */
1011 ucode = (struct iwl_ucode_header *)ucode_raw->data;
1012
1013 if (ucode->ver)
Johannes Berg965974a2012-03-06 13:30:38 -08001014 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
Johannes Berg15854ef2012-03-05 11:24:50 -08001015 else
Johannes Berg965974a2012-03-06 13:30:38 -08001016 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -08001017 &fw->ucode_capa);
1018
1019 if (err)
1020 goto try_again;
1021
Johannes Berg965974a2012-03-06 13:30:38 -08001022 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -08001023
1024 /*
1025 * api_ver should match the api version forming part of the
1026 * firmware filename ... but we don't check for that and only rely
1027 * on the API version read from firmware header from here on forward
1028 */
1029 /* no api version check required for experimental uCode */
Johannes Berg965974a2012-03-06 13:30:38 -08001030 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -08001031 if (api_ver < api_min || api_ver > api_max) {
Johannes Berg965974a2012-03-06 13:30:38 -08001032 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -08001033 "Driver unable to support your firmware API. "
1034 "Driver supports v%u, firmware is v%u.\n",
1035 api_max, api_ver);
1036 goto try_again;
1037 }
1038
1039 if (api_ver < api_ok) {
1040 if (api_ok != api_max)
Johannes Berg965974a2012-03-06 13:30:38 -08001041 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -08001042 "expected v%u through v%u, got v%u.\n",
1043 api_ok, api_max, api_ver);
1044 else
Johannes Berg965974a2012-03-06 13:30:38 -08001045 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -08001046 "expected v%u, got v%u.\n",
1047 api_max, api_ver);
Johannes Berg965974a2012-03-06 13:30:38 -08001048 IWL_ERR(drv, "New firmware can be obtained from "
Johannes Berg15854ef2012-03-05 11:24:50 -08001049 "http://www.intellinuxwireless.org/.\n");
1050 }
1051 }
1052
Johannes Berg15854ef2012-03-05 11:24:50 -08001053 /*
David Spinadel4db2c9a2012-03-10 13:00:15 -08001054 * In mvm uCode there is no difference between data and instructions
1055 * sections.
1056 */
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +02001057 if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
Johannes Berg15854ef2012-03-05 11:24:50 -08001058 goto try_again;
Johannes Berg15854ef2012-03-05 11:24:50 -08001059
1060 /* Allocate ucode buffers for card's bus-master loading ... */
1061
1062 /* Runtime instructions and 2 copies of data:
1063 * 1) unmodified from disk
1064 * 2) backup cache for save/restore during power-downs */
David Spinadel6dfa8d02012-03-10 13:00:14 -08001065 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
Johannes Berg75813bd2012-05-16 22:49:49 +02001066 if (iwl_alloc_ucode(drv, &pieces, i))
1067 goto out_free_fw;
Johannes Berg15854ef2012-03-05 11:24:50 -08001068
1069 /* Now that we can no longer fail, copy information */
1070
1071 /*
1072 * The (size - 16) / 12 formula is based on the information recorded
1073 * for each event, which is of mode 1 (including timestamp) for all
1074 * new microcodes that include this information.
1075 */
Johannes Berg0692fe42012-03-06 13:30:37 -08001076 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -08001077 if (pieces.init_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -08001078 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -08001079 else
Johannes Berg0692fe42012-03-06 13:30:37 -08001080 fw->init_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +02001081 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -08001082 fw->init_errlog_ptr = pieces.init_errlog_ptr;
1083 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -08001084 if (pieces.inst_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -08001085 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -08001086 else
Johannes Berg0692fe42012-03-06 13:30:37 -08001087 fw->inst_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +02001088 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -08001089 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -08001090
1091 /*
1092 * figure out the offset of chain noise reset and gain commands
1093 * base on the size of standard phy calibration commands table size
1094 */
1095 if (fw->ucode_capa.standard_phy_calibration_size >
1096 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
1097 fw->ucode_capa.standard_phy_calibration_size =
1098 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1099
1100 /* We have our copies now, allow OS release its copies */
1101 release_firmware(ucode_raw);
Johannes Berg15854ef2012-03-05 11:24:50 -08001102
Johannes Bergff1ffb82012-06-06 09:42:57 +02001103 mutex_lock(&iwlwifi_opmode_table_mtx);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001104 if (fw->mvm_fw)
1105 op = &iwlwifi_opmode_table[MVM_OP_MODE];
1106 else
1107 op = &iwlwifi_opmode_table[DVM_OP_MODE];
Johannes Berg15854ef2012-03-05 11:24:50 -08001108
Johannes Berg2b2719c2013-03-26 11:41:51 +01001109 IWL_INFO(drv, "loaded firmware version %s op_mode %s\n",
1110 drv->fw.fw_version, op->name);
1111
Don Frycc5f7e32012-05-16 22:54:27 +02001112 /* add this device to the list of devices using this op_mode */
1113 list_add_tail(&drv->list, &op->drv);
1114
1115 if (op->ops) {
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001116 drv->op_mode = _iwl_op_mode_start(drv, op);
John W. Linville7c9c46c2012-06-06 14:40:06 -04001117
Dan Carpenterdaf67ce2012-06-14 21:35:26 +03001118 if (!drv->op_mode) {
1119 mutex_unlock(&iwlwifi_opmode_table_mtx);
John W. Linville7c9c46c2012-06-06 14:40:06 -04001120 goto out_unbind;
Dan Carpenterdaf67ce2012-06-14 21:35:26 +03001121 }
Don Frycc5f7e32012-05-16 22:54:27 +02001122 } else {
Johannes Bergd4b10482012-06-12 19:50:43 +02001123 load_module = true;
Don Frycc5f7e32012-05-16 22:54:27 +02001124 }
Johannes Bergff1ffb82012-06-06 09:42:57 +02001125 mutex_unlock(&iwlwifi_opmode_table_mtx);
Johannes Berg15854ef2012-03-05 11:24:50 -08001126
Johannes Bergf69a23b2012-06-05 19:56:06 +02001127 /*
1128 * Complete the firmware request last so that
1129 * a driver unbind (stop) doesn't run while we
1130 * are doing the start() above.
1131 */
1132 complete(&drv->request_firmware_complete);
Johannes Bergd4b10482012-06-12 19:50:43 +02001133
1134 /*
1135 * Load the module last so we don't block anything
1136 * else from proceeding if the module fails to load
1137 * or hangs loading.
1138 */
Johannes Berg1618b2b2013-04-04 10:35:23 +02001139 if (load_module) {
1140 err = request_module("%s", op->name);
Johannes Berg8edf3fd2013-05-28 21:32:47 +02001141#ifdef CONFIG_IWLWIFI_OPMODE_MODULAR
Johannes Berg1618b2b2013-04-04 10:35:23 +02001142 if (err)
1143 IWL_ERR(drv,
1144 "failed to load module %s (error %d), is dynamic loading enabled?\n",
1145 op->name, err);
Johannes Berg8edf3fd2013-05-28 21:32:47 +02001146#endif
Johannes Berg1618b2b2013-04-04 10:35:23 +02001147 }
Johannes Berg15854ef2012-03-05 11:24:50 -08001148 return;
1149
1150 try_again:
1151 /* try next, if any */
1152 release_firmware(ucode_raw);
Johannes Berg965974a2012-03-06 13:30:38 -08001153 if (iwl_request_firmware(drv, false))
Johannes Berg15854ef2012-03-05 11:24:50 -08001154 goto out_unbind;
1155 return;
1156
Johannes Berg75813bd2012-05-16 22:49:49 +02001157 out_free_fw:
Johannes Berg965974a2012-03-06 13:30:38 -08001158 IWL_ERR(drv, "failed to allocate pci memory\n");
1159 iwl_dealloc_ucode(drv);
Johannes Berg15854ef2012-03-05 11:24:50 -08001160 release_firmware(ucode_raw);
1161 out_unbind:
Johannes Berg965974a2012-03-06 13:30:38 -08001162 complete(&drv->request_firmware_complete);
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +02001163 device_release_driver(drv->trans->dev);
Johannes Berg15854ef2012-03-05 11:24:50 -08001164}
1165
Emmanuel Grumbach87ce05a2012-03-26 09:03:18 -07001166struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -07001167 const struct iwl_cfg *cfg)
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001168{
Johannes Berg965974a2012-03-06 13:30:38 -08001169 struct iwl_drv *drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001170 int ret;
1171
Johannes Berg965974a2012-03-06 13:30:38 -08001172 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
Luciano Coelhoeafe25e2013-08-13 10:34:55 +03001173 if (!drv) {
1174 ret = -ENOMEM;
1175 goto err;
1176 }
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +03001177
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -07001178 drv->trans = trans;
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +02001179 drv->dev = trans->dev;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +02001180 drv->cfg = cfg;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001181
Johannes Berg965974a2012-03-06 13:30:38 -08001182 init_completion(&drv->request_firmware_complete);
Johannes Bergff1ffb82012-06-06 09:42:57 +02001183 INIT_LIST_HEAD(&drv->list);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001184
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001185#ifdef CONFIG_IWLWIFI_DEBUGFS
1186 /* Create the device debugfs entries. */
1187 drv->dbgfs_drv = debugfs_create_dir(dev_name(trans->dev),
1188 iwl_dbgfs_root);
1189
1190 if (!drv->dbgfs_drv) {
1191 IWL_ERR(drv, "failed to create debugfs directory\n");
Emmanuel Grumbach59f547a2012-11-28 16:42:09 +02001192 ret = -ENOMEM;
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001193 goto err_free_drv;
1194 }
1195
1196 /* Create transport layer debugfs dir */
1197 drv->trans->dbgfs_dir = debugfs_create_dir("trans", drv->dbgfs_drv);
1198
1199 if (!drv->trans->dbgfs_dir) {
1200 IWL_ERR(drv, "failed to create transport debugfs directory\n");
Emmanuel Grumbach59f547a2012-11-28 16:42:09 +02001201 ret = -ENOMEM;
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001202 goto err_free_dbgfs;
1203 }
1204#endif
1205
Johannes Berg965974a2012-03-06 13:30:38 -08001206 ret = iwl_request_firmware(drv, true);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001207 if (ret) {
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +03001208 IWL_ERR(trans, "Couldn't request the fw\n");
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001209 goto err_fw;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001210 }
1211
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -07001212 return drv;
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001213
1214err_fw:
1215#ifdef CONFIG_IWLWIFI_DEBUGFS
1216err_free_dbgfs:
1217 debugfs_remove_recursive(drv->dbgfs_drv);
1218err_free_drv:
1219#endif
1220 kfree(drv);
Luciano Coelhoeafe25e2013-08-13 10:34:55 +03001221err:
Emmanuel Grumbach59f547a2012-11-28 16:42:09 +02001222 return ERR_PTR(ret);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001223}
1224
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -07001225void iwl_drv_stop(struct iwl_drv *drv)
Emmanuel Grumbach07590f02012-02-07 14:27:31 +02001226{
Johannes Berg965974a2012-03-06 13:30:38 -08001227 wait_for_completion(&drv->request_firmware_complete);
Johannes Berg2e7eb112012-03-05 11:24:51 -08001228
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001229 _iwl_op_mode_stop(drv);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +02001230
Johannes Berg965974a2012-03-06 13:30:38 -08001231 iwl_dealloc_ucode(drv);
Johannes Berg7db5b982012-03-05 11:24:48 -08001232
Johannes Bergff1ffb82012-06-06 09:42:57 +02001233 mutex_lock(&iwlwifi_opmode_table_mtx);
1234 /*
1235 * List is empty (this item wasn't added)
1236 * when firmware loading failed -- in that
1237 * case we can't remove it from any list.
1238 */
1239 if (!list_empty(&drv->list))
1240 list_del(&drv->list);
1241 mutex_unlock(&iwlwifi_opmode_table_mtx);
1242
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001243#ifdef CONFIG_IWLWIFI_DEBUGFS
1244 debugfs_remove_recursive(drv->dbgfs_drv);
1245#endif
1246
Johannes Berg965974a2012-03-06 13:30:38 -08001247 kfree(drv);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +02001248}
Johannes Berg65de7e82012-04-17 07:36:30 -07001249
1250
1251/* shared module parameters */
1252struct iwl_mod_params iwlwifi_mod_params = {
Emmanuel Grumbach490953a2013-03-04 08:53:07 +02001253 .restart_fw = true,
Johannes Berg65de7e82012-04-17 07:36:30 -07001254 .bt_coex_active = true,
1255 .power_level = IWL_POWER_INDEX_1,
Emmanuel Grumbach020c8762012-06-24 15:51:59 +03001256 .wd_disable = true,
Johannes Bergd6ca18de2014-09-01 11:28:11 +02001257#ifndef CONFIG_IWLWIFI_UAPSD
1258 .uapsd_disable = true,
1259#endif /* CONFIG_IWLWIFI_UAPSD */
Johannes Berg65de7e82012-04-17 07:36:30 -07001260 /* the rest are 0 by default */
1261};
Johannes Berg48e29342013-03-01 00:13:33 +01001262IWL_EXPORT_SYMBOL(iwlwifi_mod_params);
Don Frycc5f7e32012-05-16 22:54:27 +02001263
1264int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
1265{
1266 int i;
1267 struct iwl_drv *drv;
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001268 struct iwlwifi_opmode_table *op;
Don Frycc5f7e32012-05-16 22:54:27 +02001269
Johannes Bergff1ffb82012-06-06 09:42:57 +02001270 mutex_lock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001271 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001272 op = &iwlwifi_opmode_table[i];
1273 if (strcmp(op->name, name))
Don Frycc5f7e32012-05-16 22:54:27 +02001274 continue;
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001275 op->ops = ops;
1276 /* TODO: need to handle exceptional case */
1277 list_for_each_entry(drv, &op->drv, list)
1278 drv->op_mode = _iwl_op_mode_start(drv, op);
1279
Johannes Bergff1ffb82012-06-06 09:42:57 +02001280 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001281 return 0;
1282 }
Johannes Bergff1ffb82012-06-06 09:42:57 +02001283 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001284 return -EIO;
1285}
Johannes Berg48e29342013-03-01 00:13:33 +01001286IWL_EXPORT_SYMBOL(iwl_opmode_register);
Don Frycc5f7e32012-05-16 22:54:27 +02001287
1288void iwl_opmode_deregister(const char *name)
1289{
1290 int i;
1291 struct iwl_drv *drv;
1292
Johannes Bergff1ffb82012-06-06 09:42:57 +02001293 mutex_lock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001294 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1295 if (strcmp(iwlwifi_opmode_table[i].name, name))
1296 continue;
1297 iwlwifi_opmode_table[i].ops = NULL;
1298
1299 /* call the stop routine for all devices */
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001300 list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
1301 _iwl_op_mode_stop(drv);
1302
Johannes Bergff1ffb82012-06-06 09:42:57 +02001303 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001304 return;
1305 }
Johannes Bergff1ffb82012-06-06 09:42:57 +02001306 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001307}
Johannes Berg48e29342013-03-01 00:13:33 +01001308IWL_EXPORT_SYMBOL(iwl_opmode_deregister);
Don Frycc5f7e32012-05-16 22:54:27 +02001309
1310static int __init iwl_drv_init(void)
1311{
1312 int i;
1313
Johannes Bergff1ffb82012-06-06 09:42:57 +02001314 mutex_init(&iwlwifi_opmode_table_mtx);
1315
Don Frycc5f7e32012-05-16 22:54:27 +02001316 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
1317 INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
1318
1319 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
1320 pr_info(DRV_COPYRIGHT "\n");
1321
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001322#ifdef CONFIG_IWLWIFI_DEBUGFS
1323 /* Create the root of iwlwifi debugfs subsystem. */
1324 iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL);
1325
1326 if (!iwl_dbgfs_root)
1327 return -EFAULT;
1328#endif
1329
Don Frycc5f7e32012-05-16 22:54:27 +02001330 return iwl_pci_register_driver();
1331}
1332module_init(iwl_drv_init);
1333
1334static void __exit iwl_drv_exit(void)
1335{
1336 iwl_pci_unregister_driver();
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001337
1338#ifdef CONFIG_IWLWIFI_DEBUGFS
1339 debugfs_remove_recursive(iwl_dbgfs_root);
1340#endif
Don Frycc5f7e32012-05-16 22:54:27 +02001341}
1342module_exit(iwl_drv_exit);
Johannes Berg65de7e82012-04-17 07:36:30 -07001343
1344#ifdef CONFIG_IWLWIFI_DEBUG
1345module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
1346 S_IRUGO | S_IWUSR);
1347MODULE_PARM_DESC(debug, "debug output mask");
1348#endif
1349
1350module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
1351MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
1352module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
1353MODULE_PARM_DESC(11n_disable,
Emmanuel Grumbach205e2212014-02-12 15:15:05 +02001354 "disable 11n functionality, bitmap: 1: full, 2: disable agg TX, 4: disable agg RX, 8 enable agg TX");
Johannes Berg65de7e82012-04-17 07:36:30 -07001355module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
1356 int, S_IRUGO);
Emmanuel Grumbachaed7d9a2013-02-20 11:33:00 +02001357MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size (default 0)");
Emmanuel Grumbach490953a2013-03-04 08:53:07 +02001358module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, bool, S_IRUGO);
1359MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)");
Johannes Berg65de7e82012-04-17 07:36:30 -07001360
1361module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
1362 int, S_IRUGO);
1363MODULE_PARM_DESC(antenna_coupling,
1364 "specify antenna coupling in dB (defualt: 0 dB)");
1365
Johannes Berg65de7e82012-04-17 07:36:30 -07001366module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
1367MODULE_PARM_DESC(wd_disable,
Eliad Peller82b715c2014-02-11 16:54:42 +02001368 "Disable stuck queue watchdog timer 0=system default, 1=disable (default: 1)");
Johannes Berg65de7e82012-04-17 07:36:30 -07001369
Eran Harary12147552013-05-09 08:07:59 +03001370module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, S_IRUGO);
1371MODULE_PARM_DESC(nvm_file, "NVM file name");
1372
Matt Chen1504f482014-04-24 18:43:18 +08001373module_param_named(uapsd_disable, iwlwifi_mod_params.uapsd_disable,
1374 bool, S_IRUGO);
Johannes Bergd6ca18de2014-09-01 11:28:11 +02001375#ifdef CONFIG_IWLWIFI_UAPSD
Matt Chen1504f482014-04-24 18:43:18 +08001376MODULE_PARM_DESC(uapsd_disable, "disable U-APSD functionality (default: N)");
Johannes Bergd6ca18de2014-09-01 11:28:11 +02001377#else
1378MODULE_PARM_DESC(uapsd_disable, "disable U-APSD functionality (default: Y)");
1379#endif
Matt Chen1504f482014-04-24 18:43:18 +08001380
Johannes Berg65de7e82012-04-17 07:36:30 -07001381/*
1382 * set bt_coex_active to true, uCode will do kill/defer
1383 * every time the priority line is asserted (BT is sending signals on the
1384 * priority line in the PCIx).
1385 * set bt_coex_active to false, uCode will ignore the BT activity and
1386 * perform the normal operation
1387 *
1388 * User might experience transmit issue on some platform due to WiFi/BT
1389 * co-exist problem. The possible behaviors are:
1390 * Able to scan and finding all the available AP
1391 * Not able to associate with any AP
1392 * On those platforms, WiFi communication can be restored by set
1393 * "bt_coex_active" module parameter to "false"
1394 *
1395 * default: bt_coex_active = true (BT_COEX_ENABLE)
1396 */
1397module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
1398 bool, S_IRUGO);
1399MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
1400
1401module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
1402MODULE_PARM_DESC(led_mode, "0=system default, "
1403 "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
1404
1405module_param_named(power_save, iwlwifi_mod_params.power_save,
1406 bool, S_IRUGO);
1407MODULE_PARM_DESC(power_save,
1408 "enable WiFi power management (default: disable)");
1409
1410module_param_named(power_level, iwlwifi_mod_params.power_level,
1411 int, S_IRUGO);
1412MODULE_PARM_DESC(power_level,
1413 "default power save level (range from 1 - 5, default: 1)");
Emmanuel Grumbachc2d20202014-06-01 08:05:52 +03001414
1415module_param_named(fw_monitor, iwlwifi_mod_params.fw_monitor, bool, S_IRUGO);
Emmanuel Grumbach6d6a4752014-06-26 09:13:26 +03001416MODULE_PARM_DESC(fw_monitor,
Emmanuel Grumbachc2d20202014-06-01 08:05:52 +03001417 "firmware monitor - to debug FW (default: false - needs lots of memory)");