iwl-agn.c 98.2 KB
Newer Older
1
2
/******************************************************************************
 *
Wey-Yi Guy's avatar
Wey-Yi Guy committed
3
 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * Portions of this file are derived from the ipw3945 project, as well
 * as portions of the ieee80211 subsystem header files.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 *
 * The full GNU General Public License is included in this distribution in the
 * file called LICENSE.
 *
 * Contact Information:
25
 *  Intel Linux Wireless <ilw@linux.intel.com>
26
27
28
29
30
31
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 *****************************************************************************/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
32
#include <linux/slab.h>
33
34
#include <linux/dma-mapping.h>
#include <linux/delay.h>
35
#include <linux/sched.h>
36
37
38
39
40
41
42
43
44
45
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/firmware.h>
#include <linux/etherdevice.h>
#include <linux/if_arp.h>

#include <net/mac80211.h>

#include <asm/div64.h>

46
#include "iwl-eeprom.h"
47
#include "iwl-dev.h"
48
#include "iwl-core.h"
49
#include "iwl-io.h"
50
#include "iwl-helpers.h"
51
#include "iwl-sta.h"
Johannes Berg's avatar
Johannes Berg committed
52
#include "iwl-agn-calib.h"
53
#include "iwl-agn.h"
54
#include "iwl-shared.h"
55
#include "iwl-bus.h"
56
#include "iwl-trans.h"
57

58
59
60
61
62
63
64
65
66
/******************************************************************************
 *
 * module boiler plate
 *
 ******************************************************************************/

/*
 * module name, copyright, version, etc.
 */
67
#define DRV_DESCRIPTION	"Intel(R) Wireless WiFi Link AGN driver for Linux"
68

69
#ifdef CONFIG_IWLWIFI_DEBUG
70
71
72
73
74
#define VD "d"
#else
#define VD
#endif

75
#define DRV_VERSION     IWLWIFI_VERSION VD
76
77
78
79


MODULE_DESCRIPTION(DRV_DESCRIPTION);
MODULE_VERSION(DRV_VERSION);
80
MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
81
82
MODULE_LICENSE("GPL");

83
void iwl_update_chain_flags(struct iwl_priv *priv)
84
{
85
	struct iwl_rxon_context *ctx;
86

Wey-Yi Guy's avatar
Wey-Yi Guy committed
87
88
89
90
	for_each_context(priv, ctx) {
		iwlagn_set_rxon_chain(priv, ctx);
		if (ctx->active.rx_chain != ctx->staging.rx_chain)
			iwlagn_commit_rxon(priv, ctx);
91
	}
92
93
}

94
95
/* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
static void iwl_set_beacon_tim(struct iwl_priv *priv,
96
97
			       struct iwl_tx_beacon_cmd *tx_beacon_cmd,
			       u8 *beacon, u32 frame_size)
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{
	u16 tim_idx;
	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;

	/*
	 * The index is relative to frame start but we start looking at the
	 * variable-length part of the beacon.
	 */
	tim_idx = mgmt->u.beacon.variable - beacon;

	/* Parse variable-length elements of beacon to find WLAN_EID_TIM */
	while ((tim_idx < (frame_size - 2)) &&
			(beacon[tim_idx] != WLAN_EID_TIM))
		tim_idx += beacon[tim_idx+1] + 2;

	/* If TIM field was found, set variables */
	if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
		tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
		tx_beacon_cmd->tim_size = beacon[tim_idx+1];
	} else
		IWL_WARN(priv, "Unable to find TIM Element in beacon\n");
}

121
int iwlagn_send_beacon_cmd(struct iwl_priv *priv)
122
123
{
	struct iwl_tx_beacon_cmd *tx_beacon_cmd;
124
125
	struct iwl_host_cmd cmd = {
		.id = REPLY_TX_BEACON,
126
		.flags = CMD_SYNC,
127
	};
128
	struct ieee80211_tx_info *info;
129
130
131
	u32 frame_size;
	u32 rate_flags;
	u32 rate;
132

133
134
135
136
	/*
	 * We have to set up the TX command, the TX Beacon command, and the
	 * beacon contents.
	 */
137

138
	lockdep_assert_held(&priv->shrd->mutex);
139
140
141

	if (!priv->beacon_ctx) {
		IWL_ERR(priv, "trying to build beacon w/o beacon context!\n");
142
		return 0;
143
144
	}

145
146
147
	if (WARN_ON(!priv->beacon_skb))
		return -EINVAL;

148
149
150
151
	/* Allocate beacon command */
	if (!priv->beacon_cmd)
		priv->beacon_cmd = kzalloc(sizeof(*tx_beacon_cmd), GFP_KERNEL);
	tx_beacon_cmd = priv->beacon_cmd;
152
153
154
155
	if (!tx_beacon_cmd)
		return -ENOMEM;

	frame_size = priv->beacon_skb->len;
156

157
	/* Set up TX command fields */
158
	tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
159
	tx_beacon_cmd->tx.sta_id = priv->beacon_ctx->bcast_sta_id;
160
161
162
	tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
	tx_beacon_cmd->tx.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK |
		TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK;
163

164
	/* Set up TX beacon command fields */
165
	iwl_set_beacon_tim(priv, tx_beacon_cmd, priv->beacon_skb->data,
166
			   frame_size);
167

168
	/* Set up packet rate and flags */
169
170
171
172
173
174
175
176
177
178
179
180
181
	info = IEEE80211_SKB_CB(priv->beacon_skb);

	/*
	 * Let's set up the rate at least somewhat correctly;
	 * it will currently not actually be used by the uCode,
	 * it uses the broadcast station's rate instead.
	 */
	if (info->control.rates[0].idx < 0 ||
	    info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
		rate = 0;
	else
		rate = info->control.rates[0].idx;

182
	priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
183
					      hw_params(priv).valid_tx_ant);
184
	rate_flags = iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
185
186
187
188
189

	/* In mac80211, rates for 5 GHz start at 0 */
	if (info->band == IEEE80211_BAND_5GHZ)
		rate += IWL_FIRST_OFDM_RATE;
	else if (rate >= IWL_FIRST_CCK_RATE && rate <= IWL_LAST_CCK_RATE)
190
		rate_flags |= RATE_MCS_CCK_MSK;
191
192
193

	tx_beacon_cmd->tx.rate_n_flags =
			iwl_hw_set_rate_n_flags(rate, rate_flags);
194

195
	/* Submit command */
196
	cmd.len[0] = sizeof(*tx_beacon_cmd);
197
	cmd.data[0] = tx_beacon_cmd;
198
199
200
201
	cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
	cmd.len[1] = frame_size;
	cmd.data[1] = priv->beacon_skb->data;
	cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
202

203
	return iwl_trans_send_cmd(trans(priv), &cmd);
204
205
}

