blob: bf584d3fe8ec0ec2c55dc63fb097a3f8f2a31332 [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 Berg15854ef92012-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 Berg965974a62012-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 Berg965974a62012-03-06 13:30:38 -0800112 * @fw: the iwl_fw structure
Johannes Berg965974a62012-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 Berg965974a62012-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 Berg965974a62012-03-06 13:30:38 -0800123 struct iwl_fw fw;
124
Johannes Berg965974a62012-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 Berg965974a62012-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 Berg965974a62012-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 Berg965974a62012-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 Berg965974a62012-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 Berg965974a62012-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 Berg965974a62012-03-06 13:30:38 -0800170static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
Johannes Berg15854ef92012-03-05 11:24:50 -0800171{
Johannes Berg83f84d72012-09-10 11:50:18 +0200172 vfree(desc->data);
173 desc->data = NULL;
Johannes Berg15854ef92012-03-05 11:24:50 -0800174 desc->len = 0;
175}
176
Johannes Berg965974a62012-03-06 13:30:38 -0800177static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
Johannes Berg15854ef92012-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 Berg15854ef92012-03-05 11:24:50 -0800182}
183
Johannes Berg965974a62012-03-06 13:30:38 -0800184static void iwl_dealloc_ucode(struct iwl_drv *drv)
Johannes Berg15854ef92012-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 Berg15854ef92012-03-05 11:24:50 -0800189}
190
Johannes Berg965974a62012-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 Berg15854ef92012-03-05 11:24:50 -0800193{
Johannes Berg83f84d72012-09-10 11:50:18 +0200194 void *data;
Johannes Berg15854ef92012-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 Berg15854ef92012-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 Berg15854ef92012-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 Berg15854ef92012-03-05 11:24:50 -0800215
216#define UCODE_EXPERIMENTAL_INDEX 100
217#define UCODE_EXPERIMENTAL_TAG "exp"
218
Johannes Berg965974a62012-03-06 13:30:38 -0800219static int iwl_request_firmware(struct iwl_drv *drv, bool first)
Johannes Berg15854ef92012-03-05 11:24:50 -0800220{
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200221 const char *name_pre = drv->cfg->fw_name_pre;
Johannes Berg15854ef92012-03-05 11:24:50 -0800222 char tag[8];
223
224 if (first) {
225#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
Johannes Berg965974a62012-03-06 13:30:38 -0800226 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
Johannes Berg15854ef92012-03-05 11:24:50 -0800227 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
Johannes Berg965974a62012-03-06 13:30:38 -0800228 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800229#endif
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200230 drv->fw_index = drv->cfg->ucode_api_max;
Johannes Berg965974a62012-03-06 13:30:38 -0800231 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef92012-03-05 11:24:50 -0800232 } else {
Johannes Berg965974a62012-03-06 13:30:38 -0800233 drv->fw_index--;
234 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef92012-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 Berg965974a62012-03-06 13:30:38 -0800238 IWL_ERR(drv, "no suitable firmware found!\n");
Johannes Berg15854ef92012-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 Berg15854ef92012-03-05 11:24:50 -0800244
Johannes Berg965974a62012-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 Berg15854ef92012-03-05 11:24:50 -0800247 ? "EXPERIMENTAL " : "",
Johannes Berg965974a62012-03-06 13:30:38 -0800248 drv->firmware_name);
Johannes Berg15854ef92012-03-05 11:24:50 -0800249
Johannes Berg965974a62012-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 Berg15854ef92012-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 Berg15854ef92012-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 Berg965974a62012-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 Berg15854ef92012-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 Berg965974a62012-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 Berg15854ef92012-03-05 11:24:50 -0800452
453 switch (api_ver) {
454 default:
455 hdr_size = 28;
456 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a62012-03-06 13:30:38 -0800457 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef92012-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 Berg15854ef92012-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 Berg965974a62012-03-06 13:30:38 -0800476 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef92012-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 Berg15854ef92012-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 Berg965974a62012-03-06 13:30:38 -0800494 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef92012-03-05 11:24:50 -0800495 ? " (EXP)" : "");
496 else
497 buildstr[0] = '\0';
498
Johannes Berg965974a62012-03-06 13:30:38 -0800499 snprintf(drv->fw.fw_version,
500 sizeof(drv->fw.fw_version),
Johannes Berg15854ef92012-03-05 11:24:50 -0800501 "%u.%u.%u.%u%s",
Johannes Berg965974a62012-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 Berg15854ef92012-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 Berg15854ef92012-03-05 11:24:50 -0800515
Johannes Berg965974a62012-03-06 13:30:38 -0800516 IWL_ERR(drv,
Johannes Berg15854ef92012-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 Berg15854ef92012-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 Berg15854ef92012-03-05 11:24:50 -0800539 return 0;
540}
541
Johannes Berg965974a62012-03-06 13:30:38 -0800542static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
Johannes Berg15854ef92012-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 Berg15854ef92012-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 Berg15854ef92012-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 Berg15854ef92012-03-05 11:24:50 -0800557
558 if (len < sizeof(*ucode)) {
Johannes Berg965974a62012-03-06 13:30:38 -0800559 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
Johannes Berg15854ef92012-03-05 11:24:50 -0800560 return -EINVAL;
561 }
562
563 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
Johannes Berg965974a62012-03-06 13:30:38 -0800564 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
Johannes Berg15854ef92012-03-05 11:24:50 -0800565 le32_to_cpu(ucode->magic));
566 return -EINVAL;
567 }
568
Johannes Berg965974a62012-03-06 13:30:38 -0800569 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
Johannes Berg15854ef92012-03-05 11:24:50 -0800570 build = le32_to_cpu(ucode->build);
571
572 if (build)
573 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a62012-03-06 13:30:38 -0800574 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef92012-03-05 11:24:50 -0800575 ? " (EXP)" : "");
576 else
577 buildstr[0] = '\0';
578
Johannes Berg965974a62012-03-06 13:30:38 -0800579 snprintf(drv->fw.fw_version,
580 sizeof(drv->fw.fw_version),
Johannes Berg15854ef92012-03-05 11:24:50 -0800581 "%u.%u.%u.%u%s",
Johannes Berg965974a62012-03-06 13:30:38 -0800582 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
583 IWL_UCODE_MINOR(drv->fw.ucode_ver),
584 IWL_UCODE_API(drv->fw.ucode_ver),
585 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef92012-03-05 11:24:50 -0800586 buildstr);
587
588 data = ucode->data;
589
590 len -= sizeof(*ucode);
591
592 while (len >= sizeof(*tlv)) {
Johannes Berg15854ef92012-03-05 11:24:50 -0800593 len -= sizeof(*tlv);
594 tlv = (void *)data;
595
596 tlv_len = le32_to_cpu(tlv->length);
Johannes Berg0479c192012-03-09 09:16:35 +0100597 tlv_type = le32_to_cpu(tlv->type);
Johannes Berg15854ef92012-03-05 11:24:50 -0800598 tlv_data = tlv->data;
599
600 if (len < tlv_len) {
Johannes Berg965974a62012-03-06 13:30:38 -0800601 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
Johannes Berg15854ef92012-03-05 11:24:50 -0800602 len, tlv_len);
603 return -EINVAL;
604 }
605 len -= ALIGN(tlv_len, 4);
606 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
607
Johannes Berg15854ef92012-03-05 11:24:50 -0800608 switch (tlv_type) {
609 case IWL_UCODE_TLV_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800610 set_sec_data(pieces, IWL_UCODE_REGULAR,
611 IWL_UCODE_SECTION_INST, tlv_data);
612 set_sec_size(pieces, IWL_UCODE_REGULAR,
613 IWL_UCODE_SECTION_INST, tlv_len);
614 set_sec_offset(pieces, IWL_UCODE_REGULAR,
615 IWL_UCODE_SECTION_INST,
616 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800617 break;
618 case IWL_UCODE_TLV_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800619 set_sec_data(pieces, IWL_UCODE_REGULAR,
620 IWL_UCODE_SECTION_DATA, tlv_data);
621 set_sec_size(pieces, IWL_UCODE_REGULAR,
622 IWL_UCODE_SECTION_DATA, tlv_len);
623 set_sec_offset(pieces, IWL_UCODE_REGULAR,
624 IWL_UCODE_SECTION_DATA,
625 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800626 break;
627 case IWL_UCODE_TLV_INIT:
David Spinadel0cedacc2012-03-10 13:00:12 -0800628 set_sec_data(pieces, IWL_UCODE_INIT,
629 IWL_UCODE_SECTION_INST, tlv_data);
630 set_sec_size(pieces, IWL_UCODE_INIT,
631 IWL_UCODE_SECTION_INST, tlv_len);
632 set_sec_offset(pieces, IWL_UCODE_INIT,
633 IWL_UCODE_SECTION_INST,
634 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800635 break;
636 case IWL_UCODE_TLV_INIT_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800637 set_sec_data(pieces, IWL_UCODE_INIT,
638 IWL_UCODE_SECTION_DATA, tlv_data);
639 set_sec_size(pieces, IWL_UCODE_INIT,
640 IWL_UCODE_SECTION_DATA, tlv_len);
641 set_sec_offset(pieces, IWL_UCODE_INIT,
642 IWL_UCODE_SECTION_DATA,
643 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800644 break;
645 case IWL_UCODE_TLV_BOOT:
Johannes Berg965974a62012-03-06 13:30:38 -0800646 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
Johannes Berg15854ef92012-03-05 11:24:50 -0800647 break;
648 case IWL_UCODE_TLV_PROBE_MAX_LEN:
649 if (tlv_len != sizeof(u32))
650 goto invalid_tlv_len;
651 capa->max_probe_length =
652 le32_to_cpup((__le32 *)tlv_data);
653 break;
654 case IWL_UCODE_TLV_PAN:
655 if (tlv_len)
656 goto invalid_tlv_len;
657 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
658 break;
659 case IWL_UCODE_TLV_FLAGS:
660 /* must be at least one u32 */
661 if (tlv_len < sizeof(u32))
662 goto invalid_tlv_len;
663 /* and a proper number of u32s */
664 if (tlv_len % sizeof(u32))
665 goto invalid_tlv_len;
666 /*
667 * This driver only reads the first u32 as
668 * right now no more features are defined,
669 * if that changes then either the driver
670 * will not work with the new firmware, or
671 * it'll not take advantage of new features.
672 */
673 capa->flags = le32_to_cpup((__le32 *)tlv_data);
674 break;
Eran Hararya2978b12014-02-20 11:00:01 +0200675 case IWL_UCODE_TLV_API_CHANGES_SET:
676 if (tlv_len != sizeof(struct iwl_ucode_api))
677 goto invalid_tlv_len;
678 if (iwl_set_ucode_api_flags(drv, tlv_data, capa))
679 goto tlv_error;
680 break;
681 case IWL_UCODE_TLV_ENABLED_CAPABILITIES:
682 if (tlv_len != sizeof(struct iwl_ucode_capa))
683 goto invalid_tlv_len;
684 if (iwl_set_ucode_capabilities(drv, tlv_data, capa))
685 goto tlv_error;
686 break;
Johannes Berg15854ef92012-03-05 11:24:50 -0800687 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
688 if (tlv_len != sizeof(u32))
689 goto invalid_tlv_len;
690 pieces->init_evtlog_ptr =
691 le32_to_cpup((__le32 *)tlv_data);
692 break;
693 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
694 if (tlv_len != sizeof(u32))
695 goto invalid_tlv_len;
696 pieces->init_evtlog_size =
697 le32_to_cpup((__le32 *)tlv_data);
698 break;
699 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
700 if (tlv_len != sizeof(u32))
701 goto invalid_tlv_len;
702 pieces->init_errlog_ptr =
703 le32_to_cpup((__le32 *)tlv_data);
704 break;
705 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
706 if (tlv_len != sizeof(u32))
707 goto invalid_tlv_len;
708 pieces->inst_evtlog_ptr =
709 le32_to_cpup((__le32 *)tlv_data);
710 break;
711 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
712 if (tlv_len != sizeof(u32))
713 goto invalid_tlv_len;
714 pieces->inst_evtlog_size =
715 le32_to_cpup((__le32 *)tlv_data);
716 break;
717 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
718 if (tlv_len != sizeof(u32))
719 goto invalid_tlv_len;
720 pieces->inst_errlog_ptr =
721 le32_to_cpup((__le32 *)tlv_data);
722 break;
723 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
724 if (tlv_len)
725 goto invalid_tlv_len;
Johannes Berg965974a62012-03-06 13:30:38 -0800726 drv->fw.enhance_sensitivity_table = true;
Johannes Berg15854ef92012-03-05 11:24:50 -0800727 break;
728 case IWL_UCODE_TLV_WOWLAN_INST:
David Spinadel0cedacc2012-03-10 13:00:12 -0800729 set_sec_data(pieces, IWL_UCODE_WOWLAN,
730 IWL_UCODE_SECTION_INST, tlv_data);
731 set_sec_size(pieces, IWL_UCODE_WOWLAN,
732 IWL_UCODE_SECTION_INST, tlv_len);
733 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
734 IWL_UCODE_SECTION_INST,
735 IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800736 break;
737 case IWL_UCODE_TLV_WOWLAN_DATA:
David Spinadel0cedacc2012-03-10 13:00:12 -0800738 set_sec_data(pieces, IWL_UCODE_WOWLAN,
739 IWL_UCODE_SECTION_DATA, tlv_data);
740 set_sec_size(pieces, IWL_UCODE_WOWLAN,
741 IWL_UCODE_SECTION_DATA, tlv_len);
742 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
743 IWL_UCODE_SECTION_DATA,
744 IWLAGN_RTC_DATA_LOWER_BOUND);
Johannes Berg15854ef92012-03-05 11:24:50 -0800745 break;
746 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
747 if (tlv_len != sizeof(u32))
748 goto invalid_tlv_len;
749 capa->standard_phy_calibration_size =
750 le32_to_cpup((__le32 *)tlv_data);
751 break;
David Spinadeled8c8362012-03-10 13:00:13 -0800752 case IWL_UCODE_TLV_SEC_RT:
753 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
754 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800755 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800756 break;
757 case IWL_UCODE_TLV_SEC_INIT:
758 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
759 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800760 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800761 break;
762 case IWL_UCODE_TLV_SEC_WOWLAN:
763 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
764 tlv_len);
David Spinadel4db2c9a2012-03-10 13:00:15 -0800765 drv->fw.mvm_fw = true;
David Spinadeled8c8362012-03-10 13:00:13 -0800766 break;
767 case IWL_UCODE_TLV_DEF_CALIB:
768 if (tlv_len != sizeof(struct iwl_tlv_calib_data))
769 goto invalid_tlv_len;
770 if (iwl_set_default_calib(drv, tlv_data))
771 goto tlv_error;
772 break;
773 case IWL_UCODE_TLV_PHY_SKU:
774 if (tlv_len != sizeof(u32))
775 goto invalid_tlv_len;
776 drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
Eran Harary77db0a32014-02-04 14:21:38 +0200777 drv->fw.valid_tx_ant = (drv->fw.phy_config &
778 FW_PHY_CFG_TX_CHAIN) >>
779 FW_PHY_CFG_TX_CHAIN_POS;
780 drv->fw.valid_rx_ant = (drv->fw.phy_config &
781 FW_PHY_CFG_RX_CHAIN) >>
782 FW_PHY_CFG_RX_CHAIN_POS;
David Spinadeled8c8362012-03-10 13:00:13 -0800783 break;
Eran Hararye2d6f4e2013-10-02 13:53:40 +0300784 case IWL_UCODE_TLV_SECURE_SEC_RT:
785 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
786 tlv_len);
787 drv->fw.mvm_fw = true;
788 drv->fw.img[IWL_UCODE_REGULAR].is_secure = true;
789 break;
790 case IWL_UCODE_TLV_SECURE_SEC_INIT:
791 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
792 tlv_len);
793 drv->fw.mvm_fw = true;
794 drv->fw.img[IWL_UCODE_INIT].is_secure = true;
795 break;
796 case IWL_UCODE_TLV_SECURE_SEC_WOWLAN:
797 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
798 tlv_len);
799 drv->fw.mvm_fw = true;
800 drv->fw.img[IWL_UCODE_WOWLAN].is_secure = true;
801 break;
802 case IWL_UCODE_TLV_NUM_OF_CPU:
803 if (tlv_len != sizeof(u32))
804 goto invalid_tlv_len;
805 num_of_cpus =
806 le32_to_cpup((__le32 *)tlv_data);
807
808 if (num_of_cpus == 2) {
809 drv->fw.img[IWL_UCODE_REGULAR].is_dual_cpus =
810 true;
811 drv->fw.img[IWL_UCODE_INIT].is_dual_cpus =
812 true;
813 drv->fw.img[IWL_UCODE_WOWLAN].is_dual_cpus =
814 true;
815 } else if ((num_of_cpus > 2) || (num_of_cpus < 1)) {
816 IWL_ERR(drv, "Driver support upto 2 CPUs\n");
817 return -EINVAL;
818 }
819 break;
Max Stepanove36e5432013-08-27 19:56:13 +0300820 case IWL_UCODE_TLV_CSCHEME:
821 if (iwl_store_cscheme(&drv->fw, tlv_data, tlv_len))
822 goto invalid_tlv_len;
823 break;
David Spinadel762533b2014-06-05 11:20:43 +0300824 case IWL_UCODE_TLV_N_SCAN_CHANNELS:
825 if (tlv_len != sizeof(u32))
826 goto invalid_tlv_len;
827 capa->n_scan_channels =
828 le32_to_cpup((__le32 *)tlv_data);
829 break;
Johannes Berg15854ef92012-03-05 11:24:50 -0800830 default:
Johannes Berg965974a62012-03-06 13:30:38 -0800831 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
Johannes Berg15854ef92012-03-05 11:24:50 -0800832 break;
833 }
834 }
835
836 if (len) {
Johannes Berg965974a62012-03-06 13:30:38 -0800837 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
838 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
Johannes Berg15854ef92012-03-05 11:24:50 -0800839 return -EINVAL;
840 }
841
842 return 0;
843
844 invalid_tlv_len:
Johannes Berg965974a62012-03-06 13:30:38 -0800845 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
David Spinadeled8c8362012-03-10 13:00:13 -0800846 tlv_error:
Johannes Berg965974a62012-03-06 13:30:38 -0800847 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
Johannes Berg15854ef92012-03-05 11:24:50 -0800848
849 return -EINVAL;
850}
851
Johannes Berg75813bd2012-05-16 22:49:49 +0200852static int iwl_alloc_ucode(struct iwl_drv *drv,
853 struct iwl_firmware_pieces *pieces,
854 enum iwl_ucode_type type)
David Spinadel6dfa8d02012-03-10 13:00:14 -0800855{
856 int i;
857 for (i = 0;
858 i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
859 i++)
860 if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
Johannes Berg75813bd2012-05-16 22:49:49 +0200861 get_sec(pieces, type, i)))
862 return -ENOMEM;
David Spinadel6dfa8d02012-03-10 13:00:14 -0800863 return 0;
864}
865
866static int validate_sec_sizes(struct iwl_drv *drv,
867 struct iwl_firmware_pieces *pieces,
868 const struct iwl_cfg *cfg)
869{
870 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
871 get_sec_size(pieces, IWL_UCODE_REGULAR,
872 IWL_UCODE_SECTION_INST));
873 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
874 get_sec_size(pieces, IWL_UCODE_REGULAR,
875 IWL_UCODE_SECTION_DATA));
876 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
877 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
878 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
879 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
880
881 /* Verify that uCode images will fit in card's SRAM. */
882 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
883 cfg->max_inst_size) {
884 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
885 get_sec_size(pieces, IWL_UCODE_REGULAR,
886 IWL_UCODE_SECTION_INST));
887 return -1;
888 }
889
890 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
891 cfg->max_data_size) {
892 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
893 get_sec_size(pieces, IWL_UCODE_REGULAR,
894 IWL_UCODE_SECTION_DATA));
895 return -1;
896 }
897
898 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
899 cfg->max_inst_size) {
900 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
901 get_sec_size(pieces, IWL_UCODE_INIT,
902 IWL_UCODE_SECTION_INST));
903 return -1;
904 }
905
906 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
907 cfg->max_data_size) {
908 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
909 get_sec_size(pieces, IWL_UCODE_REGULAR,
910 IWL_UCODE_SECTION_DATA));
911 return -1;
912 }
913 return 0;
914}
915
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -0700916static struct iwl_op_mode *
917_iwl_op_mode_start(struct iwl_drv *drv, struct iwlwifi_opmode_table *op)
918{
919 const struct iwl_op_mode_ops *ops = op->ops;
920 struct dentry *dbgfs_dir = NULL;
921 struct iwl_op_mode *op_mode = NULL;
922
923#ifdef CONFIG_IWLWIFI_DEBUGFS
924 drv->dbgfs_op_mode = debugfs_create_dir(op->name,
925 drv->dbgfs_drv);
926 if (!drv->dbgfs_op_mode) {
927 IWL_ERR(drv,
928 "failed to create opmode debugfs directory\n");
929 return op_mode;
930 }
931 dbgfs_dir = drv->dbgfs_op_mode;
932#endif
933
934 op_mode = ops->start(drv->trans, drv->cfg, &drv->fw, dbgfs_dir);
935
936#ifdef CONFIG_IWLWIFI_DEBUGFS
937 if (!op_mode) {
938 debugfs_remove_recursive(drv->dbgfs_op_mode);
939 drv->dbgfs_op_mode = NULL;
940 }
941#endif
942
943 return op_mode;
944}
945
946static void _iwl_op_mode_stop(struct iwl_drv *drv)
947{
948 /* op_mode can be NULL if its start failed */
949 if (drv->op_mode) {
950 iwl_op_mode_stop(drv->op_mode);
951 drv->op_mode = NULL;
952
953#ifdef CONFIG_IWLWIFI_DEBUGFS
954 debugfs_remove_recursive(drv->dbgfs_op_mode);
955 drv->dbgfs_op_mode = NULL;
956#endif
957 }
958}
959
Johannes Berg15854ef92012-03-05 11:24:50 -0800960/**
Emmanuel Grumbach1c8e11e2012-07-16 12:31:28 +0300961 * iwl_req_fw_callback - callback when firmware was loaded
Johannes Berg15854ef92012-03-05 11:24:50 -0800962 *
963 * If loaded successfully, copies the firmware into buffers
964 * for the card to fetch (via DMA).
965 */
Emmanuel Grumbach1c8e11e2012-07-16 12:31:28 +0300966static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context)
Johannes Berg15854ef92012-03-05 11:24:50 -0800967{
Johannes Berg965974a62012-03-06 13:30:38 -0800968 struct iwl_drv *drv = context;
Johannes Berg965974a62012-03-06 13:30:38 -0800969 struct iwl_fw *fw = &drv->fw;
Johannes Berg15854ef92012-03-05 11:24:50 -0800970 struct iwl_ucode_header *ucode;
Don Frycc5f7e32012-05-16 22:54:27 +0200971 struct iwlwifi_opmode_table *op;
Johannes Berg15854ef92012-03-05 11:24:50 -0800972 int err;
David Spinadel0cedacc2012-03-10 13:00:12 -0800973 struct iwl_firmware_pieces pieces;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +0200974 const unsigned int api_max = drv->cfg->ucode_api_max;
975 unsigned int api_ok = drv->cfg->ucode_api_ok;
976 const unsigned int api_min = drv->cfg->ucode_api_min;
Johannes Berg15854ef92012-03-05 11:24:50 -0800977 u32 api_ver;
David Spinadel6dfa8d02012-03-10 13:00:14 -0800978 int i;
Johannes Bergd4b10482012-06-12 19:50:43 +0200979 bool load_module = false;
Johannes Berg15854ef92012-03-05 11:24:50 -0800980
Luciano Coelho66140ad2013-08-12 19:30:21 +0300981 fw->ucode_capa.max_probe_length = IWL_DEFAULT_MAX_PROBE_LENGTH;
Johannes Berg15854ef92012-03-05 11:24:50 -0800982 fw->ucode_capa.standard_phy_calibration_size =
983 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
David Spinadel762533b2014-06-05 11:20:43 +0300984 fw->ucode_capa.n_scan_channels = IWL_DEFAULT_SCAN_CHANNELS;
Johannes Berg15854ef92012-03-05 11:24:50 -0800985
986 if (!api_ok)
987 api_ok = api_max;
988
989 memset(&pieces, 0, sizeof(pieces));
990
991 if (!ucode_raw) {
Johannes Berg965974a62012-03-06 13:30:38 -0800992 if (drv->fw_index <= api_ok)
993 IWL_ERR(drv,
Johannes Berg15854ef92012-03-05 11:24:50 -0800994 "request for firmware file '%s' failed.\n",
Johannes Berg965974a62012-03-06 13:30:38 -0800995 drv->firmware_name);
Johannes Berg15854ef92012-03-05 11:24:50 -0800996 goto try_again;
997 }
998
Johannes Berg965974a62012-03-06 13:30:38 -0800999 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
1000 drv->firmware_name, ucode_raw->size);
Johannes Berg15854ef92012-03-05 11:24:50 -08001001
1002 /* Make sure that we got at least the API version number */
1003 if (ucode_raw->size < 4) {
Johannes Berg965974a62012-03-06 13:30:38 -08001004 IWL_ERR(drv, "File size way too small!\n");
Johannes Berg15854ef92012-03-05 11:24:50 -08001005 goto try_again;
1006 }
1007
1008 /* Data from ucode file: header followed by uCode images */
1009 ucode = (struct iwl_ucode_header *)ucode_raw->data;
1010
1011 if (ucode->ver)
Johannes Berg965974a62012-03-06 13:30:38 -08001012 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
Johannes Berg15854ef92012-03-05 11:24:50 -08001013 else
Johannes Berg965974a62012-03-06 13:30:38 -08001014 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
Johannes Berg15854ef92012-03-05 11:24:50 -08001015 &fw->ucode_capa);
1016
1017 if (err)
1018 goto try_again;
1019
Johannes Berg965974a62012-03-06 13:30:38 -08001020 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef92012-03-05 11:24:50 -08001021
1022 /*
1023 * api_ver should match the api version forming part of the
1024 * firmware filename ... but we don't check for that and only rely
1025 * on the API version read from firmware header from here on forward
1026 */
1027 /* no api version check required for experimental uCode */
Johannes Berg965974a62012-03-06 13:30:38 -08001028 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef92012-03-05 11:24:50 -08001029 if (api_ver < api_min || api_ver > api_max) {
Johannes Berg965974a62012-03-06 13:30:38 -08001030 IWL_ERR(drv,
Johannes Berg15854ef92012-03-05 11:24:50 -08001031 "Driver unable to support your firmware API. "
1032 "Driver supports v%u, firmware is v%u.\n",
1033 api_max, api_ver);
1034 goto try_again;
1035 }
1036
1037 if (api_ver < api_ok) {
1038 if (api_ok != api_max)
Johannes Berg965974a62012-03-06 13:30:38 -08001039 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef92012-03-05 11:24:50 -08001040 "expected v%u through v%u, got v%u.\n",
1041 api_ok, api_max, api_ver);
1042 else
Johannes Berg965974a62012-03-06 13:30:38 -08001043 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef92012-03-05 11:24:50 -08001044 "expected v%u, got v%u.\n",
1045 api_max, api_ver);
Johannes Berg965974a62012-03-06 13:30:38 -08001046 IWL_ERR(drv, "New firmware can be obtained from "
Johannes Berg15854ef92012-03-05 11:24:50 -08001047 "http://www.intellinuxwireless.org/.\n");
1048 }
1049 }
1050
Johannes Berg15854ef92012-03-05 11:24:50 -08001051 /*
David Spinadel4db2c9a2012-03-10 13:00:15 -08001052 * In mvm uCode there is no difference between data and instructions
1053 * sections.
1054 */
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +02001055 if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
Johannes Berg15854ef92012-03-05 11:24:50 -08001056 goto try_again;
Johannes Berg15854ef92012-03-05 11:24:50 -08001057
1058 /* Allocate ucode buffers for card's bus-master loading ... */
1059
1060 /* Runtime instructions and 2 copies of data:
1061 * 1) unmodified from disk
1062 * 2) backup cache for save/restore during power-downs */
David Spinadel6dfa8d02012-03-10 13:00:14 -08001063 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
Johannes Berg75813bd2012-05-16 22:49:49 +02001064 if (iwl_alloc_ucode(drv, &pieces, i))
1065 goto out_free_fw;
Johannes Berg15854ef92012-03-05 11:24:50 -08001066
1067 /* Now that we can no longer fail, copy information */
1068
1069 /*
1070 * The (size - 16) / 12 formula is based on the information recorded
1071 * for each event, which is of mode 1 (including timestamp) for all
1072 * new microcodes that include this information.
1073 */
Johannes Berg0692fe42012-03-06 13:30:37 -08001074 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
Johannes Berg15854ef92012-03-05 11:24:50 -08001075 if (pieces.init_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -08001076 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
Johannes Berg15854ef92012-03-05 11:24:50 -08001077 else
Johannes Berg0692fe42012-03-06 13:30:37 -08001078 fw->init_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +02001079 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -08001080 fw->init_errlog_ptr = pieces.init_errlog_ptr;
1081 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
Johannes Berg15854ef92012-03-05 11:24:50 -08001082 if (pieces.inst_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -08001083 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
Johannes Berg15854ef92012-03-05 11:24:50 -08001084 else
Johannes Berg0692fe42012-03-06 13:30:37 -08001085 fw->inst_evtlog_size =
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +02001086 drv->cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -08001087 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
Johannes Berg15854ef92012-03-05 11:24:50 -08001088
1089 /*
1090 * figure out the offset of chain noise reset and gain commands
1091 * base on the size of standard phy calibration commands table size
1092 */
1093 if (fw->ucode_capa.standard_phy_calibration_size >
1094 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
1095 fw->ucode_capa.standard_phy_calibration_size =
1096 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1097
1098 /* We have our copies now, allow OS release its copies */
1099 release_firmware(ucode_raw);
Johannes Berg15854ef92012-03-05 11:24:50 -08001100
Johannes Bergff1ffb82012-06-06 09:42:57 +02001101 mutex_lock(&iwlwifi_opmode_table_mtx);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001102 if (fw->mvm_fw)
1103 op = &iwlwifi_opmode_table[MVM_OP_MODE];
1104 else
1105 op = &iwlwifi_opmode_table[DVM_OP_MODE];
Johannes Berg15854ef92012-03-05 11:24:50 -08001106
Johannes Berg2b2719c2013-03-26 11:41:51 +01001107 IWL_INFO(drv, "loaded firmware version %s op_mode %s\n",
1108 drv->fw.fw_version, op->name);
1109
Don Frycc5f7e32012-05-16 22:54:27 +02001110 /* add this device to the list of devices using this op_mode */
1111 list_add_tail(&drv->list, &op->drv);
1112
1113 if (op->ops) {
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001114 drv->op_mode = _iwl_op_mode_start(drv, op);
John W. Linville7c9c46c2012-06-06 14:40:06 -04001115
Dan Carpenterdaf67ce2012-06-14 21:35:26 +03001116 if (!drv->op_mode) {
1117 mutex_unlock(&iwlwifi_opmode_table_mtx);
John W. Linville7c9c46c2012-06-06 14:40:06 -04001118 goto out_unbind;
Dan Carpenterdaf67ce2012-06-14 21:35:26 +03001119 }
Don Frycc5f7e32012-05-16 22:54:27 +02001120 } else {
Johannes Bergd4b10482012-06-12 19:50:43 +02001121 load_module = true;
Don Frycc5f7e32012-05-16 22:54:27 +02001122 }
Johannes Bergff1ffb82012-06-06 09:42:57 +02001123 mutex_unlock(&iwlwifi_opmode_table_mtx);
Johannes Berg15854ef92012-03-05 11:24:50 -08001124
Johannes Bergf69a23b2012-06-05 19:56:06 +02001125 /*
1126 * Complete the firmware request last so that
1127 * a driver unbind (stop) doesn't run while we
1128 * are doing the start() above.
1129 */
1130 complete(&drv->request_firmware_complete);
Johannes Bergd4b10482012-06-12 19:50:43 +02001131
1132 /*
1133 * Load the module last so we don't block anything
1134 * else from proceeding if the module fails to load
1135 * or hangs loading.
1136 */
Johannes Berg1618b2b2013-04-04 10:35:23 +02001137 if (load_module) {
1138 err = request_module("%s", op->name);
Johannes Berg8edf3fd2013-05-28 21:32:47 +02001139#ifdef CONFIG_IWLWIFI_OPMODE_MODULAR
Johannes Berg1618b2b2013-04-04 10:35:23 +02001140 if (err)
1141 IWL_ERR(drv,
1142 "failed to load module %s (error %d), is dynamic loading enabled?\n",
1143 op->name, err);
Johannes Berg8edf3fd2013-05-28 21:32:47 +02001144#endif
Johannes Berg1618b2b2013-04-04 10:35:23 +02001145 }
Johannes Berg15854ef92012-03-05 11:24:50 -08001146 return;
1147
1148 try_again:
1149 /* try next, if any */
1150 release_firmware(ucode_raw);
Johannes Berg965974a62012-03-06 13:30:38 -08001151 if (iwl_request_firmware(drv, false))
Johannes Berg15854ef92012-03-05 11:24:50 -08001152 goto out_unbind;
1153 return;
1154
Johannes Berg75813bd2012-05-16 22:49:49 +02001155 out_free_fw:
Johannes Berg965974a62012-03-06 13:30:38 -08001156 IWL_ERR(drv, "failed to allocate pci memory\n");
1157 iwl_dealloc_ucode(drv);
Johannes Berg15854ef92012-03-05 11:24:50 -08001158 release_firmware(ucode_raw);
1159 out_unbind:
Johannes Berg965974a62012-03-06 13:30:38 -08001160 complete(&drv->request_firmware_complete);
Emmanuel Grumbach93faaee2012-03-22 17:51:44 +02001161 device_release_driver(drv->trans->dev);
Johannes Berg15854ef92012-03-05 11:24:50 -08001162}
1163
Emmanuel Grumbach87ce05a2012-03-26 09:03:18 -07001164struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -07001165 const struct iwl_cfg *cfg)
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001166{
Johannes Berg965974a62012-03-06 13:30:38 -08001167 struct iwl_drv *drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001168 int ret;
1169
Johannes Berg965974a62012-03-06 13:30:38 -08001170 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
Luciano Coelhoeafe25e2013-08-13 10:34:55 +03001171 if (!drv) {
1172 ret = -ENOMEM;
1173 goto err;
1174 }
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +03001175
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -07001176 drv->trans = trans;
Emmanuel Grumbach4b9844f2012-03-22 23:59:52 +02001177 drv->dev = trans->dev;
Emmanuel Grumbach68f360d2012-03-22 17:51:44 +02001178 drv->cfg = cfg;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001179
Johannes Berg965974a62012-03-06 13:30:38 -08001180 init_completion(&drv->request_firmware_complete);
Johannes Bergff1ffb82012-06-06 09:42:57 +02001181 INIT_LIST_HEAD(&drv->list);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001182
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001183#ifdef CONFIG_IWLWIFI_DEBUGFS
1184 /* Create the device debugfs entries. */
1185 drv->dbgfs_drv = debugfs_create_dir(dev_name(trans->dev),
1186 iwl_dbgfs_root);
1187
1188 if (!drv->dbgfs_drv) {
1189 IWL_ERR(drv, "failed to create debugfs directory\n");
Emmanuel Grumbach59f547a2012-11-28 16:42:09 +02001190 ret = -ENOMEM;
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001191 goto err_free_drv;
1192 }
1193
1194 /* Create transport layer debugfs dir */
1195 drv->trans->dbgfs_dir = debugfs_create_dir("trans", drv->dbgfs_drv);
1196
1197 if (!drv->trans->dbgfs_dir) {
1198 IWL_ERR(drv, "failed to create transport debugfs directory\n");
Emmanuel Grumbach59f547a2012-11-28 16:42:09 +02001199 ret = -ENOMEM;
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001200 goto err_free_dbgfs;
1201 }
1202#endif
1203
Johannes Berg965974a62012-03-06 13:30:38 -08001204 ret = iwl_request_firmware(drv, true);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001205 if (ret) {
Emmanuel Grumbachc15797e2012-04-19 10:29:58 +03001206 IWL_ERR(trans, "Couldn't request the fw\n");
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001207 goto err_fw;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001208 }
1209
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -07001210 return drv;
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001211
1212err_fw:
1213#ifdef CONFIG_IWLWIFI_DEBUGFS
1214err_free_dbgfs:
1215 debugfs_remove_recursive(drv->dbgfs_drv);
1216err_free_drv:
1217#endif
1218 kfree(drv);
Luciano Coelhoeafe25e2013-08-13 10:34:55 +03001219err:
Emmanuel Grumbach59f547a2012-11-28 16:42:09 +02001220 return ERR_PTR(ret);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001221}
1222
Emmanuel Grumbach9130bab2012-03-26 08:51:09 -07001223void iwl_drv_stop(struct iwl_drv *drv)
Emmanuel Grumbach07590f02012-02-07 14:27:31 +02001224{
Johannes Berg965974a62012-03-06 13:30:38 -08001225 wait_for_completion(&drv->request_firmware_complete);
Johannes Berg2e7eb112012-03-05 11:24:51 -08001226
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001227 _iwl_op_mode_stop(drv);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +02001228
Johannes Berg965974a62012-03-06 13:30:38 -08001229 iwl_dealloc_ucode(drv);
Johannes Berg7db5b982012-03-05 11:24:48 -08001230
Johannes Bergff1ffb82012-06-06 09:42:57 +02001231 mutex_lock(&iwlwifi_opmode_table_mtx);
1232 /*
1233 * List is empty (this item wasn't added)
1234 * when firmware loading failed -- in that
1235 * case we can't remove it from any list.
1236 */
1237 if (!list_empty(&drv->list))
1238 list_del(&drv->list);
1239 mutex_unlock(&iwlwifi_opmode_table_mtx);
1240
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001241#ifdef CONFIG_IWLWIFI_DEBUGFS
1242 debugfs_remove_recursive(drv->dbgfs_drv);
1243#endif
1244
Johannes Berg965974a62012-03-06 13:30:38 -08001245 kfree(drv);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +02001246}
Johannes Berg65de7e82012-04-17 07:36:30 -07001247
1248
1249/* shared module parameters */
1250struct iwl_mod_params iwlwifi_mod_params = {
Emmanuel Grumbach490953a2013-03-04 08:53:07 +02001251 .restart_fw = true,
Johannes Berg65de7e82012-04-17 07:36:30 -07001252 .bt_coex_active = true,
1253 .power_level = IWL_POWER_INDEX_1,
Emmanuel Grumbach020c8762012-06-24 15:51:59 +03001254 .wd_disable = true,
Matt Chen1504f482014-04-24 18:43:18 +08001255 .uapsd_disable = false,
Johannes Berg65de7e82012-04-17 07:36:30 -07001256 /* the rest are 0 by default */
1257};
Johannes Berg48e29342013-03-01 00:13:33 +01001258IWL_EXPORT_SYMBOL(iwlwifi_mod_params);
Don Frycc5f7e32012-05-16 22:54:27 +02001259
1260int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
1261{
1262 int i;
1263 struct iwl_drv *drv;
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001264 struct iwlwifi_opmode_table *op;
Don Frycc5f7e32012-05-16 22:54:27 +02001265
Johannes Bergff1ffb82012-06-06 09:42:57 +02001266 mutex_lock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001267 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001268 op = &iwlwifi_opmode_table[i];
1269 if (strcmp(op->name, name))
Don Frycc5f7e32012-05-16 22:54:27 +02001270 continue;
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001271 op->ops = ops;
1272 /* TODO: need to handle exceptional case */
1273 list_for_each_entry(drv, &op->drv, list)
1274 drv->op_mode = _iwl_op_mode_start(drv, op);
1275
Johannes Bergff1ffb82012-06-06 09:42:57 +02001276 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001277 return 0;
1278 }
Johannes Bergff1ffb82012-06-06 09:42:57 +02001279 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001280 return -EIO;
1281}
Johannes Berg48e29342013-03-01 00:13:33 +01001282IWL_EXPORT_SYMBOL(iwl_opmode_register);
Don Frycc5f7e32012-05-16 22:54:27 +02001283
1284void iwl_opmode_deregister(const char *name)
1285{
1286 int i;
1287 struct iwl_drv *drv;
1288
Johannes Bergff1ffb82012-06-06 09:42:57 +02001289 mutex_lock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001290 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1291 if (strcmp(iwlwifi_opmode_table[i].name, name))
1292 continue;
1293 iwlwifi_opmode_table[i].ops = NULL;
1294
1295 /* call the stop routine for all devices */
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001296 list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
1297 _iwl_op_mode_stop(drv);
1298
Johannes Bergff1ffb82012-06-06 09:42:57 +02001299 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001300 return;
1301 }
Johannes Bergff1ffb82012-06-06 09:42:57 +02001302 mutex_unlock(&iwlwifi_opmode_table_mtx);
Don Frycc5f7e32012-05-16 22:54:27 +02001303}
Johannes Berg48e29342013-03-01 00:13:33 +01001304IWL_EXPORT_SYMBOL(iwl_opmode_deregister);
Don Frycc5f7e32012-05-16 22:54:27 +02001305
1306static int __init iwl_drv_init(void)
1307{
1308 int i;
1309
Johannes Bergff1ffb82012-06-06 09:42:57 +02001310 mutex_init(&iwlwifi_opmode_table_mtx);
1311
Don Frycc5f7e32012-05-16 22:54:27 +02001312 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
1313 INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
1314
1315 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
1316 pr_info(DRV_COPYRIGHT "\n");
1317
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001318#ifdef CONFIG_IWLWIFI_DEBUGFS
1319 /* Create the root of iwlwifi debugfs subsystem. */
1320 iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL);
1321
1322 if (!iwl_dbgfs_root)
1323 return -EFAULT;
1324#endif
1325
Don Frycc5f7e32012-05-16 22:54:27 +02001326 return iwl_pci_register_driver();
1327}
1328module_init(iwl_drv_init);
1329
1330static void __exit iwl_drv_exit(void)
1331{
1332 iwl_pci_unregister_driver();
Meenakshi Venkataraman9da987a2012-07-16 18:43:56 -07001333
1334#ifdef CONFIG_IWLWIFI_DEBUGFS
1335 debugfs_remove_recursive(iwl_dbgfs_root);
1336#endif
Don Frycc5f7e32012-05-16 22:54:27 +02001337}
1338module_exit(iwl_drv_exit);
Johannes Berg65de7e82012-04-17 07:36:30 -07001339
1340#ifdef CONFIG_IWLWIFI_DEBUG
1341module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
1342 S_IRUGO | S_IWUSR);
1343MODULE_PARM_DESC(debug, "debug output mask");
1344#endif
1345
1346module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
1347MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
1348module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
1349MODULE_PARM_DESC(11n_disable,
Emmanuel Grumbach205e2212014-02-12 15:15:05 +02001350 "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 -07001351module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
1352 int, S_IRUGO);
Emmanuel Grumbachaed7d9a2013-02-20 11:33:00 +02001353MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size (default 0)");
Emmanuel Grumbach490953a2013-03-04 08:53:07 +02001354module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, bool, S_IRUGO);
1355MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)");
Johannes Berg65de7e82012-04-17 07:36:30 -07001356
1357module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
1358 int, S_IRUGO);
1359MODULE_PARM_DESC(antenna_coupling,
1360 "specify antenna coupling in dB (defualt: 0 dB)");
1361
Johannes Berg65de7e82012-04-17 07:36:30 -07001362module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
1363MODULE_PARM_DESC(wd_disable,
Eliad Peller82b715c2014-02-11 16:54:42 +02001364 "Disable stuck queue watchdog timer 0=system default, 1=disable (default: 1)");
Johannes Berg65de7e82012-04-17 07:36:30 -07001365
Eran Harary12147552013-05-09 08:07:59 +03001366module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, S_IRUGO);
1367MODULE_PARM_DESC(nvm_file, "NVM file name");
1368
Matt Chen1504f482014-04-24 18:43:18 +08001369module_param_named(uapsd_disable, iwlwifi_mod_params.uapsd_disable,
1370 bool, S_IRUGO);
1371MODULE_PARM_DESC(uapsd_disable, "disable U-APSD functionality (default: N)");
1372
Johannes Berg65de7e82012-04-17 07:36:30 -07001373/*
1374 * set bt_coex_active to true, uCode will do kill/defer
1375 * every time the priority line is asserted (BT is sending signals on the
1376 * priority line in the PCIx).
1377 * set bt_coex_active to false, uCode will ignore the BT activity and
1378 * perform the normal operation
1379 *
1380 * User might experience transmit issue on some platform due to WiFi/BT
1381 * co-exist problem. The possible behaviors are:
1382 * Able to scan and finding all the available AP
1383 * Not able to associate with any AP
1384 * On those platforms, WiFi communication can be restored by set
1385 * "bt_coex_active" module parameter to "false"
1386 *
1387 * default: bt_coex_active = true (BT_COEX_ENABLE)
1388 */
1389module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
1390 bool, S_IRUGO);
1391MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
1392
1393module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
1394MODULE_PARM_DESC(led_mode, "0=system default, "
1395 "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
1396
1397module_param_named(power_save, iwlwifi_mod_params.power_save,
1398 bool, S_IRUGO);
1399MODULE_PARM_DESC(power_save,
1400 "enable WiFi power management (default: disable)");
1401
1402module_param_named(power_level, iwlwifi_mod_params.power_level,
1403 int, S_IRUGO);
1404MODULE_PARM_DESC(power_level,
1405 "default power save level (range from 1 - 5, default: 1)");