iwl-3945.c 83.1 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 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
{
	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
156
	if (!iwl3945_hw_valid_rtc_data_addr(base)) {
157
		IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
158 159 160
		return;
	}

161 162
	disable_ptr = iwl_read_targ_mem(priv, base + (4 * sizeof(u32)));
	array_size = iwl_read_targ_mem(priv, base + (5 * sizeof(u32)));
163 164

	if (IWL_EVT_DISABLE && (array_size == IWL_EVT_DISABLE_SIZE)) {
165
		IWL_DEBUG_INFO(priv, "Disabling selected uCode log events at 0x%x\n",
166 167
			       disable_ptr);
		for (i = 0; i < IWL_EVT_DISABLE_SIZE; i++)
168
			iwl_write_targ_mem(priv,
169 170
					   disable_ptr + (i * sizeof(u32)),
					   evt_disable[i]);
171 172

	} else {
173 174 175
		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",
176 177 178 179 180
			       disable_ptr, array_size);
	}

}

181 182 183 184 185 186 187 188 189 190
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
191
#ifdef CONFIG_IWLWIFI_DEBUG
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
#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

226 227 228 229 230
/*
 * get ieee prev rate from rate scale table.
 * for A and B mode we need to overright prev
 * value
 */
231
int iwl3945_rs_next_rate(struct iwl_priv *priv, int rate)
232 233 234 235 236 237 238 239 240 241
{
	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;
242 243
	case IEEE80211_BAND_2GHZ:
		if (!(priv->sta_supp_rates & IWL_OFDM_RATES_MASK) &&
244
		    iwl_is_associated(priv)) {
245 246 247
			if (rate == IWL_RATE_11M_INDEX)
				next_rate = IWL_RATE_5M_INDEX;
		}
248
		break;
249

250 251 252 253 254 255 256
	default:
		break;
	}

	return next_rate;
}

257 258 259 260 261 262 263 264

/**
 * 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.
 */
265
static void iwl3945_tx_queue_reclaim(struct iwl_priv *priv,
266 267
				     int txq_id, int index)
{
268
	struct iwl_tx_queue *txq = &priv->txq[txq_id];
269
	struct iwl_queue *q = &txq->q;
Winkler, Tomas's avatar
Winkler, Tomas committed
270
	struct iwl_tx_info *tx_info;
271 272 273 274 275 276 277

	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];
278
		ieee80211_tx_status_irqsafe(priv->hw, tx_info->skb[0]);
279
		tx_info->skb[0] = NULL;
280
		priv->cfg->ops->lib->txq_free_tfd(priv, txq);
281 282
	}

283
	if (iwl_queue_space(q) > q->low_mark && (txq_id >= 0) &&
284 285
			(txq_id != IWL_CMD_QUEUE_NUM) &&
			priv->mac80211_registered)
286
		iwl_wake_queue(priv, txq_id);
287 288 289 290 291
}

/**
 * iwl3945_rx_reply_tx - Handle Tx response
 */
292
static void iwl3945_rx_reply_tx(struct iwl_priv *priv,
293
			    struct iwl_rx_mem_buffer *rxb)
