iwl3945-base.c 146 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
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
32
33
34
35
36
37
38
39
40
41
42
43
 * 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>
#include <linux/if_arp.h>

#include <net/ieee80211_radiotap.h>
44
#include <net/lib80211.h>
45
46
47
48
#include <net/mac80211.h>

#include <asm/div64.h>

49
50
#define DRV_NAME	"iwl3945"

Winkler, Tomas's avatar
Winkler, Tomas committed
51
52
#include "iwl-fh.h"
#include "iwl-3945-fh.h"
53
#include "iwl-commands.h"
54
#include "iwl-sta.h"
55
56
#include "iwl-3945.h"
#include "iwl-helpers.h"
57
#include "iwl-core.h"
58
#include "iwl-dev.h"
59
60
61
62
63
64
65
66

/*
 * module name, copyright, version, etc.
 */

#define DRV_DESCRIPTION	\
"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"

Samuel Ortiz's avatar
Samuel Ortiz committed
67
#ifdef CONFIG_IWLWIFI_DEBUG
68
69
70
71
72
#define VD "d"
#else
#define VD
#endif

73
#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
74
75
76
77
78
#define VS "s"
#else
#define VS
#endif

79
#define IWL39_VERSION "1.2.26k" VD VS
80
#define DRV_COPYRIGHT	"Copyright(c) 2003-2009 Intel Corporation"
81
#define DRV_AUTHOR     "<ilw@linux.intel.com>"
82
#define DRV_VERSION     IWL39_VERSION
83
84
85
86


MODULE_DESCRIPTION(DRV_DESCRIPTION);
MODULE_VERSION(DRV_VERSION);
87
MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
88
89
MODULE_LICENSE("GPL");

90
91
92
 /* module parameters */
struct iwl_mod_params iwl3945_mod_params = {
	.num_of_queues = IWL39_MAX_NUM_QUEUES,
93
	.sw_crypto = 1,
94
	.restart_fw = 1,
95
96
97
	/* the rest are 0 by default */
};

98
/*************** STATION TABLE MANAGEMENT ****
99
 * mac80211 should be examined to determine if sta_info is duplicating
100
101
102
103
 * the functionality provided here
 */

/**************************************************************/
104
#if 0 /* temporary disable till we add real remove station */
105
106
107
108
109
/**
 * iwl3945_remove_station - Remove driver's knowledge of station.
 *
 * NOTE:  This does not remove station from device's station table.
 */
110
static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
111
112
113
114
115
116
117
118
119
120
{
	int index = IWL_INVALID_STATION;
	int i;
	unsigned long flags;

	spin_lock_irqsave(&priv->sta_lock, flags);

	if (is_ap)
		index = IWL_AP_ID;
	else if (is_broadcast_ether_addr(addr))
121
		index = priv->hw_params.bcast_sta_id;
122
	else
123
		for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
124
125
			if (priv->stations_39[i].used &&
			    !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
126
127
128
129
130
131
132
133
						addr)) {
				index = i;
				break;
			}

	if (unlikely(index == IWL_INVALID_STATION))
		goto out;

134
135
	if (priv->stations_39[index].used) {
		priv->stations_39[index].used = 0;
136
137
138
139
140
141
142
143
144
		priv->num_stations--;
	}

	BUG_ON(priv->num_stations < 0);

out:
	spin_unlock_irqrestore(&priv->sta_lock, flags);
	return 0;
}
145
#endif
146
147
148
149
150
151

/**
 * iwl3945_clear_stations_table - Clear the driver's station table
 *
 * NOTE:  This does not clear or otherwise alter the device's station table.
 */
152
static void iwl3945_clear_stations_table(struct iwl_priv *priv)
153
154
155
156
157
158
{
	unsigned long flags;

	spin_lock_irqsave(&priv->sta_lock, flags);

	priv->num_stations = 0;
159
	memset(priv->stations_39, 0, sizeof(priv->stations_39));
160
161
162
163

	spin_unlock_irqrestore(&priv->sta_lock, flags);
}

164
165
166
/**
 * iwl3945_add_station - Add station to station tables in driver and device
 */
