4965-mac.c 103.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
/******************************************************************************
 *
 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
 *
 * 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:
 *  Intel Linux Wireless <ilw@linux.intel.com>
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 *****************************************************************************/

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/pci-aspm.h>
#include <linux/slab.h>
#include <linux/dma-mapping.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/firmware.h>
#include <linux/etherdevice.h>
#include <linux/if_arp.h>

#include <net/mac80211.h>

#include <asm/div64.h>

#define DRV_NAME        "iwl4965"

#include "iwl-eeprom.h"
#include "iwl-dev.h"
#include "iwl-core.h"
#include "iwl-io.h"
#include "iwl-helpers.h"
#include "iwl-sta.h"
#include "iwl-4965-calib.h"
#include "iwl-4965.h"


/******************************************************************************
 *
 * module boiler plate
 *
 ******************************************************************************/

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

74
#ifdef CONFIG_IWLEGACY_DEBUG
75 76 77 78 79 80 81 82 83 84 85 86 87 88
#define VD "d"
#else
#define VD
#endif

#define DRV_VERSION     IWLWIFI_VERSION VD


MODULE_DESCRIPTION(DRV_DESCRIPTION);
MODULE_VERSION(DRV_VERSION);
MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
MODULE_LICENSE("GPL");
MODULE_ALIAS("iwl4965");

89 90 91 92 93 94 95 96 97 98 99 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 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 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
static struct il_link_quality_cmd *
il4965_sta_alloc_lq(struct il_priv *il, u8 sta_id)
{
	int i, r;
	struct il_link_quality_cmd *link_cmd;
	u32 rate_flags = 0;
	__le32 rate_n_flags;

	link_cmd = kzalloc(sizeof(struct il_link_quality_cmd), GFP_KERNEL);
	if (!link_cmd) {
		IL_ERR("Unable to allocate memory for LQ cmd.\n");
		return NULL;
	}
	/* Set up the rate scaling to start at selected rate, fall back
	 * all the way down to 1M in IEEE order, and then spin on 1M */
	if (il->band == IEEE80211_BAND_5GHZ)
		r = RATE_6M_IDX;
	else
		r = RATE_1M_IDX;

	if (r >= IL_FIRST_CCK_RATE && r <= IL_LAST_CCK_RATE)
		rate_flags |= RATE_MCS_CCK_MSK;

	rate_flags |= il4965_first_antenna(il->hw_params.valid_tx_ant) <<
				RATE_MCS_ANT_POS;
	rate_n_flags = il4965_hw_set_rate_n_flags(il_rates[r].plcp,
						   rate_flags);
	for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
		link_cmd->rs_table[i].rate_n_flags = rate_n_flags;

	link_cmd->general_params.single_stream_ant_msk =
				il4965_first_antenna(il->hw_params.valid_tx_ant);

	link_cmd->general_params.dual_stream_ant_msk =
		il->hw_params.valid_tx_ant &
		~il4965_first_antenna(il->hw_params.valid_tx_ant);
	if (!link_cmd->general_params.dual_stream_ant_msk) {
		link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
	} else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
		link_cmd->general_params.dual_stream_ant_msk =
			il->hw_params.valid_tx_ant;
	}

	link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
	link_cmd->agg_params.agg_time_limit =
		cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);

	link_cmd->sta_id = sta_id;

	return link_cmd;
}

/*
 * il4965_add_bssid_station - Add the special IBSS BSSID station
 *
 * Function sleeps.
 */
int
il4965_add_bssid_station(struct il_priv *il, struct il_rxon_context *ctx,
			     const u8 *addr, u8 *sta_id_r)
{
	int ret;
	u8 sta_id;
	struct il_link_quality_cmd *link_cmd;
	unsigned long flags;

	if (sta_id_r)
		*sta_id_r = IL_INVALID_STATION;

	ret = il_add_station_common(il, ctx, addr, 0, NULL, &sta_id);
	if (ret) {
		IL_ERR("Unable to add station %pM\n", addr);
		return ret;
	}

	if (sta_id_r)
		*sta_id_r = sta_id;

	spin_lock_irqsave(&il->sta_lock, flags);
	il->stations[sta_id].used |= IL_STA_LOCAL;
	spin_unlock_irqrestore(&il->sta_lock, flags);

	/* Set up default rate scaling table in device's station table */
	link_cmd = il4965_sta_alloc_lq(il, sta_id);
	if (!link_cmd) {
		IL_ERR(
			"Unable to initialize rate scaling for station %pM.\n",
			addr);
		return -ENOMEM;
	}

	ret = il_send_lq_cmd(il, ctx, link_cmd, CMD_SYNC, true);
	if (ret)
		IL_ERR("Link quality command failed (%d)\n", ret);

	spin_lock_irqsave(&il->sta_lock, flags);
	il->stations[sta_id].lq = link_cmd;
	spin_unlock_irqrestore(&il->sta_lock, flags);

	return 0;
}

static int il4965_static_wepkey_cmd(struct il_priv *il,
				      struct il_rxon_context *ctx,
				      bool send_if_empty)
{
	int i, not_empty = 0;
	u8 buff[sizeof(struct il_wep_cmd) +
		sizeof(struct il_wep_key) * WEP_KEYS_MAX];
	struct il_wep_cmd *wep_cmd = (struct il_wep_cmd *)buff;
	size_t cmd_size  = sizeof(struct il_wep_cmd);
	struct il_host_cmd cmd = {
		.id = ctx->wep_key_cmd,
		.data = wep_cmd,
		.flags = CMD_SYNC,
	};

	might_sleep();

	memset(wep_cmd, 0, cmd_size +
			(sizeof(struct il_wep_key) * WEP_KEYS_MAX));

	for (i = 0; i < WEP_KEYS_MAX ; i++) {
		wep_cmd->key[i].key_idx = i;
		if (ctx->wep_keys[i].key_size) {
			wep_cmd->key[i].key_offset = i;
			not_empty = 1;
		} else {
			wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
		}

		wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
		memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
				ctx->wep_keys[i].key_size);
	}

	wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
	wep_cmd->num_keys = WEP_KEYS_MAX;

	cmd_size += sizeof(struct il_wep_key) * WEP_KEYS_MAX;

	cmd.len = cmd_size;

	if (not_empty || send_if_empty)
		return il_send_cmd(il, &cmd);
	else
		return 0;
}

int il4965_restore_default_wep_keys(struct il_priv *il,
				 struct il_rxon_context *ctx)
{
	lockdep_assert_held(&il->mutex);

	return il4965_static_wepkey_cmd(il, ctx, false);
}

int il4965_remove_default_wep_key(struct il_priv *il,
			       struct il_rxon_context *ctx,
			       struct ieee80211_key_conf *keyconf)
{
	int ret;

	lockdep_assert_held(&il->mutex);

	D_WEP("Removing default WEP key: idx=%d\n",
		      keyconf->keyidx);

	memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
	if (il_is_rfkill(il)) {
		D_WEP(
		"Not sending REPLY_WEPKEY command due to RFKILL.\n");
		/* but keys in device are clear anyway so return success */
		return 0;
	}
	ret = il4965_static_wepkey_cmd(il, ctx, 1);
	D_WEP("Remove default WEP key: idx=%d ret=%d\n",
		      keyconf->keyidx, ret);

	return ret;
}

int il4965_set_default_wep_key(struct il_priv *il,
			    struct il_rxon_context *ctx,
			    struct ieee80211_key_conf *keyconf)
{
	int ret;

	lockdep_assert_held(&il->mutex);

	if (keyconf->keylen != WEP_KEY_LEN_128 &&
	    keyconf->keylen != WEP_KEY_LEN_64) {
		D_WEP("Bad WEP key length %d\n", keyconf->keylen);
		return -EINVAL;
	}

	keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
	keyconf->hw_key_idx = HW_KEY_DEFAULT;
	il->stations[ctx->ap_sta_id].keyinfo.cipher = keyconf->cipher;

	ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
	memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
							keyconf->keylen);

	ret = il4965_static_wepkey_cmd(il, ctx, false);
	D_WEP("Set default WEP key: len=%d idx=%d ret=%d\n",
		keyconf->keylen, keyconf->keyidx, ret);

	return ret;
}

static int il4965_set_wep_dynamic_key_info(struct il_priv *il,
					struct il_rxon_context *ctx,
					struct ieee80211_key_conf *keyconf,
					u8 sta_id)
{
	unsigned long flags;
	__le16 key_flags = 0;
	struct il_addsta_cmd sta_cmd;

	lockdep_assert_held(&il->mutex);

	keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;

	key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
	key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
	key_flags &= ~STA_KEY_FLG_INVALID;

	if (keyconf->keylen == WEP_KEY_LEN_128)
		key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;

	if (sta_id == ctx->bcast_sta_id)
		key_flags |= STA_KEY_MULTICAST_MSK;

	spin_lock_irqsave(&il->sta_lock, flags);

	il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
	il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
	il->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;

	memcpy(il->stations[sta_id].keyinfo.key,
				keyconf->key, keyconf->keylen);

	memcpy(&il->stations[sta_id].sta.key.key[3],
				keyconf->key, keyconf->keylen);

	if ((il->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
			== STA_KEY_FLG_NO_ENC)
		il->stations[sta_id].sta.key.key_offset =
				 il_get_free_ucode_key_idx(il);
	/* else, we are overriding an existing key => no need to allocated room
	 * in uCode. */

	WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
		"no space for a new key");

	il->stations[sta_id].sta.key.key_flags = key_flags;
	il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
	il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;

	memcpy(&sta_cmd, &il->stations[sta_id].sta,
			sizeof(struct il_addsta_cmd));
	spin_unlock_irqrestore(&il->sta_lock, flags);

	return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
}

static int il4965_set_ccmp_dynamic_key_info(struct il_priv *il,
					 struct il_rxon_context *ctx,
					 struct ieee80211_key_conf *keyconf,
					 u8 sta_id)
{
	unsigned long flags;
	__le16 key_flags = 0;
	struct il_addsta_cmd sta_cmd;

	lockdep_assert_held(&il->mutex);

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

	if (sta_id == ctx->bcast_sta_id)
		key_flags |= STA_KEY_MULTICAST_MSK;

	keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;

	spin_lock_irqsave(&il->sta_lock, flags);
	il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
	il->stations[sta_id].keyinfo.keylen = keyconf->keylen;

	memcpy(il->stations[sta_id].keyinfo.key, keyconf->key,
	       keyconf->keylen);

	memcpy(il->stations[sta_id].sta.key.key, keyconf->key,
	       keyconf->keylen);

	if ((il->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
			== STA_KEY_FLG_NO_ENC)
		il->stations[sta_id].sta.key.key_offset =
				 il_get_free_ucode_key_idx(il);
	/* else, we are overriding an existing key => no need to allocated room
	 * in uCode. */

	WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
		"no space for a new key");

	il->stations[sta_id].sta.key.key_flags = key_flags;
	il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
	il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;

	memcpy(&sta_cmd, &il->stations[sta_id].sta,
			 sizeof(struct il_addsta_cmd));
	spin_unlock_irqrestore(&il->sta_lock, flags);

	return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
}

static int il4965_set_tkip_dynamic_key_info(struct il_priv *il,
					 struct il_rxon_context *ctx,
					 struct ieee80211_key_conf *keyconf,
					 u8 sta_id)
{
	unsigned long flags;
	int ret = 0;
	__le16 key_flags = 0;

	key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
	key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
	key_flags &= ~STA_KEY_FLG_INVALID;

	if (sta_id == ctx->bcast_sta_id)
		key_flags |= STA_KEY_MULTICAST_MSK;

	keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
	keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;

	spin_lock_irqsave(&il->sta_lock, flags);

	il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
	il->stations[sta_id].keyinfo.keylen = 16;

	if ((il->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
			== STA_KEY_FLG_NO_ENC)
		il->stations[sta_id].sta.key.key_offset =
				 il_get_free_ucode_key_idx(il);
	/* else, we are overriding an existing key => no need to allocated room
	 * in uCode. */

	WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
		"no space for a new key");

	il->stations[sta_id].sta.key.key_flags = key_flags;


	/* This copy is acutally not needed: we get the key with each TX */
	memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, 16);

	memcpy(il->stations[sta_id].sta.key.key, keyconf->key, 16);

	spin_unlock_irqrestore(&il->sta_lock, flags);

	return ret;
}

void il4965_update_tkip_key(struct il_priv *il,
			 struct il_rxon_context *ctx,
			 struct ieee80211_key_conf *keyconf,
			 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
{
	u8 sta_id;
	unsigned long flags;
	int i;

	if (il_scan_cancel(il)) {
		/* cancel scan failed, just live w/ bad key and rely
		   briefly on SW decryption */
		return;
	}

	sta_id = il_sta_id_or_broadcast(il, ctx, sta);
	if (sta_id == IL_INVALID_STATION)
		return;

	spin_lock_irqsave(&il->sta_lock, flags);

	il->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;

	for (i = 0; i < 5; i++)
		il->stations[sta_id].sta.key.tkip_rx_ttak[i] =
			cpu_to_le16(phase1key[i]);

	il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
	il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;

	il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);

	spin_unlock_irqrestore(&il->sta_lock, flags);

}

int il4965_remove_dynamic_key(struct il_priv *il,
			   struct il_rxon_context *ctx,
			   struct ieee80211_key_conf *keyconf,
			   u8 sta_id)
{
	unsigned long flags;
	u16 key_flags;
	u8 keyidx;
	struct il_addsta_cmd sta_cmd;

	lockdep_assert_held(&il->mutex);

	ctx->key_mapping_keys--;

	spin_lock_irqsave(&il->sta_lock, flags);
	key_flags = le16_to_cpu(il->stations[sta_id].sta.key.key_flags);
	keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;

	D_WEP("Remove dynamic key: idx=%d sta=%d\n",
		      keyconf->keyidx, sta_id);

	if (keyconf->keyidx != keyidx) {
		/* We need to remove a key with idx different that the one
		 * in the uCode. This means that the key we need to remove has
		 * been replaced by another one with different idx.
		 * Don't do anything and return ok
		 */
		spin_unlock_irqrestore(&il->sta_lock, flags);
		return 0;
	}

	if (il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
		IL_WARN("Removing wrong key %d 0x%x\n",
			    keyconf->keyidx, key_flags);
		spin_unlock_irqrestore(&il->sta_lock, flags);
		return 0;
	}

	if (!test_and_clear_bit(il->stations[sta_id].sta.key.key_offset,
		&il->ucode_key_table))
		IL_ERR("idx %d not used in uCode key table.\n",
			il->stations[sta_id].sta.key.key_offset);
	memset(&il->stations[sta_id].keyinfo, 0,
					sizeof(struct il_hw_key));
	memset(&il->stations[sta_id].sta.key, 0,
					sizeof(struct il4965_keyinfo));
	il->stations[sta_id].sta.key.key_flags =
			STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
	il->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
	il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
	il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;

	if (il_is_rfkill(il)) {
		D_WEP(
		 "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
		spin_unlock_irqrestore(&il->sta_lock, flags);
		return 0;
	}
	memcpy(&sta_cmd, &il->stations[sta_id].sta,
			sizeof(struct il_addsta_cmd));
	spin_unlock_irqrestore(&il->sta_lock, flags);

	return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
}

int il4965_set_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
			struct ieee80211_key_conf *keyconf, u8 sta_id)
{
	int ret;

	lockdep_assert_held(&il->mutex);

	ctx->key_mapping_keys++;
	keyconf->hw_key_idx = HW_KEY_DYNAMIC;

	switch (keyconf->cipher) {
	case WLAN_CIPHER_SUITE_CCMP:
		ret = il4965_set_ccmp_dynamic_key_info(il, ctx,
							keyconf, sta_id);
		break;
	case WLAN_CIPHER_SUITE_TKIP:
		ret = il4965_set_tkip_dynamic_key_info(il, ctx,
							keyconf, sta_id);
		break;
	case WLAN_CIPHER_SUITE_WEP40:
	case WLAN_CIPHER_SUITE_WEP104:
		ret = il4965_set_wep_dynamic_key_info(il, ctx,
							keyconf, sta_id);
		break;
	default:
		IL_ERR(
			"Unknown alg: %s cipher = %x\n", __func__,
			keyconf->cipher);
		ret = -EINVAL;
	}

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

	return ret;
}

/**
 * il4965_alloc_bcast_station - add broadcast station into driver's station table.
 *
 * This adds the broadcast station into the driver's station table
 * and marks it driver active, so that it will be restored to the
 * device at the next best time.
 */
int il4965_alloc_bcast_station(struct il_priv *il,
			       struct il_rxon_context *ctx)
{
	struct il_link_quality_cmd *link_cmd;
	unsigned long flags;
	u8 sta_id;

	spin_lock_irqsave(&il->sta_lock, flags);
	sta_id = il_prep_station(il, ctx, il_bcast_addr,
								false, NULL);
	if (sta_id == IL_INVALID_STATION) {
		IL_ERR("Unable to prepare broadcast station\n");
		spin_unlock_irqrestore(&il->sta_lock, flags);

		return -EINVAL;
	}

	il->stations[sta_id].used |= IL_STA_DRIVER_ACTIVE;
	il->stations[sta_id].used |= IL_STA_BCAST;
	spin_unlock_irqrestore(&il->sta_lock, flags);

	link_cmd = il4965_sta_alloc_lq(il, sta_id);
	if (!link_cmd) {
		IL_ERR(
			"Unable to initialize rate scaling for bcast station.\n");
		return -ENOMEM;
	}

	spin_lock_irqsave(&il->sta_lock, flags);
	il->stations[sta_id].lq = link_cmd;
	spin_unlock_irqrestore(&il->sta_lock, flags);

	return 0;
}

/**
 * il4965_update_bcast_station - update broadcast station's LQ command
 *
 * Only used by iwl4965. Placed here to have all bcast station management
 * code together.
 */
static int il4965_update_bcast_station(struct il_priv *il,
				    struct il_rxon_context *ctx)
{
	unsigned long flags;
	struct il_link_quality_cmd *link_cmd;
	u8 sta_id = ctx->bcast_sta_id;

	link_cmd = il4965_sta_alloc_lq(il, sta_id);
	if (!link_cmd) {
		IL_ERR(
		"Unable to initialize rate scaling for bcast station.\n");
		return -ENOMEM;
	}

	spin_lock_irqsave(&il->sta_lock, flags);
	if (il->stations[sta_id].lq)
		kfree(il->stations[sta_id].lq);
	else
		D_INFO(
		"Bcast station rate scaling has not been initialized yet.\n");
	il->stations[sta_id].lq = link_cmd;
	spin_unlock_irqrestore(&il->sta_lock, flags);

	return 0;
}

int il4965_update_bcast_stations(struct il_priv *il)
{
	return il4965_update_bcast_station(il, &il->ctx);
}

/**
 * il4965_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
 */
int il4965_sta_tx_modify_enable_tid(struct il_priv *il, int sta_id, int tid)
{
	unsigned long flags;
	struct il_addsta_cmd sta_cmd;

	lockdep_assert_held(&il->mutex);

	/* Remove "disable" flag, to enable Tx for this TID */
	spin_lock_irqsave(&il->sta_lock, flags);
	il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
	il->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
	il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
	memcpy(&sta_cmd, &il->stations[sta_id].sta,
					sizeof(struct il_addsta_cmd));
	spin_unlock_irqrestore(&il->sta_lock, flags);

	return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
}

int il4965_sta_rx_agg_start(struct il_priv *il, struct ieee80211_sta *sta,
			 int tid, u16 ssn)
{
	unsigned long flags;
	int sta_id;
	struct il_addsta_cmd sta_cmd;

	lockdep_assert_held(&il->mutex);

	sta_id = il_sta_id(sta);
	if (sta_id == IL_INVALID_STATION)
		return -ENXIO;

	spin_lock_irqsave(&il->sta_lock, flags);
	il->stations[sta_id].sta.station_flags_msk = 0;
	il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
	il->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
	il->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
	il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
	memcpy(&sta_cmd, &il->stations[sta_id].sta,
					sizeof(struct il_addsta_cmd));
	spin_unlock_irqrestore(&il->sta_lock, flags);

	return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
}

int il4965_sta_rx_agg_stop(struct il_priv *il, struct ieee80211_sta *sta,
			int tid)
{
	unsigned long flags;
	int sta_id;
	struct il_addsta_cmd sta_cmd;

	lockdep_assert_held(&il->mutex);

	sta_id = il_sta_id(sta);
	if (sta_id == IL_INVALID_STATION) {
		IL_ERR("Invalid station for AGG tid %d\n", tid);
		return -ENXIO;
	}

	spin_lock_irqsave(&il->sta_lock, flags);
	il->stations[sta_id].sta.station_flags_msk = 0;
	il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
	il->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
	il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
	memcpy(&sta_cmd, &il->stations[sta_id].sta,
				sizeof(struct il_addsta_cmd));
	spin_unlock_irqrestore(&il->sta_lock, flags);

	return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
}

