iwl-3945.c 79.9 KB
Newer Older
1
2
/******************************************************************************
 *
3
 * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * 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:
22
 *  Intel Linux Wireless <ilw@linux.intel.com>
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 *****************************************************************************/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/dma-mapping.h>
#include <linux/delay.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/wireless.h>
#include <linux/firmware.h>
#include <linux/etherdevice.h>
Zhu Yi's avatar
Zhu Yi committed
38
39
#include <asm/unaligned.h>
#include <net/mac80211.h>
40

Winkler, Tomas's avatar
Winkler, Tomas committed
41
#include "iwl-fh.h"
42
#include "iwl-3945-fh.h"
43
#include "iwl-commands.h"
44
#include "iwl-sta.h"
45
#include "iwl-3945.h"
46
#include "iwl-eeprom.h"
47
#include "iwl-helpers.h"
48
#include "iwl-core.h"
49
#include "iwl-agn-rs.h"
50
51
52
53
54
55
56
57
58

#define IWL_DECLARE_RATE_INFO(r, ip, in, rp, rn, pp, np)    \
	[IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP,   \
				    IWL_RATE_##r##M_IEEE,   \
				    IWL_RATE_##ip##M_INDEX, \
				    IWL_RATE_##in##M_INDEX, \
				    IWL_RATE_##rp##M_INDEX, \
				    IWL_RATE_##rn##M_INDEX, \
				    IWL_RATE_##pp##M_INDEX, \
59
60
61
				    IWL_RATE_##np##M_INDEX, \
				    IWL_RATE_##r##M_INDEX_TABLE, \
				    IWL_RATE_##ip##M_INDEX_TABLE }
62
63
64
65
66
67
68
69
70

/*
 * Parameter order:
 *   rate, prev rate, next rate, prev tgg rate, next tgg rate
 *
 * If there isn't a valid next or previous rate then INV is used which
 * maps to IWL_RATE_INVALID
 *
 */
71
const struct iwl3945_rate_info iwl3945_rates[IWL_RATE_COUNT_3945] = {
72
73
74
75
	IWL_DECLARE_RATE_INFO(1, INV, 2, INV, 2, INV, 2),    /*  1mbps */
	IWL_DECLARE_RATE_INFO(2, 1, 5, 1, 5, 1, 5),          /*  2mbps */
	IWL_DECLARE_RATE_INFO(5, 2, 6, 2, 11, 2, 11),        /*5.5mbps */
	IWL_DECLARE_RATE_INFO(11, 9, 12, 5, 12, 5, 18),      /* 11mbps */
76
77
78
79
80
81
82
83
84
85
	IWL_DECLARE_RATE_INFO(6, 5, 9, 5, 11, 5, 11),        /*  6mbps */
	IWL_DECLARE_RATE_INFO(9, 6, 11, 5, 11, 5, 11),       /*  9mbps */
	IWL_DECLARE_RATE_INFO(12, 11, 18, 11, 18, 11, 18),   /* 12mbps */
	IWL_DECLARE_RATE_INFO(18, 12, 24, 12, 24, 11, 24),   /* 18mbps */
	IWL_DECLARE_RATE_INFO(24, 18, 36, 18, 36, 18, 36),   /* 24mbps */
	IWL_DECLARE_RATE_INFO(36, 24, 48, 24, 48, 24, 48),   /* 36mbps */
	IWL_DECLARE_RATE_INFO(48, 36, 54, 36, 54, 36, 54),   /* 48mbps */
	IWL_DECLARE_RATE_INFO(54, 48, INV, 48, INV, 48, INV),/* 54mbps */
};

Christoph Hellwig's avatar
Christoph Hellwig committed
86
/* 1 = enable the iwl3945_disable_events() function */
87
88
89
90
#define IWL_EVT_DISABLE (0)
#define IWL_EVT_DISABLE_SIZE (1532/32)

/**
Christoph Hellwig's avatar
Christoph Hellwig committed
91
 * iwl3945_disable_events - Disable selected events in uCode event log
92
93
94
95
96
97
98
 *
 * Disable an event by writing "1"s into "disable"
 *   bitmap in SRAM.  Bit position corresponds to Event # (id/type).
 *   Default values of 0 enable uCode events to be logged.
 * Use for only special debugging.  This function is just a placeholder as-is,
 *   you'll need to provide the special bits! ...
 *   ... and set IWL_EVT_DISABLE to 1. */
99
void iwl3945_disable_events(struct iwl_priv *priv)
100
{
101
	int ret;
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	int i;
	u32 base;		/* SRAM address of event log header */
	u32 disable_ptr;	/* SRAM address of event-disable bitmap array */
	u32 array_size;		/* # of u32 entries in array */
	u32 evt_disable[IWL_EVT_DISABLE_SIZE] = {
		0x00000000,	/*   31 -    0  Event id numbers */
		0x00000000,	/*   63 -   32 */
		0x00000000,	/*   95 -   64 */
		0x00000000,	/*  127 -   96 */
		0x00000000,	/*  159 -  128 */
		0x00000000,	/*  191 -  160 */
		0x00000000,	/*  223 -  192 */
		0x00000000,	/*  255 -  224 */
		0x00000000,	/*  287 -  256 */
		0x00000000,	/*  319 -  288 */
		0x00000000,	/*  351 -  320 */
		0x00000000,	/*  383 -  352 */
		0x00000000,	/*  415 -  384 */
		0x00000000,	/*  447 -  416 */
		0x00000000,	/*  479 -  448 */
		0x00000000,	/*  511 -  480 */
		0x00000000,	/*  543 -  512 */
		0x00000000,	/*  575 -  544 */
		0x00000000,	/*  607 -  576 */
		0x00000000,	/*  639 -  608 */
		0x00000000,	/*  671 -  640 */
		0x00000000,	/*  703 -  672 */
		0x00000000,	/*  735 -  704 */
		0x00000000,	/*  767 -  736 */
		0x00000000,	/*  799 -  768 */
		0x00000000,	/*  831 -  800 */
		0x00000000,	/*  863 -  832 */
		0x00000000,	/*  895 -  864 */
		0x00000000,	/*  927 -  896 */
		0x00000000,	/*  959 -  928 */
		0x00000000,	/*  991 -  960 */
		0x00000000,	/* 1023 -  992 */
		0x00000000,	/* 1055 - 1024 */
		0x00000000,	/* 1087 - 1056 */
		0x00000000,	/* 1119 - 1088 */
		0x00000000,	/* 1151 - 1120 */
		0x00000000,	/* 1183 - 1152 */
		0x00000000,	/* 1215 - 1184 */
		0x00000000,	/* 1247 - 1216 */
		0x00000000,	/* 1279 - 1248 */
		0x00000000,	/* 1311 - 1280 */
		0x00000000,	/* 1343 - 1312 */
		0x00000000,	/* 1375 - 1344 */
		0x00000000,	/* 1407 - 1376 */
		0x00000000,	/* 1439 - 1408 */
		0x00000000,	/* 1471 - 1440 */
		0x00000000,	/* 1503 - 1472 */
	};

	base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwig's avatar
Christoph Hellwig committed
157
	if (!iwl3945_hw_valid_rtc_data_addr(base)) {
158
		IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
159
160
161
		return;
	}

162
	ret = iwl_grab_nic_access(priv);
163
	if (ret) {
164
		IWL_WARN(priv, "Can not read from adapter at this time.\n");
165
166
167
		return;
	}

168
169
170
	disable_ptr = iwl_read_targ_mem(priv, base + (4 * sizeof(u32)));
	array_size = iwl_read_targ_mem(priv, base + (5 * sizeof(u32)));
	iwl_release_nic_access(priv);
171
172

	if (IWL_EVT_DISABLE && (array_size == IWL_EVT_DISABLE_SIZE)) {
173
		IWL_DEBUG_INFO(priv, "Disabling selected uCode log events at 0x%x\n",
174
			       disable_ptr);
175
		ret = iwl_grab_nic_access(priv);
176
		for (i = 0; i < IWL_EVT_DISABLE_SIZE; i++)
177
			iwl_write_targ_mem(priv,
178
179
					   disable_ptr + (i * sizeof(u32)),
					   evt_disable[i]);
180

181
		iwl_release_nic_access(priv);
182
	} else {
183
184
185
		IWL_DEBUG_INFO(priv, "Selected uCode log events may be disabled\n");
		IWL_DEBUG_INFO(priv, "  by writing \"1\"s into disable bitmap\n");
		IWL_DEBUG_INFO(priv, "  in SRAM at 0x%x, size %d u32s\n",
186
187
188
189
190
			       disable_ptr, array_size);
	}

}

191
192
193
194
195
196
197
198
199
200
static int iwl3945_hwrate_to_plcp_idx(u8 plcp)
{
	int idx;

	for (idx = 0; idx < IWL_RATE_COUNT; idx++)
		if (iwl3945_rates[idx].plcp == plcp)
			return idx;
	return -1;
}

Samuel Ortiz's avatar
Samuel Ortiz committed
201
#ifdef CONFIG_IWLWIFI_DEBUG
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x

static const char *iwl3945_get_tx_fail_reason(u32 status)
{
	switch (status & TX_STATUS_MSK) {
	case TX_STATUS_SUCCESS:
		return "SUCCESS";
		TX_STATUS_ENTRY(SHORT_LIMIT);
		TX_STATUS_ENTRY(LONG_LIMIT);
		TX_STATUS_ENTRY(FIFO_UNDERRUN);
		TX_STATUS_ENTRY(MGMNT_ABORT);
		TX_STATUS_ENTRY(NEXT_FRAG);
		TX_STATUS_ENTRY(LIFE_EXPIRE);
		TX_STATUS_ENTRY(DEST_PS);
		TX_STATUS_ENTRY(ABORTED);
		TX_STATUS_ENTRY(BT_RETRY);
		TX_STATUS_ENTRY(STA_INVALID);
		TX_STATUS_ENTRY(FRAG_DROPPED);
		TX_STATUS_ENTRY(TID_DISABLE);
		TX_STATUS_ENTRY(FRAME_FLUSHED);
		TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
		TX_STATUS_ENTRY(TX_LOCKED);
		TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
	}

	return "UNKNOWN";
}
#else
static inline const char *iwl3945_get_tx_fail_reason(u32 status)
{
	return "";
}
#endif

236
237
238
239
240
/*
 * get ieee prev rate from rate scale table.
 * for A and B mode we need to overright prev
 * value
 */
241
int iwl3945_rs_next_rate(struct iwl_priv *priv, int rate)
242
243
244
245
246
247
248
249
250
251
{
	int next_rate = iwl3945_get_prev_ieee_rate(rate);

	switch (priv->band) {
	case IEEE80211_BAND_5GHZ:
		if (rate == IWL_RATE_12M_INDEX)
			next_rate = IWL_RATE_9M_INDEX;
		else if (rate == IWL_RATE_6M_INDEX)
			next_rate = IWL_RATE_6M_INDEX;
		break;
252
253
	case IEEE80211_BAND_2GHZ:
		if (!(priv->sta_supp_rates & IWL_OFDM_RATES_MASK) &&
254
		    iwl_is_associated(priv)) {
255
256
257
			if (rate == IWL_RATE_11M_INDEX)
				next_rate = IWL_RATE_5M_INDEX;
		}
258
		break;
259

260
261
262
263
264
265
266
	default:
		break;
	}

	return next_rate;
}

267
268
269
270
271
272
273
274

/**
 * iwl3945_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
 *
 * When FW advances 'R' index, all entries between old and new 'R' index
 * need to be reclaimed. As result, some free space forms. If there is
 * enough free space (> low mark), wake the stack that feeds us.
 */
275
static void iwl3945_tx_queue_reclaim(struct iwl_priv *priv,
276
277
				     int txq_id, int index)
{
278
	struct iwl_tx_queue *txq = &priv->txq[txq_id];
279
	struct iwl_queue *q = &txq->q;
Winkler, Tomas's avatar
Winkler, Tomas committed
280
	struct iwl_tx_info *tx_info;
281
282
283
284
285
286
287

	BUG_ON(txq_id == IWL_CMD_QUEUE_NUM);

	for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
		q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {

		tx_info = &txq->txb[txq->q.read_ptr];
288
		ieee80211_tx_status_irqsafe(priv->hw, tx_info->skb[0]);
289
		tx_info->skb[0] = NULL;
290
		priv->cfg->ops->lib->txq_free_tfd(priv, txq);
291
292
	}

293
	if (iwl_queue_space(q) > q->low_mark && (txq_id >= 0) &&
294
295
296
297
298
299
300
301
			(txq_id != IWL_CMD_QUEUE_NUM) &&
			priv->mac80211_registered)
		ieee80211_wake_queue(priv->hw, txq_id);
}

/**
 * iwl3945_rx_reply_tx - Handle Tx response
 */
302
static void iwl3945_rx_reply_tx(struct iwl_priv *priv,
303
			    struct iwl_rx_mem_buffer *rxb)
304
{
305
	struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
306
307
308
	u16 sequence = le16_to_cpu(pkt->hdr.sequence);
	int txq_id = SEQ_TO_QUEUE(sequence);
	int index = SEQ_TO_INDEX(sequence);
309
	struct iwl_tx_queue *txq = &priv->txq[txq_id];
310
	struct ieee80211_tx_info *info;
311
312
313
	struct iwl3945_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
	u32  status = le32_to_cpu(tx_resp->status);
	int rate_idx;
314
	int fail;
315

316
	if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
317
		IWL_ERR(priv, "Read index for DMA queue txq_id (%d) index %d "
318
319
320
321
322
323
			  "is out of range [0-%d] %d %d\n", txq_id,
			  index, txq->q.n_bd, txq->q.write_ptr,
			  txq->q.read_ptr);
		return;
	}

324
	info = IEEE80211_SKB_CB(txq->txb[txq->q.read_ptr].skb[0]);
325
326
327
328
329
330
331
332
	ieee80211_tx_info_clear_status(info);

	/* Fill the MRR chain with some info about on-chip retransmissions */
	rate_idx = iwl3945_hwrate_to_plcp_idx(tx_resp->rate);
	if (info->band == IEEE80211_BAND_5GHZ)
		rate_idx -= IWL_FIRST_OFDM_RATE;

	fail = tx_resp->failure_frame;
333
334
335

	info->status.rates[0].idx = rate_idx;
	info->status.rates[0].count = fail + 1; /* add final attempt */
336
337

	/* tx_status->rts_retry_count = tx_resp->failure_rts; */
338
339
	info->flags |= ((status & TX_STATUS_MSK) == TX_STATUS_SUCCESS) ?
				IEEE80211_TX_STAT_ACK : 0;
340

341
	IWL_DEBUG_TX(priv, "Tx queue %d Status %s (0x%08x) plcp rate %d retries %d\n",
342
343
344
			txq_id, iwl3945_get_tx_fail_reason(status), status,
			tx_resp->rate, tx_resp->failure_frame);

345
	IWL_DEBUG_TX_REPLY(priv, "Tx queue reclaim %d\n", index);
346
347
348
	iwl3945_tx_queue_reclaim(priv, txq_id, index);

	if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
349
		IWL_ERR(priv, "TODO:  Implement Tx ABORT REQUIRED!!!\n");
350
351
352
353
}