206
static void iwl_bg_beacon_update(struct work_struct *work)
207
{
208
209
	struct iwl_priv *priv =
		container_of(work, struct iwl_priv, beacon_update);
210
211
	struct sk_buff *beacon;

212
	mutex_lock(&priv->shrd->mutex);
213
214
215
216
	if (!priv->beacon_ctx) {
		IWL_ERR(priv, "updating beacon w/o beacon context!\n");
		goto out;
	}
217

218
219
220
221
222
223
224
225
226
227
	if (priv->beacon_ctx->vif->type != NL80211_IFTYPE_AP) {
		/*
		 * The ucode will send beacon notifications even in
		 * IBSS mode, but we don't want to process them. But
		 * we need to defer the type check to here due to
		 * requiring locking around the beacon_ctx access.
		 */
		goto out;
	}

228
229
	/* Pull updated AP beacon from mac80211. will fail if not in AP mode */
	beacon = ieee80211_beacon_get(priv->hw, priv->beacon_ctx->vif);
230
	if (!beacon) {
231
		IWL_ERR(priv, "update beacon failed -- keeping old\n");
232
		goto out;
233
234
235
	}

	/* new beacon skb is allocated every time; dispose previous.*/
236
	dev_kfree_skb(priv->beacon_skb);
237

238
	priv->beacon_skb = beacon;
239

240
	iwlagn_send_beacon_cmd(priv);
241
 out:
242
	mutex_unlock(&priv->shrd->mutex);
243
244
}

245
246
247
248
249
static void iwl_bg_bt_runtime_config(struct work_struct *work)
{
	struct iwl_priv *priv =
		container_of(work, struct iwl_priv, bt_runtime_config);

250
	if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
251
252
253
254
255
		return;

	/* dont send host command if rf-kill is on */
	if (!iwl_is_ready_rf(priv))
		return;
Wey-Yi Guy's avatar
Wey-Yi Guy committed
256
	iwlagn_send_advance_bt_config(priv);
257
258
}

259
260
261
262
static void iwl_bg_bt_full_concurrency(struct work_struct *work)
{
	struct iwl_priv *priv =
		container_of(work, struct iwl_priv, bt_full_concurrency);
263
	struct iwl_rxon_context *ctx;
264

265
	mutex_lock(&priv->shrd->mutex);
266

267
	if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
268
		goto out;
269
270
271

	/* dont send host command if rf-kill is on */
	if (!iwl_is_ready_rf(priv))
272
		goto out;
273
274
275
276
277
278
279
280
281

	IWL_DEBUG_INFO(priv, "BT coex in %s mode\n",
		       priv->bt_full_concurrent ?
		       "full concurrency" : "3-wire");

	/*
	 * LQ & RXON updated cmds must be sent before BT Config cmd
	 * to avoid 3-wire collisions
	 */
282
	for_each_context(priv, ctx) {
Wey-Yi Guy's avatar
Wey-Yi Guy committed
283
		iwlagn_set_rxon_chain(priv, ctx);
284
		iwlagn_commit_rxon(priv, ctx);
285
	}
286

Wey-Yi Guy's avatar
Wey-Yi Guy committed
287
	iwlagn_send_advance_bt_config(priv);
288
out:
289
	mutex_unlock(&priv->shrd->mutex);
290
291
}

292
/**
293
 * iwl_bg_statistics_periodic - Timer callback to queue statistics
294
295
296
297
298
299
300
301
 *
 * This callback is provided in order to send a statistics request.
 *
 * This timer function is continually reset to execute within
 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
 * was received.  We need to ensure we receive the statistics in order
 * to update the temperature used for calibrating the TXPOWER.
 */
302
static void iwl_bg_statistics_periodic(unsigned long data)
303
304
305
{
	struct iwl_priv *priv = (struct iwl_priv *)data;

306
	if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
307
308
		return;

309
310
311
312
	/* dont send host command if rf-kill is on */
	if (!iwl_is_ready_rf(priv))
		return;

313
	iwl_send_statistics_request(priv, CMD_ASYNC, false);
314
315
}

316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338