167
u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
168
169
170
{
	int i;
	int index = IWL_INVALID_STATION;
Christoph Hellwig's avatar
Christoph Hellwig committed
171
	struct iwl3945_station_entry *station;
172
	unsigned long flags_spin;
173
	u8 rate;
174
175
176
177
178

	spin_lock_irqsave(&priv->sta_lock, flags_spin);
	if (is_ap)
		index = IWL_AP_ID;
	else if (is_broadcast_ether_addr(addr))
179
		index = priv->hw_params.bcast_sta_id;
180
	else
181
		for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
182
			if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
183
184
185
186
187
						addr)) {
				index = i;
				break;
			}

188
			if (!priv->stations_39[i].used &&
189
190
191
192
			    index == IWL_INVALID_STATION)
				index = i;
		}

193
	/* These two conditions has the same outcome but keep them separate
194
195
196
197
198
199
	  since they have different meaning */
	if (unlikely(index == IWL_INVALID_STATION)) {
		spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
		return index;
	}

200
201
	if (priv->stations_39[index].used &&
	   !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
202
203
204
205
		spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
		return index;
	}

206
	IWL_DEBUG_ASSOC(priv, "Add STA ID %d: %pM\n", index, addr);
207
	station = &priv->stations_39[index];
208
209
210
	station->used = 1;
	priv->num_stations++;

211
	/* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwig's avatar
Christoph Hellwig committed
212
	memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
213
214
215
216
217
	memcpy(station->sta.sta.addr, addr, ETH_ALEN);
	station->sta.mode = 0;
	station->sta.sta.sta_id = index;
	station->sta.station_flags = 0;

218
	if (priv->band == IEEE80211_BAND_5GHZ)
219
220
221
		rate = IWL_RATE_6M_PLCP;
	else
		rate =	IWL_RATE_1M_PLCP;
222
223
224

	/* Turn on both antennas for the station... */
	station->sta.rate_n_flags =
Christoph Hellwig's avatar
Christoph Hellwig committed
225
			iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
226

227
	spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
228
229

	/* Add station to device's station table */
230
231
	iwl_send_add_sta(priv,
			 (struct iwl_addsta_cmd *)&station->sta, flags);
232
233
234
235
	return index;

}

236
static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
237
238
{
	int rc = 0;
239
	struct iwl_rx_packet *res = NULL;
Christoph Hellwig's avatar
Christoph Hellwig committed
240
	struct iwl3945_rxon_assoc_cmd rxon_assoc;
241
	struct iwl_host_cmd cmd = {
242
243
244
245
246
		.id = REPLY_RXON_ASSOC,
		.len = sizeof(rxon_assoc),
		.meta.flags = CMD_WANT_SKB,
		.data = &rxon_assoc,
	};
247
248
	const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
	const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
249
250
251
252
253

	if ((rxon1->flags == rxon2->flags) &&
	    (rxon1->filter_flags == rxon2->filter_flags) &&
	    (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
	    (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
254
		IWL_DEBUG_INFO(priv, "Using current RXON_ASSOC.  Not resending.\n");
255
256
257
		return 0;
	}

258
259
260
261
	rxon_assoc.flags = priv->staging_rxon.flags;
	rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
	rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
	rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
262
263
	rxon_assoc.reserved = 0;

264
	rc = iwl_send_cmd_sync(priv, &cmd);
265
266
267
	if (rc)
		return rc;

268
	res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
269
	if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
270
		IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
271
272
273
274
275
276
277
278
279
		rc = -EIO;
	}

	priv->alloc_rxb_skb--;
	dev_kfree_skb_any(cmd.meta.u.skb);

	return rc;
}

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
 * iwl3945_get_antenna_flags - Get antenna flags for RXON command
 * @priv: eeprom and antenna fields are used to determine antenna flags
 *
 * priv->eeprom39  is used to determine if antenna AUX/MAIN are reversed
 * iwl3945_mod_params.antenna specifies the antenna diversity mode:
 *
 * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
 * IWL_ANTENNA_MAIN      - Force MAIN antenna
 * IWL_ANTENNA_AUX       - Force AUX antenna
 */
__le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
{
	struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;

	switch (iwl3945_mod_params.antenna) {
	case IWL_ANTENNA_DIVERSITY:
		return 0;

	case IWL_ANTENNA_MAIN:
		if (eeprom->antenna_switch_type)
			return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
		return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;

	case IWL_ANTENNA_AUX:
		if (eeprom->antenna_switch_type)
			return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
		return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
	}

	/* bad antenna selector value */
	IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
		iwl3945_mod_params.antenna);

	return 0;		/* "diversity" is default if error */
}