void
il4965_sta_modify_sleep_tx_count(struct il_priv *il, int sta_id, int cnt)
{
	unsigned long flags;

	spin_lock_irqsave(&il->sta_lock, flags);
	il->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
	il->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
	il->stations[sta_id].sta.sta.modify_mask =
					STA_MODIFY_SLEEP_TX_COUNT_MSK;
	il->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
	il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
	il_send_add_sta(il,
				&il->stations[sta_id].sta, CMD_ASYNC);
	spin_unlock_irqrestore(&il->sta_lock, flags);

}

S
Stanislaw Gruszka 已提交
766
void il4965_update_chain_flags(struct il_priv *il)
767
{
S
Stanislaw Gruszka 已提交
768
	if (il->cfg->ops->hcmd->set_rxon_chain) {
769 770 771
		il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
		if (il->ctx.active.rx_chain != il->ctx.staging.rx_chain)
			il_commit_rxon(il, &il->ctx);
772 773 774
	}
}

S
Stanislaw Gruszka 已提交
775
static void il4965_clear_free_frames(struct il_priv *il)
776 777 778
{
	struct list_head *element;

779
	D_INFO("%d frames on pre-allocated heap on clear.\n",
S
Stanislaw Gruszka 已提交
780
		       il->frames_count);
781

S
Stanislaw Gruszka 已提交
782 783
	while (!list_empty(&il->free_frames)) {
		element = il->free_frames.next;
784
		list_del(element);
S
Stanislaw Gruszka 已提交
785
		kfree(list_entry(element, struct il_frame, list));
S
Stanislaw Gruszka 已提交
786
		il->frames_count--;
787 788
	}

S
Stanislaw Gruszka 已提交
789
	if (il->frames_count) {
790
		IL_WARN("%d frames still in use.  Did we lose one?\n",
S
Stanislaw Gruszka 已提交
791 792
			    il->frames_count);
		il->frames_count = 0;
793 794 795
	}
}

S
Stanislaw Gruszka 已提交
796
static struct il_frame *il4965_get_free_frame(struct il_priv *il)
797
{
S
Stanislaw Gruszka 已提交
798
	struct il_frame *frame;
799
	struct list_head *element;
S
Stanislaw Gruszka 已提交
800
	if (list_empty(&il->free_frames)) {
801 802
		frame = kzalloc(sizeof(*frame), GFP_KERNEL);
		if (!frame) {
803
			IL_ERR("Could not allocate frame!\n");
804 805 806
			return NULL;
		}

S
Stanislaw Gruszka 已提交
807
		il->frames_count++;
808 809 810
		return frame;
	}

S
Stanislaw Gruszka 已提交
811
	element = il->free_frames.next;
812
	list_del(element);
S
Stanislaw Gruszka 已提交
813
	return list_entry(element, struct il_frame, list);
814 815
}

S
Stanislaw Gruszka 已提交
816
static void il4965_free_frame(struct il_priv *il, struct il_frame *frame)
817 818
{
	memset(frame, 0, sizeof(*frame));
S
Stanislaw Gruszka 已提交
819
	list_add(&frame->list, &il->free_frames);
820 821
}

S
Stanislaw Gruszka 已提交
822
static u32 il4965_fill_beacon_frame(struct il_priv *il,
823 824 825
				 struct ieee80211_hdr *hdr,
				 int left)
{
S
Stanislaw Gruszka 已提交
826
	lockdep_assert_held(&il->mutex);
827

S
Stanislaw Gruszka 已提交
828
	if (!il->beacon_skb)
829 830
		return 0;

S
Stanislaw Gruszka 已提交
831
	if (il->beacon_skb->len > left)
832 833
		return 0;

S
Stanislaw Gruszka 已提交
834
	memcpy(hdr, il->beacon_skb->data, il->beacon_skb->len);
835

S
Stanislaw Gruszka 已提交
836
	return il->beacon_skb->len;
837 838 839
}

/* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
S
Stanislaw Gruszka 已提交
840
static void il4965_set_beacon_tim(struct il_priv *il,
S
Stanislaw Gruszka 已提交
841
			       struct il_tx_beacon_cmd *tx_beacon_cmd,
842 843 844 845 846 847
			       u8 *beacon, u32 frame_size)
{
	u16 tim_idx;
	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;

	/*
S
Stanislaw Gruszka 已提交
848
	 * The idx is relative to frame start but we start looking at the
849 850 851 852 853 854 855 856 857 858 859 860 861 862
	 * variable-length part of the beacon.
	 */
	tim_idx = mgmt->u.beacon.variable - beacon;

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

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

S
Stanislaw Gruszka 已提交
866
static unsigned int il4965_hw_get_beacon_cmd(struct il_priv *il,
S
Stanislaw Gruszka 已提交
867
				       struct il_frame *frame)
868
{
S
Stanislaw Gruszka 已提交
869
	struct il_tx_beacon_cmd *tx_beacon_cmd;
870 871 872 873 874 875 876 877
	u32 frame_size;
	u32 rate_flags;
	u32 rate;
	/*
	 * We have to set up the TX command, the TX Beacon command, and the
	 * beacon contents.
	 */

S
Stanislaw Gruszka 已提交
878
	lockdep_assert_held(&il->mutex);
879

S
Stanislaw Gruszka 已提交
880
	if (!il->beacon_ctx) {
881
		IL_ERR("trying to build beacon w/o beacon context!\n");
882 883 884 885 886 887 888 889
		return 0;
	}

	/* Initialize memory */
	tx_beacon_cmd = &frame->u.beacon;
	memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));

	/* Set up TX beacon contents */
S
Stanislaw Gruszka 已提交
890
	frame_size = il4965_fill_beacon_frame(il, tx_beacon_cmd->frame,
891 892 893 894 895 896 897 898
				sizeof(frame->u) - sizeof(*tx_beacon_cmd));
	if (WARN_ON_ONCE(frame_size > MAX_MPDU_SIZE))
		return 0;
	if (!frame_size)
		return 0;

	/* Set up TX command fields */
	tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
S
Stanislaw Gruszka 已提交
899
	tx_beacon_cmd->tx.sta_id = il->beacon_ctx->bcast_sta_id;
900 901 902 903 904
	tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
	tx_beacon_cmd->tx.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK |
		TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK;

	/* Set up TX beacon command fields */
S
Stanislaw Gruszka 已提交
905
	il4965_set_beacon_tim(il, tx_beacon_cmd, (u8 *)tx_beacon_cmd->frame,
906 907 908
			   frame_size);

	/* Set up packet rate and flags */
S
Stanislaw Gruszka 已提交
909 910 911 912
	rate = il_get_lowest_plcp(il, il->beacon_ctx);
	il->mgmt_tx_ant = il4965_toggle_tx_ant(il, il->mgmt_tx_ant,
					      il->hw_params.valid_tx_ant);
	rate_flags = il4965_ant_idx_to_flags(il->mgmt_tx_ant);
S
Stanislaw Gruszka 已提交
913
	if ((rate >= IL_FIRST_CCK_RATE) && (rate <= IL_LAST_CCK_RATE))
914
		rate_flags |= RATE_MCS_CCK_MSK;
S
Stanislaw Gruszka 已提交
915
	tx_beacon_cmd->tx.rate_n_flags = il4965_hw_set_rate_n_flags(rate,
916 917 918 919 920
			rate_flags);

	return sizeof(*tx_beacon_cmd) + frame_size;
}

S
Stanislaw Gruszka 已提交
921
int il4965_send_beacon_cmd(struct il_priv *il)
922
{
S
Stanislaw Gruszka 已提交
923
	struct il_frame *frame;
924 925 926
	unsigned int frame_size;
	int rc;

S
Stanislaw Gruszka 已提交
927
	frame = il4965_get_free_frame(il);
928
	if (!frame) {
929
		IL_ERR("Could not obtain free frame buffer for beacon "
930 931 932 933
			  "command.\n");
		return -ENOMEM;
	}

S
Stanislaw Gruszka 已提交
934
	frame_size = il4965_hw_get_beacon_cmd(il, frame);
935
	if (!frame_size) {
936
		IL_ERR("Error configuring the beacon command\n");
S
Stanislaw Gruszka 已提交
937
		il4965_free_frame(il, frame);
938 939 940
		return -EINVAL;
	}

S
Stanislaw Gruszka 已提交
941
	rc = il_send_cmd_pdu(il, REPLY_TX_BEACON, frame_size,
942 943
			      &frame->u.cmd[0]);

S
Stanislaw Gruszka 已提交
944
	il4965_free_frame(il, frame);
945 946 947 948

	return rc;
}