static void iwl_print_cont_event_trace(struct iwl_priv *priv, u32 base,
					u32 start_idx, u32 num_events,
					u32 mode)
{
	u32 i;
	u32 ptr;        /* SRAM byte address of log data */
	u32 ev, time, data; /* event log data */
	unsigned long reg_flags;

	if (mode == 0)
		ptr = base + (4 * sizeof(u32)) + (start_idx * 2 * sizeof(u32));
	else
		ptr = base + (4 * sizeof(u32)) + (start_idx * 3 * sizeof(u32));

	/* Make sure device is powered up for SRAM reads */
	spin_lock_irqsave(&priv->reg_lock, reg_flags);
	if (iwl_grab_nic_access(priv)) {
		spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
		return;
	}

	/* Set starting address; reads will auto-increment */
339
	iwl_write32(priv, HBUS_TARG_MEM_RADDR, ptr);
340
341
342
343
344
345
346
	rmb();

	/*
	 * "time" is actually "data" for mode 0 (no timestamp).
	 * place event id # at far right for easier visual parsing.
	 */
	for (i = 0; i < num_events; i++) {
347
348
		ev = iwl_read32(priv, HBUS_TARG_MEM_RDAT);
		time = iwl_read32(priv, HBUS_TARG_MEM_RDAT);
349
350
351
352
		if (mode == 0) {
			trace_iwlwifi_dev_ucode_cont_event(priv,
							0, time, ev);
		} else {
353
			data = iwl_read32(priv, HBUS_TARG_MEM_RDAT);
354
355
356
357
358
359
360
361
362
			trace_iwlwifi_dev_ucode_cont_event(priv,
						time, data, ev);
		}
	}
	/* Allow device to power down */
	iwl_release_nic_access(priv);
	spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
}

Johannes Berg's avatar
Johannes Berg committed
363
static void iwl_continuous_event_trace(struct iwl_priv *priv)
364
365
366
367
368
369
370
{
	u32 capacity;   /* event log capacity in # entries */
	u32 base;       /* SRAM byte address of event log header */
	u32 mode;       /* 0 - no timestamp, 1 - timestamp recorded */
	u32 num_wraps;  /* # times uCode wrapped to top of log */
	u32 next_entry; /* index of next entry to be written by uCode */

371
	base = priv->device_pointers.error_event_table;
372
	if (iwlagn_hw_valid_rtc_data_addr(base)) {
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
		capacity = iwl_read_targ_mem(priv, base);
		num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
		mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
		next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
	} else
		return;

	if (num_wraps == priv->event_log.num_wraps) {
		iwl_print_cont_event_trace(priv,
				       base, priv->event_log.next_entry,
				       next_entry - priv->event_log.next_entry,
				       mode);
		priv->event_log.non_wraps_count++;
	} else {
		if ((num_wraps - priv->event_log.num_wraps) > 1)
			priv->event_log.wraps_more_count++;
		else
			priv->event_log.wraps_once_count++;
		trace_iwlwifi_dev_ucode_wrap_event(priv,
				num_wraps - priv->event_log.num_wraps,
				next_entry, priv->event_log.next_entry);
		if (next_entry < priv->event_log.next_entry) {
			iwl_print_cont_event_trace(priv, base,
			       priv->event_log.next_entry,
			       capacity - priv->event_log.next_entry,
			       mode);

			iwl_print_cont_event_trace(priv, base, 0,
				next_entry, mode);
		} else {
			iwl_print_cont_event_trace(priv, base,
			       next_entry, capacity - next_entry,
			       mode);

			iwl_print_cont_event_trace(priv, base, 0,
				next_entry, mode);
		}
	}
	priv->event_log.num_wraps = num_wraps;
	priv->event_log.next_entry = next_entry;
}

/**
 * iwl_bg_ucode_trace - Timer callback to log ucode event
 *
 * The timer is continually set to execute every
 * UCODE_TRACE_PERIOD milliseconds after the last timer expired
 * this function is to perform continuous uCode event logging operation
 * if enabled
 */
static void iwl_bg_ucode_trace(unsigned long data)
{
	struct iwl_priv *priv = (struct iwl_priv *)data;

427
	if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
428
429
430
431
432
433
434
435
436
437
		return;

	if (priv->event_log.ucode_trace) {
		iwl_continuous_event_trace(priv);
		/* Reschedule the timer to occur in UCODE_TRACE_PERIOD */
		mod_timer(&priv->ucode_trace,
			 jiffies + msecs_to_jiffies(UCODE_TRACE_PERIOD));
	}
}

438
439
440
441
442
static void iwl_bg_tx_flush(struct work_struct *work)
{
	struct iwl_priv *priv =
		container_of(work, struct iwl_priv, tx_flush);

443
	if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
444
445
446
447
448
449
		return;

	/* do nothing if rf-kill is on */
	if (!iwl_is_ready_rf(priv))
		return;

450
451
	IWL_DEBUG_INFO(priv, "device request: flush all tx frames\n");
	iwlagn_dev_txfifo_flush(priv, IWL_DROP_ALL);
452
453
}

454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*****************************************************************************
 *
 * sysfs attributes
 *
 *****************************************************************************/

#ifdef CONFIG_IWLWIFI_DEBUG

/*
 * The following adds a new attribute to the sysfs representation
 * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
 * used for controlling the debug level.
 *
 * See the level definitions in iwl for details.
 *
 * The debug_level being managed using sysfs below is a per device debug
 * level that is used instead of the global debug level if it (the per
 * device debug level) is set.
 */
static ssize_t show_debug_level(struct device *d,
				struct device_attribute *attr, char *buf)
{
476
	struct iwl_shared *shrd = dev_get_drvdata(d);
477
	return sprintf(buf, "0x%08X\n", iwl_get_debug_level(shrd));
478
479
480
481
482
}
static ssize_t store_debug_level(struct device *d,
				struct device_attribute *attr,
				 const char *buf, size_t count)
{
483
484
	struct iwl_shared *shrd = dev_get_drvdata(d);
	struct iwl_priv *priv = shrd->priv;
485
486
487
488
489
490
491
	unsigned long val;
	int ret;

	ret = strict_strtoul(buf, 0, &val);
	if (ret)
		IWL_ERR(priv, "%s is not in hex or decimal form.\n", buf);
	else {
492
		shrd->dbg_level_dev = val;
493
		if (iwl_alloc_traffic_mem(priv))
494
			IWL_ERR(shrd->priv,
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
				"Not enough memory to generate traffic log\n");
	}
	return strnlen(buf, count);
}