317
/**
Christoph Hellwig's avatar
Christoph Hellwig committed
318
 * iwl3945_commit_rxon - commit staging_rxon to hardware
319
 *
320
 * The RXON command in staging_rxon is committed to the hardware and
321
322
323
324
 * the active_rxon structure is updated with the new data.  This
 * function correctly transitions out of the RXON_ASSOC_MSK state if
 * a HW tune is required based on the RXON structure changes.
 */
325
static int iwl3945_commit_rxon(struct iwl_priv *priv)
326
327
{
	/* cast away the const for active_rxon in this function */
328
329
	struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
	struct iwl3945_rxon_cmd *staging_rxon = (void *)&priv->staging_rxon;
330
	int rc = 0;
331
332
	bool new_assoc =
		!!(priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK);
333

334
	if (!iwl_is_alive(priv))
335
336
337
		return -1;

	/* always get timestamp with Rx frame */
338
	staging_rxon->flags |= RXON_FLG_TSF2HOST_MSK;
339
340

	/* select antenna */
341
	staging_rxon->flags &=
342
	    ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
343
	staging_rxon->flags |= iwl3945_get_antenna_flags(priv);
344

345
	rc = iwl_check_rxon_cmd(priv);
346
	if (rc) {
347
		IWL_ERR(priv, "Invalid RXON configuration.  Not committing.\n");
348
349
350
351
		return -EINVAL;
	}

	/* If we don't need to send a full RXON, we can use
Christoph Hellwig's avatar
Christoph Hellwig committed
352
	 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
353
	 * and other flags for the current radio configuration. */
354
	if (!iwl_full_rxon_required(priv)) {
Christoph Hellwig's avatar
Christoph Hellwig committed
355
		rc = iwl3945_send_rxon_assoc(priv);
356
		if (rc) {
357
			IWL_ERR(priv, "Error setting RXON_ASSOC "
358
359
360
361
				  "configuration (%d).\n", rc);
			return rc;
		}

362
		memcpy(active_rxon, staging_rxon, sizeof(*active_rxon));
363
364
365
366
367
368
369
370

		return 0;
	}

	/* If we are currently associated and the new config requires
	 * an RXON_ASSOC and the new config wants the associated mask enabled,
	 * we must clear the associated from the active configuration
	 * before we apply the new config */
371
	if (iwl_is_associated(priv) && new_assoc) {
372
		IWL_DEBUG_INFO(priv, "Toggling associated bit on current RXON\n");
373
374
		active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;

375
376
377
378
379
380
		/*
		 * reserved4 and 5 could have been filled by the iwlcore code.
		 * Let's clear them before pushing to the 3945.
		 */
		active_rxon->reserved4 = 0;
		active_rxon->reserved5 = 0;
381
		rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwig's avatar
Christoph Hellwig committed
382
				      sizeof(struct iwl3945_rxon_cmd),
383
				      &priv->active_rxon);
384
385
386
387
388

		/* If the mask clearing failed then we set
		 * active_rxon back to what it was previously */
		if (rc) {
			active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
389
			IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
390
391
392
393
394
				  "configuration (%d).\n", rc);
			return rc;
		}
	}

395
	IWL_DEBUG_INFO(priv, "Sending RXON\n"
396
397
		       "* with%s RXON_FILTER_ASSOC_MSK\n"
		       "* channel = %d\n"
Johannes Berg's avatar
Johannes Berg committed
398
		       "* bssid = %pM\n",
399
		       (new_assoc ? "" : "out"),
400
401
402
403
404
405
406
407
408
		       le16_to_cpu(staging_rxon->channel),
		       staging_rxon->bssid_addr);

	/*
	 * reserved4 and 5 could have been filled by the iwlcore code.
	 * Let's clear them before pushing to the 3945.
	 */
	staging_rxon->reserved4 = 0;
	staging_rxon->reserved5 = 0;