294
{
295
	struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
296 297 298
	u16 sequence = le16_to_cpu(pkt->hdr.sequence);
	int txq_id = SEQ_TO_QUEUE(sequence);
	int index = SEQ_TO_INDEX(sequence);
299
	struct iwl_tx_queue *txq = &priv->txq[txq_id];
300
	struct ieee80211_tx_info *info;
301 302 303
	struct iwl3945_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
	u32  status = le32_to_cpu(tx_resp->status);
	int rate_idx;
304
	int fail;
305

306
	if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
307
		IWL_ERR(priv, "Read index for DMA queue txq_id (%d) index %d "
308 309 310 311 312 313
			  "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;
	}

314
	info = IEEE80211_SKB_CB(txq->txb[txq->q.read_ptr].skb[0]);
315 316 317 318 319 320 321 322
	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;
323 324 325

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

	/* tx_status->rts_retry_count = tx_resp->failure_rts; */
328 329
	info->flags |= ((status & TX_STATUS_MSK) == TX_STATUS_SUCCESS) ?
				IEEE80211_TX_STAT_ACK : 0;
330

331
	IWL_DEBUG_TX(priv, "Tx queue %d Status %s (0x%08x) plcp rate %d retries %d\n",
332 333 334
			txq_id, iwl3945_get_tx_fail_reason(status), status,
			tx_resp->rate, tx_resp->failure_frame);

335
	IWL_DEBUG_TX_REPLY(priv, "Tx queue reclaim %d\n", index);
336 337 338
	iwl3945_tx_queue_reclaim(priv, txq_id, index);

	if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
339
		IWL_ERR(priv, "TODO:  Implement Tx ABORT REQUIRED!!!\n");
340 341 342 343
}



344 345 346 347 348 349 350 351
/*****************************************************************************
 *
 * Intel PRO/Wireless 3945ABG/BG Network Connection
 *
 *  RX handler implementations
 *
 *****************************************************************************/

352
void iwl3945_hw_rx_statistics(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
353
{
354
	struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
355
	IWL_DEBUG_RX(priv, "Statistics notification received (%d vs %d).\n",
Christoph Hellwig's avatar
Christoph Hellwig committed
356
		     (int)sizeof(struct iwl3945_notif_statistics),
357 358
		     le32_to_cpu(pkt->len));

359
	memcpy(&priv->statistics_39, pkt->u.raw, sizeof(priv->statistics_39));
360

Mohamed Abbas's avatar
Mohamed Abbas committed
361 362
	iwl3945_led_background(priv);

363 364 365
	priv->last_statistics_time = jiffies;
}

366 367 368 369 370
/******************************************************************************
 *
 * Misc. internal state and helper functions
 *
 ******************************************************************************/
Samuel Ortiz's avatar
Samuel Ortiz committed
371
#ifdef CONFIG_IWLWIFI_DEBUG
372 373 374 375 376 377 378 379

/**
 * 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
380
static void _iwl3945_dbg_report_frame(struct iwl_priv *priv,
381
		      struct iwl_rx_packet *pkt,
382 383 384 385 386 387 388
		      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;
389
	__le16 fc;
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	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 */
408
	fc = header->frame_control;
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	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 */
432 433
	if (to_us && (fc & ~cpu_to_le16(IEEE80211_FCTL_PROTECTED)) ==
	    cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
		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;
452
		int rate;
453 454 455

		if (hundred)
			title = "100Frames";
456
		else if (ieee80211_has_retry(fc))
457
			title = "Retry";
458
		else if (ieee80211_is_assoc_resp(fc))
459
			title = "AscRsp";
460
		else if (ieee80211_is_reassoc_resp(fc))
461
			title = "RasRsp";
462
		else if (ieee80211_is_probe_resp(fc)) {
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
			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)
489
			IWL_DEBUG_RX(priv, "%s: mhd=0x%04x, dst=0x%02x, "
490
				     "len=%u, rssi=%d, chnl=%d, rate=%d, \n",
491
				     title, le16_to_cpu(fc), header->addr1[5],
492 493 494
				     length, rssi, channel, rate);
		else {
			/* src/dst addresses assume managed mode */
495
			IWL_DEBUG_RX(priv, "%s: 0x%04x, dst=0x%02x, "
496 497
				     "src=0x%02x, rssi=%u, tim=%lu usec, "
				     "phy=0x%02x, chnl=%d\n",
498
				     title, le16_to_cpu(fc), header->addr1[5],
499 500 501 502 503 504
				     header->addr3[5], rssi,
				     tsf_low - priv->scan_start_tsf,
				     phy_flags, channel);
		}
	}
	if (print_dump)
505
		iwl_print_hex_dump(priv, IWL_DL_RX, data, length);
506
}
Samuel Ortiz's avatar
Samuel Ortiz committed
507 508 509 510 511 512 513 514 515

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);
}