static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
			show_debug_level, store_debug_level);


#endif /* CONFIG_IWLWIFI_DEBUG */


static ssize_t show_temperature(struct device *d,
				struct device_attribute *attr, char *buf)
{
510
511
	struct iwl_shared *shrd = dev_get_drvdata(d);
	struct iwl_priv *priv = shrd->priv;
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569

	if (!iwl_is_alive(priv))
		return -EAGAIN;

	return sprintf(buf, "%d\n", priv->temperature);
}

static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);

static ssize_t show_tx_power(struct device *d,
			     struct device_attribute *attr, char *buf)
{
	struct iwl_priv *priv = dev_get_drvdata(d);

	if (!iwl_is_ready_rf(priv))
		return sprintf(buf, "off\n");
	else
		return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
}

static ssize_t store_tx_power(struct device *d,
			      struct device_attribute *attr,
			      const char *buf, size_t count)
{
	struct iwl_priv *priv = dev_get_drvdata(d);
	unsigned long val;
	int ret;

	ret = strict_strtoul(buf, 10, &val);
	if (ret)
		IWL_INFO(priv, "%s is not in decimal form.\n", buf);
	else {
		ret = iwl_set_tx_power(priv, val, false);
		if (ret)
			IWL_ERR(priv, "failed setting tx power (0x%d).\n",
				ret);
		else
			ret = count;
	}
	return ret;
}

static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);

static struct attribute *iwl_sysfs_entries[] = {
	&dev_attr_temperature.attr,
	&dev_attr_tx_power.attr,
#ifdef CONFIG_IWLWIFI_DEBUG
	&dev_attr_debug_level.attr,
#endif
	NULL
};

static struct attribute_group iwl_attribute_group = {
	.name = NULL,		/* put in device directory */
	.attrs = iwl_sysfs_entries,
};

570
571
572
573
574
575
/******************************************************************************
 *
 * uCode download functions
 *
 ******************************************************************************/

576
static void iwl_free_fw_desc(struct iwl_priv *priv, struct fw_desc *desc)
577
578
{
	if (desc->v_addr)
579
		dma_free_coherent(priv->bus->dev, desc->len,
580
581
582
583
584
				  desc->v_addr, desc->p_addr);
	desc->v_addr = NULL;
	desc->len = 0;
}

585
static void iwl_free_fw_img(struct iwl_priv *priv, struct fw_img *img)
586
{
587
588
	iwl_free_fw_desc(priv, &img->code);
	iwl_free_fw_desc(priv, &img->data);
589
590
}

591
592
593
594
static void iwl_dealloc_ucode(struct iwl_priv *priv)
{
	iwl_free_fw_img(priv, &priv->ucode_rt);
	iwl_free_fw_img(priv, &priv->ucode_init);
Johannes Berg's avatar
Johannes Berg committed
595
	iwl_free_fw_img(priv, &priv->ucode_wowlan);
596
597
598
}

static int iwl_alloc_fw_desc(struct iwl_priv *priv, struct fw_desc *desc,
599
600
601
602
603
604
605
			     const void *data, size_t len)
{
	if (!len) {
		desc->v_addr = NULL;
		return -EINVAL;
	}

606
	desc->v_addr = dma_alloc_coherent(priv->bus->dev, len,
607
608
609
					  &desc->p_addr, GFP_KERNEL);
	if (!desc->v_addr)
		return -ENOMEM;
610

611
612
613
614
615
	desc->len = len;
	memcpy(desc->v_addr, data, len);
	return 0;
}

