]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/wireless/iwlwifi/iwl-drv.c
Merge remote-tracking branch 'wireless-next/master' into iwlwifi-next
[mirror_ubuntu-artful-kernel.git] / drivers / net / wireless / iwlwifi / iwl-drv.c
CommitLineData
5c58edc6
EG
1/******************************************************************************
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 *
51368bf7 8 * Copyright(c) 2007 - 2014 Intel Corporation. All rights reserved.
5c58edc6
EG
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
410dc5aa 25 * in the file called COPYING.
5c58edc6
EG
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
51368bf7 33 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
5c58edc6
EG
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63#include <linux/completion.h>
15854ef9
JB
64#include <linux/dma-mapping.h>
65#include <linux/firmware.h>
66#include <linux/module.h>
83f84d7b 67#include <linux/vmalloc.h>
5c58edc6
EG
68
69#include "iwl-drv.h"
c15797e6 70#include "iwl-debug.h"
5c58edc6 71#include "iwl-trans.h"
d0f76d68 72#include "iwl-op-mode.h"
0cedacc5 73#include "iwl-agn-hw.h"
6238b008
JB
74#include "iwl-fw.h"
75#include "iwl-config.h"
65de7e84 76#include "iwl-modparams.h"
5c58edc6 77
0692fe41 78/* private includes */
3995deaf 79#include "iwl-fw-file.h"
0692fe41 80
cc5f7e39
DF
81/******************************************************************************
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
9da987ac
MV
105#ifdef CONFIG_IWLWIFI_DEBUGFS
106static struct dentry *iwl_dbgfs_root;
107#endif
108
965974a6
JB
109/**
110 * struct iwl_drv - drv common data
cc5f7e39 111 * @list: list of drv structures using this opmode
965974a6 112 * @fw: the iwl_fw structure
965974a6 113 * @op_mode: the running op_mode
68f360dc 114 * @trans: transport layer
4b9844f5 115 * @dev: for debug prints only
68f360dc 116 * @cfg: configuration struct
965974a6
JB
117 * @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 {
cc5f7e39 122 struct list_head list;
965974a6
JB
123 struct iwl_fw fw;
124
965974a6 125 struct iwl_op_mode *op_mode;
9130bab1 126 struct iwl_trans *trans;
4b9844f5 127 struct device *dev;
68f360dc 128 const struct iwl_cfg *cfg;
965974a6
JB
129
130 int fw_index; /* firmware we're trying to load */
84b0312e 131 char firmware_name[32]; /* name of firmware file to load */
965974a6
JB
132
133 struct completion request_firmware_complete;
9da987ac
MV
134
135#ifdef CONFIG_IWLWIFI_DEBUGFS
136 struct dentry *dbgfs_drv;
137 struct dentry *dbgfs_trans;
138 struct dentry *dbgfs_op_mode;
139#endif
965974a6
JB
140};
141
8ca151b5
JB
142enum {
143 DVM_OP_MODE = 0,
144 MVM_OP_MODE = 1,
145};
965974a6 146
ff1ffb85
JB
147/* Protects the table contents, i.e. the ops pointer & drv list */
148static struct mutex iwlwifi_opmode_table_mtx;
cc5f7e39
DF
149static 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 */
8ca151b5
JB
154 [DVM_OP_MODE] = { .name = "iwldvm", .ops = NULL },
155 [MVM_OP_MODE] = { .name = "iwlmvm", .ops = NULL },
cc5f7e39 156};
965974a6 157
762533ba
DS
158#define IWL_DEFAULT_SCAN_CHANNELS 40
159
0cedacc5
DS
160/*
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
965974a6 170static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
15854ef9 171{
83f84d7b
JB
172 vfree(desc->data);
173 desc->data = NULL;
15854ef9
JB
174 desc->len = 0;
175}
176
965974a6 177static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
15854ef9 178{
6dfa8d01
DS
179 int i;
180 for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
181 iwl_free_fw_desc(drv, &img->sec[i]);
15854ef9
JB
182}
183
965974a6 184static void iwl_dealloc_ucode(struct iwl_drv *drv)
15854ef9 185{
6dfa8d01
DS
186 int i;
187 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
188 iwl_free_fw_img(drv, drv->fw.img + i);
15854ef9
JB
189}
190
965974a6 191static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
83f84d7b 192 struct fw_sec *sec)
15854ef9 193{
83f84d7b
JB
194 void *data;
195
196 desc->data = NULL;
197
198 if (!sec || !sec->size)
15854ef9 199 return -EINVAL;
15854ef9 200
83f84d7b
JB
201 data = vmalloc(sec->size);
202 if (!data)
15854ef9
JB
203 return -ENOMEM;
204
0cedacc5
DS
205 desc->len = sec->size;
206 desc->offset = sec->offset;
83f84d7b
JB
207 memcpy(data, sec->data, desc->len);
208 desc->data = data;
209
15854ef9
JB
210 return 0;
211}
212
1c8e11e1
EG
213static void iwl_req_fw_callback(const struct firmware *ucode_raw,
214 void *context);
15854ef9
JB
215
216#define UCODE_EXPERIMENTAL_INDEX 100
217#define UCODE_EXPERIMENTAL_TAG "exp"
218
965974a6 219static int iwl_request_firmware(struct iwl_drv *drv, bool first)
15854ef9 220{
68f360dc 221 const char *name_pre = drv->cfg->fw_name_pre;
15854ef9
JB
222 char tag[8];
223
224 if (first) {
225#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
965974a6 226 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
15854ef9 227 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
965974a6 228 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
15854ef9 229#endif
68f360dc 230 drv->fw_index = drv->cfg->ucode_api_max;
965974a6 231 sprintf(tag, "%d", drv->fw_index);
15854ef9 232 } else {
965974a6
JB
233 drv->fw_index--;
234 sprintf(tag, "%d", drv->fw_index);
15854ef9
JB
235 }
236
68f360dc 237 if (drv->fw_index < drv->cfg->ucode_api_min) {
965974a6 238 IWL_ERR(drv, "no suitable firmware found!\n");
15854ef9
JB
239 return -ENOENT;
240 }
241
84b0312e
LK
242 snprintf(drv->firmware_name, sizeof(drv->firmware_name), "%s%s.ucode",
243 name_pre, tag);
15854ef9 244
965974a6
JB
245 IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
246 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
15854ef9 247 ? "EXPERIMENTAL " : "",
965974a6 248 drv->firmware_name);
15854ef9 249
965974a6 250 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
93faaeea 251 drv->trans->dev,
1c8e11e1 252 GFP_KERNEL, drv, iwl_req_fw_callback);
15854ef9
JB
253}
254
0cedacc5 255struct fw_img_parsing {
6dfa8d01 256 struct fw_sec sec[IWL_UCODE_SECTION_MAX];
0cedacc5
DS
257 int sec_counter;
258};
259
ed8c8365
DS
260/*
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;
aa2b1770 276 struct iwl_tlv_calib_ctrl calib;
ed8c8365
DS
277} __packed;
278
0cedacc5
DS
279struct iwl_firmware_pieces {
280 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
15854ef9
JB
281
282 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
283 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
284};
285
0cedacc5
DS
286/*
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
e36e5433
MS
328static 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
ed8c8365
DS
363/*
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;
1176f431 384 sec->size = size - sizeof(sec_parse->offset);
ed8c8365
DS
385
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 }
aa2b1770
JB
401 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
ed8c8365
DS
406 return 0;
407}
408
a2978b11
EH
409static 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
965974a6 441static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
0cedacc5
DS
442 const struct firmware *ucode_raw,
443 struct iwl_firmware_pieces *pieces)
15854ef9
JB
444{
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
965974a6
JB
450 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
451 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
15854ef9
JB
452
453 switch (api_ver) {
454 default:
455 hdr_size = 28;
456 if (ucode_raw->size < hdr_size) {
965974a6 457 IWL_ERR(drv, "File size too small!\n");
15854ef9
JB
458 return -EINVAL;
459 }
460 build = le32_to_cpu(ucode->u.v2.build);
0cedacc5
DS
461 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));
15854ef9
JB
469 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) {
965974a6 476 IWL_ERR(drv, "File size too small!\n");
15854ef9
JB
477 return -EINVAL;
478 }
479 build = 0;
0cedacc5
DS
480 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));
15854ef9
JB
488 src = ucode->u.v1.data;
489 break;
490 }
491
492 if (build)
493 sprintf(buildstr, " build %u%s", build,
965974a6 494 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
15854ef9
JB
495 ? " (EXP)" : "");
496 else
497 buildstr[0] = '\0';
498
965974a6
JB
499 snprintf(drv->fw.fw_version,
500 sizeof(drv->fw.fw_version),
15854ef9 501 "%u.%u.%u.%u%s",
965974a6
JB
502 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),
15854ef9
JB
506 buildstr);
507
508 /* Verify size of file vs. image size info in file's header */
0cedacc5
DS
509
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)) {
15854ef9 515
965974a6 516 IWL_ERR(drv,
15854ef9
JB
517 "uCode file size %d does not match expected size\n",
518 (int)ucode_raw->size);
519 return -EINVAL;
520 }
521
15854ef9 522
0cedacc5
DS
523 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);
15854ef9
JB
539 return 0;
540}
541
965974a6 542static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
15854ef9 543 const struct firmware *ucode_raw,
0cedacc5 544 struct iwl_firmware_pieces *pieces,
15854ef9
JB
545 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;
15854ef9
JB
551 u32 tlv_len;
552 enum iwl_ucode_tlv_type tlv_type;
553 const u8 *tlv_data;
554 char buildstr[25];
555 u32 build;
e2d6f4e7 556 int num_of_cpus;
15854ef9
JB
557
558 if (len < sizeof(*ucode)) {
965974a6 559 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
15854ef9
JB
560 return -EINVAL;
561 }
562
563 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
965974a6 564 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
15854ef9
JB
565 le32_to_cpu(ucode->magic));
566 return -EINVAL;
567 }
568
965974a6 569 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
06ddbf5a
EG
570 memcpy(drv->fw.human_readable, ucode->human_readable,
571 sizeof(drv->fw.human_readable));
15854ef9
JB
572 build = le32_to_cpu(ucode->build);
573
574 if (build)
575 sprintf(buildstr, " build %u%s", build,
965974a6 576 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
15854ef9
JB
577 ? " (EXP)" : "");
578 else
579 buildstr[0] = '\0';
580
965974a6
JB
581 snprintf(drv->fw.fw_version,
582 sizeof(drv->fw.fw_version),
15854ef9 583 "%u.%u.%u.%u%s",
965974a6
JB
584 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),
15854ef9
JB
588 buildstr);
589
590 data = ucode->data;
591
592 len -= sizeof(*ucode);
593
594 while (len >= sizeof(*tlv)) {
15854ef9
JB
595 len -= sizeof(*tlv);
596 tlv = (void *)data;
597
598 tlv_len = le32_to_cpu(tlv->length);
0479c19d 599 tlv_type = le32_to_cpu(tlv->type);
15854ef9
JB
600 tlv_data = tlv->data;
601
602 if (len < tlv_len) {
965974a6 603 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
15854ef9
JB
604 len, tlv_len);
605 return -EINVAL;
606 }
607 len -= ALIGN(tlv_len, 4);
608 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
609
15854ef9
JB
610 switch (tlv_type) {
611 case IWL_UCODE_TLV_INST:
0cedacc5
DS
612 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);
15854ef9
JB
619 break;
620 case IWL_UCODE_TLV_DATA:
0cedacc5
DS
621 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);
15854ef9
JB
628 break;
629 case IWL_UCODE_TLV_INIT:
0cedacc5
DS
630 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);
15854ef9
JB
637 break;
638 case IWL_UCODE_TLV_INIT_DATA:
0cedacc5
DS
639 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);
15854ef9
JB
646 break;
647 case IWL_UCODE_TLV_BOOT:
965974a6 648 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
15854ef9
JB
649 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;
a2978b11
EH
677 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;
15854ef9
JB
689 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;
965974a6 728 drv->fw.enhance_sensitivity_table = true;
15854ef9
JB
729 break;
730 case IWL_UCODE_TLV_WOWLAN_INST:
0cedacc5
DS
731 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);
15854ef9
JB
738 break;
739 case IWL_UCODE_TLV_WOWLAN_DATA:
0cedacc5
DS
740 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);
15854ef9
JB
747 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;
ed8c8365
DS
754 case IWL_UCODE_TLV_SEC_RT:
755 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
756 tlv_len);
4db2c9ae 757 drv->fw.mvm_fw = true;
ed8c8365
DS
758 break;
759 case IWL_UCODE_TLV_SEC_INIT:
760 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
761 tlv_len);
4db2c9ae 762 drv->fw.mvm_fw = true;
ed8c8365
DS
763 break;
764 case IWL_UCODE_TLV_SEC_WOWLAN:
765 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
766 tlv_len);
4db2c9ae 767 drv->fw.mvm_fw = true;
ed8c8365
DS
768 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);
77db0a3c
EH
779 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;
ed8c8365 785 break;
e2d6f4e7
EH
786 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;
e36e5433
MS
822 case IWL_UCODE_TLV_CSCHEME:
823 if (iwl_store_cscheme(&drv->fw, tlv_data, tlv_len))
824 goto invalid_tlv_len;
825 break;
762533ba
DS
826 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;
15854ef9 832 default:
965974a6 833 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
15854ef9
JB
834 break;
835 }
836 }
837
838 if (len) {
965974a6
JB
839 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
840 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
15854ef9
JB
841 return -EINVAL;
842 }
843
844 return 0;
845
846 invalid_tlv_len:
965974a6 847 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
ed8c8365 848 tlv_error:
965974a6 849 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
15854ef9
JB
850
851 return -EINVAL;
852}
853
75813bde
JB
854static int iwl_alloc_ucode(struct iwl_drv *drv,
855 struct iwl_firmware_pieces *pieces,
856 enum iwl_ucode_type type)
6dfa8d01
DS
857{
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]),
75813bde
JB
863 get_sec(pieces, type, i)))
864 return -ENOMEM;
6dfa8d01
DS
865 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
9da987ac
MV
918static 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
15854ef9 962/**
1c8e11e1 963 * iwl_req_fw_callback - callback when firmware was loaded
15854ef9
JB
964 *
965 * If loaded successfully, copies the firmware into buffers
966 * for the card to fetch (via DMA).
967 */
1c8e11e1 968static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context)
15854ef9 969{
965974a6 970 struct iwl_drv *drv = context;
965974a6 971 struct iwl_fw *fw = &drv->fw;
15854ef9 972 struct iwl_ucode_header *ucode;
cc5f7e39 973 struct iwlwifi_opmode_table *op;
15854ef9 974 int err;
0cedacc5 975 struct iwl_firmware_pieces pieces;
68f360dc
EG
976 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;
15854ef9 979 u32 api_ver;
6dfa8d01 980 int i;
d4b10483 981 bool load_module = false;
15854ef9 982
66140adc 983 fw->ucode_capa.max_probe_length = IWL_DEFAULT_MAX_PROBE_LENGTH;
15854ef9
JB
984 fw->ucode_capa.standard_phy_calibration_size =
985 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
762533ba 986 fw->ucode_capa.n_scan_channels = IWL_DEFAULT_SCAN_CHANNELS;
15854ef9
JB
987
988 if (!api_ok)
989 api_ok = api_max;
990
991 memset(&pieces, 0, sizeof(pieces));
992
993 if (!ucode_raw) {
965974a6
JB
994 if (drv->fw_index <= api_ok)
995 IWL_ERR(drv,
15854ef9 996 "request for firmware file '%s' failed.\n",
965974a6 997 drv->firmware_name);
15854ef9
JB
998 goto try_again;
999 }
1000
965974a6
JB
1001 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
1002 drv->firmware_name, ucode_raw->size);
15854ef9
JB
1003
1004 /* Make sure that we got at least the API version number */
1005 if (ucode_raw->size < 4) {
965974a6 1006 IWL_ERR(drv, "File size way too small!\n");
15854ef9
JB
1007 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)
965974a6 1014 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
15854ef9 1015 else
965974a6 1016 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
15854ef9
JB
1017 &fw->ucode_capa);
1018
1019 if (err)
1020 goto try_again;
1021
965974a6 1022 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
15854ef9
JB
1023
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 */
965974a6 1030 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
15854ef9 1031 if (api_ver < api_min || api_ver > api_max) {
965974a6 1032 IWL_ERR(drv,
15854ef9
JB
1033 "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)
965974a6 1041 IWL_ERR(drv, "Firmware has old API version, "
15854ef9
JB
1042 "expected v%u through v%u, got v%u.\n",
1043 api_ok, api_max, api_ver);
1044 else
965974a6 1045 IWL_ERR(drv, "Firmware has old API version, "
15854ef9
JB
1046 "expected v%u, got v%u.\n",
1047 api_max, api_ver);
965974a6 1048 IWL_ERR(drv, "New firmware can be obtained from "
15854ef9
JB
1049 "http://www.intellinuxwireless.org/.\n");
1050 }
1051 }
1052
4db2c9ae
DS
1053 /*
1054 * In mvm uCode there is no difference between data and instructions
1055 * sections.
1056 */
68f360dc 1057 if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
15854ef9 1058 goto try_again;
15854ef9
JB
1059
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 */
6dfa8d01 1065 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
75813bde
JB
1066 if (iwl_alloc_ucode(drv, &pieces, i))
1067 goto out_free_fw;
15854ef9
JB
1068
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 */
0692fe41 1076 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
15854ef9 1077 if (pieces.init_evtlog_size)
0692fe41 1078 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
15854ef9 1079 else
0692fe41 1080 fw->init_evtlog_size =
68f360dc 1081 drv->cfg->base_params->max_event_log_size;
0692fe41
JB
1082 fw->init_errlog_ptr = pieces.init_errlog_ptr;
1083 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
15854ef9 1084 if (pieces.inst_evtlog_size)
0692fe41 1085 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
15854ef9 1086 else
0692fe41 1087 fw->inst_evtlog_size =
68f360dc 1088 drv->cfg->base_params->max_event_log_size;
0692fe41 1089 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
15854ef9
JB
1090
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);
15854ef9 1102
ff1ffb85 1103 mutex_lock(&iwlwifi_opmode_table_mtx);
8ca151b5
JB
1104 if (fw->mvm_fw)
1105 op = &iwlwifi_opmode_table[MVM_OP_MODE];
1106 else
1107 op = &iwlwifi_opmode_table[DVM_OP_MODE];
15854ef9 1108
2b2719c7
JB
1109 IWL_INFO(drv, "loaded firmware version %s op_mode %s\n",
1110 drv->fw.fw_version, op->name);
1111
cc5f7e39
DF
1112 /* 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) {
9da987ac 1116 drv->op_mode = _iwl_op_mode_start(drv, op);
7c9c46c1 1117
daf67ce8
DC
1118 if (!drv->op_mode) {
1119 mutex_unlock(&iwlwifi_opmode_table_mtx);
7c9c46c1 1120 goto out_unbind;
daf67ce8 1121 }
cc5f7e39 1122 } else {
d4b10483 1123 load_module = true;
cc5f7e39 1124 }
ff1ffb85 1125 mutex_unlock(&iwlwifi_opmode_table_mtx);
15854ef9 1126
f69a23b7
JB
1127 /*
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);
d4b10483
JB
1133
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 */
1618b2b0
JB
1139 if (load_module) {
1140 err = request_module("%s", op->name);
8edf3fd6 1141#ifdef CONFIG_IWLWIFI_OPMODE_MODULAR
1618b2b0
JB
1142 if (err)
1143 IWL_ERR(drv,
1144 "failed to load module %s (error %d), is dynamic loading enabled?\n",
1145 op->name, err);
8edf3fd6 1146#endif
1618b2b0 1147 }
15854ef9
JB
1148 return;
1149
1150 try_again:
1151 /* try next, if any */
1152 release_firmware(ucode_raw);
965974a6 1153 if (iwl_request_firmware(drv, false))
15854ef9
JB
1154 goto out_unbind;
1155 return;
1156
75813bde 1157 out_free_fw:
965974a6
JB
1158 IWL_ERR(drv, "failed to allocate pci memory\n");
1159 iwl_dealloc_ucode(drv);
15854ef9
JB
1160 release_firmware(ucode_raw);
1161 out_unbind:
965974a6 1162 complete(&drv->request_firmware_complete);
93faaeea 1163 device_release_driver(drv->trans->dev);
15854ef9
JB
1164}
1165
87ce05a2 1166struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
9130bab1 1167 const struct iwl_cfg *cfg)
5c58edc6 1168{
965974a6 1169 struct iwl_drv *drv;
5c58edc6
EG
1170 int ret;
1171
965974a6 1172 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
eafe25e0
LC
1173 if (!drv) {
1174 ret = -ENOMEM;
1175 goto err;
1176 }
c15797e6 1177
9130bab1 1178 drv->trans = trans;
4b9844f5 1179 drv->dev = trans->dev;
68f360dc 1180 drv->cfg = cfg;
5c58edc6 1181
965974a6 1182 init_completion(&drv->request_firmware_complete);
ff1ffb85 1183 INIT_LIST_HEAD(&drv->list);
5c58edc6 1184
9da987ac
MV
1185#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");
59f547ac 1192 ret = -ENOMEM;
9da987ac
MV
1193 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");
59f547ac 1201 ret = -ENOMEM;
9da987ac
MV
1202 goto err_free_dbgfs;
1203 }
1204#endif
1205
965974a6 1206 ret = iwl_request_firmware(drv, true);
5c58edc6 1207 if (ret) {
c15797e6 1208 IWL_ERR(trans, "Couldn't request the fw\n");
9da987ac 1209 goto err_fw;
5c58edc6
EG
1210 }
1211
9da987ac
MV
1212 return drv;
1213
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);
eafe25e0 1221err:
59f547ac 1222 return ERR_PTR(ret);
5c58edc6
EG
1223}
1224
9130bab1 1225void iwl_drv_stop(struct iwl_drv *drv)
07590f08 1226{
965974a6 1227 wait_for_completion(&drv->request_firmware_complete);
2e7eb117 1228
9da987ac 1229 _iwl_op_mode_stop(drv);
07590f08 1230
965974a6 1231 iwl_dealloc_ucode(drv);
7db5b989 1232
ff1ffb85
JB
1233 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
9da987ac
MV
1243#ifdef CONFIG_IWLWIFI_DEBUGFS
1244 debugfs_remove_recursive(drv->dbgfs_drv);
1245#endif
1246
965974a6 1247 kfree(drv);
07590f08 1248}
65de7e84
JB
1249
1250
1251/* shared module parameters */
1252struct iwl_mod_params iwlwifi_mod_params = {
490953ac 1253 .restart_fw = true,
65de7e84
JB
1254 .bt_coex_active = true,
1255 .power_level = IWL_POWER_INDEX_1,
020c8767 1256 .wd_disable = true,
1504f48d 1257 .uapsd_disable = false,
65de7e84
JB
1258 /* the rest are 0 by default */
1259};
48e29340 1260IWL_EXPORT_SYMBOL(iwlwifi_mod_params);
cc5f7e39
DF
1261
1262int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
1263{
1264 int i;
1265 struct iwl_drv *drv;
9da987ac 1266 struct iwlwifi_opmode_table *op;
cc5f7e39 1267
ff1ffb85 1268 mutex_lock(&iwlwifi_opmode_table_mtx);
cc5f7e39 1269 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
9da987ac
MV
1270 op = &iwlwifi_opmode_table[i];
1271 if (strcmp(op->name, name))
cc5f7e39 1272 continue;
9da987ac
MV
1273 op->ops = ops;
1274 /* TODO: need to handle exceptional case */
1275 list_for_each_entry(drv, &op->drv, list)
1276 drv->op_mode = _iwl_op_mode_start(drv, op);
1277
ff1ffb85 1278 mutex_unlock(&iwlwifi_opmode_table_mtx);
cc5f7e39
DF
1279 return 0;
1280 }
ff1ffb85 1281 mutex_unlock(&iwlwifi_opmode_table_mtx);
cc5f7e39
DF
1282 return -EIO;
1283}
48e29340 1284IWL_EXPORT_SYMBOL(iwl_opmode_register);
cc5f7e39
DF
1285
1286void iwl_opmode_deregister(const char *name)
1287{
1288 int i;
1289 struct iwl_drv *drv;
1290
ff1ffb85 1291 mutex_lock(&iwlwifi_opmode_table_mtx);
cc5f7e39
DF
1292 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1293 if (strcmp(iwlwifi_opmode_table[i].name, name))
1294 continue;
1295 iwlwifi_opmode_table[i].ops = NULL;
1296
1297 /* call the stop routine for all devices */
9da987ac
MV
1298 list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
1299 _iwl_op_mode_stop(drv);
1300
ff1ffb85 1301 mutex_unlock(&iwlwifi_opmode_table_mtx);
cc5f7e39
DF
1302 return;
1303 }
ff1ffb85 1304 mutex_unlock(&iwlwifi_opmode_table_mtx);
cc5f7e39 1305}
48e29340 1306IWL_EXPORT_SYMBOL(iwl_opmode_deregister);
cc5f7e39
DF
1307
1308static int __init iwl_drv_init(void)
1309{
1310 int i;
1311
ff1ffb85
JB
1312 mutex_init(&iwlwifi_opmode_table_mtx);
1313
cc5f7e39
DF
1314 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
1315 INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
1316
1317 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
1318 pr_info(DRV_COPYRIGHT "\n");
1319
9da987ac
MV
1320#ifdef CONFIG_IWLWIFI_DEBUGFS
1321 /* Create the root of iwlwifi debugfs subsystem. */
1322 iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL);
1323
1324 if (!iwl_dbgfs_root)
1325 return -EFAULT;
1326#endif
1327
cc5f7e39
DF
1328 return iwl_pci_register_driver();
1329}
1330module_init(iwl_drv_init);
1331
1332static void __exit iwl_drv_exit(void)
1333{
1334 iwl_pci_unregister_driver();
9da987ac
MV
1335
1336#ifdef CONFIG_IWLWIFI_DEBUGFS
1337 debugfs_remove_recursive(iwl_dbgfs_root);
1338#endif
cc5f7e39
DF
1339}
1340module_exit(iwl_drv_exit);
65de7e84
JB
1341
1342#ifdef CONFIG_IWLWIFI_DEBUG
1343module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
1344 S_IRUGO | S_IWUSR);
1345MODULE_PARM_DESC(debug, "debug output mask");
1346#endif
1347
1348module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
1349MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
1350module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
1351MODULE_PARM_DESC(11n_disable,
205e2210 1352 "disable 11n functionality, bitmap: 1: full, 2: disable agg TX, 4: disable agg RX, 8 enable agg TX");
65de7e84
JB
1353module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
1354 int, S_IRUGO);
aed7d9ac 1355MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size (default 0)");
490953ac
EG
1356module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, bool, S_IRUGO);
1357MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)");
65de7e84
JB
1358
1359module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
1360 int, S_IRUGO);
1361MODULE_PARM_DESC(antenna_coupling,
1362 "specify antenna coupling in dB (defualt: 0 dB)");
1363
65de7e84
JB
1364module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
1365MODULE_PARM_DESC(wd_disable,
82b715c2 1366 "Disable stuck queue watchdog timer 0=system default, 1=disable (default: 1)");
65de7e84 1367
1214755c
EH
1368module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, S_IRUGO);
1369MODULE_PARM_DESC(nvm_file, "NVM file name");
1370
1504f48d
MC
1371module_param_named(uapsd_disable, iwlwifi_mod_params.uapsd_disable,
1372 bool, S_IRUGO);
1373MODULE_PARM_DESC(uapsd_disable, "disable U-APSD functionality (default: N)");
1374
65de7e84
JB
1375/*
1376 * set bt_coex_active to true, uCode will do kill/defer
1377 * every time the priority line is asserted (BT is sending signals on the
1378 * priority line in the PCIx).
1379 * set bt_coex_active to false, uCode will ignore the BT activity and
1380 * perform the normal operation
1381 *
1382 * User might experience transmit issue on some platform due to WiFi/BT
1383 * co-exist problem. The possible behaviors are:
1384 * Able to scan and finding all the available AP
1385 * Not able to associate with any AP
1386 * On those platforms, WiFi communication can be restored by set
1387 * "bt_coex_active" module parameter to "false"
1388 *
1389 * default: bt_coex_active = true (BT_COEX_ENABLE)
1390 */
1391module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
1392 bool, S_IRUGO);
1393MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
1394
1395module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
1396MODULE_PARM_DESC(led_mode, "0=system default, "
1397 "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
1398
1399module_param_named(power_save, iwlwifi_mod_params.power_save,
1400 bool, S_IRUGO);
1401MODULE_PARM_DESC(power_save,
1402 "enable WiFi power management (default: disable)");
1403
1404module_param_named(power_level, iwlwifi_mod_params.power_level,
1405 int, S_IRUGO);
1406MODULE_PARM_DESC(power_level,
1407 "default power save level (range from 1 - 5, default: 1)");
c2d20201
EG
1408
1409module_param_named(fw_monitor, iwlwifi_mod_params.fw_monitor, bool, S_IRUGO);
1410MODULE_PARM_DESC(dbgm,
1411 "firmware monitor - to debug FW (default: false - needs lots of memory)");