516
#else
517
static inline void iwl3945_dbg_report_frame(struct iwl_priv *priv,
518
		      struct iwl_rx_packet *pkt,
519 520 521 522 523
		      struct ieee80211_hdr *header, int group100)
{
}
#endif

524
/* This is necessary only for a number of statistics, see the caller. */
525
static int iwl3945_is_network_packet(struct iwl_priv *priv,
526 527 528 529 530
		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) {
531
	case NL80211_IFTYPE_ADHOC: /* Header: Dest. | Source    | BSSID */
532 533
		/* packets to our IBSS update information */
		return !compare_ether_addr(header->addr3, priv->bssid);
534
	case NL80211_IFTYPE_STATION: /* Header: Dest. | AP{BSSID} | Source */
535 536 537 538 539 540
		/* packets to our IBSS update information */
		return !compare_ether_addr(header->addr2, priv->bssid);
	default:
		return 1;
	}
}
541

542
static void iwl3945_pass_packet_to_mac80211(struct iwl_priv *priv,
543
				   struct iwl_rx_mem_buffer *rxb,
Zhu Yi's avatar
Zhu Yi committed
544
				   struct ieee80211_rx_status *stats)
545
{
546
	struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
547
#ifdef CONFIG_IWLWIFI_LEDS
548
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)IWL_RX_DATA(pkt);
549
#endif
Christoph Hellwig's avatar
Christoph Hellwig committed
550 551
	struct iwl3945_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
	struct iwl3945_rx_frame_end *rx_end = IWL_RX_END(pkt);
552 553 554
	short len = le16_to_cpu(rx_hdr->len);

	/* We received data from the HW, so stop the watchdog */
555
	if (unlikely((len + IWL39_RX_FRAME_SIZE) > skb_tailroom(rxb->skb))) {
556
		IWL_DEBUG_DROP(priv, "Corruption detected!\n");
557 558 559 560 561
		return;
	}

	/* We only process data packets if the interface is open */
	if (unlikely(!priv->is_open)) {
562 563
		IWL_DEBUG_DROP_LIMIT(priv,
			"Dropping packet while interface is not open.\n");
564 565 566 567 568 569 570
		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));

571
	if (!iwl3945_mod_params.sw_crypto)
572 573
		iwl_set_decrypted_flag(priv,
				       (struct ieee80211_hdr *)rxb->skb->data,
574 575
				       le32_to_cpu(rx_end->status), stats);

576
#ifdef CONFIG_IWLWIFI_LEDS
577
	if (ieee80211_is_data(hdr->frame_control))
Mohamed Abbas's avatar
Mohamed Abbas committed
578 579
		priv->rxtxpackets += len;
#endif
580 581
	memcpy(IEEE80211_SKB_RXCB(rxb->skb), stats, sizeof(*stats));
	ieee80211_rx_irqsafe(priv->hw, rxb->skb);
582 583 584
	rxb->skb = NULL;
}

585 586
#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)

587
static void iwl3945_rx_reply_rx(struct iwl_priv *priv,
588
				struct iwl_rx_mem_buffer *rxb)
589
{
590 591
	struct ieee80211_hdr *header;
	struct ieee80211_rx_status rx_status;
592
	struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwig's avatar
Christoph Hellwig committed
593 594 595
	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);
596
	int snr;
597 598 599
	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;
600 601 602

	rx_status.flag = 0;
	rx_status.mactime = le64_to_cpu(rx_end->timestamp);
603
	rx_status.freq =
604
		ieee80211_channel_to_frequency(le16_to_cpu(rx_hdr->channel));
605 606 607 608 609 610
	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;
611

612 613 614 615 616 617 618
	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;