616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
static void iwl_init_context(struct iwl_priv *priv, u32 ucode_flags)
{
	static const u8 iwlagn_bss_ac_to_fifo[] = {
		IWL_TX_FIFO_VO,
		IWL_TX_FIFO_VI,
		IWL_TX_FIFO_BE,
		IWL_TX_FIFO_BK,
	};
	static const u8 iwlagn_bss_ac_to_queue[] = {
		0, 1, 2, 3,
	};
	static const u8 iwlagn_pan_ac_to_fifo[] = {
		IWL_TX_FIFO_VO_IPAN,
		IWL_TX_FIFO_VI_IPAN,
		IWL_TX_FIFO_BE_IPAN,
		IWL_TX_FIFO_BK_IPAN,
	};
	static const u8 iwlagn_pan_ac_to_queue[] = {
		7, 6, 5, 4,
	};
	int i;

	/*
	 * The default context is always valid,
	 * the PAN context depends on uCode.
	 */
	priv->valid_contexts = BIT(IWL_RXON_CTX_BSS);
	if (ucode_flags & IWL_UCODE_TLV_FLAGS_PAN)
		priv->valid_contexts |= BIT(IWL_RXON_CTX_PAN);

	for (i = 0; i < NUM_IWL_RXON_CTX; i++)
		priv->contexts[i].ctxid = i;

	priv->contexts[IWL_RXON_CTX_BSS].always_active = true;
	priv->contexts[IWL_RXON_CTX_BSS].is_active = true;
	priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
	priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
	priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
	priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
	priv->contexts[IWL_RXON_CTX_BSS].ap_sta_id = IWL_AP_ID;
	priv->contexts[IWL_RXON_CTX_BSS].wep_key_cmd = REPLY_WEPKEY;
	priv->contexts[IWL_RXON_CTX_BSS].ac_to_fifo = iwlagn_bss_ac_to_fifo;
	priv->contexts[IWL_RXON_CTX_BSS].ac_to_queue = iwlagn_bss_ac_to_queue;
	priv->contexts[IWL_RXON_CTX_BSS].exclusive_interface_modes =
		BIT(NL80211_IFTYPE_ADHOC);
	priv->contexts[IWL_RXON_CTX_BSS].interface_modes =
		BIT(NL80211_IFTYPE_STATION);
	priv->contexts[IWL_RXON_CTX_BSS].ap_devtype = RXON_DEV_TYPE_AP;
	priv->contexts[IWL_RXON_CTX_BSS].ibss_devtype = RXON_DEV_TYPE_IBSS;
	priv->contexts[IWL_RXON_CTX_BSS].station_devtype = RXON_DEV_TYPE_ESS;
	priv->contexts[IWL_RXON_CTX_BSS].unused_devtype = RXON_DEV_TYPE_ESS;

	priv->contexts[IWL_RXON_CTX_PAN].rxon_cmd = REPLY_WIPAN_RXON;
	priv->contexts[IWL_RXON_CTX_PAN].rxon_timing_cmd =
		REPLY_WIPAN_RXON_TIMING;
	priv->contexts[IWL_RXON_CTX_PAN].rxon_assoc_cmd =
		REPLY_WIPAN_RXON_ASSOC;
	priv->contexts[IWL_RXON_CTX_PAN].qos_cmd = REPLY_WIPAN_QOS_PARAM;
	priv->contexts[IWL_RXON_CTX_PAN].ap_sta_id = IWL_AP_ID_PAN;
	priv->contexts[IWL_RXON_CTX_PAN].wep_key_cmd = REPLY_WIPAN_WEPKEY;
	priv->contexts[IWL_RXON_CTX_PAN].bcast_sta_id = IWLAGN_PAN_BCAST_ID;
	priv->contexts[IWL_RXON_CTX_PAN].station_flags = STA_FLG_PAN_STATION;
	priv->contexts[IWL_RXON_CTX_PAN].ac_to_fifo = iwlagn_pan_ac_to_fifo;
	priv->contexts[IWL_RXON_CTX_PAN].ac_to_queue = iwlagn_pan_ac_to_queue;
	priv->contexts[IWL_RXON_CTX_PAN].mcast_queue = IWL_IPAN_MCAST_QUEUE;
	priv->contexts[IWL_RXON_CTX_PAN].interface_modes =
		BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP);
683
684
685
686
687
688

	if (ucode_flags & IWL_UCODE_TLV_FLAGS_P2P)
		priv->contexts[IWL_RXON_CTX_PAN].interface_modes |=
			BIT(NL80211_IFTYPE_P2P_CLIENT) |
			BIT(NL80211_IFTYPE_P2P_GO);

689
690
691
692
693
694
695
696
	priv->contexts[IWL_RXON_CTX_PAN].ap_devtype = RXON_DEV_TYPE_CP;
	priv->contexts[IWL_RXON_CTX_PAN].station_devtype = RXON_DEV_TYPE_2STA;
	priv->contexts[IWL_RXON_CTX_PAN].unused_devtype = RXON_DEV_TYPE_P2P;

	BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
}


697
698
struct iwlagn_ucode_capabilities {
	u32 max_probe_length;
699
	u32 standard_phy_calibration_size;
Johannes Berg's avatar
Johannes Berg committed
700
	u32 flags;
701
};
702

703
static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
704
705
static int iwl_mac_setup_register(struct iwl_priv *priv,
				  struct iwlagn_ucode_capabilities *capa);
706

707
708
709
#define UCODE_EXPERIMENTAL_INDEX	100
#define UCODE_EXPERIMENTAL_TAG		"exp"