354
355
356
357
358
359
360
361
/*****************************************************************************
 *
 * Intel PRO/Wireless 3945ABG/BG Network Connection
 *
 *  RX handler implementations
 *
 *****************************************************************************/

362
void iwl3945_hw_rx_statistics(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
363
{
364
	struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
365
	IWL_DEBUG_RX(priv, "Statistics notification received (%d vs %d).\n",
Christoph Hellwig's avatar
Christoph Hellwig committed
366
		     (int)sizeof(struct iwl3945_notif_statistics),
367
368
		     le32_to_cpu(pkt->len));

369
	memcpy(&priv->statistics_39, pkt->u.raw, sizeof(priv->statistics_39));
370

Mohamed Abbas's avatar
Mohamed Abbas committed
371
372
	iwl3945_led_background(priv);

373
374
375
	priv->last_statistics_time = jiffies;
}

376
377
378
379
380
/******************************************************************************
 *
 * Misc. internal state and helper functions
 *
 ******************************************************************************/
Samuel Ortiz's avatar
Samuel Ortiz committed
381
#ifdef CONFIG_IWLWIFI_DEBUG
382
383
384
385
386
387
388
389

/**
 * iwl3945_report_frame - dump frame to syslog during debug sessions
 *
 * You may hack this function to show different aspects of received frames,
 * including selective frame dumps.
 * group100 parameter selects whether to show 1 out of 100 good frames.
 */
Samuel Ortiz's avatar
Samuel Ortiz committed
390
static void _iwl3945_dbg_report_frame(struct iwl_priv *priv,
391
		      struct iwl_rx_packet *pkt,
392
393
394
395
396
397
398
		      struct ieee80211_hdr *header, int group100)
{
	u32 to_us;
	u32 print_summary = 0;
	u32 print_dump = 0;	/* set to 1 to dump all frames' contents */
	u32 hundred = 0;
	u32 dataframe = 0;
399
	__le16 fc;
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
	u16 seq_ctl;
	u16 channel;
	u16 phy_flags;
	u16 length;
	u16 status;
	u16 bcn_tmr;
	u32 tsf_low;
	u64 tsf;
	u8 rssi;
	u8 agc;
	u16 sig_avg;
	u16 noise_diff;
	struct iwl3945_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
	struct iwl3945_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
	struct iwl3945_rx_frame_end *rx_end = IWL_RX_END(pkt);
	u8 *data = IWL_RX_DATA(pkt);

	/* MAC header */
418
	fc = header->frame_control;
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
	seq_ctl = le16_to_cpu(header->seq_ctrl);

	/* metadata */
	channel = le16_to_cpu(rx_hdr->channel);
	phy_flags = le16_to_cpu(rx_hdr->phy_flags);
	length = le16_to_cpu(rx_hdr->len);

	/* end-of-frame status and timestamp */
	status = le32_to_cpu(rx_end->status);
	bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
	tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
	tsf = le64_to_cpu(rx_end->timestamp);

	/* signal statistics */
	rssi = rx_stats->rssi;
	agc = rx_stats->agc;
	sig_avg = le16_to_cpu(rx_stats->sig_avg);
	noise_diff = le16_to_cpu(rx_stats->noise_diff);

	to_us = !compare_ether_addr(header->addr1, priv->mac_addr);

	/* if data frame is to us and all is good,
	 *   (optionally) print summary for only 1 out of every 100 */
442
443
	if (to_us && (fc & ~cpu_to_le16(IEEE80211_FCTL_PROTECTED)) ==
	    cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
		dataframe = 1;
		if (!group100)
			print_summary = 1;	/* print each frame */
		else if (priv->framecnt_to_us < 100) {
			priv->framecnt_to_us++;
			print_summary = 0;
		} else {
			priv->framecnt_to_us = 0;
			print_summary = 1;
			hundred = 1;
		}
	} else {
		/* print summary for all other frames */
		print_summary = 1;
	}

	if (print_summary) {
		char *title;
462
		int rate;
463
464
465

		if (hundred)
			title = "100Frames";
466
		else if (ieee80211_has_retry(fc))
467
			title = "Retry";
468
		else if (ieee80211_is_assoc_resp(fc))
469
			title = "AscRsp";
470
		else if (ieee80211_is_reassoc_resp(fc))
471
			title = "RasRsp";
472
		else if (ieee80211_is_probe_resp(fc)) {
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
			title = "PrbRsp";
			print_dump = 1;	/* dump frame contents */
		} else if (ieee80211_is_beacon(fc)) {
			title = "Beacon";
			print_dump = 1;	/* dump frame contents */
		} else if (ieee80211_is_atim(fc))
			title = "ATIM";
		else if (ieee80211_is_auth(fc))
			title = "Auth";
		else if (ieee80211_is_deauth(fc))
			title = "DeAuth";
		else if (ieee80211_is_disassoc(fc))
			title = "DisAssoc";
		else
			title = "Frame";

		rate = iwl3945_hwrate_to_plcp_idx(rx_hdr->rate);
		if (rate == -1)
			rate = 0;
		else
			rate = iwl3945_rates[rate].ieee / 2;

		/* print frame summary.
		 * MAC addresses show just the last byte (for brevity),
		 *    but you can hack it to show more, if you'd like to. */
		if (dataframe)
499
			IWL_DEBUG_RX(priv, "%s: mhd=0x%04x, dst=0x%02x, "
500
				     "len=%u, rssi=%d, chnl=%d, rate=%d, \n",
501
				     title, le16_to_cpu(fc), header->addr1[5],
502
503
504
				     length, rssi, channel, rate);
		else {
			/* src/dst addresses assume managed mode */
505
			IWL_DEBUG_RX(priv, "%s: 0x%04x, dst=0x%02x, "
506
507
				     "src=0x%02x, rssi=%u, tim=%lu usec, "
				     "phy=0x%02x, chnl=%d\n",
508
				     title, le16_to_cpu(fc), header->addr1[5],
509
510
511
512
513
514
				     header->addr3[5], rssi,
				     tsf_low - priv->scan_start_tsf,
				     phy_flags, channel);
		}
	}
	if (print_dump)
515
		iwl_print_hex_dump(priv, IWL_DL_RX, data, length);
516
}
Samuel Ortiz's avatar
Samuel Ortiz committed
517
518
519
520
521
522
523
524
525

static void iwl3945_dbg_report_frame(struct iwl_priv *priv,
		      struct iwl_rx_packet *pkt,
		      struct ieee80211_hdr *header, int group100)
{
	if (priv->debug_level & IWL_DL_RX)
		_iwl3945_dbg_report_frame(priv, pkt, header, group100);
}

526
#else
527
static inline void iwl3945_dbg_report_frame(struct iwl_priv *priv,
528
		      struct iwl_rx_packet *pkt,
529
530
531
532
533
		      struct ieee80211_hdr *header, int group100)
{
}
#endif

534
/* This is necessary only for a number of statistics, see the caller. */
535
static int iwl3945_is_network_packet(struct iwl_priv *priv,
536
537
538
539
540
		struct ieee80211_hdr *header)
{
	/* Filter incoming packets to determine if they are targeted toward
	 * this network, discarding packets coming from ourselves */
	switch (priv->iw_mode) {
541
	case NL80211_IFTYPE_ADHOC: /* Header: Dest. | Source    | BSSID */
542
543
		/* packets to our IBSS update information */
		return !compare_ether_addr(header->addr3, priv->bssid);
544
	case NL80211_IFTYPE_STATION: /* Header: Dest. | AP{BSSID} | Source */
545
546
547
548
549
550
		/* packets to our IBSS update information */
		return !compare_ether_addr(header->addr2, priv->bssid);
	default:
		return 1;
	}
}
551

552
static void iwl3945_pass_packet_to_mac80211(struct iwl_priv *priv,
553
				   struct iwl_rx_mem_buffer *rxb,
Zhu Yi's avatar
Zhu Yi committed
554
				   struct ieee80211_rx_status *stats)
555
{
556
	struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
557
#ifdef CONFIG_IWLWIFI_LEDS
558
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)IWL_RX_DATA(pkt);
559
#endif
Christoph Hellwig's avatar
Christoph Hellwig committed
560
561
	struct iwl3945_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
	struct iwl3945_rx_frame_end *rx_end = IWL_RX_END(pkt);
562
563
564
	short len = le16_to_cpu(rx_hdr->len);

	/* We received data from the HW, so stop the watchdog */
565
	if (unlikely((len + IWL39_RX_FRAME_SIZE) > skb_tailroom(rxb->skb))) {
566
		IWL_DEBUG_DROP(priv, "Corruption detected!\n");
567
568
569
570
571
		return;
	}

	/* We only process data packets if the interface is open */
	if (unlikely(!priv->is_open)) {
572
573
		IWL_DEBUG_DROP_LIMIT(priv,
			"Dropping packet while interface is not open.\n");
574
575
576
577
578
579
580
		return;
	}

	skb_reserve(rxb->skb, (void *)rx_hdr->payload - (void *)pkt);
	/* Set the size of the skb to the size of the frame */
	skb_put(rxb->skb, le16_to_cpu(rx_hdr->len));

581
	if (!iwl3945_mod_params.sw_crypto)
582
583
		iwl_set_decrypted_flag(priv,
				       (struct ieee80211_hdr *)rxb->skb->data,
584
585
				       le32_to_cpu(rx_end->status), stats);

586
#ifdef CONFIG_IWLWIFI_LEDS
587
	if (ieee80211_is_data(hdr->frame_control))
Mohamed Abbas's avatar
Mohamed Abbas committed
588
589
		priv->rxtxpackets += len;
#endif
590
591
592
593
	ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
	rxb->skb = NULL;
}