619
	if ((unlikely(rx_stats->phy_count > 20))) {
620 621
		IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
				rx_stats->phy_count);
622 623 624 625 626
		return;
	}

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

631

632 633

	/* Convert 3945's rssi indicator to dBm */
634
	rx_status.signal = rx_stats->rssi - IWL39_RSSI_OFFSET;
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649

	/* 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.
650
	 * Calculate rx_status.signal (quality indicator in %) based on SNR. */
651 652
	if (rx_stats_noise_diff) {
		snr = rx_stats_sig_avg / rx_stats_noise_diff;
653
		rx_status.noise = rx_status.signal -
654
					iwl3945_calc_db_from_ratio(snr);
655
		rx_status.qual = iwl3945_calc_sig_qual(rx_status.signal,
656
							 rx_status.noise);
657 658 659 660

	/* If noise info not available, calculate signal quality indicator (%)
	 *   using just the dBm signal level. */
	} else {
661
		rx_status.noise = priv->last_rx_noise;
662
		rx_status.qual = iwl3945_calc_sig_qual(rx_status.signal, 0);
663 664 665
	}


666
	IWL_DEBUG_STATS(priv, "Rssi %d noise %d qual %d sig_avg %d noise_diff %d\n",
667
			rx_status.signal, rx_status.noise, rx_status.qual,
668 669 670 671
			rx_stats_sig_avg, rx_stats_noise_diff);

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

Christoph Hellwig's avatar
Christoph Hellwig committed
672
	network_packet = iwl3945_is_network_packet(priv, header);
673

674
	IWL_DEBUG_STATS_LIMIT(priv, "[%c] %d RSSI:%d Signal:%u, Noise:%u, Rate:%u\n",
675 676
			      network_packet ? '*' : ' ',
			      le16_to_cpu(rx_hdr->channel),
677 678
			      rx_status.signal, rx_status.signal,
			      rx_status.noise, rx_status.rate_idx);
679

Samuel Ortiz's avatar
Samuel Ortiz committed
680 681
	/* Set "1" to report good data frames in groups of 100 */
	iwl3945_dbg_report_frame(priv, pkt, header, 1);
682 683 684 685

	if (network_packet) {
		priv->last_beacon_time = le32_to_cpu(rx_end->beacon_timestamp);
		priv->last_tsf = le64_to_cpu(rx_end->timestamp);
686
		priv->last_rx_rssi = rx_status.signal;
687
		priv->last_rx_noise = rx_status.noise;
688 689
	}

Abhijeet Kolekar's avatar
Abhijeet Kolekar committed
690
	iwl3945_pass_packet_to_mac80211(priv, rxb, &rx_status);
691 692
}