710
711
712
static int __must_check iwl_request_firmware(struct iwl_priv *priv, bool first)
{
	const char *name_pre = priv->cfg->fw_name_pre;
713
	char tag[8];
714

715
716
717
718
719
720
	if (first) {
#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
		priv->fw_index = UCODE_EXPERIMENTAL_INDEX;
		strcpy(tag, UCODE_EXPERIMENTAL_TAG);
	} else if (priv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
#endif
721
		priv->fw_index = priv->cfg->ucode_api_max;
722
723
		sprintf(tag, "%d", priv->fw_index);
	} else {
724
		priv->fw_index--;
725
726
		sprintf(tag, "%d", priv->fw_index);
	}
727
728
729
730
731
732

	if (priv->fw_index < priv->cfg->ucode_api_min) {
		IWL_ERR(priv, "no suitable firmware found!\n");
		return -ENOENT;
	}

733
	sprintf(priv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
734

735
736
737
	IWL_DEBUG_INFO(priv, "attempting to load firmware %s'%s'\n",
		       (priv->fw_index == UCODE_EXPERIMENTAL_INDEX)
				? "EXPERIMENTAL " : "",
738
739
740
		       priv->firmware_name);

	return request_firmware_nowait(THIS_MODULE, 1, priv->firmware_name,
741
				       priv->bus->dev,
742
				       GFP_KERNEL, priv, iwl_ucode_callback);
743
744
}

745
struct iwlagn_firmware_pieces {
Johannes Berg's avatar
Johannes Berg committed
746
747
748
	const void *inst, *data, *init, *init_data, *wowlan_inst, *wowlan_data;
	size_t inst_size, data_size, init_size, init_data_size,
	       wowlan_inst_size, wowlan_data_size;
749
750

	u32 build;
751
752
753

	u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
	u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
};

static int iwlagn_load_legacy_firmware(struct iwl_priv *priv,
				       const struct firmware *ucode_raw,
				       struct iwlagn_firmware_pieces *pieces)
{
	struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
	u32 api_ver, hdr_size;
	const u8 *src;

	priv->ucode_ver = le32_to_cpu(ucode->ver);
	api_ver = IWL_UCODE_API(priv->ucode_ver);

	switch (api_ver) {
	default:
769
770
771
772
		hdr_size = 28;
		if (ucode_raw->size < hdr_size) {
			IWL_ERR(priv, "File size too small!\n");
			return -EINVAL;
773
		}
774
775
776
777
778
779
780
		pieces->build = le32_to_cpu(ucode->u.v2.build);
		pieces->inst_size = le32_to_cpu(ucode->u.v2.inst_size);
		pieces->data_size = le32_to_cpu(ucode->u.v2.data_size);
		pieces->init_size = le32_to_cpu(ucode->u.v2.init_size);
		pieces->init_data_size = le32_to_cpu(ucode->u.v2.init_data_size);
		src = ucode->u.v2.data;
		break;
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
	case 0:
	case 1:
	case 2:
		hdr_size = 24;
		if (ucode_raw->size < hdr_size) {
			IWL_ERR(priv, "File size too small!\n");
			return -EINVAL;
		}
		pieces->build = 0;
		pieces->inst_size = le32_to_cpu(ucode->u.v1.inst_size);
		pieces->data_size = le32_to_cpu(ucode->u.v1.data_size);
		pieces->init_size = le32_to_cpu(ucode->u.v1.init_size);
		pieces->init_data_size = le32_to_cpu(ucode->u.v1.init_data_size);
		src = ucode->u.v1.data;
		break;
	}

	/* Verify size of file vs. image size info in file's header */
	if (ucode_raw->size != hdr_size + pieces->inst_size +
				pieces->data_size + pieces->init_size +
Johannes Berg's avatar
Johannes Berg committed
801
				pieces->init_data_size) {
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820

		IWL_ERR(priv,
			"uCode file size %d does not match expected size\n",
			(int)ucode_raw->size);
		return -EINVAL;
	}

	pieces->inst = src;
	src += pieces->inst_size;
	pieces->data = src;
	src += pieces->data_size;
	pieces->init = src;
	src += pieces->init_size;
	pieces->init_data = src;
	src += pieces->init_data_size;

	return 0;
}

821
822
823
824
825
826
827
828
829
static int iwlagn_load_firmware(struct iwl_priv *priv,
				const struct firmware *ucode_raw,
				struct iwlagn_firmware_pieces *pieces,
				struct iwlagn_ucode_capabilities *capa)
{
	struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
	struct iwl_ucode_tlv *tlv;
	size_t len = ucode_raw->size;
	const u8 *data;
830
831
	int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
	int tmp;
832
	u64 alternatives;
833
834
835
	u32 tlv_len;
	enum iwl_ucode_tlv_type tlv_type;
	const u8 *tlv_data;
836

837
838
	if (len < sizeof(*ucode)) {
		IWL_ERR(priv, "uCode has invalid length: %zd\n", len);
839
		return -EINVAL;
840
	}
841

842
843
844
	if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
		IWL_ERR(priv, "invalid uCode magic: 0X%x\n",
			le32_to_cpu(ucode->magic));
845
		return -EINVAL;
846
	}
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870

	/*
	 * Check which alternatives are present, and "downgrade"
	 * when the chosen alternative is not present, warning
	 * the user when that happens. Some files may not have
	 * any alternatives, so don't warn in that case.
	 */
	alternatives = le64_to_cpu(ucode->alternatives);
	tmp = wanted_alternative;
	if (wanted_alternative > 63)
		wanted_alternative = 63;
	while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
		wanted_alternative--;
	if (wanted_alternative && wanted_alternative != tmp)
		IWL_WARN(priv,
			 "uCode alternative %d not available, choosing %d\n",
			 tmp, wanted_alternative);

	priv->ucode_ver = le32_to_cpu(ucode->ver);
	pieces->build = le32_to_cpu(ucode->build);
	data = ucode->data;

	len -= sizeof(*ucode);

871
	while (len >= sizeof(*tlv)) {
872
873
874
875
876
877
878
879
880
881
		u16 tlv_alt;

		len -= sizeof(*tlv);
		tlv = (void *)data;

		tlv_len = le32_to_cpu(tlv->length);
		tlv_type = le16_to_cpu(tlv->type);
		tlv_alt = le16_to_cpu(tlv->alternative);
		tlv_data = tlv->data;

882
883
884
		if (len < tlv_len) {
			IWL_ERR(priv, "invalid TLV len: %zd/%u\n",
				len, tlv_len);
885
			return -EINVAL;
886
		}
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
		len -= ALIGN(tlv_len, 4);
		data += sizeof(*tlv) + ALIGN(tlv_len, 4);

		/*
		 * Alternative 0 is always valid.
		 *
		 * Skip alternative TLVs that are not selected.
		 */
		if (tlv_alt != 0 && tlv_alt != wanted_alternative)
			continue;

		switch (tlv_type) {
		case IWL_UCODE_TLV_INST:
			pieces->inst = tlv_data;
			pieces->inst_size = tlv_len;
			break;
		case IWL_UCODE_TLV_DATA:
			pieces->data = tlv_data;
			pieces->data_size = tlv_len;
			break;
		case IWL_UCODE_TLV_INIT:
			pieces->init = tlv_data;
			pieces->init_size = tlv_len;
			break;
		case IWL_UCODE_TLV_INIT_DATA:
			pieces->init_data = tlv_data;
			pieces->init_data_size = tlv_len;
			break;
		case IWL_UCODE_TLV_BOOT:
Johannes Berg's avatar
Johannes Berg committed
916
			IWL_ERR(priv, "Found unexpected BOOT ucode\n");
917
918
			break;
		case IWL_UCODE_TLV_PROBE_MAX_LEN:
919
920
921
			if (tlv_len != sizeof(u32))
				goto invalid_tlv_len;
			capa->max_probe_length =
922
					le32_to_cpup((__le32 *)tlv_data);
923
			break;
Johannes Berg's avatar
Johannes Berg committed
924
925
926
		case IWL_UCODE_TLV_PAN:
			if (tlv_len)
				goto invalid_tlv_len;
Johannes Berg's avatar
Johannes Berg committed
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
			capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
			break;
		case IWL_UCODE_TLV_FLAGS:
			/* must be at least one u32 */
			if (tlv_len < sizeof(u32))
				goto invalid_tlv_len;
			/* and a proper number of u32s */
			if (tlv_len % sizeof(u32))
				goto invalid_tlv_len;
			/*
			 * This driver only reads the first u32 as
			 * right now no more features are defined,
			 * if that changes then either the driver
			 * will not work with the new firmware, or
			 * it'll not take advantage of new features.
			 */
			capa->flags = le32_to_cpup((__le32 *)tlv_data);
Johannes Berg's avatar
Johannes Berg committed
944
			break;
945
		case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
946
947
948
			if (tlv_len != sizeof(u32))
				goto invalid_tlv_len;
			pieces->init_evtlog_ptr =
949
					le32_to_cpup((__le32 *)tlv_data);
950
951
			break;
		case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
952
953
954
			if (tlv_len != sizeof(u32))
				goto invalid_tlv_len;
			pieces->init_evtlog_size =
955
					le32_to_cpup((__le32 *)tlv_data);
956
957
			break;
		case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
958
959
960
			if (tlv_len != sizeof(u32))
				goto invalid_tlv_len;
			pieces->init_errlog_ptr =
961
					le32_to_cpup((__le32 *)tlv_data);
962
963
			break;
		case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
964
965
966
			if (tlv_len != sizeof(u32))
				goto invalid_tlv_len;
			pieces->inst_evtlog_ptr =
967
					le32_to_cpup((__le32 *)tlv_data);
968
969
			break;
		case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
970
971
972
			if (tlv_len != sizeof(u32))
				goto invalid_tlv_len;
			pieces->inst_evtlog_size =
973
					le32_to_cpup((__le32 *)tlv_data);
974
975
			break;
		case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
976
977
978
			if (tlv_len != sizeof(u32))
				goto invalid_tlv_len;
			pieces->inst_errlog_ptr =
979
					le32_to_cpup((__le32 *)tlv_data);
980
			break;
981
982
		case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
			if (tlv_len)
983
984
				goto invalid_tlv_len;
			priv->enhance_sensitivity_table = true;
985
			break;
Johannes Berg's avatar
Johannes Berg committed
986
987
988
989
990
991
992
993
		case IWL_UCODE_TLV_WOWLAN_INST:
			pieces->wowlan_inst = tlv_data;
			pieces->wowlan_inst_size = tlv_len;
			break;
		case IWL_UCODE_TLV_WOWLAN_DATA:
			pieces->wowlan_data = tlv_data;
			pieces->wowlan_data_size = tlv_len;
			break;
994
		case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
995
996
997
			if (tlv_len != sizeof(u32))
				goto invalid_tlv_len;
			capa->standard_phy_calibration_size =
998
999
					le32_to_cpup((__le32 *)tlv_data);
			break;
1000
		default:
1001
			IWL_DEBUG_INFO(priv, "unknown TLV: %d\n", tlv_type);
1002
1003
1004
1005
			break;
		}
	}

1006
1007
1008
	if (len) {
		IWL_ERR(priv, "invalid TLV after parsing: %zd\n", len);
		iwl_print_hex_dump(priv, IWL_DL_FW, (u8 *)data, len);
1009
		return -EINVAL;
1010
	}
1011

1012
1013
1014
1015
1016
1017
1018
	return 0;

 invalid_tlv_len:
	IWL_ERR(priv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
	iwl_print_hex_dump(priv, IWL_DL_FW, tlv_data, tlv_len);

	return -EINVAL;
1019
1020
}

1021
/**
1022
 * iwl_ucode_callback - callback when firmware was loaded
1023
 *
1024
1025
 * If loaded successfully, copies the firmware into buffers
 * for the card to fetch (via DMA).
1026
 */
1027
static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
1028
{
1029
	struct iwl_priv *priv = context;
1030
	struct iwl_ucode_header *ucode;
1031
1032
	int err;
	struct iwlagn_firmware_pieces pieces;
1033
	const unsigned int api_max = priv->cfg->ucode_api_max;
1034
	unsigned int api_ok = priv->cfg->ucode_api_ok;
1035
	const unsigned int api_min = priv->cfg->ucode_api_min;
1036
	u32 api_ver;
1037
	char buildstr[25];
1038
	u32 build;
1039
1040
	struct iwlagn_ucode_capabilities ucode_capa = {
		.max_probe_length = 200,
1041
		.standard_phy_calibration_size =
1042
			IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE,
1043
	};
1044

1045
1046
1047
	if (!api_ok)
		api_ok = api_max;

1048
	memset(&pieces, 0, sizeof(pieces));
1049

1050
	if (!ucode_raw) {
1051
		if (priv->fw_index <= api_ok)
1052
1053
1054
			IWL_ERR(priv,
				"request for firmware file '%s' failed.\n",
				priv->firmware_name);
1055
		goto try_again;
1056
1057
	}

1058
1059
	IWL_DEBUG_INFO(priv, "Loaded firmware file '%s' (%zd bytes).\n",
		       priv->firmware_name, ucode_raw->size);
1060

1061
1062
	/* Make sure that we got at least the API version number */
	if (ucode_raw->size < 4) {
1063
		IWL_ERR(priv, "File size way too small!\n");
1064
		goto try_again;
1065
1066
1067
	}

	/* Data from ucode file:  header followed by uCode images */
1068
	ucode = (struct iwl_ucode_header *)ucode_raw->data;
1069

1070
1071
1072
	if (ucode->ver)
		err = iwlagn_load_legacy_firmware(priv, ucode_raw, &pieces);
	else
1073
1074
		err = iwlagn_load_firmware(priv, ucode_raw, &pieces,
					   &ucode_capa);
1075

1076
1077
	if (err)
		goto try_again;
1078

1079
	api_ver = IWL_UCODE_API(priv->ucode_ver);
1080
	build = pieces.build;
1081

1082
1083
1084
1085
1086
	/*
	 * api_ver should match the api version forming part of the
	 * firmware filename ... but we don't check for that and only rely
	 * on the API version read from firmware header from here on forward
	 */
1087
1088
1089
1090
1091
1092
1093
1094
1095
	/* no api version check required for experimental uCode */
	if (priv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
		if (api_ver < api_min || api_ver > api_max) {
			IWL_ERR(priv,
				"Driver unable to support your firmware API. "
				"Driver supports v%u, firmware is v%u.\n",
				api_max, api_ver);
			goto try_again;
		}
1096

1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
		if (api_ver < api_ok) {
			if (api_ok != api_max)
				IWL_ERR(priv, "Firmware has old API version, "
					"expected v%u through v%u, got v%u.\n",
					api_ok, api_max, api_ver);
			else
				IWL_ERR(priv, "Firmware has old API version, "
					"expected v%u, got v%u.\n",
					api_max, api_ver);
			IWL_ERR(priv, "New firmware can be obtained from "
				      "http://www.intellinuxwireless.org/.\n");
		}
1109
	}
1110

1111
	if (build)
1112
1113
1114
		sprintf(buildstr, " build %u%s", build,
		       (priv->fw_index == UCODE_EXPERIMENTAL_INDEX)
				? " (EXP)" : "");
1115
1116
1117
1118
1119
1120
1121
1122
1123
	else
		buildstr[0] = '\0';

	IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u%s\n",
		 IWL_UCODE_MAJOR(priv->ucode_ver),
		 IWL_UCODE_MINOR(priv->ucode_ver),
		 IWL_UCODE_API(priv->ucode_ver),
		 IWL_UCODE_SERIAL(priv->ucode_ver),
		 buildstr);
1124

1125
1126
	snprintf(priv->hw->wiphy->fw_version,
		 sizeof(priv->hw->wiphy->fw_version),
1127
		 "%u.%u.%u.%u%s",
1128
1129
1130
		 IWL_UCODE_MAJOR(priv->ucode_ver),
		 IWL_UCODE_MINOR(priv->ucode_ver),
		 IWL_UCODE_API(priv->ucode_ver),
1131
1132
		 IWL_UCODE_SERIAL(priv->ucode_ver),
		 buildstr);
1133

1134
1135
1136
1137
1138
1139
	/*
	 * For any of the failures below (before allocating pci memory)
	 * we will try to load a version with a smaller API -- maybe the
	 * user just got a corrupted version of the latest API.
	 */

1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
	IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
		       priv->ucode_ver);
	IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %Zd\n",
		       pieces.inst_size);
	IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %Zd\n",
		       pieces.data_size);
	IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %Zd\n",
		       pieces.init_size);
	IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %Zd\n",
		       pieces.init_data_size);