594
595
#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)

596
static void iwl3945_rx_reply_rx(struct iwl_priv *priv,
597
				struct iwl_rx_mem_buffer *rxb)
598
{
599
600
	struct ieee80211_hdr *header;
	struct ieee80211_rx_status rx_status;
601
	struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwig's avatar
Christoph Hellwig committed
602
603
604
	struct iwl3945_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
	struct iwl3945_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
	struct iwl3945_rx_frame_end *rx_end = IWL_RX_END(pkt);
605
	int snr;
606
607
608
	u16 rx_stats_sig_avg = le16_to_cpu(rx_stats->sig_avg);
	u16 rx_stats_noise_diff = le16_to_cpu(rx_stats->noise_diff);
	u8 network_packet;
609
610
611

	rx_status.flag = 0;
	rx_status.mactime = le64_to_cpu(rx_end->timestamp);
612
	rx_status.freq =
613
		ieee80211_channel_to_frequency(le16_to_cpu(rx_hdr->channel));
614
615
616
617
618
619
	rx_status.band = (rx_hdr->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
				IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;

	rx_status.rate_idx = iwl3945_hwrate_to_plcp_idx(rx_hdr->rate);
	if (rx_status.band == IEEE80211_BAND_5GHZ)
		rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
620

621
622
623
624
625
626
627
	rx_status.antenna = le16_to_cpu(rx_hdr->phy_flags &
					RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;

	/* set the preamble flag if appropriate */
	if (rx_hdr->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
		rx_status.flag |= RX_FLAG_SHORTPRE;

628
	if ((unlikely(rx_stats->phy_count > 20))) {
629
630
		IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
				rx_stats->phy_count);
631
632
633
634
635
		return;
	}

	if (!(rx_end->status & RX_RES_STATUS_NO_CRC32_ERROR)
	    || !(rx_end->status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
636
		IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n", rx_end->status);
637
638
639
		return;
	}

640

641
642

	/* Convert 3945's rssi indicator to dBm */
643
	rx_status.signal = rx_stats->rssi - IWL39_RSSI_OFFSET;
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658

	/* Set default noise value to -127 */
	if (priv->last_rx_noise == 0)
		priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;

	/* 3945 provides noise info for OFDM frames only.
	 * sig_avg and noise_diff are measured by the 3945's digital signal
	 *   processor (DSP), and indicate linear levels of signal level and
	 *   distortion/noise within the packet preamble after
	 *   automatic gain control (AGC).  sig_avg should stay fairly
	 *   constant if the radio's AGC is working well.
	 * Since these values are linear (not dB or dBm), linear
	 *   signal-to-noise ratio (SNR) is (sig_avg / noise_diff).
	 * Convert linear SNR to dB SNR, then subtract that from rssi dBm
	 *   to obtain noise level in dBm.
659
	 * Calculate rx_status.signal (quality indicator in %) based on SNR. */
660
661
	if (rx_stats_noise_diff) {
		snr = rx_stats_sig_avg / rx_stats_noise_diff;
662
		rx_status.noise = rx_status.signal -
663
					iwl3945_calc_db_from_ratio(snr);
664
		rx_status.qual = iwl3945_calc_sig_qual(rx_status.signal,
665
							 rx_status.noise);
666
667
668
669

	/* If noise info not available, calculate signal quality indicator (%)
	 *   using just the dBm signal level. */
	} else {
670
		rx_status.noise = priv->last_rx_noise;
671
		rx_status.qual = iwl3945_calc_sig_qual(rx_status.signal, 0);
672
673
674
	}


675
	IWL_DEBUG_STATS(priv, "Rssi %d noise %d qual %d sig_avg %d noise_diff %d\n",
676
			rx_status.signal, rx_status.noise, rx_status.qual,
677
678
679
680
			rx_stats_sig_avg, rx_stats_noise_diff);

	header = (struct ieee80211_hdr *)IWL_RX_DATA(pkt);

Christoph Hellwig's avatar
Christoph Hellwig committed
681
	network_packet = iwl3945_is_network_packet(priv, header);
682

683
	IWL_DEBUG_STATS_LIMIT(priv, "[%c] %d RSSI:%d Signal:%u, Noise:%u, Rate:%u\n",
684
685
			      network_packet ? '*' : ' ',
			      le16_to_cpu(rx_hdr->channel),
686
687
			      rx_status.signal, rx_status.signal,
			      rx_status.noise, rx_status.rate_idx);
688

Samuel Ortiz's avatar
Samuel Ortiz committed
689
690
	/* Set "1" to report good data frames in groups of 100 */
	iwl3945_dbg_report_frame(priv, pkt, header, 1);
691
692
693
694

	if (network_packet) {
		priv->last_beacon_time = le32_to_cpu(rx_end->beacon_timestamp);
		priv->last_tsf = le64_to_cpu(rx_end->timestamp);
695
		priv->last_rx_rssi = rx_status.signal;
696
		priv->last_rx_noise = rx_status.noise;
697
698
	}

Abhijeet Kolekar's avatar
Abhijeet Kolekar committed
699
	iwl3945_pass_packet_to_mac80211(priv, rxb, &rx_status);
700
701
}

702
703
704
int iwl3945_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv,
				     struct iwl_tx_queue *txq,
				     dma_addr_t addr, u16 len, u8 reset, u8 pad)
705
706
{
	int count;
707
	struct iwl_queue *q;
708
	struct iwl3945_tfd *tfd, *tfd_tmp;
709
710

	q = &txq->q;
711
712
	tfd_tmp = (struct iwl3945_tfd *)txq->tfds;
	tfd = &tfd_tmp[q->write_ptr];
713
714
715

	if (reset)
		memset(tfd, 0, sizeof(*tfd));
716
717
718
719

	count = TFD_CTL_COUNT_GET(le32_to_cpu(tfd->control_flags));

	if ((count >= NUM_TFD_CHUNKS) || (count < 0)) {
720
		IWL_ERR(priv, "Error can not send more than %d chunks\n",
721
722
723
724
			  NUM_TFD_CHUNKS);
		return -EINVAL;
	}

Winkler, Tomas's avatar
Winkler, Tomas committed
725
726
	tfd->tbs[count].addr = cpu_to_le32(addr);
	tfd->tbs[count].len = cpu_to_le32(len);
727
728
729
730
731
732
733
734
735
736

	count++;

	tfd->control_flags = cpu_to_le32(TFD_CTL_COUNT_SET(count) |
					 TFD_CTL_PAD_SET(pad));

	return 0;
}

/**
Christoph Hellwig's avatar
Christoph Hellwig committed
737
 * iwl3945_hw_txq_free_tfd - Free one TFD, those at index [txq->q.read_ptr]
738
739
740
 *
 * Does NOT advance any indexes
 */
741
void iwl3945_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
742
{
743
	struct iwl3945_tfd *tfd_tmp = (struct iwl3945_tfd *)txq->tfds;
744
745
	int index = txq->q.read_ptr;
	struct iwl3945_tfd *tfd = &tfd_tmp[index];
746
747
748
749
750
	struct pci_dev *dev = priv->pci_dev;
	int i;
	int counter;

	/* sanity check */
Winkler, Tomas's avatar
Winkler, Tomas committed
751
	counter = TFD_CTL_COUNT_GET(le32_to_cpu(tfd->control_flags));
752
	if (counter > NUM_TFD_CHUNKS) {
753
		IWL_ERR(priv, "Too many chunks: %i\n", counter);
754
		/* @todo issue fatal error, it is quite serious situation */
755
		return;
756
757
	}

758
759
760
761
762
763
764
	/* Unmap tx_cmd */
	if (counter)
		pci_unmap_single(dev,
				pci_unmap_addr(&txq->cmd[index]->meta, mapping),
				pci_unmap_len(&txq->cmd[index]->meta, len),
				PCI_DMA_TODEVICE);

765
766
767
	/* unmap chunks if any */

	for (i = 1; i < counter; i++) {
Winkler, Tomas's avatar
Winkler, Tomas committed
768
769
		pci_unmap_single(dev, le32_to_cpu(tfd->tbs[i].addr),
			 le32_to_cpu(tfd->tbs[i].len), PCI_DMA_TODEVICE);
770
771
772
		if (txq->txb[txq->q.read_ptr].skb[0]) {
			struct sk_buff *skb = txq->txb[txq->q.read_ptr].skb[0];
			if (txq->txb[txq->q.read_ptr].skb[0]) {
773
774
				/* Can be called from interrupt context */
				dev_kfree_skb_any(skb);
775
				txq->txb[txq->q.read_ptr].skb[0] = NULL;
776
777
778
			}
		}
	}
779
	return ;
780
781
}

782
u8 iwl3945_hw_find_station(struct iwl_priv *priv, const u8 *addr)
783
{
784
	int i, start = IWL_AP_ID;
785
786
787
	int ret = IWL_INVALID_STATION;
	unsigned long flags;

788
789
790
791
792
	if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) ||
	    (priv->iw_mode == NL80211_IFTYPE_AP))
		start = IWL_STA_ID;

	if (is_broadcast_ether_addr(addr))
793
		return priv->hw_params.bcast_sta_id;
794

795
	spin_lock_irqsave(&priv->sta_lock, flags);
796
	for (i = start; i < priv->hw_params.max_stations; i++)
797
		if ((priv->stations_39[i].used) &&
798
		    (!compare_ether_addr
799
		     (priv->stations_39[i].sta.sta.addr, addr))) {
800
801
802
803
			ret = i;
			goto out;
		}

804
	IWL_DEBUG_INFO(priv, "can not find STA %pM (total %d)\n",
Johannes Berg's avatar
Johannes Berg committed
805
		       addr, priv->num_stations);
806
807
808
809
810
811
 out:
	spin_unlock_irqrestore(&priv->sta_lock, flags);
	return ret;
}