693 694 695
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)
696 697
{
	int count;
698
	struct iwl_queue *q;
699
	struct iwl3945_tfd *tfd, *tfd_tmp;
700 701

	q = &txq->q;
702 703
	tfd_tmp = (struct iwl3945_tfd *)txq->tfds;
	tfd = &tfd_tmp[q->write_ptr];
704 705 706

	if (reset)
		memset(tfd, 0, sizeof(*tfd));
707 708 709 710

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

	if ((count >= NUM_TFD_CHUNKS) || (count < 0)) {
711
		IWL_ERR(priv, "Error can not send more than %d chunks\n",
712 713 714 715
			  NUM_TFD_CHUNKS);
		return -EINVAL;
	}

Winkler, Tomas's avatar
Winkler, Tomas committed
716 717
	tfd->tbs[count].addr = cpu_to_le32(addr);
	tfd->tbs[count].len = cpu_to_le32(len);
718 719 720 721 722 723 724 725 726 727

	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
728
 * iwl3945_hw_txq_free_tfd - Free one TFD, those at index [txq->q.read_ptr]
729 730 731
 *
 * Does NOT advance any indexes
 */
732
void iwl3945_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
733
{
734
	struct iwl3945_tfd *tfd_tmp = (struct iwl3945_tfd *)txq->tfds;
735 736
	int index = txq->q.read_ptr;
	struct iwl3945_tfd *tfd = &tfd_tmp[index];
737 738 739 740 741
	struct pci_dev *dev = priv->pci_dev;
	int i;
	int counter;

	/* sanity check */
Winkler, Tomas's avatar
Winkler, Tomas committed
742
	counter = TFD_CTL_COUNT_GET(le32_to_cpu(tfd->control_flags));
743
	if (counter > NUM_TFD_CHUNKS) {
744
		IWL_ERR(priv, "Too many chunks: %i\n", counter);
745
		/* @todo issue fatal error, it is quite serious situation */
746
		return;
747 748
	}

749 750 751 752 753 754 755
	/* 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);

756 757 758
	/* unmap chunks if any */

	for (i = 1; i < counter; i++) {
Winkler, Tomas's avatar
Winkler, Tomas committed
759 760
		pci_unmap_single(dev, le32_to_cpu(tfd->tbs[i].addr),
			 le32_to_cpu(tfd->tbs[i].len), PCI_DMA_TODEVICE);
761 762 763
		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]) {
764 765
				/* Can be called from interrupt context */
				dev_kfree_skb_any(skb);
766
				txq->txb[txq->q.read_ptr].skb[0] = NULL;
767 768 769
			}
		}
	}
770
	return ;
771 772 773
}

/**
Christoph Hellwig's avatar
Christoph Hellwig committed
774
 * iwl3945_hw_build_tx_cmd_rate - Add rate portion to TX_CMD:
775 776
 *
*/
777
void iwl3945_hw_build_tx_cmd_rate(struct iwl_priv *priv, struct iwl_cmd *cmd,
778
			      struct ieee80211_tx_info *info,
779 780
			      struct ieee80211_hdr *hdr, int sta_id, int tx_id)
{
781
	u16 hw_value = ieee80211_get_tx_rate(priv->hw, info)->hw_value;
782
	u16 rate_index = min(hw_value & 0xffff, IWL_RATE_COUNT - 1);
783 784 785 786 787
	u16 rate_mask;
	int rate;
	u8 rts_retry_limit;
	u8 data_retry_limit;
	__le32 tx_flags;
788
	__le16 fc = hdr->frame_control;
789
	struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
790

Christoph Hellwig's avatar
Christoph Hellwig committed
791
	rate = iwl3945_rates[rate_index].plcp;
792
	tx_flags = tx->tx_flags;
793 794

	/* We need to figure out how to get the sta->supp_rates while
795
	 * in this running context */
796 797 798 799 800 801 802
	rate_mask = IWL_RATES_MASK;

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

803
	if (ieee80211_is_probe_resp(fc)) {
804 805 806 807 808 809 810 811 812
		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;

813 814 815 816 817 818
	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):
819 820 821 822 823 824 825 826 827 828
			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;
		}
	}

829 830 831 832
	tx->rts_retry_limit = rts_retry_limit;
	tx->data_retry_limit = data_retry_limit;
	tx->rate = rate;
	tx->tx_flags = tx_flags;
833 834

	/* OFDM */
835
	tx->supp_rates[0] =
836
	   ((rate_mask & IWL_OFDM_RATES_MASK) >> IWL_FIRST_OFDM_RATE) & 0xFF;
837 838

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

841
	IWL_DEBUG_RATE(priv, "Tx sta id: %d, rate: %d (plcp), flags: 0x%4X "
842
		       "cck/ofdm mask: 0x%x/0x%x\n", sta_id,
843 844
		       tx->rate, le32_to_cpu(tx->tx_flags),
		       tx->supp_rates[1], tx->supp_rates[0]);
845 846
}