1150
1151

	/* Verify that uCode images will fit in card's SRAM */
1152
	if (pieces.inst_size > hw_params(priv).max_inst_size) {
1153
1154
		IWL_ERR(priv, "uCode instr len %Zd too large to fit in\n",
			pieces.inst_size);
1155
		goto try_again;
1156
1157
	}

1158
	if (pieces.data_size > hw_params(priv).max_data_size) {
1159
1160
		IWL_ERR(priv, "uCode data len %Zd too large to fit in\n",
			pieces.data_size);
1161
		goto try_again;
1162
	}
1163

1164
	if (pieces.init_size > hw_params(priv).max_inst_size) {
1165
1166
		IWL_ERR(priv, "uCode init instr len %Zd too large to fit in\n",
			pieces.init_size);
1167
		goto try_again;
1168
	}
1169

1170
	if (pieces.init_data_size > hw_params(priv).max_data_size) {
1171
1172
		IWL_ERR(priv, "uCode init data len %Zd too large to fit in\n",
			pieces.init_data_size);
1173
		goto try_again;
1174
	}
1175

1176
1177
1178
1179
1180
	/* Allocate ucode buffers for card's bus-master loading ... */

	/* Runtime instructions and 2 copies of data:
	 * 1) unmodified from disk
	 * 2) backup cache for save/restore during power-downs */
1181
	if (iwl_alloc_fw_desc(priv, &priv->ucode_rt.code,
1182
1183
			      pieces.inst, pieces.inst_size))
		goto err_pci_alloc;
1184
	if (iwl_alloc_fw_desc(priv, &priv->ucode_rt.data,
1185
			      pieces.data, pieces.data_size))
1186
1187
		goto err_pci_alloc;

1188
	/* Initialization instructions and data */
1189
	if (pieces.init_size && pieces.init_data_size) {
1190
		if (iwl_alloc_fw_desc(priv, &priv->ucode_init.code,
1191
1192
				      pieces.init, pieces.init_size))
			goto err_pci_alloc;
1193
		if (iwl_alloc_fw_desc(priv, &priv->ucode_init.data,
1194
				      pieces.init_data, pieces.init_data_size))
1195
1196
			goto err_pci_alloc;
	}
Zhu Yi's avatar