/**
Christoph Hellwig's avatar
Christoph Hellwig committed
812
 * iwl3945_hw_build_tx_cmd_rate - Add rate portion to TX_CMD:
813
814
 *
*/
815
void iwl3945_hw_build_tx_cmd_rate(struct iwl_priv *priv, struct iwl_cmd *cmd,
816
			      struct ieee80211_tx_info *info,
817
818
			      struct ieee80211_hdr *hdr, int sta_id, int tx_id)
{
819
	u16 hw_value = ieee80211_get_tx_rate(priv->hw, info)->hw_value;
820
	u16 rate_index = min(hw_value & 0xffff, IWL_RATE_COUNT - 1);
821
822
823
824
825
	u16 rate_mask;
	int rate;
	u8 rts_retry_limit;
	u8 data_retry_limit;
	__le32 tx_flags;
826
	__le16 fc = hdr->frame_control;
827
	struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
828

Christoph Hellwig's avatar
Christoph Hellwig committed
829
	rate = iwl3945_rates[rate_index].plcp;
830
	tx_flags = tx->tx_flags;
831
832

	/* We need to figure out how to get the sta->supp_rates while
833
	 * in this running context */
834
835
836
837
838
839
840
	rate_mask = IWL_RATES_MASK;

	if (tx_id >= IWL_CMD_QUEUE_NUM)
		rts_retry_limit = 3;
	else
		rts_retry_limit = 7;

841
	if (ieee80211_is_probe_resp(fc)) {
842
843
844
845
846
847
848
849
850
		data_retry_limit = 3;
		if (data_retry_limit < rts_retry_limit)
			rts_retry_limit = data_retry_limit;
	} else
		data_retry_limit = IWL_DEFAULT_TX_RETRY;