409

410
411
	iwl_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);

412
	/* Apply the new configuration */
413
	rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
414
415
			      sizeof(struct iwl3945_rxon_cmd),
			      staging_rxon);
416
	if (rc) {
417
		IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
418
419
420
		return rc;
	}

421
	memcpy(active_rxon, staging_rxon, sizeof(*active_rxon));
422

Christoph Hellwig's avatar
Christoph Hellwig committed
423
	iwl3945_clear_stations_table(priv);
424

425
426
	/* If we issue a new RXON command which required a tune then we must
	 * send a new TXPOWER command or we won't be able to Tx any frames */
Samuel Ortiz's avatar
Samuel Ortiz committed
427
	rc = priv->cfg->ops->lib->send_tx_power(priv);
428
	if (rc) {
429
		IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
430
431
432
433
		return rc;
	}

	/* Add the broadcast address so we can send broadcast frames */
434
	if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
435
	    IWL_INVALID_STATION) {
436
		IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
437
438
439
440
441
		return -EIO;
	}

	/* If we have set the ASSOC_MSK and we are in BSS mode then
	 * add the IWL_AP_ID to the station rate table */
442
	if (iwl_is_associated(priv) &&
443
	    (priv->iw_mode == NL80211_IFTYPE_STATION))
444
445
		if (iwl3945_add_station(priv, priv->active_rxon.bssid_addr,
					1, 0)
446
		    == IWL_INVALID_STATION) {
447
			IWL_ERR(priv, "Error adding AP address for transmit\n");
448
449
450
			return -EIO;
		}

451
	/* Init the hardware's rate fallback order based on the band */
452
453
	rc = iwl3945_init_hw_rate_table(priv);
	if (rc) {
454
		IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
455
456
457
458
459
460
		return -EIO;
	}

	return 0;
}

461
static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
462
463
464
465
466
				   struct ieee80211_key_conf *keyconf,
				   u8 sta_id)
{
	unsigned long flags;
	__le16 key_flags = 0;
467
468
469
470
471
472
473
474
475
476
477
	int ret;

	key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
	key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);

	if (sta_id == priv->hw_params.bcast_sta_id)
		key_flags |= STA_KEY_MULTICAST_MSK;

	keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
	keyconf->hw_key_idx = keyconf->keyidx;
	key_flags &= ~STA_KEY_FLG_INVALID;
478
479

	spin_lock_irqsave(&priv->sta_lock, flags);
480
481
482
	priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
	priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
	memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
483
484
	       keyconf->keylen);

485
	memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
486
	       keyconf->keylen);
487

