iwlwifi: move firmware file format definitions to correct header
[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         if (drv->trans->cfg->device_family == IWL_DEVICE_FAMILY_8000) {
250                 char rev_step[2] = {
251                         'A' + CSR_HW_REV_STEP(drv->trans->hw_rev), 0
252                 };
253
254                 /* A-step doesn't have an indication */
255                 if (CSR_HW_REV_STEP(drv->trans->hw_rev) == SILICON_A_STEP)
256                         rev_step[0] = 0;
257
258                 snprintf(drv->firmware_name, sizeof(drv->firmware_name),
259                          "%s%s-%s.ucode", name_pre, rev_step, tag);
260         }
261
262         IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
263                        (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
264                                 ? "EXPERIMENTAL " : "",
265                        drv->firmware_name);
266
267         return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
268                                        drv->trans->dev,
269                                        GFP_KERNEL, drv, iwl_req_fw_callback);
270 }
271
272 struct fw_img_parsing {
273         struct fw_sec sec[IWL_UCODE_SECTION_MAX];
274         int sec_counter;
275 };
276
277 /*
278  * struct fw_sec_parsing: to extract fw section and it's offset from tlv
279  */
280 struct fw_sec_parsing {
281         __le32 offset;
282         const u8 data[];
283 } __packed;
284
285 /**
286  * struct iwl_tlv_calib_data - parse the default calib data from TLV
287  *
288  * @ucode_type: the uCode to which the following default calib relates.
289  * @calib: default calibrations.
290  */
291 struct iwl_tlv_calib_data {
292         __le32 ucode_type;
293         struct iwl_tlv_calib_ctrl calib;
294 } __packed;
295
296 struct iwl_firmware_pieces {
297         struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
298
299         u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
300         u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
301 };
302
303 /*
304  * These functions are just to extract uCode section data from the pieces
305  * structure.
306  */
307 static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
308                               enum iwl_ucode_type type,
309                               int  sec)
310 {
311         return &pieces->img[type].sec[sec];
312 }
313
314 static void set_sec_data(struct iwl_firmware_pieces *pieces,
315                          enum iwl_ucode_type type,
316                          int sec,
317                          const void *data)
318 {
319         pieces->img[type].sec[sec].data = data;
320 }
321
322 static void set_sec_size(struct iwl_firmware_pieces *pieces,
323                          enum iwl_ucode_type type,
324                          int sec,
325                          size_t size)
326 {
327         pieces->img[type].sec[sec].size = size;
328 }
329
330 static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
331                            enum iwl_ucode_type type,
332                            int sec)
333 {
334         return pieces->img[type].sec[sec].size;
335 }
336
337 static void set_sec_offset(struct iwl_firmware_pieces *pieces,
338                            enum iwl_ucode_type type,
339                            int sec,
340                            u32 offset)
341 {
342         pieces->img[type].sec[sec].offset = offset;
343 }
344
345 static int iwl_store_cscheme(struct iwl_fw *fw, const u8 *data, const u32 len)
346 {
347         int i, j;
348         struct iwl_fw_cscheme_list *l = (struct iwl_fw_cscheme_list *)data;
349         struct iwl_fw_cipher_scheme *fwcs;
350         struct ieee80211_cipher_scheme *cs;
351         u32 cipher;
352
353         if (len < sizeof(*l) ||
354             len < sizeof(l->size) + l->size * sizeof(l->cs[0]))
355                 return -EINVAL;
356
357         for (i = 0, j = 0; i < IWL_UCODE_MAX_CS && i < l->size; i++) {
358                 fwcs = &l->cs[j];
359                 cipher = le32_to_cpu(fwcs->cipher);
360
361                 /* we skip schemes with zero cipher suite selector */
362                 if (!cipher)
363                         continue;
364
365                 cs = &fw->cs[j++];
366                 cs->cipher = cipher;
367                 cs->iftype = BIT(NL80211_IFTYPE_STATION);
368                 cs->hdr_len = fwcs->hdr_len;
369                 cs->pn_len = fwcs->pn_len;
370                 cs->pn_off = fwcs->pn_off;
371                 cs->key_idx_off = fwcs->key_idx_off;
372                 cs->key_idx_mask = fwcs->key_idx_mask;
373                 cs->key_idx_shift = fwcs->key_idx_shift;
374                 cs->mic_len = fwcs->mic_len;
375         }
376
377         return 0;
378 }
379
380 /*
381  * Gets uCode section from tlv.
382  */
383 static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
384                                const void *data, enum iwl_ucode_type type,
385                                int size)
386 {
387         struct fw_img_parsing *img;
388         struct fw_sec *sec;
389         struct fw_sec_parsing *sec_parse;
390
391         if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
392                 return -1;
393
394         sec_parse = (struct fw_sec_parsing *)data;
395
396         img = &pieces->img[type];
397         sec = &img->sec[img->sec_counter];
398
399         sec->offset = le32_to_cpu(sec_parse->offset);
400         sec->data = sec_parse->data;
401         sec->size = size - sizeof(sec_parse->offset);
402
403         ++img->sec_counter;
404
405         return 0;
406 }
407
408 static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
409 {
410         struct iwl_tlv_calib_data *def_calib =
411                                         (struct iwl_tlv_calib_data *)data;
412         u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
413         if (ucode_type >= IWL_UCODE_TYPE_MAX) {
414                 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
415                         ucode_type);
416                 return -EINVAL;
417         }
418         drv->fw.default_calib[ucode_type].flow_trigger =
419                 def_calib->calib.flow_trigger;
420         drv->fw.default_calib[ucode_type].event_trigger =
421                 def_calib->calib.event_trigger;
422
423         return 0;
424 }
425
426 static int iwl_set_ucode_api_flags(struct iwl_drv *drv, const u8 *data,
427                                    struct iwl_ucode_capabilities *capa)
428 {
429         const struct iwl_ucode_api *ucode_api = (void *)data;
430         u32 api_index = le32_to_cpu(ucode_api->api_index);
431
432         if (api_index >= IWL_API_ARRAY_SIZE) {
433                 IWL_ERR(drv, "api_index larger than supported by driver\n");
434                 return -EINVAL;
435         }
436
437         capa->api[api_index] = le32_to_cpu(ucode_api->api_flags);
438
439         return 0;
440 }
441
442 static int iwl_set_ucode_capabilities(struct iwl_drv *drv, const u8 *data,
443                                       struct iwl_ucode_capabilities *capa)
444 {
445         const struct iwl_ucode_capa *ucode_capa = (void *)data;
446         u32 api_index = le32_to_cpu(ucode_capa->api_index);
447
448         if (api_index >= IWL_CAPABILITIES_ARRAY_SIZE) {
449                 IWL_ERR(drv, "api_index larger than supported by driver\n");
450                 return -EINVAL;
451         }
452
453         capa->capa[api_index] = le32_to_cpu(ucode_capa->api_capa);
454
455         return 0;
456 }
457
458 static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
459                                     const struct firmware *ucode_raw,
460                                     struct iwl_firmware_pieces *pieces)
461 {
462         struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
463         u32 api_ver, hdr_size, build;
464         char buildstr[25];
465         const u8 *src;
466
467         drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
468         api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
469
470         switch (api_ver) {
471         default:
472                 hdr_size = 28;
473                 if (ucode_raw->size < hdr_size) {
474                         IWL_ERR(drv, "File size too small!\n");
475                         return -EINVAL;
476                 }
477                 build = le32_to_cpu(ucode->u.v2.build);
478                 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
479                              le32_to_cpu(ucode->u.v2.inst_size));
480                 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
481                              le32_to_cpu(ucode->u.v2.data_size));
482                 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
483                              le32_to_cpu(ucode->u.v2.init_size));
484                 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
485                              le32_to_cpu(ucode->u.v2.init_data_size));
486                 src = ucode->u.v2.data;
487                 break;
488         case 0:
489         case 1:
490         case 2:
491                 hdr_size = 24;
492                 if (ucode_raw->size < hdr_size) {
493                         IWL_ERR(drv, "File size too small!\n");
494                         return -EINVAL;
495                 }
496                 build = 0;
497                 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
498                              le32_to_cpu(ucode->u.v1.inst_size));
499                 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
500                              le32_to_cpu(ucode->u.v1.data_size));
501                 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
502                              le32_to_cpu(ucode->u.v1.init_size));
503                 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
504                              le32_to_cpu(ucode->u.v1.init_data_size));
505                 src = ucode->u.v1.data;
506                 break;
507         }
508
509         if (build)
510                 sprintf(buildstr, " build %u%s", build,
511                        (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
512                                 ? " (EXP)" : "");
513         else
514                 buildstr[0] = '\0';
515
516         snprintf(drv->fw.fw_version,
517                  sizeof(drv->fw.fw_version),
518                  "%u.%u.%u.%u%s",
519                  IWL_UCODE_MAJOR(drv->fw.ucode_ver),
520                  IWL_UCODE_MINOR(drv->fw.ucode_ver),
521                  IWL_UCODE_API(drv->fw.ucode_ver),
522                  IWL_UCODE_SERIAL(drv->fw.ucode_ver),
523                  buildstr);
524
525         /* Verify size of file vs. image size info in file's header */
526
527         if (ucode_raw->size != hdr_size +
528             get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
529             get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
530             get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
531             get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
532
533                 IWL_ERR(drv,
534                         "uCode file size %d does not match expected size\n",
535                         (int)ucode_raw->size);
536                 return -EINVAL;
537         }
538
539
540         set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
541         src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
542         set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
543                        IWLAGN_RTC_INST_LOWER_BOUND);
544         set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
545         src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
546         set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
547                        IWLAGN_RTC_DATA_LOWER_BOUND);
548         set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
549         src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
550         set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
551                        IWLAGN_RTC_INST_LOWER_BOUND);
552         set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
553         src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
554         set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
555                        IWLAGN_RTC_DATA_LOWER_BOUND);
556         return 0;
557 }
558
559 static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
560                                 const struct firmware *ucode_raw,
561                                 struct iwl_firmware_pieces *pieces,
562                                 struct iwl_ucode_capabilities *capa)
563 {
564         struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
565         struct iwl_ucode_tlv *tlv;
566         size_t len = ucode_raw->size;
567         const u8 *data;
568         u32 tlv_len;
569         enum iwl_ucode_tlv_type tlv_type;
570         const u8 *tlv_data;
571         char buildstr[25];
572         u32 build;
573         int num_of_cpus;
574
575         if (len < sizeof(*ucode)) {
576                 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
577                 return -EINVAL;
578         }
579
580         if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
581                 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
582                         le32_to_cpu(ucode->magic));
583                 return -EINVAL;
584         }
585
586         drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
587         memcpy(drv->fw.human_readable, ucode->human_readable,
588                sizeof(drv->fw.human_readable));
589         build = le32_to_cpu(ucode->build);
590
591         if (build)
592                 sprintf(buildstr, " build %u%s", build,
593                        (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
594                                 ? " (EXP)" : "");
595         else
596                 buildstr[0] = '\0';
597
598         snprintf(drv->fw.fw_version,
599                  sizeof(drv->fw.fw_version),
600                  "%u.%u.%u.%u%s",
601                  IWL_UCODE_MAJOR(drv->fw.ucode_ver),
602                  IWL_UCODE_MINOR(drv->fw.ucode_ver),
603                  IWL_UCODE_API(drv->fw.ucode_ver),
604                  IWL_UCODE_SERIAL(drv->fw.ucode_ver),
605                  buildstr);
606
607         data = ucode->data;
608
609         len -= sizeof(*ucode);
610
611         while (len >= sizeof(*tlv)) {
612                 len -= sizeof(*tlv);
613                 tlv = (void *)data;
614
615                 tlv_len = le32_to_cpu(tlv->length);
616                 tlv_type = le32_to_cpu(tlv->type);
617                 tlv_data = tlv->data;
618
619                 if (len < tlv_len) {
620                         IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
621                                 len, tlv_len);
622                         return -EINVAL;
623                 }
624                 len -= ALIGN(tlv_len, 4);
625                 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
626
627                 switch (tlv_type) {
628                 case IWL_UCODE_TLV_INST:
629                         set_sec_data(pieces, IWL_UCODE_REGULAR,
630                                      IWL_UCODE_SECTION_INST, tlv_data);
631                         set_sec_size(pieces, IWL_UCODE_REGULAR,
632                                      IWL_UCODE_SECTION_INST, tlv_len);
633                         set_sec_offset(pieces, IWL_UCODE_REGULAR,
634                                        IWL_UCODE_SECTION_INST,
635                                        IWLAGN_RTC_INST_LOWER_BOUND);
636                         break;
637                 case IWL_UCODE_TLV_DATA:
638                         set_sec_data(pieces, IWL_UCODE_REGULAR,
639                                      IWL_UCODE_SECTION_DATA, tlv_data);
640                         set_sec_size(pieces, IWL_UCODE_REGULAR,
641                                      IWL_UCODE_SECTION_DATA, tlv_len);
642                         set_sec_offset(pieces, IWL_UCODE_REGULAR,
643                                        IWL_UCODE_SECTION_DATA,
644                                        IWLAGN_RTC_DATA_LOWER_BOUND);
645                         break;
646                 case IWL_UCODE_TLV_INIT:
647                         set_sec_data(pieces, IWL_UCODE_INIT,
648                                      IWL_UCODE_SECTION_INST, tlv_data);
649                         set_sec_size(pieces, IWL_UCODE_INIT,
650                                      IWL_UCODE_SECTION_INST, tlv_len);
651                         set_sec_offset(pieces, IWL_UCODE_INIT,
652                                        IWL_UCODE_SECTION_INST,
653                                        IWLAGN_RTC_INST_LOWER_BOUND);
654                         break;
655                 case IWL_UCODE_TLV_INIT_DATA:
656                         set_sec_data(pieces, IWL_UCODE_INIT,
657                                      IWL_UCODE_SECTION_DATA, tlv_data);
658                         set_sec_size(pieces, IWL_UCODE_INIT,
659                                      IWL_UCODE_SECTION_DATA, tlv_len);
660                         set_sec_offset(pieces, IWL_UCODE_INIT,
661                                        IWL_UCODE_SECTION_DATA,
662                                        IWLAGN_RTC_DATA_LOWER_BOUND);
663                         break;
664                 case IWL_UCODE_TLV_BOOT:
665                         IWL_ERR(drv, "Found unexpected BOOT ucode\n");
666                         break;
667                 case IWL_UCODE_TLV_PROBE_MAX_LEN:
668                         if (tlv_len != sizeof(u32))
669                                 goto invalid_tlv_len;
670                         capa->max_probe_length =
671                                         le32_to_cpup((__le32 *)tlv_data);
672                         break;
673                 case IWL_UCODE_TLV_PAN:
674                         if (tlv_len)
675                                 goto invalid_tlv_len;
676                         capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
677                         break;
678                 case IWL_UCODE_TLV_FLAGS:
679                         /* must be at least one u32 */
680                         if (tlv_len < sizeof(u32))
681                                 goto invalid_tlv_len;
682                         /* and a proper number of u32s */
683                         if (tlv_len % sizeof(u32))
684                                 goto invalid_tlv_len;
685                         /*
686                          * This driver only reads the first u32 as
687                          * right now no more features are defined,
688                          * if that changes then either the driver
689                          * will not work with the new firmware, or
690                          * it'll not take advantage of new features.
691                          */
692                         capa->flags = le32_to_cpup((__le32 *)tlv_data);
693                         break;
694                 case IWL_UCODE_TLV_API_CHANGES_SET:
695                         if (tlv_len != sizeof(struct iwl_ucode_api))
696                                 goto invalid_tlv_len;
697                         if (iwl_set_ucode_api_flags(drv, tlv_data, capa))
698                                 goto tlv_error;
699                         break;
700                 case IWL_UCODE_TLV_ENABLED_CAPABILITIES:
701                         if (tlv_len != sizeof(struct iwl_ucode_capa))
702                                 goto invalid_tlv_len;
703                         if (iwl_set_ucode_capabilities(drv, tlv_data, capa))
704                                 goto tlv_error;
705                         break;
706                 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
707                         if (tlv_len != sizeof(u32))
708                                 goto invalid_tlv_len;
709                         pieces->init_evtlog_ptr =
710                                         le32_to_cpup((__le32 *)tlv_data);
711                         break;
712                 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
713                         if (tlv_len != sizeof(u32))
714                                 goto invalid_tlv_len;
715                         pieces->init_evtlog_size =
716                                         le32_to_cpup((__le32 *)tlv_data);
717                         break;
718                 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
719                         if (tlv_len != sizeof(u32))
720                                 goto invalid_tlv_len;
721                         pieces->init_errlog_ptr =
722                                         le32_to_cpup((__le32 *)tlv_data);
723                         break;
724                 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
725                         if (tlv_len != sizeof(u32))
726                                 goto invalid_tlv_len;
727                         pieces->inst_evtlog_ptr =
728                                         le32_to_cpup((__le32 *)tlv_data);
729                         break;
730                 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
731                         if (tlv_len != sizeof(u32))
732                                 goto invalid_tlv_len;
733                         pieces->inst_evtlog_size =
734                                         le32_to_cpup((__le32 *)tlv_data);
735                         break;
736                 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
737                         if (tlv_len != sizeof(u32))
738                                 goto invalid_tlv_len;
739                         pieces->inst_errlog_ptr =
740                                         le32_to_cpup((__le32 *)tlv_data);
741                         break;
742                 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
743                         if (tlv_len)
744                                 goto invalid_tlv_len;
745                         drv->fw.enhance_sensitivity_table = true;
746                         break;
747                 case IWL_UCODE_TLV_WOWLAN_INST:
748                         set_sec_data(pieces, IWL_UCODE_WOWLAN,
749                                      IWL_UCODE_SECTION_INST, tlv_data);
750                         set_sec_size(pieces, IWL_UCODE_WOWLAN,
751                                      IWL_UCODE_SECTION_INST, tlv_len);
752                         set_sec_offset(pieces, IWL_UCODE_WOWLAN,
753                                        IWL_UCODE_SECTION_INST,
754                                        IWLAGN_RTC_INST_LOWER_BOUND);
755                         break;
756                 case IWL_UCODE_TLV_WOWLAN_DATA:
757                         set_sec_data(pieces, IWL_UCODE_WOWLAN,
758                                      IWL_UCODE_SECTION_DATA, tlv_data);
759                         set_sec_size(pieces, IWL_UCODE_WOWLAN,
760                                      IWL_UCODE_SECTION_DATA, tlv_len);
761                         set_sec_offset(pieces, IWL_UCODE_WOWLAN,
762                                        IWL_UCODE_SECTION_DATA,
763                                        IWLAGN_RTC_DATA_LOWER_BOUND);
764                         break;
765                 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
766                         if (tlv_len != sizeof(u32))
767                                 goto invalid_tlv_len;
768                         capa->standard_phy_calibration_size =
769                                         le32_to_cpup((__le32 *)tlv_data);
770                         break;
771                  case IWL_UCODE_TLV_SEC_RT:
772                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
773                                             tlv_len);
774                         drv->fw.mvm_fw = true;
775                         break;
776                 case IWL_UCODE_TLV_SEC_INIT:
777                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
778                                             tlv_len);
779                         drv->fw.mvm_fw = true;
780                         break;
781                 case IWL_UCODE_TLV_SEC_WOWLAN:
782                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
783                                             tlv_len);
784                         drv->fw.mvm_fw = true;
785                         break;
786                 case IWL_UCODE_TLV_DEF_CALIB:
787                         if (tlv_len != sizeof(struct iwl_tlv_calib_data))
788                                 goto invalid_tlv_len;
789                         if (iwl_set_default_calib(drv, tlv_data))
790                                 goto tlv_error;
791                         break;
792                 case IWL_UCODE_TLV_PHY_SKU:
793                         if (tlv_len != sizeof(u32))
794                                 goto invalid_tlv_len;
795                         drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
796                         drv->fw.valid_tx_ant = (drv->fw.phy_config &
797                                                 FW_PHY_CFG_TX_CHAIN) >>
798                                                 FW_PHY_CFG_TX_CHAIN_POS;
799                         drv->fw.valid_rx_ant = (drv->fw.phy_config &
800                                                 FW_PHY_CFG_RX_CHAIN) >>
801                                                 FW_PHY_CFG_RX_CHAIN_POS;
802                         break;
803                  case IWL_UCODE_TLV_SECURE_SEC_RT:
804                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
805                                             tlv_len);
806                         drv->fw.mvm_fw = true;
807                         break;
808                 case IWL_UCODE_TLV_SECURE_SEC_INIT:
809                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
810                                             tlv_len);
811                         drv->fw.mvm_fw = true;
812                         break;
813                 case IWL_UCODE_TLV_SECURE_SEC_WOWLAN:
814                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
815                                             tlv_len);
816                         drv->fw.mvm_fw = true;
817                         break;
818                 case IWL_UCODE_TLV_NUM_OF_CPU:
819                         if (tlv_len != sizeof(u32))
820                                 goto invalid_tlv_len;
821                         num_of_cpus =
822                                 le32_to_cpup((__le32 *)tlv_data);
823
824                         if (num_of_cpus == 2) {
825                                 drv->fw.img[IWL_UCODE_REGULAR].is_dual_cpus =
826                                         true;
827                                 drv->fw.img[IWL_UCODE_INIT].is_dual_cpus =
828                                         true;
829                                 drv->fw.img[IWL_UCODE_WOWLAN].is_dual_cpus =
830                                         true;
831                         } else if ((num_of_cpus > 2) || (num_of_cpus < 1)) {
832                                 IWL_ERR(drv, "Driver support upto 2 CPUs\n");
833                                 return -EINVAL;
834                         }
835                         break;
836                 case IWL_UCODE_TLV_CSCHEME:
837                         if (iwl_store_cscheme(&drv->fw, tlv_data, tlv_len))
838                                 goto invalid_tlv_len;
839                         break;
840                 case IWL_UCODE_TLV_N_SCAN_CHANNELS:
841                         if (tlv_len != sizeof(u32))
842                                 goto invalid_tlv_len;
843                         capa->n_scan_channels =
844                                 le32_to_cpup((__le32 *)tlv_data);
845                         break;
846                 default:
847                         IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
848                         break;
849                 }
850         }
851
852         if (len) {
853                 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
854                 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
855                 return -EINVAL;
856         }
857
858         return 0;
859
860  invalid_tlv_len:
861         IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
862  tlv_error:
863         iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
864
865         return -EINVAL;
866 }
867
868 static int iwl_alloc_ucode(struct iwl_drv *drv,
869                            struct iwl_firmware_pieces *pieces,
870                            enum iwl_ucode_type type)
871 {
872         int i;
873         for (i = 0;
874              i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
875              i++)
876                 if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
877                                       get_sec(pieces, type, i)))
878                         return -ENOMEM;
879         return 0;
880 }
881
882 static int validate_sec_sizes(struct iwl_drv *drv,
883                               struct iwl_firmware_pieces *pieces,
884                               const struct iwl_cfg *cfg)
885 {
886         IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
887                 get_sec_size(pieces, IWL_UCODE_REGULAR,
888                              IWL_UCODE_SECTION_INST));
889         IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
890                 get_sec_size(pieces, IWL_UCODE_REGULAR,
891                              IWL_UCODE_SECTION_DATA));
892         IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
893                 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
894         IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
895                 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
896
897         /* Verify that uCode images will fit in card's SRAM. */
898         if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
899                                                         cfg->max_inst_size) {
900                 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
901                         get_sec_size(pieces, IWL_UCODE_REGULAR,
902                                                 IWL_UCODE_SECTION_INST));
903                 return -1;
904         }
905
906         if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
907                                                         cfg->max_data_size) {
908                 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
909                         get_sec_size(pieces, IWL_UCODE_REGULAR,
910                                                 IWL_UCODE_SECTION_DATA));
911                 return -1;
912         }
913
914          if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
915                                                         cfg->max_inst_size) {
916                 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
917                         get_sec_size(pieces, IWL_UCODE_INIT,
918                                                 IWL_UCODE_SECTION_INST));
919                 return -1;
920         }
921
922         if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
923                                                         cfg->max_data_size) {
924                 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
925                         get_sec_size(pieces, IWL_UCODE_REGULAR,
926                                                 IWL_UCODE_SECTION_DATA));
927                 return -1;
928         }
929         return 0;
930 }
931
932 static struct iwl_op_mode *
933 _iwl_op_mode_start(struct iwl_drv *drv, struct iwlwifi_opmode_table *op)
934 {
935         const struct iwl_op_mode_ops *ops = op->ops;
936         struct dentry *dbgfs_dir = NULL;
937         struct iwl_op_mode *op_mode = NULL;
938
939 #ifdef CONFIG_IWLWIFI_DEBUGFS
940         drv->dbgfs_op_mode = debugfs_create_dir(op->name,
941                                                 drv->dbgfs_drv);
942         if (!drv->dbgfs_op_mode) {
943                 IWL_ERR(drv,
944                         "failed to create opmode debugfs directory\n");
945                 return op_mode;
946         }
947         dbgfs_dir = drv->dbgfs_op_mode;
948 #endif
949
950         op_mode = ops->start(drv->trans, drv->cfg, &drv->fw, dbgfs_dir);
951
952 #ifdef CONFIG_IWLWIFI_DEBUGFS
953         if (!op_mode) {
954                 debugfs_remove_recursive(drv->dbgfs_op_mode);
955                 drv->dbgfs_op_mode = NULL;
956         }
957 #endif
958
959         return op_mode;
960 }
961
962 static void _iwl_op_mode_stop(struct iwl_drv *drv)
963 {
964         /* op_mode can be NULL if its start failed */
965         if (drv->op_mode) {
966                 iwl_op_mode_stop(drv->op_mode);
967                 drv->op_mode = NULL;
968
969 #ifdef CONFIG_IWLWIFI_DEBUGFS
970                 debugfs_remove_recursive(drv->dbgfs_op_mode);
971                 drv->dbgfs_op_mode = NULL;
972 #endif
973         }
974 }
975
976 /**
977  * iwl_req_fw_callback - callback when firmware was loaded
978  *
979  * If loaded successfully, copies the firmware into buffers
980  * for the card to fetch (via DMA).
981  */
982 static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context)
983 {
984         struct iwl_drv *drv = context;
985         struct iwl_fw *fw = &drv->fw;
986         struct iwl_ucode_header *ucode;
987         struct iwlwifi_opmode_table *op;
988         int err;
989         struct iwl_firmware_pieces pieces;
990         const unsigned int api_max = drv->cfg->ucode_api_max;
991         unsigned int api_ok = drv->cfg->ucode_api_ok;
992         const unsigned int api_min = drv->cfg->ucode_api_min;
993         u32 api_ver;
994         int i;
995         bool load_module = false;
996
997         fw->ucode_capa.max_probe_length = IWL_DEFAULT_MAX_PROBE_LENGTH;
998         fw->ucode_capa.standard_phy_calibration_size =
999                         IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1000         fw->ucode_capa.n_scan_channels = IWL_DEFAULT_SCAN_CHANNELS;
1001
1002         if (!api_ok)
1003                 api_ok = api_max;
1004
1005         memset(&pieces, 0, sizeof(pieces));
1006
1007         if (!ucode_raw) {
1008                 if (drv->fw_index <= api_ok)
1009                         IWL_ERR(drv,
1010                                 "request for firmware file '%s' failed.\n",
1011                                 drv->firmware_name);
1012                 goto try_again;
1013         }
1014
1015         IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
1016                        drv->firmware_name, ucode_raw->size);
1017
1018         /* Make sure that we got at least the API version number */
1019         if (ucode_raw->size < 4) {
1020                 IWL_ERR(drv, "File size way too small!\n");
1021                 goto try_again;
1022         }
1023
1024         /* Data from ucode file:  header followed by uCode images */
1025         ucode = (struct iwl_ucode_header *)ucode_raw->data;
1026
1027         if (ucode->ver)
1028                 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
1029         else
1030                 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
1031                                            &fw->ucode_capa);
1032
1033         if (err)
1034                 goto try_again;
1035
1036         api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
1037
1038         /*
1039          * api_ver should match the api version forming part of the
1040          * firmware filename ... but we don't check for that and only rely
1041          * on the API version read from firmware header from here on forward
1042          */
1043         /* no api version check required for experimental uCode */
1044         if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
1045                 if (api_ver < api_min || api_ver > api_max) {
1046                         IWL_ERR(drv,
1047                                 "Driver unable to support your firmware API. "
1048                                 "Driver supports v%u, firmware is v%u.\n",
1049                                 api_max, api_ver);
1050                         goto try_again;
1051                 }
1052
1053                 if (api_ver < api_ok) {
1054                         if (api_ok != api_max)
1055                                 IWL_ERR(drv, "Firmware has old API version, "
1056                                         "expected v%u through v%u, got v%u.\n",
1057                                         api_ok, api_max, api_ver);
1058                         else
1059                                 IWL_ERR(drv, "Firmware has old API version, "
1060                                         "expected v%u, got v%u.\n",
1061                                         api_max, api_ver);
1062                         IWL_ERR(drv, "New firmware can be obtained from "
1063                                       "http://www.intellinuxwireless.org/.\n");
1064                 }
1065         }
1066
1067         /*
1068          * In mvm uCode there is no difference between data and instructions
1069          * sections.
1070          */
1071         if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
1072                 goto try_again;
1073
1074         /* Allocate ucode buffers for card's bus-master loading ... */
1075
1076         /* Runtime instructions and 2 copies of data:
1077          * 1) unmodified from disk
1078          * 2) backup cache for save/restore during power-downs */
1079         for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
1080                 if (iwl_alloc_ucode(drv, &pieces, i))
1081                         goto out_free_fw;
1082
1083         /* Now that we can no longer fail, copy information */
1084
1085         /*
1086          * The (size - 16) / 12 formula is based on the information recorded
1087          * for each event, which is of mode 1 (including timestamp) for all
1088          * new microcodes that include this information.
1089          */
1090         fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
1091         if (pieces.init_evtlog_size)
1092                 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
1093         else
1094                 fw->init_evtlog_size =
1095                         drv->cfg->base_params->max_event_log_size;
1096         fw->init_errlog_ptr = pieces.init_errlog_ptr;
1097         fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
1098         if (pieces.inst_evtlog_size)
1099                 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
1100         else
1101                 fw->inst_evtlog_size =
1102                         drv->cfg->base_params->max_event_log_size;
1103         fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
1104
1105         /*
1106          * figure out the offset of chain noise reset and gain commands
1107          * base on the size of standard phy calibration commands table size
1108          */
1109         if (fw->ucode_capa.standard_phy_calibration_size >
1110             IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
1111                 fw->ucode_capa.standard_phy_calibration_size =
1112                         IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1113
1114         /* We have our copies now, allow OS release its copies */
1115         release_firmware(ucode_raw);
1116
1117         mutex_lock(&iwlwifi_opmode_table_mtx);
1118         if (fw->mvm_fw)
1119                 op = &iwlwifi_opmode_table[MVM_OP_MODE];
1120         else
1121                 op = &iwlwifi_opmode_table[DVM_OP_MODE];
1122
1123         IWL_INFO(drv, "loaded firmware version %s op_mode %s\n",
1124                  drv->fw.fw_version, op->name);
1125
1126         /* add this device to the list of devices using this op_mode */
1127         list_add_tail(&drv->list, &op->drv);
1128
1129         if (op->ops) {
1130                 drv->op_mode = _iwl_op_mode_start(drv, op);
1131
1132                 if (!drv->op_mode) {
1133                         mutex_unlock(&iwlwifi_opmode_table_mtx);
1134                         goto out_unbind;
1135                 }
1136         } else {
1137                 load_module = true;
1138         }
1139         mutex_unlock(&iwlwifi_opmode_table_mtx);
1140
1141         /*
1142          * Complete the firmware request last so that
1143          * a driver unbind (stop) doesn't run while we
1144          * are doing the start() above.
1145          */
1146         complete(&drv->request_firmware_complete);
1147
1148         /*
1149          * Load the module last so we don't block anything
1150          * else from proceeding if the module fails to load
1151          * or hangs loading.
1152          */
1153         if (load_module) {
1154                 err = request_module("%s", op->name);
1155 #ifdef CONFIG_IWLWIFI_OPMODE_MODULAR
1156                 if (err)
1157                         IWL_ERR(drv,
1158                                 "failed to load module %s (error %d), is dynamic loading enabled?\n",
1159                                 op->name, err);
1160 #endif
1161         }
1162         return;
1163
1164  try_again:
1165         /* try next, if any */
1166         release_firmware(ucode_raw);
1167         if (iwl_request_firmware(drv, false))
1168                 goto out_unbind;
1169         return;
1170
1171  out_free_fw:
1172         IWL_ERR(drv, "failed to allocate pci memory\n");
1173         iwl_dealloc_ucode(drv);
1174         release_firmware(ucode_raw);
1175  out_unbind:
1176         complete(&drv->request_firmware_complete);
1177         device_release_driver(drv->trans->dev);
1178 }
1179
1180 struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
1181                               const struct iwl_cfg *cfg)
1182 {
1183         struct iwl_drv *drv;
1184         int ret;
1185
1186         drv = kzalloc(sizeof(*drv), GFP_KERNEL);
1187         if (!drv) {
1188                 ret = -ENOMEM;
1189                 goto err;
1190         }
1191
1192         drv->trans = trans;
1193         drv->dev = trans->dev;
1194         drv->cfg = cfg;
1195
1196         init_completion(&drv->request_firmware_complete);
1197         INIT_LIST_HEAD(&drv->list);
1198
1199 #ifdef CONFIG_IWLWIFI_DEBUGFS
1200         /* Create the device debugfs entries. */
1201         drv->dbgfs_drv = debugfs_create_dir(dev_name(trans->dev),
1202                                             iwl_dbgfs_root);
1203
1204         if (!drv->dbgfs_drv) {
1205                 IWL_ERR(drv, "failed to create debugfs directory\n");
1206                 ret = -ENOMEM;
1207                 goto err_free_drv;
1208         }
1209
1210         /* Create transport layer debugfs dir */
1211         drv->trans->dbgfs_dir = debugfs_create_dir("trans", drv->dbgfs_drv);
1212
1213         if (!drv->trans->dbgfs_dir) {
1214                 IWL_ERR(drv, "failed to create transport debugfs directory\n");
1215                 ret = -ENOMEM;
1216                 goto err_free_dbgfs;
1217         }
1218 #endif
1219
1220         ret = iwl_request_firmware(drv, true);
1221         if (ret) {
1222                 IWL_ERR(trans, "Couldn't request the fw\n");
1223                 goto err_fw;
1224         }
1225
1226         return drv;
1227
1228 err_fw:
1229 #ifdef CONFIG_IWLWIFI_DEBUGFS
1230 err_free_dbgfs:
1231         debugfs_remove_recursive(drv->dbgfs_drv);
1232 err_free_drv:
1233 #endif
1234         kfree(drv);
1235 err:
1236         return ERR_PTR(ret);
1237 }
1238
1239 void iwl_drv_stop(struct iwl_drv *drv)
1240 {
1241         wait_for_completion(&drv->request_firmware_complete);
1242
1243         _iwl_op_mode_stop(drv);
1244
1245         iwl_dealloc_ucode(drv);
1246
1247         mutex_lock(&iwlwifi_opmode_table_mtx);
1248         /*
1249          * List is empty (this item wasn't added)
1250          * when firmware loading failed -- in that
1251          * case we can't remove it from any list.
1252          */
1253         if (!list_empty(&drv->list))
1254                 list_del(&drv->list);
1255         mutex_unlock(&iwlwifi_opmode_table_mtx);
1256
1257 #ifdef CONFIG_IWLWIFI_DEBUGFS
1258         debugfs_remove_recursive(drv->dbgfs_drv);
1259 #endif
1260
1261         kfree(drv);
1262 }
1263
1264
1265 /* shared module parameters */
1266 struct iwl_mod_params iwlwifi_mod_params = {
1267         .restart_fw = true,
1268         .bt_coex_active = true,
1269         .power_level = IWL_POWER_INDEX_1,
1270         .wd_disable = true,
1271 #ifndef CONFIG_IWLWIFI_UAPSD
1272         .uapsd_disable = true,
1273 #endif /* CONFIG_IWLWIFI_UAPSD */
1274         /* the rest are 0 by default */
1275 };
1276 IWL_EXPORT_SYMBOL(iwlwifi_mod_params);
1277
1278 int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
1279 {
1280         int i;
1281         struct iwl_drv *drv;
1282         struct iwlwifi_opmode_table *op;
1283
1284         mutex_lock(&iwlwifi_opmode_table_mtx);
1285         for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1286                 op = &iwlwifi_opmode_table[i];
1287                 if (strcmp(op->name, name))
1288                         continue;
1289                 op->ops = ops;
1290                 /* TODO: need to handle exceptional case */
1291                 list_for_each_entry(drv, &op->drv, list)
1292                         drv->op_mode = _iwl_op_mode_start(drv, op);
1293
1294                 mutex_unlock(&iwlwifi_opmode_table_mtx);
1295                 return 0;
1296         }
1297         mutex_unlock(&iwlwifi_opmode_table_mtx);
1298         return -EIO;
1299 }
1300 IWL_EXPORT_SYMBOL(iwl_opmode_register);
1301
1302 void iwl_opmode_deregister(const char *name)
1303 {
1304         int i;
1305         struct iwl_drv *drv;
1306
1307         mutex_lock(&iwlwifi_opmode_table_mtx);
1308         for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1309                 if (strcmp(iwlwifi_opmode_table[i].name, name))
1310                         continue;
1311                 iwlwifi_opmode_table[i].ops = NULL;
1312
1313                 /* call the stop routine for all devices */
1314                 list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
1315                         _iwl_op_mode_stop(drv);
1316
1317                 mutex_unlock(&iwlwifi_opmode_table_mtx);
1318                 return;
1319         }
1320         mutex_unlock(&iwlwifi_opmode_table_mtx);
1321 }
1322 IWL_EXPORT_SYMBOL(iwl_opmode_deregister);
1323
1324 static int __init iwl_drv_init(void)
1325 {
1326         int i;
1327
1328         mutex_init(&iwlwifi_opmode_table_mtx);
1329
1330         for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
1331                 INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
1332
1333         pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
1334         pr_info(DRV_COPYRIGHT "\n");
1335
1336 #ifdef CONFIG_IWLWIFI_DEBUGFS
1337         /* Create the root of iwlwifi debugfs subsystem. */
1338         iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL);
1339
1340         if (!iwl_dbgfs_root)
1341                 return -EFAULT;
1342 #endif
1343
1344         return iwl_pci_register_driver();
1345 }
1346 module_init(iwl_drv_init);
1347
1348 static void __exit iwl_drv_exit(void)
1349 {
1350         iwl_pci_unregister_driver();
1351
1352 #ifdef CONFIG_IWLWIFI_DEBUGFS
1353         debugfs_remove_recursive(iwl_dbgfs_root);
1354 #endif
1355 }
1356 module_exit(iwl_drv_exit);
1357
1358 #ifdef CONFIG_IWLWIFI_DEBUG
1359 module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
1360                    S_IRUGO | S_IWUSR);
1361 MODULE_PARM_DESC(debug, "debug output mask");
1362 #endif
1363
1364 module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
1365 MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
1366 module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
1367 MODULE_PARM_DESC(11n_disable,
1368         "disable 11n functionality, bitmap: 1: full, 2: disable agg TX, 4: disable agg RX, 8 enable agg TX");
1369 module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
1370                    int, S_IRUGO);
1371 MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size (default 0)");
1372 module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, bool, S_IRUGO);
1373 MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)");
1374
1375 module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
1376                    int, S_IRUGO);
1377 MODULE_PARM_DESC(antenna_coupling,
1378                  "specify antenna coupling in dB (default: 0 dB)");
1379
1380 module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
1381 MODULE_PARM_DESC(wd_disable,
1382                 "Disable stuck queue watchdog timer 0=system default, 1=disable (default: 1)");
1383
1384 module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, S_IRUGO);
1385 MODULE_PARM_DESC(nvm_file, "NVM file name");
1386
1387 module_param_named(uapsd_disable, iwlwifi_mod_params.uapsd_disable,
1388                    bool, S_IRUGO);
1389 #ifdef CONFIG_IWLWIFI_UAPSD
1390 MODULE_PARM_DESC(uapsd_disable, "disable U-APSD functionality (default: N)");
1391 #else
1392 MODULE_PARM_DESC(uapsd_disable, "disable U-APSD functionality (default: Y)");
1393 #endif
1394
1395 /*
1396  * set bt_coex_active to true, uCode will do kill/defer
1397  * every time the priority line is asserted (BT is sending signals on the
1398  * priority line in the PCIx).
1399  * set bt_coex_active to false, uCode will ignore the BT activity and
1400  * perform the normal operation
1401  *
1402  * User might experience transmit issue on some platform due to WiFi/BT
1403  * co-exist problem. The possible behaviors are:
1404  *   Able to scan and finding all the available AP
1405  *   Not able to associate with any AP
1406  * On those platforms, WiFi communication can be restored by set
1407  * "bt_coex_active" module parameter to "false"
1408  *
1409  * default: bt_coex_active = true (BT_COEX_ENABLE)
1410  */
1411 module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
1412                 bool, S_IRUGO);
1413 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
1414
1415 module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
1416 MODULE_PARM_DESC(led_mode, "0=system default, "
1417                 "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
1418
1419 module_param_named(power_save, iwlwifi_mod_params.power_save,
1420                 bool, S_IRUGO);
1421 MODULE_PARM_DESC(power_save,
1422                  "enable WiFi power management (default: disable)");
1423
1424 module_param_named(power_level, iwlwifi_mod_params.power_level,
1425                 int, S_IRUGO);
1426 MODULE_PARM_DESC(power_level,
1427                  "default power save level (range from 1 - 5, default: 1)");
1428
1429 module_param_named(fw_monitor, iwlwifi_mod_params.fw_monitor, bool, S_IRUGO);
1430 MODULE_PARM_DESC(fw_monitor,
1431                  "firmware monitor - to debug FW (default: false - needs lots of memory)");