]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/net/wireless/iwlwifi/iwl-drv.c
iwlwifi: move firmware completion wait
[mv-sheeva.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-wifi.h"
71 #include "iwl-shared.h"
72 #include "iwl-op-mode.h"
73
74 static void iwl_free_fw_desc(struct iwl_nic *nic, struct fw_desc *desc)
75 {
76         if (desc->v_addr)
77                 dma_free_coherent(trans(nic)->dev, desc->len,
78                                   desc->v_addr, desc->p_addr);
79         desc->v_addr = NULL;
80         desc->len = 0;
81 }
82
83 static void iwl_free_fw_img(struct iwl_nic *nic, struct fw_img *img)
84 {
85         iwl_free_fw_desc(nic, &img->code);
86         iwl_free_fw_desc(nic, &img->data);
87 }
88
89 static void iwl_dealloc_ucode(struct iwl_nic *nic)
90 {
91         iwl_free_fw_img(nic, &nic->fw.ucode_rt);
92         iwl_free_fw_img(nic, &nic->fw.ucode_init);
93         iwl_free_fw_img(nic, &nic->fw.ucode_wowlan);
94 }
95
96 static int iwl_alloc_fw_desc(struct iwl_nic *nic, struct fw_desc *desc,
97                       const void *data, size_t len)
98 {
99         if (!len) {
100                 desc->v_addr = NULL;
101                 return -EINVAL;
102         }
103
104         desc->v_addr = dma_alloc_coherent(trans(nic)->dev, len,
105                                           &desc->p_addr, GFP_KERNEL);
106         if (!desc->v_addr)
107                 return -ENOMEM;
108
109         desc->len = len;
110         memcpy(desc->v_addr, data, len);
111         return 0;
112 }
113
114 static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
115
116 #define UCODE_EXPERIMENTAL_INDEX        100
117 #define UCODE_EXPERIMENTAL_TAG          "exp"
118
119 static int iwl_request_firmware(struct iwl_nic *nic, bool first)
120 {
121         const struct iwl_cfg *cfg = cfg(nic);
122         const char *name_pre = cfg->fw_name_pre;
123         char tag[8];
124
125         if (first) {
126 #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
127                 nic->fw_index = UCODE_EXPERIMENTAL_INDEX;
128                 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
129         } else if (nic->fw_index == UCODE_EXPERIMENTAL_INDEX) {
130 #endif
131                 nic->fw_index = cfg->ucode_api_max;
132                 sprintf(tag, "%d", nic->fw_index);
133         } else {
134                 nic->fw_index--;
135                 sprintf(tag, "%d", nic->fw_index);
136         }
137
138         if (nic->fw_index < cfg->ucode_api_min) {
139                 IWL_ERR(nic, "no suitable firmware found!\n");
140                 return -ENOENT;
141         }
142
143         sprintf(nic->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
144
145         IWL_DEBUG_INFO(nic, "attempting to load firmware %s'%s'\n",
146                        (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
147                                 ? "EXPERIMENTAL " : "",
148                        nic->firmware_name);
149
150         return request_firmware_nowait(THIS_MODULE, 1, nic->firmware_name,
151                                        trans(nic)->dev,
152                                        GFP_KERNEL, nic, iwl_ucode_callback);
153 }
154
155 struct iwlagn_firmware_pieces {
156         const void *inst, *data, *init, *init_data, *wowlan_inst, *wowlan_data;
157         size_t inst_size, data_size, init_size, init_data_size,
158                wowlan_inst_size, wowlan_data_size;
159
160         u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
161         u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
162 };
163
164 static int iwl_parse_v1_v2_firmware(struct iwl_nic *nic,
165                                        const struct firmware *ucode_raw,
166                                        struct iwlagn_firmware_pieces *pieces)
167 {
168         struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
169         u32 api_ver, hdr_size, build;
170         char buildstr[25];
171         const u8 *src;
172
173         nic->fw.ucode_ver = le32_to_cpu(ucode->ver);
174         api_ver = IWL_UCODE_API(nic->fw.ucode_ver);
175
176         switch (api_ver) {
177         default:
178                 hdr_size = 28;
179                 if (ucode_raw->size < hdr_size) {
180                         IWL_ERR(nic, "File size too small!\n");
181                         return -EINVAL;
182                 }
183                 build = le32_to_cpu(ucode->u.v2.build);
184                 pieces->inst_size = le32_to_cpu(ucode->u.v2.inst_size);
185                 pieces->data_size = le32_to_cpu(ucode->u.v2.data_size);
186                 pieces->init_size = le32_to_cpu(ucode->u.v2.init_size);
187                 pieces->init_data_size =
188                         le32_to_cpu(ucode->u.v2.init_data_size);
189                 src = ucode->u.v2.data;
190                 break;
191         case 0:
192         case 1:
193         case 2:
194                 hdr_size = 24;
195                 if (ucode_raw->size < hdr_size) {
196                         IWL_ERR(nic, "File size too small!\n");
197                         return -EINVAL;
198                 }
199                 build = 0;
200                 pieces->inst_size = le32_to_cpu(ucode->u.v1.inst_size);
201                 pieces->data_size = le32_to_cpu(ucode->u.v1.data_size);
202                 pieces->init_size = le32_to_cpu(ucode->u.v1.init_size);
203                 pieces->init_data_size =
204                         le32_to_cpu(ucode->u.v1.init_data_size);
205                 src = ucode->u.v1.data;
206                 break;
207         }
208
209         if (build)
210                 sprintf(buildstr, " build %u%s", build,
211                        (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
212                                 ? " (EXP)" : "");
213         else
214                 buildstr[0] = '\0';
215
216         snprintf(nic->fw.fw_version,
217                  sizeof(nic->fw.fw_version),
218                  "%u.%u.%u.%u%s",
219                  IWL_UCODE_MAJOR(nic->fw.ucode_ver),
220                  IWL_UCODE_MINOR(nic->fw.ucode_ver),
221                  IWL_UCODE_API(nic->fw.ucode_ver),
222                  IWL_UCODE_SERIAL(nic->fw.ucode_ver),
223                  buildstr);
224
225         /* Verify size of file vs. image size info in file's header */
226         if (ucode_raw->size != hdr_size + pieces->inst_size +
227                                 pieces->data_size + pieces->init_size +
228                                 pieces->init_data_size) {
229
230                 IWL_ERR(nic,
231                         "uCode file size %d does not match expected size\n",
232                         (int)ucode_raw->size);
233                 return -EINVAL;
234         }
235
236         pieces->inst = src;
237         src += pieces->inst_size;
238         pieces->data = src;
239         src += pieces->data_size;
240         pieces->init = src;
241         src += pieces->init_size;
242         pieces->init_data = src;
243         src += pieces->init_data_size;
244
245         return 0;
246 }
247
248 static int iwl_parse_tlv_firmware(struct iwl_nic *nic,
249                                 const struct firmware *ucode_raw,
250                                 struct iwlagn_firmware_pieces *pieces,
251                                 struct iwl_ucode_capabilities *capa)
252 {
253         struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
254         struct iwl_ucode_tlv *tlv;
255         size_t len = ucode_raw->size;
256         const u8 *data;
257         int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
258         int tmp;
259         u64 alternatives;
260         u32 tlv_len;
261         enum iwl_ucode_tlv_type tlv_type;
262         const u8 *tlv_data;
263         char buildstr[25];
264         u32 build;
265
266         if (len < sizeof(*ucode)) {
267                 IWL_ERR(nic, "uCode has invalid length: %zd\n", len);
268                 return -EINVAL;
269         }
270
271         if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
272                 IWL_ERR(nic, "invalid uCode magic: 0X%x\n",
273                         le32_to_cpu(ucode->magic));
274                 return -EINVAL;
275         }
276
277         /*
278          * Check which alternatives are present, and "downgrade"
279          * when the chosen alternative is not present, warning
280          * the user when that happens. Some files may not have
281          * any alternatives, so don't warn in that case.
282          */
283         alternatives = le64_to_cpu(ucode->alternatives);
284         tmp = wanted_alternative;
285         if (wanted_alternative > 63)
286                 wanted_alternative = 63;
287         while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
288                 wanted_alternative--;
289         if (wanted_alternative && wanted_alternative != tmp)
290                 IWL_WARN(nic,
291                          "uCode alternative %d not available, choosing %d\n",
292                          tmp, wanted_alternative);
293
294         nic->fw.ucode_ver = le32_to_cpu(ucode->ver);
295         build = le32_to_cpu(ucode->build);
296
297         if (build)
298                 sprintf(buildstr, " build %u%s", build,
299                        (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
300                                 ? " (EXP)" : "");
301         else
302                 buildstr[0] = '\0';
303
304         snprintf(nic->fw.fw_version,
305                  sizeof(nic->fw.fw_version),
306                  "%u.%u.%u.%u%s",
307                  IWL_UCODE_MAJOR(nic->fw.ucode_ver),
308                  IWL_UCODE_MINOR(nic->fw.ucode_ver),
309                  IWL_UCODE_API(nic->fw.ucode_ver),
310                  IWL_UCODE_SERIAL(nic->fw.ucode_ver),
311                  buildstr);
312
313         data = ucode->data;
314
315         len -= sizeof(*ucode);
316
317         while (len >= sizeof(*tlv)) {
318                 u16 tlv_alt;
319
320                 len -= sizeof(*tlv);
321                 tlv = (void *)data;
322
323                 tlv_len = le32_to_cpu(tlv->length);
324                 tlv_type = le16_to_cpu(tlv->type);
325                 tlv_alt = le16_to_cpu(tlv->alternative);
326                 tlv_data = tlv->data;
327
328                 if (len < tlv_len) {
329                         IWL_ERR(nic, "invalid TLV len: %zd/%u\n",
330                                 len, tlv_len);
331                         return -EINVAL;
332                 }
333                 len -= ALIGN(tlv_len, 4);
334                 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
335
336                 /*
337                  * Alternative 0 is always valid.
338                  *
339                  * Skip alternative TLVs that are not selected.
340                  */
341                 if (tlv_alt != 0 && tlv_alt != wanted_alternative)
342                         continue;
343
344                 switch (tlv_type) {
345                 case IWL_UCODE_TLV_INST:
346                         pieces->inst = tlv_data;
347                         pieces->inst_size = tlv_len;
348                         break;
349                 case IWL_UCODE_TLV_DATA:
350                         pieces->data = tlv_data;
351                         pieces->data_size = tlv_len;
352                         break;
353                 case IWL_UCODE_TLV_INIT:
354                         pieces->init = tlv_data;
355                         pieces->init_size = tlv_len;
356                         break;
357                 case IWL_UCODE_TLV_INIT_DATA:
358                         pieces->init_data = tlv_data;
359                         pieces->init_data_size = tlv_len;
360                         break;
361                 case IWL_UCODE_TLV_BOOT:
362                         IWL_ERR(nic, "Found unexpected BOOT ucode\n");
363                         break;
364                 case IWL_UCODE_TLV_PROBE_MAX_LEN:
365                         if (tlv_len != sizeof(u32))
366                                 goto invalid_tlv_len;
367                         capa->max_probe_length =
368                                         le32_to_cpup((__le32 *)tlv_data);
369                         break;
370                 case IWL_UCODE_TLV_PAN:
371                         if (tlv_len)
372                                 goto invalid_tlv_len;
373                         capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
374                         break;
375                 case IWL_UCODE_TLV_FLAGS:
376                         /* must be at least one u32 */
377                         if (tlv_len < sizeof(u32))
378                                 goto invalid_tlv_len;
379                         /* and a proper number of u32s */
380                         if (tlv_len % sizeof(u32))
381                                 goto invalid_tlv_len;
382                         /*
383                          * This driver only reads the first u32 as
384                          * right now no more features are defined,
385                          * if that changes then either the driver
386                          * will not work with the new firmware, or
387                          * it'll not take advantage of new features.
388                          */
389                         capa->flags = le32_to_cpup((__le32 *)tlv_data);
390                         break;
391                 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
392                         if (tlv_len != sizeof(u32))
393                                 goto invalid_tlv_len;
394                         pieces->init_evtlog_ptr =
395                                         le32_to_cpup((__le32 *)tlv_data);
396                         break;
397                 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
398                         if (tlv_len != sizeof(u32))
399                                 goto invalid_tlv_len;
400                         pieces->init_evtlog_size =
401                                         le32_to_cpup((__le32 *)tlv_data);
402                         break;
403                 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
404                         if (tlv_len != sizeof(u32))
405                                 goto invalid_tlv_len;
406                         pieces->init_errlog_ptr =
407                                         le32_to_cpup((__le32 *)tlv_data);
408                         break;
409                 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
410                         if (tlv_len != sizeof(u32))
411                                 goto invalid_tlv_len;
412                         pieces->inst_evtlog_ptr =
413                                         le32_to_cpup((__le32 *)tlv_data);
414                         break;
415                 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
416                         if (tlv_len != sizeof(u32))
417                                 goto invalid_tlv_len;
418                         pieces->inst_evtlog_size =
419                                         le32_to_cpup((__le32 *)tlv_data);
420                         break;
421                 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
422                         if (tlv_len != sizeof(u32))
423                                 goto invalid_tlv_len;
424                         pieces->inst_errlog_ptr =
425                                         le32_to_cpup((__le32 *)tlv_data);
426                         break;
427                 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
428                         if (tlv_len)
429                                 goto invalid_tlv_len;
430                         nic->fw.enhance_sensitivity_table = true;
431                         break;
432                 case IWL_UCODE_TLV_WOWLAN_INST:
433                         pieces->wowlan_inst = tlv_data;
434                         pieces->wowlan_inst_size = tlv_len;
435                         break;
436                 case IWL_UCODE_TLV_WOWLAN_DATA:
437                         pieces->wowlan_data = tlv_data;
438                         pieces->wowlan_data_size = tlv_len;
439                         break;
440                 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
441                         if (tlv_len != sizeof(u32))
442                                 goto invalid_tlv_len;
443                         capa->standard_phy_calibration_size =
444                                         le32_to_cpup((__le32 *)tlv_data);
445                         break;
446                 default:
447                         IWL_DEBUG_INFO(nic, "unknown TLV: %d\n", tlv_type);
448                         break;
449                 }
450         }
451
452         if (len) {
453                 IWL_ERR(nic, "invalid TLV after parsing: %zd\n", len);
454                 iwl_print_hex_dump(nic, IWL_DL_FW, (u8 *)data, len);
455                 return -EINVAL;
456         }
457
458         return 0;
459
460  invalid_tlv_len:
461         IWL_ERR(nic, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
462         iwl_print_hex_dump(nic, IWL_DL_FW, tlv_data, tlv_len);
463
464         return -EINVAL;
465 }
466
467 /**
468  * iwl_ucode_callback - callback when firmware was loaded
469  *
470  * If loaded successfully, copies the firmware into buffers
471  * for the card to fetch (via DMA).
472  */
473 static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
474 {
475         struct iwl_nic *nic = context;
476         const struct iwl_cfg *cfg = cfg(nic);
477         struct iwl_fw *fw = &nic->fw;
478         struct iwl_ucode_header *ucode;
479         int err;
480         struct iwlagn_firmware_pieces pieces;
481         const unsigned int api_max = cfg->ucode_api_max;
482         unsigned int api_ok = cfg->ucode_api_ok;
483         const unsigned int api_min = cfg->ucode_api_min;
484         u32 api_ver;
485
486         fw->ucode_capa.max_probe_length = 200;
487         fw->ucode_capa.standard_phy_calibration_size =
488                         IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
489
490         if (!api_ok)
491                 api_ok = api_max;
492
493         memset(&pieces, 0, sizeof(pieces));
494
495         if (!ucode_raw) {
496                 if (nic->fw_index <= api_ok)
497                         IWL_ERR(nic,
498                                 "request for firmware file '%s' failed.\n",
499                                 nic->firmware_name);
500                 goto try_again;
501         }
502
503         IWL_DEBUG_INFO(nic, "Loaded firmware file '%s' (%zd bytes).\n",
504                        nic->firmware_name, ucode_raw->size);
505
506         /* Make sure that we got at least the API version number */
507         if (ucode_raw->size < 4) {
508                 IWL_ERR(nic, "File size way too small!\n");
509                 goto try_again;
510         }
511
512         /* Data from ucode file:  header followed by uCode images */
513         ucode = (struct iwl_ucode_header *)ucode_raw->data;
514
515         if (ucode->ver)
516                 err = iwl_parse_v1_v2_firmware(nic, ucode_raw, &pieces);
517         else
518                 err = iwl_parse_tlv_firmware(nic, ucode_raw, &pieces,
519                                            &fw->ucode_capa);
520
521         if (err)
522                 goto try_again;
523
524         api_ver = IWL_UCODE_API(nic->fw.ucode_ver);
525
526         /*
527          * api_ver should match the api version forming part of the
528          * firmware filename ... but we don't check for that and only rely
529          * on the API version read from firmware header from here on forward
530          */
531         /* no api version check required for experimental uCode */
532         if (nic->fw_index != UCODE_EXPERIMENTAL_INDEX) {
533                 if (api_ver < api_min || api_ver > api_max) {
534                         IWL_ERR(nic,
535                                 "Driver unable to support your firmware API. "
536                                 "Driver supports v%u, firmware is v%u.\n",
537                                 api_max, api_ver);
538                         goto try_again;
539                 }
540
541                 if (api_ver < api_ok) {
542                         if (api_ok != api_max)
543                                 IWL_ERR(nic, "Firmware has old API version, "
544                                         "expected v%u through v%u, got v%u.\n",
545                                         api_ok, api_max, api_ver);
546                         else
547                                 IWL_ERR(nic, "Firmware has old API version, "
548                                         "expected v%u, got v%u.\n",
549                                         api_max, api_ver);
550                         IWL_ERR(nic, "New firmware can be obtained from "
551                                       "http://www.intellinuxwireless.org/.\n");
552                 }
553         }
554
555         IWL_INFO(nic, "loaded firmware version %s", nic->fw.fw_version);
556
557         /*
558          * For any of the failures below (before allocating pci memory)
559          * we will try to load a version with a smaller API -- maybe the
560          * user just got a corrupted version of the latest API.
561          */
562
563         IWL_DEBUG_INFO(nic, "f/w package hdr ucode version raw = 0x%x\n",
564                        nic->fw.ucode_ver);
565         IWL_DEBUG_INFO(nic, "f/w package hdr runtime inst size = %Zd\n",
566                        pieces.inst_size);
567         IWL_DEBUG_INFO(nic, "f/w package hdr runtime data size = %Zd\n",
568                        pieces.data_size);
569         IWL_DEBUG_INFO(nic, "f/w package hdr init inst size = %Zd\n",
570                        pieces.init_size);
571         IWL_DEBUG_INFO(nic, "f/w package hdr init data size = %Zd\n",
572                        pieces.init_data_size);
573
574         /* Verify that uCode images will fit in card's SRAM */
575         if (pieces.inst_size > cfg->max_inst_size) {
576                 IWL_ERR(nic, "uCode instr len %Zd too large to fit in\n",
577                         pieces.inst_size);
578                 goto try_again;
579         }
580
581         if (pieces.data_size > cfg->max_data_size) {
582                 IWL_ERR(nic, "uCode data len %Zd too large to fit in\n",
583                         pieces.data_size);
584                 goto try_again;
585         }
586
587         if (pieces.init_size > cfg->max_inst_size) {
588                 IWL_ERR(nic, "uCode init instr len %Zd too large to fit in\n",
589                         pieces.init_size);
590                 goto try_again;
591         }
592
593         if (pieces.init_data_size > cfg->max_data_size) {
594                 IWL_ERR(nic, "uCode init data len %Zd too large to fit in\n",
595                         pieces.init_data_size);
596                 goto try_again;
597         }
598
599         /* Allocate ucode buffers for card's bus-master loading ... */
600
601         /* Runtime instructions and 2 copies of data:
602          * 1) unmodified from disk
603          * 2) backup cache for save/restore during power-downs */
604         if (iwl_alloc_fw_desc(nic, &nic->fw.ucode_rt.code,
605                               pieces.inst, pieces.inst_size))
606                 goto err_pci_alloc;
607         if (iwl_alloc_fw_desc(nic, &nic->fw.ucode_rt.data,
608                               pieces.data, pieces.data_size))
609                 goto err_pci_alloc;
610
611         /* Initialization instructions and data */
612         if (pieces.init_size && pieces.init_data_size) {
613                 if (iwl_alloc_fw_desc(nic,
614                                       &nic->fw.ucode_init.code,
615                                       pieces.init, pieces.init_size))
616                         goto err_pci_alloc;
617                 if (iwl_alloc_fw_desc(nic,
618                                       &nic->fw.ucode_init.data,
619                                       pieces.init_data, pieces.init_data_size))
620                         goto err_pci_alloc;
621         }
622
623         /* WoWLAN instructions and data */
624         if (pieces.wowlan_inst_size && pieces.wowlan_data_size) {
625                 if (iwl_alloc_fw_desc(nic,
626                                       &nic->fw.ucode_wowlan.code,
627                                       pieces.wowlan_inst,
628                                       pieces.wowlan_inst_size))
629                         goto err_pci_alloc;
630                 if (iwl_alloc_fw_desc(nic,
631                                       &nic->fw.ucode_wowlan.data,
632                                       pieces.wowlan_data,
633                                       pieces.wowlan_data_size))
634                         goto err_pci_alloc;
635         }
636
637         /* Now that we can no longer fail, copy information */
638
639         /*
640          * The (size - 16) / 12 formula is based on the information recorded
641          * for each event, which is of mode 1 (including timestamp) for all
642          * new microcodes that include this information.
643          */
644         nic->init_evtlog_ptr = pieces.init_evtlog_ptr;
645         if (pieces.init_evtlog_size)
646                 nic->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
647         else
648                 nic->init_evtlog_size =
649                         cfg->base_params->max_event_log_size;
650         nic->init_errlog_ptr = pieces.init_errlog_ptr;
651         nic->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
652         if (pieces.inst_evtlog_size)
653                 nic->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
654         else
655                 nic->inst_evtlog_size =
656                         cfg->base_params->max_event_log_size;
657         nic->inst_errlog_ptr = pieces.inst_errlog_ptr;
658
659         /*
660          * figure out the offset of chain noise reset and gain commands
661          * base on the size of standard phy calibration commands table size
662          */
663         if (fw->ucode_capa.standard_phy_calibration_size >
664             IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
665                 fw->ucode_capa.standard_phy_calibration_size =
666                         IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
667
668         /* We have our copies now, allow OS release its copies */
669         release_firmware(ucode_raw);
670         complete(&nic->request_firmware_complete);
671
672         nic->op_mode = iwl_dvm_ops.start(nic->shrd->trans);
673
674         if (!nic->op_mode)
675                 goto out_unbind;
676
677         return;
678
679  try_again:
680         /* try next, if any */
681         release_firmware(ucode_raw);
682         if (iwl_request_firmware(nic, false))
683                 goto out_unbind;
684         return;
685
686  err_pci_alloc:
687         IWL_ERR(nic, "failed to allocate pci memory\n");
688         iwl_dealloc_ucode(nic);
689         release_firmware(ucode_raw);
690  out_unbind:
691         complete(&nic->request_firmware_complete);
692         device_release_driver(trans(nic)->dev);
693 }
694
695 int iwl_drv_start(struct iwl_shared *shrd,
696                   struct iwl_trans *trans, const struct iwl_cfg *cfg)
697 {
698         int ret;
699
700         shrd->cfg = cfg;
701
702         shrd->nic = kzalloc(sizeof(*shrd->nic), GFP_KERNEL);
703         if (!shrd->nic) {
704                 dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_nic");
705                 return -ENOMEM;
706         }
707         shrd->nic->shrd = shrd;
708
709         init_completion(&shrd->nic->request_firmware_complete);
710
711         ret = iwl_request_firmware(shrd->nic, true);
712
713         if (ret) {
714                 dev_printk(KERN_ERR, trans->dev, "Couldn't request the fw");
715                 kfree(shrd->nic);
716         }
717
718         return ret;
719 }
720
721 void iwl_drv_stop(struct iwl_shared *shrd)
722 {
723         wait_for_completion(&shrd->nic->request_firmware_complete);
724
725         /* op_mode can be NULL if its start failed */
726         if (shrd->nic->op_mode)
727                 iwl_op_mode_stop(shrd->nic->op_mode);
728
729         iwl_dealloc_ucode(shrd->nic);
730
731         kfree(shrd->nic);
732 }