488
	if ((priv->stations_39[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
489
			== STA_KEY_FLG_NO_ENC)
490
		priv->stations_39[sta_id].sta.key.key_offset =
491
492
493
494
				 iwl_get_free_ucode_key_index(priv);
	/* else, we are overriding an existing key => no need to allocated room
	* in uCode. */

495
	WARN(priv->stations_39[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
496
497
		"no space for a new key");

498
499
500
	priv->stations_39[sta_id].sta.key.key_flags = key_flags;
	priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
	priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
501

502
503
504
505
506
	IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");

	ret = iwl_send_add_sta(priv,
		(struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, CMD_ASYNC);

507
508
	spin_unlock_irqrestore(&priv->sta_lock, flags);

509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
	return ret;
}

static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
				  struct ieee80211_key_conf *keyconf,
				  u8 sta_id)
{
	return -EOPNOTSUPP;
}

static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
				  struct ieee80211_key_conf *keyconf,
				  u8 sta_id)
{
	return -EOPNOTSUPP;
524
525
}

526
static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
527
528
529
530
{
	unsigned long flags;

	spin_lock_irqsave(&priv->sta_lock, flags);
531
532
	memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
	memset(&priv->stations_39[sta_id].sta.key, 0,
533
		sizeof(struct iwl4965_keyinfo));
534
535
536
	priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
	priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
	priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
537
538
	spin_unlock_irqrestore(&priv->sta_lock, flags);

539
	IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
540
541
	iwl_send_add_sta(priv,
		(struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, 0);
542
543
544
	return 0;
}

Abhijeet Kolekar's avatar
Abhijeet Kolekar committed
545
static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
			struct ieee80211_key_conf *keyconf, u8 sta_id)
{
	int ret = 0;

	keyconf->hw_key_idx = HW_KEY_DYNAMIC;

	switch (keyconf->alg) {
	case ALG_CCMP:
		ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
		break;
	case ALG_TKIP:
		ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
		break;
	case ALG_WEP:
		ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
		break;
	default:
563
		IWL_ERR(priv, "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
		ret = -EINVAL;
	}

	IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
		      keyconf->alg, keyconf->keylen, keyconf->keyidx,
		      sta_id, ret);

	return ret;
}

static int iwl3945_remove_static_key(struct iwl_priv *priv)
{
	int ret = -EOPNOTSUPP;

	return ret;
}

static int iwl3945_set_static_key(struct iwl_priv *priv,
				struct ieee80211_key_conf *key)
{
	if (key->alg == ALG_WEP)
		return -EOPNOTSUPP;

	IWL_ERR(priv, "Static key invalid: alg %d\n", key->alg);
	return -EINVAL;
}

591
static void iwl3945_clear_free_frames(struct iwl_priv *priv)
592
593
594
{
	struct list_head *element;

595
	IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
596
597
598
599
600
		       priv->frames_count);

	while (!list_empty(&priv->free_frames)) {
		element = priv->free_frames.next;
		list_del(element);
Christoph Hellwig's avatar
Christoph Hellwig committed
601
		kfree(list_entry(element, struct iwl3945_frame, list));
602
603
604
605
		priv->frames_count--;
	}

	if (priv->frames_count) {
606
		IWL_WARN(priv, "%d frames still in use.  Did we lose one?\n",
607
608
609
610
611
			    priv->frames_count);
		priv->frames_count = 0;
	}
}

612
static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
613
{
Christoph Hellwig's avatar
Christoph Hellwig committed
614
	struct iwl3945_frame *frame;
615
616
617
618
	struct list_head *element;
	if (list_empty(&priv->free_frames)) {
		frame = kzalloc(sizeof(*frame), GFP_KERNEL);
		if (!frame) {
619
			IWL_ERR(priv, "Could not allocate frame!\n");
620
621
622
623
624
625
626
627
628
			return NULL;
		}

		priv->frames_count++;
		return frame;
	}

	element = priv->free_frames.next;
	list_del(element);
Christoph Hellwig's avatar
Christoph Hellwig committed
629
	return list_entry(element, struct iwl3945_frame, list);
630
631
}

632
static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
633
634
635
636
637
{
	memset(frame, 0, sizeof(*frame));
	list_add(&frame->list, &priv->free_frames);
}

638
unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
639
				struct ieee80211_hdr *hdr,
640
				int left)
641
642
{

643
	if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
644
645
	    ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
	     (priv->iw_mode != NL80211_IFTYPE_AP)))
646
647
648
649
650
651
652
653
654
655
		return 0;

	if (priv->ibss_beacon->len > left)
		return 0;

	memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);

	return priv->ibss_beacon->len;
}

656
static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
657
{
Christoph Hellwig's avatar
Christoph Hellwig committed
658
	struct iwl3945_frame *frame;
659
660
661
662
	unsigned int frame_size;
	int rc;
	u8 rate;

Christoph Hellwig's avatar
Christoph Hellwig committed
663
	frame = iwl3945_get_free_frame(priv);
664
665

	if (!frame) {
666
		IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
667
668
669
670
			  "command.\n");
		return -ENOMEM;
	}

671
	rate = iwl_rate_get_lowest_plcp(priv);
672

Christoph Hellwig's avatar
Christoph Hellwig committed
673
	frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
674

675
	rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
676
677
			      &frame->u.cmd[0]);