	if (priv->data_retry_limit != -1)
		data_retry_limit = priv->data_retry_limit;

851
852
853
854
855
856
	if (ieee80211_is_mgmt(fc)) {
		switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
		case cpu_to_le16(IEEE80211_STYPE_AUTH):
		case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
		case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
		case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
857
858
859
860
861
862
863
864
865
866
			if (tx_flags & TX_CMD_FLG_RTS_MSK) {
				tx_flags &= ~TX_CMD_FLG_RTS_MSK;
				tx_flags |= TX_CMD_FLG_CTS_MSK;
			}
			break;
		default:
			break;
		}
	}

867
868
869
870
	tx->rts_retry_limit = rts_retry_limit;
	tx->data_retry_limit = data_retry_limit;
	tx->rate = rate;
	tx->tx_flags = tx_flags;
871
872

	/* OFDM */
873
	tx->supp_rates[0] =
874
	   ((rate_mask & IWL_OFDM_RATES_MASK) >> IWL_FIRST_OFDM_RATE) & 0xFF;
875
876

	/* CCK */
877
	tx->supp_rates[1] = (rate_mask & 0xF);
878

879
	IWL_DEBUG_RATE(priv, "Tx sta id: %d, rate: %d (plcp), flags: 0x%4X "
880
		       "cck/ofdm mask: 0x%x/0x%x\n", sta_id,
881
882
		       tx->rate, le32_to_cpu(tx->tx_flags),
		       tx->supp_rates[1], tx->supp_rates[0]);