847
u8 iwl3945_sync_sta(struct iwl_priv *priv, int sta_id, u16 tx_rate, u8 flags)
848 849
{
	unsigned long flags_spin;
850
	struct iwl_station_entry *station;
851 852 853 854 855

	if (sta_id == IWL_INVALID_STATION)
		return IWL_INVALID_STATION;

	spin_lock_irqsave(&priv->sta_lock, flags_spin);
856
	station = &priv->stations[sta_id];
857 858 859 860 861 862 863

	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);

864
	iwl_send_add_sta(priv, &station->sta, flags);
865
	IWL_DEBUG_RATE(priv, "SCALE sync station %d to rate %d\n",
866 867 868 869
			sta_id, tx_rate);
	return sta_id;
}

Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
870
static int iwl3945_set_pwr_src(struct iwl_priv *priv, enum iwl_pwr_src src)
871
{
Kolekar, Abhijeet's avatar
Kolekar, Abhijeet committed
872
	if (src == IWL_PWR_SRC_VAUX) {
873
		if (pci_pme_capable(priv->pci_dev, PCI_D3cold)) {
874
			iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
875 876 877
					APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
					~APMG_PS_CTRL_MSK_PWR_SRC);

878
			iwl_poll_bit(priv, CSR_GPIO_IN,
879 880
				     CSR_GPIO_IN_VAL_VAUX_PWR_SRC,
				     CSR_GPIO_IN_BIT_AUX_POWER, 5000);
881
		}
882
	} else {
883
		iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
884 885 886
				APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
				~APMG_PS_CTRL_MSK_PWR_SRC);

887
		iwl_poll_bit(priv, CSR_GPIO_IN, CSR_GPIO_IN_VAL_VMAIN_PWR_SRC,
888 889 890
			     CSR_GPIO_IN_BIT_AUX_POWER, 5000);	/* uS */
	}

Mohamed Abbas's avatar
Mohamed Abbas committed
891
	return 0;
892 893
}

894
static int iwl3945_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
895
{
896
	iwl_write_direct32(priv, FH39_RCSR_RBD_BASE(0), rxq->dma_addr);
Winkler, Tomas's avatar
Winkler, Tomas committed
897
	iwl_write_direct32(priv, FH39_RCSR_RPTR_ADDR(0), rxq->rb_stts_dma);
898 899
	iwl_write_direct32(priv, FH39_RCSR_WPTR(0), 0);
	iwl_write_direct32(priv, FH39_RCSR_CONFIG(0),
900 901 902 903 904 905 906 907
		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);
908 909

	/* fake read to flush all prev I/O */
910
	iwl_read_direct32(priv, FH39_RSSR_CTRL);
911 912 913 914

	return 0;
}

915
static int iwl3945_tx_reset(struct iwl_priv *priv)
916 917 918
{

	/* bypass mode */
919
	iwl_write_prph(priv, ALM_SCD_MODE_REG, 0x2);
920 921

	/* RA 0 is active */
922
	iwl_write_prph(priv, ALM_SCD_ARASTAT_REG, 0x01);
923 924

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

927 928 929 930
	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);
931

932
	iwl_write_direct32(priv, FH39_TSSR_CBB_BASE,
933
			     priv->shared_phys);
934

935
	iwl_write_direct32(priv, FH39_TSSR_MSG_CONFIG,
936 937 938 939 940 941 942
		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);
943 944 945 946 947 948 949 950 951 952


	return 0;
}

/**
 * iwl3945_txq_ctx_reset - Reset TX queue context
 *
 * Destroys all DMA structures and initialize them again
 */
953
static int iwl3945_txq_ctx_reset(struct iwl_priv *priv)
954 955 956 957
{
	int rc;
	int txq_id, slots_num;

Christoph Hellwig's avatar
Christoph Hellwig committed
958
	iwl3945_hw_txq_ctx_free(priv);
959 960 961 962 963 964 965

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

	/* Tx queue(s) */
966
	for (txq_id =