Christoph Hellwig's avatar
Christoph Hellwig committed
678
	iwl3945_free_frame(priv, frame);
679
680
681
682

	return rc;
}

683
static void iwl3945_unset_hw_params(struct iwl_priv *priv)
684
{
685
	if (priv->shared_virt)
686
		pci_free_consistent(priv->pci_dev,
Christoph Hellwig's avatar
Christoph Hellwig committed
687
				    sizeof(struct iwl3945_shared),
688
689
				    priv->shared_virt,
				    priv->shared_phys);
690
691
692
}

#define MAX_UCODE_BEACON_INTERVAL	1024
693
#define INTEL_CONN_LISTEN_INTERVAL	cpu_to_le16(0xA)
694

Christoph Hellwig's avatar
Christoph Hellwig committed
695
static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
696
697
698
699
700
701
702
703
704
705
706
707
{
	u16 new_val = 0;
	u16 beacon_factor = 0;

	beacon_factor =
	    (beacon_val + MAX_UCODE_BEACON_INTERVAL)
		/ MAX_UCODE_BEACON_INTERVAL;
	new_val = beacon_val / beacon_factor;

	return cpu_to_le16(new_val);
}

708
static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
709
710
711
712
713
714
715
716
717
718
{
	u64 interval_tm_unit;
	u64 tsf, result;
	unsigned long flags;
	struct ieee80211_conf *conf = NULL;
	u16 beacon_int = 0;

	conf = ieee80211_get_hw_conf(priv->hw);

	spin_lock_irqsave(&priv->lock, flags);
719
	priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
720
721
	priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;

722
	tsf = priv->timestamp;
723
724
725
726

	beacon_int = priv->beacon_int;
	spin_unlock_irqrestore(&priv->lock, flags);

727
	if (priv->iw_mode == NL80211_IFTYPE_STATION) {
728
729
730
731
732
733
734
		if (beacon_int == 0) {
			priv->rxon_timing.beacon_interval = cpu_to_le16(100);
			priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
		} else {
			priv->rxon_timing.beacon_interval =
				cpu_to_le16(beacon_int);
			priv->rxon_timing.beacon_interval =
Christoph Hellwig's avatar
Christoph Hellwig committed
735
			    iwl3945_adjust_beacon_interval(
736
737
738
739
740
741
				le16_to_cpu(priv->rxon_timing.beacon_interval));
		}

		priv->rxon_timing.atim_window = 0;
	} else {
		priv->rxon_timing.beacon_interval =
Christoph Hellwig's avatar
Christoph Hellwig committed
742
			iwl3945_adjust_beacon_interval(conf->beacon_int);
743
744
745
746
747
748
749
750
751
752
753
		/* TODO: we need to get atim_window from upper stack
		 * for now we set to 0 */
		priv->rxon_timing.atim_window = 0;
	}

	interval_tm_unit =
		(le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
	result = do_div(tsf, interval_tm_unit);
	priv->rxon_timing.beacon_init_val =
	    cpu_to_le32((u32) ((u64) interval_tm_unit - result));

754
755
	IWL_DEBUG_ASSOC(priv,
		"beacon interval %d beacon timer %d beacon tim %d\n",
756
757
758
759
760
		le16_to_cpu(priv->rxon_timing.beacon_interval),
		le32_to_cpu(priv->rxon_timing.beacon_init_val),
		le16_to_cpu(priv->rxon_timing.atim_window));
}

761
static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
762
{
763
	if (mode == NL80211_IFTYPE_ADHOC) {
764
		const struct iwl_channel_info *ch_info;
765

766
		ch_info = iwl_get_channel_info(priv,
767
			priv->band,
768
			le16_to_cpu(priv->staging_rxon.channel));
769
770

		if (!ch_info || !is_channel_ibss(ch_info)) {
771
			IWL_ERR(priv, "channel %d not IBSS channel\n",
772
				  le16_to_cpu(priv->staging_rxon.channel));
773
774
775
776
			return -EINVAL;
		}
	}

777
	iwl_connection_init_rx_config(priv, mode);
778

Christoph Hellwig's avatar
Christoph Hellwig committed
779
	iwl3945_clear_stations_table(priv);
780

781
	/* don't commit rxon if rf-kill is on*/
782
	if (!iwl_is_ready_rf(priv))
783
784
785
		return -EAGAIN;

	cancel_delayed_work(&priv->scan_check);
786
	if (iwl_scan_cancel_timeout(priv, 100)) {
787
		IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
788
		IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
789
790
791
		return -EAGAIN;
	}

Christoph Hellwig's avatar
Christoph Hellwig committed
792
	iwl3945_commit_rxon(priv);
793
794
795
796

	return 0;
}

797
static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
798
				      struct ieee80211_tx_info *info,
799
				      struct iwl_cmd *cmd,
800
				      struct sk_buff *skb_frag,
801
				      int sta_id)