883
884
}

885
u8 iwl3945_sync_sta(struct iwl_priv *priv, int sta_id, u16 tx_rate, u8 flags)
886
887
{
	unsigned long flags_spin;
Christoph Hellwig's avatar
Christoph Hellwig committed
888
	struct iwl3945_station_entry *station;
889
890
891
892
893

	if (sta_id == IWL_INVALID_STATION)
		return IWL_INVALID_STATION;

	spin_lock_irqsave(&priv->sta_lock, flags_spin);
894
	station = &priv->stations_39[sta_id];
895
896
897
898
899
900
901

	station->sta.sta.modify_mask = STA_MODIFY_TX_RATE_MSK;
	station->sta.rate_n_flags = cpu_to_le16(tx_rate);
	station->sta.mode = STA_CONTROL_MODIFY_MSK;

	spin_unlock_irqrestore(&priv->sta_lock, flags_spin);

902
903
	iwl_send_add_sta(priv,
			 (struct iwl_addsta_cmd *)&station->sta, flags);
904
	IWL_DEBUG_RATE(priv, "SCALE sync station %d to rate %d\n",
905
906
907
908
			sta_id, tx_rate);
	return sta_id;
}

Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
909
static int iwl3945_set_pwr_src(struct iwl_priv *priv, enum iwl_pwr_src src)
910
{
911
	int ret;
912
913
914
	unsigned long flags;

	spin_lock_irqsave(&priv->lock, flags);
915
916
	ret = iwl_grab_nic_access(priv);
	if (ret) {
917
		spin_unlock_irqrestore(&priv->lock, flags);
918
		return ret;
919
920
	}

Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
921
	if (src == IWL_PWR_SRC_VAUX) {
922
		if (pci_pme_capable(priv->pci_dev, PCI_D3cold)) {
923
			iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
924
925
					APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
					~APMG_PS_CTRL_MSK_PWR_SRC);
926
			iwl_release_nic_access(priv);
927

928
			iwl_poll_bit(priv, CSR_GPIO_IN,
929
930
				     CSR_GPIO_IN_VAL_VAUX_PWR_SRC,
				     CSR_GPIO_IN_BIT_AUX_POWER, 5000);
931
		} else {
932
			iwl_release_nic_access(priv);
933
		}
934
	} else {
935
		iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
936
937
938
				APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
				~APMG_PS_CTRL_MSK_PWR_SRC);

