iwlwifi: use correct fw file in 8000 b-step
[cascardo/linux.git] / drivers / net / wireless / iwlwifi / iwl-drv.c
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  *
8  * Copyright(c) 2007 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of version 2 of the GNU General Public License as
13  * published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23  * USA
24  *
25  * The full GNU General Public License is included in this distribution
26  * in the file called COPYING.
27  *
28  * Contact Information:
29  *  Intel Linux Wireless <ilw@linux.intel.com>
30  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31  *
32  * BSD LICENSE
33  *
34  * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
35  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  *
42  *  * Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  *  * Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in
46  *    the documentation and/or other materials provided with the
47  *    distribution.
48  *  * Neither the name Intel Corporation nor the names of its
49  *    contributors may be used to endorse or promote products derived
50  *    from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63  *
64  *****************************************************************************/
65 #include <linux/completion.h>
66 #include <linux/dma-mapping.h>
67 #include <linux/firmware.h>
68 #include <linux/module.h>
69 #include <linux/vmalloc.h>
70
71 #include "iwl-drv.h"
72 #include "iwl-csr.h"
73 #include "iwl-debug.h"
74 #include "iwl-trans.h"
75 #include "iwl-op-mode.h"
76 #include "iwl-agn-hw.h"
77 #include "iwl-fw.h"
78 #include "iwl-config.h"
79 #include "iwl-modparams.h"
80
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
100 MODULE_DESCRIPTION(DRV_DESCRIPTION);
101 MODULE_VERSION(DRV_VERSION);
102 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
103 MODULE_LICENSE("GPL");
104
105 #ifdef CONFIG_IWLWIFI_DEBUGFS
106 static struct dentry *iwl_dbgfs_root;
107 #endif
108
109 /**
110  * struct iwl_drv - drv common data
111  * @list: list of drv structures using this opmode
112  * @fw: the iwl_fw structure
113  * @op_mode: the running op_mode
114  * @trans: transport layer
115  * @dev: for debug prints only
116  * @cfg: configuration struct
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  */
121 struct iwl_drv {
122         struct list_head list;
123         struct iwl_fw fw;
124
125         struct iwl_op_mode *op_mode;
126         struct iwl_trans *trans;
127         struct device *dev;
128         const struct iwl_cfg *cfg;
129
130         int fw_index;                   /* firmware we're trying to load */
131         char firmware_name[32];         /* name of firmware file to load */
132
133         struct completion request_firmware_complete;
134
135 #ifdef CONFIG_IWLWIFI_DEBUGFS
136         struct dentry *dbgfs_drv;
137         struct dentry *dbgfs_trans;
138         struct dentry *dbgfs_op_mode;
139 #endif
140 };
141
142 enum {
143         DVM_OP_MODE =   0,
144         MVM_OP_MODE =   1,
145 };
146
147 /* Protects the table contents, i.e. the ops pointer & drv list */
148 static struct mutex iwlwifi_opmode_table_mtx;
149 static 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 */
154         [DVM_OP_MODE] = { .name = "iwldvm", .ops = NULL },
155         [MVM_OP_MODE] = { .name = "iwlmvm", .ops = NULL },
156 };
157
158 #define IWL_DEFAULT_SCAN_CHANNELS 40
159
160 /*
161  * struct fw_sec: Just for the image parsing proccess.
162  * For the fw storage we are using struct fw_desc.
163  */
164 struct 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
170 static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
171 {
172         vfree(desc->data);
173         desc->data = NULL;
174         desc->len = 0;
175 }
176
177 static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
178 {
179         int i;
180         for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
181                 iwl_free_fw_desc(drv, &img->sec[i]);
182 }
183
184 static void iwl_dealloc_ucode(struct iwl_drv *drv)
185 {
186         int i;
187         for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
188                 iwl_free_fw_img(drv, drv->fw.img + i);
189 }
190
191 static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
192                              struct fw_sec *sec)
193 {
194         void *data;
195
196         desc->data = NULL;
197
198         if (!sec || !sec->size)
199                 return -EINVAL;
200
201         data = vmalloc(sec->size);
202         if (!data)
203                 return -ENOMEM;
204
205         desc->len = sec->size;
206         desc->offset = sec->offset;
207         memcpy(data, sec->data, desc->len);
208         desc->data = data;
209
210         return 0;
211 }
212
213 static void iwl_req_fw_callback(const struct firmware *ucode_raw,
214                                 void *context);
215
216 #define UCODE_EXPERIMENTAL_INDEX        100
217 #define UCODE_EXPERIMENTAL_TAG          "exp"
218
219 static int iwl_request_firmware(struct iwl_drv *drv, bool first)
220 {
221         const char *name_pre = drv->cfg->fw_name_pre;
222         char tag[8];
223
224         if (first) {
225 #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
226                 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
227                 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
228         } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
229 #endif
230                 drv->fw_index = drv->cfg->ucode_api_max;
231                 sprintf(tag, "%d", drv->fw_index);
232         } else {
233                 drv->fw_index--;
234                 sprintf(tag, "%d", drv->fw_index);
235         }
236
237         if (drv->fw_index < drv->cfg->ucode_api_min) {
238                 IWL_ERR(drv, "no suitable firmware found!\n");
239                 return -ENOENT;
240         }
241
242         snprintf(drv->firmware_name, sizeof(drv->firmware_name), "%s%s.ucode",
243                  name_pre, tag);
244
245         /*
246          * Starting 8000B - FW name format has changed. This overwrites the
247          * previous name and uses the new format.
248          *
249          * TODO:
250          * Once there is only one supported step for 8000 family - delete this!
251          */
252         if (drv->trans->cfg->device_family == IWL_DEVICE_FAMILY_8000) {
253                 char rev_step[2] = {
254                         'A' + CSR_HW_REV_STEP(drv->trans->hw_rev), 0
255                 };
256
257                 /* A-step doesn't have an indication */
258                 if (CSR_HW_REV_STEP(drv->trans->hw_rev) == SILICON_A_STEP)
259                         rev_step[0] = 0;
260
261                 /*
262                  * If hw_rev wasn't set yet - default as B-step. If it IS A-step
263                  * we'll reload that FW later instead.
264                  */
265                 if (drv->trans->hw_rev == 0)
266                         rev_step[0] = 'B';
267
268                 snprintf(drv->firmware_name, sizeof(drv->firmware_name),
269                          "%s%s-%s.ucode", name_pre, rev_step, tag);
270         }
271
272         IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
273                        (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
274                                 ? "EXPERIMENTAL " : "",
275                        drv->firmware_name);
276
277         return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
278                                        drv->trans->dev,
279                                        GFP_KERNEL, drv, iwl_req_fw_callback);
280 }
281
282 struct fw_img_parsing {
283         struct fw_sec sec[IWL_UCODE_SECTION_MAX];
284         int sec_counter;
285 };
286
287 /*
288  * struct fw_sec_parsing: to extract fw section and it's offset from tlv
289  */
290 struct fw_sec_parsing {
291         __le32 offset;
292         const u8 data[];
293 } __packed;
294
295 /**
296  * struct iwl_tlv_calib_data - parse the default calib data from TLV
297  *
298  * @ucode_type: the uCode to which the following default calib relates.
299  * @calib: default calibrations.
300  */
301 struct iwl_tlv_calib_data {
302         __le32 ucode_type;
303         struct iwl_tlv_calib_ctrl calib;
304 } __packed;
305
306 struct iwl_firmware_pieces {
307         struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
308
309         u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
310         u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
311 };
312
313 /*
314  * These functions are just to extract uCode section data from the pieces
315  * structure.
316  */
317 static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
318                               enum iwl_ucode_type type,
319                               int  sec)
320 {
321         return &pieces->img[type].sec[sec];
322 }
323
324 static void set_sec_data(struct iwl_firmware_pieces *pieces,
325                          enum iwl_ucode_type type,
326                          int sec,
327                          const void *data)
328 {
329         pieces->img[type].sec[sec].data = data;
330 }
331
332 static void set_sec_size(struct iwl_firmware_pieces *pieces,
333                          enum iwl_ucode_type type,
334                          int sec,
335                          size_t size)
336 {
337         pieces->img[type].sec[sec].size = size;
338 }
339
340 static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
341                            enum iwl_ucode_type type,
342                            int sec)
343 {
344         return pieces->img[type].sec[sec].size;
345 }
346
347 static void set_sec_offset(struct iwl_firmware_pieces *pieces,
348                            enum iwl_ucode_type type,
349                            int sec,
350                            u32 offset)
351 {
352         pieces->img[type].sec[sec].offset = offset;
353 }
354
355 static int iwl_store_cscheme(struct iwl_fw *fw, const u8 *data, const u32 len)
356 {
357         int i, j;
358         struct iwl_fw_cscheme_list *l = (struct iwl_fw_cscheme_list *)data;
359         struct iwl_fw_cipher_scheme *fwcs;
360         struct ieee80211_cipher_scheme *cs;
361         u32 cipher;
362
363         if (len < sizeof(*l) ||
364             len < sizeof(l->size) + l->size * sizeof(l->cs[0]))
365                 return -EINVAL;
366
367         for (i = 0, j = 0; i < IWL_UCODE_MAX_CS && i < l->size; i++) {
368                 fwcs = &l->cs[j];
369                 cipher = le32_to_cpu(fwcs->cipher);
370
371                 /* we skip schemes with zero cipher suite selector */
372                 if (!cipher)
373                         continue;
374
375                 cs = &fw->cs[j++];
376                 cs->cipher = cipher;
377                 cs->iftype = BIT(NL80211_IFTYPE_STATION);
378                 cs->hdr_len = fwcs->hdr_len;
379                 cs->pn_len = fwcs->pn_len;
380                 cs->pn_off = fwcs->pn_off;
381                 cs->key_idx_off = fwcs->key_idx_off;
382                 cs->key_idx_mask = fwcs->key_idx_mask;
383                 cs->key_idx_shift = fwcs->key_idx_shift;
384                 cs->mic_len = fwcs->mic_len;
385         }
386
387         return 0;
388 }
389
390 /*
391  * Gets uCode section from tlv.
392  */
393 static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
394                                const void *data, enum iwl_ucode_type type,
395                                int size)
396 {
397         struct fw_img_parsing *img;
398         struct fw_sec *sec;
399         struct fw_sec_parsing *sec_parse;
400
401         if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
402                 return -1;
403
404         sec_parse = (struct fw_sec_parsing *)data;
405
406         img = &pieces->img[type];
407         sec = &img->sec[img->sec_counter];
408
409         sec->offset = le32_to_cpu(sec_parse->offset);
410         sec->data = sec_parse->data;
411         sec->size = size - sizeof(sec_parse->offset);
412
413         ++img->sec_counter;
414
415         return 0;
416 }
417
418 static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
419 {
420         struct iwl_tlv_calib_data *def_calib =
421                                         (struct iwl_tlv_calib_data *)data;
422         u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
423         if (ucode_type >= IWL_UCODE_TYPE_MAX) {
424                 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
425                         ucode_type);
426                 return -EINVAL;
427         }
428         drv->fw.default_calib[ucode_type].flow_trigger =
429                 def_calib->calib.flow_trigger;
430         drv->fw.default_calib[ucode_type].event_trigger =
431                 def_calib->calib.event_trigger;
432
433         return 0;
434 }
435
436 static int iwl_set_ucode_api_flags(struct iwl_drv *drv, const u8 *data,
437                                    struct iwl_ucode_capabilities *capa)
438 {
439         const struct iwl_ucode_api *ucode_api = (void *)data;
440         u32 api_index = le32_to_cpu(ucode_api->api_index);
441
442         if (api_index >= IWL_API_ARRAY_SIZE) {
443                 IWL_ERR(drv, "api_index larger than supported by driver\n");
444                 return -EINVAL;
445         }
446
447         capa->api[api_index] = le32_to_cpu(ucode_api->api_flags);
448
449         return 0;
450 }
451
452 static int iwl_set_ucode_capabilities(struct iwl_drv *drv, const u8 *data,
453                                       struct iwl_ucode_capabilities *capa)
454 {
455         const struct iwl_ucode_capa *ucode_capa = (void *)data;
456         u32 api_index = le32_to_cpu(ucode_capa->api_index);
457
458         if (api_index >= IWL_CAPABILITIES_ARRAY_SIZE) {
459                 IWL_ERR(drv, "api_index larger than supported by driver\n");
460                 return -EINVAL;
461         }
462
463         capa->capa[api_index] = le32_to_cpu(ucode_capa->api_capa);
464
465         return 0;
466 }
467
468 static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
469                                     const struct firmware *ucode_raw,
470                                     struct iwl_firmware_pieces *pieces)
471 {
472         struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
473         u32 api_ver, hdr_size, build;
474         char buildstr[25];
475         const u8 *src;
476
477         drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
478         api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
479
480         switch (api_ver) {
481         default:
482                 hdr_size = 28;
483                 if (ucode_raw->size < hdr_size) {
484                         IWL_ERR(drv, "File size too small!\n");
485                         return -EINVAL;
486                 }
487                 build = le32_to_cpu(ucode->u.v2.build);
488                 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
489                              le32_to_cpu(ucode->u.v2.inst_size));
490                 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
491                              le32_to_cpu(ucode->u.v2.data_size));
492                 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
493                              le32_to_cpu(ucode->u.v2.init_size));
494                 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
495                              le32_to_cpu(ucode->u.v2.init_data_size));
496                 src = ucode->u.v2.data;
497                 break;
498         case 0:
499         case 1:
500         case 2:
501                 hdr_size = 24;
502                 if (ucode_raw->size < hdr_size) {
503                         IWL_ERR(drv, "File size too small!\n");
504                         return -EINVAL;
505                 }
506                 build = 0;
507                 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
508                              le32_to_cpu(ucode->u.v1.inst_size));
509                 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
510                              le32_to_cpu(ucode->u.v1.data_size));
511                 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
512                              le32_to_cpu(ucode->u.v1.init_size));
513                 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
514                              le32_to_cpu(ucode->u.v1.init_data_size));
515                 src = ucode->u.v1.data;
516                 break;
517         }
518
519         if (build)
520                 sprintf(buildstr, " build %u%s", build,
521                        (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
522                                 ? " (EXP)" : "");
523         else
524                 buildstr[0] = '\0';
525
526         snprintf(drv->fw.fw_version,
527                  sizeof(drv->fw.fw_version),
528                  "%u.%u.%u.%u%s",
529                  IWL_UCODE_MAJOR(drv->fw.ucode_ver),
530                  IWL_UCODE_MINOR(drv->fw.ucode_ver),
531                  IWL_UCODE_API(drv->fw.ucode_ver),
532                  IWL_UCODE_SERIAL(drv->fw.ucode_ver),
533                  buildstr);
534
535         /* Verify size of file vs. image size info in file's header */
536
537         if (ucode_raw->size != hdr_size +
538             get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
539             get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
540             get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
541             get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
542
543                 IWL_ERR(drv,
544                         "uCode file size %d does not match expected size\n",
545                         (int)ucode_raw->size);
546                 return -EINVAL;
547         }
548
549
550         set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
551         src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
552         set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
553                        IWLAGN_RTC_INST_LOWER_BOUND);
554         set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
555         src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
556         set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
557                        IWLAGN_RTC_DATA_LOWER_BOUND);
558         set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
559         src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
560         set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
561                        IWLAGN_RTC_INST_LOWER_BOUND);
562         set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
563         src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
564         set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
565                        IWLAGN_RTC_DATA_LOWER_BOUND);
566         return 0;
567 }
568
569 static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
570                                 const struct firmware *ucode_raw,
571                                 struct iwl_firmware_pieces *pieces,
572                                 struct iwl_ucode_capabilities *capa)
573 {
574         struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
575         struct iwl_ucode_tlv *tlv;
576         size_t len = ucode_raw->size;
577         const u8 *data;
578         u32 tlv_len;
579         enum iwl_ucode_tlv_type tlv_type;
580         const u8 *tlv_data;
581         char buildstr[25];
582         u32 build;
583         int num_of_cpus;
584
585         if (len < sizeof(*ucode)) {
586                 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
587                 return -EINVAL;
588         }
589
590         if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
591                 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
592                         le32_to_cpu(ucode->magic));
593                 return -EINVAL;
594         }
595
596         drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
597         memcpy(drv->fw.human_readable, ucode->human_readable,
598                sizeof(drv->fw.human_readable));
599         build = le32_to_cpu(ucode->build);
600
601         if (build)
602                 sprintf(buildstr, " build %u%s", build,
603                        (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
604                                 ? " (EXP)" : "");
605         else
606                 buildstr[0] = '\0';
607
608         snprintf(drv->fw.fw_version,
609                  sizeof(drv->fw.fw_version),
610                  "%u.%u.%u.%u%s",
611                  IWL_UCODE_MAJOR(drv->fw.ucode_ver),
612                  IWL_UCODE_MINOR(drv->fw.ucode_ver),
613                  IWL_UCODE_API(drv->fw.ucode_ver),
614                  IWL_UCODE_SERIAL(drv->fw.ucode_ver),
615                  buildstr);
616
617         data = ucode->data;
618
619         len -= sizeof(*ucode);
620
621         while (len >= sizeof(*tlv)) {
622                 len -= sizeof(*tlv);
623                 tlv = (void *)data;
624
625                 tlv_len = le32_to_cpu(tlv->length);
626                 tlv_type = le32_to_cpu(tlv->type);
627                 tlv_data = tlv->data;
628
629                 if (len < tlv_len) {
630                         IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
631                                 len, tlv_len);
632                         return -EINVAL;
633                 }
634                 len -= ALIGN(tlv_len, 4);
635                 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
636
637                 switch (tlv_type) {
638                 case IWL_UCODE_TLV_INST:
639                         set_sec_data(pieces, IWL_UCODE_REGULAR,
640                                      IWL_UCODE_SECTION_INST, tlv_data);
641                         set_sec_size(pieces, IWL_UCODE_REGULAR,
642                                      IWL_UCODE_SECTION_INST, tlv_len);
643                         set_sec_offset(pieces, IWL_UCODE_REGULAR,
644                                        IWL_UCODE_SECTION_INST,
645                                        IWLAGN_RTC_INST_LOWER_BOUND);
646                         break;
647                 case IWL_UCODE_TLV_DATA:
648                         set_sec_data(pieces, IWL_UCODE_REGULAR,
649                                      IWL_UCODE_SECTION_DATA, tlv_data);
650                         set_sec_size(pieces, IWL_UCODE_REGULAR,
651                                      IWL_UCODE_SECTION_DATA, tlv_len);
652                         set_sec_offset(pieces, IWL_UCODE_REGULAR,
653                                        IWL_UCODE_SECTION_DATA,
654                                        IWLAGN_RTC_DATA_LOWER_BOUND);
655                         break;
656                 case IWL_UCODE_TLV_INIT:
657                         set_sec_data(pieces, IWL_UCODE_INIT,
658                                      IWL_UCODE_SECTION_INST, tlv_data);
659                         set_sec_size(pieces, IWL_UCODE_INIT,
660                                      IWL_UCODE_SECTION_INST, tlv_len);
661                         set_sec_offset(pieces, IWL_UCODE_INIT,
662                                        IWL_UCODE_SECTION_INST,
663                                        IWLAGN_RTC_INST_LOWER_BOUND);
664                         break;
665                 case IWL_UCODE_TLV_INIT_DATA:
666                         set_sec_data(pieces, IWL_UCODE_INIT,
667                                      IWL_UCODE_SECTION_DATA, tlv_data);
668                         set_sec_size(pieces, IWL_UCODE_INIT,
669                                      IWL_UCODE_SECTION_DATA, tlv_len);
670                         set_sec_offset(pieces, IWL_UCODE_INIT,
671                                        IWL_UCODE_SECTION_DATA,
672                                        IWLAGN_RTC_DATA_LOWER_BOUND);
673                         break;
674                 case IWL_UCODE_TLV_BOOT:
675                         IWL_ERR(drv, "Found unexpected BOOT ucode\n");
676                         break;
677                 case IWL_UCODE_TLV_PROBE_MAX_LEN:
678                         if (tlv_len != sizeof(u32))
679                                 goto invalid_tlv_len;
680                         capa->max_probe_length =
681                                         le32_to_cpup((__le32 *)tlv_data);
682                         break;
683                 case IWL_UCODE_TLV_PAN:
684                         if (tlv_len)
685                                 goto invalid_tlv_len;
686                         capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
687                         break;
688                 case IWL_UCODE_TLV_FLAGS:
689                         /* must be at least one u32 */
690                         if (tlv_len < sizeof(u32))
691                                 goto invalid_tlv_len;
692                         /* and a proper number of u32s */
693                         if (tlv_len % sizeof(u32))
694                                 goto invalid_tlv_len;
695                         /*
696                          * This driver only reads the first u32 as
697                          * right now no more features are defined,
698                          * if that changes then either the driver
699                          * will not work with the new firmware, or
700                          * it'll not take advantage of new features.
701                          */
702                         capa->flags = le32_to_cpup((__le32 *)tlv_data);
703                         break;
704                 case IWL_UCODE_TLV_API_CHANGES_SET:
705                         if (tlv_len != sizeof(struct iwl_ucode_api))
706                                 goto invalid_tlv_len;
707                         if (iwl_set_ucode_api_flags(drv, tlv_data, capa))
708                                 goto tlv_error;
709                         break;
710                 case IWL_UCODE_TLV_ENABLED_CAPABILITIES:
711                         if (tlv_len != sizeof(struct iwl_ucode_capa))
712                                 goto invalid_tlv_len;
713                         if (iwl_set_ucode_capabilities(drv, tlv_data, capa))
714                                 goto tlv_error;
715                         break;
716                 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
717                         if (tlv_len != sizeof(u32))
718                                 goto invalid_tlv_len;
719                         pieces->init_evtlog_ptr =
720                                         le32_to_cpup((__le32 *)tlv_data);
721                         break;
722                 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
723                         if (tlv_len != sizeof(u32))
724                                 goto invalid_tlv_len;
725                         pieces->init_evtlog_size =
726                                         le32_to_cpup((__le32 *)tlv_data);
727                         break;
728                 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
729                         if (tlv_len != sizeof(u32))
730                                 goto invalid_tlv_len;
731                         pieces->init_errlog_ptr =
732                                         le32_to_cpup((__le32 *)tlv_data);
733                         break;
734                 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
735                         if (tlv_len != sizeof(u32))
736                                 goto invalid_tlv_len;
737                         pieces->inst_evtlog_ptr =
738                                         le32_to_cpup((__le32 *)tlv_data);
739                         break;
740                 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
741                         if (tlv_len != sizeof(u32))
742                                 goto invalid_tlv_len;
743                         pieces->inst_evtlog_size =
744                                         le32_to_cpup((__le32 *)tlv_data);
745                         break;
746                 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
747                         if (tlv_len != sizeof(u32))
748                                 goto invalid_tlv_len;
749                         pieces->inst_errlog_ptr =
750                                         le32_to_cpup((__le32 *)tlv_data);
751                         break;
752                 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
753                         if (tlv_len)
754                                 goto invalid_tlv_len;
755                         drv->fw.enhance_sensitivity_table = true;
756                         break;
757                 case IWL_UCODE_TLV_WOWLAN_INST:
758                         set_sec_data(pieces, IWL_UCODE_WOWLAN,
759                                      IWL_UCODE_SECTION_INST, tlv_data);
760                         set_sec_size(pieces, IWL_UCODE_WOWLAN,
761                                      IWL_UCODE_SECTION_INST, tlv_len);
762                         set_sec_offset(pieces, IWL_UCODE_WOWLAN,
763                                        IWL_UCODE_SECTION_INST,
764                                        IWLAGN_RTC_INST_LOWER_BOUND);
765                         break;
766                 case IWL_UCODE_TLV_WOWLAN_DATA:
767                         set_sec_data(pieces, IWL_UCODE_WOWLAN,
768                                      IWL_UCODE_SECTION_DATA, tlv_data);
769                         set_sec_size(pieces, IWL_UCODE_WOWLAN,
770                                      IWL_UCODE_SECTION_DATA, tlv_len);
771                         set_sec_offset(pieces, IWL_UCODE_WOWLAN,
772                                        IWL_UCODE_SECTION_DATA,
773                                        IWLAGN_RTC_DATA_LOWER_BOUND);
774                         break;
775                 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
776                         if (tlv_len != sizeof(u32))
777                                 goto invalid_tlv_len;
778                         capa->standard_phy_calibration_size =
779                                         le32_to_cpup((__le32 *)tlv_data);
780                         break;
781                  case IWL_UCODE_TLV_SEC_RT:
782                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
783                                             tlv_len);
784                         drv->fw.mvm_fw = true;
785                         break;
786                 case IWL_UCODE_TLV_SEC_INIT:
787                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
788                                             tlv_len);
789                         drv->fw.mvm_fw = true;
790                         break;
791                 case IWL_UCODE_TLV_SEC_WOWLAN:
792                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
793                                             tlv_len);
794                         drv->fw.mvm_fw = true;
795                         break;
796                 case IWL_UCODE_TLV_DEF_CALIB:
797                         if (tlv_len != sizeof(struct iwl_tlv_calib_data))
798                                 goto invalid_tlv_len;
799                         if (iwl_set_default_calib(drv, tlv_data))
800                                 goto tlv_error;
801                         break;
802                 case IWL_UCODE_TLV_PHY_SKU:
803                         if (tlv_len != sizeof(u32))
804                                 goto invalid_tlv_len;
805                         drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
806                         drv->fw.valid_tx_ant = (drv->fw.phy_config &
807                                                 FW_PHY_CFG_TX_CHAIN) >>
808                                                 FW_PHY_CFG_TX_CHAIN_POS;
809                         drv->fw.valid_rx_ant = (drv->fw.phy_config &
810                                                 FW_PHY_CFG_RX_CHAIN) >>
811                                                 FW_PHY_CFG_RX_CHAIN_POS;
812                         break;
813                  case IWL_UCODE_TLV_SECURE_SEC_RT:
814                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
815                                             tlv_len);
816                         drv->fw.mvm_fw = true;
817                         break;
818                 case IWL_UCODE_TLV_SECURE_SEC_INIT:
819                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
820                                             tlv_len);
821                         drv->fw.mvm_fw = true;
822                         break;
823                 case IWL_UCODE_TLV_SECURE_SEC_WOWLAN:
824                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
825                                             tlv_len);
826                         drv->fw.mvm_fw = true;
827                         break;
828                 case IWL_UCODE_TLV_NUM_OF_CPU:
829                         if (tlv_len != sizeof(u32))
830                                 goto invalid_tlv_len;
831                         num_of_cpus =
832                                 le32_to_cpup((__le32 *)tlv_data);
833
834                         if (num_of_cpus == 2) {
835                                 drv->fw.img[IWL_UCODE_REGULAR].is_dual_cpus =
836                                         true;
837                                 drv->fw.img[IWL_UCODE_INIT].is_dual_cpus =
838                                         true;
839                                 drv->fw.img[IWL_UCODE_WOWLAN].is_dual_cpus =
840                                         true;
841                         } else if ((num_of_cpus > 2) || (num_of_cpus < 1)) {
842                                 IWL_ERR(drv, "Driver support upto 2 CPUs\n");
843                                 return -EINVAL;
844                         }
845                         break;
846                 case IWL_UCODE_TLV_CSCHEME:
847                         if (iwl_store_cscheme(&drv->fw, tlv_data, tlv_len))
848                                 goto invalid_tlv_len;
849                         break;
850                 case IWL_UCODE_TLV_N_SCAN_CHANNELS:
851                         if (tlv_len != sizeof(u32))
852                                 goto invalid_tlv_len;
853                         capa->n_scan_channels =
854                                 le32_to_cpup((__le32 *)tlv_data);
855                         break;
856                 default:
857                         IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
858                         break;
859                 }
860         }
861
862         if (len) {
863                 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
864                 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
865                 return -EINVAL;
866         }
867
868         return 0;
869
870  invalid_tlv_len:
871         IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
872  tlv_error:
873         iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
874
875         return -EINVAL;
876 }
877
878 static int iwl_alloc_ucode(struct iwl_drv *drv,
879                            struct iwl_firmware_pieces *pieces,
880                            enum iwl_ucode_type type)
881 {
882         int i;
883         for (i = 0;
884              i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
885              i++)
886                 if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
887                                       get_sec(pieces, type, i)))
888                         return -ENOMEM;
889         return 0;
890 }
891
892 static int validate_sec_sizes(struct iwl_drv *drv,
893                               struct iwl_firmware_pieces *pieces,
894                               const struct iwl_cfg *cfg)
895 {
896         IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
897                 get_sec_size(pieces, IWL_UCODE_REGULAR,
898                              IWL_UCODE_SECTION_INST));
899         IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
900                 get_sec_size(pieces, IWL_UCODE_REGULAR,
901                              IWL_UCODE_SECTION_DATA));
902         IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
903                 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
904         IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
905                 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
906
907         /* Verify that uCode images will fit in card's SRAM. */
908         if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
909                                                         cfg->max_inst_size) {
910                 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
911                         get_sec_size(pieces, IWL_UCODE_REGULAR,
912                                                 IWL_UCODE_SECTION_INST));
913                 return -1;
914         }
915
916         if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
917                                                         cfg->max_data_size) {
918                 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
919                         get_sec_size(pieces, IWL_UCODE_REGULAR,
920                                                 IWL_UCODE_SECTION_DATA));
921                 return -1;
922         }
923
924          if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
925                                                         cfg->max_inst_size) {
926                 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
927                         get_sec_size(pieces, IWL_UCODE_INIT,
928                                                 IWL_UCODE_SECTION_INST));
929                 return -1;
930         }
931
932         if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
933                                                         cfg->max_data_size) {
934                 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
935                         get_sec_size(pieces, IWL_UCODE_REGULAR,
936                                                 IWL_UCODE_SECTION_DATA));
937                 return -1;
938         }
939         return 0;
940 }
941
942 static struct iwl_op_mode *
943 _iwl_op_mode_start(struct iwl_drv *drv, struct iwlwifi_opmode_table *op)
944 {
945         const struct iwl_op_mode_ops *ops = op->ops;
946         struct dentry *dbgfs_dir = NULL;
947         struct iwl_op_mode *op_mode = NULL;
948
949 #ifdef CONFIG_IWLWIFI_DEBUGFS
950         drv->dbgfs_op_mode = debugfs_create_dir(op->name,
951                                                 drv->dbgfs_drv);
952         if (!drv->dbgfs_op_mode) {
953                 IWL_ERR(drv,
954                         "failed to create opmode debugfs directory\n");
955                 return op_mode;
956         }
957         dbgfs_dir = drv->dbgfs_op_mode;
958 #endif
959
960         op_mode = ops->start(drv->trans, drv->cfg, &drv->fw, dbgfs_dir);
961
962 #ifdef CONFIG_IWLWIFI_DEBUGFS
963         if (!op_mode) {
964                 debugfs_remove_recursive(drv->dbgfs_op_mode);
965                 drv->dbgfs_op_mode = NULL;
966         }
967 #endif
968
969         return op_mode;
970 }
971
972 static void _iwl_op_mode_stop(struct iwl_drv *drv)
973 {
974         /* op_mode can be NULL if its start failed */
975         if (drv->op_mode) {
976                 iwl_op_mode_stop(drv->op_mode);
977                 drv->op_mode = NULL;
978
979 #ifdef CONFIG_IWLWIFI_DEBUGFS
980                 debugfs_remove_recursive(drv->dbgfs_op_mode);
981                 drv->dbgfs_op_mode = NULL;
982 #endif
983         }
984 }
985
986 /**
987  * iwl_req_fw_callback - callback when firmware was loaded
988  *
989  * If loaded successfully, copies the firmware into buffers
990  * for the card to fetch (via DMA).
991  */
992 static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context)
993 {
994         struct iwl_drv *drv = context;
995         struct iwl_fw *fw = &drv->fw;
996         struct iwl_ucode_header *ucode;
997         struct iwlwifi_opmode_table *op;
998         int err;
999         struct iwl_firmware_pieces pieces;
1000         const unsigned int api_max = drv->cfg->ucode_api_max;
1001         unsigned int api_ok = drv->cfg->ucode_api_ok;
1002         const unsigned int api_min = drv->cfg->ucode_api_min;
1003         u32 api_ver;
1004         int i;
1005         bool load_module = false;
1006         u32 hw_rev = drv->trans->hw_rev;
1007
1008         fw->ucode_capa.max_probe_length = IWL_DEFAULT_MAX_PROBE_LENGTH;
1009         fw->ucode_capa.standard_phy_calibration_size =
1010                         IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1011         fw->ucode_capa.n_scan_channels = IWL_DEFAULT_SCAN_CHANNELS;
1012
1013         if (!api_ok)
1014                 api_ok = api_max;
1015
1016         memset(&pieces, 0, sizeof(pieces));
1017
1018         if (!ucode_raw) {
1019                 if (drv->fw_index <= api_ok)
1020                         IWL_ERR(drv,
1021                                 "request for firmware file '%s' failed.\n",
1022                                 drv->firmware_name);
1023                 goto try_again;
1024         }
1025
1026         IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
1027                        drv->firmware_name, ucode_raw->size);
1028
1029         /* Make sure that we got at least the API version number */
1030         if (ucode_raw->size < 4) {
1031                 IWL_ERR(drv, "File size way too small!\n");
1032                 goto try_again;
1033         }
1034
1035         /* Data from ucode file:  header followed by uCode images */
1036         ucode = (struct iwl_ucode_header *)ucode_raw->data;
1037
1038         if (ucode->ver)
1039                 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
1040         else
1041                 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
1042                                            &fw->ucode_capa);
1043
1044         if (err)
1045                 goto try_again;
1046
1047         api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
1048
1049         /*
1050          * api_ver should match the api version forming part of the
1051          * firmware filename ... but we don't check for that and only rely
1052          * on the API version read from firmware header from here on forward
1053          */
1054         /* no api version check required for experimental uCode */
1055         if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
1056                 if (api_ver < api_min || api_ver > api_max) {
1057                         IWL_ERR(drv,
1058                                 "Driver unable to support your firmware API. "
1059                                 "Driver supports v%u, firmware is v%u.\n",
1060                                 api_max, api_ver);
1061                         goto try_again;
1062                 }
1063
1064                 if (api_ver < api_ok) {
1065                         if (api_ok != api_max)
1066                                 IWL_ERR(drv, "Firmware has old API version, "
1067                                         "expected v%u through v%u, got v%u.\n",
1068                                         api_ok, api_max, api_ver);
1069                         else
1070                                 IWL_ERR(drv, "Firmware has old API version, "
1071                                         "expected v%u, got v%u.\n",
1072                                         api_max, api_ver);
1073                         IWL_ERR(drv, "New firmware can be obtained from "
1074                                       "http://www.intellinuxwireless.org/.\n");
1075                 }
1076         }
1077
1078         /*
1079          * In mvm uCode there is no difference between data and instructions
1080          * sections.
1081          */
1082         if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
1083                 goto try_again;
1084
1085         /* Allocate ucode buffers for card's bus-master loading ... */
1086
1087         /* Runtime instructions and 2 copies of data:
1088          * 1) unmodified from disk
1089          * 2) backup cache for save/restore during power-downs */
1090         for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
1091                 if (iwl_alloc_ucode(drv, &pieces, i))
1092                         goto out_free_fw;
1093
1094         /* Now that we can no longer fail, copy information */
1095
1096         /*
1097          * The (size - 16) / 12 formula is based on the information recorded
1098          * for each event, which is of mode 1 (including timestamp) for all
1099          * new microcodes that include this information.
1100          */
1101         fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
1102         if (pieces.init_evtlog_size)
1103                 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
1104         else
1105                 fw->init_evtlog_size =
1106                         drv->cfg->base_params->max_event_log_size;
1107         fw->init_errlog_ptr = pieces.init_errlog_ptr;
1108         fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
1109         if (pieces.inst_evtlog_size)
1110                 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
1111         else
1112                 fw->inst_evtlog_size =
1113                         drv->cfg->base_params->max_event_log_size;
1114         fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
1115
1116         /*
1117          * figure out the offset of chain noise reset and gain commands
1118          * base on the size of standard phy calibration commands table size
1119          */
1120         if (fw->ucode_capa.standard_phy_calibration_size >
1121             IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
1122                 fw->ucode_capa.standard_phy_calibration_size =
1123                         IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1124
1125         /* We have our copies now, allow OS release its copies */
1126         release_firmware(ucode_raw);
1127
1128         mutex_lock(&iwlwifi_opmode_table_mtx);
1129         if (fw->mvm_fw)
1130                 op = &iwlwifi_opmode_table[MVM_OP_MODE];
1131         else
1132                 op = &iwlwifi_opmode_table[DVM_OP_MODE];
1133
1134         IWL_INFO(drv, "loaded firmware version %s op_mode %s\n",
1135                  drv->fw.fw_version, op->name);
1136
1137         /* add this device to the list of devices using this op_mode */
1138         list_add_tail(&drv->list, &op->drv);
1139
1140         if (op->ops) {
1141                 drv->op_mode = _iwl_op_mode_start(drv, op);
1142
1143                 if (!drv->op_mode) {
1144                         mutex_unlock(&iwlwifi_opmode_table_mtx);
1145                         goto out_unbind;
1146                 }
1147         } else {
1148                 load_module = true;
1149         }
1150         mutex_unlock(&iwlwifi_opmode_table_mtx);
1151
1152         /*
1153          * Complete the firmware request last so that
1154          * a driver unbind (stop) doesn't run while we
1155          * are doing the start() above.
1156          */
1157         complete(&drv->request_firmware_complete);
1158
1159         /*
1160          * Load the module last so we don't block anything
1161          * else from proceeding if the module fails to load
1162          * or hangs loading.
1163          */
1164         if (load_module) {
1165                 err = request_module("%s", op->name);
1166 #ifdef CONFIG_IWLWIFI_OPMODE_MODULAR
1167                 if (err)
1168                         IWL_ERR(drv,
1169                                 "failed to load module %s (error %d), is dynamic loading enabled?\n",
1170                                 op->name, err);
1171 #endif
1172         }
1173
1174         /*
1175          * We may have loaded the wrong FW file in 8000 HW family if it is an
1176          * A-step card, and if drv->trans->hw_rev wasn't properly read when
1177          * the FW file had been loaded. (This might happen in SDIO.) In such a
1178          * case - unload and reload the correct file.
1179          *
1180          * TODO:
1181          * Once there is only one supported step for 8000 family - delete this!
1182          */
1183         if (drv->trans->cfg->device_family == IWL_DEVICE_FAMILY_8000 &&
1184             CSR_HW_REV_STEP(drv->trans->hw_rev) == SILICON_A_STEP &&
1185             drv->trans->hw_rev != hw_rev) {
1186                 char firmware_name[32];
1187
1188                 /* Free previous FW resources */
1189                 if (drv->op_mode)
1190                         _iwl_op_mode_stop(drv);
1191                 iwl_dealloc_ucode(drv);
1192
1193                 /* Build name of correct-step FW */
1194                 snprintf(firmware_name, sizeof(firmware_name),
1195                          strrchr(drv->firmware_name, '-'));
1196                 snprintf(drv->firmware_name, sizeof(drv->firmware_name),
1197                          "%s%s", drv->cfg->fw_name_pre, firmware_name);
1198
1199                 /* Clear data before loading correct FW */
1200                 list_del(&drv->list);
1201
1202                 /* Request correct FW file this time */
1203                 IWL_DEBUG_INFO(drv, "attempting to load A-step FW %s\n",
1204                                drv->firmware_name);
1205                 err = request_firmware(&ucode_raw, drv->firmware_name,
1206                                        drv->trans->dev);
1207                 if (err) {
1208                         IWL_ERR(drv, "Failed swapping FW!\n");
1209                         goto out_unbind;
1210                 }
1211
1212                 /* Redo callback function - this time with right FW */
1213                 iwl_req_fw_callback(ucode_raw, context);
1214         }
1215
1216         return;
1217
1218  try_again:
1219         /* try next, if any */
1220         release_firmware(ucode_raw);
1221         if (iwl_request_firmware(drv, false))
1222                 goto out_unbind;
1223         return;
1224
1225  out_free_fw:
1226         IWL_ERR(drv, "failed to allocate pci memory\n");
1227         iwl_dealloc_ucode(drv);
1228         release_firmware(ucode_raw);
1229  out_unbind:
1230         complete(&drv->request_firmware_complete);
1231         device_release_driver(drv->trans->dev);
1232 }
1233
1234 struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
1235                               const struct iwl_cfg *cfg)
1236 {
1237         struct iwl_drv *drv;
1238         int ret;
1239
1240         drv = kzalloc(sizeof(*drv), GFP_KERNEL);
1241         if (!drv) {
1242                 ret = -ENOMEM;
1243                 goto err;
1244         }
1245
1246         drv->trans = trans;
1247         drv->dev = trans->dev;
1248         drv->cfg = cfg;
1249
1250         init_completion(&drv->request_firmware_complete);
1251         INIT_LIST_HEAD(&drv->list);
1252
1253 #ifdef CONFIG_IWLWIFI_DEBUGFS
1254         /* Create the device debugfs entries. */
1255         drv->dbgfs_drv = debugfs_create_dir(dev_name(trans->dev),
1256                                             iwl_dbgfs_root);
1257
1258         if (!drv->dbgfs_drv) {
1259                 IWL_ERR(drv, "failed to create debugfs directory\n");
1260                 ret = -ENOMEM;
1261                 goto err_free_drv;
1262         }
1263
1264         /* Create transport layer debugfs dir */
1265         drv->trans->dbgfs_dir = debugfs_create_dir("trans", drv->dbgfs_drv);
1266
1267         if (!drv->trans->dbgfs_dir) {
1268                 IWL_ERR(drv, "failed to create transport debugfs directory\n");
1269                 ret = -ENOMEM;
1270                 goto err_free_dbgfs;
1271         }
1272 #endif
1273
1274         ret = iwl_request_firmware(drv, true);
1275         if (ret) {
1276                 IWL_ERR(trans, "Couldn't request the fw\n");
1277                 goto err_fw;
1278         }
1279
1280         return drv;
1281
1282 err_fw:
1283 #ifdef CONFIG_IWLWIFI_DEBUGFS
1284 err_free_dbgfs:
1285         debugfs_remove_recursive(drv->dbgfs_drv);
1286 err_free_drv:
1287 #endif
1288         kfree(drv);
1289 err:
1290         return ERR_PTR(ret);
1291 }
1292
1293 void iwl_drv_stop(struct iwl_drv *drv)
1294 {
1295         wait_for_completion(&drv->request_firmware_complete);
1296
1297         _iwl_op_mode_stop(drv);
1298
1299         iwl_dealloc_ucode(drv);
1300
1301         mutex_lock(&iwlwifi_opmode_table_mtx);
1302         /*
1303          * List is empty (this item wasn't added)
1304          * when firmware loading failed -- in that
1305          * case we can't remove it from any list.
1306          */
1307         if (!list_empty(&drv->list))
1308                 list_del(&drv->list);
1309         mutex_unlock(&iwlwifi_opmode_table_mtx);
1310
1311 #ifdef CONFIG_IWLWIFI_DEBUGFS
1312         debugfs_remove_recursive(drv->dbgfs_drv);
1313 #endif
1314
1315         kfree(drv);
1316 }
1317
1318
1319 /* shared module parameters */
1320 struct iwl_mod_params iwlwifi_mod_params = {
1321         .restart_fw = true,
1322         .bt_coex_active = true,
1323         .power_level = IWL_POWER_INDEX_1,
1324         .wd_disable = true,
1325 #ifndef CONFIG_IWLWIFI_UAPSD
1326         .uapsd_disable = true,
1327 #endif /* CONFIG_IWLWIFI_UAPSD */
1328         /* the rest are 0 by default */
1329 };
1330 IWL_EXPORT_SYMBOL(iwlwifi_mod_params);
1331
1332 int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
1333 {
1334         int i;
1335         struct iwl_drv *drv;
1336         struct iwlwifi_opmode_table *op;
1337
1338         mutex_lock(&iwlwifi_opmode_table_mtx);
1339         for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1340                 op = &iwlwifi_opmode_table[i];
1341                 if (strcmp(op->name, name))
1342                         continue;
1343                 op->ops = ops;
1344                 /* TODO: need to handle exceptional case */
1345                 list_for_each_entry(drv, &op->drv, list)
1346                         drv->op_mode = _iwl_op_mode_start(drv, op);
1347
1348                 mutex_unlock(&iwlwifi_opmode_table_mtx);
1349                 return 0;
1350         }
1351         mutex_unlock(&iwlwifi_opmode_table_mtx);
1352         return -EIO;
1353 }
1354 IWL_EXPORT_SYMBOL(iwl_opmode_register);
1355
1356 void iwl_opmode_deregister(const char *name)
1357 {
1358         int i;
1359         struct iwl_drv *drv;
1360
1361         mutex_lock(&iwlwifi_opmode_table_mtx);
1362         for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1363                 if (strcmp(iwlwifi_opmode_table[i].name, name))
1364                         continue;
1365                 iwlwifi_opmode_table[i].ops = NULL;
1366
1367                 /* call the stop routine for all devices */
1368                 list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
1369                         _iwl_op_mode_stop(drv);
1370
1371                 mutex_unlock(&iwlwifi_opmode_table_mtx);
1372                 return;
1373         }
1374         mutex_unlock(&iwlwifi_opmode_table_mtx);
1375 }
1376 IWL_EXPORT_SYMBOL(iwl_opmode_deregister);
1377
1378 static int __init iwl_drv_init(void)
1379 {
1380         int i;
1381
1382         mutex_init(&iwlwifi_opmode_table_mtx);
1383
1384         for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
1385                 INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
1386
1387         pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
1388         pr_info(DRV_COPYRIGHT "\n");
1389
1390 #ifdef CONFIG_IWLWIFI_DEBUGFS
1391         /* Create the root of iwlwifi debugfs subsystem. */
1392         iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL);
1393
1394         if (!iwl_dbgfs_root)
1395                 return -EFAULT;
1396 #endif
1397
1398         return iwl_pci_register_driver();
1399 }
1400 module_init(iwl_drv_init);
1401
1402 static void __exit iwl_drv_exit(void)
1403 {
1404         iwl_pci_unregister_driver();
1405
1406 #ifdef CONFIG_IWLWIFI_DEBUGFS
1407         debugfs_remove_recursive(iwl_dbgfs_root);
1408 #endif
1409 }
1410 module_exit(iwl_drv_exit);
1411
1412 #ifdef CONFIG_IWLWIFI_DEBUG
1413 module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
1414                    S_IRUGO | S_IWUSR);
1415 MODULE_PARM_DESC(debug, "debug output mask");
1416 #endif
1417
1418 module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
1419 MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
1420 module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
1421 MODULE_PARM_DESC(11n_disable,
1422         "disable 11n functionality, bitmap: 1: full, 2: disable agg TX, 4: disable agg RX, 8 enable agg TX");
1423 module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
1424                    int, S_IRUGO);
1425 MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size (default 0)");
1426 module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, bool, S_IRUGO);
1427 MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)");
1428
1429 module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
1430                    int, S_IRUGO);
1431 MODULE_PARM_DESC(antenna_coupling,
1432                  "specify antenna coupling in dB (default: 0 dB)");
1433
1434 module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
1435 MODULE_PARM_DESC(wd_disable,
1436                 "Disable stuck queue watchdog timer 0=system default, 1=disable (default: 1)");
1437
1438 module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, S_IRUGO);
1439 MODULE_PARM_DESC(nvm_file, "NVM file name");
1440
1441 module_param_named(uapsd_disable, iwlwifi_mod_params.uapsd_disable,
1442                    bool, S_IRUGO);
1443 #ifdef CONFIG_IWLWIFI_UAPSD
1444 MODULE_PARM_DESC(uapsd_disable, "disable U-APSD functionality (default: N)");
1445 #else
1446 MODULE_PARM_DESC(uapsd_disable, "disable U-APSD functionality (default: Y)");
1447 #endif
1448
1449 /*
1450  * set bt_coex_active to true, uCode will do kill/defer
1451  * every time the priority line is asserted (BT is sending signals on the
1452  * priority line in the PCIx).
1453  * set bt_coex_active to false, uCode will ignore the BT activity and
1454  * perform the normal operation
1455  *
1456  * User might experience transmit issue on some platform due to WiFi/BT
1457  * co-exist problem. The possible behaviors are:
1458  *   Able to scan and finding all the available AP
1459  *   Not able to associate with any AP
1460  * On those platforms, WiFi communication can be restored by set
1461  * "bt_coex_active" module parameter to "false"
1462  *
1463  * default: bt_coex_active = true (BT_COEX_ENABLE)
1464  */
1465 module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
1466                 bool, S_IRUGO);
1467 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
1468
1469 module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
1470 MODULE_PARM_DESC(led_mode, "0=system default, "
1471                 "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
1472
1473 module_param_named(power_save, iwlwifi_mod_params.power_save,
1474                 bool, S_IRUGO);
1475 MODULE_PARM_DESC(power_save,
1476                  "enable WiFi power management (default: disable)");
1477
1478 module_param_named(power_level, iwlwifi_mod_params.power_level,
1479                 int, S_IRUGO);
1480 MODULE_PARM_DESC(power_level,
1481                  "default power save level (range from 1 - 5, default: 1)");
1482
1483 module_param_named(fw_monitor, iwlwifi_mod_params.fw_monitor, bool, S_IRUGO);
1484 MODULE_PARM_DESC(fw_monitor,
1485                  "firmware monitor - to debug FW (default: false - needs lots of memory)");