18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/******************************************************************************
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Portions of this file are derived from the ipw3945 project, as well
78c2ecf20Sopenharmony_ci * as portions of the ieee80211 subsystem header files.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Contact Information:
108c2ecf20Sopenharmony_ci *  Intel Linux Wireless <ilw@linux.intel.com>
118c2ecf20Sopenharmony_ci * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci *****************************************************************************/
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/kernel.h>
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci#include <linux/init.h>
208c2ecf20Sopenharmony_ci#include <linux/pci.h>
218c2ecf20Sopenharmony_ci#include <linux/slab.h>
228c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
238c2ecf20Sopenharmony_ci#include <linux/delay.h>
248c2ecf20Sopenharmony_ci#include <linux/sched.h>
258c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
268c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
278c2ecf20Sopenharmony_ci#include <linux/firmware.h>
288c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
298c2ecf20Sopenharmony_ci#include <linux/if_arp.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <net/ieee80211_radiotap.h>
328c2ecf20Sopenharmony_ci#include <net/mac80211.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#include <asm/div64.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define DRV_NAME	"iwl3945"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#include "commands.h"
398c2ecf20Sopenharmony_ci#include "common.h"
408c2ecf20Sopenharmony_ci#include "3945.h"
418c2ecf20Sopenharmony_ci#include "iwl-spectrum.h"
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/*
448c2ecf20Sopenharmony_ci * module name, copyright, version, etc.
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define DRV_DESCRIPTION	\
488c2ecf20Sopenharmony_ci"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLEGACY_DEBUG
518c2ecf20Sopenharmony_ci#define VD "d"
528c2ecf20Sopenharmony_ci#else
538c2ecf20Sopenharmony_ci#define VD
548c2ecf20Sopenharmony_ci#endif
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/*
578c2ecf20Sopenharmony_ci * add "s" to indicate spectrum measurement included.
588c2ecf20Sopenharmony_ci * we add it here to be consistent with previous releases in which
598c2ecf20Sopenharmony_ci * this was configurable.
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_ci#define DRV_VERSION  IWLWIFI_VERSION VD "s"
628c2ecf20Sopenharmony_ci#define DRV_COPYRIGHT	"Copyright(c) 2003-2011 Intel Corporation"
638c2ecf20Sopenharmony_ci#define DRV_AUTHOR     "<ilw@linux.intel.com>"
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRV_DESCRIPTION);
668c2ecf20Sopenharmony_ciMODULE_VERSION(DRV_VERSION);
678c2ecf20Sopenharmony_ciMODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
688c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci /* module parameters */
718c2ecf20Sopenharmony_cistruct il_mod_params il3945_mod_params = {
728c2ecf20Sopenharmony_ci	.sw_crypto = 1,
738c2ecf20Sopenharmony_ci	.restart_fw = 1,
748c2ecf20Sopenharmony_ci	.disable_hw_scan = 1,
758c2ecf20Sopenharmony_ci	/* the rest are 0 by default */
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/**
798c2ecf20Sopenharmony_ci * il3945_get_antenna_flags - Get antenna flags for RXON command
808c2ecf20Sopenharmony_ci * @il: eeprom and antenna fields are used to determine antenna flags
818c2ecf20Sopenharmony_ci *
828c2ecf20Sopenharmony_ci * il->eeprom39  is used to determine if antenna AUX/MAIN are reversed
838c2ecf20Sopenharmony_ci * il3945_mod_params.antenna specifies the antenna diversity mode:
848c2ecf20Sopenharmony_ci *
858c2ecf20Sopenharmony_ci * IL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
868c2ecf20Sopenharmony_ci * IL_ANTENNA_MAIN      - Force MAIN antenna
878c2ecf20Sopenharmony_ci * IL_ANTENNA_AUX       - Force AUX antenna
888c2ecf20Sopenharmony_ci */
898c2ecf20Sopenharmony_ci__le32
908c2ecf20Sopenharmony_ciil3945_get_antenna_flags(const struct il_priv *il)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct il3945_eeprom *eeprom = (struct il3945_eeprom *)il->eeprom;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	switch (il3945_mod_params.antenna) {
958c2ecf20Sopenharmony_ci	case IL_ANTENNA_DIVERSITY:
968c2ecf20Sopenharmony_ci		return 0;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	case IL_ANTENNA_MAIN:
998c2ecf20Sopenharmony_ci		if (eeprom->antenna_switch_type)
1008c2ecf20Sopenharmony_ci			return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
1018c2ecf20Sopenharmony_ci		return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	case IL_ANTENNA_AUX:
1048c2ecf20Sopenharmony_ci		if (eeprom->antenna_switch_type)
1058c2ecf20Sopenharmony_ci			return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
1068c2ecf20Sopenharmony_ci		return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	/* bad antenna selector value */
1108c2ecf20Sopenharmony_ci	IL_ERR("Bad antenna selector value (0x%x)\n",
1118c2ecf20Sopenharmony_ci	       il3945_mod_params.antenna);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return 0;		/* "diversity" is default if error */
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic int
1178c2ecf20Sopenharmony_ciil3945_set_ccmp_dynamic_key_info(struct il_priv *il,
1188c2ecf20Sopenharmony_ci				 struct ieee80211_key_conf *keyconf, u8 sta_id)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	unsigned long flags;
1218c2ecf20Sopenharmony_ci	__le16 key_flags = 0;
1228c2ecf20Sopenharmony_ci	int ret;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
1258c2ecf20Sopenharmony_ci	key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	if (sta_id == il->hw_params.bcast_id)
1288c2ecf20Sopenharmony_ci		key_flags |= STA_KEY_MULTICAST_MSK;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1318c2ecf20Sopenharmony_ci	keyconf->hw_key_idx = keyconf->keyidx;
1328c2ecf20Sopenharmony_ci	key_flags &= ~STA_KEY_FLG_INVALID;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	spin_lock_irqsave(&il->sta_lock, flags);
1358c2ecf20Sopenharmony_ci	il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
1368c2ecf20Sopenharmony_ci	il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1378c2ecf20Sopenharmony_ci	memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	memcpy(il->stations[sta_id].sta.key.key, keyconf->key, keyconf->keylen);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	if ((il->stations[sta_id].sta.key.
1428c2ecf20Sopenharmony_ci	     key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
1438c2ecf20Sopenharmony_ci		il->stations[sta_id].sta.key.key_offset =
1448c2ecf20Sopenharmony_ci		    il_get_free_ucode_key_idx(il);
1458c2ecf20Sopenharmony_ci	/* else, we are overriding an existing key => no need to allocated room
1468c2ecf20Sopenharmony_ci	 * in uCode. */
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
1498c2ecf20Sopenharmony_ci	     "no space for a new key");
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	il->stations[sta_id].sta.key.key_flags = key_flags;
1528c2ecf20Sopenharmony_ci	il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1538c2ecf20Sopenharmony_ci	il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	D_INFO("hwcrypto: modify ucode station key info\n");
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	ret = il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&il->sta_lock, flags);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	return ret;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic int
1658c2ecf20Sopenharmony_ciil3945_set_tkip_dynamic_key_info(struct il_priv *il,
1668c2ecf20Sopenharmony_ci				 struct ieee80211_key_conf *keyconf, u8 sta_id)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic int
1728c2ecf20Sopenharmony_ciil3945_set_wep_dynamic_key_info(struct il_priv *il,
1738c2ecf20Sopenharmony_ci				struct ieee80211_key_conf *keyconf, u8 sta_id)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic int
1798c2ecf20Sopenharmony_ciil3945_clear_sta_key_info(struct il_priv *il, u8 sta_id)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	unsigned long flags;
1828c2ecf20Sopenharmony_ci	struct il_addsta_cmd sta_cmd;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	spin_lock_irqsave(&il->sta_lock, flags);
1858c2ecf20Sopenharmony_ci	memset(&il->stations[sta_id].keyinfo, 0, sizeof(struct il_hw_key));
1868c2ecf20Sopenharmony_ci	memset(&il->stations[sta_id].sta.key, 0, sizeof(struct il4965_keyinfo));
1878c2ecf20Sopenharmony_ci	il->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1888c2ecf20Sopenharmony_ci	il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1898c2ecf20Sopenharmony_ci	il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1908c2ecf20Sopenharmony_ci	memcpy(&sta_cmd, &il->stations[sta_id].sta,
1918c2ecf20Sopenharmony_ci	       sizeof(struct il_addsta_cmd));
1928c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&il->sta_lock, flags);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	D_INFO("hwcrypto: clear ucode station key info\n");
1958c2ecf20Sopenharmony_ci	return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic int
1998c2ecf20Sopenharmony_ciil3945_set_dynamic_key(struct il_priv *il, struct ieee80211_key_conf *keyconf,
2008c2ecf20Sopenharmony_ci		       u8 sta_id)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	int ret = 0;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	keyconf->hw_key_idx = HW_KEY_DYNAMIC;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	switch (keyconf->cipher) {
2078c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_CCMP:
2088c2ecf20Sopenharmony_ci		ret = il3945_set_ccmp_dynamic_key_info(il, keyconf, sta_id);
2098c2ecf20Sopenharmony_ci		break;
2108c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_TKIP:
2118c2ecf20Sopenharmony_ci		ret = il3945_set_tkip_dynamic_key_info(il, keyconf, sta_id);
2128c2ecf20Sopenharmony_ci		break;
2138c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_WEP40:
2148c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_WEP104:
2158c2ecf20Sopenharmony_ci		ret = il3945_set_wep_dynamic_key_info(il, keyconf, sta_id);
2168c2ecf20Sopenharmony_ci		break;
2178c2ecf20Sopenharmony_ci	default:
2188c2ecf20Sopenharmony_ci		IL_ERR("Unknown alg: %s alg=%x\n", __func__, keyconf->cipher);
2198c2ecf20Sopenharmony_ci		ret = -EINVAL;
2208c2ecf20Sopenharmony_ci	}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	D_WEP("Set dynamic key: alg=%x len=%d idx=%d sta=%d ret=%d\n",
2238c2ecf20Sopenharmony_ci	      keyconf->cipher, keyconf->keylen, keyconf->keyidx, sta_id, ret);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	return ret;
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic int
2298c2ecf20Sopenharmony_ciil3945_remove_static_key(struct il_priv *il)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic int
2358c2ecf20Sopenharmony_ciil3945_set_static_key(struct il_priv *il, struct ieee80211_key_conf *key)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2388c2ecf20Sopenharmony_ci	    key->cipher == WLAN_CIPHER_SUITE_WEP104)
2398c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	IL_ERR("Static key invalid: cipher %x\n", key->cipher);
2428c2ecf20Sopenharmony_ci	return -EINVAL;
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic void
2468c2ecf20Sopenharmony_ciil3945_clear_free_frames(struct il_priv *il)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	struct list_head *element;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	D_INFO("%d frames on pre-allocated heap on clear.\n", il->frames_count);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	while (!list_empty(&il->free_frames)) {
2538c2ecf20Sopenharmony_ci		element = il->free_frames.next;
2548c2ecf20Sopenharmony_ci		list_del(element);
2558c2ecf20Sopenharmony_ci		kfree(list_entry(element, struct il3945_frame, list));
2568c2ecf20Sopenharmony_ci		il->frames_count--;
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	if (il->frames_count) {
2608c2ecf20Sopenharmony_ci		IL_WARN("%d frames still in use.  Did we lose one?\n",
2618c2ecf20Sopenharmony_ci			il->frames_count);
2628c2ecf20Sopenharmony_ci		il->frames_count = 0;
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic struct il3945_frame *
2678c2ecf20Sopenharmony_ciil3945_get_free_frame(struct il_priv *il)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	struct il3945_frame *frame;
2708c2ecf20Sopenharmony_ci	struct list_head *element;
2718c2ecf20Sopenharmony_ci	if (list_empty(&il->free_frames)) {
2728c2ecf20Sopenharmony_ci		frame = kzalloc(sizeof(*frame), GFP_KERNEL);
2738c2ecf20Sopenharmony_ci		if (!frame) {
2748c2ecf20Sopenharmony_ci			IL_ERR("Could not allocate frame!\n");
2758c2ecf20Sopenharmony_ci			return NULL;
2768c2ecf20Sopenharmony_ci		}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci		il->frames_count++;
2798c2ecf20Sopenharmony_ci		return frame;
2808c2ecf20Sopenharmony_ci	}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	element = il->free_frames.next;
2838c2ecf20Sopenharmony_ci	list_del(element);
2848c2ecf20Sopenharmony_ci	return list_entry(element, struct il3945_frame, list);
2858c2ecf20Sopenharmony_ci}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_cistatic void
2888c2ecf20Sopenharmony_ciil3945_free_frame(struct il_priv *il, struct il3945_frame *frame)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	memset(frame, 0, sizeof(*frame));
2918c2ecf20Sopenharmony_ci	list_add(&frame->list, &il->free_frames);
2928c2ecf20Sopenharmony_ci}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ciunsigned int
2958c2ecf20Sopenharmony_ciil3945_fill_beacon_frame(struct il_priv *il, struct ieee80211_hdr *hdr,
2968c2ecf20Sopenharmony_ci			 int left)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	if (!il_is_associated(il) || !il->beacon_skb)
3008c2ecf20Sopenharmony_ci		return 0;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	if (il->beacon_skb->len > left)
3038c2ecf20Sopenharmony_ci		return 0;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	memcpy(hdr, il->beacon_skb->data, il->beacon_skb->len);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	return il->beacon_skb->len;
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic int
3118c2ecf20Sopenharmony_ciil3945_send_beacon_cmd(struct il_priv *il)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	struct il3945_frame *frame;
3148c2ecf20Sopenharmony_ci	unsigned int frame_size;
3158c2ecf20Sopenharmony_ci	int rc;
3168c2ecf20Sopenharmony_ci	u8 rate;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	frame = il3945_get_free_frame(il);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	if (!frame) {
3218c2ecf20Sopenharmony_ci		IL_ERR("Could not obtain free frame buffer for beacon "
3228c2ecf20Sopenharmony_ci		       "command.\n");
3238c2ecf20Sopenharmony_ci		return -ENOMEM;
3248c2ecf20Sopenharmony_ci	}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	rate = il_get_lowest_plcp(il);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	frame_size = il3945_hw_get_beacon_cmd(il, frame, rate);
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	rc = il_send_cmd_pdu(il, C_TX_BEACON, frame_size, &frame->u.cmd[0]);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	il3945_free_frame(il, frame);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	return rc;
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic void
3388c2ecf20Sopenharmony_ciil3945_unset_hw_params(struct il_priv *il)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	if (il->_3945.shared_virt)
3418c2ecf20Sopenharmony_ci		dma_free_coherent(&il->pci_dev->dev,
3428c2ecf20Sopenharmony_ci				  sizeof(struct il3945_shared),
3438c2ecf20Sopenharmony_ci				  il->_3945.shared_virt, il->_3945.shared_phys);
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic void
3478c2ecf20Sopenharmony_ciil3945_build_tx_cmd_hwcrypto(struct il_priv *il, struct ieee80211_tx_info *info,
3488c2ecf20Sopenharmony_ci			     struct il_device_cmd *cmd,
3498c2ecf20Sopenharmony_ci			     struct sk_buff *skb_frag, int sta_id)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	struct il3945_tx_cmd *tx_cmd = (struct il3945_tx_cmd *)cmd->cmd.payload;
3528c2ecf20Sopenharmony_ci	struct il_hw_key *keyinfo = &il->stations[sta_id].keyinfo;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	tx_cmd->sec_ctl = 0;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	switch (keyinfo->cipher) {
3578c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_CCMP:
3588c2ecf20Sopenharmony_ci		tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
3598c2ecf20Sopenharmony_ci		memcpy(tx_cmd->key, keyinfo->key, keyinfo->keylen);
3608c2ecf20Sopenharmony_ci		D_TX("tx_cmd with AES hwcrypto\n");
3618c2ecf20Sopenharmony_ci		break;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_TKIP:
3648c2ecf20Sopenharmony_ci		break;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_WEP104:
3678c2ecf20Sopenharmony_ci		tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
3688c2ecf20Sopenharmony_ci		fallthrough;
3698c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_WEP40:
3708c2ecf20Sopenharmony_ci		tx_cmd->sec_ctl |=
3718c2ecf20Sopenharmony_ci		    TX_CMD_SEC_WEP | (info->control.hw_key->
3728c2ecf20Sopenharmony_ci				      hw_key_idx & TX_CMD_SEC_MSK) <<
3738c2ecf20Sopenharmony_ci		    TX_CMD_SEC_SHIFT;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci		memcpy(&tx_cmd->key[3], keyinfo->key, keyinfo->keylen);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci		D_TX("Configuring packet for WEP encryption " "with key %d\n",
3788c2ecf20Sopenharmony_ci		     info->control.hw_key->hw_key_idx);
3798c2ecf20Sopenharmony_ci		break;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	default:
3828c2ecf20Sopenharmony_ci		IL_ERR("Unknown encode cipher %x\n", keyinfo->cipher);
3838c2ecf20Sopenharmony_ci		break;
3848c2ecf20Sopenharmony_ci	}
3858c2ecf20Sopenharmony_ci}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci/*
3888c2ecf20Sopenharmony_ci * handle build C_TX command notification.
3898c2ecf20Sopenharmony_ci */
3908c2ecf20Sopenharmony_cistatic void
3918c2ecf20Sopenharmony_ciil3945_build_tx_cmd_basic(struct il_priv *il, struct il_device_cmd *cmd,
3928c2ecf20Sopenharmony_ci			  struct ieee80211_tx_info *info,
3938c2ecf20Sopenharmony_ci			  struct ieee80211_hdr *hdr, u8 std_id)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	struct il3945_tx_cmd *tx_cmd = (struct il3945_tx_cmd *)cmd->cmd.payload;
3968c2ecf20Sopenharmony_ci	__le32 tx_flags = tx_cmd->tx_flags;
3978c2ecf20Sopenharmony_ci	__le16 fc = hdr->frame_control;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
4008c2ecf20Sopenharmony_ci	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
4018c2ecf20Sopenharmony_ci		tx_flags |= TX_CMD_FLG_ACK_MSK;
4028c2ecf20Sopenharmony_ci		if (ieee80211_is_mgmt(fc))
4038c2ecf20Sopenharmony_ci			tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
4048c2ecf20Sopenharmony_ci		if (ieee80211_is_probe_resp(fc) &&
4058c2ecf20Sopenharmony_ci		    !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
4068c2ecf20Sopenharmony_ci			tx_flags |= TX_CMD_FLG_TSF_MSK;
4078c2ecf20Sopenharmony_ci	} else {
4088c2ecf20Sopenharmony_ci		tx_flags &= (~TX_CMD_FLG_ACK_MSK);
4098c2ecf20Sopenharmony_ci		tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	tx_cmd->sta_id = std_id;
4138c2ecf20Sopenharmony_ci	if (ieee80211_has_morefrags(fc))
4148c2ecf20Sopenharmony_ci		tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	if (ieee80211_is_data_qos(fc)) {
4178c2ecf20Sopenharmony_ci		u8 *qc = ieee80211_get_qos_ctl(hdr);
4188c2ecf20Sopenharmony_ci		tx_cmd->tid_tspec = qc[0] & 0xf;
4198c2ecf20Sopenharmony_ci		tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
4208c2ecf20Sopenharmony_ci	} else {
4218c2ecf20Sopenharmony_ci		tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
4228c2ecf20Sopenharmony_ci	}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	il_tx_cmd_protection(il, info, fc, &tx_flags);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
4278c2ecf20Sopenharmony_ci	if (ieee80211_is_mgmt(fc)) {
4288c2ecf20Sopenharmony_ci		if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
4298c2ecf20Sopenharmony_ci			tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
4308c2ecf20Sopenharmony_ci		else
4318c2ecf20Sopenharmony_ci			tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
4328c2ecf20Sopenharmony_ci	} else {
4338c2ecf20Sopenharmony_ci		tx_cmd->timeout.pm_frame_timeout = 0;
4348c2ecf20Sopenharmony_ci	}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	tx_cmd->driver_txop = 0;
4378c2ecf20Sopenharmony_ci	tx_cmd->tx_flags = tx_flags;
4388c2ecf20Sopenharmony_ci	tx_cmd->next_frame_len = 0;
4398c2ecf20Sopenharmony_ci}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci/*
4428c2ecf20Sopenharmony_ci * start C_TX command process
4438c2ecf20Sopenharmony_ci */
4448c2ecf20Sopenharmony_cistatic int
4458c2ecf20Sopenharmony_ciil3945_tx_skb(struct il_priv *il,
4468c2ecf20Sopenharmony_ci	      struct ieee80211_sta *sta,
4478c2ecf20Sopenharmony_ci	      struct sk_buff *skb)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4508c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4518c2ecf20Sopenharmony_ci	struct il3945_tx_cmd *tx_cmd;
4528c2ecf20Sopenharmony_ci	struct il_tx_queue *txq = NULL;
4538c2ecf20Sopenharmony_ci	struct il_queue *q = NULL;
4548c2ecf20Sopenharmony_ci	struct il_device_cmd *out_cmd;
4558c2ecf20Sopenharmony_ci	struct il_cmd_meta *out_meta;
4568c2ecf20Sopenharmony_ci	dma_addr_t phys_addr;
4578c2ecf20Sopenharmony_ci	dma_addr_t txcmd_phys;
4588c2ecf20Sopenharmony_ci	int txq_id = skb_get_queue_mapping(skb);
4598c2ecf20Sopenharmony_ci	u16 len, idx, hdr_len;
4608c2ecf20Sopenharmony_ci	u16 firstlen, secondlen;
4618c2ecf20Sopenharmony_ci	u8 sta_id;
4628c2ecf20Sopenharmony_ci	u8 tid = 0;
4638c2ecf20Sopenharmony_ci	__le16 fc;
4648c2ecf20Sopenharmony_ci	u8 wait_write_ptr = 0;
4658c2ecf20Sopenharmony_ci	unsigned long flags;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	spin_lock_irqsave(&il->lock, flags);
4688c2ecf20Sopenharmony_ci	if (il_is_rfkill(il)) {
4698c2ecf20Sopenharmony_ci		D_DROP("Dropping - RF KILL\n");
4708c2ecf20Sopenharmony_ci		goto drop_unlock;
4718c2ecf20Sopenharmony_ci	}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	if ((ieee80211_get_tx_rate(il->hw, info)->hw_value & 0xFF) ==
4748c2ecf20Sopenharmony_ci	    IL_INVALID_RATE) {
4758c2ecf20Sopenharmony_ci		IL_ERR("ERROR: No TX rate available.\n");
4768c2ecf20Sopenharmony_ci		goto drop_unlock;
4778c2ecf20Sopenharmony_ci	}
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	fc = hdr->frame_control;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLEGACY_DEBUG
4828c2ecf20Sopenharmony_ci	if (ieee80211_is_auth(fc))
4838c2ecf20Sopenharmony_ci		D_TX("Sending AUTH frame\n");
4848c2ecf20Sopenharmony_ci	else if (ieee80211_is_assoc_req(fc))
4858c2ecf20Sopenharmony_ci		D_TX("Sending ASSOC frame\n");
4868c2ecf20Sopenharmony_ci	else if (ieee80211_is_reassoc_req(fc))
4878c2ecf20Sopenharmony_ci		D_TX("Sending REASSOC frame\n");
4888c2ecf20Sopenharmony_ci#endif
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&il->lock, flags);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	hdr_len = ieee80211_hdrlen(fc);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	/* Find idx into station table for destination station */
4958c2ecf20Sopenharmony_ci	sta_id = il_sta_id_or_broadcast(il, sta);
4968c2ecf20Sopenharmony_ci	if (sta_id == IL_INVALID_STATION) {
4978c2ecf20Sopenharmony_ci		D_DROP("Dropping - INVALID STATION: %pM\n", hdr->addr1);
4988c2ecf20Sopenharmony_ci		goto drop;
4998c2ecf20Sopenharmony_ci	}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	D_RATE("station Id %d\n", sta_id);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	if (ieee80211_is_data_qos(fc)) {
5048c2ecf20Sopenharmony_ci		u8 *qc = ieee80211_get_qos_ctl(hdr);
5058c2ecf20Sopenharmony_ci		tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
5068c2ecf20Sopenharmony_ci		if (unlikely(tid >= MAX_TID_COUNT))
5078c2ecf20Sopenharmony_ci			goto drop;
5088c2ecf20Sopenharmony_ci	}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	/* Descriptor for chosen Tx queue */
5118c2ecf20Sopenharmony_ci	txq = &il->txq[txq_id];
5128c2ecf20Sopenharmony_ci	q = &txq->q;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	if ((il_queue_space(q) < q->high_mark))
5158c2ecf20Sopenharmony_ci		goto drop;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	spin_lock_irqsave(&il->lock, flags);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	idx = il_get_cmd_idx(q, q->write_ptr, 0);
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	txq->skbs[q->write_ptr] = skb;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	/* Init first empty entry in queue's array of Tx/cmd buffers */
5248c2ecf20Sopenharmony_ci	out_cmd = txq->cmd[idx];
5258c2ecf20Sopenharmony_ci	out_meta = &txq->meta[idx];
5268c2ecf20Sopenharmony_ci	tx_cmd = (struct il3945_tx_cmd *)out_cmd->cmd.payload;
5278c2ecf20Sopenharmony_ci	memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
5288c2ecf20Sopenharmony_ci	memset(tx_cmd, 0, sizeof(*tx_cmd));
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	/*
5318c2ecf20Sopenharmony_ci	 * Set up the Tx-command (not MAC!) header.
5328c2ecf20Sopenharmony_ci	 * Store the chosen Tx queue and TFD idx within the sequence field;
5338c2ecf20Sopenharmony_ci	 * after Tx, uCode's Tx response will return this value so driver can
5348c2ecf20Sopenharmony_ci	 * locate the frame within the tx queue and do post-tx processing.
5358c2ecf20Sopenharmony_ci	 */
5368c2ecf20Sopenharmony_ci	out_cmd->hdr.cmd = C_TX;
5378c2ecf20Sopenharmony_ci	out_cmd->hdr.sequence =
5388c2ecf20Sopenharmony_ci	    cpu_to_le16((u16)
5398c2ecf20Sopenharmony_ci			(QUEUE_TO_SEQ(txq_id) | IDX_TO_SEQ(q->write_ptr)));
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	/* Copy MAC header from skb into command buffer */
5428c2ecf20Sopenharmony_ci	memcpy(tx_cmd->hdr, hdr, hdr_len);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	if (info->control.hw_key)
5458c2ecf20Sopenharmony_ci		il3945_build_tx_cmd_hwcrypto(il, info, out_cmd, skb, sta_id);
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	/* TODO need this for burst mode later on */
5488c2ecf20Sopenharmony_ci	il3945_build_tx_cmd_basic(il, out_cmd, info, hdr, sta_id);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	il3945_hw_build_tx_cmd_rate(il, out_cmd, info, hdr, sta_id);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	/* Total # bytes to be transmitted */
5538c2ecf20Sopenharmony_ci	tx_cmd->len = cpu_to_le16((u16) skb->len);
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
5568c2ecf20Sopenharmony_ci	tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	/*
5598c2ecf20Sopenharmony_ci	 * Use the first empty entry in this queue's command buffer array
5608c2ecf20Sopenharmony_ci	 * to contain the Tx command and MAC header concatenated together
5618c2ecf20Sopenharmony_ci	 * (payload data will be in another buffer).
5628c2ecf20Sopenharmony_ci	 * Size of this varies, due to varying MAC header length.
5638c2ecf20Sopenharmony_ci	 * If end is not dword aligned, we'll have 2 extra bytes at the end
5648c2ecf20Sopenharmony_ci	 * of the MAC header (device reads on dword boundaries).
5658c2ecf20Sopenharmony_ci	 * We'll tell device about this padding later.
5668c2ecf20Sopenharmony_ci	 */
5678c2ecf20Sopenharmony_ci	len =
5688c2ecf20Sopenharmony_ci	    sizeof(struct il3945_tx_cmd) + sizeof(struct il_cmd_header) +
5698c2ecf20Sopenharmony_ci	    hdr_len;
5708c2ecf20Sopenharmony_ci	firstlen = (len + 3) & ~3;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	/* Physical address of this Tx command's header (not MAC header!),
5738c2ecf20Sopenharmony_ci	 * within command buffer array. */
5748c2ecf20Sopenharmony_ci	txcmd_phys =
5758c2ecf20Sopenharmony_ci	    pci_map_single(il->pci_dev, &out_cmd->hdr, firstlen,
5768c2ecf20Sopenharmony_ci			   PCI_DMA_TODEVICE);
5778c2ecf20Sopenharmony_ci	if (unlikely(pci_dma_mapping_error(il->pci_dev, txcmd_phys)))
5788c2ecf20Sopenharmony_ci		goto drop_unlock;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	/* Set up TFD's 2nd entry to point directly to remainder of skb,
5818c2ecf20Sopenharmony_ci	 * if any (802.11 null frames have no payload). */
5828c2ecf20Sopenharmony_ci	secondlen = skb->len - hdr_len;
5838c2ecf20Sopenharmony_ci	if (secondlen > 0) {
5848c2ecf20Sopenharmony_ci		phys_addr =
5858c2ecf20Sopenharmony_ci		    pci_map_single(il->pci_dev, skb->data + hdr_len, secondlen,
5868c2ecf20Sopenharmony_ci				   PCI_DMA_TODEVICE);
5878c2ecf20Sopenharmony_ci		if (unlikely(pci_dma_mapping_error(il->pci_dev, phys_addr)))
5888c2ecf20Sopenharmony_ci			goto drop_unlock;
5898c2ecf20Sopenharmony_ci	}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	/* Add buffer containing Tx command and MAC(!) header to TFD's
5928c2ecf20Sopenharmony_ci	 * first entry */
5938c2ecf20Sopenharmony_ci	il->ops->txq_attach_buf_to_tfd(il, txq, txcmd_phys, firstlen, 1, 0);
5948c2ecf20Sopenharmony_ci	dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
5958c2ecf20Sopenharmony_ci	dma_unmap_len_set(out_meta, len, firstlen);
5968c2ecf20Sopenharmony_ci	if (secondlen > 0)
5978c2ecf20Sopenharmony_ci		il->ops->txq_attach_buf_to_tfd(il, txq, phys_addr, secondlen, 0,
5988c2ecf20Sopenharmony_ci					       U32_PAD(secondlen));
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	if (!ieee80211_has_morefrags(hdr->frame_control)) {
6018c2ecf20Sopenharmony_ci		txq->need_update = 1;
6028c2ecf20Sopenharmony_ci	} else {
6038c2ecf20Sopenharmony_ci		wait_write_ptr = 1;
6048c2ecf20Sopenharmony_ci		txq->need_update = 0;
6058c2ecf20Sopenharmony_ci	}
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	il_update_stats(il, true, fc, skb->len);
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	D_TX("sequence nr = 0X%x\n", le16_to_cpu(out_cmd->hdr.sequence));
6108c2ecf20Sopenharmony_ci	D_TX("tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
6118c2ecf20Sopenharmony_ci	il_print_hex_dump(il, IL_DL_TX, tx_cmd, sizeof(*tx_cmd));
6128c2ecf20Sopenharmony_ci	il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd->hdr,
6138c2ecf20Sopenharmony_ci			  ieee80211_hdrlen(fc));
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	/* Tell device the write idx *just past* this latest filled TFD */
6168c2ecf20Sopenharmony_ci	q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
6178c2ecf20Sopenharmony_ci	il_txq_update_write_ptr(il, txq);
6188c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&il->lock, flags);
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	if (il_queue_space(q) < q->high_mark && il->mac80211_registered) {
6218c2ecf20Sopenharmony_ci		if (wait_write_ptr) {
6228c2ecf20Sopenharmony_ci			spin_lock_irqsave(&il->lock, flags);
6238c2ecf20Sopenharmony_ci			txq->need_update = 1;
6248c2ecf20Sopenharmony_ci			il_txq_update_write_ptr(il, txq);
6258c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&il->lock, flags);
6268c2ecf20Sopenharmony_ci		}
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci		il_stop_queue(il, txq);
6298c2ecf20Sopenharmony_ci	}
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	return 0;
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_cidrop_unlock:
6348c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&il->lock, flags);
6358c2ecf20Sopenharmony_cidrop:
6368c2ecf20Sopenharmony_ci	return -1;
6378c2ecf20Sopenharmony_ci}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_cistatic int
6408c2ecf20Sopenharmony_ciil3945_get_measurement(struct il_priv *il,
6418c2ecf20Sopenharmony_ci		       struct ieee80211_measurement_params *params, u8 type)
6428c2ecf20Sopenharmony_ci{
6438c2ecf20Sopenharmony_ci	struct il_spectrum_cmd spectrum;
6448c2ecf20Sopenharmony_ci	struct il_rx_pkt *pkt;
6458c2ecf20Sopenharmony_ci	struct il_host_cmd cmd = {
6468c2ecf20Sopenharmony_ci		.id = C_SPECTRUM_MEASUREMENT,
6478c2ecf20Sopenharmony_ci		.data = (void *)&spectrum,
6488c2ecf20Sopenharmony_ci		.flags = CMD_WANT_SKB,
6498c2ecf20Sopenharmony_ci	};
6508c2ecf20Sopenharmony_ci	u32 add_time = le64_to_cpu(params->start_time);
6518c2ecf20Sopenharmony_ci	int rc;
6528c2ecf20Sopenharmony_ci	int spectrum_resp_status;
6538c2ecf20Sopenharmony_ci	int duration = le16_to_cpu(params->duration);
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	if (il_is_associated(il))
6568c2ecf20Sopenharmony_ci		add_time =
6578c2ecf20Sopenharmony_ci		    il_usecs_to_beacons(il,
6588c2ecf20Sopenharmony_ci					le64_to_cpu(params->start_time) -
6598c2ecf20Sopenharmony_ci					il->_3945.last_tsf,
6608c2ecf20Sopenharmony_ci					le16_to_cpu(il->timing.beacon_interval));
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	memset(&spectrum, 0, sizeof(spectrum));
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	spectrum.channel_count = cpu_to_le16(1);
6658c2ecf20Sopenharmony_ci	spectrum.flags =
6668c2ecf20Sopenharmony_ci	    RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
6678c2ecf20Sopenharmony_ci	spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
6688c2ecf20Sopenharmony_ci	cmd.len = sizeof(spectrum);
6698c2ecf20Sopenharmony_ci	spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	if (il_is_associated(il))
6728c2ecf20Sopenharmony_ci		spectrum.start_time =
6738c2ecf20Sopenharmony_ci		    il_add_beacon_time(il, il->_3945.last_beacon_time, add_time,
6748c2ecf20Sopenharmony_ci				       le16_to_cpu(il->timing.beacon_interval));
6758c2ecf20Sopenharmony_ci	else
6768c2ecf20Sopenharmony_ci		spectrum.start_time = 0;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
6798c2ecf20Sopenharmony_ci	spectrum.channels[0].channel = params->channel;
6808c2ecf20Sopenharmony_ci	spectrum.channels[0].type = type;
6818c2ecf20Sopenharmony_ci	if (il->active.flags & RXON_FLG_BAND_24G_MSK)
6828c2ecf20Sopenharmony_ci		spectrum.flags |=
6838c2ecf20Sopenharmony_ci		    RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK |
6848c2ecf20Sopenharmony_ci		    RXON_FLG_TGG_PROTECT_MSK;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	rc = il_send_cmd_sync(il, &cmd);
6878c2ecf20Sopenharmony_ci	if (rc)
6888c2ecf20Sopenharmony_ci		return rc;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	pkt = (struct il_rx_pkt *)cmd.reply_page;
6918c2ecf20Sopenharmony_ci	if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
6928c2ecf20Sopenharmony_ci		IL_ERR("Bad return from N_RX_ON_ASSOC command\n");
6938c2ecf20Sopenharmony_ci		rc = -EIO;
6948c2ecf20Sopenharmony_ci	}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	spectrum_resp_status = le16_to_cpu(pkt->u.spectrum.status);
6978c2ecf20Sopenharmony_ci	switch (spectrum_resp_status) {
6988c2ecf20Sopenharmony_ci	case 0:		/* Command will be handled */
6998c2ecf20Sopenharmony_ci		if (pkt->u.spectrum.id != 0xff) {
7008c2ecf20Sopenharmony_ci			D_INFO("Replaced existing measurement: %d\n",
7018c2ecf20Sopenharmony_ci			       pkt->u.spectrum.id);
7028c2ecf20Sopenharmony_ci			il->measurement_status &= ~MEASUREMENT_READY;
7038c2ecf20Sopenharmony_ci		}
7048c2ecf20Sopenharmony_ci		il->measurement_status |= MEASUREMENT_ACTIVE;
7058c2ecf20Sopenharmony_ci		rc = 0;
7068c2ecf20Sopenharmony_ci		break;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	case 1:		/* Command will not be handled */
7098c2ecf20Sopenharmony_ci		rc = -EAGAIN;
7108c2ecf20Sopenharmony_ci		break;
7118c2ecf20Sopenharmony_ci	}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	il_free_pages(il, cmd.reply_page);
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	return rc;
7168c2ecf20Sopenharmony_ci}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_cistatic void
7198c2ecf20Sopenharmony_ciil3945_hdl_alive(struct il_priv *il, struct il_rx_buf *rxb)
7208c2ecf20Sopenharmony_ci{
7218c2ecf20Sopenharmony_ci	struct il_rx_pkt *pkt = rxb_addr(rxb);
7228c2ecf20Sopenharmony_ci	struct il_alive_resp *palive;
7238c2ecf20Sopenharmony_ci	struct delayed_work *pwork;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	palive = &pkt->u.alive_frame;
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	D_INFO("Alive ucode status 0x%08X revision " "0x%01X 0x%01X\n",
7288c2ecf20Sopenharmony_ci	       palive->is_valid, palive->ver_type, palive->ver_subtype);
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
7318c2ecf20Sopenharmony_ci		D_INFO("Initialization Alive received.\n");
7328c2ecf20Sopenharmony_ci		memcpy(&il->card_alive_init, &pkt->u.alive_frame,
7338c2ecf20Sopenharmony_ci		       sizeof(struct il_alive_resp));
7348c2ecf20Sopenharmony_ci		pwork = &il->init_alive_start;
7358c2ecf20Sopenharmony_ci	} else {
7368c2ecf20Sopenharmony_ci		D_INFO("Runtime Alive received.\n");
7378c2ecf20Sopenharmony_ci		memcpy(&il->card_alive, &pkt->u.alive_frame,
7388c2ecf20Sopenharmony_ci		       sizeof(struct il_alive_resp));
7398c2ecf20Sopenharmony_ci		pwork = &il->alive_start;
7408c2ecf20Sopenharmony_ci		il3945_disable_events(il);
7418c2ecf20Sopenharmony_ci	}
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	/* We delay the ALIVE response by 5ms to
7448c2ecf20Sopenharmony_ci	 * give the HW RF Kill time to activate... */
7458c2ecf20Sopenharmony_ci	if (palive->is_valid == UCODE_VALID_OK)
7468c2ecf20Sopenharmony_ci		queue_delayed_work(il->workqueue, pwork, msecs_to_jiffies(5));
7478c2ecf20Sopenharmony_ci	else
7488c2ecf20Sopenharmony_ci		IL_WARN("uCode did not respond OK.\n");
7498c2ecf20Sopenharmony_ci}
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_cistatic void
7528c2ecf20Sopenharmony_ciil3945_hdl_add_sta(struct il_priv *il, struct il_rx_buf *rxb)
7538c2ecf20Sopenharmony_ci{
7548c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLEGACY_DEBUG
7558c2ecf20Sopenharmony_ci	struct il_rx_pkt *pkt = rxb_addr(rxb);
7568c2ecf20Sopenharmony_ci#endif
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	D_RX("Received C_ADD_STA: 0x%02X\n", pkt->u.status);
7598c2ecf20Sopenharmony_ci}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_cistatic void
7628c2ecf20Sopenharmony_ciil3945_hdl_beacon(struct il_priv *il, struct il_rx_buf *rxb)
7638c2ecf20Sopenharmony_ci{
7648c2ecf20Sopenharmony_ci	struct il_rx_pkt *pkt = rxb_addr(rxb);
7658c2ecf20Sopenharmony_ci	struct il3945_beacon_notif *beacon = &(pkt->u.beacon_status);
7668c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLEGACY_DEBUG
7678c2ecf20Sopenharmony_ci	u8 rate = beacon->beacon_notify_hdr.rate;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	D_RX("beacon status %x retries %d iss %d " "tsf %d %d rate %d\n",
7708c2ecf20Sopenharmony_ci	     le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
7718c2ecf20Sopenharmony_ci	     beacon->beacon_notify_hdr.failure_frame,
7728c2ecf20Sopenharmony_ci	     le32_to_cpu(beacon->ibss_mgr_status),
7738c2ecf20Sopenharmony_ci	     le32_to_cpu(beacon->high_tsf), le32_to_cpu(beacon->low_tsf), rate);
7748c2ecf20Sopenharmony_ci#endif
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	il->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci/* Handle notification from uCode that card's power state is changing
7818c2ecf20Sopenharmony_ci * due to software, hardware, or critical temperature RFKILL */
7828c2ecf20Sopenharmony_cistatic void
7838c2ecf20Sopenharmony_ciil3945_hdl_card_state(struct il_priv *il, struct il_rx_buf *rxb)
7848c2ecf20Sopenharmony_ci{
7858c2ecf20Sopenharmony_ci	struct il_rx_pkt *pkt = rxb_addr(rxb);
7868c2ecf20Sopenharmony_ci	u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
7878c2ecf20Sopenharmony_ci	unsigned long status = il->status;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	IL_WARN("Card state received: HW:%s SW:%s\n",
7908c2ecf20Sopenharmony_ci		(flags & HW_CARD_DISABLED) ? "Kill" : "On",
7918c2ecf20Sopenharmony_ci		(flags & SW_CARD_DISABLED) ? "Kill" : "On");
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	_il_wr(il, CSR_UCODE_DRV_GP1_SET, CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	if (flags & HW_CARD_DISABLED)
7968c2ecf20Sopenharmony_ci		set_bit(S_RFKILL, &il->status);
7978c2ecf20Sopenharmony_ci	else
7988c2ecf20Sopenharmony_ci		clear_bit(S_RFKILL, &il->status);
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	il_scan_cancel(il);
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	if ((test_bit(S_RFKILL, &status) !=
8038c2ecf20Sopenharmony_ci	     test_bit(S_RFKILL, &il->status)))
8048c2ecf20Sopenharmony_ci		wiphy_rfkill_set_hw_state(il->hw->wiphy,
8058c2ecf20Sopenharmony_ci					  test_bit(S_RFKILL, &il->status));
8068c2ecf20Sopenharmony_ci	else
8078c2ecf20Sopenharmony_ci		wake_up(&il->wait_command_queue);
8088c2ecf20Sopenharmony_ci}
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci/*
8118c2ecf20Sopenharmony_ci * il3945_setup_handlers - Initialize Rx handler callbacks
8128c2ecf20Sopenharmony_ci *
8138c2ecf20Sopenharmony_ci * Setup the RX handlers for each of the reply types sent from the uCode
8148c2ecf20Sopenharmony_ci * to the host.
8158c2ecf20Sopenharmony_ci *
8168c2ecf20Sopenharmony_ci * This function chains into the hardware specific files for them to setup
8178c2ecf20Sopenharmony_ci * any hardware specific handlers as well.
8188c2ecf20Sopenharmony_ci */
8198c2ecf20Sopenharmony_cistatic void
8208c2ecf20Sopenharmony_ciil3945_setup_handlers(struct il_priv *il)
8218c2ecf20Sopenharmony_ci{
8228c2ecf20Sopenharmony_ci	il->handlers[N_ALIVE] = il3945_hdl_alive;
8238c2ecf20Sopenharmony_ci	il->handlers[C_ADD_STA] = il3945_hdl_add_sta;
8248c2ecf20Sopenharmony_ci	il->handlers[N_ERROR] = il_hdl_error;
8258c2ecf20Sopenharmony_ci	il->handlers[N_CHANNEL_SWITCH] = il_hdl_csa;
8268c2ecf20Sopenharmony_ci	il->handlers[N_SPECTRUM_MEASUREMENT] = il_hdl_spectrum_measurement;
8278c2ecf20Sopenharmony_ci	il->handlers[N_PM_SLEEP] = il_hdl_pm_sleep;
8288c2ecf20Sopenharmony_ci	il->handlers[N_PM_DEBUG_STATS] = il_hdl_pm_debug_stats;
8298c2ecf20Sopenharmony_ci	il->handlers[N_BEACON] = il3945_hdl_beacon;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	/*
8328c2ecf20Sopenharmony_ci	 * The same handler is used for both the REPLY to a discrete
8338c2ecf20Sopenharmony_ci	 * stats request from the host as well as for the periodic
8348c2ecf20Sopenharmony_ci	 * stats notifications (after received beacons) from the uCode.
8358c2ecf20Sopenharmony_ci	 */
8368c2ecf20Sopenharmony_ci	il->handlers[C_STATS] = il3945_hdl_c_stats;
8378c2ecf20Sopenharmony_ci	il->handlers[N_STATS] = il3945_hdl_stats;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	il_setup_rx_scan_handlers(il);
8408c2ecf20Sopenharmony_ci	il->handlers[N_CARD_STATE] = il3945_hdl_card_state;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	/* Set up hardware specific Rx handlers */
8438c2ecf20Sopenharmony_ci	il3945_hw_handler_setup(il);
8448c2ecf20Sopenharmony_ci}
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci/************************** RX-FUNCTIONS ****************************/
8478c2ecf20Sopenharmony_ci/*
8488c2ecf20Sopenharmony_ci * Rx theory of operation
8498c2ecf20Sopenharmony_ci *
8508c2ecf20Sopenharmony_ci * The host allocates 32 DMA target addresses and passes the host address
8518c2ecf20Sopenharmony_ci * to the firmware at register IL_RFDS_TBL_LOWER + N * RFD_SIZE where N is
8528c2ecf20Sopenharmony_ci * 0 to 31
8538c2ecf20Sopenharmony_ci *
8548c2ecf20Sopenharmony_ci * Rx Queue Indexes
8558c2ecf20Sopenharmony_ci * The host/firmware share two idx registers for managing the Rx buffers.
8568c2ecf20Sopenharmony_ci *
8578c2ecf20Sopenharmony_ci * The READ idx maps to the first position that the firmware may be writing
8588c2ecf20Sopenharmony_ci * to -- the driver can read up to (but not including) this position and get
8598c2ecf20Sopenharmony_ci * good data.
8608c2ecf20Sopenharmony_ci * The READ idx is managed by the firmware once the card is enabled.
8618c2ecf20Sopenharmony_ci *
8628c2ecf20Sopenharmony_ci * The WRITE idx maps to the last position the driver has read from -- the
8638c2ecf20Sopenharmony_ci * position preceding WRITE is the last slot the firmware can place a packet.
8648c2ecf20Sopenharmony_ci *
8658c2ecf20Sopenharmony_ci * The queue is empty (no good data) if WRITE = READ - 1, and is full if
8668c2ecf20Sopenharmony_ci * WRITE = READ.
8678c2ecf20Sopenharmony_ci *
8688c2ecf20Sopenharmony_ci * During initialization, the host sets up the READ queue position to the first
8698c2ecf20Sopenharmony_ci * IDX position, and WRITE to the last (READ - 1 wrapped)
8708c2ecf20Sopenharmony_ci *
8718c2ecf20Sopenharmony_ci * When the firmware places a packet in a buffer, it will advance the READ idx
8728c2ecf20Sopenharmony_ci * and fire the RX interrupt.  The driver can then query the READ idx and
8738c2ecf20Sopenharmony_ci * process as many packets as possible, moving the WRITE idx forward as it
8748c2ecf20Sopenharmony_ci * resets the Rx queue buffers with new memory.
8758c2ecf20Sopenharmony_ci *
8768c2ecf20Sopenharmony_ci * The management in the driver is as follows:
8778c2ecf20Sopenharmony_ci * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free.  When
8788c2ecf20Sopenharmony_ci *   iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
8798c2ecf20Sopenharmony_ci *   to replenish the iwl->rxq->rx_free.
8808c2ecf20Sopenharmony_ci * + In il3945_rx_replenish (scheduled) if 'processed' != 'read' then the
8818c2ecf20Sopenharmony_ci *   iwl->rxq is replenished and the READ IDX is updated (updating the
8828c2ecf20Sopenharmony_ci *   'processed' and 'read' driver idxes as well)
8838c2ecf20Sopenharmony_ci * + A received packet is processed and handed to the kernel network stack,
8848c2ecf20Sopenharmony_ci *   detached from the iwl->rxq.  The driver 'processed' idx is updated.
8858c2ecf20Sopenharmony_ci * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
8868c2ecf20Sopenharmony_ci *   list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
8878c2ecf20Sopenharmony_ci *   IDX is not incremented and iwl->status(RX_STALLED) is set.  If there
8888c2ecf20Sopenharmony_ci *   were enough free buffers and RX_STALLED is set it is cleared.
8898c2ecf20Sopenharmony_ci *
8908c2ecf20Sopenharmony_ci *
8918c2ecf20Sopenharmony_ci * Driver sequence:
8928c2ecf20Sopenharmony_ci *
8938c2ecf20Sopenharmony_ci * il3945_rx_replenish()     Replenishes rx_free list from rx_used, and calls
8948c2ecf20Sopenharmony_ci *                            il3945_rx_queue_restock
8958c2ecf20Sopenharmony_ci * il3945_rx_queue_restock() Moves available buffers from rx_free into Rx
8968c2ecf20Sopenharmony_ci *                            queue, updates firmware pointers, and updates
8978c2ecf20Sopenharmony_ci *                            the WRITE idx.  If insufficient rx_free buffers
8988c2ecf20Sopenharmony_ci *                            are available, schedules il3945_rx_replenish
8998c2ecf20Sopenharmony_ci *
9008c2ecf20Sopenharmony_ci * -- enable interrupts --
9018c2ecf20Sopenharmony_ci * ISR - il3945_rx()         Detach il_rx_bufs from pool up to the
9028c2ecf20Sopenharmony_ci *                            READ IDX, detaching the SKB from the pool.
9038c2ecf20Sopenharmony_ci *                            Moves the packet buffer from queue to rx_used.
9048c2ecf20Sopenharmony_ci *                            Calls il3945_rx_queue_restock to refill any empty
9058c2ecf20Sopenharmony_ci *                            slots.
9068c2ecf20Sopenharmony_ci * ...
9078c2ecf20Sopenharmony_ci *
9088c2ecf20Sopenharmony_ci */
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci/*
9118c2ecf20Sopenharmony_ci * il3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
9128c2ecf20Sopenharmony_ci */
9138c2ecf20Sopenharmony_cistatic inline __le32
9148c2ecf20Sopenharmony_ciil3945_dma_addr2rbd_ptr(struct il_priv *il, dma_addr_t dma_addr)
9158c2ecf20Sopenharmony_ci{
9168c2ecf20Sopenharmony_ci	return cpu_to_le32((u32) dma_addr);
9178c2ecf20Sopenharmony_ci}
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci/*
9208c2ecf20Sopenharmony_ci * il3945_rx_queue_restock - refill RX queue from pre-allocated pool
9218c2ecf20Sopenharmony_ci *
9228c2ecf20Sopenharmony_ci * If there are slots in the RX queue that need to be restocked,
9238c2ecf20Sopenharmony_ci * and we have free pre-allocated buffers, fill the ranks as much
9248c2ecf20Sopenharmony_ci * as we can, pulling from rx_free.
9258c2ecf20Sopenharmony_ci *
9268c2ecf20Sopenharmony_ci * This moves the 'write' idx forward to catch up with 'processed', and
9278c2ecf20Sopenharmony_ci * also updates the memory address in the firmware to reference the new
9288c2ecf20Sopenharmony_ci * target buffer.
9298c2ecf20Sopenharmony_ci */
9308c2ecf20Sopenharmony_cistatic void
9318c2ecf20Sopenharmony_ciil3945_rx_queue_restock(struct il_priv *il)
9328c2ecf20Sopenharmony_ci{
9338c2ecf20Sopenharmony_ci	struct il_rx_queue *rxq = &il->rxq;
9348c2ecf20Sopenharmony_ci	struct list_head *element;
9358c2ecf20Sopenharmony_ci	struct il_rx_buf *rxb;
9368c2ecf20Sopenharmony_ci	unsigned long flags;
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rxq->lock, flags);
9398c2ecf20Sopenharmony_ci	while (il_rx_queue_space(rxq) > 0 && rxq->free_count) {
9408c2ecf20Sopenharmony_ci		/* Get next free Rx buffer, remove from free list */
9418c2ecf20Sopenharmony_ci		element = rxq->rx_free.next;
9428c2ecf20Sopenharmony_ci		rxb = list_entry(element, struct il_rx_buf, list);
9438c2ecf20Sopenharmony_ci		list_del(element);
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci		/* Point to Rx buffer via next RBD in circular buffer */
9468c2ecf20Sopenharmony_ci		rxq->bd[rxq->write] =
9478c2ecf20Sopenharmony_ci		    il3945_dma_addr2rbd_ptr(il, rxb->page_dma);
9488c2ecf20Sopenharmony_ci		rxq->queue[rxq->write] = rxb;
9498c2ecf20Sopenharmony_ci		rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
9508c2ecf20Sopenharmony_ci		rxq->free_count--;
9518c2ecf20Sopenharmony_ci	}
9528c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rxq->lock, flags);
9538c2ecf20Sopenharmony_ci	/* If the pre-allocated buffer pool is dropping low, schedule to
9548c2ecf20Sopenharmony_ci	 * refill it */
9558c2ecf20Sopenharmony_ci	if (rxq->free_count <= RX_LOW_WATERMARK)
9568c2ecf20Sopenharmony_ci		queue_work(il->workqueue, &il->rx_replenish);
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	/* If we've added more space for the firmware to place data, tell it.
9598c2ecf20Sopenharmony_ci	 * Increment device's write pointer in multiples of 8. */
9608c2ecf20Sopenharmony_ci	if (rxq->write_actual != (rxq->write & ~0x7) ||
9618c2ecf20Sopenharmony_ci	    abs(rxq->write - rxq->read) > 7) {
9628c2ecf20Sopenharmony_ci		spin_lock_irqsave(&rxq->lock, flags);
9638c2ecf20Sopenharmony_ci		rxq->need_update = 1;
9648c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&rxq->lock, flags);
9658c2ecf20Sopenharmony_ci		il_rx_queue_update_write_ptr(il, rxq);
9668c2ecf20Sopenharmony_ci	}
9678c2ecf20Sopenharmony_ci}
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci/*
9708c2ecf20Sopenharmony_ci * il3945_rx_replenish - Move all used packet from rx_used to rx_free
9718c2ecf20Sopenharmony_ci *
9728c2ecf20Sopenharmony_ci * When moving to rx_free an SKB is allocated for the slot.
9738c2ecf20Sopenharmony_ci *
9748c2ecf20Sopenharmony_ci * Also restock the Rx queue via il3945_rx_queue_restock.
9758c2ecf20Sopenharmony_ci * This is called as a scheduled work item (except for during initialization)
9768c2ecf20Sopenharmony_ci */
9778c2ecf20Sopenharmony_cistatic void
9788c2ecf20Sopenharmony_ciil3945_rx_allocate(struct il_priv *il, gfp_t priority)
9798c2ecf20Sopenharmony_ci{
9808c2ecf20Sopenharmony_ci	struct il_rx_queue *rxq = &il->rxq;
9818c2ecf20Sopenharmony_ci	struct list_head *element;
9828c2ecf20Sopenharmony_ci	struct il_rx_buf *rxb;
9838c2ecf20Sopenharmony_ci	struct page *page;
9848c2ecf20Sopenharmony_ci	dma_addr_t page_dma;
9858c2ecf20Sopenharmony_ci	unsigned long flags;
9868c2ecf20Sopenharmony_ci	gfp_t gfp_mask = priority;
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	while (1) {
9898c2ecf20Sopenharmony_ci		spin_lock_irqsave(&rxq->lock, flags);
9908c2ecf20Sopenharmony_ci		if (list_empty(&rxq->rx_used)) {
9918c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&rxq->lock, flags);
9928c2ecf20Sopenharmony_ci			return;
9938c2ecf20Sopenharmony_ci		}
9948c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&rxq->lock, flags);
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci		if (rxq->free_count > RX_LOW_WATERMARK)
9978c2ecf20Sopenharmony_ci			gfp_mask |= __GFP_NOWARN;
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci		if (il->hw_params.rx_page_order > 0)
10008c2ecf20Sopenharmony_ci			gfp_mask |= __GFP_COMP;
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci		/* Alloc a new receive buffer */
10038c2ecf20Sopenharmony_ci		page = alloc_pages(gfp_mask, il->hw_params.rx_page_order);
10048c2ecf20Sopenharmony_ci		if (!page) {
10058c2ecf20Sopenharmony_ci			if (net_ratelimit())
10068c2ecf20Sopenharmony_ci				D_INFO("Failed to allocate SKB buffer.\n");
10078c2ecf20Sopenharmony_ci			if (rxq->free_count <= RX_LOW_WATERMARK &&
10088c2ecf20Sopenharmony_ci			    net_ratelimit())
10098c2ecf20Sopenharmony_ci				IL_ERR("Failed to allocate SKB buffer with %0x."
10108c2ecf20Sopenharmony_ci				       "Only %u free buffers remaining.\n",
10118c2ecf20Sopenharmony_ci				       priority, rxq->free_count);
10128c2ecf20Sopenharmony_ci			/* We don't reschedule replenish work here -- we will
10138c2ecf20Sopenharmony_ci			 * call the restock method and if it still needs
10148c2ecf20Sopenharmony_ci			 * more buffers it will schedule replenish */
10158c2ecf20Sopenharmony_ci			break;
10168c2ecf20Sopenharmony_ci		}
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci		/* Get physical address of RB/SKB */
10198c2ecf20Sopenharmony_ci		page_dma =
10208c2ecf20Sopenharmony_ci		    pci_map_page(il->pci_dev, page, 0,
10218c2ecf20Sopenharmony_ci				 PAGE_SIZE << il->hw_params.rx_page_order,
10228c2ecf20Sopenharmony_ci				 PCI_DMA_FROMDEVICE);
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci		if (unlikely(pci_dma_mapping_error(il->pci_dev, page_dma))) {
10258c2ecf20Sopenharmony_ci			__free_pages(page, il->hw_params.rx_page_order);
10268c2ecf20Sopenharmony_ci			break;
10278c2ecf20Sopenharmony_ci		}
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci		spin_lock_irqsave(&rxq->lock, flags);
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci		if (list_empty(&rxq->rx_used)) {
10328c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&rxq->lock, flags);
10338c2ecf20Sopenharmony_ci			pci_unmap_page(il->pci_dev, page_dma,
10348c2ecf20Sopenharmony_ci				       PAGE_SIZE << il->hw_params.rx_page_order,
10358c2ecf20Sopenharmony_ci				       PCI_DMA_FROMDEVICE);
10368c2ecf20Sopenharmony_ci			__free_pages(page, il->hw_params.rx_page_order);
10378c2ecf20Sopenharmony_ci			return;
10388c2ecf20Sopenharmony_ci		}
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci		element = rxq->rx_used.next;
10418c2ecf20Sopenharmony_ci		rxb = list_entry(element, struct il_rx_buf, list);
10428c2ecf20Sopenharmony_ci		list_del(element);
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci		rxb->page = page;
10458c2ecf20Sopenharmony_ci		rxb->page_dma = page_dma;
10468c2ecf20Sopenharmony_ci		list_add_tail(&rxb->list, &rxq->rx_free);
10478c2ecf20Sopenharmony_ci		rxq->free_count++;
10488c2ecf20Sopenharmony_ci		il->alloc_rxb_page++;
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&rxq->lock, flags);
10518c2ecf20Sopenharmony_ci	}
10528c2ecf20Sopenharmony_ci}
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_civoid
10558c2ecf20Sopenharmony_ciil3945_rx_queue_reset(struct il_priv *il, struct il_rx_queue *rxq)
10568c2ecf20Sopenharmony_ci{
10578c2ecf20Sopenharmony_ci	unsigned long flags;
10588c2ecf20Sopenharmony_ci	int i;
10598c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rxq->lock, flags);
10608c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&rxq->rx_free);
10618c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&rxq->rx_used);
10628c2ecf20Sopenharmony_ci	/* Fill the rx_used queue with _all_ of the Rx buffers */
10638c2ecf20Sopenharmony_ci	for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
10648c2ecf20Sopenharmony_ci		/* In the reset function, these buffers may have been allocated
10658c2ecf20Sopenharmony_ci		 * to an SKB, so we need to unmap and free potential storage */
10668c2ecf20Sopenharmony_ci		if (rxq->pool[i].page != NULL) {
10678c2ecf20Sopenharmony_ci			pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
10688c2ecf20Sopenharmony_ci				       PAGE_SIZE << il->hw_params.rx_page_order,
10698c2ecf20Sopenharmony_ci				       PCI_DMA_FROMDEVICE);
10708c2ecf20Sopenharmony_ci			__il_free_pages(il, rxq->pool[i].page);
10718c2ecf20Sopenharmony_ci			rxq->pool[i].page = NULL;
10728c2ecf20Sopenharmony_ci		}
10738c2ecf20Sopenharmony_ci		list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
10748c2ecf20Sopenharmony_ci	}
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci	/* Set us so that we have processed and used all buffers, but have
10778c2ecf20Sopenharmony_ci	 * not restocked the Rx queue with fresh buffers */
10788c2ecf20Sopenharmony_ci	rxq->read = rxq->write = 0;
10798c2ecf20Sopenharmony_ci	rxq->write_actual = 0;
10808c2ecf20Sopenharmony_ci	rxq->free_count = 0;
10818c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rxq->lock, flags);
10828c2ecf20Sopenharmony_ci}
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_civoid
10858c2ecf20Sopenharmony_ciil3945_rx_replenish(void *data)
10868c2ecf20Sopenharmony_ci{
10878c2ecf20Sopenharmony_ci	struct il_priv *il = data;
10888c2ecf20Sopenharmony_ci	unsigned long flags;
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	il3945_rx_allocate(il, GFP_KERNEL);
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci	spin_lock_irqsave(&il->lock, flags);
10938c2ecf20Sopenharmony_ci	il3945_rx_queue_restock(il);
10948c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&il->lock, flags);
10958c2ecf20Sopenharmony_ci}
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_cistatic void
10988c2ecf20Sopenharmony_ciil3945_rx_replenish_now(struct il_priv *il)
10998c2ecf20Sopenharmony_ci{
11008c2ecf20Sopenharmony_ci	il3945_rx_allocate(il, GFP_ATOMIC);
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_ci	il3945_rx_queue_restock(il);
11038c2ecf20Sopenharmony_ci}
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
11068c2ecf20Sopenharmony_ci * If an SKB has been detached, the POOL needs to have its SKB set to NULL
11078c2ecf20Sopenharmony_ci * This free routine walks the list of POOL entries and if SKB is set to
11088c2ecf20Sopenharmony_ci * non NULL it is unmapped and freed
11098c2ecf20Sopenharmony_ci */
11108c2ecf20Sopenharmony_cistatic void
11118c2ecf20Sopenharmony_ciil3945_rx_queue_free(struct il_priv *il, struct il_rx_queue *rxq)
11128c2ecf20Sopenharmony_ci{
11138c2ecf20Sopenharmony_ci	int i;
11148c2ecf20Sopenharmony_ci	for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
11158c2ecf20Sopenharmony_ci		if (rxq->pool[i].page != NULL) {
11168c2ecf20Sopenharmony_ci			pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
11178c2ecf20Sopenharmony_ci				       PAGE_SIZE << il->hw_params.rx_page_order,
11188c2ecf20Sopenharmony_ci				       PCI_DMA_FROMDEVICE);
11198c2ecf20Sopenharmony_ci			__il_free_pages(il, rxq->pool[i].page);
11208c2ecf20Sopenharmony_ci			rxq->pool[i].page = NULL;
11218c2ecf20Sopenharmony_ci		}
11228c2ecf20Sopenharmony_ci	}
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci	dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
11258c2ecf20Sopenharmony_ci			  rxq->bd_dma);
11268c2ecf20Sopenharmony_ci	dma_free_coherent(&il->pci_dev->dev, sizeof(struct il_rb_status),
11278c2ecf20Sopenharmony_ci			  rxq->rb_stts, rxq->rb_stts_dma);
11288c2ecf20Sopenharmony_ci	rxq->bd = NULL;
11298c2ecf20Sopenharmony_ci	rxq->rb_stts = NULL;
11308c2ecf20Sopenharmony_ci}
11318c2ecf20Sopenharmony_ci
11328c2ecf20Sopenharmony_ci/* Convert linear signal-to-noise ratio into dB */
11338c2ecf20Sopenharmony_cistatic u8 ratio2dB[100] = {
11348c2ecf20Sopenharmony_ci/*	 0   1   2   3   4   5   6   7   8   9 */
11358c2ecf20Sopenharmony_ci	0, 0, 6, 10, 12, 14, 16, 17, 18, 19,	/* 00 - 09 */
11368c2ecf20Sopenharmony_ci	20, 21, 22, 22, 23, 23, 24, 25, 26, 26,	/* 10 - 19 */
11378c2ecf20Sopenharmony_ci	26, 26, 26, 27, 27, 28, 28, 28, 29, 29,	/* 20 - 29 */
11388c2ecf20Sopenharmony_ci	29, 30, 30, 30, 31, 31, 31, 31, 32, 32,	/* 30 - 39 */
11398c2ecf20Sopenharmony_ci	32, 32, 32, 33, 33, 33, 33, 33, 34, 34,	/* 40 - 49 */
11408c2ecf20Sopenharmony_ci	34, 34, 34, 34, 35, 35, 35, 35, 35, 35,	/* 50 - 59 */
11418c2ecf20Sopenharmony_ci	36, 36, 36, 36, 36, 36, 36, 37, 37, 37,	/* 60 - 69 */
11428c2ecf20Sopenharmony_ci	37, 37, 37, 37, 37, 38, 38, 38, 38, 38,	/* 70 - 79 */
11438c2ecf20Sopenharmony_ci	38, 38, 38, 38, 38, 39, 39, 39, 39, 39,	/* 80 - 89 */
11448c2ecf20Sopenharmony_ci	39, 39, 39, 39, 39, 40, 40, 40, 40, 40	/* 90 - 99 */
11458c2ecf20Sopenharmony_ci};
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_ci/* Calculates a relative dB value from a ratio of linear
11488c2ecf20Sopenharmony_ci *   (i.e. not dB) signal levels.
11498c2ecf20Sopenharmony_ci * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
11508c2ecf20Sopenharmony_ciint
11518c2ecf20Sopenharmony_ciil3945_calc_db_from_ratio(int sig_ratio)
11528c2ecf20Sopenharmony_ci{
11538c2ecf20Sopenharmony_ci	/* 1000:1 or higher just report as 60 dB */
11548c2ecf20Sopenharmony_ci	if (sig_ratio >= 1000)
11558c2ecf20Sopenharmony_ci		return 60;
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	/* 100:1 or higher, divide by 10 and use table,
11588c2ecf20Sopenharmony_ci	 *   add 20 dB to make up for divide by 10 */
11598c2ecf20Sopenharmony_ci	if (sig_ratio >= 100)
11608c2ecf20Sopenharmony_ci		return 20 + (int)ratio2dB[sig_ratio / 10];
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_ci	/* We shouldn't see this */
11638c2ecf20Sopenharmony_ci	if (sig_ratio < 1)
11648c2ecf20Sopenharmony_ci		return 0;
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci	/* Use table for ratios 1:1 - 99:1 */
11678c2ecf20Sopenharmony_ci	return (int)ratio2dB[sig_ratio];
11688c2ecf20Sopenharmony_ci}
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci/*
11718c2ecf20Sopenharmony_ci * il3945_rx_handle - Main entry function for receiving responses from uCode
11728c2ecf20Sopenharmony_ci *
11738c2ecf20Sopenharmony_ci * Uses the il->handlers callback function array to invoke
11748c2ecf20Sopenharmony_ci * the appropriate handlers, including command responses,
11758c2ecf20Sopenharmony_ci * frame-received notifications, and other notifications.
11768c2ecf20Sopenharmony_ci */
11778c2ecf20Sopenharmony_cistatic void
11788c2ecf20Sopenharmony_ciil3945_rx_handle(struct il_priv *il)
11798c2ecf20Sopenharmony_ci{
11808c2ecf20Sopenharmony_ci	struct il_rx_buf *rxb;
11818c2ecf20Sopenharmony_ci	struct il_rx_pkt *pkt;
11828c2ecf20Sopenharmony_ci	struct il_rx_queue *rxq = &il->rxq;
11838c2ecf20Sopenharmony_ci	u32 r, i;
11848c2ecf20Sopenharmony_ci	int reclaim;
11858c2ecf20Sopenharmony_ci	unsigned long flags;
11868c2ecf20Sopenharmony_ci	u8 fill_rx = 0;
11878c2ecf20Sopenharmony_ci	u32 count = 8;
11888c2ecf20Sopenharmony_ci	int total_empty = 0;
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci	/* uCode's read idx (stored in shared DRAM) indicates the last Rx
11918c2ecf20Sopenharmony_ci	 * buffer that the driver may process (last buffer filled by ucode). */
11928c2ecf20Sopenharmony_ci	r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
11938c2ecf20Sopenharmony_ci	i = rxq->read;
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci	/* calculate total frames need to be restock after handling RX */
11968c2ecf20Sopenharmony_ci	total_empty = r - rxq->write_actual;
11978c2ecf20Sopenharmony_ci	if (total_empty < 0)
11988c2ecf20Sopenharmony_ci		total_empty += RX_QUEUE_SIZE;
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci	if (total_empty > (RX_QUEUE_SIZE / 2))
12018c2ecf20Sopenharmony_ci		fill_rx = 1;
12028c2ecf20Sopenharmony_ci	/* Rx interrupt, but nothing sent from uCode */
12038c2ecf20Sopenharmony_ci	if (i == r)
12048c2ecf20Sopenharmony_ci		D_RX("r = %d, i = %d\n", r, i);
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci	while (i != r) {
12078c2ecf20Sopenharmony_ci		int len;
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_ci		rxb = rxq->queue[i];
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci		/* If an RXB doesn't have a Rx queue slot associated with it,
12128c2ecf20Sopenharmony_ci		 * then a bug has been introduced in the queue refilling
12138c2ecf20Sopenharmony_ci		 * routines -- catch it here */
12148c2ecf20Sopenharmony_ci		BUG_ON(rxb == NULL);
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci		rxq->queue[i] = NULL;
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci		pci_unmap_page(il->pci_dev, rxb->page_dma,
12198c2ecf20Sopenharmony_ci			       PAGE_SIZE << il->hw_params.rx_page_order,
12208c2ecf20Sopenharmony_ci			       PCI_DMA_FROMDEVICE);
12218c2ecf20Sopenharmony_ci		pkt = rxb_addr(rxb);
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci		len = le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK;
12248c2ecf20Sopenharmony_ci		len += sizeof(u32);	/* account for status word */
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci		reclaim = il_need_reclaim(il, pkt);
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci		/* Based on type of command response or notification,
12298c2ecf20Sopenharmony_ci		 *   handle those that need handling via function in
12308c2ecf20Sopenharmony_ci		 *   handlers table.  See il3945_setup_handlers() */
12318c2ecf20Sopenharmony_ci		if (il->handlers[pkt->hdr.cmd]) {
12328c2ecf20Sopenharmony_ci			D_RX("r = %d, i = %d, %s, 0x%02x\n", r, i,
12338c2ecf20Sopenharmony_ci			     il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
12348c2ecf20Sopenharmony_ci			il->isr_stats.handlers[pkt->hdr.cmd]++;
12358c2ecf20Sopenharmony_ci			il->handlers[pkt->hdr.cmd] (il, rxb);
12368c2ecf20Sopenharmony_ci		} else {
12378c2ecf20Sopenharmony_ci			/* No handling needed */
12388c2ecf20Sopenharmony_ci			D_RX("r %d i %d No handler needed for %s, 0x%02x\n", r,
12398c2ecf20Sopenharmony_ci			     i, il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
12408c2ecf20Sopenharmony_ci		}
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci		/*
12438c2ecf20Sopenharmony_ci		 * XXX: After here, we should always check rxb->page
12448c2ecf20Sopenharmony_ci		 * against NULL before touching it or its virtual
12458c2ecf20Sopenharmony_ci		 * memory (pkt). Because some handler might have
12468c2ecf20Sopenharmony_ci		 * already taken or freed the pages.
12478c2ecf20Sopenharmony_ci		 */
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci		if (reclaim) {
12508c2ecf20Sopenharmony_ci			/* Invoke any callbacks, transfer the buffer to caller,
12518c2ecf20Sopenharmony_ci			 * and fire off the (possibly) blocking il_send_cmd()
12528c2ecf20Sopenharmony_ci			 * as we reclaim the driver command queue */
12538c2ecf20Sopenharmony_ci			if (rxb->page)
12548c2ecf20Sopenharmony_ci				il_tx_cmd_complete(il, rxb);
12558c2ecf20Sopenharmony_ci			else
12568c2ecf20Sopenharmony_ci				IL_WARN("Claim null rxb?\n");
12578c2ecf20Sopenharmony_ci		}
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci		/* Reuse the page if possible. For notification packets and
12608c2ecf20Sopenharmony_ci		 * SKBs that fail to Rx correctly, add them back into the
12618c2ecf20Sopenharmony_ci		 * rx_free list for reuse later. */
12628c2ecf20Sopenharmony_ci		spin_lock_irqsave(&rxq->lock, flags);
12638c2ecf20Sopenharmony_ci		if (rxb->page != NULL) {
12648c2ecf20Sopenharmony_ci			rxb->page_dma =
12658c2ecf20Sopenharmony_ci			    pci_map_page(il->pci_dev, rxb->page, 0,
12668c2ecf20Sopenharmony_ci					 PAGE_SIZE << il->hw_params.
12678c2ecf20Sopenharmony_ci					 rx_page_order, PCI_DMA_FROMDEVICE);
12688c2ecf20Sopenharmony_ci			if (unlikely(pci_dma_mapping_error(il->pci_dev,
12698c2ecf20Sopenharmony_ci							   rxb->page_dma))) {
12708c2ecf20Sopenharmony_ci				__il_free_pages(il, rxb->page);
12718c2ecf20Sopenharmony_ci				rxb->page = NULL;
12728c2ecf20Sopenharmony_ci				list_add_tail(&rxb->list, &rxq->rx_used);
12738c2ecf20Sopenharmony_ci			} else {
12748c2ecf20Sopenharmony_ci				list_add_tail(&rxb->list, &rxq->rx_free);
12758c2ecf20Sopenharmony_ci				rxq->free_count++;
12768c2ecf20Sopenharmony_ci			}
12778c2ecf20Sopenharmony_ci		} else
12788c2ecf20Sopenharmony_ci			list_add_tail(&rxb->list, &rxq->rx_used);
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&rxq->lock, flags);
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci		i = (i + 1) & RX_QUEUE_MASK;
12838c2ecf20Sopenharmony_ci		/* If there are a lot of unused frames,
12848c2ecf20Sopenharmony_ci		 * restock the Rx queue so ucode won't assert. */
12858c2ecf20Sopenharmony_ci		if (fill_rx) {
12868c2ecf20Sopenharmony_ci			count++;
12878c2ecf20Sopenharmony_ci			if (count >= 8) {
12888c2ecf20Sopenharmony_ci				rxq->read = i;
12898c2ecf20Sopenharmony_ci				il3945_rx_replenish_now(il);
12908c2ecf20Sopenharmony_ci				count = 0;
12918c2ecf20Sopenharmony_ci			}
12928c2ecf20Sopenharmony_ci		}
12938c2ecf20Sopenharmony_ci	}
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	/* Backtrack one entry */
12968c2ecf20Sopenharmony_ci	rxq->read = i;
12978c2ecf20Sopenharmony_ci	if (fill_rx)
12988c2ecf20Sopenharmony_ci		il3945_rx_replenish_now(il);
12998c2ecf20Sopenharmony_ci	else
13008c2ecf20Sopenharmony_ci		il3945_rx_queue_restock(il);
13018c2ecf20Sopenharmony_ci}
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_ci/* call this function to flush any scheduled tasklet */
13048c2ecf20Sopenharmony_cistatic inline void
13058c2ecf20Sopenharmony_ciil3945_synchronize_irq(struct il_priv *il)
13068c2ecf20Sopenharmony_ci{
13078c2ecf20Sopenharmony_ci	/* wait to make sure we flush pending tasklet */
13088c2ecf20Sopenharmony_ci	synchronize_irq(il->pci_dev->irq);
13098c2ecf20Sopenharmony_ci	tasklet_kill(&il->irq_tasklet);
13108c2ecf20Sopenharmony_ci}
13118c2ecf20Sopenharmony_ci
13128c2ecf20Sopenharmony_cistatic const char *
13138c2ecf20Sopenharmony_ciil3945_desc_lookup(int i)
13148c2ecf20Sopenharmony_ci{
13158c2ecf20Sopenharmony_ci	switch (i) {
13168c2ecf20Sopenharmony_ci	case 1:
13178c2ecf20Sopenharmony_ci		return "FAIL";
13188c2ecf20Sopenharmony_ci	case 2:
13198c2ecf20Sopenharmony_ci		return "BAD_PARAM";
13208c2ecf20Sopenharmony_ci	case 3:
13218c2ecf20Sopenharmony_ci		return "BAD_CHECKSUM";
13228c2ecf20Sopenharmony_ci	case 4:
13238c2ecf20Sopenharmony_ci		return "NMI_INTERRUPT";
13248c2ecf20Sopenharmony_ci	case 5:
13258c2ecf20Sopenharmony_ci		return "SYSASSERT";
13268c2ecf20Sopenharmony_ci	case 6:
13278c2ecf20Sopenharmony_ci		return "FATAL_ERROR";
13288c2ecf20Sopenharmony_ci	}
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci	return "UNKNOWN";
13318c2ecf20Sopenharmony_ci}
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_ci#define ERROR_START_OFFSET  (1 * sizeof(u32))
13348c2ecf20Sopenharmony_ci#define ERROR_ELEM_SIZE     (7 * sizeof(u32))
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_civoid
13378c2ecf20Sopenharmony_ciil3945_dump_nic_error_log(struct il_priv *il)
13388c2ecf20Sopenharmony_ci{
13398c2ecf20Sopenharmony_ci	u32 i;
13408c2ecf20Sopenharmony_ci	u32 desc, time, count, base, data1;
13418c2ecf20Sopenharmony_ci	u32 blink1, blink2, ilink1, ilink2;
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci	base = le32_to_cpu(il->card_alive.error_event_table_ptr);
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_ci	if (!il3945_hw_valid_rtc_data_addr(base)) {
13468c2ecf20Sopenharmony_ci		IL_ERR("Not valid error log pointer 0x%08X\n", base);
13478c2ecf20Sopenharmony_ci		return;
13488c2ecf20Sopenharmony_ci	}
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ci	count = il_read_targ_mem(il, base);
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ci	if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
13538c2ecf20Sopenharmony_ci		IL_ERR("Start IWL Error Log Dump:\n");
13548c2ecf20Sopenharmony_ci		IL_ERR("Status: 0x%08lX, count: %d\n", il->status, count);
13558c2ecf20Sopenharmony_ci	}
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_ci	IL_ERR("Desc       Time       asrtPC  blink2 "
13588c2ecf20Sopenharmony_ci	       "ilink1  nmiPC   Line\n");
13598c2ecf20Sopenharmony_ci	for (i = ERROR_START_OFFSET;
13608c2ecf20Sopenharmony_ci	     i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
13618c2ecf20Sopenharmony_ci	     i += ERROR_ELEM_SIZE) {
13628c2ecf20Sopenharmony_ci		desc = il_read_targ_mem(il, base + i);
13638c2ecf20Sopenharmony_ci		time = il_read_targ_mem(il, base + i + 1 * sizeof(u32));
13648c2ecf20Sopenharmony_ci		blink1 = il_read_targ_mem(il, base + i + 2 * sizeof(u32));
13658c2ecf20Sopenharmony_ci		blink2 = il_read_targ_mem(il, base + i + 3 * sizeof(u32));
13668c2ecf20Sopenharmony_ci		ilink1 = il_read_targ_mem(il, base + i + 4 * sizeof(u32));
13678c2ecf20Sopenharmony_ci		ilink2 = il_read_targ_mem(il, base + i + 5 * sizeof(u32));
13688c2ecf20Sopenharmony_ci		data1 = il_read_targ_mem(il, base + i + 6 * sizeof(u32));
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ci		IL_ERR("%-13s (0x%X) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
13718c2ecf20Sopenharmony_ci		       il3945_desc_lookup(desc), desc, time, blink1, blink2,
13728c2ecf20Sopenharmony_ci		       ilink1, ilink2, data1);
13738c2ecf20Sopenharmony_ci	}
13748c2ecf20Sopenharmony_ci}
13758c2ecf20Sopenharmony_ci
13768c2ecf20Sopenharmony_cistatic void
13778c2ecf20Sopenharmony_ciil3945_irq_tasklet(struct tasklet_struct *t)
13788c2ecf20Sopenharmony_ci{
13798c2ecf20Sopenharmony_ci	struct il_priv *il = from_tasklet(il, t, irq_tasklet);
13808c2ecf20Sopenharmony_ci	u32 inta, handled = 0;
13818c2ecf20Sopenharmony_ci	u32 inta_fh;
13828c2ecf20Sopenharmony_ci	unsigned long flags;
13838c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLEGACY_DEBUG
13848c2ecf20Sopenharmony_ci	u32 inta_mask;
13858c2ecf20Sopenharmony_ci#endif
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci	spin_lock_irqsave(&il->lock, flags);
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ci	/* Ack/clear/reset pending uCode interrupts.
13908c2ecf20Sopenharmony_ci	 * Note:  Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
13918c2ecf20Sopenharmony_ci	 *  and will clear only when CSR_FH_INT_STATUS gets cleared. */
13928c2ecf20Sopenharmony_ci	inta = _il_rd(il, CSR_INT);
13938c2ecf20Sopenharmony_ci	_il_wr(il, CSR_INT, inta);
13948c2ecf20Sopenharmony_ci
13958c2ecf20Sopenharmony_ci	/* Ack/clear/reset pending flow-handler (DMA) interrupts.
13968c2ecf20Sopenharmony_ci	 * Any new interrupts that happen after this, either while we're
13978c2ecf20Sopenharmony_ci	 * in this tasklet, or later, will show up in next ISR/tasklet. */
13988c2ecf20Sopenharmony_ci	inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
13998c2ecf20Sopenharmony_ci	_il_wr(il, CSR_FH_INT_STATUS, inta_fh);
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLEGACY_DEBUG
14028c2ecf20Sopenharmony_ci	if (il_get_debug_level(il) & IL_DL_ISR) {
14038c2ecf20Sopenharmony_ci		/* just for debug */
14048c2ecf20Sopenharmony_ci		inta_mask = _il_rd(il, CSR_INT_MASK);
14058c2ecf20Sopenharmony_ci		D_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n", inta,
14068c2ecf20Sopenharmony_ci		      inta_mask, inta_fh);
14078c2ecf20Sopenharmony_ci	}
14088c2ecf20Sopenharmony_ci#endif
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&il->lock, flags);
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_ci	/* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
14138c2ecf20Sopenharmony_ci	 * atomic, make sure that inta covers all the interrupts that
14148c2ecf20Sopenharmony_ci	 * we've discovered, even if FH interrupt came in just after
14158c2ecf20Sopenharmony_ci	 * reading CSR_INT. */
14168c2ecf20Sopenharmony_ci	if (inta_fh & CSR39_FH_INT_RX_MASK)
14178c2ecf20Sopenharmony_ci		inta |= CSR_INT_BIT_FH_RX;
14188c2ecf20Sopenharmony_ci	if (inta_fh & CSR39_FH_INT_TX_MASK)
14198c2ecf20Sopenharmony_ci		inta |= CSR_INT_BIT_FH_TX;
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci	/* Now service all interrupt bits discovered above. */
14228c2ecf20Sopenharmony_ci	if (inta & CSR_INT_BIT_HW_ERR) {
14238c2ecf20Sopenharmony_ci		IL_ERR("Hardware error detected.  Restarting.\n");
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_ci		/* Tell the device to stop sending interrupts */
14268c2ecf20Sopenharmony_ci		il_disable_interrupts(il);
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_ci		il->isr_stats.hw++;
14298c2ecf20Sopenharmony_ci		il_irq_handle_error(il);
14308c2ecf20Sopenharmony_ci
14318c2ecf20Sopenharmony_ci		handled |= CSR_INT_BIT_HW_ERR;
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci		return;
14348c2ecf20Sopenharmony_ci	}
14358c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLEGACY_DEBUG
14368c2ecf20Sopenharmony_ci	if (il_get_debug_level(il) & (IL_DL_ISR)) {
14378c2ecf20Sopenharmony_ci		/* NIC fires this, but we don't use it, redundant with WAKEUP */
14388c2ecf20Sopenharmony_ci		if (inta & CSR_INT_BIT_SCD) {
14398c2ecf20Sopenharmony_ci			D_ISR("Scheduler finished to transmit "
14408c2ecf20Sopenharmony_ci			      "the frame/frames.\n");
14418c2ecf20Sopenharmony_ci			il->isr_stats.sch++;
14428c2ecf20Sopenharmony_ci		}
14438c2ecf20Sopenharmony_ci
14448c2ecf20Sopenharmony_ci		/* Alive notification via Rx interrupt will do the real work */
14458c2ecf20Sopenharmony_ci		if (inta & CSR_INT_BIT_ALIVE) {
14468c2ecf20Sopenharmony_ci			D_ISR("Alive interrupt\n");
14478c2ecf20Sopenharmony_ci			il->isr_stats.alive++;
14488c2ecf20Sopenharmony_ci		}
14498c2ecf20Sopenharmony_ci	}
14508c2ecf20Sopenharmony_ci#endif
14518c2ecf20Sopenharmony_ci	/* Safely ignore these bits for debug checks below */
14528c2ecf20Sopenharmony_ci	inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
14538c2ecf20Sopenharmony_ci
14548c2ecf20Sopenharmony_ci	/* Error detected by uCode */
14558c2ecf20Sopenharmony_ci	if (inta & CSR_INT_BIT_SW_ERR) {
14568c2ecf20Sopenharmony_ci		IL_ERR("Microcode SW error detected. " "Restarting 0x%X.\n",
14578c2ecf20Sopenharmony_ci		       inta);
14588c2ecf20Sopenharmony_ci		il->isr_stats.sw++;
14598c2ecf20Sopenharmony_ci		il_irq_handle_error(il);
14608c2ecf20Sopenharmony_ci		handled |= CSR_INT_BIT_SW_ERR;
14618c2ecf20Sopenharmony_ci	}
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	/* uCode wakes up after power-down sleep */
14648c2ecf20Sopenharmony_ci	if (inta & CSR_INT_BIT_WAKEUP) {
14658c2ecf20Sopenharmony_ci		D_ISR("Wakeup interrupt\n");
14668c2ecf20Sopenharmony_ci		il_rx_queue_update_write_ptr(il, &il->rxq);
14678c2ecf20Sopenharmony_ci
14688c2ecf20Sopenharmony_ci		spin_lock_irqsave(&il->lock, flags);
14698c2ecf20Sopenharmony_ci		il_txq_update_write_ptr(il, &il->txq[0]);
14708c2ecf20Sopenharmony_ci		il_txq_update_write_ptr(il, &il->txq[1]);
14718c2ecf20Sopenharmony_ci		il_txq_update_write_ptr(il, &il->txq[2]);
14728c2ecf20Sopenharmony_ci		il_txq_update_write_ptr(il, &il->txq[3]);
14738c2ecf20Sopenharmony_ci		il_txq_update_write_ptr(il, &il->txq[4]);
14748c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&il->lock, flags);
14758c2ecf20Sopenharmony_ci
14768c2ecf20Sopenharmony_ci		il->isr_stats.wakeup++;
14778c2ecf20Sopenharmony_ci		handled |= CSR_INT_BIT_WAKEUP;
14788c2ecf20Sopenharmony_ci	}
14798c2ecf20Sopenharmony_ci
14808c2ecf20Sopenharmony_ci	/* All uCode command responses, including Tx command responses,
14818c2ecf20Sopenharmony_ci	 * Rx "responses" (frame-received notification), and other
14828c2ecf20Sopenharmony_ci	 * notifications from uCode come through here*/
14838c2ecf20Sopenharmony_ci	if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
14848c2ecf20Sopenharmony_ci		il3945_rx_handle(il);
14858c2ecf20Sopenharmony_ci		il->isr_stats.rx++;
14868c2ecf20Sopenharmony_ci		handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
14878c2ecf20Sopenharmony_ci	}
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_ci	if (inta & CSR_INT_BIT_FH_TX) {
14908c2ecf20Sopenharmony_ci		D_ISR("Tx interrupt\n");
14918c2ecf20Sopenharmony_ci		il->isr_stats.tx++;
14928c2ecf20Sopenharmony_ci
14938c2ecf20Sopenharmony_ci		_il_wr(il, CSR_FH_INT_STATUS, (1 << 6));
14948c2ecf20Sopenharmony_ci		il_wr(il, FH39_TCSR_CREDIT(FH39_SRVC_CHNL), 0x0);
14958c2ecf20Sopenharmony_ci		handled |= CSR_INT_BIT_FH_TX;
14968c2ecf20Sopenharmony_ci	}
14978c2ecf20Sopenharmony_ci
14988c2ecf20Sopenharmony_ci	if (inta & ~handled) {
14998c2ecf20Sopenharmony_ci		IL_ERR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
15008c2ecf20Sopenharmony_ci		il->isr_stats.unhandled++;
15018c2ecf20Sopenharmony_ci	}
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_ci	if (inta & ~il->inta_mask) {
15048c2ecf20Sopenharmony_ci		IL_WARN("Disabled INTA bits 0x%08x were pending\n",
15058c2ecf20Sopenharmony_ci			inta & ~il->inta_mask);
15068c2ecf20Sopenharmony_ci		IL_WARN("   with inta_fh = 0x%08x\n", inta_fh);
15078c2ecf20Sopenharmony_ci	}
15088c2ecf20Sopenharmony_ci
15098c2ecf20Sopenharmony_ci	/* Re-enable all interrupts */
15108c2ecf20Sopenharmony_ci	/* only Re-enable if disabled by irq */
15118c2ecf20Sopenharmony_ci	if (test_bit(S_INT_ENABLED, &il->status))
15128c2ecf20Sopenharmony_ci		il_enable_interrupts(il);
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLEGACY_DEBUG
15158c2ecf20Sopenharmony_ci	if (il_get_debug_level(il) & (IL_DL_ISR)) {
15168c2ecf20Sopenharmony_ci		inta = _il_rd(il, CSR_INT);
15178c2ecf20Sopenharmony_ci		inta_mask = _il_rd(il, CSR_INT_MASK);
15188c2ecf20Sopenharmony_ci		inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
15198c2ecf20Sopenharmony_ci		D_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
15208c2ecf20Sopenharmony_ci		      "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
15218c2ecf20Sopenharmony_ci	}
15228c2ecf20Sopenharmony_ci#endif
15238c2ecf20Sopenharmony_ci}
15248c2ecf20Sopenharmony_ci
15258c2ecf20Sopenharmony_cistatic int
15268c2ecf20Sopenharmony_ciil3945_get_channels_for_scan(struct il_priv *il, enum nl80211_band band,
15278c2ecf20Sopenharmony_ci			     u8 is_active, u8 n_probes,
15288c2ecf20Sopenharmony_ci			     struct il3945_scan_channel *scan_ch,
15298c2ecf20Sopenharmony_ci			     struct ieee80211_vif *vif)
15308c2ecf20Sopenharmony_ci{
15318c2ecf20Sopenharmony_ci	struct ieee80211_channel *chan;
15328c2ecf20Sopenharmony_ci	const struct ieee80211_supported_band *sband;
15338c2ecf20Sopenharmony_ci	const struct il_channel_info *ch_info;
15348c2ecf20Sopenharmony_ci	u16 passive_dwell = 0;
15358c2ecf20Sopenharmony_ci	u16 active_dwell = 0;
15368c2ecf20Sopenharmony_ci	int added, i;
15378c2ecf20Sopenharmony_ci
15388c2ecf20Sopenharmony_ci	sband = il_get_hw_mode(il, band);
15398c2ecf20Sopenharmony_ci	if (!sband)
15408c2ecf20Sopenharmony_ci		return 0;
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_ci	active_dwell = il_get_active_dwell_time(il, band, n_probes);
15438c2ecf20Sopenharmony_ci	passive_dwell = il_get_passive_dwell_time(il, band, vif);
15448c2ecf20Sopenharmony_ci
15458c2ecf20Sopenharmony_ci	if (passive_dwell <= active_dwell)
15468c2ecf20Sopenharmony_ci		passive_dwell = active_dwell + 1;
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci	for (i = 0, added = 0; i < il->scan_request->n_channels; i++) {
15498c2ecf20Sopenharmony_ci		chan = il->scan_request->channels[i];
15508c2ecf20Sopenharmony_ci
15518c2ecf20Sopenharmony_ci		if (chan->band != band)
15528c2ecf20Sopenharmony_ci			continue;
15538c2ecf20Sopenharmony_ci
15548c2ecf20Sopenharmony_ci		scan_ch->channel = chan->hw_value;
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci		ch_info = il_get_channel_info(il, band, scan_ch->channel);
15578c2ecf20Sopenharmony_ci		if (!il_is_channel_valid(ch_info)) {
15588c2ecf20Sopenharmony_ci			D_SCAN("Channel %d is INVALID for this band.\n",
15598c2ecf20Sopenharmony_ci			       scan_ch->channel);
15608c2ecf20Sopenharmony_ci			continue;
15618c2ecf20Sopenharmony_ci		}
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_ci		scan_ch->active_dwell = cpu_to_le16(active_dwell);
15648c2ecf20Sopenharmony_ci		scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
15658c2ecf20Sopenharmony_ci		/* If passive , set up for auto-switch
15668c2ecf20Sopenharmony_ci		 *  and use long active_dwell time.
15678c2ecf20Sopenharmony_ci		 */
15688c2ecf20Sopenharmony_ci		if (!is_active || il_is_channel_passive(ch_info) ||
15698c2ecf20Sopenharmony_ci		    (chan->flags & IEEE80211_CHAN_NO_IR)) {
15708c2ecf20Sopenharmony_ci			scan_ch->type = 0;	/* passive */
15718c2ecf20Sopenharmony_ci			if (IL_UCODE_API(il->ucode_ver) == 1)
15728c2ecf20Sopenharmony_ci				scan_ch->active_dwell =
15738c2ecf20Sopenharmony_ci				    cpu_to_le16(passive_dwell - 1);
15748c2ecf20Sopenharmony_ci		} else {
15758c2ecf20Sopenharmony_ci			scan_ch->type = 1;	/* active */
15768c2ecf20Sopenharmony_ci		}
15778c2ecf20Sopenharmony_ci
15788c2ecf20Sopenharmony_ci		/* Set direct probe bits. These may be used both for active
15798c2ecf20Sopenharmony_ci		 * scan channels (probes gets sent right away),
15808c2ecf20Sopenharmony_ci		 * or for passive channels (probes get se sent only after
15818c2ecf20Sopenharmony_ci		 * hearing clear Rx packet).*/
15828c2ecf20Sopenharmony_ci		if (IL_UCODE_API(il->ucode_ver) >= 2) {
15838c2ecf20Sopenharmony_ci			if (n_probes)
15848c2ecf20Sopenharmony_ci				scan_ch->type |= IL39_SCAN_PROBE_MASK(n_probes);
15858c2ecf20Sopenharmony_ci		} else {
15868c2ecf20Sopenharmony_ci			/* uCode v1 does not allow setting direct probe bits on
15878c2ecf20Sopenharmony_ci			 * passive channel. */
15888c2ecf20Sopenharmony_ci			if ((scan_ch->type & 1) && n_probes)
15898c2ecf20Sopenharmony_ci				scan_ch->type |= IL39_SCAN_PROBE_MASK(n_probes);
15908c2ecf20Sopenharmony_ci		}
15918c2ecf20Sopenharmony_ci
15928c2ecf20Sopenharmony_ci		/* Set txpower levels to defaults */
15938c2ecf20Sopenharmony_ci		scan_ch->tpc.dsp_atten = 110;
15948c2ecf20Sopenharmony_ci		/* scan_pwr_info->tpc.dsp_atten; */
15958c2ecf20Sopenharmony_ci
15968c2ecf20Sopenharmony_ci		/*scan_pwr_info->tpc.tx_gain; */
15978c2ecf20Sopenharmony_ci		if (band == NL80211_BAND_5GHZ)
15988c2ecf20Sopenharmony_ci			scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
15998c2ecf20Sopenharmony_ci		else {
16008c2ecf20Sopenharmony_ci			scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
16018c2ecf20Sopenharmony_ci			/* NOTE: if we were doing 6Mb OFDM for scans we'd use
16028c2ecf20Sopenharmony_ci			 * power level:
16038c2ecf20Sopenharmony_ci			 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
16048c2ecf20Sopenharmony_ci			 */
16058c2ecf20Sopenharmony_ci		}
16068c2ecf20Sopenharmony_ci
16078c2ecf20Sopenharmony_ci		D_SCAN("Scanning %d [%s %d]\n", scan_ch->channel,
16088c2ecf20Sopenharmony_ci		       (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
16098c2ecf20Sopenharmony_ci		       (scan_ch->type & 1) ? active_dwell : passive_dwell);
16108c2ecf20Sopenharmony_ci
16118c2ecf20Sopenharmony_ci		scan_ch++;
16128c2ecf20Sopenharmony_ci		added++;
16138c2ecf20Sopenharmony_ci	}
16148c2ecf20Sopenharmony_ci
16158c2ecf20Sopenharmony_ci	D_SCAN("total channels to scan %d\n", added);
16168c2ecf20Sopenharmony_ci	return added;
16178c2ecf20Sopenharmony_ci}
16188c2ecf20Sopenharmony_ci
16198c2ecf20Sopenharmony_cistatic void
16208c2ecf20Sopenharmony_ciil3945_init_hw_rates(struct il_priv *il, struct ieee80211_rate *rates)
16218c2ecf20Sopenharmony_ci{
16228c2ecf20Sopenharmony_ci	int i;
16238c2ecf20Sopenharmony_ci
16248c2ecf20Sopenharmony_ci	for (i = 0; i < RATE_COUNT_LEGACY; i++) {
16258c2ecf20Sopenharmony_ci		rates[i].bitrate = il3945_rates[i].ieee * 5;
16268c2ecf20Sopenharmony_ci		rates[i].hw_value = i;	/* Rate scaling will work on idxes */
16278c2ecf20Sopenharmony_ci		rates[i].hw_value_short = i;
16288c2ecf20Sopenharmony_ci		rates[i].flags = 0;
16298c2ecf20Sopenharmony_ci		if (i > IL39_LAST_OFDM_RATE || i < IL_FIRST_OFDM_RATE) {
16308c2ecf20Sopenharmony_ci			/*
16318c2ecf20Sopenharmony_ci			 * If CCK != 1M then set short preamble rate flag.
16328c2ecf20Sopenharmony_ci			 */
16338c2ecf20Sopenharmony_ci			rates[i].flags |=
16348c2ecf20Sopenharmony_ci			    (il3945_rates[i].plcp ==
16358c2ecf20Sopenharmony_ci			     10) ? 0 : IEEE80211_RATE_SHORT_PREAMBLE;
16368c2ecf20Sopenharmony_ci		}
16378c2ecf20Sopenharmony_ci	}
16388c2ecf20Sopenharmony_ci}
16398c2ecf20Sopenharmony_ci
16408c2ecf20Sopenharmony_ci/******************************************************************************
16418c2ecf20Sopenharmony_ci *
16428c2ecf20Sopenharmony_ci * uCode download functions
16438c2ecf20Sopenharmony_ci *
16448c2ecf20Sopenharmony_ci ******************************************************************************/
16458c2ecf20Sopenharmony_ci
16468c2ecf20Sopenharmony_cistatic void
16478c2ecf20Sopenharmony_ciil3945_dealloc_ucode_pci(struct il_priv *il)
16488c2ecf20Sopenharmony_ci{
16498c2ecf20Sopenharmony_ci	il_free_fw_desc(il->pci_dev, &il->ucode_code);
16508c2ecf20Sopenharmony_ci	il_free_fw_desc(il->pci_dev, &il->ucode_data);
16518c2ecf20Sopenharmony_ci	il_free_fw_desc(il->pci_dev, &il->ucode_data_backup);
16528c2ecf20Sopenharmony_ci	il_free_fw_desc(il->pci_dev, &il->ucode_init);
16538c2ecf20Sopenharmony_ci	il_free_fw_desc(il->pci_dev, &il->ucode_init_data);
16548c2ecf20Sopenharmony_ci	il_free_fw_desc(il->pci_dev, &il->ucode_boot);
16558c2ecf20Sopenharmony_ci}
16568c2ecf20Sopenharmony_ci
16578c2ecf20Sopenharmony_ci/*
16588c2ecf20Sopenharmony_ci * il3945_verify_inst_full - verify runtime uCode image in card vs. host,
16598c2ecf20Sopenharmony_ci *     looking at all data.
16608c2ecf20Sopenharmony_ci */
16618c2ecf20Sopenharmony_cistatic int
16628c2ecf20Sopenharmony_ciil3945_verify_inst_full(struct il_priv *il, __le32 * image, u32 len)
16638c2ecf20Sopenharmony_ci{
16648c2ecf20Sopenharmony_ci	u32 val;
16658c2ecf20Sopenharmony_ci	u32 save_len = len;
16668c2ecf20Sopenharmony_ci	int rc = 0;
16678c2ecf20Sopenharmony_ci	u32 errcnt;
16688c2ecf20Sopenharmony_ci
16698c2ecf20Sopenharmony_ci	D_INFO("ucode inst image size is %u\n", len);
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_ci	il_wr(il, HBUS_TARG_MEM_RADDR, IL39_RTC_INST_LOWER_BOUND);
16728c2ecf20Sopenharmony_ci
16738c2ecf20Sopenharmony_ci	errcnt = 0;
16748c2ecf20Sopenharmony_ci	for (; len > 0; len -= sizeof(u32), image++) {
16758c2ecf20Sopenharmony_ci		/* read data comes through single port, auto-incr addr */
16768c2ecf20Sopenharmony_ci		/* NOTE: Use the debugless read so we don't flood kernel log
16778c2ecf20Sopenharmony_ci		 * if IL_DL_IO is set */
16788c2ecf20Sopenharmony_ci		val = _il_rd(il, HBUS_TARG_MEM_RDAT);
16798c2ecf20Sopenharmony_ci		if (val != le32_to_cpu(*image)) {
16808c2ecf20Sopenharmony_ci			IL_ERR("uCode INST section is invalid at "
16818c2ecf20Sopenharmony_ci			       "offset 0x%x, is 0x%x, s/b 0x%x\n",
16828c2ecf20Sopenharmony_ci			       save_len - len, val, le32_to_cpu(*image));
16838c2ecf20Sopenharmony_ci			rc = -EIO;
16848c2ecf20Sopenharmony_ci			errcnt++;
16858c2ecf20Sopenharmony_ci			if (errcnt >= 20)
16868c2ecf20Sopenharmony_ci				break;
16878c2ecf20Sopenharmony_ci		}
16888c2ecf20Sopenharmony_ci	}
16898c2ecf20Sopenharmony_ci
16908c2ecf20Sopenharmony_ci	if (!errcnt)
16918c2ecf20Sopenharmony_ci		D_INFO("ucode image in INSTRUCTION memory is good\n");
16928c2ecf20Sopenharmony_ci
16938c2ecf20Sopenharmony_ci	return rc;
16948c2ecf20Sopenharmony_ci}
16958c2ecf20Sopenharmony_ci
16968c2ecf20Sopenharmony_ci/*
16978c2ecf20Sopenharmony_ci * il3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
16988c2ecf20Sopenharmony_ci *   using sample data 100 bytes apart.  If these sample points are good,
16998c2ecf20Sopenharmony_ci *   it's a pretty good bet that everything between them is good, too.
17008c2ecf20Sopenharmony_ci */
17018c2ecf20Sopenharmony_cistatic int
17028c2ecf20Sopenharmony_ciil3945_verify_inst_sparse(struct il_priv *il, __le32 * image, u32 len)
17038c2ecf20Sopenharmony_ci{
17048c2ecf20Sopenharmony_ci	u32 val;
17058c2ecf20Sopenharmony_ci	int rc = 0;
17068c2ecf20Sopenharmony_ci	u32 errcnt = 0;
17078c2ecf20Sopenharmony_ci	u32 i;
17088c2ecf20Sopenharmony_ci
17098c2ecf20Sopenharmony_ci	D_INFO("ucode inst image size is %u\n", len);
17108c2ecf20Sopenharmony_ci
17118c2ecf20Sopenharmony_ci	for (i = 0; i < len; i += 100, image += 100 / sizeof(u32)) {
17128c2ecf20Sopenharmony_ci		/* read data comes through single port, auto-incr addr */
17138c2ecf20Sopenharmony_ci		/* NOTE: Use the debugless read so we don't flood kernel log
17148c2ecf20Sopenharmony_ci		 * if IL_DL_IO is set */
17158c2ecf20Sopenharmony_ci		il_wr(il, HBUS_TARG_MEM_RADDR, i + IL39_RTC_INST_LOWER_BOUND);
17168c2ecf20Sopenharmony_ci		val = _il_rd(il, HBUS_TARG_MEM_RDAT);
17178c2ecf20Sopenharmony_ci		if (val != le32_to_cpu(*image)) {
17188c2ecf20Sopenharmony_ci#if 0				/* Enable this if you want to see details */
17198c2ecf20Sopenharmony_ci			IL_ERR("uCode INST section is invalid at "
17208c2ecf20Sopenharmony_ci			       "offset 0x%x, is 0x%x, s/b 0x%x\n", i, val,
17218c2ecf20Sopenharmony_ci			       *image);
17228c2ecf20Sopenharmony_ci#endif
17238c2ecf20Sopenharmony_ci			rc = -EIO;
17248c2ecf20Sopenharmony_ci			errcnt++;
17258c2ecf20Sopenharmony_ci			if (errcnt >= 3)
17268c2ecf20Sopenharmony_ci				break;
17278c2ecf20Sopenharmony_ci		}
17288c2ecf20Sopenharmony_ci	}
17298c2ecf20Sopenharmony_ci
17308c2ecf20Sopenharmony_ci	return rc;
17318c2ecf20Sopenharmony_ci}
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_ci/*
17348c2ecf20Sopenharmony_ci * il3945_verify_ucode - determine which instruction image is in SRAM,
17358c2ecf20Sopenharmony_ci *    and verify its contents
17368c2ecf20Sopenharmony_ci */
17378c2ecf20Sopenharmony_cistatic int
17388c2ecf20Sopenharmony_ciil3945_verify_ucode(struct il_priv *il)
17398c2ecf20Sopenharmony_ci{
17408c2ecf20Sopenharmony_ci	__le32 *image;
17418c2ecf20Sopenharmony_ci	u32 len;
17428c2ecf20Sopenharmony_ci	int rc = 0;
17438c2ecf20Sopenharmony_ci
17448c2ecf20Sopenharmony_ci	/* Try bootstrap */
17458c2ecf20Sopenharmony_ci	image = (__le32 *) il->ucode_boot.v_addr;
17468c2ecf20Sopenharmony_ci	len = il->ucode_boot.len;
17478c2ecf20Sopenharmony_ci	rc = il3945_verify_inst_sparse(il, image, len);
17488c2ecf20Sopenharmony_ci	if (rc == 0) {
17498c2ecf20Sopenharmony_ci		D_INFO("Bootstrap uCode is good in inst SRAM\n");
17508c2ecf20Sopenharmony_ci		return 0;
17518c2ecf20Sopenharmony_ci	}
17528c2ecf20Sopenharmony_ci
17538c2ecf20Sopenharmony_ci	/* Try initialize */
17548c2ecf20Sopenharmony_ci	image = (__le32 *) il->ucode_init.v_addr;
17558c2ecf20Sopenharmony_ci	len = il->ucode_init.len;
17568c2ecf20Sopenharmony_ci	rc = il3945_verify_inst_sparse(il, image, len);
17578c2ecf20Sopenharmony_ci	if (rc == 0) {
17588c2ecf20Sopenharmony_ci		D_INFO("Initialize uCode is good in inst SRAM\n");
17598c2ecf20Sopenharmony_ci		return 0;
17608c2ecf20Sopenharmony_ci	}
17618c2ecf20Sopenharmony_ci
17628c2ecf20Sopenharmony_ci	/* Try runtime/protocol */
17638c2ecf20Sopenharmony_ci	image = (__le32 *) il->ucode_code.v_addr;
17648c2ecf20Sopenharmony_ci	len = il->ucode_code.len;
17658c2ecf20Sopenharmony_ci	rc = il3945_verify_inst_sparse(il, image, len);
17668c2ecf20Sopenharmony_ci	if (rc == 0) {
17678c2ecf20Sopenharmony_ci		D_INFO("Runtime uCode is good in inst SRAM\n");
17688c2ecf20Sopenharmony_ci		return 0;
17698c2ecf20Sopenharmony_ci	}
17708c2ecf20Sopenharmony_ci
17718c2ecf20Sopenharmony_ci	IL_ERR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
17728c2ecf20Sopenharmony_ci
17738c2ecf20Sopenharmony_ci	/* Since nothing seems to match, show first several data entries in
17748c2ecf20Sopenharmony_ci	 * instruction SRAM, so maybe visual inspection will give a clue.
17758c2ecf20Sopenharmony_ci	 * Selection of bootstrap image (vs. other images) is arbitrary. */
17768c2ecf20Sopenharmony_ci	image = (__le32 *) il->ucode_boot.v_addr;
17778c2ecf20Sopenharmony_ci	len = il->ucode_boot.len;
17788c2ecf20Sopenharmony_ci	rc = il3945_verify_inst_full(il, image, len);
17798c2ecf20Sopenharmony_ci
17808c2ecf20Sopenharmony_ci	return rc;
17818c2ecf20Sopenharmony_ci}
17828c2ecf20Sopenharmony_ci
17838c2ecf20Sopenharmony_cistatic void
17848c2ecf20Sopenharmony_ciil3945_nic_start(struct il_priv *il)
17858c2ecf20Sopenharmony_ci{
17868c2ecf20Sopenharmony_ci	/* Remove all resets to allow NIC to operate */
17878c2ecf20Sopenharmony_ci	_il_wr(il, CSR_RESET, 0);
17888c2ecf20Sopenharmony_ci}
17898c2ecf20Sopenharmony_ci
17908c2ecf20Sopenharmony_ci#define IL3945_UCODE_GET(item)						\
17918c2ecf20Sopenharmony_cistatic u32 il3945_ucode_get_##item(const struct il_ucode_header *ucode)\
17928c2ecf20Sopenharmony_ci{									\
17938c2ecf20Sopenharmony_ci	return le32_to_cpu(ucode->v1.item);				\
17948c2ecf20Sopenharmony_ci}
17958c2ecf20Sopenharmony_ci
17968c2ecf20Sopenharmony_cistatic u32
17978c2ecf20Sopenharmony_ciil3945_ucode_get_header_size(u32 api_ver)
17988c2ecf20Sopenharmony_ci{
17998c2ecf20Sopenharmony_ci	return 24;
18008c2ecf20Sopenharmony_ci}
18018c2ecf20Sopenharmony_ci
18028c2ecf20Sopenharmony_cistatic u8 *
18038c2ecf20Sopenharmony_ciil3945_ucode_get_data(const struct il_ucode_header *ucode)
18048c2ecf20Sopenharmony_ci{
18058c2ecf20Sopenharmony_ci	return (u8 *) ucode->v1.data;
18068c2ecf20Sopenharmony_ci}
18078c2ecf20Sopenharmony_ci
18088c2ecf20Sopenharmony_ciIL3945_UCODE_GET(inst_size);
18098c2ecf20Sopenharmony_ciIL3945_UCODE_GET(data_size);
18108c2ecf20Sopenharmony_ciIL3945_UCODE_GET(init_size);
18118c2ecf20Sopenharmony_ciIL3945_UCODE_GET(init_data_size);
18128c2ecf20Sopenharmony_ciIL3945_UCODE_GET(boot_size);
18138c2ecf20Sopenharmony_ci
18148c2ecf20Sopenharmony_ci/*
18158c2ecf20Sopenharmony_ci * il3945_read_ucode - Read uCode images from disk file.
18168c2ecf20Sopenharmony_ci *
18178c2ecf20Sopenharmony_ci * Copy into buffers for card to fetch via bus-mastering
18188c2ecf20Sopenharmony_ci */
18198c2ecf20Sopenharmony_cistatic int
18208c2ecf20Sopenharmony_ciil3945_read_ucode(struct il_priv *il)
18218c2ecf20Sopenharmony_ci{
18228c2ecf20Sopenharmony_ci	const struct il_ucode_header *ucode;
18238c2ecf20Sopenharmony_ci	int ret = -EINVAL, idx;
18248c2ecf20Sopenharmony_ci	const struct firmware *ucode_raw;
18258c2ecf20Sopenharmony_ci	/* firmware file name contains uCode/driver compatibility version */
18268c2ecf20Sopenharmony_ci	const char *name_pre = il->cfg->fw_name_pre;
18278c2ecf20Sopenharmony_ci	const unsigned int api_max = il->cfg->ucode_api_max;
18288c2ecf20Sopenharmony_ci	const unsigned int api_min = il->cfg->ucode_api_min;
18298c2ecf20Sopenharmony_ci	char buf[25];
18308c2ecf20Sopenharmony_ci	u8 *src;
18318c2ecf20Sopenharmony_ci	size_t len;
18328c2ecf20Sopenharmony_ci	u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
18338c2ecf20Sopenharmony_ci
18348c2ecf20Sopenharmony_ci	/* Ask kernel firmware_class module to get the boot firmware off disk.
18358c2ecf20Sopenharmony_ci	 * request_firmware() is synchronous, file is in memory on return. */
18368c2ecf20Sopenharmony_ci	for (idx = api_max; idx >= api_min; idx--) {
18378c2ecf20Sopenharmony_ci		sprintf(buf, "%s%u%s", name_pre, idx, ".ucode");
18388c2ecf20Sopenharmony_ci		ret = request_firmware(&ucode_raw, buf, &il->pci_dev->dev);
18398c2ecf20Sopenharmony_ci		if (ret < 0) {
18408c2ecf20Sopenharmony_ci			IL_ERR("%s firmware file req failed: %d\n", buf, ret);
18418c2ecf20Sopenharmony_ci			if (ret == -ENOENT)
18428c2ecf20Sopenharmony_ci				continue;
18438c2ecf20Sopenharmony_ci			else
18448c2ecf20Sopenharmony_ci				goto error;
18458c2ecf20Sopenharmony_ci		} else {
18468c2ecf20Sopenharmony_ci			if (idx < api_max)
18478c2ecf20Sopenharmony_ci				IL_ERR("Loaded firmware %s, "
18488c2ecf20Sopenharmony_ci				       "which is deprecated. "
18498c2ecf20Sopenharmony_ci				       " Please use API v%u instead.\n", buf,
18508c2ecf20Sopenharmony_ci				       api_max);
18518c2ecf20Sopenharmony_ci			D_INFO("Got firmware '%s' file "
18528c2ecf20Sopenharmony_ci			       "(%zd bytes) from disk\n", buf, ucode_raw->size);
18538c2ecf20Sopenharmony_ci			break;
18548c2ecf20Sopenharmony_ci		}
18558c2ecf20Sopenharmony_ci	}
18568c2ecf20Sopenharmony_ci
18578c2ecf20Sopenharmony_ci	if (ret < 0)
18588c2ecf20Sopenharmony_ci		goto error;
18598c2ecf20Sopenharmony_ci
18608c2ecf20Sopenharmony_ci	/* Make sure that we got at least our header! */
18618c2ecf20Sopenharmony_ci	if (ucode_raw->size < il3945_ucode_get_header_size(1)) {
18628c2ecf20Sopenharmony_ci		IL_ERR("File size way too small!\n");
18638c2ecf20Sopenharmony_ci		ret = -EINVAL;
18648c2ecf20Sopenharmony_ci		goto err_release;
18658c2ecf20Sopenharmony_ci	}
18668c2ecf20Sopenharmony_ci
18678c2ecf20Sopenharmony_ci	/* Data from ucode file:  header followed by uCode images */
18688c2ecf20Sopenharmony_ci	ucode = (struct il_ucode_header *)ucode_raw->data;
18698c2ecf20Sopenharmony_ci
18708c2ecf20Sopenharmony_ci	il->ucode_ver = le32_to_cpu(ucode->ver);
18718c2ecf20Sopenharmony_ci	api_ver = IL_UCODE_API(il->ucode_ver);
18728c2ecf20Sopenharmony_ci	inst_size = il3945_ucode_get_inst_size(ucode);
18738c2ecf20Sopenharmony_ci	data_size = il3945_ucode_get_data_size(ucode);
18748c2ecf20Sopenharmony_ci	init_size = il3945_ucode_get_init_size(ucode);
18758c2ecf20Sopenharmony_ci	init_data_size = il3945_ucode_get_init_data_size(ucode);
18768c2ecf20Sopenharmony_ci	boot_size = il3945_ucode_get_boot_size(ucode);
18778c2ecf20Sopenharmony_ci	src = il3945_ucode_get_data(ucode);
18788c2ecf20Sopenharmony_ci
18798c2ecf20Sopenharmony_ci	/* api_ver should match the api version forming part of the
18808c2ecf20Sopenharmony_ci	 * firmware filename ... but we don't check for that and only rely
18818c2ecf20Sopenharmony_ci	 * on the API version read from firmware header from here on forward */
18828c2ecf20Sopenharmony_ci
18838c2ecf20Sopenharmony_ci	if (api_ver < api_min || api_ver > api_max) {
18848c2ecf20Sopenharmony_ci		IL_ERR("Driver unable to support your firmware API. "
18858c2ecf20Sopenharmony_ci		       "Driver supports v%u, firmware is v%u.\n", api_max,
18868c2ecf20Sopenharmony_ci		       api_ver);
18878c2ecf20Sopenharmony_ci		il->ucode_ver = 0;
18888c2ecf20Sopenharmony_ci		ret = -EINVAL;
18898c2ecf20Sopenharmony_ci		goto err_release;
18908c2ecf20Sopenharmony_ci	}
18918c2ecf20Sopenharmony_ci	if (api_ver != api_max)
18928c2ecf20Sopenharmony_ci		IL_ERR("Firmware has old API version. Expected %u, "
18938c2ecf20Sopenharmony_ci		       "got %u. New firmware can be obtained "
18948c2ecf20Sopenharmony_ci		       "from http://www.intellinuxwireless.org.\n", api_max,
18958c2ecf20Sopenharmony_ci		       api_ver);
18968c2ecf20Sopenharmony_ci
18978c2ecf20Sopenharmony_ci	IL_INFO("loaded firmware version %u.%u.%u.%u\n",
18988c2ecf20Sopenharmony_ci		IL_UCODE_MAJOR(il->ucode_ver), IL_UCODE_MINOR(il->ucode_ver),
18998c2ecf20Sopenharmony_ci		IL_UCODE_API(il->ucode_ver), IL_UCODE_SERIAL(il->ucode_ver));
19008c2ecf20Sopenharmony_ci
19018c2ecf20Sopenharmony_ci	snprintf(il->hw->wiphy->fw_version, sizeof(il->hw->wiphy->fw_version),
19028c2ecf20Sopenharmony_ci		 "%u.%u.%u.%u", IL_UCODE_MAJOR(il->ucode_ver),
19038c2ecf20Sopenharmony_ci		 IL_UCODE_MINOR(il->ucode_ver), IL_UCODE_API(il->ucode_ver),
19048c2ecf20Sopenharmony_ci		 IL_UCODE_SERIAL(il->ucode_ver));
19058c2ecf20Sopenharmony_ci
19068c2ecf20Sopenharmony_ci	D_INFO("f/w package hdr ucode version raw = 0x%x\n", il->ucode_ver);
19078c2ecf20Sopenharmony_ci	D_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
19088c2ecf20Sopenharmony_ci	D_INFO("f/w package hdr runtime data size = %u\n", data_size);
19098c2ecf20Sopenharmony_ci	D_INFO("f/w package hdr init inst size = %u\n", init_size);
19108c2ecf20Sopenharmony_ci	D_INFO("f/w package hdr init data size = %u\n", init_data_size);
19118c2ecf20Sopenharmony_ci	D_INFO("f/w package hdr boot inst size = %u\n", boot_size);
19128c2ecf20Sopenharmony_ci
19138c2ecf20Sopenharmony_ci	/* Verify size of file vs. image size info in file's header */
19148c2ecf20Sopenharmony_ci	if (ucode_raw->size !=
19158c2ecf20Sopenharmony_ci	    il3945_ucode_get_header_size(api_ver) + inst_size + data_size +
19168c2ecf20Sopenharmony_ci	    init_size + init_data_size + boot_size) {
19178c2ecf20Sopenharmony_ci
19188c2ecf20Sopenharmony_ci		D_INFO("uCode file size %zd does not match expected size\n",
19198c2ecf20Sopenharmony_ci		       ucode_raw->size);
19208c2ecf20Sopenharmony_ci		ret = -EINVAL;
19218c2ecf20Sopenharmony_ci		goto err_release;
19228c2ecf20Sopenharmony_ci	}
19238c2ecf20Sopenharmony_ci
19248c2ecf20Sopenharmony_ci	/* Verify that uCode images will fit in card's SRAM */
19258c2ecf20Sopenharmony_ci	if (inst_size > IL39_MAX_INST_SIZE) {
19268c2ecf20Sopenharmony_ci		D_INFO("uCode instr len %d too large to fit in\n", inst_size);
19278c2ecf20Sopenharmony_ci		ret = -EINVAL;
19288c2ecf20Sopenharmony_ci		goto err_release;
19298c2ecf20Sopenharmony_ci	}
19308c2ecf20Sopenharmony_ci
19318c2ecf20Sopenharmony_ci	if (data_size > IL39_MAX_DATA_SIZE) {
19328c2ecf20Sopenharmony_ci		D_INFO("uCode data len %d too large to fit in\n", data_size);
19338c2ecf20Sopenharmony_ci		ret = -EINVAL;
19348c2ecf20Sopenharmony_ci		goto err_release;
19358c2ecf20Sopenharmony_ci	}
19368c2ecf20Sopenharmony_ci	if (init_size > IL39_MAX_INST_SIZE) {
19378c2ecf20Sopenharmony_ci		D_INFO("uCode init instr len %d too large to fit in\n",
19388c2ecf20Sopenharmony_ci		       init_size);
19398c2ecf20Sopenharmony_ci		ret = -EINVAL;
19408c2ecf20Sopenharmony_ci		goto err_release;
19418c2ecf20Sopenharmony_ci	}
19428c2ecf20Sopenharmony_ci	if (init_data_size > IL39_MAX_DATA_SIZE) {
19438c2ecf20Sopenharmony_ci		D_INFO("uCode init data len %d too large to fit in\n",
19448c2ecf20Sopenharmony_ci		       init_data_size);
19458c2ecf20Sopenharmony_ci		ret = -EINVAL;
19468c2ecf20Sopenharmony_ci		goto err_release;
19478c2ecf20Sopenharmony_ci	}
19488c2ecf20Sopenharmony_ci	if (boot_size > IL39_MAX_BSM_SIZE) {
19498c2ecf20Sopenharmony_ci		D_INFO("uCode boot instr len %d too large to fit in\n",
19508c2ecf20Sopenharmony_ci		       boot_size);
19518c2ecf20Sopenharmony_ci		ret = -EINVAL;
19528c2ecf20Sopenharmony_ci		goto err_release;
19538c2ecf20Sopenharmony_ci	}
19548c2ecf20Sopenharmony_ci
19558c2ecf20Sopenharmony_ci	/* Allocate ucode buffers for card's bus-master loading ... */
19568c2ecf20Sopenharmony_ci
19578c2ecf20Sopenharmony_ci	/* Runtime instructions and 2 copies of data:
19588c2ecf20Sopenharmony_ci	 * 1) unmodified from disk
19598c2ecf20Sopenharmony_ci	 * 2) backup cache for save/restore during power-downs */
19608c2ecf20Sopenharmony_ci	il->ucode_code.len = inst_size;
19618c2ecf20Sopenharmony_ci	il_alloc_fw_desc(il->pci_dev, &il->ucode_code);
19628c2ecf20Sopenharmony_ci
19638c2ecf20Sopenharmony_ci	il->ucode_data.len = data_size;
19648c2ecf20Sopenharmony_ci	il_alloc_fw_desc(il->pci_dev, &il->ucode_data);
19658c2ecf20Sopenharmony_ci
19668c2ecf20Sopenharmony_ci	il->ucode_data_backup.len = data_size;
19678c2ecf20Sopenharmony_ci	il_alloc_fw_desc(il->pci_dev, &il->ucode_data_backup);
19688c2ecf20Sopenharmony_ci
19698c2ecf20Sopenharmony_ci	if (!il->ucode_code.v_addr || !il->ucode_data.v_addr ||
19708c2ecf20Sopenharmony_ci	    !il->ucode_data_backup.v_addr)
19718c2ecf20Sopenharmony_ci		goto err_pci_alloc;
19728c2ecf20Sopenharmony_ci
19738c2ecf20Sopenharmony_ci	/* Initialization instructions and data */
19748c2ecf20Sopenharmony_ci	if (init_size && init_data_size) {
19758c2ecf20Sopenharmony_ci		il->ucode_init.len = init_size;
19768c2ecf20Sopenharmony_ci		il_alloc_fw_desc(il->pci_dev, &il->ucode_init);
19778c2ecf20Sopenharmony_ci
19788c2ecf20Sopenharmony_ci		il->ucode_init_data.len = init_data_size;
19798c2ecf20Sopenharmony_ci		il_alloc_fw_desc(il->pci_dev, &il->ucode_init_data);
19808c2ecf20Sopenharmony_ci
19818c2ecf20Sopenharmony_ci		if (!il->ucode_init.v_addr || !il->ucode_init_data.v_addr)
19828c2ecf20Sopenharmony_ci			goto err_pci_alloc;
19838c2ecf20Sopenharmony_ci	}
19848c2ecf20Sopenharmony_ci
19858c2ecf20Sopenharmony_ci	/* Bootstrap (instructions only, no data) */
19868c2ecf20Sopenharmony_ci	if (boot_size) {
19878c2ecf20Sopenharmony_ci		il->ucode_boot.len = boot_size;
19888c2ecf20Sopenharmony_ci		il_alloc_fw_desc(il->pci_dev, &il->ucode_boot);
19898c2ecf20Sopenharmony_ci
19908c2ecf20Sopenharmony_ci		if (!il->ucode_boot.v_addr)
19918c2ecf20Sopenharmony_ci			goto err_pci_alloc;
19928c2ecf20Sopenharmony_ci	}
19938c2ecf20Sopenharmony_ci
19948c2ecf20Sopenharmony_ci	/* Copy images into buffers for card's bus-master reads ... */
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_ci	/* Runtime instructions (first block of data in file) */
19978c2ecf20Sopenharmony_ci	len = inst_size;
19988c2ecf20Sopenharmony_ci	D_INFO("Copying (but not loading) uCode instr len %zd\n", len);
19998c2ecf20Sopenharmony_ci	memcpy(il->ucode_code.v_addr, src, len);
20008c2ecf20Sopenharmony_ci	src += len;
20018c2ecf20Sopenharmony_ci
20028c2ecf20Sopenharmony_ci	D_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
20038c2ecf20Sopenharmony_ci	       il->ucode_code.v_addr, (u32) il->ucode_code.p_addr);
20048c2ecf20Sopenharmony_ci
20058c2ecf20Sopenharmony_ci	/* Runtime data (2nd block)
20068c2ecf20Sopenharmony_ci	 * NOTE:  Copy into backup buffer will be done in il3945_up()  */
20078c2ecf20Sopenharmony_ci	len = data_size;
20088c2ecf20Sopenharmony_ci	D_INFO("Copying (but not loading) uCode data len %zd\n", len);
20098c2ecf20Sopenharmony_ci	memcpy(il->ucode_data.v_addr, src, len);
20108c2ecf20Sopenharmony_ci	memcpy(il->ucode_data_backup.v_addr, src, len);
20118c2ecf20Sopenharmony_ci	src += len;
20128c2ecf20Sopenharmony_ci
20138c2ecf20Sopenharmony_ci	/* Initialization instructions (3rd block) */
20148c2ecf20Sopenharmony_ci	if (init_size) {
20158c2ecf20Sopenharmony_ci		len = init_size;
20168c2ecf20Sopenharmony_ci		D_INFO("Copying (but not loading) init instr len %zd\n", len);
20178c2ecf20Sopenharmony_ci		memcpy(il->ucode_init.v_addr, src, len);
20188c2ecf20Sopenharmony_ci		src += len;
20198c2ecf20Sopenharmony_ci	}
20208c2ecf20Sopenharmony_ci
20218c2ecf20Sopenharmony_ci	/* Initialization data (4th block) */
20228c2ecf20Sopenharmony_ci	if (init_data_size) {
20238c2ecf20Sopenharmony_ci		len = init_data_size;
20248c2ecf20Sopenharmony_ci		D_INFO("Copying (but not loading) init data len %zd\n", len);
20258c2ecf20Sopenharmony_ci		memcpy(il->ucode_init_data.v_addr, src, len);
20268c2ecf20Sopenharmony_ci		src += len;
20278c2ecf20Sopenharmony_ci	}
20288c2ecf20Sopenharmony_ci
20298c2ecf20Sopenharmony_ci	/* Bootstrap instructions (5th block) */
20308c2ecf20Sopenharmony_ci	len = boot_size;
20318c2ecf20Sopenharmony_ci	D_INFO("Copying (but not loading) boot instr len %zd\n", len);
20328c2ecf20Sopenharmony_ci	memcpy(il->ucode_boot.v_addr, src, len);
20338c2ecf20Sopenharmony_ci
20348c2ecf20Sopenharmony_ci	/* We have our copies now, allow OS release its copies */
20358c2ecf20Sopenharmony_ci	release_firmware(ucode_raw);
20368c2ecf20Sopenharmony_ci	return 0;
20378c2ecf20Sopenharmony_ci
20388c2ecf20Sopenharmony_cierr_pci_alloc:
20398c2ecf20Sopenharmony_ci	IL_ERR("failed to allocate pci memory\n");
20408c2ecf20Sopenharmony_ci	ret = -ENOMEM;
20418c2ecf20Sopenharmony_ci	il3945_dealloc_ucode_pci(il);
20428c2ecf20Sopenharmony_ci
20438c2ecf20Sopenharmony_cierr_release:
20448c2ecf20Sopenharmony_ci	release_firmware(ucode_raw);
20458c2ecf20Sopenharmony_ci
20468c2ecf20Sopenharmony_cierror:
20478c2ecf20Sopenharmony_ci	return ret;
20488c2ecf20Sopenharmony_ci}
20498c2ecf20Sopenharmony_ci
20508c2ecf20Sopenharmony_ci/*
20518c2ecf20Sopenharmony_ci * il3945_set_ucode_ptrs - Set uCode address location
20528c2ecf20Sopenharmony_ci *
20538c2ecf20Sopenharmony_ci * Tell initialization uCode where to find runtime uCode.
20548c2ecf20Sopenharmony_ci *
20558c2ecf20Sopenharmony_ci * BSM registers initially contain pointers to initialization uCode.
20568c2ecf20Sopenharmony_ci * We need to replace them to load runtime uCode inst and data,
20578c2ecf20Sopenharmony_ci * and to save runtime data when powering down.
20588c2ecf20Sopenharmony_ci */
20598c2ecf20Sopenharmony_cistatic int
20608c2ecf20Sopenharmony_ciil3945_set_ucode_ptrs(struct il_priv *il)
20618c2ecf20Sopenharmony_ci{
20628c2ecf20Sopenharmony_ci	dma_addr_t pinst;
20638c2ecf20Sopenharmony_ci	dma_addr_t pdata;
20648c2ecf20Sopenharmony_ci
20658c2ecf20Sopenharmony_ci	/* bits 31:0 for 3945 */
20668c2ecf20Sopenharmony_ci	pinst = il->ucode_code.p_addr;
20678c2ecf20Sopenharmony_ci	pdata = il->ucode_data_backup.p_addr;
20688c2ecf20Sopenharmony_ci
20698c2ecf20Sopenharmony_ci	/* Tell bootstrap uCode where to find image to load */
20708c2ecf20Sopenharmony_ci	il_wr_prph(il, BSM_DRAM_INST_PTR_REG, pinst);
20718c2ecf20Sopenharmony_ci	il_wr_prph(il, BSM_DRAM_DATA_PTR_REG, pdata);
20728c2ecf20Sopenharmony_ci	il_wr_prph(il, BSM_DRAM_DATA_BYTECOUNT_REG, il->ucode_data.len);
20738c2ecf20Sopenharmony_ci
20748c2ecf20Sopenharmony_ci	/* Inst byte count must be last to set up, bit 31 signals uCode
20758c2ecf20Sopenharmony_ci	 *   that all new ptr/size info is in place */
20768c2ecf20Sopenharmony_ci	il_wr_prph(il, BSM_DRAM_INST_BYTECOUNT_REG,
20778c2ecf20Sopenharmony_ci		   il->ucode_code.len | BSM_DRAM_INST_LOAD);
20788c2ecf20Sopenharmony_ci
20798c2ecf20Sopenharmony_ci	D_INFO("Runtime uCode pointers are set.\n");
20808c2ecf20Sopenharmony_ci
20818c2ecf20Sopenharmony_ci	return 0;
20828c2ecf20Sopenharmony_ci}
20838c2ecf20Sopenharmony_ci
20848c2ecf20Sopenharmony_ci/*
20858c2ecf20Sopenharmony_ci * il3945_init_alive_start - Called after N_ALIVE notification received
20868c2ecf20Sopenharmony_ci *
20878c2ecf20Sopenharmony_ci * Called after N_ALIVE notification received from "initialize" uCode.
20888c2ecf20Sopenharmony_ci *
20898c2ecf20Sopenharmony_ci * Tell "initialize" uCode to go ahead and load the runtime uCode.
20908c2ecf20Sopenharmony_ci */
20918c2ecf20Sopenharmony_cistatic void
20928c2ecf20Sopenharmony_ciil3945_init_alive_start(struct il_priv *il)
20938c2ecf20Sopenharmony_ci{
20948c2ecf20Sopenharmony_ci	/* Check alive response for "valid" sign from uCode */
20958c2ecf20Sopenharmony_ci	if (il->card_alive_init.is_valid != UCODE_VALID_OK) {
20968c2ecf20Sopenharmony_ci		/* We had an error bringing up the hardware, so take it
20978c2ecf20Sopenharmony_ci		 * all the way back down so we can try again */
20988c2ecf20Sopenharmony_ci		D_INFO("Initialize Alive failed.\n");
20998c2ecf20Sopenharmony_ci		goto restart;
21008c2ecf20Sopenharmony_ci	}
21018c2ecf20Sopenharmony_ci
21028c2ecf20Sopenharmony_ci	/* Bootstrap uCode has loaded initialize uCode ... verify inst image.
21038c2ecf20Sopenharmony_ci	 * This is a paranoid check, because we would not have gotten the
21048c2ecf20Sopenharmony_ci	 * "initialize" alive if code weren't properly loaded.  */
21058c2ecf20Sopenharmony_ci	if (il3945_verify_ucode(il)) {
21068c2ecf20Sopenharmony_ci		/* Runtime instruction load was bad;
21078c2ecf20Sopenharmony_ci		 * take it all the way back down so we can try again */
21088c2ecf20Sopenharmony_ci		D_INFO("Bad \"initialize\" uCode load.\n");
21098c2ecf20Sopenharmony_ci		goto restart;
21108c2ecf20Sopenharmony_ci	}
21118c2ecf20Sopenharmony_ci
21128c2ecf20Sopenharmony_ci	/* Send pointers to protocol/runtime uCode image ... init code will
21138c2ecf20Sopenharmony_ci	 * load and launch runtime uCode, which will send us another "Alive"
21148c2ecf20Sopenharmony_ci	 * notification. */
21158c2ecf20Sopenharmony_ci	D_INFO("Initialization Alive received.\n");
21168c2ecf20Sopenharmony_ci	if (il3945_set_ucode_ptrs(il)) {
21178c2ecf20Sopenharmony_ci		/* Runtime instruction load won't happen;
21188c2ecf20Sopenharmony_ci		 * take it all the way back down so we can try again */
21198c2ecf20Sopenharmony_ci		D_INFO("Couldn't set up uCode pointers.\n");
21208c2ecf20Sopenharmony_ci		goto restart;
21218c2ecf20Sopenharmony_ci	}
21228c2ecf20Sopenharmony_ci	return;
21238c2ecf20Sopenharmony_ci
21248c2ecf20Sopenharmony_cirestart:
21258c2ecf20Sopenharmony_ci	queue_work(il->workqueue, &il->restart);
21268c2ecf20Sopenharmony_ci}
21278c2ecf20Sopenharmony_ci
21288c2ecf20Sopenharmony_ci/*
21298c2ecf20Sopenharmony_ci * il3945_alive_start - called after N_ALIVE notification received
21308c2ecf20Sopenharmony_ci *                   from protocol/runtime uCode (initialization uCode's
21318c2ecf20Sopenharmony_ci *                   Alive gets handled by il3945_init_alive_start()).
21328c2ecf20Sopenharmony_ci */
21338c2ecf20Sopenharmony_cistatic void
21348c2ecf20Sopenharmony_ciil3945_alive_start(struct il_priv *il)
21358c2ecf20Sopenharmony_ci{
21368c2ecf20Sopenharmony_ci	int thermal_spin = 0;
21378c2ecf20Sopenharmony_ci	u32 rfkill;
21388c2ecf20Sopenharmony_ci
21398c2ecf20Sopenharmony_ci	D_INFO("Runtime Alive received.\n");
21408c2ecf20Sopenharmony_ci
21418c2ecf20Sopenharmony_ci	if (il->card_alive.is_valid != UCODE_VALID_OK) {
21428c2ecf20Sopenharmony_ci		/* We had an error bringing up the hardware, so take it
21438c2ecf20Sopenharmony_ci		 * all the way back down so we can try again */
21448c2ecf20Sopenharmony_ci		D_INFO("Alive failed.\n");
21458c2ecf20Sopenharmony_ci		goto restart;
21468c2ecf20Sopenharmony_ci	}
21478c2ecf20Sopenharmony_ci
21488c2ecf20Sopenharmony_ci	/* Initialize uCode has loaded Runtime uCode ... verify inst image.
21498c2ecf20Sopenharmony_ci	 * This is a paranoid check, because we would not have gotten the
21508c2ecf20Sopenharmony_ci	 * "runtime" alive if code weren't properly loaded.  */
21518c2ecf20Sopenharmony_ci	if (il3945_verify_ucode(il)) {
21528c2ecf20Sopenharmony_ci		/* Runtime instruction load was bad;
21538c2ecf20Sopenharmony_ci		 * take it all the way back down so we can try again */
21548c2ecf20Sopenharmony_ci		D_INFO("Bad runtime uCode load.\n");
21558c2ecf20Sopenharmony_ci		goto restart;
21568c2ecf20Sopenharmony_ci	}
21578c2ecf20Sopenharmony_ci
21588c2ecf20Sopenharmony_ci	rfkill = il_rd_prph(il, APMG_RFKILL_REG);
21598c2ecf20Sopenharmony_ci	D_INFO("RFKILL status: 0x%x\n", rfkill);
21608c2ecf20Sopenharmony_ci
21618c2ecf20Sopenharmony_ci	if (rfkill & 0x1) {
21628c2ecf20Sopenharmony_ci		clear_bit(S_RFKILL, &il->status);
21638c2ecf20Sopenharmony_ci		/* if RFKILL is not on, then wait for thermal
21648c2ecf20Sopenharmony_ci		 * sensor in adapter to kick in */
21658c2ecf20Sopenharmony_ci		while (il3945_hw_get_temperature(il) == 0) {
21668c2ecf20Sopenharmony_ci			thermal_spin++;
21678c2ecf20Sopenharmony_ci			udelay(10);
21688c2ecf20Sopenharmony_ci		}
21698c2ecf20Sopenharmony_ci
21708c2ecf20Sopenharmony_ci		if (thermal_spin)
21718c2ecf20Sopenharmony_ci			D_INFO("Thermal calibration took %dus\n",
21728c2ecf20Sopenharmony_ci			       thermal_spin * 10);
21738c2ecf20Sopenharmony_ci	} else
21748c2ecf20Sopenharmony_ci		set_bit(S_RFKILL, &il->status);
21758c2ecf20Sopenharmony_ci
21768c2ecf20Sopenharmony_ci	/* After the ALIVE response, we can send commands to 3945 uCode */
21778c2ecf20Sopenharmony_ci	set_bit(S_ALIVE, &il->status);
21788c2ecf20Sopenharmony_ci
21798c2ecf20Sopenharmony_ci	/* Enable watchdog to monitor the driver tx queues */
21808c2ecf20Sopenharmony_ci	il_setup_watchdog(il);
21818c2ecf20Sopenharmony_ci
21828c2ecf20Sopenharmony_ci	if (il_is_rfkill(il))
21838c2ecf20Sopenharmony_ci		return;
21848c2ecf20Sopenharmony_ci
21858c2ecf20Sopenharmony_ci	ieee80211_wake_queues(il->hw);
21868c2ecf20Sopenharmony_ci
21878c2ecf20Sopenharmony_ci	il->active_rate = RATES_MASK_3945;
21888c2ecf20Sopenharmony_ci
21898c2ecf20Sopenharmony_ci	il_power_update_mode(il, true);
21908c2ecf20Sopenharmony_ci
21918c2ecf20Sopenharmony_ci	if (il_is_associated(il)) {
21928c2ecf20Sopenharmony_ci		struct il3945_rxon_cmd *active_rxon =
21938c2ecf20Sopenharmony_ci		    (struct il3945_rxon_cmd *)(&il->active);
21948c2ecf20Sopenharmony_ci
21958c2ecf20Sopenharmony_ci		il->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
21968c2ecf20Sopenharmony_ci		active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
21978c2ecf20Sopenharmony_ci	} else {
21988c2ecf20Sopenharmony_ci		/* Initialize our rx_config data */
21998c2ecf20Sopenharmony_ci		il_connection_init_rx_config(il);
22008c2ecf20Sopenharmony_ci	}
22018c2ecf20Sopenharmony_ci
22028c2ecf20Sopenharmony_ci	/* Configure Bluetooth device coexistence support */
22038c2ecf20Sopenharmony_ci	il_send_bt_config(il);
22048c2ecf20Sopenharmony_ci
22058c2ecf20Sopenharmony_ci	set_bit(S_READY, &il->status);
22068c2ecf20Sopenharmony_ci
22078c2ecf20Sopenharmony_ci	/* Configure the adapter for unassociated operation */
22088c2ecf20Sopenharmony_ci	il3945_commit_rxon(il);
22098c2ecf20Sopenharmony_ci
22108c2ecf20Sopenharmony_ci	il3945_reg_txpower_periodic(il);
22118c2ecf20Sopenharmony_ci
22128c2ecf20Sopenharmony_ci	D_INFO("ALIVE processing complete.\n");
22138c2ecf20Sopenharmony_ci	wake_up(&il->wait_command_queue);
22148c2ecf20Sopenharmony_ci
22158c2ecf20Sopenharmony_ci	return;
22168c2ecf20Sopenharmony_ci
22178c2ecf20Sopenharmony_cirestart:
22188c2ecf20Sopenharmony_ci	queue_work(il->workqueue, &il->restart);
22198c2ecf20Sopenharmony_ci}
22208c2ecf20Sopenharmony_ci
22218c2ecf20Sopenharmony_cistatic void il3945_cancel_deferred_work(struct il_priv *il);
22228c2ecf20Sopenharmony_ci
22238c2ecf20Sopenharmony_cistatic void
22248c2ecf20Sopenharmony_ci__il3945_down(struct il_priv *il)
22258c2ecf20Sopenharmony_ci{
22268c2ecf20Sopenharmony_ci	unsigned long flags;
22278c2ecf20Sopenharmony_ci	int exit_pending;
22288c2ecf20Sopenharmony_ci
22298c2ecf20Sopenharmony_ci	D_INFO(DRV_NAME " is going down\n");
22308c2ecf20Sopenharmony_ci
22318c2ecf20Sopenharmony_ci	il_scan_cancel_timeout(il, 200);
22328c2ecf20Sopenharmony_ci
22338c2ecf20Sopenharmony_ci	exit_pending = test_and_set_bit(S_EXIT_PENDING, &il->status);
22348c2ecf20Sopenharmony_ci
22358c2ecf20Sopenharmony_ci	/* Stop TX queues watchdog. We need to have S_EXIT_PENDING bit set
22368c2ecf20Sopenharmony_ci	 * to prevent rearm timer */
22378c2ecf20Sopenharmony_ci	del_timer_sync(&il->watchdog);
22388c2ecf20Sopenharmony_ci
22398c2ecf20Sopenharmony_ci	/* Station information will now be cleared in device */
22408c2ecf20Sopenharmony_ci	il_clear_ucode_stations(il);
22418c2ecf20Sopenharmony_ci	il_dealloc_bcast_stations(il);
22428c2ecf20Sopenharmony_ci	il_clear_driver_stations(il);
22438c2ecf20Sopenharmony_ci
22448c2ecf20Sopenharmony_ci	/* Unblock any waiting calls */
22458c2ecf20Sopenharmony_ci	wake_up_all(&il->wait_command_queue);
22468c2ecf20Sopenharmony_ci
22478c2ecf20Sopenharmony_ci	/* Wipe out the EXIT_PENDING status bit if we are not actually
22488c2ecf20Sopenharmony_ci	 * exiting the module */
22498c2ecf20Sopenharmony_ci	if (!exit_pending)
22508c2ecf20Sopenharmony_ci		clear_bit(S_EXIT_PENDING, &il->status);
22518c2ecf20Sopenharmony_ci
22528c2ecf20Sopenharmony_ci	/* stop and reset the on-board processor */
22538c2ecf20Sopenharmony_ci	_il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
22548c2ecf20Sopenharmony_ci
22558c2ecf20Sopenharmony_ci	/* tell the device to stop sending interrupts */
22568c2ecf20Sopenharmony_ci	spin_lock_irqsave(&il->lock, flags);
22578c2ecf20Sopenharmony_ci	il_disable_interrupts(il);
22588c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&il->lock, flags);
22598c2ecf20Sopenharmony_ci	il3945_synchronize_irq(il);
22608c2ecf20Sopenharmony_ci
22618c2ecf20Sopenharmony_ci	if (il->mac80211_registered)
22628c2ecf20Sopenharmony_ci		ieee80211_stop_queues(il->hw);
22638c2ecf20Sopenharmony_ci
22648c2ecf20Sopenharmony_ci	/* If we have not previously called il3945_init() then
22658c2ecf20Sopenharmony_ci	 * clear all bits but the RF Kill bits and return */
22668c2ecf20Sopenharmony_ci	if (!il_is_init(il)) {
22678c2ecf20Sopenharmony_ci		il->status =
22688c2ecf20Sopenharmony_ci		    test_bit(S_RFKILL, &il->status) << S_RFKILL |
22698c2ecf20Sopenharmony_ci		    test_bit(S_GEO_CONFIGURED, &il->status) << S_GEO_CONFIGURED |
22708c2ecf20Sopenharmony_ci		    test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
22718c2ecf20Sopenharmony_ci		goto exit;
22728c2ecf20Sopenharmony_ci	}
22738c2ecf20Sopenharmony_ci
22748c2ecf20Sopenharmony_ci	/* ...otherwise clear out all the status bits but the RF Kill
22758c2ecf20Sopenharmony_ci	 * bit and continue taking the NIC down. */
22768c2ecf20Sopenharmony_ci	il->status &=
22778c2ecf20Sopenharmony_ci	    test_bit(S_RFKILL, &il->status) << S_RFKILL |
22788c2ecf20Sopenharmony_ci	    test_bit(S_GEO_CONFIGURED, &il->status) << S_GEO_CONFIGURED |
22798c2ecf20Sopenharmony_ci	    test_bit(S_FW_ERROR, &il->status) << S_FW_ERROR |
22808c2ecf20Sopenharmony_ci	    test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
22818c2ecf20Sopenharmony_ci
22828c2ecf20Sopenharmony_ci	/*
22838c2ecf20Sopenharmony_ci	 * We disabled and synchronized interrupt, and priv->mutex is taken, so
22848c2ecf20Sopenharmony_ci	 * here is the only thread which will program device registers, but
22858c2ecf20Sopenharmony_ci	 * still have lockdep assertions, so we are taking reg_lock.
22868c2ecf20Sopenharmony_ci	 */
22878c2ecf20Sopenharmony_ci	spin_lock_irq(&il->reg_lock);
22888c2ecf20Sopenharmony_ci	/* FIXME: il_grab_nic_access if rfkill is off ? */
22898c2ecf20Sopenharmony_ci
22908c2ecf20Sopenharmony_ci	il3945_hw_txq_ctx_stop(il);
22918c2ecf20Sopenharmony_ci	il3945_hw_rxq_stop(il);
22928c2ecf20Sopenharmony_ci	/* Power-down device's busmaster DMA clocks */
22938c2ecf20Sopenharmony_ci	_il_wr_prph(il, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
22948c2ecf20Sopenharmony_ci	udelay(5);
22958c2ecf20Sopenharmony_ci	/* Stop the device, and put it in low power state */
22968c2ecf20Sopenharmony_ci	_il_apm_stop(il);
22978c2ecf20Sopenharmony_ci
22988c2ecf20Sopenharmony_ci	spin_unlock_irq(&il->reg_lock);
22998c2ecf20Sopenharmony_ci
23008c2ecf20Sopenharmony_ci	il3945_hw_txq_ctx_free(il);
23018c2ecf20Sopenharmony_ciexit:
23028c2ecf20Sopenharmony_ci	memset(&il->card_alive, 0, sizeof(struct il_alive_resp));
23038c2ecf20Sopenharmony_ci	dev_kfree_skb(il->beacon_skb);
23048c2ecf20Sopenharmony_ci	il->beacon_skb = NULL;
23058c2ecf20Sopenharmony_ci
23068c2ecf20Sopenharmony_ci	/* clear out any free frames */
23078c2ecf20Sopenharmony_ci	il3945_clear_free_frames(il);
23088c2ecf20Sopenharmony_ci}
23098c2ecf20Sopenharmony_ci
23108c2ecf20Sopenharmony_cistatic void
23118c2ecf20Sopenharmony_ciil3945_down(struct il_priv *il)
23128c2ecf20Sopenharmony_ci{
23138c2ecf20Sopenharmony_ci	mutex_lock(&il->mutex);
23148c2ecf20Sopenharmony_ci	__il3945_down(il);
23158c2ecf20Sopenharmony_ci	mutex_unlock(&il->mutex);
23168c2ecf20Sopenharmony_ci
23178c2ecf20Sopenharmony_ci	il3945_cancel_deferred_work(il);
23188c2ecf20Sopenharmony_ci}
23198c2ecf20Sopenharmony_ci
23208c2ecf20Sopenharmony_ci#define MAX_HW_RESTARTS 5
23218c2ecf20Sopenharmony_ci
23228c2ecf20Sopenharmony_cistatic int
23238c2ecf20Sopenharmony_ciil3945_alloc_bcast_station(struct il_priv *il)
23248c2ecf20Sopenharmony_ci{
23258c2ecf20Sopenharmony_ci	unsigned long flags;
23268c2ecf20Sopenharmony_ci	u8 sta_id;
23278c2ecf20Sopenharmony_ci
23288c2ecf20Sopenharmony_ci	spin_lock_irqsave(&il->sta_lock, flags);
23298c2ecf20Sopenharmony_ci	sta_id = il_prep_station(il, il_bcast_addr, false, NULL);
23308c2ecf20Sopenharmony_ci	if (sta_id == IL_INVALID_STATION) {
23318c2ecf20Sopenharmony_ci		IL_ERR("Unable to prepare broadcast station\n");
23328c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&il->sta_lock, flags);
23338c2ecf20Sopenharmony_ci
23348c2ecf20Sopenharmony_ci		return -EINVAL;
23358c2ecf20Sopenharmony_ci	}
23368c2ecf20Sopenharmony_ci
23378c2ecf20Sopenharmony_ci	il->stations[sta_id].used |= IL_STA_DRIVER_ACTIVE;
23388c2ecf20Sopenharmony_ci	il->stations[sta_id].used |= IL_STA_BCAST;
23398c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&il->sta_lock, flags);
23408c2ecf20Sopenharmony_ci
23418c2ecf20Sopenharmony_ci	return 0;
23428c2ecf20Sopenharmony_ci}
23438c2ecf20Sopenharmony_ci
23448c2ecf20Sopenharmony_cistatic int
23458c2ecf20Sopenharmony_ci__il3945_up(struct il_priv *il)
23468c2ecf20Sopenharmony_ci{
23478c2ecf20Sopenharmony_ci	int rc, i;
23488c2ecf20Sopenharmony_ci
23498c2ecf20Sopenharmony_ci	rc = il3945_alloc_bcast_station(il);
23508c2ecf20Sopenharmony_ci	if (rc)
23518c2ecf20Sopenharmony_ci		return rc;
23528c2ecf20Sopenharmony_ci
23538c2ecf20Sopenharmony_ci	if (test_bit(S_EXIT_PENDING, &il->status)) {
23548c2ecf20Sopenharmony_ci		IL_WARN("Exit pending; will not bring the NIC up\n");
23558c2ecf20Sopenharmony_ci		return -EIO;
23568c2ecf20Sopenharmony_ci	}
23578c2ecf20Sopenharmony_ci
23588c2ecf20Sopenharmony_ci	if (!il->ucode_data_backup.v_addr || !il->ucode_data.v_addr) {
23598c2ecf20Sopenharmony_ci		IL_ERR("ucode not available for device bring up\n");
23608c2ecf20Sopenharmony_ci		return -EIO;
23618c2ecf20Sopenharmony_ci	}
23628c2ecf20Sopenharmony_ci
23638c2ecf20Sopenharmony_ci	/* If platform's RF_KILL switch is NOT set to KILL */
23648c2ecf20Sopenharmony_ci	if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
23658c2ecf20Sopenharmony_ci		clear_bit(S_RFKILL, &il->status);
23668c2ecf20Sopenharmony_ci	else {
23678c2ecf20Sopenharmony_ci		set_bit(S_RFKILL, &il->status);
23688c2ecf20Sopenharmony_ci		return -ERFKILL;
23698c2ecf20Sopenharmony_ci	}
23708c2ecf20Sopenharmony_ci
23718c2ecf20Sopenharmony_ci	_il_wr(il, CSR_INT, 0xFFFFFFFF);
23728c2ecf20Sopenharmony_ci
23738c2ecf20Sopenharmony_ci	rc = il3945_hw_nic_init(il);
23748c2ecf20Sopenharmony_ci	if (rc) {
23758c2ecf20Sopenharmony_ci		IL_ERR("Unable to int nic\n");
23768c2ecf20Sopenharmony_ci		return rc;
23778c2ecf20Sopenharmony_ci	}
23788c2ecf20Sopenharmony_ci
23798c2ecf20Sopenharmony_ci	/* make sure rfkill handshake bits are cleared */
23808c2ecf20Sopenharmony_ci	_il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
23818c2ecf20Sopenharmony_ci	_il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
23828c2ecf20Sopenharmony_ci
23838c2ecf20Sopenharmony_ci	/* clear (again), then enable host interrupts */
23848c2ecf20Sopenharmony_ci	_il_wr(il, CSR_INT, 0xFFFFFFFF);
23858c2ecf20Sopenharmony_ci	il_enable_interrupts(il);
23868c2ecf20Sopenharmony_ci
23878c2ecf20Sopenharmony_ci	/* really make sure rfkill handshake bits are cleared */
23888c2ecf20Sopenharmony_ci	_il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
23898c2ecf20Sopenharmony_ci	_il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
23908c2ecf20Sopenharmony_ci
23918c2ecf20Sopenharmony_ci	/* Copy original ucode data image from disk into backup cache.
23928c2ecf20Sopenharmony_ci	 * This will be used to initialize the on-board processor's
23938c2ecf20Sopenharmony_ci	 * data SRAM for a clean start when the runtime program first loads. */
23948c2ecf20Sopenharmony_ci	memcpy(il->ucode_data_backup.v_addr, il->ucode_data.v_addr,
23958c2ecf20Sopenharmony_ci	       il->ucode_data.len);
23968c2ecf20Sopenharmony_ci
23978c2ecf20Sopenharmony_ci	/* We return success when we resume from suspend and rf_kill is on. */
23988c2ecf20Sopenharmony_ci	if (test_bit(S_RFKILL, &il->status))
23998c2ecf20Sopenharmony_ci		return 0;
24008c2ecf20Sopenharmony_ci
24018c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_HW_RESTARTS; i++) {
24028c2ecf20Sopenharmony_ci
24038c2ecf20Sopenharmony_ci		/* load bootstrap state machine,
24048c2ecf20Sopenharmony_ci		 * load bootstrap program into processor's memory,
24058c2ecf20Sopenharmony_ci		 * prepare to load the "initialize" uCode */
24068c2ecf20Sopenharmony_ci		rc = il->ops->load_ucode(il);
24078c2ecf20Sopenharmony_ci
24088c2ecf20Sopenharmony_ci		if (rc) {
24098c2ecf20Sopenharmony_ci			IL_ERR("Unable to set up bootstrap uCode: %d\n", rc);
24108c2ecf20Sopenharmony_ci			continue;
24118c2ecf20Sopenharmony_ci		}
24128c2ecf20Sopenharmony_ci
24138c2ecf20Sopenharmony_ci		/* start card; "initialize" will load runtime ucode */
24148c2ecf20Sopenharmony_ci		il3945_nic_start(il);
24158c2ecf20Sopenharmony_ci
24168c2ecf20Sopenharmony_ci		D_INFO(DRV_NAME " is coming up\n");
24178c2ecf20Sopenharmony_ci
24188c2ecf20Sopenharmony_ci		return 0;
24198c2ecf20Sopenharmony_ci	}
24208c2ecf20Sopenharmony_ci
24218c2ecf20Sopenharmony_ci	set_bit(S_EXIT_PENDING, &il->status);
24228c2ecf20Sopenharmony_ci	__il3945_down(il);
24238c2ecf20Sopenharmony_ci	clear_bit(S_EXIT_PENDING, &il->status);
24248c2ecf20Sopenharmony_ci
24258c2ecf20Sopenharmony_ci	/* tried to restart and config the device for as long as our
24268c2ecf20Sopenharmony_ci	 * patience could withstand */
24278c2ecf20Sopenharmony_ci	IL_ERR("Unable to initialize device after %d attempts.\n", i);
24288c2ecf20Sopenharmony_ci	return -EIO;
24298c2ecf20Sopenharmony_ci}
24308c2ecf20Sopenharmony_ci
24318c2ecf20Sopenharmony_ci/*****************************************************************************
24328c2ecf20Sopenharmony_ci *
24338c2ecf20Sopenharmony_ci * Workqueue callbacks
24348c2ecf20Sopenharmony_ci *
24358c2ecf20Sopenharmony_ci *****************************************************************************/
24368c2ecf20Sopenharmony_ci
24378c2ecf20Sopenharmony_cistatic void
24388c2ecf20Sopenharmony_ciil3945_bg_init_alive_start(struct work_struct *data)
24398c2ecf20Sopenharmony_ci{
24408c2ecf20Sopenharmony_ci	struct il_priv *il =
24418c2ecf20Sopenharmony_ci	    container_of(data, struct il_priv, init_alive_start.work);
24428c2ecf20Sopenharmony_ci
24438c2ecf20Sopenharmony_ci	mutex_lock(&il->mutex);
24448c2ecf20Sopenharmony_ci	if (test_bit(S_EXIT_PENDING, &il->status))
24458c2ecf20Sopenharmony_ci		goto out;
24468c2ecf20Sopenharmony_ci
24478c2ecf20Sopenharmony_ci	il3945_init_alive_start(il);
24488c2ecf20Sopenharmony_ciout:
24498c2ecf20Sopenharmony_ci	mutex_unlock(&il->mutex);
24508c2ecf20Sopenharmony_ci}
24518c2ecf20Sopenharmony_ci
24528c2ecf20Sopenharmony_cistatic void
24538c2ecf20Sopenharmony_ciil3945_bg_alive_start(struct work_struct *data)
24548c2ecf20Sopenharmony_ci{
24558c2ecf20Sopenharmony_ci	struct il_priv *il =
24568c2ecf20Sopenharmony_ci	    container_of(data, struct il_priv, alive_start.work);
24578c2ecf20Sopenharmony_ci
24588c2ecf20Sopenharmony_ci	mutex_lock(&il->mutex);
24598c2ecf20Sopenharmony_ci	if (test_bit(S_EXIT_PENDING, &il->status) || il->txq == NULL)
24608c2ecf20Sopenharmony_ci		goto out;
24618c2ecf20Sopenharmony_ci
24628c2ecf20Sopenharmony_ci	il3945_alive_start(il);
24638c2ecf20Sopenharmony_ciout:
24648c2ecf20Sopenharmony_ci	mutex_unlock(&il->mutex);
24658c2ecf20Sopenharmony_ci}
24668c2ecf20Sopenharmony_ci
24678c2ecf20Sopenharmony_ci/*
24688c2ecf20Sopenharmony_ci * 3945 cannot interrupt driver when hardware rf kill switch toggles;
24698c2ecf20Sopenharmony_ci * driver must poll CSR_GP_CNTRL_REG register for change.  This register
24708c2ecf20Sopenharmony_ci * *is* readable even when device has been SW_RESET into low power mode
24718c2ecf20Sopenharmony_ci * (e.g. during RF KILL).
24728c2ecf20Sopenharmony_ci */
24738c2ecf20Sopenharmony_cistatic void
24748c2ecf20Sopenharmony_ciil3945_rfkill_poll(struct work_struct *data)
24758c2ecf20Sopenharmony_ci{
24768c2ecf20Sopenharmony_ci	struct il_priv *il =
24778c2ecf20Sopenharmony_ci	    container_of(data, struct il_priv, _3945.rfkill_poll.work);
24788c2ecf20Sopenharmony_ci	bool old_rfkill = test_bit(S_RFKILL, &il->status);
24798c2ecf20Sopenharmony_ci	bool new_rfkill =
24808c2ecf20Sopenharmony_ci	    !(_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
24818c2ecf20Sopenharmony_ci
24828c2ecf20Sopenharmony_ci	if (new_rfkill != old_rfkill) {
24838c2ecf20Sopenharmony_ci		if (new_rfkill)
24848c2ecf20Sopenharmony_ci			set_bit(S_RFKILL, &il->status);
24858c2ecf20Sopenharmony_ci		else
24868c2ecf20Sopenharmony_ci			clear_bit(S_RFKILL, &il->status);
24878c2ecf20Sopenharmony_ci
24888c2ecf20Sopenharmony_ci		wiphy_rfkill_set_hw_state(il->hw->wiphy, new_rfkill);
24898c2ecf20Sopenharmony_ci
24908c2ecf20Sopenharmony_ci		D_RF_KILL("RF_KILL bit toggled to %s.\n",
24918c2ecf20Sopenharmony_ci			  new_rfkill ? "disable radio" : "enable radio");
24928c2ecf20Sopenharmony_ci	}
24938c2ecf20Sopenharmony_ci
24948c2ecf20Sopenharmony_ci	/* Keep this running, even if radio now enabled.  This will be
24958c2ecf20Sopenharmony_ci	 * cancelled in mac_start() if system decides to start again */
24968c2ecf20Sopenharmony_ci	queue_delayed_work(il->workqueue, &il->_3945.rfkill_poll,
24978c2ecf20Sopenharmony_ci			   round_jiffies_relative(2 * HZ));
24988c2ecf20Sopenharmony_ci
24998c2ecf20Sopenharmony_ci}
25008c2ecf20Sopenharmony_ci
25018c2ecf20Sopenharmony_ciint
25028c2ecf20Sopenharmony_ciil3945_request_scan(struct il_priv *il, struct ieee80211_vif *vif)
25038c2ecf20Sopenharmony_ci{
25048c2ecf20Sopenharmony_ci	struct il_host_cmd cmd = {
25058c2ecf20Sopenharmony_ci		.id = C_SCAN,
25068c2ecf20Sopenharmony_ci		.len = sizeof(struct il3945_scan_cmd),
25078c2ecf20Sopenharmony_ci		.flags = CMD_SIZE_HUGE,
25088c2ecf20Sopenharmony_ci	};
25098c2ecf20Sopenharmony_ci	struct il3945_scan_cmd *scan;
25108c2ecf20Sopenharmony_ci	u8 n_probes = 0;
25118c2ecf20Sopenharmony_ci	enum nl80211_band band;
25128c2ecf20Sopenharmony_ci	bool is_active = false;
25138c2ecf20Sopenharmony_ci	int ret;
25148c2ecf20Sopenharmony_ci	u16 len;
25158c2ecf20Sopenharmony_ci
25168c2ecf20Sopenharmony_ci	lockdep_assert_held(&il->mutex);
25178c2ecf20Sopenharmony_ci
25188c2ecf20Sopenharmony_ci	if (!il->scan_cmd) {
25198c2ecf20Sopenharmony_ci		il->scan_cmd =
25208c2ecf20Sopenharmony_ci		    kmalloc(sizeof(struct il3945_scan_cmd) + IL_MAX_SCAN_SIZE,
25218c2ecf20Sopenharmony_ci			    GFP_KERNEL);
25228c2ecf20Sopenharmony_ci		if (!il->scan_cmd) {
25238c2ecf20Sopenharmony_ci			D_SCAN("Fail to allocate scan memory\n");
25248c2ecf20Sopenharmony_ci			return -ENOMEM;
25258c2ecf20Sopenharmony_ci		}
25268c2ecf20Sopenharmony_ci	}
25278c2ecf20Sopenharmony_ci	scan = il->scan_cmd;
25288c2ecf20Sopenharmony_ci	memset(scan, 0, sizeof(struct il3945_scan_cmd) + IL_MAX_SCAN_SIZE);
25298c2ecf20Sopenharmony_ci
25308c2ecf20Sopenharmony_ci	scan->quiet_plcp_th = IL_PLCP_QUIET_THRESH;
25318c2ecf20Sopenharmony_ci	scan->quiet_time = IL_ACTIVE_QUIET_TIME;
25328c2ecf20Sopenharmony_ci
25338c2ecf20Sopenharmony_ci	if (il_is_associated(il)) {
25348c2ecf20Sopenharmony_ci		u16 interval;
25358c2ecf20Sopenharmony_ci		u32 extra;
25368c2ecf20Sopenharmony_ci		u32 suspend_time = 100;
25378c2ecf20Sopenharmony_ci		u32 scan_suspend_time = 100;
25388c2ecf20Sopenharmony_ci
25398c2ecf20Sopenharmony_ci		D_INFO("Scanning while associated...\n");
25408c2ecf20Sopenharmony_ci
25418c2ecf20Sopenharmony_ci		interval = vif->bss_conf.beacon_int;
25428c2ecf20Sopenharmony_ci
25438c2ecf20Sopenharmony_ci		scan->suspend_time = 0;
25448c2ecf20Sopenharmony_ci		scan->max_out_time = cpu_to_le32(200 * 1024);
25458c2ecf20Sopenharmony_ci		if (!interval)
25468c2ecf20Sopenharmony_ci			interval = suspend_time;
25478c2ecf20Sopenharmony_ci		/*
25488c2ecf20Sopenharmony_ci		 * suspend time format:
25498c2ecf20Sopenharmony_ci		 *  0-19: beacon interval in usec (time before exec.)
25508c2ecf20Sopenharmony_ci		 * 20-23: 0
25518c2ecf20Sopenharmony_ci		 * 24-31: number of beacons (suspend between channels)
25528c2ecf20Sopenharmony_ci		 */
25538c2ecf20Sopenharmony_ci
25548c2ecf20Sopenharmony_ci		extra = (suspend_time / interval) << 24;
25558c2ecf20Sopenharmony_ci		scan_suspend_time =
25568c2ecf20Sopenharmony_ci		    0xFF0FFFFF & (extra | ((suspend_time % interval) * 1024));
25578c2ecf20Sopenharmony_ci
25588c2ecf20Sopenharmony_ci		scan->suspend_time = cpu_to_le32(scan_suspend_time);
25598c2ecf20Sopenharmony_ci		D_SCAN("suspend_time 0x%X beacon interval %d\n",
25608c2ecf20Sopenharmony_ci		       scan_suspend_time, interval);
25618c2ecf20Sopenharmony_ci	}
25628c2ecf20Sopenharmony_ci
25638c2ecf20Sopenharmony_ci	if (il->scan_request->n_ssids) {
25648c2ecf20Sopenharmony_ci		int i, p = 0;
25658c2ecf20Sopenharmony_ci		D_SCAN("Kicking off active scan\n");
25668c2ecf20Sopenharmony_ci		for (i = 0; i < il->scan_request->n_ssids; i++) {
25678c2ecf20Sopenharmony_ci			/* always does wildcard anyway */
25688c2ecf20Sopenharmony_ci			if (!il->scan_request->ssids[i].ssid_len)
25698c2ecf20Sopenharmony_ci				continue;
25708c2ecf20Sopenharmony_ci			scan->direct_scan[p].id = WLAN_EID_SSID;
25718c2ecf20Sopenharmony_ci			scan->direct_scan[p].len =
25728c2ecf20Sopenharmony_ci			    il->scan_request->ssids[i].ssid_len;
25738c2ecf20Sopenharmony_ci			memcpy(scan->direct_scan[p].ssid,
25748c2ecf20Sopenharmony_ci			       il->scan_request->ssids[i].ssid,
25758c2ecf20Sopenharmony_ci			       il->scan_request->ssids[i].ssid_len);
25768c2ecf20Sopenharmony_ci			n_probes++;
25778c2ecf20Sopenharmony_ci			p++;
25788c2ecf20Sopenharmony_ci		}
25798c2ecf20Sopenharmony_ci		is_active = true;
25808c2ecf20Sopenharmony_ci	} else
25818c2ecf20Sopenharmony_ci		D_SCAN("Kicking off passive scan.\n");
25828c2ecf20Sopenharmony_ci
25838c2ecf20Sopenharmony_ci	/* We don't build a direct scan probe request; the uCode will do
25848c2ecf20Sopenharmony_ci	 * that based on the direct_mask added to each channel entry */
25858c2ecf20Sopenharmony_ci	scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
25868c2ecf20Sopenharmony_ci	scan->tx_cmd.sta_id = il->hw_params.bcast_id;
25878c2ecf20Sopenharmony_ci	scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
25888c2ecf20Sopenharmony_ci
25898c2ecf20Sopenharmony_ci	/* flags + rate selection */
25908c2ecf20Sopenharmony_ci
25918c2ecf20Sopenharmony_ci	switch (il->scan_band) {
25928c2ecf20Sopenharmony_ci	case NL80211_BAND_2GHZ:
25938c2ecf20Sopenharmony_ci		scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
25948c2ecf20Sopenharmony_ci		scan->tx_cmd.rate = RATE_1M_PLCP;
25958c2ecf20Sopenharmony_ci		band = NL80211_BAND_2GHZ;
25968c2ecf20Sopenharmony_ci		break;
25978c2ecf20Sopenharmony_ci	case NL80211_BAND_5GHZ:
25988c2ecf20Sopenharmony_ci		scan->tx_cmd.rate = RATE_6M_PLCP;
25998c2ecf20Sopenharmony_ci		band = NL80211_BAND_5GHZ;
26008c2ecf20Sopenharmony_ci		break;
26018c2ecf20Sopenharmony_ci	default:
26028c2ecf20Sopenharmony_ci		IL_WARN("Invalid scan band\n");
26038c2ecf20Sopenharmony_ci		return -EIO;
26048c2ecf20Sopenharmony_ci	}
26058c2ecf20Sopenharmony_ci
26068c2ecf20Sopenharmony_ci	/*
26078c2ecf20Sopenharmony_ci	 * If active scaning is requested but a certain channel is marked
26088c2ecf20Sopenharmony_ci	 * passive, we can do active scanning if we detect transmissions. For
26098c2ecf20Sopenharmony_ci	 * passive only scanning disable switching to active on any channel.
26108c2ecf20Sopenharmony_ci	 */
26118c2ecf20Sopenharmony_ci	scan->good_CRC_th =
26128c2ecf20Sopenharmony_ci	    is_active ? IL_GOOD_CRC_TH_DEFAULT : IL_GOOD_CRC_TH_NEVER;
26138c2ecf20Sopenharmony_ci
26148c2ecf20Sopenharmony_ci	len =
26158c2ecf20Sopenharmony_ci	    il_fill_probe_req(il, (struct ieee80211_mgmt *)scan->data,
26168c2ecf20Sopenharmony_ci			      vif->addr, il->scan_request->ie,
26178c2ecf20Sopenharmony_ci			      il->scan_request->ie_len,
26188c2ecf20Sopenharmony_ci			      IL_MAX_SCAN_SIZE - sizeof(*scan));
26198c2ecf20Sopenharmony_ci	scan->tx_cmd.len = cpu_to_le16(len);
26208c2ecf20Sopenharmony_ci
26218c2ecf20Sopenharmony_ci	/* select Rx antennas */
26228c2ecf20Sopenharmony_ci	scan->flags |= il3945_get_antenna_flags(il);
26238c2ecf20Sopenharmony_ci
26248c2ecf20Sopenharmony_ci	scan->channel_count =
26258c2ecf20Sopenharmony_ci	    il3945_get_channels_for_scan(il, band, is_active, n_probes,
26268c2ecf20Sopenharmony_ci					 (void *)&scan->data[len], vif);
26278c2ecf20Sopenharmony_ci	if (scan->channel_count == 0) {
26288c2ecf20Sopenharmony_ci		D_SCAN("channel count %d\n", scan->channel_count);
26298c2ecf20Sopenharmony_ci		return -EIO;
26308c2ecf20Sopenharmony_ci	}
26318c2ecf20Sopenharmony_ci
26328c2ecf20Sopenharmony_ci	cmd.len +=
26338c2ecf20Sopenharmony_ci	    le16_to_cpu(scan->tx_cmd.len) +
26348c2ecf20Sopenharmony_ci	    scan->channel_count * sizeof(struct il3945_scan_channel);
26358c2ecf20Sopenharmony_ci	cmd.data = scan;
26368c2ecf20Sopenharmony_ci	scan->len = cpu_to_le16(cmd.len);
26378c2ecf20Sopenharmony_ci
26388c2ecf20Sopenharmony_ci	set_bit(S_SCAN_HW, &il->status);
26398c2ecf20Sopenharmony_ci	ret = il_send_cmd_sync(il, &cmd);
26408c2ecf20Sopenharmony_ci	if (ret)
26418c2ecf20Sopenharmony_ci		clear_bit(S_SCAN_HW, &il->status);
26428c2ecf20Sopenharmony_ci	return ret;
26438c2ecf20Sopenharmony_ci}
26448c2ecf20Sopenharmony_ci
26458c2ecf20Sopenharmony_civoid
26468c2ecf20Sopenharmony_ciil3945_post_scan(struct il_priv *il)
26478c2ecf20Sopenharmony_ci{
26488c2ecf20Sopenharmony_ci	/*
26498c2ecf20Sopenharmony_ci	 * Since setting the RXON may have been deferred while
26508c2ecf20Sopenharmony_ci	 * performing the scan, fire one off if needed
26518c2ecf20Sopenharmony_ci	 */
26528c2ecf20Sopenharmony_ci	if (memcmp(&il->staging, &il->active, sizeof(il->staging)))
26538c2ecf20Sopenharmony_ci		il3945_commit_rxon(il);
26548c2ecf20Sopenharmony_ci}
26558c2ecf20Sopenharmony_ci
26568c2ecf20Sopenharmony_cistatic void
26578c2ecf20Sopenharmony_ciil3945_bg_restart(struct work_struct *data)
26588c2ecf20Sopenharmony_ci{
26598c2ecf20Sopenharmony_ci	struct il_priv *il = container_of(data, struct il_priv, restart);
26608c2ecf20Sopenharmony_ci
26618c2ecf20Sopenharmony_ci	if (test_bit(S_EXIT_PENDING, &il->status))
26628c2ecf20Sopenharmony_ci		return;
26638c2ecf20Sopenharmony_ci
26648c2ecf20Sopenharmony_ci	if (test_and_clear_bit(S_FW_ERROR, &il->status)) {
26658c2ecf20Sopenharmony_ci		mutex_lock(&il->mutex);
26668c2ecf20Sopenharmony_ci		il->is_open = 0;
26678c2ecf20Sopenharmony_ci		mutex_unlock(&il->mutex);
26688c2ecf20Sopenharmony_ci		il3945_down(il);
26698c2ecf20Sopenharmony_ci		ieee80211_restart_hw(il->hw);
26708c2ecf20Sopenharmony_ci	} else {
26718c2ecf20Sopenharmony_ci		il3945_down(il);
26728c2ecf20Sopenharmony_ci
26738c2ecf20Sopenharmony_ci		mutex_lock(&il->mutex);
26748c2ecf20Sopenharmony_ci		if (test_bit(S_EXIT_PENDING, &il->status)) {
26758c2ecf20Sopenharmony_ci			mutex_unlock(&il->mutex);
26768c2ecf20Sopenharmony_ci			return;
26778c2ecf20Sopenharmony_ci		}
26788c2ecf20Sopenharmony_ci
26798c2ecf20Sopenharmony_ci		__il3945_up(il);
26808c2ecf20Sopenharmony_ci		mutex_unlock(&il->mutex);
26818c2ecf20Sopenharmony_ci	}
26828c2ecf20Sopenharmony_ci}
26838c2ecf20Sopenharmony_ci
26848c2ecf20Sopenharmony_cistatic void
26858c2ecf20Sopenharmony_ciil3945_bg_rx_replenish(struct work_struct *data)
26868c2ecf20Sopenharmony_ci{
26878c2ecf20Sopenharmony_ci	struct il_priv *il = container_of(data, struct il_priv, rx_replenish);
26888c2ecf20Sopenharmony_ci
26898c2ecf20Sopenharmony_ci	mutex_lock(&il->mutex);
26908c2ecf20Sopenharmony_ci	if (test_bit(S_EXIT_PENDING, &il->status))
26918c2ecf20Sopenharmony_ci		goto out;
26928c2ecf20Sopenharmony_ci
26938c2ecf20Sopenharmony_ci	il3945_rx_replenish(il);
26948c2ecf20Sopenharmony_ciout:
26958c2ecf20Sopenharmony_ci	mutex_unlock(&il->mutex);
26968c2ecf20Sopenharmony_ci}
26978c2ecf20Sopenharmony_ci
26988c2ecf20Sopenharmony_civoid
26998c2ecf20Sopenharmony_ciil3945_post_associate(struct il_priv *il)
27008c2ecf20Sopenharmony_ci{
27018c2ecf20Sopenharmony_ci	int rc = 0;
27028c2ecf20Sopenharmony_ci
27038c2ecf20Sopenharmony_ci	if (!il->vif || !il->is_open)
27048c2ecf20Sopenharmony_ci		return;
27058c2ecf20Sopenharmony_ci
27068c2ecf20Sopenharmony_ci	D_ASSOC("Associated as %d to: %pM\n", il->vif->bss_conf.aid,
27078c2ecf20Sopenharmony_ci		il->active.bssid_addr);
27088c2ecf20Sopenharmony_ci
27098c2ecf20Sopenharmony_ci	if (test_bit(S_EXIT_PENDING, &il->status))
27108c2ecf20Sopenharmony_ci		return;
27118c2ecf20Sopenharmony_ci
27128c2ecf20Sopenharmony_ci	il_scan_cancel_timeout(il, 200);
27138c2ecf20Sopenharmony_ci
27148c2ecf20Sopenharmony_ci	il->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
27158c2ecf20Sopenharmony_ci	il3945_commit_rxon(il);
27168c2ecf20Sopenharmony_ci
27178c2ecf20Sopenharmony_ci	rc = il_send_rxon_timing(il);
27188c2ecf20Sopenharmony_ci	if (rc)
27198c2ecf20Sopenharmony_ci		IL_WARN("C_RXON_TIMING failed - " "Attempting to continue.\n");
27208c2ecf20Sopenharmony_ci
27218c2ecf20Sopenharmony_ci	il->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
27228c2ecf20Sopenharmony_ci
27238c2ecf20Sopenharmony_ci	il->staging.assoc_id = cpu_to_le16(il->vif->bss_conf.aid);
27248c2ecf20Sopenharmony_ci
27258c2ecf20Sopenharmony_ci	D_ASSOC("assoc id %d beacon interval %d\n", il->vif->bss_conf.aid,
27268c2ecf20Sopenharmony_ci		il->vif->bss_conf.beacon_int);
27278c2ecf20Sopenharmony_ci
27288c2ecf20Sopenharmony_ci	if (il->vif->bss_conf.use_short_preamble)
27298c2ecf20Sopenharmony_ci		il->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
27308c2ecf20Sopenharmony_ci	else
27318c2ecf20Sopenharmony_ci		il->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
27328c2ecf20Sopenharmony_ci
27338c2ecf20Sopenharmony_ci	if (il->staging.flags & RXON_FLG_BAND_24G_MSK) {
27348c2ecf20Sopenharmony_ci		if (il->vif->bss_conf.use_short_slot)
27358c2ecf20Sopenharmony_ci			il->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
27368c2ecf20Sopenharmony_ci		else
27378c2ecf20Sopenharmony_ci			il->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
27388c2ecf20Sopenharmony_ci	}
27398c2ecf20Sopenharmony_ci
27408c2ecf20Sopenharmony_ci	il3945_commit_rxon(il);
27418c2ecf20Sopenharmony_ci
27428c2ecf20Sopenharmony_ci	switch (il->vif->type) {
27438c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_STATION:
27448c2ecf20Sopenharmony_ci		il3945_rate_scale_init(il->hw, IL_AP_ID);
27458c2ecf20Sopenharmony_ci		break;
27468c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_ADHOC:
27478c2ecf20Sopenharmony_ci		il3945_send_beacon_cmd(il);
27488c2ecf20Sopenharmony_ci		break;
27498c2ecf20Sopenharmony_ci	default:
27508c2ecf20Sopenharmony_ci		IL_ERR("%s Should not be called in %d mode\n", __func__,
27518c2ecf20Sopenharmony_ci		      il->vif->type);
27528c2ecf20Sopenharmony_ci		break;
27538c2ecf20Sopenharmony_ci	}
27548c2ecf20Sopenharmony_ci}
27558c2ecf20Sopenharmony_ci
27568c2ecf20Sopenharmony_ci/*****************************************************************************
27578c2ecf20Sopenharmony_ci *
27588c2ecf20Sopenharmony_ci * mac80211 entry point functions
27598c2ecf20Sopenharmony_ci *
27608c2ecf20Sopenharmony_ci *****************************************************************************/
27618c2ecf20Sopenharmony_ci
27628c2ecf20Sopenharmony_ci#define UCODE_READY_TIMEOUT	(2 * HZ)
27638c2ecf20Sopenharmony_ci
27648c2ecf20Sopenharmony_cistatic int
27658c2ecf20Sopenharmony_ciil3945_mac_start(struct ieee80211_hw *hw)
27668c2ecf20Sopenharmony_ci{
27678c2ecf20Sopenharmony_ci	struct il_priv *il = hw->priv;
27688c2ecf20Sopenharmony_ci	int ret;
27698c2ecf20Sopenharmony_ci
27708c2ecf20Sopenharmony_ci	/* we should be verifying the device is ready to be opened */
27718c2ecf20Sopenharmony_ci	mutex_lock(&il->mutex);
27728c2ecf20Sopenharmony_ci	D_MAC80211("enter\n");
27738c2ecf20Sopenharmony_ci
27748c2ecf20Sopenharmony_ci	/* fetch ucode file from disk, alloc and copy to bus-master buffers ...
27758c2ecf20Sopenharmony_ci	 * ucode filename and max sizes are card-specific. */
27768c2ecf20Sopenharmony_ci
27778c2ecf20Sopenharmony_ci	if (!il->ucode_code.len) {
27788c2ecf20Sopenharmony_ci		ret = il3945_read_ucode(il);
27798c2ecf20Sopenharmony_ci		if (ret) {
27808c2ecf20Sopenharmony_ci			IL_ERR("Could not read microcode: %d\n", ret);
27818c2ecf20Sopenharmony_ci			mutex_unlock(&il->mutex);
27828c2ecf20Sopenharmony_ci			goto out_release_irq;
27838c2ecf20Sopenharmony_ci		}
27848c2ecf20Sopenharmony_ci	}
27858c2ecf20Sopenharmony_ci
27868c2ecf20Sopenharmony_ci	ret = __il3945_up(il);
27878c2ecf20Sopenharmony_ci
27888c2ecf20Sopenharmony_ci	mutex_unlock(&il->mutex);
27898c2ecf20Sopenharmony_ci
27908c2ecf20Sopenharmony_ci	if (ret)
27918c2ecf20Sopenharmony_ci		goto out_release_irq;
27928c2ecf20Sopenharmony_ci
27938c2ecf20Sopenharmony_ci	D_INFO("Start UP work.\n");
27948c2ecf20Sopenharmony_ci
27958c2ecf20Sopenharmony_ci	/* Wait for START_ALIVE from ucode. Otherwise callbacks from
27968c2ecf20Sopenharmony_ci	 * mac80211 will not be run successfully. */
27978c2ecf20Sopenharmony_ci	ret = wait_event_timeout(il->wait_command_queue,
27988c2ecf20Sopenharmony_ci				 test_bit(S_READY, &il->status),
27998c2ecf20Sopenharmony_ci				 UCODE_READY_TIMEOUT);
28008c2ecf20Sopenharmony_ci	if (!ret) {
28018c2ecf20Sopenharmony_ci		if (!test_bit(S_READY, &il->status)) {
28028c2ecf20Sopenharmony_ci			IL_ERR("Wait for START_ALIVE timeout after %dms.\n",
28038c2ecf20Sopenharmony_ci			       jiffies_to_msecs(UCODE_READY_TIMEOUT));
28048c2ecf20Sopenharmony_ci			ret = -ETIMEDOUT;
28058c2ecf20Sopenharmony_ci			goto out_release_irq;
28068c2ecf20Sopenharmony_ci		}
28078c2ecf20Sopenharmony_ci	}
28088c2ecf20Sopenharmony_ci
28098c2ecf20Sopenharmony_ci	/* ucode is running and will send rfkill notifications,
28108c2ecf20Sopenharmony_ci	 * no need to poll the killswitch state anymore */
28118c2ecf20Sopenharmony_ci	cancel_delayed_work(&il->_3945.rfkill_poll);
28128c2ecf20Sopenharmony_ci
28138c2ecf20Sopenharmony_ci	il->is_open = 1;
28148c2ecf20Sopenharmony_ci	D_MAC80211("leave\n");
28158c2ecf20Sopenharmony_ci	return 0;
28168c2ecf20Sopenharmony_ci
28178c2ecf20Sopenharmony_ciout_release_irq:
28188c2ecf20Sopenharmony_ci	il->is_open = 0;
28198c2ecf20Sopenharmony_ci	D_MAC80211("leave - failed\n");
28208c2ecf20Sopenharmony_ci	return ret;
28218c2ecf20Sopenharmony_ci}
28228c2ecf20Sopenharmony_ci
28238c2ecf20Sopenharmony_cistatic void
28248c2ecf20Sopenharmony_ciil3945_mac_stop(struct ieee80211_hw *hw)
28258c2ecf20Sopenharmony_ci{
28268c2ecf20Sopenharmony_ci	struct il_priv *il = hw->priv;
28278c2ecf20Sopenharmony_ci
28288c2ecf20Sopenharmony_ci	D_MAC80211("enter\n");
28298c2ecf20Sopenharmony_ci
28308c2ecf20Sopenharmony_ci	if (!il->is_open) {
28318c2ecf20Sopenharmony_ci		D_MAC80211("leave - skip\n");
28328c2ecf20Sopenharmony_ci		return;
28338c2ecf20Sopenharmony_ci	}
28348c2ecf20Sopenharmony_ci
28358c2ecf20Sopenharmony_ci	il->is_open = 0;
28368c2ecf20Sopenharmony_ci
28378c2ecf20Sopenharmony_ci	il3945_down(il);
28388c2ecf20Sopenharmony_ci
28398c2ecf20Sopenharmony_ci	flush_workqueue(il->workqueue);
28408c2ecf20Sopenharmony_ci
28418c2ecf20Sopenharmony_ci	/* start polling the killswitch state again */
28428c2ecf20Sopenharmony_ci	queue_delayed_work(il->workqueue, &il->_3945.rfkill_poll,
28438c2ecf20Sopenharmony_ci			   round_jiffies_relative(2 * HZ));
28448c2ecf20Sopenharmony_ci
28458c2ecf20Sopenharmony_ci	D_MAC80211("leave\n");
28468c2ecf20Sopenharmony_ci}
28478c2ecf20Sopenharmony_ci
28488c2ecf20Sopenharmony_cistatic void
28498c2ecf20Sopenharmony_ciil3945_mac_tx(struct ieee80211_hw *hw,
28508c2ecf20Sopenharmony_ci	       struct ieee80211_tx_control *control,
28518c2ecf20Sopenharmony_ci	       struct sk_buff *skb)
28528c2ecf20Sopenharmony_ci{
28538c2ecf20Sopenharmony_ci	struct il_priv *il = hw->priv;
28548c2ecf20Sopenharmony_ci
28558c2ecf20Sopenharmony_ci	D_MAC80211("enter\n");
28568c2ecf20Sopenharmony_ci
28578c2ecf20Sopenharmony_ci	D_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
28588c2ecf20Sopenharmony_ci	     ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
28598c2ecf20Sopenharmony_ci
28608c2ecf20Sopenharmony_ci	if (il3945_tx_skb(il, control->sta, skb))
28618c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
28628c2ecf20Sopenharmony_ci
28638c2ecf20Sopenharmony_ci	D_MAC80211("leave\n");
28648c2ecf20Sopenharmony_ci}
28658c2ecf20Sopenharmony_ci
28668c2ecf20Sopenharmony_civoid
28678c2ecf20Sopenharmony_ciil3945_config_ap(struct il_priv *il)
28688c2ecf20Sopenharmony_ci{
28698c2ecf20Sopenharmony_ci	struct ieee80211_vif *vif = il->vif;
28708c2ecf20Sopenharmony_ci	int rc = 0;
28718c2ecf20Sopenharmony_ci
28728c2ecf20Sopenharmony_ci	if (test_bit(S_EXIT_PENDING, &il->status))
28738c2ecf20Sopenharmony_ci		return;
28748c2ecf20Sopenharmony_ci
28758c2ecf20Sopenharmony_ci	/* The following should be done only at AP bring up */
28768c2ecf20Sopenharmony_ci	if (!(il_is_associated(il))) {
28778c2ecf20Sopenharmony_ci
28788c2ecf20Sopenharmony_ci		/* RXON - unassoc (to set timing command) */
28798c2ecf20Sopenharmony_ci		il->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
28808c2ecf20Sopenharmony_ci		il3945_commit_rxon(il);
28818c2ecf20Sopenharmony_ci
28828c2ecf20Sopenharmony_ci		/* RXON Timing */
28838c2ecf20Sopenharmony_ci		rc = il_send_rxon_timing(il);
28848c2ecf20Sopenharmony_ci		if (rc)
28858c2ecf20Sopenharmony_ci			IL_WARN("C_RXON_TIMING failed - "
28868c2ecf20Sopenharmony_ci				"Attempting to continue.\n");
28878c2ecf20Sopenharmony_ci
28888c2ecf20Sopenharmony_ci		il->staging.assoc_id = 0;
28898c2ecf20Sopenharmony_ci
28908c2ecf20Sopenharmony_ci		if (vif->bss_conf.use_short_preamble)
28918c2ecf20Sopenharmony_ci			il->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
28928c2ecf20Sopenharmony_ci		else
28938c2ecf20Sopenharmony_ci			il->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
28948c2ecf20Sopenharmony_ci
28958c2ecf20Sopenharmony_ci		if (il->staging.flags & RXON_FLG_BAND_24G_MSK) {
28968c2ecf20Sopenharmony_ci			if (vif->bss_conf.use_short_slot)
28978c2ecf20Sopenharmony_ci				il->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
28988c2ecf20Sopenharmony_ci			else
28998c2ecf20Sopenharmony_ci				il->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
29008c2ecf20Sopenharmony_ci		}
29018c2ecf20Sopenharmony_ci		/* restore RXON assoc */
29028c2ecf20Sopenharmony_ci		il->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
29038c2ecf20Sopenharmony_ci		il3945_commit_rxon(il);
29048c2ecf20Sopenharmony_ci	}
29058c2ecf20Sopenharmony_ci	il3945_send_beacon_cmd(il);
29068c2ecf20Sopenharmony_ci}
29078c2ecf20Sopenharmony_ci
29088c2ecf20Sopenharmony_cistatic int
29098c2ecf20Sopenharmony_ciil3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
29108c2ecf20Sopenharmony_ci		   struct ieee80211_vif *vif, struct ieee80211_sta *sta,
29118c2ecf20Sopenharmony_ci		   struct ieee80211_key_conf *key)
29128c2ecf20Sopenharmony_ci{
29138c2ecf20Sopenharmony_ci	struct il_priv *il = hw->priv;
29148c2ecf20Sopenharmony_ci	int ret = 0;
29158c2ecf20Sopenharmony_ci	u8 sta_id = IL_INVALID_STATION;
29168c2ecf20Sopenharmony_ci	u8 static_key;
29178c2ecf20Sopenharmony_ci
29188c2ecf20Sopenharmony_ci	D_MAC80211("enter\n");
29198c2ecf20Sopenharmony_ci
29208c2ecf20Sopenharmony_ci	if (il3945_mod_params.sw_crypto) {
29218c2ecf20Sopenharmony_ci		D_MAC80211("leave - hwcrypto disabled\n");
29228c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
29238c2ecf20Sopenharmony_ci	}
29248c2ecf20Sopenharmony_ci
29258c2ecf20Sopenharmony_ci	/*
29268c2ecf20Sopenharmony_ci	 * To support IBSS RSN, don't program group keys in IBSS, the
29278c2ecf20Sopenharmony_ci	 * hardware will then not attempt to decrypt the frames.
29288c2ecf20Sopenharmony_ci	 */
29298c2ecf20Sopenharmony_ci	if (vif->type == NL80211_IFTYPE_ADHOC &&
29308c2ecf20Sopenharmony_ci	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
29318c2ecf20Sopenharmony_ci		D_MAC80211("leave - IBSS RSN\n");
29328c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
29338c2ecf20Sopenharmony_ci	}
29348c2ecf20Sopenharmony_ci
29358c2ecf20Sopenharmony_ci	static_key = !il_is_associated(il);
29368c2ecf20Sopenharmony_ci
29378c2ecf20Sopenharmony_ci	if (!static_key) {
29388c2ecf20Sopenharmony_ci		sta_id = il_sta_id_or_broadcast(il, sta);
29398c2ecf20Sopenharmony_ci		if (sta_id == IL_INVALID_STATION) {
29408c2ecf20Sopenharmony_ci			D_MAC80211("leave - station not found\n");
29418c2ecf20Sopenharmony_ci			return -EINVAL;
29428c2ecf20Sopenharmony_ci		}
29438c2ecf20Sopenharmony_ci	}
29448c2ecf20Sopenharmony_ci
29458c2ecf20Sopenharmony_ci	mutex_lock(&il->mutex);
29468c2ecf20Sopenharmony_ci	il_scan_cancel_timeout(il, 100);
29478c2ecf20Sopenharmony_ci
29488c2ecf20Sopenharmony_ci	switch (cmd) {
29498c2ecf20Sopenharmony_ci	case SET_KEY:
29508c2ecf20Sopenharmony_ci		if (static_key)
29518c2ecf20Sopenharmony_ci			ret = il3945_set_static_key(il, key);
29528c2ecf20Sopenharmony_ci		else
29538c2ecf20Sopenharmony_ci			ret = il3945_set_dynamic_key(il, key, sta_id);
29548c2ecf20Sopenharmony_ci		D_MAC80211("enable hwcrypto key\n");
29558c2ecf20Sopenharmony_ci		break;
29568c2ecf20Sopenharmony_ci	case DISABLE_KEY:
29578c2ecf20Sopenharmony_ci		if (static_key)
29588c2ecf20Sopenharmony_ci			ret = il3945_remove_static_key(il);
29598c2ecf20Sopenharmony_ci		else
29608c2ecf20Sopenharmony_ci			ret = il3945_clear_sta_key_info(il, sta_id);
29618c2ecf20Sopenharmony_ci		D_MAC80211("disable hwcrypto key\n");
29628c2ecf20Sopenharmony_ci		break;
29638c2ecf20Sopenharmony_ci	default:
29648c2ecf20Sopenharmony_ci		ret = -EINVAL;
29658c2ecf20Sopenharmony_ci	}
29668c2ecf20Sopenharmony_ci
29678c2ecf20Sopenharmony_ci	D_MAC80211("leave ret %d\n", ret);
29688c2ecf20Sopenharmony_ci	mutex_unlock(&il->mutex);
29698c2ecf20Sopenharmony_ci
29708c2ecf20Sopenharmony_ci	return ret;
29718c2ecf20Sopenharmony_ci}
29728c2ecf20Sopenharmony_ci
29738c2ecf20Sopenharmony_cistatic int
29748c2ecf20Sopenharmony_ciil3945_mac_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
29758c2ecf20Sopenharmony_ci		   struct ieee80211_sta *sta)
29768c2ecf20Sopenharmony_ci{
29778c2ecf20Sopenharmony_ci	struct il_priv *il = hw->priv;
29788c2ecf20Sopenharmony_ci	struct il3945_sta_priv *sta_priv = (void *)sta->drv_priv;
29798c2ecf20Sopenharmony_ci	int ret;
29808c2ecf20Sopenharmony_ci	bool is_ap = vif->type == NL80211_IFTYPE_STATION;
29818c2ecf20Sopenharmony_ci	u8 sta_id;
29828c2ecf20Sopenharmony_ci
29838c2ecf20Sopenharmony_ci	mutex_lock(&il->mutex);
29848c2ecf20Sopenharmony_ci	D_INFO("station %pM\n", sta->addr);
29858c2ecf20Sopenharmony_ci	sta_priv->common.sta_id = IL_INVALID_STATION;
29868c2ecf20Sopenharmony_ci
29878c2ecf20Sopenharmony_ci	ret = il_add_station_common(il, sta->addr, is_ap, sta, &sta_id);
29888c2ecf20Sopenharmony_ci	if (ret) {
29898c2ecf20Sopenharmony_ci		IL_ERR("Unable to add station %pM (%d)\n", sta->addr, ret);
29908c2ecf20Sopenharmony_ci		/* Should we return success if return code is EEXIST ? */
29918c2ecf20Sopenharmony_ci		mutex_unlock(&il->mutex);
29928c2ecf20Sopenharmony_ci		return ret;
29938c2ecf20Sopenharmony_ci	}
29948c2ecf20Sopenharmony_ci
29958c2ecf20Sopenharmony_ci	sta_priv->common.sta_id = sta_id;
29968c2ecf20Sopenharmony_ci
29978c2ecf20Sopenharmony_ci	/* Initialize rate scaling */
29988c2ecf20Sopenharmony_ci	D_INFO("Initializing rate scaling for station %pM\n", sta->addr);
29998c2ecf20Sopenharmony_ci	il3945_rs_rate_init(il, sta, sta_id);
30008c2ecf20Sopenharmony_ci	mutex_unlock(&il->mutex);
30018c2ecf20Sopenharmony_ci
30028c2ecf20Sopenharmony_ci	return 0;
30038c2ecf20Sopenharmony_ci}
30048c2ecf20Sopenharmony_ci
30058c2ecf20Sopenharmony_cistatic void
30068c2ecf20Sopenharmony_ciil3945_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
30078c2ecf20Sopenharmony_ci			unsigned int *total_flags, u64 multicast)
30088c2ecf20Sopenharmony_ci{
30098c2ecf20Sopenharmony_ci	struct il_priv *il = hw->priv;
30108c2ecf20Sopenharmony_ci	__le32 filter_or = 0, filter_nand = 0;
30118c2ecf20Sopenharmony_ci
30128c2ecf20Sopenharmony_ci#define CHK(test, flag)	do { \
30138c2ecf20Sopenharmony_ci	if (*total_flags & (test))		\
30148c2ecf20Sopenharmony_ci		filter_or |= (flag);		\
30158c2ecf20Sopenharmony_ci	else					\
30168c2ecf20Sopenharmony_ci		filter_nand |= (flag);		\
30178c2ecf20Sopenharmony_ci	} while (0)
30188c2ecf20Sopenharmony_ci
30198c2ecf20Sopenharmony_ci	D_MAC80211("Enter: changed: 0x%x, total: 0x%x\n", changed_flags,
30208c2ecf20Sopenharmony_ci		   *total_flags);
30218c2ecf20Sopenharmony_ci
30228c2ecf20Sopenharmony_ci	CHK(FIF_OTHER_BSS, RXON_FILTER_PROMISC_MSK);
30238c2ecf20Sopenharmony_ci	CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK);
30248c2ecf20Sopenharmony_ci	CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
30258c2ecf20Sopenharmony_ci
30268c2ecf20Sopenharmony_ci#undef CHK
30278c2ecf20Sopenharmony_ci
30288c2ecf20Sopenharmony_ci	mutex_lock(&il->mutex);
30298c2ecf20Sopenharmony_ci
30308c2ecf20Sopenharmony_ci	il->staging.filter_flags &= ~filter_nand;
30318c2ecf20Sopenharmony_ci	il->staging.filter_flags |= filter_or;
30328c2ecf20Sopenharmony_ci
30338c2ecf20Sopenharmony_ci	/*
30348c2ecf20Sopenharmony_ci	 * Not committing directly because hardware can perform a scan,
30358c2ecf20Sopenharmony_ci	 * but even if hw is ready, committing here breaks for some reason,
30368c2ecf20Sopenharmony_ci	 * we'll eventually commit the filter flags change anyway.
30378c2ecf20Sopenharmony_ci	 */
30388c2ecf20Sopenharmony_ci
30398c2ecf20Sopenharmony_ci	mutex_unlock(&il->mutex);
30408c2ecf20Sopenharmony_ci
30418c2ecf20Sopenharmony_ci	/*
30428c2ecf20Sopenharmony_ci	 * Receiving all multicast frames is always enabled by the
30438c2ecf20Sopenharmony_ci	 * default flags setup in il_connection_init_rx_config()
30448c2ecf20Sopenharmony_ci	 * since we currently do not support programming multicast
30458c2ecf20Sopenharmony_ci	 * filters into the device.
30468c2ecf20Sopenharmony_ci	 */
30478c2ecf20Sopenharmony_ci	*total_flags &=
30488c2ecf20Sopenharmony_ci	    FIF_OTHER_BSS | FIF_ALLMULTI |
30498c2ecf20Sopenharmony_ci	    FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
30508c2ecf20Sopenharmony_ci}
30518c2ecf20Sopenharmony_ci
30528c2ecf20Sopenharmony_ci/*****************************************************************************
30538c2ecf20Sopenharmony_ci *
30548c2ecf20Sopenharmony_ci * sysfs attributes
30558c2ecf20Sopenharmony_ci *
30568c2ecf20Sopenharmony_ci *****************************************************************************/
30578c2ecf20Sopenharmony_ci
30588c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLEGACY_DEBUG
30598c2ecf20Sopenharmony_ci
30608c2ecf20Sopenharmony_ci/*
30618c2ecf20Sopenharmony_ci * The following adds a new attribute to the sysfs representation
30628c2ecf20Sopenharmony_ci * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
30638c2ecf20Sopenharmony_ci * used for controlling the debug level.
30648c2ecf20Sopenharmony_ci *
30658c2ecf20Sopenharmony_ci * See the level definitions in iwl for details.
30668c2ecf20Sopenharmony_ci *
30678c2ecf20Sopenharmony_ci * The debug_level being managed using sysfs below is a per device debug
30688c2ecf20Sopenharmony_ci * level that is used instead of the global debug level if it (the per
30698c2ecf20Sopenharmony_ci * device debug level) is set.
30708c2ecf20Sopenharmony_ci */
30718c2ecf20Sopenharmony_cistatic ssize_t
30728c2ecf20Sopenharmony_ciil3945_show_debug_level(struct device *d, struct device_attribute *attr,
30738c2ecf20Sopenharmony_ci			char *buf)
30748c2ecf20Sopenharmony_ci{
30758c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
30768c2ecf20Sopenharmony_ci	return sprintf(buf, "0x%08X\n", il_get_debug_level(il));
30778c2ecf20Sopenharmony_ci}
30788c2ecf20Sopenharmony_ci
30798c2ecf20Sopenharmony_cistatic ssize_t
30808c2ecf20Sopenharmony_ciil3945_store_debug_level(struct device *d, struct device_attribute *attr,
30818c2ecf20Sopenharmony_ci			 const char *buf, size_t count)
30828c2ecf20Sopenharmony_ci{
30838c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
30848c2ecf20Sopenharmony_ci	unsigned long val;
30858c2ecf20Sopenharmony_ci	int ret;
30868c2ecf20Sopenharmony_ci
30878c2ecf20Sopenharmony_ci	ret = kstrtoul(buf, 0, &val);
30888c2ecf20Sopenharmony_ci	if (ret)
30898c2ecf20Sopenharmony_ci		IL_INFO("%s is not in hex or decimal form.\n", buf);
30908c2ecf20Sopenharmony_ci	else
30918c2ecf20Sopenharmony_ci		il->debug_level = val;
30928c2ecf20Sopenharmony_ci
30938c2ecf20Sopenharmony_ci	return strnlen(buf, count);
30948c2ecf20Sopenharmony_ci}
30958c2ecf20Sopenharmony_ci
30968c2ecf20Sopenharmony_cistatic DEVICE_ATTR(debug_level, 0644, il3945_show_debug_level,
30978c2ecf20Sopenharmony_ci		   il3945_store_debug_level);
30988c2ecf20Sopenharmony_ci
30998c2ecf20Sopenharmony_ci#endif /* CONFIG_IWLEGACY_DEBUG */
31008c2ecf20Sopenharmony_ci
31018c2ecf20Sopenharmony_cistatic ssize_t
31028c2ecf20Sopenharmony_ciil3945_show_temperature(struct device *d, struct device_attribute *attr,
31038c2ecf20Sopenharmony_ci			char *buf)
31048c2ecf20Sopenharmony_ci{
31058c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
31068c2ecf20Sopenharmony_ci
31078c2ecf20Sopenharmony_ci	if (!il_is_alive(il))
31088c2ecf20Sopenharmony_ci		return -EAGAIN;
31098c2ecf20Sopenharmony_ci
31108c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", il3945_hw_get_temperature(il));
31118c2ecf20Sopenharmony_ci}
31128c2ecf20Sopenharmony_ci
31138c2ecf20Sopenharmony_cistatic DEVICE_ATTR(temperature, 0444, il3945_show_temperature, NULL);
31148c2ecf20Sopenharmony_ci
31158c2ecf20Sopenharmony_cistatic ssize_t
31168c2ecf20Sopenharmony_ciil3945_show_tx_power(struct device *d, struct device_attribute *attr, char *buf)
31178c2ecf20Sopenharmony_ci{
31188c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
31198c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", il->tx_power_user_lmt);
31208c2ecf20Sopenharmony_ci}
31218c2ecf20Sopenharmony_ci
31228c2ecf20Sopenharmony_cistatic ssize_t
31238c2ecf20Sopenharmony_ciil3945_store_tx_power(struct device *d, struct device_attribute *attr,
31248c2ecf20Sopenharmony_ci		      const char *buf, size_t count)
31258c2ecf20Sopenharmony_ci{
31268c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
31278c2ecf20Sopenharmony_ci	char *p = (char *)buf;
31288c2ecf20Sopenharmony_ci	u32 val;
31298c2ecf20Sopenharmony_ci
31308c2ecf20Sopenharmony_ci	val = simple_strtoul(p, &p, 10);
31318c2ecf20Sopenharmony_ci	if (p == buf)
31328c2ecf20Sopenharmony_ci		IL_INFO(": %s is not in decimal form.\n", buf);
31338c2ecf20Sopenharmony_ci	else
31348c2ecf20Sopenharmony_ci		il3945_hw_reg_set_txpower(il, val);
31358c2ecf20Sopenharmony_ci
31368c2ecf20Sopenharmony_ci	return count;
31378c2ecf20Sopenharmony_ci}
31388c2ecf20Sopenharmony_ci
31398c2ecf20Sopenharmony_cistatic DEVICE_ATTR(tx_power, 0644, il3945_show_tx_power, il3945_store_tx_power);
31408c2ecf20Sopenharmony_ci
31418c2ecf20Sopenharmony_cistatic ssize_t
31428c2ecf20Sopenharmony_ciil3945_show_flags(struct device *d, struct device_attribute *attr, char *buf)
31438c2ecf20Sopenharmony_ci{
31448c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
31458c2ecf20Sopenharmony_ci
31468c2ecf20Sopenharmony_ci	return sprintf(buf, "0x%04X\n", il->active.flags);
31478c2ecf20Sopenharmony_ci}
31488c2ecf20Sopenharmony_ci
31498c2ecf20Sopenharmony_cistatic ssize_t
31508c2ecf20Sopenharmony_ciil3945_store_flags(struct device *d, struct device_attribute *attr,
31518c2ecf20Sopenharmony_ci		   const char *buf, size_t count)
31528c2ecf20Sopenharmony_ci{
31538c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
31548c2ecf20Sopenharmony_ci	u32 flags = simple_strtoul(buf, NULL, 0);
31558c2ecf20Sopenharmony_ci
31568c2ecf20Sopenharmony_ci	mutex_lock(&il->mutex);
31578c2ecf20Sopenharmony_ci	if (le32_to_cpu(il->staging.flags) != flags) {
31588c2ecf20Sopenharmony_ci		/* Cancel any currently running scans... */
31598c2ecf20Sopenharmony_ci		if (il_scan_cancel_timeout(il, 100))
31608c2ecf20Sopenharmony_ci			IL_WARN("Could not cancel scan.\n");
31618c2ecf20Sopenharmony_ci		else {
31628c2ecf20Sopenharmony_ci			D_INFO("Committing rxon.flags = 0x%04X\n", flags);
31638c2ecf20Sopenharmony_ci			il->staging.flags = cpu_to_le32(flags);
31648c2ecf20Sopenharmony_ci			il3945_commit_rxon(il);
31658c2ecf20Sopenharmony_ci		}
31668c2ecf20Sopenharmony_ci	}
31678c2ecf20Sopenharmony_ci	mutex_unlock(&il->mutex);
31688c2ecf20Sopenharmony_ci
31698c2ecf20Sopenharmony_ci	return count;
31708c2ecf20Sopenharmony_ci}
31718c2ecf20Sopenharmony_ci
31728c2ecf20Sopenharmony_cistatic DEVICE_ATTR(flags, 0644, il3945_show_flags, il3945_store_flags);
31738c2ecf20Sopenharmony_ci
31748c2ecf20Sopenharmony_cistatic ssize_t
31758c2ecf20Sopenharmony_ciil3945_show_filter_flags(struct device *d, struct device_attribute *attr,
31768c2ecf20Sopenharmony_ci			 char *buf)
31778c2ecf20Sopenharmony_ci{
31788c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
31798c2ecf20Sopenharmony_ci
31808c2ecf20Sopenharmony_ci	return sprintf(buf, "0x%04X\n", le32_to_cpu(il->active.filter_flags));
31818c2ecf20Sopenharmony_ci}
31828c2ecf20Sopenharmony_ci
31838c2ecf20Sopenharmony_cistatic ssize_t
31848c2ecf20Sopenharmony_ciil3945_store_filter_flags(struct device *d, struct device_attribute *attr,
31858c2ecf20Sopenharmony_ci			  const char *buf, size_t count)
31868c2ecf20Sopenharmony_ci{
31878c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
31888c2ecf20Sopenharmony_ci	u32 filter_flags = simple_strtoul(buf, NULL, 0);
31898c2ecf20Sopenharmony_ci
31908c2ecf20Sopenharmony_ci	mutex_lock(&il->mutex);
31918c2ecf20Sopenharmony_ci	if (le32_to_cpu(il->staging.filter_flags) != filter_flags) {
31928c2ecf20Sopenharmony_ci		/* Cancel any currently running scans... */
31938c2ecf20Sopenharmony_ci		if (il_scan_cancel_timeout(il, 100))
31948c2ecf20Sopenharmony_ci			IL_WARN("Could not cancel scan.\n");
31958c2ecf20Sopenharmony_ci		else {
31968c2ecf20Sopenharmony_ci			D_INFO("Committing rxon.filter_flags = " "0x%04X\n",
31978c2ecf20Sopenharmony_ci			       filter_flags);
31988c2ecf20Sopenharmony_ci			il->staging.filter_flags = cpu_to_le32(filter_flags);
31998c2ecf20Sopenharmony_ci			il3945_commit_rxon(il);
32008c2ecf20Sopenharmony_ci		}
32018c2ecf20Sopenharmony_ci	}
32028c2ecf20Sopenharmony_ci	mutex_unlock(&il->mutex);
32038c2ecf20Sopenharmony_ci
32048c2ecf20Sopenharmony_ci	return count;
32058c2ecf20Sopenharmony_ci}
32068c2ecf20Sopenharmony_ci
32078c2ecf20Sopenharmony_cistatic DEVICE_ATTR(filter_flags, 0644, il3945_show_filter_flags,
32088c2ecf20Sopenharmony_ci		   il3945_store_filter_flags);
32098c2ecf20Sopenharmony_ci
32108c2ecf20Sopenharmony_cistatic ssize_t
32118c2ecf20Sopenharmony_ciil3945_show_measurement(struct device *d, struct device_attribute *attr,
32128c2ecf20Sopenharmony_ci			char *buf)
32138c2ecf20Sopenharmony_ci{
32148c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
32158c2ecf20Sopenharmony_ci	struct il_spectrum_notification measure_report;
32168c2ecf20Sopenharmony_ci	u32 size = sizeof(measure_report), len = 0, ofs = 0;
32178c2ecf20Sopenharmony_ci	u8 *data = (u8 *) &measure_report;
32188c2ecf20Sopenharmony_ci	unsigned long flags;
32198c2ecf20Sopenharmony_ci
32208c2ecf20Sopenharmony_ci	spin_lock_irqsave(&il->lock, flags);
32218c2ecf20Sopenharmony_ci	if (!(il->measurement_status & MEASUREMENT_READY)) {
32228c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&il->lock, flags);
32238c2ecf20Sopenharmony_ci		return 0;
32248c2ecf20Sopenharmony_ci	}
32258c2ecf20Sopenharmony_ci	memcpy(&measure_report, &il->measure_report, size);
32268c2ecf20Sopenharmony_ci	il->measurement_status = 0;
32278c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&il->lock, flags);
32288c2ecf20Sopenharmony_ci
32298c2ecf20Sopenharmony_ci	while (size && PAGE_SIZE - len) {
32308c2ecf20Sopenharmony_ci		hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
32318c2ecf20Sopenharmony_ci				   PAGE_SIZE - len, true);
32328c2ecf20Sopenharmony_ci		len = strlen(buf);
32338c2ecf20Sopenharmony_ci		if (PAGE_SIZE - len)
32348c2ecf20Sopenharmony_ci			buf[len++] = '\n';
32358c2ecf20Sopenharmony_ci
32368c2ecf20Sopenharmony_ci		ofs += 16;
32378c2ecf20Sopenharmony_ci		size -= min(size, 16U);
32388c2ecf20Sopenharmony_ci	}
32398c2ecf20Sopenharmony_ci
32408c2ecf20Sopenharmony_ci	return len;
32418c2ecf20Sopenharmony_ci}
32428c2ecf20Sopenharmony_ci
32438c2ecf20Sopenharmony_cistatic ssize_t
32448c2ecf20Sopenharmony_ciil3945_store_measurement(struct device *d, struct device_attribute *attr,
32458c2ecf20Sopenharmony_ci			 const char *buf, size_t count)
32468c2ecf20Sopenharmony_ci{
32478c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
32488c2ecf20Sopenharmony_ci	struct ieee80211_measurement_params params = {
32498c2ecf20Sopenharmony_ci		.channel = le16_to_cpu(il->active.channel),
32508c2ecf20Sopenharmony_ci		.start_time = cpu_to_le64(il->_3945.last_tsf),
32518c2ecf20Sopenharmony_ci		.duration = cpu_to_le16(1),
32528c2ecf20Sopenharmony_ci	};
32538c2ecf20Sopenharmony_ci	u8 type = IL_MEASURE_BASIC;
32548c2ecf20Sopenharmony_ci	u8 buffer[32];
32558c2ecf20Sopenharmony_ci	u8 channel;
32568c2ecf20Sopenharmony_ci
32578c2ecf20Sopenharmony_ci	if (count) {
32588c2ecf20Sopenharmony_ci		char *p = buffer;
32598c2ecf20Sopenharmony_ci		strlcpy(buffer, buf, sizeof(buffer));
32608c2ecf20Sopenharmony_ci		channel = simple_strtoul(p, NULL, 0);
32618c2ecf20Sopenharmony_ci		if (channel)
32628c2ecf20Sopenharmony_ci			params.channel = channel;
32638c2ecf20Sopenharmony_ci
32648c2ecf20Sopenharmony_ci		p = buffer;
32658c2ecf20Sopenharmony_ci		while (*p && *p != ' ')
32668c2ecf20Sopenharmony_ci			p++;
32678c2ecf20Sopenharmony_ci		if (*p)
32688c2ecf20Sopenharmony_ci			type = simple_strtoul(p + 1, NULL, 0);
32698c2ecf20Sopenharmony_ci	}
32708c2ecf20Sopenharmony_ci
32718c2ecf20Sopenharmony_ci	D_INFO("Invoking measurement of type %d on " "channel %d (for '%s')\n",
32728c2ecf20Sopenharmony_ci	       type, params.channel, buf);
32738c2ecf20Sopenharmony_ci	il3945_get_measurement(il, &params, type);
32748c2ecf20Sopenharmony_ci
32758c2ecf20Sopenharmony_ci	return count;
32768c2ecf20Sopenharmony_ci}
32778c2ecf20Sopenharmony_ci
32788c2ecf20Sopenharmony_cistatic DEVICE_ATTR(measurement, 0600, il3945_show_measurement,
32798c2ecf20Sopenharmony_ci		   il3945_store_measurement);
32808c2ecf20Sopenharmony_ci
32818c2ecf20Sopenharmony_cistatic ssize_t
32828c2ecf20Sopenharmony_ciil3945_store_retry_rate(struct device *d, struct device_attribute *attr,
32838c2ecf20Sopenharmony_ci			const char *buf, size_t count)
32848c2ecf20Sopenharmony_ci{
32858c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
32868c2ecf20Sopenharmony_ci
32878c2ecf20Sopenharmony_ci	il->retry_rate = simple_strtoul(buf, NULL, 0);
32888c2ecf20Sopenharmony_ci	if (il->retry_rate <= 0)
32898c2ecf20Sopenharmony_ci		il->retry_rate = 1;
32908c2ecf20Sopenharmony_ci
32918c2ecf20Sopenharmony_ci	return count;
32928c2ecf20Sopenharmony_ci}
32938c2ecf20Sopenharmony_ci
32948c2ecf20Sopenharmony_cistatic ssize_t
32958c2ecf20Sopenharmony_ciil3945_show_retry_rate(struct device *d, struct device_attribute *attr,
32968c2ecf20Sopenharmony_ci		       char *buf)
32978c2ecf20Sopenharmony_ci{
32988c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
32998c2ecf20Sopenharmony_ci	return sprintf(buf, "%d", il->retry_rate);
33008c2ecf20Sopenharmony_ci}
33018c2ecf20Sopenharmony_ci
33028c2ecf20Sopenharmony_cistatic DEVICE_ATTR(retry_rate, 0600, il3945_show_retry_rate,
33038c2ecf20Sopenharmony_ci		   il3945_store_retry_rate);
33048c2ecf20Sopenharmony_ci
33058c2ecf20Sopenharmony_cistatic ssize_t
33068c2ecf20Sopenharmony_ciil3945_show_channels(struct device *d, struct device_attribute *attr, char *buf)
33078c2ecf20Sopenharmony_ci{
33088c2ecf20Sopenharmony_ci	/* all this shit doesn't belong into sysfs anyway */
33098c2ecf20Sopenharmony_ci	return 0;
33108c2ecf20Sopenharmony_ci}
33118c2ecf20Sopenharmony_ci
33128c2ecf20Sopenharmony_cistatic DEVICE_ATTR(channels, 0400, il3945_show_channels, NULL);
33138c2ecf20Sopenharmony_ci
33148c2ecf20Sopenharmony_cistatic ssize_t
33158c2ecf20Sopenharmony_ciil3945_show_antenna(struct device *d, struct device_attribute *attr, char *buf)
33168c2ecf20Sopenharmony_ci{
33178c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
33188c2ecf20Sopenharmony_ci
33198c2ecf20Sopenharmony_ci	if (!il_is_alive(il))
33208c2ecf20Sopenharmony_ci		return -EAGAIN;
33218c2ecf20Sopenharmony_ci
33228c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", il3945_mod_params.antenna);
33238c2ecf20Sopenharmony_ci}
33248c2ecf20Sopenharmony_ci
33258c2ecf20Sopenharmony_cistatic ssize_t
33268c2ecf20Sopenharmony_ciil3945_store_antenna(struct device *d, struct device_attribute *attr,
33278c2ecf20Sopenharmony_ci		     const char *buf, size_t count)
33288c2ecf20Sopenharmony_ci{
33298c2ecf20Sopenharmony_ci	struct il_priv *il __maybe_unused = dev_get_drvdata(d);
33308c2ecf20Sopenharmony_ci	int ant;
33318c2ecf20Sopenharmony_ci
33328c2ecf20Sopenharmony_ci	if (count == 0)
33338c2ecf20Sopenharmony_ci		return 0;
33348c2ecf20Sopenharmony_ci
33358c2ecf20Sopenharmony_ci	if (sscanf(buf, "%1i", &ant) != 1) {
33368c2ecf20Sopenharmony_ci		D_INFO("not in hex or decimal form.\n");
33378c2ecf20Sopenharmony_ci		return count;
33388c2ecf20Sopenharmony_ci	}
33398c2ecf20Sopenharmony_ci
33408c2ecf20Sopenharmony_ci	if (ant >= 0 && ant <= 2) {
33418c2ecf20Sopenharmony_ci		D_INFO("Setting antenna select to %d.\n", ant);
33428c2ecf20Sopenharmony_ci		il3945_mod_params.antenna = (enum il3945_antenna)ant;
33438c2ecf20Sopenharmony_ci	} else
33448c2ecf20Sopenharmony_ci		D_INFO("Bad antenna select value %d.\n", ant);
33458c2ecf20Sopenharmony_ci
33468c2ecf20Sopenharmony_ci	return count;
33478c2ecf20Sopenharmony_ci}
33488c2ecf20Sopenharmony_ci
33498c2ecf20Sopenharmony_cistatic DEVICE_ATTR(antenna, 0644, il3945_show_antenna, il3945_store_antenna);
33508c2ecf20Sopenharmony_ci
33518c2ecf20Sopenharmony_cistatic ssize_t
33528c2ecf20Sopenharmony_ciil3945_show_status(struct device *d, struct device_attribute *attr, char *buf)
33538c2ecf20Sopenharmony_ci{
33548c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
33558c2ecf20Sopenharmony_ci	if (!il_is_alive(il))
33568c2ecf20Sopenharmony_ci		return -EAGAIN;
33578c2ecf20Sopenharmony_ci	return sprintf(buf, "0x%08x\n", (int)il->status);
33588c2ecf20Sopenharmony_ci}
33598c2ecf20Sopenharmony_ci
33608c2ecf20Sopenharmony_cistatic DEVICE_ATTR(status, 0444, il3945_show_status, NULL);
33618c2ecf20Sopenharmony_ci
33628c2ecf20Sopenharmony_cistatic ssize_t
33638c2ecf20Sopenharmony_ciil3945_dump_error_log(struct device *d, struct device_attribute *attr,
33648c2ecf20Sopenharmony_ci		      const char *buf, size_t count)
33658c2ecf20Sopenharmony_ci{
33668c2ecf20Sopenharmony_ci	struct il_priv *il = dev_get_drvdata(d);
33678c2ecf20Sopenharmony_ci	char *p = (char *)buf;
33688c2ecf20Sopenharmony_ci
33698c2ecf20Sopenharmony_ci	if (p[0] == '1')
33708c2ecf20Sopenharmony_ci		il3945_dump_nic_error_log(il);
33718c2ecf20Sopenharmony_ci
33728c2ecf20Sopenharmony_ci	return strnlen(buf, count);
33738c2ecf20Sopenharmony_ci}
33748c2ecf20Sopenharmony_ci
33758c2ecf20Sopenharmony_cistatic DEVICE_ATTR(dump_errors, 0200, NULL, il3945_dump_error_log);
33768c2ecf20Sopenharmony_ci
33778c2ecf20Sopenharmony_ci/*****************************************************************************
33788c2ecf20Sopenharmony_ci *
33798c2ecf20Sopenharmony_ci * driver setup and tear down
33808c2ecf20Sopenharmony_ci *
33818c2ecf20Sopenharmony_ci *****************************************************************************/
33828c2ecf20Sopenharmony_ci
33838c2ecf20Sopenharmony_cistatic int
33848c2ecf20Sopenharmony_ciil3945_setup_deferred_work(struct il_priv *il)
33858c2ecf20Sopenharmony_ci{
33868c2ecf20Sopenharmony_ci	il->workqueue = create_singlethread_workqueue(DRV_NAME);
33878c2ecf20Sopenharmony_ci	if (!il->workqueue)
33888c2ecf20Sopenharmony_ci		return -ENOMEM;
33898c2ecf20Sopenharmony_ci
33908c2ecf20Sopenharmony_ci	init_waitqueue_head(&il->wait_command_queue);
33918c2ecf20Sopenharmony_ci
33928c2ecf20Sopenharmony_ci	INIT_WORK(&il->restart, il3945_bg_restart);
33938c2ecf20Sopenharmony_ci	INIT_WORK(&il->rx_replenish, il3945_bg_rx_replenish);
33948c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&il->init_alive_start, il3945_bg_init_alive_start);
33958c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&il->alive_start, il3945_bg_alive_start);
33968c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&il->_3945.rfkill_poll, il3945_rfkill_poll);
33978c2ecf20Sopenharmony_ci
33988c2ecf20Sopenharmony_ci	il_setup_scan_deferred_work(il);
33998c2ecf20Sopenharmony_ci
34008c2ecf20Sopenharmony_ci	il3945_hw_setup_deferred_work(il);
34018c2ecf20Sopenharmony_ci
34028c2ecf20Sopenharmony_ci	timer_setup(&il->watchdog, il_bg_watchdog, 0);
34038c2ecf20Sopenharmony_ci
34048c2ecf20Sopenharmony_ci	tasklet_setup(&il->irq_tasklet, il3945_irq_tasklet);
34058c2ecf20Sopenharmony_ci
34068c2ecf20Sopenharmony_ci	return 0;
34078c2ecf20Sopenharmony_ci}
34088c2ecf20Sopenharmony_ci
34098c2ecf20Sopenharmony_cistatic void
34108c2ecf20Sopenharmony_ciil3945_cancel_deferred_work(struct il_priv *il)
34118c2ecf20Sopenharmony_ci{
34128c2ecf20Sopenharmony_ci	il3945_hw_cancel_deferred_work(il);
34138c2ecf20Sopenharmony_ci
34148c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&il->init_alive_start);
34158c2ecf20Sopenharmony_ci	cancel_delayed_work(&il->alive_start);
34168c2ecf20Sopenharmony_ci
34178c2ecf20Sopenharmony_ci	il_cancel_scan_deferred_work(il);
34188c2ecf20Sopenharmony_ci}
34198c2ecf20Sopenharmony_ci
34208c2ecf20Sopenharmony_cistatic struct attribute *il3945_sysfs_entries[] = {
34218c2ecf20Sopenharmony_ci	&dev_attr_antenna.attr,
34228c2ecf20Sopenharmony_ci	&dev_attr_channels.attr,
34238c2ecf20Sopenharmony_ci	&dev_attr_dump_errors.attr,
34248c2ecf20Sopenharmony_ci	&dev_attr_flags.attr,
34258c2ecf20Sopenharmony_ci	&dev_attr_filter_flags.attr,
34268c2ecf20Sopenharmony_ci	&dev_attr_measurement.attr,
34278c2ecf20Sopenharmony_ci	&dev_attr_retry_rate.attr,
34288c2ecf20Sopenharmony_ci	&dev_attr_status.attr,
34298c2ecf20Sopenharmony_ci	&dev_attr_temperature.attr,
34308c2ecf20Sopenharmony_ci	&dev_attr_tx_power.attr,
34318c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLEGACY_DEBUG
34328c2ecf20Sopenharmony_ci	&dev_attr_debug_level.attr,
34338c2ecf20Sopenharmony_ci#endif
34348c2ecf20Sopenharmony_ci	NULL
34358c2ecf20Sopenharmony_ci};
34368c2ecf20Sopenharmony_ci
34378c2ecf20Sopenharmony_cistatic const struct attribute_group il3945_attribute_group = {
34388c2ecf20Sopenharmony_ci	.name = NULL,		/* put in device directory */
34398c2ecf20Sopenharmony_ci	.attrs = il3945_sysfs_entries,
34408c2ecf20Sopenharmony_ci};
34418c2ecf20Sopenharmony_ci
34428c2ecf20Sopenharmony_cistatic struct ieee80211_ops il3945_mac_ops __ro_after_init = {
34438c2ecf20Sopenharmony_ci	.tx = il3945_mac_tx,
34448c2ecf20Sopenharmony_ci	.start = il3945_mac_start,
34458c2ecf20Sopenharmony_ci	.stop = il3945_mac_stop,
34468c2ecf20Sopenharmony_ci	.add_interface = il_mac_add_interface,
34478c2ecf20Sopenharmony_ci	.remove_interface = il_mac_remove_interface,
34488c2ecf20Sopenharmony_ci	.change_interface = il_mac_change_interface,
34498c2ecf20Sopenharmony_ci	.config = il_mac_config,
34508c2ecf20Sopenharmony_ci	.configure_filter = il3945_configure_filter,
34518c2ecf20Sopenharmony_ci	.set_key = il3945_mac_set_key,
34528c2ecf20Sopenharmony_ci	.conf_tx = il_mac_conf_tx,
34538c2ecf20Sopenharmony_ci	.reset_tsf = il_mac_reset_tsf,
34548c2ecf20Sopenharmony_ci	.bss_info_changed = il_mac_bss_info_changed,
34558c2ecf20Sopenharmony_ci	.hw_scan = il_mac_hw_scan,
34568c2ecf20Sopenharmony_ci	.sta_add = il3945_mac_sta_add,
34578c2ecf20Sopenharmony_ci	.sta_remove = il_mac_sta_remove,
34588c2ecf20Sopenharmony_ci	.tx_last_beacon = il_mac_tx_last_beacon,
34598c2ecf20Sopenharmony_ci	.flush = il_mac_flush,
34608c2ecf20Sopenharmony_ci};
34618c2ecf20Sopenharmony_ci
34628c2ecf20Sopenharmony_cistatic int
34638c2ecf20Sopenharmony_ciil3945_init_drv(struct il_priv *il)
34648c2ecf20Sopenharmony_ci{
34658c2ecf20Sopenharmony_ci	int ret;
34668c2ecf20Sopenharmony_ci	struct il3945_eeprom *eeprom = (struct il3945_eeprom *)il->eeprom;
34678c2ecf20Sopenharmony_ci
34688c2ecf20Sopenharmony_ci	il->retry_rate = 1;
34698c2ecf20Sopenharmony_ci	il->beacon_skb = NULL;
34708c2ecf20Sopenharmony_ci
34718c2ecf20Sopenharmony_ci	spin_lock_init(&il->sta_lock);
34728c2ecf20Sopenharmony_ci	spin_lock_init(&il->hcmd_lock);
34738c2ecf20Sopenharmony_ci
34748c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&il->free_frames);
34758c2ecf20Sopenharmony_ci
34768c2ecf20Sopenharmony_ci	mutex_init(&il->mutex);
34778c2ecf20Sopenharmony_ci
34788c2ecf20Sopenharmony_ci	il->ieee_channels = NULL;
34798c2ecf20Sopenharmony_ci	il->ieee_rates = NULL;
34808c2ecf20Sopenharmony_ci	il->band = NL80211_BAND_2GHZ;
34818c2ecf20Sopenharmony_ci
34828c2ecf20Sopenharmony_ci	il->iw_mode = NL80211_IFTYPE_STATION;
34838c2ecf20Sopenharmony_ci	il->missed_beacon_threshold = IL_MISSED_BEACON_THRESHOLD_DEF;
34848c2ecf20Sopenharmony_ci
34858c2ecf20Sopenharmony_ci	/* initialize force reset */
34868c2ecf20Sopenharmony_ci	il->force_reset.reset_duration = IL_DELAY_NEXT_FORCE_FW_RELOAD;
34878c2ecf20Sopenharmony_ci
34888c2ecf20Sopenharmony_ci	if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
34898c2ecf20Sopenharmony_ci		IL_WARN("Unsupported EEPROM version: 0x%04X\n",
34908c2ecf20Sopenharmony_ci			eeprom->version);
34918c2ecf20Sopenharmony_ci		ret = -EINVAL;
34928c2ecf20Sopenharmony_ci		goto err;
34938c2ecf20Sopenharmony_ci	}
34948c2ecf20Sopenharmony_ci	ret = il_init_channel_map(il);
34958c2ecf20Sopenharmony_ci	if (ret) {
34968c2ecf20Sopenharmony_ci		IL_ERR("initializing regulatory failed: %d\n", ret);
34978c2ecf20Sopenharmony_ci		goto err;
34988c2ecf20Sopenharmony_ci	}
34998c2ecf20Sopenharmony_ci
35008c2ecf20Sopenharmony_ci	/* Set up txpower settings in driver for all channels */
35018c2ecf20Sopenharmony_ci	if (il3945_txpower_set_from_eeprom(il)) {
35028c2ecf20Sopenharmony_ci		ret = -EIO;
35038c2ecf20Sopenharmony_ci		goto err_free_channel_map;
35048c2ecf20Sopenharmony_ci	}
35058c2ecf20Sopenharmony_ci
35068c2ecf20Sopenharmony_ci	ret = il_init_geos(il);
35078c2ecf20Sopenharmony_ci	if (ret) {
35088c2ecf20Sopenharmony_ci		IL_ERR("initializing geos failed: %d\n", ret);
35098c2ecf20Sopenharmony_ci		goto err_free_channel_map;
35108c2ecf20Sopenharmony_ci	}
35118c2ecf20Sopenharmony_ci	il3945_init_hw_rates(il, il->ieee_rates);
35128c2ecf20Sopenharmony_ci
35138c2ecf20Sopenharmony_ci	return 0;
35148c2ecf20Sopenharmony_ci
35158c2ecf20Sopenharmony_cierr_free_channel_map:
35168c2ecf20Sopenharmony_ci	il_free_channel_map(il);
35178c2ecf20Sopenharmony_cierr:
35188c2ecf20Sopenharmony_ci	return ret;
35198c2ecf20Sopenharmony_ci}
35208c2ecf20Sopenharmony_ci
35218c2ecf20Sopenharmony_ci#define IL3945_MAX_PROBE_REQUEST	200
35228c2ecf20Sopenharmony_ci
35238c2ecf20Sopenharmony_cistatic int
35248c2ecf20Sopenharmony_ciil3945_setup_mac(struct il_priv *il)
35258c2ecf20Sopenharmony_ci{
35268c2ecf20Sopenharmony_ci	int ret;
35278c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw = il->hw;
35288c2ecf20Sopenharmony_ci
35298c2ecf20Sopenharmony_ci	hw->rate_control_algorithm = "iwl-3945-rs";
35308c2ecf20Sopenharmony_ci	hw->sta_data_size = sizeof(struct il3945_sta_priv);
35318c2ecf20Sopenharmony_ci	hw->vif_data_size = sizeof(struct il_vif_priv);
35328c2ecf20Sopenharmony_ci
35338c2ecf20Sopenharmony_ci	/* Tell mac80211 our characteristics */
35348c2ecf20Sopenharmony_ci	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
35358c2ecf20Sopenharmony_ci	ieee80211_hw_set(hw, SUPPORTS_PS);
35368c2ecf20Sopenharmony_ci	ieee80211_hw_set(hw, SIGNAL_DBM);
35378c2ecf20Sopenharmony_ci	ieee80211_hw_set(hw, SPECTRUM_MGMT);
35388c2ecf20Sopenharmony_ci
35398c2ecf20Sopenharmony_ci	hw->wiphy->interface_modes =
35408c2ecf20Sopenharmony_ci	    BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_ADHOC);
35418c2ecf20Sopenharmony_ci
35428c2ecf20Sopenharmony_ci	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
35438c2ecf20Sopenharmony_ci	hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
35448c2ecf20Sopenharmony_ci				       REGULATORY_DISABLE_BEACON_HINTS;
35458c2ecf20Sopenharmony_ci
35468c2ecf20Sopenharmony_ci	hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
35478c2ecf20Sopenharmony_ci
35488c2ecf20Sopenharmony_ci	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
35498c2ecf20Sopenharmony_ci	/* we create the 802.11 header and a zero-length SSID element */
35508c2ecf20Sopenharmony_ci	hw->wiphy->max_scan_ie_len = IL3945_MAX_PROBE_REQUEST - 24 - 2;
35518c2ecf20Sopenharmony_ci
35528c2ecf20Sopenharmony_ci	/* Default value; 4 EDCA QOS priorities */
35538c2ecf20Sopenharmony_ci	hw->queues = 4;
35548c2ecf20Sopenharmony_ci
35558c2ecf20Sopenharmony_ci	if (il->bands[NL80211_BAND_2GHZ].n_channels)
35568c2ecf20Sopenharmony_ci		il->hw->wiphy->bands[NL80211_BAND_2GHZ] =
35578c2ecf20Sopenharmony_ci		    &il->bands[NL80211_BAND_2GHZ];
35588c2ecf20Sopenharmony_ci
35598c2ecf20Sopenharmony_ci	if (il->bands[NL80211_BAND_5GHZ].n_channels)
35608c2ecf20Sopenharmony_ci		il->hw->wiphy->bands[NL80211_BAND_5GHZ] =
35618c2ecf20Sopenharmony_ci		    &il->bands[NL80211_BAND_5GHZ];
35628c2ecf20Sopenharmony_ci
35638c2ecf20Sopenharmony_ci	il_leds_init(il);
35648c2ecf20Sopenharmony_ci
35658c2ecf20Sopenharmony_ci	wiphy_ext_feature_set(il->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
35668c2ecf20Sopenharmony_ci
35678c2ecf20Sopenharmony_ci	ret = ieee80211_register_hw(il->hw);
35688c2ecf20Sopenharmony_ci	if (ret) {
35698c2ecf20Sopenharmony_ci		IL_ERR("Failed to register hw (error %d)\n", ret);
35708c2ecf20Sopenharmony_ci		return ret;
35718c2ecf20Sopenharmony_ci	}
35728c2ecf20Sopenharmony_ci	il->mac80211_registered = 1;
35738c2ecf20Sopenharmony_ci
35748c2ecf20Sopenharmony_ci	return 0;
35758c2ecf20Sopenharmony_ci}
35768c2ecf20Sopenharmony_ci
35778c2ecf20Sopenharmony_cistatic int
35788c2ecf20Sopenharmony_ciil3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
35798c2ecf20Sopenharmony_ci{
35808c2ecf20Sopenharmony_ci	int err = 0;
35818c2ecf20Sopenharmony_ci	struct il_priv *il;
35828c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw;
35838c2ecf20Sopenharmony_ci	struct il_cfg *cfg = (struct il_cfg *)(ent->driver_data);
35848c2ecf20Sopenharmony_ci	struct il3945_eeprom *eeprom;
35858c2ecf20Sopenharmony_ci	unsigned long flags;
35868c2ecf20Sopenharmony_ci
35878c2ecf20Sopenharmony_ci	/***********************
35888c2ecf20Sopenharmony_ci	 * 1. Allocating HW data
35898c2ecf20Sopenharmony_ci	 * ********************/
35908c2ecf20Sopenharmony_ci
35918c2ecf20Sopenharmony_ci	hw = ieee80211_alloc_hw(sizeof(struct il_priv), &il3945_mac_ops);
35928c2ecf20Sopenharmony_ci	if (!hw) {
35938c2ecf20Sopenharmony_ci		err = -ENOMEM;
35948c2ecf20Sopenharmony_ci		goto out;
35958c2ecf20Sopenharmony_ci	}
35968c2ecf20Sopenharmony_ci	il = hw->priv;
35978c2ecf20Sopenharmony_ci	il->hw = hw;
35988c2ecf20Sopenharmony_ci	SET_IEEE80211_DEV(hw, &pdev->dev);
35998c2ecf20Sopenharmony_ci
36008c2ecf20Sopenharmony_ci	il->cmd_queue = IL39_CMD_QUEUE_NUM;
36018c2ecf20Sopenharmony_ci
36028c2ecf20Sopenharmony_ci	D_INFO("*** LOAD DRIVER ***\n");
36038c2ecf20Sopenharmony_ci	il->cfg = cfg;
36048c2ecf20Sopenharmony_ci	il->ops = &il3945_ops;
36058c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLEGACY_DEBUGFS
36068c2ecf20Sopenharmony_ci	il->debugfs_ops = &il3945_debugfs_ops;
36078c2ecf20Sopenharmony_ci#endif
36088c2ecf20Sopenharmony_ci	il->pci_dev = pdev;
36098c2ecf20Sopenharmony_ci	il->inta_mask = CSR_INI_SET_MASK;
36108c2ecf20Sopenharmony_ci
36118c2ecf20Sopenharmony_ci	/***************************
36128c2ecf20Sopenharmony_ci	 * 2. Initializing PCI bus
36138c2ecf20Sopenharmony_ci	 * *************************/
36148c2ecf20Sopenharmony_ci	pci_disable_link_state(pdev,
36158c2ecf20Sopenharmony_ci			       PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
36168c2ecf20Sopenharmony_ci			       PCIE_LINK_STATE_CLKPM);
36178c2ecf20Sopenharmony_ci
36188c2ecf20Sopenharmony_ci	if (pci_enable_device(pdev)) {
36198c2ecf20Sopenharmony_ci		err = -ENODEV;
36208c2ecf20Sopenharmony_ci		goto out_ieee80211_free_hw;
36218c2ecf20Sopenharmony_ci	}
36228c2ecf20Sopenharmony_ci
36238c2ecf20Sopenharmony_ci	pci_set_master(pdev);
36248c2ecf20Sopenharmony_ci
36258c2ecf20Sopenharmony_ci	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
36268c2ecf20Sopenharmony_ci	if (!err)
36278c2ecf20Sopenharmony_ci		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
36288c2ecf20Sopenharmony_ci	if (err) {
36298c2ecf20Sopenharmony_ci		IL_WARN("No suitable DMA available.\n");
36308c2ecf20Sopenharmony_ci		goto out_pci_disable_device;
36318c2ecf20Sopenharmony_ci	}
36328c2ecf20Sopenharmony_ci
36338c2ecf20Sopenharmony_ci	pci_set_drvdata(pdev, il);
36348c2ecf20Sopenharmony_ci	err = pci_request_regions(pdev, DRV_NAME);
36358c2ecf20Sopenharmony_ci	if (err)
36368c2ecf20Sopenharmony_ci		goto out_pci_disable_device;
36378c2ecf20Sopenharmony_ci
36388c2ecf20Sopenharmony_ci	/***********************
36398c2ecf20Sopenharmony_ci	 * 3. Read REV Register
36408c2ecf20Sopenharmony_ci	 * ********************/
36418c2ecf20Sopenharmony_ci	il->hw_base = pci_ioremap_bar(pdev, 0);
36428c2ecf20Sopenharmony_ci	if (!il->hw_base) {
36438c2ecf20Sopenharmony_ci		err = -ENODEV;
36448c2ecf20Sopenharmony_ci		goto out_pci_release_regions;
36458c2ecf20Sopenharmony_ci	}
36468c2ecf20Sopenharmony_ci
36478c2ecf20Sopenharmony_ci	D_INFO("pci_resource_len = 0x%08llx\n",
36488c2ecf20Sopenharmony_ci	       (unsigned long long)pci_resource_len(pdev, 0));
36498c2ecf20Sopenharmony_ci	D_INFO("pci_resource_base = %p\n", il->hw_base);
36508c2ecf20Sopenharmony_ci
36518c2ecf20Sopenharmony_ci	/* We disable the RETRY_TIMEOUT register (0x41) to keep
36528c2ecf20Sopenharmony_ci	 * PCI Tx retries from interfering with C3 CPU state */
36538c2ecf20Sopenharmony_ci	pci_write_config_byte(pdev, 0x41, 0x00);
36548c2ecf20Sopenharmony_ci
36558c2ecf20Sopenharmony_ci	/* these spin locks will be used in apm_init and EEPROM access
36568c2ecf20Sopenharmony_ci	 * we should init now
36578c2ecf20Sopenharmony_ci	 */
36588c2ecf20Sopenharmony_ci	spin_lock_init(&il->reg_lock);
36598c2ecf20Sopenharmony_ci	spin_lock_init(&il->lock);
36608c2ecf20Sopenharmony_ci
36618c2ecf20Sopenharmony_ci	/*
36628c2ecf20Sopenharmony_ci	 * stop and reset the on-board processor just in case it is in a
36638c2ecf20Sopenharmony_ci	 * strange state ... like being left stranded by a primary kernel
36648c2ecf20Sopenharmony_ci	 * and this is now the kdump kernel trying to start up
36658c2ecf20Sopenharmony_ci	 */
36668c2ecf20Sopenharmony_ci	_il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
36678c2ecf20Sopenharmony_ci
36688c2ecf20Sopenharmony_ci	/***********************
36698c2ecf20Sopenharmony_ci	 * 4. Read EEPROM
36708c2ecf20Sopenharmony_ci	 * ********************/
36718c2ecf20Sopenharmony_ci
36728c2ecf20Sopenharmony_ci	/* Read the EEPROM */
36738c2ecf20Sopenharmony_ci	err = il_eeprom_init(il);
36748c2ecf20Sopenharmony_ci	if (err) {
36758c2ecf20Sopenharmony_ci		IL_ERR("Unable to init EEPROM\n");
36768c2ecf20Sopenharmony_ci		goto out_iounmap;
36778c2ecf20Sopenharmony_ci	}
36788c2ecf20Sopenharmony_ci	/* MAC Address location in EEPROM same for 3945/4965 */
36798c2ecf20Sopenharmony_ci	eeprom = (struct il3945_eeprom *)il->eeprom;
36808c2ecf20Sopenharmony_ci	D_INFO("MAC address: %pM\n", eeprom->mac_address);
36818c2ecf20Sopenharmony_ci	SET_IEEE80211_PERM_ADDR(il->hw, eeprom->mac_address);
36828c2ecf20Sopenharmony_ci
36838c2ecf20Sopenharmony_ci	/***********************
36848c2ecf20Sopenharmony_ci	 * 5. Setup HW Constants
36858c2ecf20Sopenharmony_ci	 * ********************/
36868c2ecf20Sopenharmony_ci	/* Device-specific setup */
36878c2ecf20Sopenharmony_ci	err = il3945_hw_set_hw_params(il);
36888c2ecf20Sopenharmony_ci	if (err) {
36898c2ecf20Sopenharmony_ci		IL_ERR("failed to set hw settings\n");
36908c2ecf20Sopenharmony_ci		goto out_eeprom_free;
36918c2ecf20Sopenharmony_ci	}
36928c2ecf20Sopenharmony_ci
36938c2ecf20Sopenharmony_ci	/***********************
36948c2ecf20Sopenharmony_ci	 * 6. Setup il
36958c2ecf20Sopenharmony_ci	 * ********************/
36968c2ecf20Sopenharmony_ci
36978c2ecf20Sopenharmony_ci	err = il3945_init_drv(il);
36988c2ecf20Sopenharmony_ci	if (err) {
36998c2ecf20Sopenharmony_ci		IL_ERR("initializing driver failed\n");
37008c2ecf20Sopenharmony_ci		goto out_unset_hw_params;
37018c2ecf20Sopenharmony_ci	}
37028c2ecf20Sopenharmony_ci
37038c2ecf20Sopenharmony_ci	IL_INFO("Detected Intel Wireless WiFi Link %s\n", il->cfg->name);
37048c2ecf20Sopenharmony_ci
37058c2ecf20Sopenharmony_ci	/***********************
37068c2ecf20Sopenharmony_ci	 * 7. Setup Services
37078c2ecf20Sopenharmony_ci	 * ********************/
37088c2ecf20Sopenharmony_ci
37098c2ecf20Sopenharmony_ci	spin_lock_irqsave(&il->lock, flags);
37108c2ecf20Sopenharmony_ci	il_disable_interrupts(il);
37118c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&il->lock, flags);
37128c2ecf20Sopenharmony_ci
37138c2ecf20Sopenharmony_ci	pci_enable_msi(il->pci_dev);
37148c2ecf20Sopenharmony_ci
37158c2ecf20Sopenharmony_ci	err = request_irq(il->pci_dev->irq, il_isr, IRQF_SHARED, DRV_NAME, il);
37168c2ecf20Sopenharmony_ci	if (err) {
37178c2ecf20Sopenharmony_ci		IL_ERR("Error allocating IRQ %d\n", il->pci_dev->irq);
37188c2ecf20Sopenharmony_ci		goto out_disable_msi;
37198c2ecf20Sopenharmony_ci	}
37208c2ecf20Sopenharmony_ci
37218c2ecf20Sopenharmony_ci	err = sysfs_create_group(&pdev->dev.kobj, &il3945_attribute_group);
37228c2ecf20Sopenharmony_ci	if (err) {
37238c2ecf20Sopenharmony_ci		IL_ERR("failed to create sysfs device attributes\n");
37248c2ecf20Sopenharmony_ci		goto out_release_irq;
37258c2ecf20Sopenharmony_ci	}
37268c2ecf20Sopenharmony_ci
37278c2ecf20Sopenharmony_ci	il_set_rxon_channel(il, &il->bands[NL80211_BAND_2GHZ].channels[5]);
37288c2ecf20Sopenharmony_ci	err = il3945_setup_deferred_work(il);
37298c2ecf20Sopenharmony_ci	if (err)
37308c2ecf20Sopenharmony_ci		goto out_remove_sysfs;
37318c2ecf20Sopenharmony_ci
37328c2ecf20Sopenharmony_ci	il3945_setup_handlers(il);
37338c2ecf20Sopenharmony_ci	il_power_initialize(il);
37348c2ecf20Sopenharmony_ci
37358c2ecf20Sopenharmony_ci	/*********************************
37368c2ecf20Sopenharmony_ci	 * 8. Setup and Register mac80211
37378c2ecf20Sopenharmony_ci	 * *******************************/
37388c2ecf20Sopenharmony_ci
37398c2ecf20Sopenharmony_ci	il_enable_interrupts(il);
37408c2ecf20Sopenharmony_ci
37418c2ecf20Sopenharmony_ci	err = il3945_setup_mac(il);
37428c2ecf20Sopenharmony_ci	if (err)
37438c2ecf20Sopenharmony_ci		goto out_destroy_workqueue;
37448c2ecf20Sopenharmony_ci
37458c2ecf20Sopenharmony_ci	il_dbgfs_register(il, DRV_NAME);
37468c2ecf20Sopenharmony_ci
37478c2ecf20Sopenharmony_ci	/* Start monitoring the killswitch */
37488c2ecf20Sopenharmony_ci	queue_delayed_work(il->workqueue, &il->_3945.rfkill_poll, 2 * HZ);
37498c2ecf20Sopenharmony_ci
37508c2ecf20Sopenharmony_ci	return 0;
37518c2ecf20Sopenharmony_ci
37528c2ecf20Sopenharmony_ciout_destroy_workqueue:
37538c2ecf20Sopenharmony_ci	destroy_workqueue(il->workqueue);
37548c2ecf20Sopenharmony_ci	il->workqueue = NULL;
37558c2ecf20Sopenharmony_ciout_remove_sysfs:
37568c2ecf20Sopenharmony_ci	sysfs_remove_group(&pdev->dev.kobj, &il3945_attribute_group);
37578c2ecf20Sopenharmony_ciout_release_irq:
37588c2ecf20Sopenharmony_ci	free_irq(il->pci_dev->irq, il);
37598c2ecf20Sopenharmony_ciout_disable_msi:
37608c2ecf20Sopenharmony_ci	pci_disable_msi(il->pci_dev);
37618c2ecf20Sopenharmony_ci	il_free_geos(il);
37628c2ecf20Sopenharmony_ci	il_free_channel_map(il);
37638c2ecf20Sopenharmony_ciout_unset_hw_params:
37648c2ecf20Sopenharmony_ci	il3945_unset_hw_params(il);
37658c2ecf20Sopenharmony_ciout_eeprom_free:
37668c2ecf20Sopenharmony_ci	il_eeprom_free(il);
37678c2ecf20Sopenharmony_ciout_iounmap:
37688c2ecf20Sopenharmony_ci	iounmap(il->hw_base);
37698c2ecf20Sopenharmony_ciout_pci_release_regions:
37708c2ecf20Sopenharmony_ci	pci_release_regions(pdev);
37718c2ecf20Sopenharmony_ciout_pci_disable_device:
37728c2ecf20Sopenharmony_ci	pci_disable_device(pdev);
37738c2ecf20Sopenharmony_ciout_ieee80211_free_hw:
37748c2ecf20Sopenharmony_ci	ieee80211_free_hw(il->hw);
37758c2ecf20Sopenharmony_ciout:
37768c2ecf20Sopenharmony_ci	return err;
37778c2ecf20Sopenharmony_ci}
37788c2ecf20Sopenharmony_ci
37798c2ecf20Sopenharmony_cistatic void
37808c2ecf20Sopenharmony_ciil3945_pci_remove(struct pci_dev *pdev)
37818c2ecf20Sopenharmony_ci{
37828c2ecf20Sopenharmony_ci	struct il_priv *il = pci_get_drvdata(pdev);
37838c2ecf20Sopenharmony_ci	unsigned long flags;
37848c2ecf20Sopenharmony_ci
37858c2ecf20Sopenharmony_ci	if (!il)
37868c2ecf20Sopenharmony_ci		return;
37878c2ecf20Sopenharmony_ci
37888c2ecf20Sopenharmony_ci	D_INFO("*** UNLOAD DRIVER ***\n");
37898c2ecf20Sopenharmony_ci
37908c2ecf20Sopenharmony_ci	il_dbgfs_unregister(il);
37918c2ecf20Sopenharmony_ci
37928c2ecf20Sopenharmony_ci	set_bit(S_EXIT_PENDING, &il->status);
37938c2ecf20Sopenharmony_ci
37948c2ecf20Sopenharmony_ci	il_leds_exit(il);
37958c2ecf20Sopenharmony_ci
37968c2ecf20Sopenharmony_ci	if (il->mac80211_registered) {
37978c2ecf20Sopenharmony_ci		ieee80211_unregister_hw(il->hw);
37988c2ecf20Sopenharmony_ci		il->mac80211_registered = 0;
37998c2ecf20Sopenharmony_ci	} else {
38008c2ecf20Sopenharmony_ci		il3945_down(il);
38018c2ecf20Sopenharmony_ci	}
38028c2ecf20Sopenharmony_ci
38038c2ecf20Sopenharmony_ci	/*
38048c2ecf20Sopenharmony_ci	 * Make sure device is reset to low power before unloading driver.
38058c2ecf20Sopenharmony_ci	 * This may be redundant with il_down(), but there are paths to
38068c2ecf20Sopenharmony_ci	 * run il_down() without calling apm_ops.stop(), and there are
38078c2ecf20Sopenharmony_ci	 * paths to avoid running il_down() at all before leaving driver.
38088c2ecf20Sopenharmony_ci	 * This (inexpensive) call *makes sure* device is reset.
38098c2ecf20Sopenharmony_ci	 */
38108c2ecf20Sopenharmony_ci	il_apm_stop(il);
38118c2ecf20Sopenharmony_ci
38128c2ecf20Sopenharmony_ci	/* make sure we flush any pending irq or
38138c2ecf20Sopenharmony_ci	 * tasklet for the driver
38148c2ecf20Sopenharmony_ci	 */
38158c2ecf20Sopenharmony_ci	spin_lock_irqsave(&il->lock, flags);
38168c2ecf20Sopenharmony_ci	il_disable_interrupts(il);
38178c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&il->lock, flags);
38188c2ecf20Sopenharmony_ci
38198c2ecf20Sopenharmony_ci	il3945_synchronize_irq(il);
38208c2ecf20Sopenharmony_ci
38218c2ecf20Sopenharmony_ci	sysfs_remove_group(&pdev->dev.kobj, &il3945_attribute_group);
38228c2ecf20Sopenharmony_ci
38238c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&il->_3945.rfkill_poll);
38248c2ecf20Sopenharmony_ci
38258c2ecf20Sopenharmony_ci	il3945_dealloc_ucode_pci(il);
38268c2ecf20Sopenharmony_ci
38278c2ecf20Sopenharmony_ci	if (il->rxq.bd)
38288c2ecf20Sopenharmony_ci		il3945_rx_queue_free(il, &il->rxq);
38298c2ecf20Sopenharmony_ci	il3945_hw_txq_ctx_free(il);
38308c2ecf20Sopenharmony_ci
38318c2ecf20Sopenharmony_ci	il3945_unset_hw_params(il);
38328c2ecf20Sopenharmony_ci
38338c2ecf20Sopenharmony_ci	/*netif_stop_queue(dev); */
38348c2ecf20Sopenharmony_ci	flush_workqueue(il->workqueue);
38358c2ecf20Sopenharmony_ci
38368c2ecf20Sopenharmony_ci	/* ieee80211_unregister_hw calls il3945_mac_stop, which flushes
38378c2ecf20Sopenharmony_ci	 * il->workqueue... so we can't take down the workqueue
38388c2ecf20Sopenharmony_ci	 * until now... */
38398c2ecf20Sopenharmony_ci	destroy_workqueue(il->workqueue);
38408c2ecf20Sopenharmony_ci	il->workqueue = NULL;
38418c2ecf20Sopenharmony_ci
38428c2ecf20Sopenharmony_ci	free_irq(pdev->irq, il);
38438c2ecf20Sopenharmony_ci	pci_disable_msi(pdev);
38448c2ecf20Sopenharmony_ci
38458c2ecf20Sopenharmony_ci	iounmap(il->hw_base);
38468c2ecf20Sopenharmony_ci	pci_release_regions(pdev);
38478c2ecf20Sopenharmony_ci	pci_disable_device(pdev);
38488c2ecf20Sopenharmony_ci
38498c2ecf20Sopenharmony_ci	il_free_channel_map(il);
38508c2ecf20Sopenharmony_ci	il_free_geos(il);
38518c2ecf20Sopenharmony_ci	kfree(il->scan_cmd);
38528c2ecf20Sopenharmony_ci	dev_kfree_skb(il->beacon_skb);
38538c2ecf20Sopenharmony_ci	ieee80211_free_hw(il->hw);
38548c2ecf20Sopenharmony_ci}
38558c2ecf20Sopenharmony_ci
38568c2ecf20Sopenharmony_ci/*****************************************************************************
38578c2ecf20Sopenharmony_ci *
38588c2ecf20Sopenharmony_ci * driver and module entry point
38598c2ecf20Sopenharmony_ci *
38608c2ecf20Sopenharmony_ci *****************************************************************************/
38618c2ecf20Sopenharmony_ci
38628c2ecf20Sopenharmony_cistatic struct pci_driver il3945_driver = {
38638c2ecf20Sopenharmony_ci	.name = DRV_NAME,
38648c2ecf20Sopenharmony_ci	.id_table = il3945_hw_card_ids,
38658c2ecf20Sopenharmony_ci	.probe = il3945_pci_probe,
38668c2ecf20Sopenharmony_ci	.remove = il3945_pci_remove,
38678c2ecf20Sopenharmony_ci	.driver.pm = IL_LEGACY_PM_OPS,
38688c2ecf20Sopenharmony_ci};
38698c2ecf20Sopenharmony_ci
38708c2ecf20Sopenharmony_cistatic int __init
38718c2ecf20Sopenharmony_ciil3945_init(void)
38728c2ecf20Sopenharmony_ci{
38738c2ecf20Sopenharmony_ci
38748c2ecf20Sopenharmony_ci	int ret;
38758c2ecf20Sopenharmony_ci	pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
38768c2ecf20Sopenharmony_ci	pr_info(DRV_COPYRIGHT "\n");
38778c2ecf20Sopenharmony_ci
38788c2ecf20Sopenharmony_ci	/*
38798c2ecf20Sopenharmony_ci	 * Disabling hardware scan means that mac80211 will perform scans
38808c2ecf20Sopenharmony_ci	 * "the hard way", rather than using device's scan.
38818c2ecf20Sopenharmony_ci	 */
38828c2ecf20Sopenharmony_ci	if (il3945_mod_params.disable_hw_scan) {
38838c2ecf20Sopenharmony_ci		pr_info("hw_scan is disabled\n");
38848c2ecf20Sopenharmony_ci		il3945_mac_ops.hw_scan = NULL;
38858c2ecf20Sopenharmony_ci	}
38868c2ecf20Sopenharmony_ci
38878c2ecf20Sopenharmony_ci	ret = il3945_rate_control_register();
38888c2ecf20Sopenharmony_ci	if (ret) {
38898c2ecf20Sopenharmony_ci		pr_err("Unable to register rate control algorithm: %d\n", ret);
38908c2ecf20Sopenharmony_ci		return ret;
38918c2ecf20Sopenharmony_ci	}
38928c2ecf20Sopenharmony_ci
38938c2ecf20Sopenharmony_ci	ret = pci_register_driver(&il3945_driver);
38948c2ecf20Sopenharmony_ci	if (ret) {
38958c2ecf20Sopenharmony_ci		pr_err("Unable to initialize PCI module\n");
38968c2ecf20Sopenharmony_ci		goto error_register;
38978c2ecf20Sopenharmony_ci	}
38988c2ecf20Sopenharmony_ci
38998c2ecf20Sopenharmony_ci	return ret;
39008c2ecf20Sopenharmony_ci
39018c2ecf20Sopenharmony_cierror_register:
39028c2ecf20Sopenharmony_ci	il3945_rate_control_unregister();
39038c2ecf20Sopenharmony_ci	return ret;
39048c2ecf20Sopenharmony_ci}
39058c2ecf20Sopenharmony_ci
39068c2ecf20Sopenharmony_cistatic void __exit
39078c2ecf20Sopenharmony_ciil3945_exit(void)
39088c2ecf20Sopenharmony_ci{
39098c2ecf20Sopenharmony_ci	pci_unregister_driver(&il3945_driver);
39108c2ecf20Sopenharmony_ci	il3945_rate_control_unregister();
39118c2ecf20Sopenharmony_ci}
39128c2ecf20Sopenharmony_ci
39138c2ecf20Sopenharmony_ciMODULE_FIRMWARE(IL3945_MODULE_FIRMWARE(IL3945_UCODE_API_MAX));
39148c2ecf20Sopenharmony_ci
39158c2ecf20Sopenharmony_cimodule_param_named(antenna, il3945_mod_params.antenna, int, 0444);
39168c2ecf20Sopenharmony_ciMODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
39178c2ecf20Sopenharmony_cimodule_param_named(swcrypto, il3945_mod_params.sw_crypto, int, 0444);
39188c2ecf20Sopenharmony_ciMODULE_PARM_DESC(swcrypto, "using software crypto (default 1 [software])");
39198c2ecf20Sopenharmony_cimodule_param_named(disable_hw_scan, il3945_mod_params.disable_hw_scan, int,
39208c2ecf20Sopenharmony_ci		   0444);
39218c2ecf20Sopenharmony_ciMODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 1)");
39228c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLEGACY_DEBUG
39238c2ecf20Sopenharmony_cimodule_param_named(debug, il_debug_level, uint, 0644);
39248c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "debug output mask");
39258c2ecf20Sopenharmony_ci#endif
39268c2ecf20Sopenharmony_cimodule_param_named(fw_restart, il3945_mod_params.restart_fw, int, 0444);
39278c2ecf20Sopenharmony_ciMODULE_PARM_DESC(fw_restart, "restart firmware in case of error");
39288c2ecf20Sopenharmony_ci
39298c2ecf20Sopenharmony_cimodule_exit(il3945_exit);
39308c2ecf20Sopenharmony_cimodule_init(il3945_init);
3931