939
940
		iwl_release_nic_access(priv);
		iwl_poll_bit(priv, CSR_GPIO_IN, CSR_GPIO_IN_VAL_VMAIN_PWR_SRC,
941
942
943
944
			     CSR_GPIO_IN_BIT_AUX_POWER, 5000);	/* uS */
	}
	spin_unlock_irqrestore(&priv->lock, flags);

945
	return ret;
946
947
}

948
static int iwl3945_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
949
950
951
952
953
{
	int rc;
	unsigned long flags;

	spin_lock_irqsave(&priv->lock, flags);
954
	rc = iwl_grab_nic_access(priv);
955
956
957
958
959
	if (rc) {
		spin_unlock_irqrestore(&priv->lock, flags);
		return rc;
	}

960
	iwl_write_direct32(priv, FH39_RCSR_RBD_BASE(0), rxq->dma_addr);
Winkler, Tomas's avatar
Winkler, Tomas committed
961
	iwl_write_direct32(priv, FH39_RCSR_RPTR_ADDR(0), rxq->rb_stts_dma);
962
963
	iwl_write_direct32(priv, FH39_RCSR_WPTR(0), 0);
	iwl_write_direct32(priv, FH39_RCSR_CONFIG(0),
964
965
966
967
968
969
970
971
		FH39_RCSR_RX_CONFIG_REG_VAL_DMA_CHNL_EN_ENABLE |
		FH39_RCSR_RX_CONFIG_REG_VAL_RDRBD_EN_ENABLE |
		FH39_RCSR_RX_CONFIG_REG_BIT_WR_STTS_EN |
		FH39_RCSR_RX_CONFIG_REG_VAL_MAX_FRAG_SIZE_128 |
		(RX_QUEUE_SIZE_LOG << FH39_RCSR_RX_CONFIG_REG_POS_RBDC_SIZE) |
		FH39_RCSR_RX_CONFIG_REG_VAL_IRQ_DEST_INT_HOST |
		(1 << FH39_RCSR_RX_CONFIG_REG_POS_IRQ_RBTH) |
		FH39_RCSR_RX_CONFIG_REG_VAL_MSG_MODE_FH);
972
973

	/* fake read to flush all prev I/O */
974
	iwl_read_direct32(priv, FH39_RSSR_CTRL);
975

976
	iwl_release_nic_access(priv);
977
978
979
980
981
	spin_unlock_irqrestore(&priv->lock, flags);

	return 0;
}