802
{
803
	struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
804
	struct iwl3945_hw_key *keyinfo =
805
	    &priv->stations_39[sta_id].keyinfo;
806
807
808

	switch (keyinfo->alg) {
	case ALG_CCMP:
809
810
		tx->sec_ctl = TX_CMD_SEC_CCM;
		memcpy(tx->key, keyinfo->key, keyinfo->keylen);
811
		IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
812
813
814
815
816
817
		break;

	case ALG_TKIP:
		break;

	case ALG_WEP:
818
		tx->sec_ctl = TX_CMD_SEC_WEP |
819
		    (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
820
821

		if (keyinfo->keylen == 13)
822
			tx->sec_ctl |= TX_CMD_SEC_KEY128;
823

824
		memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
825

826
		IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
827
			     "with key %d\n", info->control.hw_key->hw_key_idx);
828
829
830
		break;

	default:
Tomas Winkler's avatar
Tomas Winkler committed
831
		IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
832
833
834
835
836
837
838
		break;
	}
}

/*
 * handle build REPLY_TX command notification.
 */
839
static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
840
				  struct iwl_cmd *cmd,
841
				  struct ieee80211_tx_info *info,
842
				  struct ieee80211_hdr *hdr, u8 std_id)
843
{
844
845
	struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
	__le32 tx_flags = tx->tx_flags;
846
	__le16 fc = hdr->frame_control;
847
	u8 rc_flags = info->control.rates[0].flags;
848

849
	tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
850
	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
851
		tx_flags |= TX_CMD_FLG_ACK_MSK;
852
		if (ieee80211_is_mgmt(fc))
853
			tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
854
		if (ieee80211_is_probe_resp(fc) &&
855
856
857
858
859
860
861
		    !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
			tx_flags |= TX_CMD_FLG_TSF_MSK;
	} else {
		tx_flags &= (~TX_CMD_FLG_ACK_MSK);
		tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
	}

862
	tx->sta_id = std_id;
863
	if (ieee80211_has_morefrags(fc))
864
865
		tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;

866
867
	if (ieee80211_is_data_qos(fc)) {
		u8 *qc = ieee80211_get_qos_ctl(hdr);
868
		tx->tid_tspec = qc[0] & 0xf;
869
		tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
870
	} else {
871
		tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
872
	}
873

874
	if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
875
876
		tx_flags |= TX_CMD_FLG_RTS_MSK;
		tx_flags &= ~TX_CMD_FLG_CTS_MSK;
877
	} else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
878
879
880
881
882
883
884
885
		tx_flags &= ~TX_CMD_FLG_RTS_MSK;
		tx_flags |= TX_CMD_FLG_CTS_MSK;
	}

	if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
		tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;

	tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
886
887
	if (ieee80211_is_mgmt(fc)) {
		if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
888
			tx->timeout.pm_frame_timeout = cpu_to_le16(3);
889
		else
890
			tx->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbas's avatar
Mohamed Abbas committed
891
	} else {
892
		tx->timeout.pm_frame_timeout = 0;
893
#ifdef CONFIG_IWLWIFI_LEDS
Mohamed Abbas's avatar
Mohamed Abbas committed
894
895
896
		priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
#endif
	}
897

898
899
900
	tx->driver_txop = 0;
	tx->tx_flags = tx_flags;
	tx->next_frame_len = 0;