S
Stanislaw Gruszka 已提交
949
static inline dma_addr_t il4965_tfd_tb_get_addr(struct il_tfd *tfd, u8 idx)
950
{
S
Stanislaw Gruszka 已提交
951
	struct il_tfd_tb *tb = &tfd->tbs[idx];
952 953 954 955 956 957 958 959 960

	dma_addr_t addr = get_unaligned_le32(&tb->lo);
	if (sizeof(dma_addr_t) > sizeof(u32))
		addr |=
		((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;

	return addr;
}

S
Stanislaw Gruszka 已提交
961
static inline u16 il4965_tfd_tb_get_len(struct il_tfd *tfd, u8 idx)
962
{
S
Stanislaw Gruszka 已提交
963
	struct il_tfd_tb *tb = &tfd->tbs[idx];
964 965 966 967

	return le16_to_cpu(tb->hi_n_len) >> 4;
}

S
Stanislaw Gruszka 已提交
968
static inline void il4965_tfd_set_tb(struct il_tfd *tfd, u8 idx,
969 970
				  dma_addr_t addr, u16 len)
{
S
Stanislaw Gruszka 已提交
971
	struct il_tfd_tb *tb = &tfd->tbs[idx];
972 973 974 975 976 977 978 979 980 981 982
	u16 hi_n_len = len << 4;

	put_unaligned_le32(addr, &tb->lo);
	if (sizeof(dma_addr_t) > sizeof(u32))
		hi_n_len |= ((addr >> 16) >> 16) & 0xF;

	tb->hi_n_len = cpu_to_le16(hi_n_len);

	tfd->num_tbs = idx + 1;
}

S
Stanislaw Gruszka 已提交
983
static inline u8 il4965_tfd_get_num_tbs(struct il_tfd *tfd)
984 985 986 987 988
{
	return tfd->num_tbs & 0x1f;
}

/**
S
Stanislaw Gruszka 已提交
989
 * il4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
S
Stanislaw Gruszka 已提交
990
 * @il - driver ilate data
991 992
 * @txq - tx queue
 *
S
Stanislaw Gruszka 已提交
993
 * Does NOT advance any TFD circular buffer read/write idxes
994 995
 * Does NOT free the TFD itself (which is within circular buffer)
 */
S
Stanislaw Gruszka 已提交
996
void il4965_hw_txq_free_tfd(struct il_priv *il, struct il_tx_queue *txq)
997
{
S
Stanislaw Gruszka 已提交
998 999
	struct il_tfd *tfd_tmp = (struct il_tfd *)txq->tfds;
	struct il_tfd *tfd;
S
Stanislaw Gruszka 已提交
1000
	struct pci_dev *dev = il->pci_dev;
S
Stanislaw Gruszka 已提交
1001
	int idx = txq->q.read_ptr;
1002 1003 1004
	int i;
	int num_tbs;

S
Stanislaw Gruszka 已提交
1005
	tfd = &tfd_tmp[idx];
1006 1007

	/* Sanity check on number of chunks */
S
Stanislaw Gruszka 已提交
1008
	num_tbs = il4965_tfd_get_num_tbs(tfd);
1009

S
Stanislaw Gruszka 已提交
1010
	if (num_tbs >= IL_NUM_OF_TBS) {
1011
		IL_ERR("Too many chunks: %i\n", num_tbs);
1012 1013 1014 1015 1016 1017 1018
		/* @todo issue fatal error, it is quite serious situation */
		return;
	}

	/* Unmap tx_cmd */
	if (num_tbs)
		pci_unmap_single(dev,
S
Stanislaw Gruszka 已提交
1019 1020
				dma_unmap_addr(&txq->meta[idx], mapping),
				dma_unmap_len(&txq->meta[idx], len),
1021 1022 1023 1024
				PCI_DMA_BIDIRECTIONAL);

	/* Unmap chunks, if any. */
	for (i = 1; i < num_tbs; i++)
S
Stanislaw Gruszka 已提交
1025 1026
		pci_unmap_single(dev, il4965_tfd_tb_get_addr(tfd, i),
				il4965_tfd_tb_get_len(tfd, i),
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
				PCI_DMA_TODEVICE);

	/* free SKB */
	if (txq->txb) {
		struct sk_buff *skb;

		skb = txq->txb[txq->q.read_ptr].skb;

		/* can be called from irqs-disabled context */
		if (skb) {
			dev_kfree_skb_any(skb);
			txq->txb[txq->q.read_ptr].skb = NULL;
		}
	}
}

S
Stanislaw Gruszka 已提交
1043
int il4965_hw_txq_attach_buf_to_tfd(struct il_priv *il,
S
Stanislaw Gruszka 已提交
1044
				 struct il_tx_queue *txq,
1045 1046 1047
				 dma_addr_t addr, u16 len,
				 u8 reset, u8 pad)
{
S
Stanislaw Gruszka 已提交
1048 1049
	struct il_queue *q;
	struct il_tfd *tfd, *tfd_tmp;
1050 1051 1052
	u32 num_tbs;

	q = &txq->q;
S
Stanislaw Gruszka 已提交
1053
	tfd_tmp = (struct il_tfd *)txq->tfds;
1054 1055 1056 1057 1058
	tfd = &tfd_tmp[q->write_ptr];

	if (reset)
		memset(tfd, 0, sizeof(*tfd));

S
Stanislaw Gruszka 已提交
1059
	num_tbs = il4965_tfd_get_num_tbs(tfd);
1060 1061

	/* Each TFD can point to a maximum 20 Tx buffers */
S
Stanislaw Gruszka 已提交
1062
	if (num_tbs >= IL_NUM_OF_TBS) {
1063
		IL_ERR("Error can not send more than %d chunks\n",
S
Stanislaw Gruszka 已提交
1064
			  IL_NUM_OF_TBS);
1065 1066 1067 1068
		return -EINVAL;
	}

	BUG_ON(addr & ~DMA_BIT_MASK(36));
S
Stanislaw Gruszka 已提交
1069
	if (unlikely(addr & ~IL_TX_DMA_MASK))
1070
		IL_ERR("Unaligned address = %llx\n",
1071 1072
			  (unsigned long long)addr);

S
Stanislaw Gruszka 已提交
1073
	il4965_tfd_set_tb(tfd, num_tbs, addr, len);
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084

	return 0;
}

/*
 * Tell nic where to find circular buffer of Tx Frame Descriptors for
 * given Tx queue, and enable the DMA channel used for that queue.
 *
 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
 * channels supported in hardware.
 */
S
Stanislaw Gruszka 已提交
1085
int il4965_hw_tx_queue_init(struct il_priv *il,
S
Stanislaw Gruszka 已提交
1086
			 struct il_tx_queue *txq)
1087 1088 1089 1090
{
	int txq_id = txq->q.id;

	/* Circular buffer (TFD queue in DRAM) physical base address */
1091
	il_wr(il, FH_MEM_CBBC_QUEUE(txq_id),
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
			     txq->q.dma_addr >> 8);

	return 0;
}

/******************************************************************************
 *
 * Generic RX handler implementations
 *
 ******************************************************************************/
S
Stanislaw Gruszka 已提交
1102
static void il4965_rx_reply_alive(struct il_priv *il,
1103
				struct il_rx_buf *rxb)
1104
{
1105
	struct il_rx_pkt *pkt = rxb_addr(rxb);
S
Stanislaw Gruszka 已提交
1106
	struct il_alive_resp *palive;
1107 1108 1109 1110
	struct delayed_work *pwork;

	palive = &pkt->u.alive_frame;

1111
	D_INFO("Alive ucode status 0x%08X revision "
1112 1113 1114 1115 1116
		       "0x%01X 0x%01X\n",
		       palive->is_valid, palive->ver_type,
		       palive->ver_subtype);

	if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
1117
		D_INFO("Initialization Alive received.\n");
S
Stanislaw Gruszka 已提交
1118
		memcpy(&il->card_alive_init,
1119
		       &pkt->u.alive_frame,
S
Stanislaw Gruszka 已提交
1120
		       sizeof(struct il_init_alive_resp));
S
Stanislaw Gruszka 已提交
1121
		pwork = &il->init_alive_start;
1122
	} else {
1123
		D_INFO("Runtime Alive received.\n");
S
Stanislaw Gruszka 已提交
1124
		memcpy(&il->card_alive, &pkt->u.alive_frame,
S
Stanislaw Gruszka 已提交
1125
		       sizeof(struct il_alive_resp));
S
Stanislaw Gruszka 已提交
1126
		pwork = &il->alive_start;
1127 1128 1129 1130 1131
	}

	/* We delay the ALIVE response by 5ms to
	 * give the HW RF Kill time to activate... */
	if (palive->is_valid == UCODE_VALID_OK)
S
Stanislaw Gruszka 已提交
1132
		queue_delayed_work(il->workqueue, pwork,
1133 1134
				   msecs_to_jiffies(5));
	else
1135
		IL_WARN("uCode did not respond OK.\n");
1136 1137 1138
}

/**
S
Stanislaw Gruszka 已提交
1139
 * il4965_bg_stats_periodic - Timer callback to queue stats
1140
 *
S
Stanislaw Gruszka 已提交
1141
 * This callback is provided in order to send a stats request.
1142 1143 1144
 *
 * This timer function is continually reset to execute within
 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
S
Stanislaw Gruszka 已提交
1145
 * was received.  We need to ensure we receive the stats in order
1146 1147
 * to update the temperature used for calibrating the TXPOWER.
 */
S
Stanislaw Gruszka 已提交
1148
static void il4965_bg_stats_periodic(unsigned long data)
1149
{
S
Stanislaw Gruszka 已提交
1150
	struct il_priv *il = (struct il_priv *)data;
1151

S
Stanislaw Gruszka 已提交
1152
	if (test_bit(STATUS_EXIT_PENDING, &il->status))
1153 1154 1155
		return;

	/* dont send host command if rf-kill is on */
S
Stanislaw Gruszka 已提交
1156
	if (!il_is_ready_rf(il))
1157 1158
		return;

S
Stanislaw Gruszka 已提交
1159
	il_send_stats_request(il, CMD_ASYNC, false);
1160 1161
}

S
Stanislaw Gruszka 已提交
1162
static void il4965_rx_beacon_notif(struct il_priv *il,
1163
				struct il_rx_buf *rxb)
1164
{
1165
	struct il_rx_pkt *pkt = rxb_addr(rxb);
S
Stanislaw Gruszka 已提交
1166 1167
	struct il4965_beacon_notif *beacon =
		(struct il4965_beacon_notif *)pkt->u.raw;
1168
#ifdef CONFIG_IWLEGACY_DEBUG
S
Stanislaw Gruszka 已提交
1169
	u8 rate = il4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
1170

1171
	D_RX("beacon status %x retries %d iss %d "
1172 1173 1174 1175 1176 1177 1178 1179
		"tsf %d %d rate %d\n",
		le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
		beacon->beacon_notify_hdr.failure_frame,
		le32_to_cpu(beacon->ibss_mgr_status),
		le32_to_cpu(beacon->high_tsf),
		le32_to_cpu(beacon->low_tsf), rate);
#endif

S
Stanislaw Gruszka 已提交
1180
	il->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
1181 1182
}

S
Stanislaw Gruszka 已提交
1183
static void il4965_perform_ct_kill_task(struct il_priv *il)
1184 1185 1186
{
	unsigned long flags;

1187
	D_POWER("Stop all queues\n");
1188

S
Stanislaw Gruszka 已提交
1189 1190
	if (il->mac80211_registered)
		ieee80211_stop_queues(il->hw);
1191

1192
	_il_wr(il, CSR_UCODE_DRV_GP1_SET,
1193
			CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
1194
	_il_rd(il, CSR_UCODE_DRV_GP1);
1195

S
Stanislaw Gruszka 已提交
1196
	spin_lock_irqsave(&il->reg_lock, flags);
1197 1198
	if (!_il_grab_nic_access(il))
		_il_release_nic_access(il);
S
Stanislaw Gruszka 已提交
1199
	spin_unlock_irqrestore(&il->reg_lock, flags);
1200 1201 1202 1203
}

/* Handle notification from uCode that card's power state is changing
 * due to software, hardware, or critical temperature RFKILL */
S
Stanislaw Gruszka 已提交
1204
static void il4965_rx_card_state_notif(struct il_priv *il,
1205
				    struct il_rx_buf *rxb)
1206
{
1207
	struct il_rx_pkt *pkt = rxb_addr(rxb);
1208
	u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
S
Stanislaw Gruszka 已提交
1209
	unsigned long status = il->status;
1210

1211
	D_RF_KILL("Card state received: HW:%s SW:%s CT:%s\n",
1212 1213 1214 1215 1216 1217 1218 1219
			  (flags & HW_CARD_DISABLED) ? "Kill" : "On",
			  (flags & SW_CARD_DISABLED) ? "Kill" : "On",
			  (flags & CT_CARD_DISABLED) ?
			  "Reached" : "Not reached");

	if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
		     CT_CARD_DISABLED)) {

1220
		_il_wr(il, CSR_UCODE_DRV_GP1_SET,
1221 1222
			    CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);

1223
		il_wr(il, HBUS_TARG_MBX_C,
1224 1225 1226
					HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);

		if (!(flags & RXON_CARD_DISABLED)) {
1227
			_il_wr(il, CSR_UCODE_DRV_GP1_CLR,
1228
				    CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1229
			il_wr(il, HBUS_TARG_MBX_C,
1230 1231 1232 1233 1234
					HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
		}
	}

	if (flags & CT_CARD_DISABLED)
S
Stanislaw Gruszka 已提交
1235
		il4965_perform_ct_kill_task(il);
1236 1237

	if (flags & HW_CARD_DISABLED)
S
Stanislaw Gruszka 已提交
1238
		set_bit(STATUS_RF_KILL_HW, &il->status);
1239
	else
S
Stanislaw Gruszka 已提交
1240
		clear_bit(STATUS_RF_KILL_HW, &il->status);
1241 1242

	if (!(flags & RXON_CARD_DISABLED))
S
Stanislaw Gruszka 已提交
1243
		il_scan_cancel(il);
1244 1245

	if ((test_bit(STATUS_RF_KILL_HW, &status) !=
S
Stanislaw Gruszka 已提交
1246 1247 1248
	     test_bit(STATUS_RF_KILL_HW, &il->status)))
		wiphy_rfkill_set_hw_state(il->hw->wiphy,
			test_bit(STATUS_RF_KILL_HW, &il->status));
1249
	else
S
Stanislaw Gruszka 已提交
1250
		wake_up(&il->wait_command_queue);
1251 1252 1253
}

/**
S
Stanislaw Gruszka 已提交
1254
 * il4965_setup_rx_handlers - Initialize Rx handler callbacks
1255 1256 1257 1258 1259 1260 1261
 *
 * Setup the RX handlers for each of the reply types sent from the uCode
 * to the host.
 *
 * This function chains into the hardware specific files for them to setup
 * any hardware specific handlers as well.
 */
S
Stanislaw Gruszka 已提交
1262
static void il4965_setup_rx_handlers(struct il_priv *il)
1263
{
S
Stanislaw Gruszka 已提交
1264 1265 1266 1267
	il->rx_handlers[REPLY_ALIVE] = il4965_rx_reply_alive;
	il->rx_handlers[REPLY_ERROR] = il_rx_reply_error;
	il->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = il_rx_csa;
	il->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
S
Stanislaw Gruszka 已提交
1268
			il_rx_spectrum_measure_notif;
S
Stanislaw Gruszka 已提交
1269 1270
	il->rx_handlers[PM_SLEEP_NOTIFICATION] = il_rx_pm_sleep_notif;
	il->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
S
Stanislaw Gruszka 已提交
1271
	    il_rx_pm_debug_stats_notif;
S
Stanislaw Gruszka 已提交
1272
	il->rx_handlers[BEACON_NOTIFICATION] = il4965_rx_beacon_notif;
1273 1274 1275

	/*
	 * The same handler is used for both the REPLY to a discrete
S
Stanislaw Gruszka 已提交
1276 1277
	 * stats request from the host as well as for the periodic
	 * stats notifications (after received beacons) from the uCode.
1278
	 */
S
Stanislaw Gruszka 已提交
1279 1280
	il->rx_handlers[REPLY_STATISTICS_CMD] = il4965_reply_stats;
	il->rx_handlers[STATISTICS_NOTIFICATION] = il4965_rx_stats;
1281

S
Stanislaw Gruszka 已提交
1282
	il_setup_rx_scan_handlers(il);
1283 1284

	/* status change handler */
S
Stanislaw Gruszka 已提交
1285
	il->rx_handlers[CARD_STATE_NOTIFICATION] =
S
Stanislaw Gruszka 已提交
1286
					il4965_rx_card_state_notif;
1287

S
Stanislaw Gruszka 已提交
1288
	il->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
S
Stanislaw Gruszka 已提交
1289
	    il4965_rx_missed_beacon_notif;
1290
	/* Rx handlers */
S
Stanislaw Gruszka 已提交
1291 1292
	il->rx_handlers[REPLY_RX_PHY_CMD] = il4965_rx_reply_rx_phy;
	il->rx_handlers[REPLY_RX_MPDU_CMD] = il4965_rx_reply_rx;
1293
	/* block ack */
S
Stanislaw Gruszka 已提交
1294
	il->rx_handlers[REPLY_COMPRESSED_BA] = il4965_rx_reply_compressed_ba;
1295
	/* Set up hardware specific Rx handlers */
S
Stanislaw Gruszka 已提交
1296
	il->cfg->ops->lib->rx_handler_setup(il);
1297 1298 1299
}

/**
S
Stanislaw Gruszka 已提交
1300
 * il4965_rx_handle - Main entry function for receiving responses from uCode
1301
 *
S
Stanislaw Gruszka 已提交
1302
 * Uses the il->rx_handlers callback function array to invoke
1303 1304 1305
 * the appropriate handlers, including command responses,
 * frame-received notifications, and other notifications.
 */
S
Stanislaw Gruszka 已提交
1306
void il4965_rx_handle(struct il_priv *il)
1307
{
1308
	struct il_rx_buf *rxb;
1309
	struct il_rx_pkt *pkt;
S
Stanislaw Gruszka 已提交
1310
	struct il_rx_queue *rxq = &il->rxq;
1311 1312 1313 1314 1315 1316 1317
	u32 r, i;
	int reclaim;
	unsigned long flags;
	u8 fill_rx = 0;
	u32 count = 8;
	int total_empty;

S
Stanislaw Gruszka 已提交
1318
	/* uCode's read idx (stored in shared DRAM) indicates the last Rx
1319 1320 1321 1322 1323 1324
	 * buffer that the driver may process (last buffer filled by ucode). */
	r = le16_to_cpu(rxq->rb_stts->closed_rb_num) &  0x0FFF;
	i = rxq->read;

	/* Rx interrupt, but nothing sent from uCode */
	if (i == r)
1325
		D_RX("r = %d, i = %d\n", r, i);
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346

	/* calculate total frames need to be restock after handling RX */
	total_empty = r - rxq->write_actual;
	if (total_empty < 0)
		total_empty += RX_QUEUE_SIZE;

	if (total_empty > (RX_QUEUE_SIZE / 2))
		fill_rx = 1;

	while (i != r) {
		int len;

		rxb = rxq->queue[i];

		/* If an RXB doesn't have a Rx queue slot associated with it,
		 * then a bug has been introduced in the queue refilling
		 * routines -- catch it here */
		BUG_ON(rxb == NULL);

		rxq->queue[i] = NULL;

S
Stanislaw Gruszka 已提交
1347 1348
		pci_unmap_page(il->pci_dev, rxb->page_dma,
			       PAGE_SIZE << il->hw_params.rx_page_order,
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
			       PCI_DMA_FROMDEVICE);
		pkt = rxb_addr(rxb);

		len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
		len += sizeof(u32); /* account for status word */

		/* Reclaim a command buffer only if this packet is a response
		 *   to a (driver-originated) command.
		 * If the packet (e.g. Rx frame) originated from uCode,
		 *   there is no command buffer to reclaim.
		 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
		 *   but apparently a few don't get set; catch them here. */
		reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
			(pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
			(pkt->hdr.cmd != REPLY_RX) &&
			(pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
			(pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
			(pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
			(pkt->hdr.cmd != REPLY_TX);

		/* Based on type of command response or notification,
		 *   handle those that need handling via function in
S
Stanislaw Gruszka 已提交
1371
		 *   rx_handlers table.  See il4965_setup_rx_handlers() */
S
Stanislaw Gruszka 已提交
1372
		if (il->rx_handlers[pkt->hdr.cmd]) {
1373
			D_RX("r = %d, i = %d, %s, 0x%02x\n", r,
S
Stanislaw Gruszka 已提交
1374
				i, il_get_cmd_string(pkt->hdr.cmd),
1375
				pkt->hdr.cmd);
S
Stanislaw Gruszka 已提交
1376 1377
			il->isr_stats.rx_handlers[pkt->hdr.cmd]++;
			il->rx_handlers[pkt->hdr.cmd] (il, rxb);
1378 1379
		} else {
			/* No handling needed */
1380
			D_RX(
1381
				"r %d i %d No handler needed for %s, 0x%02x\n",
S
Stanislaw Gruszka 已提交
1382
				r, i, il_get_cmd_string(pkt->hdr.cmd),
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
				pkt->hdr.cmd);
		}

		/*
		 * XXX: After here, we should always check rxb->page
		 * against NULL before touching it or its virtual
		 * memory (pkt). Because some rx_handler might have
		 * already taken or freed the pages.
		 */

		if (reclaim) {
			/* Invoke any callbacks, transfer the buffer to caller,
S
Stanislaw Gruszka 已提交
1395
			 * and fire off the (possibly) blocking il_send_cmd()
1396 1397
			 * as we reclaim the driver command queue */
			if (rxb->page)
S
Stanislaw Gruszka 已提交
1398
				il_tx_cmd_complete(il, rxb);
1399
			else
1400
				IL_WARN("Claim null rxb?\n");
1401 1402 1403 1404 1405 1406 1407
		}

		/* Reuse the page if possible. For notification packets and
		 * SKBs that fail to Rx correctly, add them back into the
		 * rx_free list for reuse later. */
		spin_lock_irqsave(&rxq->lock, flags);
		if (rxb->page != NULL) {
S
Stanislaw Gruszka 已提交
1408 1409
			rxb->page_dma = pci_map_page(il->pci_dev, rxb->page,
				0, PAGE_SIZE << il->hw_params.rx_page_order,
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
				PCI_DMA_FROMDEVICE);
			list_add_tail(&rxb->list, &rxq->rx_free);
			rxq->free_count++;
		} else
			list_add_tail(&rxb->list, &rxq->rx_used);

		spin_unlock_irqrestore(&rxq->lock, flags);

		i = (i + 1) & RX_QUEUE_MASK;
		/* If there are a lot of unused frames,
		 * restock the Rx queue so ucode wont assert. */
		if (fill_rx) {
			count++;
			if (count >= 8) {
				rxq->read = i;
S
Stanislaw Gruszka 已提交
1425
				il4965_rx_replenish_now(il);
1426 1427 1428 1429 1430 1431 1432 1433
				count = 0;
			}
		}
	}

	/* Backtrack one entry */
	rxq->read = i;
	if (fill_rx)
S
Stanislaw Gruszka 已提交
1434
		il4965_rx_replenish_now(il);
1435
	else
S
Stanislaw Gruszka 已提交
1436
		il4965_rx_queue_restock(il);
1437 1438 1439
}

/* call this function to flush any scheduled tasklet */
S
Stanislaw Gruszka 已提交
1440
static inline void il4965_synchronize_irq(struct il_priv *il)
1441 1442
{
	/* wait to make sure we flush pending tasklet*/
S
Stanislaw Gruszka 已提交
1443 1444
	synchronize_irq(il->pci_dev->irq);
	tasklet_kill(&il->irq_tasklet);
1445 1446
}

S
Stanislaw Gruszka 已提交
1447
static void il4965_irq_tasklet(struct il_priv *il)
1448 1449 1450 1451 1452
{
	u32 inta, handled = 0;
	u32 inta_fh;
	unsigned long flags;
	u32 i;
1453
#ifdef CONFIG_IWLEGACY_DEBUG
1454 1455 1456
	u32 inta_mask;
#endif

S
Stanislaw Gruszka 已提交
1457
	spin_lock_irqsave(&il->lock, flags);
1458 1459 1460 1461

	/* Ack/clear/reset pending uCode interrupts.
	 * Note:  Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
	 *  and will clear only when CSR_FH_INT_STATUS gets cleared. */
1462 1463
	inta = _il_rd(il, CSR_INT);
	_il_wr(il, CSR_INT, inta);
1464 1465 1466 1467

	/* Ack/clear/reset pending flow-handler (DMA) interrupts.
	 * Any new interrupts that happen after this, either while we're
	 * in this tasklet, or later, will show up in next ISR/tasklet. */
1468 1469
	inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
	_il_wr(il, CSR_FH_INT_STATUS, inta_fh);
1470

1471
#ifdef CONFIG_IWLEGACY_DEBUG
S
Stanislaw Gruszka 已提交
1472
	if (il_get_debug_level(il) & IL_DL_ISR) {
1473
		/* just for debug */
1474
		inta_mask = _il_rd(il, CSR_INT_MASK);
1475
		D_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1476 1477 1478 1479
			      inta, inta_mask, inta_fh);
	}
#endif

S
Stanislaw Gruszka 已提交
1480
	spin_unlock_irqrestore(&il->lock, flags);
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492

	/* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
	 * atomic, make sure that inta covers all the interrupts that
	 * we've discovered, even if FH interrupt came in just after
	 * reading CSR_INT. */
	if (inta_fh & CSR49_FH_INT_RX_MASK)
		inta |= CSR_INT_BIT_FH_RX;
	if (inta_fh & CSR49_FH_INT_TX_MASK)
		inta |= CSR_INT_BIT_FH_TX;

	/* Now service all interrupt bits discovered above. */
	if (inta & CSR_INT_BIT_HW_ERR) {
1493
		IL_ERR("Hardware error detected.  Restarting.\n");
1494 1495

		/* Tell the device to stop sending interrupts */
S
Stanislaw Gruszka 已提交
1496
		il_disable_interrupts(il);
1497

S
Stanislaw Gruszka 已提交
1498 1499
		il->isr_stats.hw++;
		il_irq_handle_error(il);
1500 1501 1502 1503 1504 1505

		handled |= CSR_INT_BIT_HW_ERR;

		return;
	}

1506
#ifdef CONFIG_IWLEGACY_DEBUG
S
Stanislaw Gruszka 已提交
1507
	if (il_get_debug_level(il) & (IL_DL_ISR)) {
1508 1509
		/* NIC fires this, but we don't use it, redundant with WAKEUP */
		if (inta & CSR_INT_BIT_SCD) {
1510
			D_ISR("Scheduler finished to transmit "
1511
				      "the frame/frames.\n");
S
Stanislaw Gruszka 已提交
1512
			il->isr_stats.sch++;
1513 1514 1515 1516
		}

		/* Alive notification via Rx interrupt will do the real work */
		if (inta & CSR_INT_BIT_ALIVE) {
1517
			D_ISR("Alive interrupt\n");
S
Stanislaw Gruszka 已提交
1518
			il->isr_stats.alive++;
1519 1520 1521 1522 1523 1524 1525 1526 1527
		}
	}
#endif
	/* Safely ignore these bits for debug checks below */
	inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);

	/* HW RF KILL switch toggled */
	if (inta & CSR_INT_BIT_RF_KILL) {
		int hw_rf_kill = 0;
1528
		if (!(_il_rd(il, CSR_GP_CNTRL) &
1529 1530 1531
				CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
			hw_rf_kill = 1;

1532
		IL_WARN("RF_KILL bit toggled to %s.\n",
1533 1534
				hw_rf_kill ? "disable radio" : "enable radio");

S
Stanislaw Gruszka 已提交
1535
		il->isr_stats.rfkill++;
1536 1537 1538 1539 1540 1541

		/* driver only loads ucode once setting the interface up.
		 * the driver allows loading the ucode even if the radio
		 * is killed. Hence update the killswitch state here. The
		 * rfkill handler will care about restarting if needed.
		 */
S
Stanislaw Gruszka 已提交
1542
		if (!test_bit(STATUS_ALIVE, &il->status)) {
1543
			if (hw_rf_kill)
S
Stanislaw Gruszka 已提交
1544
				set_bit(STATUS_RF_KILL_HW, &il->status);
1545
			else
S
Stanislaw Gruszka 已提交
1546 1547
				clear_bit(STATUS_RF_KILL_HW, &il->status);
			wiphy_rfkill_set_hw_state(il->hw->wiphy, hw_rf_kill);
1548 1549 1550 1551 1552 1553 1554
		}

		handled |= CSR_INT_BIT_RF_KILL;
	}

	/* Chip got too hot and stopped itself */
	if (inta & CSR_INT_BIT_CT_KILL) {
1555
		IL_ERR("Microcode CT kill error detected.\n");
S
Stanislaw Gruszka 已提交
1556
		il->isr_stats.ctkill++;
1557 1558 1559 1560 1561
		handled |= CSR_INT_BIT_CT_KILL;
	}

	/* Error detected by uCode */
	if (inta & CSR_INT_BIT_SW_ERR) {
1562
		IL_ERR("Microcode SW error detected. "
1563
			" Restarting 0x%X.\n", inta);
S
Stanislaw Gruszka 已提交
1564 1565
		il->isr_stats.sw++;
		il_irq_handle_error(il);
1566 1567 1568 1569 1570 1571 1572 1573 1574
		handled |= CSR_INT_BIT_SW_ERR;
	}

	/*
	 * uCode wakes up after power-down sleep.
	 * Tell device about any new tx or host commands enqueued,
	 * and about any Rx buffers made available while asleep.
	 */
	if (inta & CSR_INT_BIT_WAKEUP) {
1575
		D_ISR("Wakeup interrupt\n");
S
Stanislaw Gruszka 已提交
1576 1577 1578 1579
		il_rx_queue_update_write_ptr(il, &il->rxq);
		for (i = 0; i < il->hw_params.max_txq_num; i++)
			il_txq_update_write_ptr(il, &il->txq[i]);
		il->isr_stats.wakeup++;
1580 1581 1582 1583 1584 1585 1586
		handled |= CSR_INT_BIT_WAKEUP;
	}

	/* All uCode command responses, including Tx command responses,
	 * Rx "responses" (frame-received notification), and other
	 * notifications from uCode come through here*/
	if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
S
Stanislaw Gruszka 已提交
1587 1588
		il4965_rx_handle(il);
		il->isr_stats.rx++;
1589 1590 1591 1592 1593
		handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
	}

	/* This "Tx" DMA channel is used only for loading uCode */
	if (inta & CSR_INT_BIT_FH_TX) {
1594
		D_ISR("uCode load interrupt\n");
S
Stanislaw Gruszka 已提交
1595
		il->isr_stats.tx++;
1596 1597
		handled |= CSR_INT_BIT_FH_TX;
		/* Wake up uCode load routine, now that load is complete */
S
Stanislaw Gruszka 已提交
1598 1599
		il->ucode_write_complete = 1;
		wake_up(&il->wait_command_queue);
1600 1601 1602
	}

	if (inta & ~handled) {
1603
		IL_ERR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
S
Stanislaw Gruszka 已提交
1604
		il->isr_stats.unhandled++;
1605 1606
	}

S
Stanislaw Gruszka 已提交
1607
	if (inta & ~(il->inta_mask)) {
1608
		IL_WARN("Disabled INTA bits 0x%08x were pending\n",
S
Stanislaw Gruszka 已提交
1609
			 inta & ~il->inta_mask);
1610
		IL_WARN("   with FH_INT = 0x%08x\n", inta_fh);
1611 1612 1613
	}

	/* Re-enable all interrupts */
1614
	/* only Re-enable if disabled by irq */
S
Stanislaw Gruszka 已提交
1615 1616
	if (test_bit(STATUS_INT_ENABLED, &il->status))
		il_enable_interrupts(il);
1617 1618
	/* Re-enable RF_KILL if it occurred */
	else if (handled & CSR_INT_BIT_RF_KILL)
S
Stanislaw Gruszka 已提交
1619
		il_enable_rfkill_int(il);
1620

1621
#ifdef CONFIG_IWLEGACY_DEBUG
S
Stanislaw Gruszka 已提交
1622
	if (il_get_debug_level(il) & (IL_DL_ISR)) {
1623 1624 1625
		inta = _il_rd(il, CSR_INT);
		inta_mask = _il_rd(il, CSR_INT_MASK);
		inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
1626
		D_ISR(
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
			"End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
			"flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
	}
#endif
}

/*****************************************************************************
 *
 * sysfs attributes
 *
 *****************************************************************************/

1639
#ifdef CONFIG_IWLEGACY_DEBUG
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651

/*
 * The following adds a new attribute to the sysfs representation
 * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
 * used for controlling the debug level.
 *
 * See the level definitions in iwl for details.
 *
 * The debug_level being managed using sysfs below is a per device debug
 * level that is used instead of the global debug level if it (the per
 * device debug level) is set.
 */
S
Stanislaw Gruszka 已提交
1652
static ssize_t il4965_show_debug_level(struct device *d,
1653 1654
				struct device_attribute *attr, char *buf)
{
S
Stanislaw Gruszka 已提交
1655 1656
	struct il_priv *il = dev_get_drvdata(d);
	return sprintf(buf, "0x%08X\n", il_get_debug_level(il));
1657
}
S
Stanislaw Gruszka 已提交
1658
static ssize_t il4965_store_debug_level(struct device *d,
1659 1660 1661
				struct device_attribute *attr,
				 const char *buf, size_t count)
{
S
Stanislaw Gruszka 已提交
1662
	struct il_priv *il = dev_get_drvdata(d);
1663 1664 1665 1666 1667
	unsigned long val;
	int ret;

	ret = strict_strtoul(buf, 0, &val);
	if (ret)
1668
		IL_ERR("%s is not in hex or decimal form.\n", buf);
1669
	else {
S
Stanislaw Gruszka 已提交
1670 1671
		il->debug_level = val;
		if (il_alloc_traffic_mem(il))
1672
			IL_ERR(
1673 1674 1675 1676 1677 1678
				"Not enough memory to generate traffic log\n");
	}
	return strnlen(buf, count);
}

static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
S
Stanislaw Gruszka 已提交
1679
			il4965_show_debug_level, il4965_store_debug_level);
1680 1681


1682
#endif /* CONFIG_IWLEGACY_DEBUG */
1683 1684


S
Stanislaw Gruszka 已提交
1685
static ssize_t il4965_show_temperature(struct device *d,
1686 1687
				struct device_attribute *attr, char *buf)
{
S
Stanislaw Gruszka 已提交
1688
	struct il_priv *il = dev_get_drvdata(d);
1689

S
Stanislaw Gruszka 已提交
1690
	if (!il_is_alive(il))
1691 1692
		return -EAGAIN;

S
Stanislaw Gruszka 已提交
1693
	return sprintf(buf, "%d\n", il->temperature);
1694 1695
}

S
Stanislaw Gruszka 已提交
1696
static DEVICE_ATTR(temperature, S_IRUGO, il4965_show_temperature, NULL);
1697

S
Stanislaw Gruszka 已提交
1698
static ssize_t il4965_show_tx_power(struct device *d,
1699 1700
			     struct device_attribute *attr, char *buf)
{
S
Stanislaw Gruszka 已提交
1701
	struct il_priv *il = dev_get_drvdata(d);
1702

S
Stanislaw Gruszka 已提交
1703
	if (!il_is_ready_rf(il))
1704 1705
		return sprintf(buf, "off\n");
	else
S
Stanislaw Gruszka 已提交
1706
		return sprintf(buf, "%d\n", il->tx_power_user_lmt);
1707 1708
}

S
Stanislaw Gruszka 已提交
1709
static ssize_t il4965_store_tx_power(struct device *d,
1710 1711 1712
			      struct device_attribute *attr,
			      const char *buf, size_t count)
{
S
Stanislaw Gruszka 已提交
1713
	struct il_priv *il = dev_get_drvdata(d);
1714 1715 1716 1717 1718
	unsigned long val;
	int ret;

	ret = strict_strtoul(buf, 10, &val);
	if (ret)
1719
		IL_INFO("%s is not in decimal form.\n", buf);
1720
	else {
S
Stanislaw Gruszka 已提交
1721
		ret = il_set_tx_power(il, val, false);
1722
		if (ret)
1723
			IL_ERR("failed setting tx power (0x%d).\n",
1724 1725 1726 1727 1728 1729 1730 1731
				ret);
		else
			ret = count;
	}
	return ret;
}

static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO,
S
Stanislaw Gruszka 已提交
1732
			il4965_show_tx_power, il4965_store_tx_power);
1733

S
Stanislaw Gruszka 已提交
1734
static struct attribute *il_sysfs_entries[] = {
1735 1736
	&dev_attr_temperature.attr,
	&dev_attr_tx_power.attr,
1737
#ifdef CONFIG_IWLEGACY_DEBUG
1738 1739 1740 1741 1742
	&dev_attr_debug_level.attr,
#endif
	NULL
};

S
Stanislaw Gruszka 已提交
1743
static struct attribute_group il_attribute_group = {
1744
	.name = NULL,		/* put in device directory */
S
Stanislaw Gruszka 已提交
1745
	.attrs = il_sysfs_entries,
1746 1747 1748 1749 1750 1751 1752 1753
};

/******************************************************************************
 *
 * uCode download functions
 *
 ******************************************************************************/

S
Stanislaw Gruszka 已提交
1754
static void il4965_dealloc_ucode_pci(struct il_priv *il)
1755
{
S
Stanislaw Gruszka 已提交
1756 1757 1758 1759 1760 1761
	il_free_fw_desc(il->pci_dev, &il->ucode_code);
	il_free_fw_desc(il->pci_dev, &il->ucode_data);
	il_free_fw_desc(il->pci_dev, &il->ucode_data_backup);
	il_free_fw_desc(il->pci_dev, &il->ucode_init);
	il_free_fw_desc(il->pci_dev, &il->ucode_init_data);
	il_free_fw_desc(il->pci_dev, &il->ucode_boot);
1762 1763
}

S
Stanislaw Gruszka 已提交
1764
static void il4965_nic_start(struct il_priv *il)
1765 1766
{
	/* Remove all resets to allow NIC to operate */
1767
	_il_wr(il, CSR_RESET, 0);
1768 1769
}

S
Stanislaw Gruszka 已提交
1770
static void il4965_ucode_callback(const struct firmware *ucode_raw,
1771
					void *context);
S
Stanislaw Gruszka 已提交
1772
static int il4965_mac_setup_register(struct il_priv *il,
1773 1774
						u32 max_probe_length);

S
Stanislaw Gruszka 已提交
1775
static int __must_check il4965_request_firmware(struct il_priv *il, bool first)
1776
{
S
Stanislaw Gruszka 已提交
1777
	const char *name_pre = il->cfg->fw_name_pre;
1778 1779 1780
	char tag[8];

	if (first) {
S
Stanislaw Gruszka 已提交
1781 1782
		il->fw_idx = il->cfg->ucode_api_max;
		sprintf(tag, "%d", il->fw_idx);
1783
	} else {
S
Stanislaw Gruszka 已提交
1784 1785
		il->fw_idx--;
		sprintf(tag, "%d", il->fw_idx);
1786 1787
	}

S
Stanislaw Gruszka 已提交
1788
	if (il->fw_idx < il->cfg->ucode_api_min) {
1789
		IL_ERR("no suitable firmware found!\n");
1790 1791 1792
		return -ENOENT;
	}

S
Stanislaw Gruszka 已提交
1793
	sprintf(il->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
1794

1795
	D_INFO("attempting to load firmware '%s'\n",
S
Stanislaw Gruszka 已提交
1796
		       il->firmware_name);
1797

S
Stanislaw Gruszka 已提交
1798 1799
	return request_firmware_nowait(THIS_MODULE, 1, il->firmware_name,
				       &il->pci_dev->dev, GFP_KERNEL, il,
S
Stanislaw Gruszka 已提交
1800
				       il4965_ucode_callback);
1801 1802
}

S
Stanislaw Gruszka 已提交
1803
struct il4965_firmware_pieces {
1804 1805 1806 1807
	const void *inst, *data, *init, *init_data, *boot;
	size_t inst_size, data_size, init_size, init_data_size, boot_size;
};

S
Stanislaw Gruszka 已提交
1808
static int il4965_load_firmware(struct il_priv *il,
1809
				       const struct firmware *ucode_raw,
S
Stanislaw Gruszka 已提交
1810
				       struct il4965_firmware_pieces *pieces)
1811
{
S
Stanislaw Gruszka 已提交
1812
	struct il_ucode_header *ucode = (void *)ucode_raw->data;
1813 1814 1815
	u32 api_ver, hdr_size;
	const u8 *src;

S
Stanislaw Gruszka 已提交
1816 1817
	il->ucode_ver = le32_to_cpu(ucode->ver);
	api_ver = IL_UCODE_API(il->ucode_ver);
1818 1819 1820 1821 1822 1823 1824 1825

	switch (api_ver) {
	default:
	case 0:
	case 1:
	case 2:
		hdr_size = 24;
		if (ucode_raw->size < hdr_size) {
1826
			IL_ERR("File size too small!\n");
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
			return -EINVAL;
		}
		pieces->inst_size = le32_to_cpu(ucode->v1.inst_size);
		pieces->data_size = le32_to_cpu(ucode->v1.data_size);
		pieces->init_size = le32_to_cpu(ucode->v1.init_size);
		pieces->init_data_size =
				le32_to_cpu(ucode->v1.init_data_size);
		pieces->boot_size = le32_to_cpu(ucode->v1.boot_size);
		src = ucode->v1.data;
		break;
	}

	/* Verify size of file vs. image size info in file's header */
	if (ucode_raw->size != hdr_size + pieces->inst_size +
				pieces->data_size + pieces->init_size +
				pieces->init_data_size + pieces->boot_size) {

1844
		IL_ERR(
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
			"uCode file size %d does not match expected size\n",
			(int)ucode_raw->size);
		return -EINVAL;
	}

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

	return 0;
}

/**
S
Stanislaw Gruszka 已提交
1865
 * il4965_ucode_callback - callback when firmware was loaded
1866 1867 1868 1869 1870
 *
 * If loaded successfully, copies the firmware into buffers
 * for the card to fetch (via DMA).
 */
static void
S
Stanislaw Gruszka 已提交
1871
il4965_ucode_callback(const struct firmware *ucode_raw, void *context)
1872
{
S
Stanislaw Gruszka 已提交
1873
	struct il_priv *il = context;
S
Stanislaw Gruszka 已提交
1874
	struct il_ucode_header *ucode;
1875
	int err;
S
Stanislaw Gruszka 已提交
1876
	struct il4965_firmware_pieces pieces;
S
Stanislaw Gruszka 已提交
1877 1878
	const unsigned int api_max = il->cfg->ucode_api_max;
	const unsigned int api_min = il->cfg->ucode_api_min;
1879 1880 1881 1882
	u32 api_ver;

	u32 max_probe_length = 200;
	u32 standard_phy_calibration_size =
S
Stanislaw Gruszka 已提交
1883
			IL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1884 1885 1886 1887

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

	if (!ucode_raw) {
S
Stanislaw Gruszka 已提交
1888
		if (il->fw_idx <= il->cfg->ucode_api_max)
1889
			IL_ERR(
1890
				"request for firmware file '%s' failed.\n",
S
Stanislaw Gruszka 已提交
1891
				il->firmware_name);
1892 1893 1894
		goto try_again;
	}

1895
	D_INFO("Loaded firmware file '%s' (%zd bytes).\n",
S
Stanislaw Gruszka 已提交
1896
		       il->firmware_name, ucode_raw->size);
1897 1898 1899

	/* Make sure that we got at least the API version number */
	if (ucode_raw->size < 4) {
1900
		IL_ERR("File size way too small!\n");
1901 1902 1903 1904
		goto try_again;
	}

	/* Data from ucode file:  header followed by uCode images */
S
Stanislaw Gruszka 已提交
1905
	ucode = (struct il_ucode_header *)ucode_raw->data;
1906

S
Stanislaw Gruszka 已提交
1907
	err = il4965_load_firmware(il, ucode_raw, &pieces);
1908 1909 1910 1911

	if (err)
		goto try_again;

S
Stanislaw Gruszka 已提交
1912
	api_ver = IL_UCODE_API(il->ucode_ver);
1913 1914 1915 1916 1917 1918 1919

	/*
	 * api_ver should match the api version forming part of the
	 * firmware filename ... but we don't check for that and only rely
	 * on the API version read from firmware header from here on forward
	 */
	if (api_ver < api_min || api_ver > api_max) {
1920
		IL_ERR(
1921 1922 1923 1924 1925 1926 1927
			"Driver unable to support your firmware API. "
			"Driver supports v%u, firmware is v%u.\n",
			api_max, api_ver);
		goto try_again;
	}

	if (api_ver != api_max)
1928
		IL_ERR(
1929 1930 1931 1932 1933
			"Firmware has old API version. Expected v%u, "
			"got v%u. New firmware can be obtained "
			"from http://www.intellinuxwireless.org.\n",
			api_max, api_ver);

1934
	IL_INFO("loaded firmware version %u.%u.%u.%u\n",
S
Stanislaw Gruszka 已提交
1935 1936 1937 1938
		 IL_UCODE_MAJOR(il->ucode_ver),
		 IL_UCODE_MINOR(il->ucode_ver),
		 IL_UCODE_API(il->ucode_ver),
		 IL_UCODE_SERIAL(il->ucode_ver));
1939

S
Stanislaw Gruszka 已提交
1940 1941
	snprintf(il->hw->wiphy->fw_version,
		 sizeof(il->hw->wiphy->fw_version),
1942
		 "%u.%u.%u.%u",
S
Stanislaw Gruszka 已提交
1943 1944 1945 1946
		 IL_UCODE_MAJOR(il->ucode_ver),
		 IL_UCODE_MINOR(il->ucode_ver),
		 IL_UCODE_API(il->ucode_ver),
		 IL_UCODE_SERIAL(il->ucode_ver));
1947 1948 1949 1950 1951 1952 1953

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

1954
	D_INFO("f/w package hdr ucode version raw = 0x%x\n",
S
Stanislaw Gruszka 已提交
1955
		       il->ucode_ver);
1956
	D_INFO("f/w package hdr runtime inst size = %Zd\n",
1957
		       pieces.inst_size);
1958
	D_INFO("f/w package hdr runtime data size = %Zd\n",
1959
		       pieces.data_size);
1960
	D_INFO("f/w package hdr init inst size = %Zd\n",
1961
		       pieces.init_size);
1962
	D_INFO("f/w package hdr init data size = %Zd\n",
1963
		       pieces.init_data_size);
1964
	D_INFO("f/w package hdr boot inst size = %Zd\n",
1965 1966 1967
		       pieces.boot_size);

	/* Verify that uCode images will fit in card's SRAM */
S
Stanislaw Gruszka 已提交
1968
	if (pieces.inst_size > il->hw_params.max_inst_size) {
1969
		IL_ERR("uCode instr len %Zd too large to fit in\n",
1970 1971 1972 1973
			pieces.inst_size);
		goto try_again;
	}

S
Stanislaw Gruszka 已提交
1974
	if (pieces.data_size > il->hw_params.max_data_size) {
1975
		IL_ERR("uCode data len %Zd too large to fit in\n",
1976 1977 1978 1979
			pieces.data_size);
		goto try_again;
	}

S
Stanislaw Gruszka 已提交
1980
	if (pieces.init_size > il->hw_params.max_inst_size) {
1981
		IL_ERR("uCode init instr len %Zd too large to fit in\n",
1982 1983 1984 1985
			pieces.init_size);
		goto try_again;
	}

S
Stanislaw Gruszka 已提交
1986
	if (pieces.init_data_size > il->hw_params.max_data_size) {
1987
		IL_ERR("uCode init data len %Zd too large to fit in\n",
1988 1989 1990 1991
			pieces.init_data_size);
		goto try_again;
	}

S
Stanislaw Gruszka 已提交
1992
	if (pieces.boot_size > il->hw_params.max_bsm_size) {
1993
		IL_ERR("uCode boot instr len %Zd too large to fit in\n",
1994 1995 1996 1997 1998 1999 2000 2001 2002
			pieces.boot_size);
		goto try_again;
	}

	/* Allocate ucode buffers for card's bus-master loading ... */

	/* Runtime instructions and 2 copies of data:
	 * 1) unmodified from disk
	 * 2) backup cache for save/restore during power-downs */
S
Stanislaw Gruszka 已提交
2003 2004
	il->ucode_code.len = pieces.inst_size;
	il_alloc_fw_desc(il->pci_dev, &il->ucode_code);
2005

S
Stanislaw Gruszka 已提交
2006 2007
	il->ucode_data.len = pieces.data_size;
	il_alloc_fw_desc(il->pci_dev, &il->ucode_data);
2008

S
Stanislaw Gruszka 已提交
2009 2010
	il->ucode_data_backup.len = pieces.data_size;
	il_alloc_fw_desc(il->pci_dev, &il->ucode_data_backup);
2011

S
Stanislaw Gruszka 已提交
2012 2013
	if (!il->ucode_code.v_addr || !il->ucode_data.v_addr ||
	    !il->ucode_data_backup.v_addr)
2014 2015 2016 2017
		goto err_pci_alloc;

	/* Initialization instructions and data */
	if (pieces.init_size && pieces.init_data_size) {
S
Stanislaw Gruszka 已提交
2018 2019
		il->ucode_init.len = pieces.init_size;
		il_alloc_fw_desc(il->pci_dev, &il->ucode_init);
2020

S
Stanislaw Gruszka 已提交
2021 2022
		il->ucode_init_data.len = pieces.init_data_size;
		il_alloc_fw_desc(il->pci_dev, &il->ucode_init_data);
2023

S
Stanislaw Gruszka 已提交
2024
		if (!il->ucode_init.v_addr || !il->ucode_init_data.v_addr)
2025 2026 2027 2028 2029
			goto err_pci_alloc;
	}

	/* Bootstrap (instructions only, no data) */
	if (pieces.boot_size) {
S
Stanislaw Gruszka 已提交
2030 2031
		il->ucode_boot.len = pieces.boot_size;
		il_alloc_fw_desc(il->pci_dev, &il->ucode_boot);
2032

S
Stanislaw Gruszka 已提交
2033
		if (!il->ucode_boot.v_addr)
2034 2035 2036 2037 2038
			goto err_pci_alloc;
	}

	/* Now that we can no longer fail, copy information */

S
Stanislaw Gruszka 已提交
2039
	il->sta_key_max_num = STA_KEY_MAX_NUM;
2040 2041 2042 2043

	/* Copy images into buffers for card's bus-master reads ... */

	/* Runtime instructions (first block of data in file) */
2044
	D_INFO("Copying (but not loading) uCode instr len %Zd\n",
2045
			pieces.inst_size);
S
Stanislaw Gruszka 已提交
2046
	memcpy(il->ucode_code.v_addr, pieces.inst, pieces.inst_size);
2047

2048
	D_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
S
Stanislaw Gruszka 已提交
2049
		il->ucode_code.v_addr, (u32)il->ucode_code.p_addr);
2050 2051 2052

	/*
	 * Runtime data
S
Stanislaw Gruszka 已提交
2053
	 * NOTE:  Copy into backup buffer will be done in il_up()
2054
	 */
2055
	D_INFO("Copying (but not loading) uCode data len %Zd\n",
2056
			pieces.data_size);
S
Stanislaw Gruszka 已提交
2057 2058
	memcpy(il->ucode_data.v_addr, pieces.data, pieces.data_size);
	memcpy(il->ucode_data_backup.v_addr, pieces.data, pieces.data_size);
2059 2060 2061

	/* Initialization instructions */
	if (pieces.init_size) {
2062
		D_INFO(
2063 2064
				"Copying (but not loading) init instr len %Zd\n",
				pieces.init_size);
S
Stanislaw Gruszka 已提交
2065
		memcpy(il->ucode_init.v_addr, pieces.init, pieces.init_size);
2066 2067 2068 2069
	}

	/* Initialization data */
	if (pieces.init_data_size) {
2070
		D_INFO(
2071 2072
				"Copying (but not loading) init data len %Zd\n",
			       pieces.init_data_size);
S
Stanislaw Gruszka 已提交
2073
		memcpy(il->ucode_init_data.v_addr, pieces.init_data,
2074 2075 2076 2077
		       pieces.init_data_size);
	}

	/* Bootstrap instructions */
2078
	D_INFO("Copying (but not loading) boot instr len %Zd\n",
2079
			pieces.boot_size);
S
Stanislaw Gruszka 已提交
2080
	memcpy(il->ucode_boot.v_addr, pieces.boot, pieces.boot_size);
2081 2082 2083 2084 2085

	/*
	 * figure out the offset of chain noise reset and gain commands
	 * base on the size of standard phy calibration commands table size
	 */
S
Stanislaw Gruszka 已提交
2086
	il->_4965.phy_calib_chain_noise_reset_cmd =
2087
		standard_phy_calibration_size;
S
Stanislaw Gruszka 已提交
2088
	il->_4965.phy_calib_chain_noise_gain_cmd =
2089 2090 2091 2092 2093 2094 2095
		standard_phy_calibration_size + 1;

	/**************************************************
	 * This is still part of probe() in a sense...
	 *
	 * 9. Setup and register with mac80211 and debugfs
	 **************************************************/
S
Stanislaw Gruszka 已提交
2096
	err = il4965_mac_setup_register(il, max_probe_length);
2097 2098 2099
	if (err)
		goto out_unbind;

S
Stanislaw Gruszka 已提交
2100
	err = il_dbgfs_register(il, DRV_NAME);
2101
	if (err)
2102
		IL_ERR(
2103 2104
		"failed to create debugfs files. Ignoring error: %d\n", err);

S
Stanislaw Gruszka 已提交
2105
	err = sysfs_create_group(&il->pci_dev->dev.kobj,
S
Stanislaw Gruszka 已提交
2106
					&il_attribute_group);
2107
	if (err) {
2108
		IL_ERR("failed to create sysfs device attributes\n");
2109 2110 2111 2112 2113
		goto out_unbind;
	}

	/* We have our copies now, allow OS release its copies */
	release_firmware(ucode_raw);
S
Stanislaw Gruszka 已提交
2114
	complete(&il->_4965.firmware_loading_complete);
2115 2116 2117 2118
	return;

 try_again:
	/* try next, if any */
S
Stanislaw Gruszka 已提交
2119
	if (il4965_request_firmware(il, false))
2120 2121 2122 2123 2124
		goto out_unbind;
	release_firmware(ucode_raw);
	return;

 err_pci_alloc:
2125
	IL_ERR("failed to allocate pci memory\n");
S
Stanislaw Gruszka 已提交
2126
	il4965_dealloc_ucode_pci(il);
2127
 out_unbind:
S
Stanislaw Gruszka 已提交
2128 2129
	complete(&il->_4965.firmware_loading_complete);
	device_release_driver(&il->pci_dev->dev);
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
	release_firmware(ucode_raw);
}

static const char * const desc_lookup_text[] = {
	"OK",
	"FAIL",
	"BAD_PARAM",
	"BAD_CHECKSUM",
	"NMI_INTERRUPT_WDG",
	"SYSASSERT",
	"FATAL_ERROR",
	"BAD_COMMAND",
	"HW_ERROR_TUNE_LOCK",
	"HW_ERROR_TEMPERATURE",
	"ILLEGAL_CHAN_FREQ",
S
Stanislaw Gruszka 已提交
2145
	"VCC_NOT_STBL",
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
	"FH_ERROR",
	"NMI_INTERRUPT_HOST",
	"NMI_INTERRUPT_ACTION_PT",
	"NMI_INTERRUPT_UNKNOWN",
	"UCODE_VERSION_MISMATCH",
	"HW_ERROR_ABS_LOCK",
	"HW_ERROR_CAL_LOCK_FAIL",
	"NMI_INTERRUPT_INST_ACTION_PT",
	"NMI_INTERRUPT_DATA_ACTION_PT",
	"NMI_TRM_HW_ER",
	"NMI_INTERRUPT_TRM",
2157
	"NMI_INTERRUPT_BREAK_POINT",
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182
	"DEBUG_0",
	"DEBUG_1",
	"DEBUG_2",
	"DEBUG_3",
};

static struct { char *name; u8 num; } advanced_lookup[] = {
	{ "NMI_INTERRUPT_WDG", 0x34 },
	{ "SYSASSERT", 0x35 },
	{ "UCODE_VERSION_MISMATCH", 0x37 },
	{ "BAD_COMMAND", 0x38 },
	{ "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
	{ "FATAL_ERROR", 0x3D },
	{ "NMI_TRM_HW_ERR", 0x46 },
	{ "NMI_INTERRUPT_TRM", 0x4C },
	{ "NMI_INTERRUPT_BREAK_POINT", 0x54 },
	{ "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
	{ "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
	{ "NMI_INTERRUPT_HOST", 0x66 },
	{ "NMI_INTERRUPT_ACTION_PT", 0x7C },
	{ "NMI_INTERRUPT_UNKNOWN", 0x84 },
	{ "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
	{ "ADVANCED_SYSASSERT", 0 },
};

S
Stanislaw Gruszka 已提交
2183
static const char *il4965_desc_lookup(u32 num)
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
{
	int i;
	int max = ARRAY_SIZE(desc_lookup_text);

	if (num < max)
		return desc_lookup_text[num];

	max = ARRAY_SIZE(advanced_lookup) - 1;
	for (i = 0; i < max; i++) {
		if (advanced_lookup[i].num == num)
			break;
	}
	return advanced_lookup[i].name;
}

#define ERROR_START_OFFSET  (1 * sizeof(u32))
#define ERROR_ELEM_SIZE     (7 * sizeof(u32))

S
Stanislaw Gruszka 已提交
2202
void il4965_dump_nic_error_log(struct il_priv *il)
2203 2204 2205 2206 2207 2208
{
	u32 data2, line;
	u32 desc, time, count, base, data1;
	u32 blink1, blink2, ilink1, ilink2;
	u32 pc, hcmd;

S
Stanislaw Gruszka 已提交
2209 2210
	if (il->ucode_type == UCODE_INIT) {
		base = le32_to_cpu(il->card_alive_init.error_event_table_ptr);
2211
	} else {
S
Stanislaw Gruszka 已提交
2212
		base = le32_to_cpu(il->card_alive.error_event_table_ptr);
2213 2214
	}

S
Stanislaw Gruszka 已提交
2215
	if (!il->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
2216
		IL_ERR(
2217
			"Not valid error log pointer 0x%08X for %s uCode\n",
S
Stanislaw Gruszka 已提交
2218
			base, (il->ucode_type == UCODE_INIT) ? "Init" : "RT");
2219 2220 2221
		return;
	}

S
Stanislaw Gruszka 已提交
2222
	count = il_read_targ_mem(il, base);
2223 2224

	if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
2225 2226
		IL_ERR("Start IWL Error Log Dump:\n");
		IL_ERR("Status: 0x%08lX, count: %d\n",
S
Stanislaw Gruszka 已提交
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
			il->status, count);
	}

	desc = il_read_targ_mem(il, base + 1 * sizeof(u32));
	il->isr_stats.err_code = desc;
	pc = il_read_targ_mem(il, base + 2 * sizeof(u32));
	blink1 = il_read_targ_mem(il, base + 3 * sizeof(u32));
	blink2 = il_read_targ_mem(il, base + 4 * sizeof(u32));
	ilink1 = il_read_targ_mem(il, base + 5 * sizeof(u32));
	ilink2 = il_read_targ_mem(il, base + 6 * sizeof(u32));
	data1 = il_read_targ_mem(il, base + 7 * sizeof(u32));
	data2 = il_read_targ_mem(il, base + 8 * sizeof(u32));
	line = il_read_targ_mem(il, base + 9 * sizeof(u32));
	time = il_read_targ_mem(il, base + 11 * sizeof(u32));
	hcmd = il_read_targ_mem(il, base + 22 * sizeof(u32));

2243
	IL_ERR("Desc                                  Time       "
2244
		"data1      data2      line\n");
2245
	IL_ERR("%-28s (0x%04X) %010u 0x%08X 0x%08X %u\n",
S
Stanislaw Gruszka 已提交
2246
		il4965_desc_lookup(desc), desc, time, data1, data2, line);
2247 2248
	IL_ERR("pc      blink1  blink2  ilink1  ilink2  hcmd\n");
	IL_ERR("0x%05X 0x%05X 0x%05X 0x%05X 0x%05X 0x%05X\n",
2249 2250 2251
		pc, blink1, blink2, ilink1, ilink2, hcmd);
}

S
Stanislaw Gruszka 已提交
2252
static void il4965_rf_kill_ct_config(struct il_priv *il)
2253
{
S
Stanislaw Gruszka 已提交
2254
	struct il_ct_kill_config cmd;
2255 2256 2257
	unsigned long flags;
	int ret = 0;

S
Stanislaw Gruszka 已提交
2258
	spin_lock_irqsave(&il->lock, flags);
2259
	_il_wr(il, CSR_UCODE_DRV_GP1_CLR,
2260
		    CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
S
Stanislaw Gruszka 已提交
2261
	spin_unlock_irqrestore(&il->lock, flags);
2262 2263

	cmd.critical_temperature_R =
S
Stanislaw Gruszka 已提交
2264
		cpu_to_le32(il->hw_params.ct_kill_threshold);
2265

S
Stanislaw Gruszka 已提交
2266
	ret = il_send_cmd_pdu(il, REPLY_CT_KILL_CONFIG_CMD,
2267 2268
			       sizeof(cmd), &cmd);
	if (ret)
2269
		IL_ERR("REPLY_CT_KILL_CONFIG_CMD failed\n");
2270
	else
2271
		D_INFO("REPLY_CT_KILL_CONFIG_CMD "
2272 2273
				"succeeded, "
				"critical temperature is %d\n",
S
Stanislaw Gruszka 已提交
2274
				il->hw_params.ct_kill_threshold);
2275 2276 2277
}

static const s8 default_queue_to_tx_fifo[] = {
S
Stanislaw Gruszka 已提交
2278 2279 2280 2281
	IL_TX_FIFO_VO,
	IL_TX_FIFO_VI,
	IL_TX_FIFO_BE,
	IL_TX_FIFO_BK,
2282
	IL49_CMD_FIFO_NUM,
S
Stanislaw Gruszka 已提交
2283 2284
	IL_TX_FIFO_UNUSED,
	IL_TX_FIFO_UNUSED,
2285 2286
};

S
Stanislaw Gruszka 已提交
2287
static int il4965_alive_notify(struct il_priv *il)
2288 2289 2290 2291 2292 2293
{
	u32 a;
	unsigned long flags;
	int i, chan;
	u32 reg_val;

S
Stanislaw Gruszka 已提交
2294
	spin_lock_irqsave(&il->lock, flags);
2295 2296

	/* Clear 4965's internal Tx Scheduler data base */
2297
	il->scd_base_addr = il_rd_prph(il,
2298 2299 2300
					IL49_SCD_SRAM_BASE_ADDR);
	a = il->scd_base_addr + IL49_SCD_CONTEXT_DATA_OFFSET;
	for (; a < il->scd_base_addr + IL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
S
Stanislaw Gruszka 已提交
2301
		il_write_targ_mem(il, a, 0);
2302
	for (; a < il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
S
Stanislaw Gruszka 已提交
2303 2304
		il_write_targ_mem(il, a, 0);
	for (; a < il->scd_base_addr +
2305
	       IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(il->hw_params.max_txq_num); a += 4)
S
Stanislaw Gruszka 已提交
2306
		il_write_targ_mem(il, a, 0);
2307 2308

	/* Tel 4965 where to find Tx byte count tables */
2309
	il_wr_prph(il, IL49_SCD_DRAM_BASE_ADDR,
S
Stanislaw Gruszka 已提交
2310
			il->scd_bc_tbls.dma >> 10);
2311 2312 2313

	/* Enable DMA channel */
	for (chan = 0; chan < FH49_TCSR_CHNL_NUM ; chan++)
2314
		il_wr(il,
2315 2316 2317 2318 2319
				FH_TCSR_CHNL_TX_CONFIG_REG(chan),
				FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
				FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);

	/* Update FH chicken bits */
2320 2321
	reg_val = il_rd(il, FH_TX_CHICKEN_BITS_REG);
	il_wr(il, FH_TX_CHICKEN_BITS_REG,
2322 2323 2324
			   reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);

	/* Disable chain mode for all queues */
2325
	il_wr_prph(il, IL49_SCD_QUEUECHAIN_SEL, 0);
2326 2327

	/* Initialize each Tx queue (including the command queue) */
S
Stanislaw Gruszka 已提交
2328
	for (i = 0; i < il->hw_params.max_txq_num; i++) {
2329

S
Stanislaw Gruszka 已提交
2330
		/* TFD circular buffer read/write idxes */
2331
		il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(i), 0);
2332
		il_wr(il, HBUS_TARG_WRPTR, 0 | (i << 8));
2333 2334

		/* Max Tx Window size for Scheduler-ACK mode */
S
Stanislaw Gruszka 已提交
2335
		il_write_targ_mem(il, il->scd_base_addr +
2336
				IL49_SCD_CONTEXT_QUEUE_OFFSET(i),
2337
				(SCD_WIN_SIZE <<
2338 2339
				IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
				IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
2340 2341

		/* Frame limit */
S
Stanislaw Gruszka 已提交
2342
		il_write_targ_mem(il, il->scd_base_addr +
2343
				IL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
2344 2345
				sizeof(u32),
				(SCD_FRAME_LIMIT <<
2346 2347
				IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
				IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
2348 2349

	}
2350
	il_wr_prph(il, IL49_SCD_INTERRUPT_MASK,
S
Stanislaw Gruszka 已提交
2351
				 (1 << il->hw_params.max_txq_num) - 1);
2352 2353

	/* Activate all Tx DMA/FIFO channels */
S
Stanislaw Gruszka 已提交
2354
	il4965_txq_set_sched(il, IL_MASK(0, 6));
2355

S
Stanislaw Gruszka 已提交
2356
	il4965_set_wr_ptrs(il, IL_DEFAULT_CMD_QUEUE_NUM, 0);
2357 2358

	/* make sure all queue are not stopped */
S
Stanislaw Gruszka 已提交
2359
	memset(&il->queue_stopped[0], 0, sizeof(il->queue_stopped));
2360
	for (i = 0; i < 4; i++)
S
Stanislaw Gruszka 已提交
2361
		atomic_set(&il->queue_stop_count[i], 0);
2362 2363

	/* reset to 0 to enable all the queue first */
S
Stanislaw Gruszka 已提交
2364
	il->txq_ctx_active_msk = 0;
2365 2366 2367 2368 2369 2370
	/* Map each Tx/cmd queue to its corresponding fifo */
	BUILD_BUG_ON(ARRAY_SIZE(default_queue_to_tx_fifo) != 7);

	for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
		int ac = default_queue_to_tx_fifo[i];

S
Stanislaw Gruszka 已提交
2371
		il_txq_ctx_activate(il, i);
2372

S
Stanislaw Gruszka 已提交
2373
		if (ac == IL_TX_FIFO_UNUSED)
2374 2375
			continue;

S
Stanislaw Gruszka 已提交
2376
		il4965_tx_queue_set_status(il, &il->txq[i], ac, 0);
2377 2378
	}

S
Stanislaw Gruszka 已提交
2379
	spin_unlock_irqrestore(&il->lock, flags);
2380 2381 2382 2383 2384

	return 0;
}

/**
S
Stanislaw Gruszka 已提交
2385
 * il4965_alive_start - called after REPLY_ALIVE notification received
2386
 *                   from protocol/runtime uCode (initialization uCode's
S
Stanislaw Gruszka 已提交
2387
 *                   Alive gets handled by il_init_alive_start()).
2388
 */
S
Stanislaw Gruszka 已提交
2389
static void il4965_alive_start(struct il_priv *il)
2390 2391
{
	int ret = 0;
2392
	struct il_rxon_context *ctx = &il->ctx;
2393

2394
	D_INFO("Runtime Alive received.\n");
2395

S
Stanislaw Gruszka 已提交
2396
	if (il->card_alive.is_valid != UCODE_VALID_OK) {
2397 2398
		/* We had an error bringing up the hardware, so take it
		 * all the way back down so we can try again */
2399
		D_INFO("Alive failed.\n");
2400 2401 2402 2403 2404 2405
		goto restart;
	}

	/* Initialize uCode has loaded Runtime uCode ... verify inst image.
	 * This is a paranoid check, because we would not have gotten the
	 * "runtime" alive if code weren't properly loaded.  */
S
Stanislaw Gruszka 已提交
2406
	if (il4965_verify_ucode(il)) {
2407 2408
		/* Runtime instruction load was bad;
		 * take it all the way back down so we can try again */
2409
		D_INFO("Bad runtime uCode load.\n");
2410 2411 2412
		goto restart;
	}

S
Stanislaw Gruszka 已提交
2413
	ret = il4965_alive_notify(il);
2414
	if (ret) {
2415
		IL_WARN(
2416 2417 2418 2419 2420 2421
			"Could not complete ALIVE transition [ntf]: %d\n", ret);
		goto restart;
	}


	/* After the ALIVE response, we can send host commands to the uCode */
S
Stanislaw Gruszka 已提交
2422
	set_bit(STATUS_ALIVE, &il->status);
2423 2424

	/* Enable watchdog to monitor the driver tx queues */
S
Stanislaw Gruszka 已提交
2425
	il_setup_watchdog(il);
2426

S
Stanislaw Gruszka 已提交
2427
	if (il_is_rfkill(il))
2428 2429
		return;

S
Stanislaw Gruszka 已提交
2430
	ieee80211_wake_queues(il->hw);
2431

S
Stanislaw Gruszka 已提交
2432
	il->active_rate = RATES_MASK;
2433

S
Stanislaw Gruszka 已提交
2434 2435 2436
	if (il_is_associated_ctx(ctx)) {
		struct il_rxon_cmd *active_rxon =
				(struct il_rxon_cmd *)&ctx->active;
2437 2438 2439 2440 2441
		/* apply any changes in staging */
		ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
		active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
	} else {
		/* Initialize our rx_config data */
2442
		il_connection_init_rx_config(il, &il->ctx);
2443

S
Stanislaw Gruszka 已提交
2444 2445
		if (il->cfg->ops->hcmd->set_rxon_chain)
			il->cfg->ops->hcmd->set_rxon_chain(il, ctx);
2446 2447 2448
	}

	/* Configure bluetooth coexistence if enabled */
S
Stanislaw Gruszka 已提交
2449
	il_send_bt_config(il);
2450

S
Stanislaw Gruszka 已提交
2451
	il4965_reset_run_time_calib(il);
2452

S
Stanislaw Gruszka 已提交
2453
	set_bit(STATUS_READY, &il->status);
2454 2455

	/* Configure the adapter for unassociated operation */
S
Stanislaw Gruszka 已提交
2456
	il_commit_rxon(il, ctx);
2457 2458

	/* At this point, the NIC is initialized and operational */
S
Stanislaw Gruszka 已提交
2459
	il4965_rf_kill_ct_config(il);
2460

2461
	D_INFO("ALIVE processing complete.\n");
S
Stanislaw Gruszka 已提交
2462
	wake_up(&il->wait_command_queue);
2463

S
Stanislaw Gruszka 已提交
2464
	il_power_update_mode(il, true);
2465
	D_INFO("Updated power mode\n");
2466 2467 2468 2469

	return;

 restart:
S
Stanislaw Gruszka 已提交
2470
	queue_work(il->workqueue, &il->restart);
2471 2472
}

S
Stanislaw Gruszka 已提交
2473
static void il4965_cancel_deferred_work(struct il_priv *il);
2474

S
Stanislaw Gruszka 已提交
2475
static void __il4965_down(struct il_priv *il)
2476 2477
{
	unsigned long flags;
2478
	int exit_pending;
2479

2480
	D_INFO(DRV_NAME " is going down\n");
2481

S
Stanislaw Gruszka 已提交
2482
	il_scan_cancel_timeout(il, 200);
2483

S
Stanislaw Gruszka 已提交
2484
	exit_pending = test_and_set_bit(STATUS_EXIT_PENDING, &il->status);
2485 2486 2487

	/* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
	 * to prevent rearm timer */
S
Stanislaw Gruszka 已提交
2488
	del_timer_sync(&il->watchdog);
2489

S
Stanislaw Gruszka 已提交
2490 2491 2492
	il_clear_ucode_stations(il, NULL);
	il_dealloc_bcast_stations(il);
	il_clear_driver_stations(il);
2493 2494

	/* Unblock any waiting calls */
S
Stanislaw Gruszka 已提交
2495
	wake_up_all(&il->wait_command_queue);
2496 2497 2498 2499

	/* Wipe out the EXIT_PENDING status bit if we are not actually
	 * exiting the module */
	if (!exit_pending)
S
Stanislaw Gruszka 已提交
2500
		clear_bit(STATUS_EXIT_PENDING, &il->status);
2501 2502

	/* stop and reset the on-board processor */
2503
	_il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
2504 2505

	/* tell the device to stop sending interrupts */
S
Stanislaw Gruszka 已提交
2506 2507 2508 2509
	spin_lock_irqsave(&il->lock, flags);
	il_disable_interrupts(il);
	spin_unlock_irqrestore(&il->lock, flags);
	il4965_synchronize_irq(il);
2510

S
Stanislaw Gruszka 已提交
2511 2512
	if (il->mac80211_registered)
		ieee80211_stop_queues(il->hw);
2513

S
Stanislaw Gruszka 已提交
2514
	/* If we have not previously called il_init() then
2515
	 * clear all bits but the RF Kill bit and return */
S
Stanislaw Gruszka 已提交
2516 2517
	if (!il_is_init(il)) {
		il->status = test_bit(STATUS_RF_KILL_HW, &il->status) <<
2518
					STATUS_RF_KILL_HW |
S
Stanislaw Gruszka 已提交
2519
			       test_bit(STATUS_GEO_CONFIGURED, &il->status) <<
2520
					STATUS_GEO_CONFIGURED |
S
Stanislaw Gruszka 已提交
2521
			       test_bit(STATUS_EXIT_PENDING, &il->status) <<
2522 2523 2524 2525 2526 2527
					STATUS_EXIT_PENDING;
		goto exit;
	}

	/* ...otherwise clear out all the status bits but the RF Kill
	 * bit and continue taking the NIC down. */
S
Stanislaw Gruszka 已提交
2528
	il->status &= test_bit(STATUS_RF_KILL_HW, &il->status) <<
2529
				STATUS_RF_KILL_HW |
S
Stanislaw Gruszka 已提交
2530
			test_bit(STATUS_GEO_CONFIGURED, &il->status) <<
2531
				STATUS_GEO_CONFIGURED |
S
Stanislaw Gruszka 已提交
2532
			test_bit(STATUS_FW_ERROR, &il->status) <<
2533
				STATUS_FW_ERROR |
S
Stanislaw Gruszka 已提交
2534
		       test_bit(STATUS_EXIT_PENDING, &il->status) <<
2535 2536
				STATUS_EXIT_PENDING;

S
Stanislaw Gruszka 已提交
2537 2538
	il4965_txq_ctx_stop(il);
	il4965_rxq_stop(il);
2539 2540

	/* Power-down device's busmaster DMA clocks */
2541
	il_wr_prph(il, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
2542 2543 2544
	udelay(5);

	/* Make sure (redundant) we've released our request to stay awake */
S
Stanislaw Gruszka 已提交
2545
	il_clear_bit(il, CSR_GP_CNTRL,
2546 2547 2548
				CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);

	/* Stop the device, and put it in low power state */
S
Stanislaw Gruszka 已提交
2549
	il_apm_stop(il);
2550 2551

 exit:
S
Stanislaw Gruszka 已提交
2552
	memset(&il->card_alive, 0, sizeof(struct il_alive_resp));
2553

S
Stanislaw Gruszka 已提交
2554 2555
	dev_kfree_skb(il->beacon_skb);
	il->beacon_skb = NULL;
2556 2557

	/* clear out any free frames */
S
Stanislaw Gruszka 已提交
2558
	il4965_clear_free_frames(il);
2559 2560
}

S
Stanislaw Gruszka 已提交
2561
static void il4965_down(struct il_priv *il)
2562
{
S
Stanislaw Gruszka 已提交
2563 2564 2565
	mutex_lock(&il->mutex);
	__il4965_down(il);
	mutex_unlock(&il->mutex);
2566

S
Stanislaw Gruszka 已提交
2567
	il4965_cancel_deferred_work(il);
2568 2569 2570 2571
}

#define HW_READY_TIMEOUT (50)

S
Stanislaw Gruszka 已提交
2572
static int il4965_set_hw_ready(struct il_priv *il)
2573 2574 2575
{
	int ret = 0;

S
Stanislaw Gruszka 已提交
2576
	il_set_bit(il, CSR_HW_IF_CONFIG_REG,
2577 2578 2579
		CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);

	/* See if we got it */
2580
	ret = _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
2581 2582 2583 2584
				CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
				CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
				HW_READY_TIMEOUT);
	if (ret != -ETIMEDOUT)
S
Stanislaw Gruszka 已提交
2585
		il->hw_ready = true;
2586
	else
S
Stanislaw Gruszka 已提交
2587
		il->hw_ready = false;
2588

2589
	D_INFO("hardware %s\n",
S
Stanislaw Gruszka 已提交
2590
		      (il->hw_ready == 1) ? "ready" : "not ready");
2591 2592 2593
	return ret;
}

S
Stanislaw Gruszka 已提交
2594
static int il4965_prepare_card_hw(struct il_priv *il)
2595 2596 2597
{
	int ret = 0;

2598
	D_INFO("il4965_prepare_card_hw enter\n");
2599

S
Stanislaw Gruszka 已提交
2600 2601
	ret = il4965_set_hw_ready(il);
	if (il->hw_ready)
2602 2603 2604
		return ret;

	/* If HW is not ready, prepare the conditions to check again */
S
Stanislaw Gruszka 已提交
2605
	il_set_bit(il, CSR_HW_IF_CONFIG_REG,
2606 2607
			CSR_HW_IF_CONFIG_REG_PREPARE);

2608
	ret = _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
2609 2610 2611 2612 2613
			~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
			CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);

	/* HW should be ready by now, check again. */
	if (ret != -ETIMEDOUT)
S
Stanislaw Gruszka 已提交
2614
		il4965_set_hw_ready(il);
2615 2616 2617 2618 2619 2620

	return ret;
}

#define MAX_HW_RESTARTS 5

S
Stanislaw Gruszka 已提交
2621
static int __il4965_up(struct il_priv *il)
2622 2623 2624 2625
{
	int i;
	int ret;

S
Stanislaw Gruszka 已提交
2626
	if (test_bit(STATUS_EXIT_PENDING, &il->status)) {
2627
		IL_WARN("Exit pending; will not bring the NIC up\n");
2628 2629 2630
		return -EIO;
	}

S
Stanislaw Gruszka 已提交
2631
	if (!il->ucode_data_backup.v_addr || !il->ucode_data.v_addr) {
2632
		IL_ERR("ucode not available for device bringup\n");
2633 2634 2635
		return -EIO;
	}

2636 2637 2638 2639
	ret = il4965_alloc_bcast_station(il, &il->ctx);
	if (ret) {
		il_dealloc_bcast_stations(il);
		return ret;
2640 2641
	}

S
Stanislaw Gruszka 已提交
2642
	il4965_prepare_card_hw(il);
2643

S
Stanislaw Gruszka 已提交
2644
	if (!il->hw_ready) {
2645
		IL_WARN("Exit HW not ready\n");
2646 2647 2648 2649
		return -EIO;
	}

	/* If platform's RF_KILL switch is NOT set to KILL */
2650
	if (_il_rd(il,
2651
		CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
S
Stanislaw Gruszka 已提交
2652
		clear_bit(STATUS_RF_KILL_HW, &il->status);
2653
	else
S
Stanislaw Gruszka 已提交
2654
		set_bit(STATUS_RF_KILL_HW, &il->status);
2655

S
Stanislaw Gruszka 已提交
2656 2657
	if (il_is_rfkill(il)) {
		wiphy_rfkill_set_hw_state(il->hw->wiphy, true);
2658

S
Stanislaw Gruszka 已提交
2659
		il_enable_interrupts(il);
2660
		IL_WARN("Radio disabled by HW RF Kill switch\n");
2661 2662 2663
		return 0;
	}

2664
	_il_wr(il, CSR_INT, 0xFFFFFFFF);
2665

S
Stanislaw Gruszka 已提交
2666
	/* must be initialised before il_hw_nic_init */
S
Stanislaw Gruszka 已提交
2667
	il->cmd_queue = IL_DEFAULT_CMD_QUEUE_NUM;
2668

S
Stanislaw Gruszka 已提交
2669
	ret = il4965_hw_nic_init(il);
2670
	if (ret) {
2671
		IL_ERR("Unable to init nic\n");
2672 2673 2674 2675
		return ret;
	}

	/* make sure rfkill handshake bits are cleared */
2676 2677
	_il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
	_il_wr(il, CSR_UCODE_DRV_GP1_CLR,
2678 2679 2680
		    CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);

	/* clear (again), then enable host interrupts */
2681
	_il_wr(il, CSR_INT, 0xFFFFFFFF);
S
Stanislaw Gruszka 已提交
2682
	il_enable_interrupts(il);
2683 2684

	/* really make sure rfkill handshake bits are cleared */
2685 2686
	_il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
	_il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2687 2688 2689 2690

	/* Copy original ucode data image from disk into backup cache.
	 * This will be used to initialize the on-board processor's
	 * data SRAM for a clean start when the runtime program first loads. */
S
Stanislaw Gruszka 已提交
2691 2692
	memcpy(il->ucode_data_backup.v_addr, il->ucode_data.v_addr,
	       il->ucode_data.len);
2693 2694 2695 2696 2697 2698

	for (i = 0; i < MAX_HW_RESTARTS; i++) {

		/* load bootstrap state machine,
		 * load bootstrap program into processor's memory,
		 * prepare to load the "initialize" uCode */
S
Stanislaw Gruszka 已提交
2699
		ret = il->cfg->ops->lib->load_ucode(il);
2700 2701

		if (ret) {
2702
			IL_ERR("Unable to set up bootstrap uCode: %d\n",
2703 2704 2705 2706 2707
				ret);
			continue;
		}

		/* start card; "initialize" will load runtime ucode */
S
Stanislaw Gruszka 已提交
2708
		il4965_nic_start(il);
2709

2710
		D_INFO(DRV_NAME " is coming up\n");
2711 2712 2713 2714

		return 0;
	}

S
Stanislaw Gruszka 已提交
2715 2716 2717
	set_bit(STATUS_EXIT_PENDING, &il->status);
	__il4965_down(il);
	clear_bit(STATUS_EXIT_PENDING, &il->status);
2718 2719 2720

	/* tried to restart and config the device for as long as our
	 * patience could withstand */
2721
	IL_ERR("Unable to initialize device after %d attempts.\n", i);
2722 2723 2724 2725 2726 2727 2728 2729 2730 2731
	return -EIO;
}


/*****************************************************************************
 *
 * Workqueue callbacks
 *
 *****************************************************************************/

S
Stanislaw Gruszka 已提交
2732
static void il4965_bg_init_alive_start(struct work_struct *data)
2733
{
S
Stanislaw Gruszka 已提交
2734
	struct il_priv *il =
S
Stanislaw Gruszka 已提交
2735
	    container_of(data, struct il_priv, init_alive_start.work);
2736

S
Stanislaw Gruszka 已提交
2737 2738
	mutex_lock(&il->mutex);
	if (test_bit(STATUS_EXIT_PENDING, &il->status))
2739
		goto out;
2740

S
Stanislaw Gruszka 已提交
2741
	il->cfg->ops->lib->init_alive_start(il);
2742
out:
S
Stanislaw Gruszka 已提交
2743
	mutex_unlock(&il->mutex);
2744 2745
}

S
Stanislaw Gruszka 已提交
2746
static void il4965_bg_alive_start(struct work_struct *data)
2747
{
S
Stanislaw Gruszka 已提交
2748
	struct il_priv *il =
S
Stanislaw Gruszka 已提交
2749
	    container_of(data, struct il_priv, alive_start.work);
2750

S
Stanislaw Gruszka 已提交
2751 2752
	mutex_lock(&il->mutex);
	if (test_bit(STATUS_EXIT_PENDING, &il->status))
2753
		goto out;
2754

S
Stanislaw Gruszka 已提交
2755
	il4965_alive_start(il);
2756
out:
S
Stanislaw Gruszka 已提交
2757
	mutex_unlock(&il->mutex);
2758 2759
}

S
Stanislaw Gruszka 已提交
2760
static void il4965_bg_run_time_calib_work(struct work_struct *work)
2761
{
S
Stanislaw Gruszka 已提交
2762
	struct il_priv *il = container_of(work, struct il_priv,
2763 2764
			run_time_calib_work);

S
Stanislaw Gruszka 已提交
2765
	mutex_lock(&il->mutex);
2766

S
Stanislaw Gruszka 已提交
2767 2768 2769
	if (test_bit(STATUS_EXIT_PENDING, &il->status) ||
	    test_bit(STATUS_SCANNING, &il->status)) {
		mutex_unlock(&il->mutex);
2770 2771 2772
		return;
	}

S
Stanislaw Gruszka 已提交
2773 2774
	if (il->start_calib) {
		il4965_chain_noise_calibration(il,
S
Stanislaw Gruszka 已提交
2775
				(void *)&il->_4965.stats);
S
Stanislaw Gruszka 已提交
2776
		il4965_sensitivity_calibration(il,
S
Stanislaw Gruszka 已提交
2777
				(void *)&il->_4965.stats);
2778 2779
	}

S
Stanislaw Gruszka 已提交
2780
	mutex_unlock(&il->mutex);
2781 2782
}

S
Stanislaw Gruszka 已提交
2783
static void il4965_bg_restart(struct work_struct *data)
2784
{
S
Stanislaw Gruszka 已提交
2785
	struct il_priv *il = container_of(data, struct il_priv, restart);
2786

S
Stanislaw Gruszka 已提交
2787
	if (test_bit(STATUS_EXIT_PENDING, &il->status))
2788 2789
		return;

S
Stanislaw Gruszka 已提交
2790 2791
	if (test_and_clear_bit(STATUS_FW_ERROR, &il->status)) {
		mutex_lock(&il->mutex);
2792
		il->ctx.vif = NULL;
S
Stanislaw Gruszka 已提交
2793
		il->is_open = 0;
2794

S
Stanislaw Gruszka 已提交
2795
		__il4965_down(il);
2796

S
Stanislaw Gruszka 已提交
2797 2798 2799
		mutex_unlock(&il->mutex);
		il4965_cancel_deferred_work(il);
		ieee80211_restart_hw(il->hw);
2800
	} else {
S
Stanislaw Gruszka 已提交
2801
		il4965_down(il);
2802

S
Stanislaw Gruszka 已提交
2803 2804 2805
		mutex_lock(&il->mutex);
		if (test_bit(STATUS_EXIT_PENDING, &il->status)) {
			mutex_unlock(&il->mutex);
2806
			return;
2807
		}
2808

S
Stanislaw Gruszka 已提交
2809 2810
		__il4965_up(il);
		mutex_unlock(&il->mutex);
2811 2812 2813
	}
}

S
Stanislaw Gruszka 已提交
2814
static void il4965_bg_rx_replenish(struct work_struct *data)
2815
{
S
Stanislaw Gruszka 已提交
2816
	struct il_priv *il =
S
Stanislaw Gruszka 已提交
2817
	    container_of(data, struct il_priv, rx_replenish);
2818

S
Stanislaw Gruszka 已提交
2819
	if (test_bit(STATUS_EXIT_PENDING, &il->status))
2820 2821
		return;

S
Stanislaw Gruszka 已提交
2822 2823 2824
	mutex_lock(&il->mutex);
	il4965_rx_replenish(il);
	mutex_unlock(&il->mutex);
2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
}

/*****************************************************************************
 *
 * mac80211 entry point functions
 *
 *****************************************************************************/

#define UCODE_READY_TIMEOUT	(4 * HZ)

/*
 * Not a mac80211 entry point function, but it fits in with all the
 * other mac80211 functions grouped here.
 */
S
Stanislaw Gruszka 已提交
2839
static int il4965_mac_setup_register(struct il_priv *il,
2840 2841 2842
				  u32 max_probe_length)
{
	int ret;
S
Stanislaw Gruszka 已提交
2843
	struct ieee80211_hw *hw = il->hw;
2844 2845 2846 2847 2848 2849 2850 2851 2852 2853

	hw->rate_control_algorithm = "iwl-4965-rs";

	/* Tell mac80211 our characteristics */
	hw->flags = IEEE80211_HW_SIGNAL_DBM |
		    IEEE80211_HW_AMPDU_AGGREGATION |
		    IEEE80211_HW_NEED_DTIM_PERIOD |
		    IEEE80211_HW_SPECTRUM_MGMT |
		    IEEE80211_HW_REPORTS_TX_ACK_STATUS;

S
Stanislaw Gruszka 已提交
2854
	if (il->cfg->sku & IL_SKU_N)
2855 2856 2857
		hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
			     IEEE80211_HW_SUPPORTS_STATIC_SMPS;

S
Stanislaw Gruszka 已提交
2858 2859
	hw->sta_data_size = sizeof(struct il_station_priv);
	hw->vif_data_size = sizeof(struct il_vif_priv);
2860

2861 2862
	hw->wiphy->interface_modes |= il->ctx.interface_modes;
	hw->wiphy->interface_modes |= il->ctx.exclusive_interface_modes;
2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879

	hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
			    WIPHY_FLAG_DISABLE_BEACON_HINTS;

	/*
	 * For now, disable PS by default because it affects
	 * RX performance significantly.
	 */
	hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;

	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
	/* we create the 802.11 header and a zero-length SSID element */
	hw->wiphy->max_scan_ie_len = max_probe_length - 24 - 2;

	/* Default value; 4 EDCA QOS priorities */
	hw->queues = 4;

S
Stanislaw Gruszka 已提交
2880
	hw->max_listen_interval = IL_CONN_MAX_LISTEN_INTERVAL;
2881

S
Stanislaw Gruszka 已提交
2882 2883 2884 2885 2886 2887
	if (il->bands[IEEE80211_BAND_2GHZ].n_channels)
		il->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
			&il->bands[IEEE80211_BAND_2GHZ];
	if (il->bands[IEEE80211_BAND_5GHZ].n_channels)
		il->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
			&il->bands[IEEE80211_BAND_5GHZ];
2888

S
Stanislaw Gruszka 已提交
2889
	il_leds_init(il);
2890

S
Stanislaw Gruszka 已提交
2891
	ret = ieee80211_register_hw(il->hw);
2892
	if (ret) {
2893
		IL_ERR("Failed to register hw (error %d)\n", ret);
2894 2895
		return ret;
	}
S
Stanislaw Gruszka 已提交
2896
	il->mac80211_registered = 1;
2897 2898 2899 2900 2901

	return 0;
}


S
Stanislaw Gruszka 已提交
2902
int il4965_mac_start(struct ieee80211_hw *hw)
2903
{
S
Stanislaw Gruszka 已提交
2904
	struct il_priv *il = hw->priv;
2905 2906
	int ret;

2907
	D_MAC80211("enter\n");
2908 2909

	/* we should be verifying the device is ready to be opened */
S
Stanislaw Gruszka 已提交
2910 2911 2912
	mutex_lock(&il->mutex);
	ret = __il4965_up(il);
	mutex_unlock(&il->mutex);
2913 2914 2915 2916

	if (ret)
		return ret;

S
Stanislaw Gruszka 已提交
2917
	if (il_is_rfkill(il))
2918 2919
		goto out;

2920
	D_INFO("Start UP work done.\n");
2921 2922 2923

	/* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
	 * mac80211 will not be run successfully. */
S
Stanislaw Gruszka 已提交
2924 2925
	ret = wait_event_timeout(il->wait_command_queue,
			test_bit(STATUS_READY, &il->status),
2926 2927
			UCODE_READY_TIMEOUT);
	if (!ret) {
S
Stanislaw Gruszka 已提交
2928
		if (!test_bit(STATUS_READY, &il->status)) {
2929
			IL_ERR("START_ALIVE timeout after %dms.\n",
2930 2931 2932 2933 2934
				jiffies_to_msecs(UCODE_READY_TIMEOUT));
			return -ETIMEDOUT;
		}
	}

S
Stanislaw Gruszka 已提交
2935
	il4965_led_enable(il);
2936 2937

out:
S
Stanislaw Gruszka 已提交
2938
	il->is_open = 1;
2939
	D_MAC80211("leave\n");
2940 2941 2942
	return 0;
}

S
Stanislaw Gruszka 已提交
2943
void il4965_mac_stop(struct ieee80211_hw *hw)
2944
{
S
Stanislaw Gruszka 已提交
2945
	struct il_priv *il = hw->priv;
2946

2947
	D_MAC80211("enter\n");
2948

S
Stanislaw Gruszka 已提交
2949
	if (!il->is_open)
2950 2951
		return;

S
Stanislaw Gruszka 已提交
2952
	il->is_open = 0;
2953

S
Stanislaw Gruszka 已提交
2954
	il4965_down(il);
2955

S
Stanislaw Gruszka 已提交
2956
	flush_workqueue(il->workqueue);
2957

2958 2959
	/* User space software may expect getting rfkill changes
	 * even if interface is down */
2960
	_il_wr(il, CSR_INT, 0xFFFFFFFF);
S
Stanislaw Gruszka 已提交
2961
	il_enable_rfkill_int(il);
2962

2963
	D_MAC80211("leave\n");
2964 2965
}

S
Stanislaw Gruszka 已提交
2966
void il4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2967
{
S
Stanislaw Gruszka 已提交
2968
	struct il_priv *il = hw->priv;
2969

2970
	D_MACDUMP("enter\n");
2971

2972
	D_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
2973 2974
		     ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);

S
Stanislaw Gruszka 已提交
2975
	if (il4965_tx_skb(il, skb))
2976 2977
		dev_kfree_skb_any(skb);

2978
	D_MACDUMP("leave\n");
2979 2980
}

S
Stanislaw Gruszka 已提交
2981
void il4965_mac_update_tkip_key(struct ieee80211_hw *hw,
2982 2983 2984 2985 2986
				struct ieee80211_vif *vif,
				struct ieee80211_key_conf *keyconf,
				struct ieee80211_sta *sta,
				u32 iv32, u16 *phase1key)
{
S
Stanislaw Gruszka 已提交
2987
	struct il_priv *il = hw->priv;
S
Stanislaw Gruszka 已提交
2988
	struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
2989

2990
	D_MAC80211("enter\n");
2991

S
Stanislaw Gruszka 已提交
2992
	il4965_update_tkip_key(il, vif_priv->ctx, keyconf, sta,
2993 2994
			    iv32, phase1key);

2995
	D_MAC80211("leave\n");
2996 2997
}

S
Stanislaw Gruszka 已提交
2998
int il4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2999 3000 3001
		       struct ieee80211_vif *vif, struct ieee80211_sta *sta,
		       struct ieee80211_key_conf *key)
{
S
Stanislaw Gruszka 已提交
3002
	struct il_priv *il = hw->priv;
S
Stanislaw Gruszka 已提交
3003 3004
	struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
	struct il_rxon_context *ctx = vif_priv->ctx;
3005 3006 3007 3008
	int ret;
	u8 sta_id;
	bool is_default_wep_key = false;

3009
	D_MAC80211("enter\n");
3010

S
Stanislaw Gruszka 已提交
3011
	if (il->cfg->mod_params->sw_crypto) {
3012
		D_MAC80211("leave - hwcrypto disabled\n");
3013 3014 3015
		return -EOPNOTSUPP;
	}

S
Stanislaw Gruszka 已提交
3016
	sta_id = il_sta_id_or_broadcast(il, vif_priv->ctx, sta);
S
Stanislaw Gruszka 已提交
3017
	if (sta_id == IL_INVALID_STATION)
3018 3019
		return -EINVAL;

S
Stanislaw Gruszka 已提交
3020 3021
	mutex_lock(&il->mutex);
	il_scan_cancel_timeout(il, 100);
3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041

	/*
	 * If we are getting WEP group key and we didn't receive any key mapping
	 * so far, we are in legacy wep mode (group key only), otherwise we are
	 * in 1X mode.
	 * In legacy wep mode, we use another host command to the uCode.
	 */
	if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
	     key->cipher == WLAN_CIPHER_SUITE_WEP104) &&
	    !sta) {
		if (cmd == SET_KEY)
			is_default_wep_key = !ctx->key_mapping_keys;
		else
			is_default_wep_key =
					(key->hw_key_idx == HW_KEY_DEFAULT);
	}

	switch (cmd) {
	case SET_KEY:
		if (is_default_wep_key)
S
Stanislaw Gruszka 已提交
3042
			ret = il4965_set_default_wep_key(il,
3043 3044
							vif_priv->ctx, key);
		else
S
Stanislaw Gruszka 已提交
3045
			ret = il4965_set_dynamic_key(il, vif_priv->ctx,
3046 3047
						  key, sta_id);

3048
		D_MAC80211("enable hwcrypto key\n");
3049 3050 3051
		break;
	case DISABLE_KEY:
		if (is_default_wep_key)
S
Stanislaw Gruszka 已提交
3052
			ret = il4965_remove_default_wep_key(il, ctx, key);
3053
		else
S
Stanislaw Gruszka 已提交
3054
			ret = il4965_remove_dynamic_key(il, ctx,
3055 3056
							key, sta_id);

3057
		D_MAC80211("disable hwcrypto key\n");
3058 3059 3060 3061 3062
		break;
	default:
		ret = -EINVAL;
	}

S
Stanislaw Gruszka 已提交
3063
	mutex_unlock(&il->mutex);
3064
	D_MAC80211("leave\n");
3065 3066 3067 3068

	return ret;
}

S
Stanislaw Gruszka 已提交
3069
int il4965_mac_ampdu_action(struct ieee80211_hw *hw,
3070 3071 3072 3073 3074
			    struct ieee80211_vif *vif,
			    enum ieee80211_ampdu_mlme_action action,
			    struct ieee80211_sta *sta, u16 tid, u16 *ssn,
			    u8 buf_size)
{
S
Stanislaw Gruszka 已提交
3075
	struct il_priv *il = hw->priv;
3076 3077
	int ret = -EINVAL;

3078
	D_HT("A-MPDU action on addr %pM tid %d\n",
3079 3080
		     sta->addr, tid);

S
Stanislaw Gruszka 已提交
3081
	if (!(il->cfg->sku & IL_SKU_N))
3082 3083
		return -EACCES;

S
Stanislaw Gruszka 已提交
3084
	mutex_lock(&il->mutex);
3085 3086 3087

	switch (action) {
	case IEEE80211_AMPDU_RX_START:
3088
		D_HT("start Rx\n");
S
Stanislaw Gruszka 已提交
3089
		ret = il4965_sta_rx_agg_start(il, sta, tid, *ssn);
3090 3091
		break;
	case IEEE80211_AMPDU_RX_STOP:
3092
		D_HT("stop Rx\n");
S
Stanislaw Gruszka 已提交
3093 3094
		ret = il4965_sta_rx_agg_stop(il, sta, tid);
		if (test_bit(STATUS_EXIT_PENDING, &il->status))
3095 3096 3097
			ret = 0;
		break;
	case IEEE80211_AMPDU_TX_START:
3098
		D_HT("start Tx\n");
S
Stanislaw Gruszka 已提交
3099
		ret = il4965_tx_agg_start(il, vif, sta, tid, ssn);
3100 3101
		break;
	case IEEE80211_AMPDU_TX_STOP:
3102
		D_HT("stop Tx\n");
S
Stanislaw Gruszka 已提交
3103 3104
		ret = il4965_tx_agg_stop(il, vif, sta, tid);
		if (test_bit(STATUS_EXIT_PENDING, &il->status))
3105 3106 3107 3108 3109 3110
			ret = 0;
		break;
	case IEEE80211_AMPDU_TX_OPERATIONAL:
		ret = 0;
		break;
	}
S
Stanislaw Gruszka 已提交
3111
	mutex_unlock(&il->mutex);
3112 3113 3114 3115

	return ret;
}

S
Stanislaw Gruszka 已提交
3116
int il4965_mac_sta_add(struct ieee80211_hw *hw,
3117 3118 3119
		       struct ieee80211_vif *vif,
		       struct ieee80211_sta *sta)
{
S
Stanislaw Gruszka 已提交
3120
	struct il_priv *il = hw->priv;
S
Stanislaw Gruszka 已提交
3121 3122
	struct il_station_priv *sta_priv = (void *)sta->drv_priv;
	struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
3123 3124 3125 3126
	bool is_ap = vif->type == NL80211_IFTYPE_STATION;
	int ret;
	u8 sta_id;

3127
	D_INFO("received request to add station %pM\n",
3128
			sta->addr);
S
Stanislaw Gruszka 已提交
3129
	mutex_lock(&il->mutex);
3130
	D_INFO("proceeding to add station %pM\n",
3131
			sta->addr);
S
Stanislaw Gruszka 已提交
3132
	sta_priv->common.sta_id = IL_INVALID_STATION;
3133 3134 3135

	atomic_set(&sta_priv->pending_frames, 0);

S
Stanislaw Gruszka 已提交
3136
	ret = il_add_station_common(il, vif_priv->ctx, sta->addr,
3137 3138
				     is_ap, sta, &sta_id);
	if (ret) {
3139
		IL_ERR("Unable to add station %pM (%d)\n",
3140 3141
			sta->addr, ret);
		/* Should we return success if return code is EEXIST ? */
S
Stanislaw Gruszka 已提交
3142
		mutex_unlock(&il->mutex);
3143 3144 3145 3146 3147 3148
		return ret;
	}

	sta_priv->common.sta_id = sta_id;

	/* Initialize rate scaling */
3149
	D_INFO("Initializing rate scaling for station %pM\n",
3150
		       sta->addr);
S
Stanislaw Gruszka 已提交
3151 3152
	il4965_rs_rate_init(il, sta, sta_id);
	mutex_unlock(&il->mutex);
3153 3154 3155 3156

	return 0;
}

S
Stanislaw Gruszka 已提交
3157
void il4965_mac_channel_switch(struct ieee80211_hw *hw,
3158 3159
			       struct ieee80211_channel_switch *ch_switch)
{
S
Stanislaw Gruszka 已提交
3160
	struct il_priv *il = hw->priv;
S
Stanislaw Gruszka 已提交
3161
	const struct il_channel_info *ch_info;
3162 3163
	struct ieee80211_conf *conf = &hw->conf;
	struct ieee80211_channel *channel = ch_switch->channel;
S
Stanislaw Gruszka 已提交
3164
	struct il_ht_config *ht_conf = &il->current_ht_config;
3165

3166
	struct il_rxon_context *ctx = &il->ctx;
3167 3168
	u16 ch;

3169
	D_MAC80211("enter\n");
3170

S
Stanislaw Gruszka 已提交
3171
	mutex_lock(&il->mutex);
3172

S
Stanislaw Gruszka 已提交
3173
	if (il_is_rfkill(il))
3174
		goto out;
3175

S
Stanislaw Gruszka 已提交
3176 3177 3178
	if (test_bit(STATUS_EXIT_PENDING, &il->status) ||
	    test_bit(STATUS_SCANNING, &il->status) ||
	    test_bit(STATUS_CHANNEL_SWITCH_PENDING, &il->status))
3179
		goto out;
3180

S
Stanislaw Gruszka 已提交
3181
	if (!il_is_associated_ctx(ctx))
3182
		goto out;
3183

S
Stanislaw Gruszka 已提交
3184
	if (!il->cfg->ops->lib->set_channel_switch)
3185
		goto out;
3186

3187 3188 3189 3190
	ch = channel->hw_value;
	if (le16_to_cpu(ctx->active.channel) == ch)
		goto out;

S
Stanislaw Gruszka 已提交
3191
	ch_info = il_get_channel_info(il, channel->band, ch);
S
Stanislaw Gruszka 已提交
3192
	if (!il_is_channel_valid(ch_info)) {
3193
		D_MAC80211("invalid channel\n");
3194 3195 3196
		goto out;
	}

S
Stanislaw Gruszka 已提交
3197
	spin_lock_irq(&il->lock);
3198

S
Stanislaw Gruszka 已提交
3199
	il->current_ht_config.smps = conf->smps_mode;
3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215

	/* Configure HT40 channels */
	ctx->ht.enabled = conf_is_ht(conf);
	if (ctx->ht.enabled) {
		if (conf_is_ht40_minus(conf)) {
			ctx->ht.extension_chan_offset =
				IEEE80211_HT_PARAM_CHA_SEC_BELOW;
			ctx->ht.is_40mhz = true;
		} else if (conf_is_ht40_plus(conf)) {
			ctx->ht.extension_chan_offset =
				IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
			ctx->ht.is_40mhz = true;
		} else {
			ctx->ht.extension_chan_offset =
				IEEE80211_HT_PARAM_CHA_SEC_NONE;
			ctx->ht.is_40mhz = false;
3216
		}
3217 3218 3219 3220 3221 3222
	} else
		ctx->ht.is_40mhz = false;

	if ((le16_to_cpu(ctx->staging.channel) != ch))
		ctx->staging.flags = 0;

S
Stanislaw Gruszka 已提交
3223 3224 3225
	il_set_rxon_channel(il, channel, ctx);
	il_set_rxon_ht(il, ht_conf);
	il_set_flags_for_band(il, ctx, channel->band, ctx->vif);
3226

S
Stanislaw Gruszka 已提交
3227
	spin_unlock_irq(&il->lock);
3228

S
Stanislaw Gruszka 已提交
3229
	il_set_rate(il);
3230 3231 3232 3233
	/*
	 * at this point, staging_rxon has the
	 * configuration for channel switch
	 */
S
Stanislaw Gruszka 已提交
3234 3235 3236 3237 3238
	set_bit(STATUS_CHANNEL_SWITCH_PENDING, &il->status);
	il->switch_channel = cpu_to_le16(ch);
	if (il->cfg->ops->lib->set_channel_switch(il, ch_switch)) {
		clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &il->status);
		il->switch_channel = 0;
3239
		ieee80211_chswitch_done(ctx->vif, false);
3240
	}
3241

3242
out:
S
Stanislaw Gruszka 已提交
3243
	mutex_unlock(&il->mutex);
3244
	D_MAC80211("leave\n");
3245 3246
}

S
Stanislaw Gruszka 已提交
3247
void il4965_configure_filter(struct ieee80211_hw *hw,
3248 3249 3250 3251
			     unsigned int changed_flags,
			     unsigned int *total_flags,
			     u64 multicast)
{
S
Stanislaw Gruszka 已提交
3252
	struct il_priv *il = hw->priv;
3253 3254 3255 3256 3257 3258 3259 3260 3261
	__le32 filter_or = 0, filter_nand = 0;

#define CHK(test, flag)	do { \
	if (*total_flags & (test))		\
		filter_or |= (flag);		\
	else					\
		filter_nand |= (flag);		\
	} while (0)

3262
	D_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
3263 3264 3265 3266 3267 3268 3269 3270 3271
			changed_flags, *total_flags);

	CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
	/* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
	CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
	CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);

#undef CHK

S
Stanislaw Gruszka 已提交
3272
	mutex_lock(&il->mutex);
3273

3274 3275
	il->ctx.staging.filter_flags &= ~filter_nand;
	il->ctx.staging.filter_flags |= filter_or;
3276

3277 3278 3279 3280
	/*
	 * Not committing directly because hardware can perform a scan,
	 * but we'll eventually commit the filter flags change anyway.
	 */
3281

S
Stanislaw Gruszka 已提交
3282
	mutex_unlock(&il->mutex);
3283 3284 3285

	/*
	 * Receiving all multicast frames is always enabled by the
S
Stanislaw Gruszka 已提交
3286
	 * default flags setup in il_connection_init_rx_config()
3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299
	 * since we currently do not support programming multicast
	 * filters into the device.
	 */
	*total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
			FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
}

/*****************************************************************************
 *
 * driver setup and teardown
 *
 *****************************************************************************/

S
Stanislaw Gruszka 已提交
3300
static void il4965_bg_txpower_work(struct work_struct *work)
3301
{
S
Stanislaw Gruszka 已提交
3302
	struct il_priv *il = container_of(work, struct il_priv,
3303 3304
			txpower_work);

S
Stanislaw Gruszka 已提交
3305
	mutex_lock(&il->mutex);
3306

3307
	/* If a scan happened to start before we got here
S
Stanislaw Gruszka 已提交
3308
	 * then just return; the stats notification will
3309 3310
	 * kick off another scheduled work to compensate for
	 * any temperature delta we missed here. */
S
Stanislaw Gruszka 已提交
3311 3312
	if (test_bit(STATUS_EXIT_PENDING, &il->status) ||
	    test_bit(STATUS_SCANNING, &il->status))
3313
		goto out;
3314 3315 3316 3317

	/* Regardless of if we are associated, we must reconfigure the
	 * TX power since frames can be sent on non-radar channels while
	 * not associated */
S
Stanislaw Gruszka 已提交
3318
	il->cfg->ops->lib->send_tx_power(il);
3319 3320 3321

	/* Update last_temperature to keep is_calib_needed from running
	 * when it isn't needed... */
S
Stanislaw Gruszka 已提交
3322
	il->last_temperature = il->temperature;
3323
out:
S
Stanislaw Gruszka 已提交
3324
	mutex_unlock(&il->mutex);
3325 3326
}

S
Stanislaw Gruszka 已提交
3327
static void il4965_setup_deferred_work(struct il_priv *il)
3328
{
S
Stanislaw Gruszka 已提交
3329
	il->workqueue = create_singlethread_workqueue(DRV_NAME);
3330

S
Stanislaw Gruszka 已提交
3331
	init_waitqueue_head(&il->wait_command_queue);
3332

S
Stanislaw Gruszka 已提交
3333 3334 3335 3336 3337
	INIT_WORK(&il->restart, il4965_bg_restart);
	INIT_WORK(&il->rx_replenish, il4965_bg_rx_replenish);
	INIT_WORK(&il->run_time_calib_work, il4965_bg_run_time_calib_work);
	INIT_DELAYED_WORK(&il->init_alive_start, il4965_bg_init_alive_start);
	INIT_DELAYED_WORK(&il->alive_start, il4965_bg_alive_start);
3338

S
Stanislaw Gruszka 已提交
3339
	il_setup_scan_deferred_work(il);
3340

S
Stanislaw Gruszka 已提交
3341
	INIT_WORK(&il->txpower_work, il4965_bg_txpower_work);
3342

S
Stanislaw Gruszka 已提交
3343 3344 3345
	init_timer(&il->stats_periodic);
	il->stats_periodic.data = (unsigned long)il;
	il->stats_periodic.function = il4965_bg_stats_periodic;
3346

S
Stanislaw Gruszka 已提交
3347 3348 3349
	init_timer(&il->watchdog);
	il->watchdog.data = (unsigned long)il;
	il->watchdog.function = il_bg_watchdog;
3350

S
Stanislaw Gruszka 已提交
3351 3352
	tasklet_init(&il->irq_tasklet, (void (*)(unsigned long))
		il4965_irq_tasklet, (unsigned long)il);
3353 3354
}

S
Stanislaw Gruszka 已提交
3355
static void il4965_cancel_deferred_work(struct il_priv *il)
3356
{
S
Stanislaw Gruszka 已提交
3357 3358 3359 3360
	cancel_work_sync(&il->txpower_work);
	cancel_delayed_work_sync(&il->init_alive_start);
	cancel_delayed_work(&il->alive_start);
	cancel_work_sync(&il->run_time_calib_work);
3361

S
Stanislaw Gruszka 已提交
3362
	il_cancel_scan_deferred_work(il);
3363

S
Stanislaw Gruszka 已提交
3364
	del_timer_sync(&il->stats_periodic);
3365 3366
}

S
Stanislaw Gruszka 已提交
3367
static void il4965_init_hw_rates(struct il_priv *il,
3368 3369 3370 3371
			      struct ieee80211_rate *rates)
{
	int i;

S
Stanislaw Gruszka 已提交
3372
	for (i = 0; i < RATE_COUNT_LEGACY; i++) {
3373
		rates[i].bitrate = il_rates[i].ieee * 5;
S
Stanislaw Gruszka 已提交
3374
		rates[i].hw_value = i; /* Rate scaling will work on idxes */
3375 3376
		rates[i].hw_value_short = i;
		rates[i].flags = 0;
S
Stanislaw Gruszka 已提交
3377
		if ((i >= IL_FIRST_CCK_RATE) && (i <= IL_LAST_CCK_RATE)) {
3378 3379 3380 3381
			/*
			 * If CCK != 1M then set short preamble rate flag.
			 */
			rates[i].flags |=
S
Stanislaw Gruszka 已提交
3382
				(il_rates[i].plcp == RATE_1M_PLCP) ?
3383 3384 3385 3386 3387
					0 : IEEE80211_RATE_SHORT_PREAMBLE;
		}
	}
}
/*
S
Stanislaw Gruszka 已提交
3388
 * Acquire il->lock before calling this function !
3389
 */
S
Stanislaw Gruszka 已提交
3390
void il4965_set_wr_ptrs(struct il_priv *il, int txq_id, u32 idx)
3391
{
3392
	il_wr(il, HBUS_TARG_WRPTR,
S
Stanislaw Gruszka 已提交
3393 3394
			     (idx & 0xff) | (txq_id << 8));
	il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(txq_id), idx);
3395 3396
}

S
Stanislaw Gruszka 已提交
3397
void il4965_tx_queue_set_status(struct il_priv *il,
S
Stanislaw Gruszka 已提交
3398
					struct il_tx_queue *txq,
3399 3400 3401 3402 3403
					int tx_fifo_id, int scd_retry)
{
	int txq_id = txq->q.id;

	/* Find out whether to activate Tx queue */
S
Stanislaw Gruszka 已提交
3404
	int active = test_bit(txq_id, &il->txq_ctx_active_msk) ? 1 : 0;
3405 3406

	/* Set up and activate */
3407 3408 3409 3410 3411 3412
	il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
			 (active << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
			 (tx_fifo_id << IL49_SCD_QUEUE_STTS_REG_POS_TXF) |
			 (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_WSL) |
			 (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
			 IL49_SCD_QUEUE_STTS_REG_MSK);
3413 3414 3415

	txq->sched_retry = scd_retry;

3416
	D_INFO("%s %s Queue %d on AC %d\n",
3417 3418 3419 3420 3421
		       active ? "Activate" : "Deactivate",
		       scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
}


S
Stanislaw Gruszka 已提交
3422
static int il4965_init_drv(struct il_priv *il)
3423 3424 3425
{
	int ret;

S
Stanislaw Gruszka 已提交
3426 3427
	spin_lock_init(&il->sta_lock);
	spin_lock_init(&il->hcmd_lock);
3428

S
Stanislaw Gruszka 已提交
3429
	INIT_LIST_HEAD(&il->free_frames);
3430

S
Stanislaw Gruszka 已提交
3431
	mutex_init(&il->mutex);
3432

S
Stanislaw Gruszka 已提交
3433 3434 3435
	il->ieee_channels = NULL;
	il->ieee_rates = NULL;
	il->band = IEEE80211_BAND_2GHZ;
3436

S
Stanislaw Gruszka 已提交
3437 3438 3439
	il->iw_mode = NL80211_IFTYPE_STATION;
	il->current_ht_config.smps = IEEE80211_SMPS_STATIC;
	il->missed_beacon_threshold = IL_MISSED_BEACON_THRESHOLD_DEF;
3440 3441

	/* initialize force reset */
S
Stanislaw Gruszka 已提交
3442
	il->force_reset.reset_duration = IL_DELAY_NEXT_FORCE_FW_RELOAD;
3443 3444

	/* Choose which receivers/antennas to use */
S
Stanislaw Gruszka 已提交
3445 3446
	if (il->cfg->ops->hcmd->set_rxon_chain)
		il->cfg->ops->hcmd->set_rxon_chain(il,
3447
					&il->ctx);
3448

S
Stanislaw Gruszka 已提交
3449
	il_init_scan_params(il);
3450

S
Stanislaw Gruszka 已提交
3451
	ret = il_init_channel_map(il);
3452
	if (ret) {
3453
		IL_ERR("initializing regulatory failed: %d\n", ret);
3454 3455 3456
		goto err;
	}

S
Stanislaw Gruszka 已提交
3457
	ret = il_init_geos(il);
3458
	if (ret) {
3459
		IL_ERR("initializing geos failed: %d\n", ret);
3460 3461
		goto err_free_channel_map;
	}
S
Stanislaw Gruszka 已提交
3462
	il4965_init_hw_rates(il, il->ieee_rates);
3463 3464 3465 3466

	return 0;

err_free_channel_map:
S
Stanislaw Gruszka 已提交
3467
	il_free_channel_map(il);
3468 3469 3470 3471
err:
	return ret;
}

S
Stanislaw Gruszka 已提交
3472
static void il4965_uninit_drv(struct il_priv *il)
3473
{
S
Stanislaw Gruszka 已提交
3474 3475 3476 3477
	il4965_calib_free_results(il);
	il_free_geos(il);
	il_free_channel_map(il);
	kfree(il->scan_cmd);
3478 3479
}

S
Stanislaw Gruszka 已提交
3480
static void il4965_hw_detect(struct il_priv *il)
3481
{
3482 3483
	il->hw_rev = _il_rd(il, CSR_HW_REV);
	il->hw_wa_rev = _il_rd(il, CSR_HW_REV_WA_REG);
S
Stanislaw Gruszka 已提交
3484
	il->rev_id = il->pci_dev->revision;
3485
	D_INFO("HW Revision ID = 0x%X\n", il->rev_id);
3486 3487
}

S
Stanislaw Gruszka 已提交
3488
static int il4965_set_hw_params(struct il_priv *il)
3489
{
S
Stanislaw Gruszka 已提交
3490 3491 3492 3493
	il->hw_params.max_rxq_size = RX_QUEUE_SIZE;
	il->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
	if (il->cfg->mod_params->amsdu_size_8K)
		il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_8K);
3494
	else
S
Stanislaw Gruszka 已提交
3495
		il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_4K);
3496

S
Stanislaw Gruszka 已提交
3497
	il->hw_params.max_beacon_itrvl = IL_MAX_UCODE_BEACON_INTERVAL;
3498

S
Stanislaw Gruszka 已提交
3499 3500
	if (il->cfg->mod_params->disable_11n)
		il->cfg->sku &= ~IL_SKU_N;
3501 3502

	/* Device-specific setup */
S
Stanislaw Gruszka 已提交
3503
	return il->cfg->ops->lib->set_hw_params(il);
3504 3505
}

S
Stanislaw Gruszka 已提交
3506 3507 3508 3509 3510
static const u8 il4965_bss_ac_to_fifo[] = {
	IL_TX_FIFO_VO,
	IL_TX_FIFO_VI,
	IL_TX_FIFO_BE,
	IL_TX_FIFO_BK,
3511 3512
};

S
Stanislaw Gruszka 已提交
3513
static const u8 il4965_bss_ac_to_queue[] = {
3514 3515 3516 3517
	0, 1, 2, 3,
};

static int
S
Stanislaw Gruszka 已提交
3518
il4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3519
{
3520
	int err = 0;
S
Stanislaw Gruszka 已提交
3521
	struct il_priv *il;
3522
	struct ieee80211_hw *hw;
S
Stanislaw Gruszka 已提交
3523
	struct il_cfg *cfg = (struct il_cfg *)(ent->driver_data);
3524 3525 3526 3527 3528 3529 3530
	unsigned long flags;
	u16 pci_cmd;

	/************************
	 * 1. Allocating HW data
	 ************************/

S
Stanislaw Gruszka 已提交
3531
	hw = il_alloc_all(cfg);
3532 3533 3534 3535
	if (!hw) {
		err = -ENOMEM;
		goto out;
	}
S
Stanislaw Gruszka 已提交
3536 3537
	il = hw->priv;
	/* At this point both hw and il are allocated. */
3538

3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551
	il->ctx.ctxid = 0;

	il->ctx.always_active = true;
	il->ctx.is_active = true;
	il->ctx.rxon_cmd = REPLY_RXON;
	il->ctx.rxon_timing_cmd = REPLY_RXON_TIMING;
	il->ctx.rxon_assoc_cmd = REPLY_RXON_ASSOC;
	il->ctx.qos_cmd = REPLY_QOS_PARAM;
	il->ctx.ap_sta_id = IL_AP_ID;
	il->ctx.wep_key_cmd = REPLY_WEPKEY;
	il->ctx.ac_to_fifo = il4965_bss_ac_to_fifo;
	il->ctx.ac_to_queue = il4965_bss_ac_to_queue;
	il->ctx.exclusive_interface_modes =
3552
		BIT(NL80211_IFTYPE_ADHOC);
3553
	il->ctx.interface_modes =
3554
		BIT(NL80211_IFTYPE_STATION);
3555 3556 3557 3558
	il->ctx.ap_devtype = RXON_DEV_TYPE_AP;
	il->ctx.ibss_devtype = RXON_DEV_TYPE_IBSS;
	il->ctx.station_devtype = RXON_DEV_TYPE_ESS;
	il->ctx.unused_devtype = RXON_DEV_TYPE_ESS;
3559 3560 3561

	SET_IEEE80211_DEV(hw, &pdev->dev);

3562
	D_INFO("*** LOAD DRIVER ***\n");
S
Stanislaw Gruszka 已提交
3563 3564 3565
	il->cfg = cfg;
	il->pci_dev = pdev;
	il->inta_mask = CSR_INI_SET_MASK;
3566

S
Stanislaw Gruszka 已提交
3567
	if (il_alloc_traffic_mem(il))
3568
		IL_ERR("Not enough memory to generate traffic log\n");
3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592

	/**************************
	 * 2. Initializing PCI bus
	 **************************/
	pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
				PCIE_LINK_STATE_CLKPM);

	if (pci_enable_device(pdev)) {
		err = -ENODEV;
		goto out_ieee80211_free_hw;
	}

	pci_set_master(pdev);

	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
	if (!err)
		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
	if (err) {
		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
		if (!err)
			err = pci_set_consistent_dma_mask(pdev,
							DMA_BIT_MASK(32));
		/* both attempts failed: */
		if (err) {
3593
			IL_WARN("No suitable DMA available.\n");
3594 3595 3596 3597 3598 3599 3600 3601
			goto out_pci_disable_device;
		}
	}

	err = pci_request_regions(pdev, DRV_NAME);
	if (err)
		goto out_pci_disable_device;

S
Stanislaw Gruszka 已提交
3602
	pci_set_drvdata(pdev, il);
3603 3604 3605 3606 3607


	/***********************
	 * 3. Read REV register
	 ***********************/
S
Stanislaw Gruszka 已提交
3608 3609
	il->hw_base = pci_iomap(pdev, 0, 0);
	if (!il->hw_base) {
3610 3611 3612 3613
		err = -ENODEV;
		goto out_pci_release_regions;
	}

3614
	D_INFO("pci_resource_len = 0x%08llx\n",
3615
		(unsigned long long) pci_resource_len(pdev, 0));
3616
	D_INFO("pci_resource_base = %p\n", il->hw_base);
3617 3618 3619 3620

	/* these spin locks will be used in apm_ops.init and EEPROM access
	 * we should init now
	 */
S
Stanislaw Gruszka 已提交
3621 3622
	spin_lock_init(&il->reg_lock);
	spin_lock_init(&il->lock);
3623 3624 3625 3626 3627 3628

	/*
	 * stop and reset the on-board processor just in case it is in a
	 * strange state ... like being left stranded by a primary kernel
	 * and this is now the kdump kernel trying to start up
	 */
3629
	_il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
3630

S
Stanislaw Gruszka 已提交
3631
	il4965_hw_detect(il);
3632
	IL_INFO("Detected %s, REV=0x%X\n",
S
Stanislaw Gruszka 已提交
3633
		il->cfg->name, il->hw_rev);
3634 3635 3636 3637 3638

	/* We disable the RETRY_TIMEOUT register (0x41) to keep
	 * PCI Tx retries from interfering with C3 CPU state */
	pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);

S
Stanislaw Gruszka 已提交
3639 3640
	il4965_prepare_card_hw(il);
	if (!il->hw_ready) {
3641
		IL_WARN("Failed, HW not ready\n");
3642 3643 3644 3645 3646 3647 3648
		goto out_iounmap;
	}

	/*****************
	 * 4. Read EEPROM
	 *****************/
	/* Read the EEPROM */
S
Stanislaw Gruszka 已提交
3649
	err = il_eeprom_init(il);
3650
	if (err) {
3651
		IL_ERR("Unable to init EEPROM\n");
3652 3653
		goto out_iounmap;
	}
S
Stanislaw Gruszka 已提交
3654
	err = il4965_eeprom_check_version(il);
3655 3656 3657 3658 3659 3660 3661
	if (err)
		goto out_free_eeprom;

	if (err)
		goto out_free_eeprom;

	/* extract MAC Address */
S
Stanislaw Gruszka 已提交
3662
	il4965_eeprom_get_mac(il, il->addresses[0].addr);
3663
	D_INFO("MAC address: %pM\n", il->addresses[0].addr);
S
Stanislaw Gruszka 已提交
3664 3665
	il->hw->wiphy->addresses = il->addresses;
	il->hw->wiphy->n_addresses = 1;
3666 3667 3668 3669

	/************************
	 * 5. Setup HW constants
	 ************************/
S
Stanislaw Gruszka 已提交
3670
	if (il4965_set_hw_params(il)) {
3671
		IL_ERR("failed to set hw parameters\n");
3672 3673 3674 3675
		goto out_free_eeprom;
	}

	/*******************
S
Stanislaw Gruszka 已提交
3676
	 * 6. Setup il
3677 3678
	 *******************/

S
Stanislaw Gruszka 已提交
3679
	err = il4965_init_drv(il);
3680 3681
	if (err)
		goto out_free_eeprom;
S
Stanislaw Gruszka 已提交
3682
	/* At this point both hw and il are initialized. */
3683 3684 3685 3686

	/********************
	 * 7. Setup services
	 ********************/
S
Stanislaw Gruszka 已提交
3687 3688 3689
	spin_lock_irqsave(&il->lock, flags);
	il_disable_interrupts(il);
	spin_unlock_irqrestore(&il->lock, flags);
3690

S
Stanislaw Gruszka 已提交
3691
	pci_enable_msi(il->pci_dev);
3692

S
Stanislaw Gruszka 已提交
3693 3694
	err = request_irq(il->pci_dev->irq, il_isr,
			  IRQF_SHARED, DRV_NAME, il);
3695
	if (err) {
3696
		IL_ERR("Error allocating IRQ %d\n", il->pci_dev->irq);
3697 3698 3699
		goto out_disable_msi;
	}

S
Stanislaw Gruszka 已提交
3700 3701
	il4965_setup_deferred_work(il);
	il4965_setup_rx_handlers(il);
3702 3703 3704 3705 3706

	/*********************************************
	 * 8. Enable interrupts and read RFKILL state
	 *********************************************/

3707
	/* enable rfkill interrupt: hw bug w/a */
S
Stanislaw Gruszka 已提交
3708
	pci_read_config_word(il->pci_dev, PCI_COMMAND, &pci_cmd);
3709 3710
	if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
		pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
S
Stanislaw Gruszka 已提交
3711
		pci_write_config_word(il->pci_dev, PCI_COMMAND, pci_cmd);
3712 3713
	}

S
Stanislaw Gruszka 已提交
3714
	il_enable_rfkill_int(il);
3715 3716

	/* If platform's RF_KILL switch is NOT set to KILL */
3717
	if (_il_rd(il, CSR_GP_CNTRL) &
3718
		CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
S
Stanislaw Gruszka 已提交
3719
		clear_bit(STATUS_RF_KILL_HW, &il->status);
3720
	else
S
Stanislaw Gruszka 已提交
3721
		set_bit(STATUS_RF_KILL_HW, &il->status);
3722

S
Stanislaw Gruszka 已提交
3723 3724
	wiphy_rfkill_set_hw_state(il->hw->wiphy,
		test_bit(STATUS_RF_KILL_HW, &il->status));
3725

S
Stanislaw Gruszka 已提交
3726
	il_power_initialize(il);
3727

S
Stanislaw Gruszka 已提交
3728
	init_completion(&il->_4965.firmware_loading_complete);
3729

S
Stanislaw Gruszka 已提交
3730
	err = il4965_request_firmware(il, true);
3731 3732 3733 3734 3735 3736
	if (err)
		goto out_destroy_workqueue;

	return 0;

 out_destroy_workqueue:
S
Stanislaw Gruszka 已提交
3737 3738 3739
	destroy_workqueue(il->workqueue);
	il->workqueue = NULL;
	free_irq(il->pci_dev->irq, il);
3740
 out_disable_msi:
S
Stanislaw Gruszka 已提交
3741 3742
	pci_disable_msi(il->pci_dev);
	il4965_uninit_drv(il);
3743
 out_free_eeprom:
S
Stanislaw Gruszka 已提交
3744
	il_eeprom_free(il);
3745
 out_iounmap:
S
Stanislaw Gruszka 已提交
3746
	pci_iounmap(pdev, il->hw_base);
3747 3748 3749 3750 3751 3752
 out_pci_release_regions:
	pci_set_drvdata(pdev, NULL);
	pci_release_regions(pdev);
 out_pci_disable_device:
	pci_disable_device(pdev);
 out_ieee80211_free_hw:
S
Stanislaw Gruszka 已提交
3753 3754
	il_free_traffic_mem(il);
	ieee80211_free_hw(il->hw);
3755 3756 3757 3758
 out:
	return err;
}

S
Stanislaw Gruszka 已提交
3759
static void __devexit il4965_pci_remove(struct pci_dev *pdev)
3760
{
S
Stanislaw Gruszka 已提交
3761
	struct il_priv *il = pci_get_drvdata(pdev);
3762 3763
	unsigned long flags;

S
Stanislaw Gruszka 已提交
3764
	if (!il)
3765 3766
		return;

S
Stanislaw Gruszka 已提交
3767
	wait_for_completion(&il->_4965.firmware_loading_complete);
3768

3769
	D_INFO("*** UNLOAD DRIVER ***\n");
3770

S
Stanislaw Gruszka 已提交
3771
	il_dbgfs_unregister(il);
S
Stanislaw Gruszka 已提交
3772
	sysfs_remove_group(&pdev->dev.kobj, &il_attribute_group);
3773

S
Stanislaw Gruszka 已提交
3774 3775
	/* ieee80211_unregister_hw call wil cause il_mac_stop to
	 * to be called and il4965_down since we are removing the device
3776 3777
	 * we need to set STATUS_EXIT_PENDING bit.
	 */
S
Stanislaw Gruszka 已提交
3778
	set_bit(STATUS_EXIT_PENDING, &il->status);
3779

S
Stanislaw Gruszka 已提交
3780
	il_leds_exit(il);
3781

S
Stanislaw Gruszka 已提交
3782 3783 3784
	if (il->mac80211_registered) {
		ieee80211_unregister_hw(il->hw);
		il->mac80211_registered = 0;
3785
	} else {
S
Stanislaw Gruszka 已提交
3786
		il4965_down(il);
3787 3788 3789 3790
	}

	/*
	 * Make sure device is reset to low power before unloading driver.
S
Stanislaw Gruszka 已提交
3791 3792 3793
	 * This may be redundant with il4965_down(), but there are paths to
	 * run il4965_down() without calling apm_ops.stop(), and there are
	 * paths to avoid running il4965_down() at all before leaving driver.
3794 3795
	 * This (inexpensive) call *makes sure* device is reset.
	 */
S
Stanislaw Gruszka 已提交
3796
	il_apm_stop(il);
3797 3798 3799 3800

	/* make sure we flush any pending irq or
	 * tasklet for the driver
	 */
S
Stanislaw Gruszka 已提交
3801 3802 3803
	spin_lock_irqsave(&il->lock, flags);
	il_disable_interrupts(il);
	spin_unlock_irqrestore(&il->lock, flags);
3804

S
Stanislaw Gruszka 已提交
3805
	il4965_synchronize_irq(il);
3806

S
Stanislaw Gruszka 已提交
3807
	il4965_dealloc_ucode_pci(il);
3808

S
Stanislaw Gruszka 已提交
3809 3810 3811
	if (il->rxq.bd)
		il4965_rx_queue_free(il, &il->rxq);
	il4965_hw_txq_ctx_free(il);
3812

S
Stanislaw Gruszka 已提交
3813
	il_eeprom_free(il);
3814 3815 3816


	/*netif_stop_queue(dev); */
S
Stanislaw Gruszka 已提交
3817
	flush_workqueue(il->workqueue);
3818

S
Stanislaw Gruszka 已提交
3819
	/* ieee80211_unregister_hw calls il_mac_stop, which flushes
S
Stanislaw Gruszka 已提交
3820
	 * il->workqueue... so we can't take down the workqueue
3821
	 * until now... */
S
Stanislaw Gruszka 已提交
3822 3823 3824
	destroy_workqueue(il->workqueue);
	il->workqueue = NULL;
	il_free_traffic_mem(il);
3825

S
Stanislaw Gruszka 已提交
3826 3827 3828
	free_irq(il->pci_dev->irq, il);
	pci_disable_msi(il->pci_dev);
	pci_iounmap(pdev, il->hw_base);
3829 3830 3831 3832
	pci_release_regions(pdev);
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);

S
Stanislaw Gruszka 已提交
3833
	il4965_uninit_drv(il);
3834

S
Stanislaw Gruszka 已提交
3835
	dev_kfree_skb(il->beacon_skb);
3836

S
Stanislaw Gruszka 已提交
3837
	ieee80211_free_hw(il->hw);
3838 3839 3840 3841
}

/*
 * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
S
Stanislaw Gruszka 已提交
3842
 * must be called under il->lock and mac access
3843
 */
S
Stanislaw Gruszka 已提交
3844
void il4965_txq_set_sched(struct il_priv *il, u32 mask)
3845
{
3846
	il_wr_prph(il, IL49_SCD_TXFACT, mask);
3847 3848 3849 3850 3851 3852 3853 3854 3855
}

/*****************************************************************************
 *
 * driver and module entry point
 *
 *****************************************************************************/

/* Hardware specific file defines the PCI IDs table for that hardware module */
S
Stanislaw Gruszka 已提交
3856 3857 3858
static DEFINE_PCI_DEVICE_TABLE(il4965_hw_card_ids) = {
	{IL_PCI_DEVICE(0x4229, PCI_ANY_ID, il4965_cfg)},
	{IL_PCI_DEVICE(0x4230, PCI_ANY_ID, il4965_cfg)},
3859 3860
	{0}
};
S
Stanislaw Gruszka 已提交
3861
MODULE_DEVICE_TABLE(pci, il4965_hw_card_ids);
3862

S
Stanislaw Gruszka 已提交
3863
static struct pci_driver il4965_driver = {
3864
	.name = DRV_NAME,
S
Stanislaw Gruszka 已提交
3865 3866 3867 3868
	.id_table = il4965_hw_card_ids,
	.probe = il4965_pci_probe,
	.remove = __devexit_p(il4965_pci_remove),
	.driver.pm = IL_LEGACY_PM_OPS,
3869 3870
};

S
Stanislaw Gruszka 已提交
3871
static int __init il4965_init(void)
3872 3873 3874 3875 3876 3877
{

	int ret;
	pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
	pr_info(DRV_COPYRIGHT "\n");

S
Stanislaw Gruszka 已提交
3878
	ret = il4965_rate_control_register();
3879 3880 3881 3882 3883
	if (ret) {
		pr_err("Unable to register rate control algorithm: %d\n", ret);
		return ret;
	}

S
Stanislaw Gruszka 已提交
3884
	ret = pci_register_driver(&il4965_driver);
3885 3886 3887 3888 3889 3890 3891 3892
	if (ret) {
		pr_err("Unable to initialize PCI module\n");
		goto error_register;
	}

	return ret;

error_register:
S
Stanislaw Gruszka 已提交
3893
	il4965_rate_control_unregister();
3894 3895 3896
	return ret;
}

S
Stanislaw Gruszka 已提交
3897
static void __exit il4965_exit(void)
3898
{
S
Stanislaw Gruszka 已提交
3899 3900
	pci_unregister_driver(&il4965_driver);
	il4965_rate_control_unregister();
3901 3902
}

S
Stanislaw Gruszka 已提交
3903 3904
module_exit(il4965_exit);
module_init(il4965_init);
3905

3906
#ifdef CONFIG_IWLEGACY_DEBUG
3907
module_param_named(debug, il_debug_level, uint, S_IRUGO | S_IWUSR);
3908 3909 3910
MODULE_PARM_DESC(debug, "debug output mask");
#endif

S
Stanislaw Gruszka 已提交
3911
module_param_named(swcrypto, il4965_mod_params.sw_crypto, int, S_IRUGO);
3912
MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
S
Stanislaw Gruszka 已提交
3913
module_param_named(queues_num, il4965_mod_params.num_of_queues, int, S_IRUGO);
3914
MODULE_PARM_DESC(queues_num, "number of hw queues.");
S
Stanislaw Gruszka 已提交
3915
module_param_named(11n_disable, il4965_mod_params.disable_11n, int, S_IRUGO);
3916
MODULE_PARM_DESC(11n_disable, "disable 11n functionality");
S
Stanislaw Gruszka 已提交
3917
module_param_named(amsdu_size_8K, il4965_mod_params.amsdu_size_8K,
3918 3919
		   int, S_IRUGO);
MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
S
Stanislaw Gruszka 已提交
3920
module_param_named(fw_restart, il4965_mod_params.restart_fw, int, S_IRUGO);
3921
MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");