982
static int iwl3945_tx_reset(struct iwl_priv *priv)
983
984
985
986
987
{
	int rc;
	unsigned long flags;

	spin_lock_irqsave(&priv->lock, flags);
988
	rc = iwl_grab_nic_access(priv);
989
990
991
992
993
994
	if (rc) {
		spin_unlock_irqrestore(&priv->lock, flags);
		return rc;
	}

	/* bypass mode */
995
	iwl_write_prph(priv, ALM_SCD_MODE_REG, 0x2);
996
997

	/* RA 0 is active */
998
	iwl_write_prph(priv, ALM_SCD_ARASTAT_REG, 0x01);
999
1000

	/* all 6 fifo are active */
1001
	iwl_write_prph(priv, ALM_SCD_TXFACT_REG, 0x3f);
1002

1003
1004
1005
1006
	iwl_write_prph(priv, ALM_SCD_SBYP_MODE_1_REG, 0x010000);
	iwl_write_prph(priv, ALM_SCD_SBYP_MODE_2_REG, 0x030002);
	iwl_write_prph(priv, ALM_SCD_TXF4MF_REG, 0x000004);
	iwl_write_prph(priv, ALM_SCD_TXF5MF_REG, 0x000005);
1007

1008
	iwl_write_direct32(priv, FH39_TSSR_CBB_BASE,
1009
			     priv->shared_phys);
1010

1011
	iwl_write_direct32(priv, FH39_TSSR_MSG_CONFIG,
1012
1013
1014
1015
1016
1017
1018
		FH39_TSSR_TX_MSG_CONFIG_REG_VAL_SNOOP_RD_TXPD_ON |
		FH39_TSSR_TX_MSG_CONFIG_REG_VAL_ORDER_RD_TXPD_ON |
		FH39_TSSR_TX_MSG_CONFIG_REG_VAL_MAX_FRAG_SIZE_128B |
		FH39_TSSR_TX_MSG_CONFIG_REG_VAL_SNOOP_RD_TFD_ON |
		FH39_TSSR_TX_MSG_CONFIG_REG_VAL_ORDER_RD_CBB_ON |
		FH39_TSSR_TX_MSG_CONFIG_REG_VAL_ORDER_RSP_WAIT_TH |
		FH39_TSSR_TX_MSG_CONFIG_REG_VAL_RSP_WAIT_TH);
1019

1020
	iwl_release_nic_access(priv);
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
	spin_unlock_irqrestore(&priv->lock, flags);

	return 0;
}

/**
 * iwl3945_txq_ctx_reset - Reset TX queue context
 *
 * Destroys all DMA structures and initialize them again
 */
1031
static int iwl3945_txq_ctx_reset(struct iwl_priv *priv)
1032
1033
1034
1035
{
	int rc;
	int txq_id, slots_num;

Christoph Hellwig's avatar
Christoph Hellwig committed
1036
	iwl3945_hw_txq_ctx_free(priv);
1037
1038
1039
1040
1041
1042
1043

	/* Tx CMD queue */
	rc = iwl3945_tx_reset(priv);
	if (rc)
		goto error;

	/* Tx queue(s) */
1044
	for (txq_id = 0; txq_id <= priv->hw_params.max_txq_num; txq_id++) {
1045
1046
		slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
				TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
1047
1048
		rc = iwl_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
				       txq_id);
1049
		if (rc) {
1050
			IWL_ERR(priv, "Tx %d queue init failed\n", txq_id);
1051
1052
1053
1054
1055
1056
1057
			goto error;
		}
	}

	return rc;

 error:
Christoph Hellwig's avatar
Christoph Hellwig committed
1058
	iwl3945_hw_txq_ctx_free(priv);
1059
1060
1061
	return rc;
}

Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
1062
static int iwl3945_apm_init(struct iwl_priv *priv)
1063
{
Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
1064
	int ret = 0;
1065

1066
	iwl_power_initialize(priv);
1067

1068
	iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
1069
1070
1071
1072
1073
			  CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);

	/* disable L0s without affecting L1 :don't wait for ICH L0s bug W/A) */
	iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
			  CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
1074

Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
1075
1076
	/* set "initialization complete" bit to move adapter
	* D0U* --> D0A* state */
1077
	iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
1078

1079
	ret = iwl_poll_direct_bit(priv, CSR_GP_CNTRL,
Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
1080
1081
			    CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
	if (ret < 0) {
1082
		IWL_DEBUG_INFO(priv, "Failed to init the card\n");
Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
1083
		goto out;
1084
1085
	}

Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
1086
1087
1088
1089
1090
1091
1092
1093
	ret = iwl_grab_nic_access(priv);
	if (ret)
		goto out;

	/* enable DMA */
	iwl_write_prph(priv, APMG_CLK_CTRL_REG, APMG_CLK_VAL_DMA_CLK_RQT |
						APMG_CLK_VAL_BSM_CLK_RQT);

1094
	udelay(20);
Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
1095
1096

	/* disable L1-Active */
1097
	iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
1098
1099
			  APMG_PCIDEV_STT_VAL_L1_ACT_DIS);

1100
	iwl_release_nic_access(priv);
Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
1101
1102
1103
out:
	return ret;
}
1104

Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
1105
1106
static void iwl3945_nic_config(struct iwl_priv *priv)
{
1107
	struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
1108
1109
	unsigned long flags;
	u8 rev_id = 0;
1110
1111
1112
1113

	spin_lock_irqsave(&priv->lock, flags);

	if (rev_id & PCI_CFG_REV_ID_BIT_RTP)
1114
		IWL_DEBUG_INFO(priv, "RTP type \n");
1115
	else if (rev_id & PCI_CFG_REV_ID_BIT_BASIC_SKU) {
1116
		IWL_DEBUG_INFO(priv, "3945 RADIO-MB type\n");
Abhijeet Kolekar's avatar