901
902
}

903
904
905
/**
 * iwl3945_get_sta_id - Find station's index within station table
 */
906
static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
907
908
909
910
{
	int sta_id;
	u16 fc = le16_to_cpu(hdr->frame_control);

911
	/* If this frame is broadcast or management, use broadcast station id */
912
913
	if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
	    is_multicast_ether_addr(hdr->addr1))
914
		return priv->hw_params.bcast_sta_id;
915
916
917

	switch (priv->iw_mode) {

918
919
	/* If we are a client station in a BSS network, use the special
	 * AP station entry (that's the only station we communicate with) */
920
	case NL80211_IFTYPE_STATION:
921
922
923
		return IWL_AP_ID;

	/* If we are an AP, then find the station, or use BCAST */
924
	case NL80211_IFTYPE_AP:
Christoph Hellwig's avatar
Christoph Hellwig committed
925
		sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
926
927
		if (sta_id != IWL_INVALID_STATION)
			return sta_id;
928
		return priv->hw_params.bcast_sta_id;
929

930
931
	/* If this frame is going out to an IBSS network, find the station,
	 * or create a new station table entry */
932
	case NL80211_IFTYPE_ADHOC: {
933
		/* Create new station table entry */
Christoph Hellwig's avatar
Christoph Hellwig committed
934
		sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
935
936
937
		if (sta_id != IWL_INVALID_STATION)
			return sta_id;

Christoph Hellwig's avatar
Christoph Hellwig committed
938
		sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
939
940
941
942

		if (sta_id != IWL_INVALID_STATION)
			return sta_id;

943
		IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
944
			       "Defaulting to broadcast...\n",
Johannes Berg's avatar
Johannes Berg committed
945
			       hdr->addr1);
946
		iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
947
		return priv->hw_params.bcast_sta_id;
948
	}
949
950
	/* If we are in monitor mode, use BCAST. This is required for
	 * packet injection. */
951
	case NL80211_IFTYPE_MONITOR:
952
		return priv->hw_params.bcast_sta_id;
953

954
	default:
955
956
		IWL_WARN(priv, "Unknown mode of operation: %d\n",
			priv->iw_mode);
957
		return priv->hw_params.bcast_sta_id;
958
959
960
961
962
963
	}
}

/*
 * start REPLY_TX command process
 */
964
static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
965
966
{
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
967
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
968
	struct iwl3945_tx_cmd *tx;
969
	struct iwl_tx_queue *txq = NULL;
970
	struct iwl_queue *q = NULL;
971
	struct iwl_cmd *out_cmd = NULL;
972
973
	dma_addr_t phys_addr;
	dma_addr_t txcmd_phys;
974
	int txq_id = skb_get_queue_mapping(skb);
975
976
977
	u16 len, idx, len_org, hdr_len;
	u8 id;
	u8 unicast;
978
	u8 sta_id;
979
	u8 tid = 0;
980
	u16 seq_number = 0;
981
	__le16 fc;
982
	u8 wait_write_ptr = 0;
983
	u8 *qc = NULL;
984
985
986
987
	unsigned long flags;
	int rc;

	spin_lock_irqsave(&priv->lock, flags);
988
	if (iwl_is_rfkill(priv)) {
989
		IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
990
991
992
		goto drop_unlock;
	}

993
	if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
994
		IWL_ERR(priv, "ERROR: No TX rate available.\n");
995
996
997
998
999
1000
		goto drop_unlock;
	}

	unicast = !is_multicast_ether_addr(hdr->addr1);
	id = 0;

1001
	fc = hdr->frame_control;
1002

Samuel Ortiz's avatar
Samuel Ortiz committed
1003
#ifdef CONFIG_IWLWIFI_DEBUG
1004
	if (ieee80211_is_auth(fc))
1005
		IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
1006
	else if (ieee80211_is_assoc_req(fc))
1007
		IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
1008
	else if (ieee80211_is_reassoc_req(fc))
1009
		IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
1010
1011
#endif

1012
	/* drop all data frame if we are not associated */
1013
	if (ieee80211_is_data(fc) &&
1014
	    (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */