6f312c77af5ef279388f6447884618ec386fbede
[platform/adaptation/renesas_rcar/renesas_kernel.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 - 2012 Intel Corporation. All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22  * USA
23  *
24  * The full GNU General Public License is included in this distribution
25  * in the file called LICENSE.GPL.
26  *
27  * Contact Information:
28  *  Intel Linux Wireless <ilw@linux.intel.com>
29  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30  *
31  * BSD LICENSE
32  *
33  * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  *
40  *  * Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  *  * Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in
44  *    the documentation and/or other materials provided with the
45  *    distribution.
46  *  * Neither the name Intel Corporation nor the names of its
47  *    contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  *
62  *****************************************************************************/
63 #include <linux/completion.h>
64 #include <linux/dma-mapping.h>
65 #include <linux/firmware.h>
66 #include <linux/module.h>
67
68 #include "iwl-drv.h"
69 #include "iwl-trans.h"
70 #include "iwl-shared.h"
71 #include "iwl-op-mode.h"
72 #include "iwl-agn-hw.h"
73
74 /* private includes */
75 #include "iwl-fw-file.h"
76
77 /**
78  * struct iwl_drv - drv common data
79  * @fw: the iwl_fw structure
80  * @shrd: pointer to common shared structure
81  * @op_mode: the running op_mode
82  * @fw_index: firmware revision to try loading
83  * @firmware_name: composite filename of ucode file to load
84  * @request_firmware_complete: the firmware has been obtained from user space
85  */
86 struct iwl_drv {
87         struct iwl_fw fw;
88
89         struct iwl_shared *shrd;
90         struct iwl_op_mode *op_mode;
91
92         int fw_index;                   /* firmware we're trying to load */
93         char firmware_name[25];         /* name of firmware file to load */
94
95         struct completion request_firmware_complete;
96 };
97
98
99
100 /*
101  * struct fw_sec: Just for the image parsing proccess.
102  * For the fw storage we are using struct fw_desc.
103  */
104 struct fw_sec {
105         const void *data;               /* the sec data */
106         size_t size;                    /* section size */
107         u32 offset;                     /* offset of writing in the device */
108 };
109
110 static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
111 {
112         if (desc->v_addr)
113                 dma_free_coherent(trans(drv)->dev, desc->len,
114                                   desc->v_addr, desc->p_addr);
115         desc->v_addr = NULL;
116         desc->len = 0;
117 }
118
119 static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
120 {
121         int i;
122         for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
123                 iwl_free_fw_desc(drv, &img->sec[i]);
124 }
125
126 static void iwl_dealloc_ucode(struct iwl_drv *drv)
127 {
128         int i;
129         for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
130                 iwl_free_fw_img(drv, drv->fw.img + i);
131 }
132
133 static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
134                       struct fw_sec *sec)
135 {
136         if (!sec || !sec->size) {
137                 desc->v_addr = NULL;
138                 return -EINVAL;
139         }
140
141         desc->v_addr = dma_alloc_coherent(trans(drv)->dev, sec->size,
142                                           &desc->p_addr, GFP_KERNEL);
143         if (!desc->v_addr)
144                 return -ENOMEM;
145
146         desc->len = sec->size;
147         desc->offset = sec->offset;
148         memcpy(desc->v_addr, sec->data, sec->size);
149         return 0;
150 }
151
152 static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
153
154 #define UCODE_EXPERIMENTAL_INDEX        100
155 #define UCODE_EXPERIMENTAL_TAG          "exp"
156
157 static int iwl_request_firmware(struct iwl_drv *drv, bool first)
158 {
159         const struct iwl_cfg *cfg = cfg(drv);
160         const char *name_pre = cfg->fw_name_pre;
161         char tag[8];
162
163         if (first) {
164 #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
165                 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
166                 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
167         } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
168 #endif
169                 drv->fw_index = cfg->ucode_api_max;
170                 sprintf(tag, "%d", drv->fw_index);
171         } else {
172                 drv->fw_index--;
173                 sprintf(tag, "%d", drv->fw_index);
174         }
175
176         if (drv->fw_index < cfg->ucode_api_min) {
177                 IWL_ERR(drv, "no suitable firmware found!\n");
178                 return -ENOENT;
179         }
180
181         sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
182
183         IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
184                        (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
185                                 ? "EXPERIMENTAL " : "",
186                        drv->firmware_name);
187
188         return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
189                                        trans(drv)->dev,
190                                        GFP_KERNEL, drv, iwl_ucode_callback);
191 }
192
193 struct fw_img_parsing {
194         struct fw_sec sec[IWL_UCODE_SECTION_MAX];
195         int sec_counter;
196 };
197
198 /*
199  * struct fw_sec_parsing: to extract fw section and it's offset from tlv
200  */
201 struct fw_sec_parsing {
202         __le32 offset;
203         const u8 data[];
204 } __packed;
205
206 /**
207  * struct iwl_tlv_calib_data - parse the default calib data from TLV
208  *
209  * @ucode_type: the uCode to which the following default calib relates.
210  * @calib: default calibrations.
211  */
212 struct iwl_tlv_calib_data {
213         __le32 ucode_type;
214         __le64 calib;
215 } __packed;
216
217 struct iwl_firmware_pieces {
218         struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
219
220         u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
221         u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
222 };
223
224 /*
225  * These functions are just to extract uCode section data from the pieces
226  * structure.
227  */
228 static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
229                               enum iwl_ucode_type type,
230                               int  sec)
231 {
232         return &pieces->img[type].sec[sec];
233 }
234
235 static void set_sec_data(struct iwl_firmware_pieces *pieces,
236                          enum iwl_ucode_type type,
237                          int sec,
238                          const void *data)
239 {
240         pieces->img[type].sec[sec].data = data;
241 }
242
243 static void set_sec_size(struct iwl_firmware_pieces *pieces,
244                          enum iwl_ucode_type type,
245                          int sec,
246                          size_t size)
247 {
248         pieces->img[type].sec[sec].size = size;
249 }
250
251 static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
252                            enum iwl_ucode_type type,
253                            int sec)
254 {
255         return pieces->img[type].sec[sec].size;
256 }
257
258 static void set_sec_offset(struct iwl_firmware_pieces *pieces,
259                            enum iwl_ucode_type type,
260                            int sec,
261                            u32 offset)
262 {
263         pieces->img[type].sec[sec].offset = offset;
264 }
265
266 /*
267  * Gets uCode section from tlv.
268  */
269 static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
270                                const void *data, enum iwl_ucode_type type,
271                                int size)
272 {
273         struct fw_img_parsing *img;
274         struct fw_sec *sec;
275         struct fw_sec_parsing *sec_parse;
276
277         if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
278                 return -1;
279
280         sec_parse = (struct fw_sec_parsing *)data;
281
282         img = &pieces->img[type];
283         sec = &img->sec[img->sec_counter];
284
285         sec->offset = le32_to_cpu(sec_parse->offset);
286         sec->data = sec_parse->data;
287
288         ++img->sec_counter;
289
290         return 0;
291 }
292
293 static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
294 {
295         struct iwl_tlv_calib_data *def_calib =
296                                         (struct iwl_tlv_calib_data *)data;
297         u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
298         if (ucode_type >= IWL_UCODE_TYPE_MAX) {
299                 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
300                         ucode_type);
301                 return -EINVAL;
302         }
303         drv->fw.default_calib[ucode_type] = le64_to_cpu(def_calib->calib);
304         return 0;
305 }
306
307 static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
308                                     const struct firmware *ucode_raw,
309                                     struct iwl_firmware_pieces *pieces)
310 {
311         struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
312         u32 api_ver, hdr_size, build;
313         char buildstr[25];
314         const u8 *src;
315
316         drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
317         api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
318
319         switch (api_ver) {
320         default:
321                 hdr_size = 28;
322                 if (ucode_raw->size < hdr_size) {
323                         IWL_ERR(drv, "File size too small!\n");
324                         return -EINVAL;
325                 }
326                 build = le32_to_cpu(ucode->u.v2.build);
327                 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
328                              le32_to_cpu(ucode->u.v2.inst_size));
329                 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
330                              le32_to_cpu(ucode->u.v2.data_size));
331                 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
332                              le32_to_cpu(ucode->u.v2.init_size));
333                 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
334                              le32_to_cpu(ucode->u.v2.init_data_size));
335                 src = ucode->u.v2.data;
336                 break;
337         case 0:
338         case 1:
339         case 2:
340                 hdr_size = 24;
341                 if (ucode_raw->size < hdr_size) {
342                         IWL_ERR(drv, "File size too small!\n");
343                         return -EINVAL;
344                 }
345                 build = 0;
346                 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
347                              le32_to_cpu(ucode->u.v1.inst_size));
348                 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
349                              le32_to_cpu(ucode->u.v1.data_size));
350                 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
351                              le32_to_cpu(ucode->u.v1.init_size));
352                 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
353                              le32_to_cpu(ucode->u.v1.init_data_size));
354                 src = ucode->u.v1.data;
355                 break;
356         }
357
358         if (build)
359                 sprintf(buildstr, " build %u%s", build,
360                        (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
361                                 ? " (EXP)" : "");
362         else
363                 buildstr[0] = '\0';
364
365         snprintf(drv->fw.fw_version,
366                  sizeof(drv->fw.fw_version),
367                  "%u.%u.%u.%u%s",
368                  IWL_UCODE_MAJOR(drv->fw.ucode_ver),
369                  IWL_UCODE_MINOR(drv->fw.ucode_ver),
370                  IWL_UCODE_API(drv->fw.ucode_ver),
371                  IWL_UCODE_SERIAL(drv->fw.ucode_ver),
372                  buildstr);
373
374         /* Verify size of file vs. image size info in file's header */
375
376         if (ucode_raw->size != hdr_size +
377             get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
378             get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
379             get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
380             get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
381
382                 IWL_ERR(drv,
383                         "uCode file size %d does not match expected size\n",
384                         (int)ucode_raw->size);
385                 return -EINVAL;
386         }
387
388
389         set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
390         src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
391         set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
392                        IWLAGN_RTC_INST_LOWER_BOUND);
393         set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
394         src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
395         set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
396                        IWLAGN_RTC_DATA_LOWER_BOUND);
397         set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
398         src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
399         set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
400                        IWLAGN_RTC_INST_LOWER_BOUND);
401         set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
402         src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
403         set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
404                        IWLAGN_RTC_DATA_LOWER_BOUND);
405         return 0;
406 }
407
408 static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
409                                 const struct firmware *ucode_raw,
410                                 struct iwl_firmware_pieces *pieces,
411                                 struct iwl_ucode_capabilities *capa)
412 {
413         struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
414         struct iwl_ucode_tlv *tlv;
415         size_t len = ucode_raw->size;
416         const u8 *data;
417         int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
418         int tmp;
419         u64 alternatives;
420         u32 tlv_len;
421         enum iwl_ucode_tlv_type tlv_type;
422         const u8 *tlv_data;
423         char buildstr[25];
424         u32 build;
425
426         if (len < sizeof(*ucode)) {
427                 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
428                 return -EINVAL;
429         }
430
431         if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
432                 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
433                         le32_to_cpu(ucode->magic));
434                 return -EINVAL;
435         }
436
437         /*
438          * Check which alternatives are present, and "downgrade"
439          * when the chosen alternative is not present, warning
440          * the user when that happens. Some files may not have
441          * any alternatives, so don't warn in that case.
442          */
443         alternatives = le64_to_cpu(ucode->alternatives);
444         tmp = wanted_alternative;
445         if (wanted_alternative > 63)
446                 wanted_alternative = 63;
447         while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
448                 wanted_alternative--;
449         if (wanted_alternative && wanted_alternative != tmp)
450                 IWL_WARN(drv,
451                          "uCode alternative %d not available, choosing %d\n",
452                          tmp, wanted_alternative);
453
454         drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
455         build = le32_to_cpu(ucode->build);
456
457         if (build)
458                 sprintf(buildstr, " build %u%s", build,
459                        (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
460                                 ? " (EXP)" : "");
461         else
462                 buildstr[0] = '\0';
463
464         snprintf(drv->fw.fw_version,
465                  sizeof(drv->fw.fw_version),
466                  "%u.%u.%u.%u%s",
467                  IWL_UCODE_MAJOR(drv->fw.ucode_ver),
468                  IWL_UCODE_MINOR(drv->fw.ucode_ver),
469                  IWL_UCODE_API(drv->fw.ucode_ver),
470                  IWL_UCODE_SERIAL(drv->fw.ucode_ver),
471                  buildstr);
472
473         data = ucode->data;
474
475         len -= sizeof(*ucode);
476
477         while (len >= sizeof(*tlv)) {
478                 u16 tlv_alt;
479
480                 len -= sizeof(*tlv);
481                 tlv = (void *)data;
482
483                 tlv_len = le32_to_cpu(tlv->length);
484                 tlv_type = le16_to_cpu(tlv->type);
485                 tlv_alt = le16_to_cpu(tlv->alternative);
486                 tlv_data = tlv->data;
487
488                 if (len < tlv_len) {
489                         IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
490                                 len, tlv_len);
491                         return -EINVAL;
492                 }
493                 len -= ALIGN(tlv_len, 4);
494                 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
495
496                 /*
497                  * Alternative 0 is always valid.
498                  *
499                  * Skip alternative TLVs that are not selected.
500                  */
501                 if (tlv_alt != 0 && tlv_alt != wanted_alternative)
502                         continue;
503
504                 switch (tlv_type) {
505                 case IWL_UCODE_TLV_INST:
506                         set_sec_data(pieces, IWL_UCODE_REGULAR,
507                                      IWL_UCODE_SECTION_INST, tlv_data);
508                         set_sec_size(pieces, IWL_UCODE_REGULAR,
509                                      IWL_UCODE_SECTION_INST, tlv_len);
510                         set_sec_offset(pieces, IWL_UCODE_REGULAR,
511                                        IWL_UCODE_SECTION_INST,
512                                        IWLAGN_RTC_INST_LOWER_BOUND);
513                         break;
514                 case IWL_UCODE_TLV_DATA:
515                         set_sec_data(pieces, IWL_UCODE_REGULAR,
516                                      IWL_UCODE_SECTION_DATA, tlv_data);
517                         set_sec_size(pieces, IWL_UCODE_REGULAR,
518                                      IWL_UCODE_SECTION_DATA, tlv_len);
519                         set_sec_offset(pieces, IWL_UCODE_REGULAR,
520                                        IWL_UCODE_SECTION_DATA,
521                                        IWLAGN_RTC_DATA_LOWER_BOUND);
522                         break;
523                 case IWL_UCODE_TLV_INIT:
524                         set_sec_data(pieces, IWL_UCODE_INIT,
525                                      IWL_UCODE_SECTION_INST, tlv_data);
526                         set_sec_size(pieces, IWL_UCODE_INIT,
527                                      IWL_UCODE_SECTION_INST, tlv_len);
528                         set_sec_offset(pieces, IWL_UCODE_INIT,
529                                        IWL_UCODE_SECTION_INST,
530                                        IWLAGN_RTC_INST_LOWER_BOUND);
531                         break;
532                 case IWL_UCODE_TLV_INIT_DATA:
533                         set_sec_data(pieces, IWL_UCODE_INIT,
534                                      IWL_UCODE_SECTION_DATA, tlv_data);
535                         set_sec_size(pieces, IWL_UCODE_INIT,
536                                      IWL_UCODE_SECTION_DATA, tlv_len);
537                         set_sec_offset(pieces, IWL_UCODE_INIT,
538                                        IWL_UCODE_SECTION_DATA,
539                                        IWLAGN_RTC_DATA_LOWER_BOUND);
540                         break;
541                 case IWL_UCODE_TLV_BOOT:
542                         IWL_ERR(drv, "Found unexpected BOOT ucode\n");
543                         break;
544                 case IWL_UCODE_TLV_PROBE_MAX_LEN:
545                         if (tlv_len != sizeof(u32))
546                                 goto invalid_tlv_len;
547                         capa->max_probe_length =
548                                         le32_to_cpup((__le32 *)tlv_data);
549                         break;
550                 case IWL_UCODE_TLV_PAN:
551                         if (tlv_len)
552                                 goto invalid_tlv_len;
553                         capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
554                         break;
555                 case IWL_UCODE_TLV_FLAGS:
556                         /* must be at least one u32 */
557                         if (tlv_len < sizeof(u32))
558                                 goto invalid_tlv_len;
559                         /* and a proper number of u32s */
560                         if (tlv_len % sizeof(u32))
561                                 goto invalid_tlv_len;
562                         /*
563                          * This driver only reads the first u32 as
564                          * right now no more features are defined,
565                          * if that changes then either the driver
566                          * will not work with the new firmware, or
567                          * it'll not take advantage of new features.
568                          */
569                         capa->flags = le32_to_cpup((__le32 *)tlv_data);
570                         break;
571                 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
572                         if (tlv_len != sizeof(u32))
573                                 goto invalid_tlv_len;
574                         pieces->init_evtlog_ptr =
575                                         le32_to_cpup((__le32 *)tlv_data);
576                         break;
577                 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
578                         if (tlv_len != sizeof(u32))
579                                 goto invalid_tlv_len;
580                         pieces->init_evtlog_size =
581                                         le32_to_cpup((__le32 *)tlv_data);
582                         break;
583                 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
584                         if (tlv_len != sizeof(u32))
585                                 goto invalid_tlv_len;
586                         pieces->init_errlog_ptr =
587                                         le32_to_cpup((__le32 *)tlv_data);
588                         break;
589                 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
590                         if (tlv_len != sizeof(u32))
591                                 goto invalid_tlv_len;
592                         pieces->inst_evtlog_ptr =
593                                         le32_to_cpup((__le32 *)tlv_data);
594                         break;
595                 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
596                         if (tlv_len != sizeof(u32))
597                                 goto invalid_tlv_len;
598                         pieces->inst_evtlog_size =
599                                         le32_to_cpup((__le32 *)tlv_data);
600                         break;
601                 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
602                         if (tlv_len != sizeof(u32))
603                                 goto invalid_tlv_len;
604                         pieces->inst_errlog_ptr =
605                                         le32_to_cpup((__le32 *)tlv_data);
606                         break;
607                 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
608                         if (tlv_len)
609                                 goto invalid_tlv_len;
610                         drv->fw.enhance_sensitivity_table = true;
611                         break;
612                 case IWL_UCODE_TLV_WOWLAN_INST:
613                         set_sec_data(pieces, IWL_UCODE_WOWLAN,
614                                      IWL_UCODE_SECTION_INST, tlv_data);
615                         set_sec_size(pieces, IWL_UCODE_WOWLAN,
616                                      IWL_UCODE_SECTION_INST, tlv_len);
617                         set_sec_offset(pieces, IWL_UCODE_WOWLAN,
618                                        IWL_UCODE_SECTION_INST,
619                                        IWLAGN_RTC_INST_LOWER_BOUND);
620                         break;
621                 case IWL_UCODE_TLV_WOWLAN_DATA:
622                         set_sec_data(pieces, IWL_UCODE_WOWLAN,
623                                      IWL_UCODE_SECTION_DATA, tlv_data);
624                         set_sec_size(pieces, IWL_UCODE_WOWLAN,
625                                      IWL_UCODE_SECTION_DATA, tlv_len);
626                         set_sec_offset(pieces, IWL_UCODE_WOWLAN,
627                                        IWL_UCODE_SECTION_DATA,
628                                        IWLAGN_RTC_DATA_LOWER_BOUND);
629                         break;
630                 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
631                         if (tlv_len != sizeof(u32))
632                                 goto invalid_tlv_len;
633                         capa->standard_phy_calibration_size =
634                                         le32_to_cpup((__le32 *)tlv_data);
635                         break;
636                  case IWL_UCODE_TLV_SEC_RT:
637                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
638                                             tlv_len);
639                         drv->fw.mvm_fw = true;
640                         break;
641                 case IWL_UCODE_TLV_SEC_INIT:
642                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
643                                             tlv_len);
644                         drv->fw.mvm_fw = true;
645                         break;
646                 case IWL_UCODE_TLV_SEC_WOWLAN:
647                         iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
648                                             tlv_len);
649                         drv->fw.mvm_fw = true;
650                         break;
651                 case IWL_UCODE_TLV_DEF_CALIB:
652                         if (tlv_len != sizeof(struct iwl_tlv_calib_data))
653                                 goto invalid_tlv_len;
654                         if (iwl_set_default_calib(drv, tlv_data))
655                                 goto tlv_error;
656                         break;
657                 case IWL_UCODE_TLV_PHY_SKU:
658                         if (tlv_len != sizeof(u32))
659                                 goto invalid_tlv_len;
660                         drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
661                         break;
662                 default:
663                         IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
664                         break;
665                 }
666         }
667
668         if (len) {
669                 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
670                 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
671                 return -EINVAL;
672         }
673
674         return 0;
675
676  invalid_tlv_len:
677         IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
678  tlv_error:
679         iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
680
681         return -EINVAL;
682 }
683
684 static int alloc_pci_desc(struct iwl_drv *drv,
685                           struct iwl_firmware_pieces *pieces,
686                           enum iwl_ucode_type type)
687 {
688         int i;
689         for (i = 0;
690              i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
691              i++)
692                 if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
693                                                 get_sec(pieces, type, i)))
694                         return -1;
695         return 0;
696 }
697
698 static int validate_sec_sizes(struct iwl_drv *drv,
699                               struct iwl_firmware_pieces *pieces,
700                               const struct iwl_cfg *cfg)
701 {
702         IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
703                 get_sec_size(pieces, IWL_UCODE_REGULAR,
704                              IWL_UCODE_SECTION_INST));
705         IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
706                 get_sec_size(pieces, IWL_UCODE_REGULAR,
707                              IWL_UCODE_SECTION_DATA));
708         IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
709                 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
710         IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
711                 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
712
713         /* Verify that uCode images will fit in card's SRAM. */
714         if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
715                                                         cfg->max_inst_size) {
716                 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
717                         get_sec_size(pieces, IWL_UCODE_REGULAR,
718                                                 IWL_UCODE_SECTION_INST));
719                 return -1;
720         }
721
722         if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
723                                                         cfg->max_data_size) {
724                 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
725                         get_sec_size(pieces, IWL_UCODE_REGULAR,
726                                                 IWL_UCODE_SECTION_DATA));
727                 return -1;
728         }
729
730          if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
731                                                         cfg->max_inst_size) {
732                 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
733                         get_sec_size(pieces, IWL_UCODE_INIT,
734                                                 IWL_UCODE_SECTION_INST));
735                 return -1;
736         }
737
738         if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
739                                                         cfg->max_data_size) {
740                 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
741                         get_sec_size(pieces, IWL_UCODE_REGULAR,
742                                                 IWL_UCODE_SECTION_DATA));
743                 return -1;
744         }
745         return 0;
746 }
747
748
749 /**
750  * iwl_ucode_callback - callback when firmware was loaded
751  *
752  * If loaded successfully, copies the firmware into buffers
753  * for the card to fetch (via DMA).
754  */
755 static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
756 {
757         struct iwl_drv *drv = context;
758         const struct iwl_cfg *cfg = cfg(drv);
759         struct iwl_fw *fw = &drv->fw;
760         struct iwl_ucode_header *ucode;
761         int err;
762         struct iwl_firmware_pieces pieces;
763         const unsigned int api_max = cfg->ucode_api_max;
764         unsigned int api_ok = cfg->ucode_api_ok;
765         const unsigned int api_min = cfg->ucode_api_min;
766         u32 api_ver;
767         int i;
768
769         fw->ucode_capa.max_probe_length = 200;
770         fw->ucode_capa.standard_phy_calibration_size =
771                         IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
772
773         if (!api_ok)
774                 api_ok = api_max;
775
776         memset(&pieces, 0, sizeof(pieces));
777
778         if (!ucode_raw) {
779                 if (drv->fw_index <= api_ok)
780                         IWL_ERR(drv,
781                                 "request for firmware file '%s' failed.\n",
782                                 drv->firmware_name);
783                 goto try_again;
784         }
785
786         IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
787                        drv->firmware_name, ucode_raw->size);
788
789         /* Make sure that we got at least the API version number */
790         if (ucode_raw->size < 4) {
791                 IWL_ERR(drv, "File size way too small!\n");
792                 goto try_again;
793         }
794
795         /* Data from ucode file:  header followed by uCode images */
796         ucode = (struct iwl_ucode_header *)ucode_raw->data;
797
798         if (ucode->ver)
799                 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
800         else
801                 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
802                                            &fw->ucode_capa);
803
804         if (err)
805                 goto try_again;
806
807         api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
808
809         /*
810          * api_ver should match the api version forming part of the
811          * firmware filename ... but we don't check for that and only rely
812          * on the API version read from firmware header from here on forward
813          */
814         /* no api version check required for experimental uCode */
815         if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
816                 if (api_ver < api_min || api_ver > api_max) {
817                         IWL_ERR(drv,
818                                 "Driver unable to support your firmware API. "
819                                 "Driver supports v%u, firmware is v%u.\n",
820                                 api_max, api_ver);
821                         goto try_again;
822                 }
823
824                 if (api_ver < api_ok) {
825                         if (api_ok != api_max)
826                                 IWL_ERR(drv, "Firmware has old API version, "
827                                         "expected v%u through v%u, got v%u.\n",
828                                         api_ok, api_max, api_ver);
829                         else
830                                 IWL_ERR(drv, "Firmware has old API version, "
831                                         "expected v%u, got v%u.\n",
832                                         api_max, api_ver);
833                         IWL_ERR(drv, "New firmware can be obtained from "
834                                       "http://www.intellinuxwireless.org/.\n");
835                 }
836         }
837
838         IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
839
840         /*
841          * For any of the failures below (before allocating pci memory)
842          * we will try to load a version with a smaller API -- maybe the
843          * user just got a corrupted version of the latest API.
844          */
845
846         IWL_DEBUG_INFO(drv, "f/w package hdr ucode version raw = 0x%x\n",
847                        drv->fw.ucode_ver);
848         IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
849                 get_sec_size(&pieces, IWL_UCODE_REGULAR,
850                              IWL_UCODE_SECTION_INST));
851         IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
852                 get_sec_size(&pieces, IWL_UCODE_REGULAR,
853                              IWL_UCODE_SECTION_DATA));
854         IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
855                 get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
856         IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
857                 get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
858
859         /* Verify that uCode images will fit in card's SRAM */
860         if (get_sec_size(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
861                                                         cfg->max_inst_size) {
862                 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
863                         get_sec_size(&pieces, IWL_UCODE_REGULAR,
864                                      IWL_UCODE_SECTION_INST));
865                 goto try_again;
866         }
867
868         if (get_sec_size(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
869                                                         cfg->max_data_size) {
870                 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
871                         get_sec_size(&pieces, IWL_UCODE_REGULAR,
872                                      IWL_UCODE_SECTION_DATA));
873                 goto try_again;
874         }
875
876         /*
877          * In mvm uCode there is no difference between data and instructions
878          * sections.
879          */
880         if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, cfg))
881                 goto try_again;
882
883         /* Allocate ucode buffers for card's bus-master loading ... */
884
885         /* Runtime instructions and 2 copies of data:
886          * 1) unmodified from disk
887          * 2) backup cache for save/restore during power-downs */
888         for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
889                 if (alloc_pci_desc(drv, &pieces, i))
890                         goto err_pci_alloc;
891
892         /* Now that we can no longer fail, copy information */
893
894         /*
895          * The (size - 16) / 12 formula is based on the information recorded
896          * for each event, which is of mode 1 (including timestamp) for all
897          * new microcodes that include this information.
898          */
899         fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
900         if (pieces.init_evtlog_size)
901                 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
902         else
903                 fw->init_evtlog_size =
904                         cfg->base_params->max_event_log_size;
905         fw->init_errlog_ptr = pieces.init_errlog_ptr;
906         fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
907         if (pieces.inst_evtlog_size)
908                 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
909         else
910                 fw->inst_evtlog_size =
911                         cfg->base_params->max_event_log_size;
912         fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
913
914         /*
915          * figure out the offset of chain noise reset and gain commands
916          * base on the size of standard phy calibration commands table size
917          */
918         if (fw->ucode_capa.standard_phy_calibration_size >
919             IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
920                 fw->ucode_capa.standard_phy_calibration_size =
921                         IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
922
923         /* We have our copies now, allow OS release its copies */
924         release_firmware(ucode_raw);
925         complete(&drv->request_firmware_complete);
926
927         drv->op_mode = iwl_dvm_ops.start(drv->shrd->trans, &drv->fw);
928
929         if (!drv->op_mode)
930                 goto out_unbind;
931
932         return;
933
934  try_again:
935         /* try next, if any */
936         release_firmware(ucode_raw);
937         if (iwl_request_firmware(drv, false))
938                 goto out_unbind;
939         return;
940
941  err_pci_alloc:
942         IWL_ERR(drv, "failed to allocate pci memory\n");
943         iwl_dealloc_ucode(drv);
944         release_firmware(ucode_raw);
945  out_unbind:
946         complete(&drv->request_firmware_complete);
947         device_release_driver(trans(drv)->dev);
948 }
949
950 int iwl_drv_start(struct iwl_shared *shrd,
951                   struct iwl_trans *trans, const struct iwl_cfg *cfg)
952 {
953         struct iwl_drv *drv;
954         int ret;
955
956         shrd->cfg = cfg;
957
958         drv = kzalloc(sizeof(*drv), GFP_KERNEL);
959         if (!drv) {
960                 dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_drv");
961                 return -ENOMEM;
962         }
963         drv->shrd = shrd;
964         shrd->drv = drv;
965
966         init_completion(&drv->request_firmware_complete);
967
968         ret = iwl_request_firmware(drv, true);
969
970         if (ret) {
971                 dev_printk(KERN_ERR, trans->dev, "Couldn't request the fw");
972                 kfree(drv);
973                 shrd->drv = NULL;
974         }
975
976         return ret;
977 }
978
979 void iwl_drv_stop(struct iwl_shared *shrd)
980 {
981         struct iwl_drv *drv = shrd->drv;
982
983         wait_for_completion(&drv->request_firmware_complete);
984
985         /* op_mode can be NULL if its start failed */
986         if (drv->op_mode)
987                 iwl_op_mode_stop(drv->op_mode);
988
989         iwl_dealloc_ucode(drv);
990
991         kfree(drv);
992         shrd->drv = NULL;
993 }